blob: 371a4fe2f61b60a71bbd235217386ddf2498fceb [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";
Simon Dardis8efa9792016-09-09 09:22:52 +0000147 case MipsISD::PseudoReadFCC: return "MipsISD::PseudoReadFCC";
Akira Hatanakab9ebf8d2012-06-02 00:03:12 +0000148 case MipsISD::LWL: return "MipsISD::LWL";
149 case MipsISD::LWR: return "MipsISD::LWR";
150 case MipsISD::SWL: return "MipsISD::SWL";
151 case MipsISD::SWR: return "MipsISD::SWR";
152 case MipsISD::LDL: return "MipsISD::LDL";
153 case MipsISD::LDR: return "MipsISD::LDR";
154 case MipsISD::SDL: return "MipsISD::SDL";
155 case MipsISD::SDR: return "MipsISD::SDR";
Akira Hatanaka233ac532012-09-21 23:52:47 +0000156 case MipsISD::EXTP: return "MipsISD::EXTP";
157 case MipsISD::EXTPDP: return "MipsISD::EXTPDP";
158 case MipsISD::EXTR_S_H: return "MipsISD::EXTR_S_H";
159 case MipsISD::EXTR_W: return "MipsISD::EXTR_W";
160 case MipsISD::EXTR_R_W: return "MipsISD::EXTR_R_W";
161 case MipsISD::EXTR_RS_W: return "MipsISD::EXTR_RS_W";
162 case MipsISD::SHILO: return "MipsISD::SHILO";
163 case MipsISD::MTHLIP: return "MipsISD::MTHLIP";
Matthias Braund04893f2015-05-07 21:33:59 +0000164 case MipsISD::MULSAQ_S_W_PH: return "MipsISD::MULSAQ_S_W_PH";
165 case MipsISD::MAQ_S_W_PHL: return "MipsISD::MAQ_S_W_PHL";
166 case MipsISD::MAQ_S_W_PHR: return "MipsISD::MAQ_S_W_PHR";
167 case MipsISD::MAQ_SA_W_PHL: return "MipsISD::MAQ_SA_W_PHL";
168 case MipsISD::MAQ_SA_W_PHR: return "MipsISD::MAQ_SA_W_PHR";
169 case MipsISD::DPAU_H_QBL: return "MipsISD::DPAU_H_QBL";
170 case MipsISD::DPAU_H_QBR: return "MipsISD::DPAU_H_QBR";
171 case MipsISD::DPSU_H_QBL: return "MipsISD::DPSU_H_QBL";
172 case MipsISD::DPSU_H_QBR: return "MipsISD::DPSU_H_QBR";
173 case MipsISD::DPAQ_S_W_PH: return "MipsISD::DPAQ_S_W_PH";
174 case MipsISD::DPSQ_S_W_PH: return "MipsISD::DPSQ_S_W_PH";
175 case MipsISD::DPAQ_SA_L_W: return "MipsISD::DPAQ_SA_L_W";
176 case MipsISD::DPSQ_SA_L_W: return "MipsISD::DPSQ_SA_L_W";
177 case MipsISD::DPA_W_PH: return "MipsISD::DPA_W_PH";
178 case MipsISD::DPS_W_PH: return "MipsISD::DPS_W_PH";
179 case MipsISD::DPAQX_S_W_PH: return "MipsISD::DPAQX_S_W_PH";
180 case MipsISD::DPAQX_SA_W_PH: return "MipsISD::DPAQX_SA_W_PH";
181 case MipsISD::DPAX_W_PH: return "MipsISD::DPAX_W_PH";
182 case MipsISD::DPSX_W_PH: return "MipsISD::DPSX_W_PH";
183 case MipsISD::DPSQX_S_W_PH: return "MipsISD::DPSQX_S_W_PH";
184 case MipsISD::DPSQX_SA_W_PH: return "MipsISD::DPSQX_SA_W_PH";
185 case MipsISD::MULSA_W_PH: return "MipsISD::MULSA_W_PH";
Akira Hatanaka233ac532012-09-21 23:52:47 +0000186 case MipsISD::MULT: return "MipsISD::MULT";
187 case MipsISD::MULTU: return "MipsISD::MULTU";
Jia Liu434874d2013-03-04 01:06:54 +0000188 case MipsISD::MADD_DSP: return "MipsISD::MADD_DSP";
Akira Hatanaka233ac532012-09-21 23:52:47 +0000189 case MipsISD::MADDU_DSP: return "MipsISD::MADDU_DSP";
190 case MipsISD::MSUB_DSP: return "MipsISD::MSUB_DSP";
191 case MipsISD::MSUBU_DSP: return "MipsISD::MSUBU_DSP";
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000192 case MipsISD::SHLL_DSP: return "MipsISD::SHLL_DSP";
193 case MipsISD::SHRA_DSP: return "MipsISD::SHRA_DSP";
194 case MipsISD::SHRL_DSP: return "MipsISD::SHRL_DSP";
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000195 case MipsISD::SETCC_DSP: return "MipsISD::SETCC_DSP";
196 case MipsISD::SELECT_CC_DSP: return "MipsISD::SELECT_CC_DSP";
Daniel Sandersce09d072013-08-28 12:14:50 +0000197 case MipsISD::VALL_ZERO: return "MipsISD::VALL_ZERO";
198 case MipsISD::VANY_ZERO: return "MipsISD::VANY_ZERO";
199 case MipsISD::VALL_NONZERO: return "MipsISD::VALL_NONZERO";
200 case MipsISD::VANY_NONZERO: return "MipsISD::VANY_NONZERO";
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000201 case MipsISD::VCEQ: return "MipsISD::VCEQ";
202 case MipsISD::VCLE_S: return "MipsISD::VCLE_S";
203 case MipsISD::VCLE_U: return "MipsISD::VCLE_U";
204 case MipsISD::VCLT_S: return "MipsISD::VCLT_S";
205 case MipsISD::VCLT_U: return "MipsISD::VCLT_U";
Daniel Sanders3ce56622013-09-24 12:18:31 +0000206 case MipsISD::VSMAX: return "MipsISD::VSMAX";
207 case MipsISD::VSMIN: return "MipsISD::VSMIN";
208 case MipsISD::VUMAX: return "MipsISD::VUMAX";
209 case MipsISD::VUMIN: return "MipsISD::VUMIN";
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000210 case MipsISD::VEXTRACT_SEXT_ELT: return "MipsISD::VEXTRACT_SEXT_ELT";
211 case MipsISD::VEXTRACT_ZEXT_ELT: return "MipsISD::VEXTRACT_ZEXT_ELT";
Daniel Sandersf7456c72013-09-23 13:22:24 +0000212 case MipsISD::VNOR: return "MipsISD::VNOR";
Daniel Sanderse5087042013-09-24 14:02:15 +0000213 case MipsISD::VSHF: return "MipsISD::VSHF";
Daniel Sanders26307182013-09-24 14:20:00 +0000214 case MipsISD::SHF: return "MipsISD::SHF";
Daniel Sanders2ed228b2013-09-24 14:36:12 +0000215 case MipsISD::ILVEV: return "MipsISD::ILVEV";
216 case MipsISD::ILVOD: return "MipsISD::ILVOD";
217 case MipsISD::ILVL: return "MipsISD::ILVL";
218 case MipsISD::ILVR: return "MipsISD::ILVR";
Daniel Sandersfae5f2a2013-09-24 14:53:25 +0000219 case MipsISD::PCKEV: return "MipsISD::PCKEV";
220 case MipsISD::PCKOD: return "MipsISD::PCKOD";
Daniel Sandersb50ccf82014-04-01 10:35:28 +0000221 case MipsISD::INSVE: return "MipsISD::INSVE";
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000222 }
Matthias Braund04893f2015-05-07 21:33:59 +0000223 return nullptr;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000224}
225
Eric Christopherb1526602014-09-19 23:30:42 +0000226MipsTargetLowering::MipsTargetLowering(const MipsTargetMachine &TM,
Eric Christopher8924d272014-07-18 23:25:04 +0000227 const MipsSubtarget &STI)
Eric Christopher96e72c62015-01-29 23:27:36 +0000228 : TargetLowering(TM), Subtarget(STI), ABI(TM.getABI()) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000229 // Mips does not have i1 type, so use i32 for
Wesley Peck527da1b2010-11-23 03:31:01 +0000230 // setcc operations results (slt, sgt, ...).
Duncan Sands8d6e2e12008-11-23 15:47:28 +0000231 setBooleanContents(ZeroOrOneBooleanContent);
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000232 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Daniel Sanderscbd44c52014-07-10 10:18:12 +0000233 // The cmp.cond.fmt instruction in MIPS32r6/MIPS64r6 uses 0 and -1 like MSA
234 // does. Integer booleans still use 0 and 1.
Eric Christopher1c29a652014-07-18 22:55:25 +0000235 if (Subtarget.hasMips32r6())
Daniel Sanderscbd44c52014-07-10 10:18:12 +0000236 setBooleanContents(ZeroOrOneBooleanContent,
237 ZeroOrNegativeOneBooleanContent);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000238
Wesley Peck527da1b2010-11-23 03:31:01 +0000239 // Load extented operations for i1 types must be promoted
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000240 for (MVT VT : MVT::integer_valuetypes()) {
241 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
242 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
243 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
244 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000245
Pirama Arumuga Nainar34056de2015-04-20 20:15:36 +0000246 // MIPS doesn't have extending float->double load/store. Set LoadExtAction
247 // for f32, f16
248 for (MVT VT : MVT::fp_valuetypes()) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000249 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
Pirama Arumuga Nainar34056de2015-04-20 20:15:36 +0000250 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f16, Expand);
251 }
252
253 // Set LoadExtAction for f16 vectors to Expand
254 for (MVT VT : MVT::fp_vector_valuetypes()) {
255 MVT F16VT = MVT::getVectorVT(MVT::f16, VT.getVectorNumElements());
256 if (F16VT.isValid())
257 setLoadExtAction(ISD::EXTLOAD, VT, F16VT, Expand);
258 }
259
260 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
261 setTruncStoreAction(MVT::f64, MVT::f16, Expand);
262
Owen Anderson9f944592009-08-11 20:47:22 +0000263 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman39d6faa2009-07-17 02:28:12 +0000264
Wesley Peck527da1b2010-11-23 03:31:01 +0000265 // Used by legalize types to correctly generate the setcc result.
266 // Without this, every float setcc comes with a AND/OR with the result,
267 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +0000268 // which is used implicitly by brcond and select operations.
Vasileios Kalintiris36901dd2016-03-01 20:25:43 +0000269 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +0000270
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000271 // Mips Custom Operations
Akira Hatanaka0f693a82013-03-06 21:32:03 +0000272 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000273 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000274 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000275 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
276 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
277 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000278 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
279 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000280 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000281
Eric Christopher1c29a652014-07-18 22:55:25 +0000282 if (Subtarget.isGP64bit()) {
Akira Hatanakada00aa82012-03-10 00:03:50 +0000283 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
284 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
285 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
286 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
287 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
Akira Hatanaka019e5922012-06-02 00:04:42 +0000288 setOperationAction(ISD::LOAD, MVT::i64, Custom);
289 setOperationAction(ISD::STORE, MVT::i64, Custom);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000290 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +0000291 setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom);
292 setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom);
293 setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000294 }
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +0000295
Eric Christopher1c29a652014-07-18 22:55:25 +0000296 if (!Subtarget.isGP64bit()) {
Akira Hatanaka0a8ab712012-05-09 00:55:21 +0000297 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
298 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
299 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
300 }
301
Hal Finkel5081ac22016-09-01 10:28:47 +0000302 setOperationAction(ISD::EH_DWARF_CFA, MVT::i32, Custom);
Eric Christopher1c29a652014-07-18 22:55:25 +0000303 if (Subtarget.isGP64bit())
Hal Finkel5081ac22016-09-01 10:28:47 +0000304 setOperationAction(ISD::EH_DWARF_CFA, MVT::i64, Custom);
Akira Hatanaka28e02ec2012-11-07 19:10:58 +0000305
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000306 setOperationAction(ISD::SDIV, MVT::i32, Expand);
307 setOperationAction(ISD::SREM, MVT::i32, Expand);
308 setOperationAction(ISD::UDIV, MVT::i32, Expand);
309 setOperationAction(ISD::UREM, MVT::i32, Expand);
Akira Hatanakab1538f92011-10-03 21:06:13 +0000310 setOperationAction(ISD::SDIV, MVT::i64, Expand);
311 setOperationAction(ISD::SREM, MVT::i64, Expand);
312 setOperationAction(ISD::UDIV, MVT::i64, Expand);
313 setOperationAction(ISD::UREM, MVT::i64, Expand);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000314
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000315 // Operations not directly supported by Mips.
Tom Stellardb1588fc2013-03-08 15:36:57 +0000316 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
317 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
318 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
319 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
Tom Stellard3787b122014-06-10 16:01:29 +0000320 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
321 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
Matt Arsenaulta0e5cd52016-01-11 16:44:48 +0000322 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
323 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000324 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Akira Hatanaka79aed152011-12-20 23:40:56 +0000325 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000326 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Akira Hatanaka79aed152011-12-20 23:40:56 +0000327 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000328 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Eric Christopher1c29a652014-07-18 22:55:25 +0000329 if (Subtarget.hasCnMips()) {
Kai Nacke93fe5e82014-03-20 11:51:58 +0000330 setOperationAction(ISD::CTPOP, MVT::i32, Legal);
331 setOperationAction(ISD::CTPOP, MVT::i64, Legal);
332 } else {
333 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
334 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
335 }
Owen Anderson9f944592009-08-11 20:47:22 +0000336 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
Akira Hatanaka410ce9c2011-12-21 00:14:05 +0000337 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000338 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Akira Hatanaka7ba8a8d2011-09-30 18:51:46 +0000339 setOperationAction(ISD::ROTL, MVT::i64, Expand);
Akira Hatanaka33a25af2012-07-31 20:54:48 +0000340 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
341 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Bruno Cardoso Lopesd47180e2010-12-09 17:32:30 +0000342
Eric Christopher1c29a652014-07-18 22:55:25 +0000343 if (!Subtarget.hasMips32r2())
Bruno Cardoso Lopesd47180e2010-12-09 17:32:30 +0000344 setOperationAction(ISD::ROTR, MVT::i32, Expand);
345
Eric Christopher1c29a652014-07-18 22:55:25 +0000346 if (!Subtarget.hasMips64r2())
Akira Hatanaka7ba8a8d2011-09-30 18:51:46 +0000347 setOperationAction(ISD::ROTR, MVT::i64, Expand);
348
Owen Anderson9f944592009-08-11 20:47:22 +0000349 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes22b69db2011-03-04 18:54:14 +0000350 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000351 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes22b69db2011-03-04 18:54:14 +0000352 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000353 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
354 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000355 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
356 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanakadfb8cda2011-05-23 22:23:58 +0000357 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000358 setOperationAction(ISD::FLOG, MVT::f32, Expand);
359 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
360 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
361 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000362 setOperationAction(ISD::FMA, MVT::f32, Expand);
363 setOperationAction(ISD::FMA, MVT::f64, Expand);
Akira Hatanaka0603ad82012-03-29 18:43:11 +0000364 setOperationAction(ISD::FREM, MVT::f32, Expand);
365 setOperationAction(ISD::FREM, MVT::f64, Expand);
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000366
Pirama Arumuga Nainar34056de2015-04-20 20:15:36 +0000367 // Lower f16 conversion operations into library calls
368 setOperationAction(ISD::FP16_TO_FP, MVT::f32, Expand);
369 setOperationAction(ISD::FP_TO_FP16, MVT::f32, Expand);
370 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand);
371 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Expand);
372
Akira Hatanakac0b02062013-01-30 00:26:49 +0000373 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
374
Daniel Sanders2b553d42014-08-01 09:17:39 +0000375 setOperationAction(ISD::VASTART, MVT::Other, Custom);
376 setOperationAction(ISD::VAARG, MVT::Other, Custom);
Bruno Cardoso Lopes048ffab2011-03-09 19:22:22 +0000377 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
378 setOperationAction(ISD::VAEND, MVT::Other, Expand);
379
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000380 // Use the default for now
Owen Anderson9f944592009-08-11 20:47:22 +0000381 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
382 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman26a48482011-07-27 22:21:52 +0000383
Vasileios Kalintirisb04672c2015-11-06 12:07:20 +0000384 if (!Subtarget.isGP64bit()) {
385 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
386 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
387 }
Eli Friedman7dfa7912011-08-29 18:23:02 +0000388
Eli Friedman30a49e92011-08-03 21:06:02 +0000389
Eric Christopher1c29a652014-07-18 22:55:25 +0000390 if (!Subtarget.hasMips32r2()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000391 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
392 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000393 }
394
Daniel Sanders070fd1c2014-05-12 12:41:59 +0000395 // MIPS16 lacks MIPS32's clz and clo instructions.
Eric Christopher1c29a652014-07-18 22:55:25 +0000396 if (!Subtarget.hasMips32() || Subtarget.inMips16Mode())
Owen Anderson9f944592009-08-11 20:47:22 +0000397 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Eric Christopher1c29a652014-07-18 22:55:25 +0000398 if (!Subtarget.hasMips64())
Akira Hatanaka1d8efab2011-12-21 00:20:27 +0000399 setOperationAction(ISD::CTLZ, MVT::i64, Expand);
Bruno Cardoso Lopes93da7e62008-08-08 06:16:31 +0000400
Eric Christopher1c29a652014-07-18 22:55:25 +0000401 if (!Subtarget.hasMips32r2())
Owen Anderson9f944592009-08-11 20:47:22 +0000402 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Eric Christopher1c29a652014-07-18 22:55:25 +0000403 if (!Subtarget.hasMips64r2())
Akira Hatanaka4706ac92011-12-20 23:56:43 +0000404 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000405
Eric Christopher1c29a652014-07-18 22:55:25 +0000406 if (Subtarget.isGP64bit()) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000407 setLoadExtAction(ISD::SEXTLOAD, MVT::i64, MVT::i32, Custom);
408 setLoadExtAction(ISD::ZEXTLOAD, MVT::i64, MVT::i32, Custom);
409 setLoadExtAction(ISD::EXTLOAD, MVT::i64, MVT::i32, Custom);
Akira Hatanaka019e5922012-06-02 00:04:42 +0000410 setTruncStoreAction(MVT::i64, MVT::i32, Custom);
411 }
412
Akira Hatanakaa3d9ab92013-07-26 20:58:55 +0000413 setOperationAction(ISD::TRAP, MVT::Other, Legal);
414
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000415 setTargetDAGCombine(ISD::SDIVREM);
416 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka5e152182012-03-08 03:26:37 +0000417 setTargetDAGCombine(ISD::SELECT);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000418 setTargetDAGCombine(ISD::AND);
419 setTargetDAGCombine(ISD::OR);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000420 setTargetDAGCombine(ISD::ADD);
Vasileios Kalintiris3751d412016-04-13 15:07:45 +0000421 setTargetDAGCombine(ISD::AssertZext);
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000422
Vasileios Kalintiris1ed49fd2016-09-07 10:01:18 +0000423 if (ABI.IsO32()) {
424 // These libcalls are not available in 32-bit.
425 setLibcallName(RTLIB::SHL_I128, nullptr);
426 setLibcallName(RTLIB::SRL_I128, nullptr);
427 setLibcallName(RTLIB::SRA_I128, nullptr);
428 }
429
Eric Christopher1c29a652014-07-18 22:55:25 +0000430 setMinFunctionAlignment(Subtarget.isGP64bit() ? 3 : 2);
Eli Friedman2518f832011-05-06 20:34:06 +0000431
Daniel Sanders2b553d42014-08-01 09:17:39 +0000432 // The arguments on the stack are defined in terms of 4-byte slots on O32
433 // and 8-byte slots on N32/N64.
Eric Christopher96e72c62015-01-29 23:27:36 +0000434 setMinStackArgumentAlignment((ABI.IsN32() || ABI.IsN64()) ? 8 : 4);
Daniel Sanders2b553d42014-08-01 09:17:39 +0000435
Eric Christopher96e72c62015-01-29 23:27:36 +0000436 setStackPointerRegisterToSaveRestore(ABI.IsN64() ? Mips::SP_64 : Mips::SP);
Akira Hatanakaaa560002011-05-26 18:59:03 +0000437
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000438 MaxStoresPerMemcpy = 16;
Zoran Jovanovicff9d5f32013-12-19 16:12:56 +0000439
Eric Christopher1c29a652014-07-18 22:55:25 +0000440 isMicroMips = Subtarget.inMicroMipsMode();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000441}
442
Eric Christopherb1526602014-09-19 23:30:42 +0000443const MipsTargetLowering *MipsTargetLowering::create(const MipsTargetMachine &TM,
Eric Christopher8924d272014-07-18 23:25:04 +0000444 const MipsSubtarget &STI) {
445 if (STI.inMips16Mode())
446 return llvm::createMips16TargetLowering(TM, STI);
Jia Liuf54f60f2012-02-28 07:46:26 +0000447
Eric Christopher8924d272014-07-18 23:25:04 +0000448 return llvm::createMipsSETargetLowering(TM, STI);
Akira Hatanaka2fcc1cf2011-08-12 21:30:06 +0000449}
450
Reed Kotler720c5ca2014-04-17 22:15:34 +0000451// Create a fast isel object.
452FastISel *
453MipsTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
454 const TargetLibraryInfo *libInfo) const {
Vasileios Kalintiris2041b1d2015-07-30 12:39:33 +0000455 if (!funcInfo.MF->getTarget().Options.EnableFastISel)
Reed Kotler720c5ca2014-04-17 22:15:34 +0000456 return TargetLowering::createFastISel(funcInfo, libInfo);
457 return Mips::createFastISel(funcInfo, libInfo);
458}
459
Mehdi Amini44ede332015-07-09 02:09:04 +0000460EVT MipsTargetLowering::getSetCCResultType(const DataLayout &, LLVMContext &,
461 EVT VT) const {
Vasileios Kalintiris36901dd2016-03-01 20:25:43 +0000462 if (!VT.isVector())
463 return MVT::i32;
464 return VT.changeVectorElementTypeToInteger();
Scott Michela6729e82008-03-10 15:42:14 +0000465}
466
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000467static SDValue performDivRemCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000468 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000469 const MipsSubtarget &Subtarget) {
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000470 if (DCI.isBeforeLegalizeOps())
471 return SDValue();
472
Akira Hatanakab1538f92011-10-03 21:06:13 +0000473 EVT Ty = N->getValueType(0);
Akira Hatanaka8002a3f2013-08-14 00:47:08 +0000474 unsigned LO = (Ty == MVT::i32) ? Mips::LO0 : Mips::LO0_64;
475 unsigned HI = (Ty == MVT::i32) ? Mips::HI0 : Mips::HI0_64;
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000476 unsigned Opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem16 :
477 MipsISD::DivRemU16;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000478 SDLoc DL(N);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000479
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000480 SDValue DivRem = DAG.getNode(Opc, DL, MVT::Glue,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000481 N->getOperand(0), N->getOperand(1));
482 SDValue InChain = DAG.getEntryNode();
483 SDValue InGlue = DivRem;
484
485 // insert MFLO
486 if (N->hasAnyUseOfValue(0)) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000487 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, DL, LO, Ty,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000488 InGlue);
489 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
490 InChain = CopyFromLo.getValue(1);
491 InGlue = CopyFromLo.getValue(2);
492 }
493
494 // insert MFHI
495 if (N->hasAnyUseOfValue(1)) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000496 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, DL,
Akira Hatanakab1538f92011-10-03 21:06:13 +0000497 HI, Ty, InGlue);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000498 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
499 }
500
501 return SDValue();
502}
503
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000504static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000505 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000506 const MipsSubtarget &Subtarget) {
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000507 if (DCI.isBeforeLegalizeOps())
508 return SDValue();
509
510 SDValue SetCC = N->getOperand(0);
511
512 if ((SetCC.getOpcode() != ISD::SETCC) ||
513 !SetCC.getOperand(0).getValueType().isInteger())
514 return SDValue();
515
516 SDValue False = N->getOperand(2);
517 EVT FalseTy = False.getValueType();
518
519 if (!FalseTy.isInteger())
520 return SDValue();
521
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000522 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(False);
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000523
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000524 // If the RHS (False) is 0, we swap the order of the operands
525 // of ISD::SELECT (obviously also inverting the condition) so that we can
526 // take advantage of conditional moves using the $0 register.
527 // Example:
528 // return (a != 0) ? x : 0;
529 // load $reg, x
530 // movz $reg, $0, a
531 if (!FalseC)
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000532 return SDValue();
533
Andrew Trickef9de2a2013-05-25 02:42:55 +0000534 const SDLoc DL(N);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000535
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000536 if (!FalseC->getZExtValue()) {
537 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
538 SDValue True = N->getOperand(1);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000539
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000540 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
541 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
542
543 return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True);
544 }
545
Matheus Almeidaa6beac12013-12-05 12:07:05 +0000546 // If both operands are integer constants there's a possibility that we
547 // can do some interesting optimizations.
548 SDValue True = N->getOperand(1);
549 ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(True);
550
551 if (!TrueC || !True.getValueType().isInteger())
552 return SDValue();
553
Vasileios Kalintiris36901dd2016-03-01 20:25:43 +0000554 // We'll also ignore MVT::i64 operands as this optimizations proves
555 // to be ineffective because of the required sign extensions as the result
556 // of a SETCC operator is always MVT::i32 for non-vector types.
557 if (True.getValueType() == MVT::i64)
558 return SDValue();
559
Matheus Almeidaa6beac12013-12-05 12:07:05 +0000560 int64_t Diff = TrueC->getSExtValue() - FalseC->getSExtValue();
561
562 // 1) (a < x) ? y : y-1
563 // slti $reg1, a, x
564 // addiu $reg2, $reg1, y-1
Vasileios Kalintiris36901dd2016-03-01 20:25:43 +0000565 if (Diff == 1)
566 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, False);
Matheus Almeidaa6beac12013-12-05 12:07:05 +0000567
568 // 2) (a < x) ? y-1 : y
569 // slti $reg1, a, x
570 // xor $reg1, $reg1, 1
571 // addiu $reg2, $reg1, y-1
572 if (Diff == -1) {
573 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
574 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
575 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
576 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, True);
577 }
578
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000579 // Couldn't optimize.
580 return SDValue();
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000581}
582
Vasileios Kalintirise741eb22015-03-02 12:47:32 +0000583static SDValue performCMovFPCombine(SDNode *N, SelectionDAG &DAG,
584 TargetLowering::DAGCombinerInfo &DCI,
585 const MipsSubtarget &Subtarget) {
586 if (DCI.isBeforeLegalizeOps())
587 return SDValue();
588
589 SDValue ValueIfTrue = N->getOperand(0), ValueIfFalse = N->getOperand(2);
590
591 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(ValueIfFalse);
592 if (!FalseC || FalseC->getZExtValue())
593 return SDValue();
594
595 // Since RHS (False) is 0, we swap the order of the True/False operands
596 // (obviously also inverting the condition) so that we can
597 // take advantage of conditional moves using the $0 register.
598 // Example:
599 // return (a != 0) ? x : 0;
600 // load $reg, x
601 // movz $reg, $0, a
602 unsigned Opc = (N->getOpcode() == MipsISD::CMovFP_T) ? MipsISD::CMovFP_F :
603 MipsISD::CMovFP_T;
604
605 SDValue FCC = N->getOperand(1), Glue = N->getOperand(3);
Vasileios Kalintiris2ef28882015-03-04 12:10:18 +0000606 return DAG.getNode(Opc, SDLoc(N), ValueIfFalse.getValueType(),
607 ValueIfFalse, FCC, ValueIfTrue, Glue);
Vasileios Kalintirise741eb22015-03-02 12:47:32 +0000608}
609
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000610static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000611 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000612 const MipsSubtarget &Subtarget) {
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000613 // Pattern match EXT.
614 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
615 // => ext $dst, $src, size, pos
Eric Christopher1c29a652014-07-18 22:55:25 +0000616 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasExtractInsert())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000617 return SDValue();
618
619 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000620 unsigned ShiftRightOpc = ShiftRight.getOpcode();
621
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000622 // Op's first operand must be a shift right.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000623 if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL)
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000624 return SDValue();
625
626 // The second operand of the shift must be an immediate.
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000627 ConstantSDNode *CN;
628 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
629 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000630
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000631 uint64_t Pos = CN->getZExtValue();
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000632 uint64_t SMPos, SMSize;
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000633
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000634 // Op's second operand must be a shifted mask.
635 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000636 !isShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000637 return SDValue();
638
639 // Return if the shifted mask does not start at bit 0 or the sum of its size
640 // and Pos exceeds the word's size.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000641 EVT ValTy = N->getValueType(0);
642 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000643 return SDValue();
644
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000645 SDLoc DL(N);
646 return DAG.getNode(MipsISD::Ext, DL, ValTy,
647 ShiftRight.getOperand(0),
648 DAG.getConstant(Pos, DL, MVT::i32),
649 DAG.getConstant(SMSize, DL, MVT::i32));
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000650}
Jia Liuf54f60f2012-02-28 07:46:26 +0000651
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000652static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000653 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000654 const MipsSubtarget &Subtarget) {
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000655 // Pattern match INS.
656 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
Jia Liuf54f60f2012-02-28 07:46:26 +0000657 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000658 // => ins $dst, $src, size, pos, $src1
Eric Christopher1c29a652014-07-18 22:55:25 +0000659 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasExtractInsert())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000660 return SDValue();
661
662 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
663 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
664 ConstantSDNode *CN;
665
666 // See if Op's first operand matches (and $src1 , mask0).
667 if (And0.getOpcode() != ISD::AND)
668 return SDValue();
669
670 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000671 !isShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000672 return SDValue();
673
674 // See if Op's second operand matches (and (shl $src, pos), mask1).
675 if (And1.getOpcode() != ISD::AND)
676 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000677
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000678 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000679 !isShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000680 return SDValue();
681
682 // The shift masks must have the same position and size.
683 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
684 return SDValue();
685
686 SDValue Shl = And1.getOperand(0);
687 if (Shl.getOpcode() != ISD::SHL)
688 return SDValue();
689
690 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
691 return SDValue();
692
693 unsigned Shamt = CN->getZExtValue();
694
695 // Return if the shift amount and the first bit position of mask are not the
Jia Liuf54f60f2012-02-28 07:46:26 +0000696 // same.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000697 EVT ValTy = N->getValueType(0);
698 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000699 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000700
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000701 SDLoc DL(N);
702 return DAG.getNode(MipsISD::Ins, DL, ValTy, Shl.getOperand(0),
703 DAG.getConstant(SMPos0, DL, MVT::i32),
704 DAG.getConstant(SMSize0, DL, MVT::i32),
705 And0.getOperand(0));
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000706}
Jia Liuf54f60f2012-02-28 07:46:26 +0000707
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000708static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000709 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000710 const MipsSubtarget &Subtarget) {
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000711 // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt))
712
713 if (DCI.isBeforeLegalizeOps())
714 return SDValue();
715
716 SDValue Add = N->getOperand(1);
717
718 if (Add.getOpcode() != ISD::ADD)
719 return SDValue();
720
721 SDValue Lo = Add.getOperand(1);
722
723 if ((Lo.getOpcode() != MipsISD::Lo) ||
724 (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable))
725 return SDValue();
726
727 EVT ValTy = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000728 SDLoc DL(N);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000729
730 SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, N->getOperand(0),
731 Add.getOperand(0));
732 return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo);
733}
734
Vasileios Kalintiris3751d412016-04-13 15:07:45 +0000735static SDValue performAssertZextCombine(SDNode *N, SelectionDAG &DAG,
736 TargetLowering::DAGCombinerInfo &DCI,
737 const MipsSubtarget &Subtarget) {
738 SDValue N0 = N->getOperand(0);
739 EVT NarrowerVT = cast<VTSDNode>(N->getOperand(1))->getVT();
740
741 if (N0.getOpcode() != ISD::TRUNCATE)
742 return SDValue();
743
744 if (N0.getOperand(0).getOpcode() != ISD::AssertZext)
745 return SDValue();
746
747 // fold (AssertZext (trunc (AssertZext x))) -> (trunc (AssertZext x))
748 // if the type of the extension of the innermost AssertZext node is
749 // smaller from that of the outermost node, eg:
750 // (AssertZext:i32 (trunc:i32 (AssertZext:i64 X, i32)), i8)
751 // -> (trunc:i32 (AssertZext X, i8))
752 SDValue WiderAssertZext = N0.getOperand(0);
753 EVT WiderVT = cast<VTSDNode>(WiderAssertZext->getOperand(1))->getVT();
754
755 if (NarrowerVT.bitsLT(WiderVT)) {
756 SDValue NewAssertZext = DAG.getNode(
757 ISD::AssertZext, SDLoc(N), WiderAssertZext.getValueType(),
758 WiderAssertZext.getOperand(0), DAG.getValueType(NarrowerVT));
759 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0),
760 NewAssertZext);
761 }
762
763 return SDValue();
764}
765
Bruno Cardoso Lopes61a61e92011-02-10 18:05:10 +0000766SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000767 const {
768 SelectionDAG &DAG = DCI.DAG;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000769 unsigned Opc = N->getOpcode();
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000770
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000771 switch (Opc) {
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000772 default: break;
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000773 case ISD::SDIVREM:
774 case ISD::UDIVREM:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000775 return performDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000776 case ISD::SELECT:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000777 return performSELECTCombine(N, DAG, DCI, Subtarget);
Vasileios Kalintirise741eb22015-03-02 12:47:32 +0000778 case MipsISD::CMovFP_F:
779 case MipsISD::CMovFP_T:
780 return performCMovFPCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000781 case ISD::AND:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000782 return performANDCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000783 case ISD::OR:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000784 return performORCombine(N, DAG, DCI, Subtarget);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000785 case ISD::ADD:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000786 return performADDCombine(N, DAG, DCI, Subtarget);
Vasileios Kalintiris3751d412016-04-13 15:07:45 +0000787 case ISD::AssertZext:
788 return performAssertZextCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000789 }
790
791 return SDValue();
792}
793
Sanjay Patelf7401292015-11-11 17:24:56 +0000794bool MipsTargetLowering::isCheapToSpeculateCttz() const {
795 return Subtarget.hasMips32();
796}
797
798bool MipsTargetLowering::isCheapToSpeculateCtlz() const {
799 return Subtarget.hasMips32();
800}
801
Akira Hatanakafabb8cf2012-09-21 23:58:31 +0000802void
803MipsTargetLowering::LowerOperationWrapper(SDNode *N,
804 SmallVectorImpl<SDValue> &Results,
805 SelectionDAG &DAG) const {
806 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
807
808 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
809 Results.push_back(Res.getValue(I));
810}
811
812void
813MipsTargetLowering::ReplaceNodeResults(SDNode *N,
814 SmallVectorImpl<SDValue> &Results,
815 SelectionDAG &DAG) const {
Akira Hatanaka9da442f2013-04-30 21:17:07 +0000816 return LowerOperationWrapper(N, Results, DAG);
Akira Hatanakafabb8cf2012-09-21 23:58:31 +0000817}
818
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000819SDValue MipsTargetLowering::
Dan Gohman21cea8a2010-04-17 15:26:15 +0000820LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000821{
Wesley Peck527da1b2010-11-23 03:31:01 +0000822 switch (Op.getOpcode())
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000823 {
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000824 case ISD::BR_JT: return lowerBR_JT(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000825 case ISD::ConstantPool: return lowerConstantPool(Op, DAG);
826 case ISD::GlobalAddress: return lowerGlobalAddress(Op, DAG);
827 case ISD::BlockAddress: return lowerBlockAddress(Op, DAG);
828 case ISD::GlobalTLSAddress: return lowerGlobalTLSAddress(Op, DAG);
829 case ISD::JumpTable: return lowerJumpTable(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000830 case ISD::VASTART: return lowerVASTART(Op, DAG);
Daniel Sanders2b553d42014-08-01 09:17:39 +0000831 case ISD::VAARG: return lowerVAARG(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000832 case ISD::FCOPYSIGN: return lowerFCOPYSIGN(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000833 case ISD::FRAMEADDR: return lowerFRAMEADDR(Op, DAG);
834 case ISD::RETURNADDR: return lowerRETURNADDR(Op, DAG);
835 case ISD::EH_RETURN: return lowerEH_RETURN(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000836 case ISD::ATOMIC_FENCE: return lowerATOMIC_FENCE(Op, DAG);
837 case ISD::SHL_PARTS: return lowerShiftLeftParts(Op, DAG);
838 case ISD::SRA_PARTS: return lowerShiftRightParts(Op, DAG, true);
839 case ISD::SRL_PARTS: return lowerShiftRightParts(Op, DAG, false);
840 case ISD::LOAD: return lowerLOAD(Op, DAG);
841 case ISD::STORE: return lowerSTORE(Op, DAG);
Hal Finkel5081ac22016-09-01 10:28:47 +0000842 case ISD::EH_DWARF_CFA: return lowerEH_DWARF_CFA(Op, DAG);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000843 case ISD::FP_TO_SINT: return lowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000844 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000845 return SDValue();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000846}
847
Akira Hatanakae2489122011-04-15 21:51:11 +0000848//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000849// Lower helper functions
Akira Hatanakae2489122011-04-15 21:51:11 +0000850//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000851
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000852// addLiveIn - This helper function adds the specified physical register to the
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000853// MachineFunction as a live in value. It also creates a corresponding
854// virtual register for it.
855static unsigned
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000856addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000857{
Chris Lattnera10fff52007-12-31 04:13:23 +0000858 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
859 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000860 return VReg;
861}
862
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000863static MachineBasicBlock *insertDivByZeroTrap(MachineInstr &MI,
Daniel Sanders308181e2014-06-12 10:44:10 +0000864 MachineBasicBlock &MBB,
865 const TargetInstrInfo &TII,
Zlatko Buljan58d6a952016-04-13 08:02:26 +0000866 bool Is64Bit, bool IsMicroMips) {
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000867 if (NoZeroDivCheck)
868 return &MBB;
869
870 // Insert instruction "teq $divisor_reg, $zero, 7".
871 MachineBasicBlock::iterator I(MI);
872 MachineInstrBuilder MIB;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000873 MachineOperand &Divisor = MI.getOperand(2);
874 MIB = BuildMI(MBB, std::next(I), MI.getDebugLoc(),
Zlatko Buljan58d6a952016-04-13 08:02:26 +0000875 TII.get(IsMicroMips ? Mips::TEQ_MM : Mips::TEQ))
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000876 .addReg(Divisor.getReg(), getKillRegState(Divisor.isKill()))
877 .addReg(Mips::ZERO)
878 .addImm(7);
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000879
880 // Use the 32-bit sub-register if this is a 64-bit division.
881 if (Is64Bit)
882 MIB->getOperand(0).setSubReg(Mips::sub_32);
883
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000884 // Clear Divisor's kill flag.
885 Divisor.setIsKill(false);
Daniel Sanders308181e2014-06-12 10:44:10 +0000886
887 // We would normally delete the original instruction here but in this case
888 // we only needed to inject an additional instruction rather than replace it.
889
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000890 return &MBB;
891}
892
Akira Hatanakae4bd0542012-09-27 02:15:57 +0000893MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000894MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
Dan Gohman25c16532010-05-01 00:01:06 +0000895 MachineBasicBlock *BB) const {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +0000896 switch (MI.getOpcode()) {
Reed Kotler97ba5f22013-02-21 04:22:38 +0000897 default:
898 llvm_unreachable("Unexpected instr type to insert");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000899 case Mips::ATOMIC_LOAD_ADD_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000900 return emitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000901 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000902 return emitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000903 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000904 return emitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000905 case Mips::ATOMIC_LOAD_ADD_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000906 return emitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000907
908 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000909 return emitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000910 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000911 return emitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000912 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000913 return emitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000914 case Mips::ATOMIC_LOAD_AND_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000915 return emitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000916
917 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000918 return emitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000919 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000920 return emitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000921 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000922 return emitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000923 case Mips::ATOMIC_LOAD_OR_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000924 return emitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000925
926 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000927 return emitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000928 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000929 return emitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000930 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000931 return emitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000932 case Mips::ATOMIC_LOAD_XOR_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000933 return emitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000934
935 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000936 return emitAtomicBinaryPartword(MI, BB, 1, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000937 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000938 return emitAtomicBinaryPartword(MI, BB, 2, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000939 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000940 return emitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000941 case Mips::ATOMIC_LOAD_NAND_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000942 return emitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000943
944 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000945 return emitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000946 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000947 return emitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000948 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000949 return emitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000950 case Mips::ATOMIC_LOAD_SUB_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000951 return emitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000952
953 case Mips::ATOMIC_SWAP_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000954 return emitAtomicBinaryPartword(MI, BB, 1, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000955 case Mips::ATOMIC_SWAP_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000956 return emitAtomicBinaryPartword(MI, BB, 2, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000957 case Mips::ATOMIC_SWAP_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000958 return emitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000959 case Mips::ATOMIC_SWAP_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000960 return emitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000961
962 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000963 return emitAtomicCmpSwapPartword(MI, BB, 1);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000964 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000965 return emitAtomicCmpSwapPartword(MI, BB, 2);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000966 case Mips::ATOMIC_CMP_SWAP_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000967 return emitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000968 case Mips::ATOMIC_CMP_SWAP_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000969 return emitAtomicCmpSwap(MI, BB, 8);
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000970 case Mips::PseudoSDIV:
971 case Mips::PseudoUDIV:
Daniel Sanders308181e2014-06-12 10:44:10 +0000972 case Mips::DIV:
973 case Mips::DIVU:
974 case Mips::MOD:
975 case Mips::MODU:
Zlatko Buljan58d6a952016-04-13 08:02:26 +0000976 return insertDivByZeroTrap(MI, *BB, *Subtarget.getInstrInfo(), false,
977 false);
978 case Mips::SDIV_MM_Pseudo:
979 case Mips::UDIV_MM_Pseudo:
980 case Mips::SDIV_MM:
981 case Mips::UDIV_MM:
982 case Mips::DIV_MMR6:
983 case Mips::DIVU_MMR6:
984 case Mips::MOD_MMR6:
985 case Mips::MODU_MMR6:
986 return insertDivByZeroTrap(MI, *BB, *Subtarget.getInstrInfo(), false, true);
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000987 case Mips::PseudoDSDIV:
988 case Mips::PseudoDUDIV:
Daniel Sanders308181e2014-06-12 10:44:10 +0000989 case Mips::DDIV:
990 case Mips::DDIVU:
991 case Mips::DMOD:
992 case Mips::DMODU:
Zlatko Buljan58d6a952016-04-13 08:02:26 +0000993 return insertDivByZeroTrap(MI, *BB, *Subtarget.getInstrInfo(), true, false);
994 case Mips::DDIV_MM64R6:
995 case Mips::DDIVU_MM64R6:
996 case Mips::DMOD_MM64R6:
997 case Mips::DMODU_MM64R6:
998 return insertDivByZeroTrap(MI, *BB, *Subtarget.getInstrInfo(), true, true);
Daniel Sanders0fa60412014-06-12 13:39:06 +0000999 case Mips::SEL_D:
Zlatko Buljancd242c12016-06-09 11:15:53 +00001000 case Mips::SEL_D_MMR6:
Daniel Sanders0fa60412014-06-12 13:39:06 +00001001 return emitSEL_D(MI, BB);
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001002
1003 case Mips::PseudoSELECT_I:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001004 case Mips::PseudoSELECT_I64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001005 case Mips::PseudoSELECT_S:
1006 case Mips::PseudoSELECT_D32:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001007 case Mips::PseudoSELECT_D64:
Vasileios Kalintiris36901dd2016-03-01 20:25:43 +00001008 return emitPseudoSELECT(MI, BB, false, Mips::BNE);
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001009 case Mips::PseudoSELECTFP_F_I:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001010 case Mips::PseudoSELECTFP_F_I64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001011 case Mips::PseudoSELECTFP_F_S:
1012 case Mips::PseudoSELECTFP_F_D32:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001013 case Mips::PseudoSELECTFP_F_D64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001014 return emitPseudoSELECT(MI, BB, true, Mips::BC1F);
1015 case Mips::PseudoSELECTFP_T_I:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001016 case Mips::PseudoSELECTFP_T_I64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001017 case Mips::PseudoSELECTFP_T_S:
1018 case Mips::PseudoSELECTFP_T_D32:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001019 case Mips::PseudoSELECTFP_T_D64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001020 return emitPseudoSELECT(MI, BB, true, Mips::BC1T);
Akira Hatanakaa5352702011-03-31 18:26:17 +00001021 }
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001022}
1023
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001024// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
1025// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001026MachineBasicBlock *MipsTargetLowering::emitAtomicBinary(MachineInstr &MI,
1027 MachineBasicBlock *BB,
1028 unsigned Size,
1029 unsigned BinOpcode,
1030 bool Nand) const {
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001031 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001032
1033 MachineFunction *MF = BB->getParent();
1034 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001035 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Eric Christopher96e72c62015-01-29 23:27:36 +00001036 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001037 const bool ArePtrs64bit = ABI.ArePtrs64bit();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001038 DebugLoc DL = MI.getDebugLoc();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001039 unsigned LL, SC, AND, NOR, ZERO, BEQ;
1040
1041 if (Size == 4) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001042 if (isMicroMips) {
1043 LL = Mips::LL_MM;
1044 SC = Mips::SC_MM;
1045 } else {
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001046 LL = Subtarget.hasMips32r6()
1047 ? (ArePtrs64bit ? Mips::LL64_R6 : Mips::LL_R6)
1048 : (ArePtrs64bit ? Mips::LL64 : Mips::LL);
1049 SC = Subtarget.hasMips32r6()
1050 ? (ArePtrs64bit ? Mips::SC64_R6 : Mips::SC_R6)
1051 : (ArePtrs64bit ? Mips::SC64 : Mips::SC);
Daniel Sanders6a803f62014-06-16 13:13:03 +00001052 }
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001053
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001054 AND = Mips::AND;
1055 NOR = Mips::NOR;
1056 ZERO = Mips::ZERO;
1057 BEQ = Mips::BEQ;
Daniel Sanders6a803f62014-06-16 13:13:03 +00001058 } else {
Daniel Sandersbdcfab12014-07-24 09:47:14 +00001059 LL = Subtarget.hasMips64r6() ? Mips::LLD_R6 : Mips::LLD;
1060 SC = Subtarget.hasMips64r6() ? Mips::SCD_R6 : Mips::SCD;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001061 AND = Mips::AND64;
1062 NOR = Mips::NOR64;
1063 ZERO = Mips::ZERO_64;
1064 BEQ = Mips::BEQ64;
1065 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001066
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001067 unsigned OldVal = MI.getOperand(0).getReg();
1068 unsigned Ptr = MI.getOperand(1).getReg();
1069 unsigned Incr = MI.getOperand(2).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001070
Akira Hatanaka0e019592011-07-19 20:11:17 +00001071 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1072 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1073 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001074
1075 // insert new blocks after the current block
1076 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1077 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1078 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +00001079 MachineFunction::iterator It = ++BB->getIterator();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001080 MF->insert(It, loopMBB);
1081 MF->insert(It, exitMBB);
1082
1083 // Transfer the remainder of BB and its successor edges to exitMBB.
1084 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001085 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001086 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1087
1088 // thisMBB:
1089 // ...
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001090 // fallthrough --> loopMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001091 BB->addSuccessor(loopMBB);
Akira Hatanaka08636b42011-07-19 17:09:53 +00001092 loopMBB->addSuccessor(loopMBB);
1093 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001094
1095 // loopMBB:
1096 // ll oldval, 0(ptr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001097 // <binop> storeval, oldval, incr
1098 // sc success, storeval, 0(ptr)
1099 // beq success, $0, loopMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001100 BB = loopMBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001101 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001102 if (Nand) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001103 // and andres, oldval, incr
1104 // nor storeval, $0, andres
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001105 BuildMI(BB, DL, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
1106 BuildMI(BB, DL, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001107 } else if (BinOpcode) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001108 // <binop> storeval, oldval, incr
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001109 BuildMI(BB, DL, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001110 } else {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001111 StoreVal = Incr;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001112 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001113 BuildMI(BB, DL, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
1114 BuildMI(BB, DL, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001115
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001116 MI.eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001117
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001118 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001119}
1120
Daniel Sanders6a803f62014-06-16 13:13:03 +00001121MachineBasicBlock *MipsTargetLowering::emitSignExtendToI32InReg(
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001122 MachineInstr &MI, MachineBasicBlock *BB, unsigned Size, unsigned DstReg,
Daniel Sanders6a803f62014-06-16 13:13:03 +00001123 unsigned SrcReg) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00001124 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001125 const DebugLoc &DL = MI.getDebugLoc();
Daniel Sanders6a803f62014-06-16 13:13:03 +00001126
Eric Christopher1c29a652014-07-18 22:55:25 +00001127 if (Subtarget.hasMips32r2() && Size == 1) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001128 BuildMI(BB, DL, TII->get(Mips::SEB), DstReg).addReg(SrcReg);
1129 return BB;
1130 }
1131
Eric Christopher1c29a652014-07-18 22:55:25 +00001132 if (Subtarget.hasMips32r2() && Size == 2) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001133 BuildMI(BB, DL, TII->get(Mips::SEH), DstReg).addReg(SrcReg);
1134 return BB;
1135 }
1136
1137 MachineFunction *MF = BB->getParent();
1138 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1139 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1140 unsigned ScrReg = RegInfo.createVirtualRegister(RC);
1141
1142 assert(Size < 32);
1143 int64_t ShiftImm = 32 - (Size * 8);
1144
1145 BuildMI(BB, DL, TII->get(Mips::SLL), ScrReg).addReg(SrcReg).addImm(ShiftImm);
1146 BuildMI(BB, DL, TII->get(Mips::SRA), DstReg).addReg(ScrReg).addImm(ShiftImm);
1147
1148 return BB;
1149}
1150
1151MachineBasicBlock *MipsTargetLowering::emitAtomicBinaryPartword(
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001152 MachineInstr &MI, MachineBasicBlock *BB, unsigned Size, unsigned BinOpcode,
Daniel Sanders6a803f62014-06-16 13:13:03 +00001153 bool Nand) const {
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001154 assert((Size == 1 || Size == 2) &&
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001155 "Unsupported size for EmitAtomicBinaryPartial.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001156
1157 MachineFunction *MF = BB->getParent();
1158 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1159 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001160 const bool ArePtrs64bit = ABI.ArePtrs64bit();
Simon Dardisa2d8cc32016-04-28 16:26:43 +00001161 const TargetRegisterClass *RCp =
1162 getRegClassFor(ArePtrs64bit ? MVT::i64 : MVT::i32);
Eric Christopher96e72c62015-01-29 23:27:36 +00001163 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001164 DebugLoc DL = MI.getDebugLoc();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001165
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001166 unsigned Dest = MI.getOperand(0).getReg();
1167 unsigned Ptr = MI.getOperand(1).getReg();
1168 unsigned Incr = MI.getOperand(2).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001169
Simon Dardisa2d8cc32016-04-28 16:26:43 +00001170 unsigned AlignedAddr = RegInfo.createVirtualRegister(RCp);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001171 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001172 unsigned Mask = RegInfo.createVirtualRegister(RC);
1173 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001174 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1175 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001176 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Simon Dardisa2d8cc32016-04-28 16:26:43 +00001177 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RCp);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001178 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1179 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1180 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1181 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka9663dd32011-07-19 20:56:53 +00001182 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001183 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1184 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1185 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001186 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001187
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001188 unsigned LL, SC;
1189 if (isMicroMips) {
1190 LL = Mips::LL_MM;
1191 SC = Mips::SC_MM;
1192 } else {
1193 LL = Subtarget.hasMips32r6() ? (ArePtrs64bit ? Mips::LL64_R6 : Mips::LL_R6)
1194 : (ArePtrs64bit ? Mips::LL64 : Mips::LL);
1195 SC = Subtarget.hasMips32r6() ? (ArePtrs64bit ? Mips::SC64_R6 : Mips::SC_R6)
1196 : (ArePtrs64bit ? Mips::SC64 : Mips::SC);
1197 }
1198
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001199 // insert new blocks after the current block
1200 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1201 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001202 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001203 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +00001204 MachineFunction::iterator It = ++BB->getIterator();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001205 MF->insert(It, loopMBB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001206 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001207 MF->insert(It, exitMBB);
1208
1209 // Transfer the remainder of BB and its successor edges to exitMBB.
1210 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001211 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001212 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1213
Akira Hatanaka08636b42011-07-19 17:09:53 +00001214 BB->addSuccessor(loopMBB);
1215 loopMBB->addSuccessor(loopMBB);
1216 loopMBB->addSuccessor(sinkMBB);
1217 sinkMBB->addSuccessor(exitMBB);
1218
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001219 // thisMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001220 // addiu masklsb2,$0,-4 # 0xfffffffc
1221 // and alignedaddr,ptr,masklsb2
1222 // andi ptrlsb2,ptr,3
1223 // sll shiftamt,ptrlsb2,3
1224 // ori maskupper,$0,255 # 0xff
1225 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001226 // nor mask2,$0,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001227 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001228
1229 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Simon Dardisa2d8cc32016-04-28 16:26:43 +00001230 BuildMI(BB, DL, TII->get(ABI.GetPtrAddiuOp()), MaskLSB2)
1231 .addReg(ABI.GetNullPtr()).addImm(-4);
1232 BuildMI(BB, DL, TII->get(ABI.GetPtrAndOp()), AlignedAddr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001233 .addReg(Ptr).addReg(MaskLSB2);
Simon Dardisa2d8cc32016-04-28 16:26:43 +00001234 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2)
1235 .addReg(Ptr, 0, ArePtrs64bit ? Mips::sub_32 : 0).addImm(3);
Eric Christopher1c29a652014-07-18 22:55:25 +00001236 if (Subtarget.isLittle()) {
Akira Hatanaka2bf97332013-05-31 03:25:44 +00001237 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1238 } else {
1239 unsigned Off = RegInfo.createVirtualRegister(RC);
1240 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1241 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1242 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1243 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001244 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001245 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001246 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001247 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001248 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001249 BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(Incr).addReg(ShiftAmt);
Bruno Cardoso Lopesf771a0f2011-05-31 20:25:26 +00001250
Akira Hatanaka27292632011-07-18 18:52:12 +00001251 // atomic.load.binop
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001252 // loopMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001253 // ll oldval,0(alignedaddr)
1254 // binop binopres,oldval,incr2
1255 // and newval,binopres,mask
1256 // and maskedoldval0,oldval,mask2
1257 // or storeval,maskedoldval0,newval
1258 // sc success,storeval,0(alignedaddr)
1259 // beq success,$0,loopMBB
1260
Akira Hatanaka27292632011-07-18 18:52:12 +00001261 // atomic.swap
1262 // loopMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001263 // ll oldval,0(alignedaddr)
Akira Hatanakae4503582011-07-19 18:14:26 +00001264 // and newval,incr2,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001265 // and maskedoldval0,oldval,mask2
1266 // or storeval,maskedoldval0,newval
1267 // sc success,storeval,0(alignedaddr)
1268 // beq success,$0,loopMBB
Akira Hatanaka27292632011-07-18 18:52:12 +00001269
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001270 BB = loopMBB;
Jozef Kolek2f27d572014-12-18 16:39:29 +00001271 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001272 if (Nand) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001273 // and andres, oldval, incr2
1274 // nor binopres, $0, andres
1275 // and newval, binopres, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001276 BuildMI(BB, DL, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1277 BuildMI(BB, DL, TII->get(Mips::NOR), BinOpRes)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001278 .addReg(Mips::ZERO).addReg(AndRes);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001279 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001280 } else if (BinOpcode) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001281 // <binop> binopres, oldval, incr2
1282 // and newval, binopres, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001283 BuildMI(BB, DL, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1284 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001285 } else { // atomic.swap
Akira Hatanaka0e019592011-07-19 20:11:17 +00001286 // and newval, incr2, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001287 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanakae4503582011-07-19 18:14:26 +00001288 }
Jia Liuf54f60f2012-02-28 07:46:26 +00001289
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001290 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001291 .addReg(OldVal).addReg(Mask2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001292 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka9663dd32011-07-19 20:56:53 +00001293 .addReg(MaskedOldVal0).addReg(NewVal);
Jozef Kolek2f27d572014-12-18 16:39:29 +00001294 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001295 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001296 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001297 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001298
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001299 // sinkMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001300 // and maskedoldval1,oldval,mask
1301 // srl srlres,maskedoldval1,shiftamt
Daniel Sanders6a803f62014-06-16 13:13:03 +00001302 // sign_extend dest,srlres
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001303 BB = sinkMBB;
Akira Hatanakae97bd812011-07-19 03:14:58 +00001304
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001305 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001306 .addReg(OldVal).addReg(Mask);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001307 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001308 .addReg(MaskedOldVal1).addReg(ShiftAmt);
Daniel Sanders6a803f62014-06-16 13:13:03 +00001309 BB = emitSignExtendToI32InReg(MI, BB, Size, Dest, SrlRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001310
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001311 MI.eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001312
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001313 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001314}
1315
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001316MachineBasicBlock *MipsTargetLowering::emitAtomicCmpSwap(MachineInstr &MI,
1317 MachineBasicBlock *BB,
1318 unsigned Size) const {
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001319 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001320
1321 MachineFunction *MF = BB->getParent();
1322 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001323 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Eric Christopher96e72c62015-01-29 23:27:36 +00001324 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001325 const bool ArePtrs64bit = ABI.ArePtrs64bit();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001326 DebugLoc DL = MI.getDebugLoc();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001327 unsigned LL, SC, ZERO, BNE, BEQ;
1328
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001329 if (Size == 4) {
1330 if (isMicroMips) {
1331 LL = Mips::LL_MM;
1332 SC = Mips::SC_MM;
1333 } else {
1334 LL = Subtarget.hasMips32r6()
1335 ? (ArePtrs64bit ? Mips::LL64_R6 : Mips::LL_R6)
1336 : (ArePtrs64bit ? Mips::LL64 : Mips::LL);
1337 SC = Subtarget.hasMips32r6()
1338 ? (ArePtrs64bit ? Mips::SC64_R6 : Mips::SC_R6)
1339 : (ArePtrs64bit ? Mips::SC64 : Mips::SC);
1340 }
1341
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001342 ZERO = Mips::ZERO;
1343 BNE = Mips::BNE;
1344 BEQ = Mips::BEQ;
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001345 } else {
Zoran Jovanovic796ed6d2015-10-29 14:40:19 +00001346 LL = Subtarget.hasMips64r6() ? Mips::LLD_R6 : Mips::LLD;
1347 SC = Subtarget.hasMips64r6() ? Mips::SCD_R6 : Mips::SCD;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001348 ZERO = Mips::ZERO_64;
1349 BNE = Mips::BNE64;
1350 BEQ = Mips::BEQ64;
1351 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001352
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001353 unsigned Dest = MI.getOperand(0).getReg();
1354 unsigned Ptr = MI.getOperand(1).getReg();
1355 unsigned OldVal = MI.getOperand(2).getReg();
1356 unsigned NewVal = MI.getOperand(3).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001357
Akira Hatanaka0e019592011-07-19 20:11:17 +00001358 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001359
1360 // insert new blocks after the current block
1361 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1362 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1363 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1364 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +00001365 MachineFunction::iterator It = ++BB->getIterator();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001366 MF->insert(It, loop1MBB);
1367 MF->insert(It, loop2MBB);
1368 MF->insert(It, exitMBB);
1369
1370 // Transfer the remainder of BB and its successor edges to exitMBB.
1371 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001372 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001373 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1374
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001375 // thisMBB:
1376 // ...
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001377 // fallthrough --> loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001378 BB->addSuccessor(loop1MBB);
Akira Hatanaka08636b42011-07-19 17:09:53 +00001379 loop1MBB->addSuccessor(exitMBB);
1380 loop1MBB->addSuccessor(loop2MBB);
1381 loop2MBB->addSuccessor(loop1MBB);
1382 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001383
1384 // loop1MBB:
1385 // ll dest, 0(ptr)
1386 // bne dest, oldval, exitMBB
1387 BB = loop1MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001388 BuildMI(BB, DL, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1389 BuildMI(BB, DL, TII->get(BNE))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001390 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001391
1392 // loop2MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001393 // sc success, newval, 0(ptr)
1394 // beq success, $0, loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001395 BB = loop2MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001396 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001397 .addReg(NewVal).addReg(Ptr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001398 BuildMI(BB, DL, TII->get(BEQ))
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001399 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001400
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001401 MI.eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001402
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001403 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001404}
1405
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001406MachineBasicBlock *MipsTargetLowering::emitAtomicCmpSwapPartword(
1407 MachineInstr &MI, MachineBasicBlock *BB, unsigned Size) const {
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001408 assert((Size == 1 || Size == 2) &&
1409 "Unsupported size for EmitAtomicCmpSwapPartial.");
1410
1411 MachineFunction *MF = BB->getParent();
1412 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1413 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001414 const bool ArePtrs64bit = ABI.ArePtrs64bit();
Zoran Jovanovic2f6845b2016-04-13 16:02:25 +00001415 const TargetRegisterClass *RCp =
1416 getRegClassFor(ArePtrs64bit ? MVT::i64 : MVT::i32);
Eric Christopher96e72c62015-01-29 23:27:36 +00001417 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001418 DebugLoc DL = MI.getDebugLoc();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001419
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001420 unsigned Dest = MI.getOperand(0).getReg();
1421 unsigned Ptr = MI.getOperand(1).getReg();
1422 unsigned CmpVal = MI.getOperand(2).getReg();
1423 unsigned NewVal = MI.getOperand(3).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001424
Zoran Jovanovic2f6845b2016-04-13 16:02:25 +00001425 unsigned AlignedAddr = RegInfo.createVirtualRegister(RCp);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001426 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001427 unsigned Mask = RegInfo.createVirtualRegister(RC);
1428 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001429 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1430 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1431 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1432 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
Zoran Jovanovic2f6845b2016-04-13 16:02:25 +00001433 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RCp);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001434 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1435 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1436 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1437 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1438 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1439 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1440 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001441 unsigned Success = RegInfo.createVirtualRegister(RC);
Simon Dardis4fbf76f2016-06-14 11:29:28 +00001442 unsigned LL, SC;
1443
1444 if (isMicroMips) {
1445 LL = Mips::LL_MM;
1446 SC = Mips::SC_MM;
1447 } else {
1448 LL = Subtarget.hasMips32r6() ? (ArePtrs64bit ? Mips::LL64_R6 : Mips::LL_R6)
1449 : (ArePtrs64bit ? Mips::LL64 : Mips::LL);
1450 SC = Subtarget.hasMips32r6() ? (ArePtrs64bit ? Mips::SC64_R6 : Mips::SC_R6)
1451 : (ArePtrs64bit ? Mips::SC64 : Mips::SC);
1452 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001453
1454 // insert new blocks after the current block
1455 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1456 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1457 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001458 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001459 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +00001460 MachineFunction::iterator It = ++BB->getIterator();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001461 MF->insert(It, loop1MBB);
1462 MF->insert(It, loop2MBB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001463 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001464 MF->insert(It, exitMBB);
1465
1466 // Transfer the remainder of BB and its successor edges to exitMBB.
1467 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001468 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001469 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1470
Akira Hatanaka08636b42011-07-19 17:09:53 +00001471 BB->addSuccessor(loop1MBB);
1472 loop1MBB->addSuccessor(sinkMBB);
1473 loop1MBB->addSuccessor(loop2MBB);
1474 loop2MBB->addSuccessor(loop1MBB);
1475 loop2MBB->addSuccessor(sinkMBB);
1476 sinkMBB->addSuccessor(exitMBB);
1477
Akira Hatanakae4503582011-07-19 18:14:26 +00001478 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001479 // thisMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001480 // addiu masklsb2,$0,-4 # 0xfffffffc
1481 // and alignedaddr,ptr,masklsb2
1482 // andi ptrlsb2,ptr,3
Zoran Jovanovic2f6845b2016-04-13 16:02:25 +00001483 // xori ptrlsb2,ptrlsb2,3 # Only for BE
Akira Hatanaka0e019592011-07-19 20:11:17 +00001484 // sll shiftamt,ptrlsb2,3
1485 // ori maskupper,$0,255 # 0xff
1486 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001487 // nor mask2,$0,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001488 // andi maskedcmpval,cmpval,255
1489 // sll shiftedcmpval,maskedcmpval,shiftamt
1490 // andi maskednewval,newval,255
1491 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001492 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Zoran Jovanovic2f6845b2016-04-13 16:02:25 +00001493 BuildMI(BB, DL, TII->get(ArePtrs64bit ? Mips::DADDiu : Mips::ADDiu), MaskLSB2)
1494 .addReg(ABI.GetNullPtr()).addImm(-4);
1495 BuildMI(BB, DL, TII->get(ArePtrs64bit ? Mips::AND64 : Mips::AND), AlignedAddr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001496 .addReg(Ptr).addReg(MaskLSB2);
Zoran Jovanovic2f6845b2016-04-13 16:02:25 +00001497 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2)
1498 .addReg(Ptr, 0, ArePtrs64bit ? Mips::sub_32 : 0).addImm(3);
Eric Christopher1c29a652014-07-18 22:55:25 +00001499 if (Subtarget.isLittle()) {
Akira Hatanaka2bf97332013-05-31 03:25:44 +00001500 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1501 } else {
1502 unsigned Off = RegInfo.createVirtualRegister(RC);
1503 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1504 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1505 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1506 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001507 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001508 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001509 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001510 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001511 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1512 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001513 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001514 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001515 .addReg(MaskedCmpVal).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001516 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001517 .addReg(NewVal).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001518 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001519 .addReg(MaskedNewVal).addReg(ShiftAmt);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001520
1521 // loop1MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001522 // ll oldval,0(alginedaddr)
1523 // and maskedoldval0,oldval,mask
1524 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001525 BB = loop1MBB;
Jozef Kolek2f27d572014-12-18 16:39:29 +00001526 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001527 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001528 .addReg(OldVal).addReg(Mask);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001529 BuildMI(BB, DL, TII->get(Mips::BNE))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001530 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001531
1532 // loop2MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001533 // and maskedoldval1,oldval,mask2
1534 // or storeval,maskedoldval1,shiftednewval
1535 // sc success,storeval,0(alignedaddr)
1536 // beq success,$0,loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001537 BB = loop2MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001538 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001539 .addReg(OldVal).addReg(Mask2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001540 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001541 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
Jozef Kolek2f27d572014-12-18 16:39:29 +00001542 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001543 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001544 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001545 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001546
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001547 // sinkMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001548 // srl srlres,maskedoldval0,shiftamt
Daniel Sanders6a803f62014-06-16 13:13:03 +00001549 // sign_extend dest,srlres
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001550 BB = sinkMBB;
Akira Hatanakae97bd812011-07-19 03:14:58 +00001551
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001552 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001553 .addReg(MaskedOldVal0).addReg(ShiftAmt);
Daniel Sanders6a803f62014-06-16 13:13:03 +00001554 BB = emitSignExtendToI32InReg(MI, BB, Size, Dest, SrlRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001555
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001556 MI.eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001557
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001558 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001559}
1560
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001561MachineBasicBlock *MipsTargetLowering::emitSEL_D(MachineInstr &MI,
Daniel Sanders0fa60412014-06-12 13:39:06 +00001562 MachineBasicBlock *BB) const {
1563 MachineFunction *MF = BB->getParent();
Eric Christopher96e72c62015-01-29 23:27:36 +00001564 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
1565 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders0fa60412014-06-12 13:39:06 +00001566 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001567 DebugLoc DL = MI.getDebugLoc();
Daniel Sanders0fa60412014-06-12 13:39:06 +00001568 MachineBasicBlock::iterator II(MI);
1569
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001570 unsigned Fc = MI.getOperand(1).getReg();
Daniel Sanders0fa60412014-06-12 13:39:06 +00001571 const auto &FGR64RegClass = TRI->getRegClass(Mips::FGR64RegClassID);
1572
1573 unsigned Fc2 = RegInfo.createVirtualRegister(FGR64RegClass);
1574
1575 BuildMI(*BB, II, DL, TII->get(Mips::SUBREG_TO_REG), Fc2)
1576 .addImm(0)
1577 .addReg(Fc)
1578 .addImm(Mips::sub_lo);
1579
1580 // We don't erase the original instruction, we just replace the condition
1581 // register with the 64-bit super-register.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001582 MI.getOperand(1).setReg(Fc2);
Daniel Sanders0fa60412014-06-12 13:39:06 +00001583
1584 return BB;
1585}
1586
Akira Hatanakae2489122011-04-15 21:51:11 +00001587//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001588// Misc Lower Operation implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00001589//===----------------------------------------------------------------------===//
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001590SDValue MipsTargetLowering::lowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001591 SDValue Chain = Op.getOperand(0);
1592 SDValue Table = Op.getOperand(1);
1593 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001594 SDLoc DL(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00001595 auto &TD = DAG.getDataLayout();
1596 EVT PTy = getPointerTy(TD);
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001597 unsigned EntrySize =
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001598 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(TD);
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001599
1600 Index = DAG.getNode(ISD::MUL, DL, PTy, Index,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001601 DAG.getConstant(EntrySize, DL, PTy));
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001602 SDValue Addr = DAG.getNode(ISD::ADD, DL, PTy, Index, Table);
1603
1604 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
Justin Lebar9c375812016-07-15 18:27:10 +00001605 Addr = DAG.getExtLoad(
1606 ISD::SEXTLOAD, DL, PTy, Chain, Addr,
1607 MachinePointerInfo::getJumpTable(DAG.getMachineFunction()), MemVT);
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001608 Chain = Addr.getValue(1);
1609
Rafael Espindola9f1c1fe2016-06-27 12:48:21 +00001610 if (isPositionIndependent() || ABI.IsN64()) {
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001611 // For PIC, the sequence is:
1612 // BRIND(load(Jumptable + index) + RelocBase)
1613 // RelocBase can be JumpTable, GOT or some sort of global base.
1614 Addr = DAG.getNode(ISD::ADD, DL, PTy, Addr,
1615 getPICJumpTableRelocBase(Table, DAG));
1616 }
1617
1618 return DAG.getNode(ISD::BRIND, DL, MVT::Other, Chain, Addr);
1619}
1620
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001621SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001622 SelectionDAG &DAG) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001623 EVT Ty = Op.getValueType();
1624 GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
1625 const GlobalValue *GV = N->getGlobal();
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001626
Rafael Espindola9f1c1fe2016-06-27 12:48:21 +00001627 if (!isPositionIndependent() && !ABI.IsN64()) {
Eric Christopher36fe0282015-02-03 07:22:52 +00001628 const MipsTargetObjectFile *TLOF =
1629 static_cast<const MipsTargetObjectFile *>(
1630 getTargetMachine().getObjFileLowering());
1631 if (TLOF->IsGlobalInSmallSection(GV, getTargetMachine()))
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001632 // %gp_rel relocation
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001633 return getAddrGPRel(N, SDLoc(N), Ty, DAG);
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001634
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001635 // %hi/%lo relocation
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001636 return getAddrNonPIC(N, SDLoc(N), Ty, DAG);
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001637 }
1638
Rafael Espindolab2b6a852016-06-27 12:33:33 +00001639 // Every other architecture would use shouldAssumeDSOLocal in here, but
1640 // mips is special.
Rafael Espindola97ca8272016-06-27 23:21:07 +00001641 // * In PIC code mips requires got loads even for local statics!
Rafael Espindolab2b6a852016-06-27 12:33:33 +00001642 // * To save on got entries, for local statics the got entry contains the
1643 // page and an additional add instruction takes care of the low bits.
1644 // * It is legal to access a hidden symbol with a non hidden undefined,
1645 // so one cannot guarantee that all access to a hidden symbol will know
1646 // it is hidden.
1647 // * Mips linkers don't support creating a page and a full got entry for
1648 // the same symbol.
1649 // * Given all that, we have to use a full got entry for hidden symbols :-(
Rafael Espindola1ac1fa82016-06-27 03:19:40 +00001650 if (GV->hasLocalLinkage())
Eric Christopher96e72c62015-01-29 23:27:36 +00001651 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001652
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00001653 if (LargeGOT)
Alex Lorenze40c8a22015-08-11 23:09:45 +00001654 return getAddrGlobalLargeGOT(
1655 N, SDLoc(N), Ty, DAG, MipsII::MO_GOT_HI16, MipsII::MO_GOT_LO16,
1656 DAG.getEntryNode(),
1657 MachinePointerInfo::getGOT(DAG.getMachineFunction()));
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00001658
Alex Lorenze40c8a22015-08-11 23:09:45 +00001659 return getAddrGlobal(
1660 N, SDLoc(N), Ty, DAG,
Daniel Sandersfe98b2f2016-05-03 13:35:44 +00001661 (ABI.IsN32() || ABI.IsN64()) ? MipsII::MO_GOT_DISP : MipsII::MO_GOT,
Alex Lorenze40c8a22015-08-11 23:09:45 +00001662 DAG.getEntryNode(), MachinePointerInfo::getGOT(DAG.getMachineFunction()));
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001663}
1664
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001665SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op,
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +00001666 SelectionDAG &DAG) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001667 BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op);
1668 EVT Ty = Op.getValueType();
Akira Hatanaka30f97cf2013-09-25 00:30:25 +00001669
Rafael Espindola9f1c1fe2016-06-27 12:48:21 +00001670 if (!isPositionIndependent() && !ABI.IsN64())
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001671 return getAddrNonPIC(N, SDLoc(N), Ty, DAG);
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001672
Eric Christopher96e72c62015-01-29 23:27:36 +00001673 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +00001674}
1675
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001676SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001677lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001678{
Akira Hatanakabff84e12011-12-14 18:26:41 +00001679 // If the relocation model is PIC, use the General Dynamic TLS Model or
1680 // Local Dynamic TLS model, otherwise use the Initial Exec or
1681 // Local Exec TLS Model.
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001682
1683 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +00001684 if (DAG.getTarget().Options.EmulatedTLS)
1685 return LowerToTLSEmulatedModel(GA, DAG);
1686
Andrew Trickef9de2a2013-05-25 02:42:55 +00001687 SDLoc DL(GA);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001688 const GlobalValue *GV = GA->getGlobal();
Mehdi Amini44ede332015-07-09 02:09:04 +00001689 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001690
Hans Wennborgaea41202012-05-04 09:40:39 +00001691 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
1692
1693 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Hans Wennborg245917b2012-06-04 14:02:08 +00001694 // General Dynamic and Local Dynamic TLS Model.
1695 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
1696 : MipsII::MO_TLSGD;
1697
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001698 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag);
1699 SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT,
1700 getGlobalReg(DAG, PtrVT), TGA);
Akira Hatanakaf10ee842011-12-08 21:05:38 +00001701 unsigned PtrSize = PtrVT.getSizeInBits();
1702 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1703
Benjamin Kramer64ba50a2011-12-11 12:21:34 +00001704 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001705
1706 ArgListTy Args;
1707 ArgListEntry Entry;
1708 Entry.Node = Argument;
Akira Hatanakadee6c822011-12-08 20:34:32 +00001709 Entry.Ty = PtrTy;
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001710 Args.push_back(Entry);
Jia Liuf54f60f2012-02-28 07:46:26 +00001711
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00001712 TargetLowering::CallLoweringInfo CLI(DAG);
1713 CLI.setDebugLoc(DL).setChain(DAG.getEntryNode())
Krzysztof Parzyszeke116d5002016-06-22 12:54:25 +00001714 .setCallee(CallingConv::C, PtrTy, TlsGetAddr, std::move(Args));
Justin Holewinskiaa583972012-05-25 16:35:28 +00001715 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001716
Akira Hatanakabff84e12011-12-14 18:26:41 +00001717 SDValue Ret = CallResult.first;
1718
Hans Wennborgaea41202012-05-04 09:40:39 +00001719 if (model != TLSModel::LocalDynamic)
Akira Hatanakabff84e12011-12-14 18:26:41 +00001720 return Ret;
1721
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001722 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanakabff84e12011-12-14 18:26:41 +00001723 MipsII::MO_DTPREL_HI);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001724 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1725 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanakabff84e12011-12-14 18:26:41 +00001726 MipsII::MO_DTPREL_LO);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001727 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1728 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret);
1729 return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001730 }
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001731
1732 SDValue Offset;
Hans Wennborgaea41202012-05-04 09:40:39 +00001733 if (model == TLSModel::InitialExec) {
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001734 // Initial Exec TLS Model
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001735 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001736 MipsII::MO_GOTTPREL);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001737 TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT),
Akira Hatanakab049aef2012-02-24 22:34:47 +00001738 TGA);
Justin Lebar9c375812016-07-15 18:27:10 +00001739 Offset =
1740 DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), TGA, MachinePointerInfo());
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001741 } else {
1742 // Local Exec TLS Model
Hans Wennborgaea41202012-05-04 09:40:39 +00001743 assert(model == TLSModel::LocalExec);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001744 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001745 MipsII::MO_TPREL_HI);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001746 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001747 MipsII::MO_TPREL_LO);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001748 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1749 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1750 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001751 }
1752
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001753 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
1754 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001755}
1756
1757SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001758lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +00001759{
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001760 JumpTableSDNode *N = cast<JumpTableSDNode>(Op);
1761 EVT Ty = Op.getValueType();
Akira Hatanaka30f97cf2013-09-25 00:30:25 +00001762
Rafael Espindola9f1c1fe2016-06-27 12:48:21 +00001763 if (!isPositionIndependent() && !ABI.IsN64())
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001764 return getAddrNonPIC(N, SDLoc(N), Ty, DAG);
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001765
Eric Christopher96e72c62015-01-29 23:27:36 +00001766 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +00001767}
1768
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001769SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001770lowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +00001771{
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001772 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
1773 EVT Ty = Op.getValueType();
Bruno Cardoso Lopes2db07582009-11-25 12:17:58 +00001774
Rafael Espindola9f1c1fe2016-06-27 12:48:21 +00001775 if (!isPositionIndependent() && !ABI.IsN64()) {
Eric Christopher36fe0282015-02-03 07:22:52 +00001776 const MipsTargetObjectFile *TLOF =
1777 static_cast<const MipsTargetObjectFile *>(
1778 getTargetMachine().getObjFileLowering());
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001779
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001780 if (TLOF->IsConstantInSmallSection(DAG.getDataLayout(), N->getConstVal(),
1781 getTargetMachine()))
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001782 // %gp_rel relocation
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001783 return getAddrGPRel(N, SDLoc(N), Ty, DAG);
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001784
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001785 return getAddrNonPIC(N, SDLoc(N), Ty, DAG);
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001786 }
Bruno Cardoso Lopesfdb4cec2008-07-23 16:01:50 +00001787
Eric Christopher96e72c62015-01-29 23:27:36 +00001788 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +00001789}
1790
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001791SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman31ae5862010-04-17 14:41:14 +00001792 MachineFunction &MF = DAG.getMachineFunction();
1793 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1794
Andrew Trickef9de2a2013-05-25 02:42:55 +00001795 SDLoc DL(Op);
Dan Gohman31ae5862010-04-17 14:41:14 +00001796 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
Mehdi Amini44ede332015-07-09 02:09:04 +00001797 getPointerTy(MF.getDataLayout()));
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +00001798
1799 // vastart just stores the address of the VarArgsFrameIndex slot into the
1800 // memory location argument.
1801 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001802 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
Justin Lebar9c375812016-07-15 18:27:10 +00001803 MachinePointerInfo(SV));
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +00001804}
Jia Liuf54f60f2012-02-28 07:46:26 +00001805
Daniel Sanders2b553d42014-08-01 09:17:39 +00001806SDValue MipsTargetLowering::lowerVAARG(SDValue Op, SelectionDAG &DAG) const {
1807 SDNode *Node = Op.getNode();
1808 EVT VT = Node->getValueType(0);
1809 SDValue Chain = Node->getOperand(0);
1810 SDValue VAListPtr = Node->getOperand(1);
1811 unsigned Align = Node->getConstantOperandVal(3);
1812 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
1813 SDLoc DL(Node);
Eric Christopher96e72c62015-01-29 23:27:36 +00001814 unsigned ArgSlotSizeInBytes = (ABI.IsN32() || ABI.IsN64()) ? 8 : 4;
Daniel Sanders2b553d42014-08-01 09:17:39 +00001815
Justin Lebar9c375812016-07-15 18:27:10 +00001816 SDValue VAListLoad = DAG.getLoad(getPointerTy(DAG.getDataLayout()), DL, Chain,
1817 VAListPtr, MachinePointerInfo(SV));
Daniel Sanders2b553d42014-08-01 09:17:39 +00001818 SDValue VAList = VAListLoad;
1819
1820 // Re-align the pointer if necessary.
1821 // It should only ever be necessary for 64-bit types on O32 since the minimum
1822 // argument alignment is the same as the maximum type alignment for N32/N64.
1823 //
1824 // FIXME: We currently align too often. The code generator doesn't notice
1825 // when the pointer is still aligned from the last va_arg (or pair of
1826 // va_args for the i64 on O32 case).
1827 if (Align > getMinStackArgumentAlignment()) {
1828 assert(((Align & (Align-1)) == 0) && "Expected Align to be a power of 2");
1829
1830 VAList = DAG.getNode(ISD::ADD, DL, VAList.getValueType(), VAList,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001831 DAG.getConstant(Align - 1, DL, VAList.getValueType()));
Daniel Sanders2b553d42014-08-01 09:17:39 +00001832
1833 VAList = DAG.getNode(ISD::AND, DL, VAList.getValueType(), VAList,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001834 DAG.getConstant(-(int64_t)Align, DL,
Daniel Sanders2b553d42014-08-01 09:17:39 +00001835 VAList.getValueType()));
1836 }
1837
1838 // Increment the pointer, VAList, to the next vaarg.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001839 auto &TD = DAG.getDataLayout();
1840 unsigned ArgSizeInBytes =
1841 TD.getTypeAllocSize(VT.getTypeForEVT(*DAG.getContext()));
Rui Ueyamada00f2f2016-01-14 21:06:47 +00001842 SDValue Tmp3 =
1843 DAG.getNode(ISD::ADD, DL, VAList.getValueType(), VAList,
1844 DAG.getConstant(alignTo(ArgSizeInBytes, ArgSlotSizeInBytes),
1845 DL, VAList.getValueType()));
Daniel Sanders2b553d42014-08-01 09:17:39 +00001846 // Store the incremented VAList to the legalized pointer
1847 Chain = DAG.getStore(VAListLoad.getValue(1), DL, Tmp3, VAListPtr,
Justin Lebar9c375812016-07-15 18:27:10 +00001848 MachinePointerInfo(SV));
Daniel Sanders2b553d42014-08-01 09:17:39 +00001849
1850 // In big-endian mode we must adjust the pointer when the load size is smaller
1851 // than the argument slot size. We must also reduce the known alignment to
1852 // match. For example in the N64 ABI, we must add 4 bytes to the offset to get
1853 // the correct half of the slot, and reduce the alignment from 8 (slot
1854 // alignment) down to 4 (type alignment).
1855 if (!Subtarget.isLittle() && ArgSizeInBytes < ArgSlotSizeInBytes) {
1856 unsigned Adjustment = ArgSlotSizeInBytes - ArgSizeInBytes;
1857 VAList = DAG.getNode(ISD::ADD, DL, VAListPtr.getValueType(), VAList,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001858 DAG.getIntPtrConstant(Adjustment, DL));
Daniel Sanders2b553d42014-08-01 09:17:39 +00001859 }
1860 // Load the actual argument out of the pointer VAList
Justin Lebar9c375812016-07-15 18:27:10 +00001861 return DAG.getLoad(VT, DL, Chain, VAList, MachinePointerInfo());
Daniel Sanders2b553d42014-08-01 09:17:39 +00001862}
1863
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001864static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG,
1865 bool HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001866 EVT TyX = Op.getOperand(0).getValueType();
1867 EVT TyY = Op.getOperand(1).getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001868 SDLoc DL(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001869 SDValue Const1 = DAG.getConstant(1, DL, MVT::i32);
1870 SDValue Const31 = DAG.getConstant(31, DL, MVT::i32);
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001871 SDValue Res;
1872
1873 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1874 // to i32.
1875 SDValue X = (TyX == MVT::f32) ?
1876 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1877 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1878 Const1);
1879 SDValue Y = (TyY == MVT::f32) ?
1880 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
1881 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
1882 Const1);
1883
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001884 if (HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001885 // ext E, Y, 31, 1 ; extract bit31 of Y
1886 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
1887 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
1888 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
1889 } else {
1890 // sll SllX, X, 1
1891 // srl SrlX, SllX, 1
1892 // srl SrlY, Y, 31
1893 // sll SllY, SrlX, 31
1894 // or Or, SrlX, SllY
1895 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1896 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1897 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
1898 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
1899 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
1900 }
1901
1902 if (TyX == MVT::f32)
1903 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
1904
1905 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001906 Op.getOperand(0),
1907 DAG.getConstant(0, DL, MVT::i32));
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001908 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001909}
1910
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001911static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG,
1912 bool HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001913 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
1914 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
1915 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001916 SDLoc DL(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001917 SDValue Const1 = DAG.getConstant(1, DL, MVT::i32);
Eric Christopher0713a9d2011-06-08 23:55:35 +00001918
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001919 // Bitcast to integer nodes.
1920 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
1921 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001922
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001923 if (HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001924 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
1925 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
1926 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001927 DAG.getConstant(WidthY - 1, DL, MVT::i32), Const1);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001928
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001929 if (WidthX > WidthY)
1930 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
1931 else if (WidthY > WidthX)
1932 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001933
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001934 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001935 DAG.getConstant(WidthX - 1, DL, MVT::i32), Const1,
1936 X);
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001937 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
1938 }
1939
1940 // (d)sll SllX, X, 1
1941 // (d)srl SrlX, SllX, 1
1942 // (d)srl SrlY, Y, width(Y)-1
1943 // (d)sll SllY, SrlX, width(Y)-1
1944 // or Or, SrlX, SllY
1945 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
1946 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
1947 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001948 DAG.getConstant(WidthY - 1, DL, MVT::i32));
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001949
1950 if (WidthX > WidthY)
1951 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
1952 else if (WidthY > WidthX)
1953 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
1954
1955 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001956 DAG.getConstant(WidthX - 1, DL, MVT::i32));
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001957 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
1958 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001959}
1960
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00001961SDValue
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001962MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00001963 if (Subtarget.isGP64bit())
1964 return lowerFCOPYSIGN64(Op, DAG, Subtarget.hasExtractInsert());
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001965
Eric Christopher1c29a652014-07-18 22:55:25 +00001966 return lowerFCOPYSIGN32(Op, DAG, Subtarget.hasExtractInsert());
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001967}
1968
Akira Hatanaka66277522011-06-02 00:24:44 +00001969SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001970lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes5444a7b2011-06-16 00:40:02 +00001971 // check the depth
1972 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka15506782011-06-07 18:58:42 +00001973 "Frame address can only be determined for current frame.");
Akira Hatanaka66277522011-06-02 00:24:44 +00001974
Matthias Braun941a7052016-07-28 18:40:00 +00001975 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
1976 MFI.setFrameAddressIsTaken(true);
Akira Hatanaka66277522011-06-02 00:24:44 +00001977 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001978 SDLoc DL(Op);
Eric Christopher96e72c62015-01-29 23:27:36 +00001979 SDValue FrameAddr = DAG.getCopyFromReg(
1980 DAG.getEntryNode(), DL, ABI.IsN64() ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka66277522011-06-02 00:24:44 +00001981 return FrameAddr;
1982}
1983
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001984SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op,
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00001985 SelectionDAG &DAG) const {
Bill Wendling908bf812014-01-06 00:43:20 +00001986 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001987 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001988
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00001989 // check the depth
1990 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
1991 "Return address can be determined only for current frame.");
1992
1993 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00001994 MachineFrameInfo &MFI = MF.getFrameInfo();
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001995 MVT VT = Op.getSimpleValueType();
Eric Christopher96e72c62015-01-29 23:27:36 +00001996 unsigned RA = ABI.IsN64() ? Mips::RA_64 : Mips::RA;
Matthias Braun941a7052016-07-28 18:40:00 +00001997 MFI.setReturnAddressIsTaken(true);
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00001998
1999 // Return RA, which contains the return address. Mark it an implicit live-in.
2000 unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00002001 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT);
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002002}
2003
Akira Hatanakac0b02062013-01-30 00:26:49 +00002004// An EH_RETURN is the result of lowering llvm.eh.return which in turn is
2005// generated from __builtin_eh_return (offset, handler)
2006// The effect of this is to adjust the stack pointer by "offset"
2007// and then branch to "handler".
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002008SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Akira Hatanakac0b02062013-01-30 00:26:49 +00002009 const {
2010 MachineFunction &MF = DAG.getMachineFunction();
2011 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2012
2013 MipsFI->setCallsEhReturn();
2014 SDValue Chain = Op.getOperand(0);
2015 SDValue Offset = Op.getOperand(1);
2016 SDValue Handler = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002017 SDLoc DL(Op);
Eric Christopher96e72c62015-01-29 23:27:36 +00002018 EVT Ty = ABI.IsN64() ? MVT::i64 : MVT::i32;
Akira Hatanakac0b02062013-01-30 00:26:49 +00002019
2020 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
2021 // EH_RETURN nodes, so that instructions are emitted back-to-back.
Eric Christopher96e72c62015-01-29 23:27:36 +00002022 unsigned OffsetReg = ABI.IsN64() ? Mips::V1_64 : Mips::V1;
2023 unsigned AddrReg = ABI.IsN64() ? Mips::V0_64 : Mips::V0;
Akira Hatanakac0b02062013-01-30 00:26:49 +00002024 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
2025 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
2026 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
2027 DAG.getRegister(OffsetReg, Ty),
Mehdi Amini44ede332015-07-09 02:09:04 +00002028 DAG.getRegister(AddrReg, getPointerTy(MF.getDataLayout())),
Akira Hatanakac0b02062013-01-30 00:26:49 +00002029 Chain.getValue(1));
2030}
2031
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002032SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
Akira Hatanaka5fd22482012-06-14 21:10:56 +00002033 SelectionDAG &DAG) const {
Eli Friedman26a48482011-07-27 22:21:52 +00002034 // FIXME: Need pseudo-fence for 'singlethread' fences
2035 // FIXME: Set SType for weaker fences where supported/appropriate.
2036 unsigned SType = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002037 SDLoc DL(Op);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002038 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002039 DAG.getConstant(SType, DL, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00002040}
2041
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002042SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
Akira Hatanaka5fd22482012-06-14 21:10:56 +00002043 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002044 SDLoc DL(Op);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002045 MVT VT = Subtarget.isGP64bit() ? MVT::i64 : MVT::i32;
2046
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002047 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2048 SDValue Shamt = Op.getOperand(2);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002049 // if shamt < (VT.bits):
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002050 // lo = (shl lo, shamt)
2051 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
2052 // else:
2053 // lo = 0
2054 // hi = (shl lo, shamt[4:0])
2055 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002056 DAG.getConstant(-1, DL, MVT::i32));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002057 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, VT, Lo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002058 DAG.getConstant(1, DL, VT));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002059 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, VT, ShiftRight1Lo, Not);
2060 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, VT, Hi, Shamt);
2061 SDValue Or = DAG.getNode(ISD::OR, DL, VT, ShiftLeftHi, ShiftRightLo);
2062 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, VT, Lo, Shamt);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002063 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
Daniel Sanders301f9372015-04-29 12:28:58 +00002064 DAG.getConstant(VT.getSizeInBits(), DL, MVT::i32));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002065 Lo = DAG.getNode(ISD::SELECT, DL, VT, Cond,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002066 DAG.getConstant(0, DL, VT), ShiftLeftLo);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002067 Hi = DAG.getNode(ISD::SELECT, DL, VT, Cond, ShiftLeftLo, Or);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002068
2069 SDValue Ops[2] = {Lo, Hi};
Craig Topper64941d92014-04-27 19:20:57 +00002070 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002071}
2072
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002073SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002074 bool IsSRA) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002075 SDLoc DL(Op);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002076 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2077 SDValue Shamt = Op.getOperand(2);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002078 MVT VT = Subtarget.isGP64bit() ? MVT::i64 : MVT::i32;
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002079
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002080 // if shamt < (VT.bits):
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002081 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
2082 // if isSRA:
2083 // hi = (sra hi, shamt)
2084 // else:
2085 // hi = (srl hi, shamt)
2086 // else:
2087 // if isSRA:
2088 // lo = (sra hi, shamt[4:0])
2089 // hi = (sra hi, 31)
2090 // else:
2091 // lo = (srl hi, shamt[4:0])
2092 // hi = 0
2093 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002094 DAG.getConstant(-1, DL, MVT::i32));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002095 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, VT, Hi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002096 DAG.getConstant(1, DL, VT));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002097 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, VT, ShiftLeft1Hi, Not);
2098 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, VT, Lo, Shamt);
2099 SDValue Or = DAG.getNode(ISD::OR, DL, VT, ShiftLeftHi, ShiftRightLo);
2100 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL,
2101 DL, VT, Hi, Shamt);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002102 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
Daniel Sanders301f9372015-04-29 12:28:58 +00002103 DAG.getConstant(VT.getSizeInBits(), DL, MVT::i32));
2104 SDValue Ext = DAG.getNode(ISD::SRA, DL, VT, Hi,
2105 DAG.getConstant(VT.getSizeInBits() - 1, DL, VT));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002106 Lo = DAG.getNode(ISD::SELECT, DL, VT, Cond, ShiftRightHi, Or);
2107 Hi = DAG.getNode(ISD::SELECT, DL, VT, Cond,
Daniel Sanders301f9372015-04-29 12:28:58 +00002108 IsSRA ? Ext : DAG.getConstant(0, DL, VT), ShiftRightHi);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002109
2110 SDValue Ops[2] = {Lo, Hi};
Craig Topper64941d92014-04-27 19:20:57 +00002111 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002112}
2113
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002114static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002115 SDValue Chain, SDValue Src, unsigned Offset) {
Akira Hatanaka95866182012-06-13 19:06:08 +00002116 SDValue Ptr = LD->getBasePtr();
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002117 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
Akira Hatanaka95866182012-06-13 19:06:08 +00002118 EVT BasePtrVT = Ptr.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002119 SDLoc DL(LD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002120 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
2121
2122 if (Offset)
Akira Hatanaka95866182012-06-13 19:06:08 +00002123 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002124 DAG.getConstant(Offset, DL, BasePtrVT));
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002125
2126 SDValue Ops[] = { Chain, Ptr, Src };
Craig Topper206fcd42014-04-26 19:29:41 +00002127 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002128 LD->getMemOperand());
2129}
2130
2131// Expand an unaligned 32 or 64-bit integer load node.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002132SDValue MipsTargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002133 LoadSDNode *LD = cast<LoadSDNode>(Op);
2134 EVT MemVT = LD->getMemoryVT();
2135
Eric Christopher1c29a652014-07-18 22:55:25 +00002136 if (Subtarget.systemSupportsUnalignedAccess())
Daniel Sandersac272632014-05-23 13:18:02 +00002137 return Op;
2138
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002139 // Return if load is aligned or if MemVT is neither i32 nor i64.
2140 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
2141 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
2142 return SDValue();
2143
Eric Christopher1c29a652014-07-18 22:55:25 +00002144 bool IsLittle = Subtarget.isLittle();
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002145 EVT VT = Op.getValueType();
2146 ISD::LoadExtType ExtType = LD->getExtensionType();
2147 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
2148
2149 assert((VT == MVT::i32) || (VT == MVT::i64));
2150
2151 // Expand
2152 // (set dst, (i64 (load baseptr)))
2153 // to
2154 // (set tmp, (ldl (add baseptr, 7), undef))
2155 // (set dst, (ldr baseptr, tmp))
2156 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002157 SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002158 IsLittle ? 7 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002159 return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002160 IsLittle ? 0 : 7);
2161 }
2162
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002163 SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002164 IsLittle ? 3 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002165 SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002166 IsLittle ? 0 : 3);
2167
2168 // Expand
2169 // (set dst, (i32 (load baseptr))) or
2170 // (set dst, (i64 (sextload baseptr))) or
2171 // (set dst, (i64 (extload baseptr)))
2172 // to
2173 // (set tmp, (lwl (add baseptr, 3), undef))
2174 // (set dst, (lwr baseptr, tmp))
2175 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
2176 (ExtType == ISD::EXTLOAD))
2177 return LWR;
2178
2179 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
2180
2181 // Expand
2182 // (set dst, (i64 (zextload baseptr)))
2183 // to
2184 // (set tmp0, (lwl (add baseptr, 3), undef))
2185 // (set tmp1, (lwr baseptr, tmp0))
2186 // (set tmp2, (shl tmp1, 32))
2187 // (set dst, (srl tmp2, 32))
Andrew Trickef9de2a2013-05-25 02:42:55 +00002188 SDLoc DL(LD);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002189 SDValue Const32 = DAG.getConstant(32, DL, MVT::i32);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002190 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
Akira Hatanaka67346852012-06-04 17:46:29 +00002191 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
2192 SDValue Ops[] = { SRL, LWR.getValue(1) };
Craig Topper64941d92014-04-27 19:20:57 +00002193 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002194}
2195
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002196static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002197 SDValue Chain, unsigned Offset) {
Akira Hatanaka95866182012-06-13 19:06:08 +00002198 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
2199 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002200 SDLoc DL(SD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002201 SDVTList VTList = DAG.getVTList(MVT::Other);
2202
2203 if (Offset)
Akira Hatanaka95866182012-06-13 19:06:08 +00002204 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002205 DAG.getConstant(Offset, DL, BasePtrVT));
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002206
2207 SDValue Ops[] = { Chain, Value, Ptr };
Craig Topper206fcd42014-04-26 19:29:41 +00002208 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002209 SD->getMemOperand());
2210}
2211
2212// Expand an unaligned 32 or 64-bit integer store node.
Akira Hatanakad82ee942013-05-16 20:45:17 +00002213static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG,
2214 bool IsLittle) {
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002215 SDValue Value = SD->getValue(), Chain = SD->getChain();
2216 EVT VT = Value.getValueType();
2217
2218 // Expand
2219 // (store val, baseptr) or
2220 // (truncstore val, baseptr)
2221 // to
2222 // (swl val, (add baseptr, 3))
2223 // (swr val, baseptr)
2224 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002225 SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002226 IsLittle ? 3 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002227 return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002228 }
2229
2230 assert(VT == MVT::i64);
2231
2232 // Expand
2233 // (store val, baseptr)
2234 // to
2235 // (sdl val, (add baseptr, 7))
2236 // (sdr val, baseptr)
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002237 SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2238 return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002239}
2240
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002241// Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr).
2242static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG) {
2243 SDValue Val = SD->getValue();
2244
2245 if (Val.getOpcode() != ISD::FP_TO_SINT)
2246 return SDValue();
2247
2248 EVT FPTy = EVT::getFloatingPointVT(Val.getValueSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002249 SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy,
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002250 Val.getOperand(0));
Andrew Trickef9de2a2013-05-25 02:42:55 +00002251 return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(),
Justin Lebar9c375812016-07-15 18:27:10 +00002252 SD->getPointerInfo(), SD->getAlignment(),
2253 SD->getMemOperand()->getFlags());
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002254}
2255
Akira Hatanakad82ee942013-05-16 20:45:17 +00002256SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2257 StoreSDNode *SD = cast<StoreSDNode>(Op);
2258 EVT MemVT = SD->getMemoryVT();
2259
2260 // Lower unaligned integer stores.
Eric Christopher1c29a652014-07-18 22:55:25 +00002261 if (!Subtarget.systemSupportsUnalignedAccess() &&
Daniel Sandersac272632014-05-23 13:18:02 +00002262 (SD->getAlignment() < MemVT.getSizeInBits() / 8) &&
Akira Hatanakad82ee942013-05-16 20:45:17 +00002263 ((MemVT == MVT::i32) || (MemVT == MVT::i64)))
Eric Christopher1c29a652014-07-18 22:55:25 +00002264 return lowerUnalignedIntStore(SD, DAG, Subtarget.isLittle());
Akira Hatanakad82ee942013-05-16 20:45:17 +00002265
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002266 return lowerFP_TO_SINT_STORE(SD, DAG);
Akira Hatanakad82ee942013-05-16 20:45:17 +00002267}
2268
Hal Finkel5081ac22016-09-01 10:28:47 +00002269SDValue MipsTargetLowering::lowerEH_DWARF_CFA(SDValue Op,
2270 SelectionDAG &DAG) const {
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002271
Hal Finkel5081ac22016-09-01 10:28:47 +00002272 // Return a fixed StackObject with offset 0 which points to the old stack
2273 // pointer.
Matthias Braun941a7052016-07-28 18:40:00 +00002274 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002275 EVT ValTy = Op->getValueType(0);
Matthias Braun941a7052016-07-28 18:40:00 +00002276 int FI = MFI.CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
Hal Finkel5081ac22016-09-01 10:28:47 +00002277 return DAG.getFrameIndex(FI, ValTy);
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002278}
2279
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002280SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op,
2281 SelectionDAG &DAG) const {
2282 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002283 SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy,
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002284 Op.getOperand(0));
Andrew Trickef9de2a2013-05-25 02:42:55 +00002285 return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc);
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002286}
2287
Akira Hatanakae2489122011-04-15 21:51:11 +00002288//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002289// Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002290//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002291
Akira Hatanakae2489122011-04-15 21:51:11 +00002292//===----------------------------------------------------------------------===//
Wesley Peck527da1b2010-11-23 03:31:01 +00002293// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002294// Mips O32 ABI rules:
2295// ---
2296// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peck527da1b2010-11-23 03:31:01 +00002297// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002298// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peck527da1b2010-11-23 03:31:01 +00002299// f64 - Only passed in two aliased f32 registers if no int reg has been used
2300// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Sylvestre Ledru469de192014-08-11 18:04:46 +00002301// not used, it must be shadowed. If only A3 is available, shadow it and
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002302// go to stack.
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002303//
2304// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanakae2489122011-04-15 21:51:11 +00002305//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002306
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00002307static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2308 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002309 CCState &State, ArrayRef<MCPhysReg> F64Regs) {
Eric Christopher96e72c62015-01-29 23:27:36 +00002310 const MipsSubtarget &Subtarget = static_cast<const MipsSubtarget &>(
2311 State.getMachineFunction().getSubtarget());
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002312
Craig Topper840beec2014-04-04 05:16:06 +00002313 static const MCPhysReg IntRegs[] = { Mips::A0, Mips::A1, Mips::A2, Mips::A3 };
2314 static const MCPhysReg F32Regs[] = { Mips::F12, Mips::F14 };
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002315
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002316 // Do not process byval args here.
2317 if (ArgFlags.isByVal())
2318 return true;
Akira Hatanaka5e16c6a2011-05-24 19:18:33 +00002319
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002320 // Promote i8 and i16
Daniel Sandersd134c9d2014-12-02 20:40:27 +00002321 if (ArgFlags.isInReg() && !Subtarget.isLittle()) {
2322 if (LocVT == MVT::i8 || LocVT == MVT::i16 || LocVT == MVT::i32) {
2323 LocVT = MVT::i32;
2324 if (ArgFlags.isSExt())
2325 LocInfo = CCValAssign::SExtUpper;
2326 else if (ArgFlags.isZExt())
2327 LocInfo = CCValAssign::ZExtUpper;
2328 else
2329 LocInfo = CCValAssign::AExtUpper;
2330 }
2331 }
2332
2333 // Promote i8 and i16
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002334 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
2335 LocVT = MVT::i32;
2336 if (ArgFlags.isSExt())
2337 LocInfo = CCValAssign::SExt;
2338 else if (ArgFlags.isZExt())
2339 LocInfo = CCValAssign::ZExt;
2340 else
2341 LocInfo = CCValAssign::AExt;
2342 }
2343
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002344 unsigned Reg;
2345
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002346 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
2347 // is true: function is vararg, argument is 3rd or higher, there is previous
2348 // argument which is not f32 or f64.
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002349 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1 ||
2350 State.getFirstUnallocated(F32Regs) != ValNo;
Akira Hatanaka9e6a8cc2011-05-19 20:29:48 +00002351 unsigned OrigAlign = ArgFlags.getOrigAlign();
2352 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002353
2354 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002355 Reg = State.AllocateReg(IntRegs);
Akira Hatanaka9e6a8cc2011-05-19 20:29:48 +00002356 // If this is the first part of an i64 arg,
2357 // the allocated register must be either A0 or A2.
2358 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002359 Reg = State.AllocateReg(IntRegs);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002360 LocVT = MVT::i32;
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002361 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
2362 // Allocate int register and shadow next int register. If first
2363 // available register is Mips::A1 or Mips::A3, shadow it too.
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002364 Reg = State.AllocateReg(IntRegs);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002365 if (Reg == Mips::A1 || Reg == Mips::A3)
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002366 Reg = State.AllocateReg(IntRegs);
2367 State.AllocateReg(IntRegs);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002368 LocVT = MVT::i32;
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002369 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
2370 // we are guaranteed to find an available float register
2371 if (ValVT == MVT::f32) {
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002372 Reg = State.AllocateReg(F32Regs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002373 // Shadow int register
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002374 State.AllocateReg(IntRegs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002375 } else {
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002376 Reg = State.AllocateReg(F64Regs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002377 // Shadow int registers
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002378 unsigned Reg2 = State.AllocateReg(IntRegs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002379 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002380 State.AllocateReg(IntRegs);
2381 State.AllocateReg(IntRegs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002382 }
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002383 } else
2384 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002385
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002386 if (!Reg) {
2387 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3,
2388 OrigAlign);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002389 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002390 } else
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002391 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002392
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002393 return false;
Akira Hatanaka202f6402011-11-12 02:20:46 +00002394}
2395
Akira Hatanakabfb66242013-08-20 23:38:40 +00002396static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT,
2397 MVT LocVT, CCValAssign::LocInfo LocInfo,
2398 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002399 static const MCPhysReg F64Regs[] = { Mips::D6, Mips::D7 };
Akira Hatanakabfb66242013-08-20 23:38:40 +00002400
2401 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2402}
2403
2404static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT,
2405 MVT LocVT, CCValAssign::LocInfo LocInfo,
2406 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002407 static const MCPhysReg F64Regs[] = { Mips::D12_64, Mips::D14_64 };
Akira Hatanakabfb66242013-08-20 23:38:40 +00002408
2409 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2410}
2411
Reid Klecknerd3781742014-11-14 00:39:33 +00002412static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2413 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
2414 CCState &State) LLVM_ATTRIBUTE_UNUSED;
Reed Kotlerd5c41962014-11-13 23:37:45 +00002415
Akira Hatanaka202f6402011-11-12 02:20:46 +00002416#include "MipsGenCallingConv.inc"
2417
Akira Hatanakae2489122011-04-15 21:51:11 +00002418//===----------------------------------------------------------------------===//
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002419// Call Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002420//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002421
Akira Hatanaka61bbcce2011-09-23 00:58:33 +00002422// Return next O32 integer argument register.
2423static unsigned getNextIntArgReg(unsigned Reg) {
2424 assert((Reg == Mips::A0) || (Reg == Mips::A2));
2425 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
2426}
2427
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002428SDValue MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
2429 SDValue Chain, SDValue Arg,
2430 const SDLoc &DL, bool IsTailCall,
2431 SelectionDAG &DAG) const {
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002432 if (!IsTailCall) {
Mehdi Amini44ede332015-07-09 02:09:04 +00002433 SDValue PtrOff =
2434 DAG.getNode(ISD::ADD, DL, getPointerTy(DAG.getDataLayout()), StackPtr,
2435 DAG.getIntPtrConstant(Offset, DL));
Justin Lebar9c375812016-07-15 18:27:10 +00002436 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo());
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002437 }
2438
Matthias Braun941a7052016-07-28 18:40:00 +00002439 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
2440 int FI = MFI.CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00002441 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002442 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(),
Justin Lebar9c375812016-07-15 18:27:10 +00002443 /* Alignment = */ 0, MachineMemOperand::MOVolatile);
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002444}
2445
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002446void MipsTargetLowering::
2447getOpndList(SmallVectorImpl<SDValue> &Ops,
2448 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
2449 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
Sasa Stankovic7072a792014-10-01 08:22:21 +00002450 bool IsCallReloc, CallLoweringInfo &CLI, SDValue Callee,
2451 SDValue Chain) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002452 // Insert node "GP copy globalreg" before call to function.
2453 //
2454 // R_MIPS_CALL* operators (emitted when non-internal functions are called
2455 // in PIC mode) allow symbols to be resolved via lazy binding.
2456 // The lazy binding stub requires GP to point to the GOT.
Sasa Stankovic7072a792014-10-01 08:22:21 +00002457 // Note that we don't need GP to point to the GOT for indirect calls
2458 // (when R_MIPS_CALL* is not used for the call) because Mips linker generates
2459 // lazy binding stub for a function only when R_MIPS_CALL* are the only relocs
2460 // used for the function (that is, Mips linker doesn't generate lazy binding
2461 // stub for a function whose address is taken in the program).
2462 if (IsPICCall && !InternalLinkage && IsCallReloc) {
Eric Christopher96e72c62015-01-29 23:27:36 +00002463 unsigned GPReg = ABI.IsN64() ? Mips::GP_64 : Mips::GP;
2464 EVT Ty = ABI.IsN64() ? MVT::i64 : MVT::i32;
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002465 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty)));
2466 }
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002467
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002468 // Build a sequence of copy-to-reg nodes chained together with token
2469 // chain and flag operands which copy the outgoing args into registers.
2470 // The InFlag in necessary since all emitted instructions must be
2471 // stuck together.
2472 SDValue InFlag;
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002473
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002474 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2475 Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, RegsToPass[i].first,
2476 RegsToPass[i].second, InFlag);
2477 InFlag = Chain.getValue(1);
2478 }
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002479
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002480 // Add argument registers to the end of the list so that they are
2481 // known live into the call.
2482 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2483 Ops.push_back(CLI.DAG.getRegister(RegsToPass[i].first,
2484 RegsToPass[i].second.getValueType()));
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002485
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002486 // Add a register mask operand representing the call-preserved registers.
Eric Christopher96e72c62015-01-29 23:27:36 +00002487 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00002488 const uint32_t *Mask =
2489 TRI->getCallPreservedMask(CLI.DAG.getMachineFunction(), CLI.CallConv);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002490 assert(Mask && "Missing call preserved mask for calling convention");
Eric Christopher1c29a652014-07-18 22:55:25 +00002491 if (Subtarget.inMips16HardFloat()) {
Reed Kotler783c7942013-05-10 22:25:39 +00002492 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
2493 llvm::StringRef Sym = G->getGlobal()->getName();
2494 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
Reed Kotler3230e722013-12-12 02:41:11 +00002495 if (F && F->hasFnAttribute("__Mips16RetHelper")) {
Reed Kotler783c7942013-05-10 22:25:39 +00002496 Mask = MipsRegisterInfo::getMips16RetHelperMask();
2497 }
2498 }
2499 }
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002500 Ops.push_back(CLI.DAG.getRegisterMask(Mask));
2501
2502 if (InFlag.getNode())
2503 Ops.push_back(InFlag);
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002504}
2505
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002506/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman624801e2009-01-26 03:15:54 +00002507/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002508SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00002509MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002510 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00002511 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002512 SDLoc DL = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00002513 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
2514 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
2515 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Akira Hatanakabeda2242012-07-31 18:46:41 +00002516 SDValue Chain = CLI.Chain;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002517 SDValue Callee = CLI.Callee;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002518 bool &IsTailCall = CLI.IsTailCall;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002519 CallingConv::ID CallConv = CLI.CallConv;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002520 bool IsVarArg = CLI.IsVarArg;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002521
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002522 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00002523 MachineFrameInfo &MFI = MF.getFrameInfo();
Eric Christopher96e72c62015-01-29 23:27:36 +00002524 const TargetFrameLowering *TFL = Subtarget.getFrameLowering();
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002525 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
Rafael Espindola9f1c1fe2016-06-27 12:48:21 +00002526 bool IsPIC = isPositionIndependent();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002527
2528 // Analyze operands of the call, assigning locations to each operand.
2529 SmallVector<CCValAssign, 16> ArgLocs;
Daniel Sanders41a64c42014-11-07 11:10:48 +00002530 MipsCCState CCInfo(
2531 CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs, *DAG.getContext(),
2532 MipsCCState::getSpecialCallingConvForCallee(Callee.getNode(), Subtarget));
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002533
2534 // Allocate the reserved argument area. It seems strange to do this from the
2535 // caller side but removing it breaks the frame size calculation.
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002536 CCInfo.AllocateStack(ABI.GetCalleeAllocdArgSizeInBytes(CallConv), 1);
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002537
Daniel Sanderscfad1e32014-11-07 11:43:49 +00002538 CCInfo.AnalyzeCallOperands(Outs, CC_Mips, CLI.getArgs(), Callee.getNode());
Wesley Peck527da1b2010-11-23 03:31:01 +00002539
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002540 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka195a1e22011-06-08 17:39:33 +00002541 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka97ba7692012-07-26 23:27:01 +00002542
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002543 // Check if it's really possible to do a tail call.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002544 if (IsTailCall)
Daniel Sanders23e98772014-11-02 16:09:29 +00002545 IsTailCall = isEligibleForTailCallOptimization(
2546 CCInfo, NextStackOffset, *MF.getInfo<MipsFunctionInfo>());
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002547
Reid Kleckner5772b772014-04-24 20:14:34 +00002548 if (!IsTailCall && CLI.CS && CLI.CS->isMustTailCall())
2549 report_fatal_error("failed to perform tail call elimination on a call "
2550 "site marked musttail");
2551
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002552 if (IsTailCall)
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002553 ++NumTailCalls;
2554
Akira Hatanaka79738332011-09-19 20:26:02 +00002555 // Chain is the output chain of the last Load/Store or CopyToReg node.
2556 // ByValChain is the output chain of the last Memcpy node created for copying
2557 // byval arguments to the stack.
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002558 unsigned StackAlignment = TFL->getStackAlignment();
Rui Ueyamada00f2f2016-01-14 21:06:47 +00002559 NextStackOffset = alignTo(NextStackOffset, StackAlignment);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002560 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, DL, true);
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002561
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002562 if (!IsTailCall)
Andrew Trickad6d08a2013-05-29 22:03:55 +00002563 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffsetVal, DL);
Akira Hatanakabeda2242012-07-31 18:46:41 +00002564
Mehdi Amini44ede332015-07-09 02:09:04 +00002565 SDValue StackPtr =
2566 DAG.getCopyFromReg(Chain, DL, ABI.IsN64() ? Mips::SP_64 : Mips::SP,
2567 getPointerTy(DAG.getDataLayout()));
Akira Hatanaka195a1e22011-06-08 17:39:33 +00002568
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002569 std::deque< std::pair<unsigned, SDValue> > RegsToPass;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002570 SmallVector<SDValue, 8> MemOpChains;
Daniel Sanders23e98772014-11-02 16:09:29 +00002571
2572 CCInfo.rewindByValRegsInfo();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002573
2574 // Walk the register/memloc assignments, inserting copies/loads.
2575 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002576 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002577 CCValAssign &VA = ArgLocs[i];
Akira Hatanakab20a3252011-10-28 19:49:00 +00002578 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka19891f82011-11-12 02:34:50 +00002579 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002580 bool UseUpperBits = false;
Akira Hatanaka19891f82011-11-12 02:34:50 +00002581
2582 // ByVal Arg.
2583 if (Flags.isByVal()) {
Daniel Sanders23e98772014-11-02 16:09:29 +00002584 unsigned FirstByValReg, LastByValReg;
2585 unsigned ByValIdx = CCInfo.getInRegsParamsProcessed();
2586 CCInfo.getInRegsParamInfo(ByValIdx, FirstByValReg, LastByValReg);
2587
Akira Hatanaka19891f82011-11-12 02:34:50 +00002588 assert(Flags.getByValSize() &&
2589 "ByVal args of size 0 should have been ignored by front-end.");
Daniel Sanders23e98772014-11-02 16:09:29 +00002590 assert(ByValIdx < CCInfo.getInRegsParamsCount());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002591 assert(!IsTailCall &&
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002592 "Do not tail-call optimize if there is a byval argument.");
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002593 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002594 FirstByValReg, LastByValReg, Flags, Subtarget.isLittle(),
2595 VA);
Daniel Sanders23e98772014-11-02 16:09:29 +00002596 CCInfo.nextInRegsParam();
Akira Hatanaka19891f82011-11-12 02:34:50 +00002597 continue;
2598 }
Jia Liuf54f60f2012-02-28 07:46:26 +00002599
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002600 // Promote the value if needed.
2601 switch (VA.getLocInfo()) {
Daniel Sandersc43cda82014-11-07 16:54:21 +00002602 default:
2603 llvm_unreachable("Unknown loc info!");
Wesley Peck527da1b2010-11-23 03:31:01 +00002604 case CCValAssign::Full:
Akira Hatanakab20a3252011-10-28 19:49:00 +00002605 if (VA.isRegLoc()) {
2606 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
Akira Hatanaka3b7391d2013-03-05 22:20:28 +00002607 (ValVT == MVT::f64 && LocVT == MVT::i64) ||
2608 (ValVT == MVT::i64 && LocVT == MVT::f64))
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002609 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
Akira Hatanakab20a3252011-10-28 19:49:00 +00002610 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002611 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002612 Arg, DAG.getConstant(0, DL, MVT::i32));
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002613 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002614 Arg, DAG.getConstant(1, DL, MVT::i32));
Eric Christopher1c29a652014-07-18 22:55:25 +00002615 if (!Subtarget.isLittle())
Akira Hatanaka27916972011-04-15 19:52:08 +00002616 std::swap(Lo, Hi);
Jia Liuf54f60f2012-02-28 07:46:26 +00002617 unsigned LocRegLo = VA.getLocReg();
Akira Hatanaka61bbcce2011-09-23 00:58:33 +00002618 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2619 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2620 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002621 continue;
Wesley Peck527da1b2010-11-23 03:31:01 +00002622 }
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002623 }
2624 break;
Daniel Sanders23e98772014-11-02 16:09:29 +00002625 case CCValAssign::BCvt:
2626 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
2627 break;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002628 case CCValAssign::SExtUpper:
2629 UseUpperBits = true;
Justin Bognerb03fd122016-08-17 05:10:15 +00002630 LLVM_FALLTHROUGH;
Chris Lattner52f16de2008-03-17 06:57:02 +00002631 case CCValAssign::SExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002632 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002633 break;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002634 case CCValAssign::ZExtUpper:
2635 UseUpperBits = true;
Justin Bognerb03fd122016-08-17 05:10:15 +00002636 LLVM_FALLTHROUGH;
Chris Lattner52f16de2008-03-17 06:57:02 +00002637 case CCValAssign::ZExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002638 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002639 break;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002640 case CCValAssign::AExtUpper:
2641 UseUpperBits = true;
Justin Bognerb03fd122016-08-17 05:10:15 +00002642 LLVM_FALLTHROUGH;
Chris Lattner52f16de2008-03-17 06:57:02 +00002643 case CCValAssign::AExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002644 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002645 break;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002646 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002647
Daniel Sandersc43cda82014-11-07 16:54:21 +00002648 if (UseUpperBits) {
2649 unsigned ValSizeInBits = Outs[i].ArgVT.getSizeInBits();
2650 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
2651 Arg = DAG.getNode(
2652 ISD::SHL, DL, VA.getLocVT(), Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002653 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
Daniel Sandersc43cda82014-11-07 16:54:21 +00002654 }
2655
Wesley Peck527da1b2010-11-23 03:31:01 +00002656 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +00002657 // RegsToPass vector
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002658 if (VA.isRegLoc()) {
2659 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattner52f16de2008-03-17 06:57:02 +00002660 continue;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002661 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002662
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002663 // Register can't get to this point...
Chris Lattner52f16de2008-03-17 06:57:02 +00002664 assert(VA.isMemLoc());
Wesley Peck527da1b2010-11-23 03:31:01 +00002665
Wesley Peck527da1b2010-11-23 03:31:01 +00002666 // emit ISD::STORE whichs stores the
Chris Lattner52f16de2008-03-17 06:57:02 +00002667 // parameter value to a stack Location
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002668 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002669 Chain, Arg, DL, IsTailCall, DAG));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002670 }
2671
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002672 // Transform all store nodes into one single node because all store
2673 // nodes are independent of each other.
Wesley Peck527da1b2010-11-23 03:31:01 +00002674 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00002675 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002676
Bill Wendling24c79f22008-09-16 21:48:12 +00002677 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peck527da1b2010-11-23 03:31:01 +00002678 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2679 // node so that legalize doesn't hack it.
Eric Christopher96e72c62015-01-29 23:27:36 +00002680 bool IsPICCall = (ABI.IsN64() || IsPIC); // true if calls are translated to
2681 // jalr $25
Sasa Stankovic7072a792014-10-01 08:22:21 +00002682 bool GlobalOrExternal = false, InternalLinkage = false, IsCallReloc = false;
Akira Hatanakad6f1c582011-04-07 19:51:44 +00002683 SDValue CalleeLo;
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002684 EVT Ty = Callee.getValueType();
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002685
2686 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002687 if (IsPICCall) {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002688 const GlobalValue *Val = G->getGlobal();
2689 InternalLinkage = Val->hasInternalLinkage();
Akira Hatanakacf9a61b2012-12-13 03:17:29 +00002690
2691 if (InternalLinkage)
Eric Christopher96e72c62015-01-29 23:27:36 +00002692 Callee = getAddrLocal(G, DL, Ty, DAG, ABI.IsN32() || ABI.IsN64());
Sasa Stankovic7072a792014-10-01 08:22:21 +00002693 else if (LargeGOT) {
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002694 Callee = getAddrGlobalLargeGOT(G, DL, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002695 MipsII::MO_CALL_LO16, Chain,
2696 FuncInfo->callPtrInfo(Val));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002697 IsCallReloc = true;
2698 } else {
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002699 Callee = getAddrGlobal(G, DL, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002700 FuncInfo->callPtrInfo(Val));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002701 IsCallReloc = true;
2702 }
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002703 } else
Mehdi Amini44ede332015-07-09 02:09:04 +00002704 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL,
2705 getPointerTy(DAG.getDataLayout()), 0,
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002706 MipsII::MO_NO_FLAG);
Akira Hatanaka8e16aac2011-12-09 01:45:12 +00002707 GlobalOrExternal = true;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002708 }
2709 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002710 const char *Sym = S->getSymbol();
2711
Eric Christopher96e72c62015-01-29 23:27:36 +00002712 if (!ABI.IsN64() && !IsPIC) // !N64 && static
Mehdi Amini44ede332015-07-09 02:09:04 +00002713 Callee = DAG.getTargetExternalSymbol(
2714 Sym, getPointerTy(DAG.getDataLayout()), MipsII::MO_NO_FLAG);
Sasa Stankovic7072a792014-10-01 08:22:21 +00002715 else if (LargeGOT) {
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002716 Callee = getAddrGlobalLargeGOT(S, DL, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002717 MipsII::MO_CALL_LO16, Chain,
2718 FuncInfo->callPtrInfo(Sym));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002719 IsCallReloc = true;
2720 } else { // N64 || PIC
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002721 Callee = getAddrGlobal(S, DL, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002722 FuncInfo->callPtrInfo(Sym));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002723 IsCallReloc = true;
2724 }
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002725
Akira Hatanaka8e16aac2011-12-09 01:45:12 +00002726 GlobalOrExternal = true;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002727 }
2728
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002729 SmallVector<SDValue, 8> Ops(1, Chain);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002730 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002731
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002732 getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal, InternalLinkage,
Sasa Stankovic7072a792014-10-01 08:22:21 +00002733 IsCallReloc, CLI, Callee, Chain);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002734
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002735 if (IsTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +00002736 return DAG.getNode(MipsISD::TailCall, DL, MVT::Other, Ops);
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002737
Craig Topper48d114b2014-04-26 18:35:24 +00002738 Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, Ops);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002739 SDValue InFlag = Chain.getValue(1);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002740
Bruno Cardoso Lopes193e64c2010-01-30 18:32:07 +00002741 // Create the CALLSEQ_END node.
Akira Hatanaka97ba7692012-07-26 23:27:01 +00002742 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002743 DAG.getIntPtrConstant(0, DL, true), InFlag, DL);
Bruno Cardoso Lopes193e64c2010-01-30 18:32:07 +00002744 InFlag = Chain.getValue(1);
2745
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002746 // Handle result values, copying them out of physregs into vregs that we
2747 // return.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002748 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg, Ins, DL, DAG,
2749 InVals, CLI);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002750}
2751
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002752/// LowerCallResult - Lower the result values of a call into the
2753/// appropriate copies out of appropriate physical registers.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002754SDValue MipsTargetLowering::LowerCallResult(
2755 SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool IsVarArg,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002756 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
2757 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals,
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002758 TargetLowering::CallLoweringInfo &CLI) const {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002759 // Assign locations to each value returned by this call.
2760 SmallVector<CCValAssign, 16> RVLocs;
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002761 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
2762 *DAG.getContext());
2763 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips, CLI);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002764
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002765 // Copy all of the result registers out of their specified physreg.
2766 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Daniel Sandersae275e32014-09-25 12:15:05 +00002767 CCValAssign &VA = RVLocs[i];
2768 assert(VA.isRegLoc() && "Can only return in registers!");
2769
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002770 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(),
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002771 RVLocs[i].getLocVT(), InFlag);
2772 Chain = Val.getValue(1);
2773 InFlag = Val.getValue(2);
2774
Daniel Sandersae275e32014-09-25 12:15:05 +00002775 if (VA.isUpperBitsInLoc()) {
2776 unsigned ValSizeInBits = Ins[i].ArgVT.getSizeInBits();
2777 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
2778 unsigned Shift =
2779 VA.getLocInfo() == CCValAssign::ZExtUpper ? ISD::SRL : ISD::SRA;
2780 Val = DAG.getNode(
2781 Shift, DL, VA.getLocVT(), Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002782 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
Daniel Sandersae275e32014-09-25 12:15:05 +00002783 }
2784
2785 switch (VA.getLocInfo()) {
2786 default:
2787 llvm_unreachable("Unknown loc info!");
2788 case CCValAssign::Full:
2789 break;
2790 case CCValAssign::BCvt:
2791 Val = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Val);
2792 break;
2793 case CCValAssign::AExt:
2794 case CCValAssign::AExtUpper:
2795 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2796 break;
2797 case CCValAssign::ZExt:
2798 case CCValAssign::ZExtUpper:
2799 Val = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Val,
2800 DAG.getValueType(VA.getValVT()));
2801 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2802 break;
2803 case CCValAssign::SExt:
2804 case CCValAssign::SExtUpper:
2805 Val = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Val,
2806 DAG.getValueType(VA.getValVT()));
2807 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2808 break;
2809 }
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002810
2811 InVals.push_back(Val);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002812 }
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +00002813
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002814 return Chain;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002815}
2816
Daniel Sandersc43cda82014-11-07 16:54:21 +00002817static SDValue UnpackFromArgumentSlot(SDValue Val, const CCValAssign &VA,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002818 EVT ArgVT, const SDLoc &DL,
2819 SelectionDAG &DAG) {
Daniel Sandersc43cda82014-11-07 16:54:21 +00002820 MVT LocVT = VA.getLocVT();
2821 EVT ValVT = VA.getValVT();
2822
2823 // Shift into the upper bits if necessary.
2824 switch (VA.getLocInfo()) {
2825 default:
2826 break;
2827 case CCValAssign::AExtUpper:
2828 case CCValAssign::SExtUpper:
2829 case CCValAssign::ZExtUpper: {
2830 unsigned ValSizeInBits = ArgVT.getSizeInBits();
2831 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
2832 unsigned Opcode =
2833 VA.getLocInfo() == CCValAssign::ZExtUpper ? ISD::SRL : ISD::SRA;
2834 Val = DAG.getNode(
2835 Opcode, DL, VA.getLocVT(), Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002836 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
Daniel Sandersc43cda82014-11-07 16:54:21 +00002837 break;
2838 }
2839 }
2840
2841 // If this is an value smaller than the argument slot size (32-bit for O32,
2842 // 64-bit for N32/N64), it has been promoted in some way to the argument slot
2843 // size. Extract the value and insert any appropriate assertions regarding
2844 // sign/zero extension.
2845 switch (VA.getLocInfo()) {
2846 default:
2847 llvm_unreachable("Unknown loc info!");
2848 case CCValAssign::Full:
2849 break;
2850 case CCValAssign::AExtUpper:
2851 case CCValAssign::AExt:
2852 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
2853 break;
2854 case CCValAssign::SExtUpper:
2855 case CCValAssign::SExt:
2856 Val = DAG.getNode(ISD::AssertSext, DL, LocVT, Val, DAG.getValueType(ValVT));
2857 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
2858 break;
2859 case CCValAssign::ZExtUpper:
2860 case CCValAssign::ZExt:
2861 Val = DAG.getNode(ISD::AssertZext, DL, LocVT, Val, DAG.getValueType(ValVT));
2862 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
2863 break;
2864 case CCValAssign::BCvt:
2865 Val = DAG.getNode(ISD::BITCAST, DL, ValVT, Val);
2866 break;
2867 }
2868
2869 return Val;
2870}
2871
Akira Hatanakae2489122011-04-15 21:51:11 +00002872//===----------------------------------------------------------------------===//
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002873// Formal Arguments Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002874//===----------------------------------------------------------------------===//
Wesley Peck527da1b2010-11-23 03:31:01 +00002875/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002876/// and generate load operations for arguments places on the stack.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002877SDValue MipsTargetLowering::LowerFormalArguments(
2878 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
2879 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
2880 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Bruno Cardoso Lopesa01ede22008-08-04 07:12:52 +00002881 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00002882 MachineFrameInfo &MFI = MF.getFrameInfo();
Bruno Cardoso Lopes14033fb2007-08-28 05:08:16 +00002883 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002884
Dan Gohman31ae5862010-04-17 14:41:14 +00002885 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002886
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002887 // Used with vargs to acumulate store chains.
2888 std::vector<SDValue> OutChains;
2889
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002890 // Assign locations to all of the incoming arguments.
2891 SmallVector<CCValAssign, 16> ArgLocs;
Daniel Sanders23e98772014-11-02 16:09:29 +00002892 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
2893 *DAG.getContext());
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002894 CCInfo.AllocateStack(ABI.GetCalleeAllocdArgSizeInBytes(CallConv), 1);
Vasileios Kalintiris43dff0c2015-10-26 12:38:43 +00002895 const Function *Func = DAG.getMachineFunction().getFunction();
2896 Function::const_arg_iterator FuncArg = Func->arg_begin();
2897
Vasileios Kalintiris165121f2015-10-26 14:24:30 +00002898 if (Func->hasFnAttribute("interrupt") && !Func->arg_empty())
2899 report_fatal_error(
2900 "Functions with the interrupt attribute cannot have arguments!");
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002901
Daniel Sandersb70e27c2014-11-06 16:36:30 +00002902 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips_FixedArg);
Akira Hatanaka4866fe12012-10-30 19:37:25 +00002903 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(),
Daniel Sanders23e98772014-11-02 16:09:29 +00002904 CCInfo.getInRegsParamsCount() > 0);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002905
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00002906 unsigned CurArgIdx = 0;
Daniel Sanders23e98772014-11-02 16:09:29 +00002907 CCInfo.rewindByValRegsInfo();
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002908
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00002909 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002910 CCValAssign &VA = ArgLocs[i];
Andrew Trick05938a52015-02-16 18:10:47 +00002911 if (Ins[i].isOrigArg()) {
2912 std::advance(FuncArg, Ins[i].getOrigArgIndex() - CurArgIdx);
2913 CurArgIdx = Ins[i].getOrigArgIndex();
2914 }
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002915 EVT ValVT = VA.getValVT();
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002916 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2917 bool IsRegLoc = VA.isRegLoc();
2918
2919 if (Flags.isByVal()) {
Andrew Trick05938a52015-02-16 18:10:47 +00002920 assert(Ins[i].isOrigArg() && "Byval arguments cannot be implicit");
Daniel Sanders23e98772014-11-02 16:09:29 +00002921 unsigned FirstByValReg, LastByValReg;
2922 unsigned ByValIdx = CCInfo.getInRegsParamsProcessed();
2923 CCInfo.getInRegsParamInfo(ByValIdx, FirstByValReg, LastByValReg);
2924
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002925 assert(Flags.getByValSize() &&
2926 "ByVal args of size 0 should have been ignored by front-end.");
Daniel Sanders23e98772014-11-02 16:09:29 +00002927 assert(ByValIdx < CCInfo.getInRegsParamsCount());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002928 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002929 FirstByValReg, LastByValReg, VA, CCInfo);
Daniel Sanders23e98772014-11-02 16:09:29 +00002930 CCInfo.nextInRegsParam();
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002931 continue;
2932 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002933
2934 // Arguments stored on registers
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002935 if (IsRegLoc) {
Akira Hatanaka7d822522013-10-28 21:21:36 +00002936 MVT RegVT = VA.getLocVT();
Akira Hatanakacb4a1a82011-05-24 00:23:52 +00002937 unsigned ArgReg = VA.getLocReg();
Akira Hatanaka7d822522013-10-28 21:21:36 +00002938 const TargetRegisterClass *RC = getRegClassFor(RegVT);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002939
Wesley Peck527da1b2010-11-23 03:31:01 +00002940 // Transform the arguments stored on
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002941 // physical registers into virtual ones
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002942 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC);
2943 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
Wesley Peck527da1b2010-11-23 03:31:01 +00002944
Daniel Sandersc43cda82014-11-07 16:54:21 +00002945 ArgValue = UnpackFromArgumentSlot(ArgValue, VA, Ins[i].ArgVT, DL, DAG);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002946
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002947 // Handle floating point arguments passed in integer registers and
2948 // long double arguments passed in floating point registers.
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002949 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002950 (RegVT == MVT::i64 && ValVT == MVT::f64) ||
2951 (RegVT == MVT::f64 && ValVT == MVT::i64))
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002952 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
Eric Christopher96e72c62015-01-29 23:27:36 +00002953 else if (ABI.IsO32() && RegVT == MVT::i32 &&
Eric Christopherbf33a3c2014-07-02 23:18:40 +00002954 ValVT == MVT::f64) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002955 unsigned Reg2 = addLiveIn(DAG.getMachineFunction(),
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002956 getNextIntArgReg(ArgReg), RC);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002957 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT);
Eric Christopher1c29a652014-07-18 22:55:25 +00002958 if (!Subtarget.isLittle())
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002959 std::swap(ArgValue, ArgValue2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002960 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64,
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002961 ArgValue, ArgValue2);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002962 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002963
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002964 InVals.push_back(ArgValue);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002965 } else { // VA.isRegLoc()
Daniel Sandersc43cda82014-11-07 16:54:21 +00002966 MVT LocVT = VA.getLocVT();
2967
Eric Christopher96e72c62015-01-29 23:27:36 +00002968 if (ABI.IsO32()) {
Daniel Sandersc43cda82014-11-07 16:54:21 +00002969 // We ought to be able to use LocVT directly but O32 sets it to i32
2970 // when allocating floating point values to integer registers.
2971 // This shouldn't influence how we load the value into registers unless
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00002972 // we are targeting softfloat.
Eric Christophere8ae3e32015-05-07 23:10:21 +00002973 if (VA.getValVT().isFloatingPoint() && !Subtarget.useSoftFloat())
Daniel Sandersc43cda82014-11-07 16:54:21 +00002974 LocVT = VA.getValVT();
2975 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002976
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002977 // sanity check
2978 assert(VA.isMemLoc());
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002979
Wesley Peck527da1b2010-11-23 03:31:01 +00002980 // The stack pointer offset is relative to the caller stack frame.
Matthias Braun941a7052016-07-28 18:40:00 +00002981 int FI = MFI.CreateFixedObject(LocVT.getSizeInBits() / 8,
2982 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002983
2984 // Create load nodes to retrieve arguments from the stack
Mehdi Amini44ede332015-07-09 02:09:04 +00002985 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Alex Lorenze40c8a22015-08-11 23:09:45 +00002986 SDValue ArgValue = DAG.getLoad(
2987 LocVT, DL, Chain, FIN,
Justin Lebar9c375812016-07-15 18:27:10 +00002988 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI));
Daniel Sandersc43cda82014-11-07 16:54:21 +00002989 OutChains.push_back(ArgValue.getValue(1));
2990
2991 ArgValue = UnpackFromArgumentSlot(ArgValue, VA, Ins[i].ArgVT, DL, DAG);
2992
2993 InVals.push_back(ArgValue);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002994 }
Reid Kleckner7a59e082014-05-12 22:01:27 +00002995 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002996
Reid Kleckner7a59e082014-05-12 22:01:27 +00002997 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Reid Kleckner79418562014-05-09 22:32:13 +00002998 // The mips ABIs for returning structs by value requires that we copy
2999 // the sret argument into $v0 for the return. Save the argument into
3000 // a virtual register so that we can access it from the return points.
Reid Kleckner7a59e082014-05-12 22:01:27 +00003001 if (Ins[i].Flags.isSRet()) {
Reid Kleckner79418562014-05-09 22:32:13 +00003002 unsigned Reg = MipsFI->getSRetReturnReg();
3003 if (!Reg) {
3004 Reg = MF.getRegInfo().createVirtualRegister(
Eric Christopher96e72c62015-01-29 23:27:36 +00003005 getRegClassFor(ABI.IsN64() ? MVT::i64 : MVT::i32));
Reid Kleckner79418562014-05-09 22:32:13 +00003006 MipsFI->setSRetReturnReg(Reg);
3007 }
Reid Kleckner7a59e082014-05-12 22:01:27 +00003008 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[i]);
Reid Kleckner79418562014-05-09 22:32:13 +00003009 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
Reid Kleckner7a59e082014-05-12 22:01:27 +00003010 break;
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003011 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003012 }
3013
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003014 if (IsVarArg)
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003015 writeVarArgRegs(OutChains, Chain, DL, DAG, CCInfo);
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003016
Wesley Peck527da1b2010-11-23 03:31:01 +00003017 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003018 // the size of Ins and InVals. This only happens when on varg functions
3019 if (!OutChains.empty()) {
3020 OutChains.push_back(Chain);
Craig Topper48d114b2014-04-26 18:35:24 +00003021 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003022 }
3023
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003024 return Chain;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003025}
3026
Akira Hatanakae2489122011-04-15 21:51:11 +00003027//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003028// Return Value Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00003029//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003030
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003031bool
3032MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003033 MachineFunction &MF, bool IsVarArg,
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003034 const SmallVectorImpl<ISD::OutputArg> &Outs,
3035 LLVMContext &Context) const {
3036 SmallVector<CCValAssign, 16> RVLocs;
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003037 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003038 return CCInfo.CheckReturn(Outs, RetCC_Mips);
3039}
3040
Petar Jovanovic5b436222015-03-23 12:28:13 +00003041bool
3042MipsTargetLowering::shouldSignExtendTypeInLibCall(EVT Type, bool IsSigned) const {
Eric Christophere8ae3e32015-05-07 23:10:21 +00003043 if (Subtarget.hasMips3() && Subtarget.useSoftFloat()) {
Petar Jovanovic5b436222015-03-23 12:28:13 +00003044 if (Type == MVT::i32)
3045 return true;
3046 }
3047 return IsSigned;
3048}
3049
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003050SDValue
Vasileios Kalintiris43dff0c2015-10-26 12:38:43 +00003051MipsTargetLowering::LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003052 const SDLoc &DL,
3053 SelectionDAG &DAG) const {
Vasileios Kalintiris43dff0c2015-10-26 12:38:43 +00003054
3055 MachineFunction &MF = DAG.getMachineFunction();
3056 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3057
3058 MipsFI->setISR();
3059
3060 return DAG.getNode(MipsISD::ERet, DL, MVT::Other, RetOps);
3061}
3062
3063SDValue
3064MipsTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
3065 bool IsVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003066 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003067 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003068 const SDLoc &DL, SelectionDAG &DAG) const {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003069 // CCValAssign - represent the assignment of
3070 // the return value to a location
3071 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003072 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003073
3074 // CCState - Info about the registers and stack slot.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003075 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, *DAG.getContext());
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003076
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003077 // Analyze return values.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003078 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003079
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003080 SDValue Flag;
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003081 SmallVector<SDValue, 4> RetOps(1, Chain);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003082
3083 // Copy the result values into the output registers.
3084 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003085 SDValue Val = OutVals[i];
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003086 CCValAssign &VA = RVLocs[i];
3087 assert(VA.isRegLoc() && "Can only return in registers!");
Daniel Sandersae275e32014-09-25 12:15:05 +00003088 bool UseUpperBits = false;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003089
Daniel Sandersae275e32014-09-25 12:15:05 +00003090 switch (VA.getLocInfo()) {
3091 default:
3092 llvm_unreachable("Unknown loc info!");
3093 case CCValAssign::Full:
3094 break;
3095 case CCValAssign::BCvt:
3096 Val = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Val);
3097 break;
3098 case CCValAssign::AExtUpper:
3099 UseUpperBits = true;
Justin Bognerb03fd122016-08-17 05:10:15 +00003100 LLVM_FALLTHROUGH;
Daniel Sandersae275e32014-09-25 12:15:05 +00003101 case CCValAssign::AExt:
3102 Val = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Val);
3103 break;
3104 case CCValAssign::ZExtUpper:
3105 UseUpperBits = true;
Justin Bognerb03fd122016-08-17 05:10:15 +00003106 LLVM_FALLTHROUGH;
Daniel Sandersae275e32014-09-25 12:15:05 +00003107 case CCValAssign::ZExt:
3108 Val = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Val);
3109 break;
3110 case CCValAssign::SExtUpper:
3111 UseUpperBits = true;
Justin Bognerb03fd122016-08-17 05:10:15 +00003112 LLVM_FALLTHROUGH;
Daniel Sandersae275e32014-09-25 12:15:05 +00003113 case CCValAssign::SExt:
3114 Val = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Val);
3115 break;
3116 }
3117
3118 if (UseUpperBits) {
3119 unsigned ValSizeInBits = Outs[i].ArgVT.getSizeInBits();
3120 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
3121 Val = DAG.getNode(
3122 ISD::SHL, DL, VA.getLocVT(), Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003123 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
Daniel Sandersae275e32014-09-25 12:15:05 +00003124 }
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003125
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003126 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003127
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003128 // Guarantee that all emitted copies are stuck together with flags.
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003129 Flag = Chain.getValue(1);
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003130 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003131 }
3132
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003133 // The mips ABIs for returning structs by value requires that we copy
3134 // the sret argument into $v0 for the return. We saved the argument into
3135 // a virtual register in the entry block, so now we copy the value out
3136 // and into $v0.
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003137 if (MF.getFunction()->hasStructRetAttr()) {
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003138 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3139 unsigned Reg = MipsFI->getSRetReturnReg();
3140
Wesley Peck527da1b2010-11-23 03:31:01 +00003141 if (!Reg)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003142 llvm_unreachable("sret virtual register not created in the entry block");
Mehdi Amini44ede332015-07-09 02:09:04 +00003143 SDValue Val =
3144 DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(DAG.getDataLayout()));
Eric Christopher96e72c62015-01-29 23:27:36 +00003145 unsigned V0 = ABI.IsN64() ? Mips::V0_64 : Mips::V0;
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003146
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003147 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003148 Flag = Chain.getValue(1);
Mehdi Amini44ede332015-07-09 02:09:04 +00003149 RetOps.push_back(DAG.getRegister(V0, getPointerTy(DAG.getDataLayout())));
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003150 }
3151
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003152 RetOps[0] = Chain; // Update chain.
Akira Hatanakaefff7b72012-07-10 00:19:06 +00003153
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003154 // Add the flag if we have it.
3155 if (Flag.getNode())
3156 RetOps.push_back(Flag);
3157
Vasileios Kalintiris43dff0c2015-10-26 12:38:43 +00003158 // ISRs must use "eret".
3159 if (DAG.getMachineFunction().getFunction()->hasFnAttribute("interrupt"))
3160 return LowerInterruptReturn(RetOps, DL, DAG);
3161
3162 // Standard return on Mips is a "jr $ra"
Craig Topper48d114b2014-04-26 18:35:24 +00003163 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, RetOps);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003164}
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003165
Akira Hatanakae2489122011-04-15 21:51:11 +00003166//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003167// Mips Inline Assembly Support
Akira Hatanakae2489122011-04-15 21:51:11 +00003168//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003169
3170/// getConstraintType - Given a constraint letter, return the type of
3171/// constraint it is for this target.
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003172MipsTargetLowering::ConstraintType
3173MipsTargetLowering::getConstraintType(StringRef Constraint) const {
Daniel Sanders8b59af12013-11-12 12:56:01 +00003174 // Mips specific constraints
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003175 // GCC config/mips/constraints.md
3176 //
Wesley Peck527da1b2010-11-23 03:31:01 +00003177 // 'd' : An address register. Equivalent to r
3178 // unless generating MIPS16 code.
3179 // 'y' : Equivalent to r; retained for
3180 // backwards compatibility.
Eric Christophere3c494d2012-05-07 06:25:10 +00003181 // 'c' : A register suitable for use in an indirect
3182 // jump. This will always be $25 for -mabicalls.
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003183 // 'l' : The lo register. 1 word storage.
3184 // 'x' : The hilo register pair. Double word storage.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003185 if (Constraint.size() == 1) {
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003186 switch (Constraint[0]) {
3187 default : break;
Wesley Peck527da1b2010-11-23 03:31:01 +00003188 case 'd':
3189 case 'y':
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003190 case 'f':
Eric Christophere3c494d2012-05-07 06:25:10 +00003191 case 'c':
Eric Christopher9c492e62012-05-07 06:25:15 +00003192 case 'l':
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003193 case 'x':
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003194 return C_RegisterClass;
Jack Carter0e149b02013-03-04 21:33:15 +00003195 case 'R':
3196 return C_Memory;
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003197 }
3198 }
Daniel Sandersa73d8fe2015-03-24 11:26:34 +00003199
3200 if (Constraint == "ZC")
3201 return C_Memory;
3202
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003203 return TargetLowering::getConstraintType(Constraint);
3204}
3205
John Thompsone8360b72010-10-29 17:29:13 +00003206/// Examine constraint type and operand type and determine a weight value.
3207/// This object must already have been set up with the operand type
3208/// and the current alternative constraint selected.
3209TargetLowering::ConstraintWeight
3210MipsTargetLowering::getSingleConstraintMatchWeight(
3211 AsmOperandInfo &info, const char *constraint) const {
3212 ConstraintWeight weight = CW_Invalid;
3213 Value *CallOperandVal = info.CallOperandVal;
3214 // If we don't have a value, we can't do a match,
3215 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +00003216 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +00003217 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +00003218 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +00003219 // Look at the constraint type.
3220 switch (*constraint) {
3221 default:
3222 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
3223 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00003224 case 'd':
3225 case 'y':
John Thompsone8360b72010-10-29 17:29:13 +00003226 if (type->isIntegerTy())
3227 weight = CW_Register;
3228 break;
Daniel Sanders8b59af12013-11-12 12:56:01 +00003229 case 'f': // FPU or MSA register
Eric Christopher1c29a652014-07-18 22:55:25 +00003230 if (Subtarget.hasMSA() && type->isVectorTy() &&
Daniel Sanders8b59af12013-11-12 12:56:01 +00003231 cast<VectorType>(type)->getBitWidth() == 128)
3232 weight = CW_Register;
3233 else if (type->isFloatTy())
John Thompsone8360b72010-10-29 17:29:13 +00003234 weight = CW_Register;
3235 break;
Eric Christophere3c494d2012-05-07 06:25:10 +00003236 case 'c': // $25 for indirect jumps
Eric Christopher9c492e62012-05-07 06:25:15 +00003237 case 'l': // lo register
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003238 case 'x': // hilo register pair
Daniel Sanders8b59af12013-11-12 12:56:01 +00003239 if (type->isIntegerTy())
Eric Christophere3c494d2012-05-07 06:25:10 +00003240 weight = CW_SpecificReg;
Daniel Sanders8b59af12013-11-12 12:56:01 +00003241 break;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003242 case 'I': // signed 16 bit immediate
Eric Christopher7201e1b2012-05-07 03:13:42 +00003243 case 'J': // integer zero
Eric Christopher3ff88a02012-05-07 05:46:29 +00003244 case 'K': // unsigned 16 bit immediate
Eric Christopher1109b342012-05-07 05:46:37 +00003245 case 'L': // signed 32 bit immediate where lower 16 bits are 0
Eric Christophere07aa432012-05-07 05:46:43 +00003246 case 'N': // immediate in the range of -65535 to -1 (inclusive)
Eric Christopher470578a2012-05-07 05:46:48 +00003247 case 'O': // signed 15 bit immediate (+- 16383)
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003248 case 'P': // immediate in the range of 65535 to 1 (inclusive)
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003249 if (isa<ConstantInt>(CallOperandVal))
3250 weight = CW_Constant;
3251 break;
Jack Carter0e149b02013-03-04 21:33:15 +00003252 case 'R':
3253 weight = CW_Memory;
3254 break;
John Thompsone8360b72010-10-29 17:29:13 +00003255 }
3256 return weight;
3257}
3258
Akira Hatanaka7473b472013-08-14 00:21:25 +00003259/// This is a helper function to parse a physical register string and split it
3260/// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag
3261/// that is returned indicates whether parsing was successful. The second flag
3262/// is true if the numeric part exists.
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003263static std::pair<bool, bool> parsePhysicalReg(StringRef C, StringRef &Prefix,
3264 unsigned long long &Reg) {
Akira Hatanaka7473b472013-08-14 00:21:25 +00003265 if (C.front() != '{' || C.back() != '}')
3266 return std::make_pair(false, false);
3267
3268 // Search for the first numeric character.
3269 StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1;
Craig Topper2241dfd2015-11-23 07:19:06 +00003270 I = std::find_if(B, E, isdigit);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003271
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003272 Prefix = StringRef(B, I - B);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003273
3274 // The second flag is set to false if no numeric characters were found.
3275 if (I == E)
3276 return std::make_pair(true, false);
3277
3278 // Parse the numeric characters.
3279 return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg),
3280 true);
3281}
3282
3283std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering::
Craig Topper6dc4a8bc2014-08-30 16:48:02 +00003284parseRegForInlineAsmConstraint(StringRef C, MVT VT) const {
Eric Christopherd9134482014-08-04 21:25:23 +00003285 const TargetRegisterInfo *TRI =
Eric Christopher96e72c62015-01-29 23:27:36 +00003286 Subtarget.getRegisterInfo();
Akira Hatanaka7473b472013-08-14 00:21:25 +00003287 const TargetRegisterClass *RC;
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003288 StringRef Prefix;
Akira Hatanaka7473b472013-08-14 00:21:25 +00003289 unsigned long long Reg;
3290
3291 std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg);
3292
3293 if (!R.first)
Craig Topper062a2ba2014-04-25 05:30:21 +00003294 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003295
3296 if ((Prefix == "hi" || Prefix == "lo")) { // Parse hi/lo.
3297 // No numeric characters follow "hi" or "lo".
3298 if (R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003299 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003300
3301 RC = TRI->getRegClass(Prefix == "hi" ?
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00003302 Mips::HI32RegClassID : Mips::LO32RegClassID);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003303 return std::make_pair(*(RC->begin()), RC);
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003304 } else if (Prefix.startswith("$msa")) {
Daniel Sanders8b59af12013-11-12 12:56:01 +00003305 // Parse $msa(ir|csr|access|save|modify|request|map|unmap)
3306
3307 // No numeric characters follow the name.
3308 if (R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003309 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003310
3311 Reg = StringSwitch<unsigned long long>(Prefix)
3312 .Case("$msair", Mips::MSAIR)
3313 .Case("$msacsr", Mips::MSACSR)
3314 .Case("$msaaccess", Mips::MSAAccess)
3315 .Case("$msasave", Mips::MSASave)
3316 .Case("$msamodify", Mips::MSAModify)
3317 .Case("$msarequest", Mips::MSARequest)
3318 .Case("$msamap", Mips::MSAMap)
3319 .Case("$msaunmap", Mips::MSAUnmap)
3320 .Default(0);
3321
3322 if (!Reg)
Craig Topper062a2ba2014-04-25 05:30:21 +00003323 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003324
3325 RC = TRI->getRegClass(Mips::MSACtrlRegClassID);
3326 return std::make_pair(Reg, RC);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003327 }
3328
3329 if (!R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003330 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003331
3332 if (Prefix == "$f") { // Parse $f0-$f31.
3333 // If the size of FP registers is 64-bit or Reg is an even number, select
3334 // the 64-bit register class. Otherwise, select the 32-bit register class.
3335 if (VT == MVT::Other)
Eric Christopher1c29a652014-07-18 22:55:25 +00003336 VT = (Subtarget.isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32;
Akira Hatanaka7473b472013-08-14 00:21:25 +00003337
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003338 RC = getRegClassFor(VT);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003339
3340 if (RC == &Mips::AFGR64RegClass) {
3341 assert(Reg % 2 == 0);
3342 Reg >>= 1;
3343 }
Daniel Sanders8b59af12013-11-12 12:56:01 +00003344 } else if (Prefix == "$fcc") // Parse $fcc0-$fcc7.
Akira Hatanaka7473b472013-08-14 00:21:25 +00003345 RC = TRI->getRegClass(Mips::FCCRegClassID);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003346 else if (Prefix == "$w") { // Parse $w0-$w31.
3347 RC = getRegClassFor((VT == MVT::Other) ? MVT::v16i8 : VT);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003348 } else { // Parse $0-$31.
3349 assert(Prefix == "$");
3350 RC = getRegClassFor((VT == MVT::Other) ? MVT::i32 : VT);
3351 }
3352
3353 assert(Reg < RC->getNumRegs());
3354 return std::make_pair(*(RC->begin() + Reg), RC);
3355}
3356
Eric Christophereaf77dc2011-06-29 19:33:04 +00003357/// Given a register class constraint, like 'r', if this corresponds directly
3358/// to an LLVM register class, return a register of 0 and the register class
3359/// pointer.
Eric Christopher11e4df72015-02-26 22:38:43 +00003360std::pair<unsigned, const TargetRegisterClass *>
3361MipsTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003362 StringRef Constraint,
Eric Christopher11e4df72015-02-26 22:38:43 +00003363 MVT VT) const {
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003364 if (Constraint.size() == 1) {
3365 switch (Constraint[0]) {
Eric Christopher9519c082011-06-29 19:04:31 +00003366 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
3367 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003368 case 'r':
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003369 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
Eric Christopher1c29a652014-07-18 22:55:25 +00003370 if (Subtarget.inMips16Mode())
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003371 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003372 return std::make_pair(0U, &Mips::GPR32RegClass);
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003373 }
Eric Christopher1c29a652014-07-18 22:55:25 +00003374 if (VT == MVT::i64 && !Subtarget.isGP64bit())
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003375 return std::make_pair(0U, &Mips::GPR32RegClass);
Eric Christopher1c29a652014-07-18 22:55:25 +00003376 if (VT == MVT::i64 && Subtarget.isGP64bit())
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003377 return std::make_pair(0U, &Mips::GPR64RegClass);
Eric Christopher58daf042012-05-07 03:13:22 +00003378 // This will generate an error message
Craig Topper062a2ba2014-04-25 05:30:21 +00003379 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003380 case 'f': // FPU or MSA register
3381 if (VT == MVT::v16i8)
3382 return std::make_pair(0U, &Mips::MSA128BRegClass);
3383 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
3384 return std::make_pair(0U, &Mips::MSA128HRegClass);
3385 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
3386 return std::make_pair(0U, &Mips::MSA128WRegClass);
3387 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
3388 return std::make_pair(0U, &Mips::MSA128DRegClass);
3389 else if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00003390 return std::make_pair(0U, &Mips::FGR32RegClass);
Eric Christopher1c29a652014-07-18 22:55:25 +00003391 else if ((VT == MVT::f64) && (!Subtarget.isSingleFloat())) {
3392 if (Subtarget.isFP64bit())
Craig Topperc7242e02012-04-20 07:30:17 +00003393 return std::make_pair(0U, &Mips::FGR64RegClass);
3394 return std::make_pair(0U, &Mips::AFGR64RegClass);
Akira Hatanakac669d7a2012-01-04 02:45:01 +00003395 }
Eric Christophere3c494d2012-05-07 06:25:10 +00003396 break;
3397 case 'c': // register suitable for indirect jump
3398 if (VT == MVT::i32)
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003399 return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass);
Eric Christophere3c494d2012-05-07 06:25:10 +00003400 assert(VT == MVT::i64 && "Unexpected type.");
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003401 return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass);
Eric Christopher9c492e62012-05-07 06:25:15 +00003402 case 'l': // register suitable for indirect jump
3403 if (VT == MVT::i32)
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00003404 return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass);
3405 return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass);
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003406 case 'x': // register suitable for indirect jump
3407 // Fixme: Not triggering the use of both hi and low
3408 // This will generate an error message
Craig Topper062a2ba2014-04-25 05:30:21 +00003409 return std::make_pair(0U, nullptr);
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003410 }
3411 }
Akira Hatanaka7473b472013-08-14 00:21:25 +00003412
3413 std::pair<unsigned, const TargetRegisterClass *> R;
3414 R = parseRegForInlineAsmConstraint(Constraint, VT);
3415
3416 if (R.second)
3417 return R;
3418
Eric Christopher11e4df72015-02-26 22:38:43 +00003419 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003420}
3421
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003422/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3423/// vector. If it is invalid, don't add anything to Ops.
3424void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3425 std::string &Constraint,
3426 std::vector<SDValue>&Ops,
3427 SelectionDAG &DAG) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003428 SDLoc DL(Op);
Craig Topper062a2ba2014-04-25 05:30:21 +00003429 SDValue Result;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003430
3431 // Only support length 1 constraints for now.
3432 if (Constraint.length() > 1) return;
3433
3434 char ConstraintLetter = Constraint[0];
3435 switch (ConstraintLetter) {
3436 default: break; // This will fall through to the generic implementation
3437 case 'I': // Signed 16 bit constant
3438 // If this fails, the parent routine will give an error
3439 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3440 EVT Type = Op.getValueType();
3441 int64_t Val = C->getSExtValue();
3442 if (isInt<16>(Val)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003443 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003444 break;
3445 }
3446 }
3447 return;
Eric Christopher7201e1b2012-05-07 03:13:42 +00003448 case 'J': // integer zero
3449 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3450 EVT Type = Op.getValueType();
3451 int64_t Val = C->getZExtValue();
3452 if (Val == 0) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003453 Result = DAG.getTargetConstant(0, DL, Type);
Eric Christopher7201e1b2012-05-07 03:13:42 +00003454 break;
3455 }
3456 }
3457 return;
Eric Christopher3ff88a02012-05-07 05:46:29 +00003458 case 'K': // unsigned 16 bit immediate
3459 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3460 EVT Type = Op.getValueType();
3461 uint64_t Val = (uint64_t)C->getZExtValue();
3462 if (isUInt<16>(Val)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003463 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopher3ff88a02012-05-07 05:46:29 +00003464 break;
3465 }
3466 }
3467 return;
Eric Christopher1109b342012-05-07 05:46:37 +00003468 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3469 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3470 EVT Type = Op.getValueType();
3471 int64_t Val = C->getSExtValue();
3472 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003473 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopher1109b342012-05-07 05:46:37 +00003474 break;
3475 }
3476 }
3477 return;
Eric Christophere07aa432012-05-07 05:46:43 +00003478 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3479 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3480 EVT Type = Op.getValueType();
3481 int64_t Val = C->getSExtValue();
3482 if ((Val >= -65535) && (Val <= -1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003483 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christophere07aa432012-05-07 05:46:43 +00003484 break;
3485 }
3486 }
3487 return;
Eric Christopher470578a2012-05-07 05:46:48 +00003488 case 'O': // signed 15 bit immediate
3489 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3490 EVT Type = Op.getValueType();
3491 int64_t Val = C->getSExtValue();
3492 if ((isInt<15>(Val))) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003493 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopher470578a2012-05-07 05:46:48 +00003494 break;
3495 }
3496 }
3497 return;
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003498 case 'P': // immediate in the range of 1 to 65535 (inclusive)
3499 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3500 EVT Type = Op.getValueType();
3501 int64_t Val = C->getSExtValue();
3502 if ((Val <= 65535) && (Val >= 1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003503 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003504 break;
3505 }
3506 }
3507 return;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003508 }
3509
3510 if (Result.getNode()) {
3511 Ops.push_back(Result);
3512 return;
3513 }
3514
3515 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3516}
3517
Mehdi Amini0cdec1e2015-07-09 02:09:40 +00003518bool MipsTargetLowering::isLegalAddressingMode(const DataLayout &DL,
3519 const AddrMode &AM, Type *Ty,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +00003520 unsigned AS) const {
Akira Hatanakaef839192012-11-17 00:25:41 +00003521 // No global is ever allowed as a base.
3522 if (AM.BaseGV)
3523 return false;
3524
3525 switch (AM.Scale) {
3526 case 0: // "r+i" or just "i", depending on HasBaseReg.
3527 break;
3528 case 1:
3529 if (!AM.HasBaseReg) // allow "r+i".
3530 break;
3531 return false; // disallow "r+r" or "r+r+i".
3532 default:
3533 return false;
3534 }
3535
3536 return true;
3537}
3538
3539bool
Dan Gohman2fe6bee2008-10-18 02:06:02 +00003540MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3541 // The Mips target isn't yet aware of offsets.
3542 return false;
3543}
Evan Cheng16993aa2009-10-27 19:56:55 +00003544
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003545EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003546 unsigned SrcAlign,
3547 bool IsMemset, bool ZeroMemset,
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003548 bool MemcpyStrSrc,
3549 MachineFunction &MF) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003550 if (Subtarget.hasMips64())
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003551 return MVT::i64;
3552
3553 return MVT::i32;
3554}
3555
Evan Cheng83896a52009-10-28 01:43:28 +00003556bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
3557 if (VT != MVT::f32 && VT != MVT::f64)
3558 return false;
Bruno Cardoso Lopesb02a9df2011-01-18 19:41:41 +00003559 if (Imm.isNegZero())
3560 return false;
Evan Cheng16993aa2009-10-27 19:56:55 +00003561 return Imm.isZero();
3562}
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003563
3564unsigned MipsTargetLowering::getJumpTableEncoding() const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003565 if (ABI.IsN64())
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003566 return MachineJumpTableInfo::EK_GPRel64BlockAddress;
Jia Liuf54f60f2012-02-28 07:46:26 +00003567
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003568 return TargetLowering::getJumpTableEncoding();
3569}
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003570
Eric Christopher824f42f2015-05-12 01:26:05 +00003571bool MipsTargetLowering::useSoftFloat() const {
3572 return Subtarget.useSoftFloat();
3573}
3574
Daniel Sandersf43e6872014-11-01 18:44:56 +00003575void MipsTargetLowering::copyByValRegs(
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003576 SDValue Chain, const SDLoc &DL, std::vector<SDValue> &OutChains,
3577 SelectionDAG &DAG, const ISD::ArgFlagsTy &Flags,
3578 SmallVectorImpl<SDValue> &InVals, const Argument *FuncArg,
3579 unsigned FirstReg, unsigned LastReg, const CCValAssign &VA,
3580 MipsCCState &State) const {
Akira Hatanaka25dad192012-10-27 00:10:18 +00003581 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00003582 MachineFrameInfo &MFI = MF.getFrameInfo();
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003583 unsigned GPRSizeInBytes = Subtarget.getGPRSizeInBytes();
Daniel Sanders23e98772014-11-02 16:09:29 +00003584 unsigned NumRegs = LastReg - FirstReg;
3585 unsigned RegAreaSize = NumRegs * GPRSizeInBytes;
Akira Hatanaka25dad192012-10-27 00:10:18 +00003586 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
3587 int FrameObjOffset;
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003588 ArrayRef<MCPhysReg> ByValArgRegs = ABI.GetByValArgRegs();
Akira Hatanaka25dad192012-10-27 00:10:18 +00003589
3590 if (RegAreaSize)
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003591 FrameObjOffset =
3592 (int)ABI.GetCalleeAllocdArgSizeInBytes(State.getCallingConv()) -
3593 (int)((ByValArgRegs.size() - FirstReg) * GPRSizeInBytes);
Akira Hatanaka25dad192012-10-27 00:10:18 +00003594 else
Daniel Sandersf43e6872014-11-01 18:44:56 +00003595 FrameObjOffset = VA.getLocMemOffset();
Akira Hatanaka25dad192012-10-27 00:10:18 +00003596
3597 // Create frame object.
Mehdi Amini44ede332015-07-09 02:09:04 +00003598 EVT PtrTy = getPointerTy(DAG.getDataLayout());
Matthias Braun941a7052016-07-28 18:40:00 +00003599 int FI = MFI.CreateFixedObject(FrameObjSize, FrameObjOffset, true);
Akira Hatanaka25dad192012-10-27 00:10:18 +00003600 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
3601 InVals.push_back(FIN);
3602
Daniel Sanders23e98772014-11-02 16:09:29 +00003603 if (!NumRegs)
Akira Hatanaka25dad192012-10-27 00:10:18 +00003604 return;
3605
3606 // Copy arg registers.
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003607 MVT RegTy = MVT::getIntegerVT(GPRSizeInBytes * 8);
Akira Hatanaka25dad192012-10-27 00:10:18 +00003608 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3609
Daniel Sanders23e98772014-11-02 16:09:29 +00003610 for (unsigned I = 0; I < NumRegs; ++I) {
Daniel Sandersd7eba312014-11-07 12:21:37 +00003611 unsigned ArgReg = ByValArgRegs[FirstReg + I];
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003612 unsigned VReg = addLiveIn(MF, ArgReg, RC);
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003613 unsigned Offset = I * GPRSizeInBytes;
Akira Hatanaka25dad192012-10-27 00:10:18 +00003614 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003615 DAG.getConstant(Offset, DL, PtrTy));
Akira Hatanaka25dad192012-10-27 00:10:18 +00003616 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
Justin Lebar9c375812016-07-15 18:27:10 +00003617 StorePtr, MachinePointerInfo(FuncArg, Offset));
Akira Hatanaka25dad192012-10-27 00:10:18 +00003618 OutChains.push_back(Store);
3619 }
3620}
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003621
3622// Copy byVal arg to registers and stack.
Daniel Sandersf43e6872014-11-01 18:44:56 +00003623void MipsTargetLowering::passByValArg(
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003624 SDValue Chain, const SDLoc &DL,
Daniel Sandersf43e6872014-11-01 18:44:56 +00003625 std::deque<std::pair<unsigned, SDValue>> &RegsToPass,
3626 SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr,
Matthias Braun941a7052016-07-28 18:40:00 +00003627 MachineFrameInfo &MFI, SelectionDAG &DAG, SDValue Arg, unsigned FirstReg,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003628 unsigned LastReg, const ISD::ArgFlagsTy &Flags, bool isLittle,
3629 const CCValAssign &VA) const {
Daniel Sandersac272632014-05-23 13:18:02 +00003630 unsigned ByValSizeInBytes = Flags.getByValSize();
3631 unsigned OffsetInBytes = 0; // From beginning of struct
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003632 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
Daniel Sandersac272632014-05-23 13:18:02 +00003633 unsigned Alignment = std::min(Flags.getByValAlign(), RegSizeInBytes);
Mehdi Amini44ede332015-07-09 02:09:04 +00003634 EVT PtrTy = getPointerTy(DAG.getDataLayout()),
3635 RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
Daniel Sanders23e98772014-11-02 16:09:29 +00003636 unsigned NumRegs = LastReg - FirstReg;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003637
Daniel Sanders23e98772014-11-02 16:09:29 +00003638 if (NumRegs) {
Craig Topper862d5d82015-09-28 00:15:34 +00003639 ArrayRef<MCPhysReg> ArgRegs = ABI.GetByValArgRegs();
Daniel Sanders23e98772014-11-02 16:09:29 +00003640 bool LeftoverBytes = (NumRegs * RegSizeInBytes > ByValSizeInBytes);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003641 unsigned I = 0;
3642
3643 // Copy words to registers.
Daniel Sanders23e98772014-11-02 16:09:29 +00003644 for (; I < NumRegs - LeftoverBytes; ++I, OffsetInBytes += RegSizeInBytes) {
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003645 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003646 DAG.getConstant(OffsetInBytes, DL, PtrTy));
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003647 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
Justin Lebar9c375812016-07-15 18:27:10 +00003648 MachinePointerInfo(), Alignment);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003649 MemOpChains.push_back(LoadVal.getValue(1));
Daniel Sanders23e98772014-11-02 16:09:29 +00003650 unsigned ArgReg = ArgRegs[FirstReg + I];
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003651 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
3652 }
3653
3654 // Return if the struct has been fully copied.
Daniel Sandersac272632014-05-23 13:18:02 +00003655 if (ByValSizeInBytes == OffsetInBytes)
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003656 return;
3657
3658 // Copy the remainder of the byval argument with sub-word loads and shifts.
3659 if (LeftoverBytes) {
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003660 SDValue Val;
3661
Daniel Sandersac272632014-05-23 13:18:02 +00003662 for (unsigned LoadSizeInBytes = RegSizeInBytes / 2, TotalBytesLoaded = 0;
3663 OffsetInBytes < ByValSizeInBytes; LoadSizeInBytes /= 2) {
3664 unsigned RemainingSizeInBytes = ByValSizeInBytes - OffsetInBytes;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003665
Daniel Sandersac272632014-05-23 13:18:02 +00003666 if (RemainingSizeInBytes < LoadSizeInBytes)
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003667 continue;
3668
3669 // Load subword.
3670 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003671 DAG.getConstant(OffsetInBytes, DL,
3672 PtrTy));
Daniel Sandersac272632014-05-23 13:18:02 +00003673 SDValue LoadVal = DAG.getExtLoad(
3674 ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr, MachinePointerInfo(),
Justin Lebar9c375812016-07-15 18:27:10 +00003675 MVT::getIntegerVT(LoadSizeInBytes * 8), Alignment);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003676 MemOpChains.push_back(LoadVal.getValue(1));
3677
3678 // Shift the loaded value.
3679 unsigned Shamt;
3680
3681 if (isLittle)
Daniel Sandersac272632014-05-23 13:18:02 +00003682 Shamt = TotalBytesLoaded * 8;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003683 else
Daniel Sandersac272632014-05-23 13:18:02 +00003684 Shamt = (RegSizeInBytes - (TotalBytesLoaded + LoadSizeInBytes)) * 8;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003685
3686 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003687 DAG.getConstant(Shamt, DL, MVT::i32));
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003688
3689 if (Val.getNode())
3690 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
3691 else
3692 Val = Shift;
3693
Daniel Sandersac272632014-05-23 13:18:02 +00003694 OffsetInBytes += LoadSizeInBytes;
3695 TotalBytesLoaded += LoadSizeInBytes;
3696 Alignment = std::min(Alignment, LoadSizeInBytes);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003697 }
3698
Daniel Sanders23e98772014-11-02 16:09:29 +00003699 unsigned ArgReg = ArgRegs[FirstReg + I];
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003700 RegsToPass.push_back(std::make_pair(ArgReg, Val));
3701 return;
3702 }
3703 }
3704
3705 // Copy remainder of byval arg to it with memcpy.
Daniel Sandersac272632014-05-23 13:18:02 +00003706 unsigned MemCpySize = ByValSizeInBytes - OffsetInBytes;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003707 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003708 DAG.getConstant(OffsetInBytes, DL, PtrTy));
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003709 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003710 DAG.getIntPtrConstant(VA.getLocMemOffset(), DL));
3711 Chain = DAG.getMemcpy(Chain, DL, Dst, Src,
3712 DAG.getConstant(MemCpySize, DL, PtrTy),
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003713 Alignment, /*isVolatile=*/false, /*AlwaysInline=*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00003714 /*isTailCall=*/false,
Nick Lewyckyaad475b2014-04-15 07:22:52 +00003715 MachinePointerInfo(), MachinePointerInfo());
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003716 MemOpChains.push_back(Chain);
3717}
Akira Hatanaka2a134022012-10-27 00:21:13 +00003718
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003719void MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003720 SDValue Chain, const SDLoc &DL,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003721 SelectionDAG &DAG,
Daniel Sanders853c2432014-11-01 18:13:52 +00003722 CCState &State) const {
Craig Topper862d5d82015-09-28 00:15:34 +00003723 ArrayRef<MCPhysReg> ArgRegs = ABI.GetVarArgRegs();
Tim Northover3b6b7ca2015-02-21 02:11:17 +00003724 unsigned Idx = State.getFirstUnallocated(ArgRegs);
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003725 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
3726 MVT RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003727 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3728 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00003729 MachineFrameInfo &MFI = MF.getFrameInfo();
Akira Hatanaka2a134022012-10-27 00:21:13 +00003730 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3731
3732 // Offset of the first variable argument from stack pointer.
3733 int VaArgOffset;
3734
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003735 if (ArgRegs.size() == Idx)
Rui Ueyamada00f2f2016-01-14 21:06:47 +00003736 VaArgOffset = alignTo(State.getNextStackOffset(), RegSizeInBytes);
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003737 else {
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003738 VaArgOffset =
3739 (int)ABI.GetCalleeAllocdArgSizeInBytes(State.getCallingConv()) -
3740 (int)(RegSizeInBytes * (ArgRegs.size() - Idx));
3741 }
Akira Hatanaka2a134022012-10-27 00:21:13 +00003742
3743 // Record the frame index of the first variable argument
3744 // which is a value necessary to VASTART.
Matthias Braun941a7052016-07-28 18:40:00 +00003745 int FI = MFI.CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003746 MipsFI->setVarArgsFrameIndex(FI);
3747
3748 // Copy the integer registers that have not been used for argument passing
3749 // to the argument register save area. For O32, the save area is allocated
3750 // in the caller's stack frame, while for N32/64, it is allocated in the
3751 // callee's stack frame.
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003752 for (unsigned I = Idx; I < ArgRegs.size();
3753 ++I, VaArgOffset += RegSizeInBytes) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003754 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003755 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
Matthias Braun941a7052016-07-28 18:40:00 +00003756 FI = MFI.CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
Mehdi Amini44ede332015-07-09 02:09:04 +00003757 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Justin Lebar9c375812016-07-15 18:27:10 +00003758 SDValue Store =
3759 DAG.getStore(Chain, DL, ArgValue, PtrOff, MachinePointerInfo());
Eric Christopher1c29a652014-07-18 22:55:25 +00003760 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(
3761 (Value *)nullptr);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003762 OutChains.push_back(Store);
3763 }
3764}
Daniel Sanders23e98772014-11-02 16:09:29 +00003765
3766void MipsTargetLowering::HandleByVal(CCState *State, unsigned &Size,
3767 unsigned Align) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003768 const TargetFrameLowering *TFL = Subtarget.getFrameLowering();
Daniel Sanders23e98772014-11-02 16:09:29 +00003769
3770 assert(Size && "Byval argument's size shouldn't be 0.");
3771
3772 Align = std::min(Align, TFL->getStackAlignment());
3773
3774 unsigned FirstReg = 0;
3775 unsigned NumRegs = 0;
3776
3777 if (State->getCallingConv() != CallingConv::Fast) {
3778 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
Craig Topper862d5d82015-09-28 00:15:34 +00003779 ArrayRef<MCPhysReg> IntArgRegs = ABI.GetByValArgRegs();
Daniel Sanders23e98772014-11-02 16:09:29 +00003780 // FIXME: The O32 case actually describes no shadow registers.
3781 const MCPhysReg *ShadowRegs =
Eric Christopher96e72c62015-01-29 23:27:36 +00003782 ABI.IsO32() ? IntArgRegs.data() : Mips64DPRegs;
Daniel Sanders23e98772014-11-02 16:09:29 +00003783
3784 // We used to check the size as well but we can't do that anymore since
3785 // CCState::HandleByVal() rounds up the size after calling this function.
3786 assert(!(Align % RegSizeInBytes) &&
3787 "Byval argument's alignment should be a multiple of"
3788 "RegSizeInBytes.");
3789
Tim Northover3b6b7ca2015-02-21 02:11:17 +00003790 FirstReg = State->getFirstUnallocated(IntArgRegs);
Daniel Sanders23e98772014-11-02 16:09:29 +00003791
3792 // If Align > RegSizeInBytes, the first arg register must be even.
3793 // FIXME: This condition happens to do the right thing but it's not the
3794 // right way to test it. We want to check that the stack frame offset
3795 // of the register is aligned.
3796 if ((Align > RegSizeInBytes) && (FirstReg % 2)) {
3797 State->AllocateReg(IntArgRegs[FirstReg], ShadowRegs[FirstReg]);
3798 ++FirstReg;
3799 }
3800
3801 // Mark the registers allocated.
Rui Ueyamada00f2f2016-01-14 21:06:47 +00003802 Size = alignTo(Size, RegSizeInBytes);
Daniel Sanders23e98772014-11-02 16:09:29 +00003803 for (unsigned I = FirstReg; Size > 0 && (I < IntArgRegs.size());
3804 Size -= RegSizeInBytes, ++I, ++NumRegs)
3805 State->AllocateReg(IntArgRegs[I], ShadowRegs[I]);
3806 }
3807
3808 State->addInRegsParamInfo(FirstReg, FirstReg + NumRegs);
3809}
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00003810
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003811MachineBasicBlock *MipsTargetLowering::emitPseudoSELECT(MachineInstr &MI,
3812 MachineBasicBlock *BB,
3813 bool isFPCmp,
3814 unsigned Opc) const {
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00003815 assert(!(Subtarget.hasMips4() || Subtarget.hasMips32()) &&
3816 "Subtarget already supports SELECT nodes with the use of"
3817 "conditional-move instructions.");
3818
3819 const TargetInstrInfo *TII =
Eric Christopher96e72c62015-01-29 23:27:36 +00003820 Subtarget.getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003821 DebugLoc DL = MI.getDebugLoc();
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00003822
3823 // To "insert" a SELECT instruction, we actually have to insert the
3824 // diamond control-flow pattern. The incoming instruction knows the
3825 // destination vreg to set, the condition code register to branch on, the
3826 // true/false values to select between, and a branch opcode to use.
3827 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +00003828 MachineFunction::iterator It = ++BB->getIterator();
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00003829
3830 // thisMBB:
3831 // ...
3832 // TrueVal = ...
3833 // setcc r1, r2, r3
3834 // bNE r1, r0, copy1MBB
3835 // fallthrough --> copy0MBB
3836 MachineBasicBlock *thisMBB = BB;
3837 MachineFunction *F = BB->getParent();
3838 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
3839 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
3840 F->insert(It, copy0MBB);
3841 F->insert(It, sinkMBB);
3842
3843 // Transfer the remainder of BB and its successor edges to sinkMBB.
3844 sinkMBB->splice(sinkMBB->begin(), BB,
3845 std::next(MachineBasicBlock::iterator(MI)), BB->end());
3846 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
3847
3848 // Next, add the true and fallthrough blocks as its successors.
3849 BB->addSuccessor(copy0MBB);
3850 BB->addSuccessor(sinkMBB);
3851
3852 if (isFPCmp) {
3853 // bc1[tf] cc, sinkMBB
3854 BuildMI(BB, DL, TII->get(Opc))
Simon Dardis8efa9792016-09-09 09:22:52 +00003855 .addReg(MI.getOperand(2).getReg())
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003856 .addMBB(sinkMBB);
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00003857 } else {
3858 // bne rs, $0, sinkMBB
3859 BuildMI(BB, DL, TII->get(Opc))
Simon Dardis8efa9792016-09-09 09:22:52 +00003860 .addReg(MI.getOperand(2).getReg())
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003861 .addReg(Mips::ZERO)
3862 .addMBB(sinkMBB);
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00003863 }
3864
3865 // copy0MBB:
3866 // %FalseValue = ...
3867 // # fallthrough to sinkMBB
3868 BB = copy0MBB;
3869
3870 // Update machine-CFG edges
3871 BB->addSuccessor(sinkMBB);
3872
3873 // sinkMBB:
3874 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
3875 // ...
3876 BB = sinkMBB;
3877
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003878 BuildMI(*BB, BB->begin(), DL, TII->get(Mips::PHI), MI.getOperand(0).getReg())
Simon Dardis8efa9792016-09-09 09:22:52 +00003879 .addReg(MI.getOperand(1).getReg())
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003880 .addMBB(thisMBB)
3881 .addReg(MI.getOperand(3).getReg())
3882 .addMBB(copy0MBB);
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00003883
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003884 MI.eraseFromParent(); // The pseudo instruction is gone now.
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00003885
3886 return BB;
3887}
Daniel Sanders1440bb22015-01-09 17:21:30 +00003888
3889// FIXME? Maybe this could be a TableGen attribute on some registers and
3890// this table could be generated automatically from RegInfo.
Pat Gavlina717f252015-07-09 17:40:29 +00003891unsigned MipsTargetLowering::getRegisterByName(const char* RegName, EVT VT,
3892 SelectionDAG &DAG) const {
Daniel Sanders1440bb22015-01-09 17:21:30 +00003893 // Named registers is expected to be fairly rare. For now, just support $28
3894 // since the linux kernel uses it.
3895 if (Subtarget.isGP64bit()) {
3896 unsigned Reg = StringSwitch<unsigned>(RegName)
3897 .Case("$28", Mips::GP_64)
3898 .Default(0);
3899 if (Reg)
3900 return Reg;
3901 } else {
3902 unsigned Reg = StringSwitch<unsigned>(RegName)
3903 .Case("$28", Mips::GP)
3904 .Default(0);
3905 if (Reg)
3906 return Reg;
3907 }
3908 report_fatal_error("Invalid register name global variable");
3909}