blob: 4c8f87f9381caec681b2df4d091a77de710cdc38 [file] [log] [blame]
Eugene Zelenko79220eae2017-08-03 22:12:30 +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//===----------------------------------------------------------------------===//
Eugene Zelenko79220eae2017-08-03 22:12:30 +000014
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000015#include "MipsISelLowering.h"
Craig Topperb25fda92012-03-17 18:46:09 +000016#include "InstPrinter/MipsInstPrinter.h"
17#include "MCTargetDesc/MipsBaseInfo.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000018#include "MCTargetDesc/MipsMCTargetDesc.h"
Daniel Sanders0456c152014-11-07 14:24:31 +000019#include "MipsCCState.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000020#include "MipsInstrInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "MipsMachineFunction.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000022#include "MipsRegisterInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "MipsSubtarget.h"
24#include "MipsTargetMachine.h"
25#include "MipsTargetObjectFile.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000026#include "llvm/ADT/APFloat.h"
27#include "llvm/ADT/ArrayRef.h"
28#include "llvm/ADT/SmallVector.h"
Akira Hatanaka90131ac2012-10-19 21:47:33 +000029#include "llvm/ADT/Statistic.h"
Daniel Sanders8b59af12013-11-12 12:56:01 +000030#include "llvm/ADT/StringSwitch.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000031#include "llvm/ADT/StringRef.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000032#include "llvm/CodeGen/CallingConvLower.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000033#include "llvm/CodeGen/FunctionLoweringInfo.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000034#include "llvm/CodeGen/ISDOpcodes.h"
35#include "llvm/CodeGen/MachineBasicBlock.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000036#include "llvm/CodeGen/MachineFrameInfo.h"
37#include "llvm/CodeGen/MachineFunction.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000038#include "llvm/CodeGen/MachineInstr.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000039#include "llvm/CodeGen/MachineInstrBuilder.h"
Eric Christopher79cc1e32014-09-02 22:28:02 +000040#include "llvm/CodeGen/MachineJumpTableInfo.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000041#include "llvm/CodeGen/MachineMemOperand.h"
42#include "llvm/CodeGen/MachineOperand.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000043#include "llvm/CodeGen/MachineRegisterInfo.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000044#include "llvm/CodeGen/MachineValueType.h"
45#include "llvm/CodeGen/RuntimeLibcalls.h"
46#include "llvm/CodeGen/SelectionDAG.h"
47#include "llvm/CodeGen/SelectionDAGNodes.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000048#include "llvm/CodeGen/ValueTypes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000049#include "llvm/IR/CallingConv.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000050#include "llvm/IR/Constants.h"
51#include "llvm/IR/DataLayout.h"
52#include "llvm/IR/DebugLoc.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000053#include "llvm/IR/DerivedTypes.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000054#include "llvm/IR/Function.h"
55#include "llvm/IR/GlobalValue.h"
56#include "llvm/IR/Type.h"
57#include "llvm/IR/Value.h"
58#include "llvm/MC/MCRegisterInfo.h"
59#include "llvm/Support/Casting.h"
60#include "llvm/Support/CodeGen.h"
Akira Hatanaka90131ac2012-10-19 21:47:33 +000061#include "llvm/Support/CommandLine.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000062#include "llvm/Support/Compiler.h"
Torok Edwin56d06592009-07-11 20:10:48 +000063#include "llvm/Support/ErrorHandling.h"
Eugene Zelenko79220eae2017-08-03 22:12:30 +000064#include "llvm/Support/MathExtras.h"
65#include "llvm/Target/TargetFrameLowering.h"
66#include "llvm/Target/TargetInstrInfo.h"
67#include "llvm/Target/TargetMachine.h"
68#include "llvm/Target/TargetOptions.h"
69#include "llvm/Target/TargetRegisterInfo.h"
70#include <algorithm>
71#include <cassert>
Akira Hatanaka7473b472013-08-14 00:21:25 +000072#include <cctype>
Eugene Zelenko79220eae2017-08-03 22:12:30 +000073#include <cstdint>
74#include <deque>
75#include <iterator>
76#include <utility>
77#include <vector>
NAKAMURA Takumie30303f2012-04-21 15:31:45 +000078
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000079using namespace llvm;
80
Chandler Carruth84e68b22014-04-22 02:41:26 +000081#define DEBUG_TYPE "mips-lower"
82
Akira Hatanaka90131ac2012-10-19 21:47:33 +000083STATISTIC(NumTailCalls, "Number of tail calls");
84
85static cl::opt<bool>
Akira Hatanaka59f299f2012-11-21 20:21:11 +000086LargeGOT("mxgot", cl::Hidden,
87 cl::desc("MIPS: Enable GOT larger than 64k."), cl::init(false));
88
Akira Hatanaka1cb02422013-05-20 18:07:43 +000089static cl::opt<bool>
Akira Hatanakabe76cd02013-05-21 17:17:59 +000090NoZeroDivCheck("mno-check-zero-division", cl::Hidden,
Akira Hatanaka1cb02422013-05-20 18:07:43 +000091 cl::desc("MIPS: Don't trap on integer division by zero."),
92 cl::init(false));
93
Craig Topper840beec2014-04-04 05:16:06 +000094static const MCPhysReg Mips64DPRegs[8] = {
Akira Hatanakaac8c6692012-10-27 00:29:43 +000095 Mips::D12_64, Mips::D13_64, Mips::D14_64, Mips::D15_64,
96 Mips::D16_64, Mips::D17_64, Mips::D18_64, Mips::D19_64
97};
98
Jia Liuf54f60f2012-02-28 07:46:26 +000099// If I is a shifted mask, set the size (Size) and the first bit of the
Akira Hatanaka73d78b72011-08-18 20:07:42 +0000100// mask (Pos), and return true.
Jia Liuf54f60f2012-02-28 07:46:26 +0000101// For example, if I is 0x003ff800, (Pos, Size) = (11, 11).
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000102static bool isShiftedMask(uint64_t I, uint64_t &Pos, uint64_t &Size) {
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000103 if (!isShiftedMask_64(I))
Akira Hatanaka4c0a7122013-10-07 19:33:02 +0000104 return false;
Akira Hatanaka5360f882011-08-17 02:05:42 +0000105
Benjamin Kramer5f6a9072015-02-12 15:35:40 +0000106 Size = countPopulation(I);
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000107 Pos = countTrailingZeros(I);
Akira Hatanaka73d78b72011-08-18 20:07:42 +0000108 return true;
Akira Hatanaka5360f882011-08-17 02:05:42 +0000109}
110
Simon Dardis212cccb2017-06-09 14:37:08 +0000111// The MIPS MSA ABI passes vector arguments in the integer register set.
112// The number of integer registers used is dependant on the ABI used.
113MVT MipsTargetLowering::getRegisterTypeForCallingConv(MVT VT) const {
114 if (VT.isVector() && Subtarget.hasMSA())
115 return Subtarget.isABI_O32() ? MVT::i32 : MVT::i64;
116 return MipsTargetLowering::getRegisterType(VT);
117}
118
119MVT MipsTargetLowering::getRegisterTypeForCallingConv(LLVMContext &Context,
120 EVT VT) const {
121 if (VT.isVector()) {
122 if (Subtarget.isABI_O32()) {
123 return MVT::i32;
124 } else {
125 return (VT.getSizeInBits() == 32) ? MVT::i32 : MVT::i64;
126 }
127 }
128 return MipsTargetLowering::getRegisterType(Context, VT);
129}
130
131unsigned MipsTargetLowering::getNumRegistersForCallingConv(LLVMContext &Context,
132 EVT VT) const {
133 if (VT.isVector())
134 return std::max((VT.getSizeInBits() / (Subtarget.isABI_O32() ? 32 : 64)),
135 1U);
136 return MipsTargetLowering::getNumRegisters(Context, VT);
137}
138
139unsigned MipsTargetLowering::getVectorTypeBreakdownForCallingConv(
140 LLVMContext &Context, EVT VT, EVT &IntermediateVT,
141 unsigned &NumIntermediates, MVT &RegisterVT) const {
Simon Dardis212cccb2017-06-09 14:37:08 +0000142 // Break down vector types to either 2 i64s or 4 i32s.
143 RegisterVT = getRegisterTypeForCallingConv(Context, VT) ;
144 IntermediateVT = RegisterVT;
145 NumIntermediates = VT.getSizeInBits() < RegisterVT.getSizeInBits()
146 ? VT.getVectorNumElements()
147 : VT.getSizeInBits() / RegisterVT.getSizeInBits();
148
149 return NumIntermediates;
150}
151
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000152SDValue MipsTargetLowering::getGlobalReg(SelectionDAG &DAG, EVT Ty) const {
Akira Hatanakab049aef2012-02-24 22:34:47 +0000153 MipsFunctionInfo *FI = DAG.getMachineFunction().getInfo<MipsFunctionInfo>();
154 return DAG.getRegister(FI->getGlobalBaseReg(), Ty);
155}
156
Akira Hatanakad8f10ce2013-09-27 19:51:35 +0000157SDValue MipsTargetLowering::getTargetNode(GlobalAddressSDNode *N, EVT Ty,
158 SelectionDAG &DAG,
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000159 unsigned Flag) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +0000160 return DAG.getTargetGlobalAddress(N->getGlobal(), SDLoc(N), Ty, 0, Flag);
Akira Hatanakafd04ad42012-11-21 20:26:38 +0000161}
162
Akira Hatanakad8f10ce2013-09-27 19:51:35 +0000163SDValue MipsTargetLowering::getTargetNode(ExternalSymbolSDNode *N, EVT Ty,
164 SelectionDAG &DAG,
165 unsigned Flag) const {
166 return DAG.getTargetExternalSymbol(N->getSymbol(), Ty, Flag);
167}
168
169SDValue MipsTargetLowering::getTargetNode(BlockAddressSDNode *N, EVT Ty,
170 SelectionDAG &DAG,
171 unsigned Flag) const {
172 return DAG.getTargetBlockAddress(N->getBlockAddress(), Ty, 0, Flag);
173}
174
175SDValue MipsTargetLowering::getTargetNode(JumpTableSDNode *N, EVT Ty,
176 SelectionDAG &DAG,
177 unsigned Flag) const {
178 return DAG.getTargetJumpTable(N->getIndex(), Ty, Flag);
179}
180
181SDValue MipsTargetLowering::getTargetNode(ConstantPoolSDNode *N, EVT Ty,
182 SelectionDAG &DAG,
183 unsigned Flag) const {
184 return DAG.getTargetConstantPool(N->getConstVal(), Ty, N->getAlignment(),
185 N->getOffset(), Flag);
Akira Hatanakafd04ad42012-11-21 20:26:38 +0000186}
187
Chris Lattner5e693ed2009-07-28 03:13:23 +0000188const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +0000189 switch ((MipsISD::NodeType)Opcode) {
190 case MipsISD::FIRST_NUMBER: break;
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000191 case MipsISD::JmpLink: return "MipsISD::JmpLink";
Akira Hatanaka91318df2012-10-19 20:59:39 +0000192 case MipsISD::TailCall: return "MipsISD::TailCall";
Simon Dardisca74dd72017-01-27 11:36:52 +0000193 case MipsISD::Highest: return "MipsISD::Highest";
194 case MipsISD::Higher: return "MipsISD::Higher";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000195 case MipsISD::Hi: return "MipsISD::Hi";
196 case MipsISD::Lo: return "MipsISD::Lo";
Simon Dardisca74dd72017-01-27 11:36:52 +0000197 case MipsISD::GotHi: return "MipsISD::GotHi";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000198 case MipsISD::GPRel: return "MipsISD::GPRel";
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +0000199 case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000200 case MipsISD::Ret: return "MipsISD::Ret";
Vasileios Kalintiris43dff0c2015-10-26 12:38:43 +0000201 case MipsISD::ERet: return "MipsISD::ERet";
Akira Hatanakac0b02062013-01-30 00:26:49 +0000202 case MipsISD::EH_RETURN: return "MipsISD::EH_RETURN";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000203 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
204 case MipsISD::FPCmp: return "MipsISD::FPCmp";
Stefan Maksimovicbe0bc712017-07-20 13:08:18 +0000205 case MipsISD::FSELECT: return "MipsISD::FSELECT";
206 case MipsISD::MTC1_D64: return "MipsISD::MTC1_D64";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000207 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
208 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000209 case MipsISD::TruncIntFP: return "MipsISD::TruncIntFP";
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000210 case MipsISD::MFHI: return "MipsISD::MFHI";
211 case MipsISD::MFLO: return "MipsISD::MFLO";
212 case MipsISD::MTLOHI: return "MipsISD::MTLOHI";
Akira Hatanaka28721bd2013-03-30 01:14:04 +0000213 case MipsISD::Mult: return "MipsISD::Mult";
214 case MipsISD::Multu: return "MipsISD::Multu";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000215 case MipsISD::MAdd: return "MipsISD::MAdd";
216 case MipsISD::MAddu: return "MipsISD::MAddu";
217 case MipsISD::MSub: return "MipsISD::MSub";
218 case MipsISD::MSubu: return "MipsISD::MSubu";
219 case MipsISD::DivRem: return "MipsISD::DivRem";
220 case MipsISD::DivRemU: return "MipsISD::DivRemU";
Akira Hatanaka28721bd2013-03-30 01:14:04 +0000221 case MipsISD::DivRem16: return "MipsISD::DivRem16";
222 case MipsISD::DivRemU16: return "MipsISD::DivRemU16";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000223 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
224 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
Akira Hatanakafaa88c02011-12-12 22:38:19 +0000225 case MipsISD::Wrapper: return "MipsISD::Wrapper";
Matthias Braund04893f2015-05-07 21:33:59 +0000226 case MipsISD::DynAlloc: return "MipsISD::DynAlloc";
Akira Hatanakaa4c09bc2011-07-19 23:30:50 +0000227 case MipsISD::Sync: return "MipsISD::Sync";
Akira Hatanaka5360f882011-08-17 02:05:42 +0000228 case MipsISD::Ext: return "MipsISD::Ext";
229 case MipsISD::Ins: return "MipsISD::Ins";
Petar Jovanovicb71386a2017-03-15 13:10:08 +0000230 case MipsISD::CIns: return "MipsISD::CIns";
Akira Hatanakab9ebf8d2012-06-02 00:03:12 +0000231 case MipsISD::LWL: return "MipsISD::LWL";
232 case MipsISD::LWR: return "MipsISD::LWR";
233 case MipsISD::SWL: return "MipsISD::SWL";
234 case MipsISD::SWR: return "MipsISD::SWR";
235 case MipsISD::LDL: return "MipsISD::LDL";
236 case MipsISD::LDR: return "MipsISD::LDR";
237 case MipsISD::SDL: return "MipsISD::SDL";
238 case MipsISD::SDR: return "MipsISD::SDR";
Akira Hatanaka233ac532012-09-21 23:52:47 +0000239 case MipsISD::EXTP: return "MipsISD::EXTP";
240 case MipsISD::EXTPDP: return "MipsISD::EXTPDP";
241 case MipsISD::EXTR_S_H: return "MipsISD::EXTR_S_H";
242 case MipsISD::EXTR_W: return "MipsISD::EXTR_W";
243 case MipsISD::EXTR_R_W: return "MipsISD::EXTR_R_W";
244 case MipsISD::EXTR_RS_W: return "MipsISD::EXTR_RS_W";
245 case MipsISD::SHILO: return "MipsISD::SHILO";
246 case MipsISD::MTHLIP: return "MipsISD::MTHLIP";
Matthias Braund04893f2015-05-07 21:33:59 +0000247 case MipsISD::MULSAQ_S_W_PH: return "MipsISD::MULSAQ_S_W_PH";
248 case MipsISD::MAQ_S_W_PHL: return "MipsISD::MAQ_S_W_PHL";
249 case MipsISD::MAQ_S_W_PHR: return "MipsISD::MAQ_S_W_PHR";
250 case MipsISD::MAQ_SA_W_PHL: return "MipsISD::MAQ_SA_W_PHL";
251 case MipsISD::MAQ_SA_W_PHR: return "MipsISD::MAQ_SA_W_PHR";
252 case MipsISD::DPAU_H_QBL: return "MipsISD::DPAU_H_QBL";
253 case MipsISD::DPAU_H_QBR: return "MipsISD::DPAU_H_QBR";
254 case MipsISD::DPSU_H_QBL: return "MipsISD::DPSU_H_QBL";
255 case MipsISD::DPSU_H_QBR: return "MipsISD::DPSU_H_QBR";
256 case MipsISD::DPAQ_S_W_PH: return "MipsISD::DPAQ_S_W_PH";
257 case MipsISD::DPSQ_S_W_PH: return "MipsISD::DPSQ_S_W_PH";
258 case MipsISD::DPAQ_SA_L_W: return "MipsISD::DPAQ_SA_L_W";
259 case MipsISD::DPSQ_SA_L_W: return "MipsISD::DPSQ_SA_L_W";
260 case MipsISD::DPA_W_PH: return "MipsISD::DPA_W_PH";
261 case MipsISD::DPS_W_PH: return "MipsISD::DPS_W_PH";
262 case MipsISD::DPAQX_S_W_PH: return "MipsISD::DPAQX_S_W_PH";
263 case MipsISD::DPAQX_SA_W_PH: return "MipsISD::DPAQX_SA_W_PH";
264 case MipsISD::DPAX_W_PH: return "MipsISD::DPAX_W_PH";
265 case MipsISD::DPSX_W_PH: return "MipsISD::DPSX_W_PH";
266 case MipsISD::DPSQX_S_W_PH: return "MipsISD::DPSQX_S_W_PH";
267 case MipsISD::DPSQX_SA_W_PH: return "MipsISD::DPSQX_SA_W_PH";
268 case MipsISD::MULSA_W_PH: return "MipsISD::MULSA_W_PH";
Akira Hatanaka233ac532012-09-21 23:52:47 +0000269 case MipsISD::MULT: return "MipsISD::MULT";
270 case MipsISD::MULTU: return "MipsISD::MULTU";
Jia Liu434874d2013-03-04 01:06:54 +0000271 case MipsISD::MADD_DSP: return "MipsISD::MADD_DSP";
Akira Hatanaka233ac532012-09-21 23:52:47 +0000272 case MipsISD::MADDU_DSP: return "MipsISD::MADDU_DSP";
273 case MipsISD::MSUB_DSP: return "MipsISD::MSUB_DSP";
274 case MipsISD::MSUBU_DSP: return "MipsISD::MSUBU_DSP";
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000275 case MipsISD::SHLL_DSP: return "MipsISD::SHLL_DSP";
276 case MipsISD::SHRA_DSP: return "MipsISD::SHRA_DSP";
277 case MipsISD::SHRL_DSP: return "MipsISD::SHRL_DSP";
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000278 case MipsISD::SETCC_DSP: return "MipsISD::SETCC_DSP";
279 case MipsISD::SELECT_CC_DSP: return "MipsISD::SELECT_CC_DSP";
Daniel Sandersce09d072013-08-28 12:14:50 +0000280 case MipsISD::VALL_ZERO: return "MipsISD::VALL_ZERO";
281 case MipsISD::VANY_ZERO: return "MipsISD::VANY_ZERO";
282 case MipsISD::VALL_NONZERO: return "MipsISD::VALL_NONZERO";
283 case MipsISD::VANY_NONZERO: return "MipsISD::VANY_NONZERO";
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000284 case MipsISD::VCEQ: return "MipsISD::VCEQ";
285 case MipsISD::VCLE_S: return "MipsISD::VCLE_S";
286 case MipsISD::VCLE_U: return "MipsISD::VCLE_U";
287 case MipsISD::VCLT_S: return "MipsISD::VCLT_S";
288 case MipsISD::VCLT_U: return "MipsISD::VCLT_U";
Daniel Sanders3ce56622013-09-24 12:18:31 +0000289 case MipsISD::VSMAX: return "MipsISD::VSMAX";
290 case MipsISD::VSMIN: return "MipsISD::VSMIN";
291 case MipsISD::VUMAX: return "MipsISD::VUMAX";
292 case MipsISD::VUMIN: return "MipsISD::VUMIN";
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000293 case MipsISD::VEXTRACT_SEXT_ELT: return "MipsISD::VEXTRACT_SEXT_ELT";
294 case MipsISD::VEXTRACT_ZEXT_ELT: return "MipsISD::VEXTRACT_ZEXT_ELT";
Daniel Sandersf7456c72013-09-23 13:22:24 +0000295 case MipsISD::VNOR: return "MipsISD::VNOR";
Daniel Sanderse5087042013-09-24 14:02:15 +0000296 case MipsISD::VSHF: return "MipsISD::VSHF";
Daniel Sanders26307182013-09-24 14:20:00 +0000297 case MipsISD::SHF: return "MipsISD::SHF";
Daniel Sanders2ed228b2013-09-24 14:36:12 +0000298 case MipsISD::ILVEV: return "MipsISD::ILVEV";
299 case MipsISD::ILVOD: return "MipsISD::ILVOD";
300 case MipsISD::ILVL: return "MipsISD::ILVL";
301 case MipsISD::ILVR: return "MipsISD::ILVR";
Daniel Sandersfae5f2a2013-09-24 14:53:25 +0000302 case MipsISD::PCKEV: return "MipsISD::PCKEV";
303 case MipsISD::PCKOD: return "MipsISD::PCKOD";
Daniel Sandersb50ccf82014-04-01 10:35:28 +0000304 case MipsISD::INSVE: return "MipsISD::INSVE";
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000305 }
Matthias Braund04893f2015-05-07 21:33:59 +0000306 return nullptr;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000307}
308
Eric Christopherb1526602014-09-19 23:30:42 +0000309MipsTargetLowering::MipsTargetLowering(const MipsTargetMachine &TM,
Eric Christopher8924d272014-07-18 23:25:04 +0000310 const MipsSubtarget &STI)
Eric Christopher96e72c62015-01-29 23:27:36 +0000311 : TargetLowering(TM), Subtarget(STI), ABI(TM.getABI()) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000312 // Mips does not have i1 type, so use i32 for
Wesley Peck527da1b2010-11-23 03:31:01 +0000313 // setcc operations results (slt, sgt, ...).
Duncan Sands8d6e2e12008-11-23 15:47:28 +0000314 setBooleanContents(ZeroOrOneBooleanContent);
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000315 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Daniel Sanderscbd44c52014-07-10 10:18:12 +0000316 // The cmp.cond.fmt instruction in MIPS32r6/MIPS64r6 uses 0 and -1 like MSA
317 // does. Integer booleans still use 0 and 1.
Eric Christopher1c29a652014-07-18 22:55:25 +0000318 if (Subtarget.hasMips32r6())
Daniel Sanderscbd44c52014-07-10 10:18:12 +0000319 setBooleanContents(ZeroOrOneBooleanContent,
320 ZeroOrNegativeOneBooleanContent);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000321
Wesley Peck527da1b2010-11-23 03:31:01 +0000322 // Load extented operations for i1 types must be promoted
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000323 for (MVT VT : MVT::integer_valuetypes()) {
324 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
325 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
326 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
327 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000328
Pirama Arumuga Nainar34056de2015-04-20 20:15:36 +0000329 // MIPS doesn't have extending float->double load/store. Set LoadExtAction
330 // for f32, f16
331 for (MVT VT : MVT::fp_valuetypes()) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000332 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
Pirama Arumuga Nainar34056de2015-04-20 20:15:36 +0000333 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f16, Expand);
334 }
335
336 // Set LoadExtAction for f16 vectors to Expand
337 for (MVT VT : MVT::fp_vector_valuetypes()) {
338 MVT F16VT = MVT::getVectorVT(MVT::f16, VT.getVectorNumElements());
339 if (F16VT.isValid())
340 setLoadExtAction(ISD::EXTLOAD, VT, F16VT, Expand);
341 }
342
343 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
344 setTruncStoreAction(MVT::f64, MVT::f16, Expand);
345
Owen Anderson9f944592009-08-11 20:47:22 +0000346 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman39d6faa2009-07-17 02:28:12 +0000347
Wesley Peck527da1b2010-11-23 03:31:01 +0000348 // Used by legalize types to correctly generate the setcc result.
349 // Without this, every float setcc comes with a AND/OR with the result,
350 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +0000351 // which is used implicitly by brcond and select operations.
Vasileios Kalintiris36901dd2016-03-01 20:25:43 +0000352 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +0000353
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000354 // Mips Custom Operations
Joerg Sonnenberger1a7eec62016-11-15 12:39:46 +0000355 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000356 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000357 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000358 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
359 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
360 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Simon Dardisba92b032016-09-09 11:06:01 +0000361 setOperationAction(ISD::SELECT, MVT::f32, Custom);
362 setOperationAction(ISD::SELECT, MVT::f64, Custom);
363 setOperationAction(ISD::SELECT, MVT::i32, Custom);
364 setOperationAction(ISD::SETCC, MVT::f32, Custom);
365 setOperationAction(ISD::SETCC, MVT::f64, Custom);
366 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000367 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
368 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000369 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000370
Eric Christopher1c29a652014-07-18 22:55:25 +0000371 if (Subtarget.isGP64bit()) {
Akira Hatanakada00aa82012-03-10 00:03:50 +0000372 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
373 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
374 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
375 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
376 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
Simon Dardisba92b032016-09-09 11:06:01 +0000377 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Akira Hatanaka019e5922012-06-02 00:04:42 +0000378 setOperationAction(ISD::LOAD, MVT::i64, Custom);
379 setOperationAction(ISD::STORE, MVT::i64, Custom);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000380 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +0000381 setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom);
382 setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom);
383 setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000384 }
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +0000385
Eric Christopher1c29a652014-07-18 22:55:25 +0000386 if (!Subtarget.isGP64bit()) {
Akira Hatanaka0a8ab712012-05-09 00:55:21 +0000387 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
388 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
389 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
390 }
391
Hal Finkel5081ac22016-09-01 10:28:47 +0000392 setOperationAction(ISD::EH_DWARF_CFA, MVT::i32, Custom);
Eric Christopher1c29a652014-07-18 22:55:25 +0000393 if (Subtarget.isGP64bit())
Hal Finkel5081ac22016-09-01 10:28:47 +0000394 setOperationAction(ISD::EH_DWARF_CFA, MVT::i64, Custom);
Akira Hatanaka28e02ec2012-11-07 19:10:58 +0000395
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000396 setOperationAction(ISD::SDIV, MVT::i32, Expand);
397 setOperationAction(ISD::SREM, MVT::i32, Expand);
398 setOperationAction(ISD::UDIV, MVT::i32, Expand);
399 setOperationAction(ISD::UREM, MVT::i32, Expand);
Akira Hatanakab1538f92011-10-03 21:06:13 +0000400 setOperationAction(ISD::SDIV, MVT::i64, Expand);
401 setOperationAction(ISD::SREM, MVT::i64, Expand);
402 setOperationAction(ISD::UDIV, MVT::i64, Expand);
403 setOperationAction(ISD::UREM, MVT::i64, Expand);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000404
Simon Dardis250256f2017-07-13 11:28:05 +0000405 if (!(Subtarget.hasDSP() && Subtarget.hasMips32r2())) {
406 setOperationAction(ISD::ADDC, MVT::i32, Expand);
407 setOperationAction(ISD::ADDE, MVT::i32, Expand);
408 }
409
410 setOperationAction(ISD::ADDC, MVT::i64, Expand);
411 setOperationAction(ISD::ADDE, MVT::i64, Expand);
412 setOperationAction(ISD::SUBC, MVT::i32, Expand);
413 setOperationAction(ISD::SUBE, MVT::i32, Expand);
414 setOperationAction(ISD::SUBC, MVT::i64, Expand);
415 setOperationAction(ISD::SUBE, MVT::i64, Expand);
416
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000417 // Operations not directly supported by Mips.
Tom Stellardb1588fc2013-03-08 15:36:57 +0000418 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
419 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
420 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
421 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
Tom Stellard3787b122014-06-10 16:01:29 +0000422 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
423 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
Matt Arsenaulta0e5cd52016-01-11 16:44:48 +0000424 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
425 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000426 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Akira Hatanaka79aed152011-12-20 23:40:56 +0000427 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000428 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Akira Hatanaka79aed152011-12-20 23:40:56 +0000429 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000430 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Eric Christopher1c29a652014-07-18 22:55:25 +0000431 if (Subtarget.hasCnMips()) {
Kai Nacke93fe5e82014-03-20 11:51:58 +0000432 setOperationAction(ISD::CTPOP, MVT::i32, Legal);
433 setOperationAction(ISD::CTPOP, MVT::i64, Legal);
434 } else {
435 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
436 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
437 }
Owen Anderson9f944592009-08-11 20:47:22 +0000438 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
Akira Hatanaka410ce9c2011-12-21 00:14:05 +0000439 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000440 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Akira Hatanaka7ba8a8d2011-09-30 18:51:46 +0000441 setOperationAction(ISD::ROTL, MVT::i64, Expand);
Akira Hatanaka33a25af2012-07-31 20:54:48 +0000442 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
443 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Bruno Cardoso Lopesd47180e2010-12-09 17:32:30 +0000444
Eric Christopher1c29a652014-07-18 22:55:25 +0000445 if (!Subtarget.hasMips32r2())
Bruno Cardoso Lopesd47180e2010-12-09 17:32:30 +0000446 setOperationAction(ISD::ROTR, MVT::i32, Expand);
447
Eric Christopher1c29a652014-07-18 22:55:25 +0000448 if (!Subtarget.hasMips64r2())
Akira Hatanaka7ba8a8d2011-09-30 18:51:46 +0000449 setOperationAction(ISD::ROTR, MVT::i64, Expand);
450
Owen Anderson9f944592009-08-11 20:47:22 +0000451 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes22b69db2011-03-04 18:54:14 +0000452 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000453 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes22b69db2011-03-04 18:54:14 +0000454 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000455 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
456 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000457 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanakadfb8cda2011-05-23 22:23:58 +0000458 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000459 setOperationAction(ISD::FLOG, MVT::f32, Expand);
460 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
461 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
462 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000463 setOperationAction(ISD::FMA, MVT::f32, Expand);
464 setOperationAction(ISD::FMA, MVT::f64, Expand);
Akira Hatanaka0603ad82012-03-29 18:43:11 +0000465 setOperationAction(ISD::FREM, MVT::f32, Expand);
466 setOperationAction(ISD::FREM, MVT::f64, Expand);
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000467
Pirama Arumuga Nainar34056de2015-04-20 20:15:36 +0000468 // Lower f16 conversion operations into library calls
469 setOperationAction(ISD::FP16_TO_FP, MVT::f32, Expand);
470 setOperationAction(ISD::FP_TO_FP16, MVT::f32, Expand);
471 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand);
472 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Expand);
473
Akira Hatanakac0b02062013-01-30 00:26:49 +0000474 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
475
Daniel Sanders2b553d42014-08-01 09:17:39 +0000476 setOperationAction(ISD::VASTART, MVT::Other, Custom);
477 setOperationAction(ISD::VAARG, MVT::Other, Custom);
Bruno Cardoso Lopes048ffab2011-03-09 19:22:22 +0000478 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
479 setOperationAction(ISD::VAEND, MVT::Other, Expand);
480
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000481 // Use the default for now
Owen Anderson9f944592009-08-11 20:47:22 +0000482 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
483 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman26a48482011-07-27 22:21:52 +0000484
Vasileios Kalintirisb04672c2015-11-06 12:07:20 +0000485 if (!Subtarget.isGP64bit()) {
486 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
487 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
488 }
Eli Friedman7dfa7912011-08-29 18:23:02 +0000489
Eric Christopher1c29a652014-07-18 22:55:25 +0000490 if (!Subtarget.hasMips32r2()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000491 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
492 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000493 }
494
Daniel Sanders070fd1c2014-05-12 12:41:59 +0000495 // MIPS16 lacks MIPS32's clz and clo instructions.
Eric Christopher1c29a652014-07-18 22:55:25 +0000496 if (!Subtarget.hasMips32() || Subtarget.inMips16Mode())
Owen Anderson9f944592009-08-11 20:47:22 +0000497 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Eric Christopher1c29a652014-07-18 22:55:25 +0000498 if (!Subtarget.hasMips64())
Akira Hatanaka1d8efab2011-12-21 00:20:27 +0000499 setOperationAction(ISD::CTLZ, MVT::i64, Expand);
Bruno Cardoso Lopes93da7e62008-08-08 06:16:31 +0000500
Eric Christopher1c29a652014-07-18 22:55:25 +0000501 if (!Subtarget.hasMips32r2())
Owen Anderson9f944592009-08-11 20:47:22 +0000502 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Eric Christopher1c29a652014-07-18 22:55:25 +0000503 if (!Subtarget.hasMips64r2())
Akira Hatanaka4706ac92011-12-20 23:56:43 +0000504 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000505
Eric Christopher1c29a652014-07-18 22:55:25 +0000506 if (Subtarget.isGP64bit()) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000507 setLoadExtAction(ISD::SEXTLOAD, MVT::i64, MVT::i32, Custom);
508 setLoadExtAction(ISD::ZEXTLOAD, MVT::i64, MVT::i32, Custom);
509 setLoadExtAction(ISD::EXTLOAD, MVT::i64, MVT::i32, Custom);
Akira Hatanaka019e5922012-06-02 00:04:42 +0000510 setTruncStoreAction(MVT::i64, MVT::i32, Custom);
511 }
512
Akira Hatanakaa3d9ab92013-07-26 20:58:55 +0000513 setOperationAction(ISD::TRAP, MVT::Other, Legal);
514
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000515 setTargetDAGCombine(ISD::SDIVREM);
516 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka5e152182012-03-08 03:26:37 +0000517 setTargetDAGCombine(ISD::SELECT);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000518 setTargetDAGCombine(ISD::AND);
519 setTargetDAGCombine(ISD::OR);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000520 setTargetDAGCombine(ISD::ADD);
Simon Dardis250256f2017-07-13 11:28:05 +0000521 setTargetDAGCombine(ISD::SUB);
Vasileios Kalintiris3751d412016-04-13 15:07:45 +0000522 setTargetDAGCombine(ISD::AssertZext);
Petar Jovanovicb71386a2017-03-15 13:10:08 +0000523 setTargetDAGCombine(ISD::SHL);
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000524
Vasileios Kalintiris1ed49fd2016-09-07 10:01:18 +0000525 if (ABI.IsO32()) {
526 // These libcalls are not available in 32-bit.
527 setLibcallName(RTLIB::SHL_I128, nullptr);
528 setLibcallName(RTLIB::SRL_I128, nullptr);
529 setLibcallName(RTLIB::SRA_I128, nullptr);
530 }
531
Eric Christopher1c29a652014-07-18 22:55:25 +0000532 setMinFunctionAlignment(Subtarget.isGP64bit() ? 3 : 2);
Eli Friedman2518f832011-05-06 20:34:06 +0000533
Daniel Sanders2b553d42014-08-01 09:17:39 +0000534 // The arguments on the stack are defined in terms of 4-byte slots on O32
535 // and 8-byte slots on N32/N64.
Eric Christopher96e72c62015-01-29 23:27:36 +0000536 setMinStackArgumentAlignment((ABI.IsN32() || ABI.IsN64()) ? 8 : 4);
Daniel Sanders2b553d42014-08-01 09:17:39 +0000537
Eric Christopher96e72c62015-01-29 23:27:36 +0000538 setStackPointerRegisterToSaveRestore(ABI.IsN64() ? Mips::SP_64 : Mips::SP);
Akira Hatanakaaa560002011-05-26 18:59:03 +0000539
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000540 MaxStoresPerMemcpy = 16;
Zoran Jovanovicff9d5f32013-12-19 16:12:56 +0000541
Eric Christopher1c29a652014-07-18 22:55:25 +0000542 isMicroMips = Subtarget.inMicroMipsMode();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000543}
544
Eric Christopherb1526602014-09-19 23:30:42 +0000545const MipsTargetLowering *MipsTargetLowering::create(const MipsTargetMachine &TM,
Eric Christopher8924d272014-07-18 23:25:04 +0000546 const MipsSubtarget &STI) {
547 if (STI.inMips16Mode())
Eugene Zelenko79220eae2017-08-03 22:12:30 +0000548 return createMips16TargetLowering(TM, STI);
Jia Liuf54f60f2012-02-28 07:46:26 +0000549
Eugene Zelenko79220eae2017-08-03 22:12:30 +0000550 return createMipsSETargetLowering(TM, STI);
Akira Hatanaka2fcc1cf2011-08-12 21:30:06 +0000551}
552
Reed Kotler720c5ca2014-04-17 22:15:34 +0000553// Create a fast isel object.
554FastISel *
555MipsTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
556 const TargetLibraryInfo *libInfo) const {
Vasileios Kalintiris3955b752016-10-18 13:05:42 +0000557 const MipsTargetMachine &TM =
558 static_cast<const MipsTargetMachine &>(funcInfo.MF->getTarget());
559
560 // We support only the standard encoding [MIPS32,MIPS32R5] ISAs.
561 bool UseFastISel = TM.Options.EnableFastISel && Subtarget.hasMips32() &&
562 !Subtarget.hasMips32r6() && !Subtarget.inMips16Mode() &&
563 !Subtarget.inMicroMipsMode();
564
Petar Jovanovic3c039d92017-06-07 12:59:53 +0000565 // Disable if either of the following is true:
566 // We do not generate PIC, the ABI is not O32, LargeGOT is being used.
567 if (!TM.isPositionIndependent() || !TM.getABI().IsO32() || LargeGOT)
Vasileios Kalintiris3955b752016-10-18 13:05:42 +0000568 UseFastISel = false;
569
570 return UseFastISel ? Mips::createFastISel(funcInfo, libInfo) : nullptr;
Reed Kotler720c5ca2014-04-17 22:15:34 +0000571}
572
Mehdi Amini44ede332015-07-09 02:09:04 +0000573EVT MipsTargetLowering::getSetCCResultType(const DataLayout &, LLVMContext &,
574 EVT VT) const {
Vasileios Kalintiris36901dd2016-03-01 20:25:43 +0000575 if (!VT.isVector())
576 return MVT::i32;
577 return VT.changeVectorElementTypeToInteger();
Scott Michela6729e82008-03-10 15:42:14 +0000578}
579
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000580static SDValue performDivRemCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000581 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000582 const MipsSubtarget &Subtarget) {
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000583 if (DCI.isBeforeLegalizeOps())
584 return SDValue();
585
Akira Hatanakab1538f92011-10-03 21:06:13 +0000586 EVT Ty = N->getValueType(0);
Akira Hatanaka8002a3f2013-08-14 00:47:08 +0000587 unsigned LO = (Ty == MVT::i32) ? Mips::LO0 : Mips::LO0_64;
588 unsigned HI = (Ty == MVT::i32) ? Mips::HI0 : Mips::HI0_64;
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000589 unsigned Opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem16 :
590 MipsISD::DivRemU16;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000591 SDLoc DL(N);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000592
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000593 SDValue DivRem = DAG.getNode(Opc, DL, MVT::Glue,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000594 N->getOperand(0), N->getOperand(1));
595 SDValue InChain = DAG.getEntryNode();
596 SDValue InGlue = DivRem;
597
598 // insert MFLO
599 if (N->hasAnyUseOfValue(0)) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000600 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, DL, LO, Ty,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000601 InGlue);
602 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
603 InChain = CopyFromLo.getValue(1);
604 InGlue = CopyFromLo.getValue(2);
605 }
606
607 // insert MFHI
608 if (N->hasAnyUseOfValue(1)) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000609 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, DL,
Akira Hatanakab1538f92011-10-03 21:06:13 +0000610 HI, Ty, InGlue);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000611 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
612 }
613
614 return SDValue();
615}
616
Simon Dardisba92b032016-09-09 11:06:01 +0000617static Mips::CondCode condCodeToFCC(ISD::CondCode CC) {
618 switch (CC) {
619 default: llvm_unreachable("Unknown fp condition code!");
620 case ISD::SETEQ:
621 case ISD::SETOEQ: return Mips::FCOND_OEQ;
622 case ISD::SETUNE: return Mips::FCOND_UNE;
623 case ISD::SETLT:
624 case ISD::SETOLT: return Mips::FCOND_OLT;
625 case ISD::SETGT:
626 case ISD::SETOGT: return Mips::FCOND_OGT;
627 case ISD::SETLE:
628 case ISD::SETOLE: return Mips::FCOND_OLE;
629 case ISD::SETGE:
630 case ISD::SETOGE: return Mips::FCOND_OGE;
631 case ISD::SETULT: return Mips::FCOND_ULT;
632 case ISD::SETULE: return Mips::FCOND_ULE;
633 case ISD::SETUGT: return Mips::FCOND_UGT;
634 case ISD::SETUGE: return Mips::FCOND_UGE;
635 case ISD::SETUO: return Mips::FCOND_UN;
636 case ISD::SETO: return Mips::FCOND_OR;
637 case ISD::SETNE:
638 case ISD::SETONE: return Mips::FCOND_ONE;
639 case ISD::SETUEQ: return Mips::FCOND_UEQ;
640 }
641}
642
Simon Dardisba92b032016-09-09 11:06:01 +0000643/// This function returns true if the floating point conditional branches and
644/// conditional moves which use condition code CC should be inverted.
645static bool invertFPCondCodeUser(Mips::CondCode CC) {
646 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
647 return false;
648
649 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
650 "Illegal Condition Code");
651
652 return true;
653}
654
655// Creates and returns an FPCmp node from a setcc node.
656// Returns Op if setcc is not a floating point comparison.
657static SDValue createFPCmp(SelectionDAG &DAG, const SDValue &Op) {
658 // must be a SETCC node
659 if (Op.getOpcode() != ISD::SETCC)
660 return Op;
661
662 SDValue LHS = Op.getOperand(0);
663
664 if (!LHS.getValueType().isFloatingPoint())
665 return Op;
666
667 SDValue RHS = Op.getOperand(1);
668 SDLoc DL(Op);
669
670 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
671 // node if necessary.
672 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
673
674 return DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS,
675 DAG.getConstant(condCodeToFCC(CC), DL, MVT::i32));
676}
677
678// Creates and returns a CMovFPT/F node.
679static SDValue createCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True,
680 SDValue False, const SDLoc &DL) {
681 ConstantSDNode *CC = cast<ConstantSDNode>(Cond.getOperand(2));
682 bool invert = invertFPCondCodeUser((Mips::CondCode)CC->getSExtValue());
683 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
684
685 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
686 True.getValueType(), True, FCC0, False, Cond);
687}
688
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000689static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000690 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000691 const MipsSubtarget &Subtarget) {
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000692 if (DCI.isBeforeLegalizeOps())
693 return SDValue();
694
695 SDValue SetCC = N->getOperand(0);
696
697 if ((SetCC.getOpcode() != ISD::SETCC) ||
698 !SetCC.getOperand(0).getValueType().isInteger())
699 return SDValue();
700
701 SDValue False = N->getOperand(2);
702 EVT FalseTy = False.getValueType();
703
704 if (!FalseTy.isInteger())
705 return SDValue();
706
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000707 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(False);
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000708
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000709 // If the RHS (False) is 0, we swap the order of the operands
710 // of ISD::SELECT (obviously also inverting the condition) so that we can
711 // take advantage of conditional moves using the $0 register.
712 // Example:
713 // return (a != 0) ? x : 0;
714 // load $reg, x
715 // movz $reg, $0, a
716 if (!FalseC)
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000717 return SDValue();
718
Andrew Trickef9de2a2013-05-25 02:42:55 +0000719 const SDLoc DL(N);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000720
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000721 if (!FalseC->getZExtValue()) {
722 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
723 SDValue True = N->getOperand(1);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000724
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000725 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
726 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
727
728 return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True);
729 }
730
Matheus Almeidaa6beac12013-12-05 12:07:05 +0000731 // If both operands are integer constants there's a possibility that we
732 // can do some interesting optimizations.
733 SDValue True = N->getOperand(1);
734 ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(True);
735
736 if (!TrueC || !True.getValueType().isInteger())
737 return SDValue();
738
Vasileios Kalintiris36901dd2016-03-01 20:25:43 +0000739 // We'll also ignore MVT::i64 operands as this optimizations proves
740 // to be ineffective because of the required sign extensions as the result
741 // of a SETCC operator is always MVT::i32 for non-vector types.
742 if (True.getValueType() == MVT::i64)
743 return SDValue();
744
Matheus Almeidaa6beac12013-12-05 12:07:05 +0000745 int64_t Diff = TrueC->getSExtValue() - FalseC->getSExtValue();
746
747 // 1) (a < x) ? y : y-1
748 // slti $reg1, a, x
749 // addiu $reg2, $reg1, y-1
Vasileios Kalintiris36901dd2016-03-01 20:25:43 +0000750 if (Diff == 1)
751 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, False);
Matheus Almeidaa6beac12013-12-05 12:07:05 +0000752
753 // 2) (a < x) ? y-1 : y
754 // slti $reg1, a, x
755 // xor $reg1, $reg1, 1
756 // addiu $reg2, $reg1, y-1
757 if (Diff == -1) {
758 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
759 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
760 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
761 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, True);
762 }
763
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000764 // Couldn't optimize.
765 return SDValue();
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000766}
767
Vasileios Kalintirise741eb22015-03-02 12:47:32 +0000768static SDValue performCMovFPCombine(SDNode *N, SelectionDAG &DAG,
769 TargetLowering::DAGCombinerInfo &DCI,
770 const MipsSubtarget &Subtarget) {
771 if (DCI.isBeforeLegalizeOps())
772 return SDValue();
773
774 SDValue ValueIfTrue = N->getOperand(0), ValueIfFalse = N->getOperand(2);
775
776 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(ValueIfFalse);
777 if (!FalseC || FalseC->getZExtValue())
778 return SDValue();
779
780 // Since RHS (False) is 0, we swap the order of the True/False operands
781 // (obviously also inverting the condition) so that we can
782 // take advantage of conditional moves using the $0 register.
783 // Example:
784 // return (a != 0) ? x : 0;
785 // load $reg, x
786 // movz $reg, $0, a
787 unsigned Opc = (N->getOpcode() == MipsISD::CMovFP_T) ? MipsISD::CMovFP_F :
788 MipsISD::CMovFP_T;
789
790 SDValue FCC = N->getOperand(1), Glue = N->getOperand(3);
Vasileios Kalintiris2ef28882015-03-04 12:10:18 +0000791 return DAG.getNode(Opc, SDLoc(N), ValueIfFalse.getValueType(),
792 ValueIfFalse, FCC, ValueIfTrue, Glue);
Vasileios Kalintirise741eb22015-03-02 12:47:32 +0000793}
794
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000795static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000796 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000797 const MipsSubtarget &Subtarget) {
Eric Christopher1c29a652014-07-18 22:55:25 +0000798 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasExtractInsert())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000799 return SDValue();
800
Petar Jovanovicb71386a2017-03-15 13:10:08 +0000801 SDValue FirstOperand = N->getOperand(0);
802 unsigned FirstOperandOpc = FirstOperand.getOpcode();
803 SDValue Mask = N->getOperand(1);
804 EVT ValTy = N->getValueType(0);
805 SDLoc DL(N);
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000806
Petar Jovanovicb71386a2017-03-15 13:10:08 +0000807 uint64_t Pos = 0, SMPos, SMSize;
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000808 ConstantSDNode *CN;
Petar Jovanovicb71386a2017-03-15 13:10:08 +0000809 SDValue NewOperand;
810 unsigned Opc;
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000811
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000812 // Op's second operand must be a shifted mask.
813 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000814 !isShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000815 return SDValue();
816
Petar Jovanovicb71386a2017-03-15 13:10:08 +0000817 if (FirstOperandOpc == ISD::SRA || FirstOperandOpc == ISD::SRL) {
818 // Pattern match EXT.
819 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
820 // => ext $dst, $src, pos, size
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000821
Petar Jovanovicb71386a2017-03-15 13:10:08 +0000822 // The second operand of the shift must be an immediate.
823 if (!(CN = dyn_cast<ConstantSDNode>(FirstOperand.getOperand(1))))
824 return SDValue();
825
826 Pos = CN->getZExtValue();
827
828 // Return if the shifted mask does not start at bit 0 or the sum of its size
829 // and Pos exceeds the word's size.
830 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
831 return SDValue();
832
833 Opc = MipsISD::Ext;
834 NewOperand = FirstOperand.getOperand(0);
835 } else if (FirstOperandOpc == ISD::SHL && Subtarget.hasCnMips()) {
836 // Pattern match CINS.
837 // $dst = and (shl $src , pos), mask
838 // => cins $dst, $src, pos, size
839 // mask is a shifted mask with consecutive 1's, pos = shift amount,
840 // size = population count.
841
842 // The second operand of the shift must be an immediate.
843 if (!(CN = dyn_cast<ConstantSDNode>(FirstOperand.getOperand(1))))
844 return SDValue();
845
846 Pos = CN->getZExtValue();
847
848 if (SMPos != Pos || Pos >= ValTy.getSizeInBits() || SMSize >= 32 ||
849 Pos + SMSize > ValTy.getSizeInBits())
850 return SDValue();
851
852 NewOperand = FirstOperand.getOperand(0);
853 // SMSize is 'location' (position) in this case, not size.
854 SMSize--;
855 Opc = MipsISD::CIns;
856 } else {
857 // Pattern match EXT.
858 // $dst = and $src, (2**size - 1) , if size > 16
859 // => ext $dst, $src, pos, size , pos = 0
860
861 // If the mask is <= 0xffff, andi can be used instead.
862 if (CN->getZExtValue() <= 0xffff)
863 return SDValue();
864
865 // Return if the mask doesn't start at position 0.
866 if (SMPos)
867 return SDValue();
868
869 Opc = MipsISD::Ext;
870 NewOperand = FirstOperand;
871 }
872 return DAG.getNode(Opc, DL, ValTy, NewOperand,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000873 DAG.getConstant(Pos, DL, MVT::i32),
874 DAG.getConstant(SMSize, DL, MVT::i32));
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000875}
Jia Liuf54f60f2012-02-28 07:46:26 +0000876
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000877static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000878 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000879 const MipsSubtarget &Subtarget) {
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000880 // Pattern match INS.
881 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
Jia Liuf54f60f2012-02-28 07:46:26 +0000882 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000883 // => ins $dst, $src, size, pos, $src1
Eric Christopher1c29a652014-07-18 22:55:25 +0000884 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasExtractInsert())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000885 return SDValue();
886
887 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
888 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
Strahinja Petrovicab9573f2017-05-22 09:06:44 +0000889 ConstantSDNode *CN, *CN1;
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000890
891 // See if Op's first operand matches (and $src1 , mask0).
892 if (And0.getOpcode() != ISD::AND)
893 return SDValue();
894
895 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000896 !isShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000897 return SDValue();
898
899 // See if Op's second operand matches (and (shl $src, pos), mask1).
Strahinja Petrovicab9573f2017-05-22 09:06:44 +0000900 if (And1.getOpcode() == ISD::AND &&
901 And1.getOperand(0).getOpcode() == ISD::SHL) {
902
903 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
904 !isShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
905 return SDValue();
906
Davide Italianoef9bfe92017-05-26 21:56:19 +0000907 // The shift masks must have the same position and size.
908 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
909 return SDValue();
Strahinja Petrovicab9573f2017-05-22 09:06:44 +0000910
Davide Italianoef9bfe92017-05-26 21:56:19 +0000911 SDValue Shl = And1.getOperand(0);
Strahinja Petrovicab9573f2017-05-22 09:06:44 +0000912
Davide Italianoef9bfe92017-05-26 21:56:19 +0000913 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
914 return SDValue();
Strahinja Petrovicab9573f2017-05-22 09:06:44 +0000915
Davide Italianoef9bfe92017-05-26 21:56:19 +0000916 unsigned Shamt = CN->getZExtValue();
Strahinja Petrovicab9573f2017-05-22 09:06:44 +0000917
Davide Italianoef9bfe92017-05-26 21:56:19 +0000918 // Return if the shift amount and the first bit position of mask are not the
919 // same.
920 EVT ValTy = N->getValueType(0);
921 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
922 return SDValue();
Strahinja Petrovicab9573f2017-05-22 09:06:44 +0000923
Davide Italianoef9bfe92017-05-26 21:56:19 +0000924 SDLoc DL(N);
925 return DAG.getNode(MipsISD::Ins, DL, ValTy, Shl.getOperand(0),
926 DAG.getConstant(SMPos0, DL, MVT::i32),
927 DAG.getConstant(SMSize0, DL, MVT::i32),
928 And0.getOperand(0));
Strahinja Petrovicab9573f2017-05-22 09:06:44 +0000929 } else {
930 // Pattern match DINS.
931 // $dst = or (and $src, mask0), mask1
932 // where mask0 = ((1 << SMSize0) -1) << SMPos0
933 // => dins $dst, $src, pos, size
934 if (~CN->getSExtValue() == ((((int64_t)1 << SMSize0) - 1) << SMPos0) &&
935 ((SMSize0 + SMPos0 <= 64 && Subtarget.hasMips64r2()) ||
936 (SMSize0 + SMPos0 <= 32))) {
937 // Check if AND instruction has constant as argument
938 bool isConstCase = And1.getOpcode() != ISD::AND;
939 if (And1.getOpcode() == ISD::AND) {
940 if (!(CN1 = dyn_cast<ConstantSDNode>(And1->getOperand(1))))
941 return SDValue();
942 } else {
943 if (!(CN1 = dyn_cast<ConstantSDNode>(N->getOperand(1))))
944 return SDValue();
945 }
Strahinja Petrovicd280ea42017-06-21 09:25:51 +0000946 // Don't generate INS if constant OR operand doesn't fit into bits
947 // cleared by constant AND operand.
948 if (CN->getSExtValue() & CN1->getSExtValue())
949 return SDValue();
950
Strahinja Petrovicab9573f2017-05-22 09:06:44 +0000951 SDLoc DL(N);
952 EVT ValTy = N->getOperand(0)->getValueType(0);
953 SDValue Const1;
954 SDValue SrlX;
955 if (!isConstCase) {
956 Const1 = DAG.getConstant(SMPos0, DL, MVT::i32);
957 SrlX = DAG.getNode(ISD::SRL, DL, And1->getValueType(0), And1, Const1);
958 }
959 return DAG.getNode(
960 MipsISD::Ins, DL, N->getValueType(0),
961 isConstCase
962 ? DAG.getConstant(CN1->getSExtValue() >> SMPos0, DL, ValTy)
963 : SrlX,
964 DAG.getConstant(SMPos0, DL, MVT::i32),
965 DAG.getConstant(ValTy.getSizeInBits() / 8 < 8 ? SMSize0 & 31
966 : SMSize0,
967 DL, MVT::i32),
968 And0->getOperand(0));
969
970 }
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000971 return SDValue();
Strahinja Petrovicab9573f2017-05-22 09:06:44 +0000972 }
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000973}
Jia Liuf54f60f2012-02-28 07:46:26 +0000974
Simon Dardis250256f2017-07-13 11:28:05 +0000975static SDValue performMADD_MSUBCombine(SDNode *ROOTNode, SelectionDAG &CurDAG,
976 const MipsSubtarget &Subtarget) {
977 // ROOTNode must have a multiplication as an operand for the match to be
978 // successful.
979 if (ROOTNode->getOperand(0).getOpcode() != ISD::MUL &&
980 ROOTNode->getOperand(1).getOpcode() != ISD::MUL)
981 return SDValue();
982
983 // We don't handle vector types here.
984 if (ROOTNode->getValueType(0).isVector())
985 return SDValue();
986
987 // For MIPS64, madd / msub instructions are inefficent to use with 64 bit
988 // arithmetic. E.g.
989 // (add (mul a b) c) =>
990 // let res = (madd (mthi (drotr c 32))x(mtlo c) a b) in
991 // MIPS64: (or (dsll (mfhi res) 32) (dsrl (dsll (mflo res) 32) 32)
992 // or
993 // MIPS64R2: (dins (mflo res) (mfhi res) 32 32)
994 //
995 // The overhead of setting up the Hi/Lo registers and reassembling the
996 // result makes this a dubious optimzation for MIPS64. The core of the
997 // problem is that Hi/Lo contain the upper and lower 32 bits of the
998 // operand and result.
999 //
1000 // It requires a chain of 4 add/mul for MIPS64R2 to get better code
1001 // density than doing it naively, 5 for MIPS64. Additionally, using
1002 // madd/msub on MIPS64 requires the operands actually be 32 bit sign
1003 // extended operands, not true 64 bit values.
1004 //
1005 // FIXME: For the moment, disable this completely for MIPS64.
1006 if (Subtarget.hasMips64())
1007 return SDValue();
1008
1009 SDValue Mult = ROOTNode->getOperand(0).getOpcode() == ISD::MUL
1010 ? ROOTNode->getOperand(0)
1011 : ROOTNode->getOperand(1);
1012
1013 SDValue AddOperand = ROOTNode->getOperand(0).getOpcode() == ISD::MUL
1014 ? ROOTNode->getOperand(1)
1015 : ROOTNode->getOperand(0);
1016
1017 // Transform this to a MADD only if the user of this node is the add.
1018 // If there are other users of the mul, this function returns here.
1019 if (!Mult.hasOneUse())
1020 return SDValue();
1021
1022 // maddu and madd are unusual instructions in that on MIPS64 bits 63..31
1023 // must be in canonical form, i.e. sign extended. For MIPS32, the operands
1024 // of the multiply must have 32 or more sign bits, otherwise we cannot
1025 // perform this optimization. We have to check this here as we're performing
1026 // this optimization pre-legalization.
1027 SDValue MultLHS = Mult->getOperand(0);
1028 SDValue MultRHS = Mult->getOperand(1);
1029
1030 bool IsSigned = MultLHS->getOpcode() == ISD::SIGN_EXTEND &&
1031 MultRHS->getOpcode() == ISD::SIGN_EXTEND;
1032 bool IsUnsigned = MultLHS->getOpcode() == ISD::ZERO_EXTEND &&
1033 MultRHS->getOpcode() == ISD::ZERO_EXTEND;
1034
1035 if (!IsSigned && !IsUnsigned)
1036 return SDValue();
1037
1038 // Initialize accumulator.
1039 SDLoc DL(ROOTNode);
1040 SDValue TopHalf;
1041 SDValue BottomHalf;
1042 BottomHalf = CurDAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, AddOperand,
1043 CurDAG.getIntPtrConstant(0, DL));
1044
1045 TopHalf = CurDAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, AddOperand,
1046 CurDAG.getIntPtrConstant(1, DL));
1047 SDValue ACCIn = CurDAG.getNode(MipsISD::MTLOHI, DL, MVT::Untyped,
1048 BottomHalf,
1049 TopHalf);
1050
1051 // Create MipsMAdd(u) / MipsMSub(u) node.
1052 bool IsAdd = ROOTNode->getOpcode() == ISD::ADD;
1053 unsigned Opcode = IsAdd ? (IsUnsigned ? MipsISD::MAddu : MipsISD::MAdd)
1054 : (IsUnsigned ? MipsISD::MSubu : MipsISD::MSub);
1055 SDValue MAddOps[3] = {
1056 CurDAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Mult->getOperand(0)),
1057 CurDAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Mult->getOperand(1)), ACCIn};
1058 EVT VTs[2] = {MVT::i32, MVT::i32};
1059 SDValue MAdd = CurDAG.getNode(Opcode, DL, VTs, MAddOps);
1060
1061 SDValue ResLo = CurDAG.getNode(MipsISD::MFLO, DL, MVT::i32, MAdd);
1062 SDValue ResHi = CurDAG.getNode(MipsISD::MFHI, DL, MVT::i32, MAdd);
1063 SDValue Combined =
1064 CurDAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, ResLo, ResHi);
1065 return Combined;
1066}
1067
1068static SDValue performSUBCombine(SDNode *N, SelectionDAG &DAG,
1069 TargetLowering::DAGCombinerInfo &DCI,
1070 const MipsSubtarget &Subtarget) {
1071 // (sub v0 (mul v1, v2)) => (msub v1, v2, v0)
1072 if (DCI.isBeforeLegalizeOps()) {
1073 if (Subtarget.hasMips32() && !Subtarget.hasMips32r6() &&
1074 !Subtarget.inMips16Mode() && N->getValueType(0) == MVT::i64)
1075 return performMADD_MSUBCombine(N, DAG, Subtarget);
1076
1077 return SDValue();
1078 }
1079
1080 return SDValue();
1081}
1082
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001083static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanakadf5205e2012-06-13 20:33:18 +00001084 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +00001085 const MipsSubtarget &Subtarget) {
Simon Dardis250256f2017-07-13 11:28:05 +00001086 // (add v0 (mul v1, v2)) => (madd v1, v2, v0)
1087 if (DCI.isBeforeLegalizeOps()) {
1088 if (Subtarget.hasMips32() && !Subtarget.hasMips32r6() &&
1089 !Subtarget.inMips16Mode() && N->getValueType(0) == MVT::i64)
1090 return performMADD_MSUBCombine(N, DAG, Subtarget);
Simon Dardisdede76f2017-06-29 20:59:47 +00001091
Simon Dardisdede76f2017-06-29 20:59:47 +00001092 return SDValue();
Simon Dardis250256f2017-07-13 11:28:05 +00001093 }
Simon Dardisdede76f2017-06-29 20:59:47 +00001094
Simon Dardis250256f2017-07-13 11:28:05 +00001095 // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt))
Akira Hatanakadf5205e2012-06-13 20:33:18 +00001096 SDValue Add = N->getOperand(1);
1097
1098 if (Add.getOpcode() != ISD::ADD)
1099 return SDValue();
1100
1101 SDValue Lo = Add.getOperand(1);
1102
1103 if ((Lo.getOpcode() != MipsISD::Lo) ||
1104 (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable))
1105 return SDValue();
1106
1107 EVT ValTy = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001108 SDLoc DL(N);
Akira Hatanakadf5205e2012-06-13 20:33:18 +00001109
1110 SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, N->getOperand(0),
1111 Add.getOperand(0));
1112 return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo);
1113}
1114
Vasileios Kalintiris3751d412016-04-13 15:07:45 +00001115static SDValue performAssertZextCombine(SDNode *N, SelectionDAG &DAG,
1116 TargetLowering::DAGCombinerInfo &DCI,
1117 const MipsSubtarget &Subtarget) {
1118 SDValue N0 = N->getOperand(0);
1119 EVT NarrowerVT = cast<VTSDNode>(N->getOperand(1))->getVT();
1120
1121 if (N0.getOpcode() != ISD::TRUNCATE)
1122 return SDValue();
1123
1124 if (N0.getOperand(0).getOpcode() != ISD::AssertZext)
1125 return SDValue();
1126
1127 // fold (AssertZext (trunc (AssertZext x))) -> (trunc (AssertZext x))
1128 // if the type of the extension of the innermost AssertZext node is
1129 // smaller from that of the outermost node, eg:
1130 // (AssertZext:i32 (trunc:i32 (AssertZext:i64 X, i32)), i8)
1131 // -> (trunc:i32 (AssertZext X, i8))
1132 SDValue WiderAssertZext = N0.getOperand(0);
1133 EVT WiderVT = cast<VTSDNode>(WiderAssertZext->getOperand(1))->getVT();
1134
1135 if (NarrowerVT.bitsLT(WiderVT)) {
1136 SDValue NewAssertZext = DAG.getNode(
1137 ISD::AssertZext, SDLoc(N), WiderAssertZext.getValueType(),
1138 WiderAssertZext.getOperand(0), DAG.getValueType(NarrowerVT));
1139 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0),
1140 NewAssertZext);
1141 }
1142
1143 return SDValue();
1144}
1145
Petar Jovanovicb71386a2017-03-15 13:10:08 +00001146static SDValue performSHLCombine(SDNode *N, SelectionDAG &DAG,
1147 TargetLowering::DAGCombinerInfo &DCI,
1148 const MipsSubtarget &Subtarget) {
1149 // Pattern match CINS.
1150 // $dst = shl (and $src , imm), pos
1151 // => cins $dst, $src, pos, size
1152
1153 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasCnMips())
1154 return SDValue();
1155
1156 SDValue FirstOperand = N->getOperand(0);
1157 unsigned FirstOperandOpc = FirstOperand.getOpcode();
1158 SDValue SecondOperand = N->getOperand(1);
1159 EVT ValTy = N->getValueType(0);
1160 SDLoc DL(N);
1161
1162 uint64_t Pos = 0, SMPos, SMSize;
1163 ConstantSDNode *CN;
1164 SDValue NewOperand;
1165
1166 // The second operand of the shift must be an immediate.
1167 if (!(CN = dyn_cast<ConstantSDNode>(SecondOperand)))
1168 return SDValue();
1169
1170 Pos = CN->getZExtValue();
1171
1172 if (Pos >= ValTy.getSizeInBits())
1173 return SDValue();
1174
1175 if (FirstOperandOpc != ISD::AND)
1176 return SDValue();
1177
1178 // AND's second operand must be a shifted mask.
1179 if (!(CN = dyn_cast<ConstantSDNode>(FirstOperand.getOperand(1))) ||
1180 !isShiftedMask(CN->getZExtValue(), SMPos, SMSize))
1181 return SDValue();
1182
1183 // Return if the shifted mask does not start at bit 0 or the sum of its size
1184 // and Pos exceeds the word's size.
1185 if (SMPos != 0 || SMSize > 32 || Pos + SMSize > ValTy.getSizeInBits())
1186 return SDValue();
1187
1188 NewOperand = FirstOperand.getOperand(0);
1189 // SMSize is 'location' (position) in this case, not size.
1190 SMSize--;
1191
1192 return DAG.getNode(MipsISD::CIns, DL, ValTy, NewOperand,
1193 DAG.getConstant(Pos, DL, MVT::i32),
1194 DAG.getConstant(SMSize, DL, MVT::i32));
1195}
1196
Bruno Cardoso Lopes61a61e92011-02-10 18:05:10 +00001197SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +00001198 const {
1199 SelectionDAG &DAG = DCI.DAG;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001200 unsigned Opc = N->getOpcode();
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +00001201
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001202 switch (Opc) {
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +00001203 default: break;
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +00001204 case ISD::SDIVREM:
1205 case ISD::UDIVREM:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001206 return performDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka7dd7c082012-03-08 02:14:24 +00001207 case ISD::SELECT:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001208 return performSELECTCombine(N, DAG, DCI, Subtarget);
Vasileios Kalintirise741eb22015-03-02 12:47:32 +00001209 case MipsISD::CMovFP_F:
1210 case MipsISD::CMovFP_T:
1211 return performCMovFPCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka184b63d2011-08-17 17:45:08 +00001212 case ISD::AND:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001213 return performANDCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka184b63d2011-08-17 17:45:08 +00001214 case ISD::OR:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001215 return performORCombine(N, DAG, DCI, Subtarget);
Akira Hatanakadf5205e2012-06-13 20:33:18 +00001216 case ISD::ADD:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001217 return performADDCombine(N, DAG, DCI, Subtarget);
Vasileios Kalintiris3751d412016-04-13 15:07:45 +00001218 case ISD::AssertZext:
1219 return performAssertZextCombine(N, DAG, DCI, Subtarget);
Petar Jovanovicb71386a2017-03-15 13:10:08 +00001220 case ISD::SHL:
1221 return performSHLCombine(N, DAG, DCI, Subtarget);
Simon Dardis250256f2017-07-13 11:28:05 +00001222 case ISD::SUB:
1223 return performSUBCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +00001224 }
1225
1226 return SDValue();
1227}
1228
Sanjay Patelf7401292015-11-11 17:24:56 +00001229bool MipsTargetLowering::isCheapToSpeculateCttz() const {
1230 return Subtarget.hasMips32();
1231}
1232
1233bool MipsTargetLowering::isCheapToSpeculateCtlz() const {
1234 return Subtarget.hasMips32();
1235}
1236
Akira Hatanakafabb8cf2012-09-21 23:58:31 +00001237void
1238MipsTargetLowering::LowerOperationWrapper(SDNode *N,
1239 SmallVectorImpl<SDValue> &Results,
1240 SelectionDAG &DAG) const {
1241 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
1242
1243 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
1244 Results.push_back(Res.getValue(I));
1245}
1246
1247void
1248MipsTargetLowering::ReplaceNodeResults(SDNode *N,
1249 SmallVectorImpl<SDValue> &Results,
1250 SelectionDAG &DAG) const {
Akira Hatanaka9da442f2013-04-30 21:17:07 +00001251 return LowerOperationWrapper(N, Results, DAG);
Akira Hatanakafabb8cf2012-09-21 23:58:31 +00001252}
1253
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001254SDValue MipsTargetLowering::
Dan Gohman21cea8a2010-04-17 15:26:15 +00001255LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001256{
Wesley Peck527da1b2010-11-23 03:31:01 +00001257 switch (Op.getOpcode())
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001258 {
Simon Dardisba92b032016-09-09 11:06:01 +00001259 case ISD::BRCOND: return lowerBRCOND(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +00001260 case ISD::ConstantPool: return lowerConstantPool(Op, DAG);
1261 case ISD::GlobalAddress: return lowerGlobalAddress(Op, DAG);
1262 case ISD::BlockAddress: return lowerBlockAddress(Op, DAG);
1263 case ISD::GlobalTLSAddress: return lowerGlobalTLSAddress(Op, DAG);
1264 case ISD::JumpTable: return lowerJumpTable(Op, DAG);
Simon Dardisba92b032016-09-09 11:06:01 +00001265 case ISD::SELECT: return lowerSELECT(Op, DAG);
1266 case ISD::SETCC: return lowerSETCC(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +00001267 case ISD::VASTART: return lowerVASTART(Op, DAG);
Daniel Sanders2b553d42014-08-01 09:17:39 +00001268 case ISD::VAARG: return lowerVAARG(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +00001269 case ISD::FCOPYSIGN: return lowerFCOPYSIGN(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +00001270 case ISD::FRAMEADDR: return lowerFRAMEADDR(Op, DAG);
1271 case ISD::RETURNADDR: return lowerRETURNADDR(Op, DAG);
1272 case ISD::EH_RETURN: return lowerEH_RETURN(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +00001273 case ISD::ATOMIC_FENCE: return lowerATOMIC_FENCE(Op, DAG);
1274 case ISD::SHL_PARTS: return lowerShiftLeftParts(Op, DAG);
1275 case ISD::SRA_PARTS: return lowerShiftRightParts(Op, DAG, true);
1276 case ISD::SRL_PARTS: return lowerShiftRightParts(Op, DAG, false);
1277 case ISD::LOAD: return lowerLOAD(Op, DAG);
1278 case ISD::STORE: return lowerSTORE(Op, DAG);
Hal Finkel5081ac22016-09-01 10:28:47 +00001279 case ISD::EH_DWARF_CFA: return lowerEH_DWARF_CFA(Op, DAG);
Akira Hatanaka252f54f2013-05-16 21:17:15 +00001280 case ISD::FP_TO_SINT: return lowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001281 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001282 return SDValue();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001283}
1284
Akira Hatanakae2489122011-04-15 21:51:11 +00001285//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001286// Lower helper functions
Akira Hatanakae2489122011-04-15 21:51:11 +00001287//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001288
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001289// addLiveIn - This helper function adds the specified physical register to the
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001290// MachineFunction as a live in value. It also creates a corresponding
1291// virtual register for it.
1292static unsigned
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001293addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001294{
Chris Lattnera10fff52007-12-31 04:13:23 +00001295 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
1296 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001297 return VReg;
1298}
1299
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001300static MachineBasicBlock *insertDivByZeroTrap(MachineInstr &MI,
Daniel Sanders308181e2014-06-12 10:44:10 +00001301 MachineBasicBlock &MBB,
1302 const TargetInstrInfo &TII,
Zlatko Buljan58d6a952016-04-13 08:02:26 +00001303 bool Is64Bit, bool IsMicroMips) {
Akira Hatanaka1cb02422013-05-20 18:07:43 +00001304 if (NoZeroDivCheck)
1305 return &MBB;
1306
1307 // Insert instruction "teq $divisor_reg, $zero, 7".
1308 MachineBasicBlock::iterator I(MI);
1309 MachineInstrBuilder MIB;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001310 MachineOperand &Divisor = MI.getOperand(2);
1311 MIB = BuildMI(MBB, std::next(I), MI.getDebugLoc(),
Zlatko Buljan58d6a952016-04-13 08:02:26 +00001312 TII.get(IsMicroMips ? Mips::TEQ_MM : Mips::TEQ))
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001313 .addReg(Divisor.getReg(), getKillRegState(Divisor.isKill()))
1314 .addReg(Mips::ZERO)
1315 .addImm(7);
Akira Hatanaka1cb02422013-05-20 18:07:43 +00001316
1317 // Use the 32-bit sub-register if this is a 64-bit division.
1318 if (Is64Bit)
1319 MIB->getOperand(0).setSubReg(Mips::sub_32);
1320
Akira Hatanaka86c3c792013-10-15 01:06:30 +00001321 // Clear Divisor's kill flag.
1322 Divisor.setIsKill(false);
Daniel Sanders308181e2014-06-12 10:44:10 +00001323
1324 // We would normally delete the original instruction here but in this case
1325 // we only needed to inject an additional instruction rather than replace it.
1326
Akira Hatanaka1cb02422013-05-20 18:07:43 +00001327 return &MBB;
1328}
1329
Akira Hatanakae4bd0542012-09-27 02:15:57 +00001330MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001331MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
Dan Gohman25c16532010-05-01 00:01:06 +00001332 MachineBasicBlock *BB) const {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001333 switch (MI.getOpcode()) {
Reed Kotler97ba5f22013-02-21 04:22:38 +00001334 default:
1335 llvm_unreachable("Unexpected instr type to insert");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001336 case Mips::ATOMIC_LOAD_ADD_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001337 return emitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001338 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001339 return emitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001340 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001341 return emitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001342 case Mips::ATOMIC_LOAD_ADD_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001343 return emitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001344
1345 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001346 return emitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001347 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001348 return emitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001349 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001350 return emitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001351 case Mips::ATOMIC_LOAD_AND_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001352 return emitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001353
1354 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001355 return emitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001356 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001357 return emitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001358 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001359 return emitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001360 case Mips::ATOMIC_LOAD_OR_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001361 return emitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001362
1363 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001364 return emitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001365 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001366 return emitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001367 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001368 return emitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001369 case Mips::ATOMIC_LOAD_XOR_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001370 return emitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001371
1372 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001373 return emitAtomicBinaryPartword(MI, BB, 1, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001374 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001375 return emitAtomicBinaryPartword(MI, BB, 2, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001376 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001377 return emitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001378 case Mips::ATOMIC_LOAD_NAND_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001379 return emitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001380
1381 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001382 return emitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001383 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001384 return emitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001385 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001386 return emitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001387 case Mips::ATOMIC_LOAD_SUB_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001388 return emitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001389
1390 case Mips::ATOMIC_SWAP_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001391 return emitAtomicBinaryPartword(MI, BB, 1, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001392 case Mips::ATOMIC_SWAP_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001393 return emitAtomicBinaryPartword(MI, BB, 2, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001394 case Mips::ATOMIC_SWAP_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001395 return emitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001396 case Mips::ATOMIC_SWAP_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001397 return emitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001398
Simon Dardis7577ce22017-03-09 14:03:26 +00001399 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001400 return emitAtomicCmpSwapPartword(MI, BB, 1);
Simon Dardis7577ce22017-03-09 14:03:26 +00001401 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001402 return emitAtomicCmpSwapPartword(MI, BB, 2);
Simon Dardis7577ce22017-03-09 14:03:26 +00001403 case Mips::ATOMIC_CMP_SWAP_I32:
1404 return emitAtomicCmpSwap(MI, BB, 4);
1405 case Mips::ATOMIC_CMP_SWAP_I64:
1406 return emitAtomicCmpSwap(MI, BB, 8);
Akira Hatanaka1cb02422013-05-20 18:07:43 +00001407 case Mips::PseudoSDIV:
1408 case Mips::PseudoUDIV:
Daniel Sanders308181e2014-06-12 10:44:10 +00001409 case Mips::DIV:
1410 case Mips::DIVU:
1411 case Mips::MOD:
1412 case Mips::MODU:
Zlatko Buljan58d6a952016-04-13 08:02:26 +00001413 return insertDivByZeroTrap(MI, *BB, *Subtarget.getInstrInfo(), false,
1414 false);
1415 case Mips::SDIV_MM_Pseudo:
1416 case Mips::UDIV_MM_Pseudo:
1417 case Mips::SDIV_MM:
1418 case Mips::UDIV_MM:
1419 case Mips::DIV_MMR6:
1420 case Mips::DIVU_MMR6:
1421 case Mips::MOD_MMR6:
1422 case Mips::MODU_MMR6:
1423 return insertDivByZeroTrap(MI, *BB, *Subtarget.getInstrInfo(), false, true);
Akira Hatanaka1cb02422013-05-20 18:07:43 +00001424 case Mips::PseudoDSDIV:
1425 case Mips::PseudoDUDIV:
Daniel Sanders308181e2014-06-12 10:44:10 +00001426 case Mips::DDIV:
1427 case Mips::DDIVU:
1428 case Mips::DMOD:
1429 case Mips::DMODU:
Zlatko Buljan58d6a952016-04-13 08:02:26 +00001430 return insertDivByZeroTrap(MI, *BB, *Subtarget.getInstrInfo(), true, false);
1431 case Mips::DDIV_MM64R6:
1432 case Mips::DDIVU_MM64R6:
1433 case Mips::DMOD_MM64R6:
1434 case Mips::DMODU_MM64R6:
1435 return insertDivByZeroTrap(MI, *BB, *Subtarget.getInstrInfo(), true, true);
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001436
1437 case Mips::PseudoSELECT_I:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001438 case Mips::PseudoSELECT_I64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001439 case Mips::PseudoSELECT_S:
1440 case Mips::PseudoSELECT_D32:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001441 case Mips::PseudoSELECT_D64:
Vasileios Kalintiris36901dd2016-03-01 20:25:43 +00001442 return emitPseudoSELECT(MI, BB, false, Mips::BNE);
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001443 case Mips::PseudoSELECTFP_F_I:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001444 case Mips::PseudoSELECTFP_F_I64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001445 case Mips::PseudoSELECTFP_F_S:
1446 case Mips::PseudoSELECTFP_F_D32:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001447 case Mips::PseudoSELECTFP_F_D64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001448 return emitPseudoSELECT(MI, BB, true, Mips::BC1F);
1449 case Mips::PseudoSELECTFP_T_I:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001450 case Mips::PseudoSELECTFP_T_I64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001451 case Mips::PseudoSELECTFP_T_S:
1452 case Mips::PseudoSELECTFP_T_D32:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001453 case Mips::PseudoSELECTFP_T_D64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001454 return emitPseudoSELECT(MI, BB, true, Mips::BC1T);
Akira Hatanakaa5352702011-03-31 18:26:17 +00001455 }
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001456}
1457
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001458// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
1459// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001460MachineBasicBlock *MipsTargetLowering::emitAtomicBinary(MachineInstr &MI,
1461 MachineBasicBlock *BB,
1462 unsigned Size,
1463 unsigned BinOpcode,
1464 bool Nand) const {
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001465 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001466
1467 MachineFunction *MF = BB->getParent();
1468 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001469 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Eric Christopher96e72c62015-01-29 23:27:36 +00001470 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001471 const bool ArePtrs64bit = ABI.ArePtrs64bit();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001472 DebugLoc DL = MI.getDebugLoc();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001473 unsigned LL, SC, AND, NOR, ZERO, BEQ;
1474
1475 if (Size == 4) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001476 if (isMicroMips) {
1477 LL = Mips::LL_MM;
1478 SC = Mips::SC_MM;
1479 } else {
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001480 LL = Subtarget.hasMips32r6()
1481 ? (ArePtrs64bit ? Mips::LL64_R6 : Mips::LL_R6)
1482 : (ArePtrs64bit ? Mips::LL64 : Mips::LL);
1483 SC = Subtarget.hasMips32r6()
1484 ? (ArePtrs64bit ? Mips::SC64_R6 : Mips::SC_R6)
1485 : (ArePtrs64bit ? Mips::SC64 : Mips::SC);
Daniel Sanders6a803f62014-06-16 13:13:03 +00001486 }
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001487
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001488 AND = Mips::AND;
1489 NOR = Mips::NOR;
1490 ZERO = Mips::ZERO;
1491 BEQ = Mips::BEQ;
Daniel Sanders6a803f62014-06-16 13:13:03 +00001492 } else {
Daniel Sandersbdcfab12014-07-24 09:47:14 +00001493 LL = Subtarget.hasMips64r6() ? Mips::LLD_R6 : Mips::LLD;
1494 SC = Subtarget.hasMips64r6() ? Mips::SCD_R6 : Mips::SCD;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001495 AND = Mips::AND64;
1496 NOR = Mips::NOR64;
1497 ZERO = Mips::ZERO_64;
1498 BEQ = Mips::BEQ64;
1499 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001500
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001501 unsigned OldVal = MI.getOperand(0).getReg();
1502 unsigned Ptr = MI.getOperand(1).getReg();
1503 unsigned Incr = MI.getOperand(2).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001504
Akira Hatanaka0e019592011-07-19 20:11:17 +00001505 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1506 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1507 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001508
1509 // insert new blocks after the current block
1510 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1511 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1512 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +00001513 MachineFunction::iterator It = ++BB->getIterator();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001514 MF->insert(It, loopMBB);
1515 MF->insert(It, exitMBB);
1516
1517 // Transfer the remainder of BB and its successor edges to exitMBB.
1518 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001519 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001520 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1521
1522 // thisMBB:
1523 // ...
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001524 // fallthrough --> loopMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001525 BB->addSuccessor(loopMBB);
Akira Hatanaka08636b42011-07-19 17:09:53 +00001526 loopMBB->addSuccessor(loopMBB);
1527 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001528
1529 // loopMBB:
1530 // ll oldval, 0(ptr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001531 // <binop> storeval, oldval, incr
1532 // sc success, storeval, 0(ptr)
1533 // beq success, $0, loopMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001534 BB = loopMBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001535 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001536 if (Nand) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001537 // and andres, oldval, incr
1538 // nor storeval, $0, andres
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001539 BuildMI(BB, DL, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
1540 BuildMI(BB, DL, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001541 } else if (BinOpcode) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001542 // <binop> storeval, oldval, incr
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001543 BuildMI(BB, DL, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001544 } else {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001545 StoreVal = Incr;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001546 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001547 BuildMI(BB, DL, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
1548 BuildMI(BB, DL, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001549
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001550 MI.eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001551
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001552 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001553}
1554
Daniel Sanders6a803f62014-06-16 13:13:03 +00001555MachineBasicBlock *MipsTargetLowering::emitSignExtendToI32InReg(
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001556 MachineInstr &MI, MachineBasicBlock *BB, unsigned Size, unsigned DstReg,
Daniel Sanders6a803f62014-06-16 13:13:03 +00001557 unsigned SrcReg) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00001558 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001559 const DebugLoc &DL = MI.getDebugLoc();
Daniel Sanders6a803f62014-06-16 13:13:03 +00001560
Eric Christopher1c29a652014-07-18 22:55:25 +00001561 if (Subtarget.hasMips32r2() && Size == 1) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001562 BuildMI(BB, DL, TII->get(Mips::SEB), DstReg).addReg(SrcReg);
1563 return BB;
1564 }
1565
Eric Christopher1c29a652014-07-18 22:55:25 +00001566 if (Subtarget.hasMips32r2() && Size == 2) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001567 BuildMI(BB, DL, TII->get(Mips::SEH), DstReg).addReg(SrcReg);
1568 return BB;
1569 }
1570
1571 MachineFunction *MF = BB->getParent();
1572 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1573 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1574 unsigned ScrReg = RegInfo.createVirtualRegister(RC);
1575
1576 assert(Size < 32);
1577 int64_t ShiftImm = 32 - (Size * 8);
1578
1579 BuildMI(BB, DL, TII->get(Mips::SLL), ScrReg).addReg(SrcReg).addImm(ShiftImm);
1580 BuildMI(BB, DL, TII->get(Mips::SRA), DstReg).addReg(ScrReg).addImm(ShiftImm);
1581
1582 return BB;
1583}
1584
1585MachineBasicBlock *MipsTargetLowering::emitAtomicBinaryPartword(
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001586 MachineInstr &MI, MachineBasicBlock *BB, unsigned Size, unsigned BinOpcode,
Daniel Sanders6a803f62014-06-16 13:13:03 +00001587 bool Nand) const {
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001588 assert((Size == 1 || Size == 2) &&
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001589 "Unsupported size for EmitAtomicBinaryPartial.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001590
1591 MachineFunction *MF = BB->getParent();
1592 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1593 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001594 const bool ArePtrs64bit = ABI.ArePtrs64bit();
Simon Dardisa2d8cc32016-04-28 16:26:43 +00001595 const TargetRegisterClass *RCp =
1596 getRegClassFor(ArePtrs64bit ? MVT::i64 : MVT::i32);
Eric Christopher96e72c62015-01-29 23:27:36 +00001597 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001598 DebugLoc DL = MI.getDebugLoc();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001599
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001600 unsigned Dest = MI.getOperand(0).getReg();
1601 unsigned Ptr = MI.getOperand(1).getReg();
1602 unsigned Incr = MI.getOperand(2).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001603
Simon Dardisa2d8cc32016-04-28 16:26:43 +00001604 unsigned AlignedAddr = RegInfo.createVirtualRegister(RCp);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001605 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001606 unsigned Mask = RegInfo.createVirtualRegister(RC);
1607 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001608 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1609 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001610 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Simon Dardisa2d8cc32016-04-28 16:26:43 +00001611 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RCp);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001612 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1613 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1614 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1615 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka9663dd32011-07-19 20:56:53 +00001616 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001617 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1618 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1619 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001620 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001621
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001622 unsigned LL, SC;
1623 if (isMicroMips) {
1624 LL = Mips::LL_MM;
1625 SC = Mips::SC_MM;
1626 } else {
1627 LL = Subtarget.hasMips32r6() ? (ArePtrs64bit ? Mips::LL64_R6 : Mips::LL_R6)
1628 : (ArePtrs64bit ? Mips::LL64 : Mips::LL);
1629 SC = Subtarget.hasMips32r6() ? (ArePtrs64bit ? Mips::SC64_R6 : Mips::SC_R6)
1630 : (ArePtrs64bit ? Mips::SC64 : Mips::SC);
1631 }
1632
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001633 // insert new blocks after the current block
1634 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1635 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001636 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001637 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +00001638 MachineFunction::iterator It = ++BB->getIterator();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001639 MF->insert(It, loopMBB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001640 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001641 MF->insert(It, exitMBB);
1642
1643 // Transfer the remainder of BB and its successor edges to exitMBB.
1644 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001645 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001646 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1647
Akira Hatanaka08636b42011-07-19 17:09:53 +00001648 BB->addSuccessor(loopMBB);
1649 loopMBB->addSuccessor(loopMBB);
1650 loopMBB->addSuccessor(sinkMBB);
1651 sinkMBB->addSuccessor(exitMBB);
1652
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001653 // thisMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001654 // addiu masklsb2,$0,-4 # 0xfffffffc
1655 // and alignedaddr,ptr,masklsb2
1656 // andi ptrlsb2,ptr,3
1657 // sll shiftamt,ptrlsb2,3
1658 // ori maskupper,$0,255 # 0xff
1659 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001660 // nor mask2,$0,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001661 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001662
1663 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Simon Dardisa2d8cc32016-04-28 16:26:43 +00001664 BuildMI(BB, DL, TII->get(ABI.GetPtrAddiuOp()), MaskLSB2)
1665 .addReg(ABI.GetNullPtr()).addImm(-4);
1666 BuildMI(BB, DL, TII->get(ABI.GetPtrAndOp()), AlignedAddr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001667 .addReg(Ptr).addReg(MaskLSB2);
Simon Dardisa2d8cc32016-04-28 16:26:43 +00001668 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2)
1669 .addReg(Ptr, 0, ArePtrs64bit ? Mips::sub_32 : 0).addImm(3);
Eric Christopher1c29a652014-07-18 22:55:25 +00001670 if (Subtarget.isLittle()) {
Akira Hatanaka2bf97332013-05-31 03:25:44 +00001671 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1672 } else {
1673 unsigned Off = RegInfo.createVirtualRegister(RC);
1674 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1675 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1676 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1677 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001678 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001679 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001680 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001681 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001682 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001683 BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(Incr).addReg(ShiftAmt);
Bruno Cardoso Lopesf771a0f2011-05-31 20:25:26 +00001684
Akira Hatanaka27292632011-07-18 18:52:12 +00001685 // atomic.load.binop
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001686 // loopMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001687 // ll oldval,0(alignedaddr)
1688 // binop binopres,oldval,incr2
1689 // and newval,binopres,mask
1690 // and maskedoldval0,oldval,mask2
1691 // or storeval,maskedoldval0,newval
1692 // sc success,storeval,0(alignedaddr)
1693 // beq success,$0,loopMBB
1694
Akira Hatanaka27292632011-07-18 18:52:12 +00001695 // atomic.swap
1696 // loopMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001697 // ll oldval,0(alignedaddr)
Akira Hatanakae4503582011-07-19 18:14:26 +00001698 // and newval,incr2,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001699 // and maskedoldval0,oldval,mask2
1700 // or storeval,maskedoldval0,newval
1701 // sc success,storeval,0(alignedaddr)
1702 // beq success,$0,loopMBB
Akira Hatanaka27292632011-07-18 18:52:12 +00001703
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001704 BB = loopMBB;
Jozef Kolek2f27d572014-12-18 16:39:29 +00001705 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001706 if (Nand) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001707 // and andres, oldval, incr2
1708 // nor binopres, $0, andres
1709 // and newval, binopres, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001710 BuildMI(BB, DL, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1711 BuildMI(BB, DL, TII->get(Mips::NOR), BinOpRes)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001712 .addReg(Mips::ZERO).addReg(AndRes);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001713 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001714 } else if (BinOpcode) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001715 // <binop> binopres, oldval, incr2
1716 // and newval, binopres, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001717 BuildMI(BB, DL, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1718 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001719 } else { // atomic.swap
Akira Hatanaka0e019592011-07-19 20:11:17 +00001720 // and newval, incr2, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001721 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanakae4503582011-07-19 18:14:26 +00001722 }
Jia Liuf54f60f2012-02-28 07:46:26 +00001723
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001724 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001725 .addReg(OldVal).addReg(Mask2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001726 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka9663dd32011-07-19 20:56:53 +00001727 .addReg(MaskedOldVal0).addReg(NewVal);
Jozef Kolek2f27d572014-12-18 16:39:29 +00001728 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001729 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001730 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001731 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001732
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001733 // sinkMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001734 // and maskedoldval1,oldval,mask
1735 // srl srlres,maskedoldval1,shiftamt
Daniel Sanders6a803f62014-06-16 13:13:03 +00001736 // sign_extend dest,srlres
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001737 BB = sinkMBB;
Akira Hatanakae97bd812011-07-19 03:14:58 +00001738
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001739 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001740 .addReg(OldVal).addReg(Mask);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001741 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001742 .addReg(MaskedOldVal1).addReg(ShiftAmt);
Daniel Sanders6a803f62014-06-16 13:13:03 +00001743 BB = emitSignExtendToI32InReg(MI, BB, Size, Dest, SrlRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001744
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001745 MI.eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001746
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001747 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001748}
1749
Simon Dardis7577ce22017-03-09 14:03:26 +00001750MachineBasicBlock *MipsTargetLowering::emitAtomicCmpSwap(MachineInstr &MI,
1751 MachineBasicBlock *BB,
1752 unsigned Size) const {
1753 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
1754
1755 MachineFunction *MF = BB->getParent();
1756 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1757 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
1758 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
1759 const bool ArePtrs64bit = ABI.ArePtrs64bit();
1760 DebugLoc DL = MI.getDebugLoc();
1761 unsigned LL, SC, ZERO, BNE, BEQ;
1762
1763 if (Size == 4) {
1764 if (isMicroMips) {
1765 LL = Mips::LL_MM;
1766 SC = Mips::SC_MM;
1767 } else {
1768 LL = Subtarget.hasMips32r6()
1769 ? (ArePtrs64bit ? Mips::LL64_R6 : Mips::LL_R6)
1770 : (ArePtrs64bit ? Mips::LL64 : Mips::LL);
1771 SC = Subtarget.hasMips32r6()
1772 ? (ArePtrs64bit ? Mips::SC64_R6 : Mips::SC_R6)
1773 : (ArePtrs64bit ? Mips::SC64 : Mips::SC);
1774 }
1775
1776 ZERO = Mips::ZERO;
1777 BNE = Mips::BNE;
1778 BEQ = Mips::BEQ;
1779 } else {
1780 LL = Subtarget.hasMips64r6() ? Mips::LLD_R6 : Mips::LLD;
1781 SC = Subtarget.hasMips64r6() ? Mips::SCD_R6 : Mips::SCD;
1782 ZERO = Mips::ZERO_64;
1783 BNE = Mips::BNE64;
1784 BEQ = Mips::BEQ64;
1785 }
1786
1787 unsigned Dest = MI.getOperand(0).getReg();
1788 unsigned Ptr = MI.getOperand(1).getReg();
1789 unsigned OldVal = MI.getOperand(2).getReg();
1790 unsigned NewVal = MI.getOperand(3).getReg();
1791
1792 unsigned Success = RegInfo.createVirtualRegister(RC);
1793
1794 // insert new blocks after the current block
1795 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1796 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1797 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1798 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1799 MachineFunction::iterator It = ++BB->getIterator();
1800 MF->insert(It, loop1MBB);
1801 MF->insert(It, loop2MBB);
1802 MF->insert(It, exitMBB);
1803
1804 // Transfer the remainder of BB and its successor edges to exitMBB.
1805 exitMBB->splice(exitMBB->begin(), BB,
1806 std::next(MachineBasicBlock::iterator(MI)), BB->end());
1807 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1808
1809 // thisMBB:
1810 // ...
1811 // fallthrough --> loop1MBB
1812 BB->addSuccessor(loop1MBB);
1813 loop1MBB->addSuccessor(exitMBB);
1814 loop1MBB->addSuccessor(loop2MBB);
1815 loop2MBB->addSuccessor(loop1MBB);
1816 loop2MBB->addSuccessor(exitMBB);
1817
1818 // loop1MBB:
1819 // ll dest, 0(ptr)
1820 // bne dest, oldval, exitMBB
1821 BB = loop1MBB;
1822 BuildMI(BB, DL, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1823 BuildMI(BB, DL, TII->get(BNE))
1824 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
1825
1826 // loop2MBB:
1827 // sc success, newval, 0(ptr)
1828 // beq success, $0, loop1MBB
1829 BB = loop2MBB;
1830 BuildMI(BB, DL, TII->get(SC), Success)
1831 .addReg(NewVal).addReg(Ptr).addImm(0);
1832 BuildMI(BB, DL, TII->get(BEQ))
1833 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
1834
1835 MI.eraseFromParent(); // The instruction is gone now.
1836
1837 return exitMBB;
1838}
1839
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001840MachineBasicBlock *MipsTargetLowering::emitAtomicCmpSwapPartword(
1841 MachineInstr &MI, MachineBasicBlock *BB, unsigned Size) const {
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001842 assert((Size == 1 || Size == 2) &&
1843 "Unsupported size for EmitAtomicCmpSwapPartial.");
1844
1845 MachineFunction *MF = BB->getParent();
1846 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1847 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001848 const bool ArePtrs64bit = ABI.ArePtrs64bit();
Zoran Jovanovic2f6845b2016-04-13 16:02:25 +00001849 const TargetRegisterClass *RCp =
1850 getRegClassFor(ArePtrs64bit ? MVT::i64 : MVT::i32);
Eric Christopher96e72c62015-01-29 23:27:36 +00001851 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001852 DebugLoc DL = MI.getDebugLoc();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001853
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001854 unsigned Dest = MI.getOperand(0).getReg();
1855 unsigned Ptr = MI.getOperand(1).getReg();
1856 unsigned CmpVal = MI.getOperand(2).getReg();
1857 unsigned NewVal = MI.getOperand(3).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001858
Zoran Jovanovic2f6845b2016-04-13 16:02:25 +00001859 unsigned AlignedAddr = RegInfo.createVirtualRegister(RCp);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001860 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001861 unsigned Mask = RegInfo.createVirtualRegister(RC);
1862 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001863 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
Simon Dardis7577ce22017-03-09 14:03:26 +00001864 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1865 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001866 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
Zoran Jovanovic2f6845b2016-04-13 16:02:25 +00001867 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RCp);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001868 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1869 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1870 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1871 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
Simon Dardis7577ce22017-03-09 14:03:26 +00001872 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1873 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1874 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1875 unsigned Success = RegInfo.createVirtualRegister(RC);
1876 unsigned LL, SC;
1877
1878 if (isMicroMips) {
1879 LL = Mips::LL_MM;
1880 SC = Mips::SC_MM;
1881 } else {
1882 LL = Subtarget.hasMips32r6() ? (ArePtrs64bit ? Mips::LL64_R6 : Mips::LL_R6)
1883 : (ArePtrs64bit ? Mips::LL64 : Mips::LL);
1884 SC = Subtarget.hasMips32r6() ? (ArePtrs64bit ? Mips::SC64_R6 : Mips::SC_R6)
1885 : (ArePtrs64bit ? Mips::SC64 : Mips::SC);
1886 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001887
1888 // insert new blocks after the current block
1889 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Simon Dardis7577ce22017-03-09 14:03:26 +00001890 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1891 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1892 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001893 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +00001894 MachineFunction::iterator It = ++BB->getIterator();
Simon Dardis7577ce22017-03-09 14:03:26 +00001895 MF->insert(It, loop1MBB);
1896 MF->insert(It, loop2MBB);
1897 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001898 MF->insert(It, exitMBB);
1899
1900 // Transfer the remainder of BB and its successor edges to exitMBB.
1901 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001902 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001903 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1904
Simon Dardis7577ce22017-03-09 14:03:26 +00001905 BB->addSuccessor(loop1MBB);
1906 loop1MBB->addSuccessor(sinkMBB);
1907 loop1MBB->addSuccessor(loop2MBB);
1908 loop2MBB->addSuccessor(loop1MBB);
1909 loop2MBB->addSuccessor(sinkMBB);
1910 sinkMBB->addSuccessor(exitMBB);
Akira Hatanaka08636b42011-07-19 17:09:53 +00001911
Akira Hatanakae4503582011-07-19 18:14:26 +00001912 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001913 // thisMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001914 // addiu masklsb2,$0,-4 # 0xfffffffc
1915 // and alignedaddr,ptr,masklsb2
1916 // andi ptrlsb2,ptr,3
Zoran Jovanovic2f6845b2016-04-13 16:02:25 +00001917 // xori ptrlsb2,ptrlsb2,3 # Only for BE
Akira Hatanaka0e019592011-07-19 20:11:17 +00001918 // sll shiftamt,ptrlsb2,3
1919 // ori maskupper,$0,255 # 0xff
1920 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001921 // nor mask2,$0,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001922 // andi maskedcmpval,cmpval,255
1923 // sll shiftedcmpval,maskedcmpval,shiftamt
1924 // andi maskednewval,newval,255
1925 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001926 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Zoran Jovanovic2f6845b2016-04-13 16:02:25 +00001927 BuildMI(BB, DL, TII->get(ArePtrs64bit ? Mips::DADDiu : Mips::ADDiu), MaskLSB2)
1928 .addReg(ABI.GetNullPtr()).addImm(-4);
1929 BuildMI(BB, DL, TII->get(ArePtrs64bit ? Mips::AND64 : Mips::AND), AlignedAddr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001930 .addReg(Ptr).addReg(MaskLSB2);
Zoran Jovanovic2f6845b2016-04-13 16:02:25 +00001931 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2)
1932 .addReg(Ptr, 0, ArePtrs64bit ? Mips::sub_32 : 0).addImm(3);
Eric Christopher1c29a652014-07-18 22:55:25 +00001933 if (Subtarget.isLittle()) {
Akira Hatanaka2bf97332013-05-31 03:25:44 +00001934 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1935 } else {
1936 unsigned Off = RegInfo.createVirtualRegister(RC);
1937 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1938 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1939 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1940 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001941 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001942 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001943 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001944 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001945 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1946 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001947 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001948 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001949 .addReg(MaskedCmpVal).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001950 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001951 .addReg(NewVal).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001952 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001953 .addReg(MaskedNewVal).addReg(ShiftAmt);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001954
Simon Dardis7577ce22017-03-09 14:03:26 +00001955 // loop1MBB:
1956 // ll oldval,0(alginedaddr)
1957 // and maskedoldval0,oldval,mask
1958 // bne maskedoldval0,shiftedcmpval,sinkMBB
1959 BB = loop1MBB;
1960 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
1961 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
1962 .addReg(OldVal).addReg(Mask);
1963 BuildMI(BB, DL, TII->get(Mips::BNE))
1964 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
1965
1966 // loop2MBB:
1967 // and maskedoldval1,oldval,mask2
1968 // or storeval,maskedoldval1,shiftednewval
1969 // sc success,storeval,0(alignedaddr)
1970 // beq success,$0,loop1MBB
1971 BB = loop2MBB;
1972 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
1973 .addReg(OldVal).addReg(Mask2);
1974 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
1975 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
1976 BuildMI(BB, DL, TII->get(SC), Success)
1977 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
1978 BuildMI(BB, DL, TII->get(Mips::BEQ))
1979 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
1980
1981 // sinkMBB:
1982 // srl srlres,maskedoldval0,shiftamt
1983 // sign_extend dest,srlres
1984 BB = sinkMBB;
1985
1986 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
1987 .addReg(MaskedOldVal0).addReg(ShiftAmt);
1988 BB = emitSignExtendToI32InReg(MI, BB, Size, Dest, SrlRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001989
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001990 MI.eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001991
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001992 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001993}
1994
Simon Dardisba92b032016-09-09 11:06:01 +00001995SDValue MipsTargetLowering::lowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
1996 // The first operand is the chain, the second is the condition, the third is
1997 // the block to branch to if the condition is true.
1998 SDValue Chain = Op.getOperand(0);
1999 SDValue Dest = Op.getOperand(2);
2000 SDLoc DL(Op);
2001
2002 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
2003 SDValue CondRes = createFPCmp(DAG, Op.getOperand(1));
2004
2005 // Return if flag is not set by a floating point comparison.
2006 if (CondRes.getOpcode() != MipsISD::FPCmp)
2007 return Op;
2008
2009 SDValue CCNode = CondRes.getOperand(2);
2010 Mips::CondCode CC =
2011 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
2012 unsigned Opc = invertFPCondCodeUser(CC) ? Mips::BRANCH_F : Mips::BRANCH_T;
2013 SDValue BrCode = DAG.getConstant(Opc, DL, MVT::i32);
2014 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
2015 return DAG.getNode(MipsISD::FPBrcond, DL, Op.getValueType(), Chain, BrCode,
2016 FCC0, Dest, CondRes);
2017}
2018
2019SDValue MipsTargetLowering::
2020lowerSELECT(SDValue Op, SelectionDAG &DAG) const
2021{
2022 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
2023 SDValue Cond = createFPCmp(DAG, Op.getOperand(0));
2024
2025 // Return if flag is not set by a floating point comparison.
2026 if (Cond.getOpcode() != MipsISD::FPCmp)
2027 return Op;
2028
2029 return createCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
2030 SDLoc(Op));
2031}
2032
2033SDValue MipsTargetLowering::lowerSETCC(SDValue Op, SelectionDAG &DAG) const {
2034 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
2035 SDValue Cond = createFPCmp(DAG, Op);
2036
2037 assert(Cond.getOpcode() == MipsISD::FPCmp &&
2038 "Floating point operand expected.");
2039
2040 SDLoc DL(Op);
2041 SDValue True = DAG.getConstant(1, DL, MVT::i32);
2042 SDValue False = DAG.getConstant(0, DL, MVT::i32);
2043
2044 return createCMovFP(DAG, Cond, True, False, DL);
2045}
2046
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002047SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002048 SelectionDAG &DAG) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002049 EVT Ty = Op.getValueType();
2050 GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
2051 const GlobalValue *GV = N->getGlobal();
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00002052
Simon Dardisca74dd72017-01-27 11:36:52 +00002053 if (!isPositionIndependent()) {
Eric Christopher36fe0282015-02-03 07:22:52 +00002054 const MipsTargetObjectFile *TLOF =
2055 static_cast<const MipsTargetObjectFile *>(
2056 getTargetMachine().getObjFileLowering());
Peter Collingbourne67335642016-10-24 19:23:39 +00002057 const GlobalObject *GO = GV->getBaseObject();
2058 if (GO && TLOF->IsGlobalInSmallSection(GO, getTargetMachine()))
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00002059 // %gp_rel relocation
Simon Dardisae5b53e2017-08-11 14:36:05 +00002060 return getAddrGPRel(N, SDLoc(N), Ty, DAG, ABI.IsN64());
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002061
Simon Dardisca74dd72017-01-27 11:36:52 +00002062 // %hi/%lo relocation
2063 return Subtarget.hasSym32() ? getAddrNonPIC(N, SDLoc(N), Ty, DAG)
2064 // %highest/%higher/%hi/%lo relocation
2065 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG);
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00002066 }
2067
Rafael Espindolab2b6a852016-06-27 12:33:33 +00002068 // Every other architecture would use shouldAssumeDSOLocal in here, but
2069 // mips is special.
Rafael Espindola97ca8272016-06-27 23:21:07 +00002070 // * In PIC code mips requires got loads even for local statics!
Rafael Espindolab2b6a852016-06-27 12:33:33 +00002071 // * To save on got entries, for local statics the got entry contains the
2072 // page and an additional add instruction takes care of the low bits.
2073 // * It is legal to access a hidden symbol with a non hidden undefined,
2074 // so one cannot guarantee that all access to a hidden symbol will know
2075 // it is hidden.
2076 // * Mips linkers don't support creating a page and a full got entry for
2077 // the same symbol.
2078 // * Given all that, we have to use a full got entry for hidden symbols :-(
Rafael Espindola1ac1fa82016-06-27 03:19:40 +00002079 if (GV->hasLocalLinkage())
Eric Christopher96e72c62015-01-29 23:27:36 +00002080 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002081
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00002082 if (LargeGOT)
Alex Lorenze40c8a22015-08-11 23:09:45 +00002083 return getAddrGlobalLargeGOT(
2084 N, SDLoc(N), Ty, DAG, MipsII::MO_GOT_HI16, MipsII::MO_GOT_LO16,
2085 DAG.getEntryNode(),
2086 MachinePointerInfo::getGOT(DAG.getMachineFunction()));
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00002087
Alex Lorenze40c8a22015-08-11 23:09:45 +00002088 return getAddrGlobal(
2089 N, SDLoc(N), Ty, DAG,
Daniel Sandersfe98b2f2016-05-03 13:35:44 +00002090 (ABI.IsN32() || ABI.IsN64()) ? MipsII::MO_GOT_DISP : MipsII::MO_GOT,
Alex Lorenze40c8a22015-08-11 23:09:45 +00002091 DAG.getEntryNode(), MachinePointerInfo::getGOT(DAG.getMachineFunction()));
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00002092}
2093
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002094SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op,
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +00002095 SelectionDAG &DAG) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002096 BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op);
2097 EVT Ty = Op.getValueType();
Akira Hatanaka30f97cf2013-09-25 00:30:25 +00002098
Simon Dardisca74dd72017-01-27 11:36:52 +00002099 if (!isPositionIndependent())
2100 return Subtarget.hasSym32() ? getAddrNonPIC(N, SDLoc(N), Ty, DAG)
2101 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG);
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002102
Eric Christopher96e72c62015-01-29 23:27:36 +00002103 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +00002104}
2105
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00002106SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002107lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00002108{
Akira Hatanakabff84e12011-12-14 18:26:41 +00002109 // If the relocation model is PIC, use the General Dynamic TLS Model or
2110 // Local Dynamic TLS model, otherwise use the Initial Exec or
2111 // Local Exec TLS Model.
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00002112
2113 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +00002114 if (DAG.getTarget().Options.EmulatedTLS)
2115 return LowerToTLSEmulatedModel(GA, DAG);
2116
Andrew Trickef9de2a2013-05-25 02:42:55 +00002117 SDLoc DL(GA);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00002118 const GlobalValue *GV = GA->getGlobal();
Mehdi Amini44ede332015-07-09 02:09:04 +00002119 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00002120
Hans Wennborgaea41202012-05-04 09:40:39 +00002121 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
2122
2123 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Hans Wennborg245917b2012-06-04 14:02:08 +00002124 // General Dynamic and Local Dynamic TLS Model.
2125 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
2126 : MipsII::MO_TLSGD;
2127
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002128 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag);
2129 SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT,
2130 getGlobalReg(DAG, PtrVT), TGA);
Akira Hatanakaf10ee842011-12-08 21:05:38 +00002131 unsigned PtrSize = PtrVT.getSizeInBits();
2132 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
2133
Benjamin Kramer64ba50a2011-12-11 12:21:34 +00002134 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00002135
2136 ArgListTy Args;
2137 ArgListEntry Entry;
2138 Entry.Node = Argument;
Akira Hatanakadee6c822011-12-08 20:34:32 +00002139 Entry.Ty = PtrTy;
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00002140 Args.push_back(Entry);
Jia Liuf54f60f2012-02-28 07:46:26 +00002141
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002142 TargetLowering::CallLoweringInfo CLI(DAG);
Nirav Daveac6081c2017-03-18 00:44:07 +00002143 CLI.setDebugLoc(DL)
2144 .setChain(DAG.getEntryNode())
2145 .setLibCallee(CallingConv::C, PtrTy, TlsGetAddr, std::move(Args));
Justin Holewinskiaa583972012-05-25 16:35:28 +00002146 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00002147
Akira Hatanakabff84e12011-12-14 18:26:41 +00002148 SDValue Ret = CallResult.first;
2149
Hans Wennborgaea41202012-05-04 09:40:39 +00002150 if (model != TLSModel::LocalDynamic)
Akira Hatanakabff84e12011-12-14 18:26:41 +00002151 return Ret;
2152
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002153 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanakabff84e12011-12-14 18:26:41 +00002154 MipsII::MO_DTPREL_HI);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002155 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
2156 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanakabff84e12011-12-14 18:26:41 +00002157 MipsII::MO_DTPREL_LO);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002158 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
2159 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret);
2160 return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00002161 }
Akira Hatanaka5b350be2011-06-21 01:02:03 +00002162
2163 SDValue Offset;
Hans Wennborgaea41202012-05-04 09:40:39 +00002164 if (model == TLSModel::InitialExec) {
Akira Hatanaka5b350be2011-06-21 01:02:03 +00002165 // Initial Exec TLS Model
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002166 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00002167 MipsII::MO_GOTTPREL);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002168 TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT),
Akira Hatanakab049aef2012-02-24 22:34:47 +00002169 TGA);
Justin Lebar9c375812016-07-15 18:27:10 +00002170 Offset =
2171 DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), TGA, MachinePointerInfo());
Akira Hatanaka5b350be2011-06-21 01:02:03 +00002172 } else {
2173 // Local Exec TLS Model
Hans Wennborgaea41202012-05-04 09:40:39 +00002174 assert(model == TLSModel::LocalExec);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002175 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00002176 MipsII::MO_TPREL_HI);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002177 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00002178 MipsII::MO_TPREL_LO);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002179 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
2180 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
2181 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
Akira Hatanaka5b350be2011-06-21 01:02:03 +00002182 }
2183
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002184 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
2185 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00002186}
2187
2188SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002189lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +00002190{
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002191 JumpTableSDNode *N = cast<JumpTableSDNode>(Op);
2192 EVT Ty = Op.getValueType();
Akira Hatanaka30f97cf2013-09-25 00:30:25 +00002193
Simon Dardisca74dd72017-01-27 11:36:52 +00002194 if (!isPositionIndependent())
2195 return Subtarget.hasSym32() ? getAddrNonPIC(N, SDLoc(N), Ty, DAG)
2196 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG);
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002197
Eric Christopher96e72c62015-01-29 23:27:36 +00002198 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +00002199}
2200
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002201SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002202lowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +00002203{
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002204 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
2205 EVT Ty = Op.getValueType();
Bruno Cardoso Lopes2db07582009-11-25 12:17:58 +00002206
Simon Dardisca74dd72017-01-27 11:36:52 +00002207 if (!isPositionIndependent()) {
Eric Christopher36fe0282015-02-03 07:22:52 +00002208 const MipsTargetObjectFile *TLOF =
2209 static_cast<const MipsTargetObjectFile *>(
2210 getTargetMachine().getObjFileLowering());
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00002211
Mehdi Aminibd7287e2015-07-16 06:11:10 +00002212 if (TLOF->IsConstantInSmallSection(DAG.getDataLayout(), N->getConstVal(),
2213 getTargetMachine()))
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00002214 // %gp_rel relocation
Simon Dardisae5b53e2017-08-11 14:36:05 +00002215 return getAddrGPRel(N, SDLoc(N), Ty, DAG, ABI.IsN64());
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00002216
Simon Dardisca74dd72017-01-27 11:36:52 +00002217 return Subtarget.hasSym32() ? getAddrNonPIC(N, SDLoc(N), Ty, DAG)
2218 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG);
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00002219 }
Bruno Cardoso Lopesfdb4cec2008-07-23 16:01:50 +00002220
Simon Dardisca74dd72017-01-27 11:36:52 +00002221 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +00002222}
2223
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002224SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman31ae5862010-04-17 14:41:14 +00002225 MachineFunction &MF = DAG.getMachineFunction();
2226 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
2227
Andrew Trickef9de2a2013-05-25 02:42:55 +00002228 SDLoc DL(Op);
Dan Gohman31ae5862010-04-17 14:41:14 +00002229 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
Mehdi Amini44ede332015-07-09 02:09:04 +00002230 getPointerTy(MF.getDataLayout()));
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +00002231
2232 // vastart just stores the address of the VarArgsFrameIndex slot into the
2233 // memory location argument.
2234 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002235 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
Justin Lebar9c375812016-07-15 18:27:10 +00002236 MachinePointerInfo(SV));
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +00002237}
Jia Liuf54f60f2012-02-28 07:46:26 +00002238
Daniel Sanders2b553d42014-08-01 09:17:39 +00002239SDValue MipsTargetLowering::lowerVAARG(SDValue Op, SelectionDAG &DAG) const {
2240 SDNode *Node = Op.getNode();
2241 EVT VT = Node->getValueType(0);
2242 SDValue Chain = Node->getOperand(0);
2243 SDValue VAListPtr = Node->getOperand(1);
2244 unsigned Align = Node->getConstantOperandVal(3);
2245 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
2246 SDLoc DL(Node);
Eric Christopher96e72c62015-01-29 23:27:36 +00002247 unsigned ArgSlotSizeInBytes = (ABI.IsN32() || ABI.IsN64()) ? 8 : 4;
Daniel Sanders2b553d42014-08-01 09:17:39 +00002248
Justin Lebar9c375812016-07-15 18:27:10 +00002249 SDValue VAListLoad = DAG.getLoad(getPointerTy(DAG.getDataLayout()), DL, Chain,
2250 VAListPtr, MachinePointerInfo(SV));
Daniel Sanders2b553d42014-08-01 09:17:39 +00002251 SDValue VAList = VAListLoad;
2252
2253 // Re-align the pointer if necessary.
2254 // It should only ever be necessary for 64-bit types on O32 since the minimum
2255 // argument alignment is the same as the maximum type alignment for N32/N64.
2256 //
2257 // FIXME: We currently align too often. The code generator doesn't notice
2258 // when the pointer is still aligned from the last va_arg (or pair of
2259 // va_args for the i64 on O32 case).
2260 if (Align > getMinStackArgumentAlignment()) {
2261 assert(((Align & (Align-1)) == 0) && "Expected Align to be a power of 2");
2262
2263 VAList = DAG.getNode(ISD::ADD, DL, VAList.getValueType(), VAList,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002264 DAG.getConstant(Align - 1, DL, VAList.getValueType()));
Daniel Sanders2b553d42014-08-01 09:17:39 +00002265
2266 VAList = DAG.getNode(ISD::AND, DL, VAList.getValueType(), VAList,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002267 DAG.getConstant(-(int64_t)Align, DL,
Daniel Sanders2b553d42014-08-01 09:17:39 +00002268 VAList.getValueType()));
2269 }
2270
2271 // Increment the pointer, VAList, to the next vaarg.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00002272 auto &TD = DAG.getDataLayout();
2273 unsigned ArgSizeInBytes =
2274 TD.getTypeAllocSize(VT.getTypeForEVT(*DAG.getContext()));
Rui Ueyamada00f2f2016-01-14 21:06:47 +00002275 SDValue Tmp3 =
2276 DAG.getNode(ISD::ADD, DL, VAList.getValueType(), VAList,
2277 DAG.getConstant(alignTo(ArgSizeInBytes, ArgSlotSizeInBytes),
2278 DL, VAList.getValueType()));
Daniel Sanders2b553d42014-08-01 09:17:39 +00002279 // Store the incremented VAList to the legalized pointer
2280 Chain = DAG.getStore(VAListLoad.getValue(1), DL, Tmp3, VAListPtr,
Justin Lebar9c375812016-07-15 18:27:10 +00002281 MachinePointerInfo(SV));
Daniel Sanders2b553d42014-08-01 09:17:39 +00002282
2283 // In big-endian mode we must adjust the pointer when the load size is smaller
2284 // than the argument slot size. We must also reduce the known alignment to
2285 // match. For example in the N64 ABI, we must add 4 bytes to the offset to get
2286 // the correct half of the slot, and reduce the alignment from 8 (slot
2287 // alignment) down to 4 (type alignment).
2288 if (!Subtarget.isLittle() && ArgSizeInBytes < ArgSlotSizeInBytes) {
2289 unsigned Adjustment = ArgSlotSizeInBytes - ArgSizeInBytes;
2290 VAList = DAG.getNode(ISD::ADD, DL, VAListPtr.getValueType(), VAList,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002291 DAG.getIntPtrConstant(Adjustment, DL));
Daniel Sanders2b553d42014-08-01 09:17:39 +00002292 }
2293 // Load the actual argument out of the pointer VAList
Justin Lebar9c375812016-07-15 18:27:10 +00002294 return DAG.getLoad(VT, DL, Chain, VAList, MachinePointerInfo());
Daniel Sanders2b553d42014-08-01 09:17:39 +00002295}
2296
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00002297static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG,
2298 bool HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002299 EVT TyX = Op.getOperand(0).getValueType();
2300 EVT TyY = Op.getOperand(1).getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002301 SDLoc DL(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002302 SDValue Const1 = DAG.getConstant(1, DL, MVT::i32);
2303 SDValue Const31 = DAG.getConstant(31, DL, MVT::i32);
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002304 SDValue Res;
2305
2306 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
2307 // to i32.
2308 SDValue X = (TyX == MVT::f32) ?
2309 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
2310 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
2311 Const1);
2312 SDValue Y = (TyY == MVT::f32) ?
2313 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
2314 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
2315 Const1);
2316
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00002317 if (HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002318 // ext E, Y, 31, 1 ; extract bit31 of Y
2319 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
2320 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
2321 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
2322 } else {
2323 // sll SllX, X, 1
2324 // srl SrlX, SllX, 1
2325 // srl SrlY, Y, 31
2326 // sll SllY, SrlX, 31
2327 // or Or, SrlX, SllY
2328 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
2329 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
2330 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
2331 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
2332 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
2333 }
2334
2335 if (TyX == MVT::f32)
2336 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
2337
2338 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002339 Op.getOperand(0),
2340 DAG.getConstant(0, DL, MVT::i32));
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002341 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002342}
2343
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00002344static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG,
2345 bool HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002346 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
2347 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
2348 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002349 SDLoc DL(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002350 SDValue Const1 = DAG.getConstant(1, DL, MVT::i32);
Eric Christopher0713a9d2011-06-08 23:55:35 +00002351
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002352 // Bitcast to integer nodes.
2353 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
2354 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002355
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00002356 if (HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002357 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
2358 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
2359 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002360 DAG.getConstant(WidthY - 1, DL, MVT::i32), Const1);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002361
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002362 if (WidthX > WidthY)
2363 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
2364 else if (WidthY > WidthX)
2365 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002366
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002367 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002368 DAG.getConstant(WidthX - 1, DL, MVT::i32), Const1,
2369 X);
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002370 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
2371 }
2372
2373 // (d)sll SllX, X, 1
2374 // (d)srl SrlX, SllX, 1
2375 // (d)srl SrlY, Y, width(Y)-1
2376 // (d)sll SllY, SrlX, width(Y)-1
2377 // or Or, SrlX, SllY
2378 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
2379 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
2380 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002381 DAG.getConstant(WidthY - 1, DL, MVT::i32));
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002382
2383 if (WidthX > WidthY)
2384 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
2385 else if (WidthY > WidthX)
2386 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
2387
2388 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002389 DAG.getConstant(WidthX - 1, DL, MVT::i32));
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002390 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
2391 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002392}
2393
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00002394SDValue
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002395MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00002396 if (Subtarget.isGP64bit())
2397 return lowerFCOPYSIGN64(Op, DAG, Subtarget.hasExtractInsert());
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002398
Eric Christopher1c29a652014-07-18 22:55:25 +00002399 return lowerFCOPYSIGN32(Op, DAG, Subtarget.hasExtractInsert());
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002400}
2401
Akira Hatanaka66277522011-06-02 00:24:44 +00002402SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002403lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes5444a7b2011-06-16 00:40:02 +00002404 // check the depth
2405 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka15506782011-06-07 18:58:42 +00002406 "Frame address can only be determined for current frame.");
Akira Hatanaka66277522011-06-02 00:24:44 +00002407
Matthias Braun941a7052016-07-28 18:40:00 +00002408 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
2409 MFI.setFrameAddressIsTaken(true);
Akira Hatanaka66277522011-06-02 00:24:44 +00002410 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002411 SDLoc DL(Op);
Eric Christopher96e72c62015-01-29 23:27:36 +00002412 SDValue FrameAddr = DAG.getCopyFromReg(
2413 DAG.getEntryNode(), DL, ABI.IsN64() ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka66277522011-06-02 00:24:44 +00002414 return FrameAddr;
2415}
2416
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002417SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op,
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002418 SelectionDAG &DAG) const {
Bill Wendling908bf812014-01-06 00:43:20 +00002419 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00002420 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00002421
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002422 // check the depth
2423 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
2424 "Return address can be determined only for current frame.");
2425
2426 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00002427 MachineFrameInfo &MFI = MF.getFrameInfo();
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00002428 MVT VT = Op.getSimpleValueType();
Eric Christopher96e72c62015-01-29 23:27:36 +00002429 unsigned RA = ABI.IsN64() ? Mips::RA_64 : Mips::RA;
Matthias Braun941a7052016-07-28 18:40:00 +00002430 MFI.setReturnAddressIsTaken(true);
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002431
2432 // Return RA, which contains the return address. Mark it an implicit live-in.
2433 unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00002434 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT);
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002435}
2436
Akira Hatanakac0b02062013-01-30 00:26:49 +00002437// An EH_RETURN is the result of lowering llvm.eh.return which in turn is
2438// generated from __builtin_eh_return (offset, handler)
2439// The effect of this is to adjust the stack pointer by "offset"
2440// and then branch to "handler".
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002441SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Akira Hatanakac0b02062013-01-30 00:26:49 +00002442 const {
2443 MachineFunction &MF = DAG.getMachineFunction();
2444 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2445
2446 MipsFI->setCallsEhReturn();
2447 SDValue Chain = Op.getOperand(0);
2448 SDValue Offset = Op.getOperand(1);
2449 SDValue Handler = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002450 SDLoc DL(Op);
Eric Christopher96e72c62015-01-29 23:27:36 +00002451 EVT Ty = ABI.IsN64() ? MVT::i64 : MVT::i32;
Akira Hatanakac0b02062013-01-30 00:26:49 +00002452
2453 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
2454 // EH_RETURN nodes, so that instructions are emitted back-to-back.
Eric Christopher96e72c62015-01-29 23:27:36 +00002455 unsigned OffsetReg = ABI.IsN64() ? Mips::V1_64 : Mips::V1;
2456 unsigned AddrReg = ABI.IsN64() ? Mips::V0_64 : Mips::V0;
Akira Hatanakac0b02062013-01-30 00:26:49 +00002457 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
2458 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
2459 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
2460 DAG.getRegister(OffsetReg, Ty),
Mehdi Amini44ede332015-07-09 02:09:04 +00002461 DAG.getRegister(AddrReg, getPointerTy(MF.getDataLayout())),
Akira Hatanakac0b02062013-01-30 00:26:49 +00002462 Chain.getValue(1));
2463}
2464
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002465SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
Akira Hatanaka5fd22482012-06-14 21:10:56 +00002466 SelectionDAG &DAG) const {
Eli Friedman26a48482011-07-27 22:21:52 +00002467 // FIXME: Need pseudo-fence for 'singlethread' fences
2468 // FIXME: Set SType for weaker fences where supported/appropriate.
2469 unsigned SType = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002470 SDLoc DL(Op);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002471 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002472 DAG.getConstant(SType, DL, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00002473}
2474
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002475SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
Akira Hatanaka5fd22482012-06-14 21:10:56 +00002476 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002477 SDLoc DL(Op);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002478 MVT VT = Subtarget.isGP64bit() ? MVT::i64 : MVT::i32;
2479
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002480 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2481 SDValue Shamt = Op.getOperand(2);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002482 // if shamt < (VT.bits):
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002483 // lo = (shl lo, shamt)
2484 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
2485 // else:
2486 // lo = 0
2487 // hi = (shl lo, shamt[4:0])
2488 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002489 DAG.getConstant(-1, DL, MVT::i32));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002490 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, VT, Lo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002491 DAG.getConstant(1, DL, VT));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002492 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, VT, ShiftRight1Lo, Not);
2493 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, VT, Hi, Shamt);
2494 SDValue Or = DAG.getNode(ISD::OR, DL, VT, ShiftLeftHi, ShiftRightLo);
2495 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, VT, Lo, Shamt);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002496 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
Daniel Sanders301f9372015-04-29 12:28:58 +00002497 DAG.getConstant(VT.getSizeInBits(), DL, MVT::i32));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002498 Lo = DAG.getNode(ISD::SELECT, DL, VT, Cond,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002499 DAG.getConstant(0, DL, VT), ShiftLeftLo);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002500 Hi = DAG.getNode(ISD::SELECT, DL, VT, Cond, ShiftLeftLo, Or);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002501
2502 SDValue Ops[2] = {Lo, Hi};
Craig Topper64941d92014-04-27 19:20:57 +00002503 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002504}
2505
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002506SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002507 bool IsSRA) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002508 SDLoc DL(Op);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002509 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2510 SDValue Shamt = Op.getOperand(2);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002511 MVT VT = Subtarget.isGP64bit() ? MVT::i64 : MVT::i32;
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002512
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002513 // if shamt < (VT.bits):
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002514 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
2515 // if isSRA:
2516 // hi = (sra hi, shamt)
2517 // else:
2518 // hi = (srl hi, shamt)
2519 // else:
2520 // if isSRA:
2521 // lo = (sra hi, shamt[4:0])
2522 // hi = (sra hi, 31)
2523 // else:
2524 // lo = (srl hi, shamt[4:0])
2525 // hi = 0
2526 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002527 DAG.getConstant(-1, DL, MVT::i32));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002528 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, VT, Hi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002529 DAG.getConstant(1, DL, VT));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002530 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, VT, ShiftLeft1Hi, Not);
2531 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, VT, Lo, Shamt);
2532 SDValue Or = DAG.getNode(ISD::OR, DL, VT, ShiftLeftHi, ShiftRightLo);
2533 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL,
2534 DL, VT, Hi, Shamt);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002535 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
Daniel Sanders301f9372015-04-29 12:28:58 +00002536 DAG.getConstant(VT.getSizeInBits(), DL, MVT::i32));
2537 SDValue Ext = DAG.getNode(ISD::SRA, DL, VT, Hi,
2538 DAG.getConstant(VT.getSizeInBits() - 1, DL, VT));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002539 Lo = DAG.getNode(ISD::SELECT, DL, VT, Cond, ShiftRightHi, Or);
2540 Hi = DAG.getNode(ISD::SELECT, DL, VT, Cond,
Daniel Sanders301f9372015-04-29 12:28:58 +00002541 IsSRA ? Ext : DAG.getConstant(0, DL, VT), ShiftRightHi);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002542
2543 SDValue Ops[2] = {Lo, Hi};
Craig Topper64941d92014-04-27 19:20:57 +00002544 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002545}
2546
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002547static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002548 SDValue Chain, SDValue Src, unsigned Offset) {
Akira Hatanaka95866182012-06-13 19:06:08 +00002549 SDValue Ptr = LD->getBasePtr();
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002550 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
Akira Hatanaka95866182012-06-13 19:06:08 +00002551 EVT BasePtrVT = Ptr.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002552 SDLoc DL(LD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002553 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
2554
2555 if (Offset)
Akira Hatanaka95866182012-06-13 19:06:08 +00002556 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002557 DAG.getConstant(Offset, DL, BasePtrVT));
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002558
2559 SDValue Ops[] = { Chain, Ptr, Src };
Craig Topper206fcd42014-04-26 19:29:41 +00002560 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002561 LD->getMemOperand());
2562}
2563
2564// Expand an unaligned 32 or 64-bit integer load node.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002565SDValue MipsTargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002566 LoadSDNode *LD = cast<LoadSDNode>(Op);
2567 EVT MemVT = LD->getMemoryVT();
2568
Eric Christopher1c29a652014-07-18 22:55:25 +00002569 if (Subtarget.systemSupportsUnalignedAccess())
Daniel Sandersac272632014-05-23 13:18:02 +00002570 return Op;
2571
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002572 // Return if load is aligned or if MemVT is neither i32 nor i64.
2573 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
2574 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
2575 return SDValue();
2576
Eric Christopher1c29a652014-07-18 22:55:25 +00002577 bool IsLittle = Subtarget.isLittle();
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002578 EVT VT = Op.getValueType();
2579 ISD::LoadExtType ExtType = LD->getExtensionType();
2580 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
2581
2582 assert((VT == MVT::i32) || (VT == MVT::i64));
2583
2584 // Expand
2585 // (set dst, (i64 (load baseptr)))
2586 // to
2587 // (set tmp, (ldl (add baseptr, 7), undef))
2588 // (set dst, (ldr baseptr, tmp))
2589 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002590 SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002591 IsLittle ? 7 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002592 return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002593 IsLittle ? 0 : 7);
2594 }
2595
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002596 SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002597 IsLittle ? 3 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002598 SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002599 IsLittle ? 0 : 3);
2600
2601 // Expand
2602 // (set dst, (i32 (load baseptr))) or
2603 // (set dst, (i64 (sextload baseptr))) or
2604 // (set dst, (i64 (extload baseptr)))
2605 // to
2606 // (set tmp, (lwl (add baseptr, 3), undef))
2607 // (set dst, (lwr baseptr, tmp))
2608 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
2609 (ExtType == ISD::EXTLOAD))
2610 return LWR;
2611
2612 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
2613
2614 // Expand
2615 // (set dst, (i64 (zextload baseptr)))
2616 // to
2617 // (set tmp0, (lwl (add baseptr, 3), undef))
2618 // (set tmp1, (lwr baseptr, tmp0))
2619 // (set tmp2, (shl tmp1, 32))
2620 // (set dst, (srl tmp2, 32))
Andrew Trickef9de2a2013-05-25 02:42:55 +00002621 SDLoc DL(LD);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002622 SDValue Const32 = DAG.getConstant(32, DL, MVT::i32);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002623 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
Akira Hatanaka67346852012-06-04 17:46:29 +00002624 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
2625 SDValue Ops[] = { SRL, LWR.getValue(1) };
Craig Topper64941d92014-04-27 19:20:57 +00002626 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002627}
2628
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002629static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002630 SDValue Chain, unsigned Offset) {
Akira Hatanaka95866182012-06-13 19:06:08 +00002631 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
2632 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002633 SDLoc DL(SD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002634 SDVTList VTList = DAG.getVTList(MVT::Other);
2635
2636 if (Offset)
Akira Hatanaka95866182012-06-13 19:06:08 +00002637 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002638 DAG.getConstant(Offset, DL, BasePtrVT));
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002639
2640 SDValue Ops[] = { Chain, Value, Ptr };
Craig Topper206fcd42014-04-26 19:29:41 +00002641 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002642 SD->getMemOperand());
2643}
2644
2645// Expand an unaligned 32 or 64-bit integer store node.
Akira Hatanakad82ee942013-05-16 20:45:17 +00002646static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG,
2647 bool IsLittle) {
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002648 SDValue Value = SD->getValue(), Chain = SD->getChain();
2649 EVT VT = Value.getValueType();
2650
2651 // Expand
2652 // (store val, baseptr) or
2653 // (truncstore val, baseptr)
2654 // to
2655 // (swl val, (add baseptr, 3))
2656 // (swr val, baseptr)
2657 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002658 SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002659 IsLittle ? 3 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002660 return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002661 }
2662
2663 assert(VT == MVT::i64);
2664
2665 // Expand
2666 // (store val, baseptr)
2667 // to
2668 // (sdl val, (add baseptr, 7))
2669 // (sdr val, baseptr)
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002670 SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2671 return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002672}
2673
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002674// Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr).
2675static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG) {
2676 SDValue Val = SD->getValue();
2677
2678 if (Val.getOpcode() != ISD::FP_TO_SINT)
2679 return SDValue();
2680
2681 EVT FPTy = EVT::getFloatingPointVT(Val.getValueSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002682 SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy,
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002683 Val.getOperand(0));
Andrew Trickef9de2a2013-05-25 02:42:55 +00002684 return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(),
Justin Lebar9c375812016-07-15 18:27:10 +00002685 SD->getPointerInfo(), SD->getAlignment(),
2686 SD->getMemOperand()->getFlags());
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002687}
2688
Akira Hatanakad82ee942013-05-16 20:45:17 +00002689SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2690 StoreSDNode *SD = cast<StoreSDNode>(Op);
2691 EVT MemVT = SD->getMemoryVT();
2692
2693 // Lower unaligned integer stores.
Eric Christopher1c29a652014-07-18 22:55:25 +00002694 if (!Subtarget.systemSupportsUnalignedAccess() &&
Daniel Sandersac272632014-05-23 13:18:02 +00002695 (SD->getAlignment() < MemVT.getSizeInBits() / 8) &&
Akira Hatanakad82ee942013-05-16 20:45:17 +00002696 ((MemVT == MVT::i32) || (MemVT == MVT::i64)))
Eric Christopher1c29a652014-07-18 22:55:25 +00002697 return lowerUnalignedIntStore(SD, DAG, Subtarget.isLittle());
Akira Hatanakad82ee942013-05-16 20:45:17 +00002698
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002699 return lowerFP_TO_SINT_STORE(SD, DAG);
Akira Hatanakad82ee942013-05-16 20:45:17 +00002700}
2701
Hal Finkel5081ac22016-09-01 10:28:47 +00002702SDValue MipsTargetLowering::lowerEH_DWARF_CFA(SDValue Op,
2703 SelectionDAG &DAG) const {
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002704
Hal Finkel5081ac22016-09-01 10:28:47 +00002705 // Return a fixed StackObject with offset 0 which points to the old stack
2706 // pointer.
Matthias Braun941a7052016-07-28 18:40:00 +00002707 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002708 EVT ValTy = Op->getValueType(0);
Matthias Braun941a7052016-07-28 18:40:00 +00002709 int FI = MFI.CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
Hal Finkel5081ac22016-09-01 10:28:47 +00002710 return DAG.getFrameIndex(FI, ValTy);
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002711}
2712
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002713SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op,
2714 SelectionDAG &DAG) const {
2715 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002716 SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy,
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002717 Op.getOperand(0));
Andrew Trickef9de2a2013-05-25 02:42:55 +00002718 return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc);
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002719}
2720
Akira Hatanakae2489122011-04-15 21:51:11 +00002721//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002722// Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002723//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002724
Akira Hatanakae2489122011-04-15 21:51:11 +00002725//===----------------------------------------------------------------------===//
Wesley Peck527da1b2010-11-23 03:31:01 +00002726// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002727// Mips O32 ABI rules:
2728// ---
2729// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peck527da1b2010-11-23 03:31:01 +00002730// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002731// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peck527da1b2010-11-23 03:31:01 +00002732// f64 - Only passed in two aliased f32 registers if no int reg has been used
2733// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Sylvestre Ledru469de192014-08-11 18:04:46 +00002734// not used, it must be shadowed. If only A3 is available, shadow it and
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002735// go to stack.
Simon Dardis212cccb2017-06-09 14:37:08 +00002736// vXiX - Received as scalarized i32s, passed in A0 - A3 and the stack.
2737// vXf32 - Passed in either a pair of registers {A0, A1}, {A2, A3} or {A0 - A3}
2738// with the remainder spilled to the stack.
2739// vXf64 - Passed in either {A0, A1, A2, A3} or {A2, A3} and in both cases
2740// spilling the remainder to the stack.
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002741//
2742// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanakae2489122011-04-15 21:51:11 +00002743//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002744
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00002745static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2746 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002747 CCState &State, ArrayRef<MCPhysReg> F64Regs) {
Eric Christopher96e72c62015-01-29 23:27:36 +00002748 const MipsSubtarget &Subtarget = static_cast<const MipsSubtarget &>(
2749 State.getMachineFunction().getSubtarget());
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002750
Craig Topper840beec2014-04-04 05:16:06 +00002751 static const MCPhysReg IntRegs[] = { Mips::A0, Mips::A1, Mips::A2, Mips::A3 };
Simon Dardis212cccb2017-06-09 14:37:08 +00002752
2753 const MipsCCState * MipsState = static_cast<MipsCCState *>(&State);
2754
Craig Topper840beec2014-04-04 05:16:06 +00002755 static const MCPhysReg F32Regs[] = { Mips::F12, Mips::F14 };
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002756
Simon Dardis212cccb2017-06-09 14:37:08 +00002757 static const MCPhysReg FloatVectorIntRegs[] = { Mips::A0, Mips::A2 };
2758
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002759 // Do not process byval args here.
2760 if (ArgFlags.isByVal())
2761 return true;
Akira Hatanaka5e16c6a2011-05-24 19:18:33 +00002762
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002763 // Promote i8 and i16
Daniel Sandersd134c9d2014-12-02 20:40:27 +00002764 if (ArgFlags.isInReg() && !Subtarget.isLittle()) {
2765 if (LocVT == MVT::i8 || LocVT == MVT::i16 || LocVT == MVT::i32) {
2766 LocVT = MVT::i32;
2767 if (ArgFlags.isSExt())
2768 LocInfo = CCValAssign::SExtUpper;
2769 else if (ArgFlags.isZExt())
2770 LocInfo = CCValAssign::ZExtUpper;
2771 else
2772 LocInfo = CCValAssign::AExtUpper;
2773 }
2774 }
2775
2776 // Promote i8 and i16
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002777 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
2778 LocVT = MVT::i32;
2779 if (ArgFlags.isSExt())
2780 LocInfo = CCValAssign::SExt;
2781 else if (ArgFlags.isZExt())
2782 LocInfo = CCValAssign::ZExt;
2783 else
2784 LocInfo = CCValAssign::AExt;
2785 }
2786
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002787 unsigned Reg;
2788
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002789 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
2790 // is true: function is vararg, argument is 3rd or higher, there is previous
2791 // argument which is not f32 or f64.
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002792 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1 ||
2793 State.getFirstUnallocated(F32Regs) != ValNo;
Akira Hatanaka9e6a8cc2011-05-19 20:29:48 +00002794 unsigned OrigAlign = ArgFlags.getOrigAlign();
2795 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Simon Dardis212cccb2017-06-09 14:37:08 +00002796 bool isVectorFloat = MipsState->WasOriginalArgVectorFloat(ValNo);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002797
Simon Dardis212cccb2017-06-09 14:37:08 +00002798 // The MIPS vector ABI for floats passes them in a pair of registers
2799 if (ValVT == MVT::i32 && isVectorFloat) {
2800 // This is the start of an vector that was scalarized into an unknown number
2801 // of components. It doesn't matter how many there are. Allocate one of the
2802 // notional 8 byte aligned registers which map onto the argument stack, and
2803 // shadow the register lost to alignment requirements.
2804 if (ArgFlags.isSplit()) {
2805 Reg = State.AllocateReg(FloatVectorIntRegs);
2806 if (Reg == Mips::A2)
2807 State.AllocateReg(Mips::A1);
2808 else if (Reg == 0)
2809 State.AllocateReg(Mips::A3);
2810 } else {
2811 // If we're an intermediate component of the split, we can just attempt to
2812 // allocate a register directly.
2813 Reg = State.AllocateReg(IntRegs);
2814 }
2815 } else if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002816 Reg = State.AllocateReg(IntRegs);
Akira Hatanaka9e6a8cc2011-05-19 20:29:48 +00002817 // If this is the first part of an i64 arg,
2818 // the allocated register must be either A0 or A2.
2819 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002820 Reg = State.AllocateReg(IntRegs);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002821 LocVT = MVT::i32;
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002822 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
2823 // Allocate int register and shadow next int register. If first
2824 // available register is Mips::A1 or Mips::A3, shadow it too.
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002825 Reg = State.AllocateReg(IntRegs);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002826 if (Reg == Mips::A1 || Reg == Mips::A3)
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002827 Reg = State.AllocateReg(IntRegs);
2828 State.AllocateReg(IntRegs);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002829 LocVT = MVT::i32;
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002830 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
2831 // we are guaranteed to find an available float register
2832 if (ValVT == MVT::f32) {
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002833 Reg = State.AllocateReg(F32Regs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002834 // Shadow int register
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002835 State.AllocateReg(IntRegs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002836 } else {
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002837 Reg = State.AllocateReg(F64Regs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002838 // Shadow int registers
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002839 unsigned Reg2 = State.AllocateReg(IntRegs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002840 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002841 State.AllocateReg(IntRegs);
2842 State.AllocateReg(IntRegs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002843 }
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002844 } else
2845 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002846
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002847 if (!Reg) {
2848 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3,
2849 OrigAlign);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002850 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002851 } else
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002852 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002853
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002854 return false;
Akira Hatanaka202f6402011-11-12 02:20:46 +00002855}
2856
Akira Hatanakabfb66242013-08-20 23:38:40 +00002857static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT,
2858 MVT LocVT, CCValAssign::LocInfo LocInfo,
2859 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002860 static const MCPhysReg F64Regs[] = { Mips::D6, Mips::D7 };
Akira Hatanakabfb66242013-08-20 23:38:40 +00002861
2862 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2863}
2864
2865static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT,
2866 MVT LocVT, CCValAssign::LocInfo LocInfo,
2867 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002868 static const MCPhysReg F64Regs[] = { Mips::D12_64, Mips::D14_64 };
Akira Hatanakabfb66242013-08-20 23:38:40 +00002869
2870 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2871}
2872
Reid Klecknerd3781742014-11-14 00:39:33 +00002873static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2874 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
2875 CCState &State) LLVM_ATTRIBUTE_UNUSED;
Reed Kotlerd5c41962014-11-13 23:37:45 +00002876
Akira Hatanaka202f6402011-11-12 02:20:46 +00002877#include "MipsGenCallingConv.inc"
2878
Akira Hatanakae2489122011-04-15 21:51:11 +00002879//===----------------------------------------------------------------------===//
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002880// Call Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002881//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002882
Akira Hatanaka61bbcce2011-09-23 00:58:33 +00002883// Return next O32 integer argument register.
2884static unsigned getNextIntArgReg(unsigned Reg) {
2885 assert((Reg == Mips::A0) || (Reg == Mips::A2));
2886 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
2887}
2888
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002889SDValue MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
2890 SDValue Chain, SDValue Arg,
2891 const SDLoc &DL, bool IsTailCall,
2892 SelectionDAG &DAG) const {
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002893 if (!IsTailCall) {
Mehdi Amini44ede332015-07-09 02:09:04 +00002894 SDValue PtrOff =
2895 DAG.getNode(ISD::ADD, DL, getPointerTy(DAG.getDataLayout()), StackPtr,
2896 DAG.getIntPtrConstant(Offset, DL));
Justin Lebar9c375812016-07-15 18:27:10 +00002897 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo());
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002898 }
2899
Matthias Braun941a7052016-07-28 18:40:00 +00002900 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
2901 int FI = MFI.CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00002902 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002903 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(),
Justin Lebar9c375812016-07-15 18:27:10 +00002904 /* Alignment = */ 0, MachineMemOperand::MOVolatile);
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002905}
2906
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002907void MipsTargetLowering::
2908getOpndList(SmallVectorImpl<SDValue> &Ops,
Eugene Zelenko79220eae2017-08-03 22:12:30 +00002909 std::deque<std::pair<unsigned, SDValue>> &RegsToPass,
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002910 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
Sasa Stankovic7072a792014-10-01 08:22:21 +00002911 bool IsCallReloc, CallLoweringInfo &CLI, SDValue Callee,
2912 SDValue Chain) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002913 // Insert node "GP copy globalreg" before call to function.
2914 //
2915 // R_MIPS_CALL* operators (emitted when non-internal functions are called
2916 // in PIC mode) allow symbols to be resolved via lazy binding.
2917 // The lazy binding stub requires GP to point to the GOT.
Sasa Stankovic7072a792014-10-01 08:22:21 +00002918 // Note that we don't need GP to point to the GOT for indirect calls
2919 // (when R_MIPS_CALL* is not used for the call) because Mips linker generates
2920 // lazy binding stub for a function only when R_MIPS_CALL* are the only relocs
2921 // used for the function (that is, Mips linker doesn't generate lazy binding
2922 // stub for a function whose address is taken in the program).
2923 if (IsPICCall && !InternalLinkage && IsCallReloc) {
Eric Christopher96e72c62015-01-29 23:27:36 +00002924 unsigned GPReg = ABI.IsN64() ? Mips::GP_64 : Mips::GP;
2925 EVT Ty = ABI.IsN64() ? MVT::i64 : MVT::i32;
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002926 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty)));
2927 }
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002928
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002929 // Build a sequence of copy-to-reg nodes chained together with token
2930 // chain and flag operands which copy the outgoing args into registers.
2931 // The InFlag in necessary since all emitted instructions must be
2932 // stuck together.
2933 SDValue InFlag;
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002934
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002935 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2936 Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, RegsToPass[i].first,
2937 RegsToPass[i].second, InFlag);
2938 InFlag = Chain.getValue(1);
2939 }
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002940
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002941 // Add argument registers to the end of the list so that they are
2942 // known live into the call.
2943 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2944 Ops.push_back(CLI.DAG.getRegister(RegsToPass[i].first,
2945 RegsToPass[i].second.getValueType()));
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002946
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002947 // Add a register mask operand representing the call-preserved registers.
Eric Christopher96e72c62015-01-29 23:27:36 +00002948 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00002949 const uint32_t *Mask =
2950 TRI->getCallPreservedMask(CLI.DAG.getMachineFunction(), CLI.CallConv);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002951 assert(Mask && "Missing call preserved mask for calling convention");
Eric Christopher1c29a652014-07-18 22:55:25 +00002952 if (Subtarget.inMips16HardFloat()) {
Reed Kotler783c7942013-05-10 22:25:39 +00002953 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
Eugene Zelenko79220eae2017-08-03 22:12:30 +00002954 StringRef Sym = G->getGlobal()->getName();
Reed Kotler783c7942013-05-10 22:25:39 +00002955 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
Reed Kotler3230e722013-12-12 02:41:11 +00002956 if (F && F->hasFnAttribute("__Mips16RetHelper")) {
Reed Kotler783c7942013-05-10 22:25:39 +00002957 Mask = MipsRegisterInfo::getMips16RetHelperMask();
2958 }
2959 }
2960 }
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002961 Ops.push_back(CLI.DAG.getRegisterMask(Mask));
2962
2963 if (InFlag.getNode())
2964 Ops.push_back(InFlag);
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002965}
2966
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002967/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman624801e2009-01-26 03:15:54 +00002968/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002969SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00002970MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002971 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00002972 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002973 SDLoc DL = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00002974 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
2975 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
2976 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Akira Hatanakabeda2242012-07-31 18:46:41 +00002977 SDValue Chain = CLI.Chain;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002978 SDValue Callee = CLI.Callee;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002979 bool &IsTailCall = CLI.IsTailCall;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002980 CallingConv::ID CallConv = CLI.CallConv;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002981 bool IsVarArg = CLI.IsVarArg;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002982
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002983 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00002984 MachineFrameInfo &MFI = MF.getFrameInfo();
Eric Christopher96e72c62015-01-29 23:27:36 +00002985 const TargetFrameLowering *TFL = Subtarget.getFrameLowering();
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002986 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
Rafael Espindola9f1c1fe2016-06-27 12:48:21 +00002987 bool IsPIC = isPositionIndependent();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002988
2989 // Analyze operands of the call, assigning locations to each operand.
2990 SmallVector<CCValAssign, 16> ArgLocs;
Daniel Sanders41a64c42014-11-07 11:10:48 +00002991 MipsCCState CCInfo(
2992 CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs, *DAG.getContext(),
2993 MipsCCState::getSpecialCallingConvForCallee(Callee.getNode(), Subtarget));
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002994
2995 // Allocate the reserved argument area. It seems strange to do this from the
2996 // caller side but removing it breaks the frame size calculation.
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002997 CCInfo.AllocateStack(ABI.GetCalleeAllocdArgSizeInBytes(CallConv), 1);
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002998
Simon Dardis70f79252017-04-26 11:10:38 +00002999 const ExternalSymbolSDNode *ES =
3000 dyn_cast_or_null<const ExternalSymbolSDNode>(Callee.getNode());
3001 CCInfo.AnalyzeCallOperands(Outs, CC_Mips, CLI.getArgs(),
3002 ES ? ES->getSymbol() : nullptr);
Wesley Peck527da1b2010-11-23 03:31:01 +00003003
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003004 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka195a1e22011-06-08 17:39:33 +00003005 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka97ba7692012-07-26 23:27:01 +00003006
Simon Dardis1dcb9112016-11-20 21:23:08 +00003007 // Check if it's really possible to do a tail call. Restrict it to functions
3008 // that are part of this compilation unit.
3009 bool InternalLinkage = false;
3010 if (IsTailCall) {
Daniel Sanders23e98772014-11-02 16:09:29 +00003011 IsTailCall = isEligibleForTailCallOptimization(
3012 CCInfo, NextStackOffset, *MF.getInfo<MipsFunctionInfo>());
Simon Dardis1dcb9112016-11-20 21:23:08 +00003013 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3014 InternalLinkage = G->getGlobal()->hasInternalLinkage();
3015 IsTailCall &= (InternalLinkage || G->getGlobal()->hasLocalLinkage() ||
3016 G->getGlobal()->hasPrivateLinkage() ||
3017 G->getGlobal()->hasHiddenVisibility() ||
3018 G->getGlobal()->hasProtectedVisibility());
3019 }
3020 }
Peter Collingbourne081ffe22017-07-26 19:15:29 +00003021 if (!IsTailCall && CLI.CS && CLI.CS.isMustTailCall())
Reid Kleckner5772b772014-04-24 20:14:34 +00003022 report_fatal_error("failed to perform tail call elimination on a call "
3023 "site marked musttail");
3024
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003025 if (IsTailCall)
Akira Hatanaka90131ac2012-10-19 21:47:33 +00003026 ++NumTailCalls;
3027
Akira Hatanaka79738332011-09-19 20:26:02 +00003028 // Chain is the output chain of the last Load/Store or CopyToReg node.
3029 // ByValChain is the output chain of the last Memcpy node created for copying
3030 // byval arguments to the stack.
Akira Hatanaka9c962c02012-10-30 20:16:31 +00003031 unsigned StackAlignment = TFL->getStackAlignment();
Rui Ueyamada00f2f2016-01-14 21:06:47 +00003032 NextStackOffset = alignTo(NextStackOffset, StackAlignment);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003033 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, DL, true);
Akira Hatanaka90131ac2012-10-19 21:47:33 +00003034
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003035 if (!IsTailCall)
Serge Pavlovd526b132017-05-09 13:35:13 +00003036 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffset, 0, DL);
Akira Hatanakabeda2242012-07-31 18:46:41 +00003037
Mehdi Amini44ede332015-07-09 02:09:04 +00003038 SDValue StackPtr =
3039 DAG.getCopyFromReg(Chain, DL, ABI.IsN64() ? Mips::SP_64 : Mips::SP,
3040 getPointerTy(DAG.getDataLayout()));
Akira Hatanaka195a1e22011-06-08 17:39:33 +00003041
Eugene Zelenko79220eae2017-08-03 22:12:30 +00003042 std::deque<std::pair<unsigned, SDValue>> RegsToPass;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003043 SmallVector<SDValue, 8> MemOpChains;
Daniel Sanders23e98772014-11-02 16:09:29 +00003044
3045 CCInfo.rewindByValRegsInfo();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003046
3047 // Walk the register/memloc assignments, inserting copies/loads.
3048 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003049 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003050 CCValAssign &VA = ArgLocs[i];
Akira Hatanakab20a3252011-10-28 19:49:00 +00003051 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka19891f82011-11-12 02:34:50 +00003052 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Daniel Sandersc43cda82014-11-07 16:54:21 +00003053 bool UseUpperBits = false;
Akira Hatanaka19891f82011-11-12 02:34:50 +00003054
3055 // ByVal Arg.
3056 if (Flags.isByVal()) {
Daniel Sanders23e98772014-11-02 16:09:29 +00003057 unsigned FirstByValReg, LastByValReg;
3058 unsigned ByValIdx = CCInfo.getInRegsParamsProcessed();
3059 CCInfo.getInRegsParamInfo(ByValIdx, FirstByValReg, LastByValReg);
3060
Akira Hatanaka19891f82011-11-12 02:34:50 +00003061 assert(Flags.getByValSize() &&
3062 "ByVal args of size 0 should have been ignored by front-end.");
Daniel Sanders23e98772014-11-02 16:09:29 +00003063 assert(ByValIdx < CCInfo.getInRegsParamsCount());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003064 assert(!IsTailCall &&
Akira Hatanaka9c962c02012-10-30 20:16:31 +00003065 "Do not tail-call optimize if there is a byval argument.");
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003066 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003067 FirstByValReg, LastByValReg, Flags, Subtarget.isLittle(),
3068 VA);
Daniel Sanders23e98772014-11-02 16:09:29 +00003069 CCInfo.nextInRegsParam();
Akira Hatanaka19891f82011-11-12 02:34:50 +00003070 continue;
3071 }
Jia Liuf54f60f2012-02-28 07:46:26 +00003072
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003073 // Promote the value if needed.
3074 switch (VA.getLocInfo()) {
Daniel Sandersc43cda82014-11-07 16:54:21 +00003075 default:
3076 llvm_unreachable("Unknown loc info!");
Wesley Peck527da1b2010-11-23 03:31:01 +00003077 case CCValAssign::Full:
Akira Hatanakab20a3252011-10-28 19:49:00 +00003078 if (VA.isRegLoc()) {
3079 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
Akira Hatanaka3b7391d2013-03-05 22:20:28 +00003080 (ValVT == MVT::f64 && LocVT == MVT::i64) ||
3081 (ValVT == MVT::i64 && LocVT == MVT::f64))
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003082 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
Akira Hatanakab20a3252011-10-28 19:49:00 +00003083 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003084 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003085 Arg, DAG.getConstant(0, DL, MVT::i32));
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003086 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003087 Arg, DAG.getConstant(1, DL, MVT::i32));
Eric Christopher1c29a652014-07-18 22:55:25 +00003088 if (!Subtarget.isLittle())
Akira Hatanaka27916972011-04-15 19:52:08 +00003089 std::swap(Lo, Hi);
Jia Liuf54f60f2012-02-28 07:46:26 +00003090 unsigned LocRegLo = VA.getLocReg();
Akira Hatanaka61bbcce2011-09-23 00:58:33 +00003091 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
3092 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
3093 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00003094 continue;
Wesley Peck527da1b2010-11-23 03:31:01 +00003095 }
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00003096 }
3097 break;
Daniel Sanders23e98772014-11-02 16:09:29 +00003098 case CCValAssign::BCvt:
3099 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
3100 break;
Daniel Sandersc43cda82014-11-07 16:54:21 +00003101 case CCValAssign::SExtUpper:
3102 UseUpperBits = true;
Justin Bognerb03fd122016-08-17 05:10:15 +00003103 LLVM_FALLTHROUGH;
Chris Lattner52f16de2008-03-17 06:57:02 +00003104 case CCValAssign::SExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003105 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00003106 break;
Daniel Sandersc43cda82014-11-07 16:54:21 +00003107 case CCValAssign::ZExtUpper:
3108 UseUpperBits = true;
Justin Bognerb03fd122016-08-17 05:10:15 +00003109 LLVM_FALLTHROUGH;
Chris Lattner52f16de2008-03-17 06:57:02 +00003110 case CCValAssign::ZExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003111 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00003112 break;
Daniel Sandersc43cda82014-11-07 16:54:21 +00003113 case CCValAssign::AExtUpper:
3114 UseUpperBits = true;
Justin Bognerb03fd122016-08-17 05:10:15 +00003115 LLVM_FALLTHROUGH;
Chris Lattner52f16de2008-03-17 06:57:02 +00003116 case CCValAssign::AExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003117 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00003118 break;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003119 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003120
Daniel Sandersc43cda82014-11-07 16:54:21 +00003121 if (UseUpperBits) {
3122 unsigned ValSizeInBits = Outs[i].ArgVT.getSizeInBits();
3123 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
3124 Arg = DAG.getNode(
3125 ISD::SHL, DL, VA.getLocVT(), Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003126 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
Daniel Sandersc43cda82014-11-07 16:54:21 +00003127 }
3128
Wesley Peck527da1b2010-11-23 03:31:01 +00003129 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +00003130 // RegsToPass vector
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003131 if (VA.isRegLoc()) {
3132 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattner52f16de2008-03-17 06:57:02 +00003133 continue;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003134 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003135
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00003136 // Register can't get to this point...
Chris Lattner52f16de2008-03-17 06:57:02 +00003137 assert(VA.isMemLoc());
Wesley Peck527da1b2010-11-23 03:31:01 +00003138
Wesley Peck527da1b2010-11-23 03:31:01 +00003139 // emit ISD::STORE whichs stores the
Chris Lattner52f16de2008-03-17 06:57:02 +00003140 // parameter value to a stack Location
Akira Hatanaka9c962c02012-10-30 20:16:31 +00003141 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003142 Chain, Arg, DL, IsTailCall, DAG));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003143 }
3144
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003145 // Transform all store nodes into one single node because all store
3146 // nodes are independent of each other.
Wesley Peck527da1b2010-11-23 03:31:01 +00003147 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00003148 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003149
Bill Wendling24c79f22008-09-16 21:48:12 +00003150 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peck527da1b2010-11-23 03:31:01 +00003151 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
3152 // node so that legalize doesn't hack it.
Simon Dardisca74dd72017-01-27 11:36:52 +00003153
Akira Hatanakad6f1c582011-04-07 19:51:44 +00003154 SDValue CalleeLo;
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00003155 EVT Ty = Callee.getValueType();
Simon Dardis1dcb9112016-11-20 21:23:08 +00003156 bool GlobalOrExternal = false, IsCallReloc = false;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00003157
Simon Atanasyanf217c7b2017-07-15 07:14:25 +00003158 // The long-calls feature is ignored in case of PIC.
3159 // While we do not support -mshared / -mno-shared properly,
3160 // ignore long-calls in case of -mabicalls too.
Simon Atanasyanfb953922017-07-20 12:19:26 +00003161 if (!Subtarget.isABICalls() && !IsPIC) {
3162 // If the function should be called using "long call",
3163 // get its address into a register to prevent using
3164 // of the `jal` instruction for the direct call.
3165 if (auto *N = dyn_cast<ExternalSymbolSDNode>(Callee)) {
3166 if (Subtarget.useLongCalls())
3167 Callee = Subtarget.hasSym32()
3168 ? getAddrNonPIC(N, SDLoc(N), Ty, DAG)
3169 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG);
3170 } else if (auto *N = dyn_cast<GlobalAddressSDNode>(Callee)) {
3171 bool UseLongCalls = Subtarget.useLongCalls();
3172 // If the function has long-call/far/near attribute
3173 // it overrides command line switch pased to the backend.
3174 if (auto *F = dyn_cast<Function>(N->getGlobal())) {
3175 if (F->hasFnAttribute("long-call"))
3176 UseLongCalls = true;
3177 else if (F->hasFnAttribute("short-call"))
3178 UseLongCalls = false;
3179 }
3180 if (UseLongCalls)
3181 Callee = Subtarget.hasSym32()
3182 ? getAddrNonPIC(N, SDLoc(N), Ty, DAG)
3183 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG);
3184 }
Simon Atanasyanf217c7b2017-07-15 07:14:25 +00003185 }
3186
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00003187 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Simon Dardisca74dd72017-01-27 11:36:52 +00003188 if (IsPIC) {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00003189 const GlobalValue *Val = G->getGlobal();
3190 InternalLinkage = Val->hasInternalLinkage();
Akira Hatanakacf9a61b2012-12-13 03:17:29 +00003191
3192 if (InternalLinkage)
Eric Christopher96e72c62015-01-29 23:27:36 +00003193 Callee = getAddrLocal(G, DL, Ty, DAG, ABI.IsN32() || ABI.IsN64());
Sasa Stankovic7072a792014-10-01 08:22:21 +00003194 else if (LargeGOT) {
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00003195 Callee = getAddrGlobalLargeGOT(G, DL, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00003196 MipsII::MO_CALL_LO16, Chain,
3197 FuncInfo->callPtrInfo(Val));
Sasa Stankovic7072a792014-10-01 08:22:21 +00003198 IsCallReloc = true;
3199 } else {
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00003200 Callee = getAddrGlobal(G, DL, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00003201 FuncInfo->callPtrInfo(Val));
Sasa Stankovic7072a792014-10-01 08:22:21 +00003202 IsCallReloc = true;
3203 }
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00003204 } else
Mehdi Amini44ede332015-07-09 02:09:04 +00003205 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL,
3206 getPointerTy(DAG.getDataLayout()), 0,
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00003207 MipsII::MO_NO_FLAG);
Akira Hatanaka8e16aac2011-12-09 01:45:12 +00003208 GlobalOrExternal = true;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00003209 }
3210 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00003211 const char *Sym = S->getSymbol();
3212
Simon Dardisca74dd72017-01-27 11:36:52 +00003213 if (!IsPIC) // static
Mehdi Amini44ede332015-07-09 02:09:04 +00003214 Callee = DAG.getTargetExternalSymbol(
3215 Sym, getPointerTy(DAG.getDataLayout()), MipsII::MO_NO_FLAG);
Sasa Stankovic7072a792014-10-01 08:22:21 +00003216 else if (LargeGOT) {
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00003217 Callee = getAddrGlobalLargeGOT(S, DL, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00003218 MipsII::MO_CALL_LO16, Chain,
3219 FuncInfo->callPtrInfo(Sym));
Sasa Stankovic7072a792014-10-01 08:22:21 +00003220 IsCallReloc = true;
Simon Dardisca74dd72017-01-27 11:36:52 +00003221 } else { // PIC
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00003222 Callee = getAddrGlobal(S, DL, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00003223 FuncInfo->callPtrInfo(Sym));
Sasa Stankovic7072a792014-10-01 08:22:21 +00003224 IsCallReloc = true;
3225 }
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00003226
Akira Hatanaka8e16aac2011-12-09 01:45:12 +00003227 GlobalOrExternal = true;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00003228 }
3229
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00003230 SmallVector<SDValue, 8> Ops(1, Chain);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003231 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00003232
Simon Dardisca74dd72017-01-27 11:36:52 +00003233 getOpndList(Ops, RegsToPass, IsPIC, GlobalOrExternal, InternalLinkage,
Sasa Stankovic7072a792014-10-01 08:22:21 +00003234 IsCallReloc, CLI, Callee, Chain);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003235
Simon Dardis9a66bbe2016-09-21 09:43:40 +00003236 if (IsTailCall) {
3237 MF.getFrameInfo().setHasTailCall();
Craig Topper48d114b2014-04-26 18:35:24 +00003238 return DAG.getNode(MipsISD::TailCall, DL, MVT::Other, Ops);
Simon Dardis9a66bbe2016-09-21 09:43:40 +00003239 }
Akira Hatanaka90131ac2012-10-19 21:47:33 +00003240
Craig Topper48d114b2014-04-26 18:35:24 +00003241 Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, Ops);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003242 SDValue InFlag = Chain.getValue(1);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003243
Bruno Cardoso Lopes193e64c2010-01-30 18:32:07 +00003244 // Create the CALLSEQ_END node.
Akira Hatanaka97ba7692012-07-26 23:27:01 +00003245 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003246 DAG.getIntPtrConstant(0, DL, true), InFlag, DL);
Bruno Cardoso Lopes193e64c2010-01-30 18:32:07 +00003247 InFlag = Chain.getValue(1);
3248
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003249 // Handle result values, copying them out of physregs into vregs that we
3250 // return.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003251 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg, Ins, DL, DAG,
3252 InVals, CLI);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003253}
3254
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003255/// LowerCallResult - Lower the result values of a call into the
3256/// appropriate copies out of appropriate physical registers.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003257SDValue MipsTargetLowering::LowerCallResult(
3258 SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool IsVarArg,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003259 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
3260 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals,
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003261 TargetLowering::CallLoweringInfo &CLI) const {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003262 // Assign locations to each value returned by this call.
3263 SmallVector<CCValAssign, 16> RVLocs;
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003264 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
3265 *DAG.getContext());
Simon Dardis70f79252017-04-26 11:10:38 +00003266
3267 const ExternalSymbolSDNode *ES =
3268 dyn_cast_or_null<const ExternalSymbolSDNode>(CLI.Callee.getNode());
3269 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips, CLI.RetTy,
3270 ES ? ES->getSymbol() : nullptr);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003271
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003272 // Copy all of the result registers out of their specified physreg.
3273 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Daniel Sandersae275e32014-09-25 12:15:05 +00003274 CCValAssign &VA = RVLocs[i];
3275 assert(VA.isRegLoc() && "Can only return in registers!");
3276
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003277 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(),
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003278 RVLocs[i].getLocVT(), InFlag);
3279 Chain = Val.getValue(1);
3280 InFlag = Val.getValue(2);
3281
Daniel Sandersae275e32014-09-25 12:15:05 +00003282 if (VA.isUpperBitsInLoc()) {
3283 unsigned ValSizeInBits = Ins[i].ArgVT.getSizeInBits();
3284 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
3285 unsigned Shift =
3286 VA.getLocInfo() == CCValAssign::ZExtUpper ? ISD::SRL : ISD::SRA;
3287 Val = DAG.getNode(
3288 Shift, DL, VA.getLocVT(), Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003289 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
Daniel Sandersae275e32014-09-25 12:15:05 +00003290 }
3291
3292 switch (VA.getLocInfo()) {
3293 default:
3294 llvm_unreachable("Unknown loc info!");
3295 case CCValAssign::Full:
3296 break;
3297 case CCValAssign::BCvt:
3298 Val = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Val);
3299 break;
3300 case CCValAssign::AExt:
3301 case CCValAssign::AExtUpper:
3302 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
3303 break;
3304 case CCValAssign::ZExt:
3305 case CCValAssign::ZExtUpper:
3306 Val = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Val,
3307 DAG.getValueType(VA.getValVT()));
3308 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
3309 break;
3310 case CCValAssign::SExt:
3311 case CCValAssign::SExtUpper:
3312 Val = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Val,
3313 DAG.getValueType(VA.getValVT()));
3314 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
3315 break;
3316 }
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003317
3318 InVals.push_back(Val);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003319 }
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +00003320
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003321 return Chain;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003322}
3323
Daniel Sandersc43cda82014-11-07 16:54:21 +00003324static SDValue UnpackFromArgumentSlot(SDValue Val, const CCValAssign &VA,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003325 EVT ArgVT, const SDLoc &DL,
3326 SelectionDAG &DAG) {
Daniel Sandersc43cda82014-11-07 16:54:21 +00003327 MVT LocVT = VA.getLocVT();
3328 EVT ValVT = VA.getValVT();
3329
3330 // Shift into the upper bits if necessary.
3331 switch (VA.getLocInfo()) {
3332 default:
3333 break;
3334 case CCValAssign::AExtUpper:
3335 case CCValAssign::SExtUpper:
3336 case CCValAssign::ZExtUpper: {
3337 unsigned ValSizeInBits = ArgVT.getSizeInBits();
3338 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
3339 unsigned Opcode =
3340 VA.getLocInfo() == CCValAssign::ZExtUpper ? ISD::SRL : ISD::SRA;
3341 Val = DAG.getNode(
3342 Opcode, DL, VA.getLocVT(), Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003343 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
Daniel Sandersc43cda82014-11-07 16:54:21 +00003344 break;
3345 }
3346 }
3347
3348 // If this is an value smaller than the argument slot size (32-bit for O32,
3349 // 64-bit for N32/N64), it has been promoted in some way to the argument slot
3350 // size. Extract the value and insert any appropriate assertions regarding
3351 // sign/zero extension.
3352 switch (VA.getLocInfo()) {
3353 default:
3354 llvm_unreachable("Unknown loc info!");
3355 case CCValAssign::Full:
3356 break;
3357 case CCValAssign::AExtUpper:
3358 case CCValAssign::AExt:
3359 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
3360 break;
3361 case CCValAssign::SExtUpper:
3362 case CCValAssign::SExt:
3363 Val = DAG.getNode(ISD::AssertSext, DL, LocVT, Val, DAG.getValueType(ValVT));
3364 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
3365 break;
3366 case CCValAssign::ZExtUpper:
3367 case CCValAssign::ZExt:
3368 Val = DAG.getNode(ISD::AssertZext, DL, LocVT, Val, DAG.getValueType(ValVT));
3369 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
3370 break;
3371 case CCValAssign::BCvt:
3372 Val = DAG.getNode(ISD::BITCAST, DL, ValVT, Val);
3373 break;
3374 }
3375
3376 return Val;
3377}
3378
Akira Hatanakae2489122011-04-15 21:51:11 +00003379//===----------------------------------------------------------------------===//
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003380// Formal Arguments Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00003381//===----------------------------------------------------------------------===//
Wesley Peck527da1b2010-11-23 03:31:01 +00003382/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003383/// and generate load operations for arguments places on the stack.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003384SDValue MipsTargetLowering::LowerFormalArguments(
3385 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
3386 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
3387 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Bruno Cardoso Lopesa01ede22008-08-04 07:12:52 +00003388 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00003389 MachineFrameInfo &MFI = MF.getFrameInfo();
Bruno Cardoso Lopes14033fb2007-08-28 05:08:16 +00003390 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00003391
Dan Gohman31ae5862010-04-17 14:41:14 +00003392 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003393
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003394 // Used with vargs to acumulate store chains.
3395 std::vector<SDValue> OutChains;
3396
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003397 // Assign locations to all of the incoming arguments.
3398 SmallVector<CCValAssign, 16> ArgLocs;
Daniel Sanders23e98772014-11-02 16:09:29 +00003399 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
3400 *DAG.getContext());
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003401 CCInfo.AllocateStack(ABI.GetCalleeAllocdArgSizeInBytes(CallConv), 1);
Vasileios Kalintiris43dff0c2015-10-26 12:38:43 +00003402 const Function *Func = DAG.getMachineFunction().getFunction();
3403 Function::const_arg_iterator FuncArg = Func->arg_begin();
3404
Vasileios Kalintiris165121f2015-10-26 14:24:30 +00003405 if (Func->hasFnAttribute("interrupt") && !Func->arg_empty())
3406 report_fatal_error(
3407 "Functions with the interrupt attribute cannot have arguments!");
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00003408
Daniel Sandersb70e27c2014-11-06 16:36:30 +00003409 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips_FixedArg);
Akira Hatanaka4866fe12012-10-30 19:37:25 +00003410 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(),
Daniel Sanders23e98772014-11-02 16:09:29 +00003411 CCInfo.getInRegsParamsCount() > 0);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00003412
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00003413 unsigned CurArgIdx = 0;
Daniel Sanders23e98772014-11-02 16:09:29 +00003414 CCInfo.rewindByValRegsInfo();
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003415
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00003416 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003417 CCValAssign &VA = ArgLocs[i];
Andrew Trick05938a52015-02-16 18:10:47 +00003418 if (Ins[i].isOrigArg()) {
3419 std::advance(FuncArg, Ins[i].getOrigArgIndex() - CurArgIdx);
3420 CurArgIdx = Ins[i].getOrigArgIndex();
3421 }
Akira Hatanaka104b7e32011-10-28 19:55:48 +00003422 EVT ValVT = VA.getValVT();
Akira Hatanakafb9bae32011-11-12 02:29:58 +00003423 ISD::ArgFlagsTy Flags = Ins[i].Flags;
3424 bool IsRegLoc = VA.isRegLoc();
3425
3426 if (Flags.isByVal()) {
Andrew Trick05938a52015-02-16 18:10:47 +00003427 assert(Ins[i].isOrigArg() && "Byval arguments cannot be implicit");
Daniel Sanders23e98772014-11-02 16:09:29 +00003428 unsigned FirstByValReg, LastByValReg;
3429 unsigned ByValIdx = CCInfo.getInRegsParamsProcessed();
3430 CCInfo.getInRegsParamInfo(ByValIdx, FirstByValReg, LastByValReg);
3431
Akira Hatanakafb9bae32011-11-12 02:29:58 +00003432 assert(Flags.getByValSize() &&
3433 "ByVal args of size 0 should have been ignored by front-end.");
Daniel Sanders23e98772014-11-02 16:09:29 +00003434 assert(ByValIdx < CCInfo.getInRegsParamsCount());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003435 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003436 FirstByValReg, LastByValReg, VA, CCInfo);
Daniel Sanders23e98772014-11-02 16:09:29 +00003437 CCInfo.nextInRegsParam();
Akira Hatanakafb9bae32011-11-12 02:29:58 +00003438 continue;
3439 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003440
3441 // Arguments stored on registers
Akira Hatanakafb9bae32011-11-12 02:29:58 +00003442 if (IsRegLoc) {
Akira Hatanaka7d822522013-10-28 21:21:36 +00003443 MVT RegVT = VA.getLocVT();
Akira Hatanakacb4a1a82011-05-24 00:23:52 +00003444 unsigned ArgReg = VA.getLocReg();
Akira Hatanaka7d822522013-10-28 21:21:36 +00003445 const TargetRegisterClass *RC = getRegClassFor(RegVT);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003446
Wesley Peck527da1b2010-11-23 03:31:01 +00003447 // Transform the arguments stored on
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003448 // physical registers into virtual ones
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003449 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC);
3450 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
Wesley Peck527da1b2010-11-23 03:31:01 +00003451
Daniel Sandersc43cda82014-11-07 16:54:21 +00003452 ArgValue = UnpackFromArgumentSlot(ArgValue, VA, Ins[i].ArgVT, DL, DAG);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00003453
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003454 // Handle floating point arguments passed in integer registers and
3455 // long double arguments passed in floating point registers.
Akira Hatanaka104b7e32011-10-28 19:55:48 +00003456 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003457 (RegVT == MVT::i64 && ValVT == MVT::f64) ||
3458 (RegVT == MVT::f64 && ValVT == MVT::i64))
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003459 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
Eric Christopher96e72c62015-01-29 23:27:36 +00003460 else if (ABI.IsO32() && RegVT == MVT::i32 &&
Eric Christopherbf33a3c2014-07-02 23:18:40 +00003461 ValVT == MVT::f64) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003462 unsigned Reg2 = addLiveIn(DAG.getMachineFunction(),
Akira Hatanaka104b7e32011-10-28 19:55:48 +00003463 getNextIntArgReg(ArgReg), RC);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003464 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT);
Eric Christopher1c29a652014-07-18 22:55:25 +00003465 if (!Subtarget.isLittle())
Akira Hatanaka104b7e32011-10-28 19:55:48 +00003466 std::swap(ArgValue, ArgValue2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003467 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64,
Akira Hatanaka104b7e32011-10-28 19:55:48 +00003468 ArgValue, ArgValue2);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00003469 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003470
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003471 InVals.push_back(ArgValue);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003472 } else { // VA.isRegLoc()
Daniel Sandersc43cda82014-11-07 16:54:21 +00003473 MVT LocVT = VA.getLocVT();
3474
Eric Christopher96e72c62015-01-29 23:27:36 +00003475 if (ABI.IsO32()) {
Daniel Sandersc43cda82014-11-07 16:54:21 +00003476 // We ought to be able to use LocVT directly but O32 sets it to i32
3477 // when allocating floating point values to integer registers.
3478 // This shouldn't influence how we load the value into registers unless
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00003479 // we are targeting softfloat.
Eric Christophere8ae3e32015-05-07 23:10:21 +00003480 if (VA.getValVT().isFloatingPoint() && !Subtarget.useSoftFloat())
Daniel Sandersc43cda82014-11-07 16:54:21 +00003481 LocVT = VA.getValVT();
3482 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003483
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003484 // sanity check
3485 assert(VA.isMemLoc());
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003486
Wesley Peck527da1b2010-11-23 03:31:01 +00003487 // The stack pointer offset is relative to the caller stack frame.
Matthias Braun941a7052016-07-28 18:40:00 +00003488 int FI = MFI.CreateFixedObject(LocVT.getSizeInBits() / 8,
3489 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003490
3491 // Create load nodes to retrieve arguments from the stack
Mehdi Amini44ede332015-07-09 02:09:04 +00003492 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Alex Lorenze40c8a22015-08-11 23:09:45 +00003493 SDValue ArgValue = DAG.getLoad(
3494 LocVT, DL, Chain, FIN,
Justin Lebar9c375812016-07-15 18:27:10 +00003495 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI));
Daniel Sandersc43cda82014-11-07 16:54:21 +00003496 OutChains.push_back(ArgValue.getValue(1));
3497
3498 ArgValue = UnpackFromArgumentSlot(ArgValue, VA, Ins[i].ArgVT, DL, DAG);
3499
3500 InVals.push_back(ArgValue);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003501 }
Reid Kleckner7a59e082014-05-12 22:01:27 +00003502 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003503
Reid Kleckner7a59e082014-05-12 22:01:27 +00003504 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Reid Kleckner79418562014-05-09 22:32:13 +00003505 // The mips ABIs for returning structs by value requires that we copy
3506 // the sret argument into $v0 for the return. Save the argument into
3507 // a virtual register so that we can access it from the return points.
Reid Kleckner7a59e082014-05-12 22:01:27 +00003508 if (Ins[i].Flags.isSRet()) {
Reid Kleckner79418562014-05-09 22:32:13 +00003509 unsigned Reg = MipsFI->getSRetReturnReg();
3510 if (!Reg) {
3511 Reg = MF.getRegInfo().createVirtualRegister(
Eric Christopher96e72c62015-01-29 23:27:36 +00003512 getRegClassFor(ABI.IsN64() ? MVT::i64 : MVT::i32));
Reid Kleckner79418562014-05-09 22:32:13 +00003513 MipsFI->setSRetReturnReg(Reg);
3514 }
Reid Kleckner7a59e082014-05-12 22:01:27 +00003515 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[i]);
Reid Kleckner79418562014-05-09 22:32:13 +00003516 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
Reid Kleckner7a59e082014-05-12 22:01:27 +00003517 break;
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003518 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003519 }
3520
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003521 if (IsVarArg)
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003522 writeVarArgRegs(OutChains, Chain, DL, DAG, CCInfo);
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003523
Wesley Peck527da1b2010-11-23 03:31:01 +00003524 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003525 // the size of Ins and InVals. This only happens when on varg functions
3526 if (!OutChains.empty()) {
3527 OutChains.push_back(Chain);
Craig Topper48d114b2014-04-26 18:35:24 +00003528 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003529 }
3530
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003531 return Chain;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003532}
3533
Akira Hatanakae2489122011-04-15 21:51:11 +00003534//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003535// Return Value Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00003536//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003537
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003538bool
3539MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003540 MachineFunction &MF, bool IsVarArg,
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003541 const SmallVectorImpl<ISD::OutputArg> &Outs,
3542 LLVMContext &Context) const {
3543 SmallVector<CCValAssign, 16> RVLocs;
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003544 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003545 return CCInfo.CheckReturn(Outs, RetCC_Mips);
3546}
3547
Petar Jovanovic5b436222015-03-23 12:28:13 +00003548bool
3549MipsTargetLowering::shouldSignExtendTypeInLibCall(EVT Type, bool IsSigned) const {
Eric Christophere8ae3e32015-05-07 23:10:21 +00003550 if (Subtarget.hasMips3() && Subtarget.useSoftFloat()) {
Petar Jovanovic5b436222015-03-23 12:28:13 +00003551 if (Type == MVT::i32)
3552 return true;
3553 }
3554 return IsSigned;
3555}
3556
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003557SDValue
Vasileios Kalintiris43dff0c2015-10-26 12:38:43 +00003558MipsTargetLowering::LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003559 const SDLoc &DL,
3560 SelectionDAG &DAG) const {
Vasileios Kalintiris43dff0c2015-10-26 12:38:43 +00003561 MachineFunction &MF = DAG.getMachineFunction();
3562 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3563
3564 MipsFI->setISR();
3565
3566 return DAG.getNode(MipsISD::ERet, DL, MVT::Other, RetOps);
3567}
3568
3569SDValue
3570MipsTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
3571 bool IsVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003572 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003573 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003574 const SDLoc &DL, SelectionDAG &DAG) const {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003575 // CCValAssign - represent the assignment of
3576 // the return value to a location
3577 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003578 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003579
3580 // CCState - Info about the registers and stack slot.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003581 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, *DAG.getContext());
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003582
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003583 // Analyze return values.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003584 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003585
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003586 SDValue Flag;
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003587 SmallVector<SDValue, 4> RetOps(1, Chain);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003588
3589 // Copy the result values into the output registers.
3590 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003591 SDValue Val = OutVals[i];
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003592 CCValAssign &VA = RVLocs[i];
3593 assert(VA.isRegLoc() && "Can only return in registers!");
Daniel Sandersae275e32014-09-25 12:15:05 +00003594 bool UseUpperBits = false;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003595
Daniel Sandersae275e32014-09-25 12:15:05 +00003596 switch (VA.getLocInfo()) {
3597 default:
3598 llvm_unreachable("Unknown loc info!");
3599 case CCValAssign::Full:
3600 break;
3601 case CCValAssign::BCvt:
3602 Val = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Val);
3603 break;
3604 case CCValAssign::AExtUpper:
3605 UseUpperBits = true;
Justin Bognerb03fd122016-08-17 05:10:15 +00003606 LLVM_FALLTHROUGH;
Daniel Sandersae275e32014-09-25 12:15:05 +00003607 case CCValAssign::AExt:
3608 Val = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Val);
3609 break;
3610 case CCValAssign::ZExtUpper:
3611 UseUpperBits = true;
Justin Bognerb03fd122016-08-17 05:10:15 +00003612 LLVM_FALLTHROUGH;
Daniel Sandersae275e32014-09-25 12:15:05 +00003613 case CCValAssign::ZExt:
3614 Val = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Val);
3615 break;
3616 case CCValAssign::SExtUpper:
3617 UseUpperBits = true;
Justin Bognerb03fd122016-08-17 05:10:15 +00003618 LLVM_FALLTHROUGH;
Daniel Sandersae275e32014-09-25 12:15:05 +00003619 case CCValAssign::SExt:
3620 Val = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Val);
3621 break;
3622 }
3623
3624 if (UseUpperBits) {
3625 unsigned ValSizeInBits = Outs[i].ArgVT.getSizeInBits();
3626 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
3627 Val = DAG.getNode(
3628 ISD::SHL, DL, VA.getLocVT(), Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003629 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
Daniel Sandersae275e32014-09-25 12:15:05 +00003630 }
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003631
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003632 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003633
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003634 // Guarantee that all emitted copies are stuck together with flags.
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003635 Flag = Chain.getValue(1);
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003636 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003637 }
3638
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003639 // The mips ABIs for returning structs by value requires that we copy
3640 // the sret argument into $v0 for the return. We saved the argument into
3641 // a virtual register in the entry block, so now we copy the value out
3642 // and into $v0.
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003643 if (MF.getFunction()->hasStructRetAttr()) {
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003644 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3645 unsigned Reg = MipsFI->getSRetReturnReg();
3646
Wesley Peck527da1b2010-11-23 03:31:01 +00003647 if (!Reg)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003648 llvm_unreachable("sret virtual register not created in the entry block");
Mehdi Amini44ede332015-07-09 02:09:04 +00003649 SDValue Val =
3650 DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(DAG.getDataLayout()));
Eric Christopher96e72c62015-01-29 23:27:36 +00003651 unsigned V0 = ABI.IsN64() ? Mips::V0_64 : Mips::V0;
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003652
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003653 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003654 Flag = Chain.getValue(1);
Mehdi Amini44ede332015-07-09 02:09:04 +00003655 RetOps.push_back(DAG.getRegister(V0, getPointerTy(DAG.getDataLayout())));
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003656 }
3657
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003658 RetOps[0] = Chain; // Update chain.
Akira Hatanakaefff7b72012-07-10 00:19:06 +00003659
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003660 // Add the flag if we have it.
3661 if (Flag.getNode())
3662 RetOps.push_back(Flag);
3663
Vasileios Kalintiris43dff0c2015-10-26 12:38:43 +00003664 // ISRs must use "eret".
3665 if (DAG.getMachineFunction().getFunction()->hasFnAttribute("interrupt"))
3666 return LowerInterruptReturn(RetOps, DL, DAG);
3667
3668 // Standard return on Mips is a "jr $ra"
Craig Topper48d114b2014-04-26 18:35:24 +00003669 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, RetOps);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003670}
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003671
Akira Hatanakae2489122011-04-15 21:51:11 +00003672//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003673// Mips Inline Assembly Support
Akira Hatanakae2489122011-04-15 21:51:11 +00003674//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003675
3676/// getConstraintType - Given a constraint letter, return the type of
3677/// constraint it is for this target.
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003678MipsTargetLowering::ConstraintType
3679MipsTargetLowering::getConstraintType(StringRef Constraint) const {
Daniel Sanders8b59af12013-11-12 12:56:01 +00003680 // Mips specific constraints
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003681 // GCC config/mips/constraints.md
3682 //
Wesley Peck527da1b2010-11-23 03:31:01 +00003683 // 'd' : An address register. Equivalent to r
3684 // unless generating MIPS16 code.
3685 // 'y' : Equivalent to r; retained for
3686 // backwards compatibility.
Eric Christophere3c494d2012-05-07 06:25:10 +00003687 // 'c' : A register suitable for use in an indirect
3688 // jump. This will always be $25 for -mabicalls.
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003689 // 'l' : The lo register. 1 word storage.
3690 // 'x' : The hilo register pair. Double word storage.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003691 if (Constraint.size() == 1) {
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003692 switch (Constraint[0]) {
3693 default : break;
Wesley Peck527da1b2010-11-23 03:31:01 +00003694 case 'd':
3695 case 'y':
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003696 case 'f':
Eric Christophere3c494d2012-05-07 06:25:10 +00003697 case 'c':
Eric Christopher9c492e62012-05-07 06:25:15 +00003698 case 'l':
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003699 case 'x':
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003700 return C_RegisterClass;
Jack Carter0e149b02013-03-04 21:33:15 +00003701 case 'R':
3702 return C_Memory;
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003703 }
3704 }
Daniel Sandersa73d8fe2015-03-24 11:26:34 +00003705
3706 if (Constraint == "ZC")
3707 return C_Memory;
3708
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003709 return TargetLowering::getConstraintType(Constraint);
3710}
3711
John Thompsone8360b72010-10-29 17:29:13 +00003712/// Examine constraint type and operand type and determine a weight value.
3713/// This object must already have been set up with the operand type
3714/// and the current alternative constraint selected.
3715TargetLowering::ConstraintWeight
3716MipsTargetLowering::getSingleConstraintMatchWeight(
3717 AsmOperandInfo &info, const char *constraint) const {
3718 ConstraintWeight weight = CW_Invalid;
3719 Value *CallOperandVal = info.CallOperandVal;
3720 // If we don't have a value, we can't do a match,
3721 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +00003722 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +00003723 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +00003724 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +00003725 // Look at the constraint type.
3726 switch (*constraint) {
3727 default:
3728 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
3729 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00003730 case 'd':
3731 case 'y':
John Thompsone8360b72010-10-29 17:29:13 +00003732 if (type->isIntegerTy())
3733 weight = CW_Register;
3734 break;
Daniel Sanders8b59af12013-11-12 12:56:01 +00003735 case 'f': // FPU or MSA register
Eric Christopher1c29a652014-07-18 22:55:25 +00003736 if (Subtarget.hasMSA() && type->isVectorTy() &&
Daniel Sanders8b59af12013-11-12 12:56:01 +00003737 cast<VectorType>(type)->getBitWidth() == 128)
3738 weight = CW_Register;
3739 else if (type->isFloatTy())
John Thompsone8360b72010-10-29 17:29:13 +00003740 weight = CW_Register;
3741 break;
Eric Christophere3c494d2012-05-07 06:25:10 +00003742 case 'c': // $25 for indirect jumps
Eric Christopher9c492e62012-05-07 06:25:15 +00003743 case 'l': // lo register
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003744 case 'x': // hilo register pair
Daniel Sanders8b59af12013-11-12 12:56:01 +00003745 if (type->isIntegerTy())
Eric Christophere3c494d2012-05-07 06:25:10 +00003746 weight = CW_SpecificReg;
Daniel Sanders8b59af12013-11-12 12:56:01 +00003747 break;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003748 case 'I': // signed 16 bit immediate
Eric Christopher7201e1b2012-05-07 03:13:42 +00003749 case 'J': // integer zero
Eric Christopher3ff88a02012-05-07 05:46:29 +00003750 case 'K': // unsigned 16 bit immediate
Eric Christopher1109b342012-05-07 05:46:37 +00003751 case 'L': // signed 32 bit immediate where lower 16 bits are 0
Eric Christophere07aa432012-05-07 05:46:43 +00003752 case 'N': // immediate in the range of -65535 to -1 (inclusive)
Eric Christopher470578a2012-05-07 05:46:48 +00003753 case 'O': // signed 15 bit immediate (+- 16383)
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003754 case 'P': // immediate in the range of 65535 to 1 (inclusive)
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003755 if (isa<ConstantInt>(CallOperandVal))
3756 weight = CW_Constant;
3757 break;
Jack Carter0e149b02013-03-04 21:33:15 +00003758 case 'R':
3759 weight = CW_Memory;
3760 break;
John Thompsone8360b72010-10-29 17:29:13 +00003761 }
3762 return weight;
3763}
3764
Akira Hatanaka7473b472013-08-14 00:21:25 +00003765/// This is a helper function to parse a physical register string and split it
3766/// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag
3767/// that is returned indicates whether parsing was successful. The second flag
3768/// is true if the numeric part exists.
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003769static std::pair<bool, bool> parsePhysicalReg(StringRef C, StringRef &Prefix,
3770 unsigned long long &Reg) {
Akira Hatanaka7473b472013-08-14 00:21:25 +00003771 if (C.front() != '{' || C.back() != '}')
3772 return std::make_pair(false, false);
3773
3774 // Search for the first numeric character.
3775 StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1;
Craig Topper2241dfd2015-11-23 07:19:06 +00003776 I = std::find_if(B, E, isdigit);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003777
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003778 Prefix = StringRef(B, I - B);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003779
3780 // The second flag is set to false if no numeric characters were found.
3781 if (I == E)
3782 return std::make_pair(true, false);
3783
3784 // Parse the numeric characters.
3785 return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg),
3786 true);
3787}
3788
3789std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering::
Craig Topper6dc4a8bc2014-08-30 16:48:02 +00003790parseRegForInlineAsmConstraint(StringRef C, MVT VT) const {
Eric Christopherd9134482014-08-04 21:25:23 +00003791 const TargetRegisterInfo *TRI =
Eric Christopher96e72c62015-01-29 23:27:36 +00003792 Subtarget.getRegisterInfo();
Akira Hatanaka7473b472013-08-14 00:21:25 +00003793 const TargetRegisterClass *RC;
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003794 StringRef Prefix;
Akira Hatanaka7473b472013-08-14 00:21:25 +00003795 unsigned long long Reg;
3796
3797 std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg);
3798
3799 if (!R.first)
Craig Topper062a2ba2014-04-25 05:30:21 +00003800 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003801
3802 if ((Prefix == "hi" || Prefix == "lo")) { // Parse hi/lo.
3803 // No numeric characters follow "hi" or "lo".
3804 if (R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003805 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003806
3807 RC = TRI->getRegClass(Prefix == "hi" ?
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00003808 Mips::HI32RegClassID : Mips::LO32RegClassID);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003809 return std::make_pair(*(RC->begin()), RC);
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003810 } else if (Prefix.startswith("$msa")) {
Daniel Sanders8b59af12013-11-12 12:56:01 +00003811 // Parse $msa(ir|csr|access|save|modify|request|map|unmap)
3812
3813 // No numeric characters follow the name.
3814 if (R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003815 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003816
3817 Reg = StringSwitch<unsigned long long>(Prefix)
3818 .Case("$msair", Mips::MSAIR)
3819 .Case("$msacsr", Mips::MSACSR)
3820 .Case("$msaaccess", Mips::MSAAccess)
3821 .Case("$msasave", Mips::MSASave)
3822 .Case("$msamodify", Mips::MSAModify)
3823 .Case("$msarequest", Mips::MSARequest)
3824 .Case("$msamap", Mips::MSAMap)
3825 .Case("$msaunmap", Mips::MSAUnmap)
3826 .Default(0);
3827
3828 if (!Reg)
Craig Topper062a2ba2014-04-25 05:30:21 +00003829 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003830
3831 RC = TRI->getRegClass(Mips::MSACtrlRegClassID);
3832 return std::make_pair(Reg, RC);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003833 }
3834
3835 if (!R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003836 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003837
3838 if (Prefix == "$f") { // Parse $f0-$f31.
3839 // If the size of FP registers is 64-bit or Reg is an even number, select
3840 // the 64-bit register class. Otherwise, select the 32-bit register class.
3841 if (VT == MVT::Other)
Eric Christopher1c29a652014-07-18 22:55:25 +00003842 VT = (Subtarget.isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32;
Akira Hatanaka7473b472013-08-14 00:21:25 +00003843
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003844 RC = getRegClassFor(VT);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003845
3846 if (RC == &Mips::AFGR64RegClass) {
3847 assert(Reg % 2 == 0);
3848 Reg >>= 1;
3849 }
Daniel Sanders8b59af12013-11-12 12:56:01 +00003850 } else if (Prefix == "$fcc") // Parse $fcc0-$fcc7.
Akira Hatanaka7473b472013-08-14 00:21:25 +00003851 RC = TRI->getRegClass(Mips::FCCRegClassID);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003852 else if (Prefix == "$w") { // Parse $w0-$w31.
3853 RC = getRegClassFor((VT == MVT::Other) ? MVT::v16i8 : VT);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003854 } else { // Parse $0-$31.
3855 assert(Prefix == "$");
3856 RC = getRegClassFor((VT == MVT::Other) ? MVT::i32 : VT);
3857 }
3858
3859 assert(Reg < RC->getNumRegs());
3860 return std::make_pair(*(RC->begin() + Reg), RC);
3861}
3862
Eric Christophereaf77dc2011-06-29 19:33:04 +00003863/// Given a register class constraint, like 'r', if this corresponds directly
3864/// to an LLVM register class, return a register of 0 and the register class
3865/// pointer.
Eric Christopher11e4df72015-02-26 22:38:43 +00003866std::pair<unsigned, const TargetRegisterClass *>
3867MipsTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003868 StringRef Constraint,
Eric Christopher11e4df72015-02-26 22:38:43 +00003869 MVT VT) const {
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003870 if (Constraint.size() == 1) {
3871 switch (Constraint[0]) {
Eric Christopher9519c082011-06-29 19:04:31 +00003872 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
3873 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003874 case 'r':
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003875 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
Eric Christopher1c29a652014-07-18 22:55:25 +00003876 if (Subtarget.inMips16Mode())
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003877 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003878 return std::make_pair(0U, &Mips::GPR32RegClass);
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003879 }
Eric Christopher1c29a652014-07-18 22:55:25 +00003880 if (VT == MVT::i64 && !Subtarget.isGP64bit())
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003881 return std::make_pair(0U, &Mips::GPR32RegClass);
Eric Christopher1c29a652014-07-18 22:55:25 +00003882 if (VT == MVT::i64 && Subtarget.isGP64bit())
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003883 return std::make_pair(0U, &Mips::GPR64RegClass);
Eric Christopher58daf042012-05-07 03:13:22 +00003884 // This will generate an error message
Craig Topper062a2ba2014-04-25 05:30:21 +00003885 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003886 case 'f': // FPU or MSA register
3887 if (VT == MVT::v16i8)
3888 return std::make_pair(0U, &Mips::MSA128BRegClass);
3889 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
3890 return std::make_pair(0U, &Mips::MSA128HRegClass);
3891 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
3892 return std::make_pair(0U, &Mips::MSA128WRegClass);
3893 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
3894 return std::make_pair(0U, &Mips::MSA128DRegClass);
3895 else if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00003896 return std::make_pair(0U, &Mips::FGR32RegClass);
Eric Christopher1c29a652014-07-18 22:55:25 +00003897 else if ((VT == MVT::f64) && (!Subtarget.isSingleFloat())) {
3898 if (Subtarget.isFP64bit())
Craig Topperc7242e02012-04-20 07:30:17 +00003899 return std::make_pair(0U, &Mips::FGR64RegClass);
3900 return std::make_pair(0U, &Mips::AFGR64RegClass);
Akira Hatanakac669d7a2012-01-04 02:45:01 +00003901 }
Eric Christophere3c494d2012-05-07 06:25:10 +00003902 break;
3903 case 'c': // register suitable for indirect jump
3904 if (VT == MVT::i32)
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003905 return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass);
Eric Christophere3c494d2012-05-07 06:25:10 +00003906 assert(VT == MVT::i64 && "Unexpected type.");
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003907 return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass);
Eric Christopher9c492e62012-05-07 06:25:15 +00003908 case 'l': // register suitable for indirect jump
3909 if (VT == MVT::i32)
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00003910 return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass);
3911 return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass);
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003912 case 'x': // register suitable for indirect jump
3913 // Fixme: Not triggering the use of both hi and low
3914 // This will generate an error message
Craig Topper062a2ba2014-04-25 05:30:21 +00003915 return std::make_pair(0U, nullptr);
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003916 }
3917 }
Akira Hatanaka7473b472013-08-14 00:21:25 +00003918
3919 std::pair<unsigned, const TargetRegisterClass *> R;
3920 R = parseRegForInlineAsmConstraint(Constraint, VT);
3921
3922 if (R.second)
3923 return R;
3924
Eric Christopher11e4df72015-02-26 22:38:43 +00003925 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003926}
3927
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003928/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3929/// vector. If it is invalid, don't add anything to Ops.
3930void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3931 std::string &Constraint,
3932 std::vector<SDValue>&Ops,
3933 SelectionDAG &DAG) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003934 SDLoc DL(Op);
Craig Topper062a2ba2014-04-25 05:30:21 +00003935 SDValue Result;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003936
3937 // Only support length 1 constraints for now.
3938 if (Constraint.length() > 1) return;
3939
3940 char ConstraintLetter = Constraint[0];
3941 switch (ConstraintLetter) {
3942 default: break; // This will fall through to the generic implementation
3943 case 'I': // Signed 16 bit constant
3944 // If this fails, the parent routine will give an error
3945 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3946 EVT Type = Op.getValueType();
3947 int64_t Val = C->getSExtValue();
3948 if (isInt<16>(Val)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003949 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003950 break;
3951 }
3952 }
3953 return;
Eric Christopher7201e1b2012-05-07 03:13:42 +00003954 case 'J': // integer zero
3955 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3956 EVT Type = Op.getValueType();
3957 int64_t Val = C->getZExtValue();
3958 if (Val == 0) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003959 Result = DAG.getTargetConstant(0, DL, Type);
Eric Christopher7201e1b2012-05-07 03:13:42 +00003960 break;
3961 }
3962 }
3963 return;
Eric Christopher3ff88a02012-05-07 05:46:29 +00003964 case 'K': // unsigned 16 bit immediate
3965 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3966 EVT Type = Op.getValueType();
3967 uint64_t Val = (uint64_t)C->getZExtValue();
3968 if (isUInt<16>(Val)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003969 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopher3ff88a02012-05-07 05:46:29 +00003970 break;
3971 }
3972 }
3973 return;
Eric Christopher1109b342012-05-07 05:46:37 +00003974 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3975 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3976 EVT Type = Op.getValueType();
3977 int64_t Val = C->getSExtValue();
3978 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003979 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopher1109b342012-05-07 05:46:37 +00003980 break;
3981 }
3982 }
3983 return;
Eric Christophere07aa432012-05-07 05:46:43 +00003984 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3985 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3986 EVT Type = Op.getValueType();
3987 int64_t Val = C->getSExtValue();
3988 if ((Val >= -65535) && (Val <= -1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003989 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christophere07aa432012-05-07 05:46:43 +00003990 break;
3991 }
3992 }
3993 return;
Eric Christopher470578a2012-05-07 05:46:48 +00003994 case 'O': // signed 15 bit immediate
3995 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3996 EVT Type = Op.getValueType();
3997 int64_t Val = C->getSExtValue();
3998 if ((isInt<15>(Val))) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003999 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopher470578a2012-05-07 05:46:48 +00004000 break;
4001 }
4002 }
4003 return;
Eric Christopherc18ae4a2012-05-07 06:25:02 +00004004 case 'P': // immediate in the range of 1 to 65535 (inclusive)
4005 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4006 EVT Type = Op.getValueType();
4007 int64_t Val = C->getSExtValue();
4008 if ((Val <= 65535) && (Val >= 1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004009 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopherc18ae4a2012-05-07 06:25:02 +00004010 break;
4011 }
4012 }
4013 return;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00004014 }
4015
4016 if (Result.getNode()) {
4017 Ops.push_back(Result);
4018 return;
4019 }
4020
4021 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
4022}
4023
Mehdi Amini0cdec1e2015-07-09 02:09:40 +00004024bool MipsTargetLowering::isLegalAddressingMode(const DataLayout &DL,
4025 const AddrMode &AM, Type *Ty,
Jonas Paulsson024e3192017-07-21 11:59:37 +00004026 unsigned AS, Instruction *I) const {
Akira Hatanakaef839192012-11-17 00:25:41 +00004027 // No global is ever allowed as a base.
4028 if (AM.BaseGV)
4029 return false;
4030
4031 switch (AM.Scale) {
4032 case 0: // "r+i" or just "i", depending on HasBaseReg.
4033 break;
4034 case 1:
4035 if (!AM.HasBaseReg) // allow "r+i".
4036 break;
4037 return false; // disallow "r+r" or "r+r+i".
4038 default:
4039 return false;
4040 }
4041
4042 return true;
4043}
4044
4045bool
Dan Gohman2fe6bee2008-10-18 02:06:02 +00004046MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
4047 // The Mips target isn't yet aware of offsets.
4048 return false;
4049}
Evan Cheng16993aa2009-10-27 19:56:55 +00004050
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00004051EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00004052 unsigned SrcAlign,
4053 bool IsMemset, bool ZeroMemset,
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00004054 bool MemcpyStrSrc,
4055 MachineFunction &MF) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00004056 if (Subtarget.hasMips64())
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00004057 return MVT::i64;
4058
4059 return MVT::i32;
4060}
4061
Evan Cheng83896a52009-10-28 01:43:28 +00004062bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
4063 if (VT != MVT::f32 && VT != MVT::f64)
4064 return false;
Bruno Cardoso Lopesb02a9df2011-01-18 19:41:41 +00004065 if (Imm.isNegZero())
4066 return false;
Evan Cheng16993aa2009-10-27 19:56:55 +00004067 return Imm.isZero();
4068}
Akira Hatanakaf0b08442012-02-03 04:33:00 +00004069
4070unsigned MipsTargetLowering::getJumpTableEncoding() const {
Simon Dardisca74dd72017-01-27 11:36:52 +00004071
4072 // FIXME: For space reasons this should be: EK_GPRel32BlockAddress.
4073 if (ABI.IsN64() && isPositionIndependent())
Akira Hatanakaf0b08442012-02-03 04:33:00 +00004074 return MachineJumpTableInfo::EK_GPRel64BlockAddress;
Jia Liuf54f60f2012-02-28 07:46:26 +00004075
Akira Hatanakaf0b08442012-02-03 04:33:00 +00004076 return TargetLowering::getJumpTableEncoding();
4077}
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00004078
Eric Christopher824f42f2015-05-12 01:26:05 +00004079bool MipsTargetLowering::useSoftFloat() const {
4080 return Subtarget.useSoftFloat();
4081}
4082
Daniel Sandersf43e6872014-11-01 18:44:56 +00004083void MipsTargetLowering::copyByValRegs(
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004084 SDValue Chain, const SDLoc &DL, std::vector<SDValue> &OutChains,
4085 SelectionDAG &DAG, const ISD::ArgFlagsTy &Flags,
4086 SmallVectorImpl<SDValue> &InVals, const Argument *FuncArg,
4087 unsigned FirstReg, unsigned LastReg, const CCValAssign &VA,
4088 MipsCCState &State) const {
Akira Hatanaka25dad192012-10-27 00:10:18 +00004089 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00004090 MachineFrameInfo &MFI = MF.getFrameInfo();
Daniel Sanders2b746bc2014-09-09 12:11:16 +00004091 unsigned GPRSizeInBytes = Subtarget.getGPRSizeInBytes();
Daniel Sanders23e98772014-11-02 16:09:29 +00004092 unsigned NumRegs = LastReg - FirstReg;
4093 unsigned RegAreaSize = NumRegs * GPRSizeInBytes;
Akira Hatanaka25dad192012-10-27 00:10:18 +00004094 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
4095 int FrameObjOffset;
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00004096 ArrayRef<MCPhysReg> ByValArgRegs = ABI.GetByValArgRegs();
Akira Hatanaka25dad192012-10-27 00:10:18 +00004097
4098 if (RegAreaSize)
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00004099 FrameObjOffset =
4100 (int)ABI.GetCalleeAllocdArgSizeInBytes(State.getCallingConv()) -
4101 (int)((ByValArgRegs.size() - FirstReg) * GPRSizeInBytes);
Akira Hatanaka25dad192012-10-27 00:10:18 +00004102 else
Daniel Sandersf43e6872014-11-01 18:44:56 +00004103 FrameObjOffset = VA.getLocMemOffset();
Akira Hatanaka25dad192012-10-27 00:10:18 +00004104
4105 // Create frame object.
Mehdi Amini44ede332015-07-09 02:09:04 +00004106 EVT PtrTy = getPointerTy(DAG.getDataLayout());
Matthias Braun941a7052016-07-28 18:40:00 +00004107 int FI = MFI.CreateFixedObject(FrameObjSize, FrameObjOffset, true);
Akira Hatanaka25dad192012-10-27 00:10:18 +00004108 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
4109 InVals.push_back(FIN);
4110
Daniel Sanders23e98772014-11-02 16:09:29 +00004111 if (!NumRegs)
Akira Hatanaka25dad192012-10-27 00:10:18 +00004112 return;
4113
4114 // Copy arg registers.
Daniel Sanders2b746bc2014-09-09 12:11:16 +00004115 MVT RegTy = MVT::getIntegerVT(GPRSizeInBytes * 8);
Akira Hatanaka25dad192012-10-27 00:10:18 +00004116 const TargetRegisterClass *RC = getRegClassFor(RegTy);
4117
Daniel Sanders23e98772014-11-02 16:09:29 +00004118 for (unsigned I = 0; I < NumRegs; ++I) {
Daniel Sandersd7eba312014-11-07 12:21:37 +00004119 unsigned ArgReg = ByValArgRegs[FirstReg + I];
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00004120 unsigned VReg = addLiveIn(MF, ArgReg, RC);
Daniel Sanders2b746bc2014-09-09 12:11:16 +00004121 unsigned Offset = I * GPRSizeInBytes;
Akira Hatanaka25dad192012-10-27 00:10:18 +00004122 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004123 DAG.getConstant(Offset, DL, PtrTy));
Akira Hatanaka25dad192012-10-27 00:10:18 +00004124 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
Justin Lebar9c375812016-07-15 18:27:10 +00004125 StorePtr, MachinePointerInfo(FuncArg, Offset));
Akira Hatanaka25dad192012-10-27 00:10:18 +00004126 OutChains.push_back(Store);
4127 }
4128}
Akira Hatanaka35f55b12012-10-27 00:16:36 +00004129
4130// Copy byVal arg to registers and stack.
Daniel Sandersf43e6872014-11-01 18:44:56 +00004131void MipsTargetLowering::passByValArg(
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004132 SDValue Chain, const SDLoc &DL,
Daniel Sandersf43e6872014-11-01 18:44:56 +00004133 std::deque<std::pair<unsigned, SDValue>> &RegsToPass,
4134 SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr,
Matthias Braun941a7052016-07-28 18:40:00 +00004135 MachineFrameInfo &MFI, SelectionDAG &DAG, SDValue Arg, unsigned FirstReg,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00004136 unsigned LastReg, const ISD::ArgFlagsTy &Flags, bool isLittle,
4137 const CCValAssign &VA) const {
Daniel Sandersac272632014-05-23 13:18:02 +00004138 unsigned ByValSizeInBytes = Flags.getByValSize();
4139 unsigned OffsetInBytes = 0; // From beginning of struct
Daniel Sanders2b746bc2014-09-09 12:11:16 +00004140 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
Daniel Sandersac272632014-05-23 13:18:02 +00004141 unsigned Alignment = std::min(Flags.getByValAlign(), RegSizeInBytes);
Mehdi Amini44ede332015-07-09 02:09:04 +00004142 EVT PtrTy = getPointerTy(DAG.getDataLayout()),
4143 RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
Daniel Sanders23e98772014-11-02 16:09:29 +00004144 unsigned NumRegs = LastReg - FirstReg;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00004145
Daniel Sanders23e98772014-11-02 16:09:29 +00004146 if (NumRegs) {
Craig Topper862d5d82015-09-28 00:15:34 +00004147 ArrayRef<MCPhysReg> ArgRegs = ABI.GetByValArgRegs();
Daniel Sanders23e98772014-11-02 16:09:29 +00004148 bool LeftoverBytes = (NumRegs * RegSizeInBytes > ByValSizeInBytes);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00004149 unsigned I = 0;
4150
4151 // Copy words to registers.
Daniel Sanders23e98772014-11-02 16:09:29 +00004152 for (; I < NumRegs - LeftoverBytes; ++I, OffsetInBytes += RegSizeInBytes) {
Akira Hatanaka35f55b12012-10-27 00:16:36 +00004153 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004154 DAG.getConstant(OffsetInBytes, DL, PtrTy));
Akira Hatanaka35f55b12012-10-27 00:16:36 +00004155 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
Justin Lebar9c375812016-07-15 18:27:10 +00004156 MachinePointerInfo(), Alignment);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00004157 MemOpChains.push_back(LoadVal.getValue(1));
Daniel Sanders23e98772014-11-02 16:09:29 +00004158 unsigned ArgReg = ArgRegs[FirstReg + I];
Akira Hatanaka35f55b12012-10-27 00:16:36 +00004159 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
4160 }
4161
4162 // Return if the struct has been fully copied.
Daniel Sandersac272632014-05-23 13:18:02 +00004163 if (ByValSizeInBytes == OffsetInBytes)
Akira Hatanaka35f55b12012-10-27 00:16:36 +00004164 return;
4165
4166 // Copy the remainder of the byval argument with sub-word loads and shifts.
4167 if (LeftoverBytes) {
Akira Hatanaka35f55b12012-10-27 00:16:36 +00004168 SDValue Val;
4169
Daniel Sandersac272632014-05-23 13:18:02 +00004170 for (unsigned LoadSizeInBytes = RegSizeInBytes / 2, TotalBytesLoaded = 0;
4171 OffsetInBytes < ByValSizeInBytes; LoadSizeInBytes /= 2) {
4172 unsigned RemainingSizeInBytes = ByValSizeInBytes - OffsetInBytes;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00004173
Daniel Sandersac272632014-05-23 13:18:02 +00004174 if (RemainingSizeInBytes < LoadSizeInBytes)
Akira Hatanaka35f55b12012-10-27 00:16:36 +00004175 continue;
4176
4177 // Load subword.
4178 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004179 DAG.getConstant(OffsetInBytes, DL,
4180 PtrTy));
Daniel Sandersac272632014-05-23 13:18:02 +00004181 SDValue LoadVal = DAG.getExtLoad(
4182 ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr, MachinePointerInfo(),
Justin Lebar9c375812016-07-15 18:27:10 +00004183 MVT::getIntegerVT(LoadSizeInBytes * 8), Alignment);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00004184 MemOpChains.push_back(LoadVal.getValue(1));
4185
4186 // Shift the loaded value.
4187 unsigned Shamt;
4188
4189 if (isLittle)
Daniel Sandersac272632014-05-23 13:18:02 +00004190 Shamt = TotalBytesLoaded * 8;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00004191 else
Daniel Sandersac272632014-05-23 13:18:02 +00004192 Shamt = (RegSizeInBytes - (TotalBytesLoaded + LoadSizeInBytes)) * 8;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00004193
4194 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004195 DAG.getConstant(Shamt, DL, MVT::i32));
Akira Hatanaka35f55b12012-10-27 00:16:36 +00004196
4197 if (Val.getNode())
4198 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
4199 else
4200 Val = Shift;
4201
Daniel Sandersac272632014-05-23 13:18:02 +00004202 OffsetInBytes += LoadSizeInBytes;
4203 TotalBytesLoaded += LoadSizeInBytes;
4204 Alignment = std::min(Alignment, LoadSizeInBytes);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00004205 }
4206
Daniel Sanders23e98772014-11-02 16:09:29 +00004207 unsigned ArgReg = ArgRegs[FirstReg + I];
Akira Hatanaka35f55b12012-10-27 00:16:36 +00004208 RegsToPass.push_back(std::make_pair(ArgReg, Val));
4209 return;
4210 }
4211 }
4212
4213 // Copy remainder of byval arg to it with memcpy.
Daniel Sandersac272632014-05-23 13:18:02 +00004214 unsigned MemCpySize = ByValSizeInBytes - OffsetInBytes;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00004215 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004216 DAG.getConstant(OffsetInBytes, DL, PtrTy));
Akira Hatanaka35f55b12012-10-27 00:16:36 +00004217 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004218 DAG.getIntPtrConstant(VA.getLocMemOffset(), DL));
4219 Chain = DAG.getMemcpy(Chain, DL, Dst, Src,
4220 DAG.getConstant(MemCpySize, DL, PtrTy),
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00004221 Alignment, /*isVolatile=*/false, /*AlwaysInline=*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00004222 /*isTailCall=*/false,
Nick Lewyckyaad475b2014-04-15 07:22:52 +00004223 MachinePointerInfo(), MachinePointerInfo());
Akira Hatanaka35f55b12012-10-27 00:16:36 +00004224 MemOpChains.push_back(Chain);
4225}
Akira Hatanaka2a134022012-10-27 00:21:13 +00004226
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00004227void MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004228 SDValue Chain, const SDLoc &DL,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00004229 SelectionDAG &DAG,
Daniel Sanders853c2432014-11-01 18:13:52 +00004230 CCState &State) const {
Craig Topper862d5d82015-09-28 00:15:34 +00004231 ArrayRef<MCPhysReg> ArgRegs = ABI.GetVarArgRegs();
Tim Northover3b6b7ca2015-02-21 02:11:17 +00004232 unsigned Idx = State.getFirstUnallocated(ArgRegs);
Daniel Sanders2b746bc2014-09-09 12:11:16 +00004233 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
4234 MVT RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
Akira Hatanaka2a134022012-10-27 00:21:13 +00004235 const TargetRegisterClass *RC = getRegClassFor(RegTy);
4236 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00004237 MachineFrameInfo &MFI = MF.getFrameInfo();
Akira Hatanaka2a134022012-10-27 00:21:13 +00004238 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
4239
4240 // Offset of the first variable argument from stack pointer.
4241 int VaArgOffset;
4242
Daniel Sanders75ee6b42014-09-10 10:37:03 +00004243 if (ArgRegs.size() == Idx)
Rui Ueyamada00f2f2016-01-14 21:06:47 +00004244 VaArgOffset = alignTo(State.getNextStackOffset(), RegSizeInBytes);
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00004245 else {
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00004246 VaArgOffset =
4247 (int)ABI.GetCalleeAllocdArgSizeInBytes(State.getCallingConv()) -
4248 (int)(RegSizeInBytes * (ArgRegs.size() - Idx));
4249 }
Akira Hatanaka2a134022012-10-27 00:21:13 +00004250
4251 // Record the frame index of the first variable argument
4252 // which is a value necessary to VASTART.
Matthias Braun941a7052016-07-28 18:40:00 +00004253 int FI = MFI.CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
Akira Hatanaka2a134022012-10-27 00:21:13 +00004254 MipsFI->setVarArgsFrameIndex(FI);
4255
4256 // Copy the integer registers that have not been used for argument passing
4257 // to the argument register save area. For O32, the save area is allocated
4258 // in the caller's stack frame, while for N32/64, it is allocated in the
4259 // callee's stack frame.
Daniel Sanders75ee6b42014-09-10 10:37:03 +00004260 for (unsigned I = Idx; I < ArgRegs.size();
4261 ++I, VaArgOffset += RegSizeInBytes) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00004262 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC);
Akira Hatanaka2a134022012-10-27 00:21:13 +00004263 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
Matthias Braun941a7052016-07-28 18:40:00 +00004264 FI = MFI.CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
Mehdi Amini44ede332015-07-09 02:09:04 +00004265 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Justin Lebar9c375812016-07-15 18:27:10 +00004266 SDValue Store =
4267 DAG.getStore(Chain, DL, ArgValue, PtrOff, MachinePointerInfo());
Eric Christopher1c29a652014-07-18 22:55:25 +00004268 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(
4269 (Value *)nullptr);
Akira Hatanaka2a134022012-10-27 00:21:13 +00004270 OutChains.push_back(Store);
4271 }
4272}
Daniel Sanders23e98772014-11-02 16:09:29 +00004273
4274void MipsTargetLowering::HandleByVal(CCState *State, unsigned &Size,
4275 unsigned Align) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00004276 const TargetFrameLowering *TFL = Subtarget.getFrameLowering();
Daniel Sanders23e98772014-11-02 16:09:29 +00004277
4278 assert(Size && "Byval argument's size shouldn't be 0.");
4279
4280 Align = std::min(Align, TFL->getStackAlignment());
4281
4282 unsigned FirstReg = 0;
4283 unsigned NumRegs = 0;
4284
4285 if (State->getCallingConv() != CallingConv::Fast) {
4286 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
Craig Topper862d5d82015-09-28 00:15:34 +00004287 ArrayRef<MCPhysReg> IntArgRegs = ABI.GetByValArgRegs();
Daniel Sanders23e98772014-11-02 16:09:29 +00004288 // FIXME: The O32 case actually describes no shadow registers.
4289 const MCPhysReg *ShadowRegs =
Eric Christopher96e72c62015-01-29 23:27:36 +00004290 ABI.IsO32() ? IntArgRegs.data() : Mips64DPRegs;
Daniel Sanders23e98772014-11-02 16:09:29 +00004291
4292 // We used to check the size as well but we can't do that anymore since
4293 // CCState::HandleByVal() rounds up the size after calling this function.
4294 assert(!(Align % RegSizeInBytes) &&
4295 "Byval argument's alignment should be a multiple of"
4296 "RegSizeInBytes.");
4297
Tim Northover3b6b7ca2015-02-21 02:11:17 +00004298 FirstReg = State->getFirstUnallocated(IntArgRegs);
Daniel Sanders23e98772014-11-02 16:09:29 +00004299
4300 // If Align > RegSizeInBytes, the first arg register must be even.
4301 // FIXME: This condition happens to do the right thing but it's not the
4302 // right way to test it. We want to check that the stack frame offset
4303 // of the register is aligned.
4304 if ((Align > RegSizeInBytes) && (FirstReg % 2)) {
4305 State->AllocateReg(IntArgRegs[FirstReg], ShadowRegs[FirstReg]);
4306 ++FirstReg;
4307 }
4308
4309 // Mark the registers allocated.
Rui Ueyamada00f2f2016-01-14 21:06:47 +00004310 Size = alignTo(Size, RegSizeInBytes);
Daniel Sanders23e98772014-11-02 16:09:29 +00004311 for (unsigned I = FirstReg; Size > 0 && (I < IntArgRegs.size());
4312 Size -= RegSizeInBytes, ++I, ++NumRegs)
4313 State->AllocateReg(IntArgRegs[I], ShadowRegs[I]);
4314 }
4315
4316 State->addInRegsParamInfo(FirstReg, FirstReg + NumRegs);
4317}
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00004318
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004319MachineBasicBlock *MipsTargetLowering::emitPseudoSELECT(MachineInstr &MI,
4320 MachineBasicBlock *BB,
4321 bool isFPCmp,
4322 unsigned Opc) const {
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00004323 assert(!(Subtarget.hasMips4() || Subtarget.hasMips32()) &&
4324 "Subtarget already supports SELECT nodes with the use of"
4325 "conditional-move instructions.");
4326
4327 const TargetInstrInfo *TII =
Eric Christopher96e72c62015-01-29 23:27:36 +00004328 Subtarget.getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004329 DebugLoc DL = MI.getDebugLoc();
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00004330
4331 // To "insert" a SELECT instruction, we actually have to insert the
4332 // diamond control-flow pattern. The incoming instruction knows the
4333 // destination vreg to set, the condition code register to branch on, the
4334 // true/false values to select between, and a branch opcode to use.
4335 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +00004336 MachineFunction::iterator It = ++BB->getIterator();
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00004337
4338 // thisMBB:
4339 // ...
4340 // TrueVal = ...
4341 // setcc r1, r2, r3
4342 // bNE r1, r0, copy1MBB
4343 // fallthrough --> copy0MBB
4344 MachineBasicBlock *thisMBB = BB;
4345 MachineFunction *F = BB->getParent();
4346 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
4347 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
4348 F->insert(It, copy0MBB);
4349 F->insert(It, sinkMBB);
4350
4351 // Transfer the remainder of BB and its successor edges to sinkMBB.
4352 sinkMBB->splice(sinkMBB->begin(), BB,
4353 std::next(MachineBasicBlock::iterator(MI)), BB->end());
4354 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
4355
4356 // Next, add the true and fallthrough blocks as its successors.
4357 BB->addSuccessor(copy0MBB);
4358 BB->addSuccessor(sinkMBB);
4359
4360 if (isFPCmp) {
4361 // bc1[tf] cc, sinkMBB
4362 BuildMI(BB, DL, TII->get(Opc))
Simon Dardisba92b032016-09-09 11:06:01 +00004363 .addReg(MI.getOperand(1).getReg())
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004364 .addMBB(sinkMBB);
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00004365 } else {
4366 // bne rs, $0, sinkMBB
4367 BuildMI(BB, DL, TII->get(Opc))
Simon Dardisba92b032016-09-09 11:06:01 +00004368 .addReg(MI.getOperand(1).getReg())
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004369 .addReg(Mips::ZERO)
4370 .addMBB(sinkMBB);
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00004371 }
4372
4373 // copy0MBB:
4374 // %FalseValue = ...
4375 // # fallthrough to sinkMBB
4376 BB = copy0MBB;
4377
4378 // Update machine-CFG edges
4379 BB->addSuccessor(sinkMBB);
4380
4381 // sinkMBB:
4382 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
4383 // ...
4384 BB = sinkMBB;
4385
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004386 BuildMI(*BB, BB->begin(), DL, TII->get(Mips::PHI), MI.getOperand(0).getReg())
Simon Dardisba92b032016-09-09 11:06:01 +00004387 .addReg(MI.getOperand(2).getReg())
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004388 .addMBB(thisMBB)
4389 .addReg(MI.getOperand(3).getReg())
4390 .addMBB(copy0MBB);
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00004391
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004392 MI.eraseFromParent(); // The pseudo instruction is gone now.
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00004393
4394 return BB;
4395}
Daniel Sanders1440bb22015-01-09 17:21:30 +00004396
4397// FIXME? Maybe this could be a TableGen attribute on some registers and
4398// this table could be generated automatically from RegInfo.
Pat Gavlina717f252015-07-09 17:40:29 +00004399unsigned MipsTargetLowering::getRegisterByName(const char* RegName, EVT VT,
4400 SelectionDAG &DAG) const {
Daniel Sanders1440bb22015-01-09 17:21:30 +00004401 // Named registers is expected to be fairly rare. For now, just support $28
4402 // since the linux kernel uses it.
4403 if (Subtarget.isGP64bit()) {
4404 unsigned Reg = StringSwitch<unsigned>(RegName)
4405 .Case("$28", Mips::GP_64)
4406 .Default(0);
4407 if (Reg)
4408 return Reg;
4409 } else {
4410 unsigned Reg = StringSwitch<unsigned>(RegName)
4411 .Case("$28", Mips::GP)
4412 .Default(0);
4413 if (Reg)
4414 return Reg;
4415 }
4416 report_fatal_error("Invalid register name global variable");
4417}