blob: a1fb0275277062ec2e60747908dd0fee832c3504 [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);
277 setOperationAction(ISD::SELECT, MVT::f32, Custom);
278 setOperationAction(ISD::SELECT, MVT::f64, Custom);
279 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Akira Hatanakab7f78592012-03-09 23:46:03 +0000280 setOperationAction(ISD::SETCC, MVT::f32, Custom);
281 setOperationAction(ISD::SETCC, MVT::f64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000282 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);
293 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
Akira Hatanaka28e02ec2012-11-07 19:10:58 +0000308 setOperationAction(ISD::ADD, MVT::i32, Custom);
Eric Christopher1c29a652014-07-18 22:55:25 +0000309 if (Subtarget.isGP64bit())
Akira Hatanaka28e02ec2012-11-07 19:10:58 +0000310 setOperationAction(ISD::ADD, MVT::i64, Custom);
311
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
Eric Christopher1c29a652014-07-18 22:55:25 +0000429 setMinFunctionAlignment(Subtarget.isGP64bit() ? 3 : 2);
Eli Friedman2518f832011-05-06 20:34:06 +0000430
Daniel Sanders2b553d42014-08-01 09:17:39 +0000431 // The arguments on the stack are defined in terms of 4-byte slots on O32
432 // and 8-byte slots on N32/N64.
Eric Christopher96e72c62015-01-29 23:27:36 +0000433 setMinStackArgumentAlignment((ABI.IsN32() || ABI.IsN64()) ? 8 : 4);
Daniel Sanders2b553d42014-08-01 09:17:39 +0000434
Eric Christopher96e72c62015-01-29 23:27:36 +0000435 setStackPointerRegisterToSaveRestore(ABI.IsN64() ? Mips::SP_64 : Mips::SP);
Akira Hatanakaaa560002011-05-26 18:59:03 +0000436
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000437 MaxStoresPerMemcpy = 16;
Zoran Jovanovicff9d5f32013-12-19 16:12:56 +0000438
Eric Christopher1c29a652014-07-18 22:55:25 +0000439 isMicroMips = Subtarget.inMicroMipsMode();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000440}
441
Eric Christopherb1526602014-09-19 23:30:42 +0000442const MipsTargetLowering *MipsTargetLowering::create(const MipsTargetMachine &TM,
Eric Christopher8924d272014-07-18 23:25:04 +0000443 const MipsSubtarget &STI) {
444 if (STI.inMips16Mode())
445 return llvm::createMips16TargetLowering(TM, STI);
Jia Liuf54f60f2012-02-28 07:46:26 +0000446
Eric Christopher8924d272014-07-18 23:25:04 +0000447 return llvm::createMipsSETargetLowering(TM, STI);
Akira Hatanaka2fcc1cf2011-08-12 21:30:06 +0000448}
449
Reed Kotler720c5ca2014-04-17 22:15:34 +0000450// Create a fast isel object.
451FastISel *
452MipsTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
453 const TargetLibraryInfo *libInfo) const {
Vasileios Kalintiris2041b1d2015-07-30 12:39:33 +0000454 if (!funcInfo.MF->getTarget().Options.EnableFastISel)
Reed Kotler720c5ca2014-04-17 22:15:34 +0000455 return TargetLowering::createFastISel(funcInfo, libInfo);
456 return Mips::createFastISel(funcInfo, libInfo);
457}
458
Mehdi Amini44ede332015-07-09 02:09:04 +0000459EVT MipsTargetLowering::getSetCCResultType(const DataLayout &, LLVMContext &,
460 EVT VT) const {
Vasileios Kalintiris36901dd2016-03-01 20:25:43 +0000461 if (!VT.isVector())
462 return MVT::i32;
463 return VT.changeVectorElementTypeToInteger();
Scott Michela6729e82008-03-10 15:42:14 +0000464}
465
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000466static SDValue performDivRemCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000467 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000468 const MipsSubtarget &Subtarget) {
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000469 if (DCI.isBeforeLegalizeOps())
470 return SDValue();
471
Akira Hatanakab1538f92011-10-03 21:06:13 +0000472 EVT Ty = N->getValueType(0);
Akira Hatanaka8002a3f2013-08-14 00:47:08 +0000473 unsigned LO = (Ty == MVT::i32) ? Mips::LO0 : Mips::LO0_64;
474 unsigned HI = (Ty == MVT::i32) ? Mips::HI0 : Mips::HI0_64;
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000475 unsigned Opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem16 :
476 MipsISD::DivRemU16;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000477 SDLoc DL(N);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000478
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000479 SDValue DivRem = DAG.getNode(Opc, DL, MVT::Glue,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000480 N->getOperand(0), N->getOperand(1));
481 SDValue InChain = DAG.getEntryNode();
482 SDValue InGlue = DivRem;
483
484 // insert MFLO
485 if (N->hasAnyUseOfValue(0)) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000486 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, DL, LO, Ty,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000487 InGlue);
488 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
489 InChain = CopyFromLo.getValue(1);
490 InGlue = CopyFromLo.getValue(2);
491 }
492
493 // insert MFHI
494 if (N->hasAnyUseOfValue(1)) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000495 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, DL,
Akira Hatanakab1538f92011-10-03 21:06:13 +0000496 HI, Ty, InGlue);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000497 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
498 }
499
500 return SDValue();
501}
502
Akira Hatanaka89af5892013-04-18 01:00:46 +0000503static Mips::CondCode condCodeToFCC(ISD::CondCode CC) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000504 switch (CC) {
505 default: llvm_unreachable("Unknown fp condition code!");
506 case ISD::SETEQ:
507 case ISD::SETOEQ: return Mips::FCOND_OEQ;
508 case ISD::SETUNE: return Mips::FCOND_UNE;
509 case ISD::SETLT:
510 case ISD::SETOLT: return Mips::FCOND_OLT;
511 case ISD::SETGT:
512 case ISD::SETOGT: return Mips::FCOND_OGT;
513 case ISD::SETLE:
514 case ISD::SETOLE: return Mips::FCOND_OLE;
515 case ISD::SETGE:
516 case ISD::SETOGE: return Mips::FCOND_OGE;
517 case ISD::SETULT: return Mips::FCOND_ULT;
518 case ISD::SETULE: return Mips::FCOND_ULE;
519 case ISD::SETUGT: return Mips::FCOND_UGT;
520 case ISD::SETUGE: return Mips::FCOND_UGE;
521 case ISD::SETUO: return Mips::FCOND_UN;
522 case ISD::SETO: return Mips::FCOND_OR;
523 case ISD::SETNE:
524 case ISD::SETONE: return Mips::FCOND_ONE;
525 case ISD::SETUEQ: return Mips::FCOND_UEQ;
526 }
527}
528
529
Akira Hatanakaf0ea5002013-03-30 01:16:38 +0000530/// This function returns true if the floating point conditional branches and
531/// conditional moves which use condition code CC should be inverted.
532static bool invertFPCondCodeUser(Mips::CondCode CC) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000533 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
534 return false;
535
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000536 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
537 "Illegal Condition Code");
Akira Hatanakaa5352702011-03-31 18:26:17 +0000538
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000539 return true;
Akira Hatanakaa5352702011-03-31 18:26:17 +0000540}
541
542// Creates and returns an FPCmp node from a setcc node.
543// Returns Op if setcc is not a floating point comparison.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000544static SDValue createFPCmp(SelectionDAG &DAG, const SDValue &Op) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000545 // must be a SETCC node
546 if (Op.getOpcode() != ISD::SETCC)
547 return Op;
548
549 SDValue LHS = Op.getOperand(0);
550
551 if (!LHS.getValueType().isFloatingPoint())
552 return Op;
553
554 SDValue RHS = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000555 SDLoc DL(Op);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000556
Akira Hatanakaaef55c82011-04-15 21:00:26 +0000557 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
558 // node if necessary.
Akira Hatanakaa5352702011-03-31 18:26:17 +0000559 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
560
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000561 return DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000562 DAG.getConstant(condCodeToFCC(CC), DL, MVT::i32));
Akira Hatanakaa5352702011-03-31 18:26:17 +0000563}
564
565// Creates and returns a CMovFPT/F node.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000566static SDValue createCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000567 SDValue False, const SDLoc &DL) {
Akira Hatanakaf0ea5002013-03-30 01:16:38 +0000568 ConstantSDNode *CC = cast<ConstantSDNode>(Cond.getOperand(2));
569 bool invert = invertFPCondCodeUser((Mips::CondCode)CC->getSExtValue());
Akira Hatanaka8bce21c2013-07-26 20:51:20 +0000570 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000571
572 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
Akira Hatanaka8bce21c2013-07-26 20:51:20 +0000573 True.getValueType(), True, FCC0, False, Cond);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000574}
575
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000576static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000577 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000578 const MipsSubtarget &Subtarget) {
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000579 if (DCI.isBeforeLegalizeOps())
580 return SDValue();
581
582 SDValue SetCC = N->getOperand(0);
583
584 if ((SetCC.getOpcode() != ISD::SETCC) ||
585 !SetCC.getOperand(0).getValueType().isInteger())
586 return SDValue();
587
588 SDValue False = N->getOperand(2);
589 EVT FalseTy = False.getValueType();
590
591 if (!FalseTy.isInteger())
592 return SDValue();
593
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000594 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(False);
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000595
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000596 // If the RHS (False) is 0, we swap the order of the operands
597 // of ISD::SELECT (obviously also inverting the condition) so that we can
598 // take advantage of conditional moves using the $0 register.
599 // Example:
600 // return (a != 0) ? x : 0;
601 // load $reg, x
602 // movz $reg, $0, a
603 if (!FalseC)
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000604 return SDValue();
605
Andrew Trickef9de2a2013-05-25 02:42:55 +0000606 const SDLoc DL(N);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000607
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000608 if (!FalseC->getZExtValue()) {
609 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
610 SDValue True = N->getOperand(1);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000611
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000612 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
613 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
614
615 return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True);
616 }
617
Matheus Almeidaa6beac12013-12-05 12:07:05 +0000618 // If both operands are integer constants there's a possibility that we
619 // can do some interesting optimizations.
620 SDValue True = N->getOperand(1);
621 ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(True);
622
623 if (!TrueC || !True.getValueType().isInteger())
624 return SDValue();
625
Vasileios Kalintiris36901dd2016-03-01 20:25:43 +0000626 // We'll also ignore MVT::i64 operands as this optimizations proves
627 // to be ineffective because of the required sign extensions as the result
628 // of a SETCC operator is always MVT::i32 for non-vector types.
629 if (True.getValueType() == MVT::i64)
630 return SDValue();
631
Matheus Almeidaa6beac12013-12-05 12:07:05 +0000632 int64_t Diff = TrueC->getSExtValue() - FalseC->getSExtValue();
633
634 // 1) (a < x) ? y : y-1
635 // slti $reg1, a, x
636 // addiu $reg2, $reg1, y-1
Vasileios Kalintiris36901dd2016-03-01 20:25:43 +0000637 if (Diff == 1)
638 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, False);
Matheus Almeidaa6beac12013-12-05 12:07:05 +0000639
640 // 2) (a < x) ? y-1 : y
641 // slti $reg1, a, x
642 // xor $reg1, $reg1, 1
643 // addiu $reg2, $reg1, y-1
644 if (Diff == -1) {
645 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
646 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
647 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
648 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, True);
649 }
650
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000651 // Couldn't optimize.
652 return SDValue();
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000653}
654
Vasileios Kalintirise741eb22015-03-02 12:47:32 +0000655static SDValue performCMovFPCombine(SDNode *N, SelectionDAG &DAG,
656 TargetLowering::DAGCombinerInfo &DCI,
657 const MipsSubtarget &Subtarget) {
658 if (DCI.isBeforeLegalizeOps())
659 return SDValue();
660
661 SDValue ValueIfTrue = N->getOperand(0), ValueIfFalse = N->getOperand(2);
662
663 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(ValueIfFalse);
664 if (!FalseC || FalseC->getZExtValue())
665 return SDValue();
666
667 // Since RHS (False) is 0, we swap the order of the True/False operands
668 // (obviously also inverting the condition) so that we can
669 // take advantage of conditional moves using the $0 register.
670 // Example:
671 // return (a != 0) ? x : 0;
672 // load $reg, x
673 // movz $reg, $0, a
674 unsigned Opc = (N->getOpcode() == MipsISD::CMovFP_T) ? MipsISD::CMovFP_F :
675 MipsISD::CMovFP_T;
676
677 SDValue FCC = N->getOperand(1), Glue = N->getOperand(3);
Vasileios Kalintiris2ef28882015-03-04 12:10:18 +0000678 return DAG.getNode(Opc, SDLoc(N), ValueIfFalse.getValueType(),
679 ValueIfFalse, FCC, ValueIfTrue, Glue);
Vasileios Kalintirise741eb22015-03-02 12:47:32 +0000680}
681
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000682static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000683 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000684 const MipsSubtarget &Subtarget) {
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000685 // Pattern match EXT.
686 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
687 // => ext $dst, $src, size, pos
Eric Christopher1c29a652014-07-18 22:55:25 +0000688 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasExtractInsert())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000689 return SDValue();
690
691 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000692 unsigned ShiftRightOpc = ShiftRight.getOpcode();
693
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000694 // Op's first operand must be a shift right.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000695 if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL)
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000696 return SDValue();
697
698 // The second operand of the shift must be an immediate.
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000699 ConstantSDNode *CN;
700 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
701 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000702
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000703 uint64_t Pos = CN->getZExtValue();
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000704 uint64_t SMPos, SMSize;
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000705
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000706 // Op's second operand must be a shifted mask.
707 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000708 !isShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000709 return SDValue();
710
711 // Return if the shifted mask does not start at bit 0 or the sum of its size
712 // and Pos exceeds the word's size.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000713 EVT ValTy = N->getValueType(0);
714 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000715 return SDValue();
716
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000717 SDLoc DL(N);
718 return DAG.getNode(MipsISD::Ext, DL, ValTy,
719 ShiftRight.getOperand(0),
720 DAG.getConstant(Pos, DL, MVT::i32),
721 DAG.getConstant(SMSize, DL, MVT::i32));
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000722}
Jia Liuf54f60f2012-02-28 07:46:26 +0000723
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000724static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000725 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000726 const MipsSubtarget &Subtarget) {
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000727 // Pattern match INS.
728 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
Jia Liuf54f60f2012-02-28 07:46:26 +0000729 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000730 // => ins $dst, $src, size, pos, $src1
Eric Christopher1c29a652014-07-18 22:55:25 +0000731 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasExtractInsert())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000732 return SDValue();
733
734 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
735 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
736 ConstantSDNode *CN;
737
738 // See if Op's first operand matches (and $src1 , mask0).
739 if (And0.getOpcode() != ISD::AND)
740 return SDValue();
741
742 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000743 !isShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000744 return SDValue();
745
746 // See if Op's second operand matches (and (shl $src, pos), mask1).
747 if (And1.getOpcode() != ISD::AND)
748 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000749
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000750 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000751 !isShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000752 return SDValue();
753
754 // The shift masks must have the same position and size.
755 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
756 return SDValue();
757
758 SDValue Shl = And1.getOperand(0);
759 if (Shl.getOpcode() != ISD::SHL)
760 return SDValue();
761
762 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
763 return SDValue();
764
765 unsigned Shamt = CN->getZExtValue();
766
767 // Return if the shift amount and the first bit position of mask are not the
Jia Liuf54f60f2012-02-28 07:46:26 +0000768 // same.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000769 EVT ValTy = N->getValueType(0);
770 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000771 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000772
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000773 SDLoc DL(N);
774 return DAG.getNode(MipsISD::Ins, DL, ValTy, Shl.getOperand(0),
775 DAG.getConstant(SMPos0, DL, MVT::i32),
776 DAG.getConstant(SMSize0, DL, MVT::i32),
777 And0.getOperand(0));
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000778}
Jia Liuf54f60f2012-02-28 07:46:26 +0000779
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000780static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000781 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000782 const MipsSubtarget &Subtarget) {
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000783 // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt))
784
785 if (DCI.isBeforeLegalizeOps())
786 return SDValue();
787
788 SDValue Add = N->getOperand(1);
789
790 if (Add.getOpcode() != ISD::ADD)
791 return SDValue();
792
793 SDValue Lo = Add.getOperand(1);
794
795 if ((Lo.getOpcode() != MipsISD::Lo) ||
796 (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable))
797 return SDValue();
798
799 EVT ValTy = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000800 SDLoc DL(N);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000801
802 SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, N->getOperand(0),
803 Add.getOperand(0));
804 return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo);
805}
806
Vasileios Kalintiris3751d412016-04-13 15:07:45 +0000807static SDValue performAssertZextCombine(SDNode *N, SelectionDAG &DAG,
808 TargetLowering::DAGCombinerInfo &DCI,
809 const MipsSubtarget &Subtarget) {
810 SDValue N0 = N->getOperand(0);
811 EVT NarrowerVT = cast<VTSDNode>(N->getOperand(1))->getVT();
812
813 if (N0.getOpcode() != ISD::TRUNCATE)
814 return SDValue();
815
816 if (N0.getOperand(0).getOpcode() != ISD::AssertZext)
817 return SDValue();
818
819 // fold (AssertZext (trunc (AssertZext x))) -> (trunc (AssertZext x))
820 // if the type of the extension of the innermost AssertZext node is
821 // smaller from that of the outermost node, eg:
822 // (AssertZext:i32 (trunc:i32 (AssertZext:i64 X, i32)), i8)
823 // -> (trunc:i32 (AssertZext X, i8))
824 SDValue WiderAssertZext = N0.getOperand(0);
825 EVT WiderVT = cast<VTSDNode>(WiderAssertZext->getOperand(1))->getVT();
826
827 if (NarrowerVT.bitsLT(WiderVT)) {
828 SDValue NewAssertZext = DAG.getNode(
829 ISD::AssertZext, SDLoc(N), WiderAssertZext.getValueType(),
830 WiderAssertZext.getOperand(0), DAG.getValueType(NarrowerVT));
831 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0),
832 NewAssertZext);
833 }
834
835 return SDValue();
836}
837
Bruno Cardoso Lopes61a61e92011-02-10 18:05:10 +0000838SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000839 const {
840 SelectionDAG &DAG = DCI.DAG;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000841 unsigned Opc = N->getOpcode();
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000842
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000843 switch (Opc) {
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000844 default: break;
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000845 case ISD::SDIVREM:
846 case ISD::UDIVREM:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000847 return performDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000848 case ISD::SELECT:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000849 return performSELECTCombine(N, DAG, DCI, Subtarget);
Vasileios Kalintirise741eb22015-03-02 12:47:32 +0000850 case MipsISD::CMovFP_F:
851 case MipsISD::CMovFP_T:
852 return performCMovFPCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000853 case ISD::AND:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000854 return performANDCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000855 case ISD::OR:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000856 return performORCombine(N, DAG, DCI, Subtarget);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000857 case ISD::ADD:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000858 return performADDCombine(N, DAG, DCI, Subtarget);
Vasileios Kalintiris3751d412016-04-13 15:07:45 +0000859 case ISD::AssertZext:
860 return performAssertZextCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000861 }
862
863 return SDValue();
864}
865
Sanjay Patelf7401292015-11-11 17:24:56 +0000866bool MipsTargetLowering::isCheapToSpeculateCttz() const {
867 return Subtarget.hasMips32();
868}
869
870bool MipsTargetLowering::isCheapToSpeculateCtlz() const {
871 return Subtarget.hasMips32();
872}
873
Akira Hatanakafabb8cf2012-09-21 23:58:31 +0000874void
875MipsTargetLowering::LowerOperationWrapper(SDNode *N,
876 SmallVectorImpl<SDValue> &Results,
877 SelectionDAG &DAG) const {
878 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
879
880 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
881 Results.push_back(Res.getValue(I));
882}
883
884void
885MipsTargetLowering::ReplaceNodeResults(SDNode *N,
886 SmallVectorImpl<SDValue> &Results,
887 SelectionDAG &DAG) const {
Akira Hatanaka9da442f2013-04-30 21:17:07 +0000888 return LowerOperationWrapper(N, Results, DAG);
Akira Hatanakafabb8cf2012-09-21 23:58:31 +0000889}
890
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000891SDValue MipsTargetLowering::
Dan Gohman21cea8a2010-04-17 15:26:15 +0000892LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000893{
Wesley Peck527da1b2010-11-23 03:31:01 +0000894 switch (Op.getOpcode())
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000895 {
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000896 case ISD::BR_JT: return lowerBR_JT(Op, DAG);
897 case ISD::BRCOND: return lowerBRCOND(Op, DAG);
898 case ISD::ConstantPool: return lowerConstantPool(Op, DAG);
899 case ISD::GlobalAddress: return lowerGlobalAddress(Op, DAG);
900 case ISD::BlockAddress: return lowerBlockAddress(Op, DAG);
901 case ISD::GlobalTLSAddress: return lowerGlobalTLSAddress(Op, DAG);
902 case ISD::JumpTable: return lowerJumpTable(Op, DAG);
903 case ISD::SELECT: return lowerSELECT(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000904 case ISD::SETCC: return lowerSETCC(Op, DAG);
905 case ISD::VASTART: return lowerVASTART(Op, DAG);
Daniel Sanders2b553d42014-08-01 09:17:39 +0000906 case ISD::VAARG: return lowerVAARG(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000907 case ISD::FCOPYSIGN: return lowerFCOPYSIGN(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000908 case ISD::FRAMEADDR: return lowerFRAMEADDR(Op, DAG);
909 case ISD::RETURNADDR: return lowerRETURNADDR(Op, DAG);
910 case ISD::EH_RETURN: return lowerEH_RETURN(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000911 case ISD::ATOMIC_FENCE: return lowerATOMIC_FENCE(Op, DAG);
912 case ISD::SHL_PARTS: return lowerShiftLeftParts(Op, DAG);
913 case ISD::SRA_PARTS: return lowerShiftRightParts(Op, DAG, true);
914 case ISD::SRL_PARTS: return lowerShiftRightParts(Op, DAG, false);
915 case ISD::LOAD: return lowerLOAD(Op, DAG);
916 case ISD::STORE: return lowerSTORE(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000917 case ISD::ADD: return lowerADD(Op, DAG);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000918 case ISD::FP_TO_SINT: return lowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000919 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000920 return SDValue();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000921}
922
Akira Hatanakae2489122011-04-15 21:51:11 +0000923//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000924// Lower helper functions
Akira Hatanakae2489122011-04-15 21:51:11 +0000925//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000926
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000927// addLiveIn - This helper function adds the specified physical register to the
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000928// MachineFunction as a live in value. It also creates a corresponding
929// virtual register for it.
930static unsigned
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000931addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000932{
Chris Lattnera10fff52007-12-31 04:13:23 +0000933 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
934 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000935 return VReg;
936}
937
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000938static MachineBasicBlock *insertDivByZeroTrap(MachineInstr &MI,
Daniel Sanders308181e2014-06-12 10:44:10 +0000939 MachineBasicBlock &MBB,
940 const TargetInstrInfo &TII,
Zlatko Buljan58d6a952016-04-13 08:02:26 +0000941 bool Is64Bit, bool IsMicroMips) {
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000942 if (NoZeroDivCheck)
943 return &MBB;
944
945 // Insert instruction "teq $divisor_reg, $zero, 7".
946 MachineBasicBlock::iterator I(MI);
947 MachineInstrBuilder MIB;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000948 MachineOperand &Divisor = MI.getOperand(2);
949 MIB = BuildMI(MBB, std::next(I), MI.getDebugLoc(),
Zlatko Buljan58d6a952016-04-13 08:02:26 +0000950 TII.get(IsMicroMips ? Mips::TEQ_MM : Mips::TEQ))
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000951 .addReg(Divisor.getReg(), getKillRegState(Divisor.isKill()))
952 .addReg(Mips::ZERO)
953 .addImm(7);
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000954
955 // Use the 32-bit sub-register if this is a 64-bit division.
956 if (Is64Bit)
957 MIB->getOperand(0).setSubReg(Mips::sub_32);
958
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000959 // Clear Divisor's kill flag.
960 Divisor.setIsKill(false);
Daniel Sanders308181e2014-06-12 10:44:10 +0000961
962 // We would normally delete the original instruction here but in this case
963 // we only needed to inject an additional instruction rather than replace it.
964
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000965 return &MBB;
966}
967
Akira Hatanakae4bd0542012-09-27 02:15:57 +0000968MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000969MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
Dan Gohman25c16532010-05-01 00:01:06 +0000970 MachineBasicBlock *BB) const {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000971 switch (MI.getOpcode()) {
Reed Kotler97ba5f22013-02-21 04:22:38 +0000972 default:
973 llvm_unreachable("Unexpected instr type to insert");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000974 case Mips::ATOMIC_LOAD_ADD_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000975 return emitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000976 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000977 return emitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000978 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000979 return emitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000980 case Mips::ATOMIC_LOAD_ADD_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000981 return emitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000982
983 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000984 return emitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000985 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000986 return emitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000987 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000988 return emitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000989 case Mips::ATOMIC_LOAD_AND_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000990 return emitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000991
992 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000993 return emitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000994 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000995 return emitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000996 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000997 return emitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000998 case Mips::ATOMIC_LOAD_OR_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000999 return emitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001000
1001 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001002 return emitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001003 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001004 return emitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001005 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001006 return emitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001007 case Mips::ATOMIC_LOAD_XOR_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001008 return emitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001009
1010 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001011 return emitAtomicBinaryPartword(MI, BB, 1, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001012 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001013 return emitAtomicBinaryPartword(MI, BB, 2, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001014 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001015 return emitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001016 case Mips::ATOMIC_LOAD_NAND_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001017 return emitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001018
1019 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001020 return emitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001021 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001022 return emitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001023 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001024 return emitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001025 case Mips::ATOMIC_LOAD_SUB_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001026 return emitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001027
1028 case Mips::ATOMIC_SWAP_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001029 return emitAtomicBinaryPartword(MI, BB, 1, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001030 case Mips::ATOMIC_SWAP_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001031 return emitAtomicBinaryPartword(MI, BB, 2, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001032 case Mips::ATOMIC_SWAP_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001033 return emitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001034 case Mips::ATOMIC_SWAP_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001035 return emitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001036
1037 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001038 return emitAtomicCmpSwapPartword(MI, BB, 1);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001039 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001040 return emitAtomicCmpSwapPartword(MI, BB, 2);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001041 case Mips::ATOMIC_CMP_SWAP_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001042 return emitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001043 case Mips::ATOMIC_CMP_SWAP_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001044 return emitAtomicCmpSwap(MI, BB, 8);
Akira Hatanaka1cb02422013-05-20 18:07:43 +00001045 case Mips::PseudoSDIV:
1046 case Mips::PseudoUDIV:
Daniel Sanders308181e2014-06-12 10:44:10 +00001047 case Mips::DIV:
1048 case Mips::DIVU:
1049 case Mips::MOD:
1050 case Mips::MODU:
Zlatko Buljan58d6a952016-04-13 08:02:26 +00001051 return insertDivByZeroTrap(MI, *BB, *Subtarget.getInstrInfo(), false,
1052 false);
1053 case Mips::SDIV_MM_Pseudo:
1054 case Mips::UDIV_MM_Pseudo:
1055 case Mips::SDIV_MM:
1056 case Mips::UDIV_MM:
1057 case Mips::DIV_MMR6:
1058 case Mips::DIVU_MMR6:
1059 case Mips::MOD_MMR6:
1060 case Mips::MODU_MMR6:
1061 return insertDivByZeroTrap(MI, *BB, *Subtarget.getInstrInfo(), false, true);
Akira Hatanaka1cb02422013-05-20 18:07:43 +00001062 case Mips::PseudoDSDIV:
1063 case Mips::PseudoDUDIV:
Daniel Sanders308181e2014-06-12 10:44:10 +00001064 case Mips::DDIV:
1065 case Mips::DDIVU:
1066 case Mips::DMOD:
1067 case Mips::DMODU:
Zlatko Buljan58d6a952016-04-13 08:02:26 +00001068 return insertDivByZeroTrap(MI, *BB, *Subtarget.getInstrInfo(), true, false);
1069 case Mips::DDIV_MM64R6:
1070 case Mips::DDIVU_MM64R6:
1071 case Mips::DMOD_MM64R6:
1072 case Mips::DMODU_MM64R6:
1073 return insertDivByZeroTrap(MI, *BB, *Subtarget.getInstrInfo(), true, true);
Daniel Sanders0fa60412014-06-12 13:39:06 +00001074 case Mips::SEL_D:
Zlatko Buljancd242c12016-06-09 11:15:53 +00001075 case Mips::SEL_D_MMR6:
Daniel Sanders0fa60412014-06-12 13:39:06 +00001076 return emitSEL_D(MI, BB);
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001077
1078 case Mips::PseudoSELECT_I:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001079 case Mips::PseudoSELECT_I64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001080 case Mips::PseudoSELECT_S:
1081 case Mips::PseudoSELECT_D32:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001082 case Mips::PseudoSELECT_D64:
Vasileios Kalintiris36901dd2016-03-01 20:25:43 +00001083 return emitPseudoSELECT(MI, BB, false, Mips::BNE);
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001084 case Mips::PseudoSELECTFP_F_I:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001085 case Mips::PseudoSELECTFP_F_I64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001086 case Mips::PseudoSELECTFP_F_S:
1087 case Mips::PseudoSELECTFP_F_D32:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001088 case Mips::PseudoSELECTFP_F_D64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001089 return emitPseudoSELECT(MI, BB, true, Mips::BC1F);
1090 case Mips::PseudoSELECTFP_T_I:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001091 case Mips::PseudoSELECTFP_T_I64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001092 case Mips::PseudoSELECTFP_T_S:
1093 case Mips::PseudoSELECTFP_T_D32:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001094 case Mips::PseudoSELECTFP_T_D64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001095 return emitPseudoSELECT(MI, BB, true, Mips::BC1T);
Akira Hatanakaa5352702011-03-31 18:26:17 +00001096 }
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001097}
1098
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001099// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
1100// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001101MachineBasicBlock *MipsTargetLowering::emitAtomicBinary(MachineInstr &MI,
1102 MachineBasicBlock *BB,
1103 unsigned Size,
1104 unsigned BinOpcode,
1105 bool Nand) const {
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001106 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001107
1108 MachineFunction *MF = BB->getParent();
1109 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001110 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Eric Christopher96e72c62015-01-29 23:27:36 +00001111 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001112 const bool ArePtrs64bit = ABI.ArePtrs64bit();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001113 DebugLoc DL = MI.getDebugLoc();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001114 unsigned LL, SC, AND, NOR, ZERO, BEQ;
1115
1116 if (Size == 4) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001117 if (isMicroMips) {
1118 LL = Mips::LL_MM;
1119 SC = Mips::SC_MM;
1120 } else {
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001121 LL = Subtarget.hasMips32r6()
1122 ? (ArePtrs64bit ? Mips::LL64_R6 : Mips::LL_R6)
1123 : (ArePtrs64bit ? Mips::LL64 : Mips::LL);
1124 SC = Subtarget.hasMips32r6()
1125 ? (ArePtrs64bit ? Mips::SC64_R6 : Mips::SC_R6)
1126 : (ArePtrs64bit ? Mips::SC64 : Mips::SC);
Daniel Sanders6a803f62014-06-16 13:13:03 +00001127 }
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001128
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001129 AND = Mips::AND;
1130 NOR = Mips::NOR;
1131 ZERO = Mips::ZERO;
1132 BEQ = Mips::BEQ;
Daniel Sanders6a803f62014-06-16 13:13:03 +00001133 } else {
Daniel Sandersbdcfab12014-07-24 09:47:14 +00001134 LL = Subtarget.hasMips64r6() ? Mips::LLD_R6 : Mips::LLD;
1135 SC = Subtarget.hasMips64r6() ? Mips::SCD_R6 : Mips::SCD;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001136 AND = Mips::AND64;
1137 NOR = Mips::NOR64;
1138 ZERO = Mips::ZERO_64;
1139 BEQ = Mips::BEQ64;
1140 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001141
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001142 unsigned OldVal = MI.getOperand(0).getReg();
1143 unsigned Ptr = MI.getOperand(1).getReg();
1144 unsigned Incr = MI.getOperand(2).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001145
Akira Hatanaka0e019592011-07-19 20:11:17 +00001146 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1147 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1148 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001149
1150 // insert new blocks after the current block
1151 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1152 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1153 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +00001154 MachineFunction::iterator It = ++BB->getIterator();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001155 MF->insert(It, loopMBB);
1156 MF->insert(It, exitMBB);
1157
1158 // Transfer the remainder of BB and its successor edges to exitMBB.
1159 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001160 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001161 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1162
1163 // thisMBB:
1164 // ...
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001165 // fallthrough --> loopMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001166 BB->addSuccessor(loopMBB);
Akira Hatanaka08636b42011-07-19 17:09:53 +00001167 loopMBB->addSuccessor(loopMBB);
1168 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001169
1170 // loopMBB:
1171 // ll oldval, 0(ptr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001172 // <binop> storeval, oldval, incr
1173 // sc success, storeval, 0(ptr)
1174 // beq success, $0, loopMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001175 BB = loopMBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001176 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001177 if (Nand) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001178 // and andres, oldval, incr
1179 // nor storeval, $0, andres
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001180 BuildMI(BB, DL, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
1181 BuildMI(BB, DL, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001182 } else if (BinOpcode) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001183 // <binop> storeval, oldval, incr
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001184 BuildMI(BB, DL, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001185 } else {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001186 StoreVal = Incr;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001187 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001188 BuildMI(BB, DL, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
1189 BuildMI(BB, DL, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001190
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001191 MI.eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001192
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001193 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001194}
1195
Daniel Sanders6a803f62014-06-16 13:13:03 +00001196MachineBasicBlock *MipsTargetLowering::emitSignExtendToI32InReg(
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001197 MachineInstr &MI, MachineBasicBlock *BB, unsigned Size, unsigned DstReg,
Daniel Sanders6a803f62014-06-16 13:13:03 +00001198 unsigned SrcReg) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00001199 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001200 const DebugLoc &DL = MI.getDebugLoc();
Daniel Sanders6a803f62014-06-16 13:13:03 +00001201
Eric Christopher1c29a652014-07-18 22:55:25 +00001202 if (Subtarget.hasMips32r2() && Size == 1) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001203 BuildMI(BB, DL, TII->get(Mips::SEB), DstReg).addReg(SrcReg);
1204 return BB;
1205 }
1206
Eric Christopher1c29a652014-07-18 22:55:25 +00001207 if (Subtarget.hasMips32r2() && Size == 2) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001208 BuildMI(BB, DL, TII->get(Mips::SEH), DstReg).addReg(SrcReg);
1209 return BB;
1210 }
1211
1212 MachineFunction *MF = BB->getParent();
1213 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1214 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1215 unsigned ScrReg = RegInfo.createVirtualRegister(RC);
1216
1217 assert(Size < 32);
1218 int64_t ShiftImm = 32 - (Size * 8);
1219
1220 BuildMI(BB, DL, TII->get(Mips::SLL), ScrReg).addReg(SrcReg).addImm(ShiftImm);
1221 BuildMI(BB, DL, TII->get(Mips::SRA), DstReg).addReg(ScrReg).addImm(ShiftImm);
1222
1223 return BB;
1224}
1225
1226MachineBasicBlock *MipsTargetLowering::emitAtomicBinaryPartword(
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001227 MachineInstr &MI, MachineBasicBlock *BB, unsigned Size, unsigned BinOpcode,
Daniel Sanders6a803f62014-06-16 13:13:03 +00001228 bool Nand) const {
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001229 assert((Size == 1 || Size == 2) &&
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001230 "Unsupported size for EmitAtomicBinaryPartial.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001231
1232 MachineFunction *MF = BB->getParent();
1233 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1234 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001235 const bool ArePtrs64bit = ABI.ArePtrs64bit();
Simon Dardisa2d8cc32016-04-28 16:26:43 +00001236 const TargetRegisterClass *RCp =
1237 getRegClassFor(ArePtrs64bit ? MVT::i64 : MVT::i32);
Eric Christopher96e72c62015-01-29 23:27:36 +00001238 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001239 DebugLoc DL = MI.getDebugLoc();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001240
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001241 unsigned Dest = MI.getOperand(0).getReg();
1242 unsigned Ptr = MI.getOperand(1).getReg();
1243 unsigned Incr = MI.getOperand(2).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001244
Simon Dardisa2d8cc32016-04-28 16:26:43 +00001245 unsigned AlignedAddr = RegInfo.createVirtualRegister(RCp);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001246 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001247 unsigned Mask = RegInfo.createVirtualRegister(RC);
1248 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001249 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1250 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001251 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Simon Dardisa2d8cc32016-04-28 16:26:43 +00001252 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RCp);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001253 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1254 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1255 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1256 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka9663dd32011-07-19 20:56:53 +00001257 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001258 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1259 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1260 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001261 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001262
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001263 unsigned LL, SC;
1264 if (isMicroMips) {
1265 LL = Mips::LL_MM;
1266 SC = Mips::SC_MM;
1267 } else {
1268 LL = Subtarget.hasMips32r6() ? (ArePtrs64bit ? Mips::LL64_R6 : Mips::LL_R6)
1269 : (ArePtrs64bit ? Mips::LL64 : Mips::LL);
1270 SC = Subtarget.hasMips32r6() ? (ArePtrs64bit ? Mips::SC64_R6 : Mips::SC_R6)
1271 : (ArePtrs64bit ? Mips::SC64 : Mips::SC);
1272 }
1273
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001274 // insert new blocks after the current block
1275 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1276 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001277 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001278 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +00001279 MachineFunction::iterator It = ++BB->getIterator();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001280 MF->insert(It, loopMBB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001281 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001282 MF->insert(It, exitMBB);
1283
1284 // Transfer the remainder of BB and its successor edges to exitMBB.
1285 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001286 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001287 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1288
Akira Hatanaka08636b42011-07-19 17:09:53 +00001289 BB->addSuccessor(loopMBB);
1290 loopMBB->addSuccessor(loopMBB);
1291 loopMBB->addSuccessor(sinkMBB);
1292 sinkMBB->addSuccessor(exitMBB);
1293
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001294 // thisMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001295 // addiu masklsb2,$0,-4 # 0xfffffffc
1296 // and alignedaddr,ptr,masklsb2
1297 // andi ptrlsb2,ptr,3
1298 // sll shiftamt,ptrlsb2,3
1299 // ori maskupper,$0,255 # 0xff
1300 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001301 // nor mask2,$0,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001302 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001303
1304 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Simon Dardisa2d8cc32016-04-28 16:26:43 +00001305 BuildMI(BB, DL, TII->get(ABI.GetPtrAddiuOp()), MaskLSB2)
1306 .addReg(ABI.GetNullPtr()).addImm(-4);
1307 BuildMI(BB, DL, TII->get(ABI.GetPtrAndOp()), AlignedAddr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001308 .addReg(Ptr).addReg(MaskLSB2);
Simon Dardisa2d8cc32016-04-28 16:26:43 +00001309 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2)
1310 .addReg(Ptr, 0, ArePtrs64bit ? Mips::sub_32 : 0).addImm(3);
Eric Christopher1c29a652014-07-18 22:55:25 +00001311 if (Subtarget.isLittle()) {
Akira Hatanaka2bf97332013-05-31 03:25:44 +00001312 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1313 } else {
1314 unsigned Off = RegInfo.createVirtualRegister(RC);
1315 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1316 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1317 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1318 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001319 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001320 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001321 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001322 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001323 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001324 BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(Incr).addReg(ShiftAmt);
Bruno Cardoso Lopesf771a0f2011-05-31 20:25:26 +00001325
Akira Hatanaka27292632011-07-18 18:52:12 +00001326 // atomic.load.binop
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001327 // loopMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001328 // ll oldval,0(alignedaddr)
1329 // binop binopres,oldval,incr2
1330 // and newval,binopres,mask
1331 // and maskedoldval0,oldval,mask2
1332 // or storeval,maskedoldval0,newval
1333 // sc success,storeval,0(alignedaddr)
1334 // beq success,$0,loopMBB
1335
Akira Hatanaka27292632011-07-18 18:52:12 +00001336 // atomic.swap
1337 // loopMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001338 // ll oldval,0(alignedaddr)
Akira Hatanakae4503582011-07-19 18:14:26 +00001339 // and newval,incr2,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001340 // and maskedoldval0,oldval,mask2
1341 // or storeval,maskedoldval0,newval
1342 // sc success,storeval,0(alignedaddr)
1343 // beq success,$0,loopMBB
Akira Hatanaka27292632011-07-18 18:52:12 +00001344
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001345 BB = loopMBB;
Jozef Kolek2f27d572014-12-18 16:39:29 +00001346 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001347 if (Nand) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001348 // and andres, oldval, incr2
1349 // nor binopres, $0, andres
1350 // and newval, binopres, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001351 BuildMI(BB, DL, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1352 BuildMI(BB, DL, TII->get(Mips::NOR), BinOpRes)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001353 .addReg(Mips::ZERO).addReg(AndRes);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001354 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001355 } else if (BinOpcode) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001356 // <binop> binopres, oldval, incr2
1357 // and newval, binopres, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001358 BuildMI(BB, DL, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1359 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001360 } else { // atomic.swap
Akira Hatanaka0e019592011-07-19 20:11:17 +00001361 // and newval, incr2, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001362 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanakae4503582011-07-19 18:14:26 +00001363 }
Jia Liuf54f60f2012-02-28 07:46:26 +00001364
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001365 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001366 .addReg(OldVal).addReg(Mask2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001367 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka9663dd32011-07-19 20:56:53 +00001368 .addReg(MaskedOldVal0).addReg(NewVal);
Jozef Kolek2f27d572014-12-18 16:39:29 +00001369 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001370 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001371 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001372 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001373
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001374 // sinkMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001375 // and maskedoldval1,oldval,mask
1376 // srl srlres,maskedoldval1,shiftamt
Daniel Sanders6a803f62014-06-16 13:13:03 +00001377 // sign_extend dest,srlres
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001378 BB = sinkMBB;
Akira Hatanakae97bd812011-07-19 03:14:58 +00001379
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001380 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001381 .addReg(OldVal).addReg(Mask);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001382 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001383 .addReg(MaskedOldVal1).addReg(ShiftAmt);
Daniel Sanders6a803f62014-06-16 13:13:03 +00001384 BB = emitSignExtendToI32InReg(MI, BB, Size, Dest, SrlRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001385
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001386 MI.eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001387
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001388 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001389}
1390
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001391MachineBasicBlock *MipsTargetLowering::emitAtomicCmpSwap(MachineInstr &MI,
1392 MachineBasicBlock *BB,
1393 unsigned Size) const {
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001394 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001395
1396 MachineFunction *MF = BB->getParent();
1397 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001398 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Eric Christopher96e72c62015-01-29 23:27:36 +00001399 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001400 const bool ArePtrs64bit = ABI.ArePtrs64bit();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001401 DebugLoc DL = MI.getDebugLoc();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001402 unsigned LL, SC, ZERO, BNE, BEQ;
1403
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001404 if (Size == 4) {
1405 if (isMicroMips) {
1406 LL = Mips::LL_MM;
1407 SC = Mips::SC_MM;
1408 } else {
1409 LL = Subtarget.hasMips32r6()
1410 ? (ArePtrs64bit ? Mips::LL64_R6 : Mips::LL_R6)
1411 : (ArePtrs64bit ? Mips::LL64 : Mips::LL);
1412 SC = Subtarget.hasMips32r6()
1413 ? (ArePtrs64bit ? Mips::SC64_R6 : Mips::SC_R6)
1414 : (ArePtrs64bit ? Mips::SC64 : Mips::SC);
1415 }
1416
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001417 ZERO = Mips::ZERO;
1418 BNE = Mips::BNE;
1419 BEQ = Mips::BEQ;
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001420 } else {
Zoran Jovanovic796ed6d2015-10-29 14:40:19 +00001421 LL = Subtarget.hasMips64r6() ? Mips::LLD_R6 : Mips::LLD;
1422 SC = Subtarget.hasMips64r6() ? Mips::SCD_R6 : Mips::SCD;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001423 ZERO = Mips::ZERO_64;
1424 BNE = Mips::BNE64;
1425 BEQ = Mips::BEQ64;
1426 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001427
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001428 unsigned Dest = MI.getOperand(0).getReg();
1429 unsigned Ptr = MI.getOperand(1).getReg();
1430 unsigned OldVal = MI.getOperand(2).getReg();
1431 unsigned NewVal = MI.getOperand(3).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001432
Akira Hatanaka0e019592011-07-19 20:11:17 +00001433 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001434
1435 // insert new blocks after the current block
1436 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1437 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1438 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1439 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +00001440 MachineFunction::iterator It = ++BB->getIterator();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001441 MF->insert(It, loop1MBB);
1442 MF->insert(It, loop2MBB);
1443 MF->insert(It, exitMBB);
1444
1445 // Transfer the remainder of BB and its successor edges to exitMBB.
1446 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001447 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001448 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1449
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001450 // thisMBB:
1451 // ...
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001452 // fallthrough --> loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001453 BB->addSuccessor(loop1MBB);
Akira Hatanaka08636b42011-07-19 17:09:53 +00001454 loop1MBB->addSuccessor(exitMBB);
1455 loop1MBB->addSuccessor(loop2MBB);
1456 loop2MBB->addSuccessor(loop1MBB);
1457 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001458
1459 // loop1MBB:
1460 // ll dest, 0(ptr)
1461 // bne dest, oldval, exitMBB
1462 BB = loop1MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001463 BuildMI(BB, DL, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1464 BuildMI(BB, DL, TII->get(BNE))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001465 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001466
1467 // loop2MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001468 // sc success, newval, 0(ptr)
1469 // beq success, $0, loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001470 BB = loop2MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001471 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001472 .addReg(NewVal).addReg(Ptr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001473 BuildMI(BB, DL, TII->get(BEQ))
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001474 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001475
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001476 MI.eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001477
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001478 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001479}
1480
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001481MachineBasicBlock *MipsTargetLowering::emitAtomicCmpSwapPartword(
1482 MachineInstr &MI, MachineBasicBlock *BB, unsigned Size) const {
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001483 assert((Size == 1 || Size == 2) &&
1484 "Unsupported size for EmitAtomicCmpSwapPartial.");
1485
1486 MachineFunction *MF = BB->getParent();
1487 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1488 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001489 const bool ArePtrs64bit = ABI.ArePtrs64bit();
Zoran Jovanovic2f6845b2016-04-13 16:02:25 +00001490 const TargetRegisterClass *RCp =
1491 getRegClassFor(ArePtrs64bit ? MVT::i64 : MVT::i32);
Eric Christopher96e72c62015-01-29 23:27:36 +00001492 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001493 DebugLoc DL = MI.getDebugLoc();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001494
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001495 unsigned Dest = MI.getOperand(0).getReg();
1496 unsigned Ptr = MI.getOperand(1).getReg();
1497 unsigned CmpVal = MI.getOperand(2).getReg();
1498 unsigned NewVal = MI.getOperand(3).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001499
Zoran Jovanovic2f6845b2016-04-13 16:02:25 +00001500 unsigned AlignedAddr = RegInfo.createVirtualRegister(RCp);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001501 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001502 unsigned Mask = RegInfo.createVirtualRegister(RC);
1503 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001504 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1505 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1506 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1507 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
Zoran Jovanovic2f6845b2016-04-13 16:02:25 +00001508 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RCp);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001509 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1510 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1511 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1512 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1513 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1514 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1515 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001516 unsigned Success = RegInfo.createVirtualRegister(RC);
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001517 unsigned LL, SC;
1518
1519 if (isMicroMips) {
1520 LL = Mips::LL_MM;
1521 SC = Mips::SC_MM;
1522 } else {
1523 LL = Subtarget.hasMips32r6() ? (ArePtrs64bit ? Mips::LL64_R6 : Mips::LL_R6)
1524 : (ArePtrs64bit ? Mips::LL64 : Mips::LL);
1525 SC = Subtarget.hasMips32r6() ? (ArePtrs64bit ? Mips::SC64_R6 : Mips::SC_R6)
1526 : (ArePtrs64bit ? Mips::SC64 : Mips::SC);
1527 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001528
1529 // insert new blocks after the current block
1530 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1531 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1532 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001533 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001534 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +00001535 MachineFunction::iterator It = ++BB->getIterator();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001536 MF->insert(It, loop1MBB);
1537 MF->insert(It, loop2MBB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001538 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001539 MF->insert(It, exitMBB);
1540
1541 // Transfer the remainder of BB and its successor edges to exitMBB.
1542 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001543 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001544 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1545
Akira Hatanaka08636b42011-07-19 17:09:53 +00001546 BB->addSuccessor(loop1MBB);
1547 loop1MBB->addSuccessor(sinkMBB);
1548 loop1MBB->addSuccessor(loop2MBB);
1549 loop2MBB->addSuccessor(loop1MBB);
1550 loop2MBB->addSuccessor(sinkMBB);
1551 sinkMBB->addSuccessor(exitMBB);
1552
Akira Hatanakae4503582011-07-19 18:14:26 +00001553 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001554 // thisMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001555 // addiu masklsb2,$0,-4 # 0xfffffffc
1556 // and alignedaddr,ptr,masklsb2
1557 // andi ptrlsb2,ptr,3
Zoran Jovanovic2f6845b2016-04-13 16:02:25 +00001558 // xori ptrlsb2,ptrlsb2,3 # Only for BE
Akira Hatanaka0e019592011-07-19 20:11:17 +00001559 // sll shiftamt,ptrlsb2,3
1560 // ori maskupper,$0,255 # 0xff
1561 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001562 // nor mask2,$0,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001563 // andi maskedcmpval,cmpval,255
1564 // sll shiftedcmpval,maskedcmpval,shiftamt
1565 // andi maskednewval,newval,255
1566 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001567 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Zoran Jovanovic2f6845b2016-04-13 16:02:25 +00001568 BuildMI(BB, DL, TII->get(ArePtrs64bit ? Mips::DADDiu : Mips::ADDiu), MaskLSB2)
1569 .addReg(ABI.GetNullPtr()).addImm(-4);
1570 BuildMI(BB, DL, TII->get(ArePtrs64bit ? Mips::AND64 : Mips::AND), AlignedAddr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001571 .addReg(Ptr).addReg(MaskLSB2);
Zoran Jovanovic2f6845b2016-04-13 16:02:25 +00001572 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2)
1573 .addReg(Ptr, 0, ArePtrs64bit ? Mips::sub_32 : 0).addImm(3);
Eric Christopher1c29a652014-07-18 22:55:25 +00001574 if (Subtarget.isLittle()) {
Akira Hatanaka2bf97332013-05-31 03:25:44 +00001575 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1576 } else {
1577 unsigned Off = RegInfo.createVirtualRegister(RC);
1578 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1579 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1580 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1581 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001582 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001583 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001584 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001585 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001586 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1587 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001588 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001589 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001590 .addReg(MaskedCmpVal).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001591 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001592 .addReg(NewVal).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001593 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001594 .addReg(MaskedNewVal).addReg(ShiftAmt);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001595
1596 // loop1MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001597 // ll oldval,0(alginedaddr)
1598 // and maskedoldval0,oldval,mask
1599 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001600 BB = loop1MBB;
Jozef Kolek2f27d572014-12-18 16:39:29 +00001601 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001602 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001603 .addReg(OldVal).addReg(Mask);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001604 BuildMI(BB, DL, TII->get(Mips::BNE))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001605 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001606
1607 // loop2MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001608 // and maskedoldval1,oldval,mask2
1609 // or storeval,maskedoldval1,shiftednewval
1610 // sc success,storeval,0(alignedaddr)
1611 // beq success,$0,loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001612 BB = loop2MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001613 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001614 .addReg(OldVal).addReg(Mask2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001615 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001616 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
Jozef Kolek2f27d572014-12-18 16:39:29 +00001617 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001618 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001619 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001620 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001621
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001622 // sinkMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001623 // srl srlres,maskedoldval0,shiftamt
Daniel Sanders6a803f62014-06-16 13:13:03 +00001624 // sign_extend dest,srlres
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001625 BB = sinkMBB;
Akira Hatanakae97bd812011-07-19 03:14:58 +00001626
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001627 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001628 .addReg(MaskedOldVal0).addReg(ShiftAmt);
Daniel Sanders6a803f62014-06-16 13:13:03 +00001629 BB = emitSignExtendToI32InReg(MI, BB, Size, Dest, SrlRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001630
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001631 MI.eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001632
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001633 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001634}
1635
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001636MachineBasicBlock *MipsTargetLowering::emitSEL_D(MachineInstr &MI,
Daniel Sanders0fa60412014-06-12 13:39:06 +00001637 MachineBasicBlock *BB) const {
1638 MachineFunction *MF = BB->getParent();
Eric Christopher96e72c62015-01-29 23:27:36 +00001639 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
1640 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders0fa60412014-06-12 13:39:06 +00001641 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001642 DebugLoc DL = MI.getDebugLoc();
Daniel Sanders0fa60412014-06-12 13:39:06 +00001643 MachineBasicBlock::iterator II(MI);
1644
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001645 unsigned Fc = MI.getOperand(1).getReg();
Daniel Sanders0fa60412014-06-12 13:39:06 +00001646 const auto &FGR64RegClass = TRI->getRegClass(Mips::FGR64RegClassID);
1647
1648 unsigned Fc2 = RegInfo.createVirtualRegister(FGR64RegClass);
1649
1650 BuildMI(*BB, II, DL, TII->get(Mips::SUBREG_TO_REG), Fc2)
1651 .addImm(0)
1652 .addReg(Fc)
1653 .addImm(Mips::sub_lo);
1654
1655 // We don't erase the original instruction, we just replace the condition
1656 // register with the 64-bit super-register.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001657 MI.getOperand(1).setReg(Fc2);
Daniel Sanders0fa60412014-06-12 13:39:06 +00001658
1659 return BB;
1660}
1661
Akira Hatanakae2489122011-04-15 21:51:11 +00001662//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001663// Misc Lower Operation implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00001664//===----------------------------------------------------------------------===//
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001665SDValue MipsTargetLowering::lowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001666 SDValue Chain = Op.getOperand(0);
1667 SDValue Table = Op.getOperand(1);
1668 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001669 SDLoc DL(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00001670 auto &TD = DAG.getDataLayout();
1671 EVT PTy = getPointerTy(TD);
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001672 unsigned EntrySize =
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001673 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(TD);
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001674
1675 Index = DAG.getNode(ISD::MUL, DL, PTy, Index,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001676 DAG.getConstant(EntrySize, DL, PTy));
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001677 SDValue Addr = DAG.getNode(ISD::ADD, DL, PTy, Index, Table);
1678
1679 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001680 Addr =
1681 DAG.getExtLoad(ISD::SEXTLOAD, DL, PTy, Chain, Addr,
1682 MachinePointerInfo::getJumpTable(DAG.getMachineFunction()),
1683 MemVT, false, false, false, 0);
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001684 Chain = Addr.getValue(1);
1685
Rafael Espindola9f1c1fe2016-06-27 12:48:21 +00001686 if (isPositionIndependent() || ABI.IsN64()) {
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001687 // For PIC, the sequence is:
1688 // BRIND(load(Jumptable + index) + RelocBase)
1689 // RelocBase can be JumpTable, GOT or some sort of global base.
1690 Addr = DAG.getNode(ISD::ADD, DL, PTy, Addr,
1691 getPICJumpTableRelocBase(Table, DAG));
1692 }
1693
1694 return DAG.getNode(ISD::BRIND, DL, MVT::Other, Chain, Addr);
1695}
1696
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001697SDValue MipsTargetLowering::lowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Wesley Peck527da1b2010-11-23 03:31:01 +00001698 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001699 // the block to branch to if the condition is true.
1700 SDValue Chain = Op.getOperand(0);
1701 SDValue Dest = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001702 SDLoc DL(Op);
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001703
Eric Christopher1c29a652014-07-18 22:55:25 +00001704 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001705 SDValue CondRes = createFPCmp(DAG, Op.getOperand(1));
Akira Hatanakaa5352702011-03-31 18:26:17 +00001706
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001707 // Return if flag is not set by a floating point comparison.
Akira Hatanakaa5352702011-03-31 18:26:17 +00001708 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopesa9504222008-07-30 17:06:13 +00001709 return Op;
Wesley Peck527da1b2010-11-23 03:31:01 +00001710
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +00001711 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmaneffb8942008-09-12 16:56:44 +00001712 Mips::CondCode CC =
1713 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Akira Hatanakaf0ea5002013-03-30 01:16:38 +00001714 unsigned Opc = invertFPCondCodeUser(CC) ? Mips::BRANCH_F : Mips::BRANCH_T;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001715 SDValue BrCode = DAG.getConstant(Opc, DL, MVT::i32);
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +00001716 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001717 return DAG.getNode(MipsISD::FPBrcond, DL, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +00001718 FCC0, Dest, CondRes);
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001719}
1720
1721SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001722lowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001723{
Eric Christopher1c29a652014-07-18 22:55:25 +00001724 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001725 SDValue Cond = createFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001726
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001727 // Return if flag is not set by a floating point comparison.
Akira Hatanakaa5352702011-03-31 18:26:17 +00001728 if (Cond.getOpcode() != MipsISD::FPCmp)
1729 return Op;
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +00001730
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001731 return createCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
Andrew Trickef9de2a2013-05-25 02:42:55 +00001732 SDLoc(Op));
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001733}
1734
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001735SDValue MipsTargetLowering::lowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00001736 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001737 SDValue Cond = createFPCmp(DAG, Op);
Akira Hatanakab7f78592012-03-09 23:46:03 +00001738
1739 assert(Cond.getOpcode() == MipsISD::FPCmp &&
1740 "Floating point operand expected.");
1741
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001742 SDLoc DL(Op);
Vasileios Kalintiris36901dd2016-03-01 20:25:43 +00001743 SDValue True = DAG.getConstant(1, DL, MVT::i32);
1744 SDValue False = DAG.getConstant(0, DL, MVT::i32);
Akira Hatanakab7f78592012-03-09 23:46:03 +00001745
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001746 return createCMovFP(DAG, Cond, True, False, DL);
Akira Hatanakab7f78592012-03-09 23:46:03 +00001747}
1748
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001749SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001750 SelectionDAG &DAG) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001751 EVT Ty = Op.getValueType();
1752 GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
1753 const GlobalValue *GV = N->getGlobal();
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001754
Rafael Espindola9f1c1fe2016-06-27 12:48:21 +00001755 if (!isPositionIndependent() && !ABI.IsN64()) {
Eric Christopher36fe0282015-02-03 07:22:52 +00001756 const MipsTargetObjectFile *TLOF =
1757 static_cast<const MipsTargetObjectFile *>(
1758 getTargetMachine().getObjFileLowering());
1759 if (TLOF->IsGlobalInSmallSection(GV, getTargetMachine()))
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001760 // %gp_rel relocation
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001761 return getAddrGPRel(N, SDLoc(N), Ty, DAG);
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001762
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001763 // %hi/%lo relocation
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001764 return getAddrNonPIC(N, SDLoc(N), Ty, DAG);
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001765 }
1766
Rafael Espindolab2b6a852016-06-27 12:33:33 +00001767 // Every other architecture would use shouldAssumeDSOLocal in here, but
1768 // mips is special.
Rafael Espindola97ca8272016-06-27 23:21:07 +00001769 // * In PIC code mips requires got loads even for local statics!
Rafael Espindolab2b6a852016-06-27 12:33:33 +00001770 // * To save on got entries, for local statics the got entry contains the
1771 // page and an additional add instruction takes care of the low bits.
1772 // * It is legal to access a hidden symbol with a non hidden undefined,
1773 // so one cannot guarantee that all access to a hidden symbol will know
1774 // it is hidden.
1775 // * Mips linkers don't support creating a page and a full got entry for
1776 // the same symbol.
1777 // * Given all that, we have to use a full got entry for hidden symbols :-(
Rafael Espindola1ac1fa82016-06-27 03:19:40 +00001778 if (GV->hasLocalLinkage())
Eric Christopher96e72c62015-01-29 23:27:36 +00001779 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001780
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00001781 if (LargeGOT)
Alex Lorenze40c8a22015-08-11 23:09:45 +00001782 return getAddrGlobalLargeGOT(
1783 N, SDLoc(N), Ty, DAG, MipsII::MO_GOT_HI16, MipsII::MO_GOT_LO16,
1784 DAG.getEntryNode(),
1785 MachinePointerInfo::getGOT(DAG.getMachineFunction()));
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00001786
Alex Lorenze40c8a22015-08-11 23:09:45 +00001787 return getAddrGlobal(
1788 N, SDLoc(N), Ty, DAG,
Daniel Sandersfe98b2f2016-05-03 13:35:44 +00001789 (ABI.IsN32() || ABI.IsN64()) ? MipsII::MO_GOT_DISP : MipsII::MO_GOT,
Alex Lorenze40c8a22015-08-11 23:09:45 +00001790 DAG.getEntryNode(), MachinePointerInfo::getGOT(DAG.getMachineFunction()));
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001791}
1792
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001793SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op,
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +00001794 SelectionDAG &DAG) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001795 BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op);
1796 EVT Ty = Op.getValueType();
Akira Hatanaka30f97cf2013-09-25 00:30:25 +00001797
Rafael Espindola9f1c1fe2016-06-27 12:48:21 +00001798 if (!isPositionIndependent() && !ABI.IsN64())
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001799 return getAddrNonPIC(N, SDLoc(N), Ty, DAG);
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001800
Eric Christopher96e72c62015-01-29 23:27:36 +00001801 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +00001802}
1803
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001804SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001805lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001806{
Akira Hatanakabff84e12011-12-14 18:26:41 +00001807 // If the relocation model is PIC, use the General Dynamic TLS Model or
1808 // Local Dynamic TLS model, otherwise use the Initial Exec or
1809 // Local Exec TLS Model.
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001810
1811 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +00001812 if (DAG.getTarget().Options.EmulatedTLS)
1813 return LowerToTLSEmulatedModel(GA, DAG);
1814
Andrew Trickef9de2a2013-05-25 02:42:55 +00001815 SDLoc DL(GA);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001816 const GlobalValue *GV = GA->getGlobal();
Mehdi Amini44ede332015-07-09 02:09:04 +00001817 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001818
Hans Wennborgaea41202012-05-04 09:40:39 +00001819 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
1820
1821 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Hans Wennborg245917b2012-06-04 14:02:08 +00001822 // General Dynamic and Local Dynamic TLS Model.
1823 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
1824 : MipsII::MO_TLSGD;
1825
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001826 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag);
1827 SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT,
1828 getGlobalReg(DAG, PtrVT), TGA);
Akira Hatanakaf10ee842011-12-08 21:05:38 +00001829 unsigned PtrSize = PtrVT.getSizeInBits();
1830 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1831
Benjamin Kramer64ba50a2011-12-11 12:21:34 +00001832 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001833
1834 ArgListTy Args;
1835 ArgListEntry Entry;
1836 Entry.Node = Argument;
Akira Hatanakadee6c822011-12-08 20:34:32 +00001837 Entry.Ty = PtrTy;
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001838 Args.push_back(Entry);
Jia Liuf54f60f2012-02-28 07:46:26 +00001839
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00001840 TargetLowering::CallLoweringInfo CLI(DAG);
1841 CLI.setDebugLoc(DL).setChain(DAG.getEntryNode())
Krzysztof Parzyszeke116d5002016-06-22 12:54:25 +00001842 .setCallee(CallingConv::C, PtrTy, TlsGetAddr, std::move(Args));
Justin Holewinskiaa583972012-05-25 16:35:28 +00001843 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001844
Akira Hatanakabff84e12011-12-14 18:26:41 +00001845 SDValue Ret = CallResult.first;
1846
Hans Wennborgaea41202012-05-04 09:40:39 +00001847 if (model != TLSModel::LocalDynamic)
Akira Hatanakabff84e12011-12-14 18:26:41 +00001848 return Ret;
1849
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001850 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanakabff84e12011-12-14 18:26:41 +00001851 MipsII::MO_DTPREL_HI);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001852 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1853 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanakabff84e12011-12-14 18:26:41 +00001854 MipsII::MO_DTPREL_LO);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001855 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1856 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret);
1857 return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001858 }
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001859
1860 SDValue Offset;
Hans Wennborgaea41202012-05-04 09:40:39 +00001861 if (model == TLSModel::InitialExec) {
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001862 // Initial Exec TLS Model
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001863 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001864 MipsII::MO_GOTTPREL);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001865 TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT),
Akira Hatanakab049aef2012-02-24 22:34:47 +00001866 TGA);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001867 Offset = DAG.getLoad(PtrVT, DL,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001868 DAG.getEntryNode(), TGA, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001869 false, false, false, 0);
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001870 } else {
1871 // Local Exec TLS Model
Hans Wennborgaea41202012-05-04 09:40:39 +00001872 assert(model == TLSModel::LocalExec);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001873 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001874 MipsII::MO_TPREL_HI);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001875 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001876 MipsII::MO_TPREL_LO);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001877 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1878 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1879 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001880 }
1881
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001882 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
1883 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001884}
1885
1886SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001887lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +00001888{
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001889 JumpTableSDNode *N = cast<JumpTableSDNode>(Op);
1890 EVT Ty = Op.getValueType();
Akira Hatanaka30f97cf2013-09-25 00:30:25 +00001891
Rafael Espindola9f1c1fe2016-06-27 12:48:21 +00001892 if (!isPositionIndependent() && !ABI.IsN64())
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001893 return getAddrNonPIC(N, SDLoc(N), Ty, DAG);
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001894
Eric Christopher96e72c62015-01-29 23:27:36 +00001895 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +00001896}
1897
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001898SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001899lowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +00001900{
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001901 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
1902 EVT Ty = Op.getValueType();
Bruno Cardoso Lopes2db07582009-11-25 12:17:58 +00001903
Rafael Espindola9f1c1fe2016-06-27 12:48:21 +00001904 if (!isPositionIndependent() && !ABI.IsN64()) {
Eric Christopher36fe0282015-02-03 07:22:52 +00001905 const MipsTargetObjectFile *TLOF =
1906 static_cast<const MipsTargetObjectFile *>(
1907 getTargetMachine().getObjFileLowering());
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001908
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001909 if (TLOF->IsConstantInSmallSection(DAG.getDataLayout(), N->getConstVal(),
1910 getTargetMachine()))
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001911 // %gp_rel relocation
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001912 return getAddrGPRel(N, SDLoc(N), Ty, DAG);
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001913
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001914 return getAddrNonPIC(N, SDLoc(N), Ty, DAG);
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001915 }
Bruno Cardoso Lopesfdb4cec2008-07-23 16:01:50 +00001916
Eric Christopher96e72c62015-01-29 23:27:36 +00001917 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +00001918}
1919
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001920SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman31ae5862010-04-17 14:41:14 +00001921 MachineFunction &MF = DAG.getMachineFunction();
1922 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1923
Andrew Trickef9de2a2013-05-25 02:42:55 +00001924 SDLoc DL(Op);
Dan Gohman31ae5862010-04-17 14:41:14 +00001925 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
Mehdi Amini44ede332015-07-09 02:09:04 +00001926 getPointerTy(MF.getDataLayout()));
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +00001927
1928 // vastart just stores the address of the VarArgsFrameIndex slot into the
1929 // memory location argument.
1930 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001931 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00001932 MachinePointerInfo(SV), false, false, 0);
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +00001933}
Jia Liuf54f60f2012-02-28 07:46:26 +00001934
Daniel Sanders2b553d42014-08-01 09:17:39 +00001935SDValue MipsTargetLowering::lowerVAARG(SDValue Op, SelectionDAG &DAG) const {
1936 SDNode *Node = Op.getNode();
1937 EVT VT = Node->getValueType(0);
1938 SDValue Chain = Node->getOperand(0);
1939 SDValue VAListPtr = Node->getOperand(1);
1940 unsigned Align = Node->getConstantOperandVal(3);
1941 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
1942 SDLoc DL(Node);
Eric Christopher96e72c62015-01-29 23:27:36 +00001943 unsigned ArgSlotSizeInBytes = (ABI.IsN32() || ABI.IsN64()) ? 8 : 4;
Daniel Sanders2b553d42014-08-01 09:17:39 +00001944
Mehdi Amini44ede332015-07-09 02:09:04 +00001945 SDValue VAListLoad =
1946 DAG.getLoad(getPointerTy(DAG.getDataLayout()), DL, Chain, VAListPtr,
1947 MachinePointerInfo(SV), false, false, false, 0);
Daniel Sanders2b553d42014-08-01 09:17:39 +00001948 SDValue VAList = VAListLoad;
1949
1950 // Re-align the pointer if necessary.
1951 // It should only ever be necessary for 64-bit types on O32 since the minimum
1952 // argument alignment is the same as the maximum type alignment for N32/N64.
1953 //
1954 // FIXME: We currently align too often. The code generator doesn't notice
1955 // when the pointer is still aligned from the last va_arg (or pair of
1956 // va_args for the i64 on O32 case).
1957 if (Align > getMinStackArgumentAlignment()) {
1958 assert(((Align & (Align-1)) == 0) && "Expected Align to be a power of 2");
1959
1960 VAList = DAG.getNode(ISD::ADD, DL, VAList.getValueType(), VAList,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001961 DAG.getConstant(Align - 1, DL, VAList.getValueType()));
Daniel Sanders2b553d42014-08-01 09:17:39 +00001962
1963 VAList = DAG.getNode(ISD::AND, DL, VAList.getValueType(), VAList,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001964 DAG.getConstant(-(int64_t)Align, DL,
Daniel Sanders2b553d42014-08-01 09:17:39 +00001965 VAList.getValueType()));
1966 }
1967
1968 // Increment the pointer, VAList, to the next vaarg.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001969 auto &TD = DAG.getDataLayout();
1970 unsigned ArgSizeInBytes =
1971 TD.getTypeAllocSize(VT.getTypeForEVT(*DAG.getContext()));
Rui Ueyamada00f2f2016-01-14 21:06:47 +00001972 SDValue Tmp3 =
1973 DAG.getNode(ISD::ADD, DL, VAList.getValueType(), VAList,
1974 DAG.getConstant(alignTo(ArgSizeInBytes, ArgSlotSizeInBytes),
1975 DL, VAList.getValueType()));
Daniel Sanders2b553d42014-08-01 09:17:39 +00001976 // Store the incremented VAList to the legalized pointer
1977 Chain = DAG.getStore(VAListLoad.getValue(1), DL, Tmp3, VAListPtr,
1978 MachinePointerInfo(SV), false, false, 0);
1979
1980 // In big-endian mode we must adjust the pointer when the load size is smaller
1981 // than the argument slot size. We must also reduce the known alignment to
1982 // match. For example in the N64 ABI, we must add 4 bytes to the offset to get
1983 // the correct half of the slot, and reduce the alignment from 8 (slot
1984 // alignment) down to 4 (type alignment).
1985 if (!Subtarget.isLittle() && ArgSizeInBytes < ArgSlotSizeInBytes) {
1986 unsigned Adjustment = ArgSlotSizeInBytes - ArgSizeInBytes;
1987 VAList = DAG.getNode(ISD::ADD, DL, VAListPtr.getValueType(), VAList,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001988 DAG.getIntPtrConstant(Adjustment, DL));
Daniel Sanders2b553d42014-08-01 09:17:39 +00001989 }
1990 // Load the actual argument out of the pointer VAList
1991 return DAG.getLoad(VT, DL, Chain, VAList, MachinePointerInfo(), false, false,
1992 false, 0);
1993}
1994
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001995static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG,
1996 bool HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001997 EVT TyX = Op.getOperand(0).getValueType();
1998 EVT TyY = Op.getOperand(1).getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001999 SDLoc DL(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002000 SDValue Const1 = DAG.getConstant(1, DL, MVT::i32);
2001 SDValue Const31 = DAG.getConstant(31, DL, MVT::i32);
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002002 SDValue Res;
2003
2004 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
2005 // to i32.
2006 SDValue X = (TyX == MVT::f32) ?
2007 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
2008 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
2009 Const1);
2010 SDValue Y = (TyY == MVT::f32) ?
2011 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
2012 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
2013 Const1);
2014
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00002015 if (HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002016 // ext E, Y, 31, 1 ; extract bit31 of Y
2017 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
2018 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
2019 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
2020 } else {
2021 // sll SllX, X, 1
2022 // srl SrlX, SllX, 1
2023 // srl SrlY, Y, 31
2024 // sll SllY, SrlX, 31
2025 // or Or, SrlX, SllY
2026 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
2027 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
2028 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
2029 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
2030 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
2031 }
2032
2033 if (TyX == MVT::f32)
2034 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
2035
2036 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002037 Op.getOperand(0),
2038 DAG.getConstant(0, DL, MVT::i32));
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002039 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002040}
2041
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00002042static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG,
2043 bool HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002044 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
2045 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
2046 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002047 SDLoc DL(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002048 SDValue Const1 = DAG.getConstant(1, DL, MVT::i32);
Eric Christopher0713a9d2011-06-08 23:55:35 +00002049
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002050 // Bitcast to integer nodes.
2051 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
2052 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002053
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00002054 if (HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002055 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
2056 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
2057 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002058 DAG.getConstant(WidthY - 1, DL, MVT::i32), Const1);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002059
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002060 if (WidthX > WidthY)
2061 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
2062 else if (WidthY > WidthX)
2063 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002064
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002065 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002066 DAG.getConstant(WidthX - 1, DL, MVT::i32), Const1,
2067 X);
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002068 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
2069 }
2070
2071 // (d)sll SllX, X, 1
2072 // (d)srl SrlX, SllX, 1
2073 // (d)srl SrlY, Y, width(Y)-1
2074 // (d)sll SllY, SrlX, width(Y)-1
2075 // or Or, SrlX, SllY
2076 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
2077 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
2078 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002079 DAG.getConstant(WidthY - 1, DL, MVT::i32));
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002080
2081 if (WidthX > WidthY)
2082 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
2083 else if (WidthY > WidthX)
2084 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
2085
2086 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002087 DAG.getConstant(WidthX - 1, DL, MVT::i32));
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002088 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
2089 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002090}
2091
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00002092SDValue
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002093MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00002094 if (Subtarget.isGP64bit())
2095 return lowerFCOPYSIGN64(Op, DAG, Subtarget.hasExtractInsert());
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002096
Eric Christopher1c29a652014-07-18 22:55:25 +00002097 return lowerFCOPYSIGN32(Op, DAG, Subtarget.hasExtractInsert());
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002098}
2099
Akira Hatanaka66277522011-06-02 00:24:44 +00002100SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002101lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes5444a7b2011-06-16 00:40:02 +00002102 // check the depth
2103 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka15506782011-06-07 18:58:42 +00002104 "Frame address can only be determined for current frame.");
Akira Hatanaka66277522011-06-02 00:24:44 +00002105
2106 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2107 MFI->setFrameAddressIsTaken(true);
2108 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002109 SDLoc DL(Op);
Eric Christopher96e72c62015-01-29 23:27:36 +00002110 SDValue FrameAddr = DAG.getCopyFromReg(
2111 DAG.getEntryNode(), DL, ABI.IsN64() ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka66277522011-06-02 00:24:44 +00002112 return FrameAddr;
2113}
2114
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002115SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op,
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002116 SelectionDAG &DAG) const {
Bill Wendling908bf812014-01-06 00:43:20 +00002117 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00002118 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00002119
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002120 // check the depth
2121 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
2122 "Return address can be determined only for current frame.");
2123
2124 MachineFunction &MF = DAG.getMachineFunction();
2125 MachineFrameInfo *MFI = MF.getFrameInfo();
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00002126 MVT VT = Op.getSimpleValueType();
Eric Christopher96e72c62015-01-29 23:27:36 +00002127 unsigned RA = ABI.IsN64() ? Mips::RA_64 : Mips::RA;
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002128 MFI->setReturnAddressIsTaken(true);
2129
2130 // Return RA, which contains the return address. Mark it an implicit live-in.
2131 unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00002132 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT);
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002133}
2134
Akira Hatanakac0b02062013-01-30 00:26:49 +00002135// An EH_RETURN is the result of lowering llvm.eh.return which in turn is
2136// generated from __builtin_eh_return (offset, handler)
2137// The effect of this is to adjust the stack pointer by "offset"
2138// and then branch to "handler".
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002139SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Akira Hatanakac0b02062013-01-30 00:26:49 +00002140 const {
2141 MachineFunction &MF = DAG.getMachineFunction();
2142 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2143
2144 MipsFI->setCallsEhReturn();
2145 SDValue Chain = Op.getOperand(0);
2146 SDValue Offset = Op.getOperand(1);
2147 SDValue Handler = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002148 SDLoc DL(Op);
Eric Christopher96e72c62015-01-29 23:27:36 +00002149 EVT Ty = ABI.IsN64() ? MVT::i64 : MVT::i32;
Akira Hatanakac0b02062013-01-30 00:26:49 +00002150
2151 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
2152 // EH_RETURN nodes, so that instructions are emitted back-to-back.
Eric Christopher96e72c62015-01-29 23:27:36 +00002153 unsigned OffsetReg = ABI.IsN64() ? Mips::V1_64 : Mips::V1;
2154 unsigned AddrReg = ABI.IsN64() ? Mips::V0_64 : Mips::V0;
Akira Hatanakac0b02062013-01-30 00:26:49 +00002155 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
2156 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
2157 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
2158 DAG.getRegister(OffsetReg, Ty),
Mehdi Amini44ede332015-07-09 02:09:04 +00002159 DAG.getRegister(AddrReg, getPointerTy(MF.getDataLayout())),
Akira Hatanakac0b02062013-01-30 00:26:49 +00002160 Chain.getValue(1));
2161}
2162
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002163SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
Akira Hatanaka5fd22482012-06-14 21:10:56 +00002164 SelectionDAG &DAG) const {
Eli Friedman26a48482011-07-27 22:21:52 +00002165 // FIXME: Need pseudo-fence for 'singlethread' fences
2166 // FIXME: Set SType for weaker fences where supported/appropriate.
2167 unsigned SType = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002168 SDLoc DL(Op);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002169 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002170 DAG.getConstant(SType, DL, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00002171}
2172
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002173SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
Akira Hatanaka5fd22482012-06-14 21:10:56 +00002174 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002175 SDLoc DL(Op);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002176 MVT VT = Subtarget.isGP64bit() ? MVT::i64 : MVT::i32;
2177
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002178 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2179 SDValue Shamt = Op.getOperand(2);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002180 // if shamt < (VT.bits):
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002181 // lo = (shl lo, shamt)
2182 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
2183 // else:
2184 // lo = 0
2185 // hi = (shl lo, shamt[4:0])
2186 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002187 DAG.getConstant(-1, DL, MVT::i32));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002188 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, VT, Lo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002189 DAG.getConstant(1, DL, VT));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002190 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, VT, ShiftRight1Lo, Not);
2191 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, VT, Hi, Shamt);
2192 SDValue Or = DAG.getNode(ISD::OR, DL, VT, ShiftLeftHi, ShiftRightLo);
2193 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, VT, Lo, Shamt);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002194 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
Daniel Sanders301f9372015-04-29 12:28:58 +00002195 DAG.getConstant(VT.getSizeInBits(), DL, MVT::i32));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002196 Lo = DAG.getNode(ISD::SELECT, DL, VT, Cond,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002197 DAG.getConstant(0, DL, VT), ShiftLeftLo);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002198 Hi = DAG.getNode(ISD::SELECT, DL, VT, Cond, ShiftLeftLo, Or);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002199
2200 SDValue Ops[2] = {Lo, Hi};
Craig Topper64941d92014-04-27 19:20:57 +00002201 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002202}
2203
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002204SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002205 bool IsSRA) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002206 SDLoc DL(Op);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002207 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2208 SDValue Shamt = Op.getOperand(2);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002209 MVT VT = Subtarget.isGP64bit() ? MVT::i64 : MVT::i32;
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002210
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002211 // if shamt < (VT.bits):
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002212 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
2213 // if isSRA:
2214 // hi = (sra hi, shamt)
2215 // else:
2216 // hi = (srl hi, shamt)
2217 // else:
2218 // if isSRA:
2219 // lo = (sra hi, shamt[4:0])
2220 // hi = (sra hi, 31)
2221 // else:
2222 // lo = (srl hi, shamt[4:0])
2223 // hi = 0
2224 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002225 DAG.getConstant(-1, DL, MVT::i32));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002226 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, VT, Hi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002227 DAG.getConstant(1, DL, VT));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002228 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, VT, ShiftLeft1Hi, Not);
2229 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, VT, Lo, Shamt);
2230 SDValue Or = DAG.getNode(ISD::OR, DL, VT, ShiftLeftHi, ShiftRightLo);
2231 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL,
2232 DL, VT, Hi, Shamt);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002233 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
Daniel Sanders301f9372015-04-29 12:28:58 +00002234 DAG.getConstant(VT.getSizeInBits(), DL, MVT::i32));
2235 SDValue Ext = DAG.getNode(ISD::SRA, DL, VT, Hi,
2236 DAG.getConstant(VT.getSizeInBits() - 1, DL, VT));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002237 Lo = DAG.getNode(ISD::SELECT, DL, VT, Cond, ShiftRightHi, Or);
2238 Hi = DAG.getNode(ISD::SELECT, DL, VT, Cond,
Daniel Sanders301f9372015-04-29 12:28:58 +00002239 IsSRA ? Ext : DAG.getConstant(0, DL, VT), ShiftRightHi);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002240
2241 SDValue Ops[2] = {Lo, Hi};
Craig Topper64941d92014-04-27 19:20:57 +00002242 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002243}
2244
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002245static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002246 SDValue Chain, SDValue Src, unsigned Offset) {
Akira Hatanaka95866182012-06-13 19:06:08 +00002247 SDValue Ptr = LD->getBasePtr();
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002248 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
Akira Hatanaka95866182012-06-13 19:06:08 +00002249 EVT BasePtrVT = Ptr.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002250 SDLoc DL(LD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002251 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
2252
2253 if (Offset)
Akira Hatanaka95866182012-06-13 19:06:08 +00002254 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002255 DAG.getConstant(Offset, DL, BasePtrVT));
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002256
2257 SDValue Ops[] = { Chain, Ptr, Src };
Craig Topper206fcd42014-04-26 19:29:41 +00002258 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002259 LD->getMemOperand());
2260}
2261
2262// Expand an unaligned 32 or 64-bit integer load node.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002263SDValue MipsTargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002264 LoadSDNode *LD = cast<LoadSDNode>(Op);
2265 EVT MemVT = LD->getMemoryVT();
2266
Eric Christopher1c29a652014-07-18 22:55:25 +00002267 if (Subtarget.systemSupportsUnalignedAccess())
Daniel Sandersac272632014-05-23 13:18:02 +00002268 return Op;
2269
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002270 // Return if load is aligned or if MemVT is neither i32 nor i64.
2271 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
2272 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
2273 return SDValue();
2274
Eric Christopher1c29a652014-07-18 22:55:25 +00002275 bool IsLittle = Subtarget.isLittle();
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002276 EVT VT = Op.getValueType();
2277 ISD::LoadExtType ExtType = LD->getExtensionType();
2278 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
2279
2280 assert((VT == MVT::i32) || (VT == MVT::i64));
2281
2282 // Expand
2283 // (set dst, (i64 (load baseptr)))
2284 // to
2285 // (set tmp, (ldl (add baseptr, 7), undef))
2286 // (set dst, (ldr baseptr, tmp))
2287 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002288 SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002289 IsLittle ? 7 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002290 return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002291 IsLittle ? 0 : 7);
2292 }
2293
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002294 SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002295 IsLittle ? 3 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002296 SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002297 IsLittle ? 0 : 3);
2298
2299 // Expand
2300 // (set dst, (i32 (load baseptr))) or
2301 // (set dst, (i64 (sextload baseptr))) or
2302 // (set dst, (i64 (extload baseptr)))
2303 // to
2304 // (set tmp, (lwl (add baseptr, 3), undef))
2305 // (set dst, (lwr baseptr, tmp))
2306 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
2307 (ExtType == ISD::EXTLOAD))
2308 return LWR;
2309
2310 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
2311
2312 // Expand
2313 // (set dst, (i64 (zextload baseptr)))
2314 // to
2315 // (set tmp0, (lwl (add baseptr, 3), undef))
2316 // (set tmp1, (lwr baseptr, tmp0))
2317 // (set tmp2, (shl tmp1, 32))
2318 // (set dst, (srl tmp2, 32))
Andrew Trickef9de2a2013-05-25 02:42:55 +00002319 SDLoc DL(LD);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002320 SDValue Const32 = DAG.getConstant(32, DL, MVT::i32);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002321 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
Akira Hatanaka67346852012-06-04 17:46:29 +00002322 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
2323 SDValue Ops[] = { SRL, LWR.getValue(1) };
Craig Topper64941d92014-04-27 19:20:57 +00002324 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002325}
2326
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002327static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002328 SDValue Chain, unsigned Offset) {
Akira Hatanaka95866182012-06-13 19:06:08 +00002329 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
2330 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002331 SDLoc DL(SD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002332 SDVTList VTList = DAG.getVTList(MVT::Other);
2333
2334 if (Offset)
Akira Hatanaka95866182012-06-13 19:06:08 +00002335 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002336 DAG.getConstant(Offset, DL, BasePtrVT));
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002337
2338 SDValue Ops[] = { Chain, Value, Ptr };
Craig Topper206fcd42014-04-26 19:29:41 +00002339 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002340 SD->getMemOperand());
2341}
2342
2343// Expand an unaligned 32 or 64-bit integer store node.
Akira Hatanakad82ee942013-05-16 20:45:17 +00002344static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG,
2345 bool IsLittle) {
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002346 SDValue Value = SD->getValue(), Chain = SD->getChain();
2347 EVT VT = Value.getValueType();
2348
2349 // Expand
2350 // (store val, baseptr) or
2351 // (truncstore val, baseptr)
2352 // to
2353 // (swl val, (add baseptr, 3))
2354 // (swr val, baseptr)
2355 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002356 SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002357 IsLittle ? 3 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002358 return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002359 }
2360
2361 assert(VT == MVT::i64);
2362
2363 // Expand
2364 // (store val, baseptr)
2365 // to
2366 // (sdl val, (add baseptr, 7))
2367 // (sdr val, baseptr)
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002368 SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2369 return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002370}
2371
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002372// Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr).
2373static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG) {
2374 SDValue Val = SD->getValue();
2375
2376 if (Val.getOpcode() != ISD::FP_TO_SINT)
2377 return SDValue();
2378
2379 EVT FPTy = EVT::getFloatingPointVT(Val.getValueSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002380 SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy,
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002381 Val.getOperand(0));
2382
Andrew Trickef9de2a2013-05-25 02:42:55 +00002383 return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(),
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002384 SD->getPointerInfo(), SD->isVolatile(),
2385 SD->isNonTemporal(), SD->getAlignment());
2386}
2387
Akira Hatanakad82ee942013-05-16 20:45:17 +00002388SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2389 StoreSDNode *SD = cast<StoreSDNode>(Op);
2390 EVT MemVT = SD->getMemoryVT();
2391
2392 // Lower unaligned integer stores.
Eric Christopher1c29a652014-07-18 22:55:25 +00002393 if (!Subtarget.systemSupportsUnalignedAccess() &&
Daniel Sandersac272632014-05-23 13:18:02 +00002394 (SD->getAlignment() < MemVT.getSizeInBits() / 8) &&
Akira Hatanakad82ee942013-05-16 20:45:17 +00002395 ((MemVT == MVT::i32) || (MemVT == MVT::i64)))
Eric Christopher1c29a652014-07-18 22:55:25 +00002396 return lowerUnalignedIntStore(SD, DAG, Subtarget.isLittle());
Akira Hatanakad82ee942013-05-16 20:45:17 +00002397
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002398 return lowerFP_TO_SINT_STORE(SD, DAG);
Akira Hatanakad82ee942013-05-16 20:45:17 +00002399}
2400
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002401SDValue MipsTargetLowering::lowerADD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002402 if (Op->getOperand(0).getOpcode() != ISD::FRAMEADDR
2403 || cast<ConstantSDNode>
2404 (Op->getOperand(0).getOperand(0))->getZExtValue() != 0
2405 || Op->getOperand(1).getOpcode() != ISD::FRAME_TO_ARGS_OFFSET)
2406 return SDValue();
2407
2408 // The pattern
2409 // (add (frameaddr 0), (frame_to_args_offset))
2410 // results from lowering llvm.eh.dwarf.cfa intrinsic. Transform it to
2411 // (add FrameObject, 0)
2412 // where FrameObject is a fixed StackObject with offset 0 which points to
2413 // the old stack pointer.
2414 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2415 EVT ValTy = Op->getValueType(0);
2416 int FI = MFI->CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
2417 SDValue InArgsAddr = DAG.getFrameIndex(FI, ValTy);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002418 SDLoc DL(Op);
2419 return DAG.getNode(ISD::ADD, DL, ValTy, InArgsAddr,
2420 DAG.getConstant(0, DL, ValTy));
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002421}
2422
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002423SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op,
2424 SelectionDAG &DAG) const {
2425 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002426 SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy,
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002427 Op.getOperand(0));
Andrew Trickef9de2a2013-05-25 02:42:55 +00002428 return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc);
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002429}
2430
Akira Hatanakae2489122011-04-15 21:51:11 +00002431//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002432// Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002433//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002434
Akira Hatanakae2489122011-04-15 21:51:11 +00002435//===----------------------------------------------------------------------===//
Wesley Peck527da1b2010-11-23 03:31:01 +00002436// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002437// Mips O32 ABI rules:
2438// ---
2439// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peck527da1b2010-11-23 03:31:01 +00002440// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002441// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peck527da1b2010-11-23 03:31:01 +00002442// f64 - Only passed in two aliased f32 registers if no int reg has been used
2443// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Sylvestre Ledru469de192014-08-11 18:04:46 +00002444// not used, it must be shadowed. If only A3 is available, shadow it and
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002445// go to stack.
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002446//
2447// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanakae2489122011-04-15 21:51:11 +00002448//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002449
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00002450static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2451 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002452 CCState &State, ArrayRef<MCPhysReg> F64Regs) {
Eric Christopher96e72c62015-01-29 23:27:36 +00002453 const MipsSubtarget &Subtarget = static_cast<const MipsSubtarget &>(
2454 State.getMachineFunction().getSubtarget());
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002455
Craig Topper840beec2014-04-04 05:16:06 +00002456 static const MCPhysReg IntRegs[] = { Mips::A0, Mips::A1, Mips::A2, Mips::A3 };
2457 static const MCPhysReg F32Regs[] = { Mips::F12, Mips::F14 };
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002458
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002459 // Do not process byval args here.
2460 if (ArgFlags.isByVal())
2461 return true;
Akira Hatanaka5e16c6a2011-05-24 19:18:33 +00002462
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002463 // Promote i8 and i16
Daniel Sandersd134c9d2014-12-02 20:40:27 +00002464 if (ArgFlags.isInReg() && !Subtarget.isLittle()) {
2465 if (LocVT == MVT::i8 || LocVT == MVT::i16 || LocVT == MVT::i32) {
2466 LocVT = MVT::i32;
2467 if (ArgFlags.isSExt())
2468 LocInfo = CCValAssign::SExtUpper;
2469 else if (ArgFlags.isZExt())
2470 LocInfo = CCValAssign::ZExtUpper;
2471 else
2472 LocInfo = CCValAssign::AExtUpper;
2473 }
2474 }
2475
2476 // Promote i8 and i16
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002477 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
2478 LocVT = MVT::i32;
2479 if (ArgFlags.isSExt())
2480 LocInfo = CCValAssign::SExt;
2481 else if (ArgFlags.isZExt())
2482 LocInfo = CCValAssign::ZExt;
2483 else
2484 LocInfo = CCValAssign::AExt;
2485 }
2486
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002487 unsigned Reg;
2488
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002489 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
2490 // is true: function is vararg, argument is 3rd or higher, there is previous
2491 // argument which is not f32 or f64.
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002492 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1 ||
2493 State.getFirstUnallocated(F32Regs) != ValNo;
Akira Hatanaka9e6a8cc2011-05-19 20:29:48 +00002494 unsigned OrigAlign = ArgFlags.getOrigAlign();
2495 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002496
2497 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002498 Reg = State.AllocateReg(IntRegs);
Akira Hatanaka9e6a8cc2011-05-19 20:29:48 +00002499 // If this is the first part of an i64 arg,
2500 // the allocated register must be either A0 or A2.
2501 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002502 Reg = State.AllocateReg(IntRegs);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002503 LocVT = MVT::i32;
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002504 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
2505 // Allocate int register and shadow next int register. If first
2506 // available register is Mips::A1 or Mips::A3, shadow it too.
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002507 Reg = State.AllocateReg(IntRegs);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002508 if (Reg == Mips::A1 || Reg == Mips::A3)
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002509 Reg = State.AllocateReg(IntRegs);
2510 State.AllocateReg(IntRegs);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002511 LocVT = MVT::i32;
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002512 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
2513 // we are guaranteed to find an available float register
2514 if (ValVT == MVT::f32) {
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002515 Reg = State.AllocateReg(F32Regs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002516 // Shadow int register
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002517 State.AllocateReg(IntRegs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002518 } else {
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002519 Reg = State.AllocateReg(F64Regs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002520 // Shadow int registers
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002521 unsigned Reg2 = State.AllocateReg(IntRegs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002522 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002523 State.AllocateReg(IntRegs);
2524 State.AllocateReg(IntRegs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002525 }
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002526 } else
2527 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002528
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002529 if (!Reg) {
2530 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3,
2531 OrigAlign);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002532 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002533 } else
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002534 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002535
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002536 return false;
Akira Hatanaka202f6402011-11-12 02:20:46 +00002537}
2538
Akira Hatanakabfb66242013-08-20 23:38:40 +00002539static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT,
2540 MVT LocVT, CCValAssign::LocInfo LocInfo,
2541 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002542 static const MCPhysReg F64Regs[] = { Mips::D6, Mips::D7 };
Akira Hatanakabfb66242013-08-20 23:38:40 +00002543
2544 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2545}
2546
2547static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT,
2548 MVT LocVT, CCValAssign::LocInfo LocInfo,
2549 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002550 static const MCPhysReg F64Regs[] = { Mips::D12_64, Mips::D14_64 };
Akira Hatanakabfb66242013-08-20 23:38:40 +00002551
2552 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2553}
2554
Reid Klecknerd3781742014-11-14 00:39:33 +00002555static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2556 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
2557 CCState &State) LLVM_ATTRIBUTE_UNUSED;
Reed Kotlerd5c41962014-11-13 23:37:45 +00002558
Akira Hatanaka202f6402011-11-12 02:20:46 +00002559#include "MipsGenCallingConv.inc"
2560
Akira Hatanakae2489122011-04-15 21:51:11 +00002561//===----------------------------------------------------------------------===//
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002562// Call Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002563//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002564
Akira Hatanaka61bbcce2011-09-23 00:58:33 +00002565// Return next O32 integer argument register.
2566static unsigned getNextIntArgReg(unsigned Reg) {
2567 assert((Reg == Mips::A0) || (Reg == Mips::A2));
2568 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
2569}
2570
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002571SDValue MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
2572 SDValue Chain, SDValue Arg,
2573 const SDLoc &DL, bool IsTailCall,
2574 SelectionDAG &DAG) const {
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002575 if (!IsTailCall) {
Mehdi Amini44ede332015-07-09 02:09:04 +00002576 SDValue PtrOff =
2577 DAG.getNode(ISD::ADD, DL, getPointerTy(DAG.getDataLayout()), StackPtr,
2578 DAG.getIntPtrConstant(Offset, DL));
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002579 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo(), false,
2580 false, 0);
2581 }
2582
2583 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2584 int FI = MFI->CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00002585 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002586 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(),
2587 /*isVolatile=*/ true, false, 0);
2588}
2589
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002590void MipsTargetLowering::
2591getOpndList(SmallVectorImpl<SDValue> &Ops,
2592 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
2593 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
Sasa Stankovic7072a792014-10-01 08:22:21 +00002594 bool IsCallReloc, CallLoweringInfo &CLI, SDValue Callee,
2595 SDValue Chain) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002596 // Insert node "GP copy globalreg" before call to function.
2597 //
2598 // R_MIPS_CALL* operators (emitted when non-internal functions are called
2599 // in PIC mode) allow symbols to be resolved via lazy binding.
2600 // The lazy binding stub requires GP to point to the GOT.
Sasa Stankovic7072a792014-10-01 08:22:21 +00002601 // Note that we don't need GP to point to the GOT for indirect calls
2602 // (when R_MIPS_CALL* is not used for the call) because Mips linker generates
2603 // lazy binding stub for a function only when R_MIPS_CALL* are the only relocs
2604 // used for the function (that is, Mips linker doesn't generate lazy binding
2605 // stub for a function whose address is taken in the program).
2606 if (IsPICCall && !InternalLinkage && IsCallReloc) {
Eric Christopher96e72c62015-01-29 23:27:36 +00002607 unsigned GPReg = ABI.IsN64() ? Mips::GP_64 : Mips::GP;
2608 EVT Ty = ABI.IsN64() ? MVT::i64 : MVT::i32;
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002609 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty)));
2610 }
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002611
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002612 // Build a sequence of copy-to-reg nodes chained together with token
2613 // chain and flag operands which copy the outgoing args into registers.
2614 // The InFlag in necessary since all emitted instructions must be
2615 // stuck together.
2616 SDValue InFlag;
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002617
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002618 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2619 Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, RegsToPass[i].first,
2620 RegsToPass[i].second, InFlag);
2621 InFlag = Chain.getValue(1);
2622 }
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002623
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002624 // Add argument registers to the end of the list so that they are
2625 // known live into the call.
2626 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2627 Ops.push_back(CLI.DAG.getRegister(RegsToPass[i].first,
2628 RegsToPass[i].second.getValueType()));
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002629
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002630 // Add a register mask operand representing the call-preserved registers.
Eric Christopher96e72c62015-01-29 23:27:36 +00002631 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00002632 const uint32_t *Mask =
2633 TRI->getCallPreservedMask(CLI.DAG.getMachineFunction(), CLI.CallConv);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002634 assert(Mask && "Missing call preserved mask for calling convention");
Eric Christopher1c29a652014-07-18 22:55:25 +00002635 if (Subtarget.inMips16HardFloat()) {
Reed Kotler783c7942013-05-10 22:25:39 +00002636 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
2637 llvm::StringRef Sym = G->getGlobal()->getName();
2638 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
Reed Kotler3230e722013-12-12 02:41:11 +00002639 if (F && F->hasFnAttribute("__Mips16RetHelper")) {
Reed Kotler783c7942013-05-10 22:25:39 +00002640 Mask = MipsRegisterInfo::getMips16RetHelperMask();
2641 }
2642 }
2643 }
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002644 Ops.push_back(CLI.DAG.getRegisterMask(Mask));
2645
2646 if (InFlag.getNode())
2647 Ops.push_back(InFlag);
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002648}
2649
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002650/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman624801e2009-01-26 03:15:54 +00002651/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002652SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00002653MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002654 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00002655 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002656 SDLoc DL = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00002657 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
2658 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
2659 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Akira Hatanakabeda2242012-07-31 18:46:41 +00002660 SDValue Chain = CLI.Chain;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002661 SDValue Callee = CLI.Callee;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002662 bool &IsTailCall = CLI.IsTailCall;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002663 CallingConv::ID CallConv = CLI.CallConv;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002664 bool IsVarArg = CLI.IsVarArg;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002665
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002666 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002667 MachineFrameInfo *MFI = MF.getFrameInfo();
Eric Christopher96e72c62015-01-29 23:27:36 +00002668 const TargetFrameLowering *TFL = Subtarget.getFrameLowering();
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002669 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
Rafael Espindola9f1c1fe2016-06-27 12:48:21 +00002670 bool IsPIC = isPositionIndependent();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002671
2672 // Analyze operands of the call, assigning locations to each operand.
2673 SmallVector<CCValAssign, 16> ArgLocs;
Daniel Sanders41a64c42014-11-07 11:10:48 +00002674 MipsCCState CCInfo(
2675 CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs, *DAG.getContext(),
2676 MipsCCState::getSpecialCallingConvForCallee(Callee.getNode(), Subtarget));
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002677
2678 // Allocate the reserved argument area. It seems strange to do this from the
2679 // caller side but removing it breaks the frame size calculation.
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002680 CCInfo.AllocateStack(ABI.GetCalleeAllocdArgSizeInBytes(CallConv), 1);
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002681
Daniel Sanderscfad1e32014-11-07 11:43:49 +00002682 CCInfo.AnalyzeCallOperands(Outs, CC_Mips, CLI.getArgs(), Callee.getNode());
Wesley Peck527da1b2010-11-23 03:31:01 +00002683
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002684 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka195a1e22011-06-08 17:39:33 +00002685 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka97ba7692012-07-26 23:27:01 +00002686
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002687 // Check if it's really possible to do a tail call.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002688 if (IsTailCall)
Daniel Sanders23e98772014-11-02 16:09:29 +00002689 IsTailCall = isEligibleForTailCallOptimization(
2690 CCInfo, NextStackOffset, *MF.getInfo<MipsFunctionInfo>());
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002691
Reid Kleckner5772b772014-04-24 20:14:34 +00002692 if (!IsTailCall && CLI.CS && CLI.CS->isMustTailCall())
2693 report_fatal_error("failed to perform tail call elimination on a call "
2694 "site marked musttail");
2695
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002696 if (IsTailCall)
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002697 ++NumTailCalls;
2698
Akira Hatanaka79738332011-09-19 20:26:02 +00002699 // Chain is the output chain of the last Load/Store or CopyToReg node.
2700 // ByValChain is the output chain of the last Memcpy node created for copying
2701 // byval arguments to the stack.
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002702 unsigned StackAlignment = TFL->getStackAlignment();
Rui Ueyamada00f2f2016-01-14 21:06:47 +00002703 NextStackOffset = alignTo(NextStackOffset, StackAlignment);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002704 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, DL, true);
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002705
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002706 if (!IsTailCall)
Andrew Trickad6d08a2013-05-29 22:03:55 +00002707 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffsetVal, DL);
Akira Hatanakabeda2242012-07-31 18:46:41 +00002708
Mehdi Amini44ede332015-07-09 02:09:04 +00002709 SDValue StackPtr =
2710 DAG.getCopyFromReg(Chain, DL, ABI.IsN64() ? Mips::SP_64 : Mips::SP,
2711 getPointerTy(DAG.getDataLayout()));
Akira Hatanaka195a1e22011-06-08 17:39:33 +00002712
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002713 std::deque< std::pair<unsigned, SDValue> > RegsToPass;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002714 SmallVector<SDValue, 8> MemOpChains;
Daniel Sanders23e98772014-11-02 16:09:29 +00002715
2716 CCInfo.rewindByValRegsInfo();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002717
2718 // Walk the register/memloc assignments, inserting copies/loads.
2719 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002720 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002721 CCValAssign &VA = ArgLocs[i];
Akira Hatanakab20a3252011-10-28 19:49:00 +00002722 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka19891f82011-11-12 02:34:50 +00002723 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002724 bool UseUpperBits = false;
Akira Hatanaka19891f82011-11-12 02:34:50 +00002725
2726 // ByVal Arg.
2727 if (Flags.isByVal()) {
Daniel Sanders23e98772014-11-02 16:09:29 +00002728 unsigned FirstByValReg, LastByValReg;
2729 unsigned ByValIdx = CCInfo.getInRegsParamsProcessed();
2730 CCInfo.getInRegsParamInfo(ByValIdx, FirstByValReg, LastByValReg);
2731
Akira Hatanaka19891f82011-11-12 02:34:50 +00002732 assert(Flags.getByValSize() &&
2733 "ByVal args of size 0 should have been ignored by front-end.");
Daniel Sanders23e98772014-11-02 16:09:29 +00002734 assert(ByValIdx < CCInfo.getInRegsParamsCount());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002735 assert(!IsTailCall &&
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002736 "Do not tail-call optimize if there is a byval argument.");
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002737 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002738 FirstByValReg, LastByValReg, Flags, Subtarget.isLittle(),
2739 VA);
Daniel Sanders23e98772014-11-02 16:09:29 +00002740 CCInfo.nextInRegsParam();
Akira Hatanaka19891f82011-11-12 02:34:50 +00002741 continue;
2742 }
Jia Liuf54f60f2012-02-28 07:46:26 +00002743
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002744 // Promote the value if needed.
2745 switch (VA.getLocInfo()) {
Daniel Sandersc43cda82014-11-07 16:54:21 +00002746 default:
2747 llvm_unreachable("Unknown loc info!");
Wesley Peck527da1b2010-11-23 03:31:01 +00002748 case CCValAssign::Full:
Akira Hatanakab20a3252011-10-28 19:49:00 +00002749 if (VA.isRegLoc()) {
2750 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
Akira Hatanaka3b7391d2013-03-05 22:20:28 +00002751 (ValVT == MVT::f64 && LocVT == MVT::i64) ||
2752 (ValVT == MVT::i64 && LocVT == MVT::f64))
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002753 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
Akira Hatanakab20a3252011-10-28 19:49:00 +00002754 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002755 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002756 Arg, DAG.getConstant(0, DL, MVT::i32));
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002757 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002758 Arg, DAG.getConstant(1, DL, MVT::i32));
Eric Christopher1c29a652014-07-18 22:55:25 +00002759 if (!Subtarget.isLittle())
Akira Hatanaka27916972011-04-15 19:52:08 +00002760 std::swap(Lo, Hi);
Jia Liuf54f60f2012-02-28 07:46:26 +00002761 unsigned LocRegLo = VA.getLocReg();
Akira Hatanaka61bbcce2011-09-23 00:58:33 +00002762 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2763 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2764 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002765 continue;
Wesley Peck527da1b2010-11-23 03:31:01 +00002766 }
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002767 }
2768 break;
Daniel Sanders23e98772014-11-02 16:09:29 +00002769 case CCValAssign::BCvt:
2770 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
2771 break;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002772 case CCValAssign::SExtUpper:
2773 UseUpperBits = true;
2774 // Fallthrough
Chris Lattner52f16de2008-03-17 06:57:02 +00002775 case CCValAssign::SExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002776 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002777 break;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002778 case CCValAssign::ZExtUpper:
2779 UseUpperBits = true;
2780 // Fallthrough
Chris Lattner52f16de2008-03-17 06:57:02 +00002781 case CCValAssign::ZExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002782 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002783 break;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002784 case CCValAssign::AExtUpper:
2785 UseUpperBits = true;
2786 // Fallthrough
Chris Lattner52f16de2008-03-17 06:57:02 +00002787 case CCValAssign::AExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002788 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002789 break;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002790 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002791
Daniel Sandersc43cda82014-11-07 16:54:21 +00002792 if (UseUpperBits) {
2793 unsigned ValSizeInBits = Outs[i].ArgVT.getSizeInBits();
2794 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
2795 Arg = DAG.getNode(
2796 ISD::SHL, DL, VA.getLocVT(), Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002797 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
Daniel Sandersc43cda82014-11-07 16:54:21 +00002798 }
2799
Wesley Peck527da1b2010-11-23 03:31:01 +00002800 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +00002801 // RegsToPass vector
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002802 if (VA.isRegLoc()) {
2803 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattner52f16de2008-03-17 06:57:02 +00002804 continue;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002805 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002806
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002807 // Register can't get to this point...
Chris Lattner52f16de2008-03-17 06:57:02 +00002808 assert(VA.isMemLoc());
Wesley Peck527da1b2010-11-23 03:31:01 +00002809
Wesley Peck527da1b2010-11-23 03:31:01 +00002810 // emit ISD::STORE whichs stores the
Chris Lattner52f16de2008-03-17 06:57:02 +00002811 // parameter value to a stack Location
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002812 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002813 Chain, Arg, DL, IsTailCall, DAG));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002814 }
2815
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002816 // Transform all store nodes into one single node because all store
2817 // nodes are independent of each other.
Wesley Peck527da1b2010-11-23 03:31:01 +00002818 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00002819 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002820
Bill Wendling24c79f22008-09-16 21:48:12 +00002821 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peck527da1b2010-11-23 03:31:01 +00002822 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2823 // node so that legalize doesn't hack it.
Eric Christopher96e72c62015-01-29 23:27:36 +00002824 bool IsPICCall = (ABI.IsN64() || IsPIC); // true if calls are translated to
2825 // jalr $25
Sasa Stankovic7072a792014-10-01 08:22:21 +00002826 bool GlobalOrExternal = false, InternalLinkage = false, IsCallReloc = false;
Akira Hatanakad6f1c582011-04-07 19:51:44 +00002827 SDValue CalleeLo;
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002828 EVT Ty = Callee.getValueType();
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002829
2830 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002831 if (IsPICCall) {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002832 const GlobalValue *Val = G->getGlobal();
2833 InternalLinkage = Val->hasInternalLinkage();
Akira Hatanakacf9a61b2012-12-13 03:17:29 +00002834
2835 if (InternalLinkage)
Eric Christopher96e72c62015-01-29 23:27:36 +00002836 Callee = getAddrLocal(G, DL, Ty, DAG, ABI.IsN32() || ABI.IsN64());
Sasa Stankovic7072a792014-10-01 08:22:21 +00002837 else if (LargeGOT) {
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002838 Callee = getAddrGlobalLargeGOT(G, DL, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002839 MipsII::MO_CALL_LO16, Chain,
2840 FuncInfo->callPtrInfo(Val));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002841 IsCallReloc = true;
2842 } else {
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002843 Callee = getAddrGlobal(G, DL, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002844 FuncInfo->callPtrInfo(Val));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002845 IsCallReloc = true;
2846 }
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002847 } else
Mehdi Amini44ede332015-07-09 02:09:04 +00002848 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL,
2849 getPointerTy(DAG.getDataLayout()), 0,
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002850 MipsII::MO_NO_FLAG);
Akira Hatanaka8e16aac2011-12-09 01:45:12 +00002851 GlobalOrExternal = true;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002852 }
2853 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002854 const char *Sym = S->getSymbol();
2855
Eric Christopher96e72c62015-01-29 23:27:36 +00002856 if (!ABI.IsN64() && !IsPIC) // !N64 && static
Mehdi Amini44ede332015-07-09 02:09:04 +00002857 Callee = DAG.getTargetExternalSymbol(
2858 Sym, getPointerTy(DAG.getDataLayout()), MipsII::MO_NO_FLAG);
Sasa Stankovic7072a792014-10-01 08:22:21 +00002859 else if (LargeGOT) {
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002860 Callee = getAddrGlobalLargeGOT(S, DL, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002861 MipsII::MO_CALL_LO16, Chain,
2862 FuncInfo->callPtrInfo(Sym));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002863 IsCallReloc = true;
2864 } else { // N64 || PIC
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002865 Callee = getAddrGlobal(S, DL, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002866 FuncInfo->callPtrInfo(Sym));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002867 IsCallReloc = true;
2868 }
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002869
Akira Hatanaka8e16aac2011-12-09 01:45:12 +00002870 GlobalOrExternal = true;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002871 }
2872
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002873 SmallVector<SDValue, 8> Ops(1, Chain);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002874 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002875
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002876 getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal, InternalLinkage,
Sasa Stankovic7072a792014-10-01 08:22:21 +00002877 IsCallReloc, CLI, Callee, Chain);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002878
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002879 if (IsTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +00002880 return DAG.getNode(MipsISD::TailCall, DL, MVT::Other, Ops);
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002881
Craig Topper48d114b2014-04-26 18:35:24 +00002882 Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, Ops);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002883 SDValue InFlag = Chain.getValue(1);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002884
Bruno Cardoso Lopes193e64c2010-01-30 18:32:07 +00002885 // Create the CALLSEQ_END node.
Akira Hatanaka97ba7692012-07-26 23:27:01 +00002886 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002887 DAG.getIntPtrConstant(0, DL, true), InFlag, DL);
Bruno Cardoso Lopes193e64c2010-01-30 18:32:07 +00002888 InFlag = Chain.getValue(1);
2889
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002890 // Handle result values, copying them out of physregs into vregs that we
2891 // return.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002892 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg, Ins, DL, DAG,
2893 InVals, CLI);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002894}
2895
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002896/// LowerCallResult - Lower the result values of a call into the
2897/// appropriate copies out of appropriate physical registers.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002898SDValue MipsTargetLowering::LowerCallResult(
2899 SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool IsVarArg,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002900 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
2901 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals,
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002902 TargetLowering::CallLoweringInfo &CLI) const {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002903 // Assign locations to each value returned by this call.
2904 SmallVector<CCValAssign, 16> RVLocs;
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002905 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
2906 *DAG.getContext());
2907 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips, CLI);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002908
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002909 // Copy all of the result registers out of their specified physreg.
2910 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Daniel Sandersae275e32014-09-25 12:15:05 +00002911 CCValAssign &VA = RVLocs[i];
2912 assert(VA.isRegLoc() && "Can only return in registers!");
2913
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002914 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(),
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002915 RVLocs[i].getLocVT(), InFlag);
2916 Chain = Val.getValue(1);
2917 InFlag = Val.getValue(2);
2918
Daniel Sandersae275e32014-09-25 12:15:05 +00002919 if (VA.isUpperBitsInLoc()) {
2920 unsigned ValSizeInBits = Ins[i].ArgVT.getSizeInBits();
2921 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
2922 unsigned Shift =
2923 VA.getLocInfo() == CCValAssign::ZExtUpper ? ISD::SRL : ISD::SRA;
2924 Val = DAG.getNode(
2925 Shift, DL, VA.getLocVT(), Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002926 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
Daniel Sandersae275e32014-09-25 12:15:05 +00002927 }
2928
2929 switch (VA.getLocInfo()) {
2930 default:
2931 llvm_unreachable("Unknown loc info!");
2932 case CCValAssign::Full:
2933 break;
2934 case CCValAssign::BCvt:
2935 Val = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Val);
2936 break;
2937 case CCValAssign::AExt:
2938 case CCValAssign::AExtUpper:
2939 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2940 break;
2941 case CCValAssign::ZExt:
2942 case CCValAssign::ZExtUpper:
2943 Val = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Val,
2944 DAG.getValueType(VA.getValVT()));
2945 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2946 break;
2947 case CCValAssign::SExt:
2948 case CCValAssign::SExtUpper:
2949 Val = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Val,
2950 DAG.getValueType(VA.getValVT()));
2951 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2952 break;
2953 }
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002954
2955 InVals.push_back(Val);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002956 }
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +00002957
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002958 return Chain;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002959}
2960
Daniel Sandersc43cda82014-11-07 16:54:21 +00002961static SDValue UnpackFromArgumentSlot(SDValue Val, const CCValAssign &VA,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002962 EVT ArgVT, const SDLoc &DL,
2963 SelectionDAG &DAG) {
Daniel Sandersc43cda82014-11-07 16:54:21 +00002964 MVT LocVT = VA.getLocVT();
2965 EVT ValVT = VA.getValVT();
2966
2967 // Shift into the upper bits if necessary.
2968 switch (VA.getLocInfo()) {
2969 default:
2970 break;
2971 case CCValAssign::AExtUpper:
2972 case CCValAssign::SExtUpper:
2973 case CCValAssign::ZExtUpper: {
2974 unsigned ValSizeInBits = ArgVT.getSizeInBits();
2975 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
2976 unsigned Opcode =
2977 VA.getLocInfo() == CCValAssign::ZExtUpper ? ISD::SRL : ISD::SRA;
2978 Val = DAG.getNode(
2979 Opcode, DL, VA.getLocVT(), Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002980 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
Daniel Sandersc43cda82014-11-07 16:54:21 +00002981 break;
2982 }
2983 }
2984
2985 // If this is an value smaller than the argument slot size (32-bit for O32,
2986 // 64-bit for N32/N64), it has been promoted in some way to the argument slot
2987 // size. Extract the value and insert any appropriate assertions regarding
2988 // sign/zero extension.
2989 switch (VA.getLocInfo()) {
2990 default:
2991 llvm_unreachable("Unknown loc info!");
2992 case CCValAssign::Full:
2993 break;
2994 case CCValAssign::AExtUpper:
2995 case CCValAssign::AExt:
2996 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
2997 break;
2998 case CCValAssign::SExtUpper:
2999 case CCValAssign::SExt:
3000 Val = DAG.getNode(ISD::AssertSext, DL, LocVT, Val, DAG.getValueType(ValVT));
3001 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
3002 break;
3003 case CCValAssign::ZExtUpper:
3004 case CCValAssign::ZExt:
3005 Val = DAG.getNode(ISD::AssertZext, DL, LocVT, Val, DAG.getValueType(ValVT));
3006 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
3007 break;
3008 case CCValAssign::BCvt:
3009 Val = DAG.getNode(ISD::BITCAST, DL, ValVT, Val);
3010 break;
3011 }
3012
3013 return Val;
3014}
3015
Akira Hatanakae2489122011-04-15 21:51:11 +00003016//===----------------------------------------------------------------------===//
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003017// Formal Arguments Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00003018//===----------------------------------------------------------------------===//
Wesley Peck527da1b2010-11-23 03:31:01 +00003019/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003020/// and generate load operations for arguments places on the stack.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003021SDValue MipsTargetLowering::LowerFormalArguments(
3022 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
3023 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
3024 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Bruno Cardoso Lopesa01ede22008-08-04 07:12:52 +00003025 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003026 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopes14033fb2007-08-28 05:08:16 +00003027 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00003028
Dan Gohman31ae5862010-04-17 14:41:14 +00003029 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003030
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003031 // Used with vargs to acumulate store chains.
3032 std::vector<SDValue> OutChains;
3033
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003034 // Assign locations to all of the incoming arguments.
3035 SmallVector<CCValAssign, 16> ArgLocs;
Daniel Sanders23e98772014-11-02 16:09:29 +00003036 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
3037 *DAG.getContext());
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003038 CCInfo.AllocateStack(ABI.GetCalleeAllocdArgSizeInBytes(CallConv), 1);
Vasileios Kalintiris43dff0c2015-10-26 12:38:43 +00003039 const Function *Func = DAG.getMachineFunction().getFunction();
3040 Function::const_arg_iterator FuncArg = Func->arg_begin();
3041
Vasileios Kalintiris165121f2015-10-26 14:24:30 +00003042 if (Func->hasFnAttribute("interrupt") && !Func->arg_empty())
3043 report_fatal_error(
3044 "Functions with the interrupt attribute cannot have arguments!");
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00003045
Daniel Sandersb70e27c2014-11-06 16:36:30 +00003046 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips_FixedArg);
Akira Hatanaka4866fe12012-10-30 19:37:25 +00003047 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(),
Daniel Sanders23e98772014-11-02 16:09:29 +00003048 CCInfo.getInRegsParamsCount() > 0);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00003049
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00003050 unsigned CurArgIdx = 0;
Daniel Sanders23e98772014-11-02 16:09:29 +00003051 CCInfo.rewindByValRegsInfo();
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003052
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00003053 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003054 CCValAssign &VA = ArgLocs[i];
Andrew Trick05938a52015-02-16 18:10:47 +00003055 if (Ins[i].isOrigArg()) {
3056 std::advance(FuncArg, Ins[i].getOrigArgIndex() - CurArgIdx);
3057 CurArgIdx = Ins[i].getOrigArgIndex();
3058 }
Akira Hatanaka104b7e32011-10-28 19:55:48 +00003059 EVT ValVT = VA.getValVT();
Akira Hatanakafb9bae32011-11-12 02:29:58 +00003060 ISD::ArgFlagsTy Flags = Ins[i].Flags;
3061 bool IsRegLoc = VA.isRegLoc();
3062
3063 if (Flags.isByVal()) {
Andrew Trick05938a52015-02-16 18:10:47 +00003064 assert(Ins[i].isOrigArg() && "Byval arguments cannot be implicit");
Daniel Sanders23e98772014-11-02 16:09:29 +00003065 unsigned FirstByValReg, LastByValReg;
3066 unsigned ByValIdx = CCInfo.getInRegsParamsProcessed();
3067 CCInfo.getInRegsParamInfo(ByValIdx, FirstByValReg, LastByValReg);
3068
Akira Hatanakafb9bae32011-11-12 02:29:58 +00003069 assert(Flags.getByValSize() &&
3070 "ByVal args of size 0 should have been ignored by front-end.");
Daniel Sanders23e98772014-11-02 16:09:29 +00003071 assert(ByValIdx < CCInfo.getInRegsParamsCount());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003072 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003073 FirstByValReg, LastByValReg, VA, CCInfo);
Daniel Sanders23e98772014-11-02 16:09:29 +00003074 CCInfo.nextInRegsParam();
Akira Hatanakafb9bae32011-11-12 02:29:58 +00003075 continue;
3076 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003077
3078 // Arguments stored on registers
Akira Hatanakafb9bae32011-11-12 02:29:58 +00003079 if (IsRegLoc) {
Akira Hatanaka7d822522013-10-28 21:21:36 +00003080 MVT RegVT = VA.getLocVT();
Akira Hatanakacb4a1a82011-05-24 00:23:52 +00003081 unsigned ArgReg = VA.getLocReg();
Akira Hatanaka7d822522013-10-28 21:21:36 +00003082 const TargetRegisterClass *RC = getRegClassFor(RegVT);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003083
Wesley Peck527da1b2010-11-23 03:31:01 +00003084 // Transform the arguments stored on
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003085 // physical registers into virtual ones
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003086 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC);
3087 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
Wesley Peck527da1b2010-11-23 03:31:01 +00003088
Daniel Sandersc43cda82014-11-07 16:54:21 +00003089 ArgValue = UnpackFromArgumentSlot(ArgValue, VA, Ins[i].ArgVT, DL, DAG);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00003090
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003091 // Handle floating point arguments passed in integer registers and
3092 // long double arguments passed in floating point registers.
Akira Hatanaka104b7e32011-10-28 19:55:48 +00003093 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003094 (RegVT == MVT::i64 && ValVT == MVT::f64) ||
3095 (RegVT == MVT::f64 && ValVT == MVT::i64))
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003096 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
Eric Christopher96e72c62015-01-29 23:27:36 +00003097 else if (ABI.IsO32() && RegVT == MVT::i32 &&
Eric Christopherbf33a3c2014-07-02 23:18:40 +00003098 ValVT == MVT::f64) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003099 unsigned Reg2 = addLiveIn(DAG.getMachineFunction(),
Akira Hatanaka104b7e32011-10-28 19:55:48 +00003100 getNextIntArgReg(ArgReg), RC);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003101 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT);
Eric Christopher1c29a652014-07-18 22:55:25 +00003102 if (!Subtarget.isLittle())
Akira Hatanaka104b7e32011-10-28 19:55:48 +00003103 std::swap(ArgValue, ArgValue2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003104 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64,
Akira Hatanaka104b7e32011-10-28 19:55:48 +00003105 ArgValue, ArgValue2);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00003106 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003107
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003108 InVals.push_back(ArgValue);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003109 } else { // VA.isRegLoc()
Daniel Sandersc43cda82014-11-07 16:54:21 +00003110 MVT LocVT = VA.getLocVT();
3111
Eric Christopher96e72c62015-01-29 23:27:36 +00003112 if (ABI.IsO32()) {
Daniel Sandersc43cda82014-11-07 16:54:21 +00003113 // We ought to be able to use LocVT directly but O32 sets it to i32
3114 // when allocating floating point values to integer registers.
3115 // This shouldn't influence how we load the value into registers unless
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00003116 // we are targeting softfloat.
Eric Christophere8ae3e32015-05-07 23:10:21 +00003117 if (VA.getValVT().isFloatingPoint() && !Subtarget.useSoftFloat())
Daniel Sandersc43cda82014-11-07 16:54:21 +00003118 LocVT = VA.getValVT();
3119 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003120
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003121 // sanity check
3122 assert(VA.isMemLoc());
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003123
Wesley Peck527da1b2010-11-23 03:31:01 +00003124 // The stack pointer offset is relative to the caller stack frame.
Daniel Sandersc43cda82014-11-07 16:54:21 +00003125 int FI = MFI->CreateFixedObject(LocVT.getSizeInBits() / 8,
Akira Hatanakacb4a1a82011-05-24 00:23:52 +00003126 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003127
3128 // Create load nodes to retrieve arguments from the stack
Mehdi Amini44ede332015-07-09 02:09:04 +00003129 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Alex Lorenze40c8a22015-08-11 23:09:45 +00003130 SDValue ArgValue = DAG.getLoad(
3131 LocVT, DL, Chain, FIN,
3132 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI),
3133 false, false, false, 0);
Daniel Sandersc43cda82014-11-07 16:54:21 +00003134 OutChains.push_back(ArgValue.getValue(1));
3135
3136 ArgValue = UnpackFromArgumentSlot(ArgValue, VA, Ins[i].ArgVT, DL, DAG);
3137
3138 InVals.push_back(ArgValue);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003139 }
Reid Kleckner7a59e082014-05-12 22:01:27 +00003140 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003141
Reid Kleckner7a59e082014-05-12 22:01:27 +00003142 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Reid Kleckner79418562014-05-09 22:32:13 +00003143 // The mips ABIs for returning structs by value requires that we copy
3144 // the sret argument into $v0 for the return. Save the argument into
3145 // a virtual register so that we can access it from the return points.
Reid Kleckner7a59e082014-05-12 22:01:27 +00003146 if (Ins[i].Flags.isSRet()) {
Reid Kleckner79418562014-05-09 22:32:13 +00003147 unsigned Reg = MipsFI->getSRetReturnReg();
3148 if (!Reg) {
3149 Reg = MF.getRegInfo().createVirtualRegister(
Eric Christopher96e72c62015-01-29 23:27:36 +00003150 getRegClassFor(ABI.IsN64() ? MVT::i64 : MVT::i32));
Reid Kleckner79418562014-05-09 22:32:13 +00003151 MipsFI->setSRetReturnReg(Reg);
3152 }
Reid Kleckner7a59e082014-05-12 22:01:27 +00003153 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[i]);
Reid Kleckner79418562014-05-09 22:32:13 +00003154 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
Reid Kleckner7a59e082014-05-12 22:01:27 +00003155 break;
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003156 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003157 }
3158
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003159 if (IsVarArg)
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003160 writeVarArgRegs(OutChains, Chain, DL, DAG, CCInfo);
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003161
Wesley Peck527da1b2010-11-23 03:31:01 +00003162 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003163 // the size of Ins and InVals. This only happens when on varg functions
3164 if (!OutChains.empty()) {
3165 OutChains.push_back(Chain);
Craig Topper48d114b2014-04-26 18:35:24 +00003166 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003167 }
3168
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003169 return Chain;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003170}
3171
Akira Hatanakae2489122011-04-15 21:51:11 +00003172//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003173// Return Value Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00003174//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003175
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003176bool
3177MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003178 MachineFunction &MF, bool IsVarArg,
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003179 const SmallVectorImpl<ISD::OutputArg> &Outs,
3180 LLVMContext &Context) const {
3181 SmallVector<CCValAssign, 16> RVLocs;
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003182 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003183 return CCInfo.CheckReturn(Outs, RetCC_Mips);
3184}
3185
Petar Jovanovic5b436222015-03-23 12:28:13 +00003186bool
3187MipsTargetLowering::shouldSignExtendTypeInLibCall(EVT Type, bool IsSigned) const {
Eric Christophere8ae3e32015-05-07 23:10:21 +00003188 if (Subtarget.hasMips3() && Subtarget.useSoftFloat()) {
Petar Jovanovic5b436222015-03-23 12:28:13 +00003189 if (Type == MVT::i32)
3190 return true;
3191 }
3192 return IsSigned;
3193}
3194
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003195SDValue
Vasileios Kalintiris43dff0c2015-10-26 12:38:43 +00003196MipsTargetLowering::LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003197 const SDLoc &DL,
3198 SelectionDAG &DAG) const {
Vasileios Kalintiris43dff0c2015-10-26 12:38:43 +00003199
3200 MachineFunction &MF = DAG.getMachineFunction();
3201 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3202
3203 MipsFI->setISR();
3204
3205 return DAG.getNode(MipsISD::ERet, DL, MVT::Other, RetOps);
3206}
3207
3208SDValue
3209MipsTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
3210 bool IsVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003211 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003212 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003213 const SDLoc &DL, SelectionDAG &DAG) const {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003214 // CCValAssign - represent the assignment of
3215 // the return value to a location
3216 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003217 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003218
3219 // CCState - Info about the registers and stack slot.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003220 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, *DAG.getContext());
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003221
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003222 // Analyze return values.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003223 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003224
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003225 SDValue Flag;
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003226 SmallVector<SDValue, 4> RetOps(1, Chain);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003227
3228 // Copy the result values into the output registers.
3229 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003230 SDValue Val = OutVals[i];
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003231 CCValAssign &VA = RVLocs[i];
3232 assert(VA.isRegLoc() && "Can only return in registers!");
Daniel Sandersae275e32014-09-25 12:15:05 +00003233 bool UseUpperBits = false;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003234
Daniel Sandersae275e32014-09-25 12:15:05 +00003235 switch (VA.getLocInfo()) {
3236 default:
3237 llvm_unreachable("Unknown loc info!");
3238 case CCValAssign::Full:
3239 break;
3240 case CCValAssign::BCvt:
3241 Val = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Val);
3242 break;
3243 case CCValAssign::AExtUpper:
3244 UseUpperBits = true;
3245 // Fallthrough
3246 case CCValAssign::AExt:
3247 Val = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Val);
3248 break;
3249 case CCValAssign::ZExtUpper:
3250 UseUpperBits = true;
3251 // Fallthrough
3252 case CCValAssign::ZExt:
3253 Val = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Val);
3254 break;
3255 case CCValAssign::SExtUpper:
3256 UseUpperBits = true;
3257 // Fallthrough
3258 case CCValAssign::SExt:
3259 Val = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Val);
3260 break;
3261 }
3262
3263 if (UseUpperBits) {
3264 unsigned ValSizeInBits = Outs[i].ArgVT.getSizeInBits();
3265 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
3266 Val = DAG.getNode(
3267 ISD::SHL, DL, VA.getLocVT(), Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003268 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
Daniel Sandersae275e32014-09-25 12:15:05 +00003269 }
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003270
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003271 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003272
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003273 // Guarantee that all emitted copies are stuck together with flags.
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003274 Flag = Chain.getValue(1);
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003275 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003276 }
3277
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003278 // The mips ABIs for returning structs by value requires that we copy
3279 // the sret argument into $v0 for the return. We saved the argument into
3280 // a virtual register in the entry block, so now we copy the value out
3281 // and into $v0.
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003282 if (MF.getFunction()->hasStructRetAttr()) {
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003283 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3284 unsigned Reg = MipsFI->getSRetReturnReg();
3285
Wesley Peck527da1b2010-11-23 03:31:01 +00003286 if (!Reg)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003287 llvm_unreachable("sret virtual register not created in the entry block");
Mehdi Amini44ede332015-07-09 02:09:04 +00003288 SDValue Val =
3289 DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(DAG.getDataLayout()));
Eric Christopher96e72c62015-01-29 23:27:36 +00003290 unsigned V0 = ABI.IsN64() ? Mips::V0_64 : Mips::V0;
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003291
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003292 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003293 Flag = Chain.getValue(1);
Mehdi Amini44ede332015-07-09 02:09:04 +00003294 RetOps.push_back(DAG.getRegister(V0, getPointerTy(DAG.getDataLayout())));
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003295 }
3296
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003297 RetOps[0] = Chain; // Update chain.
Akira Hatanakaefff7b72012-07-10 00:19:06 +00003298
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003299 // Add the flag if we have it.
3300 if (Flag.getNode())
3301 RetOps.push_back(Flag);
3302
Vasileios Kalintiris43dff0c2015-10-26 12:38:43 +00003303 // ISRs must use "eret".
3304 if (DAG.getMachineFunction().getFunction()->hasFnAttribute("interrupt"))
3305 return LowerInterruptReturn(RetOps, DL, DAG);
3306
3307 // Standard return on Mips is a "jr $ra"
Craig Topper48d114b2014-04-26 18:35:24 +00003308 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, RetOps);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003309}
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003310
Akira Hatanakae2489122011-04-15 21:51:11 +00003311//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003312// Mips Inline Assembly Support
Akira Hatanakae2489122011-04-15 21:51:11 +00003313//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003314
3315/// getConstraintType - Given a constraint letter, return the type of
3316/// constraint it is for this target.
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003317MipsTargetLowering::ConstraintType
3318MipsTargetLowering::getConstraintType(StringRef Constraint) const {
Daniel Sanders8b59af12013-11-12 12:56:01 +00003319 // Mips specific constraints
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003320 // GCC config/mips/constraints.md
3321 //
Wesley Peck527da1b2010-11-23 03:31:01 +00003322 // 'd' : An address register. Equivalent to r
3323 // unless generating MIPS16 code.
3324 // 'y' : Equivalent to r; retained for
3325 // backwards compatibility.
Eric Christophere3c494d2012-05-07 06:25:10 +00003326 // 'c' : A register suitable for use in an indirect
3327 // jump. This will always be $25 for -mabicalls.
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003328 // 'l' : The lo register. 1 word storage.
3329 // 'x' : The hilo register pair. Double word storage.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003330 if (Constraint.size() == 1) {
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003331 switch (Constraint[0]) {
3332 default : break;
Wesley Peck527da1b2010-11-23 03:31:01 +00003333 case 'd':
3334 case 'y':
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003335 case 'f':
Eric Christophere3c494d2012-05-07 06:25:10 +00003336 case 'c':
Eric Christopher9c492e62012-05-07 06:25:15 +00003337 case 'l':
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003338 case 'x':
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003339 return C_RegisterClass;
Jack Carter0e149b02013-03-04 21:33:15 +00003340 case 'R':
3341 return C_Memory;
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003342 }
3343 }
Daniel Sandersa73d8fe2015-03-24 11:26:34 +00003344
3345 if (Constraint == "ZC")
3346 return C_Memory;
3347
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003348 return TargetLowering::getConstraintType(Constraint);
3349}
3350
John Thompsone8360b72010-10-29 17:29:13 +00003351/// Examine constraint type and operand type and determine a weight value.
3352/// This object must already have been set up with the operand type
3353/// and the current alternative constraint selected.
3354TargetLowering::ConstraintWeight
3355MipsTargetLowering::getSingleConstraintMatchWeight(
3356 AsmOperandInfo &info, const char *constraint) const {
3357 ConstraintWeight weight = CW_Invalid;
3358 Value *CallOperandVal = info.CallOperandVal;
3359 // If we don't have a value, we can't do a match,
3360 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +00003361 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +00003362 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +00003363 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +00003364 // Look at the constraint type.
3365 switch (*constraint) {
3366 default:
3367 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
3368 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00003369 case 'd':
3370 case 'y':
John Thompsone8360b72010-10-29 17:29:13 +00003371 if (type->isIntegerTy())
3372 weight = CW_Register;
3373 break;
Daniel Sanders8b59af12013-11-12 12:56:01 +00003374 case 'f': // FPU or MSA register
Eric Christopher1c29a652014-07-18 22:55:25 +00003375 if (Subtarget.hasMSA() && type->isVectorTy() &&
Daniel Sanders8b59af12013-11-12 12:56:01 +00003376 cast<VectorType>(type)->getBitWidth() == 128)
3377 weight = CW_Register;
3378 else if (type->isFloatTy())
John Thompsone8360b72010-10-29 17:29:13 +00003379 weight = CW_Register;
3380 break;
Eric Christophere3c494d2012-05-07 06:25:10 +00003381 case 'c': // $25 for indirect jumps
Eric Christopher9c492e62012-05-07 06:25:15 +00003382 case 'l': // lo register
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003383 case 'x': // hilo register pair
Daniel Sanders8b59af12013-11-12 12:56:01 +00003384 if (type->isIntegerTy())
Eric Christophere3c494d2012-05-07 06:25:10 +00003385 weight = CW_SpecificReg;
Daniel Sanders8b59af12013-11-12 12:56:01 +00003386 break;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003387 case 'I': // signed 16 bit immediate
Eric Christopher7201e1b2012-05-07 03:13:42 +00003388 case 'J': // integer zero
Eric Christopher3ff88a02012-05-07 05:46:29 +00003389 case 'K': // unsigned 16 bit immediate
Eric Christopher1109b342012-05-07 05:46:37 +00003390 case 'L': // signed 32 bit immediate where lower 16 bits are 0
Eric Christophere07aa432012-05-07 05:46:43 +00003391 case 'N': // immediate in the range of -65535 to -1 (inclusive)
Eric Christopher470578a2012-05-07 05:46:48 +00003392 case 'O': // signed 15 bit immediate (+- 16383)
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003393 case 'P': // immediate in the range of 65535 to 1 (inclusive)
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003394 if (isa<ConstantInt>(CallOperandVal))
3395 weight = CW_Constant;
3396 break;
Jack Carter0e149b02013-03-04 21:33:15 +00003397 case 'R':
3398 weight = CW_Memory;
3399 break;
John Thompsone8360b72010-10-29 17:29:13 +00003400 }
3401 return weight;
3402}
3403
Akira Hatanaka7473b472013-08-14 00:21:25 +00003404/// This is a helper function to parse a physical register string and split it
3405/// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag
3406/// that is returned indicates whether parsing was successful. The second flag
3407/// is true if the numeric part exists.
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003408static std::pair<bool, bool> parsePhysicalReg(StringRef C, StringRef &Prefix,
3409 unsigned long long &Reg) {
Akira Hatanaka7473b472013-08-14 00:21:25 +00003410 if (C.front() != '{' || C.back() != '}')
3411 return std::make_pair(false, false);
3412
3413 // Search for the first numeric character.
3414 StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1;
Craig Topper2241dfd2015-11-23 07:19:06 +00003415 I = std::find_if(B, E, isdigit);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003416
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003417 Prefix = StringRef(B, I - B);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003418
3419 // The second flag is set to false if no numeric characters were found.
3420 if (I == E)
3421 return std::make_pair(true, false);
3422
3423 // Parse the numeric characters.
3424 return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg),
3425 true);
3426}
3427
3428std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering::
Craig Topper6dc4a8bc2014-08-30 16:48:02 +00003429parseRegForInlineAsmConstraint(StringRef C, MVT VT) const {
Eric Christopherd9134482014-08-04 21:25:23 +00003430 const TargetRegisterInfo *TRI =
Eric Christopher96e72c62015-01-29 23:27:36 +00003431 Subtarget.getRegisterInfo();
Akira Hatanaka7473b472013-08-14 00:21:25 +00003432 const TargetRegisterClass *RC;
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003433 StringRef Prefix;
Akira Hatanaka7473b472013-08-14 00:21:25 +00003434 unsigned long long Reg;
3435
3436 std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg);
3437
3438 if (!R.first)
Craig Topper062a2ba2014-04-25 05:30:21 +00003439 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003440
3441 if ((Prefix == "hi" || Prefix == "lo")) { // Parse hi/lo.
3442 // No numeric characters follow "hi" or "lo".
3443 if (R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003444 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003445
3446 RC = TRI->getRegClass(Prefix == "hi" ?
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00003447 Mips::HI32RegClassID : Mips::LO32RegClassID);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003448 return std::make_pair(*(RC->begin()), RC);
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003449 } else if (Prefix.startswith("$msa")) {
Daniel Sanders8b59af12013-11-12 12:56:01 +00003450 // Parse $msa(ir|csr|access|save|modify|request|map|unmap)
3451
3452 // No numeric characters follow the name.
3453 if (R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003454 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003455
3456 Reg = StringSwitch<unsigned long long>(Prefix)
3457 .Case("$msair", Mips::MSAIR)
3458 .Case("$msacsr", Mips::MSACSR)
3459 .Case("$msaaccess", Mips::MSAAccess)
3460 .Case("$msasave", Mips::MSASave)
3461 .Case("$msamodify", Mips::MSAModify)
3462 .Case("$msarequest", Mips::MSARequest)
3463 .Case("$msamap", Mips::MSAMap)
3464 .Case("$msaunmap", Mips::MSAUnmap)
3465 .Default(0);
3466
3467 if (!Reg)
Craig Topper062a2ba2014-04-25 05:30:21 +00003468 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003469
3470 RC = TRI->getRegClass(Mips::MSACtrlRegClassID);
3471 return std::make_pair(Reg, RC);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003472 }
3473
3474 if (!R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003475 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003476
3477 if (Prefix == "$f") { // Parse $f0-$f31.
3478 // If the size of FP registers is 64-bit or Reg is an even number, select
3479 // the 64-bit register class. Otherwise, select the 32-bit register class.
3480 if (VT == MVT::Other)
Eric Christopher1c29a652014-07-18 22:55:25 +00003481 VT = (Subtarget.isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32;
Akira Hatanaka7473b472013-08-14 00:21:25 +00003482
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003483 RC = getRegClassFor(VT);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003484
3485 if (RC == &Mips::AFGR64RegClass) {
3486 assert(Reg % 2 == 0);
3487 Reg >>= 1;
3488 }
Daniel Sanders8b59af12013-11-12 12:56:01 +00003489 } else if (Prefix == "$fcc") // Parse $fcc0-$fcc7.
Akira Hatanaka7473b472013-08-14 00:21:25 +00003490 RC = TRI->getRegClass(Mips::FCCRegClassID);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003491 else if (Prefix == "$w") { // Parse $w0-$w31.
3492 RC = getRegClassFor((VT == MVT::Other) ? MVT::v16i8 : VT);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003493 } else { // Parse $0-$31.
3494 assert(Prefix == "$");
3495 RC = getRegClassFor((VT == MVT::Other) ? MVT::i32 : VT);
3496 }
3497
3498 assert(Reg < RC->getNumRegs());
3499 return std::make_pair(*(RC->begin() + Reg), RC);
3500}
3501
Eric Christophereaf77dc2011-06-29 19:33:04 +00003502/// Given a register class constraint, like 'r', if this corresponds directly
3503/// to an LLVM register class, return a register of 0 and the register class
3504/// pointer.
Eric Christopher11e4df72015-02-26 22:38:43 +00003505std::pair<unsigned, const TargetRegisterClass *>
3506MipsTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003507 StringRef Constraint,
Eric Christopher11e4df72015-02-26 22:38:43 +00003508 MVT VT) const {
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003509 if (Constraint.size() == 1) {
3510 switch (Constraint[0]) {
Eric Christopher9519c082011-06-29 19:04:31 +00003511 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
3512 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003513 case 'r':
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003514 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
Eric Christopher1c29a652014-07-18 22:55:25 +00003515 if (Subtarget.inMips16Mode())
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003516 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003517 return std::make_pair(0U, &Mips::GPR32RegClass);
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003518 }
Eric Christopher1c29a652014-07-18 22:55:25 +00003519 if (VT == MVT::i64 && !Subtarget.isGP64bit())
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003520 return std::make_pair(0U, &Mips::GPR32RegClass);
Eric Christopher1c29a652014-07-18 22:55:25 +00003521 if (VT == MVT::i64 && Subtarget.isGP64bit())
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003522 return std::make_pair(0U, &Mips::GPR64RegClass);
Eric Christopher58daf042012-05-07 03:13:22 +00003523 // This will generate an error message
Craig Topper062a2ba2014-04-25 05:30:21 +00003524 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003525 case 'f': // FPU or MSA register
3526 if (VT == MVT::v16i8)
3527 return std::make_pair(0U, &Mips::MSA128BRegClass);
3528 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
3529 return std::make_pair(0U, &Mips::MSA128HRegClass);
3530 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
3531 return std::make_pair(0U, &Mips::MSA128WRegClass);
3532 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
3533 return std::make_pair(0U, &Mips::MSA128DRegClass);
3534 else if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00003535 return std::make_pair(0U, &Mips::FGR32RegClass);
Eric Christopher1c29a652014-07-18 22:55:25 +00003536 else if ((VT == MVT::f64) && (!Subtarget.isSingleFloat())) {
3537 if (Subtarget.isFP64bit())
Craig Topperc7242e02012-04-20 07:30:17 +00003538 return std::make_pair(0U, &Mips::FGR64RegClass);
3539 return std::make_pair(0U, &Mips::AFGR64RegClass);
Akira Hatanakac669d7a2012-01-04 02:45:01 +00003540 }
Eric Christophere3c494d2012-05-07 06:25:10 +00003541 break;
3542 case 'c': // register suitable for indirect jump
3543 if (VT == MVT::i32)
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003544 return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass);
Eric Christophere3c494d2012-05-07 06:25:10 +00003545 assert(VT == MVT::i64 && "Unexpected type.");
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003546 return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass);
Eric Christopher9c492e62012-05-07 06:25:15 +00003547 case 'l': // register suitable for indirect jump
3548 if (VT == MVT::i32)
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00003549 return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass);
3550 return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass);
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003551 case 'x': // register suitable for indirect jump
3552 // Fixme: Not triggering the use of both hi and low
3553 // This will generate an error message
Craig Topper062a2ba2014-04-25 05:30:21 +00003554 return std::make_pair(0U, nullptr);
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003555 }
3556 }
Akira Hatanaka7473b472013-08-14 00:21:25 +00003557
3558 std::pair<unsigned, const TargetRegisterClass *> R;
3559 R = parseRegForInlineAsmConstraint(Constraint, VT);
3560
3561 if (R.second)
3562 return R;
3563
Eric Christopher11e4df72015-02-26 22:38:43 +00003564 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003565}
3566
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003567/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3568/// vector. If it is invalid, don't add anything to Ops.
3569void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3570 std::string &Constraint,
3571 std::vector<SDValue>&Ops,
3572 SelectionDAG &DAG) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003573 SDLoc DL(Op);
Craig Topper062a2ba2014-04-25 05:30:21 +00003574 SDValue Result;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003575
3576 // Only support length 1 constraints for now.
3577 if (Constraint.length() > 1) return;
3578
3579 char ConstraintLetter = Constraint[0];
3580 switch (ConstraintLetter) {
3581 default: break; // This will fall through to the generic implementation
3582 case 'I': // Signed 16 bit constant
3583 // If this fails, the parent routine will give an error
3584 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3585 EVT Type = Op.getValueType();
3586 int64_t Val = C->getSExtValue();
3587 if (isInt<16>(Val)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003588 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003589 break;
3590 }
3591 }
3592 return;
Eric Christopher7201e1b2012-05-07 03:13:42 +00003593 case 'J': // integer zero
3594 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3595 EVT Type = Op.getValueType();
3596 int64_t Val = C->getZExtValue();
3597 if (Val == 0) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003598 Result = DAG.getTargetConstant(0, DL, Type);
Eric Christopher7201e1b2012-05-07 03:13:42 +00003599 break;
3600 }
3601 }
3602 return;
Eric Christopher3ff88a02012-05-07 05:46:29 +00003603 case 'K': // unsigned 16 bit immediate
3604 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3605 EVT Type = Op.getValueType();
3606 uint64_t Val = (uint64_t)C->getZExtValue();
3607 if (isUInt<16>(Val)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003608 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopher3ff88a02012-05-07 05:46:29 +00003609 break;
3610 }
3611 }
3612 return;
Eric Christopher1109b342012-05-07 05:46:37 +00003613 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3614 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3615 EVT Type = Op.getValueType();
3616 int64_t Val = C->getSExtValue();
3617 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003618 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopher1109b342012-05-07 05:46:37 +00003619 break;
3620 }
3621 }
3622 return;
Eric Christophere07aa432012-05-07 05:46:43 +00003623 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3624 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3625 EVT Type = Op.getValueType();
3626 int64_t Val = C->getSExtValue();
3627 if ((Val >= -65535) && (Val <= -1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003628 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christophere07aa432012-05-07 05:46:43 +00003629 break;
3630 }
3631 }
3632 return;
Eric Christopher470578a2012-05-07 05:46:48 +00003633 case 'O': // signed 15 bit immediate
3634 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3635 EVT Type = Op.getValueType();
3636 int64_t Val = C->getSExtValue();
3637 if ((isInt<15>(Val))) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003638 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopher470578a2012-05-07 05:46:48 +00003639 break;
3640 }
3641 }
3642 return;
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003643 case 'P': // immediate in the range of 1 to 65535 (inclusive)
3644 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3645 EVT Type = Op.getValueType();
3646 int64_t Val = C->getSExtValue();
3647 if ((Val <= 65535) && (Val >= 1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003648 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003649 break;
3650 }
3651 }
3652 return;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003653 }
3654
3655 if (Result.getNode()) {
3656 Ops.push_back(Result);
3657 return;
3658 }
3659
3660 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3661}
3662
Mehdi Amini0cdec1e2015-07-09 02:09:40 +00003663bool MipsTargetLowering::isLegalAddressingMode(const DataLayout &DL,
3664 const AddrMode &AM, Type *Ty,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +00003665 unsigned AS) const {
Akira Hatanakaef839192012-11-17 00:25:41 +00003666 // No global is ever allowed as a base.
3667 if (AM.BaseGV)
3668 return false;
3669
3670 switch (AM.Scale) {
3671 case 0: // "r+i" or just "i", depending on HasBaseReg.
3672 break;
3673 case 1:
3674 if (!AM.HasBaseReg) // allow "r+i".
3675 break;
3676 return false; // disallow "r+r" or "r+r+i".
3677 default:
3678 return false;
3679 }
3680
3681 return true;
3682}
3683
3684bool
Dan Gohman2fe6bee2008-10-18 02:06:02 +00003685MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3686 // The Mips target isn't yet aware of offsets.
3687 return false;
3688}
Evan Cheng16993aa2009-10-27 19:56:55 +00003689
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003690EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003691 unsigned SrcAlign,
3692 bool IsMemset, bool ZeroMemset,
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003693 bool MemcpyStrSrc,
3694 MachineFunction &MF) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003695 if (Subtarget.hasMips64())
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003696 return MVT::i64;
3697
3698 return MVT::i32;
3699}
3700
Evan Cheng83896a52009-10-28 01:43:28 +00003701bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
3702 if (VT != MVT::f32 && VT != MVT::f64)
3703 return false;
Bruno Cardoso Lopesb02a9df2011-01-18 19:41:41 +00003704 if (Imm.isNegZero())
3705 return false;
Evan Cheng16993aa2009-10-27 19:56:55 +00003706 return Imm.isZero();
3707}
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003708
3709unsigned MipsTargetLowering::getJumpTableEncoding() const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003710 if (ABI.IsN64())
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003711 return MachineJumpTableInfo::EK_GPRel64BlockAddress;
Jia Liuf54f60f2012-02-28 07:46:26 +00003712
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003713 return TargetLowering::getJumpTableEncoding();
3714}
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003715
Eric Christopher824f42f2015-05-12 01:26:05 +00003716bool MipsTargetLowering::useSoftFloat() const {
3717 return Subtarget.useSoftFloat();
3718}
3719
Daniel Sandersf43e6872014-11-01 18:44:56 +00003720void MipsTargetLowering::copyByValRegs(
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003721 SDValue Chain, const SDLoc &DL, std::vector<SDValue> &OutChains,
3722 SelectionDAG &DAG, const ISD::ArgFlagsTy &Flags,
3723 SmallVectorImpl<SDValue> &InVals, const Argument *FuncArg,
3724 unsigned FirstReg, unsigned LastReg, const CCValAssign &VA,
3725 MipsCCState &State) const {
Akira Hatanaka25dad192012-10-27 00:10:18 +00003726 MachineFunction &MF = DAG.getMachineFunction();
3727 MachineFrameInfo *MFI = MF.getFrameInfo();
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003728 unsigned GPRSizeInBytes = Subtarget.getGPRSizeInBytes();
Daniel Sanders23e98772014-11-02 16:09:29 +00003729 unsigned NumRegs = LastReg - FirstReg;
3730 unsigned RegAreaSize = NumRegs * GPRSizeInBytes;
Akira Hatanaka25dad192012-10-27 00:10:18 +00003731 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
3732 int FrameObjOffset;
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003733 ArrayRef<MCPhysReg> ByValArgRegs = ABI.GetByValArgRegs();
Akira Hatanaka25dad192012-10-27 00:10:18 +00003734
3735 if (RegAreaSize)
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003736 FrameObjOffset =
3737 (int)ABI.GetCalleeAllocdArgSizeInBytes(State.getCallingConv()) -
3738 (int)((ByValArgRegs.size() - FirstReg) * GPRSizeInBytes);
Akira Hatanaka25dad192012-10-27 00:10:18 +00003739 else
Daniel Sandersf43e6872014-11-01 18:44:56 +00003740 FrameObjOffset = VA.getLocMemOffset();
Akira Hatanaka25dad192012-10-27 00:10:18 +00003741
3742 // Create frame object.
Mehdi Amini44ede332015-07-09 02:09:04 +00003743 EVT PtrTy = getPointerTy(DAG.getDataLayout());
Akira Hatanaka25dad192012-10-27 00:10:18 +00003744 int FI = MFI->CreateFixedObject(FrameObjSize, FrameObjOffset, true);
3745 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
3746 InVals.push_back(FIN);
3747
Daniel Sanders23e98772014-11-02 16:09:29 +00003748 if (!NumRegs)
Akira Hatanaka25dad192012-10-27 00:10:18 +00003749 return;
3750
3751 // Copy arg registers.
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003752 MVT RegTy = MVT::getIntegerVT(GPRSizeInBytes * 8);
Akira Hatanaka25dad192012-10-27 00:10:18 +00003753 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3754
Daniel Sanders23e98772014-11-02 16:09:29 +00003755 for (unsigned I = 0; I < NumRegs; ++I) {
Daniel Sandersd7eba312014-11-07 12:21:37 +00003756 unsigned ArgReg = ByValArgRegs[FirstReg + I];
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003757 unsigned VReg = addLiveIn(MF, ArgReg, RC);
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003758 unsigned Offset = I * GPRSizeInBytes;
Akira Hatanaka25dad192012-10-27 00:10:18 +00003759 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003760 DAG.getConstant(Offset, DL, PtrTy));
Akira Hatanaka25dad192012-10-27 00:10:18 +00003761 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
3762 StorePtr, MachinePointerInfo(FuncArg, Offset),
3763 false, false, 0);
3764 OutChains.push_back(Store);
3765 }
3766}
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003767
3768// Copy byVal arg to registers and stack.
Daniel Sandersf43e6872014-11-01 18:44:56 +00003769void MipsTargetLowering::passByValArg(
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003770 SDValue Chain, const SDLoc &DL,
Daniel Sandersf43e6872014-11-01 18:44:56 +00003771 std::deque<std::pair<unsigned, SDValue>> &RegsToPass,
3772 SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003773 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg, unsigned FirstReg,
3774 unsigned LastReg, const ISD::ArgFlagsTy &Flags, bool isLittle,
3775 const CCValAssign &VA) const {
Daniel Sandersac272632014-05-23 13:18:02 +00003776 unsigned ByValSizeInBytes = Flags.getByValSize();
3777 unsigned OffsetInBytes = 0; // From beginning of struct
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003778 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
Daniel Sandersac272632014-05-23 13:18:02 +00003779 unsigned Alignment = std::min(Flags.getByValAlign(), RegSizeInBytes);
Mehdi Amini44ede332015-07-09 02:09:04 +00003780 EVT PtrTy = getPointerTy(DAG.getDataLayout()),
3781 RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
Daniel Sanders23e98772014-11-02 16:09:29 +00003782 unsigned NumRegs = LastReg - FirstReg;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003783
Daniel Sanders23e98772014-11-02 16:09:29 +00003784 if (NumRegs) {
Craig Topper862d5d82015-09-28 00:15:34 +00003785 ArrayRef<MCPhysReg> ArgRegs = ABI.GetByValArgRegs();
Daniel Sanders23e98772014-11-02 16:09:29 +00003786 bool LeftoverBytes = (NumRegs * RegSizeInBytes > ByValSizeInBytes);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003787 unsigned I = 0;
3788
3789 // Copy words to registers.
Daniel Sanders23e98772014-11-02 16:09:29 +00003790 for (; I < NumRegs - LeftoverBytes; ++I, OffsetInBytes += RegSizeInBytes) {
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003791 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003792 DAG.getConstant(OffsetInBytes, DL, PtrTy));
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003793 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
3794 MachinePointerInfo(), false, false, false,
3795 Alignment);
3796 MemOpChains.push_back(LoadVal.getValue(1));
Daniel Sanders23e98772014-11-02 16:09:29 +00003797 unsigned ArgReg = ArgRegs[FirstReg + I];
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003798 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
3799 }
3800
3801 // Return if the struct has been fully copied.
Daniel Sandersac272632014-05-23 13:18:02 +00003802 if (ByValSizeInBytes == OffsetInBytes)
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003803 return;
3804
3805 // Copy the remainder of the byval argument with sub-word loads and shifts.
3806 if (LeftoverBytes) {
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003807 SDValue Val;
3808
Daniel Sandersac272632014-05-23 13:18:02 +00003809 for (unsigned LoadSizeInBytes = RegSizeInBytes / 2, TotalBytesLoaded = 0;
3810 OffsetInBytes < ByValSizeInBytes; LoadSizeInBytes /= 2) {
3811 unsigned RemainingSizeInBytes = ByValSizeInBytes - OffsetInBytes;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003812
Daniel Sandersac272632014-05-23 13:18:02 +00003813 if (RemainingSizeInBytes < LoadSizeInBytes)
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003814 continue;
3815
3816 // Load subword.
3817 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003818 DAG.getConstant(OffsetInBytes, DL,
3819 PtrTy));
Daniel Sandersac272632014-05-23 13:18:02 +00003820 SDValue LoadVal = DAG.getExtLoad(
3821 ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr, MachinePointerInfo(),
Louis Gerbarg67474e32014-07-31 21:45:05 +00003822 MVT::getIntegerVT(LoadSizeInBytes * 8), false, false, false,
3823 Alignment);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003824 MemOpChains.push_back(LoadVal.getValue(1));
3825
3826 // Shift the loaded value.
3827 unsigned Shamt;
3828
3829 if (isLittle)
Daniel Sandersac272632014-05-23 13:18:02 +00003830 Shamt = TotalBytesLoaded * 8;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003831 else
Daniel Sandersac272632014-05-23 13:18:02 +00003832 Shamt = (RegSizeInBytes - (TotalBytesLoaded + LoadSizeInBytes)) * 8;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003833
3834 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003835 DAG.getConstant(Shamt, DL, MVT::i32));
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003836
3837 if (Val.getNode())
3838 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
3839 else
3840 Val = Shift;
3841
Daniel Sandersac272632014-05-23 13:18:02 +00003842 OffsetInBytes += LoadSizeInBytes;
3843 TotalBytesLoaded += LoadSizeInBytes;
3844 Alignment = std::min(Alignment, LoadSizeInBytes);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003845 }
3846
Daniel Sanders23e98772014-11-02 16:09:29 +00003847 unsigned ArgReg = ArgRegs[FirstReg + I];
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003848 RegsToPass.push_back(std::make_pair(ArgReg, Val));
3849 return;
3850 }
3851 }
3852
3853 // Copy remainder of byval arg to it with memcpy.
Daniel Sandersac272632014-05-23 13:18:02 +00003854 unsigned MemCpySize = ByValSizeInBytes - OffsetInBytes;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003855 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003856 DAG.getConstant(OffsetInBytes, DL, PtrTy));
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003857 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003858 DAG.getIntPtrConstant(VA.getLocMemOffset(), DL));
3859 Chain = DAG.getMemcpy(Chain, DL, Dst, Src,
3860 DAG.getConstant(MemCpySize, DL, PtrTy),
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003861 Alignment, /*isVolatile=*/false, /*AlwaysInline=*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00003862 /*isTailCall=*/false,
Nick Lewyckyaad475b2014-04-15 07:22:52 +00003863 MachinePointerInfo(), MachinePointerInfo());
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003864 MemOpChains.push_back(Chain);
3865}
Akira Hatanaka2a134022012-10-27 00:21:13 +00003866
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003867void MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003868 SDValue Chain, const SDLoc &DL,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003869 SelectionDAG &DAG,
Daniel Sanders853c2432014-11-01 18:13:52 +00003870 CCState &State) const {
Craig Topper862d5d82015-09-28 00:15:34 +00003871 ArrayRef<MCPhysReg> ArgRegs = ABI.GetVarArgRegs();
Tim Northover3b6b7ca2015-02-21 02:11:17 +00003872 unsigned Idx = State.getFirstUnallocated(ArgRegs);
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003873 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
3874 MVT RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003875 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3876 MachineFunction &MF = DAG.getMachineFunction();
3877 MachineFrameInfo *MFI = MF.getFrameInfo();
3878 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3879
3880 // Offset of the first variable argument from stack pointer.
3881 int VaArgOffset;
3882
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003883 if (ArgRegs.size() == Idx)
Rui Ueyamada00f2f2016-01-14 21:06:47 +00003884 VaArgOffset = alignTo(State.getNextStackOffset(), RegSizeInBytes);
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003885 else {
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003886 VaArgOffset =
3887 (int)ABI.GetCalleeAllocdArgSizeInBytes(State.getCallingConv()) -
3888 (int)(RegSizeInBytes * (ArgRegs.size() - Idx));
3889 }
Akira Hatanaka2a134022012-10-27 00:21:13 +00003890
3891 // Record the frame index of the first variable argument
3892 // which is a value necessary to VASTART.
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003893 int FI = MFI->CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003894 MipsFI->setVarArgsFrameIndex(FI);
3895
3896 // Copy the integer registers that have not been used for argument passing
3897 // to the argument register save area. For O32, the save area is allocated
3898 // in the caller's stack frame, while for N32/64, it is allocated in the
3899 // callee's stack frame.
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003900 for (unsigned I = Idx; I < ArgRegs.size();
3901 ++I, VaArgOffset += RegSizeInBytes) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003902 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003903 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003904 FI = MFI->CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
Mehdi Amini44ede332015-07-09 02:09:04 +00003905 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Akira Hatanaka2a134022012-10-27 00:21:13 +00003906 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
3907 MachinePointerInfo(), false, false, 0);
Eric Christopher1c29a652014-07-18 22:55:25 +00003908 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(
3909 (Value *)nullptr);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003910 OutChains.push_back(Store);
3911 }
3912}
Daniel Sanders23e98772014-11-02 16:09:29 +00003913
3914void MipsTargetLowering::HandleByVal(CCState *State, unsigned &Size,
3915 unsigned Align) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003916 const TargetFrameLowering *TFL = Subtarget.getFrameLowering();
Daniel Sanders23e98772014-11-02 16:09:29 +00003917
3918 assert(Size && "Byval argument's size shouldn't be 0.");
3919
3920 Align = std::min(Align, TFL->getStackAlignment());
3921
3922 unsigned FirstReg = 0;
3923 unsigned NumRegs = 0;
3924
3925 if (State->getCallingConv() != CallingConv::Fast) {
3926 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
Craig Topper862d5d82015-09-28 00:15:34 +00003927 ArrayRef<MCPhysReg> IntArgRegs = ABI.GetByValArgRegs();
Daniel Sanders23e98772014-11-02 16:09:29 +00003928 // FIXME: The O32 case actually describes no shadow registers.
3929 const MCPhysReg *ShadowRegs =
Eric Christopher96e72c62015-01-29 23:27:36 +00003930 ABI.IsO32() ? IntArgRegs.data() : Mips64DPRegs;
Daniel Sanders23e98772014-11-02 16:09:29 +00003931
3932 // We used to check the size as well but we can't do that anymore since
3933 // CCState::HandleByVal() rounds up the size after calling this function.
3934 assert(!(Align % RegSizeInBytes) &&
3935 "Byval argument's alignment should be a multiple of"
3936 "RegSizeInBytes.");
3937
Tim Northover3b6b7ca2015-02-21 02:11:17 +00003938 FirstReg = State->getFirstUnallocated(IntArgRegs);
Daniel Sanders23e98772014-11-02 16:09:29 +00003939
3940 // If Align > RegSizeInBytes, the first arg register must be even.
3941 // FIXME: This condition happens to do the right thing but it's not the
3942 // right way to test it. We want to check that the stack frame offset
3943 // of the register is aligned.
3944 if ((Align > RegSizeInBytes) && (FirstReg % 2)) {
3945 State->AllocateReg(IntArgRegs[FirstReg], ShadowRegs[FirstReg]);
3946 ++FirstReg;
3947 }
3948
3949 // Mark the registers allocated.
Rui Ueyamada00f2f2016-01-14 21:06:47 +00003950 Size = alignTo(Size, RegSizeInBytes);
Daniel Sanders23e98772014-11-02 16:09:29 +00003951 for (unsigned I = FirstReg; Size > 0 && (I < IntArgRegs.size());
3952 Size -= RegSizeInBytes, ++I, ++NumRegs)
3953 State->AllocateReg(IntArgRegs[I], ShadowRegs[I]);
3954 }
3955
3956 State->addInRegsParamInfo(FirstReg, FirstReg + NumRegs);
3957}
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00003958
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003959MachineBasicBlock *MipsTargetLowering::emitPseudoSELECT(MachineInstr &MI,
3960 MachineBasicBlock *BB,
3961 bool isFPCmp,
3962 unsigned Opc) const {
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00003963 assert(!(Subtarget.hasMips4() || Subtarget.hasMips32()) &&
3964 "Subtarget already supports SELECT nodes with the use of"
3965 "conditional-move instructions.");
3966
3967 const TargetInstrInfo *TII =
Eric Christopher96e72c62015-01-29 23:27:36 +00003968 Subtarget.getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003969 DebugLoc DL = MI.getDebugLoc();
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00003970
3971 // To "insert" a SELECT instruction, we actually have to insert the
3972 // diamond control-flow pattern. The incoming instruction knows the
3973 // destination vreg to set, the condition code register to branch on, the
3974 // true/false values to select between, and a branch opcode to use.
3975 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +00003976 MachineFunction::iterator It = ++BB->getIterator();
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00003977
3978 // thisMBB:
3979 // ...
3980 // TrueVal = ...
3981 // setcc r1, r2, r3
3982 // bNE r1, r0, copy1MBB
3983 // fallthrough --> copy0MBB
3984 MachineBasicBlock *thisMBB = BB;
3985 MachineFunction *F = BB->getParent();
3986 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
3987 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
3988 F->insert(It, copy0MBB);
3989 F->insert(It, sinkMBB);
3990
3991 // Transfer the remainder of BB and its successor edges to sinkMBB.
3992 sinkMBB->splice(sinkMBB->begin(), BB,
3993 std::next(MachineBasicBlock::iterator(MI)), BB->end());
3994 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
3995
3996 // Next, add the true and fallthrough blocks as its successors.
3997 BB->addSuccessor(copy0MBB);
3998 BB->addSuccessor(sinkMBB);
3999
4000 if (isFPCmp) {
4001 // bc1[tf] cc, sinkMBB
4002 BuildMI(BB, DL, TII->get(Opc))
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004003 .addReg(MI.getOperand(1).getReg())
4004 .addMBB(sinkMBB);
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00004005 } else {
4006 // bne rs, $0, sinkMBB
4007 BuildMI(BB, DL, TII->get(Opc))
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004008 .addReg(MI.getOperand(1).getReg())
4009 .addReg(Mips::ZERO)
4010 .addMBB(sinkMBB);
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00004011 }
4012
4013 // copy0MBB:
4014 // %FalseValue = ...
4015 // # fallthrough to sinkMBB
4016 BB = copy0MBB;
4017
4018 // Update machine-CFG edges
4019 BB->addSuccessor(sinkMBB);
4020
4021 // sinkMBB:
4022 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
4023 // ...
4024 BB = sinkMBB;
4025
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004026 BuildMI(*BB, BB->begin(), DL, TII->get(Mips::PHI), MI.getOperand(0).getReg())
4027 .addReg(MI.getOperand(2).getReg())
4028 .addMBB(thisMBB)
4029 .addReg(MI.getOperand(3).getReg())
4030 .addMBB(copy0MBB);
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00004031
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004032 MI.eraseFromParent(); // The pseudo instruction is gone now.
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00004033
4034 return BB;
4035}
Daniel Sanders1440bb22015-01-09 17:21:30 +00004036
4037// FIXME? Maybe this could be a TableGen attribute on some registers and
4038// this table could be generated automatically from RegInfo.
Pat Gavlina717f252015-07-09 17:40:29 +00004039unsigned MipsTargetLowering::getRegisterByName(const char* RegName, EVT VT,
4040 SelectionDAG &DAG) const {
Daniel Sanders1440bb22015-01-09 17:21:30 +00004041 // Named registers is expected to be fairly rare. For now, just support $28
4042 // since the linux kernel uses it.
4043 if (Subtarget.isGP64bit()) {
4044 unsigned Reg = StringSwitch<unsigned>(RegName)
4045 .Case("$28", Mips::GP_64)
4046 .Default(0);
4047 if (Reg)
4048 return Reg;
4049 } else {
4050 unsigned Reg = StringSwitch<unsigned>(RegName)
4051 .Case("$28", Mips::GP)
4052 .Default(0);
4053 if (Reg)
4054 return Reg;
4055 }
4056 report_fatal_error("Invalid register name global variable");
4057}