blob: ad278c280579191e2cd8b595568024533ee7929e [file] [log] [blame]
Jia Liuf54f60f2012-02-28 07:46:26 +00001//===-- MipsISelLowering.cpp - Mips DAG Lowering Implementation -----------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Lopes35e43c42007-06-06 07:42:06 +00007//
Akira Hatanakae2489122011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-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 Hatanakae2489122011-04-15 21:51:11 +000013//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000014#include "MipsISelLowering.h"
Craig Topperb25fda92012-03-17 18:46:09 +000015#include "InstPrinter/MipsInstPrinter.h"
16#include "MCTargetDesc/MipsBaseInfo.h"
Daniel Sanders0456c152014-11-07 14:24:31 +000017#include "MipsCCState.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "MipsMachineFunction.h"
19#include "MipsSubtarget.h"
20#include "MipsTargetMachine.h"
21#include "MipsTargetObjectFile.h"
Akira Hatanaka90131ac2012-10-19 21:47:33 +000022#include "llvm/ADT/Statistic.h"
Daniel Sanders8b59af12013-11-12 12:56:01 +000023#include "llvm/ADT/StringSwitch.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000024#include "llvm/CodeGen/CallingConvLower.h"
25#include "llvm/CodeGen/MachineFrameInfo.h"
26#include "llvm/CodeGen/MachineFunction.h"
27#include "llvm/CodeGen/MachineInstrBuilder.h"
Eric Christopher79cc1e32014-09-02 22:28:02 +000028#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000029#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000030#include "llvm/CodeGen/SelectionDAGISel.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000031#include "llvm/CodeGen/ValueTypes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000032#include "llvm/IR/CallingConv.h"
33#include "llvm/IR/DerivedTypes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000034#include "llvm/IR/GlobalVariable.h"
Akira Hatanaka90131ac2012-10-19 21:47:33 +000035#include "llvm/Support/CommandLine.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000036#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000037#include "llvm/Support/ErrorHandling.h"
NAKAMURA Takumie30303f2012-04-21 15:31:45 +000038#include "llvm/Support/raw_ostream.h"
Akira Hatanaka7473b472013-08-14 00:21:25 +000039#include <cctype>
NAKAMURA Takumie30303f2012-04-21 15:31:45 +000040
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000041using namespace llvm;
42
Chandler Carruth84e68b22014-04-22 02:41:26 +000043#define DEBUG_TYPE "mips-lower"
44
Akira Hatanaka90131ac2012-10-19 21:47:33 +000045STATISTIC(NumTailCalls, "Number of tail calls");
46
47static cl::opt<bool>
Akira Hatanaka59f299f2012-11-21 20:21:11 +000048LargeGOT("mxgot", cl::Hidden,
49 cl::desc("MIPS: Enable GOT larger than 64k."), cl::init(false));
50
Akira Hatanaka1cb02422013-05-20 18:07:43 +000051static cl::opt<bool>
Akira Hatanakabe76cd02013-05-21 17:17:59 +000052NoZeroDivCheck("mno-check-zero-division", cl::Hidden,
Akira Hatanaka1cb02422013-05-20 18:07:43 +000053 cl::desc("MIPS: Don't trap on integer division by zero."),
54 cl::init(false));
55
Reed Kotler720c5ca2014-04-17 22:15:34 +000056cl::opt<bool>
57EnableMipsFastISel("mips-fast-isel", cl::Hidden,
58 cl::desc("Allow mips-fast-isel to be used"),
59 cl::init(false));
60
Craig Topper840beec2014-04-04 05:16:06 +000061static const MCPhysReg Mips64DPRegs[8] = {
Akira Hatanakaac8c6692012-10-27 00:29:43 +000062 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 Liuf54f60f2012-02-28 07:46:26 +000066// If I is a shifted mask, set the size (Size) and the first bit of the
Akira Hatanaka73d78b72011-08-18 20:07:42 +000067// mask (Pos), and return true.
Jia Liuf54f60f2012-02-28 07:46:26 +000068// For example, if I is 0x003ff800, (Pos, Size) = (11, 11).
Akira Hatanaka0bb60d892013-03-12 00:16:36 +000069static bool isShiftedMask(uint64_t I, uint64_t &Pos, uint64_t &Size) {
Akira Hatanaka20cee2e2011-12-05 21:26:34 +000070 if (!isShiftedMask_64(I))
Akira Hatanaka4c0a7122013-10-07 19:33:02 +000071 return false;
Akira Hatanaka5360f882011-08-17 02:05:42 +000072
Benjamin Kramer5f6a9072015-02-12 15:35:40 +000073 Size = countPopulation(I);
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +000074 Pos = countTrailingZeros(I);
Akira Hatanaka73d78b72011-08-18 20:07:42 +000075 return true;
Akira Hatanaka5360f882011-08-17 02:05:42 +000076}
77
Akira Hatanaka96ca1822013-03-13 00:54:29 +000078SDValue MipsTargetLowering::getGlobalReg(SelectionDAG &DAG, EVT Ty) const {
Akira Hatanakab049aef2012-02-24 22:34:47 +000079 MipsFunctionInfo *FI = DAG.getMachineFunction().getInfo<MipsFunctionInfo>();
80 return DAG.getRegister(FI->getGlobalBaseReg(), Ty);
81}
82
Akira Hatanakad8f10ce2013-09-27 19:51:35 +000083SDValue MipsTargetLowering::getTargetNode(GlobalAddressSDNode *N, EVT Ty,
84 SelectionDAG &DAG,
Akira Hatanaka96ca1822013-03-13 00:54:29 +000085 unsigned Flag) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +000086 return DAG.getTargetGlobalAddress(N->getGlobal(), SDLoc(N), Ty, 0, Flag);
Akira Hatanakafd04ad42012-11-21 20:26:38 +000087}
88
Akira Hatanakad8f10ce2013-09-27 19:51:35 +000089SDValue MipsTargetLowering::getTargetNode(ExternalSymbolSDNode *N, EVT Ty,
90 SelectionDAG &DAG,
91 unsigned Flag) const {
92 return DAG.getTargetExternalSymbol(N->getSymbol(), Ty, Flag);
93}
94
95SDValue MipsTargetLowering::getTargetNode(BlockAddressSDNode *N, EVT Ty,
96 SelectionDAG &DAG,
97 unsigned Flag) const {
98 return DAG.getTargetBlockAddress(N->getBlockAddress(), Ty, 0, Flag);
99}
100
101SDValue MipsTargetLowering::getTargetNode(JumpTableSDNode *N, EVT Ty,
102 SelectionDAG &DAG,
103 unsigned Flag) const {
104 return DAG.getTargetJumpTable(N->getIndex(), Ty, Flag);
105}
106
107SDValue MipsTargetLowering::getTargetNode(ConstantPoolSDNode *N, EVT Ty,
108 SelectionDAG &DAG,
109 unsigned Flag) const {
110 return DAG.getTargetConstantPool(N->getConstVal(), Ty, N->getAlignment(),
111 N->getOffset(), Flag);
Akira Hatanakafd04ad42012-11-21 20:26:38 +0000112}
113
Chris Lattner5e693ed2009-07-28 03:13:23 +0000114const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +0000115 switch ((MipsISD::NodeType)Opcode) {
116 case MipsISD::FIRST_NUMBER: break;
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000117 case MipsISD::JmpLink: return "MipsISD::JmpLink";
Akira Hatanaka91318df2012-10-19 20:59:39 +0000118 case MipsISD::TailCall: return "MipsISD::TailCall";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000119 case MipsISD::Hi: return "MipsISD::Hi";
120 case MipsISD::Lo: return "MipsISD::Lo";
121 case MipsISD::GPRel: return "MipsISD::GPRel";
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +0000122 case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000123 case MipsISD::Ret: return "MipsISD::Ret";
Akira Hatanakac0b02062013-01-30 00:26:49 +0000124 case MipsISD::EH_RETURN: return "MipsISD::EH_RETURN";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000125 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
126 case MipsISD::FPCmp: return "MipsISD::FPCmp";
127 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
128 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000129 case MipsISD::TruncIntFP: return "MipsISD::TruncIntFP";
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000130 case MipsISD::MFHI: return "MipsISD::MFHI";
131 case MipsISD::MFLO: return "MipsISD::MFLO";
132 case MipsISD::MTLOHI: return "MipsISD::MTLOHI";
Akira Hatanaka28721bd2013-03-30 01:14:04 +0000133 case MipsISD::Mult: return "MipsISD::Mult";
134 case MipsISD::Multu: return "MipsISD::Multu";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000135 case MipsISD::MAdd: return "MipsISD::MAdd";
136 case MipsISD::MAddu: return "MipsISD::MAddu";
137 case MipsISD::MSub: return "MipsISD::MSub";
138 case MipsISD::MSubu: return "MipsISD::MSubu";
139 case MipsISD::DivRem: return "MipsISD::DivRem";
140 case MipsISD::DivRemU: return "MipsISD::DivRemU";
Akira Hatanaka28721bd2013-03-30 01:14:04 +0000141 case MipsISD::DivRem16: return "MipsISD::DivRem16";
142 case MipsISD::DivRemU16: return "MipsISD::DivRemU16";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000143 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
144 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
Akira Hatanakafaa88c02011-12-12 22:38:19 +0000145 case MipsISD::Wrapper: return "MipsISD::Wrapper";
Matthias Braund04893f2015-05-07 21:33:59 +0000146 case MipsISD::DynAlloc: return "MipsISD::DynAlloc";
Akira Hatanakaa4c09bc2011-07-19 23:30:50 +0000147 case MipsISD::Sync: return "MipsISD::Sync";
Akira Hatanaka5360f882011-08-17 02:05:42 +0000148 case MipsISD::Ext: return "MipsISD::Ext";
149 case MipsISD::Ins: return "MipsISD::Ins";
Akira Hatanakab9ebf8d2012-06-02 00:03:12 +0000150 case MipsISD::LWL: return "MipsISD::LWL";
151 case MipsISD::LWR: return "MipsISD::LWR";
152 case MipsISD::SWL: return "MipsISD::SWL";
153 case MipsISD::SWR: return "MipsISD::SWR";
154 case MipsISD::LDL: return "MipsISD::LDL";
155 case MipsISD::LDR: return "MipsISD::LDR";
156 case MipsISD::SDL: return "MipsISD::SDL";
157 case MipsISD::SDR: return "MipsISD::SDR";
Akira Hatanaka233ac532012-09-21 23:52:47 +0000158 case MipsISD::EXTP: return "MipsISD::EXTP";
159 case MipsISD::EXTPDP: return "MipsISD::EXTPDP";
160 case MipsISD::EXTR_S_H: return "MipsISD::EXTR_S_H";
161 case MipsISD::EXTR_W: return "MipsISD::EXTR_W";
162 case MipsISD::EXTR_R_W: return "MipsISD::EXTR_R_W";
163 case MipsISD::EXTR_RS_W: return "MipsISD::EXTR_RS_W";
164 case MipsISD::SHILO: return "MipsISD::SHILO";
165 case MipsISD::MTHLIP: return "MipsISD::MTHLIP";
Matthias Braund04893f2015-05-07 21:33:59 +0000166 case MipsISD::MULSAQ_S_W_PH: return "MipsISD::MULSAQ_S_W_PH";
167 case MipsISD::MAQ_S_W_PHL: return "MipsISD::MAQ_S_W_PHL";
168 case MipsISD::MAQ_S_W_PHR: return "MipsISD::MAQ_S_W_PHR";
169 case MipsISD::MAQ_SA_W_PHL: return "MipsISD::MAQ_SA_W_PHL";
170 case MipsISD::MAQ_SA_W_PHR: return "MipsISD::MAQ_SA_W_PHR";
171 case MipsISD::DPAU_H_QBL: return "MipsISD::DPAU_H_QBL";
172 case MipsISD::DPAU_H_QBR: return "MipsISD::DPAU_H_QBR";
173 case MipsISD::DPSU_H_QBL: return "MipsISD::DPSU_H_QBL";
174 case MipsISD::DPSU_H_QBR: return "MipsISD::DPSU_H_QBR";
175 case MipsISD::DPAQ_S_W_PH: return "MipsISD::DPAQ_S_W_PH";
176 case MipsISD::DPSQ_S_W_PH: return "MipsISD::DPSQ_S_W_PH";
177 case MipsISD::DPAQ_SA_L_W: return "MipsISD::DPAQ_SA_L_W";
178 case MipsISD::DPSQ_SA_L_W: return "MipsISD::DPSQ_SA_L_W";
179 case MipsISD::DPA_W_PH: return "MipsISD::DPA_W_PH";
180 case MipsISD::DPS_W_PH: return "MipsISD::DPS_W_PH";
181 case MipsISD::DPAQX_S_W_PH: return "MipsISD::DPAQX_S_W_PH";
182 case MipsISD::DPAQX_SA_W_PH: return "MipsISD::DPAQX_SA_W_PH";
183 case MipsISD::DPAX_W_PH: return "MipsISD::DPAX_W_PH";
184 case MipsISD::DPSX_W_PH: return "MipsISD::DPSX_W_PH";
185 case MipsISD::DPSQX_S_W_PH: return "MipsISD::DPSQX_S_W_PH";
186 case MipsISD::DPSQX_SA_W_PH: return "MipsISD::DPSQX_SA_W_PH";
187 case MipsISD::MULSA_W_PH: return "MipsISD::MULSA_W_PH";
Akira Hatanaka233ac532012-09-21 23:52:47 +0000188 case MipsISD::MULT: return "MipsISD::MULT";
189 case MipsISD::MULTU: return "MipsISD::MULTU";
Jia Liu434874d2013-03-04 01:06:54 +0000190 case MipsISD::MADD_DSP: return "MipsISD::MADD_DSP";
Akira Hatanaka233ac532012-09-21 23:52:47 +0000191 case MipsISD::MADDU_DSP: return "MipsISD::MADDU_DSP";
192 case MipsISD::MSUB_DSP: return "MipsISD::MSUB_DSP";
193 case MipsISD::MSUBU_DSP: return "MipsISD::MSUBU_DSP";
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000194 case MipsISD::SHLL_DSP: return "MipsISD::SHLL_DSP";
195 case MipsISD::SHRA_DSP: return "MipsISD::SHRA_DSP";
196 case MipsISD::SHRL_DSP: return "MipsISD::SHRL_DSP";
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000197 case MipsISD::SETCC_DSP: return "MipsISD::SETCC_DSP";
198 case MipsISD::SELECT_CC_DSP: return "MipsISD::SELECT_CC_DSP";
Daniel Sandersce09d072013-08-28 12:14:50 +0000199 case MipsISD::VALL_ZERO: return "MipsISD::VALL_ZERO";
200 case MipsISD::VANY_ZERO: return "MipsISD::VANY_ZERO";
201 case MipsISD::VALL_NONZERO: return "MipsISD::VALL_NONZERO";
202 case MipsISD::VANY_NONZERO: return "MipsISD::VANY_NONZERO";
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000203 case MipsISD::VCEQ: return "MipsISD::VCEQ";
204 case MipsISD::VCLE_S: return "MipsISD::VCLE_S";
205 case MipsISD::VCLE_U: return "MipsISD::VCLE_U";
206 case MipsISD::VCLT_S: return "MipsISD::VCLT_S";
207 case MipsISD::VCLT_U: return "MipsISD::VCLT_U";
Daniel Sanders3ce56622013-09-24 12:18:31 +0000208 case MipsISD::VSMAX: return "MipsISD::VSMAX";
209 case MipsISD::VSMIN: return "MipsISD::VSMIN";
210 case MipsISD::VUMAX: return "MipsISD::VUMAX";
211 case MipsISD::VUMIN: return "MipsISD::VUMIN";
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000212 case MipsISD::VEXTRACT_SEXT_ELT: return "MipsISD::VEXTRACT_SEXT_ELT";
213 case MipsISD::VEXTRACT_ZEXT_ELT: return "MipsISD::VEXTRACT_ZEXT_ELT";
Daniel Sandersf7456c72013-09-23 13:22:24 +0000214 case MipsISD::VNOR: return "MipsISD::VNOR";
Daniel Sanderse5087042013-09-24 14:02:15 +0000215 case MipsISD::VSHF: return "MipsISD::VSHF";
Daniel Sanders26307182013-09-24 14:20:00 +0000216 case MipsISD::SHF: return "MipsISD::SHF";
Daniel Sanders2ed228b2013-09-24 14:36:12 +0000217 case MipsISD::ILVEV: return "MipsISD::ILVEV";
218 case MipsISD::ILVOD: return "MipsISD::ILVOD";
219 case MipsISD::ILVL: return "MipsISD::ILVL";
220 case MipsISD::ILVR: return "MipsISD::ILVR";
Daniel Sandersfae5f2a2013-09-24 14:53:25 +0000221 case MipsISD::PCKEV: return "MipsISD::PCKEV";
222 case MipsISD::PCKOD: return "MipsISD::PCKOD";
Daniel Sandersb50ccf82014-04-01 10:35:28 +0000223 case MipsISD::INSVE: return "MipsISD::INSVE";
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000224 }
Matthias Braund04893f2015-05-07 21:33:59 +0000225 return nullptr;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000226}
227
Eric Christopherb1526602014-09-19 23:30:42 +0000228MipsTargetLowering::MipsTargetLowering(const MipsTargetMachine &TM,
Eric Christopher8924d272014-07-18 23:25:04 +0000229 const MipsSubtarget &STI)
Eric Christopher96e72c62015-01-29 23:27:36 +0000230 : TargetLowering(TM), Subtarget(STI), ABI(TM.getABI()) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000231 // Mips does not have i1 type, so use i32 for
Wesley Peck527da1b2010-11-23 03:31:01 +0000232 // setcc operations results (slt, sgt, ...).
Duncan Sands8d6e2e12008-11-23 15:47:28 +0000233 setBooleanContents(ZeroOrOneBooleanContent);
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000234 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Daniel Sanderscbd44c52014-07-10 10:18:12 +0000235 // The cmp.cond.fmt instruction in MIPS32r6/MIPS64r6 uses 0 and -1 like MSA
236 // does. Integer booleans still use 0 and 1.
Eric Christopher1c29a652014-07-18 22:55:25 +0000237 if (Subtarget.hasMips32r6())
Daniel Sanderscbd44c52014-07-10 10:18:12 +0000238 setBooleanContents(ZeroOrOneBooleanContent,
239 ZeroOrNegativeOneBooleanContent);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000240
Wesley Peck527da1b2010-11-23 03:31:01 +0000241 // Load extented operations for i1 types must be promoted
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000242 for (MVT VT : MVT::integer_valuetypes()) {
243 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
244 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
245 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
246 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000247
Pirama Arumuga Nainar34056de2015-04-20 20:15:36 +0000248 // MIPS doesn't have extending float->double load/store. Set LoadExtAction
249 // for f32, f16
250 for (MVT VT : MVT::fp_valuetypes()) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000251 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
Pirama Arumuga Nainar34056de2015-04-20 20:15:36 +0000252 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f16, Expand);
253 }
254
255 // Set LoadExtAction for f16 vectors to Expand
256 for (MVT VT : MVT::fp_vector_valuetypes()) {
257 MVT F16VT = MVT::getVectorVT(MVT::f16, VT.getVectorNumElements());
258 if (F16VT.isValid())
259 setLoadExtAction(ISD::EXTLOAD, VT, F16VT, Expand);
260 }
261
262 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
263 setTruncStoreAction(MVT::f64, MVT::f16, Expand);
264
Owen Anderson9f944592009-08-11 20:47:22 +0000265 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman39d6faa2009-07-17 02:28:12 +0000266
Wesley Peck527da1b2010-11-23 03:31:01 +0000267 // Used by legalize types to correctly generate the setcc result.
268 // Without this, every float setcc comes with a AND/OR with the result,
269 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +0000270 // which is used implicitly by brcond and select operations.
Owen Anderson9f944592009-08-11 20:47:22 +0000271 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +0000272
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000273 // Mips Custom Operations
Akira Hatanaka0f693a82013-03-06 21:32:03 +0000274 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000275 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000276 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000277 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
278 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
279 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
280 setOperationAction(ISD::SELECT, MVT::f32, Custom);
281 setOperationAction(ISD::SELECT, MVT::f64, Custom);
282 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Akira Hatanaka24cf4e32012-07-11 19:32:27 +0000283 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
284 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Akira Hatanakab7f78592012-03-09 23:46:03 +0000285 setOperationAction(ISD::SETCC, MVT::f32, Custom);
286 setOperationAction(ISD::SETCC, MVT::f64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000287 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000288 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
289 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000290 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000291
Eric Christopher1c29a652014-07-18 22:55:25 +0000292 if (Subtarget.isGP64bit()) {
Akira Hatanakada00aa82012-03-10 00:03:50 +0000293 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
294 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
295 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
296 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
297 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
298 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Akira Hatanaka019e5922012-06-02 00:04:42 +0000299 setOperationAction(ISD::LOAD, MVT::i64, Custom);
300 setOperationAction(ISD::STORE, MVT::i64, Custom);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000301 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +0000302 setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom);
303 setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom);
304 setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000305 }
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +0000306
Eric Christopher1c29a652014-07-18 22:55:25 +0000307 if (!Subtarget.isGP64bit()) {
Akira Hatanaka0a8ab712012-05-09 00:55:21 +0000308 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
309 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
310 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
311 }
312
Akira Hatanaka28e02ec2012-11-07 19:10:58 +0000313 setOperationAction(ISD::ADD, MVT::i32, Custom);
Eric Christopher1c29a652014-07-18 22:55:25 +0000314 if (Subtarget.isGP64bit())
Akira Hatanaka28e02ec2012-11-07 19:10:58 +0000315 setOperationAction(ISD::ADD, MVT::i64, Custom);
316
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000317 setOperationAction(ISD::SDIV, MVT::i32, Expand);
318 setOperationAction(ISD::SREM, MVT::i32, Expand);
319 setOperationAction(ISD::UDIV, MVT::i32, Expand);
320 setOperationAction(ISD::UREM, MVT::i32, Expand);
Akira Hatanakab1538f92011-10-03 21:06:13 +0000321 setOperationAction(ISD::SDIV, MVT::i64, Expand);
322 setOperationAction(ISD::SREM, MVT::i64, Expand);
323 setOperationAction(ISD::UDIV, MVT::i64, Expand);
324 setOperationAction(ISD::UREM, MVT::i64, Expand);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000325
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000326 // Operations not directly supported by Mips.
Tom Stellardb1588fc2013-03-08 15:36:57 +0000327 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
328 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
329 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
330 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
Tom Stellard3787b122014-06-10 16:01:29 +0000331 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
332 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000333 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Akira Hatanaka79aed152011-12-20 23:40:56 +0000334 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000335 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Akira Hatanaka79aed152011-12-20 23:40:56 +0000336 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000337 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Eric Christopher1c29a652014-07-18 22:55:25 +0000338 if (Subtarget.hasCnMips()) {
Kai Nacke93fe5e82014-03-20 11:51:58 +0000339 setOperationAction(ISD::CTPOP, MVT::i32, Legal);
340 setOperationAction(ISD::CTPOP, MVT::i64, Legal);
341 } else {
342 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
343 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
344 }
Owen Anderson9f944592009-08-11 20:47:22 +0000345 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
Akira Hatanaka410ce9c2011-12-21 00:14:05 +0000346 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000347 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
348 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
349 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
350 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000351 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Akira Hatanaka7ba8a8d2011-09-30 18:51:46 +0000352 setOperationAction(ISD::ROTL, MVT::i64, Expand);
Akira Hatanaka33a25af2012-07-31 20:54:48 +0000353 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
354 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Bruno Cardoso Lopesd47180e2010-12-09 17:32:30 +0000355
Eric Christopher1c29a652014-07-18 22:55:25 +0000356 if (!Subtarget.hasMips32r2())
Bruno Cardoso Lopesd47180e2010-12-09 17:32:30 +0000357 setOperationAction(ISD::ROTR, MVT::i32, Expand);
358
Eric Christopher1c29a652014-07-18 22:55:25 +0000359 if (!Subtarget.hasMips64r2())
Akira Hatanaka7ba8a8d2011-09-30 18:51:46 +0000360 setOperationAction(ISD::ROTR, MVT::i64, Expand);
361
Owen Anderson9f944592009-08-11 20:47:22 +0000362 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes22b69db2011-03-04 18:54:14 +0000363 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000364 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes22b69db2011-03-04 18:54:14 +0000365 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000366 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
367 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000368 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
369 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanakadfb8cda2011-05-23 22:23:58 +0000370 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000371 setOperationAction(ISD::FLOG, MVT::f32, Expand);
372 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
373 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
374 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000375 setOperationAction(ISD::FMA, MVT::f32, Expand);
376 setOperationAction(ISD::FMA, MVT::f64, Expand);
Akira Hatanaka0603ad82012-03-29 18:43:11 +0000377 setOperationAction(ISD::FREM, MVT::f32, Expand);
378 setOperationAction(ISD::FREM, MVT::f64, Expand);
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000379
Pirama Arumuga Nainar34056de2015-04-20 20:15:36 +0000380 // Lower f16 conversion operations into library calls
381 setOperationAction(ISD::FP16_TO_FP, MVT::f32, Expand);
382 setOperationAction(ISD::FP_TO_FP16, MVT::f32, Expand);
383 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand);
384 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Expand);
385
Akira Hatanakac0b02062013-01-30 00:26:49 +0000386 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
387
Daniel Sanders2b553d42014-08-01 09:17:39 +0000388 setOperationAction(ISD::VASTART, MVT::Other, Custom);
389 setOperationAction(ISD::VAARG, MVT::Other, Custom);
Bruno Cardoso Lopes048ffab2011-03-09 19:22:22 +0000390 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
391 setOperationAction(ISD::VAEND, MVT::Other, Expand);
392
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000393 // Use the default for now
Owen Anderson9f944592009-08-11 20:47:22 +0000394 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
395 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman26a48482011-07-27 22:21:52 +0000396
Jia Liuf54f60f2012-02-28 07:46:26 +0000397 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
398 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
399 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
400 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
Eli Friedman7dfa7912011-08-29 18:23:02 +0000401
Eli Friedman30a49e92011-08-03 21:06:02 +0000402 setInsertFencesForAtomic(true);
403
Eric Christopher1c29a652014-07-18 22:55:25 +0000404 if (!Subtarget.hasMips32r2()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000405 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
406 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000407 }
408
Daniel Sanders070fd1c2014-05-12 12:41:59 +0000409 // MIPS16 lacks MIPS32's clz and clo instructions.
Eric Christopher1c29a652014-07-18 22:55:25 +0000410 if (!Subtarget.hasMips32() || Subtarget.inMips16Mode())
Owen Anderson9f944592009-08-11 20:47:22 +0000411 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Eric Christopher1c29a652014-07-18 22:55:25 +0000412 if (!Subtarget.hasMips64())
Akira Hatanaka1d8efab2011-12-21 00:20:27 +0000413 setOperationAction(ISD::CTLZ, MVT::i64, Expand);
Bruno Cardoso Lopes93da7e62008-08-08 06:16:31 +0000414
Eric Christopher1c29a652014-07-18 22:55:25 +0000415 if (!Subtarget.hasMips32r2())
Owen Anderson9f944592009-08-11 20:47:22 +0000416 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Eric Christopher1c29a652014-07-18 22:55:25 +0000417 if (!Subtarget.hasMips64r2())
Akira Hatanaka4706ac92011-12-20 23:56:43 +0000418 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000419
Eric Christopher1c29a652014-07-18 22:55:25 +0000420 if (Subtarget.isGP64bit()) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000421 setLoadExtAction(ISD::SEXTLOAD, MVT::i64, MVT::i32, Custom);
422 setLoadExtAction(ISD::ZEXTLOAD, MVT::i64, MVT::i32, Custom);
423 setLoadExtAction(ISD::EXTLOAD, MVT::i64, MVT::i32, Custom);
Akira Hatanaka019e5922012-06-02 00:04:42 +0000424 setTruncStoreAction(MVT::i64, MVT::i32, Custom);
425 }
426
Akira Hatanakaa3d9ab92013-07-26 20:58:55 +0000427 setOperationAction(ISD::TRAP, MVT::Other, Legal);
428
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000429 setTargetDAGCombine(ISD::SDIVREM);
430 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka5e152182012-03-08 03:26:37 +0000431 setTargetDAGCombine(ISD::SELECT);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000432 setTargetDAGCombine(ISD::AND);
433 setTargetDAGCombine(ISD::OR);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000434 setTargetDAGCombine(ISD::ADD);
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000435
Eric Christopher1c29a652014-07-18 22:55:25 +0000436 setMinFunctionAlignment(Subtarget.isGP64bit() ? 3 : 2);
Eli Friedman2518f832011-05-06 20:34:06 +0000437
Daniel Sanders2b553d42014-08-01 09:17:39 +0000438 // The arguments on the stack are defined in terms of 4-byte slots on O32
439 // and 8-byte slots on N32/N64.
Eric Christopher96e72c62015-01-29 23:27:36 +0000440 setMinStackArgumentAlignment((ABI.IsN32() || ABI.IsN64()) ? 8 : 4);
Daniel Sanders2b553d42014-08-01 09:17:39 +0000441
Eric Christopher96e72c62015-01-29 23:27:36 +0000442 setStackPointerRegisterToSaveRestore(ABI.IsN64() ? Mips::SP_64 : Mips::SP);
Akira Hatanakaaa560002011-05-26 18:59:03 +0000443
Eric Christopher96e72c62015-01-29 23:27:36 +0000444 setExceptionPointerRegister(ABI.IsN64() ? Mips::A0_64 : Mips::A0);
445 setExceptionSelectorRegister(ABI.IsN64() ? Mips::A1_64 : Mips::A1);
Akira Hatanaka1daf8c22012-06-13 19:33:32 +0000446
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000447 MaxStoresPerMemcpy = 16;
Zoran Jovanovicff9d5f32013-12-19 16:12:56 +0000448
Eric Christopher1c29a652014-07-18 22:55:25 +0000449 isMicroMips = Subtarget.inMicroMipsMode();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000450}
451
Eric Christopherb1526602014-09-19 23:30:42 +0000452const MipsTargetLowering *MipsTargetLowering::create(const MipsTargetMachine &TM,
Eric Christopher8924d272014-07-18 23:25:04 +0000453 const MipsSubtarget &STI) {
454 if (STI.inMips16Mode())
455 return llvm::createMips16TargetLowering(TM, STI);
Jia Liuf54f60f2012-02-28 07:46:26 +0000456
Eric Christopher8924d272014-07-18 23:25:04 +0000457 return llvm::createMipsSETargetLowering(TM, STI);
Akira Hatanaka2fcc1cf2011-08-12 21:30:06 +0000458}
459
Reed Kotler720c5ca2014-04-17 22:15:34 +0000460// Create a fast isel object.
461FastISel *
462MipsTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
463 const TargetLibraryInfo *libInfo) const {
464 if (!EnableMipsFastISel)
465 return TargetLowering::createFastISel(funcInfo, libInfo);
466 return Mips::createFastISel(funcInfo, libInfo);
467}
468
Matt Arsenault758659232013-05-18 00:21:46 +0000469EVT MipsTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Akira Hatanakab13b3332013-01-04 20:06:01 +0000470 if (!VT.isVector())
471 return MVT::i32;
472 return VT.changeVectorElementTypeToInteger();
Scott Michela6729e82008-03-10 15:42:14 +0000473}
474
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000475static SDValue performDivRemCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000476 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000477 const MipsSubtarget &Subtarget) {
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000478 if (DCI.isBeforeLegalizeOps())
479 return SDValue();
480
Akira Hatanakab1538f92011-10-03 21:06:13 +0000481 EVT Ty = N->getValueType(0);
Akira Hatanaka8002a3f2013-08-14 00:47:08 +0000482 unsigned LO = (Ty == MVT::i32) ? Mips::LO0 : Mips::LO0_64;
483 unsigned HI = (Ty == MVT::i32) ? Mips::HI0 : Mips::HI0_64;
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000484 unsigned Opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem16 :
485 MipsISD::DivRemU16;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000486 SDLoc DL(N);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000487
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000488 SDValue DivRem = DAG.getNode(Opc, DL, MVT::Glue,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000489 N->getOperand(0), N->getOperand(1));
490 SDValue InChain = DAG.getEntryNode();
491 SDValue InGlue = DivRem;
492
493 // insert MFLO
494 if (N->hasAnyUseOfValue(0)) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000495 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, DL, LO, Ty,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000496 InGlue);
497 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
498 InChain = CopyFromLo.getValue(1);
499 InGlue = CopyFromLo.getValue(2);
500 }
501
502 // insert MFHI
503 if (N->hasAnyUseOfValue(1)) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000504 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, DL,
Akira Hatanakab1538f92011-10-03 21:06:13 +0000505 HI, Ty, InGlue);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000506 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
507 }
508
509 return SDValue();
510}
511
Akira Hatanaka89af5892013-04-18 01:00:46 +0000512static Mips::CondCode condCodeToFCC(ISD::CondCode CC) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000513 switch (CC) {
514 default: llvm_unreachable("Unknown fp condition code!");
515 case ISD::SETEQ:
516 case ISD::SETOEQ: return Mips::FCOND_OEQ;
517 case ISD::SETUNE: return Mips::FCOND_UNE;
518 case ISD::SETLT:
519 case ISD::SETOLT: return Mips::FCOND_OLT;
520 case ISD::SETGT:
521 case ISD::SETOGT: return Mips::FCOND_OGT;
522 case ISD::SETLE:
523 case ISD::SETOLE: return Mips::FCOND_OLE;
524 case ISD::SETGE:
525 case ISD::SETOGE: return Mips::FCOND_OGE;
526 case ISD::SETULT: return Mips::FCOND_ULT;
527 case ISD::SETULE: return Mips::FCOND_ULE;
528 case ISD::SETUGT: return Mips::FCOND_UGT;
529 case ISD::SETUGE: return Mips::FCOND_UGE;
530 case ISD::SETUO: return Mips::FCOND_UN;
531 case ISD::SETO: return Mips::FCOND_OR;
532 case ISD::SETNE:
533 case ISD::SETONE: return Mips::FCOND_ONE;
534 case ISD::SETUEQ: return Mips::FCOND_UEQ;
535 }
536}
537
538
Akira Hatanakaf0ea5002013-03-30 01:16:38 +0000539/// This function returns true if the floating point conditional branches and
540/// conditional moves which use condition code CC should be inverted.
541static bool invertFPCondCodeUser(Mips::CondCode CC) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000542 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
543 return false;
544
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000545 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
546 "Illegal Condition Code");
Akira Hatanakaa5352702011-03-31 18:26:17 +0000547
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000548 return true;
Akira Hatanakaa5352702011-03-31 18:26:17 +0000549}
550
551// Creates and returns an FPCmp node from a setcc node.
552// Returns Op if setcc is not a floating point comparison.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000553static SDValue createFPCmp(SelectionDAG &DAG, const SDValue &Op) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000554 // must be a SETCC node
555 if (Op.getOpcode() != ISD::SETCC)
556 return Op;
557
558 SDValue LHS = Op.getOperand(0);
559
560 if (!LHS.getValueType().isFloatingPoint())
561 return Op;
562
563 SDValue RHS = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000564 SDLoc DL(Op);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000565
Akira Hatanakaaef55c82011-04-15 21:00:26 +0000566 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
567 // node if necessary.
Akira Hatanakaa5352702011-03-31 18:26:17 +0000568 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
569
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000570 return DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000571 DAG.getConstant(condCodeToFCC(CC), DL, MVT::i32));
Akira Hatanakaa5352702011-03-31 18:26:17 +0000572}
573
574// Creates and returns a CMovFPT/F node.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000575static SDValue createCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000576 SDValue False, SDLoc DL) {
Akira Hatanakaf0ea5002013-03-30 01:16:38 +0000577 ConstantSDNode *CC = cast<ConstantSDNode>(Cond.getOperand(2));
578 bool invert = invertFPCondCodeUser((Mips::CondCode)CC->getSExtValue());
Akira Hatanaka8bce21c2013-07-26 20:51:20 +0000579 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000580
581 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
Akira Hatanaka8bce21c2013-07-26 20:51:20 +0000582 True.getValueType(), True, FCC0, False, Cond);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000583}
584
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000585static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000586 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000587 const MipsSubtarget &Subtarget) {
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000588 if (DCI.isBeforeLegalizeOps())
589 return SDValue();
590
591 SDValue SetCC = N->getOperand(0);
592
593 if ((SetCC.getOpcode() != ISD::SETCC) ||
594 !SetCC.getOperand(0).getValueType().isInteger())
595 return SDValue();
596
597 SDValue False = N->getOperand(2);
598 EVT FalseTy = False.getValueType();
599
600 if (!FalseTy.isInteger())
601 return SDValue();
602
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000603 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(False);
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000604
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000605 // If the RHS (False) is 0, we swap the order of the operands
606 // of ISD::SELECT (obviously also inverting the condition) so that we can
607 // take advantage of conditional moves using the $0 register.
608 // Example:
609 // return (a != 0) ? x : 0;
610 // load $reg, x
611 // movz $reg, $0, a
612 if (!FalseC)
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000613 return SDValue();
614
Andrew Trickef9de2a2013-05-25 02:42:55 +0000615 const SDLoc DL(N);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000616
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000617 if (!FalseC->getZExtValue()) {
618 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
619 SDValue True = N->getOperand(1);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000620
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000621 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
622 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
623
624 return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True);
625 }
626
Matheus Almeidaa6beac12013-12-05 12:07:05 +0000627 // If both operands are integer constants there's a possibility that we
628 // can do some interesting optimizations.
629 SDValue True = N->getOperand(1);
630 ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(True);
631
632 if (!TrueC || !True.getValueType().isInteger())
633 return SDValue();
634
635 // We'll also ignore MVT::i64 operands as this optimizations proves
636 // to be ineffective because of the required sign extensions as the result
637 // of a SETCC operator is always MVT::i32 for non-vector types.
638 if (True.getValueType() == MVT::i64)
639 return SDValue();
640
641 int64_t Diff = TrueC->getSExtValue() - FalseC->getSExtValue();
642
643 // 1) (a < x) ? y : y-1
644 // slti $reg1, a, x
645 // addiu $reg2, $reg1, y-1
646 if (Diff == 1)
647 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, False);
648
649 // 2) (a < x) ? y-1 : y
650 // slti $reg1, a, x
651 // xor $reg1, $reg1, 1
652 // addiu $reg2, $reg1, y-1
653 if (Diff == -1) {
654 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
655 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
656 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
657 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, True);
658 }
659
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000660 // Couldn't optimize.
661 return SDValue();
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000662}
663
Vasileios Kalintirise741eb22015-03-02 12:47:32 +0000664static SDValue performCMovFPCombine(SDNode *N, SelectionDAG &DAG,
665 TargetLowering::DAGCombinerInfo &DCI,
666 const MipsSubtarget &Subtarget) {
667 if (DCI.isBeforeLegalizeOps())
668 return SDValue();
669
670 SDValue ValueIfTrue = N->getOperand(0), ValueIfFalse = N->getOperand(2);
671
672 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(ValueIfFalse);
673 if (!FalseC || FalseC->getZExtValue())
674 return SDValue();
675
676 // Since RHS (False) is 0, we swap the order of the True/False operands
677 // (obviously also inverting the condition) so that we can
678 // take advantage of conditional moves using the $0 register.
679 // Example:
680 // return (a != 0) ? x : 0;
681 // load $reg, x
682 // movz $reg, $0, a
683 unsigned Opc = (N->getOpcode() == MipsISD::CMovFP_T) ? MipsISD::CMovFP_F :
684 MipsISD::CMovFP_T;
685
686 SDValue FCC = N->getOperand(1), Glue = N->getOperand(3);
Vasileios Kalintiris2ef28882015-03-04 12:10:18 +0000687 return DAG.getNode(Opc, SDLoc(N), ValueIfFalse.getValueType(),
688 ValueIfFalse, FCC, ValueIfTrue, Glue);
Vasileios Kalintirise741eb22015-03-02 12:47:32 +0000689}
690
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000691static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000692 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000693 const MipsSubtarget &Subtarget) {
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000694 // Pattern match EXT.
695 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
696 // => ext $dst, $src, size, pos
Eric Christopher1c29a652014-07-18 22:55:25 +0000697 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasExtractInsert())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000698 return SDValue();
699
700 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000701 unsigned ShiftRightOpc = ShiftRight.getOpcode();
702
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000703 // Op's first operand must be a shift right.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000704 if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL)
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000705 return SDValue();
706
707 // The second operand of the shift must be an immediate.
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000708 ConstantSDNode *CN;
709 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
710 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000711
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000712 uint64_t Pos = CN->getZExtValue();
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000713 uint64_t SMPos, SMSize;
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000714
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000715 // Op's second operand must be a shifted mask.
716 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000717 !isShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000718 return SDValue();
719
720 // Return if the shifted mask does not start at bit 0 or the sum of its size
721 // and Pos exceeds the word's size.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000722 EVT ValTy = N->getValueType(0);
723 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000724 return SDValue();
725
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000726 SDLoc DL(N);
727 return DAG.getNode(MipsISD::Ext, DL, ValTy,
728 ShiftRight.getOperand(0),
729 DAG.getConstant(Pos, DL, MVT::i32),
730 DAG.getConstant(SMSize, DL, MVT::i32));
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000731}
Jia Liuf54f60f2012-02-28 07:46:26 +0000732
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000733static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000734 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000735 const MipsSubtarget &Subtarget) {
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000736 // Pattern match INS.
737 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
Jia Liuf54f60f2012-02-28 07:46:26 +0000738 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000739 // => ins $dst, $src, size, pos, $src1
Eric Christopher1c29a652014-07-18 22:55:25 +0000740 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasExtractInsert())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000741 return SDValue();
742
743 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
744 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
745 ConstantSDNode *CN;
746
747 // See if Op's first operand matches (and $src1 , mask0).
748 if (And0.getOpcode() != ISD::AND)
749 return SDValue();
750
751 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000752 !isShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000753 return SDValue();
754
755 // See if Op's second operand matches (and (shl $src, pos), mask1).
756 if (And1.getOpcode() != ISD::AND)
757 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000758
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000759 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000760 !isShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000761 return SDValue();
762
763 // The shift masks must have the same position and size.
764 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
765 return SDValue();
766
767 SDValue Shl = And1.getOperand(0);
768 if (Shl.getOpcode() != ISD::SHL)
769 return SDValue();
770
771 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
772 return SDValue();
773
774 unsigned Shamt = CN->getZExtValue();
775
776 // Return if the shift amount and the first bit position of mask are not the
Jia Liuf54f60f2012-02-28 07:46:26 +0000777 // same.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000778 EVT ValTy = N->getValueType(0);
779 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000780 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000781
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000782 SDLoc DL(N);
783 return DAG.getNode(MipsISD::Ins, DL, ValTy, Shl.getOperand(0),
784 DAG.getConstant(SMPos0, DL, MVT::i32),
785 DAG.getConstant(SMSize0, DL, MVT::i32),
786 And0.getOperand(0));
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000787}
Jia Liuf54f60f2012-02-28 07:46:26 +0000788
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000789static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000790 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000791 const MipsSubtarget &Subtarget) {
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000792 // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt))
793
794 if (DCI.isBeforeLegalizeOps())
795 return SDValue();
796
797 SDValue Add = N->getOperand(1);
798
799 if (Add.getOpcode() != ISD::ADD)
800 return SDValue();
801
802 SDValue Lo = Add.getOperand(1);
803
804 if ((Lo.getOpcode() != MipsISD::Lo) ||
805 (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable))
806 return SDValue();
807
808 EVT ValTy = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000809 SDLoc DL(N);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000810
811 SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, N->getOperand(0),
812 Add.getOperand(0));
813 return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo);
814}
815
Bruno Cardoso Lopes61a61e92011-02-10 18:05:10 +0000816SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000817 const {
818 SelectionDAG &DAG = DCI.DAG;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000819 unsigned Opc = N->getOpcode();
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000820
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000821 switch (Opc) {
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000822 default: break;
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000823 case ISD::SDIVREM:
824 case ISD::UDIVREM:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000825 return performDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000826 case ISD::SELECT:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000827 return performSELECTCombine(N, DAG, DCI, Subtarget);
Vasileios Kalintirise741eb22015-03-02 12:47:32 +0000828 case MipsISD::CMovFP_F:
829 case MipsISD::CMovFP_T:
830 return performCMovFPCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000831 case ISD::AND:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000832 return performANDCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000833 case ISD::OR:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000834 return performORCombine(N, DAG, DCI, Subtarget);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000835 case ISD::ADD:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000836 return performADDCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000837 }
838
839 return SDValue();
840}
841
Akira Hatanakafabb8cf2012-09-21 23:58:31 +0000842void
843MipsTargetLowering::LowerOperationWrapper(SDNode *N,
844 SmallVectorImpl<SDValue> &Results,
845 SelectionDAG &DAG) const {
846 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
847
848 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
849 Results.push_back(Res.getValue(I));
850}
851
852void
853MipsTargetLowering::ReplaceNodeResults(SDNode *N,
854 SmallVectorImpl<SDValue> &Results,
855 SelectionDAG &DAG) const {
Akira Hatanaka9da442f2013-04-30 21:17:07 +0000856 return LowerOperationWrapper(N, Results, DAG);
Akira Hatanakafabb8cf2012-09-21 23:58:31 +0000857}
858
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000859SDValue MipsTargetLowering::
Dan Gohman21cea8a2010-04-17 15:26:15 +0000860LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000861{
Wesley Peck527da1b2010-11-23 03:31:01 +0000862 switch (Op.getOpcode())
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000863 {
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000864 case ISD::BR_JT: return lowerBR_JT(Op, DAG);
865 case ISD::BRCOND: return lowerBRCOND(Op, DAG);
866 case ISD::ConstantPool: return lowerConstantPool(Op, DAG);
867 case ISD::GlobalAddress: return lowerGlobalAddress(Op, DAG);
868 case ISD::BlockAddress: return lowerBlockAddress(Op, DAG);
869 case ISD::GlobalTLSAddress: return lowerGlobalTLSAddress(Op, DAG);
870 case ISD::JumpTable: return lowerJumpTable(Op, DAG);
871 case ISD::SELECT: return lowerSELECT(Op, DAG);
872 case ISD::SELECT_CC: return lowerSELECT_CC(Op, DAG);
873 case ISD::SETCC: return lowerSETCC(Op, DAG);
874 case ISD::VASTART: return lowerVASTART(Op, DAG);
Daniel Sanders2b553d42014-08-01 09:17:39 +0000875 case ISD::VAARG: return lowerVAARG(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000876 case ISD::FCOPYSIGN: return lowerFCOPYSIGN(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000877 case ISD::FRAMEADDR: return lowerFRAMEADDR(Op, DAG);
878 case ISD::RETURNADDR: return lowerRETURNADDR(Op, DAG);
879 case ISD::EH_RETURN: return lowerEH_RETURN(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000880 case ISD::ATOMIC_FENCE: return lowerATOMIC_FENCE(Op, DAG);
881 case ISD::SHL_PARTS: return lowerShiftLeftParts(Op, DAG);
882 case ISD::SRA_PARTS: return lowerShiftRightParts(Op, DAG, true);
883 case ISD::SRL_PARTS: return lowerShiftRightParts(Op, DAG, false);
884 case ISD::LOAD: return lowerLOAD(Op, DAG);
885 case ISD::STORE: return lowerSTORE(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000886 case ISD::ADD: return lowerADD(Op, DAG);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000887 case ISD::FP_TO_SINT: return lowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000888 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000889 return SDValue();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000890}
891
Akira Hatanakae2489122011-04-15 21:51:11 +0000892//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000893// Lower helper functions
Akira Hatanakae2489122011-04-15 21:51:11 +0000894//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000895
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000896// addLiveIn - This helper function adds the specified physical register to the
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000897// MachineFunction as a live in value. It also creates a corresponding
898// virtual register for it.
899static unsigned
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000900addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000901{
Chris Lattnera10fff52007-12-31 04:13:23 +0000902 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
903 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000904 return VReg;
905}
906
Daniel Sanders308181e2014-06-12 10:44:10 +0000907static MachineBasicBlock *insertDivByZeroTrap(MachineInstr *MI,
908 MachineBasicBlock &MBB,
909 const TargetInstrInfo &TII,
910 bool Is64Bit) {
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000911 if (NoZeroDivCheck)
912 return &MBB;
913
914 // Insert instruction "teq $divisor_reg, $zero, 7".
915 MachineBasicBlock::iterator I(MI);
916 MachineInstrBuilder MIB;
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000917 MachineOperand &Divisor = MI->getOperand(2);
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000918 MIB = BuildMI(MBB, std::next(I), MI->getDebugLoc(), TII.get(Mips::TEQ))
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000919 .addReg(Divisor.getReg(), getKillRegState(Divisor.isKill()))
920 .addReg(Mips::ZERO).addImm(7);
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000921
922 // Use the 32-bit sub-register if this is a 64-bit division.
923 if (Is64Bit)
924 MIB->getOperand(0).setSubReg(Mips::sub_32);
925
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000926 // Clear Divisor's kill flag.
927 Divisor.setIsKill(false);
Daniel Sanders308181e2014-06-12 10:44:10 +0000928
929 // We would normally delete the original instruction here but in this case
930 // we only needed to inject an additional instruction rather than replace it.
931
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000932 return &MBB;
933}
934
Akira Hatanakae4bd0542012-09-27 02:15:57 +0000935MachineBasicBlock *
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +0000936MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +0000937 MachineBasicBlock *BB) const {
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +0000938 switch (MI->getOpcode()) {
Reed Kotler97ba5f22013-02-21 04:22:38 +0000939 default:
940 llvm_unreachable("Unexpected instr type to insert");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000941 case Mips::ATOMIC_LOAD_ADD_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000942 return emitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000943 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000944 return emitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000945 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000946 return emitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000947 case Mips::ATOMIC_LOAD_ADD_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000948 return emitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000949
950 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000951 return emitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000952 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000953 return emitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000954 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000955 return emitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000956 case Mips::ATOMIC_LOAD_AND_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000957 return emitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000958
959 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000960 return emitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000961 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000962 return emitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000963 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000964 return emitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000965 case Mips::ATOMIC_LOAD_OR_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000966 return emitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000967
968 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000969 return emitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000970 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000971 return emitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000972 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000973 return emitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000974 case Mips::ATOMIC_LOAD_XOR_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000975 return emitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000976
977 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000978 return emitAtomicBinaryPartword(MI, BB, 1, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000979 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000980 return emitAtomicBinaryPartword(MI, BB, 2, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000981 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000982 return emitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000983 case Mips::ATOMIC_LOAD_NAND_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000984 return emitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000985
986 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000987 return emitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000988 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000989 return emitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000990 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000991 return emitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000992 case Mips::ATOMIC_LOAD_SUB_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000993 return emitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000994
995 case Mips::ATOMIC_SWAP_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000996 return emitAtomicBinaryPartword(MI, BB, 1, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000997 case Mips::ATOMIC_SWAP_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000998 return emitAtomicBinaryPartword(MI, BB, 2, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000999 case Mips::ATOMIC_SWAP_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001000 return emitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001001 case Mips::ATOMIC_SWAP_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001002 return emitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001003
1004 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001005 return emitAtomicCmpSwapPartword(MI, BB, 1);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001006 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001007 return emitAtomicCmpSwapPartword(MI, BB, 2);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001008 case Mips::ATOMIC_CMP_SWAP_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001009 return emitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001010 case Mips::ATOMIC_CMP_SWAP_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001011 return emitAtomicCmpSwap(MI, BB, 8);
Akira Hatanaka1cb02422013-05-20 18:07:43 +00001012 case Mips::PseudoSDIV:
1013 case Mips::PseudoUDIV:
Daniel Sanders308181e2014-06-12 10:44:10 +00001014 case Mips::DIV:
1015 case Mips::DIVU:
1016 case Mips::MOD:
1017 case Mips::MODU:
Eric Christopher96e72c62015-01-29 23:27:36 +00001018 return insertDivByZeroTrap(MI, *BB, *Subtarget.getInstrInfo(), false);
Akira Hatanaka1cb02422013-05-20 18:07:43 +00001019 case Mips::PseudoDSDIV:
1020 case Mips::PseudoDUDIV:
Daniel Sanders308181e2014-06-12 10:44:10 +00001021 case Mips::DDIV:
1022 case Mips::DDIVU:
1023 case Mips::DMOD:
1024 case Mips::DMODU:
Eric Christopher96e72c62015-01-29 23:27:36 +00001025 return insertDivByZeroTrap(MI, *BB, *Subtarget.getInstrInfo(), true);
Daniel Sanders0fa60412014-06-12 13:39:06 +00001026 case Mips::SEL_D:
1027 return emitSEL_D(MI, BB);
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001028
1029 case Mips::PseudoSELECT_I:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001030 case Mips::PseudoSELECT_I64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001031 case Mips::PseudoSELECT_S:
1032 case Mips::PseudoSELECT_D32:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001033 case Mips::PseudoSELECT_D64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001034 return emitPseudoSELECT(MI, BB, false, Mips::BNE);
1035 case Mips::PseudoSELECTFP_F_I:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001036 case Mips::PseudoSELECTFP_F_I64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001037 case Mips::PseudoSELECTFP_F_S:
1038 case Mips::PseudoSELECTFP_F_D32:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001039 case Mips::PseudoSELECTFP_F_D64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001040 return emitPseudoSELECT(MI, BB, true, Mips::BC1F);
1041 case Mips::PseudoSELECTFP_T_I:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001042 case Mips::PseudoSELECTFP_T_I64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001043 case Mips::PseudoSELECTFP_T_S:
1044 case Mips::PseudoSELECTFP_T_D32:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001045 case Mips::PseudoSELECTFP_T_D64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001046 return emitPseudoSELECT(MI, BB, true, Mips::BC1T);
Akira Hatanakaa5352702011-03-31 18:26:17 +00001047 }
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001048}
1049
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001050// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
1051// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
1052MachineBasicBlock *
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001053MipsTargetLowering::emitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher0713a9d2011-06-08 23:55:35 +00001054 unsigned Size, unsigned BinOpcode,
Akira Hatanaka15506782011-06-07 18:58:42 +00001055 bool Nand) const {
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001056 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001057
1058 MachineFunction *MF = BB->getParent();
1059 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001060 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Eric Christopher96e72c62015-01-29 23:27:36 +00001061 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001062 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001063 unsigned LL, SC, AND, NOR, ZERO, BEQ;
1064
1065 if (Size == 4) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001066 if (isMicroMips) {
1067 LL = Mips::LL_MM;
1068 SC = Mips::SC_MM;
1069 } else {
Daniel Sandersbdcfab12014-07-24 09:47:14 +00001070 LL = Subtarget.hasMips32r6() ? Mips::LL_R6 : Mips::LL;
1071 SC = Subtarget.hasMips32r6() ? Mips::SC_R6 : Mips::SC;
Daniel Sanders6a803f62014-06-16 13:13:03 +00001072 }
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001073 AND = Mips::AND;
1074 NOR = Mips::NOR;
1075 ZERO = Mips::ZERO;
1076 BEQ = Mips::BEQ;
Daniel Sanders6a803f62014-06-16 13:13:03 +00001077 } else {
Daniel Sandersbdcfab12014-07-24 09:47:14 +00001078 LL = Subtarget.hasMips64r6() ? Mips::LLD_R6 : Mips::LLD;
1079 SC = Subtarget.hasMips64r6() ? Mips::SCD_R6 : Mips::SCD;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001080 AND = Mips::AND64;
1081 NOR = Mips::NOR64;
1082 ZERO = Mips::ZERO_64;
1083 BEQ = Mips::BEQ64;
1084 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001085
Akira Hatanaka0e019592011-07-19 20:11:17 +00001086 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001087 unsigned Ptr = MI->getOperand(1).getReg();
1088 unsigned Incr = MI->getOperand(2).getReg();
1089
Akira Hatanaka0e019592011-07-19 20:11:17 +00001090 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1091 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1092 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001093
1094 // insert new blocks after the current block
1095 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1096 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1097 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1098 MachineFunction::iterator It = BB;
1099 ++It;
1100 MF->insert(It, loopMBB);
1101 MF->insert(It, exitMBB);
1102
1103 // Transfer the remainder of BB and its successor edges to exitMBB.
1104 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001105 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001106 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1107
1108 // thisMBB:
1109 // ...
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001110 // fallthrough --> loopMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001111 BB->addSuccessor(loopMBB);
Akira Hatanaka08636b42011-07-19 17:09:53 +00001112 loopMBB->addSuccessor(loopMBB);
1113 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001114
1115 // loopMBB:
1116 // ll oldval, 0(ptr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001117 // <binop> storeval, oldval, incr
1118 // sc success, storeval, 0(ptr)
1119 // beq success, $0, loopMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001120 BB = loopMBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001121 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001122 if (Nand) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001123 // and andres, oldval, incr
1124 // nor storeval, $0, andres
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001125 BuildMI(BB, DL, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
1126 BuildMI(BB, DL, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001127 } else if (BinOpcode) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001128 // <binop> storeval, oldval, incr
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001129 BuildMI(BB, DL, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001130 } else {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001131 StoreVal = Incr;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001132 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001133 BuildMI(BB, DL, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
1134 BuildMI(BB, DL, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001135
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001136 MI->eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001137
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001138 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001139}
1140
Daniel Sanders6a803f62014-06-16 13:13:03 +00001141MachineBasicBlock *MipsTargetLowering::emitSignExtendToI32InReg(
1142 MachineInstr *MI, MachineBasicBlock *BB, unsigned Size, unsigned DstReg,
1143 unsigned SrcReg) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00001144 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders6a803f62014-06-16 13:13:03 +00001145 DebugLoc DL = MI->getDebugLoc();
1146
Eric Christopher1c29a652014-07-18 22:55:25 +00001147 if (Subtarget.hasMips32r2() && Size == 1) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001148 BuildMI(BB, DL, TII->get(Mips::SEB), DstReg).addReg(SrcReg);
1149 return BB;
1150 }
1151
Eric Christopher1c29a652014-07-18 22:55:25 +00001152 if (Subtarget.hasMips32r2() && Size == 2) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001153 BuildMI(BB, DL, TII->get(Mips::SEH), DstReg).addReg(SrcReg);
1154 return BB;
1155 }
1156
1157 MachineFunction *MF = BB->getParent();
1158 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1159 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1160 unsigned ScrReg = RegInfo.createVirtualRegister(RC);
1161
1162 assert(Size < 32);
1163 int64_t ShiftImm = 32 - (Size * 8);
1164
1165 BuildMI(BB, DL, TII->get(Mips::SLL), ScrReg).addReg(SrcReg).addImm(ShiftImm);
1166 BuildMI(BB, DL, TII->get(Mips::SRA), DstReg).addReg(ScrReg).addImm(ShiftImm);
1167
1168 return BB;
1169}
1170
1171MachineBasicBlock *MipsTargetLowering::emitAtomicBinaryPartword(
1172 MachineInstr *MI, MachineBasicBlock *BB, unsigned Size, unsigned BinOpcode,
1173 bool Nand) const {
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001174 assert((Size == 1 || Size == 2) &&
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001175 "Unsupported size for EmitAtomicBinaryPartial.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001176
1177 MachineFunction *MF = BB->getParent();
1178 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1179 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
Eric Christopher96e72c62015-01-29 23:27:36 +00001180 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001181 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001182
1183 unsigned Dest = MI->getOperand(0).getReg();
1184 unsigned Ptr = MI->getOperand(1).getReg();
1185 unsigned Incr = MI->getOperand(2).getReg();
1186
Akira Hatanaka0e019592011-07-19 20:11:17 +00001187 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1188 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001189 unsigned Mask = RegInfo.createVirtualRegister(RC);
1190 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001191 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1192 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001193 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001194 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1195 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1196 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1197 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1198 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka9663dd32011-07-19 20:56:53 +00001199 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001200 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1201 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1202 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001203 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001204
1205 // insert new blocks after the current block
1206 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1207 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001208 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001209 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1210 MachineFunction::iterator It = BB;
1211 ++It;
1212 MF->insert(It, loopMBB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001213 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001214 MF->insert(It, exitMBB);
1215
1216 // Transfer the remainder of BB and its successor edges to exitMBB.
1217 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001218 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001219 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1220
Akira Hatanaka08636b42011-07-19 17:09:53 +00001221 BB->addSuccessor(loopMBB);
1222 loopMBB->addSuccessor(loopMBB);
1223 loopMBB->addSuccessor(sinkMBB);
1224 sinkMBB->addSuccessor(exitMBB);
1225
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001226 // thisMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001227 // addiu masklsb2,$0,-4 # 0xfffffffc
1228 // and alignedaddr,ptr,masklsb2
1229 // andi ptrlsb2,ptr,3
1230 // sll shiftamt,ptrlsb2,3
1231 // ori maskupper,$0,255 # 0xff
1232 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001233 // nor mask2,$0,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001234 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001235
1236 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001237 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001238 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001239 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001240 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001241 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Eric Christopher1c29a652014-07-18 22:55:25 +00001242 if (Subtarget.isLittle()) {
Akira Hatanaka2bf97332013-05-31 03:25:44 +00001243 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1244 } else {
1245 unsigned Off = RegInfo.createVirtualRegister(RC);
1246 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1247 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1248 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1249 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001250 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001251 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001252 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001253 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001254 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001255 BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(Incr).addReg(ShiftAmt);
Bruno Cardoso Lopesf771a0f2011-05-31 20:25:26 +00001256
Akira Hatanaka27292632011-07-18 18:52:12 +00001257 // atomic.load.binop
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001258 // loopMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001259 // ll oldval,0(alignedaddr)
1260 // binop binopres,oldval,incr2
1261 // and newval,binopres,mask
1262 // and maskedoldval0,oldval,mask2
1263 // or storeval,maskedoldval0,newval
1264 // sc success,storeval,0(alignedaddr)
1265 // beq success,$0,loopMBB
1266
Akira Hatanaka27292632011-07-18 18:52:12 +00001267 // atomic.swap
1268 // loopMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001269 // ll oldval,0(alignedaddr)
Akira Hatanakae4503582011-07-19 18:14:26 +00001270 // and newval,incr2,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001271 // and maskedoldval0,oldval,mask2
1272 // or storeval,maskedoldval0,newval
1273 // sc success,storeval,0(alignedaddr)
1274 // beq success,$0,loopMBB
Akira Hatanaka27292632011-07-18 18:52:12 +00001275
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001276 BB = loopMBB;
Jozef Kolek2f27d572014-12-18 16:39:29 +00001277 unsigned LL = isMicroMips ? Mips::LL_MM : Mips::LL;
1278 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001279 if (Nand) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001280 // and andres, oldval, incr2
1281 // nor binopres, $0, andres
1282 // and newval, binopres, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001283 BuildMI(BB, DL, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1284 BuildMI(BB, DL, TII->get(Mips::NOR), BinOpRes)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001285 .addReg(Mips::ZERO).addReg(AndRes);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001286 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001287 } else if (BinOpcode) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001288 // <binop> binopres, oldval, incr2
1289 // and newval, binopres, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001290 BuildMI(BB, DL, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1291 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001292 } else { // atomic.swap
Akira Hatanaka0e019592011-07-19 20:11:17 +00001293 // and newval, incr2, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001294 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanakae4503582011-07-19 18:14:26 +00001295 }
Jia Liuf54f60f2012-02-28 07:46:26 +00001296
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001297 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001298 .addReg(OldVal).addReg(Mask2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001299 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka9663dd32011-07-19 20:56:53 +00001300 .addReg(MaskedOldVal0).addReg(NewVal);
Jozef Kolek2f27d572014-12-18 16:39:29 +00001301 unsigned SC = isMicroMips ? Mips::SC_MM : Mips::SC;
1302 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001303 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001304 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001305 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001306
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001307 // sinkMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001308 // and maskedoldval1,oldval,mask
1309 // srl srlres,maskedoldval1,shiftamt
Daniel Sanders6a803f62014-06-16 13:13:03 +00001310 // sign_extend dest,srlres
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001311 BB = sinkMBB;
Akira Hatanakae97bd812011-07-19 03:14:58 +00001312
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001313 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001314 .addReg(OldVal).addReg(Mask);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001315 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001316 .addReg(MaskedOldVal1).addReg(ShiftAmt);
Daniel Sanders6a803f62014-06-16 13:13:03 +00001317 BB = emitSignExtendToI32InReg(MI, BB, Size, Dest, SrlRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001318
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001319 MI->eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001320
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001321 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001322}
1323
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001324MachineBasicBlock * MipsTargetLowering::emitAtomicCmpSwap(MachineInstr *MI,
1325 MachineBasicBlock *BB,
1326 unsigned Size) const {
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001327 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001328
1329 MachineFunction *MF = BB->getParent();
1330 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001331 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Eric Christopher96e72c62015-01-29 23:27:36 +00001332 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001333 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001334 unsigned LL, SC, ZERO, BNE, BEQ;
1335
1336 if (Size == 4) {
Zoran Jovanovicff9d5f32013-12-19 16:12:56 +00001337 LL = isMicroMips ? Mips::LL_MM : Mips::LL;
1338 SC = isMicroMips ? Mips::SC_MM : Mips::SC;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001339 ZERO = Mips::ZERO;
1340 BNE = Mips::BNE;
1341 BEQ = Mips::BEQ;
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001342 } else {
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001343 LL = Mips::LLD;
1344 SC = Mips::SCD;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001345 ZERO = Mips::ZERO_64;
1346 BNE = Mips::BNE64;
1347 BEQ = Mips::BEQ64;
1348 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001349
1350 unsigned Dest = MI->getOperand(0).getReg();
1351 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka0e019592011-07-19 20:11:17 +00001352 unsigned OldVal = MI->getOperand(2).getReg();
1353 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001354
Akira Hatanaka0e019592011-07-19 20:11:17 +00001355 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001356
1357 // insert new blocks after the current block
1358 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1359 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1360 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1361 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1362 MachineFunction::iterator It = BB;
1363 ++It;
1364 MF->insert(It, loop1MBB);
1365 MF->insert(It, loop2MBB);
1366 MF->insert(It, exitMBB);
1367
1368 // Transfer the remainder of BB and its successor edges to exitMBB.
1369 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001370 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001371 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1372
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001373 // thisMBB:
1374 // ...
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001375 // fallthrough --> loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001376 BB->addSuccessor(loop1MBB);
Akira Hatanaka08636b42011-07-19 17:09:53 +00001377 loop1MBB->addSuccessor(exitMBB);
1378 loop1MBB->addSuccessor(loop2MBB);
1379 loop2MBB->addSuccessor(loop1MBB);
1380 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001381
1382 // loop1MBB:
1383 // ll dest, 0(ptr)
1384 // bne dest, oldval, exitMBB
1385 BB = loop1MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001386 BuildMI(BB, DL, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1387 BuildMI(BB, DL, TII->get(BNE))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001388 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001389
1390 // loop2MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001391 // sc success, newval, 0(ptr)
1392 // beq success, $0, loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001393 BB = loop2MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001394 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001395 .addReg(NewVal).addReg(Ptr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001396 BuildMI(BB, DL, TII->get(BEQ))
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001397 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001398
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001399 MI->eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001400
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001401 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001402}
1403
1404MachineBasicBlock *
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001405MipsTargetLowering::emitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka15506782011-06-07 18:58:42 +00001406 MachineBasicBlock *BB,
1407 unsigned Size) const {
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001408 assert((Size == 1 || Size == 2) &&
1409 "Unsupported size for EmitAtomicCmpSwapPartial.");
1410
1411 MachineFunction *MF = BB->getParent();
1412 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1413 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
Eric Christopher96e72c62015-01-29 23:27:36 +00001414 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001415 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001416
1417 unsigned Dest = MI->getOperand(0).getReg();
1418 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka0e019592011-07-19 20:11:17 +00001419 unsigned CmpVal = MI->getOperand(2).getReg();
1420 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001421
Akira Hatanaka0e019592011-07-19 20:11:17 +00001422 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1423 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001424 unsigned Mask = RegInfo.createVirtualRegister(RC);
1425 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001426 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1427 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1428 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1429 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1430 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1431 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1432 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1433 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1434 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1435 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1436 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1437 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001438 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001439
1440 // insert new blocks after the current block
1441 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1442 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1443 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001444 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001445 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1446 MachineFunction::iterator It = BB;
1447 ++It;
1448 MF->insert(It, loop1MBB);
1449 MF->insert(It, loop2MBB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001450 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001451 MF->insert(It, exitMBB);
1452
1453 // Transfer the remainder of BB and its successor edges to exitMBB.
1454 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001455 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001456 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1457
Akira Hatanaka08636b42011-07-19 17:09:53 +00001458 BB->addSuccessor(loop1MBB);
1459 loop1MBB->addSuccessor(sinkMBB);
1460 loop1MBB->addSuccessor(loop2MBB);
1461 loop2MBB->addSuccessor(loop1MBB);
1462 loop2MBB->addSuccessor(sinkMBB);
1463 sinkMBB->addSuccessor(exitMBB);
1464
Akira Hatanakae4503582011-07-19 18:14:26 +00001465 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001466 // thisMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001467 // addiu masklsb2,$0,-4 # 0xfffffffc
1468 // and alignedaddr,ptr,masklsb2
1469 // andi ptrlsb2,ptr,3
1470 // sll shiftamt,ptrlsb2,3
1471 // ori maskupper,$0,255 # 0xff
1472 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001473 // nor mask2,$0,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001474 // andi maskedcmpval,cmpval,255
1475 // sll shiftedcmpval,maskedcmpval,shiftamt
1476 // andi maskednewval,newval,255
1477 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001478 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001479 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001480 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001481 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001482 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001483 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Eric Christopher1c29a652014-07-18 22:55:25 +00001484 if (Subtarget.isLittle()) {
Akira Hatanaka2bf97332013-05-31 03:25:44 +00001485 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1486 } else {
1487 unsigned Off = RegInfo.createVirtualRegister(RC);
1488 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1489 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1490 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1491 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001492 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001493 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001494 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001495 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001496 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1497 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001498 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001499 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001500 .addReg(MaskedCmpVal).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001501 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001502 .addReg(NewVal).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001503 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001504 .addReg(MaskedNewVal).addReg(ShiftAmt);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001505
1506 // loop1MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001507 // ll oldval,0(alginedaddr)
1508 // and maskedoldval0,oldval,mask
1509 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001510 BB = loop1MBB;
Jozef Kolek2f27d572014-12-18 16:39:29 +00001511 unsigned LL = isMicroMips ? Mips::LL_MM : Mips::LL;
1512 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001513 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001514 .addReg(OldVal).addReg(Mask);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001515 BuildMI(BB, DL, TII->get(Mips::BNE))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001516 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001517
1518 // loop2MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001519 // and maskedoldval1,oldval,mask2
1520 // or storeval,maskedoldval1,shiftednewval
1521 // sc success,storeval,0(alignedaddr)
1522 // beq success,$0,loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001523 BB = loop2MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001524 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001525 .addReg(OldVal).addReg(Mask2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001526 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001527 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
Jozef Kolek2f27d572014-12-18 16:39:29 +00001528 unsigned SC = isMicroMips ? Mips::SC_MM : Mips::SC;
1529 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001530 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001531 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001532 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001533
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001534 // sinkMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001535 // srl srlres,maskedoldval0,shiftamt
Daniel Sanders6a803f62014-06-16 13:13:03 +00001536 // sign_extend dest,srlres
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001537 BB = sinkMBB;
Akira Hatanakae97bd812011-07-19 03:14:58 +00001538
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001539 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001540 .addReg(MaskedOldVal0).addReg(ShiftAmt);
Daniel Sanders6a803f62014-06-16 13:13:03 +00001541 BB = emitSignExtendToI32InReg(MI, BB, Size, Dest, SrlRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001542
1543 MI->eraseFromParent(); // The instruction is gone now.
1544
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001545 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001546}
1547
Daniel Sanders0fa60412014-06-12 13:39:06 +00001548MachineBasicBlock *MipsTargetLowering::emitSEL_D(MachineInstr *MI,
1549 MachineBasicBlock *BB) const {
1550 MachineFunction *MF = BB->getParent();
Eric Christopher96e72c62015-01-29 23:27:36 +00001551 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
1552 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders0fa60412014-06-12 13:39:06 +00001553 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1554 DebugLoc DL = MI->getDebugLoc();
1555 MachineBasicBlock::iterator II(MI);
1556
1557 unsigned Fc = MI->getOperand(1).getReg();
1558 const auto &FGR64RegClass = TRI->getRegClass(Mips::FGR64RegClassID);
1559
1560 unsigned Fc2 = RegInfo.createVirtualRegister(FGR64RegClass);
1561
1562 BuildMI(*BB, II, DL, TII->get(Mips::SUBREG_TO_REG), Fc2)
1563 .addImm(0)
1564 .addReg(Fc)
1565 .addImm(Mips::sub_lo);
1566
1567 // We don't erase the original instruction, we just replace the condition
1568 // register with the 64-bit super-register.
1569 MI->getOperand(1).setReg(Fc2);
1570
1571 return BB;
1572}
1573
Akira Hatanakae2489122011-04-15 21:51:11 +00001574//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001575// Misc Lower Operation implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00001576//===----------------------------------------------------------------------===//
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001577SDValue MipsTargetLowering::lowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001578 SDValue Chain = Op.getOperand(0);
1579 SDValue Table = Op.getOperand(1);
1580 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001581 SDLoc DL(Op);
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001582 EVT PTy = getPointerTy();
1583 unsigned EntrySize =
1584 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(*getDataLayout());
1585
1586 Index = DAG.getNode(ISD::MUL, DL, PTy, Index,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001587 DAG.getConstant(EntrySize, DL, PTy));
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001588 SDValue Addr = DAG.getNode(ISD::ADD, DL, PTy, Index, Table);
1589
1590 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
1591 Addr = DAG.getExtLoad(ISD::SEXTLOAD, DL, PTy, Chain, Addr,
1592 MachinePointerInfo::getJumpTable(), MemVT, false, false,
Louis Gerbarg67474e32014-07-31 21:45:05 +00001593 false, 0);
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001594 Chain = Addr.getValue(1);
1595
Eric Christopher96e72c62015-01-29 23:27:36 +00001596 if ((getTargetMachine().getRelocationModel() == Reloc::PIC_) || ABI.IsN64()) {
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001597 // For PIC, the sequence is:
1598 // BRIND(load(Jumptable + index) + RelocBase)
1599 // RelocBase can be JumpTable, GOT or some sort of global base.
1600 Addr = DAG.getNode(ISD::ADD, DL, PTy, Addr,
1601 getPICJumpTableRelocBase(Table, DAG));
1602 }
1603
1604 return DAG.getNode(ISD::BRIND, DL, MVT::Other, Chain, Addr);
1605}
1606
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001607SDValue MipsTargetLowering::lowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Wesley Peck527da1b2010-11-23 03:31:01 +00001608 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001609 // the block to branch to if the condition is true.
1610 SDValue Chain = Op.getOperand(0);
1611 SDValue Dest = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001612 SDLoc DL(Op);
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001613
Eric Christopher1c29a652014-07-18 22:55:25 +00001614 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001615 SDValue CondRes = createFPCmp(DAG, Op.getOperand(1));
Akira Hatanakaa5352702011-03-31 18:26:17 +00001616
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001617 // Return if flag is not set by a floating point comparison.
Akira Hatanakaa5352702011-03-31 18:26:17 +00001618 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopesa9504222008-07-30 17:06:13 +00001619 return Op;
Wesley Peck527da1b2010-11-23 03:31:01 +00001620
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +00001621 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmaneffb8942008-09-12 16:56:44 +00001622 Mips::CondCode CC =
1623 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Akira Hatanakaf0ea5002013-03-30 01:16:38 +00001624 unsigned Opc = invertFPCondCodeUser(CC) ? Mips::BRANCH_F : Mips::BRANCH_T;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001625 SDValue BrCode = DAG.getConstant(Opc, DL, MVT::i32);
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +00001626 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001627 return DAG.getNode(MipsISD::FPBrcond, DL, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +00001628 FCC0, Dest, CondRes);
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001629}
1630
1631SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001632lowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001633{
Eric Christopher1c29a652014-07-18 22:55:25 +00001634 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001635 SDValue Cond = createFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001636
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001637 // Return if flag is not set by a floating point comparison.
Akira Hatanakaa5352702011-03-31 18:26:17 +00001638 if (Cond.getOpcode() != MipsISD::FPCmp)
1639 return Op;
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +00001640
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001641 return createCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
Andrew Trickef9de2a2013-05-25 02:42:55 +00001642 SDLoc(Op));
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001643}
1644
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001645SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001646lowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001647{
Andrew Trickef9de2a2013-05-25 02:42:55 +00001648 SDLoc DL(Op);
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001649 EVT Ty = Op.getOperand(0).getValueType();
Matt Arsenault758659232013-05-18 00:21:46 +00001650 SDValue Cond = DAG.getNode(ISD::SETCC, DL,
1651 getSetCCResultType(*DAG.getContext(), Ty),
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001652 Op.getOperand(0), Op.getOperand(1),
1653 Op.getOperand(4));
1654
1655 return DAG.getNode(ISD::SELECT, DL, Op.getValueType(), Cond, Op.getOperand(2),
1656 Op.getOperand(3));
1657}
1658
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001659SDValue MipsTargetLowering::lowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00001660 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001661 SDValue Cond = createFPCmp(DAG, Op);
Akira Hatanakab7f78592012-03-09 23:46:03 +00001662
1663 assert(Cond.getOpcode() == MipsISD::FPCmp &&
1664 "Floating point operand expected.");
1665
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001666 SDLoc DL(Op);
1667 SDValue True = DAG.getConstant(1, DL, MVT::i32);
1668 SDValue False = DAG.getConstant(0, DL, MVT::i32);
Akira Hatanakab7f78592012-03-09 23:46:03 +00001669
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001670 return createCMovFP(DAG, Cond, True, False, DL);
Akira Hatanakab7f78592012-03-09 23:46:03 +00001671}
1672
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001673SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001674 SelectionDAG &DAG) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001675 EVT Ty = Op.getValueType();
1676 GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
1677 const GlobalValue *GV = N->getGlobal();
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001678
Eric Christopher96e72c62015-01-29 23:27:36 +00001679 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !ABI.IsN64()) {
Eric Christopher36fe0282015-02-03 07:22:52 +00001680 const MipsTargetObjectFile *TLOF =
1681 static_cast<const MipsTargetObjectFile *>(
1682 getTargetMachine().getObjFileLowering());
1683 if (TLOF->IsGlobalInSmallSection(GV, getTargetMachine()))
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001684 // %gp_rel relocation
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001685 return getAddrGPRel(N, SDLoc(N), Ty, DAG);
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001686
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001687 // %hi/%lo relocation
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001688 return getAddrNonPIC(N, SDLoc(N), Ty, DAG);
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001689 }
1690
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001691 if (GV->hasInternalLinkage() || (GV->hasLocalLinkage() && !isa<Function>(GV)))
Eric Christopher96e72c62015-01-29 23:27:36 +00001692 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001693
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00001694 if (LargeGOT)
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001695 return getAddrGlobalLargeGOT(N, SDLoc(N), Ty, DAG, MipsII::MO_GOT_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00001696 MipsII::MO_GOT_LO16, DAG.getEntryNode(),
1697 MachinePointerInfo::getGOT());
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00001698
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001699 return getAddrGlobal(N, SDLoc(N), Ty, DAG,
Eric Christopher96e72c62015-01-29 23:27:36 +00001700 (ABI.IsN32() || ABI.IsN64()) ? MipsII::MO_GOT_DISP
1701 : MipsII::MO_GOT16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00001702 DAG.getEntryNode(), MachinePointerInfo::getGOT());
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001703}
1704
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001705SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op,
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +00001706 SelectionDAG &DAG) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001707 BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op);
1708 EVT Ty = Op.getValueType();
Akira Hatanaka30f97cf2013-09-25 00:30:25 +00001709
Eric Christopher96e72c62015-01-29 23:27:36 +00001710 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !ABI.IsN64())
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001711 return getAddrNonPIC(N, SDLoc(N), Ty, DAG);
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001712
Eric Christopher96e72c62015-01-29 23:27:36 +00001713 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +00001714}
1715
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001716SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001717lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001718{
Akira Hatanakabff84e12011-12-14 18:26:41 +00001719 // If the relocation model is PIC, use the General Dynamic TLS Model or
1720 // Local Dynamic TLS model, otherwise use the Initial Exec or
1721 // Local Exec TLS Model.
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001722
1723 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001724 SDLoc DL(GA);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001725 const GlobalValue *GV = GA->getGlobal();
1726 EVT PtrVT = getPointerTy();
1727
Hans Wennborgaea41202012-05-04 09:40:39 +00001728 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
1729
1730 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Hans Wennborg245917b2012-06-04 14:02:08 +00001731 // General Dynamic and Local Dynamic TLS Model.
1732 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
1733 : MipsII::MO_TLSGD;
1734
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001735 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag);
1736 SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT,
1737 getGlobalReg(DAG, PtrVT), TGA);
Akira Hatanakaf10ee842011-12-08 21:05:38 +00001738 unsigned PtrSize = PtrVT.getSizeInBits();
1739 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1740
Benjamin Kramer64ba50a2011-12-11 12:21:34 +00001741 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001742
1743 ArgListTy Args;
1744 ArgListEntry Entry;
1745 Entry.Node = Argument;
Akira Hatanakadee6c822011-12-08 20:34:32 +00001746 Entry.Ty = PtrTy;
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001747 Args.push_back(Entry);
Jia Liuf54f60f2012-02-28 07:46:26 +00001748
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00001749 TargetLowering::CallLoweringInfo CLI(DAG);
1750 CLI.setDebugLoc(DL).setChain(DAG.getEntryNode())
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00001751 .setCallee(CallingConv::C, PtrTy, TlsGetAddr, std::move(Args), 0);
Justin Holewinskiaa583972012-05-25 16:35:28 +00001752 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001753
Akira Hatanakabff84e12011-12-14 18:26:41 +00001754 SDValue Ret = CallResult.first;
1755
Hans Wennborgaea41202012-05-04 09:40:39 +00001756 if (model != TLSModel::LocalDynamic)
Akira Hatanakabff84e12011-12-14 18:26:41 +00001757 return Ret;
1758
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001759 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanakabff84e12011-12-14 18:26:41 +00001760 MipsII::MO_DTPREL_HI);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001761 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1762 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanakabff84e12011-12-14 18:26:41 +00001763 MipsII::MO_DTPREL_LO);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001764 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1765 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret);
1766 return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001767 }
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001768
1769 SDValue Offset;
Hans Wennborgaea41202012-05-04 09:40:39 +00001770 if (model == TLSModel::InitialExec) {
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001771 // Initial Exec TLS Model
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001772 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001773 MipsII::MO_GOTTPREL);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001774 TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT),
Akira Hatanakab049aef2012-02-24 22:34:47 +00001775 TGA);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001776 Offset = DAG.getLoad(PtrVT, DL,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001777 DAG.getEntryNode(), TGA, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001778 false, false, false, 0);
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001779 } else {
1780 // Local Exec TLS Model
Hans Wennborgaea41202012-05-04 09:40:39 +00001781 assert(model == TLSModel::LocalExec);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001782 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001783 MipsII::MO_TPREL_HI);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001784 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001785 MipsII::MO_TPREL_LO);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001786 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1787 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1788 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001789 }
1790
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001791 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
1792 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001793}
1794
1795SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001796lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +00001797{
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001798 JumpTableSDNode *N = cast<JumpTableSDNode>(Op);
1799 EVT Ty = Op.getValueType();
Akira Hatanaka30f97cf2013-09-25 00:30:25 +00001800
Eric Christopher96e72c62015-01-29 23:27:36 +00001801 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !ABI.IsN64())
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001802 return getAddrNonPIC(N, SDLoc(N), Ty, DAG);
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001803
Eric Christopher96e72c62015-01-29 23:27:36 +00001804 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +00001805}
1806
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001807SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001808lowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +00001809{
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001810 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
1811 EVT Ty = Op.getValueType();
Bruno Cardoso Lopes2db07582009-11-25 12:17:58 +00001812
Eric Christopher96e72c62015-01-29 23:27:36 +00001813 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !ABI.IsN64()) {
Eric Christopher36fe0282015-02-03 07:22:52 +00001814 const MipsTargetObjectFile *TLOF =
1815 static_cast<const MipsTargetObjectFile *>(
1816 getTargetMachine().getObjFileLowering());
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001817
Eric Christopher36fe0282015-02-03 07:22:52 +00001818 if (TLOF->IsConstantInSmallSection(N->getConstVal(), getTargetMachine()))
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001819 // %gp_rel relocation
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001820 return getAddrGPRel(N, SDLoc(N), Ty, DAG);
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001821
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001822 return getAddrNonPIC(N, SDLoc(N), Ty, DAG);
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001823 }
Bruno Cardoso Lopesfdb4cec2008-07-23 16:01:50 +00001824
Eric Christopher96e72c62015-01-29 23:27:36 +00001825 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +00001826}
1827
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001828SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman31ae5862010-04-17 14:41:14 +00001829 MachineFunction &MF = DAG.getMachineFunction();
1830 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1831
Andrew Trickef9de2a2013-05-25 02:42:55 +00001832 SDLoc DL(Op);
Dan Gohman31ae5862010-04-17 14:41:14 +00001833 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1834 getPointerTy());
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +00001835
1836 // vastart just stores the address of the VarArgsFrameIndex slot into the
1837 // memory location argument.
1838 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001839 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00001840 MachinePointerInfo(SV), false, false, 0);
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +00001841}
Jia Liuf54f60f2012-02-28 07:46:26 +00001842
Daniel Sanders2b553d42014-08-01 09:17:39 +00001843SDValue MipsTargetLowering::lowerVAARG(SDValue Op, SelectionDAG &DAG) const {
1844 SDNode *Node = Op.getNode();
1845 EVT VT = Node->getValueType(0);
1846 SDValue Chain = Node->getOperand(0);
1847 SDValue VAListPtr = Node->getOperand(1);
1848 unsigned Align = Node->getConstantOperandVal(3);
1849 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
1850 SDLoc DL(Node);
Eric Christopher96e72c62015-01-29 23:27:36 +00001851 unsigned ArgSlotSizeInBytes = (ABI.IsN32() || ABI.IsN64()) ? 8 : 4;
Daniel Sanders2b553d42014-08-01 09:17:39 +00001852
1853 SDValue VAListLoad = DAG.getLoad(getPointerTy(), DL, Chain, VAListPtr,
1854 MachinePointerInfo(SV), false, false, false,
1855 0);
1856 SDValue VAList = VAListLoad;
1857
1858 // Re-align the pointer if necessary.
1859 // It should only ever be necessary for 64-bit types on O32 since the minimum
1860 // argument alignment is the same as the maximum type alignment for N32/N64.
1861 //
1862 // FIXME: We currently align too often. The code generator doesn't notice
1863 // when the pointer is still aligned from the last va_arg (or pair of
1864 // va_args for the i64 on O32 case).
1865 if (Align > getMinStackArgumentAlignment()) {
1866 assert(((Align & (Align-1)) == 0) && "Expected Align to be a power of 2");
1867
1868 VAList = DAG.getNode(ISD::ADD, DL, VAList.getValueType(), VAList,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001869 DAG.getConstant(Align - 1, DL, VAList.getValueType()));
Daniel Sanders2b553d42014-08-01 09:17:39 +00001870
1871 VAList = DAG.getNode(ISD::AND, DL, VAList.getValueType(), VAList,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001872 DAG.getConstant(-(int64_t)Align, DL,
Daniel Sanders2b553d42014-08-01 09:17:39 +00001873 VAList.getValueType()));
1874 }
1875
1876 // Increment the pointer, VAList, to the next vaarg.
1877 unsigned ArgSizeInBytes = getDataLayout()->getTypeAllocSize(VT.getTypeForEVT(*DAG.getContext()));
1878 SDValue Tmp3 = DAG.getNode(ISD::ADD, DL, VAList.getValueType(), VAList,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001879 DAG.getConstant(RoundUpToAlignment(ArgSizeInBytes,
1880 ArgSlotSizeInBytes),
1881 DL, VAList.getValueType()));
Daniel Sanders2b553d42014-08-01 09:17:39 +00001882 // Store the incremented VAList to the legalized pointer
1883 Chain = DAG.getStore(VAListLoad.getValue(1), DL, Tmp3, VAListPtr,
1884 MachinePointerInfo(SV), false, false, 0);
1885
1886 // In big-endian mode we must adjust the pointer when the load size is smaller
1887 // than the argument slot size. We must also reduce the known alignment to
1888 // match. For example in the N64 ABI, we must add 4 bytes to the offset to get
1889 // the correct half of the slot, and reduce the alignment from 8 (slot
1890 // alignment) down to 4 (type alignment).
1891 if (!Subtarget.isLittle() && ArgSizeInBytes < ArgSlotSizeInBytes) {
1892 unsigned Adjustment = ArgSlotSizeInBytes - ArgSizeInBytes;
1893 VAList = DAG.getNode(ISD::ADD, DL, VAListPtr.getValueType(), VAList,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001894 DAG.getIntPtrConstant(Adjustment, DL));
Daniel Sanders2b553d42014-08-01 09:17:39 +00001895 }
1896 // Load the actual argument out of the pointer VAList
1897 return DAG.getLoad(VT, DL, Chain, VAList, MachinePointerInfo(), false, false,
1898 false, 0);
1899}
1900
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001901static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG,
1902 bool HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001903 EVT TyX = Op.getOperand(0).getValueType();
1904 EVT TyY = Op.getOperand(1).getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001905 SDLoc DL(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001906 SDValue Const1 = DAG.getConstant(1, DL, MVT::i32);
1907 SDValue Const31 = DAG.getConstant(31, DL, MVT::i32);
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001908 SDValue Res;
1909
1910 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1911 // to i32.
1912 SDValue X = (TyX == MVT::f32) ?
1913 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1914 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1915 Const1);
1916 SDValue Y = (TyY == MVT::f32) ?
1917 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
1918 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
1919 Const1);
1920
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001921 if (HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001922 // ext E, Y, 31, 1 ; extract bit31 of Y
1923 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
1924 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
1925 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
1926 } else {
1927 // sll SllX, X, 1
1928 // srl SrlX, SllX, 1
1929 // srl SrlY, Y, 31
1930 // sll SllY, SrlX, 31
1931 // or Or, SrlX, SllY
1932 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1933 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1934 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
1935 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
1936 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
1937 }
1938
1939 if (TyX == MVT::f32)
1940 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
1941
1942 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001943 Op.getOperand(0),
1944 DAG.getConstant(0, DL, MVT::i32));
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001945 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001946}
1947
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001948static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG,
1949 bool HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001950 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
1951 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
1952 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001953 SDLoc DL(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001954 SDValue Const1 = DAG.getConstant(1, DL, MVT::i32);
Eric Christopher0713a9d2011-06-08 23:55:35 +00001955
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001956 // Bitcast to integer nodes.
1957 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
1958 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001959
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001960 if (HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001961 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
1962 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
1963 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001964 DAG.getConstant(WidthY - 1, DL, MVT::i32), Const1);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001965
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001966 if (WidthX > WidthY)
1967 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
1968 else if (WidthY > WidthX)
1969 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001970
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001971 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001972 DAG.getConstant(WidthX - 1, DL, MVT::i32), Const1,
1973 X);
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001974 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
1975 }
1976
1977 // (d)sll SllX, X, 1
1978 // (d)srl SrlX, SllX, 1
1979 // (d)srl SrlY, Y, width(Y)-1
1980 // (d)sll SllY, SrlX, width(Y)-1
1981 // or Or, SrlX, SllY
1982 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
1983 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
1984 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001985 DAG.getConstant(WidthY - 1, DL, MVT::i32));
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001986
1987 if (WidthX > WidthY)
1988 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
1989 else if (WidthY > WidthX)
1990 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
1991
1992 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001993 DAG.getConstant(WidthX - 1, DL, MVT::i32));
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001994 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
1995 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001996}
1997
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00001998SDValue
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001999MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00002000 if (Subtarget.isGP64bit())
2001 return lowerFCOPYSIGN64(Op, DAG, Subtarget.hasExtractInsert());
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002002
Eric Christopher1c29a652014-07-18 22:55:25 +00002003 return lowerFCOPYSIGN32(Op, DAG, Subtarget.hasExtractInsert());
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002004}
2005
Akira Hatanaka66277522011-06-02 00:24:44 +00002006SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002007lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes5444a7b2011-06-16 00:40:02 +00002008 // check the depth
2009 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka15506782011-06-07 18:58:42 +00002010 "Frame address can only be determined for current frame.");
Akira Hatanaka66277522011-06-02 00:24:44 +00002011
2012 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2013 MFI->setFrameAddressIsTaken(true);
2014 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002015 SDLoc DL(Op);
Eric Christopher96e72c62015-01-29 23:27:36 +00002016 SDValue FrameAddr = DAG.getCopyFromReg(
2017 DAG.getEntryNode(), DL, ABI.IsN64() ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka66277522011-06-02 00:24:44 +00002018 return FrameAddr;
2019}
2020
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002021SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op,
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002022 SelectionDAG &DAG) const {
Bill Wendling908bf812014-01-06 00:43:20 +00002023 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00002024 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00002025
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002026 // check the depth
2027 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
2028 "Return address can be determined only for current frame.");
2029
2030 MachineFunction &MF = DAG.getMachineFunction();
2031 MachineFrameInfo *MFI = MF.getFrameInfo();
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00002032 MVT VT = Op.getSimpleValueType();
Eric Christopher96e72c62015-01-29 23:27:36 +00002033 unsigned RA = ABI.IsN64() ? Mips::RA_64 : Mips::RA;
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002034 MFI->setReturnAddressIsTaken(true);
2035
2036 // Return RA, which contains the return address. Mark it an implicit live-in.
2037 unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00002038 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT);
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002039}
2040
Akira Hatanakac0b02062013-01-30 00:26:49 +00002041// An EH_RETURN is the result of lowering llvm.eh.return which in turn is
2042// generated from __builtin_eh_return (offset, handler)
2043// The effect of this is to adjust the stack pointer by "offset"
2044// and then branch to "handler".
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002045SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Akira Hatanakac0b02062013-01-30 00:26:49 +00002046 const {
2047 MachineFunction &MF = DAG.getMachineFunction();
2048 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2049
2050 MipsFI->setCallsEhReturn();
2051 SDValue Chain = Op.getOperand(0);
2052 SDValue Offset = Op.getOperand(1);
2053 SDValue Handler = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002054 SDLoc DL(Op);
Eric Christopher96e72c62015-01-29 23:27:36 +00002055 EVT Ty = ABI.IsN64() ? MVT::i64 : MVT::i32;
Akira Hatanakac0b02062013-01-30 00:26:49 +00002056
2057 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
2058 // EH_RETURN nodes, so that instructions are emitted back-to-back.
Eric Christopher96e72c62015-01-29 23:27:36 +00002059 unsigned OffsetReg = ABI.IsN64() ? Mips::V1_64 : Mips::V1;
2060 unsigned AddrReg = ABI.IsN64() ? Mips::V0_64 : Mips::V0;
Akira Hatanakac0b02062013-01-30 00:26:49 +00002061 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
2062 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
2063 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
2064 DAG.getRegister(OffsetReg, Ty),
2065 DAG.getRegister(AddrReg, getPointerTy()),
2066 Chain.getValue(1));
2067}
2068
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002069SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
Akira Hatanaka5fd22482012-06-14 21:10:56 +00002070 SelectionDAG &DAG) const {
Eli Friedman26a48482011-07-27 22:21:52 +00002071 // FIXME: Need pseudo-fence for 'singlethread' fences
2072 // FIXME: Set SType for weaker fences where supported/appropriate.
2073 unsigned SType = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002074 SDLoc DL(Op);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002075 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002076 DAG.getConstant(SType, DL, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00002077}
2078
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002079SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
Akira Hatanaka5fd22482012-06-14 21:10:56 +00002080 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002081 SDLoc DL(Op);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002082 MVT VT = Subtarget.isGP64bit() ? MVT::i64 : MVT::i32;
2083
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002084 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2085 SDValue Shamt = Op.getOperand(2);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002086 // if shamt < (VT.bits):
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002087 // lo = (shl lo, shamt)
2088 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
2089 // else:
2090 // lo = 0
2091 // hi = (shl lo, shamt[4:0])
2092 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002093 DAG.getConstant(-1, DL, MVT::i32));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002094 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, VT, Lo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002095 DAG.getConstant(1, DL, VT));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002096 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, VT, ShiftRight1Lo, Not);
2097 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, VT, Hi, Shamt);
2098 SDValue Or = DAG.getNode(ISD::OR, DL, VT, ShiftLeftHi, ShiftRightLo);
2099 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, VT, Lo, Shamt);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002100 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
Daniel Sanders301f9372015-04-29 12:28:58 +00002101 DAG.getConstant(VT.getSizeInBits(), DL, MVT::i32));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002102 Lo = DAG.getNode(ISD::SELECT, DL, VT, Cond,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002103 DAG.getConstant(0, DL, VT), ShiftLeftLo);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002104 Hi = DAG.getNode(ISD::SELECT, DL, VT, Cond, ShiftLeftLo, Or);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002105
2106 SDValue Ops[2] = {Lo, Hi};
Craig Topper64941d92014-04-27 19:20:57 +00002107 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002108}
2109
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002110SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002111 bool IsSRA) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002112 SDLoc DL(Op);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002113 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2114 SDValue Shamt = Op.getOperand(2);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002115 MVT VT = Subtarget.isGP64bit() ? MVT::i64 : MVT::i32;
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002116
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002117 // if shamt < (VT.bits):
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002118 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
2119 // if isSRA:
2120 // hi = (sra hi, shamt)
2121 // else:
2122 // hi = (srl hi, shamt)
2123 // else:
2124 // if isSRA:
2125 // lo = (sra hi, shamt[4:0])
2126 // hi = (sra hi, 31)
2127 // else:
2128 // lo = (srl hi, shamt[4:0])
2129 // hi = 0
2130 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002131 DAG.getConstant(-1, DL, MVT::i32));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002132 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, VT, Hi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002133 DAG.getConstant(1, DL, VT));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002134 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, VT, ShiftLeft1Hi, Not);
2135 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, VT, Lo, Shamt);
2136 SDValue Or = DAG.getNode(ISD::OR, DL, VT, ShiftLeftHi, ShiftRightLo);
2137 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL,
2138 DL, VT, Hi, Shamt);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002139 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
Daniel Sanders301f9372015-04-29 12:28:58 +00002140 DAG.getConstant(VT.getSizeInBits(), DL, MVT::i32));
2141 SDValue Ext = DAG.getNode(ISD::SRA, DL, VT, Hi,
2142 DAG.getConstant(VT.getSizeInBits() - 1, DL, VT));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002143 Lo = DAG.getNode(ISD::SELECT, DL, VT, Cond, ShiftRightHi, Or);
2144 Hi = DAG.getNode(ISD::SELECT, DL, VT, Cond,
Daniel Sanders301f9372015-04-29 12:28:58 +00002145 IsSRA ? Ext : DAG.getConstant(0, DL, VT), ShiftRightHi);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002146
2147 SDValue Ops[2] = {Lo, Hi};
Craig Topper64941d92014-04-27 19:20:57 +00002148 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002149}
2150
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002151static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002152 SDValue Chain, SDValue Src, unsigned Offset) {
Akira Hatanaka95866182012-06-13 19:06:08 +00002153 SDValue Ptr = LD->getBasePtr();
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002154 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
Akira Hatanaka95866182012-06-13 19:06:08 +00002155 EVT BasePtrVT = Ptr.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002156 SDLoc DL(LD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002157 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
2158
2159 if (Offset)
Akira Hatanaka95866182012-06-13 19:06:08 +00002160 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002161 DAG.getConstant(Offset, DL, BasePtrVT));
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002162
2163 SDValue Ops[] = { Chain, Ptr, Src };
Craig Topper206fcd42014-04-26 19:29:41 +00002164 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002165 LD->getMemOperand());
2166}
2167
2168// Expand an unaligned 32 or 64-bit integer load node.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002169SDValue MipsTargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002170 LoadSDNode *LD = cast<LoadSDNode>(Op);
2171 EVT MemVT = LD->getMemoryVT();
2172
Eric Christopher1c29a652014-07-18 22:55:25 +00002173 if (Subtarget.systemSupportsUnalignedAccess())
Daniel Sandersac272632014-05-23 13:18:02 +00002174 return Op;
2175
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002176 // Return if load is aligned or if MemVT is neither i32 nor i64.
2177 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
2178 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
2179 return SDValue();
2180
Eric Christopher1c29a652014-07-18 22:55:25 +00002181 bool IsLittle = Subtarget.isLittle();
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002182 EVT VT = Op.getValueType();
2183 ISD::LoadExtType ExtType = LD->getExtensionType();
2184 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
2185
2186 assert((VT == MVT::i32) || (VT == MVT::i64));
2187
2188 // Expand
2189 // (set dst, (i64 (load baseptr)))
2190 // to
2191 // (set tmp, (ldl (add baseptr, 7), undef))
2192 // (set dst, (ldr baseptr, tmp))
2193 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002194 SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002195 IsLittle ? 7 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002196 return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002197 IsLittle ? 0 : 7);
2198 }
2199
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002200 SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002201 IsLittle ? 3 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002202 SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002203 IsLittle ? 0 : 3);
2204
2205 // Expand
2206 // (set dst, (i32 (load baseptr))) or
2207 // (set dst, (i64 (sextload baseptr))) or
2208 // (set dst, (i64 (extload baseptr)))
2209 // to
2210 // (set tmp, (lwl (add baseptr, 3), undef))
2211 // (set dst, (lwr baseptr, tmp))
2212 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
2213 (ExtType == ISD::EXTLOAD))
2214 return LWR;
2215
2216 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
2217
2218 // Expand
2219 // (set dst, (i64 (zextload baseptr)))
2220 // to
2221 // (set tmp0, (lwl (add baseptr, 3), undef))
2222 // (set tmp1, (lwr baseptr, tmp0))
2223 // (set tmp2, (shl tmp1, 32))
2224 // (set dst, (srl tmp2, 32))
Andrew Trickef9de2a2013-05-25 02:42:55 +00002225 SDLoc DL(LD);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002226 SDValue Const32 = DAG.getConstant(32, DL, MVT::i32);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002227 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
Akira Hatanaka67346852012-06-04 17:46:29 +00002228 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
2229 SDValue Ops[] = { SRL, LWR.getValue(1) };
Craig Topper64941d92014-04-27 19:20:57 +00002230 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002231}
2232
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002233static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002234 SDValue Chain, unsigned Offset) {
Akira Hatanaka95866182012-06-13 19:06:08 +00002235 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
2236 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002237 SDLoc DL(SD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002238 SDVTList VTList = DAG.getVTList(MVT::Other);
2239
2240 if (Offset)
Akira Hatanaka95866182012-06-13 19:06:08 +00002241 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002242 DAG.getConstant(Offset, DL, BasePtrVT));
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002243
2244 SDValue Ops[] = { Chain, Value, Ptr };
Craig Topper206fcd42014-04-26 19:29:41 +00002245 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002246 SD->getMemOperand());
2247}
2248
2249// Expand an unaligned 32 or 64-bit integer store node.
Akira Hatanakad82ee942013-05-16 20:45:17 +00002250static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG,
2251 bool IsLittle) {
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002252 SDValue Value = SD->getValue(), Chain = SD->getChain();
2253 EVT VT = Value.getValueType();
2254
2255 // Expand
2256 // (store val, baseptr) or
2257 // (truncstore val, baseptr)
2258 // to
2259 // (swl val, (add baseptr, 3))
2260 // (swr val, baseptr)
2261 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002262 SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002263 IsLittle ? 3 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002264 return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002265 }
2266
2267 assert(VT == MVT::i64);
2268
2269 // Expand
2270 // (store val, baseptr)
2271 // to
2272 // (sdl val, (add baseptr, 7))
2273 // (sdr val, baseptr)
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002274 SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2275 return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002276}
2277
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002278// Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr).
2279static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG) {
2280 SDValue Val = SD->getValue();
2281
2282 if (Val.getOpcode() != ISD::FP_TO_SINT)
2283 return SDValue();
2284
2285 EVT FPTy = EVT::getFloatingPointVT(Val.getValueSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002286 SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy,
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002287 Val.getOperand(0));
2288
Andrew Trickef9de2a2013-05-25 02:42:55 +00002289 return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(),
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002290 SD->getPointerInfo(), SD->isVolatile(),
2291 SD->isNonTemporal(), SD->getAlignment());
2292}
2293
Akira Hatanakad82ee942013-05-16 20:45:17 +00002294SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2295 StoreSDNode *SD = cast<StoreSDNode>(Op);
2296 EVT MemVT = SD->getMemoryVT();
2297
2298 // Lower unaligned integer stores.
Eric Christopher1c29a652014-07-18 22:55:25 +00002299 if (!Subtarget.systemSupportsUnalignedAccess() &&
Daniel Sandersac272632014-05-23 13:18:02 +00002300 (SD->getAlignment() < MemVT.getSizeInBits() / 8) &&
Akira Hatanakad82ee942013-05-16 20:45:17 +00002301 ((MemVT == MVT::i32) || (MemVT == MVT::i64)))
Eric Christopher1c29a652014-07-18 22:55:25 +00002302 return lowerUnalignedIntStore(SD, DAG, Subtarget.isLittle());
Akira Hatanakad82ee942013-05-16 20:45:17 +00002303
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002304 return lowerFP_TO_SINT_STORE(SD, DAG);
Akira Hatanakad82ee942013-05-16 20:45:17 +00002305}
2306
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002307SDValue MipsTargetLowering::lowerADD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002308 if (Op->getOperand(0).getOpcode() != ISD::FRAMEADDR
2309 || cast<ConstantSDNode>
2310 (Op->getOperand(0).getOperand(0))->getZExtValue() != 0
2311 || Op->getOperand(1).getOpcode() != ISD::FRAME_TO_ARGS_OFFSET)
2312 return SDValue();
2313
2314 // The pattern
2315 // (add (frameaddr 0), (frame_to_args_offset))
2316 // results from lowering llvm.eh.dwarf.cfa intrinsic. Transform it to
2317 // (add FrameObject, 0)
2318 // where FrameObject is a fixed StackObject with offset 0 which points to
2319 // the old stack pointer.
2320 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2321 EVT ValTy = Op->getValueType(0);
2322 int FI = MFI->CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
2323 SDValue InArgsAddr = DAG.getFrameIndex(FI, ValTy);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002324 SDLoc DL(Op);
2325 return DAG.getNode(ISD::ADD, DL, ValTy, InArgsAddr,
2326 DAG.getConstant(0, DL, ValTy));
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002327}
2328
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002329SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op,
2330 SelectionDAG &DAG) const {
2331 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002332 SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy,
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002333 Op.getOperand(0));
Andrew Trickef9de2a2013-05-25 02:42:55 +00002334 return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc);
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002335}
2336
Akira Hatanakae2489122011-04-15 21:51:11 +00002337//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002338// Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002339//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002340
Akira Hatanakae2489122011-04-15 21:51:11 +00002341//===----------------------------------------------------------------------===//
Wesley Peck527da1b2010-11-23 03:31:01 +00002342// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002343// Mips O32 ABI rules:
2344// ---
2345// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peck527da1b2010-11-23 03:31:01 +00002346// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002347// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peck527da1b2010-11-23 03:31:01 +00002348// f64 - Only passed in two aliased f32 registers if no int reg has been used
2349// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Sylvestre Ledru469de192014-08-11 18:04:46 +00002350// not used, it must be shadowed. If only A3 is available, shadow it and
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002351// go to stack.
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002352//
2353// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanakae2489122011-04-15 21:51:11 +00002354//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002355
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00002356static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2357 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002358 CCState &State, ArrayRef<MCPhysReg> F64Regs) {
Eric Christopher96e72c62015-01-29 23:27:36 +00002359 const MipsSubtarget &Subtarget = static_cast<const MipsSubtarget &>(
2360 State.getMachineFunction().getSubtarget());
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002361
Craig Topper840beec2014-04-04 05:16:06 +00002362 static const MCPhysReg IntRegs[] = { Mips::A0, Mips::A1, Mips::A2, Mips::A3 };
2363 static const MCPhysReg F32Regs[] = { Mips::F12, Mips::F14 };
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002364
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002365 // Do not process byval args here.
2366 if (ArgFlags.isByVal())
2367 return true;
Akira Hatanaka5e16c6a2011-05-24 19:18:33 +00002368
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002369 // Promote i8 and i16
Daniel Sandersd134c9d2014-12-02 20:40:27 +00002370 if (ArgFlags.isInReg() && !Subtarget.isLittle()) {
2371 if (LocVT == MVT::i8 || LocVT == MVT::i16 || LocVT == MVT::i32) {
2372 LocVT = MVT::i32;
2373 if (ArgFlags.isSExt())
2374 LocInfo = CCValAssign::SExtUpper;
2375 else if (ArgFlags.isZExt())
2376 LocInfo = CCValAssign::ZExtUpper;
2377 else
2378 LocInfo = CCValAssign::AExtUpper;
2379 }
2380 }
2381
2382 // Promote i8 and i16
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002383 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
2384 LocVT = MVT::i32;
2385 if (ArgFlags.isSExt())
2386 LocInfo = CCValAssign::SExt;
2387 else if (ArgFlags.isZExt())
2388 LocInfo = CCValAssign::ZExt;
2389 else
2390 LocInfo = CCValAssign::AExt;
2391 }
2392
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002393 unsigned Reg;
2394
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002395 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
2396 // is true: function is vararg, argument is 3rd or higher, there is previous
2397 // argument which is not f32 or f64.
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002398 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1 ||
2399 State.getFirstUnallocated(F32Regs) != ValNo;
Akira Hatanaka9e6a8cc2011-05-19 20:29:48 +00002400 unsigned OrigAlign = ArgFlags.getOrigAlign();
2401 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002402
2403 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002404 Reg = State.AllocateReg(IntRegs);
Akira Hatanaka9e6a8cc2011-05-19 20:29:48 +00002405 // If this is the first part of an i64 arg,
2406 // the allocated register must be either A0 or A2.
2407 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002408 Reg = State.AllocateReg(IntRegs);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002409 LocVT = MVT::i32;
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002410 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
2411 // Allocate int register and shadow next int register. If first
2412 // available register is Mips::A1 or Mips::A3, shadow it too.
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002413 Reg = State.AllocateReg(IntRegs);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002414 if (Reg == Mips::A1 || Reg == Mips::A3)
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002415 Reg = State.AllocateReg(IntRegs);
2416 State.AllocateReg(IntRegs);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002417 LocVT = MVT::i32;
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002418 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
2419 // we are guaranteed to find an available float register
2420 if (ValVT == MVT::f32) {
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002421 Reg = State.AllocateReg(F32Regs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002422 // Shadow int register
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002423 State.AllocateReg(IntRegs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002424 } else {
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002425 Reg = State.AllocateReg(F64Regs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002426 // Shadow int registers
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002427 unsigned Reg2 = State.AllocateReg(IntRegs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002428 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002429 State.AllocateReg(IntRegs);
2430 State.AllocateReg(IntRegs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002431 }
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002432 } else
2433 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002434
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002435 if (!Reg) {
2436 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3,
2437 OrigAlign);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002438 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002439 } else
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002440 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002441
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002442 return false;
Akira Hatanaka202f6402011-11-12 02:20:46 +00002443}
2444
Akira Hatanakabfb66242013-08-20 23:38:40 +00002445static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT,
2446 MVT LocVT, CCValAssign::LocInfo LocInfo,
2447 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002448 static const MCPhysReg F64Regs[] = { Mips::D6, Mips::D7 };
Akira Hatanakabfb66242013-08-20 23:38:40 +00002449
2450 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2451}
2452
2453static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT,
2454 MVT LocVT, CCValAssign::LocInfo LocInfo,
2455 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002456 static const MCPhysReg F64Regs[] = { Mips::D12_64, Mips::D14_64 };
Akira Hatanakabfb66242013-08-20 23:38:40 +00002457
2458 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2459}
2460
Reid Klecknerd3781742014-11-14 00:39:33 +00002461static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2462 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
2463 CCState &State) LLVM_ATTRIBUTE_UNUSED;
Reed Kotlerd5c41962014-11-13 23:37:45 +00002464
Akira Hatanaka202f6402011-11-12 02:20:46 +00002465#include "MipsGenCallingConv.inc"
2466
Akira Hatanakae2489122011-04-15 21:51:11 +00002467//===----------------------------------------------------------------------===//
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002468// Call Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002469//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002470
Akira Hatanaka61bbcce2011-09-23 00:58:33 +00002471// Return next O32 integer argument register.
2472static unsigned getNextIntArgReg(unsigned Reg) {
2473 assert((Reg == Mips::A0) || (Reg == Mips::A2));
2474 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
2475}
2476
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002477SDValue
2478MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002479 SDValue Chain, SDValue Arg, SDLoc DL,
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002480 bool IsTailCall, SelectionDAG &DAG) const {
2481 if (!IsTailCall) {
2482 SDValue PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002483 DAG.getIntPtrConstant(Offset, DL));
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002484 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo(), false,
2485 false, 0);
2486 }
2487
2488 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2489 int FI = MFI->CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
2490 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2491 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(),
2492 /*isVolatile=*/ true, false, 0);
2493}
2494
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002495void MipsTargetLowering::
2496getOpndList(SmallVectorImpl<SDValue> &Ops,
2497 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
2498 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
Sasa Stankovic7072a792014-10-01 08:22:21 +00002499 bool IsCallReloc, CallLoweringInfo &CLI, SDValue Callee,
2500 SDValue Chain) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002501 // Insert node "GP copy globalreg" before call to function.
2502 //
2503 // R_MIPS_CALL* operators (emitted when non-internal functions are called
2504 // in PIC mode) allow symbols to be resolved via lazy binding.
2505 // The lazy binding stub requires GP to point to the GOT.
Sasa Stankovic7072a792014-10-01 08:22:21 +00002506 // Note that we don't need GP to point to the GOT for indirect calls
2507 // (when R_MIPS_CALL* is not used for the call) because Mips linker generates
2508 // lazy binding stub for a function only when R_MIPS_CALL* are the only relocs
2509 // used for the function (that is, Mips linker doesn't generate lazy binding
2510 // stub for a function whose address is taken in the program).
2511 if (IsPICCall && !InternalLinkage && IsCallReloc) {
Eric Christopher96e72c62015-01-29 23:27:36 +00002512 unsigned GPReg = ABI.IsN64() ? Mips::GP_64 : Mips::GP;
2513 EVT Ty = ABI.IsN64() ? MVT::i64 : MVT::i32;
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002514 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty)));
2515 }
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002516
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002517 // Build a sequence of copy-to-reg nodes chained together with token
2518 // chain and flag operands which copy the outgoing args into registers.
2519 // The InFlag in necessary since all emitted instructions must be
2520 // stuck together.
2521 SDValue InFlag;
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002522
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002523 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2524 Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, RegsToPass[i].first,
2525 RegsToPass[i].second, InFlag);
2526 InFlag = Chain.getValue(1);
2527 }
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002528
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002529 // Add argument registers to the end of the list so that they are
2530 // known live into the call.
2531 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2532 Ops.push_back(CLI.DAG.getRegister(RegsToPass[i].first,
2533 RegsToPass[i].second.getValueType()));
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002534
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002535 // Add a register mask operand representing the call-preserved registers.
Eric Christopher96e72c62015-01-29 23:27:36 +00002536 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00002537 const uint32_t *Mask =
2538 TRI->getCallPreservedMask(CLI.DAG.getMachineFunction(), CLI.CallConv);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002539 assert(Mask && "Missing call preserved mask for calling convention");
Eric Christopher1c29a652014-07-18 22:55:25 +00002540 if (Subtarget.inMips16HardFloat()) {
Reed Kotler783c7942013-05-10 22:25:39 +00002541 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
2542 llvm::StringRef Sym = G->getGlobal()->getName();
2543 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
Reed Kotler3230e722013-12-12 02:41:11 +00002544 if (F && F->hasFnAttribute("__Mips16RetHelper")) {
Reed Kotler783c7942013-05-10 22:25:39 +00002545 Mask = MipsRegisterInfo::getMips16RetHelperMask();
2546 }
2547 }
2548 }
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002549 Ops.push_back(CLI.DAG.getRegisterMask(Mask));
2550
2551 if (InFlag.getNode())
2552 Ops.push_back(InFlag);
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002553}
2554
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002555/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman624801e2009-01-26 03:15:54 +00002556/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002557SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00002558MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002559 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00002560 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002561 SDLoc DL = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00002562 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
2563 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
2564 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Akira Hatanakabeda2242012-07-31 18:46:41 +00002565 SDValue Chain = CLI.Chain;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002566 SDValue Callee = CLI.Callee;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002567 bool &IsTailCall = CLI.IsTailCall;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002568 CallingConv::ID CallConv = CLI.CallConv;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002569 bool IsVarArg = CLI.IsVarArg;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002570
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002571 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002572 MachineFrameInfo *MFI = MF.getFrameInfo();
Eric Christopher96e72c62015-01-29 23:27:36 +00002573 const TargetFrameLowering *TFL = Subtarget.getFrameLowering();
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002574 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +00002575 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002576
2577 // Analyze operands of the call, assigning locations to each operand.
2578 SmallVector<CCValAssign, 16> ArgLocs;
Daniel Sanders41a64c42014-11-07 11:10:48 +00002579 MipsCCState CCInfo(
2580 CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs, *DAG.getContext(),
2581 MipsCCState::getSpecialCallingConvForCallee(Callee.getNode(), Subtarget));
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002582
2583 // Allocate the reserved argument area. It seems strange to do this from the
2584 // caller side but removing it breaks the frame size calculation.
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002585 CCInfo.AllocateStack(ABI.GetCalleeAllocdArgSizeInBytes(CallConv), 1);
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002586
Daniel Sanderscfad1e32014-11-07 11:43:49 +00002587 CCInfo.AnalyzeCallOperands(Outs, CC_Mips, CLI.getArgs(), Callee.getNode());
Wesley Peck527da1b2010-11-23 03:31:01 +00002588
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002589 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka195a1e22011-06-08 17:39:33 +00002590 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka97ba7692012-07-26 23:27:01 +00002591
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002592 // Check if it's really possible to do a tail call.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002593 if (IsTailCall)
Daniel Sanders23e98772014-11-02 16:09:29 +00002594 IsTailCall = isEligibleForTailCallOptimization(
2595 CCInfo, NextStackOffset, *MF.getInfo<MipsFunctionInfo>());
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002596
Reid Kleckner5772b772014-04-24 20:14:34 +00002597 if (!IsTailCall && CLI.CS && CLI.CS->isMustTailCall())
2598 report_fatal_error("failed to perform tail call elimination on a call "
2599 "site marked musttail");
2600
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002601 if (IsTailCall)
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002602 ++NumTailCalls;
2603
Akira Hatanaka79738332011-09-19 20:26:02 +00002604 // Chain is the output chain of the last Load/Store or CopyToReg node.
2605 // ByValChain is the output chain of the last Memcpy node created for copying
2606 // byval arguments to the stack.
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002607 unsigned StackAlignment = TFL->getStackAlignment();
2608 NextStackOffset = RoundUpToAlignment(NextStackOffset, StackAlignment);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002609 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, DL, true);
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002610
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002611 if (!IsTailCall)
Andrew Trickad6d08a2013-05-29 22:03:55 +00002612 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffsetVal, DL);
Akira Hatanakabeda2242012-07-31 18:46:41 +00002613
Daniel Sandersd897b562014-03-27 10:46:12 +00002614 SDValue StackPtr = DAG.getCopyFromReg(
Eric Christopher96e72c62015-01-29 23:27:36 +00002615 Chain, DL, ABI.IsN64() ? Mips::SP_64 : Mips::SP, getPointerTy());
Akira Hatanaka195a1e22011-06-08 17:39:33 +00002616
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002617 // With EABI is it possible to have 16 args on registers.
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002618 std::deque< std::pair<unsigned, SDValue> > RegsToPass;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002619 SmallVector<SDValue, 8> MemOpChains;
Daniel Sanders23e98772014-11-02 16:09:29 +00002620
2621 CCInfo.rewindByValRegsInfo();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002622
2623 // Walk the register/memloc assignments, inserting copies/loads.
2624 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002625 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002626 CCValAssign &VA = ArgLocs[i];
Akira Hatanakab20a3252011-10-28 19:49:00 +00002627 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka19891f82011-11-12 02:34:50 +00002628 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002629 bool UseUpperBits = false;
Akira Hatanaka19891f82011-11-12 02:34:50 +00002630
2631 // ByVal Arg.
2632 if (Flags.isByVal()) {
Daniel Sanders23e98772014-11-02 16:09:29 +00002633 unsigned FirstByValReg, LastByValReg;
2634 unsigned ByValIdx = CCInfo.getInRegsParamsProcessed();
2635 CCInfo.getInRegsParamInfo(ByValIdx, FirstByValReg, LastByValReg);
2636
Akira Hatanaka19891f82011-11-12 02:34:50 +00002637 assert(Flags.getByValSize() &&
2638 "ByVal args of size 0 should have been ignored by front-end.");
Daniel Sanders23e98772014-11-02 16:09:29 +00002639 assert(ByValIdx < CCInfo.getInRegsParamsCount());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002640 assert(!IsTailCall &&
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002641 "Do not tail-call optimize if there is a byval argument.");
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002642 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002643 FirstByValReg, LastByValReg, Flags, Subtarget.isLittle(),
2644 VA);
Daniel Sanders23e98772014-11-02 16:09:29 +00002645 CCInfo.nextInRegsParam();
Akira Hatanaka19891f82011-11-12 02:34:50 +00002646 continue;
2647 }
Jia Liuf54f60f2012-02-28 07:46:26 +00002648
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002649 // Promote the value if needed.
2650 switch (VA.getLocInfo()) {
Daniel Sandersc43cda82014-11-07 16:54:21 +00002651 default:
2652 llvm_unreachable("Unknown loc info!");
Wesley Peck527da1b2010-11-23 03:31:01 +00002653 case CCValAssign::Full:
Akira Hatanakab20a3252011-10-28 19:49:00 +00002654 if (VA.isRegLoc()) {
2655 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
Akira Hatanaka3b7391d2013-03-05 22:20:28 +00002656 (ValVT == MVT::f64 && LocVT == MVT::i64) ||
2657 (ValVT == MVT::i64 && LocVT == MVT::f64))
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002658 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
Akira Hatanakab20a3252011-10-28 19:49:00 +00002659 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002660 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002661 Arg, DAG.getConstant(0, DL, MVT::i32));
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002662 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002663 Arg, DAG.getConstant(1, DL, MVT::i32));
Eric Christopher1c29a652014-07-18 22:55:25 +00002664 if (!Subtarget.isLittle())
Akira Hatanaka27916972011-04-15 19:52:08 +00002665 std::swap(Lo, Hi);
Jia Liuf54f60f2012-02-28 07:46:26 +00002666 unsigned LocRegLo = VA.getLocReg();
Akira Hatanaka61bbcce2011-09-23 00:58:33 +00002667 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2668 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2669 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002670 continue;
Wesley Peck527da1b2010-11-23 03:31:01 +00002671 }
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002672 }
2673 break;
Daniel Sanders23e98772014-11-02 16:09:29 +00002674 case CCValAssign::BCvt:
2675 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
2676 break;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002677 case CCValAssign::SExtUpper:
2678 UseUpperBits = true;
2679 // Fallthrough
Chris Lattner52f16de2008-03-17 06:57:02 +00002680 case CCValAssign::SExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002681 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002682 break;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002683 case CCValAssign::ZExtUpper:
2684 UseUpperBits = true;
2685 // Fallthrough
Chris Lattner52f16de2008-03-17 06:57:02 +00002686 case CCValAssign::ZExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002687 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002688 break;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002689 case CCValAssign::AExtUpper:
2690 UseUpperBits = true;
2691 // Fallthrough
Chris Lattner52f16de2008-03-17 06:57:02 +00002692 case CCValAssign::AExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002693 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002694 break;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002695 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002696
Daniel Sandersc43cda82014-11-07 16:54:21 +00002697 if (UseUpperBits) {
2698 unsigned ValSizeInBits = Outs[i].ArgVT.getSizeInBits();
2699 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
2700 Arg = DAG.getNode(
2701 ISD::SHL, DL, VA.getLocVT(), Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002702 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
Daniel Sandersc43cda82014-11-07 16:54:21 +00002703 }
2704
Wesley Peck527da1b2010-11-23 03:31:01 +00002705 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +00002706 // RegsToPass vector
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002707 if (VA.isRegLoc()) {
2708 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattner52f16de2008-03-17 06:57:02 +00002709 continue;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002710 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002711
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002712 // Register can't get to this point...
Chris Lattner52f16de2008-03-17 06:57:02 +00002713 assert(VA.isMemLoc());
Wesley Peck527da1b2010-11-23 03:31:01 +00002714
Wesley Peck527da1b2010-11-23 03:31:01 +00002715 // emit ISD::STORE whichs stores the
Chris Lattner52f16de2008-03-17 06:57:02 +00002716 // parameter value to a stack Location
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002717 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002718 Chain, Arg, DL, IsTailCall, DAG));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002719 }
2720
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002721 // Transform all store nodes into one single node because all store
2722 // nodes are independent of each other.
Wesley Peck527da1b2010-11-23 03:31:01 +00002723 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00002724 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002725
Bill Wendling24c79f22008-09-16 21:48:12 +00002726 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peck527da1b2010-11-23 03:31:01 +00002727 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2728 // node so that legalize doesn't hack it.
Eric Christopher96e72c62015-01-29 23:27:36 +00002729 bool IsPICCall = (ABI.IsN64() || IsPIC); // true if calls are translated to
2730 // jalr $25
Sasa Stankovic7072a792014-10-01 08:22:21 +00002731 bool GlobalOrExternal = false, InternalLinkage = false, IsCallReloc = false;
Akira Hatanakad6f1c582011-04-07 19:51:44 +00002732 SDValue CalleeLo;
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002733 EVT Ty = Callee.getValueType();
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002734
2735 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002736 if (IsPICCall) {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002737 const GlobalValue *Val = G->getGlobal();
2738 InternalLinkage = Val->hasInternalLinkage();
Akira Hatanakacf9a61b2012-12-13 03:17:29 +00002739
2740 if (InternalLinkage)
Eric Christopher96e72c62015-01-29 23:27:36 +00002741 Callee = getAddrLocal(G, DL, Ty, DAG, ABI.IsN32() || ABI.IsN64());
Sasa Stankovic7072a792014-10-01 08:22:21 +00002742 else if (LargeGOT) {
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002743 Callee = getAddrGlobalLargeGOT(G, DL, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002744 MipsII::MO_CALL_LO16, Chain,
2745 FuncInfo->callPtrInfo(Val));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002746 IsCallReloc = true;
2747 } else {
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002748 Callee = getAddrGlobal(G, DL, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002749 FuncInfo->callPtrInfo(Val));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002750 IsCallReloc = true;
2751 }
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002752 } else
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002753 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, getPointerTy(), 0,
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002754 MipsII::MO_NO_FLAG);
Akira Hatanaka8e16aac2011-12-09 01:45:12 +00002755 GlobalOrExternal = true;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002756 }
2757 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002758 const char *Sym = S->getSymbol();
2759
Eric Christopher96e72c62015-01-29 23:27:36 +00002760 if (!ABI.IsN64() && !IsPIC) // !N64 && static
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002761 Callee =
2762 DAG.getTargetExternalSymbol(Sym, getPointerTy(), MipsII::MO_NO_FLAG);
Sasa Stankovic7072a792014-10-01 08:22:21 +00002763 else if (LargeGOT) {
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002764 Callee = getAddrGlobalLargeGOT(S, DL, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002765 MipsII::MO_CALL_LO16, Chain,
2766 FuncInfo->callPtrInfo(Sym));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002767 IsCallReloc = true;
2768 } else { // N64 || PIC
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002769 Callee = getAddrGlobal(S, DL, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002770 FuncInfo->callPtrInfo(Sym));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002771 IsCallReloc = true;
2772 }
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002773
Akira Hatanaka8e16aac2011-12-09 01:45:12 +00002774 GlobalOrExternal = true;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002775 }
2776
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002777 SmallVector<SDValue, 8> Ops(1, Chain);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002778 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002779
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002780 getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal, InternalLinkage,
Sasa Stankovic7072a792014-10-01 08:22:21 +00002781 IsCallReloc, CLI, Callee, Chain);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002782
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002783 if (IsTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +00002784 return DAG.getNode(MipsISD::TailCall, DL, MVT::Other, Ops);
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002785
Craig Topper48d114b2014-04-26 18:35:24 +00002786 Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, Ops);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002787 SDValue InFlag = Chain.getValue(1);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002788
Bruno Cardoso Lopes193e64c2010-01-30 18:32:07 +00002789 // Create the CALLSEQ_END node.
Akira Hatanaka97ba7692012-07-26 23:27:01 +00002790 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002791 DAG.getIntPtrConstant(0, DL, true), InFlag, DL);
Bruno Cardoso Lopes193e64c2010-01-30 18:32:07 +00002792 InFlag = Chain.getValue(1);
2793
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002794 // Handle result values, copying them out of physregs into vregs that we
2795 // return.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002796 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg, Ins, DL, DAG,
2797 InVals, CLI);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002798}
2799
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002800/// LowerCallResult - Lower the result values of a call into the
2801/// appropriate copies out of appropriate physical registers.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002802SDValue MipsTargetLowering::LowerCallResult(
2803 SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool IsVarArg,
2804 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG,
2805 SmallVectorImpl<SDValue> &InVals,
2806 TargetLowering::CallLoweringInfo &CLI) const {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002807 // Assign locations to each value returned by this call.
2808 SmallVector<CCValAssign, 16> RVLocs;
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002809 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
2810 *DAG.getContext());
2811 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips, CLI);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002812
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002813 // Copy all of the result registers out of their specified physreg.
2814 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Daniel Sandersae275e32014-09-25 12:15:05 +00002815 CCValAssign &VA = RVLocs[i];
2816 assert(VA.isRegLoc() && "Can only return in registers!");
2817
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002818 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(),
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002819 RVLocs[i].getLocVT(), InFlag);
2820 Chain = Val.getValue(1);
2821 InFlag = Val.getValue(2);
2822
Daniel Sandersae275e32014-09-25 12:15:05 +00002823 if (VA.isUpperBitsInLoc()) {
2824 unsigned ValSizeInBits = Ins[i].ArgVT.getSizeInBits();
2825 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
2826 unsigned Shift =
2827 VA.getLocInfo() == CCValAssign::ZExtUpper ? ISD::SRL : ISD::SRA;
2828 Val = DAG.getNode(
2829 Shift, DL, VA.getLocVT(), Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002830 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
Daniel Sandersae275e32014-09-25 12:15:05 +00002831 }
2832
2833 switch (VA.getLocInfo()) {
2834 default:
2835 llvm_unreachable("Unknown loc info!");
2836 case CCValAssign::Full:
2837 break;
2838 case CCValAssign::BCvt:
2839 Val = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Val);
2840 break;
2841 case CCValAssign::AExt:
2842 case CCValAssign::AExtUpper:
2843 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2844 break;
2845 case CCValAssign::ZExt:
2846 case CCValAssign::ZExtUpper:
2847 Val = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Val,
2848 DAG.getValueType(VA.getValVT()));
2849 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2850 break;
2851 case CCValAssign::SExt:
2852 case CCValAssign::SExtUpper:
2853 Val = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Val,
2854 DAG.getValueType(VA.getValVT()));
2855 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2856 break;
2857 }
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002858
2859 InVals.push_back(Val);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002860 }
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +00002861
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002862 return Chain;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002863}
2864
Daniel Sandersc43cda82014-11-07 16:54:21 +00002865static SDValue UnpackFromArgumentSlot(SDValue Val, const CCValAssign &VA,
2866 EVT ArgVT, SDLoc DL, SelectionDAG &DAG) {
2867 MVT LocVT = VA.getLocVT();
2868 EVT ValVT = VA.getValVT();
2869
2870 // Shift into the upper bits if necessary.
2871 switch (VA.getLocInfo()) {
2872 default:
2873 break;
2874 case CCValAssign::AExtUpper:
2875 case CCValAssign::SExtUpper:
2876 case CCValAssign::ZExtUpper: {
2877 unsigned ValSizeInBits = ArgVT.getSizeInBits();
2878 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
2879 unsigned Opcode =
2880 VA.getLocInfo() == CCValAssign::ZExtUpper ? ISD::SRL : ISD::SRA;
2881 Val = DAG.getNode(
2882 Opcode, DL, VA.getLocVT(), Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002883 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
Daniel Sandersc43cda82014-11-07 16:54:21 +00002884 break;
2885 }
2886 }
2887
2888 // If this is an value smaller than the argument slot size (32-bit for O32,
2889 // 64-bit for N32/N64), it has been promoted in some way to the argument slot
2890 // size. Extract the value and insert any appropriate assertions regarding
2891 // sign/zero extension.
2892 switch (VA.getLocInfo()) {
2893 default:
2894 llvm_unreachable("Unknown loc info!");
2895 case CCValAssign::Full:
2896 break;
2897 case CCValAssign::AExtUpper:
2898 case CCValAssign::AExt:
2899 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
2900 break;
2901 case CCValAssign::SExtUpper:
2902 case CCValAssign::SExt:
2903 Val = DAG.getNode(ISD::AssertSext, DL, LocVT, Val, DAG.getValueType(ValVT));
2904 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
2905 break;
2906 case CCValAssign::ZExtUpper:
2907 case CCValAssign::ZExt:
2908 Val = DAG.getNode(ISD::AssertZext, DL, LocVT, Val, DAG.getValueType(ValVT));
2909 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
2910 break;
2911 case CCValAssign::BCvt:
2912 Val = DAG.getNode(ISD::BITCAST, DL, ValVT, Val);
2913 break;
2914 }
2915
2916 return Val;
2917}
2918
Akira Hatanakae2489122011-04-15 21:51:11 +00002919//===----------------------------------------------------------------------===//
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002920// Formal Arguments Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002921//===----------------------------------------------------------------------===//
Wesley Peck527da1b2010-11-23 03:31:01 +00002922/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002923/// and generate load operations for arguments places on the stack.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002924SDValue
2925MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanakaaef55c82011-04-15 21:00:26 +00002926 CallingConv::ID CallConv,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002927 bool IsVarArg,
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00002928 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002929 SDLoc DL, SelectionDAG &DAG,
Akira Hatanakaaef55c82011-04-15 21:00:26 +00002930 SmallVectorImpl<SDValue> &InVals)
Akira Hatanakae2489122011-04-15 21:51:11 +00002931 const {
Bruno Cardoso Lopesa01ede22008-08-04 07:12:52 +00002932 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002933 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopes14033fb2007-08-28 05:08:16 +00002934 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002935
Dan Gohman31ae5862010-04-17 14:41:14 +00002936 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002937
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002938 // Used with vargs to acumulate store chains.
2939 std::vector<SDValue> OutChains;
2940
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002941 // Assign locations to all of the incoming arguments.
2942 SmallVector<CCValAssign, 16> ArgLocs;
Daniel Sanders23e98772014-11-02 16:09:29 +00002943 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
2944 *DAG.getContext());
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002945 CCInfo.AllocateStack(ABI.GetCalleeAllocdArgSizeInBytes(CallConv), 1);
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002946 Function::const_arg_iterator FuncArg =
2947 DAG.getMachineFunction().getFunction()->arg_begin();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002948
Daniel Sandersb70e27c2014-11-06 16:36:30 +00002949 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips_FixedArg);
Akira Hatanaka4866fe12012-10-30 19:37:25 +00002950 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(),
Daniel Sanders23e98772014-11-02 16:09:29 +00002951 CCInfo.getInRegsParamsCount() > 0);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002952
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00002953 unsigned CurArgIdx = 0;
Daniel Sanders23e98772014-11-02 16:09:29 +00002954 CCInfo.rewindByValRegsInfo();
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002955
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00002956 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002957 CCValAssign &VA = ArgLocs[i];
Andrew Trick05938a52015-02-16 18:10:47 +00002958 if (Ins[i].isOrigArg()) {
2959 std::advance(FuncArg, Ins[i].getOrigArgIndex() - CurArgIdx);
2960 CurArgIdx = Ins[i].getOrigArgIndex();
2961 }
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002962 EVT ValVT = VA.getValVT();
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002963 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2964 bool IsRegLoc = VA.isRegLoc();
2965
2966 if (Flags.isByVal()) {
Andrew Trick05938a52015-02-16 18:10:47 +00002967 assert(Ins[i].isOrigArg() && "Byval arguments cannot be implicit");
Daniel Sanders23e98772014-11-02 16:09:29 +00002968 unsigned FirstByValReg, LastByValReg;
2969 unsigned ByValIdx = CCInfo.getInRegsParamsProcessed();
2970 CCInfo.getInRegsParamInfo(ByValIdx, FirstByValReg, LastByValReg);
2971
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002972 assert(Flags.getByValSize() &&
2973 "ByVal args of size 0 should have been ignored by front-end.");
Daniel Sanders23e98772014-11-02 16:09:29 +00002974 assert(ByValIdx < CCInfo.getInRegsParamsCount());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002975 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002976 FirstByValReg, LastByValReg, VA, CCInfo);
Daniel Sanders23e98772014-11-02 16:09:29 +00002977 CCInfo.nextInRegsParam();
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002978 continue;
2979 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002980
2981 // Arguments stored on registers
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002982 if (IsRegLoc) {
Akira Hatanaka7d822522013-10-28 21:21:36 +00002983 MVT RegVT = VA.getLocVT();
Akira Hatanakacb4a1a82011-05-24 00:23:52 +00002984 unsigned ArgReg = VA.getLocReg();
Akira Hatanaka7d822522013-10-28 21:21:36 +00002985 const TargetRegisterClass *RC = getRegClassFor(RegVT);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002986
Wesley Peck527da1b2010-11-23 03:31:01 +00002987 // Transform the arguments stored on
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002988 // physical registers into virtual ones
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002989 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC);
2990 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
Wesley Peck527da1b2010-11-23 03:31:01 +00002991
Daniel Sandersc43cda82014-11-07 16:54:21 +00002992 ArgValue = UnpackFromArgumentSlot(ArgValue, VA, Ins[i].ArgVT, DL, DAG);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002993
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002994 // Handle floating point arguments passed in integer registers and
2995 // long double arguments passed in floating point registers.
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002996 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002997 (RegVT == MVT::i64 && ValVT == MVT::f64) ||
2998 (RegVT == MVT::f64 && ValVT == MVT::i64))
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002999 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
Eric Christopher96e72c62015-01-29 23:27:36 +00003000 else if (ABI.IsO32() && RegVT == MVT::i32 &&
Eric Christopherbf33a3c2014-07-02 23:18:40 +00003001 ValVT == MVT::f64) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003002 unsigned Reg2 = addLiveIn(DAG.getMachineFunction(),
Akira Hatanaka104b7e32011-10-28 19:55:48 +00003003 getNextIntArgReg(ArgReg), RC);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003004 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT);
Eric Christopher1c29a652014-07-18 22:55:25 +00003005 if (!Subtarget.isLittle())
Akira Hatanaka104b7e32011-10-28 19:55:48 +00003006 std::swap(ArgValue, ArgValue2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003007 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64,
Akira Hatanaka104b7e32011-10-28 19:55:48 +00003008 ArgValue, ArgValue2);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00003009 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003010
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003011 InVals.push_back(ArgValue);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003012 } else { // VA.isRegLoc()
Daniel Sandersc43cda82014-11-07 16:54:21 +00003013 MVT LocVT = VA.getLocVT();
3014
Eric Christopher96e72c62015-01-29 23:27:36 +00003015 if (ABI.IsO32()) {
Daniel Sandersc43cda82014-11-07 16:54:21 +00003016 // We ought to be able to use LocVT directly but O32 sets it to i32
3017 // when allocating floating point values to integer registers.
3018 // This shouldn't influence how we load the value into registers unless
3019 // we are targetting softfloat.
Eric Christophere8ae3e32015-05-07 23:10:21 +00003020 if (VA.getValVT().isFloatingPoint() && !Subtarget.useSoftFloat())
Daniel Sandersc43cda82014-11-07 16:54:21 +00003021 LocVT = VA.getValVT();
3022 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003023
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003024 // sanity check
3025 assert(VA.isMemLoc());
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003026
Wesley Peck527da1b2010-11-23 03:31:01 +00003027 // The stack pointer offset is relative to the caller stack frame.
Daniel Sandersc43cda82014-11-07 16:54:21 +00003028 int FI = MFI->CreateFixedObject(LocVT.getSizeInBits() / 8,
Akira Hatanakacb4a1a82011-05-24 00:23:52 +00003029 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003030
3031 // Create load nodes to retrieve arguments from the stack
Akira Hatanakaac8c6692012-10-27 00:29:43 +00003032 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Daniel Sandersc43cda82014-11-07 16:54:21 +00003033 SDValue ArgValue = DAG.getLoad(LocVT, DL, Chain, FIN,
3034 MachinePointerInfo::getFixedStack(FI),
3035 false, false, false, 0);
3036 OutChains.push_back(ArgValue.getValue(1));
3037
3038 ArgValue = UnpackFromArgumentSlot(ArgValue, VA, Ins[i].ArgVT, DL, DAG);
3039
3040 InVals.push_back(ArgValue);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003041 }
Reid Kleckner7a59e082014-05-12 22:01:27 +00003042 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003043
Reid Kleckner7a59e082014-05-12 22:01:27 +00003044 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Reid Kleckner79418562014-05-09 22:32:13 +00003045 // The mips ABIs for returning structs by value requires that we copy
3046 // the sret argument into $v0 for the return. Save the argument into
3047 // a virtual register so that we can access it from the return points.
Reid Kleckner7a59e082014-05-12 22:01:27 +00003048 if (Ins[i].Flags.isSRet()) {
Reid Kleckner79418562014-05-09 22:32:13 +00003049 unsigned Reg = MipsFI->getSRetReturnReg();
3050 if (!Reg) {
3051 Reg = MF.getRegInfo().createVirtualRegister(
Eric Christopher96e72c62015-01-29 23:27:36 +00003052 getRegClassFor(ABI.IsN64() ? MVT::i64 : MVT::i32));
Reid Kleckner79418562014-05-09 22:32:13 +00003053 MipsFI->setSRetReturnReg(Reg);
3054 }
Reid Kleckner7a59e082014-05-12 22:01:27 +00003055 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[i]);
Reid Kleckner79418562014-05-09 22:32:13 +00003056 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
Reid Kleckner7a59e082014-05-12 22:01:27 +00003057 break;
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003058 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003059 }
3060
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003061 if (IsVarArg)
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003062 writeVarArgRegs(OutChains, Chain, DL, DAG, CCInfo);
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003063
Wesley Peck527da1b2010-11-23 03:31:01 +00003064 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003065 // the size of Ins and InVals. This only happens when on varg functions
3066 if (!OutChains.empty()) {
3067 OutChains.push_back(Chain);
Craig Topper48d114b2014-04-26 18:35:24 +00003068 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003069 }
3070
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003071 return Chain;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003072}
3073
Akira Hatanakae2489122011-04-15 21:51:11 +00003074//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003075// Return Value Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00003076//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003077
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003078bool
3079MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003080 MachineFunction &MF, bool IsVarArg,
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003081 const SmallVectorImpl<ISD::OutputArg> &Outs,
3082 LLVMContext &Context) const {
3083 SmallVector<CCValAssign, 16> RVLocs;
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003084 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003085 return CCInfo.CheckReturn(Outs, RetCC_Mips);
3086}
3087
Petar Jovanovic5b436222015-03-23 12:28:13 +00003088bool
3089MipsTargetLowering::shouldSignExtendTypeInLibCall(EVT Type, bool IsSigned) const {
Eric Christophere8ae3e32015-05-07 23:10:21 +00003090 if (Subtarget.hasMips3() && Subtarget.useSoftFloat()) {
Petar Jovanovic5b436222015-03-23 12:28:13 +00003091 if (Type == MVT::i32)
3092 return true;
3093 }
3094 return IsSigned;
3095}
3096
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003097SDValue
3098MipsTargetLowering::LowerReturn(SDValue Chain,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003099 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003100 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003101 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003102 SDLoc DL, SelectionDAG &DAG) const {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003103 // CCValAssign - represent the assignment of
3104 // the return value to a location
3105 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003106 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003107
3108 // CCState - Info about the registers and stack slot.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003109 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, *DAG.getContext());
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003110
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003111 // Analyze return values.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003112 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003113
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003114 SDValue Flag;
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003115 SmallVector<SDValue, 4> RetOps(1, Chain);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003116
3117 // Copy the result values into the output registers.
3118 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003119 SDValue Val = OutVals[i];
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003120 CCValAssign &VA = RVLocs[i];
3121 assert(VA.isRegLoc() && "Can only return in registers!");
Daniel Sandersae275e32014-09-25 12:15:05 +00003122 bool UseUpperBits = false;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003123
Daniel Sandersae275e32014-09-25 12:15:05 +00003124 switch (VA.getLocInfo()) {
3125 default:
3126 llvm_unreachable("Unknown loc info!");
3127 case CCValAssign::Full:
3128 break;
3129 case CCValAssign::BCvt:
3130 Val = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Val);
3131 break;
3132 case CCValAssign::AExtUpper:
3133 UseUpperBits = true;
3134 // Fallthrough
3135 case CCValAssign::AExt:
3136 Val = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Val);
3137 break;
3138 case CCValAssign::ZExtUpper:
3139 UseUpperBits = true;
3140 // Fallthrough
3141 case CCValAssign::ZExt:
3142 Val = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Val);
3143 break;
3144 case CCValAssign::SExtUpper:
3145 UseUpperBits = true;
3146 // Fallthrough
3147 case CCValAssign::SExt:
3148 Val = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Val);
3149 break;
3150 }
3151
3152 if (UseUpperBits) {
3153 unsigned ValSizeInBits = Outs[i].ArgVT.getSizeInBits();
3154 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
3155 Val = DAG.getNode(
3156 ISD::SHL, DL, VA.getLocVT(), Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003157 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
Daniel Sandersae275e32014-09-25 12:15:05 +00003158 }
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003159
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003160 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003161
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003162 // Guarantee that all emitted copies are stuck together with flags.
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003163 Flag = Chain.getValue(1);
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003164 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003165 }
3166
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003167 // The mips ABIs for returning structs by value requires that we copy
3168 // the sret argument into $v0 for the return. We saved the argument into
3169 // a virtual register in the entry block, so now we copy the value out
3170 // and into $v0.
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003171 if (MF.getFunction()->hasStructRetAttr()) {
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003172 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3173 unsigned Reg = MipsFI->getSRetReturnReg();
3174
Wesley Peck527da1b2010-11-23 03:31:01 +00003175 if (!Reg)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003176 llvm_unreachable("sret virtual register not created in the entry block");
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003177 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Eric Christopher96e72c62015-01-29 23:27:36 +00003178 unsigned V0 = ABI.IsN64() ? Mips::V0_64 : Mips::V0;
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003179
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003180 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003181 Flag = Chain.getValue(1);
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003182 RetOps.push_back(DAG.getRegister(V0, getPointerTy()));
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003183 }
3184
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003185 RetOps[0] = Chain; // Update chain.
Akira Hatanakaefff7b72012-07-10 00:19:06 +00003186
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003187 // Add the flag if we have it.
3188 if (Flag.getNode())
3189 RetOps.push_back(Flag);
3190
3191 // Return on Mips is always a "jr $ra"
Craig Topper48d114b2014-04-26 18:35:24 +00003192 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, RetOps);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003193}
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003194
Akira Hatanakae2489122011-04-15 21:51:11 +00003195//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003196// Mips Inline Assembly Support
Akira Hatanakae2489122011-04-15 21:51:11 +00003197//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003198
3199/// getConstraintType - Given a constraint letter, return the type of
3200/// constraint it is for this target.
3201MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peck527da1b2010-11-23 03:31:01 +00003202getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003203{
Daniel Sanders8b59af12013-11-12 12:56:01 +00003204 // Mips specific constraints
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003205 // GCC config/mips/constraints.md
3206 //
Wesley Peck527da1b2010-11-23 03:31:01 +00003207 // 'd' : An address register. Equivalent to r
3208 // unless generating MIPS16 code.
3209 // 'y' : Equivalent to r; retained for
3210 // backwards compatibility.
Eric Christophere3c494d2012-05-07 06:25:10 +00003211 // 'c' : A register suitable for use in an indirect
3212 // jump. This will always be $25 for -mabicalls.
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003213 // 'l' : The lo register. 1 word storage.
3214 // 'x' : The hilo register pair. Double word storage.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003215 if (Constraint.size() == 1) {
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003216 switch (Constraint[0]) {
3217 default : break;
Wesley Peck527da1b2010-11-23 03:31:01 +00003218 case 'd':
3219 case 'y':
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003220 case 'f':
Eric Christophere3c494d2012-05-07 06:25:10 +00003221 case 'c':
Eric Christopher9c492e62012-05-07 06:25:15 +00003222 case 'l':
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003223 case 'x':
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003224 return C_RegisterClass;
Jack Carter0e149b02013-03-04 21:33:15 +00003225 case 'R':
3226 return C_Memory;
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003227 }
3228 }
Daniel Sandersa73d8fe2015-03-24 11:26:34 +00003229
3230 if (Constraint == "ZC")
3231 return C_Memory;
3232
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003233 return TargetLowering::getConstraintType(Constraint);
3234}
3235
John Thompsone8360b72010-10-29 17:29:13 +00003236/// Examine constraint type and operand type and determine a weight value.
3237/// This object must already have been set up with the operand type
3238/// and the current alternative constraint selected.
3239TargetLowering::ConstraintWeight
3240MipsTargetLowering::getSingleConstraintMatchWeight(
3241 AsmOperandInfo &info, const char *constraint) const {
3242 ConstraintWeight weight = CW_Invalid;
3243 Value *CallOperandVal = info.CallOperandVal;
3244 // If we don't have a value, we can't do a match,
3245 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +00003246 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +00003247 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +00003248 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +00003249 // Look at the constraint type.
3250 switch (*constraint) {
3251 default:
3252 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
3253 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00003254 case 'd':
3255 case 'y':
John Thompsone8360b72010-10-29 17:29:13 +00003256 if (type->isIntegerTy())
3257 weight = CW_Register;
3258 break;
Daniel Sanders8b59af12013-11-12 12:56:01 +00003259 case 'f': // FPU or MSA register
Eric Christopher1c29a652014-07-18 22:55:25 +00003260 if (Subtarget.hasMSA() && type->isVectorTy() &&
Daniel Sanders8b59af12013-11-12 12:56:01 +00003261 cast<VectorType>(type)->getBitWidth() == 128)
3262 weight = CW_Register;
3263 else if (type->isFloatTy())
John Thompsone8360b72010-10-29 17:29:13 +00003264 weight = CW_Register;
3265 break;
Eric Christophere3c494d2012-05-07 06:25:10 +00003266 case 'c': // $25 for indirect jumps
Eric Christopher9c492e62012-05-07 06:25:15 +00003267 case 'l': // lo register
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003268 case 'x': // hilo register pair
Daniel Sanders8b59af12013-11-12 12:56:01 +00003269 if (type->isIntegerTy())
Eric Christophere3c494d2012-05-07 06:25:10 +00003270 weight = CW_SpecificReg;
Daniel Sanders8b59af12013-11-12 12:56:01 +00003271 break;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003272 case 'I': // signed 16 bit immediate
Eric Christopher7201e1b2012-05-07 03:13:42 +00003273 case 'J': // integer zero
Eric Christopher3ff88a02012-05-07 05:46:29 +00003274 case 'K': // unsigned 16 bit immediate
Eric Christopher1109b342012-05-07 05:46:37 +00003275 case 'L': // signed 32 bit immediate where lower 16 bits are 0
Eric Christophere07aa432012-05-07 05:46:43 +00003276 case 'N': // immediate in the range of -65535 to -1 (inclusive)
Eric Christopher470578a2012-05-07 05:46:48 +00003277 case 'O': // signed 15 bit immediate (+- 16383)
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003278 case 'P': // immediate in the range of 65535 to 1 (inclusive)
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003279 if (isa<ConstantInt>(CallOperandVal))
3280 weight = CW_Constant;
3281 break;
Jack Carter0e149b02013-03-04 21:33:15 +00003282 case 'R':
3283 weight = CW_Memory;
3284 break;
John Thompsone8360b72010-10-29 17:29:13 +00003285 }
3286 return weight;
3287}
3288
Akira Hatanaka7473b472013-08-14 00:21:25 +00003289/// This is a helper function to parse a physical register string and split it
3290/// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag
3291/// that is returned indicates whether parsing was successful. The second flag
3292/// is true if the numeric part exists.
3293static std::pair<bool, bool>
Craig Topper6dc4a8bc2014-08-30 16:48:02 +00003294parsePhysicalReg(StringRef C, std::string &Prefix,
Akira Hatanaka7473b472013-08-14 00:21:25 +00003295 unsigned long long &Reg) {
3296 if (C.front() != '{' || C.back() != '}')
3297 return std::make_pair(false, false);
3298
3299 // Search for the first numeric character.
3300 StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1;
3301 I = std::find_if(B, E, std::ptr_fun(isdigit));
3302
3303 Prefix.assign(B, I - B);
3304
3305 // The second flag is set to false if no numeric characters were found.
3306 if (I == E)
3307 return std::make_pair(true, false);
3308
3309 // Parse the numeric characters.
3310 return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg),
3311 true);
3312}
3313
3314std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering::
Craig Topper6dc4a8bc2014-08-30 16:48:02 +00003315parseRegForInlineAsmConstraint(StringRef C, MVT VT) const {
Eric Christopherd9134482014-08-04 21:25:23 +00003316 const TargetRegisterInfo *TRI =
Eric Christopher96e72c62015-01-29 23:27:36 +00003317 Subtarget.getRegisterInfo();
Akira Hatanaka7473b472013-08-14 00:21:25 +00003318 const TargetRegisterClass *RC;
3319 std::string Prefix;
3320 unsigned long long Reg;
3321
3322 std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg);
3323
3324 if (!R.first)
Craig Topper062a2ba2014-04-25 05:30:21 +00003325 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003326
3327 if ((Prefix == "hi" || Prefix == "lo")) { // Parse hi/lo.
3328 // No numeric characters follow "hi" or "lo".
3329 if (R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003330 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003331
3332 RC = TRI->getRegClass(Prefix == "hi" ?
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00003333 Mips::HI32RegClassID : Mips::LO32RegClassID);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003334 return std::make_pair(*(RC->begin()), RC);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003335 } else if (Prefix.compare(0, 4, "$msa") == 0) {
3336 // Parse $msa(ir|csr|access|save|modify|request|map|unmap)
3337
3338 // No numeric characters follow the name.
3339 if (R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003340 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003341
3342 Reg = StringSwitch<unsigned long long>(Prefix)
3343 .Case("$msair", Mips::MSAIR)
3344 .Case("$msacsr", Mips::MSACSR)
3345 .Case("$msaaccess", Mips::MSAAccess)
3346 .Case("$msasave", Mips::MSASave)
3347 .Case("$msamodify", Mips::MSAModify)
3348 .Case("$msarequest", Mips::MSARequest)
3349 .Case("$msamap", Mips::MSAMap)
3350 .Case("$msaunmap", Mips::MSAUnmap)
3351 .Default(0);
3352
3353 if (!Reg)
Craig Topper062a2ba2014-04-25 05:30:21 +00003354 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003355
3356 RC = TRI->getRegClass(Mips::MSACtrlRegClassID);
3357 return std::make_pair(Reg, RC);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003358 }
3359
3360 if (!R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003361 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003362
3363 if (Prefix == "$f") { // Parse $f0-$f31.
3364 // If the size of FP registers is 64-bit or Reg is an even number, select
3365 // the 64-bit register class. Otherwise, select the 32-bit register class.
3366 if (VT == MVT::Other)
Eric Christopher1c29a652014-07-18 22:55:25 +00003367 VT = (Subtarget.isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32;
Akira Hatanaka7473b472013-08-14 00:21:25 +00003368
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003369 RC = getRegClassFor(VT);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003370
3371 if (RC == &Mips::AFGR64RegClass) {
3372 assert(Reg % 2 == 0);
3373 Reg >>= 1;
3374 }
Daniel Sanders8b59af12013-11-12 12:56:01 +00003375 } else if (Prefix == "$fcc") // Parse $fcc0-$fcc7.
Akira Hatanaka7473b472013-08-14 00:21:25 +00003376 RC = TRI->getRegClass(Mips::FCCRegClassID);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003377 else if (Prefix == "$w") { // Parse $w0-$w31.
3378 RC = getRegClassFor((VT == MVT::Other) ? MVT::v16i8 : VT);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003379 } else { // Parse $0-$31.
3380 assert(Prefix == "$");
3381 RC = getRegClassFor((VT == MVT::Other) ? MVT::i32 : VT);
3382 }
3383
3384 assert(Reg < RC->getNumRegs());
3385 return std::make_pair(*(RC->begin() + Reg), RC);
3386}
3387
Eric Christophereaf77dc2011-06-29 19:33:04 +00003388/// Given a register class constraint, like 'r', if this corresponds directly
3389/// to an LLVM register class, return a register of 0 and the register class
3390/// pointer.
Eric Christopher11e4df72015-02-26 22:38:43 +00003391std::pair<unsigned, const TargetRegisterClass *>
3392MipsTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
3393 const std::string &Constraint,
3394 MVT VT) const {
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003395 if (Constraint.size() == 1) {
3396 switch (Constraint[0]) {
Eric Christopher9519c082011-06-29 19:04:31 +00003397 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
3398 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003399 case 'r':
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003400 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
Eric Christopher1c29a652014-07-18 22:55:25 +00003401 if (Subtarget.inMips16Mode())
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003402 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003403 return std::make_pair(0U, &Mips::GPR32RegClass);
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003404 }
Eric Christopher1c29a652014-07-18 22:55:25 +00003405 if (VT == MVT::i64 && !Subtarget.isGP64bit())
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003406 return std::make_pair(0U, &Mips::GPR32RegClass);
Eric Christopher1c29a652014-07-18 22:55:25 +00003407 if (VT == MVT::i64 && Subtarget.isGP64bit())
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003408 return std::make_pair(0U, &Mips::GPR64RegClass);
Eric Christopher58daf042012-05-07 03:13:22 +00003409 // This will generate an error message
Craig Topper062a2ba2014-04-25 05:30:21 +00003410 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003411 case 'f': // FPU or MSA register
3412 if (VT == MVT::v16i8)
3413 return std::make_pair(0U, &Mips::MSA128BRegClass);
3414 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
3415 return std::make_pair(0U, &Mips::MSA128HRegClass);
3416 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
3417 return std::make_pair(0U, &Mips::MSA128WRegClass);
3418 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
3419 return std::make_pair(0U, &Mips::MSA128DRegClass);
3420 else if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00003421 return std::make_pair(0U, &Mips::FGR32RegClass);
Eric Christopher1c29a652014-07-18 22:55:25 +00003422 else if ((VT == MVT::f64) && (!Subtarget.isSingleFloat())) {
3423 if (Subtarget.isFP64bit())
Craig Topperc7242e02012-04-20 07:30:17 +00003424 return std::make_pair(0U, &Mips::FGR64RegClass);
3425 return std::make_pair(0U, &Mips::AFGR64RegClass);
Akira Hatanakac669d7a2012-01-04 02:45:01 +00003426 }
Eric Christophere3c494d2012-05-07 06:25:10 +00003427 break;
3428 case 'c': // register suitable for indirect jump
3429 if (VT == MVT::i32)
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003430 return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass);
Eric Christophere3c494d2012-05-07 06:25:10 +00003431 assert(VT == MVT::i64 && "Unexpected type.");
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003432 return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass);
Eric Christopher9c492e62012-05-07 06:25:15 +00003433 case 'l': // register suitable for indirect jump
3434 if (VT == MVT::i32)
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00003435 return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass);
3436 return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass);
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003437 case 'x': // register suitable for indirect jump
3438 // Fixme: Not triggering the use of both hi and low
3439 // This will generate an error message
Craig Topper062a2ba2014-04-25 05:30:21 +00003440 return std::make_pair(0U, nullptr);
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003441 }
3442 }
Akira Hatanaka7473b472013-08-14 00:21:25 +00003443
3444 std::pair<unsigned, const TargetRegisterClass *> R;
3445 R = parseRegForInlineAsmConstraint(Constraint, VT);
3446
3447 if (R.second)
3448 return R;
3449
Eric Christopher11e4df72015-02-26 22:38:43 +00003450 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003451}
3452
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003453/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3454/// vector. If it is invalid, don't add anything to Ops.
3455void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3456 std::string &Constraint,
3457 std::vector<SDValue>&Ops,
3458 SelectionDAG &DAG) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003459 SDLoc DL(Op);
Craig Topper062a2ba2014-04-25 05:30:21 +00003460 SDValue Result;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003461
3462 // Only support length 1 constraints for now.
3463 if (Constraint.length() > 1) return;
3464
3465 char ConstraintLetter = Constraint[0];
3466 switch (ConstraintLetter) {
3467 default: break; // This will fall through to the generic implementation
3468 case 'I': // Signed 16 bit constant
3469 // If this fails, the parent routine will give an error
3470 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3471 EVT Type = Op.getValueType();
3472 int64_t Val = C->getSExtValue();
3473 if (isInt<16>(Val)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003474 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003475 break;
3476 }
3477 }
3478 return;
Eric Christopher7201e1b2012-05-07 03:13:42 +00003479 case 'J': // integer zero
3480 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3481 EVT Type = Op.getValueType();
3482 int64_t Val = C->getZExtValue();
3483 if (Val == 0) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003484 Result = DAG.getTargetConstant(0, DL, Type);
Eric Christopher7201e1b2012-05-07 03:13:42 +00003485 break;
3486 }
3487 }
3488 return;
Eric Christopher3ff88a02012-05-07 05:46:29 +00003489 case 'K': // unsigned 16 bit immediate
3490 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3491 EVT Type = Op.getValueType();
3492 uint64_t Val = (uint64_t)C->getZExtValue();
3493 if (isUInt<16>(Val)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003494 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopher3ff88a02012-05-07 05:46:29 +00003495 break;
3496 }
3497 }
3498 return;
Eric Christopher1109b342012-05-07 05:46:37 +00003499 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3500 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3501 EVT Type = Op.getValueType();
3502 int64_t Val = C->getSExtValue();
3503 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003504 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopher1109b342012-05-07 05:46:37 +00003505 break;
3506 }
3507 }
3508 return;
Eric Christophere07aa432012-05-07 05:46:43 +00003509 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3510 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3511 EVT Type = Op.getValueType();
3512 int64_t Val = C->getSExtValue();
3513 if ((Val >= -65535) && (Val <= -1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003514 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christophere07aa432012-05-07 05:46:43 +00003515 break;
3516 }
3517 }
3518 return;
Eric Christopher470578a2012-05-07 05:46:48 +00003519 case 'O': // signed 15 bit immediate
3520 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3521 EVT Type = Op.getValueType();
3522 int64_t Val = C->getSExtValue();
3523 if ((isInt<15>(Val))) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003524 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopher470578a2012-05-07 05:46:48 +00003525 break;
3526 }
3527 }
3528 return;
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003529 case 'P': // immediate in the range of 1 to 65535 (inclusive)
3530 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3531 EVT Type = Op.getValueType();
3532 int64_t Val = C->getSExtValue();
3533 if ((Val <= 65535) && (Val >= 1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003534 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003535 break;
3536 }
3537 }
3538 return;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003539 }
3540
3541 if (Result.getNode()) {
3542 Ops.push_back(Result);
3543 return;
3544 }
3545
3546 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3547}
3548
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003549bool MipsTargetLowering::isLegalAddressingMode(const AddrMode &AM,
3550 Type *Ty) const {
Akira Hatanakaef839192012-11-17 00:25:41 +00003551 // No global is ever allowed as a base.
3552 if (AM.BaseGV)
3553 return false;
3554
3555 switch (AM.Scale) {
3556 case 0: // "r+i" or just "i", depending on HasBaseReg.
3557 break;
3558 case 1:
3559 if (!AM.HasBaseReg) // allow "r+i".
3560 break;
3561 return false; // disallow "r+r" or "r+r+i".
3562 default:
3563 return false;
3564 }
3565
3566 return true;
3567}
3568
3569bool
Dan Gohman2fe6bee2008-10-18 02:06:02 +00003570MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3571 // The Mips target isn't yet aware of offsets.
3572 return false;
3573}
Evan Cheng16993aa2009-10-27 19:56:55 +00003574
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003575EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003576 unsigned SrcAlign,
3577 bool IsMemset, bool ZeroMemset,
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003578 bool MemcpyStrSrc,
3579 MachineFunction &MF) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003580 if (Subtarget.hasMips64())
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003581 return MVT::i64;
3582
3583 return MVT::i32;
3584}
3585
Evan Cheng83896a52009-10-28 01:43:28 +00003586bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
3587 if (VT != MVT::f32 && VT != MVT::f64)
3588 return false;
Bruno Cardoso Lopesb02a9df2011-01-18 19:41:41 +00003589 if (Imm.isNegZero())
3590 return false;
Evan Cheng16993aa2009-10-27 19:56:55 +00003591 return Imm.isZero();
3592}
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003593
3594unsigned MipsTargetLowering::getJumpTableEncoding() const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003595 if (ABI.IsN64())
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003596 return MachineJumpTableInfo::EK_GPRel64BlockAddress;
Jia Liuf54f60f2012-02-28 07:46:26 +00003597
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003598 return TargetLowering::getJumpTableEncoding();
3599}
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003600
Daniel Sandersf43e6872014-11-01 18:44:56 +00003601void MipsTargetLowering::copyByValRegs(
3602 SDValue Chain, SDLoc DL, std::vector<SDValue> &OutChains, SelectionDAG &DAG,
3603 const ISD::ArgFlagsTy &Flags, SmallVectorImpl<SDValue> &InVals,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003604 const Argument *FuncArg, unsigned FirstReg, unsigned LastReg,
3605 const CCValAssign &VA, MipsCCState &State) const {
Akira Hatanaka25dad192012-10-27 00:10:18 +00003606 MachineFunction &MF = DAG.getMachineFunction();
3607 MachineFrameInfo *MFI = MF.getFrameInfo();
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003608 unsigned GPRSizeInBytes = Subtarget.getGPRSizeInBytes();
Daniel Sanders23e98772014-11-02 16:09:29 +00003609 unsigned NumRegs = LastReg - FirstReg;
3610 unsigned RegAreaSize = NumRegs * GPRSizeInBytes;
Akira Hatanaka25dad192012-10-27 00:10:18 +00003611 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
3612 int FrameObjOffset;
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003613 ArrayRef<MCPhysReg> ByValArgRegs = ABI.GetByValArgRegs();
Akira Hatanaka25dad192012-10-27 00:10:18 +00003614
3615 if (RegAreaSize)
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003616 FrameObjOffset =
3617 (int)ABI.GetCalleeAllocdArgSizeInBytes(State.getCallingConv()) -
3618 (int)((ByValArgRegs.size() - FirstReg) * GPRSizeInBytes);
Akira Hatanaka25dad192012-10-27 00:10:18 +00003619 else
Daniel Sandersf43e6872014-11-01 18:44:56 +00003620 FrameObjOffset = VA.getLocMemOffset();
Akira Hatanaka25dad192012-10-27 00:10:18 +00003621
3622 // Create frame object.
3623 EVT PtrTy = getPointerTy();
3624 int FI = MFI->CreateFixedObject(FrameObjSize, FrameObjOffset, true);
3625 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
3626 InVals.push_back(FIN);
3627
Daniel Sanders23e98772014-11-02 16:09:29 +00003628 if (!NumRegs)
Akira Hatanaka25dad192012-10-27 00:10:18 +00003629 return;
3630
3631 // Copy arg registers.
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003632 MVT RegTy = MVT::getIntegerVT(GPRSizeInBytes * 8);
Akira Hatanaka25dad192012-10-27 00:10:18 +00003633 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3634
Daniel Sanders23e98772014-11-02 16:09:29 +00003635 for (unsigned I = 0; I < NumRegs; ++I) {
Daniel Sandersd7eba312014-11-07 12:21:37 +00003636 unsigned ArgReg = ByValArgRegs[FirstReg + I];
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003637 unsigned VReg = addLiveIn(MF, ArgReg, RC);
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003638 unsigned Offset = I * GPRSizeInBytes;
Akira Hatanaka25dad192012-10-27 00:10:18 +00003639 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003640 DAG.getConstant(Offset, DL, PtrTy));
Akira Hatanaka25dad192012-10-27 00:10:18 +00003641 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
3642 StorePtr, MachinePointerInfo(FuncArg, Offset),
3643 false, false, 0);
3644 OutChains.push_back(Store);
3645 }
3646}
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003647
3648// Copy byVal arg to registers and stack.
Daniel Sandersf43e6872014-11-01 18:44:56 +00003649void MipsTargetLowering::passByValArg(
3650 SDValue Chain, SDLoc DL,
3651 std::deque<std::pair<unsigned, SDValue>> &RegsToPass,
3652 SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003653 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg, unsigned FirstReg,
3654 unsigned LastReg, const ISD::ArgFlagsTy &Flags, bool isLittle,
3655 const CCValAssign &VA) const {
Daniel Sandersac272632014-05-23 13:18:02 +00003656 unsigned ByValSizeInBytes = Flags.getByValSize();
3657 unsigned OffsetInBytes = 0; // From beginning of struct
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003658 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
Daniel Sandersac272632014-05-23 13:18:02 +00003659 unsigned Alignment = std::min(Flags.getByValAlign(), RegSizeInBytes);
3660 EVT PtrTy = getPointerTy(), RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
Daniel Sanders23e98772014-11-02 16:09:29 +00003661 unsigned NumRegs = LastReg - FirstReg;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003662
Daniel Sanders23e98772014-11-02 16:09:29 +00003663 if (NumRegs) {
Eric Christopher96e72c62015-01-29 23:27:36 +00003664 const ArrayRef<MCPhysReg> ArgRegs = ABI.GetByValArgRegs();
Daniel Sanders23e98772014-11-02 16:09:29 +00003665 bool LeftoverBytes = (NumRegs * RegSizeInBytes > ByValSizeInBytes);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003666 unsigned I = 0;
3667
3668 // Copy words to registers.
Daniel Sanders23e98772014-11-02 16:09:29 +00003669 for (; I < NumRegs - LeftoverBytes; ++I, OffsetInBytes += RegSizeInBytes) {
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003670 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003671 DAG.getConstant(OffsetInBytes, DL, PtrTy));
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003672 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
3673 MachinePointerInfo(), false, false, false,
3674 Alignment);
3675 MemOpChains.push_back(LoadVal.getValue(1));
Daniel Sanders23e98772014-11-02 16:09:29 +00003676 unsigned ArgReg = ArgRegs[FirstReg + I];
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003677 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
3678 }
3679
3680 // Return if the struct has been fully copied.
Daniel Sandersac272632014-05-23 13:18:02 +00003681 if (ByValSizeInBytes == OffsetInBytes)
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003682 return;
3683
3684 // Copy the remainder of the byval argument with sub-word loads and shifts.
3685 if (LeftoverBytes) {
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003686 SDValue Val;
3687
Daniel Sandersac272632014-05-23 13:18:02 +00003688 for (unsigned LoadSizeInBytes = RegSizeInBytes / 2, TotalBytesLoaded = 0;
3689 OffsetInBytes < ByValSizeInBytes; LoadSizeInBytes /= 2) {
3690 unsigned RemainingSizeInBytes = ByValSizeInBytes - OffsetInBytes;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003691
Daniel Sandersac272632014-05-23 13:18:02 +00003692 if (RemainingSizeInBytes < LoadSizeInBytes)
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003693 continue;
3694
3695 // Load subword.
3696 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003697 DAG.getConstant(OffsetInBytes, DL,
3698 PtrTy));
Daniel Sandersac272632014-05-23 13:18:02 +00003699 SDValue LoadVal = DAG.getExtLoad(
3700 ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr, MachinePointerInfo(),
Louis Gerbarg67474e32014-07-31 21:45:05 +00003701 MVT::getIntegerVT(LoadSizeInBytes * 8), false, false, false,
3702 Alignment);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003703 MemOpChains.push_back(LoadVal.getValue(1));
3704
3705 // Shift the loaded value.
3706 unsigned Shamt;
3707
3708 if (isLittle)
Daniel Sandersac272632014-05-23 13:18:02 +00003709 Shamt = TotalBytesLoaded * 8;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003710 else
Daniel Sandersac272632014-05-23 13:18:02 +00003711 Shamt = (RegSizeInBytes - (TotalBytesLoaded + LoadSizeInBytes)) * 8;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003712
3713 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003714 DAG.getConstant(Shamt, DL, MVT::i32));
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003715
3716 if (Val.getNode())
3717 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
3718 else
3719 Val = Shift;
3720
Daniel Sandersac272632014-05-23 13:18:02 +00003721 OffsetInBytes += LoadSizeInBytes;
3722 TotalBytesLoaded += LoadSizeInBytes;
3723 Alignment = std::min(Alignment, LoadSizeInBytes);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003724 }
3725
Daniel Sanders23e98772014-11-02 16:09:29 +00003726 unsigned ArgReg = ArgRegs[FirstReg + I];
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003727 RegsToPass.push_back(std::make_pair(ArgReg, Val));
3728 return;
3729 }
3730 }
3731
3732 // Copy remainder of byval arg to it with memcpy.
Daniel Sandersac272632014-05-23 13:18:02 +00003733 unsigned MemCpySize = ByValSizeInBytes - OffsetInBytes;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003734 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003735 DAG.getConstant(OffsetInBytes, DL, PtrTy));
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003736 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003737 DAG.getIntPtrConstant(VA.getLocMemOffset(), DL));
3738 Chain = DAG.getMemcpy(Chain, DL, Dst, Src,
3739 DAG.getConstant(MemCpySize, DL, PtrTy),
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003740 Alignment, /*isVolatile=*/false, /*AlwaysInline=*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00003741 /*isTailCall=*/false,
Nick Lewyckyaad475b2014-04-15 07:22:52 +00003742 MachinePointerInfo(), MachinePointerInfo());
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003743 MemOpChains.push_back(Chain);
3744}
Akira Hatanaka2a134022012-10-27 00:21:13 +00003745
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003746void MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003747 SDValue Chain, SDLoc DL,
3748 SelectionDAG &DAG,
Daniel Sanders853c2432014-11-01 18:13:52 +00003749 CCState &State) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003750 const ArrayRef<MCPhysReg> ArgRegs = ABI.GetVarArgRegs();
Tim Northover3b6b7ca2015-02-21 02:11:17 +00003751 unsigned Idx = State.getFirstUnallocated(ArgRegs);
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003752 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
3753 MVT RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003754 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3755 MachineFunction &MF = DAG.getMachineFunction();
3756 MachineFrameInfo *MFI = MF.getFrameInfo();
3757 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3758
3759 // Offset of the first variable argument from stack pointer.
3760 int VaArgOffset;
3761
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003762 if (ArgRegs.size() == Idx)
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003763 VaArgOffset =
Daniel Sanders853c2432014-11-01 18:13:52 +00003764 RoundUpToAlignment(State.getNextStackOffset(), RegSizeInBytes);
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003765 else {
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003766 VaArgOffset =
3767 (int)ABI.GetCalleeAllocdArgSizeInBytes(State.getCallingConv()) -
3768 (int)(RegSizeInBytes * (ArgRegs.size() - Idx));
3769 }
Akira Hatanaka2a134022012-10-27 00:21:13 +00003770
3771 // Record the frame index of the first variable argument
3772 // which is a value necessary to VASTART.
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003773 int FI = MFI->CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003774 MipsFI->setVarArgsFrameIndex(FI);
3775
3776 // Copy the integer registers that have not been used for argument passing
3777 // to the argument register save area. For O32, the save area is allocated
3778 // in the caller's stack frame, while for N32/64, it is allocated in the
3779 // callee's stack frame.
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003780 for (unsigned I = Idx; I < ArgRegs.size();
3781 ++I, VaArgOffset += RegSizeInBytes) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003782 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003783 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003784 FI = MFI->CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003785 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
3786 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
3787 MachinePointerInfo(), false, false, 0);
Eric Christopher1c29a652014-07-18 22:55:25 +00003788 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(
3789 (Value *)nullptr);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003790 OutChains.push_back(Store);
3791 }
3792}
Daniel Sanders23e98772014-11-02 16:09:29 +00003793
3794void MipsTargetLowering::HandleByVal(CCState *State, unsigned &Size,
3795 unsigned Align) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003796 const TargetFrameLowering *TFL = Subtarget.getFrameLowering();
Daniel Sanders23e98772014-11-02 16:09:29 +00003797
3798 assert(Size && "Byval argument's size shouldn't be 0.");
3799
3800 Align = std::min(Align, TFL->getStackAlignment());
3801
3802 unsigned FirstReg = 0;
3803 unsigned NumRegs = 0;
3804
3805 if (State->getCallingConv() != CallingConv::Fast) {
3806 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
Eric Christopher96e72c62015-01-29 23:27:36 +00003807 const ArrayRef<MCPhysReg> IntArgRegs = ABI.GetByValArgRegs();
Daniel Sanders23e98772014-11-02 16:09:29 +00003808 // FIXME: The O32 case actually describes no shadow registers.
3809 const MCPhysReg *ShadowRegs =
Eric Christopher96e72c62015-01-29 23:27:36 +00003810 ABI.IsO32() ? IntArgRegs.data() : Mips64DPRegs;
Daniel Sanders23e98772014-11-02 16:09:29 +00003811
3812 // We used to check the size as well but we can't do that anymore since
3813 // CCState::HandleByVal() rounds up the size after calling this function.
3814 assert(!(Align % RegSizeInBytes) &&
3815 "Byval argument's alignment should be a multiple of"
3816 "RegSizeInBytes.");
3817
Tim Northover3b6b7ca2015-02-21 02:11:17 +00003818 FirstReg = State->getFirstUnallocated(IntArgRegs);
Daniel Sanders23e98772014-11-02 16:09:29 +00003819
3820 // If Align > RegSizeInBytes, the first arg register must be even.
3821 // FIXME: This condition happens to do the right thing but it's not the
3822 // right way to test it. We want to check that the stack frame offset
3823 // of the register is aligned.
3824 if ((Align > RegSizeInBytes) && (FirstReg % 2)) {
3825 State->AllocateReg(IntArgRegs[FirstReg], ShadowRegs[FirstReg]);
3826 ++FirstReg;
3827 }
3828
3829 // Mark the registers allocated.
3830 Size = RoundUpToAlignment(Size, RegSizeInBytes);
3831 for (unsigned I = FirstReg; Size > 0 && (I < IntArgRegs.size());
3832 Size -= RegSizeInBytes, ++I, ++NumRegs)
3833 State->AllocateReg(IntArgRegs[I], ShadowRegs[I]);
3834 }
3835
3836 State->addInRegsParamInfo(FirstReg, FirstReg + NumRegs);
3837}
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00003838
3839MachineBasicBlock *
3840MipsTargetLowering::emitPseudoSELECT(MachineInstr *MI, MachineBasicBlock *BB,
3841 bool isFPCmp, unsigned Opc) const {
3842 assert(!(Subtarget.hasMips4() || Subtarget.hasMips32()) &&
3843 "Subtarget already supports SELECT nodes with the use of"
3844 "conditional-move instructions.");
3845
3846 const TargetInstrInfo *TII =
Eric Christopher96e72c62015-01-29 23:27:36 +00003847 Subtarget.getInstrInfo();
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00003848 DebugLoc DL = MI->getDebugLoc();
3849
3850 // To "insert" a SELECT instruction, we actually have to insert the
3851 // diamond control-flow pattern. The incoming instruction knows the
3852 // destination vreg to set, the condition code register to branch on, the
3853 // true/false values to select between, and a branch opcode to use.
3854 const BasicBlock *LLVM_BB = BB->getBasicBlock();
3855 MachineFunction::iterator It = BB;
3856 ++It;
3857
3858 // thisMBB:
3859 // ...
3860 // TrueVal = ...
3861 // setcc r1, r2, r3
3862 // bNE r1, r0, copy1MBB
3863 // fallthrough --> copy0MBB
3864 MachineBasicBlock *thisMBB = BB;
3865 MachineFunction *F = BB->getParent();
3866 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
3867 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
3868 F->insert(It, copy0MBB);
3869 F->insert(It, sinkMBB);
3870
3871 // Transfer the remainder of BB and its successor edges to sinkMBB.
3872 sinkMBB->splice(sinkMBB->begin(), BB,
3873 std::next(MachineBasicBlock::iterator(MI)), BB->end());
3874 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
3875
3876 // Next, add the true and fallthrough blocks as its successors.
3877 BB->addSuccessor(copy0MBB);
3878 BB->addSuccessor(sinkMBB);
3879
3880 if (isFPCmp) {
3881 // bc1[tf] cc, sinkMBB
3882 BuildMI(BB, DL, TII->get(Opc))
3883 .addReg(MI->getOperand(1).getReg())
3884 .addMBB(sinkMBB);
3885 } else {
3886 // bne rs, $0, sinkMBB
3887 BuildMI(BB, DL, TII->get(Opc))
3888 .addReg(MI->getOperand(1).getReg())
3889 .addReg(Mips::ZERO)
3890 .addMBB(sinkMBB);
3891 }
3892
3893 // copy0MBB:
3894 // %FalseValue = ...
3895 // # fallthrough to sinkMBB
3896 BB = copy0MBB;
3897
3898 // Update machine-CFG edges
3899 BB->addSuccessor(sinkMBB);
3900
3901 // sinkMBB:
3902 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
3903 // ...
3904 BB = sinkMBB;
3905
3906 BuildMI(*BB, BB->begin(), DL,
3907 TII->get(Mips::PHI), MI->getOperand(0).getReg())
3908 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
3909 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB);
3910
3911 MI->eraseFromParent(); // The pseudo instruction is gone now.
3912
3913 return BB;
3914}
Daniel Sanders1440bb22015-01-09 17:21:30 +00003915
3916// FIXME? Maybe this could be a TableGen attribute on some registers and
3917// this table could be generated automatically from RegInfo.
3918unsigned MipsTargetLowering::getRegisterByName(const char* RegName,
3919 EVT VT) const {
3920 // Named registers is expected to be fairly rare. For now, just support $28
3921 // since the linux kernel uses it.
3922 if (Subtarget.isGP64bit()) {
3923 unsigned Reg = StringSwitch<unsigned>(RegName)
3924 .Case("$28", Mips::GP_64)
3925 .Default(0);
3926 if (Reg)
3927 return Reg;
3928 } else {
3929 unsigned Reg = StringSwitch<unsigned>(RegName)
3930 .Case("$28", Mips::GP)
3931 .Default(0);
3932 if (Reg)
3933 return Reg;
3934 }
3935 report_fatal_error("Invalid register name global variable");
3936}