blob: 93c5f496ce9716234ee7ca9ca40e9b70d536a130 [file] [log] [blame]
Jia Liuf54f60f2012-02-28 07:46:26 +00001//===-- MipsISelLowering.cpp - Mips DAG Lowering Implementation -----------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00007//
Akira Hatanakae2489122011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00009//
10// This file defines the interfaces that Mips uses to lower LLVM code into a
11// selection DAG.
12//
Akira Hatanakae2489122011-04-15 21:51:11 +000013//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000014#include "MipsISelLowering.h"
Craig Topperb25fda92012-03-17 18:46:09 +000015#include "InstPrinter/MipsInstPrinter.h"
16#include "MCTargetDesc/MipsBaseInfo.h"
Daniel Sanders0456c152014-11-07 14:24:31 +000017#include "MipsCCState.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "MipsMachineFunction.h"
19#include "MipsSubtarget.h"
20#include "MipsTargetMachine.h"
21#include "MipsTargetObjectFile.h"
Akira Hatanaka90131ac2012-10-19 21:47:33 +000022#include "llvm/ADT/Statistic.h"
Daniel Sanders8b59af12013-11-12 12:56:01 +000023#include "llvm/ADT/StringSwitch.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000024#include "llvm/CodeGen/CallingConvLower.h"
25#include "llvm/CodeGen/MachineFrameInfo.h"
26#include "llvm/CodeGen/MachineFunction.h"
27#include "llvm/CodeGen/MachineInstrBuilder.h"
Eric Christopher79cc1e32014-09-02 22:28:02 +000028#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000029#include "llvm/CodeGen/MachineRegisterInfo.h"
Vasileios Kalintiris2041b1d2015-07-30 12:39:33 +000030#include "llvm/CodeGen/FunctionLoweringInfo.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000031#include "llvm/CodeGen/SelectionDAGISel.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000032#include "llvm/CodeGen/ValueTypes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000033#include "llvm/IR/CallingConv.h"
34#include "llvm/IR/DerivedTypes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000035#include "llvm/IR/GlobalVariable.h"
Akira Hatanaka90131ac2012-10-19 21:47:33 +000036#include "llvm/Support/CommandLine.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000037#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000038#include "llvm/Support/ErrorHandling.h"
NAKAMURA Takumie30303f2012-04-21 15:31:45 +000039#include "llvm/Support/raw_ostream.h"
Akira Hatanaka7473b472013-08-14 00:21:25 +000040#include <cctype>
NAKAMURA Takumie30303f2012-04-21 15:31:45 +000041
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000042using namespace llvm;
43
Chandler Carruth84e68b22014-04-22 02:41:26 +000044#define DEBUG_TYPE "mips-lower"
45
Akira Hatanaka90131ac2012-10-19 21:47:33 +000046STATISTIC(NumTailCalls, "Number of tail calls");
47
48static cl::opt<bool>
Akira Hatanaka59f299f2012-11-21 20:21:11 +000049LargeGOT("mxgot", cl::Hidden,
50 cl::desc("MIPS: Enable GOT larger than 64k."), cl::init(false));
51
Akira Hatanaka1cb02422013-05-20 18:07:43 +000052static cl::opt<bool>
Akira Hatanakabe76cd02013-05-21 17:17:59 +000053NoZeroDivCheck("mno-check-zero-division", cl::Hidden,
Akira Hatanaka1cb02422013-05-20 18:07:43 +000054 cl::desc("MIPS: Don't trap on integer division by zero."),
55 cl::init(false));
56
Craig Topper840beec2014-04-04 05:16:06 +000057static const MCPhysReg Mips64DPRegs[8] = {
Akira Hatanakaac8c6692012-10-27 00:29:43 +000058 Mips::D12_64, Mips::D13_64, Mips::D14_64, Mips::D15_64,
59 Mips::D16_64, Mips::D17_64, Mips::D18_64, Mips::D19_64
60};
61
Jia Liuf54f60f2012-02-28 07:46:26 +000062// If I is a shifted mask, set the size (Size) and the first bit of the
Akira Hatanaka73d78b72011-08-18 20:07:42 +000063// mask (Pos), and return true.
Jia Liuf54f60f2012-02-28 07:46:26 +000064// For example, if I is 0x003ff800, (Pos, Size) = (11, 11).
Akira Hatanaka0bb60d892013-03-12 00:16:36 +000065static bool isShiftedMask(uint64_t I, uint64_t &Pos, uint64_t &Size) {
Akira Hatanaka20cee2e2011-12-05 21:26:34 +000066 if (!isShiftedMask_64(I))
Akira Hatanaka4c0a7122013-10-07 19:33:02 +000067 return false;
Akira Hatanaka5360f882011-08-17 02:05:42 +000068
Benjamin Kramer5f6a9072015-02-12 15:35:40 +000069 Size = countPopulation(I);
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +000070 Pos = countTrailingZeros(I);
Akira Hatanaka73d78b72011-08-18 20:07:42 +000071 return true;
Akira Hatanaka5360f882011-08-17 02:05:42 +000072}
73
Akira Hatanaka96ca1822013-03-13 00:54:29 +000074SDValue MipsTargetLowering::getGlobalReg(SelectionDAG &DAG, EVT Ty) const {
Akira Hatanakab049aef2012-02-24 22:34:47 +000075 MipsFunctionInfo *FI = DAG.getMachineFunction().getInfo<MipsFunctionInfo>();
76 return DAG.getRegister(FI->getGlobalBaseReg(), Ty);
77}
78
Akira Hatanakad8f10ce2013-09-27 19:51:35 +000079SDValue MipsTargetLowering::getTargetNode(GlobalAddressSDNode *N, EVT Ty,
80 SelectionDAG &DAG,
Akira Hatanaka96ca1822013-03-13 00:54:29 +000081 unsigned Flag) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +000082 return DAG.getTargetGlobalAddress(N->getGlobal(), SDLoc(N), Ty, 0, Flag);
Akira Hatanakafd04ad42012-11-21 20:26:38 +000083}
84
Akira Hatanakad8f10ce2013-09-27 19:51:35 +000085SDValue MipsTargetLowering::getTargetNode(ExternalSymbolSDNode *N, EVT Ty,
86 SelectionDAG &DAG,
87 unsigned Flag) const {
88 return DAG.getTargetExternalSymbol(N->getSymbol(), Ty, Flag);
89}
90
91SDValue MipsTargetLowering::getTargetNode(BlockAddressSDNode *N, EVT Ty,
92 SelectionDAG &DAG,
93 unsigned Flag) const {
94 return DAG.getTargetBlockAddress(N->getBlockAddress(), Ty, 0, Flag);
95}
96
97SDValue MipsTargetLowering::getTargetNode(JumpTableSDNode *N, EVT Ty,
98 SelectionDAG &DAG,
99 unsigned Flag) const {
100 return DAG.getTargetJumpTable(N->getIndex(), Ty, Flag);
101}
102
103SDValue MipsTargetLowering::getTargetNode(ConstantPoolSDNode *N, EVT Ty,
104 SelectionDAG &DAG,
105 unsigned Flag) const {
106 return DAG.getTargetConstantPool(N->getConstVal(), Ty, N->getAlignment(),
107 N->getOffset(), Flag);
Akira Hatanakafd04ad42012-11-21 20:26:38 +0000108}
109
Chris Lattner5e693ed2009-07-28 03:13:23 +0000110const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +0000111 switch ((MipsISD::NodeType)Opcode) {
112 case MipsISD::FIRST_NUMBER: break;
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000113 case MipsISD::JmpLink: return "MipsISD::JmpLink";
Akira Hatanaka91318df2012-10-19 20:59:39 +0000114 case MipsISD::TailCall: return "MipsISD::TailCall";
Simon Dardisca74dd72017-01-27 11:36:52 +0000115 case MipsISD::Highest: return "MipsISD::Highest";
116 case MipsISD::Higher: return "MipsISD::Higher";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000117 case MipsISD::Hi: return "MipsISD::Hi";
118 case MipsISD::Lo: return "MipsISD::Lo";
Simon Dardisca74dd72017-01-27 11:36:52 +0000119 case MipsISD::GotHi: return "MipsISD::GotHi";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000120 case MipsISD::GPRel: return "MipsISD::GPRel";
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +0000121 case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000122 case MipsISD::Ret: return "MipsISD::Ret";
Vasileios Kalintiris43dff0c2015-10-26 12:38:43 +0000123 case MipsISD::ERet: return "MipsISD::ERet";
Akira Hatanakac0b02062013-01-30 00:26:49 +0000124 case MipsISD::EH_RETURN: return "MipsISD::EH_RETURN";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000125 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
126 case MipsISD::FPCmp: return "MipsISD::FPCmp";
127 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
128 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000129 case MipsISD::TruncIntFP: return "MipsISD::TruncIntFP";
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000130 case MipsISD::MFHI: return "MipsISD::MFHI";
131 case MipsISD::MFLO: return "MipsISD::MFLO";
132 case MipsISD::MTLOHI: return "MipsISD::MTLOHI";
Akira Hatanaka28721bd2013-03-30 01:14:04 +0000133 case MipsISD::Mult: return "MipsISD::Mult";
134 case MipsISD::Multu: return "MipsISD::Multu";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000135 case MipsISD::MAdd: return "MipsISD::MAdd";
136 case MipsISD::MAddu: return "MipsISD::MAddu";
137 case MipsISD::MSub: return "MipsISD::MSub";
138 case MipsISD::MSubu: return "MipsISD::MSubu";
139 case MipsISD::DivRem: return "MipsISD::DivRem";
140 case MipsISD::DivRemU: return "MipsISD::DivRemU";
Akira Hatanaka28721bd2013-03-30 01:14:04 +0000141 case MipsISD::DivRem16: return "MipsISD::DivRem16";
142 case MipsISD::DivRemU16: return "MipsISD::DivRemU16";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000143 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
144 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
Akira Hatanakafaa88c02011-12-12 22:38:19 +0000145 case MipsISD::Wrapper: return "MipsISD::Wrapper";
Matthias Braund04893f2015-05-07 21:33:59 +0000146 case MipsISD::DynAlloc: return "MipsISD::DynAlloc";
Akira Hatanakaa4c09bc2011-07-19 23:30:50 +0000147 case MipsISD::Sync: return "MipsISD::Sync";
Akira Hatanaka5360f882011-08-17 02:05:42 +0000148 case MipsISD::Ext: return "MipsISD::Ext";
149 case MipsISD::Ins: return "MipsISD::Ins";
Petar Jovanovicb71386a2017-03-15 13:10:08 +0000150 case MipsISD::CIns: return "MipsISD::CIns";
Akira Hatanakab9ebf8d2012-06-02 00:03:12 +0000151 case MipsISD::LWL: return "MipsISD::LWL";
152 case MipsISD::LWR: return "MipsISD::LWR";
153 case MipsISD::SWL: return "MipsISD::SWL";
154 case MipsISD::SWR: return "MipsISD::SWR";
155 case MipsISD::LDL: return "MipsISD::LDL";
156 case MipsISD::LDR: return "MipsISD::LDR";
157 case MipsISD::SDL: return "MipsISD::SDL";
158 case MipsISD::SDR: return "MipsISD::SDR";
Akira Hatanaka233ac532012-09-21 23:52:47 +0000159 case MipsISD::EXTP: return "MipsISD::EXTP";
160 case MipsISD::EXTPDP: return "MipsISD::EXTPDP";
161 case MipsISD::EXTR_S_H: return "MipsISD::EXTR_S_H";
162 case MipsISD::EXTR_W: return "MipsISD::EXTR_W";
163 case MipsISD::EXTR_R_W: return "MipsISD::EXTR_R_W";
164 case MipsISD::EXTR_RS_W: return "MipsISD::EXTR_RS_W";
165 case MipsISD::SHILO: return "MipsISD::SHILO";
166 case MipsISD::MTHLIP: return "MipsISD::MTHLIP";
Matthias Braund04893f2015-05-07 21:33:59 +0000167 case MipsISD::MULSAQ_S_W_PH: return "MipsISD::MULSAQ_S_W_PH";
168 case MipsISD::MAQ_S_W_PHL: return "MipsISD::MAQ_S_W_PHL";
169 case MipsISD::MAQ_S_W_PHR: return "MipsISD::MAQ_S_W_PHR";
170 case MipsISD::MAQ_SA_W_PHL: return "MipsISD::MAQ_SA_W_PHL";
171 case MipsISD::MAQ_SA_W_PHR: return "MipsISD::MAQ_SA_W_PHR";
172 case MipsISD::DPAU_H_QBL: return "MipsISD::DPAU_H_QBL";
173 case MipsISD::DPAU_H_QBR: return "MipsISD::DPAU_H_QBR";
174 case MipsISD::DPSU_H_QBL: return "MipsISD::DPSU_H_QBL";
175 case MipsISD::DPSU_H_QBR: return "MipsISD::DPSU_H_QBR";
176 case MipsISD::DPAQ_S_W_PH: return "MipsISD::DPAQ_S_W_PH";
177 case MipsISD::DPSQ_S_W_PH: return "MipsISD::DPSQ_S_W_PH";
178 case MipsISD::DPAQ_SA_L_W: return "MipsISD::DPAQ_SA_L_W";
179 case MipsISD::DPSQ_SA_L_W: return "MipsISD::DPSQ_SA_L_W";
180 case MipsISD::DPA_W_PH: return "MipsISD::DPA_W_PH";
181 case MipsISD::DPS_W_PH: return "MipsISD::DPS_W_PH";
182 case MipsISD::DPAQX_S_W_PH: return "MipsISD::DPAQX_S_W_PH";
183 case MipsISD::DPAQX_SA_W_PH: return "MipsISD::DPAQX_SA_W_PH";
184 case MipsISD::DPAX_W_PH: return "MipsISD::DPAX_W_PH";
185 case MipsISD::DPSX_W_PH: return "MipsISD::DPSX_W_PH";
186 case MipsISD::DPSQX_S_W_PH: return "MipsISD::DPSQX_S_W_PH";
187 case MipsISD::DPSQX_SA_W_PH: return "MipsISD::DPSQX_SA_W_PH";
188 case MipsISD::MULSA_W_PH: return "MipsISD::MULSA_W_PH";
Akira Hatanaka233ac532012-09-21 23:52:47 +0000189 case MipsISD::MULT: return "MipsISD::MULT";
190 case MipsISD::MULTU: return "MipsISD::MULTU";
Jia Liu434874d2013-03-04 01:06:54 +0000191 case MipsISD::MADD_DSP: return "MipsISD::MADD_DSP";
Akira Hatanaka233ac532012-09-21 23:52:47 +0000192 case MipsISD::MADDU_DSP: return "MipsISD::MADDU_DSP";
193 case MipsISD::MSUB_DSP: return "MipsISD::MSUB_DSP";
194 case MipsISD::MSUBU_DSP: return "MipsISD::MSUBU_DSP";
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000195 case MipsISD::SHLL_DSP: return "MipsISD::SHLL_DSP";
196 case MipsISD::SHRA_DSP: return "MipsISD::SHRA_DSP";
197 case MipsISD::SHRL_DSP: return "MipsISD::SHRL_DSP";
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000198 case MipsISD::SETCC_DSP: return "MipsISD::SETCC_DSP";
199 case MipsISD::SELECT_CC_DSP: return "MipsISD::SELECT_CC_DSP";
Daniel Sandersce09d072013-08-28 12:14:50 +0000200 case MipsISD::VALL_ZERO: return "MipsISD::VALL_ZERO";
201 case MipsISD::VANY_ZERO: return "MipsISD::VANY_ZERO";
202 case MipsISD::VALL_NONZERO: return "MipsISD::VALL_NONZERO";
203 case MipsISD::VANY_NONZERO: return "MipsISD::VANY_NONZERO";
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000204 case MipsISD::VCEQ: return "MipsISD::VCEQ";
205 case MipsISD::VCLE_S: return "MipsISD::VCLE_S";
206 case MipsISD::VCLE_U: return "MipsISD::VCLE_U";
207 case MipsISD::VCLT_S: return "MipsISD::VCLT_S";
208 case MipsISD::VCLT_U: return "MipsISD::VCLT_U";
Daniel Sanders3ce56622013-09-24 12:18:31 +0000209 case MipsISD::VSMAX: return "MipsISD::VSMAX";
210 case MipsISD::VSMIN: return "MipsISD::VSMIN";
211 case MipsISD::VUMAX: return "MipsISD::VUMAX";
212 case MipsISD::VUMIN: return "MipsISD::VUMIN";
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000213 case MipsISD::VEXTRACT_SEXT_ELT: return "MipsISD::VEXTRACT_SEXT_ELT";
214 case MipsISD::VEXTRACT_ZEXT_ELT: return "MipsISD::VEXTRACT_ZEXT_ELT";
Daniel Sandersf7456c72013-09-23 13:22:24 +0000215 case MipsISD::VNOR: return "MipsISD::VNOR";
Daniel Sanderse5087042013-09-24 14:02:15 +0000216 case MipsISD::VSHF: return "MipsISD::VSHF";
Daniel Sanders26307182013-09-24 14:20:00 +0000217 case MipsISD::SHF: return "MipsISD::SHF";
Daniel Sanders2ed228b2013-09-24 14:36:12 +0000218 case MipsISD::ILVEV: return "MipsISD::ILVEV";
219 case MipsISD::ILVOD: return "MipsISD::ILVOD";
220 case MipsISD::ILVL: return "MipsISD::ILVL";
221 case MipsISD::ILVR: return "MipsISD::ILVR";
Daniel Sandersfae5f2a2013-09-24 14:53:25 +0000222 case MipsISD::PCKEV: return "MipsISD::PCKEV";
223 case MipsISD::PCKOD: return "MipsISD::PCKOD";
Daniel Sandersb50ccf82014-04-01 10:35:28 +0000224 case MipsISD::INSVE: return "MipsISD::INSVE";
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000225 }
Matthias Braund04893f2015-05-07 21:33:59 +0000226 return nullptr;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000227}
228
Eric Christopherb1526602014-09-19 23:30:42 +0000229MipsTargetLowering::MipsTargetLowering(const MipsTargetMachine &TM,
Eric Christopher8924d272014-07-18 23:25:04 +0000230 const MipsSubtarget &STI)
Eric Christopher96e72c62015-01-29 23:27:36 +0000231 : TargetLowering(TM), Subtarget(STI), ABI(TM.getABI()) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000232 // Mips does not have i1 type, so use i32 for
Wesley Peck527da1b2010-11-23 03:31:01 +0000233 // setcc operations results (slt, sgt, ...).
Duncan Sands8d6e2e12008-11-23 15:47:28 +0000234 setBooleanContents(ZeroOrOneBooleanContent);
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000235 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Daniel Sanderscbd44c52014-07-10 10:18:12 +0000236 // The cmp.cond.fmt instruction in MIPS32r6/MIPS64r6 uses 0 and -1 like MSA
237 // does. Integer booleans still use 0 and 1.
Eric Christopher1c29a652014-07-18 22:55:25 +0000238 if (Subtarget.hasMips32r6())
Daniel Sanderscbd44c52014-07-10 10:18:12 +0000239 setBooleanContents(ZeroOrOneBooleanContent,
240 ZeroOrNegativeOneBooleanContent);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000241
Wesley Peck527da1b2010-11-23 03:31:01 +0000242 // Load extented operations for i1 types must be promoted
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000243 for (MVT VT : MVT::integer_valuetypes()) {
244 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
245 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
246 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
247 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000248
Pirama Arumuga Nainar34056de2015-04-20 20:15:36 +0000249 // MIPS doesn't have extending float->double load/store. Set LoadExtAction
250 // for f32, f16
251 for (MVT VT : MVT::fp_valuetypes()) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000252 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
Pirama Arumuga Nainar34056de2015-04-20 20:15:36 +0000253 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f16, Expand);
254 }
255
256 // Set LoadExtAction for f16 vectors to Expand
257 for (MVT VT : MVT::fp_vector_valuetypes()) {
258 MVT F16VT = MVT::getVectorVT(MVT::f16, VT.getVectorNumElements());
259 if (F16VT.isValid())
260 setLoadExtAction(ISD::EXTLOAD, VT, F16VT, Expand);
261 }
262
263 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
264 setTruncStoreAction(MVT::f64, MVT::f16, Expand);
265
Owen Anderson9f944592009-08-11 20:47:22 +0000266 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman39d6faa2009-07-17 02:28:12 +0000267
Wesley Peck527da1b2010-11-23 03:31:01 +0000268 // Used by legalize types to correctly generate the setcc result.
269 // Without this, every float setcc comes with a AND/OR with the result,
270 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +0000271 // which is used implicitly by brcond and select operations.
Vasileios Kalintiris36901dd2016-03-01 20:25:43 +0000272 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +0000273
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000274 // Mips Custom Operations
Joerg Sonnenberger1a7eec62016-11-15 12:39:46 +0000275 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000276 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000277 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000278 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
279 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
280 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Simon Dardisba92b032016-09-09 11:06:01 +0000281 setOperationAction(ISD::SELECT, MVT::f32, Custom);
282 setOperationAction(ISD::SELECT, MVT::f64, Custom);
283 setOperationAction(ISD::SELECT, MVT::i32, Custom);
284 setOperationAction(ISD::SETCC, MVT::f32, Custom);
285 setOperationAction(ISD::SETCC, MVT::f64, Custom);
286 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000287 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
288 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000289 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000290
Eric Christopher1c29a652014-07-18 22:55:25 +0000291 if (Subtarget.isGP64bit()) {
Akira Hatanakada00aa82012-03-10 00:03:50 +0000292 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
293 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
294 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
295 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
296 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
Simon Dardisba92b032016-09-09 11:06:01 +0000297 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Akira Hatanaka019e5922012-06-02 00:04:42 +0000298 setOperationAction(ISD::LOAD, MVT::i64, Custom);
299 setOperationAction(ISD::STORE, MVT::i64, Custom);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000300 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +0000301 setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom);
302 setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom);
303 setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000304 }
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +0000305
Eric Christopher1c29a652014-07-18 22:55:25 +0000306 if (!Subtarget.isGP64bit()) {
Akira Hatanaka0a8ab712012-05-09 00:55:21 +0000307 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
308 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
309 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
310 }
311
Hal Finkel5081ac22016-09-01 10:28:47 +0000312 setOperationAction(ISD::EH_DWARF_CFA, MVT::i32, Custom);
Eric Christopher1c29a652014-07-18 22:55:25 +0000313 if (Subtarget.isGP64bit())
Hal Finkel5081ac22016-09-01 10:28:47 +0000314 setOperationAction(ISD::EH_DWARF_CFA, MVT::i64, Custom);
Akira Hatanaka28e02ec2012-11-07 19:10:58 +0000315
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000316 setOperationAction(ISD::SDIV, MVT::i32, Expand);
317 setOperationAction(ISD::SREM, MVT::i32, Expand);
318 setOperationAction(ISD::UDIV, MVT::i32, Expand);
319 setOperationAction(ISD::UREM, MVT::i32, Expand);
Akira Hatanakab1538f92011-10-03 21:06:13 +0000320 setOperationAction(ISD::SDIV, MVT::i64, Expand);
321 setOperationAction(ISD::SREM, MVT::i64, Expand);
322 setOperationAction(ISD::UDIV, MVT::i64, Expand);
323 setOperationAction(ISD::UREM, MVT::i64, Expand);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000324
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000325 // Operations not directly supported by Mips.
Tom Stellardb1588fc2013-03-08 15:36:57 +0000326 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
327 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
328 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
329 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
Tom Stellard3787b122014-06-10 16:01:29 +0000330 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
331 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
Matt Arsenaulta0e5cd52016-01-11 16:44:48 +0000332 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
333 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000334 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Akira Hatanaka79aed152011-12-20 23:40:56 +0000335 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000336 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Akira Hatanaka79aed152011-12-20 23:40:56 +0000337 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000338 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Eric Christopher1c29a652014-07-18 22:55:25 +0000339 if (Subtarget.hasCnMips()) {
Kai Nacke93fe5e82014-03-20 11:51:58 +0000340 setOperationAction(ISD::CTPOP, MVT::i32, Legal);
341 setOperationAction(ISD::CTPOP, MVT::i64, Legal);
342 } else {
343 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
344 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
345 }
Owen Anderson9f944592009-08-11 20:47:22 +0000346 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
Akira Hatanaka410ce9c2011-12-21 00:14:05 +0000347 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000348 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Akira Hatanaka7ba8a8d2011-09-30 18:51:46 +0000349 setOperationAction(ISD::ROTL, MVT::i64, Expand);
Akira Hatanaka33a25af2012-07-31 20:54:48 +0000350 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
351 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Bruno Cardoso Lopesd47180e2010-12-09 17:32:30 +0000352
Eric Christopher1c29a652014-07-18 22:55:25 +0000353 if (!Subtarget.hasMips32r2())
Bruno Cardoso Lopesd47180e2010-12-09 17:32:30 +0000354 setOperationAction(ISD::ROTR, MVT::i32, Expand);
355
Eric Christopher1c29a652014-07-18 22:55:25 +0000356 if (!Subtarget.hasMips64r2())
Akira Hatanaka7ba8a8d2011-09-30 18:51:46 +0000357 setOperationAction(ISD::ROTR, MVT::i64, Expand);
358
Owen Anderson9f944592009-08-11 20:47:22 +0000359 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes22b69db2011-03-04 18:54:14 +0000360 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000361 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes22b69db2011-03-04 18:54:14 +0000362 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000363 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
364 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000365 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
366 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanakadfb8cda2011-05-23 22:23:58 +0000367 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000368 setOperationAction(ISD::FLOG, MVT::f32, Expand);
369 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
370 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
371 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000372 setOperationAction(ISD::FMA, MVT::f32, Expand);
373 setOperationAction(ISD::FMA, MVT::f64, Expand);
Akira Hatanaka0603ad82012-03-29 18:43:11 +0000374 setOperationAction(ISD::FREM, MVT::f32, Expand);
375 setOperationAction(ISD::FREM, MVT::f64, Expand);
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000376
Pirama Arumuga Nainar34056de2015-04-20 20:15:36 +0000377 // Lower f16 conversion operations into library calls
378 setOperationAction(ISD::FP16_TO_FP, MVT::f32, Expand);
379 setOperationAction(ISD::FP_TO_FP16, MVT::f32, Expand);
380 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand);
381 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Expand);
382
Akira Hatanakac0b02062013-01-30 00:26:49 +0000383 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
384
Daniel Sanders2b553d42014-08-01 09:17:39 +0000385 setOperationAction(ISD::VASTART, MVT::Other, Custom);
386 setOperationAction(ISD::VAARG, MVT::Other, Custom);
Bruno Cardoso Lopes048ffab2011-03-09 19:22:22 +0000387 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
388 setOperationAction(ISD::VAEND, MVT::Other, Expand);
389
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000390 // Use the default for now
Owen Anderson9f944592009-08-11 20:47:22 +0000391 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
392 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman26a48482011-07-27 22:21:52 +0000393
Vasileios Kalintirisb04672c2015-11-06 12:07:20 +0000394 if (!Subtarget.isGP64bit()) {
395 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
396 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
397 }
Eli Friedman7dfa7912011-08-29 18:23:02 +0000398
Eli Friedman30a49e92011-08-03 21:06:02 +0000399
Eric Christopher1c29a652014-07-18 22:55:25 +0000400 if (!Subtarget.hasMips32r2()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000401 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
402 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000403 }
404
Daniel Sanders070fd1c2014-05-12 12:41:59 +0000405 // MIPS16 lacks MIPS32's clz and clo instructions.
Eric Christopher1c29a652014-07-18 22:55:25 +0000406 if (!Subtarget.hasMips32() || Subtarget.inMips16Mode())
Owen Anderson9f944592009-08-11 20:47:22 +0000407 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Eric Christopher1c29a652014-07-18 22:55:25 +0000408 if (!Subtarget.hasMips64())
Akira Hatanaka1d8efab2011-12-21 00:20:27 +0000409 setOperationAction(ISD::CTLZ, MVT::i64, Expand);
Bruno Cardoso Lopes93da7e62008-08-08 06:16:31 +0000410
Eric Christopher1c29a652014-07-18 22:55:25 +0000411 if (!Subtarget.hasMips32r2())
Owen Anderson9f944592009-08-11 20:47:22 +0000412 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Eric Christopher1c29a652014-07-18 22:55:25 +0000413 if (!Subtarget.hasMips64r2())
Akira Hatanaka4706ac92011-12-20 23:56:43 +0000414 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000415
Eric Christopher1c29a652014-07-18 22:55:25 +0000416 if (Subtarget.isGP64bit()) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000417 setLoadExtAction(ISD::SEXTLOAD, MVT::i64, MVT::i32, Custom);
418 setLoadExtAction(ISD::ZEXTLOAD, MVT::i64, MVT::i32, Custom);
419 setLoadExtAction(ISD::EXTLOAD, MVT::i64, MVT::i32, Custom);
Akira Hatanaka019e5922012-06-02 00:04:42 +0000420 setTruncStoreAction(MVT::i64, MVT::i32, Custom);
421 }
422
Akira Hatanakaa3d9ab92013-07-26 20:58:55 +0000423 setOperationAction(ISD::TRAP, MVT::Other, Legal);
424
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000425 setTargetDAGCombine(ISD::SDIVREM);
426 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka5e152182012-03-08 03:26:37 +0000427 setTargetDAGCombine(ISD::SELECT);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000428 setTargetDAGCombine(ISD::AND);
429 setTargetDAGCombine(ISD::OR);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000430 setTargetDAGCombine(ISD::ADD);
Vasileios Kalintiris3751d412016-04-13 15:07:45 +0000431 setTargetDAGCombine(ISD::AssertZext);
Petar Jovanovicb71386a2017-03-15 13:10:08 +0000432 setTargetDAGCombine(ISD::SHL);
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000433
Vasileios Kalintiris1ed49fd2016-09-07 10:01:18 +0000434 if (ABI.IsO32()) {
435 // These libcalls are not available in 32-bit.
436 setLibcallName(RTLIB::SHL_I128, nullptr);
437 setLibcallName(RTLIB::SRL_I128, nullptr);
438 setLibcallName(RTLIB::SRA_I128, nullptr);
439 }
440
Eric Christopher1c29a652014-07-18 22:55:25 +0000441 setMinFunctionAlignment(Subtarget.isGP64bit() ? 3 : 2);
Eli Friedman2518f832011-05-06 20:34:06 +0000442
Daniel Sanders2b553d42014-08-01 09:17:39 +0000443 // The arguments on the stack are defined in terms of 4-byte slots on O32
444 // and 8-byte slots on N32/N64.
Eric Christopher96e72c62015-01-29 23:27:36 +0000445 setMinStackArgumentAlignment((ABI.IsN32() || ABI.IsN64()) ? 8 : 4);
Daniel Sanders2b553d42014-08-01 09:17:39 +0000446
Eric Christopher96e72c62015-01-29 23:27:36 +0000447 setStackPointerRegisterToSaveRestore(ABI.IsN64() ? Mips::SP_64 : Mips::SP);
Akira Hatanakaaa560002011-05-26 18:59:03 +0000448
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000449 MaxStoresPerMemcpy = 16;
Zoran Jovanovicff9d5f32013-12-19 16:12:56 +0000450
Eric Christopher1c29a652014-07-18 22:55:25 +0000451 isMicroMips = Subtarget.inMicroMipsMode();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000452}
453
Eric Christopherb1526602014-09-19 23:30:42 +0000454const MipsTargetLowering *MipsTargetLowering::create(const MipsTargetMachine &TM,
Eric Christopher8924d272014-07-18 23:25:04 +0000455 const MipsSubtarget &STI) {
456 if (STI.inMips16Mode())
457 return llvm::createMips16TargetLowering(TM, STI);
Jia Liuf54f60f2012-02-28 07:46:26 +0000458
Eric Christopher8924d272014-07-18 23:25:04 +0000459 return llvm::createMipsSETargetLowering(TM, STI);
Akira Hatanaka2fcc1cf2011-08-12 21:30:06 +0000460}
461
Reed Kotler720c5ca2014-04-17 22:15:34 +0000462// Create a fast isel object.
463FastISel *
464MipsTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
465 const TargetLibraryInfo *libInfo) const {
Vasileios Kalintiris3955b752016-10-18 13:05:42 +0000466 const MipsTargetMachine &TM =
467 static_cast<const MipsTargetMachine &>(funcInfo.MF->getTarget());
468
469 // We support only the standard encoding [MIPS32,MIPS32R5] ISAs.
470 bool UseFastISel = TM.Options.EnableFastISel && Subtarget.hasMips32() &&
471 !Subtarget.hasMips32r6() && !Subtarget.inMips16Mode() &&
472 !Subtarget.inMicroMipsMode();
473
474 // Disable if we don't generate PIC or the ABI isn't O32.
475 if (!TM.isPositionIndependent() || !TM.getABI().IsO32())
476 UseFastISel = false;
477
478 return UseFastISel ? Mips::createFastISel(funcInfo, libInfo) : nullptr;
Reed Kotler720c5ca2014-04-17 22:15:34 +0000479}
480
Mehdi Amini44ede332015-07-09 02:09:04 +0000481EVT MipsTargetLowering::getSetCCResultType(const DataLayout &, LLVMContext &,
482 EVT VT) const {
Vasileios Kalintiris36901dd2016-03-01 20:25:43 +0000483 if (!VT.isVector())
484 return MVT::i32;
485 return VT.changeVectorElementTypeToInteger();
Scott Michela6729e82008-03-10 15:42:14 +0000486}
487
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000488static SDValue performDivRemCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000489 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000490 const MipsSubtarget &Subtarget) {
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000491 if (DCI.isBeforeLegalizeOps())
492 return SDValue();
493
Akira Hatanakab1538f92011-10-03 21:06:13 +0000494 EVT Ty = N->getValueType(0);
Akira Hatanaka8002a3f2013-08-14 00:47:08 +0000495 unsigned LO = (Ty == MVT::i32) ? Mips::LO0 : Mips::LO0_64;
496 unsigned HI = (Ty == MVT::i32) ? Mips::HI0 : Mips::HI0_64;
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000497 unsigned Opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem16 :
498 MipsISD::DivRemU16;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000499 SDLoc DL(N);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000500
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000501 SDValue DivRem = DAG.getNode(Opc, DL, MVT::Glue,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000502 N->getOperand(0), N->getOperand(1));
503 SDValue InChain = DAG.getEntryNode();
504 SDValue InGlue = DivRem;
505
506 // insert MFLO
507 if (N->hasAnyUseOfValue(0)) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000508 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, DL, LO, Ty,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000509 InGlue);
510 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
511 InChain = CopyFromLo.getValue(1);
512 InGlue = CopyFromLo.getValue(2);
513 }
514
515 // insert MFHI
516 if (N->hasAnyUseOfValue(1)) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000517 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, DL,
Akira Hatanakab1538f92011-10-03 21:06:13 +0000518 HI, Ty, InGlue);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000519 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
520 }
521
522 return SDValue();
523}
524
Simon Dardisba92b032016-09-09 11:06:01 +0000525static Mips::CondCode condCodeToFCC(ISD::CondCode CC) {
526 switch (CC) {
527 default: llvm_unreachable("Unknown fp condition code!");
528 case ISD::SETEQ:
529 case ISD::SETOEQ: return Mips::FCOND_OEQ;
530 case ISD::SETUNE: return Mips::FCOND_UNE;
531 case ISD::SETLT:
532 case ISD::SETOLT: return Mips::FCOND_OLT;
533 case ISD::SETGT:
534 case ISD::SETOGT: return Mips::FCOND_OGT;
535 case ISD::SETLE:
536 case ISD::SETOLE: return Mips::FCOND_OLE;
537 case ISD::SETGE:
538 case ISD::SETOGE: return Mips::FCOND_OGE;
539 case ISD::SETULT: return Mips::FCOND_ULT;
540 case ISD::SETULE: return Mips::FCOND_ULE;
541 case ISD::SETUGT: return Mips::FCOND_UGT;
542 case ISD::SETUGE: return Mips::FCOND_UGE;
543 case ISD::SETUO: return Mips::FCOND_UN;
544 case ISD::SETO: return Mips::FCOND_OR;
545 case ISD::SETNE:
546 case ISD::SETONE: return Mips::FCOND_ONE;
547 case ISD::SETUEQ: return Mips::FCOND_UEQ;
548 }
549}
550
551
552/// This function returns true if the floating point conditional branches and
553/// conditional moves which use condition code CC should be inverted.
554static bool invertFPCondCodeUser(Mips::CondCode CC) {
555 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
556 return false;
557
558 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
559 "Illegal Condition Code");
560
561 return true;
562}
563
564// Creates and returns an FPCmp node from a setcc node.
565// Returns Op if setcc is not a floating point comparison.
566static SDValue createFPCmp(SelectionDAG &DAG, const SDValue &Op) {
567 // must be a SETCC node
568 if (Op.getOpcode() != ISD::SETCC)
569 return Op;
570
571 SDValue LHS = Op.getOperand(0);
572
573 if (!LHS.getValueType().isFloatingPoint())
574 return Op;
575
576 SDValue RHS = Op.getOperand(1);
577 SDLoc DL(Op);
578
579 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
580 // node if necessary.
581 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
582
583 return DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS,
584 DAG.getConstant(condCodeToFCC(CC), DL, MVT::i32));
585}
586
587// Creates and returns a CMovFPT/F node.
588static SDValue createCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True,
589 SDValue False, const SDLoc &DL) {
590 ConstantSDNode *CC = cast<ConstantSDNode>(Cond.getOperand(2));
591 bool invert = invertFPCondCodeUser((Mips::CondCode)CC->getSExtValue());
592 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
593
594 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
595 True.getValueType(), True, FCC0, False, Cond);
596}
597
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000598static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000599 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000600 const MipsSubtarget &Subtarget) {
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000601 if (DCI.isBeforeLegalizeOps())
602 return SDValue();
603
604 SDValue SetCC = N->getOperand(0);
605
606 if ((SetCC.getOpcode() != ISD::SETCC) ||
607 !SetCC.getOperand(0).getValueType().isInteger())
608 return SDValue();
609
610 SDValue False = N->getOperand(2);
611 EVT FalseTy = False.getValueType();
612
613 if (!FalseTy.isInteger())
614 return SDValue();
615
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000616 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(False);
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000617
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000618 // If the RHS (False) is 0, we swap the order of the operands
619 // of ISD::SELECT (obviously also inverting the condition) so that we can
620 // take advantage of conditional moves using the $0 register.
621 // Example:
622 // return (a != 0) ? x : 0;
623 // load $reg, x
624 // movz $reg, $0, a
625 if (!FalseC)
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000626 return SDValue();
627
Andrew Trickef9de2a2013-05-25 02:42:55 +0000628 const SDLoc DL(N);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000629
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000630 if (!FalseC->getZExtValue()) {
631 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
632 SDValue True = N->getOperand(1);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000633
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000634 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
635 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
636
637 return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True);
638 }
639
Matheus Almeidaa6beac12013-12-05 12:07:05 +0000640 // If both operands are integer constants there's a possibility that we
641 // can do some interesting optimizations.
642 SDValue True = N->getOperand(1);
643 ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(True);
644
645 if (!TrueC || !True.getValueType().isInteger())
646 return SDValue();
647
Vasileios Kalintiris36901dd2016-03-01 20:25:43 +0000648 // We'll also ignore MVT::i64 operands as this optimizations proves
649 // to be ineffective because of the required sign extensions as the result
650 // of a SETCC operator is always MVT::i32 for non-vector types.
651 if (True.getValueType() == MVT::i64)
652 return SDValue();
653
Matheus Almeidaa6beac12013-12-05 12:07:05 +0000654 int64_t Diff = TrueC->getSExtValue() - FalseC->getSExtValue();
655
656 // 1) (a < x) ? y : y-1
657 // slti $reg1, a, x
658 // addiu $reg2, $reg1, y-1
Vasileios Kalintiris36901dd2016-03-01 20:25:43 +0000659 if (Diff == 1)
660 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, False);
Matheus Almeidaa6beac12013-12-05 12:07:05 +0000661
662 // 2) (a < x) ? y-1 : y
663 // slti $reg1, a, x
664 // xor $reg1, $reg1, 1
665 // addiu $reg2, $reg1, y-1
666 if (Diff == -1) {
667 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
668 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
669 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
670 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, True);
671 }
672
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000673 // Couldn't optimize.
674 return SDValue();
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000675}
676
Vasileios Kalintirise741eb22015-03-02 12:47:32 +0000677static SDValue performCMovFPCombine(SDNode *N, SelectionDAG &DAG,
678 TargetLowering::DAGCombinerInfo &DCI,
679 const MipsSubtarget &Subtarget) {
680 if (DCI.isBeforeLegalizeOps())
681 return SDValue();
682
683 SDValue ValueIfTrue = N->getOperand(0), ValueIfFalse = N->getOperand(2);
684
685 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(ValueIfFalse);
686 if (!FalseC || FalseC->getZExtValue())
687 return SDValue();
688
689 // Since RHS (False) is 0, we swap the order of the True/False operands
690 // (obviously also inverting the condition) so that we can
691 // take advantage of conditional moves using the $0 register.
692 // Example:
693 // return (a != 0) ? x : 0;
694 // load $reg, x
695 // movz $reg, $0, a
696 unsigned Opc = (N->getOpcode() == MipsISD::CMovFP_T) ? MipsISD::CMovFP_F :
697 MipsISD::CMovFP_T;
698
699 SDValue FCC = N->getOperand(1), Glue = N->getOperand(3);
Vasileios Kalintiris2ef28882015-03-04 12:10:18 +0000700 return DAG.getNode(Opc, SDLoc(N), ValueIfFalse.getValueType(),
701 ValueIfFalse, FCC, ValueIfTrue, Glue);
Vasileios Kalintirise741eb22015-03-02 12:47:32 +0000702}
703
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000704static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000705 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000706 const MipsSubtarget &Subtarget) {
Eric Christopher1c29a652014-07-18 22:55:25 +0000707 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasExtractInsert())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000708 return SDValue();
709
Petar Jovanovicb71386a2017-03-15 13:10:08 +0000710 SDValue FirstOperand = N->getOperand(0);
711 unsigned FirstOperandOpc = FirstOperand.getOpcode();
712 SDValue Mask = N->getOperand(1);
713 EVT ValTy = N->getValueType(0);
714 SDLoc DL(N);
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000715
Petar Jovanovicb71386a2017-03-15 13:10:08 +0000716 uint64_t Pos = 0, SMPos, SMSize;
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000717 ConstantSDNode *CN;
Petar Jovanovicb71386a2017-03-15 13:10:08 +0000718 SDValue NewOperand;
719 unsigned Opc;
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000720
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000721 // Op's second operand must be a shifted mask.
722 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000723 !isShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000724 return SDValue();
725
Petar Jovanovicb71386a2017-03-15 13:10:08 +0000726 if (FirstOperandOpc == ISD::SRA || FirstOperandOpc == ISD::SRL) {
727 // Pattern match EXT.
728 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
729 // => ext $dst, $src, pos, size
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000730
Petar Jovanovicb71386a2017-03-15 13:10:08 +0000731 // The second operand of the shift must be an immediate.
732 if (!(CN = dyn_cast<ConstantSDNode>(FirstOperand.getOperand(1))))
733 return SDValue();
734
735 Pos = CN->getZExtValue();
736
737 // Return if the shifted mask does not start at bit 0 or the sum of its size
738 // and Pos exceeds the word's size.
739 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
740 return SDValue();
741
742 Opc = MipsISD::Ext;
743 NewOperand = FirstOperand.getOperand(0);
744 } else if (FirstOperandOpc == ISD::SHL && Subtarget.hasCnMips()) {
745 // Pattern match CINS.
746 // $dst = and (shl $src , pos), mask
747 // => cins $dst, $src, pos, size
748 // mask is a shifted mask with consecutive 1's, pos = shift amount,
749 // size = population count.
750
751 // The second operand of the shift must be an immediate.
752 if (!(CN = dyn_cast<ConstantSDNode>(FirstOperand.getOperand(1))))
753 return SDValue();
754
755 Pos = CN->getZExtValue();
756
757 if (SMPos != Pos || Pos >= ValTy.getSizeInBits() || SMSize >= 32 ||
758 Pos + SMSize > ValTy.getSizeInBits())
759 return SDValue();
760
761 NewOperand = FirstOperand.getOperand(0);
762 // SMSize is 'location' (position) in this case, not size.
763 SMSize--;
764 Opc = MipsISD::CIns;
765 } else {
766 // Pattern match EXT.
767 // $dst = and $src, (2**size - 1) , if size > 16
768 // => ext $dst, $src, pos, size , pos = 0
769
770 // If the mask is <= 0xffff, andi can be used instead.
771 if (CN->getZExtValue() <= 0xffff)
772 return SDValue();
773
774 // Return if the mask doesn't start at position 0.
775 if (SMPos)
776 return SDValue();
777
778 Opc = MipsISD::Ext;
779 NewOperand = FirstOperand;
780 }
781 return DAG.getNode(Opc, DL, ValTy, NewOperand,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000782 DAG.getConstant(Pos, DL, MVT::i32),
783 DAG.getConstant(SMSize, DL, MVT::i32));
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000784}
Jia Liuf54f60f2012-02-28 07:46:26 +0000785
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000786static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000787 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000788 const MipsSubtarget &Subtarget) {
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000789 // Pattern match INS.
790 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
Jia Liuf54f60f2012-02-28 07:46:26 +0000791 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000792 // => ins $dst, $src, size, pos, $src1
Eric Christopher1c29a652014-07-18 22:55:25 +0000793 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasExtractInsert())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000794 return SDValue();
795
796 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
797 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
798 ConstantSDNode *CN;
799
800 // See if Op's first operand matches (and $src1 , mask0).
801 if (And0.getOpcode() != ISD::AND)
802 return SDValue();
803
804 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000805 !isShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000806 return SDValue();
807
808 // See if Op's second operand matches (and (shl $src, pos), mask1).
809 if (And1.getOpcode() != ISD::AND)
810 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000811
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000812 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000813 !isShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000814 return SDValue();
815
816 // The shift masks must have the same position and size.
817 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
818 return SDValue();
819
820 SDValue Shl = And1.getOperand(0);
821 if (Shl.getOpcode() != ISD::SHL)
822 return SDValue();
823
824 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
825 return SDValue();
826
827 unsigned Shamt = CN->getZExtValue();
828
829 // Return if the shift amount and the first bit position of mask are not the
Jia Liuf54f60f2012-02-28 07:46:26 +0000830 // same.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000831 EVT ValTy = N->getValueType(0);
832 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000833 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000834
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000835 SDLoc DL(N);
836 return DAG.getNode(MipsISD::Ins, DL, ValTy, Shl.getOperand(0),
837 DAG.getConstant(SMPos0, DL, MVT::i32),
838 DAG.getConstant(SMSize0, DL, MVT::i32),
839 And0.getOperand(0));
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000840}
Jia Liuf54f60f2012-02-28 07:46:26 +0000841
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000842static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000843 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000844 const MipsSubtarget &Subtarget) {
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000845 // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt))
846
847 if (DCI.isBeforeLegalizeOps())
848 return SDValue();
849
850 SDValue Add = N->getOperand(1);
851
852 if (Add.getOpcode() != ISD::ADD)
853 return SDValue();
854
855 SDValue Lo = Add.getOperand(1);
856
857 if ((Lo.getOpcode() != MipsISD::Lo) ||
858 (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable))
859 return SDValue();
860
861 EVT ValTy = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000862 SDLoc DL(N);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000863
864 SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, N->getOperand(0),
865 Add.getOperand(0));
866 return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo);
867}
868
Vasileios Kalintiris3751d412016-04-13 15:07:45 +0000869static SDValue performAssertZextCombine(SDNode *N, SelectionDAG &DAG,
870 TargetLowering::DAGCombinerInfo &DCI,
871 const MipsSubtarget &Subtarget) {
872 SDValue N0 = N->getOperand(0);
873 EVT NarrowerVT = cast<VTSDNode>(N->getOperand(1))->getVT();
874
875 if (N0.getOpcode() != ISD::TRUNCATE)
876 return SDValue();
877
878 if (N0.getOperand(0).getOpcode() != ISD::AssertZext)
879 return SDValue();
880
881 // fold (AssertZext (trunc (AssertZext x))) -> (trunc (AssertZext x))
882 // if the type of the extension of the innermost AssertZext node is
883 // smaller from that of the outermost node, eg:
884 // (AssertZext:i32 (trunc:i32 (AssertZext:i64 X, i32)), i8)
885 // -> (trunc:i32 (AssertZext X, i8))
886 SDValue WiderAssertZext = N0.getOperand(0);
887 EVT WiderVT = cast<VTSDNode>(WiderAssertZext->getOperand(1))->getVT();
888
889 if (NarrowerVT.bitsLT(WiderVT)) {
890 SDValue NewAssertZext = DAG.getNode(
891 ISD::AssertZext, SDLoc(N), WiderAssertZext.getValueType(),
892 WiderAssertZext.getOperand(0), DAG.getValueType(NarrowerVT));
893 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0),
894 NewAssertZext);
895 }
896
897 return SDValue();
898}
899
Petar Jovanovicb71386a2017-03-15 13:10:08 +0000900
901static SDValue performSHLCombine(SDNode *N, SelectionDAG &DAG,
902 TargetLowering::DAGCombinerInfo &DCI,
903 const MipsSubtarget &Subtarget) {
904 // Pattern match CINS.
905 // $dst = shl (and $src , imm), pos
906 // => cins $dst, $src, pos, size
907
908 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasCnMips())
909 return SDValue();
910
911 SDValue FirstOperand = N->getOperand(0);
912 unsigned FirstOperandOpc = FirstOperand.getOpcode();
913 SDValue SecondOperand = N->getOperand(1);
914 EVT ValTy = N->getValueType(0);
915 SDLoc DL(N);
916
917 uint64_t Pos = 0, SMPos, SMSize;
918 ConstantSDNode *CN;
919 SDValue NewOperand;
920
921 // The second operand of the shift must be an immediate.
922 if (!(CN = dyn_cast<ConstantSDNode>(SecondOperand)))
923 return SDValue();
924
925 Pos = CN->getZExtValue();
926
927 if (Pos >= ValTy.getSizeInBits())
928 return SDValue();
929
930 if (FirstOperandOpc != ISD::AND)
931 return SDValue();
932
933 // AND's second operand must be a shifted mask.
934 if (!(CN = dyn_cast<ConstantSDNode>(FirstOperand.getOperand(1))) ||
935 !isShiftedMask(CN->getZExtValue(), SMPos, SMSize))
936 return SDValue();
937
938 // Return if the shifted mask does not start at bit 0 or the sum of its size
939 // and Pos exceeds the word's size.
940 if (SMPos != 0 || SMSize > 32 || Pos + SMSize > ValTy.getSizeInBits())
941 return SDValue();
942
943 NewOperand = FirstOperand.getOperand(0);
944 // SMSize is 'location' (position) in this case, not size.
945 SMSize--;
946
947 return DAG.getNode(MipsISD::CIns, DL, ValTy, NewOperand,
948 DAG.getConstant(Pos, DL, MVT::i32),
949 DAG.getConstant(SMSize, DL, MVT::i32));
950}
951
Bruno Cardoso Lopes61a61e92011-02-10 18:05:10 +0000952SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000953 const {
954 SelectionDAG &DAG = DCI.DAG;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000955 unsigned Opc = N->getOpcode();
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000956
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000957 switch (Opc) {
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000958 default: break;
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000959 case ISD::SDIVREM:
960 case ISD::UDIVREM:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000961 return performDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000962 case ISD::SELECT:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000963 return performSELECTCombine(N, DAG, DCI, Subtarget);
Vasileios Kalintirise741eb22015-03-02 12:47:32 +0000964 case MipsISD::CMovFP_F:
965 case MipsISD::CMovFP_T:
966 return performCMovFPCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000967 case ISD::AND:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000968 return performANDCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000969 case ISD::OR:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000970 return performORCombine(N, DAG, DCI, Subtarget);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000971 case ISD::ADD:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000972 return performADDCombine(N, DAG, DCI, Subtarget);
Vasileios Kalintiris3751d412016-04-13 15:07:45 +0000973 case ISD::AssertZext:
974 return performAssertZextCombine(N, DAG, DCI, Subtarget);
Petar Jovanovicb71386a2017-03-15 13:10:08 +0000975 case ISD::SHL:
976 return performSHLCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000977 }
978
979 return SDValue();
980}
981
Sanjay Patelf7401292015-11-11 17:24:56 +0000982bool MipsTargetLowering::isCheapToSpeculateCttz() const {
983 return Subtarget.hasMips32();
984}
985
986bool MipsTargetLowering::isCheapToSpeculateCtlz() const {
987 return Subtarget.hasMips32();
988}
989
Akira Hatanakafabb8cf2012-09-21 23:58:31 +0000990void
991MipsTargetLowering::LowerOperationWrapper(SDNode *N,
992 SmallVectorImpl<SDValue> &Results,
993 SelectionDAG &DAG) const {
994 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
995
996 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
997 Results.push_back(Res.getValue(I));
998}
999
1000void
1001MipsTargetLowering::ReplaceNodeResults(SDNode *N,
1002 SmallVectorImpl<SDValue> &Results,
1003 SelectionDAG &DAG) const {
Akira Hatanaka9da442f2013-04-30 21:17:07 +00001004 return LowerOperationWrapper(N, Results, DAG);
Akira Hatanakafabb8cf2012-09-21 23:58:31 +00001005}
1006
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001007SDValue MipsTargetLowering::
Dan Gohman21cea8a2010-04-17 15:26:15 +00001008LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001009{
Wesley Peck527da1b2010-11-23 03:31:01 +00001010 switch (Op.getOpcode())
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001011 {
Simon Dardisba92b032016-09-09 11:06:01 +00001012 case ISD::BRCOND: return lowerBRCOND(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +00001013 case ISD::ConstantPool: return lowerConstantPool(Op, DAG);
1014 case ISD::GlobalAddress: return lowerGlobalAddress(Op, DAG);
1015 case ISD::BlockAddress: return lowerBlockAddress(Op, DAG);
1016 case ISD::GlobalTLSAddress: return lowerGlobalTLSAddress(Op, DAG);
1017 case ISD::JumpTable: return lowerJumpTable(Op, DAG);
Simon Dardisba92b032016-09-09 11:06:01 +00001018 case ISD::SELECT: return lowerSELECT(Op, DAG);
1019 case ISD::SETCC: return lowerSETCC(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +00001020 case ISD::VASTART: return lowerVASTART(Op, DAG);
Daniel Sanders2b553d42014-08-01 09:17:39 +00001021 case ISD::VAARG: return lowerVAARG(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +00001022 case ISD::FCOPYSIGN: return lowerFCOPYSIGN(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +00001023 case ISD::FRAMEADDR: return lowerFRAMEADDR(Op, DAG);
1024 case ISD::RETURNADDR: return lowerRETURNADDR(Op, DAG);
1025 case ISD::EH_RETURN: return lowerEH_RETURN(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +00001026 case ISD::ATOMIC_FENCE: return lowerATOMIC_FENCE(Op, DAG);
1027 case ISD::SHL_PARTS: return lowerShiftLeftParts(Op, DAG);
1028 case ISD::SRA_PARTS: return lowerShiftRightParts(Op, DAG, true);
1029 case ISD::SRL_PARTS: return lowerShiftRightParts(Op, DAG, false);
1030 case ISD::LOAD: return lowerLOAD(Op, DAG);
1031 case ISD::STORE: return lowerSTORE(Op, DAG);
Hal Finkel5081ac22016-09-01 10:28:47 +00001032 case ISD::EH_DWARF_CFA: return lowerEH_DWARF_CFA(Op, DAG);
Akira Hatanaka252f54f2013-05-16 21:17:15 +00001033 case ISD::FP_TO_SINT: return lowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001034 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001035 return SDValue();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001036}
1037
Akira Hatanakae2489122011-04-15 21:51:11 +00001038//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001039// Lower helper functions
Akira Hatanakae2489122011-04-15 21:51:11 +00001040//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001041
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001042// addLiveIn - This helper function adds the specified physical register to the
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001043// MachineFunction as a live in value. It also creates a corresponding
1044// virtual register for it.
1045static unsigned
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001046addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001047{
Chris Lattnera10fff52007-12-31 04:13:23 +00001048 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
1049 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001050 return VReg;
1051}
1052
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001053static MachineBasicBlock *insertDivByZeroTrap(MachineInstr &MI,
Daniel Sanders308181e2014-06-12 10:44:10 +00001054 MachineBasicBlock &MBB,
1055 const TargetInstrInfo &TII,
Zlatko Buljan58d6a952016-04-13 08:02:26 +00001056 bool Is64Bit, bool IsMicroMips) {
Akira Hatanaka1cb02422013-05-20 18:07:43 +00001057 if (NoZeroDivCheck)
1058 return &MBB;
1059
1060 // Insert instruction "teq $divisor_reg, $zero, 7".
1061 MachineBasicBlock::iterator I(MI);
1062 MachineInstrBuilder MIB;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001063 MachineOperand &Divisor = MI.getOperand(2);
1064 MIB = BuildMI(MBB, std::next(I), MI.getDebugLoc(),
Zlatko Buljan58d6a952016-04-13 08:02:26 +00001065 TII.get(IsMicroMips ? Mips::TEQ_MM : Mips::TEQ))
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001066 .addReg(Divisor.getReg(), getKillRegState(Divisor.isKill()))
1067 .addReg(Mips::ZERO)
1068 .addImm(7);
Akira Hatanaka1cb02422013-05-20 18:07:43 +00001069
1070 // Use the 32-bit sub-register if this is a 64-bit division.
1071 if (Is64Bit)
1072 MIB->getOperand(0).setSubReg(Mips::sub_32);
1073
Akira Hatanaka86c3c792013-10-15 01:06:30 +00001074 // Clear Divisor's kill flag.
1075 Divisor.setIsKill(false);
Daniel Sanders308181e2014-06-12 10:44:10 +00001076
1077 // We would normally delete the original instruction here but in this case
1078 // we only needed to inject an additional instruction rather than replace it.
1079
Akira Hatanaka1cb02422013-05-20 18:07:43 +00001080 return &MBB;
1081}
1082
Akira Hatanakae4bd0542012-09-27 02:15:57 +00001083MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001084MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
Dan Gohman25c16532010-05-01 00:01:06 +00001085 MachineBasicBlock *BB) const {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001086 switch (MI.getOpcode()) {
Reed Kotler97ba5f22013-02-21 04:22:38 +00001087 default:
1088 llvm_unreachable("Unexpected instr type to insert");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001089 case Mips::ATOMIC_LOAD_ADD_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001090 return emitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001091 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001092 return emitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001093 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001094 return emitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001095 case Mips::ATOMIC_LOAD_ADD_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001096 return emitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001097
1098 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001099 return emitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001100 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001101 return emitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001102 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001103 return emitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001104 case Mips::ATOMIC_LOAD_AND_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001105 return emitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001106
1107 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001108 return emitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001109 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001110 return emitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001111 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001112 return emitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001113 case Mips::ATOMIC_LOAD_OR_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001114 return emitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001115
1116 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001117 return emitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001118 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001119 return emitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001120 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001121 return emitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001122 case Mips::ATOMIC_LOAD_XOR_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001123 return emitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001124
1125 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001126 return emitAtomicBinaryPartword(MI, BB, 1, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001127 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001128 return emitAtomicBinaryPartword(MI, BB, 2, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001129 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001130 return emitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001131 case Mips::ATOMIC_LOAD_NAND_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001132 return emitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001133
1134 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001135 return emitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001136 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001137 return emitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001138 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001139 return emitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001140 case Mips::ATOMIC_LOAD_SUB_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001141 return emitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001142
1143 case Mips::ATOMIC_SWAP_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001144 return emitAtomicBinaryPartword(MI, BB, 1, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001145 case Mips::ATOMIC_SWAP_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001146 return emitAtomicBinaryPartword(MI, BB, 2, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001147 case Mips::ATOMIC_SWAP_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001148 return emitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001149 case Mips::ATOMIC_SWAP_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001150 return emitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001151
Simon Dardis7577ce22017-03-09 14:03:26 +00001152 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001153 return emitAtomicCmpSwapPartword(MI, BB, 1);
Simon Dardis7577ce22017-03-09 14:03:26 +00001154 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001155 return emitAtomicCmpSwapPartword(MI, BB, 2);
Simon Dardis7577ce22017-03-09 14:03:26 +00001156 case Mips::ATOMIC_CMP_SWAP_I32:
1157 return emitAtomicCmpSwap(MI, BB, 4);
1158 case Mips::ATOMIC_CMP_SWAP_I64:
1159 return emitAtomicCmpSwap(MI, BB, 8);
Akira Hatanaka1cb02422013-05-20 18:07:43 +00001160 case Mips::PseudoSDIV:
1161 case Mips::PseudoUDIV:
Daniel Sanders308181e2014-06-12 10:44:10 +00001162 case Mips::DIV:
1163 case Mips::DIVU:
1164 case Mips::MOD:
1165 case Mips::MODU:
Zlatko Buljan58d6a952016-04-13 08:02:26 +00001166 return insertDivByZeroTrap(MI, *BB, *Subtarget.getInstrInfo(), false,
1167 false);
1168 case Mips::SDIV_MM_Pseudo:
1169 case Mips::UDIV_MM_Pseudo:
1170 case Mips::SDIV_MM:
1171 case Mips::UDIV_MM:
1172 case Mips::DIV_MMR6:
1173 case Mips::DIVU_MMR6:
1174 case Mips::MOD_MMR6:
1175 case Mips::MODU_MMR6:
1176 return insertDivByZeroTrap(MI, *BB, *Subtarget.getInstrInfo(), false, true);
Akira Hatanaka1cb02422013-05-20 18:07:43 +00001177 case Mips::PseudoDSDIV:
1178 case Mips::PseudoDUDIV:
Daniel Sanders308181e2014-06-12 10:44:10 +00001179 case Mips::DDIV:
1180 case Mips::DDIVU:
1181 case Mips::DMOD:
1182 case Mips::DMODU:
Zlatko Buljan58d6a952016-04-13 08:02:26 +00001183 return insertDivByZeroTrap(MI, *BB, *Subtarget.getInstrInfo(), true, false);
1184 case Mips::DDIV_MM64R6:
1185 case Mips::DDIVU_MM64R6:
1186 case Mips::DMOD_MM64R6:
1187 case Mips::DMODU_MM64R6:
1188 return insertDivByZeroTrap(MI, *BB, *Subtarget.getInstrInfo(), true, true);
Daniel Sanders0fa60412014-06-12 13:39:06 +00001189 case Mips::SEL_D:
Zlatko Buljancd242c12016-06-09 11:15:53 +00001190 case Mips::SEL_D_MMR6:
Daniel Sanders0fa60412014-06-12 13:39:06 +00001191 return emitSEL_D(MI, BB);
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001192
1193 case Mips::PseudoSELECT_I:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001194 case Mips::PseudoSELECT_I64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001195 case Mips::PseudoSELECT_S:
1196 case Mips::PseudoSELECT_D32:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001197 case Mips::PseudoSELECT_D64:
Vasileios Kalintiris36901dd2016-03-01 20:25:43 +00001198 return emitPseudoSELECT(MI, BB, false, Mips::BNE);
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001199 case Mips::PseudoSELECTFP_F_I:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001200 case Mips::PseudoSELECTFP_F_I64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001201 case Mips::PseudoSELECTFP_F_S:
1202 case Mips::PseudoSELECTFP_F_D32:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001203 case Mips::PseudoSELECTFP_F_D64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001204 return emitPseudoSELECT(MI, BB, true, Mips::BC1F);
1205 case Mips::PseudoSELECTFP_T_I:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001206 case Mips::PseudoSELECTFP_T_I64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001207 case Mips::PseudoSELECTFP_T_S:
1208 case Mips::PseudoSELECTFP_T_D32:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001209 case Mips::PseudoSELECTFP_T_D64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001210 return emitPseudoSELECT(MI, BB, true, Mips::BC1T);
Akira Hatanakaa5352702011-03-31 18:26:17 +00001211 }
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001212}
1213
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001214// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
1215// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001216MachineBasicBlock *MipsTargetLowering::emitAtomicBinary(MachineInstr &MI,
1217 MachineBasicBlock *BB,
1218 unsigned Size,
1219 unsigned BinOpcode,
1220 bool Nand) const {
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001221 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001222
1223 MachineFunction *MF = BB->getParent();
1224 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001225 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Eric Christopher96e72c62015-01-29 23:27:36 +00001226 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001227 const bool ArePtrs64bit = ABI.ArePtrs64bit();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001228 DebugLoc DL = MI.getDebugLoc();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001229 unsigned LL, SC, AND, NOR, ZERO, BEQ;
1230
1231 if (Size == 4) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001232 if (isMicroMips) {
1233 LL = Mips::LL_MM;
1234 SC = Mips::SC_MM;
1235 } else {
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001236 LL = Subtarget.hasMips32r6()
1237 ? (ArePtrs64bit ? Mips::LL64_R6 : Mips::LL_R6)
1238 : (ArePtrs64bit ? Mips::LL64 : Mips::LL);
1239 SC = Subtarget.hasMips32r6()
1240 ? (ArePtrs64bit ? Mips::SC64_R6 : Mips::SC_R6)
1241 : (ArePtrs64bit ? Mips::SC64 : Mips::SC);
Daniel Sanders6a803f62014-06-16 13:13:03 +00001242 }
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001243
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001244 AND = Mips::AND;
1245 NOR = Mips::NOR;
1246 ZERO = Mips::ZERO;
1247 BEQ = Mips::BEQ;
Daniel Sanders6a803f62014-06-16 13:13:03 +00001248 } else {
Daniel Sandersbdcfab12014-07-24 09:47:14 +00001249 LL = Subtarget.hasMips64r6() ? Mips::LLD_R6 : Mips::LLD;
1250 SC = Subtarget.hasMips64r6() ? Mips::SCD_R6 : Mips::SCD;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001251 AND = Mips::AND64;
1252 NOR = Mips::NOR64;
1253 ZERO = Mips::ZERO_64;
1254 BEQ = Mips::BEQ64;
1255 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001256
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001257 unsigned OldVal = MI.getOperand(0).getReg();
1258 unsigned Ptr = MI.getOperand(1).getReg();
1259 unsigned Incr = MI.getOperand(2).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001260
Akira Hatanaka0e019592011-07-19 20:11:17 +00001261 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1262 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1263 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001264
1265 // insert new blocks after the current block
1266 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1267 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1268 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +00001269 MachineFunction::iterator It = ++BB->getIterator();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001270 MF->insert(It, loopMBB);
1271 MF->insert(It, exitMBB);
1272
1273 // Transfer the remainder of BB and its successor edges to exitMBB.
1274 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001275 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001276 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1277
1278 // thisMBB:
1279 // ...
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001280 // fallthrough --> loopMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001281 BB->addSuccessor(loopMBB);
Akira Hatanaka08636b42011-07-19 17:09:53 +00001282 loopMBB->addSuccessor(loopMBB);
1283 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001284
1285 // loopMBB:
1286 // ll oldval, 0(ptr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001287 // <binop> storeval, oldval, incr
1288 // sc success, storeval, 0(ptr)
1289 // beq success, $0, loopMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001290 BB = loopMBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001291 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001292 if (Nand) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001293 // and andres, oldval, incr
1294 // nor storeval, $0, andres
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001295 BuildMI(BB, DL, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
1296 BuildMI(BB, DL, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001297 } else if (BinOpcode) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001298 // <binop> storeval, oldval, incr
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001299 BuildMI(BB, DL, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001300 } else {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001301 StoreVal = Incr;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001302 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001303 BuildMI(BB, DL, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
1304 BuildMI(BB, DL, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001305
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001306 MI.eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001307
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001308 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001309}
1310
Daniel Sanders6a803f62014-06-16 13:13:03 +00001311MachineBasicBlock *MipsTargetLowering::emitSignExtendToI32InReg(
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001312 MachineInstr &MI, MachineBasicBlock *BB, unsigned Size, unsigned DstReg,
Daniel Sanders6a803f62014-06-16 13:13:03 +00001313 unsigned SrcReg) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00001314 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001315 const DebugLoc &DL = MI.getDebugLoc();
Daniel Sanders6a803f62014-06-16 13:13:03 +00001316
Eric Christopher1c29a652014-07-18 22:55:25 +00001317 if (Subtarget.hasMips32r2() && Size == 1) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001318 BuildMI(BB, DL, TII->get(Mips::SEB), DstReg).addReg(SrcReg);
1319 return BB;
1320 }
1321
Eric Christopher1c29a652014-07-18 22:55:25 +00001322 if (Subtarget.hasMips32r2() && Size == 2) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001323 BuildMI(BB, DL, TII->get(Mips::SEH), DstReg).addReg(SrcReg);
1324 return BB;
1325 }
1326
1327 MachineFunction *MF = BB->getParent();
1328 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1329 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1330 unsigned ScrReg = RegInfo.createVirtualRegister(RC);
1331
1332 assert(Size < 32);
1333 int64_t ShiftImm = 32 - (Size * 8);
1334
1335 BuildMI(BB, DL, TII->get(Mips::SLL), ScrReg).addReg(SrcReg).addImm(ShiftImm);
1336 BuildMI(BB, DL, TII->get(Mips::SRA), DstReg).addReg(ScrReg).addImm(ShiftImm);
1337
1338 return BB;
1339}
1340
1341MachineBasicBlock *MipsTargetLowering::emitAtomicBinaryPartword(
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001342 MachineInstr &MI, MachineBasicBlock *BB, unsigned Size, unsigned BinOpcode,
Daniel Sanders6a803f62014-06-16 13:13:03 +00001343 bool Nand) const {
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001344 assert((Size == 1 || Size == 2) &&
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001345 "Unsupported size for EmitAtomicBinaryPartial.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001346
1347 MachineFunction *MF = BB->getParent();
1348 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1349 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001350 const bool ArePtrs64bit = ABI.ArePtrs64bit();
Simon Dardisa2d8cc32016-04-28 16:26:43 +00001351 const TargetRegisterClass *RCp =
1352 getRegClassFor(ArePtrs64bit ? MVT::i64 : MVT::i32);
Eric Christopher96e72c62015-01-29 23:27:36 +00001353 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001354 DebugLoc DL = MI.getDebugLoc();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001355
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001356 unsigned Dest = MI.getOperand(0).getReg();
1357 unsigned Ptr = MI.getOperand(1).getReg();
1358 unsigned Incr = MI.getOperand(2).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001359
Simon Dardisa2d8cc32016-04-28 16:26:43 +00001360 unsigned AlignedAddr = RegInfo.createVirtualRegister(RCp);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001361 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001362 unsigned Mask = RegInfo.createVirtualRegister(RC);
1363 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001364 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1365 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001366 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Simon Dardisa2d8cc32016-04-28 16:26:43 +00001367 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RCp);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001368 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1369 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1370 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1371 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka9663dd32011-07-19 20:56:53 +00001372 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001373 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1374 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1375 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001376 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001377
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001378 unsigned LL, SC;
1379 if (isMicroMips) {
1380 LL = Mips::LL_MM;
1381 SC = Mips::SC_MM;
1382 } else {
1383 LL = Subtarget.hasMips32r6() ? (ArePtrs64bit ? Mips::LL64_R6 : Mips::LL_R6)
1384 : (ArePtrs64bit ? Mips::LL64 : Mips::LL);
1385 SC = Subtarget.hasMips32r6() ? (ArePtrs64bit ? Mips::SC64_R6 : Mips::SC_R6)
1386 : (ArePtrs64bit ? Mips::SC64 : Mips::SC);
1387 }
1388
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001389 // insert new blocks after the current block
1390 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1391 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001392 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001393 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +00001394 MachineFunction::iterator It = ++BB->getIterator();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001395 MF->insert(It, loopMBB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001396 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001397 MF->insert(It, exitMBB);
1398
1399 // Transfer the remainder of BB and its successor edges to exitMBB.
1400 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001401 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001402 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1403
Akira Hatanaka08636b42011-07-19 17:09:53 +00001404 BB->addSuccessor(loopMBB);
1405 loopMBB->addSuccessor(loopMBB);
1406 loopMBB->addSuccessor(sinkMBB);
1407 sinkMBB->addSuccessor(exitMBB);
1408
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001409 // thisMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001410 // addiu masklsb2,$0,-4 # 0xfffffffc
1411 // and alignedaddr,ptr,masklsb2
1412 // andi ptrlsb2,ptr,3
1413 // sll shiftamt,ptrlsb2,3
1414 // ori maskupper,$0,255 # 0xff
1415 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001416 // nor mask2,$0,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001417 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001418
1419 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Simon Dardisa2d8cc32016-04-28 16:26:43 +00001420 BuildMI(BB, DL, TII->get(ABI.GetPtrAddiuOp()), MaskLSB2)
1421 .addReg(ABI.GetNullPtr()).addImm(-4);
1422 BuildMI(BB, DL, TII->get(ABI.GetPtrAndOp()), AlignedAddr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001423 .addReg(Ptr).addReg(MaskLSB2);
Simon Dardisa2d8cc32016-04-28 16:26:43 +00001424 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2)
1425 .addReg(Ptr, 0, ArePtrs64bit ? Mips::sub_32 : 0).addImm(3);
Eric Christopher1c29a652014-07-18 22:55:25 +00001426 if (Subtarget.isLittle()) {
Akira Hatanaka2bf97332013-05-31 03:25:44 +00001427 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1428 } else {
1429 unsigned Off = RegInfo.createVirtualRegister(RC);
1430 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1431 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1432 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1433 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001434 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001435 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001436 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001437 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001438 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001439 BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(Incr).addReg(ShiftAmt);
Bruno Cardoso Lopesf771a0f2011-05-31 20:25:26 +00001440
Akira Hatanaka27292632011-07-18 18:52:12 +00001441 // atomic.load.binop
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001442 // loopMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001443 // ll oldval,0(alignedaddr)
1444 // binop binopres,oldval,incr2
1445 // and newval,binopres,mask
1446 // and maskedoldval0,oldval,mask2
1447 // or storeval,maskedoldval0,newval
1448 // sc success,storeval,0(alignedaddr)
1449 // beq success,$0,loopMBB
1450
Akira Hatanaka27292632011-07-18 18:52:12 +00001451 // atomic.swap
1452 // loopMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001453 // ll oldval,0(alignedaddr)
Akira Hatanakae4503582011-07-19 18:14:26 +00001454 // and newval,incr2,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001455 // and maskedoldval0,oldval,mask2
1456 // or storeval,maskedoldval0,newval
1457 // sc success,storeval,0(alignedaddr)
1458 // beq success,$0,loopMBB
Akira Hatanaka27292632011-07-18 18:52:12 +00001459
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001460 BB = loopMBB;
Jozef Kolek2f27d572014-12-18 16:39:29 +00001461 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001462 if (Nand) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001463 // and andres, oldval, incr2
1464 // nor binopres, $0, andres
1465 // and newval, binopres, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001466 BuildMI(BB, DL, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1467 BuildMI(BB, DL, TII->get(Mips::NOR), BinOpRes)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001468 .addReg(Mips::ZERO).addReg(AndRes);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001469 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001470 } else if (BinOpcode) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001471 // <binop> binopres, oldval, incr2
1472 // and newval, binopres, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001473 BuildMI(BB, DL, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1474 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001475 } else { // atomic.swap
Akira Hatanaka0e019592011-07-19 20:11:17 +00001476 // and newval, incr2, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001477 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanakae4503582011-07-19 18:14:26 +00001478 }
Jia Liuf54f60f2012-02-28 07:46:26 +00001479
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001480 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001481 .addReg(OldVal).addReg(Mask2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001482 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka9663dd32011-07-19 20:56:53 +00001483 .addReg(MaskedOldVal0).addReg(NewVal);
Jozef Kolek2f27d572014-12-18 16:39:29 +00001484 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001485 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001486 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001487 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001488
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001489 // sinkMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001490 // and maskedoldval1,oldval,mask
1491 // srl srlres,maskedoldval1,shiftamt
Daniel Sanders6a803f62014-06-16 13:13:03 +00001492 // sign_extend dest,srlres
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001493 BB = sinkMBB;
Akira Hatanakae97bd812011-07-19 03:14:58 +00001494
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001495 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001496 .addReg(OldVal).addReg(Mask);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001497 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001498 .addReg(MaskedOldVal1).addReg(ShiftAmt);
Daniel Sanders6a803f62014-06-16 13:13:03 +00001499 BB = emitSignExtendToI32InReg(MI, BB, Size, Dest, SrlRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001500
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001501 MI.eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001502
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001503 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001504}
1505
Simon Dardis7577ce22017-03-09 14:03:26 +00001506MachineBasicBlock *MipsTargetLowering::emitAtomicCmpSwap(MachineInstr &MI,
1507 MachineBasicBlock *BB,
1508 unsigned Size) const {
1509 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
1510
1511 MachineFunction *MF = BB->getParent();
1512 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1513 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
1514 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
1515 const bool ArePtrs64bit = ABI.ArePtrs64bit();
1516 DebugLoc DL = MI.getDebugLoc();
1517 unsigned LL, SC, ZERO, BNE, BEQ;
1518
1519 if (Size == 4) {
1520 if (isMicroMips) {
1521 LL = Mips::LL_MM;
1522 SC = Mips::SC_MM;
1523 } else {
1524 LL = Subtarget.hasMips32r6()
1525 ? (ArePtrs64bit ? Mips::LL64_R6 : Mips::LL_R6)
1526 : (ArePtrs64bit ? Mips::LL64 : Mips::LL);
1527 SC = Subtarget.hasMips32r6()
1528 ? (ArePtrs64bit ? Mips::SC64_R6 : Mips::SC_R6)
1529 : (ArePtrs64bit ? Mips::SC64 : Mips::SC);
1530 }
1531
1532 ZERO = Mips::ZERO;
1533 BNE = Mips::BNE;
1534 BEQ = Mips::BEQ;
1535 } else {
1536 LL = Subtarget.hasMips64r6() ? Mips::LLD_R6 : Mips::LLD;
1537 SC = Subtarget.hasMips64r6() ? Mips::SCD_R6 : Mips::SCD;
1538 ZERO = Mips::ZERO_64;
1539 BNE = Mips::BNE64;
1540 BEQ = Mips::BEQ64;
1541 }
1542
1543 unsigned Dest = MI.getOperand(0).getReg();
1544 unsigned Ptr = MI.getOperand(1).getReg();
1545 unsigned OldVal = MI.getOperand(2).getReg();
1546 unsigned NewVal = MI.getOperand(3).getReg();
1547
1548 unsigned Success = RegInfo.createVirtualRegister(RC);
1549
1550 // insert new blocks after the current block
1551 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1552 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1553 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1554 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1555 MachineFunction::iterator It = ++BB->getIterator();
1556 MF->insert(It, loop1MBB);
1557 MF->insert(It, loop2MBB);
1558 MF->insert(It, exitMBB);
1559
1560 // Transfer the remainder of BB and its successor edges to exitMBB.
1561 exitMBB->splice(exitMBB->begin(), BB,
1562 std::next(MachineBasicBlock::iterator(MI)), BB->end());
1563 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1564
1565 // thisMBB:
1566 // ...
1567 // fallthrough --> loop1MBB
1568 BB->addSuccessor(loop1MBB);
1569 loop1MBB->addSuccessor(exitMBB);
1570 loop1MBB->addSuccessor(loop2MBB);
1571 loop2MBB->addSuccessor(loop1MBB);
1572 loop2MBB->addSuccessor(exitMBB);
1573
1574 // loop1MBB:
1575 // ll dest, 0(ptr)
1576 // bne dest, oldval, exitMBB
1577 BB = loop1MBB;
1578 BuildMI(BB, DL, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1579 BuildMI(BB, DL, TII->get(BNE))
1580 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
1581
1582 // loop2MBB:
1583 // sc success, newval, 0(ptr)
1584 // beq success, $0, loop1MBB
1585 BB = loop2MBB;
1586 BuildMI(BB, DL, TII->get(SC), Success)
1587 .addReg(NewVal).addReg(Ptr).addImm(0);
1588 BuildMI(BB, DL, TII->get(BEQ))
1589 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
1590
1591 MI.eraseFromParent(); // The instruction is gone now.
1592
1593 return exitMBB;
1594}
1595
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001596MachineBasicBlock *MipsTargetLowering::emitAtomicCmpSwapPartword(
1597 MachineInstr &MI, MachineBasicBlock *BB, unsigned Size) const {
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001598 assert((Size == 1 || Size == 2) &&
1599 "Unsupported size for EmitAtomicCmpSwapPartial.");
1600
1601 MachineFunction *MF = BB->getParent();
1602 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1603 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001604 const bool ArePtrs64bit = ABI.ArePtrs64bit();
Zoran Jovanovic2f6845b2016-04-13 16:02:25 +00001605 const TargetRegisterClass *RCp =
1606 getRegClassFor(ArePtrs64bit ? MVT::i64 : MVT::i32);
Eric Christopher96e72c62015-01-29 23:27:36 +00001607 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001608 DebugLoc DL = MI.getDebugLoc();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001609
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001610 unsigned Dest = MI.getOperand(0).getReg();
1611 unsigned Ptr = MI.getOperand(1).getReg();
1612 unsigned CmpVal = MI.getOperand(2).getReg();
1613 unsigned NewVal = MI.getOperand(3).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001614
Zoran Jovanovic2f6845b2016-04-13 16:02:25 +00001615 unsigned AlignedAddr = RegInfo.createVirtualRegister(RCp);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001616 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001617 unsigned Mask = RegInfo.createVirtualRegister(RC);
1618 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001619 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
Simon Dardis7577ce22017-03-09 14:03:26 +00001620 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1621 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001622 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
Zoran Jovanovic2f6845b2016-04-13 16:02:25 +00001623 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RCp);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001624 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1625 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1626 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1627 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
Simon Dardis7577ce22017-03-09 14:03:26 +00001628 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1629 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1630 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1631 unsigned Success = RegInfo.createVirtualRegister(RC);
1632 unsigned LL, SC;
1633
1634 if (isMicroMips) {
1635 LL = Mips::LL_MM;
1636 SC = Mips::SC_MM;
1637 } else {
1638 LL = Subtarget.hasMips32r6() ? (ArePtrs64bit ? Mips::LL64_R6 : Mips::LL_R6)
1639 : (ArePtrs64bit ? Mips::LL64 : Mips::LL);
1640 SC = Subtarget.hasMips32r6() ? (ArePtrs64bit ? Mips::SC64_R6 : Mips::SC_R6)
1641 : (ArePtrs64bit ? Mips::SC64 : Mips::SC);
1642 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001643
1644 // insert new blocks after the current block
1645 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Simon Dardis7577ce22017-03-09 14:03:26 +00001646 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1647 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1648 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001649 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +00001650 MachineFunction::iterator It = ++BB->getIterator();
Simon Dardis7577ce22017-03-09 14:03:26 +00001651 MF->insert(It, loop1MBB);
1652 MF->insert(It, loop2MBB);
1653 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001654 MF->insert(It, exitMBB);
1655
1656 // Transfer the remainder of BB and its successor edges to exitMBB.
1657 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001658 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001659 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1660
Simon Dardis7577ce22017-03-09 14:03:26 +00001661 BB->addSuccessor(loop1MBB);
1662 loop1MBB->addSuccessor(sinkMBB);
1663 loop1MBB->addSuccessor(loop2MBB);
1664 loop2MBB->addSuccessor(loop1MBB);
1665 loop2MBB->addSuccessor(sinkMBB);
1666 sinkMBB->addSuccessor(exitMBB);
Akira Hatanaka08636b42011-07-19 17:09:53 +00001667
Akira Hatanakae4503582011-07-19 18:14:26 +00001668 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001669 // thisMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001670 // addiu masklsb2,$0,-4 # 0xfffffffc
1671 // and alignedaddr,ptr,masklsb2
1672 // andi ptrlsb2,ptr,3
Zoran Jovanovic2f6845b2016-04-13 16:02:25 +00001673 // xori ptrlsb2,ptrlsb2,3 # Only for BE
Akira Hatanaka0e019592011-07-19 20:11:17 +00001674 // sll shiftamt,ptrlsb2,3
1675 // ori maskupper,$0,255 # 0xff
1676 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001677 // nor mask2,$0,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001678 // andi maskedcmpval,cmpval,255
1679 // sll shiftedcmpval,maskedcmpval,shiftamt
1680 // andi maskednewval,newval,255
1681 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001682 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Zoran Jovanovic2f6845b2016-04-13 16:02:25 +00001683 BuildMI(BB, DL, TII->get(ArePtrs64bit ? Mips::DADDiu : Mips::ADDiu), MaskLSB2)
1684 .addReg(ABI.GetNullPtr()).addImm(-4);
1685 BuildMI(BB, DL, TII->get(ArePtrs64bit ? Mips::AND64 : Mips::AND), AlignedAddr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001686 .addReg(Ptr).addReg(MaskLSB2);
Zoran Jovanovic2f6845b2016-04-13 16:02:25 +00001687 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2)
1688 .addReg(Ptr, 0, ArePtrs64bit ? Mips::sub_32 : 0).addImm(3);
Eric Christopher1c29a652014-07-18 22:55:25 +00001689 if (Subtarget.isLittle()) {
Akira Hatanaka2bf97332013-05-31 03:25:44 +00001690 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1691 } else {
1692 unsigned Off = RegInfo.createVirtualRegister(RC);
1693 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1694 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1695 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1696 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001697 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001698 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001699 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001700 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001701 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1702 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001703 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001704 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001705 .addReg(MaskedCmpVal).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001706 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001707 .addReg(NewVal).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001708 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001709 .addReg(MaskedNewVal).addReg(ShiftAmt);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001710
Simon Dardis7577ce22017-03-09 14:03:26 +00001711 // loop1MBB:
1712 // ll oldval,0(alginedaddr)
1713 // and maskedoldval0,oldval,mask
1714 // bne maskedoldval0,shiftedcmpval,sinkMBB
1715 BB = loop1MBB;
1716 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
1717 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
1718 .addReg(OldVal).addReg(Mask);
1719 BuildMI(BB, DL, TII->get(Mips::BNE))
1720 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
1721
1722 // loop2MBB:
1723 // and maskedoldval1,oldval,mask2
1724 // or storeval,maskedoldval1,shiftednewval
1725 // sc success,storeval,0(alignedaddr)
1726 // beq success,$0,loop1MBB
1727 BB = loop2MBB;
1728 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
1729 .addReg(OldVal).addReg(Mask2);
1730 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
1731 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
1732 BuildMI(BB, DL, TII->get(SC), Success)
1733 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
1734 BuildMI(BB, DL, TII->get(Mips::BEQ))
1735 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
1736
1737 // sinkMBB:
1738 // srl srlres,maskedoldval0,shiftamt
1739 // sign_extend dest,srlres
1740 BB = sinkMBB;
1741
1742 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
1743 .addReg(MaskedOldVal0).addReg(ShiftAmt);
1744 BB = emitSignExtendToI32InReg(MI, BB, Size, Dest, SrlRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001745
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001746 MI.eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001747
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001748 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001749}
1750
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001751MachineBasicBlock *MipsTargetLowering::emitSEL_D(MachineInstr &MI,
Daniel Sanders0fa60412014-06-12 13:39:06 +00001752 MachineBasicBlock *BB) const {
1753 MachineFunction *MF = BB->getParent();
Eric Christopher96e72c62015-01-29 23:27:36 +00001754 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
1755 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders0fa60412014-06-12 13:39:06 +00001756 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001757 DebugLoc DL = MI.getDebugLoc();
Daniel Sanders0fa60412014-06-12 13:39:06 +00001758 MachineBasicBlock::iterator II(MI);
1759
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001760 unsigned Fc = MI.getOperand(1).getReg();
Daniel Sanders0fa60412014-06-12 13:39:06 +00001761 const auto &FGR64RegClass = TRI->getRegClass(Mips::FGR64RegClassID);
1762
1763 unsigned Fc2 = RegInfo.createVirtualRegister(FGR64RegClass);
1764
1765 BuildMI(*BB, II, DL, TII->get(Mips::SUBREG_TO_REG), Fc2)
1766 .addImm(0)
1767 .addReg(Fc)
1768 .addImm(Mips::sub_lo);
1769
1770 // We don't erase the original instruction, we just replace the condition
1771 // register with the 64-bit super-register.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001772 MI.getOperand(1).setReg(Fc2);
Daniel Sanders0fa60412014-06-12 13:39:06 +00001773
1774 return BB;
1775}
1776
Simon Dardisba92b032016-09-09 11:06:01 +00001777SDValue MipsTargetLowering::lowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
1778 // The first operand is the chain, the second is the condition, the third is
1779 // the block to branch to if the condition is true.
1780 SDValue Chain = Op.getOperand(0);
1781 SDValue Dest = Op.getOperand(2);
1782 SDLoc DL(Op);
1783
1784 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
1785 SDValue CondRes = createFPCmp(DAG, Op.getOperand(1));
1786
1787 // Return if flag is not set by a floating point comparison.
1788 if (CondRes.getOpcode() != MipsISD::FPCmp)
1789 return Op;
1790
1791 SDValue CCNode = CondRes.getOperand(2);
1792 Mips::CondCode CC =
1793 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
1794 unsigned Opc = invertFPCondCodeUser(CC) ? Mips::BRANCH_F : Mips::BRANCH_T;
1795 SDValue BrCode = DAG.getConstant(Opc, DL, MVT::i32);
1796 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
1797 return DAG.getNode(MipsISD::FPBrcond, DL, Op.getValueType(), Chain, BrCode,
1798 FCC0, Dest, CondRes);
1799}
1800
1801SDValue MipsTargetLowering::
1802lowerSELECT(SDValue Op, SelectionDAG &DAG) const
1803{
1804 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
1805 SDValue Cond = createFPCmp(DAG, Op.getOperand(0));
1806
1807 // Return if flag is not set by a floating point comparison.
1808 if (Cond.getOpcode() != MipsISD::FPCmp)
1809 return Op;
1810
1811 return createCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
1812 SDLoc(Op));
1813}
1814
1815SDValue MipsTargetLowering::lowerSETCC(SDValue Op, SelectionDAG &DAG) const {
1816 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
1817 SDValue Cond = createFPCmp(DAG, Op);
1818
1819 assert(Cond.getOpcode() == MipsISD::FPCmp &&
1820 "Floating point operand expected.");
1821
1822 SDLoc DL(Op);
1823 SDValue True = DAG.getConstant(1, DL, MVT::i32);
1824 SDValue False = DAG.getConstant(0, DL, MVT::i32);
1825
1826 return createCMovFP(DAG, Cond, True, False, DL);
1827}
1828
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001829SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001830 SelectionDAG &DAG) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001831 EVT Ty = Op.getValueType();
1832 GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
1833 const GlobalValue *GV = N->getGlobal();
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001834
Simon Dardisca74dd72017-01-27 11:36:52 +00001835 if (!isPositionIndependent()) {
Eric Christopher36fe0282015-02-03 07:22:52 +00001836 const MipsTargetObjectFile *TLOF =
1837 static_cast<const MipsTargetObjectFile *>(
1838 getTargetMachine().getObjFileLowering());
Peter Collingbourne67335642016-10-24 19:23:39 +00001839 const GlobalObject *GO = GV->getBaseObject();
1840 if (GO && TLOF->IsGlobalInSmallSection(GO, getTargetMachine()))
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001841 // %gp_rel relocation
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001842 return getAddrGPRel(N, SDLoc(N), Ty, DAG);
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001843
Simon Dardisca74dd72017-01-27 11:36:52 +00001844 // %hi/%lo relocation
1845 return Subtarget.hasSym32() ? getAddrNonPIC(N, SDLoc(N), Ty, DAG)
1846 // %highest/%higher/%hi/%lo relocation
1847 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG);
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001848 }
1849
Rafael Espindolab2b6a852016-06-27 12:33:33 +00001850 // Every other architecture would use shouldAssumeDSOLocal in here, but
1851 // mips is special.
Rafael Espindola97ca8272016-06-27 23:21:07 +00001852 // * In PIC code mips requires got loads even for local statics!
Rafael Espindolab2b6a852016-06-27 12:33:33 +00001853 // * To save on got entries, for local statics the got entry contains the
1854 // page and an additional add instruction takes care of the low bits.
1855 // * It is legal to access a hidden symbol with a non hidden undefined,
1856 // so one cannot guarantee that all access to a hidden symbol will know
1857 // it is hidden.
1858 // * Mips linkers don't support creating a page and a full got entry for
1859 // the same symbol.
1860 // * Given all that, we have to use a full got entry for hidden symbols :-(
Rafael Espindola1ac1fa82016-06-27 03:19:40 +00001861 if (GV->hasLocalLinkage())
Eric Christopher96e72c62015-01-29 23:27:36 +00001862 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001863
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00001864 if (LargeGOT)
Alex Lorenze40c8a22015-08-11 23:09:45 +00001865 return getAddrGlobalLargeGOT(
1866 N, SDLoc(N), Ty, DAG, MipsII::MO_GOT_HI16, MipsII::MO_GOT_LO16,
1867 DAG.getEntryNode(),
1868 MachinePointerInfo::getGOT(DAG.getMachineFunction()));
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00001869
Alex Lorenze40c8a22015-08-11 23:09:45 +00001870 return getAddrGlobal(
1871 N, SDLoc(N), Ty, DAG,
Daniel Sandersfe98b2f2016-05-03 13:35:44 +00001872 (ABI.IsN32() || ABI.IsN64()) ? MipsII::MO_GOT_DISP : MipsII::MO_GOT,
Alex Lorenze40c8a22015-08-11 23:09:45 +00001873 DAG.getEntryNode(), MachinePointerInfo::getGOT(DAG.getMachineFunction()));
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001874}
1875
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001876SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op,
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +00001877 SelectionDAG &DAG) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001878 BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op);
1879 EVT Ty = Op.getValueType();
Akira Hatanaka30f97cf2013-09-25 00:30:25 +00001880
Simon Dardisca74dd72017-01-27 11:36:52 +00001881 if (!isPositionIndependent())
1882 return Subtarget.hasSym32() ? getAddrNonPIC(N, SDLoc(N), Ty, DAG)
1883 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG);
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001884
Eric Christopher96e72c62015-01-29 23:27:36 +00001885 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +00001886}
1887
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001888SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001889lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001890{
Akira Hatanakabff84e12011-12-14 18:26:41 +00001891 // If the relocation model is PIC, use the General Dynamic TLS Model or
1892 // Local Dynamic TLS model, otherwise use the Initial Exec or
1893 // Local Exec TLS Model.
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001894
1895 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +00001896 if (DAG.getTarget().Options.EmulatedTLS)
1897 return LowerToTLSEmulatedModel(GA, DAG);
1898
Andrew Trickef9de2a2013-05-25 02:42:55 +00001899 SDLoc DL(GA);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001900 const GlobalValue *GV = GA->getGlobal();
Mehdi Amini44ede332015-07-09 02:09:04 +00001901 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001902
Hans Wennborgaea41202012-05-04 09:40:39 +00001903 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
1904
1905 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Hans Wennborg245917b2012-06-04 14:02:08 +00001906 // General Dynamic and Local Dynamic TLS Model.
1907 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
1908 : MipsII::MO_TLSGD;
1909
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001910 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag);
1911 SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT,
1912 getGlobalReg(DAG, PtrVT), TGA);
Akira Hatanakaf10ee842011-12-08 21:05:38 +00001913 unsigned PtrSize = PtrVT.getSizeInBits();
1914 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1915
Benjamin Kramer64ba50a2011-12-11 12:21:34 +00001916 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001917
1918 ArgListTy Args;
1919 ArgListEntry Entry;
1920 Entry.Node = Argument;
Akira Hatanakadee6c822011-12-08 20:34:32 +00001921 Entry.Ty = PtrTy;
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001922 Args.push_back(Entry);
Jia Liuf54f60f2012-02-28 07:46:26 +00001923
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00001924 TargetLowering::CallLoweringInfo CLI(DAG);
Nirav Daveac6081c2017-03-18 00:44:07 +00001925 CLI.setDebugLoc(DL)
1926 .setChain(DAG.getEntryNode())
1927 .setLibCallee(CallingConv::C, PtrTy, TlsGetAddr, std::move(Args));
Justin Holewinskiaa583972012-05-25 16:35:28 +00001928 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001929
Akira Hatanakabff84e12011-12-14 18:26:41 +00001930 SDValue Ret = CallResult.first;
1931
Hans Wennborgaea41202012-05-04 09:40:39 +00001932 if (model != TLSModel::LocalDynamic)
Akira Hatanakabff84e12011-12-14 18:26:41 +00001933 return Ret;
1934
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001935 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanakabff84e12011-12-14 18:26:41 +00001936 MipsII::MO_DTPREL_HI);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001937 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1938 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanakabff84e12011-12-14 18:26:41 +00001939 MipsII::MO_DTPREL_LO);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001940 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1941 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret);
1942 return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001943 }
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001944
1945 SDValue Offset;
Hans Wennborgaea41202012-05-04 09:40:39 +00001946 if (model == TLSModel::InitialExec) {
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001947 // Initial Exec TLS Model
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001948 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001949 MipsII::MO_GOTTPREL);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001950 TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT),
Akira Hatanakab049aef2012-02-24 22:34:47 +00001951 TGA);
Justin Lebar9c375812016-07-15 18:27:10 +00001952 Offset =
1953 DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), TGA, MachinePointerInfo());
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001954 } else {
1955 // Local Exec TLS Model
Hans Wennborgaea41202012-05-04 09:40:39 +00001956 assert(model == TLSModel::LocalExec);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001957 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001958 MipsII::MO_TPREL_HI);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001959 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001960 MipsII::MO_TPREL_LO);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001961 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1962 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1963 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001964 }
1965
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001966 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
1967 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001968}
1969
1970SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001971lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +00001972{
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001973 JumpTableSDNode *N = cast<JumpTableSDNode>(Op);
1974 EVT Ty = Op.getValueType();
Akira Hatanaka30f97cf2013-09-25 00:30:25 +00001975
Simon Dardisca74dd72017-01-27 11:36:52 +00001976 if (!isPositionIndependent())
1977 return Subtarget.hasSym32() ? getAddrNonPIC(N, SDLoc(N), Ty, DAG)
1978 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG);
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001979
Eric Christopher96e72c62015-01-29 23:27:36 +00001980 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +00001981}
1982
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001983SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001984lowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +00001985{
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001986 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
1987 EVT Ty = Op.getValueType();
Bruno Cardoso Lopes2db07582009-11-25 12:17:58 +00001988
Simon Dardisca74dd72017-01-27 11:36:52 +00001989 if (!isPositionIndependent()) {
Eric Christopher36fe0282015-02-03 07:22:52 +00001990 const MipsTargetObjectFile *TLOF =
1991 static_cast<const MipsTargetObjectFile *>(
1992 getTargetMachine().getObjFileLowering());
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001993
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001994 if (TLOF->IsConstantInSmallSection(DAG.getDataLayout(), N->getConstVal(),
1995 getTargetMachine()))
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001996 // %gp_rel relocation
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001997 return getAddrGPRel(N, SDLoc(N), Ty, DAG);
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001998
Simon Dardisca74dd72017-01-27 11:36:52 +00001999 return Subtarget.hasSym32() ? getAddrNonPIC(N, SDLoc(N), Ty, DAG)
2000 : getAddrNonPICSym64(N, SDLoc(N), Ty, DAG);
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00002001 }
Bruno Cardoso Lopesfdb4cec2008-07-23 16:01:50 +00002002
Simon Dardisca74dd72017-01-27 11:36:52 +00002003 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +00002004}
2005
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002006SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman31ae5862010-04-17 14:41:14 +00002007 MachineFunction &MF = DAG.getMachineFunction();
2008 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
2009
Andrew Trickef9de2a2013-05-25 02:42:55 +00002010 SDLoc DL(Op);
Dan Gohman31ae5862010-04-17 14:41:14 +00002011 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
Mehdi Amini44ede332015-07-09 02:09:04 +00002012 getPointerTy(MF.getDataLayout()));
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +00002013
2014 // vastart just stores the address of the VarArgsFrameIndex slot into the
2015 // memory location argument.
2016 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002017 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
Justin Lebar9c375812016-07-15 18:27:10 +00002018 MachinePointerInfo(SV));
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +00002019}
Jia Liuf54f60f2012-02-28 07:46:26 +00002020
Daniel Sanders2b553d42014-08-01 09:17:39 +00002021SDValue MipsTargetLowering::lowerVAARG(SDValue Op, SelectionDAG &DAG) const {
2022 SDNode *Node = Op.getNode();
2023 EVT VT = Node->getValueType(0);
2024 SDValue Chain = Node->getOperand(0);
2025 SDValue VAListPtr = Node->getOperand(1);
2026 unsigned Align = Node->getConstantOperandVal(3);
2027 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
2028 SDLoc DL(Node);
Eric Christopher96e72c62015-01-29 23:27:36 +00002029 unsigned ArgSlotSizeInBytes = (ABI.IsN32() || ABI.IsN64()) ? 8 : 4;
Daniel Sanders2b553d42014-08-01 09:17:39 +00002030
Justin Lebar9c375812016-07-15 18:27:10 +00002031 SDValue VAListLoad = DAG.getLoad(getPointerTy(DAG.getDataLayout()), DL, Chain,
2032 VAListPtr, MachinePointerInfo(SV));
Daniel Sanders2b553d42014-08-01 09:17:39 +00002033 SDValue VAList = VAListLoad;
2034
2035 // Re-align the pointer if necessary.
2036 // It should only ever be necessary for 64-bit types on O32 since the minimum
2037 // argument alignment is the same as the maximum type alignment for N32/N64.
2038 //
2039 // FIXME: We currently align too often. The code generator doesn't notice
2040 // when the pointer is still aligned from the last va_arg (or pair of
2041 // va_args for the i64 on O32 case).
2042 if (Align > getMinStackArgumentAlignment()) {
2043 assert(((Align & (Align-1)) == 0) && "Expected Align to be a power of 2");
2044
2045 VAList = DAG.getNode(ISD::ADD, DL, VAList.getValueType(), VAList,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002046 DAG.getConstant(Align - 1, DL, VAList.getValueType()));
Daniel Sanders2b553d42014-08-01 09:17:39 +00002047
2048 VAList = DAG.getNode(ISD::AND, DL, VAList.getValueType(), VAList,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002049 DAG.getConstant(-(int64_t)Align, DL,
Daniel Sanders2b553d42014-08-01 09:17:39 +00002050 VAList.getValueType()));
2051 }
2052
2053 // Increment the pointer, VAList, to the next vaarg.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00002054 auto &TD = DAG.getDataLayout();
2055 unsigned ArgSizeInBytes =
2056 TD.getTypeAllocSize(VT.getTypeForEVT(*DAG.getContext()));
Rui Ueyamada00f2f2016-01-14 21:06:47 +00002057 SDValue Tmp3 =
2058 DAG.getNode(ISD::ADD, DL, VAList.getValueType(), VAList,
2059 DAG.getConstant(alignTo(ArgSizeInBytes, ArgSlotSizeInBytes),
2060 DL, VAList.getValueType()));
Daniel Sanders2b553d42014-08-01 09:17:39 +00002061 // Store the incremented VAList to the legalized pointer
2062 Chain = DAG.getStore(VAListLoad.getValue(1), DL, Tmp3, VAListPtr,
Justin Lebar9c375812016-07-15 18:27:10 +00002063 MachinePointerInfo(SV));
Daniel Sanders2b553d42014-08-01 09:17:39 +00002064
2065 // In big-endian mode we must adjust the pointer when the load size is smaller
2066 // than the argument slot size. We must also reduce the known alignment to
2067 // match. For example in the N64 ABI, we must add 4 bytes to the offset to get
2068 // the correct half of the slot, and reduce the alignment from 8 (slot
2069 // alignment) down to 4 (type alignment).
2070 if (!Subtarget.isLittle() && ArgSizeInBytes < ArgSlotSizeInBytes) {
2071 unsigned Adjustment = ArgSlotSizeInBytes - ArgSizeInBytes;
2072 VAList = DAG.getNode(ISD::ADD, DL, VAListPtr.getValueType(), VAList,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002073 DAG.getIntPtrConstant(Adjustment, DL));
Daniel Sanders2b553d42014-08-01 09:17:39 +00002074 }
2075 // Load the actual argument out of the pointer VAList
Justin Lebar9c375812016-07-15 18:27:10 +00002076 return DAG.getLoad(VT, DL, Chain, VAList, MachinePointerInfo());
Daniel Sanders2b553d42014-08-01 09:17:39 +00002077}
2078
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00002079static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG,
2080 bool HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002081 EVT TyX = Op.getOperand(0).getValueType();
2082 EVT TyY = Op.getOperand(1).getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002083 SDLoc DL(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002084 SDValue Const1 = DAG.getConstant(1, DL, MVT::i32);
2085 SDValue Const31 = DAG.getConstant(31, DL, MVT::i32);
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002086 SDValue Res;
2087
2088 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
2089 // to i32.
2090 SDValue X = (TyX == MVT::f32) ?
2091 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
2092 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
2093 Const1);
2094 SDValue Y = (TyY == MVT::f32) ?
2095 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
2096 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
2097 Const1);
2098
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00002099 if (HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002100 // ext E, Y, 31, 1 ; extract bit31 of Y
2101 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
2102 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
2103 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
2104 } else {
2105 // sll SllX, X, 1
2106 // srl SrlX, SllX, 1
2107 // srl SrlY, Y, 31
2108 // sll SllY, SrlX, 31
2109 // or Or, SrlX, SllY
2110 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
2111 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
2112 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
2113 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
2114 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
2115 }
2116
2117 if (TyX == MVT::f32)
2118 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
2119
2120 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002121 Op.getOperand(0),
2122 DAG.getConstant(0, DL, MVT::i32));
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002123 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002124}
2125
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00002126static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG,
2127 bool HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002128 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
2129 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
2130 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002131 SDLoc DL(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002132 SDValue Const1 = DAG.getConstant(1, DL, MVT::i32);
Eric Christopher0713a9d2011-06-08 23:55:35 +00002133
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002134 // Bitcast to integer nodes.
2135 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
2136 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002137
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00002138 if (HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002139 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
2140 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
2141 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002142 DAG.getConstant(WidthY - 1, DL, MVT::i32), Const1);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002143
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002144 if (WidthX > WidthY)
2145 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
2146 else if (WidthY > WidthX)
2147 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002148
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002149 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002150 DAG.getConstant(WidthX - 1, DL, MVT::i32), Const1,
2151 X);
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002152 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
2153 }
2154
2155 // (d)sll SllX, X, 1
2156 // (d)srl SrlX, SllX, 1
2157 // (d)srl SrlY, Y, width(Y)-1
2158 // (d)sll SllY, SrlX, width(Y)-1
2159 // or Or, SrlX, SllY
2160 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
2161 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
2162 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002163 DAG.getConstant(WidthY - 1, DL, MVT::i32));
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002164
2165 if (WidthX > WidthY)
2166 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
2167 else if (WidthY > WidthX)
2168 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
2169
2170 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002171 DAG.getConstant(WidthX - 1, DL, MVT::i32));
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002172 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
2173 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002174}
2175
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00002176SDValue
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002177MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00002178 if (Subtarget.isGP64bit())
2179 return lowerFCOPYSIGN64(Op, DAG, Subtarget.hasExtractInsert());
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002180
Eric Christopher1c29a652014-07-18 22:55:25 +00002181 return lowerFCOPYSIGN32(Op, DAG, Subtarget.hasExtractInsert());
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002182}
2183
Akira Hatanaka66277522011-06-02 00:24:44 +00002184SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002185lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes5444a7b2011-06-16 00:40:02 +00002186 // check the depth
2187 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka15506782011-06-07 18:58:42 +00002188 "Frame address can only be determined for current frame.");
Akira Hatanaka66277522011-06-02 00:24:44 +00002189
Matthias Braun941a7052016-07-28 18:40:00 +00002190 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
2191 MFI.setFrameAddressIsTaken(true);
Akira Hatanaka66277522011-06-02 00:24:44 +00002192 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002193 SDLoc DL(Op);
Eric Christopher96e72c62015-01-29 23:27:36 +00002194 SDValue FrameAddr = DAG.getCopyFromReg(
2195 DAG.getEntryNode(), DL, ABI.IsN64() ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka66277522011-06-02 00:24:44 +00002196 return FrameAddr;
2197}
2198
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002199SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op,
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002200 SelectionDAG &DAG) const {
Bill Wendling908bf812014-01-06 00:43:20 +00002201 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00002202 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00002203
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002204 // check the depth
2205 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
2206 "Return address can be determined only for current frame.");
2207
2208 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00002209 MachineFrameInfo &MFI = MF.getFrameInfo();
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00002210 MVT VT = Op.getSimpleValueType();
Eric Christopher96e72c62015-01-29 23:27:36 +00002211 unsigned RA = ABI.IsN64() ? Mips::RA_64 : Mips::RA;
Matthias Braun941a7052016-07-28 18:40:00 +00002212 MFI.setReturnAddressIsTaken(true);
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002213
2214 // Return RA, which contains the return address. Mark it an implicit live-in.
2215 unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00002216 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT);
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002217}
2218
Akira Hatanakac0b02062013-01-30 00:26:49 +00002219// An EH_RETURN is the result of lowering llvm.eh.return which in turn is
2220// generated from __builtin_eh_return (offset, handler)
2221// The effect of this is to adjust the stack pointer by "offset"
2222// and then branch to "handler".
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002223SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Akira Hatanakac0b02062013-01-30 00:26:49 +00002224 const {
2225 MachineFunction &MF = DAG.getMachineFunction();
2226 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2227
2228 MipsFI->setCallsEhReturn();
2229 SDValue Chain = Op.getOperand(0);
2230 SDValue Offset = Op.getOperand(1);
2231 SDValue Handler = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002232 SDLoc DL(Op);
Eric Christopher96e72c62015-01-29 23:27:36 +00002233 EVT Ty = ABI.IsN64() ? MVT::i64 : MVT::i32;
Akira Hatanakac0b02062013-01-30 00:26:49 +00002234
2235 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
2236 // EH_RETURN nodes, so that instructions are emitted back-to-back.
Eric Christopher96e72c62015-01-29 23:27:36 +00002237 unsigned OffsetReg = ABI.IsN64() ? Mips::V1_64 : Mips::V1;
2238 unsigned AddrReg = ABI.IsN64() ? Mips::V0_64 : Mips::V0;
Akira Hatanakac0b02062013-01-30 00:26:49 +00002239 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
2240 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
2241 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
2242 DAG.getRegister(OffsetReg, Ty),
Mehdi Amini44ede332015-07-09 02:09:04 +00002243 DAG.getRegister(AddrReg, getPointerTy(MF.getDataLayout())),
Akira Hatanakac0b02062013-01-30 00:26:49 +00002244 Chain.getValue(1));
2245}
2246
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002247SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
Akira Hatanaka5fd22482012-06-14 21:10:56 +00002248 SelectionDAG &DAG) const {
Eli Friedman26a48482011-07-27 22:21:52 +00002249 // FIXME: Need pseudo-fence for 'singlethread' fences
2250 // FIXME: Set SType for weaker fences where supported/appropriate.
2251 unsigned SType = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002252 SDLoc DL(Op);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002253 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002254 DAG.getConstant(SType, DL, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00002255}
2256
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002257SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
Akira Hatanaka5fd22482012-06-14 21:10:56 +00002258 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002259 SDLoc DL(Op);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002260 MVT VT = Subtarget.isGP64bit() ? MVT::i64 : MVT::i32;
2261
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002262 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2263 SDValue Shamt = Op.getOperand(2);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002264 // if shamt < (VT.bits):
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002265 // lo = (shl lo, shamt)
2266 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
2267 // else:
2268 // lo = 0
2269 // hi = (shl lo, shamt[4:0])
2270 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002271 DAG.getConstant(-1, DL, MVT::i32));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002272 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, VT, Lo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002273 DAG.getConstant(1, DL, VT));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002274 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, VT, ShiftRight1Lo, Not);
2275 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, VT, Hi, Shamt);
2276 SDValue Or = DAG.getNode(ISD::OR, DL, VT, ShiftLeftHi, ShiftRightLo);
2277 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, VT, Lo, Shamt);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002278 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
Daniel Sanders301f9372015-04-29 12:28:58 +00002279 DAG.getConstant(VT.getSizeInBits(), DL, MVT::i32));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002280 Lo = DAG.getNode(ISD::SELECT, DL, VT, Cond,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002281 DAG.getConstant(0, DL, VT), ShiftLeftLo);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002282 Hi = DAG.getNode(ISD::SELECT, DL, VT, Cond, ShiftLeftLo, Or);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002283
2284 SDValue Ops[2] = {Lo, Hi};
Craig Topper64941d92014-04-27 19:20:57 +00002285 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002286}
2287
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002288SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002289 bool IsSRA) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002290 SDLoc DL(Op);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002291 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2292 SDValue Shamt = Op.getOperand(2);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002293 MVT VT = Subtarget.isGP64bit() ? MVT::i64 : MVT::i32;
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002294
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002295 // if shamt < (VT.bits):
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002296 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
2297 // if isSRA:
2298 // hi = (sra hi, shamt)
2299 // else:
2300 // hi = (srl hi, shamt)
2301 // else:
2302 // if isSRA:
2303 // lo = (sra hi, shamt[4:0])
2304 // hi = (sra hi, 31)
2305 // else:
2306 // lo = (srl hi, shamt[4:0])
2307 // hi = 0
2308 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002309 DAG.getConstant(-1, DL, MVT::i32));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002310 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, VT, Hi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002311 DAG.getConstant(1, DL, VT));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002312 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, VT, ShiftLeft1Hi, Not);
2313 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, VT, Lo, Shamt);
2314 SDValue Or = DAG.getNode(ISD::OR, DL, VT, ShiftLeftHi, ShiftRightLo);
2315 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL,
2316 DL, VT, Hi, Shamt);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002317 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
Daniel Sanders301f9372015-04-29 12:28:58 +00002318 DAG.getConstant(VT.getSizeInBits(), DL, MVT::i32));
2319 SDValue Ext = DAG.getNode(ISD::SRA, DL, VT, Hi,
2320 DAG.getConstant(VT.getSizeInBits() - 1, DL, VT));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002321 Lo = DAG.getNode(ISD::SELECT, DL, VT, Cond, ShiftRightHi, Or);
2322 Hi = DAG.getNode(ISD::SELECT, DL, VT, Cond,
Daniel Sanders301f9372015-04-29 12:28:58 +00002323 IsSRA ? Ext : DAG.getConstant(0, DL, VT), ShiftRightHi);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002324
2325 SDValue Ops[2] = {Lo, Hi};
Craig Topper64941d92014-04-27 19:20:57 +00002326 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002327}
2328
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002329static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002330 SDValue Chain, SDValue Src, unsigned Offset) {
Akira Hatanaka95866182012-06-13 19:06:08 +00002331 SDValue Ptr = LD->getBasePtr();
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002332 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
Akira Hatanaka95866182012-06-13 19:06:08 +00002333 EVT BasePtrVT = Ptr.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002334 SDLoc DL(LD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002335 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
2336
2337 if (Offset)
Akira Hatanaka95866182012-06-13 19:06:08 +00002338 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002339 DAG.getConstant(Offset, DL, BasePtrVT));
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002340
2341 SDValue Ops[] = { Chain, Ptr, Src };
Craig Topper206fcd42014-04-26 19:29:41 +00002342 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002343 LD->getMemOperand());
2344}
2345
2346// Expand an unaligned 32 or 64-bit integer load node.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002347SDValue MipsTargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002348 LoadSDNode *LD = cast<LoadSDNode>(Op);
2349 EVT MemVT = LD->getMemoryVT();
2350
Eric Christopher1c29a652014-07-18 22:55:25 +00002351 if (Subtarget.systemSupportsUnalignedAccess())
Daniel Sandersac272632014-05-23 13:18:02 +00002352 return Op;
2353
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002354 // Return if load is aligned or if MemVT is neither i32 nor i64.
2355 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
2356 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
2357 return SDValue();
2358
Eric Christopher1c29a652014-07-18 22:55:25 +00002359 bool IsLittle = Subtarget.isLittle();
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002360 EVT VT = Op.getValueType();
2361 ISD::LoadExtType ExtType = LD->getExtensionType();
2362 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
2363
2364 assert((VT == MVT::i32) || (VT == MVT::i64));
2365
2366 // Expand
2367 // (set dst, (i64 (load baseptr)))
2368 // to
2369 // (set tmp, (ldl (add baseptr, 7), undef))
2370 // (set dst, (ldr baseptr, tmp))
2371 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002372 SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002373 IsLittle ? 7 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002374 return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002375 IsLittle ? 0 : 7);
2376 }
2377
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002378 SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002379 IsLittle ? 3 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002380 SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002381 IsLittle ? 0 : 3);
2382
2383 // Expand
2384 // (set dst, (i32 (load baseptr))) or
2385 // (set dst, (i64 (sextload baseptr))) or
2386 // (set dst, (i64 (extload baseptr)))
2387 // to
2388 // (set tmp, (lwl (add baseptr, 3), undef))
2389 // (set dst, (lwr baseptr, tmp))
2390 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
2391 (ExtType == ISD::EXTLOAD))
2392 return LWR;
2393
2394 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
2395
2396 // Expand
2397 // (set dst, (i64 (zextload baseptr)))
2398 // to
2399 // (set tmp0, (lwl (add baseptr, 3), undef))
2400 // (set tmp1, (lwr baseptr, tmp0))
2401 // (set tmp2, (shl tmp1, 32))
2402 // (set dst, (srl tmp2, 32))
Andrew Trickef9de2a2013-05-25 02:42:55 +00002403 SDLoc DL(LD);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002404 SDValue Const32 = DAG.getConstant(32, DL, MVT::i32);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002405 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
Akira Hatanaka67346852012-06-04 17:46:29 +00002406 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
2407 SDValue Ops[] = { SRL, LWR.getValue(1) };
Craig Topper64941d92014-04-27 19:20:57 +00002408 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002409}
2410
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002411static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002412 SDValue Chain, unsigned Offset) {
Akira Hatanaka95866182012-06-13 19:06:08 +00002413 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
2414 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002415 SDLoc DL(SD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002416 SDVTList VTList = DAG.getVTList(MVT::Other);
2417
2418 if (Offset)
Akira Hatanaka95866182012-06-13 19:06:08 +00002419 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002420 DAG.getConstant(Offset, DL, BasePtrVT));
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002421
2422 SDValue Ops[] = { Chain, Value, Ptr };
Craig Topper206fcd42014-04-26 19:29:41 +00002423 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002424 SD->getMemOperand());
2425}
2426
2427// Expand an unaligned 32 or 64-bit integer store node.
Akira Hatanakad82ee942013-05-16 20:45:17 +00002428static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG,
2429 bool IsLittle) {
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002430 SDValue Value = SD->getValue(), Chain = SD->getChain();
2431 EVT VT = Value.getValueType();
2432
2433 // Expand
2434 // (store val, baseptr) or
2435 // (truncstore val, baseptr)
2436 // to
2437 // (swl val, (add baseptr, 3))
2438 // (swr val, baseptr)
2439 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002440 SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002441 IsLittle ? 3 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002442 return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002443 }
2444
2445 assert(VT == MVT::i64);
2446
2447 // Expand
2448 // (store val, baseptr)
2449 // to
2450 // (sdl val, (add baseptr, 7))
2451 // (sdr val, baseptr)
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002452 SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2453 return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002454}
2455
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002456// Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr).
2457static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG) {
2458 SDValue Val = SD->getValue();
2459
2460 if (Val.getOpcode() != ISD::FP_TO_SINT)
2461 return SDValue();
2462
2463 EVT FPTy = EVT::getFloatingPointVT(Val.getValueSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002464 SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy,
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002465 Val.getOperand(0));
Andrew Trickef9de2a2013-05-25 02:42:55 +00002466 return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(),
Justin Lebar9c375812016-07-15 18:27:10 +00002467 SD->getPointerInfo(), SD->getAlignment(),
2468 SD->getMemOperand()->getFlags());
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002469}
2470
Akira Hatanakad82ee942013-05-16 20:45:17 +00002471SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2472 StoreSDNode *SD = cast<StoreSDNode>(Op);
2473 EVT MemVT = SD->getMemoryVT();
2474
2475 // Lower unaligned integer stores.
Eric Christopher1c29a652014-07-18 22:55:25 +00002476 if (!Subtarget.systemSupportsUnalignedAccess() &&
Daniel Sandersac272632014-05-23 13:18:02 +00002477 (SD->getAlignment() < MemVT.getSizeInBits() / 8) &&
Akira Hatanakad82ee942013-05-16 20:45:17 +00002478 ((MemVT == MVT::i32) || (MemVT == MVT::i64)))
Eric Christopher1c29a652014-07-18 22:55:25 +00002479 return lowerUnalignedIntStore(SD, DAG, Subtarget.isLittle());
Akira Hatanakad82ee942013-05-16 20:45:17 +00002480
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002481 return lowerFP_TO_SINT_STORE(SD, DAG);
Akira Hatanakad82ee942013-05-16 20:45:17 +00002482}
2483
Hal Finkel5081ac22016-09-01 10:28:47 +00002484SDValue MipsTargetLowering::lowerEH_DWARF_CFA(SDValue Op,
2485 SelectionDAG &DAG) const {
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002486
Hal Finkel5081ac22016-09-01 10:28:47 +00002487 // Return a fixed StackObject with offset 0 which points to the old stack
2488 // pointer.
Matthias Braun941a7052016-07-28 18:40:00 +00002489 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002490 EVT ValTy = Op->getValueType(0);
Matthias Braun941a7052016-07-28 18:40:00 +00002491 int FI = MFI.CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
Hal Finkel5081ac22016-09-01 10:28:47 +00002492 return DAG.getFrameIndex(FI, ValTy);
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002493}
2494
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002495SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op,
2496 SelectionDAG &DAG) const {
2497 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002498 SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy,
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002499 Op.getOperand(0));
Andrew Trickef9de2a2013-05-25 02:42:55 +00002500 return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc);
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002501}
2502
Akira Hatanakae2489122011-04-15 21:51:11 +00002503//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002504// Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002505//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002506
Akira Hatanakae2489122011-04-15 21:51:11 +00002507//===----------------------------------------------------------------------===//
Wesley Peck527da1b2010-11-23 03:31:01 +00002508// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002509// Mips O32 ABI rules:
2510// ---
2511// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peck527da1b2010-11-23 03:31:01 +00002512// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002513// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peck527da1b2010-11-23 03:31:01 +00002514// f64 - Only passed in two aliased f32 registers if no int reg has been used
2515// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Sylvestre Ledru469de192014-08-11 18:04:46 +00002516// not used, it must be shadowed. If only A3 is available, shadow it and
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002517// go to stack.
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002518//
2519// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanakae2489122011-04-15 21:51:11 +00002520//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002521
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00002522static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2523 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002524 CCState &State, ArrayRef<MCPhysReg> F64Regs) {
Eric Christopher96e72c62015-01-29 23:27:36 +00002525 const MipsSubtarget &Subtarget = static_cast<const MipsSubtarget &>(
2526 State.getMachineFunction().getSubtarget());
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002527
Craig Topper840beec2014-04-04 05:16:06 +00002528 static const MCPhysReg IntRegs[] = { Mips::A0, Mips::A1, Mips::A2, Mips::A3 };
2529 static const MCPhysReg F32Regs[] = { Mips::F12, Mips::F14 };
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002530
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002531 // Do not process byval args here.
2532 if (ArgFlags.isByVal())
2533 return true;
Akira Hatanaka5e16c6a2011-05-24 19:18:33 +00002534
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002535 // Promote i8 and i16
Daniel Sandersd134c9d2014-12-02 20:40:27 +00002536 if (ArgFlags.isInReg() && !Subtarget.isLittle()) {
2537 if (LocVT == MVT::i8 || LocVT == MVT::i16 || LocVT == MVT::i32) {
2538 LocVT = MVT::i32;
2539 if (ArgFlags.isSExt())
2540 LocInfo = CCValAssign::SExtUpper;
2541 else if (ArgFlags.isZExt())
2542 LocInfo = CCValAssign::ZExtUpper;
2543 else
2544 LocInfo = CCValAssign::AExtUpper;
2545 }
2546 }
2547
2548 // Promote i8 and i16
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002549 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
2550 LocVT = MVT::i32;
2551 if (ArgFlags.isSExt())
2552 LocInfo = CCValAssign::SExt;
2553 else if (ArgFlags.isZExt())
2554 LocInfo = CCValAssign::ZExt;
2555 else
2556 LocInfo = CCValAssign::AExt;
2557 }
2558
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002559 unsigned Reg;
2560
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002561 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
2562 // is true: function is vararg, argument is 3rd or higher, there is previous
2563 // argument which is not f32 or f64.
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002564 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1 ||
2565 State.getFirstUnallocated(F32Regs) != ValNo;
Akira Hatanaka9e6a8cc2011-05-19 20:29:48 +00002566 unsigned OrigAlign = ArgFlags.getOrigAlign();
2567 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002568
2569 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002570 Reg = State.AllocateReg(IntRegs);
Akira Hatanaka9e6a8cc2011-05-19 20:29:48 +00002571 // If this is the first part of an i64 arg,
2572 // the allocated register must be either A0 or A2.
2573 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002574 Reg = State.AllocateReg(IntRegs);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002575 LocVT = MVT::i32;
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002576 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
2577 // Allocate int register and shadow next int register. If first
2578 // available register is Mips::A1 or Mips::A3, shadow it too.
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002579 Reg = State.AllocateReg(IntRegs);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002580 if (Reg == Mips::A1 || Reg == Mips::A3)
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002581 Reg = State.AllocateReg(IntRegs);
2582 State.AllocateReg(IntRegs);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002583 LocVT = MVT::i32;
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002584 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
2585 // we are guaranteed to find an available float register
2586 if (ValVT == MVT::f32) {
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002587 Reg = State.AllocateReg(F32Regs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002588 // Shadow int register
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002589 State.AllocateReg(IntRegs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002590 } else {
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002591 Reg = State.AllocateReg(F64Regs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002592 // Shadow int registers
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002593 unsigned Reg2 = State.AllocateReg(IntRegs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002594 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002595 State.AllocateReg(IntRegs);
2596 State.AllocateReg(IntRegs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002597 }
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002598 } else
2599 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002600
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002601 if (!Reg) {
2602 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3,
2603 OrigAlign);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002604 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002605 } else
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002606 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002607
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002608 return false;
Akira Hatanaka202f6402011-11-12 02:20:46 +00002609}
2610
Akira Hatanakabfb66242013-08-20 23:38:40 +00002611static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT,
2612 MVT LocVT, CCValAssign::LocInfo LocInfo,
2613 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002614 static const MCPhysReg F64Regs[] = { Mips::D6, Mips::D7 };
Akira Hatanakabfb66242013-08-20 23:38:40 +00002615
2616 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2617}
2618
2619static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT,
2620 MVT LocVT, CCValAssign::LocInfo LocInfo,
2621 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002622 static const MCPhysReg F64Regs[] = { Mips::D12_64, Mips::D14_64 };
Akira Hatanakabfb66242013-08-20 23:38:40 +00002623
2624 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2625}
2626
Reid Klecknerd3781742014-11-14 00:39:33 +00002627static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2628 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
2629 CCState &State) LLVM_ATTRIBUTE_UNUSED;
Reed Kotlerd5c41962014-11-13 23:37:45 +00002630
Akira Hatanaka202f6402011-11-12 02:20:46 +00002631#include "MipsGenCallingConv.inc"
2632
Akira Hatanakae2489122011-04-15 21:51:11 +00002633//===----------------------------------------------------------------------===//
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002634// Call Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002635//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002636
Akira Hatanaka61bbcce2011-09-23 00:58:33 +00002637// Return next O32 integer argument register.
2638static unsigned getNextIntArgReg(unsigned Reg) {
2639 assert((Reg == Mips::A0) || (Reg == Mips::A2));
2640 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
2641}
2642
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002643SDValue MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
2644 SDValue Chain, SDValue Arg,
2645 const SDLoc &DL, bool IsTailCall,
2646 SelectionDAG &DAG) const {
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002647 if (!IsTailCall) {
Mehdi Amini44ede332015-07-09 02:09:04 +00002648 SDValue PtrOff =
2649 DAG.getNode(ISD::ADD, DL, getPointerTy(DAG.getDataLayout()), StackPtr,
2650 DAG.getIntPtrConstant(Offset, DL));
Justin Lebar9c375812016-07-15 18:27:10 +00002651 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo());
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002652 }
2653
Matthias Braun941a7052016-07-28 18:40:00 +00002654 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
2655 int FI = MFI.CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00002656 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002657 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(),
Justin Lebar9c375812016-07-15 18:27:10 +00002658 /* Alignment = */ 0, MachineMemOperand::MOVolatile);
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002659}
2660
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002661void MipsTargetLowering::
2662getOpndList(SmallVectorImpl<SDValue> &Ops,
2663 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
2664 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
Sasa Stankovic7072a792014-10-01 08:22:21 +00002665 bool IsCallReloc, CallLoweringInfo &CLI, SDValue Callee,
2666 SDValue Chain) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002667 // Insert node "GP copy globalreg" before call to function.
2668 //
2669 // R_MIPS_CALL* operators (emitted when non-internal functions are called
2670 // in PIC mode) allow symbols to be resolved via lazy binding.
2671 // The lazy binding stub requires GP to point to the GOT.
Sasa Stankovic7072a792014-10-01 08:22:21 +00002672 // Note that we don't need GP to point to the GOT for indirect calls
2673 // (when R_MIPS_CALL* is not used for the call) because Mips linker generates
2674 // lazy binding stub for a function only when R_MIPS_CALL* are the only relocs
2675 // used for the function (that is, Mips linker doesn't generate lazy binding
2676 // stub for a function whose address is taken in the program).
2677 if (IsPICCall && !InternalLinkage && IsCallReloc) {
Eric Christopher96e72c62015-01-29 23:27:36 +00002678 unsigned GPReg = ABI.IsN64() ? Mips::GP_64 : Mips::GP;
2679 EVT Ty = ABI.IsN64() ? MVT::i64 : MVT::i32;
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002680 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty)));
2681 }
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002682
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002683 // Build a sequence of copy-to-reg nodes chained together with token
2684 // chain and flag operands which copy the outgoing args into registers.
2685 // The InFlag in necessary since all emitted instructions must be
2686 // stuck together.
2687 SDValue InFlag;
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002688
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002689 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2690 Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, RegsToPass[i].first,
2691 RegsToPass[i].second, InFlag);
2692 InFlag = Chain.getValue(1);
2693 }
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002694
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002695 // Add argument registers to the end of the list so that they are
2696 // known live into the call.
2697 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2698 Ops.push_back(CLI.DAG.getRegister(RegsToPass[i].first,
2699 RegsToPass[i].second.getValueType()));
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002700
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002701 // Add a register mask operand representing the call-preserved registers.
Eric Christopher96e72c62015-01-29 23:27:36 +00002702 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00002703 const uint32_t *Mask =
2704 TRI->getCallPreservedMask(CLI.DAG.getMachineFunction(), CLI.CallConv);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002705 assert(Mask && "Missing call preserved mask for calling convention");
Eric Christopher1c29a652014-07-18 22:55:25 +00002706 if (Subtarget.inMips16HardFloat()) {
Reed Kotler783c7942013-05-10 22:25:39 +00002707 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
2708 llvm::StringRef Sym = G->getGlobal()->getName();
2709 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
Reed Kotler3230e722013-12-12 02:41:11 +00002710 if (F && F->hasFnAttribute("__Mips16RetHelper")) {
Reed Kotler783c7942013-05-10 22:25:39 +00002711 Mask = MipsRegisterInfo::getMips16RetHelperMask();
2712 }
2713 }
2714 }
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002715 Ops.push_back(CLI.DAG.getRegisterMask(Mask));
2716
2717 if (InFlag.getNode())
2718 Ops.push_back(InFlag);
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002719}
2720
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002721/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman624801e2009-01-26 03:15:54 +00002722/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002723SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00002724MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002725 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00002726 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002727 SDLoc DL = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00002728 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
2729 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
2730 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Akira Hatanakabeda2242012-07-31 18:46:41 +00002731 SDValue Chain = CLI.Chain;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002732 SDValue Callee = CLI.Callee;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002733 bool &IsTailCall = CLI.IsTailCall;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002734 CallingConv::ID CallConv = CLI.CallConv;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002735 bool IsVarArg = CLI.IsVarArg;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002736
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002737 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00002738 MachineFrameInfo &MFI = MF.getFrameInfo();
Eric Christopher96e72c62015-01-29 23:27:36 +00002739 const TargetFrameLowering *TFL = Subtarget.getFrameLowering();
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002740 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
Rafael Espindola9f1c1fe2016-06-27 12:48:21 +00002741 bool IsPIC = isPositionIndependent();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002742
2743 // Analyze operands of the call, assigning locations to each operand.
2744 SmallVector<CCValAssign, 16> ArgLocs;
Daniel Sanders41a64c42014-11-07 11:10:48 +00002745 MipsCCState CCInfo(
2746 CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs, *DAG.getContext(),
2747 MipsCCState::getSpecialCallingConvForCallee(Callee.getNode(), Subtarget));
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002748
2749 // Allocate the reserved argument area. It seems strange to do this from the
2750 // caller side but removing it breaks the frame size calculation.
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002751 CCInfo.AllocateStack(ABI.GetCalleeAllocdArgSizeInBytes(CallConv), 1);
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002752
Daniel Sanderscfad1e32014-11-07 11:43:49 +00002753 CCInfo.AnalyzeCallOperands(Outs, CC_Mips, CLI.getArgs(), Callee.getNode());
Wesley Peck527da1b2010-11-23 03:31:01 +00002754
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002755 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka195a1e22011-06-08 17:39:33 +00002756 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka97ba7692012-07-26 23:27:01 +00002757
Simon Dardis1dcb9112016-11-20 21:23:08 +00002758 // Check if it's really possible to do a tail call. Restrict it to functions
2759 // that are part of this compilation unit.
2760 bool InternalLinkage = false;
2761 if (IsTailCall) {
Daniel Sanders23e98772014-11-02 16:09:29 +00002762 IsTailCall = isEligibleForTailCallOptimization(
2763 CCInfo, NextStackOffset, *MF.getInfo<MipsFunctionInfo>());
Simon Dardis1dcb9112016-11-20 21:23:08 +00002764 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2765 InternalLinkage = G->getGlobal()->hasInternalLinkage();
2766 IsTailCall &= (InternalLinkage || G->getGlobal()->hasLocalLinkage() ||
2767 G->getGlobal()->hasPrivateLinkage() ||
2768 G->getGlobal()->hasHiddenVisibility() ||
2769 G->getGlobal()->hasProtectedVisibility());
2770 }
2771 }
Reid Kleckner5772b772014-04-24 20:14:34 +00002772 if (!IsTailCall && CLI.CS && CLI.CS->isMustTailCall())
2773 report_fatal_error("failed to perform tail call elimination on a call "
2774 "site marked musttail");
2775
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002776 if (IsTailCall)
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002777 ++NumTailCalls;
2778
Akira Hatanaka79738332011-09-19 20:26:02 +00002779 // Chain is the output chain of the last Load/Store or CopyToReg node.
2780 // ByValChain is the output chain of the last Memcpy node created for copying
2781 // byval arguments to the stack.
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002782 unsigned StackAlignment = TFL->getStackAlignment();
Rui Ueyamada00f2f2016-01-14 21:06:47 +00002783 NextStackOffset = alignTo(NextStackOffset, StackAlignment);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002784 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, DL, true);
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002785
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002786 if (!IsTailCall)
Andrew Trickad6d08a2013-05-29 22:03:55 +00002787 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffsetVal, DL);
Akira Hatanakabeda2242012-07-31 18:46:41 +00002788
Mehdi Amini44ede332015-07-09 02:09:04 +00002789 SDValue StackPtr =
2790 DAG.getCopyFromReg(Chain, DL, ABI.IsN64() ? Mips::SP_64 : Mips::SP,
2791 getPointerTy(DAG.getDataLayout()));
Akira Hatanaka195a1e22011-06-08 17:39:33 +00002792
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002793 std::deque< std::pair<unsigned, SDValue> > RegsToPass;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002794 SmallVector<SDValue, 8> MemOpChains;
Daniel Sanders23e98772014-11-02 16:09:29 +00002795
2796 CCInfo.rewindByValRegsInfo();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002797
2798 // Walk the register/memloc assignments, inserting copies/loads.
2799 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002800 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002801 CCValAssign &VA = ArgLocs[i];
Akira Hatanakab20a3252011-10-28 19:49:00 +00002802 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka19891f82011-11-12 02:34:50 +00002803 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002804 bool UseUpperBits = false;
Akira Hatanaka19891f82011-11-12 02:34:50 +00002805
2806 // ByVal Arg.
2807 if (Flags.isByVal()) {
Daniel Sanders23e98772014-11-02 16:09:29 +00002808 unsigned FirstByValReg, LastByValReg;
2809 unsigned ByValIdx = CCInfo.getInRegsParamsProcessed();
2810 CCInfo.getInRegsParamInfo(ByValIdx, FirstByValReg, LastByValReg);
2811
Akira Hatanaka19891f82011-11-12 02:34:50 +00002812 assert(Flags.getByValSize() &&
2813 "ByVal args of size 0 should have been ignored by front-end.");
Daniel Sanders23e98772014-11-02 16:09:29 +00002814 assert(ByValIdx < CCInfo.getInRegsParamsCount());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002815 assert(!IsTailCall &&
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002816 "Do not tail-call optimize if there is a byval argument.");
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002817 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002818 FirstByValReg, LastByValReg, Flags, Subtarget.isLittle(),
2819 VA);
Daniel Sanders23e98772014-11-02 16:09:29 +00002820 CCInfo.nextInRegsParam();
Akira Hatanaka19891f82011-11-12 02:34:50 +00002821 continue;
2822 }
Jia Liuf54f60f2012-02-28 07:46:26 +00002823
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002824 // Promote the value if needed.
2825 switch (VA.getLocInfo()) {
Daniel Sandersc43cda82014-11-07 16:54:21 +00002826 default:
2827 llvm_unreachable("Unknown loc info!");
Wesley Peck527da1b2010-11-23 03:31:01 +00002828 case CCValAssign::Full:
Akira Hatanakab20a3252011-10-28 19:49:00 +00002829 if (VA.isRegLoc()) {
2830 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
Akira Hatanaka3b7391d2013-03-05 22:20:28 +00002831 (ValVT == MVT::f64 && LocVT == MVT::i64) ||
2832 (ValVT == MVT::i64 && LocVT == MVT::f64))
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002833 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
Akira Hatanakab20a3252011-10-28 19:49:00 +00002834 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002835 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002836 Arg, DAG.getConstant(0, DL, MVT::i32));
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002837 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002838 Arg, DAG.getConstant(1, DL, MVT::i32));
Eric Christopher1c29a652014-07-18 22:55:25 +00002839 if (!Subtarget.isLittle())
Akira Hatanaka27916972011-04-15 19:52:08 +00002840 std::swap(Lo, Hi);
Jia Liuf54f60f2012-02-28 07:46:26 +00002841 unsigned LocRegLo = VA.getLocReg();
Akira Hatanaka61bbcce2011-09-23 00:58:33 +00002842 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2843 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2844 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002845 continue;
Wesley Peck527da1b2010-11-23 03:31:01 +00002846 }
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002847 }
2848 break;
Daniel Sanders23e98772014-11-02 16:09:29 +00002849 case CCValAssign::BCvt:
2850 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
2851 break;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002852 case CCValAssign::SExtUpper:
2853 UseUpperBits = true;
Justin Bognerb03fd122016-08-17 05:10:15 +00002854 LLVM_FALLTHROUGH;
Chris Lattner52f16de2008-03-17 06:57:02 +00002855 case CCValAssign::SExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002856 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002857 break;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002858 case CCValAssign::ZExtUpper:
2859 UseUpperBits = true;
Justin Bognerb03fd122016-08-17 05:10:15 +00002860 LLVM_FALLTHROUGH;
Chris Lattner52f16de2008-03-17 06:57:02 +00002861 case CCValAssign::ZExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002862 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002863 break;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002864 case CCValAssign::AExtUpper:
2865 UseUpperBits = true;
Justin Bognerb03fd122016-08-17 05:10:15 +00002866 LLVM_FALLTHROUGH;
Chris Lattner52f16de2008-03-17 06:57:02 +00002867 case CCValAssign::AExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002868 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002869 break;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002870 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002871
Daniel Sandersc43cda82014-11-07 16:54:21 +00002872 if (UseUpperBits) {
2873 unsigned ValSizeInBits = Outs[i].ArgVT.getSizeInBits();
2874 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
2875 Arg = DAG.getNode(
2876 ISD::SHL, DL, VA.getLocVT(), Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002877 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
Daniel Sandersc43cda82014-11-07 16:54:21 +00002878 }
2879
Wesley Peck527da1b2010-11-23 03:31:01 +00002880 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +00002881 // RegsToPass vector
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002882 if (VA.isRegLoc()) {
2883 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattner52f16de2008-03-17 06:57:02 +00002884 continue;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002885 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002886
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002887 // Register can't get to this point...
Chris Lattner52f16de2008-03-17 06:57:02 +00002888 assert(VA.isMemLoc());
Wesley Peck527da1b2010-11-23 03:31:01 +00002889
Wesley Peck527da1b2010-11-23 03:31:01 +00002890 // emit ISD::STORE whichs stores the
Chris Lattner52f16de2008-03-17 06:57:02 +00002891 // parameter value to a stack Location
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002892 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002893 Chain, Arg, DL, IsTailCall, DAG));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002894 }
2895
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002896 // Transform all store nodes into one single node because all store
2897 // nodes are independent of each other.
Wesley Peck527da1b2010-11-23 03:31:01 +00002898 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00002899 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002900
Bill Wendling24c79f22008-09-16 21:48:12 +00002901 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peck527da1b2010-11-23 03:31:01 +00002902 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2903 // node so that legalize doesn't hack it.
Simon Dardisca74dd72017-01-27 11:36:52 +00002904
Akira Hatanakad6f1c582011-04-07 19:51:44 +00002905 SDValue CalleeLo;
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002906 EVT Ty = Callee.getValueType();
Simon Dardis1dcb9112016-11-20 21:23:08 +00002907 bool GlobalOrExternal = false, IsCallReloc = false;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002908
2909 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Simon Dardisca74dd72017-01-27 11:36:52 +00002910 if (IsPIC) {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002911 const GlobalValue *Val = G->getGlobal();
2912 InternalLinkage = Val->hasInternalLinkage();
Akira Hatanakacf9a61b2012-12-13 03:17:29 +00002913
2914 if (InternalLinkage)
Eric Christopher96e72c62015-01-29 23:27:36 +00002915 Callee = getAddrLocal(G, DL, Ty, DAG, ABI.IsN32() || ABI.IsN64());
Sasa Stankovic7072a792014-10-01 08:22:21 +00002916 else if (LargeGOT) {
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002917 Callee = getAddrGlobalLargeGOT(G, DL, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002918 MipsII::MO_CALL_LO16, Chain,
2919 FuncInfo->callPtrInfo(Val));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002920 IsCallReloc = true;
2921 } else {
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002922 Callee = getAddrGlobal(G, DL, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002923 FuncInfo->callPtrInfo(Val));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002924 IsCallReloc = true;
2925 }
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002926 } else
Mehdi Amini44ede332015-07-09 02:09:04 +00002927 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL,
2928 getPointerTy(DAG.getDataLayout()), 0,
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002929 MipsII::MO_NO_FLAG);
Akira Hatanaka8e16aac2011-12-09 01:45:12 +00002930 GlobalOrExternal = true;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002931 }
2932 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002933 const char *Sym = S->getSymbol();
2934
Simon Dardisca74dd72017-01-27 11:36:52 +00002935 if (!IsPIC) // static
Mehdi Amini44ede332015-07-09 02:09:04 +00002936 Callee = DAG.getTargetExternalSymbol(
2937 Sym, getPointerTy(DAG.getDataLayout()), MipsII::MO_NO_FLAG);
Sasa Stankovic7072a792014-10-01 08:22:21 +00002938 else if (LargeGOT) {
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002939 Callee = getAddrGlobalLargeGOT(S, DL, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002940 MipsII::MO_CALL_LO16, Chain,
2941 FuncInfo->callPtrInfo(Sym));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002942 IsCallReloc = true;
Simon Dardisca74dd72017-01-27 11:36:52 +00002943 } else { // PIC
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002944 Callee = getAddrGlobal(S, DL, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002945 FuncInfo->callPtrInfo(Sym));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002946 IsCallReloc = true;
2947 }
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002948
Akira Hatanaka8e16aac2011-12-09 01:45:12 +00002949 GlobalOrExternal = true;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002950 }
2951
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002952 SmallVector<SDValue, 8> Ops(1, Chain);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002953 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002954
Simon Dardisca74dd72017-01-27 11:36:52 +00002955 getOpndList(Ops, RegsToPass, IsPIC, GlobalOrExternal, InternalLinkage,
Sasa Stankovic7072a792014-10-01 08:22:21 +00002956 IsCallReloc, CLI, Callee, Chain);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002957
Simon Dardis9a66bbe2016-09-21 09:43:40 +00002958 if (IsTailCall) {
2959 MF.getFrameInfo().setHasTailCall();
Craig Topper48d114b2014-04-26 18:35:24 +00002960 return DAG.getNode(MipsISD::TailCall, DL, MVT::Other, Ops);
Simon Dardis9a66bbe2016-09-21 09:43:40 +00002961 }
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002962
Craig Topper48d114b2014-04-26 18:35:24 +00002963 Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, Ops);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002964 SDValue InFlag = Chain.getValue(1);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002965
Bruno Cardoso Lopes193e64c2010-01-30 18:32:07 +00002966 // Create the CALLSEQ_END node.
Akira Hatanaka97ba7692012-07-26 23:27:01 +00002967 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002968 DAG.getIntPtrConstant(0, DL, true), InFlag, DL);
Bruno Cardoso Lopes193e64c2010-01-30 18:32:07 +00002969 InFlag = Chain.getValue(1);
2970
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002971 // Handle result values, copying them out of physregs into vregs that we
2972 // return.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002973 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg, Ins, DL, DAG,
2974 InVals, CLI);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002975}
2976
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002977/// LowerCallResult - Lower the result values of a call into the
2978/// appropriate copies out of appropriate physical registers.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002979SDValue MipsTargetLowering::LowerCallResult(
2980 SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool IsVarArg,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002981 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
2982 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals,
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002983 TargetLowering::CallLoweringInfo &CLI) const {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002984 // Assign locations to each value returned by this call.
2985 SmallVector<CCValAssign, 16> RVLocs;
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002986 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
2987 *DAG.getContext());
2988 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips, CLI);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002989
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002990 // Copy all of the result registers out of their specified physreg.
2991 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Daniel Sandersae275e32014-09-25 12:15:05 +00002992 CCValAssign &VA = RVLocs[i];
2993 assert(VA.isRegLoc() && "Can only return in registers!");
2994
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002995 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(),
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002996 RVLocs[i].getLocVT(), InFlag);
2997 Chain = Val.getValue(1);
2998 InFlag = Val.getValue(2);
2999
Daniel Sandersae275e32014-09-25 12:15:05 +00003000 if (VA.isUpperBitsInLoc()) {
3001 unsigned ValSizeInBits = Ins[i].ArgVT.getSizeInBits();
3002 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
3003 unsigned Shift =
3004 VA.getLocInfo() == CCValAssign::ZExtUpper ? ISD::SRL : ISD::SRA;
3005 Val = DAG.getNode(
3006 Shift, DL, VA.getLocVT(), Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003007 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
Daniel Sandersae275e32014-09-25 12:15:05 +00003008 }
3009
3010 switch (VA.getLocInfo()) {
3011 default:
3012 llvm_unreachable("Unknown loc info!");
3013 case CCValAssign::Full:
3014 break;
3015 case CCValAssign::BCvt:
3016 Val = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Val);
3017 break;
3018 case CCValAssign::AExt:
3019 case CCValAssign::AExtUpper:
3020 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
3021 break;
3022 case CCValAssign::ZExt:
3023 case CCValAssign::ZExtUpper:
3024 Val = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Val,
3025 DAG.getValueType(VA.getValVT()));
3026 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
3027 break;
3028 case CCValAssign::SExt:
3029 case CCValAssign::SExtUpper:
3030 Val = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Val,
3031 DAG.getValueType(VA.getValVT()));
3032 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
3033 break;
3034 }
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003035
3036 InVals.push_back(Val);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003037 }
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +00003038
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003039 return Chain;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003040}
3041
Daniel Sandersc43cda82014-11-07 16:54:21 +00003042static SDValue UnpackFromArgumentSlot(SDValue Val, const CCValAssign &VA,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003043 EVT ArgVT, const SDLoc &DL,
3044 SelectionDAG &DAG) {
Daniel Sandersc43cda82014-11-07 16:54:21 +00003045 MVT LocVT = VA.getLocVT();
3046 EVT ValVT = VA.getValVT();
3047
3048 // Shift into the upper bits if necessary.
3049 switch (VA.getLocInfo()) {
3050 default:
3051 break;
3052 case CCValAssign::AExtUpper:
3053 case CCValAssign::SExtUpper:
3054 case CCValAssign::ZExtUpper: {
3055 unsigned ValSizeInBits = ArgVT.getSizeInBits();
3056 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
3057 unsigned Opcode =
3058 VA.getLocInfo() == CCValAssign::ZExtUpper ? ISD::SRL : ISD::SRA;
3059 Val = DAG.getNode(
3060 Opcode, DL, VA.getLocVT(), Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003061 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
Daniel Sandersc43cda82014-11-07 16:54:21 +00003062 break;
3063 }
3064 }
3065
3066 // If this is an value smaller than the argument slot size (32-bit for O32,
3067 // 64-bit for N32/N64), it has been promoted in some way to the argument slot
3068 // size. Extract the value and insert any appropriate assertions regarding
3069 // sign/zero extension.
3070 switch (VA.getLocInfo()) {
3071 default:
3072 llvm_unreachable("Unknown loc info!");
3073 case CCValAssign::Full:
3074 break;
3075 case CCValAssign::AExtUpper:
3076 case CCValAssign::AExt:
3077 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
3078 break;
3079 case CCValAssign::SExtUpper:
3080 case CCValAssign::SExt:
3081 Val = DAG.getNode(ISD::AssertSext, DL, LocVT, Val, DAG.getValueType(ValVT));
3082 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
3083 break;
3084 case CCValAssign::ZExtUpper:
3085 case CCValAssign::ZExt:
3086 Val = DAG.getNode(ISD::AssertZext, DL, LocVT, Val, DAG.getValueType(ValVT));
3087 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
3088 break;
3089 case CCValAssign::BCvt:
3090 Val = DAG.getNode(ISD::BITCAST, DL, ValVT, Val);
3091 break;
3092 }
3093
3094 return Val;
3095}
3096
Akira Hatanakae2489122011-04-15 21:51:11 +00003097//===----------------------------------------------------------------------===//
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003098// Formal Arguments Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00003099//===----------------------------------------------------------------------===//
Wesley Peck527da1b2010-11-23 03:31:01 +00003100/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003101/// and generate load operations for arguments places on the stack.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003102SDValue MipsTargetLowering::LowerFormalArguments(
3103 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
3104 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
3105 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Bruno Cardoso Lopesa01ede22008-08-04 07:12:52 +00003106 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00003107 MachineFrameInfo &MFI = MF.getFrameInfo();
Bruno Cardoso Lopes14033fb2007-08-28 05:08:16 +00003108 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00003109
Dan Gohman31ae5862010-04-17 14:41:14 +00003110 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003111
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003112 // Used with vargs to acumulate store chains.
3113 std::vector<SDValue> OutChains;
3114
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003115 // Assign locations to all of the incoming arguments.
3116 SmallVector<CCValAssign, 16> ArgLocs;
Daniel Sanders23e98772014-11-02 16:09:29 +00003117 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
3118 *DAG.getContext());
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003119 CCInfo.AllocateStack(ABI.GetCalleeAllocdArgSizeInBytes(CallConv), 1);
Vasileios Kalintiris43dff0c2015-10-26 12:38:43 +00003120 const Function *Func = DAG.getMachineFunction().getFunction();
3121 Function::const_arg_iterator FuncArg = Func->arg_begin();
3122
Vasileios Kalintiris165121f2015-10-26 14:24:30 +00003123 if (Func->hasFnAttribute("interrupt") && !Func->arg_empty())
3124 report_fatal_error(
3125 "Functions with the interrupt attribute cannot have arguments!");
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00003126
Daniel Sandersb70e27c2014-11-06 16:36:30 +00003127 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips_FixedArg);
Akira Hatanaka4866fe12012-10-30 19:37:25 +00003128 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(),
Daniel Sanders23e98772014-11-02 16:09:29 +00003129 CCInfo.getInRegsParamsCount() > 0);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00003130
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00003131 unsigned CurArgIdx = 0;
Daniel Sanders23e98772014-11-02 16:09:29 +00003132 CCInfo.rewindByValRegsInfo();
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003133
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00003134 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003135 CCValAssign &VA = ArgLocs[i];
Andrew Trick05938a52015-02-16 18:10:47 +00003136 if (Ins[i].isOrigArg()) {
3137 std::advance(FuncArg, Ins[i].getOrigArgIndex() - CurArgIdx);
3138 CurArgIdx = Ins[i].getOrigArgIndex();
3139 }
Akira Hatanaka104b7e32011-10-28 19:55:48 +00003140 EVT ValVT = VA.getValVT();
Akira Hatanakafb9bae32011-11-12 02:29:58 +00003141 ISD::ArgFlagsTy Flags = Ins[i].Flags;
3142 bool IsRegLoc = VA.isRegLoc();
3143
3144 if (Flags.isByVal()) {
Andrew Trick05938a52015-02-16 18:10:47 +00003145 assert(Ins[i].isOrigArg() && "Byval arguments cannot be implicit");
Daniel Sanders23e98772014-11-02 16:09:29 +00003146 unsigned FirstByValReg, LastByValReg;
3147 unsigned ByValIdx = CCInfo.getInRegsParamsProcessed();
3148 CCInfo.getInRegsParamInfo(ByValIdx, FirstByValReg, LastByValReg);
3149
Akira Hatanakafb9bae32011-11-12 02:29:58 +00003150 assert(Flags.getByValSize() &&
3151 "ByVal args of size 0 should have been ignored by front-end.");
Daniel Sanders23e98772014-11-02 16:09:29 +00003152 assert(ByValIdx < CCInfo.getInRegsParamsCount());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003153 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003154 FirstByValReg, LastByValReg, VA, CCInfo);
Daniel Sanders23e98772014-11-02 16:09:29 +00003155 CCInfo.nextInRegsParam();
Akira Hatanakafb9bae32011-11-12 02:29:58 +00003156 continue;
3157 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003158
3159 // Arguments stored on registers
Akira Hatanakafb9bae32011-11-12 02:29:58 +00003160 if (IsRegLoc) {
Akira Hatanaka7d822522013-10-28 21:21:36 +00003161 MVT RegVT = VA.getLocVT();
Akira Hatanakacb4a1a82011-05-24 00:23:52 +00003162 unsigned ArgReg = VA.getLocReg();
Akira Hatanaka7d822522013-10-28 21:21:36 +00003163 const TargetRegisterClass *RC = getRegClassFor(RegVT);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003164
Wesley Peck527da1b2010-11-23 03:31:01 +00003165 // Transform the arguments stored on
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003166 // physical registers into virtual ones
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003167 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC);
3168 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
Wesley Peck527da1b2010-11-23 03:31:01 +00003169
Daniel Sandersc43cda82014-11-07 16:54:21 +00003170 ArgValue = UnpackFromArgumentSlot(ArgValue, VA, Ins[i].ArgVT, DL, DAG);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00003171
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003172 // Handle floating point arguments passed in integer registers and
3173 // long double arguments passed in floating point registers.
Akira Hatanaka104b7e32011-10-28 19:55:48 +00003174 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003175 (RegVT == MVT::i64 && ValVT == MVT::f64) ||
3176 (RegVT == MVT::f64 && ValVT == MVT::i64))
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003177 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
Eric Christopher96e72c62015-01-29 23:27:36 +00003178 else if (ABI.IsO32() && RegVT == MVT::i32 &&
Eric Christopherbf33a3c2014-07-02 23:18:40 +00003179 ValVT == MVT::f64) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003180 unsigned Reg2 = addLiveIn(DAG.getMachineFunction(),
Akira Hatanaka104b7e32011-10-28 19:55:48 +00003181 getNextIntArgReg(ArgReg), RC);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003182 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT);
Eric Christopher1c29a652014-07-18 22:55:25 +00003183 if (!Subtarget.isLittle())
Akira Hatanaka104b7e32011-10-28 19:55:48 +00003184 std::swap(ArgValue, ArgValue2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003185 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64,
Akira Hatanaka104b7e32011-10-28 19:55:48 +00003186 ArgValue, ArgValue2);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00003187 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003188
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003189 InVals.push_back(ArgValue);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003190 } else { // VA.isRegLoc()
Daniel Sandersc43cda82014-11-07 16:54:21 +00003191 MVT LocVT = VA.getLocVT();
3192
Eric Christopher96e72c62015-01-29 23:27:36 +00003193 if (ABI.IsO32()) {
Daniel Sandersc43cda82014-11-07 16:54:21 +00003194 // We ought to be able to use LocVT directly but O32 sets it to i32
3195 // when allocating floating point values to integer registers.
3196 // This shouldn't influence how we load the value into registers unless
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00003197 // we are targeting softfloat.
Eric Christophere8ae3e32015-05-07 23:10:21 +00003198 if (VA.getValVT().isFloatingPoint() && !Subtarget.useSoftFloat())
Daniel Sandersc43cda82014-11-07 16:54:21 +00003199 LocVT = VA.getValVT();
3200 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003201
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003202 // sanity check
3203 assert(VA.isMemLoc());
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003204
Wesley Peck527da1b2010-11-23 03:31:01 +00003205 // The stack pointer offset is relative to the caller stack frame.
Matthias Braun941a7052016-07-28 18:40:00 +00003206 int FI = MFI.CreateFixedObject(LocVT.getSizeInBits() / 8,
3207 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003208
3209 // Create load nodes to retrieve arguments from the stack
Mehdi Amini44ede332015-07-09 02:09:04 +00003210 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Alex Lorenze40c8a22015-08-11 23:09:45 +00003211 SDValue ArgValue = DAG.getLoad(
3212 LocVT, DL, Chain, FIN,
Justin Lebar9c375812016-07-15 18:27:10 +00003213 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI));
Daniel Sandersc43cda82014-11-07 16:54:21 +00003214 OutChains.push_back(ArgValue.getValue(1));
3215
3216 ArgValue = UnpackFromArgumentSlot(ArgValue, VA, Ins[i].ArgVT, DL, DAG);
3217
3218 InVals.push_back(ArgValue);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003219 }
Reid Kleckner7a59e082014-05-12 22:01:27 +00003220 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003221
Reid Kleckner7a59e082014-05-12 22:01:27 +00003222 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Reid Kleckner79418562014-05-09 22:32:13 +00003223 // The mips ABIs for returning structs by value requires that we copy
3224 // the sret argument into $v0 for the return. Save the argument into
3225 // a virtual register so that we can access it from the return points.
Reid Kleckner7a59e082014-05-12 22:01:27 +00003226 if (Ins[i].Flags.isSRet()) {
Reid Kleckner79418562014-05-09 22:32:13 +00003227 unsigned Reg = MipsFI->getSRetReturnReg();
3228 if (!Reg) {
3229 Reg = MF.getRegInfo().createVirtualRegister(
Eric Christopher96e72c62015-01-29 23:27:36 +00003230 getRegClassFor(ABI.IsN64() ? MVT::i64 : MVT::i32));
Reid Kleckner79418562014-05-09 22:32:13 +00003231 MipsFI->setSRetReturnReg(Reg);
3232 }
Reid Kleckner7a59e082014-05-12 22:01:27 +00003233 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[i]);
Reid Kleckner79418562014-05-09 22:32:13 +00003234 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
Reid Kleckner7a59e082014-05-12 22:01:27 +00003235 break;
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003236 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003237 }
3238
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003239 if (IsVarArg)
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003240 writeVarArgRegs(OutChains, Chain, DL, DAG, CCInfo);
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003241
Wesley Peck527da1b2010-11-23 03:31:01 +00003242 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003243 // the size of Ins and InVals. This only happens when on varg functions
3244 if (!OutChains.empty()) {
3245 OutChains.push_back(Chain);
Craig Topper48d114b2014-04-26 18:35:24 +00003246 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003247 }
3248
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003249 return Chain;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003250}
3251
Akira Hatanakae2489122011-04-15 21:51:11 +00003252//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003253// Return Value Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00003254//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003255
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003256bool
3257MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003258 MachineFunction &MF, bool IsVarArg,
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003259 const SmallVectorImpl<ISD::OutputArg> &Outs,
3260 LLVMContext &Context) const {
3261 SmallVector<CCValAssign, 16> RVLocs;
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003262 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003263 return CCInfo.CheckReturn(Outs, RetCC_Mips);
3264}
3265
Petar Jovanovic5b436222015-03-23 12:28:13 +00003266bool
3267MipsTargetLowering::shouldSignExtendTypeInLibCall(EVT Type, bool IsSigned) const {
Eric Christophere8ae3e32015-05-07 23:10:21 +00003268 if (Subtarget.hasMips3() && Subtarget.useSoftFloat()) {
Petar Jovanovic5b436222015-03-23 12:28:13 +00003269 if (Type == MVT::i32)
3270 return true;
3271 }
3272 return IsSigned;
3273}
3274
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003275SDValue
Vasileios Kalintiris43dff0c2015-10-26 12:38:43 +00003276MipsTargetLowering::LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003277 const SDLoc &DL,
3278 SelectionDAG &DAG) const {
Vasileios Kalintiris43dff0c2015-10-26 12:38:43 +00003279
3280 MachineFunction &MF = DAG.getMachineFunction();
3281 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3282
3283 MipsFI->setISR();
3284
3285 return DAG.getNode(MipsISD::ERet, DL, MVT::Other, RetOps);
3286}
3287
3288SDValue
3289MipsTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
3290 bool IsVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003291 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003292 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003293 const SDLoc &DL, SelectionDAG &DAG) const {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003294 // CCValAssign - represent the assignment of
3295 // the return value to a location
3296 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003297 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003298
3299 // CCState - Info about the registers and stack slot.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003300 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, *DAG.getContext());
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003301
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003302 // Analyze return values.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003303 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003304
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003305 SDValue Flag;
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003306 SmallVector<SDValue, 4> RetOps(1, Chain);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003307
3308 // Copy the result values into the output registers.
3309 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003310 SDValue Val = OutVals[i];
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003311 CCValAssign &VA = RVLocs[i];
3312 assert(VA.isRegLoc() && "Can only return in registers!");
Daniel Sandersae275e32014-09-25 12:15:05 +00003313 bool UseUpperBits = false;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003314
Daniel Sandersae275e32014-09-25 12:15:05 +00003315 switch (VA.getLocInfo()) {
3316 default:
3317 llvm_unreachable("Unknown loc info!");
3318 case CCValAssign::Full:
3319 break;
3320 case CCValAssign::BCvt:
3321 Val = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Val);
3322 break;
3323 case CCValAssign::AExtUpper:
3324 UseUpperBits = true;
Justin Bognerb03fd122016-08-17 05:10:15 +00003325 LLVM_FALLTHROUGH;
Daniel Sandersae275e32014-09-25 12:15:05 +00003326 case CCValAssign::AExt:
3327 Val = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Val);
3328 break;
3329 case CCValAssign::ZExtUpper:
3330 UseUpperBits = true;
Justin Bognerb03fd122016-08-17 05:10:15 +00003331 LLVM_FALLTHROUGH;
Daniel Sandersae275e32014-09-25 12:15:05 +00003332 case CCValAssign::ZExt:
3333 Val = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Val);
3334 break;
3335 case CCValAssign::SExtUpper:
3336 UseUpperBits = true;
Justin Bognerb03fd122016-08-17 05:10:15 +00003337 LLVM_FALLTHROUGH;
Daniel Sandersae275e32014-09-25 12:15:05 +00003338 case CCValAssign::SExt:
3339 Val = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Val);
3340 break;
3341 }
3342
3343 if (UseUpperBits) {
3344 unsigned ValSizeInBits = Outs[i].ArgVT.getSizeInBits();
3345 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
3346 Val = DAG.getNode(
3347 ISD::SHL, DL, VA.getLocVT(), Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003348 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
Daniel Sandersae275e32014-09-25 12:15:05 +00003349 }
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003350
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003351 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003352
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003353 // Guarantee that all emitted copies are stuck together with flags.
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003354 Flag = Chain.getValue(1);
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003355 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003356 }
3357
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003358 // The mips ABIs for returning structs by value requires that we copy
3359 // the sret argument into $v0 for the return. We saved the argument into
3360 // a virtual register in the entry block, so now we copy the value out
3361 // and into $v0.
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003362 if (MF.getFunction()->hasStructRetAttr()) {
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003363 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3364 unsigned Reg = MipsFI->getSRetReturnReg();
3365
Wesley Peck527da1b2010-11-23 03:31:01 +00003366 if (!Reg)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003367 llvm_unreachable("sret virtual register not created in the entry block");
Mehdi Amini44ede332015-07-09 02:09:04 +00003368 SDValue Val =
3369 DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(DAG.getDataLayout()));
Eric Christopher96e72c62015-01-29 23:27:36 +00003370 unsigned V0 = ABI.IsN64() ? Mips::V0_64 : Mips::V0;
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003371
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003372 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003373 Flag = Chain.getValue(1);
Mehdi Amini44ede332015-07-09 02:09:04 +00003374 RetOps.push_back(DAG.getRegister(V0, getPointerTy(DAG.getDataLayout())));
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003375 }
3376
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003377 RetOps[0] = Chain; // Update chain.
Akira Hatanakaefff7b72012-07-10 00:19:06 +00003378
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003379 // Add the flag if we have it.
3380 if (Flag.getNode())
3381 RetOps.push_back(Flag);
3382
Vasileios Kalintiris43dff0c2015-10-26 12:38:43 +00003383 // ISRs must use "eret".
3384 if (DAG.getMachineFunction().getFunction()->hasFnAttribute("interrupt"))
3385 return LowerInterruptReturn(RetOps, DL, DAG);
3386
3387 // Standard return on Mips is a "jr $ra"
Craig Topper48d114b2014-04-26 18:35:24 +00003388 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, RetOps);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003389}
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003390
Akira Hatanakae2489122011-04-15 21:51:11 +00003391//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003392// Mips Inline Assembly Support
Akira Hatanakae2489122011-04-15 21:51:11 +00003393//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003394
3395/// getConstraintType - Given a constraint letter, return the type of
3396/// constraint it is for this target.
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003397MipsTargetLowering::ConstraintType
3398MipsTargetLowering::getConstraintType(StringRef Constraint) const {
Daniel Sanders8b59af12013-11-12 12:56:01 +00003399 // Mips specific constraints
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003400 // GCC config/mips/constraints.md
3401 //
Wesley Peck527da1b2010-11-23 03:31:01 +00003402 // 'd' : An address register. Equivalent to r
3403 // unless generating MIPS16 code.
3404 // 'y' : Equivalent to r; retained for
3405 // backwards compatibility.
Eric Christophere3c494d2012-05-07 06:25:10 +00003406 // 'c' : A register suitable for use in an indirect
3407 // jump. This will always be $25 for -mabicalls.
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003408 // 'l' : The lo register. 1 word storage.
3409 // 'x' : The hilo register pair. Double word storage.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003410 if (Constraint.size() == 1) {
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003411 switch (Constraint[0]) {
3412 default : break;
Wesley Peck527da1b2010-11-23 03:31:01 +00003413 case 'd':
3414 case 'y':
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003415 case 'f':
Eric Christophere3c494d2012-05-07 06:25:10 +00003416 case 'c':
Eric Christopher9c492e62012-05-07 06:25:15 +00003417 case 'l':
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003418 case 'x':
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003419 return C_RegisterClass;
Jack Carter0e149b02013-03-04 21:33:15 +00003420 case 'R':
3421 return C_Memory;
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003422 }
3423 }
Daniel Sandersa73d8fe2015-03-24 11:26:34 +00003424
3425 if (Constraint == "ZC")
3426 return C_Memory;
3427
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003428 return TargetLowering::getConstraintType(Constraint);
3429}
3430
John Thompsone8360b72010-10-29 17:29:13 +00003431/// Examine constraint type and operand type and determine a weight value.
3432/// This object must already have been set up with the operand type
3433/// and the current alternative constraint selected.
3434TargetLowering::ConstraintWeight
3435MipsTargetLowering::getSingleConstraintMatchWeight(
3436 AsmOperandInfo &info, const char *constraint) const {
3437 ConstraintWeight weight = CW_Invalid;
3438 Value *CallOperandVal = info.CallOperandVal;
3439 // If we don't have a value, we can't do a match,
3440 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +00003441 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +00003442 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +00003443 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +00003444 // Look at the constraint type.
3445 switch (*constraint) {
3446 default:
3447 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
3448 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00003449 case 'd':
3450 case 'y':
John Thompsone8360b72010-10-29 17:29:13 +00003451 if (type->isIntegerTy())
3452 weight = CW_Register;
3453 break;
Daniel Sanders8b59af12013-11-12 12:56:01 +00003454 case 'f': // FPU or MSA register
Eric Christopher1c29a652014-07-18 22:55:25 +00003455 if (Subtarget.hasMSA() && type->isVectorTy() &&
Daniel Sanders8b59af12013-11-12 12:56:01 +00003456 cast<VectorType>(type)->getBitWidth() == 128)
3457 weight = CW_Register;
3458 else if (type->isFloatTy())
John Thompsone8360b72010-10-29 17:29:13 +00003459 weight = CW_Register;
3460 break;
Eric Christophere3c494d2012-05-07 06:25:10 +00003461 case 'c': // $25 for indirect jumps
Eric Christopher9c492e62012-05-07 06:25:15 +00003462 case 'l': // lo register
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003463 case 'x': // hilo register pair
Daniel Sanders8b59af12013-11-12 12:56:01 +00003464 if (type->isIntegerTy())
Eric Christophere3c494d2012-05-07 06:25:10 +00003465 weight = CW_SpecificReg;
Daniel Sanders8b59af12013-11-12 12:56:01 +00003466 break;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003467 case 'I': // signed 16 bit immediate
Eric Christopher7201e1b2012-05-07 03:13:42 +00003468 case 'J': // integer zero
Eric Christopher3ff88a02012-05-07 05:46:29 +00003469 case 'K': // unsigned 16 bit immediate
Eric Christopher1109b342012-05-07 05:46:37 +00003470 case 'L': // signed 32 bit immediate where lower 16 bits are 0
Eric Christophere07aa432012-05-07 05:46:43 +00003471 case 'N': // immediate in the range of -65535 to -1 (inclusive)
Eric Christopher470578a2012-05-07 05:46:48 +00003472 case 'O': // signed 15 bit immediate (+- 16383)
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003473 case 'P': // immediate in the range of 65535 to 1 (inclusive)
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003474 if (isa<ConstantInt>(CallOperandVal))
3475 weight = CW_Constant;
3476 break;
Jack Carter0e149b02013-03-04 21:33:15 +00003477 case 'R':
3478 weight = CW_Memory;
3479 break;
John Thompsone8360b72010-10-29 17:29:13 +00003480 }
3481 return weight;
3482}
3483
Akira Hatanaka7473b472013-08-14 00:21:25 +00003484/// This is a helper function to parse a physical register string and split it
3485/// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag
3486/// that is returned indicates whether parsing was successful. The second flag
3487/// is true if the numeric part exists.
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003488static std::pair<bool, bool> parsePhysicalReg(StringRef C, StringRef &Prefix,
3489 unsigned long long &Reg) {
Akira Hatanaka7473b472013-08-14 00:21:25 +00003490 if (C.front() != '{' || C.back() != '}')
3491 return std::make_pair(false, false);
3492
3493 // Search for the first numeric character.
3494 StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1;
Craig Topper2241dfd2015-11-23 07:19:06 +00003495 I = std::find_if(B, E, isdigit);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003496
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003497 Prefix = StringRef(B, I - B);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003498
3499 // The second flag is set to false if no numeric characters were found.
3500 if (I == E)
3501 return std::make_pair(true, false);
3502
3503 // Parse the numeric characters.
3504 return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg),
3505 true);
3506}
3507
3508std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering::
Craig Topper6dc4a8bc2014-08-30 16:48:02 +00003509parseRegForInlineAsmConstraint(StringRef C, MVT VT) const {
Eric Christopherd9134482014-08-04 21:25:23 +00003510 const TargetRegisterInfo *TRI =
Eric Christopher96e72c62015-01-29 23:27:36 +00003511 Subtarget.getRegisterInfo();
Akira Hatanaka7473b472013-08-14 00:21:25 +00003512 const TargetRegisterClass *RC;
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003513 StringRef Prefix;
Akira Hatanaka7473b472013-08-14 00:21:25 +00003514 unsigned long long Reg;
3515
3516 std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg);
3517
3518 if (!R.first)
Craig Topper062a2ba2014-04-25 05:30:21 +00003519 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003520
3521 if ((Prefix == "hi" || Prefix == "lo")) { // Parse hi/lo.
3522 // No numeric characters follow "hi" or "lo".
3523 if (R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003524 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003525
3526 RC = TRI->getRegClass(Prefix == "hi" ?
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00003527 Mips::HI32RegClassID : Mips::LO32RegClassID);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003528 return std::make_pair(*(RC->begin()), RC);
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003529 } else if (Prefix.startswith("$msa")) {
Daniel Sanders8b59af12013-11-12 12:56:01 +00003530 // Parse $msa(ir|csr|access|save|modify|request|map|unmap)
3531
3532 // No numeric characters follow the name.
3533 if (R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003534 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003535
3536 Reg = StringSwitch<unsigned long long>(Prefix)
3537 .Case("$msair", Mips::MSAIR)
3538 .Case("$msacsr", Mips::MSACSR)
3539 .Case("$msaaccess", Mips::MSAAccess)
3540 .Case("$msasave", Mips::MSASave)
3541 .Case("$msamodify", Mips::MSAModify)
3542 .Case("$msarequest", Mips::MSARequest)
3543 .Case("$msamap", Mips::MSAMap)
3544 .Case("$msaunmap", Mips::MSAUnmap)
3545 .Default(0);
3546
3547 if (!Reg)
Craig Topper062a2ba2014-04-25 05:30:21 +00003548 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003549
3550 RC = TRI->getRegClass(Mips::MSACtrlRegClassID);
3551 return std::make_pair(Reg, RC);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003552 }
3553
3554 if (!R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003555 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003556
3557 if (Prefix == "$f") { // Parse $f0-$f31.
3558 // If the size of FP registers is 64-bit or Reg is an even number, select
3559 // the 64-bit register class. Otherwise, select the 32-bit register class.
3560 if (VT == MVT::Other)
Eric Christopher1c29a652014-07-18 22:55:25 +00003561 VT = (Subtarget.isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32;
Akira Hatanaka7473b472013-08-14 00:21:25 +00003562
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003563 RC = getRegClassFor(VT);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003564
3565 if (RC == &Mips::AFGR64RegClass) {
3566 assert(Reg % 2 == 0);
3567 Reg >>= 1;
3568 }
Daniel Sanders8b59af12013-11-12 12:56:01 +00003569 } else if (Prefix == "$fcc") // Parse $fcc0-$fcc7.
Akira Hatanaka7473b472013-08-14 00:21:25 +00003570 RC = TRI->getRegClass(Mips::FCCRegClassID);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003571 else if (Prefix == "$w") { // Parse $w0-$w31.
3572 RC = getRegClassFor((VT == MVT::Other) ? MVT::v16i8 : VT);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003573 } else { // Parse $0-$31.
3574 assert(Prefix == "$");
3575 RC = getRegClassFor((VT == MVT::Other) ? MVT::i32 : VT);
3576 }
3577
3578 assert(Reg < RC->getNumRegs());
3579 return std::make_pair(*(RC->begin() + Reg), RC);
3580}
3581
Eric Christophereaf77dc2011-06-29 19:33:04 +00003582/// Given a register class constraint, like 'r', if this corresponds directly
3583/// to an LLVM register class, return a register of 0 and the register class
3584/// pointer.
Eric Christopher11e4df72015-02-26 22:38:43 +00003585std::pair<unsigned, const TargetRegisterClass *>
3586MipsTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003587 StringRef Constraint,
Eric Christopher11e4df72015-02-26 22:38:43 +00003588 MVT VT) const {
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003589 if (Constraint.size() == 1) {
3590 switch (Constraint[0]) {
Eric Christopher9519c082011-06-29 19:04:31 +00003591 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
3592 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003593 case 'r':
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003594 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
Eric Christopher1c29a652014-07-18 22:55:25 +00003595 if (Subtarget.inMips16Mode())
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003596 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003597 return std::make_pair(0U, &Mips::GPR32RegClass);
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003598 }
Eric Christopher1c29a652014-07-18 22:55:25 +00003599 if (VT == MVT::i64 && !Subtarget.isGP64bit())
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003600 return std::make_pair(0U, &Mips::GPR32RegClass);
Eric Christopher1c29a652014-07-18 22:55:25 +00003601 if (VT == MVT::i64 && Subtarget.isGP64bit())
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003602 return std::make_pair(0U, &Mips::GPR64RegClass);
Eric Christopher58daf042012-05-07 03:13:22 +00003603 // This will generate an error message
Craig Topper062a2ba2014-04-25 05:30:21 +00003604 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003605 case 'f': // FPU or MSA register
3606 if (VT == MVT::v16i8)
3607 return std::make_pair(0U, &Mips::MSA128BRegClass);
3608 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
3609 return std::make_pair(0U, &Mips::MSA128HRegClass);
3610 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
3611 return std::make_pair(0U, &Mips::MSA128WRegClass);
3612 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
3613 return std::make_pair(0U, &Mips::MSA128DRegClass);
3614 else if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00003615 return std::make_pair(0U, &Mips::FGR32RegClass);
Eric Christopher1c29a652014-07-18 22:55:25 +00003616 else if ((VT == MVT::f64) && (!Subtarget.isSingleFloat())) {
3617 if (Subtarget.isFP64bit())
Craig Topperc7242e02012-04-20 07:30:17 +00003618 return std::make_pair(0U, &Mips::FGR64RegClass);
3619 return std::make_pair(0U, &Mips::AFGR64RegClass);
Akira Hatanakac669d7a2012-01-04 02:45:01 +00003620 }
Eric Christophere3c494d2012-05-07 06:25:10 +00003621 break;
3622 case 'c': // register suitable for indirect jump
3623 if (VT == MVT::i32)
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003624 return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass);
Eric Christophere3c494d2012-05-07 06:25:10 +00003625 assert(VT == MVT::i64 && "Unexpected type.");
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003626 return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass);
Eric Christopher9c492e62012-05-07 06:25:15 +00003627 case 'l': // register suitable for indirect jump
3628 if (VT == MVT::i32)
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00003629 return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass);
3630 return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass);
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003631 case 'x': // register suitable for indirect jump
3632 // Fixme: Not triggering the use of both hi and low
3633 // This will generate an error message
Craig Topper062a2ba2014-04-25 05:30:21 +00003634 return std::make_pair(0U, nullptr);
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003635 }
3636 }
Akira Hatanaka7473b472013-08-14 00:21:25 +00003637
3638 std::pair<unsigned, const TargetRegisterClass *> R;
3639 R = parseRegForInlineAsmConstraint(Constraint, VT);
3640
3641 if (R.second)
3642 return R;
3643
Eric Christopher11e4df72015-02-26 22:38:43 +00003644 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003645}
3646
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003647/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3648/// vector. If it is invalid, don't add anything to Ops.
3649void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3650 std::string &Constraint,
3651 std::vector<SDValue>&Ops,
3652 SelectionDAG &DAG) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003653 SDLoc DL(Op);
Craig Topper062a2ba2014-04-25 05:30:21 +00003654 SDValue Result;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003655
3656 // Only support length 1 constraints for now.
3657 if (Constraint.length() > 1) return;
3658
3659 char ConstraintLetter = Constraint[0];
3660 switch (ConstraintLetter) {
3661 default: break; // This will fall through to the generic implementation
3662 case 'I': // Signed 16 bit constant
3663 // If this fails, the parent routine will give an error
3664 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3665 EVT Type = Op.getValueType();
3666 int64_t Val = C->getSExtValue();
3667 if (isInt<16>(Val)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003668 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003669 break;
3670 }
3671 }
3672 return;
Eric Christopher7201e1b2012-05-07 03:13:42 +00003673 case 'J': // integer zero
3674 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3675 EVT Type = Op.getValueType();
3676 int64_t Val = C->getZExtValue();
3677 if (Val == 0) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003678 Result = DAG.getTargetConstant(0, DL, Type);
Eric Christopher7201e1b2012-05-07 03:13:42 +00003679 break;
3680 }
3681 }
3682 return;
Eric Christopher3ff88a02012-05-07 05:46:29 +00003683 case 'K': // unsigned 16 bit immediate
3684 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3685 EVT Type = Op.getValueType();
3686 uint64_t Val = (uint64_t)C->getZExtValue();
3687 if (isUInt<16>(Val)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003688 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopher3ff88a02012-05-07 05:46:29 +00003689 break;
3690 }
3691 }
3692 return;
Eric Christopher1109b342012-05-07 05:46:37 +00003693 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3694 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3695 EVT Type = Op.getValueType();
3696 int64_t Val = C->getSExtValue();
3697 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003698 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopher1109b342012-05-07 05:46:37 +00003699 break;
3700 }
3701 }
3702 return;
Eric Christophere07aa432012-05-07 05:46:43 +00003703 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3704 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3705 EVT Type = Op.getValueType();
3706 int64_t Val = C->getSExtValue();
3707 if ((Val >= -65535) && (Val <= -1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003708 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christophere07aa432012-05-07 05:46:43 +00003709 break;
3710 }
3711 }
3712 return;
Eric Christopher470578a2012-05-07 05:46:48 +00003713 case 'O': // signed 15 bit immediate
3714 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3715 EVT Type = Op.getValueType();
3716 int64_t Val = C->getSExtValue();
3717 if ((isInt<15>(Val))) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003718 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopher470578a2012-05-07 05:46:48 +00003719 break;
3720 }
3721 }
3722 return;
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003723 case 'P': // immediate in the range of 1 to 65535 (inclusive)
3724 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3725 EVT Type = Op.getValueType();
3726 int64_t Val = C->getSExtValue();
3727 if ((Val <= 65535) && (Val >= 1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003728 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003729 break;
3730 }
3731 }
3732 return;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003733 }
3734
3735 if (Result.getNode()) {
3736 Ops.push_back(Result);
3737 return;
3738 }
3739
3740 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3741}
3742
Mehdi Amini0cdec1e2015-07-09 02:09:40 +00003743bool MipsTargetLowering::isLegalAddressingMode(const DataLayout &DL,
3744 const AddrMode &AM, Type *Ty,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +00003745 unsigned AS) const {
Akira Hatanakaef839192012-11-17 00:25:41 +00003746 // No global is ever allowed as a base.
3747 if (AM.BaseGV)
3748 return false;
3749
3750 switch (AM.Scale) {
3751 case 0: // "r+i" or just "i", depending on HasBaseReg.
3752 break;
3753 case 1:
3754 if (!AM.HasBaseReg) // allow "r+i".
3755 break;
3756 return false; // disallow "r+r" or "r+r+i".
3757 default:
3758 return false;
3759 }
3760
3761 return true;
3762}
3763
3764bool
Dan Gohman2fe6bee2008-10-18 02:06:02 +00003765MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3766 // The Mips target isn't yet aware of offsets.
3767 return false;
3768}
Evan Cheng16993aa2009-10-27 19:56:55 +00003769
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003770EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003771 unsigned SrcAlign,
3772 bool IsMemset, bool ZeroMemset,
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003773 bool MemcpyStrSrc,
3774 MachineFunction &MF) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003775 if (Subtarget.hasMips64())
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003776 return MVT::i64;
3777
3778 return MVT::i32;
3779}
3780
Evan Cheng83896a52009-10-28 01:43:28 +00003781bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
3782 if (VT != MVT::f32 && VT != MVT::f64)
3783 return false;
Bruno Cardoso Lopesb02a9df2011-01-18 19:41:41 +00003784 if (Imm.isNegZero())
3785 return false;
Evan Cheng16993aa2009-10-27 19:56:55 +00003786 return Imm.isZero();
3787}
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003788
3789unsigned MipsTargetLowering::getJumpTableEncoding() const {
Simon Dardisca74dd72017-01-27 11:36:52 +00003790
3791 // FIXME: For space reasons this should be: EK_GPRel32BlockAddress.
3792 if (ABI.IsN64() && isPositionIndependent())
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003793 return MachineJumpTableInfo::EK_GPRel64BlockAddress;
Jia Liuf54f60f2012-02-28 07:46:26 +00003794
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003795 return TargetLowering::getJumpTableEncoding();
3796}
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003797
Eric Christopher824f42f2015-05-12 01:26:05 +00003798bool MipsTargetLowering::useSoftFloat() const {
3799 return Subtarget.useSoftFloat();
3800}
3801
Daniel Sandersf43e6872014-11-01 18:44:56 +00003802void MipsTargetLowering::copyByValRegs(
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003803 SDValue Chain, const SDLoc &DL, std::vector<SDValue> &OutChains,
3804 SelectionDAG &DAG, const ISD::ArgFlagsTy &Flags,
3805 SmallVectorImpl<SDValue> &InVals, const Argument *FuncArg,
3806 unsigned FirstReg, unsigned LastReg, const CCValAssign &VA,
3807 MipsCCState &State) const {
Akira Hatanaka25dad192012-10-27 00:10:18 +00003808 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00003809 MachineFrameInfo &MFI = MF.getFrameInfo();
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003810 unsigned GPRSizeInBytes = Subtarget.getGPRSizeInBytes();
Daniel Sanders23e98772014-11-02 16:09:29 +00003811 unsigned NumRegs = LastReg - FirstReg;
3812 unsigned RegAreaSize = NumRegs * GPRSizeInBytes;
Akira Hatanaka25dad192012-10-27 00:10:18 +00003813 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
3814 int FrameObjOffset;
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003815 ArrayRef<MCPhysReg> ByValArgRegs = ABI.GetByValArgRegs();
Akira Hatanaka25dad192012-10-27 00:10:18 +00003816
3817 if (RegAreaSize)
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003818 FrameObjOffset =
3819 (int)ABI.GetCalleeAllocdArgSizeInBytes(State.getCallingConv()) -
3820 (int)((ByValArgRegs.size() - FirstReg) * GPRSizeInBytes);
Akira Hatanaka25dad192012-10-27 00:10:18 +00003821 else
Daniel Sandersf43e6872014-11-01 18:44:56 +00003822 FrameObjOffset = VA.getLocMemOffset();
Akira Hatanaka25dad192012-10-27 00:10:18 +00003823
3824 // Create frame object.
Mehdi Amini44ede332015-07-09 02:09:04 +00003825 EVT PtrTy = getPointerTy(DAG.getDataLayout());
Matthias Braun941a7052016-07-28 18:40:00 +00003826 int FI = MFI.CreateFixedObject(FrameObjSize, FrameObjOffset, true);
Akira Hatanaka25dad192012-10-27 00:10:18 +00003827 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
3828 InVals.push_back(FIN);
3829
Daniel Sanders23e98772014-11-02 16:09:29 +00003830 if (!NumRegs)
Akira Hatanaka25dad192012-10-27 00:10:18 +00003831 return;
3832
3833 // Copy arg registers.
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003834 MVT RegTy = MVT::getIntegerVT(GPRSizeInBytes * 8);
Akira Hatanaka25dad192012-10-27 00:10:18 +00003835 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3836
Daniel Sanders23e98772014-11-02 16:09:29 +00003837 for (unsigned I = 0; I < NumRegs; ++I) {
Daniel Sandersd7eba312014-11-07 12:21:37 +00003838 unsigned ArgReg = ByValArgRegs[FirstReg + I];
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003839 unsigned VReg = addLiveIn(MF, ArgReg, RC);
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003840 unsigned Offset = I * GPRSizeInBytes;
Akira Hatanaka25dad192012-10-27 00:10:18 +00003841 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003842 DAG.getConstant(Offset, DL, PtrTy));
Akira Hatanaka25dad192012-10-27 00:10:18 +00003843 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
Justin Lebar9c375812016-07-15 18:27:10 +00003844 StorePtr, MachinePointerInfo(FuncArg, Offset));
Akira Hatanaka25dad192012-10-27 00:10:18 +00003845 OutChains.push_back(Store);
3846 }
3847}
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003848
3849// Copy byVal arg to registers and stack.
Daniel Sandersf43e6872014-11-01 18:44:56 +00003850void MipsTargetLowering::passByValArg(
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003851 SDValue Chain, const SDLoc &DL,
Daniel Sandersf43e6872014-11-01 18:44:56 +00003852 std::deque<std::pair<unsigned, SDValue>> &RegsToPass,
3853 SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr,
Matthias Braun941a7052016-07-28 18:40:00 +00003854 MachineFrameInfo &MFI, SelectionDAG &DAG, SDValue Arg, unsigned FirstReg,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003855 unsigned LastReg, const ISD::ArgFlagsTy &Flags, bool isLittle,
3856 const CCValAssign &VA) const {
Daniel Sandersac272632014-05-23 13:18:02 +00003857 unsigned ByValSizeInBytes = Flags.getByValSize();
3858 unsigned OffsetInBytes = 0; // From beginning of struct
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003859 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
Daniel Sandersac272632014-05-23 13:18:02 +00003860 unsigned Alignment = std::min(Flags.getByValAlign(), RegSizeInBytes);
Mehdi Amini44ede332015-07-09 02:09:04 +00003861 EVT PtrTy = getPointerTy(DAG.getDataLayout()),
3862 RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
Daniel Sanders23e98772014-11-02 16:09:29 +00003863 unsigned NumRegs = LastReg - FirstReg;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003864
Daniel Sanders23e98772014-11-02 16:09:29 +00003865 if (NumRegs) {
Craig Topper862d5d82015-09-28 00:15:34 +00003866 ArrayRef<MCPhysReg> ArgRegs = ABI.GetByValArgRegs();
Daniel Sanders23e98772014-11-02 16:09:29 +00003867 bool LeftoverBytes = (NumRegs * RegSizeInBytes > ByValSizeInBytes);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003868 unsigned I = 0;
3869
3870 // Copy words to registers.
Daniel Sanders23e98772014-11-02 16:09:29 +00003871 for (; I < NumRegs - LeftoverBytes; ++I, OffsetInBytes += RegSizeInBytes) {
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003872 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003873 DAG.getConstant(OffsetInBytes, DL, PtrTy));
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003874 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
Justin Lebar9c375812016-07-15 18:27:10 +00003875 MachinePointerInfo(), Alignment);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003876 MemOpChains.push_back(LoadVal.getValue(1));
Daniel Sanders23e98772014-11-02 16:09:29 +00003877 unsigned ArgReg = ArgRegs[FirstReg + I];
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003878 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
3879 }
3880
3881 // Return if the struct has been fully copied.
Daniel Sandersac272632014-05-23 13:18:02 +00003882 if (ByValSizeInBytes == OffsetInBytes)
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003883 return;
3884
3885 // Copy the remainder of the byval argument with sub-word loads and shifts.
3886 if (LeftoverBytes) {
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003887 SDValue Val;
3888
Daniel Sandersac272632014-05-23 13:18:02 +00003889 for (unsigned LoadSizeInBytes = RegSizeInBytes / 2, TotalBytesLoaded = 0;
3890 OffsetInBytes < ByValSizeInBytes; LoadSizeInBytes /= 2) {
3891 unsigned RemainingSizeInBytes = ByValSizeInBytes - OffsetInBytes;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003892
Daniel Sandersac272632014-05-23 13:18:02 +00003893 if (RemainingSizeInBytes < LoadSizeInBytes)
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003894 continue;
3895
3896 // Load subword.
3897 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003898 DAG.getConstant(OffsetInBytes, DL,
3899 PtrTy));
Daniel Sandersac272632014-05-23 13:18:02 +00003900 SDValue LoadVal = DAG.getExtLoad(
3901 ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr, MachinePointerInfo(),
Justin Lebar9c375812016-07-15 18:27:10 +00003902 MVT::getIntegerVT(LoadSizeInBytes * 8), Alignment);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003903 MemOpChains.push_back(LoadVal.getValue(1));
3904
3905 // Shift the loaded value.
3906 unsigned Shamt;
3907
3908 if (isLittle)
Daniel Sandersac272632014-05-23 13:18:02 +00003909 Shamt = TotalBytesLoaded * 8;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003910 else
Daniel Sandersac272632014-05-23 13:18:02 +00003911 Shamt = (RegSizeInBytes - (TotalBytesLoaded + LoadSizeInBytes)) * 8;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003912
3913 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003914 DAG.getConstant(Shamt, DL, MVT::i32));
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003915
3916 if (Val.getNode())
3917 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
3918 else
3919 Val = Shift;
3920
Daniel Sandersac272632014-05-23 13:18:02 +00003921 OffsetInBytes += LoadSizeInBytes;
3922 TotalBytesLoaded += LoadSizeInBytes;
3923 Alignment = std::min(Alignment, LoadSizeInBytes);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003924 }
3925
Daniel Sanders23e98772014-11-02 16:09:29 +00003926 unsigned ArgReg = ArgRegs[FirstReg + I];
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003927 RegsToPass.push_back(std::make_pair(ArgReg, Val));
3928 return;
3929 }
3930 }
3931
3932 // Copy remainder of byval arg to it with memcpy.
Daniel Sandersac272632014-05-23 13:18:02 +00003933 unsigned MemCpySize = ByValSizeInBytes - OffsetInBytes;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003934 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003935 DAG.getConstant(OffsetInBytes, DL, PtrTy));
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003936 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003937 DAG.getIntPtrConstant(VA.getLocMemOffset(), DL));
3938 Chain = DAG.getMemcpy(Chain, DL, Dst, Src,
3939 DAG.getConstant(MemCpySize, DL, PtrTy),
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003940 Alignment, /*isVolatile=*/false, /*AlwaysInline=*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00003941 /*isTailCall=*/false,
Nick Lewyckyaad475b2014-04-15 07:22:52 +00003942 MachinePointerInfo(), MachinePointerInfo());
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003943 MemOpChains.push_back(Chain);
3944}
Akira Hatanaka2a134022012-10-27 00:21:13 +00003945
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003946void MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003947 SDValue Chain, const SDLoc &DL,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003948 SelectionDAG &DAG,
Daniel Sanders853c2432014-11-01 18:13:52 +00003949 CCState &State) const {
Craig Topper862d5d82015-09-28 00:15:34 +00003950 ArrayRef<MCPhysReg> ArgRegs = ABI.GetVarArgRegs();
Tim Northover3b6b7ca2015-02-21 02:11:17 +00003951 unsigned Idx = State.getFirstUnallocated(ArgRegs);
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003952 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
3953 MVT RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003954 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3955 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00003956 MachineFrameInfo &MFI = MF.getFrameInfo();
Akira Hatanaka2a134022012-10-27 00:21:13 +00003957 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3958
3959 // Offset of the first variable argument from stack pointer.
3960 int VaArgOffset;
3961
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003962 if (ArgRegs.size() == Idx)
Rui Ueyamada00f2f2016-01-14 21:06:47 +00003963 VaArgOffset = alignTo(State.getNextStackOffset(), RegSizeInBytes);
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003964 else {
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003965 VaArgOffset =
3966 (int)ABI.GetCalleeAllocdArgSizeInBytes(State.getCallingConv()) -
3967 (int)(RegSizeInBytes * (ArgRegs.size() - Idx));
3968 }
Akira Hatanaka2a134022012-10-27 00:21:13 +00003969
3970 // Record the frame index of the first variable argument
3971 // which is a value necessary to VASTART.
Matthias Braun941a7052016-07-28 18:40:00 +00003972 int FI = MFI.CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003973 MipsFI->setVarArgsFrameIndex(FI);
3974
3975 // Copy the integer registers that have not been used for argument passing
3976 // to the argument register save area. For O32, the save area is allocated
3977 // in the caller's stack frame, while for N32/64, it is allocated in the
3978 // callee's stack frame.
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003979 for (unsigned I = Idx; I < ArgRegs.size();
3980 ++I, VaArgOffset += RegSizeInBytes) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003981 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003982 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
Matthias Braun941a7052016-07-28 18:40:00 +00003983 FI = MFI.CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
Mehdi Amini44ede332015-07-09 02:09:04 +00003984 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Justin Lebar9c375812016-07-15 18:27:10 +00003985 SDValue Store =
3986 DAG.getStore(Chain, DL, ArgValue, PtrOff, MachinePointerInfo());
Eric Christopher1c29a652014-07-18 22:55:25 +00003987 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(
3988 (Value *)nullptr);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003989 OutChains.push_back(Store);
3990 }
3991}
Daniel Sanders23e98772014-11-02 16:09:29 +00003992
3993void MipsTargetLowering::HandleByVal(CCState *State, unsigned &Size,
3994 unsigned Align) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003995 const TargetFrameLowering *TFL = Subtarget.getFrameLowering();
Daniel Sanders23e98772014-11-02 16:09:29 +00003996
3997 assert(Size && "Byval argument's size shouldn't be 0.");
3998
3999 Align = std::min(Align, TFL->getStackAlignment());
4000
4001 unsigned FirstReg = 0;
4002 unsigned NumRegs = 0;
4003
4004 if (State->getCallingConv() != CallingConv::Fast) {
4005 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
Craig Topper862d5d82015-09-28 00:15:34 +00004006 ArrayRef<MCPhysReg> IntArgRegs = ABI.GetByValArgRegs();
Daniel Sanders23e98772014-11-02 16:09:29 +00004007 // FIXME: The O32 case actually describes no shadow registers.
4008 const MCPhysReg *ShadowRegs =
Eric Christopher96e72c62015-01-29 23:27:36 +00004009 ABI.IsO32() ? IntArgRegs.data() : Mips64DPRegs;
Daniel Sanders23e98772014-11-02 16:09:29 +00004010
4011 // We used to check the size as well but we can't do that anymore since
4012 // CCState::HandleByVal() rounds up the size after calling this function.
4013 assert(!(Align % RegSizeInBytes) &&
4014 "Byval argument's alignment should be a multiple of"
4015 "RegSizeInBytes.");
4016
Tim Northover3b6b7ca2015-02-21 02:11:17 +00004017 FirstReg = State->getFirstUnallocated(IntArgRegs);
Daniel Sanders23e98772014-11-02 16:09:29 +00004018
4019 // If Align > RegSizeInBytes, the first arg register must be even.
4020 // FIXME: This condition happens to do the right thing but it's not the
4021 // right way to test it. We want to check that the stack frame offset
4022 // of the register is aligned.
4023 if ((Align > RegSizeInBytes) && (FirstReg % 2)) {
4024 State->AllocateReg(IntArgRegs[FirstReg], ShadowRegs[FirstReg]);
4025 ++FirstReg;
4026 }
4027
4028 // Mark the registers allocated.
Rui Ueyamada00f2f2016-01-14 21:06:47 +00004029 Size = alignTo(Size, RegSizeInBytes);
Daniel Sanders23e98772014-11-02 16:09:29 +00004030 for (unsigned I = FirstReg; Size > 0 && (I < IntArgRegs.size());
4031 Size -= RegSizeInBytes, ++I, ++NumRegs)
4032 State->AllocateReg(IntArgRegs[I], ShadowRegs[I]);
4033 }
4034
4035 State->addInRegsParamInfo(FirstReg, FirstReg + NumRegs);
4036}
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00004037
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004038MachineBasicBlock *MipsTargetLowering::emitPseudoSELECT(MachineInstr &MI,
4039 MachineBasicBlock *BB,
4040 bool isFPCmp,
4041 unsigned Opc) const {
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00004042 assert(!(Subtarget.hasMips4() || Subtarget.hasMips32()) &&
4043 "Subtarget already supports SELECT nodes with the use of"
4044 "conditional-move instructions.");
4045
4046 const TargetInstrInfo *TII =
Eric Christopher96e72c62015-01-29 23:27:36 +00004047 Subtarget.getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004048 DebugLoc DL = MI.getDebugLoc();
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00004049
4050 // To "insert" a SELECT instruction, we actually have to insert the
4051 // diamond control-flow pattern. The incoming instruction knows the
4052 // destination vreg to set, the condition code register to branch on, the
4053 // true/false values to select between, and a branch opcode to use.
4054 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +00004055 MachineFunction::iterator It = ++BB->getIterator();
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00004056
4057 // thisMBB:
4058 // ...
4059 // TrueVal = ...
4060 // setcc r1, r2, r3
4061 // bNE r1, r0, copy1MBB
4062 // fallthrough --> copy0MBB
4063 MachineBasicBlock *thisMBB = BB;
4064 MachineFunction *F = BB->getParent();
4065 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
4066 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
4067 F->insert(It, copy0MBB);
4068 F->insert(It, sinkMBB);
4069
4070 // Transfer the remainder of BB and its successor edges to sinkMBB.
4071 sinkMBB->splice(sinkMBB->begin(), BB,
4072 std::next(MachineBasicBlock::iterator(MI)), BB->end());
4073 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
4074
4075 // Next, add the true and fallthrough blocks as its successors.
4076 BB->addSuccessor(copy0MBB);
4077 BB->addSuccessor(sinkMBB);
4078
4079 if (isFPCmp) {
4080 // bc1[tf] cc, sinkMBB
4081 BuildMI(BB, DL, TII->get(Opc))
Simon Dardisba92b032016-09-09 11:06:01 +00004082 .addReg(MI.getOperand(1).getReg())
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004083 .addMBB(sinkMBB);
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00004084 } else {
4085 // bne rs, $0, sinkMBB
4086 BuildMI(BB, DL, TII->get(Opc))
Simon Dardisba92b032016-09-09 11:06:01 +00004087 .addReg(MI.getOperand(1).getReg())
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004088 .addReg(Mips::ZERO)
4089 .addMBB(sinkMBB);
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00004090 }
4091
4092 // copy0MBB:
4093 // %FalseValue = ...
4094 // # fallthrough to sinkMBB
4095 BB = copy0MBB;
4096
4097 // Update machine-CFG edges
4098 BB->addSuccessor(sinkMBB);
4099
4100 // sinkMBB:
4101 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
4102 // ...
4103 BB = sinkMBB;
4104
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004105 BuildMI(*BB, BB->begin(), DL, TII->get(Mips::PHI), MI.getOperand(0).getReg())
Simon Dardisba92b032016-09-09 11:06:01 +00004106 .addReg(MI.getOperand(2).getReg())
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004107 .addMBB(thisMBB)
4108 .addReg(MI.getOperand(3).getReg())
4109 .addMBB(copy0MBB);
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00004110
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004111 MI.eraseFromParent(); // The pseudo instruction is gone now.
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00004112
4113 return BB;
4114}
Daniel Sanders1440bb22015-01-09 17:21:30 +00004115
4116// FIXME? Maybe this could be a TableGen attribute on some registers and
4117// this table could be generated automatically from RegInfo.
Pat Gavlina717f252015-07-09 17:40:29 +00004118unsigned MipsTargetLowering::getRegisterByName(const char* RegName, EVT VT,
4119 SelectionDAG &DAG) const {
Daniel Sanders1440bb22015-01-09 17:21:30 +00004120 // Named registers is expected to be fairly rare. For now, just support $28
4121 // since the linux kernel uses it.
4122 if (Subtarget.isGP64bit()) {
4123 unsigned Reg = StringSwitch<unsigned>(RegName)
4124 .Case("$28", Mips::GP_64)
4125 .Default(0);
4126 if (Reg)
4127 return Reg;
4128 } else {
4129 unsigned Reg = StringSwitch<unsigned>(RegName)
4130 .Case("$28", Mips::GP)
4131 .Default(0);
4132 if (Reg)
4133 return Reg;
4134 }
4135 report_fatal_error("Invalid register name global variable");
4136}