blob: c78b79fcf1a7778103ffdb675a3bbd32ecadc312 [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 {
115 switch (Opcode) {
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000116 case MipsISD::JmpLink: return "MipsISD::JmpLink";
Akira Hatanaka91318df2012-10-19 20:59:39 +0000117 case MipsISD::TailCall: return "MipsISD::TailCall";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000118 case MipsISD::Hi: return "MipsISD::Hi";
119 case MipsISD::Lo: return "MipsISD::Lo";
120 case MipsISD::GPRel: return "MipsISD::GPRel";
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +0000121 case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000122 case MipsISD::Ret: return "MipsISD::Ret";
Akira Hatanakac0b02062013-01-30 00:26:49 +0000123 case MipsISD::EH_RETURN: return "MipsISD::EH_RETURN";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000124 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
125 case MipsISD::FPCmp: return "MipsISD::FPCmp";
126 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
127 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000128 case MipsISD::TruncIntFP: return "MipsISD::TruncIntFP";
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000129 case MipsISD::MFHI: return "MipsISD::MFHI";
130 case MipsISD::MFLO: return "MipsISD::MFLO";
131 case MipsISD::MTLOHI: return "MipsISD::MTLOHI";
Akira Hatanaka28721bd2013-03-30 01:14:04 +0000132 case MipsISD::Mult: return "MipsISD::Mult";
133 case MipsISD::Multu: return "MipsISD::Multu";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000134 case MipsISD::MAdd: return "MipsISD::MAdd";
135 case MipsISD::MAddu: return "MipsISD::MAddu";
136 case MipsISD::MSub: return "MipsISD::MSub";
137 case MipsISD::MSubu: return "MipsISD::MSubu";
138 case MipsISD::DivRem: return "MipsISD::DivRem";
139 case MipsISD::DivRemU: return "MipsISD::DivRemU";
Akira Hatanaka28721bd2013-03-30 01:14:04 +0000140 case MipsISD::DivRem16: return "MipsISD::DivRem16";
141 case MipsISD::DivRemU16: return "MipsISD::DivRemU16";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000142 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
143 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
Akira Hatanakafaa88c02011-12-12 22:38:19 +0000144 case MipsISD::Wrapper: return "MipsISD::Wrapper";
Akira Hatanakaa4c09bc2011-07-19 23:30:50 +0000145 case MipsISD::Sync: return "MipsISD::Sync";
Akira Hatanaka5360f882011-08-17 02:05:42 +0000146 case MipsISD::Ext: return "MipsISD::Ext";
147 case MipsISD::Ins: return "MipsISD::Ins";
Akira Hatanakab9ebf8d2012-06-02 00:03:12 +0000148 case MipsISD::LWL: return "MipsISD::LWL";
149 case MipsISD::LWR: return "MipsISD::LWR";
150 case MipsISD::SWL: return "MipsISD::SWL";
151 case MipsISD::SWR: return "MipsISD::SWR";
152 case MipsISD::LDL: return "MipsISD::LDL";
153 case MipsISD::LDR: return "MipsISD::LDR";
154 case MipsISD::SDL: return "MipsISD::SDL";
155 case MipsISD::SDR: return "MipsISD::SDR";
Akira Hatanaka233ac532012-09-21 23:52:47 +0000156 case MipsISD::EXTP: return "MipsISD::EXTP";
157 case MipsISD::EXTPDP: return "MipsISD::EXTPDP";
158 case MipsISD::EXTR_S_H: return "MipsISD::EXTR_S_H";
159 case MipsISD::EXTR_W: return "MipsISD::EXTR_W";
160 case MipsISD::EXTR_R_W: return "MipsISD::EXTR_R_W";
161 case MipsISD::EXTR_RS_W: return "MipsISD::EXTR_RS_W";
162 case MipsISD::SHILO: return "MipsISD::SHILO";
163 case MipsISD::MTHLIP: return "MipsISD::MTHLIP";
164 case MipsISD::MULT: return "MipsISD::MULT";
165 case MipsISD::MULTU: return "MipsISD::MULTU";
Jia Liu434874d2013-03-04 01:06:54 +0000166 case MipsISD::MADD_DSP: return "MipsISD::MADD_DSP";
Akira Hatanaka233ac532012-09-21 23:52:47 +0000167 case MipsISD::MADDU_DSP: return "MipsISD::MADDU_DSP";
168 case MipsISD::MSUB_DSP: return "MipsISD::MSUB_DSP";
169 case MipsISD::MSUBU_DSP: return "MipsISD::MSUBU_DSP";
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000170 case MipsISD::SHLL_DSP: return "MipsISD::SHLL_DSP";
171 case MipsISD::SHRA_DSP: return "MipsISD::SHRA_DSP";
172 case MipsISD::SHRL_DSP: return "MipsISD::SHRL_DSP";
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000173 case MipsISD::SETCC_DSP: return "MipsISD::SETCC_DSP";
174 case MipsISD::SELECT_CC_DSP: return "MipsISD::SELECT_CC_DSP";
Daniel Sandersce09d072013-08-28 12:14:50 +0000175 case MipsISD::VALL_ZERO: return "MipsISD::VALL_ZERO";
176 case MipsISD::VANY_ZERO: return "MipsISD::VANY_ZERO";
177 case MipsISD::VALL_NONZERO: return "MipsISD::VALL_NONZERO";
178 case MipsISD::VANY_NONZERO: return "MipsISD::VANY_NONZERO";
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000179 case MipsISD::VCEQ: return "MipsISD::VCEQ";
180 case MipsISD::VCLE_S: return "MipsISD::VCLE_S";
181 case MipsISD::VCLE_U: return "MipsISD::VCLE_U";
182 case MipsISD::VCLT_S: return "MipsISD::VCLT_S";
183 case MipsISD::VCLT_U: return "MipsISD::VCLT_U";
Daniel Sanders3ce56622013-09-24 12:18:31 +0000184 case MipsISD::VSMAX: return "MipsISD::VSMAX";
185 case MipsISD::VSMIN: return "MipsISD::VSMIN";
186 case MipsISD::VUMAX: return "MipsISD::VUMAX";
187 case MipsISD::VUMIN: return "MipsISD::VUMIN";
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000188 case MipsISD::VEXTRACT_SEXT_ELT: return "MipsISD::VEXTRACT_SEXT_ELT";
189 case MipsISD::VEXTRACT_ZEXT_ELT: return "MipsISD::VEXTRACT_ZEXT_ELT";
Daniel Sandersf7456c72013-09-23 13:22:24 +0000190 case MipsISD::VNOR: return "MipsISD::VNOR";
Daniel Sanderse5087042013-09-24 14:02:15 +0000191 case MipsISD::VSHF: return "MipsISD::VSHF";
Daniel Sanders26307182013-09-24 14:20:00 +0000192 case MipsISD::SHF: return "MipsISD::SHF";
Daniel Sanders2ed228b2013-09-24 14:36:12 +0000193 case MipsISD::ILVEV: return "MipsISD::ILVEV";
194 case MipsISD::ILVOD: return "MipsISD::ILVOD";
195 case MipsISD::ILVL: return "MipsISD::ILVL";
196 case MipsISD::ILVR: return "MipsISD::ILVR";
Daniel Sandersfae5f2a2013-09-24 14:53:25 +0000197 case MipsISD::PCKEV: return "MipsISD::PCKEV";
198 case MipsISD::PCKOD: return "MipsISD::PCKOD";
Daniel Sandersb50ccf82014-04-01 10:35:28 +0000199 case MipsISD::INSVE: return "MipsISD::INSVE";
Craig Topper062a2ba2014-04-25 05:30:21 +0000200 default: return nullptr;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000201 }
202}
203
Eric Christopherb1526602014-09-19 23:30:42 +0000204MipsTargetLowering::MipsTargetLowering(const MipsTargetMachine &TM,
Eric Christopher8924d272014-07-18 23:25:04 +0000205 const MipsSubtarget &STI)
Eric Christopher96e72c62015-01-29 23:27:36 +0000206 : TargetLowering(TM), Subtarget(STI), ABI(TM.getABI()) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000207 // Mips does not have i1 type, so use i32 for
Wesley Peck527da1b2010-11-23 03:31:01 +0000208 // setcc operations results (slt, sgt, ...).
Duncan Sands8d6e2e12008-11-23 15:47:28 +0000209 setBooleanContents(ZeroOrOneBooleanContent);
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000210 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Daniel Sanderscbd44c52014-07-10 10:18:12 +0000211 // The cmp.cond.fmt instruction in MIPS32r6/MIPS64r6 uses 0 and -1 like MSA
212 // does. Integer booleans still use 0 and 1.
Eric Christopher1c29a652014-07-18 22:55:25 +0000213 if (Subtarget.hasMips32r6())
Daniel Sanderscbd44c52014-07-10 10:18:12 +0000214 setBooleanContents(ZeroOrOneBooleanContent,
215 ZeroOrNegativeOneBooleanContent);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000216
Wesley Peck527da1b2010-11-23 03:31:01 +0000217 // Load extented operations for i1 types must be promoted
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000218 for (MVT VT : MVT::integer_valuetypes()) {
219 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
220 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
221 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
222 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000223
Pirama Arumuga Nainar34056de2015-04-20 20:15:36 +0000224 // MIPS doesn't have extending float->double load/store. Set LoadExtAction
225 // for f32, f16
226 for (MVT VT : MVT::fp_valuetypes()) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000227 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
Pirama Arumuga Nainar34056de2015-04-20 20:15:36 +0000228 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f16, Expand);
229 }
230
231 // Set LoadExtAction for f16 vectors to Expand
232 for (MVT VT : MVT::fp_vector_valuetypes()) {
233 MVT F16VT = MVT::getVectorVT(MVT::f16, VT.getVectorNumElements());
234 if (F16VT.isValid())
235 setLoadExtAction(ISD::EXTLOAD, VT, F16VT, Expand);
236 }
237
238 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
239 setTruncStoreAction(MVT::f64, MVT::f16, Expand);
240
Owen Anderson9f944592009-08-11 20:47:22 +0000241 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman39d6faa2009-07-17 02:28:12 +0000242
Wesley Peck527da1b2010-11-23 03:31:01 +0000243 // Used by legalize types to correctly generate the setcc result.
244 // Without this, every float setcc comes with a AND/OR with the result,
245 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +0000246 // which is used implicitly by brcond and select operations.
Owen Anderson9f944592009-08-11 20:47:22 +0000247 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +0000248
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000249 // Mips Custom Operations
Akira Hatanaka0f693a82013-03-06 21:32:03 +0000250 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000251 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000252 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000253 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
254 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
255 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
256 setOperationAction(ISD::SELECT, MVT::f32, Custom);
257 setOperationAction(ISD::SELECT, MVT::f64, Custom);
258 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Akira Hatanaka24cf4e32012-07-11 19:32:27 +0000259 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
260 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Akira Hatanakab7f78592012-03-09 23:46:03 +0000261 setOperationAction(ISD::SETCC, MVT::f32, Custom);
262 setOperationAction(ISD::SETCC, MVT::f64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000263 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000264 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
265 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000266 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000267
Eric Christopher1c29a652014-07-18 22:55:25 +0000268 if (Subtarget.isGP64bit()) {
Akira Hatanakada00aa82012-03-10 00:03:50 +0000269 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
270 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
271 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
272 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
273 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
274 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Akira Hatanaka019e5922012-06-02 00:04:42 +0000275 setOperationAction(ISD::LOAD, MVT::i64, Custom);
276 setOperationAction(ISD::STORE, MVT::i64, Custom);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000277 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +0000278 setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom);
279 setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom);
280 setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000281 }
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +0000282
Eric Christopher1c29a652014-07-18 22:55:25 +0000283 if (!Subtarget.isGP64bit()) {
Akira Hatanaka0a8ab712012-05-09 00:55:21 +0000284 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
285 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
286 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
287 }
288
Akira Hatanaka28e02ec2012-11-07 19:10:58 +0000289 setOperationAction(ISD::ADD, MVT::i32, Custom);
Eric Christopher1c29a652014-07-18 22:55:25 +0000290 if (Subtarget.isGP64bit())
Akira Hatanaka28e02ec2012-11-07 19:10:58 +0000291 setOperationAction(ISD::ADD, MVT::i64, Custom);
292
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000293 setOperationAction(ISD::SDIV, MVT::i32, Expand);
294 setOperationAction(ISD::SREM, MVT::i32, Expand);
295 setOperationAction(ISD::UDIV, MVT::i32, Expand);
296 setOperationAction(ISD::UREM, MVT::i32, Expand);
Akira Hatanakab1538f92011-10-03 21:06:13 +0000297 setOperationAction(ISD::SDIV, MVT::i64, Expand);
298 setOperationAction(ISD::SREM, MVT::i64, Expand);
299 setOperationAction(ISD::UDIV, MVT::i64, Expand);
300 setOperationAction(ISD::UREM, MVT::i64, Expand);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000301
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000302 // Operations not directly supported by Mips.
Tom Stellardb1588fc2013-03-08 15:36:57 +0000303 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
304 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
305 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
306 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
Tom Stellard3787b122014-06-10 16:01:29 +0000307 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
308 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000309 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Akira Hatanaka79aed152011-12-20 23:40:56 +0000310 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000311 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Akira Hatanaka79aed152011-12-20 23:40:56 +0000312 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000313 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Eric Christopher1c29a652014-07-18 22:55:25 +0000314 if (Subtarget.hasCnMips()) {
Kai Nacke93fe5e82014-03-20 11:51:58 +0000315 setOperationAction(ISD::CTPOP, MVT::i32, Legal);
316 setOperationAction(ISD::CTPOP, MVT::i64, Legal);
317 } else {
318 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
319 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
320 }
Owen Anderson9f944592009-08-11 20:47:22 +0000321 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
Akira Hatanaka410ce9c2011-12-21 00:14:05 +0000322 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000323 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
324 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
325 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
326 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000327 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Akira Hatanaka7ba8a8d2011-09-30 18:51:46 +0000328 setOperationAction(ISD::ROTL, MVT::i64, Expand);
Akira Hatanaka33a25af2012-07-31 20:54:48 +0000329 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
330 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Bruno Cardoso Lopesd47180e2010-12-09 17:32:30 +0000331
Eric Christopher1c29a652014-07-18 22:55:25 +0000332 if (!Subtarget.hasMips32r2())
Bruno Cardoso Lopesd47180e2010-12-09 17:32:30 +0000333 setOperationAction(ISD::ROTR, MVT::i32, Expand);
334
Eric Christopher1c29a652014-07-18 22:55:25 +0000335 if (!Subtarget.hasMips64r2())
Akira Hatanaka7ba8a8d2011-09-30 18:51:46 +0000336 setOperationAction(ISD::ROTR, MVT::i64, Expand);
337
Owen Anderson9f944592009-08-11 20:47:22 +0000338 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes22b69db2011-03-04 18:54:14 +0000339 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000340 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes22b69db2011-03-04 18:54:14 +0000341 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000342 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
343 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000344 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
345 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanakadfb8cda2011-05-23 22:23:58 +0000346 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000347 setOperationAction(ISD::FLOG, MVT::f32, Expand);
348 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
349 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
350 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000351 setOperationAction(ISD::FMA, MVT::f32, Expand);
352 setOperationAction(ISD::FMA, MVT::f64, Expand);
Akira Hatanaka0603ad82012-03-29 18:43:11 +0000353 setOperationAction(ISD::FREM, MVT::f32, Expand);
354 setOperationAction(ISD::FREM, MVT::f64, Expand);
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000355
Pirama Arumuga Nainar34056de2015-04-20 20:15:36 +0000356 // Lower f16 conversion operations into library calls
357 setOperationAction(ISD::FP16_TO_FP, MVT::f32, Expand);
358 setOperationAction(ISD::FP_TO_FP16, MVT::f32, Expand);
359 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand);
360 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Expand);
361
Akira Hatanakac0b02062013-01-30 00:26:49 +0000362 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
363
Daniel Sanders2b553d42014-08-01 09:17:39 +0000364 setOperationAction(ISD::VASTART, MVT::Other, Custom);
365 setOperationAction(ISD::VAARG, MVT::Other, Custom);
Bruno Cardoso Lopes048ffab2011-03-09 19:22:22 +0000366 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
367 setOperationAction(ISD::VAEND, MVT::Other, Expand);
368
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000369 // Use the default for now
Owen Anderson9f944592009-08-11 20:47:22 +0000370 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
371 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman26a48482011-07-27 22:21:52 +0000372
Jia Liuf54f60f2012-02-28 07:46:26 +0000373 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
374 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
375 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
376 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
Eli Friedman7dfa7912011-08-29 18:23:02 +0000377
Eli Friedman30a49e92011-08-03 21:06:02 +0000378 setInsertFencesForAtomic(true);
379
Eric Christopher1c29a652014-07-18 22:55:25 +0000380 if (!Subtarget.hasMips32r2()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000381 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
382 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000383 }
384
Daniel Sanders070fd1c2014-05-12 12:41:59 +0000385 // MIPS16 lacks MIPS32's clz and clo instructions.
Eric Christopher1c29a652014-07-18 22:55:25 +0000386 if (!Subtarget.hasMips32() || Subtarget.inMips16Mode())
Owen Anderson9f944592009-08-11 20:47:22 +0000387 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Eric Christopher1c29a652014-07-18 22:55:25 +0000388 if (!Subtarget.hasMips64())
Akira Hatanaka1d8efab2011-12-21 00:20:27 +0000389 setOperationAction(ISD::CTLZ, MVT::i64, Expand);
Bruno Cardoso Lopes93da7e62008-08-08 06:16:31 +0000390
Eric Christopher1c29a652014-07-18 22:55:25 +0000391 if (!Subtarget.hasMips32r2())
Owen Anderson9f944592009-08-11 20:47:22 +0000392 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Eric Christopher1c29a652014-07-18 22:55:25 +0000393 if (!Subtarget.hasMips64r2())
Akira Hatanaka4706ac92011-12-20 23:56:43 +0000394 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000395
Eric Christopher1c29a652014-07-18 22:55:25 +0000396 if (Subtarget.isGP64bit()) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000397 setLoadExtAction(ISD::SEXTLOAD, MVT::i64, MVT::i32, Custom);
398 setLoadExtAction(ISD::ZEXTLOAD, MVT::i64, MVT::i32, Custom);
399 setLoadExtAction(ISD::EXTLOAD, MVT::i64, MVT::i32, Custom);
Akira Hatanaka019e5922012-06-02 00:04:42 +0000400 setTruncStoreAction(MVT::i64, MVT::i32, Custom);
401 }
402
Akira Hatanakaa3d9ab92013-07-26 20:58:55 +0000403 setOperationAction(ISD::TRAP, MVT::Other, Legal);
404
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000405 setTargetDAGCombine(ISD::SDIVREM);
406 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka5e152182012-03-08 03:26:37 +0000407 setTargetDAGCombine(ISD::SELECT);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000408 setTargetDAGCombine(ISD::AND);
409 setTargetDAGCombine(ISD::OR);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000410 setTargetDAGCombine(ISD::ADD);
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000411
Eric Christopher1c29a652014-07-18 22:55:25 +0000412 setMinFunctionAlignment(Subtarget.isGP64bit() ? 3 : 2);
Eli Friedman2518f832011-05-06 20:34:06 +0000413
Daniel Sanders2b553d42014-08-01 09:17:39 +0000414 // The arguments on the stack are defined in terms of 4-byte slots on O32
415 // and 8-byte slots on N32/N64.
Eric Christopher96e72c62015-01-29 23:27:36 +0000416 setMinStackArgumentAlignment((ABI.IsN32() || ABI.IsN64()) ? 8 : 4);
Daniel Sanders2b553d42014-08-01 09:17:39 +0000417
Eric Christopher96e72c62015-01-29 23:27:36 +0000418 setStackPointerRegisterToSaveRestore(ABI.IsN64() ? Mips::SP_64 : Mips::SP);
Akira Hatanakaaa560002011-05-26 18:59:03 +0000419
Eric Christopher96e72c62015-01-29 23:27:36 +0000420 setExceptionPointerRegister(ABI.IsN64() ? Mips::A0_64 : Mips::A0);
421 setExceptionSelectorRegister(ABI.IsN64() ? Mips::A1_64 : Mips::A1);
Akira Hatanaka1daf8c22012-06-13 19:33:32 +0000422
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000423 MaxStoresPerMemcpy = 16;
Zoran Jovanovicff9d5f32013-12-19 16:12:56 +0000424
Eric Christopher1c29a652014-07-18 22:55:25 +0000425 isMicroMips = Subtarget.inMicroMipsMode();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000426}
427
Eric Christopherb1526602014-09-19 23:30:42 +0000428const MipsTargetLowering *MipsTargetLowering::create(const MipsTargetMachine &TM,
Eric Christopher8924d272014-07-18 23:25:04 +0000429 const MipsSubtarget &STI) {
430 if (STI.inMips16Mode())
431 return llvm::createMips16TargetLowering(TM, STI);
Jia Liuf54f60f2012-02-28 07:46:26 +0000432
Eric Christopher8924d272014-07-18 23:25:04 +0000433 return llvm::createMipsSETargetLowering(TM, STI);
Akira Hatanaka2fcc1cf2011-08-12 21:30:06 +0000434}
435
Reed Kotler720c5ca2014-04-17 22:15:34 +0000436// Create a fast isel object.
437FastISel *
438MipsTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
439 const TargetLibraryInfo *libInfo) const {
440 if (!EnableMipsFastISel)
441 return TargetLowering::createFastISel(funcInfo, libInfo);
442 return Mips::createFastISel(funcInfo, libInfo);
443}
444
Matt Arsenault758659232013-05-18 00:21:46 +0000445EVT MipsTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Akira Hatanakab13b3332013-01-04 20:06:01 +0000446 if (!VT.isVector())
447 return MVT::i32;
448 return VT.changeVectorElementTypeToInteger();
Scott Michela6729e82008-03-10 15:42:14 +0000449}
450
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000451static SDValue performDivRemCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000452 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000453 const MipsSubtarget &Subtarget) {
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000454 if (DCI.isBeforeLegalizeOps())
455 return SDValue();
456
Akira Hatanakab1538f92011-10-03 21:06:13 +0000457 EVT Ty = N->getValueType(0);
Akira Hatanaka8002a3f2013-08-14 00:47:08 +0000458 unsigned LO = (Ty == MVT::i32) ? Mips::LO0 : Mips::LO0_64;
459 unsigned HI = (Ty == MVT::i32) ? Mips::HI0 : Mips::HI0_64;
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000460 unsigned Opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem16 :
461 MipsISD::DivRemU16;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000462 SDLoc DL(N);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000463
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000464 SDValue DivRem = DAG.getNode(Opc, DL, MVT::Glue,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000465 N->getOperand(0), N->getOperand(1));
466 SDValue InChain = DAG.getEntryNode();
467 SDValue InGlue = DivRem;
468
469 // insert MFLO
470 if (N->hasAnyUseOfValue(0)) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000471 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, DL, LO, Ty,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000472 InGlue);
473 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
474 InChain = CopyFromLo.getValue(1);
475 InGlue = CopyFromLo.getValue(2);
476 }
477
478 // insert MFHI
479 if (N->hasAnyUseOfValue(1)) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000480 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, DL,
Akira Hatanakab1538f92011-10-03 21:06:13 +0000481 HI, Ty, InGlue);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000482 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
483 }
484
485 return SDValue();
486}
487
Akira Hatanaka89af5892013-04-18 01:00:46 +0000488static Mips::CondCode condCodeToFCC(ISD::CondCode CC) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000489 switch (CC) {
490 default: llvm_unreachable("Unknown fp condition code!");
491 case ISD::SETEQ:
492 case ISD::SETOEQ: return Mips::FCOND_OEQ;
493 case ISD::SETUNE: return Mips::FCOND_UNE;
494 case ISD::SETLT:
495 case ISD::SETOLT: return Mips::FCOND_OLT;
496 case ISD::SETGT:
497 case ISD::SETOGT: return Mips::FCOND_OGT;
498 case ISD::SETLE:
499 case ISD::SETOLE: return Mips::FCOND_OLE;
500 case ISD::SETGE:
501 case ISD::SETOGE: return Mips::FCOND_OGE;
502 case ISD::SETULT: return Mips::FCOND_ULT;
503 case ISD::SETULE: return Mips::FCOND_ULE;
504 case ISD::SETUGT: return Mips::FCOND_UGT;
505 case ISD::SETUGE: return Mips::FCOND_UGE;
506 case ISD::SETUO: return Mips::FCOND_UN;
507 case ISD::SETO: return Mips::FCOND_OR;
508 case ISD::SETNE:
509 case ISD::SETONE: return Mips::FCOND_ONE;
510 case ISD::SETUEQ: return Mips::FCOND_UEQ;
511 }
512}
513
514
Akira Hatanakaf0ea5002013-03-30 01:16:38 +0000515/// This function returns true if the floating point conditional branches and
516/// conditional moves which use condition code CC should be inverted.
517static bool invertFPCondCodeUser(Mips::CondCode CC) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000518 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
519 return false;
520
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000521 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
522 "Illegal Condition Code");
Akira Hatanakaa5352702011-03-31 18:26:17 +0000523
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000524 return true;
Akira Hatanakaa5352702011-03-31 18:26:17 +0000525}
526
527// Creates and returns an FPCmp node from a setcc node.
528// Returns Op if setcc is not a floating point comparison.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000529static SDValue createFPCmp(SelectionDAG &DAG, const SDValue &Op) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000530 // must be a SETCC node
531 if (Op.getOpcode() != ISD::SETCC)
532 return Op;
533
534 SDValue LHS = Op.getOperand(0);
535
536 if (!LHS.getValueType().isFloatingPoint())
537 return Op;
538
539 SDValue RHS = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000540 SDLoc DL(Op);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000541
Akira Hatanakaaef55c82011-04-15 21:00:26 +0000542 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
543 // node if necessary.
Akira Hatanakaa5352702011-03-31 18:26:17 +0000544 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
545
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000546 return DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS,
Akira Hatanaka89af5892013-04-18 01:00:46 +0000547 DAG.getConstant(condCodeToFCC(CC), MVT::i32));
Akira Hatanakaa5352702011-03-31 18:26:17 +0000548}
549
550// Creates and returns a CMovFPT/F node.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000551static SDValue createCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000552 SDValue False, SDLoc DL) {
Akira Hatanakaf0ea5002013-03-30 01:16:38 +0000553 ConstantSDNode *CC = cast<ConstantSDNode>(Cond.getOperand(2));
554 bool invert = invertFPCondCodeUser((Mips::CondCode)CC->getSExtValue());
Akira Hatanaka8bce21c2013-07-26 20:51:20 +0000555 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000556
557 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
Akira Hatanaka8bce21c2013-07-26 20:51:20 +0000558 True.getValueType(), True, FCC0, False, Cond);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000559}
560
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000561static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000562 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000563 const MipsSubtarget &Subtarget) {
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000564 if (DCI.isBeforeLegalizeOps())
565 return SDValue();
566
567 SDValue SetCC = N->getOperand(0);
568
569 if ((SetCC.getOpcode() != ISD::SETCC) ||
570 !SetCC.getOperand(0).getValueType().isInteger())
571 return SDValue();
572
573 SDValue False = N->getOperand(2);
574 EVT FalseTy = False.getValueType();
575
576 if (!FalseTy.isInteger())
577 return SDValue();
578
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000579 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(False);
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000580
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000581 // If the RHS (False) is 0, we swap the order of the operands
582 // of ISD::SELECT (obviously also inverting the condition) so that we can
583 // take advantage of conditional moves using the $0 register.
584 // Example:
585 // return (a != 0) ? x : 0;
586 // load $reg, x
587 // movz $reg, $0, a
588 if (!FalseC)
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000589 return SDValue();
590
Andrew Trickef9de2a2013-05-25 02:42:55 +0000591 const SDLoc DL(N);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000592
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000593 if (!FalseC->getZExtValue()) {
594 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
595 SDValue True = N->getOperand(1);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000596
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000597 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
598 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
599
600 return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True);
601 }
602
Matheus Almeidaa6beac12013-12-05 12:07:05 +0000603 // If both operands are integer constants there's a possibility that we
604 // can do some interesting optimizations.
605 SDValue True = N->getOperand(1);
606 ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(True);
607
608 if (!TrueC || !True.getValueType().isInteger())
609 return SDValue();
610
611 // We'll also ignore MVT::i64 operands as this optimizations proves
612 // to be ineffective because of the required sign extensions as the result
613 // of a SETCC operator is always MVT::i32 for non-vector types.
614 if (True.getValueType() == MVT::i64)
615 return SDValue();
616
617 int64_t Diff = TrueC->getSExtValue() - FalseC->getSExtValue();
618
619 // 1) (a < x) ? y : y-1
620 // slti $reg1, a, x
621 // addiu $reg2, $reg1, y-1
622 if (Diff == 1)
623 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, False);
624
625 // 2) (a < x) ? y-1 : y
626 // slti $reg1, a, x
627 // xor $reg1, $reg1, 1
628 // addiu $reg2, $reg1, y-1
629 if (Diff == -1) {
630 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
631 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
632 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
633 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, True);
634 }
635
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000636 // Couldn't optimize.
637 return SDValue();
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000638}
639
Vasileios Kalintirise741eb22015-03-02 12:47:32 +0000640static SDValue performCMovFPCombine(SDNode *N, SelectionDAG &DAG,
641 TargetLowering::DAGCombinerInfo &DCI,
642 const MipsSubtarget &Subtarget) {
643 if (DCI.isBeforeLegalizeOps())
644 return SDValue();
645
646 SDValue ValueIfTrue = N->getOperand(0), ValueIfFalse = N->getOperand(2);
647
648 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(ValueIfFalse);
649 if (!FalseC || FalseC->getZExtValue())
650 return SDValue();
651
652 // Since RHS (False) is 0, we swap the order of the True/False operands
653 // (obviously also inverting the condition) so that we can
654 // take advantage of conditional moves using the $0 register.
655 // Example:
656 // return (a != 0) ? x : 0;
657 // load $reg, x
658 // movz $reg, $0, a
659 unsigned Opc = (N->getOpcode() == MipsISD::CMovFP_T) ? MipsISD::CMovFP_F :
660 MipsISD::CMovFP_T;
661
662 SDValue FCC = N->getOperand(1), Glue = N->getOperand(3);
Vasileios Kalintiris2ef28882015-03-04 12:10:18 +0000663 return DAG.getNode(Opc, SDLoc(N), ValueIfFalse.getValueType(),
664 ValueIfFalse, FCC, ValueIfTrue, Glue);
Vasileios Kalintirise741eb22015-03-02 12:47:32 +0000665}
666
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000667static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000668 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000669 const MipsSubtarget &Subtarget) {
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000670 // Pattern match EXT.
671 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
672 // => ext $dst, $src, size, pos
Eric Christopher1c29a652014-07-18 22:55:25 +0000673 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasExtractInsert())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000674 return SDValue();
675
676 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000677 unsigned ShiftRightOpc = ShiftRight.getOpcode();
678
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000679 // Op's first operand must be a shift right.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000680 if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL)
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000681 return SDValue();
682
683 // The second operand of the shift must be an immediate.
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000684 ConstantSDNode *CN;
685 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
686 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000687
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000688 uint64_t Pos = CN->getZExtValue();
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000689 uint64_t SMPos, SMSize;
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000690
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000691 // Op's second operand must be a shifted mask.
692 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000693 !isShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000694 return SDValue();
695
696 // Return if the shifted mask does not start at bit 0 or the sum of its size
697 // and Pos exceeds the word's size.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000698 EVT ValTy = N->getValueType(0);
699 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000700 return SDValue();
701
Andrew Trickef9de2a2013-05-25 02:42:55 +0000702 return DAG.getNode(MipsISD::Ext, SDLoc(N), ValTy,
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000703 ShiftRight.getOperand(0), DAG.getConstant(Pos, MVT::i32),
Akira Hatanakaeea541c2011-08-17 22:59:46 +0000704 DAG.getConstant(SMSize, MVT::i32));
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000705}
Jia Liuf54f60f2012-02-28 07:46:26 +0000706
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000707static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000708 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000709 const MipsSubtarget &Subtarget) {
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000710 // Pattern match INS.
711 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
Jia Liuf54f60f2012-02-28 07:46:26 +0000712 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000713 // => ins $dst, $src, size, pos, $src1
Eric Christopher1c29a652014-07-18 22:55:25 +0000714 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasExtractInsert())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000715 return SDValue();
716
717 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
718 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
719 ConstantSDNode *CN;
720
721 // See if Op's first operand matches (and $src1 , mask0).
722 if (And0.getOpcode() != ISD::AND)
723 return SDValue();
724
725 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000726 !isShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000727 return SDValue();
728
729 // See if Op's second operand matches (and (shl $src, pos), mask1).
730 if (And1.getOpcode() != ISD::AND)
731 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000732
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000733 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000734 !isShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000735 return SDValue();
736
737 // The shift masks must have the same position and size.
738 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
739 return SDValue();
740
741 SDValue Shl = And1.getOperand(0);
742 if (Shl.getOpcode() != ISD::SHL)
743 return SDValue();
744
745 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
746 return SDValue();
747
748 unsigned Shamt = CN->getZExtValue();
749
750 // Return if the shift amount and the first bit position of mask are not the
Jia Liuf54f60f2012-02-28 07:46:26 +0000751 // same.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000752 EVT ValTy = N->getValueType(0);
753 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000754 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000755
Andrew Trickef9de2a2013-05-25 02:42:55 +0000756 return DAG.getNode(MipsISD::Ins, SDLoc(N), ValTy, Shl.getOperand(0),
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000757 DAG.getConstant(SMPos0, MVT::i32),
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000758 DAG.getConstant(SMSize0, MVT::i32), And0.getOperand(0));
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000759}
Jia Liuf54f60f2012-02-28 07:46:26 +0000760
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000761static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000762 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000763 const MipsSubtarget &Subtarget) {
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000764 // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt))
765
766 if (DCI.isBeforeLegalizeOps())
767 return SDValue();
768
769 SDValue Add = N->getOperand(1);
770
771 if (Add.getOpcode() != ISD::ADD)
772 return SDValue();
773
774 SDValue Lo = Add.getOperand(1);
775
776 if ((Lo.getOpcode() != MipsISD::Lo) ||
777 (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable))
778 return SDValue();
779
780 EVT ValTy = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000781 SDLoc DL(N);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000782
783 SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, N->getOperand(0),
784 Add.getOperand(0));
785 return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo);
786}
787
Bruno Cardoso Lopes61a61e92011-02-10 18:05:10 +0000788SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000789 const {
790 SelectionDAG &DAG = DCI.DAG;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000791 unsigned Opc = N->getOpcode();
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000792
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000793 switch (Opc) {
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000794 default: break;
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000795 case ISD::SDIVREM:
796 case ISD::UDIVREM:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000797 return performDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000798 case ISD::SELECT:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000799 return performSELECTCombine(N, DAG, DCI, Subtarget);
Vasileios Kalintirise741eb22015-03-02 12:47:32 +0000800 case MipsISD::CMovFP_F:
801 case MipsISD::CMovFP_T:
802 return performCMovFPCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000803 case ISD::AND:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000804 return performANDCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000805 case ISD::OR:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000806 return performORCombine(N, DAG, DCI, Subtarget);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000807 case ISD::ADD:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000808 return performADDCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000809 }
810
811 return SDValue();
812}
813
Akira Hatanakafabb8cf2012-09-21 23:58:31 +0000814void
815MipsTargetLowering::LowerOperationWrapper(SDNode *N,
816 SmallVectorImpl<SDValue> &Results,
817 SelectionDAG &DAG) const {
818 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
819
820 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
821 Results.push_back(Res.getValue(I));
822}
823
824void
825MipsTargetLowering::ReplaceNodeResults(SDNode *N,
826 SmallVectorImpl<SDValue> &Results,
827 SelectionDAG &DAG) const {
Akira Hatanaka9da442f2013-04-30 21:17:07 +0000828 return LowerOperationWrapper(N, Results, DAG);
Akira Hatanakafabb8cf2012-09-21 23:58:31 +0000829}
830
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000831SDValue MipsTargetLowering::
Dan Gohman21cea8a2010-04-17 15:26:15 +0000832LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000833{
Wesley Peck527da1b2010-11-23 03:31:01 +0000834 switch (Op.getOpcode())
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000835 {
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000836 case ISD::BR_JT: return lowerBR_JT(Op, DAG);
837 case ISD::BRCOND: return lowerBRCOND(Op, DAG);
838 case ISD::ConstantPool: return lowerConstantPool(Op, DAG);
839 case ISD::GlobalAddress: return lowerGlobalAddress(Op, DAG);
840 case ISD::BlockAddress: return lowerBlockAddress(Op, DAG);
841 case ISD::GlobalTLSAddress: return lowerGlobalTLSAddress(Op, DAG);
842 case ISD::JumpTable: return lowerJumpTable(Op, DAG);
843 case ISD::SELECT: return lowerSELECT(Op, DAG);
844 case ISD::SELECT_CC: return lowerSELECT_CC(Op, DAG);
845 case ISD::SETCC: return lowerSETCC(Op, DAG);
846 case ISD::VASTART: return lowerVASTART(Op, DAG);
Daniel Sanders2b553d42014-08-01 09:17:39 +0000847 case ISD::VAARG: return lowerVAARG(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000848 case ISD::FCOPYSIGN: return lowerFCOPYSIGN(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000849 case ISD::FRAMEADDR: return lowerFRAMEADDR(Op, DAG);
850 case ISD::RETURNADDR: return lowerRETURNADDR(Op, DAG);
851 case ISD::EH_RETURN: return lowerEH_RETURN(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000852 case ISD::ATOMIC_FENCE: return lowerATOMIC_FENCE(Op, DAG);
853 case ISD::SHL_PARTS: return lowerShiftLeftParts(Op, DAG);
854 case ISD::SRA_PARTS: return lowerShiftRightParts(Op, DAG, true);
855 case ISD::SRL_PARTS: return lowerShiftRightParts(Op, DAG, false);
856 case ISD::LOAD: return lowerLOAD(Op, DAG);
857 case ISD::STORE: return lowerSTORE(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000858 case ISD::ADD: return lowerADD(Op, DAG);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000859 case ISD::FP_TO_SINT: return lowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000860 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000861 return SDValue();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000862}
863
Akira Hatanakae2489122011-04-15 21:51:11 +0000864//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000865// Lower helper functions
Akira Hatanakae2489122011-04-15 21:51:11 +0000866//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000867
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000868// addLiveIn - This helper function adds the specified physical register to the
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000869// MachineFunction as a live in value. It also creates a corresponding
870// virtual register for it.
871static unsigned
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000872addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000873{
Chris Lattnera10fff52007-12-31 04:13:23 +0000874 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
875 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000876 return VReg;
877}
878
Daniel Sanders308181e2014-06-12 10:44:10 +0000879static MachineBasicBlock *insertDivByZeroTrap(MachineInstr *MI,
880 MachineBasicBlock &MBB,
881 const TargetInstrInfo &TII,
882 bool Is64Bit) {
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000883 if (NoZeroDivCheck)
884 return &MBB;
885
886 // Insert instruction "teq $divisor_reg, $zero, 7".
887 MachineBasicBlock::iterator I(MI);
888 MachineInstrBuilder MIB;
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000889 MachineOperand &Divisor = MI->getOperand(2);
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000890 MIB = BuildMI(MBB, std::next(I), MI->getDebugLoc(), TII.get(Mips::TEQ))
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000891 .addReg(Divisor.getReg(), getKillRegState(Divisor.isKill()))
892 .addReg(Mips::ZERO).addImm(7);
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000893
894 // Use the 32-bit sub-register if this is a 64-bit division.
895 if (Is64Bit)
896 MIB->getOperand(0).setSubReg(Mips::sub_32);
897
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000898 // Clear Divisor's kill flag.
899 Divisor.setIsKill(false);
Daniel Sanders308181e2014-06-12 10:44:10 +0000900
901 // We would normally delete the original instruction here but in this case
902 // we only needed to inject an additional instruction rather than replace it.
903
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000904 return &MBB;
905}
906
Akira Hatanakae4bd0542012-09-27 02:15:57 +0000907MachineBasicBlock *
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +0000908MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +0000909 MachineBasicBlock *BB) const {
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +0000910 switch (MI->getOpcode()) {
Reed Kotler97ba5f22013-02-21 04:22:38 +0000911 default:
912 llvm_unreachable("Unexpected instr type to insert");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000913 case Mips::ATOMIC_LOAD_ADD_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000914 return emitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000915 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000916 return emitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000917 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000918 return emitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000919 case Mips::ATOMIC_LOAD_ADD_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000920 return emitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000921
922 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000923 return emitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000924 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000925 return emitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000926 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000927 return emitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000928 case Mips::ATOMIC_LOAD_AND_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000929 return emitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000930
931 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000932 return emitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000933 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000934 return emitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000935 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000936 return emitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000937 case Mips::ATOMIC_LOAD_OR_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000938 return emitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000939
940 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000941 return emitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000942 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000943 return emitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000944 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000945 return emitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000946 case Mips::ATOMIC_LOAD_XOR_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000947 return emitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000948
949 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000950 return emitAtomicBinaryPartword(MI, BB, 1, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000951 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000952 return emitAtomicBinaryPartword(MI, BB, 2, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000953 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000954 return emitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000955 case Mips::ATOMIC_LOAD_NAND_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000956 return emitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000957
958 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000959 return emitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000960 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000961 return emitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000962 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000963 return emitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000964 case Mips::ATOMIC_LOAD_SUB_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000965 return emitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000966
967 case Mips::ATOMIC_SWAP_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000968 return emitAtomicBinaryPartword(MI, BB, 1, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000969 case Mips::ATOMIC_SWAP_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000970 return emitAtomicBinaryPartword(MI, BB, 2, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000971 case Mips::ATOMIC_SWAP_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000972 return emitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000973 case Mips::ATOMIC_SWAP_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000974 return emitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000975
976 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000977 return emitAtomicCmpSwapPartword(MI, BB, 1);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000978 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000979 return emitAtomicCmpSwapPartword(MI, BB, 2);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000980 case Mips::ATOMIC_CMP_SWAP_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000981 return emitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000982 case Mips::ATOMIC_CMP_SWAP_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000983 return emitAtomicCmpSwap(MI, BB, 8);
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000984 case Mips::PseudoSDIV:
985 case Mips::PseudoUDIV:
Daniel Sanders308181e2014-06-12 10:44:10 +0000986 case Mips::DIV:
987 case Mips::DIVU:
988 case Mips::MOD:
989 case Mips::MODU:
Eric Christopher96e72c62015-01-29 23:27:36 +0000990 return insertDivByZeroTrap(MI, *BB, *Subtarget.getInstrInfo(), false);
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000991 case Mips::PseudoDSDIV:
992 case Mips::PseudoDUDIV:
Daniel Sanders308181e2014-06-12 10:44:10 +0000993 case Mips::DDIV:
994 case Mips::DDIVU:
995 case Mips::DMOD:
996 case Mips::DMODU:
Eric Christopher96e72c62015-01-29 23:27:36 +0000997 return insertDivByZeroTrap(MI, *BB, *Subtarget.getInstrInfo(), true);
Daniel Sanders0fa60412014-06-12 13:39:06 +0000998 case Mips::SEL_D:
999 return emitSEL_D(MI, BB);
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001000
1001 case Mips::PseudoSELECT_I:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001002 case Mips::PseudoSELECT_I64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001003 case Mips::PseudoSELECT_S:
1004 case Mips::PseudoSELECT_D32:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001005 case Mips::PseudoSELECT_D64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001006 return emitPseudoSELECT(MI, BB, false, Mips::BNE);
1007 case Mips::PseudoSELECTFP_F_I:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001008 case Mips::PseudoSELECTFP_F_I64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001009 case Mips::PseudoSELECTFP_F_S:
1010 case Mips::PseudoSELECTFP_F_D32:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001011 case Mips::PseudoSELECTFP_F_D64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001012 return emitPseudoSELECT(MI, BB, true, Mips::BC1F);
1013 case Mips::PseudoSELECTFP_T_I:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001014 case Mips::PseudoSELECTFP_T_I64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001015 case Mips::PseudoSELECTFP_T_S:
1016 case Mips::PseudoSELECTFP_T_D32:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +00001017 case Mips::PseudoSELECTFP_T_D64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00001018 return emitPseudoSELECT(MI, BB, true, Mips::BC1T);
Akira Hatanakaa5352702011-03-31 18:26:17 +00001019 }
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001020}
1021
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001022// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
1023// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
1024MachineBasicBlock *
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001025MipsTargetLowering::emitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher0713a9d2011-06-08 23:55:35 +00001026 unsigned Size, unsigned BinOpcode,
Akira Hatanaka15506782011-06-07 18:58:42 +00001027 bool Nand) const {
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001028 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001029
1030 MachineFunction *MF = BB->getParent();
1031 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001032 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Eric Christopher96e72c62015-01-29 23:27:36 +00001033 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001034 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001035 unsigned LL, SC, AND, NOR, ZERO, BEQ;
1036
1037 if (Size == 4) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001038 if (isMicroMips) {
1039 LL = Mips::LL_MM;
1040 SC = Mips::SC_MM;
1041 } else {
Daniel Sandersbdcfab12014-07-24 09:47:14 +00001042 LL = Subtarget.hasMips32r6() ? Mips::LL_R6 : Mips::LL;
1043 SC = Subtarget.hasMips32r6() ? Mips::SC_R6 : Mips::SC;
Daniel Sanders6a803f62014-06-16 13:13:03 +00001044 }
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001045 AND = Mips::AND;
1046 NOR = Mips::NOR;
1047 ZERO = Mips::ZERO;
1048 BEQ = Mips::BEQ;
Daniel Sanders6a803f62014-06-16 13:13:03 +00001049 } else {
Daniel Sandersbdcfab12014-07-24 09:47:14 +00001050 LL = Subtarget.hasMips64r6() ? Mips::LLD_R6 : Mips::LLD;
1051 SC = Subtarget.hasMips64r6() ? Mips::SCD_R6 : Mips::SCD;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001052 AND = Mips::AND64;
1053 NOR = Mips::NOR64;
1054 ZERO = Mips::ZERO_64;
1055 BEQ = Mips::BEQ64;
1056 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001057
Akira Hatanaka0e019592011-07-19 20:11:17 +00001058 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001059 unsigned Ptr = MI->getOperand(1).getReg();
1060 unsigned Incr = MI->getOperand(2).getReg();
1061
Akira Hatanaka0e019592011-07-19 20:11:17 +00001062 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1063 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1064 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001065
1066 // insert new blocks after the current block
1067 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1068 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1069 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1070 MachineFunction::iterator It = BB;
1071 ++It;
1072 MF->insert(It, loopMBB);
1073 MF->insert(It, exitMBB);
1074
1075 // Transfer the remainder of BB and its successor edges to exitMBB.
1076 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001077 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001078 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1079
1080 // thisMBB:
1081 // ...
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001082 // fallthrough --> loopMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001083 BB->addSuccessor(loopMBB);
Akira Hatanaka08636b42011-07-19 17:09:53 +00001084 loopMBB->addSuccessor(loopMBB);
1085 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001086
1087 // loopMBB:
1088 // ll oldval, 0(ptr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001089 // <binop> storeval, oldval, incr
1090 // sc success, storeval, 0(ptr)
1091 // beq success, $0, loopMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001092 BB = loopMBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001093 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001094 if (Nand) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001095 // and andres, oldval, incr
1096 // nor storeval, $0, andres
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001097 BuildMI(BB, DL, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
1098 BuildMI(BB, DL, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001099 } else if (BinOpcode) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001100 // <binop> storeval, oldval, incr
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001101 BuildMI(BB, DL, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001102 } else {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001103 StoreVal = Incr;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001104 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001105 BuildMI(BB, DL, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
1106 BuildMI(BB, DL, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001107
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001108 MI->eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001109
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001110 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001111}
1112
Daniel Sanders6a803f62014-06-16 13:13:03 +00001113MachineBasicBlock *MipsTargetLowering::emitSignExtendToI32InReg(
1114 MachineInstr *MI, MachineBasicBlock *BB, unsigned Size, unsigned DstReg,
1115 unsigned SrcReg) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00001116 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders6a803f62014-06-16 13:13:03 +00001117 DebugLoc DL = MI->getDebugLoc();
1118
Eric Christopher1c29a652014-07-18 22:55:25 +00001119 if (Subtarget.hasMips32r2() && Size == 1) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001120 BuildMI(BB, DL, TII->get(Mips::SEB), DstReg).addReg(SrcReg);
1121 return BB;
1122 }
1123
Eric Christopher1c29a652014-07-18 22:55:25 +00001124 if (Subtarget.hasMips32r2() && Size == 2) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001125 BuildMI(BB, DL, TII->get(Mips::SEH), DstReg).addReg(SrcReg);
1126 return BB;
1127 }
1128
1129 MachineFunction *MF = BB->getParent();
1130 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1131 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1132 unsigned ScrReg = RegInfo.createVirtualRegister(RC);
1133
1134 assert(Size < 32);
1135 int64_t ShiftImm = 32 - (Size * 8);
1136
1137 BuildMI(BB, DL, TII->get(Mips::SLL), ScrReg).addReg(SrcReg).addImm(ShiftImm);
1138 BuildMI(BB, DL, TII->get(Mips::SRA), DstReg).addReg(ScrReg).addImm(ShiftImm);
1139
1140 return BB;
1141}
1142
1143MachineBasicBlock *MipsTargetLowering::emitAtomicBinaryPartword(
1144 MachineInstr *MI, MachineBasicBlock *BB, unsigned Size, unsigned BinOpcode,
1145 bool Nand) const {
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001146 assert((Size == 1 || Size == 2) &&
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001147 "Unsupported size for EmitAtomicBinaryPartial.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001148
1149 MachineFunction *MF = BB->getParent();
1150 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1151 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
Eric Christopher96e72c62015-01-29 23:27:36 +00001152 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001153 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001154
1155 unsigned Dest = MI->getOperand(0).getReg();
1156 unsigned Ptr = MI->getOperand(1).getReg();
1157 unsigned Incr = MI->getOperand(2).getReg();
1158
Akira Hatanaka0e019592011-07-19 20:11:17 +00001159 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1160 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001161 unsigned Mask = RegInfo.createVirtualRegister(RC);
1162 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001163 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1164 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001165 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001166 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1167 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1168 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1169 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1170 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka9663dd32011-07-19 20:56:53 +00001171 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001172 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1173 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1174 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001175 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001176
1177 // insert new blocks after the current block
1178 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1179 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001180 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001181 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1182 MachineFunction::iterator It = BB;
1183 ++It;
1184 MF->insert(It, loopMBB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001185 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001186 MF->insert(It, exitMBB);
1187
1188 // Transfer the remainder of BB and its successor edges to exitMBB.
1189 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001190 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001191 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1192
Akira Hatanaka08636b42011-07-19 17:09:53 +00001193 BB->addSuccessor(loopMBB);
1194 loopMBB->addSuccessor(loopMBB);
1195 loopMBB->addSuccessor(sinkMBB);
1196 sinkMBB->addSuccessor(exitMBB);
1197
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001198 // thisMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001199 // addiu masklsb2,$0,-4 # 0xfffffffc
1200 // and alignedaddr,ptr,masklsb2
1201 // andi ptrlsb2,ptr,3
1202 // sll shiftamt,ptrlsb2,3
1203 // ori maskupper,$0,255 # 0xff
1204 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001205 // nor mask2,$0,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001206 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001207
1208 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001209 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001210 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001211 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001212 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001213 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Eric Christopher1c29a652014-07-18 22:55:25 +00001214 if (Subtarget.isLittle()) {
Akira Hatanaka2bf97332013-05-31 03:25:44 +00001215 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1216 } else {
1217 unsigned Off = RegInfo.createVirtualRegister(RC);
1218 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1219 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1220 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1221 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001222 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001223 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001224 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001225 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001226 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001227 BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(Incr).addReg(ShiftAmt);
Bruno Cardoso Lopesf771a0f2011-05-31 20:25:26 +00001228
Akira Hatanaka27292632011-07-18 18:52:12 +00001229 // atomic.load.binop
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001230 // loopMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001231 // ll oldval,0(alignedaddr)
1232 // binop binopres,oldval,incr2
1233 // and newval,binopres,mask
1234 // and maskedoldval0,oldval,mask2
1235 // or storeval,maskedoldval0,newval
1236 // sc success,storeval,0(alignedaddr)
1237 // beq success,$0,loopMBB
1238
Akira Hatanaka27292632011-07-18 18:52:12 +00001239 // atomic.swap
1240 // loopMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001241 // ll oldval,0(alignedaddr)
Akira Hatanakae4503582011-07-19 18:14:26 +00001242 // and newval,incr2,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001243 // and maskedoldval0,oldval,mask2
1244 // or storeval,maskedoldval0,newval
1245 // sc success,storeval,0(alignedaddr)
1246 // beq success,$0,loopMBB
Akira Hatanaka27292632011-07-18 18:52:12 +00001247
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001248 BB = loopMBB;
Jozef Kolek2f27d572014-12-18 16:39:29 +00001249 unsigned LL = isMicroMips ? Mips::LL_MM : Mips::LL;
1250 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001251 if (Nand) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001252 // and andres, oldval, incr2
1253 // nor binopres, $0, andres
1254 // and newval, binopres, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001255 BuildMI(BB, DL, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1256 BuildMI(BB, DL, TII->get(Mips::NOR), BinOpRes)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001257 .addReg(Mips::ZERO).addReg(AndRes);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001258 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001259 } else if (BinOpcode) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001260 // <binop> binopres, oldval, incr2
1261 // and newval, binopres, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001262 BuildMI(BB, DL, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1263 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001264 } else { // atomic.swap
Akira Hatanaka0e019592011-07-19 20:11:17 +00001265 // and newval, incr2, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001266 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanakae4503582011-07-19 18:14:26 +00001267 }
Jia Liuf54f60f2012-02-28 07:46:26 +00001268
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001269 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001270 .addReg(OldVal).addReg(Mask2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001271 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka9663dd32011-07-19 20:56:53 +00001272 .addReg(MaskedOldVal0).addReg(NewVal);
Jozef Kolek2f27d572014-12-18 16:39:29 +00001273 unsigned SC = isMicroMips ? Mips::SC_MM : Mips::SC;
1274 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001275 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001276 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001277 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001278
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001279 // sinkMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001280 // and maskedoldval1,oldval,mask
1281 // srl srlres,maskedoldval1,shiftamt
Daniel Sanders6a803f62014-06-16 13:13:03 +00001282 // sign_extend dest,srlres
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001283 BB = sinkMBB;
Akira Hatanakae97bd812011-07-19 03:14:58 +00001284
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001285 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001286 .addReg(OldVal).addReg(Mask);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001287 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001288 .addReg(MaskedOldVal1).addReg(ShiftAmt);
Daniel Sanders6a803f62014-06-16 13:13:03 +00001289 BB = emitSignExtendToI32InReg(MI, BB, Size, Dest, SrlRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001290
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001291 MI->eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001292
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001293 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001294}
1295
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001296MachineBasicBlock * MipsTargetLowering::emitAtomicCmpSwap(MachineInstr *MI,
1297 MachineBasicBlock *BB,
1298 unsigned Size) const {
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001299 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001300
1301 MachineFunction *MF = BB->getParent();
1302 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001303 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Eric Christopher96e72c62015-01-29 23:27:36 +00001304 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001305 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001306 unsigned LL, SC, ZERO, BNE, BEQ;
1307
1308 if (Size == 4) {
Zoran Jovanovicff9d5f32013-12-19 16:12:56 +00001309 LL = isMicroMips ? Mips::LL_MM : Mips::LL;
1310 SC = isMicroMips ? Mips::SC_MM : Mips::SC;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001311 ZERO = Mips::ZERO;
1312 BNE = Mips::BNE;
1313 BEQ = Mips::BEQ;
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001314 } else {
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001315 LL = Mips::LLD;
1316 SC = Mips::SCD;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001317 ZERO = Mips::ZERO_64;
1318 BNE = Mips::BNE64;
1319 BEQ = Mips::BEQ64;
1320 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001321
1322 unsigned Dest = MI->getOperand(0).getReg();
1323 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka0e019592011-07-19 20:11:17 +00001324 unsigned OldVal = MI->getOperand(2).getReg();
1325 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001326
Akira Hatanaka0e019592011-07-19 20:11:17 +00001327 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001328
1329 // insert new blocks after the current block
1330 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1331 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1332 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1333 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1334 MachineFunction::iterator It = BB;
1335 ++It;
1336 MF->insert(It, loop1MBB);
1337 MF->insert(It, loop2MBB);
1338 MF->insert(It, exitMBB);
1339
1340 // Transfer the remainder of BB and its successor edges to exitMBB.
1341 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001342 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001343 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1344
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001345 // thisMBB:
1346 // ...
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001347 // fallthrough --> loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001348 BB->addSuccessor(loop1MBB);
Akira Hatanaka08636b42011-07-19 17:09:53 +00001349 loop1MBB->addSuccessor(exitMBB);
1350 loop1MBB->addSuccessor(loop2MBB);
1351 loop2MBB->addSuccessor(loop1MBB);
1352 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001353
1354 // loop1MBB:
1355 // ll dest, 0(ptr)
1356 // bne dest, oldval, exitMBB
1357 BB = loop1MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001358 BuildMI(BB, DL, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1359 BuildMI(BB, DL, TII->get(BNE))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001360 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001361
1362 // loop2MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001363 // sc success, newval, 0(ptr)
1364 // beq success, $0, loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001365 BB = loop2MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001366 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001367 .addReg(NewVal).addReg(Ptr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001368 BuildMI(BB, DL, TII->get(BEQ))
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001369 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001370
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001371 MI->eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001372
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001373 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001374}
1375
1376MachineBasicBlock *
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001377MipsTargetLowering::emitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka15506782011-06-07 18:58:42 +00001378 MachineBasicBlock *BB,
1379 unsigned Size) const {
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001380 assert((Size == 1 || Size == 2) &&
1381 "Unsupported size for EmitAtomicCmpSwapPartial.");
1382
1383 MachineFunction *MF = BB->getParent();
1384 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1385 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
Eric Christopher96e72c62015-01-29 23:27:36 +00001386 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001387 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001388
1389 unsigned Dest = MI->getOperand(0).getReg();
1390 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka0e019592011-07-19 20:11:17 +00001391 unsigned CmpVal = MI->getOperand(2).getReg();
1392 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001393
Akira Hatanaka0e019592011-07-19 20:11:17 +00001394 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1395 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001396 unsigned Mask = RegInfo.createVirtualRegister(RC);
1397 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001398 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1399 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1400 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1401 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1402 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1403 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1404 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1405 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1406 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1407 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1408 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1409 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001410 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001411
1412 // insert new blocks after the current block
1413 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1414 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1415 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001416 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001417 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1418 MachineFunction::iterator It = BB;
1419 ++It;
1420 MF->insert(It, loop1MBB);
1421 MF->insert(It, loop2MBB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001422 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001423 MF->insert(It, exitMBB);
1424
1425 // Transfer the remainder of BB and its successor edges to exitMBB.
1426 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001427 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001428 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1429
Akira Hatanaka08636b42011-07-19 17:09:53 +00001430 BB->addSuccessor(loop1MBB);
1431 loop1MBB->addSuccessor(sinkMBB);
1432 loop1MBB->addSuccessor(loop2MBB);
1433 loop2MBB->addSuccessor(loop1MBB);
1434 loop2MBB->addSuccessor(sinkMBB);
1435 sinkMBB->addSuccessor(exitMBB);
1436
Akira Hatanakae4503582011-07-19 18:14:26 +00001437 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001438 // thisMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001439 // addiu masklsb2,$0,-4 # 0xfffffffc
1440 // and alignedaddr,ptr,masklsb2
1441 // andi ptrlsb2,ptr,3
1442 // sll shiftamt,ptrlsb2,3
1443 // ori maskupper,$0,255 # 0xff
1444 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001445 // nor mask2,$0,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001446 // andi maskedcmpval,cmpval,255
1447 // sll shiftedcmpval,maskedcmpval,shiftamt
1448 // andi maskednewval,newval,255
1449 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001450 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001451 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001452 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001453 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001454 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001455 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Eric Christopher1c29a652014-07-18 22:55:25 +00001456 if (Subtarget.isLittle()) {
Akira Hatanaka2bf97332013-05-31 03:25:44 +00001457 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1458 } else {
1459 unsigned Off = RegInfo.createVirtualRegister(RC);
1460 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1461 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1462 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1463 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001464 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001465 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001466 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001467 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001468 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1469 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001470 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001471 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001472 .addReg(MaskedCmpVal).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001473 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001474 .addReg(NewVal).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001475 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001476 .addReg(MaskedNewVal).addReg(ShiftAmt);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001477
1478 // loop1MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001479 // ll oldval,0(alginedaddr)
1480 // and maskedoldval0,oldval,mask
1481 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001482 BB = loop1MBB;
Jozef Kolek2f27d572014-12-18 16:39:29 +00001483 unsigned LL = isMicroMips ? Mips::LL_MM : Mips::LL;
1484 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001485 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001486 .addReg(OldVal).addReg(Mask);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001487 BuildMI(BB, DL, TII->get(Mips::BNE))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001488 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001489
1490 // loop2MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001491 // and maskedoldval1,oldval,mask2
1492 // or storeval,maskedoldval1,shiftednewval
1493 // sc success,storeval,0(alignedaddr)
1494 // beq success,$0,loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001495 BB = loop2MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001496 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001497 .addReg(OldVal).addReg(Mask2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001498 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001499 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
Jozef Kolek2f27d572014-12-18 16:39:29 +00001500 unsigned SC = isMicroMips ? Mips::SC_MM : Mips::SC;
1501 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001502 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001503 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001504 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001505
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001506 // sinkMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001507 // srl srlres,maskedoldval0,shiftamt
Daniel Sanders6a803f62014-06-16 13:13:03 +00001508 // sign_extend dest,srlres
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001509 BB = sinkMBB;
Akira Hatanakae97bd812011-07-19 03:14:58 +00001510
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001511 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001512 .addReg(MaskedOldVal0).addReg(ShiftAmt);
Daniel Sanders6a803f62014-06-16 13:13:03 +00001513 BB = emitSignExtendToI32InReg(MI, BB, Size, Dest, SrlRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001514
1515 MI->eraseFromParent(); // The instruction is gone now.
1516
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001517 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001518}
1519
Daniel Sanders0fa60412014-06-12 13:39:06 +00001520MachineBasicBlock *MipsTargetLowering::emitSEL_D(MachineInstr *MI,
1521 MachineBasicBlock *BB) const {
1522 MachineFunction *MF = BB->getParent();
Eric Christopher96e72c62015-01-29 23:27:36 +00001523 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
1524 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders0fa60412014-06-12 13:39:06 +00001525 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1526 DebugLoc DL = MI->getDebugLoc();
1527 MachineBasicBlock::iterator II(MI);
1528
1529 unsigned Fc = MI->getOperand(1).getReg();
1530 const auto &FGR64RegClass = TRI->getRegClass(Mips::FGR64RegClassID);
1531
1532 unsigned Fc2 = RegInfo.createVirtualRegister(FGR64RegClass);
1533
1534 BuildMI(*BB, II, DL, TII->get(Mips::SUBREG_TO_REG), Fc2)
1535 .addImm(0)
1536 .addReg(Fc)
1537 .addImm(Mips::sub_lo);
1538
1539 // We don't erase the original instruction, we just replace the condition
1540 // register with the 64-bit super-register.
1541 MI->getOperand(1).setReg(Fc2);
1542
1543 return BB;
1544}
1545
Akira Hatanakae2489122011-04-15 21:51:11 +00001546//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001547// Misc Lower Operation implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00001548//===----------------------------------------------------------------------===//
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001549SDValue MipsTargetLowering::lowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001550 SDValue Chain = Op.getOperand(0);
1551 SDValue Table = Op.getOperand(1);
1552 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001553 SDLoc DL(Op);
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001554 EVT PTy = getPointerTy();
1555 unsigned EntrySize =
1556 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(*getDataLayout());
1557
1558 Index = DAG.getNode(ISD::MUL, DL, PTy, Index,
1559 DAG.getConstant(EntrySize, PTy));
1560 SDValue Addr = DAG.getNode(ISD::ADD, DL, PTy, Index, Table);
1561
1562 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
1563 Addr = DAG.getExtLoad(ISD::SEXTLOAD, DL, PTy, Chain, Addr,
1564 MachinePointerInfo::getJumpTable(), MemVT, false, false,
Louis Gerbarg67474e32014-07-31 21:45:05 +00001565 false, 0);
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001566 Chain = Addr.getValue(1);
1567
Eric Christopher96e72c62015-01-29 23:27:36 +00001568 if ((getTargetMachine().getRelocationModel() == Reloc::PIC_) || ABI.IsN64()) {
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001569 // For PIC, the sequence is:
1570 // BRIND(load(Jumptable + index) + RelocBase)
1571 // RelocBase can be JumpTable, GOT or some sort of global base.
1572 Addr = DAG.getNode(ISD::ADD, DL, PTy, Addr,
1573 getPICJumpTableRelocBase(Table, DAG));
1574 }
1575
1576 return DAG.getNode(ISD::BRIND, DL, MVT::Other, Chain, Addr);
1577}
1578
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001579SDValue MipsTargetLowering::lowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Wesley Peck527da1b2010-11-23 03:31:01 +00001580 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001581 // the block to branch to if the condition is true.
1582 SDValue Chain = Op.getOperand(0);
1583 SDValue Dest = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001584 SDLoc DL(Op);
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001585
Eric Christopher1c29a652014-07-18 22:55:25 +00001586 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001587 SDValue CondRes = createFPCmp(DAG, Op.getOperand(1));
Akira Hatanakaa5352702011-03-31 18:26:17 +00001588
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001589 // Return if flag is not set by a floating point comparison.
Akira Hatanakaa5352702011-03-31 18:26:17 +00001590 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopesa9504222008-07-30 17:06:13 +00001591 return Op;
Wesley Peck527da1b2010-11-23 03:31:01 +00001592
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +00001593 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmaneffb8942008-09-12 16:56:44 +00001594 Mips::CondCode CC =
1595 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Akira Hatanakaf0ea5002013-03-30 01:16:38 +00001596 unsigned Opc = invertFPCondCodeUser(CC) ? Mips::BRANCH_F : Mips::BRANCH_T;
1597 SDValue BrCode = DAG.getConstant(Opc, MVT::i32);
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +00001598 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001599 return DAG.getNode(MipsISD::FPBrcond, DL, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +00001600 FCC0, Dest, CondRes);
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001601}
1602
1603SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001604lowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001605{
Eric Christopher1c29a652014-07-18 22:55:25 +00001606 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001607 SDValue Cond = createFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001608
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001609 // Return if flag is not set by a floating point comparison.
Akira Hatanakaa5352702011-03-31 18:26:17 +00001610 if (Cond.getOpcode() != MipsISD::FPCmp)
1611 return Op;
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +00001612
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001613 return createCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
Andrew Trickef9de2a2013-05-25 02:42:55 +00001614 SDLoc(Op));
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001615}
1616
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001617SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001618lowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001619{
Andrew Trickef9de2a2013-05-25 02:42:55 +00001620 SDLoc DL(Op);
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001621 EVT Ty = Op.getOperand(0).getValueType();
Matt Arsenault758659232013-05-18 00:21:46 +00001622 SDValue Cond = DAG.getNode(ISD::SETCC, DL,
1623 getSetCCResultType(*DAG.getContext(), Ty),
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001624 Op.getOperand(0), Op.getOperand(1),
1625 Op.getOperand(4));
1626
1627 return DAG.getNode(ISD::SELECT, DL, Op.getValueType(), Cond, Op.getOperand(2),
1628 Op.getOperand(3));
1629}
1630
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001631SDValue MipsTargetLowering::lowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00001632 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001633 SDValue Cond = createFPCmp(DAG, Op);
Akira Hatanakab7f78592012-03-09 23:46:03 +00001634
1635 assert(Cond.getOpcode() == MipsISD::FPCmp &&
1636 "Floating point operand expected.");
1637
1638 SDValue True = DAG.getConstant(1, MVT::i32);
1639 SDValue False = DAG.getConstant(0, MVT::i32);
1640
Andrew Trickef9de2a2013-05-25 02:42:55 +00001641 return createCMovFP(DAG, Cond, True, False, SDLoc(Op));
Akira Hatanakab7f78592012-03-09 23:46:03 +00001642}
1643
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001644SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001645 SelectionDAG &DAG) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001646 EVT Ty = Op.getValueType();
1647 GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
1648 const GlobalValue *GV = N->getGlobal();
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001649
Eric Christopher96e72c62015-01-29 23:27:36 +00001650 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !ABI.IsN64()) {
Eric Christopher36fe0282015-02-03 07:22:52 +00001651 const MipsTargetObjectFile *TLOF =
1652 static_cast<const MipsTargetObjectFile *>(
1653 getTargetMachine().getObjFileLowering());
1654 if (TLOF->IsGlobalInSmallSection(GV, getTargetMachine()))
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001655 // %gp_rel relocation
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001656 return getAddrGPRel(N, SDLoc(N), Ty, DAG);
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001657
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001658 // %hi/%lo relocation
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001659 return getAddrNonPIC(N, SDLoc(N), Ty, DAG);
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001660 }
1661
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001662 if (GV->hasInternalLinkage() || (GV->hasLocalLinkage() && !isa<Function>(GV)))
Eric Christopher96e72c62015-01-29 23:27:36 +00001663 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001664
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00001665 if (LargeGOT)
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001666 return getAddrGlobalLargeGOT(N, SDLoc(N), Ty, DAG, MipsII::MO_GOT_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00001667 MipsII::MO_GOT_LO16, DAG.getEntryNode(),
1668 MachinePointerInfo::getGOT());
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00001669
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001670 return getAddrGlobal(N, SDLoc(N), Ty, DAG,
Eric Christopher96e72c62015-01-29 23:27:36 +00001671 (ABI.IsN32() || ABI.IsN64()) ? MipsII::MO_GOT_DISP
1672 : MipsII::MO_GOT16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00001673 DAG.getEntryNode(), MachinePointerInfo::getGOT());
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001674}
1675
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001676SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op,
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +00001677 SelectionDAG &DAG) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001678 BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op);
1679 EVT Ty = Op.getValueType();
Akira Hatanaka30f97cf2013-09-25 00:30:25 +00001680
Eric Christopher96e72c62015-01-29 23:27:36 +00001681 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !ABI.IsN64())
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001682 return getAddrNonPIC(N, SDLoc(N), Ty, DAG);
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001683
Eric Christopher96e72c62015-01-29 23:27:36 +00001684 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +00001685}
1686
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001687SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001688lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001689{
Akira Hatanakabff84e12011-12-14 18:26:41 +00001690 // If the relocation model is PIC, use the General Dynamic TLS Model or
1691 // Local Dynamic TLS model, otherwise use the Initial Exec or
1692 // Local Exec TLS Model.
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001693
1694 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001695 SDLoc DL(GA);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001696 const GlobalValue *GV = GA->getGlobal();
1697 EVT PtrVT = getPointerTy();
1698
Hans Wennborgaea41202012-05-04 09:40:39 +00001699 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
1700
1701 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Hans Wennborg245917b2012-06-04 14:02:08 +00001702 // General Dynamic and Local Dynamic TLS Model.
1703 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
1704 : MipsII::MO_TLSGD;
1705
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001706 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag);
1707 SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT,
1708 getGlobalReg(DAG, PtrVT), TGA);
Akira Hatanakaf10ee842011-12-08 21:05:38 +00001709 unsigned PtrSize = PtrVT.getSizeInBits();
1710 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1711
Benjamin Kramer64ba50a2011-12-11 12:21:34 +00001712 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001713
1714 ArgListTy Args;
1715 ArgListEntry Entry;
1716 Entry.Node = Argument;
Akira Hatanakadee6c822011-12-08 20:34:32 +00001717 Entry.Ty = PtrTy;
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001718 Args.push_back(Entry);
Jia Liuf54f60f2012-02-28 07:46:26 +00001719
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00001720 TargetLowering::CallLoweringInfo CLI(DAG);
1721 CLI.setDebugLoc(DL).setChain(DAG.getEntryNode())
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00001722 .setCallee(CallingConv::C, PtrTy, TlsGetAddr, std::move(Args), 0);
Justin Holewinskiaa583972012-05-25 16:35:28 +00001723 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001724
Akira Hatanakabff84e12011-12-14 18:26:41 +00001725 SDValue Ret = CallResult.first;
1726
Hans Wennborgaea41202012-05-04 09:40:39 +00001727 if (model != TLSModel::LocalDynamic)
Akira Hatanakabff84e12011-12-14 18:26:41 +00001728 return Ret;
1729
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001730 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanakabff84e12011-12-14 18:26:41 +00001731 MipsII::MO_DTPREL_HI);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001732 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1733 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanakabff84e12011-12-14 18:26:41 +00001734 MipsII::MO_DTPREL_LO);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001735 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1736 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret);
1737 return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001738 }
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001739
1740 SDValue Offset;
Hans Wennborgaea41202012-05-04 09:40:39 +00001741 if (model == TLSModel::InitialExec) {
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001742 // Initial Exec TLS Model
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001743 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001744 MipsII::MO_GOTTPREL);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001745 TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT),
Akira Hatanakab049aef2012-02-24 22:34:47 +00001746 TGA);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001747 Offset = DAG.getLoad(PtrVT, DL,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001748 DAG.getEntryNode(), TGA, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001749 false, false, false, 0);
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001750 } else {
1751 // Local Exec TLS Model
Hans Wennborgaea41202012-05-04 09:40:39 +00001752 assert(model == TLSModel::LocalExec);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001753 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001754 MipsII::MO_TPREL_HI);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001755 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001756 MipsII::MO_TPREL_LO);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001757 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1758 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1759 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001760 }
1761
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001762 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
1763 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001764}
1765
1766SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001767lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +00001768{
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001769 JumpTableSDNode *N = cast<JumpTableSDNode>(Op);
1770 EVT Ty = Op.getValueType();
Akira Hatanaka30f97cf2013-09-25 00:30:25 +00001771
Eric Christopher96e72c62015-01-29 23:27:36 +00001772 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !ABI.IsN64())
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001773 return getAddrNonPIC(N, SDLoc(N), Ty, DAG);
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001774
Eric Christopher96e72c62015-01-29 23:27:36 +00001775 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +00001776}
1777
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001778SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001779lowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +00001780{
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001781 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
1782 EVT Ty = Op.getValueType();
Bruno Cardoso Lopes2db07582009-11-25 12:17:58 +00001783
Eric Christopher96e72c62015-01-29 23:27:36 +00001784 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !ABI.IsN64()) {
Eric Christopher36fe0282015-02-03 07:22:52 +00001785 const MipsTargetObjectFile *TLOF =
1786 static_cast<const MipsTargetObjectFile *>(
1787 getTargetMachine().getObjFileLowering());
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001788
Eric Christopher36fe0282015-02-03 07:22:52 +00001789 if (TLOF->IsConstantInSmallSection(N->getConstVal(), getTargetMachine()))
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001790 // %gp_rel relocation
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001791 return getAddrGPRel(N, SDLoc(N), Ty, DAG);
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001792
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001793 return getAddrNonPIC(N, SDLoc(N), Ty, DAG);
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001794 }
Bruno Cardoso Lopesfdb4cec2008-07-23 16:01:50 +00001795
Eric Christopher96e72c62015-01-29 23:27:36 +00001796 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +00001797}
1798
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001799SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman31ae5862010-04-17 14:41:14 +00001800 MachineFunction &MF = DAG.getMachineFunction();
1801 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1802
Andrew Trickef9de2a2013-05-25 02:42:55 +00001803 SDLoc DL(Op);
Dan Gohman31ae5862010-04-17 14:41:14 +00001804 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1805 getPointerTy());
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +00001806
1807 // vastart just stores the address of the VarArgsFrameIndex slot into the
1808 // memory location argument.
1809 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001810 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00001811 MachinePointerInfo(SV), false, false, 0);
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +00001812}
Jia Liuf54f60f2012-02-28 07:46:26 +00001813
Daniel Sanders2b553d42014-08-01 09:17:39 +00001814SDValue MipsTargetLowering::lowerVAARG(SDValue Op, SelectionDAG &DAG) const {
1815 SDNode *Node = Op.getNode();
1816 EVT VT = Node->getValueType(0);
1817 SDValue Chain = Node->getOperand(0);
1818 SDValue VAListPtr = Node->getOperand(1);
1819 unsigned Align = Node->getConstantOperandVal(3);
1820 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
1821 SDLoc DL(Node);
Eric Christopher96e72c62015-01-29 23:27:36 +00001822 unsigned ArgSlotSizeInBytes = (ABI.IsN32() || ABI.IsN64()) ? 8 : 4;
Daniel Sanders2b553d42014-08-01 09:17:39 +00001823
1824 SDValue VAListLoad = DAG.getLoad(getPointerTy(), DL, Chain, VAListPtr,
1825 MachinePointerInfo(SV), false, false, false,
1826 0);
1827 SDValue VAList = VAListLoad;
1828
1829 // Re-align the pointer if necessary.
1830 // It should only ever be necessary for 64-bit types on O32 since the minimum
1831 // argument alignment is the same as the maximum type alignment for N32/N64.
1832 //
1833 // FIXME: We currently align too often. The code generator doesn't notice
1834 // when the pointer is still aligned from the last va_arg (or pair of
1835 // va_args for the i64 on O32 case).
1836 if (Align > getMinStackArgumentAlignment()) {
1837 assert(((Align & (Align-1)) == 0) && "Expected Align to be a power of 2");
1838
1839 VAList = DAG.getNode(ISD::ADD, DL, VAList.getValueType(), VAList,
1840 DAG.getConstant(Align - 1,
1841 VAList.getValueType()));
1842
1843 VAList = DAG.getNode(ISD::AND, DL, VAList.getValueType(), VAList,
1844 DAG.getConstant(-(int64_t)Align,
1845 VAList.getValueType()));
1846 }
1847
1848 // Increment the pointer, VAList, to the next vaarg.
1849 unsigned ArgSizeInBytes = getDataLayout()->getTypeAllocSize(VT.getTypeForEVT(*DAG.getContext()));
1850 SDValue Tmp3 = DAG.getNode(ISD::ADD, DL, VAList.getValueType(), VAList,
1851 DAG.getConstant(RoundUpToAlignment(ArgSizeInBytes, ArgSlotSizeInBytes),
1852 VAList.getValueType()));
1853 // Store the incremented VAList to the legalized pointer
1854 Chain = DAG.getStore(VAListLoad.getValue(1), DL, Tmp3, VAListPtr,
1855 MachinePointerInfo(SV), false, false, 0);
1856
1857 // In big-endian mode we must adjust the pointer when the load size is smaller
1858 // than the argument slot size. We must also reduce the known alignment to
1859 // match. For example in the N64 ABI, we must add 4 bytes to the offset to get
1860 // the correct half of the slot, and reduce the alignment from 8 (slot
1861 // alignment) down to 4 (type alignment).
1862 if (!Subtarget.isLittle() && ArgSizeInBytes < ArgSlotSizeInBytes) {
1863 unsigned Adjustment = ArgSlotSizeInBytes - ArgSizeInBytes;
1864 VAList = DAG.getNode(ISD::ADD, DL, VAListPtr.getValueType(), VAList,
1865 DAG.getIntPtrConstant(Adjustment));
1866 }
1867 // Load the actual argument out of the pointer VAList
1868 return DAG.getLoad(VT, DL, Chain, VAList, MachinePointerInfo(), false, false,
1869 false, 0);
1870}
1871
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001872static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG,
1873 bool HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001874 EVT TyX = Op.getOperand(0).getValueType();
1875 EVT TyY = Op.getOperand(1).getValueType();
1876 SDValue Const1 = DAG.getConstant(1, MVT::i32);
1877 SDValue Const31 = DAG.getConstant(31, MVT::i32);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001878 SDLoc DL(Op);
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001879 SDValue Res;
1880
1881 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1882 // to i32.
1883 SDValue X = (TyX == MVT::f32) ?
1884 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1885 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1886 Const1);
1887 SDValue Y = (TyY == MVT::f32) ?
1888 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
1889 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
1890 Const1);
1891
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001892 if (HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001893 // ext E, Y, 31, 1 ; extract bit31 of Y
1894 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
1895 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
1896 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
1897 } else {
1898 // sll SllX, X, 1
1899 // srl SrlX, SllX, 1
1900 // srl SrlY, Y, 31
1901 // sll SllY, SrlX, 31
1902 // or Or, SrlX, SllY
1903 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1904 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1905 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
1906 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
1907 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
1908 }
1909
1910 if (TyX == MVT::f32)
1911 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
1912
1913 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1914 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1915 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001916}
1917
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001918static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG,
1919 bool HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001920 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
1921 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
1922 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
1923 SDValue Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001924 SDLoc DL(Op);
Eric Christopher0713a9d2011-06-08 23:55:35 +00001925
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001926 // Bitcast to integer nodes.
1927 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
1928 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001929
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001930 if (HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001931 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
1932 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
1933 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
1934 DAG.getConstant(WidthY - 1, MVT::i32), Const1);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001935
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001936 if (WidthX > WidthY)
1937 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
1938 else if (WidthY > WidthX)
1939 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001940
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001941 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
1942 DAG.getConstant(WidthX - 1, MVT::i32), Const1, X);
1943 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
1944 }
1945
1946 // (d)sll SllX, X, 1
1947 // (d)srl SrlX, SllX, 1
1948 // (d)srl SrlY, Y, width(Y)-1
1949 // (d)sll SllY, SrlX, width(Y)-1
1950 // or Or, SrlX, SllY
1951 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
1952 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
1953 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
1954 DAG.getConstant(WidthY - 1, MVT::i32));
1955
1956 if (WidthX > WidthY)
1957 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
1958 else if (WidthY > WidthX)
1959 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
1960
1961 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
1962 DAG.getConstant(WidthX - 1, MVT::i32));
1963 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
1964 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001965}
1966
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00001967SDValue
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001968MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00001969 if (Subtarget.isGP64bit())
1970 return lowerFCOPYSIGN64(Op, DAG, Subtarget.hasExtractInsert());
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001971
Eric Christopher1c29a652014-07-18 22:55:25 +00001972 return lowerFCOPYSIGN32(Op, DAG, Subtarget.hasExtractInsert());
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001973}
1974
Akira Hatanaka66277522011-06-02 00:24:44 +00001975SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001976lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes5444a7b2011-06-16 00:40:02 +00001977 // check the depth
1978 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka15506782011-06-07 18:58:42 +00001979 "Frame address can only be determined for current frame.");
Akira Hatanaka66277522011-06-02 00:24:44 +00001980
1981 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1982 MFI->setFrameAddressIsTaken(true);
1983 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001984 SDLoc DL(Op);
Eric Christopher96e72c62015-01-29 23:27:36 +00001985 SDValue FrameAddr = DAG.getCopyFromReg(
1986 DAG.getEntryNode(), DL, ABI.IsN64() ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka66277522011-06-02 00:24:44 +00001987 return FrameAddr;
1988}
1989
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001990SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op,
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00001991 SelectionDAG &DAG) const {
Bill Wendling908bf812014-01-06 00:43:20 +00001992 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001993 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001994
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00001995 // check the depth
1996 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
1997 "Return address can be determined only for current frame.");
1998
1999 MachineFunction &MF = DAG.getMachineFunction();
2000 MachineFrameInfo *MFI = MF.getFrameInfo();
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00002001 MVT VT = Op.getSimpleValueType();
Eric Christopher96e72c62015-01-29 23:27:36 +00002002 unsigned RA = ABI.IsN64() ? Mips::RA_64 : Mips::RA;
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002003 MFI->setReturnAddressIsTaken(true);
2004
2005 // Return RA, which contains the return address. Mark it an implicit live-in.
2006 unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00002007 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT);
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002008}
2009
Akira Hatanakac0b02062013-01-30 00:26:49 +00002010// An EH_RETURN is the result of lowering llvm.eh.return which in turn is
2011// generated from __builtin_eh_return (offset, handler)
2012// The effect of this is to adjust the stack pointer by "offset"
2013// and then branch to "handler".
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002014SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Akira Hatanakac0b02062013-01-30 00:26:49 +00002015 const {
2016 MachineFunction &MF = DAG.getMachineFunction();
2017 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2018
2019 MipsFI->setCallsEhReturn();
2020 SDValue Chain = Op.getOperand(0);
2021 SDValue Offset = Op.getOperand(1);
2022 SDValue Handler = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002023 SDLoc DL(Op);
Eric Christopher96e72c62015-01-29 23:27:36 +00002024 EVT Ty = ABI.IsN64() ? MVT::i64 : MVT::i32;
Akira Hatanakac0b02062013-01-30 00:26:49 +00002025
2026 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
2027 // EH_RETURN nodes, so that instructions are emitted back-to-back.
Eric Christopher96e72c62015-01-29 23:27:36 +00002028 unsigned OffsetReg = ABI.IsN64() ? Mips::V1_64 : Mips::V1;
2029 unsigned AddrReg = ABI.IsN64() ? Mips::V0_64 : Mips::V0;
Akira Hatanakac0b02062013-01-30 00:26:49 +00002030 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
2031 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
2032 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
2033 DAG.getRegister(OffsetReg, Ty),
2034 DAG.getRegister(AddrReg, getPointerTy()),
2035 Chain.getValue(1));
2036}
2037
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002038SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
Akira Hatanaka5fd22482012-06-14 21:10:56 +00002039 SelectionDAG &DAG) const {
Eli Friedman26a48482011-07-27 22:21:52 +00002040 // FIXME: Need pseudo-fence for 'singlethread' fences
2041 // FIXME: Set SType for weaker fences where supported/appropriate.
2042 unsigned SType = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002043 SDLoc DL(Op);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002044 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
Eli Friedman26a48482011-07-27 22:21:52 +00002045 DAG.getConstant(SType, MVT::i32));
2046}
2047
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002048SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
Akira Hatanaka5fd22482012-06-14 21:10:56 +00002049 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002050 SDLoc DL(Op);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002051 MVT VT = Subtarget.isGP64bit() ? MVT::i64 : MVT::i32;
2052
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002053 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2054 SDValue Shamt = Op.getOperand(2);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002055 // if shamt < (VT.bits):
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002056 // lo = (shl lo, shamt)
2057 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
2058 // else:
2059 // lo = 0
2060 // hi = (shl lo, shamt[4:0])
2061 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
2062 DAG.getConstant(-1, MVT::i32));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002063 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, VT, Lo,
2064 DAG.getConstant(1, VT));
2065 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, VT, ShiftRight1Lo, Not);
2066 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, VT, Hi, Shamt);
2067 SDValue Or = DAG.getNode(ISD::OR, DL, VT, ShiftLeftHi, ShiftRightLo);
2068 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, VT, Lo, Shamt);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002069 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
2070 DAG.getConstant(0x20, MVT::i32));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002071 Lo = DAG.getNode(ISD::SELECT, DL, VT, Cond,
2072 DAG.getConstant(0, VT), ShiftLeftLo);
2073 Hi = DAG.getNode(ISD::SELECT, DL, VT, Cond, ShiftLeftLo, Or);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002074
2075 SDValue Ops[2] = {Lo, Hi};
Craig Topper64941d92014-04-27 19:20:57 +00002076 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002077}
2078
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002079SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002080 bool IsSRA) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002081 SDLoc DL(Op);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002082 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2083 SDValue Shamt = Op.getOperand(2);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002084 MVT VT = Subtarget.isGP64bit() ? MVT::i64 : MVT::i32;
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002085
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002086 // if shamt < (VT.bits):
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002087 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
2088 // if isSRA:
2089 // hi = (sra hi, shamt)
2090 // else:
2091 // hi = (srl hi, shamt)
2092 // else:
2093 // if isSRA:
2094 // lo = (sra hi, shamt[4:0])
2095 // hi = (sra hi, 31)
2096 // else:
2097 // lo = (srl hi, shamt[4:0])
2098 // hi = 0
2099 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
2100 DAG.getConstant(-1, MVT::i32));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002101 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, VT, Hi,
2102 DAG.getConstant(1, VT));
2103 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, VT, ShiftLeft1Hi, Not);
2104 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, VT, Lo, Shamt);
2105 SDValue Or = DAG.getNode(ISD::OR, DL, VT, ShiftLeftHi, ShiftRightLo);
2106 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL,
2107 DL, VT, Hi, Shamt);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002108 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
2109 DAG.getConstant(0x20, MVT::i32));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002110 SDValue Shift31 = DAG.getNode(ISD::SRA, DL, VT, Hi, DAG.getConstant(31, VT));
2111 Lo = DAG.getNode(ISD::SELECT, DL, VT, Cond, ShiftRightHi, Or);
2112 Hi = DAG.getNode(ISD::SELECT, DL, VT, Cond,
2113 IsSRA ? Shift31 : DAG.getConstant(0, VT), ShiftRightHi);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002114
2115 SDValue Ops[2] = {Lo, Hi};
Craig Topper64941d92014-04-27 19:20:57 +00002116 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002117}
2118
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002119static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002120 SDValue Chain, SDValue Src, unsigned Offset) {
Akira Hatanaka95866182012-06-13 19:06:08 +00002121 SDValue Ptr = LD->getBasePtr();
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002122 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
Akira Hatanaka95866182012-06-13 19:06:08 +00002123 EVT BasePtrVT = Ptr.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002124 SDLoc DL(LD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002125 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
2126
2127 if (Offset)
Akira Hatanaka95866182012-06-13 19:06:08 +00002128 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002129 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002130
2131 SDValue Ops[] = { Chain, Ptr, Src };
Craig Topper206fcd42014-04-26 19:29:41 +00002132 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002133 LD->getMemOperand());
2134}
2135
2136// Expand an unaligned 32 or 64-bit integer load node.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002137SDValue MipsTargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002138 LoadSDNode *LD = cast<LoadSDNode>(Op);
2139 EVT MemVT = LD->getMemoryVT();
2140
Eric Christopher1c29a652014-07-18 22:55:25 +00002141 if (Subtarget.systemSupportsUnalignedAccess())
Daniel Sandersac272632014-05-23 13:18:02 +00002142 return Op;
2143
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002144 // Return if load is aligned or if MemVT is neither i32 nor i64.
2145 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
2146 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
2147 return SDValue();
2148
Eric Christopher1c29a652014-07-18 22:55:25 +00002149 bool IsLittle = Subtarget.isLittle();
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002150 EVT VT = Op.getValueType();
2151 ISD::LoadExtType ExtType = LD->getExtensionType();
2152 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
2153
2154 assert((VT == MVT::i32) || (VT == MVT::i64));
2155
2156 // Expand
2157 // (set dst, (i64 (load baseptr)))
2158 // to
2159 // (set tmp, (ldl (add baseptr, 7), undef))
2160 // (set dst, (ldr baseptr, tmp))
2161 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002162 SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002163 IsLittle ? 7 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002164 return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002165 IsLittle ? 0 : 7);
2166 }
2167
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002168 SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002169 IsLittle ? 3 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002170 SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002171 IsLittle ? 0 : 3);
2172
2173 // Expand
2174 // (set dst, (i32 (load baseptr))) or
2175 // (set dst, (i64 (sextload baseptr))) or
2176 // (set dst, (i64 (extload baseptr)))
2177 // to
2178 // (set tmp, (lwl (add baseptr, 3), undef))
2179 // (set dst, (lwr baseptr, tmp))
2180 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
2181 (ExtType == ISD::EXTLOAD))
2182 return LWR;
2183
2184 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
2185
2186 // Expand
2187 // (set dst, (i64 (zextload baseptr)))
2188 // to
2189 // (set tmp0, (lwl (add baseptr, 3), undef))
2190 // (set tmp1, (lwr baseptr, tmp0))
2191 // (set tmp2, (shl tmp1, 32))
2192 // (set dst, (srl tmp2, 32))
Andrew Trickef9de2a2013-05-25 02:42:55 +00002193 SDLoc DL(LD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002194 SDValue Const32 = DAG.getConstant(32, MVT::i32);
2195 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
Akira Hatanaka67346852012-06-04 17:46:29 +00002196 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
2197 SDValue Ops[] = { SRL, LWR.getValue(1) };
Craig Topper64941d92014-04-27 19:20:57 +00002198 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002199}
2200
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002201static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002202 SDValue Chain, unsigned Offset) {
Akira Hatanaka95866182012-06-13 19:06:08 +00002203 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
2204 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002205 SDLoc DL(SD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002206 SDVTList VTList = DAG.getVTList(MVT::Other);
2207
2208 if (Offset)
Akira Hatanaka95866182012-06-13 19:06:08 +00002209 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002210 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002211
2212 SDValue Ops[] = { Chain, Value, Ptr };
Craig Topper206fcd42014-04-26 19:29:41 +00002213 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002214 SD->getMemOperand());
2215}
2216
2217// Expand an unaligned 32 or 64-bit integer store node.
Akira Hatanakad82ee942013-05-16 20:45:17 +00002218static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG,
2219 bool IsLittle) {
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002220 SDValue Value = SD->getValue(), Chain = SD->getChain();
2221 EVT VT = Value.getValueType();
2222
2223 // Expand
2224 // (store val, baseptr) or
2225 // (truncstore val, baseptr)
2226 // to
2227 // (swl val, (add baseptr, 3))
2228 // (swr val, baseptr)
2229 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002230 SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002231 IsLittle ? 3 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002232 return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002233 }
2234
2235 assert(VT == MVT::i64);
2236
2237 // Expand
2238 // (store val, baseptr)
2239 // to
2240 // (sdl val, (add baseptr, 7))
2241 // (sdr val, baseptr)
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002242 SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2243 return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002244}
2245
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002246// Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr).
2247static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG) {
2248 SDValue Val = SD->getValue();
2249
2250 if (Val.getOpcode() != ISD::FP_TO_SINT)
2251 return SDValue();
2252
2253 EVT FPTy = EVT::getFloatingPointVT(Val.getValueSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002254 SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy,
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002255 Val.getOperand(0));
2256
Andrew Trickef9de2a2013-05-25 02:42:55 +00002257 return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(),
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002258 SD->getPointerInfo(), SD->isVolatile(),
2259 SD->isNonTemporal(), SD->getAlignment());
2260}
2261
Akira Hatanakad82ee942013-05-16 20:45:17 +00002262SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2263 StoreSDNode *SD = cast<StoreSDNode>(Op);
2264 EVT MemVT = SD->getMemoryVT();
2265
2266 // Lower unaligned integer stores.
Eric Christopher1c29a652014-07-18 22:55:25 +00002267 if (!Subtarget.systemSupportsUnalignedAccess() &&
Daniel Sandersac272632014-05-23 13:18:02 +00002268 (SD->getAlignment() < MemVT.getSizeInBits() / 8) &&
Akira Hatanakad82ee942013-05-16 20:45:17 +00002269 ((MemVT == MVT::i32) || (MemVT == MVT::i64)))
Eric Christopher1c29a652014-07-18 22:55:25 +00002270 return lowerUnalignedIntStore(SD, DAG, Subtarget.isLittle());
Akira Hatanakad82ee942013-05-16 20:45:17 +00002271
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002272 return lowerFP_TO_SINT_STORE(SD, DAG);
Akira Hatanakad82ee942013-05-16 20:45:17 +00002273}
2274
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002275SDValue MipsTargetLowering::lowerADD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002276 if (Op->getOperand(0).getOpcode() != ISD::FRAMEADDR
2277 || cast<ConstantSDNode>
2278 (Op->getOperand(0).getOperand(0))->getZExtValue() != 0
2279 || Op->getOperand(1).getOpcode() != ISD::FRAME_TO_ARGS_OFFSET)
2280 return SDValue();
2281
2282 // The pattern
2283 // (add (frameaddr 0), (frame_to_args_offset))
2284 // results from lowering llvm.eh.dwarf.cfa intrinsic. Transform it to
2285 // (add FrameObject, 0)
2286 // where FrameObject is a fixed StackObject with offset 0 which points to
2287 // the old stack pointer.
2288 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2289 EVT ValTy = Op->getValueType(0);
2290 int FI = MFI->CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
2291 SDValue InArgsAddr = DAG.getFrameIndex(FI, ValTy);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002292 return DAG.getNode(ISD::ADD, SDLoc(Op), ValTy, InArgsAddr,
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002293 DAG.getConstant(0, ValTy));
2294}
2295
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002296SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op,
2297 SelectionDAG &DAG) const {
2298 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002299 SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy,
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002300 Op.getOperand(0));
Andrew Trickef9de2a2013-05-25 02:42:55 +00002301 return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc);
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002302}
2303
Akira Hatanakae2489122011-04-15 21:51:11 +00002304//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002305// Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002306//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002307
Akira Hatanakae2489122011-04-15 21:51:11 +00002308//===----------------------------------------------------------------------===//
Wesley Peck527da1b2010-11-23 03:31:01 +00002309// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002310// Mips O32 ABI rules:
2311// ---
2312// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peck527da1b2010-11-23 03:31:01 +00002313// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002314// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peck527da1b2010-11-23 03:31:01 +00002315// f64 - Only passed in two aliased f32 registers if no int reg has been used
2316// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Sylvestre Ledru469de192014-08-11 18:04:46 +00002317// not used, it must be shadowed. If only A3 is available, shadow it and
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002318// go to stack.
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002319//
2320// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanakae2489122011-04-15 21:51:11 +00002321//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002322
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00002323static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2324 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002325 CCState &State, ArrayRef<MCPhysReg> F64Regs) {
Eric Christopher96e72c62015-01-29 23:27:36 +00002326 const MipsSubtarget &Subtarget = static_cast<const MipsSubtarget &>(
2327 State.getMachineFunction().getSubtarget());
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002328
Craig Topper840beec2014-04-04 05:16:06 +00002329 static const MCPhysReg IntRegs[] = { Mips::A0, Mips::A1, Mips::A2, Mips::A3 };
2330 static const MCPhysReg F32Regs[] = { Mips::F12, Mips::F14 };
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002331
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002332 // Do not process byval args here.
2333 if (ArgFlags.isByVal())
2334 return true;
Akira Hatanaka5e16c6a2011-05-24 19:18:33 +00002335
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002336 // Promote i8 and i16
Daniel Sandersd134c9d2014-12-02 20:40:27 +00002337 if (ArgFlags.isInReg() && !Subtarget.isLittle()) {
2338 if (LocVT == MVT::i8 || LocVT == MVT::i16 || LocVT == MVT::i32) {
2339 LocVT = MVT::i32;
2340 if (ArgFlags.isSExt())
2341 LocInfo = CCValAssign::SExtUpper;
2342 else if (ArgFlags.isZExt())
2343 LocInfo = CCValAssign::ZExtUpper;
2344 else
2345 LocInfo = CCValAssign::AExtUpper;
2346 }
2347 }
2348
2349 // Promote i8 and i16
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002350 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
2351 LocVT = MVT::i32;
2352 if (ArgFlags.isSExt())
2353 LocInfo = CCValAssign::SExt;
2354 else if (ArgFlags.isZExt())
2355 LocInfo = CCValAssign::ZExt;
2356 else
2357 LocInfo = CCValAssign::AExt;
2358 }
2359
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002360 unsigned Reg;
2361
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002362 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
2363 // is true: function is vararg, argument is 3rd or higher, there is previous
2364 // argument which is not f32 or f64.
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002365 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1 ||
2366 State.getFirstUnallocated(F32Regs) != ValNo;
Akira Hatanaka9e6a8cc2011-05-19 20:29:48 +00002367 unsigned OrigAlign = ArgFlags.getOrigAlign();
2368 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002369
2370 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002371 Reg = State.AllocateReg(IntRegs);
Akira Hatanaka9e6a8cc2011-05-19 20:29:48 +00002372 // If this is the first part of an i64 arg,
2373 // the allocated register must be either A0 or A2.
2374 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002375 Reg = State.AllocateReg(IntRegs);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002376 LocVT = MVT::i32;
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002377 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
2378 // Allocate int register and shadow next int register. If first
2379 // available register is Mips::A1 or Mips::A3, shadow it too.
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002380 Reg = State.AllocateReg(IntRegs);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002381 if (Reg == Mips::A1 || Reg == Mips::A3)
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002382 Reg = State.AllocateReg(IntRegs);
2383 State.AllocateReg(IntRegs);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002384 LocVT = MVT::i32;
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002385 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
2386 // we are guaranteed to find an available float register
2387 if (ValVT == MVT::f32) {
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002388 Reg = State.AllocateReg(F32Regs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002389 // Shadow int register
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002390 State.AllocateReg(IntRegs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002391 } else {
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002392 Reg = State.AllocateReg(F64Regs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002393 // Shadow int registers
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002394 unsigned Reg2 = State.AllocateReg(IntRegs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002395 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002396 State.AllocateReg(IntRegs);
2397 State.AllocateReg(IntRegs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002398 }
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002399 } else
2400 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002401
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002402 if (!Reg) {
2403 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3,
2404 OrigAlign);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002405 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002406 } else
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002407 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002408
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002409 return false;
Akira Hatanaka202f6402011-11-12 02:20:46 +00002410}
2411
Akira Hatanakabfb66242013-08-20 23:38:40 +00002412static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT,
2413 MVT LocVT, CCValAssign::LocInfo LocInfo,
2414 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002415 static const MCPhysReg F64Regs[] = { Mips::D6, Mips::D7 };
Akira Hatanakabfb66242013-08-20 23:38:40 +00002416
2417 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2418}
2419
2420static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT,
2421 MVT LocVT, CCValAssign::LocInfo LocInfo,
2422 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002423 static const MCPhysReg F64Regs[] = { Mips::D12_64, Mips::D14_64 };
Akira Hatanakabfb66242013-08-20 23:38:40 +00002424
2425 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2426}
2427
Reid Klecknerd3781742014-11-14 00:39:33 +00002428static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2429 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
2430 CCState &State) LLVM_ATTRIBUTE_UNUSED;
Reed Kotlerd5c41962014-11-13 23:37:45 +00002431
Akira Hatanaka202f6402011-11-12 02:20:46 +00002432#include "MipsGenCallingConv.inc"
2433
Akira Hatanakae2489122011-04-15 21:51:11 +00002434//===----------------------------------------------------------------------===//
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002435// Call Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002436//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002437
Akira Hatanaka61bbcce2011-09-23 00:58:33 +00002438// Return next O32 integer argument register.
2439static unsigned getNextIntArgReg(unsigned Reg) {
2440 assert((Reg == Mips::A0) || (Reg == Mips::A2));
2441 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
2442}
2443
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002444SDValue
2445MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002446 SDValue Chain, SDValue Arg, SDLoc DL,
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002447 bool IsTailCall, SelectionDAG &DAG) const {
2448 if (!IsTailCall) {
2449 SDValue PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr,
2450 DAG.getIntPtrConstant(Offset));
2451 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo(), false,
2452 false, 0);
2453 }
2454
2455 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2456 int FI = MFI->CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
2457 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2458 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(),
2459 /*isVolatile=*/ true, false, 0);
2460}
2461
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002462void MipsTargetLowering::
2463getOpndList(SmallVectorImpl<SDValue> &Ops,
2464 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
2465 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
Sasa Stankovic7072a792014-10-01 08:22:21 +00002466 bool IsCallReloc, CallLoweringInfo &CLI, SDValue Callee,
2467 SDValue Chain) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002468 // Insert node "GP copy globalreg" before call to function.
2469 //
2470 // R_MIPS_CALL* operators (emitted when non-internal functions are called
2471 // in PIC mode) allow symbols to be resolved via lazy binding.
2472 // The lazy binding stub requires GP to point to the GOT.
Sasa Stankovic7072a792014-10-01 08:22:21 +00002473 // Note that we don't need GP to point to the GOT for indirect calls
2474 // (when R_MIPS_CALL* is not used for the call) because Mips linker generates
2475 // lazy binding stub for a function only when R_MIPS_CALL* are the only relocs
2476 // used for the function (that is, Mips linker doesn't generate lazy binding
2477 // stub for a function whose address is taken in the program).
2478 if (IsPICCall && !InternalLinkage && IsCallReloc) {
Eric Christopher96e72c62015-01-29 23:27:36 +00002479 unsigned GPReg = ABI.IsN64() ? Mips::GP_64 : Mips::GP;
2480 EVT Ty = ABI.IsN64() ? MVT::i64 : MVT::i32;
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002481 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty)));
2482 }
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002483
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002484 // Build a sequence of copy-to-reg nodes chained together with token
2485 // chain and flag operands which copy the outgoing args into registers.
2486 // The InFlag in necessary since all emitted instructions must be
2487 // stuck together.
2488 SDValue InFlag;
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002489
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002490 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2491 Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, RegsToPass[i].first,
2492 RegsToPass[i].second, InFlag);
2493 InFlag = Chain.getValue(1);
2494 }
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002495
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002496 // Add argument registers to the end of the list so that they are
2497 // known live into the call.
2498 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2499 Ops.push_back(CLI.DAG.getRegister(RegsToPass[i].first,
2500 RegsToPass[i].second.getValueType()));
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002501
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002502 // Add a register mask operand representing the call-preserved registers.
Eric Christopher96e72c62015-01-29 23:27:36 +00002503 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00002504 const uint32_t *Mask =
2505 TRI->getCallPreservedMask(CLI.DAG.getMachineFunction(), CLI.CallConv);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002506 assert(Mask && "Missing call preserved mask for calling convention");
Eric Christopher1c29a652014-07-18 22:55:25 +00002507 if (Subtarget.inMips16HardFloat()) {
Reed Kotler783c7942013-05-10 22:25:39 +00002508 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
2509 llvm::StringRef Sym = G->getGlobal()->getName();
2510 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
Reed Kotler3230e722013-12-12 02:41:11 +00002511 if (F && F->hasFnAttribute("__Mips16RetHelper")) {
Reed Kotler783c7942013-05-10 22:25:39 +00002512 Mask = MipsRegisterInfo::getMips16RetHelperMask();
2513 }
2514 }
2515 }
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002516 Ops.push_back(CLI.DAG.getRegisterMask(Mask));
2517
2518 if (InFlag.getNode())
2519 Ops.push_back(InFlag);
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002520}
2521
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002522/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman624801e2009-01-26 03:15:54 +00002523/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002524SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00002525MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002526 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00002527 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002528 SDLoc DL = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00002529 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
2530 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
2531 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Akira Hatanakabeda2242012-07-31 18:46:41 +00002532 SDValue Chain = CLI.Chain;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002533 SDValue Callee = CLI.Callee;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002534 bool &IsTailCall = CLI.IsTailCall;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002535 CallingConv::ID CallConv = CLI.CallConv;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002536 bool IsVarArg = CLI.IsVarArg;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002537
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002538 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002539 MachineFrameInfo *MFI = MF.getFrameInfo();
Eric Christopher96e72c62015-01-29 23:27:36 +00002540 const TargetFrameLowering *TFL = Subtarget.getFrameLowering();
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002541 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +00002542 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002543
2544 // Analyze operands of the call, assigning locations to each operand.
2545 SmallVector<CCValAssign, 16> ArgLocs;
Daniel Sanders41a64c42014-11-07 11:10:48 +00002546 MipsCCState CCInfo(
2547 CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs, *DAG.getContext(),
2548 MipsCCState::getSpecialCallingConvForCallee(Callee.getNode(), Subtarget));
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002549
2550 // Allocate the reserved argument area. It seems strange to do this from the
2551 // caller side but removing it breaks the frame size calculation.
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002552 CCInfo.AllocateStack(ABI.GetCalleeAllocdArgSizeInBytes(CallConv), 1);
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002553
Daniel Sanderscfad1e32014-11-07 11:43:49 +00002554 CCInfo.AnalyzeCallOperands(Outs, CC_Mips, CLI.getArgs(), Callee.getNode());
Wesley Peck527da1b2010-11-23 03:31:01 +00002555
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002556 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka195a1e22011-06-08 17:39:33 +00002557 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka97ba7692012-07-26 23:27:01 +00002558
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002559 // Check if it's really possible to do a tail call.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002560 if (IsTailCall)
Daniel Sanders23e98772014-11-02 16:09:29 +00002561 IsTailCall = isEligibleForTailCallOptimization(
2562 CCInfo, NextStackOffset, *MF.getInfo<MipsFunctionInfo>());
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002563
Reid Kleckner5772b772014-04-24 20:14:34 +00002564 if (!IsTailCall && CLI.CS && CLI.CS->isMustTailCall())
2565 report_fatal_error("failed to perform tail call elimination on a call "
2566 "site marked musttail");
2567
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002568 if (IsTailCall)
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002569 ++NumTailCalls;
2570
Akira Hatanaka79738332011-09-19 20:26:02 +00002571 // Chain is the output chain of the last Load/Store or CopyToReg node.
2572 // ByValChain is the output chain of the last Memcpy node created for copying
2573 // byval arguments to the stack.
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002574 unsigned StackAlignment = TFL->getStackAlignment();
2575 NextStackOffset = RoundUpToAlignment(NextStackOffset, StackAlignment);
Akira Hatanaka79738332011-09-19 20:26:02 +00002576 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002577
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002578 if (!IsTailCall)
Andrew Trickad6d08a2013-05-29 22:03:55 +00002579 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffsetVal, DL);
Akira Hatanakabeda2242012-07-31 18:46:41 +00002580
Daniel Sandersd897b562014-03-27 10:46:12 +00002581 SDValue StackPtr = DAG.getCopyFromReg(
Eric Christopher96e72c62015-01-29 23:27:36 +00002582 Chain, DL, ABI.IsN64() ? Mips::SP_64 : Mips::SP, getPointerTy());
Akira Hatanaka195a1e22011-06-08 17:39:33 +00002583
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002584 // With EABI is it possible to have 16 args on registers.
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002585 std::deque< std::pair<unsigned, SDValue> > RegsToPass;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002586 SmallVector<SDValue, 8> MemOpChains;
Daniel Sanders23e98772014-11-02 16:09:29 +00002587
2588 CCInfo.rewindByValRegsInfo();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002589
2590 // Walk the register/memloc assignments, inserting copies/loads.
2591 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002592 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002593 CCValAssign &VA = ArgLocs[i];
Akira Hatanakab20a3252011-10-28 19:49:00 +00002594 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka19891f82011-11-12 02:34:50 +00002595 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002596 bool UseUpperBits = false;
Akira Hatanaka19891f82011-11-12 02:34:50 +00002597
2598 // ByVal Arg.
2599 if (Flags.isByVal()) {
Daniel Sanders23e98772014-11-02 16:09:29 +00002600 unsigned FirstByValReg, LastByValReg;
2601 unsigned ByValIdx = CCInfo.getInRegsParamsProcessed();
2602 CCInfo.getInRegsParamInfo(ByValIdx, FirstByValReg, LastByValReg);
2603
Akira Hatanaka19891f82011-11-12 02:34:50 +00002604 assert(Flags.getByValSize() &&
2605 "ByVal args of size 0 should have been ignored by front-end.");
Daniel Sanders23e98772014-11-02 16:09:29 +00002606 assert(ByValIdx < CCInfo.getInRegsParamsCount());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002607 assert(!IsTailCall &&
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002608 "Do not tail-call optimize if there is a byval argument.");
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002609 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002610 FirstByValReg, LastByValReg, Flags, Subtarget.isLittle(),
2611 VA);
Daniel Sanders23e98772014-11-02 16:09:29 +00002612 CCInfo.nextInRegsParam();
Akira Hatanaka19891f82011-11-12 02:34:50 +00002613 continue;
2614 }
Jia Liuf54f60f2012-02-28 07:46:26 +00002615
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002616 // Promote the value if needed.
2617 switch (VA.getLocInfo()) {
Daniel Sandersc43cda82014-11-07 16:54:21 +00002618 default:
2619 llvm_unreachable("Unknown loc info!");
Wesley Peck527da1b2010-11-23 03:31:01 +00002620 case CCValAssign::Full:
Akira Hatanakab20a3252011-10-28 19:49:00 +00002621 if (VA.isRegLoc()) {
2622 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
Akira Hatanaka3b7391d2013-03-05 22:20:28 +00002623 (ValVT == MVT::f64 && LocVT == MVT::i64) ||
2624 (ValVT == MVT::i64 && LocVT == MVT::f64))
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002625 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
Akira Hatanakab20a3252011-10-28 19:49:00 +00002626 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002627 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanakae2489122011-04-15 21:51:11 +00002628 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002629 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanakaaef55c82011-04-15 21:00:26 +00002630 Arg, DAG.getConstant(1, MVT::i32));
Eric Christopher1c29a652014-07-18 22:55:25 +00002631 if (!Subtarget.isLittle())
Akira Hatanaka27916972011-04-15 19:52:08 +00002632 std::swap(Lo, Hi);
Jia Liuf54f60f2012-02-28 07:46:26 +00002633 unsigned LocRegLo = VA.getLocReg();
Akira Hatanaka61bbcce2011-09-23 00:58:33 +00002634 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2635 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2636 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002637 continue;
Wesley Peck527da1b2010-11-23 03:31:01 +00002638 }
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002639 }
2640 break;
Daniel Sanders23e98772014-11-02 16:09:29 +00002641 case CCValAssign::BCvt:
2642 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
2643 break;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002644 case CCValAssign::SExtUpper:
2645 UseUpperBits = true;
2646 // Fallthrough
Chris Lattner52f16de2008-03-17 06:57:02 +00002647 case CCValAssign::SExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002648 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002649 break;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002650 case CCValAssign::ZExtUpper:
2651 UseUpperBits = true;
2652 // Fallthrough
Chris Lattner52f16de2008-03-17 06:57:02 +00002653 case CCValAssign::ZExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002654 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002655 break;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002656 case CCValAssign::AExtUpper:
2657 UseUpperBits = true;
2658 // Fallthrough
Chris Lattner52f16de2008-03-17 06:57:02 +00002659 case CCValAssign::AExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002660 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002661 break;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002662 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002663
Daniel Sandersc43cda82014-11-07 16:54:21 +00002664 if (UseUpperBits) {
2665 unsigned ValSizeInBits = Outs[i].ArgVT.getSizeInBits();
2666 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
2667 Arg = DAG.getNode(
2668 ISD::SHL, DL, VA.getLocVT(), Arg,
2669 DAG.getConstant(LocSizeInBits - ValSizeInBits, VA.getLocVT()));
2670 }
2671
Wesley Peck527da1b2010-11-23 03:31:01 +00002672 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +00002673 // RegsToPass vector
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002674 if (VA.isRegLoc()) {
2675 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattner52f16de2008-03-17 06:57:02 +00002676 continue;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002677 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002678
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002679 // Register can't get to this point...
Chris Lattner52f16de2008-03-17 06:57:02 +00002680 assert(VA.isMemLoc());
Wesley Peck527da1b2010-11-23 03:31:01 +00002681
Wesley Peck527da1b2010-11-23 03:31:01 +00002682 // emit ISD::STORE whichs stores the
Chris Lattner52f16de2008-03-17 06:57:02 +00002683 // parameter value to a stack Location
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002684 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002685 Chain, Arg, DL, IsTailCall, DAG));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002686 }
2687
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002688 // Transform all store nodes into one single node because all store
2689 // nodes are independent of each other.
Wesley Peck527da1b2010-11-23 03:31:01 +00002690 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00002691 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002692
Bill Wendling24c79f22008-09-16 21:48:12 +00002693 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peck527da1b2010-11-23 03:31:01 +00002694 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2695 // node so that legalize doesn't hack it.
Eric Christopher96e72c62015-01-29 23:27:36 +00002696 bool IsPICCall = (ABI.IsN64() || IsPIC); // true if calls are translated to
2697 // jalr $25
Sasa Stankovic7072a792014-10-01 08:22:21 +00002698 bool GlobalOrExternal = false, InternalLinkage = false, IsCallReloc = false;
Akira Hatanakad6f1c582011-04-07 19:51:44 +00002699 SDValue CalleeLo;
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002700 EVT Ty = Callee.getValueType();
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002701
2702 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002703 if (IsPICCall) {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002704 const GlobalValue *Val = G->getGlobal();
2705 InternalLinkage = Val->hasInternalLinkage();
Akira Hatanakacf9a61b2012-12-13 03:17:29 +00002706
2707 if (InternalLinkage)
Eric Christopher96e72c62015-01-29 23:27:36 +00002708 Callee = getAddrLocal(G, DL, Ty, DAG, ABI.IsN32() || ABI.IsN64());
Sasa Stankovic7072a792014-10-01 08:22:21 +00002709 else if (LargeGOT) {
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002710 Callee = getAddrGlobalLargeGOT(G, DL, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002711 MipsII::MO_CALL_LO16, Chain,
2712 FuncInfo->callPtrInfo(Val));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002713 IsCallReloc = true;
2714 } else {
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002715 Callee = getAddrGlobal(G, DL, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002716 FuncInfo->callPtrInfo(Val));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002717 IsCallReloc = true;
2718 }
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002719 } else
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002720 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, getPointerTy(), 0,
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002721 MipsII::MO_NO_FLAG);
Akira Hatanaka8e16aac2011-12-09 01:45:12 +00002722 GlobalOrExternal = true;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002723 }
2724 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002725 const char *Sym = S->getSymbol();
2726
Eric Christopher96e72c62015-01-29 23:27:36 +00002727 if (!ABI.IsN64() && !IsPIC) // !N64 && static
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002728 Callee =
2729 DAG.getTargetExternalSymbol(Sym, getPointerTy(), MipsII::MO_NO_FLAG);
Sasa Stankovic7072a792014-10-01 08:22:21 +00002730 else if (LargeGOT) {
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002731 Callee = getAddrGlobalLargeGOT(S, DL, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002732 MipsII::MO_CALL_LO16, Chain,
2733 FuncInfo->callPtrInfo(Sym));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002734 IsCallReloc = true;
2735 } else { // N64 || PIC
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002736 Callee = getAddrGlobal(S, DL, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002737 FuncInfo->callPtrInfo(Sym));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002738 IsCallReloc = true;
2739 }
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002740
Akira Hatanaka8e16aac2011-12-09 01:45:12 +00002741 GlobalOrExternal = true;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002742 }
2743
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002744 SmallVector<SDValue, 8> Ops(1, Chain);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002745 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002746
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002747 getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal, InternalLinkage,
Sasa Stankovic7072a792014-10-01 08:22:21 +00002748 IsCallReloc, CLI, Callee, Chain);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002749
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002750 if (IsTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +00002751 return DAG.getNode(MipsISD::TailCall, DL, MVT::Other, Ops);
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002752
Craig Topper48d114b2014-04-26 18:35:24 +00002753 Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, Ops);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002754 SDValue InFlag = Chain.getValue(1);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002755
Bruno Cardoso Lopes193e64c2010-01-30 18:32:07 +00002756 // Create the CALLSEQ_END node.
Akira Hatanaka97ba7692012-07-26 23:27:01 +00002757 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal,
Andrew Trickad6d08a2013-05-29 22:03:55 +00002758 DAG.getIntPtrConstant(0, true), InFlag, DL);
Bruno Cardoso Lopes193e64c2010-01-30 18:32:07 +00002759 InFlag = Chain.getValue(1);
2760
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002761 // Handle result values, copying them out of physregs into vregs that we
2762 // return.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002763 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg, Ins, DL, DAG,
2764 InVals, CLI);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002765}
2766
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002767/// LowerCallResult - Lower the result values of a call into the
2768/// appropriate copies out of appropriate physical registers.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002769SDValue MipsTargetLowering::LowerCallResult(
2770 SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool IsVarArg,
2771 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG,
2772 SmallVectorImpl<SDValue> &InVals,
2773 TargetLowering::CallLoweringInfo &CLI) const {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002774 // Assign locations to each value returned by this call.
2775 SmallVector<CCValAssign, 16> RVLocs;
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002776 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
2777 *DAG.getContext());
2778 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips, CLI);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002779
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002780 // Copy all of the result registers out of their specified physreg.
2781 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Daniel Sandersae275e32014-09-25 12:15:05 +00002782 CCValAssign &VA = RVLocs[i];
2783 assert(VA.isRegLoc() && "Can only return in registers!");
2784
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002785 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(),
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002786 RVLocs[i].getLocVT(), InFlag);
2787 Chain = Val.getValue(1);
2788 InFlag = Val.getValue(2);
2789
Daniel Sandersae275e32014-09-25 12:15:05 +00002790 if (VA.isUpperBitsInLoc()) {
2791 unsigned ValSizeInBits = Ins[i].ArgVT.getSizeInBits();
2792 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
2793 unsigned Shift =
2794 VA.getLocInfo() == CCValAssign::ZExtUpper ? ISD::SRL : ISD::SRA;
2795 Val = DAG.getNode(
2796 Shift, DL, VA.getLocVT(), Val,
2797 DAG.getConstant(LocSizeInBits - ValSizeInBits, VA.getLocVT()));
2798 }
2799
2800 switch (VA.getLocInfo()) {
2801 default:
2802 llvm_unreachable("Unknown loc info!");
2803 case CCValAssign::Full:
2804 break;
2805 case CCValAssign::BCvt:
2806 Val = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Val);
2807 break;
2808 case CCValAssign::AExt:
2809 case CCValAssign::AExtUpper:
2810 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2811 break;
2812 case CCValAssign::ZExt:
2813 case CCValAssign::ZExtUpper:
2814 Val = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Val,
2815 DAG.getValueType(VA.getValVT()));
2816 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2817 break;
2818 case CCValAssign::SExt:
2819 case CCValAssign::SExtUpper:
2820 Val = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Val,
2821 DAG.getValueType(VA.getValVT()));
2822 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2823 break;
2824 }
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002825
2826 InVals.push_back(Val);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002827 }
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +00002828
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002829 return Chain;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002830}
2831
Daniel Sandersc43cda82014-11-07 16:54:21 +00002832static SDValue UnpackFromArgumentSlot(SDValue Val, const CCValAssign &VA,
2833 EVT ArgVT, SDLoc DL, SelectionDAG &DAG) {
2834 MVT LocVT = VA.getLocVT();
2835 EVT ValVT = VA.getValVT();
2836
2837 // Shift into the upper bits if necessary.
2838 switch (VA.getLocInfo()) {
2839 default:
2840 break;
2841 case CCValAssign::AExtUpper:
2842 case CCValAssign::SExtUpper:
2843 case CCValAssign::ZExtUpper: {
2844 unsigned ValSizeInBits = ArgVT.getSizeInBits();
2845 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
2846 unsigned Opcode =
2847 VA.getLocInfo() == CCValAssign::ZExtUpper ? ISD::SRL : ISD::SRA;
2848 Val = DAG.getNode(
2849 Opcode, DL, VA.getLocVT(), Val,
2850 DAG.getConstant(LocSizeInBits - ValSizeInBits, VA.getLocVT()));
2851 break;
2852 }
2853 }
2854
2855 // If this is an value smaller than the argument slot size (32-bit for O32,
2856 // 64-bit for N32/N64), it has been promoted in some way to the argument slot
2857 // size. Extract the value and insert any appropriate assertions regarding
2858 // sign/zero extension.
2859 switch (VA.getLocInfo()) {
2860 default:
2861 llvm_unreachable("Unknown loc info!");
2862 case CCValAssign::Full:
2863 break;
2864 case CCValAssign::AExtUpper:
2865 case CCValAssign::AExt:
2866 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
2867 break;
2868 case CCValAssign::SExtUpper:
2869 case CCValAssign::SExt:
2870 Val = DAG.getNode(ISD::AssertSext, DL, LocVT, Val, DAG.getValueType(ValVT));
2871 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
2872 break;
2873 case CCValAssign::ZExtUpper:
2874 case CCValAssign::ZExt:
2875 Val = DAG.getNode(ISD::AssertZext, DL, LocVT, Val, DAG.getValueType(ValVT));
2876 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
2877 break;
2878 case CCValAssign::BCvt:
2879 Val = DAG.getNode(ISD::BITCAST, DL, ValVT, Val);
2880 break;
2881 }
2882
2883 return Val;
2884}
2885
Akira Hatanakae2489122011-04-15 21:51:11 +00002886//===----------------------------------------------------------------------===//
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002887// Formal Arguments Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002888//===----------------------------------------------------------------------===//
Wesley Peck527da1b2010-11-23 03:31:01 +00002889/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002890/// and generate load operations for arguments places on the stack.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002891SDValue
2892MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanakaaef55c82011-04-15 21:00:26 +00002893 CallingConv::ID CallConv,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002894 bool IsVarArg,
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00002895 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002896 SDLoc DL, SelectionDAG &DAG,
Akira Hatanakaaef55c82011-04-15 21:00:26 +00002897 SmallVectorImpl<SDValue> &InVals)
Akira Hatanakae2489122011-04-15 21:51:11 +00002898 const {
Bruno Cardoso Lopesa01ede22008-08-04 07:12:52 +00002899 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002900 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopes14033fb2007-08-28 05:08:16 +00002901 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002902
Dan Gohman31ae5862010-04-17 14:41:14 +00002903 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002904
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002905 // Used with vargs to acumulate store chains.
2906 std::vector<SDValue> OutChains;
2907
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002908 // Assign locations to all of the incoming arguments.
2909 SmallVector<CCValAssign, 16> ArgLocs;
Daniel Sanders23e98772014-11-02 16:09:29 +00002910 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
2911 *DAG.getContext());
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002912 CCInfo.AllocateStack(ABI.GetCalleeAllocdArgSizeInBytes(CallConv), 1);
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002913 Function::const_arg_iterator FuncArg =
2914 DAG.getMachineFunction().getFunction()->arg_begin();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002915
Daniel Sandersb70e27c2014-11-06 16:36:30 +00002916 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips_FixedArg);
Akira Hatanaka4866fe12012-10-30 19:37:25 +00002917 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(),
Daniel Sanders23e98772014-11-02 16:09:29 +00002918 CCInfo.getInRegsParamsCount() > 0);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002919
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00002920 unsigned CurArgIdx = 0;
Daniel Sanders23e98772014-11-02 16:09:29 +00002921 CCInfo.rewindByValRegsInfo();
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002922
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00002923 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002924 CCValAssign &VA = ArgLocs[i];
Andrew Trick05938a52015-02-16 18:10:47 +00002925 if (Ins[i].isOrigArg()) {
2926 std::advance(FuncArg, Ins[i].getOrigArgIndex() - CurArgIdx);
2927 CurArgIdx = Ins[i].getOrigArgIndex();
2928 }
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002929 EVT ValVT = VA.getValVT();
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002930 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2931 bool IsRegLoc = VA.isRegLoc();
2932
2933 if (Flags.isByVal()) {
Andrew Trick05938a52015-02-16 18:10:47 +00002934 assert(Ins[i].isOrigArg() && "Byval arguments cannot be implicit");
Daniel Sanders23e98772014-11-02 16:09:29 +00002935 unsigned FirstByValReg, LastByValReg;
2936 unsigned ByValIdx = CCInfo.getInRegsParamsProcessed();
2937 CCInfo.getInRegsParamInfo(ByValIdx, FirstByValReg, LastByValReg);
2938
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002939 assert(Flags.getByValSize() &&
2940 "ByVal args of size 0 should have been ignored by front-end.");
Daniel Sanders23e98772014-11-02 16:09:29 +00002941 assert(ByValIdx < CCInfo.getInRegsParamsCount());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002942 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002943 FirstByValReg, LastByValReg, VA, CCInfo);
Daniel Sanders23e98772014-11-02 16:09:29 +00002944 CCInfo.nextInRegsParam();
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002945 continue;
2946 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002947
2948 // Arguments stored on registers
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002949 if (IsRegLoc) {
Akira Hatanaka7d822522013-10-28 21:21:36 +00002950 MVT RegVT = VA.getLocVT();
Akira Hatanakacb4a1a82011-05-24 00:23:52 +00002951 unsigned ArgReg = VA.getLocReg();
Akira Hatanaka7d822522013-10-28 21:21:36 +00002952 const TargetRegisterClass *RC = getRegClassFor(RegVT);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002953
Wesley Peck527da1b2010-11-23 03:31:01 +00002954 // Transform the arguments stored on
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002955 // physical registers into virtual ones
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002956 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC);
2957 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
Wesley Peck527da1b2010-11-23 03:31:01 +00002958
Daniel Sandersc43cda82014-11-07 16:54:21 +00002959 ArgValue = UnpackFromArgumentSlot(ArgValue, VA, Ins[i].ArgVT, DL, DAG);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002960
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002961 // Handle floating point arguments passed in integer registers and
2962 // long double arguments passed in floating point registers.
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002963 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002964 (RegVT == MVT::i64 && ValVT == MVT::f64) ||
2965 (RegVT == MVT::f64 && ValVT == MVT::i64))
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002966 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
Eric Christopher96e72c62015-01-29 23:27:36 +00002967 else if (ABI.IsO32() && RegVT == MVT::i32 &&
Eric Christopherbf33a3c2014-07-02 23:18:40 +00002968 ValVT == MVT::f64) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002969 unsigned Reg2 = addLiveIn(DAG.getMachineFunction(),
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002970 getNextIntArgReg(ArgReg), RC);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002971 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT);
Eric Christopher1c29a652014-07-18 22:55:25 +00002972 if (!Subtarget.isLittle())
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002973 std::swap(ArgValue, ArgValue2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002974 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64,
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002975 ArgValue, ArgValue2);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002976 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002977
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002978 InVals.push_back(ArgValue);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002979 } else { // VA.isRegLoc()
Daniel Sandersc43cda82014-11-07 16:54:21 +00002980 MVT LocVT = VA.getLocVT();
2981
Eric Christopher96e72c62015-01-29 23:27:36 +00002982 if (ABI.IsO32()) {
Daniel Sandersc43cda82014-11-07 16:54:21 +00002983 // We ought to be able to use LocVT directly but O32 sets it to i32
2984 // when allocating floating point values to integer registers.
2985 // This shouldn't influence how we load the value into registers unless
2986 // we are targetting softfloat.
2987 if (VA.getValVT().isFloatingPoint() && !Subtarget.abiUsesSoftFloat())
2988 LocVT = VA.getValVT();
2989 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002990
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002991 // sanity check
2992 assert(VA.isMemLoc());
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002993
Wesley Peck527da1b2010-11-23 03:31:01 +00002994 // The stack pointer offset is relative to the caller stack frame.
Daniel Sandersc43cda82014-11-07 16:54:21 +00002995 int FI = MFI->CreateFixedObject(LocVT.getSizeInBits() / 8,
Akira Hatanakacb4a1a82011-05-24 00:23:52 +00002996 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002997
2998 // Create load nodes to retrieve arguments from the stack
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002999 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Daniel Sandersc43cda82014-11-07 16:54:21 +00003000 SDValue ArgValue = DAG.getLoad(LocVT, DL, Chain, FIN,
3001 MachinePointerInfo::getFixedStack(FI),
3002 false, false, false, 0);
3003 OutChains.push_back(ArgValue.getValue(1));
3004
3005 ArgValue = UnpackFromArgumentSlot(ArgValue, VA, Ins[i].ArgVT, DL, DAG);
3006
3007 InVals.push_back(ArgValue);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003008 }
Reid Kleckner7a59e082014-05-12 22:01:27 +00003009 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003010
Reid Kleckner7a59e082014-05-12 22:01:27 +00003011 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Reid Kleckner79418562014-05-09 22:32:13 +00003012 // The mips ABIs for returning structs by value requires that we copy
3013 // the sret argument into $v0 for the return. Save the argument into
3014 // a virtual register so that we can access it from the return points.
Reid Kleckner7a59e082014-05-12 22:01:27 +00003015 if (Ins[i].Flags.isSRet()) {
Reid Kleckner79418562014-05-09 22:32:13 +00003016 unsigned Reg = MipsFI->getSRetReturnReg();
3017 if (!Reg) {
3018 Reg = MF.getRegInfo().createVirtualRegister(
Eric Christopher96e72c62015-01-29 23:27:36 +00003019 getRegClassFor(ABI.IsN64() ? MVT::i64 : MVT::i32));
Reid Kleckner79418562014-05-09 22:32:13 +00003020 MipsFI->setSRetReturnReg(Reg);
3021 }
Reid Kleckner7a59e082014-05-12 22:01:27 +00003022 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[i]);
Reid Kleckner79418562014-05-09 22:32:13 +00003023 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
Reid Kleckner7a59e082014-05-12 22:01:27 +00003024 break;
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003025 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003026 }
3027
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003028 if (IsVarArg)
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003029 writeVarArgRegs(OutChains, Chain, DL, DAG, CCInfo);
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003030
Wesley Peck527da1b2010-11-23 03:31:01 +00003031 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003032 // the size of Ins and InVals. This only happens when on varg functions
3033 if (!OutChains.empty()) {
3034 OutChains.push_back(Chain);
Craig Topper48d114b2014-04-26 18:35:24 +00003035 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003036 }
3037
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003038 return Chain;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003039}
3040
Akira Hatanakae2489122011-04-15 21:51:11 +00003041//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003042// Return Value Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00003043//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003044
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003045bool
3046MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003047 MachineFunction &MF, bool IsVarArg,
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003048 const SmallVectorImpl<ISD::OutputArg> &Outs,
3049 LLVMContext &Context) const {
3050 SmallVector<CCValAssign, 16> RVLocs;
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003051 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003052 return CCInfo.CheckReturn(Outs, RetCC_Mips);
3053}
3054
Petar Jovanovic5b436222015-03-23 12:28:13 +00003055bool
3056MipsTargetLowering::shouldSignExtendTypeInLibCall(EVT Type, bool IsSigned) const {
3057 if (Subtarget.hasMips3() && Subtarget.abiUsesSoftFloat()) {
3058 if (Type == MVT::i32)
3059 return true;
3060 }
3061 return IsSigned;
3062}
3063
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003064SDValue
3065MipsTargetLowering::LowerReturn(SDValue Chain,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003066 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003067 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003068 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003069 SDLoc DL, SelectionDAG &DAG) const {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003070 // CCValAssign - represent the assignment of
3071 // the return value to a location
3072 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003073 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003074
3075 // CCState - Info about the registers and stack slot.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003076 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, *DAG.getContext());
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003077
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003078 // Analyze return values.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003079 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003080
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003081 SDValue Flag;
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003082 SmallVector<SDValue, 4> RetOps(1, Chain);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003083
3084 // Copy the result values into the output registers.
3085 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003086 SDValue Val = OutVals[i];
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003087 CCValAssign &VA = RVLocs[i];
3088 assert(VA.isRegLoc() && "Can only return in registers!");
Daniel Sandersae275e32014-09-25 12:15:05 +00003089 bool UseUpperBits = false;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003090
Daniel Sandersae275e32014-09-25 12:15:05 +00003091 switch (VA.getLocInfo()) {
3092 default:
3093 llvm_unreachable("Unknown loc info!");
3094 case CCValAssign::Full:
3095 break;
3096 case CCValAssign::BCvt:
3097 Val = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Val);
3098 break;
3099 case CCValAssign::AExtUpper:
3100 UseUpperBits = true;
3101 // Fallthrough
3102 case CCValAssign::AExt:
3103 Val = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Val);
3104 break;
3105 case CCValAssign::ZExtUpper:
3106 UseUpperBits = true;
3107 // Fallthrough
3108 case CCValAssign::ZExt:
3109 Val = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Val);
3110 break;
3111 case CCValAssign::SExtUpper:
3112 UseUpperBits = true;
3113 // Fallthrough
3114 case CCValAssign::SExt:
3115 Val = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Val);
3116 break;
3117 }
3118
3119 if (UseUpperBits) {
3120 unsigned ValSizeInBits = Outs[i].ArgVT.getSizeInBits();
3121 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
3122 Val = DAG.getNode(
3123 ISD::SHL, DL, VA.getLocVT(), Val,
3124 DAG.getConstant(LocSizeInBits - ValSizeInBits, VA.getLocVT()));
3125 }
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003126
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003127 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003128
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003129 // Guarantee that all emitted copies are stuck together with flags.
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003130 Flag = Chain.getValue(1);
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003131 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003132 }
3133
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003134 // The mips ABIs for returning structs by value requires that we copy
3135 // the sret argument into $v0 for the return. We saved the argument into
3136 // a virtual register in the entry block, so now we copy the value out
3137 // and into $v0.
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003138 if (MF.getFunction()->hasStructRetAttr()) {
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003139 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3140 unsigned Reg = MipsFI->getSRetReturnReg();
3141
Wesley Peck527da1b2010-11-23 03:31:01 +00003142 if (!Reg)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003143 llvm_unreachable("sret virtual register not created in the entry block");
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003144 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Eric Christopher96e72c62015-01-29 23:27:36 +00003145 unsigned V0 = ABI.IsN64() ? Mips::V0_64 : Mips::V0;
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003146
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003147 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003148 Flag = Chain.getValue(1);
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003149 RetOps.push_back(DAG.getRegister(V0, getPointerTy()));
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003150 }
3151
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003152 RetOps[0] = Chain; // Update chain.
Akira Hatanakaefff7b72012-07-10 00:19:06 +00003153
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003154 // Add the flag if we have it.
3155 if (Flag.getNode())
3156 RetOps.push_back(Flag);
3157
3158 // Return on Mips is always a "jr $ra"
Craig Topper48d114b2014-04-26 18:35:24 +00003159 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, RetOps);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003160}
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003161
Akira Hatanakae2489122011-04-15 21:51:11 +00003162//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003163// Mips Inline Assembly Support
Akira Hatanakae2489122011-04-15 21:51:11 +00003164//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003165
3166/// getConstraintType - Given a constraint letter, return the type of
3167/// constraint it is for this target.
3168MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peck527da1b2010-11-23 03:31:01 +00003169getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003170{
Daniel Sanders8b59af12013-11-12 12:56:01 +00003171 // Mips specific constraints
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003172 // GCC config/mips/constraints.md
3173 //
Wesley Peck527da1b2010-11-23 03:31:01 +00003174 // 'd' : An address register. Equivalent to r
3175 // unless generating MIPS16 code.
3176 // 'y' : Equivalent to r; retained for
3177 // backwards compatibility.
Eric Christophere3c494d2012-05-07 06:25:10 +00003178 // 'c' : A register suitable for use in an indirect
3179 // jump. This will always be $25 for -mabicalls.
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003180 // 'l' : The lo register. 1 word storage.
3181 // 'x' : The hilo register pair. Double word storage.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003182 if (Constraint.size() == 1) {
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003183 switch (Constraint[0]) {
3184 default : break;
Wesley Peck527da1b2010-11-23 03:31:01 +00003185 case 'd':
3186 case 'y':
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003187 case 'f':
Eric Christophere3c494d2012-05-07 06:25:10 +00003188 case 'c':
Eric Christopher9c492e62012-05-07 06:25:15 +00003189 case 'l':
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003190 case 'x':
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003191 return C_RegisterClass;
Jack Carter0e149b02013-03-04 21:33:15 +00003192 case 'R':
3193 return C_Memory;
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003194 }
3195 }
Daniel Sandersa73d8fe2015-03-24 11:26:34 +00003196
3197 if (Constraint == "ZC")
3198 return C_Memory;
3199
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003200 return TargetLowering::getConstraintType(Constraint);
3201}
3202
John Thompsone8360b72010-10-29 17:29:13 +00003203/// Examine constraint type and operand type and determine a weight value.
3204/// This object must already have been set up with the operand type
3205/// and the current alternative constraint selected.
3206TargetLowering::ConstraintWeight
3207MipsTargetLowering::getSingleConstraintMatchWeight(
3208 AsmOperandInfo &info, const char *constraint) const {
3209 ConstraintWeight weight = CW_Invalid;
3210 Value *CallOperandVal = info.CallOperandVal;
3211 // If we don't have a value, we can't do a match,
3212 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +00003213 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +00003214 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +00003215 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +00003216 // Look at the constraint type.
3217 switch (*constraint) {
3218 default:
3219 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
3220 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00003221 case 'd':
3222 case 'y':
John Thompsone8360b72010-10-29 17:29:13 +00003223 if (type->isIntegerTy())
3224 weight = CW_Register;
3225 break;
Daniel Sanders8b59af12013-11-12 12:56:01 +00003226 case 'f': // FPU or MSA register
Eric Christopher1c29a652014-07-18 22:55:25 +00003227 if (Subtarget.hasMSA() && type->isVectorTy() &&
Daniel Sanders8b59af12013-11-12 12:56:01 +00003228 cast<VectorType>(type)->getBitWidth() == 128)
3229 weight = CW_Register;
3230 else if (type->isFloatTy())
John Thompsone8360b72010-10-29 17:29:13 +00003231 weight = CW_Register;
3232 break;
Eric Christophere3c494d2012-05-07 06:25:10 +00003233 case 'c': // $25 for indirect jumps
Eric Christopher9c492e62012-05-07 06:25:15 +00003234 case 'l': // lo register
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003235 case 'x': // hilo register pair
Daniel Sanders8b59af12013-11-12 12:56:01 +00003236 if (type->isIntegerTy())
Eric Christophere3c494d2012-05-07 06:25:10 +00003237 weight = CW_SpecificReg;
Daniel Sanders8b59af12013-11-12 12:56:01 +00003238 break;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003239 case 'I': // signed 16 bit immediate
Eric Christopher7201e1b2012-05-07 03:13:42 +00003240 case 'J': // integer zero
Eric Christopher3ff88a02012-05-07 05:46:29 +00003241 case 'K': // unsigned 16 bit immediate
Eric Christopher1109b342012-05-07 05:46:37 +00003242 case 'L': // signed 32 bit immediate where lower 16 bits are 0
Eric Christophere07aa432012-05-07 05:46:43 +00003243 case 'N': // immediate in the range of -65535 to -1 (inclusive)
Eric Christopher470578a2012-05-07 05:46:48 +00003244 case 'O': // signed 15 bit immediate (+- 16383)
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003245 case 'P': // immediate in the range of 65535 to 1 (inclusive)
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003246 if (isa<ConstantInt>(CallOperandVal))
3247 weight = CW_Constant;
3248 break;
Jack Carter0e149b02013-03-04 21:33:15 +00003249 case 'R':
3250 weight = CW_Memory;
3251 break;
John Thompsone8360b72010-10-29 17:29:13 +00003252 }
3253 return weight;
3254}
3255
Akira Hatanaka7473b472013-08-14 00:21:25 +00003256/// This is a helper function to parse a physical register string and split it
3257/// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag
3258/// that is returned indicates whether parsing was successful. The second flag
3259/// is true if the numeric part exists.
3260static std::pair<bool, bool>
Craig Topper6dc4a8bc2014-08-30 16:48:02 +00003261parsePhysicalReg(StringRef C, std::string &Prefix,
Akira Hatanaka7473b472013-08-14 00:21:25 +00003262 unsigned long long &Reg) {
3263 if (C.front() != '{' || C.back() != '}')
3264 return std::make_pair(false, false);
3265
3266 // Search for the first numeric character.
3267 StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1;
3268 I = std::find_if(B, E, std::ptr_fun(isdigit));
3269
3270 Prefix.assign(B, I - B);
3271
3272 // The second flag is set to false if no numeric characters were found.
3273 if (I == E)
3274 return std::make_pair(true, false);
3275
3276 // Parse the numeric characters.
3277 return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg),
3278 true);
3279}
3280
3281std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering::
Craig Topper6dc4a8bc2014-08-30 16:48:02 +00003282parseRegForInlineAsmConstraint(StringRef C, MVT VT) const {
Eric Christopherd9134482014-08-04 21:25:23 +00003283 const TargetRegisterInfo *TRI =
Eric Christopher96e72c62015-01-29 23:27:36 +00003284 Subtarget.getRegisterInfo();
Akira Hatanaka7473b472013-08-14 00:21:25 +00003285 const TargetRegisterClass *RC;
3286 std::string Prefix;
3287 unsigned long long Reg;
3288
3289 std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg);
3290
3291 if (!R.first)
Craig Topper062a2ba2014-04-25 05:30:21 +00003292 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003293
3294 if ((Prefix == "hi" || Prefix == "lo")) { // Parse hi/lo.
3295 // No numeric characters follow "hi" or "lo".
3296 if (R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003297 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003298
3299 RC = TRI->getRegClass(Prefix == "hi" ?
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00003300 Mips::HI32RegClassID : Mips::LO32RegClassID);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003301 return std::make_pair(*(RC->begin()), RC);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003302 } else if (Prefix.compare(0, 4, "$msa") == 0) {
3303 // Parse $msa(ir|csr|access|save|modify|request|map|unmap)
3304
3305 // No numeric characters follow the name.
3306 if (R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003307 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003308
3309 Reg = StringSwitch<unsigned long long>(Prefix)
3310 .Case("$msair", Mips::MSAIR)
3311 .Case("$msacsr", Mips::MSACSR)
3312 .Case("$msaaccess", Mips::MSAAccess)
3313 .Case("$msasave", Mips::MSASave)
3314 .Case("$msamodify", Mips::MSAModify)
3315 .Case("$msarequest", Mips::MSARequest)
3316 .Case("$msamap", Mips::MSAMap)
3317 .Case("$msaunmap", Mips::MSAUnmap)
3318 .Default(0);
3319
3320 if (!Reg)
Craig Topper062a2ba2014-04-25 05:30:21 +00003321 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003322
3323 RC = TRI->getRegClass(Mips::MSACtrlRegClassID);
3324 return std::make_pair(Reg, RC);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003325 }
3326
3327 if (!R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003328 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003329
3330 if (Prefix == "$f") { // Parse $f0-$f31.
3331 // If the size of FP registers is 64-bit or Reg is an even number, select
3332 // the 64-bit register class. Otherwise, select the 32-bit register class.
3333 if (VT == MVT::Other)
Eric Christopher1c29a652014-07-18 22:55:25 +00003334 VT = (Subtarget.isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32;
Akira Hatanaka7473b472013-08-14 00:21:25 +00003335
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003336 RC = getRegClassFor(VT);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003337
3338 if (RC == &Mips::AFGR64RegClass) {
3339 assert(Reg % 2 == 0);
3340 Reg >>= 1;
3341 }
Daniel Sanders8b59af12013-11-12 12:56:01 +00003342 } else if (Prefix == "$fcc") // Parse $fcc0-$fcc7.
Akira Hatanaka7473b472013-08-14 00:21:25 +00003343 RC = TRI->getRegClass(Mips::FCCRegClassID);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003344 else if (Prefix == "$w") { // Parse $w0-$w31.
3345 RC = getRegClassFor((VT == MVT::Other) ? MVT::v16i8 : VT);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003346 } else { // Parse $0-$31.
3347 assert(Prefix == "$");
3348 RC = getRegClassFor((VT == MVT::Other) ? MVT::i32 : VT);
3349 }
3350
3351 assert(Reg < RC->getNumRegs());
3352 return std::make_pair(*(RC->begin() + Reg), RC);
3353}
3354
Eric Christophereaf77dc2011-06-29 19:33:04 +00003355/// Given a register class constraint, like 'r', if this corresponds directly
3356/// to an LLVM register class, return a register of 0 and the register class
3357/// pointer.
Eric Christopher11e4df72015-02-26 22:38:43 +00003358std::pair<unsigned, const TargetRegisterClass *>
3359MipsTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
3360 const std::string &Constraint,
3361 MVT VT) const {
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003362 if (Constraint.size() == 1) {
3363 switch (Constraint[0]) {
Eric Christopher9519c082011-06-29 19:04:31 +00003364 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
3365 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003366 case 'r':
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003367 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
Eric Christopher1c29a652014-07-18 22:55:25 +00003368 if (Subtarget.inMips16Mode())
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003369 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003370 return std::make_pair(0U, &Mips::GPR32RegClass);
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003371 }
Eric Christopher1c29a652014-07-18 22:55:25 +00003372 if (VT == MVT::i64 && !Subtarget.isGP64bit())
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003373 return std::make_pair(0U, &Mips::GPR32RegClass);
Eric Christopher1c29a652014-07-18 22:55:25 +00003374 if (VT == MVT::i64 && Subtarget.isGP64bit())
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003375 return std::make_pair(0U, &Mips::GPR64RegClass);
Eric Christopher58daf042012-05-07 03:13:22 +00003376 // This will generate an error message
Craig Topper062a2ba2014-04-25 05:30:21 +00003377 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003378 case 'f': // FPU or MSA register
3379 if (VT == MVT::v16i8)
3380 return std::make_pair(0U, &Mips::MSA128BRegClass);
3381 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
3382 return std::make_pair(0U, &Mips::MSA128HRegClass);
3383 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
3384 return std::make_pair(0U, &Mips::MSA128WRegClass);
3385 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
3386 return std::make_pair(0U, &Mips::MSA128DRegClass);
3387 else if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00003388 return std::make_pair(0U, &Mips::FGR32RegClass);
Eric Christopher1c29a652014-07-18 22:55:25 +00003389 else if ((VT == MVT::f64) && (!Subtarget.isSingleFloat())) {
3390 if (Subtarget.isFP64bit())
Craig Topperc7242e02012-04-20 07:30:17 +00003391 return std::make_pair(0U, &Mips::FGR64RegClass);
3392 return std::make_pair(0U, &Mips::AFGR64RegClass);
Akira Hatanakac669d7a2012-01-04 02:45:01 +00003393 }
Eric Christophere3c494d2012-05-07 06:25:10 +00003394 break;
3395 case 'c': // register suitable for indirect jump
3396 if (VT == MVT::i32)
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003397 return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass);
Eric Christophere3c494d2012-05-07 06:25:10 +00003398 assert(VT == MVT::i64 && "Unexpected type.");
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003399 return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass);
Eric Christopher9c492e62012-05-07 06:25:15 +00003400 case 'l': // register suitable for indirect jump
3401 if (VT == MVT::i32)
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00003402 return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass);
3403 return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass);
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003404 case 'x': // register suitable for indirect jump
3405 // Fixme: Not triggering the use of both hi and low
3406 // This will generate an error message
Craig Topper062a2ba2014-04-25 05:30:21 +00003407 return std::make_pair(0U, nullptr);
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003408 }
3409 }
Akira Hatanaka7473b472013-08-14 00:21:25 +00003410
3411 std::pair<unsigned, const TargetRegisterClass *> R;
3412 R = parseRegForInlineAsmConstraint(Constraint, VT);
3413
3414 if (R.second)
3415 return R;
3416
Eric Christopher11e4df72015-02-26 22:38:43 +00003417 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003418}
3419
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003420/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3421/// vector. If it is invalid, don't add anything to Ops.
3422void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3423 std::string &Constraint,
3424 std::vector<SDValue>&Ops,
3425 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +00003426 SDValue Result;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003427
3428 // Only support length 1 constraints for now.
3429 if (Constraint.length() > 1) return;
3430
3431 char ConstraintLetter = Constraint[0];
3432 switch (ConstraintLetter) {
3433 default: break; // This will fall through to the generic implementation
3434 case 'I': // Signed 16 bit constant
3435 // If this fails, the parent routine will give an error
3436 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3437 EVT Type = Op.getValueType();
3438 int64_t Val = C->getSExtValue();
3439 if (isInt<16>(Val)) {
3440 Result = DAG.getTargetConstant(Val, Type);
3441 break;
3442 }
3443 }
3444 return;
Eric Christopher7201e1b2012-05-07 03:13:42 +00003445 case 'J': // integer zero
3446 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3447 EVT Type = Op.getValueType();
3448 int64_t Val = C->getZExtValue();
3449 if (Val == 0) {
3450 Result = DAG.getTargetConstant(0, Type);
3451 break;
3452 }
3453 }
3454 return;
Eric Christopher3ff88a02012-05-07 05:46:29 +00003455 case 'K': // unsigned 16 bit immediate
3456 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3457 EVT Type = Op.getValueType();
3458 uint64_t Val = (uint64_t)C->getZExtValue();
3459 if (isUInt<16>(Val)) {
3460 Result = DAG.getTargetConstant(Val, Type);
3461 break;
3462 }
3463 }
3464 return;
Eric Christopher1109b342012-05-07 05:46:37 +00003465 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3466 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3467 EVT Type = Op.getValueType();
3468 int64_t Val = C->getSExtValue();
3469 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
3470 Result = DAG.getTargetConstant(Val, Type);
3471 break;
3472 }
3473 }
3474 return;
Eric Christophere07aa432012-05-07 05:46:43 +00003475 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3476 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3477 EVT Type = Op.getValueType();
3478 int64_t Val = C->getSExtValue();
3479 if ((Val >= -65535) && (Val <= -1)) {
3480 Result = DAG.getTargetConstant(Val, Type);
3481 break;
3482 }
3483 }
3484 return;
Eric Christopher470578a2012-05-07 05:46:48 +00003485 case 'O': // signed 15 bit immediate
3486 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3487 EVT Type = Op.getValueType();
3488 int64_t Val = C->getSExtValue();
3489 if ((isInt<15>(Val))) {
3490 Result = DAG.getTargetConstant(Val, Type);
3491 break;
3492 }
3493 }
3494 return;
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003495 case 'P': // immediate in the range of 1 to 65535 (inclusive)
3496 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3497 EVT Type = Op.getValueType();
3498 int64_t Val = C->getSExtValue();
3499 if ((Val <= 65535) && (Val >= 1)) {
3500 Result = DAG.getTargetConstant(Val, Type);
3501 break;
3502 }
3503 }
3504 return;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003505 }
3506
3507 if (Result.getNode()) {
3508 Ops.push_back(Result);
3509 return;
3510 }
3511
3512 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3513}
3514
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003515bool MipsTargetLowering::isLegalAddressingMode(const AddrMode &AM,
3516 Type *Ty) const {
Akira Hatanakaef839192012-11-17 00:25:41 +00003517 // No global is ever allowed as a base.
3518 if (AM.BaseGV)
3519 return false;
3520
3521 switch (AM.Scale) {
3522 case 0: // "r+i" or just "i", depending on HasBaseReg.
3523 break;
3524 case 1:
3525 if (!AM.HasBaseReg) // allow "r+i".
3526 break;
3527 return false; // disallow "r+r" or "r+r+i".
3528 default:
3529 return false;
3530 }
3531
3532 return true;
3533}
3534
3535bool
Dan Gohman2fe6bee2008-10-18 02:06:02 +00003536MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3537 // The Mips target isn't yet aware of offsets.
3538 return false;
3539}
Evan Cheng16993aa2009-10-27 19:56:55 +00003540
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003541EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003542 unsigned SrcAlign,
3543 bool IsMemset, bool ZeroMemset,
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003544 bool MemcpyStrSrc,
3545 MachineFunction &MF) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003546 if (Subtarget.hasMips64())
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003547 return MVT::i64;
3548
3549 return MVT::i32;
3550}
3551
Evan Cheng83896a52009-10-28 01:43:28 +00003552bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
3553 if (VT != MVT::f32 && VT != MVT::f64)
3554 return false;
Bruno Cardoso Lopesb02a9df2011-01-18 19:41:41 +00003555 if (Imm.isNegZero())
3556 return false;
Evan Cheng16993aa2009-10-27 19:56:55 +00003557 return Imm.isZero();
3558}
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003559
3560unsigned MipsTargetLowering::getJumpTableEncoding() const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003561 if (ABI.IsN64())
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003562 return MachineJumpTableInfo::EK_GPRel64BlockAddress;
Jia Liuf54f60f2012-02-28 07:46:26 +00003563
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003564 return TargetLowering::getJumpTableEncoding();
3565}
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003566
Daniel Sandersf43e6872014-11-01 18:44:56 +00003567void MipsTargetLowering::copyByValRegs(
3568 SDValue Chain, SDLoc DL, std::vector<SDValue> &OutChains, SelectionDAG &DAG,
3569 const ISD::ArgFlagsTy &Flags, SmallVectorImpl<SDValue> &InVals,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003570 const Argument *FuncArg, unsigned FirstReg, unsigned LastReg,
3571 const CCValAssign &VA, MipsCCState &State) const {
Akira Hatanaka25dad192012-10-27 00:10:18 +00003572 MachineFunction &MF = DAG.getMachineFunction();
3573 MachineFrameInfo *MFI = MF.getFrameInfo();
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003574 unsigned GPRSizeInBytes = Subtarget.getGPRSizeInBytes();
Daniel Sanders23e98772014-11-02 16:09:29 +00003575 unsigned NumRegs = LastReg - FirstReg;
3576 unsigned RegAreaSize = NumRegs * GPRSizeInBytes;
Akira Hatanaka25dad192012-10-27 00:10:18 +00003577 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
3578 int FrameObjOffset;
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003579 ArrayRef<MCPhysReg> ByValArgRegs = ABI.GetByValArgRegs();
Akira Hatanaka25dad192012-10-27 00:10:18 +00003580
3581 if (RegAreaSize)
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003582 FrameObjOffset =
3583 (int)ABI.GetCalleeAllocdArgSizeInBytes(State.getCallingConv()) -
3584 (int)((ByValArgRegs.size() - FirstReg) * GPRSizeInBytes);
Akira Hatanaka25dad192012-10-27 00:10:18 +00003585 else
Daniel Sandersf43e6872014-11-01 18:44:56 +00003586 FrameObjOffset = VA.getLocMemOffset();
Akira Hatanaka25dad192012-10-27 00:10:18 +00003587
3588 // Create frame object.
3589 EVT PtrTy = getPointerTy();
3590 int FI = MFI->CreateFixedObject(FrameObjSize, FrameObjOffset, true);
3591 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
3592 InVals.push_back(FIN);
3593
Daniel Sanders23e98772014-11-02 16:09:29 +00003594 if (!NumRegs)
Akira Hatanaka25dad192012-10-27 00:10:18 +00003595 return;
3596
3597 // Copy arg registers.
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003598 MVT RegTy = MVT::getIntegerVT(GPRSizeInBytes * 8);
Akira Hatanaka25dad192012-10-27 00:10:18 +00003599 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3600
Daniel Sanders23e98772014-11-02 16:09:29 +00003601 for (unsigned I = 0; I < NumRegs; ++I) {
Daniel Sandersd7eba312014-11-07 12:21:37 +00003602 unsigned ArgReg = ByValArgRegs[FirstReg + I];
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003603 unsigned VReg = addLiveIn(MF, ArgReg, RC);
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003604 unsigned Offset = I * GPRSizeInBytes;
Akira Hatanaka25dad192012-10-27 00:10:18 +00003605 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
3606 DAG.getConstant(Offset, PtrTy));
3607 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
3608 StorePtr, MachinePointerInfo(FuncArg, Offset),
3609 false, false, 0);
3610 OutChains.push_back(Store);
3611 }
3612}
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003613
3614// Copy byVal arg to registers and stack.
Daniel Sandersf43e6872014-11-01 18:44:56 +00003615void MipsTargetLowering::passByValArg(
3616 SDValue Chain, SDLoc DL,
3617 std::deque<std::pair<unsigned, SDValue>> &RegsToPass,
3618 SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003619 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg, unsigned FirstReg,
3620 unsigned LastReg, const ISD::ArgFlagsTy &Flags, bool isLittle,
3621 const CCValAssign &VA) const {
Daniel Sandersac272632014-05-23 13:18:02 +00003622 unsigned ByValSizeInBytes = Flags.getByValSize();
3623 unsigned OffsetInBytes = 0; // From beginning of struct
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003624 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
Daniel Sandersac272632014-05-23 13:18:02 +00003625 unsigned Alignment = std::min(Flags.getByValAlign(), RegSizeInBytes);
3626 EVT PtrTy = getPointerTy(), RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
Daniel Sanders23e98772014-11-02 16:09:29 +00003627 unsigned NumRegs = LastReg - FirstReg;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003628
Daniel Sanders23e98772014-11-02 16:09:29 +00003629 if (NumRegs) {
Eric Christopher96e72c62015-01-29 23:27:36 +00003630 const ArrayRef<MCPhysReg> ArgRegs = ABI.GetByValArgRegs();
Daniel Sanders23e98772014-11-02 16:09:29 +00003631 bool LeftoverBytes = (NumRegs * RegSizeInBytes > ByValSizeInBytes);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003632 unsigned I = 0;
3633
3634 // Copy words to registers.
Daniel Sanders23e98772014-11-02 16:09:29 +00003635 for (; I < NumRegs - LeftoverBytes; ++I, OffsetInBytes += RegSizeInBytes) {
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003636 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Daniel Sandersac272632014-05-23 13:18:02 +00003637 DAG.getConstant(OffsetInBytes, PtrTy));
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003638 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
3639 MachinePointerInfo(), false, false, false,
3640 Alignment);
3641 MemOpChains.push_back(LoadVal.getValue(1));
Daniel Sanders23e98772014-11-02 16:09:29 +00003642 unsigned ArgReg = ArgRegs[FirstReg + I];
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003643 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
3644 }
3645
3646 // Return if the struct has been fully copied.
Daniel Sandersac272632014-05-23 13:18:02 +00003647 if (ByValSizeInBytes == OffsetInBytes)
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003648 return;
3649
3650 // Copy the remainder of the byval argument with sub-word loads and shifts.
3651 if (LeftoverBytes) {
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003652 SDValue Val;
3653
Daniel Sandersac272632014-05-23 13:18:02 +00003654 for (unsigned LoadSizeInBytes = RegSizeInBytes / 2, TotalBytesLoaded = 0;
3655 OffsetInBytes < ByValSizeInBytes; LoadSizeInBytes /= 2) {
3656 unsigned RemainingSizeInBytes = ByValSizeInBytes - OffsetInBytes;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003657
Daniel Sandersac272632014-05-23 13:18:02 +00003658 if (RemainingSizeInBytes < LoadSizeInBytes)
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003659 continue;
3660
3661 // Load subword.
3662 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Daniel Sandersac272632014-05-23 13:18:02 +00003663 DAG.getConstant(OffsetInBytes, PtrTy));
3664 SDValue LoadVal = DAG.getExtLoad(
3665 ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr, MachinePointerInfo(),
Louis Gerbarg67474e32014-07-31 21:45:05 +00003666 MVT::getIntegerVT(LoadSizeInBytes * 8), false, false, false,
3667 Alignment);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003668 MemOpChains.push_back(LoadVal.getValue(1));
3669
3670 // Shift the loaded value.
3671 unsigned Shamt;
3672
3673 if (isLittle)
Daniel Sandersac272632014-05-23 13:18:02 +00003674 Shamt = TotalBytesLoaded * 8;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003675 else
Daniel Sandersac272632014-05-23 13:18:02 +00003676 Shamt = (RegSizeInBytes - (TotalBytesLoaded + LoadSizeInBytes)) * 8;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003677
3678 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
3679 DAG.getConstant(Shamt, MVT::i32));
3680
3681 if (Val.getNode())
3682 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
3683 else
3684 Val = Shift;
3685
Daniel Sandersac272632014-05-23 13:18:02 +00003686 OffsetInBytes += LoadSizeInBytes;
3687 TotalBytesLoaded += LoadSizeInBytes;
3688 Alignment = std::min(Alignment, LoadSizeInBytes);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003689 }
3690
Daniel Sanders23e98772014-11-02 16:09:29 +00003691 unsigned ArgReg = ArgRegs[FirstReg + I];
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003692 RegsToPass.push_back(std::make_pair(ArgReg, Val));
3693 return;
3694 }
3695 }
3696
3697 // Copy remainder of byval arg to it with memcpy.
Daniel Sandersac272632014-05-23 13:18:02 +00003698 unsigned MemCpySize = ByValSizeInBytes - OffsetInBytes;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003699 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Daniel Sandersac272632014-05-23 13:18:02 +00003700 DAG.getConstant(OffsetInBytes, PtrTy));
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003701 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
Daniel Sandersf43e6872014-11-01 18:44:56 +00003702 DAG.getIntPtrConstant(VA.getLocMemOffset()));
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003703 Chain = DAG.getMemcpy(Chain, DL, Dst, Src, DAG.getConstant(MemCpySize, PtrTy),
3704 Alignment, /*isVolatile=*/false, /*AlwaysInline=*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00003705 /*isTailCall=*/false,
Nick Lewyckyaad475b2014-04-15 07:22:52 +00003706 MachinePointerInfo(), MachinePointerInfo());
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003707 MemOpChains.push_back(Chain);
3708}
Akira Hatanaka2a134022012-10-27 00:21:13 +00003709
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003710void MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003711 SDValue Chain, SDLoc DL,
3712 SelectionDAG &DAG,
Daniel Sanders853c2432014-11-01 18:13:52 +00003713 CCState &State) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003714 const ArrayRef<MCPhysReg> ArgRegs = ABI.GetVarArgRegs();
Tim Northover3b6b7ca2015-02-21 02:11:17 +00003715 unsigned Idx = State.getFirstUnallocated(ArgRegs);
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003716 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
3717 MVT RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003718 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3719 MachineFunction &MF = DAG.getMachineFunction();
3720 MachineFrameInfo *MFI = MF.getFrameInfo();
3721 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3722
3723 // Offset of the first variable argument from stack pointer.
3724 int VaArgOffset;
3725
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003726 if (ArgRegs.size() == Idx)
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003727 VaArgOffset =
Daniel Sanders853c2432014-11-01 18:13:52 +00003728 RoundUpToAlignment(State.getNextStackOffset(), RegSizeInBytes);
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003729 else {
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003730 VaArgOffset =
3731 (int)ABI.GetCalleeAllocdArgSizeInBytes(State.getCallingConv()) -
3732 (int)(RegSizeInBytes * (ArgRegs.size() - Idx));
3733 }
Akira Hatanaka2a134022012-10-27 00:21:13 +00003734
3735 // Record the frame index of the first variable argument
3736 // which is a value necessary to VASTART.
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003737 int FI = MFI->CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003738 MipsFI->setVarArgsFrameIndex(FI);
3739
3740 // Copy the integer registers that have not been used for argument passing
3741 // to the argument register save area. For O32, the save area is allocated
3742 // in the caller's stack frame, while for N32/64, it is allocated in the
3743 // callee's stack frame.
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003744 for (unsigned I = Idx; I < ArgRegs.size();
3745 ++I, VaArgOffset += RegSizeInBytes) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003746 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003747 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003748 FI = MFI->CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003749 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
3750 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
3751 MachinePointerInfo(), false, false, 0);
Eric Christopher1c29a652014-07-18 22:55:25 +00003752 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(
3753 (Value *)nullptr);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003754 OutChains.push_back(Store);
3755 }
3756}
Daniel Sanders23e98772014-11-02 16:09:29 +00003757
3758void MipsTargetLowering::HandleByVal(CCState *State, unsigned &Size,
3759 unsigned Align) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003760 const TargetFrameLowering *TFL = Subtarget.getFrameLowering();
Daniel Sanders23e98772014-11-02 16:09:29 +00003761
3762 assert(Size && "Byval argument's size shouldn't be 0.");
3763
3764 Align = std::min(Align, TFL->getStackAlignment());
3765
3766 unsigned FirstReg = 0;
3767 unsigned NumRegs = 0;
3768
3769 if (State->getCallingConv() != CallingConv::Fast) {
3770 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
Eric Christopher96e72c62015-01-29 23:27:36 +00003771 const ArrayRef<MCPhysReg> IntArgRegs = ABI.GetByValArgRegs();
Daniel Sanders23e98772014-11-02 16:09:29 +00003772 // FIXME: The O32 case actually describes no shadow registers.
3773 const MCPhysReg *ShadowRegs =
Eric Christopher96e72c62015-01-29 23:27:36 +00003774 ABI.IsO32() ? IntArgRegs.data() : Mips64DPRegs;
Daniel Sanders23e98772014-11-02 16:09:29 +00003775
3776 // We used to check the size as well but we can't do that anymore since
3777 // CCState::HandleByVal() rounds up the size after calling this function.
3778 assert(!(Align % RegSizeInBytes) &&
3779 "Byval argument's alignment should be a multiple of"
3780 "RegSizeInBytes.");
3781
Tim Northover3b6b7ca2015-02-21 02:11:17 +00003782 FirstReg = State->getFirstUnallocated(IntArgRegs);
Daniel Sanders23e98772014-11-02 16:09:29 +00003783
3784 // If Align > RegSizeInBytes, the first arg register must be even.
3785 // FIXME: This condition happens to do the right thing but it's not the
3786 // right way to test it. We want to check that the stack frame offset
3787 // of the register is aligned.
3788 if ((Align > RegSizeInBytes) && (FirstReg % 2)) {
3789 State->AllocateReg(IntArgRegs[FirstReg], ShadowRegs[FirstReg]);
3790 ++FirstReg;
3791 }
3792
3793 // Mark the registers allocated.
3794 Size = RoundUpToAlignment(Size, RegSizeInBytes);
3795 for (unsigned I = FirstReg; Size > 0 && (I < IntArgRegs.size());
3796 Size -= RegSizeInBytes, ++I, ++NumRegs)
3797 State->AllocateReg(IntArgRegs[I], ShadowRegs[I]);
3798 }
3799
3800 State->addInRegsParamInfo(FirstReg, FirstReg + NumRegs);
3801}
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00003802
3803MachineBasicBlock *
3804MipsTargetLowering::emitPseudoSELECT(MachineInstr *MI, MachineBasicBlock *BB,
3805 bool isFPCmp, unsigned Opc) const {
3806 assert(!(Subtarget.hasMips4() || Subtarget.hasMips32()) &&
3807 "Subtarget already supports SELECT nodes with the use of"
3808 "conditional-move instructions.");
3809
3810 const TargetInstrInfo *TII =
Eric Christopher96e72c62015-01-29 23:27:36 +00003811 Subtarget.getInstrInfo();
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00003812 DebugLoc DL = MI->getDebugLoc();
3813
3814 // To "insert" a SELECT instruction, we actually have to insert the
3815 // diamond control-flow pattern. The incoming instruction knows the
3816 // destination vreg to set, the condition code register to branch on, the
3817 // true/false values to select between, and a branch opcode to use.
3818 const BasicBlock *LLVM_BB = BB->getBasicBlock();
3819 MachineFunction::iterator It = BB;
3820 ++It;
3821
3822 // thisMBB:
3823 // ...
3824 // TrueVal = ...
3825 // setcc r1, r2, r3
3826 // bNE r1, r0, copy1MBB
3827 // fallthrough --> copy0MBB
3828 MachineBasicBlock *thisMBB = BB;
3829 MachineFunction *F = BB->getParent();
3830 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
3831 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
3832 F->insert(It, copy0MBB);
3833 F->insert(It, sinkMBB);
3834
3835 // Transfer the remainder of BB and its successor edges to sinkMBB.
3836 sinkMBB->splice(sinkMBB->begin(), BB,
3837 std::next(MachineBasicBlock::iterator(MI)), BB->end());
3838 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
3839
3840 // Next, add the true and fallthrough blocks as its successors.
3841 BB->addSuccessor(copy0MBB);
3842 BB->addSuccessor(sinkMBB);
3843
3844 if (isFPCmp) {
3845 // bc1[tf] cc, sinkMBB
3846 BuildMI(BB, DL, TII->get(Opc))
3847 .addReg(MI->getOperand(1).getReg())
3848 .addMBB(sinkMBB);
3849 } else {
3850 // bne rs, $0, sinkMBB
3851 BuildMI(BB, DL, TII->get(Opc))
3852 .addReg(MI->getOperand(1).getReg())
3853 .addReg(Mips::ZERO)
3854 .addMBB(sinkMBB);
3855 }
3856
3857 // copy0MBB:
3858 // %FalseValue = ...
3859 // # fallthrough to sinkMBB
3860 BB = copy0MBB;
3861
3862 // Update machine-CFG edges
3863 BB->addSuccessor(sinkMBB);
3864
3865 // sinkMBB:
3866 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
3867 // ...
3868 BB = sinkMBB;
3869
3870 BuildMI(*BB, BB->begin(), DL,
3871 TII->get(Mips::PHI), MI->getOperand(0).getReg())
3872 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
3873 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB);
3874
3875 MI->eraseFromParent(); // The pseudo instruction is gone now.
3876
3877 return BB;
3878}
Daniel Sanders1440bb22015-01-09 17:21:30 +00003879
3880// FIXME? Maybe this could be a TableGen attribute on some registers and
3881// this table could be generated automatically from RegInfo.
3882unsigned MipsTargetLowering::getRegisterByName(const char* RegName,
3883 EVT VT) const {
3884 // Named registers is expected to be fairly rare. For now, just support $28
3885 // since the linux kernel uses it.
3886 if (Subtarget.isGP64bit()) {
3887 unsigned Reg = StringSwitch<unsigned>(RegName)
3888 .Case("$28", Mips::GP_64)
3889 .Default(0);
3890 if (Reg)
3891 return Reg;
3892 } else {
3893 unsigned Reg = StringSwitch<unsigned>(RegName)
3894 .Case("$28", Mips::GP)
3895 .Default(0);
3896 if (Reg)
3897 return Reg;
3898 }
3899 report_fatal_error("Invalid register name global variable");
3900}