blob: fcd0450a0671aa7c6b50a010ba91eff6cc403364 [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";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000115 case MipsISD::Hi: return "MipsISD::Hi";
116 case MipsISD::Lo: return "MipsISD::Lo";
117 case MipsISD::GPRel: return "MipsISD::GPRel";
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +0000118 case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000119 case MipsISD::Ret: return "MipsISD::Ret";
Vasileios Kalintiris43dff0c2015-10-26 12:38:43 +0000120 case MipsISD::ERet: return "MipsISD::ERet";
Akira Hatanakac0b02062013-01-30 00:26:49 +0000121 case MipsISD::EH_RETURN: return "MipsISD::EH_RETURN";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000122 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
123 case MipsISD::FPCmp: return "MipsISD::FPCmp";
124 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
125 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000126 case MipsISD::TruncIntFP: return "MipsISD::TruncIntFP";
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000127 case MipsISD::MFHI: return "MipsISD::MFHI";
128 case MipsISD::MFLO: return "MipsISD::MFLO";
129 case MipsISD::MTLOHI: return "MipsISD::MTLOHI";
Akira Hatanaka28721bd2013-03-30 01:14:04 +0000130 case MipsISD::Mult: return "MipsISD::Mult";
131 case MipsISD::Multu: return "MipsISD::Multu";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000132 case MipsISD::MAdd: return "MipsISD::MAdd";
133 case MipsISD::MAddu: return "MipsISD::MAddu";
134 case MipsISD::MSub: return "MipsISD::MSub";
135 case MipsISD::MSubu: return "MipsISD::MSubu";
136 case MipsISD::DivRem: return "MipsISD::DivRem";
137 case MipsISD::DivRemU: return "MipsISD::DivRemU";
Akira Hatanaka28721bd2013-03-30 01:14:04 +0000138 case MipsISD::DivRem16: return "MipsISD::DivRem16";
139 case MipsISD::DivRemU16: return "MipsISD::DivRemU16";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000140 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
141 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
Akira Hatanakafaa88c02011-12-12 22:38:19 +0000142 case MipsISD::Wrapper: return "MipsISD::Wrapper";
Matthias Braund04893f2015-05-07 21:33:59 +0000143 case MipsISD::DynAlloc: return "MipsISD::DynAlloc";
Akira Hatanakaa4c09bc2011-07-19 23:30:50 +0000144 case MipsISD::Sync: return "MipsISD::Sync";
Akira Hatanaka5360f882011-08-17 02:05:42 +0000145 case MipsISD::Ext: return "MipsISD::Ext";
146 case MipsISD::Ins: return "MipsISD::Ins";
Akira Hatanakab9ebf8d2012-06-02 00:03:12 +0000147 case MipsISD::LWL: return "MipsISD::LWL";
148 case MipsISD::LWR: return "MipsISD::LWR";
149 case MipsISD::SWL: return "MipsISD::SWL";
150 case MipsISD::SWR: return "MipsISD::SWR";
151 case MipsISD::LDL: return "MipsISD::LDL";
152 case MipsISD::LDR: return "MipsISD::LDR";
153 case MipsISD::SDL: return "MipsISD::SDL";
154 case MipsISD::SDR: return "MipsISD::SDR";
Akira Hatanaka233ac532012-09-21 23:52:47 +0000155 case MipsISD::EXTP: return "MipsISD::EXTP";
156 case MipsISD::EXTPDP: return "MipsISD::EXTPDP";
157 case MipsISD::EXTR_S_H: return "MipsISD::EXTR_S_H";
158 case MipsISD::EXTR_W: return "MipsISD::EXTR_W";
159 case MipsISD::EXTR_R_W: return "MipsISD::EXTR_R_W";
160 case MipsISD::EXTR_RS_W: return "MipsISD::EXTR_RS_W";
161 case MipsISD::SHILO: return "MipsISD::SHILO";
162 case MipsISD::MTHLIP: return "MipsISD::MTHLIP";
Matthias Braund04893f2015-05-07 21:33:59 +0000163 case MipsISD::MULSAQ_S_W_PH: return "MipsISD::MULSAQ_S_W_PH";
164 case MipsISD::MAQ_S_W_PHL: return "MipsISD::MAQ_S_W_PHL";
165 case MipsISD::MAQ_S_W_PHR: return "MipsISD::MAQ_S_W_PHR";
166 case MipsISD::MAQ_SA_W_PHL: return "MipsISD::MAQ_SA_W_PHL";
167 case MipsISD::MAQ_SA_W_PHR: return "MipsISD::MAQ_SA_W_PHR";
168 case MipsISD::DPAU_H_QBL: return "MipsISD::DPAU_H_QBL";
169 case MipsISD::DPAU_H_QBR: return "MipsISD::DPAU_H_QBR";
170 case MipsISD::DPSU_H_QBL: return "MipsISD::DPSU_H_QBL";
171 case MipsISD::DPSU_H_QBR: return "MipsISD::DPSU_H_QBR";
172 case MipsISD::DPAQ_S_W_PH: return "MipsISD::DPAQ_S_W_PH";
173 case MipsISD::DPSQ_S_W_PH: return "MipsISD::DPSQ_S_W_PH";
174 case MipsISD::DPAQ_SA_L_W: return "MipsISD::DPAQ_SA_L_W";
175 case MipsISD::DPSQ_SA_L_W: return "MipsISD::DPSQ_SA_L_W";
176 case MipsISD::DPA_W_PH: return "MipsISD::DPA_W_PH";
177 case MipsISD::DPS_W_PH: return "MipsISD::DPS_W_PH";
178 case MipsISD::DPAQX_S_W_PH: return "MipsISD::DPAQX_S_W_PH";
179 case MipsISD::DPAQX_SA_W_PH: return "MipsISD::DPAQX_SA_W_PH";
180 case MipsISD::DPAX_W_PH: return "MipsISD::DPAX_W_PH";
181 case MipsISD::DPSX_W_PH: return "MipsISD::DPSX_W_PH";
182 case MipsISD::DPSQX_S_W_PH: return "MipsISD::DPSQX_S_W_PH";
183 case MipsISD::DPSQX_SA_W_PH: return "MipsISD::DPSQX_SA_W_PH";
184 case MipsISD::MULSA_W_PH: return "MipsISD::MULSA_W_PH";
Akira Hatanaka233ac532012-09-21 23:52:47 +0000185 case MipsISD::MULT: return "MipsISD::MULT";
186 case MipsISD::MULTU: return "MipsISD::MULTU";
Jia Liu434874d2013-03-04 01:06:54 +0000187 case MipsISD::MADD_DSP: return "MipsISD::MADD_DSP";
Akira Hatanaka233ac532012-09-21 23:52:47 +0000188 case MipsISD::MADDU_DSP: return "MipsISD::MADDU_DSP";
189 case MipsISD::MSUB_DSP: return "MipsISD::MSUB_DSP";
190 case MipsISD::MSUBU_DSP: return "MipsISD::MSUBU_DSP";
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000191 case MipsISD::SHLL_DSP: return "MipsISD::SHLL_DSP";
192 case MipsISD::SHRA_DSP: return "MipsISD::SHRA_DSP";
193 case MipsISD::SHRL_DSP: return "MipsISD::SHRL_DSP";
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000194 case MipsISD::SETCC_DSP: return "MipsISD::SETCC_DSP";
195 case MipsISD::SELECT_CC_DSP: return "MipsISD::SELECT_CC_DSP";
Daniel Sandersce09d072013-08-28 12:14:50 +0000196 case MipsISD::VALL_ZERO: return "MipsISD::VALL_ZERO";
197 case MipsISD::VANY_ZERO: return "MipsISD::VANY_ZERO";
198 case MipsISD::VALL_NONZERO: return "MipsISD::VALL_NONZERO";
199 case MipsISD::VANY_NONZERO: return "MipsISD::VANY_NONZERO";
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000200 case MipsISD::VCEQ: return "MipsISD::VCEQ";
201 case MipsISD::VCLE_S: return "MipsISD::VCLE_S";
202 case MipsISD::VCLE_U: return "MipsISD::VCLE_U";
203 case MipsISD::VCLT_S: return "MipsISD::VCLT_S";
204 case MipsISD::VCLT_U: return "MipsISD::VCLT_U";
Daniel Sanders3ce56622013-09-24 12:18:31 +0000205 case MipsISD::VSMAX: return "MipsISD::VSMAX";
206 case MipsISD::VSMIN: return "MipsISD::VSMIN";
207 case MipsISD::VUMAX: return "MipsISD::VUMAX";
208 case MipsISD::VUMIN: return "MipsISD::VUMIN";
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000209 case MipsISD::VEXTRACT_SEXT_ELT: return "MipsISD::VEXTRACT_SEXT_ELT";
210 case MipsISD::VEXTRACT_ZEXT_ELT: return "MipsISD::VEXTRACT_ZEXT_ELT";
Daniel Sandersf7456c72013-09-23 13:22:24 +0000211 case MipsISD::VNOR: return "MipsISD::VNOR";
Daniel Sanderse5087042013-09-24 14:02:15 +0000212 case MipsISD::VSHF: return "MipsISD::VSHF";
Daniel Sanders26307182013-09-24 14:20:00 +0000213 case MipsISD::SHF: return "MipsISD::SHF";
Daniel Sanders2ed228b2013-09-24 14:36:12 +0000214 case MipsISD::ILVEV: return "MipsISD::ILVEV";
215 case MipsISD::ILVOD: return "MipsISD::ILVOD";
216 case MipsISD::ILVL: return "MipsISD::ILVL";
217 case MipsISD::ILVR: return "MipsISD::ILVR";
Daniel Sandersfae5f2a2013-09-24 14:53:25 +0000218 case MipsISD::PCKEV: return "MipsISD::PCKEV";
219 case MipsISD::PCKOD: return "MipsISD::PCKOD";
Daniel Sandersb50ccf82014-04-01 10:35:28 +0000220 case MipsISD::INSVE: return "MipsISD::INSVE";
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000221 }
Matthias Braund04893f2015-05-07 21:33:59 +0000222 return nullptr;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000223}
224
Eric Christopherb1526602014-09-19 23:30:42 +0000225MipsTargetLowering::MipsTargetLowering(const MipsTargetMachine &TM,
Eric Christopher8924d272014-07-18 23:25:04 +0000226 const MipsSubtarget &STI)
Eric Christopher96e72c62015-01-29 23:27:36 +0000227 : TargetLowering(TM), Subtarget(STI), ABI(TM.getABI()) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000228 // Mips does not have i1 type, so use i32 for
Wesley Peck527da1b2010-11-23 03:31:01 +0000229 // setcc operations results (slt, sgt, ...).
Duncan Sands8d6e2e12008-11-23 15:47:28 +0000230 setBooleanContents(ZeroOrOneBooleanContent);
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000231 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Daniel Sanderscbd44c52014-07-10 10:18:12 +0000232 // The cmp.cond.fmt instruction in MIPS32r6/MIPS64r6 uses 0 and -1 like MSA
233 // does. Integer booleans still use 0 and 1.
Eric Christopher1c29a652014-07-18 22:55:25 +0000234 if (Subtarget.hasMips32r6())
Daniel Sanderscbd44c52014-07-10 10:18:12 +0000235 setBooleanContents(ZeroOrOneBooleanContent,
236 ZeroOrNegativeOneBooleanContent);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000237
Wesley Peck527da1b2010-11-23 03:31:01 +0000238 // Load extented operations for i1 types must be promoted
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000239 for (MVT VT : MVT::integer_valuetypes()) {
240 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
241 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
242 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
243 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000244
Pirama Arumuga Nainar34056de2015-04-20 20:15:36 +0000245 // MIPS doesn't have extending float->double load/store. Set LoadExtAction
246 // for f32, f16
247 for (MVT VT : MVT::fp_valuetypes()) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000248 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
Pirama Arumuga Nainar34056de2015-04-20 20:15:36 +0000249 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f16, Expand);
250 }
251
252 // Set LoadExtAction for f16 vectors to Expand
253 for (MVT VT : MVT::fp_vector_valuetypes()) {
254 MVT F16VT = MVT::getVectorVT(MVT::f16, VT.getVectorNumElements());
255 if (F16VT.isValid())
256 setLoadExtAction(ISD::EXTLOAD, VT, F16VT, Expand);
257 }
258
259 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
260 setTruncStoreAction(MVT::f64, MVT::f16, Expand);
261
Owen Anderson9f944592009-08-11 20:47:22 +0000262 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman39d6faa2009-07-17 02:28:12 +0000263
Wesley Peck527da1b2010-11-23 03:31:01 +0000264 // Used by legalize types to correctly generate the setcc result.
265 // Without this, every float setcc comes with a AND/OR with the result,
266 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +0000267 // which is used implicitly by brcond and select operations.
Vasileios Kalintiris36901dd2016-03-01 20:25:43 +0000268 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +0000269
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000270 // Mips Custom Operations
Akira Hatanaka0f693a82013-03-06 21:32:03 +0000271 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000272 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000273 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000274 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
275 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
276 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Simon Dardisba92b032016-09-09 11:06:01 +0000277 setOperationAction(ISD::SELECT, MVT::f32, Custom);
278 setOperationAction(ISD::SELECT, MVT::f64, Custom);
279 setOperationAction(ISD::SELECT, MVT::i32, Custom);
280 setOperationAction(ISD::SETCC, MVT::f32, Custom);
281 setOperationAction(ISD::SETCC, MVT::f64, Custom);
282 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000283 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
284 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000285 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000286
Eric Christopher1c29a652014-07-18 22:55:25 +0000287 if (Subtarget.isGP64bit()) {
Akira Hatanakada00aa82012-03-10 00:03:50 +0000288 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
289 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
290 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
291 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
292 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
Simon Dardisba92b032016-09-09 11:06:01 +0000293 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Akira Hatanaka019e5922012-06-02 00:04:42 +0000294 setOperationAction(ISD::LOAD, MVT::i64, Custom);
295 setOperationAction(ISD::STORE, MVT::i64, Custom);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000296 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +0000297 setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom);
298 setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom);
299 setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000300 }
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +0000301
Eric Christopher1c29a652014-07-18 22:55:25 +0000302 if (!Subtarget.isGP64bit()) {
Akira Hatanaka0a8ab712012-05-09 00:55:21 +0000303 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
304 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
305 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
306 }
307
Hal Finkel5081ac22016-09-01 10:28:47 +0000308 setOperationAction(ISD::EH_DWARF_CFA, MVT::i32, Custom);
Eric Christopher1c29a652014-07-18 22:55:25 +0000309 if (Subtarget.isGP64bit())
Hal Finkel5081ac22016-09-01 10:28:47 +0000310 setOperationAction(ISD::EH_DWARF_CFA, MVT::i64, Custom);
Akira Hatanaka28e02ec2012-11-07 19:10:58 +0000311
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000312 setOperationAction(ISD::SDIV, MVT::i32, Expand);
313 setOperationAction(ISD::SREM, MVT::i32, Expand);
314 setOperationAction(ISD::UDIV, MVT::i32, Expand);
315 setOperationAction(ISD::UREM, MVT::i32, Expand);
Akira Hatanakab1538f92011-10-03 21:06:13 +0000316 setOperationAction(ISD::SDIV, MVT::i64, Expand);
317 setOperationAction(ISD::SREM, MVT::i64, Expand);
318 setOperationAction(ISD::UDIV, MVT::i64, Expand);
319 setOperationAction(ISD::UREM, MVT::i64, Expand);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000320
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000321 // Operations not directly supported by Mips.
Tom Stellardb1588fc2013-03-08 15:36:57 +0000322 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
323 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
324 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
325 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
Tom Stellard3787b122014-06-10 16:01:29 +0000326 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
327 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
Matt Arsenaulta0e5cd52016-01-11 16:44:48 +0000328 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
329 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000330 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Akira Hatanaka79aed152011-12-20 23:40:56 +0000331 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000332 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Akira Hatanaka79aed152011-12-20 23:40:56 +0000333 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000334 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Eric Christopher1c29a652014-07-18 22:55:25 +0000335 if (Subtarget.hasCnMips()) {
Kai Nacke93fe5e82014-03-20 11:51:58 +0000336 setOperationAction(ISD::CTPOP, MVT::i32, Legal);
337 setOperationAction(ISD::CTPOP, MVT::i64, Legal);
338 } else {
339 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
340 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
341 }
Owen Anderson9f944592009-08-11 20:47:22 +0000342 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
Akira Hatanaka410ce9c2011-12-21 00:14:05 +0000343 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000344 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Akira Hatanaka7ba8a8d2011-09-30 18:51:46 +0000345 setOperationAction(ISD::ROTL, MVT::i64, Expand);
Akira Hatanaka33a25af2012-07-31 20:54:48 +0000346 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
347 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Bruno Cardoso Lopesd47180e2010-12-09 17:32:30 +0000348
Eric Christopher1c29a652014-07-18 22:55:25 +0000349 if (!Subtarget.hasMips32r2())
Bruno Cardoso Lopesd47180e2010-12-09 17:32:30 +0000350 setOperationAction(ISD::ROTR, MVT::i32, Expand);
351
Eric Christopher1c29a652014-07-18 22:55:25 +0000352 if (!Subtarget.hasMips64r2())
Akira Hatanaka7ba8a8d2011-09-30 18:51:46 +0000353 setOperationAction(ISD::ROTR, MVT::i64, Expand);
354
Owen Anderson9f944592009-08-11 20:47:22 +0000355 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes22b69db2011-03-04 18:54:14 +0000356 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000357 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes22b69db2011-03-04 18:54:14 +0000358 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000359 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
360 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000361 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
362 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanakadfb8cda2011-05-23 22:23:58 +0000363 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000364 setOperationAction(ISD::FLOG, MVT::f32, Expand);
365 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
366 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
367 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000368 setOperationAction(ISD::FMA, MVT::f32, Expand);
369 setOperationAction(ISD::FMA, MVT::f64, Expand);
Akira Hatanaka0603ad82012-03-29 18:43:11 +0000370 setOperationAction(ISD::FREM, MVT::f32, Expand);
371 setOperationAction(ISD::FREM, MVT::f64, Expand);
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000372
Pirama Arumuga Nainar34056de2015-04-20 20:15:36 +0000373 // Lower f16 conversion operations into library calls
374 setOperationAction(ISD::FP16_TO_FP, MVT::f32, Expand);
375 setOperationAction(ISD::FP_TO_FP16, MVT::f32, Expand);
376 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand);
377 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Expand);
378
Akira Hatanakac0b02062013-01-30 00:26:49 +0000379 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
380
Daniel Sanders2b553d42014-08-01 09:17:39 +0000381 setOperationAction(ISD::VASTART, MVT::Other, Custom);
382 setOperationAction(ISD::VAARG, MVT::Other, Custom);
Bruno Cardoso Lopes048ffab2011-03-09 19:22:22 +0000383 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
384 setOperationAction(ISD::VAEND, MVT::Other, Expand);
385
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000386 // Use the default for now
Owen Anderson9f944592009-08-11 20:47:22 +0000387 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
388 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman26a48482011-07-27 22:21:52 +0000389
Vasileios Kalintirisb04672c2015-11-06 12:07:20 +0000390 if (!Subtarget.isGP64bit()) {
391 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
392 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
393 }
Eli Friedman7dfa7912011-08-29 18:23:02 +0000394
Eli Friedman30a49e92011-08-03 21:06:02 +0000395
Eric Christopher1c29a652014-07-18 22:55:25 +0000396 if (!Subtarget.hasMips32r2()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000397 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
398 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000399 }
400
Daniel Sanders070fd1c2014-05-12 12:41:59 +0000401 // MIPS16 lacks MIPS32's clz and clo instructions.
Eric Christopher1c29a652014-07-18 22:55:25 +0000402 if (!Subtarget.hasMips32() || Subtarget.inMips16Mode())
Owen Anderson9f944592009-08-11 20:47:22 +0000403 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Eric Christopher1c29a652014-07-18 22:55:25 +0000404 if (!Subtarget.hasMips64())
Akira Hatanaka1d8efab2011-12-21 00:20:27 +0000405 setOperationAction(ISD::CTLZ, MVT::i64, Expand);
Bruno Cardoso Lopes93da7e62008-08-08 06:16:31 +0000406
Eric Christopher1c29a652014-07-18 22:55:25 +0000407 if (!Subtarget.hasMips32r2())
Owen Anderson9f944592009-08-11 20:47:22 +0000408 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Eric Christopher1c29a652014-07-18 22:55:25 +0000409 if (!Subtarget.hasMips64r2())
Akira Hatanaka4706ac92011-12-20 23:56:43 +0000410 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000411
Eric Christopher1c29a652014-07-18 22:55:25 +0000412 if (Subtarget.isGP64bit()) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000413 setLoadExtAction(ISD::SEXTLOAD, MVT::i64, MVT::i32, Custom);
414 setLoadExtAction(ISD::ZEXTLOAD, MVT::i64, MVT::i32, Custom);
415 setLoadExtAction(ISD::EXTLOAD, MVT::i64, MVT::i32, Custom);
Akira Hatanaka019e5922012-06-02 00:04:42 +0000416 setTruncStoreAction(MVT::i64, MVT::i32, Custom);
417 }
418
Akira Hatanakaa3d9ab92013-07-26 20:58:55 +0000419 setOperationAction(ISD::TRAP, MVT::Other, Legal);
420
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000421 setTargetDAGCombine(ISD::SDIVREM);
422 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka5e152182012-03-08 03:26:37 +0000423 setTargetDAGCombine(ISD::SELECT);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000424 setTargetDAGCombine(ISD::AND);
425 setTargetDAGCombine(ISD::OR);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000426 setTargetDAGCombine(ISD::ADD);
Vasileios Kalintiris3751d412016-04-13 15:07:45 +0000427 setTargetDAGCombine(ISD::AssertZext);
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000428
Vasileios Kalintiris1ed49fd2016-09-07 10:01:18 +0000429 if (ABI.IsO32()) {
430 // These libcalls are not available in 32-bit.
431 setLibcallName(RTLIB::SHL_I128, nullptr);
432 setLibcallName(RTLIB::SRL_I128, nullptr);
433 setLibcallName(RTLIB::SRA_I128, nullptr);
434 }
435
Eric Christopher1c29a652014-07-18 22:55:25 +0000436 setMinFunctionAlignment(Subtarget.isGP64bit() ? 3 : 2);
Eli Friedman2518f832011-05-06 20:34:06 +0000437
Daniel Sanders2b553d42014-08-01 09:17:39 +0000438 // The arguments on the stack are defined in terms of 4-byte slots on O32
439 // and 8-byte slots on N32/N64.
Eric Christopher96e72c62015-01-29 23:27:36 +0000440 setMinStackArgumentAlignment((ABI.IsN32() || ABI.IsN64()) ? 8 : 4);
Daniel Sanders2b553d42014-08-01 09:17:39 +0000441
Eric Christopher96e72c62015-01-29 23:27:36 +0000442 setStackPointerRegisterToSaveRestore(ABI.IsN64() ? Mips::SP_64 : Mips::SP);
Akira Hatanakaaa560002011-05-26 18:59:03 +0000443
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000444 MaxStoresPerMemcpy = 16;
Zoran Jovanovicff9d5f32013-12-19 16:12:56 +0000445
Eric Christopher1c29a652014-07-18 22:55:25 +0000446 isMicroMips = Subtarget.inMicroMipsMode();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000447}
448
Eric Christopherb1526602014-09-19 23:30:42 +0000449const MipsTargetLowering *MipsTargetLowering::create(const MipsTargetMachine &TM,
Eric Christopher8924d272014-07-18 23:25:04 +0000450 const MipsSubtarget &STI) {
451 if (STI.inMips16Mode())
452 return llvm::createMips16TargetLowering(TM, STI);
Jia Liuf54f60f2012-02-28 07:46:26 +0000453
Eric Christopher8924d272014-07-18 23:25:04 +0000454 return llvm::createMipsSETargetLowering(TM, STI);
Akira Hatanaka2fcc1cf2011-08-12 21:30:06 +0000455}
456
Reed Kotler720c5ca2014-04-17 22:15:34 +0000457// Create a fast isel object.
458FastISel *
459MipsTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
460 const TargetLibraryInfo *libInfo) const {
Vasileios Kalintiris2041b1d2015-07-30 12:39:33 +0000461 if (!funcInfo.MF->getTarget().Options.EnableFastISel)
Reed Kotler720c5ca2014-04-17 22:15:34 +0000462 return TargetLowering::createFastISel(funcInfo, libInfo);
463 return Mips::createFastISel(funcInfo, libInfo);
464}
465
Mehdi Amini44ede332015-07-09 02:09:04 +0000466EVT MipsTargetLowering::getSetCCResultType(const DataLayout &, LLVMContext &,
467 EVT VT) const {
Vasileios Kalintiris36901dd2016-03-01 20:25:43 +0000468 if (!VT.isVector())
469 return MVT::i32;
470 return VT.changeVectorElementTypeToInteger();
Scott Michela6729e82008-03-10 15:42:14 +0000471}
472
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000473static SDValue performDivRemCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000474 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000475 const MipsSubtarget &Subtarget) {
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000476 if (DCI.isBeforeLegalizeOps())
477 return SDValue();
478
Akira Hatanakab1538f92011-10-03 21:06:13 +0000479 EVT Ty = N->getValueType(0);
Akira Hatanaka8002a3f2013-08-14 00:47:08 +0000480 unsigned LO = (Ty == MVT::i32) ? Mips::LO0 : Mips::LO0_64;
481 unsigned HI = (Ty == MVT::i32) ? Mips::HI0 : Mips::HI0_64;
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000482 unsigned Opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem16 :
483 MipsISD::DivRemU16;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000484 SDLoc DL(N);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000485
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000486 SDValue DivRem = DAG.getNode(Opc, DL, MVT::Glue,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000487 N->getOperand(0), N->getOperand(1));
488 SDValue InChain = DAG.getEntryNode();
489 SDValue InGlue = DivRem;
490
491 // insert MFLO
492 if (N->hasAnyUseOfValue(0)) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000493 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, DL, LO, Ty,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000494 InGlue);
495 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
496 InChain = CopyFromLo.getValue(1);
497 InGlue = CopyFromLo.getValue(2);
498 }
499
500 // insert MFHI
501 if (N->hasAnyUseOfValue(1)) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000502 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, DL,
Akira Hatanakab1538f92011-10-03 21:06:13 +0000503 HI, Ty, InGlue);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000504 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
505 }
506
507 return SDValue();
508}
509
Simon Dardisba92b032016-09-09 11:06:01 +0000510static Mips::CondCode condCodeToFCC(ISD::CondCode CC) {
511 switch (CC) {
512 default: llvm_unreachable("Unknown fp condition code!");
513 case ISD::SETEQ:
514 case ISD::SETOEQ: return Mips::FCOND_OEQ;
515 case ISD::SETUNE: return Mips::FCOND_UNE;
516 case ISD::SETLT:
517 case ISD::SETOLT: return Mips::FCOND_OLT;
518 case ISD::SETGT:
519 case ISD::SETOGT: return Mips::FCOND_OGT;
520 case ISD::SETLE:
521 case ISD::SETOLE: return Mips::FCOND_OLE;
522 case ISD::SETGE:
523 case ISD::SETOGE: return Mips::FCOND_OGE;
524 case ISD::SETULT: return Mips::FCOND_ULT;
525 case ISD::SETULE: return Mips::FCOND_ULE;
526 case ISD::SETUGT: return Mips::FCOND_UGT;
527 case ISD::SETUGE: return Mips::FCOND_UGE;
528 case ISD::SETUO: return Mips::FCOND_UN;
529 case ISD::SETO: return Mips::FCOND_OR;
530 case ISD::SETNE:
531 case ISD::SETONE: return Mips::FCOND_ONE;
532 case ISD::SETUEQ: return Mips::FCOND_UEQ;
533 }
534}
535
536
537/// This function returns true if the floating point conditional branches and
538/// conditional moves which use condition code CC should be inverted.
539static bool invertFPCondCodeUser(Mips::CondCode CC) {
540 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
541 return false;
542
543 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
544 "Illegal Condition Code");
545
546 return true;
547}
548
549// Creates and returns an FPCmp node from a setcc node.
550// Returns Op if setcc is not a floating point comparison.
551static SDValue createFPCmp(SelectionDAG &DAG, const SDValue &Op) {
552 // must be a SETCC node
553 if (Op.getOpcode() != ISD::SETCC)
554 return Op;
555
556 SDValue LHS = Op.getOperand(0);
557
558 if (!LHS.getValueType().isFloatingPoint())
559 return Op;
560
561 SDValue RHS = Op.getOperand(1);
562 SDLoc DL(Op);
563
564 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
565 // node if necessary.
566 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
567
568 return DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS,
569 DAG.getConstant(condCodeToFCC(CC), DL, MVT::i32));
570}
571
572// Creates and returns a CMovFPT/F node.
573static SDValue createCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True,
574 SDValue False, const SDLoc &DL) {
575 ConstantSDNode *CC = cast<ConstantSDNode>(Cond.getOperand(2));
576 bool invert = invertFPCondCodeUser((Mips::CondCode)CC->getSExtValue());
577 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
578
579 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
580 True.getValueType(), True, FCC0, False, Cond);
581}
582
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000583static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000584 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000585 const MipsSubtarget &Subtarget) {
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000586 if (DCI.isBeforeLegalizeOps())
587 return SDValue();
588
589 SDValue SetCC = N->getOperand(0);
590
591 if ((SetCC.getOpcode() != ISD::SETCC) ||
592 !SetCC.getOperand(0).getValueType().isInteger())
593 return SDValue();
594
595 SDValue False = N->getOperand(2);
596 EVT FalseTy = False.getValueType();
597
598 if (!FalseTy.isInteger())
599 return SDValue();
600
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000601 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(False);
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000602
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000603 // If the RHS (False) is 0, we swap the order of the operands
604 // of ISD::SELECT (obviously also inverting the condition) so that we can
605 // take advantage of conditional moves using the $0 register.
606 // Example:
607 // return (a != 0) ? x : 0;
608 // load $reg, x
609 // movz $reg, $0, a
610 if (!FalseC)
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000611 return SDValue();
612
Andrew Trickef9de2a2013-05-25 02:42:55 +0000613 const SDLoc DL(N);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000614
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000615 if (!FalseC->getZExtValue()) {
616 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
617 SDValue True = N->getOperand(1);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000618
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000619 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
620 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
621
622 return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True);
623 }
624
Matheus Almeidaa6beac12013-12-05 12:07:05 +0000625 // If both operands are integer constants there's a possibility that we
626 // can do some interesting optimizations.
627 SDValue True = N->getOperand(1);
628 ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(True);
629
630 if (!TrueC || !True.getValueType().isInteger())
631 return SDValue();
632
Vasileios Kalintiris36901dd2016-03-01 20:25:43 +0000633 // We'll also ignore MVT::i64 operands as this optimizations proves
634 // to be ineffective because of the required sign extensions as the result
635 // of a SETCC operator is always MVT::i32 for non-vector types.
636 if (True.getValueType() == MVT::i64)
637 return SDValue();
638
Matheus Almeidaa6beac12013-12-05 12:07:05 +0000639 int64_t Diff = TrueC->getSExtValue() - FalseC->getSExtValue();
640
641 // 1) (a < x) ? y : y-1
642 // slti $reg1, a, x
643 // addiu $reg2, $reg1, y-1
Vasileios Kalintiris36901dd2016-03-01 20:25:43 +0000644 if (Diff == 1)
645 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, False);
Matheus Almeidaa6beac12013-12-05 12:07:05 +0000646
647 // 2) (a < x) ? y-1 : y
648 // slti $reg1, a, x
649 // xor $reg1, $reg1, 1
650 // addiu $reg2, $reg1, y-1
651 if (Diff == -1) {
652 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
653 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
654 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
655 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, True);
656 }
657
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000658 // Couldn't optimize.
659 return SDValue();
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000660}
661
Vasileios Kalintirise741eb22015-03-02 12:47:32 +0000662static SDValue performCMovFPCombine(SDNode *N, SelectionDAG &DAG,
663 TargetLowering::DAGCombinerInfo &DCI,
664 const MipsSubtarget &Subtarget) {
665 if (DCI.isBeforeLegalizeOps())
666 return SDValue();
667
668 SDValue ValueIfTrue = N->getOperand(0), ValueIfFalse = N->getOperand(2);
669
670 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(ValueIfFalse);
671 if (!FalseC || FalseC->getZExtValue())
672 return SDValue();
673
674 // Since RHS (False) is 0, we swap the order of the True/False operands
675 // (obviously also inverting the condition) so that we can
676 // take advantage of conditional moves using the $0 register.
677 // Example:
678 // return (a != 0) ? x : 0;
679 // load $reg, x
680 // movz $reg, $0, a
681 unsigned Opc = (N->getOpcode() == MipsISD::CMovFP_T) ? MipsISD::CMovFP_F :
682 MipsISD::CMovFP_T;
683
684 SDValue FCC = N->getOperand(1), Glue = N->getOperand(3);
Vasileios Kalintiris2ef28882015-03-04 12:10:18 +0000685 return DAG.getNode(Opc, SDLoc(N), ValueIfFalse.getValueType(),
686 ValueIfFalse, FCC, ValueIfTrue, Glue);
Vasileios Kalintirise741eb22015-03-02 12:47:32 +0000687}
688
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000689static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000690 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000691 const MipsSubtarget &Subtarget) {
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000692 // Pattern match EXT.
693 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
694 // => ext $dst, $src, size, pos
Eric Christopher1c29a652014-07-18 22:55:25 +0000695 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasExtractInsert())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000696 return SDValue();
697
698 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000699 unsigned ShiftRightOpc = ShiftRight.getOpcode();
700
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000701 // Op's first operand must be a shift right.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000702 if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL)
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000703 return SDValue();
704
705 // The second operand of the shift must be an immediate.
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000706 ConstantSDNode *CN;
707 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
708 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000709
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000710 uint64_t Pos = CN->getZExtValue();
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000711 uint64_t SMPos, SMSize;
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000712
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000713 // Op's second operand must be a shifted mask.
714 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000715 !isShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000716 return SDValue();
717
718 // Return if the shifted mask does not start at bit 0 or the sum of its size
719 // and Pos exceeds the word's size.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000720 EVT ValTy = N->getValueType(0);
721 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000722 return SDValue();
723
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000724 SDLoc DL(N);
725 return DAG.getNode(MipsISD::Ext, DL, ValTy,
726 ShiftRight.getOperand(0),
727 DAG.getConstant(Pos, DL, MVT::i32),
728 DAG.getConstant(SMSize, DL, MVT::i32));
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000729}
Jia Liuf54f60f2012-02-28 07:46:26 +0000730
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000731static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000732 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000733 const MipsSubtarget &Subtarget) {
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000734 // Pattern match INS.
735 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
Jia Liuf54f60f2012-02-28 07:46:26 +0000736 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000737 // => ins $dst, $src, size, pos, $src1
Eric Christopher1c29a652014-07-18 22:55:25 +0000738 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasExtractInsert())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000739 return SDValue();
740
741 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
742 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
743 ConstantSDNode *CN;
744
745 // See if Op's first operand matches (and $src1 , mask0).
746 if (And0.getOpcode() != ISD::AND)
747 return SDValue();
748
749 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000750 !isShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000751 return SDValue();
752
753 // See if Op's second operand matches (and (shl $src, pos), mask1).
754 if (And1.getOpcode() != ISD::AND)
755 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000756
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000757 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000758 !isShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000759 return SDValue();
760
761 // The shift masks must have the same position and size.
762 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
763 return SDValue();
764
765 SDValue Shl = And1.getOperand(0);
766 if (Shl.getOpcode() != ISD::SHL)
767 return SDValue();
768
769 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
770 return SDValue();
771
772 unsigned Shamt = CN->getZExtValue();
773
774 // Return if the shift amount and the first bit position of mask are not the
Jia Liuf54f60f2012-02-28 07:46:26 +0000775 // same.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000776 EVT ValTy = N->getValueType(0);
777 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000778 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000779
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000780 SDLoc DL(N);
781 return DAG.getNode(MipsISD::Ins, DL, ValTy, Shl.getOperand(0),
782 DAG.getConstant(SMPos0, DL, MVT::i32),
783 DAG.getConstant(SMSize0, DL, MVT::i32),
784 And0.getOperand(0));
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000785}
Jia Liuf54f60f2012-02-28 07:46:26 +0000786
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000787static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000788 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000789 const MipsSubtarget &Subtarget) {
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000790 // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt))
791
792 if (DCI.isBeforeLegalizeOps())
793 return SDValue();
794
795 SDValue Add = N->getOperand(1);
796
797 if (Add.getOpcode() != ISD::ADD)
798 return SDValue();
799
800 SDValue Lo = Add.getOperand(1);
801
802 if ((Lo.getOpcode() != MipsISD::Lo) ||
803 (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable))
804 return SDValue();
805
806 EVT ValTy = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000807 SDLoc DL(N);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000808
809 SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, N->getOperand(0),
810 Add.getOperand(0));
811 return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo);
812}
813
Vasileios Kalintiris3751d412016-04-13 15:07:45 +0000814static SDValue performAssertZextCombine(SDNode *N, SelectionDAG &DAG,
815 TargetLowering::DAGCombinerInfo &DCI,
816 const MipsSubtarget &Subtarget) {
817 SDValue N0 = N->getOperand(0);
818 EVT NarrowerVT = cast<VTSDNode>(N->getOperand(1))->getVT();
819
820 if (N0.getOpcode() != ISD::TRUNCATE)
821 return SDValue();
822
823 if (N0.getOperand(0).getOpcode() != ISD::AssertZext)
824 return SDValue();
825
826 // fold (AssertZext (trunc (AssertZext x))) -> (trunc (AssertZext x))
827 // if the type of the extension of the innermost AssertZext node is
828 // smaller from that of the outermost node, eg:
829 // (AssertZext:i32 (trunc:i32 (AssertZext:i64 X, i32)), i8)
830 // -> (trunc:i32 (AssertZext X, i8))
831 SDValue WiderAssertZext = N0.getOperand(0);
832 EVT WiderVT = cast<VTSDNode>(WiderAssertZext->getOperand(1))->getVT();
833
834 if (NarrowerVT.bitsLT(WiderVT)) {
835 SDValue NewAssertZext = DAG.getNode(
836 ISD::AssertZext, SDLoc(N), WiderAssertZext.getValueType(),
837 WiderAssertZext.getOperand(0), DAG.getValueType(NarrowerVT));
838 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0),
839 NewAssertZext);
840 }
841
842 return SDValue();
843}
844
Bruno Cardoso Lopes61a61e92011-02-10 18:05:10 +0000845SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000846 const {
847 SelectionDAG &DAG = DCI.DAG;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000848 unsigned Opc = N->getOpcode();
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000849
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000850 switch (Opc) {
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000851 default: break;
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000852 case ISD::SDIVREM:
853 case ISD::UDIVREM:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000854 return performDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000855 case ISD::SELECT:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000856 return performSELECTCombine(N, DAG, DCI, Subtarget);
Vasileios Kalintirise741eb22015-03-02 12:47:32 +0000857 case MipsISD::CMovFP_F:
858 case MipsISD::CMovFP_T:
859 return performCMovFPCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000860 case ISD::AND:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000861 return performANDCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000862 case ISD::OR:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000863 return performORCombine(N, DAG, DCI, Subtarget);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000864 case ISD::ADD:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000865 return performADDCombine(N, DAG, DCI, Subtarget);
Vasileios Kalintiris3751d412016-04-13 15:07:45 +0000866 case ISD::AssertZext:
867 return performAssertZextCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000868 }
869
870 return SDValue();
871}
872
Sanjay Patelf7401292015-11-11 17:24:56 +0000873bool MipsTargetLowering::isCheapToSpeculateCttz() const {
874 return Subtarget.hasMips32();
875}
876
877bool MipsTargetLowering::isCheapToSpeculateCtlz() const {
878 return Subtarget.hasMips32();
879}
880
Akira Hatanakafabb8cf2012-09-21 23:58:31 +0000881void
882MipsTargetLowering::LowerOperationWrapper(SDNode *N,
883 SmallVectorImpl<SDValue> &Results,
884 SelectionDAG &DAG) const {
885 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
886
887 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
888 Results.push_back(Res.getValue(I));
889}
890
891void
892MipsTargetLowering::ReplaceNodeResults(SDNode *N,
893 SmallVectorImpl<SDValue> &Results,
894 SelectionDAG &DAG) const {
Akira Hatanaka9da442f2013-04-30 21:17:07 +0000895 return LowerOperationWrapper(N, Results, DAG);
Akira Hatanakafabb8cf2012-09-21 23:58:31 +0000896}
897
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000898SDValue MipsTargetLowering::
Dan Gohman21cea8a2010-04-17 15:26:15 +0000899LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000900{
Wesley Peck527da1b2010-11-23 03:31:01 +0000901 switch (Op.getOpcode())
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000902 {
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000903 case ISD::BR_JT: return lowerBR_JT(Op, DAG);
Simon Dardisba92b032016-09-09 11:06:01 +0000904 case ISD::BRCOND: return lowerBRCOND(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000905 case ISD::ConstantPool: return lowerConstantPool(Op, DAG);
906 case ISD::GlobalAddress: return lowerGlobalAddress(Op, DAG);
907 case ISD::BlockAddress: return lowerBlockAddress(Op, DAG);
908 case ISD::GlobalTLSAddress: return lowerGlobalTLSAddress(Op, DAG);
909 case ISD::JumpTable: return lowerJumpTable(Op, DAG);
Simon Dardisba92b032016-09-09 11:06:01 +0000910 case ISD::SELECT: return lowerSELECT(Op, DAG);
911 case ISD::SETCC: return lowerSETCC(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000912 case ISD::VASTART: return lowerVASTART(Op, DAG);
Daniel Sanders2b553d42014-08-01 09:17:39 +0000913 case ISD::VAARG: return lowerVAARG(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000914 case ISD::FCOPYSIGN: return lowerFCOPYSIGN(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000915 case ISD::FRAMEADDR: return lowerFRAMEADDR(Op, DAG);
916 case ISD::RETURNADDR: return lowerRETURNADDR(Op, DAG);
917 case ISD::EH_RETURN: return lowerEH_RETURN(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000918 case ISD::ATOMIC_FENCE: return lowerATOMIC_FENCE(Op, DAG);
919 case ISD::SHL_PARTS: return lowerShiftLeftParts(Op, DAG);
920 case ISD::SRA_PARTS: return lowerShiftRightParts(Op, DAG, true);
921 case ISD::SRL_PARTS: return lowerShiftRightParts(Op, DAG, false);
922 case ISD::LOAD: return lowerLOAD(Op, DAG);
923 case ISD::STORE: return lowerSTORE(Op, DAG);
Hal Finkel5081ac22016-09-01 10:28:47 +0000924 case ISD::EH_DWARF_CFA: return lowerEH_DWARF_CFA(Op, DAG);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000925 case ISD::FP_TO_SINT: return lowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000926 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000927 return SDValue();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000928}
929
Akira Hatanakae2489122011-04-15 21:51:11 +0000930//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000931// Lower helper functions
Akira Hatanakae2489122011-04-15 21:51:11 +0000932//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000933
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000934// addLiveIn - This helper function adds the specified physical register to the
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000935// MachineFunction as a live in value. It also creates a corresponding
936// virtual register for it.
937static unsigned
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000938addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000939{
Chris Lattnera10fff52007-12-31 04:13:23 +0000940 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
941 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000942 return VReg;
943}
944
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000945static MachineBasicBlock *insertDivByZeroTrap(MachineInstr &MI,
Daniel Sanders308181e2014-06-12 10:44:10 +0000946 MachineBasicBlock &MBB,
947 const TargetInstrInfo &TII,
Zlatko Buljan58d6a952016-04-13 08:02:26 +0000948 bool Is64Bit, bool IsMicroMips) {
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000949 if (NoZeroDivCheck)
950 return &MBB;
951
952 // Insert instruction "teq $divisor_reg, $zero, 7".
953 MachineBasicBlock::iterator I(MI);
954 MachineInstrBuilder MIB;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000955 MachineOperand &Divisor = MI.getOperand(2);
956 MIB = BuildMI(MBB, std::next(I), MI.getDebugLoc(),
Zlatko Buljan58d6a952016-04-13 08:02:26 +0000957 TII.get(IsMicroMips ? Mips::TEQ_MM : Mips::TEQ))
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000958 .addReg(Divisor.getReg(), getKillRegState(Divisor.isKill()))
959 .addReg(Mips::ZERO)
960 .addImm(7);
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000961
962 // Use the 32-bit sub-register if this is a 64-bit division.
963 if (Is64Bit)
964 MIB->getOperand(0).setSubReg(Mips::sub_32);
965
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000966 // Clear Divisor's kill flag.
967 Divisor.setIsKill(false);
Daniel Sanders308181e2014-06-12 10:44:10 +0000968
969 // We would normally delete the original instruction here but in this case
970 // we only needed to inject an additional instruction rather than replace it.
971
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000972 return &MBB;
973}
974
Akira Hatanakae4bd0542012-09-27 02:15:57 +0000975MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000976MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
Dan Gohman25c16532010-05-01 00:01:06 +0000977 MachineBasicBlock *BB) const {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000978 switch (MI.getOpcode()) {
Reed Kotler97ba5f22013-02-21 04:22:38 +0000979 default:
980 llvm_unreachable("Unexpected instr type to insert");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000981 case Mips::ATOMIC_LOAD_ADD_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000982 return emitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000983 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000984 return emitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000985 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000986 return emitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000987 case Mips::ATOMIC_LOAD_ADD_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000988 return emitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000989
990 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000991 return emitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000992 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000993 return emitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000994 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000995 return emitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000996 case Mips::ATOMIC_LOAD_AND_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000997 return emitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000998
999 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001000 return emitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001001 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001002 return emitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001003 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001004 return emitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001005 case Mips::ATOMIC_LOAD_OR_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001006 return emitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001007
1008 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001009 return emitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001010 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001011 return emitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001012 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001013 return emitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001014 case Mips::ATOMIC_LOAD_XOR_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001015 return emitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001016
1017 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001018 return emitAtomicBinaryPartword(MI, BB, 1, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001019 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001020 return emitAtomicBinaryPartword(MI, BB, 2, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001021 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001022 return emitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001023 case Mips::ATOMIC_LOAD_NAND_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001024 return emitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001025
1026 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001027 return emitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001028 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001029 return emitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001030 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001031 return emitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001032 case Mips::ATOMIC_LOAD_SUB_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001033 return emitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001034
1035 case Mips::ATOMIC_SWAP_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001036 return emitAtomicBinaryPartword(MI, BB, 1, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001037 case Mips::ATOMIC_SWAP_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001038 return emitAtomicBinaryPartword(MI, BB, 2, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001039 case Mips::ATOMIC_SWAP_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001040 return emitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001041 case Mips::ATOMIC_SWAP_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001042 return emitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001043
1044 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001045 return emitAtomicCmpSwapPartword(MI, BB, 1);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001046 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001047 return emitAtomicCmpSwapPartword(MI, BB, 2);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001048 case Mips::ATOMIC_CMP_SWAP_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001049 return emitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001050 case Mips::ATOMIC_CMP_SWAP_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001051 return emitAtomicCmpSwap(MI, BB, 8);
Akira Hatanaka1cb02422013-05-20 18:07:43 +00001052 case Mips::PseudoSDIV:
1053 case Mips::PseudoUDIV:
Daniel Sanders308181e2014-06-12 10:44:10 +00001054 case Mips::DIV:
1055 case Mips::DIVU:
1056 case Mips::MOD:
1057 case Mips::MODU:
Zlatko Buljan58d6a952016-04-13 08:02:26 +00001058 return insertDivByZeroTrap(MI, *BB, *Subtarget.getInstrInfo(), false,
1059 false);
1060 case Mips::SDIV_MM_Pseudo:
1061 case Mips::UDIV_MM_Pseudo:
1062 case Mips::SDIV_MM:
1063 case Mips::UDIV_MM:
1064 case Mips::DIV_MMR6:
1065 case Mips::DIVU_MMR6:
1066 case Mips::MOD_MMR6:
1067 case Mips::MODU_MMR6:
1068 return insertDivByZeroTrap(MI, *BB, *Subtarget.getInstrInfo(), false, true);
Akira Hatanaka1cb02422013-05-20 18:07:43 +00001069 case Mips::PseudoDSDIV:
1070 case Mips::PseudoDUDIV:
Daniel Sanders308181e2014-06-12 10:44:10 +00001071 case Mips::DDIV:
1072 case Mips::DDIVU:
1073 case Mips::DMOD:
1074 case Mips::DMODU:
Zlatko Buljan58d6a952016-04-13 08:02:26 +00001075 return insertDivByZeroTrap(MI, *BB, *Subtarget.getInstrInfo(), true, false);
1076 case Mips::DDIV_MM64R6:
1077 case Mips::DDIVU_MM64R6:
1078 case Mips::DMOD_MM64R6:
1079 case Mips::DMODU_MM64R6:
1080 return insertDivByZeroTrap(MI, *BB, *Subtarget.getInstrInfo(), true, true);
Daniel Sanders0fa60412014-06-12 13:39:06 +00001081 case Mips::SEL_D:
Zlatko Buljancd242c12016-06-09 11:15:53 +00001082 case Mips::SEL_D_MMR6:
Daniel Sanders0fa60412014-06-12 13:39:06 +00001083 return emitSEL_D(MI, BB);
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001084
1085 case Mips::PseudoSELECT_I:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001086 case Mips::PseudoSELECT_I64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001087 case Mips::PseudoSELECT_S:
1088 case Mips::PseudoSELECT_D32:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001089 case Mips::PseudoSELECT_D64:
Vasileios Kalintiris36901dd2016-03-01 20:25:43 +00001090 return emitPseudoSELECT(MI, BB, false, Mips::BNE);
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001091 case Mips::PseudoSELECTFP_F_I:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001092 case Mips::PseudoSELECTFP_F_I64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001093 case Mips::PseudoSELECTFP_F_S:
1094 case Mips::PseudoSELECTFP_F_D32:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001095 case Mips::PseudoSELECTFP_F_D64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001096 return emitPseudoSELECT(MI, BB, true, Mips::BC1F);
1097 case Mips::PseudoSELECTFP_T_I:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001098 case Mips::PseudoSELECTFP_T_I64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001099 case Mips::PseudoSELECTFP_T_S:
1100 case Mips::PseudoSELECTFP_T_D32:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001101 case Mips::PseudoSELECTFP_T_D64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001102 return emitPseudoSELECT(MI, BB, true, Mips::BC1T);
Akira Hatanakaa5352702011-03-31 18:26:17 +00001103 }
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001104}
1105
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001106// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
1107// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001108MachineBasicBlock *MipsTargetLowering::emitAtomicBinary(MachineInstr &MI,
1109 MachineBasicBlock *BB,
1110 unsigned Size,
1111 unsigned BinOpcode,
1112 bool Nand) const {
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001113 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001114
1115 MachineFunction *MF = BB->getParent();
1116 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001117 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Eric Christopher96e72c62015-01-29 23:27:36 +00001118 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001119 const bool ArePtrs64bit = ABI.ArePtrs64bit();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001120 DebugLoc DL = MI.getDebugLoc();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001121 unsigned LL, SC, AND, NOR, ZERO, BEQ;
1122
1123 if (Size == 4) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001124 if (isMicroMips) {
1125 LL = Mips::LL_MM;
1126 SC = Mips::SC_MM;
1127 } else {
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001128 LL = Subtarget.hasMips32r6()
1129 ? (ArePtrs64bit ? Mips::LL64_R6 : Mips::LL_R6)
1130 : (ArePtrs64bit ? Mips::LL64 : Mips::LL);
1131 SC = Subtarget.hasMips32r6()
1132 ? (ArePtrs64bit ? Mips::SC64_R6 : Mips::SC_R6)
1133 : (ArePtrs64bit ? Mips::SC64 : Mips::SC);
Daniel Sanders6a803f62014-06-16 13:13:03 +00001134 }
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001135
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001136 AND = Mips::AND;
1137 NOR = Mips::NOR;
1138 ZERO = Mips::ZERO;
1139 BEQ = Mips::BEQ;
Daniel Sanders6a803f62014-06-16 13:13:03 +00001140 } else {
Daniel Sandersbdcfab12014-07-24 09:47:14 +00001141 LL = Subtarget.hasMips64r6() ? Mips::LLD_R6 : Mips::LLD;
1142 SC = Subtarget.hasMips64r6() ? Mips::SCD_R6 : Mips::SCD;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001143 AND = Mips::AND64;
1144 NOR = Mips::NOR64;
1145 ZERO = Mips::ZERO_64;
1146 BEQ = Mips::BEQ64;
1147 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001148
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001149 unsigned OldVal = MI.getOperand(0).getReg();
1150 unsigned Ptr = MI.getOperand(1).getReg();
1151 unsigned Incr = MI.getOperand(2).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001152
Akira Hatanaka0e019592011-07-19 20:11:17 +00001153 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1154 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1155 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001156
1157 // insert new blocks after the current block
1158 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1159 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1160 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +00001161 MachineFunction::iterator It = ++BB->getIterator();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001162 MF->insert(It, loopMBB);
1163 MF->insert(It, exitMBB);
1164
1165 // Transfer the remainder of BB and its successor edges to exitMBB.
1166 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001167 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001168 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1169
1170 // thisMBB:
1171 // ...
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001172 // fallthrough --> loopMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001173 BB->addSuccessor(loopMBB);
Akira Hatanaka08636b42011-07-19 17:09:53 +00001174 loopMBB->addSuccessor(loopMBB);
1175 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001176
1177 // loopMBB:
1178 // ll oldval, 0(ptr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001179 // <binop> storeval, oldval, incr
1180 // sc success, storeval, 0(ptr)
1181 // beq success, $0, loopMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001182 BB = loopMBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001183 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001184 if (Nand) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001185 // and andres, oldval, incr
1186 // nor storeval, $0, andres
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001187 BuildMI(BB, DL, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
1188 BuildMI(BB, DL, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001189 } else if (BinOpcode) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001190 // <binop> storeval, oldval, incr
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001191 BuildMI(BB, DL, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001192 } else {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001193 StoreVal = Incr;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001194 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001195 BuildMI(BB, DL, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
1196 BuildMI(BB, DL, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001197
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001198 MI.eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001199
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001200 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001201}
1202
Daniel Sanders6a803f62014-06-16 13:13:03 +00001203MachineBasicBlock *MipsTargetLowering::emitSignExtendToI32InReg(
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001204 MachineInstr &MI, MachineBasicBlock *BB, unsigned Size, unsigned DstReg,
Daniel Sanders6a803f62014-06-16 13:13:03 +00001205 unsigned SrcReg) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00001206 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001207 const DebugLoc &DL = MI.getDebugLoc();
Daniel Sanders6a803f62014-06-16 13:13:03 +00001208
Eric Christopher1c29a652014-07-18 22:55:25 +00001209 if (Subtarget.hasMips32r2() && Size == 1) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001210 BuildMI(BB, DL, TII->get(Mips::SEB), DstReg).addReg(SrcReg);
1211 return BB;
1212 }
1213
Eric Christopher1c29a652014-07-18 22:55:25 +00001214 if (Subtarget.hasMips32r2() && Size == 2) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001215 BuildMI(BB, DL, TII->get(Mips::SEH), DstReg).addReg(SrcReg);
1216 return BB;
1217 }
1218
1219 MachineFunction *MF = BB->getParent();
1220 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1221 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1222 unsigned ScrReg = RegInfo.createVirtualRegister(RC);
1223
1224 assert(Size < 32);
1225 int64_t ShiftImm = 32 - (Size * 8);
1226
1227 BuildMI(BB, DL, TII->get(Mips::SLL), ScrReg).addReg(SrcReg).addImm(ShiftImm);
1228 BuildMI(BB, DL, TII->get(Mips::SRA), DstReg).addReg(ScrReg).addImm(ShiftImm);
1229
1230 return BB;
1231}
1232
1233MachineBasicBlock *MipsTargetLowering::emitAtomicBinaryPartword(
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001234 MachineInstr &MI, MachineBasicBlock *BB, unsigned Size, unsigned BinOpcode,
Daniel Sanders6a803f62014-06-16 13:13:03 +00001235 bool Nand) const {
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001236 assert((Size == 1 || Size == 2) &&
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001237 "Unsupported size for EmitAtomicBinaryPartial.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001238
1239 MachineFunction *MF = BB->getParent();
1240 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1241 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001242 const bool ArePtrs64bit = ABI.ArePtrs64bit();
Simon Dardisa2d8cc32016-04-28 16:26:43 +00001243 const TargetRegisterClass *RCp =
1244 getRegClassFor(ArePtrs64bit ? MVT::i64 : MVT::i32);
Eric Christopher96e72c62015-01-29 23:27:36 +00001245 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001246 DebugLoc DL = MI.getDebugLoc();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001247
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001248 unsigned Dest = MI.getOperand(0).getReg();
1249 unsigned Ptr = MI.getOperand(1).getReg();
1250 unsigned Incr = MI.getOperand(2).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001251
Simon Dardisa2d8cc32016-04-28 16:26:43 +00001252 unsigned AlignedAddr = RegInfo.createVirtualRegister(RCp);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001253 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001254 unsigned Mask = RegInfo.createVirtualRegister(RC);
1255 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001256 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1257 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001258 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Simon Dardisa2d8cc32016-04-28 16:26:43 +00001259 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RCp);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001260 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1261 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1262 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1263 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka9663dd32011-07-19 20:56:53 +00001264 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001265 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1266 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1267 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001268 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001269
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001270 unsigned LL, SC;
1271 if (isMicroMips) {
1272 LL = Mips::LL_MM;
1273 SC = Mips::SC_MM;
1274 } else {
1275 LL = Subtarget.hasMips32r6() ? (ArePtrs64bit ? Mips::LL64_R6 : Mips::LL_R6)
1276 : (ArePtrs64bit ? Mips::LL64 : Mips::LL);
1277 SC = Subtarget.hasMips32r6() ? (ArePtrs64bit ? Mips::SC64_R6 : Mips::SC_R6)
1278 : (ArePtrs64bit ? Mips::SC64 : Mips::SC);
1279 }
1280
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001281 // insert new blocks after the current block
1282 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1283 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001284 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001285 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +00001286 MachineFunction::iterator It = ++BB->getIterator();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001287 MF->insert(It, loopMBB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001288 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001289 MF->insert(It, exitMBB);
1290
1291 // Transfer the remainder of BB and its successor edges to exitMBB.
1292 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001293 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001294 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1295
Akira Hatanaka08636b42011-07-19 17:09:53 +00001296 BB->addSuccessor(loopMBB);
1297 loopMBB->addSuccessor(loopMBB);
1298 loopMBB->addSuccessor(sinkMBB);
1299 sinkMBB->addSuccessor(exitMBB);
1300
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001301 // thisMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001302 // addiu masklsb2,$0,-4 # 0xfffffffc
1303 // and alignedaddr,ptr,masklsb2
1304 // andi ptrlsb2,ptr,3
1305 // sll shiftamt,ptrlsb2,3
1306 // ori maskupper,$0,255 # 0xff
1307 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001308 // nor mask2,$0,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001309 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001310
1311 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Simon Dardisa2d8cc32016-04-28 16:26:43 +00001312 BuildMI(BB, DL, TII->get(ABI.GetPtrAddiuOp()), MaskLSB2)
1313 .addReg(ABI.GetNullPtr()).addImm(-4);
1314 BuildMI(BB, DL, TII->get(ABI.GetPtrAndOp()), AlignedAddr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001315 .addReg(Ptr).addReg(MaskLSB2);
Simon Dardisa2d8cc32016-04-28 16:26:43 +00001316 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2)
1317 .addReg(Ptr, 0, ArePtrs64bit ? Mips::sub_32 : 0).addImm(3);
Eric Christopher1c29a652014-07-18 22:55:25 +00001318 if (Subtarget.isLittle()) {
Akira Hatanaka2bf97332013-05-31 03:25:44 +00001319 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1320 } else {
1321 unsigned Off = RegInfo.createVirtualRegister(RC);
1322 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1323 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1324 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1325 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001326 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001327 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001328 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001329 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001330 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001331 BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(Incr).addReg(ShiftAmt);
Bruno Cardoso Lopesf771a0f2011-05-31 20:25:26 +00001332
Akira Hatanaka27292632011-07-18 18:52:12 +00001333 // atomic.load.binop
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001334 // loopMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001335 // ll oldval,0(alignedaddr)
1336 // binop binopres,oldval,incr2
1337 // and newval,binopres,mask
1338 // and maskedoldval0,oldval,mask2
1339 // or storeval,maskedoldval0,newval
1340 // sc success,storeval,0(alignedaddr)
1341 // beq success,$0,loopMBB
1342
Akira Hatanaka27292632011-07-18 18:52:12 +00001343 // atomic.swap
1344 // loopMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001345 // ll oldval,0(alignedaddr)
Akira Hatanakae4503582011-07-19 18:14:26 +00001346 // and newval,incr2,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001347 // and maskedoldval0,oldval,mask2
1348 // or storeval,maskedoldval0,newval
1349 // sc success,storeval,0(alignedaddr)
1350 // beq success,$0,loopMBB
Akira Hatanaka27292632011-07-18 18:52:12 +00001351
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001352 BB = loopMBB;
Jozef Kolek2f27d572014-12-18 16:39:29 +00001353 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001354 if (Nand) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001355 // and andres, oldval, incr2
1356 // nor binopres, $0, andres
1357 // and newval, binopres, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001358 BuildMI(BB, DL, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1359 BuildMI(BB, DL, TII->get(Mips::NOR), BinOpRes)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001360 .addReg(Mips::ZERO).addReg(AndRes);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001361 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001362 } else if (BinOpcode) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001363 // <binop> binopres, oldval, incr2
1364 // and newval, binopres, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001365 BuildMI(BB, DL, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1366 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001367 } else { // atomic.swap
Akira Hatanaka0e019592011-07-19 20:11:17 +00001368 // and newval, incr2, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001369 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanakae4503582011-07-19 18:14:26 +00001370 }
Jia Liuf54f60f2012-02-28 07:46:26 +00001371
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001372 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001373 .addReg(OldVal).addReg(Mask2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001374 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka9663dd32011-07-19 20:56:53 +00001375 .addReg(MaskedOldVal0).addReg(NewVal);
Jozef Kolek2f27d572014-12-18 16:39:29 +00001376 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001377 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001378 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001379 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001380
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001381 // sinkMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001382 // and maskedoldval1,oldval,mask
1383 // srl srlres,maskedoldval1,shiftamt
Daniel Sanders6a803f62014-06-16 13:13:03 +00001384 // sign_extend dest,srlres
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001385 BB = sinkMBB;
Akira Hatanakae97bd812011-07-19 03:14:58 +00001386
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001387 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001388 .addReg(OldVal).addReg(Mask);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001389 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001390 .addReg(MaskedOldVal1).addReg(ShiftAmt);
Daniel Sanders6a803f62014-06-16 13:13:03 +00001391 BB = emitSignExtendToI32InReg(MI, BB, Size, Dest, SrlRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001392
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001393 MI.eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001394
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001395 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001396}
1397
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001398MachineBasicBlock *MipsTargetLowering::emitAtomicCmpSwap(MachineInstr &MI,
1399 MachineBasicBlock *BB,
1400 unsigned Size) const {
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001401 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001402
1403 MachineFunction *MF = BB->getParent();
1404 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001405 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Eric Christopher96e72c62015-01-29 23:27:36 +00001406 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001407 const bool ArePtrs64bit = ABI.ArePtrs64bit();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001408 DebugLoc DL = MI.getDebugLoc();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001409 unsigned LL, SC, ZERO, BNE, BEQ;
1410
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001411 if (Size == 4) {
1412 if (isMicroMips) {
1413 LL = Mips::LL_MM;
1414 SC = Mips::SC_MM;
1415 } else {
1416 LL = Subtarget.hasMips32r6()
1417 ? (ArePtrs64bit ? Mips::LL64_R6 : Mips::LL_R6)
1418 : (ArePtrs64bit ? Mips::LL64 : Mips::LL);
1419 SC = Subtarget.hasMips32r6()
1420 ? (ArePtrs64bit ? Mips::SC64_R6 : Mips::SC_R6)
1421 : (ArePtrs64bit ? Mips::SC64 : Mips::SC);
1422 }
1423
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001424 ZERO = Mips::ZERO;
1425 BNE = Mips::BNE;
1426 BEQ = Mips::BEQ;
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001427 } else {
Zoran Jovanovic796ed6d2015-10-29 14:40:19 +00001428 LL = Subtarget.hasMips64r6() ? Mips::LLD_R6 : Mips::LLD;
1429 SC = Subtarget.hasMips64r6() ? Mips::SCD_R6 : Mips::SCD;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001430 ZERO = Mips::ZERO_64;
1431 BNE = Mips::BNE64;
1432 BEQ = Mips::BEQ64;
1433 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001434
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001435 unsigned Dest = MI.getOperand(0).getReg();
1436 unsigned Ptr = MI.getOperand(1).getReg();
1437 unsigned OldVal = MI.getOperand(2).getReg();
1438 unsigned NewVal = MI.getOperand(3).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001439
Akira Hatanaka0e019592011-07-19 20:11:17 +00001440 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001441
1442 // insert new blocks after the current block
1443 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1444 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1445 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1446 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +00001447 MachineFunction::iterator It = ++BB->getIterator();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001448 MF->insert(It, loop1MBB);
1449 MF->insert(It, loop2MBB);
1450 MF->insert(It, exitMBB);
1451
1452 // Transfer the remainder of BB and its successor edges to exitMBB.
1453 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001454 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001455 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1456
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001457 // thisMBB:
1458 // ...
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001459 // fallthrough --> loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001460 BB->addSuccessor(loop1MBB);
Akira Hatanaka08636b42011-07-19 17:09:53 +00001461 loop1MBB->addSuccessor(exitMBB);
1462 loop1MBB->addSuccessor(loop2MBB);
1463 loop2MBB->addSuccessor(loop1MBB);
1464 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001465
1466 // loop1MBB:
1467 // ll dest, 0(ptr)
1468 // bne dest, oldval, exitMBB
1469 BB = loop1MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001470 BuildMI(BB, DL, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1471 BuildMI(BB, DL, TII->get(BNE))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001472 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001473
1474 // loop2MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001475 // sc success, newval, 0(ptr)
1476 // beq success, $0, loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001477 BB = loop2MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001478 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001479 .addReg(NewVal).addReg(Ptr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001480 BuildMI(BB, DL, TII->get(BEQ))
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001481 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001482
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001483 MI.eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001484
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001485 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001486}
1487
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001488MachineBasicBlock *MipsTargetLowering::emitAtomicCmpSwapPartword(
1489 MachineInstr &MI, MachineBasicBlock *BB, unsigned Size) const {
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001490 assert((Size == 1 || Size == 2) &&
1491 "Unsupported size for EmitAtomicCmpSwapPartial.");
1492
1493 MachineFunction *MF = BB->getParent();
1494 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1495 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001496 const bool ArePtrs64bit = ABI.ArePtrs64bit();
Zoran Jovanovic2f6845b2016-04-13 16:02:25 +00001497 const TargetRegisterClass *RCp =
1498 getRegClassFor(ArePtrs64bit ? MVT::i64 : MVT::i32);
Eric Christopher96e72c62015-01-29 23:27:36 +00001499 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001500 DebugLoc DL = MI.getDebugLoc();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001501
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001502 unsigned Dest = MI.getOperand(0).getReg();
1503 unsigned Ptr = MI.getOperand(1).getReg();
1504 unsigned CmpVal = MI.getOperand(2).getReg();
1505 unsigned NewVal = MI.getOperand(3).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001506
Zoran Jovanovic2f6845b2016-04-13 16:02:25 +00001507 unsigned AlignedAddr = RegInfo.createVirtualRegister(RCp);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001508 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001509 unsigned Mask = RegInfo.createVirtualRegister(RC);
1510 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001511 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1512 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1513 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1514 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
Zoran Jovanovic2f6845b2016-04-13 16:02:25 +00001515 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RCp);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001516 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1517 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1518 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1519 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1520 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1521 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1522 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001523 unsigned Success = RegInfo.createVirtualRegister(RC);
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001524 unsigned LL, SC;
1525
1526 if (isMicroMips) {
1527 LL = Mips::LL_MM;
1528 SC = Mips::SC_MM;
1529 } else {
1530 LL = Subtarget.hasMips32r6() ? (ArePtrs64bit ? Mips::LL64_R6 : Mips::LL_R6)
1531 : (ArePtrs64bit ? Mips::LL64 : Mips::LL);
1532 SC = Subtarget.hasMips32r6() ? (ArePtrs64bit ? Mips::SC64_R6 : Mips::SC_R6)
1533 : (ArePtrs64bit ? Mips::SC64 : Mips::SC);
1534 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001535
1536 // insert new blocks after the current block
1537 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1538 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1539 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001540 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001541 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +00001542 MachineFunction::iterator It = ++BB->getIterator();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001543 MF->insert(It, loop1MBB);
1544 MF->insert(It, loop2MBB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001545 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001546 MF->insert(It, exitMBB);
1547
1548 // Transfer the remainder of BB and its successor edges to exitMBB.
1549 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001550 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001551 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1552
Akira Hatanaka08636b42011-07-19 17:09:53 +00001553 BB->addSuccessor(loop1MBB);
1554 loop1MBB->addSuccessor(sinkMBB);
1555 loop1MBB->addSuccessor(loop2MBB);
1556 loop2MBB->addSuccessor(loop1MBB);
1557 loop2MBB->addSuccessor(sinkMBB);
1558 sinkMBB->addSuccessor(exitMBB);
1559
Akira Hatanakae4503582011-07-19 18:14:26 +00001560 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001561 // thisMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001562 // addiu masklsb2,$0,-4 # 0xfffffffc
1563 // and alignedaddr,ptr,masklsb2
1564 // andi ptrlsb2,ptr,3
Zoran Jovanovic2f6845b2016-04-13 16:02:25 +00001565 // xori ptrlsb2,ptrlsb2,3 # Only for BE
Akira Hatanaka0e019592011-07-19 20:11:17 +00001566 // sll shiftamt,ptrlsb2,3
1567 // ori maskupper,$0,255 # 0xff
1568 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001569 // nor mask2,$0,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001570 // andi maskedcmpval,cmpval,255
1571 // sll shiftedcmpval,maskedcmpval,shiftamt
1572 // andi maskednewval,newval,255
1573 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001574 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Zoran Jovanovic2f6845b2016-04-13 16:02:25 +00001575 BuildMI(BB, DL, TII->get(ArePtrs64bit ? Mips::DADDiu : Mips::ADDiu), MaskLSB2)
1576 .addReg(ABI.GetNullPtr()).addImm(-4);
1577 BuildMI(BB, DL, TII->get(ArePtrs64bit ? Mips::AND64 : Mips::AND), AlignedAddr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001578 .addReg(Ptr).addReg(MaskLSB2);
Zoran Jovanovic2f6845b2016-04-13 16:02:25 +00001579 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2)
1580 .addReg(Ptr, 0, ArePtrs64bit ? Mips::sub_32 : 0).addImm(3);
Eric Christopher1c29a652014-07-18 22:55:25 +00001581 if (Subtarget.isLittle()) {
Akira Hatanaka2bf97332013-05-31 03:25:44 +00001582 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1583 } else {
1584 unsigned Off = RegInfo.createVirtualRegister(RC);
1585 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1586 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1587 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1588 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001589 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001590 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001591 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001592 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001593 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1594 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001595 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001596 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001597 .addReg(MaskedCmpVal).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001598 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001599 .addReg(NewVal).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001600 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001601 .addReg(MaskedNewVal).addReg(ShiftAmt);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001602
1603 // loop1MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001604 // ll oldval,0(alginedaddr)
1605 // and maskedoldval0,oldval,mask
1606 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001607 BB = loop1MBB;
Jozef Kolek2f27d572014-12-18 16:39:29 +00001608 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001609 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001610 .addReg(OldVal).addReg(Mask);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001611 BuildMI(BB, DL, TII->get(Mips::BNE))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001612 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001613
1614 // loop2MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001615 // and maskedoldval1,oldval,mask2
1616 // or storeval,maskedoldval1,shiftednewval
1617 // sc success,storeval,0(alignedaddr)
1618 // beq success,$0,loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001619 BB = loop2MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001620 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001621 .addReg(OldVal).addReg(Mask2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001622 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001623 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
Jozef Kolek2f27d572014-12-18 16:39:29 +00001624 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001625 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001626 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001627 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001628
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001629 // sinkMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001630 // srl srlres,maskedoldval0,shiftamt
Daniel Sanders6a803f62014-06-16 13:13:03 +00001631 // sign_extend dest,srlres
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001632 BB = sinkMBB;
Akira Hatanakae97bd812011-07-19 03:14:58 +00001633
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001634 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001635 .addReg(MaskedOldVal0).addReg(ShiftAmt);
Daniel Sanders6a803f62014-06-16 13:13:03 +00001636 BB = emitSignExtendToI32InReg(MI, BB, Size, Dest, SrlRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001637
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001638 MI.eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001639
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001640 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001641}
1642
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001643MachineBasicBlock *MipsTargetLowering::emitSEL_D(MachineInstr &MI,
Daniel Sanders0fa60412014-06-12 13:39:06 +00001644 MachineBasicBlock *BB) const {
1645 MachineFunction *MF = BB->getParent();
Eric Christopher96e72c62015-01-29 23:27:36 +00001646 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
1647 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders0fa60412014-06-12 13:39:06 +00001648 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001649 DebugLoc DL = MI.getDebugLoc();
Daniel Sanders0fa60412014-06-12 13:39:06 +00001650 MachineBasicBlock::iterator II(MI);
1651
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001652 unsigned Fc = MI.getOperand(1).getReg();
Daniel Sanders0fa60412014-06-12 13:39:06 +00001653 const auto &FGR64RegClass = TRI->getRegClass(Mips::FGR64RegClassID);
1654
1655 unsigned Fc2 = RegInfo.createVirtualRegister(FGR64RegClass);
1656
1657 BuildMI(*BB, II, DL, TII->get(Mips::SUBREG_TO_REG), Fc2)
1658 .addImm(0)
1659 .addReg(Fc)
1660 .addImm(Mips::sub_lo);
1661
1662 // We don't erase the original instruction, we just replace the condition
1663 // register with the 64-bit super-register.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001664 MI.getOperand(1).setReg(Fc2);
Daniel Sanders0fa60412014-06-12 13:39:06 +00001665
1666 return BB;
1667}
1668
Akira Hatanakae2489122011-04-15 21:51:11 +00001669//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001670// Misc Lower Operation implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00001671//===----------------------------------------------------------------------===//
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001672SDValue MipsTargetLowering::lowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001673 SDValue Chain = Op.getOperand(0);
1674 SDValue Table = Op.getOperand(1);
1675 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001676 SDLoc DL(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00001677 auto &TD = DAG.getDataLayout();
1678 EVT PTy = getPointerTy(TD);
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001679 unsigned EntrySize =
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001680 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(TD);
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001681
1682 Index = DAG.getNode(ISD::MUL, DL, PTy, Index,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001683 DAG.getConstant(EntrySize, DL, PTy));
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001684 SDValue Addr = DAG.getNode(ISD::ADD, DL, PTy, Index, Table);
1685
1686 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
Justin Lebar9c375812016-07-15 18:27:10 +00001687 Addr = DAG.getExtLoad(
1688 ISD::SEXTLOAD, DL, PTy, Chain, Addr,
1689 MachinePointerInfo::getJumpTable(DAG.getMachineFunction()), MemVT);
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001690 Chain = Addr.getValue(1);
1691
Rafael Espindola9f1c1fe2016-06-27 12:48:21 +00001692 if (isPositionIndependent() || ABI.IsN64()) {
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001693 // For PIC, the sequence is:
1694 // BRIND(load(Jumptable + index) + RelocBase)
1695 // RelocBase can be JumpTable, GOT or some sort of global base.
1696 Addr = DAG.getNode(ISD::ADD, DL, PTy, Addr,
1697 getPICJumpTableRelocBase(Table, DAG));
1698 }
1699
1700 return DAG.getNode(ISD::BRIND, DL, MVT::Other, Chain, Addr);
1701}
1702
Simon Dardisba92b032016-09-09 11:06:01 +00001703SDValue MipsTargetLowering::lowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
1704 // The first operand is the chain, the second is the condition, the third is
1705 // the block to branch to if the condition is true.
1706 SDValue Chain = Op.getOperand(0);
1707 SDValue Dest = Op.getOperand(2);
1708 SDLoc DL(Op);
1709
1710 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
1711 SDValue CondRes = createFPCmp(DAG, Op.getOperand(1));
1712
1713 // Return if flag is not set by a floating point comparison.
1714 if (CondRes.getOpcode() != MipsISD::FPCmp)
1715 return Op;
1716
1717 SDValue CCNode = CondRes.getOperand(2);
1718 Mips::CondCode CC =
1719 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
1720 unsigned Opc = invertFPCondCodeUser(CC) ? Mips::BRANCH_F : Mips::BRANCH_T;
1721 SDValue BrCode = DAG.getConstant(Opc, DL, MVT::i32);
1722 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
1723 return DAG.getNode(MipsISD::FPBrcond, DL, Op.getValueType(), Chain, BrCode,
1724 FCC0, Dest, CondRes);
1725}
1726
1727SDValue MipsTargetLowering::
1728lowerSELECT(SDValue Op, SelectionDAG &DAG) const
1729{
1730 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
1731 SDValue Cond = createFPCmp(DAG, Op.getOperand(0));
1732
1733 // Return if flag is not set by a floating point comparison.
1734 if (Cond.getOpcode() != MipsISD::FPCmp)
1735 return Op;
1736
1737 return createCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
1738 SDLoc(Op));
1739}
1740
1741SDValue MipsTargetLowering::lowerSETCC(SDValue Op, SelectionDAG &DAG) const {
1742 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
1743 SDValue Cond = createFPCmp(DAG, Op);
1744
1745 assert(Cond.getOpcode() == MipsISD::FPCmp &&
1746 "Floating point operand expected.");
1747
1748 SDLoc DL(Op);
1749 SDValue True = DAG.getConstant(1, DL, MVT::i32);
1750 SDValue False = DAG.getConstant(0, DL, MVT::i32);
1751
1752 return createCMovFP(DAG, Cond, True, False, DL);
1753}
1754
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001755SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001756 SelectionDAG &DAG) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001757 EVT Ty = Op.getValueType();
1758 GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
1759 const GlobalValue *GV = N->getGlobal();
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001760
Rafael Espindola9f1c1fe2016-06-27 12:48:21 +00001761 if (!isPositionIndependent() && !ABI.IsN64()) {
Eric Christopher36fe0282015-02-03 07:22:52 +00001762 const MipsTargetObjectFile *TLOF =
1763 static_cast<const MipsTargetObjectFile *>(
1764 getTargetMachine().getObjFileLowering());
1765 if (TLOF->IsGlobalInSmallSection(GV, getTargetMachine()))
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001766 // %gp_rel relocation
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001767 return getAddrGPRel(N, SDLoc(N), Ty, DAG);
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001768
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001769 // %hi/%lo relocation
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001770 return getAddrNonPIC(N, SDLoc(N), Ty, DAG);
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001771 }
1772
Rafael Espindolab2b6a852016-06-27 12:33:33 +00001773 // Every other architecture would use shouldAssumeDSOLocal in here, but
1774 // mips is special.
Rafael Espindola97ca8272016-06-27 23:21:07 +00001775 // * In PIC code mips requires got loads even for local statics!
Rafael Espindolab2b6a852016-06-27 12:33:33 +00001776 // * To save on got entries, for local statics the got entry contains the
1777 // page and an additional add instruction takes care of the low bits.
1778 // * It is legal to access a hidden symbol with a non hidden undefined,
1779 // so one cannot guarantee that all access to a hidden symbol will know
1780 // it is hidden.
1781 // * Mips linkers don't support creating a page and a full got entry for
1782 // the same symbol.
1783 // * Given all that, we have to use a full got entry for hidden symbols :-(
Rafael Espindola1ac1fa82016-06-27 03:19:40 +00001784 if (GV->hasLocalLinkage())
Eric Christopher96e72c62015-01-29 23:27:36 +00001785 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001786
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00001787 if (LargeGOT)
Alex Lorenze40c8a22015-08-11 23:09:45 +00001788 return getAddrGlobalLargeGOT(
1789 N, SDLoc(N), Ty, DAG, MipsII::MO_GOT_HI16, MipsII::MO_GOT_LO16,
1790 DAG.getEntryNode(),
1791 MachinePointerInfo::getGOT(DAG.getMachineFunction()));
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00001792
Alex Lorenze40c8a22015-08-11 23:09:45 +00001793 return getAddrGlobal(
1794 N, SDLoc(N), Ty, DAG,
Daniel Sandersfe98b2f2016-05-03 13:35:44 +00001795 (ABI.IsN32() || ABI.IsN64()) ? MipsII::MO_GOT_DISP : MipsII::MO_GOT,
Alex Lorenze40c8a22015-08-11 23:09:45 +00001796 DAG.getEntryNode(), MachinePointerInfo::getGOT(DAG.getMachineFunction()));
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001797}
1798
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001799SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op,
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +00001800 SelectionDAG &DAG) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001801 BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op);
1802 EVT Ty = Op.getValueType();
Akira Hatanaka30f97cf2013-09-25 00:30:25 +00001803
Rafael Espindola9f1c1fe2016-06-27 12:48:21 +00001804 if (!isPositionIndependent() && !ABI.IsN64())
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001805 return getAddrNonPIC(N, SDLoc(N), Ty, DAG);
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001806
Eric Christopher96e72c62015-01-29 23:27:36 +00001807 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +00001808}
1809
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001810SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001811lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001812{
Akira Hatanakabff84e12011-12-14 18:26:41 +00001813 // If the relocation model is PIC, use the General Dynamic TLS Model or
1814 // Local Dynamic TLS model, otherwise use the Initial Exec or
1815 // Local Exec TLS Model.
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001816
1817 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +00001818 if (DAG.getTarget().Options.EmulatedTLS)
1819 return LowerToTLSEmulatedModel(GA, DAG);
1820
Andrew Trickef9de2a2013-05-25 02:42:55 +00001821 SDLoc DL(GA);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001822 const GlobalValue *GV = GA->getGlobal();
Mehdi Amini44ede332015-07-09 02:09:04 +00001823 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001824
Hans Wennborgaea41202012-05-04 09:40:39 +00001825 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
1826
1827 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Hans Wennborg245917b2012-06-04 14:02:08 +00001828 // General Dynamic and Local Dynamic TLS Model.
1829 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
1830 : MipsII::MO_TLSGD;
1831
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001832 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag);
1833 SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT,
1834 getGlobalReg(DAG, PtrVT), TGA);
Akira Hatanakaf10ee842011-12-08 21:05:38 +00001835 unsigned PtrSize = PtrVT.getSizeInBits();
1836 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1837
Benjamin Kramer64ba50a2011-12-11 12:21:34 +00001838 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001839
1840 ArgListTy Args;
1841 ArgListEntry Entry;
1842 Entry.Node = Argument;
Akira Hatanakadee6c822011-12-08 20:34:32 +00001843 Entry.Ty = PtrTy;
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001844 Args.push_back(Entry);
Jia Liuf54f60f2012-02-28 07:46:26 +00001845
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00001846 TargetLowering::CallLoweringInfo CLI(DAG);
1847 CLI.setDebugLoc(DL).setChain(DAG.getEntryNode())
Krzysztof Parzyszeke116d5002016-06-22 12:54:25 +00001848 .setCallee(CallingConv::C, PtrTy, TlsGetAddr, std::move(Args));
Justin Holewinskiaa583972012-05-25 16:35:28 +00001849 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001850
Akira Hatanakabff84e12011-12-14 18:26:41 +00001851 SDValue Ret = CallResult.first;
1852
Hans Wennborgaea41202012-05-04 09:40:39 +00001853 if (model != TLSModel::LocalDynamic)
Akira Hatanakabff84e12011-12-14 18:26:41 +00001854 return Ret;
1855
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001856 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanakabff84e12011-12-14 18:26:41 +00001857 MipsII::MO_DTPREL_HI);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001858 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1859 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanakabff84e12011-12-14 18:26:41 +00001860 MipsII::MO_DTPREL_LO);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001861 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1862 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret);
1863 return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001864 }
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001865
1866 SDValue Offset;
Hans Wennborgaea41202012-05-04 09:40:39 +00001867 if (model == TLSModel::InitialExec) {
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001868 // Initial Exec TLS Model
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001869 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001870 MipsII::MO_GOTTPREL);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001871 TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT),
Akira Hatanakab049aef2012-02-24 22:34:47 +00001872 TGA);
Justin Lebar9c375812016-07-15 18:27:10 +00001873 Offset =
1874 DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), TGA, MachinePointerInfo());
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001875 } else {
1876 // Local Exec TLS Model
Hans Wennborgaea41202012-05-04 09:40:39 +00001877 assert(model == TLSModel::LocalExec);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001878 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001879 MipsII::MO_TPREL_HI);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001880 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001881 MipsII::MO_TPREL_LO);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001882 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1883 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1884 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001885 }
1886
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001887 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
1888 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001889}
1890
1891SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001892lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +00001893{
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001894 JumpTableSDNode *N = cast<JumpTableSDNode>(Op);
1895 EVT Ty = Op.getValueType();
Akira Hatanaka30f97cf2013-09-25 00:30:25 +00001896
Rafael Espindola9f1c1fe2016-06-27 12:48:21 +00001897 if (!isPositionIndependent() && !ABI.IsN64())
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001898 return getAddrNonPIC(N, SDLoc(N), Ty, DAG);
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001899
Eric Christopher96e72c62015-01-29 23:27:36 +00001900 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +00001901}
1902
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001903SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001904lowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +00001905{
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001906 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
1907 EVT Ty = Op.getValueType();
Bruno Cardoso Lopes2db07582009-11-25 12:17:58 +00001908
Rafael Espindola9f1c1fe2016-06-27 12:48:21 +00001909 if (!isPositionIndependent() && !ABI.IsN64()) {
Eric Christopher36fe0282015-02-03 07:22:52 +00001910 const MipsTargetObjectFile *TLOF =
1911 static_cast<const MipsTargetObjectFile *>(
1912 getTargetMachine().getObjFileLowering());
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001913
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001914 if (TLOF->IsConstantInSmallSection(DAG.getDataLayout(), N->getConstVal(),
1915 getTargetMachine()))
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001916 // %gp_rel relocation
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001917 return getAddrGPRel(N, SDLoc(N), Ty, DAG);
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001918
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001919 return getAddrNonPIC(N, SDLoc(N), Ty, DAG);
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001920 }
Bruno Cardoso Lopesfdb4cec2008-07-23 16:01:50 +00001921
Eric Christopher96e72c62015-01-29 23:27:36 +00001922 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +00001923}
1924
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001925SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman31ae5862010-04-17 14:41:14 +00001926 MachineFunction &MF = DAG.getMachineFunction();
1927 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1928
Andrew Trickef9de2a2013-05-25 02:42:55 +00001929 SDLoc DL(Op);
Dan Gohman31ae5862010-04-17 14:41:14 +00001930 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
Mehdi Amini44ede332015-07-09 02:09:04 +00001931 getPointerTy(MF.getDataLayout()));
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +00001932
1933 // vastart just stores the address of the VarArgsFrameIndex slot into the
1934 // memory location argument.
1935 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001936 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
Justin Lebar9c375812016-07-15 18:27:10 +00001937 MachinePointerInfo(SV));
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +00001938}
Jia Liuf54f60f2012-02-28 07:46:26 +00001939
Daniel Sanders2b553d42014-08-01 09:17:39 +00001940SDValue MipsTargetLowering::lowerVAARG(SDValue Op, SelectionDAG &DAG) const {
1941 SDNode *Node = Op.getNode();
1942 EVT VT = Node->getValueType(0);
1943 SDValue Chain = Node->getOperand(0);
1944 SDValue VAListPtr = Node->getOperand(1);
1945 unsigned Align = Node->getConstantOperandVal(3);
1946 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
1947 SDLoc DL(Node);
Eric Christopher96e72c62015-01-29 23:27:36 +00001948 unsigned ArgSlotSizeInBytes = (ABI.IsN32() || ABI.IsN64()) ? 8 : 4;
Daniel Sanders2b553d42014-08-01 09:17:39 +00001949
Justin Lebar9c375812016-07-15 18:27:10 +00001950 SDValue VAListLoad = DAG.getLoad(getPointerTy(DAG.getDataLayout()), DL, Chain,
1951 VAListPtr, MachinePointerInfo(SV));
Daniel Sanders2b553d42014-08-01 09:17:39 +00001952 SDValue VAList = VAListLoad;
1953
1954 // Re-align the pointer if necessary.
1955 // It should only ever be necessary for 64-bit types on O32 since the minimum
1956 // argument alignment is the same as the maximum type alignment for N32/N64.
1957 //
1958 // FIXME: We currently align too often. The code generator doesn't notice
1959 // when the pointer is still aligned from the last va_arg (or pair of
1960 // va_args for the i64 on O32 case).
1961 if (Align > getMinStackArgumentAlignment()) {
1962 assert(((Align & (Align-1)) == 0) && "Expected Align to be a power of 2");
1963
1964 VAList = DAG.getNode(ISD::ADD, DL, VAList.getValueType(), VAList,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001965 DAG.getConstant(Align - 1, DL, VAList.getValueType()));
Daniel Sanders2b553d42014-08-01 09:17:39 +00001966
1967 VAList = DAG.getNode(ISD::AND, DL, VAList.getValueType(), VAList,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001968 DAG.getConstant(-(int64_t)Align, DL,
Daniel Sanders2b553d42014-08-01 09:17:39 +00001969 VAList.getValueType()));
1970 }
1971
1972 // Increment the pointer, VAList, to the next vaarg.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001973 auto &TD = DAG.getDataLayout();
1974 unsigned ArgSizeInBytes =
1975 TD.getTypeAllocSize(VT.getTypeForEVT(*DAG.getContext()));
Rui Ueyamada00f2f2016-01-14 21:06:47 +00001976 SDValue Tmp3 =
1977 DAG.getNode(ISD::ADD, DL, VAList.getValueType(), VAList,
1978 DAG.getConstant(alignTo(ArgSizeInBytes, ArgSlotSizeInBytes),
1979 DL, VAList.getValueType()));
Daniel Sanders2b553d42014-08-01 09:17:39 +00001980 // Store the incremented VAList to the legalized pointer
1981 Chain = DAG.getStore(VAListLoad.getValue(1), DL, Tmp3, VAListPtr,
Justin Lebar9c375812016-07-15 18:27:10 +00001982 MachinePointerInfo(SV));
Daniel Sanders2b553d42014-08-01 09:17:39 +00001983
1984 // In big-endian mode we must adjust the pointer when the load size is smaller
1985 // than the argument slot size. We must also reduce the known alignment to
1986 // match. For example in the N64 ABI, we must add 4 bytes to the offset to get
1987 // the correct half of the slot, and reduce the alignment from 8 (slot
1988 // alignment) down to 4 (type alignment).
1989 if (!Subtarget.isLittle() && ArgSizeInBytes < ArgSlotSizeInBytes) {
1990 unsigned Adjustment = ArgSlotSizeInBytes - ArgSizeInBytes;
1991 VAList = DAG.getNode(ISD::ADD, DL, VAListPtr.getValueType(), VAList,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001992 DAG.getIntPtrConstant(Adjustment, DL));
Daniel Sanders2b553d42014-08-01 09:17:39 +00001993 }
1994 // Load the actual argument out of the pointer VAList
Justin Lebar9c375812016-07-15 18:27:10 +00001995 return DAG.getLoad(VT, DL, Chain, VAList, MachinePointerInfo());
Daniel Sanders2b553d42014-08-01 09:17:39 +00001996}
1997
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001998static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG,
1999 bool HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002000 EVT TyX = Op.getOperand(0).getValueType();
2001 EVT TyY = Op.getOperand(1).getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002002 SDLoc DL(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002003 SDValue Const1 = DAG.getConstant(1, DL, MVT::i32);
2004 SDValue Const31 = DAG.getConstant(31, DL, MVT::i32);
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002005 SDValue Res;
2006
2007 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
2008 // to i32.
2009 SDValue X = (TyX == MVT::f32) ?
2010 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
2011 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
2012 Const1);
2013 SDValue Y = (TyY == MVT::f32) ?
2014 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
2015 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
2016 Const1);
2017
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00002018 if (HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002019 // ext E, Y, 31, 1 ; extract bit31 of Y
2020 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
2021 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
2022 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
2023 } else {
2024 // sll SllX, X, 1
2025 // srl SrlX, SllX, 1
2026 // srl SrlY, Y, 31
2027 // sll SllY, SrlX, 31
2028 // or Or, SrlX, SllY
2029 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
2030 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
2031 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
2032 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
2033 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
2034 }
2035
2036 if (TyX == MVT::f32)
2037 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
2038
2039 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002040 Op.getOperand(0),
2041 DAG.getConstant(0, DL, MVT::i32));
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002042 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002043}
2044
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00002045static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG,
2046 bool HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002047 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
2048 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
2049 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002050 SDLoc DL(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002051 SDValue Const1 = DAG.getConstant(1, DL, MVT::i32);
Eric Christopher0713a9d2011-06-08 23:55:35 +00002052
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002053 // Bitcast to integer nodes.
2054 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
2055 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002056
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00002057 if (HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002058 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
2059 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
2060 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002061 DAG.getConstant(WidthY - 1, DL, MVT::i32), Const1);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002062
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002063 if (WidthX > WidthY)
2064 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
2065 else if (WidthY > WidthX)
2066 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002067
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002068 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002069 DAG.getConstant(WidthX - 1, DL, MVT::i32), Const1,
2070 X);
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002071 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
2072 }
2073
2074 // (d)sll SllX, X, 1
2075 // (d)srl SrlX, SllX, 1
2076 // (d)srl SrlY, Y, width(Y)-1
2077 // (d)sll SllY, SrlX, width(Y)-1
2078 // or Or, SrlX, SllY
2079 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
2080 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
2081 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002082 DAG.getConstant(WidthY - 1, DL, MVT::i32));
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002083
2084 if (WidthX > WidthY)
2085 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
2086 else if (WidthY > WidthX)
2087 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
2088
2089 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002090 DAG.getConstant(WidthX - 1, DL, MVT::i32));
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002091 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
2092 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002093}
2094
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00002095SDValue
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002096MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00002097 if (Subtarget.isGP64bit())
2098 return lowerFCOPYSIGN64(Op, DAG, Subtarget.hasExtractInsert());
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002099
Eric Christopher1c29a652014-07-18 22:55:25 +00002100 return lowerFCOPYSIGN32(Op, DAG, Subtarget.hasExtractInsert());
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002101}
2102
Akira Hatanaka66277522011-06-02 00:24:44 +00002103SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002104lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes5444a7b2011-06-16 00:40:02 +00002105 // check the depth
2106 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka15506782011-06-07 18:58:42 +00002107 "Frame address can only be determined for current frame.");
Akira Hatanaka66277522011-06-02 00:24:44 +00002108
Matthias Braun941a7052016-07-28 18:40:00 +00002109 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
2110 MFI.setFrameAddressIsTaken(true);
Akira Hatanaka66277522011-06-02 00:24:44 +00002111 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002112 SDLoc DL(Op);
Eric Christopher96e72c62015-01-29 23:27:36 +00002113 SDValue FrameAddr = DAG.getCopyFromReg(
2114 DAG.getEntryNode(), DL, ABI.IsN64() ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka66277522011-06-02 00:24:44 +00002115 return FrameAddr;
2116}
2117
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002118SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op,
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002119 SelectionDAG &DAG) const {
Bill Wendling908bf812014-01-06 00:43:20 +00002120 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00002121 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00002122
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002123 // check the depth
2124 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
2125 "Return address can be determined only for current frame.");
2126
2127 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00002128 MachineFrameInfo &MFI = MF.getFrameInfo();
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00002129 MVT VT = Op.getSimpleValueType();
Eric Christopher96e72c62015-01-29 23:27:36 +00002130 unsigned RA = ABI.IsN64() ? Mips::RA_64 : Mips::RA;
Matthias Braun941a7052016-07-28 18:40:00 +00002131 MFI.setReturnAddressIsTaken(true);
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002132
2133 // Return RA, which contains the return address. Mark it an implicit live-in.
2134 unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00002135 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT);
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002136}
2137
Akira Hatanakac0b02062013-01-30 00:26:49 +00002138// An EH_RETURN is the result of lowering llvm.eh.return which in turn is
2139// generated from __builtin_eh_return (offset, handler)
2140// The effect of this is to adjust the stack pointer by "offset"
2141// and then branch to "handler".
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002142SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Akira Hatanakac0b02062013-01-30 00:26:49 +00002143 const {
2144 MachineFunction &MF = DAG.getMachineFunction();
2145 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2146
2147 MipsFI->setCallsEhReturn();
2148 SDValue Chain = Op.getOperand(0);
2149 SDValue Offset = Op.getOperand(1);
2150 SDValue Handler = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002151 SDLoc DL(Op);
Eric Christopher96e72c62015-01-29 23:27:36 +00002152 EVT Ty = ABI.IsN64() ? MVT::i64 : MVT::i32;
Akira Hatanakac0b02062013-01-30 00:26:49 +00002153
2154 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
2155 // EH_RETURN nodes, so that instructions are emitted back-to-back.
Eric Christopher96e72c62015-01-29 23:27:36 +00002156 unsigned OffsetReg = ABI.IsN64() ? Mips::V1_64 : Mips::V1;
2157 unsigned AddrReg = ABI.IsN64() ? Mips::V0_64 : Mips::V0;
Akira Hatanakac0b02062013-01-30 00:26:49 +00002158 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
2159 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
2160 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
2161 DAG.getRegister(OffsetReg, Ty),
Mehdi Amini44ede332015-07-09 02:09:04 +00002162 DAG.getRegister(AddrReg, getPointerTy(MF.getDataLayout())),
Akira Hatanakac0b02062013-01-30 00:26:49 +00002163 Chain.getValue(1));
2164}
2165
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002166SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
Akira Hatanaka5fd22482012-06-14 21:10:56 +00002167 SelectionDAG &DAG) const {
Eli Friedman26a48482011-07-27 22:21:52 +00002168 // FIXME: Need pseudo-fence for 'singlethread' fences
2169 // FIXME: Set SType for weaker fences where supported/appropriate.
2170 unsigned SType = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002171 SDLoc DL(Op);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002172 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002173 DAG.getConstant(SType, DL, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00002174}
2175
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002176SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
Akira Hatanaka5fd22482012-06-14 21:10:56 +00002177 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002178 SDLoc DL(Op);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002179 MVT VT = Subtarget.isGP64bit() ? MVT::i64 : MVT::i32;
2180
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002181 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2182 SDValue Shamt = Op.getOperand(2);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002183 // if shamt < (VT.bits):
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002184 // lo = (shl lo, shamt)
2185 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
2186 // else:
2187 // lo = 0
2188 // hi = (shl lo, shamt[4:0])
2189 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002190 DAG.getConstant(-1, DL, MVT::i32));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002191 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, VT, Lo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002192 DAG.getConstant(1, DL, VT));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002193 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, VT, ShiftRight1Lo, Not);
2194 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, VT, Hi, Shamt);
2195 SDValue Or = DAG.getNode(ISD::OR, DL, VT, ShiftLeftHi, ShiftRightLo);
2196 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, VT, Lo, Shamt);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002197 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
Daniel Sanders301f9372015-04-29 12:28:58 +00002198 DAG.getConstant(VT.getSizeInBits(), DL, MVT::i32));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002199 Lo = DAG.getNode(ISD::SELECT, DL, VT, Cond,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002200 DAG.getConstant(0, DL, VT), ShiftLeftLo);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002201 Hi = DAG.getNode(ISD::SELECT, DL, VT, Cond, ShiftLeftLo, Or);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002202
2203 SDValue Ops[2] = {Lo, Hi};
Craig Topper64941d92014-04-27 19:20:57 +00002204 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002205}
2206
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002207SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002208 bool IsSRA) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002209 SDLoc DL(Op);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002210 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2211 SDValue Shamt = Op.getOperand(2);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002212 MVT VT = Subtarget.isGP64bit() ? MVT::i64 : MVT::i32;
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002213
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002214 // if shamt < (VT.bits):
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002215 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
2216 // if isSRA:
2217 // hi = (sra hi, shamt)
2218 // else:
2219 // hi = (srl hi, shamt)
2220 // else:
2221 // if isSRA:
2222 // lo = (sra hi, shamt[4:0])
2223 // hi = (sra hi, 31)
2224 // else:
2225 // lo = (srl hi, shamt[4:0])
2226 // hi = 0
2227 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002228 DAG.getConstant(-1, DL, MVT::i32));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002229 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, VT, Hi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002230 DAG.getConstant(1, DL, VT));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002231 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, VT, ShiftLeft1Hi, Not);
2232 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, VT, Lo, Shamt);
2233 SDValue Or = DAG.getNode(ISD::OR, DL, VT, ShiftLeftHi, ShiftRightLo);
2234 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL,
2235 DL, VT, Hi, Shamt);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002236 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
Daniel Sanders301f9372015-04-29 12:28:58 +00002237 DAG.getConstant(VT.getSizeInBits(), DL, MVT::i32));
2238 SDValue Ext = DAG.getNode(ISD::SRA, DL, VT, Hi,
2239 DAG.getConstant(VT.getSizeInBits() - 1, DL, VT));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002240 Lo = DAG.getNode(ISD::SELECT, DL, VT, Cond, ShiftRightHi, Or);
2241 Hi = DAG.getNode(ISD::SELECT, DL, VT, Cond,
Daniel Sanders301f9372015-04-29 12:28:58 +00002242 IsSRA ? Ext : DAG.getConstant(0, DL, VT), ShiftRightHi);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002243
2244 SDValue Ops[2] = {Lo, Hi};
Craig Topper64941d92014-04-27 19:20:57 +00002245 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002246}
2247
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002248static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002249 SDValue Chain, SDValue Src, unsigned Offset) {
Akira Hatanaka95866182012-06-13 19:06:08 +00002250 SDValue Ptr = LD->getBasePtr();
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002251 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
Akira Hatanaka95866182012-06-13 19:06:08 +00002252 EVT BasePtrVT = Ptr.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002253 SDLoc DL(LD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002254 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
2255
2256 if (Offset)
Akira Hatanaka95866182012-06-13 19:06:08 +00002257 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002258 DAG.getConstant(Offset, DL, BasePtrVT));
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002259
2260 SDValue Ops[] = { Chain, Ptr, Src };
Craig Topper206fcd42014-04-26 19:29:41 +00002261 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002262 LD->getMemOperand());
2263}
2264
2265// Expand an unaligned 32 or 64-bit integer load node.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002266SDValue MipsTargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002267 LoadSDNode *LD = cast<LoadSDNode>(Op);
2268 EVT MemVT = LD->getMemoryVT();
2269
Eric Christopher1c29a652014-07-18 22:55:25 +00002270 if (Subtarget.systemSupportsUnalignedAccess())
Daniel Sandersac272632014-05-23 13:18:02 +00002271 return Op;
2272
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002273 // Return if load is aligned or if MemVT is neither i32 nor i64.
2274 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
2275 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
2276 return SDValue();
2277
Eric Christopher1c29a652014-07-18 22:55:25 +00002278 bool IsLittle = Subtarget.isLittle();
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002279 EVT VT = Op.getValueType();
2280 ISD::LoadExtType ExtType = LD->getExtensionType();
2281 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
2282
2283 assert((VT == MVT::i32) || (VT == MVT::i64));
2284
2285 // Expand
2286 // (set dst, (i64 (load baseptr)))
2287 // to
2288 // (set tmp, (ldl (add baseptr, 7), undef))
2289 // (set dst, (ldr baseptr, tmp))
2290 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002291 SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002292 IsLittle ? 7 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002293 return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002294 IsLittle ? 0 : 7);
2295 }
2296
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002297 SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002298 IsLittle ? 3 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002299 SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002300 IsLittle ? 0 : 3);
2301
2302 // Expand
2303 // (set dst, (i32 (load baseptr))) or
2304 // (set dst, (i64 (sextload baseptr))) or
2305 // (set dst, (i64 (extload baseptr)))
2306 // to
2307 // (set tmp, (lwl (add baseptr, 3), undef))
2308 // (set dst, (lwr baseptr, tmp))
2309 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
2310 (ExtType == ISD::EXTLOAD))
2311 return LWR;
2312
2313 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
2314
2315 // Expand
2316 // (set dst, (i64 (zextload baseptr)))
2317 // to
2318 // (set tmp0, (lwl (add baseptr, 3), undef))
2319 // (set tmp1, (lwr baseptr, tmp0))
2320 // (set tmp2, (shl tmp1, 32))
2321 // (set dst, (srl tmp2, 32))
Andrew Trickef9de2a2013-05-25 02:42:55 +00002322 SDLoc DL(LD);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002323 SDValue Const32 = DAG.getConstant(32, DL, MVT::i32);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002324 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
Akira Hatanaka67346852012-06-04 17:46:29 +00002325 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
2326 SDValue Ops[] = { SRL, LWR.getValue(1) };
Craig Topper64941d92014-04-27 19:20:57 +00002327 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002328}
2329
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002330static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002331 SDValue Chain, unsigned Offset) {
Akira Hatanaka95866182012-06-13 19:06:08 +00002332 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
2333 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002334 SDLoc DL(SD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002335 SDVTList VTList = DAG.getVTList(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, Value, Ptr };
Craig Topper206fcd42014-04-26 19:29:41 +00002342 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002343 SD->getMemOperand());
2344}
2345
2346// Expand an unaligned 32 or 64-bit integer store node.
Akira Hatanakad82ee942013-05-16 20:45:17 +00002347static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG,
2348 bool IsLittle) {
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002349 SDValue Value = SD->getValue(), Chain = SD->getChain();
2350 EVT VT = Value.getValueType();
2351
2352 // Expand
2353 // (store val, baseptr) or
2354 // (truncstore val, baseptr)
2355 // to
2356 // (swl val, (add baseptr, 3))
2357 // (swr val, baseptr)
2358 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002359 SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002360 IsLittle ? 3 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002361 return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002362 }
2363
2364 assert(VT == MVT::i64);
2365
2366 // Expand
2367 // (store val, baseptr)
2368 // to
2369 // (sdl val, (add baseptr, 7))
2370 // (sdr val, baseptr)
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002371 SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2372 return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002373}
2374
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002375// Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr).
2376static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG) {
2377 SDValue Val = SD->getValue();
2378
2379 if (Val.getOpcode() != ISD::FP_TO_SINT)
2380 return SDValue();
2381
2382 EVT FPTy = EVT::getFloatingPointVT(Val.getValueSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002383 SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy,
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002384 Val.getOperand(0));
Andrew Trickef9de2a2013-05-25 02:42:55 +00002385 return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(),
Justin Lebar9c375812016-07-15 18:27:10 +00002386 SD->getPointerInfo(), SD->getAlignment(),
2387 SD->getMemOperand()->getFlags());
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002388}
2389
Akira Hatanakad82ee942013-05-16 20:45:17 +00002390SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2391 StoreSDNode *SD = cast<StoreSDNode>(Op);
2392 EVT MemVT = SD->getMemoryVT();
2393
2394 // Lower unaligned integer stores.
Eric Christopher1c29a652014-07-18 22:55:25 +00002395 if (!Subtarget.systemSupportsUnalignedAccess() &&
Daniel Sandersac272632014-05-23 13:18:02 +00002396 (SD->getAlignment() < MemVT.getSizeInBits() / 8) &&
Akira Hatanakad82ee942013-05-16 20:45:17 +00002397 ((MemVT == MVT::i32) || (MemVT == MVT::i64)))
Eric Christopher1c29a652014-07-18 22:55:25 +00002398 return lowerUnalignedIntStore(SD, DAG, Subtarget.isLittle());
Akira Hatanakad82ee942013-05-16 20:45:17 +00002399
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002400 return lowerFP_TO_SINT_STORE(SD, DAG);
Akira Hatanakad82ee942013-05-16 20:45:17 +00002401}
2402
Hal Finkel5081ac22016-09-01 10:28:47 +00002403SDValue MipsTargetLowering::lowerEH_DWARF_CFA(SDValue Op,
2404 SelectionDAG &DAG) const {
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002405
Hal Finkel5081ac22016-09-01 10:28:47 +00002406 // Return a fixed StackObject with offset 0 which points to the old stack
2407 // pointer.
Matthias Braun941a7052016-07-28 18:40:00 +00002408 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002409 EVT ValTy = Op->getValueType(0);
Matthias Braun941a7052016-07-28 18:40:00 +00002410 int FI = MFI.CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
Hal Finkel5081ac22016-09-01 10:28:47 +00002411 return DAG.getFrameIndex(FI, ValTy);
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002412}
2413
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002414SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op,
2415 SelectionDAG &DAG) const {
2416 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002417 SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy,
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002418 Op.getOperand(0));
Andrew Trickef9de2a2013-05-25 02:42:55 +00002419 return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc);
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002420}
2421
Akira Hatanakae2489122011-04-15 21:51:11 +00002422//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002423// Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002424//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002425
Akira Hatanakae2489122011-04-15 21:51:11 +00002426//===----------------------------------------------------------------------===//
Wesley Peck527da1b2010-11-23 03:31:01 +00002427// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002428// Mips O32 ABI rules:
2429// ---
2430// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peck527da1b2010-11-23 03:31:01 +00002431// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002432// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peck527da1b2010-11-23 03:31:01 +00002433// f64 - Only passed in two aliased f32 registers if no int reg has been used
2434// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Sylvestre Ledru469de192014-08-11 18:04:46 +00002435// not used, it must be shadowed. If only A3 is available, shadow it and
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002436// go to stack.
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002437//
2438// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanakae2489122011-04-15 21:51:11 +00002439//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002440
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00002441static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2442 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002443 CCState &State, ArrayRef<MCPhysReg> F64Regs) {
Eric Christopher96e72c62015-01-29 23:27:36 +00002444 const MipsSubtarget &Subtarget = static_cast<const MipsSubtarget &>(
2445 State.getMachineFunction().getSubtarget());
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002446
Craig Topper840beec2014-04-04 05:16:06 +00002447 static const MCPhysReg IntRegs[] = { Mips::A0, Mips::A1, Mips::A2, Mips::A3 };
2448 static const MCPhysReg F32Regs[] = { Mips::F12, Mips::F14 };
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002449
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002450 // Do not process byval args here.
2451 if (ArgFlags.isByVal())
2452 return true;
Akira Hatanaka5e16c6a2011-05-24 19:18:33 +00002453
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002454 // Promote i8 and i16
Daniel Sandersd134c9d2014-12-02 20:40:27 +00002455 if (ArgFlags.isInReg() && !Subtarget.isLittle()) {
2456 if (LocVT == MVT::i8 || LocVT == MVT::i16 || LocVT == MVT::i32) {
2457 LocVT = MVT::i32;
2458 if (ArgFlags.isSExt())
2459 LocInfo = CCValAssign::SExtUpper;
2460 else if (ArgFlags.isZExt())
2461 LocInfo = CCValAssign::ZExtUpper;
2462 else
2463 LocInfo = CCValAssign::AExtUpper;
2464 }
2465 }
2466
2467 // Promote i8 and i16
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002468 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
2469 LocVT = MVT::i32;
2470 if (ArgFlags.isSExt())
2471 LocInfo = CCValAssign::SExt;
2472 else if (ArgFlags.isZExt())
2473 LocInfo = CCValAssign::ZExt;
2474 else
2475 LocInfo = CCValAssign::AExt;
2476 }
2477
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002478 unsigned Reg;
2479
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002480 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
2481 // is true: function is vararg, argument is 3rd or higher, there is previous
2482 // argument which is not f32 or f64.
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002483 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1 ||
2484 State.getFirstUnallocated(F32Regs) != ValNo;
Akira Hatanaka9e6a8cc2011-05-19 20:29:48 +00002485 unsigned OrigAlign = ArgFlags.getOrigAlign();
2486 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002487
2488 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002489 Reg = State.AllocateReg(IntRegs);
Akira Hatanaka9e6a8cc2011-05-19 20:29:48 +00002490 // If this is the first part of an i64 arg,
2491 // the allocated register must be either A0 or A2.
2492 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002493 Reg = State.AllocateReg(IntRegs);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002494 LocVT = MVT::i32;
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002495 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
2496 // Allocate int register and shadow next int register. If first
2497 // available register is Mips::A1 or Mips::A3, shadow it too.
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002498 Reg = State.AllocateReg(IntRegs);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002499 if (Reg == Mips::A1 || Reg == Mips::A3)
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002500 Reg = State.AllocateReg(IntRegs);
2501 State.AllocateReg(IntRegs);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002502 LocVT = MVT::i32;
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002503 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
2504 // we are guaranteed to find an available float register
2505 if (ValVT == MVT::f32) {
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002506 Reg = State.AllocateReg(F32Regs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002507 // Shadow int register
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002508 State.AllocateReg(IntRegs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002509 } else {
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002510 Reg = State.AllocateReg(F64Regs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002511 // Shadow int registers
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002512 unsigned Reg2 = State.AllocateReg(IntRegs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002513 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002514 State.AllocateReg(IntRegs);
2515 State.AllocateReg(IntRegs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002516 }
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002517 } else
2518 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002519
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002520 if (!Reg) {
2521 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3,
2522 OrigAlign);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002523 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002524 } else
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002525 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002526
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002527 return false;
Akira Hatanaka202f6402011-11-12 02:20:46 +00002528}
2529
Akira Hatanakabfb66242013-08-20 23:38:40 +00002530static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT,
2531 MVT LocVT, CCValAssign::LocInfo LocInfo,
2532 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002533 static const MCPhysReg F64Regs[] = { Mips::D6, Mips::D7 };
Akira Hatanakabfb66242013-08-20 23:38:40 +00002534
2535 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2536}
2537
2538static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT,
2539 MVT LocVT, CCValAssign::LocInfo LocInfo,
2540 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002541 static const MCPhysReg F64Regs[] = { Mips::D12_64, Mips::D14_64 };
Akira Hatanakabfb66242013-08-20 23:38:40 +00002542
2543 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2544}
2545
Reid Klecknerd3781742014-11-14 00:39:33 +00002546static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2547 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
2548 CCState &State) LLVM_ATTRIBUTE_UNUSED;
Reed Kotlerd5c41962014-11-13 23:37:45 +00002549
Akira Hatanaka202f6402011-11-12 02:20:46 +00002550#include "MipsGenCallingConv.inc"
2551
Akira Hatanakae2489122011-04-15 21:51:11 +00002552//===----------------------------------------------------------------------===//
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002553// Call Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002554//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002555
Akira Hatanaka61bbcce2011-09-23 00:58:33 +00002556// Return next O32 integer argument register.
2557static unsigned getNextIntArgReg(unsigned Reg) {
2558 assert((Reg == Mips::A0) || (Reg == Mips::A2));
2559 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
2560}
2561
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002562SDValue MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
2563 SDValue Chain, SDValue Arg,
2564 const SDLoc &DL, bool IsTailCall,
2565 SelectionDAG &DAG) const {
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002566 if (!IsTailCall) {
Mehdi Amini44ede332015-07-09 02:09:04 +00002567 SDValue PtrOff =
2568 DAG.getNode(ISD::ADD, DL, getPointerTy(DAG.getDataLayout()), StackPtr,
2569 DAG.getIntPtrConstant(Offset, DL));
Justin Lebar9c375812016-07-15 18:27:10 +00002570 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo());
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002571 }
2572
Matthias Braun941a7052016-07-28 18:40:00 +00002573 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
2574 int FI = MFI.CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00002575 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002576 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(),
Justin Lebar9c375812016-07-15 18:27:10 +00002577 /* Alignment = */ 0, MachineMemOperand::MOVolatile);
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002578}
2579
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002580void MipsTargetLowering::
2581getOpndList(SmallVectorImpl<SDValue> &Ops,
2582 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
2583 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
Sasa Stankovic7072a792014-10-01 08:22:21 +00002584 bool IsCallReloc, CallLoweringInfo &CLI, SDValue Callee,
2585 SDValue Chain) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002586 // Insert node "GP copy globalreg" before call to function.
2587 //
2588 // R_MIPS_CALL* operators (emitted when non-internal functions are called
2589 // in PIC mode) allow symbols to be resolved via lazy binding.
2590 // The lazy binding stub requires GP to point to the GOT.
Sasa Stankovic7072a792014-10-01 08:22:21 +00002591 // Note that we don't need GP to point to the GOT for indirect calls
2592 // (when R_MIPS_CALL* is not used for the call) because Mips linker generates
2593 // lazy binding stub for a function only when R_MIPS_CALL* are the only relocs
2594 // used for the function (that is, Mips linker doesn't generate lazy binding
2595 // stub for a function whose address is taken in the program).
2596 if (IsPICCall && !InternalLinkage && IsCallReloc) {
Eric Christopher96e72c62015-01-29 23:27:36 +00002597 unsigned GPReg = ABI.IsN64() ? Mips::GP_64 : Mips::GP;
2598 EVT Ty = ABI.IsN64() ? MVT::i64 : MVT::i32;
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002599 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty)));
2600 }
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002601
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002602 // Build a sequence of copy-to-reg nodes chained together with token
2603 // chain and flag operands which copy the outgoing args into registers.
2604 // The InFlag in necessary since all emitted instructions must be
2605 // stuck together.
2606 SDValue InFlag;
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002607
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002608 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2609 Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, RegsToPass[i].first,
2610 RegsToPass[i].second, InFlag);
2611 InFlag = Chain.getValue(1);
2612 }
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002613
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002614 // Add argument registers to the end of the list so that they are
2615 // known live into the call.
2616 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2617 Ops.push_back(CLI.DAG.getRegister(RegsToPass[i].first,
2618 RegsToPass[i].second.getValueType()));
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002619
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002620 // Add a register mask operand representing the call-preserved registers.
Eric Christopher96e72c62015-01-29 23:27:36 +00002621 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00002622 const uint32_t *Mask =
2623 TRI->getCallPreservedMask(CLI.DAG.getMachineFunction(), CLI.CallConv);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002624 assert(Mask && "Missing call preserved mask for calling convention");
Eric Christopher1c29a652014-07-18 22:55:25 +00002625 if (Subtarget.inMips16HardFloat()) {
Reed Kotler783c7942013-05-10 22:25:39 +00002626 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
2627 llvm::StringRef Sym = G->getGlobal()->getName();
2628 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
Reed Kotler3230e722013-12-12 02:41:11 +00002629 if (F && F->hasFnAttribute("__Mips16RetHelper")) {
Reed Kotler783c7942013-05-10 22:25:39 +00002630 Mask = MipsRegisterInfo::getMips16RetHelperMask();
2631 }
2632 }
2633 }
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002634 Ops.push_back(CLI.DAG.getRegisterMask(Mask));
2635
2636 if (InFlag.getNode())
2637 Ops.push_back(InFlag);
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002638}
2639
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002640/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman624801e2009-01-26 03:15:54 +00002641/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002642SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00002643MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002644 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00002645 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002646 SDLoc DL = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00002647 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
2648 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
2649 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Akira Hatanakabeda2242012-07-31 18:46:41 +00002650 SDValue Chain = CLI.Chain;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002651 SDValue Callee = CLI.Callee;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002652 bool &IsTailCall = CLI.IsTailCall;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002653 CallingConv::ID CallConv = CLI.CallConv;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002654 bool IsVarArg = CLI.IsVarArg;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002655
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002656 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00002657 MachineFrameInfo &MFI = MF.getFrameInfo();
Eric Christopher96e72c62015-01-29 23:27:36 +00002658 const TargetFrameLowering *TFL = Subtarget.getFrameLowering();
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002659 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
Rafael Espindola9f1c1fe2016-06-27 12:48:21 +00002660 bool IsPIC = isPositionIndependent();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002661
2662 // Analyze operands of the call, assigning locations to each operand.
2663 SmallVector<CCValAssign, 16> ArgLocs;
Daniel Sanders41a64c42014-11-07 11:10:48 +00002664 MipsCCState CCInfo(
2665 CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs, *DAG.getContext(),
2666 MipsCCState::getSpecialCallingConvForCallee(Callee.getNode(), Subtarget));
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002667
2668 // Allocate the reserved argument area. It seems strange to do this from the
2669 // caller side but removing it breaks the frame size calculation.
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002670 CCInfo.AllocateStack(ABI.GetCalleeAllocdArgSizeInBytes(CallConv), 1);
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002671
Daniel Sanderscfad1e32014-11-07 11:43:49 +00002672 CCInfo.AnalyzeCallOperands(Outs, CC_Mips, CLI.getArgs(), Callee.getNode());
Wesley Peck527da1b2010-11-23 03:31:01 +00002673
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002674 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka195a1e22011-06-08 17:39:33 +00002675 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka97ba7692012-07-26 23:27:01 +00002676
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002677 // Check if it's really possible to do a tail call.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002678 if (IsTailCall)
Daniel Sanders23e98772014-11-02 16:09:29 +00002679 IsTailCall = isEligibleForTailCallOptimization(
2680 CCInfo, NextStackOffset, *MF.getInfo<MipsFunctionInfo>());
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002681
Reid Kleckner5772b772014-04-24 20:14:34 +00002682 if (!IsTailCall && CLI.CS && CLI.CS->isMustTailCall())
2683 report_fatal_error("failed to perform tail call elimination on a call "
2684 "site marked musttail");
2685
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002686 if (IsTailCall)
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002687 ++NumTailCalls;
2688
Akira Hatanaka79738332011-09-19 20:26:02 +00002689 // Chain is the output chain of the last Load/Store or CopyToReg node.
2690 // ByValChain is the output chain of the last Memcpy node created for copying
2691 // byval arguments to the stack.
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002692 unsigned StackAlignment = TFL->getStackAlignment();
Rui Ueyamada00f2f2016-01-14 21:06:47 +00002693 NextStackOffset = alignTo(NextStackOffset, StackAlignment);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002694 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, DL, true);
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002695
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002696 if (!IsTailCall)
Andrew Trickad6d08a2013-05-29 22:03:55 +00002697 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffsetVal, DL);
Akira Hatanakabeda2242012-07-31 18:46:41 +00002698
Mehdi Amini44ede332015-07-09 02:09:04 +00002699 SDValue StackPtr =
2700 DAG.getCopyFromReg(Chain, DL, ABI.IsN64() ? Mips::SP_64 : Mips::SP,
2701 getPointerTy(DAG.getDataLayout()));
Akira Hatanaka195a1e22011-06-08 17:39:33 +00002702
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002703 std::deque< std::pair<unsigned, SDValue> > RegsToPass;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002704 SmallVector<SDValue, 8> MemOpChains;
Daniel Sanders23e98772014-11-02 16:09:29 +00002705
2706 CCInfo.rewindByValRegsInfo();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002707
2708 // Walk the register/memloc assignments, inserting copies/loads.
2709 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002710 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002711 CCValAssign &VA = ArgLocs[i];
Akira Hatanakab20a3252011-10-28 19:49:00 +00002712 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka19891f82011-11-12 02:34:50 +00002713 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002714 bool UseUpperBits = false;
Akira Hatanaka19891f82011-11-12 02:34:50 +00002715
2716 // ByVal Arg.
2717 if (Flags.isByVal()) {
Daniel Sanders23e98772014-11-02 16:09:29 +00002718 unsigned FirstByValReg, LastByValReg;
2719 unsigned ByValIdx = CCInfo.getInRegsParamsProcessed();
2720 CCInfo.getInRegsParamInfo(ByValIdx, FirstByValReg, LastByValReg);
2721
Akira Hatanaka19891f82011-11-12 02:34:50 +00002722 assert(Flags.getByValSize() &&
2723 "ByVal args of size 0 should have been ignored by front-end.");
Daniel Sanders23e98772014-11-02 16:09:29 +00002724 assert(ByValIdx < CCInfo.getInRegsParamsCount());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002725 assert(!IsTailCall &&
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002726 "Do not tail-call optimize if there is a byval argument.");
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002727 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002728 FirstByValReg, LastByValReg, Flags, Subtarget.isLittle(),
2729 VA);
Daniel Sanders23e98772014-11-02 16:09:29 +00002730 CCInfo.nextInRegsParam();
Akira Hatanaka19891f82011-11-12 02:34:50 +00002731 continue;
2732 }
Jia Liuf54f60f2012-02-28 07:46:26 +00002733
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002734 // Promote the value if needed.
2735 switch (VA.getLocInfo()) {
Daniel Sandersc43cda82014-11-07 16:54:21 +00002736 default:
2737 llvm_unreachable("Unknown loc info!");
Wesley Peck527da1b2010-11-23 03:31:01 +00002738 case CCValAssign::Full:
Akira Hatanakab20a3252011-10-28 19:49:00 +00002739 if (VA.isRegLoc()) {
2740 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
Akira Hatanaka3b7391d2013-03-05 22:20:28 +00002741 (ValVT == MVT::f64 && LocVT == MVT::i64) ||
2742 (ValVT == MVT::i64 && LocVT == MVT::f64))
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002743 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
Akira Hatanakab20a3252011-10-28 19:49:00 +00002744 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002745 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002746 Arg, DAG.getConstant(0, DL, MVT::i32));
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002747 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002748 Arg, DAG.getConstant(1, DL, MVT::i32));
Eric Christopher1c29a652014-07-18 22:55:25 +00002749 if (!Subtarget.isLittle())
Akira Hatanaka27916972011-04-15 19:52:08 +00002750 std::swap(Lo, Hi);
Jia Liuf54f60f2012-02-28 07:46:26 +00002751 unsigned LocRegLo = VA.getLocReg();
Akira Hatanaka61bbcce2011-09-23 00:58:33 +00002752 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2753 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2754 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002755 continue;
Wesley Peck527da1b2010-11-23 03:31:01 +00002756 }
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002757 }
2758 break;
Daniel Sanders23e98772014-11-02 16:09:29 +00002759 case CCValAssign::BCvt:
2760 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
2761 break;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002762 case CCValAssign::SExtUpper:
2763 UseUpperBits = true;
Justin Bognerb03fd122016-08-17 05:10:15 +00002764 LLVM_FALLTHROUGH;
Chris Lattner52f16de2008-03-17 06:57:02 +00002765 case CCValAssign::SExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002766 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002767 break;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002768 case CCValAssign::ZExtUpper:
2769 UseUpperBits = true;
Justin Bognerb03fd122016-08-17 05:10:15 +00002770 LLVM_FALLTHROUGH;
Chris Lattner52f16de2008-03-17 06:57:02 +00002771 case CCValAssign::ZExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002772 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002773 break;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002774 case CCValAssign::AExtUpper:
2775 UseUpperBits = true;
Justin Bognerb03fd122016-08-17 05:10:15 +00002776 LLVM_FALLTHROUGH;
Chris Lattner52f16de2008-03-17 06:57:02 +00002777 case CCValAssign::AExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002778 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002779 break;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002780 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002781
Daniel Sandersc43cda82014-11-07 16:54:21 +00002782 if (UseUpperBits) {
2783 unsigned ValSizeInBits = Outs[i].ArgVT.getSizeInBits();
2784 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
2785 Arg = DAG.getNode(
2786 ISD::SHL, DL, VA.getLocVT(), Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002787 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
Daniel Sandersc43cda82014-11-07 16:54:21 +00002788 }
2789
Wesley Peck527da1b2010-11-23 03:31:01 +00002790 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +00002791 // RegsToPass vector
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002792 if (VA.isRegLoc()) {
2793 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattner52f16de2008-03-17 06:57:02 +00002794 continue;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002795 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002796
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002797 // Register can't get to this point...
Chris Lattner52f16de2008-03-17 06:57:02 +00002798 assert(VA.isMemLoc());
Wesley Peck527da1b2010-11-23 03:31:01 +00002799
Wesley Peck527da1b2010-11-23 03:31:01 +00002800 // emit ISD::STORE whichs stores the
Chris Lattner52f16de2008-03-17 06:57:02 +00002801 // parameter value to a stack Location
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002802 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002803 Chain, Arg, DL, IsTailCall, DAG));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002804 }
2805
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002806 // Transform all store nodes into one single node because all store
2807 // nodes are independent of each other.
Wesley Peck527da1b2010-11-23 03:31:01 +00002808 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00002809 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002810
Bill Wendling24c79f22008-09-16 21:48:12 +00002811 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peck527da1b2010-11-23 03:31:01 +00002812 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2813 // node so that legalize doesn't hack it.
Eric Christopher96e72c62015-01-29 23:27:36 +00002814 bool IsPICCall = (ABI.IsN64() || IsPIC); // true if calls are translated to
2815 // jalr $25
Sasa Stankovic7072a792014-10-01 08:22:21 +00002816 bool GlobalOrExternal = false, InternalLinkage = false, IsCallReloc = false;
Akira Hatanakad6f1c582011-04-07 19:51:44 +00002817 SDValue CalleeLo;
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002818 EVT Ty = Callee.getValueType();
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002819
2820 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002821 if (IsPICCall) {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002822 const GlobalValue *Val = G->getGlobal();
2823 InternalLinkage = Val->hasInternalLinkage();
Akira Hatanakacf9a61b2012-12-13 03:17:29 +00002824
2825 if (InternalLinkage)
Eric Christopher96e72c62015-01-29 23:27:36 +00002826 Callee = getAddrLocal(G, DL, Ty, DAG, ABI.IsN32() || ABI.IsN64());
Sasa Stankovic7072a792014-10-01 08:22:21 +00002827 else if (LargeGOT) {
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002828 Callee = getAddrGlobalLargeGOT(G, DL, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002829 MipsII::MO_CALL_LO16, Chain,
2830 FuncInfo->callPtrInfo(Val));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002831 IsCallReloc = true;
2832 } else {
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002833 Callee = getAddrGlobal(G, DL, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002834 FuncInfo->callPtrInfo(Val));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002835 IsCallReloc = true;
2836 }
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002837 } else
Mehdi Amini44ede332015-07-09 02:09:04 +00002838 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL,
2839 getPointerTy(DAG.getDataLayout()), 0,
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002840 MipsII::MO_NO_FLAG);
Akira Hatanaka8e16aac2011-12-09 01:45:12 +00002841 GlobalOrExternal = true;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002842 }
2843 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002844 const char *Sym = S->getSymbol();
2845
Eric Christopher96e72c62015-01-29 23:27:36 +00002846 if (!ABI.IsN64() && !IsPIC) // !N64 && static
Mehdi Amini44ede332015-07-09 02:09:04 +00002847 Callee = DAG.getTargetExternalSymbol(
2848 Sym, getPointerTy(DAG.getDataLayout()), MipsII::MO_NO_FLAG);
Sasa Stankovic7072a792014-10-01 08:22:21 +00002849 else if (LargeGOT) {
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002850 Callee = getAddrGlobalLargeGOT(S, DL, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002851 MipsII::MO_CALL_LO16, Chain,
2852 FuncInfo->callPtrInfo(Sym));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002853 IsCallReloc = true;
2854 } else { // N64 || PIC
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002855 Callee = getAddrGlobal(S, DL, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002856 FuncInfo->callPtrInfo(Sym));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002857 IsCallReloc = true;
2858 }
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002859
Akira Hatanaka8e16aac2011-12-09 01:45:12 +00002860 GlobalOrExternal = true;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002861 }
2862
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002863 SmallVector<SDValue, 8> Ops(1, Chain);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002864 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002865
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002866 getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal, InternalLinkage,
Sasa Stankovic7072a792014-10-01 08:22:21 +00002867 IsCallReloc, CLI, Callee, Chain);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002868
Simon Dardis9a66bbe2016-09-21 09:43:40 +00002869 if (IsTailCall) {
2870 MF.getFrameInfo().setHasTailCall();
Craig Topper48d114b2014-04-26 18:35:24 +00002871 return DAG.getNode(MipsISD::TailCall, DL, MVT::Other, Ops);
Simon Dardis9a66bbe2016-09-21 09:43:40 +00002872 }
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002873
Craig Topper48d114b2014-04-26 18:35:24 +00002874 Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, Ops);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002875 SDValue InFlag = Chain.getValue(1);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002876
Bruno Cardoso Lopes193e64c2010-01-30 18:32:07 +00002877 // Create the CALLSEQ_END node.
Akira Hatanaka97ba7692012-07-26 23:27:01 +00002878 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002879 DAG.getIntPtrConstant(0, DL, true), InFlag, DL);
Bruno Cardoso Lopes193e64c2010-01-30 18:32:07 +00002880 InFlag = Chain.getValue(1);
2881
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002882 // Handle result values, copying them out of physregs into vregs that we
2883 // return.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002884 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg, Ins, DL, DAG,
2885 InVals, CLI);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002886}
2887
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002888/// LowerCallResult - Lower the result values of a call into the
2889/// appropriate copies out of appropriate physical registers.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002890SDValue MipsTargetLowering::LowerCallResult(
2891 SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool IsVarArg,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002892 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
2893 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals,
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002894 TargetLowering::CallLoweringInfo &CLI) const {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002895 // Assign locations to each value returned by this call.
2896 SmallVector<CCValAssign, 16> RVLocs;
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002897 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
2898 *DAG.getContext());
2899 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips, CLI);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002900
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002901 // Copy all of the result registers out of their specified physreg.
2902 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Daniel Sandersae275e32014-09-25 12:15:05 +00002903 CCValAssign &VA = RVLocs[i];
2904 assert(VA.isRegLoc() && "Can only return in registers!");
2905
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002906 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(),
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002907 RVLocs[i].getLocVT(), InFlag);
2908 Chain = Val.getValue(1);
2909 InFlag = Val.getValue(2);
2910
Daniel Sandersae275e32014-09-25 12:15:05 +00002911 if (VA.isUpperBitsInLoc()) {
2912 unsigned ValSizeInBits = Ins[i].ArgVT.getSizeInBits();
2913 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
2914 unsigned Shift =
2915 VA.getLocInfo() == CCValAssign::ZExtUpper ? ISD::SRL : ISD::SRA;
2916 Val = DAG.getNode(
2917 Shift, DL, VA.getLocVT(), Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002918 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
Daniel Sandersae275e32014-09-25 12:15:05 +00002919 }
2920
2921 switch (VA.getLocInfo()) {
2922 default:
2923 llvm_unreachable("Unknown loc info!");
2924 case CCValAssign::Full:
2925 break;
2926 case CCValAssign::BCvt:
2927 Val = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Val);
2928 break;
2929 case CCValAssign::AExt:
2930 case CCValAssign::AExtUpper:
2931 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2932 break;
2933 case CCValAssign::ZExt:
2934 case CCValAssign::ZExtUpper:
2935 Val = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Val,
2936 DAG.getValueType(VA.getValVT()));
2937 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2938 break;
2939 case CCValAssign::SExt:
2940 case CCValAssign::SExtUpper:
2941 Val = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Val,
2942 DAG.getValueType(VA.getValVT()));
2943 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2944 break;
2945 }
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002946
2947 InVals.push_back(Val);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002948 }
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +00002949
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002950 return Chain;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002951}
2952
Daniel Sandersc43cda82014-11-07 16:54:21 +00002953static SDValue UnpackFromArgumentSlot(SDValue Val, const CCValAssign &VA,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002954 EVT ArgVT, const SDLoc &DL,
2955 SelectionDAG &DAG) {
Daniel Sandersc43cda82014-11-07 16:54:21 +00002956 MVT LocVT = VA.getLocVT();
2957 EVT ValVT = VA.getValVT();
2958
2959 // Shift into the upper bits if necessary.
2960 switch (VA.getLocInfo()) {
2961 default:
2962 break;
2963 case CCValAssign::AExtUpper:
2964 case CCValAssign::SExtUpper:
2965 case CCValAssign::ZExtUpper: {
2966 unsigned ValSizeInBits = ArgVT.getSizeInBits();
2967 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
2968 unsigned Opcode =
2969 VA.getLocInfo() == CCValAssign::ZExtUpper ? ISD::SRL : ISD::SRA;
2970 Val = DAG.getNode(
2971 Opcode, DL, VA.getLocVT(), Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002972 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
Daniel Sandersc43cda82014-11-07 16:54:21 +00002973 break;
2974 }
2975 }
2976
2977 // If this is an value smaller than the argument slot size (32-bit for O32,
2978 // 64-bit for N32/N64), it has been promoted in some way to the argument slot
2979 // size. Extract the value and insert any appropriate assertions regarding
2980 // sign/zero extension.
2981 switch (VA.getLocInfo()) {
2982 default:
2983 llvm_unreachable("Unknown loc info!");
2984 case CCValAssign::Full:
2985 break;
2986 case CCValAssign::AExtUpper:
2987 case CCValAssign::AExt:
2988 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
2989 break;
2990 case CCValAssign::SExtUpper:
2991 case CCValAssign::SExt:
2992 Val = DAG.getNode(ISD::AssertSext, DL, LocVT, Val, DAG.getValueType(ValVT));
2993 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
2994 break;
2995 case CCValAssign::ZExtUpper:
2996 case CCValAssign::ZExt:
2997 Val = DAG.getNode(ISD::AssertZext, DL, LocVT, Val, DAG.getValueType(ValVT));
2998 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
2999 break;
3000 case CCValAssign::BCvt:
3001 Val = DAG.getNode(ISD::BITCAST, DL, ValVT, Val);
3002 break;
3003 }
3004
3005 return Val;
3006}
3007
Akira Hatanakae2489122011-04-15 21:51:11 +00003008//===----------------------------------------------------------------------===//
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003009// Formal Arguments Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00003010//===----------------------------------------------------------------------===//
Wesley Peck527da1b2010-11-23 03:31:01 +00003011/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003012/// and generate load operations for arguments places on the stack.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003013SDValue MipsTargetLowering::LowerFormalArguments(
3014 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
3015 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
3016 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Bruno Cardoso Lopesa01ede22008-08-04 07:12:52 +00003017 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00003018 MachineFrameInfo &MFI = MF.getFrameInfo();
Bruno Cardoso Lopes14033fb2007-08-28 05:08:16 +00003019 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00003020
Dan Gohman31ae5862010-04-17 14:41:14 +00003021 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003022
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003023 // Used with vargs to acumulate store chains.
3024 std::vector<SDValue> OutChains;
3025
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003026 // Assign locations to all of the incoming arguments.
3027 SmallVector<CCValAssign, 16> ArgLocs;
Daniel Sanders23e98772014-11-02 16:09:29 +00003028 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
3029 *DAG.getContext());
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003030 CCInfo.AllocateStack(ABI.GetCalleeAllocdArgSizeInBytes(CallConv), 1);
Vasileios Kalintiris43dff0c2015-10-26 12:38:43 +00003031 const Function *Func = DAG.getMachineFunction().getFunction();
3032 Function::const_arg_iterator FuncArg = Func->arg_begin();
3033
Vasileios Kalintiris165121f2015-10-26 14:24:30 +00003034 if (Func->hasFnAttribute("interrupt") && !Func->arg_empty())
3035 report_fatal_error(
3036 "Functions with the interrupt attribute cannot have arguments!");
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00003037
Daniel Sandersb70e27c2014-11-06 16:36:30 +00003038 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips_FixedArg);
Akira Hatanaka4866fe12012-10-30 19:37:25 +00003039 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(),
Daniel Sanders23e98772014-11-02 16:09:29 +00003040 CCInfo.getInRegsParamsCount() > 0);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00003041
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00003042 unsigned CurArgIdx = 0;
Daniel Sanders23e98772014-11-02 16:09:29 +00003043 CCInfo.rewindByValRegsInfo();
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003044
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00003045 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003046 CCValAssign &VA = ArgLocs[i];
Andrew Trick05938a52015-02-16 18:10:47 +00003047 if (Ins[i].isOrigArg()) {
3048 std::advance(FuncArg, Ins[i].getOrigArgIndex() - CurArgIdx);
3049 CurArgIdx = Ins[i].getOrigArgIndex();
3050 }
Akira Hatanaka104b7e32011-10-28 19:55:48 +00003051 EVT ValVT = VA.getValVT();
Akira Hatanakafb9bae32011-11-12 02:29:58 +00003052 ISD::ArgFlagsTy Flags = Ins[i].Flags;
3053 bool IsRegLoc = VA.isRegLoc();
3054
3055 if (Flags.isByVal()) {
Andrew Trick05938a52015-02-16 18:10:47 +00003056 assert(Ins[i].isOrigArg() && "Byval arguments cannot be implicit");
Daniel Sanders23e98772014-11-02 16:09:29 +00003057 unsigned FirstByValReg, LastByValReg;
3058 unsigned ByValIdx = CCInfo.getInRegsParamsProcessed();
3059 CCInfo.getInRegsParamInfo(ByValIdx, FirstByValReg, LastByValReg);
3060
Akira Hatanakafb9bae32011-11-12 02:29:58 +00003061 assert(Flags.getByValSize() &&
3062 "ByVal args of size 0 should have been ignored by front-end.");
Daniel Sanders23e98772014-11-02 16:09:29 +00003063 assert(ByValIdx < CCInfo.getInRegsParamsCount());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003064 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003065 FirstByValReg, LastByValReg, VA, CCInfo);
Daniel Sanders23e98772014-11-02 16:09:29 +00003066 CCInfo.nextInRegsParam();
Akira Hatanakafb9bae32011-11-12 02:29:58 +00003067 continue;
3068 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003069
3070 // Arguments stored on registers
Akira Hatanakafb9bae32011-11-12 02:29:58 +00003071 if (IsRegLoc) {
Akira Hatanaka7d822522013-10-28 21:21:36 +00003072 MVT RegVT = VA.getLocVT();
Akira Hatanakacb4a1a82011-05-24 00:23:52 +00003073 unsigned ArgReg = VA.getLocReg();
Akira Hatanaka7d822522013-10-28 21:21:36 +00003074 const TargetRegisterClass *RC = getRegClassFor(RegVT);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003075
Wesley Peck527da1b2010-11-23 03:31:01 +00003076 // Transform the arguments stored on
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003077 // physical registers into virtual ones
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003078 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC);
3079 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
Wesley Peck527da1b2010-11-23 03:31:01 +00003080
Daniel Sandersc43cda82014-11-07 16:54:21 +00003081 ArgValue = UnpackFromArgumentSlot(ArgValue, VA, Ins[i].ArgVT, DL, DAG);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00003082
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003083 // Handle floating point arguments passed in integer registers and
3084 // long double arguments passed in floating point registers.
Akira Hatanaka104b7e32011-10-28 19:55:48 +00003085 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003086 (RegVT == MVT::i64 && ValVT == MVT::f64) ||
3087 (RegVT == MVT::f64 && ValVT == MVT::i64))
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003088 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
Eric Christopher96e72c62015-01-29 23:27:36 +00003089 else if (ABI.IsO32() && RegVT == MVT::i32 &&
Eric Christopherbf33a3c2014-07-02 23:18:40 +00003090 ValVT == MVT::f64) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003091 unsigned Reg2 = addLiveIn(DAG.getMachineFunction(),
Akira Hatanaka104b7e32011-10-28 19:55:48 +00003092 getNextIntArgReg(ArgReg), RC);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003093 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT);
Eric Christopher1c29a652014-07-18 22:55:25 +00003094 if (!Subtarget.isLittle())
Akira Hatanaka104b7e32011-10-28 19:55:48 +00003095 std::swap(ArgValue, ArgValue2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003096 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64,
Akira Hatanaka104b7e32011-10-28 19:55:48 +00003097 ArgValue, ArgValue2);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00003098 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003099
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003100 InVals.push_back(ArgValue);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003101 } else { // VA.isRegLoc()
Daniel Sandersc43cda82014-11-07 16:54:21 +00003102 MVT LocVT = VA.getLocVT();
3103
Eric Christopher96e72c62015-01-29 23:27:36 +00003104 if (ABI.IsO32()) {
Daniel Sandersc43cda82014-11-07 16:54:21 +00003105 // We ought to be able to use LocVT directly but O32 sets it to i32
3106 // when allocating floating point values to integer registers.
3107 // This shouldn't influence how we load the value into registers unless
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00003108 // we are targeting softfloat.
Eric Christophere8ae3e32015-05-07 23:10:21 +00003109 if (VA.getValVT().isFloatingPoint() && !Subtarget.useSoftFloat())
Daniel Sandersc43cda82014-11-07 16:54:21 +00003110 LocVT = VA.getValVT();
3111 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003112
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003113 // sanity check
3114 assert(VA.isMemLoc());
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003115
Wesley Peck527da1b2010-11-23 03:31:01 +00003116 // The stack pointer offset is relative to the caller stack frame.
Matthias Braun941a7052016-07-28 18:40:00 +00003117 int FI = MFI.CreateFixedObject(LocVT.getSizeInBits() / 8,
3118 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003119
3120 // Create load nodes to retrieve arguments from the stack
Mehdi Amini44ede332015-07-09 02:09:04 +00003121 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Alex Lorenze40c8a22015-08-11 23:09:45 +00003122 SDValue ArgValue = DAG.getLoad(
3123 LocVT, DL, Chain, FIN,
Justin Lebar9c375812016-07-15 18:27:10 +00003124 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI));
Daniel Sandersc43cda82014-11-07 16:54:21 +00003125 OutChains.push_back(ArgValue.getValue(1));
3126
3127 ArgValue = UnpackFromArgumentSlot(ArgValue, VA, Ins[i].ArgVT, DL, DAG);
3128
3129 InVals.push_back(ArgValue);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003130 }
Reid Kleckner7a59e082014-05-12 22:01:27 +00003131 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003132
Reid Kleckner7a59e082014-05-12 22:01:27 +00003133 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Reid Kleckner79418562014-05-09 22:32:13 +00003134 // The mips ABIs for returning structs by value requires that we copy
3135 // the sret argument into $v0 for the return. Save the argument into
3136 // a virtual register so that we can access it from the return points.
Reid Kleckner7a59e082014-05-12 22:01:27 +00003137 if (Ins[i].Flags.isSRet()) {
Reid Kleckner79418562014-05-09 22:32:13 +00003138 unsigned Reg = MipsFI->getSRetReturnReg();
3139 if (!Reg) {
3140 Reg = MF.getRegInfo().createVirtualRegister(
Eric Christopher96e72c62015-01-29 23:27:36 +00003141 getRegClassFor(ABI.IsN64() ? MVT::i64 : MVT::i32));
Reid Kleckner79418562014-05-09 22:32:13 +00003142 MipsFI->setSRetReturnReg(Reg);
3143 }
Reid Kleckner7a59e082014-05-12 22:01:27 +00003144 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[i]);
Reid Kleckner79418562014-05-09 22:32:13 +00003145 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
Reid Kleckner7a59e082014-05-12 22:01:27 +00003146 break;
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003147 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003148 }
3149
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003150 if (IsVarArg)
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003151 writeVarArgRegs(OutChains, Chain, DL, DAG, CCInfo);
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003152
Wesley Peck527da1b2010-11-23 03:31:01 +00003153 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003154 // the size of Ins and InVals. This only happens when on varg functions
3155 if (!OutChains.empty()) {
3156 OutChains.push_back(Chain);
Craig Topper48d114b2014-04-26 18:35:24 +00003157 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003158 }
3159
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003160 return Chain;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003161}
3162
Akira Hatanakae2489122011-04-15 21:51:11 +00003163//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003164// Return Value Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00003165//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003166
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003167bool
3168MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003169 MachineFunction &MF, bool IsVarArg,
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003170 const SmallVectorImpl<ISD::OutputArg> &Outs,
3171 LLVMContext &Context) const {
3172 SmallVector<CCValAssign, 16> RVLocs;
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003173 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003174 return CCInfo.CheckReturn(Outs, RetCC_Mips);
3175}
3176
Petar Jovanovic5b436222015-03-23 12:28:13 +00003177bool
3178MipsTargetLowering::shouldSignExtendTypeInLibCall(EVT Type, bool IsSigned) const {
Eric Christophere8ae3e32015-05-07 23:10:21 +00003179 if (Subtarget.hasMips3() && Subtarget.useSoftFloat()) {
Petar Jovanovic5b436222015-03-23 12:28:13 +00003180 if (Type == MVT::i32)
3181 return true;
3182 }
3183 return IsSigned;
3184}
3185
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003186SDValue
Vasileios Kalintiris43dff0c2015-10-26 12:38:43 +00003187MipsTargetLowering::LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003188 const SDLoc &DL,
3189 SelectionDAG &DAG) const {
Vasileios Kalintiris43dff0c2015-10-26 12:38:43 +00003190
3191 MachineFunction &MF = DAG.getMachineFunction();
3192 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3193
3194 MipsFI->setISR();
3195
3196 return DAG.getNode(MipsISD::ERet, DL, MVT::Other, RetOps);
3197}
3198
3199SDValue
3200MipsTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
3201 bool IsVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003202 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003203 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003204 const SDLoc &DL, SelectionDAG &DAG) const {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003205 // CCValAssign - represent the assignment of
3206 // the return value to a location
3207 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003208 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003209
3210 // CCState - Info about the registers and stack slot.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003211 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, *DAG.getContext());
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003212
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003213 // Analyze return values.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003214 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003215
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003216 SDValue Flag;
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003217 SmallVector<SDValue, 4> RetOps(1, Chain);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003218
3219 // Copy the result values into the output registers.
3220 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003221 SDValue Val = OutVals[i];
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003222 CCValAssign &VA = RVLocs[i];
3223 assert(VA.isRegLoc() && "Can only return in registers!");
Daniel Sandersae275e32014-09-25 12:15:05 +00003224 bool UseUpperBits = false;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003225
Daniel Sandersae275e32014-09-25 12:15:05 +00003226 switch (VA.getLocInfo()) {
3227 default:
3228 llvm_unreachable("Unknown loc info!");
3229 case CCValAssign::Full:
3230 break;
3231 case CCValAssign::BCvt:
3232 Val = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Val);
3233 break;
3234 case CCValAssign::AExtUpper:
3235 UseUpperBits = true;
Justin Bognerb03fd122016-08-17 05:10:15 +00003236 LLVM_FALLTHROUGH;
Daniel Sandersae275e32014-09-25 12:15:05 +00003237 case CCValAssign::AExt:
3238 Val = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Val);
3239 break;
3240 case CCValAssign::ZExtUpper:
3241 UseUpperBits = true;
Justin Bognerb03fd122016-08-17 05:10:15 +00003242 LLVM_FALLTHROUGH;
Daniel Sandersae275e32014-09-25 12:15:05 +00003243 case CCValAssign::ZExt:
3244 Val = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Val);
3245 break;
3246 case CCValAssign::SExtUpper:
3247 UseUpperBits = true;
Justin Bognerb03fd122016-08-17 05:10:15 +00003248 LLVM_FALLTHROUGH;
Daniel Sandersae275e32014-09-25 12:15:05 +00003249 case CCValAssign::SExt:
3250 Val = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Val);
3251 break;
3252 }
3253
3254 if (UseUpperBits) {
3255 unsigned ValSizeInBits = Outs[i].ArgVT.getSizeInBits();
3256 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
3257 Val = DAG.getNode(
3258 ISD::SHL, DL, VA.getLocVT(), Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003259 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
Daniel Sandersae275e32014-09-25 12:15:05 +00003260 }
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003261
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003262 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003263
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003264 // Guarantee that all emitted copies are stuck together with flags.
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003265 Flag = Chain.getValue(1);
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003266 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003267 }
3268
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003269 // The mips ABIs for returning structs by value requires that we copy
3270 // the sret argument into $v0 for the return. We saved the argument into
3271 // a virtual register in the entry block, so now we copy the value out
3272 // and into $v0.
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003273 if (MF.getFunction()->hasStructRetAttr()) {
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003274 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3275 unsigned Reg = MipsFI->getSRetReturnReg();
3276
Wesley Peck527da1b2010-11-23 03:31:01 +00003277 if (!Reg)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003278 llvm_unreachable("sret virtual register not created in the entry block");
Mehdi Amini44ede332015-07-09 02:09:04 +00003279 SDValue Val =
3280 DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(DAG.getDataLayout()));
Eric Christopher96e72c62015-01-29 23:27:36 +00003281 unsigned V0 = ABI.IsN64() ? Mips::V0_64 : Mips::V0;
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003282
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003283 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003284 Flag = Chain.getValue(1);
Mehdi Amini44ede332015-07-09 02:09:04 +00003285 RetOps.push_back(DAG.getRegister(V0, getPointerTy(DAG.getDataLayout())));
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003286 }
3287
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003288 RetOps[0] = Chain; // Update chain.
Akira Hatanakaefff7b72012-07-10 00:19:06 +00003289
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003290 // Add the flag if we have it.
3291 if (Flag.getNode())
3292 RetOps.push_back(Flag);
3293
Vasileios Kalintiris43dff0c2015-10-26 12:38:43 +00003294 // ISRs must use "eret".
3295 if (DAG.getMachineFunction().getFunction()->hasFnAttribute("interrupt"))
3296 return LowerInterruptReturn(RetOps, DL, DAG);
3297
3298 // Standard return on Mips is a "jr $ra"
Craig Topper48d114b2014-04-26 18:35:24 +00003299 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, RetOps);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003300}
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003301
Akira Hatanakae2489122011-04-15 21:51:11 +00003302//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003303// Mips Inline Assembly Support
Akira Hatanakae2489122011-04-15 21:51:11 +00003304//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003305
3306/// getConstraintType - Given a constraint letter, return the type of
3307/// constraint it is for this target.
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003308MipsTargetLowering::ConstraintType
3309MipsTargetLowering::getConstraintType(StringRef Constraint) const {
Daniel Sanders8b59af12013-11-12 12:56:01 +00003310 // Mips specific constraints
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003311 // GCC config/mips/constraints.md
3312 //
Wesley Peck527da1b2010-11-23 03:31:01 +00003313 // 'd' : An address register. Equivalent to r
3314 // unless generating MIPS16 code.
3315 // 'y' : Equivalent to r; retained for
3316 // backwards compatibility.
Eric Christophere3c494d2012-05-07 06:25:10 +00003317 // 'c' : A register suitable for use in an indirect
3318 // jump. This will always be $25 for -mabicalls.
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003319 // 'l' : The lo register. 1 word storage.
3320 // 'x' : The hilo register pair. Double word storage.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003321 if (Constraint.size() == 1) {
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003322 switch (Constraint[0]) {
3323 default : break;
Wesley Peck527da1b2010-11-23 03:31:01 +00003324 case 'd':
3325 case 'y':
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003326 case 'f':
Eric Christophere3c494d2012-05-07 06:25:10 +00003327 case 'c':
Eric Christopher9c492e62012-05-07 06:25:15 +00003328 case 'l':
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003329 case 'x':
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003330 return C_RegisterClass;
Jack Carter0e149b02013-03-04 21:33:15 +00003331 case 'R':
3332 return C_Memory;
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003333 }
3334 }
Daniel Sandersa73d8fe2015-03-24 11:26:34 +00003335
3336 if (Constraint == "ZC")
3337 return C_Memory;
3338
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003339 return TargetLowering::getConstraintType(Constraint);
3340}
3341
John Thompsone8360b72010-10-29 17:29:13 +00003342/// Examine constraint type and operand type and determine a weight value.
3343/// This object must already have been set up with the operand type
3344/// and the current alternative constraint selected.
3345TargetLowering::ConstraintWeight
3346MipsTargetLowering::getSingleConstraintMatchWeight(
3347 AsmOperandInfo &info, const char *constraint) const {
3348 ConstraintWeight weight = CW_Invalid;
3349 Value *CallOperandVal = info.CallOperandVal;
3350 // If we don't have a value, we can't do a match,
3351 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +00003352 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +00003353 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +00003354 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +00003355 // Look at the constraint type.
3356 switch (*constraint) {
3357 default:
3358 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
3359 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00003360 case 'd':
3361 case 'y':
John Thompsone8360b72010-10-29 17:29:13 +00003362 if (type->isIntegerTy())
3363 weight = CW_Register;
3364 break;
Daniel Sanders8b59af12013-11-12 12:56:01 +00003365 case 'f': // FPU or MSA register
Eric Christopher1c29a652014-07-18 22:55:25 +00003366 if (Subtarget.hasMSA() && type->isVectorTy() &&
Daniel Sanders8b59af12013-11-12 12:56:01 +00003367 cast<VectorType>(type)->getBitWidth() == 128)
3368 weight = CW_Register;
3369 else if (type->isFloatTy())
John Thompsone8360b72010-10-29 17:29:13 +00003370 weight = CW_Register;
3371 break;
Eric Christophere3c494d2012-05-07 06:25:10 +00003372 case 'c': // $25 for indirect jumps
Eric Christopher9c492e62012-05-07 06:25:15 +00003373 case 'l': // lo register
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003374 case 'x': // hilo register pair
Daniel Sanders8b59af12013-11-12 12:56:01 +00003375 if (type->isIntegerTy())
Eric Christophere3c494d2012-05-07 06:25:10 +00003376 weight = CW_SpecificReg;
Daniel Sanders8b59af12013-11-12 12:56:01 +00003377 break;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003378 case 'I': // signed 16 bit immediate
Eric Christopher7201e1b2012-05-07 03:13:42 +00003379 case 'J': // integer zero
Eric Christopher3ff88a02012-05-07 05:46:29 +00003380 case 'K': // unsigned 16 bit immediate
Eric Christopher1109b342012-05-07 05:46:37 +00003381 case 'L': // signed 32 bit immediate where lower 16 bits are 0
Eric Christophere07aa432012-05-07 05:46:43 +00003382 case 'N': // immediate in the range of -65535 to -1 (inclusive)
Eric Christopher470578a2012-05-07 05:46:48 +00003383 case 'O': // signed 15 bit immediate (+- 16383)
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003384 case 'P': // immediate in the range of 65535 to 1 (inclusive)
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003385 if (isa<ConstantInt>(CallOperandVal))
3386 weight = CW_Constant;
3387 break;
Jack Carter0e149b02013-03-04 21:33:15 +00003388 case 'R':
3389 weight = CW_Memory;
3390 break;
John Thompsone8360b72010-10-29 17:29:13 +00003391 }
3392 return weight;
3393}
3394
Akira Hatanaka7473b472013-08-14 00:21:25 +00003395/// This is a helper function to parse a physical register string and split it
3396/// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag
3397/// that is returned indicates whether parsing was successful. The second flag
3398/// is true if the numeric part exists.
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003399static std::pair<bool, bool> parsePhysicalReg(StringRef C, StringRef &Prefix,
3400 unsigned long long &Reg) {
Akira Hatanaka7473b472013-08-14 00:21:25 +00003401 if (C.front() != '{' || C.back() != '}')
3402 return std::make_pair(false, false);
3403
3404 // Search for the first numeric character.
3405 StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1;
Craig Topper2241dfd2015-11-23 07:19:06 +00003406 I = std::find_if(B, E, isdigit);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003407
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003408 Prefix = StringRef(B, I - B);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003409
3410 // The second flag is set to false if no numeric characters were found.
3411 if (I == E)
3412 return std::make_pair(true, false);
3413
3414 // Parse the numeric characters.
3415 return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg),
3416 true);
3417}
3418
3419std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering::
Craig Topper6dc4a8bc2014-08-30 16:48:02 +00003420parseRegForInlineAsmConstraint(StringRef C, MVT VT) const {
Eric Christopherd9134482014-08-04 21:25:23 +00003421 const TargetRegisterInfo *TRI =
Eric Christopher96e72c62015-01-29 23:27:36 +00003422 Subtarget.getRegisterInfo();
Akira Hatanaka7473b472013-08-14 00:21:25 +00003423 const TargetRegisterClass *RC;
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003424 StringRef Prefix;
Akira Hatanaka7473b472013-08-14 00:21:25 +00003425 unsigned long long Reg;
3426
3427 std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg);
3428
3429 if (!R.first)
Craig Topper062a2ba2014-04-25 05:30:21 +00003430 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003431
3432 if ((Prefix == "hi" || Prefix == "lo")) { // Parse hi/lo.
3433 // No numeric characters follow "hi" or "lo".
3434 if (R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003435 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003436
3437 RC = TRI->getRegClass(Prefix == "hi" ?
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00003438 Mips::HI32RegClassID : Mips::LO32RegClassID);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003439 return std::make_pair(*(RC->begin()), RC);
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003440 } else if (Prefix.startswith("$msa")) {
Daniel Sanders8b59af12013-11-12 12:56:01 +00003441 // Parse $msa(ir|csr|access|save|modify|request|map|unmap)
3442
3443 // No numeric characters follow the name.
3444 if (R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003445 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003446
3447 Reg = StringSwitch<unsigned long long>(Prefix)
3448 .Case("$msair", Mips::MSAIR)
3449 .Case("$msacsr", Mips::MSACSR)
3450 .Case("$msaaccess", Mips::MSAAccess)
3451 .Case("$msasave", Mips::MSASave)
3452 .Case("$msamodify", Mips::MSAModify)
3453 .Case("$msarequest", Mips::MSARequest)
3454 .Case("$msamap", Mips::MSAMap)
3455 .Case("$msaunmap", Mips::MSAUnmap)
3456 .Default(0);
3457
3458 if (!Reg)
Craig Topper062a2ba2014-04-25 05:30:21 +00003459 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003460
3461 RC = TRI->getRegClass(Mips::MSACtrlRegClassID);
3462 return std::make_pair(Reg, RC);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003463 }
3464
3465 if (!R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003466 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003467
3468 if (Prefix == "$f") { // Parse $f0-$f31.
3469 // If the size of FP registers is 64-bit or Reg is an even number, select
3470 // the 64-bit register class. Otherwise, select the 32-bit register class.
3471 if (VT == MVT::Other)
Eric Christopher1c29a652014-07-18 22:55:25 +00003472 VT = (Subtarget.isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32;
Akira Hatanaka7473b472013-08-14 00:21:25 +00003473
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003474 RC = getRegClassFor(VT);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003475
3476 if (RC == &Mips::AFGR64RegClass) {
3477 assert(Reg % 2 == 0);
3478 Reg >>= 1;
3479 }
Daniel Sanders8b59af12013-11-12 12:56:01 +00003480 } else if (Prefix == "$fcc") // Parse $fcc0-$fcc7.
Akira Hatanaka7473b472013-08-14 00:21:25 +00003481 RC = TRI->getRegClass(Mips::FCCRegClassID);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003482 else if (Prefix == "$w") { // Parse $w0-$w31.
3483 RC = getRegClassFor((VT == MVT::Other) ? MVT::v16i8 : VT);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003484 } else { // Parse $0-$31.
3485 assert(Prefix == "$");
3486 RC = getRegClassFor((VT == MVT::Other) ? MVT::i32 : VT);
3487 }
3488
3489 assert(Reg < RC->getNumRegs());
3490 return std::make_pair(*(RC->begin() + Reg), RC);
3491}
3492
Eric Christophereaf77dc2011-06-29 19:33:04 +00003493/// Given a register class constraint, like 'r', if this corresponds directly
3494/// to an LLVM register class, return a register of 0 and the register class
3495/// pointer.
Eric Christopher11e4df72015-02-26 22:38:43 +00003496std::pair<unsigned, const TargetRegisterClass *>
3497MipsTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003498 StringRef Constraint,
Eric Christopher11e4df72015-02-26 22:38:43 +00003499 MVT VT) const {
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003500 if (Constraint.size() == 1) {
3501 switch (Constraint[0]) {
Eric Christopher9519c082011-06-29 19:04:31 +00003502 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
3503 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003504 case 'r':
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003505 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
Eric Christopher1c29a652014-07-18 22:55:25 +00003506 if (Subtarget.inMips16Mode())
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003507 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003508 return std::make_pair(0U, &Mips::GPR32RegClass);
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003509 }
Eric Christopher1c29a652014-07-18 22:55:25 +00003510 if (VT == MVT::i64 && !Subtarget.isGP64bit())
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003511 return std::make_pair(0U, &Mips::GPR32RegClass);
Eric Christopher1c29a652014-07-18 22:55:25 +00003512 if (VT == MVT::i64 && Subtarget.isGP64bit())
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003513 return std::make_pair(0U, &Mips::GPR64RegClass);
Eric Christopher58daf042012-05-07 03:13:22 +00003514 // This will generate an error message
Craig Topper062a2ba2014-04-25 05:30:21 +00003515 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003516 case 'f': // FPU or MSA register
3517 if (VT == MVT::v16i8)
3518 return std::make_pair(0U, &Mips::MSA128BRegClass);
3519 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
3520 return std::make_pair(0U, &Mips::MSA128HRegClass);
3521 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
3522 return std::make_pair(0U, &Mips::MSA128WRegClass);
3523 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
3524 return std::make_pair(0U, &Mips::MSA128DRegClass);
3525 else if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00003526 return std::make_pair(0U, &Mips::FGR32RegClass);
Eric Christopher1c29a652014-07-18 22:55:25 +00003527 else if ((VT == MVT::f64) && (!Subtarget.isSingleFloat())) {
3528 if (Subtarget.isFP64bit())
Craig Topperc7242e02012-04-20 07:30:17 +00003529 return std::make_pair(0U, &Mips::FGR64RegClass);
3530 return std::make_pair(0U, &Mips::AFGR64RegClass);
Akira Hatanakac669d7a2012-01-04 02:45:01 +00003531 }
Eric Christophere3c494d2012-05-07 06:25:10 +00003532 break;
3533 case 'c': // register suitable for indirect jump
3534 if (VT == MVT::i32)
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003535 return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass);
Eric Christophere3c494d2012-05-07 06:25:10 +00003536 assert(VT == MVT::i64 && "Unexpected type.");
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003537 return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass);
Eric Christopher9c492e62012-05-07 06:25:15 +00003538 case 'l': // register suitable for indirect jump
3539 if (VT == MVT::i32)
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00003540 return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass);
3541 return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass);
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003542 case 'x': // register suitable for indirect jump
3543 // Fixme: Not triggering the use of both hi and low
3544 // This will generate an error message
Craig Topper062a2ba2014-04-25 05:30:21 +00003545 return std::make_pair(0U, nullptr);
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003546 }
3547 }
Akira Hatanaka7473b472013-08-14 00:21:25 +00003548
3549 std::pair<unsigned, const TargetRegisterClass *> R;
3550 R = parseRegForInlineAsmConstraint(Constraint, VT);
3551
3552 if (R.second)
3553 return R;
3554
Eric Christopher11e4df72015-02-26 22:38:43 +00003555 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003556}
3557
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003558/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3559/// vector. If it is invalid, don't add anything to Ops.
3560void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3561 std::string &Constraint,
3562 std::vector<SDValue>&Ops,
3563 SelectionDAG &DAG) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003564 SDLoc DL(Op);
Craig Topper062a2ba2014-04-25 05:30:21 +00003565 SDValue Result;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003566
3567 // Only support length 1 constraints for now.
3568 if (Constraint.length() > 1) return;
3569
3570 char ConstraintLetter = Constraint[0];
3571 switch (ConstraintLetter) {
3572 default: break; // This will fall through to the generic implementation
3573 case 'I': // Signed 16 bit constant
3574 // If this fails, the parent routine will give an error
3575 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3576 EVT Type = Op.getValueType();
3577 int64_t Val = C->getSExtValue();
3578 if (isInt<16>(Val)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003579 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003580 break;
3581 }
3582 }
3583 return;
Eric Christopher7201e1b2012-05-07 03:13:42 +00003584 case 'J': // integer zero
3585 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3586 EVT Type = Op.getValueType();
3587 int64_t Val = C->getZExtValue();
3588 if (Val == 0) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003589 Result = DAG.getTargetConstant(0, DL, Type);
Eric Christopher7201e1b2012-05-07 03:13:42 +00003590 break;
3591 }
3592 }
3593 return;
Eric Christopher3ff88a02012-05-07 05:46:29 +00003594 case 'K': // unsigned 16 bit immediate
3595 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3596 EVT Type = Op.getValueType();
3597 uint64_t Val = (uint64_t)C->getZExtValue();
3598 if (isUInt<16>(Val)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003599 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopher3ff88a02012-05-07 05:46:29 +00003600 break;
3601 }
3602 }
3603 return;
Eric Christopher1109b342012-05-07 05:46:37 +00003604 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3605 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3606 EVT Type = Op.getValueType();
3607 int64_t Val = C->getSExtValue();
3608 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003609 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopher1109b342012-05-07 05:46:37 +00003610 break;
3611 }
3612 }
3613 return;
Eric Christophere07aa432012-05-07 05:46:43 +00003614 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3615 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3616 EVT Type = Op.getValueType();
3617 int64_t Val = C->getSExtValue();
3618 if ((Val >= -65535) && (Val <= -1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003619 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christophere07aa432012-05-07 05:46:43 +00003620 break;
3621 }
3622 }
3623 return;
Eric Christopher470578a2012-05-07 05:46:48 +00003624 case 'O': // signed 15 bit immediate
3625 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3626 EVT Type = Op.getValueType();
3627 int64_t Val = C->getSExtValue();
3628 if ((isInt<15>(Val))) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003629 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopher470578a2012-05-07 05:46:48 +00003630 break;
3631 }
3632 }
3633 return;
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003634 case 'P': // immediate in the range of 1 to 65535 (inclusive)
3635 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3636 EVT Type = Op.getValueType();
3637 int64_t Val = C->getSExtValue();
3638 if ((Val <= 65535) && (Val >= 1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003639 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003640 break;
3641 }
3642 }
3643 return;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003644 }
3645
3646 if (Result.getNode()) {
3647 Ops.push_back(Result);
3648 return;
3649 }
3650
3651 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3652}
3653
Mehdi Amini0cdec1e2015-07-09 02:09:40 +00003654bool MipsTargetLowering::isLegalAddressingMode(const DataLayout &DL,
3655 const AddrMode &AM, Type *Ty,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +00003656 unsigned AS) const {
Akira Hatanakaef839192012-11-17 00:25:41 +00003657 // No global is ever allowed as a base.
3658 if (AM.BaseGV)
3659 return false;
3660
3661 switch (AM.Scale) {
3662 case 0: // "r+i" or just "i", depending on HasBaseReg.
3663 break;
3664 case 1:
3665 if (!AM.HasBaseReg) // allow "r+i".
3666 break;
3667 return false; // disallow "r+r" or "r+r+i".
3668 default:
3669 return false;
3670 }
3671
3672 return true;
3673}
3674
3675bool
Dan Gohman2fe6bee2008-10-18 02:06:02 +00003676MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3677 // The Mips target isn't yet aware of offsets.
3678 return false;
3679}
Evan Cheng16993aa2009-10-27 19:56:55 +00003680
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003681EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003682 unsigned SrcAlign,
3683 bool IsMemset, bool ZeroMemset,
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003684 bool MemcpyStrSrc,
3685 MachineFunction &MF) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003686 if (Subtarget.hasMips64())
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003687 return MVT::i64;
3688
3689 return MVT::i32;
3690}
3691
Evan Cheng83896a52009-10-28 01:43:28 +00003692bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
3693 if (VT != MVT::f32 && VT != MVT::f64)
3694 return false;
Bruno Cardoso Lopesb02a9df2011-01-18 19:41:41 +00003695 if (Imm.isNegZero())
3696 return false;
Evan Cheng16993aa2009-10-27 19:56:55 +00003697 return Imm.isZero();
3698}
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003699
3700unsigned MipsTargetLowering::getJumpTableEncoding() const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003701 if (ABI.IsN64())
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003702 return MachineJumpTableInfo::EK_GPRel64BlockAddress;
Jia Liuf54f60f2012-02-28 07:46:26 +00003703
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003704 return TargetLowering::getJumpTableEncoding();
3705}
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003706
Eric Christopher824f42f2015-05-12 01:26:05 +00003707bool MipsTargetLowering::useSoftFloat() const {
3708 return Subtarget.useSoftFloat();
3709}
3710
Daniel Sandersf43e6872014-11-01 18:44:56 +00003711void MipsTargetLowering::copyByValRegs(
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003712 SDValue Chain, const SDLoc &DL, std::vector<SDValue> &OutChains,
3713 SelectionDAG &DAG, const ISD::ArgFlagsTy &Flags,
3714 SmallVectorImpl<SDValue> &InVals, const Argument *FuncArg,
3715 unsigned FirstReg, unsigned LastReg, const CCValAssign &VA,
3716 MipsCCState &State) const {
Akira Hatanaka25dad192012-10-27 00:10:18 +00003717 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00003718 MachineFrameInfo &MFI = MF.getFrameInfo();
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003719 unsigned GPRSizeInBytes = Subtarget.getGPRSizeInBytes();
Daniel Sanders23e98772014-11-02 16:09:29 +00003720 unsigned NumRegs = LastReg - FirstReg;
3721 unsigned RegAreaSize = NumRegs * GPRSizeInBytes;
Akira Hatanaka25dad192012-10-27 00:10:18 +00003722 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
3723 int FrameObjOffset;
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003724 ArrayRef<MCPhysReg> ByValArgRegs = ABI.GetByValArgRegs();
Akira Hatanaka25dad192012-10-27 00:10:18 +00003725
3726 if (RegAreaSize)
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003727 FrameObjOffset =
3728 (int)ABI.GetCalleeAllocdArgSizeInBytes(State.getCallingConv()) -
3729 (int)((ByValArgRegs.size() - FirstReg) * GPRSizeInBytes);
Akira Hatanaka25dad192012-10-27 00:10:18 +00003730 else
Daniel Sandersf43e6872014-11-01 18:44:56 +00003731 FrameObjOffset = VA.getLocMemOffset();
Akira Hatanaka25dad192012-10-27 00:10:18 +00003732
3733 // Create frame object.
Mehdi Amini44ede332015-07-09 02:09:04 +00003734 EVT PtrTy = getPointerTy(DAG.getDataLayout());
Matthias Braun941a7052016-07-28 18:40:00 +00003735 int FI = MFI.CreateFixedObject(FrameObjSize, FrameObjOffset, true);
Akira Hatanaka25dad192012-10-27 00:10:18 +00003736 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
3737 InVals.push_back(FIN);
3738
Daniel Sanders23e98772014-11-02 16:09:29 +00003739 if (!NumRegs)
Akira Hatanaka25dad192012-10-27 00:10:18 +00003740 return;
3741
3742 // Copy arg registers.
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003743 MVT RegTy = MVT::getIntegerVT(GPRSizeInBytes * 8);
Akira Hatanaka25dad192012-10-27 00:10:18 +00003744 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3745
Daniel Sanders23e98772014-11-02 16:09:29 +00003746 for (unsigned I = 0; I < NumRegs; ++I) {
Daniel Sandersd7eba312014-11-07 12:21:37 +00003747 unsigned ArgReg = ByValArgRegs[FirstReg + I];
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003748 unsigned VReg = addLiveIn(MF, ArgReg, RC);
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003749 unsigned Offset = I * GPRSizeInBytes;
Akira Hatanaka25dad192012-10-27 00:10:18 +00003750 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003751 DAG.getConstant(Offset, DL, PtrTy));
Akira Hatanaka25dad192012-10-27 00:10:18 +00003752 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
Justin Lebar9c375812016-07-15 18:27:10 +00003753 StorePtr, MachinePointerInfo(FuncArg, Offset));
Akira Hatanaka25dad192012-10-27 00:10:18 +00003754 OutChains.push_back(Store);
3755 }
3756}
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003757
3758// Copy byVal arg to registers and stack.
Daniel Sandersf43e6872014-11-01 18:44:56 +00003759void MipsTargetLowering::passByValArg(
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003760 SDValue Chain, const SDLoc &DL,
Daniel Sandersf43e6872014-11-01 18:44:56 +00003761 std::deque<std::pair<unsigned, SDValue>> &RegsToPass,
3762 SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr,
Matthias Braun941a7052016-07-28 18:40:00 +00003763 MachineFrameInfo &MFI, SelectionDAG &DAG, SDValue Arg, unsigned FirstReg,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003764 unsigned LastReg, const ISD::ArgFlagsTy &Flags, bool isLittle,
3765 const CCValAssign &VA) const {
Daniel Sandersac272632014-05-23 13:18:02 +00003766 unsigned ByValSizeInBytes = Flags.getByValSize();
3767 unsigned OffsetInBytes = 0; // From beginning of struct
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003768 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
Daniel Sandersac272632014-05-23 13:18:02 +00003769 unsigned Alignment = std::min(Flags.getByValAlign(), RegSizeInBytes);
Mehdi Amini44ede332015-07-09 02:09:04 +00003770 EVT PtrTy = getPointerTy(DAG.getDataLayout()),
3771 RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
Daniel Sanders23e98772014-11-02 16:09:29 +00003772 unsigned NumRegs = LastReg - FirstReg;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003773
Daniel Sanders23e98772014-11-02 16:09:29 +00003774 if (NumRegs) {
Craig Topper862d5d82015-09-28 00:15:34 +00003775 ArrayRef<MCPhysReg> ArgRegs = ABI.GetByValArgRegs();
Daniel Sanders23e98772014-11-02 16:09:29 +00003776 bool LeftoverBytes = (NumRegs * RegSizeInBytes > ByValSizeInBytes);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003777 unsigned I = 0;
3778
3779 // Copy words to registers.
Daniel Sanders23e98772014-11-02 16:09:29 +00003780 for (; I < NumRegs - LeftoverBytes; ++I, OffsetInBytes += RegSizeInBytes) {
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003781 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003782 DAG.getConstant(OffsetInBytes, DL, PtrTy));
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003783 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
Justin Lebar9c375812016-07-15 18:27:10 +00003784 MachinePointerInfo(), Alignment);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003785 MemOpChains.push_back(LoadVal.getValue(1));
Daniel Sanders23e98772014-11-02 16:09:29 +00003786 unsigned ArgReg = ArgRegs[FirstReg + I];
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003787 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
3788 }
3789
3790 // Return if the struct has been fully copied.
Daniel Sandersac272632014-05-23 13:18:02 +00003791 if (ByValSizeInBytes == OffsetInBytes)
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003792 return;
3793
3794 // Copy the remainder of the byval argument with sub-word loads and shifts.
3795 if (LeftoverBytes) {
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003796 SDValue Val;
3797
Daniel Sandersac272632014-05-23 13:18:02 +00003798 for (unsigned LoadSizeInBytes = RegSizeInBytes / 2, TotalBytesLoaded = 0;
3799 OffsetInBytes < ByValSizeInBytes; LoadSizeInBytes /= 2) {
3800 unsigned RemainingSizeInBytes = ByValSizeInBytes - OffsetInBytes;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003801
Daniel Sandersac272632014-05-23 13:18:02 +00003802 if (RemainingSizeInBytes < LoadSizeInBytes)
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003803 continue;
3804
3805 // Load subword.
3806 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003807 DAG.getConstant(OffsetInBytes, DL,
3808 PtrTy));
Daniel Sandersac272632014-05-23 13:18:02 +00003809 SDValue LoadVal = DAG.getExtLoad(
3810 ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr, MachinePointerInfo(),
Justin Lebar9c375812016-07-15 18:27:10 +00003811 MVT::getIntegerVT(LoadSizeInBytes * 8), Alignment);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003812 MemOpChains.push_back(LoadVal.getValue(1));
3813
3814 // Shift the loaded value.
3815 unsigned Shamt;
3816
3817 if (isLittle)
Daniel Sandersac272632014-05-23 13:18:02 +00003818 Shamt = TotalBytesLoaded * 8;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003819 else
Daniel Sandersac272632014-05-23 13:18:02 +00003820 Shamt = (RegSizeInBytes - (TotalBytesLoaded + LoadSizeInBytes)) * 8;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003821
3822 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003823 DAG.getConstant(Shamt, DL, MVT::i32));
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003824
3825 if (Val.getNode())
3826 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
3827 else
3828 Val = Shift;
3829
Daniel Sandersac272632014-05-23 13:18:02 +00003830 OffsetInBytes += LoadSizeInBytes;
3831 TotalBytesLoaded += LoadSizeInBytes;
3832 Alignment = std::min(Alignment, LoadSizeInBytes);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003833 }
3834
Daniel Sanders23e98772014-11-02 16:09:29 +00003835 unsigned ArgReg = ArgRegs[FirstReg + I];
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003836 RegsToPass.push_back(std::make_pair(ArgReg, Val));
3837 return;
3838 }
3839 }
3840
3841 // Copy remainder of byval arg to it with memcpy.
Daniel Sandersac272632014-05-23 13:18:02 +00003842 unsigned MemCpySize = ByValSizeInBytes - OffsetInBytes;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003843 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003844 DAG.getConstant(OffsetInBytes, DL, PtrTy));
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003845 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003846 DAG.getIntPtrConstant(VA.getLocMemOffset(), DL));
3847 Chain = DAG.getMemcpy(Chain, DL, Dst, Src,
3848 DAG.getConstant(MemCpySize, DL, PtrTy),
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003849 Alignment, /*isVolatile=*/false, /*AlwaysInline=*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00003850 /*isTailCall=*/false,
Nick Lewyckyaad475b2014-04-15 07:22:52 +00003851 MachinePointerInfo(), MachinePointerInfo());
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003852 MemOpChains.push_back(Chain);
3853}
Akira Hatanaka2a134022012-10-27 00:21:13 +00003854
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003855void MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003856 SDValue Chain, const SDLoc &DL,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003857 SelectionDAG &DAG,
Daniel Sanders853c2432014-11-01 18:13:52 +00003858 CCState &State) const {
Craig Topper862d5d82015-09-28 00:15:34 +00003859 ArrayRef<MCPhysReg> ArgRegs = ABI.GetVarArgRegs();
Tim Northover3b6b7ca2015-02-21 02:11:17 +00003860 unsigned Idx = State.getFirstUnallocated(ArgRegs);
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003861 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
3862 MVT RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003863 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3864 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00003865 MachineFrameInfo &MFI = MF.getFrameInfo();
Akira Hatanaka2a134022012-10-27 00:21:13 +00003866 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3867
3868 // Offset of the first variable argument from stack pointer.
3869 int VaArgOffset;
3870
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003871 if (ArgRegs.size() == Idx)
Rui Ueyamada00f2f2016-01-14 21:06:47 +00003872 VaArgOffset = alignTo(State.getNextStackOffset(), RegSizeInBytes);
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003873 else {
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003874 VaArgOffset =
3875 (int)ABI.GetCalleeAllocdArgSizeInBytes(State.getCallingConv()) -
3876 (int)(RegSizeInBytes * (ArgRegs.size() - Idx));
3877 }
Akira Hatanaka2a134022012-10-27 00:21:13 +00003878
3879 // Record the frame index of the first variable argument
3880 // which is a value necessary to VASTART.
Matthias Braun941a7052016-07-28 18:40:00 +00003881 int FI = MFI.CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003882 MipsFI->setVarArgsFrameIndex(FI);
3883
3884 // Copy the integer registers that have not been used for argument passing
3885 // to the argument register save area. For O32, the save area is allocated
3886 // in the caller's stack frame, while for N32/64, it is allocated in the
3887 // callee's stack frame.
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003888 for (unsigned I = Idx; I < ArgRegs.size();
3889 ++I, VaArgOffset += RegSizeInBytes) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003890 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003891 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
Matthias Braun941a7052016-07-28 18:40:00 +00003892 FI = MFI.CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
Mehdi Amini44ede332015-07-09 02:09:04 +00003893 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Justin Lebar9c375812016-07-15 18:27:10 +00003894 SDValue Store =
3895 DAG.getStore(Chain, DL, ArgValue, PtrOff, MachinePointerInfo());
Eric Christopher1c29a652014-07-18 22:55:25 +00003896 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(
3897 (Value *)nullptr);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003898 OutChains.push_back(Store);
3899 }
3900}
Daniel Sanders23e98772014-11-02 16:09:29 +00003901
3902void MipsTargetLowering::HandleByVal(CCState *State, unsigned &Size,
3903 unsigned Align) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003904 const TargetFrameLowering *TFL = Subtarget.getFrameLowering();
Daniel Sanders23e98772014-11-02 16:09:29 +00003905
3906 assert(Size && "Byval argument's size shouldn't be 0.");
3907
3908 Align = std::min(Align, TFL->getStackAlignment());
3909
3910 unsigned FirstReg = 0;
3911 unsigned NumRegs = 0;
3912
3913 if (State->getCallingConv() != CallingConv::Fast) {
3914 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
Craig Topper862d5d82015-09-28 00:15:34 +00003915 ArrayRef<MCPhysReg> IntArgRegs = ABI.GetByValArgRegs();
Daniel Sanders23e98772014-11-02 16:09:29 +00003916 // FIXME: The O32 case actually describes no shadow registers.
3917 const MCPhysReg *ShadowRegs =
Eric Christopher96e72c62015-01-29 23:27:36 +00003918 ABI.IsO32() ? IntArgRegs.data() : Mips64DPRegs;
Daniel Sanders23e98772014-11-02 16:09:29 +00003919
3920 // We used to check the size as well but we can't do that anymore since
3921 // CCState::HandleByVal() rounds up the size after calling this function.
3922 assert(!(Align % RegSizeInBytes) &&
3923 "Byval argument's alignment should be a multiple of"
3924 "RegSizeInBytes.");
3925
Tim Northover3b6b7ca2015-02-21 02:11:17 +00003926 FirstReg = State->getFirstUnallocated(IntArgRegs);
Daniel Sanders23e98772014-11-02 16:09:29 +00003927
3928 // If Align > RegSizeInBytes, the first arg register must be even.
3929 // FIXME: This condition happens to do the right thing but it's not the
3930 // right way to test it. We want to check that the stack frame offset
3931 // of the register is aligned.
3932 if ((Align > RegSizeInBytes) && (FirstReg % 2)) {
3933 State->AllocateReg(IntArgRegs[FirstReg], ShadowRegs[FirstReg]);
3934 ++FirstReg;
3935 }
3936
3937 // Mark the registers allocated.
Rui Ueyamada00f2f2016-01-14 21:06:47 +00003938 Size = alignTo(Size, RegSizeInBytes);
Daniel Sanders23e98772014-11-02 16:09:29 +00003939 for (unsigned I = FirstReg; Size > 0 && (I < IntArgRegs.size());
3940 Size -= RegSizeInBytes, ++I, ++NumRegs)
3941 State->AllocateReg(IntArgRegs[I], ShadowRegs[I]);
3942 }
3943
3944 State->addInRegsParamInfo(FirstReg, FirstReg + NumRegs);
3945}
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00003946
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003947MachineBasicBlock *MipsTargetLowering::emitPseudoSELECT(MachineInstr &MI,
3948 MachineBasicBlock *BB,
3949 bool isFPCmp,
3950 unsigned Opc) const {
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00003951 assert(!(Subtarget.hasMips4() || Subtarget.hasMips32()) &&
3952 "Subtarget already supports SELECT nodes with the use of"
3953 "conditional-move instructions.");
3954
3955 const TargetInstrInfo *TII =
Eric Christopher96e72c62015-01-29 23:27:36 +00003956 Subtarget.getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003957 DebugLoc DL = MI.getDebugLoc();
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00003958
3959 // To "insert" a SELECT instruction, we actually have to insert the
3960 // diamond control-flow pattern. The incoming instruction knows the
3961 // destination vreg to set, the condition code register to branch on, the
3962 // true/false values to select between, and a branch opcode to use.
3963 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +00003964 MachineFunction::iterator It = ++BB->getIterator();
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00003965
3966 // thisMBB:
3967 // ...
3968 // TrueVal = ...
3969 // setcc r1, r2, r3
3970 // bNE r1, r0, copy1MBB
3971 // fallthrough --> copy0MBB
3972 MachineBasicBlock *thisMBB = BB;
3973 MachineFunction *F = BB->getParent();
3974 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
3975 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
3976 F->insert(It, copy0MBB);
3977 F->insert(It, sinkMBB);
3978
3979 // Transfer the remainder of BB and its successor edges to sinkMBB.
3980 sinkMBB->splice(sinkMBB->begin(), BB,
3981 std::next(MachineBasicBlock::iterator(MI)), BB->end());
3982 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
3983
3984 // Next, add the true and fallthrough blocks as its successors.
3985 BB->addSuccessor(copy0MBB);
3986 BB->addSuccessor(sinkMBB);
3987
3988 if (isFPCmp) {
3989 // bc1[tf] cc, sinkMBB
3990 BuildMI(BB, DL, TII->get(Opc))
Simon Dardisba92b032016-09-09 11:06:01 +00003991 .addReg(MI.getOperand(1).getReg())
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003992 .addMBB(sinkMBB);
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00003993 } else {
3994 // bne rs, $0, sinkMBB
3995 BuildMI(BB, DL, TII->get(Opc))
Simon Dardisba92b032016-09-09 11:06:01 +00003996 .addReg(MI.getOperand(1).getReg())
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003997 .addReg(Mips::ZERO)
3998 .addMBB(sinkMBB);
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00003999 }
4000
4001 // copy0MBB:
4002 // %FalseValue = ...
4003 // # fallthrough to sinkMBB
4004 BB = copy0MBB;
4005
4006 // Update machine-CFG edges
4007 BB->addSuccessor(sinkMBB);
4008
4009 // sinkMBB:
4010 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
4011 // ...
4012 BB = sinkMBB;
4013
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004014 BuildMI(*BB, BB->begin(), DL, TII->get(Mips::PHI), MI.getOperand(0).getReg())
Simon Dardisba92b032016-09-09 11:06:01 +00004015 .addReg(MI.getOperand(2).getReg())
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004016 .addMBB(thisMBB)
4017 .addReg(MI.getOperand(3).getReg())
4018 .addMBB(copy0MBB);
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00004019
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004020 MI.eraseFromParent(); // The pseudo instruction is gone now.
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00004021
4022 return BB;
4023}
Daniel Sanders1440bb22015-01-09 17:21:30 +00004024
4025// FIXME? Maybe this could be a TableGen attribute on some registers and
4026// this table could be generated automatically from RegInfo.
Pat Gavlina717f252015-07-09 17:40:29 +00004027unsigned MipsTargetLowering::getRegisterByName(const char* RegName, EVT VT,
4028 SelectionDAG &DAG) const {
Daniel Sanders1440bb22015-01-09 17:21:30 +00004029 // Named registers is expected to be fairly rare. For now, just support $28
4030 // since the linux kernel uses it.
4031 if (Subtarget.isGP64bit()) {
4032 unsigned Reg = StringSwitch<unsigned>(RegName)
4033 .Case("$28", Mips::GP_64)
4034 .Default(0);
4035 if (Reg)
4036 return Reg;
4037 } else {
4038 unsigned Reg = StringSwitch<unsigned>(RegName)
4039 .Case("$28", Mips::GP)
4040 .Default(0);
4041 if (Reg)
4042 return Reg;
4043 }
4044 report_fatal_error("Invalid register name global variable");
4045}