blob: 5b87111d5add9495122fe40c2ae2e65207b4a60c [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
Daniel Sanderseac096082014-11-01 18:38:03 +000098 /// Identify lowered values that originated from f128 arguments and record
99 /// this.
100 void PreAnalyzeCallOperandsForF128(
101 const SmallVectorImpl<ISD::OutputArg> &Outs,
102 std::vector<TargetLowering::ArgListEntry> &FuncArgs, SDNode *CallNode) {
103 const MachineFunction &MF = getMachineFunction();
104 for (unsigned i = 0; i < Outs.size(); ++i)
105 OriginalArgWasF128.push_back(
106 originalTypeIsF128(FuncArgs[Outs[i].OrigArgIndex].Ty, CallNode));
107 }
108
109 /// Identify lowered values that originated from f128 arguments and record
110 /// this.
111 void
112 PreAnalyzeFormalArgumentsForF128(const SmallVectorImpl<ISD::InputArg> &Ins) {
113 const MachineFunction &MF = getMachineFunction();
114 for (unsigned i = 0; i < Ins.size(); ++i) {
115 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
116 std::advance(FuncArg, Ins[i].OrigArgIndex);
117
118 OriginalArgWasF128.push_back(
119 originalTypeIsF128(FuncArg->getType(), nullptr));
120 }
121 }
122
Daniel Sandersb3ca3382014-09-26 10:06:12 +0000123 /// Records whether the value has been lowered from an f128.
124 SmallVector<bool, 4> OriginalArgWasF128;
125
126public:
Daniel Sanderseac096082014-11-01 18:38:03 +0000127 // FIXME: Remove this from a public inteface ASAP. It's a temporary trap door
128 // to allow analyzeCallOperands to be removed incrementally.
129 void PreAnalyzeCallOperandsForF128_(
130 const SmallVectorImpl<ISD::OutputArg> &Outs,
131 std::vector<TargetLowering::ArgListEntry> &FuncArgs, SDNode *CallNode) {
132 PreAnalyzeCallOperandsForF128(Outs, FuncArgs, CallNode);
133 }
134 // FIXME: Remove this from a public inteface ASAP. It's a temporary trap door
135 // to allow analyzeFormalArguments to be removed incrementally.
136 void
137 PreAnalyzeFormalArgumentsForF128_(const SmallVectorImpl<ISD::InputArg> &Ins) {
138 PreAnalyzeFormalArgumentsForF128(Ins);
139 }
140 // FIXME: Remove this from a public inteface ASAP. It's a temporary trap door
141 // to clean up after the above functions.
142 void ClearOriginalArgWasF128() { OriginalArgWasF128.clear(); }
143
Daniel Sandersb3ca3382014-09-26 10:06:12 +0000144 MipsCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
145 SmallVectorImpl<CCValAssign> &locs, LLVMContext &C)
146 : CCState(CC, isVarArg, MF, locs, C) {}
147
148 void AnalyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins,
149 CCAssignFn Fn,
150 const TargetLowering::CallLoweringInfo &CLI) {
151 PreAnalyzeCallResultForF128(Ins, CLI);
152 CCState::AnalyzeCallResult(Ins, Fn);
153 OriginalArgWasF128.clear();
154 }
155
156 void AnalyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
157 CCAssignFn Fn) {
158 PreAnalyzeReturnForF128(Outs);
159 CCState::AnalyzeReturn(Outs, Fn);
160 OriginalArgWasF128.clear();
161 }
162
163 bool CheckReturn(const SmallVectorImpl<ISD::OutputArg> &ArgsFlags,
164 CCAssignFn Fn) {
165 PreAnalyzeReturnForF128(ArgsFlags);
166 bool Return = CCState::CheckReturn(ArgsFlags, Fn);
167 OriginalArgWasF128.clear();
168 return Return;
169 }
170
171 bool WasOriginalArgF128(unsigned ValNo) { return OriginalArgWasF128[ValNo]; }
172};
173}
174
Jia Liuf54f60f2012-02-28 07:46:26 +0000175// If I is a shifted mask, set the size (Size) and the first bit of the
Akira Hatanaka73d78b72011-08-18 20:07:42 +0000176// mask (Pos), and return true.
Jia Liuf54f60f2012-02-28 07:46:26 +0000177// For example, if I is 0x003ff800, (Pos, Size) = (11, 11).
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000178static bool isShiftedMask(uint64_t I, uint64_t &Pos, uint64_t &Size) {
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000179 if (!isShiftedMask_64(I))
Akira Hatanaka4c0a7122013-10-07 19:33:02 +0000180 return false;
Akira Hatanaka5360f882011-08-17 02:05:42 +0000181
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000182 Size = CountPopulation_64(I);
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000183 Pos = countTrailingZeros(I);
Akira Hatanaka73d78b72011-08-18 20:07:42 +0000184 return true;
Akira Hatanaka5360f882011-08-17 02:05:42 +0000185}
186
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000187SDValue MipsTargetLowering::getGlobalReg(SelectionDAG &DAG, EVT Ty) const {
Akira Hatanakab049aef2012-02-24 22:34:47 +0000188 MipsFunctionInfo *FI = DAG.getMachineFunction().getInfo<MipsFunctionInfo>();
189 return DAG.getRegister(FI->getGlobalBaseReg(), Ty);
190}
191
Akira Hatanakad8f10ce2013-09-27 19:51:35 +0000192SDValue MipsTargetLowering::getTargetNode(GlobalAddressSDNode *N, EVT Ty,
193 SelectionDAG &DAG,
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000194 unsigned Flag) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +0000195 return DAG.getTargetGlobalAddress(N->getGlobal(), SDLoc(N), Ty, 0, Flag);
Akira Hatanakafd04ad42012-11-21 20:26:38 +0000196}
197
Akira Hatanakad8f10ce2013-09-27 19:51:35 +0000198SDValue MipsTargetLowering::getTargetNode(ExternalSymbolSDNode *N, EVT Ty,
199 SelectionDAG &DAG,
200 unsigned Flag) const {
201 return DAG.getTargetExternalSymbol(N->getSymbol(), Ty, Flag);
202}
203
204SDValue MipsTargetLowering::getTargetNode(BlockAddressSDNode *N, EVT Ty,
205 SelectionDAG &DAG,
206 unsigned Flag) const {
207 return DAG.getTargetBlockAddress(N->getBlockAddress(), Ty, 0, Flag);
208}
209
210SDValue MipsTargetLowering::getTargetNode(JumpTableSDNode *N, EVT Ty,
211 SelectionDAG &DAG,
212 unsigned Flag) const {
213 return DAG.getTargetJumpTable(N->getIndex(), Ty, Flag);
214}
215
216SDValue MipsTargetLowering::getTargetNode(ConstantPoolSDNode *N, EVT Ty,
217 SelectionDAG &DAG,
218 unsigned Flag) const {
219 return DAG.getTargetConstantPool(N->getConstVal(), Ty, N->getAlignment(),
220 N->getOffset(), Flag);
Akira Hatanakafd04ad42012-11-21 20:26:38 +0000221}
222
Chris Lattner5e693ed2009-07-28 03:13:23 +0000223const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
224 switch (Opcode) {
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000225 case MipsISD::JmpLink: return "MipsISD::JmpLink";
Akira Hatanaka91318df2012-10-19 20:59:39 +0000226 case MipsISD::TailCall: return "MipsISD::TailCall";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000227 case MipsISD::Hi: return "MipsISD::Hi";
228 case MipsISD::Lo: return "MipsISD::Lo";
229 case MipsISD::GPRel: return "MipsISD::GPRel";
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +0000230 case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000231 case MipsISD::Ret: return "MipsISD::Ret";
Akira Hatanakac0b02062013-01-30 00:26:49 +0000232 case MipsISD::EH_RETURN: return "MipsISD::EH_RETURN";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000233 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
234 case MipsISD::FPCmp: return "MipsISD::FPCmp";
235 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
236 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000237 case MipsISD::TruncIntFP: return "MipsISD::TruncIntFP";
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000238 case MipsISD::MFHI: return "MipsISD::MFHI";
239 case MipsISD::MFLO: return "MipsISD::MFLO";
240 case MipsISD::MTLOHI: return "MipsISD::MTLOHI";
Akira Hatanaka28721bd2013-03-30 01:14:04 +0000241 case MipsISD::Mult: return "MipsISD::Mult";
242 case MipsISD::Multu: return "MipsISD::Multu";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000243 case MipsISD::MAdd: return "MipsISD::MAdd";
244 case MipsISD::MAddu: return "MipsISD::MAddu";
245 case MipsISD::MSub: return "MipsISD::MSub";
246 case MipsISD::MSubu: return "MipsISD::MSubu";
247 case MipsISD::DivRem: return "MipsISD::DivRem";
248 case MipsISD::DivRemU: return "MipsISD::DivRemU";
Akira Hatanaka28721bd2013-03-30 01:14:04 +0000249 case MipsISD::DivRem16: return "MipsISD::DivRem16";
250 case MipsISD::DivRemU16: return "MipsISD::DivRemU16";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000251 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
252 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
Akira Hatanakafaa88c02011-12-12 22:38:19 +0000253 case MipsISD::Wrapper: return "MipsISD::Wrapper";
Akira Hatanakaa4c09bc2011-07-19 23:30:50 +0000254 case MipsISD::Sync: return "MipsISD::Sync";
Akira Hatanaka5360f882011-08-17 02:05:42 +0000255 case MipsISD::Ext: return "MipsISD::Ext";
256 case MipsISD::Ins: return "MipsISD::Ins";
Akira Hatanakab9ebf8d2012-06-02 00:03:12 +0000257 case MipsISD::LWL: return "MipsISD::LWL";
258 case MipsISD::LWR: return "MipsISD::LWR";
259 case MipsISD::SWL: return "MipsISD::SWL";
260 case MipsISD::SWR: return "MipsISD::SWR";
261 case MipsISD::LDL: return "MipsISD::LDL";
262 case MipsISD::LDR: return "MipsISD::LDR";
263 case MipsISD::SDL: return "MipsISD::SDL";
264 case MipsISD::SDR: return "MipsISD::SDR";
Akira Hatanaka233ac532012-09-21 23:52:47 +0000265 case MipsISD::EXTP: return "MipsISD::EXTP";
266 case MipsISD::EXTPDP: return "MipsISD::EXTPDP";
267 case MipsISD::EXTR_S_H: return "MipsISD::EXTR_S_H";
268 case MipsISD::EXTR_W: return "MipsISD::EXTR_W";
269 case MipsISD::EXTR_R_W: return "MipsISD::EXTR_R_W";
270 case MipsISD::EXTR_RS_W: return "MipsISD::EXTR_RS_W";
271 case MipsISD::SHILO: return "MipsISD::SHILO";
272 case MipsISD::MTHLIP: return "MipsISD::MTHLIP";
273 case MipsISD::MULT: return "MipsISD::MULT";
274 case MipsISD::MULTU: return "MipsISD::MULTU";
Jia Liu434874d2013-03-04 01:06:54 +0000275 case MipsISD::MADD_DSP: return "MipsISD::MADD_DSP";
Akira Hatanaka233ac532012-09-21 23:52:47 +0000276 case MipsISD::MADDU_DSP: return "MipsISD::MADDU_DSP";
277 case MipsISD::MSUB_DSP: return "MipsISD::MSUB_DSP";
278 case MipsISD::MSUBU_DSP: return "MipsISD::MSUBU_DSP";
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000279 case MipsISD::SHLL_DSP: return "MipsISD::SHLL_DSP";
280 case MipsISD::SHRA_DSP: return "MipsISD::SHRA_DSP";
281 case MipsISD::SHRL_DSP: return "MipsISD::SHRL_DSP";
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000282 case MipsISD::SETCC_DSP: return "MipsISD::SETCC_DSP";
283 case MipsISD::SELECT_CC_DSP: return "MipsISD::SELECT_CC_DSP";
Daniel Sandersce09d072013-08-28 12:14:50 +0000284 case MipsISD::VALL_ZERO: return "MipsISD::VALL_ZERO";
285 case MipsISD::VANY_ZERO: return "MipsISD::VANY_ZERO";
286 case MipsISD::VALL_NONZERO: return "MipsISD::VALL_NONZERO";
287 case MipsISD::VANY_NONZERO: return "MipsISD::VANY_NONZERO";
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000288 case MipsISD::VCEQ: return "MipsISD::VCEQ";
289 case MipsISD::VCLE_S: return "MipsISD::VCLE_S";
290 case MipsISD::VCLE_U: return "MipsISD::VCLE_U";
291 case MipsISD::VCLT_S: return "MipsISD::VCLT_S";
292 case MipsISD::VCLT_U: return "MipsISD::VCLT_U";
Daniel Sanders3ce56622013-09-24 12:18:31 +0000293 case MipsISD::VSMAX: return "MipsISD::VSMAX";
294 case MipsISD::VSMIN: return "MipsISD::VSMIN";
295 case MipsISD::VUMAX: return "MipsISD::VUMAX";
296 case MipsISD::VUMIN: return "MipsISD::VUMIN";
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000297 case MipsISD::VEXTRACT_SEXT_ELT: return "MipsISD::VEXTRACT_SEXT_ELT";
298 case MipsISD::VEXTRACT_ZEXT_ELT: return "MipsISD::VEXTRACT_ZEXT_ELT";
Daniel Sandersf7456c72013-09-23 13:22:24 +0000299 case MipsISD::VNOR: return "MipsISD::VNOR";
Daniel Sanderse5087042013-09-24 14:02:15 +0000300 case MipsISD::VSHF: return "MipsISD::VSHF";
Daniel Sanders26307182013-09-24 14:20:00 +0000301 case MipsISD::SHF: return "MipsISD::SHF";
Daniel Sanders2ed228b2013-09-24 14:36:12 +0000302 case MipsISD::ILVEV: return "MipsISD::ILVEV";
303 case MipsISD::ILVOD: return "MipsISD::ILVOD";
304 case MipsISD::ILVL: return "MipsISD::ILVL";
305 case MipsISD::ILVR: return "MipsISD::ILVR";
Daniel Sandersfae5f2a2013-09-24 14:53:25 +0000306 case MipsISD::PCKEV: return "MipsISD::PCKEV";
307 case MipsISD::PCKOD: return "MipsISD::PCKOD";
Daniel Sandersb50ccf82014-04-01 10:35:28 +0000308 case MipsISD::INSVE: return "MipsISD::INSVE";
Craig Topper062a2ba2014-04-25 05:30:21 +0000309 default: return nullptr;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000310 }
311}
312
Eric Christopherb1526602014-09-19 23:30:42 +0000313MipsTargetLowering::MipsTargetLowering(const MipsTargetMachine &TM,
Eric Christopher8924d272014-07-18 23:25:04 +0000314 const MipsSubtarget &STI)
315 : TargetLowering(TM, new MipsTargetObjectFile()), Subtarget(STI) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000316 // Mips does not have i1 type, so use i32 for
Wesley Peck527da1b2010-11-23 03:31:01 +0000317 // setcc operations results (slt, sgt, ...).
Duncan Sands8d6e2e12008-11-23 15:47:28 +0000318 setBooleanContents(ZeroOrOneBooleanContent);
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000319 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Daniel Sanderscbd44c52014-07-10 10:18:12 +0000320 // The cmp.cond.fmt instruction in MIPS32r6/MIPS64r6 uses 0 and -1 like MSA
321 // does. Integer booleans still use 0 and 1.
Eric Christopher1c29a652014-07-18 22:55:25 +0000322 if (Subtarget.hasMips32r6())
Daniel Sanderscbd44c52014-07-10 10:18:12 +0000323 setBooleanContents(ZeroOrOneBooleanContent,
324 ZeroOrNegativeOneBooleanContent);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000325
Wesley Peck527da1b2010-11-23 03:31:01 +0000326 // Load extented operations for i1 types must be promoted
Owen Anderson9f944592009-08-11 20:47:22 +0000327 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
328 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
329 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000330
Eli Friedman1fa07e12009-07-17 04:07:24 +0000331 // MIPS doesn't have extending float->double load/store
Owen Anderson9f944592009-08-11 20:47:22 +0000332 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
333 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman39d6faa2009-07-17 02:28:12 +0000334
Wesley Peck527da1b2010-11-23 03:31:01 +0000335 // Used by legalize types to correctly generate the setcc result.
336 // Without this, every float setcc comes with a AND/OR with the result,
337 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +0000338 // which is used implicitly by brcond and select operations.
Owen Anderson9f944592009-08-11 20:47:22 +0000339 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +0000340
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000341 // Mips Custom Operations
Akira Hatanaka0f693a82013-03-06 21:32:03 +0000342 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000343 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000344 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000345 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
346 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
347 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
348 setOperationAction(ISD::SELECT, MVT::f32, Custom);
349 setOperationAction(ISD::SELECT, MVT::f64, Custom);
350 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Akira Hatanaka24cf4e32012-07-11 19:32:27 +0000351 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
352 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Akira Hatanakab7f78592012-03-09 23:46:03 +0000353 setOperationAction(ISD::SETCC, MVT::f32, Custom);
354 setOperationAction(ISD::SETCC, MVT::f64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000355 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000356 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
357 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000358 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000359
Eric Christopher1c29a652014-07-18 22:55:25 +0000360 if (Subtarget.isGP64bit()) {
Akira Hatanakada00aa82012-03-10 00:03:50 +0000361 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
362 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
363 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
364 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
365 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
366 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Akira Hatanaka019e5922012-06-02 00:04:42 +0000367 setOperationAction(ISD::LOAD, MVT::i64, Custom);
368 setOperationAction(ISD::STORE, MVT::i64, Custom);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000369 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000370 }
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +0000371
Eric Christopher1c29a652014-07-18 22:55:25 +0000372 if (!Subtarget.isGP64bit()) {
Akira Hatanaka0a8ab712012-05-09 00:55:21 +0000373 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
374 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
375 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
376 }
377
Akira Hatanaka28e02ec2012-11-07 19:10:58 +0000378 setOperationAction(ISD::ADD, MVT::i32, Custom);
Eric Christopher1c29a652014-07-18 22:55:25 +0000379 if (Subtarget.isGP64bit())
Akira Hatanaka28e02ec2012-11-07 19:10:58 +0000380 setOperationAction(ISD::ADD, MVT::i64, Custom);
381
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000382 setOperationAction(ISD::SDIV, MVT::i32, Expand);
383 setOperationAction(ISD::SREM, MVT::i32, Expand);
384 setOperationAction(ISD::UDIV, MVT::i32, Expand);
385 setOperationAction(ISD::UREM, MVT::i32, Expand);
Akira Hatanakab1538f92011-10-03 21:06:13 +0000386 setOperationAction(ISD::SDIV, MVT::i64, Expand);
387 setOperationAction(ISD::SREM, MVT::i64, Expand);
388 setOperationAction(ISD::UDIV, MVT::i64, Expand);
389 setOperationAction(ISD::UREM, MVT::i64, Expand);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000390
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000391 // Operations not directly supported by Mips.
Tom Stellardb1588fc2013-03-08 15:36:57 +0000392 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
393 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
394 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
395 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
Tom Stellard3787b122014-06-10 16:01:29 +0000396 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
397 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000398 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Akira Hatanaka79aed152011-12-20 23:40:56 +0000399 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000400 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Akira Hatanaka79aed152011-12-20 23:40:56 +0000401 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000402 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Eric Christopher1c29a652014-07-18 22:55:25 +0000403 if (Subtarget.hasCnMips()) {
Kai Nacke93fe5e82014-03-20 11:51:58 +0000404 setOperationAction(ISD::CTPOP, MVT::i32, Legal);
405 setOperationAction(ISD::CTPOP, MVT::i64, Legal);
406 } else {
407 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
408 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
409 }
Owen Anderson9f944592009-08-11 20:47:22 +0000410 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
Akira Hatanaka410ce9c2011-12-21 00:14:05 +0000411 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000412 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
413 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
414 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
415 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000416 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Akira Hatanaka7ba8a8d2011-09-30 18:51:46 +0000417 setOperationAction(ISD::ROTL, MVT::i64, Expand);
Akira Hatanaka33a25af2012-07-31 20:54:48 +0000418 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
419 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Bruno Cardoso Lopesd47180e2010-12-09 17:32:30 +0000420
Eric Christopher1c29a652014-07-18 22:55:25 +0000421 if (!Subtarget.hasMips32r2())
Bruno Cardoso Lopesd47180e2010-12-09 17:32:30 +0000422 setOperationAction(ISD::ROTR, MVT::i32, Expand);
423
Eric Christopher1c29a652014-07-18 22:55:25 +0000424 if (!Subtarget.hasMips64r2())
Akira Hatanaka7ba8a8d2011-09-30 18:51:46 +0000425 setOperationAction(ISD::ROTR, MVT::i64, Expand);
426
Owen Anderson9f944592009-08-11 20:47:22 +0000427 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes22b69db2011-03-04 18:54:14 +0000428 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000429 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes22b69db2011-03-04 18:54:14 +0000430 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000431 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
432 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000433 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
434 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanakadfb8cda2011-05-23 22:23:58 +0000435 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000436 setOperationAction(ISD::FLOG, MVT::f32, Expand);
437 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
438 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
439 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000440 setOperationAction(ISD::FMA, MVT::f32, Expand);
441 setOperationAction(ISD::FMA, MVT::f64, Expand);
Akira Hatanaka0603ad82012-03-29 18:43:11 +0000442 setOperationAction(ISD::FREM, MVT::f32, Expand);
443 setOperationAction(ISD::FREM, MVT::f64, Expand);
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000444
Akira Hatanakac0b02062013-01-30 00:26:49 +0000445 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
446
Daniel Sanders2b553d42014-08-01 09:17:39 +0000447 setOperationAction(ISD::VASTART, MVT::Other, Custom);
448 setOperationAction(ISD::VAARG, MVT::Other, Custom);
Bruno Cardoso Lopes048ffab2011-03-09 19:22:22 +0000449 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
450 setOperationAction(ISD::VAEND, MVT::Other, Expand);
451
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000452 // Use the default for now
Owen Anderson9f944592009-08-11 20:47:22 +0000453 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
454 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman26a48482011-07-27 22:21:52 +0000455
Jia Liuf54f60f2012-02-28 07:46:26 +0000456 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
457 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
458 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
459 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
Eli Friedman7dfa7912011-08-29 18:23:02 +0000460
Eli Friedman30a49e92011-08-03 21:06:02 +0000461 setInsertFencesForAtomic(true);
462
Eric Christopher1c29a652014-07-18 22:55:25 +0000463 if (!Subtarget.hasMips32r2()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000464 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
465 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000466 }
467
Daniel Sanders070fd1c2014-05-12 12:41:59 +0000468 // MIPS16 lacks MIPS32's clz and clo instructions.
Eric Christopher1c29a652014-07-18 22:55:25 +0000469 if (!Subtarget.hasMips32() || Subtarget.inMips16Mode())
Owen Anderson9f944592009-08-11 20:47:22 +0000470 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Eric Christopher1c29a652014-07-18 22:55:25 +0000471 if (!Subtarget.hasMips64())
Akira Hatanaka1d8efab2011-12-21 00:20:27 +0000472 setOperationAction(ISD::CTLZ, MVT::i64, Expand);
Bruno Cardoso Lopes93da7e62008-08-08 06:16:31 +0000473
Eric Christopher1c29a652014-07-18 22:55:25 +0000474 if (!Subtarget.hasMips32r2())
Owen Anderson9f944592009-08-11 20:47:22 +0000475 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Eric Christopher1c29a652014-07-18 22:55:25 +0000476 if (!Subtarget.hasMips64r2())
Akira Hatanaka4706ac92011-12-20 23:56:43 +0000477 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000478
Eric Christopher1c29a652014-07-18 22:55:25 +0000479 if (Subtarget.isGP64bit()) {
Akira Hatanaka019e5922012-06-02 00:04:42 +0000480 setLoadExtAction(ISD::SEXTLOAD, MVT::i32, Custom);
481 setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, Custom);
482 setLoadExtAction(ISD::EXTLOAD, MVT::i32, Custom);
483 setTruncStoreAction(MVT::i64, MVT::i32, Custom);
484 }
485
Akira Hatanakaa3d9ab92013-07-26 20:58:55 +0000486 setOperationAction(ISD::TRAP, MVT::Other, Legal);
487
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000488 setTargetDAGCombine(ISD::SDIVREM);
489 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka5e152182012-03-08 03:26:37 +0000490 setTargetDAGCombine(ISD::SELECT);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000491 setTargetDAGCombine(ISD::AND);
492 setTargetDAGCombine(ISD::OR);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000493 setTargetDAGCombine(ISD::ADD);
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000494
Eric Christopher1c29a652014-07-18 22:55:25 +0000495 setMinFunctionAlignment(Subtarget.isGP64bit() ? 3 : 2);
Eli Friedman2518f832011-05-06 20:34:06 +0000496
Daniel Sanders2b553d42014-08-01 09:17:39 +0000497 // The arguments on the stack are defined in terms of 4-byte slots on O32
498 // and 8-byte slots on N32/N64.
499 setMinStackArgumentAlignment(
500 (Subtarget.isABI_N32() || Subtarget.isABI_N64()) ? 8 : 4);
501
Eric Christopher1c29a652014-07-18 22:55:25 +0000502 setStackPointerRegisterToSaveRestore(Subtarget.isABI_N64() ? Mips::SP_64
503 : Mips::SP);
Akira Hatanakaaa560002011-05-26 18:59:03 +0000504
Eric Christopher1c29a652014-07-18 22:55:25 +0000505 setExceptionPointerRegister(Subtarget.isABI_N64() ? Mips::A0_64 : Mips::A0);
506 setExceptionSelectorRegister(Subtarget.isABI_N64() ? Mips::A1_64 : Mips::A1);
Akira Hatanaka1daf8c22012-06-13 19:33:32 +0000507
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000508 MaxStoresPerMemcpy = 16;
Zoran Jovanovicff9d5f32013-12-19 16:12:56 +0000509
Eric Christopher1c29a652014-07-18 22:55:25 +0000510 isMicroMips = Subtarget.inMicroMipsMode();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000511}
512
Eric Christopherb1526602014-09-19 23:30:42 +0000513const MipsTargetLowering *MipsTargetLowering::create(const MipsTargetMachine &TM,
Eric Christopher8924d272014-07-18 23:25:04 +0000514 const MipsSubtarget &STI) {
515 if (STI.inMips16Mode())
516 return llvm::createMips16TargetLowering(TM, STI);
Jia Liuf54f60f2012-02-28 07:46:26 +0000517
Eric Christopher8924d272014-07-18 23:25:04 +0000518 return llvm::createMipsSETargetLowering(TM, STI);
Akira Hatanaka2fcc1cf2011-08-12 21:30:06 +0000519}
520
Reed Kotler720c5ca2014-04-17 22:15:34 +0000521// Create a fast isel object.
522FastISel *
523MipsTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
524 const TargetLibraryInfo *libInfo) const {
525 if (!EnableMipsFastISel)
526 return TargetLowering::createFastISel(funcInfo, libInfo);
527 return Mips::createFastISel(funcInfo, libInfo);
528}
529
Matt Arsenault758659232013-05-18 00:21:46 +0000530EVT MipsTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Akira Hatanakab13b3332013-01-04 20:06:01 +0000531 if (!VT.isVector())
532 return MVT::i32;
533 return VT.changeVectorElementTypeToInteger();
Scott Michela6729e82008-03-10 15:42:14 +0000534}
535
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000536static SDValue performDivRemCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000537 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000538 const MipsSubtarget &Subtarget) {
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000539 if (DCI.isBeforeLegalizeOps())
540 return SDValue();
541
Akira Hatanakab1538f92011-10-03 21:06:13 +0000542 EVT Ty = N->getValueType(0);
Akira Hatanaka8002a3f2013-08-14 00:47:08 +0000543 unsigned LO = (Ty == MVT::i32) ? Mips::LO0 : Mips::LO0_64;
544 unsigned HI = (Ty == MVT::i32) ? Mips::HI0 : Mips::HI0_64;
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000545 unsigned Opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem16 :
546 MipsISD::DivRemU16;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000547 SDLoc DL(N);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000548
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000549 SDValue DivRem = DAG.getNode(Opc, DL, MVT::Glue,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000550 N->getOperand(0), N->getOperand(1));
551 SDValue InChain = DAG.getEntryNode();
552 SDValue InGlue = DivRem;
553
554 // insert MFLO
555 if (N->hasAnyUseOfValue(0)) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000556 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, DL, LO, Ty,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000557 InGlue);
558 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
559 InChain = CopyFromLo.getValue(1);
560 InGlue = CopyFromLo.getValue(2);
561 }
562
563 // insert MFHI
564 if (N->hasAnyUseOfValue(1)) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000565 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, DL,
Akira Hatanakab1538f92011-10-03 21:06:13 +0000566 HI, Ty, InGlue);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000567 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
568 }
569
570 return SDValue();
571}
572
Akira Hatanaka89af5892013-04-18 01:00:46 +0000573static Mips::CondCode condCodeToFCC(ISD::CondCode CC) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000574 switch (CC) {
575 default: llvm_unreachable("Unknown fp condition code!");
576 case ISD::SETEQ:
577 case ISD::SETOEQ: return Mips::FCOND_OEQ;
578 case ISD::SETUNE: return Mips::FCOND_UNE;
579 case ISD::SETLT:
580 case ISD::SETOLT: return Mips::FCOND_OLT;
581 case ISD::SETGT:
582 case ISD::SETOGT: return Mips::FCOND_OGT;
583 case ISD::SETLE:
584 case ISD::SETOLE: return Mips::FCOND_OLE;
585 case ISD::SETGE:
586 case ISD::SETOGE: return Mips::FCOND_OGE;
587 case ISD::SETULT: return Mips::FCOND_ULT;
588 case ISD::SETULE: return Mips::FCOND_ULE;
589 case ISD::SETUGT: return Mips::FCOND_UGT;
590 case ISD::SETUGE: return Mips::FCOND_UGE;
591 case ISD::SETUO: return Mips::FCOND_UN;
592 case ISD::SETO: return Mips::FCOND_OR;
593 case ISD::SETNE:
594 case ISD::SETONE: return Mips::FCOND_ONE;
595 case ISD::SETUEQ: return Mips::FCOND_UEQ;
596 }
597}
598
599
Akira Hatanakaf0ea5002013-03-30 01:16:38 +0000600/// This function returns true if the floating point conditional branches and
601/// conditional moves which use condition code CC should be inverted.
602static bool invertFPCondCodeUser(Mips::CondCode CC) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000603 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
604 return false;
605
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000606 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
607 "Illegal Condition Code");
Akira Hatanakaa5352702011-03-31 18:26:17 +0000608
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000609 return true;
Akira Hatanakaa5352702011-03-31 18:26:17 +0000610}
611
612// Creates and returns an FPCmp node from a setcc node.
613// Returns Op if setcc is not a floating point comparison.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000614static SDValue createFPCmp(SelectionDAG &DAG, const SDValue &Op) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000615 // must be a SETCC node
616 if (Op.getOpcode() != ISD::SETCC)
617 return Op;
618
619 SDValue LHS = Op.getOperand(0);
620
621 if (!LHS.getValueType().isFloatingPoint())
622 return Op;
623
624 SDValue RHS = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000625 SDLoc DL(Op);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000626
Akira Hatanakaaef55c82011-04-15 21:00:26 +0000627 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
628 // node if necessary.
Akira Hatanakaa5352702011-03-31 18:26:17 +0000629 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
630
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000631 return DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS,
Akira Hatanaka89af5892013-04-18 01:00:46 +0000632 DAG.getConstant(condCodeToFCC(CC), MVT::i32));
Akira Hatanakaa5352702011-03-31 18:26:17 +0000633}
634
635// Creates and returns a CMovFPT/F node.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000636static SDValue createCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000637 SDValue False, SDLoc DL) {
Akira Hatanakaf0ea5002013-03-30 01:16:38 +0000638 ConstantSDNode *CC = cast<ConstantSDNode>(Cond.getOperand(2));
639 bool invert = invertFPCondCodeUser((Mips::CondCode)CC->getSExtValue());
Akira Hatanaka8bce21c2013-07-26 20:51:20 +0000640 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000641
642 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
Akira Hatanaka8bce21c2013-07-26 20:51:20 +0000643 True.getValueType(), True, FCC0, False, Cond);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000644}
645
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000646static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000647 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000648 const MipsSubtarget &Subtarget) {
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000649 if (DCI.isBeforeLegalizeOps())
650 return SDValue();
651
652 SDValue SetCC = N->getOperand(0);
653
654 if ((SetCC.getOpcode() != ISD::SETCC) ||
655 !SetCC.getOperand(0).getValueType().isInteger())
656 return SDValue();
657
658 SDValue False = N->getOperand(2);
659 EVT FalseTy = False.getValueType();
660
661 if (!FalseTy.isInteger())
662 return SDValue();
663
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000664 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(False);
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000665
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000666 // If the RHS (False) is 0, we swap the order of the operands
667 // of ISD::SELECT (obviously also inverting the condition) so that we can
668 // take advantage of conditional moves using the $0 register.
669 // Example:
670 // return (a != 0) ? x : 0;
671 // load $reg, x
672 // movz $reg, $0, a
673 if (!FalseC)
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000674 return SDValue();
675
Andrew Trickef9de2a2013-05-25 02:42:55 +0000676 const SDLoc DL(N);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000677
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000678 if (!FalseC->getZExtValue()) {
679 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
680 SDValue True = N->getOperand(1);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000681
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000682 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
683 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
684
685 return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True);
686 }
687
Matheus Almeidaa6beac12013-12-05 12:07:05 +0000688 // If both operands are integer constants there's a possibility that we
689 // can do some interesting optimizations.
690 SDValue True = N->getOperand(1);
691 ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(True);
692
693 if (!TrueC || !True.getValueType().isInteger())
694 return SDValue();
695
696 // We'll also ignore MVT::i64 operands as this optimizations proves
697 // to be ineffective because of the required sign extensions as the result
698 // of a SETCC operator is always MVT::i32 for non-vector types.
699 if (True.getValueType() == MVT::i64)
700 return SDValue();
701
702 int64_t Diff = TrueC->getSExtValue() - FalseC->getSExtValue();
703
704 // 1) (a < x) ? y : y-1
705 // slti $reg1, a, x
706 // addiu $reg2, $reg1, y-1
707 if (Diff == 1)
708 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, False);
709
710 // 2) (a < x) ? y-1 : y
711 // slti $reg1, a, x
712 // xor $reg1, $reg1, 1
713 // addiu $reg2, $reg1, y-1
714 if (Diff == -1) {
715 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
716 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
717 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
718 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, True);
719 }
720
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000721 // Couldn't optimize.
722 return SDValue();
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000723}
724
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000725static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000726 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000727 const MipsSubtarget &Subtarget) {
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000728 // Pattern match EXT.
729 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
730 // => ext $dst, $src, size, pos
Eric Christopher1c29a652014-07-18 22:55:25 +0000731 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasExtractInsert())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000732 return SDValue();
733
734 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000735 unsigned ShiftRightOpc = ShiftRight.getOpcode();
736
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000737 // Op's first operand must be a shift right.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000738 if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL)
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000739 return SDValue();
740
741 // The second operand of the shift must be an immediate.
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000742 ConstantSDNode *CN;
743 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
744 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000745
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000746 uint64_t Pos = CN->getZExtValue();
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000747 uint64_t SMPos, SMSize;
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000748
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000749 // Op's second operand must be a shifted mask.
750 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000751 !isShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000752 return SDValue();
753
754 // Return if the shifted mask does not start at bit 0 or the sum of its size
755 // and Pos exceeds the word's size.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000756 EVT ValTy = N->getValueType(0);
757 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000758 return SDValue();
759
Andrew Trickef9de2a2013-05-25 02:42:55 +0000760 return DAG.getNode(MipsISD::Ext, SDLoc(N), ValTy,
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000761 ShiftRight.getOperand(0), DAG.getConstant(Pos, MVT::i32),
Akira Hatanakaeea541c2011-08-17 22:59:46 +0000762 DAG.getConstant(SMSize, MVT::i32));
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000763}
Jia Liuf54f60f2012-02-28 07:46:26 +0000764
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000765static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000766 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000767 const MipsSubtarget &Subtarget) {
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000768 // Pattern match INS.
769 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
Jia Liuf54f60f2012-02-28 07:46:26 +0000770 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000771 // => ins $dst, $src, size, pos, $src1
Eric Christopher1c29a652014-07-18 22:55:25 +0000772 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasExtractInsert())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000773 return SDValue();
774
775 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
776 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
777 ConstantSDNode *CN;
778
779 // See if Op's first operand matches (and $src1 , mask0).
780 if (And0.getOpcode() != ISD::AND)
781 return SDValue();
782
783 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000784 !isShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000785 return SDValue();
786
787 // See if Op's second operand matches (and (shl $src, pos), mask1).
788 if (And1.getOpcode() != ISD::AND)
789 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000790
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000791 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000792 !isShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000793 return SDValue();
794
795 // The shift masks must have the same position and size.
796 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
797 return SDValue();
798
799 SDValue Shl = And1.getOperand(0);
800 if (Shl.getOpcode() != ISD::SHL)
801 return SDValue();
802
803 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
804 return SDValue();
805
806 unsigned Shamt = CN->getZExtValue();
807
808 // Return if the shift amount and the first bit position of mask are not the
Jia Liuf54f60f2012-02-28 07:46:26 +0000809 // same.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000810 EVT ValTy = N->getValueType(0);
811 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000812 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000813
Andrew Trickef9de2a2013-05-25 02:42:55 +0000814 return DAG.getNode(MipsISD::Ins, SDLoc(N), ValTy, Shl.getOperand(0),
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000815 DAG.getConstant(SMPos0, MVT::i32),
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000816 DAG.getConstant(SMSize0, MVT::i32), And0.getOperand(0));
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000817}
Jia Liuf54f60f2012-02-28 07:46:26 +0000818
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000819static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000820 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000821 const MipsSubtarget &Subtarget) {
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000822 // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt))
823
824 if (DCI.isBeforeLegalizeOps())
825 return SDValue();
826
827 SDValue Add = N->getOperand(1);
828
829 if (Add.getOpcode() != ISD::ADD)
830 return SDValue();
831
832 SDValue Lo = Add.getOperand(1);
833
834 if ((Lo.getOpcode() != MipsISD::Lo) ||
835 (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable))
836 return SDValue();
837
838 EVT ValTy = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000839 SDLoc DL(N);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000840
841 SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, N->getOperand(0),
842 Add.getOperand(0));
843 return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo);
844}
845
Bruno Cardoso Lopes61a61e92011-02-10 18:05:10 +0000846SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000847 const {
848 SelectionDAG &DAG = DCI.DAG;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000849 unsigned Opc = N->getOpcode();
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000850
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000851 switch (Opc) {
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000852 default: break;
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000853 case ISD::SDIVREM:
854 case ISD::UDIVREM:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000855 return performDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000856 case ISD::SELECT:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000857 return performSELECTCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000858 case ISD::AND:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000859 return performANDCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000860 case ISD::OR:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000861 return performORCombine(N, DAG, DCI, Subtarget);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000862 case ISD::ADD:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000863 return performADDCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000864 }
865
866 return SDValue();
867}
868
Akira Hatanakafabb8cf2012-09-21 23:58:31 +0000869void
870MipsTargetLowering::LowerOperationWrapper(SDNode *N,
871 SmallVectorImpl<SDValue> &Results,
872 SelectionDAG &DAG) const {
873 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
874
875 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
876 Results.push_back(Res.getValue(I));
877}
878
879void
880MipsTargetLowering::ReplaceNodeResults(SDNode *N,
881 SmallVectorImpl<SDValue> &Results,
882 SelectionDAG &DAG) const {
Akira Hatanaka9da442f2013-04-30 21:17:07 +0000883 return LowerOperationWrapper(N, Results, DAG);
Akira Hatanakafabb8cf2012-09-21 23:58:31 +0000884}
885
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000886SDValue MipsTargetLowering::
Dan Gohman21cea8a2010-04-17 15:26:15 +0000887LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000888{
Wesley Peck527da1b2010-11-23 03:31:01 +0000889 switch (Op.getOpcode())
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000890 {
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000891 case ISD::BR_JT: return lowerBR_JT(Op, DAG);
892 case ISD::BRCOND: return lowerBRCOND(Op, DAG);
893 case ISD::ConstantPool: return lowerConstantPool(Op, DAG);
894 case ISD::GlobalAddress: return lowerGlobalAddress(Op, DAG);
895 case ISD::BlockAddress: return lowerBlockAddress(Op, DAG);
896 case ISD::GlobalTLSAddress: return lowerGlobalTLSAddress(Op, DAG);
897 case ISD::JumpTable: return lowerJumpTable(Op, DAG);
898 case ISD::SELECT: return lowerSELECT(Op, DAG);
899 case ISD::SELECT_CC: return lowerSELECT_CC(Op, DAG);
900 case ISD::SETCC: return lowerSETCC(Op, DAG);
901 case ISD::VASTART: return lowerVASTART(Op, DAG);
Daniel Sanders2b553d42014-08-01 09:17:39 +0000902 case ISD::VAARG: return lowerVAARG(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000903 case ISD::FCOPYSIGN: return lowerFCOPYSIGN(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000904 case ISD::FRAMEADDR: return lowerFRAMEADDR(Op, DAG);
905 case ISD::RETURNADDR: return lowerRETURNADDR(Op, DAG);
906 case ISD::EH_RETURN: return lowerEH_RETURN(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000907 case ISD::ATOMIC_FENCE: return lowerATOMIC_FENCE(Op, DAG);
908 case ISD::SHL_PARTS: return lowerShiftLeftParts(Op, DAG);
909 case ISD::SRA_PARTS: return lowerShiftRightParts(Op, DAG, true);
910 case ISD::SRL_PARTS: return lowerShiftRightParts(Op, DAG, false);
911 case ISD::LOAD: return lowerLOAD(Op, DAG);
912 case ISD::STORE: return lowerSTORE(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000913 case ISD::ADD: return lowerADD(Op, DAG);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000914 case ISD::FP_TO_SINT: return lowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000915 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000916 return SDValue();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000917}
918
Akira Hatanakae2489122011-04-15 21:51:11 +0000919//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000920// Lower helper functions
Akira Hatanakae2489122011-04-15 21:51:11 +0000921//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000922
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000923// addLiveIn - This helper function adds the specified physical register to the
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000924// MachineFunction as a live in value. It also creates a corresponding
925// virtual register for it.
926static unsigned
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000927addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000928{
Chris Lattnera10fff52007-12-31 04:13:23 +0000929 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
930 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000931 return VReg;
932}
933
Daniel Sanders308181e2014-06-12 10:44:10 +0000934static MachineBasicBlock *insertDivByZeroTrap(MachineInstr *MI,
935 MachineBasicBlock &MBB,
936 const TargetInstrInfo &TII,
937 bool Is64Bit) {
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000938 if (NoZeroDivCheck)
939 return &MBB;
940
941 // Insert instruction "teq $divisor_reg, $zero, 7".
942 MachineBasicBlock::iterator I(MI);
943 MachineInstrBuilder MIB;
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000944 MachineOperand &Divisor = MI->getOperand(2);
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000945 MIB = BuildMI(MBB, std::next(I), MI->getDebugLoc(), TII.get(Mips::TEQ))
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000946 .addReg(Divisor.getReg(), getKillRegState(Divisor.isKill()))
947 .addReg(Mips::ZERO).addImm(7);
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000948
949 // Use the 32-bit sub-register if this is a 64-bit division.
950 if (Is64Bit)
951 MIB->getOperand(0).setSubReg(Mips::sub_32);
952
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000953 // Clear Divisor's kill flag.
954 Divisor.setIsKill(false);
Daniel Sanders308181e2014-06-12 10:44:10 +0000955
956 // We would normally delete the original instruction here but in this case
957 // we only needed to inject an additional instruction rather than replace it.
958
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000959 return &MBB;
960}
961
Akira Hatanakae4bd0542012-09-27 02:15:57 +0000962MachineBasicBlock *
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +0000963MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +0000964 MachineBasicBlock *BB) const {
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +0000965 switch (MI->getOpcode()) {
Reed Kotler97ba5f22013-02-21 04:22:38 +0000966 default:
967 llvm_unreachable("Unexpected instr type to insert");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000968 case Mips::ATOMIC_LOAD_ADD_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000969 return emitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000970 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000971 return emitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000972 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000973 return emitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000974 case Mips::ATOMIC_LOAD_ADD_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000975 return emitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000976
977 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000978 return emitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000979 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000980 return emitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000981 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000982 return emitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000983 case Mips::ATOMIC_LOAD_AND_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000984 return emitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000985
986 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000987 return emitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000988 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000989 return emitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000990 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000991 return emitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000992 case Mips::ATOMIC_LOAD_OR_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000993 return emitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000994
995 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000996 return emitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000997 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000998 return emitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000999 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001000 return emitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001001 case Mips::ATOMIC_LOAD_XOR_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001002 return emitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001003
1004 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001005 return emitAtomicBinaryPartword(MI, BB, 1, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001006 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001007 return emitAtomicBinaryPartword(MI, BB, 2, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001008 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001009 return emitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001010 case Mips::ATOMIC_LOAD_NAND_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001011 return emitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001012
1013 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001014 return emitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001015 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001016 return emitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001017 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001018 return emitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001019 case Mips::ATOMIC_LOAD_SUB_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001020 return emitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001021
1022 case Mips::ATOMIC_SWAP_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001023 return emitAtomicBinaryPartword(MI, BB, 1, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001024 case Mips::ATOMIC_SWAP_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001025 return emitAtomicBinaryPartword(MI, BB, 2, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001026 case Mips::ATOMIC_SWAP_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001027 return emitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001028 case Mips::ATOMIC_SWAP_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001029 return emitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001030
1031 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001032 return emitAtomicCmpSwapPartword(MI, BB, 1);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001033 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001034 return emitAtomicCmpSwapPartword(MI, BB, 2);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001035 case Mips::ATOMIC_CMP_SWAP_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001036 return emitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001037 case Mips::ATOMIC_CMP_SWAP_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001038 return emitAtomicCmpSwap(MI, BB, 8);
Akira Hatanaka1cb02422013-05-20 18:07:43 +00001039 case Mips::PseudoSDIV:
1040 case Mips::PseudoUDIV:
Daniel Sanders308181e2014-06-12 10:44:10 +00001041 case Mips::DIV:
1042 case Mips::DIVU:
1043 case Mips::MOD:
1044 case Mips::MODU:
Eric Christopherd9134482014-08-04 21:25:23 +00001045 return insertDivByZeroTrap(
1046 MI, *BB, *getTargetMachine().getSubtargetImpl()->getInstrInfo(), false);
Akira Hatanaka1cb02422013-05-20 18:07:43 +00001047 case Mips::PseudoDSDIV:
1048 case Mips::PseudoDUDIV:
Daniel Sanders308181e2014-06-12 10:44:10 +00001049 case Mips::DDIV:
1050 case Mips::DDIVU:
1051 case Mips::DMOD:
1052 case Mips::DMODU:
Eric Christopherd9134482014-08-04 21:25:23 +00001053 return insertDivByZeroTrap(
1054 MI, *BB, *getTargetMachine().getSubtargetImpl()->getInstrInfo(), true);
Daniel Sanders0fa60412014-06-12 13:39:06 +00001055 case Mips::SEL_D:
1056 return emitSEL_D(MI, BB);
Akira Hatanakaa5352702011-03-31 18:26:17 +00001057 }
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001058}
1059
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001060// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
1061// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
1062MachineBasicBlock *
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001063MipsTargetLowering::emitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher0713a9d2011-06-08 23:55:35 +00001064 unsigned Size, unsigned BinOpcode,
Akira Hatanaka15506782011-06-07 18:58:42 +00001065 bool Nand) const {
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001066 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001067
1068 MachineFunction *MF = BB->getParent();
1069 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001070 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Eric Christopherd9134482014-08-04 21:25:23 +00001071 const TargetInstrInfo *TII =
1072 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001073 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001074 unsigned LL, SC, AND, NOR, ZERO, BEQ;
1075
1076 if (Size == 4) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001077 if (isMicroMips) {
1078 LL = Mips::LL_MM;
1079 SC = Mips::SC_MM;
1080 } else {
Daniel Sandersbdcfab12014-07-24 09:47:14 +00001081 LL = Subtarget.hasMips32r6() ? Mips::LL_R6 : Mips::LL;
1082 SC = Subtarget.hasMips32r6() ? Mips::SC_R6 : Mips::SC;
Daniel Sanders6a803f62014-06-16 13:13:03 +00001083 }
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001084 AND = Mips::AND;
1085 NOR = Mips::NOR;
1086 ZERO = Mips::ZERO;
1087 BEQ = Mips::BEQ;
Daniel Sanders6a803f62014-06-16 13:13:03 +00001088 } else {
Daniel Sandersbdcfab12014-07-24 09:47:14 +00001089 LL = Subtarget.hasMips64r6() ? Mips::LLD_R6 : Mips::LLD;
1090 SC = Subtarget.hasMips64r6() ? Mips::SCD_R6 : Mips::SCD;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001091 AND = Mips::AND64;
1092 NOR = Mips::NOR64;
1093 ZERO = Mips::ZERO_64;
1094 BEQ = Mips::BEQ64;
1095 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001096
Akira Hatanaka0e019592011-07-19 20:11:17 +00001097 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001098 unsigned Ptr = MI->getOperand(1).getReg();
1099 unsigned Incr = MI->getOperand(2).getReg();
1100
Akira Hatanaka0e019592011-07-19 20:11:17 +00001101 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1102 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1103 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001104
1105 // insert new blocks after the current block
1106 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1107 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1108 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1109 MachineFunction::iterator It = BB;
1110 ++It;
1111 MF->insert(It, loopMBB);
1112 MF->insert(It, exitMBB);
1113
1114 // Transfer the remainder of BB and its successor edges to exitMBB.
1115 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001116 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001117 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1118
1119 // thisMBB:
1120 // ...
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001121 // fallthrough --> loopMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001122 BB->addSuccessor(loopMBB);
Akira Hatanaka08636b42011-07-19 17:09:53 +00001123 loopMBB->addSuccessor(loopMBB);
1124 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001125
1126 // loopMBB:
1127 // ll oldval, 0(ptr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001128 // <binop> storeval, oldval, incr
1129 // sc success, storeval, 0(ptr)
1130 // beq success, $0, loopMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001131 BB = loopMBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001132 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001133 if (Nand) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001134 // and andres, oldval, incr
1135 // nor storeval, $0, andres
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001136 BuildMI(BB, DL, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
1137 BuildMI(BB, DL, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001138 } else if (BinOpcode) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001139 // <binop> storeval, oldval, incr
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001140 BuildMI(BB, DL, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001141 } else {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001142 StoreVal = Incr;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001143 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001144 BuildMI(BB, DL, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
1145 BuildMI(BB, DL, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001146
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001147 MI->eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001148
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001149 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001150}
1151
Daniel Sanders6a803f62014-06-16 13:13:03 +00001152MachineBasicBlock *MipsTargetLowering::emitSignExtendToI32InReg(
1153 MachineInstr *MI, MachineBasicBlock *BB, unsigned Size, unsigned DstReg,
1154 unsigned SrcReg) const {
Eric Christopherd9134482014-08-04 21:25:23 +00001155 const TargetInstrInfo *TII =
1156 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Daniel Sanders6a803f62014-06-16 13:13:03 +00001157 DebugLoc DL = MI->getDebugLoc();
1158
Eric Christopher1c29a652014-07-18 22:55:25 +00001159 if (Subtarget.hasMips32r2() && Size == 1) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001160 BuildMI(BB, DL, TII->get(Mips::SEB), DstReg).addReg(SrcReg);
1161 return BB;
1162 }
1163
Eric Christopher1c29a652014-07-18 22:55:25 +00001164 if (Subtarget.hasMips32r2() && Size == 2) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001165 BuildMI(BB, DL, TII->get(Mips::SEH), DstReg).addReg(SrcReg);
1166 return BB;
1167 }
1168
1169 MachineFunction *MF = BB->getParent();
1170 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1171 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1172 unsigned ScrReg = RegInfo.createVirtualRegister(RC);
1173
1174 assert(Size < 32);
1175 int64_t ShiftImm = 32 - (Size * 8);
1176
1177 BuildMI(BB, DL, TII->get(Mips::SLL), ScrReg).addReg(SrcReg).addImm(ShiftImm);
1178 BuildMI(BB, DL, TII->get(Mips::SRA), DstReg).addReg(ScrReg).addImm(ShiftImm);
1179
1180 return BB;
1181}
1182
1183MachineBasicBlock *MipsTargetLowering::emitAtomicBinaryPartword(
1184 MachineInstr *MI, MachineBasicBlock *BB, unsigned Size, unsigned BinOpcode,
1185 bool Nand) const {
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001186 assert((Size == 1 || Size == 2) &&
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001187 "Unsupported size for EmitAtomicBinaryPartial.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001188
1189 MachineFunction *MF = BB->getParent();
1190 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1191 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
Eric Christopherd9134482014-08-04 21:25:23 +00001192 const TargetInstrInfo *TII =
1193 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001194 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001195
1196 unsigned Dest = MI->getOperand(0).getReg();
1197 unsigned Ptr = MI->getOperand(1).getReg();
1198 unsigned Incr = MI->getOperand(2).getReg();
1199
Akira Hatanaka0e019592011-07-19 20:11:17 +00001200 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1201 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001202 unsigned Mask = RegInfo.createVirtualRegister(RC);
1203 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001204 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1205 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001206 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001207 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1208 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1209 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1210 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1211 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka9663dd32011-07-19 20:56:53 +00001212 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001213 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1214 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1215 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001216 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001217
1218 // insert new blocks after the current block
1219 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1220 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001221 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001222 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1223 MachineFunction::iterator It = BB;
1224 ++It;
1225 MF->insert(It, loopMBB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001226 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001227 MF->insert(It, exitMBB);
1228
1229 // Transfer the remainder of BB and its successor edges to exitMBB.
1230 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001231 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001232 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1233
Akira Hatanaka08636b42011-07-19 17:09:53 +00001234 BB->addSuccessor(loopMBB);
1235 loopMBB->addSuccessor(loopMBB);
1236 loopMBB->addSuccessor(sinkMBB);
1237 sinkMBB->addSuccessor(exitMBB);
1238
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001239 // thisMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001240 // addiu masklsb2,$0,-4 # 0xfffffffc
1241 // and alignedaddr,ptr,masklsb2
1242 // andi ptrlsb2,ptr,3
1243 // sll shiftamt,ptrlsb2,3
1244 // ori maskupper,$0,255 # 0xff
1245 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001246 // nor mask2,$0,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001247 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001248
1249 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001250 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001251 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001252 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001253 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001254 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Eric Christopher1c29a652014-07-18 22:55:25 +00001255 if (Subtarget.isLittle()) {
Akira Hatanaka2bf97332013-05-31 03:25:44 +00001256 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1257 } else {
1258 unsigned Off = RegInfo.createVirtualRegister(RC);
1259 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1260 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1261 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1262 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001263 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001264 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001265 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001266 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001267 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001268 BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(Incr).addReg(ShiftAmt);
Bruno Cardoso Lopesf771a0f2011-05-31 20:25:26 +00001269
Akira Hatanaka27292632011-07-18 18:52:12 +00001270 // atomic.load.binop
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001271 // loopMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001272 // ll oldval,0(alignedaddr)
1273 // binop binopres,oldval,incr2
1274 // and newval,binopres,mask
1275 // and maskedoldval0,oldval,mask2
1276 // or storeval,maskedoldval0,newval
1277 // sc success,storeval,0(alignedaddr)
1278 // beq success,$0,loopMBB
1279
Akira Hatanaka27292632011-07-18 18:52:12 +00001280 // atomic.swap
1281 // loopMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001282 // ll oldval,0(alignedaddr)
Akira Hatanakae4503582011-07-19 18:14:26 +00001283 // and newval,incr2,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001284 // and maskedoldval0,oldval,mask2
1285 // or storeval,maskedoldval0,newval
1286 // sc success,storeval,0(alignedaddr)
1287 // beq success,$0,loopMBB
Akira Hatanaka27292632011-07-18 18:52:12 +00001288
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001289 BB = loopMBB;
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001290 BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001291 if (Nand) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001292 // and andres, oldval, incr2
1293 // nor binopres, $0, andres
1294 // and newval, binopres, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001295 BuildMI(BB, DL, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1296 BuildMI(BB, DL, TII->get(Mips::NOR), BinOpRes)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001297 .addReg(Mips::ZERO).addReg(AndRes);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001298 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001299 } else if (BinOpcode) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001300 // <binop> binopres, oldval, incr2
1301 // and newval, binopres, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001302 BuildMI(BB, DL, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1303 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001304 } else { // atomic.swap
Akira Hatanaka0e019592011-07-19 20:11:17 +00001305 // and newval, incr2, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001306 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanakae4503582011-07-19 18:14:26 +00001307 }
Jia Liuf54f60f2012-02-28 07:46:26 +00001308
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001309 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001310 .addReg(OldVal).addReg(Mask2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001311 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka9663dd32011-07-19 20:56:53 +00001312 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001313 BuildMI(BB, DL, TII->get(Mips::SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001314 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001315 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001316 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001317
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001318 // sinkMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001319 // and maskedoldval1,oldval,mask
1320 // srl srlres,maskedoldval1,shiftamt
Daniel Sanders6a803f62014-06-16 13:13:03 +00001321 // sign_extend dest,srlres
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001322 BB = sinkMBB;
Akira Hatanakae97bd812011-07-19 03:14:58 +00001323
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001324 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001325 .addReg(OldVal).addReg(Mask);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001326 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001327 .addReg(MaskedOldVal1).addReg(ShiftAmt);
Daniel Sanders6a803f62014-06-16 13:13:03 +00001328 BB = emitSignExtendToI32InReg(MI, BB, Size, Dest, SrlRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001329
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001330 MI->eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001331
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001332 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001333}
1334
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001335MachineBasicBlock * MipsTargetLowering::emitAtomicCmpSwap(MachineInstr *MI,
1336 MachineBasicBlock *BB,
1337 unsigned Size) const {
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001338 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001339
1340 MachineFunction *MF = BB->getParent();
1341 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001342 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Eric Christopherd9134482014-08-04 21:25:23 +00001343 const TargetInstrInfo *TII =
1344 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001345 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001346 unsigned LL, SC, ZERO, BNE, BEQ;
1347
1348 if (Size == 4) {
Zoran Jovanovicff9d5f32013-12-19 16:12:56 +00001349 LL = isMicroMips ? Mips::LL_MM : Mips::LL;
1350 SC = isMicroMips ? Mips::SC_MM : Mips::SC;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001351 ZERO = Mips::ZERO;
1352 BNE = Mips::BNE;
1353 BEQ = Mips::BEQ;
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001354 } else {
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001355 LL = Mips::LLD;
1356 SC = Mips::SCD;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001357 ZERO = Mips::ZERO_64;
1358 BNE = Mips::BNE64;
1359 BEQ = Mips::BEQ64;
1360 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001361
1362 unsigned Dest = MI->getOperand(0).getReg();
1363 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka0e019592011-07-19 20:11:17 +00001364 unsigned OldVal = MI->getOperand(2).getReg();
1365 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001366
Akira Hatanaka0e019592011-07-19 20:11:17 +00001367 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001368
1369 // insert new blocks after the current block
1370 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1371 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1372 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1373 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1374 MachineFunction::iterator It = BB;
1375 ++It;
1376 MF->insert(It, loop1MBB);
1377 MF->insert(It, loop2MBB);
1378 MF->insert(It, exitMBB);
1379
1380 // Transfer the remainder of BB and its successor edges to exitMBB.
1381 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001382 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001383 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1384
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001385 // thisMBB:
1386 // ...
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001387 // fallthrough --> loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001388 BB->addSuccessor(loop1MBB);
Akira Hatanaka08636b42011-07-19 17:09:53 +00001389 loop1MBB->addSuccessor(exitMBB);
1390 loop1MBB->addSuccessor(loop2MBB);
1391 loop2MBB->addSuccessor(loop1MBB);
1392 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001393
1394 // loop1MBB:
1395 // ll dest, 0(ptr)
1396 // bne dest, oldval, exitMBB
1397 BB = loop1MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001398 BuildMI(BB, DL, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1399 BuildMI(BB, DL, TII->get(BNE))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001400 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001401
1402 // loop2MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001403 // sc success, newval, 0(ptr)
1404 // beq success, $0, loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001405 BB = loop2MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001406 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001407 .addReg(NewVal).addReg(Ptr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001408 BuildMI(BB, DL, TII->get(BEQ))
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001409 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001410
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001411 MI->eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001412
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001413 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001414}
1415
1416MachineBasicBlock *
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001417MipsTargetLowering::emitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka15506782011-06-07 18:58:42 +00001418 MachineBasicBlock *BB,
1419 unsigned Size) const {
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001420 assert((Size == 1 || Size == 2) &&
1421 "Unsupported size for EmitAtomicCmpSwapPartial.");
1422
1423 MachineFunction *MF = BB->getParent();
1424 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1425 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
Eric Christopherd9134482014-08-04 21:25:23 +00001426 const TargetInstrInfo *TII =
1427 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001428 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001429
1430 unsigned Dest = MI->getOperand(0).getReg();
1431 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka0e019592011-07-19 20:11:17 +00001432 unsigned CmpVal = MI->getOperand(2).getReg();
1433 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001434
Akira Hatanaka0e019592011-07-19 20:11:17 +00001435 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1436 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001437 unsigned Mask = RegInfo.createVirtualRegister(RC);
1438 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001439 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1440 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1441 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1442 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1443 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1444 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1445 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1446 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1447 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1448 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1449 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1450 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001451 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001452
1453 // insert new blocks after the current block
1454 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1455 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1456 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001457 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001458 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1459 MachineFunction::iterator It = BB;
1460 ++It;
1461 MF->insert(It, loop1MBB);
1462 MF->insert(It, loop2MBB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001463 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001464 MF->insert(It, exitMBB);
1465
1466 // Transfer the remainder of BB and its successor edges to exitMBB.
1467 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001468 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001469 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1470
Akira Hatanaka08636b42011-07-19 17:09:53 +00001471 BB->addSuccessor(loop1MBB);
1472 loop1MBB->addSuccessor(sinkMBB);
1473 loop1MBB->addSuccessor(loop2MBB);
1474 loop2MBB->addSuccessor(loop1MBB);
1475 loop2MBB->addSuccessor(sinkMBB);
1476 sinkMBB->addSuccessor(exitMBB);
1477
Akira Hatanakae4503582011-07-19 18:14:26 +00001478 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001479 // thisMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001480 // addiu masklsb2,$0,-4 # 0xfffffffc
1481 // and alignedaddr,ptr,masklsb2
1482 // andi ptrlsb2,ptr,3
1483 // sll shiftamt,ptrlsb2,3
1484 // ori maskupper,$0,255 # 0xff
1485 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001486 // nor mask2,$0,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001487 // andi maskedcmpval,cmpval,255
1488 // sll shiftedcmpval,maskedcmpval,shiftamt
1489 // andi maskednewval,newval,255
1490 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001491 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001492 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001493 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001494 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001495 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001496 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Eric Christopher1c29a652014-07-18 22:55:25 +00001497 if (Subtarget.isLittle()) {
Akira Hatanaka2bf97332013-05-31 03:25:44 +00001498 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1499 } else {
1500 unsigned Off = RegInfo.createVirtualRegister(RC);
1501 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1502 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1503 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1504 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001505 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001506 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001507 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001508 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001509 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1510 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001511 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001512 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001513 .addReg(MaskedCmpVal).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001514 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001515 .addReg(NewVal).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001516 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001517 .addReg(MaskedNewVal).addReg(ShiftAmt);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001518
1519 // loop1MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001520 // ll oldval,0(alginedaddr)
1521 // and maskedoldval0,oldval,mask
1522 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001523 BB = loop1MBB;
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001524 BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001525 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001526 .addReg(OldVal).addReg(Mask);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001527 BuildMI(BB, DL, TII->get(Mips::BNE))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001528 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001529
1530 // loop2MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001531 // and maskedoldval1,oldval,mask2
1532 // or storeval,maskedoldval1,shiftednewval
1533 // sc success,storeval,0(alignedaddr)
1534 // beq success,$0,loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001535 BB = loop2MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001536 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001537 .addReg(OldVal).addReg(Mask2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001538 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001539 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001540 BuildMI(BB, DL, TII->get(Mips::SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001541 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001542 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001543 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001544
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001545 // sinkMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001546 // srl srlres,maskedoldval0,shiftamt
Daniel Sanders6a803f62014-06-16 13:13:03 +00001547 // sign_extend dest,srlres
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001548 BB = sinkMBB;
Akira Hatanakae97bd812011-07-19 03:14:58 +00001549
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001550 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001551 .addReg(MaskedOldVal0).addReg(ShiftAmt);
Daniel Sanders6a803f62014-06-16 13:13:03 +00001552 BB = emitSignExtendToI32InReg(MI, BB, Size, Dest, SrlRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001553
1554 MI->eraseFromParent(); // The instruction is gone now.
1555
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001556 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001557}
1558
Daniel Sanders0fa60412014-06-12 13:39:06 +00001559MachineBasicBlock *MipsTargetLowering::emitSEL_D(MachineInstr *MI,
1560 MachineBasicBlock *BB) const {
1561 MachineFunction *MF = BB->getParent();
Eric Christopherd9134482014-08-04 21:25:23 +00001562 const TargetRegisterInfo *TRI =
1563 getTargetMachine().getSubtargetImpl()->getRegisterInfo();
1564 const TargetInstrInfo *TII =
1565 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Daniel Sanders0fa60412014-06-12 13:39:06 +00001566 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1567 DebugLoc DL = MI->getDebugLoc();
1568 MachineBasicBlock::iterator II(MI);
1569
1570 unsigned Fc = MI->getOperand(1).getReg();
1571 const auto &FGR64RegClass = TRI->getRegClass(Mips::FGR64RegClassID);
1572
1573 unsigned Fc2 = RegInfo.createVirtualRegister(FGR64RegClass);
1574
1575 BuildMI(*BB, II, DL, TII->get(Mips::SUBREG_TO_REG), Fc2)
1576 .addImm(0)
1577 .addReg(Fc)
1578 .addImm(Mips::sub_lo);
1579
1580 // We don't erase the original instruction, we just replace the condition
1581 // register with the 64-bit super-register.
1582 MI->getOperand(1).setReg(Fc2);
1583
1584 return BB;
1585}
1586
Akira Hatanakae2489122011-04-15 21:51:11 +00001587//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001588// Misc Lower Operation implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00001589//===----------------------------------------------------------------------===//
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001590SDValue MipsTargetLowering::lowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001591 SDValue Chain = Op.getOperand(0);
1592 SDValue Table = Op.getOperand(1);
1593 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001594 SDLoc DL(Op);
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001595 EVT PTy = getPointerTy();
1596 unsigned EntrySize =
1597 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(*getDataLayout());
1598
1599 Index = DAG.getNode(ISD::MUL, DL, PTy, Index,
1600 DAG.getConstant(EntrySize, PTy));
1601 SDValue Addr = DAG.getNode(ISD::ADD, DL, PTy, Index, Table);
1602
1603 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
1604 Addr = DAG.getExtLoad(ISD::SEXTLOAD, DL, PTy, Chain, Addr,
1605 MachinePointerInfo::getJumpTable(), MemVT, false, false,
Louis Gerbarg67474e32014-07-31 21:45:05 +00001606 false, 0);
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001607 Chain = Addr.getValue(1);
1608
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001609 if ((getTargetMachine().getRelocationModel() == Reloc::PIC_) ||
Eric Christopher1c29a652014-07-18 22:55:25 +00001610 Subtarget.isABI_N64()) {
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001611 // For PIC, the sequence is:
1612 // BRIND(load(Jumptable + index) + RelocBase)
1613 // RelocBase can be JumpTable, GOT or some sort of global base.
1614 Addr = DAG.getNode(ISD::ADD, DL, PTy, Addr,
1615 getPICJumpTableRelocBase(Table, DAG));
1616 }
1617
1618 return DAG.getNode(ISD::BRIND, DL, MVT::Other, Chain, Addr);
1619}
1620
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001621SDValue MipsTargetLowering::lowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Wesley Peck527da1b2010-11-23 03:31:01 +00001622 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001623 // the block to branch to if the condition is true.
1624 SDValue Chain = Op.getOperand(0);
1625 SDValue Dest = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001626 SDLoc DL(Op);
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001627
Eric Christopher1c29a652014-07-18 22:55:25 +00001628 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001629 SDValue CondRes = createFPCmp(DAG, Op.getOperand(1));
Akira Hatanakaa5352702011-03-31 18:26:17 +00001630
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001631 // Return if flag is not set by a floating point comparison.
Akira Hatanakaa5352702011-03-31 18:26:17 +00001632 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopesa9504222008-07-30 17:06:13 +00001633 return Op;
Wesley Peck527da1b2010-11-23 03:31:01 +00001634
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +00001635 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmaneffb8942008-09-12 16:56:44 +00001636 Mips::CondCode CC =
1637 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Akira Hatanakaf0ea5002013-03-30 01:16:38 +00001638 unsigned Opc = invertFPCondCodeUser(CC) ? Mips::BRANCH_F : Mips::BRANCH_T;
1639 SDValue BrCode = DAG.getConstant(Opc, MVT::i32);
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +00001640 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001641 return DAG.getNode(MipsISD::FPBrcond, DL, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +00001642 FCC0, Dest, CondRes);
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001643}
1644
1645SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001646lowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001647{
Eric Christopher1c29a652014-07-18 22:55:25 +00001648 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001649 SDValue Cond = createFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001650
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001651 // Return if flag is not set by a floating point comparison.
Akira Hatanakaa5352702011-03-31 18:26:17 +00001652 if (Cond.getOpcode() != MipsISD::FPCmp)
1653 return Op;
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +00001654
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001655 return createCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
Andrew Trickef9de2a2013-05-25 02:42:55 +00001656 SDLoc(Op));
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001657}
1658
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001659SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001660lowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001661{
Andrew Trickef9de2a2013-05-25 02:42:55 +00001662 SDLoc DL(Op);
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001663 EVT Ty = Op.getOperand(0).getValueType();
Matt Arsenault758659232013-05-18 00:21:46 +00001664 SDValue Cond = DAG.getNode(ISD::SETCC, DL,
1665 getSetCCResultType(*DAG.getContext(), Ty),
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001666 Op.getOperand(0), Op.getOperand(1),
1667 Op.getOperand(4));
1668
1669 return DAG.getNode(ISD::SELECT, DL, Op.getValueType(), Cond, Op.getOperand(2),
1670 Op.getOperand(3));
1671}
1672
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001673SDValue MipsTargetLowering::lowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00001674 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001675 SDValue Cond = createFPCmp(DAG, Op);
Akira Hatanakab7f78592012-03-09 23:46:03 +00001676
1677 assert(Cond.getOpcode() == MipsISD::FPCmp &&
1678 "Floating point operand expected.");
1679
1680 SDValue True = DAG.getConstant(1, MVT::i32);
1681 SDValue False = DAG.getConstant(0, MVT::i32);
1682
Andrew Trickef9de2a2013-05-25 02:42:55 +00001683 return createCMovFP(DAG, Cond, True, False, SDLoc(Op));
Akira Hatanakab7f78592012-03-09 23:46:03 +00001684}
1685
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001686SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001687 SelectionDAG &DAG) const {
Dale Johannesen400dc2e2009-02-06 21:50:26 +00001688 // FIXME there isn't actually debug info here
Andrew Trickef9de2a2013-05-25 02:42:55 +00001689 SDLoc DL(Op);
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001690 EVT Ty = Op.getValueType();
1691 GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
1692 const GlobalValue *GV = N->getGlobal();
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001693
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001694 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ &&
Eric Christopher1c29a652014-07-18 22:55:25 +00001695 !Subtarget.isABI_N64()) {
Akira Hatanaka92a96e12012-09-12 23:27:55 +00001696 const MipsTargetObjectFile &TLOF =
1697 (const MipsTargetObjectFile&)getObjFileLowering();
Wesley Peck527da1b2010-11-23 03:31:01 +00001698
Chris Lattner58e8be82009-08-13 05:41:27 +00001699 // %gp_rel relocation
Wesley Peck527da1b2010-11-23 03:31:01 +00001700 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001701 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, 0,
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +00001702 MipsII::MO_GPREL);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001703 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, DL,
Craig Topper48d114b2014-04-26 18:35:24 +00001704 DAG.getVTList(MVT::i32), GA);
Akira Hatanakaad495022012-08-22 03:18:13 +00001705 SDValue GPReg = DAG.getRegister(Mips::GP, MVT::i32);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001706 return DAG.getNode(ISD::ADD, DL, MVT::i32, GPReg, GPRelNode);
Chris Lattner58e8be82009-08-13 05:41:27 +00001707 }
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001708
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001709 // %hi/%lo relocation
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001710 return getAddrNonPIC(N, Ty, DAG);
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001711 }
1712
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001713 if (GV->hasInternalLinkage() || (GV->hasLocalLinkage() && !isa<Function>(GV)))
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001714 return getAddrLocal(N, Ty, DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +00001715 Subtarget.isABI_N32() || Subtarget.isABI_N64());
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001716
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00001717 if (LargeGOT)
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001718 return getAddrGlobalLargeGOT(N, Ty, DAG, MipsII::MO_GOT_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00001719 MipsII::MO_GOT_LO16, DAG.getEntryNode(),
1720 MachinePointerInfo::getGOT());
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00001721
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001722 return getAddrGlobal(N, Ty, DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +00001723 (Subtarget.isABI_N32() || Subtarget.isABI_N64())
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001724 ? MipsII::MO_GOT_DISP
1725 : MipsII::MO_GOT16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00001726 DAG.getEntryNode(), MachinePointerInfo::getGOT());
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001727}
1728
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001729SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op,
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +00001730 SelectionDAG &DAG) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001731 BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op);
1732 EVT Ty = Op.getValueType();
Akira Hatanaka30f97cf2013-09-25 00:30:25 +00001733
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001734 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ &&
Eric Christopher1c29a652014-07-18 22:55:25 +00001735 !Subtarget.isABI_N64())
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001736 return getAddrNonPIC(N, Ty, DAG);
1737
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001738 return getAddrLocal(N, Ty, DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +00001739 Subtarget.isABI_N32() || Subtarget.isABI_N64());
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +00001740}
1741
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001742SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001743lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001744{
Akira Hatanakabff84e12011-12-14 18:26:41 +00001745 // If the relocation model is PIC, use the General Dynamic TLS Model or
1746 // Local Dynamic TLS model, otherwise use the Initial Exec or
1747 // Local Exec TLS Model.
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001748
1749 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001750 SDLoc DL(GA);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001751 const GlobalValue *GV = GA->getGlobal();
1752 EVT PtrVT = getPointerTy();
1753
Hans Wennborgaea41202012-05-04 09:40:39 +00001754 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
1755
1756 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Hans Wennborg245917b2012-06-04 14:02:08 +00001757 // General Dynamic and Local Dynamic TLS Model.
1758 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
1759 : MipsII::MO_TLSGD;
1760
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001761 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag);
1762 SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT,
1763 getGlobalReg(DAG, PtrVT), TGA);
Akira Hatanakaf10ee842011-12-08 21:05:38 +00001764 unsigned PtrSize = PtrVT.getSizeInBits();
1765 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1766
Benjamin Kramer64ba50a2011-12-11 12:21:34 +00001767 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001768
1769 ArgListTy Args;
1770 ArgListEntry Entry;
1771 Entry.Node = Argument;
Akira Hatanakadee6c822011-12-08 20:34:32 +00001772 Entry.Ty = PtrTy;
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001773 Args.push_back(Entry);
Jia Liuf54f60f2012-02-28 07:46:26 +00001774
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00001775 TargetLowering::CallLoweringInfo CLI(DAG);
1776 CLI.setDebugLoc(DL).setChain(DAG.getEntryNode())
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00001777 .setCallee(CallingConv::C, PtrTy, TlsGetAddr, std::move(Args), 0);
Justin Holewinskiaa583972012-05-25 16:35:28 +00001778 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001779
Akira Hatanakabff84e12011-12-14 18:26:41 +00001780 SDValue Ret = CallResult.first;
1781
Hans Wennborgaea41202012-05-04 09:40:39 +00001782 if (model != TLSModel::LocalDynamic)
Akira Hatanakabff84e12011-12-14 18:26:41 +00001783 return Ret;
1784
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001785 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanakabff84e12011-12-14 18:26:41 +00001786 MipsII::MO_DTPREL_HI);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001787 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1788 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanakabff84e12011-12-14 18:26:41 +00001789 MipsII::MO_DTPREL_LO);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001790 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1791 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret);
1792 return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001793 }
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001794
1795 SDValue Offset;
Hans Wennborgaea41202012-05-04 09:40:39 +00001796 if (model == TLSModel::InitialExec) {
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001797 // Initial Exec TLS Model
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001798 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001799 MipsII::MO_GOTTPREL);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001800 TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT),
Akira Hatanakab049aef2012-02-24 22:34:47 +00001801 TGA);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001802 Offset = DAG.getLoad(PtrVT, DL,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001803 DAG.getEntryNode(), TGA, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001804 false, false, false, 0);
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001805 } else {
1806 // Local Exec TLS Model
Hans Wennborgaea41202012-05-04 09:40:39 +00001807 assert(model == TLSModel::LocalExec);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001808 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001809 MipsII::MO_TPREL_HI);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001810 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001811 MipsII::MO_TPREL_LO);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001812 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1813 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1814 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001815 }
1816
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001817 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
1818 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001819}
1820
1821SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001822lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +00001823{
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001824 JumpTableSDNode *N = cast<JumpTableSDNode>(Op);
1825 EVT Ty = Op.getValueType();
Akira Hatanaka30f97cf2013-09-25 00:30:25 +00001826
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001827 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ &&
Eric Christopher1c29a652014-07-18 22:55:25 +00001828 !Subtarget.isABI_N64())
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001829 return getAddrNonPIC(N, Ty, DAG);
1830
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001831 return getAddrLocal(N, Ty, DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +00001832 Subtarget.isABI_N32() || Subtarget.isABI_N64());
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +00001833}
1834
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001835SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001836lowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +00001837{
Bruno Cardoso Lopesfdb4cec2008-07-23 16:01:50 +00001838 // gp_rel relocation
Wesley Peck527da1b2010-11-23 03:31:01 +00001839 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001840 // but the asm printer currently doesn't support this feature without
Wesley Peck527da1b2010-11-23 03:31:01 +00001841 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopes98bda582008-07-28 19:26:25 +00001842 // stuff below.
Eli Friedman57c11da2009-08-03 02:22:28 +00001843 //if (IsInSmallSection(C->getType())) {
Owen Anderson9f944592009-08-11 20:47:22 +00001844 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1845 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peck527da1b2010-11-23 03:31:01 +00001846 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001847 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
1848 EVT Ty = Op.getValueType();
Bruno Cardoso Lopes2db07582009-11-25 12:17:58 +00001849
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001850 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ &&
Eric Christopher1c29a652014-07-18 22:55:25 +00001851 !Subtarget.isABI_N64())
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001852 return getAddrNonPIC(N, Ty, DAG);
Bruno Cardoso Lopesfdb4cec2008-07-23 16:01:50 +00001853
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001854 return getAddrLocal(N, Ty, DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +00001855 Subtarget.isABI_N32() || Subtarget.isABI_N64());
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +00001856}
1857
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001858SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman31ae5862010-04-17 14:41:14 +00001859 MachineFunction &MF = DAG.getMachineFunction();
1860 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1861
Andrew Trickef9de2a2013-05-25 02:42:55 +00001862 SDLoc DL(Op);
Dan Gohman31ae5862010-04-17 14:41:14 +00001863 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1864 getPointerTy());
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +00001865
1866 // vastart just stores the address of the VarArgsFrameIndex slot into the
1867 // memory location argument.
1868 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001869 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00001870 MachinePointerInfo(SV), false, false, 0);
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +00001871}
Jia Liuf54f60f2012-02-28 07:46:26 +00001872
Daniel Sanders2b553d42014-08-01 09:17:39 +00001873SDValue MipsTargetLowering::lowerVAARG(SDValue Op, SelectionDAG &DAG) const {
1874 SDNode *Node = Op.getNode();
1875 EVT VT = Node->getValueType(0);
1876 SDValue Chain = Node->getOperand(0);
1877 SDValue VAListPtr = Node->getOperand(1);
1878 unsigned Align = Node->getConstantOperandVal(3);
1879 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
1880 SDLoc DL(Node);
1881 unsigned ArgSlotSizeInBytes =
1882 (Subtarget.isABI_N32() || Subtarget.isABI_N64()) ? 8 : 4;
1883
1884 SDValue VAListLoad = DAG.getLoad(getPointerTy(), DL, Chain, VAListPtr,
1885 MachinePointerInfo(SV), false, false, false,
1886 0);
1887 SDValue VAList = VAListLoad;
1888
1889 // Re-align the pointer if necessary.
1890 // It should only ever be necessary for 64-bit types on O32 since the minimum
1891 // argument alignment is the same as the maximum type alignment for N32/N64.
1892 //
1893 // FIXME: We currently align too often. The code generator doesn't notice
1894 // when the pointer is still aligned from the last va_arg (or pair of
1895 // va_args for the i64 on O32 case).
1896 if (Align > getMinStackArgumentAlignment()) {
1897 assert(((Align & (Align-1)) == 0) && "Expected Align to be a power of 2");
1898
1899 VAList = DAG.getNode(ISD::ADD, DL, VAList.getValueType(), VAList,
1900 DAG.getConstant(Align - 1,
1901 VAList.getValueType()));
1902
1903 VAList = DAG.getNode(ISD::AND, DL, VAList.getValueType(), VAList,
1904 DAG.getConstant(-(int64_t)Align,
1905 VAList.getValueType()));
1906 }
1907
1908 // Increment the pointer, VAList, to the next vaarg.
1909 unsigned ArgSizeInBytes = getDataLayout()->getTypeAllocSize(VT.getTypeForEVT(*DAG.getContext()));
1910 SDValue Tmp3 = DAG.getNode(ISD::ADD, DL, VAList.getValueType(), VAList,
1911 DAG.getConstant(RoundUpToAlignment(ArgSizeInBytes, ArgSlotSizeInBytes),
1912 VAList.getValueType()));
1913 // Store the incremented VAList to the legalized pointer
1914 Chain = DAG.getStore(VAListLoad.getValue(1), DL, Tmp3, VAListPtr,
1915 MachinePointerInfo(SV), false, false, 0);
1916
1917 // In big-endian mode we must adjust the pointer when the load size is smaller
1918 // than the argument slot size. We must also reduce the known alignment to
1919 // match. For example in the N64 ABI, we must add 4 bytes to the offset to get
1920 // the correct half of the slot, and reduce the alignment from 8 (slot
1921 // alignment) down to 4 (type alignment).
1922 if (!Subtarget.isLittle() && ArgSizeInBytes < ArgSlotSizeInBytes) {
1923 unsigned Adjustment = ArgSlotSizeInBytes - ArgSizeInBytes;
1924 VAList = DAG.getNode(ISD::ADD, DL, VAListPtr.getValueType(), VAList,
1925 DAG.getIntPtrConstant(Adjustment));
1926 }
1927 // Load the actual argument out of the pointer VAList
1928 return DAG.getLoad(VT, DL, Chain, VAList, MachinePointerInfo(), false, false,
1929 false, 0);
1930}
1931
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001932static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG,
1933 bool HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001934 EVT TyX = Op.getOperand(0).getValueType();
1935 EVT TyY = Op.getOperand(1).getValueType();
1936 SDValue Const1 = DAG.getConstant(1, MVT::i32);
1937 SDValue Const31 = DAG.getConstant(31, MVT::i32);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001938 SDLoc DL(Op);
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001939 SDValue Res;
1940
1941 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1942 // to i32.
1943 SDValue X = (TyX == MVT::f32) ?
1944 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1945 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1946 Const1);
1947 SDValue Y = (TyY == MVT::f32) ?
1948 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
1949 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
1950 Const1);
1951
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001952 if (HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001953 // ext E, Y, 31, 1 ; extract bit31 of Y
1954 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
1955 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
1956 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
1957 } else {
1958 // sll SllX, X, 1
1959 // srl SrlX, SllX, 1
1960 // srl SrlY, Y, 31
1961 // sll SllY, SrlX, 31
1962 // or Or, SrlX, SllY
1963 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1964 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1965 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
1966 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
1967 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
1968 }
1969
1970 if (TyX == MVT::f32)
1971 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
1972
1973 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1974 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1975 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001976}
1977
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001978static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG,
1979 bool HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001980 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
1981 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
1982 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
1983 SDValue Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001984 SDLoc DL(Op);
Eric Christopher0713a9d2011-06-08 23:55:35 +00001985
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001986 // Bitcast to integer nodes.
1987 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
1988 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001989
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001990 if (HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001991 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
1992 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
1993 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
1994 DAG.getConstant(WidthY - 1, MVT::i32), Const1);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001995
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001996 if (WidthX > WidthY)
1997 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
1998 else if (WidthY > WidthX)
1999 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002000
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00002001 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
2002 DAG.getConstant(WidthX - 1, MVT::i32), Const1, X);
2003 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
2004 }
2005
2006 // (d)sll SllX, X, 1
2007 // (d)srl SrlX, SllX, 1
2008 // (d)srl SrlY, Y, width(Y)-1
2009 // (d)sll SllY, SrlX, width(Y)-1
2010 // or Or, SrlX, SllY
2011 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
2012 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
2013 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
2014 DAG.getConstant(WidthY - 1, MVT::i32));
2015
2016 if (WidthX > WidthY)
2017 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
2018 else if (WidthY > WidthX)
2019 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
2020
2021 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
2022 DAG.getConstant(WidthX - 1, MVT::i32));
2023 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
2024 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002025}
2026
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00002027SDValue
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002028MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00002029 if (Subtarget.isGP64bit())
2030 return lowerFCOPYSIGN64(Op, DAG, Subtarget.hasExtractInsert());
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002031
Eric Christopher1c29a652014-07-18 22:55:25 +00002032 return lowerFCOPYSIGN32(Op, DAG, Subtarget.hasExtractInsert());
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00002033}
2034
Akira Hatanaka66277522011-06-02 00:24:44 +00002035SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002036lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes5444a7b2011-06-16 00:40:02 +00002037 // check the depth
2038 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka15506782011-06-07 18:58:42 +00002039 "Frame address can only be determined for current frame.");
Akira Hatanaka66277522011-06-02 00:24:44 +00002040
2041 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2042 MFI->setFrameAddressIsTaken(true);
2043 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002044 SDLoc DL(Op);
Eric Christopherbf33a3c2014-07-02 23:18:40 +00002045 SDValue FrameAddr =
2046 DAG.getCopyFromReg(DAG.getEntryNode(), DL,
Eric Christopher1c29a652014-07-18 22:55:25 +00002047 Subtarget.isABI_N64() ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka66277522011-06-02 00:24:44 +00002048 return FrameAddr;
2049}
2050
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002051SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op,
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002052 SelectionDAG &DAG) const {
Bill Wendling908bf812014-01-06 00:43:20 +00002053 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00002054 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00002055
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002056 // check the depth
2057 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
2058 "Return address can be determined only for current frame.");
2059
2060 MachineFunction &MF = DAG.getMachineFunction();
2061 MachineFrameInfo *MFI = MF.getFrameInfo();
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00002062 MVT VT = Op.getSimpleValueType();
Eric Christopher1c29a652014-07-18 22:55:25 +00002063 unsigned RA = Subtarget.isABI_N64() ? Mips::RA_64 : Mips::RA;
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002064 MFI->setReturnAddressIsTaken(true);
2065
2066 // Return RA, which contains the return address. Mark it an implicit live-in.
2067 unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00002068 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT);
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00002069}
2070
Akira Hatanakac0b02062013-01-30 00:26:49 +00002071// An EH_RETURN is the result of lowering llvm.eh.return which in turn is
2072// generated from __builtin_eh_return (offset, handler)
2073// The effect of this is to adjust the stack pointer by "offset"
2074// and then branch to "handler".
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002075SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Akira Hatanakac0b02062013-01-30 00:26:49 +00002076 const {
2077 MachineFunction &MF = DAG.getMachineFunction();
2078 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2079
2080 MipsFI->setCallsEhReturn();
2081 SDValue Chain = Op.getOperand(0);
2082 SDValue Offset = Op.getOperand(1);
2083 SDValue Handler = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002084 SDLoc DL(Op);
Eric Christopher1c29a652014-07-18 22:55:25 +00002085 EVT Ty = Subtarget.isABI_N64() ? MVT::i64 : MVT::i32;
Akira Hatanakac0b02062013-01-30 00:26:49 +00002086
2087 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
2088 // EH_RETURN nodes, so that instructions are emitted back-to-back.
Eric Christopher1c29a652014-07-18 22:55:25 +00002089 unsigned OffsetReg = Subtarget.isABI_N64() ? Mips::V1_64 : Mips::V1;
2090 unsigned AddrReg = Subtarget.isABI_N64() ? Mips::V0_64 : Mips::V0;
Akira Hatanakac0b02062013-01-30 00:26:49 +00002091 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
2092 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
2093 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
2094 DAG.getRegister(OffsetReg, Ty),
2095 DAG.getRegister(AddrReg, getPointerTy()),
2096 Chain.getValue(1));
2097}
2098
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002099SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
Akira Hatanaka5fd22482012-06-14 21:10:56 +00002100 SelectionDAG &DAG) const {
Eli Friedman26a48482011-07-27 22:21:52 +00002101 // FIXME: Need pseudo-fence for 'singlethread' fences
2102 // FIXME: Set SType for weaker fences where supported/appropriate.
2103 unsigned SType = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002104 SDLoc DL(Op);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002105 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
Eli Friedman26a48482011-07-27 22:21:52 +00002106 DAG.getConstant(SType, MVT::i32));
2107}
2108
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002109SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
Akira Hatanaka5fd22482012-06-14 21:10:56 +00002110 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002111 SDLoc DL(Op);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002112 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2113 SDValue Shamt = Op.getOperand(2);
2114
2115 // if shamt < 32:
2116 // lo = (shl lo, shamt)
2117 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
2118 // else:
2119 // lo = 0
2120 // hi = (shl lo, shamt[4:0])
2121 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
2122 DAG.getConstant(-1, MVT::i32));
2123 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo,
2124 DAG.getConstant(1, MVT::i32));
2125 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, ShiftRight1Lo,
2126 Not);
2127 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi, Shamt);
2128 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
2129 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, MVT::i32, Lo, Shamt);
2130 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
2131 DAG.getConstant(0x20, MVT::i32));
Akira Hatanaka5fd22482012-06-14 21:10:56 +00002132 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
2133 DAG.getConstant(0, MVT::i32), ShiftLeftLo);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002134 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftLeftLo, Or);
2135
2136 SDValue Ops[2] = {Lo, Hi};
Craig Topper64941d92014-04-27 19:20:57 +00002137 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002138}
2139
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002140SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002141 bool IsSRA) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002142 SDLoc DL(Op);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002143 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2144 SDValue Shamt = Op.getOperand(2);
2145
2146 // if shamt < 32:
2147 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
2148 // if isSRA:
2149 // hi = (sra hi, shamt)
2150 // else:
2151 // hi = (srl hi, shamt)
2152 // else:
2153 // if isSRA:
2154 // lo = (sra hi, shamt[4:0])
2155 // hi = (sra hi, 31)
2156 // else:
2157 // lo = (srl hi, shamt[4:0])
2158 // hi = 0
2159 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
2160 DAG.getConstant(-1, MVT::i32));
2161 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi,
2162 DAG.getConstant(1, MVT::i32));
2163 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, ShiftLeft1Hi, Not);
2164 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo, Shamt);
2165 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
2166 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL, DL, MVT::i32,
2167 Hi, Shamt);
2168 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
2169 DAG.getConstant(0x20, MVT::i32));
2170 SDValue Shift31 = DAG.getNode(ISD::SRA, DL, MVT::i32, Hi,
2171 DAG.getConstant(31, MVT::i32));
2172 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftRightHi, Or);
2173 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
2174 IsSRA ? Shift31 : DAG.getConstant(0, MVT::i32),
2175 ShiftRightHi);
2176
2177 SDValue Ops[2] = {Lo, Hi};
Craig Topper64941d92014-04-27 19:20:57 +00002178 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002179}
2180
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002181static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002182 SDValue Chain, SDValue Src, unsigned Offset) {
Akira Hatanaka95866182012-06-13 19:06:08 +00002183 SDValue Ptr = LD->getBasePtr();
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002184 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
Akira Hatanaka95866182012-06-13 19:06:08 +00002185 EVT BasePtrVT = Ptr.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002186 SDLoc DL(LD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002187 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
2188
2189 if (Offset)
Akira Hatanaka95866182012-06-13 19:06:08 +00002190 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002191 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002192
2193 SDValue Ops[] = { Chain, Ptr, Src };
Craig Topper206fcd42014-04-26 19:29:41 +00002194 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002195 LD->getMemOperand());
2196}
2197
2198// Expand an unaligned 32 or 64-bit integer load node.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002199SDValue MipsTargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002200 LoadSDNode *LD = cast<LoadSDNode>(Op);
2201 EVT MemVT = LD->getMemoryVT();
2202
Eric Christopher1c29a652014-07-18 22:55:25 +00002203 if (Subtarget.systemSupportsUnalignedAccess())
Daniel Sandersac272632014-05-23 13:18:02 +00002204 return Op;
2205
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002206 // Return if load is aligned or if MemVT is neither i32 nor i64.
2207 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
2208 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
2209 return SDValue();
2210
Eric Christopher1c29a652014-07-18 22:55:25 +00002211 bool IsLittle = Subtarget.isLittle();
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002212 EVT VT = Op.getValueType();
2213 ISD::LoadExtType ExtType = LD->getExtensionType();
2214 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
2215
2216 assert((VT == MVT::i32) || (VT == MVT::i64));
2217
2218 // Expand
2219 // (set dst, (i64 (load baseptr)))
2220 // to
2221 // (set tmp, (ldl (add baseptr, 7), undef))
2222 // (set dst, (ldr baseptr, tmp))
2223 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002224 SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002225 IsLittle ? 7 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002226 return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002227 IsLittle ? 0 : 7);
2228 }
2229
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002230 SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002231 IsLittle ? 3 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002232 SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002233 IsLittle ? 0 : 3);
2234
2235 // Expand
2236 // (set dst, (i32 (load baseptr))) or
2237 // (set dst, (i64 (sextload baseptr))) or
2238 // (set dst, (i64 (extload baseptr)))
2239 // to
2240 // (set tmp, (lwl (add baseptr, 3), undef))
2241 // (set dst, (lwr baseptr, tmp))
2242 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
2243 (ExtType == ISD::EXTLOAD))
2244 return LWR;
2245
2246 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
2247
2248 // Expand
2249 // (set dst, (i64 (zextload baseptr)))
2250 // to
2251 // (set tmp0, (lwl (add baseptr, 3), undef))
2252 // (set tmp1, (lwr baseptr, tmp0))
2253 // (set tmp2, (shl tmp1, 32))
2254 // (set dst, (srl tmp2, 32))
Andrew Trickef9de2a2013-05-25 02:42:55 +00002255 SDLoc DL(LD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002256 SDValue Const32 = DAG.getConstant(32, MVT::i32);
2257 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
Akira Hatanaka67346852012-06-04 17:46:29 +00002258 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
2259 SDValue Ops[] = { SRL, LWR.getValue(1) };
Craig Topper64941d92014-04-27 19:20:57 +00002260 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002261}
2262
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002263static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002264 SDValue Chain, unsigned Offset) {
Akira Hatanaka95866182012-06-13 19:06:08 +00002265 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
2266 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002267 SDLoc DL(SD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002268 SDVTList VTList = DAG.getVTList(MVT::Other);
2269
2270 if (Offset)
Akira Hatanaka95866182012-06-13 19:06:08 +00002271 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002272 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002273
2274 SDValue Ops[] = { Chain, Value, Ptr };
Craig Topper206fcd42014-04-26 19:29:41 +00002275 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002276 SD->getMemOperand());
2277}
2278
2279// Expand an unaligned 32 or 64-bit integer store node.
Akira Hatanakad82ee942013-05-16 20:45:17 +00002280static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG,
2281 bool IsLittle) {
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002282 SDValue Value = SD->getValue(), Chain = SD->getChain();
2283 EVT VT = Value.getValueType();
2284
2285 // Expand
2286 // (store val, baseptr) or
2287 // (truncstore val, baseptr)
2288 // to
2289 // (swl val, (add baseptr, 3))
2290 // (swr val, baseptr)
2291 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002292 SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002293 IsLittle ? 3 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002294 return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002295 }
2296
2297 assert(VT == MVT::i64);
2298
2299 // Expand
2300 // (store val, baseptr)
2301 // to
2302 // (sdl val, (add baseptr, 7))
2303 // (sdr val, baseptr)
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002304 SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2305 return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002306}
2307
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002308// Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr).
2309static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG) {
2310 SDValue Val = SD->getValue();
2311
2312 if (Val.getOpcode() != ISD::FP_TO_SINT)
2313 return SDValue();
2314
2315 EVT FPTy = EVT::getFloatingPointVT(Val.getValueSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002316 SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy,
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002317 Val.getOperand(0));
2318
Andrew Trickef9de2a2013-05-25 02:42:55 +00002319 return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(),
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002320 SD->getPointerInfo(), SD->isVolatile(),
2321 SD->isNonTemporal(), SD->getAlignment());
2322}
2323
Akira Hatanakad82ee942013-05-16 20:45:17 +00002324SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2325 StoreSDNode *SD = cast<StoreSDNode>(Op);
2326 EVT MemVT = SD->getMemoryVT();
2327
2328 // Lower unaligned integer stores.
Eric Christopher1c29a652014-07-18 22:55:25 +00002329 if (!Subtarget.systemSupportsUnalignedAccess() &&
Daniel Sandersac272632014-05-23 13:18:02 +00002330 (SD->getAlignment() < MemVT.getSizeInBits() / 8) &&
Akira Hatanakad82ee942013-05-16 20:45:17 +00002331 ((MemVT == MVT::i32) || (MemVT == MVT::i64)))
Eric Christopher1c29a652014-07-18 22:55:25 +00002332 return lowerUnalignedIntStore(SD, DAG, Subtarget.isLittle());
Akira Hatanakad82ee942013-05-16 20:45:17 +00002333
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002334 return lowerFP_TO_SINT_STORE(SD, DAG);
Akira Hatanakad82ee942013-05-16 20:45:17 +00002335}
2336
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002337SDValue MipsTargetLowering::lowerADD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002338 if (Op->getOperand(0).getOpcode() != ISD::FRAMEADDR
2339 || cast<ConstantSDNode>
2340 (Op->getOperand(0).getOperand(0))->getZExtValue() != 0
2341 || Op->getOperand(1).getOpcode() != ISD::FRAME_TO_ARGS_OFFSET)
2342 return SDValue();
2343
2344 // The pattern
2345 // (add (frameaddr 0), (frame_to_args_offset))
2346 // results from lowering llvm.eh.dwarf.cfa intrinsic. Transform it to
2347 // (add FrameObject, 0)
2348 // where FrameObject is a fixed StackObject with offset 0 which points to
2349 // the old stack pointer.
2350 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2351 EVT ValTy = Op->getValueType(0);
2352 int FI = MFI->CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
2353 SDValue InArgsAddr = DAG.getFrameIndex(FI, ValTy);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002354 return DAG.getNode(ISD::ADD, SDLoc(Op), ValTy, InArgsAddr,
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002355 DAG.getConstant(0, ValTy));
2356}
2357
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002358SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op,
2359 SelectionDAG &DAG) const {
2360 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002361 SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy,
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002362 Op.getOperand(0));
Andrew Trickef9de2a2013-05-25 02:42:55 +00002363 return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc);
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002364}
2365
Akira Hatanakae2489122011-04-15 21:51:11 +00002366//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002367// Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002368//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002369
Akira Hatanakae2489122011-04-15 21:51:11 +00002370//===----------------------------------------------------------------------===//
Wesley Peck527da1b2010-11-23 03:31:01 +00002371// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002372// Mips O32 ABI rules:
2373// ---
2374// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peck527da1b2010-11-23 03:31:01 +00002375// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002376// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peck527da1b2010-11-23 03:31:01 +00002377// f64 - Only passed in two aliased f32 registers if no int reg has been used
2378// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Sylvestre Ledru469de192014-08-11 18:04:46 +00002379// not used, it must be shadowed. If only A3 is available, shadow it and
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002380// go to stack.
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002381//
2382// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanakae2489122011-04-15 21:51:11 +00002383//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002384
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00002385static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2386 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
Craig Topper840beec2014-04-04 05:16:06 +00002387 CCState &State, const MCPhysReg *F64Regs) {
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002388
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00002389 static const unsigned IntRegsSize = 4, FloatRegsSize = 2;
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002390
Craig Topper840beec2014-04-04 05:16:06 +00002391 static const MCPhysReg IntRegs[] = { Mips::A0, Mips::A1, Mips::A2, Mips::A3 };
2392 static const MCPhysReg F32Regs[] = { Mips::F12, Mips::F14 };
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002393
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002394 // Do not process byval args here.
2395 if (ArgFlags.isByVal())
2396 return true;
Akira Hatanaka5e16c6a2011-05-24 19:18:33 +00002397
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002398 // Promote i8 and i16
2399 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
2400 LocVT = MVT::i32;
2401 if (ArgFlags.isSExt())
2402 LocInfo = CCValAssign::SExt;
2403 else if (ArgFlags.isZExt())
2404 LocInfo = CCValAssign::ZExt;
2405 else
2406 LocInfo = CCValAssign::AExt;
2407 }
2408
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002409 unsigned Reg;
2410
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002411 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
2412 // is true: function is vararg, argument is 3rd or higher, there is previous
2413 // argument which is not f32 or f64.
2414 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
2415 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanaka9e6a8cc2011-05-19 20:29:48 +00002416 unsigned OrigAlign = ArgFlags.getOrigAlign();
2417 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002418
2419 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002420 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanaka9e6a8cc2011-05-19 20:29:48 +00002421 // If this is the first part of an i64 arg,
2422 // the allocated register must be either A0 or A2.
2423 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
2424 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002425 LocVT = MVT::i32;
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002426 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
2427 // Allocate int register and shadow next int register. If first
2428 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002429 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2430 if (Reg == Mips::A1 || Reg == Mips::A3)
2431 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2432 State.AllocateReg(IntRegs, IntRegsSize);
2433 LocVT = MVT::i32;
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002434 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
2435 // we are guaranteed to find an available float register
2436 if (ValVT == MVT::f32) {
2437 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
2438 // Shadow int register
2439 State.AllocateReg(IntRegs, IntRegsSize);
2440 } else {
2441 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
2442 // Shadow int registers
2443 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
2444 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
2445 State.AllocateReg(IntRegs, IntRegsSize);
2446 State.AllocateReg(IntRegs, IntRegsSize);
2447 }
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002448 } else
2449 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002450
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002451 if (!Reg) {
2452 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3,
2453 OrigAlign);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002454 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002455 } else
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002456 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002457
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002458 return false;
Akira Hatanaka202f6402011-11-12 02:20:46 +00002459}
2460
Akira Hatanakabfb66242013-08-20 23:38:40 +00002461static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT,
2462 MVT LocVT, CCValAssign::LocInfo LocInfo,
2463 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002464 static const MCPhysReg F64Regs[] = { Mips::D6, Mips::D7 };
Akira Hatanakabfb66242013-08-20 23:38:40 +00002465
2466 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2467}
2468
2469static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT,
2470 MVT LocVT, CCValAssign::LocInfo LocInfo,
2471 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002472 static const MCPhysReg F64Regs[] = { Mips::D12_64, Mips::D14_64 };
Akira Hatanakabfb66242013-08-20 23:38:40 +00002473
2474 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2475}
2476
Akira Hatanaka202f6402011-11-12 02:20:46 +00002477#include "MipsGenCallingConv.inc"
2478
Akira Hatanakae2489122011-04-15 21:51:11 +00002479//===----------------------------------------------------------------------===//
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002480// Call Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002481//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002482
Akira Hatanaka61bbcce2011-09-23 00:58:33 +00002483// Return next O32 integer argument register.
2484static unsigned getNextIntArgReg(unsigned Reg) {
2485 assert((Reg == Mips::A0) || (Reg == Mips::A2));
2486 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
2487}
2488
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002489SDValue
2490MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002491 SDValue Chain, SDValue Arg, SDLoc DL,
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002492 bool IsTailCall, SelectionDAG &DAG) const {
2493 if (!IsTailCall) {
2494 SDValue PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr,
2495 DAG.getIntPtrConstant(Offset));
2496 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo(), false,
2497 false, 0);
2498 }
2499
2500 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2501 int FI = MFI->CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
2502 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2503 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(),
2504 /*isVolatile=*/ true, false, 0);
2505}
2506
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002507void MipsTargetLowering::
2508getOpndList(SmallVectorImpl<SDValue> &Ops,
2509 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
2510 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
Sasa Stankovic7072a792014-10-01 08:22:21 +00002511 bool IsCallReloc, CallLoweringInfo &CLI, SDValue Callee,
2512 SDValue Chain) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002513 // Insert node "GP copy globalreg" before call to function.
2514 //
2515 // R_MIPS_CALL* operators (emitted when non-internal functions are called
2516 // in PIC mode) allow symbols to be resolved via lazy binding.
2517 // The lazy binding stub requires GP to point to the GOT.
Sasa Stankovic7072a792014-10-01 08:22:21 +00002518 // Note that we don't need GP to point to the GOT for indirect calls
2519 // (when R_MIPS_CALL* is not used for the call) because Mips linker generates
2520 // lazy binding stub for a function only when R_MIPS_CALL* are the only relocs
2521 // used for the function (that is, Mips linker doesn't generate lazy binding
2522 // stub for a function whose address is taken in the program).
2523 if (IsPICCall && !InternalLinkage && IsCallReloc) {
Eric Christopher1c29a652014-07-18 22:55:25 +00002524 unsigned GPReg = Subtarget.isABI_N64() ? Mips::GP_64 : Mips::GP;
2525 EVT Ty = Subtarget.isABI_N64() ? MVT::i64 : MVT::i32;
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002526 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty)));
2527 }
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002528
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002529 // Build a sequence of copy-to-reg nodes chained together with token
2530 // chain and flag operands which copy the outgoing args into registers.
2531 // The InFlag in necessary since all emitted instructions must be
2532 // stuck together.
2533 SDValue InFlag;
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002534
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002535 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2536 Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, RegsToPass[i].first,
2537 RegsToPass[i].second, InFlag);
2538 InFlag = Chain.getValue(1);
2539 }
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002540
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002541 // Add argument registers to the end of the list so that they are
2542 // known live into the call.
2543 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2544 Ops.push_back(CLI.DAG.getRegister(RegsToPass[i].first,
2545 RegsToPass[i].second.getValueType()));
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002546
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002547 // Add a register mask operand representing the call-preserved registers.
Eric Christopherd9134482014-08-04 21:25:23 +00002548 const TargetRegisterInfo *TRI =
2549 getTargetMachine().getSubtargetImpl()->getRegisterInfo();
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002550 const uint32_t *Mask = TRI->getCallPreservedMask(CLI.CallConv);
2551 assert(Mask && "Missing call preserved mask for calling convention");
Eric Christopher1c29a652014-07-18 22:55:25 +00002552 if (Subtarget.inMips16HardFloat()) {
Reed Kotler783c7942013-05-10 22:25:39 +00002553 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
2554 llvm::StringRef Sym = G->getGlobal()->getName();
2555 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
Reed Kotler3230e722013-12-12 02:41:11 +00002556 if (F && F->hasFnAttribute("__Mips16RetHelper")) {
Reed Kotler783c7942013-05-10 22:25:39 +00002557 Mask = MipsRegisterInfo::getMips16RetHelperMask();
2558 }
2559 }
2560 }
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002561 Ops.push_back(CLI.DAG.getRegisterMask(Mask));
2562
2563 if (InFlag.getNode())
2564 Ops.push_back(InFlag);
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002565}
2566
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002567/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman624801e2009-01-26 03:15:54 +00002568/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002569SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00002570MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002571 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00002572 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002573 SDLoc DL = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00002574 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
2575 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
2576 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Akira Hatanakabeda2242012-07-31 18:46:41 +00002577 SDValue Chain = CLI.Chain;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002578 SDValue Callee = CLI.Callee;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002579 bool &IsTailCall = CLI.IsTailCall;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002580 CallingConv::ID CallConv = CLI.CallConv;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002581 bool IsVarArg = CLI.IsVarArg;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002582
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002583 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002584 MachineFrameInfo *MFI = MF.getFrameInfo();
Eric Christopherfc6de422014-08-05 02:39:49 +00002585 const TargetFrameLowering *TFL = MF.getSubtarget().getFrameLowering();
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002586 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +00002587 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002588
2589 // Analyze operands of the call, assigning locations to each operand.
2590 SmallVector<CCValAssign, 16> ArgLocs;
Daniel Sanderseac096082014-11-01 18:38:03 +00002591 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
2592 *DAG.getContext());
Daniel Sanders068eea22014-11-01 17:44:51 +00002593 MipsCC MipsCCInfo(CallConv, Subtarget, CCInfo);
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002594
Daniel Sanderseac096082014-11-01 18:38:03 +00002595 CCInfo.PreAnalyzeCallOperandsForF128_(Outs, CLI.getArgs(), Callee.getNode());
Daniel Sanders853c2432014-11-01 18:13:52 +00002596 MipsCCInfo.analyzeCallOperands(Outs, IsVarArg, Subtarget.abiUsesSoftFloat(),
2597 Callee.getNode(), CLI.getArgs(), CCInfo);
Daniel Sanderseac096082014-11-01 18:38:03 +00002598 CCInfo.ClearOriginalArgWasF128();
Wesley Peck527da1b2010-11-23 03:31:01 +00002599
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002600 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka195a1e22011-06-08 17:39:33 +00002601 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka97ba7692012-07-26 23:27:01 +00002602
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002603 // Check if it's really possible to do a tail call.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002604 if (IsTailCall)
2605 IsTailCall =
2606 isEligibleForTailCallOptimization(MipsCCInfo, NextStackOffset,
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002607 *MF.getInfo<MipsFunctionInfo>());
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002608
Reid Kleckner5772b772014-04-24 20:14:34 +00002609 if (!IsTailCall && CLI.CS && CLI.CS->isMustTailCall())
2610 report_fatal_error("failed to perform tail call elimination on a call "
2611 "site marked musttail");
2612
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002613 if (IsTailCall)
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002614 ++NumTailCalls;
2615
Akira Hatanaka79738332011-09-19 20:26:02 +00002616 // Chain is the output chain of the last Load/Store or CopyToReg node.
2617 // ByValChain is the output chain of the last Memcpy node created for copying
2618 // byval arguments to the stack.
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002619 unsigned StackAlignment = TFL->getStackAlignment();
2620 NextStackOffset = RoundUpToAlignment(NextStackOffset, StackAlignment);
Akira Hatanaka79738332011-09-19 20:26:02 +00002621 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002622
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002623 if (!IsTailCall)
Andrew Trickad6d08a2013-05-29 22:03:55 +00002624 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffsetVal, DL);
Akira Hatanakabeda2242012-07-31 18:46:41 +00002625
Daniel Sandersd897b562014-03-27 10:46:12 +00002626 SDValue StackPtr = DAG.getCopyFromReg(
Eric Christopher1c29a652014-07-18 22:55:25 +00002627 Chain, DL, Subtarget.isABI_N64() ? Mips::SP_64 : Mips::SP,
Eric Christopherbf33a3c2014-07-02 23:18:40 +00002628 getPointerTy());
Akira Hatanaka195a1e22011-06-08 17:39:33 +00002629
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002630 // With EABI is it possible to have 16 args on registers.
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002631 std::deque< std::pair<unsigned, SDValue> > RegsToPass;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002632 SmallVector<SDValue, 8> MemOpChains;
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002633 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002634
2635 // Walk the register/memloc assignments, inserting copies/loads.
2636 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002637 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002638 CCValAssign &VA = ArgLocs[i];
Akira Hatanakab20a3252011-10-28 19:49:00 +00002639 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka19891f82011-11-12 02:34:50 +00002640 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2641
2642 // ByVal Arg.
2643 if (Flags.isByVal()) {
2644 assert(Flags.getByValSize() &&
2645 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002646 assert(ByValArg != MipsCCInfo.byval_end());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002647 assert(!IsTailCall &&
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002648 "Do not tail-call optimize if there is a byval argument.");
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002649 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
Eric Christopher1c29a652014-07-18 22:55:25 +00002650 MipsCCInfo, *ByValArg, Flags, Subtarget.isLittle());
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002651 ++ByValArg;
Akira Hatanaka19891f82011-11-12 02:34:50 +00002652 continue;
2653 }
Jia Liuf54f60f2012-02-28 07:46:26 +00002654
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002655 // Promote the value if needed.
2656 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002657 default: llvm_unreachable("Unknown loc info!");
Wesley Peck527da1b2010-11-23 03:31:01 +00002658 case CCValAssign::Full:
Akira Hatanakab20a3252011-10-28 19:49:00 +00002659 if (VA.isRegLoc()) {
2660 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
Akira Hatanaka3b7391d2013-03-05 22:20:28 +00002661 (ValVT == MVT::f64 && LocVT == MVT::i64) ||
2662 (ValVT == MVT::i64 && LocVT == MVT::f64))
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002663 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
Akira Hatanakab20a3252011-10-28 19:49:00 +00002664 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002665 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanakae2489122011-04-15 21:51:11 +00002666 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002667 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanakaaef55c82011-04-15 21:00:26 +00002668 Arg, DAG.getConstant(1, MVT::i32));
Eric Christopher1c29a652014-07-18 22:55:25 +00002669 if (!Subtarget.isLittle())
Akira Hatanaka27916972011-04-15 19:52:08 +00002670 std::swap(Lo, Hi);
Jia Liuf54f60f2012-02-28 07:46:26 +00002671 unsigned LocRegLo = VA.getLocReg();
Akira Hatanaka61bbcce2011-09-23 00:58:33 +00002672 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2673 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2674 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002675 continue;
Wesley Peck527da1b2010-11-23 03:31:01 +00002676 }
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002677 }
2678 break;
Daniel Sanderseac096082014-11-01 18:38:03 +00002679 case CCValAssign::BCvt:
2680 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
2681 break;
Chris Lattner52f16de2008-03-17 06:57:02 +00002682 case CCValAssign::SExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002683 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002684 break;
2685 case CCValAssign::ZExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002686 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002687 break;
2688 case CCValAssign::AExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002689 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002690 break;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002691 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002692
2693 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +00002694 // RegsToPass vector
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002695 if (VA.isRegLoc()) {
2696 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattner52f16de2008-03-17 06:57:02 +00002697 continue;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002698 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002699
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002700 // Register can't get to this point...
Chris Lattner52f16de2008-03-17 06:57:02 +00002701 assert(VA.isMemLoc());
Wesley Peck527da1b2010-11-23 03:31:01 +00002702
Wesley Peck527da1b2010-11-23 03:31:01 +00002703 // emit ISD::STORE whichs stores the
Chris Lattner52f16de2008-03-17 06:57:02 +00002704 // parameter value to a stack Location
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002705 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002706 Chain, Arg, DL, IsTailCall, DAG));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002707 }
2708
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002709 // Transform all store nodes into one single node because all store
2710 // nodes are independent of each other.
Wesley Peck527da1b2010-11-23 03:31:01 +00002711 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00002712 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002713
Bill Wendling24c79f22008-09-16 21:48:12 +00002714 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peck527da1b2010-11-23 03:31:01 +00002715 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2716 // node so that legalize doesn't hack it.
Eric Christopherbf33a3c2014-07-02 23:18:40 +00002717 bool IsPICCall =
Eric Christopher1c29a652014-07-18 22:55:25 +00002718 (Subtarget.isABI_N64() || IsPIC); // true if calls are translated to
Eric Christopherbf33a3c2014-07-02 23:18:40 +00002719 // jalr $25
Sasa Stankovic7072a792014-10-01 08:22:21 +00002720 bool GlobalOrExternal = false, InternalLinkage = false, IsCallReloc = false;
Akira Hatanakad6f1c582011-04-07 19:51:44 +00002721 SDValue CalleeLo;
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002722 EVT Ty = Callee.getValueType();
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002723
2724 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002725 if (IsPICCall) {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002726 const GlobalValue *Val = G->getGlobal();
2727 InternalLinkage = Val->hasInternalLinkage();
Akira Hatanakacf9a61b2012-12-13 03:17:29 +00002728
2729 if (InternalLinkage)
Eric Christopherbf33a3c2014-07-02 23:18:40 +00002730 Callee = getAddrLocal(G, Ty, DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +00002731 Subtarget.isABI_N32() || Subtarget.isABI_N64());
Sasa Stankovic7072a792014-10-01 08:22:21 +00002732 else if (LargeGOT) {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002733 Callee = getAddrGlobalLargeGOT(G, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002734 MipsII::MO_CALL_LO16, Chain,
2735 FuncInfo->callPtrInfo(Val));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002736 IsCallReloc = true;
2737 } else {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002738 Callee = getAddrGlobal(G, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
2739 FuncInfo->callPtrInfo(Val));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002740 IsCallReloc = true;
2741 }
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002742 } else
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002743 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, getPointerTy(), 0,
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002744 MipsII::MO_NO_FLAG);
Akira Hatanaka8e16aac2011-12-09 01:45:12 +00002745 GlobalOrExternal = true;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002746 }
2747 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002748 const char *Sym = S->getSymbol();
2749
Eric Christopher1c29a652014-07-18 22:55:25 +00002750 if (!Subtarget.isABI_N64() && !IsPIC) // !N64 && static
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002751 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(),
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002752 MipsII::MO_NO_FLAG);
Sasa Stankovic7072a792014-10-01 08:22:21 +00002753 else if (LargeGOT) {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002754 Callee = getAddrGlobalLargeGOT(S, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002755 MipsII::MO_CALL_LO16, Chain,
2756 FuncInfo->callPtrInfo(Sym));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002757 IsCallReloc = true;
2758 } else { // N64 || PIC
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002759 Callee = getAddrGlobal(S, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
2760 FuncInfo->callPtrInfo(Sym));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002761 IsCallReloc = true;
2762 }
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002763
Akira Hatanaka8e16aac2011-12-09 01:45:12 +00002764 GlobalOrExternal = true;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002765 }
2766
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002767 SmallVector<SDValue, 8> Ops(1, Chain);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002768 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002769
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002770 getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal, InternalLinkage,
Sasa Stankovic7072a792014-10-01 08:22:21 +00002771 IsCallReloc, CLI, Callee, Chain);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002772
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002773 if (IsTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +00002774 return DAG.getNode(MipsISD::TailCall, DL, MVT::Other, Ops);
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002775
Craig Topper48d114b2014-04-26 18:35:24 +00002776 Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, Ops);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002777 SDValue InFlag = Chain.getValue(1);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002778
Bruno Cardoso Lopes193e64c2010-01-30 18:32:07 +00002779 // Create the CALLSEQ_END node.
Akira Hatanaka97ba7692012-07-26 23:27:01 +00002780 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal,
Andrew Trickad6d08a2013-05-29 22:03:55 +00002781 DAG.getIntPtrConstant(0, true), InFlag, DL);
Bruno Cardoso Lopes193e64c2010-01-30 18:32:07 +00002782 InFlag = Chain.getValue(1);
2783
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002784 // Handle result values, copying them out of physregs into vregs that we
2785 // return.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002786 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg, Ins, DL, DAG,
2787 InVals, CLI);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002788}
2789
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002790/// LowerCallResult - Lower the result values of a call into the
2791/// appropriate copies out of appropriate physical registers.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002792SDValue MipsTargetLowering::LowerCallResult(
2793 SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool IsVarArg,
2794 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG,
2795 SmallVectorImpl<SDValue> &InVals,
2796 TargetLowering::CallLoweringInfo &CLI) const {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002797 // Assign locations to each value returned by this call.
2798 SmallVector<CCValAssign, 16> RVLocs;
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002799 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
2800 *DAG.getContext());
2801 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips, CLI);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002802
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002803 // Copy all of the result registers out of their specified physreg.
2804 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Daniel Sandersae275e32014-09-25 12:15:05 +00002805 CCValAssign &VA = RVLocs[i];
2806 assert(VA.isRegLoc() && "Can only return in registers!");
2807
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002808 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(),
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002809 RVLocs[i].getLocVT(), InFlag);
2810 Chain = Val.getValue(1);
2811 InFlag = Val.getValue(2);
2812
Daniel Sandersae275e32014-09-25 12:15:05 +00002813 if (VA.isUpperBitsInLoc()) {
2814 unsigned ValSizeInBits = Ins[i].ArgVT.getSizeInBits();
2815 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
2816 unsigned Shift =
2817 VA.getLocInfo() == CCValAssign::ZExtUpper ? ISD::SRL : ISD::SRA;
2818 Val = DAG.getNode(
2819 Shift, DL, VA.getLocVT(), Val,
2820 DAG.getConstant(LocSizeInBits - ValSizeInBits, VA.getLocVT()));
2821 }
2822
2823 switch (VA.getLocInfo()) {
2824 default:
2825 llvm_unreachable("Unknown loc info!");
2826 case CCValAssign::Full:
2827 break;
2828 case CCValAssign::BCvt:
2829 Val = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Val);
2830 break;
2831 case CCValAssign::AExt:
2832 case CCValAssign::AExtUpper:
2833 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2834 break;
2835 case CCValAssign::ZExt:
2836 case CCValAssign::ZExtUpper:
2837 Val = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Val,
2838 DAG.getValueType(VA.getValVT()));
2839 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2840 break;
2841 case CCValAssign::SExt:
2842 case CCValAssign::SExtUpper:
2843 Val = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Val,
2844 DAG.getValueType(VA.getValVT()));
2845 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2846 break;
2847 }
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002848
2849 InVals.push_back(Val);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002850 }
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +00002851
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002852 return Chain;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002853}
2854
Akira Hatanakae2489122011-04-15 21:51:11 +00002855//===----------------------------------------------------------------------===//
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002856// Formal Arguments Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002857//===----------------------------------------------------------------------===//
Wesley Peck527da1b2010-11-23 03:31:01 +00002858/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002859/// and generate load operations for arguments places on the stack.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002860SDValue
2861MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanakaaef55c82011-04-15 21:00:26 +00002862 CallingConv::ID CallConv,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002863 bool IsVarArg,
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00002864 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002865 SDLoc DL, SelectionDAG &DAG,
Akira Hatanakaaef55c82011-04-15 21:00:26 +00002866 SmallVectorImpl<SDValue> &InVals)
Akira Hatanakae2489122011-04-15 21:51:11 +00002867 const {
Bruno Cardoso Lopesa01ede22008-08-04 07:12:52 +00002868 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002869 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopes14033fb2007-08-28 05:08:16 +00002870 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002871
Dan Gohman31ae5862010-04-17 14:41:14 +00002872 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002873
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002874 // Used with vargs to acumulate store chains.
2875 std::vector<SDValue> OutChains;
2876
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002877 // Assign locations to all of the incoming arguments.
2878 SmallVector<CCValAssign, 16> ArgLocs;
Daniel Sanderseac096082014-11-01 18:38:03 +00002879 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
2880 *DAG.getContext());
Daniel Sanders4abcfe22014-09-09 10:46:48 +00002881 MipsCC MipsCCInfo(CallConv, Subtarget, CCInfo);
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002882 Function::const_arg_iterator FuncArg =
2883 DAG.getMachineFunction().getFunction()->arg_begin();
Eric Christopher1c29a652014-07-18 22:55:25 +00002884 bool UseSoftFloat = Subtarget.abiUsesSoftFloat();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002885
Daniel Sanderseac096082014-11-01 18:38:03 +00002886 CCInfo.PreAnalyzeFormalArgumentsForF128_(Ins);
2887 MipsCCInfo.analyzeFormalArguments(Ins, UseSoftFloat, CCInfo);
2888 CCInfo.ClearOriginalArgWasF128();
Akira Hatanaka4866fe12012-10-30 19:37:25 +00002889 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(),
2890 MipsCCInfo.hasByValArg());
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002891
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00002892 unsigned CurArgIdx = 0;
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002893 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002894
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00002895 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002896 CCValAssign &VA = ArgLocs[i];
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00002897 std::advance(FuncArg, Ins[i].OrigArgIndex - CurArgIdx);
2898 CurArgIdx = Ins[i].OrigArgIndex;
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002899 EVT ValVT = VA.getValVT();
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002900 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2901 bool IsRegLoc = VA.isRegLoc();
2902
2903 if (Flags.isByVal()) {
2904 assert(Flags.getByValSize() &&
2905 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002906 assert(ByValArg != MipsCCInfo.byval_end());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002907 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg,
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002908 MipsCCInfo, *ByValArg);
2909 ++ByValArg;
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002910 continue;
2911 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002912
2913 // Arguments stored on registers
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002914 if (IsRegLoc) {
Akira Hatanaka7d822522013-10-28 21:21:36 +00002915 MVT RegVT = VA.getLocVT();
Akira Hatanakacb4a1a82011-05-24 00:23:52 +00002916 unsigned ArgReg = VA.getLocReg();
Akira Hatanaka7d822522013-10-28 21:21:36 +00002917 const TargetRegisterClass *RC = getRegClassFor(RegVT);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002918
Wesley Peck527da1b2010-11-23 03:31:01 +00002919 // Transform the arguments stored on
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002920 // physical registers into virtual ones
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002921 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC);
2922 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
Wesley Peck527da1b2010-11-23 03:31:01 +00002923
2924 // If this is an 8 or 16-bit value, it has been passed promoted
2925 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002926 // truncate to the right size.
Daniel Sanderseac096082014-11-01 18:38:03 +00002927 switch (VA.getLocInfo()) {
2928 default:
2929 llvm_unreachable("Unknown loc info!");
2930 case CCValAssign::Full:
2931 break;
2932 case CCValAssign::SExt:
2933 ArgValue = DAG.getNode(ISD::AssertSext, DL, RegVT, ArgValue,
2934 DAG.getValueType(ValVT));
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002935 ArgValue = DAG.getNode(ISD::TRUNCATE, DL, ValVT, ArgValue);
Daniel Sanderseac096082014-11-01 18:38:03 +00002936 break;
2937 case CCValAssign::ZExt:
2938 ArgValue = DAG.getNode(ISD::AssertZext, DL, RegVT, ArgValue,
2939 DAG.getValueType(ValVT));
2940 ArgValue = DAG.getNode(ISD::TRUNCATE, DL, ValVT, ArgValue);
2941 break;
2942 case CCValAssign::BCvt:
2943 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
2944 break;
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002945 }
2946
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002947 // Handle floating point arguments passed in integer registers and
2948 // long double arguments passed in floating point registers.
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002949 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002950 (RegVT == MVT::i64 && ValVT == MVT::f64) ||
2951 (RegVT == MVT::f64 && ValVT == MVT::i64))
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002952 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
Eric Christopher1c29a652014-07-18 22:55:25 +00002953 else if (Subtarget.isABI_O32() && RegVT == MVT::i32 &&
Eric Christopherbf33a3c2014-07-02 23:18:40 +00002954 ValVT == MVT::f64) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002955 unsigned Reg2 = addLiveIn(DAG.getMachineFunction(),
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002956 getNextIntArgReg(ArgReg), RC);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002957 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT);
Eric Christopher1c29a652014-07-18 22:55:25 +00002958 if (!Subtarget.isLittle())
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002959 std::swap(ArgValue, ArgValue2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002960 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64,
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002961 ArgValue, ArgValue2);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002962 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002963
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002964 InVals.push_back(ArgValue);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002965 } else { // VA.isRegLoc()
2966
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002967 // sanity check
2968 assert(VA.isMemLoc());
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002969
Wesley Peck527da1b2010-11-23 03:31:01 +00002970 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002971 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakacb4a1a82011-05-24 00:23:52 +00002972 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002973
2974 // Create load nodes to retrieve arguments from the stack
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002975 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Akira Hatanakad1c58ed2013-11-09 02:38:51 +00002976 SDValue Load = DAG.getLoad(ValVT, DL, Chain, FIN,
2977 MachinePointerInfo::getFixedStack(FI),
2978 false, false, false, 0);
2979 InVals.push_back(Load);
2980 OutChains.push_back(Load.getValue(1));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002981 }
Reid Kleckner7a59e082014-05-12 22:01:27 +00002982 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002983
Reid Kleckner7a59e082014-05-12 22:01:27 +00002984 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Reid Kleckner79418562014-05-09 22:32:13 +00002985 // The mips ABIs for returning structs by value requires that we copy
2986 // the sret argument into $v0 for the return. Save the argument into
2987 // a virtual register so that we can access it from the return points.
Reid Kleckner7a59e082014-05-12 22:01:27 +00002988 if (Ins[i].Flags.isSRet()) {
Reid Kleckner79418562014-05-09 22:32:13 +00002989 unsigned Reg = MipsFI->getSRetReturnReg();
2990 if (!Reg) {
2991 Reg = MF.getRegInfo().createVirtualRegister(
Eric Christopher1c29a652014-07-18 22:55:25 +00002992 getRegClassFor(Subtarget.isABI_N64() ? MVT::i64 : MVT::i32));
Reid Kleckner79418562014-05-09 22:32:13 +00002993 MipsFI->setSRetReturnReg(Reg);
2994 }
Reid Kleckner7a59e082014-05-12 22:01:27 +00002995 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[i]);
Reid Kleckner79418562014-05-09 22:32:13 +00002996 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
Reid Kleckner7a59e082014-05-12 22:01:27 +00002997 break;
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002998 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002999 }
3000
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003001 if (IsVarArg)
Daniel Sanders853c2432014-11-01 18:13:52 +00003002 writeVarArgRegs(OutChains, MipsCCInfo, Chain, DL, DAG, CCInfo);
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003003
Wesley Peck527da1b2010-11-23 03:31:01 +00003004 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003005 // the size of Ins and InVals. This only happens when on varg functions
3006 if (!OutChains.empty()) {
3007 OutChains.push_back(Chain);
Craig Topper48d114b2014-04-26 18:35:24 +00003008 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003009 }
3010
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003011 return Chain;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003012}
3013
Akira Hatanakae2489122011-04-15 21:51:11 +00003014//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003015// Return Value Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00003016//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003017
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003018bool
3019MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003020 MachineFunction &MF, bool IsVarArg,
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003021 const SmallVectorImpl<ISD::OutputArg> &Outs,
3022 LLVMContext &Context) const {
3023 SmallVector<CCValAssign, 16> RVLocs;
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003024 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003025 return CCInfo.CheckReturn(Outs, RetCC_Mips);
3026}
3027
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003028SDValue
3029MipsTargetLowering::LowerReturn(SDValue Chain,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003030 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003031 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003032 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003033 SDLoc DL, SelectionDAG &DAG) const {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003034 // CCValAssign - represent the assignment of
3035 // the return value to a location
3036 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003037 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003038
3039 // CCState - Info about the registers and stack slot.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003040 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, *DAG.getContext());
Daniel Sanders4abcfe22014-09-09 10:46:48 +00003041 MipsCC MipsCCInfo(CallConv, Subtarget, CCInfo);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003042
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003043 // Analyze return values.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003044 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003045
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003046 SDValue Flag;
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003047 SmallVector<SDValue, 4> RetOps(1, Chain);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003048
3049 // Copy the result values into the output registers.
3050 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003051 SDValue Val = OutVals[i];
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003052 CCValAssign &VA = RVLocs[i];
3053 assert(VA.isRegLoc() && "Can only return in registers!");
Daniel Sandersae275e32014-09-25 12:15:05 +00003054 bool UseUpperBits = false;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003055
Daniel Sandersae275e32014-09-25 12:15:05 +00003056 switch (VA.getLocInfo()) {
3057 default:
3058 llvm_unreachable("Unknown loc info!");
3059 case CCValAssign::Full:
3060 break;
3061 case CCValAssign::BCvt:
3062 Val = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Val);
3063 break;
3064 case CCValAssign::AExtUpper:
3065 UseUpperBits = true;
3066 // Fallthrough
3067 case CCValAssign::AExt:
3068 Val = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Val);
3069 break;
3070 case CCValAssign::ZExtUpper:
3071 UseUpperBits = true;
3072 // Fallthrough
3073 case CCValAssign::ZExt:
3074 Val = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Val);
3075 break;
3076 case CCValAssign::SExtUpper:
3077 UseUpperBits = true;
3078 // Fallthrough
3079 case CCValAssign::SExt:
3080 Val = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Val);
3081 break;
3082 }
3083
3084 if (UseUpperBits) {
3085 unsigned ValSizeInBits = Outs[i].ArgVT.getSizeInBits();
3086 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
3087 Val = DAG.getNode(
3088 ISD::SHL, DL, VA.getLocVT(), Val,
3089 DAG.getConstant(LocSizeInBits - ValSizeInBits, VA.getLocVT()));
3090 }
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003091
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003092 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003093
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003094 // Guarantee that all emitted copies are stuck together with flags.
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003095 Flag = Chain.getValue(1);
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003096 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003097 }
3098
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003099 // The mips ABIs for returning structs by value requires that we copy
3100 // the sret argument into $v0 for the return. We saved the argument into
3101 // a virtual register in the entry block, so now we copy the value out
3102 // and into $v0.
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003103 if (MF.getFunction()->hasStructRetAttr()) {
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003104 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3105 unsigned Reg = MipsFI->getSRetReturnReg();
3106
Wesley Peck527da1b2010-11-23 03:31:01 +00003107 if (!Reg)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003108 llvm_unreachable("sret virtual register not created in the entry block");
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003109 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Eric Christopher1c29a652014-07-18 22:55:25 +00003110 unsigned V0 = Subtarget.isABI_N64() ? Mips::V0_64 : Mips::V0;
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003111
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003112 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003113 Flag = Chain.getValue(1);
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003114 RetOps.push_back(DAG.getRegister(V0, getPointerTy()));
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003115 }
3116
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003117 RetOps[0] = Chain; // Update chain.
Akira Hatanakaefff7b72012-07-10 00:19:06 +00003118
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003119 // Add the flag if we have it.
3120 if (Flag.getNode())
3121 RetOps.push_back(Flag);
3122
3123 // Return on Mips is always a "jr $ra"
Craig Topper48d114b2014-04-26 18:35:24 +00003124 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, RetOps);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003125}
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003126
Akira Hatanakae2489122011-04-15 21:51:11 +00003127//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003128// Mips Inline Assembly Support
Akira Hatanakae2489122011-04-15 21:51:11 +00003129//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003130
3131/// getConstraintType - Given a constraint letter, return the type of
3132/// constraint it is for this target.
3133MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peck527da1b2010-11-23 03:31:01 +00003134getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003135{
Daniel Sanders8b59af12013-11-12 12:56:01 +00003136 // Mips specific constraints
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003137 // GCC config/mips/constraints.md
3138 //
Wesley Peck527da1b2010-11-23 03:31:01 +00003139 // 'd' : An address register. Equivalent to r
3140 // unless generating MIPS16 code.
3141 // 'y' : Equivalent to r; retained for
3142 // backwards compatibility.
Eric Christophere3c494d2012-05-07 06:25:10 +00003143 // 'c' : A register suitable for use in an indirect
3144 // jump. This will always be $25 for -mabicalls.
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003145 // 'l' : The lo register. 1 word storage.
3146 // 'x' : The hilo register pair. Double word storage.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003147 if (Constraint.size() == 1) {
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003148 switch (Constraint[0]) {
3149 default : break;
Wesley Peck527da1b2010-11-23 03:31:01 +00003150 case 'd':
3151 case 'y':
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003152 case 'f':
Eric Christophere3c494d2012-05-07 06:25:10 +00003153 case 'c':
Eric Christopher9c492e62012-05-07 06:25:15 +00003154 case 'l':
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003155 case 'x':
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003156 return C_RegisterClass;
Jack Carter0e149b02013-03-04 21:33:15 +00003157 case 'R':
3158 return C_Memory;
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003159 }
3160 }
3161 return TargetLowering::getConstraintType(Constraint);
3162}
3163
John Thompsone8360b72010-10-29 17:29:13 +00003164/// Examine constraint type and operand type and determine a weight value.
3165/// This object must already have been set up with the operand type
3166/// and the current alternative constraint selected.
3167TargetLowering::ConstraintWeight
3168MipsTargetLowering::getSingleConstraintMatchWeight(
3169 AsmOperandInfo &info, const char *constraint) const {
3170 ConstraintWeight weight = CW_Invalid;
3171 Value *CallOperandVal = info.CallOperandVal;
3172 // If we don't have a value, we can't do a match,
3173 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +00003174 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +00003175 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +00003176 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +00003177 // Look at the constraint type.
3178 switch (*constraint) {
3179 default:
3180 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
3181 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00003182 case 'd':
3183 case 'y':
John Thompsone8360b72010-10-29 17:29:13 +00003184 if (type->isIntegerTy())
3185 weight = CW_Register;
3186 break;
Daniel Sanders8b59af12013-11-12 12:56:01 +00003187 case 'f': // FPU or MSA register
Eric Christopher1c29a652014-07-18 22:55:25 +00003188 if (Subtarget.hasMSA() && type->isVectorTy() &&
Daniel Sanders8b59af12013-11-12 12:56:01 +00003189 cast<VectorType>(type)->getBitWidth() == 128)
3190 weight = CW_Register;
3191 else if (type->isFloatTy())
John Thompsone8360b72010-10-29 17:29:13 +00003192 weight = CW_Register;
3193 break;
Eric Christophere3c494d2012-05-07 06:25:10 +00003194 case 'c': // $25 for indirect jumps
Eric Christopher9c492e62012-05-07 06:25:15 +00003195 case 'l': // lo register
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003196 case 'x': // hilo register pair
Daniel Sanders8b59af12013-11-12 12:56:01 +00003197 if (type->isIntegerTy())
Eric Christophere3c494d2012-05-07 06:25:10 +00003198 weight = CW_SpecificReg;
Daniel Sanders8b59af12013-11-12 12:56:01 +00003199 break;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003200 case 'I': // signed 16 bit immediate
Eric Christopher7201e1b2012-05-07 03:13:42 +00003201 case 'J': // integer zero
Eric Christopher3ff88a02012-05-07 05:46:29 +00003202 case 'K': // unsigned 16 bit immediate
Eric Christopher1109b342012-05-07 05:46:37 +00003203 case 'L': // signed 32 bit immediate where lower 16 bits are 0
Eric Christophere07aa432012-05-07 05:46:43 +00003204 case 'N': // immediate in the range of -65535 to -1 (inclusive)
Eric Christopher470578a2012-05-07 05:46:48 +00003205 case 'O': // signed 15 bit immediate (+- 16383)
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003206 case 'P': // immediate in the range of 65535 to 1 (inclusive)
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003207 if (isa<ConstantInt>(CallOperandVal))
3208 weight = CW_Constant;
3209 break;
Jack Carter0e149b02013-03-04 21:33:15 +00003210 case 'R':
3211 weight = CW_Memory;
3212 break;
John Thompsone8360b72010-10-29 17:29:13 +00003213 }
3214 return weight;
3215}
3216
Akira Hatanaka7473b472013-08-14 00:21:25 +00003217/// This is a helper function to parse a physical register string and split it
3218/// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag
3219/// that is returned indicates whether parsing was successful. The second flag
3220/// is true if the numeric part exists.
3221static std::pair<bool, bool>
Craig Topper6dc4a8bc2014-08-30 16:48:02 +00003222parsePhysicalReg(StringRef C, std::string &Prefix,
Akira Hatanaka7473b472013-08-14 00:21:25 +00003223 unsigned long long &Reg) {
3224 if (C.front() != '{' || C.back() != '}')
3225 return std::make_pair(false, false);
3226
3227 // Search for the first numeric character.
3228 StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1;
3229 I = std::find_if(B, E, std::ptr_fun(isdigit));
3230
3231 Prefix.assign(B, I - B);
3232
3233 // The second flag is set to false if no numeric characters were found.
3234 if (I == E)
3235 return std::make_pair(true, false);
3236
3237 // Parse the numeric characters.
3238 return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg),
3239 true);
3240}
3241
3242std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering::
Craig Topper6dc4a8bc2014-08-30 16:48:02 +00003243parseRegForInlineAsmConstraint(StringRef C, MVT VT) const {
Eric Christopherd9134482014-08-04 21:25:23 +00003244 const TargetRegisterInfo *TRI =
3245 getTargetMachine().getSubtargetImpl()->getRegisterInfo();
Akira Hatanaka7473b472013-08-14 00:21:25 +00003246 const TargetRegisterClass *RC;
3247 std::string Prefix;
3248 unsigned long long Reg;
3249
3250 std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg);
3251
3252 if (!R.first)
Craig Topper062a2ba2014-04-25 05:30:21 +00003253 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003254
3255 if ((Prefix == "hi" || Prefix == "lo")) { // Parse hi/lo.
3256 // No numeric characters follow "hi" or "lo".
3257 if (R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003258 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003259
3260 RC = TRI->getRegClass(Prefix == "hi" ?
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00003261 Mips::HI32RegClassID : Mips::LO32RegClassID);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003262 return std::make_pair(*(RC->begin()), RC);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003263 } else if (Prefix.compare(0, 4, "$msa") == 0) {
3264 // Parse $msa(ir|csr|access|save|modify|request|map|unmap)
3265
3266 // No numeric characters follow the name.
3267 if (R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003268 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003269
3270 Reg = StringSwitch<unsigned long long>(Prefix)
3271 .Case("$msair", Mips::MSAIR)
3272 .Case("$msacsr", Mips::MSACSR)
3273 .Case("$msaaccess", Mips::MSAAccess)
3274 .Case("$msasave", Mips::MSASave)
3275 .Case("$msamodify", Mips::MSAModify)
3276 .Case("$msarequest", Mips::MSARequest)
3277 .Case("$msamap", Mips::MSAMap)
3278 .Case("$msaunmap", Mips::MSAUnmap)
3279 .Default(0);
3280
3281 if (!Reg)
Craig Topper062a2ba2014-04-25 05:30:21 +00003282 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003283
3284 RC = TRI->getRegClass(Mips::MSACtrlRegClassID);
3285 return std::make_pair(Reg, RC);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003286 }
3287
3288 if (!R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003289 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003290
3291 if (Prefix == "$f") { // Parse $f0-$f31.
3292 // If the size of FP registers is 64-bit or Reg is an even number, select
3293 // the 64-bit register class. Otherwise, select the 32-bit register class.
3294 if (VT == MVT::Other)
Eric Christopher1c29a652014-07-18 22:55:25 +00003295 VT = (Subtarget.isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32;
Akira Hatanaka7473b472013-08-14 00:21:25 +00003296
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003297 RC = getRegClassFor(VT);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003298
3299 if (RC == &Mips::AFGR64RegClass) {
3300 assert(Reg % 2 == 0);
3301 Reg >>= 1;
3302 }
Daniel Sanders8b59af12013-11-12 12:56:01 +00003303 } else if (Prefix == "$fcc") // Parse $fcc0-$fcc7.
Akira Hatanaka7473b472013-08-14 00:21:25 +00003304 RC = TRI->getRegClass(Mips::FCCRegClassID);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003305 else if (Prefix == "$w") { // Parse $w0-$w31.
3306 RC = getRegClassFor((VT == MVT::Other) ? MVT::v16i8 : VT);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003307 } else { // Parse $0-$31.
3308 assert(Prefix == "$");
3309 RC = getRegClassFor((VT == MVT::Other) ? MVT::i32 : VT);
3310 }
3311
3312 assert(Reg < RC->getNumRegs());
3313 return std::make_pair(*(RC->begin() + Reg), RC);
3314}
3315
Eric Christophereaf77dc2011-06-29 19:33:04 +00003316/// Given a register class constraint, like 'r', if this corresponds directly
3317/// to an LLVM register class, return a register of 0 and the register class
3318/// pointer.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003319std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Chad Rosier295bd432013-06-22 18:37:38 +00003320getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003321{
3322 if (Constraint.size() == 1) {
3323 switch (Constraint[0]) {
Eric Christopher9519c082011-06-29 19:04:31 +00003324 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
3325 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003326 case 'r':
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003327 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
Eric Christopher1c29a652014-07-18 22:55:25 +00003328 if (Subtarget.inMips16Mode())
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003329 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003330 return std::make_pair(0U, &Mips::GPR32RegClass);
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003331 }
Eric Christopher1c29a652014-07-18 22:55:25 +00003332 if (VT == MVT::i64 && !Subtarget.isGP64bit())
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003333 return std::make_pair(0U, &Mips::GPR32RegClass);
Eric Christopher1c29a652014-07-18 22:55:25 +00003334 if (VT == MVT::i64 && Subtarget.isGP64bit())
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003335 return std::make_pair(0U, &Mips::GPR64RegClass);
Eric Christopher58daf042012-05-07 03:13:22 +00003336 // This will generate an error message
Craig Topper062a2ba2014-04-25 05:30:21 +00003337 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003338 case 'f': // FPU or MSA register
3339 if (VT == MVT::v16i8)
3340 return std::make_pair(0U, &Mips::MSA128BRegClass);
3341 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
3342 return std::make_pair(0U, &Mips::MSA128HRegClass);
3343 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
3344 return std::make_pair(0U, &Mips::MSA128WRegClass);
3345 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
3346 return std::make_pair(0U, &Mips::MSA128DRegClass);
3347 else if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00003348 return std::make_pair(0U, &Mips::FGR32RegClass);
Eric Christopher1c29a652014-07-18 22:55:25 +00003349 else if ((VT == MVT::f64) && (!Subtarget.isSingleFloat())) {
3350 if (Subtarget.isFP64bit())
Craig Topperc7242e02012-04-20 07:30:17 +00003351 return std::make_pair(0U, &Mips::FGR64RegClass);
3352 return std::make_pair(0U, &Mips::AFGR64RegClass);
Akira Hatanakac669d7a2012-01-04 02:45:01 +00003353 }
Eric Christophere3c494d2012-05-07 06:25:10 +00003354 break;
3355 case 'c': // register suitable for indirect jump
3356 if (VT == MVT::i32)
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003357 return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass);
Eric Christophere3c494d2012-05-07 06:25:10 +00003358 assert(VT == MVT::i64 && "Unexpected type.");
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003359 return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass);
Eric Christopher9c492e62012-05-07 06:25:15 +00003360 case 'l': // register suitable for indirect jump
3361 if (VT == MVT::i32)
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00003362 return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass);
3363 return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass);
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003364 case 'x': // register suitable for indirect jump
3365 // Fixme: Not triggering the use of both hi and low
3366 // This will generate an error message
Craig Topper062a2ba2014-04-25 05:30:21 +00003367 return std::make_pair(0U, nullptr);
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003368 }
3369 }
Akira Hatanaka7473b472013-08-14 00:21:25 +00003370
3371 std::pair<unsigned, const TargetRegisterClass *> R;
3372 R = parseRegForInlineAsmConstraint(Constraint, VT);
3373
3374 if (R.second)
3375 return R;
3376
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003377 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3378}
3379
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003380/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3381/// vector. If it is invalid, don't add anything to Ops.
3382void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3383 std::string &Constraint,
3384 std::vector<SDValue>&Ops,
3385 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +00003386 SDValue Result;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003387
3388 // Only support length 1 constraints for now.
3389 if (Constraint.length() > 1) return;
3390
3391 char ConstraintLetter = Constraint[0];
3392 switch (ConstraintLetter) {
3393 default: break; // This will fall through to the generic implementation
3394 case 'I': // Signed 16 bit constant
3395 // If this fails, the parent routine will give an error
3396 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3397 EVT Type = Op.getValueType();
3398 int64_t Val = C->getSExtValue();
3399 if (isInt<16>(Val)) {
3400 Result = DAG.getTargetConstant(Val, Type);
3401 break;
3402 }
3403 }
3404 return;
Eric Christopher7201e1b2012-05-07 03:13:42 +00003405 case 'J': // integer zero
3406 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3407 EVT Type = Op.getValueType();
3408 int64_t Val = C->getZExtValue();
3409 if (Val == 0) {
3410 Result = DAG.getTargetConstant(0, Type);
3411 break;
3412 }
3413 }
3414 return;
Eric Christopher3ff88a02012-05-07 05:46:29 +00003415 case 'K': // unsigned 16 bit immediate
3416 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3417 EVT Type = Op.getValueType();
3418 uint64_t Val = (uint64_t)C->getZExtValue();
3419 if (isUInt<16>(Val)) {
3420 Result = DAG.getTargetConstant(Val, Type);
3421 break;
3422 }
3423 }
3424 return;
Eric Christopher1109b342012-05-07 05:46:37 +00003425 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3426 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3427 EVT Type = Op.getValueType();
3428 int64_t Val = C->getSExtValue();
3429 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
3430 Result = DAG.getTargetConstant(Val, Type);
3431 break;
3432 }
3433 }
3434 return;
Eric Christophere07aa432012-05-07 05:46:43 +00003435 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3436 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3437 EVT Type = Op.getValueType();
3438 int64_t Val = C->getSExtValue();
3439 if ((Val >= -65535) && (Val <= -1)) {
3440 Result = DAG.getTargetConstant(Val, Type);
3441 break;
3442 }
3443 }
3444 return;
Eric Christopher470578a2012-05-07 05:46:48 +00003445 case 'O': // signed 15 bit immediate
3446 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3447 EVT Type = Op.getValueType();
3448 int64_t Val = C->getSExtValue();
3449 if ((isInt<15>(Val))) {
3450 Result = DAG.getTargetConstant(Val, Type);
3451 break;
3452 }
3453 }
3454 return;
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003455 case 'P': // immediate in the range of 1 to 65535 (inclusive)
3456 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3457 EVT Type = Op.getValueType();
3458 int64_t Val = C->getSExtValue();
3459 if ((Val <= 65535) && (Val >= 1)) {
3460 Result = DAG.getTargetConstant(Val, Type);
3461 break;
3462 }
3463 }
3464 return;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003465 }
3466
3467 if (Result.getNode()) {
3468 Ops.push_back(Result);
3469 return;
3470 }
3471
3472 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3473}
3474
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003475bool MipsTargetLowering::isLegalAddressingMode(const AddrMode &AM,
3476 Type *Ty) const {
Akira Hatanakaef839192012-11-17 00:25:41 +00003477 // No global is ever allowed as a base.
3478 if (AM.BaseGV)
3479 return false;
3480
3481 switch (AM.Scale) {
3482 case 0: // "r+i" or just "i", depending on HasBaseReg.
3483 break;
3484 case 1:
3485 if (!AM.HasBaseReg) // allow "r+i".
3486 break;
3487 return false; // disallow "r+r" or "r+r+i".
3488 default:
3489 return false;
3490 }
3491
3492 return true;
3493}
3494
3495bool
Dan Gohman2fe6bee2008-10-18 02:06:02 +00003496MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3497 // The Mips target isn't yet aware of offsets.
3498 return false;
3499}
Evan Cheng16993aa2009-10-27 19:56:55 +00003500
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003501EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003502 unsigned SrcAlign,
3503 bool IsMemset, bool ZeroMemset,
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003504 bool MemcpyStrSrc,
3505 MachineFunction &MF) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003506 if (Subtarget.hasMips64())
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003507 return MVT::i64;
3508
3509 return MVT::i32;
3510}
3511
Evan Cheng83896a52009-10-28 01:43:28 +00003512bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
3513 if (VT != MVT::f32 && VT != MVT::f64)
3514 return false;
Bruno Cardoso Lopesb02a9df2011-01-18 19:41:41 +00003515 if (Imm.isNegZero())
3516 return false;
Evan Cheng16993aa2009-10-27 19:56:55 +00003517 return Imm.isZero();
3518}
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003519
3520unsigned MipsTargetLowering::getJumpTableEncoding() const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003521 if (Subtarget.isABI_N64())
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003522 return MachineJumpTableInfo::EK_GPRel64BlockAddress;
Jia Liuf54f60f2012-02-28 07:46:26 +00003523
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003524 return TargetLowering::getJumpTableEncoding();
3525}
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003526
Akira Hatanakae092f722013-03-05 22:54:59 +00003527/// This function returns true if CallSym is a long double emulation routine.
3528static bool isF128SoftLibCall(const char *CallSym) {
3529 const char *const LibCalls[] =
3530 {"__addtf3", "__divtf3", "__eqtf2", "__extenddftf2", "__extendsftf2",
3531 "__fixtfdi", "__fixtfsi", "__fixtfti", "__fixunstfdi", "__fixunstfsi",
3532 "__fixunstfti", "__floatditf", "__floatsitf", "__floattitf",
3533 "__floatunditf", "__floatunsitf", "__floatuntitf", "__getf2", "__gttf2",
3534 "__letf2", "__lttf2", "__multf3", "__netf2", "__powitf2", "__subtf3",
3535 "__trunctfdf2", "__trunctfsf2", "__unordtf2",
3536 "ceill", "copysignl", "cosl", "exp2l", "expl", "floorl", "fmal", "fmodl",
3537 "log10l", "log2l", "logl", "nearbyintl", "powl", "rintl", "sinl", "sqrtl",
3538 "truncl"};
3539
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003540 const char *const *End = LibCalls + array_lengthof(LibCalls);
Akira Hatanakae092f722013-03-05 22:54:59 +00003541
3542 // Check that LibCalls is sorted alphabetically.
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003543 MipsTargetLowering::LTStr Comp;
Akira Hatanakae092f722013-03-05 22:54:59 +00003544
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003545#ifndef NDEBUG
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003546 for (const char *const *I = LibCalls; I < End - 1; ++I)
Akira Hatanakae092f722013-03-05 22:54:59 +00003547 assert(Comp(*I, *(I + 1)));
3548#endif
3549
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003550 return std::binary_search(LibCalls, End, CallSym, Comp);
Akira Hatanakae092f722013-03-05 22:54:59 +00003551}
3552
Daniel Sandersf3fe49a2014-10-07 09:29:59 +00003553/// This function returns true if Ty is fp128, {f128} or i128 which was
3554/// originally a fp128.
Akira Hatanakae092f722013-03-05 22:54:59 +00003555static bool originalTypeIsF128(const Type *Ty, const SDNode *CallNode) {
3556 if (Ty->isFP128Ty())
3557 return true;
3558
Daniel Sandersf3fe49a2014-10-07 09:29:59 +00003559 if (Ty->isStructTy() && Ty->getStructNumElements() == 1 &&
3560 Ty->getStructElementType(0)->isFP128Ty())
3561 return true;
3562
Akira Hatanakae092f722013-03-05 22:54:59 +00003563 const ExternalSymbolSDNode *ES =
3564 dyn_cast_or_null<const ExternalSymbolSDNode>(CallNode);
3565
3566 // If the Ty is i128 and the function being called is a long double emulation
3567 // routine, then the original type is f128.
3568 return (ES && Ty->isIntegerTy(128) && isF128SoftLibCall(ES->getSymbol()));
3569}
3570
Reed Kotler783c7942013-05-10 22:25:39 +00003571MipsTargetLowering::MipsCC::SpecialCallingConvType
Daniel Sanders068eea22014-11-01 17:44:51 +00003572MipsTargetLowering::MipsCC::getSpecialCallingConv(const SDNode *Callee) const {
Reed Kotler783c7942013-05-10 22:25:39 +00003573 MipsCC::SpecialCallingConvType SpecialCallingConv =
Alp Toker98444342014-04-19 23:56:35 +00003574 MipsCC::NoSpecialCallingConv;
Eric Christopher1c29a652014-07-18 22:55:25 +00003575 if (Subtarget.inMips16HardFloat()) {
Daniel Sanders068eea22014-11-01 17:44:51 +00003576 if (const GlobalAddressSDNode *G =
3577 dyn_cast<const GlobalAddressSDNode>(Callee)) {
Reed Kotler783c7942013-05-10 22:25:39 +00003578 llvm::StringRef Sym = G->getGlobal()->getName();
3579 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
Reed Kotler3230e722013-12-12 02:41:11 +00003580 if (F && F->hasFnAttribute("__Mips16RetHelper")) {
Reed Kotler783c7942013-05-10 22:25:39 +00003581 SpecialCallingConv = MipsCC::Mips16RetHelperConv;
3582 }
3583 }
3584 }
3585 return SpecialCallingConv;
3586}
3587
Daniel Sanders068eea22014-11-01 17:44:51 +00003588MipsTargetLowering::MipsCC::MipsCC(CallingConv::ID CC,
3589 const MipsSubtarget &Subtarget_,
3590 CCState &Info)
Daniel Sanders853c2432014-11-01 18:13:52 +00003591 : CallConv(CC), Subtarget(Subtarget_) {
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003592 // Pre-allocate reserved argument area.
Daniel Sanders853c2432014-11-01 18:13:52 +00003593 Info.AllocateStack(reservedArgArea(), 1);
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003594}
3595
Daniel Sanders853c2432014-11-01 18:13:52 +00003596void MipsTargetLowering::MipsCC::analyzeCallOperands(
3597 const SmallVectorImpl<ISD::OutputArg> &Args, bool IsVarArg,
3598 bool IsSoftFloat, const SDNode *CallNode,
3599 std::vector<ArgListEntry> &FuncArgs, CCState &State) {
Daniel Sanders068eea22014-11-01 17:44:51 +00003600 MipsCC::SpecialCallingConvType SpecialCallingConv =
3601 getSpecialCallingConv(CallNode);
Akira Hatanaka5001be52013-02-15 21:45:11 +00003602 assert((CallConv != CallingConv::Fast || !IsVarArg) &&
3603 "CallingConv::Fast shouldn't be used for vararg functions.");
3604
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003605 unsigned NumOpnds = Args.size();
Daniel Sanders068eea22014-11-01 17:44:51 +00003606 llvm::CCAssignFn *FixedFn = CC_Mips_FixedArg;
3607 if (CallConv != CallingConv::Fast &&
3608 SpecialCallingConv == Mips16RetHelperConv)
3609 FixedFn = CC_Mips16RetHelper;
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003610
3611 for (unsigned I = 0; I != NumOpnds; ++I) {
3612 MVT ArgVT = Args[I].VT;
3613 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
3614 bool R;
3615
3616 if (ArgFlags.isByVal()) {
Daniel Sanders853c2432014-11-01 18:13:52 +00003617 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, State);
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003618 continue;
3619 }
3620
Akira Hatanaka5001be52013-02-15 21:45:11 +00003621 if (IsVarArg && !Args[I].IsFixed)
Daniel Sanders853c2432014-11-01 18:13:52 +00003622 R = CC_Mips_VarArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, State);
Daniel Sanderseac096082014-11-01 18:38:03 +00003623 else
3624 R = FixedFn(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, State);
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003625
3626 if (R) {
3627#ifndef NDEBUG
3628 dbgs() << "Call operand #" << I << " has unhandled type "
3629 << EVT(ArgVT).getEVTString();
3630#endif
Craig Toppere73658d2014-04-28 04:05:08 +00003631 llvm_unreachable(nullptr);
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003632 }
3633 }
3634}
3635
Daniel Sanders853c2432014-11-01 18:13:52 +00003636void MipsTargetLowering::MipsCC::analyzeFormalArguments(
3637 const SmallVectorImpl<ISD::InputArg> &Args, bool IsSoftFloat,
Daniel Sanderseac096082014-11-01 18:38:03 +00003638 CCState &State) {
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003639 unsigned NumArgs = Args.size();
3640
3641 for (unsigned I = 0; I != NumArgs; ++I) {
3642 MVT ArgVT = Args[I].VT;
3643 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
3644
3645 if (ArgFlags.isByVal()) {
Daniel Sanders853c2432014-11-01 18:13:52 +00003646 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, State);
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003647 continue;
3648 }
3649
Daniel Sanderseac096082014-11-01 18:38:03 +00003650 if (!CC_Mips_FixedArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, State))
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003651 continue;
3652
3653#ifndef NDEBUG
3654 dbgs() << "Formal Arg #" << I << " has unhandled type "
3655 << EVT(ArgVT).getEVTString();
3656#endif
Craig Toppere73658d2014-04-28 04:05:08 +00003657 llvm_unreachable(nullptr);
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003658 }
3659}
3660
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003661void MipsTargetLowering::MipsCC::handleByValArg(unsigned ValNo, MVT ValVT,
3662 MVT LocVT,
3663 CCValAssign::LocInfo LocInfo,
Daniel Sanders853c2432014-11-01 18:13:52 +00003664 ISD::ArgFlagsTy ArgFlags,
3665 CCState &State) {
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003666 assert(ArgFlags.getByValSize() && "Byval argument's size shouldn't be 0.");
3667
3668 struct ByValArgInfo ByVal;
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003669 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
3670 unsigned ByValSize =
3671 RoundUpToAlignment(ArgFlags.getByValSize(), RegSizeInBytes);
3672 unsigned Align = std::min(std::max(ArgFlags.getByValAlign(), RegSizeInBytes),
3673 RegSizeInBytes * 2);
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003674
Akira Hatanaka5001be52013-02-15 21:45:11 +00003675 if (useRegsForByval())
Daniel Sanders853c2432014-11-01 18:13:52 +00003676 allocateRegs(ByVal, ByValSize, Align, State);
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003677
3678 // Allocate space on caller's stack.
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003679 ByVal.Address =
Daniel Sanders853c2432014-11-01 18:13:52 +00003680 State.AllocateStack(ByValSize - RegSizeInBytes * ByVal.NumRegs, Align);
3681 State.addLoc(
3682 CCValAssign::getMem(ValNo, ValVT, ByVal.Address, LocVT, LocInfo));
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003683 ByValArgs.push_back(ByVal);
3684}
3685
Akira Hatanaka5001be52013-02-15 21:45:11 +00003686unsigned MipsTargetLowering::MipsCC::reservedArgArea() const {
Daniel Sanders4abcfe22014-09-09 10:46:48 +00003687 return (Subtarget.isABI_O32() && (CallConv != CallingConv::Fast)) ? 16 : 0;
Akira Hatanaka5001be52013-02-15 21:45:11 +00003688}
3689
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003690const ArrayRef<MCPhysReg> MipsTargetLowering::MipsCC::intArgRegs() const {
3691 if (Subtarget.isABI_O32())
3692 return makeArrayRef(O32IntRegs);
3693 return makeArrayRef(Mips64IntRegs);
Akira Hatanaka5001be52013-02-15 21:45:11 +00003694}
3695
Craig Topper840beec2014-04-04 05:16:06 +00003696const MCPhysReg *MipsTargetLowering::MipsCC::shadowRegs() const {
Daniel Sanders4abcfe22014-09-09 10:46:48 +00003697 return Subtarget.isABI_O32() ? O32IntRegs : Mips64DPRegs;
Akira Hatanaka5001be52013-02-15 21:45:11 +00003698}
3699
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003700void MipsTargetLowering::MipsCC::allocateRegs(ByValArgInfo &ByVal,
3701 unsigned ByValSize,
Daniel Sanders853c2432014-11-01 18:13:52 +00003702 unsigned Align, CCState &State) {
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003703 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003704 const ArrayRef<MCPhysReg> IntArgRegs = intArgRegs();
3705 const MCPhysReg *ShadowRegs = shadowRegs();
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003706 assert(!(ByValSize % RegSizeInBytes) && !(Align % RegSizeInBytes) &&
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003707 "Byval argument's size and alignment should be a multiple of"
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003708 "RegSizeInBytes.");
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003709
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003710 ByVal.FirstIdx =
Daniel Sanders853c2432014-11-01 18:13:52 +00003711 State.getFirstUnallocated(IntArgRegs.data(), IntArgRegs.size());
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003712
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003713 // If Align > RegSizeInBytes, the first arg register must be even.
3714 if ((Align > RegSizeInBytes) && (ByVal.FirstIdx % 2)) {
Daniel Sanders853c2432014-11-01 18:13:52 +00003715 State.AllocateReg(IntArgRegs[ByVal.FirstIdx], ShadowRegs[ByVal.FirstIdx]);
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003716 ++ByVal.FirstIdx;
3717 }
3718
3719 // Mark the registers allocated.
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003720 for (unsigned I = ByVal.FirstIdx; ByValSize && (I < IntArgRegs.size());
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003721 ByValSize -= RegSizeInBytes, ++I, ++ByVal.NumRegs)
Daniel Sanders853c2432014-11-01 18:13:52 +00003722 State.AllocateReg(IntArgRegs[I], ShadowRegs[I]);
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003723}
Akira Hatanaka25dad192012-10-27 00:10:18 +00003724
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003725MVT MipsTargetLowering::MipsCC::getRegVT(MVT VT, const Type *OrigTy,
3726 const SDNode *CallNode,
3727 bool IsSoftFloat) const {
Daniel Sanders4abcfe22014-09-09 10:46:48 +00003728 if (IsSoftFloat || Subtarget.isABI_O32())
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003729 return VT;
3730
3731 // Check if the original type was fp128.
Akira Hatanakae092f722013-03-05 22:54:59 +00003732 if (originalTypeIsF128(OrigTy, CallNode)) {
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003733 assert(VT == MVT::i64);
3734 return MVT::f64;
3735 }
3736
3737 return VT;
3738}
3739
Akira Hatanaka25dad192012-10-27 00:10:18 +00003740void MipsTargetLowering::
Andrew Trickef9de2a2013-05-25 02:42:55 +00003741copyByValRegs(SDValue Chain, SDLoc DL, std::vector<SDValue> &OutChains,
Akira Hatanaka25dad192012-10-27 00:10:18 +00003742 SelectionDAG &DAG, const ISD::ArgFlagsTy &Flags,
3743 SmallVectorImpl<SDValue> &InVals, const Argument *FuncArg,
3744 const MipsCC &CC, const ByValArgInfo &ByVal) const {
3745 MachineFunction &MF = DAG.getMachineFunction();
3746 MachineFrameInfo *MFI = MF.getFrameInfo();
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003747 unsigned GPRSizeInBytes = Subtarget.getGPRSizeInBytes();
3748 unsigned RegAreaSize = ByVal.NumRegs * GPRSizeInBytes;
Akira Hatanaka25dad192012-10-27 00:10:18 +00003749 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
3750 int FrameObjOffset;
3751
3752 if (RegAreaSize)
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003753 FrameObjOffset =
3754 (int)CC.reservedArgArea() -
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003755 (int)((CC.intArgRegs().size() - ByVal.FirstIdx) * GPRSizeInBytes);
Akira Hatanaka25dad192012-10-27 00:10:18 +00003756 else
3757 FrameObjOffset = ByVal.Address;
3758
3759 // Create frame object.
3760 EVT PtrTy = getPointerTy();
3761 int FI = MFI->CreateFixedObject(FrameObjSize, FrameObjOffset, true);
3762 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
3763 InVals.push_back(FIN);
3764
3765 if (!ByVal.NumRegs)
3766 return;
3767
3768 // Copy arg registers.
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003769 MVT RegTy = MVT::getIntegerVT(GPRSizeInBytes * 8);
Akira Hatanaka25dad192012-10-27 00:10:18 +00003770 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3771
3772 for (unsigned I = 0; I < ByVal.NumRegs; ++I) {
3773 unsigned ArgReg = CC.intArgRegs()[ByVal.FirstIdx + I];
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003774 unsigned VReg = addLiveIn(MF, ArgReg, RC);
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003775 unsigned Offset = I * GPRSizeInBytes;
Akira Hatanaka25dad192012-10-27 00:10:18 +00003776 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
3777 DAG.getConstant(Offset, PtrTy));
3778 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
3779 StorePtr, MachinePointerInfo(FuncArg, Offset),
3780 false, false, 0);
3781 OutChains.push_back(Store);
3782 }
3783}
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003784
3785// Copy byVal arg to registers and stack.
3786void MipsTargetLowering::
Andrew Trickef9de2a2013-05-25 02:42:55 +00003787passByValArg(SDValue Chain, SDLoc DL,
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00003788 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
Craig Topperb94011f2013-07-14 04:42:23 +00003789 SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr,
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003790 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
3791 const MipsCC &CC, const ByValArgInfo &ByVal,
3792 const ISD::ArgFlagsTy &Flags, bool isLittle) const {
Daniel Sandersac272632014-05-23 13:18:02 +00003793 unsigned ByValSizeInBytes = Flags.getByValSize();
3794 unsigned OffsetInBytes = 0; // From beginning of struct
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003795 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
Daniel Sandersac272632014-05-23 13:18:02 +00003796 unsigned Alignment = std::min(Flags.getByValAlign(), RegSizeInBytes);
3797 EVT PtrTy = getPointerTy(), RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003798
3799 if (ByVal.NumRegs) {
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003800 const ArrayRef<MCPhysReg> ArgRegs = CC.intArgRegs();
Daniel Sandersac272632014-05-23 13:18:02 +00003801 bool LeftoverBytes = (ByVal.NumRegs * RegSizeInBytes > ByValSizeInBytes);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003802 unsigned I = 0;
3803
3804 // Copy words to registers.
Daniel Sandersac272632014-05-23 13:18:02 +00003805 for (; I < ByVal.NumRegs - LeftoverBytes;
3806 ++I, OffsetInBytes += RegSizeInBytes) {
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003807 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Daniel Sandersac272632014-05-23 13:18:02 +00003808 DAG.getConstant(OffsetInBytes, PtrTy));
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003809 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
3810 MachinePointerInfo(), false, false, false,
3811 Alignment);
3812 MemOpChains.push_back(LoadVal.getValue(1));
3813 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3814 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
3815 }
3816
3817 // Return if the struct has been fully copied.
Daniel Sandersac272632014-05-23 13:18:02 +00003818 if (ByValSizeInBytes == OffsetInBytes)
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003819 return;
3820
3821 // Copy the remainder of the byval argument with sub-word loads and shifts.
3822 if (LeftoverBytes) {
Daniel Sandersac272632014-05-23 13:18:02 +00003823 assert((ByValSizeInBytes > OffsetInBytes) &&
3824 (ByValSizeInBytes < OffsetInBytes + RegSizeInBytes) &&
3825 "Size of the remainder should be smaller than RegSizeInBytes.");
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003826 SDValue Val;
3827
Daniel Sandersac272632014-05-23 13:18:02 +00003828 for (unsigned LoadSizeInBytes = RegSizeInBytes / 2, TotalBytesLoaded = 0;
3829 OffsetInBytes < ByValSizeInBytes; LoadSizeInBytes /= 2) {
3830 unsigned RemainingSizeInBytes = ByValSizeInBytes - OffsetInBytes;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003831
Daniel Sandersac272632014-05-23 13:18:02 +00003832 if (RemainingSizeInBytes < LoadSizeInBytes)
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003833 continue;
3834
3835 // Load subword.
3836 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Daniel Sandersac272632014-05-23 13:18:02 +00003837 DAG.getConstant(OffsetInBytes, PtrTy));
3838 SDValue LoadVal = DAG.getExtLoad(
3839 ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr, MachinePointerInfo(),
Louis Gerbarg67474e32014-07-31 21:45:05 +00003840 MVT::getIntegerVT(LoadSizeInBytes * 8), false, false, false,
3841 Alignment);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003842 MemOpChains.push_back(LoadVal.getValue(1));
3843
3844 // Shift the loaded value.
3845 unsigned Shamt;
3846
3847 if (isLittle)
Daniel Sandersac272632014-05-23 13:18:02 +00003848 Shamt = TotalBytesLoaded * 8;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003849 else
Daniel Sandersac272632014-05-23 13:18:02 +00003850 Shamt = (RegSizeInBytes - (TotalBytesLoaded + LoadSizeInBytes)) * 8;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003851
3852 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
3853 DAG.getConstant(Shamt, MVT::i32));
3854
3855 if (Val.getNode())
3856 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
3857 else
3858 Val = Shift;
3859
Daniel Sandersac272632014-05-23 13:18:02 +00003860 OffsetInBytes += LoadSizeInBytes;
3861 TotalBytesLoaded += LoadSizeInBytes;
3862 Alignment = std::min(Alignment, LoadSizeInBytes);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003863 }
3864
3865 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3866 RegsToPass.push_back(std::make_pair(ArgReg, Val));
3867 return;
3868 }
3869 }
3870
3871 // Copy remainder of byval arg to it with memcpy.
Daniel Sandersac272632014-05-23 13:18:02 +00003872 unsigned MemCpySize = ByValSizeInBytes - OffsetInBytes;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003873 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Daniel Sandersac272632014-05-23 13:18:02 +00003874 DAG.getConstant(OffsetInBytes, PtrTy));
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003875 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
3876 DAG.getIntPtrConstant(ByVal.Address));
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003877 Chain = DAG.getMemcpy(Chain, DL, Dst, Src, DAG.getConstant(MemCpySize, PtrTy),
3878 Alignment, /*isVolatile=*/false, /*AlwaysInline=*/false,
Nick Lewyckyaad475b2014-04-15 07:22:52 +00003879 MachinePointerInfo(), MachinePointerInfo());
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003880 MemOpChains.push_back(Chain);
3881}
Akira Hatanaka2a134022012-10-27 00:21:13 +00003882
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003883void MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
3884 const MipsCC &CC, SDValue Chain,
Daniel Sanders853c2432014-11-01 18:13:52 +00003885 SDLoc DL, SelectionDAG &DAG,
3886 CCState &State) const {
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003887 const ArrayRef<MCPhysReg> ArgRegs = CC.intArgRegs();
Daniel Sanders853c2432014-11-01 18:13:52 +00003888 unsigned Idx = State.getFirstUnallocated(ArgRegs.data(), ArgRegs.size());
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003889 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
3890 MVT RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003891 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3892 MachineFunction &MF = DAG.getMachineFunction();
3893 MachineFrameInfo *MFI = MF.getFrameInfo();
3894 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3895
3896 // Offset of the first variable argument from stack pointer.
3897 int VaArgOffset;
3898
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003899 if (ArgRegs.size() == Idx)
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003900 VaArgOffset =
Daniel Sanders853c2432014-11-01 18:13:52 +00003901 RoundUpToAlignment(State.getNextStackOffset(), RegSizeInBytes);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003902 else
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003903 VaArgOffset = (int)CC.reservedArgArea() -
3904 (int)(RegSizeInBytes * (ArgRegs.size() - Idx));
Akira Hatanaka2a134022012-10-27 00:21:13 +00003905
3906 // Record the frame index of the first variable argument
3907 // which is a value necessary to VASTART.
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003908 int FI = MFI->CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003909 MipsFI->setVarArgsFrameIndex(FI);
3910
3911 // Copy the integer registers that have not been used for argument passing
3912 // to the argument register save area. For O32, the save area is allocated
3913 // in the caller's stack frame, while for N32/64, it is allocated in the
3914 // callee's stack frame.
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003915 for (unsigned I = Idx; I < ArgRegs.size();
3916 ++I, VaArgOffset += RegSizeInBytes) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003917 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003918 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003919 FI = MFI->CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003920 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
3921 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
3922 MachinePointerInfo(), false, false, 0);
Eric Christopher1c29a652014-07-18 22:55:25 +00003923 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(
3924 (Value *)nullptr);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003925 OutChains.push_back(Store);
3926 }
3927}