blob: 443ce192ff133837095c3ad54dcfae5852265f08 [file] [log] [blame]
Jia Liubb481f82012-02-28 07:46:26 +00001//===-- MipsISelLowering.cpp - Mips DAG Lowering Implementation -----------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00007//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00009//
10// This file defines the interfaces that Mips uses to lower LLVM code into a
11// selection DAG.
12//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000013//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000014#define DEBUG_TYPE "mips-lower"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000015#include "MipsISelLowering.h"
Craig Topper79aa3412012-03-17 18:46:09 +000016#include "InstPrinter/MipsInstPrinter.h"
17#include "MCTargetDesc/MipsBaseInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000018#include "MipsMachineFunction.h"
19#include "MipsSubtarget.h"
20#include "MipsTargetMachine.h"
21#include "MipsTargetObjectFile.h"
Akira Hatanaka2b861be2012-10-19 21:47:33 +000022#include "llvm/ADT/Statistic.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000023#include "llvm/CodeGen/CallingConvLower.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/MachineFunction.h"
26#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000027#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000028#include "llvm/CodeGen/SelectionDAGISel.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000029#include "llvm/CodeGen/ValueTypes.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000030#include "llvm/IR/CallingConv.h"
31#include "llvm/IR/DerivedTypes.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000032#include "llvm/IR/GlobalVariable.h"
Akira Hatanaka2b861be2012-10-19 21:47:33 +000033#include "llvm/Support/CommandLine.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000034#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000035#include "llvm/Support/ErrorHandling.h"
NAKAMURA Takumi89593932012-04-21 15:31:45 +000036#include "llvm/Support/raw_ostream.h"
Akira Hatanakabfb07b12013-08-14 00:21:25 +000037#include <cctype>
NAKAMURA Takumi89593932012-04-21 15:31:45 +000038
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000039using namespace llvm;
40
Akira Hatanaka2b861be2012-10-19 21:47:33 +000041STATISTIC(NumTailCalls, "Number of tail calls");
42
43static cl::opt<bool>
Akira Hatanaka81784cb2012-11-21 20:21:11 +000044LargeGOT("mxgot", cl::Hidden,
45 cl::desc("MIPS: Enable GOT larger than 64k."), cl::init(false));
46
Akira Hatanakaf8941992013-05-20 18:07:43 +000047static cl::opt<bool>
Akira Hatanaka2591b5c2013-05-21 17:17:59 +000048NoZeroDivCheck("mno-check-zero-division", cl::Hidden,
Akira Hatanakaf8941992013-05-20 18:07:43 +000049 cl::desc("MIPS: Don't trap on integer division by zero."),
50 cl::init(false));
51
Akira Hatanakafe30a9b2012-10-27 00:29:43 +000052static const uint16_t O32IntRegs[4] = {
53 Mips::A0, Mips::A1, Mips::A2, Mips::A3
54};
55
56static const uint16_t Mips64IntRegs[8] = {
57 Mips::A0_64, Mips::A1_64, Mips::A2_64, Mips::A3_64,
58 Mips::T0_64, Mips::T1_64, Mips::T2_64, Mips::T3_64
59};
60
61static const uint16_t Mips64DPRegs[8] = {
62 Mips::D12_64, Mips::D13_64, Mips::D14_64, Mips::D15_64,
63 Mips::D16_64, Mips::D17_64, Mips::D18_64, Mips::D19_64
64};
65
Jia Liubb481f82012-02-28 07:46:26 +000066// If I is a shifted mask, set the size (Size) and the first bit of the
Akira Hatanakadbe9a312011-08-18 20:07:42 +000067// mask (Pos), and return true.
Jia Liubb481f82012-02-28 07:46:26 +000068// For example, if I is 0x003ff800, (Pos, Size) = (11, 11).
Akira Hatanakaf635ef42013-03-12 00:16:36 +000069static bool isShiftedMask(uint64_t I, uint64_t &Pos, uint64_t &Size) {
Akira Hatanakad6bc5232011-12-05 21:26:34 +000070 if (!isShiftedMask_64(I))
Akira Hatanaka854a7db2011-08-19 22:59:00 +000071 return false;
Akira Hatanakabb15e112011-08-17 02:05:42 +000072
Akira Hatanakad6bc5232011-12-05 21:26:34 +000073 Size = CountPopulation_64(I);
Michael J. Spencerc6af2432013-05-24 22:23:49 +000074 Pos = countTrailingZeros(I);
Akira Hatanakadbe9a312011-08-18 20:07:42 +000075 return true;
Akira Hatanakabb15e112011-08-17 02:05:42 +000076}
77
Akira Hatanaka5ac065a2013-03-13 00:54:29 +000078SDValue MipsTargetLowering::getGlobalReg(SelectionDAG &DAG, EVT Ty) const {
Akira Hatanaka648f00c2012-02-24 22:34:47 +000079 MipsFunctionInfo *FI = DAG.getMachineFunction().getInfo<MipsFunctionInfo>();
80 return DAG.getRegister(FI->getGlobalBaseReg(), Ty);
81}
82
Akira Hatanaka6b28b802012-11-21 20:26:38 +000083static SDValue getTargetNode(SDValue Op, SelectionDAG &DAG, unsigned Flag) {
84 EVT Ty = Op.getValueType();
85
86 if (GlobalAddressSDNode *N = dyn_cast<GlobalAddressSDNode>(Op))
Andrew Trickac6d9be2013-05-25 02:42:55 +000087 return DAG.getTargetGlobalAddress(N->getGlobal(), SDLoc(Op), Ty, 0,
Akira Hatanaka6b28b802012-11-21 20:26:38 +000088 Flag);
89 if (ExternalSymbolSDNode *N = dyn_cast<ExternalSymbolSDNode>(Op))
90 return DAG.getTargetExternalSymbol(N->getSymbol(), Ty, Flag);
91 if (BlockAddressSDNode *N = dyn_cast<BlockAddressSDNode>(Op))
92 return DAG.getTargetBlockAddress(N->getBlockAddress(), Ty, 0, Flag);
93 if (JumpTableSDNode *N = dyn_cast<JumpTableSDNode>(Op))
94 return DAG.getTargetJumpTable(N->getIndex(), Ty, Flag);
95 if (ConstantPoolSDNode *N = dyn_cast<ConstantPoolSDNode>(Op))
96 return DAG.getTargetConstantPool(N->getConstVal(), Ty, N->getAlignment(),
97 N->getOffset(), Flag);
98
99 llvm_unreachable("Unexpected node type.");
100 return SDValue();
101}
102
103static SDValue getAddrNonPIC(SDValue Op, SelectionDAG &DAG) {
Andrew Trickac6d9be2013-05-25 02:42:55 +0000104 SDLoc DL(Op);
Akira Hatanaka6b28b802012-11-21 20:26:38 +0000105 EVT Ty = Op.getValueType();
106 SDValue Hi = getTargetNode(Op, DAG, MipsII::MO_ABS_HI);
107 SDValue Lo = getTargetNode(Op, DAG, MipsII::MO_ABS_LO);
108 return DAG.getNode(ISD::ADD, DL, Ty,
109 DAG.getNode(MipsISD::Hi, DL, Ty, Hi),
110 DAG.getNode(MipsISD::Lo, DL, Ty, Lo));
111}
112
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000113SDValue MipsTargetLowering::getAddrLocal(SDValue Op, SelectionDAG &DAG,
114 bool HasMips64) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +0000115 SDLoc DL(Op);
Akira Hatanaka6b28b802012-11-21 20:26:38 +0000116 EVT Ty = Op.getValueType();
117 unsigned GOTFlag = HasMips64 ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT;
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000118 SDValue GOT = DAG.getNode(MipsISD::Wrapper, DL, Ty, getGlobalReg(DAG, Ty),
Akira Hatanaka6b28b802012-11-21 20:26:38 +0000119 getTargetNode(Op, DAG, GOTFlag));
120 SDValue Load = DAG.getLoad(Ty, DL, DAG.getEntryNode(), GOT,
121 MachinePointerInfo::getGOT(), false, false, false,
122 0);
123 unsigned LoFlag = HasMips64 ? MipsII::MO_GOT_OFST : MipsII::MO_ABS_LO;
124 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, Ty, getTargetNode(Op, DAG, LoFlag));
125 return DAG.getNode(ISD::ADD, DL, Ty, Load, Lo);
126}
127
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000128SDValue MipsTargetLowering::getAddrGlobal(SDValue Op, SelectionDAG &DAG,
129 unsigned Flag) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +0000130 SDLoc DL(Op);
Akira Hatanaka6b28b802012-11-21 20:26:38 +0000131 EVT Ty = Op.getValueType();
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000132 SDValue Tgt = DAG.getNode(MipsISD::Wrapper, DL, Ty, getGlobalReg(DAG, Ty),
Akira Hatanaka6b28b802012-11-21 20:26:38 +0000133 getTargetNode(Op, DAG, Flag));
134 return DAG.getLoad(Ty, DL, DAG.getEntryNode(), Tgt,
135 MachinePointerInfo::getGOT(), false, false, false, 0);
136}
137
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000138SDValue MipsTargetLowering::getAddrGlobalLargeGOT(SDValue Op, SelectionDAG &DAG,
139 unsigned HiFlag,
140 unsigned LoFlag) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +0000141 SDLoc DL(Op);
Akira Hatanaka6b28b802012-11-21 20:26:38 +0000142 EVT Ty = Op.getValueType();
143 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, Ty, getTargetNode(Op, DAG, HiFlag));
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000144 Hi = DAG.getNode(ISD::ADD, DL, Ty, Hi, getGlobalReg(DAG, Ty));
Akira Hatanaka6b28b802012-11-21 20:26:38 +0000145 SDValue Wrapper = DAG.getNode(MipsISD::Wrapper, DL, Ty, Hi,
146 getTargetNode(Op, DAG, LoFlag));
147 return DAG.getLoad(Ty, DL, DAG.getEntryNode(), Wrapper,
148 MachinePointerInfo::getGOT(), false, false, false, 0);
149}
150
Chris Lattnerf0144122009-07-28 03:13:23 +0000151const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
152 switch (Opcode) {
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000153 case MipsISD::JmpLink: return "MipsISD::JmpLink";
Akira Hatanaka58d1e3f2012-10-19 20:59:39 +0000154 case MipsISD::TailCall: return "MipsISD::TailCall";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000155 case MipsISD::Hi: return "MipsISD::Hi";
156 case MipsISD::Lo: return "MipsISD::Lo";
157 case MipsISD::GPRel: return "MipsISD::GPRel";
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +0000158 case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000159 case MipsISD::Ret: return "MipsISD::Ret";
Akira Hatanaka544cc212013-01-30 00:26:49 +0000160 case MipsISD::EH_RETURN: return "MipsISD::EH_RETURN";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000161 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
162 case MipsISD::FPCmp: return "MipsISD::FPCmp";
163 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
164 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +0000165 case MipsISD::TruncIntFP: return "MipsISD::TruncIntFP";
Akira Hatanakadd958922013-03-30 01:14:04 +0000166 case MipsISD::ExtractLOHI: return "MipsISD::ExtractLOHI";
167 case MipsISD::InsertLOHI: return "MipsISD::InsertLOHI";
168 case MipsISD::Mult: return "MipsISD::Mult";
169 case MipsISD::Multu: return "MipsISD::Multu";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000170 case MipsISD::MAdd: return "MipsISD::MAdd";
171 case MipsISD::MAddu: return "MipsISD::MAddu";
172 case MipsISD::MSub: return "MipsISD::MSub";
173 case MipsISD::MSubu: return "MipsISD::MSubu";
174 case MipsISD::DivRem: return "MipsISD::DivRem";
175 case MipsISD::DivRemU: return "MipsISD::DivRemU";
Akira Hatanakadd958922013-03-30 01:14:04 +0000176 case MipsISD::DivRem16: return "MipsISD::DivRem16";
177 case MipsISD::DivRemU16: return "MipsISD::DivRemU16";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000178 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
179 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
Akira Hatanakabfcb83f2011-12-12 22:38:19 +0000180 case MipsISD::Wrapper: return "MipsISD::Wrapper";
Akira Hatanakadb548262011-07-19 23:30:50 +0000181 case MipsISD::Sync: return "MipsISD::Sync";
Akira Hatanakabb15e112011-08-17 02:05:42 +0000182 case MipsISD::Ext: return "MipsISD::Ext";
183 case MipsISD::Ins: return "MipsISD::Ins";
Akira Hatanakab6f1dc22012-06-02 00:03:12 +0000184 case MipsISD::LWL: return "MipsISD::LWL";
185 case MipsISD::LWR: return "MipsISD::LWR";
186 case MipsISD::SWL: return "MipsISD::SWL";
187 case MipsISD::SWR: return "MipsISD::SWR";
188 case MipsISD::LDL: return "MipsISD::LDL";
189 case MipsISD::LDR: return "MipsISD::LDR";
190 case MipsISD::SDL: return "MipsISD::SDL";
191 case MipsISD::SDR: return "MipsISD::SDR";
Akira Hatanaka6fad5e72012-09-21 23:52:47 +0000192 case MipsISD::EXTP: return "MipsISD::EXTP";
193 case MipsISD::EXTPDP: return "MipsISD::EXTPDP";
194 case MipsISD::EXTR_S_H: return "MipsISD::EXTR_S_H";
195 case MipsISD::EXTR_W: return "MipsISD::EXTR_W";
196 case MipsISD::EXTR_R_W: return "MipsISD::EXTR_R_W";
197 case MipsISD::EXTR_RS_W: return "MipsISD::EXTR_RS_W";
198 case MipsISD::SHILO: return "MipsISD::SHILO";
199 case MipsISD::MTHLIP: return "MipsISD::MTHLIP";
200 case MipsISD::MULT: return "MipsISD::MULT";
201 case MipsISD::MULTU: return "MipsISD::MULTU";
Jia Liub3ea8802013-03-04 01:06:54 +0000202 case MipsISD::MADD_DSP: return "MipsISD::MADD_DSP";
Akira Hatanaka6fad5e72012-09-21 23:52:47 +0000203 case MipsISD::MADDU_DSP: return "MipsISD::MADDU_DSP";
204 case MipsISD::MSUB_DSP: return "MipsISD::MSUB_DSP";
205 case MipsISD::MSUBU_DSP: return "MipsISD::MSUBU_DSP";
Akira Hatanaka97a62bf2013-04-19 23:21:32 +0000206 case MipsISD::SHLL_DSP: return "MipsISD::SHLL_DSP";
207 case MipsISD::SHRA_DSP: return "MipsISD::SHRA_DSP";
208 case MipsISD::SHRL_DSP: return "MipsISD::SHRL_DSP";
Akira Hatanakacd6c5792013-04-30 22:37:26 +0000209 case MipsISD::SETCC_DSP: return "MipsISD::SETCC_DSP";
210 case MipsISD::SELECT_CC_DSP: return "MipsISD::SELECT_CC_DSP";
Daniel Sanders3c380d52013-08-28 12:14:50 +0000211 case MipsISD::VALL_ZERO: return "MipsISD::VALL_ZERO";
212 case MipsISD::VANY_ZERO: return "MipsISD::VANY_ZERO";
213 case MipsISD::VALL_NONZERO: return "MipsISD::VALL_NONZERO";
214 case MipsISD::VANY_NONZERO: return "MipsISD::VANY_NONZERO";
Daniel Sandersda521cc2013-09-23 12:02:46 +0000215 case MipsISD::VSPLAT: return "MipsISD::VSPLAT";
216 case MipsISD::VSPLATD: return "MipsISD::VSPLATD";
Daniel Sanders9a1aaeb2013-09-23 14:03:12 +0000217 case MipsISD::VEXTRACT_SEXT_ELT: return "MipsISD::VEXTRACT_SEXT_ELT";
218 case MipsISD::VEXTRACT_ZEXT_ELT: return "MipsISD::VEXTRACT_ZEXT_ELT";
Daniel Sanders915432c2013-09-23 13:22:24 +0000219 case MipsISD::VNOR: return "MipsISD::VNOR";
Akira Hatanaka0f843822011-06-07 18:58:42 +0000220 default: return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000221 }
222}
223
224MipsTargetLowering::
Chris Lattnerf0144122009-07-28 03:13:23 +0000225MipsTargetLowering(MipsTargetMachine &TM)
Akira Hatanaka8b4198d2011-09-26 21:47:02 +0000226 : TargetLowering(TM, new MipsTargetObjectFile()),
227 Subtarget(&TM.getSubtarget<MipsSubtarget>()),
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +0000228 HasMips64(Subtarget->hasMips64()), IsN64(Subtarget->isABI_N64()),
229 IsO32(Subtarget->isABI_O32()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000230 // Mips does not have i1 type, so use i32 for
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000231 // setcc operations results (slt, sgt, ...).
Duncan Sands03228082008-11-23 15:47:28 +0000232 setBooleanContents(ZeroOrOneBooleanContent);
Akira Hatanakacd6c5792013-04-30 22:37:26 +0000233 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000234
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000235 // Load extented operations for i1 types must be promoted
Owen Anderson825b72b2009-08-11 20:47:22 +0000236 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
237 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
238 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000239
Eli Friedman6055a6a2009-07-17 04:07:24 +0000240 // MIPS doesn't have extending float->double load/store
Owen Anderson825b72b2009-08-11 20:47:22 +0000241 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
242 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman10a36592009-07-17 02:28:12 +0000243
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000244 // Used by legalize types to correctly generate the setcc result.
245 // Without this, every float setcc comes with a AND/OR with the result,
246 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000247 // which is used implicitly by brcond and select operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000248 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000249
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000250 // Mips Custom Operations
Akira Hatanakab7656a92013-03-06 21:32:03 +0000251 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000252 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000253 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000254 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
255 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
256 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
257 setOperationAction(ISD::SELECT, MVT::f32, Custom);
258 setOperationAction(ISD::SELECT, MVT::f64, Custom);
259 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Akira Hatanaka3fef29d2012-07-11 19:32:27 +0000260 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
261 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Akira Hatanaka0a40c232012-03-09 23:46:03 +0000262 setOperationAction(ISD::SETCC, MVT::f32, Custom);
263 setOperationAction(ISD::SETCC, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000264 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000265 setOperationAction(ISD::VASTART, MVT::Other, Custom);
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000266 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
267 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +0000268 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000269
Akira Hatanakac12a6e62012-04-11 22:49:04 +0000270 if (!TM.Options.NoNaNsFPMath) {
271 setOperationAction(ISD::FABS, MVT::f32, Custom);
272 setOperationAction(ISD::FABS, MVT::f64, Custom);
273 }
274
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000275 if (HasMips64) {
276 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
277 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
278 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
279 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
280 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
281 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Akira Hatanaka7664f052012-06-02 00:04:42 +0000282 setOperationAction(ISD::LOAD, MVT::i64, Custom);
283 setOperationAction(ISD::STORE, MVT::i64, Custom);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +0000284 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000285 }
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000286
Akira Hatanakaa284acb2012-05-09 00:55:21 +0000287 if (!HasMips64) {
288 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
289 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
290 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
291 }
292
Akira Hatanakae90a3bc2012-11-07 19:10:58 +0000293 setOperationAction(ISD::ADD, MVT::i32, Custom);
294 if (HasMips64)
295 setOperationAction(ISD::ADD, MVT::i64, Custom);
296
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000297 setOperationAction(ISD::SDIV, MVT::i32, Expand);
298 setOperationAction(ISD::SREM, MVT::i32, Expand);
299 setOperationAction(ISD::UDIV, MVT::i32, Expand);
300 setOperationAction(ISD::UREM, MVT::i32, Expand);
Akira Hatanakadda4a072011-10-03 21:06:13 +0000301 setOperationAction(ISD::SDIV, MVT::i64, Expand);
302 setOperationAction(ISD::SREM, MVT::i64, Expand);
303 setOperationAction(ISD::UDIV, MVT::i64, Expand);
304 setOperationAction(ISD::UREM, MVT::i64, Expand);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000305
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000306 // Operations not directly supported by Mips.
Tom Stellard3ef53832013-03-08 15:36:57 +0000307 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
308 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
309 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
310 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000311 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
312 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Akira Hatanakae1bcd6b2011-12-20 23:40:56 +0000313 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000314 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Akira Hatanakae1bcd6b2011-12-20 23:40:56 +0000315 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000316 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
317 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
Akira Hatanaka7f162742011-12-21 00:14:05 +0000318 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000319 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
Akira Hatanaka7f162742011-12-21 00:14:05 +0000320 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000321 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
322 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
323 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
324 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000325 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000326 setOperationAction(ISD::ROTL, MVT::i64, Expand);
Akira Hatanaka1d165f12012-07-31 20:54:48 +0000327 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
328 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000329
Akira Hatanaka56633442011-09-20 23:53:09 +0000330 if (!Subtarget->hasMips32r2())
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000331 setOperationAction(ISD::ROTR, MVT::i32, Expand);
332
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000333 if (!Subtarget->hasMips64r2())
334 setOperationAction(ISD::ROTR, MVT::i64, Expand);
335
Owen Anderson825b72b2009-08-11 20:47:22 +0000336 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000337 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000338 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000339 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng8688a582013-01-29 02:32:37 +0000340 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
341 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000342 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
343 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanaka46da1362011-05-23 22:23:58 +0000344 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000345 setOperationAction(ISD::FLOG, MVT::f32, Expand);
346 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
347 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
348 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000349 setOperationAction(ISD::FMA, MVT::f32, Expand);
350 setOperationAction(ISD::FMA, MVT::f64, Expand);
Akira Hatanaka21ecc2f2012-03-29 18:43:11 +0000351 setOperationAction(ISD::FREM, MVT::f32, Expand);
352 setOperationAction(ISD::FREM, MVT::f64, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000353
Akira Hatanaka1cc63332012-04-11 22:59:08 +0000354 if (!TM.Options.NoNaNsFPMath) {
355 setOperationAction(ISD::FNEG, MVT::f32, Expand);
356 setOperationAction(ISD::FNEG, MVT::f64, Expand);
357 }
358
Akira Hatanaka544cc212013-01-30 00:26:49 +0000359 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
360
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +0000361 setOperationAction(ISD::VAARG, MVT::Other, Expand);
362 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
363 setOperationAction(ISD::VAEND, MVT::Other, Expand);
364
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000365 // Use the default for now
Owen Anderson825b72b2009-08-11 20:47:22 +0000366 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
367 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000368
Jia Liubb481f82012-02-28 07:46:26 +0000369 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
370 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
371 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
372 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
Eli Friedman4db5aca2011-08-29 18:23:02 +0000373
Eli Friedman26689ac2011-08-03 21:06:02 +0000374 setInsertFencesForAtomic(true);
375
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000376 if (!Subtarget->hasSEInReg()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000377 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
378 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000379 }
380
Akira Hatanakac79507a2011-12-21 00:20:27 +0000381 if (!Subtarget->hasBitCount()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000382 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Akira Hatanakac79507a2011-12-21 00:20:27 +0000383 setOperationAction(ISD::CTLZ, MVT::i64, Expand);
384 }
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000385
Akira Hatanakac0ea0432011-12-20 23:56:43 +0000386 if (!Subtarget->hasSwap()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000387 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Akira Hatanakac0ea0432011-12-20 23:56:43 +0000388 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
389 }
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000390
Akira Hatanaka7664f052012-06-02 00:04:42 +0000391 if (HasMips64) {
392 setLoadExtAction(ISD::SEXTLOAD, MVT::i32, Custom);
393 setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, Custom);
394 setLoadExtAction(ISD::EXTLOAD, MVT::i32, Custom);
395 setTruncStoreAction(MVT::i64, MVT::i32, Custom);
396 }
397
Akira Hatanaka97585622013-07-26 20:58:55 +0000398 setOperationAction(ISD::TRAP, MVT::Other, Legal);
399
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000400 setTargetDAGCombine(ISD::SDIVREM);
401 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanakaee8c3b02012-03-08 03:26:37 +0000402 setTargetDAGCombine(ISD::SELECT);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000403 setTargetDAGCombine(ISD::AND);
404 setTargetDAGCombine(ISD::OR);
Akira Hatanaka87827072012-06-13 20:33:18 +0000405 setTargetDAGCombine(ISD::ADD);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000406
Akira Hatanaka5fdf5002012-03-08 01:59:33 +0000407 setMinFunctionAlignment(HasMips64 ? 3 : 2);
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000408
Akira Hatanaka3f5b1072012-02-02 03:17:04 +0000409 setStackPointerRegisterToSaveRestore(IsN64 ? Mips::SP_64 : Mips::SP);
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000410
Akira Hatanaka590baca2012-02-02 03:13:40 +0000411 setExceptionPointerRegister(IsN64 ? Mips::A0_64 : Mips::A0);
412 setExceptionSelectorRegister(IsN64 ? Mips::A1_64 : Mips::A1);
Akira Hatanakae193b322012-06-13 19:33:32 +0000413
Jim Grosbach3450f802013-02-20 21:13:59 +0000414 MaxStoresPerMemcpy = 16;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000415}
416
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000417const MipsTargetLowering *MipsTargetLowering::create(MipsTargetMachine &TM) {
418 if (TM.getSubtargetImpl()->inMips16Mode())
419 return llvm::createMips16TargetLowering(TM);
Jia Liubb481f82012-02-28 07:46:26 +0000420
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000421 return llvm::createMipsSETargetLowering(TM);
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000422}
423
Matt Arsenault225ed702013-05-18 00:21:46 +0000424EVT MipsTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Akira Hatanakae13f4412013-01-04 20:06:01 +0000425 if (!VT.isVector())
426 return MVT::i32;
427 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +0000428}
429
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000430static SDValue performDivRemCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000431 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000432 const MipsSubtarget *Subtarget) {
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000433 if (DCI.isBeforeLegalizeOps())
434 return SDValue();
435
Akira Hatanakadda4a072011-10-03 21:06:13 +0000436 EVT Ty = N->getValueType(0);
Akira Hatanakacbaf6d02013-08-14 00:47:08 +0000437 unsigned LO = (Ty == MVT::i32) ? Mips::LO0 : Mips::LO0_64;
438 unsigned HI = (Ty == MVT::i32) ? Mips::HI0 : Mips::HI0_64;
Akira Hatanakaf5926fd2013-03-30 01:36:35 +0000439 unsigned Opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem16 :
440 MipsISD::DivRemU16;
Andrew Trickac6d9be2013-05-25 02:42:55 +0000441 SDLoc DL(N);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000442
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000443 SDValue DivRem = DAG.getNode(Opc, DL, MVT::Glue,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000444 N->getOperand(0), N->getOperand(1));
445 SDValue InChain = DAG.getEntryNode();
446 SDValue InGlue = DivRem;
447
448 // insert MFLO
449 if (N->hasAnyUseOfValue(0)) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000450 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, DL, LO, Ty,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000451 InGlue);
452 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
453 InChain = CopyFromLo.getValue(1);
454 InGlue = CopyFromLo.getValue(2);
455 }
456
457 // insert MFHI
458 if (N->hasAnyUseOfValue(1)) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000459 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, DL,
Akira Hatanakadda4a072011-10-03 21:06:13 +0000460 HI, Ty, InGlue);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000461 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
462 }
463
464 return SDValue();
465}
466
Akira Hatanaka2fbe90c2013-04-18 01:00:46 +0000467static Mips::CondCode condCodeToFCC(ISD::CondCode CC) {
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000468 switch (CC) {
469 default: llvm_unreachable("Unknown fp condition code!");
470 case ISD::SETEQ:
471 case ISD::SETOEQ: return Mips::FCOND_OEQ;
472 case ISD::SETUNE: return Mips::FCOND_UNE;
473 case ISD::SETLT:
474 case ISD::SETOLT: return Mips::FCOND_OLT;
475 case ISD::SETGT:
476 case ISD::SETOGT: return Mips::FCOND_OGT;
477 case ISD::SETLE:
478 case ISD::SETOLE: return Mips::FCOND_OLE;
479 case ISD::SETGE:
480 case ISD::SETOGE: return Mips::FCOND_OGE;
481 case ISD::SETULT: return Mips::FCOND_ULT;
482 case ISD::SETULE: return Mips::FCOND_ULE;
483 case ISD::SETUGT: return Mips::FCOND_UGT;
484 case ISD::SETUGE: return Mips::FCOND_UGE;
485 case ISD::SETUO: return Mips::FCOND_UN;
486 case ISD::SETO: return Mips::FCOND_OR;
487 case ISD::SETNE:
488 case ISD::SETONE: return Mips::FCOND_ONE;
489 case ISD::SETUEQ: return Mips::FCOND_UEQ;
490 }
491}
492
493
Akira Hatanaka9cf07242013-03-30 01:16:38 +0000494/// This function returns true if the floating point conditional branches and
495/// conditional moves which use condition code CC should be inverted.
496static bool invertFPCondCodeUser(Mips::CondCode CC) {
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000497 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
498 return false;
499
Akira Hatanaka82099682011-12-19 19:52:25 +0000500 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
501 "Illegal Condition Code");
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000502
Akira Hatanaka82099682011-12-19 19:52:25 +0000503 return true;
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000504}
505
506// Creates and returns an FPCmp node from a setcc node.
507// Returns Op if setcc is not a floating point comparison.
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000508static SDValue createFPCmp(SelectionDAG &DAG, const SDValue &Op) {
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000509 // must be a SETCC node
510 if (Op.getOpcode() != ISD::SETCC)
511 return Op;
512
513 SDValue LHS = Op.getOperand(0);
514
515 if (!LHS.getValueType().isFloatingPoint())
516 return Op;
517
518 SDValue RHS = Op.getOperand(1);
Andrew Trickac6d9be2013-05-25 02:42:55 +0000519 SDLoc DL(Op);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000520
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000521 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
522 // node if necessary.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000523 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
524
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000525 return DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS,
Akira Hatanaka2fbe90c2013-04-18 01:00:46 +0000526 DAG.getConstant(condCodeToFCC(CC), MVT::i32));
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000527}
528
529// Creates and returns a CMovFPT/F node.
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000530static SDValue createCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000531 SDValue False, SDLoc DL) {
Akira Hatanaka9cf07242013-03-30 01:16:38 +0000532 ConstantSDNode *CC = cast<ConstantSDNode>(Cond.getOperand(2));
533 bool invert = invertFPCondCodeUser((Mips::CondCode)CC->getSExtValue());
Akira Hatanaka407883b2013-07-26 20:51:20 +0000534 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000535
536 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
Akira Hatanaka407883b2013-07-26 20:51:20 +0000537 True.getValueType(), True, FCC0, False, Cond);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000538}
539
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000540static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000541 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000542 const MipsSubtarget *Subtarget) {
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000543 if (DCI.isBeforeLegalizeOps())
544 return SDValue();
545
546 SDValue SetCC = N->getOperand(0);
547
548 if ((SetCC.getOpcode() != ISD::SETCC) ||
549 !SetCC.getOperand(0).getValueType().isInteger())
550 return SDValue();
551
552 SDValue False = N->getOperand(2);
553 EVT FalseTy = False.getValueType();
554
555 if (!FalseTy.isInteger())
556 return SDValue();
557
558 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(False);
559
560 if (!CN || CN->getZExtValue())
561 return SDValue();
562
Andrew Trickac6d9be2013-05-25 02:42:55 +0000563 const SDLoc DL(N);
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000564 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
565 SDValue True = N->getOperand(1);
Akira Hatanaka864f6602012-06-14 21:10:56 +0000566
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000567 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
568 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
Akira Hatanaka864f6602012-06-14 21:10:56 +0000569
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000570 return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True);
571}
572
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000573static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000574 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000575 const MipsSubtarget *Subtarget) {
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000576 // Pattern match EXT.
577 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
578 // => ext $dst, $src, size, pos
Akira Hatanaka56633442011-09-20 23:53:09 +0000579 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000580 return SDValue();
581
582 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000583 unsigned ShiftRightOpc = ShiftRight.getOpcode();
584
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000585 // Op's first operand must be a shift right.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000586 if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL)
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000587 return SDValue();
588
589 // The second operand of the shift must be an immediate.
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000590 ConstantSDNode *CN;
591 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
592 return SDValue();
Jia Liubb481f82012-02-28 07:46:26 +0000593
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000594 uint64_t Pos = CN->getZExtValue();
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000595 uint64_t SMPos, SMSize;
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000596
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000597 // Op's second operand must be a shifted mask.
598 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000599 !isShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000600 return SDValue();
601
602 // Return if the shifted mask does not start at bit 0 or the sum of its size
603 // and Pos exceeds the word's size.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000604 EVT ValTy = N->getValueType(0);
605 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000606 return SDValue();
607
Andrew Trickac6d9be2013-05-25 02:42:55 +0000608 return DAG.getNode(MipsISD::Ext, SDLoc(N), ValTy,
Akira Hatanaka82099682011-12-19 19:52:25 +0000609 ShiftRight.getOperand(0), DAG.getConstant(Pos, MVT::i32),
Akira Hatanaka667645f2011-08-17 22:59:46 +0000610 DAG.getConstant(SMSize, MVT::i32));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000611}
Jia Liubb481f82012-02-28 07:46:26 +0000612
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000613static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000614 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000615 const MipsSubtarget *Subtarget) {
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000616 // Pattern match INS.
617 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
Jia Liubb481f82012-02-28 07:46:26 +0000618 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000619 // => ins $dst, $src, size, pos, $src1
Akira Hatanaka56633442011-09-20 23:53:09 +0000620 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000621 return SDValue();
622
623 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
624 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
625 ConstantSDNode *CN;
626
627 // See if Op's first operand matches (and $src1 , mask0).
628 if (And0.getOpcode() != ISD::AND)
629 return SDValue();
630
631 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000632 !isShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000633 return SDValue();
634
635 // See if Op's second operand matches (and (shl $src, pos), mask1).
636 if (And1.getOpcode() != ISD::AND)
637 return SDValue();
Jia Liubb481f82012-02-28 07:46:26 +0000638
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000639 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000640 !isShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000641 return SDValue();
642
643 // The shift masks must have the same position and size.
644 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
645 return SDValue();
646
647 SDValue Shl = And1.getOperand(0);
648 if (Shl.getOpcode() != ISD::SHL)
649 return SDValue();
650
651 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
652 return SDValue();
653
654 unsigned Shamt = CN->getZExtValue();
655
656 // Return if the shift amount and the first bit position of mask are not the
Jia Liubb481f82012-02-28 07:46:26 +0000657 // same.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000658 EVT ValTy = N->getValueType(0);
659 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000660 return SDValue();
Jia Liubb481f82012-02-28 07:46:26 +0000661
Andrew Trickac6d9be2013-05-25 02:42:55 +0000662 return DAG.getNode(MipsISD::Ins, SDLoc(N), ValTy, Shl.getOperand(0),
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000663 DAG.getConstant(SMPos0, MVT::i32),
Akira Hatanaka82099682011-12-19 19:52:25 +0000664 DAG.getConstant(SMSize0, MVT::i32), And0.getOperand(0));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000665}
Jia Liubb481f82012-02-28 07:46:26 +0000666
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000667static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka87827072012-06-13 20:33:18 +0000668 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000669 const MipsSubtarget *Subtarget) {
Akira Hatanaka87827072012-06-13 20:33:18 +0000670 // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt))
671
672 if (DCI.isBeforeLegalizeOps())
673 return SDValue();
674
675 SDValue Add = N->getOperand(1);
676
677 if (Add.getOpcode() != ISD::ADD)
678 return SDValue();
679
680 SDValue Lo = Add.getOperand(1);
681
682 if ((Lo.getOpcode() != MipsISD::Lo) ||
683 (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable))
684 return SDValue();
685
686 EVT ValTy = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +0000687 SDLoc DL(N);
Akira Hatanaka87827072012-06-13 20:33:18 +0000688
689 SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, N->getOperand(0),
690 Add.getOperand(0));
691 return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo);
692}
693
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000694SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000695 const {
696 SelectionDAG &DAG = DCI.DAG;
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000697 unsigned Opc = N->getOpcode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000698
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000699 switch (Opc) {
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000700 default: break;
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000701 case ISD::SDIVREM:
702 case ISD::UDIVREM:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000703 return performDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000704 case ISD::SELECT:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000705 return performSELECTCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000706 case ISD::AND:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000707 return performANDCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000708 case ISD::OR:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000709 return performORCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka87827072012-06-13 20:33:18 +0000710 case ISD::ADD:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000711 return performADDCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000712 }
713
714 return SDValue();
715}
716
Akira Hatanakab430cec2012-09-21 23:58:31 +0000717void
718MipsTargetLowering::LowerOperationWrapper(SDNode *N,
719 SmallVectorImpl<SDValue> &Results,
720 SelectionDAG &DAG) const {
721 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
722
723 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
724 Results.push_back(Res.getValue(I));
725}
726
727void
728MipsTargetLowering::ReplaceNodeResults(SDNode *N,
729 SmallVectorImpl<SDValue> &Results,
730 SelectionDAG &DAG) const {
Akira Hatanaka13ec4812013-04-30 21:17:07 +0000731 return LowerOperationWrapper(N, Results, DAG);
Akira Hatanakab430cec2012-09-21 23:58:31 +0000732}
733
Dan Gohman475871a2008-07-27 21:46:04 +0000734SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000735LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000736{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000737 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000738 {
Akira Hatanaka2459afe2013-03-30 01:15:17 +0000739 case ISD::BR_JT: return lowerBR_JT(Op, DAG);
740 case ISD::BRCOND: return lowerBRCOND(Op, DAG);
741 case ISD::ConstantPool: return lowerConstantPool(Op, DAG);
742 case ISD::GlobalAddress: return lowerGlobalAddress(Op, DAG);
743 case ISD::BlockAddress: return lowerBlockAddress(Op, DAG);
744 case ISD::GlobalTLSAddress: return lowerGlobalTLSAddress(Op, DAG);
745 case ISD::JumpTable: return lowerJumpTable(Op, DAG);
746 case ISD::SELECT: return lowerSELECT(Op, DAG);
747 case ISD::SELECT_CC: return lowerSELECT_CC(Op, DAG);
748 case ISD::SETCC: return lowerSETCC(Op, DAG);
749 case ISD::VASTART: return lowerVASTART(Op, DAG);
750 case ISD::FCOPYSIGN: return lowerFCOPYSIGN(Op, DAG);
751 case ISD::FABS: return lowerFABS(Op, DAG);
752 case ISD::FRAMEADDR: return lowerFRAMEADDR(Op, DAG);
753 case ISD::RETURNADDR: return lowerRETURNADDR(Op, DAG);
754 case ISD::EH_RETURN: return lowerEH_RETURN(Op, DAG);
Akira Hatanaka2459afe2013-03-30 01:15:17 +0000755 case ISD::ATOMIC_FENCE: return lowerATOMIC_FENCE(Op, DAG);
756 case ISD::SHL_PARTS: return lowerShiftLeftParts(Op, DAG);
757 case ISD::SRA_PARTS: return lowerShiftRightParts(Op, DAG, true);
758 case ISD::SRL_PARTS: return lowerShiftRightParts(Op, DAG, false);
759 case ISD::LOAD: return lowerLOAD(Op, DAG);
760 case ISD::STORE: return lowerSTORE(Op, DAG);
Akira Hatanaka2459afe2013-03-30 01:15:17 +0000761 case ISD::ADD: return lowerADD(Op, DAG);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +0000762 case ISD::FP_TO_SINT: return lowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000763 }
Dan Gohman475871a2008-07-27 21:46:04 +0000764 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000765}
766
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000767//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000768// Lower helper functions
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000769//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000770
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000771// addLiveIn - This helper function adds the specified physical register to the
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000772// MachineFunction as a live in value. It also creates a corresponding
773// virtual register for it.
774static unsigned
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000775addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000776{
Chris Lattner84bc5422007-12-31 04:13:23 +0000777 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
778 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000779 return VReg;
780}
781
Akira Hatanakaf8941992013-05-20 18:07:43 +0000782static MachineBasicBlock *expandPseudoDIV(MachineInstr *MI,
783 MachineBasicBlock &MBB,
784 const TargetInstrInfo &TII,
785 bool Is64Bit) {
786 if (NoZeroDivCheck)
787 return &MBB;
788
789 // Insert instruction "teq $divisor_reg, $zero, 7".
790 MachineBasicBlock::iterator I(MI);
791 MachineInstrBuilder MIB;
792 MIB = BuildMI(MBB, llvm::next(I), MI->getDebugLoc(), TII.get(Mips::TEQ))
793 .addOperand(MI->getOperand(2)).addReg(Mips::ZERO).addImm(7);
794
795 // Use the 32-bit sub-register if this is a 64-bit division.
796 if (Is64Bit)
797 MIB->getOperand(0).setSubReg(Mips::sub_32);
798
799 return &MBB;
800}
801
Akira Hatanaka01f70892012-09-27 02:15:57 +0000802MachineBasicBlock *
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000803MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000804 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000805 switch (MI->getOpcode()) {
Reed Kotlerffbe4322013-02-21 04:22:38 +0000806 default:
807 llvm_unreachable("Unexpected instr type to insert");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000808 case Mips::ATOMIC_LOAD_ADD_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000809 return emitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000810 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000811 return emitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000812 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000813 return emitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000814 case Mips::ATOMIC_LOAD_ADD_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000815 return emitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000816
817 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000818 return emitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000819 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000820 return emitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000821 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000822 return emitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka59068062011-11-11 04:14:30 +0000823 case Mips::ATOMIC_LOAD_AND_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000824 return emitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000825
826 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000827 return emitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000828 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000829 return emitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000830 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000831 return emitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000832 case Mips::ATOMIC_LOAD_OR_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000833 return emitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000834
835 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000836 return emitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000837 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000838 return emitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000839 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000840 return emitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000841 case Mips::ATOMIC_LOAD_XOR_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000842 return emitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000843
844 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000845 return emitAtomicBinaryPartword(MI, BB, 1, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000846 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000847 return emitAtomicBinaryPartword(MI, BB, 2, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000848 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000849 return emitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka59068062011-11-11 04:14:30 +0000850 case Mips::ATOMIC_LOAD_NAND_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000851 return emitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000852
853 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000854 return emitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000855 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000856 return emitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000857 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000858 return emitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000859 case Mips::ATOMIC_LOAD_SUB_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000860 return emitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000861
862 case Mips::ATOMIC_SWAP_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000863 return emitAtomicBinaryPartword(MI, BB, 1, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000864 case Mips::ATOMIC_SWAP_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000865 return emitAtomicBinaryPartword(MI, BB, 2, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000866 case Mips::ATOMIC_SWAP_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000867 return emitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka59068062011-11-11 04:14:30 +0000868 case Mips::ATOMIC_SWAP_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000869 return emitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000870
871 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000872 return emitAtomicCmpSwapPartword(MI, BB, 1);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000873 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000874 return emitAtomicCmpSwapPartword(MI, BB, 2);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000875 case Mips::ATOMIC_CMP_SWAP_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000876 return emitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka59068062011-11-11 04:14:30 +0000877 case Mips::ATOMIC_CMP_SWAP_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000878 return emitAtomicCmpSwap(MI, BB, 8);
Akira Hatanakaf8941992013-05-20 18:07:43 +0000879 case Mips::PseudoSDIV:
880 case Mips::PseudoUDIV:
881 return expandPseudoDIV(MI, *BB, *getTargetMachine().getInstrInfo(), false);
882 case Mips::PseudoDSDIV:
883 case Mips::PseudoDUDIV:
884 return expandPseudoDIV(MI, *BB, *getTargetMachine().getInstrInfo(), true);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000885 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000886}
887
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000888// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
889// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
890MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000891MipsTargetLowering::emitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher471e4222011-06-08 23:55:35 +0000892 unsigned Size, unsigned BinOpcode,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000893 bool Nand) const {
Akira Hatanaka59068062011-11-11 04:14:30 +0000894 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000895
896 MachineFunction *MF = BB->getParent();
897 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +0000898 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000899 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000900 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +0000901 unsigned LL, SC, AND, NOR, ZERO, BEQ;
902
903 if (Size == 4) {
Akira Hatanakaa98a4862013-08-20 21:08:22 +0000904 LL = Mips::LL;
905 SC = Mips::SC;
Akira Hatanaka59068062011-11-11 04:14:30 +0000906 AND = Mips::AND;
907 NOR = Mips::NOR;
908 ZERO = Mips::ZERO;
909 BEQ = Mips::BEQ;
910 }
911 else {
Akira Hatanakaa98a4862013-08-20 21:08:22 +0000912 LL = Mips::LLD;
913 SC = Mips::SCD;
Akira Hatanaka59068062011-11-11 04:14:30 +0000914 AND = Mips::AND64;
915 NOR = Mips::NOR64;
916 ZERO = Mips::ZERO_64;
917 BEQ = Mips::BEQ64;
918 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000919
Akira Hatanaka4061da12011-07-19 20:11:17 +0000920 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000921 unsigned Ptr = MI->getOperand(1).getReg();
922 unsigned Incr = MI->getOperand(2).getReg();
923
Akira Hatanaka4061da12011-07-19 20:11:17 +0000924 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
925 unsigned AndRes = RegInfo.createVirtualRegister(RC);
926 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000927
928 // insert new blocks after the current block
929 const BasicBlock *LLVM_BB = BB->getBasicBlock();
930 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
931 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
932 MachineFunction::iterator It = BB;
933 ++It;
934 MF->insert(It, loopMBB);
935 MF->insert(It, exitMBB);
936
937 // Transfer the remainder of BB and its successor edges to exitMBB.
938 exitMBB->splice(exitMBB->begin(), BB,
939 llvm::next(MachineBasicBlock::iterator(MI)),
940 BB->end());
941 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
942
943 // thisMBB:
944 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000945 // fallthrough --> loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000946 BB->addSuccessor(loopMBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +0000947 loopMBB->addSuccessor(loopMBB);
948 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000949
950 // loopMBB:
951 // ll oldval, 0(ptr)
Akira Hatanaka4061da12011-07-19 20:11:17 +0000952 // <binop> storeval, oldval, incr
953 // sc success, storeval, 0(ptr)
954 // beq success, $0, loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000955 BB = loopMBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000956 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000957 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000958 // and andres, oldval, incr
959 // nor storeval, $0, andres
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000960 BuildMI(BB, DL, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
961 BuildMI(BB, DL, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000962 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000963 // <binop> storeval, oldval, incr
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000964 BuildMI(BB, DL, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000965 } else {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000966 StoreVal = Incr;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000967 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000968 BuildMI(BB, DL, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
969 BuildMI(BB, DL, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000970
971 MI->eraseFromParent(); // The instruction is gone now.
972
Akira Hatanaka939ece12011-07-19 03:42:13 +0000973 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000974}
975
976MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000977MipsTargetLowering::emitAtomicBinaryPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000978 MachineBasicBlock *BB,
979 unsigned Size, unsigned BinOpcode,
980 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000981 assert((Size == 1 || Size == 2) &&
982 "Unsupported size for EmitAtomicBinaryPartial.");
983
984 MachineFunction *MF = BB->getParent();
985 MachineRegisterInfo &RegInfo = MF->getRegInfo();
986 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
987 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000988 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000989
990 unsigned Dest = MI->getOperand(0).getReg();
991 unsigned Ptr = MI->getOperand(1).getReg();
992 unsigned Incr = MI->getOperand(2).getReg();
993
Akira Hatanaka4061da12011-07-19 20:11:17 +0000994 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
995 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000996 unsigned Mask = RegInfo.createVirtualRegister(RC);
997 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000998 unsigned NewVal = RegInfo.createVirtualRegister(RC);
999 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001000 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001001 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1002 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1003 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1004 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1005 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001006 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001007 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1008 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1009 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1010 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1011 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001012
1013 // insert new blocks after the current block
1014 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1015 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001016 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001017 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1018 MachineFunction::iterator It = BB;
1019 ++It;
1020 MF->insert(It, loopMBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001021 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001022 MF->insert(It, exitMBB);
1023
1024 // Transfer the remainder of BB and its successor edges to exitMBB.
1025 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001026 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001027 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1028
Akira Hatanaka81b44112011-07-19 17:09:53 +00001029 BB->addSuccessor(loopMBB);
1030 loopMBB->addSuccessor(loopMBB);
1031 loopMBB->addSuccessor(sinkMBB);
1032 sinkMBB->addSuccessor(exitMBB);
1033
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001034 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001035 // addiu masklsb2,$0,-4 # 0xfffffffc
1036 // and alignedaddr,ptr,masklsb2
1037 // andi ptrlsb2,ptr,3
1038 // sll shiftamt,ptrlsb2,3
1039 // ori maskupper,$0,255 # 0xff
1040 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001041 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001042 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001043
1044 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001045 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001046 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001047 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001048 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001049 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Akira Hatanakaaffed7e2013-05-31 03:25:44 +00001050 if (Subtarget->isLittle()) {
1051 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1052 } else {
1053 unsigned Off = RegInfo.createVirtualRegister(RC);
1054 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1055 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1056 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1057 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001058 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001059 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001060 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka51122432013-07-01 20:39:53 +00001061 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001062 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka51122432013-07-01 20:39:53 +00001063 BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(Incr).addReg(ShiftAmt);
Bruno Cardoso Lopescada2d02011-05-31 20:25:26 +00001064
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001065 // atomic.load.binop
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001066 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001067 // ll oldval,0(alignedaddr)
1068 // binop binopres,oldval,incr2
1069 // and newval,binopres,mask
1070 // and maskedoldval0,oldval,mask2
1071 // or storeval,maskedoldval0,newval
1072 // sc success,storeval,0(alignedaddr)
1073 // beq success,$0,loopMBB
1074
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001075 // atomic.swap
1076 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001077 // ll oldval,0(alignedaddr)
Akira Hatanaka70564a92011-07-19 18:14:26 +00001078 // and newval,incr2,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001079 // and maskedoldval0,oldval,mask2
1080 // or storeval,maskedoldval0,newval
1081 // sc success,storeval,0(alignedaddr)
1082 // beq success,$0,loopMBB
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001083
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001084 BB = loopMBB;
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001085 BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001086 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001087 // and andres, oldval, incr2
1088 // nor binopres, $0, andres
1089 // and newval, binopres, mask
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001090 BuildMI(BB, DL, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1091 BuildMI(BB, DL, TII->get(Mips::NOR), BinOpRes)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001092 .addReg(Mips::ZERO).addReg(AndRes);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001093 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001094 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001095 // <binop> binopres, oldval, incr2
1096 // and newval, binopres, mask
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001097 BuildMI(BB, DL, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1098 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001099 } else {// atomic.swap
Akira Hatanaka4061da12011-07-19 20:11:17 +00001100 // and newval, incr2, mask
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001101 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001102 }
Jia Liubb481f82012-02-28 07:46:26 +00001103
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001104 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001105 .addReg(OldVal).addReg(Mask2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001106 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001107 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001108 BuildMI(BB, DL, TII->get(Mips::SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001109 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001110 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001111 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001112
Akira Hatanaka939ece12011-07-19 03:42:13 +00001113 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001114 // and maskedoldval1,oldval,mask
1115 // srl srlres,maskedoldval1,shiftamt
1116 // sll sllres,srlres,24
1117 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001118 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001119 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001120
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001121 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001122 .addReg(OldVal).addReg(Mask);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001123 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka51122432013-07-01 20:39:53 +00001124 .addReg(MaskedOldVal1).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001125 BuildMI(BB, DL, TII->get(Mips::SLL), SllRes)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001126 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001127 BuildMI(BB, DL, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001128 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001129
1130 MI->eraseFromParent(); // The instruction is gone now.
1131
Akira Hatanaka939ece12011-07-19 03:42:13 +00001132 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001133}
1134
1135MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001136MipsTargetLowering::emitAtomicCmpSwap(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001137 MachineBasicBlock *BB,
1138 unsigned Size) const {
Akira Hatanaka59068062011-11-11 04:14:30 +00001139 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001140
1141 MachineFunction *MF = BB->getParent();
1142 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +00001143 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001144 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001145 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001146 unsigned LL, SC, ZERO, BNE, BEQ;
1147
1148 if (Size == 4) {
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001149 LL = Mips::LL;
1150 SC = Mips::SC;
Akira Hatanaka59068062011-11-11 04:14:30 +00001151 ZERO = Mips::ZERO;
1152 BNE = Mips::BNE;
1153 BEQ = Mips::BEQ;
1154 }
1155 else {
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001156 LL = Mips::LLD;
1157 SC = Mips::SCD;
Akira Hatanaka59068062011-11-11 04:14:30 +00001158 ZERO = Mips::ZERO_64;
1159 BNE = Mips::BNE64;
1160 BEQ = Mips::BEQ64;
1161 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001162
1163 unsigned Dest = MI->getOperand(0).getReg();
1164 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001165 unsigned OldVal = MI->getOperand(2).getReg();
1166 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001167
Akira Hatanaka4061da12011-07-19 20:11:17 +00001168 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001169
1170 // insert new blocks after the current block
1171 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1172 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1173 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1174 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1175 MachineFunction::iterator It = BB;
1176 ++It;
1177 MF->insert(It, loop1MBB);
1178 MF->insert(It, loop2MBB);
1179 MF->insert(It, exitMBB);
1180
1181 // Transfer the remainder of BB and its successor edges to exitMBB.
1182 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001183 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001184 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1185
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001186 // thisMBB:
1187 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001188 // fallthrough --> loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001189 BB->addSuccessor(loop1MBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +00001190 loop1MBB->addSuccessor(exitMBB);
1191 loop1MBB->addSuccessor(loop2MBB);
1192 loop2MBB->addSuccessor(loop1MBB);
1193 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001194
1195 // loop1MBB:
1196 // ll dest, 0(ptr)
1197 // bne dest, oldval, exitMBB
1198 BB = loop1MBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001199 BuildMI(BB, DL, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1200 BuildMI(BB, DL, TII->get(BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001201 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001202
1203 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001204 // sc success, newval, 0(ptr)
1205 // beq success, $0, loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001206 BB = loop2MBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001207 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001208 .addReg(NewVal).addReg(Ptr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001209 BuildMI(BB, DL, TII->get(BEQ))
Akira Hatanaka59068062011-11-11 04:14:30 +00001210 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001211
1212 MI->eraseFromParent(); // The instruction is gone now.
1213
Akira Hatanaka939ece12011-07-19 03:42:13 +00001214 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001215}
1216
1217MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001218MipsTargetLowering::emitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001219 MachineBasicBlock *BB,
1220 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001221 assert((Size == 1 || Size == 2) &&
1222 "Unsupported size for EmitAtomicCmpSwapPartial.");
1223
1224 MachineFunction *MF = BB->getParent();
1225 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1226 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1227 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001228 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001229
1230 unsigned Dest = MI->getOperand(0).getReg();
1231 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001232 unsigned CmpVal = MI->getOperand(2).getReg();
1233 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001234
Akira Hatanaka4061da12011-07-19 20:11:17 +00001235 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1236 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001237 unsigned Mask = RegInfo.createVirtualRegister(RC);
1238 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001239 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1240 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1241 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1242 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1243 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1244 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1245 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1246 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1247 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1248 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1249 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1250 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1251 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1252 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001253
1254 // insert new blocks after the current block
1255 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1256 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1257 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001258 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001259 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1260 MachineFunction::iterator It = BB;
1261 ++It;
1262 MF->insert(It, loop1MBB);
1263 MF->insert(It, loop2MBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001264 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001265 MF->insert(It, exitMBB);
1266
1267 // Transfer the remainder of BB and its successor edges to exitMBB.
1268 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001269 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001270 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1271
Akira Hatanaka81b44112011-07-19 17:09:53 +00001272 BB->addSuccessor(loop1MBB);
1273 loop1MBB->addSuccessor(sinkMBB);
1274 loop1MBB->addSuccessor(loop2MBB);
1275 loop2MBB->addSuccessor(loop1MBB);
1276 loop2MBB->addSuccessor(sinkMBB);
1277 sinkMBB->addSuccessor(exitMBB);
1278
Akira Hatanaka70564a92011-07-19 18:14:26 +00001279 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001280 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001281 // addiu masklsb2,$0,-4 # 0xfffffffc
1282 // and alignedaddr,ptr,masklsb2
1283 // andi ptrlsb2,ptr,3
1284 // sll shiftamt,ptrlsb2,3
1285 // ori maskupper,$0,255 # 0xff
1286 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001287 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001288 // andi maskedcmpval,cmpval,255
1289 // sll shiftedcmpval,maskedcmpval,shiftamt
1290 // andi maskednewval,newval,255
1291 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001292 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001293 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001294 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001295 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001296 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001297 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Akira Hatanakaaffed7e2013-05-31 03:25:44 +00001298 if (Subtarget->isLittle()) {
1299 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1300 } else {
1301 unsigned Off = RegInfo.createVirtualRegister(RC);
1302 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1303 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1304 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1305 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001306 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001307 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001308 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka51122432013-07-01 20:39:53 +00001309 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001310 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1311 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001312 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001313 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal)
Akira Hatanaka51122432013-07-01 20:39:53 +00001314 .addReg(MaskedCmpVal).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001315 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001316 .addReg(NewVal).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001317 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal)
Akira Hatanaka51122432013-07-01 20:39:53 +00001318 .addReg(MaskedNewVal).addReg(ShiftAmt);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001319
1320 // loop1MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001321 // ll oldval,0(alginedaddr)
1322 // and maskedoldval0,oldval,mask
1323 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001324 BB = loop1MBB;
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001325 BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001326 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001327 .addReg(OldVal).addReg(Mask);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001328 BuildMI(BB, DL, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001329 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001330
1331 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001332 // and maskedoldval1,oldval,mask2
1333 // or storeval,maskedoldval1,shiftednewval
1334 // sc success,storeval,0(alignedaddr)
1335 // beq success,$0,loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001336 BB = loop2MBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001337 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001338 .addReg(OldVal).addReg(Mask2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001339 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001340 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001341 BuildMI(BB, DL, TII->get(Mips::SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001342 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001343 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001344 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001345
Akira Hatanaka939ece12011-07-19 03:42:13 +00001346 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001347 // srl srlres,maskedoldval0,shiftamt
1348 // sll sllres,srlres,24
1349 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001350 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001351 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001352
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001353 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka51122432013-07-01 20:39:53 +00001354 .addReg(MaskedOldVal0).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001355 BuildMI(BB, DL, TII->get(Mips::SLL), SllRes)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001356 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001357 BuildMI(BB, DL, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001358 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001359
1360 MI->eraseFromParent(); // The instruction is gone now.
1361
Akira Hatanaka939ece12011-07-19 03:42:13 +00001362 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001363}
1364
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001365//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001366// Misc Lower Operation implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001367//===----------------------------------------------------------------------===//
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001368SDValue MipsTargetLowering::lowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanakab7656a92013-03-06 21:32:03 +00001369 SDValue Chain = Op.getOperand(0);
1370 SDValue Table = Op.getOperand(1);
1371 SDValue Index = Op.getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001372 SDLoc DL(Op);
Akira Hatanakab7656a92013-03-06 21:32:03 +00001373 EVT PTy = getPointerTy();
1374 unsigned EntrySize =
1375 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(*getDataLayout());
1376
1377 Index = DAG.getNode(ISD::MUL, DL, PTy, Index,
1378 DAG.getConstant(EntrySize, PTy));
1379 SDValue Addr = DAG.getNode(ISD::ADD, DL, PTy, Index, Table);
1380
1381 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
1382 Addr = DAG.getExtLoad(ISD::SEXTLOAD, DL, PTy, Chain, Addr,
1383 MachinePointerInfo::getJumpTable(), MemVT, false, false,
1384 0);
1385 Chain = Addr.getValue(1);
1386
1387 if ((getTargetMachine().getRelocationModel() == Reloc::PIC_) || IsN64) {
1388 // For PIC, the sequence is:
1389 // BRIND(load(Jumptable + index) + RelocBase)
1390 // RelocBase can be JumpTable, GOT or some sort of global base.
1391 Addr = DAG.getNode(ISD::ADD, DL, PTy, Addr,
1392 getPICJumpTableRelocBase(Table, DAG));
1393 }
1394
1395 return DAG.getNode(ISD::BRIND, DL, MVT::Other, Chain, Addr);
1396}
1397
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +00001398SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001399lowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001400{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001401 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001402 // the block to branch to if the condition is true.
1403 SDValue Chain = Op.getOperand(0);
1404 SDValue Dest = Op.getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001405 SDLoc DL(Op);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001406
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001407 SDValue CondRes = createFPCmp(DAG, Op.getOperand(1));
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001408
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001409 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001410 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +00001411 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001412
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +00001413 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001414 Mips::CondCode CC =
1415 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Akira Hatanaka9cf07242013-03-30 01:16:38 +00001416 unsigned Opc = invertFPCondCodeUser(CC) ? Mips::BRANCH_F : Mips::BRANCH_T;
1417 SDValue BrCode = DAG.getConstant(Opc, MVT::i32);
Akira Hatanaka83d8ef12013-07-26 20:13:47 +00001418 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001419 return DAG.getNode(MipsISD::FPBrcond, DL, Op.getValueType(), Chain, BrCode,
Akira Hatanaka83d8ef12013-07-26 20:13:47 +00001420 FCC0, Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001421}
1422
1423SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001424lowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001425{
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001426 SDValue Cond = createFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001427
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001428 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001429 if (Cond.getOpcode() != MipsISD::FPCmp)
1430 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +00001431
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001432 return createCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001433 SDLoc(Op));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001434}
1435
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001436SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001437lowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001438{
Andrew Trickac6d9be2013-05-25 02:42:55 +00001439 SDLoc DL(Op);
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001440 EVT Ty = Op.getOperand(0).getValueType();
Matt Arsenault225ed702013-05-18 00:21:46 +00001441 SDValue Cond = DAG.getNode(ISD::SETCC, DL,
1442 getSetCCResultType(*DAG.getContext(), Ty),
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001443 Op.getOperand(0), Op.getOperand(1),
1444 Op.getOperand(4));
1445
1446 return DAG.getNode(ISD::SELECT, DL, Op.getValueType(), Cond, Op.getOperand(2),
1447 Op.getOperand(3));
1448}
1449
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001450SDValue MipsTargetLowering::lowerSETCC(SDValue Op, SelectionDAG &DAG) const {
1451 SDValue Cond = createFPCmp(DAG, Op);
Akira Hatanaka0a40c232012-03-09 23:46:03 +00001452
1453 assert(Cond.getOpcode() == MipsISD::FPCmp &&
1454 "Floating point operand expected.");
1455
1456 SDValue True = DAG.getConstant(1, MVT::i32);
1457 SDValue False = DAG.getConstant(0, MVT::i32);
1458
Andrew Trickac6d9be2013-05-25 02:42:55 +00001459 return createCMovFP(DAG, Cond, True, False, SDLoc(Op));
Akira Hatanaka0a40c232012-03-09 23:46:03 +00001460}
1461
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001462SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001463 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +00001464 // FIXME there isn't actually debug info here
Andrew Trickac6d9be2013-05-25 02:42:55 +00001465 SDLoc DL(Op);
Jia Liubb481f82012-02-28 07:46:26 +00001466 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001467
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001468 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
Akira Hatanakaafc945b2012-09-12 23:27:55 +00001469 const MipsTargetObjectFile &TLOF =
1470 (const MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001471
Chris Lattnere3736f82009-08-13 05:41:27 +00001472 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001473 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001474 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001475 MipsII::MO_GPREL);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001476 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, DL,
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001477 DAG.getVTList(MVT::i32), &GA, 1);
Akira Hatanakae7338cd2012-08-22 03:18:13 +00001478 SDValue GPReg = DAG.getRegister(Mips::GP, MVT::i32);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001479 return DAG.getNode(ISD::ADD, DL, MVT::i32, GPReg, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +00001480 }
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001481
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001482 // %hi/%lo relocation
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001483 return getAddrNonPIC(Op, DAG);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001484 }
1485
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001486 if (GV->hasInternalLinkage() || (GV->hasLocalLinkage() && !isa<Function>(GV)))
1487 return getAddrLocal(Op, DAG, HasMips64);
1488
Akira Hatanakaf09a0372012-11-21 20:40:38 +00001489 if (LargeGOT)
1490 return getAddrGlobalLargeGOT(Op, DAG, MipsII::MO_GOT_HI16,
1491 MipsII::MO_GOT_LO16);
1492
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001493 return getAddrGlobal(Op, DAG,
1494 HasMips64 ? MipsII::MO_GOT_DISP : MipsII::MO_GOT16);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001495}
1496
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001497SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op,
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001498 SelectionDAG &DAG) const {
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001499 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
1500 return getAddrNonPIC(Op, DAG);
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001501
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001502 return getAddrLocal(Op, DAG, HasMips64);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001503}
1504
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001505SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001506lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001507{
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001508 // If the relocation model is PIC, use the General Dynamic TLS Model or
1509 // Local Dynamic TLS model, otherwise use the Initial Exec or
1510 // Local Exec TLS Model.
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001511
1512 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001513 SDLoc DL(GA);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001514 const GlobalValue *GV = GA->getGlobal();
1515 EVT PtrVT = getPointerTy();
1516
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001517 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
1518
1519 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Hans Wennborg70a07c72012-06-04 14:02:08 +00001520 // General Dynamic and Local Dynamic TLS Model.
1521 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
1522 : MipsII::MO_TLSGD;
1523
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001524 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag);
1525 SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT,
1526 getGlobalReg(DAG, PtrVT), TGA);
Akira Hatanaka7a7194b2011-12-08 21:05:38 +00001527 unsigned PtrSize = PtrVT.getSizeInBits();
1528 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1529
Benjamin Kramer5eccf672011-12-11 12:21:34 +00001530 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001531
1532 ArgListTy Args;
1533 ArgListEntry Entry;
1534 Entry.Node = Argument;
Akira Hatanakaca074792011-12-08 20:34:32 +00001535 Entry.Ty = PtrTy;
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001536 Args.push_back(Entry);
Jia Liubb481f82012-02-28 07:46:26 +00001537
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001538 TargetLowering::CallLoweringInfo CLI(DAG.getEntryNode(), PtrTy,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001539 false, false, false, false, 0, CallingConv::C,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001540 /*IsTailCall=*/false, /*doesNotRet=*/false,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001541 /*isReturnValueUsed=*/true,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001542 TlsGetAddr, Args, DAG, DL);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001543 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001544
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001545 SDValue Ret = CallResult.first;
1546
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001547 if (model != TLSModel::LocalDynamic)
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001548 return Ret;
1549
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001550 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001551 MipsII::MO_DTPREL_HI);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001552 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1553 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001554 MipsII::MO_DTPREL_LO);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001555 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1556 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret);
1557 return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001558 }
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001559
1560 SDValue Offset;
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001561 if (model == TLSModel::InitialExec) {
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001562 // Initial Exec TLS Model
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001563 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001564 MipsII::MO_GOTTPREL);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001565 TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT),
Akira Hatanaka648f00c2012-02-24 22:34:47 +00001566 TGA);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001567 Offset = DAG.getLoad(PtrVT, DL,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001568 DAG.getEntryNode(), TGA, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001569 false, false, false, 0);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001570 } else {
1571 // Local Exec TLS Model
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001572 assert(model == TLSModel::LocalExec);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001573 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001574 MipsII::MO_TPREL_HI);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001575 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001576 MipsII::MO_TPREL_LO);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001577 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1578 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1579 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001580 }
1581
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001582 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
1583 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001584}
1585
1586SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001587lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001588{
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001589 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
1590 return getAddrNonPIC(Op, DAG);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001591
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001592 return getAddrLocal(Op, DAG, HasMips64);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001593}
1594
Dan Gohman475871a2008-07-27 21:46:04 +00001595SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001596lowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001597{
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001598 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001599 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001600 // but the asm printer currently doesn't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001601 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +00001602 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +00001603 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001604 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1605 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001606 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001607
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001608 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
1609 return getAddrNonPIC(Op, DAG);
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001610
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001611 return getAddrLocal(Op, DAG, HasMips64);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001612}
1613
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001614SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001615 MachineFunction &MF = DAG.getMachineFunction();
1616 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1617
Andrew Trickac6d9be2013-05-25 02:42:55 +00001618 SDLoc DL(Op);
Dan Gohman1e93df62010-04-17 14:41:14 +00001619 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1620 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001621
1622 // vastart just stores the address of the VarArgsFrameIndex slot into the
1623 // memory location argument.
1624 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001625 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
Akira Hatanaka82099682011-12-19 19:52:25 +00001626 MachinePointerInfo(SV), false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001627}
Jia Liubb481f82012-02-28 07:46:26 +00001628
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001629static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001630 EVT TyX = Op.getOperand(0).getValueType();
1631 EVT TyY = Op.getOperand(1).getValueType();
1632 SDValue Const1 = DAG.getConstant(1, MVT::i32);
1633 SDValue Const31 = DAG.getConstant(31, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001634 SDLoc DL(Op);
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001635 SDValue Res;
1636
1637 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1638 // to i32.
1639 SDValue X = (TyX == MVT::f32) ?
1640 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1641 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1642 Const1);
1643 SDValue Y = (TyY == MVT::f32) ?
1644 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
1645 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
1646 Const1);
1647
1648 if (HasR2) {
1649 // ext E, Y, 31, 1 ; extract bit31 of Y
1650 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
1651 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
1652 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
1653 } else {
1654 // sll SllX, X, 1
1655 // srl SrlX, SllX, 1
1656 // srl SrlY, Y, 31
1657 // sll SllY, SrlX, 31
1658 // or Or, SrlX, SllY
1659 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1660 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1661 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
1662 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
1663 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
1664 }
1665
1666 if (TyX == MVT::f32)
1667 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
1668
1669 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1670 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1671 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001672}
1673
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001674static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001675 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
1676 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
1677 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
1678 SDValue Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001679 SDLoc DL(Op);
Eric Christopher471e4222011-06-08 23:55:35 +00001680
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001681 // Bitcast to integer nodes.
1682 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
1683 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001684
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001685 if (HasR2) {
1686 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
1687 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
1688 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
1689 DAG.getConstant(WidthY - 1, MVT::i32), Const1);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001690
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001691 if (WidthX > WidthY)
1692 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
1693 else if (WidthY > WidthX)
1694 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001695
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001696 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
1697 DAG.getConstant(WidthX - 1, MVT::i32), Const1, X);
1698 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
1699 }
1700
1701 // (d)sll SllX, X, 1
1702 // (d)srl SrlX, SllX, 1
1703 // (d)srl SrlY, Y, width(Y)-1
1704 // (d)sll SllY, SrlX, width(Y)-1
1705 // or Or, SrlX, SllY
1706 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
1707 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
1708 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
1709 DAG.getConstant(WidthY - 1, MVT::i32));
1710
1711 if (WidthX > WidthY)
1712 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
1713 else if (WidthY > WidthX)
1714 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
1715
1716 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
1717 DAG.getConstant(WidthX - 1, MVT::i32));
1718 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
1719 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001720}
1721
Akira Hatanaka82099682011-12-19 19:52:25 +00001722SDValue
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001723MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001724 if (Subtarget->hasMips64())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001725 return lowerFCOPYSIGN64(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001726
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001727 return lowerFCOPYSIGN32(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001728}
1729
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001730static SDValue lowerFABS32(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001731 SDValue Res, Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001732 SDLoc DL(Op);
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001733
1734 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1735 // to i32.
1736 SDValue X = (Op.getValueType() == MVT::f32) ?
1737 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1738 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1739 Const1);
1740
1741 // Clear MSB.
1742 if (HasR2)
1743 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32,
1744 DAG.getRegister(Mips::ZERO, MVT::i32),
1745 DAG.getConstant(31, MVT::i32), Const1, X);
1746 else {
1747 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1748 Res = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1749 }
1750
1751 if (Op.getValueType() == MVT::f32)
1752 return DAG.getNode(ISD::BITCAST, DL, MVT::f32, Res);
1753
1754 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1755 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1756 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
1757}
1758
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001759static SDValue lowerFABS64(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001760 SDValue Res, Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001761 SDLoc DL(Op);
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001762
1763 // Bitcast to integer node.
1764 SDValue X = DAG.getNode(ISD::BITCAST, DL, MVT::i64, Op.getOperand(0));
1765
1766 // Clear MSB.
1767 if (HasR2)
1768 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i64,
1769 DAG.getRegister(Mips::ZERO_64, MVT::i64),
1770 DAG.getConstant(63, MVT::i32), Const1, X);
1771 else {
1772 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i64, X, Const1);
1773 Res = DAG.getNode(ISD::SRL, DL, MVT::i64, SllX, Const1);
1774 }
1775
1776 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, Res);
1777}
1778
1779SDValue
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001780MipsTargetLowering::lowerFABS(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001781 if (Subtarget->hasMips64() && (Op.getValueType() == MVT::f64))
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001782 return lowerFABS64(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001783
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001784 return lowerFABS32(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001785}
1786
Akira Hatanaka2e591472011-06-02 00:24:44 +00001787SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001788lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopese0b5cfc2011-06-16 00:40:02 +00001789 // check the depth
1790 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka0f843822011-06-07 18:58:42 +00001791 "Frame address can only be determined for current frame.");
Akira Hatanaka2e591472011-06-02 00:24:44 +00001792
1793 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1794 MFI->setFrameAddressIsTaken(true);
1795 EVT VT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001796 SDLoc DL(Op);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001797 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), DL,
Akira Hatanaka46ac4392011-11-11 04:11:56 +00001798 IsN64 ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka2e591472011-06-02 00:24:44 +00001799 return FrameAddr;
1800}
1801
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001802SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op,
Akira Hatanakaba584fe2012-07-11 00:53:32 +00001803 SelectionDAG &DAG) const {
1804 // check the depth
1805 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
1806 "Return address can be determined only for current frame.");
1807
1808 MachineFunction &MF = DAG.getMachineFunction();
1809 MachineFrameInfo *MFI = MF.getFrameInfo();
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00001810 MVT VT = Op.getSimpleValueType();
Akira Hatanakaba584fe2012-07-11 00:53:32 +00001811 unsigned RA = IsN64 ? Mips::RA_64 : Mips::RA;
1812 MFI->setReturnAddressIsTaken(true);
1813
1814 // Return RA, which contains the return address. Mark it an implicit live-in.
1815 unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00001816 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT);
Akira Hatanakaba584fe2012-07-11 00:53:32 +00001817}
1818
Akira Hatanaka544cc212013-01-30 00:26:49 +00001819// An EH_RETURN is the result of lowering llvm.eh.return which in turn is
1820// generated from __builtin_eh_return (offset, handler)
1821// The effect of this is to adjust the stack pointer by "offset"
1822// and then branch to "handler".
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001823SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Akira Hatanaka544cc212013-01-30 00:26:49 +00001824 const {
1825 MachineFunction &MF = DAG.getMachineFunction();
1826 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1827
1828 MipsFI->setCallsEhReturn();
1829 SDValue Chain = Op.getOperand(0);
1830 SDValue Offset = Op.getOperand(1);
1831 SDValue Handler = Op.getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001832 SDLoc DL(Op);
Akira Hatanaka544cc212013-01-30 00:26:49 +00001833 EVT Ty = IsN64 ? MVT::i64 : MVT::i32;
1834
1835 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
1836 // EH_RETURN nodes, so that instructions are emitted back-to-back.
1837 unsigned OffsetReg = IsN64 ? Mips::V1_64 : Mips::V1;
1838 unsigned AddrReg = IsN64 ? Mips::V0_64 : Mips::V0;
1839 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
1840 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
1841 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
1842 DAG.getRegister(OffsetReg, Ty),
1843 DAG.getRegister(AddrReg, getPointerTy()),
1844 Chain.getValue(1));
1845}
1846
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001847SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
Akira Hatanaka864f6602012-06-14 21:10:56 +00001848 SelectionDAG &DAG) const {
Eli Friedman14648462011-07-27 22:21:52 +00001849 // FIXME: Need pseudo-fence for 'singlethread' fences
1850 // FIXME: Set SType for weaker fences where supported/appropriate.
1851 unsigned SType = 0;
Andrew Trickac6d9be2013-05-25 02:42:55 +00001852 SDLoc DL(Op);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001853 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00001854 DAG.getConstant(SType, MVT::i32));
1855}
1856
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001857SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
Akira Hatanaka864f6602012-06-14 21:10:56 +00001858 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001859 SDLoc DL(Op);
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001860 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
1861 SDValue Shamt = Op.getOperand(2);
1862
1863 // if shamt < 32:
1864 // lo = (shl lo, shamt)
1865 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
1866 // else:
1867 // lo = 0
1868 // hi = (shl lo, shamt[4:0])
1869 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
1870 DAG.getConstant(-1, MVT::i32));
1871 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo,
1872 DAG.getConstant(1, MVT::i32));
1873 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, ShiftRight1Lo,
1874 Not);
1875 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi, Shamt);
1876 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
1877 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, MVT::i32, Lo, Shamt);
1878 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
1879 DAG.getConstant(0x20, MVT::i32));
Akira Hatanaka864f6602012-06-14 21:10:56 +00001880 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
1881 DAG.getConstant(0, MVT::i32), ShiftLeftLo);
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001882 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftLeftLo, Or);
1883
1884 SDValue Ops[2] = {Lo, Hi};
1885 return DAG.getMergeValues(Ops, 2, DL);
1886}
1887
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001888SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001889 bool IsSRA) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001890 SDLoc DL(Op);
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001891 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
1892 SDValue Shamt = Op.getOperand(2);
1893
1894 // if shamt < 32:
1895 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
1896 // if isSRA:
1897 // hi = (sra hi, shamt)
1898 // else:
1899 // hi = (srl hi, shamt)
1900 // else:
1901 // if isSRA:
1902 // lo = (sra hi, shamt[4:0])
1903 // hi = (sra hi, 31)
1904 // else:
1905 // lo = (srl hi, shamt[4:0])
1906 // hi = 0
1907 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
1908 DAG.getConstant(-1, MVT::i32));
1909 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi,
1910 DAG.getConstant(1, MVT::i32));
1911 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, ShiftLeft1Hi, Not);
1912 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo, Shamt);
1913 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
1914 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL, DL, MVT::i32,
1915 Hi, Shamt);
1916 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
1917 DAG.getConstant(0x20, MVT::i32));
1918 SDValue Shift31 = DAG.getNode(ISD::SRA, DL, MVT::i32, Hi,
1919 DAG.getConstant(31, MVT::i32));
1920 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftRightHi, Or);
1921 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
1922 IsSRA ? Shift31 : DAG.getConstant(0, MVT::i32),
1923 ShiftRightHi);
1924
1925 SDValue Ops[2] = {Lo, Hi};
1926 return DAG.getMergeValues(Ops, 2, DL);
1927}
1928
Akira Hatanakafee62c12013-04-11 19:07:14 +00001929static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001930 SDValue Chain, SDValue Src, unsigned Offset) {
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00001931 SDValue Ptr = LD->getBasePtr();
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001932 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00001933 EVT BasePtrVT = Ptr.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001934 SDLoc DL(LD);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001935 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
1936
1937 if (Offset)
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00001938 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001939 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001940
1941 SDValue Ops[] = { Chain, Ptr, Src };
1942 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT,
1943 LD->getMemOperand());
1944}
1945
1946// Expand an unaligned 32 or 64-bit integer load node.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001947SDValue MipsTargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001948 LoadSDNode *LD = cast<LoadSDNode>(Op);
1949 EVT MemVT = LD->getMemoryVT();
1950
1951 // Return if load is aligned or if MemVT is neither i32 nor i64.
1952 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
1953 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
1954 return SDValue();
1955
1956 bool IsLittle = Subtarget->isLittle();
1957 EVT VT = Op.getValueType();
1958 ISD::LoadExtType ExtType = LD->getExtensionType();
1959 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
1960
1961 assert((VT == MVT::i32) || (VT == MVT::i64));
1962
1963 // Expand
1964 // (set dst, (i64 (load baseptr)))
1965 // to
1966 // (set tmp, (ldl (add baseptr, 7), undef))
1967 // (set dst, (ldr baseptr, tmp))
1968 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
Akira Hatanakafee62c12013-04-11 19:07:14 +00001969 SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001970 IsLittle ? 7 : 0);
Akira Hatanakafee62c12013-04-11 19:07:14 +00001971 return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001972 IsLittle ? 0 : 7);
1973 }
1974
Akira Hatanakafee62c12013-04-11 19:07:14 +00001975 SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001976 IsLittle ? 3 : 0);
Akira Hatanakafee62c12013-04-11 19:07:14 +00001977 SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001978 IsLittle ? 0 : 3);
1979
1980 // Expand
1981 // (set dst, (i32 (load baseptr))) or
1982 // (set dst, (i64 (sextload baseptr))) or
1983 // (set dst, (i64 (extload baseptr)))
1984 // to
1985 // (set tmp, (lwl (add baseptr, 3), undef))
1986 // (set dst, (lwr baseptr, tmp))
1987 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
1988 (ExtType == ISD::EXTLOAD))
1989 return LWR;
1990
1991 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
1992
1993 // Expand
1994 // (set dst, (i64 (zextload baseptr)))
1995 // to
1996 // (set tmp0, (lwl (add baseptr, 3), undef))
1997 // (set tmp1, (lwr baseptr, tmp0))
1998 // (set tmp2, (shl tmp1, 32))
1999 // (set dst, (srl tmp2, 32))
Andrew Trickac6d9be2013-05-25 02:42:55 +00002000 SDLoc DL(LD);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002001 SDValue Const32 = DAG.getConstant(32, MVT::i32);
2002 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
Akira Hatanaka94ccee22012-06-04 17:46:29 +00002003 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
2004 SDValue Ops[] = { SRL, LWR.getValue(1) };
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002005 return DAG.getMergeValues(Ops, 2, DL);
2006}
2007
Akira Hatanakafee62c12013-04-11 19:07:14 +00002008static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002009 SDValue Chain, unsigned Offset) {
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00002010 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
2011 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00002012 SDLoc DL(SD);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002013 SDVTList VTList = DAG.getVTList(MVT::Other);
2014
2015 if (Offset)
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00002016 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002017 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002018
2019 SDValue Ops[] = { Chain, Value, Ptr };
2020 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT,
2021 SD->getMemOperand());
2022}
2023
2024// Expand an unaligned 32 or 64-bit integer store node.
Akira Hatanaka63451432013-05-16 20:45:17 +00002025static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG,
2026 bool IsLittle) {
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002027 SDValue Value = SD->getValue(), Chain = SD->getChain();
2028 EVT VT = Value.getValueType();
2029
2030 // Expand
2031 // (store val, baseptr) or
2032 // (truncstore val, baseptr)
2033 // to
2034 // (swl val, (add baseptr, 3))
2035 // (swr val, baseptr)
2036 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
Akira Hatanakafee62c12013-04-11 19:07:14 +00002037 SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002038 IsLittle ? 3 : 0);
Akira Hatanakafee62c12013-04-11 19:07:14 +00002039 return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002040 }
2041
2042 assert(VT == MVT::i64);
2043
2044 // Expand
2045 // (store val, baseptr)
2046 // to
2047 // (sdl val, (add baseptr, 7))
2048 // (sdr val, baseptr)
Akira Hatanakafee62c12013-04-11 19:07:14 +00002049 SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2050 return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002051}
2052
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002053// Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr).
2054static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG) {
2055 SDValue Val = SD->getValue();
2056
2057 if (Val.getOpcode() != ISD::FP_TO_SINT)
2058 return SDValue();
2059
2060 EVT FPTy = EVT::getFloatingPointVT(Val.getValueSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002061 SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy,
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002062 Val.getOperand(0));
2063
Andrew Trickac6d9be2013-05-25 02:42:55 +00002064 return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(),
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002065 SD->getPointerInfo(), SD->isVolatile(),
2066 SD->isNonTemporal(), SD->getAlignment());
2067}
2068
Akira Hatanaka63451432013-05-16 20:45:17 +00002069SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2070 StoreSDNode *SD = cast<StoreSDNode>(Op);
2071 EVT MemVT = SD->getMemoryVT();
2072
2073 // Lower unaligned integer stores.
2074 if ((SD->getAlignment() < MemVT.getSizeInBits() / 8) &&
2075 ((MemVT == MVT::i32) || (MemVT == MVT::i64)))
2076 return lowerUnalignedIntStore(SD, DAG, Subtarget->isLittle());
2077
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002078 return lowerFP_TO_SINT_STORE(SD, DAG);
Akira Hatanaka63451432013-05-16 20:45:17 +00002079}
2080
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002081SDValue MipsTargetLowering::lowerADD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanakae90a3bc2012-11-07 19:10:58 +00002082 if (Op->getOperand(0).getOpcode() != ISD::FRAMEADDR
2083 || cast<ConstantSDNode>
2084 (Op->getOperand(0).getOperand(0))->getZExtValue() != 0
2085 || Op->getOperand(1).getOpcode() != ISD::FRAME_TO_ARGS_OFFSET)
2086 return SDValue();
2087
2088 // The pattern
2089 // (add (frameaddr 0), (frame_to_args_offset))
2090 // results from lowering llvm.eh.dwarf.cfa intrinsic. Transform it to
2091 // (add FrameObject, 0)
2092 // where FrameObject is a fixed StackObject with offset 0 which points to
2093 // the old stack pointer.
2094 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2095 EVT ValTy = Op->getValueType(0);
2096 int FI = MFI->CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
2097 SDValue InArgsAddr = DAG.getFrameIndex(FI, ValTy);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002098 return DAG.getNode(ISD::ADD, SDLoc(Op), ValTy, InArgsAddr,
Akira Hatanakae90a3bc2012-11-07 19:10:58 +00002099 DAG.getConstant(0, ValTy));
2100}
2101
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002102SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op,
2103 SelectionDAG &DAG) const {
2104 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002105 SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy,
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002106 Op.getOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00002107 return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002108}
2109
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002110//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002111// Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002112//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002113
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002114//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002115// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002116// Mips O32 ABI rules:
2117// ---
2118// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002119// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002120// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002121// f64 - Only passed in two aliased f32 registers if no int reg has been used
2122// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002123// not used, it must be shadowed. If only A3 is avaiable, shadow it and
2124// go to stack.
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002125//
2126// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002127//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002128
Duncan Sands1e96bab2010-11-04 10:49:57 +00002129static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +00002130 MVT LocVT, CCValAssign::LocInfo LocInfo,
Akira Hatanakaad341d42013-08-20 23:38:40 +00002131 ISD::ArgFlagsTy ArgFlags, CCState &State,
2132 const uint16_t *F64Regs) {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002133
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002134 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002135
Craig Topperc5eaae42012-03-11 07:57:25 +00002136 static const uint16_t IntRegs[] = {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002137 Mips::A0, Mips::A1, Mips::A2, Mips::A3
2138 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002139 static const uint16_t F32Regs[] = {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002140 Mips::F12, Mips::F14
2141 };
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002142
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002143 // Do not process byval args here.
2144 if (ArgFlags.isByVal())
2145 return true;
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002146
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002147 // Promote i8 and i16
2148 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
2149 LocVT = MVT::i32;
2150 if (ArgFlags.isSExt())
2151 LocInfo = CCValAssign::SExt;
2152 else if (ArgFlags.isZExt())
2153 LocInfo = CCValAssign::ZExt;
2154 else
2155 LocInfo = CCValAssign::AExt;
2156 }
2157
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002158 unsigned Reg;
2159
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002160 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
2161 // is true: function is vararg, argument is 3rd or higher, there is previous
2162 // argument which is not f32 or f64.
2163 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
2164 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00002165 unsigned OrigAlign = ArgFlags.getOrigAlign();
2166 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002167
2168 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002169 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00002170 // If this is the first part of an i64 arg,
2171 // the allocated register must be either A0 or A2.
2172 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
2173 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002174 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002175 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
2176 // Allocate int register and shadow next int register. If first
2177 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002178 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2179 if (Reg == Mips::A1 || Reg == Mips::A3)
2180 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2181 State.AllocateReg(IntRegs, IntRegsSize);
2182 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002183 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
2184 // we are guaranteed to find an available float register
2185 if (ValVT == MVT::f32) {
2186 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
2187 // Shadow int register
2188 State.AllocateReg(IntRegs, IntRegsSize);
2189 } else {
2190 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
2191 // Shadow int registers
2192 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
2193 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
2194 State.AllocateReg(IntRegs, IntRegsSize);
2195 State.AllocateReg(IntRegs, IntRegsSize);
2196 }
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002197 } else
2198 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002199
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002200 if (!Reg) {
2201 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3,
2202 OrigAlign);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002203 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002204 } else
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002205 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002206
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002207 return false;
Akira Hatanaka2c5d6522011-11-12 02:20:46 +00002208}
2209
Akira Hatanakaad341d42013-08-20 23:38:40 +00002210static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT,
2211 MVT LocVT, CCValAssign::LocInfo LocInfo,
2212 ISD::ArgFlagsTy ArgFlags, CCState &State) {
2213 static const uint16_t F64Regs[] = { Mips::D6, Mips::D7 };
2214
2215 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2216}
2217
2218static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT,
2219 MVT LocVT, CCValAssign::LocInfo LocInfo,
2220 ISD::ArgFlagsTy ArgFlags, CCState &State) {
2221 static const uint16_t F64Regs[] = { Mips::D12_64, Mips::D12_64 };
2222
2223 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2224}
2225
Akira Hatanaka2c5d6522011-11-12 02:20:46 +00002226#include "MipsGenCallingConv.inc"
2227
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002228//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002229// Call Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002230//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002231
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002232// Return next O32 integer argument register.
2233static unsigned getNextIntArgReg(unsigned Reg) {
2234 assert((Reg == Mips::A0) || (Reg == Mips::A2));
2235 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
2236}
2237
Akira Hatanaka7d712092012-10-30 19:23:25 +00002238SDValue
2239MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002240 SDValue Chain, SDValue Arg, SDLoc DL,
Akira Hatanaka7d712092012-10-30 19:23:25 +00002241 bool IsTailCall, SelectionDAG &DAG) const {
2242 if (!IsTailCall) {
2243 SDValue PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr,
2244 DAG.getIntPtrConstant(Offset));
2245 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo(), false,
2246 false, 0);
2247 }
2248
2249 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2250 int FI = MFI->CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
2251 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2252 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(),
2253 /*isVolatile=*/ true, false, 0);
2254}
2255
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002256void MipsTargetLowering::
2257getOpndList(SmallVectorImpl<SDValue> &Ops,
2258 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
2259 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
2260 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
2261 // Insert node "GP copy globalreg" before call to function.
2262 //
2263 // R_MIPS_CALL* operators (emitted when non-internal functions are called
2264 // in PIC mode) allow symbols to be resolved via lazy binding.
2265 // The lazy binding stub requires GP to point to the GOT.
2266 if (IsPICCall && !InternalLinkage) {
2267 unsigned GPReg = IsN64 ? Mips::GP_64 : Mips::GP;
2268 EVT Ty = IsN64 ? MVT::i64 : MVT::i32;
2269 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty)));
2270 }
Reed Kotler8453b3f2013-01-24 04:24:02 +00002271
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002272 // Build a sequence of copy-to-reg nodes chained together with token
2273 // chain and flag operands which copy the outgoing args into registers.
2274 // The InFlag in necessary since all emitted instructions must be
2275 // stuck together.
2276 SDValue InFlag;
Reed Kotler8453b3f2013-01-24 04:24:02 +00002277
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002278 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2279 Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, RegsToPass[i].first,
2280 RegsToPass[i].second, InFlag);
2281 InFlag = Chain.getValue(1);
2282 }
Reed Kotler8453b3f2013-01-24 04:24:02 +00002283
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002284 // Add argument registers to the end of the list so that they are
2285 // known live into the call.
2286 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2287 Ops.push_back(CLI.DAG.getRegister(RegsToPass[i].first,
2288 RegsToPass[i].second.getValueType()));
Reed Kotler8453b3f2013-01-24 04:24:02 +00002289
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002290 // Add a register mask operand representing the call-preserved registers.
2291 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2292 const uint32_t *Mask = TRI->getCallPreservedMask(CLI.CallConv);
2293 assert(Mask && "Missing call preserved mask for calling convention");
Reed Kotler46090912013-05-10 22:25:39 +00002294 if (Subtarget->inMips16HardFloat()) {
2295 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
2296 llvm::StringRef Sym = G->getGlobal()->getName();
2297 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
2298 if (F->hasFnAttribute("__Mips16RetHelper")) {
2299 Mask = MipsRegisterInfo::getMips16RetHelperMask();
2300 }
2301 }
2302 }
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002303 Ops.push_back(CLI.DAG.getRegisterMask(Mask));
2304
2305 if (InFlag.getNode())
2306 Ops.push_back(InFlag);
Reed Kotler8453b3f2013-01-24 04:24:02 +00002307}
2308
Dan Gohman98ca4f22009-08-05 01:29:28 +00002309/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00002310/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002311SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002312MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002313 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002314 SelectionDAG &DAG = CLI.DAG;
Andrew Trickac6d9be2013-05-25 02:42:55 +00002315 SDLoc DL = CLI.DL;
Craig Toppera0ec3f92013-07-14 04:42:23 +00002316 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
2317 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
2318 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Akira Hatanakae2d529a2012-07-31 18:46:41 +00002319 SDValue Chain = CLI.Chain;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002320 SDValue Callee = CLI.Callee;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002321 bool &IsTailCall = CLI.IsTailCall;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002322 CallingConv::ID CallConv = CLI.CallConv;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002323 bool IsVarArg = CLI.IsVarArg;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002324
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002325 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002326 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanakad37776d2011-05-20 21:39:54 +00002327 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002328 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002329
2330 // Analyze operands of the call, assigning locations to each operand.
2331 SmallVector<CCValAssign, 16> ArgLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002332 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka82099682011-12-19 19:52:25 +00002333 getTargetMachine(), ArgLocs, *DAG.getContext());
Reed Kotler46090912013-05-10 22:25:39 +00002334 MipsCC::SpecialCallingConvType SpecialCallingConv =
2335 getSpecialCallingConv(Callee);
Akira Hatanakaad341d42013-08-20 23:38:40 +00002336 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo,
2337 SpecialCallingConv);
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002338
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002339 MipsCCInfo.analyzeCallOperands(Outs, IsVarArg,
Reed Kotlerc673f9c2013-08-30 19:40:56 +00002340 Subtarget->mipsSEUsesSoftFloat(),
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00002341 Callee.getNode(), CLI.Args);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002342
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002343 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002344 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka480eeb52012-07-26 23:27:01 +00002345
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002346 // Check if it's really possible to do a tail call.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002347 if (IsTailCall)
2348 IsTailCall =
2349 isEligibleForTailCallOptimization(MipsCCInfo, NextStackOffset,
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002350 *MF.getInfo<MipsFunctionInfo>());
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002351
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002352 if (IsTailCall)
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002353 ++NumTailCalls;
2354
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002355 // Chain is the output chain of the last Load/Store or CopyToReg node.
2356 // ByValChain is the output chain of the last Memcpy node created for copying
2357 // byval arguments to the stack.
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002358 unsigned StackAlignment = TFL->getStackAlignment();
2359 NextStackOffset = RoundUpToAlignment(NextStackOffset, StackAlignment);
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002360 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002361
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002362 if (!IsTailCall)
Andrew Trick6e0b2a02013-05-29 22:03:55 +00002363 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffsetVal, DL);
Akira Hatanakae2d529a2012-07-31 18:46:41 +00002364
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002365 SDValue StackPtr = DAG.getCopyFromReg(Chain, DL,
Akira Hatanakae2d529a2012-07-31 18:46:41 +00002366 IsN64 ? Mips::SP_64 : Mips::SP,
2367 getPointerTy());
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002368
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002369 // With EABI is it possible to have 16 args on registers.
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00002370 std::deque< std::pair<unsigned, SDValue> > RegsToPass;
Dan Gohman475871a2008-07-27 21:46:04 +00002371 SmallVector<SDValue, 8> MemOpChains;
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002372 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002373
2374 // Walk the register/memloc assignments, inserting copies/loads.
2375 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00002376 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002377 CCValAssign &VA = ArgLocs[i];
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002378 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002379 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2380
2381 // ByVal Arg.
2382 if (Flags.isByVal()) {
2383 assert(Flags.getByValSize() &&
2384 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002385 assert(ByValArg != MipsCCInfo.byval_end());
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002386 assert(!IsTailCall &&
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002387 "Do not tail-call optimize if there is a byval argument.");
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002388 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002389 MipsCCInfo, *ByValArg, Flags, Subtarget->isLittle());
2390 ++ByValArg;
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002391 continue;
2392 }
Jia Liubb481f82012-02-28 07:46:26 +00002393
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002394 // Promote the value if needed.
2395 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002396 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002397 case CCValAssign::Full:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002398 if (VA.isRegLoc()) {
2399 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00002400 (ValVT == MVT::f64 && LocVT == MVT::i64) ||
2401 (ValVT == MVT::i64 && LocVT == MVT::f64))
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002402 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002403 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002404 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002405 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002406 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002407 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00002408 if (!Subtarget->isLittle())
2409 std::swap(Lo, Hi);
Jia Liubb481f82012-02-28 07:46:26 +00002410 unsigned LocRegLo = VA.getLocReg();
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002411 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2412 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2413 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002414 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002415 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002416 }
2417 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00002418 case CCValAssign::SExt:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002419 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002420 break;
2421 case CCValAssign::ZExt:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002422 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002423 break;
2424 case CCValAssign::AExt:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002425 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002426 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002427 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002428
2429 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002430 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002431 if (VA.isRegLoc()) {
2432 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00002433 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002434 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002435
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002436 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00002437 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002438
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002439 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00002440 // parameter value to a stack Location
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002441 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002442 Chain, Arg, DL, IsTailCall, DAG));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002443 }
2444
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002445 // Transform all store nodes into one single node because all store
2446 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002447 if (!MemOpChains.empty())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002448 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002449 &MemOpChains[0], MemOpChains.size());
2450
Bill Wendling056292f2008-09-16 21:48:12 +00002451 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002452 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2453 // node so that legalize doesn't hack it.
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002454 bool IsPICCall = (IsN64 || IsPIC); // true if calls are translated to jalr $25
Akira Hatanakaed185da2012-12-13 03:17:29 +00002455 bool GlobalOrExternal = false, InternalLinkage = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002456 SDValue CalleeLo;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002457
2458 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002459 if (IsPICCall) {
Akira Hatanakaed185da2012-12-13 03:17:29 +00002460 InternalLinkage = G->getGlobal()->hasInternalLinkage();
2461
2462 if (InternalLinkage)
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002463 Callee = getAddrLocal(Callee, DAG, HasMips64);
Akira Hatanakaf09a0372012-11-21 20:40:38 +00002464 else if (LargeGOT)
2465 Callee = getAddrGlobalLargeGOT(Callee, DAG, MipsII::MO_CALL_HI16,
2466 MipsII::MO_CALL_LO16);
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002467 else
2468 Callee = getAddrGlobal(Callee, DAG, MipsII::MO_GOT_CALL);
2469 } else
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002470 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, getPointerTy(), 0,
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002471 MipsII::MO_NO_FLAG);
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002472 GlobalOrExternal = true;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002473 }
2474 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakaf09a0372012-11-21 20:40:38 +00002475 if (!IsN64 && !IsPIC) // !N64 && static
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002476 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2477 MipsII::MO_NO_FLAG);
Akira Hatanakaf09a0372012-11-21 20:40:38 +00002478 else if (LargeGOT)
2479 Callee = getAddrGlobalLargeGOT(Callee, DAG, MipsII::MO_CALL_HI16,
2480 MipsII::MO_CALL_LO16);
Akira Hatanaka60689322013-02-22 21:10:03 +00002481 else // N64 || PIC
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002482 Callee = getAddrGlobal(Callee, DAG, MipsII::MO_GOT_CALL);
2483
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002484 GlobalOrExternal = true;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002485 }
2486
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00002487 SmallVector<SDValue, 8> Ops(1, Chain);
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002488 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00002489
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002490 getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal, InternalLinkage,
2491 CLI, Callee, Chain);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002492
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002493 if (IsTailCall)
2494 return DAG.getNode(MipsISD::TailCall, DL, MVT::Other, &Ops[0], Ops.size());
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002495
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002496 Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, &Ops[0], Ops.size());
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002497 SDValue InFlag = Chain.getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002498
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002499 // Create the CALLSEQ_END node.
Akira Hatanaka480eeb52012-07-26 23:27:01 +00002500 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal,
Andrew Trick6e0b2a02013-05-29 22:03:55 +00002501 DAG.getIntPtrConstant(0, true), InFlag, DL);
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002502 InFlag = Chain.getValue(1);
2503
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002504 // Handle result values, copying them out of physregs into vregs that we
2505 // return.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002506 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg,
2507 Ins, DL, DAG, InVals, CLI.Callee.getNode(), CLI.RetTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002508}
2509
Dan Gohman98ca4f22009-08-05 01:29:28 +00002510/// LowerCallResult - Lower the result values of a call into the
2511/// appropriate copies out of appropriate physical registers.
2512SDValue
2513MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002514 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002515 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002516 SDLoc DL, SelectionDAG &DAG,
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002517 SmallVectorImpl<SDValue> &InVals,
2518 const SDNode *CallNode,
2519 const Type *RetTy) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002520 // Assign locations to each value returned by this call.
2521 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002522 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka864f6602012-06-14 21:10:56 +00002523 getTargetMachine(), RVLocs, *DAG.getContext());
Akira Hatanakaad341d42013-08-20 23:38:40 +00002524 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo);
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002525
Reed Kotlerc673f9c2013-08-30 19:40:56 +00002526 MipsCCInfo.analyzeCallResult(Ins, Subtarget->mipsSEUsesSoftFloat(),
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002527 CallNode, RetTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002528
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002529 // Copy all of the result registers out of their specified physreg.
2530 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002531 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(),
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002532 RVLocs[i].getLocVT(), InFlag);
2533 Chain = Val.getValue(1);
2534 InFlag = Val.getValue(2);
2535
2536 if (RVLocs[i].getValVT() != RVLocs[i].getLocVT())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002537 Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getValVT(), Val);
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002538
2539 InVals.push_back(Val);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002540 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002541
Dan Gohman98ca4f22009-08-05 01:29:28 +00002542 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002543}
2544
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002545//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002546// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002547//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002548/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002549/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002550SDValue
2551MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002552 CallingConv::ID CallConv,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002553 bool IsVarArg,
Akira Hatanaka82099682011-12-19 19:52:25 +00002554 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002555 SDLoc DL, SelectionDAG &DAG,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002556 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002557 const {
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00002558 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002559 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00002560 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002561
Dan Gohman1e93df62010-04-17 14:41:14 +00002562 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002563
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002564 // Used with vargs to acumulate store chains.
2565 std::vector<SDValue> OutChains;
2566
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002567 // Assign locations to all of the incoming arguments.
2568 SmallVector<CCValAssign, 16> ArgLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002569 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka82099682011-12-19 19:52:25 +00002570 getTargetMachine(), ArgLocs, *DAG.getContext());
Akira Hatanakaad341d42013-08-20 23:38:40 +00002571 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo);
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002572 Function::const_arg_iterator FuncArg =
2573 DAG.getMachineFunction().getFunction()->arg_begin();
Reed Kotlerc673f9c2013-08-30 19:40:56 +00002574 bool UseSoftFloat = Subtarget->mipsSEUsesSoftFloat();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002575
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002576 MipsCCInfo.analyzeFormalArguments(Ins, UseSoftFloat, FuncArg);
Akira Hatanakab33b34a2012-10-30 19:37:25 +00002577 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(),
2578 MipsCCInfo.hasByValArg());
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002579
Akira Hatanaka4618e0b2012-10-27 00:44:39 +00002580 unsigned CurArgIdx = 0;
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002581 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002582
Akira Hatanaka4618e0b2012-10-27 00:44:39 +00002583 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002584 CCValAssign &VA = ArgLocs[i];
Akira Hatanaka4618e0b2012-10-27 00:44:39 +00002585 std::advance(FuncArg, Ins[i].OrigArgIndex - CurArgIdx);
2586 CurArgIdx = Ins[i].OrigArgIndex;
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002587 EVT ValVT = VA.getValVT();
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002588 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2589 bool IsRegLoc = VA.isRegLoc();
2590
2591 if (Flags.isByVal()) {
2592 assert(Flags.getByValSize() &&
2593 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002594 assert(ByValArg != MipsCCInfo.byval_end());
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002595 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg,
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002596 MipsCCInfo, *ByValArg);
2597 ++ByValArg;
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002598 continue;
2599 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002600
2601 // Arguments stored on registers
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002602 if (IsRegLoc) {
Owen Andersone50ed302009-08-10 22:56:29 +00002603 EVT RegVT = VA.getLocVT();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002604 unsigned ArgReg = VA.getLocReg();
Craig Topper44d23822012-02-22 05:59:10 +00002605 const TargetRegisterClass *RC;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002606
Owen Anderson825b72b2009-08-11 20:47:22 +00002607 if (RegVT == MVT::i32)
Reed Kotlerbacbf1c2012-12-20 06:06:35 +00002608 RC = Subtarget->inMips16Mode()? &Mips::CPU16RegsRegClass :
Akira Hatanaka18587862013-08-06 23:08:38 +00002609 &Mips::GPR32RegClass;
Akira Hatanaka95934842011-09-24 01:34:44 +00002610 else if (RegVT == MVT::i64)
Akira Hatanaka18587862013-08-06 23:08:38 +00002611 RC = &Mips::GPR64RegClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002612 else if (RegVT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00002613 RC = &Mips::FGR32RegClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002614 else if (RegVT == MVT::f64)
Akira Hatanakaad341d42013-08-20 23:38:40 +00002615 RC = Subtarget->isFP64bit() ? &Mips::FGR64RegClass :
2616 &Mips::AFGR64RegClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002617 else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002618 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002619
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002620 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002621 // physical registers into virtual ones
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002622 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC);
2623 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002624
2625 // If this is an 8 or 16-bit value, it has been passed promoted
2626 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002627 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002628 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00002629 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002630 if (VA.getLocInfo() == CCValAssign::SExt)
2631 Opcode = ISD::AssertSext;
2632 else if (VA.getLocInfo() == CCValAssign::ZExt)
2633 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00002634 if (Opcode)
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002635 ArgValue = DAG.getNode(Opcode, DL, RegVT, ArgValue,
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002636 DAG.getValueType(ValVT));
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002637 ArgValue = DAG.getNode(ISD::TRUNCATE, DL, ValVT, ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002638 }
2639
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002640 // Handle floating point arguments passed in integer registers and
2641 // long double arguments passed in floating point registers.
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002642 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002643 (RegVT == MVT::i64 && ValVT == MVT::f64) ||
2644 (RegVT == MVT::f64 && ValVT == MVT::i64))
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002645 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002646 else if (IsO32 && RegVT == MVT::i32 && ValVT == MVT::f64) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002647 unsigned Reg2 = addLiveIn(DAG.getMachineFunction(),
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002648 getNextIntArgReg(ArgReg), RC);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002649 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT);
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002650 if (!Subtarget->isLittle())
2651 std::swap(ArgValue, ArgValue2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002652 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64,
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002653 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002654 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002655
Dan Gohman98ca4f22009-08-05 01:29:28 +00002656 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002657 } else { // VA.isRegLoc()
2658
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002659 // sanity check
2660 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002661
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002662 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002663 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002664 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002665
2666 // Create load nodes to retrieve arguments from the stack
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002667 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002668 InVals.push_back(DAG.getLoad(ValVT, DL, Chain, FIN,
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002669 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002670 false, false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002671 }
2672 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002673
2674 // The mips ABIs for returning structs by value requires that we copy
2675 // the sret argument into $v0 for the return. Save the argument into
2676 // a virtual register so that we can access it from the return points.
2677 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2678 unsigned Reg = MipsFI->getSRetReturnReg();
2679 if (!Reg) {
Akira Hatanaka30580ce2012-10-19 22:11:40 +00002680 Reg = MF.getRegInfo().
2681 createVirtualRegister(getRegClassFor(IsN64 ? MVT::i64 : MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002682 MipsFI->setSRetReturnReg(Reg);
2683 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002684 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[0]);
2685 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002686 }
2687
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002688 if (IsVarArg)
2689 writeVarArgRegs(OutChains, MipsCCInfo, Chain, DL, DAG);
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002690
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002691 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002692 // the size of Ins and InVals. This only happens when on varg functions
2693 if (!OutChains.empty()) {
2694 OutChains.push_back(Chain);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002695 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002696 &OutChains[0], OutChains.size());
2697 }
2698
Dan Gohman98ca4f22009-08-05 01:29:28 +00002699 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002700}
2701
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002702//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002703// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002704//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002705
Akira Hatanaka97d9f082012-10-10 01:27:09 +00002706bool
2707MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002708 MachineFunction &MF, bool IsVarArg,
Akira Hatanaka97d9f082012-10-10 01:27:09 +00002709 const SmallVectorImpl<ISD::OutputArg> &Outs,
2710 LLVMContext &Context) const {
2711 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002712 CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(),
Akira Hatanaka97d9f082012-10-10 01:27:09 +00002713 RVLocs, Context);
2714 return CCInfo.CheckReturn(Outs, RetCC_Mips);
2715}
2716
Dan Gohman98ca4f22009-08-05 01:29:28 +00002717SDValue
2718MipsTargetLowering::LowerReturn(SDValue Chain,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002719 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002720 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002721 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002722 SDLoc DL, SelectionDAG &DAG) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002723 // CCValAssign - represent the assignment of
2724 // the return value to a location
2725 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002726 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002727
2728 // CCState - Info about the registers and stack slot.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002729 CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(), RVLocs,
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002730 *DAG.getContext());
Akira Hatanakaad341d42013-08-20 23:38:40 +00002731 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002732
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002733 // Analyze return values.
Reed Kotlerc673f9c2013-08-30 19:40:56 +00002734 MipsCCInfo.analyzeReturn(Outs, Subtarget->mipsSEUsesSoftFloat(),
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002735 MF.getFunction()->getReturnType());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002736
Dan Gohman475871a2008-07-27 21:46:04 +00002737 SDValue Flag;
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002738 SmallVector<SDValue, 4> RetOps(1, Chain);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002739
2740 // Copy the result values into the output registers.
2741 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002742 SDValue Val = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002743 CCValAssign &VA = RVLocs[i];
2744 assert(VA.isRegLoc() && "Can only return in registers!");
2745
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002746 if (RVLocs[i].getValVT() != RVLocs[i].getLocVT())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002747 Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getLocVT(), Val);
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002748
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002749 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002750
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002751 // Guarantee that all emitted copies are stuck together with flags.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002752 Flag = Chain.getValue(1);
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002753 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002754 }
2755
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002756 // The mips ABIs for returning structs by value requires that we copy
2757 // the sret argument into $v0 for the return. We saved the argument into
2758 // a virtual register in the entry block, so now we copy the value out
2759 // and into $v0.
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002760 if (MF.getFunction()->hasStructRetAttr()) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002761 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2762 unsigned Reg = MipsFI->getSRetReturnReg();
2763
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002764 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00002765 llvm_unreachable("sret virtual register not created in the entry block");
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002766 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Akira Hatanaka2ef5bd32012-10-24 02:10:54 +00002767 unsigned V0 = IsN64 ? Mips::V0_64 : Mips::V0;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002768
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002769 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002770 Flag = Chain.getValue(1);
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002771 RetOps.push_back(DAG.getRegister(V0, getPointerTy()));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002772 }
2773
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002774 RetOps[0] = Chain; // Update chain.
Akira Hatanaka182ef6f2012-07-10 00:19:06 +00002775
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002776 // Add the flag if we have it.
2777 if (Flag.getNode())
2778 RetOps.push_back(Flag);
2779
2780 // Return on Mips is always a "jr $ra"
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002781 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, &RetOps[0], RetOps.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002782}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002783
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002784//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002785// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002786//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002787
2788/// getConstraintType - Given a constraint letter, return the type of
2789/// constraint it is for this target.
2790MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002791getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002792{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002793 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002794 // GCC config/mips/constraints.md
2795 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002796 // 'd' : An address register. Equivalent to r
2797 // unless generating MIPS16 code.
2798 // 'y' : Equivalent to r; retained for
2799 // backwards compatibility.
Eric Christopher1d5a3922012-05-07 06:25:10 +00002800 // 'c' : A register suitable for use in an indirect
2801 // jump. This will always be $25 for -mabicalls.
Eric Christopheraf97f732012-05-07 06:25:19 +00002802 // 'l' : The lo register. 1 word storage.
2803 // 'x' : The hilo register pair. Double word storage.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002804 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002805 switch (Constraint[0]) {
2806 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002807 case 'd':
2808 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002809 case 'f':
Eric Christopher1d5a3922012-05-07 06:25:10 +00002810 case 'c':
Eric Christopher4adbefe2012-05-07 06:25:15 +00002811 case 'l':
Eric Christopheraf97f732012-05-07 06:25:19 +00002812 case 'x':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002813 return C_RegisterClass;
Jack Carter0b9675d2013-03-04 21:33:15 +00002814 case 'R':
2815 return C_Memory;
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002816 }
2817 }
2818 return TargetLowering::getConstraintType(Constraint);
2819}
2820
John Thompson44ab89e2010-10-29 17:29:13 +00002821/// Examine constraint type and operand type and determine a weight value.
2822/// This object must already have been set up with the operand type
2823/// and the current alternative constraint selected.
2824TargetLowering::ConstraintWeight
2825MipsTargetLowering::getSingleConstraintMatchWeight(
2826 AsmOperandInfo &info, const char *constraint) const {
2827 ConstraintWeight weight = CW_Invalid;
2828 Value *CallOperandVal = info.CallOperandVal;
2829 // If we don't have a value, we can't do a match,
2830 // but allow it at the lowest weight.
2831 if (CallOperandVal == NULL)
2832 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002833 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00002834 // Look at the constraint type.
2835 switch (*constraint) {
2836 default:
2837 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2838 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002839 case 'd':
2840 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00002841 if (type->isIntegerTy())
2842 weight = CW_Register;
2843 break;
2844 case 'f':
2845 if (type->isFloatTy())
2846 weight = CW_Register;
2847 break;
Eric Christopher1d5a3922012-05-07 06:25:10 +00002848 case 'c': // $25 for indirect jumps
Eric Christopher4adbefe2012-05-07 06:25:15 +00002849 case 'l': // lo register
Eric Christopheraf97f732012-05-07 06:25:19 +00002850 case 'x': // hilo register pair
Eric Christopher1d5a3922012-05-07 06:25:10 +00002851 if (type->isIntegerTy())
2852 weight = CW_SpecificReg;
2853 break;
Eric Christopher50ab0392012-05-07 03:13:32 +00002854 case 'I': // signed 16 bit immediate
Eric Christophere5076d42012-05-07 03:13:42 +00002855 case 'J': // integer zero
Eric Christopherf49f8462012-05-07 05:46:29 +00002856 case 'K': // unsigned 16 bit immediate
Eric Christopher5ac47bb2012-05-07 05:46:37 +00002857 case 'L': // signed 32 bit immediate where lower 16 bits are 0
Eric Christopher60cfc792012-05-07 05:46:43 +00002858 case 'N': // immediate in the range of -65535 to -1 (inclusive)
Eric Christopher1ce20342012-05-07 05:46:48 +00002859 case 'O': // signed 15 bit immediate (+- 16383)
Eric Christopher54412a72012-05-07 06:25:02 +00002860 case 'P': // immediate in the range of 65535 to 1 (inclusive)
Eric Christopher50ab0392012-05-07 03:13:32 +00002861 if (isa<ConstantInt>(CallOperandVal))
2862 weight = CW_Constant;
2863 break;
Jack Carter0b9675d2013-03-04 21:33:15 +00002864 case 'R':
2865 weight = CW_Memory;
2866 break;
John Thompson44ab89e2010-10-29 17:29:13 +00002867 }
2868 return weight;
2869}
2870
Akira Hatanakabfb07b12013-08-14 00:21:25 +00002871/// This is a helper function to parse a physical register string and split it
2872/// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag
2873/// that is returned indicates whether parsing was successful. The second flag
2874/// is true if the numeric part exists.
2875static std::pair<bool, bool>
2876parsePhysicalReg(const StringRef &C, std::string &Prefix,
2877 unsigned long long &Reg) {
2878 if (C.front() != '{' || C.back() != '}')
2879 return std::make_pair(false, false);
2880
2881 // Search for the first numeric character.
2882 StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1;
2883 I = std::find_if(B, E, std::ptr_fun(isdigit));
2884
2885 Prefix.assign(B, I - B);
2886
2887 // The second flag is set to false if no numeric characters were found.
2888 if (I == E)
2889 return std::make_pair(true, false);
2890
2891 // Parse the numeric characters.
2892 return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg),
2893 true);
2894}
2895
2896std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering::
2897parseRegForInlineAsmConstraint(const StringRef &C, MVT VT) const {
2898 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2899 const TargetRegisterClass *RC;
2900 std::string Prefix;
2901 unsigned long long Reg;
2902
2903 std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg);
2904
2905 if (!R.first)
2906 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2907
2908 if ((Prefix == "hi" || Prefix == "lo")) { // Parse hi/lo.
2909 // No numeric characters follow "hi" or "lo".
2910 if (R.second)
2911 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2912
2913 RC = TRI->getRegClass(Prefix == "hi" ?
Akira Hatanakacbaf6d02013-08-14 00:47:08 +00002914 Mips::HI32RegClassID : Mips::LO32RegClassID);
Akira Hatanakabfb07b12013-08-14 00:21:25 +00002915 return std::make_pair(*(RC->begin()), RC);
2916 }
2917
2918 if (!R.second)
2919 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2920
2921 if (Prefix == "$f") { // Parse $f0-$f31.
2922 // If the size of FP registers is 64-bit or Reg is an even number, select
2923 // the 64-bit register class. Otherwise, select the 32-bit register class.
2924 if (VT == MVT::Other)
2925 VT = (Subtarget->isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32;
2926
2927 RC= getRegClassFor(VT);
2928
2929 if (RC == &Mips::AFGR64RegClass) {
2930 assert(Reg % 2 == 0);
2931 Reg >>= 1;
2932 }
2933 } else if (Prefix == "$fcc") { // Parse $fcc0-$fcc7.
2934 RC = TRI->getRegClass(Mips::FCCRegClassID);
2935 } else { // Parse $0-$31.
2936 assert(Prefix == "$");
2937 RC = getRegClassFor((VT == MVT::Other) ? MVT::i32 : VT);
2938 }
2939
2940 assert(Reg < RC->getNumRegs());
2941 return std::make_pair(*(RC->begin() + Reg), RC);
2942}
2943
Eric Christopher38d64262011-06-29 19:33:04 +00002944/// Given a register class constraint, like 'r', if this corresponds directly
2945/// to an LLVM register class, return a register of 0 and the register class
2946/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002947std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Chad Rosier5b3fca52013-06-22 18:37:38 +00002948getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002949{
2950 if (Constraint.size() == 1) {
2951 switch (Constraint[0]) {
Eric Christopher314aff12011-06-29 19:04:31 +00002952 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
2953 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002954 case 'r':
Akira Hatanakaafc945b2012-09-12 23:27:55 +00002955 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
2956 if (Subtarget->inMips16Mode())
2957 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
Akira Hatanaka18587862013-08-06 23:08:38 +00002958 return std::make_pair(0U, &Mips::GPR32RegClass);
Akira Hatanakaafc945b2012-09-12 23:27:55 +00002959 }
Jack Carter10de0252012-07-02 23:35:23 +00002960 if (VT == MVT::i64 && !HasMips64)
Akira Hatanaka18587862013-08-06 23:08:38 +00002961 return std::make_pair(0U, &Mips::GPR32RegClass);
Eric Christopher0ed1f762012-05-07 03:13:22 +00002962 if (VT == MVT::i64 && HasMips64)
Akira Hatanaka18587862013-08-06 23:08:38 +00002963 return std::make_pair(0U, &Mips::GPR64RegClass);
Eric Christopher0ed1f762012-05-07 03:13:22 +00002964 // This will generate an error message
2965 return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002966 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00002967 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00002968 return std::make_pair(0U, &Mips::FGR32RegClass);
Akira Hatanakacb9dd722012-01-04 02:45:01 +00002969 if ((VT == MVT::f64) && (!Subtarget->isSingleFloat())) {
2970 if (Subtarget->isFP64bit())
Craig Topper420761a2012-04-20 07:30:17 +00002971 return std::make_pair(0U, &Mips::FGR64RegClass);
2972 return std::make_pair(0U, &Mips::AFGR64RegClass);
Akira Hatanakacb9dd722012-01-04 02:45:01 +00002973 }
Eric Christopher1d5a3922012-05-07 06:25:10 +00002974 break;
2975 case 'c': // register suitable for indirect jump
2976 if (VT == MVT::i32)
Akira Hatanaka18587862013-08-06 23:08:38 +00002977 return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass);
Eric Christopher1d5a3922012-05-07 06:25:10 +00002978 assert(VT == MVT::i64 && "Unexpected type.");
Akira Hatanaka18587862013-08-06 23:08:38 +00002979 return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass);
Eric Christopher4adbefe2012-05-07 06:25:15 +00002980 case 'l': // register suitable for indirect jump
2981 if (VT == MVT::i32)
Akira Hatanakacbaf6d02013-08-14 00:47:08 +00002982 return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass);
2983 return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass);
Eric Christopheraf97f732012-05-07 06:25:19 +00002984 case 'x': // register suitable for indirect jump
2985 // Fixme: Not triggering the use of both hi and low
2986 // This will generate an error message
2987 return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002988 }
2989 }
Akira Hatanakabfb07b12013-08-14 00:21:25 +00002990
2991 std::pair<unsigned, const TargetRegisterClass *> R;
2992 R = parseRegForInlineAsmConstraint(Constraint, VT);
2993
2994 if (R.second)
2995 return R;
2996
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002997 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2998}
2999
Eric Christopher50ab0392012-05-07 03:13:32 +00003000/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3001/// vector. If it is invalid, don't add anything to Ops.
3002void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3003 std::string &Constraint,
3004 std::vector<SDValue>&Ops,
3005 SelectionDAG &DAG) const {
3006 SDValue Result(0, 0);
3007
3008 // Only support length 1 constraints for now.
3009 if (Constraint.length() > 1) return;
3010
3011 char ConstraintLetter = Constraint[0];
3012 switch (ConstraintLetter) {
3013 default: break; // This will fall through to the generic implementation
3014 case 'I': // Signed 16 bit constant
3015 // If this fails, the parent routine will give an error
3016 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3017 EVT Type = Op.getValueType();
3018 int64_t Val = C->getSExtValue();
3019 if (isInt<16>(Val)) {
3020 Result = DAG.getTargetConstant(Val, Type);
3021 break;
3022 }
3023 }
3024 return;
Eric Christophere5076d42012-05-07 03:13:42 +00003025 case 'J': // integer zero
3026 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3027 EVT Type = Op.getValueType();
3028 int64_t Val = C->getZExtValue();
3029 if (Val == 0) {
3030 Result = DAG.getTargetConstant(0, Type);
3031 break;
3032 }
3033 }
3034 return;
Eric Christopherf49f8462012-05-07 05:46:29 +00003035 case 'K': // unsigned 16 bit immediate
3036 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3037 EVT Type = Op.getValueType();
3038 uint64_t Val = (uint64_t)C->getZExtValue();
3039 if (isUInt<16>(Val)) {
3040 Result = DAG.getTargetConstant(Val, Type);
3041 break;
3042 }
3043 }
3044 return;
Eric Christopher5ac47bb2012-05-07 05:46:37 +00003045 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3046 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3047 EVT Type = Op.getValueType();
3048 int64_t Val = C->getSExtValue();
3049 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
3050 Result = DAG.getTargetConstant(Val, Type);
3051 break;
3052 }
3053 }
3054 return;
Eric Christopher60cfc792012-05-07 05:46:43 +00003055 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3056 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3057 EVT Type = Op.getValueType();
3058 int64_t Val = C->getSExtValue();
3059 if ((Val >= -65535) && (Val <= -1)) {
3060 Result = DAG.getTargetConstant(Val, Type);
3061 break;
3062 }
3063 }
3064 return;
Eric Christopher1ce20342012-05-07 05:46:48 +00003065 case 'O': // signed 15 bit immediate
3066 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3067 EVT Type = Op.getValueType();
3068 int64_t Val = C->getSExtValue();
3069 if ((isInt<15>(Val))) {
3070 Result = DAG.getTargetConstant(Val, Type);
3071 break;
3072 }
3073 }
3074 return;
Eric Christopher54412a72012-05-07 06:25:02 +00003075 case 'P': // immediate in the range of 1 to 65535 (inclusive)
3076 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3077 EVT Type = Op.getValueType();
3078 int64_t Val = C->getSExtValue();
3079 if ((Val <= 65535) && (Val >= 1)) {
3080 Result = DAG.getTargetConstant(Val, Type);
3081 break;
3082 }
3083 }
3084 return;
Eric Christopher50ab0392012-05-07 03:13:32 +00003085 }
3086
3087 if (Result.getNode()) {
3088 Ops.push_back(Result);
3089 return;
3090 }
3091
3092 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3093}
3094
Dan Gohman6520e202008-10-18 02:06:02 +00003095bool
Akira Hatanaka94e47282012-11-17 00:25:41 +00003096MipsTargetLowering::isLegalAddressingMode(const AddrMode &AM, Type *Ty) const {
3097 // No global is ever allowed as a base.
3098 if (AM.BaseGV)
3099 return false;
3100
3101 switch (AM.Scale) {
3102 case 0: // "r+i" or just "i", depending on HasBaseReg.
3103 break;
3104 case 1:
3105 if (!AM.HasBaseReg) // allow "r+i".
3106 break;
3107 return false; // disallow "r+r" or "r+r+i".
3108 default:
3109 return false;
3110 }
3111
3112 return true;
3113}
3114
3115bool
Dan Gohman6520e202008-10-18 02:06:02 +00003116MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3117 // The Mips target isn't yet aware of offsets.
3118 return false;
3119}
Evan Chengeb2f9692009-10-27 19:56:55 +00003120
Akira Hatanakae193b322012-06-13 19:33:32 +00003121EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00003122 unsigned SrcAlign,
3123 bool IsMemset, bool ZeroMemset,
Akira Hatanakae193b322012-06-13 19:33:32 +00003124 bool MemcpyStrSrc,
3125 MachineFunction &MF) const {
3126 if (Subtarget->hasMips64())
3127 return MVT::i64;
3128
3129 return MVT::i32;
3130}
3131
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003132bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
3133 if (VT != MVT::f32 && VT != MVT::f64)
3134 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00003135 if (Imm.isNegZero())
3136 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00003137 return Imm.isZero();
3138}
Akira Hatanaka6c2cf8b2012-02-03 04:33:00 +00003139
3140unsigned MipsTargetLowering::getJumpTableEncoding() const {
3141 if (IsN64)
3142 return MachineJumpTableInfo::EK_GPRel64BlockAddress;
Jia Liubb481f82012-02-28 07:46:26 +00003143
Akira Hatanaka6c2cf8b2012-02-03 04:33:00 +00003144 return TargetLowering::getJumpTableEncoding();
3145}
Akira Hatanaka7887c902012-10-26 23:56:38 +00003146
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003147/// This function returns true if CallSym is a long double emulation routine.
3148static bool isF128SoftLibCall(const char *CallSym) {
3149 const char *const LibCalls[] =
3150 {"__addtf3", "__divtf3", "__eqtf2", "__extenddftf2", "__extendsftf2",
3151 "__fixtfdi", "__fixtfsi", "__fixtfti", "__fixunstfdi", "__fixunstfsi",
3152 "__fixunstfti", "__floatditf", "__floatsitf", "__floattitf",
3153 "__floatunditf", "__floatunsitf", "__floatuntitf", "__getf2", "__gttf2",
3154 "__letf2", "__lttf2", "__multf3", "__netf2", "__powitf2", "__subtf3",
3155 "__trunctfdf2", "__trunctfsf2", "__unordtf2",
3156 "ceill", "copysignl", "cosl", "exp2l", "expl", "floorl", "fmal", "fmodl",
3157 "log10l", "log2l", "logl", "nearbyintl", "powl", "rintl", "sinl", "sqrtl",
3158 "truncl"};
3159
3160 const char * const *End = LibCalls + array_lengthof(LibCalls);
3161
3162 // Check that LibCalls is sorted alphabetically.
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00003163 MipsTargetLowering::LTStr Comp;
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003164
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00003165#ifndef NDEBUG
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003166 for (const char * const *I = LibCalls; I < End - 1; ++I)
3167 assert(Comp(*I, *(I + 1)));
3168#endif
3169
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00003170 return std::binary_search(LibCalls, End, CallSym, Comp);
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003171}
3172
3173/// This function returns true if Ty is fp128 or i128 which was originally a
3174/// fp128.
3175static bool originalTypeIsF128(const Type *Ty, const SDNode *CallNode) {
3176 if (Ty->isFP128Ty())
3177 return true;
3178
3179 const ExternalSymbolSDNode *ES =
3180 dyn_cast_or_null<const ExternalSymbolSDNode>(CallNode);
3181
3182 // If the Ty is i128 and the function being called is a long double emulation
3183 // routine, then the original type is f128.
3184 return (ES && Ty->isIntegerTy(128) && isF128SoftLibCall(ES->getSymbol()));
3185}
3186
Reed Kotler46090912013-05-10 22:25:39 +00003187MipsTargetLowering::MipsCC::SpecialCallingConvType
3188 MipsTargetLowering::getSpecialCallingConv(SDValue Callee) const {
3189 MipsCC::SpecialCallingConvType SpecialCallingConv =
3190 MipsCC::NoSpecialCallingConv;;
3191 if (Subtarget->inMips16HardFloat()) {
3192 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3193 llvm::StringRef Sym = G->getGlobal()->getName();
3194 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
3195 if (F->hasFnAttribute("__Mips16RetHelper")) {
3196 SpecialCallingConv = MipsCC::Mips16RetHelperConv;
3197 }
3198 }
3199 }
3200 return SpecialCallingConv;
3201}
3202
3203MipsTargetLowering::MipsCC::MipsCC(
Akira Hatanakaad341d42013-08-20 23:38:40 +00003204 CallingConv::ID CC, bool IsO32_, bool IsFP64_, CCState &Info,
Reed Kotler46090912013-05-10 22:25:39 +00003205 MipsCC::SpecialCallingConvType SpecialCallingConv_)
Akira Hatanakaad341d42013-08-20 23:38:40 +00003206 : CCInfo(Info), CallConv(CC), IsO32(IsO32_), IsFP64(IsFP64_),
Reed Kotler46090912013-05-10 22:25:39 +00003207 SpecialCallingConv(SpecialCallingConv_){
Akira Hatanaka7887c902012-10-26 23:56:38 +00003208 // Pre-allocate reserved argument area.
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003209 CCInfo.AllocateStack(reservedArgArea(), 1);
Akira Hatanaka7887c902012-10-26 23:56:38 +00003210}
3211
Reed Kotler46090912013-05-10 22:25:39 +00003212
Akira Hatanaka7887c902012-10-26 23:56:38 +00003213void MipsTargetLowering::MipsCC::
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003214analyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Args,
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00003215 bool IsVarArg, bool IsSoftFloat, const SDNode *CallNode,
3216 std::vector<ArgListEntry> &FuncArgs) {
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003217 assert((CallConv != CallingConv::Fast || !IsVarArg) &&
3218 "CallingConv::Fast shouldn't be used for vararg functions.");
3219
Akira Hatanaka7887c902012-10-26 23:56:38 +00003220 unsigned NumOpnds = Args.size();
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003221 llvm::CCAssignFn *FixedFn = fixedArgFn(), *VarFn = varArgFn();
Akira Hatanaka7887c902012-10-26 23:56:38 +00003222
3223 for (unsigned I = 0; I != NumOpnds; ++I) {
3224 MVT ArgVT = Args[I].VT;
3225 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
3226 bool R;
3227
3228 if (ArgFlags.isByVal()) {
3229 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
3230 continue;
3231 }
3232
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003233 if (IsVarArg && !Args[I].IsFixed)
Akira Hatanaka7887c902012-10-26 23:56:38 +00003234 R = VarFn(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00003235 else {
3236 MVT RegVT = getRegVT(ArgVT, FuncArgs[Args[I].OrigArgIndex].Ty, CallNode,
3237 IsSoftFloat);
3238 R = FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo);
3239 }
Akira Hatanaka7887c902012-10-26 23:56:38 +00003240
3241 if (R) {
3242#ifndef NDEBUG
3243 dbgs() << "Call operand #" << I << " has unhandled type "
3244 << EVT(ArgVT).getEVTString();
3245#endif
3246 llvm_unreachable(0);
3247 }
3248 }
3249}
3250
3251void MipsTargetLowering::MipsCC::
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003252analyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Args,
3253 bool IsSoftFloat, Function::const_arg_iterator FuncArg) {
Akira Hatanaka7887c902012-10-26 23:56:38 +00003254 unsigned NumArgs = Args.size();
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003255 llvm::CCAssignFn *FixedFn = fixedArgFn();
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003256 unsigned CurArgIdx = 0;
Akira Hatanaka7887c902012-10-26 23:56:38 +00003257
3258 for (unsigned I = 0; I != NumArgs; ++I) {
3259 MVT ArgVT = Args[I].VT;
3260 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003261 std::advance(FuncArg, Args[I].OrigArgIndex - CurArgIdx);
3262 CurArgIdx = Args[I].OrigArgIndex;
Akira Hatanaka7887c902012-10-26 23:56:38 +00003263
3264 if (ArgFlags.isByVal()) {
3265 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
3266 continue;
3267 }
3268
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003269 MVT RegVT = getRegVT(ArgVT, FuncArg->getType(), 0, IsSoftFloat);
3270
3271 if (!FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo))
Akira Hatanaka7887c902012-10-26 23:56:38 +00003272 continue;
3273
3274#ifndef NDEBUG
3275 dbgs() << "Formal Arg #" << I << " has unhandled type "
3276 << EVT(ArgVT).getEVTString();
3277#endif
3278 llvm_unreachable(0);
3279 }
3280}
3281
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003282template<typename Ty>
3283void MipsTargetLowering::MipsCC::
3284analyzeReturn(const SmallVectorImpl<Ty> &RetVals, bool IsSoftFloat,
3285 const SDNode *CallNode, const Type *RetTy) const {
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003286 CCAssignFn *Fn;
3287
3288 if (IsSoftFloat && originalTypeIsF128(RetTy, CallNode))
3289 Fn = RetCC_F128Soft;
3290 else
3291 Fn = RetCC_Mips;
3292
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003293 for (unsigned I = 0, E = RetVals.size(); I < E; ++I) {
3294 MVT VT = RetVals[I].VT;
3295 ISD::ArgFlagsTy Flags = RetVals[I].Flags;
3296 MVT RegVT = this->getRegVT(VT, RetTy, CallNode, IsSoftFloat);
3297
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003298 if (Fn(I, VT, RegVT, CCValAssign::Full, Flags, this->CCInfo)) {
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003299#ifndef NDEBUG
3300 dbgs() << "Call result #" << I << " has unhandled type "
3301 << EVT(VT).getEVTString() << '\n';
3302#endif
3303 llvm_unreachable(0);
3304 }
3305 }
3306}
3307
3308void MipsTargetLowering::MipsCC::
3309analyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins, bool IsSoftFloat,
3310 const SDNode *CallNode, const Type *RetTy) const {
3311 analyzeReturn(Ins, IsSoftFloat, CallNode, RetTy);
3312}
3313
3314void MipsTargetLowering::MipsCC::
3315analyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs, bool IsSoftFloat,
3316 const Type *RetTy) const {
3317 analyzeReturn(Outs, IsSoftFloat, 0, RetTy);
3318}
3319
Akira Hatanaka7887c902012-10-26 23:56:38 +00003320void
3321MipsTargetLowering::MipsCC::handleByValArg(unsigned ValNo, MVT ValVT,
3322 MVT LocVT,
3323 CCValAssign::LocInfo LocInfo,
3324 ISD::ArgFlagsTy ArgFlags) {
3325 assert(ArgFlags.getByValSize() && "Byval argument's size shouldn't be 0.");
3326
3327 struct ByValArgInfo ByVal;
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003328 unsigned RegSize = regSize();
Akira Hatanaka7887c902012-10-26 23:56:38 +00003329 unsigned ByValSize = RoundUpToAlignment(ArgFlags.getByValSize(), RegSize);
3330 unsigned Align = std::min(std::max(ArgFlags.getByValAlign(), RegSize),
3331 RegSize * 2);
3332
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003333 if (useRegsForByval())
Akira Hatanaka7887c902012-10-26 23:56:38 +00003334 allocateRegs(ByVal, ByValSize, Align);
3335
3336 // Allocate space on caller's stack.
3337 ByVal.Address = CCInfo.AllocateStack(ByValSize - RegSize * ByVal.NumRegs,
3338 Align);
3339 CCInfo.addLoc(CCValAssign::getMem(ValNo, ValVT, ByVal.Address, LocVT,
3340 LocInfo));
3341 ByValArgs.push_back(ByVal);
3342}
3343
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003344unsigned MipsTargetLowering::MipsCC::numIntArgRegs() const {
3345 return IsO32 ? array_lengthof(O32IntRegs) : array_lengthof(Mips64IntRegs);
3346}
3347
3348unsigned MipsTargetLowering::MipsCC::reservedArgArea() const {
3349 return (IsO32 && (CallConv != CallingConv::Fast)) ? 16 : 0;
3350}
3351
3352const uint16_t *MipsTargetLowering::MipsCC::intArgRegs() const {
3353 return IsO32 ? O32IntRegs : Mips64IntRegs;
3354}
3355
3356llvm::CCAssignFn *MipsTargetLowering::MipsCC::fixedArgFn() const {
3357 if (CallConv == CallingConv::Fast)
3358 return CC_Mips_FastCC;
3359
Reed Kotler46090912013-05-10 22:25:39 +00003360 if (SpecialCallingConv == Mips16RetHelperConv)
3361 return CC_Mips16RetHelper;
Akira Hatanakaad341d42013-08-20 23:38:40 +00003362 return IsO32 ? (IsFP64 ? CC_MipsO32_FP64 : CC_MipsO32_FP32) : CC_MipsN;
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003363}
3364
3365llvm::CCAssignFn *MipsTargetLowering::MipsCC::varArgFn() const {
Akira Hatanakaad341d42013-08-20 23:38:40 +00003366 return IsO32 ? (IsFP64 ? CC_MipsO32_FP64 : CC_MipsO32_FP32) : CC_MipsN_VarArg;
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003367}
3368
3369const uint16_t *MipsTargetLowering::MipsCC::shadowRegs() const {
3370 return IsO32 ? O32IntRegs : Mips64DPRegs;
3371}
3372
Akira Hatanaka7887c902012-10-26 23:56:38 +00003373void MipsTargetLowering::MipsCC::allocateRegs(ByValArgInfo &ByVal,
3374 unsigned ByValSize,
3375 unsigned Align) {
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003376 unsigned RegSize = regSize(), NumIntArgRegs = numIntArgRegs();
3377 const uint16_t *IntArgRegs = intArgRegs(), *ShadowRegs = shadowRegs();
Akira Hatanaka7887c902012-10-26 23:56:38 +00003378 assert(!(ByValSize % RegSize) && !(Align % RegSize) &&
3379 "Byval argument's size and alignment should be a multiple of"
3380 "RegSize.");
3381
3382 ByVal.FirstIdx = CCInfo.getFirstUnallocated(IntArgRegs, NumIntArgRegs);
3383
3384 // If Align > RegSize, the first arg register must be even.
3385 if ((Align > RegSize) && (ByVal.FirstIdx % 2)) {
3386 CCInfo.AllocateReg(IntArgRegs[ByVal.FirstIdx], ShadowRegs[ByVal.FirstIdx]);
3387 ++ByVal.FirstIdx;
3388 }
3389
3390 // Mark the registers allocated.
3391 for (unsigned I = ByVal.FirstIdx; ByValSize && (I < NumIntArgRegs);
3392 ByValSize -= RegSize, ++I, ++ByVal.NumRegs)
3393 CCInfo.AllocateReg(IntArgRegs[I], ShadowRegs[I]);
3394}
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003395
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003396MVT MipsTargetLowering::MipsCC::getRegVT(MVT VT, const Type *OrigTy,
3397 const SDNode *CallNode,
3398 bool IsSoftFloat) const {
3399 if (IsSoftFloat || IsO32)
3400 return VT;
3401
3402 // Check if the original type was fp128.
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003403 if (originalTypeIsF128(OrigTy, CallNode)) {
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003404 assert(VT == MVT::i64);
3405 return MVT::f64;
3406 }
3407
3408 return VT;
3409}
3410
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003411void MipsTargetLowering::
Andrew Trickac6d9be2013-05-25 02:42:55 +00003412copyByValRegs(SDValue Chain, SDLoc DL, std::vector<SDValue> &OutChains,
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003413 SelectionDAG &DAG, const ISD::ArgFlagsTy &Flags,
3414 SmallVectorImpl<SDValue> &InVals, const Argument *FuncArg,
3415 const MipsCC &CC, const ByValArgInfo &ByVal) const {
3416 MachineFunction &MF = DAG.getMachineFunction();
3417 MachineFrameInfo *MFI = MF.getFrameInfo();
3418 unsigned RegAreaSize = ByVal.NumRegs * CC.regSize();
3419 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
3420 int FrameObjOffset;
3421
3422 if (RegAreaSize)
3423 FrameObjOffset = (int)CC.reservedArgArea() -
3424 (int)((CC.numIntArgRegs() - ByVal.FirstIdx) * CC.regSize());
3425 else
3426 FrameObjOffset = ByVal.Address;
3427
3428 // Create frame object.
3429 EVT PtrTy = getPointerTy();
3430 int FI = MFI->CreateFixedObject(FrameObjSize, FrameObjOffset, true);
3431 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
3432 InVals.push_back(FIN);
3433
3434 if (!ByVal.NumRegs)
3435 return;
3436
3437 // Copy arg registers.
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00003438 MVT RegTy = MVT::getIntegerVT(CC.regSize() * 8);
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003439 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3440
3441 for (unsigned I = 0; I < ByVal.NumRegs; ++I) {
3442 unsigned ArgReg = CC.intArgRegs()[ByVal.FirstIdx + I];
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003443 unsigned VReg = addLiveIn(MF, ArgReg, RC);
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003444 unsigned Offset = I * CC.regSize();
3445 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
3446 DAG.getConstant(Offset, PtrTy));
3447 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
3448 StorePtr, MachinePointerInfo(FuncArg, Offset),
3449 false, false, 0);
3450 OutChains.push_back(Store);
3451 }
3452}
Akira Hatanakadb40ede2012-10-27 00:16:36 +00003453
3454// Copy byVal arg to registers and stack.
3455void MipsTargetLowering::
Andrew Trickac6d9be2013-05-25 02:42:55 +00003456passByValArg(SDValue Chain, SDLoc DL,
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00003457 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
Craig Toppera0ec3f92013-07-14 04:42:23 +00003458 SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr,
Akira Hatanakadb40ede2012-10-27 00:16:36 +00003459 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
3460 const MipsCC &CC, const ByValArgInfo &ByVal,
3461 const ISD::ArgFlagsTy &Flags, bool isLittle) const {
3462 unsigned ByValSize = Flags.getByValSize();
3463 unsigned Offset = 0; // Offset in # of bytes from the beginning of struct.
3464 unsigned RegSize = CC.regSize();
3465 unsigned Alignment = std::min(Flags.getByValAlign(), RegSize);
3466 EVT PtrTy = getPointerTy(), RegTy = MVT::getIntegerVT(RegSize * 8);
3467
3468 if (ByVal.NumRegs) {
3469 const uint16_t *ArgRegs = CC.intArgRegs();
3470 bool LeftoverBytes = (ByVal.NumRegs * RegSize > ByValSize);
3471 unsigned I = 0;
3472
3473 // Copy words to registers.
3474 for (; I < ByVal.NumRegs - LeftoverBytes; ++I, Offset += RegSize) {
3475 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3476 DAG.getConstant(Offset, PtrTy));
3477 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
3478 MachinePointerInfo(), false, false, false,
3479 Alignment);
3480 MemOpChains.push_back(LoadVal.getValue(1));
3481 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3482 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
3483 }
3484
3485 // Return if the struct has been fully copied.
3486 if (ByValSize == Offset)
3487 return;
3488
3489 // Copy the remainder of the byval argument with sub-word loads and shifts.
3490 if (LeftoverBytes) {
3491 assert((ByValSize > Offset) && (ByValSize < Offset + RegSize) &&
3492 "Size of the remainder should be smaller than RegSize.");
3493 SDValue Val;
3494
3495 for (unsigned LoadSize = RegSize / 2, TotalSizeLoaded = 0;
3496 Offset < ByValSize; LoadSize /= 2) {
3497 unsigned RemSize = ByValSize - Offset;
3498
3499 if (RemSize < LoadSize)
3500 continue;
3501
3502 // Load subword.
3503 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3504 DAG.getConstant(Offset, PtrTy));
3505 SDValue LoadVal =
3506 DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr,
3507 MachinePointerInfo(), MVT::getIntegerVT(LoadSize * 8),
3508 false, false, Alignment);
3509 MemOpChains.push_back(LoadVal.getValue(1));
3510
3511 // Shift the loaded value.
3512 unsigned Shamt;
3513
3514 if (isLittle)
3515 Shamt = TotalSizeLoaded;
3516 else
3517 Shamt = (RegSize - (TotalSizeLoaded + LoadSize)) * 8;
3518
3519 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
3520 DAG.getConstant(Shamt, MVT::i32));
3521
3522 if (Val.getNode())
3523 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
3524 else
3525 Val = Shift;
3526
3527 Offset += LoadSize;
3528 TotalSizeLoaded += LoadSize;
3529 Alignment = std::min(Alignment, LoadSize);
3530 }
3531
3532 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3533 RegsToPass.push_back(std::make_pair(ArgReg, Val));
3534 return;
3535 }
3536 }
3537
3538 // Copy remainder of byval arg to it with memcpy.
3539 unsigned MemCpySize = ByValSize - Offset;
3540 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3541 DAG.getConstant(Offset, PtrTy));
3542 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
3543 DAG.getIntPtrConstant(ByVal.Address));
3544 Chain = DAG.getMemcpy(Chain, DL, Dst, Src,
3545 DAG.getConstant(MemCpySize, PtrTy), Alignment,
3546 /*isVolatile=*/false, /*AlwaysInline=*/false,
3547 MachinePointerInfo(0), MachinePointerInfo(0));
3548 MemOpChains.push_back(Chain);
3549}
Akira Hatanakaf0848472012-10-27 00:21:13 +00003550
3551void
3552MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
3553 const MipsCC &CC, SDValue Chain,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003554 SDLoc DL, SelectionDAG &DAG) const {
Akira Hatanakaf0848472012-10-27 00:21:13 +00003555 unsigned NumRegs = CC.numIntArgRegs();
3556 const uint16_t *ArgRegs = CC.intArgRegs();
3557 const CCState &CCInfo = CC.getCCInfo();
3558 unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs, NumRegs);
3559 unsigned RegSize = CC.regSize();
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00003560 MVT RegTy = MVT::getIntegerVT(RegSize * 8);
Akira Hatanakaf0848472012-10-27 00:21:13 +00003561 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3562 MachineFunction &MF = DAG.getMachineFunction();
3563 MachineFrameInfo *MFI = MF.getFrameInfo();
3564 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3565
3566 // Offset of the first variable argument from stack pointer.
3567 int VaArgOffset;
3568
3569 if (NumRegs == Idx)
3570 VaArgOffset = RoundUpToAlignment(CCInfo.getNextStackOffset(), RegSize);
3571 else
3572 VaArgOffset =
3573 (int)CC.reservedArgArea() - (int)(RegSize * (NumRegs - Idx));
3574
3575 // Record the frame index of the first variable argument
3576 // which is a value necessary to VASTART.
3577 int FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
3578 MipsFI->setVarArgsFrameIndex(FI);
3579
3580 // Copy the integer registers that have not been used for argument passing
3581 // to the argument register save area. For O32, the save area is allocated
3582 // in the caller's stack frame, while for N32/64, it is allocated in the
3583 // callee's stack frame.
3584 for (unsigned I = Idx; I < NumRegs; ++I, VaArgOffset += RegSize) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003585 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC);
Akira Hatanakaf0848472012-10-27 00:21:13 +00003586 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
3587 FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
3588 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
3589 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
3590 MachinePointerInfo(), false, false, 0);
3591 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(0);
3592 OutChains.push_back(Store);
3593 }
3594}