blob: 2a841b925cee025274d917aa325374958ba87d51 [file] [log] [blame]
Jia Liubb481f82012-02-28 07:46:26 +00001//===-- MipsISelLowering.cpp - Mips DAG Lowering Implementation -----------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00007//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00009//
10// This file defines the interfaces that Mips uses to lower LLVM code into a
11// selection DAG.
12//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000013//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000014#define DEBUG_TYPE "mips-lower"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000015#include "MipsISelLowering.h"
Craig Topper79aa3412012-03-17 18:46:09 +000016#include "InstPrinter/MipsInstPrinter.h"
17#include "MCTargetDesc/MipsBaseInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000018#include "MipsMachineFunction.h"
19#include "MipsSubtarget.h"
20#include "MipsTargetMachine.h"
21#include "MipsTargetObjectFile.h"
Akira Hatanaka2b861be2012-10-19 21:47:33 +000022#include "llvm/ADT/Statistic.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000023#include "llvm/CodeGen/CallingConvLower.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/MachineFunction.h"
26#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000027#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000028#include "llvm/CodeGen/SelectionDAGISel.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000029#include "llvm/CodeGen/ValueTypes.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000030#include "llvm/IR/CallingConv.h"
31#include "llvm/IR/DerivedTypes.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000032#include "llvm/IR/GlobalVariable.h"
Akira Hatanaka2b861be2012-10-19 21:47:33 +000033#include "llvm/Support/CommandLine.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000034#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000035#include "llvm/Support/ErrorHandling.h"
NAKAMURA Takumi89593932012-04-21 15:31:45 +000036#include "llvm/Support/raw_ostream.h"
Akira Hatanakabfb07b12013-08-14 00:21:25 +000037#include <cctype>
NAKAMURA Takumi89593932012-04-21 15:31:45 +000038
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000039using namespace llvm;
40
Akira Hatanaka2b861be2012-10-19 21:47:33 +000041STATISTIC(NumTailCalls, "Number of tail calls");
42
43static cl::opt<bool>
Akira Hatanaka81784cb2012-11-21 20:21:11 +000044LargeGOT("mxgot", cl::Hidden,
45 cl::desc("MIPS: Enable GOT larger than 64k."), cl::init(false));
46
Akira Hatanakaf8941992013-05-20 18:07:43 +000047static cl::opt<bool>
Akira Hatanaka2591b5c2013-05-21 17:17:59 +000048NoZeroDivCheck("mno-check-zero-division", cl::Hidden,
Akira Hatanakaf8941992013-05-20 18:07:43 +000049 cl::desc("MIPS: Don't trap on integer division by zero."),
50 cl::init(false));
51
Akira Hatanakafe30a9b2012-10-27 00:29:43 +000052static const uint16_t O32IntRegs[4] = {
53 Mips::A0, Mips::A1, Mips::A2, Mips::A3
54};
55
56static const uint16_t Mips64IntRegs[8] = {
57 Mips::A0_64, Mips::A1_64, Mips::A2_64, Mips::A3_64,
58 Mips::T0_64, Mips::T1_64, Mips::T2_64, Mips::T3_64
59};
60
61static const uint16_t Mips64DPRegs[8] = {
62 Mips::D12_64, Mips::D13_64, Mips::D14_64, Mips::D15_64,
63 Mips::D16_64, Mips::D17_64, Mips::D18_64, Mips::D19_64
64};
65
Jia Liubb481f82012-02-28 07:46:26 +000066// If I is a shifted mask, set the size (Size) and the first bit of the
Akira Hatanakadbe9a312011-08-18 20:07:42 +000067// mask (Pos), and return true.
Jia Liubb481f82012-02-28 07:46:26 +000068// For example, if I is 0x003ff800, (Pos, Size) = (11, 11).
Akira Hatanakaf635ef42013-03-12 00:16:36 +000069static bool isShiftedMask(uint64_t I, uint64_t &Pos, uint64_t &Size) {
Akira Hatanakad6bc5232011-12-05 21:26:34 +000070 if (!isShiftedMask_64(I))
Akira Hatanaka854a7db2011-08-19 22:59:00 +000071 return false;
Akira Hatanakabb15e112011-08-17 02:05:42 +000072
Akira Hatanakad6bc5232011-12-05 21:26:34 +000073 Size = CountPopulation_64(I);
Michael J. Spencerc6af2432013-05-24 22:23:49 +000074 Pos = countTrailingZeros(I);
Akira Hatanakadbe9a312011-08-18 20:07:42 +000075 return true;
Akira Hatanakabb15e112011-08-17 02:05:42 +000076}
77
Akira Hatanaka5ac065a2013-03-13 00:54:29 +000078SDValue MipsTargetLowering::getGlobalReg(SelectionDAG &DAG, EVT Ty) const {
Akira Hatanaka648f00c2012-02-24 22:34:47 +000079 MipsFunctionInfo *FI = DAG.getMachineFunction().getInfo<MipsFunctionInfo>();
80 return DAG.getRegister(FI->getGlobalBaseReg(), Ty);
81}
82
Akira Hatanaka6b28b802012-11-21 20:26:38 +000083static SDValue getTargetNode(SDValue Op, SelectionDAG &DAG, unsigned Flag) {
84 EVT Ty = Op.getValueType();
85
86 if (GlobalAddressSDNode *N = dyn_cast<GlobalAddressSDNode>(Op))
Andrew Trickac6d9be2013-05-25 02:42:55 +000087 return DAG.getTargetGlobalAddress(N->getGlobal(), SDLoc(Op), Ty, 0,
Akira Hatanaka6b28b802012-11-21 20:26:38 +000088 Flag);
89 if (ExternalSymbolSDNode *N = dyn_cast<ExternalSymbolSDNode>(Op))
90 return DAG.getTargetExternalSymbol(N->getSymbol(), Ty, Flag);
91 if (BlockAddressSDNode *N = dyn_cast<BlockAddressSDNode>(Op))
92 return DAG.getTargetBlockAddress(N->getBlockAddress(), Ty, 0, Flag);
93 if (JumpTableSDNode *N = dyn_cast<JumpTableSDNode>(Op))
94 return DAG.getTargetJumpTable(N->getIndex(), Ty, Flag);
95 if (ConstantPoolSDNode *N = dyn_cast<ConstantPoolSDNode>(Op))
96 return DAG.getTargetConstantPool(N->getConstVal(), Ty, N->getAlignment(),
97 N->getOffset(), Flag);
98
99 llvm_unreachable("Unexpected node type.");
100 return SDValue();
101}
102
103static SDValue getAddrNonPIC(SDValue Op, SelectionDAG &DAG) {
Andrew Trickac6d9be2013-05-25 02:42:55 +0000104 SDLoc DL(Op);
Akira Hatanaka6b28b802012-11-21 20:26:38 +0000105 EVT Ty = Op.getValueType();
106 SDValue Hi = getTargetNode(Op, DAG, MipsII::MO_ABS_HI);
107 SDValue Lo = getTargetNode(Op, DAG, MipsII::MO_ABS_LO);
108 return DAG.getNode(ISD::ADD, DL, Ty,
109 DAG.getNode(MipsISD::Hi, DL, Ty, Hi),
110 DAG.getNode(MipsISD::Lo, DL, Ty, Lo));
111}
112
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000113SDValue MipsTargetLowering::getAddrLocal(SDValue Op, SelectionDAG &DAG,
114 bool HasMips64) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +0000115 SDLoc DL(Op);
Akira Hatanaka6b28b802012-11-21 20:26:38 +0000116 EVT Ty = Op.getValueType();
117 unsigned GOTFlag = HasMips64 ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT;
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000118 SDValue GOT = DAG.getNode(MipsISD::Wrapper, DL, Ty, getGlobalReg(DAG, Ty),
Akira Hatanaka6b28b802012-11-21 20:26:38 +0000119 getTargetNode(Op, DAG, GOTFlag));
120 SDValue Load = DAG.getLoad(Ty, DL, DAG.getEntryNode(), GOT,
121 MachinePointerInfo::getGOT(), false, false, false,
122 0);
123 unsigned LoFlag = HasMips64 ? MipsII::MO_GOT_OFST : MipsII::MO_ABS_LO;
124 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, Ty, getTargetNode(Op, DAG, LoFlag));
125 return DAG.getNode(ISD::ADD, DL, Ty, Load, Lo);
126}
127
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000128SDValue MipsTargetLowering::getAddrGlobal(SDValue Op, SelectionDAG &DAG,
129 unsigned Flag) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +0000130 SDLoc DL(Op);
Akira Hatanaka6b28b802012-11-21 20:26:38 +0000131 EVT Ty = Op.getValueType();
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000132 SDValue Tgt = DAG.getNode(MipsISD::Wrapper, DL, Ty, getGlobalReg(DAG, Ty),
Akira Hatanaka6b28b802012-11-21 20:26:38 +0000133 getTargetNode(Op, DAG, Flag));
134 return DAG.getLoad(Ty, DL, DAG.getEntryNode(), Tgt,
135 MachinePointerInfo::getGOT(), false, false, false, 0);
136}
137
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000138SDValue MipsTargetLowering::getAddrGlobalLargeGOT(SDValue Op, SelectionDAG &DAG,
139 unsigned HiFlag,
140 unsigned LoFlag) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +0000141 SDLoc DL(Op);
Akira Hatanaka6b28b802012-11-21 20:26:38 +0000142 EVT Ty = Op.getValueType();
143 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, Ty, getTargetNode(Op, DAG, HiFlag));
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000144 Hi = DAG.getNode(ISD::ADD, DL, Ty, Hi, getGlobalReg(DAG, Ty));
Akira Hatanaka6b28b802012-11-21 20:26:38 +0000145 SDValue Wrapper = DAG.getNode(MipsISD::Wrapper, DL, Ty, Hi,
146 getTargetNode(Op, DAG, LoFlag));
147 return DAG.getLoad(Ty, DL, DAG.getEntryNode(), Wrapper,
148 MachinePointerInfo::getGOT(), false, false, false, 0);
149}
150
Chris Lattnerf0144122009-07-28 03:13:23 +0000151const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
152 switch (Opcode) {
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000153 case MipsISD::JmpLink: return "MipsISD::JmpLink";
Akira Hatanaka58d1e3f2012-10-19 20:59:39 +0000154 case MipsISD::TailCall: return "MipsISD::TailCall";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000155 case MipsISD::Hi: return "MipsISD::Hi";
156 case MipsISD::Lo: return "MipsISD::Lo";
157 case MipsISD::GPRel: return "MipsISD::GPRel";
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +0000158 case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000159 case MipsISD::Ret: return "MipsISD::Ret";
Akira Hatanaka544cc212013-01-30 00:26:49 +0000160 case MipsISD::EH_RETURN: return "MipsISD::EH_RETURN";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000161 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
162 case MipsISD::FPCmp: return "MipsISD::FPCmp";
163 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
164 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +0000165 case MipsISD::TruncIntFP: return "MipsISD::TruncIntFP";
Akira Hatanakadd958922013-03-30 01:14:04 +0000166 case MipsISD::ExtractLOHI: return "MipsISD::ExtractLOHI";
167 case MipsISD::InsertLOHI: return "MipsISD::InsertLOHI";
168 case MipsISD::Mult: return "MipsISD::Mult";
169 case MipsISD::Multu: return "MipsISD::Multu";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000170 case MipsISD::MAdd: return "MipsISD::MAdd";
171 case MipsISD::MAddu: return "MipsISD::MAddu";
172 case MipsISD::MSub: return "MipsISD::MSub";
173 case MipsISD::MSubu: return "MipsISD::MSubu";
174 case MipsISD::DivRem: return "MipsISD::DivRem";
175 case MipsISD::DivRemU: return "MipsISD::DivRemU";
Akira Hatanakadd958922013-03-30 01:14:04 +0000176 case MipsISD::DivRem16: return "MipsISD::DivRem16";
177 case MipsISD::DivRemU16: return "MipsISD::DivRemU16";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000178 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
179 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
Akira Hatanakabfcb83f2011-12-12 22:38:19 +0000180 case MipsISD::Wrapper: return "MipsISD::Wrapper";
Akira Hatanakadb548262011-07-19 23:30:50 +0000181 case MipsISD::Sync: return "MipsISD::Sync";
Akira Hatanakabb15e112011-08-17 02:05:42 +0000182 case MipsISD::Ext: return "MipsISD::Ext";
183 case MipsISD::Ins: return "MipsISD::Ins";
Akira Hatanakab6f1dc22012-06-02 00:03:12 +0000184 case MipsISD::LWL: return "MipsISD::LWL";
185 case MipsISD::LWR: return "MipsISD::LWR";
186 case MipsISD::SWL: return "MipsISD::SWL";
187 case MipsISD::SWR: return "MipsISD::SWR";
188 case MipsISD::LDL: return "MipsISD::LDL";
189 case MipsISD::LDR: return "MipsISD::LDR";
190 case MipsISD::SDL: return "MipsISD::SDL";
191 case MipsISD::SDR: return "MipsISD::SDR";
Akira Hatanaka6fad5e72012-09-21 23:52:47 +0000192 case MipsISD::EXTP: return "MipsISD::EXTP";
193 case MipsISD::EXTPDP: return "MipsISD::EXTPDP";
194 case MipsISD::EXTR_S_H: return "MipsISD::EXTR_S_H";
195 case MipsISD::EXTR_W: return "MipsISD::EXTR_W";
196 case MipsISD::EXTR_R_W: return "MipsISD::EXTR_R_W";
197 case MipsISD::EXTR_RS_W: return "MipsISD::EXTR_RS_W";
198 case MipsISD::SHILO: return "MipsISD::SHILO";
199 case MipsISD::MTHLIP: return "MipsISD::MTHLIP";
200 case MipsISD::MULT: return "MipsISD::MULT";
201 case MipsISD::MULTU: return "MipsISD::MULTU";
Jia Liub3ea8802013-03-04 01:06:54 +0000202 case MipsISD::MADD_DSP: return "MipsISD::MADD_DSP";
Akira Hatanaka6fad5e72012-09-21 23:52:47 +0000203 case MipsISD::MADDU_DSP: return "MipsISD::MADDU_DSP";
204 case MipsISD::MSUB_DSP: return "MipsISD::MSUB_DSP";
205 case MipsISD::MSUBU_DSP: return "MipsISD::MSUBU_DSP";
Akira Hatanaka97a62bf2013-04-19 23:21:32 +0000206 case MipsISD::SHLL_DSP: return "MipsISD::SHLL_DSP";
207 case MipsISD::SHRA_DSP: return "MipsISD::SHRA_DSP";
208 case MipsISD::SHRL_DSP: return "MipsISD::SHRL_DSP";
Akira Hatanakacd6c5792013-04-30 22:37:26 +0000209 case MipsISD::SETCC_DSP: return "MipsISD::SETCC_DSP";
210 case MipsISD::SELECT_CC_DSP: return "MipsISD::SELECT_CC_DSP";
Daniel Sanders3c380d52013-08-28 12:14:50 +0000211 case MipsISD::VALL_ZERO: return "MipsISD::VALL_ZERO";
212 case MipsISD::VANY_ZERO: return "MipsISD::VANY_ZERO";
213 case MipsISD::VALL_NONZERO: return "MipsISD::VALL_NONZERO";
214 case MipsISD::VANY_NONZERO: return "MipsISD::VANY_NONZERO";
Daniel Sandersae1fb8f2013-09-24 10:46:19 +0000215 case MipsISD::VCEQ: return "MipsISD::VCEQ";
216 case MipsISD::VCLE_S: return "MipsISD::VCLE_S";
217 case MipsISD::VCLE_U: return "MipsISD::VCLE_U";
218 case MipsISD::VCLT_S: return "MipsISD::VCLT_S";
219 case MipsISD::VCLT_U: return "MipsISD::VCLT_U";
Daniel Sanders89d13c12013-09-24 12:18:31 +0000220 case MipsISD::VSMAX: return "MipsISD::VSMAX";
221 case MipsISD::VSMIN: return "MipsISD::VSMIN";
222 case MipsISD::VUMAX: return "MipsISD::VUMAX";
223 case MipsISD::VUMIN: return "MipsISD::VUMIN";
Daniel Sandersda521cc2013-09-23 12:02:46 +0000224 case MipsISD::VSPLAT: return "MipsISD::VSPLAT";
225 case MipsISD::VSPLATD: return "MipsISD::VSPLATD";
Daniel Sanders9a1aaeb2013-09-23 14:03:12 +0000226 case MipsISD::VEXTRACT_SEXT_ELT: return "MipsISD::VEXTRACT_SEXT_ELT";
227 case MipsISD::VEXTRACT_ZEXT_ELT: return "MipsISD::VEXTRACT_ZEXT_ELT";
Daniel Sanders915432c2013-09-23 13:22:24 +0000228 case MipsISD::VNOR: return "MipsISD::VNOR";
Akira Hatanaka0f843822011-06-07 18:58:42 +0000229 default: return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000230 }
231}
232
233MipsTargetLowering::
Chris Lattnerf0144122009-07-28 03:13:23 +0000234MipsTargetLowering(MipsTargetMachine &TM)
Akira Hatanaka8b4198d2011-09-26 21:47:02 +0000235 : TargetLowering(TM, new MipsTargetObjectFile()),
236 Subtarget(&TM.getSubtarget<MipsSubtarget>()),
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +0000237 HasMips64(Subtarget->hasMips64()), IsN64(Subtarget->isABI_N64()),
238 IsO32(Subtarget->isABI_O32()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000239 // Mips does not have i1 type, so use i32 for
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000240 // setcc operations results (slt, sgt, ...).
Duncan Sands03228082008-11-23 15:47:28 +0000241 setBooleanContents(ZeroOrOneBooleanContent);
Akira Hatanakacd6c5792013-04-30 22:37:26 +0000242 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000243
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000244 // Load extented operations for i1 types must be promoted
Owen Anderson825b72b2009-08-11 20:47:22 +0000245 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
246 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
247 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000248
Eli Friedman6055a6a2009-07-17 04:07:24 +0000249 // MIPS doesn't have extending float->double load/store
Owen Anderson825b72b2009-08-11 20:47:22 +0000250 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
251 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman10a36592009-07-17 02:28:12 +0000252
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000253 // Used by legalize types to correctly generate the setcc result.
254 // Without this, every float setcc comes with a AND/OR with the result,
255 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000256 // which is used implicitly by brcond and select operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000257 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000258
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000259 // Mips Custom Operations
Akira Hatanakab7656a92013-03-06 21:32:03 +0000260 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000261 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000262 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000263 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
264 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
265 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
266 setOperationAction(ISD::SELECT, MVT::f32, Custom);
267 setOperationAction(ISD::SELECT, MVT::f64, Custom);
268 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Akira Hatanaka3fef29d2012-07-11 19:32:27 +0000269 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
270 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Akira Hatanaka0a40c232012-03-09 23:46:03 +0000271 setOperationAction(ISD::SETCC, MVT::f32, Custom);
272 setOperationAction(ISD::SETCC, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000273 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000274 setOperationAction(ISD::VASTART, MVT::Other, Custom);
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000275 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
276 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +0000277 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000278
Akira Hatanakac12a6e62012-04-11 22:49:04 +0000279 if (!TM.Options.NoNaNsFPMath) {
280 setOperationAction(ISD::FABS, MVT::f32, Custom);
281 setOperationAction(ISD::FABS, MVT::f64, Custom);
282 }
283
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000284 if (HasMips64) {
285 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
286 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
287 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
288 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
289 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
290 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Akira Hatanaka7664f052012-06-02 00:04:42 +0000291 setOperationAction(ISD::LOAD, MVT::i64, Custom);
292 setOperationAction(ISD::STORE, MVT::i64, Custom);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +0000293 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000294 }
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000295
Akira Hatanakaa284acb2012-05-09 00:55:21 +0000296 if (!HasMips64) {
297 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
298 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
299 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
300 }
301
Akira Hatanakae90a3bc2012-11-07 19:10:58 +0000302 setOperationAction(ISD::ADD, MVT::i32, Custom);
303 if (HasMips64)
304 setOperationAction(ISD::ADD, MVT::i64, Custom);
305
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000306 setOperationAction(ISD::SDIV, MVT::i32, Expand);
307 setOperationAction(ISD::SREM, MVT::i32, Expand);
308 setOperationAction(ISD::UDIV, MVT::i32, Expand);
309 setOperationAction(ISD::UREM, MVT::i32, Expand);
Akira Hatanakadda4a072011-10-03 21:06:13 +0000310 setOperationAction(ISD::SDIV, MVT::i64, Expand);
311 setOperationAction(ISD::SREM, MVT::i64, Expand);
312 setOperationAction(ISD::UDIV, MVT::i64, Expand);
313 setOperationAction(ISD::UREM, MVT::i64, Expand);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000314
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000315 // Operations not directly supported by Mips.
Tom Stellard3ef53832013-03-08 15:36:57 +0000316 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
317 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
318 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
319 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000320 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
321 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Akira Hatanakae1bcd6b2011-12-20 23:40:56 +0000322 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000323 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Akira Hatanakae1bcd6b2011-12-20 23:40:56 +0000324 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000325 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
326 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
Akira Hatanaka7f162742011-12-21 00:14:05 +0000327 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000328 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
Akira Hatanaka7f162742011-12-21 00:14:05 +0000329 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000330 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
331 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
332 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
333 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000334 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000335 setOperationAction(ISD::ROTL, MVT::i64, Expand);
Akira Hatanaka1d165f12012-07-31 20:54:48 +0000336 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
337 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000338
Akira Hatanaka56633442011-09-20 23:53:09 +0000339 if (!Subtarget->hasMips32r2())
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000340 setOperationAction(ISD::ROTR, MVT::i32, Expand);
341
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000342 if (!Subtarget->hasMips64r2())
343 setOperationAction(ISD::ROTR, MVT::i64, Expand);
344
Owen Anderson825b72b2009-08-11 20:47:22 +0000345 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000346 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000347 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000348 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng8688a582013-01-29 02:32:37 +0000349 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
350 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000351 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
352 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanaka46da1362011-05-23 22:23:58 +0000353 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000354 setOperationAction(ISD::FLOG, MVT::f32, Expand);
355 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
356 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
357 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000358 setOperationAction(ISD::FMA, MVT::f32, Expand);
359 setOperationAction(ISD::FMA, MVT::f64, Expand);
Akira Hatanaka21ecc2f2012-03-29 18:43:11 +0000360 setOperationAction(ISD::FREM, MVT::f32, Expand);
361 setOperationAction(ISD::FREM, MVT::f64, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000362
Akira Hatanaka1cc63332012-04-11 22:59:08 +0000363 if (!TM.Options.NoNaNsFPMath) {
364 setOperationAction(ISD::FNEG, MVT::f32, Expand);
365 setOperationAction(ISD::FNEG, MVT::f64, Expand);
366 }
367
Akira Hatanaka544cc212013-01-30 00:26:49 +0000368 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
369
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +0000370 setOperationAction(ISD::VAARG, MVT::Other, Expand);
371 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
372 setOperationAction(ISD::VAEND, MVT::Other, Expand);
373
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000374 // Use the default for now
Owen Anderson825b72b2009-08-11 20:47:22 +0000375 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
376 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000377
Jia Liubb481f82012-02-28 07:46:26 +0000378 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
379 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
380 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
381 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
Eli Friedman4db5aca2011-08-29 18:23:02 +0000382
Eli Friedman26689ac2011-08-03 21:06:02 +0000383 setInsertFencesForAtomic(true);
384
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000385 if (!Subtarget->hasSEInReg()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000386 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
387 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000388 }
389
Akira Hatanakac79507a2011-12-21 00:20:27 +0000390 if (!Subtarget->hasBitCount()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000391 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Akira Hatanakac79507a2011-12-21 00:20:27 +0000392 setOperationAction(ISD::CTLZ, MVT::i64, Expand);
393 }
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000394
Akira Hatanakac0ea0432011-12-20 23:56:43 +0000395 if (!Subtarget->hasSwap()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000396 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Akira Hatanakac0ea0432011-12-20 23:56:43 +0000397 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
398 }
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000399
Akira Hatanaka7664f052012-06-02 00:04:42 +0000400 if (HasMips64) {
401 setLoadExtAction(ISD::SEXTLOAD, MVT::i32, Custom);
402 setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, Custom);
403 setLoadExtAction(ISD::EXTLOAD, MVT::i32, Custom);
404 setTruncStoreAction(MVT::i64, MVT::i32, Custom);
405 }
406
Akira Hatanaka97585622013-07-26 20:58:55 +0000407 setOperationAction(ISD::TRAP, MVT::Other, Legal);
408
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000409 setTargetDAGCombine(ISD::SDIVREM);
410 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanakaee8c3b02012-03-08 03:26:37 +0000411 setTargetDAGCombine(ISD::SELECT);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000412 setTargetDAGCombine(ISD::AND);
413 setTargetDAGCombine(ISD::OR);
Akira Hatanaka87827072012-06-13 20:33:18 +0000414 setTargetDAGCombine(ISD::ADD);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000415
Akira Hatanaka5fdf5002012-03-08 01:59:33 +0000416 setMinFunctionAlignment(HasMips64 ? 3 : 2);
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000417
Akira Hatanaka3f5b1072012-02-02 03:17:04 +0000418 setStackPointerRegisterToSaveRestore(IsN64 ? Mips::SP_64 : Mips::SP);
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000419
Akira Hatanaka590baca2012-02-02 03:13:40 +0000420 setExceptionPointerRegister(IsN64 ? Mips::A0_64 : Mips::A0);
421 setExceptionSelectorRegister(IsN64 ? Mips::A1_64 : Mips::A1);
Akira Hatanakae193b322012-06-13 19:33:32 +0000422
Jim Grosbach3450f802013-02-20 21:13:59 +0000423 MaxStoresPerMemcpy = 16;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000424}
425
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000426const MipsTargetLowering *MipsTargetLowering::create(MipsTargetMachine &TM) {
427 if (TM.getSubtargetImpl()->inMips16Mode())
428 return llvm::createMips16TargetLowering(TM);
Jia Liubb481f82012-02-28 07:46:26 +0000429
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000430 return llvm::createMipsSETargetLowering(TM);
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000431}
432
Matt Arsenault225ed702013-05-18 00:21:46 +0000433EVT MipsTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Akira Hatanakae13f4412013-01-04 20:06:01 +0000434 if (!VT.isVector())
435 return MVT::i32;
436 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +0000437}
438
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000439static SDValue performDivRemCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000440 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000441 const MipsSubtarget *Subtarget) {
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000442 if (DCI.isBeforeLegalizeOps())
443 return SDValue();
444
Akira Hatanakadda4a072011-10-03 21:06:13 +0000445 EVT Ty = N->getValueType(0);
Akira Hatanakacbaf6d02013-08-14 00:47:08 +0000446 unsigned LO = (Ty == MVT::i32) ? Mips::LO0 : Mips::LO0_64;
447 unsigned HI = (Ty == MVT::i32) ? Mips::HI0 : Mips::HI0_64;
Akira Hatanakaf5926fd2013-03-30 01:36:35 +0000448 unsigned Opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem16 :
449 MipsISD::DivRemU16;
Andrew Trickac6d9be2013-05-25 02:42:55 +0000450 SDLoc DL(N);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000451
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000452 SDValue DivRem = DAG.getNode(Opc, DL, MVT::Glue,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000453 N->getOperand(0), N->getOperand(1));
454 SDValue InChain = DAG.getEntryNode();
455 SDValue InGlue = DivRem;
456
457 // insert MFLO
458 if (N->hasAnyUseOfValue(0)) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000459 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, DL, LO, Ty,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000460 InGlue);
461 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
462 InChain = CopyFromLo.getValue(1);
463 InGlue = CopyFromLo.getValue(2);
464 }
465
466 // insert MFHI
467 if (N->hasAnyUseOfValue(1)) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000468 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, DL,
Akira Hatanakadda4a072011-10-03 21:06:13 +0000469 HI, Ty, InGlue);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000470 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
471 }
472
473 return SDValue();
474}
475
Akira Hatanaka2fbe90c2013-04-18 01:00:46 +0000476static Mips::CondCode condCodeToFCC(ISD::CondCode CC) {
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000477 switch (CC) {
478 default: llvm_unreachable("Unknown fp condition code!");
479 case ISD::SETEQ:
480 case ISD::SETOEQ: return Mips::FCOND_OEQ;
481 case ISD::SETUNE: return Mips::FCOND_UNE;
482 case ISD::SETLT:
483 case ISD::SETOLT: return Mips::FCOND_OLT;
484 case ISD::SETGT:
485 case ISD::SETOGT: return Mips::FCOND_OGT;
486 case ISD::SETLE:
487 case ISD::SETOLE: return Mips::FCOND_OLE;
488 case ISD::SETGE:
489 case ISD::SETOGE: return Mips::FCOND_OGE;
490 case ISD::SETULT: return Mips::FCOND_ULT;
491 case ISD::SETULE: return Mips::FCOND_ULE;
492 case ISD::SETUGT: return Mips::FCOND_UGT;
493 case ISD::SETUGE: return Mips::FCOND_UGE;
494 case ISD::SETUO: return Mips::FCOND_UN;
495 case ISD::SETO: return Mips::FCOND_OR;
496 case ISD::SETNE:
497 case ISD::SETONE: return Mips::FCOND_ONE;
498 case ISD::SETUEQ: return Mips::FCOND_UEQ;
499 }
500}
501
502
Akira Hatanaka9cf07242013-03-30 01:16:38 +0000503/// This function returns true if the floating point conditional branches and
504/// conditional moves which use condition code CC should be inverted.
505static bool invertFPCondCodeUser(Mips::CondCode CC) {
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000506 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
507 return false;
508
Akira Hatanaka82099682011-12-19 19:52:25 +0000509 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
510 "Illegal Condition Code");
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000511
Akira Hatanaka82099682011-12-19 19:52:25 +0000512 return true;
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000513}
514
515// Creates and returns an FPCmp node from a setcc node.
516// Returns Op if setcc is not a floating point comparison.
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000517static SDValue createFPCmp(SelectionDAG &DAG, const SDValue &Op) {
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000518 // must be a SETCC node
519 if (Op.getOpcode() != ISD::SETCC)
520 return Op;
521
522 SDValue LHS = Op.getOperand(0);
523
524 if (!LHS.getValueType().isFloatingPoint())
525 return Op;
526
527 SDValue RHS = Op.getOperand(1);
Andrew Trickac6d9be2013-05-25 02:42:55 +0000528 SDLoc DL(Op);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000529
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000530 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
531 // node if necessary.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000532 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
533
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000534 return DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS,
Akira Hatanaka2fbe90c2013-04-18 01:00:46 +0000535 DAG.getConstant(condCodeToFCC(CC), MVT::i32));
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000536}
537
538// Creates and returns a CMovFPT/F node.
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000539static SDValue createCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000540 SDValue False, SDLoc DL) {
Akira Hatanaka9cf07242013-03-30 01:16:38 +0000541 ConstantSDNode *CC = cast<ConstantSDNode>(Cond.getOperand(2));
542 bool invert = invertFPCondCodeUser((Mips::CondCode)CC->getSExtValue());
Akira Hatanaka407883b2013-07-26 20:51:20 +0000543 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000544
545 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
Akira Hatanaka407883b2013-07-26 20:51:20 +0000546 True.getValueType(), True, FCC0, False, Cond);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000547}
548
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000549static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000550 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000551 const MipsSubtarget *Subtarget) {
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000552 if (DCI.isBeforeLegalizeOps())
553 return SDValue();
554
555 SDValue SetCC = N->getOperand(0);
556
557 if ((SetCC.getOpcode() != ISD::SETCC) ||
558 !SetCC.getOperand(0).getValueType().isInteger())
559 return SDValue();
560
561 SDValue False = N->getOperand(2);
562 EVT FalseTy = False.getValueType();
563
564 if (!FalseTy.isInteger())
565 return SDValue();
566
567 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(False);
568
569 if (!CN || CN->getZExtValue())
570 return SDValue();
571
Andrew Trickac6d9be2013-05-25 02:42:55 +0000572 const SDLoc DL(N);
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000573 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
574 SDValue True = N->getOperand(1);
Akira Hatanaka864f6602012-06-14 21:10:56 +0000575
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000576 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
577 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
Akira Hatanaka864f6602012-06-14 21:10:56 +0000578
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000579 return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True);
580}
581
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000582static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000583 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000584 const MipsSubtarget *Subtarget) {
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000585 // Pattern match EXT.
586 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
587 // => ext $dst, $src, size, pos
Akira Hatanaka56633442011-09-20 23:53:09 +0000588 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000589 return SDValue();
590
591 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000592 unsigned ShiftRightOpc = ShiftRight.getOpcode();
593
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000594 // Op's first operand must be a shift right.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000595 if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL)
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000596 return SDValue();
597
598 // The second operand of the shift must be an immediate.
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000599 ConstantSDNode *CN;
600 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
601 return SDValue();
Jia Liubb481f82012-02-28 07:46:26 +0000602
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000603 uint64_t Pos = CN->getZExtValue();
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000604 uint64_t SMPos, SMSize;
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000605
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000606 // Op's second operand must be a shifted mask.
607 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000608 !isShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000609 return SDValue();
610
611 // Return if the shifted mask does not start at bit 0 or the sum of its size
612 // and Pos exceeds the word's size.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000613 EVT ValTy = N->getValueType(0);
614 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000615 return SDValue();
616
Andrew Trickac6d9be2013-05-25 02:42:55 +0000617 return DAG.getNode(MipsISD::Ext, SDLoc(N), ValTy,
Akira Hatanaka82099682011-12-19 19:52:25 +0000618 ShiftRight.getOperand(0), DAG.getConstant(Pos, MVT::i32),
Akira Hatanaka667645f2011-08-17 22:59:46 +0000619 DAG.getConstant(SMSize, MVT::i32));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000620}
Jia Liubb481f82012-02-28 07:46:26 +0000621
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000622static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000623 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000624 const MipsSubtarget *Subtarget) {
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000625 // Pattern match INS.
626 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
Jia Liubb481f82012-02-28 07:46:26 +0000627 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000628 // => ins $dst, $src, size, pos, $src1
Akira Hatanaka56633442011-09-20 23:53:09 +0000629 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000630 return SDValue();
631
632 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
633 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
634 ConstantSDNode *CN;
635
636 // See if Op's first operand matches (and $src1 , mask0).
637 if (And0.getOpcode() != ISD::AND)
638 return SDValue();
639
640 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000641 !isShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000642 return SDValue();
643
644 // See if Op's second operand matches (and (shl $src, pos), mask1).
645 if (And1.getOpcode() != ISD::AND)
646 return SDValue();
Jia Liubb481f82012-02-28 07:46:26 +0000647
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000648 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000649 !isShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000650 return SDValue();
651
652 // The shift masks must have the same position and size.
653 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
654 return SDValue();
655
656 SDValue Shl = And1.getOperand(0);
657 if (Shl.getOpcode() != ISD::SHL)
658 return SDValue();
659
660 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
661 return SDValue();
662
663 unsigned Shamt = CN->getZExtValue();
664
665 // Return if the shift amount and the first bit position of mask are not the
Jia Liubb481f82012-02-28 07:46:26 +0000666 // same.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000667 EVT ValTy = N->getValueType(0);
668 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000669 return SDValue();
Jia Liubb481f82012-02-28 07:46:26 +0000670
Andrew Trickac6d9be2013-05-25 02:42:55 +0000671 return DAG.getNode(MipsISD::Ins, SDLoc(N), ValTy, Shl.getOperand(0),
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000672 DAG.getConstant(SMPos0, MVT::i32),
Akira Hatanaka82099682011-12-19 19:52:25 +0000673 DAG.getConstant(SMSize0, MVT::i32), And0.getOperand(0));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000674}
Jia Liubb481f82012-02-28 07:46:26 +0000675
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000676static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka87827072012-06-13 20:33:18 +0000677 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000678 const MipsSubtarget *Subtarget) {
Akira Hatanaka87827072012-06-13 20:33:18 +0000679 // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt))
680
681 if (DCI.isBeforeLegalizeOps())
682 return SDValue();
683
684 SDValue Add = N->getOperand(1);
685
686 if (Add.getOpcode() != ISD::ADD)
687 return SDValue();
688
689 SDValue Lo = Add.getOperand(1);
690
691 if ((Lo.getOpcode() != MipsISD::Lo) ||
692 (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable))
693 return SDValue();
694
695 EVT ValTy = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +0000696 SDLoc DL(N);
Akira Hatanaka87827072012-06-13 20:33:18 +0000697
698 SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, N->getOperand(0),
699 Add.getOperand(0));
700 return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo);
701}
702
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000703SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000704 const {
705 SelectionDAG &DAG = DCI.DAG;
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000706 unsigned Opc = N->getOpcode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000707
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000708 switch (Opc) {
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000709 default: break;
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000710 case ISD::SDIVREM:
711 case ISD::UDIVREM:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000712 return performDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000713 case ISD::SELECT:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000714 return performSELECTCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000715 case ISD::AND:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000716 return performANDCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000717 case ISD::OR:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000718 return performORCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka87827072012-06-13 20:33:18 +0000719 case ISD::ADD:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000720 return performADDCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000721 }
722
723 return SDValue();
724}
725
Akira Hatanakab430cec2012-09-21 23:58:31 +0000726void
727MipsTargetLowering::LowerOperationWrapper(SDNode *N,
728 SmallVectorImpl<SDValue> &Results,
729 SelectionDAG &DAG) const {
730 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
731
732 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
733 Results.push_back(Res.getValue(I));
734}
735
736void
737MipsTargetLowering::ReplaceNodeResults(SDNode *N,
738 SmallVectorImpl<SDValue> &Results,
739 SelectionDAG &DAG) const {
Akira Hatanaka13ec4812013-04-30 21:17:07 +0000740 return LowerOperationWrapper(N, Results, DAG);
Akira Hatanakab430cec2012-09-21 23:58:31 +0000741}
742
Dan Gohman475871a2008-07-27 21:46:04 +0000743SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000744LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000745{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000746 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000747 {
Akira Hatanaka2459afe2013-03-30 01:15:17 +0000748 case ISD::BR_JT: return lowerBR_JT(Op, DAG);
749 case ISD::BRCOND: return lowerBRCOND(Op, DAG);
750 case ISD::ConstantPool: return lowerConstantPool(Op, DAG);
751 case ISD::GlobalAddress: return lowerGlobalAddress(Op, DAG);
752 case ISD::BlockAddress: return lowerBlockAddress(Op, DAG);
753 case ISD::GlobalTLSAddress: return lowerGlobalTLSAddress(Op, DAG);
754 case ISD::JumpTable: return lowerJumpTable(Op, DAG);
755 case ISD::SELECT: return lowerSELECT(Op, DAG);
756 case ISD::SELECT_CC: return lowerSELECT_CC(Op, DAG);
757 case ISD::SETCC: return lowerSETCC(Op, DAG);
758 case ISD::VASTART: return lowerVASTART(Op, DAG);
759 case ISD::FCOPYSIGN: return lowerFCOPYSIGN(Op, DAG);
760 case ISD::FABS: return lowerFABS(Op, DAG);
761 case ISD::FRAMEADDR: return lowerFRAMEADDR(Op, DAG);
762 case ISD::RETURNADDR: return lowerRETURNADDR(Op, DAG);
763 case ISD::EH_RETURN: return lowerEH_RETURN(Op, DAG);
Akira Hatanaka2459afe2013-03-30 01:15:17 +0000764 case ISD::ATOMIC_FENCE: return lowerATOMIC_FENCE(Op, DAG);
765 case ISD::SHL_PARTS: return lowerShiftLeftParts(Op, DAG);
766 case ISD::SRA_PARTS: return lowerShiftRightParts(Op, DAG, true);
767 case ISD::SRL_PARTS: return lowerShiftRightParts(Op, DAG, false);
768 case ISD::LOAD: return lowerLOAD(Op, DAG);
769 case ISD::STORE: return lowerSTORE(Op, DAG);
Akira Hatanaka2459afe2013-03-30 01:15:17 +0000770 case ISD::ADD: return lowerADD(Op, DAG);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +0000771 case ISD::FP_TO_SINT: return lowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000772 }
Dan Gohman475871a2008-07-27 21:46:04 +0000773 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000774}
775
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000776//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000777// Lower helper functions
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000778//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000779
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000780// addLiveIn - This helper function adds the specified physical register to the
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000781// MachineFunction as a live in value. It also creates a corresponding
782// virtual register for it.
783static unsigned
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000784addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000785{
Chris Lattner84bc5422007-12-31 04:13:23 +0000786 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
787 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000788 return VReg;
789}
790
Akira Hatanakaf8941992013-05-20 18:07:43 +0000791static MachineBasicBlock *expandPseudoDIV(MachineInstr *MI,
792 MachineBasicBlock &MBB,
793 const TargetInstrInfo &TII,
794 bool Is64Bit) {
795 if (NoZeroDivCheck)
796 return &MBB;
797
798 // Insert instruction "teq $divisor_reg, $zero, 7".
799 MachineBasicBlock::iterator I(MI);
800 MachineInstrBuilder MIB;
801 MIB = BuildMI(MBB, llvm::next(I), MI->getDebugLoc(), TII.get(Mips::TEQ))
802 .addOperand(MI->getOperand(2)).addReg(Mips::ZERO).addImm(7);
803
804 // Use the 32-bit sub-register if this is a 64-bit division.
805 if (Is64Bit)
806 MIB->getOperand(0).setSubReg(Mips::sub_32);
807
808 return &MBB;
809}
810
Akira Hatanaka01f70892012-09-27 02:15:57 +0000811MachineBasicBlock *
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000812MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000813 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000814 switch (MI->getOpcode()) {
Reed Kotlerffbe4322013-02-21 04:22:38 +0000815 default:
816 llvm_unreachable("Unexpected instr type to insert");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000817 case Mips::ATOMIC_LOAD_ADD_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000818 return emitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000819 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000820 return emitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000821 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000822 return emitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000823 case Mips::ATOMIC_LOAD_ADD_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000824 return emitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000825
826 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000827 return emitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000828 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000829 return emitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000830 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000831 return emitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka59068062011-11-11 04:14:30 +0000832 case Mips::ATOMIC_LOAD_AND_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000833 return emitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000834
835 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000836 return emitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000837 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000838 return emitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000839 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000840 return emitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000841 case Mips::ATOMIC_LOAD_OR_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000842 return emitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000843
844 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000845 return emitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000846 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000847 return emitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000848 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000849 return emitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000850 case Mips::ATOMIC_LOAD_XOR_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000851 return emitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000852
853 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000854 return emitAtomicBinaryPartword(MI, BB, 1, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000855 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000856 return emitAtomicBinaryPartword(MI, BB, 2, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000857 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000858 return emitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka59068062011-11-11 04:14:30 +0000859 case Mips::ATOMIC_LOAD_NAND_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000860 return emitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000861
862 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000863 return emitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000864 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000865 return emitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000866 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000867 return emitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000868 case Mips::ATOMIC_LOAD_SUB_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000869 return emitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000870
871 case Mips::ATOMIC_SWAP_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000872 return emitAtomicBinaryPartword(MI, BB, 1, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000873 case Mips::ATOMIC_SWAP_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000874 return emitAtomicBinaryPartword(MI, BB, 2, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000875 case Mips::ATOMIC_SWAP_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000876 return emitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka59068062011-11-11 04:14:30 +0000877 case Mips::ATOMIC_SWAP_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000878 return emitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000879
880 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000881 return emitAtomicCmpSwapPartword(MI, BB, 1);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000882 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000883 return emitAtomicCmpSwapPartword(MI, BB, 2);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000884 case Mips::ATOMIC_CMP_SWAP_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000885 return emitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka59068062011-11-11 04:14:30 +0000886 case Mips::ATOMIC_CMP_SWAP_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000887 return emitAtomicCmpSwap(MI, BB, 8);
Akira Hatanakaf8941992013-05-20 18:07:43 +0000888 case Mips::PseudoSDIV:
889 case Mips::PseudoUDIV:
890 return expandPseudoDIV(MI, *BB, *getTargetMachine().getInstrInfo(), false);
891 case Mips::PseudoDSDIV:
892 case Mips::PseudoDUDIV:
893 return expandPseudoDIV(MI, *BB, *getTargetMachine().getInstrInfo(), true);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000894 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000895}
896
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000897// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
898// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
899MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000900MipsTargetLowering::emitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher471e4222011-06-08 23:55:35 +0000901 unsigned Size, unsigned BinOpcode,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000902 bool Nand) const {
Akira Hatanaka59068062011-11-11 04:14:30 +0000903 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000904
905 MachineFunction *MF = BB->getParent();
906 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +0000907 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000908 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000909 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +0000910 unsigned LL, SC, AND, NOR, ZERO, BEQ;
911
912 if (Size == 4) {
Akira Hatanakaa98a4862013-08-20 21:08:22 +0000913 LL = Mips::LL;
914 SC = Mips::SC;
Akira Hatanaka59068062011-11-11 04:14:30 +0000915 AND = Mips::AND;
916 NOR = Mips::NOR;
917 ZERO = Mips::ZERO;
918 BEQ = Mips::BEQ;
919 }
920 else {
Akira Hatanakaa98a4862013-08-20 21:08:22 +0000921 LL = Mips::LLD;
922 SC = Mips::SCD;
Akira Hatanaka59068062011-11-11 04:14:30 +0000923 AND = Mips::AND64;
924 NOR = Mips::NOR64;
925 ZERO = Mips::ZERO_64;
926 BEQ = Mips::BEQ64;
927 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000928
Akira Hatanaka4061da12011-07-19 20:11:17 +0000929 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000930 unsigned Ptr = MI->getOperand(1).getReg();
931 unsigned Incr = MI->getOperand(2).getReg();
932
Akira Hatanaka4061da12011-07-19 20:11:17 +0000933 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
934 unsigned AndRes = RegInfo.createVirtualRegister(RC);
935 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000936
937 // insert new blocks after the current block
938 const BasicBlock *LLVM_BB = BB->getBasicBlock();
939 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
940 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
941 MachineFunction::iterator It = BB;
942 ++It;
943 MF->insert(It, loopMBB);
944 MF->insert(It, exitMBB);
945
946 // Transfer the remainder of BB and its successor edges to exitMBB.
947 exitMBB->splice(exitMBB->begin(), BB,
948 llvm::next(MachineBasicBlock::iterator(MI)),
949 BB->end());
950 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
951
952 // thisMBB:
953 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000954 // fallthrough --> loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000955 BB->addSuccessor(loopMBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +0000956 loopMBB->addSuccessor(loopMBB);
957 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000958
959 // loopMBB:
960 // ll oldval, 0(ptr)
Akira Hatanaka4061da12011-07-19 20:11:17 +0000961 // <binop> storeval, oldval, incr
962 // sc success, storeval, 0(ptr)
963 // beq success, $0, loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000964 BB = loopMBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000965 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000966 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000967 // and andres, oldval, incr
968 // nor storeval, $0, andres
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000969 BuildMI(BB, DL, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
970 BuildMI(BB, DL, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000971 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000972 // <binop> storeval, oldval, incr
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000973 BuildMI(BB, DL, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000974 } else {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000975 StoreVal = Incr;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000976 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000977 BuildMI(BB, DL, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
978 BuildMI(BB, DL, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000979
980 MI->eraseFromParent(); // The instruction is gone now.
981
Akira Hatanaka939ece12011-07-19 03:42:13 +0000982 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000983}
984
985MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000986MipsTargetLowering::emitAtomicBinaryPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000987 MachineBasicBlock *BB,
988 unsigned Size, unsigned BinOpcode,
989 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000990 assert((Size == 1 || Size == 2) &&
991 "Unsupported size for EmitAtomicBinaryPartial.");
992
993 MachineFunction *MF = BB->getParent();
994 MachineRegisterInfo &RegInfo = MF->getRegInfo();
995 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
996 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000997 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000998
999 unsigned Dest = MI->getOperand(0).getReg();
1000 unsigned Ptr = MI->getOperand(1).getReg();
1001 unsigned Incr = MI->getOperand(2).getReg();
1002
Akira Hatanaka4061da12011-07-19 20:11:17 +00001003 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1004 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001005 unsigned Mask = RegInfo.createVirtualRegister(RC);
1006 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001007 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1008 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001009 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001010 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1011 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1012 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1013 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1014 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001015 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001016 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1017 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1018 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1019 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1020 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001021
1022 // insert new blocks after the current block
1023 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1024 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001025 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001026 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1027 MachineFunction::iterator It = BB;
1028 ++It;
1029 MF->insert(It, loopMBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001030 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001031 MF->insert(It, exitMBB);
1032
1033 // Transfer the remainder of BB and its successor edges to exitMBB.
1034 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001035 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001036 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1037
Akira Hatanaka81b44112011-07-19 17:09:53 +00001038 BB->addSuccessor(loopMBB);
1039 loopMBB->addSuccessor(loopMBB);
1040 loopMBB->addSuccessor(sinkMBB);
1041 sinkMBB->addSuccessor(exitMBB);
1042
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001043 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001044 // addiu masklsb2,$0,-4 # 0xfffffffc
1045 // and alignedaddr,ptr,masklsb2
1046 // andi ptrlsb2,ptr,3
1047 // sll shiftamt,ptrlsb2,3
1048 // ori maskupper,$0,255 # 0xff
1049 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001050 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001051 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001052
1053 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001054 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001055 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001056 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001057 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001058 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Akira Hatanakaaffed7e2013-05-31 03:25:44 +00001059 if (Subtarget->isLittle()) {
1060 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1061 } else {
1062 unsigned Off = RegInfo.createVirtualRegister(RC);
1063 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1064 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1065 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1066 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001067 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001068 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001069 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka51122432013-07-01 20:39:53 +00001070 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001071 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka51122432013-07-01 20:39:53 +00001072 BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(Incr).addReg(ShiftAmt);
Bruno Cardoso Lopescada2d02011-05-31 20:25:26 +00001073
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001074 // atomic.load.binop
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001075 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001076 // ll oldval,0(alignedaddr)
1077 // binop binopres,oldval,incr2
1078 // and newval,binopres,mask
1079 // and maskedoldval0,oldval,mask2
1080 // or storeval,maskedoldval0,newval
1081 // sc success,storeval,0(alignedaddr)
1082 // beq success,$0,loopMBB
1083
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001084 // atomic.swap
1085 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001086 // ll oldval,0(alignedaddr)
Akira Hatanaka70564a92011-07-19 18:14:26 +00001087 // and newval,incr2,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001088 // and maskedoldval0,oldval,mask2
1089 // or storeval,maskedoldval0,newval
1090 // sc success,storeval,0(alignedaddr)
1091 // beq success,$0,loopMBB
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001092
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001093 BB = loopMBB;
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001094 BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001095 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001096 // and andres, oldval, incr2
1097 // nor binopres, $0, andres
1098 // and newval, binopres, mask
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001099 BuildMI(BB, DL, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1100 BuildMI(BB, DL, TII->get(Mips::NOR), BinOpRes)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001101 .addReg(Mips::ZERO).addReg(AndRes);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001102 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001103 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001104 // <binop> binopres, oldval, incr2
1105 // and newval, binopres, mask
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001106 BuildMI(BB, DL, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1107 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001108 } else {// atomic.swap
Akira Hatanaka4061da12011-07-19 20:11:17 +00001109 // and newval, incr2, mask
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001110 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001111 }
Jia Liubb481f82012-02-28 07:46:26 +00001112
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001113 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001114 .addReg(OldVal).addReg(Mask2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001115 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001116 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001117 BuildMI(BB, DL, TII->get(Mips::SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001118 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001119 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001120 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001121
Akira Hatanaka939ece12011-07-19 03:42:13 +00001122 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001123 // and maskedoldval1,oldval,mask
1124 // srl srlres,maskedoldval1,shiftamt
1125 // sll sllres,srlres,24
1126 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001127 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001128 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001129
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001130 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001131 .addReg(OldVal).addReg(Mask);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001132 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka51122432013-07-01 20:39:53 +00001133 .addReg(MaskedOldVal1).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001134 BuildMI(BB, DL, TII->get(Mips::SLL), SllRes)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001135 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001136 BuildMI(BB, DL, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001137 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001138
1139 MI->eraseFromParent(); // The instruction is gone now.
1140
Akira Hatanaka939ece12011-07-19 03:42:13 +00001141 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001142}
1143
1144MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001145MipsTargetLowering::emitAtomicCmpSwap(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001146 MachineBasicBlock *BB,
1147 unsigned Size) const {
Akira Hatanaka59068062011-11-11 04:14:30 +00001148 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001149
1150 MachineFunction *MF = BB->getParent();
1151 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +00001152 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001153 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001154 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001155 unsigned LL, SC, ZERO, BNE, BEQ;
1156
1157 if (Size == 4) {
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001158 LL = Mips::LL;
1159 SC = Mips::SC;
Akira Hatanaka59068062011-11-11 04:14:30 +00001160 ZERO = Mips::ZERO;
1161 BNE = Mips::BNE;
1162 BEQ = Mips::BEQ;
1163 }
1164 else {
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001165 LL = Mips::LLD;
1166 SC = Mips::SCD;
Akira Hatanaka59068062011-11-11 04:14:30 +00001167 ZERO = Mips::ZERO_64;
1168 BNE = Mips::BNE64;
1169 BEQ = Mips::BEQ64;
1170 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001171
1172 unsigned Dest = MI->getOperand(0).getReg();
1173 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001174 unsigned OldVal = MI->getOperand(2).getReg();
1175 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001176
Akira Hatanaka4061da12011-07-19 20:11:17 +00001177 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001178
1179 // insert new blocks after the current block
1180 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1181 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1182 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1183 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1184 MachineFunction::iterator It = BB;
1185 ++It;
1186 MF->insert(It, loop1MBB);
1187 MF->insert(It, loop2MBB);
1188 MF->insert(It, exitMBB);
1189
1190 // Transfer the remainder of BB and its successor edges to exitMBB.
1191 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001192 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001193 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1194
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001195 // thisMBB:
1196 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001197 // fallthrough --> loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001198 BB->addSuccessor(loop1MBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +00001199 loop1MBB->addSuccessor(exitMBB);
1200 loop1MBB->addSuccessor(loop2MBB);
1201 loop2MBB->addSuccessor(loop1MBB);
1202 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001203
1204 // loop1MBB:
1205 // ll dest, 0(ptr)
1206 // bne dest, oldval, exitMBB
1207 BB = loop1MBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001208 BuildMI(BB, DL, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1209 BuildMI(BB, DL, TII->get(BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001210 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001211
1212 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001213 // sc success, newval, 0(ptr)
1214 // beq success, $0, loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001215 BB = loop2MBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001216 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001217 .addReg(NewVal).addReg(Ptr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001218 BuildMI(BB, DL, TII->get(BEQ))
Akira Hatanaka59068062011-11-11 04:14:30 +00001219 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001220
1221 MI->eraseFromParent(); // The instruction is gone now.
1222
Akira Hatanaka939ece12011-07-19 03:42:13 +00001223 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001224}
1225
1226MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001227MipsTargetLowering::emitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001228 MachineBasicBlock *BB,
1229 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001230 assert((Size == 1 || Size == 2) &&
1231 "Unsupported size for EmitAtomicCmpSwapPartial.");
1232
1233 MachineFunction *MF = BB->getParent();
1234 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1235 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1236 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001237 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001238
1239 unsigned Dest = MI->getOperand(0).getReg();
1240 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001241 unsigned CmpVal = MI->getOperand(2).getReg();
1242 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001243
Akira Hatanaka4061da12011-07-19 20:11:17 +00001244 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1245 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001246 unsigned Mask = RegInfo.createVirtualRegister(RC);
1247 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001248 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1249 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1250 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1251 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1252 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1253 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1254 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1255 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1256 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1257 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1258 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1259 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1260 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1261 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001262
1263 // insert new blocks after the current block
1264 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1265 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1266 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001267 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001268 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1269 MachineFunction::iterator It = BB;
1270 ++It;
1271 MF->insert(It, loop1MBB);
1272 MF->insert(It, loop2MBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001273 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001274 MF->insert(It, exitMBB);
1275
1276 // Transfer the remainder of BB and its successor edges to exitMBB.
1277 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001278 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001279 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1280
Akira Hatanaka81b44112011-07-19 17:09:53 +00001281 BB->addSuccessor(loop1MBB);
1282 loop1MBB->addSuccessor(sinkMBB);
1283 loop1MBB->addSuccessor(loop2MBB);
1284 loop2MBB->addSuccessor(loop1MBB);
1285 loop2MBB->addSuccessor(sinkMBB);
1286 sinkMBB->addSuccessor(exitMBB);
1287
Akira Hatanaka70564a92011-07-19 18:14:26 +00001288 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001289 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001290 // addiu masklsb2,$0,-4 # 0xfffffffc
1291 // and alignedaddr,ptr,masklsb2
1292 // andi ptrlsb2,ptr,3
1293 // sll shiftamt,ptrlsb2,3
1294 // ori maskupper,$0,255 # 0xff
1295 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001296 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001297 // andi maskedcmpval,cmpval,255
1298 // sll shiftedcmpval,maskedcmpval,shiftamt
1299 // andi maskednewval,newval,255
1300 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001301 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001302 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001303 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001304 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001305 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001306 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Akira Hatanakaaffed7e2013-05-31 03:25:44 +00001307 if (Subtarget->isLittle()) {
1308 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1309 } else {
1310 unsigned Off = RegInfo.createVirtualRegister(RC);
1311 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1312 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1313 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1314 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001315 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001316 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001317 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka51122432013-07-01 20:39:53 +00001318 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001319 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1320 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001321 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001322 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal)
Akira Hatanaka51122432013-07-01 20:39:53 +00001323 .addReg(MaskedCmpVal).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001324 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001325 .addReg(NewVal).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001326 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal)
Akira Hatanaka51122432013-07-01 20:39:53 +00001327 .addReg(MaskedNewVal).addReg(ShiftAmt);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001328
1329 // loop1MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001330 // ll oldval,0(alginedaddr)
1331 // and maskedoldval0,oldval,mask
1332 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001333 BB = loop1MBB;
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001334 BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001335 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001336 .addReg(OldVal).addReg(Mask);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001337 BuildMI(BB, DL, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001338 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001339
1340 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001341 // and maskedoldval1,oldval,mask2
1342 // or storeval,maskedoldval1,shiftednewval
1343 // sc success,storeval,0(alignedaddr)
1344 // beq success,$0,loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001345 BB = loop2MBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001346 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001347 .addReg(OldVal).addReg(Mask2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001348 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001349 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001350 BuildMI(BB, DL, TII->get(Mips::SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001351 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001352 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001353 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001354
Akira Hatanaka939ece12011-07-19 03:42:13 +00001355 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001356 // srl srlres,maskedoldval0,shiftamt
1357 // sll sllres,srlres,24
1358 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001359 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001360 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001361
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001362 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka51122432013-07-01 20:39:53 +00001363 .addReg(MaskedOldVal0).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001364 BuildMI(BB, DL, TII->get(Mips::SLL), SllRes)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001365 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001366 BuildMI(BB, DL, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001367 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001368
1369 MI->eraseFromParent(); // The instruction is gone now.
1370
Akira Hatanaka939ece12011-07-19 03:42:13 +00001371 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001372}
1373
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001374//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001375// Misc Lower Operation implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001376//===----------------------------------------------------------------------===//
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001377SDValue MipsTargetLowering::lowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanakab7656a92013-03-06 21:32:03 +00001378 SDValue Chain = Op.getOperand(0);
1379 SDValue Table = Op.getOperand(1);
1380 SDValue Index = Op.getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001381 SDLoc DL(Op);
Akira Hatanakab7656a92013-03-06 21:32:03 +00001382 EVT PTy = getPointerTy();
1383 unsigned EntrySize =
1384 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(*getDataLayout());
1385
1386 Index = DAG.getNode(ISD::MUL, DL, PTy, Index,
1387 DAG.getConstant(EntrySize, PTy));
1388 SDValue Addr = DAG.getNode(ISD::ADD, DL, PTy, Index, Table);
1389
1390 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
1391 Addr = DAG.getExtLoad(ISD::SEXTLOAD, DL, PTy, Chain, Addr,
1392 MachinePointerInfo::getJumpTable(), MemVT, false, false,
1393 0);
1394 Chain = Addr.getValue(1);
1395
1396 if ((getTargetMachine().getRelocationModel() == Reloc::PIC_) || IsN64) {
1397 // For PIC, the sequence is:
1398 // BRIND(load(Jumptable + index) + RelocBase)
1399 // RelocBase can be JumpTable, GOT or some sort of global base.
1400 Addr = DAG.getNode(ISD::ADD, DL, PTy, Addr,
1401 getPICJumpTableRelocBase(Table, DAG));
1402 }
1403
1404 return DAG.getNode(ISD::BRIND, DL, MVT::Other, Chain, Addr);
1405}
1406
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +00001407SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001408lowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001409{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001410 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001411 // the block to branch to if the condition is true.
1412 SDValue Chain = Op.getOperand(0);
1413 SDValue Dest = Op.getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001414 SDLoc DL(Op);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001415
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001416 SDValue CondRes = createFPCmp(DAG, Op.getOperand(1));
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001417
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001418 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001419 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +00001420 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001421
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +00001422 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001423 Mips::CondCode CC =
1424 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Akira Hatanaka9cf07242013-03-30 01:16:38 +00001425 unsigned Opc = invertFPCondCodeUser(CC) ? Mips::BRANCH_F : Mips::BRANCH_T;
1426 SDValue BrCode = DAG.getConstant(Opc, MVT::i32);
Akira Hatanaka83d8ef12013-07-26 20:13:47 +00001427 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001428 return DAG.getNode(MipsISD::FPBrcond, DL, Op.getValueType(), Chain, BrCode,
Akira Hatanaka83d8ef12013-07-26 20:13:47 +00001429 FCC0, Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001430}
1431
1432SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001433lowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001434{
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001435 SDValue Cond = createFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001436
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001437 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001438 if (Cond.getOpcode() != MipsISD::FPCmp)
1439 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +00001440
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001441 return createCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001442 SDLoc(Op));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001443}
1444
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001445SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001446lowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001447{
Andrew Trickac6d9be2013-05-25 02:42:55 +00001448 SDLoc DL(Op);
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001449 EVT Ty = Op.getOperand(0).getValueType();
Matt Arsenault225ed702013-05-18 00:21:46 +00001450 SDValue Cond = DAG.getNode(ISD::SETCC, DL,
1451 getSetCCResultType(*DAG.getContext(), Ty),
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001452 Op.getOperand(0), Op.getOperand(1),
1453 Op.getOperand(4));
1454
1455 return DAG.getNode(ISD::SELECT, DL, Op.getValueType(), Cond, Op.getOperand(2),
1456 Op.getOperand(3));
1457}
1458
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001459SDValue MipsTargetLowering::lowerSETCC(SDValue Op, SelectionDAG &DAG) const {
1460 SDValue Cond = createFPCmp(DAG, Op);
Akira Hatanaka0a40c232012-03-09 23:46:03 +00001461
1462 assert(Cond.getOpcode() == MipsISD::FPCmp &&
1463 "Floating point operand expected.");
1464
1465 SDValue True = DAG.getConstant(1, MVT::i32);
1466 SDValue False = DAG.getConstant(0, MVT::i32);
1467
Andrew Trickac6d9be2013-05-25 02:42:55 +00001468 return createCMovFP(DAG, Cond, True, False, SDLoc(Op));
Akira Hatanaka0a40c232012-03-09 23:46:03 +00001469}
1470
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001471SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001472 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +00001473 // FIXME there isn't actually debug info here
Andrew Trickac6d9be2013-05-25 02:42:55 +00001474 SDLoc DL(Op);
Jia Liubb481f82012-02-28 07:46:26 +00001475 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001476
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001477 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
Akira Hatanakaafc945b2012-09-12 23:27:55 +00001478 const MipsTargetObjectFile &TLOF =
1479 (const MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001480
Chris Lattnere3736f82009-08-13 05:41:27 +00001481 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001482 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001483 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001484 MipsII::MO_GPREL);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001485 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, DL,
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001486 DAG.getVTList(MVT::i32), &GA, 1);
Akira Hatanakae7338cd2012-08-22 03:18:13 +00001487 SDValue GPReg = DAG.getRegister(Mips::GP, MVT::i32);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001488 return DAG.getNode(ISD::ADD, DL, MVT::i32, GPReg, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +00001489 }
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001490
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001491 // %hi/%lo relocation
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001492 return getAddrNonPIC(Op, DAG);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001493 }
1494
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001495 if (GV->hasInternalLinkage() || (GV->hasLocalLinkage() && !isa<Function>(GV)))
1496 return getAddrLocal(Op, DAG, HasMips64);
1497
Akira Hatanakaf09a0372012-11-21 20:40:38 +00001498 if (LargeGOT)
1499 return getAddrGlobalLargeGOT(Op, DAG, MipsII::MO_GOT_HI16,
1500 MipsII::MO_GOT_LO16);
1501
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001502 return getAddrGlobal(Op, DAG,
1503 HasMips64 ? MipsII::MO_GOT_DISP : MipsII::MO_GOT16);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001504}
1505
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001506SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op,
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001507 SelectionDAG &DAG) const {
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001508 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
1509 return getAddrNonPIC(Op, DAG);
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001510
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001511 return getAddrLocal(Op, DAG, HasMips64);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001512}
1513
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001514SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001515lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001516{
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001517 // If the relocation model is PIC, use the General Dynamic TLS Model or
1518 // Local Dynamic TLS model, otherwise use the Initial Exec or
1519 // Local Exec TLS Model.
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001520
1521 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001522 SDLoc DL(GA);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001523 const GlobalValue *GV = GA->getGlobal();
1524 EVT PtrVT = getPointerTy();
1525
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001526 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
1527
1528 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Hans Wennborg70a07c72012-06-04 14:02:08 +00001529 // General Dynamic and Local Dynamic TLS Model.
1530 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
1531 : MipsII::MO_TLSGD;
1532
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001533 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag);
1534 SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT,
1535 getGlobalReg(DAG, PtrVT), TGA);
Akira Hatanaka7a7194b2011-12-08 21:05:38 +00001536 unsigned PtrSize = PtrVT.getSizeInBits();
1537 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1538
Benjamin Kramer5eccf672011-12-11 12:21:34 +00001539 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001540
1541 ArgListTy Args;
1542 ArgListEntry Entry;
1543 Entry.Node = Argument;
Akira Hatanakaca074792011-12-08 20:34:32 +00001544 Entry.Ty = PtrTy;
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001545 Args.push_back(Entry);
Jia Liubb481f82012-02-28 07:46:26 +00001546
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001547 TargetLowering::CallLoweringInfo CLI(DAG.getEntryNode(), PtrTy,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001548 false, false, false, false, 0, CallingConv::C,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001549 /*IsTailCall=*/false, /*doesNotRet=*/false,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001550 /*isReturnValueUsed=*/true,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001551 TlsGetAddr, Args, DAG, DL);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001552 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001553
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001554 SDValue Ret = CallResult.first;
1555
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001556 if (model != TLSModel::LocalDynamic)
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001557 return Ret;
1558
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001559 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001560 MipsII::MO_DTPREL_HI);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001561 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1562 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001563 MipsII::MO_DTPREL_LO);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001564 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1565 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret);
1566 return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001567 }
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001568
1569 SDValue Offset;
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001570 if (model == TLSModel::InitialExec) {
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001571 // Initial Exec TLS Model
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001572 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001573 MipsII::MO_GOTTPREL);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001574 TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT),
Akira Hatanaka648f00c2012-02-24 22:34:47 +00001575 TGA);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001576 Offset = DAG.getLoad(PtrVT, DL,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001577 DAG.getEntryNode(), TGA, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001578 false, false, false, 0);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001579 } else {
1580 // Local Exec TLS Model
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001581 assert(model == TLSModel::LocalExec);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001582 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001583 MipsII::MO_TPREL_HI);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001584 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001585 MipsII::MO_TPREL_LO);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001586 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1587 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1588 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001589 }
1590
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001591 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
1592 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001593}
1594
1595SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001596lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001597{
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001598 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
1599 return getAddrNonPIC(Op, DAG);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001600
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001601 return getAddrLocal(Op, DAG, HasMips64);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001602}
1603
Dan Gohman475871a2008-07-27 21:46:04 +00001604SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001605lowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001606{
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001607 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001608 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001609 // but the asm printer currently doesn't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001610 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +00001611 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +00001612 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001613 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1614 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001615 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001616
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001617 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
1618 return getAddrNonPIC(Op, DAG);
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001619
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001620 return getAddrLocal(Op, DAG, HasMips64);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001621}
1622
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001623SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001624 MachineFunction &MF = DAG.getMachineFunction();
1625 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1626
Andrew Trickac6d9be2013-05-25 02:42:55 +00001627 SDLoc DL(Op);
Dan Gohman1e93df62010-04-17 14:41:14 +00001628 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1629 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001630
1631 // vastart just stores the address of the VarArgsFrameIndex slot into the
1632 // memory location argument.
1633 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001634 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
Akira Hatanaka82099682011-12-19 19:52:25 +00001635 MachinePointerInfo(SV), false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001636}
Jia Liubb481f82012-02-28 07:46:26 +00001637
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001638static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001639 EVT TyX = Op.getOperand(0).getValueType();
1640 EVT TyY = Op.getOperand(1).getValueType();
1641 SDValue Const1 = DAG.getConstant(1, MVT::i32);
1642 SDValue Const31 = DAG.getConstant(31, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001643 SDLoc DL(Op);
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001644 SDValue Res;
1645
1646 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1647 // to i32.
1648 SDValue X = (TyX == MVT::f32) ?
1649 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1650 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1651 Const1);
1652 SDValue Y = (TyY == MVT::f32) ?
1653 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
1654 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
1655 Const1);
1656
1657 if (HasR2) {
1658 // ext E, Y, 31, 1 ; extract bit31 of Y
1659 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
1660 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
1661 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
1662 } else {
1663 // sll SllX, X, 1
1664 // srl SrlX, SllX, 1
1665 // srl SrlY, Y, 31
1666 // sll SllY, SrlX, 31
1667 // or Or, SrlX, SllY
1668 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1669 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1670 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
1671 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
1672 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
1673 }
1674
1675 if (TyX == MVT::f32)
1676 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
1677
1678 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1679 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1680 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001681}
1682
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001683static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001684 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
1685 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
1686 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
1687 SDValue Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001688 SDLoc DL(Op);
Eric Christopher471e4222011-06-08 23:55:35 +00001689
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001690 // Bitcast to integer nodes.
1691 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
1692 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001693
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001694 if (HasR2) {
1695 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
1696 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
1697 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
1698 DAG.getConstant(WidthY - 1, MVT::i32), Const1);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001699
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001700 if (WidthX > WidthY)
1701 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
1702 else if (WidthY > WidthX)
1703 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001704
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001705 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
1706 DAG.getConstant(WidthX - 1, MVT::i32), Const1, X);
1707 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
1708 }
1709
1710 // (d)sll SllX, X, 1
1711 // (d)srl SrlX, SllX, 1
1712 // (d)srl SrlY, Y, width(Y)-1
1713 // (d)sll SllY, SrlX, width(Y)-1
1714 // or Or, SrlX, SllY
1715 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
1716 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
1717 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
1718 DAG.getConstant(WidthY - 1, MVT::i32));
1719
1720 if (WidthX > WidthY)
1721 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
1722 else if (WidthY > WidthX)
1723 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
1724
1725 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
1726 DAG.getConstant(WidthX - 1, MVT::i32));
1727 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
1728 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001729}
1730
Akira Hatanaka82099682011-12-19 19:52:25 +00001731SDValue
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001732MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001733 if (Subtarget->hasMips64())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001734 return lowerFCOPYSIGN64(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001735
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001736 return lowerFCOPYSIGN32(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001737}
1738
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001739static SDValue lowerFABS32(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001740 SDValue Res, Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001741 SDLoc DL(Op);
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001742
1743 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1744 // to i32.
1745 SDValue X = (Op.getValueType() == MVT::f32) ?
1746 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1747 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1748 Const1);
1749
1750 // Clear MSB.
1751 if (HasR2)
1752 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32,
1753 DAG.getRegister(Mips::ZERO, MVT::i32),
1754 DAG.getConstant(31, MVT::i32), Const1, X);
1755 else {
1756 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1757 Res = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1758 }
1759
1760 if (Op.getValueType() == MVT::f32)
1761 return DAG.getNode(ISD::BITCAST, DL, MVT::f32, Res);
1762
1763 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1764 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1765 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
1766}
1767
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001768static SDValue lowerFABS64(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001769 SDValue Res, Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001770 SDLoc DL(Op);
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001771
1772 // Bitcast to integer node.
1773 SDValue X = DAG.getNode(ISD::BITCAST, DL, MVT::i64, Op.getOperand(0));
1774
1775 // Clear MSB.
1776 if (HasR2)
1777 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i64,
1778 DAG.getRegister(Mips::ZERO_64, MVT::i64),
1779 DAG.getConstant(63, MVT::i32), Const1, X);
1780 else {
1781 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i64, X, Const1);
1782 Res = DAG.getNode(ISD::SRL, DL, MVT::i64, SllX, Const1);
1783 }
1784
1785 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, Res);
1786}
1787
1788SDValue
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001789MipsTargetLowering::lowerFABS(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001790 if (Subtarget->hasMips64() && (Op.getValueType() == MVT::f64))
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001791 return lowerFABS64(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001792
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001793 return lowerFABS32(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001794}
1795
Akira Hatanaka2e591472011-06-02 00:24:44 +00001796SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001797lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopese0b5cfc2011-06-16 00:40:02 +00001798 // check the depth
1799 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka0f843822011-06-07 18:58:42 +00001800 "Frame address can only be determined for current frame.");
Akira Hatanaka2e591472011-06-02 00:24:44 +00001801
1802 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1803 MFI->setFrameAddressIsTaken(true);
1804 EVT VT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001805 SDLoc DL(Op);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001806 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), DL,
Akira Hatanaka46ac4392011-11-11 04:11:56 +00001807 IsN64 ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka2e591472011-06-02 00:24:44 +00001808 return FrameAddr;
1809}
1810
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001811SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op,
Akira Hatanakaba584fe2012-07-11 00:53:32 +00001812 SelectionDAG &DAG) const {
1813 // check the depth
1814 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
1815 "Return address can be determined only for current frame.");
1816
1817 MachineFunction &MF = DAG.getMachineFunction();
1818 MachineFrameInfo *MFI = MF.getFrameInfo();
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00001819 MVT VT = Op.getSimpleValueType();
Akira Hatanakaba584fe2012-07-11 00:53:32 +00001820 unsigned RA = IsN64 ? Mips::RA_64 : Mips::RA;
1821 MFI->setReturnAddressIsTaken(true);
1822
1823 // Return RA, which contains the return address. Mark it an implicit live-in.
1824 unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00001825 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT);
Akira Hatanakaba584fe2012-07-11 00:53:32 +00001826}
1827
Akira Hatanaka544cc212013-01-30 00:26:49 +00001828// An EH_RETURN is the result of lowering llvm.eh.return which in turn is
1829// generated from __builtin_eh_return (offset, handler)
1830// The effect of this is to adjust the stack pointer by "offset"
1831// and then branch to "handler".
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001832SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Akira Hatanaka544cc212013-01-30 00:26:49 +00001833 const {
1834 MachineFunction &MF = DAG.getMachineFunction();
1835 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1836
1837 MipsFI->setCallsEhReturn();
1838 SDValue Chain = Op.getOperand(0);
1839 SDValue Offset = Op.getOperand(1);
1840 SDValue Handler = Op.getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001841 SDLoc DL(Op);
Akira Hatanaka544cc212013-01-30 00:26:49 +00001842 EVT Ty = IsN64 ? MVT::i64 : MVT::i32;
1843
1844 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
1845 // EH_RETURN nodes, so that instructions are emitted back-to-back.
1846 unsigned OffsetReg = IsN64 ? Mips::V1_64 : Mips::V1;
1847 unsigned AddrReg = IsN64 ? Mips::V0_64 : Mips::V0;
1848 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
1849 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
1850 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
1851 DAG.getRegister(OffsetReg, Ty),
1852 DAG.getRegister(AddrReg, getPointerTy()),
1853 Chain.getValue(1));
1854}
1855
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001856SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
Akira Hatanaka864f6602012-06-14 21:10:56 +00001857 SelectionDAG &DAG) const {
Eli Friedman14648462011-07-27 22:21:52 +00001858 // FIXME: Need pseudo-fence for 'singlethread' fences
1859 // FIXME: Set SType for weaker fences where supported/appropriate.
1860 unsigned SType = 0;
Andrew Trickac6d9be2013-05-25 02:42:55 +00001861 SDLoc DL(Op);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001862 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00001863 DAG.getConstant(SType, MVT::i32));
1864}
1865
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001866SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
Akira Hatanaka864f6602012-06-14 21:10:56 +00001867 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001868 SDLoc DL(Op);
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001869 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
1870 SDValue Shamt = Op.getOperand(2);
1871
1872 // if shamt < 32:
1873 // lo = (shl lo, shamt)
1874 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
1875 // else:
1876 // lo = 0
1877 // hi = (shl lo, shamt[4:0])
1878 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
1879 DAG.getConstant(-1, MVT::i32));
1880 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo,
1881 DAG.getConstant(1, MVT::i32));
1882 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, ShiftRight1Lo,
1883 Not);
1884 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi, Shamt);
1885 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
1886 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, MVT::i32, Lo, Shamt);
1887 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
1888 DAG.getConstant(0x20, MVT::i32));
Akira Hatanaka864f6602012-06-14 21:10:56 +00001889 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
1890 DAG.getConstant(0, MVT::i32), ShiftLeftLo);
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001891 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftLeftLo, Or);
1892
1893 SDValue Ops[2] = {Lo, Hi};
1894 return DAG.getMergeValues(Ops, 2, DL);
1895}
1896
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001897SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001898 bool IsSRA) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001899 SDLoc DL(Op);
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001900 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
1901 SDValue Shamt = Op.getOperand(2);
1902
1903 // if shamt < 32:
1904 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
1905 // if isSRA:
1906 // hi = (sra hi, shamt)
1907 // else:
1908 // hi = (srl hi, shamt)
1909 // else:
1910 // if isSRA:
1911 // lo = (sra hi, shamt[4:0])
1912 // hi = (sra hi, 31)
1913 // else:
1914 // lo = (srl hi, shamt[4:0])
1915 // hi = 0
1916 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
1917 DAG.getConstant(-1, MVT::i32));
1918 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi,
1919 DAG.getConstant(1, MVT::i32));
1920 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, ShiftLeft1Hi, Not);
1921 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo, Shamt);
1922 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
1923 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL, DL, MVT::i32,
1924 Hi, Shamt);
1925 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
1926 DAG.getConstant(0x20, MVT::i32));
1927 SDValue Shift31 = DAG.getNode(ISD::SRA, DL, MVT::i32, Hi,
1928 DAG.getConstant(31, MVT::i32));
1929 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftRightHi, Or);
1930 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
1931 IsSRA ? Shift31 : DAG.getConstant(0, MVT::i32),
1932 ShiftRightHi);
1933
1934 SDValue Ops[2] = {Lo, Hi};
1935 return DAG.getMergeValues(Ops, 2, DL);
1936}
1937
Akira Hatanakafee62c12013-04-11 19:07:14 +00001938static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001939 SDValue Chain, SDValue Src, unsigned Offset) {
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00001940 SDValue Ptr = LD->getBasePtr();
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001941 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00001942 EVT BasePtrVT = Ptr.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001943 SDLoc DL(LD);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001944 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
1945
1946 if (Offset)
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00001947 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001948 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001949
1950 SDValue Ops[] = { Chain, Ptr, Src };
1951 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT,
1952 LD->getMemOperand());
1953}
1954
1955// Expand an unaligned 32 or 64-bit integer load node.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001956SDValue MipsTargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001957 LoadSDNode *LD = cast<LoadSDNode>(Op);
1958 EVT MemVT = LD->getMemoryVT();
1959
1960 // Return if load is aligned or if MemVT is neither i32 nor i64.
1961 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
1962 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
1963 return SDValue();
1964
1965 bool IsLittle = Subtarget->isLittle();
1966 EVT VT = Op.getValueType();
1967 ISD::LoadExtType ExtType = LD->getExtensionType();
1968 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
1969
1970 assert((VT == MVT::i32) || (VT == MVT::i64));
1971
1972 // Expand
1973 // (set dst, (i64 (load baseptr)))
1974 // to
1975 // (set tmp, (ldl (add baseptr, 7), undef))
1976 // (set dst, (ldr baseptr, tmp))
1977 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
Akira Hatanakafee62c12013-04-11 19:07:14 +00001978 SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001979 IsLittle ? 7 : 0);
Akira Hatanakafee62c12013-04-11 19:07:14 +00001980 return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001981 IsLittle ? 0 : 7);
1982 }
1983
Akira Hatanakafee62c12013-04-11 19:07:14 +00001984 SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001985 IsLittle ? 3 : 0);
Akira Hatanakafee62c12013-04-11 19:07:14 +00001986 SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001987 IsLittle ? 0 : 3);
1988
1989 // Expand
1990 // (set dst, (i32 (load baseptr))) or
1991 // (set dst, (i64 (sextload baseptr))) or
1992 // (set dst, (i64 (extload baseptr)))
1993 // to
1994 // (set tmp, (lwl (add baseptr, 3), undef))
1995 // (set dst, (lwr baseptr, tmp))
1996 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
1997 (ExtType == ISD::EXTLOAD))
1998 return LWR;
1999
2000 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
2001
2002 // Expand
2003 // (set dst, (i64 (zextload baseptr)))
2004 // to
2005 // (set tmp0, (lwl (add baseptr, 3), undef))
2006 // (set tmp1, (lwr baseptr, tmp0))
2007 // (set tmp2, (shl tmp1, 32))
2008 // (set dst, (srl tmp2, 32))
Andrew Trickac6d9be2013-05-25 02:42:55 +00002009 SDLoc DL(LD);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002010 SDValue Const32 = DAG.getConstant(32, MVT::i32);
2011 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
Akira Hatanaka94ccee22012-06-04 17:46:29 +00002012 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
2013 SDValue Ops[] = { SRL, LWR.getValue(1) };
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002014 return DAG.getMergeValues(Ops, 2, DL);
2015}
2016
Akira Hatanakafee62c12013-04-11 19:07:14 +00002017static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002018 SDValue Chain, unsigned Offset) {
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00002019 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
2020 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00002021 SDLoc DL(SD);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002022 SDVTList VTList = DAG.getVTList(MVT::Other);
2023
2024 if (Offset)
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00002025 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002026 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002027
2028 SDValue Ops[] = { Chain, Value, Ptr };
2029 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT,
2030 SD->getMemOperand());
2031}
2032
2033// Expand an unaligned 32 or 64-bit integer store node.
Akira Hatanaka63451432013-05-16 20:45:17 +00002034static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG,
2035 bool IsLittle) {
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002036 SDValue Value = SD->getValue(), Chain = SD->getChain();
2037 EVT VT = Value.getValueType();
2038
2039 // Expand
2040 // (store val, baseptr) or
2041 // (truncstore val, baseptr)
2042 // to
2043 // (swl val, (add baseptr, 3))
2044 // (swr val, baseptr)
2045 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
Akira Hatanakafee62c12013-04-11 19:07:14 +00002046 SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002047 IsLittle ? 3 : 0);
Akira Hatanakafee62c12013-04-11 19:07:14 +00002048 return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002049 }
2050
2051 assert(VT == MVT::i64);
2052
2053 // Expand
2054 // (store val, baseptr)
2055 // to
2056 // (sdl val, (add baseptr, 7))
2057 // (sdr val, baseptr)
Akira Hatanakafee62c12013-04-11 19:07:14 +00002058 SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2059 return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002060}
2061
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002062// Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr).
2063static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG) {
2064 SDValue Val = SD->getValue();
2065
2066 if (Val.getOpcode() != ISD::FP_TO_SINT)
2067 return SDValue();
2068
2069 EVT FPTy = EVT::getFloatingPointVT(Val.getValueSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002070 SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy,
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002071 Val.getOperand(0));
2072
Andrew Trickac6d9be2013-05-25 02:42:55 +00002073 return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(),
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002074 SD->getPointerInfo(), SD->isVolatile(),
2075 SD->isNonTemporal(), SD->getAlignment());
2076}
2077
Akira Hatanaka63451432013-05-16 20:45:17 +00002078SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2079 StoreSDNode *SD = cast<StoreSDNode>(Op);
2080 EVT MemVT = SD->getMemoryVT();
2081
2082 // Lower unaligned integer stores.
2083 if ((SD->getAlignment() < MemVT.getSizeInBits() / 8) &&
2084 ((MemVT == MVT::i32) || (MemVT == MVT::i64)))
2085 return lowerUnalignedIntStore(SD, DAG, Subtarget->isLittle());
2086
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002087 return lowerFP_TO_SINT_STORE(SD, DAG);
Akira Hatanaka63451432013-05-16 20:45:17 +00002088}
2089
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002090SDValue MipsTargetLowering::lowerADD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanakae90a3bc2012-11-07 19:10:58 +00002091 if (Op->getOperand(0).getOpcode() != ISD::FRAMEADDR
2092 || cast<ConstantSDNode>
2093 (Op->getOperand(0).getOperand(0))->getZExtValue() != 0
2094 || Op->getOperand(1).getOpcode() != ISD::FRAME_TO_ARGS_OFFSET)
2095 return SDValue();
2096
2097 // The pattern
2098 // (add (frameaddr 0), (frame_to_args_offset))
2099 // results from lowering llvm.eh.dwarf.cfa intrinsic. Transform it to
2100 // (add FrameObject, 0)
2101 // where FrameObject is a fixed StackObject with offset 0 which points to
2102 // the old stack pointer.
2103 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2104 EVT ValTy = Op->getValueType(0);
2105 int FI = MFI->CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
2106 SDValue InArgsAddr = DAG.getFrameIndex(FI, ValTy);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002107 return DAG.getNode(ISD::ADD, SDLoc(Op), ValTy, InArgsAddr,
Akira Hatanakae90a3bc2012-11-07 19:10:58 +00002108 DAG.getConstant(0, ValTy));
2109}
2110
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002111SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op,
2112 SelectionDAG &DAG) const {
2113 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002114 SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy,
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002115 Op.getOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00002116 return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002117}
2118
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002119//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002120// Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002121//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002122
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002123//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002124// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002125// Mips O32 ABI rules:
2126// ---
2127// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002128// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002129// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002130// f64 - Only passed in two aliased f32 registers if no int reg has been used
2131// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002132// not used, it must be shadowed. If only A3 is avaiable, shadow it and
2133// go to stack.
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002134//
2135// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002136//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002137
Duncan Sands1e96bab2010-11-04 10:49:57 +00002138static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +00002139 MVT LocVT, CCValAssign::LocInfo LocInfo,
Akira Hatanakaad341d42013-08-20 23:38:40 +00002140 ISD::ArgFlagsTy ArgFlags, CCState &State,
2141 const uint16_t *F64Regs) {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002142
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002143 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002144
Craig Topperc5eaae42012-03-11 07:57:25 +00002145 static const uint16_t IntRegs[] = {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002146 Mips::A0, Mips::A1, Mips::A2, Mips::A3
2147 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002148 static const uint16_t F32Regs[] = {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002149 Mips::F12, Mips::F14
2150 };
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002151
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002152 // Do not process byval args here.
2153 if (ArgFlags.isByVal())
2154 return true;
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002155
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002156 // Promote i8 and i16
2157 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
2158 LocVT = MVT::i32;
2159 if (ArgFlags.isSExt())
2160 LocInfo = CCValAssign::SExt;
2161 else if (ArgFlags.isZExt())
2162 LocInfo = CCValAssign::ZExt;
2163 else
2164 LocInfo = CCValAssign::AExt;
2165 }
2166
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002167 unsigned Reg;
2168
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002169 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
2170 // is true: function is vararg, argument is 3rd or higher, there is previous
2171 // argument which is not f32 or f64.
2172 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
2173 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00002174 unsigned OrigAlign = ArgFlags.getOrigAlign();
2175 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002176
2177 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002178 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00002179 // If this is the first part of an i64 arg,
2180 // the allocated register must be either A0 or A2.
2181 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
2182 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002183 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002184 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
2185 // Allocate int register and shadow next int register. If first
2186 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002187 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2188 if (Reg == Mips::A1 || Reg == Mips::A3)
2189 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2190 State.AllocateReg(IntRegs, IntRegsSize);
2191 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002192 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
2193 // we are guaranteed to find an available float register
2194 if (ValVT == MVT::f32) {
2195 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
2196 // Shadow int register
2197 State.AllocateReg(IntRegs, IntRegsSize);
2198 } else {
2199 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
2200 // Shadow int registers
2201 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
2202 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
2203 State.AllocateReg(IntRegs, IntRegsSize);
2204 State.AllocateReg(IntRegs, IntRegsSize);
2205 }
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002206 } else
2207 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002208
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002209 if (!Reg) {
2210 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3,
2211 OrigAlign);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002212 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002213 } else
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002214 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002215
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002216 return false;
Akira Hatanaka2c5d6522011-11-12 02:20:46 +00002217}
2218
Akira Hatanakaad341d42013-08-20 23:38:40 +00002219static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT,
2220 MVT LocVT, CCValAssign::LocInfo LocInfo,
2221 ISD::ArgFlagsTy ArgFlags, CCState &State) {
2222 static const uint16_t F64Regs[] = { Mips::D6, Mips::D7 };
2223
2224 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2225}
2226
2227static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT,
2228 MVT LocVT, CCValAssign::LocInfo LocInfo,
2229 ISD::ArgFlagsTy ArgFlags, CCState &State) {
2230 static const uint16_t F64Regs[] = { Mips::D12_64, Mips::D12_64 };
2231
2232 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2233}
2234
Akira Hatanaka2c5d6522011-11-12 02:20:46 +00002235#include "MipsGenCallingConv.inc"
2236
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002237//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002238// Call Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002239//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002240
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002241// Return next O32 integer argument register.
2242static unsigned getNextIntArgReg(unsigned Reg) {
2243 assert((Reg == Mips::A0) || (Reg == Mips::A2));
2244 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
2245}
2246
Akira Hatanaka7d712092012-10-30 19:23:25 +00002247SDValue
2248MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002249 SDValue Chain, SDValue Arg, SDLoc DL,
Akira Hatanaka7d712092012-10-30 19:23:25 +00002250 bool IsTailCall, SelectionDAG &DAG) const {
2251 if (!IsTailCall) {
2252 SDValue PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr,
2253 DAG.getIntPtrConstant(Offset));
2254 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo(), false,
2255 false, 0);
2256 }
2257
2258 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2259 int FI = MFI->CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
2260 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2261 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(),
2262 /*isVolatile=*/ true, false, 0);
2263}
2264
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002265void MipsTargetLowering::
2266getOpndList(SmallVectorImpl<SDValue> &Ops,
2267 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
2268 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
2269 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
2270 // Insert node "GP copy globalreg" before call to function.
2271 //
2272 // R_MIPS_CALL* operators (emitted when non-internal functions are called
2273 // in PIC mode) allow symbols to be resolved via lazy binding.
2274 // The lazy binding stub requires GP to point to the GOT.
2275 if (IsPICCall && !InternalLinkage) {
2276 unsigned GPReg = IsN64 ? Mips::GP_64 : Mips::GP;
2277 EVT Ty = IsN64 ? MVT::i64 : MVT::i32;
2278 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty)));
2279 }
Reed Kotler8453b3f2013-01-24 04:24:02 +00002280
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002281 // Build a sequence of copy-to-reg nodes chained together with token
2282 // chain and flag operands which copy the outgoing args into registers.
2283 // The InFlag in necessary since all emitted instructions must be
2284 // stuck together.
2285 SDValue InFlag;
Reed Kotler8453b3f2013-01-24 04:24:02 +00002286
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002287 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2288 Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, RegsToPass[i].first,
2289 RegsToPass[i].second, InFlag);
2290 InFlag = Chain.getValue(1);
2291 }
Reed Kotler8453b3f2013-01-24 04:24:02 +00002292
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002293 // Add argument registers to the end of the list so that they are
2294 // known live into the call.
2295 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2296 Ops.push_back(CLI.DAG.getRegister(RegsToPass[i].first,
2297 RegsToPass[i].second.getValueType()));
Reed Kotler8453b3f2013-01-24 04:24:02 +00002298
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002299 // Add a register mask operand representing the call-preserved registers.
2300 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2301 const uint32_t *Mask = TRI->getCallPreservedMask(CLI.CallConv);
2302 assert(Mask && "Missing call preserved mask for calling convention");
Reed Kotler46090912013-05-10 22:25:39 +00002303 if (Subtarget->inMips16HardFloat()) {
2304 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
2305 llvm::StringRef Sym = G->getGlobal()->getName();
2306 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
2307 if (F->hasFnAttribute("__Mips16RetHelper")) {
2308 Mask = MipsRegisterInfo::getMips16RetHelperMask();
2309 }
2310 }
2311 }
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002312 Ops.push_back(CLI.DAG.getRegisterMask(Mask));
2313
2314 if (InFlag.getNode())
2315 Ops.push_back(InFlag);
Reed Kotler8453b3f2013-01-24 04:24:02 +00002316}
2317
Dan Gohman98ca4f22009-08-05 01:29:28 +00002318/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00002319/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002320SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002321MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002322 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002323 SelectionDAG &DAG = CLI.DAG;
Andrew Trickac6d9be2013-05-25 02:42:55 +00002324 SDLoc DL = CLI.DL;
Craig Toppera0ec3f92013-07-14 04:42:23 +00002325 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
2326 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
2327 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Akira Hatanakae2d529a2012-07-31 18:46:41 +00002328 SDValue Chain = CLI.Chain;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002329 SDValue Callee = CLI.Callee;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002330 bool &IsTailCall = CLI.IsTailCall;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002331 CallingConv::ID CallConv = CLI.CallConv;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002332 bool IsVarArg = CLI.IsVarArg;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002333
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002334 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002335 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanakad37776d2011-05-20 21:39:54 +00002336 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002337 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002338
2339 // Analyze operands of the call, assigning locations to each operand.
2340 SmallVector<CCValAssign, 16> ArgLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002341 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka82099682011-12-19 19:52:25 +00002342 getTargetMachine(), ArgLocs, *DAG.getContext());
Reed Kotler46090912013-05-10 22:25:39 +00002343 MipsCC::SpecialCallingConvType SpecialCallingConv =
2344 getSpecialCallingConv(Callee);
Akira Hatanakaad341d42013-08-20 23:38:40 +00002345 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo,
2346 SpecialCallingConv);
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002347
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002348 MipsCCInfo.analyzeCallOperands(Outs, IsVarArg,
Reed Kotlerc673f9c2013-08-30 19:40:56 +00002349 Subtarget->mipsSEUsesSoftFloat(),
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00002350 Callee.getNode(), CLI.Args);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002351
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002352 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002353 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka480eeb52012-07-26 23:27:01 +00002354
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002355 // Check if it's really possible to do a tail call.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002356 if (IsTailCall)
2357 IsTailCall =
2358 isEligibleForTailCallOptimization(MipsCCInfo, NextStackOffset,
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002359 *MF.getInfo<MipsFunctionInfo>());
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002360
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002361 if (IsTailCall)
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002362 ++NumTailCalls;
2363
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002364 // Chain is the output chain of the last Load/Store or CopyToReg node.
2365 // ByValChain is the output chain of the last Memcpy node created for copying
2366 // byval arguments to the stack.
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002367 unsigned StackAlignment = TFL->getStackAlignment();
2368 NextStackOffset = RoundUpToAlignment(NextStackOffset, StackAlignment);
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002369 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002370
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002371 if (!IsTailCall)
Andrew Trick6e0b2a02013-05-29 22:03:55 +00002372 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffsetVal, DL);
Akira Hatanakae2d529a2012-07-31 18:46:41 +00002373
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002374 SDValue StackPtr = DAG.getCopyFromReg(Chain, DL,
Akira Hatanakae2d529a2012-07-31 18:46:41 +00002375 IsN64 ? Mips::SP_64 : Mips::SP,
2376 getPointerTy());
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002377
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002378 // With EABI is it possible to have 16 args on registers.
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00002379 std::deque< std::pair<unsigned, SDValue> > RegsToPass;
Dan Gohman475871a2008-07-27 21:46:04 +00002380 SmallVector<SDValue, 8> MemOpChains;
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002381 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002382
2383 // Walk the register/memloc assignments, inserting copies/loads.
2384 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00002385 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002386 CCValAssign &VA = ArgLocs[i];
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002387 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002388 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2389
2390 // ByVal Arg.
2391 if (Flags.isByVal()) {
2392 assert(Flags.getByValSize() &&
2393 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002394 assert(ByValArg != MipsCCInfo.byval_end());
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002395 assert(!IsTailCall &&
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002396 "Do not tail-call optimize if there is a byval argument.");
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002397 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002398 MipsCCInfo, *ByValArg, Flags, Subtarget->isLittle());
2399 ++ByValArg;
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002400 continue;
2401 }
Jia Liubb481f82012-02-28 07:46:26 +00002402
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002403 // Promote the value if needed.
2404 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002405 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002406 case CCValAssign::Full:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002407 if (VA.isRegLoc()) {
2408 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00002409 (ValVT == MVT::f64 && LocVT == MVT::i64) ||
2410 (ValVT == MVT::i64 && LocVT == MVT::f64))
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002411 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002412 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002413 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002414 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002415 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002416 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00002417 if (!Subtarget->isLittle())
2418 std::swap(Lo, Hi);
Jia Liubb481f82012-02-28 07:46:26 +00002419 unsigned LocRegLo = VA.getLocReg();
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002420 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2421 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2422 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002423 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002424 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002425 }
2426 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00002427 case CCValAssign::SExt:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002428 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002429 break;
2430 case CCValAssign::ZExt:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002431 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002432 break;
2433 case CCValAssign::AExt:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002434 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002435 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002436 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002437
2438 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002439 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002440 if (VA.isRegLoc()) {
2441 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00002442 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002443 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002444
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002445 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00002446 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002447
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002448 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00002449 // parameter value to a stack Location
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002450 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002451 Chain, Arg, DL, IsTailCall, DAG));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002452 }
2453
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002454 // Transform all store nodes into one single node because all store
2455 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002456 if (!MemOpChains.empty())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002457 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002458 &MemOpChains[0], MemOpChains.size());
2459
Bill Wendling056292f2008-09-16 21:48:12 +00002460 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002461 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2462 // node so that legalize doesn't hack it.
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002463 bool IsPICCall = (IsN64 || IsPIC); // true if calls are translated to jalr $25
Akira Hatanakaed185da2012-12-13 03:17:29 +00002464 bool GlobalOrExternal = false, InternalLinkage = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002465 SDValue CalleeLo;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002466
2467 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002468 if (IsPICCall) {
Akira Hatanakaed185da2012-12-13 03:17:29 +00002469 InternalLinkage = G->getGlobal()->hasInternalLinkage();
2470
2471 if (InternalLinkage)
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002472 Callee = getAddrLocal(Callee, DAG, HasMips64);
Akira Hatanakaf09a0372012-11-21 20:40:38 +00002473 else if (LargeGOT)
2474 Callee = getAddrGlobalLargeGOT(Callee, DAG, MipsII::MO_CALL_HI16,
2475 MipsII::MO_CALL_LO16);
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002476 else
2477 Callee = getAddrGlobal(Callee, DAG, MipsII::MO_GOT_CALL);
2478 } else
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002479 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, getPointerTy(), 0,
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002480 MipsII::MO_NO_FLAG);
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002481 GlobalOrExternal = true;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002482 }
2483 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakaf09a0372012-11-21 20:40:38 +00002484 if (!IsN64 && !IsPIC) // !N64 && static
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002485 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2486 MipsII::MO_NO_FLAG);
Akira Hatanakaf09a0372012-11-21 20:40:38 +00002487 else if (LargeGOT)
2488 Callee = getAddrGlobalLargeGOT(Callee, DAG, MipsII::MO_CALL_HI16,
2489 MipsII::MO_CALL_LO16);
Akira Hatanaka60689322013-02-22 21:10:03 +00002490 else // N64 || PIC
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002491 Callee = getAddrGlobal(Callee, DAG, MipsII::MO_GOT_CALL);
2492
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002493 GlobalOrExternal = true;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002494 }
2495
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00002496 SmallVector<SDValue, 8> Ops(1, Chain);
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002497 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00002498
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002499 getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal, InternalLinkage,
2500 CLI, Callee, Chain);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002501
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002502 if (IsTailCall)
2503 return DAG.getNode(MipsISD::TailCall, DL, MVT::Other, &Ops[0], Ops.size());
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002504
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002505 Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, &Ops[0], Ops.size());
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002506 SDValue InFlag = Chain.getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002507
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002508 // Create the CALLSEQ_END node.
Akira Hatanaka480eeb52012-07-26 23:27:01 +00002509 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal,
Andrew Trick6e0b2a02013-05-29 22:03:55 +00002510 DAG.getIntPtrConstant(0, true), InFlag, DL);
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002511 InFlag = Chain.getValue(1);
2512
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002513 // Handle result values, copying them out of physregs into vregs that we
2514 // return.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002515 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg,
2516 Ins, DL, DAG, InVals, CLI.Callee.getNode(), CLI.RetTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002517}
2518
Dan Gohman98ca4f22009-08-05 01:29:28 +00002519/// LowerCallResult - Lower the result values of a call into the
2520/// appropriate copies out of appropriate physical registers.
2521SDValue
2522MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002523 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002524 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002525 SDLoc DL, SelectionDAG &DAG,
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002526 SmallVectorImpl<SDValue> &InVals,
2527 const SDNode *CallNode,
2528 const Type *RetTy) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002529 // Assign locations to each value returned by this call.
2530 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002531 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka864f6602012-06-14 21:10:56 +00002532 getTargetMachine(), RVLocs, *DAG.getContext());
Akira Hatanakaad341d42013-08-20 23:38:40 +00002533 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo);
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002534
Reed Kotlerc673f9c2013-08-30 19:40:56 +00002535 MipsCCInfo.analyzeCallResult(Ins, Subtarget->mipsSEUsesSoftFloat(),
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002536 CallNode, RetTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002537
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002538 // Copy all of the result registers out of their specified physreg.
2539 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002540 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(),
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002541 RVLocs[i].getLocVT(), InFlag);
2542 Chain = Val.getValue(1);
2543 InFlag = Val.getValue(2);
2544
2545 if (RVLocs[i].getValVT() != RVLocs[i].getLocVT())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002546 Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getValVT(), Val);
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002547
2548 InVals.push_back(Val);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002549 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002550
Dan Gohman98ca4f22009-08-05 01:29:28 +00002551 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002552}
2553
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002554//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002555// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002556//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002557/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002558/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002559SDValue
2560MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002561 CallingConv::ID CallConv,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002562 bool IsVarArg,
Akira Hatanaka82099682011-12-19 19:52:25 +00002563 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002564 SDLoc DL, SelectionDAG &DAG,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002565 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002566 const {
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00002567 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002568 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00002569 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002570
Dan Gohman1e93df62010-04-17 14:41:14 +00002571 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002572
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002573 // Used with vargs to acumulate store chains.
2574 std::vector<SDValue> OutChains;
2575
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002576 // Assign locations to all of the incoming arguments.
2577 SmallVector<CCValAssign, 16> ArgLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002578 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka82099682011-12-19 19:52:25 +00002579 getTargetMachine(), ArgLocs, *DAG.getContext());
Akira Hatanakaad341d42013-08-20 23:38:40 +00002580 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo);
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002581 Function::const_arg_iterator FuncArg =
2582 DAG.getMachineFunction().getFunction()->arg_begin();
Reed Kotlerc673f9c2013-08-30 19:40:56 +00002583 bool UseSoftFloat = Subtarget->mipsSEUsesSoftFloat();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002584
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002585 MipsCCInfo.analyzeFormalArguments(Ins, UseSoftFloat, FuncArg);
Akira Hatanakab33b34a2012-10-30 19:37:25 +00002586 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(),
2587 MipsCCInfo.hasByValArg());
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002588
Akira Hatanaka4618e0b2012-10-27 00:44:39 +00002589 unsigned CurArgIdx = 0;
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002590 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002591
Akira Hatanaka4618e0b2012-10-27 00:44:39 +00002592 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002593 CCValAssign &VA = ArgLocs[i];
Akira Hatanaka4618e0b2012-10-27 00:44:39 +00002594 std::advance(FuncArg, Ins[i].OrigArgIndex - CurArgIdx);
2595 CurArgIdx = Ins[i].OrigArgIndex;
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002596 EVT ValVT = VA.getValVT();
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002597 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2598 bool IsRegLoc = VA.isRegLoc();
2599
2600 if (Flags.isByVal()) {
2601 assert(Flags.getByValSize() &&
2602 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002603 assert(ByValArg != MipsCCInfo.byval_end());
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002604 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg,
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002605 MipsCCInfo, *ByValArg);
2606 ++ByValArg;
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002607 continue;
2608 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002609
2610 // Arguments stored on registers
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002611 if (IsRegLoc) {
Owen Andersone50ed302009-08-10 22:56:29 +00002612 EVT RegVT = VA.getLocVT();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002613 unsigned ArgReg = VA.getLocReg();
Craig Topper44d23822012-02-22 05:59:10 +00002614 const TargetRegisterClass *RC;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002615
Owen Anderson825b72b2009-08-11 20:47:22 +00002616 if (RegVT == MVT::i32)
Reed Kotlerbacbf1c2012-12-20 06:06:35 +00002617 RC = Subtarget->inMips16Mode()? &Mips::CPU16RegsRegClass :
Akira Hatanaka18587862013-08-06 23:08:38 +00002618 &Mips::GPR32RegClass;
Akira Hatanaka95934842011-09-24 01:34:44 +00002619 else if (RegVT == MVT::i64)
Akira Hatanaka18587862013-08-06 23:08:38 +00002620 RC = &Mips::GPR64RegClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002621 else if (RegVT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00002622 RC = &Mips::FGR32RegClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002623 else if (RegVT == MVT::f64)
Akira Hatanakaad341d42013-08-20 23:38:40 +00002624 RC = Subtarget->isFP64bit() ? &Mips::FGR64RegClass :
2625 &Mips::AFGR64RegClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002626 else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002627 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002628
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002629 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002630 // physical registers into virtual ones
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002631 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC);
2632 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002633
2634 // If this is an 8 or 16-bit value, it has been passed promoted
2635 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002636 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002637 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00002638 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002639 if (VA.getLocInfo() == CCValAssign::SExt)
2640 Opcode = ISD::AssertSext;
2641 else if (VA.getLocInfo() == CCValAssign::ZExt)
2642 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00002643 if (Opcode)
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002644 ArgValue = DAG.getNode(Opcode, DL, RegVT, ArgValue,
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002645 DAG.getValueType(ValVT));
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002646 ArgValue = DAG.getNode(ISD::TRUNCATE, DL, ValVT, ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002647 }
2648
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002649 // Handle floating point arguments passed in integer registers and
2650 // long double arguments passed in floating point registers.
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002651 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002652 (RegVT == MVT::i64 && ValVT == MVT::f64) ||
2653 (RegVT == MVT::f64 && ValVT == MVT::i64))
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002654 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002655 else if (IsO32 && RegVT == MVT::i32 && ValVT == MVT::f64) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002656 unsigned Reg2 = addLiveIn(DAG.getMachineFunction(),
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002657 getNextIntArgReg(ArgReg), RC);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002658 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT);
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002659 if (!Subtarget->isLittle())
2660 std::swap(ArgValue, ArgValue2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002661 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64,
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002662 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002663 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002664
Dan Gohman98ca4f22009-08-05 01:29:28 +00002665 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002666 } else { // VA.isRegLoc()
2667
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002668 // sanity check
2669 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002670
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002671 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002672 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002673 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002674
2675 // Create load nodes to retrieve arguments from the stack
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002676 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002677 InVals.push_back(DAG.getLoad(ValVT, DL, Chain, FIN,
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002678 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002679 false, false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002680 }
2681 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002682
2683 // The mips ABIs for returning structs by value requires that we copy
2684 // the sret argument into $v0 for the return. Save the argument into
2685 // a virtual register so that we can access it from the return points.
2686 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2687 unsigned Reg = MipsFI->getSRetReturnReg();
2688 if (!Reg) {
Akira Hatanaka30580ce2012-10-19 22:11:40 +00002689 Reg = MF.getRegInfo().
2690 createVirtualRegister(getRegClassFor(IsN64 ? MVT::i64 : MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002691 MipsFI->setSRetReturnReg(Reg);
2692 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002693 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[0]);
2694 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002695 }
2696
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002697 if (IsVarArg)
2698 writeVarArgRegs(OutChains, MipsCCInfo, Chain, DL, DAG);
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002699
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002700 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002701 // the size of Ins and InVals. This only happens when on varg functions
2702 if (!OutChains.empty()) {
2703 OutChains.push_back(Chain);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002704 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002705 &OutChains[0], OutChains.size());
2706 }
2707
Dan Gohman98ca4f22009-08-05 01:29:28 +00002708 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002709}
2710
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002711//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002712// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002713//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002714
Akira Hatanaka97d9f082012-10-10 01:27:09 +00002715bool
2716MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002717 MachineFunction &MF, bool IsVarArg,
Akira Hatanaka97d9f082012-10-10 01:27:09 +00002718 const SmallVectorImpl<ISD::OutputArg> &Outs,
2719 LLVMContext &Context) const {
2720 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002721 CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(),
Akira Hatanaka97d9f082012-10-10 01:27:09 +00002722 RVLocs, Context);
2723 return CCInfo.CheckReturn(Outs, RetCC_Mips);
2724}
2725
Dan Gohman98ca4f22009-08-05 01:29:28 +00002726SDValue
2727MipsTargetLowering::LowerReturn(SDValue Chain,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002728 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002729 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002730 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002731 SDLoc DL, SelectionDAG &DAG) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002732 // CCValAssign - represent the assignment of
2733 // the return value to a location
2734 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002735 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002736
2737 // CCState - Info about the registers and stack slot.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002738 CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(), RVLocs,
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002739 *DAG.getContext());
Akira Hatanakaad341d42013-08-20 23:38:40 +00002740 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002741
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002742 // Analyze return values.
Reed Kotlerc673f9c2013-08-30 19:40:56 +00002743 MipsCCInfo.analyzeReturn(Outs, Subtarget->mipsSEUsesSoftFloat(),
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002744 MF.getFunction()->getReturnType());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002745
Dan Gohman475871a2008-07-27 21:46:04 +00002746 SDValue Flag;
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002747 SmallVector<SDValue, 4> RetOps(1, Chain);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002748
2749 // Copy the result values into the output registers.
2750 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002751 SDValue Val = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002752 CCValAssign &VA = RVLocs[i];
2753 assert(VA.isRegLoc() && "Can only return in registers!");
2754
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002755 if (RVLocs[i].getValVT() != RVLocs[i].getLocVT())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002756 Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getLocVT(), Val);
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002757
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002758 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002759
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002760 // Guarantee that all emitted copies are stuck together with flags.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002761 Flag = Chain.getValue(1);
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002762 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002763 }
2764
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002765 // The mips ABIs for returning structs by value requires that we copy
2766 // the sret argument into $v0 for the return. We saved the argument into
2767 // a virtual register in the entry block, so now we copy the value out
2768 // and into $v0.
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002769 if (MF.getFunction()->hasStructRetAttr()) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002770 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2771 unsigned Reg = MipsFI->getSRetReturnReg();
2772
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002773 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00002774 llvm_unreachable("sret virtual register not created in the entry block");
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002775 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Akira Hatanaka2ef5bd32012-10-24 02:10:54 +00002776 unsigned V0 = IsN64 ? Mips::V0_64 : Mips::V0;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002777
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002778 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002779 Flag = Chain.getValue(1);
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002780 RetOps.push_back(DAG.getRegister(V0, getPointerTy()));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002781 }
2782
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002783 RetOps[0] = Chain; // Update chain.
Akira Hatanaka182ef6f2012-07-10 00:19:06 +00002784
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002785 // Add the flag if we have it.
2786 if (Flag.getNode())
2787 RetOps.push_back(Flag);
2788
2789 // Return on Mips is always a "jr $ra"
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002790 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, &RetOps[0], RetOps.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002791}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002792
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002793//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002794// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002795//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002796
2797/// getConstraintType - Given a constraint letter, return the type of
2798/// constraint it is for this target.
2799MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002800getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002801{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002802 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002803 // GCC config/mips/constraints.md
2804 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002805 // 'd' : An address register. Equivalent to r
2806 // unless generating MIPS16 code.
2807 // 'y' : Equivalent to r; retained for
2808 // backwards compatibility.
Eric Christopher1d5a3922012-05-07 06:25:10 +00002809 // 'c' : A register suitable for use in an indirect
2810 // jump. This will always be $25 for -mabicalls.
Eric Christopheraf97f732012-05-07 06:25:19 +00002811 // 'l' : The lo register. 1 word storage.
2812 // 'x' : The hilo register pair. Double word storage.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002813 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002814 switch (Constraint[0]) {
2815 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002816 case 'd':
2817 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002818 case 'f':
Eric Christopher1d5a3922012-05-07 06:25:10 +00002819 case 'c':
Eric Christopher4adbefe2012-05-07 06:25:15 +00002820 case 'l':
Eric Christopheraf97f732012-05-07 06:25:19 +00002821 case 'x':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002822 return C_RegisterClass;
Jack Carter0b9675d2013-03-04 21:33:15 +00002823 case 'R':
2824 return C_Memory;
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002825 }
2826 }
2827 return TargetLowering::getConstraintType(Constraint);
2828}
2829
John Thompson44ab89e2010-10-29 17:29:13 +00002830/// Examine constraint type and operand type and determine a weight value.
2831/// This object must already have been set up with the operand type
2832/// and the current alternative constraint selected.
2833TargetLowering::ConstraintWeight
2834MipsTargetLowering::getSingleConstraintMatchWeight(
2835 AsmOperandInfo &info, const char *constraint) const {
2836 ConstraintWeight weight = CW_Invalid;
2837 Value *CallOperandVal = info.CallOperandVal;
2838 // If we don't have a value, we can't do a match,
2839 // but allow it at the lowest weight.
2840 if (CallOperandVal == NULL)
2841 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002842 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00002843 // Look at the constraint type.
2844 switch (*constraint) {
2845 default:
2846 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2847 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002848 case 'd':
2849 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00002850 if (type->isIntegerTy())
2851 weight = CW_Register;
2852 break;
2853 case 'f':
2854 if (type->isFloatTy())
2855 weight = CW_Register;
2856 break;
Eric Christopher1d5a3922012-05-07 06:25:10 +00002857 case 'c': // $25 for indirect jumps
Eric Christopher4adbefe2012-05-07 06:25:15 +00002858 case 'l': // lo register
Eric Christopheraf97f732012-05-07 06:25:19 +00002859 case 'x': // hilo register pair
Eric Christopher1d5a3922012-05-07 06:25:10 +00002860 if (type->isIntegerTy())
2861 weight = CW_SpecificReg;
2862 break;
Eric Christopher50ab0392012-05-07 03:13:32 +00002863 case 'I': // signed 16 bit immediate
Eric Christophere5076d42012-05-07 03:13:42 +00002864 case 'J': // integer zero
Eric Christopherf49f8462012-05-07 05:46:29 +00002865 case 'K': // unsigned 16 bit immediate
Eric Christopher5ac47bb2012-05-07 05:46:37 +00002866 case 'L': // signed 32 bit immediate where lower 16 bits are 0
Eric Christopher60cfc792012-05-07 05:46:43 +00002867 case 'N': // immediate in the range of -65535 to -1 (inclusive)
Eric Christopher1ce20342012-05-07 05:46:48 +00002868 case 'O': // signed 15 bit immediate (+- 16383)
Eric Christopher54412a72012-05-07 06:25:02 +00002869 case 'P': // immediate in the range of 65535 to 1 (inclusive)
Eric Christopher50ab0392012-05-07 03:13:32 +00002870 if (isa<ConstantInt>(CallOperandVal))
2871 weight = CW_Constant;
2872 break;
Jack Carter0b9675d2013-03-04 21:33:15 +00002873 case 'R':
2874 weight = CW_Memory;
2875 break;
John Thompson44ab89e2010-10-29 17:29:13 +00002876 }
2877 return weight;
2878}
2879
Akira Hatanakabfb07b12013-08-14 00:21:25 +00002880/// This is a helper function to parse a physical register string and split it
2881/// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag
2882/// that is returned indicates whether parsing was successful. The second flag
2883/// is true if the numeric part exists.
2884static std::pair<bool, bool>
2885parsePhysicalReg(const StringRef &C, std::string &Prefix,
2886 unsigned long long &Reg) {
2887 if (C.front() != '{' || C.back() != '}')
2888 return std::make_pair(false, false);
2889
2890 // Search for the first numeric character.
2891 StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1;
2892 I = std::find_if(B, E, std::ptr_fun(isdigit));
2893
2894 Prefix.assign(B, I - B);
2895
2896 // The second flag is set to false if no numeric characters were found.
2897 if (I == E)
2898 return std::make_pair(true, false);
2899
2900 // Parse the numeric characters.
2901 return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg),
2902 true);
2903}
2904
2905std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering::
2906parseRegForInlineAsmConstraint(const StringRef &C, MVT VT) const {
2907 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2908 const TargetRegisterClass *RC;
2909 std::string Prefix;
2910 unsigned long long Reg;
2911
2912 std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg);
2913
2914 if (!R.first)
2915 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2916
2917 if ((Prefix == "hi" || Prefix == "lo")) { // Parse hi/lo.
2918 // No numeric characters follow "hi" or "lo".
2919 if (R.second)
2920 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2921
2922 RC = TRI->getRegClass(Prefix == "hi" ?
Akira Hatanakacbaf6d02013-08-14 00:47:08 +00002923 Mips::HI32RegClassID : Mips::LO32RegClassID);
Akira Hatanakabfb07b12013-08-14 00:21:25 +00002924 return std::make_pair(*(RC->begin()), RC);
2925 }
2926
2927 if (!R.second)
2928 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2929
2930 if (Prefix == "$f") { // Parse $f0-$f31.
2931 // If the size of FP registers is 64-bit or Reg is an even number, select
2932 // the 64-bit register class. Otherwise, select the 32-bit register class.
2933 if (VT == MVT::Other)
2934 VT = (Subtarget->isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32;
2935
2936 RC= getRegClassFor(VT);
2937
2938 if (RC == &Mips::AFGR64RegClass) {
2939 assert(Reg % 2 == 0);
2940 Reg >>= 1;
2941 }
2942 } else if (Prefix == "$fcc") { // Parse $fcc0-$fcc7.
2943 RC = TRI->getRegClass(Mips::FCCRegClassID);
2944 } else { // Parse $0-$31.
2945 assert(Prefix == "$");
2946 RC = getRegClassFor((VT == MVT::Other) ? MVT::i32 : VT);
2947 }
2948
2949 assert(Reg < RC->getNumRegs());
2950 return std::make_pair(*(RC->begin() + Reg), RC);
2951}
2952
Eric Christopher38d64262011-06-29 19:33:04 +00002953/// Given a register class constraint, like 'r', if this corresponds directly
2954/// to an LLVM register class, return a register of 0 and the register class
2955/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002956std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Chad Rosier5b3fca52013-06-22 18:37:38 +00002957getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002958{
2959 if (Constraint.size() == 1) {
2960 switch (Constraint[0]) {
Eric Christopher314aff12011-06-29 19:04:31 +00002961 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
2962 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002963 case 'r':
Akira Hatanakaafc945b2012-09-12 23:27:55 +00002964 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
2965 if (Subtarget->inMips16Mode())
2966 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
Akira Hatanaka18587862013-08-06 23:08:38 +00002967 return std::make_pair(0U, &Mips::GPR32RegClass);
Akira Hatanakaafc945b2012-09-12 23:27:55 +00002968 }
Jack Carter10de0252012-07-02 23:35:23 +00002969 if (VT == MVT::i64 && !HasMips64)
Akira Hatanaka18587862013-08-06 23:08:38 +00002970 return std::make_pair(0U, &Mips::GPR32RegClass);
Eric Christopher0ed1f762012-05-07 03:13:22 +00002971 if (VT == MVT::i64 && HasMips64)
Akira Hatanaka18587862013-08-06 23:08:38 +00002972 return std::make_pair(0U, &Mips::GPR64RegClass);
Eric Christopher0ed1f762012-05-07 03:13:22 +00002973 // This will generate an error message
2974 return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002975 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00002976 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00002977 return std::make_pair(0U, &Mips::FGR32RegClass);
Akira Hatanakacb9dd722012-01-04 02:45:01 +00002978 if ((VT == MVT::f64) && (!Subtarget->isSingleFloat())) {
2979 if (Subtarget->isFP64bit())
Craig Topper420761a2012-04-20 07:30:17 +00002980 return std::make_pair(0U, &Mips::FGR64RegClass);
2981 return std::make_pair(0U, &Mips::AFGR64RegClass);
Akira Hatanakacb9dd722012-01-04 02:45:01 +00002982 }
Eric Christopher1d5a3922012-05-07 06:25:10 +00002983 break;
2984 case 'c': // register suitable for indirect jump
2985 if (VT == MVT::i32)
Akira Hatanaka18587862013-08-06 23:08:38 +00002986 return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass);
Eric Christopher1d5a3922012-05-07 06:25:10 +00002987 assert(VT == MVT::i64 && "Unexpected type.");
Akira Hatanaka18587862013-08-06 23:08:38 +00002988 return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass);
Eric Christopher4adbefe2012-05-07 06:25:15 +00002989 case 'l': // register suitable for indirect jump
2990 if (VT == MVT::i32)
Akira Hatanakacbaf6d02013-08-14 00:47:08 +00002991 return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass);
2992 return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass);
Eric Christopheraf97f732012-05-07 06:25:19 +00002993 case 'x': // register suitable for indirect jump
2994 // Fixme: Not triggering the use of both hi and low
2995 // This will generate an error message
2996 return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002997 }
2998 }
Akira Hatanakabfb07b12013-08-14 00:21:25 +00002999
3000 std::pair<unsigned, const TargetRegisterClass *> R;
3001 R = parseRegForInlineAsmConstraint(Constraint, VT);
3002
3003 if (R.second)
3004 return R;
3005
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00003006 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3007}
3008
Eric Christopher50ab0392012-05-07 03:13:32 +00003009/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3010/// vector. If it is invalid, don't add anything to Ops.
3011void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3012 std::string &Constraint,
3013 std::vector<SDValue>&Ops,
3014 SelectionDAG &DAG) const {
3015 SDValue Result(0, 0);
3016
3017 // Only support length 1 constraints for now.
3018 if (Constraint.length() > 1) return;
3019
3020 char ConstraintLetter = Constraint[0];
3021 switch (ConstraintLetter) {
3022 default: break; // This will fall through to the generic implementation
3023 case 'I': // Signed 16 bit constant
3024 // If this fails, the parent routine will give an error
3025 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3026 EVT Type = Op.getValueType();
3027 int64_t Val = C->getSExtValue();
3028 if (isInt<16>(Val)) {
3029 Result = DAG.getTargetConstant(Val, Type);
3030 break;
3031 }
3032 }
3033 return;
Eric Christophere5076d42012-05-07 03:13:42 +00003034 case 'J': // integer zero
3035 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3036 EVT Type = Op.getValueType();
3037 int64_t Val = C->getZExtValue();
3038 if (Val == 0) {
3039 Result = DAG.getTargetConstant(0, Type);
3040 break;
3041 }
3042 }
3043 return;
Eric Christopherf49f8462012-05-07 05:46:29 +00003044 case 'K': // unsigned 16 bit immediate
3045 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3046 EVT Type = Op.getValueType();
3047 uint64_t Val = (uint64_t)C->getZExtValue();
3048 if (isUInt<16>(Val)) {
3049 Result = DAG.getTargetConstant(Val, Type);
3050 break;
3051 }
3052 }
3053 return;
Eric Christopher5ac47bb2012-05-07 05:46:37 +00003054 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3055 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3056 EVT Type = Op.getValueType();
3057 int64_t Val = C->getSExtValue();
3058 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
3059 Result = DAG.getTargetConstant(Val, Type);
3060 break;
3061 }
3062 }
3063 return;
Eric Christopher60cfc792012-05-07 05:46:43 +00003064 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3065 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3066 EVT Type = Op.getValueType();
3067 int64_t Val = C->getSExtValue();
3068 if ((Val >= -65535) && (Val <= -1)) {
3069 Result = DAG.getTargetConstant(Val, Type);
3070 break;
3071 }
3072 }
3073 return;
Eric Christopher1ce20342012-05-07 05:46:48 +00003074 case 'O': // signed 15 bit immediate
3075 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3076 EVT Type = Op.getValueType();
3077 int64_t Val = C->getSExtValue();
3078 if ((isInt<15>(Val))) {
3079 Result = DAG.getTargetConstant(Val, Type);
3080 break;
3081 }
3082 }
3083 return;
Eric Christopher54412a72012-05-07 06:25:02 +00003084 case 'P': // immediate in the range of 1 to 65535 (inclusive)
3085 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3086 EVT Type = Op.getValueType();
3087 int64_t Val = C->getSExtValue();
3088 if ((Val <= 65535) && (Val >= 1)) {
3089 Result = DAG.getTargetConstant(Val, Type);
3090 break;
3091 }
3092 }
3093 return;
Eric Christopher50ab0392012-05-07 03:13:32 +00003094 }
3095
3096 if (Result.getNode()) {
3097 Ops.push_back(Result);
3098 return;
3099 }
3100
3101 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3102}
3103
Dan Gohman6520e202008-10-18 02:06:02 +00003104bool
Akira Hatanaka94e47282012-11-17 00:25:41 +00003105MipsTargetLowering::isLegalAddressingMode(const AddrMode &AM, Type *Ty) const {
3106 // No global is ever allowed as a base.
3107 if (AM.BaseGV)
3108 return false;
3109
3110 switch (AM.Scale) {
3111 case 0: // "r+i" or just "i", depending on HasBaseReg.
3112 break;
3113 case 1:
3114 if (!AM.HasBaseReg) // allow "r+i".
3115 break;
3116 return false; // disallow "r+r" or "r+r+i".
3117 default:
3118 return false;
3119 }
3120
3121 return true;
3122}
3123
3124bool
Dan Gohman6520e202008-10-18 02:06:02 +00003125MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3126 // The Mips target isn't yet aware of offsets.
3127 return false;
3128}
Evan Chengeb2f9692009-10-27 19:56:55 +00003129
Akira Hatanakae193b322012-06-13 19:33:32 +00003130EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00003131 unsigned SrcAlign,
3132 bool IsMemset, bool ZeroMemset,
Akira Hatanakae193b322012-06-13 19:33:32 +00003133 bool MemcpyStrSrc,
3134 MachineFunction &MF) const {
3135 if (Subtarget->hasMips64())
3136 return MVT::i64;
3137
3138 return MVT::i32;
3139}
3140
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003141bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
3142 if (VT != MVT::f32 && VT != MVT::f64)
3143 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00003144 if (Imm.isNegZero())
3145 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00003146 return Imm.isZero();
3147}
Akira Hatanaka6c2cf8b2012-02-03 04:33:00 +00003148
3149unsigned MipsTargetLowering::getJumpTableEncoding() const {
3150 if (IsN64)
3151 return MachineJumpTableInfo::EK_GPRel64BlockAddress;
Jia Liubb481f82012-02-28 07:46:26 +00003152
Akira Hatanaka6c2cf8b2012-02-03 04:33:00 +00003153 return TargetLowering::getJumpTableEncoding();
3154}
Akira Hatanaka7887c902012-10-26 23:56:38 +00003155
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003156/// This function returns true if CallSym is a long double emulation routine.
3157static bool isF128SoftLibCall(const char *CallSym) {
3158 const char *const LibCalls[] =
3159 {"__addtf3", "__divtf3", "__eqtf2", "__extenddftf2", "__extendsftf2",
3160 "__fixtfdi", "__fixtfsi", "__fixtfti", "__fixunstfdi", "__fixunstfsi",
3161 "__fixunstfti", "__floatditf", "__floatsitf", "__floattitf",
3162 "__floatunditf", "__floatunsitf", "__floatuntitf", "__getf2", "__gttf2",
3163 "__letf2", "__lttf2", "__multf3", "__netf2", "__powitf2", "__subtf3",
3164 "__trunctfdf2", "__trunctfsf2", "__unordtf2",
3165 "ceill", "copysignl", "cosl", "exp2l", "expl", "floorl", "fmal", "fmodl",
3166 "log10l", "log2l", "logl", "nearbyintl", "powl", "rintl", "sinl", "sqrtl",
3167 "truncl"};
3168
3169 const char * const *End = LibCalls + array_lengthof(LibCalls);
3170
3171 // Check that LibCalls is sorted alphabetically.
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00003172 MipsTargetLowering::LTStr Comp;
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003173
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00003174#ifndef NDEBUG
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003175 for (const char * const *I = LibCalls; I < End - 1; ++I)
3176 assert(Comp(*I, *(I + 1)));
3177#endif
3178
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00003179 return std::binary_search(LibCalls, End, CallSym, Comp);
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003180}
3181
3182/// This function returns true if Ty is fp128 or i128 which was originally a
3183/// fp128.
3184static bool originalTypeIsF128(const Type *Ty, const SDNode *CallNode) {
3185 if (Ty->isFP128Ty())
3186 return true;
3187
3188 const ExternalSymbolSDNode *ES =
3189 dyn_cast_or_null<const ExternalSymbolSDNode>(CallNode);
3190
3191 // If the Ty is i128 and the function being called is a long double emulation
3192 // routine, then the original type is f128.
3193 return (ES && Ty->isIntegerTy(128) && isF128SoftLibCall(ES->getSymbol()));
3194}
3195
Reed Kotler46090912013-05-10 22:25:39 +00003196MipsTargetLowering::MipsCC::SpecialCallingConvType
3197 MipsTargetLowering::getSpecialCallingConv(SDValue Callee) const {
3198 MipsCC::SpecialCallingConvType SpecialCallingConv =
3199 MipsCC::NoSpecialCallingConv;;
3200 if (Subtarget->inMips16HardFloat()) {
3201 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3202 llvm::StringRef Sym = G->getGlobal()->getName();
3203 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
3204 if (F->hasFnAttribute("__Mips16RetHelper")) {
3205 SpecialCallingConv = MipsCC::Mips16RetHelperConv;
3206 }
3207 }
3208 }
3209 return SpecialCallingConv;
3210}
3211
3212MipsTargetLowering::MipsCC::MipsCC(
Akira Hatanakaad341d42013-08-20 23:38:40 +00003213 CallingConv::ID CC, bool IsO32_, bool IsFP64_, CCState &Info,
Reed Kotler46090912013-05-10 22:25:39 +00003214 MipsCC::SpecialCallingConvType SpecialCallingConv_)
Akira Hatanakaad341d42013-08-20 23:38:40 +00003215 : CCInfo(Info), CallConv(CC), IsO32(IsO32_), IsFP64(IsFP64_),
Reed Kotler46090912013-05-10 22:25:39 +00003216 SpecialCallingConv(SpecialCallingConv_){
Akira Hatanaka7887c902012-10-26 23:56:38 +00003217 // Pre-allocate reserved argument area.
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003218 CCInfo.AllocateStack(reservedArgArea(), 1);
Akira Hatanaka7887c902012-10-26 23:56:38 +00003219}
3220
Reed Kotler46090912013-05-10 22:25:39 +00003221
Akira Hatanaka7887c902012-10-26 23:56:38 +00003222void MipsTargetLowering::MipsCC::
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003223analyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Args,
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00003224 bool IsVarArg, bool IsSoftFloat, const SDNode *CallNode,
3225 std::vector<ArgListEntry> &FuncArgs) {
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003226 assert((CallConv != CallingConv::Fast || !IsVarArg) &&
3227 "CallingConv::Fast shouldn't be used for vararg functions.");
3228
Akira Hatanaka7887c902012-10-26 23:56:38 +00003229 unsigned NumOpnds = Args.size();
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003230 llvm::CCAssignFn *FixedFn = fixedArgFn(), *VarFn = varArgFn();
Akira Hatanaka7887c902012-10-26 23:56:38 +00003231
3232 for (unsigned I = 0; I != NumOpnds; ++I) {
3233 MVT ArgVT = Args[I].VT;
3234 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
3235 bool R;
3236
3237 if (ArgFlags.isByVal()) {
3238 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
3239 continue;
3240 }
3241
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003242 if (IsVarArg && !Args[I].IsFixed)
Akira Hatanaka7887c902012-10-26 23:56:38 +00003243 R = VarFn(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00003244 else {
3245 MVT RegVT = getRegVT(ArgVT, FuncArgs[Args[I].OrigArgIndex].Ty, CallNode,
3246 IsSoftFloat);
3247 R = FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo);
3248 }
Akira Hatanaka7887c902012-10-26 23:56:38 +00003249
3250 if (R) {
3251#ifndef NDEBUG
3252 dbgs() << "Call operand #" << I << " has unhandled type "
3253 << EVT(ArgVT).getEVTString();
3254#endif
3255 llvm_unreachable(0);
3256 }
3257 }
3258}
3259
3260void MipsTargetLowering::MipsCC::
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003261analyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Args,
3262 bool IsSoftFloat, Function::const_arg_iterator FuncArg) {
Akira Hatanaka7887c902012-10-26 23:56:38 +00003263 unsigned NumArgs = Args.size();
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003264 llvm::CCAssignFn *FixedFn = fixedArgFn();
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003265 unsigned CurArgIdx = 0;
Akira Hatanaka7887c902012-10-26 23:56:38 +00003266
3267 for (unsigned I = 0; I != NumArgs; ++I) {
3268 MVT ArgVT = Args[I].VT;
3269 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003270 std::advance(FuncArg, Args[I].OrigArgIndex - CurArgIdx);
3271 CurArgIdx = Args[I].OrigArgIndex;
Akira Hatanaka7887c902012-10-26 23:56:38 +00003272
3273 if (ArgFlags.isByVal()) {
3274 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
3275 continue;
3276 }
3277
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003278 MVT RegVT = getRegVT(ArgVT, FuncArg->getType(), 0, IsSoftFloat);
3279
3280 if (!FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo))
Akira Hatanaka7887c902012-10-26 23:56:38 +00003281 continue;
3282
3283#ifndef NDEBUG
3284 dbgs() << "Formal Arg #" << I << " has unhandled type "
3285 << EVT(ArgVT).getEVTString();
3286#endif
3287 llvm_unreachable(0);
3288 }
3289}
3290
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003291template<typename Ty>
3292void MipsTargetLowering::MipsCC::
3293analyzeReturn(const SmallVectorImpl<Ty> &RetVals, bool IsSoftFloat,
3294 const SDNode *CallNode, const Type *RetTy) const {
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003295 CCAssignFn *Fn;
3296
3297 if (IsSoftFloat && originalTypeIsF128(RetTy, CallNode))
3298 Fn = RetCC_F128Soft;
3299 else
3300 Fn = RetCC_Mips;
3301
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003302 for (unsigned I = 0, E = RetVals.size(); I < E; ++I) {
3303 MVT VT = RetVals[I].VT;
3304 ISD::ArgFlagsTy Flags = RetVals[I].Flags;
3305 MVT RegVT = this->getRegVT(VT, RetTy, CallNode, IsSoftFloat);
3306
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003307 if (Fn(I, VT, RegVT, CCValAssign::Full, Flags, this->CCInfo)) {
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003308#ifndef NDEBUG
3309 dbgs() << "Call result #" << I << " has unhandled type "
3310 << EVT(VT).getEVTString() << '\n';
3311#endif
3312 llvm_unreachable(0);
3313 }
3314 }
3315}
3316
3317void MipsTargetLowering::MipsCC::
3318analyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins, bool IsSoftFloat,
3319 const SDNode *CallNode, const Type *RetTy) const {
3320 analyzeReturn(Ins, IsSoftFloat, CallNode, RetTy);
3321}
3322
3323void MipsTargetLowering::MipsCC::
3324analyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs, bool IsSoftFloat,
3325 const Type *RetTy) const {
3326 analyzeReturn(Outs, IsSoftFloat, 0, RetTy);
3327}
3328
Akira Hatanaka7887c902012-10-26 23:56:38 +00003329void
3330MipsTargetLowering::MipsCC::handleByValArg(unsigned ValNo, MVT ValVT,
3331 MVT LocVT,
3332 CCValAssign::LocInfo LocInfo,
3333 ISD::ArgFlagsTy ArgFlags) {
3334 assert(ArgFlags.getByValSize() && "Byval argument's size shouldn't be 0.");
3335
3336 struct ByValArgInfo ByVal;
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003337 unsigned RegSize = regSize();
Akira Hatanaka7887c902012-10-26 23:56:38 +00003338 unsigned ByValSize = RoundUpToAlignment(ArgFlags.getByValSize(), RegSize);
3339 unsigned Align = std::min(std::max(ArgFlags.getByValAlign(), RegSize),
3340 RegSize * 2);
3341
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003342 if (useRegsForByval())
Akira Hatanaka7887c902012-10-26 23:56:38 +00003343 allocateRegs(ByVal, ByValSize, Align);
3344
3345 // Allocate space on caller's stack.
3346 ByVal.Address = CCInfo.AllocateStack(ByValSize - RegSize * ByVal.NumRegs,
3347 Align);
3348 CCInfo.addLoc(CCValAssign::getMem(ValNo, ValVT, ByVal.Address, LocVT,
3349 LocInfo));
3350 ByValArgs.push_back(ByVal);
3351}
3352
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003353unsigned MipsTargetLowering::MipsCC::numIntArgRegs() const {
3354 return IsO32 ? array_lengthof(O32IntRegs) : array_lengthof(Mips64IntRegs);
3355}
3356
3357unsigned MipsTargetLowering::MipsCC::reservedArgArea() const {
3358 return (IsO32 && (CallConv != CallingConv::Fast)) ? 16 : 0;
3359}
3360
3361const uint16_t *MipsTargetLowering::MipsCC::intArgRegs() const {
3362 return IsO32 ? O32IntRegs : Mips64IntRegs;
3363}
3364
3365llvm::CCAssignFn *MipsTargetLowering::MipsCC::fixedArgFn() const {
3366 if (CallConv == CallingConv::Fast)
3367 return CC_Mips_FastCC;
3368
Reed Kotler46090912013-05-10 22:25:39 +00003369 if (SpecialCallingConv == Mips16RetHelperConv)
3370 return CC_Mips16RetHelper;
Akira Hatanakaad341d42013-08-20 23:38:40 +00003371 return IsO32 ? (IsFP64 ? CC_MipsO32_FP64 : CC_MipsO32_FP32) : CC_MipsN;
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003372}
3373
3374llvm::CCAssignFn *MipsTargetLowering::MipsCC::varArgFn() const {
Akira Hatanakaad341d42013-08-20 23:38:40 +00003375 return IsO32 ? (IsFP64 ? CC_MipsO32_FP64 : CC_MipsO32_FP32) : CC_MipsN_VarArg;
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003376}
3377
3378const uint16_t *MipsTargetLowering::MipsCC::shadowRegs() const {
3379 return IsO32 ? O32IntRegs : Mips64DPRegs;
3380}
3381
Akira Hatanaka7887c902012-10-26 23:56:38 +00003382void MipsTargetLowering::MipsCC::allocateRegs(ByValArgInfo &ByVal,
3383 unsigned ByValSize,
3384 unsigned Align) {
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003385 unsigned RegSize = regSize(), NumIntArgRegs = numIntArgRegs();
3386 const uint16_t *IntArgRegs = intArgRegs(), *ShadowRegs = shadowRegs();
Akira Hatanaka7887c902012-10-26 23:56:38 +00003387 assert(!(ByValSize % RegSize) && !(Align % RegSize) &&
3388 "Byval argument's size and alignment should be a multiple of"
3389 "RegSize.");
3390
3391 ByVal.FirstIdx = CCInfo.getFirstUnallocated(IntArgRegs, NumIntArgRegs);
3392
3393 // If Align > RegSize, the first arg register must be even.
3394 if ((Align > RegSize) && (ByVal.FirstIdx % 2)) {
3395 CCInfo.AllocateReg(IntArgRegs[ByVal.FirstIdx], ShadowRegs[ByVal.FirstIdx]);
3396 ++ByVal.FirstIdx;
3397 }
3398
3399 // Mark the registers allocated.
3400 for (unsigned I = ByVal.FirstIdx; ByValSize && (I < NumIntArgRegs);
3401 ByValSize -= RegSize, ++I, ++ByVal.NumRegs)
3402 CCInfo.AllocateReg(IntArgRegs[I], ShadowRegs[I]);
3403}
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003404
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003405MVT MipsTargetLowering::MipsCC::getRegVT(MVT VT, const Type *OrigTy,
3406 const SDNode *CallNode,
3407 bool IsSoftFloat) const {
3408 if (IsSoftFloat || IsO32)
3409 return VT;
3410
3411 // Check if the original type was fp128.
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003412 if (originalTypeIsF128(OrigTy, CallNode)) {
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003413 assert(VT == MVT::i64);
3414 return MVT::f64;
3415 }
3416
3417 return VT;
3418}
3419
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003420void MipsTargetLowering::
Andrew Trickac6d9be2013-05-25 02:42:55 +00003421copyByValRegs(SDValue Chain, SDLoc DL, std::vector<SDValue> &OutChains,
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003422 SelectionDAG &DAG, const ISD::ArgFlagsTy &Flags,
3423 SmallVectorImpl<SDValue> &InVals, const Argument *FuncArg,
3424 const MipsCC &CC, const ByValArgInfo &ByVal) const {
3425 MachineFunction &MF = DAG.getMachineFunction();
3426 MachineFrameInfo *MFI = MF.getFrameInfo();
3427 unsigned RegAreaSize = ByVal.NumRegs * CC.regSize();
3428 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
3429 int FrameObjOffset;
3430
3431 if (RegAreaSize)
3432 FrameObjOffset = (int)CC.reservedArgArea() -
3433 (int)((CC.numIntArgRegs() - ByVal.FirstIdx) * CC.regSize());
3434 else
3435 FrameObjOffset = ByVal.Address;
3436
3437 // Create frame object.
3438 EVT PtrTy = getPointerTy();
3439 int FI = MFI->CreateFixedObject(FrameObjSize, FrameObjOffset, true);
3440 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
3441 InVals.push_back(FIN);
3442
3443 if (!ByVal.NumRegs)
3444 return;
3445
3446 // Copy arg registers.
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00003447 MVT RegTy = MVT::getIntegerVT(CC.regSize() * 8);
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003448 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3449
3450 for (unsigned I = 0; I < ByVal.NumRegs; ++I) {
3451 unsigned ArgReg = CC.intArgRegs()[ByVal.FirstIdx + I];
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003452 unsigned VReg = addLiveIn(MF, ArgReg, RC);
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003453 unsigned Offset = I * CC.regSize();
3454 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
3455 DAG.getConstant(Offset, PtrTy));
3456 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
3457 StorePtr, MachinePointerInfo(FuncArg, Offset),
3458 false, false, 0);
3459 OutChains.push_back(Store);
3460 }
3461}
Akira Hatanakadb40ede2012-10-27 00:16:36 +00003462
3463// Copy byVal arg to registers and stack.
3464void MipsTargetLowering::
Andrew Trickac6d9be2013-05-25 02:42:55 +00003465passByValArg(SDValue Chain, SDLoc DL,
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00003466 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
Craig Toppera0ec3f92013-07-14 04:42:23 +00003467 SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr,
Akira Hatanakadb40ede2012-10-27 00:16:36 +00003468 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
3469 const MipsCC &CC, const ByValArgInfo &ByVal,
3470 const ISD::ArgFlagsTy &Flags, bool isLittle) const {
3471 unsigned ByValSize = Flags.getByValSize();
3472 unsigned Offset = 0; // Offset in # of bytes from the beginning of struct.
3473 unsigned RegSize = CC.regSize();
3474 unsigned Alignment = std::min(Flags.getByValAlign(), RegSize);
3475 EVT PtrTy = getPointerTy(), RegTy = MVT::getIntegerVT(RegSize * 8);
3476
3477 if (ByVal.NumRegs) {
3478 const uint16_t *ArgRegs = CC.intArgRegs();
3479 bool LeftoverBytes = (ByVal.NumRegs * RegSize > ByValSize);
3480 unsigned I = 0;
3481
3482 // Copy words to registers.
3483 for (; I < ByVal.NumRegs - LeftoverBytes; ++I, Offset += RegSize) {
3484 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3485 DAG.getConstant(Offset, PtrTy));
3486 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
3487 MachinePointerInfo(), false, false, false,
3488 Alignment);
3489 MemOpChains.push_back(LoadVal.getValue(1));
3490 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3491 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
3492 }
3493
3494 // Return if the struct has been fully copied.
3495 if (ByValSize == Offset)
3496 return;
3497
3498 // Copy the remainder of the byval argument with sub-word loads and shifts.
3499 if (LeftoverBytes) {
3500 assert((ByValSize > Offset) && (ByValSize < Offset + RegSize) &&
3501 "Size of the remainder should be smaller than RegSize.");
3502 SDValue Val;
3503
3504 for (unsigned LoadSize = RegSize / 2, TotalSizeLoaded = 0;
3505 Offset < ByValSize; LoadSize /= 2) {
3506 unsigned RemSize = ByValSize - Offset;
3507
3508 if (RemSize < LoadSize)
3509 continue;
3510
3511 // Load subword.
3512 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3513 DAG.getConstant(Offset, PtrTy));
3514 SDValue LoadVal =
3515 DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr,
3516 MachinePointerInfo(), MVT::getIntegerVT(LoadSize * 8),
3517 false, false, Alignment);
3518 MemOpChains.push_back(LoadVal.getValue(1));
3519
3520 // Shift the loaded value.
3521 unsigned Shamt;
3522
3523 if (isLittle)
3524 Shamt = TotalSizeLoaded;
3525 else
3526 Shamt = (RegSize - (TotalSizeLoaded + LoadSize)) * 8;
3527
3528 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
3529 DAG.getConstant(Shamt, MVT::i32));
3530
3531 if (Val.getNode())
3532 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
3533 else
3534 Val = Shift;
3535
3536 Offset += LoadSize;
3537 TotalSizeLoaded += LoadSize;
3538 Alignment = std::min(Alignment, LoadSize);
3539 }
3540
3541 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3542 RegsToPass.push_back(std::make_pair(ArgReg, Val));
3543 return;
3544 }
3545 }
3546
3547 // Copy remainder of byval arg to it with memcpy.
3548 unsigned MemCpySize = ByValSize - Offset;
3549 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3550 DAG.getConstant(Offset, PtrTy));
3551 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
3552 DAG.getIntPtrConstant(ByVal.Address));
3553 Chain = DAG.getMemcpy(Chain, DL, Dst, Src,
3554 DAG.getConstant(MemCpySize, PtrTy), Alignment,
3555 /*isVolatile=*/false, /*AlwaysInline=*/false,
3556 MachinePointerInfo(0), MachinePointerInfo(0));
3557 MemOpChains.push_back(Chain);
3558}
Akira Hatanakaf0848472012-10-27 00:21:13 +00003559
3560void
3561MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
3562 const MipsCC &CC, SDValue Chain,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003563 SDLoc DL, SelectionDAG &DAG) const {
Akira Hatanakaf0848472012-10-27 00:21:13 +00003564 unsigned NumRegs = CC.numIntArgRegs();
3565 const uint16_t *ArgRegs = CC.intArgRegs();
3566 const CCState &CCInfo = CC.getCCInfo();
3567 unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs, NumRegs);
3568 unsigned RegSize = CC.regSize();
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00003569 MVT RegTy = MVT::getIntegerVT(RegSize * 8);
Akira Hatanakaf0848472012-10-27 00:21:13 +00003570 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3571 MachineFunction &MF = DAG.getMachineFunction();
3572 MachineFrameInfo *MFI = MF.getFrameInfo();
3573 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3574
3575 // Offset of the first variable argument from stack pointer.
3576 int VaArgOffset;
3577
3578 if (NumRegs == Idx)
3579 VaArgOffset = RoundUpToAlignment(CCInfo.getNextStackOffset(), RegSize);
3580 else
3581 VaArgOffset =
3582 (int)CC.reservedArgArea() - (int)(RegSize * (NumRegs - Idx));
3583
3584 // Record the frame index of the first variable argument
3585 // which is a value necessary to VASTART.
3586 int FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
3587 MipsFI->setVarArgsFrameIndex(FI);
3588
3589 // Copy the integer registers that have not been used for argument passing
3590 // to the argument register save area. For O32, the save area is allocated
3591 // in the caller's stack frame, while for N32/64, it is allocated in the
3592 // callee's stack frame.
3593 for (unsigned I = Idx; I < NumRegs; ++I, VaArgOffset += RegSize) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003594 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC);
Akira Hatanakaf0848472012-10-27 00:21:13 +00003595 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
3596 FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
3597 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
3598 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
3599 MachinePointerInfo(), false, false, 0);
3600 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(0);
3601 OutChains.push_back(Store);
3602 }
3603}