blob: 64244ee109653477eb16125806931e67729cb1c5 [file] [log] [blame]
Jia Liuf54f60f2012-02-28 07:46:26 +00001//===-- MipsISelLowering.cpp - Mips DAG Lowering Implementation -----------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00007//
Akira Hatanakae2489122011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00009//
10// This file defines the interfaces that Mips uses to lower LLVM code into a
11// selection DAG.
12//
Akira Hatanakae2489122011-04-15 21:51:11 +000013//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000014#include "MipsISelLowering.h"
Craig Topperb25fda92012-03-17 18:46:09 +000015#include "InstPrinter/MipsInstPrinter.h"
16#include "MCTargetDesc/MipsBaseInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "MipsMachineFunction.h"
18#include "MipsSubtarget.h"
19#include "MipsTargetMachine.h"
20#include "MipsTargetObjectFile.h"
Akira Hatanaka90131ac2012-10-19 21:47:33 +000021#include "llvm/ADT/Statistic.h"
Daniel Sanders8b59af12013-11-12 12:56:01 +000022#include "llvm/ADT/StringSwitch.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000023#include "llvm/CodeGen/CallingConvLower.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/MachineFunction.h"
26#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000027#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000028#include "llvm/CodeGen/SelectionDAGISel.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000029#include "llvm/CodeGen/ValueTypes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000030#include "llvm/IR/CallingConv.h"
31#include "llvm/IR/DerivedTypes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000032#include "llvm/IR/GlobalVariable.h"
Akira Hatanaka90131ac2012-10-19 21:47:33 +000033#include "llvm/Support/CommandLine.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000034#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000035#include "llvm/Support/ErrorHandling.h"
NAKAMURA Takumie30303f2012-04-21 15:31:45 +000036#include "llvm/Support/raw_ostream.h"
Akira Hatanaka7473b472013-08-14 00:21:25 +000037#include <cctype>
NAKAMURA Takumie30303f2012-04-21 15:31:45 +000038
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000039using namespace llvm;
40
Chandler Carruth84e68b22014-04-22 02:41:26 +000041#define DEBUG_TYPE "mips-lower"
42
Akira Hatanaka90131ac2012-10-19 21:47:33 +000043STATISTIC(NumTailCalls, "Number of tail calls");
44
45static cl::opt<bool>
Akira Hatanaka59f299f2012-11-21 20:21:11 +000046LargeGOT("mxgot", cl::Hidden,
47 cl::desc("MIPS: Enable GOT larger than 64k."), cl::init(false));
48
Akira Hatanaka1cb02422013-05-20 18:07:43 +000049static cl::opt<bool>
Akira Hatanakabe76cd02013-05-21 17:17:59 +000050NoZeroDivCheck("mno-check-zero-division", cl::Hidden,
Akira Hatanaka1cb02422013-05-20 18:07:43 +000051 cl::desc("MIPS: Don't trap on integer division by zero."),
52 cl::init(false));
53
Reed Kotler720c5ca2014-04-17 22:15:34 +000054cl::opt<bool>
55EnableMipsFastISel("mips-fast-isel", cl::Hidden,
56 cl::desc("Allow mips-fast-isel to be used"),
57 cl::init(false));
58
Craig Topper840beec2014-04-04 05:16:06 +000059static const MCPhysReg O32IntRegs[4] = {
Akira Hatanakaac8c6692012-10-27 00:29:43 +000060 Mips::A0, Mips::A1, Mips::A2, Mips::A3
61};
62
Craig Topper840beec2014-04-04 05:16:06 +000063static const MCPhysReg Mips64IntRegs[8] = {
Akira Hatanakaac8c6692012-10-27 00:29:43 +000064 Mips::A0_64, Mips::A1_64, Mips::A2_64, Mips::A3_64,
65 Mips::T0_64, Mips::T1_64, Mips::T2_64, Mips::T3_64
66};
67
Craig Topper840beec2014-04-04 05:16:06 +000068static const MCPhysReg Mips64DPRegs[8] = {
Akira Hatanakaac8c6692012-10-27 00:29:43 +000069 Mips::D12_64, Mips::D13_64, Mips::D14_64, Mips::D15_64,
70 Mips::D16_64, Mips::D17_64, Mips::D18_64, Mips::D19_64
71};
72
Jia Liuf54f60f2012-02-28 07:46:26 +000073// If I is a shifted mask, set the size (Size) and the first bit of the
Akira Hatanaka73d78b72011-08-18 20:07:42 +000074// mask (Pos), and return true.
Jia Liuf54f60f2012-02-28 07:46:26 +000075// For example, if I is 0x003ff800, (Pos, Size) = (11, 11).
Akira Hatanaka0bb60d892013-03-12 00:16:36 +000076static bool isShiftedMask(uint64_t I, uint64_t &Pos, uint64_t &Size) {
Akira Hatanaka20cee2e2011-12-05 21:26:34 +000077 if (!isShiftedMask_64(I))
Akira Hatanaka4c0a7122013-10-07 19:33:02 +000078 return false;
Akira Hatanaka5360f882011-08-17 02:05:42 +000079
Akira Hatanaka20cee2e2011-12-05 21:26:34 +000080 Size = CountPopulation_64(I);
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +000081 Pos = countTrailingZeros(I);
Akira Hatanaka73d78b72011-08-18 20:07:42 +000082 return true;
Akira Hatanaka5360f882011-08-17 02:05:42 +000083}
84
Akira Hatanaka96ca1822013-03-13 00:54:29 +000085SDValue MipsTargetLowering::getGlobalReg(SelectionDAG &DAG, EVT Ty) const {
Akira Hatanakab049aef2012-02-24 22:34:47 +000086 MipsFunctionInfo *FI = DAG.getMachineFunction().getInfo<MipsFunctionInfo>();
87 return DAG.getRegister(FI->getGlobalBaseReg(), Ty);
88}
89
Akira Hatanakad8f10ce2013-09-27 19:51:35 +000090SDValue MipsTargetLowering::getTargetNode(GlobalAddressSDNode *N, EVT Ty,
91 SelectionDAG &DAG,
Akira Hatanaka96ca1822013-03-13 00:54:29 +000092 unsigned Flag) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +000093 return DAG.getTargetGlobalAddress(N->getGlobal(), SDLoc(N), Ty, 0, Flag);
Akira Hatanakafd04ad42012-11-21 20:26:38 +000094}
95
Akira Hatanakad8f10ce2013-09-27 19:51:35 +000096SDValue MipsTargetLowering::getTargetNode(ExternalSymbolSDNode *N, EVT Ty,
97 SelectionDAG &DAG,
98 unsigned Flag) const {
99 return DAG.getTargetExternalSymbol(N->getSymbol(), Ty, Flag);
100}
101
102SDValue MipsTargetLowering::getTargetNode(BlockAddressSDNode *N, EVT Ty,
103 SelectionDAG &DAG,
104 unsigned Flag) const {
105 return DAG.getTargetBlockAddress(N->getBlockAddress(), Ty, 0, Flag);
106}
107
108SDValue MipsTargetLowering::getTargetNode(JumpTableSDNode *N, EVT Ty,
109 SelectionDAG &DAG,
110 unsigned Flag) const {
111 return DAG.getTargetJumpTable(N->getIndex(), Ty, Flag);
112}
113
114SDValue MipsTargetLowering::getTargetNode(ConstantPoolSDNode *N, EVT Ty,
115 SelectionDAG &DAG,
116 unsigned Flag) const {
117 return DAG.getTargetConstantPool(N->getConstVal(), Ty, N->getAlignment(),
118 N->getOffset(), Flag);
Akira Hatanakafd04ad42012-11-21 20:26:38 +0000119}
120
Chris Lattner5e693ed2009-07-28 03:13:23 +0000121const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
122 switch (Opcode) {
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000123 case MipsISD::JmpLink: return "MipsISD::JmpLink";
Akira Hatanaka91318df2012-10-19 20:59:39 +0000124 case MipsISD::TailCall: return "MipsISD::TailCall";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000125 case MipsISD::Hi: return "MipsISD::Hi";
126 case MipsISD::Lo: return "MipsISD::Lo";
127 case MipsISD::GPRel: return "MipsISD::GPRel";
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +0000128 case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000129 case MipsISD::Ret: return "MipsISD::Ret";
Akira Hatanakac0b02062013-01-30 00:26:49 +0000130 case MipsISD::EH_RETURN: return "MipsISD::EH_RETURN";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000131 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
132 case MipsISD::FPCmp: return "MipsISD::FPCmp";
133 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
134 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000135 case MipsISD::TruncIntFP: return "MipsISD::TruncIntFP";
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000136 case MipsISD::MFHI: return "MipsISD::MFHI";
137 case MipsISD::MFLO: return "MipsISD::MFLO";
138 case MipsISD::MTLOHI: return "MipsISD::MTLOHI";
Akira Hatanaka28721bd2013-03-30 01:14:04 +0000139 case MipsISD::Mult: return "MipsISD::Mult";
140 case MipsISD::Multu: return "MipsISD::Multu";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000141 case MipsISD::MAdd: return "MipsISD::MAdd";
142 case MipsISD::MAddu: return "MipsISD::MAddu";
143 case MipsISD::MSub: return "MipsISD::MSub";
144 case MipsISD::MSubu: return "MipsISD::MSubu";
145 case MipsISD::DivRem: return "MipsISD::DivRem";
146 case MipsISD::DivRemU: return "MipsISD::DivRemU";
Akira Hatanaka28721bd2013-03-30 01:14:04 +0000147 case MipsISD::DivRem16: return "MipsISD::DivRem16";
148 case MipsISD::DivRemU16: return "MipsISD::DivRemU16";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000149 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
150 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
Akira Hatanakafaa88c02011-12-12 22:38:19 +0000151 case MipsISD::Wrapper: return "MipsISD::Wrapper";
Akira Hatanakaa4c09bc2011-07-19 23:30:50 +0000152 case MipsISD::Sync: return "MipsISD::Sync";
Akira Hatanaka5360f882011-08-17 02:05:42 +0000153 case MipsISD::Ext: return "MipsISD::Ext";
154 case MipsISD::Ins: return "MipsISD::Ins";
Akira Hatanakab9ebf8d2012-06-02 00:03:12 +0000155 case MipsISD::LWL: return "MipsISD::LWL";
156 case MipsISD::LWR: return "MipsISD::LWR";
157 case MipsISD::SWL: return "MipsISD::SWL";
158 case MipsISD::SWR: return "MipsISD::SWR";
159 case MipsISD::LDL: return "MipsISD::LDL";
160 case MipsISD::LDR: return "MipsISD::LDR";
161 case MipsISD::SDL: return "MipsISD::SDL";
162 case MipsISD::SDR: return "MipsISD::SDR";
Akira Hatanaka233ac532012-09-21 23:52:47 +0000163 case MipsISD::EXTP: return "MipsISD::EXTP";
164 case MipsISD::EXTPDP: return "MipsISD::EXTPDP";
165 case MipsISD::EXTR_S_H: return "MipsISD::EXTR_S_H";
166 case MipsISD::EXTR_W: return "MipsISD::EXTR_W";
167 case MipsISD::EXTR_R_W: return "MipsISD::EXTR_R_W";
168 case MipsISD::EXTR_RS_W: return "MipsISD::EXTR_RS_W";
169 case MipsISD::SHILO: return "MipsISD::SHILO";
170 case MipsISD::MTHLIP: return "MipsISD::MTHLIP";
171 case MipsISD::MULT: return "MipsISD::MULT";
172 case MipsISD::MULTU: return "MipsISD::MULTU";
Jia Liu434874d2013-03-04 01:06:54 +0000173 case MipsISD::MADD_DSP: return "MipsISD::MADD_DSP";
Akira Hatanaka233ac532012-09-21 23:52:47 +0000174 case MipsISD::MADDU_DSP: return "MipsISD::MADDU_DSP";
175 case MipsISD::MSUB_DSP: return "MipsISD::MSUB_DSP";
176 case MipsISD::MSUBU_DSP: return "MipsISD::MSUBU_DSP";
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000177 case MipsISD::SHLL_DSP: return "MipsISD::SHLL_DSP";
178 case MipsISD::SHRA_DSP: return "MipsISD::SHRA_DSP";
179 case MipsISD::SHRL_DSP: return "MipsISD::SHRL_DSP";
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000180 case MipsISD::SETCC_DSP: return "MipsISD::SETCC_DSP";
181 case MipsISD::SELECT_CC_DSP: return "MipsISD::SELECT_CC_DSP";
Daniel Sandersce09d072013-08-28 12:14:50 +0000182 case MipsISD::VALL_ZERO: return "MipsISD::VALL_ZERO";
183 case MipsISD::VANY_ZERO: return "MipsISD::VANY_ZERO";
184 case MipsISD::VALL_NONZERO: return "MipsISD::VALL_NONZERO";
185 case MipsISD::VANY_NONZERO: return "MipsISD::VANY_NONZERO";
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000186 case MipsISD::VCEQ: return "MipsISD::VCEQ";
187 case MipsISD::VCLE_S: return "MipsISD::VCLE_S";
188 case MipsISD::VCLE_U: return "MipsISD::VCLE_U";
189 case MipsISD::VCLT_S: return "MipsISD::VCLT_S";
190 case MipsISD::VCLT_U: return "MipsISD::VCLT_U";
Daniel Sanders3ce56622013-09-24 12:18:31 +0000191 case MipsISD::VSMAX: return "MipsISD::VSMAX";
192 case MipsISD::VSMIN: return "MipsISD::VSMIN";
193 case MipsISD::VUMAX: return "MipsISD::VUMAX";
194 case MipsISD::VUMIN: return "MipsISD::VUMIN";
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000195 case MipsISD::VEXTRACT_SEXT_ELT: return "MipsISD::VEXTRACT_SEXT_ELT";
196 case MipsISD::VEXTRACT_ZEXT_ELT: return "MipsISD::VEXTRACT_ZEXT_ELT";
Daniel Sandersf7456c72013-09-23 13:22:24 +0000197 case MipsISD::VNOR: return "MipsISD::VNOR";
Daniel Sanderse5087042013-09-24 14:02:15 +0000198 case MipsISD::VSHF: return "MipsISD::VSHF";
Daniel Sanders26307182013-09-24 14:20:00 +0000199 case MipsISD::SHF: return "MipsISD::SHF";
Daniel Sanders2ed228b2013-09-24 14:36:12 +0000200 case MipsISD::ILVEV: return "MipsISD::ILVEV";
201 case MipsISD::ILVOD: return "MipsISD::ILVOD";
202 case MipsISD::ILVL: return "MipsISD::ILVL";
203 case MipsISD::ILVR: return "MipsISD::ILVR";
Daniel Sandersfae5f2a2013-09-24 14:53:25 +0000204 case MipsISD::PCKEV: return "MipsISD::PCKEV";
205 case MipsISD::PCKOD: return "MipsISD::PCKOD";
Daniel Sandersb50ccf82014-04-01 10:35:28 +0000206 case MipsISD::INSVE: return "MipsISD::INSVE";
Craig Topper062a2ba2014-04-25 05:30:21 +0000207 default: return nullptr;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000208 }
209}
210
Daniel Sandersd897b562014-03-27 10:46:12 +0000211MipsTargetLowering::MipsTargetLowering(MipsTargetMachine &TM)
212 : TargetLowering(TM, new MipsTargetObjectFile()),
213 Subtarget(&TM.getSubtarget<MipsSubtarget>()) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000214 // Mips does not have i1 type, so use i32 for
Wesley Peck527da1b2010-11-23 03:31:01 +0000215 // setcc operations results (slt, sgt, ...).
Duncan Sands8d6e2e12008-11-23 15:47:28 +0000216 setBooleanContents(ZeroOrOneBooleanContent);
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000217 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000218
Wesley Peck527da1b2010-11-23 03:31:01 +0000219 // Load extented operations for i1 types must be promoted
Owen Anderson9f944592009-08-11 20:47:22 +0000220 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
221 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
222 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
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
Owen Anderson9f944592009-08-11 20:47:22 +0000225 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
226 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman39d6faa2009-07-17 02:28:12 +0000227
Wesley Peck527da1b2010-11-23 03:31:01 +0000228 // Used by legalize types to correctly generate the setcc result.
229 // Without this, every float setcc comes with a AND/OR with the result,
230 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +0000231 // which is used implicitly by brcond and select operations.
Owen Anderson9f944592009-08-11 20:47:22 +0000232 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +0000233
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000234 // Mips Custom Operations
Akira Hatanaka0f693a82013-03-06 21:32:03 +0000235 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000236 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000237 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000238 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
239 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
240 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
241 setOperationAction(ISD::SELECT, MVT::f32, Custom);
242 setOperationAction(ISD::SELECT, MVT::f64, Custom);
243 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Akira Hatanaka24cf4e32012-07-11 19:32:27 +0000244 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
245 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Akira Hatanakab7f78592012-03-09 23:46:03 +0000246 setOperationAction(ISD::SETCC, MVT::f32, Custom);
247 setOperationAction(ISD::SETCC, MVT::f64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000248 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +0000249 setOperationAction(ISD::VASTART, 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
Daniel Sanders3d849352014-04-14 15:44:42 +0000254 if (isGP64bit()) {
Akira Hatanakada00aa82012-03-10 00:03:50 +0000255 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
256 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
257 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
258 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
259 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
260 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Akira Hatanaka019e5922012-06-02 00:04:42 +0000261 setOperationAction(ISD::LOAD, MVT::i64, Custom);
262 setOperationAction(ISD::STORE, MVT::i64, Custom);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000263 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000264 }
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +0000265
Daniel Sanders3d849352014-04-14 15:44:42 +0000266 if (!isGP64bit()) {
Akira Hatanaka0a8ab712012-05-09 00:55:21 +0000267 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
268 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
269 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
270 }
271
Akira Hatanaka28e02ec2012-11-07 19:10:58 +0000272 setOperationAction(ISD::ADD, MVT::i32, Custom);
Daniel Sanders3d849352014-04-14 15:44:42 +0000273 if (isGP64bit())
Akira Hatanaka28e02ec2012-11-07 19:10:58 +0000274 setOperationAction(ISD::ADD, MVT::i64, Custom);
275
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000276 setOperationAction(ISD::SDIV, MVT::i32, Expand);
277 setOperationAction(ISD::SREM, MVT::i32, Expand);
278 setOperationAction(ISD::UDIV, MVT::i32, Expand);
279 setOperationAction(ISD::UREM, MVT::i32, Expand);
Akira Hatanakab1538f92011-10-03 21:06:13 +0000280 setOperationAction(ISD::SDIV, MVT::i64, Expand);
281 setOperationAction(ISD::SREM, MVT::i64, Expand);
282 setOperationAction(ISD::UDIV, MVT::i64, Expand);
283 setOperationAction(ISD::UREM, MVT::i64, Expand);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000284
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000285 // Operations not directly supported by Mips.
Tom Stellardb1588fc2013-03-08 15:36:57 +0000286 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
287 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
288 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
289 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000290 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
291 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Akira Hatanaka79aed152011-12-20 23:40:56 +0000292 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000293 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Akira Hatanaka79aed152011-12-20 23:40:56 +0000294 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000295 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Kai Nacke93fe5e82014-03-20 11:51:58 +0000296 if (Subtarget->hasCnMips()) {
297 setOperationAction(ISD::CTPOP, MVT::i32, Legal);
298 setOperationAction(ISD::CTPOP, MVT::i64, Legal);
299 } else {
300 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
301 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
302 }
Owen Anderson9f944592009-08-11 20:47:22 +0000303 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
Akira Hatanaka410ce9c2011-12-21 00:14:05 +0000304 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000305 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
306 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
307 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
308 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000309 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Akira Hatanaka7ba8a8d2011-09-30 18:51:46 +0000310 setOperationAction(ISD::ROTL, MVT::i64, Expand);
Akira Hatanaka33a25af2012-07-31 20:54:48 +0000311 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
312 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Bruno Cardoso Lopesd47180e2010-12-09 17:32:30 +0000313
Akira Hatanakabb49e722011-09-20 23:53:09 +0000314 if (!Subtarget->hasMips32r2())
Bruno Cardoso Lopesd47180e2010-12-09 17:32:30 +0000315 setOperationAction(ISD::ROTR, MVT::i32, Expand);
316
Akira Hatanaka7ba8a8d2011-09-30 18:51:46 +0000317 if (!Subtarget->hasMips64r2())
318 setOperationAction(ISD::ROTR, MVT::i64, Expand);
319
Owen Anderson9f944592009-08-11 20:47:22 +0000320 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes22b69db2011-03-04 18:54:14 +0000321 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000322 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes22b69db2011-03-04 18:54:14 +0000323 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000324 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
325 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000326 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
327 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanakadfb8cda2011-05-23 22:23:58 +0000328 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000329 setOperationAction(ISD::FLOG, MVT::f32, Expand);
330 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
331 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
332 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000333 setOperationAction(ISD::FMA, MVT::f32, Expand);
334 setOperationAction(ISD::FMA, MVT::f64, Expand);
Akira Hatanaka0603ad82012-03-29 18:43:11 +0000335 setOperationAction(ISD::FREM, MVT::f32, Expand);
336 setOperationAction(ISD::FREM, MVT::f64, Expand);
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000337
Akira Hatanakac0b02062013-01-30 00:26:49 +0000338 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
339
Bruno Cardoso Lopes048ffab2011-03-09 19:22:22 +0000340 setOperationAction(ISD::VAARG, MVT::Other, Expand);
341 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
342 setOperationAction(ISD::VAEND, MVT::Other, Expand);
343
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000344 // Use the default for now
Owen Anderson9f944592009-08-11 20:47:22 +0000345 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
346 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman26a48482011-07-27 22:21:52 +0000347
Jia Liuf54f60f2012-02-28 07:46:26 +0000348 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
349 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
350 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
351 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
Eli Friedman7dfa7912011-08-29 18:23:02 +0000352
Eli Friedman30a49e92011-08-03 21:06:02 +0000353 setInsertFencesForAtomic(true);
354
Daniel Sandersfcea8102014-05-12 12:28:15 +0000355 if (!Subtarget->hasMips32r2()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000356 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
357 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000358 }
359
Akira Hatanaka1d8efab2011-12-21 00:20:27 +0000360 if (!Subtarget->hasBitCount()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000361 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Akira Hatanaka1d8efab2011-12-21 00:20:27 +0000362 setOperationAction(ISD::CTLZ, MVT::i64, Expand);
363 }
Bruno Cardoso Lopes93da7e62008-08-08 06:16:31 +0000364
Daniel Sanders39d00512014-05-12 12:15:41 +0000365 if (!Subtarget->hasMips32r2())
Owen Anderson9f944592009-08-11 20:47:22 +0000366 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Daniel Sanders39d00512014-05-12 12:15:41 +0000367 if (!Subtarget->hasMips64r2())
Akira Hatanaka4706ac92011-12-20 23:56:43 +0000368 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000369
Daniel Sanders3d849352014-04-14 15:44:42 +0000370 if (isGP64bit()) {
Akira Hatanaka019e5922012-06-02 00:04:42 +0000371 setLoadExtAction(ISD::SEXTLOAD, MVT::i32, Custom);
372 setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, Custom);
373 setLoadExtAction(ISD::EXTLOAD, MVT::i32, Custom);
374 setTruncStoreAction(MVT::i64, MVT::i32, Custom);
375 }
376
Akira Hatanakaa3d9ab92013-07-26 20:58:55 +0000377 setOperationAction(ISD::TRAP, MVT::Other, Legal);
378
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000379 setTargetDAGCombine(ISD::SDIVREM);
380 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka5e152182012-03-08 03:26:37 +0000381 setTargetDAGCombine(ISD::SELECT);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000382 setTargetDAGCombine(ISD::AND);
383 setTargetDAGCombine(ISD::OR);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000384 setTargetDAGCombine(ISD::ADD);
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000385
Daniel Sanders3d849352014-04-14 15:44:42 +0000386 setMinFunctionAlignment(isGP64bit() ? 3 : 2);
Eli Friedman2518f832011-05-06 20:34:06 +0000387
Daniel Sandersd897b562014-03-27 10:46:12 +0000388 setStackPointerRegisterToSaveRestore(isN64() ? Mips::SP_64 : Mips::SP);
Akira Hatanakaaa560002011-05-26 18:59:03 +0000389
Daniel Sandersd897b562014-03-27 10:46:12 +0000390 setExceptionPointerRegister(isN64() ? Mips::A0_64 : Mips::A0);
391 setExceptionSelectorRegister(isN64() ? Mips::A1_64 : Mips::A1);
Akira Hatanaka1daf8c22012-06-13 19:33:32 +0000392
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000393 MaxStoresPerMemcpy = 16;
Zoran Jovanovicff9d5f32013-12-19 16:12:56 +0000394
395 isMicroMips = Subtarget->inMicroMipsMode();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000396}
397
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000398const MipsTargetLowering *MipsTargetLowering::create(MipsTargetMachine &TM) {
399 if (TM.getSubtargetImpl()->inMips16Mode())
400 return llvm::createMips16TargetLowering(TM);
Jia Liuf54f60f2012-02-28 07:46:26 +0000401
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000402 return llvm::createMipsSETargetLowering(TM);
Akira Hatanaka2fcc1cf2011-08-12 21:30:06 +0000403}
404
Reed Kotler720c5ca2014-04-17 22:15:34 +0000405// Create a fast isel object.
406FastISel *
407MipsTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
408 const TargetLibraryInfo *libInfo) const {
409 if (!EnableMipsFastISel)
410 return TargetLowering::createFastISel(funcInfo, libInfo);
411 return Mips::createFastISel(funcInfo, libInfo);
412}
413
Matt Arsenault758659232013-05-18 00:21:46 +0000414EVT MipsTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Akira Hatanakab13b3332013-01-04 20:06:01 +0000415 if (!VT.isVector())
416 return MVT::i32;
417 return VT.changeVectorElementTypeToInteger();
Scott Michela6729e82008-03-10 15:42:14 +0000418}
419
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000420static SDValue performDivRemCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000421 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000422 const MipsSubtarget *Subtarget) {
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000423 if (DCI.isBeforeLegalizeOps())
424 return SDValue();
425
Akira Hatanakab1538f92011-10-03 21:06:13 +0000426 EVT Ty = N->getValueType(0);
Akira Hatanaka8002a3f2013-08-14 00:47:08 +0000427 unsigned LO = (Ty == MVT::i32) ? Mips::LO0 : Mips::LO0_64;
428 unsigned HI = (Ty == MVT::i32) ? Mips::HI0 : Mips::HI0_64;
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000429 unsigned Opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem16 :
430 MipsISD::DivRemU16;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000431 SDLoc DL(N);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000432
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000433 SDValue DivRem = DAG.getNode(Opc, DL, MVT::Glue,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000434 N->getOperand(0), N->getOperand(1));
435 SDValue InChain = DAG.getEntryNode();
436 SDValue InGlue = DivRem;
437
438 // insert MFLO
439 if (N->hasAnyUseOfValue(0)) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000440 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, DL, LO, Ty,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000441 InGlue);
442 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
443 InChain = CopyFromLo.getValue(1);
444 InGlue = CopyFromLo.getValue(2);
445 }
446
447 // insert MFHI
448 if (N->hasAnyUseOfValue(1)) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000449 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, DL,
Akira Hatanakab1538f92011-10-03 21:06:13 +0000450 HI, Ty, InGlue);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000451 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
452 }
453
454 return SDValue();
455}
456
Akira Hatanaka89af5892013-04-18 01:00:46 +0000457static Mips::CondCode condCodeToFCC(ISD::CondCode CC) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000458 switch (CC) {
459 default: llvm_unreachable("Unknown fp condition code!");
460 case ISD::SETEQ:
461 case ISD::SETOEQ: return Mips::FCOND_OEQ;
462 case ISD::SETUNE: return Mips::FCOND_UNE;
463 case ISD::SETLT:
464 case ISD::SETOLT: return Mips::FCOND_OLT;
465 case ISD::SETGT:
466 case ISD::SETOGT: return Mips::FCOND_OGT;
467 case ISD::SETLE:
468 case ISD::SETOLE: return Mips::FCOND_OLE;
469 case ISD::SETGE:
470 case ISD::SETOGE: return Mips::FCOND_OGE;
471 case ISD::SETULT: return Mips::FCOND_ULT;
472 case ISD::SETULE: return Mips::FCOND_ULE;
473 case ISD::SETUGT: return Mips::FCOND_UGT;
474 case ISD::SETUGE: return Mips::FCOND_UGE;
475 case ISD::SETUO: return Mips::FCOND_UN;
476 case ISD::SETO: return Mips::FCOND_OR;
477 case ISD::SETNE:
478 case ISD::SETONE: return Mips::FCOND_ONE;
479 case ISD::SETUEQ: return Mips::FCOND_UEQ;
480 }
481}
482
483
Akira Hatanakaf0ea5002013-03-30 01:16:38 +0000484/// This function returns true if the floating point conditional branches and
485/// conditional moves which use condition code CC should be inverted.
486static bool invertFPCondCodeUser(Mips::CondCode CC) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000487 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
488 return false;
489
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000490 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
491 "Illegal Condition Code");
Akira Hatanakaa5352702011-03-31 18:26:17 +0000492
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000493 return true;
Akira Hatanakaa5352702011-03-31 18:26:17 +0000494}
495
496// Creates and returns an FPCmp node from a setcc node.
497// Returns Op if setcc is not a floating point comparison.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000498static SDValue createFPCmp(SelectionDAG &DAG, const SDValue &Op) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000499 // must be a SETCC node
500 if (Op.getOpcode() != ISD::SETCC)
501 return Op;
502
503 SDValue LHS = Op.getOperand(0);
504
505 if (!LHS.getValueType().isFloatingPoint())
506 return Op;
507
508 SDValue RHS = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000509 SDLoc DL(Op);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000510
Akira Hatanakaaef55c82011-04-15 21:00:26 +0000511 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
512 // node if necessary.
Akira Hatanakaa5352702011-03-31 18:26:17 +0000513 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
514
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000515 return DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS,
Akira Hatanaka89af5892013-04-18 01:00:46 +0000516 DAG.getConstant(condCodeToFCC(CC), MVT::i32));
Akira Hatanakaa5352702011-03-31 18:26:17 +0000517}
518
519// Creates and returns a CMovFPT/F node.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000520static SDValue createCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000521 SDValue False, SDLoc DL) {
Akira Hatanakaf0ea5002013-03-30 01:16:38 +0000522 ConstantSDNode *CC = cast<ConstantSDNode>(Cond.getOperand(2));
523 bool invert = invertFPCondCodeUser((Mips::CondCode)CC->getSExtValue());
Akira Hatanaka8bce21c2013-07-26 20:51:20 +0000524 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000525
526 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
Akira Hatanaka8bce21c2013-07-26 20:51:20 +0000527 True.getValueType(), True, FCC0, False, Cond);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000528}
529
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000530static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000531 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000532 const MipsSubtarget *Subtarget) {
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000533 if (DCI.isBeforeLegalizeOps())
534 return SDValue();
535
536 SDValue SetCC = N->getOperand(0);
537
538 if ((SetCC.getOpcode() != ISD::SETCC) ||
539 !SetCC.getOperand(0).getValueType().isInteger())
540 return SDValue();
541
542 SDValue False = N->getOperand(2);
543 EVT FalseTy = False.getValueType();
544
545 if (!FalseTy.isInteger())
546 return SDValue();
547
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000548 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(False);
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000549
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000550 // If the RHS (False) is 0, we swap the order of the operands
551 // of ISD::SELECT (obviously also inverting the condition) so that we can
552 // take advantage of conditional moves using the $0 register.
553 // Example:
554 // return (a != 0) ? x : 0;
555 // load $reg, x
556 // movz $reg, $0, a
557 if (!FalseC)
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000558 return SDValue();
559
Andrew Trickef9de2a2013-05-25 02:42:55 +0000560 const SDLoc DL(N);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000561
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000562 if (!FalseC->getZExtValue()) {
563 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
564 SDValue True = N->getOperand(1);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000565
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000566 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
567 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
568
569 return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True);
570 }
571
Matheus Almeidaa6beac12013-12-05 12:07:05 +0000572 // If both operands are integer constants there's a possibility that we
573 // can do some interesting optimizations.
574 SDValue True = N->getOperand(1);
575 ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(True);
576
577 if (!TrueC || !True.getValueType().isInteger())
578 return SDValue();
579
580 // We'll also ignore MVT::i64 operands as this optimizations proves
581 // to be ineffective because of the required sign extensions as the result
582 // of a SETCC operator is always MVT::i32 for non-vector types.
583 if (True.getValueType() == MVT::i64)
584 return SDValue();
585
586 int64_t Diff = TrueC->getSExtValue() - FalseC->getSExtValue();
587
588 // 1) (a < x) ? y : y-1
589 // slti $reg1, a, x
590 // addiu $reg2, $reg1, y-1
591 if (Diff == 1)
592 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, False);
593
594 // 2) (a < x) ? y-1 : y
595 // slti $reg1, a, x
596 // xor $reg1, $reg1, 1
597 // addiu $reg2, $reg1, y-1
598 if (Diff == -1) {
599 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
600 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
601 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
602 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, True);
603 }
604
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000605 // Couldn't optimize.
606 return SDValue();
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000607}
608
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000609static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000610 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000611 const MipsSubtarget *Subtarget) {
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000612 // Pattern match EXT.
613 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
614 // => ext $dst, $src, size, pos
Akira Hatanaka4a3836b2013-10-09 23:36:17 +0000615 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasExtractInsert())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000616 return SDValue();
617
618 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000619 unsigned ShiftRightOpc = ShiftRight.getOpcode();
620
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000621 // Op's first operand must be a shift right.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000622 if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL)
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000623 return SDValue();
624
625 // The second operand of the shift must be an immediate.
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000626 ConstantSDNode *CN;
627 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
628 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000629
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000630 uint64_t Pos = CN->getZExtValue();
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000631 uint64_t SMPos, SMSize;
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000632
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000633 // Op's second operand must be a shifted mask.
634 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000635 !isShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000636 return SDValue();
637
638 // Return if the shifted mask does not start at bit 0 or the sum of its size
639 // and Pos exceeds the word's size.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000640 EVT ValTy = N->getValueType(0);
641 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000642 return SDValue();
643
Andrew Trickef9de2a2013-05-25 02:42:55 +0000644 return DAG.getNode(MipsISD::Ext, SDLoc(N), ValTy,
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000645 ShiftRight.getOperand(0), DAG.getConstant(Pos, MVT::i32),
Akira Hatanakaeea541c2011-08-17 22:59:46 +0000646 DAG.getConstant(SMSize, MVT::i32));
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000647}
Jia Liuf54f60f2012-02-28 07:46:26 +0000648
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000649static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000650 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000651 const MipsSubtarget *Subtarget) {
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000652 // Pattern match INS.
653 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
Jia Liuf54f60f2012-02-28 07:46:26 +0000654 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000655 // => ins $dst, $src, size, pos, $src1
Akira Hatanaka4a3836b2013-10-09 23:36:17 +0000656 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasExtractInsert())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000657 return SDValue();
658
659 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
660 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
661 ConstantSDNode *CN;
662
663 // See if Op's first operand matches (and $src1 , mask0).
664 if (And0.getOpcode() != ISD::AND)
665 return SDValue();
666
667 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000668 !isShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000669 return SDValue();
670
671 // See if Op's second operand matches (and (shl $src, pos), mask1).
672 if (And1.getOpcode() != ISD::AND)
673 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000674
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000675 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000676 !isShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000677 return SDValue();
678
679 // The shift masks must have the same position and size.
680 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
681 return SDValue();
682
683 SDValue Shl = And1.getOperand(0);
684 if (Shl.getOpcode() != ISD::SHL)
685 return SDValue();
686
687 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
688 return SDValue();
689
690 unsigned Shamt = CN->getZExtValue();
691
692 // Return if the shift amount and the first bit position of mask are not the
Jia Liuf54f60f2012-02-28 07:46:26 +0000693 // same.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000694 EVT ValTy = N->getValueType(0);
695 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000696 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000697
Andrew Trickef9de2a2013-05-25 02:42:55 +0000698 return DAG.getNode(MipsISD::Ins, SDLoc(N), ValTy, Shl.getOperand(0),
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000699 DAG.getConstant(SMPos0, MVT::i32),
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000700 DAG.getConstant(SMSize0, MVT::i32), And0.getOperand(0));
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000701}
Jia Liuf54f60f2012-02-28 07:46:26 +0000702
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000703static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000704 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000705 const MipsSubtarget *Subtarget) {
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000706 // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt))
707
708 if (DCI.isBeforeLegalizeOps())
709 return SDValue();
710
711 SDValue Add = N->getOperand(1);
712
713 if (Add.getOpcode() != ISD::ADD)
714 return SDValue();
715
716 SDValue Lo = Add.getOperand(1);
717
718 if ((Lo.getOpcode() != MipsISD::Lo) ||
719 (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable))
720 return SDValue();
721
722 EVT ValTy = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000723 SDLoc DL(N);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000724
725 SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, N->getOperand(0),
726 Add.getOperand(0));
727 return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo);
728}
729
Bruno Cardoso Lopes61a61e92011-02-10 18:05:10 +0000730SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000731 const {
732 SelectionDAG &DAG = DCI.DAG;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000733 unsigned Opc = N->getOpcode();
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000734
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000735 switch (Opc) {
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000736 default: break;
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000737 case ISD::SDIVREM:
738 case ISD::UDIVREM:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000739 return performDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000740 case ISD::SELECT:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000741 return performSELECTCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000742 case ISD::AND:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000743 return performANDCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000744 case ISD::OR:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000745 return performORCombine(N, DAG, DCI, Subtarget);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000746 case ISD::ADD:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000747 return performADDCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000748 }
749
750 return SDValue();
751}
752
Akira Hatanakafabb8cf2012-09-21 23:58:31 +0000753void
754MipsTargetLowering::LowerOperationWrapper(SDNode *N,
755 SmallVectorImpl<SDValue> &Results,
756 SelectionDAG &DAG) const {
757 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
758
759 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
760 Results.push_back(Res.getValue(I));
761}
762
763void
764MipsTargetLowering::ReplaceNodeResults(SDNode *N,
765 SmallVectorImpl<SDValue> &Results,
766 SelectionDAG &DAG) const {
Akira Hatanaka9da442f2013-04-30 21:17:07 +0000767 return LowerOperationWrapper(N, Results, DAG);
Akira Hatanakafabb8cf2012-09-21 23:58:31 +0000768}
769
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000770SDValue MipsTargetLowering::
Dan Gohman21cea8a2010-04-17 15:26:15 +0000771LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000772{
Wesley Peck527da1b2010-11-23 03:31:01 +0000773 switch (Op.getOpcode())
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000774 {
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000775 case ISD::BR_JT: return lowerBR_JT(Op, DAG);
776 case ISD::BRCOND: return lowerBRCOND(Op, DAG);
777 case ISD::ConstantPool: return lowerConstantPool(Op, DAG);
778 case ISD::GlobalAddress: return lowerGlobalAddress(Op, DAG);
779 case ISD::BlockAddress: return lowerBlockAddress(Op, DAG);
780 case ISD::GlobalTLSAddress: return lowerGlobalTLSAddress(Op, DAG);
781 case ISD::JumpTable: return lowerJumpTable(Op, DAG);
782 case ISD::SELECT: return lowerSELECT(Op, DAG);
783 case ISD::SELECT_CC: return lowerSELECT_CC(Op, DAG);
784 case ISD::SETCC: return lowerSETCC(Op, DAG);
785 case ISD::VASTART: return lowerVASTART(Op, DAG);
786 case ISD::FCOPYSIGN: return lowerFCOPYSIGN(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000787 case ISD::FRAMEADDR: return lowerFRAMEADDR(Op, DAG);
788 case ISD::RETURNADDR: return lowerRETURNADDR(Op, DAG);
789 case ISD::EH_RETURN: return lowerEH_RETURN(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000790 case ISD::ATOMIC_FENCE: return lowerATOMIC_FENCE(Op, DAG);
791 case ISD::SHL_PARTS: return lowerShiftLeftParts(Op, DAG);
792 case ISD::SRA_PARTS: return lowerShiftRightParts(Op, DAG, true);
793 case ISD::SRL_PARTS: return lowerShiftRightParts(Op, DAG, false);
794 case ISD::LOAD: return lowerLOAD(Op, DAG);
795 case ISD::STORE: return lowerSTORE(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000796 case ISD::ADD: return lowerADD(Op, DAG);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000797 case ISD::FP_TO_SINT: return lowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000798 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000799 return SDValue();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000800}
801
Akira Hatanakae2489122011-04-15 21:51:11 +0000802//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000803// Lower helper functions
Akira Hatanakae2489122011-04-15 21:51:11 +0000804//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000805
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000806// addLiveIn - This helper function adds the specified physical register to the
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000807// MachineFunction as a live in value. It also creates a corresponding
808// virtual register for it.
809static unsigned
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000810addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000811{
Chris Lattnera10fff52007-12-31 04:13:23 +0000812 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
813 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000814 return VReg;
815}
816
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000817static MachineBasicBlock *expandPseudoDIV(MachineInstr *MI,
818 MachineBasicBlock &MBB,
819 const TargetInstrInfo &TII,
820 bool Is64Bit) {
821 if (NoZeroDivCheck)
822 return &MBB;
823
824 // Insert instruction "teq $divisor_reg, $zero, 7".
825 MachineBasicBlock::iterator I(MI);
826 MachineInstrBuilder MIB;
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000827 MachineOperand &Divisor = MI->getOperand(2);
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000828 MIB = BuildMI(MBB, std::next(I), MI->getDebugLoc(), TII.get(Mips::TEQ))
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000829 .addReg(Divisor.getReg(), getKillRegState(Divisor.isKill()))
830 .addReg(Mips::ZERO).addImm(7);
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000831
832 // Use the 32-bit sub-register if this is a 64-bit division.
833 if (Is64Bit)
834 MIB->getOperand(0).setSubReg(Mips::sub_32);
835
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000836 // Clear Divisor's kill flag.
837 Divisor.setIsKill(false);
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000838 return &MBB;
839}
840
Akira Hatanakae4bd0542012-09-27 02:15:57 +0000841MachineBasicBlock *
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +0000842MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +0000843 MachineBasicBlock *BB) const {
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +0000844 switch (MI->getOpcode()) {
Reed Kotler97ba5f22013-02-21 04:22:38 +0000845 default:
846 llvm_unreachable("Unexpected instr type to insert");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000847 case Mips::ATOMIC_LOAD_ADD_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000848 return emitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000849 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000850 return emitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000851 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000852 return emitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000853 case Mips::ATOMIC_LOAD_ADD_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000854 return emitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000855
856 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000857 return emitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000858 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000859 return emitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000860 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000861 return emitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000862 case Mips::ATOMIC_LOAD_AND_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000863 return emitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000864
865 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000866 return emitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000867 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000868 return emitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000869 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000870 return emitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000871 case Mips::ATOMIC_LOAD_OR_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000872 return emitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000873
874 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000875 return emitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000876 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000877 return emitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000878 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000879 return emitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000880 case Mips::ATOMIC_LOAD_XOR_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000881 return emitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000882
883 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000884 return emitAtomicBinaryPartword(MI, BB, 1, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000885 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000886 return emitAtomicBinaryPartword(MI, BB, 2, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000887 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000888 return emitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000889 case Mips::ATOMIC_LOAD_NAND_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000890 return emitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000891
892 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000893 return emitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000894 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000895 return emitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000896 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000897 return emitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000898 case Mips::ATOMIC_LOAD_SUB_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000899 return emitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000900
901 case Mips::ATOMIC_SWAP_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000902 return emitAtomicBinaryPartword(MI, BB, 1, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000903 case Mips::ATOMIC_SWAP_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000904 return emitAtomicBinaryPartword(MI, BB, 2, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000905 case Mips::ATOMIC_SWAP_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000906 return emitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000907 case Mips::ATOMIC_SWAP_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000908 return emitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000909
910 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000911 return emitAtomicCmpSwapPartword(MI, BB, 1);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000912 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000913 return emitAtomicCmpSwapPartword(MI, BB, 2);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000914 case Mips::ATOMIC_CMP_SWAP_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000915 return emitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000916 case Mips::ATOMIC_CMP_SWAP_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000917 return emitAtomicCmpSwap(MI, BB, 8);
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000918 case Mips::PseudoSDIV:
919 case Mips::PseudoUDIV:
920 return expandPseudoDIV(MI, *BB, *getTargetMachine().getInstrInfo(), false);
921 case Mips::PseudoDSDIV:
922 case Mips::PseudoDUDIV:
923 return expandPseudoDIV(MI, *BB, *getTargetMachine().getInstrInfo(), true);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000924 }
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +0000925}
926
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000927// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
928// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
929MachineBasicBlock *
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000930MipsTargetLowering::emitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher0713a9d2011-06-08 23:55:35 +0000931 unsigned Size, unsigned BinOpcode,
Akira Hatanaka15506782011-06-07 18:58:42 +0000932 bool Nand) const {
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000933 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000934
935 MachineFunction *MF = BB->getParent();
936 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000937 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000938 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000939 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000940 unsigned LL, SC, AND, NOR, ZERO, BEQ;
941
942 if (Size == 4) {
Zoran Jovanovicff9d5f32013-12-19 16:12:56 +0000943 LL = isMicroMips ? Mips::LL_MM : Mips::LL;
944 SC = isMicroMips ? Mips::SC_MM : Mips::SC;
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000945 AND = Mips::AND;
946 NOR = Mips::NOR;
947 ZERO = Mips::ZERO;
948 BEQ = Mips::BEQ;
949 }
950 else {
Akira Hatanaka6781fc12013-08-20 21:08:22 +0000951 LL = Mips::LLD;
952 SC = Mips::SCD;
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000953 AND = Mips::AND64;
954 NOR = Mips::NOR64;
955 ZERO = Mips::ZERO_64;
956 BEQ = Mips::BEQ64;
957 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000958
Akira Hatanaka0e019592011-07-19 20:11:17 +0000959 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000960 unsigned Ptr = MI->getOperand(1).getReg();
961 unsigned Incr = MI->getOperand(2).getReg();
962
Akira Hatanaka0e019592011-07-19 20:11:17 +0000963 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
964 unsigned AndRes = RegInfo.createVirtualRegister(RC);
965 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000966
967 // insert new blocks after the current block
968 const BasicBlock *LLVM_BB = BB->getBasicBlock();
969 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
970 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
971 MachineFunction::iterator It = BB;
972 ++It;
973 MF->insert(It, loopMBB);
974 MF->insert(It, exitMBB);
975
976 // Transfer the remainder of BB and its successor edges to exitMBB.
977 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000978 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000979 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
980
981 // thisMBB:
982 // ...
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000983 // fallthrough --> loopMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000984 BB->addSuccessor(loopMBB);
Akira Hatanaka08636b42011-07-19 17:09:53 +0000985 loopMBB->addSuccessor(loopMBB);
986 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000987
988 // loopMBB:
989 // ll oldval, 0(ptr)
Akira Hatanaka0e019592011-07-19 20:11:17 +0000990 // <binop> storeval, oldval, incr
991 // sc success, storeval, 0(ptr)
992 // beq success, $0, loopMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000993 BB = loopMBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000994 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000995 if (Nand) {
Akira Hatanaka0e019592011-07-19 20:11:17 +0000996 // and andres, oldval, incr
997 // nor storeval, $0, andres
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000998 BuildMI(BB, DL, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
999 BuildMI(BB, DL, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001000 } else if (BinOpcode) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001001 // <binop> storeval, oldval, incr
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001002 BuildMI(BB, DL, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001003 } else {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001004 StoreVal = Incr;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001005 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001006 BuildMI(BB, DL, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
1007 BuildMI(BB, DL, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001008
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001009 MI->eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001010
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001011 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001012}
1013
1014MachineBasicBlock *
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001015MipsTargetLowering::emitAtomicBinaryPartword(MachineInstr *MI,
Akira Hatanaka15506782011-06-07 18:58:42 +00001016 MachineBasicBlock *BB,
1017 unsigned Size, unsigned BinOpcode,
1018 bool Nand) const {
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001019 assert((Size == 1 || Size == 2) &&
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001020 "Unsupported size for EmitAtomicBinaryPartial.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001021
1022 MachineFunction *MF = BB->getParent();
1023 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1024 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1025 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001026 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001027
1028 unsigned Dest = MI->getOperand(0).getReg();
1029 unsigned Ptr = MI->getOperand(1).getReg();
1030 unsigned Incr = MI->getOperand(2).getReg();
1031
Akira Hatanaka0e019592011-07-19 20:11:17 +00001032 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1033 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001034 unsigned Mask = RegInfo.createVirtualRegister(RC);
1035 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001036 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1037 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001038 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001039 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1040 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1041 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1042 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1043 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka9663dd32011-07-19 20:56:53 +00001044 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001045 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1046 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1047 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1048 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1049 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001050
1051 // insert new blocks after the current block
1052 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1053 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001054 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001055 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1056 MachineFunction::iterator It = BB;
1057 ++It;
1058 MF->insert(It, loopMBB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001059 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001060 MF->insert(It, exitMBB);
1061
1062 // Transfer the remainder of BB and its successor edges to exitMBB.
1063 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001064 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001065 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1066
Akira Hatanaka08636b42011-07-19 17:09:53 +00001067 BB->addSuccessor(loopMBB);
1068 loopMBB->addSuccessor(loopMBB);
1069 loopMBB->addSuccessor(sinkMBB);
1070 sinkMBB->addSuccessor(exitMBB);
1071
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001072 // thisMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001073 // addiu masklsb2,$0,-4 # 0xfffffffc
1074 // and alignedaddr,ptr,masklsb2
1075 // andi ptrlsb2,ptr,3
1076 // sll shiftamt,ptrlsb2,3
1077 // ori maskupper,$0,255 # 0xff
1078 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001079 // nor mask2,$0,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001080 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001081
1082 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001083 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001084 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001085 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001086 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001087 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Akira Hatanaka2bf97332013-05-31 03:25:44 +00001088 if (Subtarget->isLittle()) {
1089 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1090 } else {
1091 unsigned Off = RegInfo.createVirtualRegister(RC);
1092 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1093 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1094 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1095 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001096 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001097 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001098 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001099 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001100 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001101 BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(Incr).addReg(ShiftAmt);
Bruno Cardoso Lopesf771a0f2011-05-31 20:25:26 +00001102
Akira Hatanaka27292632011-07-18 18:52:12 +00001103 // atomic.load.binop
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001104 // loopMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001105 // ll oldval,0(alignedaddr)
1106 // binop binopres,oldval,incr2
1107 // and newval,binopres,mask
1108 // and maskedoldval0,oldval,mask2
1109 // or storeval,maskedoldval0,newval
1110 // sc success,storeval,0(alignedaddr)
1111 // beq success,$0,loopMBB
1112
Akira Hatanaka27292632011-07-18 18:52:12 +00001113 // atomic.swap
1114 // loopMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001115 // ll oldval,0(alignedaddr)
Akira Hatanakae4503582011-07-19 18:14:26 +00001116 // and newval,incr2,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001117 // and maskedoldval0,oldval,mask2
1118 // or storeval,maskedoldval0,newval
1119 // sc success,storeval,0(alignedaddr)
1120 // beq success,$0,loopMBB
Akira Hatanaka27292632011-07-18 18:52:12 +00001121
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001122 BB = loopMBB;
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001123 BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001124 if (Nand) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001125 // and andres, oldval, incr2
1126 // nor binopres, $0, andres
1127 // and newval, binopres, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001128 BuildMI(BB, DL, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1129 BuildMI(BB, DL, TII->get(Mips::NOR), BinOpRes)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001130 .addReg(Mips::ZERO).addReg(AndRes);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001131 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001132 } else if (BinOpcode) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001133 // <binop> binopres, oldval, incr2
1134 // and newval, binopres, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001135 BuildMI(BB, DL, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1136 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001137 } else { // atomic.swap
Akira Hatanaka0e019592011-07-19 20:11:17 +00001138 // and newval, incr2, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001139 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanakae4503582011-07-19 18:14:26 +00001140 }
Jia Liuf54f60f2012-02-28 07:46:26 +00001141
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001142 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001143 .addReg(OldVal).addReg(Mask2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001144 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka9663dd32011-07-19 20:56:53 +00001145 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001146 BuildMI(BB, DL, TII->get(Mips::SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001147 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001148 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001149 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001150
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001151 // sinkMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001152 // and maskedoldval1,oldval,mask
1153 // srl srlres,maskedoldval1,shiftamt
1154 // sll sllres,srlres,24
1155 // sra dest,sllres,24
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001156 BB = sinkMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001157 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakae97bd812011-07-19 03:14:58 +00001158
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001159 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001160 .addReg(OldVal).addReg(Mask);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001161 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001162 .addReg(MaskedOldVal1).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001163 BuildMI(BB, DL, TII->get(Mips::SLL), SllRes)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001164 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001165 BuildMI(BB, DL, TII->get(Mips::SRA), Dest)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001166 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001167
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001168 MI->eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001169
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001170 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001171}
1172
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001173MachineBasicBlock * MipsTargetLowering::emitAtomicCmpSwap(MachineInstr *MI,
1174 MachineBasicBlock *BB,
1175 unsigned Size) const {
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001176 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001177
1178 MachineFunction *MF = BB->getParent();
1179 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001180 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001181 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001182 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001183 unsigned LL, SC, ZERO, BNE, BEQ;
1184
1185 if (Size == 4) {
Zoran Jovanovicff9d5f32013-12-19 16:12:56 +00001186 LL = isMicroMips ? Mips::LL_MM : Mips::LL;
1187 SC = isMicroMips ? Mips::SC_MM : Mips::SC;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001188 ZERO = Mips::ZERO;
1189 BNE = Mips::BNE;
1190 BEQ = Mips::BEQ;
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001191 } else {
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001192 LL = Mips::LLD;
1193 SC = Mips::SCD;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001194 ZERO = Mips::ZERO_64;
1195 BNE = Mips::BNE64;
1196 BEQ = Mips::BEQ64;
1197 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001198
1199 unsigned Dest = MI->getOperand(0).getReg();
1200 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka0e019592011-07-19 20:11:17 +00001201 unsigned OldVal = MI->getOperand(2).getReg();
1202 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001203
Akira Hatanaka0e019592011-07-19 20:11:17 +00001204 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001205
1206 // insert new blocks after the current block
1207 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1208 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1209 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1210 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1211 MachineFunction::iterator It = BB;
1212 ++It;
1213 MF->insert(It, loop1MBB);
1214 MF->insert(It, loop2MBB);
1215 MF->insert(It, exitMBB);
1216
1217 // Transfer the remainder of BB and its successor edges to exitMBB.
1218 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001219 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001220 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1221
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001222 // thisMBB:
1223 // ...
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001224 // fallthrough --> loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001225 BB->addSuccessor(loop1MBB);
Akira Hatanaka08636b42011-07-19 17:09:53 +00001226 loop1MBB->addSuccessor(exitMBB);
1227 loop1MBB->addSuccessor(loop2MBB);
1228 loop2MBB->addSuccessor(loop1MBB);
1229 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001230
1231 // loop1MBB:
1232 // ll dest, 0(ptr)
1233 // bne dest, oldval, exitMBB
1234 BB = loop1MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001235 BuildMI(BB, DL, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1236 BuildMI(BB, DL, TII->get(BNE))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001237 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001238
1239 // loop2MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001240 // sc success, newval, 0(ptr)
1241 // beq success, $0, loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001242 BB = loop2MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001243 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001244 .addReg(NewVal).addReg(Ptr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001245 BuildMI(BB, DL, TII->get(BEQ))
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001246 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001247
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001248 MI->eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001249
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001250 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001251}
1252
1253MachineBasicBlock *
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001254MipsTargetLowering::emitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka15506782011-06-07 18:58:42 +00001255 MachineBasicBlock *BB,
1256 unsigned Size) const {
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001257 assert((Size == 1 || Size == 2) &&
1258 "Unsupported size for EmitAtomicCmpSwapPartial.");
1259
1260 MachineFunction *MF = BB->getParent();
1261 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1262 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1263 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001264 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001265
1266 unsigned Dest = MI->getOperand(0).getReg();
1267 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka0e019592011-07-19 20:11:17 +00001268 unsigned CmpVal = MI->getOperand(2).getReg();
1269 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001270
Akira Hatanaka0e019592011-07-19 20:11:17 +00001271 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1272 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001273 unsigned Mask = RegInfo.createVirtualRegister(RC);
1274 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001275 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1276 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1277 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1278 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1279 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1280 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1281 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1282 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1283 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1284 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1285 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1286 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1287 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1288 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001289
1290 // insert new blocks after the current block
1291 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1292 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1293 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001294 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001295 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1296 MachineFunction::iterator It = BB;
1297 ++It;
1298 MF->insert(It, loop1MBB);
1299 MF->insert(It, loop2MBB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001300 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001301 MF->insert(It, exitMBB);
1302
1303 // Transfer the remainder of BB and its successor edges to exitMBB.
1304 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001305 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001306 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1307
Akira Hatanaka08636b42011-07-19 17:09:53 +00001308 BB->addSuccessor(loop1MBB);
1309 loop1MBB->addSuccessor(sinkMBB);
1310 loop1MBB->addSuccessor(loop2MBB);
1311 loop2MBB->addSuccessor(loop1MBB);
1312 loop2MBB->addSuccessor(sinkMBB);
1313 sinkMBB->addSuccessor(exitMBB);
1314
Akira Hatanakae4503582011-07-19 18:14:26 +00001315 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001316 // thisMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001317 // addiu masklsb2,$0,-4 # 0xfffffffc
1318 // and alignedaddr,ptr,masklsb2
1319 // andi ptrlsb2,ptr,3
1320 // sll shiftamt,ptrlsb2,3
1321 // ori maskupper,$0,255 # 0xff
1322 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001323 // nor mask2,$0,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001324 // andi maskedcmpval,cmpval,255
1325 // sll shiftedcmpval,maskedcmpval,shiftamt
1326 // andi maskednewval,newval,255
1327 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001328 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001329 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001330 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001331 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001332 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001333 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Akira Hatanaka2bf97332013-05-31 03:25:44 +00001334 if (Subtarget->isLittle()) {
1335 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1336 } else {
1337 unsigned Off = RegInfo.createVirtualRegister(RC);
1338 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1339 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1340 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1341 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001342 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001343 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001344 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001345 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001346 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1347 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001348 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001349 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001350 .addReg(MaskedCmpVal).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001351 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001352 .addReg(NewVal).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001353 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001354 .addReg(MaskedNewVal).addReg(ShiftAmt);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001355
1356 // loop1MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001357 // ll oldval,0(alginedaddr)
1358 // and maskedoldval0,oldval,mask
1359 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001360 BB = loop1MBB;
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001361 BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001362 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001363 .addReg(OldVal).addReg(Mask);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001364 BuildMI(BB, DL, TII->get(Mips::BNE))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001365 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001366
1367 // loop2MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001368 // and maskedoldval1,oldval,mask2
1369 // or storeval,maskedoldval1,shiftednewval
1370 // sc success,storeval,0(alignedaddr)
1371 // beq success,$0,loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001372 BB = loop2MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001373 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001374 .addReg(OldVal).addReg(Mask2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001375 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001376 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001377 BuildMI(BB, DL, TII->get(Mips::SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001378 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001379 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001380 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001381
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001382 // sinkMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001383 // srl srlres,maskedoldval0,shiftamt
1384 // sll sllres,srlres,24
1385 // sra dest,sllres,24
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001386 BB = sinkMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001387 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakae97bd812011-07-19 03:14:58 +00001388
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001389 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001390 .addReg(MaskedOldVal0).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001391 BuildMI(BB, DL, TII->get(Mips::SLL), SllRes)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001392 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001393 BuildMI(BB, DL, TII->get(Mips::SRA), Dest)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001394 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001395
1396 MI->eraseFromParent(); // The instruction is gone now.
1397
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001398 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001399}
1400
Akira Hatanakae2489122011-04-15 21:51:11 +00001401//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001402// Misc Lower Operation implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00001403//===----------------------------------------------------------------------===//
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001404SDValue MipsTargetLowering::lowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001405 SDValue Chain = Op.getOperand(0);
1406 SDValue Table = Op.getOperand(1);
1407 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001408 SDLoc DL(Op);
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001409 EVT PTy = getPointerTy();
1410 unsigned EntrySize =
1411 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(*getDataLayout());
1412
1413 Index = DAG.getNode(ISD::MUL, DL, PTy, Index,
1414 DAG.getConstant(EntrySize, PTy));
1415 SDValue Addr = DAG.getNode(ISD::ADD, DL, PTy, Index, Table);
1416
1417 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
1418 Addr = DAG.getExtLoad(ISD::SEXTLOAD, DL, PTy, Chain, Addr,
1419 MachinePointerInfo::getJumpTable(), MemVT, false, false,
1420 0);
1421 Chain = Addr.getValue(1);
1422
Daniel Sandersd897b562014-03-27 10:46:12 +00001423 if ((getTargetMachine().getRelocationModel() == Reloc::PIC_) || isN64()) {
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001424 // For PIC, the sequence is:
1425 // BRIND(load(Jumptable + index) + RelocBase)
1426 // RelocBase can be JumpTable, GOT or some sort of global base.
1427 Addr = DAG.getNode(ISD::ADD, DL, PTy, Addr,
1428 getPICJumpTableRelocBase(Table, DAG));
1429 }
1430
1431 return DAG.getNode(ISD::BRIND, DL, MVT::Other, Chain, Addr);
1432}
1433
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001434SDValue MipsTargetLowering::lowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Wesley Peck527da1b2010-11-23 03:31:01 +00001435 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001436 // the block to branch to if the condition is true.
1437 SDValue Chain = Op.getOperand(0);
1438 SDValue Dest = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001439 SDLoc DL(Op);
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001440
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001441 SDValue CondRes = createFPCmp(DAG, Op.getOperand(1));
Akira Hatanakaa5352702011-03-31 18:26:17 +00001442
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001443 // Return if flag is not set by a floating point comparison.
Akira Hatanakaa5352702011-03-31 18:26:17 +00001444 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopesa9504222008-07-30 17:06:13 +00001445 return Op;
Wesley Peck527da1b2010-11-23 03:31:01 +00001446
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +00001447 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmaneffb8942008-09-12 16:56:44 +00001448 Mips::CondCode CC =
1449 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Akira Hatanakaf0ea5002013-03-30 01:16:38 +00001450 unsigned Opc = invertFPCondCodeUser(CC) ? Mips::BRANCH_F : Mips::BRANCH_T;
1451 SDValue BrCode = DAG.getConstant(Opc, MVT::i32);
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +00001452 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001453 return DAG.getNode(MipsISD::FPBrcond, DL, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +00001454 FCC0, Dest, CondRes);
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001455}
1456
1457SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001458lowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001459{
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001460 SDValue Cond = createFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001461
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001462 // Return if flag is not set by a floating point comparison.
Akira Hatanakaa5352702011-03-31 18:26:17 +00001463 if (Cond.getOpcode() != MipsISD::FPCmp)
1464 return Op;
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +00001465
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001466 return createCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
Andrew Trickef9de2a2013-05-25 02:42:55 +00001467 SDLoc(Op));
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001468}
1469
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001470SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001471lowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001472{
Andrew Trickef9de2a2013-05-25 02:42:55 +00001473 SDLoc DL(Op);
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001474 EVT Ty = Op.getOperand(0).getValueType();
Matt Arsenault758659232013-05-18 00:21:46 +00001475 SDValue Cond = DAG.getNode(ISD::SETCC, DL,
1476 getSetCCResultType(*DAG.getContext(), Ty),
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001477 Op.getOperand(0), Op.getOperand(1),
1478 Op.getOperand(4));
1479
1480 return DAG.getNode(ISD::SELECT, DL, Op.getValueType(), Cond, Op.getOperand(2),
1481 Op.getOperand(3));
1482}
1483
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001484SDValue MipsTargetLowering::lowerSETCC(SDValue Op, SelectionDAG &DAG) const {
1485 SDValue Cond = createFPCmp(DAG, Op);
Akira Hatanakab7f78592012-03-09 23:46:03 +00001486
1487 assert(Cond.getOpcode() == MipsISD::FPCmp &&
1488 "Floating point operand expected.");
1489
1490 SDValue True = DAG.getConstant(1, MVT::i32);
1491 SDValue False = DAG.getConstant(0, MVT::i32);
1492
Andrew Trickef9de2a2013-05-25 02:42:55 +00001493 return createCMovFP(DAG, Cond, True, False, SDLoc(Op));
Akira Hatanakab7f78592012-03-09 23:46:03 +00001494}
1495
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001496SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001497 SelectionDAG &DAG) const {
Dale Johannesen400dc2e2009-02-06 21:50:26 +00001498 // FIXME there isn't actually debug info here
Andrew Trickef9de2a2013-05-25 02:42:55 +00001499 SDLoc DL(Op);
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001500 EVT Ty = Op.getValueType();
1501 GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
1502 const GlobalValue *GV = N->getGlobal();
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001503
Daniel Sandersd897b562014-03-27 10:46:12 +00001504 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !isN64()) {
Akira Hatanaka92a96e12012-09-12 23:27:55 +00001505 const MipsTargetObjectFile &TLOF =
1506 (const MipsTargetObjectFile&)getObjFileLowering();
Wesley Peck527da1b2010-11-23 03:31:01 +00001507
Chris Lattner58e8be82009-08-13 05:41:27 +00001508 // %gp_rel relocation
Wesley Peck527da1b2010-11-23 03:31:01 +00001509 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001510 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, 0,
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +00001511 MipsII::MO_GPREL);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001512 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, DL,
Craig Topper48d114b2014-04-26 18:35:24 +00001513 DAG.getVTList(MVT::i32), GA);
Akira Hatanakaad495022012-08-22 03:18:13 +00001514 SDValue GPReg = DAG.getRegister(Mips::GP, MVT::i32);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001515 return DAG.getNode(ISD::ADD, DL, MVT::i32, GPReg, GPRelNode);
Chris Lattner58e8be82009-08-13 05:41:27 +00001516 }
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001517
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001518 // %hi/%lo relocation
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001519 return getAddrNonPIC(N, Ty, DAG);
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001520 }
1521
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001522 if (GV->hasInternalLinkage() || (GV->hasLocalLinkage() && !isa<Function>(GV)))
Daniel Sandersd897b562014-03-27 10:46:12 +00001523 return getAddrLocal(N, Ty, DAG, isN32() || isN64());
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001524
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00001525 if (LargeGOT)
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001526 return getAddrGlobalLargeGOT(N, Ty, DAG, MipsII::MO_GOT_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00001527 MipsII::MO_GOT_LO16, DAG.getEntryNode(),
1528 MachinePointerInfo::getGOT());
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00001529
Daniel Sandersbd0e3902014-03-27 12:49:34 +00001530 return getAddrGlobal(N, Ty, DAG, (isN32() || isN64()) ? MipsII::MO_GOT_DISP
1531 : MipsII::MO_GOT16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00001532 DAG.getEntryNode(), MachinePointerInfo::getGOT());
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001533}
1534
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001535SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op,
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +00001536 SelectionDAG &DAG) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001537 BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op);
1538 EVT Ty = Op.getValueType();
Akira Hatanaka30f97cf2013-09-25 00:30:25 +00001539
Daniel Sandersd897b562014-03-27 10:46:12 +00001540 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !isN64())
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001541 return getAddrNonPIC(N, Ty, DAG);
1542
Daniel Sandersd897b562014-03-27 10:46:12 +00001543 return getAddrLocal(N, Ty, DAG, isN32() || isN64());
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +00001544}
1545
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001546SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001547lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001548{
Akira Hatanakabff84e12011-12-14 18:26:41 +00001549 // If the relocation model is PIC, use the General Dynamic TLS Model or
1550 // Local Dynamic TLS model, otherwise use the Initial Exec or
1551 // Local Exec TLS Model.
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001552
1553 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001554 SDLoc DL(GA);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001555 const GlobalValue *GV = GA->getGlobal();
1556 EVT PtrVT = getPointerTy();
1557
Hans Wennborgaea41202012-05-04 09:40:39 +00001558 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
1559
1560 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Hans Wennborg245917b2012-06-04 14:02:08 +00001561 // General Dynamic and Local Dynamic TLS Model.
1562 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
1563 : MipsII::MO_TLSGD;
1564
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001565 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag);
1566 SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT,
1567 getGlobalReg(DAG, PtrVT), TGA);
Akira Hatanakaf10ee842011-12-08 21:05:38 +00001568 unsigned PtrSize = PtrVT.getSizeInBits();
1569 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1570
Benjamin Kramer64ba50a2011-12-11 12:21:34 +00001571 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001572
1573 ArgListTy Args;
1574 ArgListEntry Entry;
1575 Entry.Node = Argument;
Akira Hatanakadee6c822011-12-08 20:34:32 +00001576 Entry.Ty = PtrTy;
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001577 Args.push_back(Entry);
Jia Liuf54f60f2012-02-28 07:46:26 +00001578
Justin Holewinskiaa583972012-05-25 16:35:28 +00001579 TargetLowering::CallLoweringInfo CLI(DAG.getEntryNode(), PtrTy,
Evan Cheng65f9d192012-02-28 18:51:51 +00001580 false, false, false, false, 0, CallingConv::C,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001581 /*IsTailCall=*/false, /*doesNotRet=*/false,
Evan Cheng65f9d192012-02-28 18:51:51 +00001582 /*isReturnValueUsed=*/true,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001583 TlsGetAddr, Args, DAG, DL);
Justin Holewinskiaa583972012-05-25 16:35:28 +00001584 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001585
Akira Hatanakabff84e12011-12-14 18:26:41 +00001586 SDValue Ret = CallResult.first;
1587
Hans Wennborgaea41202012-05-04 09:40:39 +00001588 if (model != TLSModel::LocalDynamic)
Akira Hatanakabff84e12011-12-14 18:26:41 +00001589 return Ret;
1590
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001591 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanakabff84e12011-12-14 18:26:41 +00001592 MipsII::MO_DTPREL_HI);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001593 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1594 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanakabff84e12011-12-14 18:26:41 +00001595 MipsII::MO_DTPREL_LO);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001596 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1597 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret);
1598 return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001599 }
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001600
1601 SDValue Offset;
Hans Wennborgaea41202012-05-04 09:40:39 +00001602 if (model == TLSModel::InitialExec) {
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001603 // Initial Exec TLS Model
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001604 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001605 MipsII::MO_GOTTPREL);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001606 TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT),
Akira Hatanakab049aef2012-02-24 22:34:47 +00001607 TGA);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001608 Offset = DAG.getLoad(PtrVT, DL,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001609 DAG.getEntryNode(), TGA, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001610 false, false, false, 0);
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001611 } else {
1612 // Local Exec TLS Model
Hans Wennborgaea41202012-05-04 09:40:39 +00001613 assert(model == TLSModel::LocalExec);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001614 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001615 MipsII::MO_TPREL_HI);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001616 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001617 MipsII::MO_TPREL_LO);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001618 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1619 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1620 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001621 }
1622
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001623 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
1624 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001625}
1626
1627SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001628lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +00001629{
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001630 JumpTableSDNode *N = cast<JumpTableSDNode>(Op);
1631 EVT Ty = Op.getValueType();
Akira Hatanaka30f97cf2013-09-25 00:30:25 +00001632
Daniel Sandersd897b562014-03-27 10:46:12 +00001633 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !isN64())
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001634 return getAddrNonPIC(N, Ty, DAG);
1635
Daniel Sandersd897b562014-03-27 10:46:12 +00001636 return getAddrLocal(N, Ty, DAG, isN32() || isN64());
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +00001637}
1638
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001639SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001640lowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +00001641{
Bruno Cardoso Lopesfdb4cec2008-07-23 16:01:50 +00001642 // gp_rel relocation
Wesley Peck527da1b2010-11-23 03:31:01 +00001643 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001644 // but the asm printer currently doesn't support this feature without
Wesley Peck527da1b2010-11-23 03:31:01 +00001645 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopes98bda582008-07-28 19:26:25 +00001646 // stuff below.
Eli Friedman57c11da2009-08-03 02:22:28 +00001647 //if (IsInSmallSection(C->getType())) {
Owen Anderson9f944592009-08-11 20:47:22 +00001648 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1649 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peck527da1b2010-11-23 03:31:01 +00001650 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001651 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
1652 EVT Ty = Op.getValueType();
Bruno Cardoso Lopes2db07582009-11-25 12:17:58 +00001653
Daniel Sandersd897b562014-03-27 10:46:12 +00001654 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !isN64())
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001655 return getAddrNonPIC(N, Ty, DAG);
Bruno Cardoso Lopesfdb4cec2008-07-23 16:01:50 +00001656
Daniel Sandersd897b562014-03-27 10:46:12 +00001657 return getAddrLocal(N, Ty, DAG, isN32() || isN64());
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +00001658}
1659
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001660SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman31ae5862010-04-17 14:41:14 +00001661 MachineFunction &MF = DAG.getMachineFunction();
1662 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1663
Andrew Trickef9de2a2013-05-25 02:42:55 +00001664 SDLoc DL(Op);
Dan Gohman31ae5862010-04-17 14:41:14 +00001665 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1666 getPointerTy());
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +00001667
1668 // vastart just stores the address of the VarArgsFrameIndex slot into the
1669 // memory location argument.
1670 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001671 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00001672 MachinePointerInfo(SV), false, false, 0);
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +00001673}
Jia Liuf54f60f2012-02-28 07:46:26 +00001674
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001675static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG,
1676 bool HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001677 EVT TyX = Op.getOperand(0).getValueType();
1678 EVT TyY = Op.getOperand(1).getValueType();
1679 SDValue Const1 = DAG.getConstant(1, MVT::i32);
1680 SDValue Const31 = DAG.getConstant(31, MVT::i32);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001681 SDLoc DL(Op);
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001682 SDValue Res;
1683
1684 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1685 // to i32.
1686 SDValue X = (TyX == MVT::f32) ?
1687 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1688 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1689 Const1);
1690 SDValue Y = (TyY == MVT::f32) ?
1691 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
1692 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
1693 Const1);
1694
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001695 if (HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001696 // ext E, Y, 31, 1 ; extract bit31 of Y
1697 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
1698 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
1699 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
1700 } else {
1701 // sll SllX, X, 1
1702 // srl SrlX, SllX, 1
1703 // srl SrlY, Y, 31
1704 // sll SllY, SrlX, 31
1705 // or Or, SrlX, SllY
1706 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1707 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1708 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
1709 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
1710 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
1711 }
1712
1713 if (TyX == MVT::f32)
1714 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
1715
1716 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1717 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1718 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001719}
1720
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001721static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG,
1722 bool HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001723 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
1724 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
1725 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
1726 SDValue Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001727 SDLoc DL(Op);
Eric Christopher0713a9d2011-06-08 23:55:35 +00001728
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001729 // Bitcast to integer nodes.
1730 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
1731 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001732
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001733 if (HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001734 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
1735 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
1736 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
1737 DAG.getConstant(WidthY - 1, MVT::i32), Const1);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001738
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001739 if (WidthX > WidthY)
1740 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
1741 else if (WidthY > WidthX)
1742 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001743
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001744 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
1745 DAG.getConstant(WidthX - 1, MVT::i32), Const1, X);
1746 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
1747 }
1748
1749 // (d)sll SllX, X, 1
1750 // (d)srl SrlX, SllX, 1
1751 // (d)srl SrlY, Y, width(Y)-1
1752 // (d)sll SllY, SrlX, width(Y)-1
1753 // or Or, SrlX, SllY
1754 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
1755 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
1756 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
1757 DAG.getConstant(WidthY - 1, MVT::i32));
1758
1759 if (WidthX > WidthY)
1760 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
1761 else if (WidthY > WidthX)
1762 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
1763
1764 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
1765 DAG.getConstant(WidthX - 1, MVT::i32));
1766 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
1767 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001768}
1769
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00001770SDValue
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001771MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Daniel Sanders863c35a2014-04-14 16:24:12 +00001772 if (Subtarget->isGP64bit())
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001773 return lowerFCOPYSIGN64(Op, DAG, Subtarget->hasExtractInsert());
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001774
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001775 return lowerFCOPYSIGN32(Op, DAG, Subtarget->hasExtractInsert());
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001776}
1777
Akira Hatanaka66277522011-06-02 00:24:44 +00001778SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001779lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes5444a7b2011-06-16 00:40:02 +00001780 // check the depth
1781 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka15506782011-06-07 18:58:42 +00001782 "Frame address can only be determined for current frame.");
Akira Hatanaka66277522011-06-02 00:24:44 +00001783
1784 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1785 MFI->setFrameAddressIsTaken(true);
1786 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001787 SDLoc DL(Op);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001788 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), DL,
Daniel Sandersd897b562014-03-27 10:46:12 +00001789 isN64() ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka66277522011-06-02 00:24:44 +00001790 return FrameAddr;
1791}
1792
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001793SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op,
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00001794 SelectionDAG &DAG) const {
Bill Wendling908bf812014-01-06 00:43:20 +00001795 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001796 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001797
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00001798 // check the depth
1799 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
1800 "Return address can be determined only for current frame.");
1801
1802 MachineFunction &MF = DAG.getMachineFunction();
1803 MachineFrameInfo *MFI = MF.getFrameInfo();
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001804 MVT VT = Op.getSimpleValueType();
Daniel Sandersd897b562014-03-27 10:46:12 +00001805 unsigned RA = isN64() ? Mips::RA_64 : Mips::RA;
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00001806 MFI->setReturnAddressIsTaken(true);
1807
1808 // Return RA, which contains the return address. Mark it an implicit live-in.
1809 unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00001810 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT);
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00001811}
1812
Akira Hatanakac0b02062013-01-30 00:26:49 +00001813// An EH_RETURN is the result of lowering llvm.eh.return which in turn is
1814// generated from __builtin_eh_return (offset, handler)
1815// The effect of this is to adjust the stack pointer by "offset"
1816// and then branch to "handler".
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001817SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Akira Hatanakac0b02062013-01-30 00:26:49 +00001818 const {
1819 MachineFunction &MF = DAG.getMachineFunction();
1820 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1821
1822 MipsFI->setCallsEhReturn();
1823 SDValue Chain = Op.getOperand(0);
1824 SDValue Offset = Op.getOperand(1);
1825 SDValue Handler = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001826 SDLoc DL(Op);
Daniel Sandersd897b562014-03-27 10:46:12 +00001827 EVT Ty = isN64() ? MVT::i64 : MVT::i32;
Akira Hatanakac0b02062013-01-30 00:26:49 +00001828
1829 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
1830 // EH_RETURN nodes, so that instructions are emitted back-to-back.
Daniel Sandersd897b562014-03-27 10:46:12 +00001831 unsigned OffsetReg = isN64() ? Mips::V1_64 : Mips::V1;
1832 unsigned AddrReg = isN64() ? Mips::V0_64 : Mips::V0;
Akira Hatanakac0b02062013-01-30 00:26:49 +00001833 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
1834 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
1835 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
1836 DAG.getRegister(OffsetReg, Ty),
1837 DAG.getRegister(AddrReg, getPointerTy()),
1838 Chain.getValue(1));
1839}
1840
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001841SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
Akira Hatanaka5fd22482012-06-14 21:10:56 +00001842 SelectionDAG &DAG) const {
Eli Friedman26a48482011-07-27 22:21:52 +00001843 // FIXME: Need pseudo-fence for 'singlethread' fences
1844 // FIXME: Set SType for weaker fences where supported/appropriate.
1845 unsigned SType = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001846 SDLoc DL(Op);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001847 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
Eli Friedman26a48482011-07-27 22:21:52 +00001848 DAG.getConstant(SType, MVT::i32));
1849}
1850
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001851SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
Akira Hatanaka5fd22482012-06-14 21:10:56 +00001852 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001853 SDLoc DL(Op);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00001854 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
1855 SDValue Shamt = Op.getOperand(2);
1856
1857 // if shamt < 32:
1858 // lo = (shl lo, shamt)
1859 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
1860 // else:
1861 // lo = 0
1862 // hi = (shl lo, shamt[4:0])
1863 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
1864 DAG.getConstant(-1, MVT::i32));
1865 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo,
1866 DAG.getConstant(1, MVT::i32));
1867 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, ShiftRight1Lo,
1868 Not);
1869 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi, Shamt);
1870 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
1871 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, MVT::i32, Lo, Shamt);
1872 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
1873 DAG.getConstant(0x20, MVT::i32));
Akira Hatanaka5fd22482012-06-14 21:10:56 +00001874 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
1875 DAG.getConstant(0, MVT::i32), ShiftLeftLo);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00001876 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftLeftLo, Or);
1877
1878 SDValue Ops[2] = {Lo, Hi};
Craig Topper64941d92014-04-27 19:20:57 +00001879 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00001880}
1881
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001882SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00001883 bool IsSRA) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001884 SDLoc DL(Op);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00001885 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
1886 SDValue Shamt = Op.getOperand(2);
1887
1888 // if shamt < 32:
1889 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
1890 // if isSRA:
1891 // hi = (sra hi, shamt)
1892 // else:
1893 // hi = (srl hi, shamt)
1894 // else:
1895 // if isSRA:
1896 // lo = (sra hi, shamt[4:0])
1897 // hi = (sra hi, 31)
1898 // else:
1899 // lo = (srl hi, shamt[4:0])
1900 // hi = 0
1901 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
1902 DAG.getConstant(-1, MVT::i32));
1903 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi,
1904 DAG.getConstant(1, MVT::i32));
1905 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, ShiftLeft1Hi, Not);
1906 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo, Shamt);
1907 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
1908 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL, DL, MVT::i32,
1909 Hi, Shamt);
1910 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
1911 DAG.getConstant(0x20, MVT::i32));
1912 SDValue Shift31 = DAG.getNode(ISD::SRA, DL, MVT::i32, Hi,
1913 DAG.getConstant(31, MVT::i32));
1914 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftRightHi, Or);
1915 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
1916 IsSRA ? Shift31 : DAG.getConstant(0, MVT::i32),
1917 ShiftRightHi);
1918
1919 SDValue Ops[2] = {Lo, Hi};
Craig Topper64941d92014-04-27 19:20:57 +00001920 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00001921}
1922
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00001923static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00001924 SDValue Chain, SDValue Src, unsigned Offset) {
Akira Hatanaka95866182012-06-13 19:06:08 +00001925 SDValue Ptr = LD->getBasePtr();
Akira Hatanaka8f1db772012-06-02 00:03:49 +00001926 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
Akira Hatanaka95866182012-06-13 19:06:08 +00001927 EVT BasePtrVT = Ptr.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001928 SDLoc DL(LD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00001929 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
1930
1931 if (Offset)
Akira Hatanaka95866182012-06-13 19:06:08 +00001932 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00001933 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka8f1db772012-06-02 00:03:49 +00001934
1935 SDValue Ops[] = { Chain, Ptr, Src };
Craig Topper206fcd42014-04-26 19:29:41 +00001936 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00001937 LD->getMemOperand());
1938}
1939
1940// Expand an unaligned 32 or 64-bit integer load node.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001941SDValue MipsTargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka8f1db772012-06-02 00:03:49 +00001942 LoadSDNode *LD = cast<LoadSDNode>(Op);
1943 EVT MemVT = LD->getMemoryVT();
1944
1945 // Return if load is aligned or if MemVT is neither i32 nor i64.
1946 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
1947 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
1948 return SDValue();
1949
1950 bool IsLittle = Subtarget->isLittle();
1951 EVT VT = Op.getValueType();
1952 ISD::LoadExtType ExtType = LD->getExtensionType();
1953 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
1954
1955 assert((VT == MVT::i32) || (VT == MVT::i64));
1956
1957 // Expand
1958 // (set dst, (i64 (load baseptr)))
1959 // to
1960 // (set tmp, (ldl (add baseptr, 7), undef))
1961 // (set dst, (ldr baseptr, tmp))
1962 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00001963 SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00001964 IsLittle ? 7 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00001965 return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00001966 IsLittle ? 0 : 7);
1967 }
1968
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00001969 SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00001970 IsLittle ? 3 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00001971 SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00001972 IsLittle ? 0 : 3);
1973
1974 // Expand
1975 // (set dst, (i32 (load baseptr))) or
1976 // (set dst, (i64 (sextload baseptr))) or
1977 // (set dst, (i64 (extload baseptr)))
1978 // to
1979 // (set tmp, (lwl (add baseptr, 3), undef))
1980 // (set dst, (lwr baseptr, tmp))
1981 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
1982 (ExtType == ISD::EXTLOAD))
1983 return LWR;
1984
1985 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
1986
1987 // Expand
1988 // (set dst, (i64 (zextload baseptr)))
1989 // to
1990 // (set tmp0, (lwl (add baseptr, 3), undef))
1991 // (set tmp1, (lwr baseptr, tmp0))
1992 // (set tmp2, (shl tmp1, 32))
1993 // (set dst, (srl tmp2, 32))
Andrew Trickef9de2a2013-05-25 02:42:55 +00001994 SDLoc DL(LD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00001995 SDValue Const32 = DAG.getConstant(32, MVT::i32);
1996 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
Akira Hatanaka67346852012-06-04 17:46:29 +00001997 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
1998 SDValue Ops[] = { SRL, LWR.getValue(1) };
Craig Topper64941d92014-04-27 19:20:57 +00001999 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002000}
2001
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002002static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002003 SDValue Chain, unsigned Offset) {
Akira Hatanaka95866182012-06-13 19:06:08 +00002004 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
2005 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002006 SDLoc DL(SD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002007 SDVTList VTList = DAG.getVTList(MVT::Other);
2008
2009 if (Offset)
Akira Hatanaka95866182012-06-13 19:06:08 +00002010 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002011 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002012
2013 SDValue Ops[] = { Chain, Value, Ptr };
Craig Topper206fcd42014-04-26 19:29:41 +00002014 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002015 SD->getMemOperand());
2016}
2017
2018// Expand an unaligned 32 or 64-bit integer store node.
Akira Hatanakad82ee942013-05-16 20:45:17 +00002019static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG,
2020 bool IsLittle) {
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002021 SDValue Value = SD->getValue(), Chain = SD->getChain();
2022 EVT VT = Value.getValueType();
2023
2024 // Expand
2025 // (store val, baseptr) or
2026 // (truncstore val, baseptr)
2027 // to
2028 // (swl val, (add baseptr, 3))
2029 // (swr val, baseptr)
2030 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002031 SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002032 IsLittle ? 3 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002033 return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002034 }
2035
2036 assert(VT == MVT::i64);
2037
2038 // Expand
2039 // (store val, baseptr)
2040 // to
2041 // (sdl val, (add baseptr, 7))
2042 // (sdr val, baseptr)
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002043 SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2044 return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002045}
2046
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002047// Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr).
2048static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG) {
2049 SDValue Val = SD->getValue();
2050
2051 if (Val.getOpcode() != ISD::FP_TO_SINT)
2052 return SDValue();
2053
2054 EVT FPTy = EVT::getFloatingPointVT(Val.getValueSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002055 SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy,
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002056 Val.getOperand(0));
2057
Andrew Trickef9de2a2013-05-25 02:42:55 +00002058 return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(),
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002059 SD->getPointerInfo(), SD->isVolatile(),
2060 SD->isNonTemporal(), SD->getAlignment());
2061}
2062
Akira Hatanakad82ee942013-05-16 20:45:17 +00002063SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2064 StoreSDNode *SD = cast<StoreSDNode>(Op);
2065 EVT MemVT = SD->getMemoryVT();
2066
2067 // Lower unaligned integer stores.
2068 if ((SD->getAlignment() < MemVT.getSizeInBits() / 8) &&
2069 ((MemVT == MVT::i32) || (MemVT == MVT::i64)))
2070 return lowerUnalignedIntStore(SD, DAG, Subtarget->isLittle());
2071
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002072 return lowerFP_TO_SINT_STORE(SD, DAG);
Akira Hatanakad82ee942013-05-16 20:45:17 +00002073}
2074
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002075SDValue MipsTargetLowering::lowerADD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002076 if (Op->getOperand(0).getOpcode() != ISD::FRAMEADDR
2077 || cast<ConstantSDNode>
2078 (Op->getOperand(0).getOperand(0))->getZExtValue() != 0
2079 || Op->getOperand(1).getOpcode() != ISD::FRAME_TO_ARGS_OFFSET)
2080 return SDValue();
2081
2082 // The pattern
2083 // (add (frameaddr 0), (frame_to_args_offset))
2084 // results from lowering llvm.eh.dwarf.cfa intrinsic. Transform it to
2085 // (add FrameObject, 0)
2086 // where FrameObject is a fixed StackObject with offset 0 which points to
2087 // the old stack pointer.
2088 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2089 EVT ValTy = Op->getValueType(0);
2090 int FI = MFI->CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
2091 SDValue InArgsAddr = DAG.getFrameIndex(FI, ValTy);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002092 return DAG.getNode(ISD::ADD, SDLoc(Op), ValTy, InArgsAddr,
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002093 DAG.getConstant(0, ValTy));
2094}
2095
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002096SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op,
2097 SelectionDAG &DAG) const {
2098 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002099 SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy,
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002100 Op.getOperand(0));
Andrew Trickef9de2a2013-05-25 02:42:55 +00002101 return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc);
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002102}
2103
Akira Hatanakae2489122011-04-15 21:51:11 +00002104//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002105// Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002106//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002107
Akira Hatanakae2489122011-04-15 21:51:11 +00002108//===----------------------------------------------------------------------===//
Wesley Peck527da1b2010-11-23 03:31:01 +00002109// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002110// Mips O32 ABI rules:
2111// ---
2112// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peck527da1b2010-11-23 03:31:01 +00002113// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002114// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peck527da1b2010-11-23 03:31:01 +00002115// f64 - Only passed in two aliased f32 registers if no int reg has been used
2116// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002117// not used, it must be shadowed. If only A3 is avaiable, shadow it and
2118// go to stack.
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002119//
2120// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanakae2489122011-04-15 21:51:11 +00002121//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002122
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00002123static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2124 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
Craig Topper840beec2014-04-04 05:16:06 +00002125 CCState &State, const MCPhysReg *F64Regs) {
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002126
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00002127 static const unsigned IntRegsSize = 4, FloatRegsSize = 2;
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002128
Craig Topper840beec2014-04-04 05:16:06 +00002129 static const MCPhysReg IntRegs[] = { Mips::A0, Mips::A1, Mips::A2, Mips::A3 };
2130 static const MCPhysReg F32Regs[] = { Mips::F12, Mips::F14 };
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002131
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002132 // Do not process byval args here.
2133 if (ArgFlags.isByVal())
2134 return true;
Akira Hatanaka5e16c6a2011-05-24 19:18:33 +00002135
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002136 // Promote i8 and i16
2137 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
2138 LocVT = MVT::i32;
2139 if (ArgFlags.isSExt())
2140 LocInfo = CCValAssign::SExt;
2141 else if (ArgFlags.isZExt())
2142 LocInfo = CCValAssign::ZExt;
2143 else
2144 LocInfo = CCValAssign::AExt;
2145 }
2146
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002147 unsigned Reg;
2148
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002149 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
2150 // is true: function is vararg, argument is 3rd or higher, there is previous
2151 // argument which is not f32 or f64.
2152 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
2153 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanaka9e6a8cc2011-05-19 20:29:48 +00002154 unsigned OrigAlign = ArgFlags.getOrigAlign();
2155 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002156
2157 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002158 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanaka9e6a8cc2011-05-19 20:29:48 +00002159 // If this is the first part of an i64 arg,
2160 // the allocated register must be either A0 or A2.
2161 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
2162 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002163 LocVT = MVT::i32;
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002164 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
2165 // Allocate int register and shadow next int register. If first
2166 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002167 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2168 if (Reg == Mips::A1 || Reg == Mips::A3)
2169 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2170 State.AllocateReg(IntRegs, IntRegsSize);
2171 LocVT = MVT::i32;
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002172 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
2173 // we are guaranteed to find an available float register
2174 if (ValVT == MVT::f32) {
2175 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
2176 // Shadow int register
2177 State.AllocateReg(IntRegs, IntRegsSize);
2178 } else {
2179 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
2180 // Shadow int registers
2181 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
2182 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
2183 State.AllocateReg(IntRegs, IntRegsSize);
2184 State.AllocateReg(IntRegs, IntRegsSize);
2185 }
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002186 } else
2187 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002188
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002189 if (!Reg) {
2190 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3,
2191 OrigAlign);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002192 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002193 } else
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002194 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002195
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002196 return false;
Akira Hatanaka202f6402011-11-12 02:20:46 +00002197}
2198
Akira Hatanakabfb66242013-08-20 23:38:40 +00002199static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT,
2200 MVT LocVT, CCValAssign::LocInfo LocInfo,
2201 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002202 static const MCPhysReg F64Regs[] = { Mips::D6, Mips::D7 };
Akira Hatanakabfb66242013-08-20 23:38:40 +00002203
2204 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2205}
2206
2207static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT,
2208 MVT LocVT, CCValAssign::LocInfo LocInfo,
2209 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002210 static const MCPhysReg F64Regs[] = { Mips::D12_64, Mips::D14_64 };
Akira Hatanakabfb66242013-08-20 23:38:40 +00002211
2212 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2213}
2214
Akira Hatanaka202f6402011-11-12 02:20:46 +00002215#include "MipsGenCallingConv.inc"
2216
Akira Hatanakae2489122011-04-15 21:51:11 +00002217//===----------------------------------------------------------------------===//
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002218// Call Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002219//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002220
Akira Hatanaka61bbcce2011-09-23 00:58:33 +00002221// Return next O32 integer argument register.
2222static unsigned getNextIntArgReg(unsigned Reg) {
2223 assert((Reg == Mips::A0) || (Reg == Mips::A2));
2224 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
2225}
2226
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002227SDValue
2228MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002229 SDValue Chain, SDValue Arg, SDLoc DL,
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002230 bool IsTailCall, SelectionDAG &DAG) const {
2231 if (!IsTailCall) {
2232 SDValue PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr,
2233 DAG.getIntPtrConstant(Offset));
2234 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo(), false,
2235 false, 0);
2236 }
2237
2238 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2239 int FI = MFI->CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
2240 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2241 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(),
2242 /*isVolatile=*/ true, false, 0);
2243}
2244
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002245void MipsTargetLowering::
2246getOpndList(SmallVectorImpl<SDValue> &Ops,
2247 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
2248 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
2249 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
2250 // Insert node "GP copy globalreg" before call to function.
2251 //
2252 // R_MIPS_CALL* operators (emitted when non-internal functions are called
2253 // in PIC mode) allow symbols to be resolved via lazy binding.
2254 // The lazy binding stub requires GP to point to the GOT.
2255 if (IsPICCall && !InternalLinkage) {
Daniel Sandersd897b562014-03-27 10:46:12 +00002256 unsigned GPReg = isN64() ? Mips::GP_64 : Mips::GP;
2257 EVT Ty = isN64() ? MVT::i64 : MVT::i32;
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002258 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty)));
2259 }
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002260
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002261 // Build a sequence of copy-to-reg nodes chained together with token
2262 // chain and flag operands which copy the outgoing args into registers.
2263 // The InFlag in necessary since all emitted instructions must be
2264 // stuck together.
2265 SDValue InFlag;
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002266
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002267 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2268 Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, RegsToPass[i].first,
2269 RegsToPass[i].second, InFlag);
2270 InFlag = Chain.getValue(1);
2271 }
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002272
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002273 // Add argument registers to the end of the list so that they are
2274 // known live into the call.
2275 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2276 Ops.push_back(CLI.DAG.getRegister(RegsToPass[i].first,
2277 RegsToPass[i].second.getValueType()));
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002278
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002279 // Add a register mask operand representing the call-preserved registers.
2280 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2281 const uint32_t *Mask = TRI->getCallPreservedMask(CLI.CallConv);
2282 assert(Mask && "Missing call preserved mask for calling convention");
Reed Kotler783c7942013-05-10 22:25:39 +00002283 if (Subtarget->inMips16HardFloat()) {
2284 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
2285 llvm::StringRef Sym = G->getGlobal()->getName();
2286 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
Reed Kotler3230e722013-12-12 02:41:11 +00002287 if (F && F->hasFnAttribute("__Mips16RetHelper")) {
Reed Kotler783c7942013-05-10 22:25:39 +00002288 Mask = MipsRegisterInfo::getMips16RetHelperMask();
2289 }
2290 }
2291 }
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002292 Ops.push_back(CLI.DAG.getRegisterMask(Mask));
2293
2294 if (InFlag.getNode())
2295 Ops.push_back(InFlag);
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002296}
2297
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002298/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman624801e2009-01-26 03:15:54 +00002299/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002300SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00002301MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002302 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00002303 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002304 SDLoc DL = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00002305 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
2306 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
2307 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Akira Hatanakabeda2242012-07-31 18:46:41 +00002308 SDValue Chain = CLI.Chain;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002309 SDValue Callee = CLI.Callee;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002310 bool &IsTailCall = CLI.IsTailCall;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002311 CallingConv::ID CallConv = CLI.CallConv;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002312 bool IsVarArg = CLI.IsVarArg;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002313
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002314 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002315 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanaka7c619f12011-05-20 21:39:54 +00002316 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002317 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +00002318 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002319
2320 // Analyze operands of the call, assigning locations to each operand.
2321 SmallVector<CCValAssign, 16> ArgLocs;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002322 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00002323 getTargetMachine(), ArgLocs, *DAG.getContext());
Reed Kotler783c7942013-05-10 22:25:39 +00002324 MipsCC::SpecialCallingConvType SpecialCallingConv =
2325 getSpecialCallingConv(Callee);
Daniel Sandersd897b562014-03-27 10:46:12 +00002326 MipsCC MipsCCInfo(CallConv, isO32(), Subtarget->isFP64bit(), CCInfo,
Akira Hatanakabfb66242013-08-20 23:38:40 +00002327 SpecialCallingConv);
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002328
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002329 MipsCCInfo.analyzeCallOperands(Outs, IsVarArg,
Reed Kotlerc03807a2013-08-30 19:40:56 +00002330 Subtarget->mipsSEUsesSoftFloat(),
Akira Hatanaka3b7391d2013-03-05 22:20:28 +00002331 Callee.getNode(), CLI.Args);
Wesley Peck527da1b2010-11-23 03:31:01 +00002332
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002333 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka195a1e22011-06-08 17:39:33 +00002334 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka97ba7692012-07-26 23:27:01 +00002335
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002336 // Check if it's really possible to do a tail call.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002337 if (IsTailCall)
2338 IsTailCall =
2339 isEligibleForTailCallOptimization(MipsCCInfo, NextStackOffset,
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002340 *MF.getInfo<MipsFunctionInfo>());
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002341
Reid Kleckner5772b772014-04-24 20:14:34 +00002342 if (!IsTailCall && CLI.CS && CLI.CS->isMustTailCall())
2343 report_fatal_error("failed to perform tail call elimination on a call "
2344 "site marked musttail");
2345
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002346 if (IsTailCall)
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002347 ++NumTailCalls;
2348
Akira Hatanaka79738332011-09-19 20:26:02 +00002349 // Chain is the output chain of the last Load/Store or CopyToReg node.
2350 // ByValChain is the output chain of the last Memcpy node created for copying
2351 // byval arguments to the stack.
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002352 unsigned StackAlignment = TFL->getStackAlignment();
2353 NextStackOffset = RoundUpToAlignment(NextStackOffset, StackAlignment);
Akira Hatanaka79738332011-09-19 20:26:02 +00002354 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002355
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002356 if (!IsTailCall)
Andrew Trickad6d08a2013-05-29 22:03:55 +00002357 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffsetVal, DL);
Akira Hatanakabeda2242012-07-31 18:46:41 +00002358
Daniel Sandersd897b562014-03-27 10:46:12 +00002359 SDValue StackPtr = DAG.getCopyFromReg(
2360 Chain, DL, isN64() ? Mips::SP_64 : Mips::SP, getPointerTy());
Akira Hatanaka195a1e22011-06-08 17:39:33 +00002361
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002362 // With EABI is it possible to have 16 args on registers.
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002363 std::deque< std::pair<unsigned, SDValue> > RegsToPass;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002364 SmallVector<SDValue, 8> MemOpChains;
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002365 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002366
2367 // Walk the register/memloc assignments, inserting copies/loads.
2368 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002369 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002370 CCValAssign &VA = ArgLocs[i];
Akira Hatanakab20a3252011-10-28 19:49:00 +00002371 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka19891f82011-11-12 02:34:50 +00002372 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2373
2374 // ByVal Arg.
2375 if (Flags.isByVal()) {
2376 assert(Flags.getByValSize() &&
2377 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002378 assert(ByValArg != MipsCCInfo.byval_end());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002379 assert(!IsTailCall &&
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002380 "Do not tail-call optimize if there is a byval argument.");
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002381 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002382 MipsCCInfo, *ByValArg, Flags, Subtarget->isLittle());
2383 ++ByValArg;
Akira Hatanaka19891f82011-11-12 02:34:50 +00002384 continue;
2385 }
Jia Liuf54f60f2012-02-28 07:46:26 +00002386
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002387 // Promote the value if needed.
2388 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002389 default: llvm_unreachable("Unknown loc info!");
Wesley Peck527da1b2010-11-23 03:31:01 +00002390 case CCValAssign::Full:
Akira Hatanakab20a3252011-10-28 19:49:00 +00002391 if (VA.isRegLoc()) {
2392 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
Akira Hatanaka3b7391d2013-03-05 22:20:28 +00002393 (ValVT == MVT::f64 && LocVT == MVT::i64) ||
2394 (ValVT == MVT::i64 && LocVT == MVT::f64))
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002395 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
Akira Hatanakab20a3252011-10-28 19:49:00 +00002396 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002397 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanakae2489122011-04-15 21:51:11 +00002398 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002399 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanakaaef55c82011-04-15 21:00:26 +00002400 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka27916972011-04-15 19:52:08 +00002401 if (!Subtarget->isLittle())
2402 std::swap(Lo, Hi);
Jia Liuf54f60f2012-02-28 07:46:26 +00002403 unsigned LocRegLo = VA.getLocReg();
Akira Hatanaka61bbcce2011-09-23 00:58:33 +00002404 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2405 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2406 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002407 continue;
Wesley Peck527da1b2010-11-23 03:31:01 +00002408 }
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002409 }
2410 break;
Chris Lattner52f16de2008-03-17 06:57:02 +00002411 case CCValAssign::SExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002412 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002413 break;
2414 case CCValAssign::ZExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002415 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002416 break;
2417 case CCValAssign::AExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002418 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002419 break;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002420 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002421
2422 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +00002423 // RegsToPass vector
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002424 if (VA.isRegLoc()) {
2425 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattner52f16de2008-03-17 06:57:02 +00002426 continue;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002427 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002428
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002429 // Register can't get to this point...
Chris Lattner52f16de2008-03-17 06:57:02 +00002430 assert(VA.isMemLoc());
Wesley Peck527da1b2010-11-23 03:31:01 +00002431
Wesley Peck527da1b2010-11-23 03:31:01 +00002432 // emit ISD::STORE whichs stores the
Chris Lattner52f16de2008-03-17 06:57:02 +00002433 // parameter value to a stack Location
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002434 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002435 Chain, Arg, DL, IsTailCall, DAG));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002436 }
2437
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002438 // Transform all store nodes into one single node because all store
2439 // nodes are independent of each other.
Wesley Peck527da1b2010-11-23 03:31:01 +00002440 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00002441 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002442
Bill Wendling24c79f22008-09-16 21:48:12 +00002443 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peck527da1b2010-11-23 03:31:01 +00002444 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2445 // node so that legalize doesn't hack it.
Daniel Sandersd897b562014-03-27 10:46:12 +00002446 bool IsPICCall = (isN64() || IsPIC); // true if calls are translated to
2447 // jalr $25
Akira Hatanakacf9a61b2012-12-13 03:17:29 +00002448 bool GlobalOrExternal = false, InternalLinkage = false;
Akira Hatanakad6f1c582011-04-07 19:51:44 +00002449 SDValue CalleeLo;
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002450 EVT Ty = Callee.getValueType();
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002451
2452 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002453 if (IsPICCall) {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002454 const GlobalValue *Val = G->getGlobal();
2455 InternalLinkage = Val->hasInternalLinkage();
Akira Hatanakacf9a61b2012-12-13 03:17:29 +00002456
2457 if (InternalLinkage)
Daniel Sandersd897b562014-03-27 10:46:12 +00002458 Callee = getAddrLocal(G, Ty, DAG, isN32() || isN64());
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00002459 else if (LargeGOT)
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002460 Callee = getAddrGlobalLargeGOT(G, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002461 MipsII::MO_CALL_LO16, Chain,
2462 FuncInfo->callPtrInfo(Val));
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002463 else
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002464 Callee = getAddrGlobal(G, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
2465 FuncInfo->callPtrInfo(Val));
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002466 } else
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002467 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, getPointerTy(), 0,
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002468 MipsII::MO_NO_FLAG);
Akira Hatanaka8e16aac2011-12-09 01:45:12 +00002469 GlobalOrExternal = true;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002470 }
2471 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002472 const char *Sym = S->getSymbol();
2473
Daniel Sandersd897b562014-03-27 10:46:12 +00002474 if (!isN64() && !IsPIC) // !N64 && static
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002475 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(),
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002476 MipsII::MO_NO_FLAG);
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00002477 else if (LargeGOT)
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002478 Callee = getAddrGlobalLargeGOT(S, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002479 MipsII::MO_CALL_LO16, Chain,
2480 FuncInfo->callPtrInfo(Sym));
Akira Hatanaka02b0e482013-02-22 21:10:03 +00002481 else // N64 || PIC
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002482 Callee = getAddrGlobal(S, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
2483 FuncInfo->callPtrInfo(Sym));
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002484
Akira Hatanaka8e16aac2011-12-09 01:45:12 +00002485 GlobalOrExternal = true;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002486 }
2487
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002488 SmallVector<SDValue, 8> Ops(1, Chain);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002489 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002490
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002491 getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal, InternalLinkage,
2492 CLI, Callee, Chain);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002493
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002494 if (IsTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +00002495 return DAG.getNode(MipsISD::TailCall, DL, MVT::Other, Ops);
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002496
Craig Topper48d114b2014-04-26 18:35:24 +00002497 Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, Ops);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002498 SDValue InFlag = Chain.getValue(1);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002499
Bruno Cardoso Lopes193e64c2010-01-30 18:32:07 +00002500 // Create the CALLSEQ_END node.
Akira Hatanaka97ba7692012-07-26 23:27:01 +00002501 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal,
Andrew Trickad6d08a2013-05-29 22:03:55 +00002502 DAG.getIntPtrConstant(0, true), InFlag, DL);
Bruno Cardoso Lopes193e64c2010-01-30 18:32:07 +00002503 InFlag = Chain.getValue(1);
2504
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002505 // Handle result values, copying them out of physregs into vregs that we
2506 // return.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002507 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg,
2508 Ins, DL, DAG, InVals, CLI.Callee.getNode(), CLI.RetTy);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002509}
2510
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002511/// LowerCallResult - Lower the result values of a call into the
2512/// appropriate copies out of appropriate physical registers.
2513SDValue
2514MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002515 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002516 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002517 SDLoc DL, SelectionDAG &DAG,
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002518 SmallVectorImpl<SDValue> &InVals,
2519 const SDNode *CallNode,
2520 const Type *RetTy) const {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002521 // Assign locations to each value returned by this call.
2522 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002523 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka5fd22482012-06-14 21:10:56 +00002524 getTargetMachine(), RVLocs, *DAG.getContext());
Daniel Sandersd897b562014-03-27 10:46:12 +00002525 MipsCC MipsCCInfo(CallConv, isO32(), Subtarget->isFP64bit(), CCInfo);
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002526
Reed Kotlerc03807a2013-08-30 19:40:56 +00002527 MipsCCInfo.analyzeCallResult(Ins, Subtarget->mipsSEUsesSoftFloat(),
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002528 CallNode, RetTy);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002529
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002530 // Copy all of the result registers out of their specified physreg.
2531 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002532 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(),
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002533 RVLocs[i].getLocVT(), InFlag);
2534 Chain = Val.getValue(1);
2535 InFlag = Val.getValue(2);
2536
2537 if (RVLocs[i].getValVT() != RVLocs[i].getLocVT())
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002538 Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getValVT(), Val);
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002539
2540 InVals.push_back(Val);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002541 }
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +00002542
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002543 return Chain;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002544}
2545
Akira Hatanakae2489122011-04-15 21:51:11 +00002546//===----------------------------------------------------------------------===//
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002547// Formal Arguments Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002548//===----------------------------------------------------------------------===//
Wesley Peck527da1b2010-11-23 03:31:01 +00002549/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002550/// and generate load operations for arguments places on the stack.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002551SDValue
2552MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanakaaef55c82011-04-15 21:00:26 +00002553 CallingConv::ID CallConv,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002554 bool IsVarArg,
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00002555 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002556 SDLoc DL, SelectionDAG &DAG,
Akira Hatanakaaef55c82011-04-15 21:00:26 +00002557 SmallVectorImpl<SDValue> &InVals)
Akira Hatanakae2489122011-04-15 21:51:11 +00002558 const {
Bruno Cardoso Lopesa01ede22008-08-04 07:12:52 +00002559 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002560 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopes14033fb2007-08-28 05:08:16 +00002561 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002562
Dan Gohman31ae5862010-04-17 14:41:14 +00002563 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002564
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002565 // Used with vargs to acumulate store chains.
2566 std::vector<SDValue> OutChains;
2567
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002568 // Assign locations to all of the incoming arguments.
2569 SmallVector<CCValAssign, 16> ArgLocs;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002570 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00002571 getTargetMachine(), ArgLocs, *DAG.getContext());
Daniel Sandersd897b562014-03-27 10:46:12 +00002572 MipsCC MipsCCInfo(CallConv, isO32(), Subtarget->isFP64bit(), CCInfo);
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002573 Function::const_arg_iterator FuncArg =
2574 DAG.getMachineFunction().getFunction()->arg_begin();
Reed Kotlerc03807a2013-08-30 19:40:56 +00002575 bool UseSoftFloat = Subtarget->mipsSEUsesSoftFloat();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002576
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002577 MipsCCInfo.analyzeFormalArguments(Ins, UseSoftFloat, FuncArg);
Akira Hatanaka4866fe12012-10-30 19:37:25 +00002578 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(),
2579 MipsCCInfo.hasByValArg());
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002580
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00002581 unsigned CurArgIdx = 0;
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002582 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002583
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00002584 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002585 CCValAssign &VA = ArgLocs[i];
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00002586 std::advance(FuncArg, Ins[i].OrigArgIndex - CurArgIdx);
2587 CurArgIdx = Ins[i].OrigArgIndex;
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002588 EVT ValVT = VA.getValVT();
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002589 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2590 bool IsRegLoc = VA.isRegLoc();
2591
2592 if (Flags.isByVal()) {
2593 assert(Flags.getByValSize() &&
2594 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002595 assert(ByValArg != MipsCCInfo.byval_end());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002596 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg,
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002597 MipsCCInfo, *ByValArg);
2598 ++ByValArg;
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002599 continue;
2600 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002601
2602 // Arguments stored on registers
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002603 if (IsRegLoc) {
Akira Hatanaka7d822522013-10-28 21:21:36 +00002604 MVT RegVT = VA.getLocVT();
Akira Hatanakacb4a1a82011-05-24 00:23:52 +00002605 unsigned ArgReg = VA.getLocReg();
Akira Hatanaka7d822522013-10-28 21:21:36 +00002606 const TargetRegisterClass *RC = getRegClassFor(RegVT);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002607
Wesley Peck527da1b2010-11-23 03:31:01 +00002608 // Transform the arguments stored on
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002609 // physical registers into virtual ones
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002610 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC);
2611 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
Wesley Peck527da1b2010-11-23 03:31:01 +00002612
2613 // If this is an 8 or 16-bit value, it has been passed promoted
2614 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002615 // truncate to the right size.
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002616 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattner3c049702009-03-26 05:28:14 +00002617 unsigned Opcode = 0;
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002618 if (VA.getLocInfo() == CCValAssign::SExt)
2619 Opcode = ISD::AssertSext;
2620 else if (VA.getLocInfo() == CCValAssign::ZExt)
2621 Opcode = ISD::AssertZext;
Chris Lattner3c049702009-03-26 05:28:14 +00002622 if (Opcode)
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002623 ArgValue = DAG.getNode(Opcode, DL, RegVT, ArgValue,
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002624 DAG.getValueType(ValVT));
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002625 ArgValue = DAG.getNode(ISD::TRUNCATE, DL, ValVT, ArgValue);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002626 }
2627
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002628 // Handle floating point arguments passed in integer registers and
2629 // long double arguments passed in floating point registers.
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002630 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002631 (RegVT == MVT::i64 && ValVT == MVT::f64) ||
2632 (RegVT == MVT::f64 && ValVT == MVT::i64))
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002633 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
Daniel Sandersd897b562014-03-27 10:46:12 +00002634 else if (isO32() && RegVT == MVT::i32 && ValVT == MVT::f64) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002635 unsigned Reg2 = addLiveIn(DAG.getMachineFunction(),
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002636 getNextIntArgReg(ArgReg), RC);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002637 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT);
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002638 if (!Subtarget->isLittle())
2639 std::swap(ArgValue, ArgValue2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002640 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64,
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002641 ArgValue, ArgValue2);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002642 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002643
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002644 InVals.push_back(ArgValue);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002645 } else { // VA.isRegLoc()
2646
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002647 // sanity check
2648 assert(VA.isMemLoc());
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002649
Wesley Peck527da1b2010-11-23 03:31:01 +00002650 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002651 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakacb4a1a82011-05-24 00:23:52 +00002652 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002653
2654 // Create load nodes to retrieve arguments from the stack
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002655 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Akira Hatanakad1c58ed2013-11-09 02:38:51 +00002656 SDValue Load = DAG.getLoad(ValVT, DL, Chain, FIN,
2657 MachinePointerInfo::getFixedStack(FI),
2658 false, false, false, 0);
2659 InVals.push_back(Load);
2660 OutChains.push_back(Load.getValue(1));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002661 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002662
Reid Kleckner79418562014-05-09 22:32:13 +00002663 // The mips ABIs for returning structs by value requires that we copy
2664 // the sret argument into $v0 for the return. Save the argument into
2665 // a virtual register so that we can access it from the return points.
2666 if (Flags.isSRet()) {
2667 unsigned Reg = MipsFI->getSRetReturnReg();
2668 if (!Reg) {
2669 Reg = MF.getRegInfo().createVirtualRegister(
2670 getRegClassFor(isN64() ? MVT::i64 : MVT::i32));
2671 MipsFI->setSRetReturnReg(Reg);
2672 }
2673 SDValue Copy =
2674 DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals.back());
2675 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002676 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002677 }
2678
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002679 if (IsVarArg)
2680 writeVarArgRegs(OutChains, MipsCCInfo, Chain, DL, DAG);
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002681
Wesley Peck527da1b2010-11-23 03:31:01 +00002682 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002683 // the size of Ins and InVals. This only happens when on varg functions
2684 if (!OutChains.empty()) {
2685 OutChains.push_back(Chain);
Craig Topper48d114b2014-04-26 18:35:24 +00002686 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002687 }
2688
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002689 return Chain;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002690}
2691
Akira Hatanakae2489122011-04-15 21:51:11 +00002692//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002693// Return Value Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002694//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002695
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00002696bool
2697MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002698 MachineFunction &MF, bool IsVarArg,
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00002699 const SmallVectorImpl<ISD::OutputArg> &Outs,
2700 LLVMContext &Context) const {
2701 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002702 CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(),
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00002703 RVLocs, Context);
2704 return CCInfo.CheckReturn(Outs, RetCC_Mips);
2705}
2706
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002707SDValue
2708MipsTargetLowering::LowerReturn(SDValue Chain,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002709 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002710 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002711 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002712 SDLoc DL, SelectionDAG &DAG) const {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002713 // CCValAssign - represent the assignment of
2714 // the return value to a location
2715 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002716 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002717
2718 // CCState - Info about the registers and stack slot.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002719 CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(), RVLocs,
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002720 *DAG.getContext());
Daniel Sandersd897b562014-03-27 10:46:12 +00002721 MipsCC MipsCCInfo(CallConv, isO32(), Subtarget->isFP64bit(), CCInfo);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002722
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002723 // Analyze return values.
Reed Kotlerc03807a2013-08-30 19:40:56 +00002724 MipsCCInfo.analyzeReturn(Outs, Subtarget->mipsSEUsesSoftFloat(),
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002725 MF.getFunction()->getReturnType());
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002726
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002727 SDValue Flag;
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00002728 SmallVector<SDValue, 4> RetOps(1, Chain);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002729
2730 // Copy the result values into the output registers.
2731 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002732 SDValue Val = OutVals[i];
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002733 CCValAssign &VA = RVLocs[i];
2734 assert(VA.isRegLoc() && "Can only return in registers!");
2735
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002736 if (RVLocs[i].getValVT() != RVLocs[i].getLocVT())
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002737 Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getLocVT(), Val);
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002738
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002739 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002740
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00002741 // Guarantee that all emitted copies are stuck together with flags.
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002742 Flag = Chain.getValue(1);
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00002743 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002744 }
2745
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002746 // The mips ABIs for returning structs by value requires that we copy
2747 // the sret argument into $v0 for the return. We saved the argument into
2748 // a virtual register in the entry block, so now we copy the value out
2749 // and into $v0.
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002750 if (MF.getFunction()->hasStructRetAttr()) {
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002751 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2752 unsigned Reg = MipsFI->getSRetReturnReg();
2753
Wesley Peck527da1b2010-11-23 03:31:01 +00002754 if (!Reg)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002755 llvm_unreachable("sret virtual register not created in the entry block");
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002756 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Daniel Sandersd897b562014-03-27 10:46:12 +00002757 unsigned V0 = isN64() ? Mips::V0_64 : Mips::V0;
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002758
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002759 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002760 Flag = Chain.getValue(1);
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00002761 RetOps.push_back(DAG.getRegister(V0, getPointerTy()));
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002762 }
2763
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00002764 RetOps[0] = Chain; // Update chain.
Akira Hatanakaefff7b72012-07-10 00:19:06 +00002765
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00002766 // Add the flag if we have it.
2767 if (Flag.getNode())
2768 RetOps.push_back(Flag);
2769
2770 // Return on Mips is always a "jr $ra"
Craig Topper48d114b2014-04-26 18:35:24 +00002771 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, RetOps);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002772}
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002773
Akira Hatanakae2489122011-04-15 21:51:11 +00002774//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002775// Mips Inline Assembly Support
Akira Hatanakae2489122011-04-15 21:51:11 +00002776//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002777
2778/// getConstraintType - Given a constraint letter, return the type of
2779/// constraint it is for this target.
2780MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peck527da1b2010-11-23 03:31:01 +00002781getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002782{
Daniel Sanders8b59af12013-11-12 12:56:01 +00002783 // Mips specific constraints
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002784 // GCC config/mips/constraints.md
2785 //
Wesley Peck527da1b2010-11-23 03:31:01 +00002786 // 'd' : An address register. Equivalent to r
2787 // unless generating MIPS16 code.
2788 // 'y' : Equivalent to r; retained for
2789 // backwards compatibility.
Eric Christophere3c494d2012-05-07 06:25:10 +00002790 // 'c' : A register suitable for use in an indirect
2791 // jump. This will always be $25 for -mabicalls.
Eric Christopher0d8c15d2012-05-07 06:25:19 +00002792 // 'l' : The lo register. 1 word storage.
2793 // 'x' : The hilo register pair. Double word storage.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002794 if (Constraint.size() == 1) {
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002795 switch (Constraint[0]) {
2796 default : break;
Wesley Peck527da1b2010-11-23 03:31:01 +00002797 case 'd':
2798 case 'y':
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002799 case 'f':
Eric Christophere3c494d2012-05-07 06:25:10 +00002800 case 'c':
Eric Christopher9c492e62012-05-07 06:25:15 +00002801 case 'l':
Eric Christopher0d8c15d2012-05-07 06:25:19 +00002802 case 'x':
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002803 return C_RegisterClass;
Jack Carter0e149b02013-03-04 21:33:15 +00002804 case 'R':
2805 return C_Memory;
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002806 }
2807 }
2808 return TargetLowering::getConstraintType(Constraint);
2809}
2810
John Thompsone8360b72010-10-29 17:29:13 +00002811/// Examine constraint type and operand type and determine a weight value.
2812/// This object must already have been set up with the operand type
2813/// and the current alternative constraint selected.
2814TargetLowering::ConstraintWeight
2815MipsTargetLowering::getSingleConstraintMatchWeight(
2816 AsmOperandInfo &info, const char *constraint) const {
2817 ConstraintWeight weight = CW_Invalid;
2818 Value *CallOperandVal = info.CallOperandVal;
2819 // If we don't have a value, we can't do a match,
2820 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +00002821 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +00002822 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +00002823 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +00002824 // Look at the constraint type.
2825 switch (*constraint) {
2826 default:
2827 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2828 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00002829 case 'd':
2830 case 'y':
John Thompsone8360b72010-10-29 17:29:13 +00002831 if (type->isIntegerTy())
2832 weight = CW_Register;
2833 break;
Daniel Sanders8b59af12013-11-12 12:56:01 +00002834 case 'f': // FPU or MSA register
2835 if (Subtarget->hasMSA() && type->isVectorTy() &&
2836 cast<VectorType>(type)->getBitWidth() == 128)
2837 weight = CW_Register;
2838 else if (type->isFloatTy())
John Thompsone8360b72010-10-29 17:29:13 +00002839 weight = CW_Register;
2840 break;
Eric Christophere3c494d2012-05-07 06:25:10 +00002841 case 'c': // $25 for indirect jumps
Eric Christopher9c492e62012-05-07 06:25:15 +00002842 case 'l': // lo register
Eric Christopher0d8c15d2012-05-07 06:25:19 +00002843 case 'x': // hilo register pair
Daniel Sanders8b59af12013-11-12 12:56:01 +00002844 if (type->isIntegerTy())
Eric Christophere3c494d2012-05-07 06:25:10 +00002845 weight = CW_SpecificReg;
Daniel Sanders8b59af12013-11-12 12:56:01 +00002846 break;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00002847 case 'I': // signed 16 bit immediate
Eric Christopher7201e1b2012-05-07 03:13:42 +00002848 case 'J': // integer zero
Eric Christopher3ff88a02012-05-07 05:46:29 +00002849 case 'K': // unsigned 16 bit immediate
Eric Christopher1109b342012-05-07 05:46:37 +00002850 case 'L': // signed 32 bit immediate where lower 16 bits are 0
Eric Christophere07aa432012-05-07 05:46:43 +00002851 case 'N': // immediate in the range of -65535 to -1 (inclusive)
Eric Christopher470578a2012-05-07 05:46:48 +00002852 case 'O': // signed 15 bit immediate (+- 16383)
Eric Christopherc18ae4a2012-05-07 06:25:02 +00002853 case 'P': // immediate in the range of 65535 to 1 (inclusive)
Eric Christopher1d6c89e2012-05-07 03:13:32 +00002854 if (isa<ConstantInt>(CallOperandVal))
2855 weight = CW_Constant;
2856 break;
Jack Carter0e149b02013-03-04 21:33:15 +00002857 case 'R':
2858 weight = CW_Memory;
2859 break;
John Thompsone8360b72010-10-29 17:29:13 +00002860 }
2861 return weight;
2862}
2863
Akira Hatanaka7473b472013-08-14 00:21:25 +00002864/// This is a helper function to parse a physical register string and split it
2865/// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag
2866/// that is returned indicates whether parsing was successful. The second flag
2867/// is true if the numeric part exists.
2868static std::pair<bool, bool>
2869parsePhysicalReg(const StringRef &C, std::string &Prefix,
2870 unsigned long long &Reg) {
2871 if (C.front() != '{' || C.back() != '}')
2872 return std::make_pair(false, false);
2873
2874 // Search for the first numeric character.
2875 StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1;
2876 I = std::find_if(B, E, std::ptr_fun(isdigit));
2877
2878 Prefix.assign(B, I - B);
2879
2880 // The second flag is set to false if no numeric characters were found.
2881 if (I == E)
2882 return std::make_pair(true, false);
2883
2884 // Parse the numeric characters.
2885 return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg),
2886 true);
2887}
2888
2889std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering::
2890parseRegForInlineAsmConstraint(const StringRef &C, MVT VT) const {
2891 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2892 const TargetRegisterClass *RC;
2893 std::string Prefix;
2894 unsigned long long Reg;
2895
2896 std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg);
2897
2898 if (!R.first)
Craig Topper062a2ba2014-04-25 05:30:21 +00002899 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00002900
2901 if ((Prefix == "hi" || Prefix == "lo")) { // Parse hi/lo.
2902 // No numeric characters follow "hi" or "lo".
2903 if (R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00002904 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00002905
2906 RC = TRI->getRegClass(Prefix == "hi" ?
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00002907 Mips::HI32RegClassID : Mips::LO32RegClassID);
Akira Hatanaka7473b472013-08-14 00:21:25 +00002908 return std::make_pair(*(RC->begin()), RC);
Daniel Sanders8b59af12013-11-12 12:56:01 +00002909 } else if (Prefix.compare(0, 4, "$msa") == 0) {
2910 // Parse $msa(ir|csr|access|save|modify|request|map|unmap)
2911
2912 // No numeric characters follow the name.
2913 if (R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00002914 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00002915
2916 Reg = StringSwitch<unsigned long long>(Prefix)
2917 .Case("$msair", Mips::MSAIR)
2918 .Case("$msacsr", Mips::MSACSR)
2919 .Case("$msaaccess", Mips::MSAAccess)
2920 .Case("$msasave", Mips::MSASave)
2921 .Case("$msamodify", Mips::MSAModify)
2922 .Case("$msarequest", Mips::MSARequest)
2923 .Case("$msamap", Mips::MSAMap)
2924 .Case("$msaunmap", Mips::MSAUnmap)
2925 .Default(0);
2926
2927 if (!Reg)
Craig Topper062a2ba2014-04-25 05:30:21 +00002928 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00002929
2930 RC = TRI->getRegClass(Mips::MSACtrlRegClassID);
2931 return std::make_pair(Reg, RC);
Akira Hatanaka7473b472013-08-14 00:21:25 +00002932 }
2933
2934 if (!R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00002935 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00002936
2937 if (Prefix == "$f") { // Parse $f0-$f31.
2938 // If the size of FP registers is 64-bit or Reg is an even number, select
2939 // the 64-bit register class. Otherwise, select the 32-bit register class.
2940 if (VT == MVT::Other)
2941 VT = (Subtarget->isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32;
2942
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00002943 RC = getRegClassFor(VT);
Akira Hatanaka7473b472013-08-14 00:21:25 +00002944
2945 if (RC == &Mips::AFGR64RegClass) {
2946 assert(Reg % 2 == 0);
2947 Reg >>= 1;
2948 }
Daniel Sanders8b59af12013-11-12 12:56:01 +00002949 } else if (Prefix == "$fcc") // Parse $fcc0-$fcc7.
Akira Hatanaka7473b472013-08-14 00:21:25 +00002950 RC = TRI->getRegClass(Mips::FCCRegClassID);
Daniel Sanders8b59af12013-11-12 12:56:01 +00002951 else if (Prefix == "$w") { // Parse $w0-$w31.
2952 RC = getRegClassFor((VT == MVT::Other) ? MVT::v16i8 : VT);
Akira Hatanaka7473b472013-08-14 00:21:25 +00002953 } else { // Parse $0-$31.
2954 assert(Prefix == "$");
2955 RC = getRegClassFor((VT == MVT::Other) ? MVT::i32 : VT);
2956 }
2957
2958 assert(Reg < RC->getNumRegs());
2959 return std::make_pair(*(RC->begin() + Reg), RC);
2960}
2961
Eric Christophereaf77dc2011-06-29 19:33:04 +00002962/// Given a register class constraint, like 'r', if this corresponds directly
2963/// to an LLVM register class, return a register of 0 and the register class
2964/// pointer.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002965std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Chad Rosier295bd432013-06-22 18:37:38 +00002966getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002967{
2968 if (Constraint.size() == 1) {
2969 switch (Constraint[0]) {
Eric Christopher9519c082011-06-29 19:04:31 +00002970 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
2971 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002972 case 'r':
Akira Hatanaka92a96e12012-09-12 23:27:55 +00002973 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
2974 if (Subtarget->inMips16Mode())
2975 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00002976 return std::make_pair(0U, &Mips::GPR32RegClass);
Akira Hatanaka92a96e12012-09-12 23:27:55 +00002977 }
Daniel Sanders5e94e682014-03-27 16:42:17 +00002978 if (VT == MVT::i64 && !isGP64bit())
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00002979 return std::make_pair(0U, &Mips::GPR32RegClass);
Daniel Sanders5e94e682014-03-27 16:42:17 +00002980 if (VT == MVT::i64 && isGP64bit())
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00002981 return std::make_pair(0U, &Mips::GPR64RegClass);
Eric Christopher58daf042012-05-07 03:13:22 +00002982 // This will generate an error message
Craig Topper062a2ba2014-04-25 05:30:21 +00002983 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00002984 case 'f': // FPU or MSA register
2985 if (VT == MVT::v16i8)
2986 return std::make_pair(0U, &Mips::MSA128BRegClass);
2987 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
2988 return std::make_pair(0U, &Mips::MSA128HRegClass);
2989 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2990 return std::make_pair(0U, &Mips::MSA128WRegClass);
2991 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2992 return std::make_pair(0U, &Mips::MSA128DRegClass);
2993 else if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00002994 return std::make_pair(0U, &Mips::FGR32RegClass);
Daniel Sanders8b59af12013-11-12 12:56:01 +00002995 else if ((VT == MVT::f64) && (!Subtarget->isSingleFloat())) {
Akira Hatanakac669d7a2012-01-04 02:45:01 +00002996 if (Subtarget->isFP64bit())
Craig Topperc7242e02012-04-20 07:30:17 +00002997 return std::make_pair(0U, &Mips::FGR64RegClass);
2998 return std::make_pair(0U, &Mips::AFGR64RegClass);
Akira Hatanakac669d7a2012-01-04 02:45:01 +00002999 }
Eric Christophere3c494d2012-05-07 06:25:10 +00003000 break;
3001 case 'c': // register suitable for indirect jump
3002 if (VT == MVT::i32)
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003003 return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass);
Eric Christophere3c494d2012-05-07 06:25:10 +00003004 assert(VT == MVT::i64 && "Unexpected type.");
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003005 return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass);
Eric Christopher9c492e62012-05-07 06:25:15 +00003006 case 'l': // register suitable for indirect jump
3007 if (VT == MVT::i32)
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00003008 return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass);
3009 return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass);
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003010 case 'x': // register suitable for indirect jump
3011 // Fixme: Not triggering the use of both hi and low
3012 // This will generate an error message
Craig Topper062a2ba2014-04-25 05:30:21 +00003013 return std::make_pair(0U, nullptr);
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003014 }
3015 }
Akira Hatanaka7473b472013-08-14 00:21:25 +00003016
3017 std::pair<unsigned, const TargetRegisterClass *> R;
3018 R = parseRegForInlineAsmConstraint(Constraint, VT);
3019
3020 if (R.second)
3021 return R;
3022
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003023 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3024}
3025
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003026/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3027/// vector. If it is invalid, don't add anything to Ops.
3028void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3029 std::string &Constraint,
3030 std::vector<SDValue>&Ops,
3031 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +00003032 SDValue Result;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003033
3034 // Only support length 1 constraints for now.
3035 if (Constraint.length() > 1) return;
3036
3037 char ConstraintLetter = Constraint[0];
3038 switch (ConstraintLetter) {
3039 default: break; // This will fall through to the generic implementation
3040 case 'I': // Signed 16 bit constant
3041 // If this fails, the parent routine will give an error
3042 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3043 EVT Type = Op.getValueType();
3044 int64_t Val = C->getSExtValue();
3045 if (isInt<16>(Val)) {
3046 Result = DAG.getTargetConstant(Val, Type);
3047 break;
3048 }
3049 }
3050 return;
Eric Christopher7201e1b2012-05-07 03:13:42 +00003051 case 'J': // integer zero
3052 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3053 EVT Type = Op.getValueType();
3054 int64_t Val = C->getZExtValue();
3055 if (Val == 0) {
3056 Result = DAG.getTargetConstant(0, Type);
3057 break;
3058 }
3059 }
3060 return;
Eric Christopher3ff88a02012-05-07 05:46:29 +00003061 case 'K': // unsigned 16 bit immediate
3062 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3063 EVT Type = Op.getValueType();
3064 uint64_t Val = (uint64_t)C->getZExtValue();
3065 if (isUInt<16>(Val)) {
3066 Result = DAG.getTargetConstant(Val, Type);
3067 break;
3068 }
3069 }
3070 return;
Eric Christopher1109b342012-05-07 05:46:37 +00003071 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3072 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3073 EVT Type = Op.getValueType();
3074 int64_t Val = C->getSExtValue();
3075 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
3076 Result = DAG.getTargetConstant(Val, Type);
3077 break;
3078 }
3079 }
3080 return;
Eric Christophere07aa432012-05-07 05:46:43 +00003081 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3082 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3083 EVT Type = Op.getValueType();
3084 int64_t Val = C->getSExtValue();
3085 if ((Val >= -65535) && (Val <= -1)) {
3086 Result = DAG.getTargetConstant(Val, Type);
3087 break;
3088 }
3089 }
3090 return;
Eric Christopher470578a2012-05-07 05:46:48 +00003091 case 'O': // signed 15 bit immediate
3092 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3093 EVT Type = Op.getValueType();
3094 int64_t Val = C->getSExtValue();
3095 if ((isInt<15>(Val))) {
3096 Result = DAG.getTargetConstant(Val, Type);
3097 break;
3098 }
3099 }
3100 return;
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003101 case 'P': // immediate in the range of 1 to 65535 (inclusive)
3102 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3103 EVT Type = Op.getValueType();
3104 int64_t Val = C->getSExtValue();
3105 if ((Val <= 65535) && (Val >= 1)) {
3106 Result = DAG.getTargetConstant(Val, Type);
3107 break;
3108 }
3109 }
3110 return;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003111 }
3112
3113 if (Result.getNode()) {
3114 Ops.push_back(Result);
3115 return;
3116 }
3117
3118 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3119}
3120
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003121bool MipsTargetLowering::isLegalAddressingMode(const AddrMode &AM,
3122 Type *Ty) const {
Akira Hatanakaef839192012-11-17 00:25:41 +00003123 // No global is ever allowed as a base.
3124 if (AM.BaseGV)
3125 return false;
3126
3127 switch (AM.Scale) {
3128 case 0: // "r+i" or just "i", depending on HasBaseReg.
3129 break;
3130 case 1:
3131 if (!AM.HasBaseReg) // allow "r+i".
3132 break;
3133 return false; // disallow "r+r" or "r+r+i".
3134 default:
3135 return false;
3136 }
3137
3138 return true;
3139}
3140
3141bool
Dan Gohman2fe6bee2008-10-18 02:06:02 +00003142MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3143 // The Mips target isn't yet aware of offsets.
3144 return false;
3145}
Evan Cheng16993aa2009-10-27 19:56:55 +00003146
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003147EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003148 unsigned SrcAlign,
3149 bool IsMemset, bool ZeroMemset,
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003150 bool MemcpyStrSrc,
3151 MachineFunction &MF) const {
3152 if (Subtarget->hasMips64())
3153 return MVT::i64;
3154
3155 return MVT::i32;
3156}
3157
Evan Cheng83896a52009-10-28 01:43:28 +00003158bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
3159 if (VT != MVT::f32 && VT != MVT::f64)
3160 return false;
Bruno Cardoso Lopesb02a9df2011-01-18 19:41:41 +00003161 if (Imm.isNegZero())
3162 return false;
Evan Cheng16993aa2009-10-27 19:56:55 +00003163 return Imm.isZero();
3164}
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003165
3166unsigned MipsTargetLowering::getJumpTableEncoding() const {
Daniel Sandersd897b562014-03-27 10:46:12 +00003167 if (isN64())
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003168 return MachineJumpTableInfo::EK_GPRel64BlockAddress;
Jia Liuf54f60f2012-02-28 07:46:26 +00003169
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003170 return TargetLowering::getJumpTableEncoding();
3171}
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003172
Akira Hatanakae092f722013-03-05 22:54:59 +00003173/// This function returns true if CallSym is a long double emulation routine.
3174static bool isF128SoftLibCall(const char *CallSym) {
3175 const char *const LibCalls[] =
3176 {"__addtf3", "__divtf3", "__eqtf2", "__extenddftf2", "__extendsftf2",
3177 "__fixtfdi", "__fixtfsi", "__fixtfti", "__fixunstfdi", "__fixunstfsi",
3178 "__fixunstfti", "__floatditf", "__floatsitf", "__floattitf",
3179 "__floatunditf", "__floatunsitf", "__floatuntitf", "__getf2", "__gttf2",
3180 "__letf2", "__lttf2", "__multf3", "__netf2", "__powitf2", "__subtf3",
3181 "__trunctfdf2", "__trunctfsf2", "__unordtf2",
3182 "ceill", "copysignl", "cosl", "exp2l", "expl", "floorl", "fmal", "fmodl",
3183 "log10l", "log2l", "logl", "nearbyintl", "powl", "rintl", "sinl", "sqrtl",
3184 "truncl"};
3185
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003186 const char *const *End = LibCalls + array_lengthof(LibCalls);
Akira Hatanakae092f722013-03-05 22:54:59 +00003187
3188 // Check that LibCalls is sorted alphabetically.
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003189 MipsTargetLowering::LTStr Comp;
Akira Hatanakae092f722013-03-05 22:54:59 +00003190
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003191#ifndef NDEBUG
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003192 for (const char *const *I = LibCalls; I < End - 1; ++I)
Akira Hatanakae092f722013-03-05 22:54:59 +00003193 assert(Comp(*I, *(I + 1)));
3194#endif
3195
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003196 return std::binary_search(LibCalls, End, CallSym, Comp);
Akira Hatanakae092f722013-03-05 22:54:59 +00003197}
3198
3199/// This function returns true if Ty is fp128 or i128 which was originally a
3200/// fp128.
3201static bool originalTypeIsF128(const Type *Ty, const SDNode *CallNode) {
3202 if (Ty->isFP128Ty())
3203 return true;
3204
3205 const ExternalSymbolSDNode *ES =
3206 dyn_cast_or_null<const ExternalSymbolSDNode>(CallNode);
3207
3208 // If the Ty is i128 and the function being called is a long double emulation
3209 // routine, then the original type is f128.
3210 return (ES && Ty->isIntegerTy(128) && isF128SoftLibCall(ES->getSymbol()));
3211}
3212
Reed Kotler783c7942013-05-10 22:25:39 +00003213MipsTargetLowering::MipsCC::SpecialCallingConvType
3214 MipsTargetLowering::getSpecialCallingConv(SDValue Callee) const {
3215 MipsCC::SpecialCallingConvType SpecialCallingConv =
Alp Toker98444342014-04-19 23:56:35 +00003216 MipsCC::NoSpecialCallingConv;
Reed Kotler783c7942013-05-10 22:25:39 +00003217 if (Subtarget->inMips16HardFloat()) {
3218 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3219 llvm::StringRef Sym = G->getGlobal()->getName();
3220 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
Reed Kotler3230e722013-12-12 02:41:11 +00003221 if (F && F->hasFnAttribute("__Mips16RetHelper")) {
Reed Kotler783c7942013-05-10 22:25:39 +00003222 SpecialCallingConv = MipsCC::Mips16RetHelperConv;
3223 }
3224 }
3225 }
3226 return SpecialCallingConv;
3227}
3228
3229MipsTargetLowering::MipsCC::MipsCC(
Akira Hatanakabfb66242013-08-20 23:38:40 +00003230 CallingConv::ID CC, bool IsO32_, bool IsFP64_, CCState &Info,
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003231 MipsCC::SpecialCallingConvType SpecialCallingConv_)
Akira Hatanakabfb66242013-08-20 23:38:40 +00003232 : CCInfo(Info), CallConv(CC), IsO32(IsO32_), IsFP64(IsFP64_),
Reed Kotler783c7942013-05-10 22:25:39 +00003233 SpecialCallingConv(SpecialCallingConv_){
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003234 // Pre-allocate reserved argument area.
Akira Hatanaka5001be52013-02-15 21:45:11 +00003235 CCInfo.AllocateStack(reservedArgArea(), 1);
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003236}
3237
Reed Kotler783c7942013-05-10 22:25:39 +00003238
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003239void MipsTargetLowering::MipsCC::
Akira Hatanaka5001be52013-02-15 21:45:11 +00003240analyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Args,
Akira Hatanaka3b7391d2013-03-05 22:20:28 +00003241 bool IsVarArg, bool IsSoftFloat, const SDNode *CallNode,
3242 std::vector<ArgListEntry> &FuncArgs) {
Akira Hatanaka5001be52013-02-15 21:45:11 +00003243 assert((CallConv != CallingConv::Fast || !IsVarArg) &&
3244 "CallingConv::Fast shouldn't be used for vararg functions.");
3245
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003246 unsigned NumOpnds = Args.size();
Akira Hatanaka5001be52013-02-15 21:45:11 +00003247 llvm::CCAssignFn *FixedFn = fixedArgFn(), *VarFn = varArgFn();
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003248
3249 for (unsigned I = 0; I != NumOpnds; ++I) {
3250 MVT ArgVT = Args[I].VT;
3251 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
3252 bool R;
3253
3254 if (ArgFlags.isByVal()) {
3255 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
3256 continue;
3257 }
3258
Akira Hatanaka5001be52013-02-15 21:45:11 +00003259 if (IsVarArg && !Args[I].IsFixed)
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003260 R = VarFn(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
Akira Hatanaka3b7391d2013-03-05 22:20:28 +00003261 else {
3262 MVT RegVT = getRegVT(ArgVT, FuncArgs[Args[I].OrigArgIndex].Ty, CallNode,
3263 IsSoftFloat);
3264 R = FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo);
3265 }
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003266
3267 if (R) {
3268#ifndef NDEBUG
3269 dbgs() << "Call operand #" << I << " has unhandled type "
3270 << EVT(ArgVT).getEVTString();
3271#endif
Craig Toppere73658d2014-04-28 04:05:08 +00003272 llvm_unreachable(nullptr);
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003273 }
3274 }
3275}
3276
3277void MipsTargetLowering::MipsCC::
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003278analyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Args,
3279 bool IsSoftFloat, Function::const_arg_iterator FuncArg) {
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003280 unsigned NumArgs = Args.size();
Akira Hatanaka5001be52013-02-15 21:45:11 +00003281 llvm::CCAssignFn *FixedFn = fixedArgFn();
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003282 unsigned CurArgIdx = 0;
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003283
3284 for (unsigned I = 0; I != NumArgs; ++I) {
3285 MVT ArgVT = Args[I].VT;
3286 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003287 std::advance(FuncArg, Args[I].OrigArgIndex - CurArgIdx);
3288 CurArgIdx = Args[I].OrigArgIndex;
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003289
3290 if (ArgFlags.isByVal()) {
3291 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
3292 continue;
3293 }
3294
Craig Topper062a2ba2014-04-25 05:30:21 +00003295 MVT RegVT = getRegVT(ArgVT, FuncArg->getType(), nullptr, IsSoftFloat);
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003296
3297 if (!FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo))
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003298 continue;
3299
3300#ifndef NDEBUG
3301 dbgs() << "Formal Arg #" << I << " has unhandled type "
3302 << EVT(ArgVT).getEVTString();
3303#endif
Craig Toppere73658d2014-04-28 04:05:08 +00003304 llvm_unreachable(nullptr);
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003305 }
3306}
3307
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003308template<typename Ty>
3309void MipsTargetLowering::MipsCC::
3310analyzeReturn(const SmallVectorImpl<Ty> &RetVals, bool IsSoftFloat,
3311 const SDNode *CallNode, const Type *RetTy) const {
Akira Hatanakae092f722013-03-05 22:54:59 +00003312 CCAssignFn *Fn;
3313
3314 if (IsSoftFloat && originalTypeIsF128(RetTy, CallNode))
3315 Fn = RetCC_F128Soft;
3316 else
3317 Fn = RetCC_Mips;
3318
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003319 for (unsigned I = 0, E = RetVals.size(); I < E; ++I) {
3320 MVT VT = RetVals[I].VT;
3321 ISD::ArgFlagsTy Flags = RetVals[I].Flags;
3322 MVT RegVT = this->getRegVT(VT, RetTy, CallNode, IsSoftFloat);
3323
Akira Hatanakae092f722013-03-05 22:54:59 +00003324 if (Fn(I, VT, RegVT, CCValAssign::Full, Flags, this->CCInfo)) {
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003325#ifndef NDEBUG
3326 dbgs() << "Call result #" << I << " has unhandled type "
3327 << EVT(VT).getEVTString() << '\n';
3328#endif
Craig Toppere73658d2014-04-28 04:05:08 +00003329 llvm_unreachable(nullptr);
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003330 }
3331 }
3332}
3333
3334void MipsTargetLowering::MipsCC::
3335analyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins, bool IsSoftFloat,
3336 const SDNode *CallNode, const Type *RetTy) const {
3337 analyzeReturn(Ins, IsSoftFloat, CallNode, RetTy);
3338}
3339
3340void MipsTargetLowering::MipsCC::
3341analyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs, bool IsSoftFloat,
3342 const Type *RetTy) const {
Craig Topper062a2ba2014-04-25 05:30:21 +00003343 analyzeReturn(Outs, IsSoftFloat, nullptr, RetTy);
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003344}
3345
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003346void MipsTargetLowering::MipsCC::handleByValArg(unsigned ValNo, MVT ValVT,
3347 MVT LocVT,
3348 CCValAssign::LocInfo LocInfo,
3349 ISD::ArgFlagsTy ArgFlags) {
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003350 assert(ArgFlags.getByValSize() && "Byval argument's size shouldn't be 0.");
3351
3352 struct ByValArgInfo ByVal;
Akira Hatanaka5001be52013-02-15 21:45:11 +00003353 unsigned RegSize = regSize();
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003354 unsigned ByValSize = RoundUpToAlignment(ArgFlags.getByValSize(), RegSize);
3355 unsigned Align = std::min(std::max(ArgFlags.getByValAlign(), RegSize),
3356 RegSize * 2);
3357
Akira Hatanaka5001be52013-02-15 21:45:11 +00003358 if (useRegsForByval())
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003359 allocateRegs(ByVal, ByValSize, Align);
3360
3361 // Allocate space on caller's stack.
3362 ByVal.Address = CCInfo.AllocateStack(ByValSize - RegSize * ByVal.NumRegs,
3363 Align);
3364 CCInfo.addLoc(CCValAssign::getMem(ValNo, ValVT, ByVal.Address, LocVT,
3365 LocInfo));
3366 ByValArgs.push_back(ByVal);
3367}
3368
Akira Hatanaka5001be52013-02-15 21:45:11 +00003369unsigned MipsTargetLowering::MipsCC::numIntArgRegs() const {
3370 return IsO32 ? array_lengthof(O32IntRegs) : array_lengthof(Mips64IntRegs);
3371}
3372
3373unsigned MipsTargetLowering::MipsCC::reservedArgArea() const {
3374 return (IsO32 && (CallConv != CallingConv::Fast)) ? 16 : 0;
3375}
3376
Craig Topper840beec2014-04-04 05:16:06 +00003377const MCPhysReg *MipsTargetLowering::MipsCC::intArgRegs() const {
Akira Hatanaka5001be52013-02-15 21:45:11 +00003378 return IsO32 ? O32IntRegs : Mips64IntRegs;
3379}
3380
3381llvm::CCAssignFn *MipsTargetLowering::MipsCC::fixedArgFn() const {
3382 if (CallConv == CallingConv::Fast)
3383 return CC_Mips_FastCC;
3384
Reed Kotler783c7942013-05-10 22:25:39 +00003385 if (SpecialCallingConv == Mips16RetHelperConv)
3386 return CC_Mips16RetHelper;
Akira Hatanakabfb66242013-08-20 23:38:40 +00003387 return IsO32 ? (IsFP64 ? CC_MipsO32_FP64 : CC_MipsO32_FP32) : CC_MipsN;
Akira Hatanaka5001be52013-02-15 21:45:11 +00003388}
3389
3390llvm::CCAssignFn *MipsTargetLowering::MipsCC::varArgFn() const {
Akira Hatanakabfb66242013-08-20 23:38:40 +00003391 return IsO32 ? (IsFP64 ? CC_MipsO32_FP64 : CC_MipsO32_FP32) : CC_MipsN_VarArg;
Akira Hatanaka5001be52013-02-15 21:45:11 +00003392}
3393
Craig Topper840beec2014-04-04 05:16:06 +00003394const MCPhysReg *MipsTargetLowering::MipsCC::shadowRegs() const {
Akira Hatanaka5001be52013-02-15 21:45:11 +00003395 return IsO32 ? O32IntRegs : Mips64DPRegs;
3396}
3397
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003398void MipsTargetLowering::MipsCC::allocateRegs(ByValArgInfo &ByVal,
3399 unsigned ByValSize,
3400 unsigned Align) {
Akira Hatanaka5001be52013-02-15 21:45:11 +00003401 unsigned RegSize = regSize(), NumIntArgRegs = numIntArgRegs();
Craig Topper840beec2014-04-04 05:16:06 +00003402 const MCPhysReg *IntArgRegs = intArgRegs(), *ShadowRegs = shadowRegs();
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003403 assert(!(ByValSize % RegSize) && !(Align % RegSize) &&
3404 "Byval argument's size and alignment should be a multiple of"
3405 "RegSize.");
3406
3407 ByVal.FirstIdx = CCInfo.getFirstUnallocated(IntArgRegs, NumIntArgRegs);
3408
3409 // If Align > RegSize, the first arg register must be even.
3410 if ((Align > RegSize) && (ByVal.FirstIdx % 2)) {
3411 CCInfo.AllocateReg(IntArgRegs[ByVal.FirstIdx], ShadowRegs[ByVal.FirstIdx]);
3412 ++ByVal.FirstIdx;
3413 }
3414
3415 // Mark the registers allocated.
3416 for (unsigned I = ByVal.FirstIdx; ByValSize && (I < NumIntArgRegs);
3417 ByValSize -= RegSize, ++I, ++ByVal.NumRegs)
3418 CCInfo.AllocateReg(IntArgRegs[I], ShadowRegs[I]);
3419}
Akira Hatanaka25dad192012-10-27 00:10:18 +00003420
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003421MVT MipsTargetLowering::MipsCC::getRegVT(MVT VT, const Type *OrigTy,
3422 const SDNode *CallNode,
3423 bool IsSoftFloat) const {
3424 if (IsSoftFloat || IsO32)
3425 return VT;
3426
3427 // Check if the original type was fp128.
Akira Hatanakae092f722013-03-05 22:54:59 +00003428 if (originalTypeIsF128(OrigTy, CallNode)) {
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003429 assert(VT == MVT::i64);
3430 return MVT::f64;
3431 }
3432
3433 return VT;
3434}
3435
Akira Hatanaka25dad192012-10-27 00:10:18 +00003436void MipsTargetLowering::
Andrew Trickef9de2a2013-05-25 02:42:55 +00003437copyByValRegs(SDValue Chain, SDLoc DL, std::vector<SDValue> &OutChains,
Akira Hatanaka25dad192012-10-27 00:10:18 +00003438 SelectionDAG &DAG, const ISD::ArgFlagsTy &Flags,
3439 SmallVectorImpl<SDValue> &InVals, const Argument *FuncArg,
3440 const MipsCC &CC, const ByValArgInfo &ByVal) const {
3441 MachineFunction &MF = DAG.getMachineFunction();
3442 MachineFrameInfo *MFI = MF.getFrameInfo();
3443 unsigned RegAreaSize = ByVal.NumRegs * CC.regSize();
3444 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
3445 int FrameObjOffset;
3446
3447 if (RegAreaSize)
3448 FrameObjOffset = (int)CC.reservedArgArea() -
3449 (int)((CC.numIntArgRegs() - ByVal.FirstIdx) * CC.regSize());
3450 else
3451 FrameObjOffset = ByVal.Address;
3452
3453 // Create frame object.
3454 EVT PtrTy = getPointerTy();
3455 int FI = MFI->CreateFixedObject(FrameObjSize, FrameObjOffset, true);
3456 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
3457 InVals.push_back(FIN);
3458
3459 if (!ByVal.NumRegs)
3460 return;
3461
3462 // Copy arg registers.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00003463 MVT RegTy = MVT::getIntegerVT(CC.regSize() * 8);
Akira Hatanaka25dad192012-10-27 00:10:18 +00003464 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3465
3466 for (unsigned I = 0; I < ByVal.NumRegs; ++I) {
3467 unsigned ArgReg = CC.intArgRegs()[ByVal.FirstIdx + I];
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003468 unsigned VReg = addLiveIn(MF, ArgReg, RC);
Akira Hatanaka25dad192012-10-27 00:10:18 +00003469 unsigned Offset = I * CC.regSize();
3470 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
3471 DAG.getConstant(Offset, PtrTy));
3472 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
3473 StorePtr, MachinePointerInfo(FuncArg, Offset),
3474 false, false, 0);
3475 OutChains.push_back(Store);
3476 }
3477}
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003478
3479// Copy byVal arg to registers and stack.
3480void MipsTargetLowering::
Andrew Trickef9de2a2013-05-25 02:42:55 +00003481passByValArg(SDValue Chain, SDLoc DL,
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00003482 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
Craig Topperb94011f2013-07-14 04:42:23 +00003483 SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr,
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003484 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
3485 const MipsCC &CC, const ByValArgInfo &ByVal,
3486 const ISD::ArgFlagsTy &Flags, bool isLittle) const {
3487 unsigned ByValSize = Flags.getByValSize();
3488 unsigned Offset = 0; // Offset in # of bytes from the beginning of struct.
3489 unsigned RegSize = CC.regSize();
3490 unsigned Alignment = std::min(Flags.getByValAlign(), RegSize);
3491 EVT PtrTy = getPointerTy(), RegTy = MVT::getIntegerVT(RegSize * 8);
3492
3493 if (ByVal.NumRegs) {
Craig Topper840beec2014-04-04 05:16:06 +00003494 const MCPhysReg *ArgRegs = CC.intArgRegs();
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003495 bool LeftoverBytes = (ByVal.NumRegs * RegSize > ByValSize);
3496 unsigned I = 0;
3497
3498 // Copy words to registers.
3499 for (; I < ByVal.NumRegs - LeftoverBytes; ++I, Offset += RegSize) {
3500 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3501 DAG.getConstant(Offset, PtrTy));
3502 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
3503 MachinePointerInfo(), false, false, false,
3504 Alignment);
3505 MemOpChains.push_back(LoadVal.getValue(1));
3506 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3507 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
3508 }
3509
3510 // Return if the struct has been fully copied.
3511 if (ByValSize == Offset)
3512 return;
3513
3514 // Copy the remainder of the byval argument with sub-word loads and shifts.
3515 if (LeftoverBytes) {
3516 assert((ByValSize > Offset) && (ByValSize < Offset + RegSize) &&
3517 "Size of the remainder should be smaller than RegSize.");
3518 SDValue Val;
3519
3520 for (unsigned LoadSize = RegSize / 2, TotalSizeLoaded = 0;
3521 Offset < ByValSize; LoadSize /= 2) {
3522 unsigned RemSize = ByValSize - Offset;
3523
3524 if (RemSize < LoadSize)
3525 continue;
3526
3527 // Load subword.
3528 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3529 DAG.getConstant(Offset, PtrTy));
3530 SDValue LoadVal =
3531 DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr,
3532 MachinePointerInfo(), MVT::getIntegerVT(LoadSize * 8),
3533 false, false, Alignment);
3534 MemOpChains.push_back(LoadVal.getValue(1));
3535
3536 // Shift the loaded value.
3537 unsigned Shamt;
3538
3539 if (isLittle)
3540 Shamt = TotalSizeLoaded;
3541 else
3542 Shamt = (RegSize - (TotalSizeLoaded + LoadSize)) * 8;
3543
3544 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
3545 DAG.getConstant(Shamt, MVT::i32));
3546
3547 if (Val.getNode())
3548 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
3549 else
3550 Val = Shift;
3551
3552 Offset += LoadSize;
3553 TotalSizeLoaded += LoadSize;
3554 Alignment = std::min(Alignment, LoadSize);
3555 }
3556
3557 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3558 RegsToPass.push_back(std::make_pair(ArgReg, Val));
3559 return;
3560 }
3561 }
3562
3563 // Copy remainder of byval arg to it with memcpy.
3564 unsigned MemCpySize = ByValSize - Offset;
3565 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3566 DAG.getConstant(Offset, PtrTy));
3567 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
3568 DAG.getIntPtrConstant(ByVal.Address));
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003569 Chain = DAG.getMemcpy(Chain, DL, Dst, Src, DAG.getConstant(MemCpySize, PtrTy),
3570 Alignment, /*isVolatile=*/false, /*AlwaysInline=*/false,
Nick Lewyckyaad475b2014-04-15 07:22:52 +00003571 MachinePointerInfo(), MachinePointerInfo());
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003572 MemOpChains.push_back(Chain);
3573}
Akira Hatanaka2a134022012-10-27 00:21:13 +00003574
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003575void MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
3576 const MipsCC &CC, SDValue Chain,
3577 SDLoc DL, SelectionDAG &DAG) const {
Akira Hatanaka2a134022012-10-27 00:21:13 +00003578 unsigned NumRegs = CC.numIntArgRegs();
Craig Topper840beec2014-04-04 05:16:06 +00003579 const MCPhysReg *ArgRegs = CC.intArgRegs();
Akira Hatanaka2a134022012-10-27 00:21:13 +00003580 const CCState &CCInfo = CC.getCCInfo();
3581 unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs, NumRegs);
3582 unsigned RegSize = CC.regSize();
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00003583 MVT RegTy = MVT::getIntegerVT(RegSize * 8);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003584 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3585 MachineFunction &MF = DAG.getMachineFunction();
3586 MachineFrameInfo *MFI = MF.getFrameInfo();
3587 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3588
3589 // Offset of the first variable argument from stack pointer.
3590 int VaArgOffset;
3591
3592 if (NumRegs == Idx)
3593 VaArgOffset = RoundUpToAlignment(CCInfo.getNextStackOffset(), RegSize);
3594 else
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003595 VaArgOffset = (int)CC.reservedArgArea() - (int)(RegSize * (NumRegs - Idx));
Akira Hatanaka2a134022012-10-27 00:21:13 +00003596
3597 // Record the frame index of the first variable argument
3598 // which is a value necessary to VASTART.
3599 int FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
3600 MipsFI->setVarArgsFrameIndex(FI);
3601
3602 // Copy the integer registers that have not been used for argument passing
3603 // to the argument register save area. For O32, the save area is allocated
3604 // in the caller's stack frame, while for N32/64, it is allocated in the
3605 // callee's stack frame.
3606 for (unsigned I = Idx; I < NumRegs; ++I, VaArgOffset += RegSize) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003607 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003608 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
3609 FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
3610 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
3611 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
3612 MachinePointerInfo(), false, false, 0);
Craig Topper062a2ba2014-04-25 05:30:21 +00003613 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue((Value*)nullptr);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003614 OutChains.push_back(Store);
3615 }
3616}