blob: 6756c1702f76b48bbf7173a497d59612ddc167d5 [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.
Owen Anderson9f944592009-08-11 20:47:22 +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 Hatanaka24cf4e32012-07-11 19:32:27 +0000280 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
281 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Akira Hatanakab7f78592012-03-09 23:46:03 +0000282 setOperationAction(ISD::SETCC, MVT::f32, Custom);
283 setOperationAction(ISD::SETCC, MVT::f64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000284 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000285 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
286 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000287 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000288
Eric Christopher1c29a652014-07-18 22:55:25 +0000289 if (Subtarget.isGP64bit()) {
Akira Hatanakada00aa82012-03-10 00:03:50 +0000290 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
291 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
292 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
293 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
294 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
295 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Akira Hatanaka019e5922012-06-02 00:04:42 +0000296 setOperationAction(ISD::LOAD, MVT::i64, Custom);
297 setOperationAction(ISD::STORE, MVT::i64, Custom);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000298 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +0000299 setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom);
300 setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom);
301 setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000302 }
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +0000303
Eric Christopher1c29a652014-07-18 22:55:25 +0000304 if (!Subtarget.isGP64bit()) {
Akira Hatanaka0a8ab712012-05-09 00:55:21 +0000305 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
306 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
307 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
308 }
309
Akira Hatanaka28e02ec2012-11-07 19:10:58 +0000310 setOperationAction(ISD::ADD, MVT::i32, Custom);
Eric Christopher1c29a652014-07-18 22:55:25 +0000311 if (Subtarget.isGP64bit())
Akira Hatanaka28e02ec2012-11-07 19:10:58 +0000312 setOperationAction(ISD::ADD, MVT::i64, Custom);
313
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000314 setOperationAction(ISD::SDIV, MVT::i32, Expand);
315 setOperationAction(ISD::SREM, MVT::i32, Expand);
316 setOperationAction(ISD::UDIV, MVT::i32, Expand);
317 setOperationAction(ISD::UREM, MVT::i32, Expand);
Akira Hatanakab1538f92011-10-03 21:06:13 +0000318 setOperationAction(ISD::SDIV, MVT::i64, Expand);
319 setOperationAction(ISD::SREM, MVT::i64, Expand);
320 setOperationAction(ISD::UDIV, MVT::i64, Expand);
321 setOperationAction(ISD::UREM, MVT::i64, Expand);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000322
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000323 // Operations not directly supported by Mips.
Tom Stellardb1588fc2013-03-08 15:36:57 +0000324 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
325 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
326 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
327 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
Tom Stellard3787b122014-06-10 16:01:29 +0000328 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
329 setOperationAction(ISD::SELECT_CC, MVT::i64, 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);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000344 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
345 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
346 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
347 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000348 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Akira Hatanaka7ba8a8d2011-09-30 18:51:46 +0000349 setOperationAction(ISD::ROTL, MVT::i64, Expand);
Akira Hatanaka33a25af2012-07-31 20:54:48 +0000350 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
351 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Bruno Cardoso Lopesd47180e2010-12-09 17:32:30 +0000352
Eric Christopher1c29a652014-07-18 22:55:25 +0000353 if (!Subtarget.hasMips32r2())
Bruno Cardoso Lopesd47180e2010-12-09 17:32:30 +0000354 setOperationAction(ISD::ROTR, MVT::i32, Expand);
355
Eric Christopher1c29a652014-07-18 22:55:25 +0000356 if (!Subtarget.hasMips64r2())
Akira Hatanaka7ba8a8d2011-09-30 18:51:46 +0000357 setOperationAction(ISD::ROTR, MVT::i64, Expand);
358
Owen Anderson9f944592009-08-11 20:47:22 +0000359 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes22b69db2011-03-04 18:54:14 +0000360 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000361 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes22b69db2011-03-04 18:54:14 +0000362 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000363 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
364 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000365 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
366 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanakadfb8cda2011-05-23 22:23:58 +0000367 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000368 setOperationAction(ISD::FLOG, MVT::f32, Expand);
369 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
370 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
371 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000372 setOperationAction(ISD::FMA, MVT::f32, Expand);
373 setOperationAction(ISD::FMA, MVT::f64, Expand);
Akira Hatanaka0603ad82012-03-29 18:43:11 +0000374 setOperationAction(ISD::FREM, MVT::f32, Expand);
375 setOperationAction(ISD::FREM, MVT::f64, Expand);
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000376
Pirama Arumuga Nainar34056de2015-04-20 20:15:36 +0000377 // Lower f16 conversion operations into library calls
378 setOperationAction(ISD::FP16_TO_FP, MVT::f32, Expand);
379 setOperationAction(ISD::FP_TO_FP16, MVT::f32, Expand);
380 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand);
381 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Expand);
382
Akira Hatanakac0b02062013-01-30 00:26:49 +0000383 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
384
Daniel Sanders2b553d42014-08-01 09:17:39 +0000385 setOperationAction(ISD::VASTART, MVT::Other, Custom);
386 setOperationAction(ISD::VAARG, MVT::Other, Custom);
Bruno Cardoso Lopes048ffab2011-03-09 19:22:22 +0000387 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
388 setOperationAction(ISD::VAEND, MVT::Other, Expand);
389
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000390 // Use the default for now
Owen Anderson9f944592009-08-11 20:47:22 +0000391 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
392 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman26a48482011-07-27 22:21:52 +0000393
Vasileios Kalintirisb04672c2015-11-06 12:07:20 +0000394 if (!Subtarget.isGP64bit()) {
395 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
396 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
397 }
Eli Friedman7dfa7912011-08-29 18:23:02 +0000398
Eli Friedman30a49e92011-08-03 21:06:02 +0000399 setInsertFencesForAtomic(true);
400
Eric Christopher1c29a652014-07-18 22:55:25 +0000401 if (!Subtarget.hasMips32r2()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000402 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
403 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000404 }
405
Daniel Sanders070fd1c2014-05-12 12:41:59 +0000406 // MIPS16 lacks MIPS32's clz and clo instructions.
Eric Christopher1c29a652014-07-18 22:55:25 +0000407 if (!Subtarget.hasMips32() || Subtarget.inMips16Mode())
Owen Anderson9f944592009-08-11 20:47:22 +0000408 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Eric Christopher1c29a652014-07-18 22:55:25 +0000409 if (!Subtarget.hasMips64())
Akira Hatanaka1d8efab2011-12-21 00:20:27 +0000410 setOperationAction(ISD::CTLZ, MVT::i64, Expand);
Bruno Cardoso Lopes93da7e62008-08-08 06:16:31 +0000411
Eric Christopher1c29a652014-07-18 22:55:25 +0000412 if (!Subtarget.hasMips32r2())
Owen Anderson9f944592009-08-11 20:47:22 +0000413 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Eric Christopher1c29a652014-07-18 22:55:25 +0000414 if (!Subtarget.hasMips64r2())
Akira Hatanaka4706ac92011-12-20 23:56:43 +0000415 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000416
Eric Christopher1c29a652014-07-18 22:55:25 +0000417 if (Subtarget.isGP64bit()) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000418 setLoadExtAction(ISD::SEXTLOAD, MVT::i64, MVT::i32, Custom);
419 setLoadExtAction(ISD::ZEXTLOAD, MVT::i64, MVT::i32, Custom);
420 setLoadExtAction(ISD::EXTLOAD, MVT::i64, MVT::i32, Custom);
Akira Hatanaka019e5922012-06-02 00:04:42 +0000421 setTruncStoreAction(MVT::i64, MVT::i32, Custom);
422 }
423
Akira Hatanakaa3d9ab92013-07-26 20:58:55 +0000424 setOperationAction(ISD::TRAP, MVT::Other, Legal);
425
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000426 setTargetDAGCombine(ISD::SDIVREM);
427 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka5e152182012-03-08 03:26:37 +0000428 setTargetDAGCombine(ISD::SELECT);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000429 setTargetDAGCombine(ISD::AND);
430 setTargetDAGCombine(ISD::OR);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000431 setTargetDAGCombine(ISD::ADD);
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000432
Eric Christopher1c29a652014-07-18 22:55:25 +0000433 setMinFunctionAlignment(Subtarget.isGP64bit() ? 3 : 2);
Eli Friedman2518f832011-05-06 20:34:06 +0000434
Daniel Sanders2b553d42014-08-01 09:17:39 +0000435 // The arguments on the stack are defined in terms of 4-byte slots on O32
436 // and 8-byte slots on N32/N64.
Eric Christopher96e72c62015-01-29 23:27:36 +0000437 setMinStackArgumentAlignment((ABI.IsN32() || ABI.IsN64()) ? 8 : 4);
Daniel Sanders2b553d42014-08-01 09:17:39 +0000438
Eric Christopher96e72c62015-01-29 23:27:36 +0000439 setStackPointerRegisterToSaveRestore(ABI.IsN64() ? Mips::SP_64 : Mips::SP);
Akira Hatanakaaa560002011-05-26 18:59:03 +0000440
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000441 MaxStoresPerMemcpy = 16;
Zoran Jovanovicff9d5f32013-12-19 16:12:56 +0000442
Eric Christopher1c29a652014-07-18 22:55:25 +0000443 isMicroMips = Subtarget.inMicroMipsMode();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000444}
445
Eric Christopherb1526602014-09-19 23:30:42 +0000446const MipsTargetLowering *MipsTargetLowering::create(const MipsTargetMachine &TM,
Eric Christopher8924d272014-07-18 23:25:04 +0000447 const MipsSubtarget &STI) {
448 if (STI.inMips16Mode())
449 return llvm::createMips16TargetLowering(TM, STI);
Jia Liuf54f60f2012-02-28 07:46:26 +0000450
Eric Christopher8924d272014-07-18 23:25:04 +0000451 return llvm::createMipsSETargetLowering(TM, STI);
Akira Hatanaka2fcc1cf2011-08-12 21:30:06 +0000452}
453
Reed Kotler720c5ca2014-04-17 22:15:34 +0000454// Create a fast isel object.
455FastISel *
456MipsTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
457 const TargetLibraryInfo *libInfo) const {
Vasileios Kalintiris2041b1d2015-07-30 12:39:33 +0000458 if (!funcInfo.MF->getTarget().Options.EnableFastISel)
Reed Kotler720c5ca2014-04-17 22:15:34 +0000459 return TargetLowering::createFastISel(funcInfo, libInfo);
460 return Mips::createFastISel(funcInfo, libInfo);
461}
462
Mehdi Amini44ede332015-07-09 02:09:04 +0000463EVT MipsTargetLowering::getSetCCResultType(const DataLayout &, LLVMContext &,
464 EVT VT) const {
Akira Hatanakab13b3332013-01-04 20:06:01 +0000465 if (!VT.isVector())
466 return MVT::i32;
467 return VT.changeVectorElementTypeToInteger();
Scott Michela6729e82008-03-10 15:42:14 +0000468}
469
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000470static SDValue performDivRemCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000471 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000472 const MipsSubtarget &Subtarget) {
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000473 if (DCI.isBeforeLegalizeOps())
474 return SDValue();
475
Akira Hatanakab1538f92011-10-03 21:06:13 +0000476 EVT Ty = N->getValueType(0);
Akira Hatanaka8002a3f2013-08-14 00:47:08 +0000477 unsigned LO = (Ty == MVT::i32) ? Mips::LO0 : Mips::LO0_64;
478 unsigned HI = (Ty == MVT::i32) ? Mips::HI0 : Mips::HI0_64;
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000479 unsigned Opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem16 :
480 MipsISD::DivRemU16;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000481 SDLoc DL(N);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000482
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000483 SDValue DivRem = DAG.getNode(Opc, DL, MVT::Glue,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000484 N->getOperand(0), N->getOperand(1));
485 SDValue InChain = DAG.getEntryNode();
486 SDValue InGlue = DivRem;
487
488 // insert MFLO
489 if (N->hasAnyUseOfValue(0)) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000490 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, DL, LO, Ty,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000491 InGlue);
492 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
493 InChain = CopyFromLo.getValue(1);
494 InGlue = CopyFromLo.getValue(2);
495 }
496
497 // insert MFHI
498 if (N->hasAnyUseOfValue(1)) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000499 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, DL,
Akira Hatanakab1538f92011-10-03 21:06:13 +0000500 HI, Ty, InGlue);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000501 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
502 }
503
504 return SDValue();
505}
506
Akira Hatanaka89af5892013-04-18 01:00:46 +0000507static Mips::CondCode condCodeToFCC(ISD::CondCode CC) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000508 switch (CC) {
509 default: llvm_unreachable("Unknown fp condition code!");
510 case ISD::SETEQ:
511 case ISD::SETOEQ: return Mips::FCOND_OEQ;
512 case ISD::SETUNE: return Mips::FCOND_UNE;
513 case ISD::SETLT:
514 case ISD::SETOLT: return Mips::FCOND_OLT;
515 case ISD::SETGT:
516 case ISD::SETOGT: return Mips::FCOND_OGT;
517 case ISD::SETLE:
518 case ISD::SETOLE: return Mips::FCOND_OLE;
519 case ISD::SETGE:
520 case ISD::SETOGE: return Mips::FCOND_OGE;
521 case ISD::SETULT: return Mips::FCOND_ULT;
522 case ISD::SETULE: return Mips::FCOND_ULE;
523 case ISD::SETUGT: return Mips::FCOND_UGT;
524 case ISD::SETUGE: return Mips::FCOND_UGE;
525 case ISD::SETUO: return Mips::FCOND_UN;
526 case ISD::SETO: return Mips::FCOND_OR;
527 case ISD::SETNE:
528 case ISD::SETONE: return Mips::FCOND_ONE;
529 case ISD::SETUEQ: return Mips::FCOND_UEQ;
530 }
531}
532
533
Akira Hatanakaf0ea5002013-03-30 01:16:38 +0000534/// This function returns true if the floating point conditional branches and
535/// conditional moves which use condition code CC should be inverted.
536static bool invertFPCondCodeUser(Mips::CondCode CC) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000537 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
538 return false;
539
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000540 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
541 "Illegal Condition Code");
Akira Hatanakaa5352702011-03-31 18:26:17 +0000542
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000543 return true;
Akira Hatanakaa5352702011-03-31 18:26:17 +0000544}
545
546// Creates and returns an FPCmp node from a setcc node.
547// Returns Op if setcc is not a floating point comparison.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000548static SDValue createFPCmp(SelectionDAG &DAG, const SDValue &Op) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000549 // must be a SETCC node
550 if (Op.getOpcode() != ISD::SETCC)
551 return Op;
552
553 SDValue LHS = Op.getOperand(0);
554
555 if (!LHS.getValueType().isFloatingPoint())
556 return Op;
557
558 SDValue RHS = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000559 SDLoc DL(Op);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000560
Akira Hatanakaaef55c82011-04-15 21:00:26 +0000561 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
562 // node if necessary.
Akira Hatanakaa5352702011-03-31 18:26:17 +0000563 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
564
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000565 return DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000566 DAG.getConstant(condCodeToFCC(CC), DL, MVT::i32));
Akira Hatanakaa5352702011-03-31 18:26:17 +0000567}
568
569// Creates and returns a CMovFPT/F node.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000570static SDValue createCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000571 SDValue False, SDLoc DL) {
Akira Hatanakaf0ea5002013-03-30 01:16:38 +0000572 ConstantSDNode *CC = cast<ConstantSDNode>(Cond.getOperand(2));
573 bool invert = invertFPCondCodeUser((Mips::CondCode)CC->getSExtValue());
Akira Hatanaka8bce21c2013-07-26 20:51:20 +0000574 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000575
576 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
Akira Hatanaka8bce21c2013-07-26 20:51:20 +0000577 True.getValueType(), True, FCC0, False, Cond);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000578}
579
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000580static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000581 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000582 const MipsSubtarget &Subtarget) {
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000583 if (DCI.isBeforeLegalizeOps())
584 return SDValue();
585
586 SDValue SetCC = N->getOperand(0);
587
588 if ((SetCC.getOpcode() != ISD::SETCC) ||
589 !SetCC.getOperand(0).getValueType().isInteger())
590 return SDValue();
591
592 SDValue False = N->getOperand(2);
593 EVT FalseTy = False.getValueType();
594
595 if (!FalseTy.isInteger())
596 return SDValue();
597
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000598 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(False);
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000599
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000600 // If the RHS (False) is 0, we swap the order of the operands
601 // of ISD::SELECT (obviously also inverting the condition) so that we can
602 // take advantage of conditional moves using the $0 register.
603 // Example:
604 // return (a != 0) ? x : 0;
605 // load $reg, x
606 // movz $reg, $0, a
607 if (!FalseC)
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000608 return SDValue();
609
Andrew Trickef9de2a2013-05-25 02:42:55 +0000610 const SDLoc DL(N);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000611
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000612 if (!FalseC->getZExtValue()) {
613 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
614 SDValue True = N->getOperand(1);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000615
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000616 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
617 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
618
619 return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True);
620 }
621
Matheus Almeidaa6beac12013-12-05 12:07:05 +0000622 // If both operands are integer constants there's a possibility that we
623 // can do some interesting optimizations.
624 SDValue True = N->getOperand(1);
625 ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(True);
626
627 if (!TrueC || !True.getValueType().isInteger())
628 return SDValue();
629
630 // We'll also ignore MVT::i64 operands as this optimizations proves
631 // to be ineffective because of the required sign extensions as the result
632 // of a SETCC operator is always MVT::i32 for non-vector types.
633 if (True.getValueType() == MVT::i64)
634 return SDValue();
635
636 int64_t Diff = TrueC->getSExtValue() - FalseC->getSExtValue();
637
638 // 1) (a < x) ? y : y-1
639 // slti $reg1, a, x
640 // addiu $reg2, $reg1, y-1
641 if (Diff == 1)
642 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, False);
643
644 // 2) (a < x) ? y-1 : y
645 // slti $reg1, a, x
646 // xor $reg1, $reg1, 1
647 // addiu $reg2, $reg1, y-1
648 if (Diff == -1) {
649 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
650 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
651 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
652 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, True);
653 }
654
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000655 // Couldn't optimize.
656 return SDValue();
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000657}
658
Vasileios Kalintirise741eb22015-03-02 12:47:32 +0000659static SDValue performCMovFPCombine(SDNode *N, SelectionDAG &DAG,
660 TargetLowering::DAGCombinerInfo &DCI,
661 const MipsSubtarget &Subtarget) {
662 if (DCI.isBeforeLegalizeOps())
663 return SDValue();
664
665 SDValue ValueIfTrue = N->getOperand(0), ValueIfFalse = N->getOperand(2);
666
667 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(ValueIfFalse);
668 if (!FalseC || FalseC->getZExtValue())
669 return SDValue();
670
671 // Since RHS (False) is 0, we swap the order of the True/False operands
672 // (obviously also inverting the condition) so that we can
673 // take advantage of conditional moves using the $0 register.
674 // Example:
675 // return (a != 0) ? x : 0;
676 // load $reg, x
677 // movz $reg, $0, a
678 unsigned Opc = (N->getOpcode() == MipsISD::CMovFP_T) ? MipsISD::CMovFP_F :
679 MipsISD::CMovFP_T;
680
681 SDValue FCC = N->getOperand(1), Glue = N->getOperand(3);
Vasileios Kalintiris2ef28882015-03-04 12:10:18 +0000682 return DAG.getNode(Opc, SDLoc(N), ValueIfFalse.getValueType(),
683 ValueIfFalse, FCC, ValueIfTrue, Glue);
Vasileios Kalintirise741eb22015-03-02 12:47:32 +0000684}
685
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000686static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000687 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000688 const MipsSubtarget &Subtarget) {
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000689 // Pattern match EXT.
690 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
691 // => ext $dst, $src, size, pos
Eric Christopher1c29a652014-07-18 22:55:25 +0000692 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasExtractInsert())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000693 return SDValue();
694
695 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000696 unsigned ShiftRightOpc = ShiftRight.getOpcode();
697
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000698 // Op's first operand must be a shift right.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000699 if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL)
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000700 return SDValue();
701
702 // The second operand of the shift must be an immediate.
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000703 ConstantSDNode *CN;
704 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
705 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000706
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000707 uint64_t Pos = CN->getZExtValue();
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000708 uint64_t SMPos, SMSize;
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000709
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000710 // Op's second operand must be a shifted mask.
711 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000712 !isShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000713 return SDValue();
714
715 // Return if the shifted mask does not start at bit 0 or the sum of its size
716 // and Pos exceeds the word's size.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000717 EVT ValTy = N->getValueType(0);
718 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000719 return SDValue();
720
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000721 SDLoc DL(N);
722 return DAG.getNode(MipsISD::Ext, DL, ValTy,
723 ShiftRight.getOperand(0),
724 DAG.getConstant(Pos, DL, MVT::i32),
725 DAG.getConstant(SMSize, DL, MVT::i32));
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000726}
Jia Liuf54f60f2012-02-28 07:46:26 +0000727
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000728static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000729 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000730 const MipsSubtarget &Subtarget) {
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000731 // Pattern match INS.
732 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
Jia Liuf54f60f2012-02-28 07:46:26 +0000733 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000734 // => ins $dst, $src, size, pos, $src1
Eric Christopher1c29a652014-07-18 22:55:25 +0000735 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasExtractInsert())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000736 return SDValue();
737
738 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
739 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
740 ConstantSDNode *CN;
741
742 // See if Op's first operand matches (and $src1 , mask0).
743 if (And0.getOpcode() != ISD::AND)
744 return SDValue();
745
746 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000747 !isShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000748 return SDValue();
749
750 // See if Op's second operand matches (and (shl $src, pos), mask1).
751 if (And1.getOpcode() != ISD::AND)
752 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000753
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000754 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000755 !isShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000756 return SDValue();
757
758 // The shift masks must have the same position and size.
759 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
760 return SDValue();
761
762 SDValue Shl = And1.getOperand(0);
763 if (Shl.getOpcode() != ISD::SHL)
764 return SDValue();
765
766 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
767 return SDValue();
768
769 unsigned Shamt = CN->getZExtValue();
770
771 // Return if the shift amount and the first bit position of mask are not the
Jia Liuf54f60f2012-02-28 07:46:26 +0000772 // same.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000773 EVT ValTy = N->getValueType(0);
774 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000775 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000776
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000777 SDLoc DL(N);
778 return DAG.getNode(MipsISD::Ins, DL, ValTy, Shl.getOperand(0),
779 DAG.getConstant(SMPos0, DL, MVT::i32),
780 DAG.getConstant(SMSize0, DL, MVT::i32),
781 And0.getOperand(0));
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000782}
Jia Liuf54f60f2012-02-28 07:46:26 +0000783
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000784static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000785 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000786 const MipsSubtarget &Subtarget) {
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000787 // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt))
788
789 if (DCI.isBeforeLegalizeOps())
790 return SDValue();
791
792 SDValue Add = N->getOperand(1);
793
794 if (Add.getOpcode() != ISD::ADD)
795 return SDValue();
796
797 SDValue Lo = Add.getOperand(1);
798
799 if ((Lo.getOpcode() != MipsISD::Lo) ||
800 (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable))
801 return SDValue();
802
803 EVT ValTy = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000804 SDLoc DL(N);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000805
806 SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, N->getOperand(0),
807 Add.getOperand(0));
808 return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo);
809}
810
Bruno Cardoso Lopes61a61e92011-02-10 18:05:10 +0000811SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000812 const {
813 SelectionDAG &DAG = DCI.DAG;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000814 unsigned Opc = N->getOpcode();
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000815
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000816 switch (Opc) {
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000817 default: break;
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000818 case ISD::SDIVREM:
819 case ISD::UDIVREM:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000820 return performDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000821 case ISD::SELECT:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000822 return performSELECTCombine(N, DAG, DCI, Subtarget);
Vasileios Kalintirise741eb22015-03-02 12:47:32 +0000823 case MipsISD::CMovFP_F:
824 case MipsISD::CMovFP_T:
825 return performCMovFPCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000826 case ISD::AND:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000827 return performANDCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000828 case ISD::OR:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000829 return performORCombine(N, DAG, DCI, Subtarget);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000830 case ISD::ADD:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000831 return performADDCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000832 }
833
834 return SDValue();
835}
836
Sanjay Patelf7401292015-11-11 17:24:56 +0000837bool MipsTargetLowering::isCheapToSpeculateCttz() const {
838 return Subtarget.hasMips32();
839}
840
841bool MipsTargetLowering::isCheapToSpeculateCtlz() const {
842 return Subtarget.hasMips32();
843}
844
Akira Hatanakafabb8cf2012-09-21 23:58:31 +0000845void
846MipsTargetLowering::LowerOperationWrapper(SDNode *N,
847 SmallVectorImpl<SDValue> &Results,
848 SelectionDAG &DAG) const {
849 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
850
851 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
852 Results.push_back(Res.getValue(I));
853}
854
855void
856MipsTargetLowering::ReplaceNodeResults(SDNode *N,
857 SmallVectorImpl<SDValue> &Results,
858 SelectionDAG &DAG) const {
Akira Hatanaka9da442f2013-04-30 21:17:07 +0000859 return LowerOperationWrapper(N, Results, DAG);
Akira Hatanakafabb8cf2012-09-21 23:58:31 +0000860}
861
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000862SDValue MipsTargetLowering::
Dan Gohman21cea8a2010-04-17 15:26:15 +0000863LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000864{
Wesley Peck527da1b2010-11-23 03:31:01 +0000865 switch (Op.getOpcode())
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000866 {
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000867 case ISD::BR_JT: return lowerBR_JT(Op, DAG);
868 case ISD::BRCOND: return lowerBRCOND(Op, DAG);
869 case ISD::ConstantPool: return lowerConstantPool(Op, DAG);
870 case ISD::GlobalAddress: return lowerGlobalAddress(Op, DAG);
871 case ISD::BlockAddress: return lowerBlockAddress(Op, DAG);
872 case ISD::GlobalTLSAddress: return lowerGlobalTLSAddress(Op, DAG);
873 case ISD::JumpTable: return lowerJumpTable(Op, DAG);
874 case ISD::SELECT: return lowerSELECT(Op, DAG);
875 case ISD::SELECT_CC: return lowerSELECT_CC(Op, DAG);
876 case ISD::SETCC: return lowerSETCC(Op, DAG);
877 case ISD::VASTART: return lowerVASTART(Op, DAG);
Daniel Sanders2b553d42014-08-01 09:17:39 +0000878 case ISD::VAARG: return lowerVAARG(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000879 case ISD::FCOPYSIGN: return lowerFCOPYSIGN(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000880 case ISD::FRAMEADDR: return lowerFRAMEADDR(Op, DAG);
881 case ISD::RETURNADDR: return lowerRETURNADDR(Op, DAG);
882 case ISD::EH_RETURN: return lowerEH_RETURN(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000883 case ISD::ATOMIC_FENCE: return lowerATOMIC_FENCE(Op, DAG);
884 case ISD::SHL_PARTS: return lowerShiftLeftParts(Op, DAG);
885 case ISD::SRA_PARTS: return lowerShiftRightParts(Op, DAG, true);
886 case ISD::SRL_PARTS: return lowerShiftRightParts(Op, DAG, false);
887 case ISD::LOAD: return lowerLOAD(Op, DAG);
888 case ISD::STORE: return lowerSTORE(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000889 case ISD::ADD: return lowerADD(Op, DAG);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000890 case ISD::FP_TO_SINT: return lowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000891 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000892 return SDValue();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000893}
894
Akira Hatanakae2489122011-04-15 21:51:11 +0000895//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000896// Lower helper functions
Akira Hatanakae2489122011-04-15 21:51:11 +0000897//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000898
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000899// addLiveIn - This helper function adds the specified physical register to the
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000900// MachineFunction as a live in value. It also creates a corresponding
901// virtual register for it.
902static unsigned
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000903addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000904{
Chris Lattnera10fff52007-12-31 04:13:23 +0000905 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
906 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000907 return VReg;
908}
909
Daniel Sanders308181e2014-06-12 10:44:10 +0000910static MachineBasicBlock *insertDivByZeroTrap(MachineInstr *MI,
911 MachineBasicBlock &MBB,
912 const TargetInstrInfo &TII,
913 bool Is64Bit) {
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000914 if (NoZeroDivCheck)
915 return &MBB;
916
917 // Insert instruction "teq $divisor_reg, $zero, 7".
918 MachineBasicBlock::iterator I(MI);
919 MachineInstrBuilder MIB;
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000920 MachineOperand &Divisor = MI->getOperand(2);
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000921 MIB = BuildMI(MBB, std::next(I), MI->getDebugLoc(), TII.get(Mips::TEQ))
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000922 .addReg(Divisor.getReg(), getKillRegState(Divisor.isKill()))
923 .addReg(Mips::ZERO).addImm(7);
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000924
925 // Use the 32-bit sub-register if this is a 64-bit division.
926 if (Is64Bit)
927 MIB->getOperand(0).setSubReg(Mips::sub_32);
928
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000929 // Clear Divisor's kill flag.
930 Divisor.setIsKill(false);
Daniel Sanders308181e2014-06-12 10:44:10 +0000931
932 // We would normally delete the original instruction here but in this case
933 // we only needed to inject an additional instruction rather than replace it.
934
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000935 return &MBB;
936}
937
Akira Hatanakae4bd0542012-09-27 02:15:57 +0000938MachineBasicBlock *
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +0000939MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +0000940 MachineBasicBlock *BB) const {
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +0000941 switch (MI->getOpcode()) {
Reed Kotler97ba5f22013-02-21 04:22:38 +0000942 default:
943 llvm_unreachable("Unexpected instr type to insert");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000944 case Mips::ATOMIC_LOAD_ADD_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000945 return emitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000946 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000947 return emitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000948 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000949 return emitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000950 case Mips::ATOMIC_LOAD_ADD_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000951 return emitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000952
953 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000954 return emitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000955 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000956 return emitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000957 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000958 return emitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000959 case Mips::ATOMIC_LOAD_AND_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000960 return emitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000961
962 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000963 return emitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000964 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000965 return emitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000966 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000967 return emitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000968 case Mips::ATOMIC_LOAD_OR_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000969 return emitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000970
971 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000972 return emitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000973 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000974 return emitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000975 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000976 return emitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000977 case Mips::ATOMIC_LOAD_XOR_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000978 return emitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000979
980 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000981 return emitAtomicBinaryPartword(MI, BB, 1, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000982 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000983 return emitAtomicBinaryPartword(MI, BB, 2, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000984 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000985 return emitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000986 case Mips::ATOMIC_LOAD_NAND_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000987 return emitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000988
989 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000990 return emitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000991 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000992 return emitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000993 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000994 return emitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000995 case Mips::ATOMIC_LOAD_SUB_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000996 return emitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000997
998 case Mips::ATOMIC_SWAP_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000999 return emitAtomicBinaryPartword(MI, BB, 1, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001000 case Mips::ATOMIC_SWAP_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001001 return emitAtomicBinaryPartword(MI, BB, 2, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001002 case Mips::ATOMIC_SWAP_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001003 return emitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001004 case Mips::ATOMIC_SWAP_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001005 return emitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001006
1007 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001008 return emitAtomicCmpSwapPartword(MI, BB, 1);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001009 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001010 return emitAtomicCmpSwapPartword(MI, BB, 2);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001011 case Mips::ATOMIC_CMP_SWAP_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001012 return emitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001013 case Mips::ATOMIC_CMP_SWAP_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001014 return emitAtomicCmpSwap(MI, BB, 8);
Akira Hatanaka1cb02422013-05-20 18:07:43 +00001015 case Mips::PseudoSDIV:
1016 case Mips::PseudoUDIV:
Daniel Sanders308181e2014-06-12 10:44:10 +00001017 case Mips::DIV:
1018 case Mips::DIVU:
1019 case Mips::MOD:
1020 case Mips::MODU:
Eric Christopher96e72c62015-01-29 23:27:36 +00001021 return insertDivByZeroTrap(MI, *BB, *Subtarget.getInstrInfo(), false);
Akira Hatanaka1cb02422013-05-20 18:07:43 +00001022 case Mips::PseudoDSDIV:
1023 case Mips::PseudoDUDIV:
Daniel Sanders308181e2014-06-12 10:44:10 +00001024 case Mips::DDIV:
1025 case Mips::DDIVU:
1026 case Mips::DMOD:
1027 case Mips::DMODU:
Eric Christopher96e72c62015-01-29 23:27:36 +00001028 return insertDivByZeroTrap(MI, *BB, *Subtarget.getInstrInfo(), true);
Daniel Sanders0fa60412014-06-12 13:39:06 +00001029 case Mips::SEL_D:
1030 return emitSEL_D(MI, BB);
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001031
1032 case Mips::PseudoSELECT_I:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001033 case Mips::PseudoSELECT_I64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001034 case Mips::PseudoSELECT_S:
1035 case Mips::PseudoSELECT_D32:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001036 case Mips::PseudoSELECT_D64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001037 return emitPseudoSELECT(MI, BB, false, Mips::BNE);
1038 case Mips::PseudoSELECTFP_F_I:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001039 case Mips::PseudoSELECTFP_F_I64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001040 case Mips::PseudoSELECTFP_F_S:
1041 case Mips::PseudoSELECTFP_F_D32:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001042 case Mips::PseudoSELECTFP_F_D64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001043 return emitPseudoSELECT(MI, BB, true, Mips::BC1F);
1044 case Mips::PseudoSELECTFP_T_I:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001045 case Mips::PseudoSELECTFP_T_I64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001046 case Mips::PseudoSELECTFP_T_S:
1047 case Mips::PseudoSELECTFP_T_D32:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001048 case Mips::PseudoSELECTFP_T_D64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001049 return emitPseudoSELECT(MI, BB, true, Mips::BC1T);
Akira Hatanakaa5352702011-03-31 18:26:17 +00001050 }
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001051}
1052
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001053// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
1054// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
1055MachineBasicBlock *
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001056MipsTargetLowering::emitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher0713a9d2011-06-08 23:55:35 +00001057 unsigned Size, unsigned BinOpcode,
Akira Hatanaka15506782011-06-07 18:58:42 +00001058 bool Nand) const {
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001059 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001060
1061 MachineFunction *MF = BB->getParent();
1062 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001063 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Eric Christopher96e72c62015-01-29 23:27:36 +00001064 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001065 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001066 unsigned LL, SC, AND, NOR, ZERO, BEQ;
1067
1068 if (Size == 4) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001069 if (isMicroMips) {
1070 LL = Mips::LL_MM;
1071 SC = Mips::SC_MM;
1072 } else {
Daniel Sandersbdcfab12014-07-24 09:47:14 +00001073 LL = Subtarget.hasMips32r6() ? Mips::LL_R6 : Mips::LL;
1074 SC = Subtarget.hasMips32r6() ? Mips::SC_R6 : Mips::SC;
Daniel Sanders6a803f62014-06-16 13:13:03 +00001075 }
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001076 AND = Mips::AND;
1077 NOR = Mips::NOR;
1078 ZERO = Mips::ZERO;
1079 BEQ = Mips::BEQ;
Daniel Sanders6a803f62014-06-16 13:13:03 +00001080 } else {
Daniel Sandersbdcfab12014-07-24 09:47:14 +00001081 LL = Subtarget.hasMips64r6() ? Mips::LLD_R6 : Mips::LLD;
1082 SC = Subtarget.hasMips64r6() ? Mips::SCD_R6 : Mips::SCD;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001083 AND = Mips::AND64;
1084 NOR = Mips::NOR64;
1085 ZERO = Mips::ZERO_64;
1086 BEQ = Mips::BEQ64;
1087 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001088
Akira Hatanaka0e019592011-07-19 20:11:17 +00001089 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001090 unsigned Ptr = MI->getOperand(1).getReg();
1091 unsigned Incr = MI->getOperand(2).getReg();
1092
Akira Hatanaka0e019592011-07-19 20:11:17 +00001093 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1094 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1095 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001096
1097 // insert new blocks after the current block
1098 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1099 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1100 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +00001101 MachineFunction::iterator It = ++BB->getIterator();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001102 MF->insert(It, loopMBB);
1103 MF->insert(It, exitMBB);
1104
1105 // Transfer the remainder of BB and its successor edges to exitMBB.
1106 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001107 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001108 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1109
1110 // thisMBB:
1111 // ...
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001112 // fallthrough --> loopMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001113 BB->addSuccessor(loopMBB);
Akira Hatanaka08636b42011-07-19 17:09:53 +00001114 loopMBB->addSuccessor(loopMBB);
1115 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001116
1117 // loopMBB:
1118 // ll oldval, 0(ptr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001119 // <binop> storeval, oldval, incr
1120 // sc success, storeval, 0(ptr)
1121 // beq success, $0, loopMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001122 BB = loopMBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001123 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001124 if (Nand) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001125 // and andres, oldval, incr
1126 // nor storeval, $0, andres
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001127 BuildMI(BB, DL, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
1128 BuildMI(BB, DL, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001129 } else if (BinOpcode) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001130 // <binop> storeval, oldval, incr
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001131 BuildMI(BB, DL, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001132 } else {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001133 StoreVal = Incr;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001134 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001135 BuildMI(BB, DL, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
1136 BuildMI(BB, DL, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001137
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001138 MI->eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001139
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001140 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001141}
1142
Daniel Sanders6a803f62014-06-16 13:13:03 +00001143MachineBasicBlock *MipsTargetLowering::emitSignExtendToI32InReg(
1144 MachineInstr *MI, MachineBasicBlock *BB, unsigned Size, unsigned DstReg,
1145 unsigned SrcReg) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00001146 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders6a803f62014-06-16 13:13:03 +00001147 DebugLoc DL = MI->getDebugLoc();
1148
Eric Christopher1c29a652014-07-18 22:55:25 +00001149 if (Subtarget.hasMips32r2() && Size == 1) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001150 BuildMI(BB, DL, TII->get(Mips::SEB), DstReg).addReg(SrcReg);
1151 return BB;
1152 }
1153
Eric Christopher1c29a652014-07-18 22:55:25 +00001154 if (Subtarget.hasMips32r2() && Size == 2) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001155 BuildMI(BB, DL, TII->get(Mips::SEH), DstReg).addReg(SrcReg);
1156 return BB;
1157 }
1158
1159 MachineFunction *MF = BB->getParent();
1160 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1161 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1162 unsigned ScrReg = RegInfo.createVirtualRegister(RC);
1163
1164 assert(Size < 32);
1165 int64_t ShiftImm = 32 - (Size * 8);
1166
1167 BuildMI(BB, DL, TII->get(Mips::SLL), ScrReg).addReg(SrcReg).addImm(ShiftImm);
1168 BuildMI(BB, DL, TII->get(Mips::SRA), DstReg).addReg(ScrReg).addImm(ShiftImm);
1169
1170 return BB;
1171}
1172
1173MachineBasicBlock *MipsTargetLowering::emitAtomicBinaryPartword(
1174 MachineInstr *MI, MachineBasicBlock *BB, unsigned Size, unsigned BinOpcode,
1175 bool Nand) const {
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001176 assert((Size == 1 || Size == 2) &&
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001177 "Unsupported size for EmitAtomicBinaryPartial.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001178
1179 MachineFunction *MF = BB->getParent();
1180 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1181 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
Eric Christopher96e72c62015-01-29 23:27:36 +00001182 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001183 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001184
1185 unsigned Dest = MI->getOperand(0).getReg();
1186 unsigned Ptr = MI->getOperand(1).getReg();
1187 unsigned Incr = MI->getOperand(2).getReg();
1188
Akira Hatanaka0e019592011-07-19 20:11:17 +00001189 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1190 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001191 unsigned Mask = RegInfo.createVirtualRegister(RC);
1192 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001193 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1194 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001195 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001196 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1197 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1198 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1199 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1200 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka9663dd32011-07-19 20:56:53 +00001201 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001202 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1203 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1204 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001205 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001206
1207 // insert new blocks after the current block
1208 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1209 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001210 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001211 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +00001212 MachineFunction::iterator It = ++BB->getIterator();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001213 MF->insert(It, loopMBB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001214 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001215 MF->insert(It, exitMBB);
1216
1217 // Transfer the remainder of BB and its successor edges to exitMBB.
1218 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001219 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001220 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1221
Akira Hatanaka08636b42011-07-19 17:09:53 +00001222 BB->addSuccessor(loopMBB);
1223 loopMBB->addSuccessor(loopMBB);
1224 loopMBB->addSuccessor(sinkMBB);
1225 sinkMBB->addSuccessor(exitMBB);
1226
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001227 // thisMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001228 // addiu masklsb2,$0,-4 # 0xfffffffc
1229 // and alignedaddr,ptr,masklsb2
1230 // andi ptrlsb2,ptr,3
1231 // sll shiftamt,ptrlsb2,3
1232 // ori maskupper,$0,255 # 0xff
1233 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001234 // nor mask2,$0,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001235 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001236
1237 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001238 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001239 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001240 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001241 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001242 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Eric Christopher1c29a652014-07-18 22:55:25 +00001243 if (Subtarget.isLittle()) {
Akira Hatanaka2bf97332013-05-31 03:25:44 +00001244 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1245 } else {
1246 unsigned Off = RegInfo.createVirtualRegister(RC);
1247 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1248 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1249 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1250 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001251 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001252 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001253 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001254 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001255 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001256 BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(Incr).addReg(ShiftAmt);
Bruno Cardoso Lopesf771a0f2011-05-31 20:25:26 +00001257
Akira Hatanaka27292632011-07-18 18:52:12 +00001258 // atomic.load.binop
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001259 // loopMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001260 // ll oldval,0(alignedaddr)
1261 // binop binopres,oldval,incr2
1262 // and newval,binopres,mask
1263 // and maskedoldval0,oldval,mask2
1264 // or storeval,maskedoldval0,newval
1265 // sc success,storeval,0(alignedaddr)
1266 // beq success,$0,loopMBB
1267
Akira Hatanaka27292632011-07-18 18:52:12 +00001268 // atomic.swap
1269 // loopMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001270 // ll oldval,0(alignedaddr)
Akira Hatanakae4503582011-07-19 18:14:26 +00001271 // and newval,incr2,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001272 // and maskedoldval0,oldval,mask2
1273 // or storeval,maskedoldval0,newval
1274 // sc success,storeval,0(alignedaddr)
1275 // beq success,$0,loopMBB
Akira Hatanaka27292632011-07-18 18:52:12 +00001276
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001277 BB = loopMBB;
Jozef Kolek2f27d572014-12-18 16:39:29 +00001278 unsigned LL = isMicroMips ? Mips::LL_MM : Mips::LL;
1279 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001280 if (Nand) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001281 // and andres, oldval, incr2
1282 // nor binopres, $0, andres
1283 // and newval, binopres, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001284 BuildMI(BB, DL, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1285 BuildMI(BB, DL, TII->get(Mips::NOR), BinOpRes)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001286 .addReg(Mips::ZERO).addReg(AndRes);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001287 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001288 } else if (BinOpcode) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001289 // <binop> binopres, oldval, incr2
1290 // and newval, binopres, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001291 BuildMI(BB, DL, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1292 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001293 } else { // atomic.swap
Akira Hatanaka0e019592011-07-19 20:11:17 +00001294 // and newval, incr2, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001295 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanakae4503582011-07-19 18:14:26 +00001296 }
Jia Liuf54f60f2012-02-28 07:46:26 +00001297
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001298 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001299 .addReg(OldVal).addReg(Mask2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001300 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka9663dd32011-07-19 20:56:53 +00001301 .addReg(MaskedOldVal0).addReg(NewVal);
Jozef Kolek2f27d572014-12-18 16:39:29 +00001302 unsigned SC = isMicroMips ? Mips::SC_MM : Mips::SC;
1303 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001304 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001305 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001306 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001307
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001308 // sinkMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001309 // and maskedoldval1,oldval,mask
1310 // srl srlres,maskedoldval1,shiftamt
Daniel Sanders6a803f62014-06-16 13:13:03 +00001311 // sign_extend dest,srlres
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001312 BB = sinkMBB;
Akira Hatanakae97bd812011-07-19 03:14:58 +00001313
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001314 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001315 .addReg(OldVal).addReg(Mask);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001316 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001317 .addReg(MaskedOldVal1).addReg(ShiftAmt);
Daniel Sanders6a803f62014-06-16 13:13:03 +00001318 BB = emitSignExtendToI32InReg(MI, BB, Size, Dest, SrlRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001319
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001320 MI->eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001321
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001322 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001323}
1324
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001325MachineBasicBlock * MipsTargetLowering::emitAtomicCmpSwap(MachineInstr *MI,
1326 MachineBasicBlock *BB,
1327 unsigned Size) const {
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001328 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001329
1330 MachineFunction *MF = BB->getParent();
1331 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001332 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Eric Christopher96e72c62015-01-29 23:27:36 +00001333 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001334 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001335 unsigned LL, SC, ZERO, BNE, BEQ;
1336
Zoran Jovanovic796ed6d2015-10-29 14:40:19 +00001337 if (Size == 4) {
1338 if (isMicroMips) {
1339 LL = Mips::LL_MM;
1340 SC = Mips::SC_MM;
1341 } else {
1342 LL = Subtarget.hasMips32r6() ? Mips::LL_R6 : Mips::LL;
1343 SC = Subtarget.hasMips32r6() ? Mips::SC_R6 : Mips::SC;
1344 }
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001345 ZERO = Mips::ZERO;
1346 BNE = Mips::BNE;
1347 BEQ = Mips::BEQ;
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001348 } else {
Zoran Jovanovic796ed6d2015-10-29 14:40:19 +00001349 LL = Subtarget.hasMips64r6() ? Mips::LLD_R6 : Mips::LLD;
1350 SC = Subtarget.hasMips64r6() ? Mips::SCD_R6 : Mips::SCD;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001351 ZERO = Mips::ZERO_64;
1352 BNE = Mips::BNE64;
1353 BEQ = Mips::BEQ64;
1354 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001355
1356 unsigned Dest = MI->getOperand(0).getReg();
1357 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka0e019592011-07-19 20:11:17 +00001358 unsigned OldVal = MI->getOperand(2).getReg();
1359 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001360
Akira Hatanaka0e019592011-07-19 20:11:17 +00001361 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001362
1363 // insert new blocks after the current block
1364 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1365 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1366 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1367 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +00001368 MachineFunction::iterator It = ++BB->getIterator();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001369 MF->insert(It, loop1MBB);
1370 MF->insert(It, loop2MBB);
1371 MF->insert(It, exitMBB);
1372
1373 // Transfer the remainder of BB and its successor edges to exitMBB.
1374 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001375 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001376 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1377
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001378 // thisMBB:
1379 // ...
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001380 // fallthrough --> loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001381 BB->addSuccessor(loop1MBB);
Akira Hatanaka08636b42011-07-19 17:09:53 +00001382 loop1MBB->addSuccessor(exitMBB);
1383 loop1MBB->addSuccessor(loop2MBB);
1384 loop2MBB->addSuccessor(loop1MBB);
1385 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001386
1387 // loop1MBB:
1388 // ll dest, 0(ptr)
1389 // bne dest, oldval, exitMBB
1390 BB = loop1MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001391 BuildMI(BB, DL, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1392 BuildMI(BB, DL, TII->get(BNE))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001393 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001394
1395 // loop2MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001396 // sc success, newval, 0(ptr)
1397 // beq success, $0, loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001398 BB = loop2MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001399 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001400 .addReg(NewVal).addReg(Ptr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001401 BuildMI(BB, DL, TII->get(BEQ))
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001402 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001403
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001404 MI->eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001405
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001406 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001407}
1408
1409MachineBasicBlock *
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001410MipsTargetLowering::emitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka15506782011-06-07 18:58:42 +00001411 MachineBasicBlock *BB,
1412 unsigned Size) const {
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001413 assert((Size == 1 || Size == 2) &&
1414 "Unsupported size for EmitAtomicCmpSwapPartial.");
1415
1416 MachineFunction *MF = BB->getParent();
1417 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1418 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
Eric Christopher96e72c62015-01-29 23:27:36 +00001419 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001420 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001421
1422 unsigned Dest = MI->getOperand(0).getReg();
1423 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka0e019592011-07-19 20:11:17 +00001424 unsigned CmpVal = MI->getOperand(2).getReg();
1425 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001426
Akira Hatanaka0e019592011-07-19 20:11:17 +00001427 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1428 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001429 unsigned Mask = RegInfo.createVirtualRegister(RC);
1430 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001431 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1432 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1433 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1434 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1435 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1436 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1437 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1438 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1439 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1440 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1441 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1442 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001443 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001444
1445 // insert new blocks after the current block
1446 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1447 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1448 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001449 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001450 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +00001451 MachineFunction::iterator It = ++BB->getIterator();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001452 MF->insert(It, loop1MBB);
1453 MF->insert(It, loop2MBB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001454 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001455 MF->insert(It, exitMBB);
1456
1457 // Transfer the remainder of BB and its successor edges to exitMBB.
1458 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001459 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001460 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1461
Akira Hatanaka08636b42011-07-19 17:09:53 +00001462 BB->addSuccessor(loop1MBB);
1463 loop1MBB->addSuccessor(sinkMBB);
1464 loop1MBB->addSuccessor(loop2MBB);
1465 loop2MBB->addSuccessor(loop1MBB);
1466 loop2MBB->addSuccessor(sinkMBB);
1467 sinkMBB->addSuccessor(exitMBB);
1468
Akira Hatanakae4503582011-07-19 18:14:26 +00001469 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001470 // thisMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001471 // addiu masklsb2,$0,-4 # 0xfffffffc
1472 // and alignedaddr,ptr,masklsb2
1473 // andi ptrlsb2,ptr,3
1474 // sll shiftamt,ptrlsb2,3
1475 // ori maskupper,$0,255 # 0xff
1476 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001477 // nor mask2,$0,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001478 // andi maskedcmpval,cmpval,255
1479 // sll shiftedcmpval,maskedcmpval,shiftamt
1480 // andi maskednewval,newval,255
1481 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001482 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001483 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001484 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001485 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001486 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001487 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Eric Christopher1c29a652014-07-18 22:55:25 +00001488 if (Subtarget.isLittle()) {
Akira Hatanaka2bf97332013-05-31 03:25:44 +00001489 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1490 } else {
1491 unsigned Off = RegInfo.createVirtualRegister(RC);
1492 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1493 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1494 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1495 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001496 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001497 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001498 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001499 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001500 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1501 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001502 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001503 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001504 .addReg(MaskedCmpVal).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001505 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001506 .addReg(NewVal).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001507 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001508 .addReg(MaskedNewVal).addReg(ShiftAmt);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001509
1510 // loop1MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001511 // ll oldval,0(alginedaddr)
1512 // and maskedoldval0,oldval,mask
1513 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001514 BB = loop1MBB;
Jozef Kolek2f27d572014-12-18 16:39:29 +00001515 unsigned LL = isMicroMips ? Mips::LL_MM : Mips::LL;
1516 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001517 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001518 .addReg(OldVal).addReg(Mask);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001519 BuildMI(BB, DL, TII->get(Mips::BNE))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001520 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001521
1522 // loop2MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001523 // and maskedoldval1,oldval,mask2
1524 // or storeval,maskedoldval1,shiftednewval
1525 // sc success,storeval,0(alignedaddr)
1526 // beq success,$0,loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001527 BB = loop2MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001528 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001529 .addReg(OldVal).addReg(Mask2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001530 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001531 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
Jozef Kolek2f27d572014-12-18 16:39:29 +00001532 unsigned SC = isMicroMips ? Mips::SC_MM : Mips::SC;
1533 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001534 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001535 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001536 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001537
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001538 // sinkMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001539 // srl srlres,maskedoldval0,shiftamt
Daniel Sanders6a803f62014-06-16 13:13:03 +00001540 // sign_extend dest,srlres
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001541 BB = sinkMBB;
Akira Hatanakae97bd812011-07-19 03:14:58 +00001542
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001543 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001544 .addReg(MaskedOldVal0).addReg(ShiftAmt);
Daniel Sanders6a803f62014-06-16 13:13:03 +00001545 BB = emitSignExtendToI32InReg(MI, BB, Size, Dest, SrlRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001546
1547 MI->eraseFromParent(); // The instruction is gone now.
1548
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001549 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001550}
1551
Daniel Sanders0fa60412014-06-12 13:39:06 +00001552MachineBasicBlock *MipsTargetLowering::emitSEL_D(MachineInstr *MI,
1553 MachineBasicBlock *BB) const {
1554 MachineFunction *MF = BB->getParent();
Eric Christopher96e72c62015-01-29 23:27:36 +00001555 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
1556 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders0fa60412014-06-12 13:39:06 +00001557 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1558 DebugLoc DL = MI->getDebugLoc();
1559 MachineBasicBlock::iterator II(MI);
1560
1561 unsigned Fc = MI->getOperand(1).getReg();
1562 const auto &FGR64RegClass = TRI->getRegClass(Mips::FGR64RegClassID);
1563
1564 unsigned Fc2 = RegInfo.createVirtualRegister(FGR64RegClass);
1565
1566 BuildMI(*BB, II, DL, TII->get(Mips::SUBREG_TO_REG), Fc2)
1567 .addImm(0)
1568 .addReg(Fc)
1569 .addImm(Mips::sub_lo);
1570
1571 // We don't erase the original instruction, we just replace the condition
1572 // register with the 64-bit super-register.
1573 MI->getOperand(1).setReg(Fc2);
1574
1575 return BB;
1576}
1577
Akira Hatanakae2489122011-04-15 21:51:11 +00001578//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001579// Misc Lower Operation implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00001580//===----------------------------------------------------------------------===//
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001581SDValue MipsTargetLowering::lowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001582 SDValue Chain = Op.getOperand(0);
1583 SDValue Table = Op.getOperand(1);
1584 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001585 SDLoc DL(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00001586 auto &TD = DAG.getDataLayout();
1587 EVT PTy = getPointerTy(TD);
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001588 unsigned EntrySize =
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001589 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(TD);
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001590
1591 Index = DAG.getNode(ISD::MUL, DL, PTy, Index,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001592 DAG.getConstant(EntrySize, DL, PTy));
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001593 SDValue Addr = DAG.getNode(ISD::ADD, DL, PTy, Index, Table);
1594
1595 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001596 Addr =
1597 DAG.getExtLoad(ISD::SEXTLOAD, DL, PTy, Chain, Addr,
1598 MachinePointerInfo::getJumpTable(DAG.getMachineFunction()),
1599 MemVT, false, false, false, 0);
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001600 Chain = Addr.getValue(1);
1601
Eric Christopher96e72c62015-01-29 23:27:36 +00001602 if ((getTargetMachine().getRelocationModel() == Reloc::PIC_) || ABI.IsN64()) {
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001603 // For PIC, the sequence is:
1604 // BRIND(load(Jumptable + index) + RelocBase)
1605 // RelocBase can be JumpTable, GOT or some sort of global base.
1606 Addr = DAG.getNode(ISD::ADD, DL, PTy, Addr,
1607 getPICJumpTableRelocBase(Table, DAG));
1608 }
1609
1610 return DAG.getNode(ISD::BRIND, DL, MVT::Other, Chain, Addr);
1611}
1612
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001613SDValue MipsTargetLowering::lowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Wesley Peck527da1b2010-11-23 03:31:01 +00001614 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001615 // the block to branch to if the condition is true.
1616 SDValue Chain = Op.getOperand(0);
1617 SDValue Dest = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001618 SDLoc DL(Op);
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001619
Eric Christopher1c29a652014-07-18 22:55:25 +00001620 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001621 SDValue CondRes = createFPCmp(DAG, Op.getOperand(1));
Akira Hatanakaa5352702011-03-31 18:26:17 +00001622
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001623 // Return if flag is not set by a floating point comparison.
Akira Hatanakaa5352702011-03-31 18:26:17 +00001624 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopesa9504222008-07-30 17:06:13 +00001625 return Op;
Wesley Peck527da1b2010-11-23 03:31:01 +00001626
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +00001627 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmaneffb8942008-09-12 16:56:44 +00001628 Mips::CondCode CC =
1629 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Akira Hatanakaf0ea5002013-03-30 01:16:38 +00001630 unsigned Opc = invertFPCondCodeUser(CC) ? Mips::BRANCH_F : Mips::BRANCH_T;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001631 SDValue BrCode = DAG.getConstant(Opc, DL, MVT::i32);
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +00001632 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001633 return DAG.getNode(MipsISD::FPBrcond, DL, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +00001634 FCC0, Dest, CondRes);
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001635}
1636
1637SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001638lowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001639{
Eric Christopher1c29a652014-07-18 22:55:25 +00001640 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001641 SDValue Cond = createFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001642
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001643 // Return if flag is not set by a floating point comparison.
Akira Hatanakaa5352702011-03-31 18:26:17 +00001644 if (Cond.getOpcode() != MipsISD::FPCmp)
1645 return Op;
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +00001646
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001647 return createCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
Andrew Trickef9de2a2013-05-25 02:42:55 +00001648 SDLoc(Op));
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001649}
1650
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001651SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001652lowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001653{
Andrew Trickef9de2a2013-05-25 02:42:55 +00001654 SDLoc DL(Op);
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001655 EVT Ty = Op.getOperand(0).getValueType();
Mehdi Amini44ede332015-07-09 02:09:04 +00001656 SDValue Cond =
1657 DAG.getNode(ISD::SETCC, DL, getSetCCResultType(DAG.getDataLayout(),
1658 *DAG.getContext(), Ty),
1659 Op.getOperand(0), Op.getOperand(1), Op.getOperand(4));
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001660
1661 return DAG.getNode(ISD::SELECT, DL, Op.getValueType(), Cond, Op.getOperand(2),
1662 Op.getOperand(3));
1663}
1664
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001665SDValue MipsTargetLowering::lowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00001666 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001667 SDValue Cond = createFPCmp(DAG, Op);
Akira Hatanakab7f78592012-03-09 23:46:03 +00001668
1669 assert(Cond.getOpcode() == MipsISD::FPCmp &&
1670 "Floating point operand expected.");
1671
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001672 SDLoc DL(Op);
1673 SDValue True = DAG.getConstant(1, DL, MVT::i32);
1674 SDValue False = DAG.getConstant(0, DL, MVT::i32);
Akira Hatanakab7f78592012-03-09 23:46:03 +00001675
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001676 return createCMovFP(DAG, Cond, True, False, DL);
Akira Hatanakab7f78592012-03-09 23:46:03 +00001677}
1678
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001679SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001680 SelectionDAG &DAG) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001681 EVT Ty = Op.getValueType();
1682 GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
1683 const GlobalValue *GV = N->getGlobal();
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001684
Eric Christopher96e72c62015-01-29 23:27:36 +00001685 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !ABI.IsN64()) {
Eric Christopher36fe0282015-02-03 07:22:52 +00001686 const MipsTargetObjectFile *TLOF =
1687 static_cast<const MipsTargetObjectFile *>(
1688 getTargetMachine().getObjFileLowering());
1689 if (TLOF->IsGlobalInSmallSection(GV, getTargetMachine()))
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001690 // %gp_rel relocation
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001691 return getAddrGPRel(N, SDLoc(N), Ty, DAG);
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001692
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001693 // %hi/%lo relocation
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001694 return getAddrNonPIC(N, SDLoc(N), Ty, DAG);
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001695 }
1696
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001697 if (GV->hasInternalLinkage() || (GV->hasLocalLinkage() && !isa<Function>(GV)))
Eric Christopher96e72c62015-01-29 23:27:36 +00001698 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001699
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00001700 if (LargeGOT)
Alex Lorenze40c8a22015-08-11 23:09:45 +00001701 return getAddrGlobalLargeGOT(
1702 N, SDLoc(N), Ty, DAG, MipsII::MO_GOT_HI16, MipsII::MO_GOT_LO16,
1703 DAG.getEntryNode(),
1704 MachinePointerInfo::getGOT(DAG.getMachineFunction()));
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00001705
Alex Lorenze40c8a22015-08-11 23:09:45 +00001706 return getAddrGlobal(
1707 N, SDLoc(N), Ty, DAG,
1708 (ABI.IsN32() || ABI.IsN64()) ? MipsII::MO_GOT_DISP : MipsII::MO_GOT16,
1709 DAG.getEntryNode(), MachinePointerInfo::getGOT(DAG.getMachineFunction()));
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001710}
1711
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001712SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op,
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +00001713 SelectionDAG &DAG) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001714 BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op);
1715 EVT Ty = Op.getValueType();
Akira Hatanaka30f97cf2013-09-25 00:30:25 +00001716
Eric Christopher96e72c62015-01-29 23:27:36 +00001717 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !ABI.IsN64())
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001718 return getAddrNonPIC(N, SDLoc(N), Ty, DAG);
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001719
Eric Christopher96e72c62015-01-29 23:27:36 +00001720 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +00001721}
1722
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001723SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001724lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001725{
Akira Hatanakabff84e12011-12-14 18:26:41 +00001726 // If the relocation model is PIC, use the General Dynamic TLS Model or
1727 // Local Dynamic TLS model, otherwise use the Initial Exec or
1728 // Local Exec TLS Model.
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001729
1730 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +00001731 if (DAG.getTarget().Options.EmulatedTLS)
1732 return LowerToTLSEmulatedModel(GA, DAG);
1733
Andrew Trickef9de2a2013-05-25 02:42:55 +00001734 SDLoc DL(GA);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001735 const GlobalValue *GV = GA->getGlobal();
Mehdi Amini44ede332015-07-09 02:09:04 +00001736 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001737
Hans Wennborgaea41202012-05-04 09:40:39 +00001738 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
1739
1740 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Hans Wennborg245917b2012-06-04 14:02:08 +00001741 // General Dynamic and Local Dynamic TLS Model.
1742 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
1743 : MipsII::MO_TLSGD;
1744
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001745 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag);
1746 SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT,
1747 getGlobalReg(DAG, PtrVT), TGA);
Akira Hatanakaf10ee842011-12-08 21:05:38 +00001748 unsigned PtrSize = PtrVT.getSizeInBits();
1749 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1750
Benjamin Kramer64ba50a2011-12-11 12:21:34 +00001751 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001752
1753 ArgListTy Args;
1754 ArgListEntry Entry;
1755 Entry.Node = Argument;
Akira Hatanakadee6c822011-12-08 20:34:32 +00001756 Entry.Ty = PtrTy;
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001757 Args.push_back(Entry);
Jia Liuf54f60f2012-02-28 07:46:26 +00001758
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00001759 TargetLowering::CallLoweringInfo CLI(DAG);
1760 CLI.setDebugLoc(DL).setChain(DAG.getEntryNode())
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00001761 .setCallee(CallingConv::C, PtrTy, TlsGetAddr, std::move(Args), 0);
Justin Holewinskiaa583972012-05-25 16:35:28 +00001762 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001763
Akira Hatanakabff84e12011-12-14 18:26:41 +00001764 SDValue Ret = CallResult.first;
1765
Hans Wennborgaea41202012-05-04 09:40:39 +00001766 if (model != TLSModel::LocalDynamic)
Akira Hatanakabff84e12011-12-14 18:26:41 +00001767 return Ret;
1768
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001769 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanakabff84e12011-12-14 18:26:41 +00001770 MipsII::MO_DTPREL_HI);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001771 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1772 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanakabff84e12011-12-14 18:26:41 +00001773 MipsII::MO_DTPREL_LO);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001774 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1775 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret);
1776 return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001777 }
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001778
1779 SDValue Offset;
Hans Wennborgaea41202012-05-04 09:40:39 +00001780 if (model == TLSModel::InitialExec) {
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001781 // Initial Exec TLS Model
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001782 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001783 MipsII::MO_GOTTPREL);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001784 TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT),
Akira Hatanakab049aef2012-02-24 22:34:47 +00001785 TGA);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001786 Offset = DAG.getLoad(PtrVT, DL,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001787 DAG.getEntryNode(), TGA, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001788 false, false, false, 0);
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001789 } else {
1790 // Local Exec TLS Model
Hans Wennborgaea41202012-05-04 09:40:39 +00001791 assert(model == TLSModel::LocalExec);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001792 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001793 MipsII::MO_TPREL_HI);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001794 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001795 MipsII::MO_TPREL_LO);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001796 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1797 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1798 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001799 }
1800
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001801 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
1802 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001803}
1804
1805SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001806lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +00001807{
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001808 JumpTableSDNode *N = cast<JumpTableSDNode>(Op);
1809 EVT Ty = Op.getValueType();
Akira Hatanaka30f97cf2013-09-25 00:30:25 +00001810
Eric Christopher96e72c62015-01-29 23:27:36 +00001811 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !ABI.IsN64())
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001812 return getAddrNonPIC(N, SDLoc(N), Ty, DAG);
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001813
Eric Christopher96e72c62015-01-29 23:27:36 +00001814 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +00001815}
1816
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001817SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001818lowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +00001819{
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001820 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
1821 EVT Ty = Op.getValueType();
Bruno Cardoso Lopes2db07582009-11-25 12:17:58 +00001822
Eric Christopher96e72c62015-01-29 23:27:36 +00001823 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !ABI.IsN64()) {
Eric Christopher36fe0282015-02-03 07:22:52 +00001824 const MipsTargetObjectFile *TLOF =
1825 static_cast<const MipsTargetObjectFile *>(
1826 getTargetMachine().getObjFileLowering());
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001827
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001828 if (TLOF->IsConstantInSmallSection(DAG.getDataLayout(), N->getConstVal(),
1829 getTargetMachine()))
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001830 // %gp_rel relocation
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001831 return getAddrGPRel(N, SDLoc(N), Ty, DAG);
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001832
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001833 return getAddrNonPIC(N, SDLoc(N), Ty, DAG);
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001834 }
Bruno Cardoso Lopesfdb4cec2008-07-23 16:01:50 +00001835
Eric Christopher96e72c62015-01-29 23:27:36 +00001836 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +00001837}
1838
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001839SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman31ae5862010-04-17 14:41:14 +00001840 MachineFunction &MF = DAG.getMachineFunction();
1841 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1842
Andrew Trickef9de2a2013-05-25 02:42:55 +00001843 SDLoc DL(Op);
Dan Gohman31ae5862010-04-17 14:41:14 +00001844 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
Mehdi Amini44ede332015-07-09 02:09:04 +00001845 getPointerTy(MF.getDataLayout()));
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +00001846
1847 // vastart just stores the address of the VarArgsFrameIndex slot into the
1848 // memory location argument.
1849 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001850 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00001851 MachinePointerInfo(SV), false, false, 0);
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +00001852}
Jia Liuf54f60f2012-02-28 07:46:26 +00001853
Daniel Sanders2b553d42014-08-01 09:17:39 +00001854SDValue MipsTargetLowering::lowerVAARG(SDValue Op, SelectionDAG &DAG) const {
1855 SDNode *Node = Op.getNode();
1856 EVT VT = Node->getValueType(0);
1857 SDValue Chain = Node->getOperand(0);
1858 SDValue VAListPtr = Node->getOperand(1);
1859 unsigned Align = Node->getConstantOperandVal(3);
1860 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
1861 SDLoc DL(Node);
Eric Christopher96e72c62015-01-29 23:27:36 +00001862 unsigned ArgSlotSizeInBytes = (ABI.IsN32() || ABI.IsN64()) ? 8 : 4;
Daniel Sanders2b553d42014-08-01 09:17:39 +00001863
Mehdi Amini44ede332015-07-09 02:09:04 +00001864 SDValue VAListLoad =
1865 DAG.getLoad(getPointerTy(DAG.getDataLayout()), DL, Chain, VAListPtr,
1866 MachinePointerInfo(SV), false, false, false, 0);
Daniel Sanders2b553d42014-08-01 09:17:39 +00001867 SDValue VAList = VAListLoad;
1868
1869 // Re-align the pointer if necessary.
1870 // It should only ever be necessary for 64-bit types on O32 since the minimum
1871 // argument alignment is the same as the maximum type alignment for N32/N64.
1872 //
1873 // FIXME: We currently align too often. The code generator doesn't notice
1874 // when the pointer is still aligned from the last va_arg (or pair of
1875 // va_args for the i64 on O32 case).
1876 if (Align > getMinStackArgumentAlignment()) {
1877 assert(((Align & (Align-1)) == 0) && "Expected Align to be a power of 2");
1878
1879 VAList = DAG.getNode(ISD::ADD, DL, VAList.getValueType(), VAList,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001880 DAG.getConstant(Align - 1, DL, VAList.getValueType()));
Daniel Sanders2b553d42014-08-01 09:17:39 +00001881
1882 VAList = DAG.getNode(ISD::AND, DL, VAList.getValueType(), VAList,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001883 DAG.getConstant(-(int64_t)Align, DL,
Daniel Sanders2b553d42014-08-01 09:17:39 +00001884 VAList.getValueType()));
1885 }
1886
1887 // Increment the pointer, VAList, to the next vaarg.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001888 auto &TD = DAG.getDataLayout();
1889 unsigned ArgSizeInBytes =
1890 TD.getTypeAllocSize(VT.getTypeForEVT(*DAG.getContext()));
Daniel Sanders2b553d42014-08-01 09:17:39 +00001891 SDValue Tmp3 = DAG.getNode(ISD::ADD, DL, VAList.getValueType(), VAList,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001892 DAG.getConstant(RoundUpToAlignment(ArgSizeInBytes,
1893 ArgSlotSizeInBytes),
1894 DL, VAList.getValueType()));
Daniel Sanders2b553d42014-08-01 09:17:39 +00001895 // Store the incremented VAList to the legalized pointer
1896 Chain = DAG.getStore(VAListLoad.getValue(1), DL, Tmp3, VAListPtr,
1897 MachinePointerInfo(SV), false, false, 0);
1898
1899 // In big-endian mode we must adjust the pointer when the load size is smaller
1900 // than the argument slot size. We must also reduce the known alignment to
1901 // match. For example in the N64 ABI, we must add 4 bytes to the offset to get
1902 // the correct half of the slot, and reduce the alignment from 8 (slot
1903 // alignment) down to 4 (type alignment).
1904 if (!Subtarget.isLittle() && ArgSizeInBytes < ArgSlotSizeInBytes) {
1905 unsigned Adjustment = ArgSlotSizeInBytes - ArgSizeInBytes;
1906 VAList = DAG.getNode(ISD::ADD, DL, VAListPtr.getValueType(), VAList,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001907 DAG.getIntPtrConstant(Adjustment, DL));
Daniel Sanders2b553d42014-08-01 09:17:39 +00001908 }
1909 // Load the actual argument out of the pointer VAList
1910 return DAG.getLoad(VT, DL, Chain, VAList, MachinePointerInfo(), false, false,
1911 false, 0);
1912}
1913
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001914static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG,
1915 bool HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001916 EVT TyX = Op.getOperand(0).getValueType();
1917 EVT TyY = Op.getOperand(1).getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001918 SDLoc DL(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001919 SDValue Const1 = DAG.getConstant(1, DL, MVT::i32);
1920 SDValue Const31 = DAG.getConstant(31, DL, MVT::i32);
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001921 SDValue Res;
1922
1923 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1924 // to i32.
1925 SDValue X = (TyX == MVT::f32) ?
1926 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1927 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1928 Const1);
1929 SDValue Y = (TyY == MVT::f32) ?
1930 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
1931 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
1932 Const1);
1933
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001934 if (HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001935 // ext E, Y, 31, 1 ; extract bit31 of Y
1936 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
1937 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
1938 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
1939 } else {
1940 // sll SllX, X, 1
1941 // srl SrlX, SllX, 1
1942 // srl SrlY, Y, 31
1943 // sll SllY, SrlX, 31
1944 // or Or, SrlX, SllY
1945 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1946 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1947 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
1948 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
1949 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
1950 }
1951
1952 if (TyX == MVT::f32)
1953 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
1954
1955 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001956 Op.getOperand(0),
1957 DAG.getConstant(0, DL, MVT::i32));
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001958 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001959}
1960
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001961static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG,
1962 bool HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001963 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
1964 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
1965 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001966 SDLoc DL(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001967 SDValue Const1 = DAG.getConstant(1, DL, MVT::i32);
Eric Christopher0713a9d2011-06-08 23:55:35 +00001968
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001969 // Bitcast to integer nodes.
1970 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
1971 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001972
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001973 if (HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001974 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
1975 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
1976 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001977 DAG.getConstant(WidthY - 1, DL, MVT::i32), Const1);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001978
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001979 if (WidthX > WidthY)
1980 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
1981 else if (WidthY > WidthX)
1982 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001983
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001984 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001985 DAG.getConstant(WidthX - 1, DL, MVT::i32), Const1,
1986 X);
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001987 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
1988 }
1989
1990 // (d)sll SllX, X, 1
1991 // (d)srl SrlX, SllX, 1
1992 // (d)srl SrlY, Y, width(Y)-1
1993 // (d)sll SllY, SrlX, width(Y)-1
1994 // or Or, SrlX, SllY
1995 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
1996 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
1997 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001998 DAG.getConstant(WidthY - 1, DL, MVT::i32));
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001999
2000 if (WidthX > WidthY)
2001 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
2002 else if (WidthY > WidthX)
2003 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
2004
2005 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002006 DAG.getConstant(WidthX - 1, DL, MVT::i32));
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002007 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
2008 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002009}
2010
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00002011SDValue
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002012MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00002013 if (Subtarget.isGP64bit())
2014 return lowerFCOPYSIGN64(Op, DAG, Subtarget.hasExtractInsert());
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002015
Eric Christopher1c29a652014-07-18 22:55:25 +00002016 return lowerFCOPYSIGN32(Op, DAG, Subtarget.hasExtractInsert());
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002017}
2018
Akira Hatanaka66277522011-06-02 00:24:44 +00002019SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002020lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes5444a7b2011-06-16 00:40:02 +00002021 // check the depth
2022 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka15506782011-06-07 18:58:42 +00002023 "Frame address can only be determined for current frame.");
Akira Hatanaka66277522011-06-02 00:24:44 +00002024
2025 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2026 MFI->setFrameAddressIsTaken(true);
2027 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002028 SDLoc DL(Op);
Eric Christopher96e72c62015-01-29 23:27:36 +00002029 SDValue FrameAddr = DAG.getCopyFromReg(
2030 DAG.getEntryNode(), DL, ABI.IsN64() ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka66277522011-06-02 00:24:44 +00002031 return FrameAddr;
2032}
2033
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002034SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op,
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002035 SelectionDAG &DAG) const {
Bill Wendling908bf812014-01-06 00:43:20 +00002036 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00002037 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00002038
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002039 // check the depth
2040 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
2041 "Return address can be determined only for current frame.");
2042
2043 MachineFunction &MF = DAG.getMachineFunction();
2044 MachineFrameInfo *MFI = MF.getFrameInfo();
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00002045 MVT VT = Op.getSimpleValueType();
Eric Christopher96e72c62015-01-29 23:27:36 +00002046 unsigned RA = ABI.IsN64() ? Mips::RA_64 : Mips::RA;
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002047 MFI->setReturnAddressIsTaken(true);
2048
2049 // Return RA, which contains the return address. Mark it an implicit live-in.
2050 unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00002051 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT);
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002052}
2053
Akira Hatanakac0b02062013-01-30 00:26:49 +00002054// An EH_RETURN is the result of lowering llvm.eh.return which in turn is
2055// generated from __builtin_eh_return (offset, handler)
2056// The effect of this is to adjust the stack pointer by "offset"
2057// and then branch to "handler".
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002058SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Akira Hatanakac0b02062013-01-30 00:26:49 +00002059 const {
2060 MachineFunction &MF = DAG.getMachineFunction();
2061 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2062
2063 MipsFI->setCallsEhReturn();
2064 SDValue Chain = Op.getOperand(0);
2065 SDValue Offset = Op.getOperand(1);
2066 SDValue Handler = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002067 SDLoc DL(Op);
Eric Christopher96e72c62015-01-29 23:27:36 +00002068 EVT Ty = ABI.IsN64() ? MVT::i64 : MVT::i32;
Akira Hatanakac0b02062013-01-30 00:26:49 +00002069
2070 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
2071 // EH_RETURN nodes, so that instructions are emitted back-to-back.
Eric Christopher96e72c62015-01-29 23:27:36 +00002072 unsigned OffsetReg = ABI.IsN64() ? Mips::V1_64 : Mips::V1;
2073 unsigned AddrReg = ABI.IsN64() ? Mips::V0_64 : Mips::V0;
Akira Hatanakac0b02062013-01-30 00:26:49 +00002074 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
2075 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
2076 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
2077 DAG.getRegister(OffsetReg, Ty),
Mehdi Amini44ede332015-07-09 02:09:04 +00002078 DAG.getRegister(AddrReg, getPointerTy(MF.getDataLayout())),
Akira Hatanakac0b02062013-01-30 00:26:49 +00002079 Chain.getValue(1));
2080}
2081
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002082SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
Akira Hatanaka5fd22482012-06-14 21:10:56 +00002083 SelectionDAG &DAG) const {
Eli Friedman26a48482011-07-27 22:21:52 +00002084 // FIXME: Need pseudo-fence for 'singlethread' fences
2085 // FIXME: Set SType for weaker fences where supported/appropriate.
2086 unsigned SType = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002087 SDLoc DL(Op);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002088 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002089 DAG.getConstant(SType, DL, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00002090}
2091
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002092SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
Akira Hatanaka5fd22482012-06-14 21:10:56 +00002093 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002094 SDLoc DL(Op);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002095 MVT VT = Subtarget.isGP64bit() ? MVT::i64 : MVT::i32;
2096
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002097 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2098 SDValue Shamt = Op.getOperand(2);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002099 // if shamt < (VT.bits):
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002100 // lo = (shl lo, shamt)
2101 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
2102 // else:
2103 // lo = 0
2104 // hi = (shl lo, shamt[4:0])
2105 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002106 DAG.getConstant(-1, DL, MVT::i32));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002107 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, VT, Lo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002108 DAG.getConstant(1, DL, VT));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002109 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, VT, ShiftRight1Lo, Not);
2110 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, VT, Hi, Shamt);
2111 SDValue Or = DAG.getNode(ISD::OR, DL, VT, ShiftLeftHi, ShiftRightLo);
2112 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, VT, Lo, Shamt);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002113 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
Daniel Sanders301f9372015-04-29 12:28:58 +00002114 DAG.getConstant(VT.getSizeInBits(), DL, MVT::i32));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002115 Lo = DAG.getNode(ISD::SELECT, DL, VT, Cond,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002116 DAG.getConstant(0, DL, VT), ShiftLeftLo);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002117 Hi = DAG.getNode(ISD::SELECT, DL, VT, Cond, ShiftLeftLo, Or);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002118
2119 SDValue Ops[2] = {Lo, Hi};
Craig Topper64941d92014-04-27 19:20:57 +00002120 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002121}
2122
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002123SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002124 bool IsSRA) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002125 SDLoc DL(Op);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002126 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2127 SDValue Shamt = Op.getOperand(2);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002128 MVT VT = Subtarget.isGP64bit() ? MVT::i64 : MVT::i32;
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002129
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002130 // if shamt < (VT.bits):
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002131 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
2132 // if isSRA:
2133 // hi = (sra hi, shamt)
2134 // else:
2135 // hi = (srl hi, shamt)
2136 // else:
2137 // if isSRA:
2138 // lo = (sra hi, shamt[4:0])
2139 // hi = (sra hi, 31)
2140 // else:
2141 // lo = (srl hi, shamt[4:0])
2142 // hi = 0
2143 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002144 DAG.getConstant(-1, DL, MVT::i32));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002145 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, VT, Hi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002146 DAG.getConstant(1, DL, VT));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002147 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, VT, ShiftLeft1Hi, Not);
2148 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, VT, Lo, Shamt);
2149 SDValue Or = DAG.getNode(ISD::OR, DL, VT, ShiftLeftHi, ShiftRightLo);
2150 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL,
2151 DL, VT, Hi, Shamt);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002152 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
Daniel Sanders301f9372015-04-29 12:28:58 +00002153 DAG.getConstant(VT.getSizeInBits(), DL, MVT::i32));
2154 SDValue Ext = DAG.getNode(ISD::SRA, DL, VT, Hi,
2155 DAG.getConstant(VT.getSizeInBits() - 1, DL, VT));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002156 Lo = DAG.getNode(ISD::SELECT, DL, VT, Cond, ShiftRightHi, Or);
2157 Hi = DAG.getNode(ISD::SELECT, DL, VT, Cond,
Daniel Sanders301f9372015-04-29 12:28:58 +00002158 IsSRA ? Ext : DAG.getConstant(0, DL, VT), ShiftRightHi);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002159
2160 SDValue Ops[2] = {Lo, Hi};
Craig Topper64941d92014-04-27 19:20:57 +00002161 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002162}
2163
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002164static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002165 SDValue Chain, SDValue Src, unsigned Offset) {
Akira Hatanaka95866182012-06-13 19:06:08 +00002166 SDValue Ptr = LD->getBasePtr();
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002167 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
Akira Hatanaka95866182012-06-13 19:06:08 +00002168 EVT BasePtrVT = Ptr.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002169 SDLoc DL(LD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002170 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
2171
2172 if (Offset)
Akira Hatanaka95866182012-06-13 19:06:08 +00002173 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002174 DAG.getConstant(Offset, DL, BasePtrVT));
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002175
2176 SDValue Ops[] = { Chain, Ptr, Src };
Craig Topper206fcd42014-04-26 19:29:41 +00002177 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002178 LD->getMemOperand());
2179}
2180
2181// Expand an unaligned 32 or 64-bit integer load node.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002182SDValue MipsTargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002183 LoadSDNode *LD = cast<LoadSDNode>(Op);
2184 EVT MemVT = LD->getMemoryVT();
2185
Eric Christopher1c29a652014-07-18 22:55:25 +00002186 if (Subtarget.systemSupportsUnalignedAccess())
Daniel Sandersac272632014-05-23 13:18:02 +00002187 return Op;
2188
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002189 // Return if load is aligned or if MemVT is neither i32 nor i64.
2190 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
2191 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
2192 return SDValue();
2193
Eric Christopher1c29a652014-07-18 22:55:25 +00002194 bool IsLittle = Subtarget.isLittle();
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002195 EVT VT = Op.getValueType();
2196 ISD::LoadExtType ExtType = LD->getExtensionType();
2197 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
2198
2199 assert((VT == MVT::i32) || (VT == MVT::i64));
2200
2201 // Expand
2202 // (set dst, (i64 (load baseptr)))
2203 // to
2204 // (set tmp, (ldl (add baseptr, 7), undef))
2205 // (set dst, (ldr baseptr, tmp))
2206 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002207 SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002208 IsLittle ? 7 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002209 return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002210 IsLittle ? 0 : 7);
2211 }
2212
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002213 SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002214 IsLittle ? 3 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002215 SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002216 IsLittle ? 0 : 3);
2217
2218 // Expand
2219 // (set dst, (i32 (load baseptr))) or
2220 // (set dst, (i64 (sextload baseptr))) or
2221 // (set dst, (i64 (extload baseptr)))
2222 // to
2223 // (set tmp, (lwl (add baseptr, 3), undef))
2224 // (set dst, (lwr baseptr, tmp))
2225 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
2226 (ExtType == ISD::EXTLOAD))
2227 return LWR;
2228
2229 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
2230
2231 // Expand
2232 // (set dst, (i64 (zextload baseptr)))
2233 // to
2234 // (set tmp0, (lwl (add baseptr, 3), undef))
2235 // (set tmp1, (lwr baseptr, tmp0))
2236 // (set tmp2, (shl tmp1, 32))
2237 // (set dst, (srl tmp2, 32))
Andrew Trickef9de2a2013-05-25 02:42:55 +00002238 SDLoc DL(LD);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002239 SDValue Const32 = DAG.getConstant(32, DL, MVT::i32);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002240 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
Akira Hatanaka67346852012-06-04 17:46:29 +00002241 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
2242 SDValue Ops[] = { SRL, LWR.getValue(1) };
Craig Topper64941d92014-04-27 19:20:57 +00002243 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002244}
2245
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002246static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002247 SDValue Chain, unsigned Offset) {
Akira Hatanaka95866182012-06-13 19:06:08 +00002248 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
2249 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002250 SDLoc DL(SD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002251 SDVTList VTList = DAG.getVTList(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, Value, Ptr };
Craig Topper206fcd42014-04-26 19:29:41 +00002258 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002259 SD->getMemOperand());
2260}
2261
2262// Expand an unaligned 32 or 64-bit integer store node.
Akira Hatanakad82ee942013-05-16 20:45:17 +00002263static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG,
2264 bool IsLittle) {
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002265 SDValue Value = SD->getValue(), Chain = SD->getChain();
2266 EVT VT = Value.getValueType();
2267
2268 // Expand
2269 // (store val, baseptr) or
2270 // (truncstore val, baseptr)
2271 // to
2272 // (swl val, (add baseptr, 3))
2273 // (swr val, baseptr)
2274 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002275 SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002276 IsLittle ? 3 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002277 return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002278 }
2279
2280 assert(VT == MVT::i64);
2281
2282 // Expand
2283 // (store val, baseptr)
2284 // to
2285 // (sdl val, (add baseptr, 7))
2286 // (sdr val, baseptr)
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002287 SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2288 return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002289}
2290
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002291// Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr).
2292static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG) {
2293 SDValue Val = SD->getValue();
2294
2295 if (Val.getOpcode() != ISD::FP_TO_SINT)
2296 return SDValue();
2297
2298 EVT FPTy = EVT::getFloatingPointVT(Val.getValueSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002299 SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy,
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002300 Val.getOperand(0));
2301
Andrew Trickef9de2a2013-05-25 02:42:55 +00002302 return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(),
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002303 SD->getPointerInfo(), SD->isVolatile(),
2304 SD->isNonTemporal(), SD->getAlignment());
2305}
2306
Akira Hatanakad82ee942013-05-16 20:45:17 +00002307SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2308 StoreSDNode *SD = cast<StoreSDNode>(Op);
2309 EVT MemVT = SD->getMemoryVT();
2310
2311 // Lower unaligned integer stores.
Eric Christopher1c29a652014-07-18 22:55:25 +00002312 if (!Subtarget.systemSupportsUnalignedAccess() &&
Daniel Sandersac272632014-05-23 13:18:02 +00002313 (SD->getAlignment() < MemVT.getSizeInBits() / 8) &&
Akira Hatanakad82ee942013-05-16 20:45:17 +00002314 ((MemVT == MVT::i32) || (MemVT == MVT::i64)))
Eric Christopher1c29a652014-07-18 22:55:25 +00002315 return lowerUnalignedIntStore(SD, DAG, Subtarget.isLittle());
Akira Hatanakad82ee942013-05-16 20:45:17 +00002316
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002317 return lowerFP_TO_SINT_STORE(SD, DAG);
Akira Hatanakad82ee942013-05-16 20:45:17 +00002318}
2319
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002320SDValue MipsTargetLowering::lowerADD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002321 if (Op->getOperand(0).getOpcode() != ISD::FRAMEADDR
2322 || cast<ConstantSDNode>
2323 (Op->getOperand(0).getOperand(0))->getZExtValue() != 0
2324 || Op->getOperand(1).getOpcode() != ISD::FRAME_TO_ARGS_OFFSET)
2325 return SDValue();
2326
2327 // The pattern
2328 // (add (frameaddr 0), (frame_to_args_offset))
2329 // results from lowering llvm.eh.dwarf.cfa intrinsic. Transform it to
2330 // (add FrameObject, 0)
2331 // where FrameObject is a fixed StackObject with offset 0 which points to
2332 // the old stack pointer.
2333 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2334 EVT ValTy = Op->getValueType(0);
2335 int FI = MFI->CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
2336 SDValue InArgsAddr = DAG.getFrameIndex(FI, ValTy);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002337 SDLoc DL(Op);
2338 return DAG.getNode(ISD::ADD, DL, ValTy, InArgsAddr,
2339 DAG.getConstant(0, DL, ValTy));
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002340}
2341
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002342SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op,
2343 SelectionDAG &DAG) const {
2344 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002345 SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy,
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002346 Op.getOperand(0));
Andrew Trickef9de2a2013-05-25 02:42:55 +00002347 return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc);
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002348}
2349
Akira Hatanakae2489122011-04-15 21:51:11 +00002350//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002351// Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002352//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002353
Akira Hatanakae2489122011-04-15 21:51:11 +00002354//===----------------------------------------------------------------------===//
Wesley Peck527da1b2010-11-23 03:31:01 +00002355// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002356// Mips O32 ABI rules:
2357// ---
2358// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peck527da1b2010-11-23 03:31:01 +00002359// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002360// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peck527da1b2010-11-23 03:31:01 +00002361// f64 - Only passed in two aliased f32 registers if no int reg has been used
2362// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Sylvestre Ledru469de192014-08-11 18:04:46 +00002363// not used, it must be shadowed. If only A3 is available, shadow it and
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002364// go to stack.
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002365//
2366// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanakae2489122011-04-15 21:51:11 +00002367//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002368
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00002369static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2370 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002371 CCState &State, ArrayRef<MCPhysReg> F64Regs) {
Eric Christopher96e72c62015-01-29 23:27:36 +00002372 const MipsSubtarget &Subtarget = static_cast<const MipsSubtarget &>(
2373 State.getMachineFunction().getSubtarget());
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002374
Craig Topper840beec2014-04-04 05:16:06 +00002375 static const MCPhysReg IntRegs[] = { Mips::A0, Mips::A1, Mips::A2, Mips::A3 };
2376 static const MCPhysReg F32Regs[] = { Mips::F12, Mips::F14 };
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002377
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002378 // Do not process byval args here.
2379 if (ArgFlags.isByVal())
2380 return true;
Akira Hatanaka5e16c6a2011-05-24 19:18:33 +00002381
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002382 // Promote i8 and i16
Daniel Sandersd134c9d2014-12-02 20:40:27 +00002383 if (ArgFlags.isInReg() && !Subtarget.isLittle()) {
2384 if (LocVT == MVT::i8 || LocVT == MVT::i16 || LocVT == MVT::i32) {
2385 LocVT = MVT::i32;
2386 if (ArgFlags.isSExt())
2387 LocInfo = CCValAssign::SExtUpper;
2388 else if (ArgFlags.isZExt())
2389 LocInfo = CCValAssign::ZExtUpper;
2390 else
2391 LocInfo = CCValAssign::AExtUpper;
2392 }
2393 }
2394
2395 // Promote i8 and i16
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002396 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
2397 LocVT = MVT::i32;
2398 if (ArgFlags.isSExt())
2399 LocInfo = CCValAssign::SExt;
2400 else if (ArgFlags.isZExt())
2401 LocInfo = CCValAssign::ZExt;
2402 else
2403 LocInfo = CCValAssign::AExt;
2404 }
2405
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002406 unsigned Reg;
2407
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002408 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
2409 // is true: function is vararg, argument is 3rd or higher, there is previous
2410 // argument which is not f32 or f64.
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002411 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1 ||
2412 State.getFirstUnallocated(F32Regs) != ValNo;
Akira Hatanaka9e6a8cc2011-05-19 20:29:48 +00002413 unsigned OrigAlign = ArgFlags.getOrigAlign();
2414 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002415
2416 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002417 Reg = State.AllocateReg(IntRegs);
Akira Hatanaka9e6a8cc2011-05-19 20:29:48 +00002418 // If this is the first part of an i64 arg,
2419 // the allocated register must be either A0 or A2.
2420 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002421 Reg = State.AllocateReg(IntRegs);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002422 LocVT = MVT::i32;
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002423 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
2424 // Allocate int register and shadow next int register. If first
2425 // available register is Mips::A1 or Mips::A3, shadow it too.
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002426 Reg = State.AllocateReg(IntRegs);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002427 if (Reg == Mips::A1 || Reg == Mips::A3)
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002428 Reg = State.AllocateReg(IntRegs);
2429 State.AllocateReg(IntRegs);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002430 LocVT = MVT::i32;
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002431 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
2432 // we are guaranteed to find an available float register
2433 if (ValVT == MVT::f32) {
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002434 Reg = State.AllocateReg(F32Regs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002435 // Shadow int register
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002436 State.AllocateReg(IntRegs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002437 } else {
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002438 Reg = State.AllocateReg(F64Regs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002439 // Shadow int registers
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002440 unsigned Reg2 = State.AllocateReg(IntRegs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002441 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002442 State.AllocateReg(IntRegs);
2443 State.AllocateReg(IntRegs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002444 }
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002445 } else
2446 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002447
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002448 if (!Reg) {
2449 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3,
2450 OrigAlign);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002451 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002452 } else
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002453 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002454
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002455 return false;
Akira Hatanaka202f6402011-11-12 02:20:46 +00002456}
2457
Akira Hatanakabfb66242013-08-20 23:38:40 +00002458static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT,
2459 MVT LocVT, CCValAssign::LocInfo LocInfo,
2460 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002461 static const MCPhysReg F64Regs[] = { Mips::D6, Mips::D7 };
Akira Hatanakabfb66242013-08-20 23:38:40 +00002462
2463 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2464}
2465
2466static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT,
2467 MVT LocVT, CCValAssign::LocInfo LocInfo,
2468 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002469 static const MCPhysReg F64Regs[] = { Mips::D12_64, Mips::D14_64 };
Akira Hatanakabfb66242013-08-20 23:38:40 +00002470
2471 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2472}
2473
Reid Klecknerd3781742014-11-14 00:39:33 +00002474static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2475 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
2476 CCState &State) LLVM_ATTRIBUTE_UNUSED;
Reed Kotlerd5c41962014-11-13 23:37:45 +00002477
Akira Hatanaka202f6402011-11-12 02:20:46 +00002478#include "MipsGenCallingConv.inc"
2479
Akira Hatanakae2489122011-04-15 21:51:11 +00002480//===----------------------------------------------------------------------===//
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002481// Call Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002482//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002483
Akira Hatanaka61bbcce2011-09-23 00:58:33 +00002484// Return next O32 integer argument register.
2485static unsigned getNextIntArgReg(unsigned Reg) {
2486 assert((Reg == Mips::A0) || (Reg == Mips::A2));
2487 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
2488}
2489
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002490SDValue
2491MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002492 SDValue Chain, SDValue Arg, SDLoc DL,
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002493 bool IsTailCall, SelectionDAG &DAG) const {
2494 if (!IsTailCall) {
Mehdi Amini44ede332015-07-09 02:09:04 +00002495 SDValue PtrOff =
2496 DAG.getNode(ISD::ADD, DL, getPointerTy(DAG.getDataLayout()), StackPtr,
2497 DAG.getIntPtrConstant(Offset, DL));
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002498 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo(), false,
2499 false, 0);
2500 }
2501
2502 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2503 int FI = MFI->CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00002504 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002505 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(),
2506 /*isVolatile=*/ true, false, 0);
2507}
2508
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002509void MipsTargetLowering::
2510getOpndList(SmallVectorImpl<SDValue> &Ops,
2511 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
2512 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
Sasa Stankovic7072a792014-10-01 08:22:21 +00002513 bool IsCallReloc, CallLoweringInfo &CLI, SDValue Callee,
2514 SDValue Chain) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002515 // Insert node "GP copy globalreg" before call to function.
2516 //
2517 // R_MIPS_CALL* operators (emitted when non-internal functions are called
2518 // in PIC mode) allow symbols to be resolved via lazy binding.
2519 // The lazy binding stub requires GP to point to the GOT.
Sasa Stankovic7072a792014-10-01 08:22:21 +00002520 // Note that we don't need GP to point to the GOT for indirect calls
2521 // (when R_MIPS_CALL* is not used for the call) because Mips linker generates
2522 // lazy binding stub for a function only when R_MIPS_CALL* are the only relocs
2523 // used for the function (that is, Mips linker doesn't generate lazy binding
2524 // stub for a function whose address is taken in the program).
2525 if (IsPICCall && !InternalLinkage && IsCallReloc) {
Eric Christopher96e72c62015-01-29 23:27:36 +00002526 unsigned GPReg = ABI.IsN64() ? Mips::GP_64 : Mips::GP;
2527 EVT Ty = ABI.IsN64() ? MVT::i64 : MVT::i32;
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002528 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty)));
2529 }
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002530
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002531 // Build a sequence of copy-to-reg nodes chained together with token
2532 // chain and flag operands which copy the outgoing args into registers.
2533 // The InFlag in necessary since all emitted instructions must be
2534 // stuck together.
2535 SDValue InFlag;
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002536
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002537 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2538 Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, RegsToPass[i].first,
2539 RegsToPass[i].second, InFlag);
2540 InFlag = Chain.getValue(1);
2541 }
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002542
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002543 // Add argument registers to the end of the list so that they are
2544 // known live into the call.
2545 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2546 Ops.push_back(CLI.DAG.getRegister(RegsToPass[i].first,
2547 RegsToPass[i].second.getValueType()));
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002548
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002549 // Add a register mask operand representing the call-preserved registers.
Eric Christopher96e72c62015-01-29 23:27:36 +00002550 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00002551 const uint32_t *Mask =
2552 TRI->getCallPreservedMask(CLI.DAG.getMachineFunction(), CLI.CallConv);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002553 assert(Mask && "Missing call preserved mask for calling convention");
Eric Christopher1c29a652014-07-18 22:55:25 +00002554 if (Subtarget.inMips16HardFloat()) {
Reed Kotler783c7942013-05-10 22:25:39 +00002555 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
2556 llvm::StringRef Sym = G->getGlobal()->getName();
2557 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
Reed Kotler3230e722013-12-12 02:41:11 +00002558 if (F && F->hasFnAttribute("__Mips16RetHelper")) {
Reed Kotler783c7942013-05-10 22:25:39 +00002559 Mask = MipsRegisterInfo::getMips16RetHelperMask();
2560 }
2561 }
2562 }
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002563 Ops.push_back(CLI.DAG.getRegisterMask(Mask));
2564
2565 if (InFlag.getNode())
2566 Ops.push_back(InFlag);
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002567}
2568
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002569/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman624801e2009-01-26 03:15:54 +00002570/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002571SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00002572MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002573 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00002574 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002575 SDLoc DL = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00002576 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
2577 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
2578 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Akira Hatanakabeda2242012-07-31 18:46:41 +00002579 SDValue Chain = CLI.Chain;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002580 SDValue Callee = CLI.Callee;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002581 bool &IsTailCall = CLI.IsTailCall;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002582 CallingConv::ID CallConv = CLI.CallConv;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002583 bool IsVarArg = CLI.IsVarArg;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002584
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002585 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002586 MachineFrameInfo *MFI = MF.getFrameInfo();
Eric Christopher96e72c62015-01-29 23:27:36 +00002587 const TargetFrameLowering *TFL = Subtarget.getFrameLowering();
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002588 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +00002589 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002590
2591 // Analyze operands of the call, assigning locations to each operand.
2592 SmallVector<CCValAssign, 16> ArgLocs;
Daniel Sanders41a64c42014-11-07 11:10:48 +00002593 MipsCCState CCInfo(
2594 CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs, *DAG.getContext(),
2595 MipsCCState::getSpecialCallingConvForCallee(Callee.getNode(), Subtarget));
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002596
2597 // Allocate the reserved argument area. It seems strange to do this from the
2598 // caller side but removing it breaks the frame size calculation.
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002599 CCInfo.AllocateStack(ABI.GetCalleeAllocdArgSizeInBytes(CallConv), 1);
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002600
Daniel Sanderscfad1e32014-11-07 11:43:49 +00002601 CCInfo.AnalyzeCallOperands(Outs, CC_Mips, CLI.getArgs(), Callee.getNode());
Wesley Peck527da1b2010-11-23 03:31:01 +00002602
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002603 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka195a1e22011-06-08 17:39:33 +00002604 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka97ba7692012-07-26 23:27:01 +00002605
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002606 // Check if it's really possible to do a tail call.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002607 if (IsTailCall)
Daniel Sanders23e98772014-11-02 16:09:29 +00002608 IsTailCall = isEligibleForTailCallOptimization(
2609 CCInfo, NextStackOffset, *MF.getInfo<MipsFunctionInfo>());
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002610
Reid Kleckner5772b772014-04-24 20:14:34 +00002611 if (!IsTailCall && CLI.CS && CLI.CS->isMustTailCall())
2612 report_fatal_error("failed to perform tail call elimination on a call "
2613 "site marked musttail");
2614
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002615 if (IsTailCall)
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002616 ++NumTailCalls;
2617
Akira Hatanaka79738332011-09-19 20:26:02 +00002618 // Chain is the output chain of the last Load/Store or CopyToReg node.
2619 // ByValChain is the output chain of the last Memcpy node created for copying
2620 // byval arguments to the stack.
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002621 unsigned StackAlignment = TFL->getStackAlignment();
2622 NextStackOffset = RoundUpToAlignment(NextStackOffset, StackAlignment);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002623 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, DL, true);
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002624
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002625 if (!IsTailCall)
Andrew Trickad6d08a2013-05-29 22:03:55 +00002626 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffsetVal, DL);
Akira Hatanakabeda2242012-07-31 18:46:41 +00002627
Mehdi Amini44ede332015-07-09 02:09:04 +00002628 SDValue StackPtr =
2629 DAG.getCopyFromReg(Chain, DL, ABI.IsN64() ? Mips::SP_64 : Mips::SP,
2630 getPointerTy(DAG.getDataLayout()));
Akira Hatanaka195a1e22011-06-08 17:39:33 +00002631
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002632 // With EABI is it possible to have 16 args on registers.
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002633 std::deque< std::pair<unsigned, SDValue> > RegsToPass;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002634 SmallVector<SDValue, 8> MemOpChains;
Daniel Sanders23e98772014-11-02 16:09:29 +00002635
2636 CCInfo.rewindByValRegsInfo();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002637
2638 // Walk the register/memloc assignments, inserting copies/loads.
2639 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002640 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002641 CCValAssign &VA = ArgLocs[i];
Akira Hatanakab20a3252011-10-28 19:49:00 +00002642 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka19891f82011-11-12 02:34:50 +00002643 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002644 bool UseUpperBits = false;
Akira Hatanaka19891f82011-11-12 02:34:50 +00002645
2646 // ByVal Arg.
2647 if (Flags.isByVal()) {
Daniel Sanders23e98772014-11-02 16:09:29 +00002648 unsigned FirstByValReg, LastByValReg;
2649 unsigned ByValIdx = CCInfo.getInRegsParamsProcessed();
2650 CCInfo.getInRegsParamInfo(ByValIdx, FirstByValReg, LastByValReg);
2651
Akira Hatanaka19891f82011-11-12 02:34:50 +00002652 assert(Flags.getByValSize() &&
2653 "ByVal args of size 0 should have been ignored by front-end.");
Daniel Sanders23e98772014-11-02 16:09:29 +00002654 assert(ByValIdx < CCInfo.getInRegsParamsCount());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002655 assert(!IsTailCall &&
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002656 "Do not tail-call optimize if there is a byval argument.");
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002657 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002658 FirstByValReg, LastByValReg, Flags, Subtarget.isLittle(),
2659 VA);
Daniel Sanders23e98772014-11-02 16:09:29 +00002660 CCInfo.nextInRegsParam();
Akira Hatanaka19891f82011-11-12 02:34:50 +00002661 continue;
2662 }
Jia Liuf54f60f2012-02-28 07:46:26 +00002663
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002664 // Promote the value if needed.
2665 switch (VA.getLocInfo()) {
Daniel Sandersc43cda82014-11-07 16:54:21 +00002666 default:
2667 llvm_unreachable("Unknown loc info!");
Wesley Peck527da1b2010-11-23 03:31:01 +00002668 case CCValAssign::Full:
Akira Hatanakab20a3252011-10-28 19:49:00 +00002669 if (VA.isRegLoc()) {
2670 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
Akira Hatanaka3b7391d2013-03-05 22:20:28 +00002671 (ValVT == MVT::f64 && LocVT == MVT::i64) ||
2672 (ValVT == MVT::i64 && LocVT == MVT::f64))
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002673 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
Akira Hatanakab20a3252011-10-28 19:49:00 +00002674 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002675 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002676 Arg, DAG.getConstant(0, DL, MVT::i32));
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002677 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002678 Arg, DAG.getConstant(1, DL, MVT::i32));
Eric Christopher1c29a652014-07-18 22:55:25 +00002679 if (!Subtarget.isLittle())
Akira Hatanaka27916972011-04-15 19:52:08 +00002680 std::swap(Lo, Hi);
Jia Liuf54f60f2012-02-28 07:46:26 +00002681 unsigned LocRegLo = VA.getLocReg();
Akira Hatanaka61bbcce2011-09-23 00:58:33 +00002682 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2683 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2684 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002685 continue;
Wesley Peck527da1b2010-11-23 03:31:01 +00002686 }
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002687 }
2688 break;
Daniel Sanders23e98772014-11-02 16:09:29 +00002689 case CCValAssign::BCvt:
2690 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
2691 break;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002692 case CCValAssign::SExtUpper:
2693 UseUpperBits = true;
2694 // Fallthrough
Chris Lattner52f16de2008-03-17 06:57:02 +00002695 case CCValAssign::SExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002696 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002697 break;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002698 case CCValAssign::ZExtUpper:
2699 UseUpperBits = true;
2700 // Fallthrough
Chris Lattner52f16de2008-03-17 06:57:02 +00002701 case CCValAssign::ZExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002702 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002703 break;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002704 case CCValAssign::AExtUpper:
2705 UseUpperBits = true;
2706 // Fallthrough
Chris Lattner52f16de2008-03-17 06:57:02 +00002707 case CCValAssign::AExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002708 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002709 break;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002710 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002711
Daniel Sandersc43cda82014-11-07 16:54:21 +00002712 if (UseUpperBits) {
2713 unsigned ValSizeInBits = Outs[i].ArgVT.getSizeInBits();
2714 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
2715 Arg = DAG.getNode(
2716 ISD::SHL, DL, VA.getLocVT(), Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002717 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
Daniel Sandersc43cda82014-11-07 16:54:21 +00002718 }
2719
Wesley Peck527da1b2010-11-23 03:31:01 +00002720 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +00002721 // RegsToPass vector
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002722 if (VA.isRegLoc()) {
2723 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattner52f16de2008-03-17 06:57:02 +00002724 continue;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002725 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002726
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002727 // Register can't get to this point...
Chris Lattner52f16de2008-03-17 06:57:02 +00002728 assert(VA.isMemLoc());
Wesley Peck527da1b2010-11-23 03:31:01 +00002729
Wesley Peck527da1b2010-11-23 03:31:01 +00002730 // emit ISD::STORE whichs stores the
Chris Lattner52f16de2008-03-17 06:57:02 +00002731 // parameter value to a stack Location
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002732 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002733 Chain, Arg, DL, IsTailCall, DAG));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002734 }
2735
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002736 // Transform all store nodes into one single node because all store
2737 // nodes are independent of each other.
Wesley Peck527da1b2010-11-23 03:31:01 +00002738 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00002739 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002740
Bill Wendling24c79f22008-09-16 21:48:12 +00002741 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peck527da1b2010-11-23 03:31:01 +00002742 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2743 // node so that legalize doesn't hack it.
Eric Christopher96e72c62015-01-29 23:27:36 +00002744 bool IsPICCall = (ABI.IsN64() || IsPIC); // true if calls are translated to
2745 // jalr $25
Sasa Stankovic7072a792014-10-01 08:22:21 +00002746 bool GlobalOrExternal = false, InternalLinkage = false, IsCallReloc = false;
Akira Hatanakad6f1c582011-04-07 19:51:44 +00002747 SDValue CalleeLo;
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002748 EVT Ty = Callee.getValueType();
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002749
2750 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002751 if (IsPICCall) {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002752 const GlobalValue *Val = G->getGlobal();
2753 InternalLinkage = Val->hasInternalLinkage();
Akira Hatanakacf9a61b2012-12-13 03:17:29 +00002754
2755 if (InternalLinkage)
Eric Christopher96e72c62015-01-29 23:27:36 +00002756 Callee = getAddrLocal(G, DL, Ty, DAG, ABI.IsN32() || ABI.IsN64());
Sasa Stankovic7072a792014-10-01 08:22:21 +00002757 else if (LargeGOT) {
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002758 Callee = getAddrGlobalLargeGOT(G, DL, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002759 MipsII::MO_CALL_LO16, Chain,
2760 FuncInfo->callPtrInfo(Val));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002761 IsCallReloc = true;
2762 } else {
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002763 Callee = getAddrGlobal(G, DL, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002764 FuncInfo->callPtrInfo(Val));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002765 IsCallReloc = true;
2766 }
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002767 } else
Mehdi Amini44ede332015-07-09 02:09:04 +00002768 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL,
2769 getPointerTy(DAG.getDataLayout()), 0,
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002770 MipsII::MO_NO_FLAG);
Akira Hatanaka8e16aac2011-12-09 01:45:12 +00002771 GlobalOrExternal = true;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002772 }
2773 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002774 const char *Sym = S->getSymbol();
2775
Eric Christopher96e72c62015-01-29 23:27:36 +00002776 if (!ABI.IsN64() && !IsPIC) // !N64 && static
Mehdi Amini44ede332015-07-09 02:09:04 +00002777 Callee = DAG.getTargetExternalSymbol(
2778 Sym, getPointerTy(DAG.getDataLayout()), MipsII::MO_NO_FLAG);
Sasa Stankovic7072a792014-10-01 08:22:21 +00002779 else if (LargeGOT) {
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002780 Callee = getAddrGlobalLargeGOT(S, DL, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002781 MipsII::MO_CALL_LO16, Chain,
2782 FuncInfo->callPtrInfo(Sym));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002783 IsCallReloc = true;
2784 } else { // N64 || PIC
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002785 Callee = getAddrGlobal(S, DL, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002786 FuncInfo->callPtrInfo(Sym));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002787 IsCallReloc = true;
2788 }
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002789
Akira Hatanaka8e16aac2011-12-09 01:45:12 +00002790 GlobalOrExternal = true;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002791 }
2792
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002793 SmallVector<SDValue, 8> Ops(1, Chain);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002794 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002795
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002796 getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal, InternalLinkage,
Sasa Stankovic7072a792014-10-01 08:22:21 +00002797 IsCallReloc, CLI, Callee, Chain);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002798
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002799 if (IsTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +00002800 return DAG.getNode(MipsISD::TailCall, DL, MVT::Other, Ops);
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002801
Craig Topper48d114b2014-04-26 18:35:24 +00002802 Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, Ops);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002803 SDValue InFlag = Chain.getValue(1);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002804
Bruno Cardoso Lopes193e64c2010-01-30 18:32:07 +00002805 // Create the CALLSEQ_END node.
Akira Hatanaka97ba7692012-07-26 23:27:01 +00002806 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002807 DAG.getIntPtrConstant(0, DL, true), InFlag, DL);
Bruno Cardoso Lopes193e64c2010-01-30 18:32:07 +00002808 InFlag = Chain.getValue(1);
2809
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002810 // Handle result values, copying them out of physregs into vregs that we
2811 // return.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002812 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg, Ins, DL, DAG,
2813 InVals, CLI);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002814}
2815
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002816/// LowerCallResult - Lower the result values of a call into the
2817/// appropriate copies out of appropriate physical registers.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002818SDValue MipsTargetLowering::LowerCallResult(
2819 SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool IsVarArg,
2820 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG,
2821 SmallVectorImpl<SDValue> &InVals,
2822 TargetLowering::CallLoweringInfo &CLI) const {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002823 // Assign locations to each value returned by this call.
2824 SmallVector<CCValAssign, 16> RVLocs;
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002825 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
2826 *DAG.getContext());
2827 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips, CLI);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002828
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002829 // Copy all of the result registers out of their specified physreg.
2830 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Daniel Sandersae275e32014-09-25 12:15:05 +00002831 CCValAssign &VA = RVLocs[i];
2832 assert(VA.isRegLoc() && "Can only return in registers!");
2833
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002834 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(),
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002835 RVLocs[i].getLocVT(), InFlag);
2836 Chain = Val.getValue(1);
2837 InFlag = Val.getValue(2);
2838
Daniel Sandersae275e32014-09-25 12:15:05 +00002839 if (VA.isUpperBitsInLoc()) {
2840 unsigned ValSizeInBits = Ins[i].ArgVT.getSizeInBits();
2841 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
2842 unsigned Shift =
2843 VA.getLocInfo() == CCValAssign::ZExtUpper ? ISD::SRL : ISD::SRA;
2844 Val = DAG.getNode(
2845 Shift, DL, VA.getLocVT(), Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002846 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
Daniel Sandersae275e32014-09-25 12:15:05 +00002847 }
2848
2849 switch (VA.getLocInfo()) {
2850 default:
2851 llvm_unreachable("Unknown loc info!");
2852 case CCValAssign::Full:
2853 break;
2854 case CCValAssign::BCvt:
2855 Val = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Val);
2856 break;
2857 case CCValAssign::AExt:
2858 case CCValAssign::AExtUpper:
2859 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2860 break;
2861 case CCValAssign::ZExt:
2862 case CCValAssign::ZExtUpper:
2863 Val = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Val,
2864 DAG.getValueType(VA.getValVT()));
2865 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2866 break;
2867 case CCValAssign::SExt:
2868 case CCValAssign::SExtUpper:
2869 Val = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Val,
2870 DAG.getValueType(VA.getValVT()));
2871 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2872 break;
2873 }
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002874
2875 InVals.push_back(Val);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002876 }
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +00002877
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002878 return Chain;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002879}
2880
Daniel Sandersc43cda82014-11-07 16:54:21 +00002881static SDValue UnpackFromArgumentSlot(SDValue Val, const CCValAssign &VA,
2882 EVT ArgVT, SDLoc DL, SelectionDAG &DAG) {
2883 MVT LocVT = VA.getLocVT();
2884 EVT ValVT = VA.getValVT();
2885
2886 // Shift into the upper bits if necessary.
2887 switch (VA.getLocInfo()) {
2888 default:
2889 break;
2890 case CCValAssign::AExtUpper:
2891 case CCValAssign::SExtUpper:
2892 case CCValAssign::ZExtUpper: {
2893 unsigned ValSizeInBits = ArgVT.getSizeInBits();
2894 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
2895 unsigned Opcode =
2896 VA.getLocInfo() == CCValAssign::ZExtUpper ? ISD::SRL : ISD::SRA;
2897 Val = DAG.getNode(
2898 Opcode, DL, VA.getLocVT(), Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002899 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
Daniel Sandersc43cda82014-11-07 16:54:21 +00002900 break;
2901 }
2902 }
2903
2904 // If this is an value smaller than the argument slot size (32-bit for O32,
2905 // 64-bit for N32/N64), it has been promoted in some way to the argument slot
2906 // size. Extract the value and insert any appropriate assertions regarding
2907 // sign/zero extension.
2908 switch (VA.getLocInfo()) {
2909 default:
2910 llvm_unreachable("Unknown loc info!");
2911 case CCValAssign::Full:
2912 break;
2913 case CCValAssign::AExtUpper:
2914 case CCValAssign::AExt:
2915 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
2916 break;
2917 case CCValAssign::SExtUpper:
2918 case CCValAssign::SExt:
2919 Val = DAG.getNode(ISD::AssertSext, DL, LocVT, Val, DAG.getValueType(ValVT));
2920 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
2921 break;
2922 case CCValAssign::ZExtUpper:
2923 case CCValAssign::ZExt:
2924 Val = DAG.getNode(ISD::AssertZext, DL, LocVT, Val, DAG.getValueType(ValVT));
2925 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
2926 break;
2927 case CCValAssign::BCvt:
2928 Val = DAG.getNode(ISD::BITCAST, DL, ValVT, Val);
2929 break;
2930 }
2931
2932 return Val;
2933}
2934
Akira Hatanakae2489122011-04-15 21:51:11 +00002935//===----------------------------------------------------------------------===//
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002936// Formal Arguments Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002937//===----------------------------------------------------------------------===//
Wesley Peck527da1b2010-11-23 03:31:01 +00002938/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002939/// and generate load operations for arguments places on the stack.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002940SDValue
2941MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanakaaef55c82011-04-15 21:00:26 +00002942 CallingConv::ID CallConv,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002943 bool IsVarArg,
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00002944 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002945 SDLoc DL, SelectionDAG &DAG,
Akira Hatanakaaef55c82011-04-15 21:00:26 +00002946 SmallVectorImpl<SDValue> &InVals)
Akira Hatanakae2489122011-04-15 21:51:11 +00002947 const {
Bruno Cardoso Lopesa01ede22008-08-04 07:12:52 +00002948 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002949 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopes14033fb2007-08-28 05:08:16 +00002950 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002951
Dan Gohman31ae5862010-04-17 14:41:14 +00002952 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002953
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002954 // Used with vargs to acumulate store chains.
2955 std::vector<SDValue> OutChains;
2956
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002957 // Assign locations to all of the incoming arguments.
2958 SmallVector<CCValAssign, 16> ArgLocs;
Daniel Sanders23e98772014-11-02 16:09:29 +00002959 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
2960 *DAG.getContext());
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002961 CCInfo.AllocateStack(ABI.GetCalleeAllocdArgSizeInBytes(CallConv), 1);
Vasileios Kalintiris43dff0c2015-10-26 12:38:43 +00002962 const Function *Func = DAG.getMachineFunction().getFunction();
2963 Function::const_arg_iterator FuncArg = Func->arg_begin();
2964
Vasileios Kalintiris165121f2015-10-26 14:24:30 +00002965 if (Func->hasFnAttribute("interrupt") && !Func->arg_empty())
2966 report_fatal_error(
2967 "Functions with the interrupt attribute cannot have arguments!");
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002968
Daniel Sandersb70e27c2014-11-06 16:36:30 +00002969 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips_FixedArg);
Akira Hatanaka4866fe12012-10-30 19:37:25 +00002970 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(),
Daniel Sanders23e98772014-11-02 16:09:29 +00002971 CCInfo.getInRegsParamsCount() > 0);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002972
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00002973 unsigned CurArgIdx = 0;
Daniel Sanders23e98772014-11-02 16:09:29 +00002974 CCInfo.rewindByValRegsInfo();
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002975
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00002976 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002977 CCValAssign &VA = ArgLocs[i];
Andrew Trick05938a52015-02-16 18:10:47 +00002978 if (Ins[i].isOrigArg()) {
2979 std::advance(FuncArg, Ins[i].getOrigArgIndex() - CurArgIdx);
2980 CurArgIdx = Ins[i].getOrigArgIndex();
2981 }
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002982 EVT ValVT = VA.getValVT();
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002983 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2984 bool IsRegLoc = VA.isRegLoc();
2985
2986 if (Flags.isByVal()) {
Andrew Trick05938a52015-02-16 18:10:47 +00002987 assert(Ins[i].isOrigArg() && "Byval arguments cannot be implicit");
Daniel Sanders23e98772014-11-02 16:09:29 +00002988 unsigned FirstByValReg, LastByValReg;
2989 unsigned ByValIdx = CCInfo.getInRegsParamsProcessed();
2990 CCInfo.getInRegsParamInfo(ByValIdx, FirstByValReg, LastByValReg);
2991
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002992 assert(Flags.getByValSize() &&
2993 "ByVal args of size 0 should have been ignored by front-end.");
Daniel Sanders23e98772014-11-02 16:09:29 +00002994 assert(ByValIdx < CCInfo.getInRegsParamsCount());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002995 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002996 FirstByValReg, LastByValReg, VA, CCInfo);
Daniel Sanders23e98772014-11-02 16:09:29 +00002997 CCInfo.nextInRegsParam();
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002998 continue;
2999 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003000
3001 // Arguments stored on registers
Akira Hatanakafb9bae32011-11-12 02:29:58 +00003002 if (IsRegLoc) {
Akira Hatanaka7d822522013-10-28 21:21:36 +00003003 MVT RegVT = VA.getLocVT();
Akira Hatanakacb4a1a82011-05-24 00:23:52 +00003004 unsigned ArgReg = VA.getLocReg();
Akira Hatanaka7d822522013-10-28 21:21:36 +00003005 const TargetRegisterClass *RC = getRegClassFor(RegVT);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003006
Wesley Peck527da1b2010-11-23 03:31:01 +00003007 // Transform the arguments stored on
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003008 // physical registers into virtual ones
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003009 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC);
3010 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
Wesley Peck527da1b2010-11-23 03:31:01 +00003011
Daniel Sandersc43cda82014-11-07 16:54:21 +00003012 ArgValue = UnpackFromArgumentSlot(ArgValue, VA, Ins[i].ArgVT, DL, DAG);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00003013
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003014 // Handle floating point arguments passed in integer registers and
3015 // long double arguments passed in floating point registers.
Akira Hatanaka104b7e32011-10-28 19:55:48 +00003016 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003017 (RegVT == MVT::i64 && ValVT == MVT::f64) ||
3018 (RegVT == MVT::f64 && ValVT == MVT::i64))
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003019 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
Eric Christopher96e72c62015-01-29 23:27:36 +00003020 else if (ABI.IsO32() && RegVT == MVT::i32 &&
Eric Christopherbf33a3c2014-07-02 23:18:40 +00003021 ValVT == MVT::f64) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003022 unsigned Reg2 = addLiveIn(DAG.getMachineFunction(),
Akira Hatanaka104b7e32011-10-28 19:55:48 +00003023 getNextIntArgReg(ArgReg), RC);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003024 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT);
Eric Christopher1c29a652014-07-18 22:55:25 +00003025 if (!Subtarget.isLittle())
Akira Hatanaka104b7e32011-10-28 19:55:48 +00003026 std::swap(ArgValue, ArgValue2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003027 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64,
Akira Hatanaka104b7e32011-10-28 19:55:48 +00003028 ArgValue, ArgValue2);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00003029 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003030
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003031 InVals.push_back(ArgValue);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003032 } else { // VA.isRegLoc()
Daniel Sandersc43cda82014-11-07 16:54:21 +00003033 MVT LocVT = VA.getLocVT();
3034
Eric Christopher96e72c62015-01-29 23:27:36 +00003035 if (ABI.IsO32()) {
Daniel Sandersc43cda82014-11-07 16:54:21 +00003036 // We ought to be able to use LocVT directly but O32 sets it to i32
3037 // when allocating floating point values to integer registers.
3038 // This shouldn't influence how we load the value into registers unless
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00003039 // we are targeting softfloat.
Eric Christophere8ae3e32015-05-07 23:10:21 +00003040 if (VA.getValVT().isFloatingPoint() && !Subtarget.useSoftFloat())
Daniel Sandersc43cda82014-11-07 16:54:21 +00003041 LocVT = VA.getValVT();
3042 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003043
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003044 // sanity check
3045 assert(VA.isMemLoc());
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003046
Wesley Peck527da1b2010-11-23 03:31:01 +00003047 // The stack pointer offset is relative to the caller stack frame.
Daniel Sandersc43cda82014-11-07 16:54:21 +00003048 int FI = MFI->CreateFixedObject(LocVT.getSizeInBits() / 8,
Akira Hatanakacb4a1a82011-05-24 00:23:52 +00003049 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003050
3051 // Create load nodes to retrieve arguments from the stack
Mehdi Amini44ede332015-07-09 02:09:04 +00003052 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Alex Lorenze40c8a22015-08-11 23:09:45 +00003053 SDValue ArgValue = DAG.getLoad(
3054 LocVT, DL, Chain, FIN,
3055 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI),
3056 false, false, false, 0);
Daniel Sandersc43cda82014-11-07 16:54:21 +00003057 OutChains.push_back(ArgValue.getValue(1));
3058
3059 ArgValue = UnpackFromArgumentSlot(ArgValue, VA, Ins[i].ArgVT, DL, DAG);
3060
3061 InVals.push_back(ArgValue);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003062 }
Reid Kleckner7a59e082014-05-12 22:01:27 +00003063 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003064
Reid Kleckner7a59e082014-05-12 22:01:27 +00003065 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Reid Kleckner79418562014-05-09 22:32:13 +00003066 // The mips ABIs for returning structs by value requires that we copy
3067 // the sret argument into $v0 for the return. Save the argument into
3068 // a virtual register so that we can access it from the return points.
Reid Kleckner7a59e082014-05-12 22:01:27 +00003069 if (Ins[i].Flags.isSRet()) {
Reid Kleckner79418562014-05-09 22:32:13 +00003070 unsigned Reg = MipsFI->getSRetReturnReg();
3071 if (!Reg) {
3072 Reg = MF.getRegInfo().createVirtualRegister(
Eric Christopher96e72c62015-01-29 23:27:36 +00003073 getRegClassFor(ABI.IsN64() ? MVT::i64 : MVT::i32));
Reid Kleckner79418562014-05-09 22:32:13 +00003074 MipsFI->setSRetReturnReg(Reg);
3075 }
Reid Kleckner7a59e082014-05-12 22:01:27 +00003076 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[i]);
Reid Kleckner79418562014-05-09 22:32:13 +00003077 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
Reid Kleckner7a59e082014-05-12 22:01:27 +00003078 break;
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003079 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003080 }
3081
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003082 if (IsVarArg)
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003083 writeVarArgRegs(OutChains, Chain, DL, DAG, CCInfo);
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003084
Wesley Peck527da1b2010-11-23 03:31:01 +00003085 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003086 // the size of Ins and InVals. This only happens when on varg functions
3087 if (!OutChains.empty()) {
3088 OutChains.push_back(Chain);
Craig Topper48d114b2014-04-26 18:35:24 +00003089 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003090 }
3091
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003092 return Chain;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003093}
3094
Akira Hatanakae2489122011-04-15 21:51:11 +00003095//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003096// Return Value Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00003097//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003098
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003099bool
3100MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003101 MachineFunction &MF, bool IsVarArg,
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003102 const SmallVectorImpl<ISD::OutputArg> &Outs,
3103 LLVMContext &Context) const {
3104 SmallVector<CCValAssign, 16> RVLocs;
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003105 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003106 return CCInfo.CheckReturn(Outs, RetCC_Mips);
3107}
3108
Petar Jovanovic5b436222015-03-23 12:28:13 +00003109bool
3110MipsTargetLowering::shouldSignExtendTypeInLibCall(EVT Type, bool IsSigned) const {
Eric Christophere8ae3e32015-05-07 23:10:21 +00003111 if (Subtarget.hasMips3() && Subtarget.useSoftFloat()) {
Petar Jovanovic5b436222015-03-23 12:28:13 +00003112 if (Type == MVT::i32)
3113 return true;
3114 }
3115 return IsSigned;
3116}
3117
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003118SDValue
Vasileios Kalintiris43dff0c2015-10-26 12:38:43 +00003119MipsTargetLowering::LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps,
3120 SDLoc DL, SelectionDAG &DAG) const {
3121
3122 MachineFunction &MF = DAG.getMachineFunction();
3123 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3124
3125 MipsFI->setISR();
3126
3127 return DAG.getNode(MipsISD::ERet, DL, MVT::Other, RetOps);
3128}
3129
3130SDValue
3131MipsTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
3132 bool IsVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003133 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003134 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003135 SDLoc DL, SelectionDAG &DAG) const {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003136 // CCValAssign - represent the assignment of
3137 // the return value to a location
3138 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003139 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003140
3141 // CCState - Info about the registers and stack slot.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003142 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, *DAG.getContext());
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003143
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003144 // Analyze return values.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003145 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003146
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003147 SDValue Flag;
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003148 SmallVector<SDValue, 4> RetOps(1, Chain);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003149
3150 // Copy the result values into the output registers.
3151 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003152 SDValue Val = OutVals[i];
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003153 CCValAssign &VA = RVLocs[i];
3154 assert(VA.isRegLoc() && "Can only return in registers!");
Daniel Sandersae275e32014-09-25 12:15:05 +00003155 bool UseUpperBits = false;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003156
Daniel Sandersae275e32014-09-25 12:15:05 +00003157 switch (VA.getLocInfo()) {
3158 default:
3159 llvm_unreachable("Unknown loc info!");
3160 case CCValAssign::Full:
3161 break;
3162 case CCValAssign::BCvt:
3163 Val = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Val);
3164 break;
3165 case CCValAssign::AExtUpper:
3166 UseUpperBits = true;
3167 // Fallthrough
3168 case CCValAssign::AExt:
3169 Val = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Val);
3170 break;
3171 case CCValAssign::ZExtUpper:
3172 UseUpperBits = true;
3173 // Fallthrough
3174 case CCValAssign::ZExt:
3175 Val = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Val);
3176 break;
3177 case CCValAssign::SExtUpper:
3178 UseUpperBits = true;
3179 // Fallthrough
3180 case CCValAssign::SExt:
3181 Val = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Val);
3182 break;
3183 }
3184
3185 if (UseUpperBits) {
3186 unsigned ValSizeInBits = Outs[i].ArgVT.getSizeInBits();
3187 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
3188 Val = DAG.getNode(
3189 ISD::SHL, DL, VA.getLocVT(), Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003190 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
Daniel Sandersae275e32014-09-25 12:15:05 +00003191 }
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003192
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003193 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003194
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003195 // Guarantee that all emitted copies are stuck together with flags.
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003196 Flag = Chain.getValue(1);
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003197 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003198 }
3199
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003200 // The mips ABIs for returning structs by value requires that we copy
3201 // the sret argument into $v0 for the return. We saved the argument into
3202 // a virtual register in the entry block, so now we copy the value out
3203 // and into $v0.
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003204 if (MF.getFunction()->hasStructRetAttr()) {
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003205 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3206 unsigned Reg = MipsFI->getSRetReturnReg();
3207
Wesley Peck527da1b2010-11-23 03:31:01 +00003208 if (!Reg)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003209 llvm_unreachable("sret virtual register not created in the entry block");
Mehdi Amini44ede332015-07-09 02:09:04 +00003210 SDValue Val =
3211 DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(DAG.getDataLayout()));
Eric Christopher96e72c62015-01-29 23:27:36 +00003212 unsigned V0 = ABI.IsN64() ? Mips::V0_64 : Mips::V0;
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003213
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003214 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003215 Flag = Chain.getValue(1);
Mehdi Amini44ede332015-07-09 02:09:04 +00003216 RetOps.push_back(DAG.getRegister(V0, getPointerTy(DAG.getDataLayout())));
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003217 }
3218
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003219 RetOps[0] = Chain; // Update chain.
Akira Hatanakaefff7b72012-07-10 00:19:06 +00003220
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003221 // Add the flag if we have it.
3222 if (Flag.getNode())
3223 RetOps.push_back(Flag);
3224
Vasileios Kalintiris43dff0c2015-10-26 12:38:43 +00003225 // ISRs must use "eret".
3226 if (DAG.getMachineFunction().getFunction()->hasFnAttribute("interrupt"))
3227 return LowerInterruptReturn(RetOps, DL, DAG);
3228
3229 // Standard return on Mips is a "jr $ra"
Craig Topper48d114b2014-04-26 18:35:24 +00003230 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, RetOps);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003231}
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003232
Akira Hatanakae2489122011-04-15 21:51:11 +00003233//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003234// Mips Inline Assembly Support
Akira Hatanakae2489122011-04-15 21:51:11 +00003235//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003236
3237/// getConstraintType - Given a constraint letter, return the type of
3238/// constraint it is for this target.
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003239MipsTargetLowering::ConstraintType
3240MipsTargetLowering::getConstraintType(StringRef Constraint) const {
Daniel Sanders8b59af12013-11-12 12:56:01 +00003241 // Mips specific constraints
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003242 // GCC config/mips/constraints.md
3243 //
Wesley Peck527da1b2010-11-23 03:31:01 +00003244 // 'd' : An address register. Equivalent to r
3245 // unless generating MIPS16 code.
3246 // 'y' : Equivalent to r; retained for
3247 // backwards compatibility.
Eric Christophere3c494d2012-05-07 06:25:10 +00003248 // 'c' : A register suitable for use in an indirect
3249 // jump. This will always be $25 for -mabicalls.
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003250 // 'l' : The lo register. 1 word storage.
3251 // 'x' : The hilo register pair. Double word storage.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003252 if (Constraint.size() == 1) {
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003253 switch (Constraint[0]) {
3254 default : break;
Wesley Peck527da1b2010-11-23 03:31:01 +00003255 case 'd':
3256 case 'y':
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003257 case 'f':
Eric Christophere3c494d2012-05-07 06:25:10 +00003258 case 'c':
Eric Christopher9c492e62012-05-07 06:25:15 +00003259 case 'l':
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003260 case 'x':
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003261 return C_RegisterClass;
Jack Carter0e149b02013-03-04 21:33:15 +00003262 case 'R':
3263 return C_Memory;
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003264 }
3265 }
Daniel Sandersa73d8fe2015-03-24 11:26:34 +00003266
3267 if (Constraint == "ZC")
3268 return C_Memory;
3269
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003270 return TargetLowering::getConstraintType(Constraint);
3271}
3272
John Thompsone8360b72010-10-29 17:29:13 +00003273/// Examine constraint type and operand type and determine a weight value.
3274/// This object must already have been set up with the operand type
3275/// and the current alternative constraint selected.
3276TargetLowering::ConstraintWeight
3277MipsTargetLowering::getSingleConstraintMatchWeight(
3278 AsmOperandInfo &info, const char *constraint) const {
3279 ConstraintWeight weight = CW_Invalid;
3280 Value *CallOperandVal = info.CallOperandVal;
3281 // If we don't have a value, we can't do a match,
3282 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +00003283 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +00003284 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +00003285 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +00003286 // Look at the constraint type.
3287 switch (*constraint) {
3288 default:
3289 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
3290 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00003291 case 'd':
3292 case 'y':
John Thompsone8360b72010-10-29 17:29:13 +00003293 if (type->isIntegerTy())
3294 weight = CW_Register;
3295 break;
Daniel Sanders8b59af12013-11-12 12:56:01 +00003296 case 'f': // FPU or MSA register
Eric Christopher1c29a652014-07-18 22:55:25 +00003297 if (Subtarget.hasMSA() && type->isVectorTy() &&
Daniel Sanders8b59af12013-11-12 12:56:01 +00003298 cast<VectorType>(type)->getBitWidth() == 128)
3299 weight = CW_Register;
3300 else if (type->isFloatTy())
John Thompsone8360b72010-10-29 17:29:13 +00003301 weight = CW_Register;
3302 break;
Eric Christophere3c494d2012-05-07 06:25:10 +00003303 case 'c': // $25 for indirect jumps
Eric Christopher9c492e62012-05-07 06:25:15 +00003304 case 'l': // lo register
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003305 case 'x': // hilo register pair
Daniel Sanders8b59af12013-11-12 12:56:01 +00003306 if (type->isIntegerTy())
Eric Christophere3c494d2012-05-07 06:25:10 +00003307 weight = CW_SpecificReg;
Daniel Sanders8b59af12013-11-12 12:56:01 +00003308 break;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003309 case 'I': // signed 16 bit immediate
Eric Christopher7201e1b2012-05-07 03:13:42 +00003310 case 'J': // integer zero
Eric Christopher3ff88a02012-05-07 05:46:29 +00003311 case 'K': // unsigned 16 bit immediate
Eric Christopher1109b342012-05-07 05:46:37 +00003312 case 'L': // signed 32 bit immediate where lower 16 bits are 0
Eric Christophere07aa432012-05-07 05:46:43 +00003313 case 'N': // immediate in the range of -65535 to -1 (inclusive)
Eric Christopher470578a2012-05-07 05:46:48 +00003314 case 'O': // signed 15 bit immediate (+- 16383)
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003315 case 'P': // immediate in the range of 65535 to 1 (inclusive)
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003316 if (isa<ConstantInt>(CallOperandVal))
3317 weight = CW_Constant;
3318 break;
Jack Carter0e149b02013-03-04 21:33:15 +00003319 case 'R':
3320 weight = CW_Memory;
3321 break;
John Thompsone8360b72010-10-29 17:29:13 +00003322 }
3323 return weight;
3324}
3325
Akira Hatanaka7473b472013-08-14 00:21:25 +00003326/// This is a helper function to parse a physical register string and split it
3327/// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag
3328/// that is returned indicates whether parsing was successful. The second flag
3329/// is true if the numeric part exists.
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003330static std::pair<bool, bool> parsePhysicalReg(StringRef C, StringRef &Prefix,
3331 unsigned long long &Reg) {
Akira Hatanaka7473b472013-08-14 00:21:25 +00003332 if (C.front() != '{' || C.back() != '}')
3333 return std::make_pair(false, false);
3334
3335 // Search for the first numeric character.
3336 StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1;
Craig Topper2241dfd2015-11-23 07:19:06 +00003337 I = std::find_if(B, E, isdigit);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003338
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003339 Prefix = StringRef(B, I - B);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003340
3341 // The second flag is set to false if no numeric characters were found.
3342 if (I == E)
3343 return std::make_pair(true, false);
3344
3345 // Parse the numeric characters.
3346 return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg),
3347 true);
3348}
3349
3350std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering::
Craig Topper6dc4a8bc2014-08-30 16:48:02 +00003351parseRegForInlineAsmConstraint(StringRef C, MVT VT) const {
Eric Christopherd9134482014-08-04 21:25:23 +00003352 const TargetRegisterInfo *TRI =
Eric Christopher96e72c62015-01-29 23:27:36 +00003353 Subtarget.getRegisterInfo();
Akira Hatanaka7473b472013-08-14 00:21:25 +00003354 const TargetRegisterClass *RC;
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003355 StringRef Prefix;
Akira Hatanaka7473b472013-08-14 00:21:25 +00003356 unsigned long long Reg;
3357
3358 std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg);
3359
3360 if (!R.first)
Craig Topper062a2ba2014-04-25 05:30:21 +00003361 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003362
3363 if ((Prefix == "hi" || Prefix == "lo")) { // Parse hi/lo.
3364 // No numeric characters follow "hi" or "lo".
3365 if (R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003366 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003367
3368 RC = TRI->getRegClass(Prefix == "hi" ?
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00003369 Mips::HI32RegClassID : Mips::LO32RegClassID);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003370 return std::make_pair(*(RC->begin()), RC);
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003371 } else if (Prefix.startswith("$msa")) {
Daniel Sanders8b59af12013-11-12 12:56:01 +00003372 // Parse $msa(ir|csr|access|save|modify|request|map|unmap)
3373
3374 // No numeric characters follow the name.
3375 if (R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003376 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003377
3378 Reg = StringSwitch<unsigned long long>(Prefix)
3379 .Case("$msair", Mips::MSAIR)
3380 .Case("$msacsr", Mips::MSACSR)
3381 .Case("$msaaccess", Mips::MSAAccess)
3382 .Case("$msasave", Mips::MSASave)
3383 .Case("$msamodify", Mips::MSAModify)
3384 .Case("$msarequest", Mips::MSARequest)
3385 .Case("$msamap", Mips::MSAMap)
3386 .Case("$msaunmap", Mips::MSAUnmap)
3387 .Default(0);
3388
3389 if (!Reg)
Craig Topper062a2ba2014-04-25 05:30:21 +00003390 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003391
3392 RC = TRI->getRegClass(Mips::MSACtrlRegClassID);
3393 return std::make_pair(Reg, RC);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003394 }
3395
3396 if (!R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003397 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003398
3399 if (Prefix == "$f") { // Parse $f0-$f31.
3400 // If the size of FP registers is 64-bit or Reg is an even number, select
3401 // the 64-bit register class. Otherwise, select the 32-bit register class.
3402 if (VT == MVT::Other)
Eric Christopher1c29a652014-07-18 22:55:25 +00003403 VT = (Subtarget.isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32;
Akira Hatanaka7473b472013-08-14 00:21:25 +00003404
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003405 RC = getRegClassFor(VT);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003406
3407 if (RC == &Mips::AFGR64RegClass) {
3408 assert(Reg % 2 == 0);
3409 Reg >>= 1;
3410 }
Daniel Sanders8b59af12013-11-12 12:56:01 +00003411 } else if (Prefix == "$fcc") // Parse $fcc0-$fcc7.
Akira Hatanaka7473b472013-08-14 00:21:25 +00003412 RC = TRI->getRegClass(Mips::FCCRegClassID);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003413 else if (Prefix == "$w") { // Parse $w0-$w31.
3414 RC = getRegClassFor((VT == MVT::Other) ? MVT::v16i8 : VT);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003415 } else { // Parse $0-$31.
3416 assert(Prefix == "$");
3417 RC = getRegClassFor((VT == MVT::Other) ? MVT::i32 : VT);
3418 }
3419
3420 assert(Reg < RC->getNumRegs());
3421 return std::make_pair(*(RC->begin() + Reg), RC);
3422}
3423
Eric Christophereaf77dc2011-06-29 19:33:04 +00003424/// Given a register class constraint, like 'r', if this corresponds directly
3425/// to an LLVM register class, return a register of 0 and the register class
3426/// pointer.
Eric Christopher11e4df72015-02-26 22:38:43 +00003427std::pair<unsigned, const TargetRegisterClass *>
3428MipsTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003429 StringRef Constraint,
Eric Christopher11e4df72015-02-26 22:38:43 +00003430 MVT VT) const {
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003431 if (Constraint.size() == 1) {
3432 switch (Constraint[0]) {
Eric Christopher9519c082011-06-29 19:04:31 +00003433 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
3434 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003435 case 'r':
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003436 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
Eric Christopher1c29a652014-07-18 22:55:25 +00003437 if (Subtarget.inMips16Mode())
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003438 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003439 return std::make_pair(0U, &Mips::GPR32RegClass);
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003440 }
Eric Christopher1c29a652014-07-18 22:55:25 +00003441 if (VT == MVT::i64 && !Subtarget.isGP64bit())
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003442 return std::make_pair(0U, &Mips::GPR32RegClass);
Eric Christopher1c29a652014-07-18 22:55:25 +00003443 if (VT == MVT::i64 && Subtarget.isGP64bit())
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003444 return std::make_pair(0U, &Mips::GPR64RegClass);
Eric Christopher58daf042012-05-07 03:13:22 +00003445 // This will generate an error message
Craig Topper062a2ba2014-04-25 05:30:21 +00003446 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003447 case 'f': // FPU or MSA register
3448 if (VT == MVT::v16i8)
3449 return std::make_pair(0U, &Mips::MSA128BRegClass);
3450 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
3451 return std::make_pair(0U, &Mips::MSA128HRegClass);
3452 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
3453 return std::make_pair(0U, &Mips::MSA128WRegClass);
3454 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
3455 return std::make_pair(0U, &Mips::MSA128DRegClass);
3456 else if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00003457 return std::make_pair(0U, &Mips::FGR32RegClass);
Eric Christopher1c29a652014-07-18 22:55:25 +00003458 else if ((VT == MVT::f64) && (!Subtarget.isSingleFloat())) {
3459 if (Subtarget.isFP64bit())
Craig Topperc7242e02012-04-20 07:30:17 +00003460 return std::make_pair(0U, &Mips::FGR64RegClass);
3461 return std::make_pair(0U, &Mips::AFGR64RegClass);
Akira Hatanakac669d7a2012-01-04 02:45:01 +00003462 }
Eric Christophere3c494d2012-05-07 06:25:10 +00003463 break;
3464 case 'c': // register suitable for indirect jump
3465 if (VT == MVT::i32)
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003466 return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass);
Eric Christophere3c494d2012-05-07 06:25:10 +00003467 assert(VT == MVT::i64 && "Unexpected type.");
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003468 return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass);
Eric Christopher9c492e62012-05-07 06:25:15 +00003469 case 'l': // register suitable for indirect jump
3470 if (VT == MVT::i32)
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00003471 return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass);
3472 return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass);
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003473 case 'x': // register suitable for indirect jump
3474 // Fixme: Not triggering the use of both hi and low
3475 // This will generate an error message
Craig Topper062a2ba2014-04-25 05:30:21 +00003476 return std::make_pair(0U, nullptr);
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003477 }
3478 }
Akira Hatanaka7473b472013-08-14 00:21:25 +00003479
3480 std::pair<unsigned, const TargetRegisterClass *> R;
3481 R = parseRegForInlineAsmConstraint(Constraint, VT);
3482
3483 if (R.second)
3484 return R;
3485
Eric Christopher11e4df72015-02-26 22:38:43 +00003486 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003487}
3488
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003489/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3490/// vector. If it is invalid, don't add anything to Ops.
3491void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3492 std::string &Constraint,
3493 std::vector<SDValue>&Ops,
3494 SelectionDAG &DAG) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003495 SDLoc DL(Op);
Craig Topper062a2ba2014-04-25 05:30:21 +00003496 SDValue Result;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003497
3498 // Only support length 1 constraints for now.
3499 if (Constraint.length() > 1) return;
3500
3501 char ConstraintLetter = Constraint[0];
3502 switch (ConstraintLetter) {
3503 default: break; // This will fall through to the generic implementation
3504 case 'I': // Signed 16 bit constant
3505 // If this fails, the parent routine will give an error
3506 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3507 EVT Type = Op.getValueType();
3508 int64_t Val = C->getSExtValue();
3509 if (isInt<16>(Val)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003510 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003511 break;
3512 }
3513 }
3514 return;
Eric Christopher7201e1b2012-05-07 03:13:42 +00003515 case 'J': // integer zero
3516 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3517 EVT Type = Op.getValueType();
3518 int64_t Val = C->getZExtValue();
3519 if (Val == 0) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003520 Result = DAG.getTargetConstant(0, DL, Type);
Eric Christopher7201e1b2012-05-07 03:13:42 +00003521 break;
3522 }
3523 }
3524 return;
Eric Christopher3ff88a02012-05-07 05:46:29 +00003525 case 'K': // unsigned 16 bit immediate
3526 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3527 EVT Type = Op.getValueType();
3528 uint64_t Val = (uint64_t)C->getZExtValue();
3529 if (isUInt<16>(Val)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003530 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopher3ff88a02012-05-07 05:46:29 +00003531 break;
3532 }
3533 }
3534 return;
Eric Christopher1109b342012-05-07 05:46:37 +00003535 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3536 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3537 EVT Type = Op.getValueType();
3538 int64_t Val = C->getSExtValue();
3539 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003540 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopher1109b342012-05-07 05:46:37 +00003541 break;
3542 }
3543 }
3544 return;
Eric Christophere07aa432012-05-07 05:46:43 +00003545 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3546 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3547 EVT Type = Op.getValueType();
3548 int64_t Val = C->getSExtValue();
3549 if ((Val >= -65535) && (Val <= -1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003550 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christophere07aa432012-05-07 05:46:43 +00003551 break;
3552 }
3553 }
3554 return;
Eric Christopher470578a2012-05-07 05:46:48 +00003555 case 'O': // signed 15 bit immediate
3556 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3557 EVT Type = Op.getValueType();
3558 int64_t Val = C->getSExtValue();
3559 if ((isInt<15>(Val))) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003560 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopher470578a2012-05-07 05:46:48 +00003561 break;
3562 }
3563 }
3564 return;
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003565 case 'P': // immediate in the range of 1 to 65535 (inclusive)
3566 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3567 EVT Type = Op.getValueType();
3568 int64_t Val = C->getSExtValue();
3569 if ((Val <= 65535) && (Val >= 1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003570 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003571 break;
3572 }
3573 }
3574 return;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003575 }
3576
3577 if (Result.getNode()) {
3578 Ops.push_back(Result);
3579 return;
3580 }
3581
3582 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3583}
3584
Mehdi Amini0cdec1e2015-07-09 02:09:40 +00003585bool MipsTargetLowering::isLegalAddressingMode(const DataLayout &DL,
3586 const AddrMode &AM, Type *Ty,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +00003587 unsigned AS) const {
Akira Hatanakaef839192012-11-17 00:25:41 +00003588 // No global is ever allowed as a base.
3589 if (AM.BaseGV)
3590 return false;
3591
3592 switch (AM.Scale) {
3593 case 0: // "r+i" or just "i", depending on HasBaseReg.
3594 break;
3595 case 1:
3596 if (!AM.HasBaseReg) // allow "r+i".
3597 break;
3598 return false; // disallow "r+r" or "r+r+i".
3599 default:
3600 return false;
3601 }
3602
3603 return true;
3604}
3605
3606bool
Dan Gohman2fe6bee2008-10-18 02:06:02 +00003607MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3608 // The Mips target isn't yet aware of offsets.
3609 return false;
3610}
Evan Cheng16993aa2009-10-27 19:56:55 +00003611
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003612EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003613 unsigned SrcAlign,
3614 bool IsMemset, bool ZeroMemset,
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003615 bool MemcpyStrSrc,
3616 MachineFunction &MF) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003617 if (Subtarget.hasMips64())
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003618 return MVT::i64;
3619
3620 return MVT::i32;
3621}
3622
Evan Cheng83896a52009-10-28 01:43:28 +00003623bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
3624 if (VT != MVT::f32 && VT != MVT::f64)
3625 return false;
Bruno Cardoso Lopesb02a9df2011-01-18 19:41:41 +00003626 if (Imm.isNegZero())
3627 return false;
Evan Cheng16993aa2009-10-27 19:56:55 +00003628 return Imm.isZero();
3629}
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003630
3631unsigned MipsTargetLowering::getJumpTableEncoding() const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003632 if (ABI.IsN64())
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003633 return MachineJumpTableInfo::EK_GPRel64BlockAddress;
Jia Liuf54f60f2012-02-28 07:46:26 +00003634
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003635 return TargetLowering::getJumpTableEncoding();
3636}
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003637
Eric Christopher824f42f2015-05-12 01:26:05 +00003638bool MipsTargetLowering::useSoftFloat() const {
3639 return Subtarget.useSoftFloat();
3640}
3641
Daniel Sandersf43e6872014-11-01 18:44:56 +00003642void MipsTargetLowering::copyByValRegs(
3643 SDValue Chain, SDLoc DL, std::vector<SDValue> &OutChains, SelectionDAG &DAG,
3644 const ISD::ArgFlagsTy &Flags, SmallVectorImpl<SDValue> &InVals,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003645 const Argument *FuncArg, unsigned FirstReg, unsigned LastReg,
3646 const CCValAssign &VA, MipsCCState &State) const {
Akira Hatanaka25dad192012-10-27 00:10:18 +00003647 MachineFunction &MF = DAG.getMachineFunction();
3648 MachineFrameInfo *MFI = MF.getFrameInfo();
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003649 unsigned GPRSizeInBytes = Subtarget.getGPRSizeInBytes();
Daniel Sanders23e98772014-11-02 16:09:29 +00003650 unsigned NumRegs = LastReg - FirstReg;
3651 unsigned RegAreaSize = NumRegs * GPRSizeInBytes;
Akira Hatanaka25dad192012-10-27 00:10:18 +00003652 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
3653 int FrameObjOffset;
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003654 ArrayRef<MCPhysReg> ByValArgRegs = ABI.GetByValArgRegs();
Akira Hatanaka25dad192012-10-27 00:10:18 +00003655
3656 if (RegAreaSize)
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003657 FrameObjOffset =
3658 (int)ABI.GetCalleeAllocdArgSizeInBytes(State.getCallingConv()) -
3659 (int)((ByValArgRegs.size() - FirstReg) * GPRSizeInBytes);
Akira Hatanaka25dad192012-10-27 00:10:18 +00003660 else
Daniel Sandersf43e6872014-11-01 18:44:56 +00003661 FrameObjOffset = VA.getLocMemOffset();
Akira Hatanaka25dad192012-10-27 00:10:18 +00003662
3663 // Create frame object.
Mehdi Amini44ede332015-07-09 02:09:04 +00003664 EVT PtrTy = getPointerTy(DAG.getDataLayout());
Akira Hatanaka25dad192012-10-27 00:10:18 +00003665 int FI = MFI->CreateFixedObject(FrameObjSize, FrameObjOffset, true);
3666 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
3667 InVals.push_back(FIN);
3668
Daniel Sanders23e98772014-11-02 16:09:29 +00003669 if (!NumRegs)
Akira Hatanaka25dad192012-10-27 00:10:18 +00003670 return;
3671
3672 // Copy arg registers.
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003673 MVT RegTy = MVT::getIntegerVT(GPRSizeInBytes * 8);
Akira Hatanaka25dad192012-10-27 00:10:18 +00003674 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3675
Daniel Sanders23e98772014-11-02 16:09:29 +00003676 for (unsigned I = 0; I < NumRegs; ++I) {
Daniel Sandersd7eba312014-11-07 12:21:37 +00003677 unsigned ArgReg = ByValArgRegs[FirstReg + I];
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003678 unsigned VReg = addLiveIn(MF, ArgReg, RC);
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003679 unsigned Offset = I * GPRSizeInBytes;
Akira Hatanaka25dad192012-10-27 00:10:18 +00003680 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003681 DAG.getConstant(Offset, DL, PtrTy));
Akira Hatanaka25dad192012-10-27 00:10:18 +00003682 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
3683 StorePtr, MachinePointerInfo(FuncArg, Offset),
3684 false, false, 0);
3685 OutChains.push_back(Store);
3686 }
3687}
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003688
3689// Copy byVal arg to registers and stack.
Daniel Sandersf43e6872014-11-01 18:44:56 +00003690void MipsTargetLowering::passByValArg(
3691 SDValue Chain, SDLoc DL,
3692 std::deque<std::pair<unsigned, SDValue>> &RegsToPass,
3693 SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003694 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg, unsigned FirstReg,
3695 unsigned LastReg, const ISD::ArgFlagsTy &Flags, bool isLittle,
3696 const CCValAssign &VA) const {
Daniel Sandersac272632014-05-23 13:18:02 +00003697 unsigned ByValSizeInBytes = Flags.getByValSize();
3698 unsigned OffsetInBytes = 0; // From beginning of struct
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003699 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
Daniel Sandersac272632014-05-23 13:18:02 +00003700 unsigned Alignment = std::min(Flags.getByValAlign(), RegSizeInBytes);
Mehdi Amini44ede332015-07-09 02:09:04 +00003701 EVT PtrTy = getPointerTy(DAG.getDataLayout()),
3702 RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
Daniel Sanders23e98772014-11-02 16:09:29 +00003703 unsigned NumRegs = LastReg - FirstReg;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003704
Daniel Sanders23e98772014-11-02 16:09:29 +00003705 if (NumRegs) {
Craig Topper862d5d82015-09-28 00:15:34 +00003706 ArrayRef<MCPhysReg> ArgRegs = ABI.GetByValArgRegs();
Daniel Sanders23e98772014-11-02 16:09:29 +00003707 bool LeftoverBytes = (NumRegs * RegSizeInBytes > ByValSizeInBytes);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003708 unsigned I = 0;
3709
3710 // Copy words to registers.
Daniel Sanders23e98772014-11-02 16:09:29 +00003711 for (; I < NumRegs - LeftoverBytes; ++I, OffsetInBytes += RegSizeInBytes) {
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003712 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003713 DAG.getConstant(OffsetInBytes, DL, PtrTy));
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003714 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
3715 MachinePointerInfo(), false, false, false,
3716 Alignment);
3717 MemOpChains.push_back(LoadVal.getValue(1));
Daniel Sanders23e98772014-11-02 16:09:29 +00003718 unsigned ArgReg = ArgRegs[FirstReg + I];
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003719 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
3720 }
3721
3722 // Return if the struct has been fully copied.
Daniel Sandersac272632014-05-23 13:18:02 +00003723 if (ByValSizeInBytes == OffsetInBytes)
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003724 return;
3725
3726 // Copy the remainder of the byval argument with sub-word loads and shifts.
3727 if (LeftoverBytes) {
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003728 SDValue Val;
3729
Daniel Sandersac272632014-05-23 13:18:02 +00003730 for (unsigned LoadSizeInBytes = RegSizeInBytes / 2, TotalBytesLoaded = 0;
3731 OffsetInBytes < ByValSizeInBytes; LoadSizeInBytes /= 2) {
3732 unsigned RemainingSizeInBytes = ByValSizeInBytes - OffsetInBytes;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003733
Daniel Sandersac272632014-05-23 13:18:02 +00003734 if (RemainingSizeInBytes < LoadSizeInBytes)
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003735 continue;
3736
3737 // Load subword.
3738 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003739 DAG.getConstant(OffsetInBytes, DL,
3740 PtrTy));
Daniel Sandersac272632014-05-23 13:18:02 +00003741 SDValue LoadVal = DAG.getExtLoad(
3742 ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr, MachinePointerInfo(),
Louis Gerbarg67474e32014-07-31 21:45:05 +00003743 MVT::getIntegerVT(LoadSizeInBytes * 8), false, false, false,
3744 Alignment);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003745 MemOpChains.push_back(LoadVal.getValue(1));
3746
3747 // Shift the loaded value.
3748 unsigned Shamt;
3749
3750 if (isLittle)
Daniel Sandersac272632014-05-23 13:18:02 +00003751 Shamt = TotalBytesLoaded * 8;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003752 else
Daniel Sandersac272632014-05-23 13:18:02 +00003753 Shamt = (RegSizeInBytes - (TotalBytesLoaded + LoadSizeInBytes)) * 8;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003754
3755 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003756 DAG.getConstant(Shamt, DL, MVT::i32));
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003757
3758 if (Val.getNode())
3759 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
3760 else
3761 Val = Shift;
3762
Daniel Sandersac272632014-05-23 13:18:02 +00003763 OffsetInBytes += LoadSizeInBytes;
3764 TotalBytesLoaded += LoadSizeInBytes;
3765 Alignment = std::min(Alignment, LoadSizeInBytes);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003766 }
3767
Daniel Sanders23e98772014-11-02 16:09:29 +00003768 unsigned ArgReg = ArgRegs[FirstReg + I];
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003769 RegsToPass.push_back(std::make_pair(ArgReg, Val));
3770 return;
3771 }
3772 }
3773
3774 // Copy remainder of byval arg to it with memcpy.
Daniel Sandersac272632014-05-23 13:18:02 +00003775 unsigned MemCpySize = ByValSizeInBytes - OffsetInBytes;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003776 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003777 DAG.getConstant(OffsetInBytes, DL, PtrTy));
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003778 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003779 DAG.getIntPtrConstant(VA.getLocMemOffset(), DL));
3780 Chain = DAG.getMemcpy(Chain, DL, Dst, Src,
3781 DAG.getConstant(MemCpySize, DL, PtrTy),
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003782 Alignment, /*isVolatile=*/false, /*AlwaysInline=*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00003783 /*isTailCall=*/false,
Nick Lewyckyaad475b2014-04-15 07:22:52 +00003784 MachinePointerInfo(), MachinePointerInfo());
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003785 MemOpChains.push_back(Chain);
3786}
Akira Hatanaka2a134022012-10-27 00:21:13 +00003787
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003788void MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003789 SDValue Chain, SDLoc DL,
3790 SelectionDAG &DAG,
Daniel Sanders853c2432014-11-01 18:13:52 +00003791 CCState &State) const {
Craig Topper862d5d82015-09-28 00:15:34 +00003792 ArrayRef<MCPhysReg> ArgRegs = ABI.GetVarArgRegs();
Tim Northover3b6b7ca2015-02-21 02:11:17 +00003793 unsigned Idx = State.getFirstUnallocated(ArgRegs);
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003794 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
3795 MVT RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003796 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3797 MachineFunction &MF = DAG.getMachineFunction();
3798 MachineFrameInfo *MFI = MF.getFrameInfo();
3799 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3800
3801 // Offset of the first variable argument from stack pointer.
3802 int VaArgOffset;
3803
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003804 if (ArgRegs.size() == Idx)
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003805 VaArgOffset =
Daniel Sanders853c2432014-11-01 18:13:52 +00003806 RoundUpToAlignment(State.getNextStackOffset(), RegSizeInBytes);
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003807 else {
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003808 VaArgOffset =
3809 (int)ABI.GetCalleeAllocdArgSizeInBytes(State.getCallingConv()) -
3810 (int)(RegSizeInBytes * (ArgRegs.size() - Idx));
3811 }
Akira Hatanaka2a134022012-10-27 00:21:13 +00003812
3813 // Record the frame index of the first variable argument
3814 // which is a value necessary to VASTART.
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003815 int FI = MFI->CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003816 MipsFI->setVarArgsFrameIndex(FI);
3817
3818 // Copy the integer registers that have not been used for argument passing
3819 // to the argument register save area. For O32, the save area is allocated
3820 // in the caller's stack frame, while for N32/64, it is allocated in the
3821 // callee's stack frame.
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003822 for (unsigned I = Idx; I < ArgRegs.size();
3823 ++I, VaArgOffset += RegSizeInBytes) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003824 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003825 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003826 FI = MFI->CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
Mehdi Amini44ede332015-07-09 02:09:04 +00003827 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Akira Hatanaka2a134022012-10-27 00:21:13 +00003828 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
3829 MachinePointerInfo(), false, false, 0);
Eric Christopher1c29a652014-07-18 22:55:25 +00003830 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(
3831 (Value *)nullptr);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003832 OutChains.push_back(Store);
3833 }
3834}
Daniel Sanders23e98772014-11-02 16:09:29 +00003835
3836void MipsTargetLowering::HandleByVal(CCState *State, unsigned &Size,
3837 unsigned Align) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003838 const TargetFrameLowering *TFL = Subtarget.getFrameLowering();
Daniel Sanders23e98772014-11-02 16:09:29 +00003839
3840 assert(Size && "Byval argument's size shouldn't be 0.");
3841
3842 Align = std::min(Align, TFL->getStackAlignment());
3843
3844 unsigned FirstReg = 0;
3845 unsigned NumRegs = 0;
3846
3847 if (State->getCallingConv() != CallingConv::Fast) {
3848 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
Craig Topper862d5d82015-09-28 00:15:34 +00003849 ArrayRef<MCPhysReg> IntArgRegs = ABI.GetByValArgRegs();
Daniel Sanders23e98772014-11-02 16:09:29 +00003850 // FIXME: The O32 case actually describes no shadow registers.
3851 const MCPhysReg *ShadowRegs =
Eric Christopher96e72c62015-01-29 23:27:36 +00003852 ABI.IsO32() ? IntArgRegs.data() : Mips64DPRegs;
Daniel Sanders23e98772014-11-02 16:09:29 +00003853
3854 // We used to check the size as well but we can't do that anymore since
3855 // CCState::HandleByVal() rounds up the size after calling this function.
3856 assert(!(Align % RegSizeInBytes) &&
3857 "Byval argument's alignment should be a multiple of"
3858 "RegSizeInBytes.");
3859
Tim Northover3b6b7ca2015-02-21 02:11:17 +00003860 FirstReg = State->getFirstUnallocated(IntArgRegs);
Daniel Sanders23e98772014-11-02 16:09:29 +00003861
3862 // If Align > RegSizeInBytes, the first arg register must be even.
3863 // FIXME: This condition happens to do the right thing but it's not the
3864 // right way to test it. We want to check that the stack frame offset
3865 // of the register is aligned.
3866 if ((Align > RegSizeInBytes) && (FirstReg % 2)) {
3867 State->AllocateReg(IntArgRegs[FirstReg], ShadowRegs[FirstReg]);
3868 ++FirstReg;
3869 }
3870
3871 // Mark the registers allocated.
3872 Size = RoundUpToAlignment(Size, RegSizeInBytes);
3873 for (unsigned I = FirstReg; Size > 0 && (I < IntArgRegs.size());
3874 Size -= RegSizeInBytes, ++I, ++NumRegs)
3875 State->AllocateReg(IntArgRegs[I], ShadowRegs[I]);
3876 }
3877
3878 State->addInRegsParamInfo(FirstReg, FirstReg + NumRegs);
3879}
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00003880
3881MachineBasicBlock *
3882MipsTargetLowering::emitPseudoSELECT(MachineInstr *MI, MachineBasicBlock *BB,
3883 bool isFPCmp, unsigned Opc) const {
3884 assert(!(Subtarget.hasMips4() || Subtarget.hasMips32()) &&
3885 "Subtarget already supports SELECT nodes with the use of"
3886 "conditional-move instructions.");
3887
3888 const TargetInstrInfo *TII =
Eric Christopher96e72c62015-01-29 23:27:36 +00003889 Subtarget.getInstrInfo();
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00003890 DebugLoc DL = MI->getDebugLoc();
3891
3892 // To "insert" a SELECT instruction, we actually have to insert the
3893 // diamond control-flow pattern. The incoming instruction knows the
3894 // destination vreg to set, the condition code register to branch on, the
3895 // true/false values to select between, and a branch opcode to use.
3896 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +00003897 MachineFunction::iterator It = ++BB->getIterator();
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00003898
3899 // thisMBB:
3900 // ...
3901 // TrueVal = ...
3902 // setcc r1, r2, r3
3903 // bNE r1, r0, copy1MBB
3904 // fallthrough --> copy0MBB
3905 MachineBasicBlock *thisMBB = BB;
3906 MachineFunction *F = BB->getParent();
3907 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
3908 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
3909 F->insert(It, copy0MBB);
3910 F->insert(It, sinkMBB);
3911
3912 // Transfer the remainder of BB and its successor edges to sinkMBB.
3913 sinkMBB->splice(sinkMBB->begin(), BB,
3914 std::next(MachineBasicBlock::iterator(MI)), BB->end());
3915 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
3916
3917 // Next, add the true and fallthrough blocks as its successors.
3918 BB->addSuccessor(copy0MBB);
3919 BB->addSuccessor(sinkMBB);
3920
3921 if (isFPCmp) {
3922 // bc1[tf] cc, sinkMBB
3923 BuildMI(BB, DL, TII->get(Opc))
3924 .addReg(MI->getOperand(1).getReg())
3925 .addMBB(sinkMBB);
3926 } else {
3927 // bne rs, $0, sinkMBB
3928 BuildMI(BB, DL, TII->get(Opc))
3929 .addReg(MI->getOperand(1).getReg())
3930 .addReg(Mips::ZERO)
3931 .addMBB(sinkMBB);
3932 }
3933
3934 // copy0MBB:
3935 // %FalseValue = ...
3936 // # fallthrough to sinkMBB
3937 BB = copy0MBB;
3938
3939 // Update machine-CFG edges
3940 BB->addSuccessor(sinkMBB);
3941
3942 // sinkMBB:
3943 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
3944 // ...
3945 BB = sinkMBB;
3946
3947 BuildMI(*BB, BB->begin(), DL,
3948 TII->get(Mips::PHI), MI->getOperand(0).getReg())
3949 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
3950 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB);
3951
3952 MI->eraseFromParent(); // The pseudo instruction is gone now.
3953
3954 return BB;
3955}
Daniel Sanders1440bb22015-01-09 17:21:30 +00003956
3957// FIXME? Maybe this could be a TableGen attribute on some registers and
3958// this table could be generated automatically from RegInfo.
Pat Gavlina717f252015-07-09 17:40:29 +00003959unsigned MipsTargetLowering::getRegisterByName(const char* RegName, EVT VT,
3960 SelectionDAG &DAG) const {
Daniel Sanders1440bb22015-01-09 17:21:30 +00003961 // Named registers is expected to be fairly rare. For now, just support $28
3962 // since the linux kernel uses it.
3963 if (Subtarget.isGP64bit()) {
3964 unsigned Reg = StringSwitch<unsigned>(RegName)
3965 .Case("$28", Mips::GP_64)
3966 .Default(0);
3967 if (Reg)
3968 return Reg;
3969 } else {
3970 unsigned Reg = StringSwitch<unsigned>(RegName)
3971 .Case("$28", Mips::GP)
3972 .Default(0);
3973 if (Reg)
3974 return Reg;
3975 }
3976 report_fatal_error("Invalid register name global variable");
3977}