blob: f95150f6a9c115ea6d77a650fcf06d8e3cbca83f [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 =
1586 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(*getDataLayout());
1587
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);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001726 SDLoc DL(GA);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001727 const GlobalValue *GV = GA->getGlobal();
Mehdi Amini44ede332015-07-09 02:09:04 +00001728 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001729
Hans Wennborgaea41202012-05-04 09:40:39 +00001730 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
1731
1732 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Hans Wennborg245917b2012-06-04 14:02:08 +00001733 // General Dynamic and Local Dynamic TLS Model.
1734 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
1735 : MipsII::MO_TLSGD;
1736
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001737 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag);
1738 SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT,
1739 getGlobalReg(DAG, PtrVT), TGA);
Akira Hatanakaf10ee842011-12-08 21:05:38 +00001740 unsigned PtrSize = PtrVT.getSizeInBits();
1741 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1742
Benjamin Kramer64ba50a2011-12-11 12:21:34 +00001743 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001744
1745 ArgListTy Args;
1746 ArgListEntry Entry;
1747 Entry.Node = Argument;
Akira Hatanakadee6c822011-12-08 20:34:32 +00001748 Entry.Ty = PtrTy;
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001749 Args.push_back(Entry);
Jia Liuf54f60f2012-02-28 07:46:26 +00001750
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00001751 TargetLowering::CallLoweringInfo CLI(DAG);
1752 CLI.setDebugLoc(DL).setChain(DAG.getEntryNode())
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00001753 .setCallee(CallingConv::C, PtrTy, TlsGetAddr, std::move(Args), 0);
Justin Holewinskiaa583972012-05-25 16:35:28 +00001754 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001755
Akira Hatanakabff84e12011-12-14 18:26:41 +00001756 SDValue Ret = CallResult.first;
1757
Hans Wennborgaea41202012-05-04 09:40:39 +00001758 if (model != TLSModel::LocalDynamic)
Akira Hatanakabff84e12011-12-14 18:26:41 +00001759 return Ret;
1760
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001761 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanakabff84e12011-12-14 18:26:41 +00001762 MipsII::MO_DTPREL_HI);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001763 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1764 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanakabff84e12011-12-14 18:26:41 +00001765 MipsII::MO_DTPREL_LO);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001766 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1767 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret);
1768 return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001769 }
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001770
1771 SDValue Offset;
Hans Wennborgaea41202012-05-04 09:40:39 +00001772 if (model == TLSModel::InitialExec) {
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001773 // Initial Exec TLS Model
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001774 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001775 MipsII::MO_GOTTPREL);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001776 TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT),
Akira Hatanakab049aef2012-02-24 22:34:47 +00001777 TGA);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001778 Offset = DAG.getLoad(PtrVT, DL,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001779 DAG.getEntryNode(), TGA, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001780 false, false, false, 0);
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001781 } else {
1782 // Local Exec TLS Model
Hans Wennborgaea41202012-05-04 09:40:39 +00001783 assert(model == TLSModel::LocalExec);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001784 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001785 MipsII::MO_TPREL_HI);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001786 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001787 MipsII::MO_TPREL_LO);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001788 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1789 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1790 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001791 }
1792
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001793 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
1794 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001795}
1796
1797SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001798lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +00001799{
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001800 JumpTableSDNode *N = cast<JumpTableSDNode>(Op);
1801 EVT Ty = Op.getValueType();
Akira Hatanaka30f97cf2013-09-25 00:30:25 +00001802
Eric Christopher96e72c62015-01-29 23:27:36 +00001803 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !ABI.IsN64())
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001804 return getAddrNonPIC(N, SDLoc(N), Ty, DAG);
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001805
Eric Christopher96e72c62015-01-29 23:27:36 +00001806 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +00001807}
1808
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001809SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001810lowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +00001811{
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001812 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
1813 EVT Ty = Op.getValueType();
Bruno Cardoso Lopes2db07582009-11-25 12:17:58 +00001814
Eric Christopher96e72c62015-01-29 23:27:36 +00001815 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !ABI.IsN64()) {
Eric Christopher36fe0282015-02-03 07:22:52 +00001816 const MipsTargetObjectFile *TLOF =
1817 static_cast<const MipsTargetObjectFile *>(
1818 getTargetMachine().getObjFileLowering());
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001819
Eric Christopher36fe0282015-02-03 07:22:52 +00001820 if (TLOF->IsConstantInSmallSection(N->getConstVal(), getTargetMachine()))
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001821 // %gp_rel relocation
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001822 return getAddrGPRel(N, SDLoc(N), Ty, DAG);
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001823
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001824 return getAddrNonPIC(N, SDLoc(N), Ty, DAG);
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001825 }
Bruno Cardoso Lopesfdb4cec2008-07-23 16:01:50 +00001826
Eric Christopher96e72c62015-01-29 23:27:36 +00001827 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +00001828}
1829
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001830SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman31ae5862010-04-17 14:41:14 +00001831 MachineFunction &MF = DAG.getMachineFunction();
1832 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1833
Andrew Trickef9de2a2013-05-25 02:42:55 +00001834 SDLoc DL(Op);
Dan Gohman31ae5862010-04-17 14:41:14 +00001835 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
Mehdi Amini44ede332015-07-09 02:09:04 +00001836 getPointerTy(MF.getDataLayout()));
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +00001837
1838 // vastart just stores the address of the VarArgsFrameIndex slot into the
1839 // memory location argument.
1840 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001841 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00001842 MachinePointerInfo(SV), false, false, 0);
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +00001843}
Jia Liuf54f60f2012-02-28 07:46:26 +00001844
Daniel Sanders2b553d42014-08-01 09:17:39 +00001845SDValue MipsTargetLowering::lowerVAARG(SDValue Op, SelectionDAG &DAG) const {
1846 SDNode *Node = Op.getNode();
1847 EVT VT = Node->getValueType(0);
1848 SDValue Chain = Node->getOperand(0);
1849 SDValue VAListPtr = Node->getOperand(1);
1850 unsigned Align = Node->getConstantOperandVal(3);
1851 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
1852 SDLoc DL(Node);
Eric Christopher96e72c62015-01-29 23:27:36 +00001853 unsigned ArgSlotSizeInBytes = (ABI.IsN32() || ABI.IsN64()) ? 8 : 4;
Daniel Sanders2b553d42014-08-01 09:17:39 +00001854
Mehdi Amini44ede332015-07-09 02:09:04 +00001855 SDValue VAListLoad =
1856 DAG.getLoad(getPointerTy(DAG.getDataLayout()), DL, Chain, VAListPtr,
1857 MachinePointerInfo(SV), false, false, false, 0);
Daniel Sanders2b553d42014-08-01 09:17:39 +00001858 SDValue VAList = VAListLoad;
1859
1860 // Re-align the pointer if necessary.
1861 // It should only ever be necessary for 64-bit types on O32 since the minimum
1862 // argument alignment is the same as the maximum type alignment for N32/N64.
1863 //
1864 // FIXME: We currently align too often. The code generator doesn't notice
1865 // when the pointer is still aligned from the last va_arg (or pair of
1866 // va_args for the i64 on O32 case).
1867 if (Align > getMinStackArgumentAlignment()) {
1868 assert(((Align & (Align-1)) == 0) && "Expected Align to be a power of 2");
1869
1870 VAList = DAG.getNode(ISD::ADD, DL, VAList.getValueType(), VAList,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001871 DAG.getConstant(Align - 1, DL, VAList.getValueType()));
Daniel Sanders2b553d42014-08-01 09:17:39 +00001872
1873 VAList = DAG.getNode(ISD::AND, DL, VAList.getValueType(), VAList,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001874 DAG.getConstant(-(int64_t)Align, DL,
Daniel Sanders2b553d42014-08-01 09:17:39 +00001875 VAList.getValueType()));
1876 }
1877
1878 // Increment the pointer, VAList, to the next vaarg.
1879 unsigned ArgSizeInBytes = getDataLayout()->getTypeAllocSize(VT.getTypeForEVT(*DAG.getContext()));
1880 SDValue Tmp3 = DAG.getNode(ISD::ADD, DL, VAList.getValueType(), VAList,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001881 DAG.getConstant(RoundUpToAlignment(ArgSizeInBytes,
1882 ArgSlotSizeInBytes),
1883 DL, VAList.getValueType()));
Daniel Sanders2b553d42014-08-01 09:17:39 +00001884 // Store the incremented VAList to the legalized pointer
1885 Chain = DAG.getStore(VAListLoad.getValue(1), DL, Tmp3, VAListPtr,
1886 MachinePointerInfo(SV), false, false, 0);
1887
1888 // In big-endian mode we must adjust the pointer when the load size is smaller
1889 // than the argument slot size. We must also reduce the known alignment to
1890 // match. For example in the N64 ABI, we must add 4 bytes to the offset to get
1891 // the correct half of the slot, and reduce the alignment from 8 (slot
1892 // alignment) down to 4 (type alignment).
1893 if (!Subtarget.isLittle() && ArgSizeInBytes < ArgSlotSizeInBytes) {
1894 unsigned Adjustment = ArgSlotSizeInBytes - ArgSizeInBytes;
1895 VAList = DAG.getNode(ISD::ADD, DL, VAListPtr.getValueType(), VAList,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001896 DAG.getIntPtrConstant(Adjustment, DL));
Daniel Sanders2b553d42014-08-01 09:17:39 +00001897 }
1898 // Load the actual argument out of the pointer VAList
1899 return DAG.getLoad(VT, DL, Chain, VAList, MachinePointerInfo(), false, false,
1900 false, 0);
1901}
1902
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001903static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG,
1904 bool HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001905 EVT TyX = Op.getOperand(0).getValueType();
1906 EVT TyY = Op.getOperand(1).getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001907 SDLoc DL(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001908 SDValue Const1 = DAG.getConstant(1, DL, MVT::i32);
1909 SDValue Const31 = DAG.getConstant(31, DL, MVT::i32);
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001910 SDValue Res;
1911
1912 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1913 // to i32.
1914 SDValue X = (TyX == MVT::f32) ?
1915 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1916 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1917 Const1);
1918 SDValue Y = (TyY == MVT::f32) ?
1919 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
1920 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
1921 Const1);
1922
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001923 if (HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001924 // ext E, Y, 31, 1 ; extract bit31 of Y
1925 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
1926 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
1927 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
1928 } else {
1929 // sll SllX, X, 1
1930 // srl SrlX, SllX, 1
1931 // srl SrlY, Y, 31
1932 // sll SllY, SrlX, 31
1933 // or Or, SrlX, SllY
1934 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1935 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1936 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
1937 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
1938 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
1939 }
1940
1941 if (TyX == MVT::f32)
1942 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
1943
1944 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001945 Op.getOperand(0),
1946 DAG.getConstant(0, DL, MVT::i32));
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001947 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001948}
1949
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001950static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG,
1951 bool HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001952 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
1953 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
1954 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001955 SDLoc DL(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001956 SDValue Const1 = DAG.getConstant(1, DL, MVT::i32);
Eric Christopher0713a9d2011-06-08 23:55:35 +00001957
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001958 // Bitcast to integer nodes.
1959 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
1960 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001961
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001962 if (HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001963 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
1964 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
1965 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001966 DAG.getConstant(WidthY - 1, DL, MVT::i32), Const1);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001967
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001968 if (WidthX > WidthY)
1969 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
1970 else if (WidthY > WidthX)
1971 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001972
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001973 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001974 DAG.getConstant(WidthX - 1, DL, MVT::i32), Const1,
1975 X);
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001976 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
1977 }
1978
1979 // (d)sll SllX, X, 1
1980 // (d)srl SrlX, SllX, 1
1981 // (d)srl SrlY, Y, width(Y)-1
1982 // (d)sll SllY, SrlX, width(Y)-1
1983 // or Or, SrlX, SllY
1984 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
1985 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
1986 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001987 DAG.getConstant(WidthY - 1, DL, MVT::i32));
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001988
1989 if (WidthX > WidthY)
1990 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
1991 else if (WidthY > WidthX)
1992 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
1993
1994 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001995 DAG.getConstant(WidthX - 1, DL, MVT::i32));
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001996 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
1997 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001998}
1999
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00002000SDValue
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002001MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00002002 if (Subtarget.isGP64bit())
2003 return lowerFCOPYSIGN64(Op, DAG, Subtarget.hasExtractInsert());
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002004
Eric Christopher1c29a652014-07-18 22:55:25 +00002005 return lowerFCOPYSIGN32(Op, DAG, Subtarget.hasExtractInsert());
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002006}
2007
Akira Hatanaka66277522011-06-02 00:24:44 +00002008SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002009lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes5444a7b2011-06-16 00:40:02 +00002010 // check the depth
2011 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka15506782011-06-07 18:58:42 +00002012 "Frame address can only be determined for current frame.");
Akira Hatanaka66277522011-06-02 00:24:44 +00002013
2014 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2015 MFI->setFrameAddressIsTaken(true);
2016 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002017 SDLoc DL(Op);
Eric Christopher96e72c62015-01-29 23:27:36 +00002018 SDValue FrameAddr = DAG.getCopyFromReg(
2019 DAG.getEntryNode(), DL, ABI.IsN64() ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka66277522011-06-02 00:24:44 +00002020 return FrameAddr;
2021}
2022
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002023SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op,
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002024 SelectionDAG &DAG) const {
Bill Wendling908bf812014-01-06 00:43:20 +00002025 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00002026 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00002027
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002028 // check the depth
2029 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
2030 "Return address can be determined only for current frame.");
2031
2032 MachineFunction &MF = DAG.getMachineFunction();
2033 MachineFrameInfo *MFI = MF.getFrameInfo();
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00002034 MVT VT = Op.getSimpleValueType();
Eric Christopher96e72c62015-01-29 23:27:36 +00002035 unsigned RA = ABI.IsN64() ? Mips::RA_64 : Mips::RA;
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002036 MFI->setReturnAddressIsTaken(true);
2037
2038 // Return RA, which contains the return address. Mark it an implicit live-in.
2039 unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00002040 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT);
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002041}
2042
Akira Hatanakac0b02062013-01-30 00:26:49 +00002043// An EH_RETURN is the result of lowering llvm.eh.return which in turn is
2044// generated from __builtin_eh_return (offset, handler)
2045// The effect of this is to adjust the stack pointer by "offset"
2046// and then branch to "handler".
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002047SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Akira Hatanakac0b02062013-01-30 00:26:49 +00002048 const {
2049 MachineFunction &MF = DAG.getMachineFunction();
2050 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2051
2052 MipsFI->setCallsEhReturn();
2053 SDValue Chain = Op.getOperand(0);
2054 SDValue Offset = Op.getOperand(1);
2055 SDValue Handler = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002056 SDLoc DL(Op);
Eric Christopher96e72c62015-01-29 23:27:36 +00002057 EVT Ty = ABI.IsN64() ? MVT::i64 : MVT::i32;
Akira Hatanakac0b02062013-01-30 00:26:49 +00002058
2059 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
2060 // EH_RETURN nodes, so that instructions are emitted back-to-back.
Eric Christopher96e72c62015-01-29 23:27:36 +00002061 unsigned OffsetReg = ABI.IsN64() ? Mips::V1_64 : Mips::V1;
2062 unsigned AddrReg = ABI.IsN64() ? Mips::V0_64 : Mips::V0;
Akira Hatanakac0b02062013-01-30 00:26:49 +00002063 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
2064 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
2065 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
2066 DAG.getRegister(OffsetReg, Ty),
Mehdi Amini44ede332015-07-09 02:09:04 +00002067 DAG.getRegister(AddrReg, getPointerTy(MF.getDataLayout())),
Akira Hatanakac0b02062013-01-30 00:26:49 +00002068 Chain.getValue(1));
2069}
2070
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002071SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
Akira Hatanaka5fd22482012-06-14 21:10:56 +00002072 SelectionDAG &DAG) const {
Eli Friedman26a48482011-07-27 22:21:52 +00002073 // FIXME: Need pseudo-fence for 'singlethread' fences
2074 // FIXME: Set SType for weaker fences where supported/appropriate.
2075 unsigned SType = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002076 SDLoc DL(Op);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002077 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002078 DAG.getConstant(SType, DL, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00002079}
2080
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002081SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
Akira Hatanaka5fd22482012-06-14 21:10:56 +00002082 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002083 SDLoc DL(Op);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002084 MVT VT = Subtarget.isGP64bit() ? MVT::i64 : MVT::i32;
2085
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002086 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2087 SDValue Shamt = Op.getOperand(2);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002088 // if shamt < (VT.bits):
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002089 // lo = (shl lo, shamt)
2090 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
2091 // else:
2092 // lo = 0
2093 // hi = (shl lo, shamt[4:0])
2094 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002095 DAG.getConstant(-1, DL, MVT::i32));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002096 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, VT, Lo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002097 DAG.getConstant(1, DL, VT));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002098 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, VT, ShiftRight1Lo, Not);
2099 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, VT, Hi, Shamt);
2100 SDValue Or = DAG.getNode(ISD::OR, DL, VT, ShiftLeftHi, ShiftRightLo);
2101 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, VT, Lo, Shamt);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002102 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
Daniel Sanders301f9372015-04-29 12:28:58 +00002103 DAG.getConstant(VT.getSizeInBits(), DL, MVT::i32));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002104 Lo = DAG.getNode(ISD::SELECT, DL, VT, Cond,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002105 DAG.getConstant(0, DL, VT), ShiftLeftLo);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002106 Hi = DAG.getNode(ISD::SELECT, DL, VT, Cond, ShiftLeftLo, Or);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002107
2108 SDValue Ops[2] = {Lo, Hi};
Craig Topper64941d92014-04-27 19:20:57 +00002109 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002110}
2111
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002112SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002113 bool IsSRA) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002114 SDLoc DL(Op);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002115 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2116 SDValue Shamt = Op.getOperand(2);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002117 MVT VT = Subtarget.isGP64bit() ? MVT::i64 : MVT::i32;
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002118
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002119 // if shamt < (VT.bits):
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002120 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
2121 // if isSRA:
2122 // hi = (sra hi, shamt)
2123 // else:
2124 // hi = (srl hi, shamt)
2125 // else:
2126 // if isSRA:
2127 // lo = (sra hi, shamt[4:0])
2128 // hi = (sra hi, 31)
2129 // else:
2130 // lo = (srl hi, shamt[4:0])
2131 // hi = 0
2132 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002133 DAG.getConstant(-1, DL, MVT::i32));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002134 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, VT, Hi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002135 DAG.getConstant(1, DL, VT));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002136 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, VT, ShiftLeft1Hi, Not);
2137 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, VT, Lo, Shamt);
2138 SDValue Or = DAG.getNode(ISD::OR, DL, VT, ShiftLeftHi, ShiftRightLo);
2139 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL,
2140 DL, VT, Hi, Shamt);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002141 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
Daniel Sanders301f9372015-04-29 12:28:58 +00002142 DAG.getConstant(VT.getSizeInBits(), DL, MVT::i32));
2143 SDValue Ext = DAG.getNode(ISD::SRA, DL, VT, Hi,
2144 DAG.getConstant(VT.getSizeInBits() - 1, DL, VT));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002145 Lo = DAG.getNode(ISD::SELECT, DL, VT, Cond, ShiftRightHi, Or);
2146 Hi = DAG.getNode(ISD::SELECT, DL, VT, Cond,
Daniel Sanders301f9372015-04-29 12:28:58 +00002147 IsSRA ? Ext : DAG.getConstant(0, DL, VT), ShiftRightHi);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002148
2149 SDValue Ops[2] = {Lo, Hi};
Craig Topper64941d92014-04-27 19:20:57 +00002150 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002151}
2152
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002153static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002154 SDValue Chain, SDValue Src, unsigned Offset) {
Akira Hatanaka95866182012-06-13 19:06:08 +00002155 SDValue Ptr = LD->getBasePtr();
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002156 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
Akira Hatanaka95866182012-06-13 19:06:08 +00002157 EVT BasePtrVT = Ptr.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002158 SDLoc DL(LD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002159 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
2160
2161 if (Offset)
Akira Hatanaka95866182012-06-13 19:06:08 +00002162 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002163 DAG.getConstant(Offset, DL, BasePtrVT));
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002164
2165 SDValue Ops[] = { Chain, Ptr, Src };
Craig Topper206fcd42014-04-26 19:29:41 +00002166 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002167 LD->getMemOperand());
2168}
2169
2170// Expand an unaligned 32 or 64-bit integer load node.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002171SDValue MipsTargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002172 LoadSDNode *LD = cast<LoadSDNode>(Op);
2173 EVT MemVT = LD->getMemoryVT();
2174
Eric Christopher1c29a652014-07-18 22:55:25 +00002175 if (Subtarget.systemSupportsUnalignedAccess())
Daniel Sandersac272632014-05-23 13:18:02 +00002176 return Op;
2177
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002178 // Return if load is aligned or if MemVT is neither i32 nor i64.
2179 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
2180 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
2181 return SDValue();
2182
Eric Christopher1c29a652014-07-18 22:55:25 +00002183 bool IsLittle = Subtarget.isLittle();
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002184 EVT VT = Op.getValueType();
2185 ISD::LoadExtType ExtType = LD->getExtensionType();
2186 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
2187
2188 assert((VT == MVT::i32) || (VT == MVT::i64));
2189
2190 // Expand
2191 // (set dst, (i64 (load baseptr)))
2192 // to
2193 // (set tmp, (ldl (add baseptr, 7), undef))
2194 // (set dst, (ldr baseptr, tmp))
2195 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002196 SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002197 IsLittle ? 7 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002198 return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002199 IsLittle ? 0 : 7);
2200 }
2201
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002202 SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002203 IsLittle ? 3 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002204 SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002205 IsLittle ? 0 : 3);
2206
2207 // Expand
2208 // (set dst, (i32 (load baseptr))) or
2209 // (set dst, (i64 (sextload baseptr))) or
2210 // (set dst, (i64 (extload baseptr)))
2211 // to
2212 // (set tmp, (lwl (add baseptr, 3), undef))
2213 // (set dst, (lwr baseptr, tmp))
2214 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
2215 (ExtType == ISD::EXTLOAD))
2216 return LWR;
2217
2218 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
2219
2220 // Expand
2221 // (set dst, (i64 (zextload baseptr)))
2222 // to
2223 // (set tmp0, (lwl (add baseptr, 3), undef))
2224 // (set tmp1, (lwr baseptr, tmp0))
2225 // (set tmp2, (shl tmp1, 32))
2226 // (set dst, (srl tmp2, 32))
Andrew Trickef9de2a2013-05-25 02:42:55 +00002227 SDLoc DL(LD);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002228 SDValue Const32 = DAG.getConstant(32, DL, MVT::i32);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002229 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
Akira Hatanaka67346852012-06-04 17:46:29 +00002230 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
2231 SDValue Ops[] = { SRL, LWR.getValue(1) };
Craig Topper64941d92014-04-27 19:20:57 +00002232 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002233}
2234
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002235static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002236 SDValue Chain, unsigned Offset) {
Akira Hatanaka95866182012-06-13 19:06:08 +00002237 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
2238 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002239 SDLoc DL(SD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002240 SDVTList VTList = DAG.getVTList(MVT::Other);
2241
2242 if (Offset)
Akira Hatanaka95866182012-06-13 19:06:08 +00002243 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002244 DAG.getConstant(Offset, DL, BasePtrVT));
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002245
2246 SDValue Ops[] = { Chain, Value, Ptr };
Craig Topper206fcd42014-04-26 19:29:41 +00002247 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002248 SD->getMemOperand());
2249}
2250
2251// Expand an unaligned 32 or 64-bit integer store node.
Akira Hatanakad82ee942013-05-16 20:45:17 +00002252static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG,
2253 bool IsLittle) {
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002254 SDValue Value = SD->getValue(), Chain = SD->getChain();
2255 EVT VT = Value.getValueType();
2256
2257 // Expand
2258 // (store val, baseptr) or
2259 // (truncstore val, baseptr)
2260 // to
2261 // (swl val, (add baseptr, 3))
2262 // (swr val, baseptr)
2263 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002264 SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002265 IsLittle ? 3 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002266 return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002267 }
2268
2269 assert(VT == MVT::i64);
2270
2271 // Expand
2272 // (store val, baseptr)
2273 // to
2274 // (sdl val, (add baseptr, 7))
2275 // (sdr val, baseptr)
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002276 SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2277 return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002278}
2279
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002280// Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr).
2281static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG) {
2282 SDValue Val = SD->getValue();
2283
2284 if (Val.getOpcode() != ISD::FP_TO_SINT)
2285 return SDValue();
2286
2287 EVT FPTy = EVT::getFloatingPointVT(Val.getValueSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002288 SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy,
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002289 Val.getOperand(0));
2290
Andrew Trickef9de2a2013-05-25 02:42:55 +00002291 return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(),
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002292 SD->getPointerInfo(), SD->isVolatile(),
2293 SD->isNonTemporal(), SD->getAlignment());
2294}
2295
Akira Hatanakad82ee942013-05-16 20:45:17 +00002296SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2297 StoreSDNode *SD = cast<StoreSDNode>(Op);
2298 EVT MemVT = SD->getMemoryVT();
2299
2300 // Lower unaligned integer stores.
Eric Christopher1c29a652014-07-18 22:55:25 +00002301 if (!Subtarget.systemSupportsUnalignedAccess() &&
Daniel Sandersac272632014-05-23 13:18:02 +00002302 (SD->getAlignment() < MemVT.getSizeInBits() / 8) &&
Akira Hatanakad82ee942013-05-16 20:45:17 +00002303 ((MemVT == MVT::i32) || (MemVT == MVT::i64)))
Eric Christopher1c29a652014-07-18 22:55:25 +00002304 return lowerUnalignedIntStore(SD, DAG, Subtarget.isLittle());
Akira Hatanakad82ee942013-05-16 20:45:17 +00002305
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002306 return lowerFP_TO_SINT_STORE(SD, DAG);
Akira Hatanakad82ee942013-05-16 20:45:17 +00002307}
2308
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002309SDValue MipsTargetLowering::lowerADD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002310 if (Op->getOperand(0).getOpcode() != ISD::FRAMEADDR
2311 || cast<ConstantSDNode>
2312 (Op->getOperand(0).getOperand(0))->getZExtValue() != 0
2313 || Op->getOperand(1).getOpcode() != ISD::FRAME_TO_ARGS_OFFSET)
2314 return SDValue();
2315
2316 // The pattern
2317 // (add (frameaddr 0), (frame_to_args_offset))
2318 // results from lowering llvm.eh.dwarf.cfa intrinsic. Transform it to
2319 // (add FrameObject, 0)
2320 // where FrameObject is a fixed StackObject with offset 0 which points to
2321 // the old stack pointer.
2322 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2323 EVT ValTy = Op->getValueType(0);
2324 int FI = MFI->CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
2325 SDValue InArgsAddr = DAG.getFrameIndex(FI, ValTy);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002326 SDLoc DL(Op);
2327 return DAG.getNode(ISD::ADD, DL, ValTy, InArgsAddr,
2328 DAG.getConstant(0, DL, ValTy));
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002329}
2330
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002331SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op,
2332 SelectionDAG &DAG) const {
2333 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002334 SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy,
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002335 Op.getOperand(0));
Andrew Trickef9de2a2013-05-25 02:42:55 +00002336 return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc);
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002337}
2338
Akira Hatanakae2489122011-04-15 21:51:11 +00002339//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002340// Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002341//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002342
Akira Hatanakae2489122011-04-15 21:51:11 +00002343//===----------------------------------------------------------------------===//
Wesley Peck527da1b2010-11-23 03:31:01 +00002344// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002345// Mips O32 ABI rules:
2346// ---
2347// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peck527da1b2010-11-23 03:31:01 +00002348// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002349// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peck527da1b2010-11-23 03:31:01 +00002350// f64 - Only passed in two aliased f32 registers if no int reg has been used
2351// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Sylvestre Ledru469de192014-08-11 18:04:46 +00002352// not used, it must be shadowed. If only A3 is available, shadow it and
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002353// go to stack.
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002354//
2355// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanakae2489122011-04-15 21:51:11 +00002356//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002357
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00002358static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2359 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002360 CCState &State, ArrayRef<MCPhysReg> F64Regs) {
Eric Christopher96e72c62015-01-29 23:27:36 +00002361 const MipsSubtarget &Subtarget = static_cast<const MipsSubtarget &>(
2362 State.getMachineFunction().getSubtarget());
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002363
Craig Topper840beec2014-04-04 05:16:06 +00002364 static const MCPhysReg IntRegs[] = { Mips::A0, Mips::A1, Mips::A2, Mips::A3 };
2365 static const MCPhysReg F32Regs[] = { Mips::F12, Mips::F14 };
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002366
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002367 // Do not process byval args here.
2368 if (ArgFlags.isByVal())
2369 return true;
Akira Hatanaka5e16c6a2011-05-24 19:18:33 +00002370
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002371 // Promote i8 and i16
Daniel Sandersd134c9d2014-12-02 20:40:27 +00002372 if (ArgFlags.isInReg() && !Subtarget.isLittle()) {
2373 if (LocVT == MVT::i8 || LocVT == MVT::i16 || LocVT == MVT::i32) {
2374 LocVT = MVT::i32;
2375 if (ArgFlags.isSExt())
2376 LocInfo = CCValAssign::SExtUpper;
2377 else if (ArgFlags.isZExt())
2378 LocInfo = CCValAssign::ZExtUpper;
2379 else
2380 LocInfo = CCValAssign::AExtUpper;
2381 }
2382 }
2383
2384 // Promote i8 and i16
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002385 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
2386 LocVT = MVT::i32;
2387 if (ArgFlags.isSExt())
2388 LocInfo = CCValAssign::SExt;
2389 else if (ArgFlags.isZExt())
2390 LocInfo = CCValAssign::ZExt;
2391 else
2392 LocInfo = CCValAssign::AExt;
2393 }
2394
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002395 unsigned Reg;
2396
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002397 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
2398 // is true: function is vararg, argument is 3rd or higher, there is previous
2399 // argument which is not f32 or f64.
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002400 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1 ||
2401 State.getFirstUnallocated(F32Regs) != ValNo;
Akira Hatanaka9e6a8cc2011-05-19 20:29:48 +00002402 unsigned OrigAlign = ArgFlags.getOrigAlign();
2403 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002404
2405 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002406 Reg = State.AllocateReg(IntRegs);
Akira Hatanaka9e6a8cc2011-05-19 20:29:48 +00002407 // If this is the first part of an i64 arg,
2408 // the allocated register must be either A0 or A2.
2409 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002410 Reg = State.AllocateReg(IntRegs);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002411 LocVT = MVT::i32;
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002412 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
2413 // Allocate int register and shadow next int register. If first
2414 // available register is Mips::A1 or Mips::A3, shadow it too.
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002415 Reg = State.AllocateReg(IntRegs);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002416 if (Reg == Mips::A1 || Reg == Mips::A3)
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002417 Reg = State.AllocateReg(IntRegs);
2418 State.AllocateReg(IntRegs);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002419 LocVT = MVT::i32;
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002420 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
2421 // we are guaranteed to find an available float register
2422 if (ValVT == MVT::f32) {
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002423 Reg = State.AllocateReg(F32Regs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002424 // Shadow int register
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002425 State.AllocateReg(IntRegs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002426 } else {
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002427 Reg = State.AllocateReg(F64Regs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002428 // Shadow int registers
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002429 unsigned Reg2 = State.AllocateReg(IntRegs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002430 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002431 State.AllocateReg(IntRegs);
2432 State.AllocateReg(IntRegs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002433 }
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002434 } else
2435 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002436
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002437 if (!Reg) {
2438 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3,
2439 OrigAlign);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002440 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002441 } else
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002442 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002443
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002444 return false;
Akira Hatanaka202f6402011-11-12 02:20:46 +00002445}
2446
Akira Hatanakabfb66242013-08-20 23:38:40 +00002447static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT,
2448 MVT LocVT, CCValAssign::LocInfo LocInfo,
2449 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002450 static const MCPhysReg F64Regs[] = { Mips::D6, Mips::D7 };
Akira Hatanakabfb66242013-08-20 23:38:40 +00002451
2452 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2453}
2454
2455static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT,
2456 MVT LocVT, CCValAssign::LocInfo LocInfo,
2457 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002458 static const MCPhysReg F64Regs[] = { Mips::D12_64, Mips::D14_64 };
Akira Hatanakabfb66242013-08-20 23:38:40 +00002459
2460 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2461}
2462
Reid Klecknerd3781742014-11-14 00:39:33 +00002463static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2464 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
2465 CCState &State) LLVM_ATTRIBUTE_UNUSED;
Reed Kotlerd5c41962014-11-13 23:37:45 +00002466
Akira Hatanaka202f6402011-11-12 02:20:46 +00002467#include "MipsGenCallingConv.inc"
2468
Akira Hatanakae2489122011-04-15 21:51:11 +00002469//===----------------------------------------------------------------------===//
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002470// Call Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002471//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002472
Akira Hatanaka61bbcce2011-09-23 00:58:33 +00002473// Return next O32 integer argument register.
2474static unsigned getNextIntArgReg(unsigned Reg) {
2475 assert((Reg == Mips::A0) || (Reg == Mips::A2));
2476 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
2477}
2478
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002479SDValue
2480MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002481 SDValue Chain, SDValue Arg, SDLoc DL,
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002482 bool IsTailCall, SelectionDAG &DAG) const {
2483 if (!IsTailCall) {
Mehdi Amini44ede332015-07-09 02:09:04 +00002484 SDValue PtrOff =
2485 DAG.getNode(ISD::ADD, DL, getPointerTy(DAG.getDataLayout()), StackPtr,
2486 DAG.getIntPtrConstant(Offset, DL));
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002487 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo(), false,
2488 false, 0);
2489 }
2490
2491 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2492 int FI = MFI->CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00002493 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002494 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(),
2495 /*isVolatile=*/ true, false, 0);
2496}
2497
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002498void MipsTargetLowering::
2499getOpndList(SmallVectorImpl<SDValue> &Ops,
2500 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
2501 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
Sasa Stankovic7072a792014-10-01 08:22:21 +00002502 bool IsCallReloc, CallLoweringInfo &CLI, SDValue Callee,
2503 SDValue Chain) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002504 // Insert node "GP copy globalreg" before call to function.
2505 //
2506 // R_MIPS_CALL* operators (emitted when non-internal functions are called
2507 // in PIC mode) allow symbols to be resolved via lazy binding.
2508 // The lazy binding stub requires GP to point to the GOT.
Sasa Stankovic7072a792014-10-01 08:22:21 +00002509 // Note that we don't need GP to point to the GOT for indirect calls
2510 // (when R_MIPS_CALL* is not used for the call) because Mips linker generates
2511 // lazy binding stub for a function only when R_MIPS_CALL* are the only relocs
2512 // used for the function (that is, Mips linker doesn't generate lazy binding
2513 // stub for a function whose address is taken in the program).
2514 if (IsPICCall && !InternalLinkage && IsCallReloc) {
Eric Christopher96e72c62015-01-29 23:27:36 +00002515 unsigned GPReg = ABI.IsN64() ? Mips::GP_64 : Mips::GP;
2516 EVT Ty = ABI.IsN64() ? MVT::i64 : MVT::i32;
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002517 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty)));
2518 }
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002519
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002520 // Build a sequence of copy-to-reg nodes chained together with token
2521 // chain and flag operands which copy the outgoing args into registers.
2522 // The InFlag in necessary since all emitted instructions must be
2523 // stuck together.
2524 SDValue InFlag;
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002525
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002526 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2527 Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, RegsToPass[i].first,
2528 RegsToPass[i].second, InFlag);
2529 InFlag = Chain.getValue(1);
2530 }
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002531
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002532 // Add argument registers to the end of the list so that they are
2533 // known live into the call.
2534 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2535 Ops.push_back(CLI.DAG.getRegister(RegsToPass[i].first,
2536 RegsToPass[i].second.getValueType()));
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002537
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002538 // Add a register mask operand representing the call-preserved registers.
Eric Christopher96e72c62015-01-29 23:27:36 +00002539 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00002540 const uint32_t *Mask =
2541 TRI->getCallPreservedMask(CLI.DAG.getMachineFunction(), CLI.CallConv);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002542 assert(Mask && "Missing call preserved mask for calling convention");
Eric Christopher1c29a652014-07-18 22:55:25 +00002543 if (Subtarget.inMips16HardFloat()) {
Reed Kotler783c7942013-05-10 22:25:39 +00002544 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
2545 llvm::StringRef Sym = G->getGlobal()->getName();
2546 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
Reed Kotler3230e722013-12-12 02:41:11 +00002547 if (F && F->hasFnAttribute("__Mips16RetHelper")) {
Reed Kotler783c7942013-05-10 22:25:39 +00002548 Mask = MipsRegisterInfo::getMips16RetHelperMask();
2549 }
2550 }
2551 }
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002552 Ops.push_back(CLI.DAG.getRegisterMask(Mask));
2553
2554 if (InFlag.getNode())
2555 Ops.push_back(InFlag);
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002556}
2557
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002558/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman624801e2009-01-26 03:15:54 +00002559/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002560SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00002561MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002562 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00002563 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002564 SDLoc DL = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00002565 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
2566 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
2567 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Akira Hatanakabeda2242012-07-31 18:46:41 +00002568 SDValue Chain = CLI.Chain;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002569 SDValue Callee = CLI.Callee;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002570 bool &IsTailCall = CLI.IsTailCall;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002571 CallingConv::ID CallConv = CLI.CallConv;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002572 bool IsVarArg = CLI.IsVarArg;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002573
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002574 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002575 MachineFrameInfo *MFI = MF.getFrameInfo();
Eric Christopher96e72c62015-01-29 23:27:36 +00002576 const TargetFrameLowering *TFL = Subtarget.getFrameLowering();
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002577 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +00002578 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002579
2580 // Analyze operands of the call, assigning locations to each operand.
2581 SmallVector<CCValAssign, 16> ArgLocs;
Daniel Sanders41a64c42014-11-07 11:10:48 +00002582 MipsCCState CCInfo(
2583 CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs, *DAG.getContext(),
2584 MipsCCState::getSpecialCallingConvForCallee(Callee.getNode(), Subtarget));
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002585
2586 // Allocate the reserved argument area. It seems strange to do this from the
2587 // caller side but removing it breaks the frame size calculation.
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002588 CCInfo.AllocateStack(ABI.GetCalleeAllocdArgSizeInBytes(CallConv), 1);
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002589
Daniel Sanderscfad1e32014-11-07 11:43:49 +00002590 CCInfo.AnalyzeCallOperands(Outs, CC_Mips, CLI.getArgs(), Callee.getNode());
Wesley Peck527da1b2010-11-23 03:31:01 +00002591
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002592 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka195a1e22011-06-08 17:39:33 +00002593 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka97ba7692012-07-26 23:27:01 +00002594
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002595 // Check if it's really possible to do a tail call.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002596 if (IsTailCall)
Daniel Sanders23e98772014-11-02 16:09:29 +00002597 IsTailCall = isEligibleForTailCallOptimization(
2598 CCInfo, NextStackOffset, *MF.getInfo<MipsFunctionInfo>());
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002599
Reid Kleckner5772b772014-04-24 20:14:34 +00002600 if (!IsTailCall && CLI.CS && CLI.CS->isMustTailCall())
2601 report_fatal_error("failed to perform tail call elimination on a call "
2602 "site marked musttail");
2603
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002604 if (IsTailCall)
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002605 ++NumTailCalls;
2606
Akira Hatanaka79738332011-09-19 20:26:02 +00002607 // Chain is the output chain of the last Load/Store or CopyToReg node.
2608 // ByValChain is the output chain of the last Memcpy node created for copying
2609 // byval arguments to the stack.
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002610 unsigned StackAlignment = TFL->getStackAlignment();
2611 NextStackOffset = RoundUpToAlignment(NextStackOffset, StackAlignment);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002612 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, DL, true);
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002613
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002614 if (!IsTailCall)
Andrew Trickad6d08a2013-05-29 22:03:55 +00002615 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffsetVal, DL);
Akira Hatanakabeda2242012-07-31 18:46:41 +00002616
Mehdi Amini44ede332015-07-09 02:09:04 +00002617 SDValue StackPtr =
2618 DAG.getCopyFromReg(Chain, DL, ABI.IsN64() ? Mips::SP_64 : Mips::SP,
2619 getPointerTy(DAG.getDataLayout()));
Akira Hatanaka195a1e22011-06-08 17:39:33 +00002620
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002621 // With EABI is it possible to have 16 args on registers.
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002622 std::deque< std::pair<unsigned, SDValue> > RegsToPass;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002623 SmallVector<SDValue, 8> MemOpChains;
Daniel Sanders23e98772014-11-02 16:09:29 +00002624
2625 CCInfo.rewindByValRegsInfo();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002626
2627 // Walk the register/memloc assignments, inserting copies/loads.
2628 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002629 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002630 CCValAssign &VA = ArgLocs[i];
Akira Hatanakab20a3252011-10-28 19:49:00 +00002631 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka19891f82011-11-12 02:34:50 +00002632 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002633 bool UseUpperBits = false;
Akira Hatanaka19891f82011-11-12 02:34:50 +00002634
2635 // ByVal Arg.
2636 if (Flags.isByVal()) {
Daniel Sanders23e98772014-11-02 16:09:29 +00002637 unsigned FirstByValReg, LastByValReg;
2638 unsigned ByValIdx = CCInfo.getInRegsParamsProcessed();
2639 CCInfo.getInRegsParamInfo(ByValIdx, FirstByValReg, LastByValReg);
2640
Akira Hatanaka19891f82011-11-12 02:34:50 +00002641 assert(Flags.getByValSize() &&
2642 "ByVal args of size 0 should have been ignored by front-end.");
Daniel Sanders23e98772014-11-02 16:09:29 +00002643 assert(ByValIdx < CCInfo.getInRegsParamsCount());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002644 assert(!IsTailCall &&
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002645 "Do not tail-call optimize if there is a byval argument.");
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002646 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002647 FirstByValReg, LastByValReg, Flags, Subtarget.isLittle(),
2648 VA);
Daniel Sanders23e98772014-11-02 16:09:29 +00002649 CCInfo.nextInRegsParam();
Akira Hatanaka19891f82011-11-12 02:34:50 +00002650 continue;
2651 }
Jia Liuf54f60f2012-02-28 07:46:26 +00002652
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002653 // Promote the value if needed.
2654 switch (VA.getLocInfo()) {
Daniel Sandersc43cda82014-11-07 16:54:21 +00002655 default:
2656 llvm_unreachable("Unknown loc info!");
Wesley Peck527da1b2010-11-23 03:31:01 +00002657 case CCValAssign::Full:
Akira Hatanakab20a3252011-10-28 19:49:00 +00002658 if (VA.isRegLoc()) {
2659 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
Akira Hatanaka3b7391d2013-03-05 22:20:28 +00002660 (ValVT == MVT::f64 && LocVT == MVT::i64) ||
2661 (ValVT == MVT::i64 && LocVT == MVT::f64))
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002662 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
Akira Hatanakab20a3252011-10-28 19:49:00 +00002663 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002664 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002665 Arg, DAG.getConstant(0, DL, MVT::i32));
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002666 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002667 Arg, DAG.getConstant(1, DL, MVT::i32));
Eric Christopher1c29a652014-07-18 22:55:25 +00002668 if (!Subtarget.isLittle())
Akira Hatanaka27916972011-04-15 19:52:08 +00002669 std::swap(Lo, Hi);
Jia Liuf54f60f2012-02-28 07:46:26 +00002670 unsigned LocRegLo = VA.getLocReg();
Akira Hatanaka61bbcce2011-09-23 00:58:33 +00002671 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2672 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2673 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002674 continue;
Wesley Peck527da1b2010-11-23 03:31:01 +00002675 }
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002676 }
2677 break;
Daniel Sanders23e98772014-11-02 16:09:29 +00002678 case CCValAssign::BCvt:
2679 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
2680 break;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002681 case CCValAssign::SExtUpper:
2682 UseUpperBits = true;
2683 // Fallthrough
Chris Lattner52f16de2008-03-17 06:57:02 +00002684 case CCValAssign::SExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002685 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002686 break;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002687 case CCValAssign::ZExtUpper:
2688 UseUpperBits = true;
2689 // Fallthrough
Chris Lattner52f16de2008-03-17 06:57:02 +00002690 case CCValAssign::ZExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002691 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002692 break;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002693 case CCValAssign::AExtUpper:
2694 UseUpperBits = true;
2695 // Fallthrough
Chris Lattner52f16de2008-03-17 06:57:02 +00002696 case CCValAssign::AExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002697 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002698 break;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002699 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002700
Daniel Sandersc43cda82014-11-07 16:54:21 +00002701 if (UseUpperBits) {
2702 unsigned ValSizeInBits = Outs[i].ArgVT.getSizeInBits();
2703 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
2704 Arg = DAG.getNode(
2705 ISD::SHL, DL, VA.getLocVT(), Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002706 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
Daniel Sandersc43cda82014-11-07 16:54:21 +00002707 }
2708
Wesley Peck527da1b2010-11-23 03:31:01 +00002709 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +00002710 // RegsToPass vector
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002711 if (VA.isRegLoc()) {
2712 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattner52f16de2008-03-17 06:57:02 +00002713 continue;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002714 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002715
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002716 // Register can't get to this point...
Chris Lattner52f16de2008-03-17 06:57:02 +00002717 assert(VA.isMemLoc());
Wesley Peck527da1b2010-11-23 03:31:01 +00002718
Wesley Peck527da1b2010-11-23 03:31:01 +00002719 // emit ISD::STORE whichs stores the
Chris Lattner52f16de2008-03-17 06:57:02 +00002720 // parameter value to a stack Location
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002721 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002722 Chain, Arg, DL, IsTailCall, DAG));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002723 }
2724
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002725 // Transform all store nodes into one single node because all store
2726 // nodes are independent of each other.
Wesley Peck527da1b2010-11-23 03:31:01 +00002727 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00002728 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002729
Bill Wendling24c79f22008-09-16 21:48:12 +00002730 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peck527da1b2010-11-23 03:31:01 +00002731 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2732 // node so that legalize doesn't hack it.
Eric Christopher96e72c62015-01-29 23:27:36 +00002733 bool IsPICCall = (ABI.IsN64() || IsPIC); // true if calls are translated to
2734 // jalr $25
Sasa Stankovic7072a792014-10-01 08:22:21 +00002735 bool GlobalOrExternal = false, InternalLinkage = false, IsCallReloc = false;
Akira Hatanakad6f1c582011-04-07 19:51:44 +00002736 SDValue CalleeLo;
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002737 EVT Ty = Callee.getValueType();
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002738
2739 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002740 if (IsPICCall) {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002741 const GlobalValue *Val = G->getGlobal();
2742 InternalLinkage = Val->hasInternalLinkage();
Akira Hatanakacf9a61b2012-12-13 03:17:29 +00002743
2744 if (InternalLinkage)
Eric Christopher96e72c62015-01-29 23:27:36 +00002745 Callee = getAddrLocal(G, DL, Ty, DAG, ABI.IsN32() || ABI.IsN64());
Sasa Stankovic7072a792014-10-01 08:22:21 +00002746 else if (LargeGOT) {
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002747 Callee = getAddrGlobalLargeGOT(G, DL, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002748 MipsII::MO_CALL_LO16, Chain,
2749 FuncInfo->callPtrInfo(Val));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002750 IsCallReloc = true;
2751 } else {
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002752 Callee = getAddrGlobal(G, DL, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002753 FuncInfo->callPtrInfo(Val));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002754 IsCallReloc = true;
2755 }
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002756 } else
Mehdi Amini44ede332015-07-09 02:09:04 +00002757 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL,
2758 getPointerTy(DAG.getDataLayout()), 0,
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002759 MipsII::MO_NO_FLAG);
Akira Hatanaka8e16aac2011-12-09 01:45:12 +00002760 GlobalOrExternal = true;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002761 }
2762 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002763 const char *Sym = S->getSymbol();
2764
Eric Christopher96e72c62015-01-29 23:27:36 +00002765 if (!ABI.IsN64() && !IsPIC) // !N64 && static
Mehdi Amini44ede332015-07-09 02:09:04 +00002766 Callee = DAG.getTargetExternalSymbol(
2767 Sym, getPointerTy(DAG.getDataLayout()), MipsII::MO_NO_FLAG);
Sasa Stankovic7072a792014-10-01 08:22:21 +00002768 else if (LargeGOT) {
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002769 Callee = getAddrGlobalLargeGOT(S, DL, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002770 MipsII::MO_CALL_LO16, Chain,
2771 FuncInfo->callPtrInfo(Sym));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002772 IsCallReloc = true;
2773 } else { // N64 || PIC
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002774 Callee = getAddrGlobal(S, DL, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002775 FuncInfo->callPtrInfo(Sym));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002776 IsCallReloc = true;
2777 }
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002778
Akira Hatanaka8e16aac2011-12-09 01:45:12 +00002779 GlobalOrExternal = true;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002780 }
2781
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002782 SmallVector<SDValue, 8> Ops(1, Chain);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002783 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002784
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002785 getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal, InternalLinkage,
Sasa Stankovic7072a792014-10-01 08:22:21 +00002786 IsCallReloc, CLI, Callee, Chain);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002787
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002788 if (IsTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +00002789 return DAG.getNode(MipsISD::TailCall, DL, MVT::Other, Ops);
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002790
Craig Topper48d114b2014-04-26 18:35:24 +00002791 Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, Ops);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002792 SDValue InFlag = Chain.getValue(1);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002793
Bruno Cardoso Lopes193e64c2010-01-30 18:32:07 +00002794 // Create the CALLSEQ_END node.
Akira Hatanaka97ba7692012-07-26 23:27:01 +00002795 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002796 DAG.getIntPtrConstant(0, DL, true), InFlag, DL);
Bruno Cardoso Lopes193e64c2010-01-30 18:32:07 +00002797 InFlag = Chain.getValue(1);
2798
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002799 // Handle result values, copying them out of physregs into vregs that we
2800 // return.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002801 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg, Ins, DL, DAG,
2802 InVals, CLI);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002803}
2804
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002805/// LowerCallResult - Lower the result values of a call into the
2806/// appropriate copies out of appropriate physical registers.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002807SDValue MipsTargetLowering::LowerCallResult(
2808 SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool IsVarArg,
2809 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG,
2810 SmallVectorImpl<SDValue> &InVals,
2811 TargetLowering::CallLoweringInfo &CLI) const {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002812 // Assign locations to each value returned by this call.
2813 SmallVector<CCValAssign, 16> RVLocs;
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002814 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
2815 *DAG.getContext());
2816 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips, CLI);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002817
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002818 // Copy all of the result registers out of their specified physreg.
2819 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Daniel Sandersae275e32014-09-25 12:15:05 +00002820 CCValAssign &VA = RVLocs[i];
2821 assert(VA.isRegLoc() && "Can only return in registers!");
2822
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002823 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(),
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002824 RVLocs[i].getLocVT(), InFlag);
2825 Chain = Val.getValue(1);
2826 InFlag = Val.getValue(2);
2827
Daniel Sandersae275e32014-09-25 12:15:05 +00002828 if (VA.isUpperBitsInLoc()) {
2829 unsigned ValSizeInBits = Ins[i].ArgVT.getSizeInBits();
2830 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
2831 unsigned Shift =
2832 VA.getLocInfo() == CCValAssign::ZExtUpper ? ISD::SRL : ISD::SRA;
2833 Val = DAG.getNode(
2834 Shift, DL, VA.getLocVT(), Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002835 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
Daniel Sandersae275e32014-09-25 12:15:05 +00002836 }
2837
2838 switch (VA.getLocInfo()) {
2839 default:
2840 llvm_unreachable("Unknown loc info!");
2841 case CCValAssign::Full:
2842 break;
2843 case CCValAssign::BCvt:
2844 Val = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Val);
2845 break;
2846 case CCValAssign::AExt:
2847 case CCValAssign::AExtUpper:
2848 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2849 break;
2850 case CCValAssign::ZExt:
2851 case CCValAssign::ZExtUpper:
2852 Val = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Val,
2853 DAG.getValueType(VA.getValVT()));
2854 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2855 break;
2856 case CCValAssign::SExt:
2857 case CCValAssign::SExtUpper:
2858 Val = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Val,
2859 DAG.getValueType(VA.getValVT()));
2860 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2861 break;
2862 }
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002863
2864 InVals.push_back(Val);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002865 }
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +00002866
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002867 return Chain;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002868}
2869
Daniel Sandersc43cda82014-11-07 16:54:21 +00002870static SDValue UnpackFromArgumentSlot(SDValue Val, const CCValAssign &VA,
2871 EVT ArgVT, SDLoc DL, SelectionDAG &DAG) {
2872 MVT LocVT = VA.getLocVT();
2873 EVT ValVT = VA.getValVT();
2874
2875 // Shift into the upper bits if necessary.
2876 switch (VA.getLocInfo()) {
2877 default:
2878 break;
2879 case CCValAssign::AExtUpper:
2880 case CCValAssign::SExtUpper:
2881 case CCValAssign::ZExtUpper: {
2882 unsigned ValSizeInBits = ArgVT.getSizeInBits();
2883 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
2884 unsigned Opcode =
2885 VA.getLocInfo() == CCValAssign::ZExtUpper ? ISD::SRL : ISD::SRA;
2886 Val = DAG.getNode(
2887 Opcode, DL, VA.getLocVT(), Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002888 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
Daniel Sandersc43cda82014-11-07 16:54:21 +00002889 break;
2890 }
2891 }
2892
2893 // If this is an value smaller than the argument slot size (32-bit for O32,
2894 // 64-bit for N32/N64), it has been promoted in some way to the argument slot
2895 // size. Extract the value and insert any appropriate assertions regarding
2896 // sign/zero extension.
2897 switch (VA.getLocInfo()) {
2898 default:
2899 llvm_unreachable("Unknown loc info!");
2900 case CCValAssign::Full:
2901 break;
2902 case CCValAssign::AExtUpper:
2903 case CCValAssign::AExt:
2904 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
2905 break;
2906 case CCValAssign::SExtUpper:
2907 case CCValAssign::SExt:
2908 Val = DAG.getNode(ISD::AssertSext, DL, LocVT, Val, DAG.getValueType(ValVT));
2909 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
2910 break;
2911 case CCValAssign::ZExtUpper:
2912 case CCValAssign::ZExt:
2913 Val = DAG.getNode(ISD::AssertZext, DL, LocVT, Val, DAG.getValueType(ValVT));
2914 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
2915 break;
2916 case CCValAssign::BCvt:
2917 Val = DAG.getNode(ISD::BITCAST, DL, ValVT, Val);
2918 break;
2919 }
2920
2921 return Val;
2922}
2923
Akira Hatanakae2489122011-04-15 21:51:11 +00002924//===----------------------------------------------------------------------===//
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002925// Formal Arguments Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002926//===----------------------------------------------------------------------===//
Wesley Peck527da1b2010-11-23 03:31:01 +00002927/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002928/// and generate load operations for arguments places on the stack.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002929SDValue
2930MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanakaaef55c82011-04-15 21:00:26 +00002931 CallingConv::ID CallConv,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002932 bool IsVarArg,
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00002933 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002934 SDLoc DL, SelectionDAG &DAG,
Akira Hatanakaaef55c82011-04-15 21:00:26 +00002935 SmallVectorImpl<SDValue> &InVals)
Akira Hatanakae2489122011-04-15 21:51:11 +00002936 const {
Bruno Cardoso Lopesa01ede22008-08-04 07:12:52 +00002937 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002938 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopes14033fb2007-08-28 05:08:16 +00002939 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002940
Dan Gohman31ae5862010-04-17 14:41:14 +00002941 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002942
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002943 // Used with vargs to acumulate store chains.
2944 std::vector<SDValue> OutChains;
2945
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002946 // Assign locations to all of the incoming arguments.
2947 SmallVector<CCValAssign, 16> ArgLocs;
Daniel Sanders23e98772014-11-02 16:09:29 +00002948 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
2949 *DAG.getContext());
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002950 CCInfo.AllocateStack(ABI.GetCalleeAllocdArgSizeInBytes(CallConv), 1);
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002951 Function::const_arg_iterator FuncArg =
2952 DAG.getMachineFunction().getFunction()->arg_begin();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002953
Daniel Sandersb70e27c2014-11-06 16:36:30 +00002954 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips_FixedArg);
Akira Hatanaka4866fe12012-10-30 19:37:25 +00002955 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(),
Daniel Sanders23e98772014-11-02 16:09:29 +00002956 CCInfo.getInRegsParamsCount() > 0);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002957
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00002958 unsigned CurArgIdx = 0;
Daniel Sanders23e98772014-11-02 16:09:29 +00002959 CCInfo.rewindByValRegsInfo();
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002960
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00002961 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002962 CCValAssign &VA = ArgLocs[i];
Andrew Trick05938a52015-02-16 18:10:47 +00002963 if (Ins[i].isOrigArg()) {
2964 std::advance(FuncArg, Ins[i].getOrigArgIndex() - CurArgIdx);
2965 CurArgIdx = Ins[i].getOrigArgIndex();
2966 }
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002967 EVT ValVT = VA.getValVT();
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002968 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2969 bool IsRegLoc = VA.isRegLoc();
2970
2971 if (Flags.isByVal()) {
Andrew Trick05938a52015-02-16 18:10:47 +00002972 assert(Ins[i].isOrigArg() && "Byval arguments cannot be implicit");
Daniel Sanders23e98772014-11-02 16:09:29 +00002973 unsigned FirstByValReg, LastByValReg;
2974 unsigned ByValIdx = CCInfo.getInRegsParamsProcessed();
2975 CCInfo.getInRegsParamInfo(ByValIdx, FirstByValReg, LastByValReg);
2976
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002977 assert(Flags.getByValSize() &&
2978 "ByVal args of size 0 should have been ignored by front-end.");
Daniel Sanders23e98772014-11-02 16:09:29 +00002979 assert(ByValIdx < CCInfo.getInRegsParamsCount());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002980 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002981 FirstByValReg, LastByValReg, VA, CCInfo);
Daniel Sanders23e98772014-11-02 16:09:29 +00002982 CCInfo.nextInRegsParam();
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002983 continue;
2984 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002985
2986 // Arguments stored on registers
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002987 if (IsRegLoc) {
Akira Hatanaka7d822522013-10-28 21:21:36 +00002988 MVT RegVT = VA.getLocVT();
Akira Hatanakacb4a1a82011-05-24 00:23:52 +00002989 unsigned ArgReg = VA.getLocReg();
Akira Hatanaka7d822522013-10-28 21:21:36 +00002990 const TargetRegisterClass *RC = getRegClassFor(RegVT);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002991
Wesley Peck527da1b2010-11-23 03:31:01 +00002992 // Transform the arguments stored on
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002993 // physical registers into virtual ones
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002994 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC);
2995 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
Wesley Peck527da1b2010-11-23 03:31:01 +00002996
Daniel Sandersc43cda82014-11-07 16:54:21 +00002997 ArgValue = UnpackFromArgumentSlot(ArgValue, VA, Ins[i].ArgVT, DL, DAG);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002998
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002999 // Handle floating point arguments passed in integer registers and
3000 // long double arguments passed in floating point registers.
Akira Hatanaka104b7e32011-10-28 19:55:48 +00003001 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003002 (RegVT == MVT::i64 && ValVT == MVT::f64) ||
3003 (RegVT == MVT::f64 && ValVT == MVT::i64))
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003004 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
Eric Christopher96e72c62015-01-29 23:27:36 +00003005 else if (ABI.IsO32() && RegVT == MVT::i32 &&
Eric Christopherbf33a3c2014-07-02 23:18:40 +00003006 ValVT == MVT::f64) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003007 unsigned Reg2 = addLiveIn(DAG.getMachineFunction(),
Akira Hatanaka104b7e32011-10-28 19:55:48 +00003008 getNextIntArgReg(ArgReg), RC);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003009 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT);
Eric Christopher1c29a652014-07-18 22:55:25 +00003010 if (!Subtarget.isLittle())
Akira Hatanaka104b7e32011-10-28 19:55:48 +00003011 std::swap(ArgValue, ArgValue2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003012 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64,
Akira Hatanaka104b7e32011-10-28 19:55:48 +00003013 ArgValue, ArgValue2);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00003014 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003015
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003016 InVals.push_back(ArgValue);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003017 } else { // VA.isRegLoc()
Daniel Sandersc43cda82014-11-07 16:54:21 +00003018 MVT LocVT = VA.getLocVT();
3019
Eric Christopher96e72c62015-01-29 23:27:36 +00003020 if (ABI.IsO32()) {
Daniel Sandersc43cda82014-11-07 16:54:21 +00003021 // We ought to be able to use LocVT directly but O32 sets it to i32
3022 // when allocating floating point values to integer registers.
3023 // This shouldn't influence how we load the value into registers unless
3024 // we are targetting softfloat.
Eric Christophere8ae3e32015-05-07 23:10:21 +00003025 if (VA.getValVT().isFloatingPoint() && !Subtarget.useSoftFloat())
Daniel Sandersc43cda82014-11-07 16:54:21 +00003026 LocVT = VA.getValVT();
3027 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003028
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003029 // sanity check
3030 assert(VA.isMemLoc());
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003031
Wesley Peck527da1b2010-11-23 03:31:01 +00003032 // The stack pointer offset is relative to the caller stack frame.
Daniel Sandersc43cda82014-11-07 16:54:21 +00003033 int FI = MFI->CreateFixedObject(LocVT.getSizeInBits() / 8,
Akira Hatanakacb4a1a82011-05-24 00:23:52 +00003034 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003035
3036 // Create load nodes to retrieve arguments from the stack
Mehdi Amini44ede332015-07-09 02:09:04 +00003037 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Daniel Sandersc43cda82014-11-07 16:54:21 +00003038 SDValue ArgValue = DAG.getLoad(LocVT, DL, Chain, FIN,
3039 MachinePointerInfo::getFixedStack(FI),
3040 false, false, false, 0);
3041 OutChains.push_back(ArgValue.getValue(1));
3042
3043 ArgValue = UnpackFromArgumentSlot(ArgValue, VA, Ins[i].ArgVT, DL, DAG);
3044
3045 InVals.push_back(ArgValue);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003046 }
Reid Kleckner7a59e082014-05-12 22:01:27 +00003047 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003048
Reid Kleckner7a59e082014-05-12 22:01:27 +00003049 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Reid Kleckner79418562014-05-09 22:32:13 +00003050 // The mips ABIs for returning structs by value requires that we copy
3051 // the sret argument into $v0 for the return. Save the argument into
3052 // a virtual register so that we can access it from the return points.
Reid Kleckner7a59e082014-05-12 22:01:27 +00003053 if (Ins[i].Flags.isSRet()) {
Reid Kleckner79418562014-05-09 22:32:13 +00003054 unsigned Reg = MipsFI->getSRetReturnReg();
3055 if (!Reg) {
3056 Reg = MF.getRegInfo().createVirtualRegister(
Eric Christopher96e72c62015-01-29 23:27:36 +00003057 getRegClassFor(ABI.IsN64() ? MVT::i64 : MVT::i32));
Reid Kleckner79418562014-05-09 22:32:13 +00003058 MipsFI->setSRetReturnReg(Reg);
3059 }
Reid Kleckner7a59e082014-05-12 22:01:27 +00003060 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[i]);
Reid Kleckner79418562014-05-09 22:32:13 +00003061 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
Reid Kleckner7a59e082014-05-12 22:01:27 +00003062 break;
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003063 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003064 }
3065
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003066 if (IsVarArg)
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003067 writeVarArgRegs(OutChains, Chain, DL, DAG, CCInfo);
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003068
Wesley Peck527da1b2010-11-23 03:31:01 +00003069 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003070 // the size of Ins and InVals. This only happens when on varg functions
3071 if (!OutChains.empty()) {
3072 OutChains.push_back(Chain);
Craig Topper48d114b2014-04-26 18:35:24 +00003073 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003074 }
3075
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003076 return Chain;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003077}
3078
Akira Hatanakae2489122011-04-15 21:51:11 +00003079//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003080// Return Value Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00003081//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003082
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003083bool
3084MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003085 MachineFunction &MF, bool IsVarArg,
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003086 const SmallVectorImpl<ISD::OutputArg> &Outs,
3087 LLVMContext &Context) const {
3088 SmallVector<CCValAssign, 16> RVLocs;
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003089 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003090 return CCInfo.CheckReturn(Outs, RetCC_Mips);
3091}
3092
Petar Jovanovic5b436222015-03-23 12:28:13 +00003093bool
3094MipsTargetLowering::shouldSignExtendTypeInLibCall(EVT Type, bool IsSigned) const {
Eric Christophere8ae3e32015-05-07 23:10:21 +00003095 if (Subtarget.hasMips3() && Subtarget.useSoftFloat()) {
Petar Jovanovic5b436222015-03-23 12:28:13 +00003096 if (Type == MVT::i32)
3097 return true;
3098 }
3099 return IsSigned;
3100}
3101
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003102SDValue
3103MipsTargetLowering::LowerReturn(SDValue Chain,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003104 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003105 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003106 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003107 SDLoc DL, SelectionDAG &DAG) const {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003108 // CCValAssign - represent the assignment of
3109 // the return value to a location
3110 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003111 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003112
3113 // CCState - Info about the registers and stack slot.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003114 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, *DAG.getContext());
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003115
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003116 // Analyze return values.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003117 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003118
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003119 SDValue Flag;
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003120 SmallVector<SDValue, 4> RetOps(1, Chain);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003121
3122 // Copy the result values into the output registers.
3123 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003124 SDValue Val = OutVals[i];
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003125 CCValAssign &VA = RVLocs[i];
3126 assert(VA.isRegLoc() && "Can only return in registers!");
Daniel Sandersae275e32014-09-25 12:15:05 +00003127 bool UseUpperBits = false;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003128
Daniel Sandersae275e32014-09-25 12:15:05 +00003129 switch (VA.getLocInfo()) {
3130 default:
3131 llvm_unreachable("Unknown loc info!");
3132 case CCValAssign::Full:
3133 break;
3134 case CCValAssign::BCvt:
3135 Val = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Val);
3136 break;
3137 case CCValAssign::AExtUpper:
3138 UseUpperBits = true;
3139 // Fallthrough
3140 case CCValAssign::AExt:
3141 Val = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Val);
3142 break;
3143 case CCValAssign::ZExtUpper:
3144 UseUpperBits = true;
3145 // Fallthrough
3146 case CCValAssign::ZExt:
3147 Val = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Val);
3148 break;
3149 case CCValAssign::SExtUpper:
3150 UseUpperBits = true;
3151 // Fallthrough
3152 case CCValAssign::SExt:
3153 Val = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Val);
3154 break;
3155 }
3156
3157 if (UseUpperBits) {
3158 unsigned ValSizeInBits = Outs[i].ArgVT.getSizeInBits();
3159 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
3160 Val = DAG.getNode(
3161 ISD::SHL, DL, VA.getLocVT(), Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003162 DAG.getConstant(LocSizeInBits - ValSizeInBits, DL, VA.getLocVT()));
Daniel Sandersae275e32014-09-25 12:15:05 +00003163 }
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003164
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003165 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003166
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003167 // Guarantee that all emitted copies are stuck together with flags.
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003168 Flag = Chain.getValue(1);
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003169 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003170 }
3171
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003172 // The mips ABIs for returning structs by value requires that we copy
3173 // the sret argument into $v0 for the return. We saved the argument into
3174 // a virtual register in the entry block, so now we copy the value out
3175 // and into $v0.
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003176 if (MF.getFunction()->hasStructRetAttr()) {
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003177 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3178 unsigned Reg = MipsFI->getSRetReturnReg();
3179
Wesley Peck527da1b2010-11-23 03:31:01 +00003180 if (!Reg)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003181 llvm_unreachable("sret virtual register not created in the entry block");
Mehdi Amini44ede332015-07-09 02:09:04 +00003182 SDValue Val =
3183 DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(DAG.getDataLayout()));
Eric Christopher96e72c62015-01-29 23:27:36 +00003184 unsigned V0 = ABI.IsN64() ? Mips::V0_64 : Mips::V0;
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003185
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003186 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003187 Flag = Chain.getValue(1);
Mehdi Amini44ede332015-07-09 02:09:04 +00003188 RetOps.push_back(DAG.getRegister(V0, getPointerTy(DAG.getDataLayout())));
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003189 }
3190
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003191 RetOps[0] = Chain; // Update chain.
Akira Hatanakaefff7b72012-07-10 00:19:06 +00003192
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003193 // Add the flag if we have it.
3194 if (Flag.getNode())
3195 RetOps.push_back(Flag);
3196
3197 // Return on Mips is always a "jr $ra"
Craig Topper48d114b2014-04-26 18:35:24 +00003198 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, RetOps);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003199}
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003200
Akira Hatanakae2489122011-04-15 21:51:11 +00003201//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003202// Mips Inline Assembly Support
Akira Hatanakae2489122011-04-15 21:51:11 +00003203//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003204
3205/// getConstraintType - Given a constraint letter, return the type of
3206/// constraint it is for this target.
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003207MipsTargetLowering::ConstraintType
3208MipsTargetLowering::getConstraintType(StringRef Constraint) const {
Daniel Sanders8b59af12013-11-12 12:56:01 +00003209 // Mips specific constraints
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003210 // GCC config/mips/constraints.md
3211 //
Wesley Peck527da1b2010-11-23 03:31:01 +00003212 // 'd' : An address register. Equivalent to r
3213 // unless generating MIPS16 code.
3214 // 'y' : Equivalent to r; retained for
3215 // backwards compatibility.
Eric Christophere3c494d2012-05-07 06:25:10 +00003216 // 'c' : A register suitable for use in an indirect
3217 // jump. This will always be $25 for -mabicalls.
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003218 // 'l' : The lo register. 1 word storage.
3219 // 'x' : The hilo register pair. Double word storage.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003220 if (Constraint.size() == 1) {
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003221 switch (Constraint[0]) {
3222 default : break;
Wesley Peck527da1b2010-11-23 03:31:01 +00003223 case 'd':
3224 case 'y':
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003225 case 'f':
Eric Christophere3c494d2012-05-07 06:25:10 +00003226 case 'c':
Eric Christopher9c492e62012-05-07 06:25:15 +00003227 case 'l':
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003228 case 'x':
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003229 return C_RegisterClass;
Jack Carter0e149b02013-03-04 21:33:15 +00003230 case 'R':
3231 return C_Memory;
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003232 }
3233 }
Daniel Sandersa73d8fe2015-03-24 11:26:34 +00003234
3235 if (Constraint == "ZC")
3236 return C_Memory;
3237
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003238 return TargetLowering::getConstraintType(Constraint);
3239}
3240
John Thompsone8360b72010-10-29 17:29:13 +00003241/// Examine constraint type and operand type and determine a weight value.
3242/// This object must already have been set up with the operand type
3243/// and the current alternative constraint selected.
3244TargetLowering::ConstraintWeight
3245MipsTargetLowering::getSingleConstraintMatchWeight(
3246 AsmOperandInfo &info, const char *constraint) const {
3247 ConstraintWeight weight = CW_Invalid;
3248 Value *CallOperandVal = info.CallOperandVal;
3249 // If we don't have a value, we can't do a match,
3250 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +00003251 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +00003252 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +00003253 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +00003254 // Look at the constraint type.
3255 switch (*constraint) {
3256 default:
3257 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
3258 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00003259 case 'd':
3260 case 'y':
John Thompsone8360b72010-10-29 17:29:13 +00003261 if (type->isIntegerTy())
3262 weight = CW_Register;
3263 break;
Daniel Sanders8b59af12013-11-12 12:56:01 +00003264 case 'f': // FPU or MSA register
Eric Christopher1c29a652014-07-18 22:55:25 +00003265 if (Subtarget.hasMSA() && type->isVectorTy() &&
Daniel Sanders8b59af12013-11-12 12:56:01 +00003266 cast<VectorType>(type)->getBitWidth() == 128)
3267 weight = CW_Register;
3268 else if (type->isFloatTy())
John Thompsone8360b72010-10-29 17:29:13 +00003269 weight = CW_Register;
3270 break;
Eric Christophere3c494d2012-05-07 06:25:10 +00003271 case 'c': // $25 for indirect jumps
Eric Christopher9c492e62012-05-07 06:25:15 +00003272 case 'l': // lo register
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003273 case 'x': // hilo register pair
Daniel Sanders8b59af12013-11-12 12:56:01 +00003274 if (type->isIntegerTy())
Eric Christophere3c494d2012-05-07 06:25:10 +00003275 weight = CW_SpecificReg;
Daniel Sanders8b59af12013-11-12 12:56:01 +00003276 break;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003277 case 'I': // signed 16 bit immediate
Eric Christopher7201e1b2012-05-07 03:13:42 +00003278 case 'J': // integer zero
Eric Christopher3ff88a02012-05-07 05:46:29 +00003279 case 'K': // unsigned 16 bit immediate
Eric Christopher1109b342012-05-07 05:46:37 +00003280 case 'L': // signed 32 bit immediate where lower 16 bits are 0
Eric Christophere07aa432012-05-07 05:46:43 +00003281 case 'N': // immediate in the range of -65535 to -1 (inclusive)
Eric Christopher470578a2012-05-07 05:46:48 +00003282 case 'O': // signed 15 bit immediate (+- 16383)
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003283 case 'P': // immediate in the range of 65535 to 1 (inclusive)
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003284 if (isa<ConstantInt>(CallOperandVal))
3285 weight = CW_Constant;
3286 break;
Jack Carter0e149b02013-03-04 21:33:15 +00003287 case 'R':
3288 weight = CW_Memory;
3289 break;
John Thompsone8360b72010-10-29 17:29:13 +00003290 }
3291 return weight;
3292}
3293
Akira Hatanaka7473b472013-08-14 00:21:25 +00003294/// This is a helper function to parse a physical register string and split it
3295/// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag
3296/// that is returned indicates whether parsing was successful. The second flag
3297/// is true if the numeric part exists.
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003298static std::pair<bool, bool> parsePhysicalReg(StringRef C, StringRef &Prefix,
3299 unsigned long long &Reg) {
Akira Hatanaka7473b472013-08-14 00:21:25 +00003300 if (C.front() != '{' || C.back() != '}')
3301 return std::make_pair(false, false);
3302
3303 // Search for the first numeric character.
3304 StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1;
3305 I = std::find_if(B, E, std::ptr_fun(isdigit));
3306
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003307 Prefix = StringRef(B, I - B);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003308
3309 // The second flag is set to false if no numeric characters were found.
3310 if (I == E)
3311 return std::make_pair(true, false);
3312
3313 // Parse the numeric characters.
3314 return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg),
3315 true);
3316}
3317
3318std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering::
Craig Topper6dc4a8bc2014-08-30 16:48:02 +00003319parseRegForInlineAsmConstraint(StringRef C, MVT VT) const {
Eric Christopherd9134482014-08-04 21:25:23 +00003320 const TargetRegisterInfo *TRI =
Eric Christopher96e72c62015-01-29 23:27:36 +00003321 Subtarget.getRegisterInfo();
Akira Hatanaka7473b472013-08-14 00:21:25 +00003322 const TargetRegisterClass *RC;
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003323 StringRef Prefix;
Akira Hatanaka7473b472013-08-14 00:21:25 +00003324 unsigned long long Reg;
3325
3326 std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg);
3327
3328 if (!R.first)
Craig Topper062a2ba2014-04-25 05:30:21 +00003329 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003330
3331 if ((Prefix == "hi" || Prefix == "lo")) { // Parse hi/lo.
3332 // No numeric characters follow "hi" or "lo".
3333 if (R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003334 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003335
3336 RC = TRI->getRegClass(Prefix == "hi" ?
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00003337 Mips::HI32RegClassID : Mips::LO32RegClassID);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003338 return std::make_pair(*(RC->begin()), RC);
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003339 } else if (Prefix.startswith("$msa")) {
Daniel Sanders8b59af12013-11-12 12:56:01 +00003340 // Parse $msa(ir|csr|access|save|modify|request|map|unmap)
3341
3342 // No numeric characters follow the name.
3343 if (R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003344 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003345
3346 Reg = StringSwitch<unsigned long long>(Prefix)
3347 .Case("$msair", Mips::MSAIR)
3348 .Case("$msacsr", Mips::MSACSR)
3349 .Case("$msaaccess", Mips::MSAAccess)
3350 .Case("$msasave", Mips::MSASave)
3351 .Case("$msamodify", Mips::MSAModify)
3352 .Case("$msarequest", Mips::MSARequest)
3353 .Case("$msamap", Mips::MSAMap)
3354 .Case("$msaunmap", Mips::MSAUnmap)
3355 .Default(0);
3356
3357 if (!Reg)
Craig Topper062a2ba2014-04-25 05:30:21 +00003358 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003359
3360 RC = TRI->getRegClass(Mips::MSACtrlRegClassID);
3361 return std::make_pair(Reg, RC);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003362 }
3363
3364 if (!R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003365 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003366
3367 if (Prefix == "$f") { // Parse $f0-$f31.
3368 // If the size of FP registers is 64-bit or Reg is an even number, select
3369 // the 64-bit register class. Otherwise, select the 32-bit register class.
3370 if (VT == MVT::Other)
Eric Christopher1c29a652014-07-18 22:55:25 +00003371 VT = (Subtarget.isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32;
Akira Hatanaka7473b472013-08-14 00:21:25 +00003372
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003373 RC = getRegClassFor(VT);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003374
3375 if (RC == &Mips::AFGR64RegClass) {
3376 assert(Reg % 2 == 0);
3377 Reg >>= 1;
3378 }
Daniel Sanders8b59af12013-11-12 12:56:01 +00003379 } else if (Prefix == "$fcc") // Parse $fcc0-$fcc7.
Akira Hatanaka7473b472013-08-14 00:21:25 +00003380 RC = TRI->getRegClass(Mips::FCCRegClassID);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003381 else if (Prefix == "$w") { // Parse $w0-$w31.
3382 RC = getRegClassFor((VT == MVT::Other) ? MVT::v16i8 : VT);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003383 } else { // Parse $0-$31.
3384 assert(Prefix == "$");
3385 RC = getRegClassFor((VT == MVT::Other) ? MVT::i32 : VT);
3386 }
3387
3388 assert(Reg < RC->getNumRegs());
3389 return std::make_pair(*(RC->begin() + Reg), RC);
3390}
3391
Eric Christophereaf77dc2011-06-29 19:33:04 +00003392/// Given a register class constraint, like 'r', if this corresponds directly
3393/// to an LLVM register class, return a register of 0 and the register class
3394/// pointer.
Eric Christopher11e4df72015-02-26 22:38:43 +00003395std::pair<unsigned, const TargetRegisterClass *>
3396MipsTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003397 StringRef Constraint,
Eric Christopher11e4df72015-02-26 22:38:43 +00003398 MVT VT) const {
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003399 if (Constraint.size() == 1) {
3400 switch (Constraint[0]) {
Eric Christopher9519c082011-06-29 19:04:31 +00003401 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
3402 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003403 case 'r':
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003404 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
Eric Christopher1c29a652014-07-18 22:55:25 +00003405 if (Subtarget.inMips16Mode())
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003406 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003407 return std::make_pair(0U, &Mips::GPR32RegClass);
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003408 }
Eric Christopher1c29a652014-07-18 22:55:25 +00003409 if (VT == MVT::i64 && !Subtarget.isGP64bit())
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003410 return std::make_pair(0U, &Mips::GPR32RegClass);
Eric Christopher1c29a652014-07-18 22:55:25 +00003411 if (VT == MVT::i64 && Subtarget.isGP64bit())
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003412 return std::make_pair(0U, &Mips::GPR64RegClass);
Eric Christopher58daf042012-05-07 03:13:22 +00003413 // This will generate an error message
Craig Topper062a2ba2014-04-25 05:30:21 +00003414 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003415 case 'f': // FPU or MSA register
3416 if (VT == MVT::v16i8)
3417 return std::make_pair(0U, &Mips::MSA128BRegClass);
3418 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
3419 return std::make_pair(0U, &Mips::MSA128HRegClass);
3420 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
3421 return std::make_pair(0U, &Mips::MSA128WRegClass);
3422 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
3423 return std::make_pair(0U, &Mips::MSA128DRegClass);
3424 else if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00003425 return std::make_pair(0U, &Mips::FGR32RegClass);
Eric Christopher1c29a652014-07-18 22:55:25 +00003426 else if ((VT == MVT::f64) && (!Subtarget.isSingleFloat())) {
3427 if (Subtarget.isFP64bit())
Craig Topperc7242e02012-04-20 07:30:17 +00003428 return std::make_pair(0U, &Mips::FGR64RegClass);
3429 return std::make_pair(0U, &Mips::AFGR64RegClass);
Akira Hatanakac669d7a2012-01-04 02:45:01 +00003430 }
Eric Christophere3c494d2012-05-07 06:25:10 +00003431 break;
3432 case 'c': // register suitable for indirect jump
3433 if (VT == MVT::i32)
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003434 return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass);
Eric Christophere3c494d2012-05-07 06:25:10 +00003435 assert(VT == MVT::i64 && "Unexpected type.");
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003436 return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass);
Eric Christopher9c492e62012-05-07 06:25:15 +00003437 case 'l': // register suitable for indirect jump
3438 if (VT == MVT::i32)
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00003439 return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass);
3440 return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass);
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003441 case 'x': // register suitable for indirect jump
3442 // Fixme: Not triggering the use of both hi and low
3443 // This will generate an error message
Craig Topper062a2ba2014-04-25 05:30:21 +00003444 return std::make_pair(0U, nullptr);
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003445 }
3446 }
Akira Hatanaka7473b472013-08-14 00:21:25 +00003447
3448 std::pair<unsigned, const TargetRegisterClass *> R;
3449 R = parseRegForInlineAsmConstraint(Constraint, VT);
3450
3451 if (R.second)
3452 return R;
3453
Eric Christopher11e4df72015-02-26 22:38:43 +00003454 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003455}
3456
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003457/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3458/// vector. If it is invalid, don't add anything to Ops.
3459void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3460 std::string &Constraint,
3461 std::vector<SDValue>&Ops,
3462 SelectionDAG &DAG) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003463 SDLoc DL(Op);
Craig Topper062a2ba2014-04-25 05:30:21 +00003464 SDValue Result;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003465
3466 // Only support length 1 constraints for now.
3467 if (Constraint.length() > 1) return;
3468
3469 char ConstraintLetter = Constraint[0];
3470 switch (ConstraintLetter) {
3471 default: break; // This will fall through to the generic implementation
3472 case 'I': // Signed 16 bit constant
3473 // If this fails, the parent routine will give an error
3474 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3475 EVT Type = Op.getValueType();
3476 int64_t Val = C->getSExtValue();
3477 if (isInt<16>(Val)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003478 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003479 break;
3480 }
3481 }
3482 return;
Eric Christopher7201e1b2012-05-07 03:13:42 +00003483 case 'J': // integer zero
3484 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3485 EVT Type = Op.getValueType();
3486 int64_t Val = C->getZExtValue();
3487 if (Val == 0) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003488 Result = DAG.getTargetConstant(0, DL, Type);
Eric Christopher7201e1b2012-05-07 03:13:42 +00003489 break;
3490 }
3491 }
3492 return;
Eric Christopher3ff88a02012-05-07 05:46:29 +00003493 case 'K': // unsigned 16 bit immediate
3494 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3495 EVT Type = Op.getValueType();
3496 uint64_t Val = (uint64_t)C->getZExtValue();
3497 if (isUInt<16>(Val)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003498 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopher3ff88a02012-05-07 05:46:29 +00003499 break;
3500 }
3501 }
3502 return;
Eric Christopher1109b342012-05-07 05:46:37 +00003503 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3504 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3505 EVT Type = Op.getValueType();
3506 int64_t Val = C->getSExtValue();
3507 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003508 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopher1109b342012-05-07 05:46:37 +00003509 break;
3510 }
3511 }
3512 return;
Eric Christophere07aa432012-05-07 05:46:43 +00003513 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3514 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3515 EVT Type = Op.getValueType();
3516 int64_t Val = C->getSExtValue();
3517 if ((Val >= -65535) && (Val <= -1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003518 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christophere07aa432012-05-07 05:46:43 +00003519 break;
3520 }
3521 }
3522 return;
Eric Christopher470578a2012-05-07 05:46:48 +00003523 case 'O': // signed 15 bit immediate
3524 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3525 EVT Type = Op.getValueType();
3526 int64_t Val = C->getSExtValue();
3527 if ((isInt<15>(Val))) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003528 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopher470578a2012-05-07 05:46:48 +00003529 break;
3530 }
3531 }
3532 return;
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003533 case 'P': // immediate in the range of 1 to 65535 (inclusive)
3534 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3535 EVT Type = Op.getValueType();
3536 int64_t Val = C->getSExtValue();
3537 if ((Val <= 65535) && (Val >= 1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003538 Result = DAG.getTargetConstant(Val, DL, Type);
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003539 break;
3540 }
3541 }
3542 return;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003543 }
3544
3545 if (Result.getNode()) {
3546 Ops.push_back(Result);
3547 return;
3548 }
3549
3550 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3551}
3552
Mehdi Amini0cdec1e2015-07-09 02:09:40 +00003553bool MipsTargetLowering::isLegalAddressingMode(const DataLayout &DL,
3554 const AddrMode &AM, Type *Ty,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +00003555 unsigned AS) const {
Akira Hatanakaef839192012-11-17 00:25:41 +00003556 // No global is ever allowed as a base.
3557 if (AM.BaseGV)
3558 return false;
3559
3560 switch (AM.Scale) {
3561 case 0: // "r+i" or just "i", depending on HasBaseReg.
3562 break;
3563 case 1:
3564 if (!AM.HasBaseReg) // allow "r+i".
3565 break;
3566 return false; // disallow "r+r" or "r+r+i".
3567 default:
3568 return false;
3569 }
3570
3571 return true;
3572}
3573
3574bool
Dan Gohman2fe6bee2008-10-18 02:06:02 +00003575MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3576 // The Mips target isn't yet aware of offsets.
3577 return false;
3578}
Evan Cheng16993aa2009-10-27 19:56:55 +00003579
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003580EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003581 unsigned SrcAlign,
3582 bool IsMemset, bool ZeroMemset,
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003583 bool MemcpyStrSrc,
3584 MachineFunction &MF) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003585 if (Subtarget.hasMips64())
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003586 return MVT::i64;
3587
3588 return MVT::i32;
3589}
3590
Evan Cheng83896a52009-10-28 01:43:28 +00003591bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
3592 if (VT != MVT::f32 && VT != MVT::f64)
3593 return false;
Bruno Cardoso Lopesb02a9df2011-01-18 19:41:41 +00003594 if (Imm.isNegZero())
3595 return false;
Evan Cheng16993aa2009-10-27 19:56:55 +00003596 return Imm.isZero();
3597}
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003598
3599unsigned MipsTargetLowering::getJumpTableEncoding() const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003600 if (ABI.IsN64())
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003601 return MachineJumpTableInfo::EK_GPRel64BlockAddress;
Jia Liuf54f60f2012-02-28 07:46:26 +00003602
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003603 return TargetLowering::getJumpTableEncoding();
3604}
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003605
Eric Christopher824f42f2015-05-12 01:26:05 +00003606bool MipsTargetLowering::useSoftFloat() const {
3607 return Subtarget.useSoftFloat();
3608}
3609
Daniel Sandersf43e6872014-11-01 18:44:56 +00003610void MipsTargetLowering::copyByValRegs(
3611 SDValue Chain, SDLoc DL, std::vector<SDValue> &OutChains, SelectionDAG &DAG,
3612 const ISD::ArgFlagsTy &Flags, SmallVectorImpl<SDValue> &InVals,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003613 const Argument *FuncArg, unsigned FirstReg, unsigned LastReg,
3614 const CCValAssign &VA, MipsCCState &State) const {
Akira Hatanaka25dad192012-10-27 00:10:18 +00003615 MachineFunction &MF = DAG.getMachineFunction();
3616 MachineFrameInfo *MFI = MF.getFrameInfo();
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003617 unsigned GPRSizeInBytes = Subtarget.getGPRSizeInBytes();
Daniel Sanders23e98772014-11-02 16:09:29 +00003618 unsigned NumRegs = LastReg - FirstReg;
3619 unsigned RegAreaSize = NumRegs * GPRSizeInBytes;
Akira Hatanaka25dad192012-10-27 00:10:18 +00003620 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
3621 int FrameObjOffset;
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003622 ArrayRef<MCPhysReg> ByValArgRegs = ABI.GetByValArgRegs();
Akira Hatanaka25dad192012-10-27 00:10:18 +00003623
3624 if (RegAreaSize)
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003625 FrameObjOffset =
3626 (int)ABI.GetCalleeAllocdArgSizeInBytes(State.getCallingConv()) -
3627 (int)((ByValArgRegs.size() - FirstReg) * GPRSizeInBytes);
Akira Hatanaka25dad192012-10-27 00:10:18 +00003628 else
Daniel Sandersf43e6872014-11-01 18:44:56 +00003629 FrameObjOffset = VA.getLocMemOffset();
Akira Hatanaka25dad192012-10-27 00:10:18 +00003630
3631 // Create frame object.
Mehdi Amini44ede332015-07-09 02:09:04 +00003632 EVT PtrTy = getPointerTy(DAG.getDataLayout());
Akira Hatanaka25dad192012-10-27 00:10:18 +00003633 int FI = MFI->CreateFixedObject(FrameObjSize, FrameObjOffset, true);
3634 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
3635 InVals.push_back(FIN);
3636
Daniel Sanders23e98772014-11-02 16:09:29 +00003637 if (!NumRegs)
Akira Hatanaka25dad192012-10-27 00:10:18 +00003638 return;
3639
3640 // Copy arg registers.
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003641 MVT RegTy = MVT::getIntegerVT(GPRSizeInBytes * 8);
Akira Hatanaka25dad192012-10-27 00:10:18 +00003642 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3643
Daniel Sanders23e98772014-11-02 16:09:29 +00003644 for (unsigned I = 0; I < NumRegs; ++I) {
Daniel Sandersd7eba312014-11-07 12:21:37 +00003645 unsigned ArgReg = ByValArgRegs[FirstReg + I];
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003646 unsigned VReg = addLiveIn(MF, ArgReg, RC);
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003647 unsigned Offset = I * GPRSizeInBytes;
Akira Hatanaka25dad192012-10-27 00:10:18 +00003648 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003649 DAG.getConstant(Offset, DL, PtrTy));
Akira Hatanaka25dad192012-10-27 00:10:18 +00003650 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
3651 StorePtr, MachinePointerInfo(FuncArg, Offset),
3652 false, false, 0);
3653 OutChains.push_back(Store);
3654 }
3655}
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003656
3657// Copy byVal arg to registers and stack.
Daniel Sandersf43e6872014-11-01 18:44:56 +00003658void MipsTargetLowering::passByValArg(
3659 SDValue Chain, SDLoc DL,
3660 std::deque<std::pair<unsigned, SDValue>> &RegsToPass,
3661 SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003662 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg, unsigned FirstReg,
3663 unsigned LastReg, const ISD::ArgFlagsTy &Flags, bool isLittle,
3664 const CCValAssign &VA) const {
Daniel Sandersac272632014-05-23 13:18:02 +00003665 unsigned ByValSizeInBytes = Flags.getByValSize();
3666 unsigned OffsetInBytes = 0; // From beginning of struct
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003667 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
Daniel Sandersac272632014-05-23 13:18:02 +00003668 unsigned Alignment = std::min(Flags.getByValAlign(), RegSizeInBytes);
Mehdi Amini44ede332015-07-09 02:09:04 +00003669 EVT PtrTy = getPointerTy(DAG.getDataLayout()),
3670 RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
Daniel Sanders23e98772014-11-02 16:09:29 +00003671 unsigned NumRegs = LastReg - FirstReg;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003672
Daniel Sanders23e98772014-11-02 16:09:29 +00003673 if (NumRegs) {
Eric Christopher96e72c62015-01-29 23:27:36 +00003674 const ArrayRef<MCPhysReg> ArgRegs = ABI.GetByValArgRegs();
Daniel Sanders23e98772014-11-02 16:09:29 +00003675 bool LeftoverBytes = (NumRegs * RegSizeInBytes > ByValSizeInBytes);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003676 unsigned I = 0;
3677
3678 // Copy words to registers.
Daniel Sanders23e98772014-11-02 16:09:29 +00003679 for (; I < NumRegs - LeftoverBytes; ++I, OffsetInBytes += RegSizeInBytes) {
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003680 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003681 DAG.getConstant(OffsetInBytes, DL, PtrTy));
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003682 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
3683 MachinePointerInfo(), false, false, false,
3684 Alignment);
3685 MemOpChains.push_back(LoadVal.getValue(1));
Daniel Sanders23e98772014-11-02 16:09:29 +00003686 unsigned ArgReg = ArgRegs[FirstReg + I];
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003687 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
3688 }
3689
3690 // Return if the struct has been fully copied.
Daniel Sandersac272632014-05-23 13:18:02 +00003691 if (ByValSizeInBytes == OffsetInBytes)
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003692 return;
3693
3694 // Copy the remainder of the byval argument with sub-word loads and shifts.
3695 if (LeftoverBytes) {
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003696 SDValue Val;
3697
Daniel Sandersac272632014-05-23 13:18:02 +00003698 for (unsigned LoadSizeInBytes = RegSizeInBytes / 2, TotalBytesLoaded = 0;
3699 OffsetInBytes < ByValSizeInBytes; LoadSizeInBytes /= 2) {
3700 unsigned RemainingSizeInBytes = ByValSizeInBytes - OffsetInBytes;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003701
Daniel Sandersac272632014-05-23 13:18:02 +00003702 if (RemainingSizeInBytes < LoadSizeInBytes)
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003703 continue;
3704
3705 // Load subword.
3706 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003707 DAG.getConstant(OffsetInBytes, DL,
3708 PtrTy));
Daniel Sandersac272632014-05-23 13:18:02 +00003709 SDValue LoadVal = DAG.getExtLoad(
3710 ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr, MachinePointerInfo(),
Louis Gerbarg67474e32014-07-31 21:45:05 +00003711 MVT::getIntegerVT(LoadSizeInBytes * 8), false, false, false,
3712 Alignment);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003713 MemOpChains.push_back(LoadVal.getValue(1));
3714
3715 // Shift the loaded value.
3716 unsigned Shamt;
3717
3718 if (isLittle)
Daniel Sandersac272632014-05-23 13:18:02 +00003719 Shamt = TotalBytesLoaded * 8;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003720 else
Daniel Sandersac272632014-05-23 13:18:02 +00003721 Shamt = (RegSizeInBytes - (TotalBytesLoaded + LoadSizeInBytes)) * 8;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003722
3723 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003724 DAG.getConstant(Shamt, DL, MVT::i32));
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003725
3726 if (Val.getNode())
3727 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
3728 else
3729 Val = Shift;
3730
Daniel Sandersac272632014-05-23 13:18:02 +00003731 OffsetInBytes += LoadSizeInBytes;
3732 TotalBytesLoaded += LoadSizeInBytes;
3733 Alignment = std::min(Alignment, LoadSizeInBytes);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003734 }
3735
Daniel Sanders23e98772014-11-02 16:09:29 +00003736 unsigned ArgReg = ArgRegs[FirstReg + I];
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003737 RegsToPass.push_back(std::make_pair(ArgReg, Val));
3738 return;
3739 }
3740 }
3741
3742 // Copy remainder of byval arg to it with memcpy.
Daniel Sandersac272632014-05-23 13:18:02 +00003743 unsigned MemCpySize = ByValSizeInBytes - OffsetInBytes;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003744 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003745 DAG.getConstant(OffsetInBytes, DL, PtrTy));
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003746 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003747 DAG.getIntPtrConstant(VA.getLocMemOffset(), DL));
3748 Chain = DAG.getMemcpy(Chain, DL, Dst, Src,
3749 DAG.getConstant(MemCpySize, DL, PtrTy),
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003750 Alignment, /*isVolatile=*/false, /*AlwaysInline=*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00003751 /*isTailCall=*/false,
Nick Lewyckyaad475b2014-04-15 07:22:52 +00003752 MachinePointerInfo(), MachinePointerInfo());
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003753 MemOpChains.push_back(Chain);
3754}
Akira Hatanaka2a134022012-10-27 00:21:13 +00003755
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003756void MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003757 SDValue Chain, SDLoc DL,
3758 SelectionDAG &DAG,
Daniel Sanders853c2432014-11-01 18:13:52 +00003759 CCState &State) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003760 const ArrayRef<MCPhysReg> ArgRegs = ABI.GetVarArgRegs();
Tim Northover3b6b7ca2015-02-21 02:11:17 +00003761 unsigned Idx = State.getFirstUnallocated(ArgRegs);
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003762 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
3763 MVT RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003764 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3765 MachineFunction &MF = DAG.getMachineFunction();
3766 MachineFrameInfo *MFI = MF.getFrameInfo();
3767 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3768
3769 // Offset of the first variable argument from stack pointer.
3770 int VaArgOffset;
3771
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003772 if (ArgRegs.size() == Idx)
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003773 VaArgOffset =
Daniel Sanders853c2432014-11-01 18:13:52 +00003774 RoundUpToAlignment(State.getNextStackOffset(), RegSizeInBytes);
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003775 else {
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003776 VaArgOffset =
3777 (int)ABI.GetCalleeAllocdArgSizeInBytes(State.getCallingConv()) -
3778 (int)(RegSizeInBytes * (ArgRegs.size() - Idx));
3779 }
Akira Hatanaka2a134022012-10-27 00:21:13 +00003780
3781 // Record the frame index of the first variable argument
3782 // which is a value necessary to VASTART.
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003783 int FI = MFI->CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003784 MipsFI->setVarArgsFrameIndex(FI);
3785
3786 // Copy the integer registers that have not been used for argument passing
3787 // to the argument register save area. For O32, the save area is allocated
3788 // in the caller's stack frame, while for N32/64, it is allocated in the
3789 // callee's stack frame.
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003790 for (unsigned I = Idx; I < ArgRegs.size();
3791 ++I, VaArgOffset += RegSizeInBytes) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003792 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003793 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003794 FI = MFI->CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
Mehdi Amini44ede332015-07-09 02:09:04 +00003795 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Akira Hatanaka2a134022012-10-27 00:21:13 +00003796 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
3797 MachinePointerInfo(), false, false, 0);
Eric Christopher1c29a652014-07-18 22:55:25 +00003798 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(
3799 (Value *)nullptr);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003800 OutChains.push_back(Store);
3801 }
3802}
Daniel Sanders23e98772014-11-02 16:09:29 +00003803
3804void MipsTargetLowering::HandleByVal(CCState *State, unsigned &Size,
3805 unsigned Align) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003806 const TargetFrameLowering *TFL = Subtarget.getFrameLowering();
Daniel Sanders23e98772014-11-02 16:09:29 +00003807
3808 assert(Size && "Byval argument's size shouldn't be 0.");
3809
3810 Align = std::min(Align, TFL->getStackAlignment());
3811
3812 unsigned FirstReg = 0;
3813 unsigned NumRegs = 0;
3814
3815 if (State->getCallingConv() != CallingConv::Fast) {
3816 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
Eric Christopher96e72c62015-01-29 23:27:36 +00003817 const ArrayRef<MCPhysReg> IntArgRegs = ABI.GetByValArgRegs();
Daniel Sanders23e98772014-11-02 16:09:29 +00003818 // FIXME: The O32 case actually describes no shadow registers.
3819 const MCPhysReg *ShadowRegs =
Eric Christopher96e72c62015-01-29 23:27:36 +00003820 ABI.IsO32() ? IntArgRegs.data() : Mips64DPRegs;
Daniel Sanders23e98772014-11-02 16:09:29 +00003821
3822 // We used to check the size as well but we can't do that anymore since
3823 // CCState::HandleByVal() rounds up the size after calling this function.
3824 assert(!(Align % RegSizeInBytes) &&
3825 "Byval argument's alignment should be a multiple of"
3826 "RegSizeInBytes.");
3827
Tim Northover3b6b7ca2015-02-21 02:11:17 +00003828 FirstReg = State->getFirstUnallocated(IntArgRegs);
Daniel Sanders23e98772014-11-02 16:09:29 +00003829
3830 // If Align > RegSizeInBytes, the first arg register must be even.
3831 // FIXME: This condition happens to do the right thing but it's not the
3832 // right way to test it. We want to check that the stack frame offset
3833 // of the register is aligned.
3834 if ((Align > RegSizeInBytes) && (FirstReg % 2)) {
3835 State->AllocateReg(IntArgRegs[FirstReg], ShadowRegs[FirstReg]);
3836 ++FirstReg;
3837 }
3838
3839 // Mark the registers allocated.
3840 Size = RoundUpToAlignment(Size, RegSizeInBytes);
3841 for (unsigned I = FirstReg; Size > 0 && (I < IntArgRegs.size());
3842 Size -= RegSizeInBytes, ++I, ++NumRegs)
3843 State->AllocateReg(IntArgRegs[I], ShadowRegs[I]);
3844 }
3845
3846 State->addInRegsParamInfo(FirstReg, FirstReg + NumRegs);
3847}
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00003848
3849MachineBasicBlock *
3850MipsTargetLowering::emitPseudoSELECT(MachineInstr *MI, MachineBasicBlock *BB,
3851 bool isFPCmp, unsigned Opc) const {
3852 assert(!(Subtarget.hasMips4() || Subtarget.hasMips32()) &&
3853 "Subtarget already supports SELECT nodes with the use of"
3854 "conditional-move instructions.");
3855
3856 const TargetInstrInfo *TII =
Eric Christopher96e72c62015-01-29 23:27:36 +00003857 Subtarget.getInstrInfo();
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00003858 DebugLoc DL = MI->getDebugLoc();
3859
3860 // To "insert" a SELECT instruction, we actually have to insert the
3861 // diamond control-flow pattern. The incoming instruction knows the
3862 // destination vreg to set, the condition code register to branch on, the
3863 // true/false values to select between, and a branch opcode to use.
3864 const BasicBlock *LLVM_BB = BB->getBasicBlock();
3865 MachineFunction::iterator It = BB;
3866 ++It;
3867
3868 // thisMBB:
3869 // ...
3870 // TrueVal = ...
3871 // setcc r1, r2, r3
3872 // bNE r1, r0, copy1MBB
3873 // fallthrough --> copy0MBB
3874 MachineBasicBlock *thisMBB = BB;
3875 MachineFunction *F = BB->getParent();
3876 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
3877 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
3878 F->insert(It, copy0MBB);
3879 F->insert(It, sinkMBB);
3880
3881 // Transfer the remainder of BB and its successor edges to sinkMBB.
3882 sinkMBB->splice(sinkMBB->begin(), BB,
3883 std::next(MachineBasicBlock::iterator(MI)), BB->end());
3884 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
3885
3886 // Next, add the true and fallthrough blocks as its successors.
3887 BB->addSuccessor(copy0MBB);
3888 BB->addSuccessor(sinkMBB);
3889
3890 if (isFPCmp) {
3891 // bc1[tf] cc, sinkMBB
3892 BuildMI(BB, DL, TII->get(Opc))
3893 .addReg(MI->getOperand(1).getReg())
3894 .addMBB(sinkMBB);
3895 } else {
3896 // bne rs, $0, sinkMBB
3897 BuildMI(BB, DL, TII->get(Opc))
3898 .addReg(MI->getOperand(1).getReg())
3899 .addReg(Mips::ZERO)
3900 .addMBB(sinkMBB);
3901 }
3902
3903 // copy0MBB:
3904 // %FalseValue = ...
3905 // # fallthrough to sinkMBB
3906 BB = copy0MBB;
3907
3908 // Update machine-CFG edges
3909 BB->addSuccessor(sinkMBB);
3910
3911 // sinkMBB:
3912 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
3913 // ...
3914 BB = sinkMBB;
3915
3916 BuildMI(*BB, BB->begin(), DL,
3917 TII->get(Mips::PHI), MI->getOperand(0).getReg())
3918 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
3919 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB);
3920
3921 MI->eraseFromParent(); // The pseudo instruction is gone now.
3922
3923 return BB;
3924}
Daniel Sanders1440bb22015-01-09 17:21:30 +00003925
3926// FIXME? Maybe this could be a TableGen attribute on some registers and
3927// this table could be generated automatically from RegInfo.
3928unsigned MipsTargetLowering::getRegisterByName(const char* RegName,
3929 EVT VT) const {
3930 // Named registers is expected to be fairly rare. For now, just support $28
3931 // since the linux kernel uses it.
3932 if (Subtarget.isGP64bit()) {
3933 unsigned Reg = StringSwitch<unsigned>(RegName)
3934 .Case("$28", Mips::GP_64)
3935 .Default(0);
3936 if (Reg)
3937 return Reg;
3938 } else {
3939 unsigned Reg = StringSwitch<unsigned>(RegName)
3940 .Case("$28", Mips::GP)
3941 .Default(0);
3942 if (Reg)
3943 return Reg;
3944 }
3945 report_fatal_error("Invalid register name global variable");
3946}