blob: 8bc216021ad334ad01a03331fbc22f303f2e266d [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"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "MipsMachineFunction.h"
18#include "MipsSubtarget.h"
19#include "MipsTargetMachine.h"
20#include "MipsTargetObjectFile.h"
Akira Hatanaka90131ac2012-10-19 21:47:33 +000021#include "llvm/ADT/Statistic.h"
Daniel Sanders8b59af12013-11-12 12:56:01 +000022#include "llvm/ADT/StringSwitch.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000023#include "llvm/CodeGen/CallingConvLower.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/MachineFunction.h"
26#include "llvm/CodeGen/MachineInstrBuilder.h"
Eric Christopher79cc1e32014-09-02 22:28:02 +000027#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000028#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000029#include "llvm/CodeGen/SelectionDAGISel.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000030#include "llvm/CodeGen/ValueTypes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000031#include "llvm/IR/CallingConv.h"
32#include "llvm/IR/DerivedTypes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000033#include "llvm/IR/GlobalVariable.h"
Akira Hatanaka90131ac2012-10-19 21:47:33 +000034#include "llvm/Support/CommandLine.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000035#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000036#include "llvm/Support/ErrorHandling.h"
NAKAMURA Takumie30303f2012-04-21 15:31:45 +000037#include "llvm/Support/raw_ostream.h"
Akira Hatanaka7473b472013-08-14 00:21:25 +000038#include <cctype>
NAKAMURA Takumie30303f2012-04-21 15:31:45 +000039
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000040using namespace llvm;
41
Chandler Carruth84e68b22014-04-22 02:41:26 +000042#define DEBUG_TYPE "mips-lower"
43
Akira Hatanaka90131ac2012-10-19 21:47:33 +000044STATISTIC(NumTailCalls, "Number of tail calls");
45
46static cl::opt<bool>
Akira Hatanaka59f299f2012-11-21 20:21:11 +000047LargeGOT("mxgot", cl::Hidden,
48 cl::desc("MIPS: Enable GOT larger than 64k."), cl::init(false));
49
Akira Hatanaka1cb02422013-05-20 18:07:43 +000050static cl::opt<bool>
Akira Hatanakabe76cd02013-05-21 17:17:59 +000051NoZeroDivCheck("mno-check-zero-division", cl::Hidden,
Akira Hatanaka1cb02422013-05-20 18:07:43 +000052 cl::desc("MIPS: Don't trap on integer division by zero."),
53 cl::init(false));
54
Reed Kotler720c5ca2014-04-17 22:15:34 +000055cl::opt<bool>
56EnableMipsFastISel("mips-fast-isel", cl::Hidden,
57 cl::desc("Allow mips-fast-isel to be used"),
58 cl::init(false));
59
Craig Topper840beec2014-04-04 05:16:06 +000060static const MCPhysReg O32IntRegs[4] = {
Akira Hatanakaac8c6692012-10-27 00:29:43 +000061 Mips::A0, Mips::A1, Mips::A2, Mips::A3
62};
63
Craig Topper840beec2014-04-04 05:16:06 +000064static const MCPhysReg Mips64IntRegs[8] = {
Akira Hatanakaac8c6692012-10-27 00:29:43 +000065 Mips::A0_64, Mips::A1_64, Mips::A2_64, Mips::A3_64,
66 Mips::T0_64, Mips::T1_64, Mips::T2_64, Mips::T3_64
67};
68
Craig Topper840beec2014-04-04 05:16:06 +000069static const MCPhysReg Mips64DPRegs[8] = {
Akira Hatanakaac8c6692012-10-27 00:29:43 +000070 Mips::D12_64, Mips::D13_64, Mips::D14_64, Mips::D15_64,
71 Mips::D16_64, Mips::D17_64, Mips::D18_64, Mips::D19_64
72};
73
Daniel Sandersb3ca3382014-09-26 10:06:12 +000074static bool originalTypeIsF128(const Type *Ty, const SDNode *CallNode);
75
76namespace {
77class MipsCCState : public CCState {
78private:
79 /// Identify lowered values that originated from f128 arguments and record
80 /// this for use by RetCC_MipsN.
81 void
82 PreAnalyzeCallResultForF128(const SmallVectorImpl<ISD::InputArg> &Ins,
83 const TargetLowering::CallLoweringInfo &CLI) {
Daniel Sandersb3ca3382014-09-26 10:06:12 +000084 for (unsigned i = 0; i < Ins.size(); ++i)
85 OriginalArgWasF128.push_back(
86 originalTypeIsF128(CLI.RetTy, CLI.Callee.getNode()));
87 }
88
89 /// Identify lowered values that originated from f128 arguments and record
90 /// this for use by RetCC_MipsN.
91 void PreAnalyzeReturnForF128(const SmallVectorImpl<ISD::OutputArg> &Outs) {
92 const MachineFunction &MF = getMachineFunction();
93 for (unsigned i = 0; i < Outs.size(); ++i)
94 OriginalArgWasF128.push_back(
95 originalTypeIsF128(MF.getFunction()->getReturnType(), nullptr));
96 }
97
98 /// Records whether the value has been lowered from an f128.
99 SmallVector<bool, 4> OriginalArgWasF128;
100
101public:
102 MipsCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
103 SmallVectorImpl<CCValAssign> &locs, LLVMContext &C)
104 : CCState(CC, isVarArg, MF, locs, C) {}
105
106 void AnalyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins,
107 CCAssignFn Fn,
108 const TargetLowering::CallLoweringInfo &CLI) {
109 PreAnalyzeCallResultForF128(Ins, CLI);
110 CCState::AnalyzeCallResult(Ins, Fn);
111 OriginalArgWasF128.clear();
112 }
113
114 void AnalyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
115 CCAssignFn Fn) {
116 PreAnalyzeReturnForF128(Outs);
117 CCState::AnalyzeReturn(Outs, Fn);
118 OriginalArgWasF128.clear();
119 }
120
121 bool CheckReturn(const SmallVectorImpl<ISD::OutputArg> &ArgsFlags,
122 CCAssignFn Fn) {
123 PreAnalyzeReturnForF128(ArgsFlags);
124 bool Return = CCState::CheckReturn(ArgsFlags, Fn);
125 OriginalArgWasF128.clear();
126 return Return;
127 }
128
129 bool WasOriginalArgF128(unsigned ValNo) { return OriginalArgWasF128[ValNo]; }
130};
131}
132
Jia Liuf54f60f2012-02-28 07:46:26 +0000133// If I is a shifted mask, set the size (Size) and the first bit of the
Akira Hatanaka73d78b72011-08-18 20:07:42 +0000134// mask (Pos), and return true.
Jia Liuf54f60f2012-02-28 07:46:26 +0000135// For example, if I is 0x003ff800, (Pos, Size) = (11, 11).
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000136static bool isShiftedMask(uint64_t I, uint64_t &Pos, uint64_t &Size) {
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000137 if (!isShiftedMask_64(I))
Akira Hatanaka4c0a7122013-10-07 19:33:02 +0000138 return false;
Akira Hatanaka5360f882011-08-17 02:05:42 +0000139
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000140 Size = CountPopulation_64(I);
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000141 Pos = countTrailingZeros(I);
Akira Hatanaka73d78b72011-08-18 20:07:42 +0000142 return true;
Akira Hatanaka5360f882011-08-17 02:05:42 +0000143}
144
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000145SDValue MipsTargetLowering::getGlobalReg(SelectionDAG &DAG, EVT Ty) const {
Akira Hatanakab049aef2012-02-24 22:34:47 +0000146 MipsFunctionInfo *FI = DAG.getMachineFunction().getInfo<MipsFunctionInfo>();
147 return DAG.getRegister(FI->getGlobalBaseReg(), Ty);
148}
149
Akira Hatanakad8f10ce2013-09-27 19:51:35 +0000150SDValue MipsTargetLowering::getTargetNode(GlobalAddressSDNode *N, EVT Ty,
151 SelectionDAG &DAG,
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000152 unsigned Flag) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +0000153 return DAG.getTargetGlobalAddress(N->getGlobal(), SDLoc(N), Ty, 0, Flag);
Akira Hatanakafd04ad42012-11-21 20:26:38 +0000154}
155
Akira Hatanakad8f10ce2013-09-27 19:51:35 +0000156SDValue MipsTargetLowering::getTargetNode(ExternalSymbolSDNode *N, EVT Ty,
157 SelectionDAG &DAG,
158 unsigned Flag) const {
159 return DAG.getTargetExternalSymbol(N->getSymbol(), Ty, Flag);
160}
161
162SDValue MipsTargetLowering::getTargetNode(BlockAddressSDNode *N, EVT Ty,
163 SelectionDAG &DAG,
164 unsigned Flag) const {
165 return DAG.getTargetBlockAddress(N->getBlockAddress(), Ty, 0, Flag);
166}
167
168SDValue MipsTargetLowering::getTargetNode(JumpTableSDNode *N, EVT Ty,
169 SelectionDAG &DAG,
170 unsigned Flag) const {
171 return DAG.getTargetJumpTable(N->getIndex(), Ty, Flag);
172}
173
174SDValue MipsTargetLowering::getTargetNode(ConstantPoolSDNode *N, EVT Ty,
175 SelectionDAG &DAG,
176 unsigned Flag) const {
177 return DAG.getTargetConstantPool(N->getConstVal(), Ty, N->getAlignment(),
178 N->getOffset(), Flag);
Akira Hatanakafd04ad42012-11-21 20:26:38 +0000179}
180
Chris Lattner5e693ed2009-07-28 03:13:23 +0000181const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
182 switch (Opcode) {
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000183 case MipsISD::JmpLink: return "MipsISD::JmpLink";
Akira Hatanaka91318df2012-10-19 20:59:39 +0000184 case MipsISD::TailCall: return "MipsISD::TailCall";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000185 case MipsISD::Hi: return "MipsISD::Hi";
186 case MipsISD::Lo: return "MipsISD::Lo";
187 case MipsISD::GPRel: return "MipsISD::GPRel";
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +0000188 case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000189 case MipsISD::Ret: return "MipsISD::Ret";
Akira Hatanakac0b02062013-01-30 00:26:49 +0000190 case MipsISD::EH_RETURN: return "MipsISD::EH_RETURN";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000191 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
192 case MipsISD::FPCmp: return "MipsISD::FPCmp";
193 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
194 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000195 case MipsISD::TruncIntFP: return "MipsISD::TruncIntFP";
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000196 case MipsISD::MFHI: return "MipsISD::MFHI";
197 case MipsISD::MFLO: return "MipsISD::MFLO";
198 case MipsISD::MTLOHI: return "MipsISD::MTLOHI";
Akira Hatanaka28721bd2013-03-30 01:14:04 +0000199 case MipsISD::Mult: return "MipsISD::Mult";
200 case MipsISD::Multu: return "MipsISD::Multu";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000201 case MipsISD::MAdd: return "MipsISD::MAdd";
202 case MipsISD::MAddu: return "MipsISD::MAddu";
203 case MipsISD::MSub: return "MipsISD::MSub";
204 case MipsISD::MSubu: return "MipsISD::MSubu";
205 case MipsISD::DivRem: return "MipsISD::DivRem";
206 case MipsISD::DivRemU: return "MipsISD::DivRemU";
Akira Hatanaka28721bd2013-03-30 01:14:04 +0000207 case MipsISD::DivRem16: return "MipsISD::DivRem16";
208 case MipsISD::DivRemU16: return "MipsISD::DivRemU16";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000209 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
210 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
Akira Hatanakafaa88c02011-12-12 22:38:19 +0000211 case MipsISD::Wrapper: return "MipsISD::Wrapper";
Akira Hatanakaa4c09bc2011-07-19 23:30:50 +0000212 case MipsISD::Sync: return "MipsISD::Sync";
Akira Hatanaka5360f882011-08-17 02:05:42 +0000213 case MipsISD::Ext: return "MipsISD::Ext";
214 case MipsISD::Ins: return "MipsISD::Ins";
Akira Hatanakab9ebf8d2012-06-02 00:03:12 +0000215 case MipsISD::LWL: return "MipsISD::LWL";
216 case MipsISD::LWR: return "MipsISD::LWR";
217 case MipsISD::SWL: return "MipsISD::SWL";
218 case MipsISD::SWR: return "MipsISD::SWR";
219 case MipsISD::LDL: return "MipsISD::LDL";
220 case MipsISD::LDR: return "MipsISD::LDR";
221 case MipsISD::SDL: return "MipsISD::SDL";
222 case MipsISD::SDR: return "MipsISD::SDR";
Akira Hatanaka233ac532012-09-21 23:52:47 +0000223 case MipsISD::EXTP: return "MipsISD::EXTP";
224 case MipsISD::EXTPDP: return "MipsISD::EXTPDP";
225 case MipsISD::EXTR_S_H: return "MipsISD::EXTR_S_H";
226 case MipsISD::EXTR_W: return "MipsISD::EXTR_W";
227 case MipsISD::EXTR_R_W: return "MipsISD::EXTR_R_W";
228 case MipsISD::EXTR_RS_W: return "MipsISD::EXTR_RS_W";
229 case MipsISD::SHILO: return "MipsISD::SHILO";
230 case MipsISD::MTHLIP: return "MipsISD::MTHLIP";
231 case MipsISD::MULT: return "MipsISD::MULT";
232 case MipsISD::MULTU: return "MipsISD::MULTU";
Jia Liu434874d2013-03-04 01:06:54 +0000233 case MipsISD::MADD_DSP: return "MipsISD::MADD_DSP";
Akira Hatanaka233ac532012-09-21 23:52:47 +0000234 case MipsISD::MADDU_DSP: return "MipsISD::MADDU_DSP";
235 case MipsISD::MSUB_DSP: return "MipsISD::MSUB_DSP";
236 case MipsISD::MSUBU_DSP: return "MipsISD::MSUBU_DSP";
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000237 case MipsISD::SHLL_DSP: return "MipsISD::SHLL_DSP";
238 case MipsISD::SHRA_DSP: return "MipsISD::SHRA_DSP";
239 case MipsISD::SHRL_DSP: return "MipsISD::SHRL_DSP";
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000240 case MipsISD::SETCC_DSP: return "MipsISD::SETCC_DSP";
241 case MipsISD::SELECT_CC_DSP: return "MipsISD::SELECT_CC_DSP";
Daniel Sandersce09d072013-08-28 12:14:50 +0000242 case MipsISD::VALL_ZERO: return "MipsISD::VALL_ZERO";
243 case MipsISD::VANY_ZERO: return "MipsISD::VANY_ZERO";
244 case MipsISD::VALL_NONZERO: return "MipsISD::VALL_NONZERO";
245 case MipsISD::VANY_NONZERO: return "MipsISD::VANY_NONZERO";
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000246 case MipsISD::VCEQ: return "MipsISD::VCEQ";
247 case MipsISD::VCLE_S: return "MipsISD::VCLE_S";
248 case MipsISD::VCLE_U: return "MipsISD::VCLE_U";
249 case MipsISD::VCLT_S: return "MipsISD::VCLT_S";
250 case MipsISD::VCLT_U: return "MipsISD::VCLT_U";
Daniel Sanders3ce56622013-09-24 12:18:31 +0000251 case MipsISD::VSMAX: return "MipsISD::VSMAX";
252 case MipsISD::VSMIN: return "MipsISD::VSMIN";
253 case MipsISD::VUMAX: return "MipsISD::VUMAX";
254 case MipsISD::VUMIN: return "MipsISD::VUMIN";
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000255 case MipsISD::VEXTRACT_SEXT_ELT: return "MipsISD::VEXTRACT_SEXT_ELT";
256 case MipsISD::VEXTRACT_ZEXT_ELT: return "MipsISD::VEXTRACT_ZEXT_ELT";
Daniel Sandersf7456c72013-09-23 13:22:24 +0000257 case MipsISD::VNOR: return "MipsISD::VNOR";
Daniel Sanderse5087042013-09-24 14:02:15 +0000258 case MipsISD::VSHF: return "MipsISD::VSHF";
Daniel Sanders26307182013-09-24 14:20:00 +0000259 case MipsISD::SHF: return "MipsISD::SHF";
Daniel Sanders2ed228b2013-09-24 14:36:12 +0000260 case MipsISD::ILVEV: return "MipsISD::ILVEV";
261 case MipsISD::ILVOD: return "MipsISD::ILVOD";
262 case MipsISD::ILVL: return "MipsISD::ILVL";
263 case MipsISD::ILVR: return "MipsISD::ILVR";
Daniel Sandersfae5f2a2013-09-24 14:53:25 +0000264 case MipsISD::PCKEV: return "MipsISD::PCKEV";
265 case MipsISD::PCKOD: return "MipsISD::PCKOD";
Daniel Sandersb50ccf82014-04-01 10:35:28 +0000266 case MipsISD::INSVE: return "MipsISD::INSVE";
Craig Topper062a2ba2014-04-25 05:30:21 +0000267 default: return nullptr;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000268 }
269}
270
Eric Christopherb1526602014-09-19 23:30:42 +0000271MipsTargetLowering::MipsTargetLowering(const MipsTargetMachine &TM,
Eric Christopher8924d272014-07-18 23:25:04 +0000272 const MipsSubtarget &STI)
273 : TargetLowering(TM, new MipsTargetObjectFile()), Subtarget(STI) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000274 // Mips does not have i1 type, so use i32 for
Wesley Peck527da1b2010-11-23 03:31:01 +0000275 // setcc operations results (slt, sgt, ...).
Duncan Sands8d6e2e12008-11-23 15:47:28 +0000276 setBooleanContents(ZeroOrOneBooleanContent);
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000277 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Daniel Sanderscbd44c52014-07-10 10:18:12 +0000278 // The cmp.cond.fmt instruction in MIPS32r6/MIPS64r6 uses 0 and -1 like MSA
279 // does. Integer booleans still use 0 and 1.
Eric Christopher1c29a652014-07-18 22:55:25 +0000280 if (Subtarget.hasMips32r6())
Daniel Sanderscbd44c52014-07-10 10:18:12 +0000281 setBooleanContents(ZeroOrOneBooleanContent,
282 ZeroOrNegativeOneBooleanContent);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000283
Wesley Peck527da1b2010-11-23 03:31:01 +0000284 // Load extented operations for i1 types must be promoted
Owen Anderson9f944592009-08-11 20:47:22 +0000285 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
286 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
287 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000288
Eli Friedman1fa07e12009-07-17 04:07:24 +0000289 // MIPS doesn't have extending float->double load/store
Owen Anderson9f944592009-08-11 20:47:22 +0000290 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
291 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman39d6faa2009-07-17 02:28:12 +0000292
Wesley Peck527da1b2010-11-23 03:31:01 +0000293 // Used by legalize types to correctly generate the setcc result.
294 // Without this, every float setcc comes with a AND/OR with the result,
295 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +0000296 // which is used implicitly by brcond and select operations.
Owen Anderson9f944592009-08-11 20:47:22 +0000297 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +0000298
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000299 // Mips Custom Operations
Akira Hatanaka0f693a82013-03-06 21:32:03 +0000300 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000301 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000302 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000303 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
304 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
305 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
306 setOperationAction(ISD::SELECT, MVT::f32, Custom);
307 setOperationAction(ISD::SELECT, MVT::f64, Custom);
308 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Akira Hatanaka24cf4e32012-07-11 19:32:27 +0000309 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
310 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Akira Hatanakab7f78592012-03-09 23:46:03 +0000311 setOperationAction(ISD::SETCC, MVT::f32, Custom);
312 setOperationAction(ISD::SETCC, MVT::f64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000313 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000314 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
315 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000316 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000317
Eric Christopher1c29a652014-07-18 22:55:25 +0000318 if (Subtarget.isGP64bit()) {
Akira Hatanakada00aa82012-03-10 00:03:50 +0000319 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
320 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
321 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
322 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
323 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
324 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Akira Hatanaka019e5922012-06-02 00:04:42 +0000325 setOperationAction(ISD::LOAD, MVT::i64, Custom);
326 setOperationAction(ISD::STORE, MVT::i64, Custom);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000327 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000328 }
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +0000329
Eric Christopher1c29a652014-07-18 22:55:25 +0000330 if (!Subtarget.isGP64bit()) {
Akira Hatanaka0a8ab712012-05-09 00:55:21 +0000331 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
332 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
333 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
334 }
335
Akira Hatanaka28e02ec2012-11-07 19:10:58 +0000336 setOperationAction(ISD::ADD, MVT::i32, Custom);
Eric Christopher1c29a652014-07-18 22:55:25 +0000337 if (Subtarget.isGP64bit())
Akira Hatanaka28e02ec2012-11-07 19:10:58 +0000338 setOperationAction(ISD::ADD, MVT::i64, Custom);
339
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000340 setOperationAction(ISD::SDIV, MVT::i32, Expand);
341 setOperationAction(ISD::SREM, MVT::i32, Expand);
342 setOperationAction(ISD::UDIV, MVT::i32, Expand);
343 setOperationAction(ISD::UREM, MVT::i32, Expand);
Akira Hatanakab1538f92011-10-03 21:06:13 +0000344 setOperationAction(ISD::SDIV, MVT::i64, Expand);
345 setOperationAction(ISD::SREM, MVT::i64, Expand);
346 setOperationAction(ISD::UDIV, MVT::i64, Expand);
347 setOperationAction(ISD::UREM, MVT::i64, Expand);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000348
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000349 // Operations not directly supported by Mips.
Tom Stellardb1588fc2013-03-08 15:36:57 +0000350 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
351 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
352 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
353 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
Tom Stellard3787b122014-06-10 16:01:29 +0000354 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
355 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000356 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Akira Hatanaka79aed152011-12-20 23:40:56 +0000357 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000358 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Akira Hatanaka79aed152011-12-20 23:40:56 +0000359 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000360 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Eric Christopher1c29a652014-07-18 22:55:25 +0000361 if (Subtarget.hasCnMips()) {
Kai Nacke93fe5e82014-03-20 11:51:58 +0000362 setOperationAction(ISD::CTPOP, MVT::i32, Legal);
363 setOperationAction(ISD::CTPOP, MVT::i64, Legal);
364 } else {
365 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
366 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
367 }
Owen Anderson9f944592009-08-11 20:47:22 +0000368 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
Akira Hatanaka410ce9c2011-12-21 00:14:05 +0000369 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000370 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
371 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
372 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
373 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000374 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Akira Hatanaka7ba8a8d2011-09-30 18:51:46 +0000375 setOperationAction(ISD::ROTL, MVT::i64, Expand);
Akira Hatanaka33a25af2012-07-31 20:54:48 +0000376 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
377 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Bruno Cardoso Lopesd47180e2010-12-09 17:32:30 +0000378
Eric Christopher1c29a652014-07-18 22:55:25 +0000379 if (!Subtarget.hasMips32r2())
Bruno Cardoso Lopesd47180e2010-12-09 17:32:30 +0000380 setOperationAction(ISD::ROTR, MVT::i32, Expand);
381
Eric Christopher1c29a652014-07-18 22:55:25 +0000382 if (!Subtarget.hasMips64r2())
Akira Hatanaka7ba8a8d2011-09-30 18:51:46 +0000383 setOperationAction(ISD::ROTR, MVT::i64, Expand);
384
Owen Anderson9f944592009-08-11 20:47:22 +0000385 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes22b69db2011-03-04 18:54:14 +0000386 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000387 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes22b69db2011-03-04 18:54:14 +0000388 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000389 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
390 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000391 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
392 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanakadfb8cda2011-05-23 22:23:58 +0000393 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000394 setOperationAction(ISD::FLOG, MVT::f32, Expand);
395 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
396 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
397 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000398 setOperationAction(ISD::FMA, MVT::f32, Expand);
399 setOperationAction(ISD::FMA, MVT::f64, Expand);
Akira Hatanaka0603ad82012-03-29 18:43:11 +0000400 setOperationAction(ISD::FREM, MVT::f32, Expand);
401 setOperationAction(ISD::FREM, MVT::f64, Expand);
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000402
Akira Hatanakac0b02062013-01-30 00:26:49 +0000403 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
404
Daniel Sanders2b553d42014-08-01 09:17:39 +0000405 setOperationAction(ISD::VASTART, MVT::Other, Custom);
406 setOperationAction(ISD::VAARG, MVT::Other, Custom);
Bruno Cardoso Lopes048ffab2011-03-09 19:22:22 +0000407 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
408 setOperationAction(ISD::VAEND, MVT::Other, Expand);
409
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000410 // Use the default for now
Owen Anderson9f944592009-08-11 20:47:22 +0000411 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
412 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman26a48482011-07-27 22:21:52 +0000413
Jia Liuf54f60f2012-02-28 07:46:26 +0000414 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
415 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
416 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
417 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
Eli Friedman7dfa7912011-08-29 18:23:02 +0000418
Eli Friedman30a49e92011-08-03 21:06:02 +0000419 setInsertFencesForAtomic(true);
420
Eric Christopher1c29a652014-07-18 22:55:25 +0000421 if (!Subtarget.hasMips32r2()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000422 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
423 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000424 }
425
Daniel Sanders070fd1c2014-05-12 12:41:59 +0000426 // MIPS16 lacks MIPS32's clz and clo instructions.
Eric Christopher1c29a652014-07-18 22:55:25 +0000427 if (!Subtarget.hasMips32() || Subtarget.inMips16Mode())
Owen Anderson9f944592009-08-11 20:47:22 +0000428 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Eric Christopher1c29a652014-07-18 22:55:25 +0000429 if (!Subtarget.hasMips64())
Akira Hatanaka1d8efab2011-12-21 00:20:27 +0000430 setOperationAction(ISD::CTLZ, MVT::i64, Expand);
Bruno Cardoso Lopes93da7e62008-08-08 06:16:31 +0000431
Eric Christopher1c29a652014-07-18 22:55:25 +0000432 if (!Subtarget.hasMips32r2())
Owen Anderson9f944592009-08-11 20:47:22 +0000433 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Eric Christopher1c29a652014-07-18 22:55:25 +0000434 if (!Subtarget.hasMips64r2())
Akira Hatanaka4706ac92011-12-20 23:56:43 +0000435 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000436
Eric Christopher1c29a652014-07-18 22:55:25 +0000437 if (Subtarget.isGP64bit()) {
Akira Hatanaka019e5922012-06-02 00:04:42 +0000438 setLoadExtAction(ISD::SEXTLOAD, MVT::i32, Custom);
439 setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, Custom);
440 setLoadExtAction(ISD::EXTLOAD, MVT::i32, Custom);
441 setTruncStoreAction(MVT::i64, MVT::i32, Custom);
442 }
443
Akira Hatanakaa3d9ab92013-07-26 20:58:55 +0000444 setOperationAction(ISD::TRAP, MVT::Other, Legal);
445
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000446 setTargetDAGCombine(ISD::SDIVREM);
447 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka5e152182012-03-08 03:26:37 +0000448 setTargetDAGCombine(ISD::SELECT);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000449 setTargetDAGCombine(ISD::AND);
450 setTargetDAGCombine(ISD::OR);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000451 setTargetDAGCombine(ISD::ADD);
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000452
Eric Christopher1c29a652014-07-18 22:55:25 +0000453 setMinFunctionAlignment(Subtarget.isGP64bit() ? 3 : 2);
Eli Friedman2518f832011-05-06 20:34:06 +0000454
Daniel Sanders2b553d42014-08-01 09:17:39 +0000455 // The arguments on the stack are defined in terms of 4-byte slots on O32
456 // and 8-byte slots on N32/N64.
457 setMinStackArgumentAlignment(
458 (Subtarget.isABI_N32() || Subtarget.isABI_N64()) ? 8 : 4);
459
Eric Christopher1c29a652014-07-18 22:55:25 +0000460 setStackPointerRegisterToSaveRestore(Subtarget.isABI_N64() ? Mips::SP_64
461 : Mips::SP);
Akira Hatanakaaa560002011-05-26 18:59:03 +0000462
Eric Christopher1c29a652014-07-18 22:55:25 +0000463 setExceptionPointerRegister(Subtarget.isABI_N64() ? Mips::A0_64 : Mips::A0);
464 setExceptionSelectorRegister(Subtarget.isABI_N64() ? Mips::A1_64 : Mips::A1);
Akira Hatanaka1daf8c22012-06-13 19:33:32 +0000465
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000466 MaxStoresPerMemcpy = 16;
Zoran Jovanovicff9d5f32013-12-19 16:12:56 +0000467
Eric Christopher1c29a652014-07-18 22:55:25 +0000468 isMicroMips = Subtarget.inMicroMipsMode();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000469}
470
Eric Christopherb1526602014-09-19 23:30:42 +0000471const MipsTargetLowering *MipsTargetLowering::create(const MipsTargetMachine &TM,
Eric Christopher8924d272014-07-18 23:25:04 +0000472 const MipsSubtarget &STI) {
473 if (STI.inMips16Mode())
474 return llvm::createMips16TargetLowering(TM, STI);
Jia Liuf54f60f2012-02-28 07:46:26 +0000475
Eric Christopher8924d272014-07-18 23:25:04 +0000476 return llvm::createMipsSETargetLowering(TM, STI);
Akira Hatanaka2fcc1cf2011-08-12 21:30:06 +0000477}
478
Reed Kotler720c5ca2014-04-17 22:15:34 +0000479// Create a fast isel object.
480FastISel *
481MipsTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
482 const TargetLibraryInfo *libInfo) const {
483 if (!EnableMipsFastISel)
484 return TargetLowering::createFastISel(funcInfo, libInfo);
485 return Mips::createFastISel(funcInfo, libInfo);
486}
487
Matt Arsenault758659232013-05-18 00:21:46 +0000488EVT MipsTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Akira Hatanakab13b3332013-01-04 20:06:01 +0000489 if (!VT.isVector())
490 return MVT::i32;
491 return VT.changeVectorElementTypeToInteger();
Scott Michela6729e82008-03-10 15:42:14 +0000492}
493
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000494static SDValue performDivRemCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000495 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000496 const MipsSubtarget &Subtarget) {
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000497 if (DCI.isBeforeLegalizeOps())
498 return SDValue();
499
Akira Hatanakab1538f92011-10-03 21:06:13 +0000500 EVT Ty = N->getValueType(0);
Akira Hatanaka8002a3f2013-08-14 00:47:08 +0000501 unsigned LO = (Ty == MVT::i32) ? Mips::LO0 : Mips::LO0_64;
502 unsigned HI = (Ty == MVT::i32) ? Mips::HI0 : Mips::HI0_64;
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000503 unsigned Opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem16 :
504 MipsISD::DivRemU16;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000505 SDLoc DL(N);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000506
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000507 SDValue DivRem = DAG.getNode(Opc, DL, MVT::Glue,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000508 N->getOperand(0), N->getOperand(1));
509 SDValue InChain = DAG.getEntryNode();
510 SDValue InGlue = DivRem;
511
512 // insert MFLO
513 if (N->hasAnyUseOfValue(0)) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000514 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, DL, LO, Ty,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000515 InGlue);
516 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
517 InChain = CopyFromLo.getValue(1);
518 InGlue = CopyFromLo.getValue(2);
519 }
520
521 // insert MFHI
522 if (N->hasAnyUseOfValue(1)) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000523 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, DL,
Akira Hatanakab1538f92011-10-03 21:06:13 +0000524 HI, Ty, InGlue);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000525 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
526 }
527
528 return SDValue();
529}
530
Akira Hatanaka89af5892013-04-18 01:00:46 +0000531static Mips::CondCode condCodeToFCC(ISD::CondCode CC) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000532 switch (CC) {
533 default: llvm_unreachable("Unknown fp condition code!");
534 case ISD::SETEQ:
535 case ISD::SETOEQ: return Mips::FCOND_OEQ;
536 case ISD::SETUNE: return Mips::FCOND_UNE;
537 case ISD::SETLT:
538 case ISD::SETOLT: return Mips::FCOND_OLT;
539 case ISD::SETGT:
540 case ISD::SETOGT: return Mips::FCOND_OGT;
541 case ISD::SETLE:
542 case ISD::SETOLE: return Mips::FCOND_OLE;
543 case ISD::SETGE:
544 case ISD::SETOGE: return Mips::FCOND_OGE;
545 case ISD::SETULT: return Mips::FCOND_ULT;
546 case ISD::SETULE: return Mips::FCOND_ULE;
547 case ISD::SETUGT: return Mips::FCOND_UGT;
548 case ISD::SETUGE: return Mips::FCOND_UGE;
549 case ISD::SETUO: return Mips::FCOND_UN;
550 case ISD::SETO: return Mips::FCOND_OR;
551 case ISD::SETNE:
552 case ISD::SETONE: return Mips::FCOND_ONE;
553 case ISD::SETUEQ: return Mips::FCOND_UEQ;
554 }
555}
556
557
Akira Hatanakaf0ea5002013-03-30 01:16:38 +0000558/// This function returns true if the floating point conditional branches and
559/// conditional moves which use condition code CC should be inverted.
560static bool invertFPCondCodeUser(Mips::CondCode CC) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000561 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
562 return false;
563
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000564 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
565 "Illegal Condition Code");
Akira Hatanakaa5352702011-03-31 18:26:17 +0000566
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000567 return true;
Akira Hatanakaa5352702011-03-31 18:26:17 +0000568}
569
570// Creates and returns an FPCmp node from a setcc node.
571// Returns Op if setcc is not a floating point comparison.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000572static SDValue createFPCmp(SelectionDAG &DAG, const SDValue &Op) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000573 // must be a SETCC node
574 if (Op.getOpcode() != ISD::SETCC)
575 return Op;
576
577 SDValue LHS = Op.getOperand(0);
578
579 if (!LHS.getValueType().isFloatingPoint())
580 return Op;
581
582 SDValue RHS = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000583 SDLoc DL(Op);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000584
Akira Hatanakaaef55c82011-04-15 21:00:26 +0000585 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
586 // node if necessary.
Akira Hatanakaa5352702011-03-31 18:26:17 +0000587 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
588
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000589 return DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS,
Akira Hatanaka89af5892013-04-18 01:00:46 +0000590 DAG.getConstant(condCodeToFCC(CC), MVT::i32));
Akira Hatanakaa5352702011-03-31 18:26:17 +0000591}
592
593// Creates and returns a CMovFPT/F node.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000594static SDValue createCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000595 SDValue False, SDLoc DL) {
Akira Hatanakaf0ea5002013-03-30 01:16:38 +0000596 ConstantSDNode *CC = cast<ConstantSDNode>(Cond.getOperand(2));
597 bool invert = invertFPCondCodeUser((Mips::CondCode)CC->getSExtValue());
Akira Hatanaka8bce21c2013-07-26 20:51:20 +0000598 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000599
600 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
Akira Hatanaka8bce21c2013-07-26 20:51:20 +0000601 True.getValueType(), True, FCC0, False, Cond);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000602}
603
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000604static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000605 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000606 const MipsSubtarget &Subtarget) {
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000607 if (DCI.isBeforeLegalizeOps())
608 return SDValue();
609
610 SDValue SetCC = N->getOperand(0);
611
612 if ((SetCC.getOpcode() != ISD::SETCC) ||
613 !SetCC.getOperand(0).getValueType().isInteger())
614 return SDValue();
615
616 SDValue False = N->getOperand(2);
617 EVT FalseTy = False.getValueType();
618
619 if (!FalseTy.isInteger())
620 return SDValue();
621
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000622 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(False);
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000623
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000624 // If the RHS (False) is 0, we swap the order of the operands
625 // of ISD::SELECT (obviously also inverting the condition) so that we can
626 // take advantage of conditional moves using the $0 register.
627 // Example:
628 // return (a != 0) ? x : 0;
629 // load $reg, x
630 // movz $reg, $0, a
631 if (!FalseC)
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000632 return SDValue();
633
Andrew Trickef9de2a2013-05-25 02:42:55 +0000634 const SDLoc DL(N);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000635
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000636 if (!FalseC->getZExtValue()) {
637 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
638 SDValue True = N->getOperand(1);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000639
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000640 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
641 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
642
643 return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True);
644 }
645
Matheus Almeidaa6beac12013-12-05 12:07:05 +0000646 // If both operands are integer constants there's a possibility that we
647 // can do some interesting optimizations.
648 SDValue True = N->getOperand(1);
649 ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(True);
650
651 if (!TrueC || !True.getValueType().isInteger())
652 return SDValue();
653
654 // We'll also ignore MVT::i64 operands as this optimizations proves
655 // to be ineffective because of the required sign extensions as the result
656 // of a SETCC operator is always MVT::i32 for non-vector types.
657 if (True.getValueType() == MVT::i64)
658 return SDValue();
659
660 int64_t Diff = TrueC->getSExtValue() - FalseC->getSExtValue();
661
662 // 1) (a < x) ? y : y-1
663 // slti $reg1, a, x
664 // addiu $reg2, $reg1, y-1
665 if (Diff == 1)
666 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, False);
667
668 // 2) (a < x) ? y-1 : y
669 // slti $reg1, a, x
670 // xor $reg1, $reg1, 1
671 // addiu $reg2, $reg1, y-1
672 if (Diff == -1) {
673 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
674 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
675 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
676 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, True);
677 }
678
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000679 // Couldn't optimize.
680 return SDValue();
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000681}
682
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000683static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000684 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000685 const MipsSubtarget &Subtarget) {
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000686 // Pattern match EXT.
687 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
688 // => ext $dst, $src, size, pos
Eric Christopher1c29a652014-07-18 22:55:25 +0000689 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasExtractInsert())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000690 return SDValue();
691
692 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000693 unsigned ShiftRightOpc = ShiftRight.getOpcode();
694
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000695 // Op's first operand must be a shift right.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000696 if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL)
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000697 return SDValue();
698
699 // The second operand of the shift must be an immediate.
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000700 ConstantSDNode *CN;
701 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
702 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000703
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000704 uint64_t Pos = CN->getZExtValue();
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000705 uint64_t SMPos, SMSize;
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000706
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000707 // Op's second operand must be a shifted mask.
708 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000709 !isShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000710 return SDValue();
711
712 // Return if the shifted mask does not start at bit 0 or the sum of its size
713 // and Pos exceeds the word's size.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000714 EVT ValTy = N->getValueType(0);
715 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000716 return SDValue();
717
Andrew Trickef9de2a2013-05-25 02:42:55 +0000718 return DAG.getNode(MipsISD::Ext, SDLoc(N), ValTy,
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000719 ShiftRight.getOperand(0), DAG.getConstant(Pos, MVT::i32),
Akira Hatanakaeea541c2011-08-17 22:59:46 +0000720 DAG.getConstant(SMSize, MVT::i32));
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000721}
Jia Liuf54f60f2012-02-28 07:46:26 +0000722
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000723static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000724 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000725 const MipsSubtarget &Subtarget) {
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000726 // Pattern match INS.
727 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
Jia Liuf54f60f2012-02-28 07:46:26 +0000728 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000729 // => ins $dst, $src, size, pos, $src1
Eric Christopher1c29a652014-07-18 22:55:25 +0000730 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasExtractInsert())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000731 return SDValue();
732
733 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
734 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
735 ConstantSDNode *CN;
736
737 // See if Op's first operand matches (and $src1 , mask0).
738 if (And0.getOpcode() != ISD::AND)
739 return SDValue();
740
741 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000742 !isShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000743 return SDValue();
744
745 // See if Op's second operand matches (and (shl $src, pos), mask1).
746 if (And1.getOpcode() != ISD::AND)
747 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000748
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000749 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000750 !isShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000751 return SDValue();
752
753 // The shift masks must have the same position and size.
754 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
755 return SDValue();
756
757 SDValue Shl = And1.getOperand(0);
758 if (Shl.getOpcode() != ISD::SHL)
759 return SDValue();
760
761 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
762 return SDValue();
763
764 unsigned Shamt = CN->getZExtValue();
765
766 // Return if the shift amount and the first bit position of mask are not the
Jia Liuf54f60f2012-02-28 07:46:26 +0000767 // same.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000768 EVT ValTy = N->getValueType(0);
769 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000770 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000771
Andrew Trickef9de2a2013-05-25 02:42:55 +0000772 return DAG.getNode(MipsISD::Ins, SDLoc(N), ValTy, Shl.getOperand(0),
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000773 DAG.getConstant(SMPos0, MVT::i32),
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000774 DAG.getConstant(SMSize0, MVT::i32), And0.getOperand(0));
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000775}
Jia Liuf54f60f2012-02-28 07:46:26 +0000776
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000777static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000778 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000779 const MipsSubtarget &Subtarget) {
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000780 // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt))
781
782 if (DCI.isBeforeLegalizeOps())
783 return SDValue();
784
785 SDValue Add = N->getOperand(1);
786
787 if (Add.getOpcode() != ISD::ADD)
788 return SDValue();
789
790 SDValue Lo = Add.getOperand(1);
791
792 if ((Lo.getOpcode() != MipsISD::Lo) ||
793 (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable))
794 return SDValue();
795
796 EVT ValTy = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000797 SDLoc DL(N);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000798
799 SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, N->getOperand(0),
800 Add.getOperand(0));
801 return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo);
802}
803
Bruno Cardoso Lopes61a61e92011-02-10 18:05:10 +0000804SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000805 const {
806 SelectionDAG &DAG = DCI.DAG;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000807 unsigned Opc = N->getOpcode();
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000808
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000809 switch (Opc) {
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000810 default: break;
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000811 case ISD::SDIVREM:
812 case ISD::UDIVREM:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000813 return performDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000814 case ISD::SELECT:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000815 return performSELECTCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000816 case ISD::AND:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000817 return performANDCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000818 case ISD::OR:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000819 return performORCombine(N, DAG, DCI, Subtarget);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000820 case ISD::ADD:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000821 return performADDCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000822 }
823
824 return SDValue();
825}
826
Akira Hatanakafabb8cf2012-09-21 23:58:31 +0000827void
828MipsTargetLowering::LowerOperationWrapper(SDNode *N,
829 SmallVectorImpl<SDValue> &Results,
830 SelectionDAG &DAG) const {
831 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
832
833 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
834 Results.push_back(Res.getValue(I));
835}
836
837void
838MipsTargetLowering::ReplaceNodeResults(SDNode *N,
839 SmallVectorImpl<SDValue> &Results,
840 SelectionDAG &DAG) const {
Akira Hatanaka9da442f2013-04-30 21:17:07 +0000841 return LowerOperationWrapper(N, Results, DAG);
Akira Hatanakafabb8cf2012-09-21 23:58:31 +0000842}
843
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000844SDValue MipsTargetLowering::
Dan Gohman21cea8a2010-04-17 15:26:15 +0000845LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000846{
Wesley Peck527da1b2010-11-23 03:31:01 +0000847 switch (Op.getOpcode())
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000848 {
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000849 case ISD::BR_JT: return lowerBR_JT(Op, DAG);
850 case ISD::BRCOND: return lowerBRCOND(Op, DAG);
851 case ISD::ConstantPool: return lowerConstantPool(Op, DAG);
852 case ISD::GlobalAddress: return lowerGlobalAddress(Op, DAG);
853 case ISD::BlockAddress: return lowerBlockAddress(Op, DAG);
854 case ISD::GlobalTLSAddress: return lowerGlobalTLSAddress(Op, DAG);
855 case ISD::JumpTable: return lowerJumpTable(Op, DAG);
856 case ISD::SELECT: return lowerSELECT(Op, DAG);
857 case ISD::SELECT_CC: return lowerSELECT_CC(Op, DAG);
858 case ISD::SETCC: return lowerSETCC(Op, DAG);
859 case ISD::VASTART: return lowerVASTART(Op, DAG);
Daniel Sanders2b553d42014-08-01 09:17:39 +0000860 case ISD::VAARG: return lowerVAARG(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000861 case ISD::FCOPYSIGN: return lowerFCOPYSIGN(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000862 case ISD::FRAMEADDR: return lowerFRAMEADDR(Op, DAG);
863 case ISD::RETURNADDR: return lowerRETURNADDR(Op, DAG);
864 case ISD::EH_RETURN: return lowerEH_RETURN(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000865 case ISD::ATOMIC_FENCE: return lowerATOMIC_FENCE(Op, DAG);
866 case ISD::SHL_PARTS: return lowerShiftLeftParts(Op, DAG);
867 case ISD::SRA_PARTS: return lowerShiftRightParts(Op, DAG, true);
868 case ISD::SRL_PARTS: return lowerShiftRightParts(Op, DAG, false);
869 case ISD::LOAD: return lowerLOAD(Op, DAG);
870 case ISD::STORE: return lowerSTORE(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000871 case ISD::ADD: return lowerADD(Op, DAG);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000872 case ISD::FP_TO_SINT: return lowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000873 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000874 return SDValue();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000875}
876
Akira Hatanakae2489122011-04-15 21:51:11 +0000877//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000878// Lower helper functions
Akira Hatanakae2489122011-04-15 21:51:11 +0000879//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000880
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000881// addLiveIn - This helper function adds the specified physical register to the
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000882// MachineFunction as a live in value. It also creates a corresponding
883// virtual register for it.
884static unsigned
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000885addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000886{
Chris Lattnera10fff52007-12-31 04:13:23 +0000887 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
888 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000889 return VReg;
890}
891
Daniel Sanders308181e2014-06-12 10:44:10 +0000892static MachineBasicBlock *insertDivByZeroTrap(MachineInstr *MI,
893 MachineBasicBlock &MBB,
894 const TargetInstrInfo &TII,
895 bool Is64Bit) {
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000896 if (NoZeroDivCheck)
897 return &MBB;
898
899 // Insert instruction "teq $divisor_reg, $zero, 7".
900 MachineBasicBlock::iterator I(MI);
901 MachineInstrBuilder MIB;
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000902 MachineOperand &Divisor = MI->getOperand(2);
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000903 MIB = BuildMI(MBB, std::next(I), MI->getDebugLoc(), TII.get(Mips::TEQ))
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000904 .addReg(Divisor.getReg(), getKillRegState(Divisor.isKill()))
905 .addReg(Mips::ZERO).addImm(7);
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000906
907 // Use the 32-bit sub-register if this is a 64-bit division.
908 if (Is64Bit)
909 MIB->getOperand(0).setSubReg(Mips::sub_32);
910
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000911 // Clear Divisor's kill flag.
912 Divisor.setIsKill(false);
Daniel Sanders308181e2014-06-12 10:44:10 +0000913
914 // We would normally delete the original instruction here but in this case
915 // we only needed to inject an additional instruction rather than replace it.
916
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000917 return &MBB;
918}
919
Akira Hatanakae4bd0542012-09-27 02:15:57 +0000920MachineBasicBlock *
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +0000921MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +0000922 MachineBasicBlock *BB) const {
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +0000923 switch (MI->getOpcode()) {
Reed Kotler97ba5f22013-02-21 04:22:38 +0000924 default:
925 llvm_unreachable("Unexpected instr type to insert");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000926 case Mips::ATOMIC_LOAD_ADD_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000927 return emitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000928 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000929 return emitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000930 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000931 return emitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000932 case Mips::ATOMIC_LOAD_ADD_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000933 return emitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000934
935 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000936 return emitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000937 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000938 return emitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000939 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000940 return emitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000941 case Mips::ATOMIC_LOAD_AND_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000942 return emitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000943
944 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000945 return emitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000946 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000947 return emitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000948 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000949 return emitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000950 case Mips::ATOMIC_LOAD_OR_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000951 return emitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000952
953 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000954 return emitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000955 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000956 return emitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000957 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000958 return emitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000959 case Mips::ATOMIC_LOAD_XOR_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000960 return emitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000961
962 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000963 return emitAtomicBinaryPartword(MI, BB, 1, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000964 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000965 return emitAtomicBinaryPartword(MI, BB, 2, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000966 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000967 return emitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000968 case Mips::ATOMIC_LOAD_NAND_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000969 return emitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000970
971 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000972 return emitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000973 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000974 return emitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000975 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000976 return emitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000977 case Mips::ATOMIC_LOAD_SUB_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000978 return emitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000979
980 case Mips::ATOMIC_SWAP_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000981 return emitAtomicBinaryPartword(MI, BB, 1, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000982 case Mips::ATOMIC_SWAP_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000983 return emitAtomicBinaryPartword(MI, BB, 2, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000984 case Mips::ATOMIC_SWAP_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000985 return emitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000986 case Mips::ATOMIC_SWAP_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000987 return emitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000988
989 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000990 return emitAtomicCmpSwapPartword(MI, BB, 1);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000991 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000992 return emitAtomicCmpSwapPartword(MI, BB, 2);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000993 case Mips::ATOMIC_CMP_SWAP_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000994 return emitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000995 case Mips::ATOMIC_CMP_SWAP_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000996 return emitAtomicCmpSwap(MI, BB, 8);
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000997 case Mips::PseudoSDIV:
998 case Mips::PseudoUDIV:
Daniel Sanders308181e2014-06-12 10:44:10 +0000999 case Mips::DIV:
1000 case Mips::DIVU:
1001 case Mips::MOD:
1002 case Mips::MODU:
Eric Christopherd9134482014-08-04 21:25:23 +00001003 return insertDivByZeroTrap(
1004 MI, *BB, *getTargetMachine().getSubtargetImpl()->getInstrInfo(), false);
Akira Hatanaka1cb02422013-05-20 18:07:43 +00001005 case Mips::PseudoDSDIV:
1006 case Mips::PseudoDUDIV:
Daniel Sanders308181e2014-06-12 10:44:10 +00001007 case Mips::DDIV:
1008 case Mips::DDIVU:
1009 case Mips::DMOD:
1010 case Mips::DMODU:
Eric Christopherd9134482014-08-04 21:25:23 +00001011 return insertDivByZeroTrap(
1012 MI, *BB, *getTargetMachine().getSubtargetImpl()->getInstrInfo(), true);
Daniel Sanders0fa60412014-06-12 13:39:06 +00001013 case Mips::SEL_D:
1014 return emitSEL_D(MI, BB);
Akira Hatanakaa5352702011-03-31 18:26:17 +00001015 }
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001016}
1017
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001018// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
1019// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
1020MachineBasicBlock *
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001021MipsTargetLowering::emitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher0713a9d2011-06-08 23:55:35 +00001022 unsigned Size, unsigned BinOpcode,
Akira Hatanaka15506782011-06-07 18:58:42 +00001023 bool Nand) const {
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001024 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001025
1026 MachineFunction *MF = BB->getParent();
1027 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001028 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Eric Christopherd9134482014-08-04 21:25:23 +00001029 const TargetInstrInfo *TII =
1030 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001031 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001032 unsigned LL, SC, AND, NOR, ZERO, BEQ;
1033
1034 if (Size == 4) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001035 if (isMicroMips) {
1036 LL = Mips::LL_MM;
1037 SC = Mips::SC_MM;
1038 } else {
Daniel Sandersbdcfab12014-07-24 09:47:14 +00001039 LL = Subtarget.hasMips32r6() ? Mips::LL_R6 : Mips::LL;
1040 SC = Subtarget.hasMips32r6() ? Mips::SC_R6 : Mips::SC;
Daniel Sanders6a803f62014-06-16 13:13:03 +00001041 }
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001042 AND = Mips::AND;
1043 NOR = Mips::NOR;
1044 ZERO = Mips::ZERO;
1045 BEQ = Mips::BEQ;
Daniel Sanders6a803f62014-06-16 13:13:03 +00001046 } else {
Daniel Sandersbdcfab12014-07-24 09:47:14 +00001047 LL = Subtarget.hasMips64r6() ? Mips::LLD_R6 : Mips::LLD;
1048 SC = Subtarget.hasMips64r6() ? Mips::SCD_R6 : Mips::SCD;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001049 AND = Mips::AND64;
1050 NOR = Mips::NOR64;
1051 ZERO = Mips::ZERO_64;
1052 BEQ = Mips::BEQ64;
1053 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001054
Akira Hatanaka0e019592011-07-19 20:11:17 +00001055 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001056 unsigned Ptr = MI->getOperand(1).getReg();
1057 unsigned Incr = MI->getOperand(2).getReg();
1058
Akira Hatanaka0e019592011-07-19 20:11:17 +00001059 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1060 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1061 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001062
1063 // insert new blocks after the current block
1064 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1065 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1066 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1067 MachineFunction::iterator It = BB;
1068 ++It;
1069 MF->insert(It, loopMBB);
1070 MF->insert(It, exitMBB);
1071
1072 // Transfer the remainder of BB and its successor edges to exitMBB.
1073 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001074 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001075 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1076
1077 // thisMBB:
1078 // ...
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001079 // fallthrough --> loopMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001080 BB->addSuccessor(loopMBB);
Akira Hatanaka08636b42011-07-19 17:09:53 +00001081 loopMBB->addSuccessor(loopMBB);
1082 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001083
1084 // loopMBB:
1085 // ll oldval, 0(ptr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001086 // <binop> storeval, oldval, incr
1087 // sc success, storeval, 0(ptr)
1088 // beq success, $0, loopMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001089 BB = loopMBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001090 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001091 if (Nand) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001092 // and andres, oldval, incr
1093 // nor storeval, $0, andres
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001094 BuildMI(BB, DL, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
1095 BuildMI(BB, DL, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001096 } else if (BinOpcode) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001097 // <binop> storeval, oldval, incr
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001098 BuildMI(BB, DL, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001099 } else {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001100 StoreVal = Incr;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001101 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001102 BuildMI(BB, DL, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
1103 BuildMI(BB, DL, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001104
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001105 MI->eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001106
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001107 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001108}
1109
Daniel Sanders6a803f62014-06-16 13:13:03 +00001110MachineBasicBlock *MipsTargetLowering::emitSignExtendToI32InReg(
1111 MachineInstr *MI, MachineBasicBlock *BB, unsigned Size, unsigned DstReg,
1112 unsigned SrcReg) const {
Eric Christopherd9134482014-08-04 21:25:23 +00001113 const TargetInstrInfo *TII =
1114 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Daniel Sanders6a803f62014-06-16 13:13:03 +00001115 DebugLoc DL = MI->getDebugLoc();
1116
Eric Christopher1c29a652014-07-18 22:55:25 +00001117 if (Subtarget.hasMips32r2() && Size == 1) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001118 BuildMI(BB, DL, TII->get(Mips::SEB), DstReg).addReg(SrcReg);
1119 return BB;
1120 }
1121
Eric Christopher1c29a652014-07-18 22:55:25 +00001122 if (Subtarget.hasMips32r2() && Size == 2) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001123 BuildMI(BB, DL, TII->get(Mips::SEH), DstReg).addReg(SrcReg);
1124 return BB;
1125 }
1126
1127 MachineFunction *MF = BB->getParent();
1128 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1129 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1130 unsigned ScrReg = RegInfo.createVirtualRegister(RC);
1131
1132 assert(Size < 32);
1133 int64_t ShiftImm = 32 - (Size * 8);
1134
1135 BuildMI(BB, DL, TII->get(Mips::SLL), ScrReg).addReg(SrcReg).addImm(ShiftImm);
1136 BuildMI(BB, DL, TII->get(Mips::SRA), DstReg).addReg(ScrReg).addImm(ShiftImm);
1137
1138 return BB;
1139}
1140
1141MachineBasicBlock *MipsTargetLowering::emitAtomicBinaryPartword(
1142 MachineInstr *MI, MachineBasicBlock *BB, unsigned Size, unsigned BinOpcode,
1143 bool Nand) const {
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001144 assert((Size == 1 || Size == 2) &&
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001145 "Unsupported size for EmitAtomicBinaryPartial.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001146
1147 MachineFunction *MF = BB->getParent();
1148 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1149 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
Eric Christopherd9134482014-08-04 21:25:23 +00001150 const TargetInstrInfo *TII =
1151 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001152 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001153
1154 unsigned Dest = MI->getOperand(0).getReg();
1155 unsigned Ptr = MI->getOperand(1).getReg();
1156 unsigned Incr = MI->getOperand(2).getReg();
1157
Akira Hatanaka0e019592011-07-19 20:11:17 +00001158 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1159 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001160 unsigned Mask = RegInfo.createVirtualRegister(RC);
1161 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001162 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1163 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001164 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001165 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1166 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1167 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1168 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1169 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka9663dd32011-07-19 20:56:53 +00001170 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001171 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1172 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1173 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001174 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001175
1176 // insert new blocks after the current block
1177 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1178 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001179 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001180 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1181 MachineFunction::iterator It = BB;
1182 ++It;
1183 MF->insert(It, loopMBB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001184 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001185 MF->insert(It, exitMBB);
1186
1187 // Transfer the remainder of BB and its successor edges to exitMBB.
1188 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001189 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001190 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1191
Akira Hatanaka08636b42011-07-19 17:09:53 +00001192 BB->addSuccessor(loopMBB);
1193 loopMBB->addSuccessor(loopMBB);
1194 loopMBB->addSuccessor(sinkMBB);
1195 sinkMBB->addSuccessor(exitMBB);
1196
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001197 // thisMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001198 // addiu masklsb2,$0,-4 # 0xfffffffc
1199 // and alignedaddr,ptr,masklsb2
1200 // andi ptrlsb2,ptr,3
1201 // sll shiftamt,ptrlsb2,3
1202 // ori maskupper,$0,255 # 0xff
1203 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001204 // nor mask2,$0,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001205 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001206
1207 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001208 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001209 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001210 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001211 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001212 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Eric Christopher1c29a652014-07-18 22:55:25 +00001213 if (Subtarget.isLittle()) {
Akira Hatanaka2bf97332013-05-31 03:25:44 +00001214 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1215 } else {
1216 unsigned Off = RegInfo.createVirtualRegister(RC);
1217 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1218 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1219 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1220 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001221 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001222 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001223 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001224 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001225 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001226 BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(Incr).addReg(ShiftAmt);
Bruno Cardoso Lopesf771a0f2011-05-31 20:25:26 +00001227
Akira Hatanaka27292632011-07-18 18:52:12 +00001228 // atomic.load.binop
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001229 // loopMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001230 // ll oldval,0(alignedaddr)
1231 // binop binopres,oldval,incr2
1232 // and newval,binopres,mask
1233 // and maskedoldval0,oldval,mask2
1234 // or storeval,maskedoldval0,newval
1235 // sc success,storeval,0(alignedaddr)
1236 // beq success,$0,loopMBB
1237
Akira Hatanaka27292632011-07-18 18:52:12 +00001238 // atomic.swap
1239 // loopMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001240 // ll oldval,0(alignedaddr)
Akira Hatanakae4503582011-07-19 18:14:26 +00001241 // and newval,incr2,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001242 // and maskedoldval0,oldval,mask2
1243 // or storeval,maskedoldval0,newval
1244 // sc success,storeval,0(alignedaddr)
1245 // beq success,$0,loopMBB
Akira Hatanaka27292632011-07-18 18:52:12 +00001246
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001247 BB = loopMBB;
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001248 BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001249 if (Nand) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001250 // and andres, oldval, incr2
1251 // nor binopres, $0, andres
1252 // and newval, binopres, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001253 BuildMI(BB, DL, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1254 BuildMI(BB, DL, TII->get(Mips::NOR), BinOpRes)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001255 .addReg(Mips::ZERO).addReg(AndRes);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001256 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001257 } else if (BinOpcode) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001258 // <binop> binopres, oldval, incr2
1259 // and newval, binopres, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001260 BuildMI(BB, DL, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1261 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001262 } else { // atomic.swap
Akira Hatanaka0e019592011-07-19 20:11:17 +00001263 // and newval, incr2, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001264 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanakae4503582011-07-19 18:14:26 +00001265 }
Jia Liuf54f60f2012-02-28 07:46:26 +00001266
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001267 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001268 .addReg(OldVal).addReg(Mask2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001269 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka9663dd32011-07-19 20:56:53 +00001270 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001271 BuildMI(BB, DL, TII->get(Mips::SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001272 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001273 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001274 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001275
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001276 // sinkMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001277 // and maskedoldval1,oldval,mask
1278 // srl srlres,maskedoldval1,shiftamt
Daniel Sanders6a803f62014-06-16 13:13:03 +00001279 // sign_extend dest,srlres
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001280 BB = sinkMBB;
Akira Hatanakae97bd812011-07-19 03:14:58 +00001281
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001282 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001283 .addReg(OldVal).addReg(Mask);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001284 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001285 .addReg(MaskedOldVal1).addReg(ShiftAmt);
Daniel Sanders6a803f62014-06-16 13:13:03 +00001286 BB = emitSignExtendToI32InReg(MI, BB, Size, Dest, SrlRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001287
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001288 MI->eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001289
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001290 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001291}
1292
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001293MachineBasicBlock * MipsTargetLowering::emitAtomicCmpSwap(MachineInstr *MI,
1294 MachineBasicBlock *BB,
1295 unsigned Size) const {
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001296 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001297
1298 MachineFunction *MF = BB->getParent();
1299 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001300 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Eric Christopherd9134482014-08-04 21:25:23 +00001301 const TargetInstrInfo *TII =
1302 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001303 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001304 unsigned LL, SC, ZERO, BNE, BEQ;
1305
1306 if (Size == 4) {
Zoran Jovanovicff9d5f32013-12-19 16:12:56 +00001307 LL = isMicroMips ? Mips::LL_MM : Mips::LL;
1308 SC = isMicroMips ? Mips::SC_MM : Mips::SC;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001309 ZERO = Mips::ZERO;
1310 BNE = Mips::BNE;
1311 BEQ = Mips::BEQ;
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001312 } else {
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001313 LL = Mips::LLD;
1314 SC = Mips::SCD;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001315 ZERO = Mips::ZERO_64;
1316 BNE = Mips::BNE64;
1317 BEQ = Mips::BEQ64;
1318 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001319
1320 unsigned Dest = MI->getOperand(0).getReg();
1321 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka0e019592011-07-19 20:11:17 +00001322 unsigned OldVal = MI->getOperand(2).getReg();
1323 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001324
Akira Hatanaka0e019592011-07-19 20:11:17 +00001325 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001326
1327 // insert new blocks after the current block
1328 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1329 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1330 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1331 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1332 MachineFunction::iterator It = BB;
1333 ++It;
1334 MF->insert(It, loop1MBB);
1335 MF->insert(It, loop2MBB);
1336 MF->insert(It, exitMBB);
1337
1338 // Transfer the remainder of BB and its successor edges to exitMBB.
1339 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001340 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001341 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1342
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001343 // thisMBB:
1344 // ...
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001345 // fallthrough --> loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001346 BB->addSuccessor(loop1MBB);
Akira Hatanaka08636b42011-07-19 17:09:53 +00001347 loop1MBB->addSuccessor(exitMBB);
1348 loop1MBB->addSuccessor(loop2MBB);
1349 loop2MBB->addSuccessor(loop1MBB);
1350 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001351
1352 // loop1MBB:
1353 // ll dest, 0(ptr)
1354 // bne dest, oldval, exitMBB
1355 BB = loop1MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001356 BuildMI(BB, DL, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1357 BuildMI(BB, DL, TII->get(BNE))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001358 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001359
1360 // loop2MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001361 // sc success, newval, 0(ptr)
1362 // beq success, $0, loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001363 BB = loop2MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001364 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001365 .addReg(NewVal).addReg(Ptr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001366 BuildMI(BB, DL, TII->get(BEQ))
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001367 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001368
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001369 MI->eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001370
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001371 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001372}
1373
1374MachineBasicBlock *
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001375MipsTargetLowering::emitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka15506782011-06-07 18:58:42 +00001376 MachineBasicBlock *BB,
1377 unsigned Size) const {
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001378 assert((Size == 1 || Size == 2) &&
1379 "Unsupported size for EmitAtomicCmpSwapPartial.");
1380
1381 MachineFunction *MF = BB->getParent();
1382 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1383 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
Eric Christopherd9134482014-08-04 21:25:23 +00001384 const TargetInstrInfo *TII =
1385 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001386 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001387
1388 unsigned Dest = MI->getOperand(0).getReg();
1389 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka0e019592011-07-19 20:11:17 +00001390 unsigned CmpVal = MI->getOperand(2).getReg();
1391 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001392
Akira Hatanaka0e019592011-07-19 20:11:17 +00001393 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1394 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001395 unsigned Mask = RegInfo.createVirtualRegister(RC);
1396 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001397 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1398 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1399 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1400 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1401 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1402 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1403 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1404 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1405 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1406 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1407 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1408 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001409 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001410
1411 // insert new blocks after the current block
1412 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1413 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1414 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001415 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001416 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1417 MachineFunction::iterator It = BB;
1418 ++It;
1419 MF->insert(It, loop1MBB);
1420 MF->insert(It, loop2MBB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001421 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001422 MF->insert(It, exitMBB);
1423
1424 // Transfer the remainder of BB and its successor edges to exitMBB.
1425 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001426 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001427 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1428
Akira Hatanaka08636b42011-07-19 17:09:53 +00001429 BB->addSuccessor(loop1MBB);
1430 loop1MBB->addSuccessor(sinkMBB);
1431 loop1MBB->addSuccessor(loop2MBB);
1432 loop2MBB->addSuccessor(loop1MBB);
1433 loop2MBB->addSuccessor(sinkMBB);
1434 sinkMBB->addSuccessor(exitMBB);
1435
Akira Hatanakae4503582011-07-19 18:14:26 +00001436 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001437 // thisMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001438 // addiu masklsb2,$0,-4 # 0xfffffffc
1439 // and alignedaddr,ptr,masklsb2
1440 // andi ptrlsb2,ptr,3
1441 // sll shiftamt,ptrlsb2,3
1442 // ori maskupper,$0,255 # 0xff
1443 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001444 // nor mask2,$0,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001445 // andi maskedcmpval,cmpval,255
1446 // sll shiftedcmpval,maskedcmpval,shiftamt
1447 // andi maskednewval,newval,255
1448 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001449 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001450 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001451 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001452 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001453 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001454 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Eric Christopher1c29a652014-07-18 22:55:25 +00001455 if (Subtarget.isLittle()) {
Akira Hatanaka2bf97332013-05-31 03:25:44 +00001456 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1457 } else {
1458 unsigned Off = RegInfo.createVirtualRegister(RC);
1459 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1460 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1461 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1462 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001463 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001464 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001465 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001466 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001467 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1468 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001469 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001470 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001471 .addReg(MaskedCmpVal).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001472 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001473 .addReg(NewVal).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001474 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001475 .addReg(MaskedNewVal).addReg(ShiftAmt);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001476
1477 // loop1MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001478 // ll oldval,0(alginedaddr)
1479 // and maskedoldval0,oldval,mask
1480 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001481 BB = loop1MBB;
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001482 BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001483 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001484 .addReg(OldVal).addReg(Mask);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001485 BuildMI(BB, DL, TII->get(Mips::BNE))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001486 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001487
1488 // loop2MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001489 // and maskedoldval1,oldval,mask2
1490 // or storeval,maskedoldval1,shiftednewval
1491 // sc success,storeval,0(alignedaddr)
1492 // beq success,$0,loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001493 BB = loop2MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001494 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001495 .addReg(OldVal).addReg(Mask2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001496 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001497 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001498 BuildMI(BB, DL, TII->get(Mips::SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001499 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001500 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001501 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001502
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001503 // sinkMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001504 // srl srlres,maskedoldval0,shiftamt
Daniel Sanders6a803f62014-06-16 13:13:03 +00001505 // sign_extend dest,srlres
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001506 BB = sinkMBB;
Akira Hatanakae97bd812011-07-19 03:14:58 +00001507
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001508 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001509 .addReg(MaskedOldVal0).addReg(ShiftAmt);
Daniel Sanders6a803f62014-06-16 13:13:03 +00001510 BB = emitSignExtendToI32InReg(MI, BB, Size, Dest, SrlRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001511
1512 MI->eraseFromParent(); // The instruction is gone now.
1513
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001514 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001515}
1516
Daniel Sanders0fa60412014-06-12 13:39:06 +00001517MachineBasicBlock *MipsTargetLowering::emitSEL_D(MachineInstr *MI,
1518 MachineBasicBlock *BB) const {
1519 MachineFunction *MF = BB->getParent();
Eric Christopherd9134482014-08-04 21:25:23 +00001520 const TargetRegisterInfo *TRI =
1521 getTargetMachine().getSubtargetImpl()->getRegisterInfo();
1522 const TargetInstrInfo *TII =
1523 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Daniel Sanders0fa60412014-06-12 13:39:06 +00001524 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1525 DebugLoc DL = MI->getDebugLoc();
1526 MachineBasicBlock::iterator II(MI);
1527
1528 unsigned Fc = MI->getOperand(1).getReg();
1529 const auto &FGR64RegClass = TRI->getRegClass(Mips::FGR64RegClassID);
1530
1531 unsigned Fc2 = RegInfo.createVirtualRegister(FGR64RegClass);
1532
1533 BuildMI(*BB, II, DL, TII->get(Mips::SUBREG_TO_REG), Fc2)
1534 .addImm(0)
1535 .addReg(Fc)
1536 .addImm(Mips::sub_lo);
1537
1538 // We don't erase the original instruction, we just replace the condition
1539 // register with the 64-bit super-register.
1540 MI->getOperand(1).setReg(Fc2);
1541
1542 return BB;
1543}
1544
Akira Hatanakae2489122011-04-15 21:51:11 +00001545//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001546// Misc Lower Operation implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00001547//===----------------------------------------------------------------------===//
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001548SDValue MipsTargetLowering::lowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001549 SDValue Chain = Op.getOperand(0);
1550 SDValue Table = Op.getOperand(1);
1551 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001552 SDLoc DL(Op);
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001553 EVT PTy = getPointerTy();
1554 unsigned EntrySize =
1555 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(*getDataLayout());
1556
1557 Index = DAG.getNode(ISD::MUL, DL, PTy, Index,
1558 DAG.getConstant(EntrySize, PTy));
1559 SDValue Addr = DAG.getNode(ISD::ADD, DL, PTy, Index, Table);
1560
1561 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
1562 Addr = DAG.getExtLoad(ISD::SEXTLOAD, DL, PTy, Chain, Addr,
1563 MachinePointerInfo::getJumpTable(), MemVT, false, false,
Louis Gerbarg67474e32014-07-31 21:45:05 +00001564 false, 0);
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001565 Chain = Addr.getValue(1);
1566
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001567 if ((getTargetMachine().getRelocationModel() == Reloc::PIC_) ||
Eric Christopher1c29a652014-07-18 22:55:25 +00001568 Subtarget.isABI_N64()) {
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 {
Dale Johannesen400dc2e2009-02-06 21:50:26 +00001646 // FIXME there isn't actually debug info here
Andrew Trickef9de2a2013-05-25 02:42:55 +00001647 SDLoc DL(Op);
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001648 EVT Ty = Op.getValueType();
1649 GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
1650 const GlobalValue *GV = N->getGlobal();
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001651
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001652 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ &&
Eric Christopher1c29a652014-07-18 22:55:25 +00001653 !Subtarget.isABI_N64()) {
Akira Hatanaka92a96e12012-09-12 23:27:55 +00001654 const MipsTargetObjectFile &TLOF =
1655 (const MipsTargetObjectFile&)getObjFileLowering();
Wesley Peck527da1b2010-11-23 03:31:01 +00001656
Chris Lattner58e8be82009-08-13 05:41:27 +00001657 // %gp_rel relocation
Wesley Peck527da1b2010-11-23 03:31:01 +00001658 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001659 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, 0,
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +00001660 MipsII::MO_GPREL);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001661 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, DL,
Craig Topper48d114b2014-04-26 18:35:24 +00001662 DAG.getVTList(MVT::i32), GA);
Akira Hatanakaad495022012-08-22 03:18:13 +00001663 SDValue GPReg = DAG.getRegister(Mips::GP, MVT::i32);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001664 return DAG.getNode(ISD::ADD, DL, MVT::i32, GPReg, GPRelNode);
Chris Lattner58e8be82009-08-13 05:41:27 +00001665 }
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001666
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001667 // %hi/%lo relocation
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001668 return getAddrNonPIC(N, Ty, DAG);
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001669 }
1670
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001671 if (GV->hasInternalLinkage() || (GV->hasLocalLinkage() && !isa<Function>(GV)))
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001672 return getAddrLocal(N, Ty, DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +00001673 Subtarget.isABI_N32() || Subtarget.isABI_N64());
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001674
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00001675 if (LargeGOT)
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001676 return getAddrGlobalLargeGOT(N, Ty, DAG, MipsII::MO_GOT_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00001677 MipsII::MO_GOT_LO16, DAG.getEntryNode(),
1678 MachinePointerInfo::getGOT());
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00001679
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001680 return getAddrGlobal(N, Ty, DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +00001681 (Subtarget.isABI_N32() || Subtarget.isABI_N64())
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001682 ? MipsII::MO_GOT_DISP
1683 : MipsII::MO_GOT16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00001684 DAG.getEntryNode(), MachinePointerInfo::getGOT());
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001685}
1686
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001687SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op,
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +00001688 SelectionDAG &DAG) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001689 BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op);
1690 EVT Ty = Op.getValueType();
Akira Hatanaka30f97cf2013-09-25 00:30:25 +00001691
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001692 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ &&
Eric Christopher1c29a652014-07-18 22:55:25 +00001693 !Subtarget.isABI_N64())
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001694 return getAddrNonPIC(N, Ty, DAG);
1695
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001696 return getAddrLocal(N, Ty, DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +00001697 Subtarget.isABI_N32() || Subtarget.isABI_N64());
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +00001698}
1699
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001700SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001701lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001702{
Akira Hatanakabff84e12011-12-14 18:26:41 +00001703 // If the relocation model is PIC, use the General Dynamic TLS Model or
1704 // Local Dynamic TLS model, otherwise use the Initial Exec or
1705 // Local Exec TLS Model.
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001706
1707 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001708 SDLoc DL(GA);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001709 const GlobalValue *GV = GA->getGlobal();
1710 EVT PtrVT = getPointerTy();
1711
Hans Wennborgaea41202012-05-04 09:40:39 +00001712 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
1713
1714 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Hans Wennborg245917b2012-06-04 14:02:08 +00001715 // General Dynamic and Local Dynamic TLS Model.
1716 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
1717 : MipsII::MO_TLSGD;
1718
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001719 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag);
1720 SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT,
1721 getGlobalReg(DAG, PtrVT), TGA);
Akira Hatanakaf10ee842011-12-08 21:05:38 +00001722 unsigned PtrSize = PtrVT.getSizeInBits();
1723 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1724
Benjamin Kramer64ba50a2011-12-11 12:21:34 +00001725 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001726
1727 ArgListTy Args;
1728 ArgListEntry Entry;
1729 Entry.Node = Argument;
Akira Hatanakadee6c822011-12-08 20:34:32 +00001730 Entry.Ty = PtrTy;
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001731 Args.push_back(Entry);
Jia Liuf54f60f2012-02-28 07:46:26 +00001732
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00001733 TargetLowering::CallLoweringInfo CLI(DAG);
1734 CLI.setDebugLoc(DL).setChain(DAG.getEntryNode())
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00001735 .setCallee(CallingConv::C, PtrTy, TlsGetAddr, std::move(Args), 0);
Justin Holewinskiaa583972012-05-25 16:35:28 +00001736 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001737
Akira Hatanakabff84e12011-12-14 18:26:41 +00001738 SDValue Ret = CallResult.first;
1739
Hans Wennborgaea41202012-05-04 09:40:39 +00001740 if (model != TLSModel::LocalDynamic)
Akira Hatanakabff84e12011-12-14 18:26:41 +00001741 return Ret;
1742
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001743 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanakabff84e12011-12-14 18:26:41 +00001744 MipsII::MO_DTPREL_HI);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001745 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1746 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanakabff84e12011-12-14 18:26:41 +00001747 MipsII::MO_DTPREL_LO);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001748 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1749 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret);
1750 return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001751 }
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001752
1753 SDValue Offset;
Hans Wennborgaea41202012-05-04 09:40:39 +00001754 if (model == TLSModel::InitialExec) {
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001755 // Initial Exec TLS Model
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001756 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001757 MipsII::MO_GOTTPREL);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001758 TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT),
Akira Hatanakab049aef2012-02-24 22:34:47 +00001759 TGA);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001760 Offset = DAG.getLoad(PtrVT, DL,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001761 DAG.getEntryNode(), TGA, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001762 false, false, false, 0);
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001763 } else {
1764 // Local Exec TLS Model
Hans Wennborgaea41202012-05-04 09:40:39 +00001765 assert(model == TLSModel::LocalExec);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001766 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001767 MipsII::MO_TPREL_HI);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001768 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001769 MipsII::MO_TPREL_LO);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001770 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1771 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1772 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001773 }
1774
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001775 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
1776 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001777}
1778
1779SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001780lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +00001781{
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001782 JumpTableSDNode *N = cast<JumpTableSDNode>(Op);
1783 EVT Ty = Op.getValueType();
Akira Hatanaka30f97cf2013-09-25 00:30:25 +00001784
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001785 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ &&
Eric Christopher1c29a652014-07-18 22:55:25 +00001786 !Subtarget.isABI_N64())
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001787 return getAddrNonPIC(N, Ty, DAG);
1788
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001789 return getAddrLocal(N, Ty, DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +00001790 Subtarget.isABI_N32() || Subtarget.isABI_N64());
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +00001791}
1792
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001793SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001794lowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +00001795{
Bruno Cardoso Lopesfdb4cec2008-07-23 16:01:50 +00001796 // gp_rel relocation
Wesley Peck527da1b2010-11-23 03:31:01 +00001797 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001798 // but the asm printer currently doesn't support this feature without
Wesley Peck527da1b2010-11-23 03:31:01 +00001799 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopes98bda582008-07-28 19:26:25 +00001800 // stuff below.
Eli Friedman57c11da2009-08-03 02:22:28 +00001801 //if (IsInSmallSection(C->getType())) {
Owen Anderson9f944592009-08-11 20:47:22 +00001802 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1803 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peck527da1b2010-11-23 03:31:01 +00001804 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001805 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
1806 EVT Ty = Op.getValueType();
Bruno Cardoso Lopes2db07582009-11-25 12:17:58 +00001807
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001808 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ &&
Eric Christopher1c29a652014-07-18 22:55:25 +00001809 !Subtarget.isABI_N64())
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001810 return getAddrNonPIC(N, Ty, DAG);
Bruno Cardoso Lopesfdb4cec2008-07-23 16:01:50 +00001811
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001812 return getAddrLocal(N, Ty, DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +00001813 Subtarget.isABI_N32() || Subtarget.isABI_N64());
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +00001814}
1815
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001816SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman31ae5862010-04-17 14:41:14 +00001817 MachineFunction &MF = DAG.getMachineFunction();
1818 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1819
Andrew Trickef9de2a2013-05-25 02:42:55 +00001820 SDLoc DL(Op);
Dan Gohman31ae5862010-04-17 14:41:14 +00001821 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1822 getPointerTy());
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +00001823
1824 // vastart just stores the address of the VarArgsFrameIndex slot into the
1825 // memory location argument.
1826 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001827 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00001828 MachinePointerInfo(SV), false, false, 0);
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +00001829}
Jia Liuf54f60f2012-02-28 07:46:26 +00001830
Daniel Sanders2b553d42014-08-01 09:17:39 +00001831SDValue MipsTargetLowering::lowerVAARG(SDValue Op, SelectionDAG &DAG) const {
1832 SDNode *Node = Op.getNode();
1833 EVT VT = Node->getValueType(0);
1834 SDValue Chain = Node->getOperand(0);
1835 SDValue VAListPtr = Node->getOperand(1);
1836 unsigned Align = Node->getConstantOperandVal(3);
1837 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
1838 SDLoc DL(Node);
1839 unsigned ArgSlotSizeInBytes =
1840 (Subtarget.isABI_N32() || Subtarget.isABI_N64()) ? 8 : 4;
1841
1842 SDValue VAListLoad = DAG.getLoad(getPointerTy(), DL, Chain, VAListPtr,
1843 MachinePointerInfo(SV), false, false, false,
1844 0);
1845 SDValue VAList = VAListLoad;
1846
1847 // Re-align the pointer if necessary.
1848 // It should only ever be necessary for 64-bit types on O32 since the minimum
1849 // argument alignment is the same as the maximum type alignment for N32/N64.
1850 //
1851 // FIXME: We currently align too often. The code generator doesn't notice
1852 // when the pointer is still aligned from the last va_arg (or pair of
1853 // va_args for the i64 on O32 case).
1854 if (Align > getMinStackArgumentAlignment()) {
1855 assert(((Align & (Align-1)) == 0) && "Expected Align to be a power of 2");
1856
1857 VAList = DAG.getNode(ISD::ADD, DL, VAList.getValueType(), VAList,
1858 DAG.getConstant(Align - 1,
1859 VAList.getValueType()));
1860
1861 VAList = DAG.getNode(ISD::AND, DL, VAList.getValueType(), VAList,
1862 DAG.getConstant(-(int64_t)Align,
1863 VAList.getValueType()));
1864 }
1865
1866 // Increment the pointer, VAList, to the next vaarg.
1867 unsigned ArgSizeInBytes = getDataLayout()->getTypeAllocSize(VT.getTypeForEVT(*DAG.getContext()));
1868 SDValue Tmp3 = DAG.getNode(ISD::ADD, DL, VAList.getValueType(), VAList,
1869 DAG.getConstant(RoundUpToAlignment(ArgSizeInBytes, ArgSlotSizeInBytes),
1870 VAList.getValueType()));
1871 // Store the incremented VAList to the legalized pointer
1872 Chain = DAG.getStore(VAListLoad.getValue(1), DL, Tmp3, VAListPtr,
1873 MachinePointerInfo(SV), false, false, 0);
1874
1875 // In big-endian mode we must adjust the pointer when the load size is smaller
1876 // than the argument slot size. We must also reduce the known alignment to
1877 // match. For example in the N64 ABI, we must add 4 bytes to the offset to get
1878 // the correct half of the slot, and reduce the alignment from 8 (slot
1879 // alignment) down to 4 (type alignment).
1880 if (!Subtarget.isLittle() && ArgSizeInBytes < ArgSlotSizeInBytes) {
1881 unsigned Adjustment = ArgSlotSizeInBytes - ArgSizeInBytes;
1882 VAList = DAG.getNode(ISD::ADD, DL, VAListPtr.getValueType(), VAList,
1883 DAG.getIntPtrConstant(Adjustment));
1884 }
1885 // Load the actual argument out of the pointer VAList
1886 return DAG.getLoad(VT, DL, Chain, VAList, MachinePointerInfo(), false, false,
1887 false, 0);
1888}
1889
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001890static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG,
1891 bool HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001892 EVT TyX = Op.getOperand(0).getValueType();
1893 EVT TyY = Op.getOperand(1).getValueType();
1894 SDValue Const1 = DAG.getConstant(1, MVT::i32);
1895 SDValue Const31 = DAG.getConstant(31, MVT::i32);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001896 SDLoc DL(Op);
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001897 SDValue Res;
1898
1899 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1900 // to i32.
1901 SDValue X = (TyX == MVT::f32) ?
1902 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1903 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1904 Const1);
1905 SDValue Y = (TyY == MVT::f32) ?
1906 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
1907 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
1908 Const1);
1909
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001910 if (HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001911 // ext E, Y, 31, 1 ; extract bit31 of Y
1912 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
1913 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
1914 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
1915 } else {
1916 // sll SllX, X, 1
1917 // srl SrlX, SllX, 1
1918 // srl SrlY, Y, 31
1919 // sll SllY, SrlX, 31
1920 // or Or, SrlX, SllY
1921 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1922 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1923 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
1924 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
1925 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
1926 }
1927
1928 if (TyX == MVT::f32)
1929 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
1930
1931 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1932 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1933 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001934}
1935
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001936static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG,
1937 bool HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001938 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
1939 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
1940 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
1941 SDValue Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001942 SDLoc DL(Op);
Eric Christopher0713a9d2011-06-08 23:55:35 +00001943
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001944 // Bitcast to integer nodes.
1945 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
1946 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001947
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001948 if (HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001949 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
1950 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
1951 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
1952 DAG.getConstant(WidthY - 1, MVT::i32), Const1);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001953
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001954 if (WidthX > WidthY)
1955 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
1956 else if (WidthY > WidthX)
1957 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001958
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001959 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
1960 DAG.getConstant(WidthX - 1, MVT::i32), Const1, X);
1961 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
1962 }
1963
1964 // (d)sll SllX, X, 1
1965 // (d)srl SrlX, SllX, 1
1966 // (d)srl SrlY, Y, width(Y)-1
1967 // (d)sll SllY, SrlX, width(Y)-1
1968 // or Or, SrlX, SllY
1969 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
1970 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
1971 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
1972 DAG.getConstant(WidthY - 1, MVT::i32));
1973
1974 if (WidthX > WidthY)
1975 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
1976 else if (WidthY > WidthX)
1977 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
1978
1979 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
1980 DAG.getConstant(WidthX - 1, MVT::i32));
1981 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
1982 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001983}
1984
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00001985SDValue
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001986MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00001987 if (Subtarget.isGP64bit())
1988 return lowerFCOPYSIGN64(Op, DAG, Subtarget.hasExtractInsert());
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001989
Eric Christopher1c29a652014-07-18 22:55:25 +00001990 return lowerFCOPYSIGN32(Op, DAG, Subtarget.hasExtractInsert());
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001991}
1992
Akira Hatanaka66277522011-06-02 00:24:44 +00001993SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001994lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes5444a7b2011-06-16 00:40:02 +00001995 // check the depth
1996 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka15506782011-06-07 18:58:42 +00001997 "Frame address can only be determined for current frame.");
Akira Hatanaka66277522011-06-02 00:24:44 +00001998
1999 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2000 MFI->setFrameAddressIsTaken(true);
2001 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002002 SDLoc DL(Op);
Eric Christopherbf33a3c2014-07-02 23:18:40 +00002003 SDValue FrameAddr =
2004 DAG.getCopyFromReg(DAG.getEntryNode(), DL,
Eric Christopher1c29a652014-07-18 22:55:25 +00002005 Subtarget.isABI_N64() ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka66277522011-06-02 00:24:44 +00002006 return FrameAddr;
2007}
2008
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002009SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op,
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002010 SelectionDAG &DAG) const {
Bill Wendling908bf812014-01-06 00:43:20 +00002011 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00002012 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00002013
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002014 // check the depth
2015 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
2016 "Return address can be determined only for current frame.");
2017
2018 MachineFunction &MF = DAG.getMachineFunction();
2019 MachineFrameInfo *MFI = MF.getFrameInfo();
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00002020 MVT VT = Op.getSimpleValueType();
Eric Christopher1c29a652014-07-18 22:55:25 +00002021 unsigned RA = Subtarget.isABI_N64() ? Mips::RA_64 : Mips::RA;
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002022 MFI->setReturnAddressIsTaken(true);
2023
2024 // Return RA, which contains the return address. Mark it an implicit live-in.
2025 unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00002026 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT);
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002027}
2028
Akira Hatanakac0b02062013-01-30 00:26:49 +00002029// An EH_RETURN is the result of lowering llvm.eh.return which in turn is
2030// generated from __builtin_eh_return (offset, handler)
2031// The effect of this is to adjust the stack pointer by "offset"
2032// and then branch to "handler".
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002033SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Akira Hatanakac0b02062013-01-30 00:26:49 +00002034 const {
2035 MachineFunction &MF = DAG.getMachineFunction();
2036 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2037
2038 MipsFI->setCallsEhReturn();
2039 SDValue Chain = Op.getOperand(0);
2040 SDValue Offset = Op.getOperand(1);
2041 SDValue Handler = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002042 SDLoc DL(Op);
Eric Christopher1c29a652014-07-18 22:55:25 +00002043 EVT Ty = Subtarget.isABI_N64() ? MVT::i64 : MVT::i32;
Akira Hatanakac0b02062013-01-30 00:26:49 +00002044
2045 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
2046 // EH_RETURN nodes, so that instructions are emitted back-to-back.
Eric Christopher1c29a652014-07-18 22:55:25 +00002047 unsigned OffsetReg = Subtarget.isABI_N64() ? Mips::V1_64 : Mips::V1;
2048 unsigned AddrReg = Subtarget.isABI_N64() ? Mips::V0_64 : Mips::V0;
Akira Hatanakac0b02062013-01-30 00:26:49 +00002049 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
2050 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
2051 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
2052 DAG.getRegister(OffsetReg, Ty),
2053 DAG.getRegister(AddrReg, getPointerTy()),
2054 Chain.getValue(1));
2055}
2056
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002057SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
Akira Hatanaka5fd22482012-06-14 21:10:56 +00002058 SelectionDAG &DAG) const {
Eli Friedman26a48482011-07-27 22:21:52 +00002059 // FIXME: Need pseudo-fence for 'singlethread' fences
2060 // FIXME: Set SType for weaker fences where supported/appropriate.
2061 unsigned SType = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002062 SDLoc DL(Op);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002063 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
Eli Friedman26a48482011-07-27 22:21:52 +00002064 DAG.getConstant(SType, MVT::i32));
2065}
2066
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002067SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
Akira Hatanaka5fd22482012-06-14 21:10:56 +00002068 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002069 SDLoc DL(Op);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002070 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2071 SDValue Shamt = Op.getOperand(2);
2072
2073 // if shamt < 32:
2074 // lo = (shl lo, shamt)
2075 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
2076 // else:
2077 // lo = 0
2078 // hi = (shl lo, shamt[4:0])
2079 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
2080 DAG.getConstant(-1, MVT::i32));
2081 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo,
2082 DAG.getConstant(1, MVT::i32));
2083 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, ShiftRight1Lo,
2084 Not);
2085 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi, Shamt);
2086 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
2087 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, MVT::i32, Lo, Shamt);
2088 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
2089 DAG.getConstant(0x20, MVT::i32));
Akira Hatanaka5fd22482012-06-14 21:10:56 +00002090 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
2091 DAG.getConstant(0, MVT::i32), ShiftLeftLo);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002092 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftLeftLo, Or);
2093
2094 SDValue Ops[2] = {Lo, Hi};
Craig Topper64941d92014-04-27 19:20:57 +00002095 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002096}
2097
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002098SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002099 bool IsSRA) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002100 SDLoc DL(Op);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002101 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2102 SDValue Shamt = Op.getOperand(2);
2103
2104 // if shamt < 32:
2105 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
2106 // if isSRA:
2107 // hi = (sra hi, shamt)
2108 // else:
2109 // hi = (srl hi, shamt)
2110 // else:
2111 // if isSRA:
2112 // lo = (sra hi, shamt[4:0])
2113 // hi = (sra hi, 31)
2114 // else:
2115 // lo = (srl hi, shamt[4:0])
2116 // hi = 0
2117 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
2118 DAG.getConstant(-1, MVT::i32));
2119 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi,
2120 DAG.getConstant(1, MVT::i32));
2121 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, ShiftLeft1Hi, Not);
2122 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo, Shamt);
2123 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
2124 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL, DL, MVT::i32,
2125 Hi, Shamt);
2126 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
2127 DAG.getConstant(0x20, MVT::i32));
2128 SDValue Shift31 = DAG.getNode(ISD::SRA, DL, MVT::i32, Hi,
2129 DAG.getConstant(31, MVT::i32));
2130 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftRightHi, Or);
2131 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
2132 IsSRA ? Shift31 : DAG.getConstant(0, MVT::i32),
2133 ShiftRightHi);
2134
2135 SDValue Ops[2] = {Lo, Hi};
Craig Topper64941d92014-04-27 19:20:57 +00002136 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002137}
2138
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002139static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002140 SDValue Chain, SDValue Src, unsigned Offset) {
Akira Hatanaka95866182012-06-13 19:06:08 +00002141 SDValue Ptr = LD->getBasePtr();
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002142 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
Akira Hatanaka95866182012-06-13 19:06:08 +00002143 EVT BasePtrVT = Ptr.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002144 SDLoc DL(LD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002145 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
2146
2147 if (Offset)
Akira Hatanaka95866182012-06-13 19:06:08 +00002148 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002149 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002150
2151 SDValue Ops[] = { Chain, Ptr, Src };
Craig Topper206fcd42014-04-26 19:29:41 +00002152 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002153 LD->getMemOperand());
2154}
2155
2156// Expand an unaligned 32 or 64-bit integer load node.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002157SDValue MipsTargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002158 LoadSDNode *LD = cast<LoadSDNode>(Op);
2159 EVT MemVT = LD->getMemoryVT();
2160
Eric Christopher1c29a652014-07-18 22:55:25 +00002161 if (Subtarget.systemSupportsUnalignedAccess())
Daniel Sandersac272632014-05-23 13:18:02 +00002162 return Op;
2163
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002164 // Return if load is aligned or if MemVT is neither i32 nor i64.
2165 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
2166 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
2167 return SDValue();
2168
Eric Christopher1c29a652014-07-18 22:55:25 +00002169 bool IsLittle = Subtarget.isLittle();
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002170 EVT VT = Op.getValueType();
2171 ISD::LoadExtType ExtType = LD->getExtensionType();
2172 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
2173
2174 assert((VT == MVT::i32) || (VT == MVT::i64));
2175
2176 // Expand
2177 // (set dst, (i64 (load baseptr)))
2178 // to
2179 // (set tmp, (ldl (add baseptr, 7), undef))
2180 // (set dst, (ldr baseptr, tmp))
2181 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002182 SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002183 IsLittle ? 7 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002184 return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002185 IsLittle ? 0 : 7);
2186 }
2187
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002188 SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002189 IsLittle ? 3 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002190 SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002191 IsLittle ? 0 : 3);
2192
2193 // Expand
2194 // (set dst, (i32 (load baseptr))) or
2195 // (set dst, (i64 (sextload baseptr))) or
2196 // (set dst, (i64 (extload baseptr)))
2197 // to
2198 // (set tmp, (lwl (add baseptr, 3), undef))
2199 // (set dst, (lwr baseptr, tmp))
2200 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
2201 (ExtType == ISD::EXTLOAD))
2202 return LWR;
2203
2204 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
2205
2206 // Expand
2207 // (set dst, (i64 (zextload baseptr)))
2208 // to
2209 // (set tmp0, (lwl (add baseptr, 3), undef))
2210 // (set tmp1, (lwr baseptr, tmp0))
2211 // (set tmp2, (shl tmp1, 32))
2212 // (set dst, (srl tmp2, 32))
Andrew Trickef9de2a2013-05-25 02:42:55 +00002213 SDLoc DL(LD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002214 SDValue Const32 = DAG.getConstant(32, MVT::i32);
2215 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
Akira Hatanaka67346852012-06-04 17:46:29 +00002216 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
2217 SDValue Ops[] = { SRL, LWR.getValue(1) };
Craig Topper64941d92014-04-27 19:20:57 +00002218 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002219}
2220
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002221static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002222 SDValue Chain, unsigned Offset) {
Akira Hatanaka95866182012-06-13 19:06:08 +00002223 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
2224 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002225 SDLoc DL(SD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002226 SDVTList VTList = DAG.getVTList(MVT::Other);
2227
2228 if (Offset)
Akira Hatanaka95866182012-06-13 19:06:08 +00002229 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002230 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002231
2232 SDValue Ops[] = { Chain, Value, Ptr };
Craig Topper206fcd42014-04-26 19:29:41 +00002233 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002234 SD->getMemOperand());
2235}
2236
2237// Expand an unaligned 32 or 64-bit integer store node.
Akira Hatanakad82ee942013-05-16 20:45:17 +00002238static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG,
2239 bool IsLittle) {
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002240 SDValue Value = SD->getValue(), Chain = SD->getChain();
2241 EVT VT = Value.getValueType();
2242
2243 // Expand
2244 // (store val, baseptr) or
2245 // (truncstore val, baseptr)
2246 // to
2247 // (swl val, (add baseptr, 3))
2248 // (swr val, baseptr)
2249 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002250 SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002251 IsLittle ? 3 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002252 return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002253 }
2254
2255 assert(VT == MVT::i64);
2256
2257 // Expand
2258 // (store val, baseptr)
2259 // to
2260 // (sdl val, (add baseptr, 7))
2261 // (sdr val, baseptr)
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002262 SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2263 return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002264}
2265
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002266// Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr).
2267static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG) {
2268 SDValue Val = SD->getValue();
2269
2270 if (Val.getOpcode() != ISD::FP_TO_SINT)
2271 return SDValue();
2272
2273 EVT FPTy = EVT::getFloatingPointVT(Val.getValueSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002274 SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy,
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002275 Val.getOperand(0));
2276
Andrew Trickef9de2a2013-05-25 02:42:55 +00002277 return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(),
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002278 SD->getPointerInfo(), SD->isVolatile(),
2279 SD->isNonTemporal(), SD->getAlignment());
2280}
2281
Akira Hatanakad82ee942013-05-16 20:45:17 +00002282SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2283 StoreSDNode *SD = cast<StoreSDNode>(Op);
2284 EVT MemVT = SD->getMemoryVT();
2285
2286 // Lower unaligned integer stores.
Eric Christopher1c29a652014-07-18 22:55:25 +00002287 if (!Subtarget.systemSupportsUnalignedAccess() &&
Daniel Sandersac272632014-05-23 13:18:02 +00002288 (SD->getAlignment() < MemVT.getSizeInBits() / 8) &&
Akira Hatanakad82ee942013-05-16 20:45:17 +00002289 ((MemVT == MVT::i32) || (MemVT == MVT::i64)))
Eric Christopher1c29a652014-07-18 22:55:25 +00002290 return lowerUnalignedIntStore(SD, DAG, Subtarget.isLittle());
Akira Hatanakad82ee942013-05-16 20:45:17 +00002291
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002292 return lowerFP_TO_SINT_STORE(SD, DAG);
Akira Hatanakad82ee942013-05-16 20:45:17 +00002293}
2294
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002295SDValue MipsTargetLowering::lowerADD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002296 if (Op->getOperand(0).getOpcode() != ISD::FRAMEADDR
2297 || cast<ConstantSDNode>
2298 (Op->getOperand(0).getOperand(0))->getZExtValue() != 0
2299 || Op->getOperand(1).getOpcode() != ISD::FRAME_TO_ARGS_OFFSET)
2300 return SDValue();
2301
2302 // The pattern
2303 // (add (frameaddr 0), (frame_to_args_offset))
2304 // results from lowering llvm.eh.dwarf.cfa intrinsic. Transform it to
2305 // (add FrameObject, 0)
2306 // where FrameObject is a fixed StackObject with offset 0 which points to
2307 // the old stack pointer.
2308 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2309 EVT ValTy = Op->getValueType(0);
2310 int FI = MFI->CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
2311 SDValue InArgsAddr = DAG.getFrameIndex(FI, ValTy);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002312 return DAG.getNode(ISD::ADD, SDLoc(Op), ValTy, InArgsAddr,
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002313 DAG.getConstant(0, ValTy));
2314}
2315
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002316SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op,
2317 SelectionDAG &DAG) const {
2318 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002319 SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy,
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002320 Op.getOperand(0));
Andrew Trickef9de2a2013-05-25 02:42:55 +00002321 return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc);
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002322}
2323
Akira Hatanakae2489122011-04-15 21:51:11 +00002324//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002325// Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002326//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002327
Akira Hatanakae2489122011-04-15 21:51:11 +00002328//===----------------------------------------------------------------------===//
Wesley Peck527da1b2010-11-23 03:31:01 +00002329// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002330// Mips O32 ABI rules:
2331// ---
2332// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peck527da1b2010-11-23 03:31:01 +00002333// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002334// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peck527da1b2010-11-23 03:31:01 +00002335// f64 - Only passed in two aliased f32 registers if no int reg has been used
2336// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Sylvestre Ledru469de192014-08-11 18:04:46 +00002337// not used, it must be shadowed. If only A3 is available, shadow it and
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002338// go to stack.
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002339//
2340// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanakae2489122011-04-15 21:51:11 +00002341//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002342
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00002343static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2344 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
Craig Topper840beec2014-04-04 05:16:06 +00002345 CCState &State, const MCPhysReg *F64Regs) {
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002346
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00002347 static const unsigned IntRegsSize = 4, FloatRegsSize = 2;
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002348
Craig Topper840beec2014-04-04 05:16:06 +00002349 static const MCPhysReg IntRegs[] = { Mips::A0, Mips::A1, Mips::A2, Mips::A3 };
2350 static const MCPhysReg F32Regs[] = { Mips::F12, Mips::F14 };
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002351
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002352 // Do not process byval args here.
2353 if (ArgFlags.isByVal())
2354 return true;
Akira Hatanaka5e16c6a2011-05-24 19:18:33 +00002355
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002356 // Promote i8 and i16
2357 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
2358 LocVT = MVT::i32;
2359 if (ArgFlags.isSExt())
2360 LocInfo = CCValAssign::SExt;
2361 else if (ArgFlags.isZExt())
2362 LocInfo = CCValAssign::ZExt;
2363 else
2364 LocInfo = CCValAssign::AExt;
2365 }
2366
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002367 unsigned Reg;
2368
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002369 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
2370 // is true: function is vararg, argument is 3rd or higher, there is previous
2371 // argument which is not f32 or f64.
2372 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
2373 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanaka9e6a8cc2011-05-19 20:29:48 +00002374 unsigned OrigAlign = ArgFlags.getOrigAlign();
2375 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002376
2377 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002378 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanaka9e6a8cc2011-05-19 20:29:48 +00002379 // If this is the first part of an i64 arg,
2380 // the allocated register must be either A0 or A2.
2381 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
2382 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002383 LocVT = MVT::i32;
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002384 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
2385 // Allocate int register and shadow next int register. If first
2386 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002387 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2388 if (Reg == Mips::A1 || Reg == Mips::A3)
2389 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2390 State.AllocateReg(IntRegs, IntRegsSize);
2391 LocVT = MVT::i32;
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002392 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
2393 // we are guaranteed to find an available float register
2394 if (ValVT == MVT::f32) {
2395 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
2396 // Shadow int register
2397 State.AllocateReg(IntRegs, IntRegsSize);
2398 } else {
2399 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
2400 // Shadow int registers
2401 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
2402 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
2403 State.AllocateReg(IntRegs, IntRegsSize);
2404 State.AllocateReg(IntRegs, IntRegsSize);
2405 }
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002406 } else
2407 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002408
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002409 if (!Reg) {
2410 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3,
2411 OrigAlign);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002412 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002413 } else
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002414 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002415
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002416 return false;
Akira Hatanaka202f6402011-11-12 02:20:46 +00002417}
2418
Akira Hatanakabfb66242013-08-20 23:38:40 +00002419static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT,
2420 MVT LocVT, CCValAssign::LocInfo LocInfo,
2421 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002422 static const MCPhysReg F64Regs[] = { Mips::D6, Mips::D7 };
Akira Hatanakabfb66242013-08-20 23:38:40 +00002423
2424 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2425}
2426
2427static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT,
2428 MVT LocVT, CCValAssign::LocInfo LocInfo,
2429 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002430 static const MCPhysReg F64Regs[] = { Mips::D12_64, Mips::D14_64 };
Akira Hatanakabfb66242013-08-20 23:38:40 +00002431
2432 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2433}
2434
Akira Hatanaka202f6402011-11-12 02:20:46 +00002435#include "MipsGenCallingConv.inc"
2436
Akira Hatanakae2489122011-04-15 21:51:11 +00002437//===----------------------------------------------------------------------===//
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002438// Call Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002439//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002440
Akira Hatanaka61bbcce2011-09-23 00:58:33 +00002441// Return next O32 integer argument register.
2442static unsigned getNextIntArgReg(unsigned Reg) {
2443 assert((Reg == Mips::A0) || (Reg == Mips::A2));
2444 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
2445}
2446
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002447SDValue
2448MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002449 SDValue Chain, SDValue Arg, SDLoc DL,
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002450 bool IsTailCall, SelectionDAG &DAG) const {
2451 if (!IsTailCall) {
2452 SDValue PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr,
2453 DAG.getIntPtrConstant(Offset));
2454 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo(), false,
2455 false, 0);
2456 }
2457
2458 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2459 int FI = MFI->CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
2460 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2461 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(),
2462 /*isVolatile=*/ true, false, 0);
2463}
2464
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002465void MipsTargetLowering::
2466getOpndList(SmallVectorImpl<SDValue> &Ops,
2467 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
2468 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
Sasa Stankovic7072a792014-10-01 08:22:21 +00002469 bool IsCallReloc, CallLoweringInfo &CLI, SDValue Callee,
2470 SDValue Chain) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002471 // Insert node "GP copy globalreg" before call to function.
2472 //
2473 // R_MIPS_CALL* operators (emitted when non-internal functions are called
2474 // in PIC mode) allow symbols to be resolved via lazy binding.
2475 // The lazy binding stub requires GP to point to the GOT.
Sasa Stankovic7072a792014-10-01 08:22:21 +00002476 // Note that we don't need GP to point to the GOT for indirect calls
2477 // (when R_MIPS_CALL* is not used for the call) because Mips linker generates
2478 // lazy binding stub for a function only when R_MIPS_CALL* are the only relocs
2479 // used for the function (that is, Mips linker doesn't generate lazy binding
2480 // stub for a function whose address is taken in the program).
2481 if (IsPICCall && !InternalLinkage && IsCallReloc) {
Eric Christopher1c29a652014-07-18 22:55:25 +00002482 unsigned GPReg = Subtarget.isABI_N64() ? Mips::GP_64 : Mips::GP;
2483 EVT Ty = Subtarget.isABI_N64() ? MVT::i64 : MVT::i32;
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002484 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty)));
2485 }
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002486
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002487 // Build a sequence of copy-to-reg nodes chained together with token
2488 // chain and flag operands which copy the outgoing args into registers.
2489 // The InFlag in necessary since all emitted instructions must be
2490 // stuck together.
2491 SDValue InFlag;
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002492
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002493 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2494 Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, RegsToPass[i].first,
2495 RegsToPass[i].second, InFlag);
2496 InFlag = Chain.getValue(1);
2497 }
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002498
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002499 // Add argument registers to the end of the list so that they are
2500 // known live into the call.
2501 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2502 Ops.push_back(CLI.DAG.getRegister(RegsToPass[i].first,
2503 RegsToPass[i].second.getValueType()));
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002504
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002505 // Add a register mask operand representing the call-preserved registers.
Eric Christopherd9134482014-08-04 21:25:23 +00002506 const TargetRegisterInfo *TRI =
2507 getTargetMachine().getSubtargetImpl()->getRegisterInfo();
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002508 const uint32_t *Mask = TRI->getCallPreservedMask(CLI.CallConv);
2509 assert(Mask && "Missing call preserved mask for calling convention");
Eric Christopher1c29a652014-07-18 22:55:25 +00002510 if (Subtarget.inMips16HardFloat()) {
Reed Kotler783c7942013-05-10 22:25:39 +00002511 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
2512 llvm::StringRef Sym = G->getGlobal()->getName();
2513 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
Reed Kotler3230e722013-12-12 02:41:11 +00002514 if (F && F->hasFnAttribute("__Mips16RetHelper")) {
Reed Kotler783c7942013-05-10 22:25:39 +00002515 Mask = MipsRegisterInfo::getMips16RetHelperMask();
2516 }
2517 }
2518 }
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002519 Ops.push_back(CLI.DAG.getRegisterMask(Mask));
2520
2521 if (InFlag.getNode())
2522 Ops.push_back(InFlag);
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002523}
2524
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002525/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman624801e2009-01-26 03:15:54 +00002526/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002527SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00002528MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002529 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00002530 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002531 SDLoc DL = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00002532 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
2533 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
2534 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Akira Hatanakabeda2242012-07-31 18:46:41 +00002535 SDValue Chain = CLI.Chain;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002536 SDValue Callee = CLI.Callee;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002537 bool &IsTailCall = CLI.IsTailCall;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002538 CallingConv::ID CallConv = CLI.CallConv;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002539 bool IsVarArg = CLI.IsVarArg;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002540
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002541 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002542 MachineFrameInfo *MFI = MF.getFrameInfo();
Eric Christopherfc6de422014-08-05 02:39:49 +00002543 const TargetFrameLowering *TFL = MF.getSubtarget().getFrameLowering();
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002544 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +00002545 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002546
2547 // Analyze operands of the call, assigning locations to each operand.
2548 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002549 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
2550 *DAG.getContext());
Daniel Sanders068eea22014-11-01 17:44:51 +00002551 MipsCC MipsCCInfo(CallConv, Subtarget, CCInfo);
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002552
Daniel Sanders853c2432014-11-01 18:13:52 +00002553 MipsCCInfo.analyzeCallOperands(Outs, IsVarArg, Subtarget.abiUsesSoftFloat(),
2554 Callee.getNode(), CLI.getArgs(), CCInfo);
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)
2561 IsTailCall =
2562 isEligibleForTailCallOptimization(MipsCCInfo, NextStackOffset,
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002563 *MF.getInfo<MipsFunctionInfo>());
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002564
Reid Kleckner5772b772014-04-24 20:14:34 +00002565 if (!IsTailCall && CLI.CS && CLI.CS->isMustTailCall())
2566 report_fatal_error("failed to perform tail call elimination on a call "
2567 "site marked musttail");
2568
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002569 if (IsTailCall)
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002570 ++NumTailCalls;
2571
Akira Hatanaka79738332011-09-19 20:26:02 +00002572 // Chain is the output chain of the last Load/Store or CopyToReg node.
2573 // ByValChain is the output chain of the last Memcpy node created for copying
2574 // byval arguments to the stack.
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002575 unsigned StackAlignment = TFL->getStackAlignment();
2576 NextStackOffset = RoundUpToAlignment(NextStackOffset, StackAlignment);
Akira Hatanaka79738332011-09-19 20:26:02 +00002577 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002578
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002579 if (!IsTailCall)
Andrew Trickad6d08a2013-05-29 22:03:55 +00002580 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffsetVal, DL);
Akira Hatanakabeda2242012-07-31 18:46:41 +00002581
Daniel Sandersd897b562014-03-27 10:46:12 +00002582 SDValue StackPtr = DAG.getCopyFromReg(
Eric Christopher1c29a652014-07-18 22:55:25 +00002583 Chain, DL, Subtarget.isABI_N64() ? Mips::SP_64 : Mips::SP,
Eric Christopherbf33a3c2014-07-02 23:18:40 +00002584 getPointerTy());
Akira Hatanaka195a1e22011-06-08 17:39:33 +00002585
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002586 // With EABI is it possible to have 16 args on registers.
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002587 std::deque< std::pair<unsigned, SDValue> > RegsToPass;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002588 SmallVector<SDValue, 8> MemOpChains;
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002589 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002590
2591 // Walk the register/memloc assignments, inserting copies/loads.
2592 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002593 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002594 CCValAssign &VA = ArgLocs[i];
Akira Hatanakab20a3252011-10-28 19:49:00 +00002595 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka19891f82011-11-12 02:34:50 +00002596 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2597
2598 // ByVal Arg.
2599 if (Flags.isByVal()) {
2600 assert(Flags.getByValSize() &&
2601 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002602 assert(ByValArg != MipsCCInfo.byval_end());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002603 assert(!IsTailCall &&
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002604 "Do not tail-call optimize if there is a byval argument.");
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002605 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
Eric Christopher1c29a652014-07-18 22:55:25 +00002606 MipsCCInfo, *ByValArg, Flags, Subtarget.isLittle());
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002607 ++ByValArg;
Akira Hatanaka19891f82011-11-12 02:34:50 +00002608 continue;
2609 }
Jia Liuf54f60f2012-02-28 07:46:26 +00002610
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002611 // Promote the value if needed.
2612 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002613 default: llvm_unreachable("Unknown loc info!");
Wesley Peck527da1b2010-11-23 03:31:01 +00002614 case CCValAssign::Full:
Akira Hatanakab20a3252011-10-28 19:49:00 +00002615 if (VA.isRegLoc()) {
2616 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
Akira Hatanaka3b7391d2013-03-05 22:20:28 +00002617 (ValVT == MVT::f64 && LocVT == MVT::i64) ||
2618 (ValVT == MVT::i64 && LocVT == MVT::f64))
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002619 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
Akira Hatanakab20a3252011-10-28 19:49:00 +00002620 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002621 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanakae2489122011-04-15 21:51:11 +00002622 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002623 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanakaaef55c82011-04-15 21:00:26 +00002624 Arg, DAG.getConstant(1, MVT::i32));
Eric Christopher1c29a652014-07-18 22:55:25 +00002625 if (!Subtarget.isLittle())
Akira Hatanaka27916972011-04-15 19:52:08 +00002626 std::swap(Lo, Hi);
Jia Liuf54f60f2012-02-28 07:46:26 +00002627 unsigned LocRegLo = VA.getLocReg();
Akira Hatanaka61bbcce2011-09-23 00:58:33 +00002628 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2629 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2630 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002631 continue;
Wesley Peck527da1b2010-11-23 03:31:01 +00002632 }
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002633 }
2634 break;
Chris Lattner52f16de2008-03-17 06:57:02 +00002635 case CCValAssign::SExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002636 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002637 break;
2638 case CCValAssign::ZExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002639 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002640 break;
2641 case CCValAssign::AExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002642 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002643 break;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002644 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002645
2646 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +00002647 // RegsToPass vector
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002648 if (VA.isRegLoc()) {
2649 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattner52f16de2008-03-17 06:57:02 +00002650 continue;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002651 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002652
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002653 // Register can't get to this point...
Chris Lattner52f16de2008-03-17 06:57:02 +00002654 assert(VA.isMemLoc());
Wesley Peck527da1b2010-11-23 03:31:01 +00002655
Wesley Peck527da1b2010-11-23 03:31:01 +00002656 // emit ISD::STORE whichs stores the
Chris Lattner52f16de2008-03-17 06:57:02 +00002657 // parameter value to a stack Location
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002658 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002659 Chain, Arg, DL, IsTailCall, DAG));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002660 }
2661
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002662 // Transform all store nodes into one single node because all store
2663 // nodes are independent of each other.
Wesley Peck527da1b2010-11-23 03:31:01 +00002664 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00002665 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002666
Bill Wendling24c79f22008-09-16 21:48:12 +00002667 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peck527da1b2010-11-23 03:31:01 +00002668 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2669 // node so that legalize doesn't hack it.
Eric Christopherbf33a3c2014-07-02 23:18:40 +00002670 bool IsPICCall =
Eric Christopher1c29a652014-07-18 22:55:25 +00002671 (Subtarget.isABI_N64() || IsPIC); // true if calls are translated to
Eric Christopherbf33a3c2014-07-02 23:18:40 +00002672 // jalr $25
Sasa Stankovic7072a792014-10-01 08:22:21 +00002673 bool GlobalOrExternal = false, InternalLinkage = false, IsCallReloc = false;
Akira Hatanakad6f1c582011-04-07 19:51:44 +00002674 SDValue CalleeLo;
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002675 EVT Ty = Callee.getValueType();
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002676
2677 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002678 if (IsPICCall) {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002679 const GlobalValue *Val = G->getGlobal();
2680 InternalLinkage = Val->hasInternalLinkage();
Akira Hatanakacf9a61b2012-12-13 03:17:29 +00002681
2682 if (InternalLinkage)
Eric Christopherbf33a3c2014-07-02 23:18:40 +00002683 Callee = getAddrLocal(G, Ty, DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +00002684 Subtarget.isABI_N32() || Subtarget.isABI_N64());
Sasa Stankovic7072a792014-10-01 08:22:21 +00002685 else if (LargeGOT) {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002686 Callee = getAddrGlobalLargeGOT(G, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002687 MipsII::MO_CALL_LO16, Chain,
2688 FuncInfo->callPtrInfo(Val));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002689 IsCallReloc = true;
2690 } else {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002691 Callee = getAddrGlobal(G, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
2692 FuncInfo->callPtrInfo(Val));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002693 IsCallReloc = true;
2694 }
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002695 } else
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002696 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, getPointerTy(), 0,
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002697 MipsII::MO_NO_FLAG);
Akira Hatanaka8e16aac2011-12-09 01:45:12 +00002698 GlobalOrExternal = true;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002699 }
2700 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002701 const char *Sym = S->getSymbol();
2702
Eric Christopher1c29a652014-07-18 22:55:25 +00002703 if (!Subtarget.isABI_N64() && !IsPIC) // !N64 && static
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002704 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(),
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002705 MipsII::MO_NO_FLAG);
Sasa Stankovic7072a792014-10-01 08:22:21 +00002706 else if (LargeGOT) {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002707 Callee = getAddrGlobalLargeGOT(S, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002708 MipsII::MO_CALL_LO16, Chain,
2709 FuncInfo->callPtrInfo(Sym));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002710 IsCallReloc = true;
2711 } else { // N64 || PIC
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002712 Callee = getAddrGlobal(S, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
2713 FuncInfo->callPtrInfo(Sym));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002714 IsCallReloc = true;
2715 }
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002716
Akira Hatanaka8e16aac2011-12-09 01:45:12 +00002717 GlobalOrExternal = true;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002718 }
2719
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002720 SmallVector<SDValue, 8> Ops(1, Chain);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002721 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002722
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002723 getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal, InternalLinkage,
Sasa Stankovic7072a792014-10-01 08:22:21 +00002724 IsCallReloc, CLI, Callee, Chain);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002725
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002726 if (IsTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +00002727 return DAG.getNode(MipsISD::TailCall, DL, MVT::Other, Ops);
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002728
Craig Topper48d114b2014-04-26 18:35:24 +00002729 Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, Ops);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002730 SDValue InFlag = Chain.getValue(1);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002731
Bruno Cardoso Lopes193e64c2010-01-30 18:32:07 +00002732 // Create the CALLSEQ_END node.
Akira Hatanaka97ba7692012-07-26 23:27:01 +00002733 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal,
Andrew Trickad6d08a2013-05-29 22:03:55 +00002734 DAG.getIntPtrConstant(0, true), InFlag, DL);
Bruno Cardoso Lopes193e64c2010-01-30 18:32:07 +00002735 InFlag = Chain.getValue(1);
2736
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002737 // Handle result values, copying them out of physregs into vregs that we
2738 // return.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002739 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg, Ins, DL, DAG,
2740 InVals, CLI);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002741}
2742
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002743/// LowerCallResult - Lower the result values of a call into the
2744/// appropriate copies out of appropriate physical registers.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002745SDValue MipsTargetLowering::LowerCallResult(
2746 SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool IsVarArg,
2747 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG,
2748 SmallVectorImpl<SDValue> &InVals,
2749 TargetLowering::CallLoweringInfo &CLI) const {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002750 // Assign locations to each value returned by this call.
2751 SmallVector<CCValAssign, 16> RVLocs;
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002752 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
2753 *DAG.getContext());
2754 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips, CLI);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002755
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002756 // Copy all of the result registers out of their specified physreg.
2757 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Daniel Sandersae275e32014-09-25 12:15:05 +00002758 CCValAssign &VA = RVLocs[i];
2759 assert(VA.isRegLoc() && "Can only return in registers!");
2760
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002761 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(),
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002762 RVLocs[i].getLocVT(), InFlag);
2763 Chain = Val.getValue(1);
2764 InFlag = Val.getValue(2);
2765
Daniel Sandersae275e32014-09-25 12:15:05 +00002766 if (VA.isUpperBitsInLoc()) {
2767 unsigned ValSizeInBits = Ins[i].ArgVT.getSizeInBits();
2768 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
2769 unsigned Shift =
2770 VA.getLocInfo() == CCValAssign::ZExtUpper ? ISD::SRL : ISD::SRA;
2771 Val = DAG.getNode(
2772 Shift, DL, VA.getLocVT(), Val,
2773 DAG.getConstant(LocSizeInBits - ValSizeInBits, VA.getLocVT()));
2774 }
2775
2776 switch (VA.getLocInfo()) {
2777 default:
2778 llvm_unreachable("Unknown loc info!");
2779 case CCValAssign::Full:
2780 break;
2781 case CCValAssign::BCvt:
2782 Val = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Val);
2783 break;
2784 case CCValAssign::AExt:
2785 case CCValAssign::AExtUpper:
2786 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2787 break;
2788 case CCValAssign::ZExt:
2789 case CCValAssign::ZExtUpper:
2790 Val = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Val,
2791 DAG.getValueType(VA.getValVT()));
2792 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2793 break;
2794 case CCValAssign::SExt:
2795 case CCValAssign::SExtUpper:
2796 Val = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Val,
2797 DAG.getValueType(VA.getValVT()));
2798 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2799 break;
2800 }
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002801
2802 InVals.push_back(Val);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002803 }
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +00002804
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002805 return Chain;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002806}
2807
Akira Hatanakae2489122011-04-15 21:51:11 +00002808//===----------------------------------------------------------------------===//
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002809// Formal Arguments Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002810//===----------------------------------------------------------------------===//
Wesley Peck527da1b2010-11-23 03:31:01 +00002811/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002812/// and generate load operations for arguments places on the stack.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002813SDValue
2814MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanakaaef55c82011-04-15 21:00:26 +00002815 CallingConv::ID CallConv,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002816 bool IsVarArg,
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00002817 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002818 SDLoc DL, SelectionDAG &DAG,
Akira Hatanakaaef55c82011-04-15 21:00:26 +00002819 SmallVectorImpl<SDValue> &InVals)
Akira Hatanakae2489122011-04-15 21:51:11 +00002820 const {
Bruno Cardoso Lopesa01ede22008-08-04 07:12:52 +00002821 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002822 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopes14033fb2007-08-28 05:08:16 +00002823 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002824
Dan Gohman31ae5862010-04-17 14:41:14 +00002825 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002826
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002827 // Used with vargs to acumulate store chains.
2828 std::vector<SDValue> OutChains;
2829
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002830 // Assign locations to all of the incoming arguments.
2831 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002832 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
2833 *DAG.getContext());
Daniel Sanders4abcfe22014-09-09 10:46:48 +00002834 MipsCC MipsCCInfo(CallConv, Subtarget, CCInfo);
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002835 Function::const_arg_iterator FuncArg =
2836 DAG.getMachineFunction().getFunction()->arg_begin();
Eric Christopher1c29a652014-07-18 22:55:25 +00002837 bool UseSoftFloat = Subtarget.abiUsesSoftFloat();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002838
Daniel Sanders853c2432014-11-01 18:13:52 +00002839 MipsCCInfo.analyzeFormalArguments(Ins, UseSoftFloat, FuncArg, CCInfo);
Akira Hatanaka4866fe12012-10-30 19:37:25 +00002840 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(),
2841 MipsCCInfo.hasByValArg());
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002842
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00002843 unsigned CurArgIdx = 0;
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002844 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002845
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00002846 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002847 CCValAssign &VA = ArgLocs[i];
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00002848 std::advance(FuncArg, Ins[i].OrigArgIndex - CurArgIdx);
2849 CurArgIdx = Ins[i].OrigArgIndex;
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002850 EVT ValVT = VA.getValVT();
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002851 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2852 bool IsRegLoc = VA.isRegLoc();
2853
2854 if (Flags.isByVal()) {
2855 assert(Flags.getByValSize() &&
2856 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002857 assert(ByValArg != MipsCCInfo.byval_end());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002858 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg,
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002859 MipsCCInfo, *ByValArg);
2860 ++ByValArg;
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002861 continue;
2862 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002863
2864 // Arguments stored on registers
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002865 if (IsRegLoc) {
Akira Hatanaka7d822522013-10-28 21:21:36 +00002866 MVT RegVT = VA.getLocVT();
Akira Hatanakacb4a1a82011-05-24 00:23:52 +00002867 unsigned ArgReg = VA.getLocReg();
Akira Hatanaka7d822522013-10-28 21:21:36 +00002868 const TargetRegisterClass *RC = getRegClassFor(RegVT);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002869
Wesley Peck527da1b2010-11-23 03:31:01 +00002870 // Transform the arguments stored on
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002871 // physical registers into virtual ones
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002872 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC);
2873 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
Wesley Peck527da1b2010-11-23 03:31:01 +00002874
2875 // If this is an 8 or 16-bit value, it has been passed promoted
2876 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002877 // truncate to the right size.
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002878 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattner3c049702009-03-26 05:28:14 +00002879 unsigned Opcode = 0;
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002880 if (VA.getLocInfo() == CCValAssign::SExt)
2881 Opcode = ISD::AssertSext;
2882 else if (VA.getLocInfo() == CCValAssign::ZExt)
2883 Opcode = ISD::AssertZext;
Chris Lattner3c049702009-03-26 05:28:14 +00002884 if (Opcode)
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002885 ArgValue = DAG.getNode(Opcode, DL, RegVT, ArgValue,
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002886 DAG.getValueType(ValVT));
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002887 ArgValue = DAG.getNode(ISD::TRUNCATE, DL, ValVT, ArgValue);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002888 }
2889
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002890 // Handle floating point arguments passed in integer registers and
2891 // long double arguments passed in floating point registers.
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002892 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002893 (RegVT == MVT::i64 && ValVT == MVT::f64) ||
2894 (RegVT == MVT::f64 && ValVT == MVT::i64))
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002895 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
Eric Christopher1c29a652014-07-18 22:55:25 +00002896 else if (Subtarget.isABI_O32() && RegVT == MVT::i32 &&
Eric Christopherbf33a3c2014-07-02 23:18:40 +00002897 ValVT == MVT::f64) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002898 unsigned Reg2 = addLiveIn(DAG.getMachineFunction(),
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002899 getNextIntArgReg(ArgReg), RC);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002900 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT);
Eric Christopher1c29a652014-07-18 22:55:25 +00002901 if (!Subtarget.isLittle())
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002902 std::swap(ArgValue, ArgValue2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002903 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64,
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002904 ArgValue, ArgValue2);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002905 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002906
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002907 InVals.push_back(ArgValue);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002908 } else { // VA.isRegLoc()
2909
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002910 // sanity check
2911 assert(VA.isMemLoc());
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002912
Wesley Peck527da1b2010-11-23 03:31:01 +00002913 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002914 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakacb4a1a82011-05-24 00:23:52 +00002915 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002916
2917 // Create load nodes to retrieve arguments from the stack
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002918 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Akira Hatanakad1c58ed2013-11-09 02:38:51 +00002919 SDValue Load = DAG.getLoad(ValVT, DL, Chain, FIN,
2920 MachinePointerInfo::getFixedStack(FI),
2921 false, false, false, 0);
2922 InVals.push_back(Load);
2923 OutChains.push_back(Load.getValue(1));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002924 }
Reid Kleckner7a59e082014-05-12 22:01:27 +00002925 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002926
Reid Kleckner7a59e082014-05-12 22:01:27 +00002927 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Reid Kleckner79418562014-05-09 22:32:13 +00002928 // The mips ABIs for returning structs by value requires that we copy
2929 // the sret argument into $v0 for the return. Save the argument into
2930 // a virtual register so that we can access it from the return points.
Reid Kleckner7a59e082014-05-12 22:01:27 +00002931 if (Ins[i].Flags.isSRet()) {
Reid Kleckner79418562014-05-09 22:32:13 +00002932 unsigned Reg = MipsFI->getSRetReturnReg();
2933 if (!Reg) {
2934 Reg = MF.getRegInfo().createVirtualRegister(
Eric Christopher1c29a652014-07-18 22:55:25 +00002935 getRegClassFor(Subtarget.isABI_N64() ? MVT::i64 : MVT::i32));
Reid Kleckner79418562014-05-09 22:32:13 +00002936 MipsFI->setSRetReturnReg(Reg);
2937 }
Reid Kleckner7a59e082014-05-12 22:01:27 +00002938 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[i]);
Reid Kleckner79418562014-05-09 22:32:13 +00002939 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
Reid Kleckner7a59e082014-05-12 22:01:27 +00002940 break;
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002941 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002942 }
2943
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002944 if (IsVarArg)
Daniel Sanders853c2432014-11-01 18:13:52 +00002945 writeVarArgRegs(OutChains, MipsCCInfo, Chain, DL, DAG, CCInfo);
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002946
Wesley Peck527da1b2010-11-23 03:31:01 +00002947 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002948 // the size of Ins and InVals. This only happens when on varg functions
2949 if (!OutChains.empty()) {
2950 OutChains.push_back(Chain);
Craig Topper48d114b2014-04-26 18:35:24 +00002951 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002952 }
2953
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002954 return Chain;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002955}
2956
Akira Hatanakae2489122011-04-15 21:51:11 +00002957//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002958// Return Value Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002959//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002960
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00002961bool
2962MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002963 MachineFunction &MF, bool IsVarArg,
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00002964 const SmallVectorImpl<ISD::OutputArg> &Outs,
2965 LLVMContext &Context) const {
2966 SmallVector<CCValAssign, 16> RVLocs;
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002967 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00002968 return CCInfo.CheckReturn(Outs, RetCC_Mips);
2969}
2970
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002971SDValue
2972MipsTargetLowering::LowerReturn(SDValue Chain,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002973 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002974 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002975 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002976 SDLoc DL, SelectionDAG &DAG) const {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002977 // CCValAssign - represent the assignment of
2978 // the return value to a location
2979 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002980 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002981
2982 // CCState - Info about the registers and stack slot.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002983 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, *DAG.getContext());
Daniel Sanders4abcfe22014-09-09 10:46:48 +00002984 MipsCC MipsCCInfo(CallConv, Subtarget, CCInfo);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002985
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002986 // Analyze return values.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002987 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002988
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002989 SDValue Flag;
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00002990 SmallVector<SDValue, 4> RetOps(1, Chain);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002991
2992 // Copy the result values into the output registers.
2993 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002994 SDValue Val = OutVals[i];
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002995 CCValAssign &VA = RVLocs[i];
2996 assert(VA.isRegLoc() && "Can only return in registers!");
Daniel Sandersae275e32014-09-25 12:15:05 +00002997 bool UseUpperBits = false;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002998
Daniel Sandersae275e32014-09-25 12:15:05 +00002999 switch (VA.getLocInfo()) {
3000 default:
3001 llvm_unreachable("Unknown loc info!");
3002 case CCValAssign::Full:
3003 break;
3004 case CCValAssign::BCvt:
3005 Val = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Val);
3006 break;
3007 case CCValAssign::AExtUpper:
3008 UseUpperBits = true;
3009 // Fallthrough
3010 case CCValAssign::AExt:
3011 Val = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Val);
3012 break;
3013 case CCValAssign::ZExtUpper:
3014 UseUpperBits = true;
3015 // Fallthrough
3016 case CCValAssign::ZExt:
3017 Val = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Val);
3018 break;
3019 case CCValAssign::SExtUpper:
3020 UseUpperBits = true;
3021 // Fallthrough
3022 case CCValAssign::SExt:
3023 Val = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Val);
3024 break;
3025 }
3026
3027 if (UseUpperBits) {
3028 unsigned ValSizeInBits = Outs[i].ArgVT.getSizeInBits();
3029 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
3030 Val = DAG.getNode(
3031 ISD::SHL, DL, VA.getLocVT(), Val,
3032 DAG.getConstant(LocSizeInBits - ValSizeInBits, VA.getLocVT()));
3033 }
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003034
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003035 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003036
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003037 // Guarantee that all emitted copies are stuck together with flags.
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003038 Flag = Chain.getValue(1);
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003039 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003040 }
3041
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003042 // The mips ABIs for returning structs by value requires that we copy
3043 // the sret argument into $v0 for the return. We saved the argument into
3044 // a virtual register in the entry block, so now we copy the value out
3045 // and into $v0.
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003046 if (MF.getFunction()->hasStructRetAttr()) {
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003047 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3048 unsigned Reg = MipsFI->getSRetReturnReg();
3049
Wesley Peck527da1b2010-11-23 03:31:01 +00003050 if (!Reg)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003051 llvm_unreachable("sret virtual register not created in the entry block");
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003052 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Eric Christopher1c29a652014-07-18 22:55:25 +00003053 unsigned V0 = Subtarget.isABI_N64() ? Mips::V0_64 : Mips::V0;
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003054
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003055 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003056 Flag = Chain.getValue(1);
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003057 RetOps.push_back(DAG.getRegister(V0, getPointerTy()));
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003058 }
3059
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003060 RetOps[0] = Chain; // Update chain.
Akira Hatanakaefff7b72012-07-10 00:19:06 +00003061
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003062 // Add the flag if we have it.
3063 if (Flag.getNode())
3064 RetOps.push_back(Flag);
3065
3066 // Return on Mips is always a "jr $ra"
Craig Topper48d114b2014-04-26 18:35:24 +00003067 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, RetOps);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003068}
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003069
Akira Hatanakae2489122011-04-15 21:51:11 +00003070//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003071// Mips Inline Assembly Support
Akira Hatanakae2489122011-04-15 21:51:11 +00003072//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003073
3074/// getConstraintType - Given a constraint letter, return the type of
3075/// constraint it is for this target.
3076MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peck527da1b2010-11-23 03:31:01 +00003077getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003078{
Daniel Sanders8b59af12013-11-12 12:56:01 +00003079 // Mips specific constraints
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003080 // GCC config/mips/constraints.md
3081 //
Wesley Peck527da1b2010-11-23 03:31:01 +00003082 // 'd' : An address register. Equivalent to r
3083 // unless generating MIPS16 code.
3084 // 'y' : Equivalent to r; retained for
3085 // backwards compatibility.
Eric Christophere3c494d2012-05-07 06:25:10 +00003086 // 'c' : A register suitable for use in an indirect
3087 // jump. This will always be $25 for -mabicalls.
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003088 // 'l' : The lo register. 1 word storage.
3089 // 'x' : The hilo register pair. Double word storage.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003090 if (Constraint.size() == 1) {
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003091 switch (Constraint[0]) {
3092 default : break;
Wesley Peck527da1b2010-11-23 03:31:01 +00003093 case 'd':
3094 case 'y':
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003095 case 'f':
Eric Christophere3c494d2012-05-07 06:25:10 +00003096 case 'c':
Eric Christopher9c492e62012-05-07 06:25:15 +00003097 case 'l':
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003098 case 'x':
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003099 return C_RegisterClass;
Jack Carter0e149b02013-03-04 21:33:15 +00003100 case 'R':
3101 return C_Memory;
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003102 }
3103 }
3104 return TargetLowering::getConstraintType(Constraint);
3105}
3106
John Thompsone8360b72010-10-29 17:29:13 +00003107/// Examine constraint type and operand type and determine a weight value.
3108/// This object must already have been set up with the operand type
3109/// and the current alternative constraint selected.
3110TargetLowering::ConstraintWeight
3111MipsTargetLowering::getSingleConstraintMatchWeight(
3112 AsmOperandInfo &info, const char *constraint) const {
3113 ConstraintWeight weight = CW_Invalid;
3114 Value *CallOperandVal = info.CallOperandVal;
3115 // If we don't have a value, we can't do a match,
3116 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +00003117 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +00003118 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +00003119 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +00003120 // Look at the constraint type.
3121 switch (*constraint) {
3122 default:
3123 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
3124 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00003125 case 'd':
3126 case 'y':
John Thompsone8360b72010-10-29 17:29:13 +00003127 if (type->isIntegerTy())
3128 weight = CW_Register;
3129 break;
Daniel Sanders8b59af12013-11-12 12:56:01 +00003130 case 'f': // FPU or MSA register
Eric Christopher1c29a652014-07-18 22:55:25 +00003131 if (Subtarget.hasMSA() && type->isVectorTy() &&
Daniel Sanders8b59af12013-11-12 12:56:01 +00003132 cast<VectorType>(type)->getBitWidth() == 128)
3133 weight = CW_Register;
3134 else if (type->isFloatTy())
John Thompsone8360b72010-10-29 17:29:13 +00003135 weight = CW_Register;
3136 break;
Eric Christophere3c494d2012-05-07 06:25:10 +00003137 case 'c': // $25 for indirect jumps
Eric Christopher9c492e62012-05-07 06:25:15 +00003138 case 'l': // lo register
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003139 case 'x': // hilo register pair
Daniel Sanders8b59af12013-11-12 12:56:01 +00003140 if (type->isIntegerTy())
Eric Christophere3c494d2012-05-07 06:25:10 +00003141 weight = CW_SpecificReg;
Daniel Sanders8b59af12013-11-12 12:56:01 +00003142 break;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003143 case 'I': // signed 16 bit immediate
Eric Christopher7201e1b2012-05-07 03:13:42 +00003144 case 'J': // integer zero
Eric Christopher3ff88a02012-05-07 05:46:29 +00003145 case 'K': // unsigned 16 bit immediate
Eric Christopher1109b342012-05-07 05:46:37 +00003146 case 'L': // signed 32 bit immediate where lower 16 bits are 0
Eric Christophere07aa432012-05-07 05:46:43 +00003147 case 'N': // immediate in the range of -65535 to -1 (inclusive)
Eric Christopher470578a2012-05-07 05:46:48 +00003148 case 'O': // signed 15 bit immediate (+- 16383)
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003149 case 'P': // immediate in the range of 65535 to 1 (inclusive)
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003150 if (isa<ConstantInt>(CallOperandVal))
3151 weight = CW_Constant;
3152 break;
Jack Carter0e149b02013-03-04 21:33:15 +00003153 case 'R':
3154 weight = CW_Memory;
3155 break;
John Thompsone8360b72010-10-29 17:29:13 +00003156 }
3157 return weight;
3158}
3159
Akira Hatanaka7473b472013-08-14 00:21:25 +00003160/// This is a helper function to parse a physical register string and split it
3161/// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag
3162/// that is returned indicates whether parsing was successful. The second flag
3163/// is true if the numeric part exists.
3164static std::pair<bool, bool>
Craig Topper6dc4a8bc2014-08-30 16:48:02 +00003165parsePhysicalReg(StringRef C, std::string &Prefix,
Akira Hatanaka7473b472013-08-14 00:21:25 +00003166 unsigned long long &Reg) {
3167 if (C.front() != '{' || C.back() != '}')
3168 return std::make_pair(false, false);
3169
3170 // Search for the first numeric character.
3171 StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1;
3172 I = std::find_if(B, E, std::ptr_fun(isdigit));
3173
3174 Prefix.assign(B, I - B);
3175
3176 // The second flag is set to false if no numeric characters were found.
3177 if (I == E)
3178 return std::make_pair(true, false);
3179
3180 // Parse the numeric characters.
3181 return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg),
3182 true);
3183}
3184
3185std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering::
Craig Topper6dc4a8bc2014-08-30 16:48:02 +00003186parseRegForInlineAsmConstraint(StringRef C, MVT VT) const {
Eric Christopherd9134482014-08-04 21:25:23 +00003187 const TargetRegisterInfo *TRI =
3188 getTargetMachine().getSubtargetImpl()->getRegisterInfo();
Akira Hatanaka7473b472013-08-14 00:21:25 +00003189 const TargetRegisterClass *RC;
3190 std::string Prefix;
3191 unsigned long long Reg;
3192
3193 std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg);
3194
3195 if (!R.first)
Craig Topper062a2ba2014-04-25 05:30:21 +00003196 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003197
3198 if ((Prefix == "hi" || Prefix == "lo")) { // Parse hi/lo.
3199 // No numeric characters follow "hi" or "lo".
3200 if (R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003201 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003202
3203 RC = TRI->getRegClass(Prefix == "hi" ?
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00003204 Mips::HI32RegClassID : Mips::LO32RegClassID);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003205 return std::make_pair(*(RC->begin()), RC);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003206 } else if (Prefix.compare(0, 4, "$msa") == 0) {
3207 // Parse $msa(ir|csr|access|save|modify|request|map|unmap)
3208
3209 // No numeric characters follow the name.
3210 if (R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003211 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003212
3213 Reg = StringSwitch<unsigned long long>(Prefix)
3214 .Case("$msair", Mips::MSAIR)
3215 .Case("$msacsr", Mips::MSACSR)
3216 .Case("$msaaccess", Mips::MSAAccess)
3217 .Case("$msasave", Mips::MSASave)
3218 .Case("$msamodify", Mips::MSAModify)
3219 .Case("$msarequest", Mips::MSARequest)
3220 .Case("$msamap", Mips::MSAMap)
3221 .Case("$msaunmap", Mips::MSAUnmap)
3222 .Default(0);
3223
3224 if (!Reg)
Craig Topper062a2ba2014-04-25 05:30:21 +00003225 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003226
3227 RC = TRI->getRegClass(Mips::MSACtrlRegClassID);
3228 return std::make_pair(Reg, RC);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003229 }
3230
3231 if (!R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003232 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003233
3234 if (Prefix == "$f") { // Parse $f0-$f31.
3235 // If the size of FP registers is 64-bit or Reg is an even number, select
3236 // the 64-bit register class. Otherwise, select the 32-bit register class.
3237 if (VT == MVT::Other)
Eric Christopher1c29a652014-07-18 22:55:25 +00003238 VT = (Subtarget.isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32;
Akira Hatanaka7473b472013-08-14 00:21:25 +00003239
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003240 RC = getRegClassFor(VT);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003241
3242 if (RC == &Mips::AFGR64RegClass) {
3243 assert(Reg % 2 == 0);
3244 Reg >>= 1;
3245 }
Daniel Sanders8b59af12013-11-12 12:56:01 +00003246 } else if (Prefix == "$fcc") // Parse $fcc0-$fcc7.
Akira Hatanaka7473b472013-08-14 00:21:25 +00003247 RC = TRI->getRegClass(Mips::FCCRegClassID);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003248 else if (Prefix == "$w") { // Parse $w0-$w31.
3249 RC = getRegClassFor((VT == MVT::Other) ? MVT::v16i8 : VT);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003250 } else { // Parse $0-$31.
3251 assert(Prefix == "$");
3252 RC = getRegClassFor((VT == MVT::Other) ? MVT::i32 : VT);
3253 }
3254
3255 assert(Reg < RC->getNumRegs());
3256 return std::make_pair(*(RC->begin() + Reg), RC);
3257}
3258
Eric Christophereaf77dc2011-06-29 19:33:04 +00003259/// Given a register class constraint, like 'r', if this corresponds directly
3260/// to an LLVM register class, return a register of 0 and the register class
3261/// pointer.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003262std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Chad Rosier295bd432013-06-22 18:37:38 +00003263getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003264{
3265 if (Constraint.size() == 1) {
3266 switch (Constraint[0]) {
Eric Christopher9519c082011-06-29 19:04:31 +00003267 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
3268 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003269 case 'r':
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003270 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
Eric Christopher1c29a652014-07-18 22:55:25 +00003271 if (Subtarget.inMips16Mode())
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003272 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003273 return std::make_pair(0U, &Mips::GPR32RegClass);
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003274 }
Eric Christopher1c29a652014-07-18 22:55:25 +00003275 if (VT == MVT::i64 && !Subtarget.isGP64bit())
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003276 return std::make_pair(0U, &Mips::GPR32RegClass);
Eric Christopher1c29a652014-07-18 22:55:25 +00003277 if (VT == MVT::i64 && Subtarget.isGP64bit())
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003278 return std::make_pair(0U, &Mips::GPR64RegClass);
Eric Christopher58daf042012-05-07 03:13:22 +00003279 // This will generate an error message
Craig Topper062a2ba2014-04-25 05:30:21 +00003280 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003281 case 'f': // FPU or MSA register
3282 if (VT == MVT::v16i8)
3283 return std::make_pair(0U, &Mips::MSA128BRegClass);
3284 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
3285 return std::make_pair(0U, &Mips::MSA128HRegClass);
3286 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
3287 return std::make_pair(0U, &Mips::MSA128WRegClass);
3288 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
3289 return std::make_pair(0U, &Mips::MSA128DRegClass);
3290 else if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00003291 return std::make_pair(0U, &Mips::FGR32RegClass);
Eric Christopher1c29a652014-07-18 22:55:25 +00003292 else if ((VT == MVT::f64) && (!Subtarget.isSingleFloat())) {
3293 if (Subtarget.isFP64bit())
Craig Topperc7242e02012-04-20 07:30:17 +00003294 return std::make_pair(0U, &Mips::FGR64RegClass);
3295 return std::make_pair(0U, &Mips::AFGR64RegClass);
Akira Hatanakac669d7a2012-01-04 02:45:01 +00003296 }
Eric Christophere3c494d2012-05-07 06:25:10 +00003297 break;
3298 case 'c': // register suitable for indirect jump
3299 if (VT == MVT::i32)
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003300 return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass);
Eric Christophere3c494d2012-05-07 06:25:10 +00003301 assert(VT == MVT::i64 && "Unexpected type.");
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003302 return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass);
Eric Christopher9c492e62012-05-07 06:25:15 +00003303 case 'l': // register suitable for indirect jump
3304 if (VT == MVT::i32)
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00003305 return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass);
3306 return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass);
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003307 case 'x': // register suitable for indirect jump
3308 // Fixme: Not triggering the use of both hi and low
3309 // This will generate an error message
Craig Topper062a2ba2014-04-25 05:30:21 +00003310 return std::make_pair(0U, nullptr);
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003311 }
3312 }
Akira Hatanaka7473b472013-08-14 00:21:25 +00003313
3314 std::pair<unsigned, const TargetRegisterClass *> R;
3315 R = parseRegForInlineAsmConstraint(Constraint, VT);
3316
3317 if (R.second)
3318 return R;
3319
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003320 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3321}
3322
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003323/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3324/// vector. If it is invalid, don't add anything to Ops.
3325void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3326 std::string &Constraint,
3327 std::vector<SDValue>&Ops,
3328 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +00003329 SDValue Result;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003330
3331 // Only support length 1 constraints for now.
3332 if (Constraint.length() > 1) return;
3333
3334 char ConstraintLetter = Constraint[0];
3335 switch (ConstraintLetter) {
3336 default: break; // This will fall through to the generic implementation
3337 case 'I': // Signed 16 bit constant
3338 // If this fails, the parent routine will give an error
3339 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3340 EVT Type = Op.getValueType();
3341 int64_t Val = C->getSExtValue();
3342 if (isInt<16>(Val)) {
3343 Result = DAG.getTargetConstant(Val, Type);
3344 break;
3345 }
3346 }
3347 return;
Eric Christopher7201e1b2012-05-07 03:13:42 +00003348 case 'J': // integer zero
3349 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3350 EVT Type = Op.getValueType();
3351 int64_t Val = C->getZExtValue();
3352 if (Val == 0) {
3353 Result = DAG.getTargetConstant(0, Type);
3354 break;
3355 }
3356 }
3357 return;
Eric Christopher3ff88a02012-05-07 05:46:29 +00003358 case 'K': // unsigned 16 bit immediate
3359 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3360 EVT Type = Op.getValueType();
3361 uint64_t Val = (uint64_t)C->getZExtValue();
3362 if (isUInt<16>(Val)) {
3363 Result = DAG.getTargetConstant(Val, Type);
3364 break;
3365 }
3366 }
3367 return;
Eric Christopher1109b342012-05-07 05:46:37 +00003368 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3369 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3370 EVT Type = Op.getValueType();
3371 int64_t Val = C->getSExtValue();
3372 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
3373 Result = DAG.getTargetConstant(Val, Type);
3374 break;
3375 }
3376 }
3377 return;
Eric Christophere07aa432012-05-07 05:46:43 +00003378 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3379 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3380 EVT Type = Op.getValueType();
3381 int64_t Val = C->getSExtValue();
3382 if ((Val >= -65535) && (Val <= -1)) {
3383 Result = DAG.getTargetConstant(Val, Type);
3384 break;
3385 }
3386 }
3387 return;
Eric Christopher470578a2012-05-07 05:46:48 +00003388 case 'O': // signed 15 bit immediate
3389 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3390 EVT Type = Op.getValueType();
3391 int64_t Val = C->getSExtValue();
3392 if ((isInt<15>(Val))) {
3393 Result = DAG.getTargetConstant(Val, Type);
3394 break;
3395 }
3396 }
3397 return;
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003398 case 'P': // immediate in the range of 1 to 65535 (inclusive)
3399 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3400 EVT Type = Op.getValueType();
3401 int64_t Val = C->getSExtValue();
3402 if ((Val <= 65535) && (Val >= 1)) {
3403 Result = DAG.getTargetConstant(Val, Type);
3404 break;
3405 }
3406 }
3407 return;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003408 }
3409
3410 if (Result.getNode()) {
3411 Ops.push_back(Result);
3412 return;
3413 }
3414
3415 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3416}
3417
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003418bool MipsTargetLowering::isLegalAddressingMode(const AddrMode &AM,
3419 Type *Ty) const {
Akira Hatanakaef839192012-11-17 00:25:41 +00003420 // No global is ever allowed as a base.
3421 if (AM.BaseGV)
3422 return false;
3423
3424 switch (AM.Scale) {
3425 case 0: // "r+i" or just "i", depending on HasBaseReg.
3426 break;
3427 case 1:
3428 if (!AM.HasBaseReg) // allow "r+i".
3429 break;
3430 return false; // disallow "r+r" or "r+r+i".
3431 default:
3432 return false;
3433 }
3434
3435 return true;
3436}
3437
3438bool
Dan Gohman2fe6bee2008-10-18 02:06:02 +00003439MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3440 // The Mips target isn't yet aware of offsets.
3441 return false;
3442}
Evan Cheng16993aa2009-10-27 19:56:55 +00003443
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003444EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003445 unsigned SrcAlign,
3446 bool IsMemset, bool ZeroMemset,
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003447 bool MemcpyStrSrc,
3448 MachineFunction &MF) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003449 if (Subtarget.hasMips64())
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003450 return MVT::i64;
3451
3452 return MVT::i32;
3453}
3454
Evan Cheng83896a52009-10-28 01:43:28 +00003455bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
3456 if (VT != MVT::f32 && VT != MVT::f64)
3457 return false;
Bruno Cardoso Lopesb02a9df2011-01-18 19:41:41 +00003458 if (Imm.isNegZero())
3459 return false;
Evan Cheng16993aa2009-10-27 19:56:55 +00003460 return Imm.isZero();
3461}
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003462
3463unsigned MipsTargetLowering::getJumpTableEncoding() const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003464 if (Subtarget.isABI_N64())
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003465 return MachineJumpTableInfo::EK_GPRel64BlockAddress;
Jia Liuf54f60f2012-02-28 07:46:26 +00003466
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003467 return TargetLowering::getJumpTableEncoding();
3468}
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003469
Akira Hatanakae092f722013-03-05 22:54:59 +00003470/// This function returns true if CallSym is a long double emulation routine.
3471static bool isF128SoftLibCall(const char *CallSym) {
3472 const char *const LibCalls[] =
3473 {"__addtf3", "__divtf3", "__eqtf2", "__extenddftf2", "__extendsftf2",
3474 "__fixtfdi", "__fixtfsi", "__fixtfti", "__fixunstfdi", "__fixunstfsi",
3475 "__fixunstfti", "__floatditf", "__floatsitf", "__floattitf",
3476 "__floatunditf", "__floatunsitf", "__floatuntitf", "__getf2", "__gttf2",
3477 "__letf2", "__lttf2", "__multf3", "__netf2", "__powitf2", "__subtf3",
3478 "__trunctfdf2", "__trunctfsf2", "__unordtf2",
3479 "ceill", "copysignl", "cosl", "exp2l", "expl", "floorl", "fmal", "fmodl",
3480 "log10l", "log2l", "logl", "nearbyintl", "powl", "rintl", "sinl", "sqrtl",
3481 "truncl"};
3482
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003483 const char *const *End = LibCalls + array_lengthof(LibCalls);
Akira Hatanakae092f722013-03-05 22:54:59 +00003484
3485 // Check that LibCalls is sorted alphabetically.
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003486 MipsTargetLowering::LTStr Comp;
Akira Hatanakae092f722013-03-05 22:54:59 +00003487
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003488#ifndef NDEBUG
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003489 for (const char *const *I = LibCalls; I < End - 1; ++I)
Akira Hatanakae092f722013-03-05 22:54:59 +00003490 assert(Comp(*I, *(I + 1)));
3491#endif
3492
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003493 return std::binary_search(LibCalls, End, CallSym, Comp);
Akira Hatanakae092f722013-03-05 22:54:59 +00003494}
3495
Daniel Sandersf3fe49a2014-10-07 09:29:59 +00003496/// This function returns true if Ty is fp128, {f128} or i128 which was
3497/// originally a fp128.
Akira Hatanakae092f722013-03-05 22:54:59 +00003498static bool originalTypeIsF128(const Type *Ty, const SDNode *CallNode) {
3499 if (Ty->isFP128Ty())
3500 return true;
3501
Daniel Sandersf3fe49a2014-10-07 09:29:59 +00003502 if (Ty->isStructTy() && Ty->getStructNumElements() == 1 &&
3503 Ty->getStructElementType(0)->isFP128Ty())
3504 return true;
3505
Akira Hatanakae092f722013-03-05 22:54:59 +00003506 const ExternalSymbolSDNode *ES =
3507 dyn_cast_or_null<const ExternalSymbolSDNode>(CallNode);
3508
3509 // If the Ty is i128 and the function being called is a long double emulation
3510 // routine, then the original type is f128.
3511 return (ES && Ty->isIntegerTy(128) && isF128SoftLibCall(ES->getSymbol()));
3512}
3513
Reed Kotler783c7942013-05-10 22:25:39 +00003514MipsTargetLowering::MipsCC::SpecialCallingConvType
Daniel Sanders068eea22014-11-01 17:44:51 +00003515MipsTargetLowering::MipsCC::getSpecialCallingConv(const SDNode *Callee) const {
Reed Kotler783c7942013-05-10 22:25:39 +00003516 MipsCC::SpecialCallingConvType SpecialCallingConv =
Alp Toker98444342014-04-19 23:56:35 +00003517 MipsCC::NoSpecialCallingConv;
Eric Christopher1c29a652014-07-18 22:55:25 +00003518 if (Subtarget.inMips16HardFloat()) {
Daniel Sanders068eea22014-11-01 17:44:51 +00003519 if (const GlobalAddressSDNode *G =
3520 dyn_cast<const GlobalAddressSDNode>(Callee)) {
Reed Kotler783c7942013-05-10 22:25:39 +00003521 llvm::StringRef Sym = G->getGlobal()->getName();
3522 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
Reed Kotler3230e722013-12-12 02:41:11 +00003523 if (F && F->hasFnAttribute("__Mips16RetHelper")) {
Reed Kotler783c7942013-05-10 22:25:39 +00003524 SpecialCallingConv = MipsCC::Mips16RetHelperConv;
3525 }
3526 }
3527 }
3528 return SpecialCallingConv;
3529}
3530
Daniel Sanders068eea22014-11-01 17:44:51 +00003531MipsTargetLowering::MipsCC::MipsCC(CallingConv::ID CC,
3532 const MipsSubtarget &Subtarget_,
3533 CCState &Info)
Daniel Sanders853c2432014-11-01 18:13:52 +00003534 : CallConv(CC), Subtarget(Subtarget_) {
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003535 // Pre-allocate reserved argument area.
Daniel Sanders853c2432014-11-01 18:13:52 +00003536 Info.AllocateStack(reservedArgArea(), 1);
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003537}
3538
Daniel Sanders853c2432014-11-01 18:13:52 +00003539void MipsTargetLowering::MipsCC::analyzeCallOperands(
3540 const SmallVectorImpl<ISD::OutputArg> &Args, bool IsVarArg,
3541 bool IsSoftFloat, const SDNode *CallNode,
3542 std::vector<ArgListEntry> &FuncArgs, CCState &State) {
Daniel Sanders068eea22014-11-01 17:44:51 +00003543 MipsCC::SpecialCallingConvType SpecialCallingConv =
3544 getSpecialCallingConv(CallNode);
Akira Hatanaka5001be52013-02-15 21:45:11 +00003545 assert((CallConv != CallingConv::Fast || !IsVarArg) &&
3546 "CallingConv::Fast shouldn't be used for vararg functions.");
3547
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003548 unsigned NumOpnds = Args.size();
Daniel Sanders068eea22014-11-01 17:44:51 +00003549 llvm::CCAssignFn *FixedFn = CC_Mips_FixedArg;
3550 if (CallConv != CallingConv::Fast &&
3551 SpecialCallingConv == Mips16RetHelperConv)
3552 FixedFn = CC_Mips16RetHelper;
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003553
3554 for (unsigned I = 0; I != NumOpnds; ++I) {
3555 MVT ArgVT = Args[I].VT;
3556 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
3557 bool R;
3558
3559 if (ArgFlags.isByVal()) {
Daniel Sanders853c2432014-11-01 18:13:52 +00003560 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, State);
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003561 continue;
3562 }
3563
Akira Hatanaka5001be52013-02-15 21:45:11 +00003564 if (IsVarArg && !Args[I].IsFixed)
Daniel Sanders853c2432014-11-01 18:13:52 +00003565 R = CC_Mips_VarArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, State);
Akira Hatanaka3b7391d2013-03-05 22:20:28 +00003566 else {
3567 MVT RegVT = getRegVT(ArgVT, FuncArgs[Args[I].OrigArgIndex].Ty, CallNode,
3568 IsSoftFloat);
Daniel Sanders853c2432014-11-01 18:13:52 +00003569 R = FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, State);
Akira Hatanaka3b7391d2013-03-05 22:20:28 +00003570 }
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003571
3572 if (R) {
3573#ifndef NDEBUG
3574 dbgs() << "Call operand #" << I << " has unhandled type "
3575 << EVT(ArgVT).getEVTString();
3576#endif
Craig Toppere73658d2014-04-28 04:05:08 +00003577 llvm_unreachable(nullptr);
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003578 }
3579 }
3580}
3581
Daniel Sanders853c2432014-11-01 18:13:52 +00003582void MipsTargetLowering::MipsCC::analyzeFormalArguments(
3583 const SmallVectorImpl<ISD::InputArg> &Args, bool IsSoftFloat,
3584 Function::const_arg_iterator FuncArg, CCState &State) {
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003585 unsigned NumArgs = Args.size();
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003586 unsigned CurArgIdx = 0;
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003587
3588 for (unsigned I = 0; I != NumArgs; ++I) {
3589 MVT ArgVT = Args[I].VT;
3590 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003591 std::advance(FuncArg, Args[I].OrigArgIndex - CurArgIdx);
3592 CurArgIdx = Args[I].OrigArgIndex;
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003593
3594 if (ArgFlags.isByVal()) {
Daniel Sanders853c2432014-11-01 18:13:52 +00003595 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, State);
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003596 continue;
3597 }
3598
Craig Topper062a2ba2014-04-25 05:30:21 +00003599 MVT RegVT = getRegVT(ArgVT, FuncArg->getType(), nullptr, IsSoftFloat);
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003600
Daniel Sanders853c2432014-11-01 18:13:52 +00003601 if (!CC_Mips_FixedArg(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, State))
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003602 continue;
3603
3604#ifndef NDEBUG
3605 dbgs() << "Formal Arg #" << I << " has unhandled type "
3606 << EVT(ArgVT).getEVTString();
3607#endif
Craig Toppere73658d2014-04-28 04:05:08 +00003608 llvm_unreachable(nullptr);
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003609 }
3610}
3611
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003612void MipsTargetLowering::MipsCC::handleByValArg(unsigned ValNo, MVT ValVT,
3613 MVT LocVT,
3614 CCValAssign::LocInfo LocInfo,
Daniel Sanders853c2432014-11-01 18:13:52 +00003615 ISD::ArgFlagsTy ArgFlags,
3616 CCState &State) {
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003617 assert(ArgFlags.getByValSize() && "Byval argument's size shouldn't be 0.");
3618
3619 struct ByValArgInfo ByVal;
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003620 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
3621 unsigned ByValSize =
3622 RoundUpToAlignment(ArgFlags.getByValSize(), RegSizeInBytes);
3623 unsigned Align = std::min(std::max(ArgFlags.getByValAlign(), RegSizeInBytes),
3624 RegSizeInBytes * 2);
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003625
Akira Hatanaka5001be52013-02-15 21:45:11 +00003626 if (useRegsForByval())
Daniel Sanders853c2432014-11-01 18:13:52 +00003627 allocateRegs(ByVal, ByValSize, Align, State);
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003628
3629 // Allocate space on caller's stack.
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003630 ByVal.Address =
Daniel Sanders853c2432014-11-01 18:13:52 +00003631 State.AllocateStack(ByValSize - RegSizeInBytes * ByVal.NumRegs, Align);
3632 State.addLoc(
3633 CCValAssign::getMem(ValNo, ValVT, ByVal.Address, LocVT, LocInfo));
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003634 ByValArgs.push_back(ByVal);
3635}
3636
Akira Hatanaka5001be52013-02-15 21:45:11 +00003637unsigned MipsTargetLowering::MipsCC::reservedArgArea() const {
Daniel Sanders4abcfe22014-09-09 10:46:48 +00003638 return (Subtarget.isABI_O32() && (CallConv != CallingConv::Fast)) ? 16 : 0;
Akira Hatanaka5001be52013-02-15 21:45:11 +00003639}
3640
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003641const ArrayRef<MCPhysReg> MipsTargetLowering::MipsCC::intArgRegs() const {
3642 if (Subtarget.isABI_O32())
3643 return makeArrayRef(O32IntRegs);
3644 return makeArrayRef(Mips64IntRegs);
Akira Hatanaka5001be52013-02-15 21:45:11 +00003645}
3646
Craig Topper840beec2014-04-04 05:16:06 +00003647const MCPhysReg *MipsTargetLowering::MipsCC::shadowRegs() const {
Daniel Sanders4abcfe22014-09-09 10:46:48 +00003648 return Subtarget.isABI_O32() ? O32IntRegs : Mips64DPRegs;
Akira Hatanaka5001be52013-02-15 21:45:11 +00003649}
3650
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003651void MipsTargetLowering::MipsCC::allocateRegs(ByValArgInfo &ByVal,
3652 unsigned ByValSize,
Daniel Sanders853c2432014-11-01 18:13:52 +00003653 unsigned Align, CCState &State) {
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003654 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003655 const ArrayRef<MCPhysReg> IntArgRegs = intArgRegs();
3656 const MCPhysReg *ShadowRegs = shadowRegs();
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003657 assert(!(ByValSize % RegSizeInBytes) && !(Align % RegSizeInBytes) &&
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003658 "Byval argument's size and alignment should be a multiple of"
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003659 "RegSizeInBytes.");
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003660
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003661 ByVal.FirstIdx =
Daniel Sanders853c2432014-11-01 18:13:52 +00003662 State.getFirstUnallocated(IntArgRegs.data(), IntArgRegs.size());
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003663
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003664 // If Align > RegSizeInBytes, the first arg register must be even.
3665 if ((Align > RegSizeInBytes) && (ByVal.FirstIdx % 2)) {
Daniel Sanders853c2432014-11-01 18:13:52 +00003666 State.AllocateReg(IntArgRegs[ByVal.FirstIdx], ShadowRegs[ByVal.FirstIdx]);
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003667 ++ByVal.FirstIdx;
3668 }
3669
3670 // Mark the registers allocated.
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003671 for (unsigned I = ByVal.FirstIdx; ByValSize && (I < IntArgRegs.size());
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003672 ByValSize -= RegSizeInBytes, ++I, ++ByVal.NumRegs)
Daniel Sanders853c2432014-11-01 18:13:52 +00003673 State.AllocateReg(IntArgRegs[I], ShadowRegs[I]);
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003674}
Akira Hatanaka25dad192012-10-27 00:10:18 +00003675
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003676MVT MipsTargetLowering::MipsCC::getRegVT(MVT VT, const Type *OrigTy,
3677 const SDNode *CallNode,
3678 bool IsSoftFloat) const {
Daniel Sanders4abcfe22014-09-09 10:46:48 +00003679 if (IsSoftFloat || Subtarget.isABI_O32())
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003680 return VT;
3681
3682 // Check if the original type was fp128.
Akira Hatanakae092f722013-03-05 22:54:59 +00003683 if (originalTypeIsF128(OrigTy, CallNode)) {
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003684 assert(VT == MVT::i64);
3685 return MVT::f64;
3686 }
3687
3688 return VT;
3689}
3690
Akira Hatanaka25dad192012-10-27 00:10:18 +00003691void MipsTargetLowering::
Andrew Trickef9de2a2013-05-25 02:42:55 +00003692copyByValRegs(SDValue Chain, SDLoc DL, std::vector<SDValue> &OutChains,
Akira Hatanaka25dad192012-10-27 00:10:18 +00003693 SelectionDAG &DAG, const ISD::ArgFlagsTy &Flags,
3694 SmallVectorImpl<SDValue> &InVals, const Argument *FuncArg,
3695 const MipsCC &CC, const ByValArgInfo &ByVal) const {
3696 MachineFunction &MF = DAG.getMachineFunction();
3697 MachineFrameInfo *MFI = MF.getFrameInfo();
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003698 unsigned GPRSizeInBytes = Subtarget.getGPRSizeInBytes();
3699 unsigned RegAreaSize = ByVal.NumRegs * GPRSizeInBytes;
Akira Hatanaka25dad192012-10-27 00:10:18 +00003700 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
3701 int FrameObjOffset;
3702
3703 if (RegAreaSize)
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003704 FrameObjOffset =
3705 (int)CC.reservedArgArea() -
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003706 (int)((CC.intArgRegs().size() - ByVal.FirstIdx) * GPRSizeInBytes);
Akira Hatanaka25dad192012-10-27 00:10:18 +00003707 else
3708 FrameObjOffset = ByVal.Address;
3709
3710 // Create frame object.
3711 EVT PtrTy = getPointerTy();
3712 int FI = MFI->CreateFixedObject(FrameObjSize, FrameObjOffset, true);
3713 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
3714 InVals.push_back(FIN);
3715
3716 if (!ByVal.NumRegs)
3717 return;
3718
3719 // Copy arg registers.
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003720 MVT RegTy = MVT::getIntegerVT(GPRSizeInBytes * 8);
Akira Hatanaka25dad192012-10-27 00:10:18 +00003721 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3722
3723 for (unsigned I = 0; I < ByVal.NumRegs; ++I) {
3724 unsigned ArgReg = CC.intArgRegs()[ByVal.FirstIdx + I];
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003725 unsigned VReg = addLiveIn(MF, ArgReg, RC);
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003726 unsigned Offset = I * GPRSizeInBytes;
Akira Hatanaka25dad192012-10-27 00:10:18 +00003727 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
3728 DAG.getConstant(Offset, PtrTy));
3729 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
3730 StorePtr, MachinePointerInfo(FuncArg, Offset),
3731 false, false, 0);
3732 OutChains.push_back(Store);
3733 }
3734}
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003735
3736// Copy byVal arg to registers and stack.
3737void MipsTargetLowering::
Andrew Trickef9de2a2013-05-25 02:42:55 +00003738passByValArg(SDValue Chain, SDLoc DL,
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00003739 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
Craig Topperb94011f2013-07-14 04:42:23 +00003740 SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr,
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003741 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
3742 const MipsCC &CC, const ByValArgInfo &ByVal,
3743 const ISD::ArgFlagsTy &Flags, bool isLittle) const {
Daniel Sandersac272632014-05-23 13:18:02 +00003744 unsigned ByValSizeInBytes = Flags.getByValSize();
3745 unsigned OffsetInBytes = 0; // From beginning of struct
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003746 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
Daniel Sandersac272632014-05-23 13:18:02 +00003747 unsigned Alignment = std::min(Flags.getByValAlign(), RegSizeInBytes);
3748 EVT PtrTy = getPointerTy(), RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003749
3750 if (ByVal.NumRegs) {
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003751 const ArrayRef<MCPhysReg> ArgRegs = CC.intArgRegs();
Daniel Sandersac272632014-05-23 13:18:02 +00003752 bool LeftoverBytes = (ByVal.NumRegs * RegSizeInBytes > ByValSizeInBytes);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003753 unsigned I = 0;
3754
3755 // Copy words to registers.
Daniel Sandersac272632014-05-23 13:18:02 +00003756 for (; I < ByVal.NumRegs - LeftoverBytes;
3757 ++I, OffsetInBytes += RegSizeInBytes) {
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003758 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Daniel Sandersac272632014-05-23 13:18:02 +00003759 DAG.getConstant(OffsetInBytes, PtrTy));
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003760 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
3761 MachinePointerInfo(), false, false, false,
3762 Alignment);
3763 MemOpChains.push_back(LoadVal.getValue(1));
3764 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3765 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
3766 }
3767
3768 // Return if the struct has been fully copied.
Daniel Sandersac272632014-05-23 13:18:02 +00003769 if (ByValSizeInBytes == OffsetInBytes)
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003770 return;
3771
3772 // Copy the remainder of the byval argument with sub-word loads and shifts.
3773 if (LeftoverBytes) {
Daniel Sandersac272632014-05-23 13:18:02 +00003774 assert((ByValSizeInBytes > OffsetInBytes) &&
3775 (ByValSizeInBytes < OffsetInBytes + RegSizeInBytes) &&
3776 "Size of the remainder should be smaller than RegSizeInBytes.");
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003777 SDValue Val;
3778
Daniel Sandersac272632014-05-23 13:18:02 +00003779 for (unsigned LoadSizeInBytes = RegSizeInBytes / 2, TotalBytesLoaded = 0;
3780 OffsetInBytes < ByValSizeInBytes; LoadSizeInBytes /= 2) {
3781 unsigned RemainingSizeInBytes = ByValSizeInBytes - OffsetInBytes;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003782
Daniel Sandersac272632014-05-23 13:18:02 +00003783 if (RemainingSizeInBytes < LoadSizeInBytes)
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003784 continue;
3785
3786 // Load subword.
3787 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Daniel Sandersac272632014-05-23 13:18:02 +00003788 DAG.getConstant(OffsetInBytes, PtrTy));
3789 SDValue LoadVal = DAG.getExtLoad(
3790 ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr, MachinePointerInfo(),
Louis Gerbarg67474e32014-07-31 21:45:05 +00003791 MVT::getIntegerVT(LoadSizeInBytes * 8), false, false, false,
3792 Alignment);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003793 MemOpChains.push_back(LoadVal.getValue(1));
3794
3795 // Shift the loaded value.
3796 unsigned Shamt;
3797
3798 if (isLittle)
Daniel Sandersac272632014-05-23 13:18:02 +00003799 Shamt = TotalBytesLoaded * 8;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003800 else
Daniel Sandersac272632014-05-23 13:18:02 +00003801 Shamt = (RegSizeInBytes - (TotalBytesLoaded + LoadSizeInBytes)) * 8;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003802
3803 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
3804 DAG.getConstant(Shamt, MVT::i32));
3805
3806 if (Val.getNode())
3807 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
3808 else
3809 Val = Shift;
3810
Daniel Sandersac272632014-05-23 13:18:02 +00003811 OffsetInBytes += LoadSizeInBytes;
3812 TotalBytesLoaded += LoadSizeInBytes;
3813 Alignment = std::min(Alignment, LoadSizeInBytes);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003814 }
3815
3816 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3817 RegsToPass.push_back(std::make_pair(ArgReg, Val));
3818 return;
3819 }
3820 }
3821
3822 // Copy remainder of byval arg to it with memcpy.
Daniel Sandersac272632014-05-23 13:18:02 +00003823 unsigned MemCpySize = ByValSizeInBytes - OffsetInBytes;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003824 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Daniel Sandersac272632014-05-23 13:18:02 +00003825 DAG.getConstant(OffsetInBytes, PtrTy));
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003826 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
3827 DAG.getIntPtrConstant(ByVal.Address));
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003828 Chain = DAG.getMemcpy(Chain, DL, Dst, Src, DAG.getConstant(MemCpySize, PtrTy),
3829 Alignment, /*isVolatile=*/false, /*AlwaysInline=*/false,
Nick Lewyckyaad475b2014-04-15 07:22:52 +00003830 MachinePointerInfo(), MachinePointerInfo());
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003831 MemOpChains.push_back(Chain);
3832}
Akira Hatanaka2a134022012-10-27 00:21:13 +00003833
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003834void MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
3835 const MipsCC &CC, SDValue Chain,
Daniel Sanders853c2432014-11-01 18:13:52 +00003836 SDLoc DL, SelectionDAG &DAG,
3837 CCState &State) const {
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003838 const ArrayRef<MCPhysReg> ArgRegs = CC.intArgRegs();
Daniel Sanders853c2432014-11-01 18:13:52 +00003839 unsigned Idx = State.getFirstUnallocated(ArgRegs.data(), ArgRegs.size());
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003840 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
3841 MVT RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003842 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3843 MachineFunction &MF = DAG.getMachineFunction();
3844 MachineFrameInfo *MFI = MF.getFrameInfo();
3845 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3846
3847 // Offset of the first variable argument from stack pointer.
3848 int VaArgOffset;
3849
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003850 if (ArgRegs.size() == Idx)
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003851 VaArgOffset =
Daniel Sanders853c2432014-11-01 18:13:52 +00003852 RoundUpToAlignment(State.getNextStackOffset(), RegSizeInBytes);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003853 else
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003854 VaArgOffset = (int)CC.reservedArgArea() -
3855 (int)(RegSizeInBytes * (ArgRegs.size() - Idx));
Akira Hatanaka2a134022012-10-27 00:21:13 +00003856
3857 // Record the frame index of the first variable argument
3858 // which is a value necessary to VASTART.
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003859 int FI = MFI->CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003860 MipsFI->setVarArgsFrameIndex(FI);
3861
3862 // Copy the integer registers that have not been used for argument passing
3863 // to the argument register save area. For O32, the save area is allocated
3864 // in the caller's stack frame, while for N32/64, it is allocated in the
3865 // callee's stack frame.
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003866 for (unsigned I = Idx; I < ArgRegs.size();
3867 ++I, VaArgOffset += RegSizeInBytes) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003868 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003869 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003870 FI = MFI->CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003871 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
3872 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
3873 MachinePointerInfo(), false, false, 0);
Eric Christopher1c29a652014-07-18 22:55:25 +00003874 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(
3875 (Value *)nullptr);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003876 OutChains.push_back(Store);
3877 }
3878}