blob: f40cf0d0338d9f6a2e9a052032bb7ef916e9734c [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"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000030#include "llvm/CodeGen/SelectionDAGISel.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000031#include "llvm/CodeGen/ValueTypes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000032#include "llvm/IR/CallingConv.h"
33#include "llvm/IR/DerivedTypes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000034#include "llvm/IR/GlobalVariable.h"
Akira Hatanaka90131ac2012-10-19 21:47:33 +000035#include "llvm/Support/CommandLine.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000036#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000037#include "llvm/Support/ErrorHandling.h"
NAKAMURA Takumie30303f2012-04-21 15:31:45 +000038#include "llvm/Support/raw_ostream.h"
Akira Hatanaka7473b472013-08-14 00:21:25 +000039#include <cctype>
NAKAMURA Takumie30303f2012-04-21 15:31:45 +000040
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000041using namespace llvm;
42
Chandler Carruth84e68b22014-04-22 02:41:26 +000043#define DEBUG_TYPE "mips-lower"
44
Akira Hatanaka90131ac2012-10-19 21:47:33 +000045STATISTIC(NumTailCalls, "Number of tail calls");
46
47static cl::opt<bool>
Akira Hatanaka59f299f2012-11-21 20:21:11 +000048LargeGOT("mxgot", cl::Hidden,
49 cl::desc("MIPS: Enable GOT larger than 64k."), cl::init(false));
50
Akira Hatanaka1cb02422013-05-20 18:07:43 +000051static cl::opt<bool>
Akira Hatanakabe76cd02013-05-21 17:17:59 +000052NoZeroDivCheck("mno-check-zero-division", cl::Hidden,
Akira Hatanaka1cb02422013-05-20 18:07:43 +000053 cl::desc("MIPS: Don't trap on integer division by zero."),
54 cl::init(false));
55
Reed Kotler720c5ca2014-04-17 22:15:34 +000056cl::opt<bool>
57EnableMipsFastISel("mips-fast-isel", cl::Hidden,
58 cl::desc("Allow mips-fast-isel to be used"),
59 cl::init(false));
60
Craig Topper840beec2014-04-04 05:16:06 +000061static const MCPhysReg Mips64DPRegs[8] = {
Akira Hatanakaac8c6692012-10-27 00:29:43 +000062 Mips::D12_64, Mips::D13_64, Mips::D14_64, Mips::D15_64,
63 Mips::D16_64, Mips::D17_64, Mips::D18_64, Mips::D19_64
64};
65
Jia Liuf54f60f2012-02-28 07:46:26 +000066// If I is a shifted mask, set the size (Size) and the first bit of the
Akira Hatanaka73d78b72011-08-18 20:07:42 +000067// mask (Pos), and return true.
Jia Liuf54f60f2012-02-28 07:46:26 +000068// For example, if I is 0x003ff800, (Pos, Size) = (11, 11).
Akira Hatanaka0bb60d892013-03-12 00:16:36 +000069static bool isShiftedMask(uint64_t I, uint64_t &Pos, uint64_t &Size) {
Akira Hatanaka20cee2e2011-12-05 21:26:34 +000070 if (!isShiftedMask_64(I))
Akira Hatanaka4c0a7122013-10-07 19:33:02 +000071 return false;
Akira Hatanaka5360f882011-08-17 02:05:42 +000072
Benjamin Kramer5f6a9072015-02-12 15:35:40 +000073 Size = countPopulation(I);
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +000074 Pos = countTrailingZeros(I);
Akira Hatanaka73d78b72011-08-18 20:07:42 +000075 return true;
Akira Hatanaka5360f882011-08-17 02:05:42 +000076}
77
Akira Hatanaka96ca1822013-03-13 00:54:29 +000078SDValue MipsTargetLowering::getGlobalReg(SelectionDAG &DAG, EVT Ty) const {
Akira Hatanakab049aef2012-02-24 22:34:47 +000079 MipsFunctionInfo *FI = DAG.getMachineFunction().getInfo<MipsFunctionInfo>();
80 return DAG.getRegister(FI->getGlobalBaseReg(), Ty);
81}
82
Akira Hatanakad8f10ce2013-09-27 19:51:35 +000083SDValue MipsTargetLowering::getTargetNode(GlobalAddressSDNode *N, EVT Ty,
84 SelectionDAG &DAG,
Akira Hatanaka96ca1822013-03-13 00:54:29 +000085 unsigned Flag) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +000086 return DAG.getTargetGlobalAddress(N->getGlobal(), SDLoc(N), Ty, 0, Flag);
Akira Hatanakafd04ad42012-11-21 20:26:38 +000087}
88
Akira Hatanakad8f10ce2013-09-27 19:51:35 +000089SDValue MipsTargetLowering::getTargetNode(ExternalSymbolSDNode *N, EVT Ty,
90 SelectionDAG &DAG,
91 unsigned Flag) const {
92 return DAG.getTargetExternalSymbol(N->getSymbol(), Ty, Flag);
93}
94
95SDValue MipsTargetLowering::getTargetNode(BlockAddressSDNode *N, EVT Ty,
96 SelectionDAG &DAG,
97 unsigned Flag) const {
98 return DAG.getTargetBlockAddress(N->getBlockAddress(), Ty, 0, Flag);
99}
100
101SDValue MipsTargetLowering::getTargetNode(JumpTableSDNode *N, EVT Ty,
102 SelectionDAG &DAG,
103 unsigned Flag) const {
104 return DAG.getTargetJumpTable(N->getIndex(), Ty, Flag);
105}
106
107SDValue MipsTargetLowering::getTargetNode(ConstantPoolSDNode *N, EVT Ty,
108 SelectionDAG &DAG,
109 unsigned Flag) const {
110 return DAG.getTargetConstantPool(N->getConstVal(), Ty, N->getAlignment(),
111 N->getOffset(), Flag);
Akira Hatanakafd04ad42012-11-21 20:26:38 +0000112}
113
Chris Lattner5e693ed2009-07-28 03:13:23 +0000114const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +0000115 switch ((MipsISD::NodeType)Opcode) {
116 case MipsISD::FIRST_NUMBER: break;
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000117 case MipsISD::JmpLink: return "MipsISD::JmpLink";
Akira Hatanaka91318df2012-10-19 20:59:39 +0000118 case MipsISD::TailCall: return "MipsISD::TailCall";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000119 case MipsISD::Hi: return "MipsISD::Hi";
120 case MipsISD::Lo: return "MipsISD::Lo";
121 case MipsISD::GPRel: return "MipsISD::GPRel";
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +0000122 case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000123 case MipsISD::Ret: return "MipsISD::Ret";
Akira Hatanakac0b02062013-01-30 00:26:49 +0000124 case MipsISD::EH_RETURN: return "MipsISD::EH_RETURN";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000125 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
126 case MipsISD::FPCmp: return "MipsISD::FPCmp";
127 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
128 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000129 case MipsISD::TruncIntFP: return "MipsISD::TruncIntFP";
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000130 case MipsISD::MFHI: return "MipsISD::MFHI";
131 case MipsISD::MFLO: return "MipsISD::MFLO";
132 case MipsISD::MTLOHI: return "MipsISD::MTLOHI";
Akira Hatanaka28721bd2013-03-30 01:14:04 +0000133 case MipsISD::Mult: return "MipsISD::Mult";
134 case MipsISD::Multu: return "MipsISD::Multu";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000135 case MipsISD::MAdd: return "MipsISD::MAdd";
136 case MipsISD::MAddu: return "MipsISD::MAddu";
137 case MipsISD::MSub: return "MipsISD::MSub";
138 case MipsISD::MSubu: return "MipsISD::MSubu";
139 case MipsISD::DivRem: return "MipsISD::DivRem";
140 case MipsISD::DivRemU: return "MipsISD::DivRemU";
Akira Hatanaka28721bd2013-03-30 01:14:04 +0000141 case MipsISD::DivRem16: return "MipsISD::DivRem16";
142 case MipsISD::DivRemU16: return "MipsISD::DivRemU16";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000143 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
144 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
Akira Hatanakafaa88c02011-12-12 22:38:19 +0000145 case MipsISD::Wrapper: return "MipsISD::Wrapper";
Matthias Braund04893f2015-05-07 21:33:59 +0000146 case MipsISD::DynAlloc: return "MipsISD::DynAlloc";
Akira Hatanakaa4c09bc2011-07-19 23:30:50 +0000147 case MipsISD::Sync: return "MipsISD::Sync";
Akira Hatanaka5360f882011-08-17 02:05:42 +0000148 case MipsISD::Ext: return "MipsISD::Ext";
149 case MipsISD::Ins: return "MipsISD::Ins";
Akira Hatanakab9ebf8d2012-06-02 00:03:12 +0000150 case MipsISD::LWL: return "MipsISD::LWL";
151 case MipsISD::LWR: return "MipsISD::LWR";
152 case MipsISD::SWL: return "MipsISD::SWL";
153 case MipsISD::SWR: return "MipsISD::SWR";
154 case MipsISD::LDL: return "MipsISD::LDL";
155 case MipsISD::LDR: return "MipsISD::LDR";
156 case MipsISD::SDL: return "MipsISD::SDL";
157 case MipsISD::SDR: return "MipsISD::SDR";
Akira Hatanaka233ac532012-09-21 23:52:47 +0000158 case MipsISD::EXTP: return "MipsISD::EXTP";
159 case MipsISD::EXTPDP: return "MipsISD::EXTPDP";
160 case MipsISD::EXTR_S_H: return "MipsISD::EXTR_S_H";
161 case MipsISD::EXTR_W: return "MipsISD::EXTR_W";
162 case MipsISD::EXTR_R_W: return "MipsISD::EXTR_R_W";
163 case MipsISD::EXTR_RS_W: return "MipsISD::EXTR_RS_W";
164 case MipsISD::SHILO: return "MipsISD::SHILO";
165 case MipsISD::MTHLIP: return "MipsISD::MTHLIP";
Matthias Braund04893f2015-05-07 21:33:59 +0000166 case MipsISD::MULSAQ_S_W_PH: return "MipsISD::MULSAQ_S_W_PH";
167 case MipsISD::MAQ_S_W_PHL: return "MipsISD::MAQ_S_W_PHL";
168 case MipsISD::MAQ_S_W_PHR: return "MipsISD::MAQ_S_W_PHR";
169 case MipsISD::MAQ_SA_W_PHL: return "MipsISD::MAQ_SA_W_PHL";
170 case MipsISD::MAQ_SA_W_PHR: return "MipsISD::MAQ_SA_W_PHR";
171 case MipsISD::DPAU_H_QBL: return "MipsISD::DPAU_H_QBL";
172 case MipsISD::DPAU_H_QBR: return "MipsISD::DPAU_H_QBR";
173 case MipsISD::DPSU_H_QBL: return "MipsISD::DPSU_H_QBL";
174 case MipsISD::DPSU_H_QBR: return "MipsISD::DPSU_H_QBR";
175 case MipsISD::DPAQ_S_W_PH: return "MipsISD::DPAQ_S_W_PH";
176 case MipsISD::DPSQ_S_W_PH: return "MipsISD::DPSQ_S_W_PH";
177 case MipsISD::DPAQ_SA_L_W: return "MipsISD::DPAQ_SA_L_W";
178 case MipsISD::DPSQ_SA_L_W: return "MipsISD::DPSQ_SA_L_W";
179 case MipsISD::DPA_W_PH: return "MipsISD::DPA_W_PH";
180 case MipsISD::DPS_W_PH: return "MipsISD::DPS_W_PH";
181 case MipsISD::DPAQX_S_W_PH: return "MipsISD::DPAQX_S_W_PH";
182 case MipsISD::DPAQX_SA_W_PH: return "MipsISD::DPAQX_SA_W_PH";
183 case MipsISD::DPAX_W_PH: return "MipsISD::DPAX_W_PH";
184 case MipsISD::DPSX_W_PH: return "MipsISD::DPSX_W_PH";
185 case MipsISD::DPSQX_S_W_PH: return "MipsISD::DPSQX_S_W_PH";
186 case MipsISD::DPSQX_SA_W_PH: return "MipsISD::DPSQX_SA_W_PH";
187 case MipsISD::MULSA_W_PH: return "MipsISD::MULSA_W_PH";
Akira Hatanaka233ac532012-09-21 23:52:47 +0000188 case MipsISD::MULT: return "MipsISD::MULT";
189 case MipsISD::MULTU: return "MipsISD::MULTU";
Jia Liu434874d2013-03-04 01:06:54 +0000190 case MipsISD::MADD_DSP: return "MipsISD::MADD_DSP";
Akira Hatanaka233ac532012-09-21 23:52:47 +0000191 case MipsISD::MADDU_DSP: return "MipsISD::MADDU_DSP";
192 case MipsISD::MSUB_DSP: return "MipsISD::MSUB_DSP";
193 case MipsISD::MSUBU_DSP: return "MipsISD::MSUBU_DSP";
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000194 case MipsISD::SHLL_DSP: return "MipsISD::SHLL_DSP";
195 case MipsISD::SHRA_DSP: return "MipsISD::SHRA_DSP";
196 case MipsISD::SHRL_DSP: return "MipsISD::SHRL_DSP";
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000197 case MipsISD::SETCC_DSP: return "MipsISD::SETCC_DSP";
198 case MipsISD::SELECT_CC_DSP: return "MipsISD::SELECT_CC_DSP";
Daniel Sandersce09d072013-08-28 12:14:50 +0000199 case MipsISD::VALL_ZERO: return "MipsISD::VALL_ZERO";
200 case MipsISD::VANY_ZERO: return "MipsISD::VANY_ZERO";
201 case MipsISD::VALL_NONZERO: return "MipsISD::VALL_NONZERO";
202 case MipsISD::VANY_NONZERO: return "MipsISD::VANY_NONZERO";
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000203 case MipsISD::VCEQ: return "MipsISD::VCEQ";
204 case MipsISD::VCLE_S: return "MipsISD::VCLE_S";
205 case MipsISD::VCLE_U: return "MipsISD::VCLE_U";
206 case MipsISD::VCLT_S: return "MipsISD::VCLT_S";
207 case MipsISD::VCLT_U: return "MipsISD::VCLT_U";
Daniel Sanders3ce56622013-09-24 12:18:31 +0000208 case MipsISD::VSMAX: return "MipsISD::VSMAX";
209 case MipsISD::VSMIN: return "MipsISD::VSMIN";
210 case MipsISD::VUMAX: return "MipsISD::VUMAX";
211 case MipsISD::VUMIN: return "MipsISD::VUMIN";
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000212 case MipsISD::VEXTRACT_SEXT_ELT: return "MipsISD::VEXTRACT_SEXT_ELT";
213 case MipsISD::VEXTRACT_ZEXT_ELT: return "MipsISD::VEXTRACT_ZEXT_ELT";
Daniel Sandersf7456c72013-09-23 13:22:24 +0000214 case MipsISD::VNOR: return "MipsISD::VNOR";
Daniel Sanderse5087042013-09-24 14:02:15 +0000215 case MipsISD::VSHF: return "MipsISD::VSHF";
Daniel Sanders26307182013-09-24 14:20:00 +0000216 case MipsISD::SHF: return "MipsISD::SHF";
Daniel Sanders2ed228b2013-09-24 14:36:12 +0000217 case MipsISD::ILVEV: return "MipsISD::ILVEV";
218 case MipsISD::ILVOD: return "MipsISD::ILVOD";
219 case MipsISD::ILVL: return "MipsISD::ILVL";
220 case MipsISD::ILVR: return "MipsISD::ILVR";
Daniel Sandersfae5f2a2013-09-24 14:53:25 +0000221 case MipsISD::PCKEV: return "MipsISD::PCKEV";
222 case MipsISD::PCKOD: return "MipsISD::PCKOD";
Daniel Sandersb50ccf82014-04-01 10:35:28 +0000223 case MipsISD::INSVE: return "MipsISD::INSVE";
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000224 }
Matthias Braund04893f2015-05-07 21:33:59 +0000225 return nullptr;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000226}
227
Eric Christopherb1526602014-09-19 23:30:42 +0000228MipsTargetLowering::MipsTargetLowering(const MipsTargetMachine &TM,
Eric Christopher8924d272014-07-18 23:25:04 +0000229 const MipsSubtarget &STI)
Eric Christopher96e72c62015-01-29 23:27:36 +0000230 : TargetLowering(TM), Subtarget(STI), ABI(TM.getABI()) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000231 // Mips does not have i1 type, so use i32 for
Wesley Peck527da1b2010-11-23 03:31:01 +0000232 // setcc operations results (slt, sgt, ...).
Duncan Sands8d6e2e12008-11-23 15:47:28 +0000233 setBooleanContents(ZeroOrOneBooleanContent);
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000234 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Daniel Sanderscbd44c52014-07-10 10:18:12 +0000235 // The cmp.cond.fmt instruction in MIPS32r6/MIPS64r6 uses 0 and -1 like MSA
236 // does. Integer booleans still use 0 and 1.
Eric Christopher1c29a652014-07-18 22:55:25 +0000237 if (Subtarget.hasMips32r6())
Daniel Sanderscbd44c52014-07-10 10:18:12 +0000238 setBooleanContents(ZeroOrOneBooleanContent,
239 ZeroOrNegativeOneBooleanContent);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000240
Wesley Peck527da1b2010-11-23 03:31:01 +0000241 // Load extented operations for i1 types must be promoted
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000242 for (MVT VT : MVT::integer_valuetypes()) {
243 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
244 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
245 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
246 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000247
Pirama Arumuga Nainar34056de2015-04-20 20:15:36 +0000248 // MIPS doesn't have extending float->double load/store. Set LoadExtAction
249 // for f32, f16
250 for (MVT VT : MVT::fp_valuetypes()) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000251 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
Pirama Arumuga Nainar34056de2015-04-20 20:15:36 +0000252 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f16, Expand);
253 }
254
255 // Set LoadExtAction for f16 vectors to Expand
256 for (MVT VT : MVT::fp_vector_valuetypes()) {
257 MVT F16VT = MVT::getVectorVT(MVT::f16, VT.getVectorNumElements());
258 if (F16VT.isValid())
259 setLoadExtAction(ISD::EXTLOAD, VT, F16VT, Expand);
260 }
261
262 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
263 setTruncStoreAction(MVT::f64, MVT::f16, Expand);
264
Owen Anderson9f944592009-08-11 20:47:22 +0000265 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman39d6faa2009-07-17 02:28:12 +0000266
Wesley Peck527da1b2010-11-23 03:31:01 +0000267 // Used by legalize types to correctly generate the setcc result.
268 // Without this, every float setcc comes with a AND/OR with the result,
269 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +0000270 // which is used implicitly by brcond and select operations.
Owen Anderson9f944592009-08-11 20:47:22 +0000271 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +0000272
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000273 // Mips Custom Operations
Akira Hatanaka0f693a82013-03-06 21:32:03 +0000274 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000275 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000276 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000277 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
278 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
279 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
280 setOperationAction(ISD::SELECT, MVT::f32, Custom);
281 setOperationAction(ISD::SELECT, MVT::f64, Custom);
282 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Akira Hatanaka24cf4e32012-07-11 19:32:27 +0000283 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
284 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Akira Hatanakab7f78592012-03-09 23:46:03 +0000285 setOperationAction(ISD::SETCC, MVT::f32, Custom);
286 setOperationAction(ISD::SETCC, MVT::f64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000287 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000288 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
289 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000290 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000291
Eric Christopher1c29a652014-07-18 22:55:25 +0000292 if (Subtarget.isGP64bit()) {
Akira Hatanakada00aa82012-03-10 00:03:50 +0000293 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
294 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
295 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
296 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
297 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
298 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Akira Hatanaka019e5922012-06-02 00:04:42 +0000299 setOperationAction(ISD::LOAD, MVT::i64, Custom);
300 setOperationAction(ISD::STORE, MVT::i64, Custom);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000301 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +0000302 setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom);
303 setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom);
304 setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000305 }
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +0000306
Eric Christopher1c29a652014-07-18 22:55:25 +0000307 if (!Subtarget.isGP64bit()) {
Akira Hatanaka0a8ab712012-05-09 00:55:21 +0000308 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
309 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
310 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
311 }
312
Akira Hatanaka28e02ec2012-11-07 19:10:58 +0000313 setOperationAction(ISD::ADD, MVT::i32, Custom);
Eric Christopher1c29a652014-07-18 22:55:25 +0000314 if (Subtarget.isGP64bit())
Akira Hatanaka28e02ec2012-11-07 19:10:58 +0000315 setOperationAction(ISD::ADD, MVT::i64, Custom);
316
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000317 setOperationAction(ISD::SDIV, MVT::i32, Expand);
318 setOperationAction(ISD::SREM, MVT::i32, Expand);
319 setOperationAction(ISD::UDIV, MVT::i32, Expand);
320 setOperationAction(ISD::UREM, MVT::i32, Expand);
Akira Hatanakab1538f92011-10-03 21:06:13 +0000321 setOperationAction(ISD::SDIV, MVT::i64, Expand);
322 setOperationAction(ISD::SREM, MVT::i64, Expand);
323 setOperationAction(ISD::UDIV, MVT::i64, Expand);
324 setOperationAction(ISD::UREM, MVT::i64, Expand);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000325
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000326 // Operations not directly supported by Mips.
Tom Stellardb1588fc2013-03-08 15:36:57 +0000327 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
328 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
329 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
330 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
Tom Stellard3787b122014-06-10 16:01:29 +0000331 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
332 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000333 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Akira Hatanaka79aed152011-12-20 23:40:56 +0000334 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000335 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Akira Hatanaka79aed152011-12-20 23:40:56 +0000336 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000337 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Eric Christopher1c29a652014-07-18 22:55:25 +0000338 if (Subtarget.hasCnMips()) {
Kai Nacke93fe5e82014-03-20 11:51:58 +0000339 setOperationAction(ISD::CTPOP, MVT::i32, Legal);
340 setOperationAction(ISD::CTPOP, MVT::i64, Legal);
341 } else {
342 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
343 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
344 }
Owen Anderson9f944592009-08-11 20:47:22 +0000345 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
Akira Hatanaka410ce9c2011-12-21 00:14:05 +0000346 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000347 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
348 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
349 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
350 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000351 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Akira Hatanaka7ba8a8d2011-09-30 18:51:46 +0000352 setOperationAction(ISD::ROTL, MVT::i64, Expand);
Akira Hatanaka33a25af2012-07-31 20:54:48 +0000353 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
354 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Bruno Cardoso Lopesd47180e2010-12-09 17:32:30 +0000355
Eric Christopher1c29a652014-07-18 22:55:25 +0000356 if (!Subtarget.hasMips32r2())
Bruno Cardoso Lopesd47180e2010-12-09 17:32:30 +0000357 setOperationAction(ISD::ROTR, MVT::i32, Expand);
358
Eric Christopher1c29a652014-07-18 22:55:25 +0000359 if (!Subtarget.hasMips64r2())
Akira Hatanaka7ba8a8d2011-09-30 18:51:46 +0000360 setOperationAction(ISD::ROTR, MVT::i64, Expand);
361
Owen Anderson9f944592009-08-11 20:47:22 +0000362 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes22b69db2011-03-04 18:54:14 +0000363 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000364 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes22b69db2011-03-04 18:54:14 +0000365 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000366 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
367 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000368 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
369 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanakadfb8cda2011-05-23 22:23:58 +0000370 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000371 setOperationAction(ISD::FLOG, MVT::f32, Expand);
372 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
373 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
374 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000375 setOperationAction(ISD::FMA, MVT::f32, Expand);
376 setOperationAction(ISD::FMA, MVT::f64, Expand);
Akira Hatanaka0603ad82012-03-29 18:43:11 +0000377 setOperationAction(ISD::FREM, MVT::f32, Expand);
378 setOperationAction(ISD::FREM, MVT::f64, Expand);
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000379
Pirama Arumuga Nainar34056de2015-04-20 20:15:36 +0000380 // Lower f16 conversion operations into library calls
381 setOperationAction(ISD::FP16_TO_FP, MVT::f32, Expand);
382 setOperationAction(ISD::FP_TO_FP16, MVT::f32, Expand);
383 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand);
384 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Expand);
385
Akira Hatanakac0b02062013-01-30 00:26:49 +0000386 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
387
Daniel Sanders2b553d42014-08-01 09:17:39 +0000388 setOperationAction(ISD::VASTART, MVT::Other, Custom);
389 setOperationAction(ISD::VAARG, MVT::Other, Custom);
Bruno Cardoso Lopes048ffab2011-03-09 19:22:22 +0000390 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
391 setOperationAction(ISD::VAEND, MVT::Other, Expand);
392
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000393 // Use the default for now
Owen Anderson9f944592009-08-11 20:47:22 +0000394 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
395 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman26a48482011-07-27 22:21:52 +0000396
Jia Liuf54f60f2012-02-28 07:46:26 +0000397 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
398 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
399 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
400 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
Eli Friedman7dfa7912011-08-29 18:23:02 +0000401
Eli Friedman30a49e92011-08-03 21:06:02 +0000402 setInsertFencesForAtomic(true);
403
Eric Christopher1c29a652014-07-18 22:55:25 +0000404 if (!Subtarget.hasMips32r2()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000405 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
406 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000407 }
408
Daniel Sanders070fd1c2014-05-12 12:41:59 +0000409 // MIPS16 lacks MIPS32's clz and clo instructions.
Eric Christopher1c29a652014-07-18 22:55:25 +0000410 if (!Subtarget.hasMips32() || Subtarget.inMips16Mode())
Owen Anderson9f944592009-08-11 20:47:22 +0000411 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Eric Christopher1c29a652014-07-18 22:55:25 +0000412 if (!Subtarget.hasMips64())
Akira Hatanaka1d8efab2011-12-21 00:20:27 +0000413 setOperationAction(ISD::CTLZ, MVT::i64, Expand);
Bruno Cardoso Lopes93da7e62008-08-08 06:16:31 +0000414
Eric Christopher1c29a652014-07-18 22:55:25 +0000415 if (!Subtarget.hasMips32r2())
Owen Anderson9f944592009-08-11 20:47:22 +0000416 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Eric Christopher1c29a652014-07-18 22:55:25 +0000417 if (!Subtarget.hasMips64r2())
Akira Hatanaka4706ac92011-12-20 23:56:43 +0000418 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000419
Eric Christopher1c29a652014-07-18 22:55:25 +0000420 if (Subtarget.isGP64bit()) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000421 setLoadExtAction(ISD::SEXTLOAD, MVT::i64, MVT::i32, Custom);
422 setLoadExtAction(ISD::ZEXTLOAD, MVT::i64, MVT::i32, Custom);
423 setLoadExtAction(ISD::EXTLOAD, MVT::i64, MVT::i32, Custom);
Akira Hatanaka019e5922012-06-02 00:04:42 +0000424 setTruncStoreAction(MVT::i64, MVT::i32, Custom);
425 }
426
Akira Hatanakaa3d9ab92013-07-26 20:58:55 +0000427 setOperationAction(ISD::TRAP, MVT::Other, Legal);
428
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000429 setTargetDAGCombine(ISD::SDIVREM);
430 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka5e152182012-03-08 03:26:37 +0000431 setTargetDAGCombine(ISD::SELECT);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000432 setTargetDAGCombine(ISD::AND);
433 setTargetDAGCombine(ISD::OR);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000434 setTargetDAGCombine(ISD::ADD);
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000435
Eric Christopher1c29a652014-07-18 22:55:25 +0000436 setMinFunctionAlignment(Subtarget.isGP64bit() ? 3 : 2);
Eli Friedman2518f832011-05-06 20:34:06 +0000437
Daniel Sanders2b553d42014-08-01 09:17:39 +0000438 // The arguments on the stack are defined in terms of 4-byte slots on O32
439 // and 8-byte slots on N32/N64.
Eric Christopher96e72c62015-01-29 23:27:36 +0000440 setMinStackArgumentAlignment((ABI.IsN32() || ABI.IsN64()) ? 8 : 4);
Daniel Sanders2b553d42014-08-01 09:17:39 +0000441
Eric Christopher96e72c62015-01-29 23:27:36 +0000442 setStackPointerRegisterToSaveRestore(ABI.IsN64() ? Mips::SP_64 : Mips::SP);
Akira Hatanakaaa560002011-05-26 18:59:03 +0000443
Eric Christopher96e72c62015-01-29 23:27:36 +0000444 setExceptionPointerRegister(ABI.IsN64() ? Mips::A0_64 : Mips::A0);
445 setExceptionSelectorRegister(ABI.IsN64() ? Mips::A1_64 : Mips::A1);
Akira Hatanaka1daf8c22012-06-13 19:33:32 +0000446
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000447 MaxStoresPerMemcpy = 16;
Zoran Jovanovicff9d5f32013-12-19 16:12:56 +0000448
Eric Christopher1c29a652014-07-18 22:55:25 +0000449 isMicroMips = Subtarget.inMicroMipsMode();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000450}
451
Eric Christopherb1526602014-09-19 23:30:42 +0000452const MipsTargetLowering *MipsTargetLowering::create(const MipsTargetMachine &TM,
Eric Christopher8924d272014-07-18 23:25:04 +0000453 const MipsSubtarget &STI) {
454 if (STI.inMips16Mode())
455 return llvm::createMips16TargetLowering(TM, STI);
Jia Liuf54f60f2012-02-28 07:46:26 +0000456
Eric Christopher8924d272014-07-18 23:25:04 +0000457 return llvm::createMipsSETargetLowering(TM, STI);
Akira Hatanaka2fcc1cf2011-08-12 21:30:06 +0000458}
459
Reed Kotler720c5ca2014-04-17 22:15:34 +0000460// Create a fast isel object.
461FastISel *
462MipsTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
463 const TargetLibraryInfo *libInfo) const {
464 if (!EnableMipsFastISel)
465 return TargetLowering::createFastISel(funcInfo, libInfo);
466 return Mips::createFastISel(funcInfo, libInfo);
467}
468
Mehdi Amini44ede332015-07-09 02:09:04 +0000469EVT MipsTargetLowering::getSetCCResultType(const DataLayout &, LLVMContext &,
470 EVT VT) const {
Akira Hatanakab13b3332013-01-04 20:06:01 +0000471 if (!VT.isVector())
472 return MVT::i32;
473 return VT.changeVectorElementTypeToInteger();
Scott Michela6729e82008-03-10 15:42:14 +0000474}
475
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000476static SDValue performDivRemCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000477 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000478 const MipsSubtarget &Subtarget) {
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000479 if (DCI.isBeforeLegalizeOps())
480 return SDValue();
481
Akira Hatanakab1538f92011-10-03 21:06:13 +0000482 EVT Ty = N->getValueType(0);
Akira Hatanaka8002a3f2013-08-14 00:47:08 +0000483 unsigned LO = (Ty == MVT::i32) ? Mips::LO0 : Mips::LO0_64;
484 unsigned HI = (Ty == MVT::i32) ? Mips::HI0 : Mips::HI0_64;
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000485 unsigned Opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem16 :
486 MipsISD::DivRemU16;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000487 SDLoc DL(N);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000488
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000489 SDValue DivRem = DAG.getNode(Opc, DL, MVT::Glue,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000490 N->getOperand(0), N->getOperand(1));
491 SDValue InChain = DAG.getEntryNode();
492 SDValue InGlue = DivRem;
493
494 // insert MFLO
495 if (N->hasAnyUseOfValue(0)) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000496 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, DL, LO, Ty,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000497 InGlue);
498 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
499 InChain = CopyFromLo.getValue(1);
500 InGlue = CopyFromLo.getValue(2);
501 }
502
503 // insert MFHI
504 if (N->hasAnyUseOfValue(1)) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000505 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, DL,
Akira Hatanakab1538f92011-10-03 21:06:13 +0000506 HI, Ty, InGlue);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000507 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
508 }
509
510 return SDValue();
511}
512
Akira Hatanaka89af5892013-04-18 01:00:46 +0000513static Mips::CondCode condCodeToFCC(ISD::CondCode CC) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000514 switch (CC) {
515 default: llvm_unreachable("Unknown fp condition code!");
516 case ISD::SETEQ:
517 case ISD::SETOEQ: return Mips::FCOND_OEQ;
518 case ISD::SETUNE: return Mips::FCOND_UNE;
519 case ISD::SETLT:
520 case ISD::SETOLT: return Mips::FCOND_OLT;
521 case ISD::SETGT:
522 case ISD::SETOGT: return Mips::FCOND_OGT;
523 case ISD::SETLE:
524 case ISD::SETOLE: return Mips::FCOND_OLE;
525 case ISD::SETGE:
526 case ISD::SETOGE: return Mips::FCOND_OGE;
527 case ISD::SETULT: return Mips::FCOND_ULT;
528 case ISD::SETULE: return Mips::FCOND_ULE;
529 case ISD::SETUGT: return Mips::FCOND_UGT;
530 case ISD::SETUGE: return Mips::FCOND_UGE;
531 case ISD::SETUO: return Mips::FCOND_UN;
532 case ISD::SETO: return Mips::FCOND_OR;
533 case ISD::SETNE:
534 case ISD::SETONE: return Mips::FCOND_ONE;
535 case ISD::SETUEQ: return Mips::FCOND_UEQ;
536 }
537}
538
539
Akira Hatanakaf0ea5002013-03-30 01:16:38 +0000540/// This function returns true if the floating point conditional branches and
541/// conditional moves which use condition code CC should be inverted.
542static bool invertFPCondCodeUser(Mips::CondCode CC) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000543 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
544 return false;
545
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000546 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
547 "Illegal Condition Code");
Akira Hatanakaa5352702011-03-31 18:26:17 +0000548
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000549 return true;
Akira Hatanakaa5352702011-03-31 18:26:17 +0000550}
551
552// Creates and returns an FPCmp node from a setcc node.
553// Returns Op if setcc is not a floating point comparison.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000554static SDValue createFPCmp(SelectionDAG &DAG, const SDValue &Op) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000555 // must be a SETCC node
556 if (Op.getOpcode() != ISD::SETCC)
557 return Op;
558
559 SDValue LHS = Op.getOperand(0);
560
561 if (!LHS.getValueType().isFloatingPoint())
562 return Op;
563
564 SDValue RHS = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000565 SDLoc DL(Op);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000566
Akira Hatanakaaef55c82011-04-15 21:00:26 +0000567 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
568 // node if necessary.
Akira Hatanakaa5352702011-03-31 18:26:17 +0000569 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
570
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000571 return DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000572 DAG.getConstant(condCodeToFCC(CC), DL, MVT::i32));
Akira Hatanakaa5352702011-03-31 18:26:17 +0000573}
574
575// Creates and returns a CMovFPT/F node.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000576static SDValue createCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000577 SDValue False, SDLoc DL) {
Akira Hatanakaf0ea5002013-03-30 01:16:38 +0000578 ConstantSDNode *CC = cast<ConstantSDNode>(Cond.getOperand(2));
579 bool invert = invertFPCondCodeUser((Mips::CondCode)CC->getSExtValue());
Akira Hatanaka8bce21c2013-07-26 20:51:20 +0000580 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000581
582 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
Akira Hatanaka8bce21c2013-07-26 20:51:20 +0000583 True.getValueType(), True, FCC0, False, Cond);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000584}
585
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000586static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000587 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000588 const MipsSubtarget &Subtarget) {
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000589 if (DCI.isBeforeLegalizeOps())
590 return SDValue();
591
592 SDValue SetCC = N->getOperand(0);
593
594 if ((SetCC.getOpcode() != ISD::SETCC) ||
595 !SetCC.getOperand(0).getValueType().isInteger())
596 return SDValue();
597
598 SDValue False = N->getOperand(2);
599 EVT FalseTy = False.getValueType();
600
601 if (!FalseTy.isInteger())
602 return SDValue();
603
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000604 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(False);
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000605
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000606 // If the RHS (False) is 0, we swap the order of the operands
607 // of ISD::SELECT (obviously also inverting the condition) so that we can
608 // take advantage of conditional moves using the $0 register.
609 // Example:
610 // return (a != 0) ? x : 0;
611 // load $reg, x
612 // movz $reg, $0, a
613 if (!FalseC)
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000614 return SDValue();
615
Andrew Trickef9de2a2013-05-25 02:42:55 +0000616 const SDLoc DL(N);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000617
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000618 if (!FalseC->getZExtValue()) {
619 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
620 SDValue True = N->getOperand(1);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000621
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000622 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
623 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
624
625 return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True);
626 }
627
Matheus Almeidaa6beac12013-12-05 12:07:05 +0000628 // If both operands are integer constants there's a possibility that we
629 // can do some interesting optimizations.
630 SDValue True = N->getOperand(1);
631 ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(True);
632
633 if (!TrueC || !True.getValueType().isInteger())
634 return SDValue();
635
636 // We'll also ignore MVT::i64 operands as this optimizations proves
637 // to be ineffective because of the required sign extensions as the result
638 // of a SETCC operator is always MVT::i32 for non-vector types.
639 if (True.getValueType() == MVT::i64)
640 return SDValue();
641
642 int64_t Diff = TrueC->getSExtValue() - FalseC->getSExtValue();
643
644 // 1) (a < x) ? y : y-1
645 // slti $reg1, a, x
646 // addiu $reg2, $reg1, y-1
647 if (Diff == 1)
648 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, False);
649
650 // 2) (a < x) ? y-1 : y
651 // slti $reg1, a, x
652 // xor $reg1, $reg1, 1
653 // addiu $reg2, $reg1, y-1
654 if (Diff == -1) {
655 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
656 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
657 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
658 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, True);
659 }
660
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000661 // Couldn't optimize.
662 return SDValue();
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000663}
664
Vasileios Kalintirise741eb22015-03-02 12:47:32 +0000665static SDValue performCMovFPCombine(SDNode *N, SelectionDAG &DAG,
666 TargetLowering::DAGCombinerInfo &DCI,
667 const MipsSubtarget &Subtarget) {
668 if (DCI.isBeforeLegalizeOps())
669 return SDValue();
670
671 SDValue ValueIfTrue = N->getOperand(0), ValueIfFalse = N->getOperand(2);
672
673 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(ValueIfFalse);
674 if (!FalseC || FalseC->getZExtValue())
675 return SDValue();
676
677 // Since RHS (False) is 0, we swap the order of the True/False operands
678 // (obviously also inverting the condition) so that we can
679 // take advantage of conditional moves using the $0 register.
680 // Example:
681 // return (a != 0) ? x : 0;
682 // load $reg, x
683 // movz $reg, $0, a
684 unsigned Opc = (N->getOpcode() == MipsISD::CMovFP_T) ? MipsISD::CMovFP_F :
685 MipsISD::CMovFP_T;
686
687 SDValue FCC = N->getOperand(1), Glue = N->getOperand(3);
Vasileios Kalintiris2ef28882015-03-04 12:10:18 +0000688 return DAG.getNode(Opc, SDLoc(N), ValueIfFalse.getValueType(),
689 ValueIfFalse, FCC, ValueIfTrue, Glue);
Vasileios Kalintirise741eb22015-03-02 12:47:32 +0000690}
691
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000692static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000693 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000694 const MipsSubtarget &Subtarget) {
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000695 // Pattern match EXT.
696 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
697 // => ext $dst, $src, size, pos
Eric Christopher1c29a652014-07-18 22:55:25 +0000698 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasExtractInsert())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000699 return SDValue();
700
701 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000702 unsigned ShiftRightOpc = ShiftRight.getOpcode();
703
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000704 // Op's first operand must be a shift right.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000705 if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL)
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000706 return SDValue();
707
708 // The second operand of the shift must be an immediate.
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000709 ConstantSDNode *CN;
710 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
711 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000712
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000713 uint64_t Pos = CN->getZExtValue();
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000714 uint64_t SMPos, SMSize;
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000715
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000716 // Op's second operand must be a shifted mask.
717 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000718 !isShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000719 return SDValue();
720
721 // Return if the shifted mask does not start at bit 0 or the sum of its size
722 // and Pos exceeds the word's size.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000723 EVT ValTy = N->getValueType(0);
724 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000725 return SDValue();
726
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000727 SDLoc DL(N);
728 return DAG.getNode(MipsISD::Ext, DL, ValTy,
729 ShiftRight.getOperand(0),
730 DAG.getConstant(Pos, DL, MVT::i32),
731 DAG.getConstant(SMSize, DL, MVT::i32));
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000732}
Jia Liuf54f60f2012-02-28 07:46:26 +0000733
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000734static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000735 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000736 const MipsSubtarget &Subtarget) {
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000737 // Pattern match INS.
738 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
Jia Liuf54f60f2012-02-28 07:46:26 +0000739 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000740 // => ins $dst, $src, size, pos, $src1
Eric Christopher1c29a652014-07-18 22:55:25 +0000741 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasExtractInsert())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000742 return SDValue();
743
744 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
745 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
746 ConstantSDNode *CN;
747
748 // See if Op's first operand matches (and $src1 , mask0).
749 if (And0.getOpcode() != ISD::AND)
750 return SDValue();
751
752 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000753 !isShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000754 return SDValue();
755
756 // See if Op's second operand matches (and (shl $src, pos), mask1).
757 if (And1.getOpcode() != ISD::AND)
758 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000759
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000760 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000761 !isShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000762 return SDValue();
763
764 // The shift masks must have the same position and size.
765 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
766 return SDValue();
767
768 SDValue Shl = And1.getOperand(0);
769 if (Shl.getOpcode() != ISD::SHL)
770 return SDValue();
771
772 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
773 return SDValue();
774
775 unsigned Shamt = CN->getZExtValue();
776
777 // Return if the shift amount and the first bit position of mask are not the
Jia Liuf54f60f2012-02-28 07:46:26 +0000778 // same.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000779 EVT ValTy = N->getValueType(0);
780 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000781 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000782
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000783 SDLoc DL(N);
784 return DAG.getNode(MipsISD::Ins, DL, ValTy, Shl.getOperand(0),
785 DAG.getConstant(SMPos0, DL, MVT::i32),
786 DAG.getConstant(SMSize0, DL, MVT::i32),
787 And0.getOperand(0));
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000788}
Jia Liuf54f60f2012-02-28 07:46:26 +0000789
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000790static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000791 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000792 const MipsSubtarget &Subtarget) {
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000793 // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt))
794
795 if (DCI.isBeforeLegalizeOps())
796 return SDValue();
797
798 SDValue Add = N->getOperand(1);
799
800 if (Add.getOpcode() != ISD::ADD)
801 return SDValue();
802
803 SDValue Lo = Add.getOperand(1);
804
805 if ((Lo.getOpcode() != MipsISD::Lo) ||
806 (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable))
807 return SDValue();
808
809 EVT ValTy = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000810 SDLoc DL(N);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000811
812 SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, N->getOperand(0),
813 Add.getOperand(0));
814 return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo);
815}
816
Bruno Cardoso Lopes61a61e92011-02-10 18:05:10 +0000817SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000818 const {
819 SelectionDAG &DAG = DCI.DAG;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000820 unsigned Opc = N->getOpcode();
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000821
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000822 switch (Opc) {
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000823 default: break;
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000824 case ISD::SDIVREM:
825 case ISD::UDIVREM:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000826 return performDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000827 case ISD::SELECT:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000828 return performSELECTCombine(N, DAG, DCI, Subtarget);
Vasileios Kalintirise741eb22015-03-02 12:47:32 +0000829 case MipsISD::CMovFP_F:
830 case MipsISD::CMovFP_T:
831 return performCMovFPCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000832 case ISD::AND:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000833 return performANDCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000834 case ISD::OR:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000835 return performORCombine(N, DAG, DCI, Subtarget);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000836 case ISD::ADD:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000837 return performADDCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000838 }
839
840 return SDValue();
841}
842
Akira Hatanakafabb8cf2012-09-21 23:58:31 +0000843void
844MipsTargetLowering::LowerOperationWrapper(SDNode *N,
845 SmallVectorImpl<SDValue> &Results,
846 SelectionDAG &DAG) const {
847 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
848
849 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
850 Results.push_back(Res.getValue(I));
851}
852
853void
854MipsTargetLowering::ReplaceNodeResults(SDNode *N,
855 SmallVectorImpl<SDValue> &Results,
856 SelectionDAG &DAG) const {
Akira Hatanaka9da442f2013-04-30 21:17:07 +0000857 return LowerOperationWrapper(N, Results, DAG);
Akira Hatanakafabb8cf2012-09-21 23:58:31 +0000858}
859
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000860SDValue MipsTargetLowering::
Dan Gohman21cea8a2010-04-17 15:26:15 +0000861LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000862{
Wesley Peck527da1b2010-11-23 03:31:01 +0000863 switch (Op.getOpcode())
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000864 {
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000865 case ISD::BR_JT: return lowerBR_JT(Op, DAG);
866 case ISD::BRCOND: return lowerBRCOND(Op, DAG);
867 case ISD::ConstantPool: return lowerConstantPool(Op, DAG);
868 case ISD::GlobalAddress: return lowerGlobalAddress(Op, DAG);
869 case ISD::BlockAddress: return lowerBlockAddress(Op, DAG);
870 case ISD::GlobalTLSAddress: return lowerGlobalTLSAddress(Op, DAG);
871 case ISD::JumpTable: return lowerJumpTable(Op, DAG);
872 case ISD::SELECT: return lowerSELECT(Op, DAG);
873 case ISD::SELECT_CC: return lowerSELECT_CC(Op, DAG);
874 case ISD::SETCC: return lowerSETCC(Op, DAG);
875 case ISD::VASTART: return lowerVASTART(Op, DAG);
Daniel Sanders2b553d42014-08-01 09:17:39 +0000876 case ISD::VAARG: return lowerVAARG(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000877 case ISD::FCOPYSIGN: return lowerFCOPYSIGN(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000878 case ISD::FRAMEADDR: return lowerFRAMEADDR(Op, DAG);
879 case ISD::RETURNADDR: return lowerRETURNADDR(Op, DAG);
880 case ISD::EH_RETURN: return lowerEH_RETURN(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000881 case ISD::ATOMIC_FENCE: return lowerATOMIC_FENCE(Op, DAG);
882 case ISD::SHL_PARTS: return lowerShiftLeftParts(Op, DAG);
883 case ISD::SRA_PARTS: return lowerShiftRightParts(Op, DAG, true);
884 case ISD::SRL_PARTS: return lowerShiftRightParts(Op, DAG, false);
885 case ISD::LOAD: return lowerLOAD(Op, DAG);
886 case ISD::STORE: return lowerSTORE(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000887 case ISD::ADD: return lowerADD(Op, DAG);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000888 case ISD::FP_TO_SINT: return lowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000889 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000890 return SDValue();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000891}
892
Akira Hatanakae2489122011-04-15 21:51:11 +0000893//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000894// Lower helper functions
Akira Hatanakae2489122011-04-15 21:51:11 +0000895//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000896
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000897// addLiveIn - This helper function adds the specified physical register to the
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000898// MachineFunction as a live in value. It also creates a corresponding
899// virtual register for it.
900static unsigned
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000901addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000902{
Chris Lattnera10fff52007-12-31 04:13:23 +0000903 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
904 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000905 return VReg;
906}
907
Daniel Sanders308181e2014-06-12 10:44:10 +0000908static MachineBasicBlock *insertDivByZeroTrap(MachineInstr *MI,
909 MachineBasicBlock &MBB,
910 const TargetInstrInfo &TII,
911 bool Is64Bit) {
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000912 if (NoZeroDivCheck)
913 return &MBB;
914
915 // Insert instruction "teq $divisor_reg, $zero, 7".
916 MachineBasicBlock::iterator I(MI);
917 MachineInstrBuilder MIB;
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000918 MachineOperand &Divisor = MI->getOperand(2);
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000919 MIB = BuildMI(MBB, std::next(I), MI->getDebugLoc(), TII.get(Mips::TEQ))
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000920 .addReg(Divisor.getReg(), getKillRegState(Divisor.isKill()))
921 .addReg(Mips::ZERO).addImm(7);
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000922
923 // Use the 32-bit sub-register if this is a 64-bit division.
924 if (Is64Bit)
925 MIB->getOperand(0).setSubReg(Mips::sub_32);
926
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000927 // Clear Divisor's kill flag.
928 Divisor.setIsKill(false);
Daniel Sanders308181e2014-06-12 10:44:10 +0000929
930 // We would normally delete the original instruction here but in this case
931 // we only needed to inject an additional instruction rather than replace it.
932
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000933 return &MBB;
934}
935
Akira Hatanakae4bd0542012-09-27 02:15:57 +0000936MachineBasicBlock *
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +0000937MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +0000938 MachineBasicBlock *BB) const {
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +0000939 switch (MI->getOpcode()) {
Reed Kotler97ba5f22013-02-21 04:22:38 +0000940 default:
941 llvm_unreachable("Unexpected instr type to insert");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000942 case Mips::ATOMIC_LOAD_ADD_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000943 return emitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000944 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000945 return emitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000946 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000947 return emitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000948 case Mips::ATOMIC_LOAD_ADD_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000949 return emitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000950
951 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000952 return emitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000953 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000954 return emitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000955 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000956 return emitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000957 case Mips::ATOMIC_LOAD_AND_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000958 return emitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000959
960 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000961 return emitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000962 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000963 return emitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000964 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000965 return emitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000966 case Mips::ATOMIC_LOAD_OR_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000967 return emitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000968
969 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000970 return emitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000971 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000972 return emitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000973 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000974 return emitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000975 case Mips::ATOMIC_LOAD_XOR_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000976 return emitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000977
978 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000979 return emitAtomicBinaryPartword(MI, BB, 1, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000980 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000981 return emitAtomicBinaryPartword(MI, BB, 2, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000982 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000983 return emitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000984 case Mips::ATOMIC_LOAD_NAND_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000985 return emitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000986
987 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000988 return emitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000989 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000990 return emitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000991 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000992 return emitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000993 case Mips::ATOMIC_LOAD_SUB_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000994 return emitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000995
996 case Mips::ATOMIC_SWAP_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000997 return emitAtomicBinaryPartword(MI, BB, 1, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000998 case Mips::ATOMIC_SWAP_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000999 return emitAtomicBinaryPartword(MI, BB, 2, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001000 case Mips::ATOMIC_SWAP_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001001 return emitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001002 case Mips::ATOMIC_SWAP_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001003 return emitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001004
1005 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001006 return emitAtomicCmpSwapPartword(MI, BB, 1);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001007 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001008 return emitAtomicCmpSwapPartword(MI, BB, 2);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001009 case Mips::ATOMIC_CMP_SWAP_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001010 return emitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001011 case Mips::ATOMIC_CMP_SWAP_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001012 return emitAtomicCmpSwap(MI, BB, 8);
Akira Hatanaka1cb02422013-05-20 18:07:43 +00001013 case Mips::PseudoSDIV:
1014 case Mips::PseudoUDIV:
Daniel Sanders308181e2014-06-12 10:44:10 +00001015 case Mips::DIV:
1016 case Mips::DIVU:
1017 case Mips::MOD:
1018 case Mips::MODU:
Eric Christopher96e72c62015-01-29 23:27:36 +00001019 return insertDivByZeroTrap(MI, *BB, *Subtarget.getInstrInfo(), false);
Akira Hatanaka1cb02422013-05-20 18:07:43 +00001020 case Mips::PseudoDSDIV:
1021 case Mips::PseudoDUDIV:
Daniel Sanders308181e2014-06-12 10:44:10 +00001022 case Mips::DDIV:
1023 case Mips::DDIVU:
1024 case Mips::DMOD:
1025 case Mips::DMODU:
Eric Christopher96e72c62015-01-29 23:27:36 +00001026 return insertDivByZeroTrap(MI, *BB, *Subtarget.getInstrInfo(), true);
Daniel Sanders0fa60412014-06-12 13:39:06 +00001027 case Mips::SEL_D:
1028 return emitSEL_D(MI, BB);
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001029
1030 case Mips::PseudoSELECT_I:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001031 case Mips::PseudoSELECT_I64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001032 case Mips::PseudoSELECT_S:
1033 case Mips::PseudoSELECT_D32:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001034 case Mips::PseudoSELECT_D64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001035 return emitPseudoSELECT(MI, BB, false, Mips::BNE);
1036 case Mips::PseudoSELECTFP_F_I:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001037 case Mips::PseudoSELECTFP_F_I64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001038 case Mips::PseudoSELECTFP_F_S:
1039 case Mips::PseudoSELECTFP_F_D32:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001040 case Mips::PseudoSELECTFP_F_D64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001041 return emitPseudoSELECT(MI, BB, true, Mips::BC1F);
1042 case Mips::PseudoSELECTFP_T_I:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001043 case Mips::PseudoSELECTFP_T_I64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001044 case Mips::PseudoSELECTFP_T_S:
1045 case Mips::PseudoSELECTFP_T_D32:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001046 case Mips::PseudoSELECTFP_T_D64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001047 return emitPseudoSELECT(MI, BB, true, Mips::BC1T);
Akira Hatanakaa5352702011-03-31 18:26:17 +00001048 }
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001049}
1050
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001051// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
1052// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
1053MachineBasicBlock *
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001054MipsTargetLowering::emitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher0713a9d2011-06-08 23:55:35 +00001055 unsigned Size, unsigned BinOpcode,
Akira Hatanaka15506782011-06-07 18:58:42 +00001056 bool Nand) const {
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001057 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001058
1059 MachineFunction *MF = BB->getParent();
1060 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001061 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Eric Christopher96e72c62015-01-29 23:27:36 +00001062 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001063 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001064 unsigned LL, SC, AND, NOR, ZERO, BEQ;
1065
1066 if (Size == 4) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001067 if (isMicroMips) {
1068 LL = Mips::LL_MM;
1069 SC = Mips::SC_MM;
1070 } else {
Daniel Sandersbdcfab12014-07-24 09:47:14 +00001071 LL = Subtarget.hasMips32r6() ? Mips::LL_R6 : Mips::LL;
1072 SC = Subtarget.hasMips32r6() ? Mips::SC_R6 : Mips::SC;
Daniel Sanders6a803f62014-06-16 13:13:03 +00001073 }
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001074 AND = Mips::AND;
1075 NOR = Mips::NOR;
1076 ZERO = Mips::ZERO;
1077 BEQ = Mips::BEQ;
Daniel Sanders6a803f62014-06-16 13:13:03 +00001078 } else {
Daniel Sandersbdcfab12014-07-24 09:47:14 +00001079 LL = Subtarget.hasMips64r6() ? Mips::LLD_R6 : Mips::LLD;
1080 SC = Subtarget.hasMips64r6() ? Mips::SCD_R6 : Mips::SCD;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001081 AND = Mips::AND64;
1082 NOR = Mips::NOR64;
1083 ZERO = Mips::ZERO_64;
1084 BEQ = Mips::BEQ64;
1085 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001086
Akira Hatanaka0e019592011-07-19 20:11:17 +00001087 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001088 unsigned Ptr = MI->getOperand(1).getReg();
1089 unsigned Incr = MI->getOperand(2).getReg();
1090
Akira Hatanaka0e019592011-07-19 20:11:17 +00001091 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1092 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1093 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001094
1095 // insert new blocks after the current block
1096 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1097 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1098 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1099 MachineFunction::iterator It = BB;
1100 ++It;
1101 MF->insert(It, loopMBB);
1102 MF->insert(It, exitMBB);
1103
1104 // Transfer the remainder of BB and its successor edges to exitMBB.
1105 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001106 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001107 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1108
1109 // thisMBB:
1110 // ...
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001111 // fallthrough --> loopMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001112 BB->addSuccessor(loopMBB);
Akira Hatanaka08636b42011-07-19 17:09:53 +00001113 loopMBB->addSuccessor(loopMBB);
1114 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001115
1116 // loopMBB:
1117 // ll oldval, 0(ptr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001118 // <binop> storeval, oldval, incr
1119 // sc success, storeval, 0(ptr)
1120 // beq success, $0, loopMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001121 BB = loopMBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001122 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001123 if (Nand) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001124 // and andres, oldval, incr
1125 // nor storeval, $0, andres
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001126 BuildMI(BB, DL, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
1127 BuildMI(BB, DL, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001128 } else if (BinOpcode) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001129 // <binop> storeval, oldval, incr
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001130 BuildMI(BB, DL, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001131 } else {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001132 StoreVal = Incr;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001133 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001134 BuildMI(BB, DL, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
1135 BuildMI(BB, DL, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001136
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001137 MI->eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001138
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001139 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001140}
1141
Daniel Sanders6a803f62014-06-16 13:13:03 +00001142MachineBasicBlock *MipsTargetLowering::emitSignExtendToI32InReg(
1143 MachineInstr *MI, MachineBasicBlock *BB, unsigned Size, unsigned DstReg,
1144 unsigned SrcReg) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00001145 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders6a803f62014-06-16 13:13:03 +00001146 DebugLoc DL = MI->getDebugLoc();
1147
Eric Christopher1c29a652014-07-18 22:55:25 +00001148 if (Subtarget.hasMips32r2() && Size == 1) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001149 BuildMI(BB, DL, TII->get(Mips::SEB), DstReg).addReg(SrcReg);
1150 return BB;
1151 }
1152
Eric Christopher1c29a652014-07-18 22:55:25 +00001153 if (Subtarget.hasMips32r2() && Size == 2) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001154 BuildMI(BB, DL, TII->get(Mips::SEH), DstReg).addReg(SrcReg);
1155 return BB;
1156 }
1157
1158 MachineFunction *MF = BB->getParent();
1159 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1160 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1161 unsigned ScrReg = RegInfo.createVirtualRegister(RC);
1162
1163 assert(Size < 32);
1164 int64_t ShiftImm = 32 - (Size * 8);
1165
1166 BuildMI(BB, DL, TII->get(Mips::SLL), ScrReg).addReg(SrcReg).addImm(ShiftImm);
1167 BuildMI(BB, DL, TII->get(Mips::SRA), DstReg).addReg(ScrReg).addImm(ShiftImm);
1168
1169 return BB;
1170}
1171
1172MachineBasicBlock *MipsTargetLowering::emitAtomicBinaryPartword(
1173 MachineInstr *MI, MachineBasicBlock *BB, unsigned Size, unsigned BinOpcode,
1174 bool Nand) const {
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001175 assert((Size == 1 || Size == 2) &&
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001176 "Unsupported size for EmitAtomicBinaryPartial.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001177
1178 MachineFunction *MF = BB->getParent();
1179 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1180 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
Eric Christopher96e72c62015-01-29 23:27:36 +00001181 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001182 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001183
1184 unsigned Dest = MI->getOperand(0).getReg();
1185 unsigned Ptr = MI->getOperand(1).getReg();
1186 unsigned Incr = MI->getOperand(2).getReg();
1187
Akira Hatanaka0e019592011-07-19 20:11:17 +00001188 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1189 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001190 unsigned Mask = RegInfo.createVirtualRegister(RC);
1191 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001192 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1193 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001194 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001195 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1196 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1197 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1198 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1199 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka9663dd32011-07-19 20:56:53 +00001200 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001201 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1202 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1203 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001204 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001205
1206 // insert new blocks after the current block
1207 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1208 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001209 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001210 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1211 MachineFunction::iterator It = BB;
1212 ++It;
1213 MF->insert(It, loopMBB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001214 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001215 MF->insert(It, exitMBB);
1216
1217 // Transfer the remainder of BB and its successor edges to exitMBB.
1218 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001219 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001220 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1221
Akira Hatanaka08636b42011-07-19 17:09:53 +00001222 BB->addSuccessor(loopMBB);
1223 loopMBB->addSuccessor(loopMBB);
1224 loopMBB->addSuccessor(sinkMBB);
1225 sinkMBB->addSuccessor(exitMBB);
1226
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001227 // thisMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001228 // addiu masklsb2,$0,-4 # 0xfffffffc
1229 // and alignedaddr,ptr,masklsb2
1230 // andi ptrlsb2,ptr,3
1231 // sll shiftamt,ptrlsb2,3
1232 // ori maskupper,$0,255 # 0xff
1233 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001234 // nor mask2,$0,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001235 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001236
1237 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001238 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001239 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001240 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001241 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001242 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Eric Christopher1c29a652014-07-18 22:55:25 +00001243 if (Subtarget.isLittle()) {
Akira Hatanaka2bf97332013-05-31 03:25:44 +00001244 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1245 } else {
1246 unsigned Off = RegInfo.createVirtualRegister(RC);
1247 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1248 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1249 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1250 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001251 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001252 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001253 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001254 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001255 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001256 BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(Incr).addReg(ShiftAmt);
Bruno Cardoso Lopesf771a0f2011-05-31 20:25:26 +00001257
Akira Hatanaka27292632011-07-18 18:52:12 +00001258 // atomic.load.binop
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001259 // loopMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001260 // ll oldval,0(alignedaddr)
1261 // binop binopres,oldval,incr2
1262 // and newval,binopres,mask
1263 // and maskedoldval0,oldval,mask2
1264 // or storeval,maskedoldval0,newval
1265 // sc success,storeval,0(alignedaddr)
1266 // beq success,$0,loopMBB
1267
Akira Hatanaka27292632011-07-18 18:52:12 +00001268 // atomic.swap
1269 // loopMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001270 // ll oldval,0(alignedaddr)
Akira Hatanakae4503582011-07-19 18:14:26 +00001271 // and newval,incr2,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001272 // and maskedoldval0,oldval,mask2
1273 // or storeval,maskedoldval0,newval
1274 // sc success,storeval,0(alignedaddr)
1275 // beq success,$0,loopMBB
Akira Hatanaka27292632011-07-18 18:52:12 +00001276
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001277 BB = loopMBB;
Jozef Kolek2f27d572014-12-18 16:39:29 +00001278 unsigned LL = isMicroMips ? Mips::LL_MM : Mips::LL;
1279 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001280 if (Nand) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001281 // and andres, oldval, incr2
1282 // nor binopres, $0, andres
1283 // and newval, binopres, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001284 BuildMI(BB, DL, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1285 BuildMI(BB, DL, TII->get(Mips::NOR), BinOpRes)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001286 .addReg(Mips::ZERO).addReg(AndRes);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001287 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001288 } else if (BinOpcode) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001289 // <binop> binopres, oldval, incr2
1290 // and newval, binopres, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001291 BuildMI(BB, DL, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1292 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001293 } else { // atomic.swap
Akira Hatanaka0e019592011-07-19 20:11:17 +00001294 // and newval, incr2, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001295 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanakae4503582011-07-19 18:14:26 +00001296 }
Jia Liuf54f60f2012-02-28 07:46:26 +00001297
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001298 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001299 .addReg(OldVal).addReg(Mask2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001300 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka9663dd32011-07-19 20:56:53 +00001301 .addReg(MaskedOldVal0).addReg(NewVal);
Jozef Kolek2f27d572014-12-18 16:39:29 +00001302 unsigned SC = isMicroMips ? Mips::SC_MM : Mips::SC;
1303 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001304 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001305 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001306 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001307
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001308 // sinkMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001309 // and maskedoldval1,oldval,mask
1310 // srl srlres,maskedoldval1,shiftamt
Daniel Sanders6a803f62014-06-16 13:13:03 +00001311 // sign_extend dest,srlres
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001312 BB = sinkMBB;
Akira Hatanakae97bd812011-07-19 03:14:58 +00001313
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001314 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001315 .addReg(OldVal).addReg(Mask);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001316 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001317 .addReg(MaskedOldVal1).addReg(ShiftAmt);
Daniel Sanders6a803f62014-06-16 13:13:03 +00001318 BB = emitSignExtendToI32InReg(MI, BB, Size, Dest, SrlRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001319
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001320 MI->eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001321
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001322 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001323}
1324
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001325MachineBasicBlock * MipsTargetLowering::emitAtomicCmpSwap(MachineInstr *MI,
1326 MachineBasicBlock *BB,
1327 unsigned Size) const {
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001328 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001329
1330 MachineFunction *MF = BB->getParent();
1331 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001332 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Eric Christopher96e72c62015-01-29 23:27:36 +00001333 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001334 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001335 unsigned LL, SC, ZERO, BNE, BEQ;
1336
1337 if (Size == 4) {
Zoran Jovanovicff9d5f32013-12-19 16:12:56 +00001338 LL = isMicroMips ? Mips::LL_MM : Mips::LL;
1339 SC = isMicroMips ? Mips::SC_MM : Mips::SC;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001340 ZERO = Mips::ZERO;
1341 BNE = Mips::BNE;
1342 BEQ = Mips::BEQ;
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001343 } else {
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001344 LL = Mips::LLD;
1345 SC = Mips::SCD;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001346 ZERO = Mips::ZERO_64;
1347 BNE = Mips::BNE64;
1348 BEQ = Mips::BEQ64;
1349 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001350
1351 unsigned Dest = MI->getOperand(0).getReg();
1352 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka0e019592011-07-19 20:11:17 +00001353 unsigned OldVal = MI->getOperand(2).getReg();
1354 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001355
Akira Hatanaka0e019592011-07-19 20:11:17 +00001356 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001357
1358 // insert new blocks after the current block
1359 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1360 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1361 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1362 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1363 MachineFunction::iterator It = BB;
1364 ++It;
1365 MF->insert(It, loop1MBB);
1366 MF->insert(It, loop2MBB);
1367 MF->insert(It, exitMBB);
1368
1369 // Transfer the remainder of BB and its successor edges to exitMBB.
1370 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001371 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001372 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1373
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001374 // thisMBB:
1375 // ...
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001376 // fallthrough --> loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001377 BB->addSuccessor(loop1MBB);
Akira Hatanaka08636b42011-07-19 17:09:53 +00001378 loop1MBB->addSuccessor(exitMBB);
1379 loop1MBB->addSuccessor(loop2MBB);
1380 loop2MBB->addSuccessor(loop1MBB);
1381 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001382
1383 // loop1MBB:
1384 // ll dest, 0(ptr)
1385 // bne dest, oldval, exitMBB
1386 BB = loop1MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001387 BuildMI(BB, DL, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1388 BuildMI(BB, DL, TII->get(BNE))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001389 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001390
1391 // loop2MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001392 // sc success, newval, 0(ptr)
1393 // beq success, $0, loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001394 BB = loop2MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001395 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001396 .addReg(NewVal).addReg(Ptr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001397 BuildMI(BB, DL, TII->get(BEQ))
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001398 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001399
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001400 MI->eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001401
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001402 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001403}
1404
1405MachineBasicBlock *
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001406MipsTargetLowering::emitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka15506782011-06-07 18:58:42 +00001407 MachineBasicBlock *BB,
1408 unsigned Size) const {
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001409 assert((Size == 1 || Size == 2) &&
1410 "Unsupported size for EmitAtomicCmpSwapPartial.");
1411
1412 MachineFunction *MF = BB->getParent();
1413 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1414 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
Eric Christopher96e72c62015-01-29 23:27:36 +00001415 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001416 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001417
1418 unsigned Dest = MI->getOperand(0).getReg();
1419 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka0e019592011-07-19 20:11:17 +00001420 unsigned CmpVal = MI->getOperand(2).getReg();
1421 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001422
Akira Hatanaka0e019592011-07-19 20:11:17 +00001423 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1424 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001425 unsigned Mask = RegInfo.createVirtualRegister(RC);
1426 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001427 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1428 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1429 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1430 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1431 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1432 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1433 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1434 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1435 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1436 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1437 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1438 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001439 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001440
1441 // insert new blocks after the current block
1442 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1443 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1444 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001445 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001446 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1447 MachineFunction::iterator It = BB;
1448 ++It;
1449 MF->insert(It, loop1MBB);
1450 MF->insert(It, loop2MBB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001451 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001452 MF->insert(It, exitMBB);
1453
1454 // Transfer the remainder of BB and its successor edges to exitMBB.
1455 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001456 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001457 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1458
Akira Hatanaka08636b42011-07-19 17:09:53 +00001459 BB->addSuccessor(loop1MBB);
1460 loop1MBB->addSuccessor(sinkMBB);
1461 loop1MBB->addSuccessor(loop2MBB);
1462 loop2MBB->addSuccessor(loop1MBB);
1463 loop2MBB->addSuccessor(sinkMBB);
1464 sinkMBB->addSuccessor(exitMBB);
1465
Akira Hatanakae4503582011-07-19 18:14:26 +00001466 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001467 // thisMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001468 // addiu masklsb2,$0,-4 # 0xfffffffc
1469 // and alignedaddr,ptr,masklsb2
1470 // andi ptrlsb2,ptr,3
1471 // sll shiftamt,ptrlsb2,3
1472 // ori maskupper,$0,255 # 0xff
1473 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001474 // nor mask2,$0,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001475 // andi maskedcmpval,cmpval,255
1476 // sll shiftedcmpval,maskedcmpval,shiftamt
1477 // andi maskednewval,newval,255
1478 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001479 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001480 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001481 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001482 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001483 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001484 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Eric Christopher1c29a652014-07-18 22:55:25 +00001485 if (Subtarget.isLittle()) {
Akira Hatanaka2bf97332013-05-31 03:25:44 +00001486 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1487 } else {
1488 unsigned Off = RegInfo.createVirtualRegister(RC);
1489 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1490 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1491 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1492 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001493 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001494 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001495 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001496 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001497 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1498 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001499 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001500 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001501 .addReg(MaskedCmpVal).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001502 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001503 .addReg(NewVal).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001504 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001505 .addReg(MaskedNewVal).addReg(ShiftAmt);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001506
1507 // loop1MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001508 // ll oldval,0(alginedaddr)
1509 // and maskedoldval0,oldval,mask
1510 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001511 BB = loop1MBB;
Jozef Kolek2f27d572014-12-18 16:39:29 +00001512 unsigned LL = isMicroMips ? Mips::LL_MM : Mips::LL;
1513 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001514 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001515 .addReg(OldVal).addReg(Mask);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001516 BuildMI(BB, DL, TII->get(Mips::BNE))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001517 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001518
1519 // loop2MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001520 // and maskedoldval1,oldval,mask2
1521 // or storeval,maskedoldval1,shiftednewval
1522 // sc success,storeval,0(alignedaddr)
1523 // beq success,$0,loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001524 BB = loop2MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001525 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001526 .addReg(OldVal).addReg(Mask2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001527 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001528 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
Jozef Kolek2f27d572014-12-18 16:39:29 +00001529 unsigned SC = isMicroMips ? Mips::SC_MM : Mips::SC;
1530 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001531 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001532 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001533 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001534
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001535 // sinkMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001536 // srl srlres,maskedoldval0,shiftamt
Daniel Sanders6a803f62014-06-16 13:13:03 +00001537 // sign_extend dest,srlres
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001538 BB = sinkMBB;
Akira Hatanakae97bd812011-07-19 03:14:58 +00001539
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001540 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001541 .addReg(MaskedOldVal0).addReg(ShiftAmt);
Daniel Sanders6a803f62014-06-16 13:13:03 +00001542 BB = emitSignExtendToI32InReg(MI, BB, Size, Dest, SrlRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001543
1544 MI->eraseFromParent(); // The instruction is gone now.
1545
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001546 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001547}
1548
Daniel Sanders0fa60412014-06-12 13:39:06 +00001549MachineBasicBlock *MipsTargetLowering::emitSEL_D(MachineInstr *MI,
1550 MachineBasicBlock *BB) const {
1551 MachineFunction *MF = BB->getParent();
Eric Christopher96e72c62015-01-29 23:27:36 +00001552 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
1553 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders0fa60412014-06-12 13:39:06 +00001554 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1555 DebugLoc DL = MI->getDebugLoc();
1556 MachineBasicBlock::iterator II(MI);
1557
1558 unsigned Fc = MI->getOperand(1).getReg();
1559 const auto &FGR64RegClass = TRI->getRegClass(Mips::FGR64RegClassID);
1560
1561 unsigned Fc2 = RegInfo.createVirtualRegister(FGR64RegClass);
1562
1563 BuildMI(*BB, II, DL, TII->get(Mips::SUBREG_TO_REG), Fc2)
1564 .addImm(0)
1565 .addReg(Fc)
1566 .addImm(Mips::sub_lo);
1567
1568 // We don't erase the original instruction, we just replace the condition
1569 // register with the 64-bit super-register.
1570 MI->getOperand(1).setReg(Fc2);
1571
1572 return BB;
1573}
1574
Akira Hatanakae2489122011-04-15 21:51:11 +00001575//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001576// Misc Lower Operation implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00001577//===----------------------------------------------------------------------===//
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001578SDValue MipsTargetLowering::lowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001579 SDValue Chain = Op.getOperand(0);
1580 SDValue Table = Op.getOperand(1);
1581 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001582 SDLoc DL(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00001583 auto &TD = DAG.getDataLayout();
1584 EVT PTy = getPointerTy(TD);
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001585 unsigned EntrySize =
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001586 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(TD);
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001587
1588 Index = DAG.getNode(ISD::MUL, DL, PTy, Index,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001589 DAG.getConstant(EntrySize, DL, PTy));
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001590 SDValue Addr = DAG.getNode(ISD::ADD, DL, PTy, Index, Table);
1591
1592 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
1593 Addr = DAG.getExtLoad(ISD::SEXTLOAD, DL, PTy, Chain, Addr,
1594 MachinePointerInfo::getJumpTable(), MemVT, false, false,
Louis Gerbarg67474e32014-07-31 21:45:05 +00001595 false, 0);
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001596 Chain = Addr.getValue(1);
1597
Eric Christopher96e72c62015-01-29 23:27:36 +00001598 if ((getTargetMachine().getRelocationModel() == Reloc::PIC_) || ABI.IsN64()) {
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001599 // For PIC, the sequence is:
1600 // BRIND(load(Jumptable + index) + RelocBase)
1601 // RelocBase can be JumpTable, GOT or some sort of global base.
1602 Addr = DAG.getNode(ISD::ADD, DL, PTy, Addr,
1603 getPICJumpTableRelocBase(Table, DAG));
1604 }
1605
1606 return DAG.getNode(ISD::BRIND, DL, MVT::Other, Chain, Addr);
1607}
1608
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001609SDValue MipsTargetLowering::lowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Wesley Peck527da1b2010-11-23 03:31:01 +00001610 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001611 // the block to branch to if the condition is true.
1612 SDValue Chain = Op.getOperand(0);
1613 SDValue Dest = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001614 SDLoc DL(Op);
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001615
Eric Christopher1c29a652014-07-18 22:55:25 +00001616 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001617 SDValue CondRes = createFPCmp(DAG, Op.getOperand(1));
Akira Hatanakaa5352702011-03-31 18:26:17 +00001618
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001619 // Return if flag is not set by a floating point comparison.
Akira Hatanakaa5352702011-03-31 18:26:17 +00001620 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopesa9504222008-07-30 17:06:13 +00001621 return Op;
Wesley Peck527da1b2010-11-23 03:31:01 +00001622
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +00001623 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmaneffb8942008-09-12 16:56:44 +00001624 Mips::CondCode CC =
1625 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Akira Hatanakaf0ea5002013-03-30 01:16:38 +00001626 unsigned Opc = invertFPCondCodeUser(CC) ? Mips::BRANCH_F : Mips::BRANCH_T;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001627 SDValue BrCode = DAG.getConstant(Opc, DL, MVT::i32);
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +00001628 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001629 return DAG.getNode(MipsISD::FPBrcond, DL, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +00001630 FCC0, Dest, CondRes);
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001631}
1632
1633SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001634lowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001635{
Eric Christopher1c29a652014-07-18 22:55:25 +00001636 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001637 SDValue Cond = createFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001638
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001639 // Return if flag is not set by a floating point comparison.
Akira Hatanakaa5352702011-03-31 18:26:17 +00001640 if (Cond.getOpcode() != MipsISD::FPCmp)
1641 return Op;
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +00001642
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001643 return createCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
Andrew Trickef9de2a2013-05-25 02:42:55 +00001644 SDLoc(Op));
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001645}
1646
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001647SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001648lowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001649{
Andrew Trickef9de2a2013-05-25 02:42:55 +00001650 SDLoc DL(Op);
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001651 EVT Ty = Op.getOperand(0).getValueType();
Mehdi Amini44ede332015-07-09 02:09:04 +00001652 SDValue Cond =
1653 DAG.getNode(ISD::SETCC, DL, getSetCCResultType(DAG.getDataLayout(),
1654 *DAG.getContext(), Ty),
1655 Op.getOperand(0), Op.getOperand(1), Op.getOperand(4));
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001656
1657 return DAG.getNode(ISD::SELECT, DL, Op.getValueType(), Cond, Op.getOperand(2),
1658 Op.getOperand(3));
1659}
1660
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001661SDValue MipsTargetLowering::lowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00001662 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001663 SDValue Cond = createFPCmp(DAG, Op);
Akira Hatanakab7f78592012-03-09 23:46:03 +00001664
1665 assert(Cond.getOpcode() == MipsISD::FPCmp &&
1666 "Floating point operand expected.");
1667
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001668 SDLoc DL(Op);
1669 SDValue True = DAG.getConstant(1, DL, MVT::i32);
1670 SDValue False = DAG.getConstant(0, DL, MVT::i32);
Akira Hatanakab7f78592012-03-09 23:46:03 +00001671
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001672 return createCMovFP(DAG, Cond, True, False, DL);
Akira Hatanakab7f78592012-03-09 23:46:03 +00001673}
1674
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001675SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001676 SelectionDAG &DAG) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001677 EVT Ty = Op.getValueType();
1678 GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
1679 const GlobalValue *GV = N->getGlobal();
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001680
Eric Christopher96e72c62015-01-29 23:27:36 +00001681 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !ABI.IsN64()) {
Eric Christopher36fe0282015-02-03 07:22:52 +00001682 const MipsTargetObjectFile *TLOF =
1683 static_cast<const MipsTargetObjectFile *>(
1684 getTargetMachine().getObjFileLowering());
1685 if (TLOF->IsGlobalInSmallSection(GV, getTargetMachine()))
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001686 // %gp_rel relocation
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001687 return getAddrGPRel(N, SDLoc(N), Ty, DAG);
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001688
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001689 // %hi/%lo relocation
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001690 return getAddrNonPIC(N, SDLoc(N), Ty, DAG);
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001691 }
1692
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001693 if (GV->hasInternalLinkage() || (GV->hasLocalLinkage() && !isa<Function>(GV)))
Eric Christopher96e72c62015-01-29 23:27:36 +00001694 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001695
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00001696 if (LargeGOT)
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001697 return getAddrGlobalLargeGOT(N, SDLoc(N), Ty, DAG, MipsII::MO_GOT_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00001698 MipsII::MO_GOT_LO16, DAG.getEntryNode(),
1699 MachinePointerInfo::getGOT());
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00001700
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001701 return getAddrGlobal(N, SDLoc(N), Ty, DAG,
Eric Christopher96e72c62015-01-29 23:27:36 +00001702 (ABI.IsN32() || ABI.IsN64()) ? MipsII::MO_GOT_DISP
1703 : MipsII::MO_GOT16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00001704 DAG.getEntryNode(), MachinePointerInfo::getGOT());
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001705}
1706
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001707SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op,
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +00001708 SelectionDAG &DAG) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001709 BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op);
1710 EVT Ty = Op.getValueType();
Akira Hatanaka30f97cf2013-09-25 00:30:25 +00001711
Eric Christopher96e72c62015-01-29 23:27:36 +00001712 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !ABI.IsN64())
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001713 return getAddrNonPIC(N, SDLoc(N), Ty, DAG);
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001714
Eric Christopher96e72c62015-01-29 23:27:36 +00001715 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +00001716}
1717
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001718SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001719lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001720{
Akira Hatanakabff84e12011-12-14 18:26:41 +00001721 // If the relocation model is PIC, use the General Dynamic TLS Model or
1722 // Local Dynamic TLS model, otherwise use the Initial Exec or
1723 // Local Exec TLS Model.
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001724
1725 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +00001726 if (DAG.getTarget().Options.EmulatedTLS)
1727 return LowerToTLSEmulatedModel(GA, DAG);
1728
Andrew Trickef9de2a2013-05-25 02:42:55 +00001729 SDLoc DL(GA);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001730 const GlobalValue *GV = GA->getGlobal();
Mehdi Amini44ede332015-07-09 02:09:04 +00001731 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001732
Hans Wennborgaea41202012-05-04 09:40:39 +00001733 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
1734
1735 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Hans Wennborg245917b2012-06-04 14:02:08 +00001736 // General Dynamic and Local Dynamic TLS Model.
1737 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
1738 : MipsII::MO_TLSGD;
1739
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001740 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag);
1741 SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT,
1742 getGlobalReg(DAG, PtrVT), TGA);
Akira Hatanakaf10ee842011-12-08 21:05:38 +00001743 unsigned PtrSize = PtrVT.getSizeInBits();
1744 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1745
Benjamin Kramer64ba50a2011-12-11 12:21:34 +00001746 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001747
1748 ArgListTy Args;
1749 ArgListEntry Entry;
1750 Entry.Node = Argument;
Akira Hatanakadee6c822011-12-08 20:34:32 +00001751 Entry.Ty = PtrTy;
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001752 Args.push_back(Entry);
Jia Liuf54f60f2012-02-28 07:46:26 +00001753
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00001754 TargetLowering::CallLoweringInfo CLI(DAG);
1755 CLI.setDebugLoc(DL).setChain(DAG.getEntryNode())
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00001756 .setCallee(CallingConv::C, PtrTy, TlsGetAddr, std::move(Args), 0);
Justin Holewinskiaa583972012-05-25 16:35:28 +00001757 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001758
Akira Hatanakabff84e12011-12-14 18:26:41 +00001759 SDValue Ret = CallResult.first;
1760
Hans Wennborgaea41202012-05-04 09:40:39 +00001761 if (model != TLSModel::LocalDynamic)
Akira Hatanakabff84e12011-12-14 18:26:41 +00001762 return Ret;
1763
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001764 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanakabff84e12011-12-14 18:26:41 +00001765 MipsII::MO_DTPREL_HI);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001766 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1767 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanakabff84e12011-12-14 18:26:41 +00001768 MipsII::MO_DTPREL_LO);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001769 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1770 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret);
1771 return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001772 }
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001773
1774 SDValue Offset;
Hans Wennborgaea41202012-05-04 09:40:39 +00001775 if (model == TLSModel::InitialExec) {
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001776 // Initial Exec TLS Model
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001777 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001778 MipsII::MO_GOTTPREL);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001779 TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT),
Akira Hatanakab049aef2012-02-24 22:34:47 +00001780 TGA);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001781 Offset = DAG.getLoad(PtrVT, DL,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001782 DAG.getEntryNode(), TGA, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001783 false, false, false, 0);
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001784 } else {
1785 // Local Exec TLS Model
Hans Wennborgaea41202012-05-04 09:40:39 +00001786 assert(model == TLSModel::LocalExec);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001787 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001788 MipsII::MO_TPREL_HI);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001789 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001790 MipsII::MO_TPREL_LO);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001791 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1792 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1793 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001794 }
1795
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001796 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
1797 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001798}
1799
1800SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001801lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +00001802{
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001803 JumpTableSDNode *N = cast<JumpTableSDNode>(Op);
1804 EVT Ty = Op.getValueType();
Akira Hatanaka30f97cf2013-09-25 00:30:25 +00001805
Eric Christopher96e72c62015-01-29 23:27:36 +00001806 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !ABI.IsN64())
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001807 return getAddrNonPIC(N, SDLoc(N), Ty, DAG);
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001808
Eric Christopher96e72c62015-01-29 23:27:36 +00001809 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +00001810}
1811
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001812SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001813lowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +00001814{
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001815 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
1816 EVT Ty = Op.getValueType();
Bruno Cardoso Lopes2db07582009-11-25 12:17:58 +00001817
Eric Christopher96e72c62015-01-29 23:27:36 +00001818 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !ABI.IsN64()) {
Eric Christopher36fe0282015-02-03 07:22:52 +00001819 const MipsTargetObjectFile *TLOF =
1820 static_cast<const MipsTargetObjectFile *>(
1821 getTargetMachine().getObjFileLowering());
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001822
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001823 if (TLOF->IsConstantInSmallSection(DAG.getDataLayout(), N->getConstVal(),
1824 getTargetMachine()))
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001825 // %gp_rel relocation
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001826 return getAddrGPRel(N, SDLoc(N), Ty, DAG);
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001827
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001828 return getAddrNonPIC(N, SDLoc(N), Ty, DAG);
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001829 }
Bruno Cardoso Lopesfdb4cec2008-07-23 16:01:50 +00001830
Eric Christopher96e72c62015-01-29 23:27:36 +00001831 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +00001832}
1833
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001834SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman31ae5862010-04-17 14:41:14 +00001835 MachineFunction &MF = DAG.getMachineFunction();
1836 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1837
Andrew Trickef9de2a2013-05-25 02:42:55 +00001838 SDLoc DL(Op);
Dan Gohman31ae5862010-04-17 14:41:14 +00001839 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
Mehdi Amini44ede332015-07-09 02:09:04 +00001840 getPointerTy(MF.getDataLayout()));
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +00001841
1842 // vastart just stores the address of the VarArgsFrameIndex slot into the
1843 // memory location argument.
1844 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001845 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00001846 MachinePointerInfo(SV), false, false, 0);
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +00001847}
Jia Liuf54f60f2012-02-28 07:46:26 +00001848
Daniel Sanders2b553d42014-08-01 09:17:39 +00001849SDValue MipsTargetLowering::lowerVAARG(SDValue Op, SelectionDAG &DAG) const {
1850 SDNode *Node = Op.getNode();
1851 EVT VT = Node->getValueType(0);
1852 SDValue Chain = Node->getOperand(0);
1853 SDValue VAListPtr = Node->getOperand(1);
1854 unsigned Align = Node->getConstantOperandVal(3);
1855 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
1856 SDLoc DL(Node);
Eric Christopher96e72c62015-01-29 23:27:36 +00001857 unsigned ArgSlotSizeInBytes = (ABI.IsN32() || ABI.IsN64()) ? 8 : 4;
Daniel Sanders2b553d42014-08-01 09:17:39 +00001858
Mehdi Amini44ede332015-07-09 02:09:04 +00001859 SDValue VAListLoad =
1860 DAG.getLoad(getPointerTy(DAG.getDataLayout()), DL, Chain, VAListPtr,
1861 MachinePointerInfo(SV), false, false, false, 0);
Daniel Sanders2b553d42014-08-01 09:17:39 +00001862 SDValue VAList = VAListLoad;
1863
1864 // Re-align the pointer if necessary.
1865 // It should only ever be necessary for 64-bit types on O32 since the minimum
1866 // argument alignment is the same as the maximum type alignment for N32/N64.
1867 //
1868 // FIXME: We currently align too often. The code generator doesn't notice
1869 // when the pointer is still aligned from the last va_arg (or pair of
1870 // va_args for the i64 on O32 case).
1871 if (Align > getMinStackArgumentAlignment()) {
1872 assert(((Align & (Align-1)) == 0) && "Expected Align to be a power of 2");
1873
1874 VAList = DAG.getNode(ISD::ADD, DL, VAList.getValueType(), VAList,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001875 DAG.getConstant(Align - 1, DL, VAList.getValueType()));
Daniel Sanders2b553d42014-08-01 09:17:39 +00001876
1877 VAList = DAG.getNode(ISD::AND, DL, VAList.getValueType(), VAList,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001878 DAG.getConstant(-(int64_t)Align, DL,
Daniel Sanders2b553d42014-08-01 09:17:39 +00001879 VAList.getValueType()));
1880 }
1881
1882 // Increment the pointer, VAList, to the next vaarg.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001883 auto &TD = DAG.getDataLayout();
1884 unsigned ArgSizeInBytes =
1885 TD.getTypeAllocSize(VT.getTypeForEVT(*DAG.getContext()));
Daniel Sanders2b553d42014-08-01 09:17:39 +00001886 SDValue Tmp3 = DAG.getNode(ISD::ADD, DL, VAList.getValueType(), VAList,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001887 DAG.getConstant(RoundUpToAlignment(ArgSizeInBytes,
1888 ArgSlotSizeInBytes),
1889 DL, VAList.getValueType()));
Daniel Sanders2b553d42014-08-01 09:17:39 +00001890 // Store the incremented VAList to the legalized pointer
1891 Chain = DAG.getStore(VAListLoad.getValue(1), DL, Tmp3, VAListPtr,
1892 MachinePointerInfo(SV), false, false, 0);
1893
1894 // In big-endian mode we must adjust the pointer when the load size is smaller
1895 // than the argument slot size. We must also reduce the known alignment to
1896 // match. For example in the N64 ABI, we must add 4 bytes to the offset to get
1897 // the correct half of the slot, and reduce the alignment from 8 (slot
1898 // alignment) down to 4 (type alignment).
1899 if (!Subtarget.isLittle() && ArgSizeInBytes < ArgSlotSizeInBytes) {
1900 unsigned Adjustment = ArgSlotSizeInBytes - ArgSizeInBytes;
1901 VAList = DAG.getNode(ISD::ADD, DL, VAListPtr.getValueType(), VAList,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001902 DAG.getIntPtrConstant(Adjustment, DL));
Daniel Sanders2b553d42014-08-01 09:17:39 +00001903 }
1904 // Load the actual argument out of the pointer VAList
1905 return DAG.getLoad(VT, DL, Chain, VAList, MachinePointerInfo(), false, false,
1906 false, 0);
1907}
1908
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001909static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG,
1910 bool HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001911 EVT TyX = Op.getOperand(0).getValueType();
1912 EVT TyY = Op.getOperand(1).getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001913 SDLoc DL(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001914 SDValue Const1 = DAG.getConstant(1, DL, MVT::i32);
1915 SDValue Const31 = DAG.getConstant(31, DL, MVT::i32);
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001916 SDValue Res;
1917
1918 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1919 // to i32.
1920 SDValue X = (TyX == MVT::f32) ?
1921 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1922 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1923 Const1);
1924 SDValue Y = (TyY == MVT::f32) ?
1925 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
1926 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
1927 Const1);
1928
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001929 if (HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001930 // ext E, Y, 31, 1 ; extract bit31 of Y
1931 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
1932 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
1933 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
1934 } else {
1935 // sll SllX, X, 1
1936 // srl SrlX, SllX, 1
1937 // srl SrlY, Y, 31
1938 // sll SllY, SrlX, 31
1939 // or Or, SrlX, SllY
1940 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1941 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1942 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
1943 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
1944 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
1945 }
1946
1947 if (TyX == MVT::f32)
1948 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
1949
1950 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001951 Op.getOperand(0),
1952 DAG.getConstant(0, DL, MVT::i32));
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001953 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001954}
1955
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001956static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG,
1957 bool HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001958 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
1959 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
1960 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001961 SDLoc DL(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001962 SDValue Const1 = DAG.getConstant(1, DL, MVT::i32);
Eric Christopher0713a9d2011-06-08 23:55:35 +00001963
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001964 // Bitcast to integer nodes.
1965 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
1966 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001967
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001968 if (HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001969 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
1970 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
1971 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001972 DAG.getConstant(WidthY - 1, DL, MVT::i32), Const1);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001973
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001974 if (WidthX > WidthY)
1975 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
1976 else if (WidthY > WidthX)
1977 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001978
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001979 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001980 DAG.getConstant(WidthX - 1, DL, MVT::i32), Const1,
1981 X);
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001982 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
1983 }
1984
1985 // (d)sll SllX, X, 1
1986 // (d)srl SrlX, SllX, 1
1987 // (d)srl SrlY, Y, width(Y)-1
1988 // (d)sll SllY, SrlX, width(Y)-1
1989 // or Or, SrlX, SllY
1990 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
1991 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
1992 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001993 DAG.getConstant(WidthY - 1, DL, MVT::i32));
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001994
1995 if (WidthX > WidthY)
1996 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
1997 else if (WidthY > WidthX)
1998 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
1999
2000 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002001 DAG.getConstant(WidthX - 1, DL, MVT::i32));
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002002 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
2003 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002004}
2005
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00002006SDValue
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002007MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00002008 if (Subtarget.isGP64bit())
2009 return lowerFCOPYSIGN64(Op, DAG, Subtarget.hasExtractInsert());
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002010
Eric Christopher1c29a652014-07-18 22:55:25 +00002011 return lowerFCOPYSIGN32(Op, DAG, Subtarget.hasExtractInsert());
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002012}
2013
Akira Hatanaka66277522011-06-02 00:24:44 +00002014SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002015lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes5444a7b2011-06-16 00:40:02 +00002016 // check the depth
2017 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka15506782011-06-07 18:58:42 +00002018 "Frame address can only be determined for current frame.");
Akira Hatanaka66277522011-06-02 00:24:44 +00002019
2020 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2021 MFI->setFrameAddressIsTaken(true);
2022 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002023 SDLoc DL(Op);
Eric Christopher96e72c62015-01-29 23:27:36 +00002024 SDValue FrameAddr = DAG.getCopyFromReg(
2025 DAG.getEntryNode(), DL, ABI.IsN64() ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka66277522011-06-02 00:24:44 +00002026 return FrameAddr;
2027}
2028
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002029SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op,
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002030 SelectionDAG &DAG) const {
Bill Wendling908bf812014-01-06 00:43:20 +00002031 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00002032 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00002033
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002034 // check the depth
2035 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
2036 "Return address can be determined only for current frame.");
2037
2038 MachineFunction &MF = DAG.getMachineFunction();
2039 MachineFrameInfo *MFI = MF.getFrameInfo();
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00002040 MVT VT = Op.getSimpleValueType();
Eric Christopher96e72c62015-01-29 23:27:36 +00002041 unsigned RA = ABI.IsN64() ? Mips::RA_64 : Mips::RA;
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002042 MFI->setReturnAddressIsTaken(true);
2043
2044 // Return RA, which contains the return address. Mark it an implicit live-in.
2045 unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00002046 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT);
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002047}
2048
Akira Hatanakac0b02062013-01-30 00:26:49 +00002049// An EH_RETURN is the result of lowering llvm.eh.return which in turn is
2050// generated from __builtin_eh_return (offset, handler)
2051// The effect of this is to adjust the stack pointer by "offset"
2052// and then branch to "handler".
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002053SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Akira Hatanakac0b02062013-01-30 00:26:49 +00002054 const {
2055 MachineFunction &MF = DAG.getMachineFunction();
2056 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2057
2058 MipsFI->setCallsEhReturn();
2059 SDValue Chain = Op.getOperand(0);
2060 SDValue Offset = Op.getOperand(1);
2061 SDValue Handler = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002062 SDLoc DL(Op);
Eric Christopher96e72c62015-01-29 23:27:36 +00002063 EVT Ty = ABI.IsN64() ? MVT::i64 : MVT::i32;
Akira Hatanakac0b02062013-01-30 00:26:49 +00002064
2065 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
2066 // EH_RETURN nodes, so that instructions are emitted back-to-back.
Eric Christopher96e72c62015-01-29 23:27:36 +00002067 unsigned OffsetReg = ABI.IsN64() ? Mips::V1_64 : Mips::V1;
2068 unsigned AddrReg = ABI.IsN64() ? Mips::V0_64 : Mips::V0;
Akira Hatanakac0b02062013-01-30 00:26:49 +00002069 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
2070 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
2071 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
2072 DAG.getRegister(OffsetReg, Ty),
Mehdi Amini44ede332015-07-09 02:09:04 +00002073 DAG.getRegister(AddrReg, getPointerTy(MF.getDataLayout())),
Akira Hatanakac0b02062013-01-30 00:26:49 +00002074 Chain.getValue(1));
2075}
2076
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002077SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
Akira Hatanaka5fd22482012-06-14 21:10:56 +00002078 SelectionDAG &DAG) const {
Eli Friedman26a48482011-07-27 22:21:52 +00002079 // FIXME: Need pseudo-fence for 'singlethread' fences
2080 // FIXME: Set SType for weaker fences where supported/appropriate.
2081 unsigned SType = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002082 SDLoc DL(Op);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002083 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002084 DAG.getConstant(SType, DL, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00002085}
2086
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002087SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
Akira Hatanaka5fd22482012-06-14 21:10:56 +00002088 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002089 SDLoc DL(Op);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002090 MVT VT = Subtarget.isGP64bit() ? MVT::i64 : MVT::i32;
2091
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002092 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2093 SDValue Shamt = Op.getOperand(2);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002094 // if shamt < (VT.bits):
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002095 // lo = (shl lo, shamt)
2096 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
2097 // else:
2098 // lo = 0
2099 // hi = (shl lo, shamt[4:0])
2100 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002101 DAG.getConstant(-1, DL, MVT::i32));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002102 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, VT, Lo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002103 DAG.getConstant(1, DL, VT));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002104 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, VT, ShiftRight1Lo, Not);
2105 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, VT, Hi, Shamt);
2106 SDValue Or = DAG.getNode(ISD::OR, DL, VT, ShiftLeftHi, ShiftRightLo);
2107 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, VT, Lo, Shamt);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002108 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
Daniel Sanders301f9372015-04-29 12:28:58 +00002109 DAG.getConstant(VT.getSizeInBits(), DL, MVT::i32));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002110 Lo = DAG.getNode(ISD::SELECT, DL, VT, Cond,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002111 DAG.getConstant(0, DL, VT), ShiftLeftLo);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002112 Hi = DAG.getNode(ISD::SELECT, DL, VT, Cond, ShiftLeftLo, Or);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002113
2114 SDValue Ops[2] = {Lo, Hi};
Craig Topper64941d92014-04-27 19:20:57 +00002115 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002116}
2117
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002118SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002119 bool IsSRA) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002120 SDLoc DL(Op);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002121 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2122 SDValue Shamt = Op.getOperand(2);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002123 MVT VT = Subtarget.isGP64bit() ? MVT::i64 : MVT::i32;
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002124
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002125 // if shamt < (VT.bits):
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002126 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
2127 // if isSRA:
2128 // hi = (sra hi, shamt)
2129 // else:
2130 // hi = (srl hi, shamt)
2131 // else:
2132 // if isSRA:
2133 // lo = (sra hi, shamt[4:0])
2134 // hi = (sra hi, 31)
2135 // else:
2136 // lo = (srl hi, shamt[4:0])
2137 // hi = 0
2138 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002139 DAG.getConstant(-1, DL, MVT::i32));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002140 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, VT, Hi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002141 DAG.getConstant(1, DL, VT));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002142 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, VT, ShiftLeft1Hi, Not);
2143 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, VT, Lo, Shamt);
2144 SDValue Or = DAG.getNode(ISD::OR, DL, VT, ShiftLeftHi, ShiftRightLo);
2145 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL,
2146 DL, VT, Hi, Shamt);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002147 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
Daniel Sanders301f9372015-04-29 12:28:58 +00002148 DAG.getConstant(VT.getSizeInBits(), DL, MVT::i32));
2149 SDValue Ext = DAG.getNode(ISD::SRA, DL, VT, Hi,
2150 DAG.getConstant(VT.getSizeInBits() - 1, DL, VT));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002151 Lo = DAG.getNode(ISD::SELECT, DL, VT, Cond, ShiftRightHi, Or);
2152 Hi = DAG.getNode(ISD::SELECT, DL, VT, Cond,
Daniel Sanders301f9372015-04-29 12:28:58 +00002153 IsSRA ? Ext : DAG.getConstant(0, DL, VT), ShiftRightHi);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002154
2155 SDValue Ops[2] = {Lo, Hi};
Craig Topper64941d92014-04-27 19:20:57 +00002156 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002157}
2158
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002159static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002160 SDValue Chain, SDValue Src, unsigned Offset) {
Akira Hatanaka95866182012-06-13 19:06:08 +00002161 SDValue Ptr = LD->getBasePtr();
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002162 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
Akira Hatanaka95866182012-06-13 19:06:08 +00002163 EVT BasePtrVT = Ptr.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002164 SDLoc DL(LD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002165 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
2166
2167 if (Offset)
Akira Hatanaka95866182012-06-13 19:06:08 +00002168 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002169 DAG.getConstant(Offset, DL, BasePtrVT));
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002170
2171 SDValue Ops[] = { Chain, Ptr, Src };
Craig Topper206fcd42014-04-26 19:29:41 +00002172 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002173 LD->getMemOperand());
2174}
2175
2176// Expand an unaligned 32 or 64-bit integer load node.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002177SDValue MipsTargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002178 LoadSDNode *LD = cast<LoadSDNode>(Op);
2179 EVT MemVT = LD->getMemoryVT();
2180
Eric Christopher1c29a652014-07-18 22:55:25 +00002181 if (Subtarget.systemSupportsUnalignedAccess())
Daniel Sandersac272632014-05-23 13:18:02 +00002182 return Op;
2183
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002184 // Return if load is aligned or if MemVT is neither i32 nor i64.
2185 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
2186 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
2187 return SDValue();
2188
Eric Christopher1c29a652014-07-18 22:55:25 +00002189 bool IsLittle = Subtarget.isLittle();
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002190 EVT VT = Op.getValueType();
2191 ISD::LoadExtType ExtType = LD->getExtensionType();
2192 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
2193
2194 assert((VT == MVT::i32) || (VT == MVT::i64));
2195
2196 // Expand
2197 // (set dst, (i64 (load baseptr)))
2198 // to
2199 // (set tmp, (ldl (add baseptr, 7), undef))
2200 // (set dst, (ldr baseptr, tmp))
2201 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002202 SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002203 IsLittle ? 7 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002204 return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002205 IsLittle ? 0 : 7);
2206 }
2207
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002208 SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002209 IsLittle ? 3 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002210 SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002211 IsLittle ? 0 : 3);
2212
2213 // Expand
2214 // (set dst, (i32 (load baseptr))) or
2215 // (set dst, (i64 (sextload baseptr))) or
2216 // (set dst, (i64 (extload baseptr)))
2217 // to
2218 // (set tmp, (lwl (add baseptr, 3), undef))
2219 // (set dst, (lwr baseptr, tmp))
2220 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
2221 (ExtType == ISD::EXTLOAD))
2222 return LWR;
2223
2224 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
2225
2226 // Expand
2227 // (set dst, (i64 (zextload baseptr)))
2228 // to
2229 // (set tmp0, (lwl (add baseptr, 3), undef))
2230 // (set tmp1, (lwr baseptr, tmp0))
2231 // (set tmp2, (shl tmp1, 32))
2232 // (set dst, (srl tmp2, 32))
Andrew Trickef9de2a2013-05-25 02:42:55 +00002233 SDLoc DL(LD);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002234 SDValue Const32 = DAG.getConstant(32, DL, MVT::i32);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002235 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
Akira Hatanaka67346852012-06-04 17:46:29 +00002236 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
2237 SDValue Ops[] = { SRL, LWR.getValue(1) };
Craig Topper64941d92014-04-27 19:20:57 +00002238 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002239}
2240
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002241static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002242 SDValue Chain, unsigned Offset) {
Akira Hatanaka95866182012-06-13 19:06:08 +00002243 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
2244 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002245 SDLoc DL(SD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002246 SDVTList VTList = DAG.getVTList(MVT::Other);
2247
2248 if (Offset)
Akira Hatanaka95866182012-06-13 19:06:08 +00002249 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002250 DAG.getConstant(Offset, DL, BasePtrVT));
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002251
2252 SDValue Ops[] = { Chain, Value, Ptr };
Craig Topper206fcd42014-04-26 19:29:41 +00002253 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002254 SD->getMemOperand());
2255}
2256
2257// Expand an unaligned 32 or 64-bit integer store node.
Akira Hatanakad82ee942013-05-16 20:45:17 +00002258static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG,
2259 bool IsLittle) {
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002260 SDValue Value = SD->getValue(), Chain = SD->getChain();
2261 EVT VT = Value.getValueType();
2262
2263 // Expand
2264 // (store val, baseptr) or
2265 // (truncstore val, baseptr)
2266 // to
2267 // (swl val, (add baseptr, 3))
2268 // (swr val, baseptr)
2269 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002270 SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002271 IsLittle ? 3 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002272 return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002273 }
2274
2275 assert(VT == MVT::i64);
2276
2277 // Expand
2278 // (store val, baseptr)
2279 // to
2280 // (sdl val, (add baseptr, 7))
2281 // (sdr val, baseptr)
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002282 SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2283 return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002284}
2285
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002286// Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr).
2287static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG) {
2288 SDValue Val = SD->getValue();
2289
2290 if (Val.getOpcode() != ISD::FP_TO_SINT)
2291 return SDValue();
2292
2293 EVT FPTy = EVT::getFloatingPointVT(Val.getValueSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002294 SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy,
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002295 Val.getOperand(0));
2296
Andrew Trickef9de2a2013-05-25 02:42:55 +00002297 return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(),
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002298 SD->getPointerInfo(), SD->isVolatile(),
2299 SD->isNonTemporal(), SD->getAlignment());
2300}
2301
Akira Hatanakad82ee942013-05-16 20:45:17 +00002302SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2303 StoreSDNode *SD = cast<StoreSDNode>(Op);
2304 EVT MemVT = SD->getMemoryVT();
2305
2306 // Lower unaligned integer stores.
Eric Christopher1c29a652014-07-18 22:55:25 +00002307 if (!Subtarget.systemSupportsUnalignedAccess() &&
Daniel Sandersac272632014-05-23 13:18:02 +00002308 (SD->getAlignment() < MemVT.getSizeInBits() / 8) &&
Akira Hatanakad82ee942013-05-16 20:45:17 +00002309 ((MemVT == MVT::i32) || (MemVT == MVT::i64)))
Eric Christopher1c29a652014-07-18 22:55:25 +00002310 return lowerUnalignedIntStore(SD, DAG, Subtarget.isLittle());
Akira Hatanakad82ee942013-05-16 20:45:17 +00002311
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002312 return lowerFP_TO_SINT_STORE(SD, DAG);
Akira Hatanakad82ee942013-05-16 20:45:17 +00002313}
2314
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002315SDValue MipsTargetLowering::lowerADD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002316 if (Op->getOperand(0).getOpcode() != ISD::FRAMEADDR
2317 || cast<ConstantSDNode>
2318 (Op->getOperand(0).getOperand(0))->getZExtValue() != 0
2319 || Op->getOperand(1).getOpcode() != ISD::FRAME_TO_ARGS_OFFSET)
2320 return SDValue();
2321
2322 // The pattern
2323 // (add (frameaddr 0), (frame_to_args_offset))
2324 // results from lowering llvm.eh.dwarf.cfa intrinsic. Transform it to
2325 // (add FrameObject, 0)
2326 // where FrameObject is a fixed StackObject with offset 0 which points to
2327 // the old stack pointer.
2328 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2329 EVT ValTy = Op->getValueType(0);
2330 int FI = MFI->CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
2331 SDValue InArgsAddr = DAG.getFrameIndex(FI, ValTy);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002332 SDLoc DL(Op);
2333 return DAG.getNode(ISD::ADD, DL, ValTy, InArgsAddr,
2334 DAG.getConstant(0, DL, ValTy));
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002335}
2336
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002337SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op,
2338 SelectionDAG &DAG) const {
2339 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002340 SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy,
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002341 Op.getOperand(0));
Andrew Trickef9de2a2013-05-25 02:42:55 +00002342 return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc);
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002343}
2344
Akira Hatanakae2489122011-04-15 21:51:11 +00002345//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002346// Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002347//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002348
Akira Hatanakae2489122011-04-15 21:51:11 +00002349//===----------------------------------------------------------------------===//
Wesley Peck527da1b2010-11-23 03:31:01 +00002350// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002351// Mips O32 ABI rules:
2352// ---
2353// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peck527da1b2010-11-23 03:31:01 +00002354// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002355// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peck527da1b2010-11-23 03:31:01 +00002356// f64 - Only passed in two aliased f32 registers if no int reg has been used
2357// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Sylvestre Ledru469de192014-08-11 18:04:46 +00002358// not used, it must be shadowed. If only A3 is available, shadow it and
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002359// go to stack.
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002360//
2361// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanakae2489122011-04-15 21:51:11 +00002362//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002363
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00002364static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2365 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002366 CCState &State, ArrayRef<MCPhysReg> F64Regs) {
Eric Christopher96e72c62015-01-29 23:27:36 +00002367 const MipsSubtarget &Subtarget = static_cast<const MipsSubtarget &>(
2368 State.getMachineFunction().getSubtarget());
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002369
Craig Topper840beec2014-04-04 05:16:06 +00002370 static const MCPhysReg IntRegs[] = { Mips::A0, Mips::A1, Mips::A2, Mips::A3 };
2371 static const MCPhysReg F32Regs[] = { Mips::F12, Mips::F14 };
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002372
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002373 // Do not process byval args here.
2374 if (ArgFlags.isByVal())
2375 return true;
Akira Hatanaka5e16c6a2011-05-24 19:18:33 +00002376
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002377 // Promote i8 and i16
Daniel Sandersd134c9d2014-12-02 20:40:27 +00002378 if (ArgFlags.isInReg() && !Subtarget.isLittle()) {
2379 if (LocVT == MVT::i8 || LocVT == MVT::i16 || LocVT == MVT::i32) {
2380 LocVT = MVT::i32;
2381 if (ArgFlags.isSExt())
2382 LocInfo = CCValAssign::SExtUpper;
2383 else if (ArgFlags.isZExt())
2384 LocInfo = CCValAssign::ZExtUpper;
2385 else
2386 LocInfo = CCValAssign::AExtUpper;
2387 }
2388 }
2389
2390 // Promote i8 and i16
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002391 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
2392 LocVT = MVT::i32;
2393 if (ArgFlags.isSExt())
2394 LocInfo = CCValAssign::SExt;
2395 else if (ArgFlags.isZExt())
2396 LocInfo = CCValAssign::ZExt;
2397 else
2398 LocInfo = CCValAssign::AExt;
2399 }
2400
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002401 unsigned Reg;
2402
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002403 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
2404 // is true: function is vararg, argument is 3rd or higher, there is previous
2405 // argument which is not f32 or f64.
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002406 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1 ||
2407 State.getFirstUnallocated(F32Regs) != ValNo;
Akira Hatanaka9e6a8cc2011-05-19 20:29:48 +00002408 unsigned OrigAlign = ArgFlags.getOrigAlign();
2409 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002410
2411 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002412 Reg = State.AllocateReg(IntRegs);
Akira Hatanaka9e6a8cc2011-05-19 20:29:48 +00002413 // If this is the first part of an i64 arg,
2414 // the allocated register must be either A0 or A2.
2415 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002416 Reg = State.AllocateReg(IntRegs);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002417 LocVT = MVT::i32;
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002418 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
2419 // Allocate int register and shadow next int register. If first
2420 // available register is Mips::A1 or Mips::A3, shadow it too.
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002421 Reg = State.AllocateReg(IntRegs);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002422 if (Reg == Mips::A1 || Reg == Mips::A3)
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002423 Reg = State.AllocateReg(IntRegs);
2424 State.AllocateReg(IntRegs);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002425 LocVT = MVT::i32;
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002426 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
2427 // we are guaranteed to find an available float register
2428 if (ValVT == MVT::f32) {
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002429 Reg = State.AllocateReg(F32Regs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002430 // Shadow int register
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002431 State.AllocateReg(IntRegs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002432 } else {
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002433 Reg = State.AllocateReg(F64Regs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002434 // Shadow int registers
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002435 unsigned Reg2 = State.AllocateReg(IntRegs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002436 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002437 State.AllocateReg(IntRegs);
2438 State.AllocateReg(IntRegs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002439 }
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002440 } else
2441 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002442
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002443 if (!Reg) {
2444 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3,
2445 OrigAlign);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002446 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002447 } else
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002448 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002449
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002450 return false;
Akira Hatanaka202f6402011-11-12 02:20:46 +00002451}
2452
Akira Hatanakabfb66242013-08-20 23:38:40 +00002453static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT,
2454 MVT LocVT, CCValAssign::LocInfo LocInfo,
2455 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002456 static const MCPhysReg F64Regs[] = { Mips::D6, Mips::D7 };
Akira Hatanakabfb66242013-08-20 23:38:40 +00002457
2458 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2459}
2460
2461static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT,
2462 MVT LocVT, CCValAssign::LocInfo LocInfo,
2463 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002464 static const MCPhysReg F64Regs[] = { Mips::D12_64, Mips::D14_64 };
Akira Hatanakabfb66242013-08-20 23:38:40 +00002465
2466 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2467}
2468
Reid Klecknerd3781742014-11-14 00:39:33 +00002469static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2470 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
2471 CCState &State) LLVM_ATTRIBUTE_UNUSED;
Reed Kotlerd5c41962014-11-13 23:37:45 +00002472
Akira Hatanaka202f6402011-11-12 02:20:46 +00002473#include "MipsGenCallingConv.inc"
2474
Akira Hatanakae2489122011-04-15 21:51:11 +00002475//===----------------------------------------------------------------------===//
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002476// Call Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002477//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002478
Akira Hatanaka61bbcce2011-09-23 00:58:33 +00002479// Return next O32 integer argument register.
2480static unsigned getNextIntArgReg(unsigned Reg) {
2481 assert((Reg == Mips::A0) || (Reg == Mips::A2));
2482 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
2483}
2484
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002485SDValue
2486MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002487 SDValue Chain, SDValue Arg, SDLoc DL,
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002488 bool IsTailCall, SelectionDAG &DAG) const {
2489 if (!IsTailCall) {
Mehdi Amini44ede332015-07-09 02:09:04 +00002490 SDValue PtrOff =
2491 DAG.getNode(ISD::ADD, DL, getPointerTy(DAG.getDataLayout()), StackPtr,
2492 DAG.getIntPtrConstant(Offset, DL));
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002493 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo(), false,
2494 false, 0);
2495 }
2496
2497 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2498 int FI = MFI->CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00002499 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002500 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(),
2501 /*isVolatile=*/ true, false, 0);
2502}
2503
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002504void MipsTargetLowering::
2505getOpndList(SmallVectorImpl<SDValue> &Ops,
2506 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
2507 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
Sasa Stankovic7072a792014-10-01 08:22:21 +00002508 bool IsCallReloc, CallLoweringInfo &CLI, SDValue Callee,
2509 SDValue Chain) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002510 // Insert node "GP copy globalreg" before call to function.
2511 //
2512 // R_MIPS_CALL* operators (emitted when non-internal functions are called
2513 // in PIC mode) allow symbols to be resolved via lazy binding.
2514 // The lazy binding stub requires GP to point to the GOT.
Sasa Stankovic7072a792014-10-01 08:22:21 +00002515 // Note that we don't need GP to point to the GOT for indirect calls
2516 // (when R_MIPS_CALL* is not used for the call) because Mips linker generates
2517 // lazy binding stub for a function only when R_MIPS_CALL* are the only relocs
2518 // used for the function (that is, Mips linker doesn't generate lazy binding
2519 // stub for a function whose address is taken in the program).
2520 if (IsPICCall && !InternalLinkage && IsCallReloc) {
Eric Christopher96e72c62015-01-29 23:27:36 +00002521 unsigned GPReg = ABI.IsN64() ? Mips::GP_64 : Mips::GP;
2522 EVT Ty = ABI.IsN64() ? MVT::i64 : MVT::i32;
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002523 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty)));
2524 }
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002525
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002526 // Build a sequence of copy-to-reg nodes chained together with token
2527 // chain and flag operands which copy the outgoing args into registers.
2528 // The InFlag in necessary since all emitted instructions must be
2529 // stuck together.
2530 SDValue InFlag;
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002531
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002532 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2533 Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, RegsToPass[i].first,
2534 RegsToPass[i].second, InFlag);
2535 InFlag = Chain.getValue(1);
2536 }
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002537
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002538 // Add argument registers to the end of the list so that they are
2539 // known live into the call.
2540 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2541 Ops.push_back(CLI.DAG.getRegister(RegsToPass[i].first,
2542 RegsToPass[i].second.getValueType()));
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002543
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002544 // Add a register mask operand representing the call-preserved registers.
Eric Christopher96e72c62015-01-29 23:27:36 +00002545 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00002546 const uint32_t *Mask =
2547 TRI->getCallPreservedMask(CLI.DAG.getMachineFunction(), CLI.CallConv);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002548 assert(Mask && "Missing call preserved mask for calling convention");
Eric Christopher1c29a652014-07-18 22:55:25 +00002549 if (Subtarget.inMips16HardFloat()) {
Reed Kotler783c7942013-05-10 22:25:39 +00002550 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
2551 llvm::StringRef Sym = G->getGlobal()->getName();
2552 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
Reed Kotler3230e722013-12-12 02:41:11 +00002553 if (F && F->hasFnAttribute("__Mips16RetHelper")) {
Reed Kotler783c7942013-05-10 22:25:39 +00002554 Mask = MipsRegisterInfo::getMips16RetHelperMask();
2555 }
2556 }
2557 }
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002558 Ops.push_back(CLI.DAG.getRegisterMask(Mask));
2559
2560 if (InFlag.getNode())
2561 Ops.push_back(InFlag);
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002562}
2563
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002564/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman624801e2009-01-26 03:15:54 +00002565/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002566SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00002567MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002568 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00002569 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002570 SDLoc DL = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00002571 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
2572 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
2573 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Akira Hatanakabeda2242012-07-31 18:46:41 +00002574 SDValue Chain = CLI.Chain;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002575 SDValue Callee = CLI.Callee;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002576 bool &IsTailCall = CLI.IsTailCall;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002577 CallingConv::ID CallConv = CLI.CallConv;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002578 bool IsVarArg = CLI.IsVarArg;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002579
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002580 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002581 MachineFrameInfo *MFI = MF.getFrameInfo();
Eric Christopher96e72c62015-01-29 23:27:36 +00002582 const TargetFrameLowering *TFL = Subtarget.getFrameLowering();
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002583 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +00002584 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002585
2586 // Analyze operands of the call, assigning locations to each operand.
2587 SmallVector<CCValAssign, 16> ArgLocs;
Daniel Sanders41a64c42014-11-07 11:10:48 +00002588 MipsCCState CCInfo(
2589 CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs, *DAG.getContext(),
2590 MipsCCState::getSpecialCallingConvForCallee(Callee.getNode(), Subtarget));
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002591
2592 // Allocate the reserved argument area. It seems strange to do this from the
2593 // caller side but removing it breaks the frame size calculation.
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002594 CCInfo.AllocateStack(ABI.GetCalleeAllocdArgSizeInBytes(CallConv), 1);
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002595
Daniel Sanderscfad1e32014-11-07 11:43:49 +00002596 CCInfo.AnalyzeCallOperands(Outs, CC_Mips, CLI.getArgs(), Callee.getNode());
Wesley Peck527da1b2010-11-23 03:31:01 +00002597
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002598 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka195a1e22011-06-08 17:39:33 +00002599 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka97ba7692012-07-26 23:27:01 +00002600
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002601 // Check if it's really possible to do a tail call.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002602 if (IsTailCall)
Daniel Sanders23e98772014-11-02 16:09:29 +00002603 IsTailCall = isEligibleForTailCallOptimization(
2604 CCInfo, NextStackOffset, *MF.getInfo<MipsFunctionInfo>());
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002605
Reid Kleckner5772b772014-04-24 20:14:34 +00002606 if (!IsTailCall && CLI.CS && CLI.CS->isMustTailCall())
2607 report_fatal_error("failed to perform tail call elimination on a call "
2608 "site marked musttail");
2609
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002610 if (IsTailCall)
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002611 ++NumTailCalls;
2612
Akira Hatanaka79738332011-09-19 20:26:02 +00002613 // Chain is the output chain of the last Load/Store or CopyToReg node.
2614 // ByValChain is the output chain of the last Memcpy node created for copying
2615 // byval arguments to the stack.
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002616 unsigned StackAlignment = TFL->getStackAlignment();
2617 NextStackOffset = RoundUpToAlignment(NextStackOffset, StackAlignment);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002618 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, DL, true);
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002619
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002620 if (!IsTailCall)
Andrew Trickad6d08a2013-05-29 22:03:55 +00002621 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffsetVal, DL);
Akira Hatanakabeda2242012-07-31 18:46:41 +00002622
Mehdi Amini44ede332015-07-09 02:09:04 +00002623 SDValue StackPtr =
2624 DAG.getCopyFromReg(Chain, DL, ABI.IsN64() ? Mips::SP_64 : Mips::SP,
2625 getPointerTy(DAG.getDataLayout()));
Akira Hatanaka195a1e22011-06-08 17:39:33 +00002626
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002627 // With EABI is it possible to have 16 args on registers.
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002628 std::deque< std::pair<unsigned, SDValue> > RegsToPass;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002629 SmallVector<SDValue, 8> MemOpChains;
Daniel Sanders23e98772014-11-02 16:09:29 +00002630
2631 CCInfo.rewindByValRegsInfo();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002632
2633 // Walk the register/memloc assignments, inserting copies/loads.
2634 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002635 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002636 CCValAssign &VA = ArgLocs[i];
Akira Hatanakab20a3252011-10-28 19:49:00 +00002637 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka19891f82011-11-12 02:34:50 +00002638 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002639 bool UseUpperBits = false;
Akira Hatanaka19891f82011-11-12 02:34:50 +00002640
2641 // ByVal Arg.
2642 if (Flags.isByVal()) {
Daniel Sanders23e98772014-11-02 16:09:29 +00002643 unsigned FirstByValReg, LastByValReg;
2644 unsigned ByValIdx = CCInfo.getInRegsParamsProcessed();
2645 CCInfo.getInRegsParamInfo(ByValIdx, FirstByValReg, LastByValReg);
2646
Akira Hatanaka19891f82011-11-12 02:34:50 +00002647 assert(Flags.getByValSize() &&
2648 "ByVal args of size 0 should have been ignored by front-end.");
Daniel Sanders23e98772014-11-02 16:09:29 +00002649 assert(ByValIdx < CCInfo.getInRegsParamsCount());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002650 assert(!IsTailCall &&
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002651 "Do not tail-call optimize if there is a byval argument.");
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002652 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002653 FirstByValReg, LastByValReg, Flags, Subtarget.isLittle(),
2654 VA);
Daniel Sanders23e98772014-11-02 16:09:29 +00002655 CCInfo.nextInRegsParam();
Akira Hatanaka19891f82011-11-12 02:34:50 +00002656 continue;
2657 }
Jia Liuf54f60f2012-02-28 07:46:26 +00002658
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002659 // Promote the value if needed.
2660 switch (VA.getLocInfo()) {
Daniel Sandersc43cda82014-11-07 16:54:21 +00002661 default:
2662 llvm_unreachable("Unknown loc info!");
Wesley Peck527da1b2010-11-23 03:31:01 +00002663 case CCValAssign::Full:
Akira Hatanakab20a3252011-10-28 19:49:00 +00002664 if (VA.isRegLoc()) {
2665 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
Akira Hatanaka3b7391d2013-03-05 22:20:28 +00002666 (ValVT == MVT::f64 && LocVT == MVT::i64) ||
2667 (ValVT == MVT::i64 && LocVT == MVT::f64))
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002668 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
Akira Hatanakab20a3252011-10-28 19:49:00 +00002669 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002670 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002671 Arg, DAG.getConstant(0, DL, MVT::i32));
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002672 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002673 Arg, DAG.getConstant(1, DL, MVT::i32));
Eric Christopher1c29a652014-07-18 22:55:25 +00002674 if (!Subtarget.isLittle())
Akira Hatanaka27916972011-04-15 19:52:08 +00002675 std::swap(Lo, Hi);
Jia Liuf54f60f2012-02-28 07:46:26 +00002676 unsigned LocRegLo = VA.getLocReg();
Akira Hatanaka61bbcce2011-09-23 00:58:33 +00002677 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2678 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2679 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002680 continue;
Wesley Peck527da1b2010-11-23 03:31:01 +00002681 }
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002682 }
2683 break;
Daniel Sanders23e98772014-11-02 16:09:29 +00002684 case CCValAssign::BCvt:
2685 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
2686 break;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002687 case CCValAssign::SExtUpper:
2688 UseUpperBits = true;
2689 // Fallthrough
Chris Lattner52f16de2008-03-17 06:57:02 +00002690 case CCValAssign::SExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002691 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002692 break;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002693 case CCValAssign::ZExtUpper:
2694 UseUpperBits = true;
2695 // Fallthrough
Chris Lattner52f16de2008-03-17 06:57:02 +00002696 case CCValAssign::ZExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002697 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002698 break;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002699 case CCValAssign::AExtUpper:
2700 UseUpperBits = true;
2701 // Fallthrough
Chris Lattner52f16de2008-03-17 06:57:02 +00002702 case CCValAssign::AExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002703 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002704 break;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002705 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002706
Daniel Sandersc43cda82014-11-07 16:54:21 +00002707 if (UseUpperBits) {
2708 unsigned ValSizeInBits = Outs[i].ArgVT.getSizeInBits();
2709 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
2710 Arg = DAG.getNode(
2711 ISD::SHL, DL, VA.getLocVT(), Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002712 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
Daniel Sandersc43cda82014-11-07 16:54:21 +00002713 }
2714
Wesley Peck527da1b2010-11-23 03:31:01 +00002715 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +00002716 // RegsToPass vector
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002717 if (VA.isRegLoc()) {
2718 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattner52f16de2008-03-17 06:57:02 +00002719 continue;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002720 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002721
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002722 // Register can't get to this point...
Chris Lattner52f16de2008-03-17 06:57:02 +00002723 assert(VA.isMemLoc());
Wesley Peck527da1b2010-11-23 03:31:01 +00002724
Wesley Peck527da1b2010-11-23 03:31:01 +00002725 // emit ISD::STORE whichs stores the
Chris Lattner52f16de2008-03-17 06:57:02 +00002726 // parameter value to a stack Location
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002727 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002728 Chain, Arg, DL, IsTailCall, DAG));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002729 }
2730
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002731 // Transform all store nodes into one single node because all store
2732 // nodes are independent of each other.
Wesley Peck527da1b2010-11-23 03:31:01 +00002733 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00002734 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002735
Bill Wendling24c79f22008-09-16 21:48:12 +00002736 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peck527da1b2010-11-23 03:31:01 +00002737 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2738 // node so that legalize doesn't hack it.
Eric Christopher96e72c62015-01-29 23:27:36 +00002739 bool IsPICCall = (ABI.IsN64() || IsPIC); // true if calls are translated to
2740 // jalr $25
Sasa Stankovic7072a792014-10-01 08:22:21 +00002741 bool GlobalOrExternal = false, InternalLinkage = false, IsCallReloc = false;
Akira Hatanakad6f1c582011-04-07 19:51:44 +00002742 SDValue CalleeLo;
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002743 EVT Ty = Callee.getValueType();
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002744
2745 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002746 if (IsPICCall) {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002747 const GlobalValue *Val = G->getGlobal();
2748 InternalLinkage = Val->hasInternalLinkage();
Akira Hatanakacf9a61b2012-12-13 03:17:29 +00002749
2750 if (InternalLinkage)
Eric Christopher96e72c62015-01-29 23:27:36 +00002751 Callee = getAddrLocal(G, DL, Ty, DAG, ABI.IsN32() || ABI.IsN64());
Sasa Stankovic7072a792014-10-01 08:22:21 +00002752 else if (LargeGOT) {
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002753 Callee = getAddrGlobalLargeGOT(G, DL, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002754 MipsII::MO_CALL_LO16, Chain,
2755 FuncInfo->callPtrInfo(Val));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002756 IsCallReloc = true;
2757 } else {
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002758 Callee = getAddrGlobal(G, DL, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002759 FuncInfo->callPtrInfo(Val));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002760 IsCallReloc = true;
2761 }
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002762 } else
Mehdi Amini44ede332015-07-09 02:09:04 +00002763 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL,
2764 getPointerTy(DAG.getDataLayout()), 0,
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002765 MipsII::MO_NO_FLAG);
Akira Hatanaka8e16aac2011-12-09 01:45:12 +00002766 GlobalOrExternal = true;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002767 }
2768 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002769 const char *Sym = S->getSymbol();
2770
Eric Christopher96e72c62015-01-29 23:27:36 +00002771 if (!ABI.IsN64() && !IsPIC) // !N64 && static
Mehdi Amini44ede332015-07-09 02:09:04 +00002772 Callee = DAG.getTargetExternalSymbol(
2773 Sym, getPointerTy(DAG.getDataLayout()), MipsII::MO_NO_FLAG);
Sasa Stankovic7072a792014-10-01 08:22:21 +00002774 else if (LargeGOT) {
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002775 Callee = getAddrGlobalLargeGOT(S, DL, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002776 MipsII::MO_CALL_LO16, Chain,
2777 FuncInfo->callPtrInfo(Sym));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002778 IsCallReloc = true;
2779 } else { // N64 || PIC
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002780 Callee = getAddrGlobal(S, DL, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002781 FuncInfo->callPtrInfo(Sym));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002782 IsCallReloc = true;
2783 }
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002784
Akira Hatanaka8e16aac2011-12-09 01:45:12 +00002785 GlobalOrExternal = true;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002786 }
2787
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002788 SmallVector<SDValue, 8> Ops(1, Chain);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002789 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002790
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002791 getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal, InternalLinkage,
Sasa Stankovic7072a792014-10-01 08:22:21 +00002792 IsCallReloc, CLI, Callee, Chain);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002793
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002794 if (IsTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +00002795 return DAG.getNode(MipsISD::TailCall, DL, MVT::Other, Ops);
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002796
Craig Topper48d114b2014-04-26 18:35:24 +00002797 Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, Ops);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002798 SDValue InFlag = Chain.getValue(1);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002799
Bruno Cardoso Lopes193e64c2010-01-30 18:32:07 +00002800 // Create the CALLSEQ_END node.
Akira Hatanaka97ba7692012-07-26 23:27:01 +00002801 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002802 DAG.getIntPtrConstant(0, DL, true), InFlag, DL);
Bruno Cardoso Lopes193e64c2010-01-30 18:32:07 +00002803 InFlag = Chain.getValue(1);
2804
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002805 // Handle result values, copying them out of physregs into vregs that we
2806 // return.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002807 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg, Ins, DL, DAG,
2808 InVals, CLI);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002809}
2810
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002811/// LowerCallResult - Lower the result values of a call into the
2812/// appropriate copies out of appropriate physical registers.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002813SDValue MipsTargetLowering::LowerCallResult(
2814 SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool IsVarArg,
2815 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG,
2816 SmallVectorImpl<SDValue> &InVals,
2817 TargetLowering::CallLoweringInfo &CLI) const {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002818 // Assign locations to each value returned by this call.
2819 SmallVector<CCValAssign, 16> RVLocs;
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002820 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
2821 *DAG.getContext());
2822 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips, CLI);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002823
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002824 // Copy all of the result registers out of their specified physreg.
2825 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Daniel Sandersae275e32014-09-25 12:15:05 +00002826 CCValAssign &VA = RVLocs[i];
2827 assert(VA.isRegLoc() && "Can only return in registers!");
2828
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002829 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(),
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002830 RVLocs[i].getLocVT(), InFlag);
2831 Chain = Val.getValue(1);
2832 InFlag = Val.getValue(2);
2833
Daniel Sandersae275e32014-09-25 12:15:05 +00002834 if (VA.isUpperBitsInLoc()) {
2835 unsigned ValSizeInBits = Ins[i].ArgVT.getSizeInBits();
2836 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
2837 unsigned Shift =
2838 VA.getLocInfo() == CCValAssign::ZExtUpper ? ISD::SRL : ISD::SRA;
2839 Val = DAG.getNode(
2840 Shift, DL, VA.getLocVT(), Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002841 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
Daniel Sandersae275e32014-09-25 12:15:05 +00002842 }
2843
2844 switch (VA.getLocInfo()) {
2845 default:
2846 llvm_unreachable("Unknown loc info!");
2847 case CCValAssign::Full:
2848 break;
2849 case CCValAssign::BCvt:
2850 Val = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Val);
2851 break;
2852 case CCValAssign::AExt:
2853 case CCValAssign::AExtUpper:
2854 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2855 break;
2856 case CCValAssign::ZExt:
2857 case CCValAssign::ZExtUpper:
2858 Val = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Val,
2859 DAG.getValueType(VA.getValVT()));
2860 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2861 break;
2862 case CCValAssign::SExt:
2863 case CCValAssign::SExtUpper:
2864 Val = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Val,
2865 DAG.getValueType(VA.getValVT()));
2866 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2867 break;
2868 }
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002869
2870 InVals.push_back(Val);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002871 }
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +00002872
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002873 return Chain;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002874}
2875
Daniel Sandersc43cda82014-11-07 16:54:21 +00002876static SDValue UnpackFromArgumentSlot(SDValue Val, const CCValAssign &VA,
2877 EVT ArgVT, SDLoc DL, SelectionDAG &DAG) {
2878 MVT LocVT = VA.getLocVT();
2879 EVT ValVT = VA.getValVT();
2880
2881 // Shift into the upper bits if necessary.
2882 switch (VA.getLocInfo()) {
2883 default:
2884 break;
2885 case CCValAssign::AExtUpper:
2886 case CCValAssign::SExtUpper:
2887 case CCValAssign::ZExtUpper: {
2888 unsigned ValSizeInBits = ArgVT.getSizeInBits();
2889 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
2890 unsigned Opcode =
2891 VA.getLocInfo() == CCValAssign::ZExtUpper ? ISD::SRL : ISD::SRA;
2892 Val = DAG.getNode(
2893 Opcode, DL, VA.getLocVT(), Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002894 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
Daniel Sandersc43cda82014-11-07 16:54:21 +00002895 break;
2896 }
2897 }
2898
2899 // If this is an value smaller than the argument slot size (32-bit for O32,
2900 // 64-bit for N32/N64), it has been promoted in some way to the argument slot
2901 // size. Extract the value and insert any appropriate assertions regarding
2902 // sign/zero extension.
2903 switch (VA.getLocInfo()) {
2904 default:
2905 llvm_unreachable("Unknown loc info!");
2906 case CCValAssign::Full:
2907 break;
2908 case CCValAssign::AExtUpper:
2909 case CCValAssign::AExt:
2910 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
2911 break;
2912 case CCValAssign::SExtUpper:
2913 case CCValAssign::SExt:
2914 Val = DAG.getNode(ISD::AssertSext, DL, LocVT, Val, DAG.getValueType(ValVT));
2915 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
2916 break;
2917 case CCValAssign::ZExtUpper:
2918 case CCValAssign::ZExt:
2919 Val = DAG.getNode(ISD::AssertZext, DL, LocVT, Val, DAG.getValueType(ValVT));
2920 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
2921 break;
2922 case CCValAssign::BCvt:
2923 Val = DAG.getNode(ISD::BITCAST, DL, ValVT, Val);
2924 break;
2925 }
2926
2927 return Val;
2928}
2929
Akira Hatanakae2489122011-04-15 21:51:11 +00002930//===----------------------------------------------------------------------===//
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002931// Formal Arguments Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002932//===----------------------------------------------------------------------===//
Wesley Peck527da1b2010-11-23 03:31:01 +00002933/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002934/// and generate load operations for arguments places on the stack.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002935SDValue
2936MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanakaaef55c82011-04-15 21:00:26 +00002937 CallingConv::ID CallConv,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002938 bool IsVarArg,
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00002939 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002940 SDLoc DL, SelectionDAG &DAG,
Akira Hatanakaaef55c82011-04-15 21:00:26 +00002941 SmallVectorImpl<SDValue> &InVals)
Akira Hatanakae2489122011-04-15 21:51:11 +00002942 const {
Bruno Cardoso Lopesa01ede22008-08-04 07:12:52 +00002943 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002944 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopes14033fb2007-08-28 05:08:16 +00002945 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002946
Dan Gohman31ae5862010-04-17 14:41:14 +00002947 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002948
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002949 // Used with vargs to acumulate store chains.
2950 std::vector<SDValue> OutChains;
2951
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002952 // Assign locations to all of the incoming arguments.
2953 SmallVector<CCValAssign, 16> ArgLocs;
Daniel Sanders23e98772014-11-02 16:09:29 +00002954 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
2955 *DAG.getContext());
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002956 CCInfo.AllocateStack(ABI.GetCalleeAllocdArgSizeInBytes(CallConv), 1);
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002957 Function::const_arg_iterator FuncArg =
2958 DAG.getMachineFunction().getFunction()->arg_begin();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002959
Daniel Sandersb70e27c2014-11-06 16:36:30 +00002960 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips_FixedArg);
Akira Hatanaka4866fe12012-10-30 19:37:25 +00002961 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(),
Daniel Sanders23e98772014-11-02 16:09:29 +00002962 CCInfo.getInRegsParamsCount() > 0);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002963
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00002964 unsigned CurArgIdx = 0;
Daniel Sanders23e98772014-11-02 16:09:29 +00002965 CCInfo.rewindByValRegsInfo();
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002966
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00002967 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002968 CCValAssign &VA = ArgLocs[i];
Andrew Trick05938a52015-02-16 18:10:47 +00002969 if (Ins[i].isOrigArg()) {
2970 std::advance(FuncArg, Ins[i].getOrigArgIndex() - CurArgIdx);
2971 CurArgIdx = Ins[i].getOrigArgIndex();
2972 }
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002973 EVT ValVT = VA.getValVT();
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002974 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2975 bool IsRegLoc = VA.isRegLoc();
2976
2977 if (Flags.isByVal()) {
Andrew Trick05938a52015-02-16 18:10:47 +00002978 assert(Ins[i].isOrigArg() && "Byval arguments cannot be implicit");
Daniel Sanders23e98772014-11-02 16:09:29 +00002979 unsigned FirstByValReg, LastByValReg;
2980 unsigned ByValIdx = CCInfo.getInRegsParamsProcessed();
2981 CCInfo.getInRegsParamInfo(ByValIdx, FirstByValReg, LastByValReg);
2982
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002983 assert(Flags.getByValSize() &&
2984 "ByVal args of size 0 should have been ignored by front-end.");
Daniel Sanders23e98772014-11-02 16:09:29 +00002985 assert(ByValIdx < CCInfo.getInRegsParamsCount());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002986 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002987 FirstByValReg, LastByValReg, VA, CCInfo);
Daniel Sanders23e98772014-11-02 16:09:29 +00002988 CCInfo.nextInRegsParam();
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002989 continue;
2990 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002991
2992 // Arguments stored on registers
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002993 if (IsRegLoc) {
Akira Hatanaka7d822522013-10-28 21:21:36 +00002994 MVT RegVT = VA.getLocVT();
Akira Hatanakacb4a1a82011-05-24 00:23:52 +00002995 unsigned ArgReg = VA.getLocReg();
Akira Hatanaka7d822522013-10-28 21:21:36 +00002996 const TargetRegisterClass *RC = getRegClassFor(RegVT);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002997
Wesley Peck527da1b2010-11-23 03:31:01 +00002998 // Transform the arguments stored on
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002999 // physical registers into virtual ones
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003000 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC);
3001 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
Wesley Peck527da1b2010-11-23 03:31:01 +00003002
Daniel Sandersc43cda82014-11-07 16:54:21 +00003003 ArgValue = UnpackFromArgumentSlot(ArgValue, VA, Ins[i].ArgVT, DL, DAG);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00003004
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003005 // Handle floating point arguments passed in integer registers and
3006 // long double arguments passed in floating point registers.
Akira Hatanaka104b7e32011-10-28 19:55:48 +00003007 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003008 (RegVT == MVT::i64 && ValVT == MVT::f64) ||
3009 (RegVT == MVT::f64 && ValVT == MVT::i64))
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003010 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
Eric Christopher96e72c62015-01-29 23:27:36 +00003011 else if (ABI.IsO32() && RegVT == MVT::i32 &&
Eric Christopherbf33a3c2014-07-02 23:18:40 +00003012 ValVT == MVT::f64) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003013 unsigned Reg2 = addLiveIn(DAG.getMachineFunction(),
Akira Hatanaka104b7e32011-10-28 19:55:48 +00003014 getNextIntArgReg(ArgReg), RC);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003015 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT);
Eric Christopher1c29a652014-07-18 22:55:25 +00003016 if (!Subtarget.isLittle())
Akira Hatanaka104b7e32011-10-28 19:55:48 +00003017 std::swap(ArgValue, ArgValue2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003018 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64,
Akira Hatanaka104b7e32011-10-28 19:55:48 +00003019 ArgValue, ArgValue2);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00003020 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003021
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003022 InVals.push_back(ArgValue);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003023 } else { // VA.isRegLoc()
Daniel Sandersc43cda82014-11-07 16:54:21 +00003024 MVT LocVT = VA.getLocVT();
3025
Eric Christopher96e72c62015-01-29 23:27:36 +00003026 if (ABI.IsO32()) {
Daniel Sandersc43cda82014-11-07 16:54:21 +00003027 // We ought to be able to use LocVT directly but O32 sets it to i32
3028 // when allocating floating point values to integer registers.
3029 // This shouldn't influence how we load the value into registers unless
3030 // we are targetting softfloat.
Eric Christophere8ae3e32015-05-07 23:10:21 +00003031 if (VA.getValVT().isFloatingPoint() && !Subtarget.useSoftFloat())
Daniel Sandersc43cda82014-11-07 16:54:21 +00003032 LocVT = VA.getValVT();
3033 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003034
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003035 // sanity check
3036 assert(VA.isMemLoc());
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003037
Wesley Peck527da1b2010-11-23 03:31:01 +00003038 // The stack pointer offset is relative to the caller stack frame.
Daniel Sandersc43cda82014-11-07 16:54:21 +00003039 int FI = MFI->CreateFixedObject(LocVT.getSizeInBits() / 8,
Akira Hatanakacb4a1a82011-05-24 00:23:52 +00003040 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003041
3042 // Create load nodes to retrieve arguments from the stack
Mehdi Amini44ede332015-07-09 02:09:04 +00003043 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Daniel Sandersc43cda82014-11-07 16:54:21 +00003044 SDValue ArgValue = DAG.getLoad(LocVT, DL, Chain, FIN,
3045 MachinePointerInfo::getFixedStack(FI),
3046 false, false, false, 0);
3047 OutChains.push_back(ArgValue.getValue(1));
3048
3049 ArgValue = UnpackFromArgumentSlot(ArgValue, VA, Ins[i].ArgVT, DL, DAG);
3050
3051 InVals.push_back(ArgValue);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003052 }
Reid Kleckner7a59e082014-05-12 22:01:27 +00003053 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003054
Reid Kleckner7a59e082014-05-12 22:01:27 +00003055 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Reid Kleckner79418562014-05-09 22:32:13 +00003056 // The mips ABIs for returning structs by value requires that we copy
3057 // the sret argument into $v0 for the return. Save the argument into
3058 // a virtual register so that we can access it from the return points.
Reid Kleckner7a59e082014-05-12 22:01:27 +00003059 if (Ins[i].Flags.isSRet()) {
Reid Kleckner79418562014-05-09 22:32:13 +00003060 unsigned Reg = MipsFI->getSRetReturnReg();
3061 if (!Reg) {
3062 Reg = MF.getRegInfo().createVirtualRegister(
Eric Christopher96e72c62015-01-29 23:27:36 +00003063 getRegClassFor(ABI.IsN64() ? MVT::i64 : MVT::i32));
Reid Kleckner79418562014-05-09 22:32:13 +00003064 MipsFI->setSRetReturnReg(Reg);
3065 }
Reid Kleckner7a59e082014-05-12 22:01:27 +00003066 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[i]);
Reid Kleckner79418562014-05-09 22:32:13 +00003067 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
Reid Kleckner7a59e082014-05-12 22:01:27 +00003068 break;
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003069 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003070 }
3071
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003072 if (IsVarArg)
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003073 writeVarArgRegs(OutChains, Chain, DL, DAG, CCInfo);
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003074
Wesley Peck527da1b2010-11-23 03:31:01 +00003075 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003076 // the size of Ins and InVals. This only happens when on varg functions
3077 if (!OutChains.empty()) {
3078 OutChains.push_back(Chain);
Craig Topper48d114b2014-04-26 18:35:24 +00003079 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003080 }
3081
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003082 return Chain;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003083}
3084
Akira Hatanakae2489122011-04-15 21:51:11 +00003085//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003086// Return Value Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00003087//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003088
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003089bool
3090MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003091 MachineFunction &MF, bool IsVarArg,
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003092 const SmallVectorImpl<ISD::OutputArg> &Outs,
3093 LLVMContext &Context) const {
3094 SmallVector<CCValAssign, 16> RVLocs;
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003095 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003096 return CCInfo.CheckReturn(Outs, RetCC_Mips);
3097}
3098
Petar Jovanovic5b436222015-03-23 12:28:13 +00003099bool
3100MipsTargetLowering::shouldSignExtendTypeInLibCall(EVT Type, bool IsSigned) const {
Eric Christophere8ae3e32015-05-07 23:10:21 +00003101 if (Subtarget.hasMips3() && Subtarget.useSoftFloat()) {
Petar Jovanovic5b436222015-03-23 12:28:13 +00003102 if (Type == MVT::i32)
3103 return true;
3104 }
3105 return IsSigned;
3106}
3107
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003108SDValue
3109MipsTargetLowering::LowerReturn(SDValue Chain,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003110 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003111 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003112 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003113 SDLoc DL, SelectionDAG &DAG) const {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003114 // CCValAssign - represent the assignment of
3115 // the return value to a location
3116 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003117 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003118
3119 // CCState - Info about the registers and stack slot.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003120 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, *DAG.getContext());
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003121
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003122 // Analyze return values.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003123 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003124
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003125 SDValue Flag;
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003126 SmallVector<SDValue, 4> RetOps(1, Chain);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003127
3128 // Copy the result values into the output registers.
3129 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003130 SDValue Val = OutVals[i];
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003131 CCValAssign &VA = RVLocs[i];
3132 assert(VA.isRegLoc() && "Can only return in registers!");
Daniel Sandersae275e32014-09-25 12:15:05 +00003133 bool UseUpperBits = false;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003134
Daniel Sandersae275e32014-09-25 12:15:05 +00003135 switch (VA.getLocInfo()) {
3136 default:
3137 llvm_unreachable("Unknown loc info!");
3138 case CCValAssign::Full:
3139 break;
3140 case CCValAssign::BCvt:
3141 Val = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Val);
3142 break;
3143 case CCValAssign::AExtUpper:
3144 UseUpperBits = true;
3145 // Fallthrough
3146 case CCValAssign::AExt:
3147 Val = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Val);
3148 break;
3149 case CCValAssign::ZExtUpper:
3150 UseUpperBits = true;
3151 // Fallthrough
3152 case CCValAssign::ZExt:
3153 Val = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Val);
3154 break;
3155 case CCValAssign::SExtUpper:
3156 UseUpperBits = true;
3157 // Fallthrough
3158 case CCValAssign::SExt:
3159 Val = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Val);
3160 break;
3161 }
3162
3163 if (UseUpperBits) {
3164 unsigned ValSizeInBits = Outs[i].ArgVT.getSizeInBits();
3165 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
3166 Val = DAG.getNode(
3167 ISD::SHL, DL, VA.getLocVT(), Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003168 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
Daniel Sandersae275e32014-09-25 12:15:05 +00003169 }
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003170
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003171 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003172
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003173 // Guarantee that all emitted copies are stuck together with flags.
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003174 Flag = Chain.getValue(1);
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003175 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003176 }
3177
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003178 // The mips ABIs for returning structs by value requires that we copy
3179 // the sret argument into $v0 for the return. We saved the argument into
3180 // a virtual register in the entry block, so now we copy the value out
3181 // and into $v0.
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003182 if (MF.getFunction()->hasStructRetAttr()) {
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003183 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3184 unsigned Reg = MipsFI->getSRetReturnReg();
3185
Wesley Peck527da1b2010-11-23 03:31:01 +00003186 if (!Reg)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003187 llvm_unreachable("sret virtual register not created in the entry block");
Mehdi Amini44ede332015-07-09 02:09:04 +00003188 SDValue Val =
3189 DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(DAG.getDataLayout()));
Eric Christopher96e72c62015-01-29 23:27:36 +00003190 unsigned V0 = ABI.IsN64() ? Mips::V0_64 : Mips::V0;
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003191
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003192 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003193 Flag = Chain.getValue(1);
Mehdi Amini44ede332015-07-09 02:09:04 +00003194 RetOps.push_back(DAG.getRegister(V0, getPointerTy(DAG.getDataLayout())));
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003195 }
3196
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003197 RetOps[0] = Chain; // Update chain.
Akira Hatanakaefff7b72012-07-10 00:19:06 +00003198
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003199 // Add the flag if we have it.
3200 if (Flag.getNode())
3201 RetOps.push_back(Flag);
3202
3203 // Return on Mips is always a "jr $ra"
Craig Topper48d114b2014-04-26 18:35:24 +00003204 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, RetOps);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003205}
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003206
Akira Hatanakae2489122011-04-15 21:51:11 +00003207//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003208// Mips Inline Assembly Support
Akira Hatanakae2489122011-04-15 21:51:11 +00003209//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003210
3211/// getConstraintType - Given a constraint letter, return the type of
3212/// constraint it is for this target.
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003213MipsTargetLowering::ConstraintType
3214MipsTargetLowering::getConstraintType(StringRef Constraint) const {
Daniel Sanders8b59af12013-11-12 12:56:01 +00003215 // Mips specific constraints
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003216 // GCC config/mips/constraints.md
3217 //
Wesley Peck527da1b2010-11-23 03:31:01 +00003218 // 'd' : An address register. Equivalent to r
3219 // unless generating MIPS16 code.
3220 // 'y' : Equivalent to r; retained for
3221 // backwards compatibility.
Eric Christophere3c494d2012-05-07 06:25:10 +00003222 // 'c' : A register suitable for use in an indirect
3223 // jump. This will always be $25 for -mabicalls.
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003224 // 'l' : The lo register. 1 word storage.
3225 // 'x' : The hilo register pair. Double word storage.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003226 if (Constraint.size() == 1) {
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003227 switch (Constraint[0]) {
3228 default : break;
Wesley Peck527da1b2010-11-23 03:31:01 +00003229 case 'd':
3230 case 'y':
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003231 case 'f':
Eric Christophere3c494d2012-05-07 06:25:10 +00003232 case 'c':
Eric Christopher9c492e62012-05-07 06:25:15 +00003233 case 'l':
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003234 case 'x':
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003235 return C_RegisterClass;
Jack Carter0e149b02013-03-04 21:33:15 +00003236 case 'R':
3237 return C_Memory;
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003238 }
3239 }
Daniel Sandersa73d8fe2015-03-24 11:26:34 +00003240
3241 if (Constraint == "ZC")
3242 return C_Memory;
3243
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003244 return TargetLowering::getConstraintType(Constraint);
3245}
3246
John Thompsone8360b72010-10-29 17:29:13 +00003247/// Examine constraint type and operand type and determine a weight value.
3248/// This object must already have been set up with the operand type
3249/// and the current alternative constraint selected.
3250TargetLowering::ConstraintWeight
3251MipsTargetLowering::getSingleConstraintMatchWeight(
3252 AsmOperandInfo &info, const char *constraint) const {
3253 ConstraintWeight weight = CW_Invalid;
3254 Value *CallOperandVal = info.CallOperandVal;
3255 // If we don't have a value, we can't do a match,
3256 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +00003257 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +00003258 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +00003259 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +00003260 // Look at the constraint type.
3261 switch (*constraint) {
3262 default:
3263 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
3264 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00003265 case 'd':
3266 case 'y':
John Thompsone8360b72010-10-29 17:29:13 +00003267 if (type->isIntegerTy())
3268 weight = CW_Register;
3269 break;
Daniel Sanders8b59af12013-11-12 12:56:01 +00003270 case 'f': // FPU or MSA register
Eric Christopher1c29a652014-07-18 22:55:25 +00003271 if (Subtarget.hasMSA() && type->isVectorTy() &&
Daniel Sanders8b59af12013-11-12 12:56:01 +00003272 cast<VectorType>(type)->getBitWidth() == 128)
3273 weight = CW_Register;
3274 else if (type->isFloatTy())
John Thompsone8360b72010-10-29 17:29:13 +00003275 weight = CW_Register;
3276 break;
Eric Christophere3c494d2012-05-07 06:25:10 +00003277 case 'c': // $25 for indirect jumps
Eric Christopher9c492e62012-05-07 06:25:15 +00003278 case 'l': // lo register
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003279 case 'x': // hilo register pair
Daniel Sanders8b59af12013-11-12 12:56:01 +00003280 if (type->isIntegerTy())
Eric Christophere3c494d2012-05-07 06:25:10 +00003281 weight = CW_SpecificReg;
Daniel Sanders8b59af12013-11-12 12:56:01 +00003282 break;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003283 case 'I': // signed 16 bit immediate
Eric Christopher7201e1b2012-05-07 03:13:42 +00003284 case 'J': // integer zero
Eric Christopher3ff88a02012-05-07 05:46:29 +00003285 case 'K': // unsigned 16 bit immediate
Eric Christopher1109b342012-05-07 05:46:37 +00003286 case 'L': // signed 32 bit immediate where lower 16 bits are 0
Eric Christophere07aa432012-05-07 05:46:43 +00003287 case 'N': // immediate in the range of -65535 to -1 (inclusive)
Eric Christopher470578a2012-05-07 05:46:48 +00003288 case 'O': // signed 15 bit immediate (+- 16383)
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003289 case 'P': // immediate in the range of 65535 to 1 (inclusive)
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003290 if (isa<ConstantInt>(CallOperandVal))
3291 weight = CW_Constant;
3292 break;
Jack Carter0e149b02013-03-04 21:33:15 +00003293 case 'R':
3294 weight = CW_Memory;
3295 break;
John Thompsone8360b72010-10-29 17:29:13 +00003296 }
3297 return weight;
3298}
3299
Akira Hatanaka7473b472013-08-14 00:21:25 +00003300/// This is a helper function to parse a physical register string and split it
3301/// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag
3302/// that is returned indicates whether parsing was successful. The second flag
3303/// is true if the numeric part exists.
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003304static std::pair<bool, bool> parsePhysicalReg(StringRef C, StringRef &Prefix,
3305 unsigned long long &Reg) {
Akira Hatanaka7473b472013-08-14 00:21:25 +00003306 if (C.front() != '{' || C.back() != '}')
3307 return std::make_pair(false, false);
3308
3309 // Search for the first numeric character.
3310 StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1;
3311 I = std::find_if(B, E, std::ptr_fun(isdigit));
3312
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003313 Prefix = StringRef(B, I - B);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003314
3315 // The second flag is set to false if no numeric characters were found.
3316 if (I == E)
3317 return std::make_pair(true, false);
3318
3319 // Parse the numeric characters.
3320 return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg),
3321 true);
3322}
3323
3324std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering::
Craig Topper6dc4a8bc2014-08-30 16:48:02 +00003325parseRegForInlineAsmConstraint(StringRef C, MVT VT) const {
Eric Christopherd9134482014-08-04 21:25:23 +00003326 const TargetRegisterInfo *TRI =
Eric Christopher96e72c62015-01-29 23:27:36 +00003327 Subtarget.getRegisterInfo();
Akira Hatanaka7473b472013-08-14 00:21:25 +00003328 const TargetRegisterClass *RC;
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003329 StringRef Prefix;
Akira Hatanaka7473b472013-08-14 00:21:25 +00003330 unsigned long long Reg;
3331
3332 std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg);
3333
3334 if (!R.first)
Craig Topper062a2ba2014-04-25 05:30:21 +00003335 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003336
3337 if ((Prefix == "hi" || Prefix == "lo")) { // Parse hi/lo.
3338 // No numeric characters follow "hi" or "lo".
3339 if (R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003340 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003341
3342 RC = TRI->getRegClass(Prefix == "hi" ?
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00003343 Mips::HI32RegClassID : Mips::LO32RegClassID);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003344 return std::make_pair(*(RC->begin()), RC);
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003345 } else if (Prefix.startswith("$msa")) {
Daniel Sanders8b59af12013-11-12 12:56:01 +00003346 // Parse $msa(ir|csr|access|save|modify|request|map|unmap)
3347
3348 // No numeric characters follow the name.
3349 if (R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003350 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003351
3352 Reg = StringSwitch<unsigned long long>(Prefix)
3353 .Case("$msair", Mips::MSAIR)
3354 .Case("$msacsr", Mips::MSACSR)
3355 .Case("$msaaccess", Mips::MSAAccess)
3356 .Case("$msasave", Mips::MSASave)
3357 .Case("$msamodify", Mips::MSAModify)
3358 .Case("$msarequest", Mips::MSARequest)
3359 .Case("$msamap", Mips::MSAMap)
3360 .Case("$msaunmap", Mips::MSAUnmap)
3361 .Default(0);
3362
3363 if (!Reg)
Craig Topper062a2ba2014-04-25 05:30:21 +00003364 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003365
3366 RC = TRI->getRegClass(Mips::MSACtrlRegClassID);
3367 return std::make_pair(Reg, RC);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003368 }
3369
3370 if (!R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003371 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003372
3373 if (Prefix == "$f") { // Parse $f0-$f31.
3374 // If the size of FP registers is 64-bit or Reg is an even number, select
3375 // the 64-bit register class. Otherwise, select the 32-bit register class.
3376 if (VT == MVT::Other)
Eric Christopher1c29a652014-07-18 22:55:25 +00003377 VT = (Subtarget.isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32;
Akira Hatanaka7473b472013-08-14 00:21:25 +00003378
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003379 RC = getRegClassFor(VT);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003380
3381 if (RC == &Mips::AFGR64RegClass) {
3382 assert(Reg % 2 == 0);
3383 Reg >>= 1;
3384 }
Daniel Sanders8b59af12013-11-12 12:56:01 +00003385 } else if (Prefix == "$fcc") // Parse $fcc0-$fcc7.
Akira Hatanaka7473b472013-08-14 00:21:25 +00003386 RC = TRI->getRegClass(Mips::FCCRegClassID);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003387 else if (Prefix == "$w") { // Parse $w0-$w31.
3388 RC = getRegClassFor((VT == MVT::Other) ? MVT::v16i8 : VT);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003389 } else { // Parse $0-$31.
3390 assert(Prefix == "$");
3391 RC = getRegClassFor((VT == MVT::Other) ? MVT::i32 : VT);
3392 }
3393
3394 assert(Reg < RC->getNumRegs());
3395 return std::make_pair(*(RC->begin() + Reg), RC);
3396}
3397
Eric Christophereaf77dc2011-06-29 19:33:04 +00003398/// Given a register class constraint, like 'r', if this corresponds directly
3399/// to an LLVM register class, return a register of 0 and the register class
3400/// pointer.
Eric Christopher11e4df72015-02-26 22:38:43 +00003401std::pair<unsigned, const TargetRegisterClass *>
3402MipsTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003403 StringRef Constraint,
Eric Christopher11e4df72015-02-26 22:38:43 +00003404 MVT VT) const {
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003405 if (Constraint.size() == 1) {
3406 switch (Constraint[0]) {
Eric Christopher9519c082011-06-29 19:04:31 +00003407 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
3408 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003409 case 'r':
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003410 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
Eric Christopher1c29a652014-07-18 22:55:25 +00003411 if (Subtarget.inMips16Mode())
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003412 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003413 return std::make_pair(0U, &Mips::GPR32RegClass);
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003414 }
Eric Christopher1c29a652014-07-18 22:55:25 +00003415 if (VT == MVT::i64 && !Subtarget.isGP64bit())
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003416 return std::make_pair(0U, &Mips::GPR32RegClass);
Eric Christopher1c29a652014-07-18 22:55:25 +00003417 if (VT == MVT::i64 && Subtarget.isGP64bit())
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003418 return std::make_pair(0U, &Mips::GPR64RegClass);
Eric Christopher58daf042012-05-07 03:13:22 +00003419 // This will generate an error message
Craig Topper062a2ba2014-04-25 05:30:21 +00003420 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003421 case 'f': // FPU or MSA register
3422 if (VT == MVT::v16i8)
3423 return std::make_pair(0U, &Mips::MSA128BRegClass);
3424 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
3425 return std::make_pair(0U, &Mips::MSA128HRegClass);
3426 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
3427 return std::make_pair(0U, &Mips::MSA128WRegClass);
3428 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
3429 return std::make_pair(0U, &Mips::MSA128DRegClass);
3430 else if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00003431 return std::make_pair(0U, &Mips::FGR32RegClass);
Eric Christopher1c29a652014-07-18 22:55:25 +00003432 else if ((VT == MVT::f64) && (!Subtarget.isSingleFloat())) {
3433 if (Subtarget.isFP64bit())
Craig Topperc7242e02012-04-20 07:30:17 +00003434 return std::make_pair(0U, &Mips::FGR64RegClass);
3435 return std::make_pair(0U, &Mips::AFGR64RegClass);
Akira Hatanakac669d7a2012-01-04 02:45:01 +00003436 }
Eric Christophere3c494d2012-05-07 06:25:10 +00003437 break;
3438 case 'c': // register suitable for indirect jump
3439 if (VT == MVT::i32)
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003440 return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass);
Eric Christophere3c494d2012-05-07 06:25:10 +00003441 assert(VT == MVT::i64 && "Unexpected type.");
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003442 return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass);
Eric Christopher9c492e62012-05-07 06:25:15 +00003443 case 'l': // register suitable for indirect jump
3444 if (VT == MVT::i32)
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00003445 return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass);
3446 return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass);
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003447 case 'x': // register suitable for indirect jump
3448 // Fixme: Not triggering the use of both hi and low
3449 // This will generate an error message
Craig Topper062a2ba2014-04-25 05:30:21 +00003450 return std::make_pair(0U, nullptr);
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003451 }
3452 }
Akira Hatanaka7473b472013-08-14 00:21:25 +00003453
3454 std::pair<unsigned, const TargetRegisterClass *> R;
3455 R = parseRegForInlineAsmConstraint(Constraint, VT);
3456
3457 if (R.second)
3458 return R;
3459
Eric Christopher11e4df72015-02-26 22:38:43 +00003460 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003461}
3462
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003463/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3464/// vector. If it is invalid, don't add anything to Ops.
3465void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3466 std::string &Constraint,
3467 std::vector<SDValue>&Ops,
3468 SelectionDAG &DAG) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003469 SDLoc DL(Op);
Craig Topper062a2ba2014-04-25 05:30:21 +00003470 SDValue Result;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003471
3472 // Only support length 1 constraints for now.
3473 if (Constraint.length() > 1) return;
3474
3475 char ConstraintLetter = Constraint[0];
3476 switch (ConstraintLetter) {
3477 default: break; // This will fall through to the generic implementation
3478 case 'I': // Signed 16 bit constant
3479 // If this fails, the parent routine will give an error
3480 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3481 EVT Type = Op.getValueType();
3482 int64_t Val = C->getSExtValue();
3483 if (isInt<16>(Val)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003484 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003485 break;
3486 }
3487 }
3488 return;
Eric Christopher7201e1b2012-05-07 03:13:42 +00003489 case 'J': // integer zero
3490 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3491 EVT Type = Op.getValueType();
3492 int64_t Val = C->getZExtValue();
3493 if (Val == 0) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003494 Result = DAG.getTargetConstant(0, DL, Type);
Eric Christopher7201e1b2012-05-07 03:13:42 +00003495 break;
3496 }
3497 }
3498 return;
Eric Christopher3ff88a02012-05-07 05:46:29 +00003499 case 'K': // unsigned 16 bit immediate
3500 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3501 EVT Type = Op.getValueType();
3502 uint64_t Val = (uint64_t)C->getZExtValue();
3503 if (isUInt<16>(Val)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003504 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopher3ff88a02012-05-07 05:46:29 +00003505 break;
3506 }
3507 }
3508 return;
Eric Christopher1109b342012-05-07 05:46:37 +00003509 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3510 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3511 EVT Type = Op.getValueType();
3512 int64_t Val = C->getSExtValue();
3513 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003514 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopher1109b342012-05-07 05:46:37 +00003515 break;
3516 }
3517 }
3518 return;
Eric Christophere07aa432012-05-07 05:46:43 +00003519 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3520 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3521 EVT Type = Op.getValueType();
3522 int64_t Val = C->getSExtValue();
3523 if ((Val >= -65535) && (Val <= -1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003524 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christophere07aa432012-05-07 05:46:43 +00003525 break;
3526 }
3527 }
3528 return;
Eric Christopher470578a2012-05-07 05:46:48 +00003529 case 'O': // signed 15 bit immediate
3530 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3531 EVT Type = Op.getValueType();
3532 int64_t Val = C->getSExtValue();
3533 if ((isInt<15>(Val))) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003534 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopher470578a2012-05-07 05:46:48 +00003535 break;
3536 }
3537 }
3538 return;
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003539 case 'P': // immediate in the range of 1 to 65535 (inclusive)
3540 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3541 EVT Type = Op.getValueType();
3542 int64_t Val = C->getSExtValue();
3543 if ((Val <= 65535) && (Val >= 1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003544 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003545 break;
3546 }
3547 }
3548 return;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003549 }
3550
3551 if (Result.getNode()) {
3552 Ops.push_back(Result);
3553 return;
3554 }
3555
3556 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3557}
3558
Mehdi Amini0cdec1e2015-07-09 02:09:40 +00003559bool MipsTargetLowering::isLegalAddressingMode(const DataLayout &DL,
3560 const AddrMode &AM, Type *Ty,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +00003561 unsigned AS) const {
Akira Hatanakaef839192012-11-17 00:25:41 +00003562 // No global is ever allowed as a base.
3563 if (AM.BaseGV)
3564 return false;
3565
3566 switch (AM.Scale) {
3567 case 0: // "r+i" or just "i", depending on HasBaseReg.
3568 break;
3569 case 1:
3570 if (!AM.HasBaseReg) // allow "r+i".
3571 break;
3572 return false; // disallow "r+r" or "r+r+i".
3573 default:
3574 return false;
3575 }
3576
3577 return true;
3578}
3579
3580bool
Dan Gohman2fe6bee2008-10-18 02:06:02 +00003581MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3582 // The Mips target isn't yet aware of offsets.
3583 return false;
3584}
Evan Cheng16993aa2009-10-27 19:56:55 +00003585
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003586EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003587 unsigned SrcAlign,
3588 bool IsMemset, bool ZeroMemset,
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003589 bool MemcpyStrSrc,
3590 MachineFunction &MF) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003591 if (Subtarget.hasMips64())
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003592 return MVT::i64;
3593
3594 return MVT::i32;
3595}
3596
Evan Cheng83896a52009-10-28 01:43:28 +00003597bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
3598 if (VT != MVT::f32 && VT != MVT::f64)
3599 return false;
Bruno Cardoso Lopesb02a9df2011-01-18 19:41:41 +00003600 if (Imm.isNegZero())
3601 return false;
Evan Cheng16993aa2009-10-27 19:56:55 +00003602 return Imm.isZero();
3603}
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003604
3605unsigned MipsTargetLowering::getJumpTableEncoding() const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003606 if (ABI.IsN64())
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003607 return MachineJumpTableInfo::EK_GPRel64BlockAddress;
Jia Liuf54f60f2012-02-28 07:46:26 +00003608
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003609 return TargetLowering::getJumpTableEncoding();
3610}
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003611
Eric Christopher824f42f2015-05-12 01:26:05 +00003612bool MipsTargetLowering::useSoftFloat() const {
3613 return Subtarget.useSoftFloat();
3614}
3615
Daniel Sandersf43e6872014-11-01 18:44:56 +00003616void MipsTargetLowering::copyByValRegs(
3617 SDValue Chain, SDLoc DL, std::vector<SDValue> &OutChains, SelectionDAG &DAG,
3618 const ISD::ArgFlagsTy &Flags, SmallVectorImpl<SDValue> &InVals,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003619 const Argument *FuncArg, unsigned FirstReg, unsigned LastReg,
3620 const CCValAssign &VA, MipsCCState &State) const {
Akira Hatanaka25dad192012-10-27 00:10:18 +00003621 MachineFunction &MF = DAG.getMachineFunction();
3622 MachineFrameInfo *MFI = MF.getFrameInfo();
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003623 unsigned GPRSizeInBytes = Subtarget.getGPRSizeInBytes();
Daniel Sanders23e98772014-11-02 16:09:29 +00003624 unsigned NumRegs = LastReg - FirstReg;
3625 unsigned RegAreaSize = NumRegs * GPRSizeInBytes;
Akira Hatanaka25dad192012-10-27 00:10:18 +00003626 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
3627 int FrameObjOffset;
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003628 ArrayRef<MCPhysReg> ByValArgRegs = ABI.GetByValArgRegs();
Akira Hatanaka25dad192012-10-27 00:10:18 +00003629
3630 if (RegAreaSize)
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003631 FrameObjOffset =
3632 (int)ABI.GetCalleeAllocdArgSizeInBytes(State.getCallingConv()) -
3633 (int)((ByValArgRegs.size() - FirstReg) * GPRSizeInBytes);
Akira Hatanaka25dad192012-10-27 00:10:18 +00003634 else
Daniel Sandersf43e6872014-11-01 18:44:56 +00003635 FrameObjOffset = VA.getLocMemOffset();
Akira Hatanaka25dad192012-10-27 00:10:18 +00003636
3637 // Create frame object.
Mehdi Amini44ede332015-07-09 02:09:04 +00003638 EVT PtrTy = getPointerTy(DAG.getDataLayout());
Akira Hatanaka25dad192012-10-27 00:10:18 +00003639 int FI = MFI->CreateFixedObject(FrameObjSize, FrameObjOffset, true);
3640 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
3641 InVals.push_back(FIN);
3642
Daniel Sanders23e98772014-11-02 16:09:29 +00003643 if (!NumRegs)
Akira Hatanaka25dad192012-10-27 00:10:18 +00003644 return;
3645
3646 // Copy arg registers.
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003647 MVT RegTy = MVT::getIntegerVT(GPRSizeInBytes * 8);
Akira Hatanaka25dad192012-10-27 00:10:18 +00003648 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3649
Daniel Sanders23e98772014-11-02 16:09:29 +00003650 for (unsigned I = 0; I < NumRegs; ++I) {
Daniel Sandersd7eba312014-11-07 12:21:37 +00003651 unsigned ArgReg = ByValArgRegs[FirstReg + I];
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003652 unsigned VReg = addLiveIn(MF, ArgReg, RC);
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003653 unsigned Offset = I * GPRSizeInBytes;
Akira Hatanaka25dad192012-10-27 00:10:18 +00003654 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003655 DAG.getConstant(Offset, DL, PtrTy));
Akira Hatanaka25dad192012-10-27 00:10:18 +00003656 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
3657 StorePtr, MachinePointerInfo(FuncArg, Offset),
3658 false, false, 0);
3659 OutChains.push_back(Store);
3660 }
3661}
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003662
3663// Copy byVal arg to registers and stack.
Daniel Sandersf43e6872014-11-01 18:44:56 +00003664void MipsTargetLowering::passByValArg(
3665 SDValue Chain, SDLoc DL,
3666 std::deque<std::pair<unsigned, SDValue>> &RegsToPass,
3667 SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003668 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg, unsigned FirstReg,
3669 unsigned LastReg, const ISD::ArgFlagsTy &Flags, bool isLittle,
3670 const CCValAssign &VA) const {
Daniel Sandersac272632014-05-23 13:18:02 +00003671 unsigned ByValSizeInBytes = Flags.getByValSize();
3672 unsigned OffsetInBytes = 0; // From beginning of struct
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003673 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
Daniel Sandersac272632014-05-23 13:18:02 +00003674 unsigned Alignment = std::min(Flags.getByValAlign(), RegSizeInBytes);
Mehdi Amini44ede332015-07-09 02:09:04 +00003675 EVT PtrTy = getPointerTy(DAG.getDataLayout()),
3676 RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
Daniel Sanders23e98772014-11-02 16:09:29 +00003677 unsigned NumRegs = LastReg - FirstReg;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003678
Daniel Sanders23e98772014-11-02 16:09:29 +00003679 if (NumRegs) {
Eric Christopher96e72c62015-01-29 23:27:36 +00003680 const ArrayRef<MCPhysReg> ArgRegs = ABI.GetByValArgRegs();
Daniel Sanders23e98772014-11-02 16:09:29 +00003681 bool LeftoverBytes = (NumRegs * RegSizeInBytes > ByValSizeInBytes);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003682 unsigned I = 0;
3683
3684 // Copy words to registers.
Daniel Sanders23e98772014-11-02 16:09:29 +00003685 for (; I < NumRegs - LeftoverBytes; ++I, OffsetInBytes += RegSizeInBytes) {
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003686 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003687 DAG.getConstant(OffsetInBytes, DL, PtrTy));
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003688 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
3689 MachinePointerInfo(), false, false, false,
3690 Alignment);
3691 MemOpChains.push_back(LoadVal.getValue(1));
Daniel Sanders23e98772014-11-02 16:09:29 +00003692 unsigned ArgReg = ArgRegs[FirstReg + I];
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003693 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
3694 }
3695
3696 // Return if the struct has been fully copied.
Daniel Sandersac272632014-05-23 13:18:02 +00003697 if (ByValSizeInBytes == OffsetInBytes)
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003698 return;
3699
3700 // Copy the remainder of the byval argument with sub-word loads and shifts.
3701 if (LeftoverBytes) {
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003702 SDValue Val;
3703
Daniel Sandersac272632014-05-23 13:18:02 +00003704 for (unsigned LoadSizeInBytes = RegSizeInBytes / 2, TotalBytesLoaded = 0;
3705 OffsetInBytes < ByValSizeInBytes; LoadSizeInBytes /= 2) {
3706 unsigned RemainingSizeInBytes = ByValSizeInBytes - OffsetInBytes;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003707
Daniel Sandersac272632014-05-23 13:18:02 +00003708 if (RemainingSizeInBytes < LoadSizeInBytes)
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003709 continue;
3710
3711 // Load subword.
3712 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003713 DAG.getConstant(OffsetInBytes, DL,
3714 PtrTy));
Daniel Sandersac272632014-05-23 13:18:02 +00003715 SDValue LoadVal = DAG.getExtLoad(
3716 ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr, MachinePointerInfo(),
Louis Gerbarg67474e32014-07-31 21:45:05 +00003717 MVT::getIntegerVT(LoadSizeInBytes * 8), false, false, false,
3718 Alignment);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003719 MemOpChains.push_back(LoadVal.getValue(1));
3720
3721 // Shift the loaded value.
3722 unsigned Shamt;
3723
3724 if (isLittle)
Daniel Sandersac272632014-05-23 13:18:02 +00003725 Shamt = TotalBytesLoaded * 8;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003726 else
Daniel Sandersac272632014-05-23 13:18:02 +00003727 Shamt = (RegSizeInBytes - (TotalBytesLoaded + LoadSizeInBytes)) * 8;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003728
3729 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003730 DAG.getConstant(Shamt, DL, MVT::i32));
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003731
3732 if (Val.getNode())
3733 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
3734 else
3735 Val = Shift;
3736
Daniel Sandersac272632014-05-23 13:18:02 +00003737 OffsetInBytes += LoadSizeInBytes;
3738 TotalBytesLoaded += LoadSizeInBytes;
3739 Alignment = std::min(Alignment, LoadSizeInBytes);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003740 }
3741
Daniel Sanders23e98772014-11-02 16:09:29 +00003742 unsigned ArgReg = ArgRegs[FirstReg + I];
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003743 RegsToPass.push_back(std::make_pair(ArgReg, Val));
3744 return;
3745 }
3746 }
3747
3748 // Copy remainder of byval arg to it with memcpy.
Daniel Sandersac272632014-05-23 13:18:02 +00003749 unsigned MemCpySize = ByValSizeInBytes - OffsetInBytes;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003750 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003751 DAG.getConstant(OffsetInBytes, DL, PtrTy));
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003752 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003753 DAG.getIntPtrConstant(VA.getLocMemOffset(), DL));
3754 Chain = DAG.getMemcpy(Chain, DL, Dst, Src,
3755 DAG.getConstant(MemCpySize, DL, PtrTy),
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003756 Alignment, /*isVolatile=*/false, /*AlwaysInline=*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00003757 /*isTailCall=*/false,
Nick Lewyckyaad475b2014-04-15 07:22:52 +00003758 MachinePointerInfo(), MachinePointerInfo());
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003759 MemOpChains.push_back(Chain);
3760}
Akira Hatanaka2a134022012-10-27 00:21:13 +00003761
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003762void MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003763 SDValue Chain, SDLoc DL,
3764 SelectionDAG &DAG,
Daniel Sanders853c2432014-11-01 18:13:52 +00003765 CCState &State) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003766 const ArrayRef<MCPhysReg> ArgRegs = ABI.GetVarArgRegs();
Tim Northover3b6b7ca2015-02-21 02:11:17 +00003767 unsigned Idx = State.getFirstUnallocated(ArgRegs);
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003768 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
3769 MVT RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003770 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3771 MachineFunction &MF = DAG.getMachineFunction();
3772 MachineFrameInfo *MFI = MF.getFrameInfo();
3773 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3774
3775 // Offset of the first variable argument from stack pointer.
3776 int VaArgOffset;
3777
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003778 if (ArgRegs.size() == Idx)
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003779 VaArgOffset =
Daniel Sanders853c2432014-11-01 18:13:52 +00003780 RoundUpToAlignment(State.getNextStackOffset(), RegSizeInBytes);
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003781 else {
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003782 VaArgOffset =
3783 (int)ABI.GetCalleeAllocdArgSizeInBytes(State.getCallingConv()) -
3784 (int)(RegSizeInBytes * (ArgRegs.size() - Idx));
3785 }
Akira Hatanaka2a134022012-10-27 00:21:13 +00003786
3787 // Record the frame index of the first variable argument
3788 // which is a value necessary to VASTART.
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003789 int FI = MFI->CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003790 MipsFI->setVarArgsFrameIndex(FI);
3791
3792 // Copy the integer registers that have not been used for argument passing
3793 // to the argument register save area. For O32, the save area is allocated
3794 // in the caller's stack frame, while for N32/64, it is allocated in the
3795 // callee's stack frame.
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003796 for (unsigned I = Idx; I < ArgRegs.size();
3797 ++I, VaArgOffset += RegSizeInBytes) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003798 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003799 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003800 FI = MFI->CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
Mehdi Amini44ede332015-07-09 02:09:04 +00003801 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Akira Hatanaka2a134022012-10-27 00:21:13 +00003802 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
3803 MachinePointerInfo(), false, false, 0);
Eric Christopher1c29a652014-07-18 22:55:25 +00003804 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(
3805 (Value *)nullptr);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003806 OutChains.push_back(Store);
3807 }
3808}
Daniel Sanders23e98772014-11-02 16:09:29 +00003809
3810void MipsTargetLowering::HandleByVal(CCState *State, unsigned &Size,
3811 unsigned Align) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003812 const TargetFrameLowering *TFL = Subtarget.getFrameLowering();
Daniel Sanders23e98772014-11-02 16:09:29 +00003813
3814 assert(Size && "Byval argument's size shouldn't be 0.");
3815
3816 Align = std::min(Align, TFL->getStackAlignment());
3817
3818 unsigned FirstReg = 0;
3819 unsigned NumRegs = 0;
3820
3821 if (State->getCallingConv() != CallingConv::Fast) {
3822 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
Eric Christopher96e72c62015-01-29 23:27:36 +00003823 const ArrayRef<MCPhysReg> IntArgRegs = ABI.GetByValArgRegs();
Daniel Sanders23e98772014-11-02 16:09:29 +00003824 // FIXME: The O32 case actually describes no shadow registers.
3825 const MCPhysReg *ShadowRegs =
Eric Christopher96e72c62015-01-29 23:27:36 +00003826 ABI.IsO32() ? IntArgRegs.data() : Mips64DPRegs;
Daniel Sanders23e98772014-11-02 16:09:29 +00003827
3828 // We used to check the size as well but we can't do that anymore since
3829 // CCState::HandleByVal() rounds up the size after calling this function.
3830 assert(!(Align % RegSizeInBytes) &&
3831 "Byval argument's alignment should be a multiple of"
3832 "RegSizeInBytes.");
3833
Tim Northover3b6b7ca2015-02-21 02:11:17 +00003834 FirstReg = State->getFirstUnallocated(IntArgRegs);
Daniel Sanders23e98772014-11-02 16:09:29 +00003835
3836 // If Align > RegSizeInBytes, the first arg register must be even.
3837 // FIXME: This condition happens to do the right thing but it's not the
3838 // right way to test it. We want to check that the stack frame offset
3839 // of the register is aligned.
3840 if ((Align > RegSizeInBytes) && (FirstReg % 2)) {
3841 State->AllocateReg(IntArgRegs[FirstReg], ShadowRegs[FirstReg]);
3842 ++FirstReg;
3843 }
3844
3845 // Mark the registers allocated.
3846 Size = RoundUpToAlignment(Size, RegSizeInBytes);
3847 for (unsigned I = FirstReg; Size > 0 && (I < IntArgRegs.size());
3848 Size -= RegSizeInBytes, ++I, ++NumRegs)
3849 State->AllocateReg(IntArgRegs[I], ShadowRegs[I]);
3850 }
3851
3852 State->addInRegsParamInfo(FirstReg, FirstReg + NumRegs);
3853}
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00003854
3855MachineBasicBlock *
3856MipsTargetLowering::emitPseudoSELECT(MachineInstr *MI, MachineBasicBlock *BB,
3857 bool isFPCmp, unsigned Opc) const {
3858 assert(!(Subtarget.hasMips4() || Subtarget.hasMips32()) &&
3859 "Subtarget already supports SELECT nodes with the use of"
3860 "conditional-move instructions.");
3861
3862 const TargetInstrInfo *TII =
Eric Christopher96e72c62015-01-29 23:27:36 +00003863 Subtarget.getInstrInfo();
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00003864 DebugLoc DL = MI->getDebugLoc();
3865
3866 // To "insert" a SELECT instruction, we actually have to insert the
3867 // diamond control-flow pattern. The incoming instruction knows the
3868 // destination vreg to set, the condition code register to branch on, the
3869 // true/false values to select between, and a branch opcode to use.
3870 const BasicBlock *LLVM_BB = BB->getBasicBlock();
3871 MachineFunction::iterator It = BB;
3872 ++It;
3873
3874 // thisMBB:
3875 // ...
3876 // TrueVal = ...
3877 // setcc r1, r2, r3
3878 // bNE r1, r0, copy1MBB
3879 // fallthrough --> copy0MBB
3880 MachineBasicBlock *thisMBB = BB;
3881 MachineFunction *F = BB->getParent();
3882 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
3883 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
3884 F->insert(It, copy0MBB);
3885 F->insert(It, sinkMBB);
3886
3887 // Transfer the remainder of BB and its successor edges to sinkMBB.
3888 sinkMBB->splice(sinkMBB->begin(), BB,
3889 std::next(MachineBasicBlock::iterator(MI)), BB->end());
3890 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
3891
3892 // Next, add the true and fallthrough blocks as its successors.
3893 BB->addSuccessor(copy0MBB);
3894 BB->addSuccessor(sinkMBB);
3895
3896 if (isFPCmp) {
3897 // bc1[tf] cc, sinkMBB
3898 BuildMI(BB, DL, TII->get(Opc))
3899 .addReg(MI->getOperand(1).getReg())
3900 .addMBB(sinkMBB);
3901 } else {
3902 // bne rs, $0, sinkMBB
3903 BuildMI(BB, DL, TII->get(Opc))
3904 .addReg(MI->getOperand(1).getReg())
3905 .addReg(Mips::ZERO)
3906 .addMBB(sinkMBB);
3907 }
3908
3909 // copy0MBB:
3910 // %FalseValue = ...
3911 // # fallthrough to sinkMBB
3912 BB = copy0MBB;
3913
3914 // Update machine-CFG edges
3915 BB->addSuccessor(sinkMBB);
3916
3917 // sinkMBB:
3918 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
3919 // ...
3920 BB = sinkMBB;
3921
3922 BuildMI(*BB, BB->begin(), DL,
3923 TII->get(Mips::PHI), MI->getOperand(0).getReg())
3924 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
3925 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB);
3926
3927 MI->eraseFromParent(); // The pseudo instruction is gone now.
3928
3929 return BB;
3930}
Daniel Sanders1440bb22015-01-09 17:21:30 +00003931
3932// FIXME? Maybe this could be a TableGen attribute on some registers and
3933// this table could be generated automatically from RegInfo.
Pat Gavlina717f252015-07-09 17:40:29 +00003934unsigned MipsTargetLowering::getRegisterByName(const char* RegName, EVT VT,
3935 SelectionDAG &DAG) const {
Daniel Sanders1440bb22015-01-09 17:21:30 +00003936 // Named registers is expected to be fairly rare. For now, just support $28
3937 // since the linux kernel uses it.
3938 if (Subtarget.isGP64bit()) {
3939 unsigned Reg = StringSwitch<unsigned>(RegName)
3940 .Case("$28", Mips::GP_64)
3941 .Default(0);
3942 if (Reg)
3943 return Reg;
3944 } else {
3945 unsigned Reg = StringSwitch<unsigned>(RegName)
3946 .Case("$28", Mips::GP)
3947 .Default(0);
3948 if (Reg)
3949 return Reg;
3950 }
3951 report_fatal_error("Invalid register name global variable");
3952}