blob: 21c5edb39cb1a410139f6b4c3abccd27c5c24d73 [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";
Akira Hatanaka0f843822011-06-07 18:58:42 +0000217 default: return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000218 }
219}
220
221MipsTargetLowering::
Chris Lattnerf0144122009-07-28 03:13:23 +0000222MipsTargetLowering(MipsTargetMachine &TM)
Akira Hatanaka8b4198d2011-09-26 21:47:02 +0000223 : TargetLowering(TM, new MipsTargetObjectFile()),
224 Subtarget(&TM.getSubtarget<MipsSubtarget>()),
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +0000225 HasMips64(Subtarget->hasMips64()), IsN64(Subtarget->isABI_N64()),
226 IsO32(Subtarget->isABI_O32()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000227 // Mips does not have i1 type, so use i32 for
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000228 // setcc operations results (slt, sgt, ...).
Duncan Sands03228082008-11-23 15:47:28 +0000229 setBooleanContents(ZeroOrOneBooleanContent);
Akira Hatanakacd6c5792013-04-30 22:37:26 +0000230 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000231
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000232 // Load extented operations for i1 types must be promoted
Owen Anderson825b72b2009-08-11 20:47:22 +0000233 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
234 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
235 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000236
Eli Friedman6055a6a2009-07-17 04:07:24 +0000237 // MIPS doesn't have extending float->double load/store
Owen Anderson825b72b2009-08-11 20:47:22 +0000238 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
239 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman10a36592009-07-17 02:28:12 +0000240
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000241 // Used by legalize types to correctly generate the setcc result.
242 // Without this, every float setcc comes with a AND/OR with the result,
243 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000244 // which is used implicitly by brcond and select operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000245 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000246
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000247 // Mips Custom Operations
Akira Hatanakab7656a92013-03-06 21:32:03 +0000248 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000249 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000250 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000251 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
252 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
253 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
254 setOperationAction(ISD::SELECT, MVT::f32, Custom);
255 setOperationAction(ISD::SELECT, MVT::f64, Custom);
256 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Akira Hatanaka3fef29d2012-07-11 19:32:27 +0000257 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
258 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Akira Hatanaka0a40c232012-03-09 23:46:03 +0000259 setOperationAction(ISD::SETCC, MVT::f32, Custom);
260 setOperationAction(ISD::SETCC, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000261 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000262 setOperationAction(ISD::VASTART, MVT::Other, Custom);
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000263 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
264 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +0000265 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000266
Akira Hatanakac12a6e62012-04-11 22:49:04 +0000267 if (!TM.Options.NoNaNsFPMath) {
268 setOperationAction(ISD::FABS, MVT::f32, Custom);
269 setOperationAction(ISD::FABS, MVT::f64, Custom);
270 }
271
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000272 if (HasMips64) {
273 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
274 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
275 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
276 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
277 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
278 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Akira Hatanaka7664f052012-06-02 00:04:42 +0000279 setOperationAction(ISD::LOAD, MVT::i64, Custom);
280 setOperationAction(ISD::STORE, MVT::i64, Custom);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +0000281 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000282 }
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000283
Akira Hatanakaa284acb2012-05-09 00:55:21 +0000284 if (!HasMips64) {
285 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
286 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
287 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
288 }
289
Akira Hatanakae90a3bc2012-11-07 19:10:58 +0000290 setOperationAction(ISD::ADD, MVT::i32, Custom);
291 if (HasMips64)
292 setOperationAction(ISD::ADD, MVT::i64, Custom);
293
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000294 setOperationAction(ISD::SDIV, MVT::i32, Expand);
295 setOperationAction(ISD::SREM, MVT::i32, Expand);
296 setOperationAction(ISD::UDIV, MVT::i32, Expand);
297 setOperationAction(ISD::UREM, MVT::i32, Expand);
Akira Hatanakadda4a072011-10-03 21:06:13 +0000298 setOperationAction(ISD::SDIV, MVT::i64, Expand);
299 setOperationAction(ISD::SREM, MVT::i64, Expand);
300 setOperationAction(ISD::UDIV, MVT::i64, Expand);
301 setOperationAction(ISD::UREM, MVT::i64, Expand);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000302
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000303 // Operations not directly supported by Mips.
Tom Stellard3ef53832013-03-08 15:36:57 +0000304 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
305 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
306 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
307 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000308 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
309 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Akira Hatanakae1bcd6b2011-12-20 23:40:56 +0000310 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000311 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Akira Hatanakae1bcd6b2011-12-20 23:40:56 +0000312 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000313 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
314 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
Akira Hatanaka7f162742011-12-21 00:14:05 +0000315 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000316 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
Akira Hatanaka7f162742011-12-21 00:14:05 +0000317 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000318 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
319 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
320 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
321 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000322 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000323 setOperationAction(ISD::ROTL, MVT::i64, Expand);
Akira Hatanaka1d165f12012-07-31 20:54:48 +0000324 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
325 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000326
Akira Hatanaka56633442011-09-20 23:53:09 +0000327 if (!Subtarget->hasMips32r2())
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000328 setOperationAction(ISD::ROTR, MVT::i32, Expand);
329
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000330 if (!Subtarget->hasMips64r2())
331 setOperationAction(ISD::ROTR, MVT::i64, Expand);
332
Owen Anderson825b72b2009-08-11 20:47:22 +0000333 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000334 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000335 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000336 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng8688a582013-01-29 02:32:37 +0000337 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
338 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000339 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
340 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanaka46da1362011-05-23 22:23:58 +0000341 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000342 setOperationAction(ISD::FLOG, MVT::f32, Expand);
343 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
344 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
345 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000346 setOperationAction(ISD::FMA, MVT::f32, Expand);
347 setOperationAction(ISD::FMA, MVT::f64, Expand);
Akira Hatanaka21ecc2f2012-03-29 18:43:11 +0000348 setOperationAction(ISD::FREM, MVT::f32, Expand);
349 setOperationAction(ISD::FREM, MVT::f64, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000350
Akira Hatanaka1cc63332012-04-11 22:59:08 +0000351 if (!TM.Options.NoNaNsFPMath) {
352 setOperationAction(ISD::FNEG, MVT::f32, Expand);
353 setOperationAction(ISD::FNEG, MVT::f64, Expand);
354 }
355
Akira Hatanaka544cc212013-01-30 00:26:49 +0000356 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
357
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +0000358 setOperationAction(ISD::VAARG, MVT::Other, Expand);
359 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
360 setOperationAction(ISD::VAEND, MVT::Other, Expand);
361
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000362 // Use the default for now
Owen Anderson825b72b2009-08-11 20:47:22 +0000363 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
364 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000365
Jia Liubb481f82012-02-28 07:46:26 +0000366 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
367 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
368 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
369 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
Eli Friedman4db5aca2011-08-29 18:23:02 +0000370
Eli Friedman26689ac2011-08-03 21:06:02 +0000371 setInsertFencesForAtomic(true);
372
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000373 if (!Subtarget->hasSEInReg()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000374 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
375 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000376 }
377
Akira Hatanakac79507a2011-12-21 00:20:27 +0000378 if (!Subtarget->hasBitCount()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000379 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Akira Hatanakac79507a2011-12-21 00:20:27 +0000380 setOperationAction(ISD::CTLZ, MVT::i64, Expand);
381 }
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000382
Akira Hatanakac0ea0432011-12-20 23:56:43 +0000383 if (!Subtarget->hasSwap()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000384 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Akira Hatanakac0ea0432011-12-20 23:56:43 +0000385 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
386 }
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000387
Akira Hatanaka7664f052012-06-02 00:04:42 +0000388 if (HasMips64) {
389 setLoadExtAction(ISD::SEXTLOAD, MVT::i32, Custom);
390 setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, Custom);
391 setLoadExtAction(ISD::EXTLOAD, MVT::i32, Custom);
392 setTruncStoreAction(MVT::i64, MVT::i32, Custom);
393 }
394
Akira Hatanaka97585622013-07-26 20:58:55 +0000395 setOperationAction(ISD::TRAP, MVT::Other, Legal);
396
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000397 setTargetDAGCombine(ISD::SDIVREM);
398 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanakaee8c3b02012-03-08 03:26:37 +0000399 setTargetDAGCombine(ISD::SELECT);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000400 setTargetDAGCombine(ISD::AND);
401 setTargetDAGCombine(ISD::OR);
Akira Hatanaka87827072012-06-13 20:33:18 +0000402 setTargetDAGCombine(ISD::ADD);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000403
Akira Hatanaka5fdf5002012-03-08 01:59:33 +0000404 setMinFunctionAlignment(HasMips64 ? 3 : 2);
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000405
Akira Hatanaka3f5b1072012-02-02 03:17:04 +0000406 setStackPointerRegisterToSaveRestore(IsN64 ? Mips::SP_64 : Mips::SP);
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000407
Akira Hatanaka590baca2012-02-02 03:13:40 +0000408 setExceptionPointerRegister(IsN64 ? Mips::A0_64 : Mips::A0);
409 setExceptionSelectorRegister(IsN64 ? Mips::A1_64 : Mips::A1);
Akira Hatanakae193b322012-06-13 19:33:32 +0000410
Jim Grosbach3450f802013-02-20 21:13:59 +0000411 MaxStoresPerMemcpy = 16;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000412}
413
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000414const MipsTargetLowering *MipsTargetLowering::create(MipsTargetMachine &TM) {
415 if (TM.getSubtargetImpl()->inMips16Mode())
416 return llvm::createMips16TargetLowering(TM);
Jia Liubb481f82012-02-28 07:46:26 +0000417
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000418 return llvm::createMipsSETargetLowering(TM);
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000419}
420
Matt Arsenault225ed702013-05-18 00:21:46 +0000421EVT MipsTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Akira Hatanakae13f4412013-01-04 20:06:01 +0000422 if (!VT.isVector())
423 return MVT::i32;
424 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +0000425}
426
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000427static SDValue performDivRemCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000428 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000429 const MipsSubtarget *Subtarget) {
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000430 if (DCI.isBeforeLegalizeOps())
431 return SDValue();
432
Akira Hatanakadda4a072011-10-03 21:06:13 +0000433 EVT Ty = N->getValueType(0);
Akira Hatanakacbaf6d02013-08-14 00:47:08 +0000434 unsigned LO = (Ty == MVT::i32) ? Mips::LO0 : Mips::LO0_64;
435 unsigned HI = (Ty == MVT::i32) ? Mips::HI0 : Mips::HI0_64;
Akira Hatanakaf5926fd2013-03-30 01:36:35 +0000436 unsigned Opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem16 :
437 MipsISD::DivRemU16;
Andrew Trickac6d9be2013-05-25 02:42:55 +0000438 SDLoc DL(N);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000439
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000440 SDValue DivRem = DAG.getNode(Opc, DL, MVT::Glue,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000441 N->getOperand(0), N->getOperand(1));
442 SDValue InChain = DAG.getEntryNode();
443 SDValue InGlue = DivRem;
444
445 // insert MFLO
446 if (N->hasAnyUseOfValue(0)) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000447 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, DL, LO, Ty,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000448 InGlue);
449 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
450 InChain = CopyFromLo.getValue(1);
451 InGlue = CopyFromLo.getValue(2);
452 }
453
454 // insert MFHI
455 if (N->hasAnyUseOfValue(1)) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000456 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, DL,
Akira Hatanakadda4a072011-10-03 21:06:13 +0000457 HI, Ty, InGlue);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000458 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
459 }
460
461 return SDValue();
462}
463
Akira Hatanaka2fbe90c2013-04-18 01:00:46 +0000464static Mips::CondCode condCodeToFCC(ISD::CondCode CC) {
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000465 switch (CC) {
466 default: llvm_unreachable("Unknown fp condition code!");
467 case ISD::SETEQ:
468 case ISD::SETOEQ: return Mips::FCOND_OEQ;
469 case ISD::SETUNE: return Mips::FCOND_UNE;
470 case ISD::SETLT:
471 case ISD::SETOLT: return Mips::FCOND_OLT;
472 case ISD::SETGT:
473 case ISD::SETOGT: return Mips::FCOND_OGT;
474 case ISD::SETLE:
475 case ISD::SETOLE: return Mips::FCOND_OLE;
476 case ISD::SETGE:
477 case ISD::SETOGE: return Mips::FCOND_OGE;
478 case ISD::SETULT: return Mips::FCOND_ULT;
479 case ISD::SETULE: return Mips::FCOND_ULE;
480 case ISD::SETUGT: return Mips::FCOND_UGT;
481 case ISD::SETUGE: return Mips::FCOND_UGE;
482 case ISD::SETUO: return Mips::FCOND_UN;
483 case ISD::SETO: return Mips::FCOND_OR;
484 case ISD::SETNE:
485 case ISD::SETONE: return Mips::FCOND_ONE;
486 case ISD::SETUEQ: return Mips::FCOND_UEQ;
487 }
488}
489
490
Akira Hatanaka9cf07242013-03-30 01:16:38 +0000491/// This function returns true if the floating point conditional branches and
492/// conditional moves which use condition code CC should be inverted.
493static bool invertFPCondCodeUser(Mips::CondCode CC) {
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000494 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
495 return false;
496
Akira Hatanaka82099682011-12-19 19:52:25 +0000497 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
498 "Illegal Condition Code");
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000499
Akira Hatanaka82099682011-12-19 19:52:25 +0000500 return true;
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000501}
502
503// Creates and returns an FPCmp node from a setcc node.
504// Returns Op if setcc is not a floating point comparison.
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000505static SDValue createFPCmp(SelectionDAG &DAG, const SDValue &Op) {
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000506 // must be a SETCC node
507 if (Op.getOpcode() != ISD::SETCC)
508 return Op;
509
510 SDValue LHS = Op.getOperand(0);
511
512 if (!LHS.getValueType().isFloatingPoint())
513 return Op;
514
515 SDValue RHS = Op.getOperand(1);
Andrew Trickac6d9be2013-05-25 02:42:55 +0000516 SDLoc DL(Op);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000517
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000518 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
519 // node if necessary.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000520 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
521
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000522 return DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS,
Akira Hatanaka2fbe90c2013-04-18 01:00:46 +0000523 DAG.getConstant(condCodeToFCC(CC), MVT::i32));
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000524}
525
526// Creates and returns a CMovFPT/F node.
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000527static SDValue createCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000528 SDValue False, SDLoc DL) {
Akira Hatanaka9cf07242013-03-30 01:16:38 +0000529 ConstantSDNode *CC = cast<ConstantSDNode>(Cond.getOperand(2));
530 bool invert = invertFPCondCodeUser((Mips::CondCode)CC->getSExtValue());
Akira Hatanaka407883b2013-07-26 20:51:20 +0000531 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000532
533 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
Akira Hatanaka407883b2013-07-26 20:51:20 +0000534 True.getValueType(), True, FCC0, False, Cond);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000535}
536
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000537static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000538 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000539 const MipsSubtarget *Subtarget) {
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000540 if (DCI.isBeforeLegalizeOps())
541 return SDValue();
542
543 SDValue SetCC = N->getOperand(0);
544
545 if ((SetCC.getOpcode() != ISD::SETCC) ||
546 !SetCC.getOperand(0).getValueType().isInteger())
547 return SDValue();
548
549 SDValue False = N->getOperand(2);
550 EVT FalseTy = False.getValueType();
551
552 if (!FalseTy.isInteger())
553 return SDValue();
554
555 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(False);
556
557 if (!CN || CN->getZExtValue())
558 return SDValue();
559
Andrew Trickac6d9be2013-05-25 02:42:55 +0000560 const SDLoc DL(N);
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000561 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
562 SDValue True = N->getOperand(1);
Akira Hatanaka864f6602012-06-14 21:10:56 +0000563
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000564 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
565 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
Akira Hatanaka864f6602012-06-14 21:10:56 +0000566
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000567 return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True);
568}
569
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000570static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000571 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000572 const MipsSubtarget *Subtarget) {
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000573 // Pattern match EXT.
574 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
575 // => ext $dst, $src, size, pos
Akira Hatanaka56633442011-09-20 23:53:09 +0000576 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000577 return SDValue();
578
579 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000580 unsigned ShiftRightOpc = ShiftRight.getOpcode();
581
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000582 // Op's first operand must be a shift right.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000583 if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL)
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000584 return SDValue();
585
586 // The second operand of the shift must be an immediate.
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000587 ConstantSDNode *CN;
588 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
589 return SDValue();
Jia Liubb481f82012-02-28 07:46:26 +0000590
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000591 uint64_t Pos = CN->getZExtValue();
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000592 uint64_t SMPos, SMSize;
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000593
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000594 // Op's second operand must be a shifted mask.
595 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000596 !isShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000597 return SDValue();
598
599 // Return if the shifted mask does not start at bit 0 or the sum of its size
600 // and Pos exceeds the word's size.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000601 EVT ValTy = N->getValueType(0);
602 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000603 return SDValue();
604
Andrew Trickac6d9be2013-05-25 02:42:55 +0000605 return DAG.getNode(MipsISD::Ext, SDLoc(N), ValTy,
Akira Hatanaka82099682011-12-19 19:52:25 +0000606 ShiftRight.getOperand(0), DAG.getConstant(Pos, MVT::i32),
Akira Hatanaka667645f2011-08-17 22:59:46 +0000607 DAG.getConstant(SMSize, MVT::i32));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000608}
Jia Liubb481f82012-02-28 07:46:26 +0000609
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000610static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000611 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000612 const MipsSubtarget *Subtarget) {
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000613 // Pattern match INS.
614 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
Jia Liubb481f82012-02-28 07:46:26 +0000615 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000616 // => ins $dst, $src, size, pos, $src1
Akira Hatanaka56633442011-09-20 23:53:09 +0000617 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000618 return SDValue();
619
620 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
621 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
622 ConstantSDNode *CN;
623
624 // See if Op's first operand matches (and $src1 , mask0).
625 if (And0.getOpcode() != ISD::AND)
626 return SDValue();
627
628 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000629 !isShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000630 return SDValue();
631
632 // See if Op's second operand matches (and (shl $src, pos), mask1).
633 if (And1.getOpcode() != ISD::AND)
634 return SDValue();
Jia Liubb481f82012-02-28 07:46:26 +0000635
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000636 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000637 !isShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000638 return SDValue();
639
640 // The shift masks must have the same position and size.
641 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
642 return SDValue();
643
644 SDValue Shl = And1.getOperand(0);
645 if (Shl.getOpcode() != ISD::SHL)
646 return SDValue();
647
648 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
649 return SDValue();
650
651 unsigned Shamt = CN->getZExtValue();
652
653 // Return if the shift amount and the first bit position of mask are not the
Jia Liubb481f82012-02-28 07:46:26 +0000654 // same.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000655 EVT ValTy = N->getValueType(0);
656 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000657 return SDValue();
Jia Liubb481f82012-02-28 07:46:26 +0000658
Andrew Trickac6d9be2013-05-25 02:42:55 +0000659 return DAG.getNode(MipsISD::Ins, SDLoc(N), ValTy, Shl.getOperand(0),
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000660 DAG.getConstant(SMPos0, MVT::i32),
Akira Hatanaka82099682011-12-19 19:52:25 +0000661 DAG.getConstant(SMSize0, MVT::i32), And0.getOperand(0));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000662}
Jia Liubb481f82012-02-28 07:46:26 +0000663
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000664static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka87827072012-06-13 20:33:18 +0000665 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000666 const MipsSubtarget *Subtarget) {
Akira Hatanaka87827072012-06-13 20:33:18 +0000667 // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt))
668
669 if (DCI.isBeforeLegalizeOps())
670 return SDValue();
671
672 SDValue Add = N->getOperand(1);
673
674 if (Add.getOpcode() != ISD::ADD)
675 return SDValue();
676
677 SDValue Lo = Add.getOperand(1);
678
679 if ((Lo.getOpcode() != MipsISD::Lo) ||
680 (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable))
681 return SDValue();
682
683 EVT ValTy = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +0000684 SDLoc DL(N);
Akira Hatanaka87827072012-06-13 20:33:18 +0000685
686 SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, N->getOperand(0),
687 Add.getOperand(0));
688 return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo);
689}
690
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000691SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000692 const {
693 SelectionDAG &DAG = DCI.DAG;
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000694 unsigned Opc = N->getOpcode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000695
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000696 switch (Opc) {
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000697 default: break;
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000698 case ISD::SDIVREM:
699 case ISD::UDIVREM:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000700 return performDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000701 case ISD::SELECT:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000702 return performSELECTCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000703 case ISD::AND:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000704 return performANDCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000705 case ISD::OR:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000706 return performORCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka87827072012-06-13 20:33:18 +0000707 case ISD::ADD:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000708 return performADDCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000709 }
710
711 return SDValue();
712}
713
Akira Hatanakab430cec2012-09-21 23:58:31 +0000714void
715MipsTargetLowering::LowerOperationWrapper(SDNode *N,
716 SmallVectorImpl<SDValue> &Results,
717 SelectionDAG &DAG) const {
718 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
719
720 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
721 Results.push_back(Res.getValue(I));
722}
723
724void
725MipsTargetLowering::ReplaceNodeResults(SDNode *N,
726 SmallVectorImpl<SDValue> &Results,
727 SelectionDAG &DAG) const {
Akira Hatanaka13ec4812013-04-30 21:17:07 +0000728 return LowerOperationWrapper(N, Results, DAG);
Akira Hatanakab430cec2012-09-21 23:58:31 +0000729}
730
Dan Gohman475871a2008-07-27 21:46:04 +0000731SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000732LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000733{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000734 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000735 {
Akira Hatanaka2459afe2013-03-30 01:15:17 +0000736 case ISD::BR_JT: return lowerBR_JT(Op, DAG);
737 case ISD::BRCOND: return lowerBRCOND(Op, DAG);
738 case ISD::ConstantPool: return lowerConstantPool(Op, DAG);
739 case ISD::GlobalAddress: return lowerGlobalAddress(Op, DAG);
740 case ISD::BlockAddress: return lowerBlockAddress(Op, DAG);
741 case ISD::GlobalTLSAddress: return lowerGlobalTLSAddress(Op, DAG);
742 case ISD::JumpTable: return lowerJumpTable(Op, DAG);
743 case ISD::SELECT: return lowerSELECT(Op, DAG);
744 case ISD::SELECT_CC: return lowerSELECT_CC(Op, DAG);
745 case ISD::SETCC: return lowerSETCC(Op, DAG);
746 case ISD::VASTART: return lowerVASTART(Op, DAG);
747 case ISD::FCOPYSIGN: return lowerFCOPYSIGN(Op, DAG);
748 case ISD::FABS: return lowerFABS(Op, DAG);
749 case ISD::FRAMEADDR: return lowerFRAMEADDR(Op, DAG);
750 case ISD::RETURNADDR: return lowerRETURNADDR(Op, DAG);
751 case ISD::EH_RETURN: return lowerEH_RETURN(Op, DAG);
Akira Hatanaka2459afe2013-03-30 01:15:17 +0000752 case ISD::ATOMIC_FENCE: return lowerATOMIC_FENCE(Op, DAG);
753 case ISD::SHL_PARTS: return lowerShiftLeftParts(Op, DAG);
754 case ISD::SRA_PARTS: return lowerShiftRightParts(Op, DAG, true);
755 case ISD::SRL_PARTS: return lowerShiftRightParts(Op, DAG, false);
756 case ISD::LOAD: return lowerLOAD(Op, DAG);
757 case ISD::STORE: return lowerSTORE(Op, DAG);
Akira Hatanaka2459afe2013-03-30 01:15:17 +0000758 case ISD::ADD: return lowerADD(Op, DAG);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +0000759 case ISD::FP_TO_SINT: return lowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000760 }
Dan Gohman475871a2008-07-27 21:46:04 +0000761 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000762}
763
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000764//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000765// Lower helper functions
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000766//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000767
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000768// addLiveIn - This helper function adds the specified physical register to the
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000769// MachineFunction as a live in value. It also creates a corresponding
770// virtual register for it.
771static unsigned
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000772addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000773{
Chris Lattner84bc5422007-12-31 04:13:23 +0000774 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
775 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000776 return VReg;
777}
778
Akira Hatanakaf8941992013-05-20 18:07:43 +0000779static MachineBasicBlock *expandPseudoDIV(MachineInstr *MI,
780 MachineBasicBlock &MBB,
781 const TargetInstrInfo &TII,
782 bool Is64Bit) {
783 if (NoZeroDivCheck)
784 return &MBB;
785
786 // Insert instruction "teq $divisor_reg, $zero, 7".
787 MachineBasicBlock::iterator I(MI);
788 MachineInstrBuilder MIB;
789 MIB = BuildMI(MBB, llvm::next(I), MI->getDebugLoc(), TII.get(Mips::TEQ))
790 .addOperand(MI->getOperand(2)).addReg(Mips::ZERO).addImm(7);
791
792 // Use the 32-bit sub-register if this is a 64-bit division.
793 if (Is64Bit)
794 MIB->getOperand(0).setSubReg(Mips::sub_32);
795
796 return &MBB;
797}
798
Akira Hatanaka01f70892012-09-27 02:15:57 +0000799MachineBasicBlock *
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000800MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000801 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000802 switch (MI->getOpcode()) {
Reed Kotlerffbe4322013-02-21 04:22:38 +0000803 default:
804 llvm_unreachable("Unexpected instr type to insert");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000805 case Mips::ATOMIC_LOAD_ADD_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000806 return emitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000807 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000808 return emitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000809 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000810 return emitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000811 case Mips::ATOMIC_LOAD_ADD_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000812 return emitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000813
814 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000815 return emitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000816 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000817 return emitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000818 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000819 return emitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka59068062011-11-11 04:14:30 +0000820 case Mips::ATOMIC_LOAD_AND_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000821 return emitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000822
823 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000824 return emitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000825 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000826 return emitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000827 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000828 return emitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000829 case Mips::ATOMIC_LOAD_OR_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000830 return emitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000831
832 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000833 return emitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000834 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000835 return emitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000836 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000837 return emitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000838 case Mips::ATOMIC_LOAD_XOR_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000839 return emitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000840
841 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000842 return emitAtomicBinaryPartword(MI, BB, 1, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000843 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000844 return emitAtomicBinaryPartword(MI, BB, 2, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000845 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000846 return emitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka59068062011-11-11 04:14:30 +0000847 case Mips::ATOMIC_LOAD_NAND_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000848 return emitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000849
850 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000851 return emitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000852 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000853 return emitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000854 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000855 return emitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000856 case Mips::ATOMIC_LOAD_SUB_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000857 return emitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000858
859 case Mips::ATOMIC_SWAP_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000860 return emitAtomicBinaryPartword(MI, BB, 1, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000861 case Mips::ATOMIC_SWAP_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000862 return emitAtomicBinaryPartword(MI, BB, 2, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000863 case Mips::ATOMIC_SWAP_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000864 return emitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka59068062011-11-11 04:14:30 +0000865 case Mips::ATOMIC_SWAP_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000866 return emitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000867
868 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000869 return emitAtomicCmpSwapPartword(MI, BB, 1);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000870 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000871 return emitAtomicCmpSwapPartword(MI, BB, 2);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000872 case Mips::ATOMIC_CMP_SWAP_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000873 return emitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka59068062011-11-11 04:14:30 +0000874 case Mips::ATOMIC_CMP_SWAP_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000875 return emitAtomicCmpSwap(MI, BB, 8);
Akira Hatanakaf8941992013-05-20 18:07:43 +0000876 case Mips::PseudoSDIV:
877 case Mips::PseudoUDIV:
878 return expandPseudoDIV(MI, *BB, *getTargetMachine().getInstrInfo(), false);
879 case Mips::PseudoDSDIV:
880 case Mips::PseudoDUDIV:
881 return expandPseudoDIV(MI, *BB, *getTargetMachine().getInstrInfo(), true);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000882 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000883}
884
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000885// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
886// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
887MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000888MipsTargetLowering::emitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher471e4222011-06-08 23:55:35 +0000889 unsigned Size, unsigned BinOpcode,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000890 bool Nand) const {
Akira Hatanaka59068062011-11-11 04:14:30 +0000891 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000892
893 MachineFunction *MF = BB->getParent();
894 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +0000895 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000896 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000897 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +0000898 unsigned LL, SC, AND, NOR, ZERO, BEQ;
899
900 if (Size == 4) {
Akira Hatanakaa98a4862013-08-20 21:08:22 +0000901 LL = Mips::LL;
902 SC = Mips::SC;
Akira Hatanaka59068062011-11-11 04:14:30 +0000903 AND = Mips::AND;
904 NOR = Mips::NOR;
905 ZERO = Mips::ZERO;
906 BEQ = Mips::BEQ;
907 }
908 else {
Akira Hatanakaa98a4862013-08-20 21:08:22 +0000909 LL = Mips::LLD;
910 SC = Mips::SCD;
Akira Hatanaka59068062011-11-11 04:14:30 +0000911 AND = Mips::AND64;
912 NOR = Mips::NOR64;
913 ZERO = Mips::ZERO_64;
914 BEQ = Mips::BEQ64;
915 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000916
Akira Hatanaka4061da12011-07-19 20:11:17 +0000917 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000918 unsigned Ptr = MI->getOperand(1).getReg();
919 unsigned Incr = MI->getOperand(2).getReg();
920
Akira Hatanaka4061da12011-07-19 20:11:17 +0000921 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
922 unsigned AndRes = RegInfo.createVirtualRegister(RC);
923 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000924
925 // insert new blocks after the current block
926 const BasicBlock *LLVM_BB = BB->getBasicBlock();
927 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
928 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
929 MachineFunction::iterator It = BB;
930 ++It;
931 MF->insert(It, loopMBB);
932 MF->insert(It, exitMBB);
933
934 // Transfer the remainder of BB and its successor edges to exitMBB.
935 exitMBB->splice(exitMBB->begin(), BB,
936 llvm::next(MachineBasicBlock::iterator(MI)),
937 BB->end());
938 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
939
940 // thisMBB:
941 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000942 // fallthrough --> loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000943 BB->addSuccessor(loopMBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +0000944 loopMBB->addSuccessor(loopMBB);
945 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000946
947 // loopMBB:
948 // ll oldval, 0(ptr)
Akira Hatanaka4061da12011-07-19 20:11:17 +0000949 // <binop> storeval, oldval, incr
950 // sc success, storeval, 0(ptr)
951 // beq success, $0, loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000952 BB = loopMBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000953 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000954 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000955 // and andres, oldval, incr
956 // nor storeval, $0, andres
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000957 BuildMI(BB, DL, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
958 BuildMI(BB, DL, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000959 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000960 // <binop> storeval, oldval, incr
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000961 BuildMI(BB, DL, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000962 } else {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000963 StoreVal = Incr;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000964 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000965 BuildMI(BB, DL, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
966 BuildMI(BB, DL, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000967
968 MI->eraseFromParent(); // The instruction is gone now.
969
Akira Hatanaka939ece12011-07-19 03:42:13 +0000970 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000971}
972
973MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000974MipsTargetLowering::emitAtomicBinaryPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000975 MachineBasicBlock *BB,
976 unsigned Size, unsigned BinOpcode,
977 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000978 assert((Size == 1 || Size == 2) &&
979 "Unsupported size for EmitAtomicBinaryPartial.");
980
981 MachineFunction *MF = BB->getParent();
982 MachineRegisterInfo &RegInfo = MF->getRegInfo();
983 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
984 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000985 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000986
987 unsigned Dest = MI->getOperand(0).getReg();
988 unsigned Ptr = MI->getOperand(1).getReg();
989 unsigned Incr = MI->getOperand(2).getReg();
990
Akira Hatanaka4061da12011-07-19 20:11:17 +0000991 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
992 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000993 unsigned Mask = RegInfo.createVirtualRegister(RC);
994 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000995 unsigned NewVal = RegInfo.createVirtualRegister(RC);
996 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000997 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +0000998 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
999 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1000 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1001 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1002 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001003 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001004 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1005 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1006 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1007 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1008 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001009
1010 // insert new blocks after the current block
1011 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1012 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001013 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001014 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1015 MachineFunction::iterator It = BB;
1016 ++It;
1017 MF->insert(It, loopMBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001018 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001019 MF->insert(It, exitMBB);
1020
1021 // Transfer the remainder of BB and its successor edges to exitMBB.
1022 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001023 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001024 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1025
Akira Hatanaka81b44112011-07-19 17:09:53 +00001026 BB->addSuccessor(loopMBB);
1027 loopMBB->addSuccessor(loopMBB);
1028 loopMBB->addSuccessor(sinkMBB);
1029 sinkMBB->addSuccessor(exitMBB);
1030
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001031 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001032 // addiu masklsb2,$0,-4 # 0xfffffffc
1033 // and alignedaddr,ptr,masklsb2
1034 // andi ptrlsb2,ptr,3
1035 // sll shiftamt,ptrlsb2,3
1036 // ori maskupper,$0,255 # 0xff
1037 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001038 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001039 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001040
1041 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001042 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001043 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001044 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001045 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001046 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Akira Hatanakaaffed7e2013-05-31 03:25:44 +00001047 if (Subtarget->isLittle()) {
1048 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1049 } else {
1050 unsigned Off = RegInfo.createVirtualRegister(RC);
1051 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1052 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1053 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1054 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001055 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001056 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001057 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka51122432013-07-01 20:39:53 +00001058 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001059 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka51122432013-07-01 20:39:53 +00001060 BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(Incr).addReg(ShiftAmt);
Bruno Cardoso Lopescada2d02011-05-31 20:25:26 +00001061
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001062 // atomic.load.binop
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001063 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001064 // ll oldval,0(alignedaddr)
1065 // binop binopres,oldval,incr2
1066 // and newval,binopres,mask
1067 // and maskedoldval0,oldval,mask2
1068 // or storeval,maskedoldval0,newval
1069 // sc success,storeval,0(alignedaddr)
1070 // beq success,$0,loopMBB
1071
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001072 // atomic.swap
1073 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001074 // ll oldval,0(alignedaddr)
Akira Hatanaka70564a92011-07-19 18:14:26 +00001075 // and newval,incr2,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001076 // and maskedoldval0,oldval,mask2
1077 // or storeval,maskedoldval0,newval
1078 // sc success,storeval,0(alignedaddr)
1079 // beq success,$0,loopMBB
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001080
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001081 BB = loopMBB;
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001082 BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001083 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001084 // and andres, oldval, incr2
1085 // nor binopres, $0, andres
1086 // and newval, binopres, mask
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001087 BuildMI(BB, DL, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1088 BuildMI(BB, DL, TII->get(Mips::NOR), BinOpRes)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001089 .addReg(Mips::ZERO).addReg(AndRes);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001090 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001091 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001092 // <binop> binopres, oldval, incr2
1093 // and newval, binopres, mask
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001094 BuildMI(BB, DL, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1095 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001096 } else {// atomic.swap
Akira Hatanaka4061da12011-07-19 20:11:17 +00001097 // and newval, incr2, mask
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001098 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001099 }
Jia Liubb481f82012-02-28 07:46:26 +00001100
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001101 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001102 .addReg(OldVal).addReg(Mask2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001103 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001104 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001105 BuildMI(BB, DL, TII->get(Mips::SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001106 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001107 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001108 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001109
Akira Hatanaka939ece12011-07-19 03:42:13 +00001110 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001111 // and maskedoldval1,oldval,mask
1112 // srl srlres,maskedoldval1,shiftamt
1113 // sll sllres,srlres,24
1114 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001115 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001116 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001117
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001118 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001119 .addReg(OldVal).addReg(Mask);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001120 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka51122432013-07-01 20:39:53 +00001121 .addReg(MaskedOldVal1).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001122 BuildMI(BB, DL, TII->get(Mips::SLL), SllRes)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001123 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001124 BuildMI(BB, DL, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001125 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001126
1127 MI->eraseFromParent(); // The instruction is gone now.
1128
Akira Hatanaka939ece12011-07-19 03:42:13 +00001129 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001130}
1131
1132MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001133MipsTargetLowering::emitAtomicCmpSwap(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001134 MachineBasicBlock *BB,
1135 unsigned Size) const {
Akira Hatanaka59068062011-11-11 04:14:30 +00001136 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001137
1138 MachineFunction *MF = BB->getParent();
1139 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +00001140 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001141 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001142 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001143 unsigned LL, SC, ZERO, BNE, BEQ;
1144
1145 if (Size == 4) {
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001146 LL = Mips::LL;
1147 SC = Mips::SC;
Akira Hatanaka59068062011-11-11 04:14:30 +00001148 ZERO = Mips::ZERO;
1149 BNE = Mips::BNE;
1150 BEQ = Mips::BEQ;
1151 }
1152 else {
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001153 LL = Mips::LLD;
1154 SC = Mips::SCD;
Akira Hatanaka59068062011-11-11 04:14:30 +00001155 ZERO = Mips::ZERO_64;
1156 BNE = Mips::BNE64;
1157 BEQ = Mips::BEQ64;
1158 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001159
1160 unsigned Dest = MI->getOperand(0).getReg();
1161 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001162 unsigned OldVal = MI->getOperand(2).getReg();
1163 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001164
Akira Hatanaka4061da12011-07-19 20:11:17 +00001165 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001166
1167 // insert new blocks after the current block
1168 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1169 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1170 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1171 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1172 MachineFunction::iterator It = BB;
1173 ++It;
1174 MF->insert(It, loop1MBB);
1175 MF->insert(It, loop2MBB);
1176 MF->insert(It, exitMBB);
1177
1178 // Transfer the remainder of BB and its successor edges to exitMBB.
1179 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001180 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001181 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1182
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001183 // thisMBB:
1184 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001185 // fallthrough --> loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001186 BB->addSuccessor(loop1MBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +00001187 loop1MBB->addSuccessor(exitMBB);
1188 loop1MBB->addSuccessor(loop2MBB);
1189 loop2MBB->addSuccessor(loop1MBB);
1190 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001191
1192 // loop1MBB:
1193 // ll dest, 0(ptr)
1194 // bne dest, oldval, exitMBB
1195 BB = loop1MBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001196 BuildMI(BB, DL, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1197 BuildMI(BB, DL, TII->get(BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001198 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001199
1200 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001201 // sc success, newval, 0(ptr)
1202 // beq success, $0, loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001203 BB = loop2MBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001204 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001205 .addReg(NewVal).addReg(Ptr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001206 BuildMI(BB, DL, TII->get(BEQ))
Akira Hatanaka59068062011-11-11 04:14:30 +00001207 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001208
1209 MI->eraseFromParent(); // The instruction is gone now.
1210
Akira Hatanaka939ece12011-07-19 03:42:13 +00001211 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001212}
1213
1214MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001215MipsTargetLowering::emitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001216 MachineBasicBlock *BB,
1217 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001218 assert((Size == 1 || Size == 2) &&
1219 "Unsupported size for EmitAtomicCmpSwapPartial.");
1220
1221 MachineFunction *MF = BB->getParent();
1222 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1223 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1224 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001225 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001226
1227 unsigned Dest = MI->getOperand(0).getReg();
1228 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001229 unsigned CmpVal = MI->getOperand(2).getReg();
1230 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001231
Akira Hatanaka4061da12011-07-19 20:11:17 +00001232 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1233 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001234 unsigned Mask = RegInfo.createVirtualRegister(RC);
1235 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001236 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1237 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1238 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1239 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1240 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1241 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1242 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1243 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1244 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1245 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1246 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1247 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1248 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1249 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001250
1251 // insert new blocks after the current block
1252 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1253 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1254 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001255 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001256 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1257 MachineFunction::iterator It = BB;
1258 ++It;
1259 MF->insert(It, loop1MBB);
1260 MF->insert(It, loop2MBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001261 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001262 MF->insert(It, exitMBB);
1263
1264 // Transfer the remainder of BB and its successor edges to exitMBB.
1265 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001266 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001267 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1268
Akira Hatanaka81b44112011-07-19 17:09:53 +00001269 BB->addSuccessor(loop1MBB);
1270 loop1MBB->addSuccessor(sinkMBB);
1271 loop1MBB->addSuccessor(loop2MBB);
1272 loop2MBB->addSuccessor(loop1MBB);
1273 loop2MBB->addSuccessor(sinkMBB);
1274 sinkMBB->addSuccessor(exitMBB);
1275
Akira Hatanaka70564a92011-07-19 18:14:26 +00001276 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001277 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001278 // addiu masklsb2,$0,-4 # 0xfffffffc
1279 // and alignedaddr,ptr,masklsb2
1280 // andi ptrlsb2,ptr,3
1281 // sll shiftamt,ptrlsb2,3
1282 // ori maskupper,$0,255 # 0xff
1283 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001284 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001285 // andi maskedcmpval,cmpval,255
1286 // sll shiftedcmpval,maskedcmpval,shiftamt
1287 // andi maskednewval,newval,255
1288 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001289 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001290 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001291 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001292 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001293 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001294 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Akira Hatanakaaffed7e2013-05-31 03:25:44 +00001295 if (Subtarget->isLittle()) {
1296 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1297 } else {
1298 unsigned Off = RegInfo.createVirtualRegister(RC);
1299 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1300 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1301 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1302 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001303 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001304 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001305 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka51122432013-07-01 20:39:53 +00001306 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001307 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1308 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001309 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001310 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal)
Akira Hatanaka51122432013-07-01 20:39:53 +00001311 .addReg(MaskedCmpVal).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001312 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001313 .addReg(NewVal).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001314 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal)
Akira Hatanaka51122432013-07-01 20:39:53 +00001315 .addReg(MaskedNewVal).addReg(ShiftAmt);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001316
1317 // loop1MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001318 // ll oldval,0(alginedaddr)
1319 // and maskedoldval0,oldval,mask
1320 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001321 BB = loop1MBB;
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001322 BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001323 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001324 .addReg(OldVal).addReg(Mask);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001325 BuildMI(BB, DL, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001326 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001327
1328 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001329 // and maskedoldval1,oldval,mask2
1330 // or storeval,maskedoldval1,shiftednewval
1331 // sc success,storeval,0(alignedaddr)
1332 // beq success,$0,loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001333 BB = loop2MBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001334 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001335 .addReg(OldVal).addReg(Mask2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001336 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001337 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001338 BuildMI(BB, DL, TII->get(Mips::SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001339 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001340 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001341 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001342
Akira Hatanaka939ece12011-07-19 03:42:13 +00001343 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001344 // srl srlres,maskedoldval0,shiftamt
1345 // sll sllres,srlres,24
1346 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001347 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001348 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001349
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001350 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka51122432013-07-01 20:39:53 +00001351 .addReg(MaskedOldVal0).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001352 BuildMI(BB, DL, TII->get(Mips::SLL), SllRes)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001353 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001354 BuildMI(BB, DL, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001355 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001356
1357 MI->eraseFromParent(); // The instruction is gone now.
1358
Akira Hatanaka939ece12011-07-19 03:42:13 +00001359 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001360}
1361
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001362//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001363// Misc Lower Operation implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001364//===----------------------------------------------------------------------===//
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001365SDValue MipsTargetLowering::lowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanakab7656a92013-03-06 21:32:03 +00001366 SDValue Chain = Op.getOperand(0);
1367 SDValue Table = Op.getOperand(1);
1368 SDValue Index = Op.getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001369 SDLoc DL(Op);
Akira Hatanakab7656a92013-03-06 21:32:03 +00001370 EVT PTy = getPointerTy();
1371 unsigned EntrySize =
1372 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(*getDataLayout());
1373
1374 Index = DAG.getNode(ISD::MUL, DL, PTy, Index,
1375 DAG.getConstant(EntrySize, PTy));
1376 SDValue Addr = DAG.getNode(ISD::ADD, DL, PTy, Index, Table);
1377
1378 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
1379 Addr = DAG.getExtLoad(ISD::SEXTLOAD, DL, PTy, Chain, Addr,
1380 MachinePointerInfo::getJumpTable(), MemVT, false, false,
1381 0);
1382 Chain = Addr.getValue(1);
1383
1384 if ((getTargetMachine().getRelocationModel() == Reloc::PIC_) || IsN64) {
1385 // For PIC, the sequence is:
1386 // BRIND(load(Jumptable + index) + RelocBase)
1387 // RelocBase can be JumpTable, GOT or some sort of global base.
1388 Addr = DAG.getNode(ISD::ADD, DL, PTy, Addr,
1389 getPICJumpTableRelocBase(Table, DAG));
1390 }
1391
1392 return DAG.getNode(ISD::BRIND, DL, MVT::Other, Chain, Addr);
1393}
1394
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +00001395SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001396lowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001397{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001398 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001399 // the block to branch to if the condition is true.
1400 SDValue Chain = Op.getOperand(0);
1401 SDValue Dest = Op.getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001402 SDLoc DL(Op);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001403
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001404 SDValue CondRes = createFPCmp(DAG, Op.getOperand(1));
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001405
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001406 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001407 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +00001408 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001409
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +00001410 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001411 Mips::CondCode CC =
1412 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Akira Hatanaka9cf07242013-03-30 01:16:38 +00001413 unsigned Opc = invertFPCondCodeUser(CC) ? Mips::BRANCH_F : Mips::BRANCH_T;
1414 SDValue BrCode = DAG.getConstant(Opc, MVT::i32);
Akira Hatanaka83d8ef12013-07-26 20:13:47 +00001415 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001416 return DAG.getNode(MipsISD::FPBrcond, DL, Op.getValueType(), Chain, BrCode,
Akira Hatanaka83d8ef12013-07-26 20:13:47 +00001417 FCC0, Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001418}
1419
1420SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001421lowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001422{
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001423 SDValue Cond = createFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001424
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001425 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001426 if (Cond.getOpcode() != MipsISD::FPCmp)
1427 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +00001428
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001429 return createCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001430 SDLoc(Op));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001431}
1432
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001433SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001434lowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001435{
Andrew Trickac6d9be2013-05-25 02:42:55 +00001436 SDLoc DL(Op);
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001437 EVT Ty = Op.getOperand(0).getValueType();
Matt Arsenault225ed702013-05-18 00:21:46 +00001438 SDValue Cond = DAG.getNode(ISD::SETCC, DL,
1439 getSetCCResultType(*DAG.getContext(), Ty),
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001440 Op.getOperand(0), Op.getOperand(1),
1441 Op.getOperand(4));
1442
1443 return DAG.getNode(ISD::SELECT, DL, Op.getValueType(), Cond, Op.getOperand(2),
1444 Op.getOperand(3));
1445}
1446
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001447SDValue MipsTargetLowering::lowerSETCC(SDValue Op, SelectionDAG &DAG) const {
1448 SDValue Cond = createFPCmp(DAG, Op);
Akira Hatanaka0a40c232012-03-09 23:46:03 +00001449
1450 assert(Cond.getOpcode() == MipsISD::FPCmp &&
1451 "Floating point operand expected.");
1452
1453 SDValue True = DAG.getConstant(1, MVT::i32);
1454 SDValue False = DAG.getConstant(0, MVT::i32);
1455
Andrew Trickac6d9be2013-05-25 02:42:55 +00001456 return createCMovFP(DAG, Cond, True, False, SDLoc(Op));
Akira Hatanaka0a40c232012-03-09 23:46:03 +00001457}
1458
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001459SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001460 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +00001461 // FIXME there isn't actually debug info here
Andrew Trickac6d9be2013-05-25 02:42:55 +00001462 SDLoc DL(Op);
Jia Liubb481f82012-02-28 07:46:26 +00001463 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001464
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001465 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
Akira Hatanakaafc945b2012-09-12 23:27:55 +00001466 const MipsTargetObjectFile &TLOF =
1467 (const MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001468
Chris Lattnere3736f82009-08-13 05:41:27 +00001469 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001470 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001471 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001472 MipsII::MO_GPREL);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001473 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, DL,
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001474 DAG.getVTList(MVT::i32), &GA, 1);
Akira Hatanakae7338cd2012-08-22 03:18:13 +00001475 SDValue GPReg = DAG.getRegister(Mips::GP, MVT::i32);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001476 return DAG.getNode(ISD::ADD, DL, MVT::i32, GPReg, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +00001477 }
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001478
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001479 // %hi/%lo relocation
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001480 return getAddrNonPIC(Op, DAG);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001481 }
1482
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001483 if (GV->hasInternalLinkage() || (GV->hasLocalLinkage() && !isa<Function>(GV)))
1484 return getAddrLocal(Op, DAG, HasMips64);
1485
Akira Hatanakaf09a0372012-11-21 20:40:38 +00001486 if (LargeGOT)
1487 return getAddrGlobalLargeGOT(Op, DAG, MipsII::MO_GOT_HI16,
1488 MipsII::MO_GOT_LO16);
1489
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001490 return getAddrGlobal(Op, DAG,
1491 HasMips64 ? MipsII::MO_GOT_DISP : MipsII::MO_GOT16);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001492}
1493
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001494SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op,
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001495 SelectionDAG &DAG) const {
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001496 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
1497 return getAddrNonPIC(Op, DAG);
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001498
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001499 return getAddrLocal(Op, DAG, HasMips64);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001500}
1501
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001502SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001503lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001504{
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001505 // If the relocation model is PIC, use the General Dynamic TLS Model or
1506 // Local Dynamic TLS model, otherwise use the Initial Exec or
1507 // Local Exec TLS Model.
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001508
1509 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001510 SDLoc DL(GA);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001511 const GlobalValue *GV = GA->getGlobal();
1512 EVT PtrVT = getPointerTy();
1513
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001514 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
1515
1516 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Hans Wennborg70a07c72012-06-04 14:02:08 +00001517 // General Dynamic and Local Dynamic TLS Model.
1518 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
1519 : MipsII::MO_TLSGD;
1520
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001521 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag);
1522 SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT,
1523 getGlobalReg(DAG, PtrVT), TGA);
Akira Hatanaka7a7194b2011-12-08 21:05:38 +00001524 unsigned PtrSize = PtrVT.getSizeInBits();
1525 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1526
Benjamin Kramer5eccf672011-12-11 12:21:34 +00001527 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001528
1529 ArgListTy Args;
1530 ArgListEntry Entry;
1531 Entry.Node = Argument;
Akira Hatanakaca074792011-12-08 20:34:32 +00001532 Entry.Ty = PtrTy;
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001533 Args.push_back(Entry);
Jia Liubb481f82012-02-28 07:46:26 +00001534
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001535 TargetLowering::CallLoweringInfo CLI(DAG.getEntryNode(), PtrTy,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001536 false, false, false, false, 0, CallingConv::C,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001537 /*IsTailCall=*/false, /*doesNotRet=*/false,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001538 /*isReturnValueUsed=*/true,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001539 TlsGetAddr, Args, DAG, DL);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001540 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001541
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001542 SDValue Ret = CallResult.first;
1543
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001544 if (model != TLSModel::LocalDynamic)
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001545 return Ret;
1546
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001547 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001548 MipsII::MO_DTPREL_HI);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001549 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1550 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001551 MipsII::MO_DTPREL_LO);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001552 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1553 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret);
1554 return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001555 }
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001556
1557 SDValue Offset;
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001558 if (model == TLSModel::InitialExec) {
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001559 // Initial Exec TLS Model
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001560 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001561 MipsII::MO_GOTTPREL);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001562 TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT),
Akira Hatanaka648f00c2012-02-24 22:34:47 +00001563 TGA);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001564 Offset = DAG.getLoad(PtrVT, DL,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001565 DAG.getEntryNode(), TGA, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001566 false, false, false, 0);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001567 } else {
1568 // Local Exec TLS Model
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001569 assert(model == TLSModel::LocalExec);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001570 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001571 MipsII::MO_TPREL_HI);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001572 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001573 MipsII::MO_TPREL_LO);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001574 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1575 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1576 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001577 }
1578
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001579 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
1580 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001581}
1582
1583SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001584lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001585{
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001586 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
1587 return getAddrNonPIC(Op, DAG);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001588
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001589 return getAddrLocal(Op, DAG, HasMips64);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001590}
1591
Dan Gohman475871a2008-07-27 21:46:04 +00001592SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001593lowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001594{
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001595 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001596 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001597 // but the asm printer currently doesn't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001598 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +00001599 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +00001600 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001601 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1602 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001603 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001604
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001605 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
1606 return getAddrNonPIC(Op, DAG);
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001607
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001608 return getAddrLocal(Op, DAG, HasMips64);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001609}
1610
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001611SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001612 MachineFunction &MF = DAG.getMachineFunction();
1613 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1614
Andrew Trickac6d9be2013-05-25 02:42:55 +00001615 SDLoc DL(Op);
Dan Gohman1e93df62010-04-17 14:41:14 +00001616 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1617 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001618
1619 // vastart just stores the address of the VarArgsFrameIndex slot into the
1620 // memory location argument.
1621 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001622 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
Akira Hatanaka82099682011-12-19 19:52:25 +00001623 MachinePointerInfo(SV), false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001624}
Jia Liubb481f82012-02-28 07:46:26 +00001625
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001626static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001627 EVT TyX = Op.getOperand(0).getValueType();
1628 EVT TyY = Op.getOperand(1).getValueType();
1629 SDValue Const1 = DAG.getConstant(1, MVT::i32);
1630 SDValue Const31 = DAG.getConstant(31, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001631 SDLoc DL(Op);
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001632 SDValue Res;
1633
1634 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1635 // to i32.
1636 SDValue X = (TyX == MVT::f32) ?
1637 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1638 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1639 Const1);
1640 SDValue Y = (TyY == MVT::f32) ?
1641 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
1642 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
1643 Const1);
1644
1645 if (HasR2) {
1646 // ext E, Y, 31, 1 ; extract bit31 of Y
1647 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
1648 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
1649 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
1650 } else {
1651 // sll SllX, X, 1
1652 // srl SrlX, SllX, 1
1653 // srl SrlY, Y, 31
1654 // sll SllY, SrlX, 31
1655 // or Or, SrlX, SllY
1656 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1657 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1658 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
1659 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
1660 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
1661 }
1662
1663 if (TyX == MVT::f32)
1664 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
1665
1666 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1667 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1668 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001669}
1670
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001671static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001672 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
1673 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
1674 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
1675 SDValue Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001676 SDLoc DL(Op);
Eric Christopher471e4222011-06-08 23:55:35 +00001677
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001678 // Bitcast to integer nodes.
1679 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
1680 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001681
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001682 if (HasR2) {
1683 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
1684 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
1685 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
1686 DAG.getConstant(WidthY - 1, MVT::i32), Const1);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001687
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001688 if (WidthX > WidthY)
1689 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
1690 else if (WidthY > WidthX)
1691 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001692
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001693 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
1694 DAG.getConstant(WidthX - 1, MVT::i32), Const1, X);
1695 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
1696 }
1697
1698 // (d)sll SllX, X, 1
1699 // (d)srl SrlX, SllX, 1
1700 // (d)srl SrlY, Y, width(Y)-1
1701 // (d)sll SllY, SrlX, width(Y)-1
1702 // or Or, SrlX, SllY
1703 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
1704 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
1705 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
1706 DAG.getConstant(WidthY - 1, MVT::i32));
1707
1708 if (WidthX > WidthY)
1709 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
1710 else if (WidthY > WidthX)
1711 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
1712
1713 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
1714 DAG.getConstant(WidthX - 1, MVT::i32));
1715 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
1716 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001717}
1718
Akira Hatanaka82099682011-12-19 19:52:25 +00001719SDValue
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001720MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001721 if (Subtarget->hasMips64())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001722 return lowerFCOPYSIGN64(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001723
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001724 return lowerFCOPYSIGN32(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001725}
1726
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001727static SDValue lowerFABS32(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001728 SDValue Res, Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001729 SDLoc DL(Op);
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001730
1731 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1732 // to i32.
1733 SDValue X = (Op.getValueType() == MVT::f32) ?
1734 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1735 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1736 Const1);
1737
1738 // Clear MSB.
1739 if (HasR2)
1740 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32,
1741 DAG.getRegister(Mips::ZERO, MVT::i32),
1742 DAG.getConstant(31, MVT::i32), Const1, X);
1743 else {
1744 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1745 Res = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1746 }
1747
1748 if (Op.getValueType() == MVT::f32)
1749 return DAG.getNode(ISD::BITCAST, DL, MVT::f32, Res);
1750
1751 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1752 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1753 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
1754}
1755
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001756static SDValue lowerFABS64(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001757 SDValue Res, Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001758 SDLoc DL(Op);
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001759
1760 // Bitcast to integer node.
1761 SDValue X = DAG.getNode(ISD::BITCAST, DL, MVT::i64, Op.getOperand(0));
1762
1763 // Clear MSB.
1764 if (HasR2)
1765 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i64,
1766 DAG.getRegister(Mips::ZERO_64, MVT::i64),
1767 DAG.getConstant(63, MVT::i32), Const1, X);
1768 else {
1769 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i64, X, Const1);
1770 Res = DAG.getNode(ISD::SRL, DL, MVT::i64, SllX, Const1);
1771 }
1772
1773 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, Res);
1774}
1775
1776SDValue
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001777MipsTargetLowering::lowerFABS(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001778 if (Subtarget->hasMips64() && (Op.getValueType() == MVT::f64))
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001779 return lowerFABS64(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001780
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001781 return lowerFABS32(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001782}
1783
Akira Hatanaka2e591472011-06-02 00:24:44 +00001784SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001785lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopese0b5cfc2011-06-16 00:40:02 +00001786 // check the depth
1787 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka0f843822011-06-07 18:58:42 +00001788 "Frame address can only be determined for current frame.");
Akira Hatanaka2e591472011-06-02 00:24:44 +00001789
1790 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1791 MFI->setFrameAddressIsTaken(true);
1792 EVT VT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001793 SDLoc DL(Op);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001794 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), DL,
Akira Hatanaka46ac4392011-11-11 04:11:56 +00001795 IsN64 ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka2e591472011-06-02 00:24:44 +00001796 return FrameAddr;
1797}
1798
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001799SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op,
Akira Hatanakaba584fe2012-07-11 00:53:32 +00001800 SelectionDAG &DAG) const {
1801 // check the depth
1802 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
1803 "Return address can be determined only for current frame.");
1804
1805 MachineFunction &MF = DAG.getMachineFunction();
1806 MachineFrameInfo *MFI = MF.getFrameInfo();
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00001807 MVT VT = Op.getSimpleValueType();
Akira Hatanakaba584fe2012-07-11 00:53:32 +00001808 unsigned RA = IsN64 ? Mips::RA_64 : Mips::RA;
1809 MFI->setReturnAddressIsTaken(true);
1810
1811 // Return RA, which contains the return address. Mark it an implicit live-in.
1812 unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00001813 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT);
Akira Hatanakaba584fe2012-07-11 00:53:32 +00001814}
1815
Akira Hatanaka544cc212013-01-30 00:26:49 +00001816// An EH_RETURN is the result of lowering llvm.eh.return which in turn is
1817// generated from __builtin_eh_return (offset, handler)
1818// The effect of this is to adjust the stack pointer by "offset"
1819// and then branch to "handler".
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001820SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Akira Hatanaka544cc212013-01-30 00:26:49 +00001821 const {
1822 MachineFunction &MF = DAG.getMachineFunction();
1823 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1824
1825 MipsFI->setCallsEhReturn();
1826 SDValue Chain = Op.getOperand(0);
1827 SDValue Offset = Op.getOperand(1);
1828 SDValue Handler = Op.getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001829 SDLoc DL(Op);
Akira Hatanaka544cc212013-01-30 00:26:49 +00001830 EVT Ty = IsN64 ? MVT::i64 : MVT::i32;
1831
1832 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
1833 // EH_RETURN nodes, so that instructions are emitted back-to-back.
1834 unsigned OffsetReg = IsN64 ? Mips::V1_64 : Mips::V1;
1835 unsigned AddrReg = IsN64 ? Mips::V0_64 : Mips::V0;
1836 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
1837 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
1838 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
1839 DAG.getRegister(OffsetReg, Ty),
1840 DAG.getRegister(AddrReg, getPointerTy()),
1841 Chain.getValue(1));
1842}
1843
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001844SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
Akira Hatanaka864f6602012-06-14 21:10:56 +00001845 SelectionDAG &DAG) const {
Eli Friedman14648462011-07-27 22:21:52 +00001846 // FIXME: Need pseudo-fence for 'singlethread' fences
1847 // FIXME: Set SType for weaker fences where supported/appropriate.
1848 unsigned SType = 0;
Andrew Trickac6d9be2013-05-25 02:42:55 +00001849 SDLoc DL(Op);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001850 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00001851 DAG.getConstant(SType, MVT::i32));
1852}
1853
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001854SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
Akira Hatanaka864f6602012-06-14 21:10:56 +00001855 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001856 SDLoc DL(Op);
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001857 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
1858 SDValue Shamt = Op.getOperand(2);
1859
1860 // if shamt < 32:
1861 // lo = (shl lo, shamt)
1862 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
1863 // else:
1864 // lo = 0
1865 // hi = (shl lo, shamt[4:0])
1866 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
1867 DAG.getConstant(-1, MVT::i32));
1868 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo,
1869 DAG.getConstant(1, MVT::i32));
1870 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, ShiftRight1Lo,
1871 Not);
1872 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi, Shamt);
1873 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
1874 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, MVT::i32, Lo, Shamt);
1875 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
1876 DAG.getConstant(0x20, MVT::i32));
Akira Hatanaka864f6602012-06-14 21:10:56 +00001877 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
1878 DAG.getConstant(0, MVT::i32), ShiftLeftLo);
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001879 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftLeftLo, Or);
1880
1881 SDValue Ops[2] = {Lo, Hi};
1882 return DAG.getMergeValues(Ops, 2, DL);
1883}
1884
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001885SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001886 bool IsSRA) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001887 SDLoc DL(Op);
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001888 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
1889 SDValue Shamt = Op.getOperand(2);
1890
1891 // if shamt < 32:
1892 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
1893 // if isSRA:
1894 // hi = (sra hi, shamt)
1895 // else:
1896 // hi = (srl hi, shamt)
1897 // else:
1898 // if isSRA:
1899 // lo = (sra hi, shamt[4:0])
1900 // hi = (sra hi, 31)
1901 // else:
1902 // lo = (srl hi, shamt[4:0])
1903 // hi = 0
1904 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
1905 DAG.getConstant(-1, MVT::i32));
1906 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi,
1907 DAG.getConstant(1, MVT::i32));
1908 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, ShiftLeft1Hi, Not);
1909 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo, Shamt);
1910 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
1911 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL, DL, MVT::i32,
1912 Hi, Shamt);
1913 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
1914 DAG.getConstant(0x20, MVT::i32));
1915 SDValue Shift31 = DAG.getNode(ISD::SRA, DL, MVT::i32, Hi,
1916 DAG.getConstant(31, MVT::i32));
1917 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftRightHi, Or);
1918 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
1919 IsSRA ? Shift31 : DAG.getConstant(0, MVT::i32),
1920 ShiftRightHi);
1921
1922 SDValue Ops[2] = {Lo, Hi};
1923 return DAG.getMergeValues(Ops, 2, DL);
1924}
1925
Akira Hatanakafee62c12013-04-11 19:07:14 +00001926static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001927 SDValue Chain, SDValue Src, unsigned Offset) {
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00001928 SDValue Ptr = LD->getBasePtr();
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001929 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00001930 EVT BasePtrVT = Ptr.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001931 SDLoc DL(LD);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001932 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
1933
1934 if (Offset)
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00001935 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001936 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001937
1938 SDValue Ops[] = { Chain, Ptr, Src };
1939 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT,
1940 LD->getMemOperand());
1941}
1942
1943// Expand an unaligned 32 or 64-bit integer load node.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001944SDValue MipsTargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001945 LoadSDNode *LD = cast<LoadSDNode>(Op);
1946 EVT MemVT = LD->getMemoryVT();
1947
1948 // Return if load is aligned or if MemVT is neither i32 nor i64.
1949 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
1950 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
1951 return SDValue();
1952
1953 bool IsLittle = Subtarget->isLittle();
1954 EVT VT = Op.getValueType();
1955 ISD::LoadExtType ExtType = LD->getExtensionType();
1956 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
1957
1958 assert((VT == MVT::i32) || (VT == MVT::i64));
1959
1960 // Expand
1961 // (set dst, (i64 (load baseptr)))
1962 // to
1963 // (set tmp, (ldl (add baseptr, 7), undef))
1964 // (set dst, (ldr baseptr, tmp))
1965 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
Akira Hatanakafee62c12013-04-11 19:07:14 +00001966 SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001967 IsLittle ? 7 : 0);
Akira Hatanakafee62c12013-04-11 19:07:14 +00001968 return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001969 IsLittle ? 0 : 7);
1970 }
1971
Akira Hatanakafee62c12013-04-11 19:07:14 +00001972 SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001973 IsLittle ? 3 : 0);
Akira Hatanakafee62c12013-04-11 19:07:14 +00001974 SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001975 IsLittle ? 0 : 3);
1976
1977 // Expand
1978 // (set dst, (i32 (load baseptr))) or
1979 // (set dst, (i64 (sextload baseptr))) or
1980 // (set dst, (i64 (extload baseptr)))
1981 // to
1982 // (set tmp, (lwl (add baseptr, 3), undef))
1983 // (set dst, (lwr baseptr, tmp))
1984 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
1985 (ExtType == ISD::EXTLOAD))
1986 return LWR;
1987
1988 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
1989
1990 // Expand
1991 // (set dst, (i64 (zextload baseptr)))
1992 // to
1993 // (set tmp0, (lwl (add baseptr, 3), undef))
1994 // (set tmp1, (lwr baseptr, tmp0))
1995 // (set tmp2, (shl tmp1, 32))
1996 // (set dst, (srl tmp2, 32))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001997 SDLoc DL(LD);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001998 SDValue Const32 = DAG.getConstant(32, MVT::i32);
1999 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
Akira Hatanaka94ccee22012-06-04 17:46:29 +00002000 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
2001 SDValue Ops[] = { SRL, LWR.getValue(1) };
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002002 return DAG.getMergeValues(Ops, 2, DL);
2003}
2004
Akira Hatanakafee62c12013-04-11 19:07:14 +00002005static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002006 SDValue Chain, unsigned Offset) {
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00002007 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
2008 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00002009 SDLoc DL(SD);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002010 SDVTList VTList = DAG.getVTList(MVT::Other);
2011
2012 if (Offset)
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00002013 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002014 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002015
2016 SDValue Ops[] = { Chain, Value, Ptr };
2017 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT,
2018 SD->getMemOperand());
2019}
2020
2021// Expand an unaligned 32 or 64-bit integer store node.
Akira Hatanaka63451432013-05-16 20:45:17 +00002022static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG,
2023 bool IsLittle) {
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002024 SDValue Value = SD->getValue(), Chain = SD->getChain();
2025 EVT VT = Value.getValueType();
2026
2027 // Expand
2028 // (store val, baseptr) or
2029 // (truncstore val, baseptr)
2030 // to
2031 // (swl val, (add baseptr, 3))
2032 // (swr val, baseptr)
2033 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
Akira Hatanakafee62c12013-04-11 19:07:14 +00002034 SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002035 IsLittle ? 3 : 0);
Akira Hatanakafee62c12013-04-11 19:07:14 +00002036 return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002037 }
2038
2039 assert(VT == MVT::i64);
2040
2041 // Expand
2042 // (store val, baseptr)
2043 // to
2044 // (sdl val, (add baseptr, 7))
2045 // (sdr val, baseptr)
Akira Hatanakafee62c12013-04-11 19:07:14 +00002046 SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2047 return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002048}
2049
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002050// Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr).
2051static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG) {
2052 SDValue Val = SD->getValue();
2053
2054 if (Val.getOpcode() != ISD::FP_TO_SINT)
2055 return SDValue();
2056
2057 EVT FPTy = EVT::getFloatingPointVT(Val.getValueSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002058 SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy,
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002059 Val.getOperand(0));
2060
Andrew Trickac6d9be2013-05-25 02:42:55 +00002061 return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(),
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002062 SD->getPointerInfo(), SD->isVolatile(),
2063 SD->isNonTemporal(), SD->getAlignment());
2064}
2065
Akira Hatanaka63451432013-05-16 20:45:17 +00002066SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2067 StoreSDNode *SD = cast<StoreSDNode>(Op);
2068 EVT MemVT = SD->getMemoryVT();
2069
2070 // Lower unaligned integer stores.
2071 if ((SD->getAlignment() < MemVT.getSizeInBits() / 8) &&
2072 ((MemVT == MVT::i32) || (MemVT == MVT::i64)))
2073 return lowerUnalignedIntStore(SD, DAG, Subtarget->isLittle());
2074
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002075 return lowerFP_TO_SINT_STORE(SD, DAG);
Akira Hatanaka63451432013-05-16 20:45:17 +00002076}
2077
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002078SDValue MipsTargetLowering::lowerADD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanakae90a3bc2012-11-07 19:10:58 +00002079 if (Op->getOperand(0).getOpcode() != ISD::FRAMEADDR
2080 || cast<ConstantSDNode>
2081 (Op->getOperand(0).getOperand(0))->getZExtValue() != 0
2082 || Op->getOperand(1).getOpcode() != ISD::FRAME_TO_ARGS_OFFSET)
2083 return SDValue();
2084
2085 // The pattern
2086 // (add (frameaddr 0), (frame_to_args_offset))
2087 // results from lowering llvm.eh.dwarf.cfa intrinsic. Transform it to
2088 // (add FrameObject, 0)
2089 // where FrameObject is a fixed StackObject with offset 0 which points to
2090 // the old stack pointer.
2091 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2092 EVT ValTy = Op->getValueType(0);
2093 int FI = MFI->CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
2094 SDValue InArgsAddr = DAG.getFrameIndex(FI, ValTy);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002095 return DAG.getNode(ISD::ADD, SDLoc(Op), ValTy, InArgsAddr,
Akira Hatanakae90a3bc2012-11-07 19:10:58 +00002096 DAG.getConstant(0, ValTy));
2097}
2098
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002099SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op,
2100 SelectionDAG &DAG) const {
2101 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002102 SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy,
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002103 Op.getOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00002104 return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002105}
2106
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002107//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002108// Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002109//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002110
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002111//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002112// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002113// Mips O32 ABI rules:
2114// ---
2115// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002116// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002117// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002118// f64 - Only passed in two aliased f32 registers if no int reg has been used
2119// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002120// not used, it must be shadowed. If only A3 is avaiable, shadow it and
2121// go to stack.
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002122//
2123// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002124//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002125
Duncan Sands1e96bab2010-11-04 10:49:57 +00002126static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +00002127 MVT LocVT, CCValAssign::LocInfo LocInfo,
Akira Hatanakaad341d42013-08-20 23:38:40 +00002128 ISD::ArgFlagsTy ArgFlags, CCState &State,
2129 const uint16_t *F64Regs) {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002130
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002131 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002132
Craig Topperc5eaae42012-03-11 07:57:25 +00002133 static const uint16_t IntRegs[] = {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002134 Mips::A0, Mips::A1, Mips::A2, Mips::A3
2135 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002136 static const uint16_t F32Regs[] = {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002137 Mips::F12, Mips::F14
2138 };
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002139
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002140 // Do not process byval args here.
2141 if (ArgFlags.isByVal())
2142 return true;
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002143
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002144 // Promote i8 and i16
2145 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
2146 LocVT = MVT::i32;
2147 if (ArgFlags.isSExt())
2148 LocInfo = CCValAssign::SExt;
2149 else if (ArgFlags.isZExt())
2150 LocInfo = CCValAssign::ZExt;
2151 else
2152 LocInfo = CCValAssign::AExt;
2153 }
2154
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002155 unsigned Reg;
2156
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002157 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
2158 // is true: function is vararg, argument is 3rd or higher, there is previous
2159 // argument which is not f32 or f64.
2160 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
2161 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00002162 unsigned OrigAlign = ArgFlags.getOrigAlign();
2163 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002164
2165 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002166 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00002167 // If this is the first part of an i64 arg,
2168 // the allocated register must be either A0 or A2.
2169 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
2170 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002171 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002172 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
2173 // Allocate int register and shadow next int register. If first
2174 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002175 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2176 if (Reg == Mips::A1 || Reg == Mips::A3)
2177 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2178 State.AllocateReg(IntRegs, IntRegsSize);
2179 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002180 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
2181 // we are guaranteed to find an available float register
2182 if (ValVT == MVT::f32) {
2183 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
2184 // Shadow int register
2185 State.AllocateReg(IntRegs, IntRegsSize);
2186 } else {
2187 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
2188 // Shadow int registers
2189 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
2190 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
2191 State.AllocateReg(IntRegs, IntRegsSize);
2192 State.AllocateReg(IntRegs, IntRegsSize);
2193 }
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002194 } else
2195 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002196
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002197 if (!Reg) {
2198 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3,
2199 OrigAlign);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002200 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002201 } else
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002202 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002203
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002204 return false;
Akira Hatanaka2c5d6522011-11-12 02:20:46 +00002205}
2206
Akira Hatanakaad341d42013-08-20 23:38:40 +00002207static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT,
2208 MVT LocVT, CCValAssign::LocInfo LocInfo,
2209 ISD::ArgFlagsTy ArgFlags, CCState &State) {
2210 static const uint16_t F64Regs[] = { Mips::D6, Mips::D7 };
2211
2212 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2213}
2214
2215static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT,
2216 MVT LocVT, CCValAssign::LocInfo LocInfo,
2217 ISD::ArgFlagsTy ArgFlags, CCState &State) {
2218 static const uint16_t F64Regs[] = { Mips::D12_64, Mips::D12_64 };
2219
2220 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2221}
2222
Akira Hatanaka2c5d6522011-11-12 02:20:46 +00002223#include "MipsGenCallingConv.inc"
2224
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002225//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002226// Call Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002227//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002228
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002229// Return next O32 integer argument register.
2230static unsigned getNextIntArgReg(unsigned Reg) {
2231 assert((Reg == Mips::A0) || (Reg == Mips::A2));
2232 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
2233}
2234
Akira Hatanaka7d712092012-10-30 19:23:25 +00002235SDValue
2236MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002237 SDValue Chain, SDValue Arg, SDLoc DL,
Akira Hatanaka7d712092012-10-30 19:23:25 +00002238 bool IsTailCall, SelectionDAG &DAG) const {
2239 if (!IsTailCall) {
2240 SDValue PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr,
2241 DAG.getIntPtrConstant(Offset));
2242 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo(), false,
2243 false, 0);
2244 }
2245
2246 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2247 int FI = MFI->CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
2248 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2249 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(),
2250 /*isVolatile=*/ true, false, 0);
2251}
2252
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002253void MipsTargetLowering::
2254getOpndList(SmallVectorImpl<SDValue> &Ops,
2255 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
2256 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
2257 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
2258 // Insert node "GP copy globalreg" before call to function.
2259 //
2260 // R_MIPS_CALL* operators (emitted when non-internal functions are called
2261 // in PIC mode) allow symbols to be resolved via lazy binding.
2262 // The lazy binding stub requires GP to point to the GOT.
2263 if (IsPICCall && !InternalLinkage) {
2264 unsigned GPReg = IsN64 ? Mips::GP_64 : Mips::GP;
2265 EVT Ty = IsN64 ? MVT::i64 : MVT::i32;
2266 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty)));
2267 }
Reed Kotler8453b3f2013-01-24 04:24:02 +00002268
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002269 // Build a sequence of copy-to-reg nodes chained together with token
2270 // chain and flag operands which copy the outgoing args into registers.
2271 // The InFlag in necessary since all emitted instructions must be
2272 // stuck together.
2273 SDValue InFlag;
Reed Kotler8453b3f2013-01-24 04:24:02 +00002274
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002275 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2276 Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, RegsToPass[i].first,
2277 RegsToPass[i].second, InFlag);
2278 InFlag = Chain.getValue(1);
2279 }
Reed Kotler8453b3f2013-01-24 04:24:02 +00002280
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002281 // Add argument registers to the end of the list so that they are
2282 // known live into the call.
2283 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2284 Ops.push_back(CLI.DAG.getRegister(RegsToPass[i].first,
2285 RegsToPass[i].second.getValueType()));
Reed Kotler8453b3f2013-01-24 04:24:02 +00002286
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002287 // Add a register mask operand representing the call-preserved registers.
2288 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2289 const uint32_t *Mask = TRI->getCallPreservedMask(CLI.CallConv);
2290 assert(Mask && "Missing call preserved mask for calling convention");
Reed Kotler46090912013-05-10 22:25:39 +00002291 if (Subtarget->inMips16HardFloat()) {
2292 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
2293 llvm::StringRef Sym = G->getGlobal()->getName();
2294 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
2295 if (F->hasFnAttribute("__Mips16RetHelper")) {
2296 Mask = MipsRegisterInfo::getMips16RetHelperMask();
2297 }
2298 }
2299 }
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002300 Ops.push_back(CLI.DAG.getRegisterMask(Mask));
2301
2302 if (InFlag.getNode())
2303 Ops.push_back(InFlag);
Reed Kotler8453b3f2013-01-24 04:24:02 +00002304}
2305
Dan Gohman98ca4f22009-08-05 01:29:28 +00002306/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00002307/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002308SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002309MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002310 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002311 SelectionDAG &DAG = CLI.DAG;
Andrew Trickac6d9be2013-05-25 02:42:55 +00002312 SDLoc DL = CLI.DL;
Craig Toppera0ec3f92013-07-14 04:42:23 +00002313 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
2314 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
2315 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Akira Hatanakae2d529a2012-07-31 18:46:41 +00002316 SDValue Chain = CLI.Chain;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002317 SDValue Callee = CLI.Callee;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002318 bool &IsTailCall = CLI.IsTailCall;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002319 CallingConv::ID CallConv = CLI.CallConv;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002320 bool IsVarArg = CLI.IsVarArg;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002321
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002322 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002323 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanakad37776d2011-05-20 21:39:54 +00002324 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002325 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002326
2327 // Analyze operands of the call, assigning locations to each operand.
2328 SmallVector<CCValAssign, 16> ArgLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002329 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka82099682011-12-19 19:52:25 +00002330 getTargetMachine(), ArgLocs, *DAG.getContext());
Reed Kotler46090912013-05-10 22:25:39 +00002331 MipsCC::SpecialCallingConvType SpecialCallingConv =
2332 getSpecialCallingConv(Callee);
Akira Hatanakaad341d42013-08-20 23:38:40 +00002333 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo,
2334 SpecialCallingConv);
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002335
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002336 MipsCCInfo.analyzeCallOperands(Outs, IsVarArg,
Reed Kotlerc673f9c2013-08-30 19:40:56 +00002337 Subtarget->mipsSEUsesSoftFloat(),
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00002338 Callee.getNode(), CLI.Args);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002339
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002340 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002341 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka480eeb52012-07-26 23:27:01 +00002342
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002343 // Check if it's really possible to do a tail call.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002344 if (IsTailCall)
2345 IsTailCall =
2346 isEligibleForTailCallOptimization(MipsCCInfo, NextStackOffset,
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002347 *MF.getInfo<MipsFunctionInfo>());
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002348
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002349 if (IsTailCall)
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002350 ++NumTailCalls;
2351
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002352 // Chain is the output chain of the last Load/Store or CopyToReg node.
2353 // ByValChain is the output chain of the last Memcpy node created for copying
2354 // byval arguments to the stack.
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002355 unsigned StackAlignment = TFL->getStackAlignment();
2356 NextStackOffset = RoundUpToAlignment(NextStackOffset, StackAlignment);
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002357 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002358
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002359 if (!IsTailCall)
Andrew Trick6e0b2a02013-05-29 22:03:55 +00002360 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffsetVal, DL);
Akira Hatanakae2d529a2012-07-31 18:46:41 +00002361
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002362 SDValue StackPtr = DAG.getCopyFromReg(Chain, DL,
Akira Hatanakae2d529a2012-07-31 18:46:41 +00002363 IsN64 ? Mips::SP_64 : Mips::SP,
2364 getPointerTy());
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002365
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002366 // With EABI is it possible to have 16 args on registers.
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00002367 std::deque< std::pair<unsigned, SDValue> > RegsToPass;
Dan Gohman475871a2008-07-27 21:46:04 +00002368 SmallVector<SDValue, 8> MemOpChains;
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002369 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002370
2371 // Walk the register/memloc assignments, inserting copies/loads.
2372 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00002373 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002374 CCValAssign &VA = ArgLocs[i];
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002375 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002376 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2377
2378 // ByVal Arg.
2379 if (Flags.isByVal()) {
2380 assert(Flags.getByValSize() &&
2381 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002382 assert(ByValArg != MipsCCInfo.byval_end());
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002383 assert(!IsTailCall &&
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002384 "Do not tail-call optimize if there is a byval argument.");
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002385 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002386 MipsCCInfo, *ByValArg, Flags, Subtarget->isLittle());
2387 ++ByValArg;
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002388 continue;
2389 }
Jia Liubb481f82012-02-28 07:46:26 +00002390
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002391 // Promote the value if needed.
2392 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002393 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002394 case CCValAssign::Full:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002395 if (VA.isRegLoc()) {
2396 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00002397 (ValVT == MVT::f64 && LocVT == MVT::i64) ||
2398 (ValVT == MVT::i64 && LocVT == MVT::f64))
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002399 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002400 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002401 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002402 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002403 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002404 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00002405 if (!Subtarget->isLittle())
2406 std::swap(Lo, Hi);
Jia Liubb481f82012-02-28 07:46:26 +00002407 unsigned LocRegLo = VA.getLocReg();
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002408 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2409 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2410 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002411 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002412 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002413 }
2414 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00002415 case CCValAssign::SExt:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002416 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002417 break;
2418 case CCValAssign::ZExt:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002419 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002420 break;
2421 case CCValAssign::AExt:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002422 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002423 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002424 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002425
2426 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002427 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002428 if (VA.isRegLoc()) {
2429 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00002430 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002431 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002432
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002433 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00002434 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002435
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002436 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00002437 // parameter value to a stack Location
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002438 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002439 Chain, Arg, DL, IsTailCall, DAG));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002440 }
2441
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002442 // Transform all store nodes into one single node because all store
2443 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002444 if (!MemOpChains.empty())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002445 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002446 &MemOpChains[0], MemOpChains.size());
2447
Bill Wendling056292f2008-09-16 21:48:12 +00002448 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002449 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2450 // node so that legalize doesn't hack it.
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002451 bool IsPICCall = (IsN64 || IsPIC); // true if calls are translated to jalr $25
Akira Hatanakaed185da2012-12-13 03:17:29 +00002452 bool GlobalOrExternal = false, InternalLinkage = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002453 SDValue CalleeLo;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002454
2455 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002456 if (IsPICCall) {
Akira Hatanakaed185da2012-12-13 03:17:29 +00002457 InternalLinkage = G->getGlobal()->hasInternalLinkage();
2458
2459 if (InternalLinkage)
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002460 Callee = getAddrLocal(Callee, DAG, HasMips64);
Akira Hatanakaf09a0372012-11-21 20:40:38 +00002461 else if (LargeGOT)
2462 Callee = getAddrGlobalLargeGOT(Callee, DAG, MipsII::MO_CALL_HI16,
2463 MipsII::MO_CALL_LO16);
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002464 else
2465 Callee = getAddrGlobal(Callee, DAG, MipsII::MO_GOT_CALL);
2466 } else
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002467 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, getPointerTy(), 0,
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002468 MipsII::MO_NO_FLAG);
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002469 GlobalOrExternal = true;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002470 }
2471 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakaf09a0372012-11-21 20:40:38 +00002472 if (!IsN64 && !IsPIC) // !N64 && static
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002473 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2474 MipsII::MO_NO_FLAG);
Akira Hatanakaf09a0372012-11-21 20:40:38 +00002475 else if (LargeGOT)
2476 Callee = getAddrGlobalLargeGOT(Callee, DAG, MipsII::MO_CALL_HI16,
2477 MipsII::MO_CALL_LO16);
Akira Hatanaka60689322013-02-22 21:10:03 +00002478 else // N64 || PIC
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002479 Callee = getAddrGlobal(Callee, DAG, MipsII::MO_GOT_CALL);
2480
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002481 GlobalOrExternal = true;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002482 }
2483
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00002484 SmallVector<SDValue, 8> Ops(1, Chain);
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002485 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00002486
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002487 getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal, InternalLinkage,
2488 CLI, Callee, Chain);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002489
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002490 if (IsTailCall)
2491 return DAG.getNode(MipsISD::TailCall, DL, MVT::Other, &Ops[0], Ops.size());
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002492
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002493 Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, &Ops[0], Ops.size());
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002494 SDValue InFlag = Chain.getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002495
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002496 // Create the CALLSEQ_END node.
Akira Hatanaka480eeb52012-07-26 23:27:01 +00002497 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal,
Andrew Trick6e0b2a02013-05-29 22:03:55 +00002498 DAG.getIntPtrConstant(0, true), InFlag, DL);
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002499 InFlag = Chain.getValue(1);
2500
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002501 // Handle result values, copying them out of physregs into vregs that we
2502 // return.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002503 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg,
2504 Ins, DL, DAG, InVals, CLI.Callee.getNode(), CLI.RetTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002505}
2506
Dan Gohman98ca4f22009-08-05 01:29:28 +00002507/// LowerCallResult - Lower the result values of a call into the
2508/// appropriate copies out of appropriate physical registers.
2509SDValue
2510MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002511 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002512 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002513 SDLoc DL, SelectionDAG &DAG,
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002514 SmallVectorImpl<SDValue> &InVals,
2515 const SDNode *CallNode,
2516 const Type *RetTy) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002517 // Assign locations to each value returned by this call.
2518 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002519 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka864f6602012-06-14 21:10:56 +00002520 getTargetMachine(), RVLocs, *DAG.getContext());
Akira Hatanakaad341d42013-08-20 23:38:40 +00002521 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo);
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002522
Reed Kotlerc673f9c2013-08-30 19:40:56 +00002523 MipsCCInfo.analyzeCallResult(Ins, Subtarget->mipsSEUsesSoftFloat(),
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002524 CallNode, RetTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002525
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002526 // Copy all of the result registers out of their specified physreg.
2527 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002528 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(),
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002529 RVLocs[i].getLocVT(), InFlag);
2530 Chain = Val.getValue(1);
2531 InFlag = Val.getValue(2);
2532
2533 if (RVLocs[i].getValVT() != RVLocs[i].getLocVT())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002534 Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getValVT(), Val);
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002535
2536 InVals.push_back(Val);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002537 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002538
Dan Gohman98ca4f22009-08-05 01:29:28 +00002539 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002540}
2541
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002542//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002543// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002544//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002545/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002546/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002547SDValue
2548MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002549 CallingConv::ID CallConv,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002550 bool IsVarArg,
Akira Hatanaka82099682011-12-19 19:52:25 +00002551 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002552 SDLoc DL, SelectionDAG &DAG,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002553 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002554 const {
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00002555 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002556 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00002557 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002558
Dan Gohman1e93df62010-04-17 14:41:14 +00002559 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002560
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002561 // Used with vargs to acumulate store chains.
2562 std::vector<SDValue> OutChains;
2563
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002564 // Assign locations to all of the incoming arguments.
2565 SmallVector<CCValAssign, 16> ArgLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002566 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka82099682011-12-19 19:52:25 +00002567 getTargetMachine(), ArgLocs, *DAG.getContext());
Akira Hatanakaad341d42013-08-20 23:38:40 +00002568 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo);
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002569 Function::const_arg_iterator FuncArg =
2570 DAG.getMachineFunction().getFunction()->arg_begin();
Reed Kotlerc673f9c2013-08-30 19:40:56 +00002571 bool UseSoftFloat = Subtarget->mipsSEUsesSoftFloat();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002572
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002573 MipsCCInfo.analyzeFormalArguments(Ins, UseSoftFloat, FuncArg);
Akira Hatanakab33b34a2012-10-30 19:37:25 +00002574 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(),
2575 MipsCCInfo.hasByValArg());
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002576
Akira Hatanaka4618e0b2012-10-27 00:44:39 +00002577 unsigned CurArgIdx = 0;
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002578 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002579
Akira Hatanaka4618e0b2012-10-27 00:44:39 +00002580 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002581 CCValAssign &VA = ArgLocs[i];
Akira Hatanaka4618e0b2012-10-27 00:44:39 +00002582 std::advance(FuncArg, Ins[i].OrigArgIndex - CurArgIdx);
2583 CurArgIdx = Ins[i].OrigArgIndex;
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002584 EVT ValVT = VA.getValVT();
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002585 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2586 bool IsRegLoc = VA.isRegLoc();
2587
2588 if (Flags.isByVal()) {
2589 assert(Flags.getByValSize() &&
2590 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002591 assert(ByValArg != MipsCCInfo.byval_end());
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002592 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg,
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002593 MipsCCInfo, *ByValArg);
2594 ++ByValArg;
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002595 continue;
2596 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002597
2598 // Arguments stored on registers
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002599 if (IsRegLoc) {
Owen Andersone50ed302009-08-10 22:56:29 +00002600 EVT RegVT = VA.getLocVT();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002601 unsigned ArgReg = VA.getLocReg();
Craig Topper44d23822012-02-22 05:59:10 +00002602 const TargetRegisterClass *RC;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002603
Owen Anderson825b72b2009-08-11 20:47:22 +00002604 if (RegVT == MVT::i32)
Reed Kotlerbacbf1c2012-12-20 06:06:35 +00002605 RC = Subtarget->inMips16Mode()? &Mips::CPU16RegsRegClass :
Akira Hatanaka18587862013-08-06 23:08:38 +00002606 &Mips::GPR32RegClass;
Akira Hatanaka95934842011-09-24 01:34:44 +00002607 else if (RegVT == MVT::i64)
Akira Hatanaka18587862013-08-06 23:08:38 +00002608 RC = &Mips::GPR64RegClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002609 else if (RegVT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00002610 RC = &Mips::FGR32RegClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002611 else if (RegVT == MVT::f64)
Akira Hatanakaad341d42013-08-20 23:38:40 +00002612 RC = Subtarget->isFP64bit() ? &Mips::FGR64RegClass :
2613 &Mips::AFGR64RegClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002614 else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002615 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002616
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002617 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002618 // physical registers into virtual ones
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002619 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC);
2620 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002621
2622 // If this is an 8 or 16-bit value, it has been passed promoted
2623 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002624 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002625 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00002626 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002627 if (VA.getLocInfo() == CCValAssign::SExt)
2628 Opcode = ISD::AssertSext;
2629 else if (VA.getLocInfo() == CCValAssign::ZExt)
2630 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00002631 if (Opcode)
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002632 ArgValue = DAG.getNode(Opcode, DL, RegVT, ArgValue,
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002633 DAG.getValueType(ValVT));
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002634 ArgValue = DAG.getNode(ISD::TRUNCATE, DL, ValVT, ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002635 }
2636
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002637 // Handle floating point arguments passed in integer registers and
2638 // long double arguments passed in floating point registers.
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002639 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002640 (RegVT == MVT::i64 && ValVT == MVT::f64) ||
2641 (RegVT == MVT::f64 && ValVT == MVT::i64))
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002642 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002643 else if (IsO32 && RegVT == MVT::i32 && ValVT == MVT::f64) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002644 unsigned Reg2 = addLiveIn(DAG.getMachineFunction(),
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002645 getNextIntArgReg(ArgReg), RC);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002646 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT);
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002647 if (!Subtarget->isLittle())
2648 std::swap(ArgValue, ArgValue2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002649 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64,
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002650 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002651 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002652
Dan Gohman98ca4f22009-08-05 01:29:28 +00002653 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002654 } else { // VA.isRegLoc()
2655
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002656 // sanity check
2657 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002658
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002659 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002660 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002661 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002662
2663 // Create load nodes to retrieve arguments from the stack
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002664 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002665 InVals.push_back(DAG.getLoad(ValVT, DL, Chain, FIN,
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002666 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002667 false, false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002668 }
2669 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002670
2671 // The mips ABIs for returning structs by value requires that we copy
2672 // the sret argument into $v0 for the return. Save the argument into
2673 // a virtual register so that we can access it from the return points.
2674 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2675 unsigned Reg = MipsFI->getSRetReturnReg();
2676 if (!Reg) {
Akira Hatanaka30580ce2012-10-19 22:11:40 +00002677 Reg = MF.getRegInfo().
2678 createVirtualRegister(getRegClassFor(IsN64 ? MVT::i64 : MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002679 MipsFI->setSRetReturnReg(Reg);
2680 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002681 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[0]);
2682 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002683 }
2684
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002685 if (IsVarArg)
2686 writeVarArgRegs(OutChains, MipsCCInfo, Chain, DL, DAG);
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002687
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002688 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002689 // the size of Ins and InVals. This only happens when on varg functions
2690 if (!OutChains.empty()) {
2691 OutChains.push_back(Chain);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002692 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002693 &OutChains[0], OutChains.size());
2694 }
2695
Dan Gohman98ca4f22009-08-05 01:29:28 +00002696 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002697}
2698
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002699//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002700// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002701//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002702
Akira Hatanaka97d9f082012-10-10 01:27:09 +00002703bool
2704MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002705 MachineFunction &MF, bool IsVarArg,
Akira Hatanaka97d9f082012-10-10 01:27:09 +00002706 const SmallVectorImpl<ISD::OutputArg> &Outs,
2707 LLVMContext &Context) const {
2708 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002709 CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(),
Akira Hatanaka97d9f082012-10-10 01:27:09 +00002710 RVLocs, Context);
2711 return CCInfo.CheckReturn(Outs, RetCC_Mips);
2712}
2713
Dan Gohman98ca4f22009-08-05 01:29:28 +00002714SDValue
2715MipsTargetLowering::LowerReturn(SDValue Chain,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002716 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002717 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002718 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002719 SDLoc DL, SelectionDAG &DAG) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002720 // CCValAssign - represent the assignment of
2721 // the return value to a location
2722 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002723 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002724
2725 // CCState - Info about the registers and stack slot.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002726 CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(), RVLocs,
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002727 *DAG.getContext());
Akira Hatanakaad341d42013-08-20 23:38:40 +00002728 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002729
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002730 // Analyze return values.
Reed Kotlerc673f9c2013-08-30 19:40:56 +00002731 MipsCCInfo.analyzeReturn(Outs, Subtarget->mipsSEUsesSoftFloat(),
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002732 MF.getFunction()->getReturnType());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002733
Dan Gohman475871a2008-07-27 21:46:04 +00002734 SDValue Flag;
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002735 SmallVector<SDValue, 4> RetOps(1, Chain);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002736
2737 // Copy the result values into the output registers.
2738 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002739 SDValue Val = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002740 CCValAssign &VA = RVLocs[i];
2741 assert(VA.isRegLoc() && "Can only return in registers!");
2742
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002743 if (RVLocs[i].getValVT() != RVLocs[i].getLocVT())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002744 Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getLocVT(), Val);
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002745
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002746 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002747
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002748 // Guarantee that all emitted copies are stuck together with flags.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002749 Flag = Chain.getValue(1);
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002750 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002751 }
2752
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002753 // The mips ABIs for returning structs by value requires that we copy
2754 // the sret argument into $v0 for the return. We saved the argument into
2755 // a virtual register in the entry block, so now we copy the value out
2756 // and into $v0.
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002757 if (MF.getFunction()->hasStructRetAttr()) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002758 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2759 unsigned Reg = MipsFI->getSRetReturnReg();
2760
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002761 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00002762 llvm_unreachable("sret virtual register not created in the entry block");
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002763 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Akira Hatanaka2ef5bd32012-10-24 02:10:54 +00002764 unsigned V0 = IsN64 ? Mips::V0_64 : Mips::V0;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002765
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002766 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002767 Flag = Chain.getValue(1);
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002768 RetOps.push_back(DAG.getRegister(V0, getPointerTy()));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002769 }
2770
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002771 RetOps[0] = Chain; // Update chain.
Akira Hatanaka182ef6f2012-07-10 00:19:06 +00002772
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002773 // Add the flag if we have it.
2774 if (Flag.getNode())
2775 RetOps.push_back(Flag);
2776
2777 // Return on Mips is always a "jr $ra"
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002778 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, &RetOps[0], RetOps.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002779}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002780
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002781//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002782// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002783//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002784
2785/// getConstraintType - Given a constraint letter, return the type of
2786/// constraint it is for this target.
2787MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002788getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002789{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002790 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002791 // GCC config/mips/constraints.md
2792 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002793 // 'd' : An address register. Equivalent to r
2794 // unless generating MIPS16 code.
2795 // 'y' : Equivalent to r; retained for
2796 // backwards compatibility.
Eric Christopher1d5a3922012-05-07 06:25:10 +00002797 // 'c' : A register suitable for use in an indirect
2798 // jump. This will always be $25 for -mabicalls.
Eric Christopheraf97f732012-05-07 06:25:19 +00002799 // 'l' : The lo register. 1 word storage.
2800 // 'x' : The hilo register pair. Double word storage.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002801 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002802 switch (Constraint[0]) {
2803 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002804 case 'd':
2805 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002806 case 'f':
Eric Christopher1d5a3922012-05-07 06:25:10 +00002807 case 'c':
Eric Christopher4adbefe2012-05-07 06:25:15 +00002808 case 'l':
Eric Christopheraf97f732012-05-07 06:25:19 +00002809 case 'x':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002810 return C_RegisterClass;
Jack Carter0b9675d2013-03-04 21:33:15 +00002811 case 'R':
2812 return C_Memory;
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002813 }
2814 }
2815 return TargetLowering::getConstraintType(Constraint);
2816}
2817
John Thompson44ab89e2010-10-29 17:29:13 +00002818/// Examine constraint type and operand type and determine a weight value.
2819/// This object must already have been set up with the operand type
2820/// and the current alternative constraint selected.
2821TargetLowering::ConstraintWeight
2822MipsTargetLowering::getSingleConstraintMatchWeight(
2823 AsmOperandInfo &info, const char *constraint) const {
2824 ConstraintWeight weight = CW_Invalid;
2825 Value *CallOperandVal = info.CallOperandVal;
2826 // If we don't have a value, we can't do a match,
2827 // but allow it at the lowest weight.
2828 if (CallOperandVal == NULL)
2829 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002830 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00002831 // Look at the constraint type.
2832 switch (*constraint) {
2833 default:
2834 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2835 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002836 case 'd':
2837 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00002838 if (type->isIntegerTy())
2839 weight = CW_Register;
2840 break;
2841 case 'f':
2842 if (type->isFloatTy())
2843 weight = CW_Register;
2844 break;
Eric Christopher1d5a3922012-05-07 06:25:10 +00002845 case 'c': // $25 for indirect jumps
Eric Christopher4adbefe2012-05-07 06:25:15 +00002846 case 'l': // lo register
Eric Christopheraf97f732012-05-07 06:25:19 +00002847 case 'x': // hilo register pair
Eric Christopher1d5a3922012-05-07 06:25:10 +00002848 if (type->isIntegerTy())
2849 weight = CW_SpecificReg;
2850 break;
Eric Christopher50ab0392012-05-07 03:13:32 +00002851 case 'I': // signed 16 bit immediate
Eric Christophere5076d42012-05-07 03:13:42 +00002852 case 'J': // integer zero
Eric Christopherf49f8462012-05-07 05:46:29 +00002853 case 'K': // unsigned 16 bit immediate
Eric Christopher5ac47bb2012-05-07 05:46:37 +00002854 case 'L': // signed 32 bit immediate where lower 16 bits are 0
Eric Christopher60cfc792012-05-07 05:46:43 +00002855 case 'N': // immediate in the range of -65535 to -1 (inclusive)
Eric Christopher1ce20342012-05-07 05:46:48 +00002856 case 'O': // signed 15 bit immediate (+- 16383)
Eric Christopher54412a72012-05-07 06:25:02 +00002857 case 'P': // immediate in the range of 65535 to 1 (inclusive)
Eric Christopher50ab0392012-05-07 03:13:32 +00002858 if (isa<ConstantInt>(CallOperandVal))
2859 weight = CW_Constant;
2860 break;
Jack Carter0b9675d2013-03-04 21:33:15 +00002861 case 'R':
2862 weight = CW_Memory;
2863 break;
John Thompson44ab89e2010-10-29 17:29:13 +00002864 }
2865 return weight;
2866}
2867
Akira Hatanakabfb07b12013-08-14 00:21:25 +00002868/// This is a helper function to parse a physical register string and split it
2869/// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag
2870/// that is returned indicates whether parsing was successful. The second flag
2871/// is true if the numeric part exists.
2872static std::pair<bool, bool>
2873parsePhysicalReg(const StringRef &C, std::string &Prefix,
2874 unsigned long long &Reg) {
2875 if (C.front() != '{' || C.back() != '}')
2876 return std::make_pair(false, false);
2877
2878 // Search for the first numeric character.
2879 StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1;
2880 I = std::find_if(B, E, std::ptr_fun(isdigit));
2881
2882 Prefix.assign(B, I - B);
2883
2884 // The second flag is set to false if no numeric characters were found.
2885 if (I == E)
2886 return std::make_pair(true, false);
2887
2888 // Parse the numeric characters.
2889 return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg),
2890 true);
2891}
2892
2893std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering::
2894parseRegForInlineAsmConstraint(const StringRef &C, MVT VT) const {
2895 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2896 const TargetRegisterClass *RC;
2897 std::string Prefix;
2898 unsigned long long Reg;
2899
2900 std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg);
2901
2902 if (!R.first)
2903 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2904
2905 if ((Prefix == "hi" || Prefix == "lo")) { // Parse hi/lo.
2906 // No numeric characters follow "hi" or "lo".
2907 if (R.second)
2908 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2909
2910 RC = TRI->getRegClass(Prefix == "hi" ?
Akira Hatanakacbaf6d02013-08-14 00:47:08 +00002911 Mips::HI32RegClassID : Mips::LO32RegClassID);
Akira Hatanakabfb07b12013-08-14 00:21:25 +00002912 return std::make_pair(*(RC->begin()), RC);
2913 }
2914
2915 if (!R.second)
2916 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2917
2918 if (Prefix == "$f") { // Parse $f0-$f31.
2919 // If the size of FP registers is 64-bit or Reg is an even number, select
2920 // the 64-bit register class. Otherwise, select the 32-bit register class.
2921 if (VT == MVT::Other)
2922 VT = (Subtarget->isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32;
2923
2924 RC= getRegClassFor(VT);
2925
2926 if (RC == &Mips::AFGR64RegClass) {
2927 assert(Reg % 2 == 0);
2928 Reg >>= 1;
2929 }
2930 } else if (Prefix == "$fcc") { // Parse $fcc0-$fcc7.
2931 RC = TRI->getRegClass(Mips::FCCRegClassID);
2932 } else { // Parse $0-$31.
2933 assert(Prefix == "$");
2934 RC = getRegClassFor((VT == MVT::Other) ? MVT::i32 : VT);
2935 }
2936
2937 assert(Reg < RC->getNumRegs());
2938 return std::make_pair(*(RC->begin() + Reg), RC);
2939}
2940
Eric Christopher38d64262011-06-29 19:33:04 +00002941/// Given a register class constraint, like 'r', if this corresponds directly
2942/// to an LLVM register class, return a register of 0 and the register class
2943/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002944std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Chad Rosier5b3fca52013-06-22 18:37:38 +00002945getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002946{
2947 if (Constraint.size() == 1) {
2948 switch (Constraint[0]) {
Eric Christopher314aff12011-06-29 19:04:31 +00002949 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
2950 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002951 case 'r':
Akira Hatanakaafc945b2012-09-12 23:27:55 +00002952 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
2953 if (Subtarget->inMips16Mode())
2954 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
Akira Hatanaka18587862013-08-06 23:08:38 +00002955 return std::make_pair(0U, &Mips::GPR32RegClass);
Akira Hatanakaafc945b2012-09-12 23:27:55 +00002956 }
Jack Carter10de0252012-07-02 23:35:23 +00002957 if (VT == MVT::i64 && !HasMips64)
Akira Hatanaka18587862013-08-06 23:08:38 +00002958 return std::make_pair(0U, &Mips::GPR32RegClass);
Eric Christopher0ed1f762012-05-07 03:13:22 +00002959 if (VT == MVT::i64 && HasMips64)
Akira Hatanaka18587862013-08-06 23:08:38 +00002960 return std::make_pair(0U, &Mips::GPR64RegClass);
Eric Christopher0ed1f762012-05-07 03:13:22 +00002961 // This will generate an error message
2962 return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002963 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00002964 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00002965 return std::make_pair(0U, &Mips::FGR32RegClass);
Akira Hatanakacb9dd722012-01-04 02:45:01 +00002966 if ((VT == MVT::f64) && (!Subtarget->isSingleFloat())) {
2967 if (Subtarget->isFP64bit())
Craig Topper420761a2012-04-20 07:30:17 +00002968 return std::make_pair(0U, &Mips::FGR64RegClass);
2969 return std::make_pair(0U, &Mips::AFGR64RegClass);
Akira Hatanakacb9dd722012-01-04 02:45:01 +00002970 }
Eric Christopher1d5a3922012-05-07 06:25:10 +00002971 break;
2972 case 'c': // register suitable for indirect jump
2973 if (VT == MVT::i32)
Akira Hatanaka18587862013-08-06 23:08:38 +00002974 return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass);
Eric Christopher1d5a3922012-05-07 06:25:10 +00002975 assert(VT == MVT::i64 && "Unexpected type.");
Akira Hatanaka18587862013-08-06 23:08:38 +00002976 return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass);
Eric Christopher4adbefe2012-05-07 06:25:15 +00002977 case 'l': // register suitable for indirect jump
2978 if (VT == MVT::i32)
Akira Hatanakacbaf6d02013-08-14 00:47:08 +00002979 return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass);
2980 return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass);
Eric Christopheraf97f732012-05-07 06:25:19 +00002981 case 'x': // register suitable for indirect jump
2982 // Fixme: Not triggering the use of both hi and low
2983 // This will generate an error message
2984 return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002985 }
2986 }
Akira Hatanakabfb07b12013-08-14 00:21:25 +00002987
2988 std::pair<unsigned, const TargetRegisterClass *> R;
2989 R = parseRegForInlineAsmConstraint(Constraint, VT);
2990
2991 if (R.second)
2992 return R;
2993
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002994 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2995}
2996
Eric Christopher50ab0392012-05-07 03:13:32 +00002997/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
2998/// vector. If it is invalid, don't add anything to Ops.
2999void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3000 std::string &Constraint,
3001 std::vector<SDValue>&Ops,
3002 SelectionDAG &DAG) const {
3003 SDValue Result(0, 0);
3004
3005 // Only support length 1 constraints for now.
3006 if (Constraint.length() > 1) return;
3007
3008 char ConstraintLetter = Constraint[0];
3009 switch (ConstraintLetter) {
3010 default: break; // This will fall through to the generic implementation
3011 case 'I': // Signed 16 bit constant
3012 // If this fails, the parent routine will give an error
3013 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3014 EVT Type = Op.getValueType();
3015 int64_t Val = C->getSExtValue();
3016 if (isInt<16>(Val)) {
3017 Result = DAG.getTargetConstant(Val, Type);
3018 break;
3019 }
3020 }
3021 return;
Eric Christophere5076d42012-05-07 03:13:42 +00003022 case 'J': // integer zero
3023 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3024 EVT Type = Op.getValueType();
3025 int64_t Val = C->getZExtValue();
3026 if (Val == 0) {
3027 Result = DAG.getTargetConstant(0, Type);
3028 break;
3029 }
3030 }
3031 return;
Eric Christopherf49f8462012-05-07 05:46:29 +00003032 case 'K': // unsigned 16 bit immediate
3033 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3034 EVT Type = Op.getValueType();
3035 uint64_t Val = (uint64_t)C->getZExtValue();
3036 if (isUInt<16>(Val)) {
3037 Result = DAG.getTargetConstant(Val, Type);
3038 break;
3039 }
3040 }
3041 return;
Eric Christopher5ac47bb2012-05-07 05:46:37 +00003042 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3043 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3044 EVT Type = Op.getValueType();
3045 int64_t Val = C->getSExtValue();
3046 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
3047 Result = DAG.getTargetConstant(Val, Type);
3048 break;
3049 }
3050 }
3051 return;
Eric Christopher60cfc792012-05-07 05:46:43 +00003052 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3053 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3054 EVT Type = Op.getValueType();
3055 int64_t Val = C->getSExtValue();
3056 if ((Val >= -65535) && (Val <= -1)) {
3057 Result = DAG.getTargetConstant(Val, Type);
3058 break;
3059 }
3060 }
3061 return;
Eric Christopher1ce20342012-05-07 05:46:48 +00003062 case 'O': // signed 15 bit immediate
3063 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3064 EVT Type = Op.getValueType();
3065 int64_t Val = C->getSExtValue();
3066 if ((isInt<15>(Val))) {
3067 Result = DAG.getTargetConstant(Val, Type);
3068 break;
3069 }
3070 }
3071 return;
Eric Christopher54412a72012-05-07 06:25:02 +00003072 case 'P': // immediate in the range of 1 to 65535 (inclusive)
3073 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3074 EVT Type = Op.getValueType();
3075 int64_t Val = C->getSExtValue();
3076 if ((Val <= 65535) && (Val >= 1)) {
3077 Result = DAG.getTargetConstant(Val, Type);
3078 break;
3079 }
3080 }
3081 return;
Eric Christopher50ab0392012-05-07 03:13:32 +00003082 }
3083
3084 if (Result.getNode()) {
3085 Ops.push_back(Result);
3086 return;
3087 }
3088
3089 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3090}
3091
Dan Gohman6520e202008-10-18 02:06:02 +00003092bool
Akira Hatanaka94e47282012-11-17 00:25:41 +00003093MipsTargetLowering::isLegalAddressingMode(const AddrMode &AM, Type *Ty) const {
3094 // No global is ever allowed as a base.
3095 if (AM.BaseGV)
3096 return false;
3097
3098 switch (AM.Scale) {
3099 case 0: // "r+i" or just "i", depending on HasBaseReg.
3100 break;
3101 case 1:
3102 if (!AM.HasBaseReg) // allow "r+i".
3103 break;
3104 return false; // disallow "r+r" or "r+r+i".
3105 default:
3106 return false;
3107 }
3108
3109 return true;
3110}
3111
3112bool
Dan Gohman6520e202008-10-18 02:06:02 +00003113MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3114 // The Mips target isn't yet aware of offsets.
3115 return false;
3116}
Evan Chengeb2f9692009-10-27 19:56:55 +00003117
Akira Hatanakae193b322012-06-13 19:33:32 +00003118EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00003119 unsigned SrcAlign,
3120 bool IsMemset, bool ZeroMemset,
Akira Hatanakae193b322012-06-13 19:33:32 +00003121 bool MemcpyStrSrc,
3122 MachineFunction &MF) const {
3123 if (Subtarget->hasMips64())
3124 return MVT::i64;
3125
3126 return MVT::i32;
3127}
3128
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003129bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
3130 if (VT != MVT::f32 && VT != MVT::f64)
3131 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00003132 if (Imm.isNegZero())
3133 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00003134 return Imm.isZero();
3135}
Akira Hatanaka6c2cf8b2012-02-03 04:33:00 +00003136
3137unsigned MipsTargetLowering::getJumpTableEncoding() const {
3138 if (IsN64)
3139 return MachineJumpTableInfo::EK_GPRel64BlockAddress;
Jia Liubb481f82012-02-28 07:46:26 +00003140
Akira Hatanaka6c2cf8b2012-02-03 04:33:00 +00003141 return TargetLowering::getJumpTableEncoding();
3142}
Akira Hatanaka7887c902012-10-26 23:56:38 +00003143
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003144/// This function returns true if CallSym is a long double emulation routine.
3145static bool isF128SoftLibCall(const char *CallSym) {
3146 const char *const LibCalls[] =
3147 {"__addtf3", "__divtf3", "__eqtf2", "__extenddftf2", "__extendsftf2",
3148 "__fixtfdi", "__fixtfsi", "__fixtfti", "__fixunstfdi", "__fixunstfsi",
3149 "__fixunstfti", "__floatditf", "__floatsitf", "__floattitf",
3150 "__floatunditf", "__floatunsitf", "__floatuntitf", "__getf2", "__gttf2",
3151 "__letf2", "__lttf2", "__multf3", "__netf2", "__powitf2", "__subtf3",
3152 "__trunctfdf2", "__trunctfsf2", "__unordtf2",
3153 "ceill", "copysignl", "cosl", "exp2l", "expl", "floorl", "fmal", "fmodl",
3154 "log10l", "log2l", "logl", "nearbyintl", "powl", "rintl", "sinl", "sqrtl",
3155 "truncl"};
3156
3157 const char * const *End = LibCalls + array_lengthof(LibCalls);
3158
3159 // Check that LibCalls is sorted alphabetically.
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00003160 MipsTargetLowering::LTStr Comp;
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003161
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00003162#ifndef NDEBUG
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003163 for (const char * const *I = LibCalls; I < End - 1; ++I)
3164 assert(Comp(*I, *(I + 1)));
3165#endif
3166
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00003167 return std::binary_search(LibCalls, End, CallSym, Comp);
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003168}
3169
3170/// This function returns true if Ty is fp128 or i128 which was originally a
3171/// fp128.
3172static bool originalTypeIsF128(const Type *Ty, const SDNode *CallNode) {
3173 if (Ty->isFP128Ty())
3174 return true;
3175
3176 const ExternalSymbolSDNode *ES =
3177 dyn_cast_or_null<const ExternalSymbolSDNode>(CallNode);
3178
3179 // If the Ty is i128 and the function being called is a long double emulation
3180 // routine, then the original type is f128.
3181 return (ES && Ty->isIntegerTy(128) && isF128SoftLibCall(ES->getSymbol()));
3182}
3183
Reed Kotler46090912013-05-10 22:25:39 +00003184MipsTargetLowering::MipsCC::SpecialCallingConvType
3185 MipsTargetLowering::getSpecialCallingConv(SDValue Callee) const {
3186 MipsCC::SpecialCallingConvType SpecialCallingConv =
3187 MipsCC::NoSpecialCallingConv;;
3188 if (Subtarget->inMips16HardFloat()) {
3189 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3190 llvm::StringRef Sym = G->getGlobal()->getName();
3191 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
3192 if (F->hasFnAttribute("__Mips16RetHelper")) {
3193 SpecialCallingConv = MipsCC::Mips16RetHelperConv;
3194 }
3195 }
3196 }
3197 return SpecialCallingConv;
3198}
3199
3200MipsTargetLowering::MipsCC::MipsCC(
Akira Hatanakaad341d42013-08-20 23:38:40 +00003201 CallingConv::ID CC, bool IsO32_, bool IsFP64_, CCState &Info,
Reed Kotler46090912013-05-10 22:25:39 +00003202 MipsCC::SpecialCallingConvType SpecialCallingConv_)
Akira Hatanakaad341d42013-08-20 23:38:40 +00003203 : CCInfo(Info), CallConv(CC), IsO32(IsO32_), IsFP64(IsFP64_),
Reed Kotler46090912013-05-10 22:25:39 +00003204 SpecialCallingConv(SpecialCallingConv_){
Akira Hatanaka7887c902012-10-26 23:56:38 +00003205 // Pre-allocate reserved argument area.
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003206 CCInfo.AllocateStack(reservedArgArea(), 1);
Akira Hatanaka7887c902012-10-26 23:56:38 +00003207}
3208
Reed Kotler46090912013-05-10 22:25:39 +00003209
Akira Hatanaka7887c902012-10-26 23:56:38 +00003210void MipsTargetLowering::MipsCC::
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003211analyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Args,
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00003212 bool IsVarArg, bool IsSoftFloat, const SDNode *CallNode,
3213 std::vector<ArgListEntry> &FuncArgs) {
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003214 assert((CallConv != CallingConv::Fast || !IsVarArg) &&
3215 "CallingConv::Fast shouldn't be used for vararg functions.");
3216
Akira Hatanaka7887c902012-10-26 23:56:38 +00003217 unsigned NumOpnds = Args.size();
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003218 llvm::CCAssignFn *FixedFn = fixedArgFn(), *VarFn = varArgFn();
Akira Hatanaka7887c902012-10-26 23:56:38 +00003219
3220 for (unsigned I = 0; I != NumOpnds; ++I) {
3221 MVT ArgVT = Args[I].VT;
3222 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
3223 bool R;
3224
3225 if (ArgFlags.isByVal()) {
3226 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
3227 continue;
3228 }
3229
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003230 if (IsVarArg && !Args[I].IsFixed)
Akira Hatanaka7887c902012-10-26 23:56:38 +00003231 R = VarFn(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00003232 else {
3233 MVT RegVT = getRegVT(ArgVT, FuncArgs[Args[I].OrigArgIndex].Ty, CallNode,
3234 IsSoftFloat);
3235 R = FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo);
3236 }
Akira Hatanaka7887c902012-10-26 23:56:38 +00003237
3238 if (R) {
3239#ifndef NDEBUG
3240 dbgs() << "Call operand #" << I << " has unhandled type "
3241 << EVT(ArgVT).getEVTString();
3242#endif
3243 llvm_unreachable(0);
3244 }
3245 }
3246}
3247
3248void MipsTargetLowering::MipsCC::
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003249analyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Args,
3250 bool IsSoftFloat, Function::const_arg_iterator FuncArg) {
Akira Hatanaka7887c902012-10-26 23:56:38 +00003251 unsigned NumArgs = Args.size();
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003252 llvm::CCAssignFn *FixedFn = fixedArgFn();
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003253 unsigned CurArgIdx = 0;
Akira Hatanaka7887c902012-10-26 23:56:38 +00003254
3255 for (unsigned I = 0; I != NumArgs; ++I) {
3256 MVT ArgVT = Args[I].VT;
3257 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003258 std::advance(FuncArg, Args[I].OrigArgIndex - CurArgIdx);
3259 CurArgIdx = Args[I].OrigArgIndex;
Akira Hatanaka7887c902012-10-26 23:56:38 +00003260
3261 if (ArgFlags.isByVal()) {
3262 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
3263 continue;
3264 }
3265
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003266 MVT RegVT = getRegVT(ArgVT, FuncArg->getType(), 0, IsSoftFloat);
3267
3268 if (!FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo))
Akira Hatanaka7887c902012-10-26 23:56:38 +00003269 continue;
3270
3271#ifndef NDEBUG
3272 dbgs() << "Formal Arg #" << I << " has unhandled type "
3273 << EVT(ArgVT).getEVTString();
3274#endif
3275 llvm_unreachable(0);
3276 }
3277}
3278
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003279template<typename Ty>
3280void MipsTargetLowering::MipsCC::
3281analyzeReturn(const SmallVectorImpl<Ty> &RetVals, bool IsSoftFloat,
3282 const SDNode *CallNode, const Type *RetTy) const {
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003283 CCAssignFn *Fn;
3284
3285 if (IsSoftFloat && originalTypeIsF128(RetTy, CallNode))
3286 Fn = RetCC_F128Soft;
3287 else
3288 Fn = RetCC_Mips;
3289
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003290 for (unsigned I = 0, E = RetVals.size(); I < E; ++I) {
3291 MVT VT = RetVals[I].VT;
3292 ISD::ArgFlagsTy Flags = RetVals[I].Flags;
3293 MVT RegVT = this->getRegVT(VT, RetTy, CallNode, IsSoftFloat);
3294
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003295 if (Fn(I, VT, RegVT, CCValAssign::Full, Flags, this->CCInfo)) {
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003296#ifndef NDEBUG
3297 dbgs() << "Call result #" << I << " has unhandled type "
3298 << EVT(VT).getEVTString() << '\n';
3299#endif
3300 llvm_unreachable(0);
3301 }
3302 }
3303}
3304
3305void MipsTargetLowering::MipsCC::
3306analyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins, bool IsSoftFloat,
3307 const SDNode *CallNode, const Type *RetTy) const {
3308 analyzeReturn(Ins, IsSoftFloat, CallNode, RetTy);
3309}
3310
3311void MipsTargetLowering::MipsCC::
3312analyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs, bool IsSoftFloat,
3313 const Type *RetTy) const {
3314 analyzeReturn(Outs, IsSoftFloat, 0, RetTy);
3315}
3316
Akira Hatanaka7887c902012-10-26 23:56:38 +00003317void
3318MipsTargetLowering::MipsCC::handleByValArg(unsigned ValNo, MVT ValVT,
3319 MVT LocVT,
3320 CCValAssign::LocInfo LocInfo,
3321 ISD::ArgFlagsTy ArgFlags) {
3322 assert(ArgFlags.getByValSize() && "Byval argument's size shouldn't be 0.");
3323
3324 struct ByValArgInfo ByVal;
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003325 unsigned RegSize = regSize();
Akira Hatanaka7887c902012-10-26 23:56:38 +00003326 unsigned ByValSize = RoundUpToAlignment(ArgFlags.getByValSize(), RegSize);
3327 unsigned Align = std::min(std::max(ArgFlags.getByValAlign(), RegSize),
3328 RegSize * 2);
3329
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003330 if (useRegsForByval())
Akira Hatanaka7887c902012-10-26 23:56:38 +00003331 allocateRegs(ByVal, ByValSize, Align);
3332
3333 // Allocate space on caller's stack.
3334 ByVal.Address = CCInfo.AllocateStack(ByValSize - RegSize * ByVal.NumRegs,
3335 Align);
3336 CCInfo.addLoc(CCValAssign::getMem(ValNo, ValVT, ByVal.Address, LocVT,
3337 LocInfo));
3338 ByValArgs.push_back(ByVal);
3339}
3340
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003341unsigned MipsTargetLowering::MipsCC::numIntArgRegs() const {
3342 return IsO32 ? array_lengthof(O32IntRegs) : array_lengthof(Mips64IntRegs);
3343}
3344
3345unsigned MipsTargetLowering::MipsCC::reservedArgArea() const {
3346 return (IsO32 && (CallConv != CallingConv::Fast)) ? 16 : 0;
3347}
3348
3349const uint16_t *MipsTargetLowering::MipsCC::intArgRegs() const {
3350 return IsO32 ? O32IntRegs : Mips64IntRegs;
3351}
3352
3353llvm::CCAssignFn *MipsTargetLowering::MipsCC::fixedArgFn() const {
3354 if (CallConv == CallingConv::Fast)
3355 return CC_Mips_FastCC;
3356
Reed Kotler46090912013-05-10 22:25:39 +00003357 if (SpecialCallingConv == Mips16RetHelperConv)
3358 return CC_Mips16RetHelper;
Akira Hatanakaad341d42013-08-20 23:38:40 +00003359 return IsO32 ? (IsFP64 ? CC_MipsO32_FP64 : CC_MipsO32_FP32) : CC_MipsN;
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003360}
3361
3362llvm::CCAssignFn *MipsTargetLowering::MipsCC::varArgFn() const {
Akira Hatanakaad341d42013-08-20 23:38:40 +00003363 return IsO32 ? (IsFP64 ? CC_MipsO32_FP64 : CC_MipsO32_FP32) : CC_MipsN_VarArg;
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003364}
3365
3366const uint16_t *MipsTargetLowering::MipsCC::shadowRegs() const {
3367 return IsO32 ? O32IntRegs : Mips64DPRegs;
3368}
3369
Akira Hatanaka7887c902012-10-26 23:56:38 +00003370void MipsTargetLowering::MipsCC::allocateRegs(ByValArgInfo &ByVal,
3371 unsigned ByValSize,
3372 unsigned Align) {
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003373 unsigned RegSize = regSize(), NumIntArgRegs = numIntArgRegs();
3374 const uint16_t *IntArgRegs = intArgRegs(), *ShadowRegs = shadowRegs();
Akira Hatanaka7887c902012-10-26 23:56:38 +00003375 assert(!(ByValSize % RegSize) && !(Align % RegSize) &&
3376 "Byval argument's size and alignment should be a multiple of"
3377 "RegSize.");
3378
3379 ByVal.FirstIdx = CCInfo.getFirstUnallocated(IntArgRegs, NumIntArgRegs);
3380
3381 // If Align > RegSize, the first arg register must be even.
3382 if ((Align > RegSize) && (ByVal.FirstIdx % 2)) {
3383 CCInfo.AllocateReg(IntArgRegs[ByVal.FirstIdx], ShadowRegs[ByVal.FirstIdx]);
3384 ++ByVal.FirstIdx;
3385 }
3386
3387 // Mark the registers allocated.
3388 for (unsigned I = ByVal.FirstIdx; ByValSize && (I < NumIntArgRegs);
3389 ByValSize -= RegSize, ++I, ++ByVal.NumRegs)
3390 CCInfo.AllocateReg(IntArgRegs[I], ShadowRegs[I]);
3391}
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003392
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003393MVT MipsTargetLowering::MipsCC::getRegVT(MVT VT, const Type *OrigTy,
3394 const SDNode *CallNode,
3395 bool IsSoftFloat) const {
3396 if (IsSoftFloat || IsO32)
3397 return VT;
3398
3399 // Check if the original type was fp128.
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003400 if (originalTypeIsF128(OrigTy, CallNode)) {
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003401 assert(VT == MVT::i64);
3402 return MVT::f64;
3403 }
3404
3405 return VT;
3406}
3407
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003408void MipsTargetLowering::
Andrew Trickac6d9be2013-05-25 02:42:55 +00003409copyByValRegs(SDValue Chain, SDLoc DL, std::vector<SDValue> &OutChains,
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003410 SelectionDAG &DAG, const ISD::ArgFlagsTy &Flags,
3411 SmallVectorImpl<SDValue> &InVals, const Argument *FuncArg,
3412 const MipsCC &CC, const ByValArgInfo &ByVal) const {
3413 MachineFunction &MF = DAG.getMachineFunction();
3414 MachineFrameInfo *MFI = MF.getFrameInfo();
3415 unsigned RegAreaSize = ByVal.NumRegs * CC.regSize();
3416 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
3417 int FrameObjOffset;
3418
3419 if (RegAreaSize)
3420 FrameObjOffset = (int)CC.reservedArgArea() -
3421 (int)((CC.numIntArgRegs() - ByVal.FirstIdx) * CC.regSize());
3422 else
3423 FrameObjOffset = ByVal.Address;
3424
3425 // Create frame object.
3426 EVT PtrTy = getPointerTy();
3427 int FI = MFI->CreateFixedObject(FrameObjSize, FrameObjOffset, true);
3428 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
3429 InVals.push_back(FIN);
3430
3431 if (!ByVal.NumRegs)
3432 return;
3433
3434 // Copy arg registers.
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00003435 MVT RegTy = MVT::getIntegerVT(CC.regSize() * 8);
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003436 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3437
3438 for (unsigned I = 0; I < ByVal.NumRegs; ++I) {
3439 unsigned ArgReg = CC.intArgRegs()[ByVal.FirstIdx + I];
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003440 unsigned VReg = addLiveIn(MF, ArgReg, RC);
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003441 unsigned Offset = I * CC.regSize();
3442 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
3443 DAG.getConstant(Offset, PtrTy));
3444 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
3445 StorePtr, MachinePointerInfo(FuncArg, Offset),
3446 false, false, 0);
3447 OutChains.push_back(Store);
3448 }
3449}
Akira Hatanakadb40ede2012-10-27 00:16:36 +00003450
3451// Copy byVal arg to registers and stack.
3452void MipsTargetLowering::
Andrew Trickac6d9be2013-05-25 02:42:55 +00003453passByValArg(SDValue Chain, SDLoc DL,
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00003454 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
Craig Toppera0ec3f92013-07-14 04:42:23 +00003455 SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr,
Akira Hatanakadb40ede2012-10-27 00:16:36 +00003456 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
3457 const MipsCC &CC, const ByValArgInfo &ByVal,
3458 const ISD::ArgFlagsTy &Flags, bool isLittle) const {
3459 unsigned ByValSize = Flags.getByValSize();
3460 unsigned Offset = 0; // Offset in # of bytes from the beginning of struct.
3461 unsigned RegSize = CC.regSize();
3462 unsigned Alignment = std::min(Flags.getByValAlign(), RegSize);
3463 EVT PtrTy = getPointerTy(), RegTy = MVT::getIntegerVT(RegSize * 8);
3464
3465 if (ByVal.NumRegs) {
3466 const uint16_t *ArgRegs = CC.intArgRegs();
3467 bool LeftoverBytes = (ByVal.NumRegs * RegSize > ByValSize);
3468 unsigned I = 0;
3469
3470 // Copy words to registers.
3471 for (; I < ByVal.NumRegs - LeftoverBytes; ++I, Offset += RegSize) {
3472 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3473 DAG.getConstant(Offset, PtrTy));
3474 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
3475 MachinePointerInfo(), false, false, false,
3476 Alignment);
3477 MemOpChains.push_back(LoadVal.getValue(1));
3478 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3479 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
3480 }
3481
3482 // Return if the struct has been fully copied.
3483 if (ByValSize == Offset)
3484 return;
3485
3486 // Copy the remainder of the byval argument with sub-word loads and shifts.
3487 if (LeftoverBytes) {
3488 assert((ByValSize > Offset) && (ByValSize < Offset + RegSize) &&
3489 "Size of the remainder should be smaller than RegSize.");
3490 SDValue Val;
3491
3492 for (unsigned LoadSize = RegSize / 2, TotalSizeLoaded = 0;
3493 Offset < ByValSize; LoadSize /= 2) {
3494 unsigned RemSize = ByValSize - Offset;
3495
3496 if (RemSize < LoadSize)
3497 continue;
3498
3499 // Load subword.
3500 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3501 DAG.getConstant(Offset, PtrTy));
3502 SDValue LoadVal =
3503 DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr,
3504 MachinePointerInfo(), MVT::getIntegerVT(LoadSize * 8),
3505 false, false, Alignment);
3506 MemOpChains.push_back(LoadVal.getValue(1));
3507
3508 // Shift the loaded value.
3509 unsigned Shamt;
3510
3511 if (isLittle)
3512 Shamt = TotalSizeLoaded;
3513 else
3514 Shamt = (RegSize - (TotalSizeLoaded + LoadSize)) * 8;
3515
3516 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
3517 DAG.getConstant(Shamt, MVT::i32));
3518
3519 if (Val.getNode())
3520 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
3521 else
3522 Val = Shift;
3523
3524 Offset += LoadSize;
3525 TotalSizeLoaded += LoadSize;
3526 Alignment = std::min(Alignment, LoadSize);
3527 }
3528
3529 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3530 RegsToPass.push_back(std::make_pair(ArgReg, Val));
3531 return;
3532 }
3533 }
3534
3535 // Copy remainder of byval arg to it with memcpy.
3536 unsigned MemCpySize = ByValSize - Offset;
3537 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3538 DAG.getConstant(Offset, PtrTy));
3539 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
3540 DAG.getIntPtrConstant(ByVal.Address));
3541 Chain = DAG.getMemcpy(Chain, DL, Dst, Src,
3542 DAG.getConstant(MemCpySize, PtrTy), Alignment,
3543 /*isVolatile=*/false, /*AlwaysInline=*/false,
3544 MachinePointerInfo(0), MachinePointerInfo(0));
3545 MemOpChains.push_back(Chain);
3546}
Akira Hatanakaf0848472012-10-27 00:21:13 +00003547
3548void
3549MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
3550 const MipsCC &CC, SDValue Chain,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003551 SDLoc DL, SelectionDAG &DAG) const {
Akira Hatanakaf0848472012-10-27 00:21:13 +00003552 unsigned NumRegs = CC.numIntArgRegs();
3553 const uint16_t *ArgRegs = CC.intArgRegs();
3554 const CCState &CCInfo = CC.getCCInfo();
3555 unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs, NumRegs);
3556 unsigned RegSize = CC.regSize();
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00003557 MVT RegTy = MVT::getIntegerVT(RegSize * 8);
Akira Hatanakaf0848472012-10-27 00:21:13 +00003558 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3559 MachineFunction &MF = DAG.getMachineFunction();
3560 MachineFrameInfo *MFI = MF.getFrameInfo();
3561 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3562
3563 // Offset of the first variable argument from stack pointer.
3564 int VaArgOffset;
3565
3566 if (NumRegs == Idx)
3567 VaArgOffset = RoundUpToAlignment(CCInfo.getNextStackOffset(), RegSize);
3568 else
3569 VaArgOffset =
3570 (int)CC.reservedArgArea() - (int)(RegSize * (NumRegs - Idx));
3571
3572 // Record the frame index of the first variable argument
3573 // which is a value necessary to VASTART.
3574 int FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
3575 MipsFI->setVarArgsFrameIndex(FI);
3576
3577 // Copy the integer registers that have not been used for argument passing
3578 // to the argument register save area. For O32, the save area is allocated
3579 // in the caller's stack frame, while for N32/64, it is allocated in the
3580 // callee's stack frame.
3581 for (unsigned I = Idx; I < NumRegs; ++I, VaArgOffset += RegSize) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003582 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC);
Akira Hatanakaf0848472012-10-27 00:21:13 +00003583 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
3584 FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
3585 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
3586 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
3587 MachinePointerInfo(), false, false, 0);
3588 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(0);
3589 OutChains.push_back(Store);
3590 }
3591}