blob: f37737dfc006fd50f87264a103457e0ab9a4a016 [file] [log] [blame]
Jia Liuf54f60f2012-02-28 07:46:26 +00001//===-- MipsISelLowering.cpp - Mips DAG Lowering Implementation -----------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00007//
Akira Hatanakae2489122011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00009//
10// This file defines the interfaces that Mips uses to lower LLVM code into a
11// selection DAG.
12//
Akira Hatanakae2489122011-04-15 21:51:11 +000013//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000014#include "MipsISelLowering.h"
Craig Topperb25fda92012-03-17 18:46:09 +000015#include "InstPrinter/MipsInstPrinter.h"
16#include "MCTargetDesc/MipsBaseInfo.h"
Daniel Sanders0456c152014-11-07 14:24:31 +000017#include "MipsCCState.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "MipsMachineFunction.h"
19#include "MipsSubtarget.h"
20#include "MipsTargetMachine.h"
21#include "MipsTargetObjectFile.h"
Akira Hatanaka90131ac2012-10-19 21:47:33 +000022#include "llvm/ADT/Statistic.h"
Daniel Sanders8b59af12013-11-12 12:56:01 +000023#include "llvm/ADT/StringSwitch.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000024#include "llvm/CodeGen/CallingConvLower.h"
25#include "llvm/CodeGen/MachineFrameInfo.h"
26#include "llvm/CodeGen/MachineFunction.h"
27#include "llvm/CodeGen/MachineInstrBuilder.h"
Eric Christopher79cc1e32014-09-02 22:28:02 +000028#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000029#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000030#include "llvm/CodeGen/SelectionDAGISel.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000031#include "llvm/CodeGen/ValueTypes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000032#include "llvm/IR/CallingConv.h"
33#include "llvm/IR/DerivedTypes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000034#include "llvm/IR/GlobalVariable.h"
Akira Hatanaka90131ac2012-10-19 21:47:33 +000035#include "llvm/Support/CommandLine.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000036#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000037#include "llvm/Support/ErrorHandling.h"
NAKAMURA Takumie30303f2012-04-21 15:31:45 +000038#include "llvm/Support/raw_ostream.h"
Akira Hatanaka7473b472013-08-14 00:21:25 +000039#include <cctype>
NAKAMURA Takumie30303f2012-04-21 15:31:45 +000040
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000041using namespace llvm;
42
Chandler Carruth84e68b22014-04-22 02:41:26 +000043#define DEBUG_TYPE "mips-lower"
44
Akira Hatanaka90131ac2012-10-19 21:47:33 +000045STATISTIC(NumTailCalls, "Number of tail calls");
46
47static cl::opt<bool>
Akira Hatanaka59f299f2012-11-21 20:21:11 +000048LargeGOT("mxgot", cl::Hidden,
49 cl::desc("MIPS: Enable GOT larger than 64k."), cl::init(false));
50
Akira Hatanaka1cb02422013-05-20 18:07:43 +000051static cl::opt<bool>
Akira Hatanakabe76cd02013-05-21 17:17:59 +000052NoZeroDivCheck("mno-check-zero-division", cl::Hidden,
Akira Hatanaka1cb02422013-05-20 18:07:43 +000053 cl::desc("MIPS: Don't trap on integer division by zero."),
54 cl::init(false));
55
Reed Kotler720c5ca2014-04-17 22:15:34 +000056cl::opt<bool>
57EnableMipsFastISel("mips-fast-isel", cl::Hidden,
58 cl::desc("Allow mips-fast-isel to be used"),
59 cl::init(false));
60
Craig Topper840beec2014-04-04 05:16:06 +000061static const MCPhysReg Mips64DPRegs[8] = {
Akira Hatanakaac8c6692012-10-27 00:29:43 +000062 Mips::D12_64, Mips::D13_64, Mips::D14_64, Mips::D15_64,
63 Mips::D16_64, Mips::D17_64, Mips::D18_64, Mips::D19_64
64};
65
Jia Liuf54f60f2012-02-28 07:46:26 +000066// If I is a shifted mask, set the size (Size) and the first bit of the
Akira Hatanaka73d78b72011-08-18 20:07:42 +000067// mask (Pos), and return true.
Jia Liuf54f60f2012-02-28 07:46:26 +000068// For example, if I is 0x003ff800, (Pos, Size) = (11, 11).
Akira Hatanaka0bb60d892013-03-12 00:16:36 +000069static bool isShiftedMask(uint64_t I, uint64_t &Pos, uint64_t &Size) {
Akira Hatanaka20cee2e2011-12-05 21:26:34 +000070 if (!isShiftedMask_64(I))
Akira Hatanaka4c0a7122013-10-07 19:33:02 +000071 return false;
Akira Hatanaka5360f882011-08-17 02:05:42 +000072
Benjamin Kramer5f6a9072015-02-12 15:35:40 +000073 Size = countPopulation(I);
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +000074 Pos = countTrailingZeros(I);
Akira Hatanaka73d78b72011-08-18 20:07:42 +000075 return true;
Akira Hatanaka5360f882011-08-17 02:05:42 +000076}
77
Akira Hatanaka96ca1822013-03-13 00:54:29 +000078SDValue MipsTargetLowering::getGlobalReg(SelectionDAG &DAG, EVT Ty) const {
Akira Hatanakab049aef2012-02-24 22:34:47 +000079 MipsFunctionInfo *FI = DAG.getMachineFunction().getInfo<MipsFunctionInfo>();
80 return DAG.getRegister(FI->getGlobalBaseReg(), Ty);
81}
82
Akira Hatanakad8f10ce2013-09-27 19:51:35 +000083SDValue MipsTargetLowering::getTargetNode(GlobalAddressSDNode *N, EVT Ty,
84 SelectionDAG &DAG,
Akira Hatanaka96ca1822013-03-13 00:54:29 +000085 unsigned Flag) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +000086 return DAG.getTargetGlobalAddress(N->getGlobal(), SDLoc(N), Ty, 0, Flag);
Akira Hatanakafd04ad42012-11-21 20:26:38 +000087}
88
Akira Hatanakad8f10ce2013-09-27 19:51:35 +000089SDValue MipsTargetLowering::getTargetNode(ExternalSymbolSDNode *N, EVT Ty,
90 SelectionDAG &DAG,
91 unsigned Flag) const {
92 return DAG.getTargetExternalSymbol(N->getSymbol(), Ty, Flag);
93}
94
95SDValue MipsTargetLowering::getTargetNode(BlockAddressSDNode *N, EVT Ty,
96 SelectionDAG &DAG,
97 unsigned Flag) const {
98 return DAG.getTargetBlockAddress(N->getBlockAddress(), Ty, 0, Flag);
99}
100
101SDValue MipsTargetLowering::getTargetNode(JumpTableSDNode *N, EVT Ty,
102 SelectionDAG &DAG,
103 unsigned Flag) const {
104 return DAG.getTargetJumpTable(N->getIndex(), Ty, Flag);
105}
106
107SDValue MipsTargetLowering::getTargetNode(ConstantPoolSDNode *N, EVT Ty,
108 SelectionDAG &DAG,
109 unsigned Flag) const {
110 return DAG.getTargetConstantPool(N->getConstVal(), Ty, N->getAlignment(),
111 N->getOffset(), Flag);
Akira Hatanakafd04ad42012-11-21 20:26:38 +0000112}
113
Chris Lattner5e693ed2009-07-28 03:13:23 +0000114const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
115 switch (Opcode) {
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000116 case MipsISD::JmpLink: return "MipsISD::JmpLink";
Akira Hatanaka91318df2012-10-19 20:59:39 +0000117 case MipsISD::TailCall: return "MipsISD::TailCall";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000118 case MipsISD::Hi: return "MipsISD::Hi";
119 case MipsISD::Lo: return "MipsISD::Lo";
120 case MipsISD::GPRel: return "MipsISD::GPRel";
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +0000121 case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000122 case MipsISD::Ret: return "MipsISD::Ret";
Akira Hatanakac0b02062013-01-30 00:26:49 +0000123 case MipsISD::EH_RETURN: return "MipsISD::EH_RETURN";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000124 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
125 case MipsISD::FPCmp: return "MipsISD::FPCmp";
126 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
127 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000128 case MipsISD::TruncIntFP: return "MipsISD::TruncIntFP";
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000129 case MipsISD::MFHI: return "MipsISD::MFHI";
130 case MipsISD::MFLO: return "MipsISD::MFLO";
131 case MipsISD::MTLOHI: return "MipsISD::MTLOHI";
Akira Hatanaka28721bd2013-03-30 01:14:04 +0000132 case MipsISD::Mult: return "MipsISD::Mult";
133 case MipsISD::Multu: return "MipsISD::Multu";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000134 case MipsISD::MAdd: return "MipsISD::MAdd";
135 case MipsISD::MAddu: return "MipsISD::MAddu";
136 case MipsISD::MSub: return "MipsISD::MSub";
137 case MipsISD::MSubu: return "MipsISD::MSubu";
138 case MipsISD::DivRem: return "MipsISD::DivRem";
139 case MipsISD::DivRemU: return "MipsISD::DivRemU";
Akira Hatanaka28721bd2013-03-30 01:14:04 +0000140 case MipsISD::DivRem16: return "MipsISD::DivRem16";
141 case MipsISD::DivRemU16: return "MipsISD::DivRemU16";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000142 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
143 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
Akira Hatanakafaa88c02011-12-12 22:38:19 +0000144 case MipsISD::Wrapper: return "MipsISD::Wrapper";
Akira Hatanakaa4c09bc2011-07-19 23:30:50 +0000145 case MipsISD::Sync: return "MipsISD::Sync";
Akira Hatanaka5360f882011-08-17 02:05:42 +0000146 case MipsISD::Ext: return "MipsISD::Ext";
147 case MipsISD::Ins: return "MipsISD::Ins";
Akira Hatanakab9ebf8d2012-06-02 00:03:12 +0000148 case MipsISD::LWL: return "MipsISD::LWL";
149 case MipsISD::LWR: return "MipsISD::LWR";
150 case MipsISD::SWL: return "MipsISD::SWL";
151 case MipsISD::SWR: return "MipsISD::SWR";
152 case MipsISD::LDL: return "MipsISD::LDL";
153 case MipsISD::LDR: return "MipsISD::LDR";
154 case MipsISD::SDL: return "MipsISD::SDL";
155 case MipsISD::SDR: return "MipsISD::SDR";
Akira Hatanaka233ac532012-09-21 23:52:47 +0000156 case MipsISD::EXTP: return "MipsISD::EXTP";
157 case MipsISD::EXTPDP: return "MipsISD::EXTPDP";
158 case MipsISD::EXTR_S_H: return "MipsISD::EXTR_S_H";
159 case MipsISD::EXTR_W: return "MipsISD::EXTR_W";
160 case MipsISD::EXTR_R_W: return "MipsISD::EXTR_R_W";
161 case MipsISD::EXTR_RS_W: return "MipsISD::EXTR_RS_W";
162 case MipsISD::SHILO: return "MipsISD::SHILO";
163 case MipsISD::MTHLIP: return "MipsISD::MTHLIP";
164 case MipsISD::MULT: return "MipsISD::MULT";
165 case MipsISD::MULTU: return "MipsISD::MULTU";
Jia Liu434874d2013-03-04 01:06:54 +0000166 case MipsISD::MADD_DSP: return "MipsISD::MADD_DSP";
Akira Hatanaka233ac532012-09-21 23:52:47 +0000167 case MipsISD::MADDU_DSP: return "MipsISD::MADDU_DSP";
168 case MipsISD::MSUB_DSP: return "MipsISD::MSUB_DSP";
169 case MipsISD::MSUBU_DSP: return "MipsISD::MSUBU_DSP";
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000170 case MipsISD::SHLL_DSP: return "MipsISD::SHLL_DSP";
171 case MipsISD::SHRA_DSP: return "MipsISD::SHRA_DSP";
172 case MipsISD::SHRL_DSP: return "MipsISD::SHRL_DSP";
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000173 case MipsISD::SETCC_DSP: return "MipsISD::SETCC_DSP";
174 case MipsISD::SELECT_CC_DSP: return "MipsISD::SELECT_CC_DSP";
Daniel Sandersce09d072013-08-28 12:14:50 +0000175 case MipsISD::VALL_ZERO: return "MipsISD::VALL_ZERO";
176 case MipsISD::VANY_ZERO: return "MipsISD::VANY_ZERO";
177 case MipsISD::VALL_NONZERO: return "MipsISD::VALL_NONZERO";
178 case MipsISD::VANY_NONZERO: return "MipsISD::VANY_NONZERO";
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000179 case MipsISD::VCEQ: return "MipsISD::VCEQ";
180 case MipsISD::VCLE_S: return "MipsISD::VCLE_S";
181 case MipsISD::VCLE_U: return "MipsISD::VCLE_U";
182 case MipsISD::VCLT_S: return "MipsISD::VCLT_S";
183 case MipsISD::VCLT_U: return "MipsISD::VCLT_U";
Daniel Sanders3ce56622013-09-24 12:18:31 +0000184 case MipsISD::VSMAX: return "MipsISD::VSMAX";
185 case MipsISD::VSMIN: return "MipsISD::VSMIN";
186 case MipsISD::VUMAX: return "MipsISD::VUMAX";
187 case MipsISD::VUMIN: return "MipsISD::VUMIN";
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000188 case MipsISD::VEXTRACT_SEXT_ELT: return "MipsISD::VEXTRACT_SEXT_ELT";
189 case MipsISD::VEXTRACT_ZEXT_ELT: return "MipsISD::VEXTRACT_ZEXT_ELT";
Daniel Sandersf7456c72013-09-23 13:22:24 +0000190 case MipsISD::VNOR: return "MipsISD::VNOR";
Daniel Sanderse5087042013-09-24 14:02:15 +0000191 case MipsISD::VSHF: return "MipsISD::VSHF";
Daniel Sanders26307182013-09-24 14:20:00 +0000192 case MipsISD::SHF: return "MipsISD::SHF";
Daniel Sanders2ed228b2013-09-24 14:36:12 +0000193 case MipsISD::ILVEV: return "MipsISD::ILVEV";
194 case MipsISD::ILVOD: return "MipsISD::ILVOD";
195 case MipsISD::ILVL: return "MipsISD::ILVL";
196 case MipsISD::ILVR: return "MipsISD::ILVR";
Daniel Sandersfae5f2a2013-09-24 14:53:25 +0000197 case MipsISD::PCKEV: return "MipsISD::PCKEV";
198 case MipsISD::PCKOD: return "MipsISD::PCKOD";
Daniel Sandersb50ccf82014-04-01 10:35:28 +0000199 case MipsISD::INSVE: return "MipsISD::INSVE";
Craig Topper062a2ba2014-04-25 05:30:21 +0000200 default: return nullptr;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000201 }
202}
203
Eric Christopherb1526602014-09-19 23:30:42 +0000204MipsTargetLowering::MipsTargetLowering(const MipsTargetMachine &TM,
Eric Christopher8924d272014-07-18 23:25:04 +0000205 const MipsSubtarget &STI)
Eric Christopher96e72c62015-01-29 23:27:36 +0000206 : TargetLowering(TM), Subtarget(STI), ABI(TM.getABI()) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000207 // Mips does not have i1 type, so use i32 for
Wesley Peck527da1b2010-11-23 03:31:01 +0000208 // setcc operations results (slt, sgt, ...).
Duncan Sands8d6e2e12008-11-23 15:47:28 +0000209 setBooleanContents(ZeroOrOneBooleanContent);
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000210 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Daniel Sanderscbd44c52014-07-10 10:18:12 +0000211 // The cmp.cond.fmt instruction in MIPS32r6/MIPS64r6 uses 0 and -1 like MSA
212 // does. Integer booleans still use 0 and 1.
Eric Christopher1c29a652014-07-18 22:55:25 +0000213 if (Subtarget.hasMips32r6())
Daniel Sanderscbd44c52014-07-10 10:18:12 +0000214 setBooleanContents(ZeroOrOneBooleanContent,
215 ZeroOrNegativeOneBooleanContent);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000216
Wesley Peck527da1b2010-11-23 03:31:01 +0000217 // Load extented operations for i1 types must be promoted
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000218 for (MVT VT : MVT::integer_valuetypes()) {
219 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
220 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
221 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
222 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000223
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);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +0000264 setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom);
265 setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom);
266 setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000267 }
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +0000268
Eric Christopher1c29a652014-07-18 22:55:25 +0000269 if (!Subtarget.isGP64bit()) {
Akira Hatanaka0a8ab712012-05-09 00:55:21 +0000270 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
271 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
272 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
273 }
274
Akira Hatanaka28e02ec2012-11-07 19:10:58 +0000275 setOperationAction(ISD::ADD, MVT::i32, Custom);
Eric Christopher1c29a652014-07-18 22:55:25 +0000276 if (Subtarget.isGP64bit())
Akira Hatanaka28e02ec2012-11-07 19:10:58 +0000277 setOperationAction(ISD::ADD, MVT::i64, Custom);
278
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000279 setOperationAction(ISD::SDIV, MVT::i32, Expand);
280 setOperationAction(ISD::SREM, MVT::i32, Expand);
281 setOperationAction(ISD::UDIV, MVT::i32, Expand);
282 setOperationAction(ISD::UREM, MVT::i32, Expand);
Akira Hatanakab1538f92011-10-03 21:06:13 +0000283 setOperationAction(ISD::SDIV, MVT::i64, Expand);
284 setOperationAction(ISD::SREM, MVT::i64, Expand);
285 setOperationAction(ISD::UDIV, MVT::i64, Expand);
286 setOperationAction(ISD::UREM, MVT::i64, Expand);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000287
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000288 // Operations not directly supported by Mips.
Tom Stellardb1588fc2013-03-08 15:36:57 +0000289 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
290 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
291 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
292 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
Tom Stellard3787b122014-06-10 16:01:29 +0000293 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
294 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000295 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Akira Hatanaka79aed152011-12-20 23:40:56 +0000296 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000297 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Akira Hatanaka79aed152011-12-20 23:40:56 +0000298 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000299 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Eric Christopher1c29a652014-07-18 22:55:25 +0000300 if (Subtarget.hasCnMips()) {
Kai Nacke93fe5e82014-03-20 11:51:58 +0000301 setOperationAction(ISD::CTPOP, MVT::i32, Legal);
302 setOperationAction(ISD::CTPOP, MVT::i64, Legal);
303 } else {
304 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
305 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
306 }
Owen Anderson9f944592009-08-11 20:47:22 +0000307 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
Akira Hatanaka410ce9c2011-12-21 00:14:05 +0000308 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000309 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
310 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
311 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
312 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000313 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Akira Hatanaka7ba8a8d2011-09-30 18:51:46 +0000314 setOperationAction(ISD::ROTL, MVT::i64, Expand);
Akira Hatanaka33a25af2012-07-31 20:54:48 +0000315 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
316 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Bruno Cardoso Lopesd47180e2010-12-09 17:32:30 +0000317
Eric Christopher1c29a652014-07-18 22:55:25 +0000318 if (!Subtarget.hasMips32r2())
Bruno Cardoso Lopesd47180e2010-12-09 17:32:30 +0000319 setOperationAction(ISD::ROTR, MVT::i32, Expand);
320
Eric Christopher1c29a652014-07-18 22:55:25 +0000321 if (!Subtarget.hasMips64r2())
Akira Hatanaka7ba8a8d2011-09-30 18:51:46 +0000322 setOperationAction(ISD::ROTR, MVT::i64, Expand);
323
Owen Anderson9f944592009-08-11 20:47:22 +0000324 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes22b69db2011-03-04 18:54:14 +0000325 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000326 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes22b69db2011-03-04 18:54:14 +0000327 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000328 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
329 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000330 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
331 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanakadfb8cda2011-05-23 22:23:58 +0000332 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000333 setOperationAction(ISD::FLOG, MVT::f32, Expand);
334 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
335 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
336 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000337 setOperationAction(ISD::FMA, MVT::f32, Expand);
338 setOperationAction(ISD::FMA, MVT::f64, Expand);
Akira Hatanaka0603ad82012-03-29 18:43:11 +0000339 setOperationAction(ISD::FREM, MVT::f32, Expand);
340 setOperationAction(ISD::FREM, MVT::f64, Expand);
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000341
Akira Hatanakac0b02062013-01-30 00:26:49 +0000342 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
343
Daniel Sanders2b553d42014-08-01 09:17:39 +0000344 setOperationAction(ISD::VASTART, MVT::Other, Custom);
345 setOperationAction(ISD::VAARG, MVT::Other, Custom);
Bruno Cardoso Lopes048ffab2011-03-09 19:22:22 +0000346 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
347 setOperationAction(ISD::VAEND, MVT::Other, Expand);
348
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000349 // Use the default for now
Owen Anderson9f944592009-08-11 20:47:22 +0000350 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
351 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman26a48482011-07-27 22:21:52 +0000352
Jia Liuf54f60f2012-02-28 07:46:26 +0000353 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
354 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
355 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
356 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
Eli Friedman7dfa7912011-08-29 18:23:02 +0000357
Eli Friedman30a49e92011-08-03 21:06:02 +0000358 setInsertFencesForAtomic(true);
359
Eric Christopher1c29a652014-07-18 22:55:25 +0000360 if (!Subtarget.hasMips32r2()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000361 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
362 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000363 }
364
Daniel Sanders070fd1c2014-05-12 12:41:59 +0000365 // MIPS16 lacks MIPS32's clz and clo instructions.
Eric Christopher1c29a652014-07-18 22:55:25 +0000366 if (!Subtarget.hasMips32() || Subtarget.inMips16Mode())
Owen Anderson9f944592009-08-11 20:47:22 +0000367 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Eric Christopher1c29a652014-07-18 22:55:25 +0000368 if (!Subtarget.hasMips64())
Akira Hatanaka1d8efab2011-12-21 00:20:27 +0000369 setOperationAction(ISD::CTLZ, MVT::i64, Expand);
Bruno Cardoso Lopes93da7e62008-08-08 06:16:31 +0000370
Eric Christopher1c29a652014-07-18 22:55:25 +0000371 if (!Subtarget.hasMips32r2())
Owen Anderson9f944592009-08-11 20:47:22 +0000372 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Eric Christopher1c29a652014-07-18 22:55:25 +0000373 if (!Subtarget.hasMips64r2())
Akira Hatanaka4706ac92011-12-20 23:56:43 +0000374 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000375
Eric Christopher1c29a652014-07-18 22:55:25 +0000376 if (Subtarget.isGP64bit()) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000377 setLoadExtAction(ISD::SEXTLOAD, MVT::i64, MVT::i32, Custom);
378 setLoadExtAction(ISD::ZEXTLOAD, MVT::i64, MVT::i32, Custom);
379 setLoadExtAction(ISD::EXTLOAD, MVT::i64, MVT::i32, Custom);
Akira Hatanaka019e5922012-06-02 00:04:42 +0000380 setTruncStoreAction(MVT::i64, MVT::i32, Custom);
381 }
382
Akira Hatanakaa3d9ab92013-07-26 20:58:55 +0000383 setOperationAction(ISD::TRAP, MVT::Other, Legal);
384
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000385 setTargetDAGCombine(ISD::SDIVREM);
386 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka5e152182012-03-08 03:26:37 +0000387 setTargetDAGCombine(ISD::SELECT);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000388 setTargetDAGCombine(ISD::AND);
389 setTargetDAGCombine(ISD::OR);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000390 setTargetDAGCombine(ISD::ADD);
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000391
Eric Christopher1c29a652014-07-18 22:55:25 +0000392 setMinFunctionAlignment(Subtarget.isGP64bit() ? 3 : 2);
Eli Friedman2518f832011-05-06 20:34:06 +0000393
Daniel Sanders2b553d42014-08-01 09:17:39 +0000394 // The arguments on the stack are defined in terms of 4-byte slots on O32
395 // and 8-byte slots on N32/N64.
Eric Christopher96e72c62015-01-29 23:27:36 +0000396 setMinStackArgumentAlignment((ABI.IsN32() || ABI.IsN64()) ? 8 : 4);
Daniel Sanders2b553d42014-08-01 09:17:39 +0000397
Eric Christopher96e72c62015-01-29 23:27:36 +0000398 setStackPointerRegisterToSaveRestore(ABI.IsN64() ? Mips::SP_64 : Mips::SP);
Akira Hatanakaaa560002011-05-26 18:59:03 +0000399
Eric Christopher96e72c62015-01-29 23:27:36 +0000400 setExceptionPointerRegister(ABI.IsN64() ? Mips::A0_64 : Mips::A0);
401 setExceptionSelectorRegister(ABI.IsN64() ? Mips::A1_64 : Mips::A1);
Akira Hatanaka1daf8c22012-06-13 19:33:32 +0000402
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000403 MaxStoresPerMemcpy = 16;
Zoran Jovanovicff9d5f32013-12-19 16:12:56 +0000404
Eric Christopher1c29a652014-07-18 22:55:25 +0000405 isMicroMips = Subtarget.inMicroMipsMode();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000406}
407
Eric Christopherb1526602014-09-19 23:30:42 +0000408const MipsTargetLowering *MipsTargetLowering::create(const MipsTargetMachine &TM,
Eric Christopher8924d272014-07-18 23:25:04 +0000409 const MipsSubtarget &STI) {
410 if (STI.inMips16Mode())
411 return llvm::createMips16TargetLowering(TM, STI);
Jia Liuf54f60f2012-02-28 07:46:26 +0000412
Eric Christopher8924d272014-07-18 23:25:04 +0000413 return llvm::createMipsSETargetLowering(TM, STI);
Akira Hatanaka2fcc1cf2011-08-12 21:30:06 +0000414}
415
Reed Kotler720c5ca2014-04-17 22:15:34 +0000416// Create a fast isel object.
417FastISel *
418MipsTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
419 const TargetLibraryInfo *libInfo) const {
420 if (!EnableMipsFastISel)
421 return TargetLowering::createFastISel(funcInfo, libInfo);
422 return Mips::createFastISel(funcInfo, libInfo);
423}
424
Matt Arsenault758659232013-05-18 00:21:46 +0000425EVT MipsTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Akira Hatanakab13b3332013-01-04 20:06:01 +0000426 if (!VT.isVector())
427 return MVT::i32;
428 return VT.changeVectorElementTypeToInteger();
Scott Michela6729e82008-03-10 15:42:14 +0000429}
430
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000431static SDValue performDivRemCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000432 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000433 const MipsSubtarget &Subtarget) {
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000434 if (DCI.isBeforeLegalizeOps())
435 return SDValue();
436
Akira Hatanakab1538f92011-10-03 21:06:13 +0000437 EVT Ty = N->getValueType(0);
Akira Hatanaka8002a3f2013-08-14 00:47:08 +0000438 unsigned LO = (Ty == MVT::i32) ? Mips::LO0 : Mips::LO0_64;
439 unsigned HI = (Ty == MVT::i32) ? Mips::HI0 : Mips::HI0_64;
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000440 unsigned Opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem16 :
441 MipsISD::DivRemU16;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000442 SDLoc DL(N);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000443
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000444 SDValue DivRem = DAG.getNode(Opc, DL, MVT::Glue,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000445 N->getOperand(0), N->getOperand(1));
446 SDValue InChain = DAG.getEntryNode();
447 SDValue InGlue = DivRem;
448
449 // insert MFLO
450 if (N->hasAnyUseOfValue(0)) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000451 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, DL, LO, Ty,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000452 InGlue);
453 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
454 InChain = CopyFromLo.getValue(1);
455 InGlue = CopyFromLo.getValue(2);
456 }
457
458 // insert MFHI
459 if (N->hasAnyUseOfValue(1)) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000460 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, DL,
Akira Hatanakab1538f92011-10-03 21:06:13 +0000461 HI, Ty, InGlue);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000462 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
463 }
464
465 return SDValue();
466}
467
Akira Hatanaka89af5892013-04-18 01:00:46 +0000468static Mips::CondCode condCodeToFCC(ISD::CondCode CC) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000469 switch (CC) {
470 default: llvm_unreachable("Unknown fp condition code!");
471 case ISD::SETEQ:
472 case ISD::SETOEQ: return Mips::FCOND_OEQ;
473 case ISD::SETUNE: return Mips::FCOND_UNE;
474 case ISD::SETLT:
475 case ISD::SETOLT: return Mips::FCOND_OLT;
476 case ISD::SETGT:
477 case ISD::SETOGT: return Mips::FCOND_OGT;
478 case ISD::SETLE:
479 case ISD::SETOLE: return Mips::FCOND_OLE;
480 case ISD::SETGE:
481 case ISD::SETOGE: return Mips::FCOND_OGE;
482 case ISD::SETULT: return Mips::FCOND_ULT;
483 case ISD::SETULE: return Mips::FCOND_ULE;
484 case ISD::SETUGT: return Mips::FCOND_UGT;
485 case ISD::SETUGE: return Mips::FCOND_UGE;
486 case ISD::SETUO: return Mips::FCOND_UN;
487 case ISD::SETO: return Mips::FCOND_OR;
488 case ISD::SETNE:
489 case ISD::SETONE: return Mips::FCOND_ONE;
490 case ISD::SETUEQ: return Mips::FCOND_UEQ;
491 }
492}
493
494
Akira Hatanakaf0ea5002013-03-30 01:16:38 +0000495/// This function returns true if the floating point conditional branches and
496/// conditional moves which use condition code CC should be inverted.
497static bool invertFPCondCodeUser(Mips::CondCode CC) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000498 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
499 return false;
500
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000501 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
502 "Illegal Condition Code");
Akira Hatanakaa5352702011-03-31 18:26:17 +0000503
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000504 return true;
Akira Hatanakaa5352702011-03-31 18:26:17 +0000505}
506
507// Creates and returns an FPCmp node from a setcc node.
508// Returns Op if setcc is not a floating point comparison.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000509static SDValue createFPCmp(SelectionDAG &DAG, const SDValue &Op) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000510 // must be a SETCC node
511 if (Op.getOpcode() != ISD::SETCC)
512 return Op;
513
514 SDValue LHS = Op.getOperand(0);
515
516 if (!LHS.getValueType().isFloatingPoint())
517 return Op;
518
519 SDValue RHS = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000520 SDLoc DL(Op);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000521
Akira Hatanakaaef55c82011-04-15 21:00:26 +0000522 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
523 // node if necessary.
Akira Hatanakaa5352702011-03-31 18:26:17 +0000524 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
525
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000526 return DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS,
Akira Hatanaka89af5892013-04-18 01:00:46 +0000527 DAG.getConstant(condCodeToFCC(CC), MVT::i32));
Akira Hatanakaa5352702011-03-31 18:26:17 +0000528}
529
530// Creates and returns a CMovFPT/F node.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000531static SDValue createCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000532 SDValue False, SDLoc DL) {
Akira Hatanakaf0ea5002013-03-30 01:16:38 +0000533 ConstantSDNode *CC = cast<ConstantSDNode>(Cond.getOperand(2));
534 bool invert = invertFPCondCodeUser((Mips::CondCode)CC->getSExtValue());
Akira Hatanaka8bce21c2013-07-26 20:51:20 +0000535 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000536
537 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
Akira Hatanaka8bce21c2013-07-26 20:51:20 +0000538 True.getValueType(), True, FCC0, False, Cond);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000539}
540
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000541static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000542 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000543 const MipsSubtarget &Subtarget) {
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000544 if (DCI.isBeforeLegalizeOps())
545 return SDValue();
546
547 SDValue SetCC = N->getOperand(0);
548
549 if ((SetCC.getOpcode() != ISD::SETCC) ||
550 !SetCC.getOperand(0).getValueType().isInteger())
551 return SDValue();
552
553 SDValue False = N->getOperand(2);
554 EVT FalseTy = False.getValueType();
555
556 if (!FalseTy.isInteger())
557 return SDValue();
558
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000559 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(False);
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000560
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000561 // If the RHS (False) is 0, we swap the order of the operands
562 // of ISD::SELECT (obviously also inverting the condition) so that we can
563 // take advantage of conditional moves using the $0 register.
564 // Example:
565 // return (a != 0) ? x : 0;
566 // load $reg, x
567 // movz $reg, $0, a
568 if (!FalseC)
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000569 return SDValue();
570
Andrew Trickef9de2a2013-05-25 02:42:55 +0000571 const SDLoc DL(N);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000572
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000573 if (!FalseC->getZExtValue()) {
574 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
575 SDValue True = N->getOperand(1);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000576
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000577 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
578 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
579
580 return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True);
581 }
582
Matheus Almeidaa6beac12013-12-05 12:07:05 +0000583 // If both operands are integer constants there's a possibility that we
584 // can do some interesting optimizations.
585 SDValue True = N->getOperand(1);
586 ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(True);
587
588 if (!TrueC || !True.getValueType().isInteger())
589 return SDValue();
590
591 // We'll also ignore MVT::i64 operands as this optimizations proves
592 // to be ineffective because of the required sign extensions as the result
593 // of a SETCC operator is always MVT::i32 for non-vector types.
594 if (True.getValueType() == MVT::i64)
595 return SDValue();
596
597 int64_t Diff = TrueC->getSExtValue() - FalseC->getSExtValue();
598
599 // 1) (a < x) ? y : y-1
600 // slti $reg1, a, x
601 // addiu $reg2, $reg1, y-1
602 if (Diff == 1)
603 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, False);
604
605 // 2) (a < x) ? y-1 : y
606 // slti $reg1, a, x
607 // xor $reg1, $reg1, 1
608 // addiu $reg2, $reg1, y-1
609 if (Diff == -1) {
610 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
611 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
612 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
613 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, True);
614 }
615
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000616 // Couldn't optimize.
617 return SDValue();
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000618}
619
Vasileios Kalintirise741eb22015-03-02 12:47:32 +0000620static SDValue performCMovFPCombine(SDNode *N, SelectionDAG &DAG,
621 TargetLowering::DAGCombinerInfo &DCI,
622 const MipsSubtarget &Subtarget) {
623 if (DCI.isBeforeLegalizeOps())
624 return SDValue();
625
626 SDValue ValueIfTrue = N->getOperand(0), ValueIfFalse = N->getOperand(2);
627
628 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(ValueIfFalse);
629 if (!FalseC || FalseC->getZExtValue())
630 return SDValue();
631
632 // Since RHS (False) is 0, we swap the order of the True/False operands
633 // (obviously also inverting the condition) so that we can
634 // take advantage of conditional moves using the $0 register.
635 // Example:
636 // return (a != 0) ? x : 0;
637 // load $reg, x
638 // movz $reg, $0, a
639 unsigned Opc = (N->getOpcode() == MipsISD::CMovFP_T) ? MipsISD::CMovFP_F :
640 MipsISD::CMovFP_T;
641
642 SDValue FCC = N->getOperand(1), Glue = N->getOperand(3);
Vasileios Kalintiris2ef28882015-03-04 12:10:18 +0000643 return DAG.getNode(Opc, SDLoc(N), ValueIfFalse.getValueType(),
644 ValueIfFalse, FCC, ValueIfTrue, Glue);
Vasileios Kalintirise741eb22015-03-02 12:47:32 +0000645}
646
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000647static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000648 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000649 const MipsSubtarget &Subtarget) {
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000650 // Pattern match EXT.
651 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
652 // => ext $dst, $src, size, pos
Eric Christopher1c29a652014-07-18 22:55:25 +0000653 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasExtractInsert())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000654 return SDValue();
655
656 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000657 unsigned ShiftRightOpc = ShiftRight.getOpcode();
658
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000659 // Op's first operand must be a shift right.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000660 if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL)
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000661 return SDValue();
662
663 // The second operand of the shift must be an immediate.
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000664 ConstantSDNode *CN;
665 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
666 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000667
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000668 uint64_t Pos = CN->getZExtValue();
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000669 uint64_t SMPos, SMSize;
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000670
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000671 // Op's second operand must be a shifted mask.
672 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000673 !isShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000674 return SDValue();
675
676 // Return if the shifted mask does not start at bit 0 or the sum of its size
677 // and Pos exceeds the word's size.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000678 EVT ValTy = N->getValueType(0);
679 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000680 return SDValue();
681
Andrew Trickef9de2a2013-05-25 02:42:55 +0000682 return DAG.getNode(MipsISD::Ext, SDLoc(N), ValTy,
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000683 ShiftRight.getOperand(0), DAG.getConstant(Pos, MVT::i32),
Akira Hatanakaeea541c2011-08-17 22:59:46 +0000684 DAG.getConstant(SMSize, MVT::i32));
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000685}
Jia Liuf54f60f2012-02-28 07:46:26 +0000686
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000687static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000688 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000689 const MipsSubtarget &Subtarget) {
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000690 // Pattern match INS.
691 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
Jia Liuf54f60f2012-02-28 07:46:26 +0000692 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000693 // => ins $dst, $src, size, pos, $src1
Eric Christopher1c29a652014-07-18 22:55:25 +0000694 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasExtractInsert())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000695 return SDValue();
696
697 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
698 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
699 ConstantSDNode *CN;
700
701 // See if Op's first operand matches (and $src1 , mask0).
702 if (And0.getOpcode() != ISD::AND)
703 return SDValue();
704
705 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000706 !isShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000707 return SDValue();
708
709 // See if Op's second operand matches (and (shl $src, pos), mask1).
710 if (And1.getOpcode() != ISD::AND)
711 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000712
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000713 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000714 !isShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000715 return SDValue();
716
717 // The shift masks must have the same position and size.
718 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
719 return SDValue();
720
721 SDValue Shl = And1.getOperand(0);
722 if (Shl.getOpcode() != ISD::SHL)
723 return SDValue();
724
725 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
726 return SDValue();
727
728 unsigned Shamt = CN->getZExtValue();
729
730 // Return if the shift amount and the first bit position of mask are not the
Jia Liuf54f60f2012-02-28 07:46:26 +0000731 // same.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000732 EVT ValTy = N->getValueType(0);
733 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000734 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000735
Andrew Trickef9de2a2013-05-25 02:42:55 +0000736 return DAG.getNode(MipsISD::Ins, SDLoc(N), ValTy, Shl.getOperand(0),
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000737 DAG.getConstant(SMPos0, MVT::i32),
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000738 DAG.getConstant(SMSize0, MVT::i32), And0.getOperand(0));
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000739}
Jia Liuf54f60f2012-02-28 07:46:26 +0000740
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000741static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000742 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000743 const MipsSubtarget &Subtarget) {
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000744 // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt))
745
746 if (DCI.isBeforeLegalizeOps())
747 return SDValue();
748
749 SDValue Add = N->getOperand(1);
750
751 if (Add.getOpcode() != ISD::ADD)
752 return SDValue();
753
754 SDValue Lo = Add.getOperand(1);
755
756 if ((Lo.getOpcode() != MipsISD::Lo) ||
757 (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable))
758 return SDValue();
759
760 EVT ValTy = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000761 SDLoc DL(N);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000762
763 SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, N->getOperand(0),
764 Add.getOperand(0));
765 return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo);
766}
767
Bruno Cardoso Lopes61a61e92011-02-10 18:05:10 +0000768SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000769 const {
770 SelectionDAG &DAG = DCI.DAG;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000771 unsigned Opc = N->getOpcode();
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000772
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000773 switch (Opc) {
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000774 default: break;
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000775 case ISD::SDIVREM:
776 case ISD::UDIVREM:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000777 return performDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000778 case ISD::SELECT:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000779 return performSELECTCombine(N, DAG, DCI, Subtarget);
Vasileios Kalintirise741eb22015-03-02 12:47:32 +0000780 case MipsISD::CMovFP_F:
781 case MipsISD::CMovFP_T:
782 return performCMovFPCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000783 case ISD::AND:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000784 return performANDCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000785 case ISD::OR:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000786 return performORCombine(N, DAG, DCI, Subtarget);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000787 case ISD::ADD:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000788 return performADDCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000789 }
790
791 return SDValue();
792}
793
Akira Hatanakafabb8cf2012-09-21 23:58:31 +0000794void
795MipsTargetLowering::LowerOperationWrapper(SDNode *N,
796 SmallVectorImpl<SDValue> &Results,
797 SelectionDAG &DAG) const {
798 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
799
800 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
801 Results.push_back(Res.getValue(I));
802}
803
804void
805MipsTargetLowering::ReplaceNodeResults(SDNode *N,
806 SmallVectorImpl<SDValue> &Results,
807 SelectionDAG &DAG) const {
Akira Hatanaka9da442f2013-04-30 21:17:07 +0000808 return LowerOperationWrapper(N, Results, DAG);
Akira Hatanakafabb8cf2012-09-21 23:58:31 +0000809}
810
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000811SDValue MipsTargetLowering::
Dan Gohman21cea8a2010-04-17 15:26:15 +0000812LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000813{
Wesley Peck527da1b2010-11-23 03:31:01 +0000814 switch (Op.getOpcode())
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000815 {
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000816 case ISD::BR_JT: return lowerBR_JT(Op, DAG);
817 case ISD::BRCOND: return lowerBRCOND(Op, DAG);
818 case ISD::ConstantPool: return lowerConstantPool(Op, DAG);
819 case ISD::GlobalAddress: return lowerGlobalAddress(Op, DAG);
820 case ISD::BlockAddress: return lowerBlockAddress(Op, DAG);
821 case ISD::GlobalTLSAddress: return lowerGlobalTLSAddress(Op, DAG);
822 case ISD::JumpTable: return lowerJumpTable(Op, DAG);
823 case ISD::SELECT: return lowerSELECT(Op, DAG);
824 case ISD::SELECT_CC: return lowerSELECT_CC(Op, DAG);
825 case ISD::SETCC: return lowerSETCC(Op, DAG);
826 case ISD::VASTART: return lowerVASTART(Op, DAG);
Daniel Sanders2b553d42014-08-01 09:17:39 +0000827 case ISD::VAARG: return lowerVAARG(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000828 case ISD::FCOPYSIGN: return lowerFCOPYSIGN(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000829 case ISD::FRAMEADDR: return lowerFRAMEADDR(Op, DAG);
830 case ISD::RETURNADDR: return lowerRETURNADDR(Op, DAG);
831 case ISD::EH_RETURN: return lowerEH_RETURN(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000832 case ISD::ATOMIC_FENCE: return lowerATOMIC_FENCE(Op, DAG);
833 case ISD::SHL_PARTS: return lowerShiftLeftParts(Op, DAG);
834 case ISD::SRA_PARTS: return lowerShiftRightParts(Op, DAG, true);
835 case ISD::SRL_PARTS: return lowerShiftRightParts(Op, DAG, false);
836 case ISD::LOAD: return lowerLOAD(Op, DAG);
837 case ISD::STORE: return lowerSTORE(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000838 case ISD::ADD: return lowerADD(Op, DAG);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000839 case ISD::FP_TO_SINT: return lowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000840 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000841 return SDValue();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000842}
843
Akira Hatanakae2489122011-04-15 21:51:11 +0000844//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000845// Lower helper functions
Akira Hatanakae2489122011-04-15 21:51:11 +0000846//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000847
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000848// addLiveIn - This helper function adds the specified physical register to the
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000849// MachineFunction as a live in value. It also creates a corresponding
850// virtual register for it.
851static unsigned
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000852addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000853{
Chris Lattnera10fff52007-12-31 04:13:23 +0000854 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
855 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000856 return VReg;
857}
858
Daniel Sanders308181e2014-06-12 10:44:10 +0000859static MachineBasicBlock *insertDivByZeroTrap(MachineInstr *MI,
860 MachineBasicBlock &MBB,
861 const TargetInstrInfo &TII,
862 bool Is64Bit) {
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000863 if (NoZeroDivCheck)
864 return &MBB;
865
866 // Insert instruction "teq $divisor_reg, $zero, 7".
867 MachineBasicBlock::iterator I(MI);
868 MachineInstrBuilder MIB;
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000869 MachineOperand &Divisor = MI->getOperand(2);
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000870 MIB = BuildMI(MBB, std::next(I), MI->getDebugLoc(), TII.get(Mips::TEQ))
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000871 .addReg(Divisor.getReg(), getKillRegState(Divisor.isKill()))
872 .addReg(Mips::ZERO).addImm(7);
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000873
874 // Use the 32-bit sub-register if this is a 64-bit division.
875 if (Is64Bit)
876 MIB->getOperand(0).setSubReg(Mips::sub_32);
877
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000878 // Clear Divisor's kill flag.
879 Divisor.setIsKill(false);
Daniel Sanders308181e2014-06-12 10:44:10 +0000880
881 // We would normally delete the original instruction here but in this case
882 // we only needed to inject an additional instruction rather than replace it.
883
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000884 return &MBB;
885}
886
Akira Hatanakae4bd0542012-09-27 02:15:57 +0000887MachineBasicBlock *
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +0000888MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +0000889 MachineBasicBlock *BB) const {
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +0000890 switch (MI->getOpcode()) {
Reed Kotler97ba5f22013-02-21 04:22:38 +0000891 default:
892 llvm_unreachable("Unexpected instr type to insert");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000893 case Mips::ATOMIC_LOAD_ADD_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000894 return emitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000895 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000896 return emitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000897 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000898 return emitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000899 case Mips::ATOMIC_LOAD_ADD_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000900 return emitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000901
902 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000903 return emitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000904 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000905 return emitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000906 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000907 return emitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000908 case Mips::ATOMIC_LOAD_AND_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000909 return emitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000910
911 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000912 return emitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000913 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000914 return emitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000915 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000916 return emitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000917 case Mips::ATOMIC_LOAD_OR_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000918 return emitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000919
920 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000921 return emitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000922 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000923 return emitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000924 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000925 return emitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000926 case Mips::ATOMIC_LOAD_XOR_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000927 return emitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000928
929 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000930 return emitAtomicBinaryPartword(MI, BB, 1, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000931 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000932 return emitAtomicBinaryPartword(MI, BB, 2, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000933 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000934 return emitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000935 case Mips::ATOMIC_LOAD_NAND_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000936 return emitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000937
938 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000939 return emitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000940 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000941 return emitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000942 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000943 return emitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000944 case Mips::ATOMIC_LOAD_SUB_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000945 return emitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000946
947 case Mips::ATOMIC_SWAP_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000948 return emitAtomicBinaryPartword(MI, BB, 1, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000949 case Mips::ATOMIC_SWAP_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000950 return emitAtomicBinaryPartword(MI, BB, 2, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000951 case Mips::ATOMIC_SWAP_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000952 return emitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000953 case Mips::ATOMIC_SWAP_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000954 return emitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000955
956 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000957 return emitAtomicCmpSwapPartword(MI, BB, 1);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000958 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000959 return emitAtomicCmpSwapPartword(MI, BB, 2);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000960 case Mips::ATOMIC_CMP_SWAP_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000961 return emitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000962 case Mips::ATOMIC_CMP_SWAP_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000963 return emitAtomicCmpSwap(MI, BB, 8);
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000964 case Mips::PseudoSDIV:
965 case Mips::PseudoUDIV:
Daniel Sanders308181e2014-06-12 10:44:10 +0000966 case Mips::DIV:
967 case Mips::DIVU:
968 case Mips::MOD:
969 case Mips::MODU:
Eric Christopher96e72c62015-01-29 23:27:36 +0000970 return insertDivByZeroTrap(MI, *BB, *Subtarget.getInstrInfo(), false);
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000971 case Mips::PseudoDSDIV:
972 case Mips::PseudoDUDIV:
Daniel Sanders308181e2014-06-12 10:44:10 +0000973 case Mips::DDIV:
974 case Mips::DDIVU:
975 case Mips::DMOD:
976 case Mips::DMODU:
Eric Christopher96e72c62015-01-29 23:27:36 +0000977 return insertDivByZeroTrap(MI, *BB, *Subtarget.getInstrInfo(), true);
Daniel Sanders0fa60412014-06-12 13:39:06 +0000978 case Mips::SEL_D:
979 return emitSEL_D(MI, BB);
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +0000980
981 case Mips::PseudoSELECT_I:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +0000982 case Mips::PseudoSELECT_I64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +0000983 case Mips::PseudoSELECT_S:
984 case Mips::PseudoSELECT_D32:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +0000985 case Mips::PseudoSELECT_D64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +0000986 return emitPseudoSELECT(MI, BB, false, Mips::BNE);
987 case Mips::PseudoSELECTFP_F_I:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +0000988 case Mips::PseudoSELECTFP_F_I64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +0000989 case Mips::PseudoSELECTFP_F_S:
990 case Mips::PseudoSELECTFP_F_D32:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +0000991 case Mips::PseudoSELECTFP_F_D64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +0000992 return emitPseudoSELECT(MI, BB, true, Mips::BC1F);
993 case Mips::PseudoSELECTFP_T_I:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +0000994 case Mips::PseudoSELECTFP_T_I64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +0000995 case Mips::PseudoSELECTFP_T_S:
996 case Mips::PseudoSELECTFP_T_D32:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +0000997 case Mips::PseudoSELECTFP_T_D64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +0000998 return emitPseudoSELECT(MI, BB, true, Mips::BC1T);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000999 }
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001000}
1001
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001002// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
1003// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
1004MachineBasicBlock *
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001005MipsTargetLowering::emitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher0713a9d2011-06-08 23:55:35 +00001006 unsigned Size, unsigned BinOpcode,
Akira Hatanaka15506782011-06-07 18:58:42 +00001007 bool Nand) const {
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001008 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001009
1010 MachineFunction *MF = BB->getParent();
1011 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001012 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Eric Christopher96e72c62015-01-29 23:27:36 +00001013 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001014 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001015 unsigned LL, SC, AND, NOR, ZERO, BEQ;
1016
1017 if (Size == 4) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001018 if (isMicroMips) {
1019 LL = Mips::LL_MM;
1020 SC = Mips::SC_MM;
1021 } else {
Daniel Sandersbdcfab12014-07-24 09:47:14 +00001022 LL = Subtarget.hasMips32r6() ? Mips::LL_R6 : Mips::LL;
1023 SC = Subtarget.hasMips32r6() ? Mips::SC_R6 : Mips::SC;
Daniel Sanders6a803f62014-06-16 13:13:03 +00001024 }
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001025 AND = Mips::AND;
1026 NOR = Mips::NOR;
1027 ZERO = Mips::ZERO;
1028 BEQ = Mips::BEQ;
Daniel Sanders6a803f62014-06-16 13:13:03 +00001029 } else {
Daniel Sandersbdcfab12014-07-24 09:47:14 +00001030 LL = Subtarget.hasMips64r6() ? Mips::LLD_R6 : Mips::LLD;
1031 SC = Subtarget.hasMips64r6() ? Mips::SCD_R6 : Mips::SCD;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001032 AND = Mips::AND64;
1033 NOR = Mips::NOR64;
1034 ZERO = Mips::ZERO_64;
1035 BEQ = Mips::BEQ64;
1036 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001037
Akira Hatanaka0e019592011-07-19 20:11:17 +00001038 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001039 unsigned Ptr = MI->getOperand(1).getReg();
1040 unsigned Incr = MI->getOperand(2).getReg();
1041
Akira Hatanaka0e019592011-07-19 20:11:17 +00001042 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1043 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1044 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001045
1046 // insert new blocks after the current block
1047 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1048 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1049 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1050 MachineFunction::iterator It = BB;
1051 ++It;
1052 MF->insert(It, loopMBB);
1053 MF->insert(It, exitMBB);
1054
1055 // Transfer the remainder of BB and its successor edges to exitMBB.
1056 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001057 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001058 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1059
1060 // thisMBB:
1061 // ...
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001062 // fallthrough --> loopMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001063 BB->addSuccessor(loopMBB);
Akira Hatanaka08636b42011-07-19 17:09:53 +00001064 loopMBB->addSuccessor(loopMBB);
1065 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001066
1067 // loopMBB:
1068 // ll oldval, 0(ptr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001069 // <binop> storeval, oldval, incr
1070 // sc success, storeval, 0(ptr)
1071 // beq success, $0, loopMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001072 BB = loopMBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001073 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001074 if (Nand) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001075 // and andres, oldval, incr
1076 // nor storeval, $0, andres
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001077 BuildMI(BB, DL, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
1078 BuildMI(BB, DL, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001079 } else if (BinOpcode) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001080 // <binop> storeval, oldval, incr
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001081 BuildMI(BB, DL, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001082 } else {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001083 StoreVal = Incr;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001084 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001085 BuildMI(BB, DL, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
1086 BuildMI(BB, DL, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001087
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001088 MI->eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001089
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001090 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001091}
1092
Daniel Sanders6a803f62014-06-16 13:13:03 +00001093MachineBasicBlock *MipsTargetLowering::emitSignExtendToI32InReg(
1094 MachineInstr *MI, MachineBasicBlock *BB, unsigned Size, unsigned DstReg,
1095 unsigned SrcReg) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00001096 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders6a803f62014-06-16 13:13:03 +00001097 DebugLoc DL = MI->getDebugLoc();
1098
Eric Christopher1c29a652014-07-18 22:55:25 +00001099 if (Subtarget.hasMips32r2() && Size == 1) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001100 BuildMI(BB, DL, TII->get(Mips::SEB), DstReg).addReg(SrcReg);
1101 return BB;
1102 }
1103
Eric Christopher1c29a652014-07-18 22:55:25 +00001104 if (Subtarget.hasMips32r2() && Size == 2) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001105 BuildMI(BB, DL, TII->get(Mips::SEH), DstReg).addReg(SrcReg);
1106 return BB;
1107 }
1108
1109 MachineFunction *MF = BB->getParent();
1110 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1111 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1112 unsigned ScrReg = RegInfo.createVirtualRegister(RC);
1113
1114 assert(Size < 32);
1115 int64_t ShiftImm = 32 - (Size * 8);
1116
1117 BuildMI(BB, DL, TII->get(Mips::SLL), ScrReg).addReg(SrcReg).addImm(ShiftImm);
1118 BuildMI(BB, DL, TII->get(Mips::SRA), DstReg).addReg(ScrReg).addImm(ShiftImm);
1119
1120 return BB;
1121}
1122
1123MachineBasicBlock *MipsTargetLowering::emitAtomicBinaryPartword(
1124 MachineInstr *MI, MachineBasicBlock *BB, unsigned Size, unsigned BinOpcode,
1125 bool Nand) const {
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001126 assert((Size == 1 || Size == 2) &&
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001127 "Unsupported size for EmitAtomicBinaryPartial.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001128
1129 MachineFunction *MF = BB->getParent();
1130 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1131 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
Eric Christopher96e72c62015-01-29 23:27:36 +00001132 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001133 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001134
1135 unsigned Dest = MI->getOperand(0).getReg();
1136 unsigned Ptr = MI->getOperand(1).getReg();
1137 unsigned Incr = MI->getOperand(2).getReg();
1138
Akira Hatanaka0e019592011-07-19 20:11:17 +00001139 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1140 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001141 unsigned Mask = RegInfo.createVirtualRegister(RC);
1142 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001143 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1144 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001145 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001146 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1147 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1148 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1149 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1150 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka9663dd32011-07-19 20:56:53 +00001151 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001152 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1153 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1154 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001155 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001156
1157 // insert new blocks after the current block
1158 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1159 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001160 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001161 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1162 MachineFunction::iterator It = BB;
1163 ++It;
1164 MF->insert(It, loopMBB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001165 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001166 MF->insert(It, exitMBB);
1167
1168 // Transfer the remainder of BB and its successor edges to exitMBB.
1169 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001170 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001171 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1172
Akira Hatanaka08636b42011-07-19 17:09:53 +00001173 BB->addSuccessor(loopMBB);
1174 loopMBB->addSuccessor(loopMBB);
1175 loopMBB->addSuccessor(sinkMBB);
1176 sinkMBB->addSuccessor(exitMBB);
1177
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001178 // thisMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001179 // addiu masklsb2,$0,-4 # 0xfffffffc
1180 // and alignedaddr,ptr,masklsb2
1181 // andi ptrlsb2,ptr,3
1182 // sll shiftamt,ptrlsb2,3
1183 // ori maskupper,$0,255 # 0xff
1184 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001185 // nor mask2,$0,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001186 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001187
1188 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001189 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001190 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001191 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001192 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001193 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Eric Christopher1c29a652014-07-18 22:55:25 +00001194 if (Subtarget.isLittle()) {
Akira Hatanaka2bf97332013-05-31 03:25:44 +00001195 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1196 } else {
1197 unsigned Off = RegInfo.createVirtualRegister(RC);
1198 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1199 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1200 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1201 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001202 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001203 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001204 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001205 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001206 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001207 BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(Incr).addReg(ShiftAmt);
Bruno Cardoso Lopesf771a0f2011-05-31 20:25:26 +00001208
Akira Hatanaka27292632011-07-18 18:52:12 +00001209 // atomic.load.binop
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001210 // loopMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001211 // ll oldval,0(alignedaddr)
1212 // binop binopres,oldval,incr2
1213 // and newval,binopres,mask
1214 // and maskedoldval0,oldval,mask2
1215 // or storeval,maskedoldval0,newval
1216 // sc success,storeval,0(alignedaddr)
1217 // beq success,$0,loopMBB
1218
Akira Hatanaka27292632011-07-18 18:52:12 +00001219 // atomic.swap
1220 // loopMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001221 // ll oldval,0(alignedaddr)
Akira Hatanakae4503582011-07-19 18:14:26 +00001222 // and newval,incr2,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001223 // and maskedoldval0,oldval,mask2
1224 // or storeval,maskedoldval0,newval
1225 // sc success,storeval,0(alignedaddr)
1226 // beq success,$0,loopMBB
Akira Hatanaka27292632011-07-18 18:52:12 +00001227
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001228 BB = loopMBB;
Jozef Kolek2f27d572014-12-18 16:39:29 +00001229 unsigned LL = isMicroMips ? Mips::LL_MM : Mips::LL;
1230 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001231 if (Nand) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001232 // and andres, oldval, incr2
1233 // nor binopres, $0, andres
1234 // and newval, binopres, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001235 BuildMI(BB, DL, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1236 BuildMI(BB, DL, TII->get(Mips::NOR), BinOpRes)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001237 .addReg(Mips::ZERO).addReg(AndRes);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001238 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001239 } else if (BinOpcode) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001240 // <binop> binopres, oldval, incr2
1241 // and newval, binopres, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001242 BuildMI(BB, DL, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1243 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001244 } else { // atomic.swap
Akira Hatanaka0e019592011-07-19 20:11:17 +00001245 // and newval, incr2, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001246 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanakae4503582011-07-19 18:14:26 +00001247 }
Jia Liuf54f60f2012-02-28 07:46:26 +00001248
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001249 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001250 .addReg(OldVal).addReg(Mask2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001251 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka9663dd32011-07-19 20:56:53 +00001252 .addReg(MaskedOldVal0).addReg(NewVal);
Jozef Kolek2f27d572014-12-18 16:39:29 +00001253 unsigned SC = isMicroMips ? Mips::SC_MM : Mips::SC;
1254 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001255 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001256 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001257 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001258
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001259 // sinkMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001260 // and maskedoldval1,oldval,mask
1261 // srl srlres,maskedoldval1,shiftamt
Daniel Sanders6a803f62014-06-16 13:13:03 +00001262 // sign_extend dest,srlres
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001263 BB = sinkMBB;
Akira Hatanakae97bd812011-07-19 03:14:58 +00001264
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001265 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001266 .addReg(OldVal).addReg(Mask);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001267 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001268 .addReg(MaskedOldVal1).addReg(ShiftAmt);
Daniel Sanders6a803f62014-06-16 13:13:03 +00001269 BB = emitSignExtendToI32InReg(MI, BB, Size, Dest, SrlRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001270
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001271 MI->eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001272
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001273 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001274}
1275
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001276MachineBasicBlock * MipsTargetLowering::emitAtomicCmpSwap(MachineInstr *MI,
1277 MachineBasicBlock *BB,
1278 unsigned Size) const {
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001279 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001280
1281 MachineFunction *MF = BB->getParent();
1282 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001283 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Eric Christopher96e72c62015-01-29 23:27:36 +00001284 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001285 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001286 unsigned LL, SC, ZERO, BNE, BEQ;
1287
1288 if (Size == 4) {
Zoran Jovanovicff9d5f32013-12-19 16:12:56 +00001289 LL = isMicroMips ? Mips::LL_MM : Mips::LL;
1290 SC = isMicroMips ? Mips::SC_MM : Mips::SC;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001291 ZERO = Mips::ZERO;
1292 BNE = Mips::BNE;
1293 BEQ = Mips::BEQ;
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001294 } else {
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001295 LL = Mips::LLD;
1296 SC = Mips::SCD;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001297 ZERO = Mips::ZERO_64;
1298 BNE = Mips::BNE64;
1299 BEQ = Mips::BEQ64;
1300 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001301
1302 unsigned Dest = MI->getOperand(0).getReg();
1303 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka0e019592011-07-19 20:11:17 +00001304 unsigned OldVal = MI->getOperand(2).getReg();
1305 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001306
Akira Hatanaka0e019592011-07-19 20:11:17 +00001307 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001308
1309 // insert new blocks after the current block
1310 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1311 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1312 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1313 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1314 MachineFunction::iterator It = BB;
1315 ++It;
1316 MF->insert(It, loop1MBB);
1317 MF->insert(It, loop2MBB);
1318 MF->insert(It, exitMBB);
1319
1320 // Transfer the remainder of BB and its successor edges to exitMBB.
1321 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001322 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001323 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1324
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001325 // thisMBB:
1326 // ...
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001327 // fallthrough --> loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001328 BB->addSuccessor(loop1MBB);
Akira Hatanaka08636b42011-07-19 17:09:53 +00001329 loop1MBB->addSuccessor(exitMBB);
1330 loop1MBB->addSuccessor(loop2MBB);
1331 loop2MBB->addSuccessor(loop1MBB);
1332 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001333
1334 // loop1MBB:
1335 // ll dest, 0(ptr)
1336 // bne dest, oldval, exitMBB
1337 BB = loop1MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001338 BuildMI(BB, DL, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1339 BuildMI(BB, DL, TII->get(BNE))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001340 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001341
1342 // loop2MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001343 // sc success, newval, 0(ptr)
1344 // beq success, $0, loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001345 BB = loop2MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001346 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001347 .addReg(NewVal).addReg(Ptr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001348 BuildMI(BB, DL, TII->get(BEQ))
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001349 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001350
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001351 MI->eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001352
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001353 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001354}
1355
1356MachineBasicBlock *
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001357MipsTargetLowering::emitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka15506782011-06-07 18:58:42 +00001358 MachineBasicBlock *BB,
1359 unsigned Size) const {
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001360 assert((Size == 1 || Size == 2) &&
1361 "Unsupported size for EmitAtomicCmpSwapPartial.");
1362
1363 MachineFunction *MF = BB->getParent();
1364 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1365 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
Eric Christopher96e72c62015-01-29 23:27:36 +00001366 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001367 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001368
1369 unsigned Dest = MI->getOperand(0).getReg();
1370 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka0e019592011-07-19 20:11:17 +00001371 unsigned CmpVal = MI->getOperand(2).getReg();
1372 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001373
Akira Hatanaka0e019592011-07-19 20:11:17 +00001374 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1375 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001376 unsigned Mask = RegInfo.createVirtualRegister(RC);
1377 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001378 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1379 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1380 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1381 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1382 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1383 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1384 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1385 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1386 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1387 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1388 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1389 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001390 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001391
1392 // insert new blocks after the current block
1393 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1394 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1395 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001396 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001397 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1398 MachineFunction::iterator It = BB;
1399 ++It;
1400 MF->insert(It, loop1MBB);
1401 MF->insert(It, loop2MBB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001402 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001403 MF->insert(It, exitMBB);
1404
1405 // Transfer the remainder of BB and its successor edges to exitMBB.
1406 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001407 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001408 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1409
Akira Hatanaka08636b42011-07-19 17:09:53 +00001410 BB->addSuccessor(loop1MBB);
1411 loop1MBB->addSuccessor(sinkMBB);
1412 loop1MBB->addSuccessor(loop2MBB);
1413 loop2MBB->addSuccessor(loop1MBB);
1414 loop2MBB->addSuccessor(sinkMBB);
1415 sinkMBB->addSuccessor(exitMBB);
1416
Akira Hatanakae4503582011-07-19 18:14:26 +00001417 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001418 // thisMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001419 // addiu masklsb2,$0,-4 # 0xfffffffc
1420 // and alignedaddr,ptr,masklsb2
1421 // andi ptrlsb2,ptr,3
1422 // sll shiftamt,ptrlsb2,3
1423 // ori maskupper,$0,255 # 0xff
1424 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001425 // nor mask2,$0,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001426 // andi maskedcmpval,cmpval,255
1427 // sll shiftedcmpval,maskedcmpval,shiftamt
1428 // andi maskednewval,newval,255
1429 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001430 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001431 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001432 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001433 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001434 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001435 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Eric Christopher1c29a652014-07-18 22:55:25 +00001436 if (Subtarget.isLittle()) {
Akira Hatanaka2bf97332013-05-31 03:25:44 +00001437 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1438 } else {
1439 unsigned Off = RegInfo.createVirtualRegister(RC);
1440 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1441 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1442 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1443 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001444 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001445 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001446 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001447 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001448 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1449 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001450 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001451 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001452 .addReg(MaskedCmpVal).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001453 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001454 .addReg(NewVal).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001455 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001456 .addReg(MaskedNewVal).addReg(ShiftAmt);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001457
1458 // loop1MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001459 // ll oldval,0(alginedaddr)
1460 // and maskedoldval0,oldval,mask
1461 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001462 BB = loop1MBB;
Jozef Kolek2f27d572014-12-18 16:39:29 +00001463 unsigned LL = isMicroMips ? Mips::LL_MM : Mips::LL;
1464 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001465 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001466 .addReg(OldVal).addReg(Mask);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001467 BuildMI(BB, DL, TII->get(Mips::BNE))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001468 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001469
1470 // loop2MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001471 // and maskedoldval1,oldval,mask2
1472 // or storeval,maskedoldval1,shiftednewval
1473 // sc success,storeval,0(alignedaddr)
1474 // beq success,$0,loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001475 BB = loop2MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001476 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001477 .addReg(OldVal).addReg(Mask2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001478 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001479 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
Jozef Kolek2f27d572014-12-18 16:39:29 +00001480 unsigned SC = isMicroMips ? Mips::SC_MM : Mips::SC;
1481 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001482 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001483 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001484 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001485
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001486 // sinkMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001487 // srl srlres,maskedoldval0,shiftamt
Daniel Sanders6a803f62014-06-16 13:13:03 +00001488 // sign_extend dest,srlres
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001489 BB = sinkMBB;
Akira Hatanakae97bd812011-07-19 03:14:58 +00001490
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001491 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001492 .addReg(MaskedOldVal0).addReg(ShiftAmt);
Daniel Sanders6a803f62014-06-16 13:13:03 +00001493 BB = emitSignExtendToI32InReg(MI, BB, Size, Dest, SrlRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001494
1495 MI->eraseFromParent(); // The instruction is gone now.
1496
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001497 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001498}
1499
Daniel Sanders0fa60412014-06-12 13:39:06 +00001500MachineBasicBlock *MipsTargetLowering::emitSEL_D(MachineInstr *MI,
1501 MachineBasicBlock *BB) const {
1502 MachineFunction *MF = BB->getParent();
Eric Christopher96e72c62015-01-29 23:27:36 +00001503 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
1504 const TargetInstrInfo *TII = Subtarget.getInstrInfo();
Daniel Sanders0fa60412014-06-12 13:39:06 +00001505 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1506 DebugLoc DL = MI->getDebugLoc();
1507 MachineBasicBlock::iterator II(MI);
1508
1509 unsigned Fc = MI->getOperand(1).getReg();
1510 const auto &FGR64RegClass = TRI->getRegClass(Mips::FGR64RegClassID);
1511
1512 unsigned Fc2 = RegInfo.createVirtualRegister(FGR64RegClass);
1513
1514 BuildMI(*BB, II, DL, TII->get(Mips::SUBREG_TO_REG), Fc2)
1515 .addImm(0)
1516 .addReg(Fc)
1517 .addImm(Mips::sub_lo);
1518
1519 // We don't erase the original instruction, we just replace the condition
1520 // register with the 64-bit super-register.
1521 MI->getOperand(1).setReg(Fc2);
1522
1523 return BB;
1524}
1525
Akira Hatanakae2489122011-04-15 21:51:11 +00001526//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001527// Misc Lower Operation implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00001528//===----------------------------------------------------------------------===//
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001529SDValue MipsTargetLowering::lowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001530 SDValue Chain = Op.getOperand(0);
1531 SDValue Table = Op.getOperand(1);
1532 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001533 SDLoc DL(Op);
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001534 EVT PTy = getPointerTy();
1535 unsigned EntrySize =
1536 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(*getDataLayout());
1537
1538 Index = DAG.getNode(ISD::MUL, DL, PTy, Index,
1539 DAG.getConstant(EntrySize, PTy));
1540 SDValue Addr = DAG.getNode(ISD::ADD, DL, PTy, Index, Table);
1541
1542 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
1543 Addr = DAG.getExtLoad(ISD::SEXTLOAD, DL, PTy, Chain, Addr,
1544 MachinePointerInfo::getJumpTable(), MemVT, false, false,
Louis Gerbarg67474e32014-07-31 21:45:05 +00001545 false, 0);
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001546 Chain = Addr.getValue(1);
1547
Eric Christopher96e72c62015-01-29 23:27:36 +00001548 if ((getTargetMachine().getRelocationModel() == Reloc::PIC_) || ABI.IsN64()) {
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001549 // For PIC, the sequence is:
1550 // BRIND(load(Jumptable + index) + RelocBase)
1551 // RelocBase can be JumpTable, GOT or some sort of global base.
1552 Addr = DAG.getNode(ISD::ADD, DL, PTy, Addr,
1553 getPICJumpTableRelocBase(Table, DAG));
1554 }
1555
1556 return DAG.getNode(ISD::BRIND, DL, MVT::Other, Chain, Addr);
1557}
1558
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001559SDValue MipsTargetLowering::lowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Wesley Peck527da1b2010-11-23 03:31:01 +00001560 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001561 // the block to branch to if the condition is true.
1562 SDValue Chain = Op.getOperand(0);
1563 SDValue Dest = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001564 SDLoc DL(Op);
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001565
Eric Christopher1c29a652014-07-18 22:55:25 +00001566 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001567 SDValue CondRes = createFPCmp(DAG, Op.getOperand(1));
Akira Hatanakaa5352702011-03-31 18:26:17 +00001568
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001569 // Return if flag is not set by a floating point comparison.
Akira Hatanakaa5352702011-03-31 18:26:17 +00001570 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopesa9504222008-07-30 17:06:13 +00001571 return Op;
Wesley Peck527da1b2010-11-23 03:31:01 +00001572
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +00001573 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmaneffb8942008-09-12 16:56:44 +00001574 Mips::CondCode CC =
1575 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Akira Hatanakaf0ea5002013-03-30 01:16:38 +00001576 unsigned Opc = invertFPCondCodeUser(CC) ? Mips::BRANCH_F : Mips::BRANCH_T;
1577 SDValue BrCode = DAG.getConstant(Opc, MVT::i32);
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +00001578 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001579 return DAG.getNode(MipsISD::FPBrcond, DL, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +00001580 FCC0, Dest, CondRes);
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001581}
1582
1583SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001584lowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001585{
Eric Christopher1c29a652014-07-18 22:55:25 +00001586 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001587 SDValue Cond = createFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001588
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001589 // Return if flag is not set by a floating point comparison.
Akira Hatanakaa5352702011-03-31 18:26:17 +00001590 if (Cond.getOpcode() != MipsISD::FPCmp)
1591 return Op;
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +00001592
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001593 return createCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
Andrew Trickef9de2a2013-05-25 02:42:55 +00001594 SDLoc(Op));
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001595}
1596
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001597SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001598lowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001599{
Andrew Trickef9de2a2013-05-25 02:42:55 +00001600 SDLoc DL(Op);
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001601 EVT Ty = Op.getOperand(0).getValueType();
Matt Arsenault758659232013-05-18 00:21:46 +00001602 SDValue Cond = DAG.getNode(ISD::SETCC, DL,
1603 getSetCCResultType(*DAG.getContext(), Ty),
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001604 Op.getOperand(0), Op.getOperand(1),
1605 Op.getOperand(4));
1606
1607 return DAG.getNode(ISD::SELECT, DL, Op.getValueType(), Cond, Op.getOperand(2),
1608 Op.getOperand(3));
1609}
1610
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001611SDValue MipsTargetLowering::lowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00001612 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001613 SDValue Cond = createFPCmp(DAG, Op);
Akira Hatanakab7f78592012-03-09 23:46:03 +00001614
1615 assert(Cond.getOpcode() == MipsISD::FPCmp &&
1616 "Floating point operand expected.");
1617
1618 SDValue True = DAG.getConstant(1, MVT::i32);
1619 SDValue False = DAG.getConstant(0, MVT::i32);
1620
Andrew Trickef9de2a2013-05-25 02:42:55 +00001621 return createCMovFP(DAG, Cond, True, False, SDLoc(Op));
Akira Hatanakab7f78592012-03-09 23:46:03 +00001622}
1623
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001624SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001625 SelectionDAG &DAG) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001626 EVT Ty = Op.getValueType();
1627 GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
1628 const GlobalValue *GV = N->getGlobal();
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001629
Eric Christopher96e72c62015-01-29 23:27:36 +00001630 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !ABI.IsN64()) {
Eric Christopher36fe0282015-02-03 07:22:52 +00001631 const MipsTargetObjectFile *TLOF =
1632 static_cast<const MipsTargetObjectFile *>(
1633 getTargetMachine().getObjFileLowering());
1634 if (TLOF->IsGlobalInSmallSection(GV, getTargetMachine()))
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001635 // %gp_rel relocation
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001636 return getAddrGPRel(N, SDLoc(N), Ty, DAG);
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001637
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001638 // %hi/%lo relocation
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001639 return getAddrNonPIC(N, SDLoc(N), Ty, DAG);
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001640 }
1641
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001642 if (GV->hasInternalLinkage() || (GV->hasLocalLinkage() && !isa<Function>(GV)))
Eric Christopher96e72c62015-01-29 23:27:36 +00001643 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001644
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00001645 if (LargeGOT)
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001646 return getAddrGlobalLargeGOT(N, SDLoc(N), Ty, DAG, MipsII::MO_GOT_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00001647 MipsII::MO_GOT_LO16, DAG.getEntryNode(),
1648 MachinePointerInfo::getGOT());
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00001649
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001650 return getAddrGlobal(N, SDLoc(N), Ty, DAG,
Eric Christopher96e72c62015-01-29 23:27:36 +00001651 (ABI.IsN32() || ABI.IsN64()) ? MipsII::MO_GOT_DISP
1652 : MipsII::MO_GOT16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00001653 DAG.getEntryNode(), MachinePointerInfo::getGOT());
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001654}
1655
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001656SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op,
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +00001657 SelectionDAG &DAG) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001658 BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op);
1659 EVT Ty = Op.getValueType();
Akira Hatanaka30f97cf2013-09-25 00:30:25 +00001660
Eric Christopher96e72c62015-01-29 23:27:36 +00001661 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !ABI.IsN64())
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001662 return getAddrNonPIC(N, SDLoc(N), Ty, DAG);
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001663
Eric Christopher96e72c62015-01-29 23:27:36 +00001664 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +00001665}
1666
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001667SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001668lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001669{
Akira Hatanakabff84e12011-12-14 18:26:41 +00001670 // If the relocation model is PIC, use the General Dynamic TLS Model or
1671 // Local Dynamic TLS model, otherwise use the Initial Exec or
1672 // Local Exec TLS Model.
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001673
1674 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001675 SDLoc DL(GA);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001676 const GlobalValue *GV = GA->getGlobal();
1677 EVT PtrVT = getPointerTy();
1678
Hans Wennborgaea41202012-05-04 09:40:39 +00001679 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
1680
1681 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Hans Wennborg245917b2012-06-04 14:02:08 +00001682 // General Dynamic and Local Dynamic TLS Model.
1683 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
1684 : MipsII::MO_TLSGD;
1685
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001686 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag);
1687 SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT,
1688 getGlobalReg(DAG, PtrVT), TGA);
Akira Hatanakaf10ee842011-12-08 21:05:38 +00001689 unsigned PtrSize = PtrVT.getSizeInBits();
1690 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1691
Benjamin Kramer64ba50a2011-12-11 12:21:34 +00001692 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001693
1694 ArgListTy Args;
1695 ArgListEntry Entry;
1696 Entry.Node = Argument;
Akira Hatanakadee6c822011-12-08 20:34:32 +00001697 Entry.Ty = PtrTy;
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001698 Args.push_back(Entry);
Jia Liuf54f60f2012-02-28 07:46:26 +00001699
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00001700 TargetLowering::CallLoweringInfo CLI(DAG);
1701 CLI.setDebugLoc(DL).setChain(DAG.getEntryNode())
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00001702 .setCallee(CallingConv::C, PtrTy, TlsGetAddr, std::move(Args), 0);
Justin Holewinskiaa583972012-05-25 16:35:28 +00001703 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001704
Akira Hatanakabff84e12011-12-14 18:26:41 +00001705 SDValue Ret = CallResult.first;
1706
Hans Wennborgaea41202012-05-04 09:40:39 +00001707 if (model != TLSModel::LocalDynamic)
Akira Hatanakabff84e12011-12-14 18:26:41 +00001708 return Ret;
1709
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001710 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanakabff84e12011-12-14 18:26:41 +00001711 MipsII::MO_DTPREL_HI);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001712 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1713 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanakabff84e12011-12-14 18:26:41 +00001714 MipsII::MO_DTPREL_LO);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001715 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1716 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret);
1717 return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001718 }
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001719
1720 SDValue Offset;
Hans Wennborgaea41202012-05-04 09:40:39 +00001721 if (model == TLSModel::InitialExec) {
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001722 // Initial Exec TLS Model
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001723 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001724 MipsII::MO_GOTTPREL);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001725 TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT),
Akira Hatanakab049aef2012-02-24 22:34:47 +00001726 TGA);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001727 Offset = DAG.getLoad(PtrVT, DL,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001728 DAG.getEntryNode(), TGA, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001729 false, false, false, 0);
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001730 } else {
1731 // Local Exec TLS Model
Hans Wennborgaea41202012-05-04 09:40:39 +00001732 assert(model == TLSModel::LocalExec);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001733 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001734 MipsII::MO_TPREL_HI);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001735 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001736 MipsII::MO_TPREL_LO);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001737 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1738 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1739 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001740 }
1741
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001742 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
1743 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001744}
1745
1746SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001747lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +00001748{
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001749 JumpTableSDNode *N = cast<JumpTableSDNode>(Op);
1750 EVT Ty = Op.getValueType();
Akira Hatanaka30f97cf2013-09-25 00:30:25 +00001751
Eric Christopher96e72c62015-01-29 23:27:36 +00001752 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !ABI.IsN64())
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001753 return getAddrNonPIC(N, SDLoc(N), Ty, DAG);
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001754
Eric Christopher96e72c62015-01-29 23:27:36 +00001755 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +00001756}
1757
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001758SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001759lowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +00001760{
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001761 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
1762 EVT Ty = Op.getValueType();
Bruno Cardoso Lopes2db07582009-11-25 12:17:58 +00001763
Eric Christopher96e72c62015-01-29 23:27:36 +00001764 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !ABI.IsN64()) {
Eric Christopher36fe0282015-02-03 07:22:52 +00001765 const MipsTargetObjectFile *TLOF =
1766 static_cast<const MipsTargetObjectFile *>(
1767 getTargetMachine().getObjFileLowering());
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001768
Eric Christopher36fe0282015-02-03 07:22:52 +00001769 if (TLOF->IsConstantInSmallSection(N->getConstVal(), getTargetMachine()))
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001770 // %gp_rel relocation
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001771 return getAddrGPRel(N, SDLoc(N), Ty, DAG);
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001772
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00001773 return getAddrNonPIC(N, SDLoc(N), Ty, DAG);
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001774 }
Bruno Cardoso Lopesfdb4cec2008-07-23 16:01:50 +00001775
Eric Christopher96e72c62015-01-29 23:27:36 +00001776 return getAddrLocal(N, SDLoc(N), Ty, DAG, ABI.IsN32() || ABI.IsN64());
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +00001777}
1778
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001779SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman31ae5862010-04-17 14:41:14 +00001780 MachineFunction &MF = DAG.getMachineFunction();
1781 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1782
Andrew Trickef9de2a2013-05-25 02:42:55 +00001783 SDLoc DL(Op);
Dan Gohman31ae5862010-04-17 14:41:14 +00001784 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1785 getPointerTy());
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +00001786
1787 // vastart just stores the address of the VarArgsFrameIndex slot into the
1788 // memory location argument.
1789 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001790 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00001791 MachinePointerInfo(SV), false, false, 0);
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +00001792}
Jia Liuf54f60f2012-02-28 07:46:26 +00001793
Daniel Sanders2b553d42014-08-01 09:17:39 +00001794SDValue MipsTargetLowering::lowerVAARG(SDValue Op, SelectionDAG &DAG) const {
1795 SDNode *Node = Op.getNode();
1796 EVT VT = Node->getValueType(0);
1797 SDValue Chain = Node->getOperand(0);
1798 SDValue VAListPtr = Node->getOperand(1);
1799 unsigned Align = Node->getConstantOperandVal(3);
1800 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
1801 SDLoc DL(Node);
Eric Christopher96e72c62015-01-29 23:27:36 +00001802 unsigned ArgSlotSizeInBytes = (ABI.IsN32() || ABI.IsN64()) ? 8 : 4;
Daniel Sanders2b553d42014-08-01 09:17:39 +00001803
1804 SDValue VAListLoad = DAG.getLoad(getPointerTy(), DL, Chain, VAListPtr,
1805 MachinePointerInfo(SV), false, false, false,
1806 0);
1807 SDValue VAList = VAListLoad;
1808
1809 // Re-align the pointer if necessary.
1810 // It should only ever be necessary for 64-bit types on O32 since the minimum
1811 // argument alignment is the same as the maximum type alignment for N32/N64.
1812 //
1813 // FIXME: We currently align too often. The code generator doesn't notice
1814 // when the pointer is still aligned from the last va_arg (or pair of
1815 // va_args for the i64 on O32 case).
1816 if (Align > getMinStackArgumentAlignment()) {
1817 assert(((Align & (Align-1)) == 0) && "Expected Align to be a power of 2");
1818
1819 VAList = DAG.getNode(ISD::ADD, DL, VAList.getValueType(), VAList,
1820 DAG.getConstant(Align - 1,
1821 VAList.getValueType()));
1822
1823 VAList = DAG.getNode(ISD::AND, DL, VAList.getValueType(), VAList,
1824 DAG.getConstant(-(int64_t)Align,
1825 VAList.getValueType()));
1826 }
1827
1828 // Increment the pointer, VAList, to the next vaarg.
1829 unsigned ArgSizeInBytes = getDataLayout()->getTypeAllocSize(VT.getTypeForEVT(*DAG.getContext()));
1830 SDValue Tmp3 = DAG.getNode(ISD::ADD, DL, VAList.getValueType(), VAList,
1831 DAG.getConstant(RoundUpToAlignment(ArgSizeInBytes, ArgSlotSizeInBytes),
1832 VAList.getValueType()));
1833 // Store the incremented VAList to the legalized pointer
1834 Chain = DAG.getStore(VAListLoad.getValue(1), DL, Tmp3, VAListPtr,
1835 MachinePointerInfo(SV), false, false, 0);
1836
1837 // In big-endian mode we must adjust the pointer when the load size is smaller
1838 // than the argument slot size. We must also reduce the known alignment to
1839 // match. For example in the N64 ABI, we must add 4 bytes to the offset to get
1840 // the correct half of the slot, and reduce the alignment from 8 (slot
1841 // alignment) down to 4 (type alignment).
1842 if (!Subtarget.isLittle() && ArgSizeInBytes < ArgSlotSizeInBytes) {
1843 unsigned Adjustment = ArgSlotSizeInBytes - ArgSizeInBytes;
1844 VAList = DAG.getNode(ISD::ADD, DL, VAListPtr.getValueType(), VAList,
1845 DAG.getIntPtrConstant(Adjustment));
1846 }
1847 // Load the actual argument out of the pointer VAList
1848 return DAG.getLoad(VT, DL, Chain, VAList, MachinePointerInfo(), false, false,
1849 false, 0);
1850}
1851
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001852static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG,
1853 bool HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001854 EVT TyX = Op.getOperand(0).getValueType();
1855 EVT TyY = Op.getOperand(1).getValueType();
1856 SDValue Const1 = DAG.getConstant(1, MVT::i32);
1857 SDValue Const31 = DAG.getConstant(31, MVT::i32);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001858 SDLoc DL(Op);
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001859 SDValue Res;
1860
1861 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1862 // to i32.
1863 SDValue X = (TyX == MVT::f32) ?
1864 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1865 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1866 Const1);
1867 SDValue Y = (TyY == MVT::f32) ?
1868 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
1869 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
1870 Const1);
1871
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001872 if (HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001873 // ext E, Y, 31, 1 ; extract bit31 of Y
1874 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
1875 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
1876 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
1877 } else {
1878 // sll SllX, X, 1
1879 // srl SrlX, SllX, 1
1880 // srl SrlY, Y, 31
1881 // sll SllY, SrlX, 31
1882 // or Or, SrlX, SllY
1883 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1884 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1885 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
1886 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
1887 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
1888 }
1889
1890 if (TyX == MVT::f32)
1891 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
1892
1893 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1894 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1895 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001896}
1897
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001898static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG,
1899 bool HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001900 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
1901 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
1902 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
1903 SDValue Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001904 SDLoc DL(Op);
Eric Christopher0713a9d2011-06-08 23:55:35 +00001905
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001906 // Bitcast to integer nodes.
1907 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
1908 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001909
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001910 if (HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001911 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
1912 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
1913 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
1914 DAG.getConstant(WidthY - 1, MVT::i32), Const1);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001915
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001916 if (WidthX > WidthY)
1917 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
1918 else if (WidthY > WidthX)
1919 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001920
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001921 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
1922 DAG.getConstant(WidthX - 1, MVT::i32), Const1, X);
1923 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
1924 }
1925
1926 // (d)sll SllX, X, 1
1927 // (d)srl SrlX, SllX, 1
1928 // (d)srl SrlY, Y, width(Y)-1
1929 // (d)sll SllY, SrlX, width(Y)-1
1930 // or Or, SrlX, SllY
1931 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
1932 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
1933 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
1934 DAG.getConstant(WidthY - 1, MVT::i32));
1935
1936 if (WidthX > WidthY)
1937 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
1938 else if (WidthY > WidthX)
1939 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
1940
1941 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
1942 DAG.getConstant(WidthX - 1, MVT::i32));
1943 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
1944 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001945}
1946
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00001947SDValue
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001948MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00001949 if (Subtarget.isGP64bit())
1950 return lowerFCOPYSIGN64(Op, DAG, Subtarget.hasExtractInsert());
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001951
Eric Christopher1c29a652014-07-18 22:55:25 +00001952 return lowerFCOPYSIGN32(Op, DAG, Subtarget.hasExtractInsert());
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001953}
1954
Akira Hatanaka66277522011-06-02 00:24:44 +00001955SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001956lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes5444a7b2011-06-16 00:40:02 +00001957 // check the depth
1958 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka15506782011-06-07 18:58:42 +00001959 "Frame address can only be determined for current frame.");
Akira Hatanaka66277522011-06-02 00:24:44 +00001960
1961 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1962 MFI->setFrameAddressIsTaken(true);
1963 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001964 SDLoc DL(Op);
Eric Christopher96e72c62015-01-29 23:27:36 +00001965 SDValue FrameAddr = DAG.getCopyFromReg(
1966 DAG.getEntryNode(), DL, ABI.IsN64() ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka66277522011-06-02 00:24:44 +00001967 return FrameAddr;
1968}
1969
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001970SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op,
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00001971 SelectionDAG &DAG) const {
Bill Wendling908bf812014-01-06 00:43:20 +00001972 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001973 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001974
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00001975 // check the depth
1976 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
1977 "Return address can be determined only for current frame.");
1978
1979 MachineFunction &MF = DAG.getMachineFunction();
1980 MachineFrameInfo *MFI = MF.getFrameInfo();
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001981 MVT VT = Op.getSimpleValueType();
Eric Christopher96e72c62015-01-29 23:27:36 +00001982 unsigned RA = ABI.IsN64() ? Mips::RA_64 : Mips::RA;
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00001983 MFI->setReturnAddressIsTaken(true);
1984
1985 // Return RA, which contains the return address. Mark it an implicit live-in.
1986 unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00001987 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT);
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00001988}
1989
Akira Hatanakac0b02062013-01-30 00:26:49 +00001990// An EH_RETURN is the result of lowering llvm.eh.return which in turn is
1991// generated from __builtin_eh_return (offset, handler)
1992// The effect of this is to adjust the stack pointer by "offset"
1993// and then branch to "handler".
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001994SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Akira Hatanakac0b02062013-01-30 00:26:49 +00001995 const {
1996 MachineFunction &MF = DAG.getMachineFunction();
1997 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1998
1999 MipsFI->setCallsEhReturn();
2000 SDValue Chain = Op.getOperand(0);
2001 SDValue Offset = Op.getOperand(1);
2002 SDValue Handler = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002003 SDLoc DL(Op);
Eric Christopher96e72c62015-01-29 23:27:36 +00002004 EVT Ty = ABI.IsN64() ? MVT::i64 : MVT::i32;
Akira Hatanakac0b02062013-01-30 00:26:49 +00002005
2006 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
2007 // EH_RETURN nodes, so that instructions are emitted back-to-back.
Eric Christopher96e72c62015-01-29 23:27:36 +00002008 unsigned OffsetReg = ABI.IsN64() ? Mips::V1_64 : Mips::V1;
2009 unsigned AddrReg = ABI.IsN64() ? Mips::V0_64 : Mips::V0;
Akira Hatanakac0b02062013-01-30 00:26:49 +00002010 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
2011 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
2012 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
2013 DAG.getRegister(OffsetReg, Ty),
2014 DAG.getRegister(AddrReg, getPointerTy()),
2015 Chain.getValue(1));
2016}
2017
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002018SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
Akira Hatanaka5fd22482012-06-14 21:10:56 +00002019 SelectionDAG &DAG) const {
Eli Friedman26a48482011-07-27 22:21:52 +00002020 // FIXME: Need pseudo-fence for 'singlethread' fences
2021 // FIXME: Set SType for weaker fences where supported/appropriate.
2022 unsigned SType = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002023 SDLoc DL(Op);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002024 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
Eli Friedman26a48482011-07-27 22:21:52 +00002025 DAG.getConstant(SType, MVT::i32));
2026}
2027
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002028SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
Akira Hatanaka5fd22482012-06-14 21:10:56 +00002029 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002030 SDLoc DL(Op);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002031 MVT VT = Subtarget.isGP64bit() ? MVT::i64 : MVT::i32;
2032
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002033 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2034 SDValue Shamt = Op.getOperand(2);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002035 // if shamt < (VT.bits):
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002036 // lo = (shl lo, shamt)
2037 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
2038 // else:
2039 // lo = 0
2040 // hi = (shl lo, shamt[4:0])
2041 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
2042 DAG.getConstant(-1, MVT::i32));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002043 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, VT, Lo,
2044 DAG.getConstant(1, VT));
2045 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, VT, ShiftRight1Lo, Not);
2046 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, VT, Hi, Shamt);
2047 SDValue Or = DAG.getNode(ISD::OR, DL, VT, ShiftLeftHi, ShiftRightLo);
2048 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, VT, Lo, Shamt);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002049 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
2050 DAG.getConstant(0x20, MVT::i32));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002051 Lo = DAG.getNode(ISD::SELECT, DL, VT, Cond,
2052 DAG.getConstant(0, VT), ShiftLeftLo);
2053 Hi = DAG.getNode(ISD::SELECT, DL, VT, Cond, ShiftLeftLo, Or);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002054
2055 SDValue Ops[2] = {Lo, Hi};
Craig Topper64941d92014-04-27 19:20:57 +00002056 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002057}
2058
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002059SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002060 bool IsSRA) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002061 SDLoc DL(Op);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002062 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2063 SDValue Shamt = Op.getOperand(2);
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002064 MVT VT = Subtarget.isGP64bit() ? MVT::i64 : MVT::i32;
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002065
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002066 // if shamt < (VT.bits):
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002067 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
2068 // if isSRA:
2069 // hi = (sra hi, shamt)
2070 // else:
2071 // hi = (srl hi, shamt)
2072 // else:
2073 // if isSRA:
2074 // lo = (sra hi, shamt[4:0])
2075 // hi = (sra hi, 31)
2076 // else:
2077 // lo = (srl hi, shamt[4:0])
2078 // hi = 0
2079 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
2080 DAG.getConstant(-1, MVT::i32));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002081 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, VT, Hi,
2082 DAG.getConstant(1, VT));
2083 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, VT, ShiftLeft1Hi, Not);
2084 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, VT, Lo, Shamt);
2085 SDValue Or = DAG.getNode(ISD::OR, DL, VT, ShiftLeftHi, ShiftRightLo);
2086 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL,
2087 DL, VT, Hi, Shamt);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002088 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
2089 DAG.getConstant(0x20, MVT::i32));
Vasileios Kalintirisef96a8e2015-01-26 12:33:22 +00002090 SDValue Shift31 = DAG.getNode(ISD::SRA, DL, VT, Hi, DAG.getConstant(31, VT));
2091 Lo = DAG.getNode(ISD::SELECT, DL, VT, Cond, ShiftRightHi, Or);
2092 Hi = DAG.getNode(ISD::SELECT, DL, VT, Cond,
2093 IsSRA ? Shift31 : DAG.getConstant(0, VT), ShiftRightHi);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002094
2095 SDValue Ops[2] = {Lo, Hi};
Craig Topper64941d92014-04-27 19:20:57 +00002096 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002097}
2098
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002099static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002100 SDValue Chain, SDValue Src, unsigned Offset) {
Akira Hatanaka95866182012-06-13 19:06:08 +00002101 SDValue Ptr = LD->getBasePtr();
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002102 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
Akira Hatanaka95866182012-06-13 19:06:08 +00002103 EVT BasePtrVT = Ptr.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002104 SDLoc DL(LD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002105 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
2106
2107 if (Offset)
Akira Hatanaka95866182012-06-13 19:06:08 +00002108 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002109 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002110
2111 SDValue Ops[] = { Chain, Ptr, Src };
Craig Topper206fcd42014-04-26 19:29:41 +00002112 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002113 LD->getMemOperand());
2114}
2115
2116// Expand an unaligned 32 or 64-bit integer load node.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002117SDValue MipsTargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002118 LoadSDNode *LD = cast<LoadSDNode>(Op);
2119 EVT MemVT = LD->getMemoryVT();
2120
Eric Christopher1c29a652014-07-18 22:55:25 +00002121 if (Subtarget.systemSupportsUnalignedAccess())
Daniel Sandersac272632014-05-23 13:18:02 +00002122 return Op;
2123
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002124 // Return if load is aligned or if MemVT is neither i32 nor i64.
2125 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
2126 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
2127 return SDValue();
2128
Eric Christopher1c29a652014-07-18 22:55:25 +00002129 bool IsLittle = Subtarget.isLittle();
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002130 EVT VT = Op.getValueType();
2131 ISD::LoadExtType ExtType = LD->getExtensionType();
2132 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
2133
2134 assert((VT == MVT::i32) || (VT == MVT::i64));
2135
2136 // Expand
2137 // (set dst, (i64 (load baseptr)))
2138 // to
2139 // (set tmp, (ldl (add baseptr, 7), undef))
2140 // (set dst, (ldr baseptr, tmp))
2141 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002142 SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002143 IsLittle ? 7 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002144 return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002145 IsLittle ? 0 : 7);
2146 }
2147
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002148 SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002149 IsLittle ? 3 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002150 SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002151 IsLittle ? 0 : 3);
2152
2153 // Expand
2154 // (set dst, (i32 (load baseptr))) or
2155 // (set dst, (i64 (sextload baseptr))) or
2156 // (set dst, (i64 (extload baseptr)))
2157 // to
2158 // (set tmp, (lwl (add baseptr, 3), undef))
2159 // (set dst, (lwr baseptr, tmp))
2160 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
2161 (ExtType == ISD::EXTLOAD))
2162 return LWR;
2163
2164 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
2165
2166 // Expand
2167 // (set dst, (i64 (zextload baseptr)))
2168 // to
2169 // (set tmp0, (lwl (add baseptr, 3), undef))
2170 // (set tmp1, (lwr baseptr, tmp0))
2171 // (set tmp2, (shl tmp1, 32))
2172 // (set dst, (srl tmp2, 32))
Andrew Trickef9de2a2013-05-25 02:42:55 +00002173 SDLoc DL(LD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002174 SDValue Const32 = DAG.getConstant(32, MVT::i32);
2175 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
Akira Hatanaka67346852012-06-04 17:46:29 +00002176 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
2177 SDValue Ops[] = { SRL, LWR.getValue(1) };
Craig Topper64941d92014-04-27 19:20:57 +00002178 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002179}
2180
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002181static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002182 SDValue Chain, unsigned Offset) {
Akira Hatanaka95866182012-06-13 19:06:08 +00002183 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
2184 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002185 SDLoc DL(SD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002186 SDVTList VTList = DAG.getVTList(MVT::Other);
2187
2188 if (Offset)
Akira Hatanaka95866182012-06-13 19:06:08 +00002189 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002190 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002191
2192 SDValue Ops[] = { Chain, Value, Ptr };
Craig Topper206fcd42014-04-26 19:29:41 +00002193 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002194 SD->getMemOperand());
2195}
2196
2197// Expand an unaligned 32 or 64-bit integer store node.
Akira Hatanakad82ee942013-05-16 20:45:17 +00002198static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG,
2199 bool IsLittle) {
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002200 SDValue Value = SD->getValue(), Chain = SD->getChain();
2201 EVT VT = Value.getValueType();
2202
2203 // Expand
2204 // (store val, baseptr) or
2205 // (truncstore val, baseptr)
2206 // to
2207 // (swl val, (add baseptr, 3))
2208 // (swr val, baseptr)
2209 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002210 SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002211 IsLittle ? 3 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002212 return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002213 }
2214
2215 assert(VT == MVT::i64);
2216
2217 // Expand
2218 // (store val, baseptr)
2219 // to
2220 // (sdl val, (add baseptr, 7))
2221 // (sdr val, baseptr)
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002222 SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2223 return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002224}
2225
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002226// Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr).
2227static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG) {
2228 SDValue Val = SD->getValue();
2229
2230 if (Val.getOpcode() != ISD::FP_TO_SINT)
2231 return SDValue();
2232
2233 EVT FPTy = EVT::getFloatingPointVT(Val.getValueSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002234 SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy,
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002235 Val.getOperand(0));
2236
Andrew Trickef9de2a2013-05-25 02:42:55 +00002237 return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(),
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002238 SD->getPointerInfo(), SD->isVolatile(),
2239 SD->isNonTemporal(), SD->getAlignment());
2240}
2241
Akira Hatanakad82ee942013-05-16 20:45:17 +00002242SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2243 StoreSDNode *SD = cast<StoreSDNode>(Op);
2244 EVT MemVT = SD->getMemoryVT();
2245
2246 // Lower unaligned integer stores.
Eric Christopher1c29a652014-07-18 22:55:25 +00002247 if (!Subtarget.systemSupportsUnalignedAccess() &&
Daniel Sandersac272632014-05-23 13:18:02 +00002248 (SD->getAlignment() < MemVT.getSizeInBits() / 8) &&
Akira Hatanakad82ee942013-05-16 20:45:17 +00002249 ((MemVT == MVT::i32) || (MemVT == MVT::i64)))
Eric Christopher1c29a652014-07-18 22:55:25 +00002250 return lowerUnalignedIntStore(SD, DAG, Subtarget.isLittle());
Akira Hatanakad82ee942013-05-16 20:45:17 +00002251
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002252 return lowerFP_TO_SINT_STORE(SD, DAG);
Akira Hatanakad82ee942013-05-16 20:45:17 +00002253}
2254
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002255SDValue MipsTargetLowering::lowerADD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002256 if (Op->getOperand(0).getOpcode() != ISD::FRAMEADDR
2257 || cast<ConstantSDNode>
2258 (Op->getOperand(0).getOperand(0))->getZExtValue() != 0
2259 || Op->getOperand(1).getOpcode() != ISD::FRAME_TO_ARGS_OFFSET)
2260 return SDValue();
2261
2262 // The pattern
2263 // (add (frameaddr 0), (frame_to_args_offset))
2264 // results from lowering llvm.eh.dwarf.cfa intrinsic. Transform it to
2265 // (add FrameObject, 0)
2266 // where FrameObject is a fixed StackObject with offset 0 which points to
2267 // the old stack pointer.
2268 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2269 EVT ValTy = Op->getValueType(0);
2270 int FI = MFI->CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
2271 SDValue InArgsAddr = DAG.getFrameIndex(FI, ValTy);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002272 return DAG.getNode(ISD::ADD, SDLoc(Op), ValTy, InArgsAddr,
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002273 DAG.getConstant(0, ValTy));
2274}
2275
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002276SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op,
2277 SelectionDAG &DAG) const {
2278 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002279 SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy,
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002280 Op.getOperand(0));
Andrew Trickef9de2a2013-05-25 02:42:55 +00002281 return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc);
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002282}
2283
Akira Hatanakae2489122011-04-15 21:51:11 +00002284//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002285// Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002286//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002287
Akira Hatanakae2489122011-04-15 21:51:11 +00002288//===----------------------------------------------------------------------===//
Wesley Peck527da1b2010-11-23 03:31:01 +00002289// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002290// Mips O32 ABI rules:
2291// ---
2292// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peck527da1b2010-11-23 03:31:01 +00002293// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002294// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peck527da1b2010-11-23 03:31:01 +00002295// f64 - Only passed in two aliased f32 registers if no int reg has been used
2296// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Sylvestre Ledru469de192014-08-11 18:04:46 +00002297// not used, it must be shadowed. If only A3 is available, shadow it and
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002298// go to stack.
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002299//
2300// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanakae2489122011-04-15 21:51:11 +00002301//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002302
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00002303static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2304 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002305 CCState &State, ArrayRef<MCPhysReg> F64Regs) {
Eric Christopher96e72c62015-01-29 23:27:36 +00002306 const MipsSubtarget &Subtarget = static_cast<const MipsSubtarget &>(
2307 State.getMachineFunction().getSubtarget());
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002308
Craig Topper840beec2014-04-04 05:16:06 +00002309 static const MCPhysReg IntRegs[] = { Mips::A0, Mips::A1, Mips::A2, Mips::A3 };
2310 static const MCPhysReg F32Regs[] = { Mips::F12, Mips::F14 };
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002311
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002312 // Do not process byval args here.
2313 if (ArgFlags.isByVal())
2314 return true;
Akira Hatanaka5e16c6a2011-05-24 19:18:33 +00002315
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002316 // Promote i8 and i16
Daniel Sandersd134c9d2014-12-02 20:40:27 +00002317 if (ArgFlags.isInReg() && !Subtarget.isLittle()) {
2318 if (LocVT == MVT::i8 || LocVT == MVT::i16 || LocVT == MVT::i32) {
2319 LocVT = MVT::i32;
2320 if (ArgFlags.isSExt())
2321 LocInfo = CCValAssign::SExtUpper;
2322 else if (ArgFlags.isZExt())
2323 LocInfo = CCValAssign::ZExtUpper;
2324 else
2325 LocInfo = CCValAssign::AExtUpper;
2326 }
2327 }
2328
2329 // Promote i8 and i16
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002330 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
2331 LocVT = MVT::i32;
2332 if (ArgFlags.isSExt())
2333 LocInfo = CCValAssign::SExt;
2334 else if (ArgFlags.isZExt())
2335 LocInfo = CCValAssign::ZExt;
2336 else
2337 LocInfo = CCValAssign::AExt;
2338 }
2339
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002340 unsigned Reg;
2341
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002342 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
2343 // is true: function is vararg, argument is 3rd or higher, there is previous
2344 // argument which is not f32 or f64.
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002345 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1 ||
2346 State.getFirstUnallocated(F32Regs) != ValNo;
Akira Hatanaka9e6a8cc2011-05-19 20:29:48 +00002347 unsigned OrigAlign = ArgFlags.getOrigAlign();
2348 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002349
2350 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002351 Reg = State.AllocateReg(IntRegs);
Akira Hatanaka9e6a8cc2011-05-19 20:29:48 +00002352 // If this is the first part of an i64 arg,
2353 // the allocated register must be either A0 or A2.
2354 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002355 Reg = State.AllocateReg(IntRegs);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002356 LocVT = MVT::i32;
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002357 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
2358 // Allocate int register and shadow next int register. If first
2359 // available register is Mips::A1 or Mips::A3, shadow it too.
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002360 Reg = State.AllocateReg(IntRegs);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002361 if (Reg == Mips::A1 || Reg == Mips::A3)
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002362 Reg = State.AllocateReg(IntRegs);
2363 State.AllocateReg(IntRegs);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002364 LocVT = MVT::i32;
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002365 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
2366 // we are guaranteed to find an available float register
2367 if (ValVT == MVT::f32) {
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002368 Reg = State.AllocateReg(F32Regs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002369 // Shadow int register
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002370 State.AllocateReg(IntRegs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002371 } else {
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002372 Reg = State.AllocateReg(F64Regs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002373 // Shadow int registers
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002374 unsigned Reg2 = State.AllocateReg(IntRegs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002375 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002376 State.AllocateReg(IntRegs);
2377 State.AllocateReg(IntRegs);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002378 }
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002379 } else
2380 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002381
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002382 if (!Reg) {
2383 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3,
2384 OrigAlign);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002385 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002386 } else
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002387 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002388
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002389 return false;
Akira Hatanaka202f6402011-11-12 02:20:46 +00002390}
2391
Akira Hatanakabfb66242013-08-20 23:38:40 +00002392static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT,
2393 MVT LocVT, CCValAssign::LocInfo LocInfo,
2394 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002395 static const MCPhysReg F64Regs[] = { Mips::D6, Mips::D7 };
Akira Hatanakabfb66242013-08-20 23:38:40 +00002396
2397 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2398}
2399
2400static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT,
2401 MVT LocVT, CCValAssign::LocInfo LocInfo,
2402 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002403 static const MCPhysReg F64Regs[] = { Mips::D12_64, Mips::D14_64 };
Akira Hatanakabfb66242013-08-20 23:38:40 +00002404
2405 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2406}
2407
Reid Klecknerd3781742014-11-14 00:39:33 +00002408static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2409 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
2410 CCState &State) LLVM_ATTRIBUTE_UNUSED;
Reed Kotlerd5c41962014-11-13 23:37:45 +00002411
Akira Hatanaka202f6402011-11-12 02:20:46 +00002412#include "MipsGenCallingConv.inc"
2413
Akira Hatanakae2489122011-04-15 21:51:11 +00002414//===----------------------------------------------------------------------===//
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002415// Call Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002416//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002417
Akira Hatanaka61bbcce2011-09-23 00:58:33 +00002418// Return next O32 integer argument register.
2419static unsigned getNextIntArgReg(unsigned Reg) {
2420 assert((Reg == Mips::A0) || (Reg == Mips::A2));
2421 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
2422}
2423
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002424SDValue
2425MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002426 SDValue Chain, SDValue Arg, SDLoc DL,
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002427 bool IsTailCall, SelectionDAG &DAG) const {
2428 if (!IsTailCall) {
2429 SDValue PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr,
2430 DAG.getIntPtrConstant(Offset));
2431 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo(), false,
2432 false, 0);
2433 }
2434
2435 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2436 int FI = MFI->CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
2437 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2438 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(),
2439 /*isVolatile=*/ true, false, 0);
2440}
2441
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002442void MipsTargetLowering::
2443getOpndList(SmallVectorImpl<SDValue> &Ops,
2444 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
2445 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
Sasa Stankovic7072a792014-10-01 08:22:21 +00002446 bool IsCallReloc, CallLoweringInfo &CLI, SDValue Callee,
2447 SDValue Chain) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002448 // Insert node "GP copy globalreg" before call to function.
2449 //
2450 // R_MIPS_CALL* operators (emitted when non-internal functions are called
2451 // in PIC mode) allow symbols to be resolved via lazy binding.
2452 // The lazy binding stub requires GP to point to the GOT.
Sasa Stankovic7072a792014-10-01 08:22:21 +00002453 // Note that we don't need GP to point to the GOT for indirect calls
2454 // (when R_MIPS_CALL* is not used for the call) because Mips linker generates
2455 // lazy binding stub for a function only when R_MIPS_CALL* are the only relocs
2456 // used for the function (that is, Mips linker doesn't generate lazy binding
2457 // stub for a function whose address is taken in the program).
2458 if (IsPICCall && !InternalLinkage && IsCallReloc) {
Eric Christopher96e72c62015-01-29 23:27:36 +00002459 unsigned GPReg = ABI.IsN64() ? Mips::GP_64 : Mips::GP;
2460 EVT Ty = ABI.IsN64() ? MVT::i64 : MVT::i32;
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002461 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty)));
2462 }
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002463
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002464 // Build a sequence of copy-to-reg nodes chained together with token
2465 // chain and flag operands which copy the outgoing args into registers.
2466 // The InFlag in necessary since all emitted instructions must be
2467 // stuck together.
2468 SDValue InFlag;
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002469
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002470 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2471 Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, RegsToPass[i].first,
2472 RegsToPass[i].second, InFlag);
2473 InFlag = Chain.getValue(1);
2474 }
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002475
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002476 // Add argument registers to the end of the list so that they are
2477 // known live into the call.
2478 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2479 Ops.push_back(CLI.DAG.getRegister(RegsToPass[i].first,
2480 RegsToPass[i].second.getValueType()));
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002481
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002482 // Add a register mask operand representing the call-preserved registers.
Eric Christopher96e72c62015-01-29 23:27:36 +00002483 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00002484 const uint32_t *Mask =
2485 TRI->getCallPreservedMask(CLI.DAG.getMachineFunction(), CLI.CallConv);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002486 assert(Mask && "Missing call preserved mask for calling convention");
Eric Christopher1c29a652014-07-18 22:55:25 +00002487 if (Subtarget.inMips16HardFloat()) {
Reed Kotler783c7942013-05-10 22:25:39 +00002488 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
2489 llvm::StringRef Sym = G->getGlobal()->getName();
2490 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
Reed Kotler3230e722013-12-12 02:41:11 +00002491 if (F && F->hasFnAttribute("__Mips16RetHelper")) {
Reed Kotler783c7942013-05-10 22:25:39 +00002492 Mask = MipsRegisterInfo::getMips16RetHelperMask();
2493 }
2494 }
2495 }
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002496 Ops.push_back(CLI.DAG.getRegisterMask(Mask));
2497
2498 if (InFlag.getNode())
2499 Ops.push_back(InFlag);
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002500}
2501
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002502/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman624801e2009-01-26 03:15:54 +00002503/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002504SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00002505MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002506 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00002507 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002508 SDLoc DL = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00002509 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
2510 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
2511 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Akira Hatanakabeda2242012-07-31 18:46:41 +00002512 SDValue Chain = CLI.Chain;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002513 SDValue Callee = CLI.Callee;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002514 bool &IsTailCall = CLI.IsTailCall;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002515 CallingConv::ID CallConv = CLI.CallConv;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002516 bool IsVarArg = CLI.IsVarArg;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002517
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002518 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002519 MachineFrameInfo *MFI = MF.getFrameInfo();
Eric Christopher96e72c62015-01-29 23:27:36 +00002520 const TargetFrameLowering *TFL = Subtarget.getFrameLowering();
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002521 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +00002522 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002523
2524 // Analyze operands of the call, assigning locations to each operand.
2525 SmallVector<CCValAssign, 16> ArgLocs;
Daniel Sanders41a64c42014-11-07 11:10:48 +00002526 MipsCCState CCInfo(
2527 CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs, *DAG.getContext(),
2528 MipsCCState::getSpecialCallingConvForCallee(Callee.getNode(), Subtarget));
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002529
2530 // Allocate the reserved argument area. It seems strange to do this from the
2531 // caller side but removing it breaks the frame size calculation.
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002532 CCInfo.AllocateStack(ABI.GetCalleeAllocdArgSizeInBytes(CallConv), 1);
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002533
Daniel Sanderscfad1e32014-11-07 11:43:49 +00002534 CCInfo.AnalyzeCallOperands(Outs, CC_Mips, CLI.getArgs(), Callee.getNode());
Wesley Peck527da1b2010-11-23 03:31:01 +00002535
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002536 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka195a1e22011-06-08 17:39:33 +00002537 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka97ba7692012-07-26 23:27:01 +00002538
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002539 // Check if it's really possible to do a tail call.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002540 if (IsTailCall)
Daniel Sanders23e98772014-11-02 16:09:29 +00002541 IsTailCall = isEligibleForTailCallOptimization(
2542 CCInfo, NextStackOffset, *MF.getInfo<MipsFunctionInfo>());
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002543
Reid Kleckner5772b772014-04-24 20:14:34 +00002544 if (!IsTailCall && CLI.CS && CLI.CS->isMustTailCall())
2545 report_fatal_error("failed to perform tail call elimination on a call "
2546 "site marked musttail");
2547
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002548 if (IsTailCall)
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002549 ++NumTailCalls;
2550
Akira Hatanaka79738332011-09-19 20:26:02 +00002551 // Chain is the output chain of the last Load/Store or CopyToReg node.
2552 // ByValChain is the output chain of the last Memcpy node created for copying
2553 // byval arguments to the stack.
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002554 unsigned StackAlignment = TFL->getStackAlignment();
2555 NextStackOffset = RoundUpToAlignment(NextStackOffset, StackAlignment);
Akira Hatanaka79738332011-09-19 20:26:02 +00002556 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002557
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002558 if (!IsTailCall)
Andrew Trickad6d08a2013-05-29 22:03:55 +00002559 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffsetVal, DL);
Akira Hatanakabeda2242012-07-31 18:46:41 +00002560
Daniel Sandersd897b562014-03-27 10:46:12 +00002561 SDValue StackPtr = DAG.getCopyFromReg(
Eric Christopher96e72c62015-01-29 23:27:36 +00002562 Chain, DL, ABI.IsN64() ? Mips::SP_64 : Mips::SP, getPointerTy());
Akira Hatanaka195a1e22011-06-08 17:39:33 +00002563
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002564 // With EABI is it possible to have 16 args on registers.
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002565 std::deque< std::pair<unsigned, SDValue> > RegsToPass;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002566 SmallVector<SDValue, 8> MemOpChains;
Daniel Sanders23e98772014-11-02 16:09:29 +00002567
2568 CCInfo.rewindByValRegsInfo();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002569
2570 // Walk the register/memloc assignments, inserting copies/loads.
2571 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002572 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002573 CCValAssign &VA = ArgLocs[i];
Akira Hatanakab20a3252011-10-28 19:49:00 +00002574 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka19891f82011-11-12 02:34:50 +00002575 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002576 bool UseUpperBits = false;
Akira Hatanaka19891f82011-11-12 02:34:50 +00002577
2578 // ByVal Arg.
2579 if (Flags.isByVal()) {
Daniel Sanders23e98772014-11-02 16:09:29 +00002580 unsigned FirstByValReg, LastByValReg;
2581 unsigned ByValIdx = CCInfo.getInRegsParamsProcessed();
2582 CCInfo.getInRegsParamInfo(ByValIdx, FirstByValReg, LastByValReg);
2583
Akira Hatanaka19891f82011-11-12 02:34:50 +00002584 assert(Flags.getByValSize() &&
2585 "ByVal args of size 0 should have been ignored by front-end.");
Daniel Sanders23e98772014-11-02 16:09:29 +00002586 assert(ByValIdx < CCInfo.getInRegsParamsCount());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002587 assert(!IsTailCall &&
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002588 "Do not tail-call optimize if there is a byval argument.");
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002589 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002590 FirstByValReg, LastByValReg, Flags, Subtarget.isLittle(),
2591 VA);
Daniel Sanders23e98772014-11-02 16:09:29 +00002592 CCInfo.nextInRegsParam();
Akira Hatanaka19891f82011-11-12 02:34:50 +00002593 continue;
2594 }
Jia Liuf54f60f2012-02-28 07:46:26 +00002595
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002596 // Promote the value if needed.
2597 switch (VA.getLocInfo()) {
Daniel Sandersc43cda82014-11-07 16:54:21 +00002598 default:
2599 llvm_unreachable("Unknown loc info!");
Wesley Peck527da1b2010-11-23 03:31:01 +00002600 case CCValAssign::Full:
Akira Hatanakab20a3252011-10-28 19:49:00 +00002601 if (VA.isRegLoc()) {
2602 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
Akira Hatanaka3b7391d2013-03-05 22:20:28 +00002603 (ValVT == MVT::f64 && LocVT == MVT::i64) ||
2604 (ValVT == MVT::i64 && LocVT == MVT::f64))
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002605 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
Akira Hatanakab20a3252011-10-28 19:49:00 +00002606 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002607 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanakae2489122011-04-15 21:51:11 +00002608 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002609 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanakaaef55c82011-04-15 21:00:26 +00002610 Arg, DAG.getConstant(1, MVT::i32));
Eric Christopher1c29a652014-07-18 22:55:25 +00002611 if (!Subtarget.isLittle())
Akira Hatanaka27916972011-04-15 19:52:08 +00002612 std::swap(Lo, Hi);
Jia Liuf54f60f2012-02-28 07:46:26 +00002613 unsigned LocRegLo = VA.getLocReg();
Akira Hatanaka61bbcce2011-09-23 00:58:33 +00002614 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2615 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2616 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002617 continue;
Wesley Peck527da1b2010-11-23 03:31:01 +00002618 }
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002619 }
2620 break;
Daniel Sanders23e98772014-11-02 16:09:29 +00002621 case CCValAssign::BCvt:
2622 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
2623 break;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002624 case CCValAssign::SExtUpper:
2625 UseUpperBits = true;
2626 // Fallthrough
Chris Lattner52f16de2008-03-17 06:57:02 +00002627 case CCValAssign::SExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002628 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002629 break;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002630 case CCValAssign::ZExtUpper:
2631 UseUpperBits = true;
2632 // Fallthrough
Chris Lattner52f16de2008-03-17 06:57:02 +00002633 case CCValAssign::ZExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002634 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002635 break;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002636 case CCValAssign::AExtUpper:
2637 UseUpperBits = true;
2638 // Fallthrough
Chris Lattner52f16de2008-03-17 06:57:02 +00002639 case CCValAssign::AExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002640 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002641 break;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002642 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002643
Daniel Sandersc43cda82014-11-07 16:54:21 +00002644 if (UseUpperBits) {
2645 unsigned ValSizeInBits = Outs[i].ArgVT.getSizeInBits();
2646 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
2647 Arg = DAG.getNode(
2648 ISD::SHL, DL, VA.getLocVT(), Arg,
2649 DAG.getConstant(LocSizeInBits - ValSizeInBits, VA.getLocVT()));
2650 }
2651
Wesley Peck527da1b2010-11-23 03:31:01 +00002652 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +00002653 // RegsToPass vector
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002654 if (VA.isRegLoc()) {
2655 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattner52f16de2008-03-17 06:57:02 +00002656 continue;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002657 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002658
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002659 // Register can't get to this point...
Chris Lattner52f16de2008-03-17 06:57:02 +00002660 assert(VA.isMemLoc());
Wesley Peck527da1b2010-11-23 03:31:01 +00002661
Wesley Peck527da1b2010-11-23 03:31:01 +00002662 // emit ISD::STORE whichs stores the
Chris Lattner52f16de2008-03-17 06:57:02 +00002663 // parameter value to a stack Location
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002664 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002665 Chain, Arg, DL, IsTailCall, DAG));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002666 }
2667
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002668 // Transform all store nodes into one single node because all store
2669 // nodes are independent of each other.
Wesley Peck527da1b2010-11-23 03:31:01 +00002670 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00002671 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002672
Bill Wendling24c79f22008-09-16 21:48:12 +00002673 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peck527da1b2010-11-23 03:31:01 +00002674 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2675 // node so that legalize doesn't hack it.
Eric Christopher96e72c62015-01-29 23:27:36 +00002676 bool IsPICCall = (ABI.IsN64() || IsPIC); // true if calls are translated to
2677 // jalr $25
Sasa Stankovic7072a792014-10-01 08:22:21 +00002678 bool GlobalOrExternal = false, InternalLinkage = false, IsCallReloc = false;
Akira Hatanakad6f1c582011-04-07 19:51:44 +00002679 SDValue CalleeLo;
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002680 EVT Ty = Callee.getValueType();
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002681
2682 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002683 if (IsPICCall) {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002684 const GlobalValue *Val = G->getGlobal();
2685 InternalLinkage = Val->hasInternalLinkage();
Akira Hatanakacf9a61b2012-12-13 03:17:29 +00002686
2687 if (InternalLinkage)
Eric Christopher96e72c62015-01-29 23:27:36 +00002688 Callee = getAddrLocal(G, DL, Ty, DAG, ABI.IsN32() || ABI.IsN64());
Sasa Stankovic7072a792014-10-01 08:22:21 +00002689 else if (LargeGOT) {
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002690 Callee = getAddrGlobalLargeGOT(G, DL, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002691 MipsII::MO_CALL_LO16, Chain,
2692 FuncInfo->callPtrInfo(Val));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002693 IsCallReloc = true;
2694 } else {
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002695 Callee = getAddrGlobal(G, DL, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002696 FuncInfo->callPtrInfo(Val));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002697 IsCallReloc = true;
2698 }
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002699 } else
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002700 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, getPointerTy(), 0,
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002701 MipsII::MO_NO_FLAG);
Akira Hatanaka8e16aac2011-12-09 01:45:12 +00002702 GlobalOrExternal = true;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002703 }
2704 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002705 const char *Sym = S->getSymbol();
2706
Eric Christopher96e72c62015-01-29 23:27:36 +00002707 if (!ABI.IsN64() && !IsPIC) // !N64 && static
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002708 Callee =
2709 DAG.getTargetExternalSymbol(Sym, getPointerTy(), MipsII::MO_NO_FLAG);
Sasa Stankovic7072a792014-10-01 08:22:21 +00002710 else if (LargeGOT) {
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002711 Callee = getAddrGlobalLargeGOT(S, DL, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002712 MipsII::MO_CALL_LO16, Chain,
2713 FuncInfo->callPtrInfo(Sym));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002714 IsCallReloc = true;
2715 } else { // N64 || PIC
Daniel Sanders9a4f2c52015-01-24 14:35:11 +00002716 Callee = getAddrGlobal(S, DL, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002717 FuncInfo->callPtrInfo(Sym));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002718 IsCallReloc = true;
2719 }
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002720
Akira Hatanaka8e16aac2011-12-09 01:45:12 +00002721 GlobalOrExternal = true;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002722 }
2723
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002724 SmallVector<SDValue, 8> Ops(1, Chain);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002725 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002726
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002727 getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal, InternalLinkage,
Sasa Stankovic7072a792014-10-01 08:22:21 +00002728 IsCallReloc, CLI, Callee, Chain);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002729
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002730 if (IsTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +00002731 return DAG.getNode(MipsISD::TailCall, DL, MVT::Other, Ops);
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002732
Craig Topper48d114b2014-04-26 18:35:24 +00002733 Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, Ops);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002734 SDValue InFlag = Chain.getValue(1);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002735
Bruno Cardoso Lopes193e64c2010-01-30 18:32:07 +00002736 // Create the CALLSEQ_END node.
Akira Hatanaka97ba7692012-07-26 23:27:01 +00002737 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal,
Andrew Trickad6d08a2013-05-29 22:03:55 +00002738 DAG.getIntPtrConstant(0, true), InFlag, DL);
Bruno Cardoso Lopes193e64c2010-01-30 18:32:07 +00002739 InFlag = Chain.getValue(1);
2740
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002741 // Handle result values, copying them out of physregs into vregs that we
2742 // return.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002743 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg, Ins, DL, DAG,
2744 InVals, CLI);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002745}
2746
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002747/// LowerCallResult - Lower the result values of a call into the
2748/// appropriate copies out of appropriate physical registers.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002749SDValue MipsTargetLowering::LowerCallResult(
2750 SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool IsVarArg,
2751 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG,
2752 SmallVectorImpl<SDValue> &InVals,
2753 TargetLowering::CallLoweringInfo &CLI) const {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002754 // Assign locations to each value returned by this call.
2755 SmallVector<CCValAssign, 16> RVLocs;
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002756 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
2757 *DAG.getContext());
2758 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips, CLI);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002759
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002760 // Copy all of the result registers out of their specified physreg.
2761 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Daniel Sandersae275e32014-09-25 12:15:05 +00002762 CCValAssign &VA = RVLocs[i];
2763 assert(VA.isRegLoc() && "Can only return in registers!");
2764
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002765 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(),
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002766 RVLocs[i].getLocVT(), InFlag);
2767 Chain = Val.getValue(1);
2768 InFlag = Val.getValue(2);
2769
Daniel Sandersae275e32014-09-25 12:15:05 +00002770 if (VA.isUpperBitsInLoc()) {
2771 unsigned ValSizeInBits = Ins[i].ArgVT.getSizeInBits();
2772 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
2773 unsigned Shift =
2774 VA.getLocInfo() == CCValAssign::ZExtUpper ? ISD::SRL : ISD::SRA;
2775 Val = DAG.getNode(
2776 Shift, DL, VA.getLocVT(), Val,
2777 DAG.getConstant(LocSizeInBits - ValSizeInBits, VA.getLocVT()));
2778 }
2779
2780 switch (VA.getLocInfo()) {
2781 default:
2782 llvm_unreachable("Unknown loc info!");
2783 case CCValAssign::Full:
2784 break;
2785 case CCValAssign::BCvt:
2786 Val = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Val);
2787 break;
2788 case CCValAssign::AExt:
2789 case CCValAssign::AExtUpper:
2790 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2791 break;
2792 case CCValAssign::ZExt:
2793 case CCValAssign::ZExtUpper:
2794 Val = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Val,
2795 DAG.getValueType(VA.getValVT()));
2796 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2797 break;
2798 case CCValAssign::SExt:
2799 case CCValAssign::SExtUpper:
2800 Val = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Val,
2801 DAG.getValueType(VA.getValVT()));
2802 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2803 break;
2804 }
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002805
2806 InVals.push_back(Val);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002807 }
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +00002808
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002809 return Chain;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002810}
2811
Daniel Sandersc43cda82014-11-07 16:54:21 +00002812static SDValue UnpackFromArgumentSlot(SDValue Val, const CCValAssign &VA,
2813 EVT ArgVT, SDLoc DL, SelectionDAG &DAG) {
2814 MVT LocVT = VA.getLocVT();
2815 EVT ValVT = VA.getValVT();
2816
2817 // Shift into the upper bits if necessary.
2818 switch (VA.getLocInfo()) {
2819 default:
2820 break;
2821 case CCValAssign::AExtUpper:
2822 case CCValAssign::SExtUpper:
2823 case CCValAssign::ZExtUpper: {
2824 unsigned ValSizeInBits = ArgVT.getSizeInBits();
2825 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
2826 unsigned Opcode =
2827 VA.getLocInfo() == CCValAssign::ZExtUpper ? ISD::SRL : ISD::SRA;
2828 Val = DAG.getNode(
2829 Opcode, DL, VA.getLocVT(), Val,
2830 DAG.getConstant(LocSizeInBits - ValSizeInBits, VA.getLocVT()));
2831 break;
2832 }
2833 }
2834
2835 // If this is an value smaller than the argument slot size (32-bit for O32,
2836 // 64-bit for N32/N64), it has been promoted in some way to the argument slot
2837 // size. Extract the value and insert any appropriate assertions regarding
2838 // sign/zero extension.
2839 switch (VA.getLocInfo()) {
2840 default:
2841 llvm_unreachable("Unknown loc info!");
2842 case CCValAssign::Full:
2843 break;
2844 case CCValAssign::AExtUpper:
2845 case CCValAssign::AExt:
2846 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
2847 break;
2848 case CCValAssign::SExtUpper:
2849 case CCValAssign::SExt:
2850 Val = DAG.getNode(ISD::AssertSext, DL, LocVT, Val, DAG.getValueType(ValVT));
2851 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
2852 break;
2853 case CCValAssign::ZExtUpper:
2854 case CCValAssign::ZExt:
2855 Val = DAG.getNode(ISD::AssertZext, DL, LocVT, Val, DAG.getValueType(ValVT));
2856 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
2857 break;
2858 case CCValAssign::BCvt:
2859 Val = DAG.getNode(ISD::BITCAST, DL, ValVT, Val);
2860 break;
2861 }
2862
2863 return Val;
2864}
2865
Akira Hatanakae2489122011-04-15 21:51:11 +00002866//===----------------------------------------------------------------------===//
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002867// Formal Arguments Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002868//===----------------------------------------------------------------------===//
Wesley Peck527da1b2010-11-23 03:31:01 +00002869/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002870/// and generate load operations for arguments places on the stack.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002871SDValue
2872MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanakaaef55c82011-04-15 21:00:26 +00002873 CallingConv::ID CallConv,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002874 bool IsVarArg,
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00002875 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002876 SDLoc DL, SelectionDAG &DAG,
Akira Hatanakaaef55c82011-04-15 21:00:26 +00002877 SmallVectorImpl<SDValue> &InVals)
Akira Hatanakae2489122011-04-15 21:51:11 +00002878 const {
Bruno Cardoso Lopesa01ede22008-08-04 07:12:52 +00002879 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002880 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopes14033fb2007-08-28 05:08:16 +00002881 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002882
Dan Gohman31ae5862010-04-17 14:41:14 +00002883 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002884
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002885 // Used with vargs to acumulate store chains.
2886 std::vector<SDValue> OutChains;
2887
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002888 // Assign locations to all of the incoming arguments.
2889 SmallVector<CCValAssign, 16> ArgLocs;
Daniel Sanders23e98772014-11-02 16:09:29 +00002890 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
2891 *DAG.getContext());
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002892 CCInfo.AllocateStack(ABI.GetCalleeAllocdArgSizeInBytes(CallConv), 1);
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002893 Function::const_arg_iterator FuncArg =
2894 DAG.getMachineFunction().getFunction()->arg_begin();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002895
Daniel Sandersb70e27c2014-11-06 16:36:30 +00002896 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips_FixedArg);
Akira Hatanaka4866fe12012-10-30 19:37:25 +00002897 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(),
Daniel Sanders23e98772014-11-02 16:09:29 +00002898 CCInfo.getInRegsParamsCount() > 0);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002899
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00002900 unsigned CurArgIdx = 0;
Daniel Sanders23e98772014-11-02 16:09:29 +00002901 CCInfo.rewindByValRegsInfo();
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002902
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00002903 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002904 CCValAssign &VA = ArgLocs[i];
Andrew Trick05938a52015-02-16 18:10:47 +00002905 if (Ins[i].isOrigArg()) {
2906 std::advance(FuncArg, Ins[i].getOrigArgIndex() - CurArgIdx);
2907 CurArgIdx = Ins[i].getOrigArgIndex();
2908 }
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002909 EVT ValVT = VA.getValVT();
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002910 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2911 bool IsRegLoc = VA.isRegLoc();
2912
2913 if (Flags.isByVal()) {
Andrew Trick05938a52015-02-16 18:10:47 +00002914 assert(Ins[i].isOrigArg() && "Byval arguments cannot be implicit");
Daniel Sanders23e98772014-11-02 16:09:29 +00002915 unsigned FirstByValReg, LastByValReg;
2916 unsigned ByValIdx = CCInfo.getInRegsParamsProcessed();
2917 CCInfo.getInRegsParamInfo(ByValIdx, FirstByValReg, LastByValReg);
2918
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002919 assert(Flags.getByValSize() &&
2920 "ByVal args of size 0 should have been ignored by front-end.");
Daniel Sanders23e98772014-11-02 16:09:29 +00002921 assert(ByValIdx < CCInfo.getInRegsParamsCount());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002922 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002923 FirstByValReg, LastByValReg, VA, CCInfo);
Daniel Sanders23e98772014-11-02 16:09:29 +00002924 CCInfo.nextInRegsParam();
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002925 continue;
2926 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002927
2928 // Arguments stored on registers
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002929 if (IsRegLoc) {
Akira Hatanaka7d822522013-10-28 21:21:36 +00002930 MVT RegVT = VA.getLocVT();
Akira Hatanakacb4a1a82011-05-24 00:23:52 +00002931 unsigned ArgReg = VA.getLocReg();
Akira Hatanaka7d822522013-10-28 21:21:36 +00002932 const TargetRegisterClass *RC = getRegClassFor(RegVT);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002933
Wesley Peck527da1b2010-11-23 03:31:01 +00002934 // Transform the arguments stored on
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002935 // physical registers into virtual ones
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002936 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC);
2937 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
Wesley Peck527da1b2010-11-23 03:31:01 +00002938
Daniel Sandersc43cda82014-11-07 16:54:21 +00002939 ArgValue = UnpackFromArgumentSlot(ArgValue, VA, Ins[i].ArgVT, DL, DAG);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002940
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002941 // Handle floating point arguments passed in integer registers and
2942 // long double arguments passed in floating point registers.
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002943 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002944 (RegVT == MVT::i64 && ValVT == MVT::f64) ||
2945 (RegVT == MVT::f64 && ValVT == MVT::i64))
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002946 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
Eric Christopher96e72c62015-01-29 23:27:36 +00002947 else if (ABI.IsO32() && RegVT == MVT::i32 &&
Eric Christopherbf33a3c2014-07-02 23:18:40 +00002948 ValVT == MVT::f64) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002949 unsigned Reg2 = addLiveIn(DAG.getMachineFunction(),
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002950 getNextIntArgReg(ArgReg), RC);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002951 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT);
Eric Christopher1c29a652014-07-18 22:55:25 +00002952 if (!Subtarget.isLittle())
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002953 std::swap(ArgValue, ArgValue2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002954 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64,
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002955 ArgValue, ArgValue2);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002956 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002957
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002958 InVals.push_back(ArgValue);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002959 } else { // VA.isRegLoc()
Daniel Sandersc43cda82014-11-07 16:54:21 +00002960 MVT LocVT = VA.getLocVT();
2961
Eric Christopher96e72c62015-01-29 23:27:36 +00002962 if (ABI.IsO32()) {
Daniel Sandersc43cda82014-11-07 16:54:21 +00002963 // We ought to be able to use LocVT directly but O32 sets it to i32
2964 // when allocating floating point values to integer registers.
2965 // This shouldn't influence how we load the value into registers unless
2966 // we are targetting softfloat.
2967 if (VA.getValVT().isFloatingPoint() && !Subtarget.abiUsesSoftFloat())
2968 LocVT = VA.getValVT();
2969 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002970
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002971 // sanity check
2972 assert(VA.isMemLoc());
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002973
Wesley Peck527da1b2010-11-23 03:31:01 +00002974 // The stack pointer offset is relative to the caller stack frame.
Daniel Sandersc43cda82014-11-07 16:54:21 +00002975 int FI = MFI->CreateFixedObject(LocVT.getSizeInBits() / 8,
Akira Hatanakacb4a1a82011-05-24 00:23:52 +00002976 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002977
2978 // Create load nodes to retrieve arguments from the stack
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002979 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Daniel Sandersc43cda82014-11-07 16:54:21 +00002980 SDValue ArgValue = DAG.getLoad(LocVT, DL, Chain, FIN,
2981 MachinePointerInfo::getFixedStack(FI),
2982 false, false, false, 0);
2983 OutChains.push_back(ArgValue.getValue(1));
2984
2985 ArgValue = UnpackFromArgumentSlot(ArgValue, VA, Ins[i].ArgVT, DL, DAG);
2986
2987 InVals.push_back(ArgValue);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002988 }
Reid Kleckner7a59e082014-05-12 22:01:27 +00002989 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002990
Reid Kleckner7a59e082014-05-12 22:01:27 +00002991 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Reid Kleckner79418562014-05-09 22:32:13 +00002992 // The mips ABIs for returning structs by value requires that we copy
2993 // the sret argument into $v0 for the return. Save the argument into
2994 // a virtual register so that we can access it from the return points.
Reid Kleckner7a59e082014-05-12 22:01:27 +00002995 if (Ins[i].Flags.isSRet()) {
Reid Kleckner79418562014-05-09 22:32:13 +00002996 unsigned Reg = MipsFI->getSRetReturnReg();
2997 if (!Reg) {
2998 Reg = MF.getRegInfo().createVirtualRegister(
Eric Christopher96e72c62015-01-29 23:27:36 +00002999 getRegClassFor(ABI.IsN64() ? MVT::i64 : MVT::i32));
Reid Kleckner79418562014-05-09 22:32:13 +00003000 MipsFI->setSRetReturnReg(Reg);
3001 }
Reid Kleckner7a59e082014-05-12 22:01:27 +00003002 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[i]);
Reid Kleckner79418562014-05-09 22:32:13 +00003003 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
Reid Kleckner7a59e082014-05-12 22:01:27 +00003004 break;
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003005 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003006 }
3007
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003008 if (IsVarArg)
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003009 writeVarArgRegs(OutChains, Chain, DL, DAG, CCInfo);
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003010
Wesley Peck527da1b2010-11-23 03:31:01 +00003011 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003012 // the size of Ins and InVals. This only happens when on varg functions
3013 if (!OutChains.empty()) {
3014 OutChains.push_back(Chain);
Craig Topper48d114b2014-04-26 18:35:24 +00003015 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003016 }
3017
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003018 return Chain;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003019}
3020
Akira Hatanakae2489122011-04-15 21:51:11 +00003021//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003022// Return Value Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00003023//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003024
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003025bool
3026MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003027 MachineFunction &MF, bool IsVarArg,
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003028 const SmallVectorImpl<ISD::OutputArg> &Outs,
3029 LLVMContext &Context) const {
3030 SmallVector<CCValAssign, 16> RVLocs;
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003031 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003032 return CCInfo.CheckReturn(Outs, RetCC_Mips);
3033}
3034
Petar Jovanovic5b436222015-03-23 12:28:13 +00003035bool
3036MipsTargetLowering::shouldSignExtendTypeInLibCall(EVT Type, bool IsSigned) const {
3037 if (Subtarget.hasMips3() && Subtarget.abiUsesSoftFloat()) {
3038 if (Type == MVT::i32)
3039 return true;
3040 }
3041 return IsSigned;
3042}
3043
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003044SDValue
3045MipsTargetLowering::LowerReturn(SDValue Chain,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003046 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003047 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003048 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003049 SDLoc DL, SelectionDAG &DAG) const {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003050 // CCValAssign - represent the assignment of
3051 // the return value to a location
3052 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003053 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003054
3055 // CCState - Info about the registers and stack slot.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003056 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, *DAG.getContext());
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003057
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003058 // Analyze return values.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003059 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003060
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003061 SDValue Flag;
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003062 SmallVector<SDValue, 4> RetOps(1, Chain);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003063
3064 // Copy the result values into the output registers.
3065 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003066 SDValue Val = OutVals[i];
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003067 CCValAssign &VA = RVLocs[i];
3068 assert(VA.isRegLoc() && "Can only return in registers!");
Daniel Sandersae275e32014-09-25 12:15:05 +00003069 bool UseUpperBits = false;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003070
Daniel Sandersae275e32014-09-25 12:15:05 +00003071 switch (VA.getLocInfo()) {
3072 default:
3073 llvm_unreachable("Unknown loc info!");
3074 case CCValAssign::Full:
3075 break;
3076 case CCValAssign::BCvt:
3077 Val = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Val);
3078 break;
3079 case CCValAssign::AExtUpper:
3080 UseUpperBits = true;
3081 // Fallthrough
3082 case CCValAssign::AExt:
3083 Val = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Val);
3084 break;
3085 case CCValAssign::ZExtUpper:
3086 UseUpperBits = true;
3087 // Fallthrough
3088 case CCValAssign::ZExt:
3089 Val = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Val);
3090 break;
3091 case CCValAssign::SExtUpper:
3092 UseUpperBits = true;
3093 // Fallthrough
3094 case CCValAssign::SExt:
3095 Val = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Val);
3096 break;
3097 }
3098
3099 if (UseUpperBits) {
3100 unsigned ValSizeInBits = Outs[i].ArgVT.getSizeInBits();
3101 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
3102 Val = DAG.getNode(
3103 ISD::SHL, DL, VA.getLocVT(), Val,
3104 DAG.getConstant(LocSizeInBits - ValSizeInBits, VA.getLocVT()));
3105 }
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003106
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003107 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003108
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003109 // Guarantee that all emitted copies are stuck together with flags.
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003110 Flag = Chain.getValue(1);
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003111 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003112 }
3113
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003114 // The mips ABIs for returning structs by value requires that we copy
3115 // the sret argument into $v0 for the return. We saved the argument into
3116 // a virtual register in the entry block, so now we copy the value out
3117 // and into $v0.
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003118 if (MF.getFunction()->hasStructRetAttr()) {
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003119 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3120 unsigned Reg = MipsFI->getSRetReturnReg();
3121
Wesley Peck527da1b2010-11-23 03:31:01 +00003122 if (!Reg)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003123 llvm_unreachable("sret virtual register not created in the entry block");
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003124 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Eric Christopher96e72c62015-01-29 23:27:36 +00003125 unsigned V0 = ABI.IsN64() ? Mips::V0_64 : Mips::V0;
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003126
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003127 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003128 Flag = Chain.getValue(1);
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003129 RetOps.push_back(DAG.getRegister(V0, getPointerTy()));
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003130 }
3131
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003132 RetOps[0] = Chain; // Update chain.
Akira Hatanakaefff7b72012-07-10 00:19:06 +00003133
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003134 // Add the flag if we have it.
3135 if (Flag.getNode())
3136 RetOps.push_back(Flag);
3137
3138 // Return on Mips is always a "jr $ra"
Craig Topper48d114b2014-04-26 18:35:24 +00003139 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, RetOps);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003140}
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003141
Akira Hatanakae2489122011-04-15 21:51:11 +00003142//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003143// Mips Inline Assembly Support
Akira Hatanakae2489122011-04-15 21:51:11 +00003144//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003145
3146/// getConstraintType - Given a constraint letter, return the type of
3147/// constraint it is for this target.
3148MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peck527da1b2010-11-23 03:31:01 +00003149getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003150{
Daniel Sanders8b59af12013-11-12 12:56:01 +00003151 // Mips specific constraints
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003152 // GCC config/mips/constraints.md
3153 //
Wesley Peck527da1b2010-11-23 03:31:01 +00003154 // 'd' : An address register. Equivalent to r
3155 // unless generating MIPS16 code.
3156 // 'y' : Equivalent to r; retained for
3157 // backwards compatibility.
Eric Christophere3c494d2012-05-07 06:25:10 +00003158 // 'c' : A register suitable for use in an indirect
3159 // jump. This will always be $25 for -mabicalls.
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003160 // 'l' : The lo register. 1 word storage.
3161 // 'x' : The hilo register pair. Double word storage.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003162 if (Constraint.size() == 1) {
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003163 switch (Constraint[0]) {
3164 default : break;
Wesley Peck527da1b2010-11-23 03:31:01 +00003165 case 'd':
3166 case 'y':
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003167 case 'f':
Eric Christophere3c494d2012-05-07 06:25:10 +00003168 case 'c':
Eric Christopher9c492e62012-05-07 06:25:15 +00003169 case 'l':
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003170 case 'x':
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003171 return C_RegisterClass;
Jack Carter0e149b02013-03-04 21:33:15 +00003172 case 'R':
3173 return C_Memory;
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003174 }
3175 }
Daniel Sandersa73d8fe2015-03-24 11:26:34 +00003176
3177 if (Constraint == "ZC")
3178 return C_Memory;
3179
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003180 return TargetLowering::getConstraintType(Constraint);
3181}
3182
John Thompsone8360b72010-10-29 17:29:13 +00003183/// Examine constraint type and operand type and determine a weight value.
3184/// This object must already have been set up with the operand type
3185/// and the current alternative constraint selected.
3186TargetLowering::ConstraintWeight
3187MipsTargetLowering::getSingleConstraintMatchWeight(
3188 AsmOperandInfo &info, const char *constraint) const {
3189 ConstraintWeight weight = CW_Invalid;
3190 Value *CallOperandVal = info.CallOperandVal;
3191 // If we don't have a value, we can't do a match,
3192 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +00003193 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +00003194 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +00003195 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +00003196 // Look at the constraint type.
3197 switch (*constraint) {
3198 default:
3199 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
3200 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00003201 case 'd':
3202 case 'y':
John Thompsone8360b72010-10-29 17:29:13 +00003203 if (type->isIntegerTy())
3204 weight = CW_Register;
3205 break;
Daniel Sanders8b59af12013-11-12 12:56:01 +00003206 case 'f': // FPU or MSA register
Eric Christopher1c29a652014-07-18 22:55:25 +00003207 if (Subtarget.hasMSA() && type->isVectorTy() &&
Daniel Sanders8b59af12013-11-12 12:56:01 +00003208 cast<VectorType>(type)->getBitWidth() == 128)
3209 weight = CW_Register;
3210 else if (type->isFloatTy())
John Thompsone8360b72010-10-29 17:29:13 +00003211 weight = CW_Register;
3212 break;
Eric Christophere3c494d2012-05-07 06:25:10 +00003213 case 'c': // $25 for indirect jumps
Eric Christopher9c492e62012-05-07 06:25:15 +00003214 case 'l': // lo register
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003215 case 'x': // hilo register pair
Daniel Sanders8b59af12013-11-12 12:56:01 +00003216 if (type->isIntegerTy())
Eric Christophere3c494d2012-05-07 06:25:10 +00003217 weight = CW_SpecificReg;
Daniel Sanders8b59af12013-11-12 12:56:01 +00003218 break;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003219 case 'I': // signed 16 bit immediate
Eric Christopher7201e1b2012-05-07 03:13:42 +00003220 case 'J': // integer zero
Eric Christopher3ff88a02012-05-07 05:46:29 +00003221 case 'K': // unsigned 16 bit immediate
Eric Christopher1109b342012-05-07 05:46:37 +00003222 case 'L': // signed 32 bit immediate where lower 16 bits are 0
Eric Christophere07aa432012-05-07 05:46:43 +00003223 case 'N': // immediate in the range of -65535 to -1 (inclusive)
Eric Christopher470578a2012-05-07 05:46:48 +00003224 case 'O': // signed 15 bit immediate (+- 16383)
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003225 case 'P': // immediate in the range of 65535 to 1 (inclusive)
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003226 if (isa<ConstantInt>(CallOperandVal))
3227 weight = CW_Constant;
3228 break;
Jack Carter0e149b02013-03-04 21:33:15 +00003229 case 'R':
3230 weight = CW_Memory;
3231 break;
John Thompsone8360b72010-10-29 17:29:13 +00003232 }
3233 return weight;
3234}
3235
Akira Hatanaka7473b472013-08-14 00:21:25 +00003236/// This is a helper function to parse a physical register string and split it
3237/// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag
3238/// that is returned indicates whether parsing was successful. The second flag
3239/// is true if the numeric part exists.
3240static std::pair<bool, bool>
Craig Topper6dc4a8bc2014-08-30 16:48:02 +00003241parsePhysicalReg(StringRef C, std::string &Prefix,
Akira Hatanaka7473b472013-08-14 00:21:25 +00003242 unsigned long long &Reg) {
3243 if (C.front() != '{' || C.back() != '}')
3244 return std::make_pair(false, false);
3245
3246 // Search for the first numeric character.
3247 StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1;
3248 I = std::find_if(B, E, std::ptr_fun(isdigit));
3249
3250 Prefix.assign(B, I - B);
3251
3252 // The second flag is set to false if no numeric characters were found.
3253 if (I == E)
3254 return std::make_pair(true, false);
3255
3256 // Parse the numeric characters.
3257 return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg),
3258 true);
3259}
3260
3261std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering::
Craig Topper6dc4a8bc2014-08-30 16:48:02 +00003262parseRegForInlineAsmConstraint(StringRef C, MVT VT) const {
Eric Christopherd9134482014-08-04 21:25:23 +00003263 const TargetRegisterInfo *TRI =
Eric Christopher96e72c62015-01-29 23:27:36 +00003264 Subtarget.getRegisterInfo();
Akira Hatanaka7473b472013-08-14 00:21:25 +00003265 const TargetRegisterClass *RC;
3266 std::string Prefix;
3267 unsigned long long Reg;
3268
3269 std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg);
3270
3271 if (!R.first)
Craig Topper062a2ba2014-04-25 05:30:21 +00003272 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003273
3274 if ((Prefix == "hi" || Prefix == "lo")) { // Parse hi/lo.
3275 // No numeric characters follow "hi" or "lo".
3276 if (R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003277 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003278
3279 RC = TRI->getRegClass(Prefix == "hi" ?
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00003280 Mips::HI32RegClassID : Mips::LO32RegClassID);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003281 return std::make_pair(*(RC->begin()), RC);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003282 } else if (Prefix.compare(0, 4, "$msa") == 0) {
3283 // Parse $msa(ir|csr|access|save|modify|request|map|unmap)
3284
3285 // No numeric characters follow the name.
3286 if (R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003287 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003288
3289 Reg = StringSwitch<unsigned long long>(Prefix)
3290 .Case("$msair", Mips::MSAIR)
3291 .Case("$msacsr", Mips::MSACSR)
3292 .Case("$msaaccess", Mips::MSAAccess)
3293 .Case("$msasave", Mips::MSASave)
3294 .Case("$msamodify", Mips::MSAModify)
3295 .Case("$msarequest", Mips::MSARequest)
3296 .Case("$msamap", Mips::MSAMap)
3297 .Case("$msaunmap", Mips::MSAUnmap)
3298 .Default(0);
3299
3300 if (!Reg)
Craig Topper062a2ba2014-04-25 05:30:21 +00003301 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003302
3303 RC = TRI->getRegClass(Mips::MSACtrlRegClassID);
3304 return std::make_pair(Reg, RC);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003305 }
3306
3307 if (!R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003308 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003309
3310 if (Prefix == "$f") { // Parse $f0-$f31.
3311 // If the size of FP registers is 64-bit or Reg is an even number, select
3312 // the 64-bit register class. Otherwise, select the 32-bit register class.
3313 if (VT == MVT::Other)
Eric Christopher1c29a652014-07-18 22:55:25 +00003314 VT = (Subtarget.isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32;
Akira Hatanaka7473b472013-08-14 00:21:25 +00003315
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003316 RC = getRegClassFor(VT);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003317
3318 if (RC == &Mips::AFGR64RegClass) {
3319 assert(Reg % 2 == 0);
3320 Reg >>= 1;
3321 }
Daniel Sanders8b59af12013-11-12 12:56:01 +00003322 } else if (Prefix == "$fcc") // Parse $fcc0-$fcc7.
Akira Hatanaka7473b472013-08-14 00:21:25 +00003323 RC = TRI->getRegClass(Mips::FCCRegClassID);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003324 else if (Prefix == "$w") { // Parse $w0-$w31.
3325 RC = getRegClassFor((VT == MVT::Other) ? MVT::v16i8 : VT);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003326 } else { // Parse $0-$31.
3327 assert(Prefix == "$");
3328 RC = getRegClassFor((VT == MVT::Other) ? MVT::i32 : VT);
3329 }
3330
3331 assert(Reg < RC->getNumRegs());
3332 return std::make_pair(*(RC->begin() + Reg), RC);
3333}
3334
Eric Christophereaf77dc2011-06-29 19:33:04 +00003335/// Given a register class constraint, like 'r', if this corresponds directly
3336/// to an LLVM register class, return a register of 0 and the register class
3337/// pointer.
Eric Christopher11e4df72015-02-26 22:38:43 +00003338std::pair<unsigned, const TargetRegisterClass *>
3339MipsTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
3340 const std::string &Constraint,
3341 MVT VT) const {
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003342 if (Constraint.size() == 1) {
3343 switch (Constraint[0]) {
Eric Christopher9519c082011-06-29 19:04:31 +00003344 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
3345 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003346 case 'r':
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003347 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
Eric Christopher1c29a652014-07-18 22:55:25 +00003348 if (Subtarget.inMips16Mode())
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003349 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003350 return std::make_pair(0U, &Mips::GPR32RegClass);
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003351 }
Eric Christopher1c29a652014-07-18 22:55:25 +00003352 if (VT == MVT::i64 && !Subtarget.isGP64bit())
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003353 return std::make_pair(0U, &Mips::GPR32RegClass);
Eric Christopher1c29a652014-07-18 22:55:25 +00003354 if (VT == MVT::i64 && Subtarget.isGP64bit())
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003355 return std::make_pair(0U, &Mips::GPR64RegClass);
Eric Christopher58daf042012-05-07 03:13:22 +00003356 // This will generate an error message
Craig Topper062a2ba2014-04-25 05:30:21 +00003357 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003358 case 'f': // FPU or MSA register
3359 if (VT == MVT::v16i8)
3360 return std::make_pair(0U, &Mips::MSA128BRegClass);
3361 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
3362 return std::make_pair(0U, &Mips::MSA128HRegClass);
3363 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
3364 return std::make_pair(0U, &Mips::MSA128WRegClass);
3365 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
3366 return std::make_pair(0U, &Mips::MSA128DRegClass);
3367 else if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00003368 return std::make_pair(0U, &Mips::FGR32RegClass);
Eric Christopher1c29a652014-07-18 22:55:25 +00003369 else if ((VT == MVT::f64) && (!Subtarget.isSingleFloat())) {
3370 if (Subtarget.isFP64bit())
Craig Topperc7242e02012-04-20 07:30:17 +00003371 return std::make_pair(0U, &Mips::FGR64RegClass);
3372 return std::make_pair(0U, &Mips::AFGR64RegClass);
Akira Hatanakac669d7a2012-01-04 02:45:01 +00003373 }
Eric Christophere3c494d2012-05-07 06:25:10 +00003374 break;
3375 case 'c': // register suitable for indirect jump
3376 if (VT == MVT::i32)
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003377 return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass);
Eric Christophere3c494d2012-05-07 06:25:10 +00003378 assert(VT == MVT::i64 && "Unexpected type.");
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003379 return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass);
Eric Christopher9c492e62012-05-07 06:25:15 +00003380 case 'l': // register suitable for indirect jump
3381 if (VT == MVT::i32)
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00003382 return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass);
3383 return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass);
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003384 case 'x': // register suitable for indirect jump
3385 // Fixme: Not triggering the use of both hi and low
3386 // This will generate an error message
Craig Topper062a2ba2014-04-25 05:30:21 +00003387 return std::make_pair(0U, nullptr);
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003388 }
3389 }
Akira Hatanaka7473b472013-08-14 00:21:25 +00003390
3391 std::pair<unsigned, const TargetRegisterClass *> R;
3392 R = parseRegForInlineAsmConstraint(Constraint, VT);
3393
3394 if (R.second)
3395 return R;
3396
Eric Christopher11e4df72015-02-26 22:38:43 +00003397 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003398}
3399
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003400/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3401/// vector. If it is invalid, don't add anything to Ops.
3402void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3403 std::string &Constraint,
3404 std::vector<SDValue>&Ops,
3405 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +00003406 SDValue Result;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003407
3408 // Only support length 1 constraints for now.
3409 if (Constraint.length() > 1) return;
3410
3411 char ConstraintLetter = Constraint[0];
3412 switch (ConstraintLetter) {
3413 default: break; // This will fall through to the generic implementation
3414 case 'I': // Signed 16 bit constant
3415 // If this fails, the parent routine will give an error
3416 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3417 EVT Type = Op.getValueType();
3418 int64_t Val = C->getSExtValue();
3419 if (isInt<16>(Val)) {
3420 Result = DAG.getTargetConstant(Val, Type);
3421 break;
3422 }
3423 }
3424 return;
Eric Christopher7201e1b2012-05-07 03:13:42 +00003425 case 'J': // integer zero
3426 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3427 EVT Type = Op.getValueType();
3428 int64_t Val = C->getZExtValue();
3429 if (Val == 0) {
3430 Result = DAG.getTargetConstant(0, Type);
3431 break;
3432 }
3433 }
3434 return;
Eric Christopher3ff88a02012-05-07 05:46:29 +00003435 case 'K': // unsigned 16 bit immediate
3436 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3437 EVT Type = Op.getValueType();
3438 uint64_t Val = (uint64_t)C->getZExtValue();
3439 if (isUInt<16>(Val)) {
3440 Result = DAG.getTargetConstant(Val, Type);
3441 break;
3442 }
3443 }
3444 return;
Eric Christopher1109b342012-05-07 05:46:37 +00003445 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3446 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3447 EVT Type = Op.getValueType();
3448 int64_t Val = C->getSExtValue();
3449 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
3450 Result = DAG.getTargetConstant(Val, Type);
3451 break;
3452 }
3453 }
3454 return;
Eric Christophere07aa432012-05-07 05:46:43 +00003455 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3456 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3457 EVT Type = Op.getValueType();
3458 int64_t Val = C->getSExtValue();
3459 if ((Val >= -65535) && (Val <= -1)) {
3460 Result = DAG.getTargetConstant(Val, Type);
3461 break;
3462 }
3463 }
3464 return;
Eric Christopher470578a2012-05-07 05:46:48 +00003465 case 'O': // signed 15 bit immediate
3466 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3467 EVT Type = Op.getValueType();
3468 int64_t Val = C->getSExtValue();
3469 if ((isInt<15>(Val))) {
3470 Result = DAG.getTargetConstant(Val, Type);
3471 break;
3472 }
3473 }
3474 return;
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003475 case 'P': // immediate in the range of 1 to 65535 (inclusive)
3476 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3477 EVT Type = Op.getValueType();
3478 int64_t Val = C->getSExtValue();
3479 if ((Val <= 65535) && (Val >= 1)) {
3480 Result = DAG.getTargetConstant(Val, Type);
3481 break;
3482 }
3483 }
3484 return;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003485 }
3486
3487 if (Result.getNode()) {
3488 Ops.push_back(Result);
3489 return;
3490 }
3491
3492 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3493}
3494
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003495bool MipsTargetLowering::isLegalAddressingMode(const AddrMode &AM,
3496 Type *Ty) const {
Akira Hatanakaef839192012-11-17 00:25:41 +00003497 // No global is ever allowed as a base.
3498 if (AM.BaseGV)
3499 return false;
3500
3501 switch (AM.Scale) {
3502 case 0: // "r+i" or just "i", depending on HasBaseReg.
3503 break;
3504 case 1:
3505 if (!AM.HasBaseReg) // allow "r+i".
3506 break;
3507 return false; // disallow "r+r" or "r+r+i".
3508 default:
3509 return false;
3510 }
3511
3512 return true;
3513}
3514
3515bool
Dan Gohman2fe6bee2008-10-18 02:06:02 +00003516MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3517 // The Mips target isn't yet aware of offsets.
3518 return false;
3519}
Evan Cheng16993aa2009-10-27 19:56:55 +00003520
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003521EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003522 unsigned SrcAlign,
3523 bool IsMemset, bool ZeroMemset,
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003524 bool MemcpyStrSrc,
3525 MachineFunction &MF) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003526 if (Subtarget.hasMips64())
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003527 return MVT::i64;
3528
3529 return MVT::i32;
3530}
3531
Evan Cheng83896a52009-10-28 01:43:28 +00003532bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
3533 if (VT != MVT::f32 && VT != MVT::f64)
3534 return false;
Bruno Cardoso Lopesb02a9df2011-01-18 19:41:41 +00003535 if (Imm.isNegZero())
3536 return false;
Evan Cheng16993aa2009-10-27 19:56:55 +00003537 return Imm.isZero();
3538}
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003539
3540unsigned MipsTargetLowering::getJumpTableEncoding() const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003541 if (ABI.IsN64())
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003542 return MachineJumpTableInfo::EK_GPRel64BlockAddress;
Jia Liuf54f60f2012-02-28 07:46:26 +00003543
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003544 return TargetLowering::getJumpTableEncoding();
3545}
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003546
Daniel Sandersf43e6872014-11-01 18:44:56 +00003547void MipsTargetLowering::copyByValRegs(
3548 SDValue Chain, SDLoc DL, std::vector<SDValue> &OutChains, SelectionDAG &DAG,
3549 const ISD::ArgFlagsTy &Flags, SmallVectorImpl<SDValue> &InVals,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003550 const Argument *FuncArg, unsigned FirstReg, unsigned LastReg,
3551 const CCValAssign &VA, MipsCCState &State) const {
Akira Hatanaka25dad192012-10-27 00:10:18 +00003552 MachineFunction &MF = DAG.getMachineFunction();
3553 MachineFrameInfo *MFI = MF.getFrameInfo();
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003554 unsigned GPRSizeInBytes = Subtarget.getGPRSizeInBytes();
Daniel Sanders23e98772014-11-02 16:09:29 +00003555 unsigned NumRegs = LastReg - FirstReg;
3556 unsigned RegAreaSize = NumRegs * GPRSizeInBytes;
Akira Hatanaka25dad192012-10-27 00:10:18 +00003557 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
3558 int FrameObjOffset;
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003559 ArrayRef<MCPhysReg> ByValArgRegs = ABI.GetByValArgRegs();
Akira Hatanaka25dad192012-10-27 00:10:18 +00003560
3561 if (RegAreaSize)
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003562 FrameObjOffset =
3563 (int)ABI.GetCalleeAllocdArgSizeInBytes(State.getCallingConv()) -
3564 (int)((ByValArgRegs.size() - FirstReg) * GPRSizeInBytes);
Akira Hatanaka25dad192012-10-27 00:10:18 +00003565 else
Daniel Sandersf43e6872014-11-01 18:44:56 +00003566 FrameObjOffset = VA.getLocMemOffset();
Akira Hatanaka25dad192012-10-27 00:10:18 +00003567
3568 // Create frame object.
3569 EVT PtrTy = getPointerTy();
3570 int FI = MFI->CreateFixedObject(FrameObjSize, FrameObjOffset, true);
3571 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
3572 InVals.push_back(FIN);
3573
Daniel Sanders23e98772014-11-02 16:09:29 +00003574 if (!NumRegs)
Akira Hatanaka25dad192012-10-27 00:10:18 +00003575 return;
3576
3577 // Copy arg registers.
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003578 MVT RegTy = MVT::getIntegerVT(GPRSizeInBytes * 8);
Akira Hatanaka25dad192012-10-27 00:10:18 +00003579 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3580
Daniel Sanders23e98772014-11-02 16:09:29 +00003581 for (unsigned I = 0; I < NumRegs; ++I) {
Daniel Sandersd7eba312014-11-07 12:21:37 +00003582 unsigned ArgReg = ByValArgRegs[FirstReg + I];
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003583 unsigned VReg = addLiveIn(MF, ArgReg, RC);
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003584 unsigned Offset = I * GPRSizeInBytes;
Akira Hatanaka25dad192012-10-27 00:10:18 +00003585 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
3586 DAG.getConstant(Offset, PtrTy));
3587 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
3588 StorePtr, MachinePointerInfo(FuncArg, Offset),
3589 false, false, 0);
3590 OutChains.push_back(Store);
3591 }
3592}
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003593
3594// Copy byVal arg to registers and stack.
Daniel Sandersf43e6872014-11-01 18:44:56 +00003595void MipsTargetLowering::passByValArg(
3596 SDValue Chain, SDLoc DL,
3597 std::deque<std::pair<unsigned, SDValue>> &RegsToPass,
3598 SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003599 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg, unsigned FirstReg,
3600 unsigned LastReg, const ISD::ArgFlagsTy &Flags, bool isLittle,
3601 const CCValAssign &VA) const {
Daniel Sandersac272632014-05-23 13:18:02 +00003602 unsigned ByValSizeInBytes = Flags.getByValSize();
3603 unsigned OffsetInBytes = 0; // From beginning of struct
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003604 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
Daniel Sandersac272632014-05-23 13:18:02 +00003605 unsigned Alignment = std::min(Flags.getByValAlign(), RegSizeInBytes);
3606 EVT PtrTy = getPointerTy(), RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
Daniel Sanders23e98772014-11-02 16:09:29 +00003607 unsigned NumRegs = LastReg - FirstReg;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003608
Daniel Sanders23e98772014-11-02 16:09:29 +00003609 if (NumRegs) {
Eric Christopher96e72c62015-01-29 23:27:36 +00003610 const ArrayRef<MCPhysReg> ArgRegs = ABI.GetByValArgRegs();
Daniel Sanders23e98772014-11-02 16:09:29 +00003611 bool LeftoverBytes = (NumRegs * RegSizeInBytes > ByValSizeInBytes);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003612 unsigned I = 0;
3613
3614 // Copy words to registers.
Daniel Sanders23e98772014-11-02 16:09:29 +00003615 for (; I < NumRegs - LeftoverBytes; ++I, OffsetInBytes += RegSizeInBytes) {
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003616 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Daniel Sandersac272632014-05-23 13:18:02 +00003617 DAG.getConstant(OffsetInBytes, PtrTy));
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003618 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
3619 MachinePointerInfo(), false, false, false,
3620 Alignment);
3621 MemOpChains.push_back(LoadVal.getValue(1));
Daniel Sanders23e98772014-11-02 16:09:29 +00003622 unsigned ArgReg = ArgRegs[FirstReg + I];
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003623 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
3624 }
3625
3626 // Return if the struct has been fully copied.
Daniel Sandersac272632014-05-23 13:18:02 +00003627 if (ByValSizeInBytes == OffsetInBytes)
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003628 return;
3629
3630 // Copy the remainder of the byval argument with sub-word loads and shifts.
3631 if (LeftoverBytes) {
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003632 SDValue Val;
3633
Daniel Sandersac272632014-05-23 13:18:02 +00003634 for (unsigned LoadSizeInBytes = RegSizeInBytes / 2, TotalBytesLoaded = 0;
3635 OffsetInBytes < ByValSizeInBytes; LoadSizeInBytes /= 2) {
3636 unsigned RemainingSizeInBytes = ByValSizeInBytes - OffsetInBytes;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003637
Daniel Sandersac272632014-05-23 13:18:02 +00003638 if (RemainingSizeInBytes < LoadSizeInBytes)
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003639 continue;
3640
3641 // Load subword.
3642 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Daniel Sandersac272632014-05-23 13:18:02 +00003643 DAG.getConstant(OffsetInBytes, PtrTy));
3644 SDValue LoadVal = DAG.getExtLoad(
3645 ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr, MachinePointerInfo(),
Louis Gerbarg67474e32014-07-31 21:45:05 +00003646 MVT::getIntegerVT(LoadSizeInBytes * 8), false, false, false,
3647 Alignment);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003648 MemOpChains.push_back(LoadVal.getValue(1));
3649
3650 // Shift the loaded value.
3651 unsigned Shamt;
3652
3653 if (isLittle)
Daniel Sandersac272632014-05-23 13:18:02 +00003654 Shamt = TotalBytesLoaded * 8;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003655 else
Daniel Sandersac272632014-05-23 13:18:02 +00003656 Shamt = (RegSizeInBytes - (TotalBytesLoaded + LoadSizeInBytes)) * 8;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003657
3658 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
3659 DAG.getConstant(Shamt, MVT::i32));
3660
3661 if (Val.getNode())
3662 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
3663 else
3664 Val = Shift;
3665
Daniel Sandersac272632014-05-23 13:18:02 +00003666 OffsetInBytes += LoadSizeInBytes;
3667 TotalBytesLoaded += LoadSizeInBytes;
3668 Alignment = std::min(Alignment, LoadSizeInBytes);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003669 }
3670
Daniel Sanders23e98772014-11-02 16:09:29 +00003671 unsigned ArgReg = ArgRegs[FirstReg + I];
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003672 RegsToPass.push_back(std::make_pair(ArgReg, Val));
3673 return;
3674 }
3675 }
3676
3677 // Copy remainder of byval arg to it with memcpy.
Daniel Sandersac272632014-05-23 13:18:02 +00003678 unsigned MemCpySize = ByValSizeInBytes - OffsetInBytes;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003679 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Daniel Sandersac272632014-05-23 13:18:02 +00003680 DAG.getConstant(OffsetInBytes, PtrTy));
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003681 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
Daniel Sandersf43e6872014-11-01 18:44:56 +00003682 DAG.getIntPtrConstant(VA.getLocMemOffset()));
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003683 Chain = DAG.getMemcpy(Chain, DL, Dst, Src, DAG.getConstant(MemCpySize, PtrTy),
3684 Alignment, /*isVolatile=*/false, /*AlwaysInline=*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00003685 /*isTailCall=*/false,
Nick Lewyckyaad475b2014-04-15 07:22:52 +00003686 MachinePointerInfo(), MachinePointerInfo());
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003687 MemOpChains.push_back(Chain);
3688}
Akira Hatanaka2a134022012-10-27 00:21:13 +00003689
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003690void MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003691 SDValue Chain, SDLoc DL,
3692 SelectionDAG &DAG,
Daniel Sanders853c2432014-11-01 18:13:52 +00003693 CCState &State) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003694 const ArrayRef<MCPhysReg> ArgRegs = ABI.GetVarArgRegs();
Tim Northover3b6b7ca2015-02-21 02:11:17 +00003695 unsigned Idx = State.getFirstUnallocated(ArgRegs);
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003696 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
3697 MVT RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003698 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3699 MachineFunction &MF = DAG.getMachineFunction();
3700 MachineFrameInfo *MFI = MF.getFrameInfo();
3701 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3702
3703 // Offset of the first variable argument from stack pointer.
3704 int VaArgOffset;
3705
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003706 if (ArgRegs.size() == Idx)
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003707 VaArgOffset =
Daniel Sanders853c2432014-11-01 18:13:52 +00003708 RoundUpToAlignment(State.getNextStackOffset(), RegSizeInBytes);
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003709 else {
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003710 VaArgOffset =
3711 (int)ABI.GetCalleeAllocdArgSizeInBytes(State.getCallingConv()) -
3712 (int)(RegSizeInBytes * (ArgRegs.size() - Idx));
3713 }
Akira Hatanaka2a134022012-10-27 00:21:13 +00003714
3715 // Record the frame index of the first variable argument
3716 // which is a value necessary to VASTART.
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003717 int FI = MFI->CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003718 MipsFI->setVarArgsFrameIndex(FI);
3719
3720 // Copy the integer registers that have not been used for argument passing
3721 // to the argument register save area. For O32, the save area is allocated
3722 // in the caller's stack frame, while for N32/64, it is allocated in the
3723 // callee's stack frame.
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003724 for (unsigned I = Idx; I < ArgRegs.size();
3725 ++I, VaArgOffset += RegSizeInBytes) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003726 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003727 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003728 FI = MFI->CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003729 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
3730 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
3731 MachinePointerInfo(), false, false, 0);
Eric Christopher1c29a652014-07-18 22:55:25 +00003732 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(
3733 (Value *)nullptr);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003734 OutChains.push_back(Store);
3735 }
3736}
Daniel Sanders23e98772014-11-02 16:09:29 +00003737
3738void MipsTargetLowering::HandleByVal(CCState *State, unsigned &Size,
3739 unsigned Align) const {
Eric Christopher96e72c62015-01-29 23:27:36 +00003740 const TargetFrameLowering *TFL = Subtarget.getFrameLowering();
Daniel Sanders23e98772014-11-02 16:09:29 +00003741
3742 assert(Size && "Byval argument's size shouldn't be 0.");
3743
3744 Align = std::min(Align, TFL->getStackAlignment());
3745
3746 unsigned FirstReg = 0;
3747 unsigned NumRegs = 0;
3748
3749 if (State->getCallingConv() != CallingConv::Fast) {
3750 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
Eric Christopher96e72c62015-01-29 23:27:36 +00003751 const ArrayRef<MCPhysReg> IntArgRegs = ABI.GetByValArgRegs();
Daniel Sanders23e98772014-11-02 16:09:29 +00003752 // FIXME: The O32 case actually describes no shadow registers.
3753 const MCPhysReg *ShadowRegs =
Eric Christopher96e72c62015-01-29 23:27:36 +00003754 ABI.IsO32() ? IntArgRegs.data() : Mips64DPRegs;
Daniel Sanders23e98772014-11-02 16:09:29 +00003755
3756 // We used to check the size as well but we can't do that anymore since
3757 // CCState::HandleByVal() rounds up the size after calling this function.
3758 assert(!(Align % RegSizeInBytes) &&
3759 "Byval argument's alignment should be a multiple of"
3760 "RegSizeInBytes.");
3761
Tim Northover3b6b7ca2015-02-21 02:11:17 +00003762 FirstReg = State->getFirstUnallocated(IntArgRegs);
Daniel Sanders23e98772014-11-02 16:09:29 +00003763
3764 // If Align > RegSizeInBytes, the first arg register must be even.
3765 // FIXME: This condition happens to do the right thing but it's not the
3766 // right way to test it. We want to check that the stack frame offset
3767 // of the register is aligned.
3768 if ((Align > RegSizeInBytes) && (FirstReg % 2)) {
3769 State->AllocateReg(IntArgRegs[FirstReg], ShadowRegs[FirstReg]);
3770 ++FirstReg;
3771 }
3772
3773 // Mark the registers allocated.
3774 Size = RoundUpToAlignment(Size, RegSizeInBytes);
3775 for (unsigned I = FirstReg; Size > 0 && (I < IntArgRegs.size());
3776 Size -= RegSizeInBytes, ++I, ++NumRegs)
3777 State->AllocateReg(IntArgRegs[I], ShadowRegs[I]);
3778 }
3779
3780 State->addInRegsParamInfo(FirstReg, FirstReg + NumRegs);
3781}
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00003782
3783MachineBasicBlock *
3784MipsTargetLowering::emitPseudoSELECT(MachineInstr *MI, MachineBasicBlock *BB,
3785 bool isFPCmp, unsigned Opc) const {
3786 assert(!(Subtarget.hasMips4() || Subtarget.hasMips32()) &&
3787 "Subtarget already supports SELECT nodes with the use of"
3788 "conditional-move instructions.");
3789
3790 const TargetInstrInfo *TII =
Eric Christopher96e72c62015-01-29 23:27:36 +00003791 Subtarget.getInstrInfo();
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00003792 DebugLoc DL = MI->getDebugLoc();
3793
3794 // To "insert" a SELECT instruction, we actually have to insert the
3795 // diamond control-flow pattern. The incoming instruction knows the
3796 // destination vreg to set, the condition code register to branch on, the
3797 // true/false values to select between, and a branch opcode to use.
3798 const BasicBlock *LLVM_BB = BB->getBasicBlock();
3799 MachineFunction::iterator It = BB;
3800 ++It;
3801
3802 // thisMBB:
3803 // ...
3804 // TrueVal = ...
3805 // setcc r1, r2, r3
3806 // bNE r1, r0, copy1MBB
3807 // fallthrough --> copy0MBB
3808 MachineBasicBlock *thisMBB = BB;
3809 MachineFunction *F = BB->getParent();
3810 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
3811 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
3812 F->insert(It, copy0MBB);
3813 F->insert(It, sinkMBB);
3814
3815 // Transfer the remainder of BB and its successor edges to sinkMBB.
3816 sinkMBB->splice(sinkMBB->begin(), BB,
3817 std::next(MachineBasicBlock::iterator(MI)), BB->end());
3818 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
3819
3820 // Next, add the true and fallthrough blocks as its successors.
3821 BB->addSuccessor(copy0MBB);
3822 BB->addSuccessor(sinkMBB);
3823
3824 if (isFPCmp) {
3825 // bc1[tf] cc, sinkMBB
3826 BuildMI(BB, DL, TII->get(Opc))
3827 .addReg(MI->getOperand(1).getReg())
3828 .addMBB(sinkMBB);
3829 } else {
3830 // bne rs, $0, sinkMBB
3831 BuildMI(BB, DL, TII->get(Opc))
3832 .addReg(MI->getOperand(1).getReg())
3833 .addReg(Mips::ZERO)
3834 .addMBB(sinkMBB);
3835 }
3836
3837 // copy0MBB:
3838 // %FalseValue = ...
3839 // # fallthrough to sinkMBB
3840 BB = copy0MBB;
3841
3842 // Update machine-CFG edges
3843 BB->addSuccessor(sinkMBB);
3844
3845 // sinkMBB:
3846 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
3847 // ...
3848 BB = sinkMBB;
3849
3850 BuildMI(*BB, BB->begin(), DL,
3851 TII->get(Mips::PHI), MI->getOperand(0).getReg())
3852 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
3853 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB);
3854
3855 MI->eraseFromParent(); // The pseudo instruction is gone now.
3856
3857 return BB;
3858}
Daniel Sanders1440bb22015-01-09 17:21:30 +00003859
3860// FIXME? Maybe this could be a TableGen attribute on some registers and
3861// this table could be generated automatically from RegInfo.
3862unsigned MipsTargetLowering::getRegisterByName(const char* RegName,
3863 EVT VT) const {
3864 // Named registers is expected to be fairly rare. For now, just support $28
3865 // since the linux kernel uses it.
3866 if (Subtarget.isGP64bit()) {
3867 unsigned Reg = StringSwitch<unsigned>(RegName)
3868 .Case("$28", Mips::GP_64)
3869 .Default(0);
3870 if (Reg)
3871 return Reg;
3872 } else {
3873 unsigned Reg = StringSwitch<unsigned>(RegName)
3874 .Case("$28", Mips::GP)
3875 .Default(0);
3876 if (Reg)
3877 return Reg;
3878 }
3879 report_fatal_error("Invalid register name global variable");
3880}