blob: 026b8cec400eac966cea4cad244ab949b8b02f90 [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"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000025#include "llvm/CodeGen/FunctionLoweringInfo.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000026#include "llvm/CodeGen/MachineFrameInfo.h"
27#include "llvm/CodeGen/MachineFunction.h"
28#include "llvm/CodeGen/MachineInstrBuilder.h"
Eric Christopher79cc1e32014-09-02 22:28:02 +000029#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000030#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000031#include "llvm/CodeGen/SelectionDAGISel.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000032#include "llvm/CodeGen/ValueTypes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000033#include "llvm/IR/CallingConv.h"
34#include "llvm/IR/DerivedTypes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000035#include "llvm/IR/GlobalVariable.h"
Akira Hatanaka90131ac2012-10-19 21:47:33 +000036#include "llvm/Support/CommandLine.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000037#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000038#include "llvm/Support/ErrorHandling.h"
NAKAMURA Takumie30303f2012-04-21 15:31:45 +000039#include "llvm/Support/raw_ostream.h"
Akira Hatanaka7473b472013-08-14 00:21:25 +000040#include <cctype>
NAKAMURA Takumie30303f2012-04-21 15:31:45 +000041
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000042using namespace llvm;
43
Chandler Carruth84e68b22014-04-22 02:41:26 +000044#define DEBUG_TYPE "mips-lower"
45
Akira Hatanaka90131ac2012-10-19 21:47:33 +000046STATISTIC(NumTailCalls, "Number of tail calls");
47
48static cl::opt<bool>
Akira Hatanaka59f299f2012-11-21 20:21:11 +000049LargeGOT("mxgot", cl::Hidden,
50 cl::desc("MIPS: Enable GOT larger than 64k."), cl::init(false));
51
Akira Hatanaka1cb02422013-05-20 18:07:43 +000052static cl::opt<bool>
Akira Hatanakabe76cd02013-05-21 17:17:59 +000053NoZeroDivCheck("mno-check-zero-division", cl::Hidden,
Akira Hatanaka1cb02422013-05-20 18:07:43 +000054 cl::desc("MIPS: Don't trap on integer division by zero."),
55 cl::init(false));
56
Craig Topper840beec2014-04-04 05:16:06 +000057static const MCPhysReg Mips64DPRegs[8] = {
Akira Hatanakaac8c6692012-10-27 00:29:43 +000058 Mips::D12_64, Mips::D13_64, Mips::D14_64, Mips::D15_64,
59 Mips::D16_64, Mips::D17_64, Mips::D18_64, Mips::D19_64
60};
61
Jia Liuf54f60f2012-02-28 07:46:26 +000062// If I is a shifted mask, set the size (Size) and the first bit of the
Akira Hatanaka73d78b72011-08-18 20:07:42 +000063// mask (Pos), and return true.
Jia Liuf54f60f2012-02-28 07:46:26 +000064// For example, if I is 0x003ff800, (Pos, Size) = (11, 11).
Akira Hatanaka0bb60d892013-03-12 00:16:36 +000065static bool isShiftedMask(uint64_t I, uint64_t &Pos, uint64_t &Size) {
Akira Hatanaka20cee2e2011-12-05 21:26:34 +000066 if (!isShiftedMask_64(I))
Akira Hatanaka4c0a7122013-10-07 19:33:02 +000067 return false;
Akira Hatanaka5360f882011-08-17 02:05:42 +000068
Benjamin Kramer5f6a9072015-02-12 15:35:40 +000069 Size = countPopulation(I);
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +000070 Pos = countTrailingZeros(I);
Akira Hatanaka73d78b72011-08-18 20:07:42 +000071 return true;
Akira Hatanaka5360f882011-08-17 02:05:42 +000072}
73
Simon Dardis212cccb2017-06-09 14:37:08 +000074// The MIPS MSA ABI passes vector arguments in the integer register set.
75// The number of integer registers used is dependant on the ABI used.
76MVT MipsTargetLowering::getRegisterTypeForCallingConv(MVT VT) const {
77 if (VT.isVector() && Subtarget.hasMSA())
78 return Subtarget.isABI_O32() ? MVT::i32 : MVT::i64;
79 return MipsTargetLowering::getRegisterType(VT);
80}
81
82MVT MipsTargetLowering::getRegisterTypeForCallingConv(LLVMContext &Context,
83 EVT VT) const {
84 if (VT.isVector()) {
85 if (Subtarget.isABI_O32()) {
86 return MVT::i32;
87 } else {
88 return (VT.getSizeInBits() == 32) ? MVT::i32 : MVT::i64;
89 }
90 }
91 return MipsTargetLowering::getRegisterType(Context, VT);
92}
93
94unsigned MipsTargetLowering::getNumRegistersForCallingConv(LLVMContext &Context,
95 EVT VT) const {
96 if (VT.isVector())
97 return std::max((VT.getSizeInBits() / (Subtarget.isABI_O32() ? 32 : 64)),
98 1U);
99 return MipsTargetLowering::getNumRegisters(Context, VT);
100}
101
102unsigned MipsTargetLowering::getVectorTypeBreakdownForCallingConv(
103 LLVMContext &Context, EVT VT, EVT &IntermediateVT,
104 unsigned &NumIntermediates, MVT &RegisterVT) const {
105
106 // Break down vector types to either 2 i64s or 4 i32s.
107 RegisterVT = getRegisterTypeForCallingConv(Context, VT) ;
108 IntermediateVT = RegisterVT;
109 NumIntermediates = VT.getSizeInBits() < RegisterVT.getSizeInBits()
110 ? VT.getVectorNumElements()
111 : VT.getSizeInBits() / RegisterVT.getSizeInBits();
112
113 return NumIntermediates;
114}
115
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000116SDValue MipsTargetLowering::getGlobalReg(SelectionDAG &DAG, EVT Ty) const {
Akira Hatanakab049aef2012-02-24 22:34:47 +0000117 MipsFunctionInfo *FI = DAG.getMachineFunction().getInfo<MipsFunctionInfo>();
118 return DAG.getRegister(FI->getGlobalBaseReg(), Ty);
119}
120
Akira Hatanakad8f10ce2013-09-27 19:51:35 +0000121SDValue MipsTargetLowering::getTargetNode(GlobalAddressSDNode *N, EVT Ty,
122 SelectionDAG &DAG,
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000123 unsigned Flag) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +0000124 return DAG.getTargetGlobalAddress(N->getGlobal(), SDLoc(N), Ty, 0, Flag);
Akira Hatanakafd04ad42012-11-21 20:26:38 +0000125}
126
Akira Hatanakad8f10ce2013-09-27 19:51:35 +0000127SDValue MipsTargetLowering::getTargetNode(ExternalSymbolSDNode *N, EVT Ty,
128 SelectionDAG &DAG,
129 unsigned Flag) const {
130 return DAG.getTargetExternalSymbol(N->getSymbol(), Ty, Flag);
131}
132
133SDValue MipsTargetLowering::getTargetNode(BlockAddressSDNode *N, EVT Ty,
134 SelectionDAG &DAG,
135 unsigned Flag) const {
136 return DAG.getTargetBlockAddress(N->getBlockAddress(), Ty, 0, Flag);
137}
138
139SDValue MipsTargetLowering::getTargetNode(JumpTableSDNode *N, EVT Ty,
140 SelectionDAG &DAG,
141 unsigned Flag) const {
142 return DAG.getTargetJumpTable(N->getIndex(), Ty, Flag);
143}
144
145SDValue MipsTargetLowering::getTargetNode(ConstantPoolSDNode *N, EVT Ty,
146 SelectionDAG &DAG,
147 unsigned Flag) const {
148 return DAG.getTargetConstantPool(N->getConstVal(), Ty, N->getAlignment(),
149 N->getOffset(), Flag);
Akira Hatanakafd04ad42012-11-21 20:26:38 +0000150}
151
Chris Lattner5e693ed2009-07-28 03:13:23 +0000152const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +0000153 switch ((MipsISD::NodeType)Opcode) {
154 case MipsISD::FIRST_NUMBER: break;
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000155 case MipsISD::JmpLink: return "MipsISD::JmpLink";
Akira Hatanaka91318df2012-10-19 20:59:39 +0000156 case MipsISD::TailCall: return "MipsISD::TailCall";
Simon Dardisca74dd72017-01-27 11:36:52 +0000157 case MipsISD::Highest: return "MipsISD::Highest";
158 case MipsISD::Higher: return "MipsISD::Higher";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000159 case MipsISD::Hi: return "MipsISD::Hi";
160 case MipsISD::Lo: return "MipsISD::Lo";
Simon Dardisca74dd72017-01-27 11:36:52 +0000161 case MipsISD::GotHi: return "MipsISD::GotHi";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000162 case MipsISD::GPRel: return "MipsISD::GPRel";
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +0000163 case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000164 case MipsISD::Ret: return "MipsISD::Ret";
Vasileios Kalintiris43dff0c2015-10-26 12:38:43 +0000165 case MipsISD::ERet: return "MipsISD::ERet";
Akira Hatanakac0b02062013-01-30 00:26:49 +0000166 case MipsISD::EH_RETURN: return "MipsISD::EH_RETURN";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000167 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
168 case MipsISD::FPCmp: return "MipsISD::FPCmp";
169 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
170 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000171 case MipsISD::TruncIntFP: return "MipsISD::TruncIntFP";
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000172 case MipsISD::MFHI: return "MipsISD::MFHI";
173 case MipsISD::MFLO: return "MipsISD::MFLO";
174 case MipsISD::MTLOHI: return "MipsISD::MTLOHI";
Akira Hatanaka28721bd2013-03-30 01:14:04 +0000175 case MipsISD::Mult: return "MipsISD::Mult";
176 case MipsISD::Multu: return "MipsISD::Multu";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000177 case MipsISD::MAdd: return "MipsISD::MAdd";
178 case MipsISD::MAddu: return "MipsISD::MAddu";
179 case MipsISD::MSub: return "MipsISD::MSub";
180 case MipsISD::MSubu: return "MipsISD::MSubu";
181 case MipsISD::DivRem: return "MipsISD::DivRem";
182 case MipsISD::DivRemU: return "MipsISD::DivRemU";
Akira Hatanaka28721bd2013-03-30 01:14:04 +0000183 case MipsISD::DivRem16: return "MipsISD::DivRem16";
184 case MipsISD::DivRemU16: return "MipsISD::DivRemU16";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000185 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
186 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
Akira Hatanakafaa88c02011-12-12 22:38:19 +0000187 case MipsISD::Wrapper: return "MipsISD::Wrapper";
Matthias Braund04893f2015-05-07 21:33:59 +0000188 case MipsISD::DynAlloc: return "MipsISD::DynAlloc";
Akira Hatanakaa4c09bc2011-07-19 23:30:50 +0000189 case MipsISD::Sync: return "MipsISD::Sync";
Akira Hatanaka5360f882011-08-17 02:05:42 +0000190 case MipsISD::Ext: return "MipsISD::Ext";
191 case MipsISD::Ins: return "MipsISD::Ins";
Petar Jovanovicb71386a2017-03-15 13:10:08 +0000192 case MipsISD::CIns: return "MipsISD::CIns";
Akira Hatanakab9ebf8d2012-06-02 00:03:12 +0000193 case MipsISD::LWL: return "MipsISD::LWL";
194 case MipsISD::LWR: return "MipsISD::LWR";
195 case MipsISD::SWL: return "MipsISD::SWL";
196 case MipsISD::SWR: return "MipsISD::SWR";
197 case MipsISD::LDL: return "MipsISD::LDL";
198 case MipsISD::LDR: return "MipsISD::LDR";
199 case MipsISD::SDL: return "MipsISD::SDL";
200 case MipsISD::SDR: return "MipsISD::SDR";
Akira Hatanaka233ac532012-09-21 23:52:47 +0000201 case MipsISD::EXTP: return "MipsISD::EXTP";
202 case MipsISD::EXTPDP: return "MipsISD::EXTPDP";
203 case MipsISD::EXTR_S_H: return "MipsISD::EXTR_S_H";
204 case MipsISD::EXTR_W: return "MipsISD::EXTR_W";
205 case MipsISD::EXTR_R_W: return "MipsISD::EXTR_R_W";
206 case MipsISD::EXTR_RS_W: return "MipsISD::EXTR_RS_W";
207 case MipsISD::SHILO: return "MipsISD::SHILO";
208 case MipsISD::MTHLIP: return "MipsISD::MTHLIP";
Matthias Braund04893f2015-05-07 21:33:59 +0000209 case MipsISD::MULSAQ_S_W_PH: return "MipsISD::MULSAQ_S_W_PH";
210 case MipsISD::MAQ_S_W_PHL: return "MipsISD::MAQ_S_W_PHL";
211 case MipsISD::MAQ_S_W_PHR: return "MipsISD::MAQ_S_W_PHR";
212 case MipsISD::MAQ_SA_W_PHL: return "MipsISD::MAQ_SA_W_PHL";
213 case MipsISD::MAQ_SA_W_PHR: return "MipsISD::MAQ_SA_W_PHR";
214 case MipsISD::DPAU_H_QBL: return "MipsISD::DPAU_H_QBL";
215 case MipsISD::DPAU_H_QBR: return "MipsISD::DPAU_H_QBR";
216 case MipsISD::DPSU_H_QBL: return "MipsISD::DPSU_H_QBL";
217 case MipsISD::DPSU_H_QBR: return "MipsISD::DPSU_H_QBR";
218 case MipsISD::DPAQ_S_W_PH: return "MipsISD::DPAQ_S_W_PH";
219 case MipsISD::DPSQ_S_W_PH: return "MipsISD::DPSQ_S_W_PH";
220 case MipsISD::DPAQ_SA_L_W: return "MipsISD::DPAQ_SA_L_W";
221 case MipsISD::DPSQ_SA_L_W: return "MipsISD::DPSQ_SA_L_W";
222 case MipsISD::DPA_W_PH: return "MipsISD::DPA_W_PH";
223 case MipsISD::DPS_W_PH: return "MipsISD::DPS_W_PH";
224 case MipsISD::DPAQX_S_W_PH: return "MipsISD::DPAQX_S_W_PH";
225 case MipsISD::DPAQX_SA_W_PH: return "MipsISD::DPAQX_SA_W_PH";
226 case MipsISD::DPAX_W_PH: return "MipsISD::DPAX_W_PH";
227 case MipsISD::DPSX_W_PH: return "MipsISD::DPSX_W_PH";
228 case MipsISD::DPSQX_S_W_PH: return "MipsISD::DPSQX_S_W_PH";
229 case MipsISD::DPSQX_SA_W_PH: return "MipsISD::DPSQX_SA_W_PH";
230 case MipsISD::MULSA_W_PH: return "MipsISD::MULSA_W_PH";
Akira Hatanaka233ac532012-09-21 23:52:47 +0000231 case MipsISD::MULT: return "MipsISD::MULT";
232 case MipsISD::MULTU: return "MipsISD::MULTU";
Jia Liu434874d2013-03-04 01:06:54 +0000233 case MipsISD::MADD_DSP: return "MipsISD::MADD_DSP";
Akira Hatanaka233ac532012-09-21 23:52:47 +0000234 case MipsISD::MADDU_DSP: return "MipsISD::MADDU_DSP";
235 case MipsISD::MSUB_DSP: return "MipsISD::MSUB_DSP";
236 case MipsISD::MSUBU_DSP: return "MipsISD::MSUBU_DSP";
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000237 case MipsISD::SHLL_DSP: return "MipsISD::SHLL_DSP";
238 case MipsISD::SHRA_DSP: return "MipsISD::SHRA_DSP";
239 case MipsISD::SHRL_DSP: return "MipsISD::SHRL_DSP";
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000240 case MipsISD::SETCC_DSP: return "MipsISD::SETCC_DSP";
241 case MipsISD::SELECT_CC_DSP: return "MipsISD::SELECT_CC_DSP";
Daniel Sandersce09d072013-08-28 12:14:50 +0000242 case MipsISD::VALL_ZERO: return "MipsISD::VALL_ZERO";
243 case MipsISD::VANY_ZERO: return "MipsISD::VANY_ZERO";
244 case MipsISD::VALL_NONZERO: return "MipsISD::VALL_NONZERO";
245 case MipsISD::VANY_NONZERO: return "MipsISD::VANY_NONZERO";
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000246 case MipsISD::VCEQ: return "MipsISD::VCEQ";
247 case MipsISD::VCLE_S: return "MipsISD::VCLE_S";
248 case MipsISD::VCLE_U: return "MipsISD::VCLE_U";
249 case MipsISD::VCLT_S: return "MipsISD::VCLT_S";
250 case MipsISD::VCLT_U: return "MipsISD::VCLT_U";
Daniel Sanders3ce56622013-09-24 12:18:31 +0000251 case MipsISD::VSMAX: return "MipsISD::VSMAX";
252 case MipsISD::VSMIN: return "MipsISD::VSMIN";
253 case MipsISD::VUMAX: return "MipsISD::VUMAX";
254 case MipsISD::VUMIN: return "MipsISD::VUMIN";
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000255 case MipsISD::VEXTRACT_SEXT_ELT: return "MipsISD::VEXTRACT_SEXT_ELT";
256 case MipsISD::VEXTRACT_ZEXT_ELT: return "MipsISD::VEXTRACT_ZEXT_ELT";
Daniel Sandersf7456c72013-09-23 13:22:24 +0000257 case MipsISD::VNOR: return "MipsISD::VNOR";
Daniel Sanderse5087042013-09-24 14:02:15 +0000258 case MipsISD::VSHF: return "MipsISD::VSHF";
Daniel Sanders26307182013-09-24 14:20:00 +0000259 case MipsISD::SHF: return "MipsISD::SHF";
Daniel Sanders2ed228b2013-09-24 14:36:12 +0000260 case MipsISD::ILVEV: return "MipsISD::ILVEV";
261 case MipsISD::ILVOD: return "MipsISD::ILVOD";
262 case MipsISD::ILVL: return "MipsISD::ILVL";
263 case MipsISD::ILVR: return "MipsISD::ILVR";
Daniel Sandersfae5f2a2013-09-24 14:53:25 +0000264 case MipsISD::PCKEV: return "MipsISD::PCKEV";
265 case MipsISD::PCKOD: return "MipsISD::PCKOD";
Daniel Sandersb50ccf82014-04-01 10:35:28 +0000266 case MipsISD::INSVE: return "MipsISD::INSVE";
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000267 }
Matthias Braund04893f2015-05-07 21:33:59 +0000268 return nullptr;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000269}
270
Eric Christopherb1526602014-09-19 23:30:42 +0000271MipsTargetLowering::MipsTargetLowering(const MipsTargetMachine &TM,
Eric Christopher8924d272014-07-18 23:25:04 +0000272 const MipsSubtarget &STI)
Eric Christopher96e72c62015-01-29 23:27:36 +0000273 : TargetLowering(TM), Subtarget(STI), ABI(TM.getABI()) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000274 // Mips does not have i1 type, so use i32 for
Wesley Peck527da1b2010-11-23 03:31:01 +0000275 // setcc operations results (slt, sgt, ...).
Duncan Sands8d6e2e12008-11-23 15:47:28 +0000276 setBooleanContents(ZeroOrOneBooleanContent);
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000277 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Daniel Sanderscbd44c52014-07-10 10:18:12 +0000278 // The cmp.cond.fmt instruction in MIPS32r6/MIPS64r6 uses 0 and -1 like MSA
279 // does. Integer booleans still use 0 and 1.
Eric Christopher1c29a652014-07-18 22:55:25 +0000280 if (Subtarget.hasMips32r6())
Daniel Sanderscbd44c52014-07-10 10:18:12 +0000281 setBooleanContents(ZeroOrOneBooleanContent,
282 ZeroOrNegativeOneBooleanContent);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000283
Wesley Peck527da1b2010-11-23 03:31:01 +0000284 // Load extented operations for i1 types must be promoted
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000285 for (MVT VT : MVT::integer_valuetypes()) {
286 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
287 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
288 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
289 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000290
Pirama Arumuga Nainar34056de2015-04-20 20:15:36 +0000291 // MIPS doesn't have extending float->double load/store. Set LoadExtAction
292 // for f32, f16
293 for (MVT VT : MVT::fp_valuetypes()) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000294 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
Pirama Arumuga Nainar34056de2015-04-20 20:15:36 +0000295 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f16, Expand);
296 }
297
298 // Set LoadExtAction for f16 vectors to Expand
299 for (MVT VT : MVT::fp_vector_valuetypes()) {
300 MVT F16VT = MVT::getVectorVT(MVT::f16, VT.getVectorNumElements());
301 if (F16VT.isValid())
302 setLoadExtAction(ISD::EXTLOAD, VT, F16VT, Expand);
303 }
304
305 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
306 setTruncStoreAction(MVT::f64, MVT::f16, Expand);
307
Owen Anderson9f944592009-08-11 20:47:22 +0000308 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman39d6faa2009-07-17 02:28:12 +0000309
Wesley Peck527da1b2010-11-23 03:31:01 +0000310 // Used by legalize types to correctly generate the setcc result.
311 // Without this, every float setcc comes with a AND/OR with the result,
312 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +0000313 // which is used implicitly by brcond and select operations.
Vasileios Kalintiris36901dd2016-03-01 20:25:43 +0000314 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +0000315
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000316 // Mips Custom Operations
Joerg Sonnenberger1a7eec62016-11-15 12:39:46 +0000317 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000318 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000319 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000320 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
321 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
322 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Simon Dardisba92b032016-09-09 11:06:01 +0000323 setOperationAction(ISD::SELECT, MVT::f32, Custom);
324 setOperationAction(ISD::SELECT, MVT::f64, Custom);
325 setOperationAction(ISD::SELECT, MVT::i32, Custom);
326 setOperationAction(ISD::SETCC, MVT::f32, Custom);
327 setOperationAction(ISD::SETCC, MVT::f64, Custom);
328 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000329 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
330 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000331 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000332
Eric Christopher1c29a652014-07-18 22:55:25 +0000333 if (Subtarget.isGP64bit()) {
Akira Hatanakada00aa82012-03-10 00:03:50 +0000334 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
335 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
336 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
337 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
338 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
Simon Dardisba92b032016-09-09 11:06:01 +0000339 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Akira Hatanaka019e5922012-06-02 00:04:42 +0000340 setOperationAction(ISD::LOAD, MVT::i64, Custom);
341 setOperationAction(ISD::STORE, MVT::i64, Custom);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000342 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +0000343 setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom);
344 setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom);
345 setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000346 }
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +0000347
Eric Christopher1c29a652014-07-18 22:55:25 +0000348 if (!Subtarget.isGP64bit()) {
Akira Hatanaka0a8ab712012-05-09 00:55:21 +0000349 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
350 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
351 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
352 }
353
Hal Finkel5081ac22016-09-01 10:28:47 +0000354 setOperationAction(ISD::EH_DWARF_CFA, MVT::i32, Custom);
Eric Christopher1c29a652014-07-18 22:55:25 +0000355 if (Subtarget.isGP64bit())
Hal Finkel5081ac22016-09-01 10:28:47 +0000356 setOperationAction(ISD::EH_DWARF_CFA, MVT::i64, Custom);
Akira Hatanaka28e02ec2012-11-07 19:10:58 +0000357
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000358 setOperationAction(ISD::SDIV, MVT::i32, Expand);
359 setOperationAction(ISD::SREM, MVT::i32, Expand);
360 setOperationAction(ISD::UDIV, MVT::i32, Expand);
361 setOperationAction(ISD::UREM, MVT::i32, Expand);
Akira Hatanakab1538f92011-10-03 21:06:13 +0000362 setOperationAction(ISD::SDIV, MVT::i64, Expand);
363 setOperationAction(ISD::SREM, MVT::i64, Expand);
364 setOperationAction(ISD::UDIV, MVT::i64, Expand);
365 setOperationAction(ISD::UREM, MVT::i64, Expand);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000366
Simon Dardis250256f2017-07-13 11:28:05 +0000367 if (!(Subtarget.hasDSP() && Subtarget.hasMips32r2())) {
368 setOperationAction(ISD::ADDC, MVT::i32, Expand);
369 setOperationAction(ISD::ADDE, MVT::i32, Expand);
370 }
371
372 setOperationAction(ISD::ADDC, MVT::i64, Expand);
373 setOperationAction(ISD::ADDE, MVT::i64, Expand);
374 setOperationAction(ISD::SUBC, MVT::i32, Expand);
375 setOperationAction(ISD::SUBE, MVT::i32, Expand);
376 setOperationAction(ISD::SUBC, MVT::i64, Expand);
377 setOperationAction(ISD::SUBE, MVT::i64, Expand);
378
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000379 // Operations not directly supported by Mips.
Tom Stellardb1588fc2013-03-08 15:36:57 +0000380 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
381 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
382 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
383 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
Tom Stellard3787b122014-06-10 16:01:29 +0000384 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
385 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
Matt Arsenaulta0e5cd52016-01-11 16:44:48 +0000386 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
387 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000388 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Akira Hatanaka79aed152011-12-20 23:40:56 +0000389 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000390 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Akira Hatanaka79aed152011-12-20 23:40:56 +0000391 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000392 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Eric Christopher1c29a652014-07-18 22:55:25 +0000393 if (Subtarget.hasCnMips()) {
Kai Nacke93fe5e82014-03-20 11:51:58 +0000394 setOperationAction(ISD::CTPOP, MVT::i32, Legal);
395 setOperationAction(ISD::CTPOP, MVT::i64, Legal);
396 } else {
397 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
398 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
399 }
Owen Anderson9f944592009-08-11 20:47:22 +0000400 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
Akira Hatanaka410ce9c2011-12-21 00:14:05 +0000401 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000402 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Akira Hatanaka7ba8a8d2011-09-30 18:51:46 +0000403 setOperationAction(ISD::ROTL, MVT::i64, Expand);
Akira Hatanaka33a25af2012-07-31 20:54:48 +0000404 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
405 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Bruno Cardoso Lopesd47180e2010-12-09 17:32:30 +0000406
Eric Christopher1c29a652014-07-18 22:55:25 +0000407 if (!Subtarget.hasMips32r2())
Bruno Cardoso Lopesd47180e2010-12-09 17:32:30 +0000408 setOperationAction(ISD::ROTR, MVT::i32, Expand);
409
Eric Christopher1c29a652014-07-18 22:55:25 +0000410 if (!Subtarget.hasMips64r2())
Akira Hatanaka7ba8a8d2011-09-30 18:51:46 +0000411 setOperationAction(ISD::ROTR, MVT::i64, Expand);
412
Owen Anderson9f944592009-08-11 20:47:22 +0000413 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes22b69db2011-03-04 18:54:14 +0000414 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000415 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes22b69db2011-03-04 18:54:14 +0000416 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000417 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
418 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000419 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanakadfb8cda2011-05-23 22:23:58 +0000420 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000421 setOperationAction(ISD::FLOG, MVT::f32, Expand);
422 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
423 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
424 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000425 setOperationAction(ISD::FMA, MVT::f32, Expand);
426 setOperationAction(ISD::FMA, MVT::f64, Expand);
Akira Hatanaka0603ad82012-03-29 18:43:11 +0000427 setOperationAction(ISD::FREM, MVT::f32, Expand);
428 setOperationAction(ISD::FREM, MVT::f64, Expand);
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000429
Pirama Arumuga Nainar34056de2015-04-20 20:15:36 +0000430 // Lower f16 conversion operations into library calls
431 setOperationAction(ISD::FP16_TO_FP, MVT::f32, Expand);
432 setOperationAction(ISD::FP_TO_FP16, MVT::f32, Expand);
433 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand);
434 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Expand);
435
Akira Hatanakac0b02062013-01-30 00:26:49 +0000436 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
437
Daniel Sanders2b553d42014-08-01 09:17:39 +0000438 setOperationAction(ISD::VASTART, MVT::Other, Custom);
439 setOperationAction(ISD::VAARG, MVT::Other, Custom);
Bruno Cardoso Lopes048ffab2011-03-09 19:22:22 +0000440 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
441 setOperationAction(ISD::VAEND, MVT::Other, Expand);
442
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000443 // Use the default for now
Owen Anderson9f944592009-08-11 20:47:22 +0000444 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
445 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman26a48482011-07-27 22:21:52 +0000446
Vasileios Kalintirisb04672c2015-11-06 12:07:20 +0000447 if (!Subtarget.isGP64bit()) {
448 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
449 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
450 }
Eli Friedman7dfa7912011-08-29 18:23:02 +0000451
Eli Friedman30a49e92011-08-03 21:06:02 +0000452
Eric Christopher1c29a652014-07-18 22:55:25 +0000453 if (!Subtarget.hasMips32r2()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000454 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
455 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000456 }
457
Daniel Sanders070fd1c2014-05-12 12:41:59 +0000458 // MIPS16 lacks MIPS32's clz and clo instructions.
Eric Christopher1c29a652014-07-18 22:55:25 +0000459 if (!Subtarget.hasMips32() || Subtarget.inMips16Mode())
Owen Anderson9f944592009-08-11 20:47:22 +0000460 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Eric Christopher1c29a652014-07-18 22:55:25 +0000461 if (!Subtarget.hasMips64())
Akira Hatanaka1d8efab2011-12-21 00:20:27 +0000462 setOperationAction(ISD::CTLZ, MVT::i64, Expand);
Bruno Cardoso Lopes93da7e62008-08-08 06:16:31 +0000463
Eric Christopher1c29a652014-07-18 22:55:25 +0000464 if (!Subtarget.hasMips32r2())
Owen Anderson9f944592009-08-11 20:47:22 +0000465 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Eric Christopher1c29a652014-07-18 22:55:25 +0000466 if (!Subtarget.hasMips64r2())
Akira Hatanaka4706ac92011-12-20 23:56:43 +0000467 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000468
Eric Christopher1c29a652014-07-18 22:55:25 +0000469 if (Subtarget.isGP64bit()) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000470 setLoadExtAction(ISD::SEXTLOAD, MVT::i64, MVT::i32, Custom);
471 setLoadExtAction(ISD::ZEXTLOAD, MVT::i64, MVT::i32, Custom);
472 setLoadExtAction(ISD::EXTLOAD, MVT::i64, MVT::i32, Custom);
Akira Hatanaka019e5922012-06-02 00:04:42 +0000473 setTruncStoreAction(MVT::i64, MVT::i32, Custom);
474 }
475
Akira Hatanakaa3d9ab92013-07-26 20:58:55 +0000476 setOperationAction(ISD::TRAP, MVT::Other, Legal);
477
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000478 setTargetDAGCombine(ISD::SDIVREM);
479 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka5e152182012-03-08 03:26:37 +0000480 setTargetDAGCombine(ISD::SELECT);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000481 setTargetDAGCombine(ISD::AND);
482 setTargetDAGCombine(ISD::OR);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000483 setTargetDAGCombine(ISD::ADD);
Simon Dardis250256f2017-07-13 11:28:05 +0000484 setTargetDAGCombine(ISD::SUB);
Vasileios Kalintiris3751d412016-04-13 15:07:45 +0000485 setTargetDAGCombine(ISD::AssertZext);
Petar Jovanovicb71386a2017-03-15 13:10:08 +0000486 setTargetDAGCombine(ISD::SHL);
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000487
Vasileios Kalintiris1ed49fd2016-09-07 10:01:18 +0000488 if (ABI.IsO32()) {
489 // These libcalls are not available in 32-bit.
490 setLibcallName(RTLIB::SHL_I128, nullptr);
491 setLibcallName(RTLIB::SRL_I128, nullptr);
492 setLibcallName(RTLIB::SRA_I128, nullptr);
493 }
494
Eric Christopher1c29a652014-07-18 22:55:25 +0000495 setMinFunctionAlignment(Subtarget.isGP64bit() ? 3 : 2);
Eli Friedman2518f832011-05-06 20:34:06 +0000496
Daniel Sanders2b553d42014-08-01 09:17:39 +0000497 // The arguments on the stack are defined in terms of 4-byte slots on O32
498 // and 8-byte slots on N32/N64.
Eric Christopher96e72c62015-01-29 23:27:36 +0000499 setMinStackArgumentAlignment((ABI.IsN32() || ABI.IsN64()) ? 8 : 4);
Daniel Sanders2b553d42014-08-01 09:17:39 +0000500
Eric Christopher96e72c62015-01-29 23:27:36 +0000501 setStackPointerRegisterToSaveRestore(ABI.IsN64() ? Mips::SP_64 : Mips::SP);
Akira Hatanakaaa560002011-05-26 18:59:03 +0000502
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000503 MaxStoresPerMemcpy = 16;
Zoran Jovanovicff9d5f32013-12-19 16:12:56 +0000504
Eric Christopher1c29a652014-07-18 22:55:25 +0000505 isMicroMips = Subtarget.inMicroMipsMode();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000506}
507
Eric Christopherb1526602014-09-19 23:30:42 +0000508const MipsTargetLowering *MipsTargetLowering::create(const MipsTargetMachine &TM,
Eric Christopher8924d272014-07-18 23:25:04 +0000509 const MipsSubtarget &STI) {
510 if (STI.inMips16Mode())
511 return llvm::createMips16TargetLowering(TM, STI);
Jia Liuf54f60f2012-02-28 07:46:26 +0000512
Eric Christopher8924d272014-07-18 23:25:04 +0000513 return llvm::createMipsSETargetLowering(TM, STI);
Akira Hatanaka2fcc1cf2011-08-12 21:30:06 +0000514}
515
Reed Kotler720c5ca2014-04-17 22:15:34 +0000516// Create a fast isel object.
517FastISel *
518MipsTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
519 const TargetLibraryInfo *libInfo) const {
Vasileios Kalintiris3955b752016-10-18 13:05:42 +0000520 const MipsTargetMachine &TM =
521 static_cast<const MipsTargetMachine &>(funcInfo.MF->getTarget());
522
523 // We support only the standard encoding [MIPS32,MIPS32R5] ISAs.
524 bool UseFastISel = TM.Options.EnableFastISel && Subtarget.hasMips32() &&
525 !Subtarget.hasMips32r6() && !Subtarget.inMips16Mode() &&
526 !Subtarget.inMicroMipsMode();
527
Petar Jovanovic3c039d92017-06-07 12:59:53 +0000528 // Disable if either of the following is true:
529 // We do not generate PIC, the ABI is not O32, LargeGOT is being used.
530 if (!TM.isPositionIndependent() || !TM.getABI().IsO32() || LargeGOT)
Vasileios Kalintiris3955b752016-10-18 13:05:42 +0000531 UseFastISel = false;
532
533 return UseFastISel ? Mips::createFastISel(funcInfo, libInfo) : nullptr;
Reed Kotler720c5ca2014-04-17 22:15:34 +0000534}
535
Mehdi Amini44ede332015-07-09 02:09:04 +0000536EVT MipsTargetLowering::getSetCCResultType(const DataLayout &, LLVMContext &,
537 EVT VT) const {
Vasileios Kalintiris36901dd2016-03-01 20:25:43 +0000538 if (!VT.isVector())
539 return MVT::i32;
540 return VT.changeVectorElementTypeToInteger();
Scott Michela6729e82008-03-10 15:42:14 +0000541}
542
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000543static SDValue performDivRemCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000544 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000545 const MipsSubtarget &Subtarget) {
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000546 if (DCI.isBeforeLegalizeOps())
547 return SDValue();
548
Akira Hatanakab1538f92011-10-03 21:06:13 +0000549 EVT Ty = N->getValueType(0);
Akira Hatanaka8002a3f2013-08-14 00:47:08 +0000550 unsigned LO = (Ty == MVT::i32) ? Mips::LO0 : Mips::LO0_64;
551 unsigned HI = (Ty == MVT::i32) ? Mips::HI0 : Mips::HI0_64;
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000552 unsigned Opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem16 :
553 MipsISD::DivRemU16;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000554 SDLoc DL(N);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000555
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000556 SDValue DivRem = DAG.getNode(Opc, DL, MVT::Glue,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000557 N->getOperand(0), N->getOperand(1));
558 SDValue InChain = DAG.getEntryNode();
559 SDValue InGlue = DivRem;
560
561 // insert MFLO
562 if (N->hasAnyUseOfValue(0)) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000563 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, DL, LO, Ty,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000564 InGlue);
565 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
566 InChain = CopyFromLo.getValue(1);
567 InGlue = CopyFromLo.getValue(2);
568 }
569
570 // insert MFHI
571 if (N->hasAnyUseOfValue(1)) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000572 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, DL,
Akira Hatanakab1538f92011-10-03 21:06:13 +0000573 HI, Ty, InGlue);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000574 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
575 }
576
577 return SDValue();
578}
579
Simon Dardisba92b032016-09-09 11:06:01 +0000580static Mips::CondCode condCodeToFCC(ISD::CondCode CC) {
581 switch (CC) {
582 default: llvm_unreachable("Unknown fp condition code!");
583 case ISD::SETEQ:
584 case ISD::SETOEQ: return Mips::FCOND_OEQ;
585 case ISD::SETUNE: return Mips::FCOND_UNE;
586 case ISD::SETLT:
587 case ISD::SETOLT: return Mips::FCOND_OLT;
588 case ISD::SETGT:
589 case ISD::SETOGT: return Mips::FCOND_OGT;
590 case ISD::SETLE:
591 case ISD::SETOLE: return Mips::FCOND_OLE;
592 case ISD::SETGE:
593 case ISD::SETOGE: return Mips::FCOND_OGE;
594 case ISD::SETULT: return Mips::FCOND_ULT;
595 case ISD::SETULE: return Mips::FCOND_ULE;
596 case ISD::SETUGT: return Mips::FCOND_UGT;
597 case ISD::SETUGE: return Mips::FCOND_UGE;
598 case ISD::SETUO: return Mips::FCOND_UN;
599 case ISD::SETO: return Mips::FCOND_OR;
600 case ISD::SETNE:
601 case ISD::SETONE: return Mips::FCOND_ONE;
602 case ISD::SETUEQ: return Mips::FCOND_UEQ;
603 }
604}
605
606
607/// This function returns true if the floating point conditional branches and
608/// conditional moves which use condition code CC should be inverted.
609static bool invertFPCondCodeUser(Mips::CondCode CC) {
610 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
611 return false;
612
613 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
614 "Illegal Condition Code");
615
616 return true;
617}
618
619// Creates and returns an FPCmp node from a setcc node.
620// Returns Op if setcc is not a floating point comparison.
621static SDValue createFPCmp(SelectionDAG &DAG, const SDValue &Op) {
622 // must be a SETCC node
623 if (Op.getOpcode() != ISD::SETCC)
624 return Op;
625
626 SDValue LHS = Op.getOperand(0);
627
628 if (!LHS.getValueType().isFloatingPoint())
629 return Op;
630
631 SDValue RHS = Op.getOperand(1);
632 SDLoc DL(Op);
633
634 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
635 // node if necessary.
636 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
637
638 return DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS,
639 DAG.getConstant(condCodeToFCC(CC), DL, MVT::i32));
640}
641
642// Creates and returns a CMovFPT/F node.
643static SDValue createCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True,
644 SDValue False, const SDLoc &DL) {
645 ConstantSDNode *CC = cast<ConstantSDNode>(Cond.getOperand(2));
646 bool invert = invertFPCondCodeUser((Mips::CondCode)CC->getSExtValue());
647 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
648
649 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
650 True.getValueType(), True, FCC0, False, Cond);
651}
652
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000653static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000654 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000655 const MipsSubtarget &Subtarget) {
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000656 if (DCI.isBeforeLegalizeOps())
657 return SDValue();
658
659 SDValue SetCC = N->getOperand(0);
660
661 if ((SetCC.getOpcode() != ISD::SETCC) ||
662 !SetCC.getOperand(0).getValueType().isInteger())
663 return SDValue();
664
665 SDValue False = N->getOperand(2);
666 EVT FalseTy = False.getValueType();
667
668 if (!FalseTy.isInteger())
669 return SDValue();
670
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000671 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(False);
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000672
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000673 // If the RHS (False) is 0, we swap the order of the operands
674 // of ISD::SELECT (obviously also inverting the condition) so that we can
675 // take advantage of conditional moves using the $0 register.
676 // Example:
677 // return (a != 0) ? x : 0;
678 // load $reg, x
679 // movz $reg, $0, a
680 if (!FalseC)
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000681 return SDValue();
682
Andrew Trickef9de2a2013-05-25 02:42:55 +0000683 const SDLoc DL(N);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000684
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000685 if (!FalseC->getZExtValue()) {
686 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
687 SDValue True = N->getOperand(1);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000688
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000689 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
690 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
691
692 return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True);
693 }
694
Matheus Almeidaa6beac12013-12-05 12:07:05 +0000695 // If both operands are integer constants there's a possibility that we
696 // can do some interesting optimizations.
697 SDValue True = N->getOperand(1);
698 ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(True);
699
700 if (!TrueC || !True.getValueType().isInteger())
701 return SDValue();
702
Vasileios Kalintiris36901dd2016-03-01 20:25:43 +0000703 // We'll also ignore MVT::i64 operands as this optimizations proves
704 // to be ineffective because of the required sign extensions as the result
705 // of a SETCC operator is always MVT::i32 for non-vector types.
706 if (True.getValueType() == MVT::i64)
707 return SDValue();
708
Matheus Almeidaa6beac12013-12-05 12:07:05 +0000709 int64_t Diff = TrueC->getSExtValue() - FalseC->getSExtValue();
710
711 // 1) (a < x) ? y : y-1
712 // slti $reg1, a, x
713 // addiu $reg2, $reg1, y-1
Vasileios Kalintiris36901dd2016-03-01 20:25:43 +0000714 if (Diff == 1)
715 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, False);
Matheus Almeidaa6beac12013-12-05 12:07:05 +0000716
717 // 2) (a < x) ? y-1 : y
718 // slti $reg1, a, x
719 // xor $reg1, $reg1, 1
720 // addiu $reg2, $reg1, y-1
721 if (Diff == -1) {
722 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
723 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
724 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
725 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, True);
726 }
727
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000728 // Couldn't optimize.
729 return SDValue();
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000730}
731
Vasileios Kalintirise741eb22015-03-02 12:47:32 +0000732static SDValue performCMovFPCombine(SDNode *N, SelectionDAG &DAG,
733 TargetLowering::DAGCombinerInfo &DCI,
734 const MipsSubtarget &Subtarget) {
735 if (DCI.isBeforeLegalizeOps())
736 return SDValue();
737
738 SDValue ValueIfTrue = N->getOperand(0), ValueIfFalse = N->getOperand(2);
739
740 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(ValueIfFalse);
741 if (!FalseC || FalseC->getZExtValue())
742 return SDValue();
743
744 // Since RHS (False) is 0, we swap the order of the True/False operands
745 // (obviously also inverting the condition) so that we can
746 // take advantage of conditional moves using the $0 register.
747 // Example:
748 // return (a != 0) ? x : 0;
749 // load $reg, x
750 // movz $reg, $0, a
751 unsigned Opc = (N->getOpcode() == MipsISD::CMovFP_T) ? MipsISD::CMovFP_F :
752 MipsISD::CMovFP_T;
753
754 SDValue FCC = N->getOperand(1), Glue = N->getOperand(3);
Vasileios Kalintiris2ef28882015-03-04 12:10:18 +0000755 return DAG.getNode(Opc, SDLoc(N), ValueIfFalse.getValueType(),
756 ValueIfFalse, FCC, ValueIfTrue, Glue);
Vasileios Kalintirise741eb22015-03-02 12:47:32 +0000757}
758
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000759static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000760 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000761 const MipsSubtarget &Subtarget) {
Eric Christopher1c29a652014-07-18 22:55:25 +0000762 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasExtractInsert())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000763 return SDValue();
764
Petar Jovanovicb71386a2017-03-15 13:10:08 +0000765 SDValue FirstOperand = N->getOperand(0);
766 unsigned FirstOperandOpc = FirstOperand.getOpcode();
767 SDValue Mask = N->getOperand(1);
768 EVT ValTy = N->getValueType(0);
769 SDLoc DL(N);
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000770
Petar Jovanovicb71386a2017-03-15 13:10:08 +0000771 uint64_t Pos = 0, SMPos, SMSize;
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000772 ConstantSDNode *CN;
Petar Jovanovicb71386a2017-03-15 13:10:08 +0000773 SDValue NewOperand;
774 unsigned Opc;
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000775
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000776 // Op's second operand must be a shifted mask.
777 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000778 !isShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000779 return SDValue();
780
Petar Jovanovicb71386a2017-03-15 13:10:08 +0000781 if (FirstOperandOpc == ISD::SRA || FirstOperandOpc == ISD::SRL) {
782 // Pattern match EXT.
783 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
784 // => ext $dst, $src, pos, size
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000785
Petar Jovanovicb71386a2017-03-15 13:10:08 +0000786 // The second operand of the shift must be an immediate.
787 if (!(CN = dyn_cast<ConstantSDNode>(FirstOperand.getOperand(1))))
788 return SDValue();
789
790 Pos = CN->getZExtValue();
791
792 // Return if the shifted mask does not start at bit 0 or the sum of its size
793 // and Pos exceeds the word's size.
794 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
795 return SDValue();
796
797 Opc = MipsISD::Ext;
798 NewOperand = FirstOperand.getOperand(0);
799 } else if (FirstOperandOpc == ISD::SHL && Subtarget.hasCnMips()) {
800 // Pattern match CINS.
801 // $dst = and (shl $src , pos), mask
802 // => cins $dst, $src, pos, size
803 // mask is a shifted mask with consecutive 1's, pos = shift amount,
804 // size = population count.
805
806 // The second operand of the shift must be an immediate.
807 if (!(CN = dyn_cast<ConstantSDNode>(FirstOperand.getOperand(1))))
808 return SDValue();
809
810 Pos = CN->getZExtValue();
811
812 if (SMPos != Pos || Pos >= ValTy.getSizeInBits() || SMSize >= 32 ||
813 Pos + SMSize > ValTy.getSizeInBits())
814 return SDValue();
815
816 NewOperand = FirstOperand.getOperand(0);
817 // SMSize is 'location' (position) in this case, not size.
818 SMSize--;
819 Opc = MipsISD::CIns;
820 } else {
821 // Pattern match EXT.
822 // $dst = and $src, (2**size - 1) , if size > 16
823 // => ext $dst, $src, pos, size , pos = 0
824
825 // If the mask is <= 0xffff, andi can be used instead.
826 if (CN->getZExtValue() <= 0xffff)
827 return SDValue();
828
829 // Return if the mask doesn't start at position 0.
830 if (SMPos)
831 return SDValue();
832
833 Opc = MipsISD::Ext;
834 NewOperand = FirstOperand;
835 }
836 return DAG.getNode(Opc, DL, ValTy, NewOperand,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000837 DAG.getConstant(Pos, DL, MVT::i32),
838 DAG.getConstant(SMSize, DL, MVT::i32));
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000839}
Jia Liuf54f60f2012-02-28 07:46:26 +0000840
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000841static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000842 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000843 const MipsSubtarget &Subtarget) {
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000844 // Pattern match INS.
845 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
Jia Liuf54f60f2012-02-28 07:46:26 +0000846 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000847 // => ins $dst, $src, size, pos, $src1
Eric Christopher1c29a652014-07-18 22:55:25 +0000848 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasExtractInsert())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000849 return SDValue();
850
851 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
852 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
Strahinja Petrovicab9573f2017-05-22 09:06:44 +0000853 ConstantSDNode *CN, *CN1;
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000854
855 // See if Op's first operand matches (and $src1 , mask0).
856 if (And0.getOpcode() != ISD::AND)
857 return SDValue();
858
859 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000860 !isShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000861 return SDValue();
862
863 // See if Op's second operand matches (and (shl $src, pos), mask1).
Strahinja Petrovicab9573f2017-05-22 09:06:44 +0000864 if (And1.getOpcode() == ISD::AND &&
865 And1.getOperand(0).getOpcode() == ISD::SHL) {
866
867 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
868 !isShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
869 return SDValue();
870
Davide Italianoef9bfe92017-05-26 21:56:19 +0000871 // The shift masks must have the same position and size.
872 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
873 return SDValue();
Strahinja Petrovicab9573f2017-05-22 09:06:44 +0000874
Davide Italianoef9bfe92017-05-26 21:56:19 +0000875 SDValue Shl = And1.getOperand(0);
Strahinja Petrovicab9573f2017-05-22 09:06:44 +0000876
Davide Italianoef9bfe92017-05-26 21:56:19 +0000877 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
878 return SDValue();
Strahinja Petrovicab9573f2017-05-22 09:06:44 +0000879
Davide Italianoef9bfe92017-05-26 21:56:19 +0000880 unsigned Shamt = CN->getZExtValue();
Strahinja Petrovicab9573f2017-05-22 09:06:44 +0000881
Davide Italianoef9bfe92017-05-26 21:56:19 +0000882 // Return if the shift amount and the first bit position of mask are not the
883 // same.
884 EVT ValTy = N->getValueType(0);
885 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
886 return SDValue();
Strahinja Petrovicab9573f2017-05-22 09:06:44 +0000887
Davide Italianoef9bfe92017-05-26 21:56:19 +0000888 SDLoc DL(N);
889 return DAG.getNode(MipsISD::Ins, DL, ValTy, Shl.getOperand(0),
890 DAG.getConstant(SMPos0, DL, MVT::i32),
891 DAG.getConstant(SMSize0, DL, MVT::i32),
892 And0.getOperand(0));
Strahinja Petrovicab9573f2017-05-22 09:06:44 +0000893 } else {
894 // Pattern match DINS.
895 // $dst = or (and $src, mask0), mask1
896 // where mask0 = ((1 << SMSize0) -1) << SMPos0
897 // => dins $dst, $src, pos, size
898 if (~CN->getSExtValue() == ((((int64_t)1 << SMSize0) - 1) << SMPos0) &&
899 ((SMSize0 + SMPos0 <= 64 && Subtarget.hasMips64r2()) ||
900 (SMSize0 + SMPos0 <= 32))) {
901 // Check if AND instruction has constant as argument
902 bool isConstCase = And1.getOpcode() != ISD::AND;
903 if (And1.getOpcode() == ISD::AND) {
904 if (!(CN1 = dyn_cast<ConstantSDNode>(And1->getOperand(1))))
905 return SDValue();
906 } else {
907 if (!(CN1 = dyn_cast<ConstantSDNode>(N->getOperand(1))))
908 return SDValue();
909 }
Strahinja Petrovicd280ea42017-06-21 09:25:51 +0000910 // Don't generate INS if constant OR operand doesn't fit into bits
911 // cleared by constant AND operand.
912 if (CN->getSExtValue() & CN1->getSExtValue())
913 return SDValue();
914
Strahinja Petrovicab9573f2017-05-22 09:06:44 +0000915 SDLoc DL(N);
916 EVT ValTy = N->getOperand(0)->getValueType(0);
917 SDValue Const1;
918 SDValue SrlX;
919 if (!isConstCase) {
920 Const1 = DAG.getConstant(SMPos0, DL, MVT::i32);
921 SrlX = DAG.getNode(ISD::SRL, DL, And1->getValueType(0), And1, Const1);
922 }
923 return DAG.getNode(
924 MipsISD::Ins, DL, N->getValueType(0),
925 isConstCase
926 ? DAG.getConstant(CN1->getSExtValue() >> SMPos0, DL, ValTy)
927 : SrlX,
928 DAG.getConstant(SMPos0, DL, MVT::i32),
929 DAG.getConstant(ValTy.getSizeInBits() / 8 < 8 ? SMSize0 & 31
930 : SMSize0,
931 DL, MVT::i32),
932 And0->getOperand(0));
933
934 }
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000935 return SDValue();
Strahinja Petrovicab9573f2017-05-22 09:06:44 +0000936 }
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000937}
Jia Liuf54f60f2012-02-28 07:46:26 +0000938
Simon Dardis250256f2017-07-13 11:28:05 +0000939static SDValue performMADD_MSUBCombine(SDNode *ROOTNode, SelectionDAG &CurDAG,
940 const MipsSubtarget &Subtarget) {
941 // ROOTNode must have a multiplication as an operand for the match to be
942 // successful.
943 if (ROOTNode->getOperand(0).getOpcode() != ISD::MUL &&
944 ROOTNode->getOperand(1).getOpcode() != ISD::MUL)
945 return SDValue();
946
947 // We don't handle vector types here.
948 if (ROOTNode->getValueType(0).isVector())
949 return SDValue();
950
951 // For MIPS64, madd / msub instructions are inefficent to use with 64 bit
952 // arithmetic. E.g.
953 // (add (mul a b) c) =>
954 // let res = (madd (mthi (drotr c 32))x(mtlo c) a b) in
955 // MIPS64: (or (dsll (mfhi res) 32) (dsrl (dsll (mflo res) 32) 32)
956 // or
957 // MIPS64R2: (dins (mflo res) (mfhi res) 32 32)
958 //
959 // The overhead of setting up the Hi/Lo registers and reassembling the
960 // result makes this a dubious optimzation for MIPS64. The core of the
961 // problem is that Hi/Lo contain the upper and lower 32 bits of the
962 // operand and result.
963 //
964 // It requires a chain of 4 add/mul for MIPS64R2 to get better code
965 // density than doing it naively, 5 for MIPS64. Additionally, using
966 // madd/msub on MIPS64 requires the operands actually be 32 bit sign
967 // extended operands, not true 64 bit values.
968 //
969 // FIXME: For the moment, disable this completely for MIPS64.
970 if (Subtarget.hasMips64())
971 return SDValue();
972
973 SDValue Mult = ROOTNode->getOperand(0).getOpcode() == ISD::MUL
974 ? ROOTNode->getOperand(0)
975 : ROOTNode->getOperand(1);
976
977 SDValue AddOperand = ROOTNode->getOperand(0).getOpcode() == ISD::MUL
978 ? ROOTNode->getOperand(1)
979 : ROOTNode->getOperand(0);
980
981 // Transform this to a MADD only if the user of this node is the add.
982 // If there are other users of the mul, this function returns here.
983 if (!Mult.hasOneUse())
984 return SDValue();
985
986 // maddu and madd are unusual instructions in that on MIPS64 bits 63..31
987 // must be in canonical form, i.e. sign extended. For MIPS32, the operands
988 // of the multiply must have 32 or more sign bits, otherwise we cannot
989 // perform this optimization. We have to check this here as we're performing
990 // this optimization pre-legalization.
991 SDValue MultLHS = Mult->getOperand(0);
992 SDValue MultRHS = Mult->getOperand(1);
993
994 bool IsSigned = MultLHS->getOpcode() == ISD::SIGN_EXTEND &&
995 MultRHS->getOpcode() == ISD::SIGN_EXTEND;
996 bool IsUnsigned = MultLHS->getOpcode() == ISD::ZERO_EXTEND &&
997 MultRHS->getOpcode() == ISD::ZERO_EXTEND;
998
999 if (!IsSigned && !IsUnsigned)
1000 return SDValue();
1001
1002 // Initialize accumulator.
1003 SDLoc DL(ROOTNode);
1004 SDValue TopHalf;
1005 SDValue BottomHalf;
1006 BottomHalf = CurDAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, AddOperand,
1007 CurDAG.getIntPtrConstant(0, DL));
1008
1009 TopHalf = CurDAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, AddOperand,
1010 CurDAG.getIntPtrConstant(1, DL));
1011 SDValue ACCIn = CurDAG.getNode(MipsISD::MTLOHI, DL, MVT::Untyped,
1012 BottomHalf,
1013 TopHalf);
1014
1015 // Create MipsMAdd(u) / MipsMSub(u) node.
1016 bool IsAdd = ROOTNode->getOpcode() == ISD::ADD;
1017 unsigned Opcode = IsAdd ? (IsUnsigned ? MipsISD::MAddu : MipsISD::MAdd)
1018 : (IsUnsigned ? MipsISD::MSubu : MipsISD::MSub);
1019 SDValue MAddOps[3] = {
1020 CurDAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Mult->getOperand(0)),
1021 CurDAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Mult->getOperand(1)), ACCIn};
1022 EVT VTs[2] = {MVT::i32, MVT::i32};
1023 SDValue MAdd = CurDAG.getNode(Opcode, DL, VTs, MAddOps);
1024
1025 SDValue ResLo = CurDAG.getNode(MipsISD::MFLO, DL, MVT::i32, MAdd);
1026 SDValue ResHi = CurDAG.getNode(MipsISD::MFHI, DL, MVT::i32, MAdd);
1027 SDValue Combined =
1028 CurDAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, ResLo, ResHi);
1029 return Combined;
1030}
1031
1032static SDValue performSUBCombine(SDNode *N, SelectionDAG &DAG,
1033 TargetLowering::DAGCombinerInfo &DCI,
1034 const MipsSubtarget &Subtarget) {
1035 // (sub v0 (mul v1, v2)) => (msub v1, v2, v0)
1036 if (DCI.isBeforeLegalizeOps()) {
1037 if (Subtarget.hasMips32() && !Subtarget.hasMips32r6() &&
1038 !Subtarget.inMips16Mode() && N->getValueType(0) == MVT::i64)
1039 return performMADD_MSUBCombine(N, DAG, Subtarget);
1040
1041 return SDValue();
1042 }
1043
1044 return SDValue();
1045}
1046
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001047static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanakadf5205e2012-06-13 20:33:18 +00001048 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +00001049 const MipsSubtarget &Subtarget) {
Simon Dardis250256f2017-07-13 11:28:05 +00001050 // (add v0 (mul v1, v2)) => (madd v1, v2, v0)
1051 if (DCI.isBeforeLegalizeOps()) {
1052 if (Subtarget.hasMips32() && !Subtarget.hasMips32r6() &&
1053 !Subtarget.inMips16Mode() && N->getValueType(0) == MVT::i64)
1054 return performMADD_MSUBCombine(N, DAG, Subtarget);
Simon Dardisdede76f2017-06-29 20:59:47 +00001055
Simon Dardisdede76f2017-06-29 20:59:47 +00001056 return SDValue();
Simon Dardis250256f2017-07-13 11:28:05 +00001057 }
Simon Dardisdede76f2017-06-29 20:59:47 +00001058
Simon Dardis250256f2017-07-13 11:28:05 +00001059 // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt))
Akira Hatanakadf5205e2012-06-13 20:33:18 +00001060 SDValue Add = N->getOperand(1);
1061
1062 if (Add.getOpcode() != ISD::ADD)
1063 return SDValue();
1064
1065 SDValue Lo = Add.getOperand(1);
1066
1067 if ((Lo.getOpcode() != MipsISD::Lo) ||
1068 (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable))
1069 return SDValue();
1070
1071 EVT ValTy = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001072 SDLoc DL(N);
Akira Hatanakadf5205e2012-06-13 20:33:18 +00001073
1074 SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, N->getOperand(0),
1075 Add.getOperand(0));
1076 return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo);
1077}
1078
Vasileios Kalintiris3751d412016-04-13 15:07:45 +00001079static SDValue performAssertZextCombine(SDNode *N, SelectionDAG &DAG,
1080 TargetLowering::DAGCombinerInfo &DCI,
1081 const MipsSubtarget &Subtarget) {
1082 SDValue N0 = N->getOperand(0);
1083 EVT NarrowerVT = cast<VTSDNode>(N->getOperand(1))->getVT();
1084
1085 if (N0.getOpcode() != ISD::TRUNCATE)
1086 return SDValue();
1087
1088 if (N0.getOperand(0).getOpcode() != ISD::AssertZext)
1089 return SDValue();
1090
1091 // fold (AssertZext (trunc (AssertZext x))) -> (trunc (AssertZext x))
1092 // if the type of the extension of the innermost AssertZext node is
1093 // smaller from that of the outermost node, eg:
1094 // (AssertZext:i32 (trunc:i32 (AssertZext:i64 X, i32)), i8)
1095 // -> (trunc:i32 (AssertZext X, i8))
1096 SDValue WiderAssertZext = N0.getOperand(0);
1097 EVT WiderVT = cast<VTSDNode>(WiderAssertZext->getOperand(1))->getVT();
1098
1099 if (NarrowerVT.bitsLT(WiderVT)) {
1100 SDValue NewAssertZext = DAG.getNode(
1101 ISD::AssertZext, SDLoc(N), WiderAssertZext.getValueType(),
1102 WiderAssertZext.getOperand(0), DAG.getValueType(NarrowerVT));
1103 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0),
1104 NewAssertZext);
1105 }
1106
1107 return SDValue();
1108}
1109
Petar Jovanovicb71386a2017-03-15 13:10:08 +00001110
1111static SDValue performSHLCombine(SDNode *N, SelectionDAG &DAG,
1112 TargetLowering::DAGCombinerInfo &DCI,
1113 const MipsSubtarget &Subtarget) {
1114 // Pattern match CINS.
1115 // $dst = shl (and $src , imm), pos
1116 // => cins $dst, $src, pos, size
1117
1118 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasCnMips())
1119 return SDValue();
1120
1121 SDValue FirstOperand = N->getOperand(0);
1122 unsigned FirstOperandOpc = FirstOperand.getOpcode();
1123 SDValue SecondOperand = N->getOperand(1);
1124 EVT ValTy = N->getValueType(0);
1125 SDLoc DL(N);
1126
1127 uint64_t Pos = 0, SMPos, SMSize;
1128 ConstantSDNode *CN;
1129 SDValue NewOperand;
1130
1131 // The second operand of the shift must be an immediate.
1132 if (!(CN = dyn_cast<ConstantSDNode>(SecondOperand)))
1133 return SDValue();
1134
1135 Pos = CN->getZExtValue();
1136
1137 if (Pos >= ValTy.getSizeInBits())
1138 return SDValue();
1139
1140 if (FirstOperandOpc != ISD::AND)
1141 return SDValue();
1142
1143 // AND's second operand must be a shifted mask.
1144 if (!(CN = dyn_cast<ConstantSDNode>(FirstOperand.getOperand(1))) ||
1145 !isShiftedMask(CN->getZExtValue(), SMPos, SMSize))
1146 return SDValue();
1147
1148 // Return if the shifted mask does not start at bit 0 or the sum of its size
1149 // and Pos exceeds the word's size.
1150 if (SMPos != 0 || SMSize > 32 || Pos + SMSize > ValTy.getSizeInBits())
1151 return SDValue();
1152
1153 NewOperand = FirstOperand.getOperand(0);
1154 // SMSize is 'location' (position) in this case, not size.
1155 SMSize--;
1156
1157 return DAG.getNode(MipsISD::CIns, DL, ValTy, NewOperand,
1158 DAG.getConstant(Pos, DL, MVT::i32),
1159 DAG.getConstant(SMSize, DL, MVT::i32));
1160}
1161
Bruno Cardoso Lopes61a61e92011-02-10 18:05:10 +00001162SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +00001163 const {
1164 SelectionDAG &DAG = DCI.DAG;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001165 unsigned Opc = N->getOpcode();
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +00001166
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001167 switch (Opc) {
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +00001168 default: break;
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +00001169 case ISD::SDIVREM:
1170 case ISD::UDIVREM:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001171 return performDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka7dd7c082012-03-08 02:14:24 +00001172 case ISD::SELECT:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001173 return performSELECTCombine(N, DAG, DCI, Subtarget);
Vasileios Kalintirise741eb22015-03-02 12:47:32 +00001174 case MipsISD::CMovFP_F:
1175 case MipsISD::CMovFP_T:
1176 return performCMovFPCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka184b63d2011-08-17 17:45:08 +00001177 case ISD::AND:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001178 return performANDCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka184b63d2011-08-17 17:45:08 +00001179 case ISD::OR:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001180 return performORCombine(N, DAG, DCI, Subtarget);
Akira Hatanakadf5205e2012-06-13 20:33:18 +00001181 case ISD::ADD:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001182 return performADDCombine(N, DAG, DCI, Subtarget);
Vasileios Kalintiris3751d412016-04-13 15:07:45 +00001183 case ISD::AssertZext:
1184 return performAssertZextCombine(N, DAG, DCI, Subtarget);
Petar Jovanovicb71386a2017-03-15 13:10:08 +00001185 case ISD::SHL:
1186 return performSHLCombine(N, DAG, DCI, Subtarget);
Simon Dardis250256f2017-07-13 11:28:05 +00001187 case ISD::SUB:
1188 return performSUBCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +00001189 }
1190
1191 return SDValue();
1192}
1193
Sanjay Patelf7401292015-11-11 17:24:56 +00001194bool MipsTargetLowering::isCheapToSpeculateCttz() const {
1195 return Subtarget.hasMips32();
1196}
1197
1198bool MipsTargetLowering::isCheapToSpeculateCtlz() const {
1199 return Subtarget.hasMips32();
1200}
1201
Akira Hatanakafabb8cf2012-09-21 23:58:31 +00001202void
1203MipsTargetLowering::LowerOperationWrapper(SDNode *N,
1204 SmallVectorImpl<SDValue> &Results,
1205 SelectionDAG &DAG) const {
1206 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
1207
1208 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
1209 Results.push_back(Res.getValue(I));
1210}
1211
1212void
1213MipsTargetLowering::ReplaceNodeResults(SDNode *N,
1214 SmallVectorImpl<SDValue> &Results,
1215 SelectionDAG &DAG) const {
Akira Hatanaka9da442f2013-04-30 21:17:07 +00001216 return LowerOperationWrapper(N, Results, DAG);
Akira Hatanakafabb8cf2012-09-21 23:58:31 +00001217}
1218
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001219SDValue MipsTargetLowering::
Dan Gohman21cea8a2010-04-17 15:26:15 +00001220LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001221{
Wesley Peck527da1b2010-11-23 03:31:01 +00001222 switch (Op.getOpcode())
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001223 {
Simon Dardisba92b032016-09-09 11:06:01 +00001224 case ISD::BRCOND: return lowerBRCOND(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +00001225 case ISD::ConstantPool: return lowerConstantPool(Op, DAG);
1226 case ISD::GlobalAddress: return lowerGlobalAddress(Op, DAG);
1227 case ISD::BlockAddress: return lowerBlockAddress(Op, DAG);
1228 case ISD::GlobalTLSAddress: return lowerGlobalTLSAddress(Op, DAG);
1229 case ISD::JumpTable: return lowerJumpTable(Op, DAG);
Simon Dardisba92b032016-09-09 11:06:01 +00001230 case ISD::SELECT: return lowerSELECT(Op, DAG);
1231 case ISD::SETCC: return lowerSETCC(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +00001232 case ISD::VASTART: return lowerVASTART(Op, DAG);
Daniel Sanders2b553d42014-08-01 09:17:39 +00001233 case ISD::VAARG: return lowerVAARG(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +00001234 case ISD::FCOPYSIGN: return lowerFCOPYSIGN(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +00001235 case ISD::FRAMEADDR: return lowerFRAMEADDR(Op, DAG);
1236 case ISD::RETURNADDR: return lowerRETURNADDR(Op, DAG);
1237 case ISD::EH_RETURN: return lowerEH_RETURN(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +00001238 case ISD::ATOMIC_FENCE: return lowerATOMIC_FENCE(Op, DAG);
1239 case ISD::SHL_PARTS: return lowerShiftLeftParts(Op, DAG);
1240 case ISD::SRA_PARTS: return lowerShiftRightParts(Op, DAG, true);
1241 case ISD::SRL_PARTS: return lowerShiftRightParts(Op, DAG, false);
1242 case ISD::LOAD: return lowerLOAD(Op, DAG);
1243 case ISD::STORE: return lowerSTORE(Op, DAG);
Hal Finkel5081ac22016-09-01 10:28:47 +00001244 case ISD::EH_DWARF_CFA: return lowerEH_DWARF_CFA(Op, DAG);
Akira Hatanaka252f54f2013-05-16 21:17:15 +00001245 case ISD::FP_TO_SINT: return lowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001246 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001247 return SDValue();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001248}
1249
Akira Hatanakae2489122011-04-15 21:51:11 +00001250//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001251// Lower helper functions
Akira Hatanakae2489122011-04-15 21:51:11 +00001252//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001253
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001254// addLiveIn - This helper function adds the specified physical register to the
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001255// MachineFunction as a live in value. It also creates a corresponding
1256// virtual register for it.
1257static unsigned
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001258addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001259{
Chris Lattnera10fff52007-12-31 04:13:23 +00001260 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
1261 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001262 return VReg;
1263}
1264
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001265static MachineBasicBlock *insertDivByZeroTrap(MachineInstr &MI,
Daniel Sanders308181e2014-06-12 10:44:10 +00001266 MachineBasicBlock &MBB,
1267 const TargetInstrInfo &TII,
Zlatko Buljan58d6a952016-04-13 08:02:26 +00001268 bool Is64Bit, bool IsMicroMips) {
Akira Hatanaka1cb02422013-05-20 18:07:43 +00001269 if (NoZeroDivCheck)
1270 return &MBB;
1271
1272 // Insert instruction "teq $divisor_reg, $zero, 7".
1273 MachineBasicBlock::iterator I(MI);
1274 MachineInstrBuilder MIB;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001275 MachineOperand &Divisor = MI.getOperand(2);
1276 MIB = BuildMI(MBB, std::next(I), MI.getDebugLoc(),
Zlatko Buljan58d6a952016-04-13 08:02:26 +00001277 TII.get(IsMicroMips ? Mips::TEQ_MM : Mips::TEQ))
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001278 .addReg(Divisor.getReg(), getKillRegState(Divisor.isKill()))
1279 .addReg(Mips::ZERO)
1280 .addImm(7);
Akira Hatanaka1cb02422013-05-20 18:07:43 +00001281
1282 // Use the 32-bit sub-register if this is a 64-bit division.
1283 if (Is64Bit)
1284 MIB->getOperand(0).setSubReg(Mips::sub_32);
1285
Akira Hatanaka86c3c792013-10-15 01:06:30 +00001286 // Clear Divisor's kill flag.
1287 Divisor.setIsKill(false);
Daniel Sanders308181e2014-06-12 10:44:10 +00001288
1289 // We would normally delete the original instruction here but in this case
1290 // we only needed to inject an additional instruction rather than replace it.
1291
Akira Hatanaka1cb02422013-05-20 18:07:43 +00001292 return &MBB;
1293}
1294
Akira Hatanakae4bd0542012-09-27 02:15:57 +00001295MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001296MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
Dan Gohman25c16532010-05-01 00:01:06 +00001297 MachineBasicBlock *BB) const {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001298 switch (MI.getOpcode()) {
Reed Kotler97ba5f22013-02-21 04:22:38 +00001299 default:
1300 llvm_unreachable("Unexpected instr type to insert");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001301 case Mips::ATOMIC_LOAD_ADD_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001302 return emitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001303 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001304 return emitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001305 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001306 return emitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001307 case Mips::ATOMIC_LOAD_ADD_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001308 return emitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001309
1310 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001311 return emitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001312 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001313 return emitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001314 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001315 return emitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001316 case Mips::ATOMIC_LOAD_AND_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001317 return emitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001318
1319 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001320 return emitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001321 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001322 return emitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001323 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001324 return emitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001325 case Mips::ATOMIC_LOAD_OR_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001326 return emitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001327
1328 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001329 return emitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001330 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001331 return emitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001332 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001333 return emitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001334 case Mips::ATOMIC_LOAD_XOR_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001335 return emitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001336
1337 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001338 return emitAtomicBinaryPartword(MI, BB, 1, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001339 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001340 return emitAtomicBinaryPartword(MI, BB, 2, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001341 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001342 return emitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001343 case Mips::ATOMIC_LOAD_NAND_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001344 return emitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001345
1346 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001347 return emitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001348 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001349 return emitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001350 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001351 return emitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001352 case Mips::ATOMIC_LOAD_SUB_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001353 return emitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001354
1355 case Mips::ATOMIC_SWAP_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001356 return emitAtomicBinaryPartword(MI, BB, 1, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001357 case Mips::ATOMIC_SWAP_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001358 return emitAtomicBinaryPartword(MI, BB, 2, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001359 case Mips::ATOMIC_SWAP_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001360 return emitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001361 case Mips::ATOMIC_SWAP_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001362 return emitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001363
Simon Dardis7577ce22017-03-09 14:03:26 +00001364 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001365 return emitAtomicCmpSwapPartword(MI, BB, 1);
Simon Dardis7577ce22017-03-09 14:03:26 +00001366 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001367 return emitAtomicCmpSwapPartword(MI, BB, 2);
Simon Dardis7577ce22017-03-09 14:03:26 +00001368 case Mips::ATOMIC_CMP_SWAP_I32:
1369 return emitAtomicCmpSwap(MI, BB, 4);
1370 case Mips::ATOMIC_CMP_SWAP_I64:
1371 return emitAtomicCmpSwap(MI, BB, 8);
Akira Hatanaka1cb02422013-05-20 18:07:43 +00001372 case Mips::PseudoSDIV:
1373 case Mips::PseudoUDIV:
Daniel Sanders308181e2014-06-12 10:44:10 +00001374 case Mips::DIV:
1375 case Mips::DIVU:
1376 case Mips::MOD:
1377 case Mips::MODU:
Zlatko Buljan58d6a952016-04-13 08:02:26 +00001378 return insertDivByZeroTrap(MI, *BB, *Subtarget.getInstrInfo(), false,
1379 false);
1380 case Mips::SDIV_MM_Pseudo:
1381 case Mips::UDIV_MM_Pseudo:
1382 case Mips::SDIV_MM:
1383 case Mips::UDIV_MM:
1384 case Mips::DIV_MMR6:
1385 case Mips::DIVU_MMR6:
1386 case Mips::MOD_MMR6:
1387 case Mips::MODU_MMR6:
1388 return insertDivByZeroTrap(MI, *BB, *Subtarget.getInstrInfo(), false, true);
Akira Hatanaka1cb02422013-05-20 18:07:43 +00001389 case Mips::PseudoDSDIV:
1390 case Mips::PseudoDUDIV:
Daniel Sanders308181e2014-06-12 10:44:10 +00001391 case Mips::DDIV:
1392 case Mips::DDIVU:
1393 case Mips::DMOD:
1394 case Mips::DMODU:
Zlatko Buljan58d6a952016-04-13 08:02:26 +00001395 return insertDivByZeroTrap(MI, *BB, *Subtarget.getInstrInfo(), true, false);
1396 case Mips::DDIV_MM64R6:
1397 case Mips::DDIVU_MM64R6:
1398 case Mips::DMOD_MM64R6:
1399 case Mips::DMODU_MM64R6:
1400 return insertDivByZeroTrap(MI, *BB, *Subtarget.getInstrInfo(), true, true);
Daniel Sanders0fa60412014-06-12 13:39:06 +00001401 case Mips::SEL_D:
Zlatko Buljancd242c12016-06-09 11:15:53 +00001402 case Mips::SEL_D_MMR6:
Daniel Sanders0fa60412014-06-12 13:39:06 +00001403 return emitSEL_D(MI, BB);
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001404
1405 case Mips::PseudoSELECT_I:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001406 case Mips::PseudoSELECT_I64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001407 case Mips::PseudoSELECT_S:
1408 case Mips::PseudoSELECT_D32:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001409 case Mips::PseudoSELECT_D64:
Vasileios Kalintiris36901dd2016-03-01 20:25:43 +00001410 return emitPseudoSELECT(MI, BB, false, Mips::BNE);
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001411 case Mips::PseudoSELECTFP_F_I:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001412 case Mips::PseudoSELECTFP_F_I64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001413 case Mips::PseudoSELECTFP_F_S:
1414 case Mips::PseudoSELECTFP_F_D32:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001415 case Mips::PseudoSELECTFP_F_D64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001416 return emitPseudoSELECT(MI, BB, true, Mips::BC1F);
1417 case Mips::PseudoSELECTFP_T_I:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001418 case Mips::PseudoSELECTFP_T_I64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001419 case Mips::PseudoSELECTFP_T_S:
1420 case Mips::PseudoSELECTFP_T_D32:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001421 case Mips::PseudoSELECTFP_T_D64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001422 return emitPseudoSELECT(MI, BB, true, Mips::BC1T);
Akira Hatanakaa5352702011-03-31 18:26:17 +00001423 }
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001424}
1425
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001426// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
1427// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001428MachineBasicBlock *MipsTargetLowering::emitAtomicBinary(MachineInstr &MI,
1429 MachineBasicBlock *BB,
1430 unsigned Size,
1431 unsigned BinOpcode,
1432 bool Nand) const {
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001433 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001434
1435 MachineFunction *MF = BB->getParent();
1436 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001437 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Eric Christopher96e72c62015-01-29 23:27:36 +00001438 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001439 const bool ArePtrs64bit = ABI.ArePtrs64bit();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001440 DebugLoc DL = MI.getDebugLoc();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001441 unsigned LL, SC, AND, NOR, ZERO, BEQ;
1442
1443 if (Size == 4) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001444 if (isMicroMips) {
1445 LL = Mips::LL_MM;
1446 SC = Mips::SC_MM;
1447 } else {
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001448 LL = Subtarget.hasMips32r6()
1449 ? (ArePtrs64bit ? Mips::LL64_R6 : Mips::LL_R6)
1450 : (ArePtrs64bit ? Mips::LL64 : Mips::LL);
1451 SC = Subtarget.hasMips32r6()
1452 ? (ArePtrs64bit ? Mips::SC64_R6 : Mips::SC_R6)
1453 : (ArePtrs64bit ? Mips::SC64 : Mips::SC);
Daniel Sanders6a803f62014-06-16 13:13:03 +00001454 }
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001455
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001456 AND = Mips::AND;
1457 NOR = Mips::NOR;
1458 ZERO = Mips::ZERO;
1459 BEQ = Mips::BEQ;
Daniel Sanders6a803f62014-06-16 13:13:03 +00001460 } else {
Daniel Sandersbdcfab12014-07-24 09:47:14 +00001461 LL = Subtarget.hasMips64r6() ? Mips::LLD_R6 : Mips::LLD;
1462 SC = Subtarget.hasMips64r6() ? Mips::SCD_R6 : Mips::SCD;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001463 AND = Mips::AND64;
1464 NOR = Mips::NOR64;
1465 ZERO = Mips::ZERO_64;
1466 BEQ = Mips::BEQ64;
1467 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001468
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001469 unsigned OldVal = MI.getOperand(0).getReg();
1470 unsigned Ptr = MI.getOperand(1).getReg();
1471 unsigned Incr = MI.getOperand(2).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001472
Akira Hatanaka0e019592011-07-19 20:11:17 +00001473 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1474 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1475 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001476
1477 // insert new blocks after the current block
1478 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1479 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1480 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +00001481 MachineFunction::iterator It = ++BB->getIterator();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001482 MF->insert(It, loopMBB);
1483 MF->insert(It, exitMBB);
1484
1485 // Transfer the remainder of BB and its successor edges to exitMBB.
1486 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001487 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001488 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1489
1490 // thisMBB:
1491 // ...
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001492 // fallthrough --> loopMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001493 BB->addSuccessor(loopMBB);
Akira Hatanaka08636b42011-07-19 17:09:53 +00001494 loopMBB->addSuccessor(loopMBB);
1495 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001496
1497 // loopMBB:
1498 // ll oldval, 0(ptr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001499 // <binop> storeval, oldval, incr
1500 // sc success, storeval, 0(ptr)
1501 // beq success, $0, loopMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001502 BB = loopMBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001503 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001504 if (Nand) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001505 // and andres, oldval, incr
1506 // nor storeval, $0, andres
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001507 BuildMI(BB, DL, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
1508 BuildMI(BB, DL, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001509 } else if (BinOpcode) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001510 // <binop> storeval, oldval, incr
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001511 BuildMI(BB, DL, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001512 } else {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001513 StoreVal = Incr;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001514 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001515 BuildMI(BB, DL, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
1516 BuildMI(BB, DL, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001517
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001518 MI.eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001519
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001520 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001521}
1522
Daniel Sanders6a803f62014-06-16 13:13:03 +00001523MachineBasicBlock *MipsTargetLowering::emitSignExtendToI32InReg(
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001524 MachineInstr &MI, MachineBasicBlock *BB, unsigned Size, unsigned DstReg,
Daniel Sanders6a803f62014-06-16 13:13:03 +00001525 unsigned SrcReg) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00001526 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001527 const DebugLoc &DL = MI.getDebugLoc();
Daniel Sanders6a803f62014-06-16 13:13:03 +00001528
Eric Christopher1c29a652014-07-18 22:55:25 +00001529 if (Subtarget.hasMips32r2() && Size == 1) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001530 BuildMI(BB, DL, TII->get(Mips::SEB), DstReg).addReg(SrcReg);
1531 return BB;
1532 }
1533
Eric Christopher1c29a652014-07-18 22:55:25 +00001534 if (Subtarget.hasMips32r2() && Size == 2) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001535 BuildMI(BB, DL, TII->get(Mips::SEH), DstReg).addReg(SrcReg);
1536 return BB;
1537 }
1538
1539 MachineFunction *MF = BB->getParent();
1540 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1541 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1542 unsigned ScrReg = RegInfo.createVirtualRegister(RC);
1543
1544 assert(Size < 32);
1545 int64_t ShiftImm = 32 - (Size * 8);
1546
1547 BuildMI(BB, DL, TII->get(Mips::SLL), ScrReg).addReg(SrcReg).addImm(ShiftImm);
1548 BuildMI(BB, DL, TII->get(Mips::SRA), DstReg).addReg(ScrReg).addImm(ShiftImm);
1549
1550 return BB;
1551}
1552
1553MachineBasicBlock *MipsTargetLowering::emitAtomicBinaryPartword(
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001554 MachineInstr &MI, MachineBasicBlock *BB, unsigned Size, unsigned BinOpcode,
Daniel Sanders6a803f62014-06-16 13:13:03 +00001555 bool Nand) const {
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001556 assert((Size == 1 || Size == 2) &&
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001557 "Unsupported size for EmitAtomicBinaryPartial.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001558
1559 MachineFunction *MF = BB->getParent();
1560 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1561 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001562 const bool ArePtrs64bit = ABI.ArePtrs64bit();
Simon Dardisa2d8cc32016-04-28 16:26:43 +00001563 const TargetRegisterClass *RCp =
1564 getRegClassFor(ArePtrs64bit ? MVT::i64 : MVT::i32);
Eric Christopher96e72c62015-01-29 23:27:36 +00001565 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001566 DebugLoc DL = MI.getDebugLoc();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001567
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001568 unsigned Dest = MI.getOperand(0).getReg();
1569 unsigned Ptr = MI.getOperand(1).getReg();
1570 unsigned Incr = MI.getOperand(2).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001571
Simon Dardisa2d8cc32016-04-28 16:26:43 +00001572 unsigned AlignedAddr = RegInfo.createVirtualRegister(RCp);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001573 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001574 unsigned Mask = RegInfo.createVirtualRegister(RC);
1575 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001576 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1577 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001578 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Simon Dardisa2d8cc32016-04-28 16:26:43 +00001579 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RCp);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001580 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1581 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1582 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1583 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka9663dd32011-07-19 20:56:53 +00001584 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001585 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1586 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1587 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001588 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001589
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001590 unsigned LL, SC;
1591 if (isMicroMips) {
1592 LL = Mips::LL_MM;
1593 SC = Mips::SC_MM;
1594 } else {
1595 LL = Subtarget.hasMips32r6() ? (ArePtrs64bit ? Mips::LL64_R6 : Mips::LL_R6)
1596 : (ArePtrs64bit ? Mips::LL64 : Mips::LL);
1597 SC = Subtarget.hasMips32r6() ? (ArePtrs64bit ? Mips::SC64_R6 : Mips::SC_R6)
1598 : (ArePtrs64bit ? Mips::SC64 : Mips::SC);
1599 }
1600
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001601 // insert new blocks after the current block
1602 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1603 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001604 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001605 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +00001606 MachineFunction::iterator It = ++BB->getIterator();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001607 MF->insert(It, loopMBB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001608 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001609 MF->insert(It, exitMBB);
1610
1611 // Transfer the remainder of BB and its successor edges to exitMBB.
1612 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001613 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001614 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1615
Akira Hatanaka08636b42011-07-19 17:09:53 +00001616 BB->addSuccessor(loopMBB);
1617 loopMBB->addSuccessor(loopMBB);
1618 loopMBB->addSuccessor(sinkMBB);
1619 sinkMBB->addSuccessor(exitMBB);
1620
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001621 // thisMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001622 // addiu masklsb2,$0,-4 # 0xfffffffc
1623 // and alignedaddr,ptr,masklsb2
1624 // andi ptrlsb2,ptr,3
1625 // sll shiftamt,ptrlsb2,3
1626 // ori maskupper,$0,255 # 0xff
1627 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001628 // nor mask2,$0,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001629 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001630
1631 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Simon Dardisa2d8cc32016-04-28 16:26:43 +00001632 BuildMI(BB, DL, TII->get(ABI.GetPtrAddiuOp()), MaskLSB2)
1633 .addReg(ABI.GetNullPtr()).addImm(-4);
1634 BuildMI(BB, DL, TII->get(ABI.GetPtrAndOp()), AlignedAddr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001635 .addReg(Ptr).addReg(MaskLSB2);
Simon Dardisa2d8cc32016-04-28 16:26:43 +00001636 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2)
1637 .addReg(Ptr, 0, ArePtrs64bit ? Mips::sub_32 : 0).addImm(3);
Eric Christopher1c29a652014-07-18 22:55:25 +00001638 if (Subtarget.isLittle()) {
Akira Hatanaka2bf97332013-05-31 03:25:44 +00001639 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1640 } else {
1641 unsigned Off = RegInfo.createVirtualRegister(RC);
1642 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1643 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1644 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1645 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001646 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001647 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001648 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001649 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001650 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001651 BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(Incr).addReg(ShiftAmt);
Bruno Cardoso Lopesf771a0f2011-05-31 20:25:26 +00001652
Akira Hatanaka27292632011-07-18 18:52:12 +00001653 // atomic.load.binop
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001654 // loopMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001655 // ll oldval,0(alignedaddr)
1656 // binop binopres,oldval,incr2
1657 // and newval,binopres,mask
1658 // and maskedoldval0,oldval,mask2
1659 // or storeval,maskedoldval0,newval
1660 // sc success,storeval,0(alignedaddr)
1661 // beq success,$0,loopMBB
1662
Akira Hatanaka27292632011-07-18 18:52:12 +00001663 // atomic.swap
1664 // loopMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001665 // ll oldval,0(alignedaddr)
Akira Hatanakae4503582011-07-19 18:14:26 +00001666 // and newval,incr2,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001667 // and maskedoldval0,oldval,mask2
1668 // or storeval,maskedoldval0,newval
1669 // sc success,storeval,0(alignedaddr)
1670 // beq success,$0,loopMBB
Akira Hatanaka27292632011-07-18 18:52:12 +00001671
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001672 BB = loopMBB;
Jozef Kolek2f27d572014-12-18 16:39:29 +00001673 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001674 if (Nand) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001675 // and andres, oldval, incr2
1676 // nor binopres, $0, andres
1677 // and newval, binopres, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001678 BuildMI(BB, DL, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1679 BuildMI(BB, DL, TII->get(Mips::NOR), BinOpRes)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001680 .addReg(Mips::ZERO).addReg(AndRes);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001681 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001682 } else if (BinOpcode) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001683 // <binop> binopres, oldval, incr2
1684 // and newval, binopres, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001685 BuildMI(BB, DL, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1686 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001687 } else { // atomic.swap
Akira Hatanaka0e019592011-07-19 20:11:17 +00001688 // and newval, incr2, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001689 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanakae4503582011-07-19 18:14:26 +00001690 }
Jia Liuf54f60f2012-02-28 07:46:26 +00001691
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001692 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001693 .addReg(OldVal).addReg(Mask2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001694 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka9663dd32011-07-19 20:56:53 +00001695 .addReg(MaskedOldVal0).addReg(NewVal);
Jozef Kolek2f27d572014-12-18 16:39:29 +00001696 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001697 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001698 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001699 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001700
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001701 // sinkMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001702 // and maskedoldval1,oldval,mask
1703 // srl srlres,maskedoldval1,shiftamt
Daniel Sanders6a803f62014-06-16 13:13:03 +00001704 // sign_extend dest,srlres
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001705 BB = sinkMBB;
Akira Hatanakae97bd812011-07-19 03:14:58 +00001706
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001707 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001708 .addReg(OldVal).addReg(Mask);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001709 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001710 .addReg(MaskedOldVal1).addReg(ShiftAmt);
Daniel Sanders6a803f62014-06-16 13:13:03 +00001711 BB = emitSignExtendToI32InReg(MI, BB, Size, Dest, SrlRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001712
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001713 MI.eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001714
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001715 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001716}
1717
Simon Dardis7577ce22017-03-09 14:03:26 +00001718MachineBasicBlock *MipsTargetLowering::emitAtomicCmpSwap(MachineInstr &MI,
1719 MachineBasicBlock *BB,
1720 unsigned Size) const {
1721 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
1722
1723 MachineFunction *MF = BB->getParent();
1724 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1725 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
1726 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
1727 const bool ArePtrs64bit = ABI.ArePtrs64bit();
1728 DebugLoc DL = MI.getDebugLoc();
1729 unsigned LL, SC, ZERO, BNE, BEQ;
1730
1731 if (Size == 4) {
1732 if (isMicroMips) {
1733 LL = Mips::LL_MM;
1734 SC = Mips::SC_MM;
1735 } else {
1736 LL = Subtarget.hasMips32r6()
1737 ? (ArePtrs64bit ? Mips::LL64_R6 : Mips::LL_R6)
1738 : (ArePtrs64bit ? Mips::LL64 : Mips::LL);
1739 SC = Subtarget.hasMips32r6()
1740 ? (ArePtrs64bit ? Mips::SC64_R6 : Mips::SC_R6)
1741 : (ArePtrs64bit ? Mips::SC64 : Mips::SC);
1742 }
1743
1744 ZERO = Mips::ZERO;
1745 BNE = Mips::BNE;
1746 BEQ = Mips::BEQ;
1747 } else {
1748 LL = Subtarget.hasMips64r6() ? Mips::LLD_R6 : Mips::LLD;
1749 SC = Subtarget.hasMips64r6() ? Mips::SCD_R6 : Mips::SCD;
1750 ZERO = Mips::ZERO_64;
1751 BNE = Mips::BNE64;
1752 BEQ = Mips::BEQ64;
1753 }
1754
1755 unsigned Dest = MI.getOperand(0).getReg();
1756 unsigned Ptr = MI.getOperand(1).getReg();
1757 unsigned OldVal = MI.getOperand(2).getReg();
1758 unsigned NewVal = MI.getOperand(3).getReg();
1759
1760 unsigned Success = RegInfo.createVirtualRegister(RC);
1761
1762 // insert new blocks after the current block
1763 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1764 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1765 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1766 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1767 MachineFunction::iterator It = ++BB->getIterator();
1768 MF->insert(It, loop1MBB);
1769 MF->insert(It, loop2MBB);
1770 MF->insert(It, exitMBB);
1771
1772 // Transfer the remainder of BB and its successor edges to exitMBB.
1773 exitMBB->splice(exitMBB->begin(), BB,
1774 std::next(MachineBasicBlock::iterator(MI)), BB->end());
1775 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1776
1777 // thisMBB:
1778 // ...
1779 // fallthrough --> loop1MBB
1780 BB->addSuccessor(loop1MBB);
1781 loop1MBB->addSuccessor(exitMBB);
1782 loop1MBB->addSuccessor(loop2MBB);
1783 loop2MBB->addSuccessor(loop1MBB);
1784 loop2MBB->addSuccessor(exitMBB);
1785
1786 // loop1MBB:
1787 // ll dest, 0(ptr)
1788 // bne dest, oldval, exitMBB
1789 BB = loop1MBB;
1790 BuildMI(BB, DL, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1791 BuildMI(BB, DL, TII->get(BNE))
1792 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
1793
1794 // loop2MBB:
1795 // sc success, newval, 0(ptr)
1796 // beq success, $0, loop1MBB
1797 BB = loop2MBB;
1798 BuildMI(BB, DL, TII->get(SC), Success)
1799 .addReg(NewVal).addReg(Ptr).addImm(0);
1800 BuildMI(BB, DL, TII->get(BEQ))
1801 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
1802
1803 MI.eraseFromParent(); // The instruction is gone now.
1804
1805 return exitMBB;
1806}
1807
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001808MachineBasicBlock *MipsTargetLowering::emitAtomicCmpSwapPartword(
1809 MachineInstr &MI, MachineBasicBlock *BB, unsigned Size) const {
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001810 assert((Size == 1 || Size == 2) &&
1811 "Unsupported size for EmitAtomicCmpSwapPartial.");
1812
1813 MachineFunction *MF = BB->getParent();
1814 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1815 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001816 const bool ArePtrs64bit = ABI.ArePtrs64bit();
Zoran Jovanovic2f6845b2016-04-13 16:02:25 +00001817 const TargetRegisterClass *RCp =
1818 getRegClassFor(ArePtrs64bit ? MVT::i64 : MVT::i32);
Eric Christopher96e72c62015-01-29 23:27:36 +00001819 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001820 DebugLoc DL = MI.getDebugLoc();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001821
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001822 unsigned Dest = MI.getOperand(0).getReg();
1823 unsigned Ptr = MI.getOperand(1).getReg();
1824 unsigned CmpVal = MI.getOperand(2).getReg();
1825 unsigned NewVal = MI.getOperand(3).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001826
Zoran Jovanovic2f6845b2016-04-13 16:02:25 +00001827 unsigned AlignedAddr = RegInfo.createVirtualRegister(RCp);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001828 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001829 unsigned Mask = RegInfo.createVirtualRegister(RC);
1830 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001831 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
Simon Dardis7577ce22017-03-09 14:03:26 +00001832 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1833 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001834 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
Zoran Jovanovic2f6845b2016-04-13 16:02:25 +00001835 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RCp);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001836 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1837 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1838 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1839 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
Simon Dardis7577ce22017-03-09 14:03:26 +00001840 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1841 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1842 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1843 unsigned Success = RegInfo.createVirtualRegister(RC);
1844 unsigned LL, SC;
1845
1846 if (isMicroMips) {
1847 LL = Mips::LL_MM;
1848 SC = Mips::SC_MM;
1849 } else {
1850 LL = Subtarget.hasMips32r6() ? (ArePtrs64bit ? Mips::LL64_R6 : Mips::LL_R6)
1851 : (ArePtrs64bit ? Mips::LL64 : Mips::LL);
1852 SC = Subtarget.hasMips32r6() ? (ArePtrs64bit ? Mips::SC64_R6 : Mips::SC_R6)
1853 : (ArePtrs64bit ? Mips::SC64 : Mips::SC);
1854 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001855
1856 // insert new blocks after the current block
1857 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Simon Dardis7577ce22017-03-09 14:03:26 +00001858 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1859 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1860 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001861 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +00001862 MachineFunction::iterator It = ++BB->getIterator();
Simon Dardis7577ce22017-03-09 14:03:26 +00001863 MF->insert(It, loop1MBB);
1864 MF->insert(It, loop2MBB);
1865 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001866 MF->insert(It, exitMBB);
1867
1868 // Transfer the remainder of BB and its successor edges to exitMBB.
1869 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001870 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001871 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1872
Simon Dardis7577ce22017-03-09 14:03:26 +00001873 BB->addSuccessor(loop1MBB);
1874 loop1MBB->addSuccessor(sinkMBB);
1875 loop1MBB->addSuccessor(loop2MBB);
1876 loop2MBB->addSuccessor(loop1MBB);
1877 loop2MBB->addSuccessor(sinkMBB);
1878 sinkMBB->addSuccessor(exitMBB);
Akira Hatanaka08636b42011-07-19 17:09:53 +00001879
Akira Hatanakae4503582011-07-19 18:14:26 +00001880 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001881 // thisMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001882 // addiu masklsb2,$0,-4 # 0xfffffffc
1883 // and alignedaddr,ptr,masklsb2
1884 // andi ptrlsb2,ptr,3
Zoran Jovanovic2f6845b2016-04-13 16:02:25 +00001885 // xori ptrlsb2,ptrlsb2,3 # Only for BE
Akira Hatanaka0e019592011-07-19 20:11:17 +00001886 // sll shiftamt,ptrlsb2,3
1887 // ori maskupper,$0,255 # 0xff
1888 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001889 // nor mask2,$0,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001890 // andi maskedcmpval,cmpval,255
1891 // sll shiftedcmpval,maskedcmpval,shiftamt
1892 // andi maskednewval,newval,255
1893 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001894 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Zoran Jovanovic2f6845b2016-04-13 16:02:25 +00001895 BuildMI(BB, DL, TII->get(ArePtrs64bit ? Mips::DADDiu : Mips::ADDiu), MaskLSB2)
1896 .addReg(ABI.GetNullPtr()).addImm(-4);
1897 BuildMI(BB, DL, TII->get(ArePtrs64bit ? Mips::AND64 : Mips::AND), AlignedAddr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001898 .addReg(Ptr).addReg(MaskLSB2);
Zoran Jovanovic2f6845b2016-04-13 16:02:25 +00001899 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2)
1900 .addReg(Ptr, 0, ArePtrs64bit ? Mips::sub_32 : 0).addImm(3);
Eric Christopher1c29a652014-07-18 22:55:25 +00001901 if (Subtarget.isLittle()) {
Akira Hatanaka2bf97332013-05-31 03:25:44 +00001902 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1903 } else {
1904 unsigned Off = RegInfo.createVirtualRegister(RC);
1905 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1906 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1907 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1908 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001909 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001910 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001911 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001912 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001913 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1914 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001915 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001916 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001917 .addReg(MaskedCmpVal).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001918 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001919 .addReg(NewVal).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001920 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001921 .addReg(MaskedNewVal).addReg(ShiftAmt);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001922
Simon Dardis7577ce22017-03-09 14:03:26 +00001923 // loop1MBB:
1924 // ll oldval,0(alginedaddr)
1925 // and maskedoldval0,oldval,mask
1926 // bne maskedoldval0,shiftedcmpval,sinkMBB
1927 BB = loop1MBB;
1928 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
1929 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
1930 .addReg(OldVal).addReg(Mask);
1931 BuildMI(BB, DL, TII->get(Mips::BNE))
1932 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
1933
1934 // loop2MBB:
1935 // and maskedoldval1,oldval,mask2
1936 // or storeval,maskedoldval1,shiftednewval
1937 // sc success,storeval,0(alignedaddr)
1938 // beq success,$0,loop1MBB
1939 BB = loop2MBB;
1940 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
1941 .addReg(OldVal).addReg(Mask2);
1942 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
1943 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
1944 BuildMI(BB, DL, TII->get(SC), Success)
1945 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
1946 BuildMI(BB, DL, TII->get(Mips::BEQ))
1947 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
1948
1949 // sinkMBB:
1950 // srl srlres,maskedoldval0,shiftamt
1951 // sign_extend dest,srlres
1952 BB = sinkMBB;
1953
1954 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
1955 .addReg(MaskedOldVal0).addReg(ShiftAmt);
1956 BB = emitSignExtendToI32InReg(MI, BB, Size, Dest, SrlRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001957
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001958 MI.eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001959
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001960 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001961}
1962
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001963MachineBasicBlock *MipsTargetLowering::emitSEL_D(MachineInstr &MI,
Daniel Sanders0fa60412014-06-12 13:39:06 +00001964 MachineBasicBlock *BB) const {
1965 MachineFunction *MF = BB->getParent();
Eric Christopher96e72c62015-01-29 23:27:36 +00001966 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
1967 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders0fa60412014-06-12 13:39:06 +00001968 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001969 DebugLoc DL = MI.getDebugLoc();
Daniel Sanders0fa60412014-06-12 13:39:06 +00001970 MachineBasicBlock::iterator II(MI);
1971
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001972 unsigned Fc = MI.getOperand(1).getReg();
Daniel Sanders0fa60412014-06-12 13:39:06 +00001973 const auto &FGR64RegClass = TRI->getRegClass(Mips::FGR64RegClassID);
1974
1975 unsigned Fc2 = RegInfo.createVirtualRegister(FGR64RegClass);
1976
1977 BuildMI(*BB, II, DL, TII->get(Mips::SUBREG_TO_REG), Fc2)
1978 .addImm(0)
1979 .addReg(Fc)
1980 .addImm(Mips::sub_lo);
1981
1982 // We don't erase the original instruction, we just replace the condition
1983 // register with the 64-bit super-register.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001984 MI.getOperand(1).setReg(Fc2);
Daniel Sanders0fa60412014-06-12 13:39:06 +00001985
1986 return BB;
1987}
1988
Simon Dardisba92b032016-09-09 11:06:01 +00001989SDValue MipsTargetLowering::lowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
1990 // The first operand is the chain, the second is the condition, the third is
1991 // the block to branch to if the condition is true.
1992 SDValue Chain = Op.getOperand(0);
1993 SDValue Dest = Op.getOperand(2);
1994 SDLoc DL(Op);
1995
1996 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
1997 SDValue CondRes = createFPCmp(DAG, Op.getOperand(1));
1998
1999 // Return if flag is not set by a floating point comparison.
2000 if (CondRes.getOpcode() != MipsISD::FPCmp)
2001 return Op;
2002
2003 SDValue CCNode = CondRes.getOperand(2);
2004 Mips::CondCode CC =
2005 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
2006 unsigned Opc = invertFPCondCodeUser(CC) ? Mips::BRANCH_F : Mips::BRANCH_T;
2007 SDValue BrCode = DAG.getConstant(Opc, DL, MVT::i32);
2008 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
2009 return DAG.getNode(MipsISD::FPBrcond, DL, Op.getValueType(), Chain, BrCode,
2010 FCC0, Dest, CondRes);
2011}
2012
2013SDValue MipsTargetLowering::
2014lowerSELECT(SDValue Op, SelectionDAG &DAG) const
2015{
2016 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
2017 SDValue Cond = createFPCmp(DAG, Op.getOperand(0));
2018
2019 // Return if flag is not set by a floating point comparison.
2020 if (Cond.getOpcode() != MipsISD::FPCmp)
2021 return Op;
2022
2023 return createCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
2024 SDLoc(Op));
2025}
2026
2027SDValue MipsTargetLowering::lowerSETCC(SDValue Op, SelectionDAG &DAG) const {
2028 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
2029 SDValue Cond = createFPCmp(DAG, Op);
2030
2031 assert(Cond.getOpcode() == MipsISD::FPCmp &&
2032 "Floating point operand expected.");
2033
2034 SDLoc DL(Op);
2035 SDValue True = DAG.getConstant(1, DL, MVT::i32);
2036 SDValue False = DAG.getConstant(0, DL, MVT::i32);
2037
2038 return createCMovFP(DAG, Cond, True, False, DL);
2039}
2040
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002041SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002042 SelectionDAG &DAG) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002043 EVT Ty = Op.getValueType();
2044 GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
2045 const GlobalValue *GV = N->getGlobal();
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00002046
Simon Dardisca74dd72017-01-27 11:36:52 +00002047 if (!isPositionIndependent()) {
Eric Christopher36fe0282015-02-03 07:22:52 +00002048 const MipsTargetObjectFile *TLOF =
2049 static_cast<const MipsTargetObjectFile *>(
2050 getTargetMachine().getObjFileLowering());
Peter Collingbourne67335642016-10-24 19:23:39 +00002051 const GlobalObject *GO = GV->getBaseObject();
2052 if (GO && TLOF->IsGlobalInSmallSection(GO, getTargetMachine()))
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00002053 // %gp_rel relocation
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002054 return getAddrGPRel(N, SDLoc(N), Ty, DAG);
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002055
Simon Dardisca74dd72017-01-27 11:36:52 +00002056 // %hi/%lo relocation
2057 return Subtarget.hasSym32() ? getAddrNonPIC(N, SDLoc(N), Ty, DAG)
2058 // %highest/%higher/%hi/%lo relocation
2059 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG);
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00002060 }
2061
Rafael Espindolab2b6a852016-06-27 12:33:33 +00002062 // Every other architecture would use shouldAssumeDSOLocal in here, but
2063 // mips is special.
Rafael Espindola97ca8272016-06-27 23:21:07 +00002064 // * In PIC code mips requires got loads even for local statics!
Rafael Espindolab2b6a852016-06-27 12:33:33 +00002065 // * To save on got entries, for local statics the got entry contains the
2066 // page and an additional add instruction takes care of the low bits.
2067 // * It is legal to access a hidden symbol with a non hidden undefined,
2068 // so one cannot guarantee that all access to a hidden symbol will know
2069 // it is hidden.
2070 // * Mips linkers don't support creating a page and a full got entry for
2071 // the same symbol.
2072 // * Given all that, we have to use a full got entry for hidden symbols :-(
Rafael Espindola1ac1fa82016-06-27 03:19:40 +00002073 if (GV->hasLocalLinkage())
Eric Christopher96e72c62015-01-29 23:27:36 +00002074 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002075
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00002076 if (LargeGOT)
Alex Lorenze40c8a22015-08-11 23:09:45 +00002077 return getAddrGlobalLargeGOT(
2078 N, SDLoc(N), Ty, DAG, MipsII::MO_GOT_HI16, MipsII::MO_GOT_LO16,
2079 DAG.getEntryNode(),
2080 MachinePointerInfo::getGOT(DAG.getMachineFunction()));
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00002081
Alex Lorenze40c8a22015-08-11 23:09:45 +00002082 return getAddrGlobal(
2083 N, SDLoc(N), Ty, DAG,
Daniel Sandersfe98b2f2016-05-03 13:35:44 +00002084 (ABI.IsN32() || ABI.IsN64()) ? MipsII::MO_GOT_DISP : MipsII::MO_GOT,
Alex Lorenze40c8a22015-08-11 23:09:45 +00002085 DAG.getEntryNode(), MachinePointerInfo::getGOT(DAG.getMachineFunction()));
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00002086}
2087
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002088SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op,
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +00002089 SelectionDAG &DAG) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002090 BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op);
2091 EVT Ty = Op.getValueType();
Akira Hatanaka30f97cf2013-09-25 00:30:25 +00002092
Simon Dardisca74dd72017-01-27 11:36:52 +00002093 if (!isPositionIndependent())
2094 return Subtarget.hasSym32() ? getAddrNonPIC(N, SDLoc(N), Ty, DAG)
2095 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG);
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002096
Eric Christopher96e72c62015-01-29 23:27:36 +00002097 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +00002098}
2099
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00002100SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002101lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00002102{
Akira Hatanakabff84e12011-12-14 18:26:41 +00002103 // If the relocation model is PIC, use the General Dynamic TLS Model or
2104 // Local Dynamic TLS model, otherwise use the Initial Exec or
2105 // Local Exec TLS Model.
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00002106
2107 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +00002108 if (DAG.getTarget().Options.EmulatedTLS)
2109 return LowerToTLSEmulatedModel(GA, DAG);
2110
Andrew Trickef9de2a2013-05-25 02:42:55 +00002111 SDLoc DL(GA);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00002112 const GlobalValue *GV = GA->getGlobal();
Mehdi Amini44ede332015-07-09 02:09:04 +00002113 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00002114
Hans Wennborgaea41202012-05-04 09:40:39 +00002115 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
2116
2117 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Hans Wennborg245917b2012-06-04 14:02:08 +00002118 // General Dynamic and Local Dynamic TLS Model.
2119 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
2120 : MipsII::MO_TLSGD;
2121
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002122 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag);
2123 SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT,
2124 getGlobalReg(DAG, PtrVT), TGA);
Akira Hatanakaf10ee842011-12-08 21:05:38 +00002125 unsigned PtrSize = PtrVT.getSizeInBits();
2126 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
2127
Benjamin Kramer64ba50a2011-12-11 12:21:34 +00002128 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00002129
2130 ArgListTy Args;
2131 ArgListEntry Entry;
2132 Entry.Node = Argument;
Akira Hatanakadee6c822011-12-08 20:34:32 +00002133 Entry.Ty = PtrTy;
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00002134 Args.push_back(Entry);
Jia Liuf54f60f2012-02-28 07:46:26 +00002135
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002136 TargetLowering::CallLoweringInfo CLI(DAG);
Nirav Daveac6081c2017-03-18 00:44:07 +00002137 CLI.setDebugLoc(DL)
2138 .setChain(DAG.getEntryNode())
2139 .setLibCallee(CallingConv::C, PtrTy, TlsGetAddr, std::move(Args));
Justin Holewinskiaa583972012-05-25 16:35:28 +00002140 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00002141
Akira Hatanakabff84e12011-12-14 18:26:41 +00002142 SDValue Ret = CallResult.first;
2143
Hans Wennborgaea41202012-05-04 09:40:39 +00002144 if (model != TLSModel::LocalDynamic)
Akira Hatanakabff84e12011-12-14 18:26:41 +00002145 return Ret;
2146
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002147 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanakabff84e12011-12-14 18:26:41 +00002148 MipsII::MO_DTPREL_HI);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002149 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
2150 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanakabff84e12011-12-14 18:26:41 +00002151 MipsII::MO_DTPREL_LO);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002152 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
2153 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret);
2154 return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00002155 }
Akira Hatanaka5b350be2011-06-21 01:02:03 +00002156
2157 SDValue Offset;
Hans Wennborgaea41202012-05-04 09:40:39 +00002158 if (model == TLSModel::InitialExec) {
Akira Hatanaka5b350be2011-06-21 01:02:03 +00002159 // Initial Exec TLS Model
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002160 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00002161 MipsII::MO_GOTTPREL);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002162 TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT),
Akira Hatanakab049aef2012-02-24 22:34:47 +00002163 TGA);
Justin Lebar9c375812016-07-15 18:27:10 +00002164 Offset =
2165 DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), TGA, MachinePointerInfo());
Akira Hatanaka5b350be2011-06-21 01:02:03 +00002166 } else {
2167 // Local Exec TLS Model
Hans Wennborgaea41202012-05-04 09:40:39 +00002168 assert(model == TLSModel::LocalExec);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002169 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00002170 MipsII::MO_TPREL_HI);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002171 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00002172 MipsII::MO_TPREL_LO);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002173 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
2174 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
2175 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
Akira Hatanaka5b350be2011-06-21 01:02:03 +00002176 }
2177
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002178 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
2179 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00002180}
2181
2182SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002183lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +00002184{
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002185 JumpTableSDNode *N = cast<JumpTableSDNode>(Op);
2186 EVT Ty = Op.getValueType();
Akira Hatanaka30f97cf2013-09-25 00:30:25 +00002187
Simon Dardisca74dd72017-01-27 11:36:52 +00002188 if (!isPositionIndependent())
2189 return Subtarget.hasSym32() ? getAddrNonPIC(N, SDLoc(N), Ty, DAG)
2190 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG);
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002191
Eric Christopher96e72c62015-01-29 23:27:36 +00002192 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +00002193}
2194
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002195SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002196lowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +00002197{
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002198 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
2199 EVT Ty = Op.getValueType();
Bruno Cardoso Lopes2db07582009-11-25 12:17:58 +00002200
Simon Dardisca74dd72017-01-27 11:36:52 +00002201 if (!isPositionIndependent()) {
Eric Christopher36fe0282015-02-03 07:22:52 +00002202 const MipsTargetObjectFile *TLOF =
2203 static_cast<const MipsTargetObjectFile *>(
2204 getTargetMachine().getObjFileLowering());
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00002205
Mehdi Aminibd7287e2015-07-16 06:11:10 +00002206 if (TLOF->IsConstantInSmallSection(DAG.getDataLayout(), N->getConstVal(),
2207 getTargetMachine()))
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00002208 // %gp_rel relocation
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002209 return getAddrGPRel(N, SDLoc(N), Ty, DAG);
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00002210
Simon Dardisca74dd72017-01-27 11:36:52 +00002211 return Subtarget.hasSym32() ? getAddrNonPIC(N, SDLoc(N), Ty, DAG)
2212 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG);
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00002213 }
Bruno Cardoso Lopesfdb4cec2008-07-23 16:01:50 +00002214
Simon Dardisca74dd72017-01-27 11:36:52 +00002215 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +00002216}
2217
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002218SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman31ae5862010-04-17 14:41:14 +00002219 MachineFunction &MF = DAG.getMachineFunction();
2220 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
2221
Andrew Trickef9de2a2013-05-25 02:42:55 +00002222 SDLoc DL(Op);
Dan Gohman31ae5862010-04-17 14:41:14 +00002223 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
Mehdi Amini44ede332015-07-09 02:09:04 +00002224 getPointerTy(MF.getDataLayout()));
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +00002225
2226 // vastart just stores the address of the VarArgsFrameIndex slot into the
2227 // memory location argument.
2228 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002229 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
Justin Lebar9c375812016-07-15 18:27:10 +00002230 MachinePointerInfo(SV));
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +00002231}
Jia Liuf54f60f2012-02-28 07:46:26 +00002232
Daniel Sanders2b553d42014-08-01 09:17:39 +00002233SDValue MipsTargetLowering::lowerVAARG(SDValue Op, SelectionDAG &DAG) const {
2234 SDNode *Node = Op.getNode();
2235 EVT VT = Node->getValueType(0);
2236 SDValue Chain = Node->getOperand(0);
2237 SDValue VAListPtr = Node->getOperand(1);
2238 unsigned Align = Node->getConstantOperandVal(3);
2239 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
2240 SDLoc DL(Node);
Eric Christopher96e72c62015-01-29 23:27:36 +00002241 unsigned ArgSlotSizeInBytes = (ABI.IsN32() || ABI.IsN64()) ? 8 : 4;
Daniel Sanders2b553d42014-08-01 09:17:39 +00002242
Justin Lebar9c375812016-07-15 18:27:10 +00002243 SDValue VAListLoad = DAG.getLoad(getPointerTy(DAG.getDataLayout()), DL, Chain,
2244 VAListPtr, MachinePointerInfo(SV));
Daniel Sanders2b553d42014-08-01 09:17:39 +00002245 SDValue VAList = VAListLoad;
2246
2247 // Re-align the pointer if necessary.
2248 // It should only ever be necessary for 64-bit types on O32 since the minimum
2249 // argument alignment is the same as the maximum type alignment for N32/N64.
2250 //
2251 // FIXME: We currently align too often. The code generator doesn't notice
2252 // when the pointer is still aligned from the last va_arg (or pair of
2253 // va_args for the i64 on O32 case).
2254 if (Align > getMinStackArgumentAlignment()) {
2255 assert(((Align & (Align-1)) == 0) && "Expected Align to be a power of 2");
2256
2257 VAList = DAG.getNode(ISD::ADD, DL, VAList.getValueType(), VAList,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002258 DAG.getConstant(Align - 1, DL, VAList.getValueType()));
Daniel Sanders2b553d42014-08-01 09:17:39 +00002259
2260 VAList = DAG.getNode(ISD::AND, DL, VAList.getValueType(), VAList,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002261 DAG.getConstant(-(int64_t)Align, DL,
Daniel Sanders2b553d42014-08-01 09:17:39 +00002262 VAList.getValueType()));
2263 }
2264
2265 // Increment the pointer, VAList, to the next vaarg.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00002266 auto &TD = DAG.getDataLayout();
2267 unsigned ArgSizeInBytes =
2268 TD.getTypeAllocSize(VT.getTypeForEVT(*DAG.getContext()));
Rui Ueyamada00f2f2016-01-14 21:06:47 +00002269 SDValue Tmp3 =
2270 DAG.getNode(ISD::ADD, DL, VAList.getValueType(), VAList,
2271 DAG.getConstant(alignTo(ArgSizeInBytes, ArgSlotSizeInBytes),
2272 DL, VAList.getValueType()));
Daniel Sanders2b553d42014-08-01 09:17:39 +00002273 // Store the incremented VAList to the legalized pointer
2274 Chain = DAG.getStore(VAListLoad.getValue(1), DL, Tmp3, VAListPtr,
Justin Lebar9c375812016-07-15 18:27:10 +00002275 MachinePointerInfo(SV));
Daniel Sanders2b553d42014-08-01 09:17:39 +00002276
2277 // In big-endian mode we must adjust the pointer when the load size is smaller
2278 // than the argument slot size. We must also reduce the known alignment to
2279 // match. For example in the N64 ABI, we must add 4 bytes to the offset to get
2280 // the correct half of the slot, and reduce the alignment from 8 (slot
2281 // alignment) down to 4 (type alignment).
2282 if (!Subtarget.isLittle() && ArgSizeInBytes < ArgSlotSizeInBytes) {
2283 unsigned Adjustment = ArgSlotSizeInBytes - ArgSizeInBytes;
2284 VAList = DAG.getNode(ISD::ADD, DL, VAListPtr.getValueType(), VAList,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002285 DAG.getIntPtrConstant(Adjustment, DL));
Daniel Sanders2b553d42014-08-01 09:17:39 +00002286 }
2287 // Load the actual argument out of the pointer VAList
Justin Lebar9c375812016-07-15 18:27:10 +00002288 return DAG.getLoad(VT, DL, Chain, VAList, MachinePointerInfo());
Daniel Sanders2b553d42014-08-01 09:17:39 +00002289}
2290
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00002291static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG,
2292 bool HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002293 EVT TyX = Op.getOperand(0).getValueType();
2294 EVT TyY = Op.getOperand(1).getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002295 SDLoc DL(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002296 SDValue Const1 = DAG.getConstant(1, DL, MVT::i32);
2297 SDValue Const31 = DAG.getConstant(31, DL, MVT::i32);
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002298 SDValue Res;
2299
2300 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
2301 // to i32.
2302 SDValue X = (TyX == MVT::f32) ?
2303 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
2304 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
2305 Const1);
2306 SDValue Y = (TyY == MVT::f32) ?
2307 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
2308 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
2309 Const1);
2310
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00002311 if (HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002312 // ext E, Y, 31, 1 ; extract bit31 of Y
2313 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
2314 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
2315 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
2316 } else {
2317 // sll SllX, X, 1
2318 // srl SrlX, SllX, 1
2319 // srl SrlY, Y, 31
2320 // sll SllY, SrlX, 31
2321 // or Or, SrlX, SllY
2322 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
2323 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
2324 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
2325 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
2326 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
2327 }
2328
2329 if (TyX == MVT::f32)
2330 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
2331
2332 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002333 Op.getOperand(0),
2334 DAG.getConstant(0, DL, MVT::i32));
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002335 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002336}
2337
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00002338static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG,
2339 bool HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002340 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
2341 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
2342 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002343 SDLoc DL(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002344 SDValue Const1 = DAG.getConstant(1, DL, MVT::i32);
Eric Christopher0713a9d2011-06-08 23:55:35 +00002345
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002346 // Bitcast to integer nodes.
2347 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
2348 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002349
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00002350 if (HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002351 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
2352 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
2353 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002354 DAG.getConstant(WidthY - 1, DL, MVT::i32), Const1);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002355
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002356 if (WidthX > WidthY)
2357 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
2358 else if (WidthY > WidthX)
2359 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002360
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002361 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002362 DAG.getConstant(WidthX - 1, DL, MVT::i32), Const1,
2363 X);
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002364 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
2365 }
2366
2367 // (d)sll SllX, X, 1
2368 // (d)srl SrlX, SllX, 1
2369 // (d)srl SrlY, Y, width(Y)-1
2370 // (d)sll SllY, SrlX, width(Y)-1
2371 // or Or, SrlX, SllY
2372 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
2373 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
2374 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002375 DAG.getConstant(WidthY - 1, DL, MVT::i32));
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002376
2377 if (WidthX > WidthY)
2378 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
2379 else if (WidthY > WidthX)
2380 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
2381
2382 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002383 DAG.getConstant(WidthX - 1, DL, MVT::i32));
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002384 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
2385 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002386}
2387
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00002388SDValue
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002389MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00002390 if (Subtarget.isGP64bit())
2391 return lowerFCOPYSIGN64(Op, DAG, Subtarget.hasExtractInsert());
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002392
Eric Christopher1c29a652014-07-18 22:55:25 +00002393 return lowerFCOPYSIGN32(Op, DAG, Subtarget.hasExtractInsert());
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002394}
2395
Akira Hatanaka66277522011-06-02 00:24:44 +00002396SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002397lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes5444a7b2011-06-16 00:40:02 +00002398 // check the depth
2399 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka15506782011-06-07 18:58:42 +00002400 "Frame address can only be determined for current frame.");
Akira Hatanaka66277522011-06-02 00:24:44 +00002401
Matthias Braun941a7052016-07-28 18:40:00 +00002402 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
2403 MFI.setFrameAddressIsTaken(true);
Akira Hatanaka66277522011-06-02 00:24:44 +00002404 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002405 SDLoc DL(Op);
Eric Christopher96e72c62015-01-29 23:27:36 +00002406 SDValue FrameAddr = DAG.getCopyFromReg(
2407 DAG.getEntryNode(), DL, ABI.IsN64() ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka66277522011-06-02 00:24:44 +00002408 return FrameAddr;
2409}
2410
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002411SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op,
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002412 SelectionDAG &DAG) const {
Bill Wendling908bf812014-01-06 00:43:20 +00002413 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00002414 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00002415
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002416 // check the depth
2417 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
2418 "Return address can be determined only for current frame.");
2419
2420 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00002421 MachineFrameInfo &MFI = MF.getFrameInfo();
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00002422 MVT VT = Op.getSimpleValueType();
Eric Christopher96e72c62015-01-29 23:27:36 +00002423 unsigned RA = ABI.IsN64() ? Mips::RA_64 : Mips::RA;
Matthias Braun941a7052016-07-28 18:40:00 +00002424 MFI.setReturnAddressIsTaken(true);
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002425
2426 // Return RA, which contains the return address. Mark it an implicit live-in.
2427 unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00002428 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT);
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002429}
2430
Akira Hatanakac0b02062013-01-30 00:26:49 +00002431// An EH_RETURN is the result of lowering llvm.eh.return which in turn is
2432// generated from __builtin_eh_return (offset, handler)
2433// The effect of this is to adjust the stack pointer by "offset"
2434// and then branch to "handler".
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002435SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Akira Hatanakac0b02062013-01-30 00:26:49 +00002436 const {
2437 MachineFunction &MF = DAG.getMachineFunction();
2438 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2439
2440 MipsFI->setCallsEhReturn();
2441 SDValue Chain = Op.getOperand(0);
2442 SDValue Offset = Op.getOperand(1);
2443 SDValue Handler = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002444 SDLoc DL(Op);
Eric Christopher96e72c62015-01-29 23:27:36 +00002445 EVT Ty = ABI.IsN64() ? MVT::i64 : MVT::i32;
Akira Hatanakac0b02062013-01-30 00:26:49 +00002446
2447 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
2448 // EH_RETURN nodes, so that instructions are emitted back-to-back.
Eric Christopher96e72c62015-01-29 23:27:36 +00002449 unsigned OffsetReg = ABI.IsN64() ? Mips::V1_64 : Mips::V1;
2450 unsigned AddrReg = ABI.IsN64() ? Mips::V0_64 : Mips::V0;
Akira Hatanakac0b02062013-01-30 00:26:49 +00002451 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
2452 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
2453 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
2454 DAG.getRegister(OffsetReg, Ty),
Mehdi Amini44ede332015-07-09 02:09:04 +00002455 DAG.getRegister(AddrReg, getPointerTy(MF.getDataLayout())),
Akira Hatanakac0b02062013-01-30 00:26:49 +00002456 Chain.getValue(1));
2457}
2458
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002459SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
Akira Hatanaka5fd22482012-06-14 21:10:56 +00002460 SelectionDAG &DAG) const {
Eli Friedman26a48482011-07-27 22:21:52 +00002461 // FIXME: Need pseudo-fence for 'singlethread' fences
2462 // FIXME: Set SType for weaker fences where supported/appropriate.
2463 unsigned SType = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002464 SDLoc DL(Op);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002465 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002466 DAG.getConstant(SType, DL, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00002467}
2468
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002469SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
Akira Hatanaka5fd22482012-06-14 21:10:56 +00002470 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002471 SDLoc DL(Op);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002472 MVT VT = Subtarget.isGP64bit() ? MVT::i64 : MVT::i32;
2473
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002474 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2475 SDValue Shamt = Op.getOperand(2);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002476 // if shamt < (VT.bits):
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002477 // lo = (shl lo, shamt)
2478 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
2479 // else:
2480 // lo = 0
2481 // hi = (shl lo, shamt[4:0])
2482 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002483 DAG.getConstant(-1, DL, MVT::i32));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002484 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, VT, Lo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002485 DAG.getConstant(1, DL, VT));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002486 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, VT, ShiftRight1Lo, Not);
2487 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, VT, Hi, Shamt);
2488 SDValue Or = DAG.getNode(ISD::OR, DL, VT, ShiftLeftHi, ShiftRightLo);
2489 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, VT, Lo, Shamt);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002490 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
Daniel Sanders301f9372015-04-29 12:28:58 +00002491 DAG.getConstant(VT.getSizeInBits(), DL, MVT::i32));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002492 Lo = DAG.getNode(ISD::SELECT, DL, VT, Cond,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002493 DAG.getConstant(0, DL, VT), ShiftLeftLo);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002494 Hi = DAG.getNode(ISD::SELECT, DL, VT, Cond, ShiftLeftLo, Or);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002495
2496 SDValue Ops[2] = {Lo, Hi};
Craig Topper64941d92014-04-27 19:20:57 +00002497 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002498}
2499
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002500SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002501 bool IsSRA) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002502 SDLoc DL(Op);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002503 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2504 SDValue Shamt = Op.getOperand(2);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002505 MVT VT = Subtarget.isGP64bit() ? MVT::i64 : MVT::i32;
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002506
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002507 // if shamt < (VT.bits):
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002508 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
2509 // if isSRA:
2510 // hi = (sra hi, shamt)
2511 // else:
2512 // hi = (srl hi, shamt)
2513 // else:
2514 // if isSRA:
2515 // lo = (sra hi, shamt[4:0])
2516 // hi = (sra hi, 31)
2517 // else:
2518 // lo = (srl hi, shamt[4:0])
2519 // hi = 0
2520 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002521 DAG.getConstant(-1, DL, MVT::i32));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002522 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, VT, Hi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002523 DAG.getConstant(1, DL, VT));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002524 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, VT, ShiftLeft1Hi, Not);
2525 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, VT, Lo, Shamt);
2526 SDValue Or = DAG.getNode(ISD::OR, DL, VT, ShiftLeftHi, ShiftRightLo);
2527 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL,
2528 DL, VT, Hi, Shamt);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002529 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
Daniel Sanders301f9372015-04-29 12:28:58 +00002530 DAG.getConstant(VT.getSizeInBits(), DL, MVT::i32));
2531 SDValue Ext = DAG.getNode(ISD::SRA, DL, VT, Hi,
2532 DAG.getConstant(VT.getSizeInBits() - 1, DL, VT));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002533 Lo = DAG.getNode(ISD::SELECT, DL, VT, Cond, ShiftRightHi, Or);
2534 Hi = DAG.getNode(ISD::SELECT, DL, VT, Cond,
Daniel Sanders301f9372015-04-29 12:28:58 +00002535 IsSRA ? Ext : DAG.getConstant(0, DL, VT), ShiftRightHi);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002536
2537 SDValue Ops[2] = {Lo, Hi};
Craig Topper64941d92014-04-27 19:20:57 +00002538 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002539}
2540
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002541static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002542 SDValue Chain, SDValue Src, unsigned Offset) {
Akira Hatanaka95866182012-06-13 19:06:08 +00002543 SDValue Ptr = LD->getBasePtr();
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002544 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
Akira Hatanaka95866182012-06-13 19:06:08 +00002545 EVT BasePtrVT = Ptr.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002546 SDLoc DL(LD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002547 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
2548
2549 if (Offset)
Akira Hatanaka95866182012-06-13 19:06:08 +00002550 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002551 DAG.getConstant(Offset, DL, BasePtrVT));
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002552
2553 SDValue Ops[] = { Chain, Ptr, Src };
Craig Topper206fcd42014-04-26 19:29:41 +00002554 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002555 LD->getMemOperand());
2556}
2557
2558// Expand an unaligned 32 or 64-bit integer load node.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002559SDValue MipsTargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002560 LoadSDNode *LD = cast<LoadSDNode>(Op);
2561 EVT MemVT = LD->getMemoryVT();
2562
Eric Christopher1c29a652014-07-18 22:55:25 +00002563 if (Subtarget.systemSupportsUnalignedAccess())
Daniel Sandersac272632014-05-23 13:18:02 +00002564 return Op;
2565
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002566 // Return if load is aligned or if MemVT is neither i32 nor i64.
2567 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
2568 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
2569 return SDValue();
2570
Eric Christopher1c29a652014-07-18 22:55:25 +00002571 bool IsLittle = Subtarget.isLittle();
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002572 EVT VT = Op.getValueType();
2573 ISD::LoadExtType ExtType = LD->getExtensionType();
2574 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
2575
2576 assert((VT == MVT::i32) || (VT == MVT::i64));
2577
2578 // Expand
2579 // (set dst, (i64 (load baseptr)))
2580 // to
2581 // (set tmp, (ldl (add baseptr, 7), undef))
2582 // (set dst, (ldr baseptr, tmp))
2583 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002584 SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002585 IsLittle ? 7 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002586 return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002587 IsLittle ? 0 : 7);
2588 }
2589
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002590 SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002591 IsLittle ? 3 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002592 SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002593 IsLittle ? 0 : 3);
2594
2595 // Expand
2596 // (set dst, (i32 (load baseptr))) or
2597 // (set dst, (i64 (sextload baseptr))) or
2598 // (set dst, (i64 (extload baseptr)))
2599 // to
2600 // (set tmp, (lwl (add baseptr, 3), undef))
2601 // (set dst, (lwr baseptr, tmp))
2602 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
2603 (ExtType == ISD::EXTLOAD))
2604 return LWR;
2605
2606 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
2607
2608 // Expand
2609 // (set dst, (i64 (zextload baseptr)))
2610 // to
2611 // (set tmp0, (lwl (add baseptr, 3), undef))
2612 // (set tmp1, (lwr baseptr, tmp0))
2613 // (set tmp2, (shl tmp1, 32))
2614 // (set dst, (srl tmp2, 32))
Andrew Trickef9de2a2013-05-25 02:42:55 +00002615 SDLoc DL(LD);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002616 SDValue Const32 = DAG.getConstant(32, DL, MVT::i32);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002617 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
Akira Hatanaka67346852012-06-04 17:46:29 +00002618 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
2619 SDValue Ops[] = { SRL, LWR.getValue(1) };
Craig Topper64941d92014-04-27 19:20:57 +00002620 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002621}
2622
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002623static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002624 SDValue Chain, unsigned Offset) {
Akira Hatanaka95866182012-06-13 19:06:08 +00002625 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
2626 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002627 SDLoc DL(SD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002628 SDVTList VTList = DAG.getVTList(MVT::Other);
2629
2630 if (Offset)
Akira Hatanaka95866182012-06-13 19:06:08 +00002631 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002632 DAG.getConstant(Offset, DL, BasePtrVT));
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002633
2634 SDValue Ops[] = { Chain, Value, Ptr };
Craig Topper206fcd42014-04-26 19:29:41 +00002635 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002636 SD->getMemOperand());
2637}
2638
2639// Expand an unaligned 32 or 64-bit integer store node.
Akira Hatanakad82ee942013-05-16 20:45:17 +00002640static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG,
2641 bool IsLittle) {
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002642 SDValue Value = SD->getValue(), Chain = SD->getChain();
2643 EVT VT = Value.getValueType();
2644
2645 // Expand
2646 // (store val, baseptr) or
2647 // (truncstore val, baseptr)
2648 // to
2649 // (swl val, (add baseptr, 3))
2650 // (swr val, baseptr)
2651 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002652 SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002653 IsLittle ? 3 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002654 return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002655 }
2656
2657 assert(VT == MVT::i64);
2658
2659 // Expand
2660 // (store val, baseptr)
2661 // to
2662 // (sdl val, (add baseptr, 7))
2663 // (sdr val, baseptr)
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002664 SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2665 return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002666}
2667
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002668// Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr).
2669static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG) {
2670 SDValue Val = SD->getValue();
2671
2672 if (Val.getOpcode() != ISD::FP_TO_SINT)
2673 return SDValue();
2674
2675 EVT FPTy = EVT::getFloatingPointVT(Val.getValueSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002676 SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy,
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002677 Val.getOperand(0));
Andrew Trickef9de2a2013-05-25 02:42:55 +00002678 return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(),
Justin Lebar9c375812016-07-15 18:27:10 +00002679 SD->getPointerInfo(), SD->getAlignment(),
2680 SD->getMemOperand()->getFlags());
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002681}
2682
Akira Hatanakad82ee942013-05-16 20:45:17 +00002683SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2684 StoreSDNode *SD = cast<StoreSDNode>(Op);
2685 EVT MemVT = SD->getMemoryVT();
2686
2687 // Lower unaligned integer stores.
Eric Christopher1c29a652014-07-18 22:55:25 +00002688 if (!Subtarget.systemSupportsUnalignedAccess() &&
Daniel Sandersac272632014-05-23 13:18:02 +00002689 (SD->getAlignment() < MemVT.getSizeInBits() / 8) &&
Akira Hatanakad82ee942013-05-16 20:45:17 +00002690 ((MemVT == MVT::i32) || (MemVT == MVT::i64)))
Eric Christopher1c29a652014-07-18 22:55:25 +00002691 return lowerUnalignedIntStore(SD, DAG, Subtarget.isLittle());
Akira Hatanakad82ee942013-05-16 20:45:17 +00002692
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002693 return lowerFP_TO_SINT_STORE(SD, DAG);
Akira Hatanakad82ee942013-05-16 20:45:17 +00002694}
2695
Hal Finkel5081ac22016-09-01 10:28:47 +00002696SDValue MipsTargetLowering::lowerEH_DWARF_CFA(SDValue Op,
2697 SelectionDAG &DAG) const {
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002698
Hal Finkel5081ac22016-09-01 10:28:47 +00002699 // Return a fixed StackObject with offset 0 which points to the old stack
2700 // pointer.
Matthias Braun941a7052016-07-28 18:40:00 +00002701 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002702 EVT ValTy = Op->getValueType(0);
Matthias Braun941a7052016-07-28 18:40:00 +00002703 int FI = MFI.CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
Hal Finkel5081ac22016-09-01 10:28:47 +00002704 return DAG.getFrameIndex(FI, ValTy);
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002705}
2706
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002707SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op,
2708 SelectionDAG &DAG) const {
2709 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002710 SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy,
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002711 Op.getOperand(0));
Andrew Trickef9de2a2013-05-25 02:42:55 +00002712 return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc);
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002713}
2714
Akira Hatanakae2489122011-04-15 21:51:11 +00002715//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002716// Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002717//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002718
Akira Hatanakae2489122011-04-15 21:51:11 +00002719//===----------------------------------------------------------------------===//
Wesley Peck527da1b2010-11-23 03:31:01 +00002720// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002721// Mips O32 ABI rules:
2722// ---
2723// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peck527da1b2010-11-23 03:31:01 +00002724// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002725// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peck527da1b2010-11-23 03:31:01 +00002726// f64 - Only passed in two aliased f32 registers if no int reg has been used
2727// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Sylvestre Ledru469de192014-08-11 18:04:46 +00002728// not used, it must be shadowed. If only A3 is available, shadow it and
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002729// go to stack.
Simon Dardis212cccb2017-06-09 14:37:08 +00002730// vXiX - Received as scalarized i32s, passed in A0 - A3 and the stack.
2731// vXf32 - Passed in either a pair of registers {A0, A1}, {A2, A3} or {A0 - A3}
2732// with the remainder spilled to the stack.
2733// vXf64 - Passed in either {A0, A1, A2, A3} or {A2, A3} and in both cases
2734// spilling the remainder to the stack.
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002735//
2736// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanakae2489122011-04-15 21:51:11 +00002737//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002738
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00002739static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2740 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002741 CCState &State, ArrayRef<MCPhysReg> F64Regs) {
Eric Christopher96e72c62015-01-29 23:27:36 +00002742 const MipsSubtarget &Subtarget = static_cast<const MipsSubtarget &>(
2743 State.getMachineFunction().getSubtarget());
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002744
Craig Topper840beec2014-04-04 05:16:06 +00002745 static const MCPhysReg IntRegs[] = { Mips::A0, Mips::A1, Mips::A2, Mips::A3 };
Simon Dardis212cccb2017-06-09 14:37:08 +00002746
2747 const MipsCCState * MipsState = static_cast<MipsCCState *>(&State);
2748
Craig Topper840beec2014-04-04 05:16:06 +00002749 static const MCPhysReg F32Regs[] = { Mips::F12, Mips::F14 };
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002750
Simon Dardis212cccb2017-06-09 14:37:08 +00002751 static const MCPhysReg FloatVectorIntRegs[] = { Mips::A0, Mips::A2 };
2752
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002753 // Do not process byval args here.
2754 if (ArgFlags.isByVal())
2755 return true;
Akira Hatanaka5e16c6a2011-05-24 19:18:33 +00002756
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002757 // Promote i8 and i16
Daniel Sandersd134c9d2014-12-02 20:40:27 +00002758 if (ArgFlags.isInReg() && !Subtarget.isLittle()) {
2759 if (LocVT == MVT::i8 || LocVT == MVT::i16 || LocVT == MVT::i32) {
2760 LocVT = MVT::i32;
2761 if (ArgFlags.isSExt())
2762 LocInfo = CCValAssign::SExtUpper;
2763 else if (ArgFlags.isZExt())
2764 LocInfo = CCValAssign::ZExtUpper;
2765 else
2766 LocInfo = CCValAssign::AExtUpper;
2767 }
2768 }
2769
2770 // Promote i8 and i16
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002771 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
2772 LocVT = MVT::i32;
2773 if (ArgFlags.isSExt())
2774 LocInfo = CCValAssign::SExt;
2775 else if (ArgFlags.isZExt())
2776 LocInfo = CCValAssign::ZExt;
2777 else
2778 LocInfo = CCValAssign::AExt;
2779 }
2780
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002781 unsigned Reg;
2782
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002783 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
2784 // is true: function is vararg, argument is 3rd or higher, there is previous
2785 // argument which is not f32 or f64.
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002786 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1 ||
2787 State.getFirstUnallocated(F32Regs) != ValNo;
Akira Hatanaka9e6a8cc2011-05-19 20:29:48 +00002788 unsigned OrigAlign = ArgFlags.getOrigAlign();
2789 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Simon Dardis212cccb2017-06-09 14:37:08 +00002790 bool isVectorFloat = MipsState->WasOriginalArgVectorFloat(ValNo);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002791
Simon Dardis212cccb2017-06-09 14:37:08 +00002792 // The MIPS vector ABI for floats passes them in a pair of registers
2793 if (ValVT == MVT::i32 && isVectorFloat) {
2794 // This is the start of an vector that was scalarized into an unknown number
2795 // of components. It doesn't matter how many there are. Allocate one of the
2796 // notional 8 byte aligned registers which map onto the argument stack, and
2797 // shadow the register lost to alignment requirements.
2798 if (ArgFlags.isSplit()) {
2799 Reg = State.AllocateReg(FloatVectorIntRegs);
2800 if (Reg == Mips::A2)
2801 State.AllocateReg(Mips::A1);
2802 else if (Reg == 0)
2803 State.AllocateReg(Mips::A3);
2804 } else {
2805 // If we're an intermediate component of the split, we can just attempt to
2806 // allocate a register directly.
2807 Reg = State.AllocateReg(IntRegs);
2808 }
2809 } else if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002810 Reg = State.AllocateReg(IntRegs);
Akira Hatanaka9e6a8cc2011-05-19 20:29:48 +00002811 // If this is the first part of an i64 arg,
2812 // the allocated register must be either A0 or A2.
2813 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002814 Reg = State.AllocateReg(IntRegs);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002815 LocVT = MVT::i32;
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002816 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
2817 // Allocate int register and shadow next int register. If first
2818 // available register is Mips::A1 or Mips::A3, shadow it too.
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002819 Reg = State.AllocateReg(IntRegs);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002820 if (Reg == Mips::A1 || Reg == Mips::A3)
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002821 Reg = State.AllocateReg(IntRegs);
2822 State.AllocateReg(IntRegs);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002823 LocVT = MVT::i32;
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002824 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
2825 // we are guaranteed to find an available float register
2826 if (ValVT == MVT::f32) {
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002827 Reg = State.AllocateReg(F32Regs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002828 // Shadow int register
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002829 State.AllocateReg(IntRegs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002830 } else {
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002831 Reg = State.AllocateReg(F64Regs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002832 // Shadow int registers
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002833 unsigned Reg2 = State.AllocateReg(IntRegs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002834 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002835 State.AllocateReg(IntRegs);
2836 State.AllocateReg(IntRegs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002837 }
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002838 } else
2839 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002840
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002841 if (!Reg) {
2842 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3,
2843 OrigAlign);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002844 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002845 } else
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002846 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002847
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002848 return false;
Akira Hatanaka202f6402011-11-12 02:20:46 +00002849}
2850
Akira Hatanakabfb66242013-08-20 23:38:40 +00002851static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT,
2852 MVT LocVT, CCValAssign::LocInfo LocInfo,
2853 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002854 static const MCPhysReg F64Regs[] = { Mips::D6, Mips::D7 };
Akira Hatanakabfb66242013-08-20 23:38:40 +00002855
2856 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2857}
2858
2859static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT,
2860 MVT LocVT, CCValAssign::LocInfo LocInfo,
2861 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002862 static const MCPhysReg F64Regs[] = { Mips::D12_64, Mips::D14_64 };
Akira Hatanakabfb66242013-08-20 23:38:40 +00002863
2864 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2865}
2866
Reid Klecknerd3781742014-11-14 00:39:33 +00002867static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2868 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
2869 CCState &State) LLVM_ATTRIBUTE_UNUSED;
Reed Kotlerd5c41962014-11-13 23:37:45 +00002870
Akira Hatanaka202f6402011-11-12 02:20:46 +00002871#include "MipsGenCallingConv.inc"
2872
Akira Hatanakae2489122011-04-15 21:51:11 +00002873//===----------------------------------------------------------------------===//
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002874// Call Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002875//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002876
Akira Hatanaka61bbcce2011-09-23 00:58:33 +00002877// Return next O32 integer argument register.
2878static unsigned getNextIntArgReg(unsigned Reg) {
2879 assert((Reg == Mips::A0) || (Reg == Mips::A2));
2880 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
2881}
2882
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002883SDValue MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
2884 SDValue Chain, SDValue Arg,
2885 const SDLoc &DL, bool IsTailCall,
2886 SelectionDAG &DAG) const {
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002887 if (!IsTailCall) {
Mehdi Amini44ede332015-07-09 02:09:04 +00002888 SDValue PtrOff =
2889 DAG.getNode(ISD::ADD, DL, getPointerTy(DAG.getDataLayout()), StackPtr,
2890 DAG.getIntPtrConstant(Offset, DL));
Justin Lebar9c375812016-07-15 18:27:10 +00002891 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo());
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002892 }
2893
Matthias Braun941a7052016-07-28 18:40:00 +00002894 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
2895 int FI = MFI.CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00002896 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002897 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(),
Justin Lebar9c375812016-07-15 18:27:10 +00002898 /* Alignment = */ 0, MachineMemOperand::MOVolatile);
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002899}
2900
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002901void MipsTargetLowering::
2902getOpndList(SmallVectorImpl<SDValue> &Ops,
2903 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
2904 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
Sasa Stankovic7072a792014-10-01 08:22:21 +00002905 bool IsCallReloc, CallLoweringInfo &CLI, SDValue Callee,
2906 SDValue Chain) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002907 // Insert node "GP copy globalreg" before call to function.
2908 //
2909 // R_MIPS_CALL* operators (emitted when non-internal functions are called
2910 // in PIC mode) allow symbols to be resolved via lazy binding.
2911 // The lazy binding stub requires GP to point to the GOT.
Sasa Stankovic7072a792014-10-01 08:22:21 +00002912 // Note that we don't need GP to point to the GOT for indirect calls
2913 // (when R_MIPS_CALL* is not used for the call) because Mips linker generates
2914 // lazy binding stub for a function only when R_MIPS_CALL* are the only relocs
2915 // used for the function (that is, Mips linker doesn't generate lazy binding
2916 // stub for a function whose address is taken in the program).
2917 if (IsPICCall && !InternalLinkage && IsCallReloc) {
Eric Christopher96e72c62015-01-29 23:27:36 +00002918 unsigned GPReg = ABI.IsN64() ? Mips::GP_64 : Mips::GP;
2919 EVT Ty = ABI.IsN64() ? MVT::i64 : MVT::i32;
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002920 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty)));
2921 }
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002922
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002923 // Build a sequence of copy-to-reg nodes chained together with token
2924 // chain and flag operands which copy the outgoing args into registers.
2925 // The InFlag in necessary since all emitted instructions must be
2926 // stuck together.
2927 SDValue InFlag;
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002928
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002929 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2930 Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, RegsToPass[i].first,
2931 RegsToPass[i].second, InFlag);
2932 InFlag = Chain.getValue(1);
2933 }
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002934
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002935 // Add argument registers to the end of the list so that they are
2936 // known live into the call.
2937 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2938 Ops.push_back(CLI.DAG.getRegister(RegsToPass[i].first,
2939 RegsToPass[i].second.getValueType()));
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002940
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002941 // Add a register mask operand representing the call-preserved registers.
Eric Christopher96e72c62015-01-29 23:27:36 +00002942 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00002943 const uint32_t *Mask =
2944 TRI->getCallPreservedMask(CLI.DAG.getMachineFunction(), CLI.CallConv);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002945 assert(Mask && "Missing call preserved mask for calling convention");
Eric Christopher1c29a652014-07-18 22:55:25 +00002946 if (Subtarget.inMips16HardFloat()) {
Reed Kotler783c7942013-05-10 22:25:39 +00002947 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
2948 llvm::StringRef Sym = G->getGlobal()->getName();
2949 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
Reed Kotler3230e722013-12-12 02:41:11 +00002950 if (F && F->hasFnAttribute("__Mips16RetHelper")) {
Reed Kotler783c7942013-05-10 22:25:39 +00002951 Mask = MipsRegisterInfo::getMips16RetHelperMask();
2952 }
2953 }
2954 }
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002955 Ops.push_back(CLI.DAG.getRegisterMask(Mask));
2956
2957 if (InFlag.getNode())
2958 Ops.push_back(InFlag);
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002959}
2960
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002961/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman624801e2009-01-26 03:15:54 +00002962/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002963SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00002964MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002965 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00002966 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002967 SDLoc DL = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00002968 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
2969 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
2970 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Akira Hatanakabeda2242012-07-31 18:46:41 +00002971 SDValue Chain = CLI.Chain;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002972 SDValue Callee = CLI.Callee;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002973 bool &IsTailCall = CLI.IsTailCall;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002974 CallingConv::ID CallConv = CLI.CallConv;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002975 bool IsVarArg = CLI.IsVarArg;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002976
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002977 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00002978 MachineFrameInfo &MFI = MF.getFrameInfo();
Eric Christopher96e72c62015-01-29 23:27:36 +00002979 const TargetFrameLowering *TFL = Subtarget.getFrameLowering();
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002980 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
Rafael Espindola9f1c1fe2016-06-27 12:48:21 +00002981 bool IsPIC = isPositionIndependent();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002982
2983 // Analyze operands of the call, assigning locations to each operand.
2984 SmallVector<CCValAssign, 16> ArgLocs;
Daniel Sanders41a64c42014-11-07 11:10:48 +00002985 MipsCCState CCInfo(
2986 CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs, *DAG.getContext(),
2987 MipsCCState::getSpecialCallingConvForCallee(Callee.getNode(), Subtarget));
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002988
2989 // Allocate the reserved argument area. It seems strange to do this from the
2990 // caller side but removing it breaks the frame size calculation.
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002991 CCInfo.AllocateStack(ABI.GetCalleeAllocdArgSizeInBytes(CallConv), 1);
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002992
Simon Dardis70f79252017-04-26 11:10:38 +00002993 const ExternalSymbolSDNode *ES =
2994 dyn_cast_or_null<const ExternalSymbolSDNode>(Callee.getNode());
2995 CCInfo.AnalyzeCallOperands(Outs, CC_Mips, CLI.getArgs(),
2996 ES ? ES->getSymbol() : nullptr);
Wesley Peck527da1b2010-11-23 03:31:01 +00002997
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002998 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka195a1e22011-06-08 17:39:33 +00002999 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka97ba7692012-07-26 23:27:01 +00003000
Simon Dardis1dcb9112016-11-20 21:23:08 +00003001 // Check if it's really possible to do a tail call. Restrict it to functions
3002 // that are part of this compilation unit.
3003 bool InternalLinkage = false;
3004 if (IsTailCall) {
Daniel Sanders23e98772014-11-02 16:09:29 +00003005 IsTailCall = isEligibleForTailCallOptimization(
3006 CCInfo, NextStackOffset, *MF.getInfo<MipsFunctionInfo>());
Simon Dardis1dcb9112016-11-20 21:23:08 +00003007 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3008 InternalLinkage = G->getGlobal()->hasInternalLinkage();
3009 IsTailCall &= (InternalLinkage || G->getGlobal()->hasLocalLinkage() ||
3010 G->getGlobal()->hasPrivateLinkage() ||
3011 G->getGlobal()->hasHiddenVisibility() ||
3012 G->getGlobal()->hasProtectedVisibility());
3013 }
3014 }
Reid Kleckner5772b772014-04-24 20:14:34 +00003015 if (!IsTailCall && CLI.CS && CLI.CS->isMustTailCall())
3016 report_fatal_error("failed to perform tail call elimination on a call "
3017 "site marked musttail");
3018
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003019 if (IsTailCall)
Akira Hatanaka90131ac2012-10-19 21:47:33 +00003020 ++NumTailCalls;
3021
Akira Hatanaka79738332011-09-19 20:26:02 +00003022 // Chain is the output chain of the last Load/Store or CopyToReg node.
3023 // ByValChain is the output chain of the last Memcpy node created for copying
3024 // byval arguments to the stack.
Akira Hatanaka9c962c02012-10-30 20:16:31 +00003025 unsigned StackAlignment = TFL->getStackAlignment();
Rui Ueyamada00f2f2016-01-14 21:06:47 +00003026 NextStackOffset = alignTo(NextStackOffset, StackAlignment);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003027 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, DL, true);
Akira Hatanaka90131ac2012-10-19 21:47:33 +00003028
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003029 if (!IsTailCall)
Serge Pavlovd526b132017-05-09 13:35:13 +00003030 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffset, 0, DL);
Akira Hatanakabeda2242012-07-31 18:46:41 +00003031
Mehdi Amini44ede332015-07-09 02:09:04 +00003032 SDValue StackPtr =
3033 DAG.getCopyFromReg(Chain, DL, ABI.IsN64() ? Mips::SP_64 : Mips::SP,
3034 getPointerTy(DAG.getDataLayout()));
Akira Hatanaka195a1e22011-06-08 17:39:33 +00003035
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00003036 std::deque< std::pair<unsigned, SDValue> > RegsToPass;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003037 SmallVector<SDValue, 8> MemOpChains;
Daniel Sanders23e98772014-11-02 16:09:29 +00003038
3039 CCInfo.rewindByValRegsInfo();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003040
3041 // Walk the register/memloc assignments, inserting copies/loads.
3042 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003043 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003044 CCValAssign &VA = ArgLocs[i];
Akira Hatanakab20a3252011-10-28 19:49:00 +00003045 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka19891f82011-11-12 02:34:50 +00003046 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Daniel Sandersc43cda82014-11-07 16:54:21 +00003047 bool UseUpperBits = false;
Akira Hatanaka19891f82011-11-12 02:34:50 +00003048
3049 // ByVal Arg.
3050 if (Flags.isByVal()) {
Daniel Sanders23e98772014-11-02 16:09:29 +00003051 unsigned FirstByValReg, LastByValReg;
3052 unsigned ByValIdx = CCInfo.getInRegsParamsProcessed();
3053 CCInfo.getInRegsParamInfo(ByValIdx, FirstByValReg, LastByValReg);
3054
Akira Hatanaka19891f82011-11-12 02:34:50 +00003055 assert(Flags.getByValSize() &&
3056 "ByVal args of size 0 should have been ignored by front-end.");
Daniel Sanders23e98772014-11-02 16:09:29 +00003057 assert(ByValIdx < CCInfo.getInRegsParamsCount());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003058 assert(!IsTailCall &&
Akira Hatanaka9c962c02012-10-30 20:16:31 +00003059 "Do not tail-call optimize if there is a byval argument.");
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003060 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003061 FirstByValReg, LastByValReg, Flags, Subtarget.isLittle(),
3062 VA);
Daniel Sanders23e98772014-11-02 16:09:29 +00003063 CCInfo.nextInRegsParam();
Akira Hatanaka19891f82011-11-12 02:34:50 +00003064 continue;
3065 }
Jia Liuf54f60f2012-02-28 07:46:26 +00003066
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003067 // Promote the value if needed.
3068 switch (VA.getLocInfo()) {
Daniel Sandersc43cda82014-11-07 16:54:21 +00003069 default:
3070 llvm_unreachable("Unknown loc info!");
Wesley Peck527da1b2010-11-23 03:31:01 +00003071 case CCValAssign::Full:
Akira Hatanakab20a3252011-10-28 19:49:00 +00003072 if (VA.isRegLoc()) {
3073 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
Akira Hatanaka3b7391d2013-03-05 22:20:28 +00003074 (ValVT == MVT::f64 && LocVT == MVT::i64) ||
3075 (ValVT == MVT::i64 && LocVT == MVT::f64))
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003076 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
Akira Hatanakab20a3252011-10-28 19:49:00 +00003077 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003078 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003079 Arg, DAG.getConstant(0, DL, MVT::i32));
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003080 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003081 Arg, DAG.getConstant(1, DL, MVT::i32));
Eric Christopher1c29a652014-07-18 22:55:25 +00003082 if (!Subtarget.isLittle())
Akira Hatanaka27916972011-04-15 19:52:08 +00003083 std::swap(Lo, Hi);
Jia Liuf54f60f2012-02-28 07:46:26 +00003084 unsigned LocRegLo = VA.getLocReg();
Akira Hatanaka61bbcce2011-09-23 00:58:33 +00003085 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
3086 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
3087 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00003088 continue;
Wesley Peck527da1b2010-11-23 03:31:01 +00003089 }
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00003090 }
3091 break;
Daniel Sanders23e98772014-11-02 16:09:29 +00003092 case CCValAssign::BCvt:
3093 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
3094 break;
Daniel Sandersc43cda82014-11-07 16:54:21 +00003095 case CCValAssign::SExtUpper:
3096 UseUpperBits = true;
Justin Bognerb03fd122016-08-17 05:10:15 +00003097 LLVM_FALLTHROUGH;
Chris Lattner52f16de2008-03-17 06:57:02 +00003098 case CCValAssign::SExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003099 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00003100 break;
Daniel Sandersc43cda82014-11-07 16:54:21 +00003101 case CCValAssign::ZExtUpper:
3102 UseUpperBits = true;
Justin Bognerb03fd122016-08-17 05:10:15 +00003103 LLVM_FALLTHROUGH;
Chris Lattner52f16de2008-03-17 06:57:02 +00003104 case CCValAssign::ZExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003105 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00003106 break;
Daniel Sandersc43cda82014-11-07 16:54:21 +00003107 case CCValAssign::AExtUpper:
3108 UseUpperBits = true;
Justin Bognerb03fd122016-08-17 05:10:15 +00003109 LLVM_FALLTHROUGH;
Chris Lattner52f16de2008-03-17 06:57:02 +00003110 case CCValAssign::AExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003111 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00003112 break;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003113 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003114
Daniel Sandersc43cda82014-11-07 16:54:21 +00003115 if (UseUpperBits) {
3116 unsigned ValSizeInBits = Outs[i].ArgVT.getSizeInBits();
3117 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
3118 Arg = DAG.getNode(
3119 ISD::SHL, DL, VA.getLocVT(), Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003120 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
Daniel Sandersc43cda82014-11-07 16:54:21 +00003121 }
3122
Wesley Peck527da1b2010-11-23 03:31:01 +00003123 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +00003124 // RegsToPass vector
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003125 if (VA.isRegLoc()) {
3126 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattner52f16de2008-03-17 06:57:02 +00003127 continue;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003128 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003129
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00003130 // Register can't get to this point...
Chris Lattner52f16de2008-03-17 06:57:02 +00003131 assert(VA.isMemLoc());
Wesley Peck527da1b2010-11-23 03:31:01 +00003132
Wesley Peck527da1b2010-11-23 03:31:01 +00003133 // emit ISD::STORE whichs stores the
Chris Lattner52f16de2008-03-17 06:57:02 +00003134 // parameter value to a stack Location
Akira Hatanaka9c962c02012-10-30 20:16:31 +00003135 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003136 Chain, Arg, DL, IsTailCall, DAG));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003137 }
3138
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003139 // Transform all store nodes into one single node because all store
3140 // nodes are independent of each other.
Wesley Peck527da1b2010-11-23 03:31:01 +00003141 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00003142 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003143
Bill Wendling24c79f22008-09-16 21:48:12 +00003144 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peck527da1b2010-11-23 03:31:01 +00003145 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
3146 // node so that legalize doesn't hack it.
Simon Dardisca74dd72017-01-27 11:36:52 +00003147
Akira Hatanakad6f1c582011-04-07 19:51:44 +00003148 SDValue CalleeLo;
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00003149 EVT Ty = Callee.getValueType();
Simon Dardis1dcb9112016-11-20 21:23:08 +00003150 bool GlobalOrExternal = false, IsCallReloc = false;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00003151
3152 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Simon Dardisca74dd72017-01-27 11:36:52 +00003153 if (IsPIC) {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00003154 const GlobalValue *Val = G->getGlobal();
3155 InternalLinkage = Val->hasInternalLinkage();
Akira Hatanakacf9a61b2012-12-13 03:17:29 +00003156
3157 if (InternalLinkage)
Eric Christopher96e72c62015-01-29 23:27:36 +00003158 Callee = getAddrLocal(G, DL, Ty, DAG, ABI.IsN32() || ABI.IsN64());
Sasa Stankovic7072a792014-10-01 08:22:21 +00003159 else if (LargeGOT) {
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00003160 Callee = getAddrGlobalLargeGOT(G, DL, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00003161 MipsII::MO_CALL_LO16, Chain,
3162 FuncInfo->callPtrInfo(Val));
Sasa Stankovic7072a792014-10-01 08:22:21 +00003163 IsCallReloc = true;
3164 } else {
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00003165 Callee = getAddrGlobal(G, DL, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00003166 FuncInfo->callPtrInfo(Val));
Sasa Stankovic7072a792014-10-01 08:22:21 +00003167 IsCallReloc = true;
3168 }
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00003169 } else
Mehdi Amini44ede332015-07-09 02:09:04 +00003170 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL,
3171 getPointerTy(DAG.getDataLayout()), 0,
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00003172 MipsII::MO_NO_FLAG);
Akira Hatanaka8e16aac2011-12-09 01:45:12 +00003173 GlobalOrExternal = true;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00003174 }
3175 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00003176 const char *Sym = S->getSymbol();
3177
Simon Dardisca74dd72017-01-27 11:36:52 +00003178 if (!IsPIC) // static
Mehdi Amini44ede332015-07-09 02:09:04 +00003179 Callee = DAG.getTargetExternalSymbol(
3180 Sym, getPointerTy(DAG.getDataLayout()), MipsII::MO_NO_FLAG);
Sasa Stankovic7072a792014-10-01 08:22:21 +00003181 else if (LargeGOT) {
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00003182 Callee = getAddrGlobalLargeGOT(S, DL, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00003183 MipsII::MO_CALL_LO16, Chain,
3184 FuncInfo->callPtrInfo(Sym));
Sasa Stankovic7072a792014-10-01 08:22:21 +00003185 IsCallReloc = true;
Simon Dardisca74dd72017-01-27 11:36:52 +00003186 } else { // PIC
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00003187 Callee = getAddrGlobal(S, DL, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00003188 FuncInfo->callPtrInfo(Sym));
Sasa Stankovic7072a792014-10-01 08:22:21 +00003189 IsCallReloc = true;
3190 }
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00003191
Akira Hatanaka8e16aac2011-12-09 01:45:12 +00003192 GlobalOrExternal = true;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00003193 }
3194
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00003195 SmallVector<SDValue, 8> Ops(1, Chain);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003196 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00003197
Simon Dardisca74dd72017-01-27 11:36:52 +00003198 getOpndList(Ops, RegsToPass, IsPIC, GlobalOrExternal, InternalLinkage,
Sasa Stankovic7072a792014-10-01 08:22:21 +00003199 IsCallReloc, CLI, Callee, Chain);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003200
Simon Dardis9a66bbe2016-09-21 09:43:40 +00003201 if (IsTailCall) {
3202 MF.getFrameInfo().setHasTailCall();
Craig Topper48d114b2014-04-26 18:35:24 +00003203 return DAG.getNode(MipsISD::TailCall, DL, MVT::Other, Ops);
Simon Dardis9a66bbe2016-09-21 09:43:40 +00003204 }
Akira Hatanaka90131ac2012-10-19 21:47:33 +00003205
Craig Topper48d114b2014-04-26 18:35:24 +00003206 Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, Ops);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003207 SDValue InFlag = Chain.getValue(1);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003208
Bruno Cardoso Lopes193e64c2010-01-30 18:32:07 +00003209 // Create the CALLSEQ_END node.
Akira Hatanaka97ba7692012-07-26 23:27:01 +00003210 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003211 DAG.getIntPtrConstant(0, DL, true), InFlag, DL);
Bruno Cardoso Lopes193e64c2010-01-30 18:32:07 +00003212 InFlag = Chain.getValue(1);
3213
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003214 // Handle result values, copying them out of physregs into vregs that we
3215 // return.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003216 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg, Ins, DL, DAG,
3217 InVals, CLI);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003218}
3219
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003220/// LowerCallResult - Lower the result values of a call into the
3221/// appropriate copies out of appropriate physical registers.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003222SDValue MipsTargetLowering::LowerCallResult(
3223 SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool IsVarArg,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003224 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
3225 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals,
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003226 TargetLowering::CallLoweringInfo &CLI) const {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003227 // Assign locations to each value returned by this call.
3228 SmallVector<CCValAssign, 16> RVLocs;
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003229 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
3230 *DAG.getContext());
Simon Dardis70f79252017-04-26 11:10:38 +00003231
3232 const ExternalSymbolSDNode *ES =
3233 dyn_cast_or_null<const ExternalSymbolSDNode>(CLI.Callee.getNode());
3234 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips, CLI.RetTy,
3235 ES ? ES->getSymbol() : nullptr);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003236
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003237 // Copy all of the result registers out of their specified physreg.
3238 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Daniel Sandersae275e32014-09-25 12:15:05 +00003239 CCValAssign &VA = RVLocs[i];
3240 assert(VA.isRegLoc() && "Can only return in registers!");
3241
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003242 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(),
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003243 RVLocs[i].getLocVT(), InFlag);
3244 Chain = Val.getValue(1);
3245 InFlag = Val.getValue(2);
3246
Daniel Sandersae275e32014-09-25 12:15:05 +00003247 if (VA.isUpperBitsInLoc()) {
3248 unsigned ValSizeInBits = Ins[i].ArgVT.getSizeInBits();
3249 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
3250 unsigned Shift =
3251 VA.getLocInfo() == CCValAssign::ZExtUpper ? ISD::SRL : ISD::SRA;
3252 Val = DAG.getNode(
3253 Shift, DL, VA.getLocVT(), Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003254 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
Daniel Sandersae275e32014-09-25 12:15:05 +00003255 }
3256
3257 switch (VA.getLocInfo()) {
3258 default:
3259 llvm_unreachable("Unknown loc info!");
3260 case CCValAssign::Full:
3261 break;
3262 case CCValAssign::BCvt:
3263 Val = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Val);
3264 break;
3265 case CCValAssign::AExt:
3266 case CCValAssign::AExtUpper:
3267 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
3268 break;
3269 case CCValAssign::ZExt:
3270 case CCValAssign::ZExtUpper:
3271 Val = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Val,
3272 DAG.getValueType(VA.getValVT()));
3273 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
3274 break;
3275 case CCValAssign::SExt:
3276 case CCValAssign::SExtUpper:
3277 Val = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Val,
3278 DAG.getValueType(VA.getValVT()));
3279 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
3280 break;
3281 }
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003282
3283 InVals.push_back(Val);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003284 }
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +00003285
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003286 return Chain;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003287}
3288
Daniel Sandersc43cda82014-11-07 16:54:21 +00003289static SDValue UnpackFromArgumentSlot(SDValue Val, const CCValAssign &VA,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003290 EVT ArgVT, const SDLoc &DL,
3291 SelectionDAG &DAG) {
Daniel Sandersc43cda82014-11-07 16:54:21 +00003292 MVT LocVT = VA.getLocVT();
3293 EVT ValVT = VA.getValVT();
3294
3295 // Shift into the upper bits if necessary.
3296 switch (VA.getLocInfo()) {
3297 default:
3298 break;
3299 case CCValAssign::AExtUpper:
3300 case CCValAssign::SExtUpper:
3301 case CCValAssign::ZExtUpper: {
3302 unsigned ValSizeInBits = ArgVT.getSizeInBits();
3303 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
3304 unsigned Opcode =
3305 VA.getLocInfo() == CCValAssign::ZExtUpper ? ISD::SRL : ISD::SRA;
3306 Val = DAG.getNode(
3307 Opcode, DL, VA.getLocVT(), Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003308 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
Daniel Sandersc43cda82014-11-07 16:54:21 +00003309 break;
3310 }
3311 }
3312
3313 // If this is an value smaller than the argument slot size (32-bit for O32,
3314 // 64-bit for N32/N64), it has been promoted in some way to the argument slot
3315 // size. Extract the value and insert any appropriate assertions regarding
3316 // sign/zero extension.
3317 switch (VA.getLocInfo()) {
3318 default:
3319 llvm_unreachable("Unknown loc info!");
3320 case CCValAssign::Full:
3321 break;
3322 case CCValAssign::AExtUpper:
3323 case CCValAssign::AExt:
3324 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
3325 break;
3326 case CCValAssign::SExtUpper:
3327 case CCValAssign::SExt:
3328 Val = DAG.getNode(ISD::AssertSext, DL, LocVT, Val, DAG.getValueType(ValVT));
3329 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
3330 break;
3331 case CCValAssign::ZExtUpper:
3332 case CCValAssign::ZExt:
3333 Val = DAG.getNode(ISD::AssertZext, DL, LocVT, Val, DAG.getValueType(ValVT));
3334 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
3335 break;
3336 case CCValAssign::BCvt:
3337 Val = DAG.getNode(ISD::BITCAST, DL, ValVT, Val);
3338 break;
3339 }
3340
3341 return Val;
3342}
3343
Akira Hatanakae2489122011-04-15 21:51:11 +00003344//===----------------------------------------------------------------------===//
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003345// Formal Arguments Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00003346//===----------------------------------------------------------------------===//
Wesley Peck527da1b2010-11-23 03:31:01 +00003347/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003348/// and generate load operations for arguments places on the stack.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003349SDValue MipsTargetLowering::LowerFormalArguments(
3350 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
3351 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
3352 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Bruno Cardoso Lopesa01ede22008-08-04 07:12:52 +00003353 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00003354 MachineFrameInfo &MFI = MF.getFrameInfo();
Bruno Cardoso Lopes14033fb2007-08-28 05:08:16 +00003355 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00003356
Dan Gohman31ae5862010-04-17 14:41:14 +00003357 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003358
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003359 // Used with vargs to acumulate store chains.
3360 std::vector<SDValue> OutChains;
3361
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003362 // Assign locations to all of the incoming arguments.
3363 SmallVector<CCValAssign, 16> ArgLocs;
Daniel Sanders23e98772014-11-02 16:09:29 +00003364 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
3365 *DAG.getContext());
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003366 CCInfo.AllocateStack(ABI.GetCalleeAllocdArgSizeInBytes(CallConv), 1);
Vasileios Kalintiris43dff0c2015-10-26 12:38:43 +00003367 const Function *Func = DAG.getMachineFunction().getFunction();
3368 Function::const_arg_iterator FuncArg = Func->arg_begin();
3369
Vasileios Kalintiris165121f2015-10-26 14:24:30 +00003370 if (Func->hasFnAttribute("interrupt") && !Func->arg_empty())
3371 report_fatal_error(
3372 "Functions with the interrupt attribute cannot have arguments!");
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00003373
Daniel Sandersb70e27c2014-11-06 16:36:30 +00003374 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips_FixedArg);
Akira Hatanaka4866fe12012-10-30 19:37:25 +00003375 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(),
Daniel Sanders23e98772014-11-02 16:09:29 +00003376 CCInfo.getInRegsParamsCount() > 0);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00003377
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00003378 unsigned CurArgIdx = 0;
Daniel Sanders23e98772014-11-02 16:09:29 +00003379 CCInfo.rewindByValRegsInfo();
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003380
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00003381 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003382 CCValAssign &VA = ArgLocs[i];
Andrew Trick05938a52015-02-16 18:10:47 +00003383 if (Ins[i].isOrigArg()) {
3384 std::advance(FuncArg, Ins[i].getOrigArgIndex() - CurArgIdx);
3385 CurArgIdx = Ins[i].getOrigArgIndex();
3386 }
Akira Hatanaka104b7e32011-10-28 19:55:48 +00003387 EVT ValVT = VA.getValVT();
Akira Hatanakafb9bae32011-11-12 02:29:58 +00003388 ISD::ArgFlagsTy Flags = Ins[i].Flags;
3389 bool IsRegLoc = VA.isRegLoc();
3390
3391 if (Flags.isByVal()) {
Andrew Trick05938a52015-02-16 18:10:47 +00003392 assert(Ins[i].isOrigArg() && "Byval arguments cannot be implicit");
Daniel Sanders23e98772014-11-02 16:09:29 +00003393 unsigned FirstByValReg, LastByValReg;
3394 unsigned ByValIdx = CCInfo.getInRegsParamsProcessed();
3395 CCInfo.getInRegsParamInfo(ByValIdx, FirstByValReg, LastByValReg);
3396
Akira Hatanakafb9bae32011-11-12 02:29:58 +00003397 assert(Flags.getByValSize() &&
3398 "ByVal args of size 0 should have been ignored by front-end.");
Daniel Sanders23e98772014-11-02 16:09:29 +00003399 assert(ByValIdx < CCInfo.getInRegsParamsCount());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003400 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003401 FirstByValReg, LastByValReg, VA, CCInfo);
Daniel Sanders23e98772014-11-02 16:09:29 +00003402 CCInfo.nextInRegsParam();
Akira Hatanakafb9bae32011-11-12 02:29:58 +00003403 continue;
3404 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003405
3406 // Arguments stored on registers
Akira Hatanakafb9bae32011-11-12 02:29:58 +00003407 if (IsRegLoc) {
Akira Hatanaka7d822522013-10-28 21:21:36 +00003408 MVT RegVT = VA.getLocVT();
Akira Hatanakacb4a1a82011-05-24 00:23:52 +00003409 unsigned ArgReg = VA.getLocReg();
Akira Hatanaka7d822522013-10-28 21:21:36 +00003410 const TargetRegisterClass *RC = getRegClassFor(RegVT);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003411
Wesley Peck527da1b2010-11-23 03:31:01 +00003412 // Transform the arguments stored on
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003413 // physical registers into virtual ones
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003414 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC);
3415 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
Wesley Peck527da1b2010-11-23 03:31:01 +00003416
Daniel Sandersc43cda82014-11-07 16:54:21 +00003417 ArgValue = UnpackFromArgumentSlot(ArgValue, VA, Ins[i].ArgVT, DL, DAG);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00003418
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003419 // Handle floating point arguments passed in integer registers and
3420 // long double arguments passed in floating point registers.
Akira Hatanaka104b7e32011-10-28 19:55:48 +00003421 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003422 (RegVT == MVT::i64 && ValVT == MVT::f64) ||
3423 (RegVT == MVT::f64 && ValVT == MVT::i64))
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003424 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
Eric Christopher96e72c62015-01-29 23:27:36 +00003425 else if (ABI.IsO32() && RegVT == MVT::i32 &&
Eric Christopherbf33a3c2014-07-02 23:18:40 +00003426 ValVT == MVT::f64) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003427 unsigned Reg2 = addLiveIn(DAG.getMachineFunction(),
Akira Hatanaka104b7e32011-10-28 19:55:48 +00003428 getNextIntArgReg(ArgReg), RC);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003429 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT);
Eric Christopher1c29a652014-07-18 22:55:25 +00003430 if (!Subtarget.isLittle())
Akira Hatanaka104b7e32011-10-28 19:55:48 +00003431 std::swap(ArgValue, ArgValue2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003432 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64,
Akira Hatanaka104b7e32011-10-28 19:55:48 +00003433 ArgValue, ArgValue2);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00003434 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003435
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003436 InVals.push_back(ArgValue);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003437 } else { // VA.isRegLoc()
Daniel Sandersc43cda82014-11-07 16:54:21 +00003438 MVT LocVT = VA.getLocVT();
3439
Eric Christopher96e72c62015-01-29 23:27:36 +00003440 if (ABI.IsO32()) {
Daniel Sandersc43cda82014-11-07 16:54:21 +00003441 // We ought to be able to use LocVT directly but O32 sets it to i32
3442 // when allocating floating point values to integer registers.
3443 // This shouldn't influence how we load the value into registers unless
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00003444 // we are targeting softfloat.
Eric Christophere8ae3e32015-05-07 23:10:21 +00003445 if (VA.getValVT().isFloatingPoint() && !Subtarget.useSoftFloat())
Daniel Sandersc43cda82014-11-07 16:54:21 +00003446 LocVT = VA.getValVT();
3447 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003448
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003449 // sanity check
3450 assert(VA.isMemLoc());
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003451
Wesley Peck527da1b2010-11-23 03:31:01 +00003452 // The stack pointer offset is relative to the caller stack frame.
Matthias Braun941a7052016-07-28 18:40:00 +00003453 int FI = MFI.CreateFixedObject(LocVT.getSizeInBits() / 8,
3454 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003455
3456 // Create load nodes to retrieve arguments from the stack
Mehdi Amini44ede332015-07-09 02:09:04 +00003457 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Alex Lorenze40c8a22015-08-11 23:09:45 +00003458 SDValue ArgValue = DAG.getLoad(
3459 LocVT, DL, Chain, FIN,
Justin Lebar9c375812016-07-15 18:27:10 +00003460 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI));
Daniel Sandersc43cda82014-11-07 16:54:21 +00003461 OutChains.push_back(ArgValue.getValue(1));
3462
3463 ArgValue = UnpackFromArgumentSlot(ArgValue, VA, Ins[i].ArgVT, DL, DAG);
3464
3465 InVals.push_back(ArgValue);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003466 }
Reid Kleckner7a59e082014-05-12 22:01:27 +00003467 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003468
Reid Kleckner7a59e082014-05-12 22:01:27 +00003469 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Reid Kleckner79418562014-05-09 22:32:13 +00003470 // The mips ABIs for returning structs by value requires that we copy
3471 // the sret argument into $v0 for the return. Save the argument into
3472 // a virtual register so that we can access it from the return points.
Reid Kleckner7a59e082014-05-12 22:01:27 +00003473 if (Ins[i].Flags.isSRet()) {
Reid Kleckner79418562014-05-09 22:32:13 +00003474 unsigned Reg = MipsFI->getSRetReturnReg();
3475 if (!Reg) {
3476 Reg = MF.getRegInfo().createVirtualRegister(
Eric Christopher96e72c62015-01-29 23:27:36 +00003477 getRegClassFor(ABI.IsN64() ? MVT::i64 : MVT::i32));
Reid Kleckner79418562014-05-09 22:32:13 +00003478 MipsFI->setSRetReturnReg(Reg);
3479 }
Reid Kleckner7a59e082014-05-12 22:01:27 +00003480 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[i]);
Reid Kleckner79418562014-05-09 22:32:13 +00003481 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
Reid Kleckner7a59e082014-05-12 22:01:27 +00003482 break;
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003483 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003484 }
3485
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003486 if (IsVarArg)
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003487 writeVarArgRegs(OutChains, Chain, DL, DAG, CCInfo);
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003488
Wesley Peck527da1b2010-11-23 03:31:01 +00003489 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003490 // the size of Ins and InVals. This only happens when on varg functions
3491 if (!OutChains.empty()) {
3492 OutChains.push_back(Chain);
Craig Topper48d114b2014-04-26 18:35:24 +00003493 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003494 }
3495
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003496 return Chain;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003497}
3498
Akira Hatanakae2489122011-04-15 21:51:11 +00003499//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003500// Return Value Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00003501//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003502
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003503bool
3504MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003505 MachineFunction &MF, bool IsVarArg,
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003506 const SmallVectorImpl<ISD::OutputArg> &Outs,
3507 LLVMContext &Context) const {
3508 SmallVector<CCValAssign, 16> RVLocs;
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003509 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003510 return CCInfo.CheckReturn(Outs, RetCC_Mips);
3511}
3512
Petar Jovanovic5b436222015-03-23 12:28:13 +00003513bool
3514MipsTargetLowering::shouldSignExtendTypeInLibCall(EVT Type, bool IsSigned) const {
Eric Christophere8ae3e32015-05-07 23:10:21 +00003515 if (Subtarget.hasMips3() && Subtarget.useSoftFloat()) {
Petar Jovanovic5b436222015-03-23 12:28:13 +00003516 if (Type == MVT::i32)
3517 return true;
3518 }
3519 return IsSigned;
3520}
3521
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003522SDValue
Vasileios Kalintiris43dff0c2015-10-26 12:38:43 +00003523MipsTargetLowering::LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003524 const SDLoc &DL,
3525 SelectionDAG &DAG) const {
Vasileios Kalintiris43dff0c2015-10-26 12:38:43 +00003526
3527 MachineFunction &MF = DAG.getMachineFunction();
3528 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3529
3530 MipsFI->setISR();
3531
3532 return DAG.getNode(MipsISD::ERet, DL, MVT::Other, RetOps);
3533}
3534
3535SDValue
3536MipsTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
3537 bool IsVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003538 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003539 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003540 const SDLoc &DL, SelectionDAG &DAG) const {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003541 // CCValAssign - represent the assignment of
3542 // the return value to a location
3543 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003544 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003545
3546 // CCState - Info about the registers and stack slot.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003547 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, *DAG.getContext());
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003548
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003549 // Analyze return values.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003550 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003551
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003552 SDValue Flag;
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003553 SmallVector<SDValue, 4> RetOps(1, Chain);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003554
3555 // Copy the result values into the output registers.
3556 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003557 SDValue Val = OutVals[i];
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003558 CCValAssign &VA = RVLocs[i];
3559 assert(VA.isRegLoc() && "Can only return in registers!");
Daniel Sandersae275e32014-09-25 12:15:05 +00003560 bool UseUpperBits = false;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003561
Daniel Sandersae275e32014-09-25 12:15:05 +00003562 switch (VA.getLocInfo()) {
3563 default:
3564 llvm_unreachable("Unknown loc info!");
3565 case CCValAssign::Full:
3566 break;
3567 case CCValAssign::BCvt:
3568 Val = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Val);
3569 break;
3570 case CCValAssign::AExtUpper:
3571 UseUpperBits = true;
Justin Bognerb03fd122016-08-17 05:10:15 +00003572 LLVM_FALLTHROUGH;
Daniel Sandersae275e32014-09-25 12:15:05 +00003573 case CCValAssign::AExt:
3574 Val = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Val);
3575 break;
3576 case CCValAssign::ZExtUpper:
3577 UseUpperBits = true;
Justin Bognerb03fd122016-08-17 05:10:15 +00003578 LLVM_FALLTHROUGH;
Daniel Sandersae275e32014-09-25 12:15:05 +00003579 case CCValAssign::ZExt:
3580 Val = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Val);
3581 break;
3582 case CCValAssign::SExtUpper:
3583 UseUpperBits = true;
Justin Bognerb03fd122016-08-17 05:10:15 +00003584 LLVM_FALLTHROUGH;
Daniel Sandersae275e32014-09-25 12:15:05 +00003585 case CCValAssign::SExt:
3586 Val = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Val);
3587 break;
3588 }
3589
3590 if (UseUpperBits) {
3591 unsigned ValSizeInBits = Outs[i].ArgVT.getSizeInBits();
3592 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
3593 Val = DAG.getNode(
3594 ISD::SHL, DL, VA.getLocVT(), Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003595 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
Daniel Sandersae275e32014-09-25 12:15:05 +00003596 }
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003597
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003598 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003599
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003600 // Guarantee that all emitted copies are stuck together with flags.
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003601 Flag = Chain.getValue(1);
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003602 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003603 }
3604
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003605 // The mips ABIs for returning structs by value requires that we copy
3606 // the sret argument into $v0 for the return. We saved the argument into
3607 // a virtual register in the entry block, so now we copy the value out
3608 // and into $v0.
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003609 if (MF.getFunction()->hasStructRetAttr()) {
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003610 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3611 unsigned Reg = MipsFI->getSRetReturnReg();
3612
Wesley Peck527da1b2010-11-23 03:31:01 +00003613 if (!Reg)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003614 llvm_unreachable("sret virtual register not created in the entry block");
Mehdi Amini44ede332015-07-09 02:09:04 +00003615 SDValue Val =
3616 DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(DAG.getDataLayout()));
Eric Christopher96e72c62015-01-29 23:27:36 +00003617 unsigned V0 = ABI.IsN64() ? Mips::V0_64 : Mips::V0;
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003618
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003619 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003620 Flag = Chain.getValue(1);
Mehdi Amini44ede332015-07-09 02:09:04 +00003621 RetOps.push_back(DAG.getRegister(V0, getPointerTy(DAG.getDataLayout())));
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003622 }
3623
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003624 RetOps[0] = Chain; // Update chain.
Akira Hatanakaefff7b72012-07-10 00:19:06 +00003625
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003626 // Add the flag if we have it.
3627 if (Flag.getNode())
3628 RetOps.push_back(Flag);
3629
Vasileios Kalintiris43dff0c2015-10-26 12:38:43 +00003630 // ISRs must use "eret".
3631 if (DAG.getMachineFunction().getFunction()->hasFnAttribute("interrupt"))
3632 return LowerInterruptReturn(RetOps, DL, DAG);
3633
3634 // Standard return on Mips is a "jr $ra"
Craig Topper48d114b2014-04-26 18:35:24 +00003635 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, RetOps);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003636}
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003637
Akira Hatanakae2489122011-04-15 21:51:11 +00003638//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003639// Mips Inline Assembly Support
Akira Hatanakae2489122011-04-15 21:51:11 +00003640//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003641
3642/// getConstraintType - Given a constraint letter, return the type of
3643/// constraint it is for this target.
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003644MipsTargetLowering::ConstraintType
3645MipsTargetLowering::getConstraintType(StringRef Constraint) const {
Daniel Sanders8b59af12013-11-12 12:56:01 +00003646 // Mips specific constraints
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003647 // GCC config/mips/constraints.md
3648 //
Wesley Peck527da1b2010-11-23 03:31:01 +00003649 // 'd' : An address register. Equivalent to r
3650 // unless generating MIPS16 code.
3651 // 'y' : Equivalent to r; retained for
3652 // backwards compatibility.
Eric Christophere3c494d2012-05-07 06:25:10 +00003653 // 'c' : A register suitable for use in an indirect
3654 // jump. This will always be $25 for -mabicalls.
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003655 // 'l' : The lo register. 1 word storage.
3656 // 'x' : The hilo register pair. Double word storage.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003657 if (Constraint.size() == 1) {
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003658 switch (Constraint[0]) {
3659 default : break;
Wesley Peck527da1b2010-11-23 03:31:01 +00003660 case 'd':
3661 case 'y':
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003662 case 'f':
Eric Christophere3c494d2012-05-07 06:25:10 +00003663 case 'c':
Eric Christopher9c492e62012-05-07 06:25:15 +00003664 case 'l':
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003665 case 'x':
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003666 return C_RegisterClass;
Jack Carter0e149b02013-03-04 21:33:15 +00003667 case 'R':
3668 return C_Memory;
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003669 }
3670 }
Daniel Sandersa73d8fe2015-03-24 11:26:34 +00003671
3672 if (Constraint == "ZC")
3673 return C_Memory;
3674
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003675 return TargetLowering::getConstraintType(Constraint);
3676}
3677
John Thompsone8360b72010-10-29 17:29:13 +00003678/// Examine constraint type and operand type and determine a weight value.
3679/// This object must already have been set up with the operand type
3680/// and the current alternative constraint selected.
3681TargetLowering::ConstraintWeight
3682MipsTargetLowering::getSingleConstraintMatchWeight(
3683 AsmOperandInfo &info, const char *constraint) const {
3684 ConstraintWeight weight = CW_Invalid;
3685 Value *CallOperandVal = info.CallOperandVal;
3686 // If we don't have a value, we can't do a match,
3687 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +00003688 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +00003689 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +00003690 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +00003691 // Look at the constraint type.
3692 switch (*constraint) {
3693 default:
3694 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
3695 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00003696 case 'd':
3697 case 'y':
John Thompsone8360b72010-10-29 17:29:13 +00003698 if (type->isIntegerTy())
3699 weight = CW_Register;
3700 break;
Daniel Sanders8b59af12013-11-12 12:56:01 +00003701 case 'f': // FPU or MSA register
Eric Christopher1c29a652014-07-18 22:55:25 +00003702 if (Subtarget.hasMSA() && type->isVectorTy() &&
Daniel Sanders8b59af12013-11-12 12:56:01 +00003703 cast<VectorType>(type)->getBitWidth() == 128)
3704 weight = CW_Register;
3705 else if (type->isFloatTy())
John Thompsone8360b72010-10-29 17:29:13 +00003706 weight = CW_Register;
3707 break;
Eric Christophere3c494d2012-05-07 06:25:10 +00003708 case 'c': // $25 for indirect jumps
Eric Christopher9c492e62012-05-07 06:25:15 +00003709 case 'l': // lo register
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003710 case 'x': // hilo register pair
Daniel Sanders8b59af12013-11-12 12:56:01 +00003711 if (type->isIntegerTy())
Eric Christophere3c494d2012-05-07 06:25:10 +00003712 weight = CW_SpecificReg;
Daniel Sanders8b59af12013-11-12 12:56:01 +00003713 break;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003714 case 'I': // signed 16 bit immediate
Eric Christopher7201e1b2012-05-07 03:13:42 +00003715 case 'J': // integer zero
Eric Christopher3ff88a02012-05-07 05:46:29 +00003716 case 'K': // unsigned 16 bit immediate
Eric Christopher1109b342012-05-07 05:46:37 +00003717 case 'L': // signed 32 bit immediate where lower 16 bits are 0
Eric Christophere07aa432012-05-07 05:46:43 +00003718 case 'N': // immediate in the range of -65535 to -1 (inclusive)
Eric Christopher470578a2012-05-07 05:46:48 +00003719 case 'O': // signed 15 bit immediate (+- 16383)
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003720 case 'P': // immediate in the range of 65535 to 1 (inclusive)
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003721 if (isa<ConstantInt>(CallOperandVal))
3722 weight = CW_Constant;
3723 break;
Jack Carter0e149b02013-03-04 21:33:15 +00003724 case 'R':
3725 weight = CW_Memory;
3726 break;
John Thompsone8360b72010-10-29 17:29:13 +00003727 }
3728 return weight;
3729}
3730
Akira Hatanaka7473b472013-08-14 00:21:25 +00003731/// This is a helper function to parse a physical register string and split it
3732/// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag
3733/// that is returned indicates whether parsing was successful. The second flag
3734/// is true if the numeric part exists.
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003735static std::pair<bool, bool> parsePhysicalReg(StringRef C, StringRef &Prefix,
3736 unsigned long long &Reg) {
Akira Hatanaka7473b472013-08-14 00:21:25 +00003737 if (C.front() != '{' || C.back() != '}')
3738 return std::make_pair(false, false);
3739
3740 // Search for the first numeric character.
3741 StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1;
Craig Topper2241dfd2015-11-23 07:19:06 +00003742 I = std::find_if(B, E, isdigit);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003743
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003744 Prefix = StringRef(B, I - B);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003745
3746 // The second flag is set to false if no numeric characters were found.
3747 if (I == E)
3748 return std::make_pair(true, false);
3749
3750 // Parse the numeric characters.
3751 return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg),
3752 true);
3753}
3754
3755std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering::
Craig Topper6dc4a8bc2014-08-30 16:48:02 +00003756parseRegForInlineAsmConstraint(StringRef C, MVT VT) const {
Eric Christopherd9134482014-08-04 21:25:23 +00003757 const TargetRegisterInfo *TRI =
Eric Christopher96e72c62015-01-29 23:27:36 +00003758 Subtarget.getRegisterInfo();
Akira Hatanaka7473b472013-08-14 00:21:25 +00003759 const TargetRegisterClass *RC;
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003760 StringRef Prefix;
Akira Hatanaka7473b472013-08-14 00:21:25 +00003761 unsigned long long Reg;
3762
3763 std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg);
3764
3765 if (!R.first)
Craig Topper062a2ba2014-04-25 05:30:21 +00003766 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003767
3768 if ((Prefix == "hi" || Prefix == "lo")) { // Parse hi/lo.
3769 // No numeric characters follow "hi" or "lo".
3770 if (R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003771 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003772
3773 RC = TRI->getRegClass(Prefix == "hi" ?
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00003774 Mips::HI32RegClassID : Mips::LO32RegClassID);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003775 return std::make_pair(*(RC->begin()), RC);
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003776 } else if (Prefix.startswith("$msa")) {
Daniel Sanders8b59af12013-11-12 12:56:01 +00003777 // Parse $msa(ir|csr|access|save|modify|request|map|unmap)
3778
3779 // No numeric characters follow the name.
3780 if (R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003781 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003782
3783 Reg = StringSwitch<unsigned long long>(Prefix)
3784 .Case("$msair", Mips::MSAIR)
3785 .Case("$msacsr", Mips::MSACSR)
3786 .Case("$msaaccess", Mips::MSAAccess)
3787 .Case("$msasave", Mips::MSASave)
3788 .Case("$msamodify", Mips::MSAModify)
3789 .Case("$msarequest", Mips::MSARequest)
3790 .Case("$msamap", Mips::MSAMap)
3791 .Case("$msaunmap", Mips::MSAUnmap)
3792 .Default(0);
3793
3794 if (!Reg)
Craig Topper062a2ba2014-04-25 05:30:21 +00003795 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003796
3797 RC = TRI->getRegClass(Mips::MSACtrlRegClassID);
3798 return std::make_pair(Reg, RC);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003799 }
3800
3801 if (!R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003802 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003803
3804 if (Prefix == "$f") { // Parse $f0-$f31.
3805 // If the size of FP registers is 64-bit or Reg is an even number, select
3806 // the 64-bit register class. Otherwise, select the 32-bit register class.
3807 if (VT == MVT::Other)
Eric Christopher1c29a652014-07-18 22:55:25 +00003808 VT = (Subtarget.isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32;
Akira Hatanaka7473b472013-08-14 00:21:25 +00003809
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003810 RC = getRegClassFor(VT);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003811
3812 if (RC == &Mips::AFGR64RegClass) {
3813 assert(Reg % 2 == 0);
3814 Reg >>= 1;
3815 }
Daniel Sanders8b59af12013-11-12 12:56:01 +00003816 } else if (Prefix == "$fcc") // Parse $fcc0-$fcc7.
Akira Hatanaka7473b472013-08-14 00:21:25 +00003817 RC = TRI->getRegClass(Mips::FCCRegClassID);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003818 else if (Prefix == "$w") { // Parse $w0-$w31.
3819 RC = getRegClassFor((VT == MVT::Other) ? MVT::v16i8 : VT);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003820 } else { // Parse $0-$31.
3821 assert(Prefix == "$");
3822 RC = getRegClassFor((VT == MVT::Other) ? MVT::i32 : VT);
3823 }
3824
3825 assert(Reg < RC->getNumRegs());
3826 return std::make_pair(*(RC->begin() + Reg), RC);
3827}
3828
Eric Christophereaf77dc2011-06-29 19:33:04 +00003829/// Given a register class constraint, like 'r', if this corresponds directly
3830/// to an LLVM register class, return a register of 0 and the register class
3831/// pointer.
Eric Christopher11e4df72015-02-26 22:38:43 +00003832std::pair<unsigned, const TargetRegisterClass *>
3833MipsTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003834 StringRef Constraint,
Eric Christopher11e4df72015-02-26 22:38:43 +00003835 MVT VT) const {
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003836 if (Constraint.size() == 1) {
3837 switch (Constraint[0]) {
Eric Christopher9519c082011-06-29 19:04:31 +00003838 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
3839 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003840 case 'r':
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003841 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
Eric Christopher1c29a652014-07-18 22:55:25 +00003842 if (Subtarget.inMips16Mode())
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003843 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003844 return std::make_pair(0U, &Mips::GPR32RegClass);
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003845 }
Eric Christopher1c29a652014-07-18 22:55:25 +00003846 if (VT == MVT::i64 && !Subtarget.isGP64bit())
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003847 return std::make_pair(0U, &Mips::GPR32RegClass);
Eric Christopher1c29a652014-07-18 22:55:25 +00003848 if (VT == MVT::i64 && Subtarget.isGP64bit())
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003849 return std::make_pair(0U, &Mips::GPR64RegClass);
Eric Christopher58daf042012-05-07 03:13:22 +00003850 // This will generate an error message
Craig Topper062a2ba2014-04-25 05:30:21 +00003851 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003852 case 'f': // FPU or MSA register
3853 if (VT == MVT::v16i8)
3854 return std::make_pair(0U, &Mips::MSA128BRegClass);
3855 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
3856 return std::make_pair(0U, &Mips::MSA128HRegClass);
3857 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
3858 return std::make_pair(0U, &Mips::MSA128WRegClass);
3859 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
3860 return std::make_pair(0U, &Mips::MSA128DRegClass);
3861 else if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00003862 return std::make_pair(0U, &Mips::FGR32RegClass);
Eric Christopher1c29a652014-07-18 22:55:25 +00003863 else if ((VT == MVT::f64) && (!Subtarget.isSingleFloat())) {
3864 if (Subtarget.isFP64bit())
Craig Topperc7242e02012-04-20 07:30:17 +00003865 return std::make_pair(0U, &Mips::FGR64RegClass);
3866 return std::make_pair(0U, &Mips::AFGR64RegClass);
Akira Hatanakac669d7a2012-01-04 02:45:01 +00003867 }
Eric Christophere3c494d2012-05-07 06:25:10 +00003868 break;
3869 case 'c': // register suitable for indirect jump
3870 if (VT == MVT::i32)
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003871 return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass);
Eric Christophere3c494d2012-05-07 06:25:10 +00003872 assert(VT == MVT::i64 && "Unexpected type.");
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003873 return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass);
Eric Christopher9c492e62012-05-07 06:25:15 +00003874 case 'l': // register suitable for indirect jump
3875 if (VT == MVT::i32)
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00003876 return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass);
3877 return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass);
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003878 case 'x': // register suitable for indirect jump
3879 // Fixme: Not triggering the use of both hi and low
3880 // This will generate an error message
Craig Topper062a2ba2014-04-25 05:30:21 +00003881 return std::make_pair(0U, nullptr);
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003882 }
3883 }
Akira Hatanaka7473b472013-08-14 00:21:25 +00003884
3885 std::pair<unsigned, const TargetRegisterClass *> R;
3886 R = parseRegForInlineAsmConstraint(Constraint, VT);
3887
3888 if (R.second)
3889 return R;
3890
Eric Christopher11e4df72015-02-26 22:38:43 +00003891 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003892}
3893
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003894/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3895/// vector. If it is invalid, don't add anything to Ops.
3896void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3897 std::string &Constraint,
3898 std::vector<SDValue>&Ops,
3899 SelectionDAG &DAG) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003900 SDLoc DL(Op);
Craig Topper062a2ba2014-04-25 05:30:21 +00003901 SDValue Result;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003902
3903 // Only support length 1 constraints for now.
3904 if (Constraint.length() > 1) return;
3905
3906 char ConstraintLetter = Constraint[0];
3907 switch (ConstraintLetter) {
3908 default: break; // This will fall through to the generic implementation
3909 case 'I': // Signed 16 bit constant
3910 // If this fails, the parent routine will give an error
3911 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3912 EVT Type = Op.getValueType();
3913 int64_t Val = C->getSExtValue();
3914 if (isInt<16>(Val)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003915 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003916 break;
3917 }
3918 }
3919 return;
Eric Christopher7201e1b2012-05-07 03:13:42 +00003920 case 'J': // integer zero
3921 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3922 EVT Type = Op.getValueType();
3923 int64_t Val = C->getZExtValue();
3924 if (Val == 0) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003925 Result = DAG.getTargetConstant(0, DL, Type);
Eric Christopher7201e1b2012-05-07 03:13:42 +00003926 break;
3927 }
3928 }
3929 return;
Eric Christopher3ff88a02012-05-07 05:46:29 +00003930 case 'K': // unsigned 16 bit immediate
3931 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3932 EVT Type = Op.getValueType();
3933 uint64_t Val = (uint64_t)C->getZExtValue();
3934 if (isUInt<16>(Val)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003935 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopher3ff88a02012-05-07 05:46:29 +00003936 break;
3937 }
3938 }
3939 return;
Eric Christopher1109b342012-05-07 05:46:37 +00003940 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3941 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3942 EVT Type = Op.getValueType();
3943 int64_t Val = C->getSExtValue();
3944 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003945 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopher1109b342012-05-07 05:46:37 +00003946 break;
3947 }
3948 }
3949 return;
Eric Christophere07aa432012-05-07 05:46:43 +00003950 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3951 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3952 EVT Type = Op.getValueType();
3953 int64_t Val = C->getSExtValue();
3954 if ((Val >= -65535) && (Val <= -1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003955 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christophere07aa432012-05-07 05:46:43 +00003956 break;
3957 }
3958 }
3959 return;
Eric Christopher470578a2012-05-07 05:46:48 +00003960 case 'O': // signed 15 bit immediate
3961 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3962 EVT Type = Op.getValueType();
3963 int64_t Val = C->getSExtValue();
3964 if ((isInt<15>(Val))) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003965 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopher470578a2012-05-07 05:46:48 +00003966 break;
3967 }
3968 }
3969 return;
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003970 case 'P': // immediate in the range of 1 to 65535 (inclusive)
3971 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3972 EVT Type = Op.getValueType();
3973 int64_t Val = C->getSExtValue();
3974 if ((Val <= 65535) && (Val >= 1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003975 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003976 break;
3977 }
3978 }
3979 return;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003980 }
3981
3982 if (Result.getNode()) {
3983 Ops.push_back(Result);
3984 return;
3985 }
3986
3987 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3988}
3989
Mehdi Amini0cdec1e2015-07-09 02:09:40 +00003990bool MipsTargetLowering::isLegalAddressingMode(const DataLayout &DL,
3991 const AddrMode &AM, Type *Ty,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +00003992 unsigned AS) const {
Akira Hatanakaef839192012-11-17 00:25:41 +00003993 // No global is ever allowed as a base.
3994 if (AM.BaseGV)
3995 return false;
3996
3997 switch (AM.Scale) {
3998 case 0: // "r+i" or just "i", depending on HasBaseReg.
3999 break;
4000 case 1:
4001 if (!AM.HasBaseReg) // allow "r+i".
4002 break;
4003 return false; // disallow "r+r" or "r+r+i".
4004 default:
4005 return false;
4006 }
4007
4008 return true;
4009}
4010
4011bool
Dan Gohman2fe6bee2008-10-18 02:06:02 +00004012MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
4013 // The Mips target isn't yet aware of offsets.
4014 return false;
4015}
Evan Cheng16993aa2009-10-27 19:56:55 +00004016
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00004017EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00004018 unsigned SrcAlign,
4019 bool IsMemset, bool ZeroMemset,
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00004020 bool MemcpyStrSrc,
4021 MachineFunction &MF) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00004022 if (Subtarget.hasMips64())
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00004023 return MVT::i64;
4024
4025 return MVT::i32;
4026}
4027
Evan Cheng83896a52009-10-28 01:43:28 +00004028bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
4029 if (VT != MVT::f32 && VT != MVT::f64)
4030 return false;
Bruno Cardoso Lopesb02a9df2011-01-18 19:41:41 +00004031 if (Imm.isNegZero())
4032 return false;
Evan Cheng16993aa2009-10-27 19:56:55 +00004033 return Imm.isZero();
4034}
Akira Hatanakaf0b08442012-02-03 04:33:00 +00004035
4036unsigned MipsTargetLowering::getJumpTableEncoding() const {
Simon Dardisca74dd72017-01-27 11:36:52 +00004037
4038 // FIXME: For space reasons this should be: EK_GPRel32BlockAddress.
4039 if (ABI.IsN64() && isPositionIndependent())
Akira Hatanakaf0b08442012-02-03 04:33:00 +00004040 return MachineJumpTableInfo::EK_GPRel64BlockAddress;
Jia Liuf54f60f2012-02-28 07:46:26 +00004041
Akira Hatanakaf0b08442012-02-03 04:33:00 +00004042 return TargetLowering::getJumpTableEncoding();
4043}
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00004044
Eric Christopher824f42f2015-05-12 01:26:05 +00004045bool MipsTargetLowering::useSoftFloat() const {
4046 return Subtarget.useSoftFloat();
4047}
4048
Daniel Sandersf43e6872014-11-01 18:44:56 +00004049void MipsTargetLowering::copyByValRegs(
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004050 SDValue Chain, const SDLoc &DL, std::vector<SDValue> &OutChains,
4051 SelectionDAG &DAG, const ISD::ArgFlagsTy &Flags,
4052 SmallVectorImpl<SDValue> &InVals, const Argument *FuncArg,
4053 unsigned FirstReg, unsigned LastReg, const CCValAssign &VA,
4054 MipsCCState &State) const {
Akira Hatanaka25dad192012-10-27 00:10:18 +00004055 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00004056 MachineFrameInfo &MFI = MF.getFrameInfo();
Daniel Sanders2b746bc2014-09-09 12:11:16 +00004057 unsigned GPRSizeInBytes = Subtarget.getGPRSizeInBytes();
Daniel Sanders23e98772014-11-02 16:09:29 +00004058 unsigned NumRegs = LastReg - FirstReg;
4059 unsigned RegAreaSize = NumRegs * GPRSizeInBytes;
Akira Hatanaka25dad192012-10-27 00:10:18 +00004060 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
4061 int FrameObjOffset;
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00004062 ArrayRef<MCPhysReg> ByValArgRegs = ABI.GetByValArgRegs();
Akira Hatanaka25dad192012-10-27 00:10:18 +00004063
4064 if (RegAreaSize)
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00004065 FrameObjOffset =
4066 (int)ABI.GetCalleeAllocdArgSizeInBytes(State.getCallingConv()) -
4067 (int)((ByValArgRegs.size() - FirstReg) * GPRSizeInBytes);
Akira Hatanaka25dad192012-10-27 00:10:18 +00004068 else
Daniel Sandersf43e6872014-11-01 18:44:56 +00004069 FrameObjOffset = VA.getLocMemOffset();
Akira Hatanaka25dad192012-10-27 00:10:18 +00004070
4071 // Create frame object.
Mehdi Amini44ede332015-07-09 02:09:04 +00004072 EVT PtrTy = getPointerTy(DAG.getDataLayout());
Matthias Braun941a7052016-07-28 18:40:00 +00004073 int FI = MFI.CreateFixedObject(FrameObjSize, FrameObjOffset, true);
Akira Hatanaka25dad192012-10-27 00:10:18 +00004074 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
4075 InVals.push_back(FIN);
4076
Daniel Sanders23e98772014-11-02 16:09:29 +00004077 if (!NumRegs)
Akira Hatanaka25dad192012-10-27 00:10:18 +00004078 return;
4079
4080 // Copy arg registers.
Daniel Sanders2b746bc2014-09-09 12:11:16 +00004081 MVT RegTy = MVT::getIntegerVT(GPRSizeInBytes * 8);
Akira Hatanaka25dad192012-10-27 00:10:18 +00004082 const TargetRegisterClass *RC = getRegClassFor(RegTy);
4083
Daniel Sanders23e98772014-11-02 16:09:29 +00004084 for (unsigned I = 0; I < NumRegs; ++I) {
Daniel Sandersd7eba312014-11-07 12:21:37 +00004085 unsigned ArgReg = ByValArgRegs[FirstReg + I];
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00004086 unsigned VReg = addLiveIn(MF, ArgReg, RC);
Daniel Sanders2b746bc2014-09-09 12:11:16 +00004087 unsigned Offset = I * GPRSizeInBytes;
Akira Hatanaka25dad192012-10-27 00:10:18 +00004088 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004089 DAG.getConstant(Offset, DL, PtrTy));
Akira Hatanaka25dad192012-10-27 00:10:18 +00004090 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
Justin Lebar9c375812016-07-15 18:27:10 +00004091 StorePtr, MachinePointerInfo(FuncArg, Offset));
Akira Hatanaka25dad192012-10-27 00:10:18 +00004092 OutChains.push_back(Store);
4093 }
4094}
Akira Hatanaka35f55b12012-10-27 00:16:36 +00004095
4096// Copy byVal arg to registers and stack.
Daniel Sandersf43e6872014-11-01 18:44:56 +00004097void MipsTargetLowering::passByValArg(
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004098 SDValue Chain, const SDLoc &DL,
Daniel Sandersf43e6872014-11-01 18:44:56 +00004099 std::deque<std::pair<unsigned, SDValue>> &RegsToPass,
4100 SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr,
Matthias Braun941a7052016-07-28 18:40:00 +00004101 MachineFrameInfo &MFI, SelectionDAG &DAG, SDValue Arg, unsigned FirstReg,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00004102 unsigned LastReg, const ISD::ArgFlagsTy &Flags, bool isLittle,
4103 const CCValAssign &VA) const {
Daniel Sandersac272632014-05-23 13:18:02 +00004104 unsigned ByValSizeInBytes = Flags.getByValSize();
4105 unsigned OffsetInBytes = 0; // From beginning of struct
Daniel Sanders2b746bc2014-09-09 12:11:16 +00004106 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
Daniel Sandersac272632014-05-23 13:18:02 +00004107 unsigned Alignment = std::min(Flags.getByValAlign(), RegSizeInBytes);
Mehdi Amini44ede332015-07-09 02:09:04 +00004108 EVT PtrTy = getPointerTy(DAG.getDataLayout()),
4109 RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
Daniel Sanders23e98772014-11-02 16:09:29 +00004110 unsigned NumRegs = LastReg - FirstReg;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00004111
Daniel Sanders23e98772014-11-02 16:09:29 +00004112 if (NumRegs) {
Craig Topper862d5d82015-09-28 00:15:34 +00004113 ArrayRef<MCPhysReg> ArgRegs = ABI.GetByValArgRegs();
Daniel Sanders23e98772014-11-02 16:09:29 +00004114 bool LeftoverBytes = (NumRegs * RegSizeInBytes > ByValSizeInBytes);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00004115 unsigned I = 0;
4116
4117 // Copy words to registers.
Daniel Sanders23e98772014-11-02 16:09:29 +00004118 for (; I < NumRegs - LeftoverBytes; ++I, OffsetInBytes += RegSizeInBytes) {
Akira Hatanaka35f55b12012-10-27 00:16:36 +00004119 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004120 DAG.getConstant(OffsetInBytes, DL, PtrTy));
Akira Hatanaka35f55b12012-10-27 00:16:36 +00004121 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
Justin Lebar9c375812016-07-15 18:27:10 +00004122 MachinePointerInfo(), Alignment);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00004123 MemOpChains.push_back(LoadVal.getValue(1));
Daniel Sanders23e98772014-11-02 16:09:29 +00004124 unsigned ArgReg = ArgRegs[FirstReg + I];
Akira Hatanaka35f55b12012-10-27 00:16:36 +00004125 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
4126 }
4127
4128 // Return if the struct has been fully copied.
Daniel Sandersac272632014-05-23 13:18:02 +00004129 if (ByValSizeInBytes == OffsetInBytes)
Akira Hatanaka35f55b12012-10-27 00:16:36 +00004130 return;
4131
4132 // Copy the remainder of the byval argument with sub-word loads and shifts.
4133 if (LeftoverBytes) {
Akira Hatanaka35f55b12012-10-27 00:16:36 +00004134 SDValue Val;
4135
Daniel Sandersac272632014-05-23 13:18:02 +00004136 for (unsigned LoadSizeInBytes = RegSizeInBytes / 2, TotalBytesLoaded = 0;
4137 OffsetInBytes < ByValSizeInBytes; LoadSizeInBytes /= 2) {
4138 unsigned RemainingSizeInBytes = ByValSizeInBytes - OffsetInBytes;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00004139
Daniel Sandersac272632014-05-23 13:18:02 +00004140 if (RemainingSizeInBytes < LoadSizeInBytes)
Akira Hatanaka35f55b12012-10-27 00:16:36 +00004141 continue;
4142
4143 // Load subword.
4144 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004145 DAG.getConstant(OffsetInBytes, DL,
4146 PtrTy));
Daniel Sandersac272632014-05-23 13:18:02 +00004147 SDValue LoadVal = DAG.getExtLoad(
4148 ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr, MachinePointerInfo(),
Justin Lebar9c375812016-07-15 18:27:10 +00004149 MVT::getIntegerVT(LoadSizeInBytes * 8), Alignment);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00004150 MemOpChains.push_back(LoadVal.getValue(1));
4151
4152 // Shift the loaded value.
4153 unsigned Shamt;
4154
4155 if (isLittle)
Daniel Sandersac272632014-05-23 13:18:02 +00004156 Shamt = TotalBytesLoaded * 8;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00004157 else
Daniel Sandersac272632014-05-23 13:18:02 +00004158 Shamt = (RegSizeInBytes - (TotalBytesLoaded + LoadSizeInBytes)) * 8;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00004159
4160 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004161 DAG.getConstant(Shamt, DL, MVT::i32));
Akira Hatanaka35f55b12012-10-27 00:16:36 +00004162
4163 if (Val.getNode())
4164 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
4165 else
4166 Val = Shift;
4167
Daniel Sandersac272632014-05-23 13:18:02 +00004168 OffsetInBytes += LoadSizeInBytes;
4169 TotalBytesLoaded += LoadSizeInBytes;
4170 Alignment = std::min(Alignment, LoadSizeInBytes);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00004171 }
4172
Daniel Sanders23e98772014-11-02 16:09:29 +00004173 unsigned ArgReg = ArgRegs[FirstReg + I];
Akira Hatanaka35f55b12012-10-27 00:16:36 +00004174 RegsToPass.push_back(std::make_pair(ArgReg, Val));
4175 return;
4176 }
4177 }
4178
4179 // Copy remainder of byval arg to it with memcpy.
Daniel Sandersac272632014-05-23 13:18:02 +00004180 unsigned MemCpySize = ByValSizeInBytes - OffsetInBytes;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00004181 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004182 DAG.getConstant(OffsetInBytes, DL, PtrTy));
Akira Hatanaka35f55b12012-10-27 00:16:36 +00004183 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004184 DAG.getIntPtrConstant(VA.getLocMemOffset(), DL));
4185 Chain = DAG.getMemcpy(Chain, DL, Dst, Src,
4186 DAG.getConstant(MemCpySize, DL, PtrTy),
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00004187 Alignment, /*isVolatile=*/false, /*AlwaysInline=*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004188 /*isTailCall=*/false,
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004189 MachinePointerInfo(), MachinePointerInfo());
Akira Hatanaka35f55b12012-10-27 00:16:36 +00004190 MemOpChains.push_back(Chain);
4191}
Akira Hatanaka2a134022012-10-27 00:21:13 +00004192
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00004193void MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004194 SDValue Chain, const SDLoc &DL,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00004195 SelectionDAG &DAG,
Daniel Sanders853c2432014-11-01 18:13:52 +00004196 CCState &State) const {
Craig Topper862d5d82015-09-28 00:15:34 +00004197 ArrayRef<MCPhysReg> ArgRegs = ABI.GetVarArgRegs();
Tim Northover3b6b7ca2015-02-21 02:11:17 +00004198 unsigned Idx = State.getFirstUnallocated(ArgRegs);
Daniel Sanders2b746bc2014-09-09 12:11:16 +00004199 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
4200 MVT RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
Akira Hatanaka2a134022012-10-27 00:21:13 +00004201 const TargetRegisterClass *RC = getRegClassFor(RegTy);
4202 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00004203 MachineFrameInfo &MFI = MF.getFrameInfo();
Akira Hatanaka2a134022012-10-27 00:21:13 +00004204 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
4205
4206 // Offset of the first variable argument from stack pointer.
4207 int VaArgOffset;
4208
Daniel Sanders75ee6b42014-09-10 10:37:03 +00004209 if (ArgRegs.size() == Idx)
Rui Ueyamada00f2f2016-01-14 21:06:47 +00004210 VaArgOffset = alignTo(State.getNextStackOffset(), RegSizeInBytes);
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00004211 else {
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00004212 VaArgOffset =
4213 (int)ABI.GetCalleeAllocdArgSizeInBytes(State.getCallingConv()) -
4214 (int)(RegSizeInBytes * (ArgRegs.size() - Idx));
4215 }
Akira Hatanaka2a134022012-10-27 00:21:13 +00004216
4217 // Record the frame index of the first variable argument
4218 // which is a value necessary to VASTART.
Matthias Braun941a7052016-07-28 18:40:00 +00004219 int FI = MFI.CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
Akira Hatanaka2a134022012-10-27 00:21:13 +00004220 MipsFI->setVarArgsFrameIndex(FI);
4221
4222 // Copy the integer registers that have not been used for argument passing
4223 // to the argument register save area. For O32, the save area is allocated
4224 // in the caller's stack frame, while for N32/64, it is allocated in the
4225 // callee's stack frame.
Daniel Sanders75ee6b42014-09-10 10:37:03 +00004226 for (unsigned I = Idx; I < ArgRegs.size();
4227 ++I, VaArgOffset += RegSizeInBytes) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00004228 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC);
Akira Hatanaka2a134022012-10-27 00:21:13 +00004229 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
Matthias Braun941a7052016-07-28 18:40:00 +00004230 FI = MFI.CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
Mehdi Amini44ede332015-07-09 02:09:04 +00004231 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Justin Lebar9c375812016-07-15 18:27:10 +00004232 SDValue Store =
4233 DAG.getStore(Chain, DL, ArgValue, PtrOff, MachinePointerInfo());
Eric Christopher1c29a652014-07-18 22:55:25 +00004234 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(
4235 (Value *)nullptr);
Akira Hatanaka2a134022012-10-27 00:21:13 +00004236 OutChains.push_back(Store);
4237 }
4238}
Daniel Sanders23e98772014-11-02 16:09:29 +00004239
4240void MipsTargetLowering::HandleByVal(CCState *State, unsigned &Size,
4241 unsigned Align) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00004242 const TargetFrameLowering *TFL = Subtarget.getFrameLowering();
Daniel Sanders23e98772014-11-02 16:09:29 +00004243
4244 assert(Size && "Byval argument's size shouldn't be 0.");
4245
4246 Align = std::min(Align, TFL->getStackAlignment());
4247
4248 unsigned FirstReg = 0;
4249 unsigned NumRegs = 0;
4250
4251 if (State->getCallingConv() != CallingConv::Fast) {
4252 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
Craig Topper862d5d82015-09-28 00:15:34 +00004253 ArrayRef<MCPhysReg> IntArgRegs = ABI.GetByValArgRegs();
Daniel Sanders23e98772014-11-02 16:09:29 +00004254 // FIXME: The O32 case actually describes no shadow registers.
4255 const MCPhysReg *ShadowRegs =
Eric Christopher96e72c62015-01-29 23:27:36 +00004256 ABI.IsO32() ? IntArgRegs.data() : Mips64DPRegs;
Daniel Sanders23e98772014-11-02 16:09:29 +00004257
4258 // We used to check the size as well but we can't do that anymore since
4259 // CCState::HandleByVal() rounds up the size after calling this function.
4260 assert(!(Align % RegSizeInBytes) &&
4261 "Byval argument's alignment should be a multiple of"
4262 "RegSizeInBytes.");
4263
Tim Northover3b6b7ca2015-02-21 02:11:17 +00004264 FirstReg = State->getFirstUnallocated(IntArgRegs);
Daniel Sanders23e98772014-11-02 16:09:29 +00004265
4266 // If Align > RegSizeInBytes, the first arg register must be even.
4267 // FIXME: This condition happens to do the right thing but it's not the
4268 // right way to test it. We want to check that the stack frame offset
4269 // of the register is aligned.
4270 if ((Align > RegSizeInBytes) && (FirstReg % 2)) {
4271 State->AllocateReg(IntArgRegs[FirstReg], ShadowRegs[FirstReg]);
4272 ++FirstReg;
4273 }
4274
4275 // Mark the registers allocated.
Rui Ueyamada00f2f2016-01-14 21:06:47 +00004276 Size = alignTo(Size, RegSizeInBytes);
Daniel Sanders23e98772014-11-02 16:09:29 +00004277 for (unsigned I = FirstReg; Size > 0 && (I < IntArgRegs.size());
4278 Size -= RegSizeInBytes, ++I, ++NumRegs)
4279 State->AllocateReg(IntArgRegs[I], ShadowRegs[I]);
4280 }
4281
4282 State->addInRegsParamInfo(FirstReg, FirstReg + NumRegs);
4283}
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00004284
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004285MachineBasicBlock *MipsTargetLowering::emitPseudoSELECT(MachineInstr &MI,
4286 MachineBasicBlock *BB,
4287 bool isFPCmp,
4288 unsigned Opc) const {
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00004289 assert(!(Subtarget.hasMips4() || Subtarget.hasMips32()) &&
4290 "Subtarget already supports SELECT nodes with the use of"
4291 "conditional-move instructions.");
4292
4293 const TargetInstrInfo *TII =
Eric Christopher96e72c62015-01-29 23:27:36 +00004294 Subtarget.getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004295 DebugLoc DL = MI.getDebugLoc();
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00004296
4297 // To "insert" a SELECT instruction, we actually have to insert the
4298 // diamond control-flow pattern. The incoming instruction knows the
4299 // destination vreg to set, the condition code register to branch on, the
4300 // true/false values to select between, and a branch opcode to use.
4301 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +00004302 MachineFunction::iterator It = ++BB->getIterator();
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00004303
4304 // thisMBB:
4305 // ...
4306 // TrueVal = ...
4307 // setcc r1, r2, r3
4308 // bNE r1, r0, copy1MBB
4309 // fallthrough --> copy0MBB
4310 MachineBasicBlock *thisMBB = BB;
4311 MachineFunction *F = BB->getParent();
4312 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
4313 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
4314 F->insert(It, copy0MBB);
4315 F->insert(It, sinkMBB);
4316
4317 // Transfer the remainder of BB and its successor edges to sinkMBB.
4318 sinkMBB->splice(sinkMBB->begin(), BB,
4319 std::next(MachineBasicBlock::iterator(MI)), BB->end());
4320 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
4321
4322 // Next, add the true and fallthrough blocks as its successors.
4323 BB->addSuccessor(copy0MBB);
4324 BB->addSuccessor(sinkMBB);
4325
4326 if (isFPCmp) {
4327 // bc1[tf] cc, sinkMBB
4328 BuildMI(BB, DL, TII->get(Opc))
Simon Dardisba92b032016-09-09 11:06:01 +00004329 .addReg(MI.getOperand(1).getReg())
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004330 .addMBB(sinkMBB);
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00004331 } else {
4332 // bne rs, $0, sinkMBB
4333 BuildMI(BB, DL, TII->get(Opc))
Simon Dardisba92b032016-09-09 11:06:01 +00004334 .addReg(MI.getOperand(1).getReg())
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004335 .addReg(Mips::ZERO)
4336 .addMBB(sinkMBB);
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00004337 }
4338
4339 // copy0MBB:
4340 // %FalseValue = ...
4341 // # fallthrough to sinkMBB
4342 BB = copy0MBB;
4343
4344 // Update machine-CFG edges
4345 BB->addSuccessor(sinkMBB);
4346
4347 // sinkMBB:
4348 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
4349 // ...
4350 BB = sinkMBB;
4351
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004352 BuildMI(*BB, BB->begin(), DL, TII->get(Mips::PHI), MI.getOperand(0).getReg())
Simon Dardisba92b032016-09-09 11:06:01 +00004353 .addReg(MI.getOperand(2).getReg())
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004354 .addMBB(thisMBB)
4355 .addReg(MI.getOperand(3).getReg())
4356 .addMBB(copy0MBB);
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00004357
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004358 MI.eraseFromParent(); // The pseudo instruction is gone now.
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00004359
4360 return BB;
4361}
Daniel Sanders1440bb22015-01-09 17:21:30 +00004362
4363// FIXME? Maybe this could be a TableGen attribute on some registers and
4364// this table could be generated automatically from RegInfo.
Pat Gavlina717f252015-07-09 17:40:29 +00004365unsigned MipsTargetLowering::getRegisterByName(const char* RegName, EVT VT,
4366 SelectionDAG &DAG) const {
Daniel Sanders1440bb22015-01-09 17:21:30 +00004367 // Named registers is expected to be fairly rare. For now, just support $28
4368 // since the linux kernel uses it.
4369 if (Subtarget.isGP64bit()) {
4370 unsigned Reg = StringSwitch<unsigned>(RegName)
4371 .Case("$28", Mips::GP_64)
4372 .Default(0);
4373 if (Reg)
4374 return Reg;
4375 } else {
4376 unsigned Reg = StringSwitch<unsigned>(RegName)
4377 .Case("$28", Mips::GP)
4378 .Default(0);
4379 if (Reg)
4380 return Reg;
4381 }
4382 report_fatal_error("Invalid register name global variable");
4383}