blob: 443653f5f9853ea86adf8088680880ec14688f3e [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 Sanders915432c2013-09-23 13:22:24 +0000217 case MipsISD::VNOR: return "MipsISD::VNOR";
Akira Hatanaka0f843822011-06-07 18:58:42 +0000218 default: return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000219 }
220}
221
222MipsTargetLowering::
Chris Lattnerf0144122009-07-28 03:13:23 +0000223MipsTargetLowering(MipsTargetMachine &TM)
Akira Hatanaka8b4198d2011-09-26 21:47:02 +0000224 : TargetLowering(TM, new MipsTargetObjectFile()),
225 Subtarget(&TM.getSubtarget<MipsSubtarget>()),
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +0000226 HasMips64(Subtarget->hasMips64()), IsN64(Subtarget->isABI_N64()),
227 IsO32(Subtarget->isABI_O32()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000228 // Mips does not have i1 type, so use i32 for
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000229 // setcc operations results (slt, sgt, ...).
Duncan Sands03228082008-11-23 15:47:28 +0000230 setBooleanContents(ZeroOrOneBooleanContent);
Akira Hatanakacd6c5792013-04-30 22:37:26 +0000231 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000232
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000233 // Load extented operations for i1 types must be promoted
Owen Anderson825b72b2009-08-11 20:47:22 +0000234 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
235 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
236 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000237
Eli Friedman6055a6a2009-07-17 04:07:24 +0000238 // MIPS doesn't have extending float->double load/store
Owen Anderson825b72b2009-08-11 20:47:22 +0000239 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
240 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman10a36592009-07-17 02:28:12 +0000241
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000242 // Used by legalize types to correctly generate the setcc result.
243 // Without this, every float setcc comes with a AND/OR with the result,
244 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000245 // which is used implicitly by brcond and select operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000246 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000247
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000248 // Mips Custom Operations
Akira Hatanakab7656a92013-03-06 21:32:03 +0000249 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000250 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000251 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000252 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
253 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
254 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
255 setOperationAction(ISD::SELECT, MVT::f32, Custom);
256 setOperationAction(ISD::SELECT, MVT::f64, Custom);
257 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Akira Hatanaka3fef29d2012-07-11 19:32:27 +0000258 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
259 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Akira Hatanaka0a40c232012-03-09 23:46:03 +0000260 setOperationAction(ISD::SETCC, MVT::f32, Custom);
261 setOperationAction(ISD::SETCC, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000262 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000263 setOperationAction(ISD::VASTART, MVT::Other, Custom);
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000264 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
265 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +0000266 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000267
Akira Hatanakac12a6e62012-04-11 22:49:04 +0000268 if (!TM.Options.NoNaNsFPMath) {
269 setOperationAction(ISD::FABS, MVT::f32, Custom);
270 setOperationAction(ISD::FABS, MVT::f64, Custom);
271 }
272
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000273 if (HasMips64) {
274 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
275 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
276 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
277 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
278 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
279 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Akira Hatanaka7664f052012-06-02 00:04:42 +0000280 setOperationAction(ISD::LOAD, MVT::i64, Custom);
281 setOperationAction(ISD::STORE, MVT::i64, Custom);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +0000282 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000283 }
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000284
Akira Hatanakaa284acb2012-05-09 00:55:21 +0000285 if (!HasMips64) {
286 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
287 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
288 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
289 }
290
Akira Hatanakae90a3bc2012-11-07 19:10:58 +0000291 setOperationAction(ISD::ADD, MVT::i32, Custom);
292 if (HasMips64)
293 setOperationAction(ISD::ADD, MVT::i64, Custom);
294
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000295 setOperationAction(ISD::SDIV, MVT::i32, Expand);
296 setOperationAction(ISD::SREM, MVT::i32, Expand);
297 setOperationAction(ISD::UDIV, MVT::i32, Expand);
298 setOperationAction(ISD::UREM, MVT::i32, Expand);
Akira Hatanakadda4a072011-10-03 21:06:13 +0000299 setOperationAction(ISD::SDIV, MVT::i64, Expand);
300 setOperationAction(ISD::SREM, MVT::i64, Expand);
301 setOperationAction(ISD::UDIV, MVT::i64, Expand);
302 setOperationAction(ISD::UREM, MVT::i64, Expand);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000303
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000304 // Operations not directly supported by Mips.
Tom Stellard3ef53832013-03-08 15:36:57 +0000305 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
306 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
307 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
308 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000309 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
310 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Akira Hatanakae1bcd6b2011-12-20 23:40:56 +0000311 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000312 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Akira Hatanakae1bcd6b2011-12-20 23:40:56 +0000313 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000314 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
315 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
Akira Hatanaka7f162742011-12-21 00:14:05 +0000316 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000317 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
Akira Hatanaka7f162742011-12-21 00:14:05 +0000318 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000319 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
320 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
321 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
322 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000323 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000324 setOperationAction(ISD::ROTL, MVT::i64, Expand);
Akira Hatanaka1d165f12012-07-31 20:54:48 +0000325 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
326 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000327
Akira Hatanaka56633442011-09-20 23:53:09 +0000328 if (!Subtarget->hasMips32r2())
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000329 setOperationAction(ISD::ROTR, MVT::i32, Expand);
330
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000331 if (!Subtarget->hasMips64r2())
332 setOperationAction(ISD::ROTR, MVT::i64, Expand);
333
Owen Anderson825b72b2009-08-11 20:47:22 +0000334 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000335 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000336 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000337 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng8688a582013-01-29 02:32:37 +0000338 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
339 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000340 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
341 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanaka46da1362011-05-23 22:23:58 +0000342 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000343 setOperationAction(ISD::FLOG, MVT::f32, Expand);
344 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
345 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
346 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000347 setOperationAction(ISD::FMA, MVT::f32, Expand);
348 setOperationAction(ISD::FMA, MVT::f64, Expand);
Akira Hatanaka21ecc2f2012-03-29 18:43:11 +0000349 setOperationAction(ISD::FREM, MVT::f32, Expand);
350 setOperationAction(ISD::FREM, MVT::f64, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000351
Akira Hatanaka1cc63332012-04-11 22:59:08 +0000352 if (!TM.Options.NoNaNsFPMath) {
353 setOperationAction(ISD::FNEG, MVT::f32, Expand);
354 setOperationAction(ISD::FNEG, MVT::f64, Expand);
355 }
356
Akira Hatanaka544cc212013-01-30 00:26:49 +0000357 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
358
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +0000359 setOperationAction(ISD::VAARG, MVT::Other, Expand);
360 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
361 setOperationAction(ISD::VAEND, MVT::Other, Expand);
362
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000363 // Use the default for now
Owen Anderson825b72b2009-08-11 20:47:22 +0000364 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
365 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000366
Jia Liubb481f82012-02-28 07:46:26 +0000367 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
368 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
369 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
370 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
Eli Friedman4db5aca2011-08-29 18:23:02 +0000371
Eli Friedman26689ac2011-08-03 21:06:02 +0000372 setInsertFencesForAtomic(true);
373
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000374 if (!Subtarget->hasSEInReg()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000375 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
376 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000377 }
378
Akira Hatanakac79507a2011-12-21 00:20:27 +0000379 if (!Subtarget->hasBitCount()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000380 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Akira Hatanakac79507a2011-12-21 00:20:27 +0000381 setOperationAction(ISD::CTLZ, MVT::i64, Expand);
382 }
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000383
Akira Hatanakac0ea0432011-12-20 23:56:43 +0000384 if (!Subtarget->hasSwap()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000385 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Akira Hatanakac0ea0432011-12-20 23:56:43 +0000386 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
387 }
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000388
Akira Hatanaka7664f052012-06-02 00:04:42 +0000389 if (HasMips64) {
390 setLoadExtAction(ISD::SEXTLOAD, MVT::i32, Custom);
391 setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, Custom);
392 setLoadExtAction(ISD::EXTLOAD, MVT::i32, Custom);
393 setTruncStoreAction(MVT::i64, MVT::i32, Custom);
394 }
395
Akira Hatanaka97585622013-07-26 20:58:55 +0000396 setOperationAction(ISD::TRAP, MVT::Other, Legal);
397
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000398 setTargetDAGCombine(ISD::SDIVREM);
399 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanakaee8c3b02012-03-08 03:26:37 +0000400 setTargetDAGCombine(ISD::SELECT);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000401 setTargetDAGCombine(ISD::AND);
402 setTargetDAGCombine(ISD::OR);
Akira Hatanaka87827072012-06-13 20:33:18 +0000403 setTargetDAGCombine(ISD::ADD);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000404
Akira Hatanaka5fdf5002012-03-08 01:59:33 +0000405 setMinFunctionAlignment(HasMips64 ? 3 : 2);
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000406
Akira Hatanaka3f5b1072012-02-02 03:17:04 +0000407 setStackPointerRegisterToSaveRestore(IsN64 ? Mips::SP_64 : Mips::SP);
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000408
Akira Hatanaka590baca2012-02-02 03:13:40 +0000409 setExceptionPointerRegister(IsN64 ? Mips::A0_64 : Mips::A0);
410 setExceptionSelectorRegister(IsN64 ? Mips::A1_64 : Mips::A1);
Akira Hatanakae193b322012-06-13 19:33:32 +0000411
Jim Grosbach3450f802013-02-20 21:13:59 +0000412 MaxStoresPerMemcpy = 16;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000413}
414
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000415const MipsTargetLowering *MipsTargetLowering::create(MipsTargetMachine &TM) {
416 if (TM.getSubtargetImpl()->inMips16Mode())
417 return llvm::createMips16TargetLowering(TM);
Jia Liubb481f82012-02-28 07:46:26 +0000418
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000419 return llvm::createMipsSETargetLowering(TM);
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000420}
421
Matt Arsenault225ed702013-05-18 00:21:46 +0000422EVT MipsTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Akira Hatanakae13f4412013-01-04 20:06:01 +0000423 if (!VT.isVector())
424 return MVT::i32;
425 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +0000426}
427
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000428static SDValue performDivRemCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000429 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000430 const MipsSubtarget *Subtarget) {
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000431 if (DCI.isBeforeLegalizeOps())
432 return SDValue();
433
Akira Hatanakadda4a072011-10-03 21:06:13 +0000434 EVT Ty = N->getValueType(0);
Akira Hatanakacbaf6d02013-08-14 00:47:08 +0000435 unsigned LO = (Ty == MVT::i32) ? Mips::LO0 : Mips::LO0_64;
436 unsigned HI = (Ty == MVT::i32) ? Mips::HI0 : Mips::HI0_64;
Akira Hatanakaf5926fd2013-03-30 01:36:35 +0000437 unsigned Opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem16 :
438 MipsISD::DivRemU16;
Andrew Trickac6d9be2013-05-25 02:42:55 +0000439 SDLoc DL(N);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000440
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000441 SDValue DivRem = DAG.getNode(Opc, DL, MVT::Glue,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000442 N->getOperand(0), N->getOperand(1));
443 SDValue InChain = DAG.getEntryNode();
444 SDValue InGlue = DivRem;
445
446 // insert MFLO
447 if (N->hasAnyUseOfValue(0)) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000448 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, DL, LO, Ty,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000449 InGlue);
450 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
451 InChain = CopyFromLo.getValue(1);
452 InGlue = CopyFromLo.getValue(2);
453 }
454
455 // insert MFHI
456 if (N->hasAnyUseOfValue(1)) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000457 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, DL,
Akira Hatanakadda4a072011-10-03 21:06:13 +0000458 HI, Ty, InGlue);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000459 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
460 }
461
462 return SDValue();
463}
464
Akira Hatanaka2fbe90c2013-04-18 01:00:46 +0000465static Mips::CondCode condCodeToFCC(ISD::CondCode CC) {
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000466 switch (CC) {
467 default: llvm_unreachable("Unknown fp condition code!");
468 case ISD::SETEQ:
469 case ISD::SETOEQ: return Mips::FCOND_OEQ;
470 case ISD::SETUNE: return Mips::FCOND_UNE;
471 case ISD::SETLT:
472 case ISD::SETOLT: return Mips::FCOND_OLT;
473 case ISD::SETGT:
474 case ISD::SETOGT: return Mips::FCOND_OGT;
475 case ISD::SETLE:
476 case ISD::SETOLE: return Mips::FCOND_OLE;
477 case ISD::SETGE:
478 case ISD::SETOGE: return Mips::FCOND_OGE;
479 case ISD::SETULT: return Mips::FCOND_ULT;
480 case ISD::SETULE: return Mips::FCOND_ULE;
481 case ISD::SETUGT: return Mips::FCOND_UGT;
482 case ISD::SETUGE: return Mips::FCOND_UGE;
483 case ISD::SETUO: return Mips::FCOND_UN;
484 case ISD::SETO: return Mips::FCOND_OR;
485 case ISD::SETNE:
486 case ISD::SETONE: return Mips::FCOND_ONE;
487 case ISD::SETUEQ: return Mips::FCOND_UEQ;
488 }
489}
490
491
Akira Hatanaka9cf07242013-03-30 01:16:38 +0000492/// This function returns true if the floating point conditional branches and
493/// conditional moves which use condition code CC should be inverted.
494static bool invertFPCondCodeUser(Mips::CondCode CC) {
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000495 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
496 return false;
497
Akira Hatanaka82099682011-12-19 19:52:25 +0000498 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
499 "Illegal Condition Code");
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000500
Akira Hatanaka82099682011-12-19 19:52:25 +0000501 return true;
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000502}
503
504// Creates and returns an FPCmp node from a setcc node.
505// Returns Op if setcc is not a floating point comparison.
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000506static SDValue createFPCmp(SelectionDAG &DAG, const SDValue &Op) {
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000507 // must be a SETCC node
508 if (Op.getOpcode() != ISD::SETCC)
509 return Op;
510
511 SDValue LHS = Op.getOperand(0);
512
513 if (!LHS.getValueType().isFloatingPoint())
514 return Op;
515
516 SDValue RHS = Op.getOperand(1);
Andrew Trickac6d9be2013-05-25 02:42:55 +0000517 SDLoc DL(Op);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000518
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000519 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
520 // node if necessary.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000521 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
522
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000523 return DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS,
Akira Hatanaka2fbe90c2013-04-18 01:00:46 +0000524 DAG.getConstant(condCodeToFCC(CC), MVT::i32));
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000525}
526
527// Creates and returns a CMovFPT/F node.
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000528static SDValue createCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000529 SDValue False, SDLoc DL) {
Akira Hatanaka9cf07242013-03-30 01:16:38 +0000530 ConstantSDNode *CC = cast<ConstantSDNode>(Cond.getOperand(2));
531 bool invert = invertFPCondCodeUser((Mips::CondCode)CC->getSExtValue());
Akira Hatanaka407883b2013-07-26 20:51:20 +0000532 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000533
534 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
Akira Hatanaka407883b2013-07-26 20:51:20 +0000535 True.getValueType(), True, FCC0, False, Cond);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000536}
537
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000538static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000539 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000540 const MipsSubtarget *Subtarget) {
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000541 if (DCI.isBeforeLegalizeOps())
542 return SDValue();
543
544 SDValue SetCC = N->getOperand(0);
545
546 if ((SetCC.getOpcode() != ISD::SETCC) ||
547 !SetCC.getOperand(0).getValueType().isInteger())
548 return SDValue();
549
550 SDValue False = N->getOperand(2);
551 EVT FalseTy = False.getValueType();
552
553 if (!FalseTy.isInteger())
554 return SDValue();
555
556 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(False);
557
558 if (!CN || CN->getZExtValue())
559 return SDValue();
560
Andrew Trickac6d9be2013-05-25 02:42:55 +0000561 const SDLoc DL(N);
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000562 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
563 SDValue True = N->getOperand(1);
Akira Hatanaka864f6602012-06-14 21:10:56 +0000564
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000565 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
566 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
Akira Hatanaka864f6602012-06-14 21:10:56 +0000567
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000568 return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True);
569}
570
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000571static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000572 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000573 const MipsSubtarget *Subtarget) {
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000574 // Pattern match EXT.
575 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
576 // => ext $dst, $src, size, pos
Akira Hatanaka56633442011-09-20 23:53:09 +0000577 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000578 return SDValue();
579
580 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000581 unsigned ShiftRightOpc = ShiftRight.getOpcode();
582
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000583 // Op's first operand must be a shift right.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000584 if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL)
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000585 return SDValue();
586
587 // The second operand of the shift must be an immediate.
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000588 ConstantSDNode *CN;
589 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
590 return SDValue();
Jia Liubb481f82012-02-28 07:46:26 +0000591
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000592 uint64_t Pos = CN->getZExtValue();
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000593 uint64_t SMPos, SMSize;
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000594
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000595 // Op's second operand must be a shifted mask.
596 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000597 !isShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000598 return SDValue();
599
600 // Return if the shifted mask does not start at bit 0 or the sum of its size
601 // and Pos exceeds the word's size.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000602 EVT ValTy = N->getValueType(0);
603 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000604 return SDValue();
605
Andrew Trickac6d9be2013-05-25 02:42:55 +0000606 return DAG.getNode(MipsISD::Ext, SDLoc(N), ValTy,
Akira Hatanaka82099682011-12-19 19:52:25 +0000607 ShiftRight.getOperand(0), DAG.getConstant(Pos, MVT::i32),
Akira Hatanaka667645f2011-08-17 22:59:46 +0000608 DAG.getConstant(SMSize, MVT::i32));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000609}
Jia Liubb481f82012-02-28 07:46:26 +0000610
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000611static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000612 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000613 const MipsSubtarget *Subtarget) {
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000614 // Pattern match INS.
615 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
Jia Liubb481f82012-02-28 07:46:26 +0000616 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000617 // => ins $dst, $src, size, pos, $src1
Akira Hatanaka56633442011-09-20 23:53:09 +0000618 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000619 return SDValue();
620
621 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
622 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
623 ConstantSDNode *CN;
624
625 // See if Op's first operand matches (and $src1 , mask0).
626 if (And0.getOpcode() != ISD::AND)
627 return SDValue();
628
629 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000630 !isShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000631 return SDValue();
632
633 // See if Op's second operand matches (and (shl $src, pos), mask1).
634 if (And1.getOpcode() != ISD::AND)
635 return SDValue();
Jia Liubb481f82012-02-28 07:46:26 +0000636
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000637 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000638 !isShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000639 return SDValue();
640
641 // The shift masks must have the same position and size.
642 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
643 return SDValue();
644
645 SDValue Shl = And1.getOperand(0);
646 if (Shl.getOpcode() != ISD::SHL)
647 return SDValue();
648
649 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
650 return SDValue();
651
652 unsigned Shamt = CN->getZExtValue();
653
654 // Return if the shift amount and the first bit position of mask are not the
Jia Liubb481f82012-02-28 07:46:26 +0000655 // same.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000656 EVT ValTy = N->getValueType(0);
657 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000658 return SDValue();
Jia Liubb481f82012-02-28 07:46:26 +0000659
Andrew Trickac6d9be2013-05-25 02:42:55 +0000660 return DAG.getNode(MipsISD::Ins, SDLoc(N), ValTy, Shl.getOperand(0),
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000661 DAG.getConstant(SMPos0, MVT::i32),
Akira Hatanaka82099682011-12-19 19:52:25 +0000662 DAG.getConstant(SMSize0, MVT::i32), And0.getOperand(0));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000663}
Jia Liubb481f82012-02-28 07:46:26 +0000664
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000665static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka87827072012-06-13 20:33:18 +0000666 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000667 const MipsSubtarget *Subtarget) {
Akira Hatanaka87827072012-06-13 20:33:18 +0000668 // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt))
669
670 if (DCI.isBeforeLegalizeOps())
671 return SDValue();
672
673 SDValue Add = N->getOperand(1);
674
675 if (Add.getOpcode() != ISD::ADD)
676 return SDValue();
677
678 SDValue Lo = Add.getOperand(1);
679
680 if ((Lo.getOpcode() != MipsISD::Lo) ||
681 (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable))
682 return SDValue();
683
684 EVT ValTy = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +0000685 SDLoc DL(N);
Akira Hatanaka87827072012-06-13 20:33:18 +0000686
687 SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, N->getOperand(0),
688 Add.getOperand(0));
689 return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo);
690}
691
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000692SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000693 const {
694 SelectionDAG &DAG = DCI.DAG;
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000695 unsigned Opc = N->getOpcode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000696
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000697 switch (Opc) {
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000698 default: break;
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000699 case ISD::SDIVREM:
700 case ISD::UDIVREM:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000701 return performDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000702 case ISD::SELECT:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000703 return performSELECTCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000704 case ISD::AND:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000705 return performANDCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000706 case ISD::OR:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000707 return performORCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka87827072012-06-13 20:33:18 +0000708 case ISD::ADD:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000709 return performADDCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000710 }
711
712 return SDValue();
713}
714
Akira Hatanakab430cec2012-09-21 23:58:31 +0000715void
716MipsTargetLowering::LowerOperationWrapper(SDNode *N,
717 SmallVectorImpl<SDValue> &Results,
718 SelectionDAG &DAG) const {
719 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
720
721 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
722 Results.push_back(Res.getValue(I));
723}
724
725void
726MipsTargetLowering::ReplaceNodeResults(SDNode *N,
727 SmallVectorImpl<SDValue> &Results,
728 SelectionDAG &DAG) const {
Akira Hatanaka13ec4812013-04-30 21:17:07 +0000729 return LowerOperationWrapper(N, Results, DAG);
Akira Hatanakab430cec2012-09-21 23:58:31 +0000730}
731
Dan Gohman475871a2008-07-27 21:46:04 +0000732SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000733LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000734{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000735 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000736 {
Akira Hatanaka2459afe2013-03-30 01:15:17 +0000737 case ISD::BR_JT: return lowerBR_JT(Op, DAG);
738 case ISD::BRCOND: return lowerBRCOND(Op, DAG);
739 case ISD::ConstantPool: return lowerConstantPool(Op, DAG);
740 case ISD::GlobalAddress: return lowerGlobalAddress(Op, DAG);
741 case ISD::BlockAddress: return lowerBlockAddress(Op, DAG);
742 case ISD::GlobalTLSAddress: return lowerGlobalTLSAddress(Op, DAG);
743 case ISD::JumpTable: return lowerJumpTable(Op, DAG);
744 case ISD::SELECT: return lowerSELECT(Op, DAG);
745 case ISD::SELECT_CC: return lowerSELECT_CC(Op, DAG);
746 case ISD::SETCC: return lowerSETCC(Op, DAG);
747 case ISD::VASTART: return lowerVASTART(Op, DAG);
748 case ISD::FCOPYSIGN: return lowerFCOPYSIGN(Op, DAG);
749 case ISD::FABS: return lowerFABS(Op, DAG);
750 case ISD::FRAMEADDR: return lowerFRAMEADDR(Op, DAG);
751 case ISD::RETURNADDR: return lowerRETURNADDR(Op, DAG);
752 case ISD::EH_RETURN: return lowerEH_RETURN(Op, DAG);
Akira Hatanaka2459afe2013-03-30 01:15:17 +0000753 case ISD::ATOMIC_FENCE: return lowerATOMIC_FENCE(Op, DAG);
754 case ISD::SHL_PARTS: return lowerShiftLeftParts(Op, DAG);
755 case ISD::SRA_PARTS: return lowerShiftRightParts(Op, DAG, true);
756 case ISD::SRL_PARTS: return lowerShiftRightParts(Op, DAG, false);
757 case ISD::LOAD: return lowerLOAD(Op, DAG);
758 case ISD::STORE: return lowerSTORE(Op, DAG);
Akira Hatanaka2459afe2013-03-30 01:15:17 +0000759 case ISD::ADD: return lowerADD(Op, DAG);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +0000760 case ISD::FP_TO_SINT: return lowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000761 }
Dan Gohman475871a2008-07-27 21:46:04 +0000762 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000763}
764
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000765//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000766// Lower helper functions
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000767//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000768
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000769// addLiveIn - This helper function adds the specified physical register to the
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000770// MachineFunction as a live in value. It also creates a corresponding
771// virtual register for it.
772static unsigned
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000773addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000774{
Chris Lattner84bc5422007-12-31 04:13:23 +0000775 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
776 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000777 return VReg;
778}
779
Akira Hatanakaf8941992013-05-20 18:07:43 +0000780static MachineBasicBlock *expandPseudoDIV(MachineInstr *MI,
781 MachineBasicBlock &MBB,
782 const TargetInstrInfo &TII,
783 bool Is64Bit) {
784 if (NoZeroDivCheck)
785 return &MBB;
786
787 // Insert instruction "teq $divisor_reg, $zero, 7".
788 MachineBasicBlock::iterator I(MI);
789 MachineInstrBuilder MIB;
790 MIB = BuildMI(MBB, llvm::next(I), MI->getDebugLoc(), TII.get(Mips::TEQ))
791 .addOperand(MI->getOperand(2)).addReg(Mips::ZERO).addImm(7);
792
793 // Use the 32-bit sub-register if this is a 64-bit division.
794 if (Is64Bit)
795 MIB->getOperand(0).setSubReg(Mips::sub_32);
796
797 return &MBB;
798}
799
Akira Hatanaka01f70892012-09-27 02:15:57 +0000800MachineBasicBlock *
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000801MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000802 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000803 switch (MI->getOpcode()) {
Reed Kotlerffbe4322013-02-21 04:22:38 +0000804 default:
805 llvm_unreachable("Unexpected instr type to insert");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000806 case Mips::ATOMIC_LOAD_ADD_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000807 return emitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000808 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000809 return emitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000810 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000811 return emitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000812 case Mips::ATOMIC_LOAD_ADD_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000813 return emitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000814
815 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000816 return emitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000817 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000818 return emitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000819 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000820 return emitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka59068062011-11-11 04:14:30 +0000821 case Mips::ATOMIC_LOAD_AND_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000822 return emitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000823
824 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000825 return emitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000826 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000827 return emitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000828 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000829 return emitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000830 case Mips::ATOMIC_LOAD_OR_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000831 return emitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000832
833 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000834 return emitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000835 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000836 return emitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000837 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000838 return emitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000839 case Mips::ATOMIC_LOAD_XOR_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000840 return emitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000841
842 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000843 return emitAtomicBinaryPartword(MI, BB, 1, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000844 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000845 return emitAtomicBinaryPartword(MI, BB, 2, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000846 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000847 return emitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka59068062011-11-11 04:14:30 +0000848 case Mips::ATOMIC_LOAD_NAND_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000849 return emitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000850
851 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000852 return emitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000853 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000854 return emitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000855 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000856 return emitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000857 case Mips::ATOMIC_LOAD_SUB_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000858 return emitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000859
860 case Mips::ATOMIC_SWAP_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000861 return emitAtomicBinaryPartword(MI, BB, 1, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000862 case Mips::ATOMIC_SWAP_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000863 return emitAtomicBinaryPartword(MI, BB, 2, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000864 case Mips::ATOMIC_SWAP_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000865 return emitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka59068062011-11-11 04:14:30 +0000866 case Mips::ATOMIC_SWAP_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000867 return emitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000868
869 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000870 return emitAtomicCmpSwapPartword(MI, BB, 1);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000871 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000872 return emitAtomicCmpSwapPartword(MI, BB, 2);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000873 case Mips::ATOMIC_CMP_SWAP_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000874 return emitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka59068062011-11-11 04:14:30 +0000875 case Mips::ATOMIC_CMP_SWAP_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000876 return emitAtomicCmpSwap(MI, BB, 8);
Akira Hatanakaf8941992013-05-20 18:07:43 +0000877 case Mips::PseudoSDIV:
878 case Mips::PseudoUDIV:
879 return expandPseudoDIV(MI, *BB, *getTargetMachine().getInstrInfo(), false);
880 case Mips::PseudoDSDIV:
881 case Mips::PseudoDUDIV:
882 return expandPseudoDIV(MI, *BB, *getTargetMachine().getInstrInfo(), true);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000883 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000884}
885
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000886// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
887// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
888MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000889MipsTargetLowering::emitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher471e4222011-06-08 23:55:35 +0000890 unsigned Size, unsigned BinOpcode,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000891 bool Nand) const {
Akira Hatanaka59068062011-11-11 04:14:30 +0000892 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000893
894 MachineFunction *MF = BB->getParent();
895 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +0000896 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000897 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000898 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +0000899 unsigned LL, SC, AND, NOR, ZERO, BEQ;
900
901 if (Size == 4) {
Akira Hatanakaa98a4862013-08-20 21:08:22 +0000902 LL = Mips::LL;
903 SC = Mips::SC;
Akira Hatanaka59068062011-11-11 04:14:30 +0000904 AND = Mips::AND;
905 NOR = Mips::NOR;
906 ZERO = Mips::ZERO;
907 BEQ = Mips::BEQ;
908 }
909 else {
Akira Hatanakaa98a4862013-08-20 21:08:22 +0000910 LL = Mips::LLD;
911 SC = Mips::SCD;
Akira Hatanaka59068062011-11-11 04:14:30 +0000912 AND = Mips::AND64;
913 NOR = Mips::NOR64;
914 ZERO = Mips::ZERO_64;
915 BEQ = Mips::BEQ64;
916 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000917
Akira Hatanaka4061da12011-07-19 20:11:17 +0000918 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000919 unsigned Ptr = MI->getOperand(1).getReg();
920 unsigned Incr = MI->getOperand(2).getReg();
921
Akira Hatanaka4061da12011-07-19 20:11:17 +0000922 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
923 unsigned AndRes = RegInfo.createVirtualRegister(RC);
924 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000925
926 // insert new blocks after the current block
927 const BasicBlock *LLVM_BB = BB->getBasicBlock();
928 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
929 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
930 MachineFunction::iterator It = BB;
931 ++It;
932 MF->insert(It, loopMBB);
933 MF->insert(It, exitMBB);
934
935 // Transfer the remainder of BB and its successor edges to exitMBB.
936 exitMBB->splice(exitMBB->begin(), BB,
937 llvm::next(MachineBasicBlock::iterator(MI)),
938 BB->end());
939 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
940
941 // thisMBB:
942 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000943 // fallthrough --> loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000944 BB->addSuccessor(loopMBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +0000945 loopMBB->addSuccessor(loopMBB);
946 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000947
948 // loopMBB:
949 // ll oldval, 0(ptr)
Akira Hatanaka4061da12011-07-19 20:11:17 +0000950 // <binop> storeval, oldval, incr
951 // sc success, storeval, 0(ptr)
952 // beq success, $0, loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000953 BB = loopMBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000954 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000955 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000956 // and andres, oldval, incr
957 // nor storeval, $0, andres
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000958 BuildMI(BB, DL, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
959 BuildMI(BB, DL, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000960 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000961 // <binop> storeval, oldval, incr
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000962 BuildMI(BB, DL, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000963 } else {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000964 StoreVal = Incr;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000965 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000966 BuildMI(BB, DL, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
967 BuildMI(BB, DL, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000968
969 MI->eraseFromParent(); // The instruction is gone now.
970
Akira Hatanaka939ece12011-07-19 03:42:13 +0000971 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000972}
973
974MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000975MipsTargetLowering::emitAtomicBinaryPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000976 MachineBasicBlock *BB,
977 unsigned Size, unsigned BinOpcode,
978 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000979 assert((Size == 1 || Size == 2) &&
980 "Unsupported size for EmitAtomicBinaryPartial.");
981
982 MachineFunction *MF = BB->getParent();
983 MachineRegisterInfo &RegInfo = MF->getRegInfo();
984 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
985 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000986 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000987
988 unsigned Dest = MI->getOperand(0).getReg();
989 unsigned Ptr = MI->getOperand(1).getReg();
990 unsigned Incr = MI->getOperand(2).getReg();
991
Akira Hatanaka4061da12011-07-19 20:11:17 +0000992 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
993 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000994 unsigned Mask = RegInfo.createVirtualRegister(RC);
995 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000996 unsigned NewVal = RegInfo.createVirtualRegister(RC);
997 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000998 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000999 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1000 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1001 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1002 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1003 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001004 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001005 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1006 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1007 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1008 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1009 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001010
1011 // insert new blocks after the current block
1012 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1013 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001014 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001015 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1016 MachineFunction::iterator It = BB;
1017 ++It;
1018 MF->insert(It, loopMBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001019 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001020 MF->insert(It, exitMBB);
1021
1022 // Transfer the remainder of BB and its successor edges to exitMBB.
1023 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001024 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001025 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1026
Akira Hatanaka81b44112011-07-19 17:09:53 +00001027 BB->addSuccessor(loopMBB);
1028 loopMBB->addSuccessor(loopMBB);
1029 loopMBB->addSuccessor(sinkMBB);
1030 sinkMBB->addSuccessor(exitMBB);
1031
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001032 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001033 // addiu masklsb2,$0,-4 # 0xfffffffc
1034 // and alignedaddr,ptr,masklsb2
1035 // andi ptrlsb2,ptr,3
1036 // sll shiftamt,ptrlsb2,3
1037 // ori maskupper,$0,255 # 0xff
1038 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001039 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001040 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001041
1042 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001043 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001044 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001045 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001046 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001047 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Akira Hatanakaaffed7e2013-05-31 03:25:44 +00001048 if (Subtarget->isLittle()) {
1049 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1050 } else {
1051 unsigned Off = RegInfo.createVirtualRegister(RC);
1052 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1053 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1054 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1055 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001056 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001057 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001058 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka51122432013-07-01 20:39:53 +00001059 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001060 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka51122432013-07-01 20:39:53 +00001061 BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(Incr).addReg(ShiftAmt);
Bruno Cardoso Lopescada2d02011-05-31 20:25:26 +00001062
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001063 // atomic.load.binop
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001064 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001065 // ll oldval,0(alignedaddr)
1066 // binop binopres,oldval,incr2
1067 // and newval,binopres,mask
1068 // and maskedoldval0,oldval,mask2
1069 // or storeval,maskedoldval0,newval
1070 // sc success,storeval,0(alignedaddr)
1071 // beq success,$0,loopMBB
1072
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001073 // atomic.swap
1074 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001075 // ll oldval,0(alignedaddr)
Akira Hatanaka70564a92011-07-19 18:14:26 +00001076 // and newval,incr2,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001077 // and maskedoldval0,oldval,mask2
1078 // or storeval,maskedoldval0,newval
1079 // sc success,storeval,0(alignedaddr)
1080 // beq success,$0,loopMBB
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001081
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001082 BB = loopMBB;
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001083 BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001084 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001085 // and andres, oldval, incr2
1086 // nor binopres, $0, andres
1087 // and newval, binopres, mask
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001088 BuildMI(BB, DL, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1089 BuildMI(BB, DL, TII->get(Mips::NOR), BinOpRes)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001090 .addReg(Mips::ZERO).addReg(AndRes);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001091 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001092 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001093 // <binop> binopres, oldval, incr2
1094 // and newval, binopres, mask
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001095 BuildMI(BB, DL, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1096 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001097 } else {// atomic.swap
Akira Hatanaka4061da12011-07-19 20:11:17 +00001098 // and newval, incr2, mask
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001099 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001100 }
Jia Liubb481f82012-02-28 07:46:26 +00001101
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001102 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001103 .addReg(OldVal).addReg(Mask2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001104 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001105 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001106 BuildMI(BB, DL, TII->get(Mips::SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001107 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001108 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001109 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001110
Akira Hatanaka939ece12011-07-19 03:42:13 +00001111 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001112 // and maskedoldval1,oldval,mask
1113 // srl srlres,maskedoldval1,shiftamt
1114 // sll sllres,srlres,24
1115 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001116 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001117 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001118
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001119 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001120 .addReg(OldVal).addReg(Mask);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001121 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka51122432013-07-01 20:39:53 +00001122 .addReg(MaskedOldVal1).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001123 BuildMI(BB, DL, TII->get(Mips::SLL), SllRes)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001124 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001125 BuildMI(BB, DL, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001126 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001127
1128 MI->eraseFromParent(); // The instruction is gone now.
1129
Akira Hatanaka939ece12011-07-19 03:42:13 +00001130 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001131}
1132
1133MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001134MipsTargetLowering::emitAtomicCmpSwap(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001135 MachineBasicBlock *BB,
1136 unsigned Size) const {
Akira Hatanaka59068062011-11-11 04:14:30 +00001137 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001138
1139 MachineFunction *MF = BB->getParent();
1140 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +00001141 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001142 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001143 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001144 unsigned LL, SC, ZERO, BNE, BEQ;
1145
1146 if (Size == 4) {
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001147 LL = Mips::LL;
1148 SC = Mips::SC;
Akira Hatanaka59068062011-11-11 04:14:30 +00001149 ZERO = Mips::ZERO;
1150 BNE = Mips::BNE;
1151 BEQ = Mips::BEQ;
1152 }
1153 else {
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001154 LL = Mips::LLD;
1155 SC = Mips::SCD;
Akira Hatanaka59068062011-11-11 04:14:30 +00001156 ZERO = Mips::ZERO_64;
1157 BNE = Mips::BNE64;
1158 BEQ = Mips::BEQ64;
1159 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001160
1161 unsigned Dest = MI->getOperand(0).getReg();
1162 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001163 unsigned OldVal = MI->getOperand(2).getReg();
1164 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001165
Akira Hatanaka4061da12011-07-19 20:11:17 +00001166 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001167
1168 // insert new blocks after the current block
1169 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1170 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1171 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1172 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1173 MachineFunction::iterator It = BB;
1174 ++It;
1175 MF->insert(It, loop1MBB);
1176 MF->insert(It, loop2MBB);
1177 MF->insert(It, exitMBB);
1178
1179 // Transfer the remainder of BB and its successor edges to exitMBB.
1180 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001181 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001182 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1183
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001184 // thisMBB:
1185 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001186 // fallthrough --> loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001187 BB->addSuccessor(loop1MBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +00001188 loop1MBB->addSuccessor(exitMBB);
1189 loop1MBB->addSuccessor(loop2MBB);
1190 loop2MBB->addSuccessor(loop1MBB);
1191 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001192
1193 // loop1MBB:
1194 // ll dest, 0(ptr)
1195 // bne dest, oldval, exitMBB
1196 BB = loop1MBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001197 BuildMI(BB, DL, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1198 BuildMI(BB, DL, TII->get(BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001199 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001200
1201 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001202 // sc success, newval, 0(ptr)
1203 // beq success, $0, loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001204 BB = loop2MBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001205 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001206 .addReg(NewVal).addReg(Ptr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001207 BuildMI(BB, DL, TII->get(BEQ))
Akira Hatanaka59068062011-11-11 04:14:30 +00001208 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001209
1210 MI->eraseFromParent(); // The instruction is gone now.
1211
Akira Hatanaka939ece12011-07-19 03:42:13 +00001212 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001213}
1214
1215MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001216MipsTargetLowering::emitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001217 MachineBasicBlock *BB,
1218 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001219 assert((Size == 1 || Size == 2) &&
1220 "Unsupported size for EmitAtomicCmpSwapPartial.");
1221
1222 MachineFunction *MF = BB->getParent();
1223 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1224 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1225 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001226 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001227
1228 unsigned Dest = MI->getOperand(0).getReg();
1229 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001230 unsigned CmpVal = MI->getOperand(2).getReg();
1231 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001232
Akira Hatanaka4061da12011-07-19 20:11:17 +00001233 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1234 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001235 unsigned Mask = RegInfo.createVirtualRegister(RC);
1236 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001237 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1238 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1239 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1240 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1241 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1242 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1243 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1244 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1245 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1246 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1247 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1248 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1249 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1250 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001251
1252 // insert new blocks after the current block
1253 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1254 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1255 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001256 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001257 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1258 MachineFunction::iterator It = BB;
1259 ++It;
1260 MF->insert(It, loop1MBB);
1261 MF->insert(It, loop2MBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001262 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001263 MF->insert(It, exitMBB);
1264
1265 // Transfer the remainder of BB and its successor edges to exitMBB.
1266 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001267 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001268 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1269
Akira Hatanaka81b44112011-07-19 17:09:53 +00001270 BB->addSuccessor(loop1MBB);
1271 loop1MBB->addSuccessor(sinkMBB);
1272 loop1MBB->addSuccessor(loop2MBB);
1273 loop2MBB->addSuccessor(loop1MBB);
1274 loop2MBB->addSuccessor(sinkMBB);
1275 sinkMBB->addSuccessor(exitMBB);
1276
Akira Hatanaka70564a92011-07-19 18:14:26 +00001277 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001278 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001279 // addiu masklsb2,$0,-4 # 0xfffffffc
1280 // and alignedaddr,ptr,masklsb2
1281 // andi ptrlsb2,ptr,3
1282 // sll shiftamt,ptrlsb2,3
1283 // ori maskupper,$0,255 # 0xff
1284 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001285 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001286 // andi maskedcmpval,cmpval,255
1287 // sll shiftedcmpval,maskedcmpval,shiftamt
1288 // andi maskednewval,newval,255
1289 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001290 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001291 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001292 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001293 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001294 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001295 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Akira Hatanakaaffed7e2013-05-31 03:25:44 +00001296 if (Subtarget->isLittle()) {
1297 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1298 } else {
1299 unsigned Off = RegInfo.createVirtualRegister(RC);
1300 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1301 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1302 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1303 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001304 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001305 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001306 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka51122432013-07-01 20:39:53 +00001307 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001308 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1309 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001310 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001311 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal)
Akira Hatanaka51122432013-07-01 20:39:53 +00001312 .addReg(MaskedCmpVal).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001313 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001314 .addReg(NewVal).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001315 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal)
Akira Hatanaka51122432013-07-01 20:39:53 +00001316 .addReg(MaskedNewVal).addReg(ShiftAmt);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001317
1318 // loop1MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001319 // ll oldval,0(alginedaddr)
1320 // and maskedoldval0,oldval,mask
1321 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001322 BB = loop1MBB;
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001323 BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001324 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001325 .addReg(OldVal).addReg(Mask);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001326 BuildMI(BB, DL, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001327 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001328
1329 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001330 // and maskedoldval1,oldval,mask2
1331 // or storeval,maskedoldval1,shiftednewval
1332 // sc success,storeval,0(alignedaddr)
1333 // beq success,$0,loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001334 BB = loop2MBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001335 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001336 .addReg(OldVal).addReg(Mask2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001337 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001338 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001339 BuildMI(BB, DL, TII->get(Mips::SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001340 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001341 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001342 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001343
Akira Hatanaka939ece12011-07-19 03:42:13 +00001344 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001345 // srl srlres,maskedoldval0,shiftamt
1346 // sll sllres,srlres,24
1347 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001348 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001349 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001350
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001351 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka51122432013-07-01 20:39:53 +00001352 .addReg(MaskedOldVal0).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001353 BuildMI(BB, DL, TII->get(Mips::SLL), SllRes)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001354 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001355 BuildMI(BB, DL, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001356 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001357
1358 MI->eraseFromParent(); // The instruction is gone now.
1359
Akira Hatanaka939ece12011-07-19 03:42:13 +00001360 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001361}
1362
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001363//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001364// Misc Lower Operation implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001365//===----------------------------------------------------------------------===//
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001366SDValue MipsTargetLowering::lowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanakab7656a92013-03-06 21:32:03 +00001367 SDValue Chain = Op.getOperand(0);
1368 SDValue Table = Op.getOperand(1);
1369 SDValue Index = Op.getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001370 SDLoc DL(Op);
Akira Hatanakab7656a92013-03-06 21:32:03 +00001371 EVT PTy = getPointerTy();
1372 unsigned EntrySize =
1373 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(*getDataLayout());
1374
1375 Index = DAG.getNode(ISD::MUL, DL, PTy, Index,
1376 DAG.getConstant(EntrySize, PTy));
1377 SDValue Addr = DAG.getNode(ISD::ADD, DL, PTy, Index, Table);
1378
1379 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
1380 Addr = DAG.getExtLoad(ISD::SEXTLOAD, DL, PTy, Chain, Addr,
1381 MachinePointerInfo::getJumpTable(), MemVT, false, false,
1382 0);
1383 Chain = Addr.getValue(1);
1384
1385 if ((getTargetMachine().getRelocationModel() == Reloc::PIC_) || IsN64) {
1386 // For PIC, the sequence is:
1387 // BRIND(load(Jumptable + index) + RelocBase)
1388 // RelocBase can be JumpTable, GOT or some sort of global base.
1389 Addr = DAG.getNode(ISD::ADD, DL, PTy, Addr,
1390 getPICJumpTableRelocBase(Table, DAG));
1391 }
1392
1393 return DAG.getNode(ISD::BRIND, DL, MVT::Other, Chain, Addr);
1394}
1395
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +00001396SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001397lowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001398{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001399 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001400 // the block to branch to if the condition is true.
1401 SDValue Chain = Op.getOperand(0);
1402 SDValue Dest = Op.getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001403 SDLoc DL(Op);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001404
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001405 SDValue CondRes = createFPCmp(DAG, Op.getOperand(1));
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001406
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001407 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001408 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +00001409 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001410
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +00001411 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001412 Mips::CondCode CC =
1413 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Akira Hatanaka9cf07242013-03-30 01:16:38 +00001414 unsigned Opc = invertFPCondCodeUser(CC) ? Mips::BRANCH_F : Mips::BRANCH_T;
1415 SDValue BrCode = DAG.getConstant(Opc, MVT::i32);
Akira Hatanaka83d8ef12013-07-26 20:13:47 +00001416 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001417 return DAG.getNode(MipsISD::FPBrcond, DL, Op.getValueType(), Chain, BrCode,
Akira Hatanaka83d8ef12013-07-26 20:13:47 +00001418 FCC0, Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001419}
1420
1421SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001422lowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001423{
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001424 SDValue Cond = createFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001425
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001426 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001427 if (Cond.getOpcode() != MipsISD::FPCmp)
1428 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +00001429
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001430 return createCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001431 SDLoc(Op));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001432}
1433
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001434SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001435lowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001436{
Andrew Trickac6d9be2013-05-25 02:42:55 +00001437 SDLoc DL(Op);
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001438 EVT Ty = Op.getOperand(0).getValueType();
Matt Arsenault225ed702013-05-18 00:21:46 +00001439 SDValue Cond = DAG.getNode(ISD::SETCC, DL,
1440 getSetCCResultType(*DAG.getContext(), Ty),
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001441 Op.getOperand(0), Op.getOperand(1),
1442 Op.getOperand(4));
1443
1444 return DAG.getNode(ISD::SELECT, DL, Op.getValueType(), Cond, Op.getOperand(2),
1445 Op.getOperand(3));
1446}
1447
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001448SDValue MipsTargetLowering::lowerSETCC(SDValue Op, SelectionDAG &DAG) const {
1449 SDValue Cond = createFPCmp(DAG, Op);
Akira Hatanaka0a40c232012-03-09 23:46:03 +00001450
1451 assert(Cond.getOpcode() == MipsISD::FPCmp &&
1452 "Floating point operand expected.");
1453
1454 SDValue True = DAG.getConstant(1, MVT::i32);
1455 SDValue False = DAG.getConstant(0, MVT::i32);
1456
Andrew Trickac6d9be2013-05-25 02:42:55 +00001457 return createCMovFP(DAG, Cond, True, False, SDLoc(Op));
Akira Hatanaka0a40c232012-03-09 23:46:03 +00001458}
1459
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001460SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001461 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +00001462 // FIXME there isn't actually debug info here
Andrew Trickac6d9be2013-05-25 02:42:55 +00001463 SDLoc DL(Op);
Jia Liubb481f82012-02-28 07:46:26 +00001464 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001465
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001466 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
Akira Hatanakaafc945b2012-09-12 23:27:55 +00001467 const MipsTargetObjectFile &TLOF =
1468 (const MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001469
Chris Lattnere3736f82009-08-13 05:41:27 +00001470 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001471 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001472 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001473 MipsII::MO_GPREL);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001474 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, DL,
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001475 DAG.getVTList(MVT::i32), &GA, 1);
Akira Hatanakae7338cd2012-08-22 03:18:13 +00001476 SDValue GPReg = DAG.getRegister(Mips::GP, MVT::i32);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001477 return DAG.getNode(ISD::ADD, DL, MVT::i32, GPReg, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +00001478 }
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001479
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001480 // %hi/%lo relocation
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001481 return getAddrNonPIC(Op, DAG);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001482 }
1483
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001484 if (GV->hasInternalLinkage() || (GV->hasLocalLinkage() && !isa<Function>(GV)))
1485 return getAddrLocal(Op, DAG, HasMips64);
1486
Akira Hatanakaf09a0372012-11-21 20:40:38 +00001487 if (LargeGOT)
1488 return getAddrGlobalLargeGOT(Op, DAG, MipsII::MO_GOT_HI16,
1489 MipsII::MO_GOT_LO16);
1490
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001491 return getAddrGlobal(Op, DAG,
1492 HasMips64 ? MipsII::MO_GOT_DISP : MipsII::MO_GOT16);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001493}
1494
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001495SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op,
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001496 SelectionDAG &DAG) const {
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001497 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
1498 return getAddrNonPIC(Op, DAG);
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001499
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001500 return getAddrLocal(Op, DAG, HasMips64);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001501}
1502
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001503SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001504lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001505{
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001506 // If the relocation model is PIC, use the General Dynamic TLS Model or
1507 // Local Dynamic TLS model, otherwise use the Initial Exec or
1508 // Local Exec TLS Model.
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001509
1510 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001511 SDLoc DL(GA);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001512 const GlobalValue *GV = GA->getGlobal();
1513 EVT PtrVT = getPointerTy();
1514
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001515 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
1516
1517 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Hans Wennborg70a07c72012-06-04 14:02:08 +00001518 // General Dynamic and Local Dynamic TLS Model.
1519 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
1520 : MipsII::MO_TLSGD;
1521
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001522 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag);
1523 SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT,
1524 getGlobalReg(DAG, PtrVT), TGA);
Akira Hatanaka7a7194b2011-12-08 21:05:38 +00001525 unsigned PtrSize = PtrVT.getSizeInBits();
1526 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1527
Benjamin Kramer5eccf672011-12-11 12:21:34 +00001528 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001529
1530 ArgListTy Args;
1531 ArgListEntry Entry;
1532 Entry.Node = Argument;
Akira Hatanakaca074792011-12-08 20:34:32 +00001533 Entry.Ty = PtrTy;
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001534 Args.push_back(Entry);
Jia Liubb481f82012-02-28 07:46:26 +00001535
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001536 TargetLowering::CallLoweringInfo CLI(DAG.getEntryNode(), PtrTy,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001537 false, false, false, false, 0, CallingConv::C,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001538 /*IsTailCall=*/false, /*doesNotRet=*/false,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001539 /*isReturnValueUsed=*/true,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001540 TlsGetAddr, Args, DAG, DL);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001541 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001542
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001543 SDValue Ret = CallResult.first;
1544
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001545 if (model != TLSModel::LocalDynamic)
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001546 return Ret;
1547
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001548 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001549 MipsII::MO_DTPREL_HI);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001550 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1551 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001552 MipsII::MO_DTPREL_LO);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001553 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1554 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret);
1555 return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001556 }
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001557
1558 SDValue Offset;
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001559 if (model == TLSModel::InitialExec) {
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001560 // Initial Exec TLS Model
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001561 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001562 MipsII::MO_GOTTPREL);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001563 TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT),
Akira Hatanaka648f00c2012-02-24 22:34:47 +00001564 TGA);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001565 Offset = DAG.getLoad(PtrVT, DL,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001566 DAG.getEntryNode(), TGA, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001567 false, false, false, 0);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001568 } else {
1569 // Local Exec TLS Model
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001570 assert(model == TLSModel::LocalExec);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001571 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001572 MipsII::MO_TPREL_HI);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001573 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001574 MipsII::MO_TPREL_LO);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001575 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1576 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1577 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001578 }
1579
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001580 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
1581 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001582}
1583
1584SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001585lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001586{
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001587 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
1588 return getAddrNonPIC(Op, DAG);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001589
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001590 return getAddrLocal(Op, DAG, HasMips64);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001591}
1592
Dan Gohman475871a2008-07-27 21:46:04 +00001593SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001594lowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001595{
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001596 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001597 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001598 // but the asm printer currently doesn't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001599 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +00001600 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +00001601 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001602 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1603 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001604 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001605
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001606 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
1607 return getAddrNonPIC(Op, DAG);
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001608
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001609 return getAddrLocal(Op, DAG, HasMips64);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001610}
1611
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001612SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001613 MachineFunction &MF = DAG.getMachineFunction();
1614 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1615
Andrew Trickac6d9be2013-05-25 02:42:55 +00001616 SDLoc DL(Op);
Dan Gohman1e93df62010-04-17 14:41:14 +00001617 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1618 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001619
1620 // vastart just stores the address of the VarArgsFrameIndex slot into the
1621 // memory location argument.
1622 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001623 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
Akira Hatanaka82099682011-12-19 19:52:25 +00001624 MachinePointerInfo(SV), false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001625}
Jia Liubb481f82012-02-28 07:46:26 +00001626
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001627static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001628 EVT TyX = Op.getOperand(0).getValueType();
1629 EVT TyY = Op.getOperand(1).getValueType();
1630 SDValue Const1 = DAG.getConstant(1, MVT::i32);
1631 SDValue Const31 = DAG.getConstant(31, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001632 SDLoc DL(Op);
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001633 SDValue Res;
1634
1635 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1636 // to i32.
1637 SDValue X = (TyX == MVT::f32) ?
1638 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1639 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1640 Const1);
1641 SDValue Y = (TyY == MVT::f32) ?
1642 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
1643 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
1644 Const1);
1645
1646 if (HasR2) {
1647 // ext E, Y, 31, 1 ; extract bit31 of Y
1648 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
1649 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
1650 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
1651 } else {
1652 // sll SllX, X, 1
1653 // srl SrlX, SllX, 1
1654 // srl SrlY, Y, 31
1655 // sll SllY, SrlX, 31
1656 // or Or, SrlX, SllY
1657 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1658 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1659 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
1660 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
1661 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
1662 }
1663
1664 if (TyX == MVT::f32)
1665 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
1666
1667 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1668 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1669 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001670}
1671
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001672static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001673 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
1674 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
1675 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
1676 SDValue Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001677 SDLoc DL(Op);
Eric Christopher471e4222011-06-08 23:55:35 +00001678
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001679 // Bitcast to integer nodes.
1680 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
1681 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001682
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001683 if (HasR2) {
1684 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
1685 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
1686 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
1687 DAG.getConstant(WidthY - 1, MVT::i32), Const1);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001688
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001689 if (WidthX > WidthY)
1690 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
1691 else if (WidthY > WidthX)
1692 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001693
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001694 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
1695 DAG.getConstant(WidthX - 1, MVT::i32), Const1, X);
1696 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
1697 }
1698
1699 // (d)sll SllX, X, 1
1700 // (d)srl SrlX, SllX, 1
1701 // (d)srl SrlY, Y, width(Y)-1
1702 // (d)sll SllY, SrlX, width(Y)-1
1703 // or Or, SrlX, SllY
1704 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
1705 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
1706 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
1707 DAG.getConstant(WidthY - 1, MVT::i32));
1708
1709 if (WidthX > WidthY)
1710 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
1711 else if (WidthY > WidthX)
1712 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
1713
1714 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
1715 DAG.getConstant(WidthX - 1, MVT::i32));
1716 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
1717 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001718}
1719
Akira Hatanaka82099682011-12-19 19:52:25 +00001720SDValue
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001721MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001722 if (Subtarget->hasMips64())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001723 return lowerFCOPYSIGN64(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001724
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001725 return lowerFCOPYSIGN32(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001726}
1727
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001728static SDValue lowerFABS32(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001729 SDValue Res, Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001730 SDLoc DL(Op);
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001731
1732 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1733 // to i32.
1734 SDValue X = (Op.getValueType() == MVT::f32) ?
1735 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1736 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1737 Const1);
1738
1739 // Clear MSB.
1740 if (HasR2)
1741 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32,
1742 DAG.getRegister(Mips::ZERO, MVT::i32),
1743 DAG.getConstant(31, MVT::i32), Const1, X);
1744 else {
1745 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1746 Res = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1747 }
1748
1749 if (Op.getValueType() == MVT::f32)
1750 return DAG.getNode(ISD::BITCAST, DL, MVT::f32, Res);
1751
1752 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1753 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1754 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
1755}
1756
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001757static SDValue lowerFABS64(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001758 SDValue Res, Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001759 SDLoc DL(Op);
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001760
1761 // Bitcast to integer node.
1762 SDValue X = DAG.getNode(ISD::BITCAST, DL, MVT::i64, Op.getOperand(0));
1763
1764 // Clear MSB.
1765 if (HasR2)
1766 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i64,
1767 DAG.getRegister(Mips::ZERO_64, MVT::i64),
1768 DAG.getConstant(63, MVT::i32), Const1, X);
1769 else {
1770 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i64, X, Const1);
1771 Res = DAG.getNode(ISD::SRL, DL, MVT::i64, SllX, Const1);
1772 }
1773
1774 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, Res);
1775}
1776
1777SDValue
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001778MipsTargetLowering::lowerFABS(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001779 if (Subtarget->hasMips64() && (Op.getValueType() == MVT::f64))
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001780 return lowerFABS64(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001781
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001782 return lowerFABS32(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001783}
1784
Akira Hatanaka2e591472011-06-02 00:24:44 +00001785SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001786lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopese0b5cfc2011-06-16 00:40:02 +00001787 // check the depth
1788 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka0f843822011-06-07 18:58:42 +00001789 "Frame address can only be determined for current frame.");
Akira Hatanaka2e591472011-06-02 00:24:44 +00001790
1791 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1792 MFI->setFrameAddressIsTaken(true);
1793 EVT VT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001794 SDLoc DL(Op);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001795 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), DL,
Akira Hatanaka46ac4392011-11-11 04:11:56 +00001796 IsN64 ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka2e591472011-06-02 00:24:44 +00001797 return FrameAddr;
1798}
1799
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001800SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op,
Akira Hatanakaba584fe2012-07-11 00:53:32 +00001801 SelectionDAG &DAG) const {
1802 // check the depth
1803 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
1804 "Return address can be determined only for current frame.");
1805
1806 MachineFunction &MF = DAG.getMachineFunction();
1807 MachineFrameInfo *MFI = MF.getFrameInfo();
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00001808 MVT VT = Op.getSimpleValueType();
Akira Hatanakaba584fe2012-07-11 00:53:32 +00001809 unsigned RA = IsN64 ? Mips::RA_64 : Mips::RA;
1810 MFI->setReturnAddressIsTaken(true);
1811
1812 // Return RA, which contains the return address. Mark it an implicit live-in.
1813 unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00001814 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT);
Akira Hatanakaba584fe2012-07-11 00:53:32 +00001815}
1816
Akira Hatanaka544cc212013-01-30 00:26:49 +00001817// An EH_RETURN is the result of lowering llvm.eh.return which in turn is
1818// generated from __builtin_eh_return (offset, handler)
1819// The effect of this is to adjust the stack pointer by "offset"
1820// and then branch to "handler".
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001821SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Akira Hatanaka544cc212013-01-30 00:26:49 +00001822 const {
1823 MachineFunction &MF = DAG.getMachineFunction();
1824 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1825
1826 MipsFI->setCallsEhReturn();
1827 SDValue Chain = Op.getOperand(0);
1828 SDValue Offset = Op.getOperand(1);
1829 SDValue Handler = Op.getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001830 SDLoc DL(Op);
Akira Hatanaka544cc212013-01-30 00:26:49 +00001831 EVT Ty = IsN64 ? MVT::i64 : MVT::i32;
1832
1833 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
1834 // EH_RETURN nodes, so that instructions are emitted back-to-back.
1835 unsigned OffsetReg = IsN64 ? Mips::V1_64 : Mips::V1;
1836 unsigned AddrReg = IsN64 ? Mips::V0_64 : Mips::V0;
1837 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
1838 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
1839 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
1840 DAG.getRegister(OffsetReg, Ty),
1841 DAG.getRegister(AddrReg, getPointerTy()),
1842 Chain.getValue(1));
1843}
1844
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001845SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
Akira Hatanaka864f6602012-06-14 21:10:56 +00001846 SelectionDAG &DAG) const {
Eli Friedman14648462011-07-27 22:21:52 +00001847 // FIXME: Need pseudo-fence for 'singlethread' fences
1848 // FIXME: Set SType for weaker fences where supported/appropriate.
1849 unsigned SType = 0;
Andrew Trickac6d9be2013-05-25 02:42:55 +00001850 SDLoc DL(Op);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001851 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00001852 DAG.getConstant(SType, MVT::i32));
1853}
1854
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001855SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
Akira Hatanaka864f6602012-06-14 21:10:56 +00001856 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001857 SDLoc DL(Op);
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001858 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
1859 SDValue Shamt = Op.getOperand(2);
1860
1861 // if shamt < 32:
1862 // lo = (shl lo, shamt)
1863 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
1864 // else:
1865 // lo = 0
1866 // hi = (shl lo, shamt[4:0])
1867 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
1868 DAG.getConstant(-1, MVT::i32));
1869 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo,
1870 DAG.getConstant(1, MVT::i32));
1871 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, ShiftRight1Lo,
1872 Not);
1873 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi, Shamt);
1874 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
1875 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, MVT::i32, Lo, Shamt);
1876 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
1877 DAG.getConstant(0x20, MVT::i32));
Akira Hatanaka864f6602012-06-14 21:10:56 +00001878 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
1879 DAG.getConstant(0, MVT::i32), ShiftLeftLo);
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001880 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftLeftLo, Or);
1881
1882 SDValue Ops[2] = {Lo, Hi};
1883 return DAG.getMergeValues(Ops, 2, DL);
1884}
1885
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001886SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001887 bool IsSRA) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001888 SDLoc DL(Op);
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001889 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
1890 SDValue Shamt = Op.getOperand(2);
1891
1892 // if shamt < 32:
1893 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
1894 // if isSRA:
1895 // hi = (sra hi, shamt)
1896 // else:
1897 // hi = (srl hi, shamt)
1898 // else:
1899 // if isSRA:
1900 // lo = (sra hi, shamt[4:0])
1901 // hi = (sra hi, 31)
1902 // else:
1903 // lo = (srl hi, shamt[4:0])
1904 // hi = 0
1905 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
1906 DAG.getConstant(-1, MVT::i32));
1907 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi,
1908 DAG.getConstant(1, MVT::i32));
1909 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, ShiftLeft1Hi, Not);
1910 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo, Shamt);
1911 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
1912 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL, DL, MVT::i32,
1913 Hi, Shamt);
1914 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
1915 DAG.getConstant(0x20, MVT::i32));
1916 SDValue Shift31 = DAG.getNode(ISD::SRA, DL, MVT::i32, Hi,
1917 DAG.getConstant(31, MVT::i32));
1918 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftRightHi, Or);
1919 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
1920 IsSRA ? Shift31 : DAG.getConstant(0, MVT::i32),
1921 ShiftRightHi);
1922
1923 SDValue Ops[2] = {Lo, Hi};
1924 return DAG.getMergeValues(Ops, 2, DL);
1925}
1926
Akira Hatanakafee62c12013-04-11 19:07:14 +00001927static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001928 SDValue Chain, SDValue Src, unsigned Offset) {
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00001929 SDValue Ptr = LD->getBasePtr();
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001930 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00001931 EVT BasePtrVT = Ptr.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001932 SDLoc DL(LD);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001933 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
1934
1935 if (Offset)
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00001936 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001937 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001938
1939 SDValue Ops[] = { Chain, Ptr, Src };
1940 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT,
1941 LD->getMemOperand());
1942}
1943
1944// Expand an unaligned 32 or 64-bit integer load node.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001945SDValue MipsTargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001946 LoadSDNode *LD = cast<LoadSDNode>(Op);
1947 EVT MemVT = LD->getMemoryVT();
1948
1949 // Return if load is aligned or if MemVT is neither i32 nor i64.
1950 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
1951 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
1952 return SDValue();
1953
1954 bool IsLittle = Subtarget->isLittle();
1955 EVT VT = Op.getValueType();
1956 ISD::LoadExtType ExtType = LD->getExtensionType();
1957 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
1958
1959 assert((VT == MVT::i32) || (VT == MVT::i64));
1960
1961 // Expand
1962 // (set dst, (i64 (load baseptr)))
1963 // to
1964 // (set tmp, (ldl (add baseptr, 7), undef))
1965 // (set dst, (ldr baseptr, tmp))
1966 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
Akira Hatanakafee62c12013-04-11 19:07:14 +00001967 SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001968 IsLittle ? 7 : 0);
Akira Hatanakafee62c12013-04-11 19:07:14 +00001969 return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001970 IsLittle ? 0 : 7);
1971 }
1972
Akira Hatanakafee62c12013-04-11 19:07:14 +00001973 SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001974 IsLittle ? 3 : 0);
Akira Hatanakafee62c12013-04-11 19:07:14 +00001975 SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001976 IsLittle ? 0 : 3);
1977
1978 // Expand
1979 // (set dst, (i32 (load baseptr))) or
1980 // (set dst, (i64 (sextload baseptr))) or
1981 // (set dst, (i64 (extload baseptr)))
1982 // to
1983 // (set tmp, (lwl (add baseptr, 3), undef))
1984 // (set dst, (lwr baseptr, tmp))
1985 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
1986 (ExtType == ISD::EXTLOAD))
1987 return LWR;
1988
1989 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
1990
1991 // Expand
1992 // (set dst, (i64 (zextload baseptr)))
1993 // to
1994 // (set tmp0, (lwl (add baseptr, 3), undef))
1995 // (set tmp1, (lwr baseptr, tmp0))
1996 // (set tmp2, (shl tmp1, 32))
1997 // (set dst, (srl tmp2, 32))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001998 SDLoc DL(LD);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001999 SDValue Const32 = DAG.getConstant(32, MVT::i32);
2000 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
Akira Hatanaka94ccee22012-06-04 17:46:29 +00002001 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
2002 SDValue Ops[] = { SRL, LWR.getValue(1) };
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002003 return DAG.getMergeValues(Ops, 2, DL);
2004}
2005
Akira Hatanakafee62c12013-04-11 19:07:14 +00002006static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002007 SDValue Chain, unsigned Offset) {
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00002008 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
2009 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00002010 SDLoc DL(SD);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002011 SDVTList VTList = DAG.getVTList(MVT::Other);
2012
2013 if (Offset)
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00002014 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002015 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002016
2017 SDValue Ops[] = { Chain, Value, Ptr };
2018 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT,
2019 SD->getMemOperand());
2020}
2021
2022// Expand an unaligned 32 or 64-bit integer store node.
Akira Hatanaka63451432013-05-16 20:45:17 +00002023static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG,
2024 bool IsLittle) {
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002025 SDValue Value = SD->getValue(), Chain = SD->getChain();
2026 EVT VT = Value.getValueType();
2027
2028 // Expand
2029 // (store val, baseptr) or
2030 // (truncstore val, baseptr)
2031 // to
2032 // (swl val, (add baseptr, 3))
2033 // (swr val, baseptr)
2034 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
Akira Hatanakafee62c12013-04-11 19:07:14 +00002035 SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002036 IsLittle ? 3 : 0);
Akira Hatanakafee62c12013-04-11 19:07:14 +00002037 return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002038 }
2039
2040 assert(VT == MVT::i64);
2041
2042 // Expand
2043 // (store val, baseptr)
2044 // to
2045 // (sdl val, (add baseptr, 7))
2046 // (sdr val, baseptr)
Akira Hatanakafee62c12013-04-11 19:07:14 +00002047 SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2048 return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002049}
2050
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002051// Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr).
2052static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG) {
2053 SDValue Val = SD->getValue();
2054
2055 if (Val.getOpcode() != ISD::FP_TO_SINT)
2056 return SDValue();
2057
2058 EVT FPTy = EVT::getFloatingPointVT(Val.getValueSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002059 SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy,
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002060 Val.getOperand(0));
2061
Andrew Trickac6d9be2013-05-25 02:42:55 +00002062 return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(),
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002063 SD->getPointerInfo(), SD->isVolatile(),
2064 SD->isNonTemporal(), SD->getAlignment());
2065}
2066
Akira Hatanaka63451432013-05-16 20:45:17 +00002067SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2068 StoreSDNode *SD = cast<StoreSDNode>(Op);
2069 EVT MemVT = SD->getMemoryVT();
2070
2071 // Lower unaligned integer stores.
2072 if ((SD->getAlignment() < MemVT.getSizeInBits() / 8) &&
2073 ((MemVT == MVT::i32) || (MemVT == MVT::i64)))
2074 return lowerUnalignedIntStore(SD, DAG, Subtarget->isLittle());
2075
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002076 return lowerFP_TO_SINT_STORE(SD, DAG);
Akira Hatanaka63451432013-05-16 20:45:17 +00002077}
2078
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002079SDValue MipsTargetLowering::lowerADD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanakae90a3bc2012-11-07 19:10:58 +00002080 if (Op->getOperand(0).getOpcode() != ISD::FRAMEADDR
2081 || cast<ConstantSDNode>
2082 (Op->getOperand(0).getOperand(0))->getZExtValue() != 0
2083 || Op->getOperand(1).getOpcode() != ISD::FRAME_TO_ARGS_OFFSET)
2084 return SDValue();
2085
2086 // The pattern
2087 // (add (frameaddr 0), (frame_to_args_offset))
2088 // results from lowering llvm.eh.dwarf.cfa intrinsic. Transform it to
2089 // (add FrameObject, 0)
2090 // where FrameObject is a fixed StackObject with offset 0 which points to
2091 // the old stack pointer.
2092 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2093 EVT ValTy = Op->getValueType(0);
2094 int FI = MFI->CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
2095 SDValue InArgsAddr = DAG.getFrameIndex(FI, ValTy);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002096 return DAG.getNode(ISD::ADD, SDLoc(Op), ValTy, InArgsAddr,
Akira Hatanakae90a3bc2012-11-07 19:10:58 +00002097 DAG.getConstant(0, ValTy));
2098}
2099
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002100SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op,
2101 SelectionDAG &DAG) const {
2102 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002103 SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy,
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002104 Op.getOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00002105 return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002106}
2107
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002108//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002109// Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002110//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002111
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002112//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002113// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002114// Mips O32 ABI rules:
2115// ---
2116// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002117// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002118// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002119// f64 - Only passed in two aliased f32 registers if no int reg has been used
2120// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002121// not used, it must be shadowed. If only A3 is avaiable, shadow it and
2122// go to stack.
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002123//
2124// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002125//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002126
Duncan Sands1e96bab2010-11-04 10:49:57 +00002127static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +00002128 MVT LocVT, CCValAssign::LocInfo LocInfo,
Akira Hatanakaad341d42013-08-20 23:38:40 +00002129 ISD::ArgFlagsTy ArgFlags, CCState &State,
2130 const uint16_t *F64Regs) {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002131
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002132 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002133
Craig Topperc5eaae42012-03-11 07:57:25 +00002134 static const uint16_t IntRegs[] = {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002135 Mips::A0, Mips::A1, Mips::A2, Mips::A3
2136 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002137 static const uint16_t F32Regs[] = {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002138 Mips::F12, Mips::F14
2139 };
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002140
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002141 // Do not process byval args here.
2142 if (ArgFlags.isByVal())
2143 return true;
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002144
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002145 // Promote i8 and i16
2146 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
2147 LocVT = MVT::i32;
2148 if (ArgFlags.isSExt())
2149 LocInfo = CCValAssign::SExt;
2150 else if (ArgFlags.isZExt())
2151 LocInfo = CCValAssign::ZExt;
2152 else
2153 LocInfo = CCValAssign::AExt;
2154 }
2155
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002156 unsigned Reg;
2157
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002158 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
2159 // is true: function is vararg, argument is 3rd or higher, there is previous
2160 // argument which is not f32 or f64.
2161 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
2162 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00002163 unsigned OrigAlign = ArgFlags.getOrigAlign();
2164 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002165
2166 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002167 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00002168 // If this is the first part of an i64 arg,
2169 // the allocated register must be either A0 or A2.
2170 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
2171 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002172 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002173 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
2174 // Allocate int register and shadow next int register. If first
2175 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002176 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2177 if (Reg == Mips::A1 || Reg == Mips::A3)
2178 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2179 State.AllocateReg(IntRegs, IntRegsSize);
2180 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002181 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
2182 // we are guaranteed to find an available float register
2183 if (ValVT == MVT::f32) {
2184 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
2185 // Shadow int register
2186 State.AllocateReg(IntRegs, IntRegsSize);
2187 } else {
2188 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
2189 // Shadow int registers
2190 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
2191 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
2192 State.AllocateReg(IntRegs, IntRegsSize);
2193 State.AllocateReg(IntRegs, IntRegsSize);
2194 }
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002195 } else
2196 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002197
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002198 if (!Reg) {
2199 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3,
2200 OrigAlign);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002201 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002202 } else
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002203 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002204
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002205 return false;
Akira Hatanaka2c5d6522011-11-12 02:20:46 +00002206}
2207
Akira Hatanakaad341d42013-08-20 23:38:40 +00002208static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT,
2209 MVT LocVT, CCValAssign::LocInfo LocInfo,
2210 ISD::ArgFlagsTy ArgFlags, CCState &State) {
2211 static const uint16_t F64Regs[] = { Mips::D6, Mips::D7 };
2212
2213 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2214}
2215
2216static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT,
2217 MVT LocVT, CCValAssign::LocInfo LocInfo,
2218 ISD::ArgFlagsTy ArgFlags, CCState &State) {
2219 static const uint16_t F64Regs[] = { Mips::D12_64, Mips::D12_64 };
2220
2221 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2222}
2223
Akira Hatanaka2c5d6522011-11-12 02:20:46 +00002224#include "MipsGenCallingConv.inc"
2225
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002226//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002227// Call Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002228//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002229
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002230// Return next O32 integer argument register.
2231static unsigned getNextIntArgReg(unsigned Reg) {
2232 assert((Reg == Mips::A0) || (Reg == Mips::A2));
2233 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
2234}
2235
Akira Hatanaka7d712092012-10-30 19:23:25 +00002236SDValue
2237MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002238 SDValue Chain, SDValue Arg, SDLoc DL,
Akira Hatanaka7d712092012-10-30 19:23:25 +00002239 bool IsTailCall, SelectionDAG &DAG) const {
2240 if (!IsTailCall) {
2241 SDValue PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr,
2242 DAG.getIntPtrConstant(Offset));
2243 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo(), false,
2244 false, 0);
2245 }
2246
2247 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2248 int FI = MFI->CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
2249 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2250 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(),
2251 /*isVolatile=*/ true, false, 0);
2252}
2253
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002254void MipsTargetLowering::
2255getOpndList(SmallVectorImpl<SDValue> &Ops,
2256 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
2257 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
2258 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
2259 // Insert node "GP copy globalreg" before call to function.
2260 //
2261 // R_MIPS_CALL* operators (emitted when non-internal functions are called
2262 // in PIC mode) allow symbols to be resolved via lazy binding.
2263 // The lazy binding stub requires GP to point to the GOT.
2264 if (IsPICCall && !InternalLinkage) {
2265 unsigned GPReg = IsN64 ? Mips::GP_64 : Mips::GP;
2266 EVT Ty = IsN64 ? MVT::i64 : MVT::i32;
2267 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty)));
2268 }
Reed Kotler8453b3f2013-01-24 04:24:02 +00002269
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002270 // Build a sequence of copy-to-reg nodes chained together with token
2271 // chain and flag operands which copy the outgoing args into registers.
2272 // The InFlag in necessary since all emitted instructions must be
2273 // stuck together.
2274 SDValue InFlag;
Reed Kotler8453b3f2013-01-24 04:24:02 +00002275
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002276 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2277 Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, RegsToPass[i].first,
2278 RegsToPass[i].second, InFlag);
2279 InFlag = Chain.getValue(1);
2280 }
Reed Kotler8453b3f2013-01-24 04:24:02 +00002281
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002282 // Add argument registers to the end of the list so that they are
2283 // known live into the call.
2284 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2285 Ops.push_back(CLI.DAG.getRegister(RegsToPass[i].first,
2286 RegsToPass[i].second.getValueType()));
Reed Kotler8453b3f2013-01-24 04:24:02 +00002287
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002288 // Add a register mask operand representing the call-preserved registers.
2289 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2290 const uint32_t *Mask = TRI->getCallPreservedMask(CLI.CallConv);
2291 assert(Mask && "Missing call preserved mask for calling convention");
Reed Kotler46090912013-05-10 22:25:39 +00002292 if (Subtarget->inMips16HardFloat()) {
2293 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
2294 llvm::StringRef Sym = G->getGlobal()->getName();
2295 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
2296 if (F->hasFnAttribute("__Mips16RetHelper")) {
2297 Mask = MipsRegisterInfo::getMips16RetHelperMask();
2298 }
2299 }
2300 }
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002301 Ops.push_back(CLI.DAG.getRegisterMask(Mask));
2302
2303 if (InFlag.getNode())
2304 Ops.push_back(InFlag);
Reed Kotler8453b3f2013-01-24 04:24:02 +00002305}
2306
Dan Gohman98ca4f22009-08-05 01:29:28 +00002307/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00002308/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002309SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002310MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002311 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002312 SelectionDAG &DAG = CLI.DAG;
Andrew Trickac6d9be2013-05-25 02:42:55 +00002313 SDLoc DL = CLI.DL;
Craig Toppera0ec3f92013-07-14 04:42:23 +00002314 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
2315 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
2316 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Akira Hatanakae2d529a2012-07-31 18:46:41 +00002317 SDValue Chain = CLI.Chain;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002318 SDValue Callee = CLI.Callee;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002319 bool &IsTailCall = CLI.IsTailCall;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002320 CallingConv::ID CallConv = CLI.CallConv;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002321 bool IsVarArg = CLI.IsVarArg;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002322
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002323 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002324 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanakad37776d2011-05-20 21:39:54 +00002325 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002326 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002327
2328 // Analyze operands of the call, assigning locations to each operand.
2329 SmallVector<CCValAssign, 16> ArgLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002330 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka82099682011-12-19 19:52:25 +00002331 getTargetMachine(), ArgLocs, *DAG.getContext());
Reed Kotler46090912013-05-10 22:25:39 +00002332 MipsCC::SpecialCallingConvType SpecialCallingConv =
2333 getSpecialCallingConv(Callee);
Akira Hatanakaad341d42013-08-20 23:38:40 +00002334 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo,
2335 SpecialCallingConv);
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002336
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002337 MipsCCInfo.analyzeCallOperands(Outs, IsVarArg,
Reed Kotlerc673f9c2013-08-30 19:40:56 +00002338 Subtarget->mipsSEUsesSoftFloat(),
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00002339 Callee.getNode(), CLI.Args);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002340
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002341 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002342 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka480eeb52012-07-26 23:27:01 +00002343
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002344 // Check if it's really possible to do a tail call.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002345 if (IsTailCall)
2346 IsTailCall =
2347 isEligibleForTailCallOptimization(MipsCCInfo, NextStackOffset,
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002348 *MF.getInfo<MipsFunctionInfo>());
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002349
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002350 if (IsTailCall)
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002351 ++NumTailCalls;
2352
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002353 // Chain is the output chain of the last Load/Store or CopyToReg node.
2354 // ByValChain is the output chain of the last Memcpy node created for copying
2355 // byval arguments to the stack.
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002356 unsigned StackAlignment = TFL->getStackAlignment();
2357 NextStackOffset = RoundUpToAlignment(NextStackOffset, StackAlignment);
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002358 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002359
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002360 if (!IsTailCall)
Andrew Trick6e0b2a02013-05-29 22:03:55 +00002361 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffsetVal, DL);
Akira Hatanakae2d529a2012-07-31 18:46:41 +00002362
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002363 SDValue StackPtr = DAG.getCopyFromReg(Chain, DL,
Akira Hatanakae2d529a2012-07-31 18:46:41 +00002364 IsN64 ? Mips::SP_64 : Mips::SP,
2365 getPointerTy());
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002366
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002367 // With EABI is it possible to have 16 args on registers.
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00002368 std::deque< std::pair<unsigned, SDValue> > RegsToPass;
Dan Gohman475871a2008-07-27 21:46:04 +00002369 SmallVector<SDValue, 8> MemOpChains;
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002370 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002371
2372 // Walk the register/memloc assignments, inserting copies/loads.
2373 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00002374 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002375 CCValAssign &VA = ArgLocs[i];
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002376 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002377 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2378
2379 // ByVal Arg.
2380 if (Flags.isByVal()) {
2381 assert(Flags.getByValSize() &&
2382 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002383 assert(ByValArg != MipsCCInfo.byval_end());
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002384 assert(!IsTailCall &&
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002385 "Do not tail-call optimize if there is a byval argument.");
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002386 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002387 MipsCCInfo, *ByValArg, Flags, Subtarget->isLittle());
2388 ++ByValArg;
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002389 continue;
2390 }
Jia Liubb481f82012-02-28 07:46:26 +00002391
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002392 // Promote the value if needed.
2393 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002394 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002395 case CCValAssign::Full:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002396 if (VA.isRegLoc()) {
2397 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00002398 (ValVT == MVT::f64 && LocVT == MVT::i64) ||
2399 (ValVT == MVT::i64 && LocVT == MVT::f64))
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002400 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002401 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002402 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002403 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002404 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002405 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00002406 if (!Subtarget->isLittle())
2407 std::swap(Lo, Hi);
Jia Liubb481f82012-02-28 07:46:26 +00002408 unsigned LocRegLo = VA.getLocReg();
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002409 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2410 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2411 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002412 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002413 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002414 }
2415 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00002416 case CCValAssign::SExt:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002417 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002418 break;
2419 case CCValAssign::ZExt:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002420 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002421 break;
2422 case CCValAssign::AExt:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002423 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002424 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002425 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002426
2427 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002428 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002429 if (VA.isRegLoc()) {
2430 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00002431 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002432 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002433
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002434 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00002435 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002436
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002437 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00002438 // parameter value to a stack Location
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002439 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002440 Chain, Arg, DL, IsTailCall, DAG));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002441 }
2442
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002443 // Transform all store nodes into one single node because all store
2444 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002445 if (!MemOpChains.empty())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002446 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002447 &MemOpChains[0], MemOpChains.size());
2448
Bill Wendling056292f2008-09-16 21:48:12 +00002449 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002450 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2451 // node so that legalize doesn't hack it.
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002452 bool IsPICCall = (IsN64 || IsPIC); // true if calls are translated to jalr $25
Akira Hatanakaed185da2012-12-13 03:17:29 +00002453 bool GlobalOrExternal = false, InternalLinkage = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002454 SDValue CalleeLo;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002455
2456 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002457 if (IsPICCall) {
Akira Hatanakaed185da2012-12-13 03:17:29 +00002458 InternalLinkage = G->getGlobal()->hasInternalLinkage();
2459
2460 if (InternalLinkage)
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002461 Callee = getAddrLocal(Callee, DAG, HasMips64);
Akira Hatanakaf09a0372012-11-21 20:40:38 +00002462 else if (LargeGOT)
2463 Callee = getAddrGlobalLargeGOT(Callee, DAG, MipsII::MO_CALL_HI16,
2464 MipsII::MO_CALL_LO16);
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002465 else
2466 Callee = getAddrGlobal(Callee, DAG, MipsII::MO_GOT_CALL);
2467 } else
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002468 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, getPointerTy(), 0,
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002469 MipsII::MO_NO_FLAG);
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002470 GlobalOrExternal = true;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002471 }
2472 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakaf09a0372012-11-21 20:40:38 +00002473 if (!IsN64 && !IsPIC) // !N64 && static
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002474 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2475 MipsII::MO_NO_FLAG);
Akira Hatanakaf09a0372012-11-21 20:40:38 +00002476 else if (LargeGOT)
2477 Callee = getAddrGlobalLargeGOT(Callee, DAG, MipsII::MO_CALL_HI16,
2478 MipsII::MO_CALL_LO16);
Akira Hatanaka60689322013-02-22 21:10:03 +00002479 else // N64 || PIC
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002480 Callee = getAddrGlobal(Callee, DAG, MipsII::MO_GOT_CALL);
2481
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002482 GlobalOrExternal = true;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002483 }
2484
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00002485 SmallVector<SDValue, 8> Ops(1, Chain);
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002486 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00002487
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002488 getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal, InternalLinkage,
2489 CLI, Callee, Chain);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002490
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002491 if (IsTailCall)
2492 return DAG.getNode(MipsISD::TailCall, DL, MVT::Other, &Ops[0], Ops.size());
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002493
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002494 Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, &Ops[0], Ops.size());
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002495 SDValue InFlag = Chain.getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002496
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002497 // Create the CALLSEQ_END node.
Akira Hatanaka480eeb52012-07-26 23:27:01 +00002498 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal,
Andrew Trick6e0b2a02013-05-29 22:03:55 +00002499 DAG.getIntPtrConstant(0, true), InFlag, DL);
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002500 InFlag = Chain.getValue(1);
2501
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002502 // Handle result values, copying them out of physregs into vregs that we
2503 // return.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002504 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg,
2505 Ins, DL, DAG, InVals, CLI.Callee.getNode(), CLI.RetTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002506}
2507
Dan Gohman98ca4f22009-08-05 01:29:28 +00002508/// LowerCallResult - Lower the result values of a call into the
2509/// appropriate copies out of appropriate physical registers.
2510SDValue
2511MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002512 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002513 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002514 SDLoc DL, SelectionDAG &DAG,
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002515 SmallVectorImpl<SDValue> &InVals,
2516 const SDNode *CallNode,
2517 const Type *RetTy) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002518 // Assign locations to each value returned by this call.
2519 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002520 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka864f6602012-06-14 21:10:56 +00002521 getTargetMachine(), RVLocs, *DAG.getContext());
Akira Hatanakaad341d42013-08-20 23:38:40 +00002522 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo);
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002523
Reed Kotlerc673f9c2013-08-30 19:40:56 +00002524 MipsCCInfo.analyzeCallResult(Ins, Subtarget->mipsSEUsesSoftFloat(),
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002525 CallNode, RetTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002526
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002527 // Copy all of the result registers out of their specified physreg.
2528 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002529 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(),
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002530 RVLocs[i].getLocVT(), InFlag);
2531 Chain = Val.getValue(1);
2532 InFlag = Val.getValue(2);
2533
2534 if (RVLocs[i].getValVT() != RVLocs[i].getLocVT())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002535 Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getValVT(), Val);
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002536
2537 InVals.push_back(Val);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002538 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002539
Dan Gohman98ca4f22009-08-05 01:29:28 +00002540 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002541}
2542
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002543//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002544// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002545//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002546/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002547/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002548SDValue
2549MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002550 CallingConv::ID CallConv,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002551 bool IsVarArg,
Akira Hatanaka82099682011-12-19 19:52:25 +00002552 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002553 SDLoc DL, SelectionDAG &DAG,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002554 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002555 const {
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00002556 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002557 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00002558 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002559
Dan Gohman1e93df62010-04-17 14:41:14 +00002560 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002561
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002562 // Used with vargs to acumulate store chains.
2563 std::vector<SDValue> OutChains;
2564
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002565 // Assign locations to all of the incoming arguments.
2566 SmallVector<CCValAssign, 16> ArgLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002567 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka82099682011-12-19 19:52:25 +00002568 getTargetMachine(), ArgLocs, *DAG.getContext());
Akira Hatanakaad341d42013-08-20 23:38:40 +00002569 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo);
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002570 Function::const_arg_iterator FuncArg =
2571 DAG.getMachineFunction().getFunction()->arg_begin();
Reed Kotlerc673f9c2013-08-30 19:40:56 +00002572 bool UseSoftFloat = Subtarget->mipsSEUsesSoftFloat();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002573
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002574 MipsCCInfo.analyzeFormalArguments(Ins, UseSoftFloat, FuncArg);
Akira Hatanakab33b34a2012-10-30 19:37:25 +00002575 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(),
2576 MipsCCInfo.hasByValArg());
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002577
Akira Hatanaka4618e0b2012-10-27 00:44:39 +00002578 unsigned CurArgIdx = 0;
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002579 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002580
Akira Hatanaka4618e0b2012-10-27 00:44:39 +00002581 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002582 CCValAssign &VA = ArgLocs[i];
Akira Hatanaka4618e0b2012-10-27 00:44:39 +00002583 std::advance(FuncArg, Ins[i].OrigArgIndex - CurArgIdx);
2584 CurArgIdx = Ins[i].OrigArgIndex;
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002585 EVT ValVT = VA.getValVT();
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002586 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2587 bool IsRegLoc = VA.isRegLoc();
2588
2589 if (Flags.isByVal()) {
2590 assert(Flags.getByValSize() &&
2591 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002592 assert(ByValArg != MipsCCInfo.byval_end());
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002593 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg,
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002594 MipsCCInfo, *ByValArg);
2595 ++ByValArg;
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002596 continue;
2597 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002598
2599 // Arguments stored on registers
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002600 if (IsRegLoc) {
Owen Andersone50ed302009-08-10 22:56:29 +00002601 EVT RegVT = VA.getLocVT();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002602 unsigned ArgReg = VA.getLocReg();
Craig Topper44d23822012-02-22 05:59:10 +00002603 const TargetRegisterClass *RC;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002604
Owen Anderson825b72b2009-08-11 20:47:22 +00002605 if (RegVT == MVT::i32)
Reed Kotlerbacbf1c2012-12-20 06:06:35 +00002606 RC = Subtarget->inMips16Mode()? &Mips::CPU16RegsRegClass :
Akira Hatanaka18587862013-08-06 23:08:38 +00002607 &Mips::GPR32RegClass;
Akira Hatanaka95934842011-09-24 01:34:44 +00002608 else if (RegVT == MVT::i64)
Akira Hatanaka18587862013-08-06 23:08:38 +00002609 RC = &Mips::GPR64RegClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002610 else if (RegVT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00002611 RC = &Mips::FGR32RegClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002612 else if (RegVT == MVT::f64)
Akira Hatanakaad341d42013-08-20 23:38:40 +00002613 RC = Subtarget->isFP64bit() ? &Mips::FGR64RegClass :
2614 &Mips::AFGR64RegClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002615 else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002616 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002617
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002618 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002619 // physical registers into virtual ones
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002620 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC);
2621 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002622
2623 // If this is an 8 or 16-bit value, it has been passed promoted
2624 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002625 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002626 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00002627 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002628 if (VA.getLocInfo() == CCValAssign::SExt)
2629 Opcode = ISD::AssertSext;
2630 else if (VA.getLocInfo() == CCValAssign::ZExt)
2631 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00002632 if (Opcode)
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002633 ArgValue = DAG.getNode(Opcode, DL, RegVT, ArgValue,
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002634 DAG.getValueType(ValVT));
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002635 ArgValue = DAG.getNode(ISD::TRUNCATE, DL, ValVT, ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002636 }
2637
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002638 // Handle floating point arguments passed in integer registers and
2639 // long double arguments passed in floating point registers.
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002640 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002641 (RegVT == MVT::i64 && ValVT == MVT::f64) ||
2642 (RegVT == MVT::f64 && ValVT == MVT::i64))
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002643 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002644 else if (IsO32 && RegVT == MVT::i32 && ValVT == MVT::f64) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002645 unsigned Reg2 = addLiveIn(DAG.getMachineFunction(),
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002646 getNextIntArgReg(ArgReg), RC);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002647 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT);
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002648 if (!Subtarget->isLittle())
2649 std::swap(ArgValue, ArgValue2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002650 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64,
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002651 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002652 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002653
Dan Gohman98ca4f22009-08-05 01:29:28 +00002654 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002655 } else { // VA.isRegLoc()
2656
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002657 // sanity check
2658 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002659
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002660 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002661 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002662 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002663
2664 // Create load nodes to retrieve arguments from the stack
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002665 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002666 InVals.push_back(DAG.getLoad(ValVT, DL, Chain, FIN,
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002667 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002668 false, false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002669 }
2670 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002671
2672 // The mips ABIs for returning structs by value requires that we copy
2673 // the sret argument into $v0 for the return. Save the argument into
2674 // a virtual register so that we can access it from the return points.
2675 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2676 unsigned Reg = MipsFI->getSRetReturnReg();
2677 if (!Reg) {
Akira Hatanaka30580ce2012-10-19 22:11:40 +00002678 Reg = MF.getRegInfo().
2679 createVirtualRegister(getRegClassFor(IsN64 ? MVT::i64 : MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002680 MipsFI->setSRetReturnReg(Reg);
2681 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002682 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[0]);
2683 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002684 }
2685
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002686 if (IsVarArg)
2687 writeVarArgRegs(OutChains, MipsCCInfo, Chain, DL, DAG);
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002688
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002689 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002690 // the size of Ins and InVals. This only happens when on varg functions
2691 if (!OutChains.empty()) {
2692 OutChains.push_back(Chain);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002693 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002694 &OutChains[0], OutChains.size());
2695 }
2696
Dan Gohman98ca4f22009-08-05 01:29:28 +00002697 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002698}
2699
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002700//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002701// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002702//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002703
Akira Hatanaka97d9f082012-10-10 01:27:09 +00002704bool
2705MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002706 MachineFunction &MF, bool IsVarArg,
Akira Hatanaka97d9f082012-10-10 01:27:09 +00002707 const SmallVectorImpl<ISD::OutputArg> &Outs,
2708 LLVMContext &Context) const {
2709 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002710 CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(),
Akira Hatanaka97d9f082012-10-10 01:27:09 +00002711 RVLocs, Context);
2712 return CCInfo.CheckReturn(Outs, RetCC_Mips);
2713}
2714
Dan Gohman98ca4f22009-08-05 01:29:28 +00002715SDValue
2716MipsTargetLowering::LowerReturn(SDValue Chain,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002717 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002718 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002719 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002720 SDLoc DL, SelectionDAG &DAG) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002721 // CCValAssign - represent the assignment of
2722 // the return value to a location
2723 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002724 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002725
2726 // CCState - Info about the registers and stack slot.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002727 CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(), RVLocs,
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002728 *DAG.getContext());
Akira Hatanakaad341d42013-08-20 23:38:40 +00002729 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002730
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002731 // Analyze return values.
Reed Kotlerc673f9c2013-08-30 19:40:56 +00002732 MipsCCInfo.analyzeReturn(Outs, Subtarget->mipsSEUsesSoftFloat(),
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002733 MF.getFunction()->getReturnType());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002734
Dan Gohman475871a2008-07-27 21:46:04 +00002735 SDValue Flag;
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002736 SmallVector<SDValue, 4> RetOps(1, Chain);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002737
2738 // Copy the result values into the output registers.
2739 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002740 SDValue Val = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002741 CCValAssign &VA = RVLocs[i];
2742 assert(VA.isRegLoc() && "Can only return in registers!");
2743
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002744 if (RVLocs[i].getValVT() != RVLocs[i].getLocVT())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002745 Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getLocVT(), Val);
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002746
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002747 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002748
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002749 // Guarantee that all emitted copies are stuck together with flags.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002750 Flag = Chain.getValue(1);
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002751 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002752 }
2753
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002754 // The mips ABIs for returning structs by value requires that we copy
2755 // the sret argument into $v0 for the return. We saved the argument into
2756 // a virtual register in the entry block, so now we copy the value out
2757 // and into $v0.
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002758 if (MF.getFunction()->hasStructRetAttr()) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002759 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2760 unsigned Reg = MipsFI->getSRetReturnReg();
2761
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002762 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00002763 llvm_unreachable("sret virtual register not created in the entry block");
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002764 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Akira Hatanaka2ef5bd32012-10-24 02:10:54 +00002765 unsigned V0 = IsN64 ? Mips::V0_64 : Mips::V0;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002766
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002767 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002768 Flag = Chain.getValue(1);
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002769 RetOps.push_back(DAG.getRegister(V0, getPointerTy()));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002770 }
2771
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002772 RetOps[0] = Chain; // Update chain.
Akira Hatanaka182ef6f2012-07-10 00:19:06 +00002773
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002774 // Add the flag if we have it.
2775 if (Flag.getNode())
2776 RetOps.push_back(Flag);
2777
2778 // Return on Mips is always a "jr $ra"
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002779 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, &RetOps[0], RetOps.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002780}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002781
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002782//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002783// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002784//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002785
2786/// getConstraintType - Given a constraint letter, return the type of
2787/// constraint it is for this target.
2788MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002789getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002790{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002791 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002792 // GCC config/mips/constraints.md
2793 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002794 // 'd' : An address register. Equivalent to r
2795 // unless generating MIPS16 code.
2796 // 'y' : Equivalent to r; retained for
2797 // backwards compatibility.
Eric Christopher1d5a3922012-05-07 06:25:10 +00002798 // 'c' : A register suitable for use in an indirect
2799 // jump. This will always be $25 for -mabicalls.
Eric Christopheraf97f732012-05-07 06:25:19 +00002800 // 'l' : The lo register. 1 word storage.
2801 // 'x' : The hilo register pair. Double word storage.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002802 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002803 switch (Constraint[0]) {
2804 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002805 case 'd':
2806 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002807 case 'f':
Eric Christopher1d5a3922012-05-07 06:25:10 +00002808 case 'c':
Eric Christopher4adbefe2012-05-07 06:25:15 +00002809 case 'l':
Eric Christopheraf97f732012-05-07 06:25:19 +00002810 case 'x':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002811 return C_RegisterClass;
Jack Carter0b9675d2013-03-04 21:33:15 +00002812 case 'R':
2813 return C_Memory;
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002814 }
2815 }
2816 return TargetLowering::getConstraintType(Constraint);
2817}
2818
John Thompson44ab89e2010-10-29 17:29:13 +00002819/// Examine constraint type and operand type and determine a weight value.
2820/// This object must already have been set up with the operand type
2821/// and the current alternative constraint selected.
2822TargetLowering::ConstraintWeight
2823MipsTargetLowering::getSingleConstraintMatchWeight(
2824 AsmOperandInfo &info, const char *constraint) const {
2825 ConstraintWeight weight = CW_Invalid;
2826 Value *CallOperandVal = info.CallOperandVal;
2827 // If we don't have a value, we can't do a match,
2828 // but allow it at the lowest weight.
2829 if (CallOperandVal == NULL)
2830 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002831 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00002832 // Look at the constraint type.
2833 switch (*constraint) {
2834 default:
2835 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2836 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002837 case 'd':
2838 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00002839 if (type->isIntegerTy())
2840 weight = CW_Register;
2841 break;
2842 case 'f':
2843 if (type->isFloatTy())
2844 weight = CW_Register;
2845 break;
Eric Christopher1d5a3922012-05-07 06:25:10 +00002846 case 'c': // $25 for indirect jumps
Eric Christopher4adbefe2012-05-07 06:25:15 +00002847 case 'l': // lo register
Eric Christopheraf97f732012-05-07 06:25:19 +00002848 case 'x': // hilo register pair
Eric Christopher1d5a3922012-05-07 06:25:10 +00002849 if (type->isIntegerTy())
2850 weight = CW_SpecificReg;
2851 break;
Eric Christopher50ab0392012-05-07 03:13:32 +00002852 case 'I': // signed 16 bit immediate
Eric Christophere5076d42012-05-07 03:13:42 +00002853 case 'J': // integer zero
Eric Christopherf49f8462012-05-07 05:46:29 +00002854 case 'K': // unsigned 16 bit immediate
Eric Christopher5ac47bb2012-05-07 05:46:37 +00002855 case 'L': // signed 32 bit immediate where lower 16 bits are 0
Eric Christopher60cfc792012-05-07 05:46:43 +00002856 case 'N': // immediate in the range of -65535 to -1 (inclusive)
Eric Christopher1ce20342012-05-07 05:46:48 +00002857 case 'O': // signed 15 bit immediate (+- 16383)
Eric Christopher54412a72012-05-07 06:25:02 +00002858 case 'P': // immediate in the range of 65535 to 1 (inclusive)
Eric Christopher50ab0392012-05-07 03:13:32 +00002859 if (isa<ConstantInt>(CallOperandVal))
2860 weight = CW_Constant;
2861 break;
Jack Carter0b9675d2013-03-04 21:33:15 +00002862 case 'R':
2863 weight = CW_Memory;
2864 break;
John Thompson44ab89e2010-10-29 17:29:13 +00002865 }
2866 return weight;
2867}
2868
Akira Hatanakabfb07b12013-08-14 00:21:25 +00002869/// This is a helper function to parse a physical register string and split it
2870/// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag
2871/// that is returned indicates whether parsing was successful. The second flag
2872/// is true if the numeric part exists.
2873static std::pair<bool, bool>
2874parsePhysicalReg(const StringRef &C, std::string &Prefix,
2875 unsigned long long &Reg) {
2876 if (C.front() != '{' || C.back() != '}')
2877 return std::make_pair(false, false);
2878
2879 // Search for the first numeric character.
2880 StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1;
2881 I = std::find_if(B, E, std::ptr_fun(isdigit));
2882
2883 Prefix.assign(B, I - B);
2884
2885 // The second flag is set to false if no numeric characters were found.
2886 if (I == E)
2887 return std::make_pair(true, false);
2888
2889 // Parse the numeric characters.
2890 return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg),
2891 true);
2892}
2893
2894std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering::
2895parseRegForInlineAsmConstraint(const StringRef &C, MVT VT) const {
2896 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2897 const TargetRegisterClass *RC;
2898 std::string Prefix;
2899 unsigned long long Reg;
2900
2901 std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg);
2902
2903 if (!R.first)
2904 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2905
2906 if ((Prefix == "hi" || Prefix == "lo")) { // Parse hi/lo.
2907 // No numeric characters follow "hi" or "lo".
2908 if (R.second)
2909 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2910
2911 RC = TRI->getRegClass(Prefix == "hi" ?
Akira Hatanakacbaf6d02013-08-14 00:47:08 +00002912 Mips::HI32RegClassID : Mips::LO32RegClassID);
Akira Hatanakabfb07b12013-08-14 00:21:25 +00002913 return std::make_pair(*(RC->begin()), RC);
2914 }
2915
2916 if (!R.second)
2917 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2918
2919 if (Prefix == "$f") { // Parse $f0-$f31.
2920 // If the size of FP registers is 64-bit or Reg is an even number, select
2921 // the 64-bit register class. Otherwise, select the 32-bit register class.
2922 if (VT == MVT::Other)
2923 VT = (Subtarget->isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32;
2924
2925 RC= getRegClassFor(VT);
2926
2927 if (RC == &Mips::AFGR64RegClass) {
2928 assert(Reg % 2 == 0);
2929 Reg >>= 1;
2930 }
2931 } else if (Prefix == "$fcc") { // Parse $fcc0-$fcc7.
2932 RC = TRI->getRegClass(Mips::FCCRegClassID);
2933 } else { // Parse $0-$31.
2934 assert(Prefix == "$");
2935 RC = getRegClassFor((VT == MVT::Other) ? MVT::i32 : VT);
2936 }
2937
2938 assert(Reg < RC->getNumRegs());
2939 return std::make_pair(*(RC->begin() + Reg), RC);
2940}
2941
Eric Christopher38d64262011-06-29 19:33:04 +00002942/// Given a register class constraint, like 'r', if this corresponds directly
2943/// to an LLVM register class, return a register of 0 and the register class
2944/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002945std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Chad Rosier5b3fca52013-06-22 18:37:38 +00002946getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002947{
2948 if (Constraint.size() == 1) {
2949 switch (Constraint[0]) {
Eric Christopher314aff12011-06-29 19:04:31 +00002950 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
2951 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002952 case 'r':
Akira Hatanakaafc945b2012-09-12 23:27:55 +00002953 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
2954 if (Subtarget->inMips16Mode())
2955 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
Akira Hatanaka18587862013-08-06 23:08:38 +00002956 return std::make_pair(0U, &Mips::GPR32RegClass);
Akira Hatanakaafc945b2012-09-12 23:27:55 +00002957 }
Jack Carter10de0252012-07-02 23:35:23 +00002958 if (VT == MVT::i64 && !HasMips64)
Akira Hatanaka18587862013-08-06 23:08:38 +00002959 return std::make_pair(0U, &Mips::GPR32RegClass);
Eric Christopher0ed1f762012-05-07 03:13:22 +00002960 if (VT == MVT::i64 && HasMips64)
Akira Hatanaka18587862013-08-06 23:08:38 +00002961 return std::make_pair(0U, &Mips::GPR64RegClass);
Eric Christopher0ed1f762012-05-07 03:13:22 +00002962 // This will generate an error message
2963 return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002964 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00002965 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00002966 return std::make_pair(0U, &Mips::FGR32RegClass);
Akira Hatanakacb9dd722012-01-04 02:45:01 +00002967 if ((VT == MVT::f64) && (!Subtarget->isSingleFloat())) {
2968 if (Subtarget->isFP64bit())
Craig Topper420761a2012-04-20 07:30:17 +00002969 return std::make_pair(0U, &Mips::FGR64RegClass);
2970 return std::make_pair(0U, &Mips::AFGR64RegClass);
Akira Hatanakacb9dd722012-01-04 02:45:01 +00002971 }
Eric Christopher1d5a3922012-05-07 06:25:10 +00002972 break;
2973 case 'c': // register suitable for indirect jump
2974 if (VT == MVT::i32)
Akira Hatanaka18587862013-08-06 23:08:38 +00002975 return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass);
Eric Christopher1d5a3922012-05-07 06:25:10 +00002976 assert(VT == MVT::i64 && "Unexpected type.");
Akira Hatanaka18587862013-08-06 23:08:38 +00002977 return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass);
Eric Christopher4adbefe2012-05-07 06:25:15 +00002978 case 'l': // register suitable for indirect jump
2979 if (VT == MVT::i32)
Akira Hatanakacbaf6d02013-08-14 00:47:08 +00002980 return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass);
2981 return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass);
Eric Christopheraf97f732012-05-07 06:25:19 +00002982 case 'x': // register suitable for indirect jump
2983 // Fixme: Not triggering the use of both hi and low
2984 // This will generate an error message
2985 return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002986 }
2987 }
Akira Hatanakabfb07b12013-08-14 00:21:25 +00002988
2989 std::pair<unsigned, const TargetRegisterClass *> R;
2990 R = parseRegForInlineAsmConstraint(Constraint, VT);
2991
2992 if (R.second)
2993 return R;
2994
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002995 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2996}
2997
Eric Christopher50ab0392012-05-07 03:13:32 +00002998/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
2999/// vector. If it is invalid, don't add anything to Ops.
3000void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3001 std::string &Constraint,
3002 std::vector<SDValue>&Ops,
3003 SelectionDAG &DAG) const {
3004 SDValue Result(0, 0);
3005
3006 // Only support length 1 constraints for now.
3007 if (Constraint.length() > 1) return;
3008
3009 char ConstraintLetter = Constraint[0];
3010 switch (ConstraintLetter) {
3011 default: break; // This will fall through to the generic implementation
3012 case 'I': // Signed 16 bit constant
3013 // If this fails, the parent routine will give an error
3014 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3015 EVT Type = Op.getValueType();
3016 int64_t Val = C->getSExtValue();
3017 if (isInt<16>(Val)) {
3018 Result = DAG.getTargetConstant(Val, Type);
3019 break;
3020 }
3021 }
3022 return;
Eric Christophere5076d42012-05-07 03:13:42 +00003023 case 'J': // integer zero
3024 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3025 EVT Type = Op.getValueType();
3026 int64_t Val = C->getZExtValue();
3027 if (Val == 0) {
3028 Result = DAG.getTargetConstant(0, Type);
3029 break;
3030 }
3031 }
3032 return;
Eric Christopherf49f8462012-05-07 05:46:29 +00003033 case 'K': // unsigned 16 bit immediate
3034 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3035 EVT Type = Op.getValueType();
3036 uint64_t Val = (uint64_t)C->getZExtValue();
3037 if (isUInt<16>(Val)) {
3038 Result = DAG.getTargetConstant(Val, Type);
3039 break;
3040 }
3041 }
3042 return;
Eric Christopher5ac47bb2012-05-07 05:46:37 +00003043 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3044 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3045 EVT Type = Op.getValueType();
3046 int64_t Val = C->getSExtValue();
3047 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
3048 Result = DAG.getTargetConstant(Val, Type);
3049 break;
3050 }
3051 }
3052 return;
Eric Christopher60cfc792012-05-07 05:46:43 +00003053 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3054 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3055 EVT Type = Op.getValueType();
3056 int64_t Val = C->getSExtValue();
3057 if ((Val >= -65535) && (Val <= -1)) {
3058 Result = DAG.getTargetConstant(Val, Type);
3059 break;
3060 }
3061 }
3062 return;
Eric Christopher1ce20342012-05-07 05:46:48 +00003063 case 'O': // signed 15 bit immediate
3064 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3065 EVT Type = Op.getValueType();
3066 int64_t Val = C->getSExtValue();
3067 if ((isInt<15>(Val))) {
3068 Result = DAG.getTargetConstant(Val, Type);
3069 break;
3070 }
3071 }
3072 return;
Eric Christopher54412a72012-05-07 06:25:02 +00003073 case 'P': // immediate in the range of 1 to 65535 (inclusive)
3074 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3075 EVT Type = Op.getValueType();
3076 int64_t Val = C->getSExtValue();
3077 if ((Val <= 65535) && (Val >= 1)) {
3078 Result = DAG.getTargetConstant(Val, Type);
3079 break;
3080 }
3081 }
3082 return;
Eric Christopher50ab0392012-05-07 03:13:32 +00003083 }
3084
3085 if (Result.getNode()) {
3086 Ops.push_back(Result);
3087 return;
3088 }
3089
3090 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3091}
3092
Dan Gohman6520e202008-10-18 02:06:02 +00003093bool
Akira Hatanaka94e47282012-11-17 00:25:41 +00003094MipsTargetLowering::isLegalAddressingMode(const AddrMode &AM, Type *Ty) const {
3095 // No global is ever allowed as a base.
3096 if (AM.BaseGV)
3097 return false;
3098
3099 switch (AM.Scale) {
3100 case 0: // "r+i" or just "i", depending on HasBaseReg.
3101 break;
3102 case 1:
3103 if (!AM.HasBaseReg) // allow "r+i".
3104 break;
3105 return false; // disallow "r+r" or "r+r+i".
3106 default:
3107 return false;
3108 }
3109
3110 return true;
3111}
3112
3113bool
Dan Gohman6520e202008-10-18 02:06:02 +00003114MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3115 // The Mips target isn't yet aware of offsets.
3116 return false;
3117}
Evan Chengeb2f9692009-10-27 19:56:55 +00003118
Akira Hatanakae193b322012-06-13 19:33:32 +00003119EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00003120 unsigned SrcAlign,
3121 bool IsMemset, bool ZeroMemset,
Akira Hatanakae193b322012-06-13 19:33:32 +00003122 bool MemcpyStrSrc,
3123 MachineFunction &MF) const {
3124 if (Subtarget->hasMips64())
3125 return MVT::i64;
3126
3127 return MVT::i32;
3128}
3129
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003130bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
3131 if (VT != MVT::f32 && VT != MVT::f64)
3132 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00003133 if (Imm.isNegZero())
3134 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00003135 return Imm.isZero();
3136}
Akira Hatanaka6c2cf8b2012-02-03 04:33:00 +00003137
3138unsigned MipsTargetLowering::getJumpTableEncoding() const {
3139 if (IsN64)
3140 return MachineJumpTableInfo::EK_GPRel64BlockAddress;
Jia Liubb481f82012-02-28 07:46:26 +00003141
Akira Hatanaka6c2cf8b2012-02-03 04:33:00 +00003142 return TargetLowering::getJumpTableEncoding();
3143}
Akira Hatanaka7887c902012-10-26 23:56:38 +00003144
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003145/// This function returns true if CallSym is a long double emulation routine.
3146static bool isF128SoftLibCall(const char *CallSym) {
3147 const char *const LibCalls[] =
3148 {"__addtf3", "__divtf3", "__eqtf2", "__extenddftf2", "__extendsftf2",
3149 "__fixtfdi", "__fixtfsi", "__fixtfti", "__fixunstfdi", "__fixunstfsi",
3150 "__fixunstfti", "__floatditf", "__floatsitf", "__floattitf",
3151 "__floatunditf", "__floatunsitf", "__floatuntitf", "__getf2", "__gttf2",
3152 "__letf2", "__lttf2", "__multf3", "__netf2", "__powitf2", "__subtf3",
3153 "__trunctfdf2", "__trunctfsf2", "__unordtf2",
3154 "ceill", "copysignl", "cosl", "exp2l", "expl", "floorl", "fmal", "fmodl",
3155 "log10l", "log2l", "logl", "nearbyintl", "powl", "rintl", "sinl", "sqrtl",
3156 "truncl"};
3157
3158 const char * const *End = LibCalls + array_lengthof(LibCalls);
3159
3160 // Check that LibCalls is sorted alphabetically.
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00003161 MipsTargetLowering::LTStr Comp;
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003162
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00003163#ifndef NDEBUG
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003164 for (const char * const *I = LibCalls; I < End - 1; ++I)
3165 assert(Comp(*I, *(I + 1)));
3166#endif
3167
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00003168 return std::binary_search(LibCalls, End, CallSym, Comp);
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003169}
3170
3171/// This function returns true if Ty is fp128 or i128 which was originally a
3172/// fp128.
3173static bool originalTypeIsF128(const Type *Ty, const SDNode *CallNode) {
3174 if (Ty->isFP128Ty())
3175 return true;
3176
3177 const ExternalSymbolSDNode *ES =
3178 dyn_cast_or_null<const ExternalSymbolSDNode>(CallNode);
3179
3180 // If the Ty is i128 and the function being called is a long double emulation
3181 // routine, then the original type is f128.
3182 return (ES && Ty->isIntegerTy(128) && isF128SoftLibCall(ES->getSymbol()));
3183}
3184
Reed Kotler46090912013-05-10 22:25:39 +00003185MipsTargetLowering::MipsCC::SpecialCallingConvType
3186 MipsTargetLowering::getSpecialCallingConv(SDValue Callee) const {
3187 MipsCC::SpecialCallingConvType SpecialCallingConv =
3188 MipsCC::NoSpecialCallingConv;;
3189 if (Subtarget->inMips16HardFloat()) {
3190 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3191 llvm::StringRef Sym = G->getGlobal()->getName();
3192 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
3193 if (F->hasFnAttribute("__Mips16RetHelper")) {
3194 SpecialCallingConv = MipsCC::Mips16RetHelperConv;
3195 }
3196 }
3197 }
3198 return SpecialCallingConv;
3199}
3200
3201MipsTargetLowering::MipsCC::MipsCC(
Akira Hatanakaad341d42013-08-20 23:38:40 +00003202 CallingConv::ID CC, bool IsO32_, bool IsFP64_, CCState &Info,
Reed Kotler46090912013-05-10 22:25:39 +00003203 MipsCC::SpecialCallingConvType SpecialCallingConv_)
Akira Hatanakaad341d42013-08-20 23:38:40 +00003204 : CCInfo(Info), CallConv(CC), IsO32(IsO32_), IsFP64(IsFP64_),
Reed Kotler46090912013-05-10 22:25:39 +00003205 SpecialCallingConv(SpecialCallingConv_){
Akira Hatanaka7887c902012-10-26 23:56:38 +00003206 // Pre-allocate reserved argument area.
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003207 CCInfo.AllocateStack(reservedArgArea(), 1);
Akira Hatanaka7887c902012-10-26 23:56:38 +00003208}
3209
Reed Kotler46090912013-05-10 22:25:39 +00003210
Akira Hatanaka7887c902012-10-26 23:56:38 +00003211void MipsTargetLowering::MipsCC::
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003212analyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Args,
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00003213 bool IsVarArg, bool IsSoftFloat, const SDNode *CallNode,
3214 std::vector<ArgListEntry> &FuncArgs) {
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003215 assert((CallConv != CallingConv::Fast || !IsVarArg) &&
3216 "CallingConv::Fast shouldn't be used for vararg functions.");
3217
Akira Hatanaka7887c902012-10-26 23:56:38 +00003218 unsigned NumOpnds = Args.size();
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003219 llvm::CCAssignFn *FixedFn = fixedArgFn(), *VarFn = varArgFn();
Akira Hatanaka7887c902012-10-26 23:56:38 +00003220
3221 for (unsigned I = 0; I != NumOpnds; ++I) {
3222 MVT ArgVT = Args[I].VT;
3223 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
3224 bool R;
3225
3226 if (ArgFlags.isByVal()) {
3227 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
3228 continue;
3229 }
3230
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003231 if (IsVarArg && !Args[I].IsFixed)
Akira Hatanaka7887c902012-10-26 23:56:38 +00003232 R = VarFn(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00003233 else {
3234 MVT RegVT = getRegVT(ArgVT, FuncArgs[Args[I].OrigArgIndex].Ty, CallNode,
3235 IsSoftFloat);
3236 R = FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo);
3237 }
Akira Hatanaka7887c902012-10-26 23:56:38 +00003238
3239 if (R) {
3240#ifndef NDEBUG
3241 dbgs() << "Call operand #" << I << " has unhandled type "
3242 << EVT(ArgVT).getEVTString();
3243#endif
3244 llvm_unreachable(0);
3245 }
3246 }
3247}
3248
3249void MipsTargetLowering::MipsCC::
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003250analyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Args,
3251 bool IsSoftFloat, Function::const_arg_iterator FuncArg) {
Akira Hatanaka7887c902012-10-26 23:56:38 +00003252 unsigned NumArgs = Args.size();
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003253 llvm::CCAssignFn *FixedFn = fixedArgFn();
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003254 unsigned CurArgIdx = 0;
Akira Hatanaka7887c902012-10-26 23:56:38 +00003255
3256 for (unsigned I = 0; I != NumArgs; ++I) {
3257 MVT ArgVT = Args[I].VT;
3258 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003259 std::advance(FuncArg, Args[I].OrigArgIndex - CurArgIdx);
3260 CurArgIdx = Args[I].OrigArgIndex;
Akira Hatanaka7887c902012-10-26 23:56:38 +00003261
3262 if (ArgFlags.isByVal()) {
3263 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
3264 continue;
3265 }
3266
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003267 MVT RegVT = getRegVT(ArgVT, FuncArg->getType(), 0, IsSoftFloat);
3268
3269 if (!FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo))
Akira Hatanaka7887c902012-10-26 23:56:38 +00003270 continue;
3271
3272#ifndef NDEBUG
3273 dbgs() << "Formal Arg #" << I << " has unhandled type "
3274 << EVT(ArgVT).getEVTString();
3275#endif
3276 llvm_unreachable(0);
3277 }
3278}
3279
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003280template<typename Ty>
3281void MipsTargetLowering::MipsCC::
3282analyzeReturn(const SmallVectorImpl<Ty> &RetVals, bool IsSoftFloat,
3283 const SDNode *CallNode, const Type *RetTy) const {
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003284 CCAssignFn *Fn;
3285
3286 if (IsSoftFloat && originalTypeIsF128(RetTy, CallNode))
3287 Fn = RetCC_F128Soft;
3288 else
3289 Fn = RetCC_Mips;
3290
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003291 for (unsigned I = 0, E = RetVals.size(); I < E; ++I) {
3292 MVT VT = RetVals[I].VT;
3293 ISD::ArgFlagsTy Flags = RetVals[I].Flags;
3294 MVT RegVT = this->getRegVT(VT, RetTy, CallNode, IsSoftFloat);
3295
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003296 if (Fn(I, VT, RegVT, CCValAssign::Full, Flags, this->CCInfo)) {
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003297#ifndef NDEBUG
3298 dbgs() << "Call result #" << I << " has unhandled type "
3299 << EVT(VT).getEVTString() << '\n';
3300#endif
3301 llvm_unreachable(0);
3302 }
3303 }
3304}
3305
3306void MipsTargetLowering::MipsCC::
3307analyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins, bool IsSoftFloat,
3308 const SDNode *CallNode, const Type *RetTy) const {
3309 analyzeReturn(Ins, IsSoftFloat, CallNode, RetTy);
3310}
3311
3312void MipsTargetLowering::MipsCC::
3313analyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs, bool IsSoftFloat,
3314 const Type *RetTy) const {
3315 analyzeReturn(Outs, IsSoftFloat, 0, RetTy);
3316}
3317
Akira Hatanaka7887c902012-10-26 23:56:38 +00003318void
3319MipsTargetLowering::MipsCC::handleByValArg(unsigned ValNo, MVT ValVT,
3320 MVT LocVT,
3321 CCValAssign::LocInfo LocInfo,
3322 ISD::ArgFlagsTy ArgFlags) {
3323 assert(ArgFlags.getByValSize() && "Byval argument's size shouldn't be 0.");
3324
3325 struct ByValArgInfo ByVal;
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003326 unsigned RegSize = regSize();
Akira Hatanaka7887c902012-10-26 23:56:38 +00003327 unsigned ByValSize = RoundUpToAlignment(ArgFlags.getByValSize(), RegSize);
3328 unsigned Align = std::min(std::max(ArgFlags.getByValAlign(), RegSize),
3329 RegSize * 2);
3330
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003331 if (useRegsForByval())
Akira Hatanaka7887c902012-10-26 23:56:38 +00003332 allocateRegs(ByVal, ByValSize, Align);
3333
3334 // Allocate space on caller's stack.
3335 ByVal.Address = CCInfo.AllocateStack(ByValSize - RegSize * ByVal.NumRegs,
3336 Align);
3337 CCInfo.addLoc(CCValAssign::getMem(ValNo, ValVT, ByVal.Address, LocVT,
3338 LocInfo));
3339 ByValArgs.push_back(ByVal);
3340}
3341
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003342unsigned MipsTargetLowering::MipsCC::numIntArgRegs() const {
3343 return IsO32 ? array_lengthof(O32IntRegs) : array_lengthof(Mips64IntRegs);
3344}
3345
3346unsigned MipsTargetLowering::MipsCC::reservedArgArea() const {
3347 return (IsO32 && (CallConv != CallingConv::Fast)) ? 16 : 0;
3348}
3349
3350const uint16_t *MipsTargetLowering::MipsCC::intArgRegs() const {
3351 return IsO32 ? O32IntRegs : Mips64IntRegs;
3352}
3353
3354llvm::CCAssignFn *MipsTargetLowering::MipsCC::fixedArgFn() const {
3355 if (CallConv == CallingConv::Fast)
3356 return CC_Mips_FastCC;
3357
Reed Kotler46090912013-05-10 22:25:39 +00003358 if (SpecialCallingConv == Mips16RetHelperConv)
3359 return CC_Mips16RetHelper;
Akira Hatanakaad341d42013-08-20 23:38:40 +00003360 return IsO32 ? (IsFP64 ? CC_MipsO32_FP64 : CC_MipsO32_FP32) : CC_MipsN;
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003361}
3362
3363llvm::CCAssignFn *MipsTargetLowering::MipsCC::varArgFn() const {
Akira Hatanakaad341d42013-08-20 23:38:40 +00003364 return IsO32 ? (IsFP64 ? CC_MipsO32_FP64 : CC_MipsO32_FP32) : CC_MipsN_VarArg;
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003365}
3366
3367const uint16_t *MipsTargetLowering::MipsCC::shadowRegs() const {
3368 return IsO32 ? O32IntRegs : Mips64DPRegs;
3369}
3370
Akira Hatanaka7887c902012-10-26 23:56:38 +00003371void MipsTargetLowering::MipsCC::allocateRegs(ByValArgInfo &ByVal,
3372 unsigned ByValSize,
3373 unsigned Align) {
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003374 unsigned RegSize = regSize(), NumIntArgRegs = numIntArgRegs();
3375 const uint16_t *IntArgRegs = intArgRegs(), *ShadowRegs = shadowRegs();
Akira Hatanaka7887c902012-10-26 23:56:38 +00003376 assert(!(ByValSize % RegSize) && !(Align % RegSize) &&
3377 "Byval argument's size and alignment should be a multiple of"
3378 "RegSize.");
3379
3380 ByVal.FirstIdx = CCInfo.getFirstUnallocated(IntArgRegs, NumIntArgRegs);
3381
3382 // If Align > RegSize, the first arg register must be even.
3383 if ((Align > RegSize) && (ByVal.FirstIdx % 2)) {
3384 CCInfo.AllocateReg(IntArgRegs[ByVal.FirstIdx], ShadowRegs[ByVal.FirstIdx]);
3385 ++ByVal.FirstIdx;
3386 }
3387
3388 // Mark the registers allocated.
3389 for (unsigned I = ByVal.FirstIdx; ByValSize && (I < NumIntArgRegs);
3390 ByValSize -= RegSize, ++I, ++ByVal.NumRegs)
3391 CCInfo.AllocateReg(IntArgRegs[I], ShadowRegs[I]);
3392}
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003393
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003394MVT MipsTargetLowering::MipsCC::getRegVT(MVT VT, const Type *OrigTy,
3395 const SDNode *CallNode,
3396 bool IsSoftFloat) const {
3397 if (IsSoftFloat || IsO32)
3398 return VT;
3399
3400 // Check if the original type was fp128.
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003401 if (originalTypeIsF128(OrigTy, CallNode)) {
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003402 assert(VT == MVT::i64);
3403 return MVT::f64;
3404 }
3405
3406 return VT;
3407}
3408
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003409void MipsTargetLowering::
Andrew Trickac6d9be2013-05-25 02:42:55 +00003410copyByValRegs(SDValue Chain, SDLoc DL, std::vector<SDValue> &OutChains,
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003411 SelectionDAG &DAG, const ISD::ArgFlagsTy &Flags,
3412 SmallVectorImpl<SDValue> &InVals, const Argument *FuncArg,
3413 const MipsCC &CC, const ByValArgInfo &ByVal) const {
3414 MachineFunction &MF = DAG.getMachineFunction();
3415 MachineFrameInfo *MFI = MF.getFrameInfo();
3416 unsigned RegAreaSize = ByVal.NumRegs * CC.regSize();
3417 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
3418 int FrameObjOffset;
3419
3420 if (RegAreaSize)
3421 FrameObjOffset = (int)CC.reservedArgArea() -
3422 (int)((CC.numIntArgRegs() - ByVal.FirstIdx) * CC.regSize());
3423 else
3424 FrameObjOffset = ByVal.Address;
3425
3426 // Create frame object.
3427 EVT PtrTy = getPointerTy();
3428 int FI = MFI->CreateFixedObject(FrameObjSize, FrameObjOffset, true);
3429 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
3430 InVals.push_back(FIN);
3431
3432 if (!ByVal.NumRegs)
3433 return;
3434
3435 // Copy arg registers.
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00003436 MVT RegTy = MVT::getIntegerVT(CC.regSize() * 8);
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003437 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3438
3439 for (unsigned I = 0; I < ByVal.NumRegs; ++I) {
3440 unsigned ArgReg = CC.intArgRegs()[ByVal.FirstIdx + I];
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003441 unsigned VReg = addLiveIn(MF, ArgReg, RC);
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003442 unsigned Offset = I * CC.regSize();
3443 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
3444 DAG.getConstant(Offset, PtrTy));
3445 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
3446 StorePtr, MachinePointerInfo(FuncArg, Offset),
3447 false, false, 0);
3448 OutChains.push_back(Store);
3449 }
3450}
Akira Hatanakadb40ede2012-10-27 00:16:36 +00003451
3452// Copy byVal arg to registers and stack.
3453void MipsTargetLowering::
Andrew Trickac6d9be2013-05-25 02:42:55 +00003454passByValArg(SDValue Chain, SDLoc DL,
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00003455 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
Craig Toppera0ec3f92013-07-14 04:42:23 +00003456 SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr,
Akira Hatanakadb40ede2012-10-27 00:16:36 +00003457 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
3458 const MipsCC &CC, const ByValArgInfo &ByVal,
3459 const ISD::ArgFlagsTy &Flags, bool isLittle) const {
3460 unsigned ByValSize = Flags.getByValSize();
3461 unsigned Offset = 0; // Offset in # of bytes from the beginning of struct.
3462 unsigned RegSize = CC.regSize();
3463 unsigned Alignment = std::min(Flags.getByValAlign(), RegSize);
3464 EVT PtrTy = getPointerTy(), RegTy = MVT::getIntegerVT(RegSize * 8);
3465
3466 if (ByVal.NumRegs) {
3467 const uint16_t *ArgRegs = CC.intArgRegs();
3468 bool LeftoverBytes = (ByVal.NumRegs * RegSize > ByValSize);
3469 unsigned I = 0;
3470
3471 // Copy words to registers.
3472 for (; I < ByVal.NumRegs - LeftoverBytes; ++I, Offset += RegSize) {
3473 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3474 DAG.getConstant(Offset, PtrTy));
3475 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
3476 MachinePointerInfo(), false, false, false,
3477 Alignment);
3478 MemOpChains.push_back(LoadVal.getValue(1));
3479 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3480 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
3481 }
3482
3483 // Return if the struct has been fully copied.
3484 if (ByValSize == Offset)
3485 return;
3486
3487 // Copy the remainder of the byval argument with sub-word loads and shifts.
3488 if (LeftoverBytes) {
3489 assert((ByValSize > Offset) && (ByValSize < Offset + RegSize) &&
3490 "Size of the remainder should be smaller than RegSize.");
3491 SDValue Val;
3492
3493 for (unsigned LoadSize = RegSize / 2, TotalSizeLoaded = 0;
3494 Offset < ByValSize; LoadSize /= 2) {
3495 unsigned RemSize = ByValSize - Offset;
3496
3497 if (RemSize < LoadSize)
3498 continue;
3499
3500 // Load subword.
3501 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3502 DAG.getConstant(Offset, PtrTy));
3503 SDValue LoadVal =
3504 DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr,
3505 MachinePointerInfo(), MVT::getIntegerVT(LoadSize * 8),
3506 false, false, Alignment);
3507 MemOpChains.push_back(LoadVal.getValue(1));
3508
3509 // Shift the loaded value.
3510 unsigned Shamt;
3511
3512 if (isLittle)
3513 Shamt = TotalSizeLoaded;
3514 else
3515 Shamt = (RegSize - (TotalSizeLoaded + LoadSize)) * 8;
3516
3517 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
3518 DAG.getConstant(Shamt, MVT::i32));
3519
3520 if (Val.getNode())
3521 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
3522 else
3523 Val = Shift;
3524
3525 Offset += LoadSize;
3526 TotalSizeLoaded += LoadSize;
3527 Alignment = std::min(Alignment, LoadSize);
3528 }
3529
3530 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3531 RegsToPass.push_back(std::make_pair(ArgReg, Val));
3532 return;
3533 }
3534 }
3535
3536 // Copy remainder of byval arg to it with memcpy.
3537 unsigned MemCpySize = ByValSize - Offset;
3538 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3539 DAG.getConstant(Offset, PtrTy));
3540 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
3541 DAG.getIntPtrConstant(ByVal.Address));
3542 Chain = DAG.getMemcpy(Chain, DL, Dst, Src,
3543 DAG.getConstant(MemCpySize, PtrTy), Alignment,
3544 /*isVolatile=*/false, /*AlwaysInline=*/false,
3545 MachinePointerInfo(0), MachinePointerInfo(0));
3546 MemOpChains.push_back(Chain);
3547}
Akira Hatanakaf0848472012-10-27 00:21:13 +00003548
3549void
3550MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
3551 const MipsCC &CC, SDValue Chain,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003552 SDLoc DL, SelectionDAG &DAG) const {
Akira Hatanakaf0848472012-10-27 00:21:13 +00003553 unsigned NumRegs = CC.numIntArgRegs();
3554 const uint16_t *ArgRegs = CC.intArgRegs();
3555 const CCState &CCInfo = CC.getCCInfo();
3556 unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs, NumRegs);
3557 unsigned RegSize = CC.regSize();
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00003558 MVT RegTy = MVT::getIntegerVT(RegSize * 8);
Akira Hatanakaf0848472012-10-27 00:21:13 +00003559 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3560 MachineFunction &MF = DAG.getMachineFunction();
3561 MachineFrameInfo *MFI = MF.getFrameInfo();
3562 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3563
3564 // Offset of the first variable argument from stack pointer.
3565 int VaArgOffset;
3566
3567 if (NumRegs == Idx)
3568 VaArgOffset = RoundUpToAlignment(CCInfo.getNextStackOffset(), RegSize);
3569 else
3570 VaArgOffset =
3571 (int)CC.reservedArgArea() - (int)(RegSize * (NumRegs - Idx));
3572
3573 // Record the frame index of the first variable argument
3574 // which is a value necessary to VASTART.
3575 int FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
3576 MipsFI->setVarArgsFrameIndex(FI);
3577
3578 // Copy the integer registers that have not been used for argument passing
3579 // to the argument register save area. For O32, the save area is allocated
3580 // in the caller's stack frame, while for N32/64, it is allocated in the
3581 // callee's stack frame.
3582 for (unsigned I = Idx; I < NumRegs; ++I, VaArgOffset += RegSize) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003583 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC);
Akira Hatanakaf0848472012-10-27 00:21:13 +00003584 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
3585 FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
3586 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
3587 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
3588 MachinePointerInfo(), false, false, 0);
3589 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(0);
3590 OutChains.push_back(Store);
3591 }
3592}