blob: d25f5637f57cec0498d21538c3e3826d676a3b59 [file] [log] [blame]
Jia Liuf54f60f2012-02-28 07:46:26 +00001//===-- MipsISelLowering.cpp - Mips DAG Lowering Implementation -----------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00007//
Akira Hatanakae2489122011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00009//
10// This file defines the interfaces that Mips uses to lower LLVM code into a
11// selection DAG.
12//
Akira Hatanakae2489122011-04-15 21:51:11 +000013//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000014#include "MipsISelLowering.h"
Craig Topperb25fda92012-03-17 18:46:09 +000015#include "InstPrinter/MipsInstPrinter.h"
16#include "MCTargetDesc/MipsBaseInfo.h"
Daniel Sanders0456c152014-11-07 14:24:31 +000017#include "MipsCCState.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "MipsMachineFunction.h"
19#include "MipsSubtarget.h"
20#include "MipsTargetMachine.h"
21#include "MipsTargetObjectFile.h"
Akira Hatanaka90131ac2012-10-19 21:47:33 +000022#include "llvm/ADT/Statistic.h"
Daniel Sanders8b59af12013-11-12 12:56:01 +000023#include "llvm/ADT/StringSwitch.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000024#include "llvm/CodeGen/CallingConvLower.h"
25#include "llvm/CodeGen/MachineFrameInfo.h"
26#include "llvm/CodeGen/MachineFunction.h"
27#include "llvm/CodeGen/MachineInstrBuilder.h"
Eric Christopher79cc1e32014-09-02 22:28:02 +000028#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000029#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000030#include "llvm/CodeGen/SelectionDAGISel.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000031#include "llvm/CodeGen/ValueTypes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000032#include "llvm/IR/CallingConv.h"
33#include "llvm/IR/DerivedTypes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000034#include "llvm/IR/GlobalVariable.h"
Akira Hatanaka90131ac2012-10-19 21:47:33 +000035#include "llvm/Support/CommandLine.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000036#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000037#include "llvm/Support/ErrorHandling.h"
NAKAMURA Takumie30303f2012-04-21 15:31:45 +000038#include "llvm/Support/raw_ostream.h"
Akira Hatanaka7473b472013-08-14 00:21:25 +000039#include <cctype>
NAKAMURA Takumie30303f2012-04-21 15:31:45 +000040
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000041using namespace llvm;
42
Chandler Carruth84e68b22014-04-22 02:41:26 +000043#define DEBUG_TYPE "mips-lower"
44
Akira Hatanaka90131ac2012-10-19 21:47:33 +000045STATISTIC(NumTailCalls, "Number of tail calls");
46
47static cl::opt<bool>
Akira Hatanaka59f299f2012-11-21 20:21:11 +000048LargeGOT("mxgot", cl::Hidden,
49 cl::desc("MIPS: Enable GOT larger than 64k."), cl::init(false));
50
Akira Hatanaka1cb02422013-05-20 18:07:43 +000051static cl::opt<bool>
Akira Hatanakabe76cd02013-05-21 17:17:59 +000052NoZeroDivCheck("mno-check-zero-division", cl::Hidden,
Akira Hatanaka1cb02422013-05-20 18:07:43 +000053 cl::desc("MIPS: Don't trap on integer division by zero."),
54 cl::init(false));
55
Reed Kotler720c5ca2014-04-17 22:15:34 +000056cl::opt<bool>
57EnableMipsFastISel("mips-fast-isel", cl::Hidden,
58 cl::desc("Allow mips-fast-isel to be used"),
59 cl::init(false));
60
Craig Topper840beec2014-04-04 05:16:06 +000061static const MCPhysReg Mips64DPRegs[8] = {
Akira Hatanakaac8c6692012-10-27 00:29:43 +000062 Mips::D12_64, Mips::D13_64, Mips::D14_64, Mips::D15_64,
63 Mips::D16_64, Mips::D17_64, Mips::D18_64, Mips::D19_64
64};
65
Jia Liuf54f60f2012-02-28 07:46:26 +000066// If I is a shifted mask, set the size (Size) and the first bit of the
Akira Hatanaka73d78b72011-08-18 20:07:42 +000067// mask (Pos), and return true.
Jia Liuf54f60f2012-02-28 07:46:26 +000068// For example, if I is 0x003ff800, (Pos, Size) = (11, 11).
Akira Hatanaka0bb60d892013-03-12 00:16:36 +000069static bool isShiftedMask(uint64_t I, uint64_t &Pos, uint64_t &Size) {
Akira Hatanaka20cee2e2011-12-05 21:26:34 +000070 if (!isShiftedMask_64(I))
Akira Hatanaka4c0a7122013-10-07 19:33:02 +000071 return false;
Akira Hatanaka5360f882011-08-17 02:05:42 +000072
Akira Hatanaka20cee2e2011-12-05 21:26:34 +000073 Size = CountPopulation_64(I);
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +000074 Pos = countTrailingZeros(I);
Akira Hatanaka73d78b72011-08-18 20:07:42 +000075 return true;
Akira Hatanaka5360f882011-08-17 02:05:42 +000076}
77
Akira Hatanaka96ca1822013-03-13 00:54:29 +000078SDValue MipsTargetLowering::getGlobalReg(SelectionDAG &DAG, EVT Ty) const {
Akira Hatanakab049aef2012-02-24 22:34:47 +000079 MipsFunctionInfo *FI = DAG.getMachineFunction().getInfo<MipsFunctionInfo>();
80 return DAG.getRegister(FI->getGlobalBaseReg(), Ty);
81}
82
Akira Hatanakad8f10ce2013-09-27 19:51:35 +000083SDValue MipsTargetLowering::getTargetNode(GlobalAddressSDNode *N, EVT Ty,
84 SelectionDAG &DAG,
Akira Hatanaka96ca1822013-03-13 00:54:29 +000085 unsigned Flag) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +000086 return DAG.getTargetGlobalAddress(N->getGlobal(), SDLoc(N), Ty, 0, Flag);
Akira Hatanakafd04ad42012-11-21 20:26:38 +000087}
88
Akira Hatanakad8f10ce2013-09-27 19:51:35 +000089SDValue MipsTargetLowering::getTargetNode(ExternalSymbolSDNode *N, EVT Ty,
90 SelectionDAG &DAG,
91 unsigned Flag) const {
92 return DAG.getTargetExternalSymbol(N->getSymbol(), Ty, Flag);
93}
94
95SDValue MipsTargetLowering::getTargetNode(BlockAddressSDNode *N, EVT Ty,
96 SelectionDAG &DAG,
97 unsigned Flag) const {
98 return DAG.getTargetBlockAddress(N->getBlockAddress(), Ty, 0, Flag);
99}
100
101SDValue MipsTargetLowering::getTargetNode(JumpTableSDNode *N, EVT Ty,
102 SelectionDAG &DAG,
103 unsigned Flag) const {
104 return DAG.getTargetJumpTable(N->getIndex(), Ty, Flag);
105}
106
107SDValue MipsTargetLowering::getTargetNode(ConstantPoolSDNode *N, EVT Ty,
108 SelectionDAG &DAG,
109 unsigned Flag) const {
110 return DAG.getTargetConstantPool(N->getConstVal(), Ty, N->getAlignment(),
111 N->getOffset(), Flag);
Akira Hatanakafd04ad42012-11-21 20:26:38 +0000112}
113
Chris Lattner5e693ed2009-07-28 03:13:23 +0000114const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
115 switch (Opcode) {
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000116 case MipsISD::JmpLink: return "MipsISD::JmpLink";
Akira Hatanaka91318df2012-10-19 20:59:39 +0000117 case MipsISD::TailCall: return "MipsISD::TailCall";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000118 case MipsISD::Hi: return "MipsISD::Hi";
119 case MipsISD::Lo: return "MipsISD::Lo";
120 case MipsISD::GPRel: return "MipsISD::GPRel";
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +0000121 case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000122 case MipsISD::Ret: return "MipsISD::Ret";
Akira Hatanakac0b02062013-01-30 00:26:49 +0000123 case MipsISD::EH_RETURN: return "MipsISD::EH_RETURN";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000124 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
125 case MipsISD::FPCmp: return "MipsISD::FPCmp";
126 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
127 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000128 case MipsISD::TruncIntFP: return "MipsISD::TruncIntFP";
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000129 case MipsISD::MFHI: return "MipsISD::MFHI";
130 case MipsISD::MFLO: return "MipsISD::MFLO";
131 case MipsISD::MTLOHI: return "MipsISD::MTLOHI";
Akira Hatanaka28721bd2013-03-30 01:14:04 +0000132 case MipsISD::Mult: return "MipsISD::Mult";
133 case MipsISD::Multu: return "MipsISD::Multu";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000134 case MipsISD::MAdd: return "MipsISD::MAdd";
135 case MipsISD::MAddu: return "MipsISD::MAddu";
136 case MipsISD::MSub: return "MipsISD::MSub";
137 case MipsISD::MSubu: return "MipsISD::MSubu";
138 case MipsISD::DivRem: return "MipsISD::DivRem";
139 case MipsISD::DivRemU: return "MipsISD::DivRemU";
Akira Hatanaka28721bd2013-03-30 01:14:04 +0000140 case MipsISD::DivRem16: return "MipsISD::DivRem16";
141 case MipsISD::DivRemU16: return "MipsISD::DivRemU16";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000142 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
143 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
Akira Hatanakafaa88c02011-12-12 22:38:19 +0000144 case MipsISD::Wrapper: return "MipsISD::Wrapper";
Akira Hatanakaa4c09bc2011-07-19 23:30:50 +0000145 case MipsISD::Sync: return "MipsISD::Sync";
Akira Hatanaka5360f882011-08-17 02:05:42 +0000146 case MipsISD::Ext: return "MipsISD::Ext";
147 case MipsISD::Ins: return "MipsISD::Ins";
Akira Hatanakab9ebf8d2012-06-02 00:03:12 +0000148 case MipsISD::LWL: return "MipsISD::LWL";
149 case MipsISD::LWR: return "MipsISD::LWR";
150 case MipsISD::SWL: return "MipsISD::SWL";
151 case MipsISD::SWR: return "MipsISD::SWR";
152 case MipsISD::LDL: return "MipsISD::LDL";
153 case MipsISD::LDR: return "MipsISD::LDR";
154 case MipsISD::SDL: return "MipsISD::SDL";
155 case MipsISD::SDR: return "MipsISD::SDR";
Akira Hatanaka233ac532012-09-21 23:52:47 +0000156 case MipsISD::EXTP: return "MipsISD::EXTP";
157 case MipsISD::EXTPDP: return "MipsISD::EXTPDP";
158 case MipsISD::EXTR_S_H: return "MipsISD::EXTR_S_H";
159 case MipsISD::EXTR_W: return "MipsISD::EXTR_W";
160 case MipsISD::EXTR_R_W: return "MipsISD::EXTR_R_W";
161 case MipsISD::EXTR_RS_W: return "MipsISD::EXTR_RS_W";
162 case MipsISD::SHILO: return "MipsISD::SHILO";
163 case MipsISD::MTHLIP: return "MipsISD::MTHLIP";
164 case MipsISD::MULT: return "MipsISD::MULT";
165 case MipsISD::MULTU: return "MipsISD::MULTU";
Jia Liu434874d2013-03-04 01:06:54 +0000166 case MipsISD::MADD_DSP: return "MipsISD::MADD_DSP";
Akira Hatanaka233ac532012-09-21 23:52:47 +0000167 case MipsISD::MADDU_DSP: return "MipsISD::MADDU_DSP";
168 case MipsISD::MSUB_DSP: return "MipsISD::MSUB_DSP";
169 case MipsISD::MSUBU_DSP: return "MipsISD::MSUBU_DSP";
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000170 case MipsISD::SHLL_DSP: return "MipsISD::SHLL_DSP";
171 case MipsISD::SHRA_DSP: return "MipsISD::SHRA_DSP";
172 case MipsISD::SHRL_DSP: return "MipsISD::SHRL_DSP";
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000173 case MipsISD::SETCC_DSP: return "MipsISD::SETCC_DSP";
174 case MipsISD::SELECT_CC_DSP: return "MipsISD::SELECT_CC_DSP";
Daniel Sandersce09d072013-08-28 12:14:50 +0000175 case MipsISD::VALL_ZERO: return "MipsISD::VALL_ZERO";
176 case MipsISD::VANY_ZERO: return "MipsISD::VANY_ZERO";
177 case MipsISD::VALL_NONZERO: return "MipsISD::VALL_NONZERO";
178 case MipsISD::VANY_NONZERO: return "MipsISD::VANY_NONZERO";
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000179 case MipsISD::VCEQ: return "MipsISD::VCEQ";
180 case MipsISD::VCLE_S: return "MipsISD::VCLE_S";
181 case MipsISD::VCLE_U: return "MipsISD::VCLE_U";
182 case MipsISD::VCLT_S: return "MipsISD::VCLT_S";
183 case MipsISD::VCLT_U: return "MipsISD::VCLT_U";
Daniel Sanders3ce56622013-09-24 12:18:31 +0000184 case MipsISD::VSMAX: return "MipsISD::VSMAX";
185 case MipsISD::VSMIN: return "MipsISD::VSMIN";
186 case MipsISD::VUMAX: return "MipsISD::VUMAX";
187 case MipsISD::VUMIN: return "MipsISD::VUMIN";
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000188 case MipsISD::VEXTRACT_SEXT_ELT: return "MipsISD::VEXTRACT_SEXT_ELT";
189 case MipsISD::VEXTRACT_ZEXT_ELT: return "MipsISD::VEXTRACT_ZEXT_ELT";
Daniel Sandersf7456c72013-09-23 13:22:24 +0000190 case MipsISD::VNOR: return "MipsISD::VNOR";
Daniel Sanderse5087042013-09-24 14:02:15 +0000191 case MipsISD::VSHF: return "MipsISD::VSHF";
Daniel Sanders26307182013-09-24 14:20:00 +0000192 case MipsISD::SHF: return "MipsISD::SHF";
Daniel Sanders2ed228b2013-09-24 14:36:12 +0000193 case MipsISD::ILVEV: return "MipsISD::ILVEV";
194 case MipsISD::ILVOD: return "MipsISD::ILVOD";
195 case MipsISD::ILVL: return "MipsISD::ILVL";
196 case MipsISD::ILVR: return "MipsISD::ILVR";
Daniel Sandersfae5f2a2013-09-24 14:53:25 +0000197 case MipsISD::PCKEV: return "MipsISD::PCKEV";
198 case MipsISD::PCKOD: return "MipsISD::PCKOD";
Daniel Sandersb50ccf82014-04-01 10:35:28 +0000199 case MipsISD::INSVE: return "MipsISD::INSVE";
Craig Topper062a2ba2014-04-25 05:30:21 +0000200 default: return nullptr;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000201 }
202}
203
Eric Christopherb1526602014-09-19 23:30:42 +0000204MipsTargetLowering::MipsTargetLowering(const MipsTargetMachine &TM,
Eric Christopher8924d272014-07-18 23:25:04 +0000205 const MipsSubtarget &STI)
Aditya Nandakumar30531552014-11-13 21:29:21 +0000206 : TargetLowering(TM), Subtarget(STI) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000207 // Mips does not have i1 type, so use i32 for
Wesley Peck527da1b2010-11-23 03:31:01 +0000208 // setcc operations results (slt, sgt, ...).
Duncan Sands8d6e2e12008-11-23 15:47:28 +0000209 setBooleanContents(ZeroOrOneBooleanContent);
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000210 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Daniel Sanderscbd44c52014-07-10 10:18:12 +0000211 // The cmp.cond.fmt instruction in MIPS32r6/MIPS64r6 uses 0 and -1 like MSA
212 // does. Integer booleans still use 0 and 1.
Eric Christopher1c29a652014-07-18 22:55:25 +0000213 if (Subtarget.hasMips32r6())
Daniel Sanderscbd44c52014-07-10 10:18:12 +0000214 setBooleanContents(ZeroOrOneBooleanContent,
215 ZeroOrNegativeOneBooleanContent);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000216
Wesley Peck527da1b2010-11-23 03:31:01 +0000217 // Load extented operations for i1 types must be promoted
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000218 for (MVT VT : MVT::integer_valuetypes()) {
219 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
220 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
221 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
222 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000223
Eli Friedman1fa07e12009-07-17 04:07:24 +0000224 // MIPS doesn't have extending float->double load/store
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000225 for (MVT VT : MVT::fp_valuetypes())
226 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000227 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman39d6faa2009-07-17 02:28:12 +0000228
Wesley Peck527da1b2010-11-23 03:31:01 +0000229 // Used by legalize types to correctly generate the setcc result.
230 // Without this, every float setcc comes with a AND/OR with the result,
231 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +0000232 // which is used implicitly by brcond and select operations.
Owen Anderson9f944592009-08-11 20:47:22 +0000233 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +0000234
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000235 // Mips Custom Operations
Akira Hatanaka0f693a82013-03-06 21:32:03 +0000236 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000237 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000238 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000239 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
240 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
241 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
242 setOperationAction(ISD::SELECT, MVT::f32, Custom);
243 setOperationAction(ISD::SELECT, MVT::f64, Custom);
244 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Akira Hatanaka24cf4e32012-07-11 19:32:27 +0000245 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
246 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Akira Hatanakab7f78592012-03-09 23:46:03 +0000247 setOperationAction(ISD::SETCC, MVT::f32, Custom);
248 setOperationAction(ISD::SETCC, MVT::f64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000249 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000250 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
251 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000252 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000253
Eric Christopher1c29a652014-07-18 22:55:25 +0000254 if (Subtarget.isGP64bit()) {
Akira Hatanakada00aa82012-03-10 00:03:50 +0000255 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
256 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
257 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
258 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
259 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
260 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Akira Hatanaka019e5922012-06-02 00:04:42 +0000261 setOperationAction(ISD::LOAD, MVT::i64, Custom);
262 setOperationAction(ISD::STORE, MVT::i64, Custom);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000263 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000264 }
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +0000265
Eric Christopher1c29a652014-07-18 22:55:25 +0000266 if (!Subtarget.isGP64bit()) {
Akira Hatanaka0a8ab712012-05-09 00:55:21 +0000267 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
268 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
269 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
270 }
271
Akira Hatanaka28e02ec2012-11-07 19:10:58 +0000272 setOperationAction(ISD::ADD, MVT::i32, Custom);
Eric Christopher1c29a652014-07-18 22:55:25 +0000273 if (Subtarget.isGP64bit())
Akira Hatanaka28e02ec2012-11-07 19:10:58 +0000274 setOperationAction(ISD::ADD, MVT::i64, Custom);
275
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000276 setOperationAction(ISD::SDIV, MVT::i32, Expand);
277 setOperationAction(ISD::SREM, MVT::i32, Expand);
278 setOperationAction(ISD::UDIV, MVT::i32, Expand);
279 setOperationAction(ISD::UREM, MVT::i32, Expand);
Akira Hatanakab1538f92011-10-03 21:06:13 +0000280 setOperationAction(ISD::SDIV, MVT::i64, Expand);
281 setOperationAction(ISD::SREM, MVT::i64, Expand);
282 setOperationAction(ISD::UDIV, MVT::i64, Expand);
283 setOperationAction(ISD::UREM, MVT::i64, Expand);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000284
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000285 // Operations not directly supported by Mips.
Tom Stellardb1588fc2013-03-08 15:36:57 +0000286 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
287 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
288 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
289 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
Tom Stellard3787b122014-06-10 16:01:29 +0000290 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
291 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000292 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Akira Hatanaka79aed152011-12-20 23:40:56 +0000293 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000294 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Akira Hatanaka79aed152011-12-20 23:40:56 +0000295 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000296 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Eric Christopher1c29a652014-07-18 22:55:25 +0000297 if (Subtarget.hasCnMips()) {
Kai Nacke93fe5e82014-03-20 11:51:58 +0000298 setOperationAction(ISD::CTPOP, MVT::i32, Legal);
299 setOperationAction(ISD::CTPOP, MVT::i64, Legal);
300 } else {
301 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
302 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
303 }
Owen Anderson9f944592009-08-11 20:47:22 +0000304 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
Akira Hatanaka410ce9c2011-12-21 00:14:05 +0000305 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000306 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
307 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
308 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
309 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000310 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Akira Hatanaka7ba8a8d2011-09-30 18:51:46 +0000311 setOperationAction(ISD::ROTL, MVT::i64, Expand);
Akira Hatanaka33a25af2012-07-31 20:54:48 +0000312 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
313 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Bruno Cardoso Lopesd47180e2010-12-09 17:32:30 +0000314
Eric Christopher1c29a652014-07-18 22:55:25 +0000315 if (!Subtarget.hasMips32r2())
Bruno Cardoso Lopesd47180e2010-12-09 17:32:30 +0000316 setOperationAction(ISD::ROTR, MVT::i32, Expand);
317
Eric Christopher1c29a652014-07-18 22:55:25 +0000318 if (!Subtarget.hasMips64r2())
Akira Hatanaka7ba8a8d2011-09-30 18:51:46 +0000319 setOperationAction(ISD::ROTR, MVT::i64, Expand);
320
Owen Anderson9f944592009-08-11 20:47:22 +0000321 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes22b69db2011-03-04 18:54:14 +0000322 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000323 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes22b69db2011-03-04 18:54:14 +0000324 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000325 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
326 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000327 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
328 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanakadfb8cda2011-05-23 22:23:58 +0000329 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000330 setOperationAction(ISD::FLOG, MVT::f32, Expand);
331 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
332 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
333 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000334 setOperationAction(ISD::FMA, MVT::f32, Expand);
335 setOperationAction(ISD::FMA, MVT::f64, Expand);
Akira Hatanaka0603ad82012-03-29 18:43:11 +0000336 setOperationAction(ISD::FREM, MVT::f32, Expand);
337 setOperationAction(ISD::FREM, MVT::f64, Expand);
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000338
Akira Hatanakac0b02062013-01-30 00:26:49 +0000339 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
340
Daniel Sanders2b553d42014-08-01 09:17:39 +0000341 setOperationAction(ISD::VASTART, MVT::Other, Custom);
342 setOperationAction(ISD::VAARG, MVT::Other, Custom);
Bruno Cardoso Lopes048ffab2011-03-09 19:22:22 +0000343 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
344 setOperationAction(ISD::VAEND, MVT::Other, Expand);
345
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000346 // Use the default for now
Owen Anderson9f944592009-08-11 20:47:22 +0000347 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
348 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman26a48482011-07-27 22:21:52 +0000349
Jia Liuf54f60f2012-02-28 07:46:26 +0000350 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
351 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
352 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
353 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
Eli Friedman7dfa7912011-08-29 18:23:02 +0000354
Eli Friedman30a49e92011-08-03 21:06:02 +0000355 setInsertFencesForAtomic(true);
356
Eric Christopher1c29a652014-07-18 22:55:25 +0000357 if (!Subtarget.hasMips32r2()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000358 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
359 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000360 }
361
Daniel Sanders070fd1c2014-05-12 12:41:59 +0000362 // MIPS16 lacks MIPS32's clz and clo instructions.
Eric Christopher1c29a652014-07-18 22:55:25 +0000363 if (!Subtarget.hasMips32() || Subtarget.inMips16Mode())
Owen Anderson9f944592009-08-11 20:47:22 +0000364 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Eric Christopher1c29a652014-07-18 22:55:25 +0000365 if (!Subtarget.hasMips64())
Akira Hatanaka1d8efab2011-12-21 00:20:27 +0000366 setOperationAction(ISD::CTLZ, MVT::i64, Expand);
Bruno Cardoso Lopes93da7e62008-08-08 06:16:31 +0000367
Eric Christopher1c29a652014-07-18 22:55:25 +0000368 if (!Subtarget.hasMips32r2())
Owen Anderson9f944592009-08-11 20:47:22 +0000369 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Eric Christopher1c29a652014-07-18 22:55:25 +0000370 if (!Subtarget.hasMips64r2())
Akira Hatanaka4706ac92011-12-20 23:56:43 +0000371 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000372
Eric Christopher1c29a652014-07-18 22:55:25 +0000373 if (Subtarget.isGP64bit()) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000374 setLoadExtAction(ISD::SEXTLOAD, MVT::i64, MVT::i32, Custom);
375 setLoadExtAction(ISD::ZEXTLOAD, MVT::i64, MVT::i32, Custom);
376 setLoadExtAction(ISD::EXTLOAD, MVT::i64, MVT::i32, Custom);
Akira Hatanaka019e5922012-06-02 00:04:42 +0000377 setTruncStoreAction(MVT::i64, MVT::i32, Custom);
378 }
379
Akira Hatanakaa3d9ab92013-07-26 20:58:55 +0000380 setOperationAction(ISD::TRAP, MVT::Other, Legal);
381
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000382 setTargetDAGCombine(ISD::SDIVREM);
383 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka5e152182012-03-08 03:26:37 +0000384 setTargetDAGCombine(ISD::SELECT);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000385 setTargetDAGCombine(ISD::AND);
386 setTargetDAGCombine(ISD::OR);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000387 setTargetDAGCombine(ISD::ADD);
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000388
Eric Christopher1c29a652014-07-18 22:55:25 +0000389 setMinFunctionAlignment(Subtarget.isGP64bit() ? 3 : 2);
Eli Friedman2518f832011-05-06 20:34:06 +0000390
Daniel Sanders2b553d42014-08-01 09:17:39 +0000391 // The arguments on the stack are defined in terms of 4-byte slots on O32
392 // and 8-byte slots on N32/N64.
393 setMinStackArgumentAlignment(
394 (Subtarget.isABI_N32() || Subtarget.isABI_N64()) ? 8 : 4);
395
Eric Christopher1c29a652014-07-18 22:55:25 +0000396 setStackPointerRegisterToSaveRestore(Subtarget.isABI_N64() ? Mips::SP_64
397 : Mips::SP);
Akira Hatanakaaa560002011-05-26 18:59:03 +0000398
Eric Christopher1c29a652014-07-18 22:55:25 +0000399 setExceptionPointerRegister(Subtarget.isABI_N64() ? Mips::A0_64 : Mips::A0);
400 setExceptionSelectorRegister(Subtarget.isABI_N64() ? Mips::A1_64 : Mips::A1);
Akira Hatanaka1daf8c22012-06-13 19:33:32 +0000401
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000402 MaxStoresPerMemcpy = 16;
Zoran Jovanovicff9d5f32013-12-19 16:12:56 +0000403
Eric Christopher1c29a652014-07-18 22:55:25 +0000404 isMicroMips = Subtarget.inMicroMipsMode();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000405}
406
Eric Christopherb1526602014-09-19 23:30:42 +0000407const MipsTargetLowering *MipsTargetLowering::create(const MipsTargetMachine &TM,
Eric Christopher8924d272014-07-18 23:25:04 +0000408 const MipsSubtarget &STI) {
409 if (STI.inMips16Mode())
410 return llvm::createMips16TargetLowering(TM, STI);
Jia Liuf54f60f2012-02-28 07:46:26 +0000411
Eric Christopher8924d272014-07-18 23:25:04 +0000412 return llvm::createMipsSETargetLowering(TM, STI);
Akira Hatanaka2fcc1cf2011-08-12 21:30:06 +0000413}
414
Reed Kotler720c5ca2014-04-17 22:15:34 +0000415// Create a fast isel object.
416FastISel *
417MipsTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
418 const TargetLibraryInfo *libInfo) const {
419 if (!EnableMipsFastISel)
420 return TargetLowering::createFastISel(funcInfo, libInfo);
421 return Mips::createFastISel(funcInfo, libInfo);
422}
423
Matt Arsenault758659232013-05-18 00:21:46 +0000424EVT MipsTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Akira Hatanakab13b3332013-01-04 20:06:01 +0000425 if (!VT.isVector())
426 return MVT::i32;
427 return VT.changeVectorElementTypeToInteger();
Scott Michela6729e82008-03-10 15:42:14 +0000428}
429
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000430static SDValue performDivRemCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000431 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000432 const MipsSubtarget &Subtarget) {
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000433 if (DCI.isBeforeLegalizeOps())
434 return SDValue();
435
Akira Hatanakab1538f92011-10-03 21:06:13 +0000436 EVT Ty = N->getValueType(0);
Akira Hatanaka8002a3f2013-08-14 00:47:08 +0000437 unsigned LO = (Ty == MVT::i32) ? Mips::LO0 : Mips::LO0_64;
438 unsigned HI = (Ty == MVT::i32) ? Mips::HI0 : Mips::HI0_64;
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000439 unsigned Opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem16 :
440 MipsISD::DivRemU16;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000441 SDLoc DL(N);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000442
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000443 SDValue DivRem = DAG.getNode(Opc, DL, MVT::Glue,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000444 N->getOperand(0), N->getOperand(1));
445 SDValue InChain = DAG.getEntryNode();
446 SDValue InGlue = DivRem;
447
448 // insert MFLO
449 if (N->hasAnyUseOfValue(0)) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000450 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, DL, LO, Ty,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000451 InGlue);
452 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
453 InChain = CopyFromLo.getValue(1);
454 InGlue = CopyFromLo.getValue(2);
455 }
456
457 // insert MFHI
458 if (N->hasAnyUseOfValue(1)) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000459 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, DL,
Akira Hatanakab1538f92011-10-03 21:06:13 +0000460 HI, Ty, InGlue);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000461 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
462 }
463
464 return SDValue();
465}
466
Akira Hatanaka89af5892013-04-18 01:00:46 +0000467static Mips::CondCode condCodeToFCC(ISD::CondCode CC) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000468 switch (CC) {
469 default: llvm_unreachable("Unknown fp condition code!");
470 case ISD::SETEQ:
471 case ISD::SETOEQ: return Mips::FCOND_OEQ;
472 case ISD::SETUNE: return Mips::FCOND_UNE;
473 case ISD::SETLT:
474 case ISD::SETOLT: return Mips::FCOND_OLT;
475 case ISD::SETGT:
476 case ISD::SETOGT: return Mips::FCOND_OGT;
477 case ISD::SETLE:
478 case ISD::SETOLE: return Mips::FCOND_OLE;
479 case ISD::SETGE:
480 case ISD::SETOGE: return Mips::FCOND_OGE;
481 case ISD::SETULT: return Mips::FCOND_ULT;
482 case ISD::SETULE: return Mips::FCOND_ULE;
483 case ISD::SETUGT: return Mips::FCOND_UGT;
484 case ISD::SETUGE: return Mips::FCOND_UGE;
485 case ISD::SETUO: return Mips::FCOND_UN;
486 case ISD::SETO: return Mips::FCOND_OR;
487 case ISD::SETNE:
488 case ISD::SETONE: return Mips::FCOND_ONE;
489 case ISD::SETUEQ: return Mips::FCOND_UEQ;
490 }
491}
492
493
Akira Hatanakaf0ea5002013-03-30 01:16:38 +0000494/// This function returns true if the floating point conditional branches and
495/// conditional moves which use condition code CC should be inverted.
496static bool invertFPCondCodeUser(Mips::CondCode CC) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000497 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
498 return false;
499
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000500 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
501 "Illegal Condition Code");
Akira Hatanakaa5352702011-03-31 18:26:17 +0000502
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000503 return true;
Akira Hatanakaa5352702011-03-31 18:26:17 +0000504}
505
506// Creates and returns an FPCmp node from a setcc node.
507// Returns Op if setcc is not a floating point comparison.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000508static SDValue createFPCmp(SelectionDAG &DAG, const SDValue &Op) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000509 // must be a SETCC node
510 if (Op.getOpcode() != ISD::SETCC)
511 return Op;
512
513 SDValue LHS = Op.getOperand(0);
514
515 if (!LHS.getValueType().isFloatingPoint())
516 return Op;
517
518 SDValue RHS = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000519 SDLoc DL(Op);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000520
Akira Hatanakaaef55c82011-04-15 21:00:26 +0000521 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
522 // node if necessary.
Akira Hatanakaa5352702011-03-31 18:26:17 +0000523 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
524
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000525 return DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS,
Akira Hatanaka89af5892013-04-18 01:00:46 +0000526 DAG.getConstant(condCodeToFCC(CC), MVT::i32));
Akira Hatanakaa5352702011-03-31 18:26:17 +0000527}
528
529// Creates and returns a CMovFPT/F node.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000530static SDValue createCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000531 SDValue False, SDLoc DL) {
Akira Hatanakaf0ea5002013-03-30 01:16:38 +0000532 ConstantSDNode *CC = cast<ConstantSDNode>(Cond.getOperand(2));
533 bool invert = invertFPCondCodeUser((Mips::CondCode)CC->getSExtValue());
Akira Hatanaka8bce21c2013-07-26 20:51:20 +0000534 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000535
536 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
Akira Hatanaka8bce21c2013-07-26 20:51:20 +0000537 True.getValueType(), True, FCC0, False, Cond);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000538}
539
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000540static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000541 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000542 const MipsSubtarget &Subtarget) {
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000543 if (DCI.isBeforeLegalizeOps())
544 return SDValue();
545
546 SDValue SetCC = N->getOperand(0);
547
548 if ((SetCC.getOpcode() != ISD::SETCC) ||
549 !SetCC.getOperand(0).getValueType().isInteger())
550 return SDValue();
551
552 SDValue False = N->getOperand(2);
553 EVT FalseTy = False.getValueType();
554
555 if (!FalseTy.isInteger())
556 return SDValue();
557
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000558 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(False);
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000559
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000560 // If the RHS (False) is 0, we swap the order of the operands
561 // of ISD::SELECT (obviously also inverting the condition) so that we can
562 // take advantage of conditional moves using the $0 register.
563 // Example:
564 // return (a != 0) ? x : 0;
565 // load $reg, x
566 // movz $reg, $0, a
567 if (!FalseC)
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000568 return SDValue();
569
Andrew Trickef9de2a2013-05-25 02:42:55 +0000570 const SDLoc DL(N);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000571
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000572 if (!FalseC->getZExtValue()) {
573 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
574 SDValue True = N->getOperand(1);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000575
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000576 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
577 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
578
579 return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True);
580 }
581
Matheus Almeidaa6beac12013-12-05 12:07:05 +0000582 // If both operands are integer constants there's a possibility that we
583 // can do some interesting optimizations.
584 SDValue True = N->getOperand(1);
585 ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(True);
586
587 if (!TrueC || !True.getValueType().isInteger())
588 return SDValue();
589
590 // We'll also ignore MVT::i64 operands as this optimizations proves
591 // to be ineffective because of the required sign extensions as the result
592 // of a SETCC operator is always MVT::i32 for non-vector types.
593 if (True.getValueType() == MVT::i64)
594 return SDValue();
595
596 int64_t Diff = TrueC->getSExtValue() - FalseC->getSExtValue();
597
598 // 1) (a < x) ? y : y-1
599 // slti $reg1, a, x
600 // addiu $reg2, $reg1, y-1
601 if (Diff == 1)
602 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, False);
603
604 // 2) (a < x) ? y-1 : y
605 // slti $reg1, a, x
606 // xor $reg1, $reg1, 1
607 // addiu $reg2, $reg1, y-1
608 if (Diff == -1) {
609 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
610 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
611 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
612 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, True);
613 }
614
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000615 // Couldn't optimize.
616 return SDValue();
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000617}
618
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000619static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000620 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000621 const MipsSubtarget &Subtarget) {
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000622 // Pattern match EXT.
623 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
624 // => ext $dst, $src, size, pos
Eric Christopher1c29a652014-07-18 22:55:25 +0000625 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasExtractInsert())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000626 return SDValue();
627
628 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000629 unsigned ShiftRightOpc = ShiftRight.getOpcode();
630
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000631 // Op's first operand must be a shift right.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000632 if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL)
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000633 return SDValue();
634
635 // The second operand of the shift must be an immediate.
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000636 ConstantSDNode *CN;
637 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
638 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000639
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000640 uint64_t Pos = CN->getZExtValue();
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000641 uint64_t SMPos, SMSize;
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000642
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000643 // Op's second operand must be a shifted mask.
644 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000645 !isShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000646 return SDValue();
647
648 // Return if the shifted mask does not start at bit 0 or the sum of its size
649 // and Pos exceeds the word's size.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000650 EVT ValTy = N->getValueType(0);
651 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000652 return SDValue();
653
Andrew Trickef9de2a2013-05-25 02:42:55 +0000654 return DAG.getNode(MipsISD::Ext, SDLoc(N), ValTy,
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000655 ShiftRight.getOperand(0), DAG.getConstant(Pos, MVT::i32),
Akira Hatanakaeea541c2011-08-17 22:59:46 +0000656 DAG.getConstant(SMSize, MVT::i32));
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000657}
Jia Liuf54f60f2012-02-28 07:46:26 +0000658
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000659static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000660 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000661 const MipsSubtarget &Subtarget) {
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000662 // Pattern match INS.
663 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
Jia Liuf54f60f2012-02-28 07:46:26 +0000664 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000665 // => ins $dst, $src, size, pos, $src1
Eric Christopher1c29a652014-07-18 22:55:25 +0000666 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasExtractInsert())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000667 return SDValue();
668
669 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
670 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
671 ConstantSDNode *CN;
672
673 // See if Op's first operand matches (and $src1 , mask0).
674 if (And0.getOpcode() != ISD::AND)
675 return SDValue();
676
677 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000678 !isShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000679 return SDValue();
680
681 // See if Op's second operand matches (and (shl $src, pos), mask1).
682 if (And1.getOpcode() != ISD::AND)
683 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000684
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000685 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000686 !isShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000687 return SDValue();
688
689 // The shift masks must have the same position and size.
690 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
691 return SDValue();
692
693 SDValue Shl = And1.getOperand(0);
694 if (Shl.getOpcode() != ISD::SHL)
695 return SDValue();
696
697 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
698 return SDValue();
699
700 unsigned Shamt = CN->getZExtValue();
701
702 // Return if the shift amount and the first bit position of mask are not the
Jia Liuf54f60f2012-02-28 07:46:26 +0000703 // same.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000704 EVT ValTy = N->getValueType(0);
705 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000706 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000707
Andrew Trickef9de2a2013-05-25 02:42:55 +0000708 return DAG.getNode(MipsISD::Ins, SDLoc(N), ValTy, Shl.getOperand(0),
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000709 DAG.getConstant(SMPos0, MVT::i32),
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000710 DAG.getConstant(SMSize0, MVT::i32), And0.getOperand(0));
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000711}
Jia Liuf54f60f2012-02-28 07:46:26 +0000712
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000713static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000714 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000715 const MipsSubtarget &Subtarget) {
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000716 // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt))
717
718 if (DCI.isBeforeLegalizeOps())
719 return SDValue();
720
721 SDValue Add = N->getOperand(1);
722
723 if (Add.getOpcode() != ISD::ADD)
724 return SDValue();
725
726 SDValue Lo = Add.getOperand(1);
727
728 if ((Lo.getOpcode() != MipsISD::Lo) ||
729 (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable))
730 return SDValue();
731
732 EVT ValTy = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000733 SDLoc DL(N);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000734
735 SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, N->getOperand(0),
736 Add.getOperand(0));
737 return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo);
738}
739
Bruno Cardoso Lopes61a61e92011-02-10 18:05:10 +0000740SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000741 const {
742 SelectionDAG &DAG = DCI.DAG;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000743 unsigned Opc = N->getOpcode();
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000744
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000745 switch (Opc) {
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000746 default: break;
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000747 case ISD::SDIVREM:
748 case ISD::UDIVREM:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000749 return performDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000750 case ISD::SELECT:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000751 return performSELECTCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000752 case ISD::AND:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000753 return performANDCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000754 case ISD::OR:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000755 return performORCombine(N, DAG, DCI, Subtarget);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000756 case ISD::ADD:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000757 return performADDCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000758 }
759
760 return SDValue();
761}
762
Akira Hatanakafabb8cf2012-09-21 23:58:31 +0000763void
764MipsTargetLowering::LowerOperationWrapper(SDNode *N,
765 SmallVectorImpl<SDValue> &Results,
766 SelectionDAG &DAG) const {
767 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
768
769 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
770 Results.push_back(Res.getValue(I));
771}
772
773void
774MipsTargetLowering::ReplaceNodeResults(SDNode *N,
775 SmallVectorImpl<SDValue> &Results,
776 SelectionDAG &DAG) const {
Akira Hatanaka9da442f2013-04-30 21:17:07 +0000777 return LowerOperationWrapper(N, Results, DAG);
Akira Hatanakafabb8cf2012-09-21 23:58:31 +0000778}
779
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000780SDValue MipsTargetLowering::
Dan Gohman21cea8a2010-04-17 15:26:15 +0000781LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000782{
Wesley Peck527da1b2010-11-23 03:31:01 +0000783 switch (Op.getOpcode())
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000784 {
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000785 case ISD::BR_JT: return lowerBR_JT(Op, DAG);
786 case ISD::BRCOND: return lowerBRCOND(Op, DAG);
787 case ISD::ConstantPool: return lowerConstantPool(Op, DAG);
788 case ISD::GlobalAddress: return lowerGlobalAddress(Op, DAG);
789 case ISD::BlockAddress: return lowerBlockAddress(Op, DAG);
790 case ISD::GlobalTLSAddress: return lowerGlobalTLSAddress(Op, DAG);
791 case ISD::JumpTable: return lowerJumpTable(Op, DAG);
792 case ISD::SELECT: return lowerSELECT(Op, DAG);
793 case ISD::SELECT_CC: return lowerSELECT_CC(Op, DAG);
794 case ISD::SETCC: return lowerSETCC(Op, DAG);
795 case ISD::VASTART: return lowerVASTART(Op, DAG);
Daniel Sanders2b553d42014-08-01 09:17:39 +0000796 case ISD::VAARG: return lowerVAARG(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000797 case ISD::FCOPYSIGN: return lowerFCOPYSIGN(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000798 case ISD::FRAMEADDR: return lowerFRAMEADDR(Op, DAG);
799 case ISD::RETURNADDR: return lowerRETURNADDR(Op, DAG);
800 case ISD::EH_RETURN: return lowerEH_RETURN(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000801 case ISD::ATOMIC_FENCE: return lowerATOMIC_FENCE(Op, DAG);
802 case ISD::SHL_PARTS: return lowerShiftLeftParts(Op, DAG);
803 case ISD::SRA_PARTS: return lowerShiftRightParts(Op, DAG, true);
804 case ISD::SRL_PARTS: return lowerShiftRightParts(Op, DAG, false);
805 case ISD::LOAD: return lowerLOAD(Op, DAG);
806 case ISD::STORE: return lowerSTORE(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000807 case ISD::ADD: return lowerADD(Op, DAG);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000808 case ISD::FP_TO_SINT: return lowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000809 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000810 return SDValue();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000811}
812
Akira Hatanakae2489122011-04-15 21:51:11 +0000813//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000814// Lower helper functions
Akira Hatanakae2489122011-04-15 21:51:11 +0000815//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000816
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000817// addLiveIn - This helper function adds the specified physical register to the
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000818// MachineFunction as a live in value. It also creates a corresponding
819// virtual register for it.
820static unsigned
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000821addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000822{
Chris Lattnera10fff52007-12-31 04:13:23 +0000823 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
824 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000825 return VReg;
826}
827
Daniel Sanders308181e2014-06-12 10:44:10 +0000828static MachineBasicBlock *insertDivByZeroTrap(MachineInstr *MI,
829 MachineBasicBlock &MBB,
830 const TargetInstrInfo &TII,
831 bool Is64Bit) {
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000832 if (NoZeroDivCheck)
833 return &MBB;
834
835 // Insert instruction "teq $divisor_reg, $zero, 7".
836 MachineBasicBlock::iterator I(MI);
837 MachineInstrBuilder MIB;
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000838 MachineOperand &Divisor = MI->getOperand(2);
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000839 MIB = BuildMI(MBB, std::next(I), MI->getDebugLoc(), TII.get(Mips::TEQ))
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000840 .addReg(Divisor.getReg(), getKillRegState(Divisor.isKill()))
841 .addReg(Mips::ZERO).addImm(7);
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000842
843 // Use the 32-bit sub-register if this is a 64-bit division.
844 if (Is64Bit)
845 MIB->getOperand(0).setSubReg(Mips::sub_32);
846
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000847 // Clear Divisor's kill flag.
848 Divisor.setIsKill(false);
Daniel Sanders308181e2014-06-12 10:44:10 +0000849
850 // We would normally delete the original instruction here but in this case
851 // we only needed to inject an additional instruction rather than replace it.
852
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000853 return &MBB;
854}
855
Akira Hatanakae4bd0542012-09-27 02:15:57 +0000856MachineBasicBlock *
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +0000857MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +0000858 MachineBasicBlock *BB) const {
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +0000859 switch (MI->getOpcode()) {
Reed Kotler97ba5f22013-02-21 04:22:38 +0000860 default:
861 llvm_unreachable("Unexpected instr type to insert");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000862 case Mips::ATOMIC_LOAD_ADD_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000863 return emitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000864 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000865 return emitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000866 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000867 return emitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000868 case Mips::ATOMIC_LOAD_ADD_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000869 return emitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000870
871 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000872 return emitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000873 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000874 return emitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000875 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000876 return emitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000877 case Mips::ATOMIC_LOAD_AND_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000878 return emitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000879
880 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000881 return emitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000882 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000883 return emitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000884 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000885 return emitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000886 case Mips::ATOMIC_LOAD_OR_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000887 return emitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000888
889 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000890 return emitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000891 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000892 return emitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000893 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000894 return emitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000895 case Mips::ATOMIC_LOAD_XOR_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000896 return emitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000897
898 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000899 return emitAtomicBinaryPartword(MI, BB, 1, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000900 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000901 return emitAtomicBinaryPartword(MI, BB, 2, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000902 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000903 return emitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000904 case Mips::ATOMIC_LOAD_NAND_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000905 return emitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000906
907 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000908 return emitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000909 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000910 return emitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000911 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000912 return emitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000913 case Mips::ATOMIC_LOAD_SUB_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000914 return emitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000915
916 case Mips::ATOMIC_SWAP_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000917 return emitAtomicBinaryPartword(MI, BB, 1, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000918 case Mips::ATOMIC_SWAP_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000919 return emitAtomicBinaryPartword(MI, BB, 2, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000920 case Mips::ATOMIC_SWAP_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000921 return emitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000922 case Mips::ATOMIC_SWAP_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000923 return emitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000924
925 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000926 return emitAtomicCmpSwapPartword(MI, BB, 1);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000927 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000928 return emitAtomicCmpSwapPartword(MI, BB, 2);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000929 case Mips::ATOMIC_CMP_SWAP_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000930 return emitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000931 case Mips::ATOMIC_CMP_SWAP_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000932 return emitAtomicCmpSwap(MI, BB, 8);
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000933 case Mips::PseudoSDIV:
934 case Mips::PseudoUDIV:
Daniel Sanders308181e2014-06-12 10:44:10 +0000935 case Mips::DIV:
936 case Mips::DIVU:
937 case Mips::MOD:
938 case Mips::MODU:
Eric Christopherd9134482014-08-04 21:25:23 +0000939 return insertDivByZeroTrap(
940 MI, *BB, *getTargetMachine().getSubtargetImpl()->getInstrInfo(), false);
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000941 case Mips::PseudoDSDIV:
942 case Mips::PseudoDUDIV:
Daniel Sanders308181e2014-06-12 10:44:10 +0000943 case Mips::DDIV:
944 case Mips::DDIVU:
945 case Mips::DMOD:
946 case Mips::DMODU:
Eric Christopherd9134482014-08-04 21:25:23 +0000947 return insertDivByZeroTrap(
948 MI, *BB, *getTargetMachine().getSubtargetImpl()->getInstrInfo(), true);
Daniel Sanders0fa60412014-06-12 13:39:06 +0000949 case Mips::SEL_D:
950 return emitSEL_D(MI, BB);
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +0000951
952 case Mips::PseudoSELECT_I:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +0000953 case Mips::PseudoSELECT_I64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +0000954 case Mips::PseudoSELECT_S:
955 case Mips::PseudoSELECT_D32:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +0000956 case Mips::PseudoSELECT_D64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +0000957 return emitPseudoSELECT(MI, BB, false, Mips::BNE);
958 case Mips::PseudoSELECTFP_F_I:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +0000959 case Mips::PseudoSELECTFP_F_I64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +0000960 case Mips::PseudoSELECTFP_F_S:
961 case Mips::PseudoSELECTFP_F_D32:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +0000962 case Mips::PseudoSELECTFP_F_D64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +0000963 return emitPseudoSELECT(MI, BB, true, Mips::BC1F);
964 case Mips::PseudoSELECTFP_T_I:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +0000965 case Mips::PseudoSELECTFP_T_I64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +0000966 case Mips::PseudoSELECTFP_T_S:
967 case Mips::PseudoSELECTFP_T_D32:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +0000968 case Mips::PseudoSELECTFP_T_D64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +0000969 return emitPseudoSELECT(MI, BB, true, Mips::BC1T);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000970 }
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +0000971}
972
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000973// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
974// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
975MachineBasicBlock *
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000976MipsTargetLowering::emitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher0713a9d2011-06-08 23:55:35 +0000977 unsigned Size, unsigned BinOpcode,
Akira Hatanaka15506782011-06-07 18:58:42 +0000978 bool Nand) const {
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000979 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000980
981 MachineFunction *MF = BB->getParent();
982 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000983 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Eric Christopherd9134482014-08-04 21:25:23 +0000984 const TargetInstrInfo *TII =
985 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000986 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000987 unsigned LL, SC, AND, NOR, ZERO, BEQ;
988
989 if (Size == 4) {
Daniel Sanders6a803f62014-06-16 13:13:03 +0000990 if (isMicroMips) {
991 LL = Mips::LL_MM;
992 SC = Mips::SC_MM;
993 } else {
Daniel Sandersbdcfab12014-07-24 09:47:14 +0000994 LL = Subtarget.hasMips32r6() ? Mips::LL_R6 : Mips::LL;
995 SC = Subtarget.hasMips32r6() ? Mips::SC_R6 : Mips::SC;
Daniel Sanders6a803f62014-06-16 13:13:03 +0000996 }
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000997 AND = Mips::AND;
998 NOR = Mips::NOR;
999 ZERO = Mips::ZERO;
1000 BEQ = Mips::BEQ;
Daniel Sanders6a803f62014-06-16 13:13:03 +00001001 } else {
Daniel Sandersbdcfab12014-07-24 09:47:14 +00001002 LL = Subtarget.hasMips64r6() ? Mips::LLD_R6 : Mips::LLD;
1003 SC = Subtarget.hasMips64r6() ? Mips::SCD_R6 : Mips::SCD;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001004 AND = Mips::AND64;
1005 NOR = Mips::NOR64;
1006 ZERO = Mips::ZERO_64;
1007 BEQ = Mips::BEQ64;
1008 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001009
Akira Hatanaka0e019592011-07-19 20:11:17 +00001010 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001011 unsigned Ptr = MI->getOperand(1).getReg();
1012 unsigned Incr = MI->getOperand(2).getReg();
1013
Akira Hatanaka0e019592011-07-19 20:11:17 +00001014 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1015 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1016 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001017
1018 // insert new blocks after the current block
1019 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1020 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1021 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1022 MachineFunction::iterator It = BB;
1023 ++It;
1024 MF->insert(It, loopMBB);
1025 MF->insert(It, exitMBB);
1026
1027 // Transfer the remainder of BB and its successor edges to exitMBB.
1028 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001029 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001030 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1031
1032 // thisMBB:
1033 // ...
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001034 // fallthrough --> loopMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001035 BB->addSuccessor(loopMBB);
Akira Hatanaka08636b42011-07-19 17:09:53 +00001036 loopMBB->addSuccessor(loopMBB);
1037 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001038
1039 // loopMBB:
1040 // ll oldval, 0(ptr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001041 // <binop> storeval, oldval, incr
1042 // sc success, storeval, 0(ptr)
1043 // beq success, $0, loopMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001044 BB = loopMBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001045 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001046 if (Nand) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001047 // and andres, oldval, incr
1048 // nor storeval, $0, andres
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001049 BuildMI(BB, DL, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
1050 BuildMI(BB, DL, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001051 } else if (BinOpcode) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001052 // <binop> storeval, oldval, incr
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001053 BuildMI(BB, DL, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001054 } else {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001055 StoreVal = Incr;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001056 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001057 BuildMI(BB, DL, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
1058 BuildMI(BB, DL, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001059
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001060 MI->eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001061
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001062 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001063}
1064
Daniel Sanders6a803f62014-06-16 13:13:03 +00001065MachineBasicBlock *MipsTargetLowering::emitSignExtendToI32InReg(
1066 MachineInstr *MI, MachineBasicBlock *BB, unsigned Size, unsigned DstReg,
1067 unsigned SrcReg) const {
Eric Christopherd9134482014-08-04 21:25:23 +00001068 const TargetInstrInfo *TII =
1069 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Daniel Sanders6a803f62014-06-16 13:13:03 +00001070 DebugLoc DL = MI->getDebugLoc();
1071
Eric Christopher1c29a652014-07-18 22:55:25 +00001072 if (Subtarget.hasMips32r2() && Size == 1) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001073 BuildMI(BB, DL, TII->get(Mips::SEB), DstReg).addReg(SrcReg);
1074 return BB;
1075 }
1076
Eric Christopher1c29a652014-07-18 22:55:25 +00001077 if (Subtarget.hasMips32r2() && Size == 2) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001078 BuildMI(BB, DL, TII->get(Mips::SEH), DstReg).addReg(SrcReg);
1079 return BB;
1080 }
1081
1082 MachineFunction *MF = BB->getParent();
1083 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1084 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1085 unsigned ScrReg = RegInfo.createVirtualRegister(RC);
1086
1087 assert(Size < 32);
1088 int64_t ShiftImm = 32 - (Size * 8);
1089
1090 BuildMI(BB, DL, TII->get(Mips::SLL), ScrReg).addReg(SrcReg).addImm(ShiftImm);
1091 BuildMI(BB, DL, TII->get(Mips::SRA), DstReg).addReg(ScrReg).addImm(ShiftImm);
1092
1093 return BB;
1094}
1095
1096MachineBasicBlock *MipsTargetLowering::emitAtomicBinaryPartword(
1097 MachineInstr *MI, MachineBasicBlock *BB, unsigned Size, unsigned BinOpcode,
1098 bool Nand) const {
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001099 assert((Size == 1 || Size == 2) &&
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001100 "Unsupported size for EmitAtomicBinaryPartial.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001101
1102 MachineFunction *MF = BB->getParent();
1103 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1104 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
Eric Christopherd9134482014-08-04 21:25:23 +00001105 const TargetInstrInfo *TII =
1106 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001107 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001108
1109 unsigned Dest = MI->getOperand(0).getReg();
1110 unsigned Ptr = MI->getOperand(1).getReg();
1111 unsigned Incr = MI->getOperand(2).getReg();
1112
Akira Hatanaka0e019592011-07-19 20:11:17 +00001113 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1114 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001115 unsigned Mask = RegInfo.createVirtualRegister(RC);
1116 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001117 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1118 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001119 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001120 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1121 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1122 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1123 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1124 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka9663dd32011-07-19 20:56:53 +00001125 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001126 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1127 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1128 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001129 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001130
1131 // insert new blocks after the current block
1132 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1133 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001134 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001135 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1136 MachineFunction::iterator It = BB;
1137 ++It;
1138 MF->insert(It, loopMBB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001139 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001140 MF->insert(It, exitMBB);
1141
1142 // Transfer the remainder of BB and its successor edges to exitMBB.
1143 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001144 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001145 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1146
Akira Hatanaka08636b42011-07-19 17:09:53 +00001147 BB->addSuccessor(loopMBB);
1148 loopMBB->addSuccessor(loopMBB);
1149 loopMBB->addSuccessor(sinkMBB);
1150 sinkMBB->addSuccessor(exitMBB);
1151
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001152 // thisMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001153 // addiu masklsb2,$0,-4 # 0xfffffffc
1154 // and alignedaddr,ptr,masklsb2
1155 // andi ptrlsb2,ptr,3
1156 // sll shiftamt,ptrlsb2,3
1157 // ori maskupper,$0,255 # 0xff
1158 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001159 // nor mask2,$0,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001160 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001161
1162 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001163 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001164 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001165 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001166 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001167 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Eric Christopher1c29a652014-07-18 22:55:25 +00001168 if (Subtarget.isLittle()) {
Akira Hatanaka2bf97332013-05-31 03:25:44 +00001169 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1170 } else {
1171 unsigned Off = RegInfo.createVirtualRegister(RC);
1172 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1173 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1174 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1175 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001176 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001177 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001178 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001179 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001180 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001181 BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(Incr).addReg(ShiftAmt);
Bruno Cardoso Lopesf771a0f2011-05-31 20:25:26 +00001182
Akira Hatanaka27292632011-07-18 18:52:12 +00001183 // atomic.load.binop
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001184 // loopMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001185 // ll oldval,0(alignedaddr)
1186 // binop binopres,oldval,incr2
1187 // and newval,binopres,mask
1188 // and maskedoldval0,oldval,mask2
1189 // or storeval,maskedoldval0,newval
1190 // sc success,storeval,0(alignedaddr)
1191 // beq success,$0,loopMBB
1192
Akira Hatanaka27292632011-07-18 18:52:12 +00001193 // atomic.swap
1194 // loopMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001195 // ll oldval,0(alignedaddr)
Akira Hatanakae4503582011-07-19 18:14:26 +00001196 // and newval,incr2,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001197 // and maskedoldval0,oldval,mask2
1198 // or storeval,maskedoldval0,newval
1199 // sc success,storeval,0(alignedaddr)
1200 // beq success,$0,loopMBB
Akira Hatanaka27292632011-07-18 18:52:12 +00001201
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001202 BB = loopMBB;
Jozef Kolek2f27d572014-12-18 16:39:29 +00001203 unsigned LL = isMicroMips ? Mips::LL_MM : Mips::LL;
1204 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001205 if (Nand) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001206 // and andres, oldval, incr2
1207 // nor binopres, $0, andres
1208 // and newval, binopres, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001209 BuildMI(BB, DL, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1210 BuildMI(BB, DL, TII->get(Mips::NOR), BinOpRes)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001211 .addReg(Mips::ZERO).addReg(AndRes);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001212 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001213 } else if (BinOpcode) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001214 // <binop> binopres, oldval, incr2
1215 // and newval, binopres, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001216 BuildMI(BB, DL, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1217 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001218 } else { // atomic.swap
Akira Hatanaka0e019592011-07-19 20:11:17 +00001219 // and newval, incr2, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001220 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanakae4503582011-07-19 18:14:26 +00001221 }
Jia Liuf54f60f2012-02-28 07:46:26 +00001222
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001223 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001224 .addReg(OldVal).addReg(Mask2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001225 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka9663dd32011-07-19 20:56:53 +00001226 .addReg(MaskedOldVal0).addReg(NewVal);
Jozef Kolek2f27d572014-12-18 16:39:29 +00001227 unsigned SC = isMicroMips ? Mips::SC_MM : Mips::SC;
1228 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001229 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001230 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001231 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001232
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001233 // sinkMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001234 // and maskedoldval1,oldval,mask
1235 // srl srlres,maskedoldval1,shiftamt
Daniel Sanders6a803f62014-06-16 13:13:03 +00001236 // sign_extend dest,srlres
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001237 BB = sinkMBB;
Akira Hatanakae97bd812011-07-19 03:14:58 +00001238
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001239 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001240 .addReg(OldVal).addReg(Mask);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001241 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001242 .addReg(MaskedOldVal1).addReg(ShiftAmt);
Daniel Sanders6a803f62014-06-16 13:13:03 +00001243 BB = emitSignExtendToI32InReg(MI, BB, Size, Dest, SrlRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001244
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001245 MI->eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001246
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001247 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001248}
1249
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001250MachineBasicBlock * MipsTargetLowering::emitAtomicCmpSwap(MachineInstr *MI,
1251 MachineBasicBlock *BB,
1252 unsigned Size) const {
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001253 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001254
1255 MachineFunction *MF = BB->getParent();
1256 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001257 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Eric Christopherd9134482014-08-04 21:25:23 +00001258 const TargetInstrInfo *TII =
1259 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001260 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001261 unsigned LL, SC, ZERO, BNE, BEQ;
1262
1263 if (Size == 4) {
Zoran Jovanovicff9d5f32013-12-19 16:12:56 +00001264 LL = isMicroMips ? Mips::LL_MM : Mips::LL;
1265 SC = isMicroMips ? Mips::SC_MM : Mips::SC;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001266 ZERO = Mips::ZERO;
1267 BNE = Mips::BNE;
1268 BEQ = Mips::BEQ;
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001269 } else {
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001270 LL = Mips::LLD;
1271 SC = Mips::SCD;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001272 ZERO = Mips::ZERO_64;
1273 BNE = Mips::BNE64;
1274 BEQ = Mips::BEQ64;
1275 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001276
1277 unsigned Dest = MI->getOperand(0).getReg();
1278 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka0e019592011-07-19 20:11:17 +00001279 unsigned OldVal = MI->getOperand(2).getReg();
1280 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001281
Akira Hatanaka0e019592011-07-19 20:11:17 +00001282 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001283
1284 // insert new blocks after the current block
1285 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1286 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1287 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1288 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1289 MachineFunction::iterator It = BB;
1290 ++It;
1291 MF->insert(It, loop1MBB);
1292 MF->insert(It, loop2MBB);
1293 MF->insert(It, exitMBB);
1294
1295 // Transfer the remainder of BB and its successor edges to exitMBB.
1296 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001297 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001298 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1299
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001300 // thisMBB:
1301 // ...
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001302 // fallthrough --> loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001303 BB->addSuccessor(loop1MBB);
Akira Hatanaka08636b42011-07-19 17:09:53 +00001304 loop1MBB->addSuccessor(exitMBB);
1305 loop1MBB->addSuccessor(loop2MBB);
1306 loop2MBB->addSuccessor(loop1MBB);
1307 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001308
1309 // loop1MBB:
1310 // ll dest, 0(ptr)
1311 // bne dest, oldval, exitMBB
1312 BB = loop1MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001313 BuildMI(BB, DL, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1314 BuildMI(BB, DL, TII->get(BNE))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001315 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001316
1317 // loop2MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001318 // sc success, newval, 0(ptr)
1319 // beq success, $0, loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001320 BB = loop2MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001321 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001322 .addReg(NewVal).addReg(Ptr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001323 BuildMI(BB, DL, TII->get(BEQ))
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001324 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001325
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001326 MI->eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001327
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001328 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001329}
1330
1331MachineBasicBlock *
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001332MipsTargetLowering::emitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka15506782011-06-07 18:58:42 +00001333 MachineBasicBlock *BB,
1334 unsigned Size) const {
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001335 assert((Size == 1 || Size == 2) &&
1336 "Unsupported size for EmitAtomicCmpSwapPartial.");
1337
1338 MachineFunction *MF = BB->getParent();
1339 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1340 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
Eric Christopherd9134482014-08-04 21:25:23 +00001341 const TargetInstrInfo *TII =
1342 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001343 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001344
1345 unsigned Dest = MI->getOperand(0).getReg();
1346 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka0e019592011-07-19 20:11:17 +00001347 unsigned CmpVal = MI->getOperand(2).getReg();
1348 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001349
Akira Hatanaka0e019592011-07-19 20:11:17 +00001350 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1351 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001352 unsigned Mask = RegInfo.createVirtualRegister(RC);
1353 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001354 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1355 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1356 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1357 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1358 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1359 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1360 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1361 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1362 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1363 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1364 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1365 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001366 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001367
1368 // insert new blocks after the current block
1369 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1370 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1371 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001372 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001373 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1374 MachineFunction::iterator It = BB;
1375 ++It;
1376 MF->insert(It, loop1MBB);
1377 MF->insert(It, loop2MBB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001378 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001379 MF->insert(It, exitMBB);
1380
1381 // Transfer the remainder of BB and its successor edges to exitMBB.
1382 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001383 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001384 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1385
Akira Hatanaka08636b42011-07-19 17:09:53 +00001386 BB->addSuccessor(loop1MBB);
1387 loop1MBB->addSuccessor(sinkMBB);
1388 loop1MBB->addSuccessor(loop2MBB);
1389 loop2MBB->addSuccessor(loop1MBB);
1390 loop2MBB->addSuccessor(sinkMBB);
1391 sinkMBB->addSuccessor(exitMBB);
1392
Akira Hatanakae4503582011-07-19 18:14:26 +00001393 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001394 // thisMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001395 // addiu masklsb2,$0,-4 # 0xfffffffc
1396 // and alignedaddr,ptr,masklsb2
1397 // andi ptrlsb2,ptr,3
1398 // sll shiftamt,ptrlsb2,3
1399 // ori maskupper,$0,255 # 0xff
1400 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001401 // nor mask2,$0,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001402 // andi maskedcmpval,cmpval,255
1403 // sll shiftedcmpval,maskedcmpval,shiftamt
1404 // andi maskednewval,newval,255
1405 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001406 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001407 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001408 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001409 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001410 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001411 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Eric Christopher1c29a652014-07-18 22:55:25 +00001412 if (Subtarget.isLittle()) {
Akira Hatanaka2bf97332013-05-31 03:25:44 +00001413 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1414 } else {
1415 unsigned Off = RegInfo.createVirtualRegister(RC);
1416 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1417 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1418 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1419 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001420 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001421 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001422 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001423 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001424 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1425 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001426 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001427 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001428 .addReg(MaskedCmpVal).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001429 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001430 .addReg(NewVal).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001431 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001432 .addReg(MaskedNewVal).addReg(ShiftAmt);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001433
1434 // loop1MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001435 // ll oldval,0(alginedaddr)
1436 // and maskedoldval0,oldval,mask
1437 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001438 BB = loop1MBB;
Jozef Kolek2f27d572014-12-18 16:39:29 +00001439 unsigned LL = isMicroMips ? Mips::LL_MM : Mips::LL;
1440 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001441 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001442 .addReg(OldVal).addReg(Mask);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001443 BuildMI(BB, DL, TII->get(Mips::BNE))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001444 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001445
1446 // loop2MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001447 // and maskedoldval1,oldval,mask2
1448 // or storeval,maskedoldval1,shiftednewval
1449 // sc success,storeval,0(alignedaddr)
1450 // beq success,$0,loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001451 BB = loop2MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001452 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001453 .addReg(OldVal).addReg(Mask2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001454 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001455 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
Jozef Kolek2f27d572014-12-18 16:39:29 +00001456 unsigned SC = isMicroMips ? Mips::SC_MM : Mips::SC;
1457 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001458 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001459 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001460 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001461
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001462 // sinkMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001463 // srl srlres,maskedoldval0,shiftamt
Daniel Sanders6a803f62014-06-16 13:13:03 +00001464 // sign_extend dest,srlres
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001465 BB = sinkMBB;
Akira Hatanakae97bd812011-07-19 03:14:58 +00001466
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001467 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001468 .addReg(MaskedOldVal0).addReg(ShiftAmt);
Daniel Sanders6a803f62014-06-16 13:13:03 +00001469 BB = emitSignExtendToI32InReg(MI, BB, Size, Dest, SrlRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001470
1471 MI->eraseFromParent(); // The instruction is gone now.
1472
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001473 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001474}
1475
Daniel Sanders0fa60412014-06-12 13:39:06 +00001476MachineBasicBlock *MipsTargetLowering::emitSEL_D(MachineInstr *MI,
1477 MachineBasicBlock *BB) const {
1478 MachineFunction *MF = BB->getParent();
Eric Christopherd9134482014-08-04 21:25:23 +00001479 const TargetRegisterInfo *TRI =
1480 getTargetMachine().getSubtargetImpl()->getRegisterInfo();
1481 const TargetInstrInfo *TII =
1482 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Daniel Sanders0fa60412014-06-12 13:39:06 +00001483 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1484 DebugLoc DL = MI->getDebugLoc();
1485 MachineBasicBlock::iterator II(MI);
1486
1487 unsigned Fc = MI->getOperand(1).getReg();
1488 const auto &FGR64RegClass = TRI->getRegClass(Mips::FGR64RegClassID);
1489
1490 unsigned Fc2 = RegInfo.createVirtualRegister(FGR64RegClass);
1491
1492 BuildMI(*BB, II, DL, TII->get(Mips::SUBREG_TO_REG), Fc2)
1493 .addImm(0)
1494 .addReg(Fc)
1495 .addImm(Mips::sub_lo);
1496
1497 // We don't erase the original instruction, we just replace the condition
1498 // register with the 64-bit super-register.
1499 MI->getOperand(1).setReg(Fc2);
1500
1501 return BB;
1502}
1503
Akira Hatanakae2489122011-04-15 21:51:11 +00001504//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001505// Misc Lower Operation implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00001506//===----------------------------------------------------------------------===//
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001507SDValue MipsTargetLowering::lowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001508 SDValue Chain = Op.getOperand(0);
1509 SDValue Table = Op.getOperand(1);
1510 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001511 SDLoc DL(Op);
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001512 EVT PTy = getPointerTy();
1513 unsigned EntrySize =
1514 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(*getDataLayout());
1515
1516 Index = DAG.getNode(ISD::MUL, DL, PTy, Index,
1517 DAG.getConstant(EntrySize, PTy));
1518 SDValue Addr = DAG.getNode(ISD::ADD, DL, PTy, Index, Table);
1519
1520 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
1521 Addr = DAG.getExtLoad(ISD::SEXTLOAD, DL, PTy, Chain, Addr,
1522 MachinePointerInfo::getJumpTable(), MemVT, false, false,
Louis Gerbarg67474e32014-07-31 21:45:05 +00001523 false, 0);
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001524 Chain = Addr.getValue(1);
1525
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001526 if ((getTargetMachine().getRelocationModel() == Reloc::PIC_) ||
Eric Christopher1c29a652014-07-18 22:55:25 +00001527 Subtarget.isABI_N64()) {
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001528 // For PIC, the sequence is:
1529 // BRIND(load(Jumptable + index) + RelocBase)
1530 // RelocBase can be JumpTable, GOT or some sort of global base.
1531 Addr = DAG.getNode(ISD::ADD, DL, PTy, Addr,
1532 getPICJumpTableRelocBase(Table, DAG));
1533 }
1534
1535 return DAG.getNode(ISD::BRIND, DL, MVT::Other, Chain, Addr);
1536}
1537
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001538SDValue MipsTargetLowering::lowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Wesley Peck527da1b2010-11-23 03:31:01 +00001539 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001540 // the block to branch to if the condition is true.
1541 SDValue Chain = Op.getOperand(0);
1542 SDValue Dest = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001543 SDLoc DL(Op);
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001544
Eric Christopher1c29a652014-07-18 22:55:25 +00001545 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001546 SDValue CondRes = createFPCmp(DAG, Op.getOperand(1));
Akira Hatanakaa5352702011-03-31 18:26:17 +00001547
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001548 // Return if flag is not set by a floating point comparison.
Akira Hatanakaa5352702011-03-31 18:26:17 +00001549 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopesa9504222008-07-30 17:06:13 +00001550 return Op;
Wesley Peck527da1b2010-11-23 03:31:01 +00001551
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +00001552 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmaneffb8942008-09-12 16:56:44 +00001553 Mips::CondCode CC =
1554 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Akira Hatanakaf0ea5002013-03-30 01:16:38 +00001555 unsigned Opc = invertFPCondCodeUser(CC) ? Mips::BRANCH_F : Mips::BRANCH_T;
1556 SDValue BrCode = DAG.getConstant(Opc, MVT::i32);
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +00001557 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001558 return DAG.getNode(MipsISD::FPBrcond, DL, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +00001559 FCC0, Dest, CondRes);
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001560}
1561
1562SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001563lowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001564{
Eric Christopher1c29a652014-07-18 22:55:25 +00001565 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001566 SDValue Cond = createFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001567
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001568 // Return if flag is not set by a floating point comparison.
Akira Hatanakaa5352702011-03-31 18:26:17 +00001569 if (Cond.getOpcode() != MipsISD::FPCmp)
1570 return Op;
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +00001571
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001572 return createCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
Andrew Trickef9de2a2013-05-25 02:42:55 +00001573 SDLoc(Op));
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001574}
1575
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001576SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001577lowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001578{
Andrew Trickef9de2a2013-05-25 02:42:55 +00001579 SDLoc DL(Op);
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001580 EVT Ty = Op.getOperand(0).getValueType();
Matt Arsenault758659232013-05-18 00:21:46 +00001581 SDValue Cond = DAG.getNode(ISD::SETCC, DL,
1582 getSetCCResultType(*DAG.getContext(), Ty),
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001583 Op.getOperand(0), Op.getOperand(1),
1584 Op.getOperand(4));
1585
1586 return DAG.getNode(ISD::SELECT, DL, Op.getValueType(), Cond, Op.getOperand(2),
1587 Op.getOperand(3));
1588}
1589
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001590SDValue MipsTargetLowering::lowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00001591 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001592 SDValue Cond = createFPCmp(DAG, Op);
Akira Hatanakab7f78592012-03-09 23:46:03 +00001593
1594 assert(Cond.getOpcode() == MipsISD::FPCmp &&
1595 "Floating point operand expected.");
1596
1597 SDValue True = DAG.getConstant(1, MVT::i32);
1598 SDValue False = DAG.getConstant(0, MVT::i32);
1599
Andrew Trickef9de2a2013-05-25 02:42:55 +00001600 return createCMovFP(DAG, Cond, True, False, SDLoc(Op));
Akira Hatanakab7f78592012-03-09 23:46:03 +00001601}
1602
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001603SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001604 SelectionDAG &DAG) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001605 EVT Ty = Op.getValueType();
1606 GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
1607 const GlobalValue *GV = N->getGlobal();
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001608
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 Hatanaka92a96e12012-09-12 23:27:55 +00001611 const MipsTargetObjectFile &TLOF =
1612 (const MipsTargetObjectFile&)getObjFileLowering();
Wesley Peck527da1b2010-11-23 03:31:01 +00001613
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001614 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine()))
1615 // %gp_rel relocation
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001616 return getAddrGPRel(N, SDLoc(N), Ty, DAG);
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001617
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001618 // %hi/%lo relocation
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001619 return getAddrNonPIC(N, SDLoc(N), Ty, DAG);
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001620 }
1621
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001622 if (GV->hasInternalLinkage() || (GV->hasLocalLinkage() && !isa<Function>(GV)))
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001623 return getAddrLocal(N, SDLoc(N), Ty, DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +00001624 Subtarget.isABI_N32() || Subtarget.isABI_N64());
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001625
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00001626 if (LargeGOT)
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001627 return getAddrGlobalLargeGOT(N, SDLoc(N), Ty, DAG, MipsII::MO_GOT_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00001628 MipsII::MO_GOT_LO16, DAG.getEntryNode(),
1629 MachinePointerInfo::getGOT());
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00001630
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001631 return getAddrGlobal(N, SDLoc(N), Ty, DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +00001632 (Subtarget.isABI_N32() || Subtarget.isABI_N64())
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001633 ? MipsII::MO_GOT_DISP
1634 : MipsII::MO_GOT16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00001635 DAG.getEntryNode(), MachinePointerInfo::getGOT());
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001636}
1637
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001638SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op,
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +00001639 SelectionDAG &DAG) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001640 BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op);
1641 EVT Ty = Op.getValueType();
Akira Hatanaka30f97cf2013-09-25 00:30:25 +00001642
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001643 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ &&
Eric Christopher1c29a652014-07-18 22:55:25 +00001644 !Subtarget.isABI_N64())
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001645 return getAddrNonPIC(N, SDLoc(N), Ty, DAG);
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001646
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001647 return getAddrLocal(N, SDLoc(N), Ty, DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +00001648 Subtarget.isABI_N32() || Subtarget.isABI_N64());
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +00001649}
1650
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001651SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001652lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001653{
Akira Hatanakabff84e12011-12-14 18:26:41 +00001654 // If the relocation model is PIC, use the General Dynamic TLS Model or
1655 // Local Dynamic TLS model, otherwise use the Initial Exec or
1656 // Local Exec TLS Model.
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001657
1658 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001659 SDLoc DL(GA);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001660 const GlobalValue *GV = GA->getGlobal();
1661 EVT PtrVT = getPointerTy();
1662
Hans Wennborgaea41202012-05-04 09:40:39 +00001663 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
1664
1665 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Hans Wennborg245917b2012-06-04 14:02:08 +00001666 // General Dynamic and Local Dynamic TLS Model.
1667 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
1668 : MipsII::MO_TLSGD;
1669
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001670 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag);
1671 SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT,
1672 getGlobalReg(DAG, PtrVT), TGA);
Akira Hatanakaf10ee842011-12-08 21:05:38 +00001673 unsigned PtrSize = PtrVT.getSizeInBits();
1674 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1675
Benjamin Kramer64ba50a2011-12-11 12:21:34 +00001676 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001677
1678 ArgListTy Args;
1679 ArgListEntry Entry;
1680 Entry.Node = Argument;
Akira Hatanakadee6c822011-12-08 20:34:32 +00001681 Entry.Ty = PtrTy;
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001682 Args.push_back(Entry);
Jia Liuf54f60f2012-02-28 07:46:26 +00001683
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00001684 TargetLowering::CallLoweringInfo CLI(DAG);
1685 CLI.setDebugLoc(DL).setChain(DAG.getEntryNode())
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00001686 .setCallee(CallingConv::C, PtrTy, TlsGetAddr, std::move(Args), 0);
Justin Holewinskiaa583972012-05-25 16:35:28 +00001687 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001688
Akira Hatanakabff84e12011-12-14 18:26:41 +00001689 SDValue Ret = CallResult.first;
1690
Hans Wennborgaea41202012-05-04 09:40:39 +00001691 if (model != TLSModel::LocalDynamic)
Akira Hatanakabff84e12011-12-14 18:26:41 +00001692 return Ret;
1693
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001694 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanakabff84e12011-12-14 18:26:41 +00001695 MipsII::MO_DTPREL_HI);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001696 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1697 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanakabff84e12011-12-14 18:26:41 +00001698 MipsII::MO_DTPREL_LO);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001699 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1700 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret);
1701 return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001702 }
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001703
1704 SDValue Offset;
Hans Wennborgaea41202012-05-04 09:40:39 +00001705 if (model == TLSModel::InitialExec) {
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001706 // Initial Exec TLS Model
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001707 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001708 MipsII::MO_GOTTPREL);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001709 TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT),
Akira Hatanakab049aef2012-02-24 22:34:47 +00001710 TGA);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001711 Offset = DAG.getLoad(PtrVT, DL,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001712 DAG.getEntryNode(), TGA, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001713 false, false, false, 0);
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001714 } else {
1715 // Local Exec TLS Model
Hans Wennborgaea41202012-05-04 09:40:39 +00001716 assert(model == TLSModel::LocalExec);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001717 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001718 MipsII::MO_TPREL_HI);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001719 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001720 MipsII::MO_TPREL_LO);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001721 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1722 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1723 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001724 }
1725
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001726 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
1727 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001728}
1729
1730SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001731lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +00001732{
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001733 JumpTableSDNode *N = cast<JumpTableSDNode>(Op);
1734 EVT Ty = Op.getValueType();
Akira Hatanaka30f97cf2013-09-25 00:30:25 +00001735
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001736 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ &&
Eric Christopher1c29a652014-07-18 22:55:25 +00001737 !Subtarget.isABI_N64())
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001738 return getAddrNonPIC(N, SDLoc(N), Ty, DAG);
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001739
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001740 return getAddrLocal(N, SDLoc(N), Ty, DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +00001741 Subtarget.isABI_N32() || Subtarget.isABI_N64());
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +00001742}
1743
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001744SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001745lowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +00001746{
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001747 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
1748 EVT Ty = Op.getValueType();
Bruno Cardoso Lopes2db07582009-11-25 12:17:58 +00001749
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001750 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ &&
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001751 !Subtarget.isABI_N64()) {
1752 const MipsTargetObjectFile &TLOF =
1753 (const MipsTargetObjectFile&)getObjFileLowering();
1754
1755 if (TLOF.IsConstantInSmallSection(N->getConstVal(), getTargetMachine()))
1756 // %gp_rel relocation
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001757 return getAddrGPRel(N, SDLoc(N), Ty, DAG);
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001758
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001759 return getAddrNonPIC(N, SDLoc(N), Ty, DAG);
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001760 }
Bruno Cardoso Lopesfdb4cec2008-07-23 16:01:50 +00001761
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001762 return getAddrLocal(N, SDLoc(N), Ty, DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +00001763 Subtarget.isABI_N32() || Subtarget.isABI_N64());
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +00001764}
1765
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001766SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman31ae5862010-04-17 14:41:14 +00001767 MachineFunction &MF = DAG.getMachineFunction();
1768 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1769
Andrew Trickef9de2a2013-05-25 02:42:55 +00001770 SDLoc DL(Op);
Dan Gohman31ae5862010-04-17 14:41:14 +00001771 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1772 getPointerTy());
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +00001773
1774 // vastart just stores the address of the VarArgsFrameIndex slot into the
1775 // memory location argument.
1776 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001777 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00001778 MachinePointerInfo(SV), false, false, 0);
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +00001779}
Jia Liuf54f60f2012-02-28 07:46:26 +00001780
Daniel Sanders2b553d42014-08-01 09:17:39 +00001781SDValue MipsTargetLowering::lowerVAARG(SDValue Op, SelectionDAG &DAG) const {
1782 SDNode *Node = Op.getNode();
1783 EVT VT = Node->getValueType(0);
1784 SDValue Chain = Node->getOperand(0);
1785 SDValue VAListPtr = Node->getOperand(1);
1786 unsigned Align = Node->getConstantOperandVal(3);
1787 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
1788 SDLoc DL(Node);
1789 unsigned ArgSlotSizeInBytes =
1790 (Subtarget.isABI_N32() || Subtarget.isABI_N64()) ? 8 : 4;
1791
1792 SDValue VAListLoad = DAG.getLoad(getPointerTy(), DL, Chain, VAListPtr,
1793 MachinePointerInfo(SV), false, false, false,
1794 0);
1795 SDValue VAList = VAListLoad;
1796
1797 // Re-align the pointer if necessary.
1798 // It should only ever be necessary for 64-bit types on O32 since the minimum
1799 // argument alignment is the same as the maximum type alignment for N32/N64.
1800 //
1801 // FIXME: We currently align too often. The code generator doesn't notice
1802 // when the pointer is still aligned from the last va_arg (or pair of
1803 // va_args for the i64 on O32 case).
1804 if (Align > getMinStackArgumentAlignment()) {
1805 assert(((Align & (Align-1)) == 0) && "Expected Align to be a power of 2");
1806
1807 VAList = DAG.getNode(ISD::ADD, DL, VAList.getValueType(), VAList,
1808 DAG.getConstant(Align - 1,
1809 VAList.getValueType()));
1810
1811 VAList = DAG.getNode(ISD::AND, DL, VAList.getValueType(), VAList,
1812 DAG.getConstant(-(int64_t)Align,
1813 VAList.getValueType()));
1814 }
1815
1816 // Increment the pointer, VAList, to the next vaarg.
1817 unsigned ArgSizeInBytes = getDataLayout()->getTypeAllocSize(VT.getTypeForEVT(*DAG.getContext()));
1818 SDValue Tmp3 = DAG.getNode(ISD::ADD, DL, VAList.getValueType(), VAList,
1819 DAG.getConstant(RoundUpToAlignment(ArgSizeInBytes, ArgSlotSizeInBytes),
1820 VAList.getValueType()));
1821 // Store the incremented VAList to the legalized pointer
1822 Chain = DAG.getStore(VAListLoad.getValue(1), DL, Tmp3, VAListPtr,
1823 MachinePointerInfo(SV), false, false, 0);
1824
1825 // In big-endian mode we must adjust the pointer when the load size is smaller
1826 // than the argument slot size. We must also reduce the known alignment to
1827 // match. For example in the N64 ABI, we must add 4 bytes to the offset to get
1828 // the correct half of the slot, and reduce the alignment from 8 (slot
1829 // alignment) down to 4 (type alignment).
1830 if (!Subtarget.isLittle() && ArgSizeInBytes < ArgSlotSizeInBytes) {
1831 unsigned Adjustment = ArgSlotSizeInBytes - ArgSizeInBytes;
1832 VAList = DAG.getNode(ISD::ADD, DL, VAListPtr.getValueType(), VAList,
1833 DAG.getIntPtrConstant(Adjustment));
1834 }
1835 // Load the actual argument out of the pointer VAList
1836 return DAG.getLoad(VT, DL, Chain, VAList, MachinePointerInfo(), false, false,
1837 false, 0);
1838}
1839
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001840static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG,
1841 bool HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001842 EVT TyX = Op.getOperand(0).getValueType();
1843 EVT TyY = Op.getOperand(1).getValueType();
1844 SDValue Const1 = DAG.getConstant(1, MVT::i32);
1845 SDValue Const31 = DAG.getConstant(31, MVT::i32);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001846 SDLoc DL(Op);
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001847 SDValue Res;
1848
1849 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1850 // to i32.
1851 SDValue X = (TyX == MVT::f32) ?
1852 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1853 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1854 Const1);
1855 SDValue Y = (TyY == MVT::f32) ?
1856 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
1857 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
1858 Const1);
1859
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001860 if (HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001861 // ext E, Y, 31, 1 ; extract bit31 of Y
1862 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
1863 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
1864 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
1865 } else {
1866 // sll SllX, X, 1
1867 // srl SrlX, SllX, 1
1868 // srl SrlY, Y, 31
1869 // sll SllY, SrlX, 31
1870 // or Or, SrlX, SllY
1871 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1872 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1873 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
1874 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
1875 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
1876 }
1877
1878 if (TyX == MVT::f32)
1879 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
1880
1881 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1882 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1883 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001884}
1885
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001886static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG,
1887 bool HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001888 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
1889 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
1890 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
1891 SDValue Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001892 SDLoc DL(Op);
Eric Christopher0713a9d2011-06-08 23:55:35 +00001893
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001894 // Bitcast to integer nodes.
1895 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
1896 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001897
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001898 if (HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001899 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
1900 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
1901 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
1902 DAG.getConstant(WidthY - 1, MVT::i32), Const1);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001903
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001904 if (WidthX > WidthY)
1905 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
1906 else if (WidthY > WidthX)
1907 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001908
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001909 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
1910 DAG.getConstant(WidthX - 1, MVT::i32), Const1, X);
1911 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
1912 }
1913
1914 // (d)sll SllX, X, 1
1915 // (d)srl SrlX, SllX, 1
1916 // (d)srl SrlY, Y, width(Y)-1
1917 // (d)sll SllY, SrlX, width(Y)-1
1918 // or Or, SrlX, SllY
1919 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
1920 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
1921 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
1922 DAG.getConstant(WidthY - 1, MVT::i32));
1923
1924 if (WidthX > WidthY)
1925 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
1926 else if (WidthY > WidthX)
1927 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
1928
1929 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
1930 DAG.getConstant(WidthX - 1, MVT::i32));
1931 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
1932 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001933}
1934
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00001935SDValue
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001936MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00001937 if (Subtarget.isGP64bit())
1938 return lowerFCOPYSIGN64(Op, DAG, Subtarget.hasExtractInsert());
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001939
Eric Christopher1c29a652014-07-18 22:55:25 +00001940 return lowerFCOPYSIGN32(Op, DAG, Subtarget.hasExtractInsert());
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001941}
1942
Akira Hatanaka66277522011-06-02 00:24:44 +00001943SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001944lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes5444a7b2011-06-16 00:40:02 +00001945 // check the depth
1946 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka15506782011-06-07 18:58:42 +00001947 "Frame address can only be determined for current frame.");
Akira Hatanaka66277522011-06-02 00:24:44 +00001948
1949 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1950 MFI->setFrameAddressIsTaken(true);
1951 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001952 SDLoc DL(Op);
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001953 SDValue FrameAddr =
1954 DAG.getCopyFromReg(DAG.getEntryNode(), DL,
Eric Christopher1c29a652014-07-18 22:55:25 +00001955 Subtarget.isABI_N64() ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka66277522011-06-02 00:24:44 +00001956 return FrameAddr;
1957}
1958
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001959SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op,
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00001960 SelectionDAG &DAG) const {
Bill Wendling908bf812014-01-06 00:43:20 +00001961 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001962 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001963
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00001964 // check the depth
1965 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
1966 "Return address can be determined only for current frame.");
1967
1968 MachineFunction &MF = DAG.getMachineFunction();
1969 MachineFrameInfo *MFI = MF.getFrameInfo();
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001970 MVT VT = Op.getSimpleValueType();
Eric Christopher1c29a652014-07-18 22:55:25 +00001971 unsigned RA = Subtarget.isABI_N64() ? Mips::RA_64 : Mips::RA;
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00001972 MFI->setReturnAddressIsTaken(true);
1973
1974 // Return RA, which contains the return address. Mark it an implicit live-in.
1975 unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00001976 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT);
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00001977}
1978
Akira Hatanakac0b02062013-01-30 00:26:49 +00001979// An EH_RETURN is the result of lowering llvm.eh.return which in turn is
1980// generated from __builtin_eh_return (offset, handler)
1981// The effect of this is to adjust the stack pointer by "offset"
1982// and then branch to "handler".
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001983SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Akira Hatanakac0b02062013-01-30 00:26:49 +00001984 const {
1985 MachineFunction &MF = DAG.getMachineFunction();
1986 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1987
1988 MipsFI->setCallsEhReturn();
1989 SDValue Chain = Op.getOperand(0);
1990 SDValue Offset = Op.getOperand(1);
1991 SDValue Handler = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001992 SDLoc DL(Op);
Eric Christopher1c29a652014-07-18 22:55:25 +00001993 EVT Ty = Subtarget.isABI_N64() ? MVT::i64 : MVT::i32;
Akira Hatanakac0b02062013-01-30 00:26:49 +00001994
1995 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
1996 // EH_RETURN nodes, so that instructions are emitted back-to-back.
Eric Christopher1c29a652014-07-18 22:55:25 +00001997 unsigned OffsetReg = Subtarget.isABI_N64() ? Mips::V1_64 : Mips::V1;
1998 unsigned AddrReg = Subtarget.isABI_N64() ? Mips::V0_64 : Mips::V0;
Akira Hatanakac0b02062013-01-30 00:26:49 +00001999 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
2000 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
2001 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
2002 DAG.getRegister(OffsetReg, Ty),
2003 DAG.getRegister(AddrReg, getPointerTy()),
2004 Chain.getValue(1));
2005}
2006
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002007SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
Akira Hatanaka5fd22482012-06-14 21:10:56 +00002008 SelectionDAG &DAG) const {
Eli Friedman26a48482011-07-27 22:21:52 +00002009 // FIXME: Need pseudo-fence for 'singlethread' fences
2010 // FIXME: Set SType for weaker fences where supported/appropriate.
2011 unsigned SType = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002012 SDLoc DL(Op);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002013 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
Eli Friedman26a48482011-07-27 22:21:52 +00002014 DAG.getConstant(SType, MVT::i32));
2015}
2016
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002017SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
Akira Hatanaka5fd22482012-06-14 21:10:56 +00002018 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002019 SDLoc DL(Op);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002020 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2021 SDValue Shamt = Op.getOperand(2);
2022
2023 // if shamt < 32:
2024 // lo = (shl lo, shamt)
2025 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
2026 // else:
2027 // lo = 0
2028 // hi = (shl lo, shamt[4:0])
2029 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
2030 DAG.getConstant(-1, MVT::i32));
2031 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo,
2032 DAG.getConstant(1, MVT::i32));
2033 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, ShiftRight1Lo,
2034 Not);
2035 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi, Shamt);
2036 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
2037 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, MVT::i32, Lo, Shamt);
2038 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
2039 DAG.getConstant(0x20, MVT::i32));
Akira Hatanaka5fd22482012-06-14 21:10:56 +00002040 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
2041 DAG.getConstant(0, MVT::i32), ShiftLeftLo);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002042 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftLeftLo, Or);
2043
2044 SDValue Ops[2] = {Lo, Hi};
Craig Topper64941d92014-04-27 19:20:57 +00002045 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002046}
2047
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002048SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002049 bool IsSRA) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002050 SDLoc DL(Op);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002051 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2052 SDValue Shamt = Op.getOperand(2);
2053
2054 // if shamt < 32:
2055 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
2056 // if isSRA:
2057 // hi = (sra hi, shamt)
2058 // else:
2059 // hi = (srl hi, shamt)
2060 // else:
2061 // if isSRA:
2062 // lo = (sra hi, shamt[4:0])
2063 // hi = (sra hi, 31)
2064 // else:
2065 // lo = (srl hi, shamt[4:0])
2066 // hi = 0
2067 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
2068 DAG.getConstant(-1, MVT::i32));
2069 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi,
2070 DAG.getConstant(1, MVT::i32));
2071 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, ShiftLeft1Hi, Not);
2072 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo, Shamt);
2073 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
2074 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL, DL, MVT::i32,
2075 Hi, Shamt);
2076 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
2077 DAG.getConstant(0x20, MVT::i32));
2078 SDValue Shift31 = DAG.getNode(ISD::SRA, DL, MVT::i32, Hi,
2079 DAG.getConstant(31, MVT::i32));
2080 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftRightHi, Or);
2081 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
2082 IsSRA ? Shift31 : DAG.getConstant(0, MVT::i32),
2083 ShiftRightHi);
2084
2085 SDValue Ops[2] = {Lo, Hi};
Craig Topper64941d92014-04-27 19:20:57 +00002086 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002087}
2088
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002089static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002090 SDValue Chain, SDValue Src, unsigned Offset) {
Akira Hatanaka95866182012-06-13 19:06:08 +00002091 SDValue Ptr = LD->getBasePtr();
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002092 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
Akira Hatanaka95866182012-06-13 19:06:08 +00002093 EVT BasePtrVT = Ptr.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002094 SDLoc DL(LD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002095 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
2096
2097 if (Offset)
Akira Hatanaka95866182012-06-13 19:06:08 +00002098 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002099 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002100
2101 SDValue Ops[] = { Chain, Ptr, Src };
Craig Topper206fcd42014-04-26 19:29:41 +00002102 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002103 LD->getMemOperand());
2104}
2105
2106// Expand an unaligned 32 or 64-bit integer load node.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002107SDValue MipsTargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002108 LoadSDNode *LD = cast<LoadSDNode>(Op);
2109 EVT MemVT = LD->getMemoryVT();
2110
Eric Christopher1c29a652014-07-18 22:55:25 +00002111 if (Subtarget.systemSupportsUnalignedAccess())
Daniel Sandersac272632014-05-23 13:18:02 +00002112 return Op;
2113
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002114 // Return if load is aligned or if MemVT is neither i32 nor i64.
2115 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
2116 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
2117 return SDValue();
2118
Eric Christopher1c29a652014-07-18 22:55:25 +00002119 bool IsLittle = Subtarget.isLittle();
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002120 EVT VT = Op.getValueType();
2121 ISD::LoadExtType ExtType = LD->getExtensionType();
2122 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
2123
2124 assert((VT == MVT::i32) || (VT == MVT::i64));
2125
2126 // Expand
2127 // (set dst, (i64 (load baseptr)))
2128 // to
2129 // (set tmp, (ldl (add baseptr, 7), undef))
2130 // (set dst, (ldr baseptr, tmp))
2131 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002132 SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002133 IsLittle ? 7 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002134 return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002135 IsLittle ? 0 : 7);
2136 }
2137
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002138 SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002139 IsLittle ? 3 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002140 SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002141 IsLittle ? 0 : 3);
2142
2143 // Expand
2144 // (set dst, (i32 (load baseptr))) or
2145 // (set dst, (i64 (sextload baseptr))) or
2146 // (set dst, (i64 (extload baseptr)))
2147 // to
2148 // (set tmp, (lwl (add baseptr, 3), undef))
2149 // (set dst, (lwr baseptr, tmp))
2150 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
2151 (ExtType == ISD::EXTLOAD))
2152 return LWR;
2153
2154 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
2155
2156 // Expand
2157 // (set dst, (i64 (zextload baseptr)))
2158 // to
2159 // (set tmp0, (lwl (add baseptr, 3), undef))
2160 // (set tmp1, (lwr baseptr, tmp0))
2161 // (set tmp2, (shl tmp1, 32))
2162 // (set dst, (srl tmp2, 32))
Andrew Trickef9de2a2013-05-25 02:42:55 +00002163 SDLoc DL(LD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002164 SDValue Const32 = DAG.getConstant(32, MVT::i32);
2165 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
Akira Hatanaka67346852012-06-04 17:46:29 +00002166 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
2167 SDValue Ops[] = { SRL, LWR.getValue(1) };
Craig Topper64941d92014-04-27 19:20:57 +00002168 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002169}
2170
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002171static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002172 SDValue Chain, unsigned Offset) {
Akira Hatanaka95866182012-06-13 19:06:08 +00002173 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
2174 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002175 SDLoc DL(SD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002176 SDVTList VTList = DAG.getVTList(MVT::Other);
2177
2178 if (Offset)
Akira Hatanaka95866182012-06-13 19:06:08 +00002179 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002180 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002181
2182 SDValue Ops[] = { Chain, Value, Ptr };
Craig Topper206fcd42014-04-26 19:29:41 +00002183 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002184 SD->getMemOperand());
2185}
2186
2187// Expand an unaligned 32 or 64-bit integer store node.
Akira Hatanakad82ee942013-05-16 20:45:17 +00002188static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG,
2189 bool IsLittle) {
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002190 SDValue Value = SD->getValue(), Chain = SD->getChain();
2191 EVT VT = Value.getValueType();
2192
2193 // Expand
2194 // (store val, baseptr) or
2195 // (truncstore val, baseptr)
2196 // to
2197 // (swl val, (add baseptr, 3))
2198 // (swr val, baseptr)
2199 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002200 SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002201 IsLittle ? 3 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002202 return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002203 }
2204
2205 assert(VT == MVT::i64);
2206
2207 // Expand
2208 // (store val, baseptr)
2209 // to
2210 // (sdl val, (add baseptr, 7))
2211 // (sdr val, baseptr)
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002212 SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2213 return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002214}
2215
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002216// Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr).
2217static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG) {
2218 SDValue Val = SD->getValue();
2219
2220 if (Val.getOpcode() != ISD::FP_TO_SINT)
2221 return SDValue();
2222
2223 EVT FPTy = EVT::getFloatingPointVT(Val.getValueSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002224 SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy,
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002225 Val.getOperand(0));
2226
Andrew Trickef9de2a2013-05-25 02:42:55 +00002227 return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(),
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002228 SD->getPointerInfo(), SD->isVolatile(),
2229 SD->isNonTemporal(), SD->getAlignment());
2230}
2231
Akira Hatanakad82ee942013-05-16 20:45:17 +00002232SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2233 StoreSDNode *SD = cast<StoreSDNode>(Op);
2234 EVT MemVT = SD->getMemoryVT();
2235
2236 // Lower unaligned integer stores.
Eric Christopher1c29a652014-07-18 22:55:25 +00002237 if (!Subtarget.systemSupportsUnalignedAccess() &&
Daniel Sandersac272632014-05-23 13:18:02 +00002238 (SD->getAlignment() < MemVT.getSizeInBits() / 8) &&
Akira Hatanakad82ee942013-05-16 20:45:17 +00002239 ((MemVT == MVT::i32) || (MemVT == MVT::i64)))
Eric Christopher1c29a652014-07-18 22:55:25 +00002240 return lowerUnalignedIntStore(SD, DAG, Subtarget.isLittle());
Akira Hatanakad82ee942013-05-16 20:45:17 +00002241
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002242 return lowerFP_TO_SINT_STORE(SD, DAG);
Akira Hatanakad82ee942013-05-16 20:45:17 +00002243}
2244
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002245SDValue MipsTargetLowering::lowerADD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002246 if (Op->getOperand(0).getOpcode() != ISD::FRAMEADDR
2247 || cast<ConstantSDNode>
2248 (Op->getOperand(0).getOperand(0))->getZExtValue() != 0
2249 || Op->getOperand(1).getOpcode() != ISD::FRAME_TO_ARGS_OFFSET)
2250 return SDValue();
2251
2252 // The pattern
2253 // (add (frameaddr 0), (frame_to_args_offset))
2254 // results from lowering llvm.eh.dwarf.cfa intrinsic. Transform it to
2255 // (add FrameObject, 0)
2256 // where FrameObject is a fixed StackObject with offset 0 which points to
2257 // the old stack pointer.
2258 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2259 EVT ValTy = Op->getValueType(0);
2260 int FI = MFI->CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
2261 SDValue InArgsAddr = DAG.getFrameIndex(FI, ValTy);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002262 return DAG.getNode(ISD::ADD, SDLoc(Op), ValTy, InArgsAddr,
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002263 DAG.getConstant(0, ValTy));
2264}
2265
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002266SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op,
2267 SelectionDAG &DAG) const {
2268 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002269 SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy,
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002270 Op.getOperand(0));
Andrew Trickef9de2a2013-05-25 02:42:55 +00002271 return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc);
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002272}
2273
Akira Hatanakae2489122011-04-15 21:51:11 +00002274//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002275// Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002276//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002277
Akira Hatanakae2489122011-04-15 21:51:11 +00002278//===----------------------------------------------------------------------===//
Wesley Peck527da1b2010-11-23 03:31:01 +00002279// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002280// Mips O32 ABI rules:
2281// ---
2282// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peck527da1b2010-11-23 03:31:01 +00002283// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002284// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peck527da1b2010-11-23 03:31:01 +00002285// f64 - Only passed in two aliased f32 registers if no int reg has been used
2286// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Sylvestre Ledru469de192014-08-11 18:04:46 +00002287// not used, it must be shadowed. If only A3 is available, shadow it and
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002288// go to stack.
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002289//
2290// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanakae2489122011-04-15 21:51:11 +00002291//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002292
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00002293static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2294 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
Craig Topper840beec2014-04-04 05:16:06 +00002295 CCState &State, const MCPhysReg *F64Regs) {
Daniel Sandersd134c9d2014-12-02 20:40:27 +00002296 const MipsSubtarget &Subtarget =
2297 State.getMachineFunction().getTarget()
2298 .getSubtarget<const MipsSubtarget>();
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002299
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00002300 static const unsigned IntRegsSize = 4, FloatRegsSize = 2;
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002301
Craig Topper840beec2014-04-04 05:16:06 +00002302 static const MCPhysReg IntRegs[] = { Mips::A0, Mips::A1, Mips::A2, Mips::A3 };
2303 static const MCPhysReg F32Regs[] = { Mips::F12, Mips::F14 };
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002304
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002305 // Do not process byval args here.
2306 if (ArgFlags.isByVal())
2307 return true;
Akira Hatanaka5e16c6a2011-05-24 19:18:33 +00002308
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002309 // Promote i8 and i16
Daniel Sandersd134c9d2014-12-02 20:40:27 +00002310 if (ArgFlags.isInReg() && !Subtarget.isLittle()) {
2311 if (LocVT == MVT::i8 || LocVT == MVT::i16 || LocVT == MVT::i32) {
2312 LocVT = MVT::i32;
2313 if (ArgFlags.isSExt())
2314 LocInfo = CCValAssign::SExtUpper;
2315 else if (ArgFlags.isZExt())
2316 LocInfo = CCValAssign::ZExtUpper;
2317 else
2318 LocInfo = CCValAssign::AExtUpper;
2319 }
2320 }
2321
2322 // Promote i8 and i16
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002323 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
2324 LocVT = MVT::i32;
2325 if (ArgFlags.isSExt())
2326 LocInfo = CCValAssign::SExt;
2327 else if (ArgFlags.isZExt())
2328 LocInfo = CCValAssign::ZExt;
2329 else
2330 LocInfo = CCValAssign::AExt;
2331 }
2332
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002333 unsigned Reg;
2334
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002335 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
2336 // is true: function is vararg, argument is 3rd or higher, there is previous
2337 // argument which is not f32 or f64.
2338 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
2339 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanaka9e6a8cc2011-05-19 20:29:48 +00002340 unsigned OrigAlign = ArgFlags.getOrigAlign();
2341 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002342
2343 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002344 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanaka9e6a8cc2011-05-19 20:29:48 +00002345 // If this is the first part of an i64 arg,
2346 // the allocated register must be either A0 or A2.
2347 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
2348 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002349 LocVT = MVT::i32;
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002350 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
2351 // Allocate int register and shadow next int register. If first
2352 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002353 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2354 if (Reg == Mips::A1 || Reg == Mips::A3)
2355 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2356 State.AllocateReg(IntRegs, IntRegsSize);
2357 LocVT = MVT::i32;
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002358 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
2359 // we are guaranteed to find an available float register
2360 if (ValVT == MVT::f32) {
2361 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
2362 // Shadow int register
2363 State.AllocateReg(IntRegs, IntRegsSize);
2364 } else {
2365 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
2366 // Shadow int registers
2367 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
2368 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
2369 State.AllocateReg(IntRegs, IntRegsSize);
2370 State.AllocateReg(IntRegs, IntRegsSize);
2371 }
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002372 } else
2373 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002374
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002375 if (!Reg) {
2376 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3,
2377 OrigAlign);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002378 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002379 } else
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002380 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002381
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002382 return false;
Akira Hatanaka202f6402011-11-12 02:20:46 +00002383}
2384
Akira Hatanakabfb66242013-08-20 23:38:40 +00002385static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT,
2386 MVT LocVT, CCValAssign::LocInfo LocInfo,
2387 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002388 static const MCPhysReg F64Regs[] = { Mips::D6, Mips::D7 };
Akira Hatanakabfb66242013-08-20 23:38:40 +00002389
2390 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2391}
2392
2393static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT,
2394 MVT LocVT, CCValAssign::LocInfo LocInfo,
2395 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002396 static const MCPhysReg F64Regs[] = { Mips::D12_64, Mips::D14_64 };
Akira Hatanakabfb66242013-08-20 23:38:40 +00002397
2398 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2399}
2400
Reid Klecknerd3781742014-11-14 00:39:33 +00002401static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2402 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
2403 CCState &State) LLVM_ATTRIBUTE_UNUSED;
Reed Kotlerd5c41962014-11-13 23:37:45 +00002404
Akira Hatanaka202f6402011-11-12 02:20:46 +00002405#include "MipsGenCallingConv.inc"
2406
Akira Hatanakae2489122011-04-15 21:51:11 +00002407//===----------------------------------------------------------------------===//
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002408// Call Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002409//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002410
Akira Hatanaka61bbcce2011-09-23 00:58:33 +00002411// Return next O32 integer argument register.
2412static unsigned getNextIntArgReg(unsigned Reg) {
2413 assert((Reg == Mips::A0) || (Reg == Mips::A2));
2414 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
2415}
2416
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002417SDValue
2418MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002419 SDValue Chain, SDValue Arg, SDLoc DL,
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002420 bool IsTailCall, SelectionDAG &DAG) const {
2421 if (!IsTailCall) {
2422 SDValue PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr,
2423 DAG.getIntPtrConstant(Offset));
2424 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo(), false,
2425 false, 0);
2426 }
2427
2428 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2429 int FI = MFI->CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
2430 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2431 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(),
2432 /*isVolatile=*/ true, false, 0);
2433}
2434
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002435void MipsTargetLowering::
2436getOpndList(SmallVectorImpl<SDValue> &Ops,
2437 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
2438 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
Sasa Stankovic7072a792014-10-01 08:22:21 +00002439 bool IsCallReloc, CallLoweringInfo &CLI, SDValue Callee,
2440 SDValue Chain) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002441 // Insert node "GP copy globalreg" before call to function.
2442 //
2443 // R_MIPS_CALL* operators (emitted when non-internal functions are called
2444 // in PIC mode) allow symbols to be resolved via lazy binding.
2445 // The lazy binding stub requires GP to point to the GOT.
Sasa Stankovic7072a792014-10-01 08:22:21 +00002446 // Note that we don't need GP to point to the GOT for indirect calls
2447 // (when R_MIPS_CALL* is not used for the call) because Mips linker generates
2448 // lazy binding stub for a function only when R_MIPS_CALL* are the only relocs
2449 // used for the function (that is, Mips linker doesn't generate lazy binding
2450 // stub for a function whose address is taken in the program).
2451 if (IsPICCall && !InternalLinkage && IsCallReloc) {
Eric Christopher1c29a652014-07-18 22:55:25 +00002452 unsigned GPReg = Subtarget.isABI_N64() ? Mips::GP_64 : Mips::GP;
2453 EVT Ty = Subtarget.isABI_N64() ? MVT::i64 : MVT::i32;
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002454 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty)));
2455 }
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002456
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002457 // Build a sequence of copy-to-reg nodes chained together with token
2458 // chain and flag operands which copy the outgoing args into registers.
2459 // The InFlag in necessary since all emitted instructions must be
2460 // stuck together.
2461 SDValue InFlag;
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002462
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002463 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2464 Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, RegsToPass[i].first,
2465 RegsToPass[i].second, InFlag);
2466 InFlag = Chain.getValue(1);
2467 }
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002468
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002469 // Add argument registers to the end of the list so that they are
2470 // known live into the call.
2471 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2472 Ops.push_back(CLI.DAG.getRegister(RegsToPass[i].first,
2473 RegsToPass[i].second.getValueType()));
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002474
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002475 // Add a register mask operand representing the call-preserved registers.
Eric Christopherd9134482014-08-04 21:25:23 +00002476 const TargetRegisterInfo *TRI =
2477 getTargetMachine().getSubtargetImpl()->getRegisterInfo();
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002478 const uint32_t *Mask = TRI->getCallPreservedMask(CLI.CallConv);
2479 assert(Mask && "Missing call preserved mask for calling convention");
Eric Christopher1c29a652014-07-18 22:55:25 +00002480 if (Subtarget.inMips16HardFloat()) {
Reed Kotler783c7942013-05-10 22:25:39 +00002481 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
2482 llvm::StringRef Sym = G->getGlobal()->getName();
2483 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
Reed Kotler3230e722013-12-12 02:41:11 +00002484 if (F && F->hasFnAttribute("__Mips16RetHelper")) {
Reed Kotler783c7942013-05-10 22:25:39 +00002485 Mask = MipsRegisterInfo::getMips16RetHelperMask();
2486 }
2487 }
2488 }
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002489 Ops.push_back(CLI.DAG.getRegisterMask(Mask));
2490
2491 if (InFlag.getNode())
2492 Ops.push_back(InFlag);
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002493}
2494
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002495/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman624801e2009-01-26 03:15:54 +00002496/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002497SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00002498MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002499 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00002500 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002501 SDLoc DL = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00002502 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
2503 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
2504 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Akira Hatanakabeda2242012-07-31 18:46:41 +00002505 SDValue Chain = CLI.Chain;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002506 SDValue Callee = CLI.Callee;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002507 bool &IsTailCall = CLI.IsTailCall;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002508 CallingConv::ID CallConv = CLI.CallConv;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002509 bool IsVarArg = CLI.IsVarArg;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002510
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002511 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002512 MachineFrameInfo *MFI = MF.getFrameInfo();
Eric Christopherfc6de422014-08-05 02:39:49 +00002513 const TargetFrameLowering *TFL = MF.getSubtarget().getFrameLowering();
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002514 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +00002515 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002516
2517 // Analyze operands of the call, assigning locations to each operand.
2518 SmallVector<CCValAssign, 16> ArgLocs;
Daniel Sanders41a64c42014-11-07 11:10:48 +00002519 MipsCCState CCInfo(
2520 CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs, *DAG.getContext(),
2521 MipsCCState::getSpecialCallingConvForCallee(Callee.getNode(), Subtarget));
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002522
2523 // Allocate the reserved argument area. It seems strange to do this from the
2524 // caller side but removing it breaks the frame size calculation.
2525 const MipsABIInfo &ABI = Subtarget.getABI();
2526 CCInfo.AllocateStack(ABI.GetCalleeAllocdArgSizeInBytes(CallConv), 1);
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002527
Daniel Sanderscfad1e32014-11-07 11:43:49 +00002528 CCInfo.AnalyzeCallOperands(Outs, CC_Mips, CLI.getArgs(), Callee.getNode());
Wesley Peck527da1b2010-11-23 03:31:01 +00002529
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002530 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka195a1e22011-06-08 17:39:33 +00002531 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka97ba7692012-07-26 23:27:01 +00002532
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002533 // Check if it's really possible to do a tail call.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002534 if (IsTailCall)
Daniel Sanders23e98772014-11-02 16:09:29 +00002535 IsTailCall = isEligibleForTailCallOptimization(
2536 CCInfo, NextStackOffset, *MF.getInfo<MipsFunctionInfo>());
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002537
Reid Kleckner5772b772014-04-24 20:14:34 +00002538 if (!IsTailCall && CLI.CS && CLI.CS->isMustTailCall())
2539 report_fatal_error("failed to perform tail call elimination on a call "
2540 "site marked musttail");
2541
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002542 if (IsTailCall)
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002543 ++NumTailCalls;
2544
Akira Hatanaka79738332011-09-19 20:26:02 +00002545 // Chain is the output chain of the last Load/Store or CopyToReg node.
2546 // ByValChain is the output chain of the last Memcpy node created for copying
2547 // byval arguments to the stack.
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002548 unsigned StackAlignment = TFL->getStackAlignment();
2549 NextStackOffset = RoundUpToAlignment(NextStackOffset, StackAlignment);
Akira Hatanaka79738332011-09-19 20:26:02 +00002550 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002551
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002552 if (!IsTailCall)
Andrew Trickad6d08a2013-05-29 22:03:55 +00002553 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffsetVal, DL);
Akira Hatanakabeda2242012-07-31 18:46:41 +00002554
Daniel Sandersd897b562014-03-27 10:46:12 +00002555 SDValue StackPtr = DAG.getCopyFromReg(
Eric Christopher1c29a652014-07-18 22:55:25 +00002556 Chain, DL, Subtarget.isABI_N64() ? Mips::SP_64 : Mips::SP,
Eric Christopherbf33a3c2014-07-02 23:18:40 +00002557 getPointerTy());
Akira Hatanaka195a1e22011-06-08 17:39:33 +00002558
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002559 // With EABI is it possible to have 16 args on registers.
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002560 std::deque< std::pair<unsigned, SDValue> > RegsToPass;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002561 SmallVector<SDValue, 8> MemOpChains;
Daniel Sanders23e98772014-11-02 16:09:29 +00002562
2563 CCInfo.rewindByValRegsInfo();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002564
2565 // Walk the register/memloc assignments, inserting copies/loads.
2566 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002567 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002568 CCValAssign &VA = ArgLocs[i];
Akira Hatanakab20a3252011-10-28 19:49:00 +00002569 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka19891f82011-11-12 02:34:50 +00002570 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002571 bool UseUpperBits = false;
Akira Hatanaka19891f82011-11-12 02:34:50 +00002572
2573 // ByVal Arg.
2574 if (Flags.isByVal()) {
Daniel Sanders23e98772014-11-02 16:09:29 +00002575 unsigned FirstByValReg, LastByValReg;
2576 unsigned ByValIdx = CCInfo.getInRegsParamsProcessed();
2577 CCInfo.getInRegsParamInfo(ByValIdx, FirstByValReg, LastByValReg);
2578
Akira Hatanaka19891f82011-11-12 02:34:50 +00002579 assert(Flags.getByValSize() &&
2580 "ByVal args of size 0 should have been ignored by front-end.");
Daniel Sanders23e98772014-11-02 16:09:29 +00002581 assert(ByValIdx < CCInfo.getInRegsParamsCount());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002582 assert(!IsTailCall &&
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002583 "Do not tail-call optimize if there is a byval argument.");
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002584 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002585 FirstByValReg, LastByValReg, Flags, Subtarget.isLittle(),
2586 VA);
Daniel Sanders23e98772014-11-02 16:09:29 +00002587 CCInfo.nextInRegsParam();
Akira Hatanaka19891f82011-11-12 02:34:50 +00002588 continue;
2589 }
Jia Liuf54f60f2012-02-28 07:46:26 +00002590
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002591 // Promote the value if needed.
2592 switch (VA.getLocInfo()) {
Daniel Sandersc43cda82014-11-07 16:54:21 +00002593 default:
2594 llvm_unreachable("Unknown loc info!");
Wesley Peck527da1b2010-11-23 03:31:01 +00002595 case CCValAssign::Full:
Akira Hatanakab20a3252011-10-28 19:49:00 +00002596 if (VA.isRegLoc()) {
2597 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
Akira Hatanaka3b7391d2013-03-05 22:20:28 +00002598 (ValVT == MVT::f64 && LocVT == MVT::i64) ||
2599 (ValVT == MVT::i64 && LocVT == MVT::f64))
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002600 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
Akira Hatanakab20a3252011-10-28 19:49:00 +00002601 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002602 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanakae2489122011-04-15 21:51:11 +00002603 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002604 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanakaaef55c82011-04-15 21:00:26 +00002605 Arg, DAG.getConstant(1, MVT::i32));
Eric Christopher1c29a652014-07-18 22:55:25 +00002606 if (!Subtarget.isLittle())
Akira Hatanaka27916972011-04-15 19:52:08 +00002607 std::swap(Lo, Hi);
Jia Liuf54f60f2012-02-28 07:46:26 +00002608 unsigned LocRegLo = VA.getLocReg();
Akira Hatanaka61bbcce2011-09-23 00:58:33 +00002609 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2610 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2611 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002612 continue;
Wesley Peck527da1b2010-11-23 03:31:01 +00002613 }
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002614 }
2615 break;
Daniel Sanders23e98772014-11-02 16:09:29 +00002616 case CCValAssign::BCvt:
2617 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
2618 break;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002619 case CCValAssign::SExtUpper:
2620 UseUpperBits = true;
2621 // Fallthrough
Chris Lattner52f16de2008-03-17 06:57:02 +00002622 case CCValAssign::SExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002623 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002624 break;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002625 case CCValAssign::ZExtUpper:
2626 UseUpperBits = true;
2627 // Fallthrough
Chris Lattner52f16de2008-03-17 06:57:02 +00002628 case CCValAssign::ZExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002629 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002630 break;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002631 case CCValAssign::AExtUpper:
2632 UseUpperBits = true;
2633 // Fallthrough
Chris Lattner52f16de2008-03-17 06:57:02 +00002634 case CCValAssign::AExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002635 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002636 break;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002637 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002638
Daniel Sandersc43cda82014-11-07 16:54:21 +00002639 if (UseUpperBits) {
2640 unsigned ValSizeInBits = Outs[i].ArgVT.getSizeInBits();
2641 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
2642 Arg = DAG.getNode(
2643 ISD::SHL, DL, VA.getLocVT(), Arg,
2644 DAG.getConstant(LocSizeInBits - ValSizeInBits, VA.getLocVT()));
2645 }
2646
Wesley Peck527da1b2010-11-23 03:31:01 +00002647 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +00002648 // RegsToPass vector
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002649 if (VA.isRegLoc()) {
2650 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattner52f16de2008-03-17 06:57:02 +00002651 continue;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002652 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002653
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002654 // Register can't get to this point...
Chris Lattner52f16de2008-03-17 06:57:02 +00002655 assert(VA.isMemLoc());
Wesley Peck527da1b2010-11-23 03:31:01 +00002656
Wesley Peck527da1b2010-11-23 03:31:01 +00002657 // emit ISD::STORE whichs stores the
Chris Lattner52f16de2008-03-17 06:57:02 +00002658 // parameter value to a stack Location
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002659 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002660 Chain, Arg, DL, IsTailCall, DAG));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002661 }
2662
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002663 // Transform all store nodes into one single node because all store
2664 // nodes are independent of each other.
Wesley Peck527da1b2010-11-23 03:31:01 +00002665 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00002666 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002667
Bill Wendling24c79f22008-09-16 21:48:12 +00002668 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peck527da1b2010-11-23 03:31:01 +00002669 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2670 // node so that legalize doesn't hack it.
Eric Christopherbf33a3c2014-07-02 23:18:40 +00002671 bool IsPICCall =
Eric Christopher1c29a652014-07-18 22:55:25 +00002672 (Subtarget.isABI_N64() || IsPIC); // true if calls are translated to
Eric Christopherbf33a3c2014-07-02 23:18:40 +00002673 // jalr $25
Sasa Stankovic7072a792014-10-01 08:22:21 +00002674 bool GlobalOrExternal = false, InternalLinkage = false, IsCallReloc = false;
Akira Hatanakad6f1c582011-04-07 19:51:44 +00002675 SDValue CalleeLo;
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002676 EVT Ty = Callee.getValueType();
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002677
2678 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002679 if (IsPICCall) {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002680 const GlobalValue *Val = G->getGlobal();
2681 InternalLinkage = Val->hasInternalLinkage();
Akira Hatanakacf9a61b2012-12-13 03:17:29 +00002682
2683 if (InternalLinkage)
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002684 Callee = getAddrLocal(G, DL, Ty, DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +00002685 Subtarget.isABI_N32() || Subtarget.isABI_N64());
Sasa Stankovic7072a792014-10-01 08:22:21 +00002686 else if (LargeGOT) {
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002687 Callee = getAddrGlobalLargeGOT(G, DL, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002688 MipsII::MO_CALL_LO16, Chain,
2689 FuncInfo->callPtrInfo(Val));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002690 IsCallReloc = true;
2691 } else {
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002692 Callee = getAddrGlobal(G, DL, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002693 FuncInfo->callPtrInfo(Val));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002694 IsCallReloc = true;
2695 }
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002696 } else
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002697 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, getPointerTy(), 0,
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002698 MipsII::MO_NO_FLAG);
Akira Hatanaka8e16aac2011-12-09 01:45:12 +00002699 GlobalOrExternal = true;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002700 }
2701 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002702 const char *Sym = S->getSymbol();
2703
Eric Christopher1c29a652014-07-18 22:55:25 +00002704 if (!Subtarget.isABI_N64() && !IsPIC) // !N64 && static
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002705 Callee =
2706 DAG.getTargetExternalSymbol(Sym, getPointerTy(), MipsII::MO_NO_FLAG);
Sasa Stankovic7072a792014-10-01 08:22:21 +00002707 else if (LargeGOT) {
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002708 Callee = getAddrGlobalLargeGOT(S, DL, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002709 MipsII::MO_CALL_LO16, Chain,
2710 FuncInfo->callPtrInfo(Sym));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002711 IsCallReloc = true;
2712 } else { // N64 || PIC
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002713 Callee = getAddrGlobal(S, DL, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002714 FuncInfo->callPtrInfo(Sym));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002715 IsCallReloc = true;
2716 }
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002717
Akira Hatanaka8e16aac2011-12-09 01:45:12 +00002718 GlobalOrExternal = true;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002719 }
2720
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002721 SmallVector<SDValue, 8> Ops(1, Chain);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002722 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002723
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002724 getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal, InternalLinkage,
Sasa Stankovic7072a792014-10-01 08:22:21 +00002725 IsCallReloc, CLI, Callee, Chain);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002726
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002727 if (IsTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +00002728 return DAG.getNode(MipsISD::TailCall, DL, MVT::Other, Ops);
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002729
Craig Topper48d114b2014-04-26 18:35:24 +00002730 Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, Ops);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002731 SDValue InFlag = Chain.getValue(1);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002732
Bruno Cardoso Lopes193e64c2010-01-30 18:32:07 +00002733 // Create the CALLSEQ_END node.
Akira Hatanaka97ba7692012-07-26 23:27:01 +00002734 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal,
Andrew Trickad6d08a2013-05-29 22:03:55 +00002735 DAG.getIntPtrConstant(0, true), InFlag, DL);
Bruno Cardoso Lopes193e64c2010-01-30 18:32:07 +00002736 InFlag = Chain.getValue(1);
2737
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002738 // Handle result values, copying them out of physregs into vregs that we
2739 // return.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002740 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg, Ins, DL, DAG,
2741 InVals, CLI);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002742}
2743
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002744/// LowerCallResult - Lower the result values of a call into the
2745/// appropriate copies out of appropriate physical registers.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002746SDValue MipsTargetLowering::LowerCallResult(
2747 SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool IsVarArg,
2748 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG,
2749 SmallVectorImpl<SDValue> &InVals,
2750 TargetLowering::CallLoweringInfo &CLI) const {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002751 // Assign locations to each value returned by this call.
2752 SmallVector<CCValAssign, 16> RVLocs;
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002753 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
2754 *DAG.getContext());
2755 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips, CLI);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002756
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002757 // Copy all of the result registers out of their specified physreg.
2758 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Daniel Sandersae275e32014-09-25 12:15:05 +00002759 CCValAssign &VA = RVLocs[i];
2760 assert(VA.isRegLoc() && "Can only return in registers!");
2761
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002762 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(),
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002763 RVLocs[i].getLocVT(), InFlag);
2764 Chain = Val.getValue(1);
2765 InFlag = Val.getValue(2);
2766
Daniel Sandersae275e32014-09-25 12:15:05 +00002767 if (VA.isUpperBitsInLoc()) {
2768 unsigned ValSizeInBits = Ins[i].ArgVT.getSizeInBits();
2769 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
2770 unsigned Shift =
2771 VA.getLocInfo() == CCValAssign::ZExtUpper ? ISD::SRL : ISD::SRA;
2772 Val = DAG.getNode(
2773 Shift, DL, VA.getLocVT(), Val,
2774 DAG.getConstant(LocSizeInBits - ValSizeInBits, VA.getLocVT()));
2775 }
2776
2777 switch (VA.getLocInfo()) {
2778 default:
2779 llvm_unreachable("Unknown loc info!");
2780 case CCValAssign::Full:
2781 break;
2782 case CCValAssign::BCvt:
2783 Val = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Val);
2784 break;
2785 case CCValAssign::AExt:
2786 case CCValAssign::AExtUpper:
2787 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2788 break;
2789 case CCValAssign::ZExt:
2790 case CCValAssign::ZExtUpper:
2791 Val = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Val,
2792 DAG.getValueType(VA.getValVT()));
2793 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2794 break;
2795 case CCValAssign::SExt:
2796 case CCValAssign::SExtUpper:
2797 Val = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Val,
2798 DAG.getValueType(VA.getValVT()));
2799 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2800 break;
2801 }
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002802
2803 InVals.push_back(Val);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002804 }
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +00002805
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002806 return Chain;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002807}
2808
Daniel Sandersc43cda82014-11-07 16:54:21 +00002809static SDValue UnpackFromArgumentSlot(SDValue Val, const CCValAssign &VA,
2810 EVT ArgVT, SDLoc DL, SelectionDAG &DAG) {
2811 MVT LocVT = VA.getLocVT();
2812 EVT ValVT = VA.getValVT();
2813
2814 // Shift into the upper bits if necessary.
2815 switch (VA.getLocInfo()) {
2816 default:
2817 break;
2818 case CCValAssign::AExtUpper:
2819 case CCValAssign::SExtUpper:
2820 case CCValAssign::ZExtUpper: {
2821 unsigned ValSizeInBits = ArgVT.getSizeInBits();
2822 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
2823 unsigned Opcode =
2824 VA.getLocInfo() == CCValAssign::ZExtUpper ? ISD::SRL : ISD::SRA;
2825 Val = DAG.getNode(
2826 Opcode, DL, VA.getLocVT(), Val,
2827 DAG.getConstant(LocSizeInBits - ValSizeInBits, VA.getLocVT()));
2828 break;
2829 }
2830 }
2831
2832 // If this is an value smaller than the argument slot size (32-bit for O32,
2833 // 64-bit for N32/N64), it has been promoted in some way to the argument slot
2834 // size. Extract the value and insert any appropriate assertions regarding
2835 // sign/zero extension.
2836 switch (VA.getLocInfo()) {
2837 default:
2838 llvm_unreachable("Unknown loc info!");
2839 case CCValAssign::Full:
2840 break;
2841 case CCValAssign::AExtUpper:
2842 case CCValAssign::AExt:
2843 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
2844 break;
2845 case CCValAssign::SExtUpper:
2846 case CCValAssign::SExt:
2847 Val = DAG.getNode(ISD::AssertSext, DL, LocVT, Val, DAG.getValueType(ValVT));
2848 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
2849 break;
2850 case CCValAssign::ZExtUpper:
2851 case CCValAssign::ZExt:
2852 Val = DAG.getNode(ISD::AssertZext, DL, LocVT, Val, DAG.getValueType(ValVT));
2853 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
2854 break;
2855 case CCValAssign::BCvt:
2856 Val = DAG.getNode(ISD::BITCAST, DL, ValVT, Val);
2857 break;
2858 }
2859
2860 return Val;
2861}
2862
Akira Hatanakae2489122011-04-15 21:51:11 +00002863//===----------------------------------------------------------------------===//
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002864// Formal Arguments Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002865//===----------------------------------------------------------------------===//
Wesley Peck527da1b2010-11-23 03:31:01 +00002866/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002867/// and generate load operations for arguments places on the stack.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002868SDValue
2869MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanakaaef55c82011-04-15 21:00:26 +00002870 CallingConv::ID CallConv,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002871 bool IsVarArg,
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00002872 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002873 SDLoc DL, SelectionDAG &DAG,
Akira Hatanakaaef55c82011-04-15 21:00:26 +00002874 SmallVectorImpl<SDValue> &InVals)
Akira Hatanakae2489122011-04-15 21:51:11 +00002875 const {
Bruno Cardoso Lopesa01ede22008-08-04 07:12:52 +00002876 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002877 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopes14033fb2007-08-28 05:08:16 +00002878 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002879
Dan Gohman31ae5862010-04-17 14:41:14 +00002880 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002881
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002882 // Used with vargs to acumulate store chains.
2883 std::vector<SDValue> OutChains;
2884
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002885 // Assign locations to all of the incoming arguments.
2886 SmallVector<CCValAssign, 16> ArgLocs;
Daniel Sanders23e98772014-11-02 16:09:29 +00002887 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
2888 *DAG.getContext());
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002889 const MipsABIInfo &ABI = Subtarget.getABI();
2890 CCInfo.AllocateStack(ABI.GetCalleeAllocdArgSizeInBytes(CallConv), 1);
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002891 Function::const_arg_iterator FuncArg =
2892 DAG.getMachineFunction().getFunction()->arg_begin();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002893
Daniel Sandersb70e27c2014-11-06 16:36:30 +00002894 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips_FixedArg);
Akira Hatanaka4866fe12012-10-30 19:37:25 +00002895 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(),
Daniel Sanders23e98772014-11-02 16:09:29 +00002896 CCInfo.getInRegsParamsCount() > 0);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002897
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00002898 unsigned CurArgIdx = 0;
Daniel Sanders23e98772014-11-02 16:09:29 +00002899 CCInfo.rewindByValRegsInfo();
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002900
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00002901 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002902 CCValAssign &VA = ArgLocs[i];
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00002903 std::advance(FuncArg, Ins[i].OrigArgIndex - CurArgIdx);
2904 CurArgIdx = Ins[i].OrigArgIndex;
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002905 EVT ValVT = VA.getValVT();
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002906 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2907 bool IsRegLoc = VA.isRegLoc();
2908
2909 if (Flags.isByVal()) {
Daniel Sanders23e98772014-11-02 16:09:29 +00002910 unsigned FirstByValReg, LastByValReg;
2911 unsigned ByValIdx = CCInfo.getInRegsParamsProcessed();
2912 CCInfo.getInRegsParamInfo(ByValIdx, FirstByValReg, LastByValReg);
2913
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002914 assert(Flags.getByValSize() &&
2915 "ByVal args of size 0 should have been ignored by front-end.");
Daniel Sanders23e98772014-11-02 16:09:29 +00002916 assert(ByValIdx < CCInfo.getInRegsParamsCount());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002917 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002918 FirstByValReg, LastByValReg, VA, CCInfo);
Daniel Sanders23e98772014-11-02 16:09:29 +00002919 CCInfo.nextInRegsParam();
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002920 continue;
2921 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002922
2923 // Arguments stored on registers
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002924 if (IsRegLoc) {
Akira Hatanaka7d822522013-10-28 21:21:36 +00002925 MVT RegVT = VA.getLocVT();
Akira Hatanakacb4a1a82011-05-24 00:23:52 +00002926 unsigned ArgReg = VA.getLocReg();
Akira Hatanaka7d822522013-10-28 21:21:36 +00002927 const TargetRegisterClass *RC = getRegClassFor(RegVT);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002928
Wesley Peck527da1b2010-11-23 03:31:01 +00002929 // Transform the arguments stored on
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002930 // physical registers into virtual ones
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002931 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC);
2932 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
Wesley Peck527da1b2010-11-23 03:31:01 +00002933
Daniel Sandersc43cda82014-11-07 16:54:21 +00002934 ArgValue = UnpackFromArgumentSlot(ArgValue, VA, Ins[i].ArgVT, DL, DAG);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002935
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002936 // Handle floating point arguments passed in integer registers and
2937 // long double arguments passed in floating point registers.
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002938 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002939 (RegVT == MVT::i64 && ValVT == MVT::f64) ||
2940 (RegVT == MVT::f64 && ValVT == MVT::i64))
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002941 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
Eric Christopher1c29a652014-07-18 22:55:25 +00002942 else if (Subtarget.isABI_O32() && RegVT == MVT::i32 &&
Eric Christopherbf33a3c2014-07-02 23:18:40 +00002943 ValVT == MVT::f64) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002944 unsigned Reg2 = addLiveIn(DAG.getMachineFunction(),
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002945 getNextIntArgReg(ArgReg), RC);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002946 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT);
Eric Christopher1c29a652014-07-18 22:55:25 +00002947 if (!Subtarget.isLittle())
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002948 std::swap(ArgValue, ArgValue2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002949 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64,
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002950 ArgValue, ArgValue2);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002951 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002952
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002953 InVals.push_back(ArgValue);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002954 } else { // VA.isRegLoc()
Daniel Sandersc43cda82014-11-07 16:54:21 +00002955 MVT LocVT = VA.getLocVT();
2956
2957 if (Subtarget.isABI_O32()) {
2958 // We ought to be able to use LocVT directly but O32 sets it to i32
2959 // when allocating floating point values to integer registers.
2960 // This shouldn't influence how we load the value into registers unless
2961 // we are targetting softfloat.
2962 if (VA.getValVT().isFloatingPoint() && !Subtarget.abiUsesSoftFloat())
2963 LocVT = VA.getValVT();
2964 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002965
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002966 // sanity check
2967 assert(VA.isMemLoc());
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002968
Wesley Peck527da1b2010-11-23 03:31:01 +00002969 // The stack pointer offset is relative to the caller stack frame.
Daniel Sandersc43cda82014-11-07 16:54:21 +00002970 int FI = MFI->CreateFixedObject(LocVT.getSizeInBits() / 8,
Akira Hatanakacb4a1a82011-05-24 00:23:52 +00002971 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002972
2973 // Create load nodes to retrieve arguments from the stack
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002974 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Daniel Sandersc43cda82014-11-07 16:54:21 +00002975 SDValue ArgValue = DAG.getLoad(LocVT, DL, Chain, FIN,
2976 MachinePointerInfo::getFixedStack(FI),
2977 false, false, false, 0);
2978 OutChains.push_back(ArgValue.getValue(1));
2979
2980 ArgValue = UnpackFromArgumentSlot(ArgValue, VA, Ins[i].ArgVT, DL, DAG);
2981
2982 InVals.push_back(ArgValue);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002983 }
Reid Kleckner7a59e082014-05-12 22:01:27 +00002984 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002985
Reid Kleckner7a59e082014-05-12 22:01:27 +00002986 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Reid Kleckner79418562014-05-09 22:32:13 +00002987 // The mips ABIs for returning structs by value requires that we copy
2988 // the sret argument into $v0 for the return. Save the argument into
2989 // a virtual register so that we can access it from the return points.
Reid Kleckner7a59e082014-05-12 22:01:27 +00002990 if (Ins[i].Flags.isSRet()) {
Reid Kleckner79418562014-05-09 22:32:13 +00002991 unsigned Reg = MipsFI->getSRetReturnReg();
2992 if (!Reg) {
2993 Reg = MF.getRegInfo().createVirtualRegister(
Eric Christopher1c29a652014-07-18 22:55:25 +00002994 getRegClassFor(Subtarget.isABI_N64() ? MVT::i64 : MVT::i32));
Reid Kleckner79418562014-05-09 22:32:13 +00002995 MipsFI->setSRetReturnReg(Reg);
2996 }
Reid Kleckner7a59e082014-05-12 22:01:27 +00002997 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[i]);
Reid Kleckner79418562014-05-09 22:32:13 +00002998 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
Reid Kleckner7a59e082014-05-12 22:01:27 +00002999 break;
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003000 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003001 }
3002
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003003 if (IsVarArg)
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003004 writeVarArgRegs(OutChains, Chain, DL, DAG, CCInfo);
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003005
Wesley Peck527da1b2010-11-23 03:31:01 +00003006 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003007 // the size of Ins and InVals. This only happens when on varg functions
3008 if (!OutChains.empty()) {
3009 OutChains.push_back(Chain);
Craig Topper48d114b2014-04-26 18:35:24 +00003010 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003011 }
3012
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003013 return Chain;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003014}
3015
Akira Hatanakae2489122011-04-15 21:51:11 +00003016//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003017// Return Value Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00003018//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003019
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003020bool
3021MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003022 MachineFunction &MF, bool IsVarArg,
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003023 const SmallVectorImpl<ISD::OutputArg> &Outs,
3024 LLVMContext &Context) const {
3025 SmallVector<CCValAssign, 16> RVLocs;
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003026 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003027 return CCInfo.CheckReturn(Outs, RetCC_Mips);
3028}
3029
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003030SDValue
3031MipsTargetLowering::LowerReturn(SDValue Chain,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003032 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003033 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003034 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003035 SDLoc DL, SelectionDAG &DAG) const {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003036 // CCValAssign - represent the assignment of
3037 // the return value to a location
3038 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003039 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003040
3041 // CCState - Info about the registers and stack slot.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003042 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, *DAG.getContext());
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003043
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003044 // Analyze return values.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003045 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003046
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003047 SDValue Flag;
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003048 SmallVector<SDValue, 4> RetOps(1, Chain);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003049
3050 // Copy the result values into the output registers.
3051 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003052 SDValue Val = OutVals[i];
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003053 CCValAssign &VA = RVLocs[i];
3054 assert(VA.isRegLoc() && "Can only return in registers!");
Daniel Sandersae275e32014-09-25 12:15:05 +00003055 bool UseUpperBits = false;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003056
Daniel Sandersae275e32014-09-25 12:15:05 +00003057 switch (VA.getLocInfo()) {
3058 default:
3059 llvm_unreachable("Unknown loc info!");
3060 case CCValAssign::Full:
3061 break;
3062 case CCValAssign::BCvt:
3063 Val = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Val);
3064 break;
3065 case CCValAssign::AExtUpper:
3066 UseUpperBits = true;
3067 // Fallthrough
3068 case CCValAssign::AExt:
3069 Val = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Val);
3070 break;
3071 case CCValAssign::ZExtUpper:
3072 UseUpperBits = true;
3073 // Fallthrough
3074 case CCValAssign::ZExt:
3075 Val = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Val);
3076 break;
3077 case CCValAssign::SExtUpper:
3078 UseUpperBits = true;
3079 // Fallthrough
3080 case CCValAssign::SExt:
3081 Val = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Val);
3082 break;
3083 }
3084
3085 if (UseUpperBits) {
3086 unsigned ValSizeInBits = Outs[i].ArgVT.getSizeInBits();
3087 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
3088 Val = DAG.getNode(
3089 ISD::SHL, DL, VA.getLocVT(), Val,
3090 DAG.getConstant(LocSizeInBits - ValSizeInBits, VA.getLocVT()));
3091 }
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003092
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003093 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003094
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003095 // Guarantee that all emitted copies are stuck together with flags.
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003096 Flag = Chain.getValue(1);
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003097 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003098 }
3099
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003100 // The mips ABIs for returning structs by value requires that we copy
3101 // the sret argument into $v0 for the return. We saved the argument into
3102 // a virtual register in the entry block, so now we copy the value out
3103 // and into $v0.
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003104 if (MF.getFunction()->hasStructRetAttr()) {
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003105 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3106 unsigned Reg = MipsFI->getSRetReturnReg();
3107
Wesley Peck527da1b2010-11-23 03:31:01 +00003108 if (!Reg)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003109 llvm_unreachable("sret virtual register not created in the entry block");
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003110 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Eric Christopher1c29a652014-07-18 22:55:25 +00003111 unsigned V0 = Subtarget.isABI_N64() ? Mips::V0_64 : Mips::V0;
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003112
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003113 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003114 Flag = Chain.getValue(1);
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003115 RetOps.push_back(DAG.getRegister(V0, getPointerTy()));
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003116 }
3117
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003118 RetOps[0] = Chain; // Update chain.
Akira Hatanakaefff7b72012-07-10 00:19:06 +00003119
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003120 // Add the flag if we have it.
3121 if (Flag.getNode())
3122 RetOps.push_back(Flag);
3123
3124 // Return on Mips is always a "jr $ra"
Craig Topper48d114b2014-04-26 18:35:24 +00003125 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, RetOps);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003126}
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003127
Akira Hatanakae2489122011-04-15 21:51:11 +00003128//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003129// Mips Inline Assembly Support
Akira Hatanakae2489122011-04-15 21:51:11 +00003130//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003131
3132/// getConstraintType - Given a constraint letter, return the type of
3133/// constraint it is for this target.
3134MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peck527da1b2010-11-23 03:31:01 +00003135getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003136{
Daniel Sanders8b59af12013-11-12 12:56:01 +00003137 // Mips specific constraints
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003138 // GCC config/mips/constraints.md
3139 //
Wesley Peck527da1b2010-11-23 03:31:01 +00003140 // 'd' : An address register. Equivalent to r
3141 // unless generating MIPS16 code.
3142 // 'y' : Equivalent to r; retained for
3143 // backwards compatibility.
Eric Christophere3c494d2012-05-07 06:25:10 +00003144 // 'c' : A register suitable for use in an indirect
3145 // jump. This will always be $25 for -mabicalls.
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003146 // 'l' : The lo register. 1 word storage.
3147 // 'x' : The hilo register pair. Double word storage.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003148 if (Constraint.size() == 1) {
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003149 switch (Constraint[0]) {
3150 default : break;
Wesley Peck527da1b2010-11-23 03:31:01 +00003151 case 'd':
3152 case 'y':
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003153 case 'f':
Eric Christophere3c494d2012-05-07 06:25:10 +00003154 case 'c':
Eric Christopher9c492e62012-05-07 06:25:15 +00003155 case 'l':
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003156 case 'x':
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003157 return C_RegisterClass;
Jack Carter0e149b02013-03-04 21:33:15 +00003158 case 'R':
3159 return C_Memory;
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003160 }
3161 }
3162 return TargetLowering::getConstraintType(Constraint);
3163}
3164
John Thompsone8360b72010-10-29 17:29:13 +00003165/// Examine constraint type and operand type and determine a weight value.
3166/// This object must already have been set up with the operand type
3167/// and the current alternative constraint selected.
3168TargetLowering::ConstraintWeight
3169MipsTargetLowering::getSingleConstraintMatchWeight(
3170 AsmOperandInfo &info, const char *constraint) const {
3171 ConstraintWeight weight = CW_Invalid;
3172 Value *CallOperandVal = info.CallOperandVal;
3173 // If we don't have a value, we can't do a match,
3174 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +00003175 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +00003176 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +00003177 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +00003178 // Look at the constraint type.
3179 switch (*constraint) {
3180 default:
3181 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
3182 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00003183 case 'd':
3184 case 'y':
John Thompsone8360b72010-10-29 17:29:13 +00003185 if (type->isIntegerTy())
3186 weight = CW_Register;
3187 break;
Daniel Sanders8b59af12013-11-12 12:56:01 +00003188 case 'f': // FPU or MSA register
Eric Christopher1c29a652014-07-18 22:55:25 +00003189 if (Subtarget.hasMSA() && type->isVectorTy() &&
Daniel Sanders8b59af12013-11-12 12:56:01 +00003190 cast<VectorType>(type)->getBitWidth() == 128)
3191 weight = CW_Register;
3192 else if (type->isFloatTy())
John Thompsone8360b72010-10-29 17:29:13 +00003193 weight = CW_Register;
3194 break;
Eric Christophere3c494d2012-05-07 06:25:10 +00003195 case 'c': // $25 for indirect jumps
Eric Christopher9c492e62012-05-07 06:25:15 +00003196 case 'l': // lo register
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003197 case 'x': // hilo register pair
Daniel Sanders8b59af12013-11-12 12:56:01 +00003198 if (type->isIntegerTy())
Eric Christophere3c494d2012-05-07 06:25:10 +00003199 weight = CW_SpecificReg;
Daniel Sanders8b59af12013-11-12 12:56:01 +00003200 break;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003201 case 'I': // signed 16 bit immediate
Eric Christopher7201e1b2012-05-07 03:13:42 +00003202 case 'J': // integer zero
Eric Christopher3ff88a02012-05-07 05:46:29 +00003203 case 'K': // unsigned 16 bit immediate
Eric Christopher1109b342012-05-07 05:46:37 +00003204 case 'L': // signed 32 bit immediate where lower 16 bits are 0
Eric Christophere07aa432012-05-07 05:46:43 +00003205 case 'N': // immediate in the range of -65535 to -1 (inclusive)
Eric Christopher470578a2012-05-07 05:46:48 +00003206 case 'O': // signed 15 bit immediate (+- 16383)
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003207 case 'P': // immediate in the range of 65535 to 1 (inclusive)
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003208 if (isa<ConstantInt>(CallOperandVal))
3209 weight = CW_Constant;
3210 break;
Jack Carter0e149b02013-03-04 21:33:15 +00003211 case 'R':
3212 weight = CW_Memory;
3213 break;
John Thompsone8360b72010-10-29 17:29:13 +00003214 }
3215 return weight;
3216}
3217
Akira Hatanaka7473b472013-08-14 00:21:25 +00003218/// This is a helper function to parse a physical register string and split it
3219/// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag
3220/// that is returned indicates whether parsing was successful. The second flag
3221/// is true if the numeric part exists.
3222static std::pair<bool, bool>
Craig Topper6dc4a8bc2014-08-30 16:48:02 +00003223parsePhysicalReg(StringRef C, std::string &Prefix,
Akira Hatanaka7473b472013-08-14 00:21:25 +00003224 unsigned long long &Reg) {
3225 if (C.front() != '{' || C.back() != '}')
3226 return std::make_pair(false, false);
3227
3228 // Search for the first numeric character.
3229 StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1;
3230 I = std::find_if(B, E, std::ptr_fun(isdigit));
3231
3232 Prefix.assign(B, I - B);
3233
3234 // The second flag is set to false if no numeric characters were found.
3235 if (I == E)
3236 return std::make_pair(true, false);
3237
3238 // Parse the numeric characters.
3239 return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg),
3240 true);
3241}
3242
3243std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering::
Craig Topper6dc4a8bc2014-08-30 16:48:02 +00003244parseRegForInlineAsmConstraint(StringRef C, MVT VT) const {
Eric Christopherd9134482014-08-04 21:25:23 +00003245 const TargetRegisterInfo *TRI =
3246 getTargetMachine().getSubtargetImpl()->getRegisterInfo();
Akira Hatanaka7473b472013-08-14 00:21:25 +00003247 const TargetRegisterClass *RC;
3248 std::string Prefix;
3249 unsigned long long Reg;
3250
3251 std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg);
3252
3253 if (!R.first)
Craig Topper062a2ba2014-04-25 05:30:21 +00003254 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003255
3256 if ((Prefix == "hi" || Prefix == "lo")) { // Parse hi/lo.
3257 // No numeric characters follow "hi" or "lo".
3258 if (R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003259 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003260
3261 RC = TRI->getRegClass(Prefix == "hi" ?
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00003262 Mips::HI32RegClassID : Mips::LO32RegClassID);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003263 return std::make_pair(*(RC->begin()), RC);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003264 } else if (Prefix.compare(0, 4, "$msa") == 0) {
3265 // Parse $msa(ir|csr|access|save|modify|request|map|unmap)
3266
3267 // No numeric characters follow the name.
3268 if (R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003269 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003270
3271 Reg = StringSwitch<unsigned long long>(Prefix)
3272 .Case("$msair", Mips::MSAIR)
3273 .Case("$msacsr", Mips::MSACSR)
3274 .Case("$msaaccess", Mips::MSAAccess)
3275 .Case("$msasave", Mips::MSASave)
3276 .Case("$msamodify", Mips::MSAModify)
3277 .Case("$msarequest", Mips::MSARequest)
3278 .Case("$msamap", Mips::MSAMap)
3279 .Case("$msaunmap", Mips::MSAUnmap)
3280 .Default(0);
3281
3282 if (!Reg)
Craig Topper062a2ba2014-04-25 05:30:21 +00003283 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003284
3285 RC = TRI->getRegClass(Mips::MSACtrlRegClassID);
3286 return std::make_pair(Reg, RC);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003287 }
3288
3289 if (!R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003290 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003291
3292 if (Prefix == "$f") { // Parse $f0-$f31.
3293 // If the size of FP registers is 64-bit or Reg is an even number, select
3294 // the 64-bit register class. Otherwise, select the 32-bit register class.
3295 if (VT == MVT::Other)
Eric Christopher1c29a652014-07-18 22:55:25 +00003296 VT = (Subtarget.isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32;
Akira Hatanaka7473b472013-08-14 00:21:25 +00003297
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003298 RC = getRegClassFor(VT);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003299
3300 if (RC == &Mips::AFGR64RegClass) {
3301 assert(Reg % 2 == 0);
3302 Reg >>= 1;
3303 }
Daniel Sanders8b59af12013-11-12 12:56:01 +00003304 } else if (Prefix == "$fcc") // Parse $fcc0-$fcc7.
Akira Hatanaka7473b472013-08-14 00:21:25 +00003305 RC = TRI->getRegClass(Mips::FCCRegClassID);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003306 else if (Prefix == "$w") { // Parse $w0-$w31.
3307 RC = getRegClassFor((VT == MVT::Other) ? MVT::v16i8 : VT);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003308 } else { // Parse $0-$31.
3309 assert(Prefix == "$");
3310 RC = getRegClassFor((VT == MVT::Other) ? MVT::i32 : VT);
3311 }
3312
3313 assert(Reg < RC->getNumRegs());
3314 return std::make_pair(*(RC->begin() + Reg), RC);
3315}
3316
Eric Christophereaf77dc2011-06-29 19:33:04 +00003317/// Given a register class constraint, like 'r', if this corresponds directly
3318/// to an LLVM register class, return a register of 0 and the register class
3319/// pointer.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003320std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Chad Rosier295bd432013-06-22 18:37:38 +00003321getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003322{
3323 if (Constraint.size() == 1) {
3324 switch (Constraint[0]) {
Eric Christopher9519c082011-06-29 19:04:31 +00003325 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
3326 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003327 case 'r':
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003328 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
Eric Christopher1c29a652014-07-18 22:55:25 +00003329 if (Subtarget.inMips16Mode())
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003330 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003331 return std::make_pair(0U, &Mips::GPR32RegClass);
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003332 }
Eric Christopher1c29a652014-07-18 22:55:25 +00003333 if (VT == MVT::i64 && !Subtarget.isGP64bit())
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003334 return std::make_pair(0U, &Mips::GPR32RegClass);
Eric Christopher1c29a652014-07-18 22:55:25 +00003335 if (VT == MVT::i64 && Subtarget.isGP64bit())
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003336 return std::make_pair(0U, &Mips::GPR64RegClass);
Eric Christopher58daf042012-05-07 03:13:22 +00003337 // This will generate an error message
Craig Topper062a2ba2014-04-25 05:30:21 +00003338 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003339 case 'f': // FPU or MSA register
3340 if (VT == MVT::v16i8)
3341 return std::make_pair(0U, &Mips::MSA128BRegClass);
3342 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
3343 return std::make_pair(0U, &Mips::MSA128HRegClass);
3344 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
3345 return std::make_pair(0U, &Mips::MSA128WRegClass);
3346 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
3347 return std::make_pair(0U, &Mips::MSA128DRegClass);
3348 else if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00003349 return std::make_pair(0U, &Mips::FGR32RegClass);
Eric Christopher1c29a652014-07-18 22:55:25 +00003350 else if ((VT == MVT::f64) && (!Subtarget.isSingleFloat())) {
3351 if (Subtarget.isFP64bit())
Craig Topperc7242e02012-04-20 07:30:17 +00003352 return std::make_pair(0U, &Mips::FGR64RegClass);
3353 return std::make_pair(0U, &Mips::AFGR64RegClass);
Akira Hatanakac669d7a2012-01-04 02:45:01 +00003354 }
Eric Christophere3c494d2012-05-07 06:25:10 +00003355 break;
3356 case 'c': // register suitable for indirect jump
3357 if (VT == MVT::i32)
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003358 return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass);
Eric Christophere3c494d2012-05-07 06:25:10 +00003359 assert(VT == MVT::i64 && "Unexpected type.");
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003360 return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass);
Eric Christopher9c492e62012-05-07 06:25:15 +00003361 case 'l': // register suitable for indirect jump
3362 if (VT == MVT::i32)
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00003363 return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass);
3364 return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass);
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003365 case 'x': // register suitable for indirect jump
3366 // Fixme: Not triggering the use of both hi and low
3367 // This will generate an error message
Craig Topper062a2ba2014-04-25 05:30:21 +00003368 return std::make_pair(0U, nullptr);
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003369 }
3370 }
Akira Hatanaka7473b472013-08-14 00:21:25 +00003371
3372 std::pair<unsigned, const TargetRegisterClass *> R;
3373 R = parseRegForInlineAsmConstraint(Constraint, VT);
3374
3375 if (R.second)
3376 return R;
3377
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003378 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3379}
3380
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003381/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3382/// vector. If it is invalid, don't add anything to Ops.
3383void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3384 std::string &Constraint,
3385 std::vector<SDValue>&Ops,
3386 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +00003387 SDValue Result;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003388
3389 // Only support length 1 constraints for now.
3390 if (Constraint.length() > 1) return;
3391
3392 char ConstraintLetter = Constraint[0];
3393 switch (ConstraintLetter) {
3394 default: break; // This will fall through to the generic implementation
3395 case 'I': // Signed 16 bit constant
3396 // If this fails, the parent routine will give an error
3397 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3398 EVT Type = Op.getValueType();
3399 int64_t Val = C->getSExtValue();
3400 if (isInt<16>(Val)) {
3401 Result = DAG.getTargetConstant(Val, Type);
3402 break;
3403 }
3404 }
3405 return;
Eric Christopher7201e1b2012-05-07 03:13:42 +00003406 case 'J': // integer zero
3407 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3408 EVT Type = Op.getValueType();
3409 int64_t Val = C->getZExtValue();
3410 if (Val == 0) {
3411 Result = DAG.getTargetConstant(0, Type);
3412 break;
3413 }
3414 }
3415 return;
Eric Christopher3ff88a02012-05-07 05:46:29 +00003416 case 'K': // unsigned 16 bit immediate
3417 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3418 EVT Type = Op.getValueType();
3419 uint64_t Val = (uint64_t)C->getZExtValue();
3420 if (isUInt<16>(Val)) {
3421 Result = DAG.getTargetConstant(Val, Type);
3422 break;
3423 }
3424 }
3425 return;
Eric Christopher1109b342012-05-07 05:46:37 +00003426 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3427 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3428 EVT Type = Op.getValueType();
3429 int64_t Val = C->getSExtValue();
3430 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
3431 Result = DAG.getTargetConstant(Val, Type);
3432 break;
3433 }
3434 }
3435 return;
Eric Christophere07aa432012-05-07 05:46:43 +00003436 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3437 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3438 EVT Type = Op.getValueType();
3439 int64_t Val = C->getSExtValue();
3440 if ((Val >= -65535) && (Val <= -1)) {
3441 Result = DAG.getTargetConstant(Val, Type);
3442 break;
3443 }
3444 }
3445 return;
Eric Christopher470578a2012-05-07 05:46:48 +00003446 case 'O': // signed 15 bit immediate
3447 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3448 EVT Type = Op.getValueType();
3449 int64_t Val = C->getSExtValue();
3450 if ((isInt<15>(Val))) {
3451 Result = DAG.getTargetConstant(Val, Type);
3452 break;
3453 }
3454 }
3455 return;
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003456 case 'P': // immediate in the range of 1 to 65535 (inclusive)
3457 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3458 EVT Type = Op.getValueType();
3459 int64_t Val = C->getSExtValue();
3460 if ((Val <= 65535) && (Val >= 1)) {
3461 Result = DAG.getTargetConstant(Val, Type);
3462 break;
3463 }
3464 }
3465 return;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003466 }
3467
3468 if (Result.getNode()) {
3469 Ops.push_back(Result);
3470 return;
3471 }
3472
3473 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3474}
3475
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003476bool MipsTargetLowering::isLegalAddressingMode(const AddrMode &AM,
3477 Type *Ty) const {
Akira Hatanakaef839192012-11-17 00:25:41 +00003478 // No global is ever allowed as a base.
3479 if (AM.BaseGV)
3480 return false;
3481
3482 switch (AM.Scale) {
3483 case 0: // "r+i" or just "i", depending on HasBaseReg.
3484 break;
3485 case 1:
3486 if (!AM.HasBaseReg) // allow "r+i".
3487 break;
3488 return false; // disallow "r+r" or "r+r+i".
3489 default:
3490 return false;
3491 }
3492
3493 return true;
3494}
3495
3496bool
Dan Gohman2fe6bee2008-10-18 02:06:02 +00003497MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3498 // The Mips target isn't yet aware of offsets.
3499 return false;
3500}
Evan Cheng16993aa2009-10-27 19:56:55 +00003501
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003502EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003503 unsigned SrcAlign,
3504 bool IsMemset, bool ZeroMemset,
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003505 bool MemcpyStrSrc,
3506 MachineFunction &MF) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003507 if (Subtarget.hasMips64())
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003508 return MVT::i64;
3509
3510 return MVT::i32;
3511}
3512
Evan Cheng83896a52009-10-28 01:43:28 +00003513bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
3514 if (VT != MVT::f32 && VT != MVT::f64)
3515 return false;
Bruno Cardoso Lopesb02a9df2011-01-18 19:41:41 +00003516 if (Imm.isNegZero())
3517 return false;
Evan Cheng16993aa2009-10-27 19:56:55 +00003518 return Imm.isZero();
3519}
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003520
3521unsigned MipsTargetLowering::getJumpTableEncoding() const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003522 if (Subtarget.isABI_N64())
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003523 return MachineJumpTableInfo::EK_GPRel64BlockAddress;
Jia Liuf54f60f2012-02-28 07:46:26 +00003524
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003525 return TargetLowering::getJumpTableEncoding();
3526}
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003527
Daniel Sandersf43e6872014-11-01 18:44:56 +00003528void MipsTargetLowering::copyByValRegs(
3529 SDValue Chain, SDLoc DL, std::vector<SDValue> &OutChains, SelectionDAG &DAG,
3530 const ISD::ArgFlagsTy &Flags, SmallVectorImpl<SDValue> &InVals,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003531 const Argument *FuncArg, unsigned FirstReg, unsigned LastReg,
3532 const CCValAssign &VA, MipsCCState &State) const {
Akira Hatanaka25dad192012-10-27 00:10:18 +00003533 MachineFunction &MF = DAG.getMachineFunction();
3534 MachineFrameInfo *MFI = MF.getFrameInfo();
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003535 unsigned GPRSizeInBytes = Subtarget.getGPRSizeInBytes();
Daniel Sanders23e98772014-11-02 16:09:29 +00003536 unsigned NumRegs = LastReg - FirstReg;
3537 unsigned RegAreaSize = NumRegs * GPRSizeInBytes;
Akira Hatanaka25dad192012-10-27 00:10:18 +00003538 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
3539 int FrameObjOffset;
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003540 const MipsABIInfo &ABI = Subtarget.getABI();
3541 ArrayRef<MCPhysReg> ByValArgRegs = ABI.GetByValArgRegs();
Akira Hatanaka25dad192012-10-27 00:10:18 +00003542
3543 if (RegAreaSize)
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003544 FrameObjOffset =
3545 (int)ABI.GetCalleeAllocdArgSizeInBytes(State.getCallingConv()) -
3546 (int)((ByValArgRegs.size() - FirstReg) * GPRSizeInBytes);
Akira Hatanaka25dad192012-10-27 00:10:18 +00003547 else
Daniel Sandersf43e6872014-11-01 18:44:56 +00003548 FrameObjOffset = VA.getLocMemOffset();
Akira Hatanaka25dad192012-10-27 00:10:18 +00003549
3550 // Create frame object.
3551 EVT PtrTy = getPointerTy();
3552 int FI = MFI->CreateFixedObject(FrameObjSize, FrameObjOffset, true);
3553 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
3554 InVals.push_back(FIN);
3555
Daniel Sanders23e98772014-11-02 16:09:29 +00003556 if (!NumRegs)
Akira Hatanaka25dad192012-10-27 00:10:18 +00003557 return;
3558
3559 // Copy arg registers.
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003560 MVT RegTy = MVT::getIntegerVT(GPRSizeInBytes * 8);
Akira Hatanaka25dad192012-10-27 00:10:18 +00003561 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3562
Daniel Sanders23e98772014-11-02 16:09:29 +00003563 for (unsigned I = 0; I < NumRegs; ++I) {
Daniel Sandersd7eba312014-11-07 12:21:37 +00003564 unsigned ArgReg = ByValArgRegs[FirstReg + I];
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003565 unsigned VReg = addLiveIn(MF, ArgReg, RC);
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003566 unsigned Offset = I * GPRSizeInBytes;
Akira Hatanaka25dad192012-10-27 00:10:18 +00003567 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
3568 DAG.getConstant(Offset, PtrTy));
3569 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
3570 StorePtr, MachinePointerInfo(FuncArg, Offset),
3571 false, false, 0);
3572 OutChains.push_back(Store);
3573 }
3574}
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003575
3576// Copy byVal arg to registers and stack.
Daniel Sandersf43e6872014-11-01 18:44:56 +00003577void MipsTargetLowering::passByValArg(
3578 SDValue Chain, SDLoc DL,
3579 std::deque<std::pair<unsigned, SDValue>> &RegsToPass,
3580 SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003581 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg, unsigned FirstReg,
3582 unsigned LastReg, const ISD::ArgFlagsTy &Flags, bool isLittle,
3583 const CCValAssign &VA) const {
Daniel Sandersac272632014-05-23 13:18:02 +00003584 unsigned ByValSizeInBytes = Flags.getByValSize();
3585 unsigned OffsetInBytes = 0; // From beginning of struct
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003586 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
Daniel Sandersac272632014-05-23 13:18:02 +00003587 unsigned Alignment = std::min(Flags.getByValAlign(), RegSizeInBytes);
3588 EVT PtrTy = getPointerTy(), RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
Daniel Sanders23e98772014-11-02 16:09:29 +00003589 unsigned NumRegs = LastReg - FirstReg;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003590
Daniel Sanders23e98772014-11-02 16:09:29 +00003591 if (NumRegs) {
Daniel Sandersd7eba312014-11-07 12:21:37 +00003592 const ArrayRef<MCPhysReg> ArgRegs = Subtarget.getABI().GetByValArgRegs();
Daniel Sanders23e98772014-11-02 16:09:29 +00003593 bool LeftoverBytes = (NumRegs * RegSizeInBytes > ByValSizeInBytes);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003594 unsigned I = 0;
3595
3596 // Copy words to registers.
Daniel Sanders23e98772014-11-02 16:09:29 +00003597 for (; I < NumRegs - LeftoverBytes; ++I, OffsetInBytes += RegSizeInBytes) {
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003598 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Daniel Sandersac272632014-05-23 13:18:02 +00003599 DAG.getConstant(OffsetInBytes, PtrTy));
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003600 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
3601 MachinePointerInfo(), false, false, false,
3602 Alignment);
3603 MemOpChains.push_back(LoadVal.getValue(1));
Daniel Sanders23e98772014-11-02 16:09:29 +00003604 unsigned ArgReg = ArgRegs[FirstReg + I];
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003605 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
3606 }
3607
3608 // Return if the struct has been fully copied.
Daniel Sandersac272632014-05-23 13:18:02 +00003609 if (ByValSizeInBytes == OffsetInBytes)
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003610 return;
3611
3612 // Copy the remainder of the byval argument with sub-word loads and shifts.
3613 if (LeftoverBytes) {
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003614 SDValue Val;
3615
Daniel Sandersac272632014-05-23 13:18:02 +00003616 for (unsigned LoadSizeInBytes = RegSizeInBytes / 2, TotalBytesLoaded = 0;
3617 OffsetInBytes < ByValSizeInBytes; LoadSizeInBytes /= 2) {
3618 unsigned RemainingSizeInBytes = ByValSizeInBytes - OffsetInBytes;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003619
Daniel Sandersac272632014-05-23 13:18:02 +00003620 if (RemainingSizeInBytes < LoadSizeInBytes)
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003621 continue;
3622
3623 // Load subword.
3624 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Daniel Sandersac272632014-05-23 13:18:02 +00003625 DAG.getConstant(OffsetInBytes, PtrTy));
3626 SDValue LoadVal = DAG.getExtLoad(
3627 ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr, MachinePointerInfo(),
Louis Gerbarg67474e32014-07-31 21:45:05 +00003628 MVT::getIntegerVT(LoadSizeInBytes * 8), false, false, false,
3629 Alignment);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003630 MemOpChains.push_back(LoadVal.getValue(1));
3631
3632 // Shift the loaded value.
3633 unsigned Shamt;
3634
3635 if (isLittle)
Daniel Sandersac272632014-05-23 13:18:02 +00003636 Shamt = TotalBytesLoaded * 8;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003637 else
Daniel Sandersac272632014-05-23 13:18:02 +00003638 Shamt = (RegSizeInBytes - (TotalBytesLoaded + LoadSizeInBytes)) * 8;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003639
3640 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
3641 DAG.getConstant(Shamt, MVT::i32));
3642
3643 if (Val.getNode())
3644 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
3645 else
3646 Val = Shift;
3647
Daniel Sandersac272632014-05-23 13:18:02 +00003648 OffsetInBytes += LoadSizeInBytes;
3649 TotalBytesLoaded += LoadSizeInBytes;
3650 Alignment = std::min(Alignment, LoadSizeInBytes);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003651 }
3652
Daniel Sanders23e98772014-11-02 16:09:29 +00003653 unsigned ArgReg = ArgRegs[FirstReg + I];
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003654 RegsToPass.push_back(std::make_pair(ArgReg, Val));
3655 return;
3656 }
3657 }
3658
3659 // Copy remainder of byval arg to it with memcpy.
Daniel Sandersac272632014-05-23 13:18:02 +00003660 unsigned MemCpySize = ByValSizeInBytes - OffsetInBytes;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003661 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Daniel Sandersac272632014-05-23 13:18:02 +00003662 DAG.getConstant(OffsetInBytes, PtrTy));
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003663 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
Daniel Sandersf43e6872014-11-01 18:44:56 +00003664 DAG.getIntPtrConstant(VA.getLocMemOffset()));
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003665 Chain = DAG.getMemcpy(Chain, DL, Dst, Src, DAG.getConstant(MemCpySize, PtrTy),
3666 Alignment, /*isVolatile=*/false, /*AlwaysInline=*/false,
Nick Lewyckyaad475b2014-04-15 07:22:52 +00003667 MachinePointerInfo(), MachinePointerInfo());
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003668 MemOpChains.push_back(Chain);
3669}
Akira Hatanaka2a134022012-10-27 00:21:13 +00003670
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003671void MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003672 SDValue Chain, SDLoc DL,
3673 SelectionDAG &DAG,
Daniel Sanders853c2432014-11-01 18:13:52 +00003674 CCState &State) const {
Daniel Sandersd7eba312014-11-07 12:21:37 +00003675 const ArrayRef<MCPhysReg> ArgRegs = Subtarget.getABI().GetVarArgRegs();
Daniel Sanders853c2432014-11-01 18:13:52 +00003676 unsigned Idx = State.getFirstUnallocated(ArgRegs.data(), ArgRegs.size());
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003677 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
3678 MVT RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003679 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3680 MachineFunction &MF = DAG.getMachineFunction();
3681 MachineFrameInfo *MFI = MF.getFrameInfo();
3682 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3683
3684 // Offset of the first variable argument from stack pointer.
3685 int VaArgOffset;
3686
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003687 if (ArgRegs.size() == Idx)
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003688 VaArgOffset =
Daniel Sanders853c2432014-11-01 18:13:52 +00003689 RoundUpToAlignment(State.getNextStackOffset(), RegSizeInBytes);
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003690 else {
3691 const MipsABIInfo &ABI = Subtarget.getABI();
3692 VaArgOffset =
3693 (int)ABI.GetCalleeAllocdArgSizeInBytes(State.getCallingConv()) -
3694 (int)(RegSizeInBytes * (ArgRegs.size() - Idx));
3695 }
Akira Hatanaka2a134022012-10-27 00:21:13 +00003696
3697 // Record the frame index of the first variable argument
3698 // which is a value necessary to VASTART.
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003699 int FI = MFI->CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003700 MipsFI->setVarArgsFrameIndex(FI);
3701
3702 // Copy the integer registers that have not been used for argument passing
3703 // to the argument register save area. For O32, the save area is allocated
3704 // in the caller's stack frame, while for N32/64, it is allocated in the
3705 // callee's stack frame.
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003706 for (unsigned I = Idx; I < ArgRegs.size();
3707 ++I, VaArgOffset += RegSizeInBytes) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003708 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003709 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003710 FI = MFI->CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003711 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
3712 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
3713 MachinePointerInfo(), false, false, 0);
Eric Christopher1c29a652014-07-18 22:55:25 +00003714 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(
3715 (Value *)nullptr);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003716 OutChains.push_back(Store);
3717 }
3718}
Daniel Sanders23e98772014-11-02 16:09:29 +00003719
3720void MipsTargetLowering::HandleByVal(CCState *State, unsigned &Size,
3721 unsigned Align) const {
3722 MachineFunction &MF = State->getMachineFunction();
3723 const TargetFrameLowering *TFL = MF.getSubtarget().getFrameLowering();
3724
3725 assert(Size && "Byval argument's size shouldn't be 0.");
3726
3727 Align = std::min(Align, TFL->getStackAlignment());
3728
3729 unsigned FirstReg = 0;
3730 unsigned NumRegs = 0;
3731
3732 if (State->getCallingConv() != CallingConv::Fast) {
3733 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
3734 const ArrayRef<MCPhysReg> IntArgRegs = Subtarget.getABI().GetByValArgRegs();
3735 // FIXME: The O32 case actually describes no shadow registers.
3736 const MCPhysReg *ShadowRegs =
3737 Subtarget.isABI_O32() ? IntArgRegs.data() : Mips64DPRegs;
3738
3739 // We used to check the size as well but we can't do that anymore since
3740 // CCState::HandleByVal() rounds up the size after calling this function.
3741 assert(!(Align % RegSizeInBytes) &&
3742 "Byval argument's alignment should be a multiple of"
3743 "RegSizeInBytes.");
3744
3745 FirstReg = State->getFirstUnallocated(IntArgRegs.data(), IntArgRegs.size());
3746
3747 // If Align > RegSizeInBytes, the first arg register must be even.
3748 // FIXME: This condition happens to do the right thing but it's not the
3749 // right way to test it. We want to check that the stack frame offset
3750 // of the register is aligned.
3751 if ((Align > RegSizeInBytes) && (FirstReg % 2)) {
3752 State->AllocateReg(IntArgRegs[FirstReg], ShadowRegs[FirstReg]);
3753 ++FirstReg;
3754 }
3755
3756 // Mark the registers allocated.
3757 Size = RoundUpToAlignment(Size, RegSizeInBytes);
3758 for (unsigned I = FirstReg; Size > 0 && (I < IntArgRegs.size());
3759 Size -= RegSizeInBytes, ++I, ++NumRegs)
3760 State->AllocateReg(IntArgRegs[I], ShadowRegs[I]);
3761 }
3762
3763 State->addInRegsParamInfo(FirstReg, FirstReg + NumRegs);
3764}
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00003765
3766MachineBasicBlock *
3767MipsTargetLowering::emitPseudoSELECT(MachineInstr *MI, MachineBasicBlock *BB,
3768 bool isFPCmp, unsigned Opc) const {
3769 assert(!(Subtarget.hasMips4() || Subtarget.hasMips32()) &&
3770 "Subtarget already supports SELECT nodes with the use of"
3771 "conditional-move instructions.");
3772
3773 const TargetInstrInfo *TII =
3774 getTargetMachine().getSubtargetImpl()->getInstrInfo();
3775 DebugLoc DL = MI->getDebugLoc();
3776
3777 // To "insert" a SELECT instruction, we actually have to insert the
3778 // diamond control-flow pattern. The incoming instruction knows the
3779 // destination vreg to set, the condition code register to branch on, the
3780 // true/false values to select between, and a branch opcode to use.
3781 const BasicBlock *LLVM_BB = BB->getBasicBlock();
3782 MachineFunction::iterator It = BB;
3783 ++It;
3784
3785 // thisMBB:
3786 // ...
3787 // TrueVal = ...
3788 // setcc r1, r2, r3
3789 // bNE r1, r0, copy1MBB
3790 // fallthrough --> copy0MBB
3791 MachineBasicBlock *thisMBB = BB;
3792 MachineFunction *F = BB->getParent();
3793 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
3794 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
3795 F->insert(It, copy0MBB);
3796 F->insert(It, sinkMBB);
3797
3798 // Transfer the remainder of BB and its successor edges to sinkMBB.
3799 sinkMBB->splice(sinkMBB->begin(), BB,
3800 std::next(MachineBasicBlock::iterator(MI)), BB->end());
3801 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
3802
3803 // Next, add the true and fallthrough blocks as its successors.
3804 BB->addSuccessor(copy0MBB);
3805 BB->addSuccessor(sinkMBB);
3806
3807 if (isFPCmp) {
3808 // bc1[tf] cc, sinkMBB
3809 BuildMI(BB, DL, TII->get(Opc))
3810 .addReg(MI->getOperand(1).getReg())
3811 .addMBB(sinkMBB);
3812 } else {
3813 // bne rs, $0, sinkMBB
3814 BuildMI(BB, DL, TII->get(Opc))
3815 .addReg(MI->getOperand(1).getReg())
3816 .addReg(Mips::ZERO)
3817 .addMBB(sinkMBB);
3818 }
3819
3820 // copy0MBB:
3821 // %FalseValue = ...
3822 // # fallthrough to sinkMBB
3823 BB = copy0MBB;
3824
3825 // Update machine-CFG edges
3826 BB->addSuccessor(sinkMBB);
3827
3828 // sinkMBB:
3829 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
3830 // ...
3831 BB = sinkMBB;
3832
3833 BuildMI(*BB, BB->begin(), DL,
3834 TII->get(Mips::PHI), MI->getOperand(0).getReg())
3835 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
3836 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB);
3837
3838 MI->eraseFromParent(); // The pseudo instruction is gone now.
3839
3840 return BB;
3841}
Daniel Sanders1440bb22015-01-09 17:21:30 +00003842
3843// FIXME? Maybe this could be a TableGen attribute on some registers and
3844// this table could be generated automatically from RegInfo.
3845unsigned MipsTargetLowering::getRegisterByName(const char* RegName,
3846 EVT VT) const {
3847 // Named registers is expected to be fairly rare. For now, just support $28
3848 // since the linux kernel uses it.
3849 if (Subtarget.isGP64bit()) {
3850 unsigned Reg = StringSwitch<unsigned>(RegName)
3851 .Case("$28", Mips::GP_64)
3852 .Default(0);
3853 if (Reg)
3854 return Reg;
3855 } else {
3856 unsigned Reg = StringSwitch<unsigned>(RegName)
3857 .Case("$28", Mips::GP)
3858 .Default(0);
3859 if (Reg)
3860 return Reg;
3861 }
3862 report_fatal_error("Invalid register name global variable");
3863}