blob: f3a6910fa3cdbe84cf94fac8ad2626d003ce0c74 [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
Daniel Sanders070fd1c2014-05-12 12:41:59 +0000360 // MIPS16 lacks MIPS32's clz and clo instructions.
361 if (!Subtarget->hasMips32() || Subtarget->inMips16Mode())
Owen Anderson9f944592009-08-11 20:47:22 +0000362 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Daniel Sanders070fd1c2014-05-12 12:41:59 +0000363 if (!Subtarget->hasMips64())
Akira Hatanaka1d8efab2011-12-21 00:20:27 +0000364 setOperationAction(ISD::CTLZ, MVT::i64, Expand);
Bruno Cardoso Lopes93da7e62008-08-08 06:16:31 +0000365
Daniel Sanders39d00512014-05-12 12:15:41 +0000366 if (!Subtarget->hasMips32r2())
Owen Anderson9f944592009-08-11 20:47:22 +0000367 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Daniel Sanders39d00512014-05-12 12:15:41 +0000368 if (!Subtarget->hasMips64r2())
Akira Hatanaka4706ac92011-12-20 23:56:43 +0000369 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000370
Daniel Sanders3d849352014-04-14 15:44:42 +0000371 if (isGP64bit()) {
Akira Hatanaka019e5922012-06-02 00:04:42 +0000372 setLoadExtAction(ISD::SEXTLOAD, MVT::i32, Custom);
373 setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, Custom);
374 setLoadExtAction(ISD::EXTLOAD, MVT::i32, Custom);
375 setTruncStoreAction(MVT::i64, MVT::i32, Custom);
376 }
377
Akira Hatanakaa3d9ab92013-07-26 20:58:55 +0000378 setOperationAction(ISD::TRAP, MVT::Other, Legal);
379
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000380 setTargetDAGCombine(ISD::SDIVREM);
381 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka5e152182012-03-08 03:26:37 +0000382 setTargetDAGCombine(ISD::SELECT);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000383 setTargetDAGCombine(ISD::AND);
384 setTargetDAGCombine(ISD::OR);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000385 setTargetDAGCombine(ISD::ADD);
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000386
Daniel Sanders3d849352014-04-14 15:44:42 +0000387 setMinFunctionAlignment(isGP64bit() ? 3 : 2);
Eli Friedman2518f832011-05-06 20:34:06 +0000388
Daniel Sandersd897b562014-03-27 10:46:12 +0000389 setStackPointerRegisterToSaveRestore(isN64() ? Mips::SP_64 : Mips::SP);
Akira Hatanakaaa560002011-05-26 18:59:03 +0000390
Daniel Sandersd897b562014-03-27 10:46:12 +0000391 setExceptionPointerRegister(isN64() ? Mips::A0_64 : Mips::A0);
392 setExceptionSelectorRegister(isN64() ? Mips::A1_64 : Mips::A1);
Akira Hatanaka1daf8c22012-06-13 19:33:32 +0000393
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000394 MaxStoresPerMemcpy = 16;
Zoran Jovanovicff9d5f32013-12-19 16:12:56 +0000395
396 isMicroMips = Subtarget->inMicroMipsMode();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000397}
398
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000399const MipsTargetLowering *MipsTargetLowering::create(MipsTargetMachine &TM) {
400 if (TM.getSubtargetImpl()->inMips16Mode())
401 return llvm::createMips16TargetLowering(TM);
Jia Liuf54f60f2012-02-28 07:46:26 +0000402
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000403 return llvm::createMipsSETargetLowering(TM);
Akira Hatanaka2fcc1cf2011-08-12 21:30:06 +0000404}
405
Reed Kotler720c5ca2014-04-17 22:15:34 +0000406// Create a fast isel object.
407FastISel *
408MipsTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
409 const TargetLibraryInfo *libInfo) const {
410 if (!EnableMipsFastISel)
411 return TargetLowering::createFastISel(funcInfo, libInfo);
412 return Mips::createFastISel(funcInfo, libInfo);
413}
414
Matt Arsenault758659232013-05-18 00:21:46 +0000415EVT MipsTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Akira Hatanakab13b3332013-01-04 20:06:01 +0000416 if (!VT.isVector())
417 return MVT::i32;
418 return VT.changeVectorElementTypeToInteger();
Scott Michela6729e82008-03-10 15:42:14 +0000419}
420
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000421static SDValue performDivRemCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000422 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000423 const MipsSubtarget *Subtarget) {
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000424 if (DCI.isBeforeLegalizeOps())
425 return SDValue();
426
Akira Hatanakab1538f92011-10-03 21:06:13 +0000427 EVT Ty = N->getValueType(0);
Akira Hatanaka8002a3f2013-08-14 00:47:08 +0000428 unsigned LO = (Ty == MVT::i32) ? Mips::LO0 : Mips::LO0_64;
429 unsigned HI = (Ty == MVT::i32) ? Mips::HI0 : Mips::HI0_64;
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000430 unsigned Opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem16 :
431 MipsISD::DivRemU16;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000432 SDLoc DL(N);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000433
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000434 SDValue DivRem = DAG.getNode(Opc, DL, MVT::Glue,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000435 N->getOperand(0), N->getOperand(1));
436 SDValue InChain = DAG.getEntryNode();
437 SDValue InGlue = DivRem;
438
439 // insert MFLO
440 if (N->hasAnyUseOfValue(0)) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000441 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, DL, LO, Ty,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000442 InGlue);
443 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
444 InChain = CopyFromLo.getValue(1);
445 InGlue = CopyFromLo.getValue(2);
446 }
447
448 // insert MFHI
449 if (N->hasAnyUseOfValue(1)) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000450 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, DL,
Akira Hatanakab1538f92011-10-03 21:06:13 +0000451 HI, Ty, InGlue);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000452 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
453 }
454
455 return SDValue();
456}
457
Akira Hatanaka89af5892013-04-18 01:00:46 +0000458static Mips::CondCode condCodeToFCC(ISD::CondCode CC) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000459 switch (CC) {
460 default: llvm_unreachable("Unknown fp condition code!");
461 case ISD::SETEQ:
462 case ISD::SETOEQ: return Mips::FCOND_OEQ;
463 case ISD::SETUNE: return Mips::FCOND_UNE;
464 case ISD::SETLT:
465 case ISD::SETOLT: return Mips::FCOND_OLT;
466 case ISD::SETGT:
467 case ISD::SETOGT: return Mips::FCOND_OGT;
468 case ISD::SETLE:
469 case ISD::SETOLE: return Mips::FCOND_OLE;
470 case ISD::SETGE:
471 case ISD::SETOGE: return Mips::FCOND_OGE;
472 case ISD::SETULT: return Mips::FCOND_ULT;
473 case ISD::SETULE: return Mips::FCOND_ULE;
474 case ISD::SETUGT: return Mips::FCOND_UGT;
475 case ISD::SETUGE: return Mips::FCOND_UGE;
476 case ISD::SETUO: return Mips::FCOND_UN;
477 case ISD::SETO: return Mips::FCOND_OR;
478 case ISD::SETNE:
479 case ISD::SETONE: return Mips::FCOND_ONE;
480 case ISD::SETUEQ: return Mips::FCOND_UEQ;
481 }
482}
483
484
Akira Hatanakaf0ea5002013-03-30 01:16:38 +0000485/// This function returns true if the floating point conditional branches and
486/// conditional moves which use condition code CC should be inverted.
487static bool invertFPCondCodeUser(Mips::CondCode CC) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000488 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
489 return false;
490
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000491 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
492 "Illegal Condition Code");
Akira Hatanakaa5352702011-03-31 18:26:17 +0000493
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000494 return true;
Akira Hatanakaa5352702011-03-31 18:26:17 +0000495}
496
497// Creates and returns an FPCmp node from a setcc node.
498// Returns Op if setcc is not a floating point comparison.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000499static SDValue createFPCmp(SelectionDAG &DAG, const SDValue &Op) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000500 // must be a SETCC node
501 if (Op.getOpcode() != ISD::SETCC)
502 return Op;
503
504 SDValue LHS = Op.getOperand(0);
505
506 if (!LHS.getValueType().isFloatingPoint())
507 return Op;
508
509 SDValue RHS = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000510 SDLoc DL(Op);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000511
Akira Hatanakaaef55c82011-04-15 21:00:26 +0000512 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
513 // node if necessary.
Akira Hatanakaa5352702011-03-31 18:26:17 +0000514 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
515
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000516 return DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS,
Akira Hatanaka89af5892013-04-18 01:00:46 +0000517 DAG.getConstant(condCodeToFCC(CC), MVT::i32));
Akira Hatanakaa5352702011-03-31 18:26:17 +0000518}
519
520// Creates and returns a CMovFPT/F node.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000521static SDValue createCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000522 SDValue False, SDLoc DL) {
Akira Hatanakaf0ea5002013-03-30 01:16:38 +0000523 ConstantSDNode *CC = cast<ConstantSDNode>(Cond.getOperand(2));
524 bool invert = invertFPCondCodeUser((Mips::CondCode)CC->getSExtValue());
Akira Hatanaka8bce21c2013-07-26 20:51:20 +0000525 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000526
527 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
Akira Hatanaka8bce21c2013-07-26 20:51:20 +0000528 True.getValueType(), True, FCC0, False, Cond);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000529}
530
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000531static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000532 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000533 const MipsSubtarget *Subtarget) {
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000534 if (DCI.isBeforeLegalizeOps())
535 return SDValue();
536
537 SDValue SetCC = N->getOperand(0);
538
539 if ((SetCC.getOpcode() != ISD::SETCC) ||
540 !SetCC.getOperand(0).getValueType().isInteger())
541 return SDValue();
542
543 SDValue False = N->getOperand(2);
544 EVT FalseTy = False.getValueType();
545
546 if (!FalseTy.isInteger())
547 return SDValue();
548
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000549 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(False);
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000550
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000551 // If the RHS (False) is 0, we swap the order of the operands
552 // of ISD::SELECT (obviously also inverting the condition) so that we can
553 // take advantage of conditional moves using the $0 register.
554 // Example:
555 // return (a != 0) ? x : 0;
556 // load $reg, x
557 // movz $reg, $0, a
558 if (!FalseC)
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000559 return SDValue();
560
Andrew Trickef9de2a2013-05-25 02:42:55 +0000561 const SDLoc DL(N);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000562
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000563 if (!FalseC->getZExtValue()) {
564 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
565 SDValue True = N->getOperand(1);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000566
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000567 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
568 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
569
570 return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True);
571 }
572
Matheus Almeidaa6beac12013-12-05 12:07:05 +0000573 // If both operands are integer constants there's a possibility that we
574 // can do some interesting optimizations.
575 SDValue True = N->getOperand(1);
576 ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(True);
577
578 if (!TrueC || !True.getValueType().isInteger())
579 return SDValue();
580
581 // We'll also ignore MVT::i64 operands as this optimizations proves
582 // to be ineffective because of the required sign extensions as the result
583 // of a SETCC operator is always MVT::i32 for non-vector types.
584 if (True.getValueType() == MVT::i64)
585 return SDValue();
586
587 int64_t Diff = TrueC->getSExtValue() - FalseC->getSExtValue();
588
589 // 1) (a < x) ? y : y-1
590 // slti $reg1, a, x
591 // addiu $reg2, $reg1, y-1
592 if (Diff == 1)
593 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, False);
594
595 // 2) (a < x) ? y-1 : y
596 // slti $reg1, a, x
597 // xor $reg1, $reg1, 1
598 // addiu $reg2, $reg1, y-1
599 if (Diff == -1) {
600 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
601 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
602 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
603 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, True);
604 }
605
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000606 // Couldn't optimize.
607 return SDValue();
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000608}
609
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000610static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000611 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000612 const MipsSubtarget *Subtarget) {
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000613 // Pattern match EXT.
614 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
615 // => ext $dst, $src, size, pos
Akira Hatanaka4a3836b2013-10-09 23:36:17 +0000616 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasExtractInsert())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000617 return SDValue();
618
619 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000620 unsigned ShiftRightOpc = ShiftRight.getOpcode();
621
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000622 // Op's first operand must be a shift right.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000623 if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL)
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000624 return SDValue();
625
626 // The second operand of the shift must be an immediate.
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000627 ConstantSDNode *CN;
628 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
629 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000630
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000631 uint64_t Pos = CN->getZExtValue();
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000632 uint64_t SMPos, SMSize;
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000633
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000634 // Op's second operand must be a shifted mask.
635 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000636 !isShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000637 return SDValue();
638
639 // Return if the shifted mask does not start at bit 0 or the sum of its size
640 // and Pos exceeds the word's size.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000641 EVT ValTy = N->getValueType(0);
642 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000643 return SDValue();
644
Andrew Trickef9de2a2013-05-25 02:42:55 +0000645 return DAG.getNode(MipsISD::Ext, SDLoc(N), ValTy,
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000646 ShiftRight.getOperand(0), DAG.getConstant(Pos, MVT::i32),
Akira Hatanakaeea541c2011-08-17 22:59:46 +0000647 DAG.getConstant(SMSize, MVT::i32));
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000648}
Jia Liuf54f60f2012-02-28 07:46:26 +0000649
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000650static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000651 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000652 const MipsSubtarget *Subtarget) {
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000653 // Pattern match INS.
654 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
Jia Liuf54f60f2012-02-28 07:46:26 +0000655 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000656 // => ins $dst, $src, size, pos, $src1
Akira Hatanaka4a3836b2013-10-09 23:36:17 +0000657 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasExtractInsert())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000658 return SDValue();
659
660 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
661 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
662 ConstantSDNode *CN;
663
664 // See if Op's first operand matches (and $src1 , mask0).
665 if (And0.getOpcode() != ISD::AND)
666 return SDValue();
667
668 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000669 !isShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000670 return SDValue();
671
672 // See if Op's second operand matches (and (shl $src, pos), mask1).
673 if (And1.getOpcode() != ISD::AND)
674 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000675
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000676 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000677 !isShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000678 return SDValue();
679
680 // The shift masks must have the same position and size.
681 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
682 return SDValue();
683
684 SDValue Shl = And1.getOperand(0);
685 if (Shl.getOpcode() != ISD::SHL)
686 return SDValue();
687
688 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
689 return SDValue();
690
691 unsigned Shamt = CN->getZExtValue();
692
693 // Return if the shift amount and the first bit position of mask are not the
Jia Liuf54f60f2012-02-28 07:46:26 +0000694 // same.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000695 EVT ValTy = N->getValueType(0);
696 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000697 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000698
Andrew Trickef9de2a2013-05-25 02:42:55 +0000699 return DAG.getNode(MipsISD::Ins, SDLoc(N), ValTy, Shl.getOperand(0),
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000700 DAG.getConstant(SMPos0, MVT::i32),
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000701 DAG.getConstant(SMSize0, MVT::i32), And0.getOperand(0));
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000702}
Jia Liuf54f60f2012-02-28 07:46:26 +0000703
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000704static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000705 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000706 const MipsSubtarget *Subtarget) {
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000707 // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt))
708
709 if (DCI.isBeforeLegalizeOps())
710 return SDValue();
711
712 SDValue Add = N->getOperand(1);
713
714 if (Add.getOpcode() != ISD::ADD)
715 return SDValue();
716
717 SDValue Lo = Add.getOperand(1);
718
719 if ((Lo.getOpcode() != MipsISD::Lo) ||
720 (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable))
721 return SDValue();
722
723 EVT ValTy = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000724 SDLoc DL(N);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000725
726 SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, N->getOperand(0),
727 Add.getOperand(0));
728 return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo);
729}
730
Bruno Cardoso Lopes61a61e92011-02-10 18:05:10 +0000731SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000732 const {
733 SelectionDAG &DAG = DCI.DAG;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000734 unsigned Opc = N->getOpcode();
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000735
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000736 switch (Opc) {
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000737 default: break;
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000738 case ISD::SDIVREM:
739 case ISD::UDIVREM:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000740 return performDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000741 case ISD::SELECT:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000742 return performSELECTCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000743 case ISD::AND:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000744 return performANDCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000745 case ISD::OR:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000746 return performORCombine(N, DAG, DCI, Subtarget);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000747 case ISD::ADD:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000748 return performADDCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000749 }
750
751 return SDValue();
752}
753
Akira Hatanakafabb8cf2012-09-21 23:58:31 +0000754void
755MipsTargetLowering::LowerOperationWrapper(SDNode *N,
756 SmallVectorImpl<SDValue> &Results,
757 SelectionDAG &DAG) const {
758 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
759
760 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
761 Results.push_back(Res.getValue(I));
762}
763
764void
765MipsTargetLowering::ReplaceNodeResults(SDNode *N,
766 SmallVectorImpl<SDValue> &Results,
767 SelectionDAG &DAG) const {
Akira Hatanaka9da442f2013-04-30 21:17:07 +0000768 return LowerOperationWrapper(N, Results, DAG);
Akira Hatanakafabb8cf2012-09-21 23:58:31 +0000769}
770
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000771SDValue MipsTargetLowering::
Dan Gohman21cea8a2010-04-17 15:26:15 +0000772LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000773{
Wesley Peck527da1b2010-11-23 03:31:01 +0000774 switch (Op.getOpcode())
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000775 {
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000776 case ISD::BR_JT: return lowerBR_JT(Op, DAG);
777 case ISD::BRCOND: return lowerBRCOND(Op, DAG);
778 case ISD::ConstantPool: return lowerConstantPool(Op, DAG);
779 case ISD::GlobalAddress: return lowerGlobalAddress(Op, DAG);
780 case ISD::BlockAddress: return lowerBlockAddress(Op, DAG);
781 case ISD::GlobalTLSAddress: return lowerGlobalTLSAddress(Op, DAG);
782 case ISD::JumpTable: return lowerJumpTable(Op, DAG);
783 case ISD::SELECT: return lowerSELECT(Op, DAG);
784 case ISD::SELECT_CC: return lowerSELECT_CC(Op, DAG);
785 case ISD::SETCC: return lowerSETCC(Op, DAG);
786 case ISD::VASTART: return lowerVASTART(Op, DAG);
787 case ISD::FCOPYSIGN: return lowerFCOPYSIGN(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000788 case ISD::FRAMEADDR: return lowerFRAMEADDR(Op, DAG);
789 case ISD::RETURNADDR: return lowerRETURNADDR(Op, DAG);
790 case ISD::EH_RETURN: return lowerEH_RETURN(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000791 case ISD::ATOMIC_FENCE: return lowerATOMIC_FENCE(Op, DAG);
792 case ISD::SHL_PARTS: return lowerShiftLeftParts(Op, DAG);
793 case ISD::SRA_PARTS: return lowerShiftRightParts(Op, DAG, true);
794 case ISD::SRL_PARTS: return lowerShiftRightParts(Op, DAG, false);
795 case ISD::LOAD: return lowerLOAD(Op, DAG);
796 case ISD::STORE: return lowerSTORE(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000797 case ISD::ADD: return lowerADD(Op, DAG);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000798 case ISD::FP_TO_SINT: return lowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000799 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000800 return SDValue();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000801}
802
Akira Hatanakae2489122011-04-15 21:51:11 +0000803//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000804// Lower helper functions
Akira Hatanakae2489122011-04-15 21:51:11 +0000805//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000806
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000807// addLiveIn - This helper function adds the specified physical register to the
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000808// MachineFunction as a live in value. It also creates a corresponding
809// virtual register for it.
810static unsigned
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000811addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000812{
Chris Lattnera10fff52007-12-31 04:13:23 +0000813 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
814 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000815 return VReg;
816}
817
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000818static MachineBasicBlock *expandPseudoDIV(MachineInstr *MI,
819 MachineBasicBlock &MBB,
820 const TargetInstrInfo &TII,
821 bool Is64Bit) {
822 if (NoZeroDivCheck)
823 return &MBB;
824
825 // Insert instruction "teq $divisor_reg, $zero, 7".
826 MachineBasicBlock::iterator I(MI);
827 MachineInstrBuilder MIB;
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000828 MachineOperand &Divisor = MI->getOperand(2);
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000829 MIB = BuildMI(MBB, std::next(I), MI->getDebugLoc(), TII.get(Mips::TEQ))
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000830 .addReg(Divisor.getReg(), getKillRegState(Divisor.isKill()))
831 .addReg(Mips::ZERO).addImm(7);
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000832
833 // Use the 32-bit sub-register if this is a 64-bit division.
834 if (Is64Bit)
835 MIB->getOperand(0).setSubReg(Mips::sub_32);
836
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000837 // Clear Divisor's kill flag.
838 Divisor.setIsKill(false);
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000839 return &MBB;
840}
841
Akira Hatanakae4bd0542012-09-27 02:15:57 +0000842MachineBasicBlock *
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +0000843MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +0000844 MachineBasicBlock *BB) const {
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +0000845 switch (MI->getOpcode()) {
Reed Kotler97ba5f22013-02-21 04:22:38 +0000846 default:
847 llvm_unreachable("Unexpected instr type to insert");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000848 case Mips::ATOMIC_LOAD_ADD_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000849 return emitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000850 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000851 return emitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000852 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000853 return emitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000854 case Mips::ATOMIC_LOAD_ADD_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000855 return emitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000856
857 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000858 return emitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000859 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000860 return emitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000861 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000862 return emitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000863 case Mips::ATOMIC_LOAD_AND_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000864 return emitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000865
866 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000867 return emitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000868 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000869 return emitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000870 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000871 return emitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000872 case Mips::ATOMIC_LOAD_OR_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000873 return emitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000874
875 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000876 return emitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000877 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000878 return emitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000879 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000880 return emitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000881 case Mips::ATOMIC_LOAD_XOR_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000882 return emitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000883
884 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000885 return emitAtomicBinaryPartword(MI, BB, 1, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000886 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000887 return emitAtomicBinaryPartword(MI, BB, 2, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000888 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000889 return emitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000890 case Mips::ATOMIC_LOAD_NAND_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000891 return emitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000892
893 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000894 return emitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000895 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000896 return emitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000897 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000898 return emitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000899 case Mips::ATOMIC_LOAD_SUB_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000900 return emitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000901
902 case Mips::ATOMIC_SWAP_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000903 return emitAtomicBinaryPartword(MI, BB, 1, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000904 case Mips::ATOMIC_SWAP_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000905 return emitAtomicBinaryPartword(MI, BB, 2, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000906 case Mips::ATOMIC_SWAP_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000907 return emitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000908 case Mips::ATOMIC_SWAP_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000909 return emitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000910
911 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000912 return emitAtomicCmpSwapPartword(MI, BB, 1);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000913 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000914 return emitAtomicCmpSwapPartword(MI, BB, 2);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000915 case Mips::ATOMIC_CMP_SWAP_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000916 return emitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000917 case Mips::ATOMIC_CMP_SWAP_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000918 return emitAtomicCmpSwap(MI, BB, 8);
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000919 case Mips::PseudoSDIV:
920 case Mips::PseudoUDIV:
921 return expandPseudoDIV(MI, *BB, *getTargetMachine().getInstrInfo(), false);
922 case Mips::PseudoDSDIV:
923 case Mips::PseudoDUDIV:
924 return expandPseudoDIV(MI, *BB, *getTargetMachine().getInstrInfo(), true);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000925 }
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +0000926}
927
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000928// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
929// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
930MachineBasicBlock *
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000931MipsTargetLowering::emitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher0713a9d2011-06-08 23:55:35 +0000932 unsigned Size, unsigned BinOpcode,
Akira Hatanaka15506782011-06-07 18:58:42 +0000933 bool Nand) const {
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000934 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000935
936 MachineFunction *MF = BB->getParent();
937 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000938 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000939 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000940 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000941 unsigned LL, SC, AND, NOR, ZERO, BEQ;
942
943 if (Size == 4) {
Zoran Jovanovicff9d5f32013-12-19 16:12:56 +0000944 LL = isMicroMips ? Mips::LL_MM : Mips::LL;
945 SC = isMicroMips ? Mips::SC_MM : Mips::SC;
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000946 AND = Mips::AND;
947 NOR = Mips::NOR;
948 ZERO = Mips::ZERO;
949 BEQ = Mips::BEQ;
950 }
951 else {
Akira Hatanaka6781fc12013-08-20 21:08:22 +0000952 LL = Mips::LLD;
953 SC = Mips::SCD;
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000954 AND = Mips::AND64;
955 NOR = Mips::NOR64;
956 ZERO = Mips::ZERO_64;
957 BEQ = Mips::BEQ64;
958 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000959
Akira Hatanaka0e019592011-07-19 20:11:17 +0000960 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000961 unsigned Ptr = MI->getOperand(1).getReg();
962 unsigned Incr = MI->getOperand(2).getReg();
963
Akira Hatanaka0e019592011-07-19 20:11:17 +0000964 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
965 unsigned AndRes = RegInfo.createVirtualRegister(RC);
966 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000967
968 // insert new blocks after the current block
969 const BasicBlock *LLVM_BB = BB->getBasicBlock();
970 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
971 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
972 MachineFunction::iterator It = BB;
973 ++It;
974 MF->insert(It, loopMBB);
975 MF->insert(It, exitMBB);
976
977 // Transfer the remainder of BB and its successor edges to exitMBB.
978 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000979 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000980 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
981
982 // thisMBB:
983 // ...
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000984 // fallthrough --> loopMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000985 BB->addSuccessor(loopMBB);
Akira Hatanaka08636b42011-07-19 17:09:53 +0000986 loopMBB->addSuccessor(loopMBB);
987 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000988
989 // loopMBB:
990 // ll oldval, 0(ptr)
Akira Hatanaka0e019592011-07-19 20:11:17 +0000991 // <binop> storeval, oldval, incr
992 // sc success, storeval, 0(ptr)
993 // beq success, $0, loopMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000994 BB = loopMBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000995 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000996 if (Nand) {
Akira Hatanaka0e019592011-07-19 20:11:17 +0000997 // and andres, oldval, incr
998 // nor storeval, $0, andres
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000999 BuildMI(BB, DL, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
1000 BuildMI(BB, DL, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001001 } else if (BinOpcode) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001002 // <binop> storeval, oldval, incr
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001003 BuildMI(BB, DL, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001004 } else {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001005 StoreVal = Incr;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001006 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001007 BuildMI(BB, DL, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
1008 BuildMI(BB, DL, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001009
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001010 MI->eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001011
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001012 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001013}
1014
1015MachineBasicBlock *
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001016MipsTargetLowering::emitAtomicBinaryPartword(MachineInstr *MI,
Akira Hatanaka15506782011-06-07 18:58:42 +00001017 MachineBasicBlock *BB,
1018 unsigned Size, unsigned BinOpcode,
1019 bool Nand) const {
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001020 assert((Size == 1 || Size == 2) &&
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001021 "Unsupported size for EmitAtomicBinaryPartial.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001022
1023 MachineFunction *MF = BB->getParent();
1024 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1025 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1026 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001027 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001028
1029 unsigned Dest = MI->getOperand(0).getReg();
1030 unsigned Ptr = MI->getOperand(1).getReg();
1031 unsigned Incr = MI->getOperand(2).getReg();
1032
Akira Hatanaka0e019592011-07-19 20:11:17 +00001033 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1034 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001035 unsigned Mask = RegInfo.createVirtualRegister(RC);
1036 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001037 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1038 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001039 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001040 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1041 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1042 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1043 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1044 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka9663dd32011-07-19 20:56:53 +00001045 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001046 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1047 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1048 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1049 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1050 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001051
1052 // insert new blocks after the current block
1053 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1054 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001055 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001056 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1057 MachineFunction::iterator It = BB;
1058 ++It;
1059 MF->insert(It, loopMBB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001060 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001061 MF->insert(It, exitMBB);
1062
1063 // Transfer the remainder of BB and its successor edges to exitMBB.
1064 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001065 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001066 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1067
Akira Hatanaka08636b42011-07-19 17:09:53 +00001068 BB->addSuccessor(loopMBB);
1069 loopMBB->addSuccessor(loopMBB);
1070 loopMBB->addSuccessor(sinkMBB);
1071 sinkMBB->addSuccessor(exitMBB);
1072
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001073 // thisMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001074 // addiu masklsb2,$0,-4 # 0xfffffffc
1075 // and alignedaddr,ptr,masklsb2
1076 // andi ptrlsb2,ptr,3
1077 // sll shiftamt,ptrlsb2,3
1078 // ori maskupper,$0,255 # 0xff
1079 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001080 // nor mask2,$0,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001081 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001082
1083 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001084 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001085 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001086 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001087 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001088 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Akira Hatanaka2bf97332013-05-31 03:25:44 +00001089 if (Subtarget->isLittle()) {
1090 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1091 } else {
1092 unsigned Off = RegInfo.createVirtualRegister(RC);
1093 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1094 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1095 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1096 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001097 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001098 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001099 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001100 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001101 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001102 BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(Incr).addReg(ShiftAmt);
Bruno Cardoso Lopesf771a0f2011-05-31 20:25:26 +00001103
Akira Hatanaka27292632011-07-18 18:52:12 +00001104 // atomic.load.binop
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001105 // loopMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001106 // ll oldval,0(alignedaddr)
1107 // binop binopres,oldval,incr2
1108 // and newval,binopres,mask
1109 // and maskedoldval0,oldval,mask2
1110 // or storeval,maskedoldval0,newval
1111 // sc success,storeval,0(alignedaddr)
1112 // beq success,$0,loopMBB
1113
Akira Hatanaka27292632011-07-18 18:52:12 +00001114 // atomic.swap
1115 // loopMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001116 // ll oldval,0(alignedaddr)
Akira Hatanakae4503582011-07-19 18:14:26 +00001117 // and newval,incr2,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001118 // and maskedoldval0,oldval,mask2
1119 // or storeval,maskedoldval0,newval
1120 // sc success,storeval,0(alignedaddr)
1121 // beq success,$0,loopMBB
Akira Hatanaka27292632011-07-18 18:52:12 +00001122
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001123 BB = loopMBB;
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001124 BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001125 if (Nand) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001126 // and andres, oldval, incr2
1127 // nor binopres, $0, andres
1128 // and newval, binopres, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001129 BuildMI(BB, DL, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1130 BuildMI(BB, DL, TII->get(Mips::NOR), BinOpRes)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001131 .addReg(Mips::ZERO).addReg(AndRes);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001132 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001133 } else if (BinOpcode) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001134 // <binop> binopres, oldval, incr2
1135 // and newval, binopres, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001136 BuildMI(BB, DL, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1137 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001138 } else { // atomic.swap
Akira Hatanaka0e019592011-07-19 20:11:17 +00001139 // and newval, incr2, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001140 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanakae4503582011-07-19 18:14:26 +00001141 }
Jia Liuf54f60f2012-02-28 07:46:26 +00001142
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001143 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001144 .addReg(OldVal).addReg(Mask2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001145 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka9663dd32011-07-19 20:56:53 +00001146 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001147 BuildMI(BB, DL, TII->get(Mips::SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001148 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001149 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001150 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001151
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001152 // sinkMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001153 // and maskedoldval1,oldval,mask
1154 // srl srlres,maskedoldval1,shiftamt
1155 // sll sllres,srlres,24
1156 // sra dest,sllres,24
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001157 BB = sinkMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001158 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakae97bd812011-07-19 03:14:58 +00001159
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001160 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001161 .addReg(OldVal).addReg(Mask);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001162 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001163 .addReg(MaskedOldVal1).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001164 BuildMI(BB, DL, TII->get(Mips::SLL), SllRes)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001165 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001166 BuildMI(BB, DL, TII->get(Mips::SRA), Dest)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001167 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001168
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001169 MI->eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001170
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001171 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001172}
1173
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001174MachineBasicBlock * MipsTargetLowering::emitAtomicCmpSwap(MachineInstr *MI,
1175 MachineBasicBlock *BB,
1176 unsigned Size) const {
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001177 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001178
1179 MachineFunction *MF = BB->getParent();
1180 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001181 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001182 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001183 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001184 unsigned LL, SC, ZERO, BNE, BEQ;
1185
1186 if (Size == 4) {
Zoran Jovanovicff9d5f32013-12-19 16:12:56 +00001187 LL = isMicroMips ? Mips::LL_MM : Mips::LL;
1188 SC = isMicroMips ? Mips::SC_MM : Mips::SC;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001189 ZERO = Mips::ZERO;
1190 BNE = Mips::BNE;
1191 BEQ = Mips::BEQ;
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001192 } else {
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001193 LL = Mips::LLD;
1194 SC = Mips::SCD;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001195 ZERO = Mips::ZERO_64;
1196 BNE = Mips::BNE64;
1197 BEQ = Mips::BEQ64;
1198 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001199
1200 unsigned Dest = MI->getOperand(0).getReg();
1201 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka0e019592011-07-19 20:11:17 +00001202 unsigned OldVal = MI->getOperand(2).getReg();
1203 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001204
Akira Hatanaka0e019592011-07-19 20:11:17 +00001205 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001206
1207 // insert new blocks after the current block
1208 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1209 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1210 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1211 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1212 MachineFunction::iterator It = BB;
1213 ++It;
1214 MF->insert(It, loop1MBB);
1215 MF->insert(It, loop2MBB);
1216 MF->insert(It, exitMBB);
1217
1218 // Transfer the remainder of BB and its successor edges to exitMBB.
1219 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001220 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001221 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1222
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001223 // thisMBB:
1224 // ...
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001225 // fallthrough --> loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001226 BB->addSuccessor(loop1MBB);
Akira Hatanaka08636b42011-07-19 17:09:53 +00001227 loop1MBB->addSuccessor(exitMBB);
1228 loop1MBB->addSuccessor(loop2MBB);
1229 loop2MBB->addSuccessor(loop1MBB);
1230 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001231
1232 // loop1MBB:
1233 // ll dest, 0(ptr)
1234 // bne dest, oldval, exitMBB
1235 BB = loop1MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001236 BuildMI(BB, DL, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1237 BuildMI(BB, DL, TII->get(BNE))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001238 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001239
1240 // loop2MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001241 // sc success, newval, 0(ptr)
1242 // beq success, $0, loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001243 BB = loop2MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001244 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001245 .addReg(NewVal).addReg(Ptr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001246 BuildMI(BB, DL, TII->get(BEQ))
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001247 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001248
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001249 MI->eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001250
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001251 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001252}
1253
1254MachineBasicBlock *
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001255MipsTargetLowering::emitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka15506782011-06-07 18:58:42 +00001256 MachineBasicBlock *BB,
1257 unsigned Size) const {
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001258 assert((Size == 1 || Size == 2) &&
1259 "Unsupported size for EmitAtomicCmpSwapPartial.");
1260
1261 MachineFunction *MF = BB->getParent();
1262 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1263 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1264 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001265 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001266
1267 unsigned Dest = MI->getOperand(0).getReg();
1268 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka0e019592011-07-19 20:11:17 +00001269 unsigned CmpVal = MI->getOperand(2).getReg();
1270 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001271
Akira Hatanaka0e019592011-07-19 20:11:17 +00001272 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1273 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001274 unsigned Mask = RegInfo.createVirtualRegister(RC);
1275 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001276 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1277 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1278 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1279 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1280 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1281 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1282 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1283 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1284 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1285 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1286 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1287 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1288 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1289 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001290
1291 // insert new blocks after the current block
1292 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1293 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1294 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001295 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001296 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1297 MachineFunction::iterator It = BB;
1298 ++It;
1299 MF->insert(It, loop1MBB);
1300 MF->insert(It, loop2MBB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001301 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001302 MF->insert(It, exitMBB);
1303
1304 // Transfer the remainder of BB and its successor edges to exitMBB.
1305 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001306 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001307 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1308
Akira Hatanaka08636b42011-07-19 17:09:53 +00001309 BB->addSuccessor(loop1MBB);
1310 loop1MBB->addSuccessor(sinkMBB);
1311 loop1MBB->addSuccessor(loop2MBB);
1312 loop2MBB->addSuccessor(loop1MBB);
1313 loop2MBB->addSuccessor(sinkMBB);
1314 sinkMBB->addSuccessor(exitMBB);
1315
Akira Hatanakae4503582011-07-19 18:14:26 +00001316 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001317 // thisMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001318 // addiu masklsb2,$0,-4 # 0xfffffffc
1319 // and alignedaddr,ptr,masklsb2
1320 // andi ptrlsb2,ptr,3
1321 // sll shiftamt,ptrlsb2,3
1322 // ori maskupper,$0,255 # 0xff
1323 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001324 // nor mask2,$0,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001325 // andi maskedcmpval,cmpval,255
1326 // sll shiftedcmpval,maskedcmpval,shiftamt
1327 // andi maskednewval,newval,255
1328 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001329 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001330 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001331 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001332 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001333 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001334 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Akira Hatanaka2bf97332013-05-31 03:25:44 +00001335 if (Subtarget->isLittle()) {
1336 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1337 } else {
1338 unsigned Off = RegInfo.createVirtualRegister(RC);
1339 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1340 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1341 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1342 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001343 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001344 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001345 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001346 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001347 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1348 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001349 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001350 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001351 .addReg(MaskedCmpVal).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001352 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001353 .addReg(NewVal).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001354 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001355 .addReg(MaskedNewVal).addReg(ShiftAmt);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001356
1357 // loop1MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001358 // ll oldval,0(alginedaddr)
1359 // and maskedoldval0,oldval,mask
1360 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001361 BB = loop1MBB;
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001362 BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001363 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001364 .addReg(OldVal).addReg(Mask);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001365 BuildMI(BB, DL, TII->get(Mips::BNE))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001366 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001367
1368 // loop2MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001369 // and maskedoldval1,oldval,mask2
1370 // or storeval,maskedoldval1,shiftednewval
1371 // sc success,storeval,0(alignedaddr)
1372 // beq success,$0,loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001373 BB = loop2MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001374 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001375 .addReg(OldVal).addReg(Mask2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001376 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001377 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001378 BuildMI(BB, DL, TII->get(Mips::SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001379 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001380 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001381 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001382
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001383 // sinkMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001384 // srl srlres,maskedoldval0,shiftamt
1385 // sll sllres,srlres,24
1386 // sra dest,sllres,24
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001387 BB = sinkMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001388 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakae97bd812011-07-19 03:14:58 +00001389
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001390 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001391 .addReg(MaskedOldVal0).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001392 BuildMI(BB, DL, TII->get(Mips::SLL), SllRes)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001393 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001394 BuildMI(BB, DL, TII->get(Mips::SRA), Dest)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001395 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001396
1397 MI->eraseFromParent(); // The instruction is gone now.
1398
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001399 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001400}
1401
Akira Hatanakae2489122011-04-15 21:51:11 +00001402//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001403// Misc Lower Operation implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00001404//===----------------------------------------------------------------------===//
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001405SDValue MipsTargetLowering::lowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001406 SDValue Chain = Op.getOperand(0);
1407 SDValue Table = Op.getOperand(1);
1408 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001409 SDLoc DL(Op);
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001410 EVT PTy = getPointerTy();
1411 unsigned EntrySize =
1412 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(*getDataLayout());
1413
1414 Index = DAG.getNode(ISD::MUL, DL, PTy, Index,
1415 DAG.getConstant(EntrySize, PTy));
1416 SDValue Addr = DAG.getNode(ISD::ADD, DL, PTy, Index, Table);
1417
1418 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
1419 Addr = DAG.getExtLoad(ISD::SEXTLOAD, DL, PTy, Chain, Addr,
1420 MachinePointerInfo::getJumpTable(), MemVT, false, false,
1421 0);
1422 Chain = Addr.getValue(1);
1423
Daniel Sandersd897b562014-03-27 10:46:12 +00001424 if ((getTargetMachine().getRelocationModel() == Reloc::PIC_) || isN64()) {
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001425 // For PIC, the sequence is:
1426 // BRIND(load(Jumptable + index) + RelocBase)
1427 // RelocBase can be JumpTable, GOT or some sort of global base.
1428 Addr = DAG.getNode(ISD::ADD, DL, PTy, Addr,
1429 getPICJumpTableRelocBase(Table, DAG));
1430 }
1431
1432 return DAG.getNode(ISD::BRIND, DL, MVT::Other, Chain, Addr);
1433}
1434
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001435SDValue MipsTargetLowering::lowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Wesley Peck527da1b2010-11-23 03:31:01 +00001436 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001437 // the block to branch to if the condition is true.
1438 SDValue Chain = Op.getOperand(0);
1439 SDValue Dest = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001440 SDLoc DL(Op);
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001441
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001442 SDValue CondRes = createFPCmp(DAG, Op.getOperand(1));
Akira Hatanakaa5352702011-03-31 18:26:17 +00001443
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001444 // Return if flag is not set by a floating point comparison.
Akira Hatanakaa5352702011-03-31 18:26:17 +00001445 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopesa9504222008-07-30 17:06:13 +00001446 return Op;
Wesley Peck527da1b2010-11-23 03:31:01 +00001447
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +00001448 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmaneffb8942008-09-12 16:56:44 +00001449 Mips::CondCode CC =
1450 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Akira Hatanakaf0ea5002013-03-30 01:16:38 +00001451 unsigned Opc = invertFPCondCodeUser(CC) ? Mips::BRANCH_F : Mips::BRANCH_T;
1452 SDValue BrCode = DAG.getConstant(Opc, MVT::i32);
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +00001453 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001454 return DAG.getNode(MipsISD::FPBrcond, DL, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +00001455 FCC0, Dest, CondRes);
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001456}
1457
1458SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001459lowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001460{
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001461 SDValue Cond = createFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001462
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001463 // Return if flag is not set by a floating point comparison.
Akira Hatanakaa5352702011-03-31 18:26:17 +00001464 if (Cond.getOpcode() != MipsISD::FPCmp)
1465 return Op;
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +00001466
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001467 return createCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
Andrew Trickef9de2a2013-05-25 02:42:55 +00001468 SDLoc(Op));
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001469}
1470
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001471SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001472lowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001473{
Andrew Trickef9de2a2013-05-25 02:42:55 +00001474 SDLoc DL(Op);
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001475 EVT Ty = Op.getOperand(0).getValueType();
Matt Arsenault758659232013-05-18 00:21:46 +00001476 SDValue Cond = DAG.getNode(ISD::SETCC, DL,
1477 getSetCCResultType(*DAG.getContext(), Ty),
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001478 Op.getOperand(0), Op.getOperand(1),
1479 Op.getOperand(4));
1480
1481 return DAG.getNode(ISD::SELECT, DL, Op.getValueType(), Cond, Op.getOperand(2),
1482 Op.getOperand(3));
1483}
1484
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001485SDValue MipsTargetLowering::lowerSETCC(SDValue Op, SelectionDAG &DAG) const {
1486 SDValue Cond = createFPCmp(DAG, Op);
Akira Hatanakab7f78592012-03-09 23:46:03 +00001487
1488 assert(Cond.getOpcode() == MipsISD::FPCmp &&
1489 "Floating point operand expected.");
1490
1491 SDValue True = DAG.getConstant(1, MVT::i32);
1492 SDValue False = DAG.getConstant(0, MVT::i32);
1493
Andrew Trickef9de2a2013-05-25 02:42:55 +00001494 return createCMovFP(DAG, Cond, True, False, SDLoc(Op));
Akira Hatanakab7f78592012-03-09 23:46:03 +00001495}
1496
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001497SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001498 SelectionDAG &DAG) const {
Dale Johannesen400dc2e2009-02-06 21:50:26 +00001499 // FIXME there isn't actually debug info here
Andrew Trickef9de2a2013-05-25 02:42:55 +00001500 SDLoc DL(Op);
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001501 EVT Ty = Op.getValueType();
1502 GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
1503 const GlobalValue *GV = N->getGlobal();
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001504
Daniel Sandersd897b562014-03-27 10:46:12 +00001505 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !isN64()) {
Akira Hatanaka92a96e12012-09-12 23:27:55 +00001506 const MipsTargetObjectFile &TLOF =
1507 (const MipsTargetObjectFile&)getObjFileLowering();
Wesley Peck527da1b2010-11-23 03:31:01 +00001508
Chris Lattner58e8be82009-08-13 05:41:27 +00001509 // %gp_rel relocation
Wesley Peck527da1b2010-11-23 03:31:01 +00001510 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001511 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, 0,
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +00001512 MipsII::MO_GPREL);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001513 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, DL,
Craig Topper48d114b2014-04-26 18:35:24 +00001514 DAG.getVTList(MVT::i32), GA);
Akira Hatanakaad495022012-08-22 03:18:13 +00001515 SDValue GPReg = DAG.getRegister(Mips::GP, MVT::i32);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001516 return DAG.getNode(ISD::ADD, DL, MVT::i32, GPReg, GPRelNode);
Chris Lattner58e8be82009-08-13 05:41:27 +00001517 }
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001518
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001519 // %hi/%lo relocation
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001520 return getAddrNonPIC(N, Ty, DAG);
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001521 }
1522
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001523 if (GV->hasInternalLinkage() || (GV->hasLocalLinkage() && !isa<Function>(GV)))
Daniel Sandersd897b562014-03-27 10:46:12 +00001524 return getAddrLocal(N, Ty, DAG, isN32() || isN64());
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001525
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00001526 if (LargeGOT)
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001527 return getAddrGlobalLargeGOT(N, Ty, DAG, MipsII::MO_GOT_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00001528 MipsII::MO_GOT_LO16, DAG.getEntryNode(),
1529 MachinePointerInfo::getGOT());
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00001530
Daniel Sandersbd0e3902014-03-27 12:49:34 +00001531 return getAddrGlobal(N, Ty, DAG, (isN32() || isN64()) ? MipsII::MO_GOT_DISP
1532 : MipsII::MO_GOT16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00001533 DAG.getEntryNode(), MachinePointerInfo::getGOT());
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001534}
1535
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001536SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op,
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +00001537 SelectionDAG &DAG) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001538 BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op);
1539 EVT Ty = Op.getValueType();
Akira Hatanaka30f97cf2013-09-25 00:30:25 +00001540
Daniel Sandersd897b562014-03-27 10:46:12 +00001541 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !isN64())
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001542 return getAddrNonPIC(N, Ty, DAG);
1543
Daniel Sandersd897b562014-03-27 10:46:12 +00001544 return getAddrLocal(N, Ty, DAG, isN32() || isN64());
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +00001545}
1546
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001547SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001548lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001549{
Akira Hatanakabff84e12011-12-14 18:26:41 +00001550 // If the relocation model is PIC, use the General Dynamic TLS Model or
1551 // Local Dynamic TLS model, otherwise use the Initial Exec or
1552 // Local Exec TLS Model.
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001553
1554 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001555 SDLoc DL(GA);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001556 const GlobalValue *GV = GA->getGlobal();
1557 EVT PtrVT = getPointerTy();
1558
Hans Wennborgaea41202012-05-04 09:40:39 +00001559 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
1560
1561 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Hans Wennborg245917b2012-06-04 14:02:08 +00001562 // General Dynamic and Local Dynamic TLS Model.
1563 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
1564 : MipsII::MO_TLSGD;
1565
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001566 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag);
1567 SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT,
1568 getGlobalReg(DAG, PtrVT), TGA);
Akira Hatanakaf10ee842011-12-08 21:05:38 +00001569 unsigned PtrSize = PtrVT.getSizeInBits();
1570 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1571
Benjamin Kramer64ba50a2011-12-11 12:21:34 +00001572 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001573
1574 ArgListTy Args;
1575 ArgListEntry Entry;
1576 Entry.Node = Argument;
Akira Hatanakadee6c822011-12-08 20:34:32 +00001577 Entry.Ty = PtrTy;
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001578 Args.push_back(Entry);
Jia Liuf54f60f2012-02-28 07:46:26 +00001579
Justin Holewinskiaa583972012-05-25 16:35:28 +00001580 TargetLowering::CallLoweringInfo CLI(DAG.getEntryNode(), PtrTy,
Evan Cheng65f9d192012-02-28 18:51:51 +00001581 false, false, false, false, 0, CallingConv::C,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001582 /*IsTailCall=*/false, /*doesNotRet=*/false,
Evan Cheng65f9d192012-02-28 18:51:51 +00001583 /*isReturnValueUsed=*/true,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001584 TlsGetAddr, Args, DAG, DL);
Justin Holewinskiaa583972012-05-25 16:35:28 +00001585 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001586
Akira Hatanakabff84e12011-12-14 18:26:41 +00001587 SDValue Ret = CallResult.first;
1588
Hans Wennborgaea41202012-05-04 09:40:39 +00001589 if (model != TLSModel::LocalDynamic)
Akira Hatanakabff84e12011-12-14 18:26:41 +00001590 return Ret;
1591
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001592 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanakabff84e12011-12-14 18:26:41 +00001593 MipsII::MO_DTPREL_HI);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001594 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1595 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanakabff84e12011-12-14 18:26:41 +00001596 MipsII::MO_DTPREL_LO);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001597 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1598 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret);
1599 return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001600 }
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001601
1602 SDValue Offset;
Hans Wennborgaea41202012-05-04 09:40:39 +00001603 if (model == TLSModel::InitialExec) {
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001604 // Initial Exec TLS Model
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001605 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001606 MipsII::MO_GOTTPREL);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001607 TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT),
Akira Hatanakab049aef2012-02-24 22:34:47 +00001608 TGA);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001609 Offset = DAG.getLoad(PtrVT, DL,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001610 DAG.getEntryNode(), TGA, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001611 false, false, false, 0);
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001612 } else {
1613 // Local Exec TLS Model
Hans Wennborgaea41202012-05-04 09:40:39 +00001614 assert(model == TLSModel::LocalExec);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001615 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001616 MipsII::MO_TPREL_HI);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001617 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001618 MipsII::MO_TPREL_LO);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001619 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1620 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1621 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001622 }
1623
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001624 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
1625 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001626}
1627
1628SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001629lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +00001630{
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001631 JumpTableSDNode *N = cast<JumpTableSDNode>(Op);
1632 EVT Ty = Op.getValueType();
Akira Hatanaka30f97cf2013-09-25 00:30:25 +00001633
Daniel Sandersd897b562014-03-27 10:46:12 +00001634 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !isN64())
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001635 return getAddrNonPIC(N, Ty, DAG);
1636
Daniel Sandersd897b562014-03-27 10:46:12 +00001637 return getAddrLocal(N, Ty, DAG, isN32() || isN64());
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +00001638}
1639
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001640SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001641lowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +00001642{
Bruno Cardoso Lopesfdb4cec2008-07-23 16:01:50 +00001643 // gp_rel relocation
Wesley Peck527da1b2010-11-23 03:31:01 +00001644 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001645 // but the asm printer currently doesn't support this feature without
Wesley Peck527da1b2010-11-23 03:31:01 +00001646 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopes98bda582008-07-28 19:26:25 +00001647 // stuff below.
Eli Friedman57c11da2009-08-03 02:22:28 +00001648 //if (IsInSmallSection(C->getType())) {
Owen Anderson9f944592009-08-11 20:47:22 +00001649 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1650 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peck527da1b2010-11-23 03:31:01 +00001651 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001652 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
1653 EVT Ty = Op.getValueType();
Bruno Cardoso Lopes2db07582009-11-25 12:17:58 +00001654
Daniel Sandersd897b562014-03-27 10:46:12 +00001655 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !isN64())
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001656 return getAddrNonPIC(N, Ty, DAG);
Bruno Cardoso Lopesfdb4cec2008-07-23 16:01:50 +00001657
Daniel Sandersd897b562014-03-27 10:46:12 +00001658 return getAddrLocal(N, Ty, DAG, isN32() || isN64());
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +00001659}
1660
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001661SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman31ae5862010-04-17 14:41:14 +00001662 MachineFunction &MF = DAG.getMachineFunction();
1663 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1664
Andrew Trickef9de2a2013-05-25 02:42:55 +00001665 SDLoc DL(Op);
Dan Gohman31ae5862010-04-17 14:41:14 +00001666 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1667 getPointerTy());
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +00001668
1669 // vastart just stores the address of the VarArgsFrameIndex slot into the
1670 // memory location argument.
1671 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001672 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00001673 MachinePointerInfo(SV), false, false, 0);
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +00001674}
Jia Liuf54f60f2012-02-28 07:46:26 +00001675
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001676static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG,
1677 bool HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001678 EVT TyX = Op.getOperand(0).getValueType();
1679 EVT TyY = Op.getOperand(1).getValueType();
1680 SDValue Const1 = DAG.getConstant(1, MVT::i32);
1681 SDValue Const31 = DAG.getConstant(31, MVT::i32);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001682 SDLoc DL(Op);
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001683 SDValue Res;
1684
1685 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1686 // to i32.
1687 SDValue X = (TyX == MVT::f32) ?
1688 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1689 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1690 Const1);
1691 SDValue Y = (TyY == MVT::f32) ?
1692 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
1693 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
1694 Const1);
1695
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001696 if (HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001697 // ext E, Y, 31, 1 ; extract bit31 of Y
1698 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
1699 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
1700 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
1701 } else {
1702 // sll SllX, X, 1
1703 // srl SrlX, SllX, 1
1704 // srl SrlY, Y, 31
1705 // sll SllY, SrlX, 31
1706 // or Or, SrlX, SllY
1707 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1708 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1709 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
1710 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
1711 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
1712 }
1713
1714 if (TyX == MVT::f32)
1715 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
1716
1717 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1718 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1719 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001720}
1721
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001722static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG,
1723 bool HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001724 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
1725 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
1726 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
1727 SDValue Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001728 SDLoc DL(Op);
Eric Christopher0713a9d2011-06-08 23:55:35 +00001729
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001730 // Bitcast to integer nodes.
1731 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
1732 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001733
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001734 if (HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001735 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
1736 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
1737 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
1738 DAG.getConstant(WidthY - 1, MVT::i32), Const1);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001739
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001740 if (WidthX > WidthY)
1741 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
1742 else if (WidthY > WidthX)
1743 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001744
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001745 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
1746 DAG.getConstant(WidthX - 1, MVT::i32), Const1, X);
1747 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
1748 }
1749
1750 // (d)sll SllX, X, 1
1751 // (d)srl SrlX, SllX, 1
1752 // (d)srl SrlY, Y, width(Y)-1
1753 // (d)sll SllY, SrlX, width(Y)-1
1754 // or Or, SrlX, SllY
1755 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
1756 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
1757 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
1758 DAG.getConstant(WidthY - 1, MVT::i32));
1759
1760 if (WidthX > WidthY)
1761 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
1762 else if (WidthY > WidthX)
1763 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
1764
1765 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
1766 DAG.getConstant(WidthX - 1, MVT::i32));
1767 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
1768 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001769}
1770
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00001771SDValue
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001772MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Daniel Sanders863c35a2014-04-14 16:24:12 +00001773 if (Subtarget->isGP64bit())
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001774 return lowerFCOPYSIGN64(Op, DAG, Subtarget->hasExtractInsert());
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001775
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001776 return lowerFCOPYSIGN32(Op, DAG, Subtarget->hasExtractInsert());
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001777}
1778
Akira Hatanaka66277522011-06-02 00:24:44 +00001779SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001780lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes5444a7b2011-06-16 00:40:02 +00001781 // check the depth
1782 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka15506782011-06-07 18:58:42 +00001783 "Frame address can only be determined for current frame.");
Akira Hatanaka66277522011-06-02 00:24:44 +00001784
1785 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1786 MFI->setFrameAddressIsTaken(true);
1787 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001788 SDLoc DL(Op);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001789 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), DL,
Daniel Sandersd897b562014-03-27 10:46:12 +00001790 isN64() ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka66277522011-06-02 00:24:44 +00001791 return FrameAddr;
1792}
1793
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001794SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op,
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00001795 SelectionDAG &DAG) const {
Bill Wendling908bf812014-01-06 00:43:20 +00001796 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001797 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001798
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00001799 // check the depth
1800 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
1801 "Return address can be determined only for current frame.");
1802
1803 MachineFunction &MF = DAG.getMachineFunction();
1804 MachineFrameInfo *MFI = MF.getFrameInfo();
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001805 MVT VT = Op.getSimpleValueType();
Daniel Sandersd897b562014-03-27 10:46:12 +00001806 unsigned RA = isN64() ? Mips::RA_64 : Mips::RA;
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00001807 MFI->setReturnAddressIsTaken(true);
1808
1809 // Return RA, which contains the return address. Mark it an implicit live-in.
1810 unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00001811 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT);
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00001812}
1813
Akira Hatanakac0b02062013-01-30 00:26:49 +00001814// An EH_RETURN is the result of lowering llvm.eh.return which in turn is
1815// generated from __builtin_eh_return (offset, handler)
1816// The effect of this is to adjust the stack pointer by "offset"
1817// and then branch to "handler".
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001818SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Akira Hatanakac0b02062013-01-30 00:26:49 +00001819 const {
1820 MachineFunction &MF = DAG.getMachineFunction();
1821 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1822
1823 MipsFI->setCallsEhReturn();
1824 SDValue Chain = Op.getOperand(0);
1825 SDValue Offset = Op.getOperand(1);
1826 SDValue Handler = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001827 SDLoc DL(Op);
Daniel Sandersd897b562014-03-27 10:46:12 +00001828 EVT Ty = isN64() ? MVT::i64 : MVT::i32;
Akira Hatanakac0b02062013-01-30 00:26:49 +00001829
1830 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
1831 // EH_RETURN nodes, so that instructions are emitted back-to-back.
Daniel Sandersd897b562014-03-27 10:46:12 +00001832 unsigned OffsetReg = isN64() ? Mips::V1_64 : Mips::V1;
1833 unsigned AddrReg = isN64() ? Mips::V0_64 : Mips::V0;
Akira Hatanakac0b02062013-01-30 00:26:49 +00001834 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
1835 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
1836 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
1837 DAG.getRegister(OffsetReg, Ty),
1838 DAG.getRegister(AddrReg, getPointerTy()),
1839 Chain.getValue(1));
1840}
1841
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001842SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
Akira Hatanaka5fd22482012-06-14 21:10:56 +00001843 SelectionDAG &DAG) const {
Eli Friedman26a48482011-07-27 22:21:52 +00001844 // FIXME: Need pseudo-fence for 'singlethread' fences
1845 // FIXME: Set SType for weaker fences where supported/appropriate.
1846 unsigned SType = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001847 SDLoc DL(Op);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001848 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
Eli Friedman26a48482011-07-27 22:21:52 +00001849 DAG.getConstant(SType, MVT::i32));
1850}
1851
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001852SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
Akira Hatanaka5fd22482012-06-14 21:10:56 +00001853 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001854 SDLoc DL(Op);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00001855 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
1856 SDValue Shamt = Op.getOperand(2);
1857
1858 // if shamt < 32:
1859 // lo = (shl lo, shamt)
1860 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
1861 // else:
1862 // lo = 0
1863 // hi = (shl lo, shamt[4:0])
1864 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
1865 DAG.getConstant(-1, MVT::i32));
1866 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo,
1867 DAG.getConstant(1, MVT::i32));
1868 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, ShiftRight1Lo,
1869 Not);
1870 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi, Shamt);
1871 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
1872 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, MVT::i32, Lo, Shamt);
1873 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
1874 DAG.getConstant(0x20, MVT::i32));
Akira Hatanaka5fd22482012-06-14 21:10:56 +00001875 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
1876 DAG.getConstant(0, MVT::i32), ShiftLeftLo);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00001877 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftLeftLo, Or);
1878
1879 SDValue Ops[2] = {Lo, Hi};
Craig Topper64941d92014-04-27 19:20:57 +00001880 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00001881}
1882
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001883SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00001884 bool IsSRA) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001885 SDLoc DL(Op);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00001886 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
1887 SDValue Shamt = Op.getOperand(2);
1888
1889 // if shamt < 32:
1890 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
1891 // if isSRA:
1892 // hi = (sra hi, shamt)
1893 // else:
1894 // hi = (srl hi, shamt)
1895 // else:
1896 // if isSRA:
1897 // lo = (sra hi, shamt[4:0])
1898 // hi = (sra hi, 31)
1899 // else:
1900 // lo = (srl hi, shamt[4:0])
1901 // hi = 0
1902 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
1903 DAG.getConstant(-1, MVT::i32));
1904 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi,
1905 DAG.getConstant(1, MVT::i32));
1906 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, ShiftLeft1Hi, Not);
1907 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo, Shamt);
1908 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
1909 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL, DL, MVT::i32,
1910 Hi, Shamt);
1911 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
1912 DAG.getConstant(0x20, MVT::i32));
1913 SDValue Shift31 = DAG.getNode(ISD::SRA, DL, MVT::i32, Hi,
1914 DAG.getConstant(31, MVT::i32));
1915 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftRightHi, Or);
1916 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
1917 IsSRA ? Shift31 : DAG.getConstant(0, MVT::i32),
1918 ShiftRightHi);
1919
1920 SDValue Ops[2] = {Lo, Hi};
Craig Topper64941d92014-04-27 19:20:57 +00001921 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00001922}
1923
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00001924static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00001925 SDValue Chain, SDValue Src, unsigned Offset) {
Akira Hatanaka95866182012-06-13 19:06:08 +00001926 SDValue Ptr = LD->getBasePtr();
Akira Hatanaka8f1db772012-06-02 00:03:49 +00001927 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
Akira Hatanaka95866182012-06-13 19:06:08 +00001928 EVT BasePtrVT = Ptr.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001929 SDLoc DL(LD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00001930 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
1931
1932 if (Offset)
Akira Hatanaka95866182012-06-13 19:06:08 +00001933 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00001934 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka8f1db772012-06-02 00:03:49 +00001935
1936 SDValue Ops[] = { Chain, Ptr, Src };
Craig Topper206fcd42014-04-26 19:29:41 +00001937 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00001938 LD->getMemOperand());
1939}
1940
1941// Expand an unaligned 32 or 64-bit integer load node.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001942SDValue MipsTargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka8f1db772012-06-02 00:03:49 +00001943 LoadSDNode *LD = cast<LoadSDNode>(Op);
1944 EVT MemVT = LD->getMemoryVT();
1945
1946 // Return if load is aligned or if MemVT is neither i32 nor i64.
1947 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
1948 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
1949 return SDValue();
1950
1951 bool IsLittle = Subtarget->isLittle();
1952 EVT VT = Op.getValueType();
1953 ISD::LoadExtType ExtType = LD->getExtensionType();
1954 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
1955
1956 assert((VT == MVT::i32) || (VT == MVT::i64));
1957
1958 // Expand
1959 // (set dst, (i64 (load baseptr)))
1960 // to
1961 // (set tmp, (ldl (add baseptr, 7), undef))
1962 // (set dst, (ldr baseptr, tmp))
1963 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00001964 SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00001965 IsLittle ? 7 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00001966 return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00001967 IsLittle ? 0 : 7);
1968 }
1969
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00001970 SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00001971 IsLittle ? 3 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00001972 SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00001973 IsLittle ? 0 : 3);
1974
1975 // Expand
1976 // (set dst, (i32 (load baseptr))) or
1977 // (set dst, (i64 (sextload baseptr))) or
1978 // (set dst, (i64 (extload baseptr)))
1979 // to
1980 // (set tmp, (lwl (add baseptr, 3), undef))
1981 // (set dst, (lwr baseptr, tmp))
1982 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
1983 (ExtType == ISD::EXTLOAD))
1984 return LWR;
1985
1986 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
1987
1988 // Expand
1989 // (set dst, (i64 (zextload baseptr)))
1990 // to
1991 // (set tmp0, (lwl (add baseptr, 3), undef))
1992 // (set tmp1, (lwr baseptr, tmp0))
1993 // (set tmp2, (shl tmp1, 32))
1994 // (set dst, (srl tmp2, 32))
Andrew Trickef9de2a2013-05-25 02:42:55 +00001995 SDLoc DL(LD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00001996 SDValue Const32 = DAG.getConstant(32, MVT::i32);
1997 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
Akira Hatanaka67346852012-06-04 17:46:29 +00001998 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
1999 SDValue Ops[] = { SRL, LWR.getValue(1) };
Craig Topper64941d92014-04-27 19:20:57 +00002000 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002001}
2002
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002003static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002004 SDValue Chain, unsigned Offset) {
Akira Hatanaka95866182012-06-13 19:06:08 +00002005 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
2006 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002007 SDLoc DL(SD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002008 SDVTList VTList = DAG.getVTList(MVT::Other);
2009
2010 if (Offset)
Akira Hatanaka95866182012-06-13 19:06:08 +00002011 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002012 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002013
2014 SDValue Ops[] = { Chain, Value, Ptr };
Craig Topper206fcd42014-04-26 19:29:41 +00002015 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002016 SD->getMemOperand());
2017}
2018
2019// Expand an unaligned 32 or 64-bit integer store node.
Akira Hatanakad82ee942013-05-16 20:45:17 +00002020static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG,
2021 bool IsLittle) {
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002022 SDValue Value = SD->getValue(), Chain = SD->getChain();
2023 EVT VT = Value.getValueType();
2024
2025 // Expand
2026 // (store val, baseptr) or
2027 // (truncstore val, baseptr)
2028 // to
2029 // (swl val, (add baseptr, 3))
2030 // (swr val, baseptr)
2031 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002032 SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002033 IsLittle ? 3 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002034 return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002035 }
2036
2037 assert(VT == MVT::i64);
2038
2039 // Expand
2040 // (store val, baseptr)
2041 // to
2042 // (sdl val, (add baseptr, 7))
2043 // (sdr val, baseptr)
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002044 SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2045 return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002046}
2047
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002048// Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr).
2049static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG) {
2050 SDValue Val = SD->getValue();
2051
2052 if (Val.getOpcode() != ISD::FP_TO_SINT)
2053 return SDValue();
2054
2055 EVT FPTy = EVT::getFloatingPointVT(Val.getValueSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002056 SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy,
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002057 Val.getOperand(0));
2058
Andrew Trickef9de2a2013-05-25 02:42:55 +00002059 return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(),
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002060 SD->getPointerInfo(), SD->isVolatile(),
2061 SD->isNonTemporal(), SD->getAlignment());
2062}
2063
Akira Hatanakad82ee942013-05-16 20:45:17 +00002064SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2065 StoreSDNode *SD = cast<StoreSDNode>(Op);
2066 EVT MemVT = SD->getMemoryVT();
2067
2068 // Lower unaligned integer stores.
2069 if ((SD->getAlignment() < MemVT.getSizeInBits() / 8) &&
2070 ((MemVT == MVT::i32) || (MemVT == MVT::i64)))
2071 return lowerUnalignedIntStore(SD, DAG, Subtarget->isLittle());
2072
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002073 return lowerFP_TO_SINT_STORE(SD, DAG);
Akira Hatanakad82ee942013-05-16 20:45:17 +00002074}
2075
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002076SDValue MipsTargetLowering::lowerADD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002077 if (Op->getOperand(0).getOpcode() != ISD::FRAMEADDR
2078 || cast<ConstantSDNode>
2079 (Op->getOperand(0).getOperand(0))->getZExtValue() != 0
2080 || Op->getOperand(1).getOpcode() != ISD::FRAME_TO_ARGS_OFFSET)
2081 return SDValue();
2082
2083 // The pattern
2084 // (add (frameaddr 0), (frame_to_args_offset))
2085 // results from lowering llvm.eh.dwarf.cfa intrinsic. Transform it to
2086 // (add FrameObject, 0)
2087 // where FrameObject is a fixed StackObject with offset 0 which points to
2088 // the old stack pointer.
2089 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2090 EVT ValTy = Op->getValueType(0);
2091 int FI = MFI->CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
2092 SDValue InArgsAddr = DAG.getFrameIndex(FI, ValTy);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002093 return DAG.getNode(ISD::ADD, SDLoc(Op), ValTy, InArgsAddr,
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002094 DAG.getConstant(0, ValTy));
2095}
2096
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002097SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op,
2098 SelectionDAG &DAG) const {
2099 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002100 SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy,
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002101 Op.getOperand(0));
Andrew Trickef9de2a2013-05-25 02:42:55 +00002102 return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc);
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002103}
2104
Akira Hatanakae2489122011-04-15 21:51:11 +00002105//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002106// Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002107//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002108
Akira Hatanakae2489122011-04-15 21:51:11 +00002109//===----------------------------------------------------------------------===//
Wesley Peck527da1b2010-11-23 03:31:01 +00002110// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002111// Mips O32 ABI rules:
2112// ---
2113// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peck527da1b2010-11-23 03:31:01 +00002114// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002115// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peck527da1b2010-11-23 03:31:01 +00002116// f64 - Only passed in two aliased f32 registers if no int reg has been used
2117// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002118// not used, it must be shadowed. If only A3 is avaiable, shadow it and
2119// go to stack.
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002120//
2121// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanakae2489122011-04-15 21:51:11 +00002122//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002123
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00002124static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2125 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
Craig Topper840beec2014-04-04 05:16:06 +00002126 CCState &State, const MCPhysReg *F64Regs) {
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002127
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00002128 static const unsigned IntRegsSize = 4, FloatRegsSize = 2;
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002129
Craig Topper840beec2014-04-04 05:16:06 +00002130 static const MCPhysReg IntRegs[] = { Mips::A0, Mips::A1, Mips::A2, Mips::A3 };
2131 static const MCPhysReg F32Regs[] = { Mips::F12, Mips::F14 };
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002132
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002133 // Do not process byval args here.
2134 if (ArgFlags.isByVal())
2135 return true;
Akira Hatanaka5e16c6a2011-05-24 19:18:33 +00002136
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002137 // Promote i8 and i16
2138 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
2139 LocVT = MVT::i32;
2140 if (ArgFlags.isSExt())
2141 LocInfo = CCValAssign::SExt;
2142 else if (ArgFlags.isZExt())
2143 LocInfo = CCValAssign::ZExt;
2144 else
2145 LocInfo = CCValAssign::AExt;
2146 }
2147
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002148 unsigned Reg;
2149
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002150 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
2151 // is true: function is vararg, argument is 3rd or higher, there is previous
2152 // argument which is not f32 or f64.
2153 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
2154 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanaka9e6a8cc2011-05-19 20:29:48 +00002155 unsigned OrigAlign = ArgFlags.getOrigAlign();
2156 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002157
2158 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002159 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanaka9e6a8cc2011-05-19 20:29:48 +00002160 // If this is the first part of an i64 arg,
2161 // the allocated register must be either A0 or A2.
2162 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
2163 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002164 LocVT = MVT::i32;
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002165 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
2166 // Allocate int register and shadow next int register. If first
2167 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002168 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2169 if (Reg == Mips::A1 || Reg == Mips::A3)
2170 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2171 State.AllocateReg(IntRegs, IntRegsSize);
2172 LocVT = MVT::i32;
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002173 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
2174 // we are guaranteed to find an available float register
2175 if (ValVT == MVT::f32) {
2176 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
2177 // Shadow int register
2178 State.AllocateReg(IntRegs, IntRegsSize);
2179 } else {
2180 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
2181 // Shadow int registers
2182 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
2183 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
2184 State.AllocateReg(IntRegs, IntRegsSize);
2185 State.AllocateReg(IntRegs, IntRegsSize);
2186 }
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002187 } else
2188 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002189
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002190 if (!Reg) {
2191 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3,
2192 OrigAlign);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002193 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002194 } else
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002195 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002196
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002197 return false;
Akira Hatanaka202f6402011-11-12 02:20:46 +00002198}
2199
Akira Hatanakabfb66242013-08-20 23:38:40 +00002200static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT,
2201 MVT LocVT, CCValAssign::LocInfo LocInfo,
2202 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002203 static const MCPhysReg F64Regs[] = { Mips::D6, Mips::D7 };
Akira Hatanakabfb66242013-08-20 23:38:40 +00002204
2205 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2206}
2207
2208static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT,
2209 MVT LocVT, CCValAssign::LocInfo LocInfo,
2210 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002211 static const MCPhysReg F64Regs[] = { Mips::D12_64, Mips::D14_64 };
Akira Hatanakabfb66242013-08-20 23:38:40 +00002212
2213 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2214}
2215
Akira Hatanaka202f6402011-11-12 02:20:46 +00002216#include "MipsGenCallingConv.inc"
2217
Akira Hatanakae2489122011-04-15 21:51:11 +00002218//===----------------------------------------------------------------------===//
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002219// Call Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002220//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002221
Akira Hatanaka61bbcce2011-09-23 00:58:33 +00002222// Return next O32 integer argument register.
2223static unsigned getNextIntArgReg(unsigned Reg) {
2224 assert((Reg == Mips::A0) || (Reg == Mips::A2));
2225 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
2226}
2227
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002228SDValue
2229MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002230 SDValue Chain, SDValue Arg, SDLoc DL,
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002231 bool IsTailCall, SelectionDAG &DAG) const {
2232 if (!IsTailCall) {
2233 SDValue PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr,
2234 DAG.getIntPtrConstant(Offset));
2235 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo(), false,
2236 false, 0);
2237 }
2238
2239 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2240 int FI = MFI->CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
2241 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2242 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(),
2243 /*isVolatile=*/ true, false, 0);
2244}
2245
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002246void MipsTargetLowering::
2247getOpndList(SmallVectorImpl<SDValue> &Ops,
2248 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
2249 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
2250 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
2251 // Insert node "GP copy globalreg" before call to function.
2252 //
2253 // R_MIPS_CALL* operators (emitted when non-internal functions are called
2254 // in PIC mode) allow symbols to be resolved via lazy binding.
2255 // The lazy binding stub requires GP to point to the GOT.
2256 if (IsPICCall && !InternalLinkage) {
Daniel Sandersd897b562014-03-27 10:46:12 +00002257 unsigned GPReg = isN64() ? Mips::GP_64 : Mips::GP;
2258 EVT Ty = isN64() ? MVT::i64 : MVT::i32;
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002259 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty)));
2260 }
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002261
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002262 // Build a sequence of copy-to-reg nodes chained together with token
2263 // chain and flag operands which copy the outgoing args into registers.
2264 // The InFlag in necessary since all emitted instructions must be
2265 // stuck together.
2266 SDValue InFlag;
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002267
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002268 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2269 Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, RegsToPass[i].first,
2270 RegsToPass[i].second, InFlag);
2271 InFlag = Chain.getValue(1);
2272 }
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002273
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002274 // Add argument registers to the end of the list so that they are
2275 // known live into the call.
2276 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2277 Ops.push_back(CLI.DAG.getRegister(RegsToPass[i].first,
2278 RegsToPass[i].second.getValueType()));
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002279
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002280 // Add a register mask operand representing the call-preserved registers.
2281 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2282 const uint32_t *Mask = TRI->getCallPreservedMask(CLI.CallConv);
2283 assert(Mask && "Missing call preserved mask for calling convention");
Reed Kotler783c7942013-05-10 22:25:39 +00002284 if (Subtarget->inMips16HardFloat()) {
2285 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
2286 llvm::StringRef Sym = G->getGlobal()->getName();
2287 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
Reed Kotler3230e722013-12-12 02:41:11 +00002288 if (F && F->hasFnAttribute("__Mips16RetHelper")) {
Reed Kotler783c7942013-05-10 22:25:39 +00002289 Mask = MipsRegisterInfo::getMips16RetHelperMask();
2290 }
2291 }
2292 }
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002293 Ops.push_back(CLI.DAG.getRegisterMask(Mask));
2294
2295 if (InFlag.getNode())
2296 Ops.push_back(InFlag);
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002297}
2298
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002299/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman624801e2009-01-26 03:15:54 +00002300/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002301SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00002302MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002303 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00002304 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002305 SDLoc DL = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00002306 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
2307 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
2308 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Akira Hatanakabeda2242012-07-31 18:46:41 +00002309 SDValue Chain = CLI.Chain;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002310 SDValue Callee = CLI.Callee;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002311 bool &IsTailCall = CLI.IsTailCall;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002312 CallingConv::ID CallConv = CLI.CallConv;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002313 bool IsVarArg = CLI.IsVarArg;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002314
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002315 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002316 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanaka7c619f12011-05-20 21:39:54 +00002317 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002318 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +00002319 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002320
2321 // Analyze operands of the call, assigning locations to each operand.
2322 SmallVector<CCValAssign, 16> ArgLocs;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002323 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00002324 getTargetMachine(), ArgLocs, *DAG.getContext());
Reed Kotler783c7942013-05-10 22:25:39 +00002325 MipsCC::SpecialCallingConvType SpecialCallingConv =
2326 getSpecialCallingConv(Callee);
Daniel Sandersd897b562014-03-27 10:46:12 +00002327 MipsCC MipsCCInfo(CallConv, isO32(), Subtarget->isFP64bit(), CCInfo,
Akira Hatanakabfb66242013-08-20 23:38:40 +00002328 SpecialCallingConv);
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002329
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002330 MipsCCInfo.analyzeCallOperands(Outs, IsVarArg,
Reed Kotlerc03807a2013-08-30 19:40:56 +00002331 Subtarget->mipsSEUsesSoftFloat(),
Akira Hatanaka3b7391d2013-03-05 22:20:28 +00002332 Callee.getNode(), CLI.Args);
Wesley Peck527da1b2010-11-23 03:31:01 +00002333
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002334 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka195a1e22011-06-08 17:39:33 +00002335 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka97ba7692012-07-26 23:27:01 +00002336
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002337 // Check if it's really possible to do a tail call.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002338 if (IsTailCall)
2339 IsTailCall =
2340 isEligibleForTailCallOptimization(MipsCCInfo, NextStackOffset,
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002341 *MF.getInfo<MipsFunctionInfo>());
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002342
Reid Kleckner5772b772014-04-24 20:14:34 +00002343 if (!IsTailCall && CLI.CS && CLI.CS->isMustTailCall())
2344 report_fatal_error("failed to perform tail call elimination on a call "
2345 "site marked musttail");
2346
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002347 if (IsTailCall)
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002348 ++NumTailCalls;
2349
Akira Hatanaka79738332011-09-19 20:26:02 +00002350 // Chain is the output chain of the last Load/Store or CopyToReg node.
2351 // ByValChain is the output chain of the last Memcpy node created for copying
2352 // byval arguments to the stack.
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002353 unsigned StackAlignment = TFL->getStackAlignment();
2354 NextStackOffset = RoundUpToAlignment(NextStackOffset, StackAlignment);
Akira Hatanaka79738332011-09-19 20:26:02 +00002355 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002356
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002357 if (!IsTailCall)
Andrew Trickad6d08a2013-05-29 22:03:55 +00002358 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffsetVal, DL);
Akira Hatanakabeda2242012-07-31 18:46:41 +00002359
Daniel Sandersd897b562014-03-27 10:46:12 +00002360 SDValue StackPtr = DAG.getCopyFromReg(
2361 Chain, DL, isN64() ? Mips::SP_64 : Mips::SP, getPointerTy());
Akira Hatanaka195a1e22011-06-08 17:39:33 +00002362
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002363 // With EABI is it possible to have 16 args on registers.
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002364 std::deque< std::pair<unsigned, SDValue> > RegsToPass;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002365 SmallVector<SDValue, 8> MemOpChains;
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002366 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002367
2368 // Walk the register/memloc assignments, inserting copies/loads.
2369 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002370 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002371 CCValAssign &VA = ArgLocs[i];
Akira Hatanakab20a3252011-10-28 19:49:00 +00002372 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka19891f82011-11-12 02:34:50 +00002373 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2374
2375 // ByVal Arg.
2376 if (Flags.isByVal()) {
2377 assert(Flags.getByValSize() &&
2378 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002379 assert(ByValArg != MipsCCInfo.byval_end());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002380 assert(!IsTailCall &&
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002381 "Do not tail-call optimize if there is a byval argument.");
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002382 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002383 MipsCCInfo, *ByValArg, Flags, Subtarget->isLittle());
2384 ++ByValArg;
Akira Hatanaka19891f82011-11-12 02:34:50 +00002385 continue;
2386 }
Jia Liuf54f60f2012-02-28 07:46:26 +00002387
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002388 // Promote the value if needed.
2389 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002390 default: llvm_unreachable("Unknown loc info!");
Wesley Peck527da1b2010-11-23 03:31:01 +00002391 case CCValAssign::Full:
Akira Hatanakab20a3252011-10-28 19:49:00 +00002392 if (VA.isRegLoc()) {
2393 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
Akira Hatanaka3b7391d2013-03-05 22:20:28 +00002394 (ValVT == MVT::f64 && LocVT == MVT::i64) ||
2395 (ValVT == MVT::i64 && LocVT == MVT::f64))
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002396 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
Akira Hatanakab20a3252011-10-28 19:49:00 +00002397 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002398 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanakae2489122011-04-15 21:51:11 +00002399 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002400 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanakaaef55c82011-04-15 21:00:26 +00002401 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka27916972011-04-15 19:52:08 +00002402 if (!Subtarget->isLittle())
2403 std::swap(Lo, Hi);
Jia Liuf54f60f2012-02-28 07:46:26 +00002404 unsigned LocRegLo = VA.getLocReg();
Akira Hatanaka61bbcce2011-09-23 00:58:33 +00002405 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2406 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2407 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002408 continue;
Wesley Peck527da1b2010-11-23 03:31:01 +00002409 }
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002410 }
2411 break;
Chris Lattner52f16de2008-03-17 06:57:02 +00002412 case CCValAssign::SExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002413 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002414 break;
2415 case CCValAssign::ZExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002416 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002417 break;
2418 case CCValAssign::AExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002419 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002420 break;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002421 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002422
2423 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +00002424 // RegsToPass vector
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002425 if (VA.isRegLoc()) {
2426 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattner52f16de2008-03-17 06:57:02 +00002427 continue;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002428 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002429
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002430 // Register can't get to this point...
Chris Lattner52f16de2008-03-17 06:57:02 +00002431 assert(VA.isMemLoc());
Wesley Peck527da1b2010-11-23 03:31:01 +00002432
Wesley Peck527da1b2010-11-23 03:31:01 +00002433 // emit ISD::STORE whichs stores the
Chris Lattner52f16de2008-03-17 06:57:02 +00002434 // parameter value to a stack Location
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002435 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002436 Chain, Arg, DL, IsTailCall, DAG));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002437 }
2438
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002439 // Transform all store nodes into one single node because all store
2440 // nodes are independent of each other.
Wesley Peck527da1b2010-11-23 03:31:01 +00002441 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00002442 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002443
Bill Wendling24c79f22008-09-16 21:48:12 +00002444 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peck527da1b2010-11-23 03:31:01 +00002445 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2446 // node so that legalize doesn't hack it.
Daniel Sandersd897b562014-03-27 10:46:12 +00002447 bool IsPICCall = (isN64() || IsPIC); // true if calls are translated to
2448 // jalr $25
Akira Hatanakacf9a61b2012-12-13 03:17:29 +00002449 bool GlobalOrExternal = false, InternalLinkage = false;
Akira Hatanakad6f1c582011-04-07 19:51:44 +00002450 SDValue CalleeLo;
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002451 EVT Ty = Callee.getValueType();
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002452
2453 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002454 if (IsPICCall) {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002455 const GlobalValue *Val = G->getGlobal();
2456 InternalLinkage = Val->hasInternalLinkage();
Akira Hatanakacf9a61b2012-12-13 03:17:29 +00002457
2458 if (InternalLinkage)
Daniel Sandersd897b562014-03-27 10:46:12 +00002459 Callee = getAddrLocal(G, Ty, DAG, isN32() || isN64());
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00002460 else if (LargeGOT)
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002461 Callee = getAddrGlobalLargeGOT(G, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002462 MipsII::MO_CALL_LO16, Chain,
2463 FuncInfo->callPtrInfo(Val));
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002464 else
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002465 Callee = getAddrGlobal(G, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
2466 FuncInfo->callPtrInfo(Val));
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002467 } else
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002468 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, getPointerTy(), 0,
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002469 MipsII::MO_NO_FLAG);
Akira Hatanaka8e16aac2011-12-09 01:45:12 +00002470 GlobalOrExternal = true;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002471 }
2472 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002473 const char *Sym = S->getSymbol();
2474
Daniel Sandersd897b562014-03-27 10:46:12 +00002475 if (!isN64() && !IsPIC) // !N64 && static
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002476 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(),
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002477 MipsII::MO_NO_FLAG);
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00002478 else if (LargeGOT)
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002479 Callee = getAddrGlobalLargeGOT(S, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002480 MipsII::MO_CALL_LO16, Chain,
2481 FuncInfo->callPtrInfo(Sym));
Akira Hatanaka02b0e482013-02-22 21:10:03 +00002482 else // N64 || PIC
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002483 Callee = getAddrGlobal(S, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
2484 FuncInfo->callPtrInfo(Sym));
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002485
Akira Hatanaka8e16aac2011-12-09 01:45:12 +00002486 GlobalOrExternal = true;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002487 }
2488
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002489 SmallVector<SDValue, 8> Ops(1, Chain);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002490 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002491
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002492 getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal, InternalLinkage,
2493 CLI, Callee, Chain);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002494
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002495 if (IsTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +00002496 return DAG.getNode(MipsISD::TailCall, DL, MVT::Other, Ops);
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002497
Craig Topper48d114b2014-04-26 18:35:24 +00002498 Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, Ops);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002499 SDValue InFlag = Chain.getValue(1);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002500
Bruno Cardoso Lopes193e64c2010-01-30 18:32:07 +00002501 // Create the CALLSEQ_END node.
Akira Hatanaka97ba7692012-07-26 23:27:01 +00002502 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal,
Andrew Trickad6d08a2013-05-29 22:03:55 +00002503 DAG.getIntPtrConstant(0, true), InFlag, DL);
Bruno Cardoso Lopes193e64c2010-01-30 18:32:07 +00002504 InFlag = Chain.getValue(1);
2505
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002506 // Handle result values, copying them out of physregs into vregs that we
2507 // return.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002508 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg,
2509 Ins, DL, DAG, InVals, CLI.Callee.getNode(), CLI.RetTy);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002510}
2511
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002512/// LowerCallResult - Lower the result values of a call into the
2513/// appropriate copies out of appropriate physical registers.
2514SDValue
2515MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002516 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002517 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002518 SDLoc DL, SelectionDAG &DAG,
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002519 SmallVectorImpl<SDValue> &InVals,
2520 const SDNode *CallNode,
2521 const Type *RetTy) const {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002522 // Assign locations to each value returned by this call.
2523 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002524 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka5fd22482012-06-14 21:10:56 +00002525 getTargetMachine(), RVLocs, *DAG.getContext());
Daniel Sandersd897b562014-03-27 10:46:12 +00002526 MipsCC MipsCCInfo(CallConv, isO32(), Subtarget->isFP64bit(), CCInfo);
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002527
Reed Kotlerc03807a2013-08-30 19:40:56 +00002528 MipsCCInfo.analyzeCallResult(Ins, Subtarget->mipsSEUsesSoftFloat(),
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002529 CallNode, RetTy);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002530
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002531 // Copy all of the result registers out of their specified physreg.
2532 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002533 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(),
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002534 RVLocs[i].getLocVT(), InFlag);
2535 Chain = Val.getValue(1);
2536 InFlag = Val.getValue(2);
2537
2538 if (RVLocs[i].getValVT() != RVLocs[i].getLocVT())
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002539 Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getValVT(), Val);
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002540
2541 InVals.push_back(Val);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002542 }
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +00002543
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002544 return Chain;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002545}
2546
Akira Hatanakae2489122011-04-15 21:51:11 +00002547//===----------------------------------------------------------------------===//
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002548// Formal Arguments Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002549//===----------------------------------------------------------------------===//
Wesley Peck527da1b2010-11-23 03:31:01 +00002550/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002551/// and generate load operations for arguments places on the stack.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002552SDValue
2553MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanakaaef55c82011-04-15 21:00:26 +00002554 CallingConv::ID CallConv,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002555 bool IsVarArg,
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00002556 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002557 SDLoc DL, SelectionDAG &DAG,
Akira Hatanakaaef55c82011-04-15 21:00:26 +00002558 SmallVectorImpl<SDValue> &InVals)
Akira Hatanakae2489122011-04-15 21:51:11 +00002559 const {
Bruno Cardoso Lopesa01ede22008-08-04 07:12:52 +00002560 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002561 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopes14033fb2007-08-28 05:08:16 +00002562 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002563
Dan Gohman31ae5862010-04-17 14:41:14 +00002564 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002565
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002566 // Used with vargs to acumulate store chains.
2567 std::vector<SDValue> OutChains;
2568
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002569 // Assign locations to all of the incoming arguments.
2570 SmallVector<CCValAssign, 16> ArgLocs;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002571 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00002572 getTargetMachine(), ArgLocs, *DAG.getContext());
Daniel Sandersd897b562014-03-27 10:46:12 +00002573 MipsCC MipsCCInfo(CallConv, isO32(), Subtarget->isFP64bit(), CCInfo);
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002574 Function::const_arg_iterator FuncArg =
2575 DAG.getMachineFunction().getFunction()->arg_begin();
Reed Kotlerc03807a2013-08-30 19:40:56 +00002576 bool UseSoftFloat = Subtarget->mipsSEUsesSoftFloat();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002577
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002578 MipsCCInfo.analyzeFormalArguments(Ins, UseSoftFloat, FuncArg);
Akira Hatanaka4866fe12012-10-30 19:37:25 +00002579 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(),
2580 MipsCCInfo.hasByValArg());
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002581
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00002582 unsigned CurArgIdx = 0;
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002583 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002584
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00002585 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002586 CCValAssign &VA = ArgLocs[i];
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00002587 std::advance(FuncArg, Ins[i].OrigArgIndex - CurArgIdx);
2588 CurArgIdx = Ins[i].OrigArgIndex;
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002589 EVT ValVT = VA.getValVT();
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002590 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2591 bool IsRegLoc = VA.isRegLoc();
2592
2593 if (Flags.isByVal()) {
2594 assert(Flags.getByValSize() &&
2595 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002596 assert(ByValArg != MipsCCInfo.byval_end());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002597 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg,
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002598 MipsCCInfo, *ByValArg);
2599 ++ByValArg;
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002600 continue;
2601 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002602
2603 // Arguments stored on registers
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002604 if (IsRegLoc) {
Akira Hatanaka7d822522013-10-28 21:21:36 +00002605 MVT RegVT = VA.getLocVT();
Akira Hatanakacb4a1a82011-05-24 00:23:52 +00002606 unsigned ArgReg = VA.getLocReg();
Akira Hatanaka7d822522013-10-28 21:21:36 +00002607 const TargetRegisterClass *RC = getRegClassFor(RegVT);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002608
Wesley Peck527da1b2010-11-23 03:31:01 +00002609 // Transform the arguments stored on
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002610 // physical registers into virtual ones
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002611 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC);
2612 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
Wesley Peck527da1b2010-11-23 03:31:01 +00002613
2614 // If this is an 8 or 16-bit value, it has been passed promoted
2615 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002616 // truncate to the right size.
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002617 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattner3c049702009-03-26 05:28:14 +00002618 unsigned Opcode = 0;
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002619 if (VA.getLocInfo() == CCValAssign::SExt)
2620 Opcode = ISD::AssertSext;
2621 else if (VA.getLocInfo() == CCValAssign::ZExt)
2622 Opcode = ISD::AssertZext;
Chris Lattner3c049702009-03-26 05:28:14 +00002623 if (Opcode)
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002624 ArgValue = DAG.getNode(Opcode, DL, RegVT, ArgValue,
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002625 DAG.getValueType(ValVT));
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002626 ArgValue = DAG.getNode(ISD::TRUNCATE, DL, ValVT, ArgValue);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002627 }
2628
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002629 // Handle floating point arguments passed in integer registers and
2630 // long double arguments passed in floating point registers.
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002631 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002632 (RegVT == MVT::i64 && ValVT == MVT::f64) ||
2633 (RegVT == MVT::f64 && ValVT == MVT::i64))
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002634 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
Daniel Sandersd897b562014-03-27 10:46:12 +00002635 else if (isO32() && RegVT == MVT::i32 && ValVT == MVT::f64) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002636 unsigned Reg2 = addLiveIn(DAG.getMachineFunction(),
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002637 getNextIntArgReg(ArgReg), RC);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002638 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT);
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002639 if (!Subtarget->isLittle())
2640 std::swap(ArgValue, ArgValue2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002641 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64,
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002642 ArgValue, ArgValue2);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002643 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002644
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002645 InVals.push_back(ArgValue);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002646 } else { // VA.isRegLoc()
2647
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002648 // sanity check
2649 assert(VA.isMemLoc());
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002650
Wesley Peck527da1b2010-11-23 03:31:01 +00002651 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002652 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakacb4a1a82011-05-24 00:23:52 +00002653 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002654
2655 // Create load nodes to retrieve arguments from the stack
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002656 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Akira Hatanakad1c58ed2013-11-09 02:38:51 +00002657 SDValue Load = DAG.getLoad(ValVT, DL, Chain, FIN,
2658 MachinePointerInfo::getFixedStack(FI),
2659 false, false, false, 0);
2660 InVals.push_back(Load);
2661 OutChains.push_back(Load.getValue(1));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002662 }
Reid Kleckner7a59e082014-05-12 22:01:27 +00002663 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002664
Reid Kleckner7a59e082014-05-12 22:01:27 +00002665 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Reid Kleckner79418562014-05-09 22:32:13 +00002666 // The mips ABIs for returning structs by value requires that we copy
2667 // the sret argument into $v0 for the return. Save the argument into
2668 // a virtual register so that we can access it from the return points.
Reid Kleckner7a59e082014-05-12 22:01:27 +00002669 if (Ins[i].Flags.isSRet()) {
Reid Kleckner79418562014-05-09 22:32:13 +00002670 unsigned Reg = MipsFI->getSRetReturnReg();
2671 if (!Reg) {
2672 Reg = MF.getRegInfo().createVirtualRegister(
2673 getRegClassFor(isN64() ? MVT::i64 : MVT::i32));
2674 MipsFI->setSRetReturnReg(Reg);
2675 }
Reid Kleckner7a59e082014-05-12 22:01:27 +00002676 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[i]);
Reid Kleckner79418562014-05-09 22:32:13 +00002677 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
Reid Kleckner7a59e082014-05-12 22:01:27 +00002678 break;
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002679 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002680 }
2681
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002682 if (IsVarArg)
2683 writeVarArgRegs(OutChains, MipsCCInfo, Chain, DL, DAG);
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002684
Wesley Peck527da1b2010-11-23 03:31:01 +00002685 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002686 // the size of Ins and InVals. This only happens when on varg functions
2687 if (!OutChains.empty()) {
2688 OutChains.push_back(Chain);
Craig Topper48d114b2014-04-26 18:35:24 +00002689 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002690 }
2691
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002692 return Chain;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002693}
2694
Akira Hatanakae2489122011-04-15 21:51:11 +00002695//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002696// Return Value Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002697//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002698
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00002699bool
2700MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002701 MachineFunction &MF, bool IsVarArg,
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00002702 const SmallVectorImpl<ISD::OutputArg> &Outs,
2703 LLVMContext &Context) const {
2704 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002705 CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(),
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00002706 RVLocs, Context);
2707 return CCInfo.CheckReturn(Outs, RetCC_Mips);
2708}
2709
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002710SDValue
2711MipsTargetLowering::LowerReturn(SDValue Chain,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002712 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002713 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002714 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002715 SDLoc DL, SelectionDAG &DAG) const {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002716 // CCValAssign - represent the assignment of
2717 // the return value to a location
2718 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002719 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002720
2721 // CCState - Info about the registers and stack slot.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002722 CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(), RVLocs,
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002723 *DAG.getContext());
Daniel Sandersd897b562014-03-27 10:46:12 +00002724 MipsCC MipsCCInfo(CallConv, isO32(), Subtarget->isFP64bit(), CCInfo);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002725
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002726 // Analyze return values.
Reed Kotlerc03807a2013-08-30 19:40:56 +00002727 MipsCCInfo.analyzeReturn(Outs, Subtarget->mipsSEUsesSoftFloat(),
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002728 MF.getFunction()->getReturnType());
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002729
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002730 SDValue Flag;
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00002731 SmallVector<SDValue, 4> RetOps(1, Chain);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002732
2733 // Copy the result values into the output registers.
2734 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002735 SDValue Val = OutVals[i];
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002736 CCValAssign &VA = RVLocs[i];
2737 assert(VA.isRegLoc() && "Can only return in registers!");
2738
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002739 if (RVLocs[i].getValVT() != RVLocs[i].getLocVT())
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002740 Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getLocVT(), Val);
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002741
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002742 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002743
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00002744 // Guarantee that all emitted copies are stuck together with flags.
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002745 Flag = Chain.getValue(1);
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00002746 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002747 }
2748
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002749 // The mips ABIs for returning structs by value requires that we copy
2750 // the sret argument into $v0 for the return. We saved the argument into
2751 // a virtual register in the entry block, so now we copy the value out
2752 // and into $v0.
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002753 if (MF.getFunction()->hasStructRetAttr()) {
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002754 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2755 unsigned Reg = MipsFI->getSRetReturnReg();
2756
Wesley Peck527da1b2010-11-23 03:31:01 +00002757 if (!Reg)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002758 llvm_unreachable("sret virtual register not created in the entry block");
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002759 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Daniel Sandersd897b562014-03-27 10:46:12 +00002760 unsigned V0 = isN64() ? Mips::V0_64 : Mips::V0;
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002761
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002762 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002763 Flag = Chain.getValue(1);
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00002764 RetOps.push_back(DAG.getRegister(V0, getPointerTy()));
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002765 }
2766
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00002767 RetOps[0] = Chain; // Update chain.
Akira Hatanakaefff7b72012-07-10 00:19:06 +00002768
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00002769 // Add the flag if we have it.
2770 if (Flag.getNode())
2771 RetOps.push_back(Flag);
2772
2773 // Return on Mips is always a "jr $ra"
Craig Topper48d114b2014-04-26 18:35:24 +00002774 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, RetOps);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002775}
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002776
Akira Hatanakae2489122011-04-15 21:51:11 +00002777//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002778// Mips Inline Assembly Support
Akira Hatanakae2489122011-04-15 21:51:11 +00002779//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002780
2781/// getConstraintType - Given a constraint letter, return the type of
2782/// constraint it is for this target.
2783MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peck527da1b2010-11-23 03:31:01 +00002784getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002785{
Daniel Sanders8b59af12013-11-12 12:56:01 +00002786 // Mips specific constraints
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002787 // GCC config/mips/constraints.md
2788 //
Wesley Peck527da1b2010-11-23 03:31:01 +00002789 // 'd' : An address register. Equivalent to r
2790 // unless generating MIPS16 code.
2791 // 'y' : Equivalent to r; retained for
2792 // backwards compatibility.
Eric Christophere3c494d2012-05-07 06:25:10 +00002793 // 'c' : A register suitable for use in an indirect
2794 // jump. This will always be $25 for -mabicalls.
Eric Christopher0d8c15d2012-05-07 06:25:19 +00002795 // 'l' : The lo register. 1 word storage.
2796 // 'x' : The hilo register pair. Double word storage.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002797 if (Constraint.size() == 1) {
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002798 switch (Constraint[0]) {
2799 default : break;
Wesley Peck527da1b2010-11-23 03:31:01 +00002800 case 'd':
2801 case 'y':
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002802 case 'f':
Eric Christophere3c494d2012-05-07 06:25:10 +00002803 case 'c':
Eric Christopher9c492e62012-05-07 06:25:15 +00002804 case 'l':
Eric Christopher0d8c15d2012-05-07 06:25:19 +00002805 case 'x':
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002806 return C_RegisterClass;
Jack Carter0e149b02013-03-04 21:33:15 +00002807 case 'R':
2808 return C_Memory;
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002809 }
2810 }
2811 return TargetLowering::getConstraintType(Constraint);
2812}
2813
John Thompsone8360b72010-10-29 17:29:13 +00002814/// Examine constraint type and operand type and determine a weight value.
2815/// This object must already have been set up with the operand type
2816/// and the current alternative constraint selected.
2817TargetLowering::ConstraintWeight
2818MipsTargetLowering::getSingleConstraintMatchWeight(
2819 AsmOperandInfo &info, const char *constraint) const {
2820 ConstraintWeight weight = CW_Invalid;
2821 Value *CallOperandVal = info.CallOperandVal;
2822 // If we don't have a value, we can't do a match,
2823 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +00002824 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +00002825 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +00002826 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +00002827 // Look at the constraint type.
2828 switch (*constraint) {
2829 default:
2830 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2831 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00002832 case 'd':
2833 case 'y':
John Thompsone8360b72010-10-29 17:29:13 +00002834 if (type->isIntegerTy())
2835 weight = CW_Register;
2836 break;
Daniel Sanders8b59af12013-11-12 12:56:01 +00002837 case 'f': // FPU or MSA register
2838 if (Subtarget->hasMSA() && type->isVectorTy() &&
2839 cast<VectorType>(type)->getBitWidth() == 128)
2840 weight = CW_Register;
2841 else if (type->isFloatTy())
John Thompsone8360b72010-10-29 17:29:13 +00002842 weight = CW_Register;
2843 break;
Eric Christophere3c494d2012-05-07 06:25:10 +00002844 case 'c': // $25 for indirect jumps
Eric Christopher9c492e62012-05-07 06:25:15 +00002845 case 'l': // lo register
Eric Christopher0d8c15d2012-05-07 06:25:19 +00002846 case 'x': // hilo register pair
Daniel Sanders8b59af12013-11-12 12:56:01 +00002847 if (type->isIntegerTy())
Eric Christophere3c494d2012-05-07 06:25:10 +00002848 weight = CW_SpecificReg;
Daniel Sanders8b59af12013-11-12 12:56:01 +00002849 break;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00002850 case 'I': // signed 16 bit immediate
Eric Christopher7201e1b2012-05-07 03:13:42 +00002851 case 'J': // integer zero
Eric Christopher3ff88a02012-05-07 05:46:29 +00002852 case 'K': // unsigned 16 bit immediate
Eric Christopher1109b342012-05-07 05:46:37 +00002853 case 'L': // signed 32 bit immediate where lower 16 bits are 0
Eric Christophere07aa432012-05-07 05:46:43 +00002854 case 'N': // immediate in the range of -65535 to -1 (inclusive)
Eric Christopher470578a2012-05-07 05:46:48 +00002855 case 'O': // signed 15 bit immediate (+- 16383)
Eric Christopherc18ae4a2012-05-07 06:25:02 +00002856 case 'P': // immediate in the range of 65535 to 1 (inclusive)
Eric Christopher1d6c89e2012-05-07 03:13:32 +00002857 if (isa<ConstantInt>(CallOperandVal))
2858 weight = CW_Constant;
2859 break;
Jack Carter0e149b02013-03-04 21:33:15 +00002860 case 'R':
2861 weight = CW_Memory;
2862 break;
John Thompsone8360b72010-10-29 17:29:13 +00002863 }
2864 return weight;
2865}
2866
Akira Hatanaka7473b472013-08-14 00:21:25 +00002867/// This is a helper function to parse a physical register string and split it
2868/// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag
2869/// that is returned indicates whether parsing was successful. The second flag
2870/// is true if the numeric part exists.
2871static std::pair<bool, bool>
2872parsePhysicalReg(const StringRef &C, std::string &Prefix,
2873 unsigned long long &Reg) {
2874 if (C.front() != '{' || C.back() != '}')
2875 return std::make_pair(false, false);
2876
2877 // Search for the first numeric character.
2878 StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1;
2879 I = std::find_if(B, E, std::ptr_fun(isdigit));
2880
2881 Prefix.assign(B, I - B);
2882
2883 // The second flag is set to false if no numeric characters were found.
2884 if (I == E)
2885 return std::make_pair(true, false);
2886
2887 // Parse the numeric characters.
2888 return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg),
2889 true);
2890}
2891
2892std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering::
2893parseRegForInlineAsmConstraint(const StringRef &C, MVT VT) const {
2894 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2895 const TargetRegisterClass *RC;
2896 std::string Prefix;
2897 unsigned long long Reg;
2898
2899 std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg);
2900
2901 if (!R.first)
Craig Topper062a2ba2014-04-25 05:30:21 +00002902 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00002903
2904 if ((Prefix == "hi" || Prefix == "lo")) { // Parse hi/lo.
2905 // No numeric characters follow "hi" or "lo".
2906 if (R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00002907 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00002908
2909 RC = TRI->getRegClass(Prefix == "hi" ?
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00002910 Mips::HI32RegClassID : Mips::LO32RegClassID);
Akira Hatanaka7473b472013-08-14 00:21:25 +00002911 return std::make_pair(*(RC->begin()), RC);
Daniel Sanders8b59af12013-11-12 12:56:01 +00002912 } else if (Prefix.compare(0, 4, "$msa") == 0) {
2913 // Parse $msa(ir|csr|access|save|modify|request|map|unmap)
2914
2915 // No numeric characters follow the name.
2916 if (R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00002917 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00002918
2919 Reg = StringSwitch<unsigned long long>(Prefix)
2920 .Case("$msair", Mips::MSAIR)
2921 .Case("$msacsr", Mips::MSACSR)
2922 .Case("$msaaccess", Mips::MSAAccess)
2923 .Case("$msasave", Mips::MSASave)
2924 .Case("$msamodify", Mips::MSAModify)
2925 .Case("$msarequest", Mips::MSARequest)
2926 .Case("$msamap", Mips::MSAMap)
2927 .Case("$msaunmap", Mips::MSAUnmap)
2928 .Default(0);
2929
2930 if (!Reg)
Craig Topper062a2ba2014-04-25 05:30:21 +00002931 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00002932
2933 RC = TRI->getRegClass(Mips::MSACtrlRegClassID);
2934 return std::make_pair(Reg, RC);
Akira Hatanaka7473b472013-08-14 00:21:25 +00002935 }
2936
2937 if (!R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00002938 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00002939
2940 if (Prefix == "$f") { // Parse $f0-$f31.
2941 // If the size of FP registers is 64-bit or Reg is an even number, select
2942 // the 64-bit register class. Otherwise, select the 32-bit register class.
2943 if (VT == MVT::Other)
2944 VT = (Subtarget->isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32;
2945
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00002946 RC = getRegClassFor(VT);
Akira Hatanaka7473b472013-08-14 00:21:25 +00002947
2948 if (RC == &Mips::AFGR64RegClass) {
2949 assert(Reg % 2 == 0);
2950 Reg >>= 1;
2951 }
Daniel Sanders8b59af12013-11-12 12:56:01 +00002952 } else if (Prefix == "$fcc") // Parse $fcc0-$fcc7.
Akira Hatanaka7473b472013-08-14 00:21:25 +00002953 RC = TRI->getRegClass(Mips::FCCRegClassID);
Daniel Sanders8b59af12013-11-12 12:56:01 +00002954 else if (Prefix == "$w") { // Parse $w0-$w31.
2955 RC = getRegClassFor((VT == MVT::Other) ? MVT::v16i8 : VT);
Akira Hatanaka7473b472013-08-14 00:21:25 +00002956 } else { // Parse $0-$31.
2957 assert(Prefix == "$");
2958 RC = getRegClassFor((VT == MVT::Other) ? MVT::i32 : VT);
2959 }
2960
2961 assert(Reg < RC->getNumRegs());
2962 return std::make_pair(*(RC->begin() + Reg), RC);
2963}
2964
Eric Christophereaf77dc2011-06-29 19:33:04 +00002965/// Given a register class constraint, like 'r', if this corresponds directly
2966/// to an LLVM register class, return a register of 0 and the register class
2967/// pointer.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002968std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Chad Rosier295bd432013-06-22 18:37:38 +00002969getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002970{
2971 if (Constraint.size() == 1) {
2972 switch (Constraint[0]) {
Eric Christopher9519c082011-06-29 19:04:31 +00002973 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
2974 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002975 case 'r':
Akira Hatanaka92a96e12012-09-12 23:27:55 +00002976 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
2977 if (Subtarget->inMips16Mode())
2978 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00002979 return std::make_pair(0U, &Mips::GPR32RegClass);
Akira Hatanaka92a96e12012-09-12 23:27:55 +00002980 }
Daniel Sanders5e94e682014-03-27 16:42:17 +00002981 if (VT == MVT::i64 && !isGP64bit())
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00002982 return std::make_pair(0U, &Mips::GPR32RegClass);
Daniel Sanders5e94e682014-03-27 16:42:17 +00002983 if (VT == MVT::i64 && isGP64bit())
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00002984 return std::make_pair(0U, &Mips::GPR64RegClass);
Eric Christopher58daf042012-05-07 03:13:22 +00002985 // This will generate an error message
Craig Topper062a2ba2014-04-25 05:30:21 +00002986 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00002987 case 'f': // FPU or MSA register
2988 if (VT == MVT::v16i8)
2989 return std::make_pair(0U, &Mips::MSA128BRegClass);
2990 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
2991 return std::make_pair(0U, &Mips::MSA128HRegClass);
2992 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2993 return std::make_pair(0U, &Mips::MSA128WRegClass);
2994 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2995 return std::make_pair(0U, &Mips::MSA128DRegClass);
2996 else if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00002997 return std::make_pair(0U, &Mips::FGR32RegClass);
Daniel Sanders8b59af12013-11-12 12:56:01 +00002998 else if ((VT == MVT::f64) && (!Subtarget->isSingleFloat())) {
Akira Hatanakac669d7a2012-01-04 02:45:01 +00002999 if (Subtarget->isFP64bit())
Craig Topperc7242e02012-04-20 07:30:17 +00003000 return std::make_pair(0U, &Mips::FGR64RegClass);
3001 return std::make_pair(0U, &Mips::AFGR64RegClass);
Akira Hatanakac669d7a2012-01-04 02:45:01 +00003002 }
Eric Christophere3c494d2012-05-07 06:25:10 +00003003 break;
3004 case 'c': // register suitable for indirect jump
3005 if (VT == MVT::i32)
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003006 return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass);
Eric Christophere3c494d2012-05-07 06:25:10 +00003007 assert(VT == MVT::i64 && "Unexpected type.");
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003008 return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass);
Eric Christopher9c492e62012-05-07 06:25:15 +00003009 case 'l': // register suitable for indirect jump
3010 if (VT == MVT::i32)
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00003011 return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass);
3012 return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass);
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003013 case 'x': // register suitable for indirect jump
3014 // Fixme: Not triggering the use of both hi and low
3015 // This will generate an error message
Craig Topper062a2ba2014-04-25 05:30:21 +00003016 return std::make_pair(0U, nullptr);
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003017 }
3018 }
Akira Hatanaka7473b472013-08-14 00:21:25 +00003019
3020 std::pair<unsigned, const TargetRegisterClass *> R;
3021 R = parseRegForInlineAsmConstraint(Constraint, VT);
3022
3023 if (R.second)
3024 return R;
3025
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003026 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3027}
3028
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003029/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3030/// vector. If it is invalid, don't add anything to Ops.
3031void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3032 std::string &Constraint,
3033 std::vector<SDValue>&Ops,
3034 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +00003035 SDValue Result;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003036
3037 // Only support length 1 constraints for now.
3038 if (Constraint.length() > 1) return;
3039
3040 char ConstraintLetter = Constraint[0];
3041 switch (ConstraintLetter) {
3042 default: break; // This will fall through to the generic implementation
3043 case 'I': // Signed 16 bit constant
3044 // If this fails, the parent routine will give an error
3045 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3046 EVT Type = Op.getValueType();
3047 int64_t Val = C->getSExtValue();
3048 if (isInt<16>(Val)) {
3049 Result = DAG.getTargetConstant(Val, Type);
3050 break;
3051 }
3052 }
3053 return;
Eric Christopher7201e1b2012-05-07 03:13:42 +00003054 case 'J': // integer zero
3055 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3056 EVT Type = Op.getValueType();
3057 int64_t Val = C->getZExtValue();
3058 if (Val == 0) {
3059 Result = DAG.getTargetConstant(0, Type);
3060 break;
3061 }
3062 }
3063 return;
Eric Christopher3ff88a02012-05-07 05:46:29 +00003064 case 'K': // unsigned 16 bit immediate
3065 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3066 EVT Type = Op.getValueType();
3067 uint64_t Val = (uint64_t)C->getZExtValue();
3068 if (isUInt<16>(Val)) {
3069 Result = DAG.getTargetConstant(Val, Type);
3070 break;
3071 }
3072 }
3073 return;
Eric Christopher1109b342012-05-07 05:46:37 +00003074 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3075 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3076 EVT Type = Op.getValueType();
3077 int64_t Val = C->getSExtValue();
3078 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
3079 Result = DAG.getTargetConstant(Val, Type);
3080 break;
3081 }
3082 }
3083 return;
Eric Christophere07aa432012-05-07 05:46:43 +00003084 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3085 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3086 EVT Type = Op.getValueType();
3087 int64_t Val = C->getSExtValue();
3088 if ((Val >= -65535) && (Val <= -1)) {
3089 Result = DAG.getTargetConstant(Val, Type);
3090 break;
3091 }
3092 }
3093 return;
Eric Christopher470578a2012-05-07 05:46:48 +00003094 case 'O': // signed 15 bit immediate
3095 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3096 EVT Type = Op.getValueType();
3097 int64_t Val = C->getSExtValue();
3098 if ((isInt<15>(Val))) {
3099 Result = DAG.getTargetConstant(Val, Type);
3100 break;
3101 }
3102 }
3103 return;
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003104 case 'P': // immediate in the range of 1 to 65535 (inclusive)
3105 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3106 EVT Type = Op.getValueType();
3107 int64_t Val = C->getSExtValue();
3108 if ((Val <= 65535) && (Val >= 1)) {
3109 Result = DAG.getTargetConstant(Val, Type);
3110 break;
3111 }
3112 }
3113 return;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003114 }
3115
3116 if (Result.getNode()) {
3117 Ops.push_back(Result);
3118 return;
3119 }
3120
3121 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3122}
3123
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003124bool MipsTargetLowering::isLegalAddressingMode(const AddrMode &AM,
3125 Type *Ty) const {
Akira Hatanakaef839192012-11-17 00:25:41 +00003126 // No global is ever allowed as a base.
3127 if (AM.BaseGV)
3128 return false;
3129
3130 switch (AM.Scale) {
3131 case 0: // "r+i" or just "i", depending on HasBaseReg.
3132 break;
3133 case 1:
3134 if (!AM.HasBaseReg) // allow "r+i".
3135 break;
3136 return false; // disallow "r+r" or "r+r+i".
3137 default:
3138 return false;
3139 }
3140
3141 return true;
3142}
3143
3144bool
Dan Gohman2fe6bee2008-10-18 02:06:02 +00003145MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3146 // The Mips target isn't yet aware of offsets.
3147 return false;
3148}
Evan Cheng16993aa2009-10-27 19:56:55 +00003149
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003150EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003151 unsigned SrcAlign,
3152 bool IsMemset, bool ZeroMemset,
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003153 bool MemcpyStrSrc,
3154 MachineFunction &MF) const {
3155 if (Subtarget->hasMips64())
3156 return MVT::i64;
3157
3158 return MVT::i32;
3159}
3160
Evan Cheng83896a52009-10-28 01:43:28 +00003161bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
3162 if (VT != MVT::f32 && VT != MVT::f64)
3163 return false;
Bruno Cardoso Lopesb02a9df2011-01-18 19:41:41 +00003164 if (Imm.isNegZero())
3165 return false;
Evan Cheng16993aa2009-10-27 19:56:55 +00003166 return Imm.isZero();
3167}
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003168
3169unsigned MipsTargetLowering::getJumpTableEncoding() const {
Daniel Sandersd897b562014-03-27 10:46:12 +00003170 if (isN64())
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003171 return MachineJumpTableInfo::EK_GPRel64BlockAddress;
Jia Liuf54f60f2012-02-28 07:46:26 +00003172
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003173 return TargetLowering::getJumpTableEncoding();
3174}
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003175
Akira Hatanakae092f722013-03-05 22:54:59 +00003176/// This function returns true if CallSym is a long double emulation routine.
3177static bool isF128SoftLibCall(const char *CallSym) {
3178 const char *const LibCalls[] =
3179 {"__addtf3", "__divtf3", "__eqtf2", "__extenddftf2", "__extendsftf2",
3180 "__fixtfdi", "__fixtfsi", "__fixtfti", "__fixunstfdi", "__fixunstfsi",
3181 "__fixunstfti", "__floatditf", "__floatsitf", "__floattitf",
3182 "__floatunditf", "__floatunsitf", "__floatuntitf", "__getf2", "__gttf2",
3183 "__letf2", "__lttf2", "__multf3", "__netf2", "__powitf2", "__subtf3",
3184 "__trunctfdf2", "__trunctfsf2", "__unordtf2",
3185 "ceill", "copysignl", "cosl", "exp2l", "expl", "floorl", "fmal", "fmodl",
3186 "log10l", "log2l", "logl", "nearbyintl", "powl", "rintl", "sinl", "sqrtl",
3187 "truncl"};
3188
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003189 const char *const *End = LibCalls + array_lengthof(LibCalls);
Akira Hatanakae092f722013-03-05 22:54:59 +00003190
3191 // Check that LibCalls is sorted alphabetically.
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003192 MipsTargetLowering::LTStr Comp;
Akira Hatanakae092f722013-03-05 22:54:59 +00003193
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003194#ifndef NDEBUG
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003195 for (const char *const *I = LibCalls; I < End - 1; ++I)
Akira Hatanakae092f722013-03-05 22:54:59 +00003196 assert(Comp(*I, *(I + 1)));
3197#endif
3198
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003199 return std::binary_search(LibCalls, End, CallSym, Comp);
Akira Hatanakae092f722013-03-05 22:54:59 +00003200}
3201
3202/// This function returns true if Ty is fp128 or i128 which was originally a
3203/// fp128.
3204static bool originalTypeIsF128(const Type *Ty, const SDNode *CallNode) {
3205 if (Ty->isFP128Ty())
3206 return true;
3207
3208 const ExternalSymbolSDNode *ES =
3209 dyn_cast_or_null<const ExternalSymbolSDNode>(CallNode);
3210
3211 // If the Ty is i128 and the function being called is a long double emulation
3212 // routine, then the original type is f128.
3213 return (ES && Ty->isIntegerTy(128) && isF128SoftLibCall(ES->getSymbol()));
3214}
3215
Reed Kotler783c7942013-05-10 22:25:39 +00003216MipsTargetLowering::MipsCC::SpecialCallingConvType
3217 MipsTargetLowering::getSpecialCallingConv(SDValue Callee) const {
3218 MipsCC::SpecialCallingConvType SpecialCallingConv =
Alp Toker98444342014-04-19 23:56:35 +00003219 MipsCC::NoSpecialCallingConv;
Reed Kotler783c7942013-05-10 22:25:39 +00003220 if (Subtarget->inMips16HardFloat()) {
3221 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3222 llvm::StringRef Sym = G->getGlobal()->getName();
3223 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
Reed Kotler3230e722013-12-12 02:41:11 +00003224 if (F && F->hasFnAttribute("__Mips16RetHelper")) {
Reed Kotler783c7942013-05-10 22:25:39 +00003225 SpecialCallingConv = MipsCC::Mips16RetHelperConv;
3226 }
3227 }
3228 }
3229 return SpecialCallingConv;
3230}
3231
3232MipsTargetLowering::MipsCC::MipsCC(
Akira Hatanakabfb66242013-08-20 23:38:40 +00003233 CallingConv::ID CC, bool IsO32_, bool IsFP64_, CCState &Info,
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003234 MipsCC::SpecialCallingConvType SpecialCallingConv_)
Akira Hatanakabfb66242013-08-20 23:38:40 +00003235 : CCInfo(Info), CallConv(CC), IsO32(IsO32_), IsFP64(IsFP64_),
Reed Kotler783c7942013-05-10 22:25:39 +00003236 SpecialCallingConv(SpecialCallingConv_){
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003237 // Pre-allocate reserved argument area.
Akira Hatanaka5001be52013-02-15 21:45:11 +00003238 CCInfo.AllocateStack(reservedArgArea(), 1);
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003239}
3240
Reed Kotler783c7942013-05-10 22:25:39 +00003241
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003242void MipsTargetLowering::MipsCC::
Akira Hatanaka5001be52013-02-15 21:45:11 +00003243analyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Args,
Akira Hatanaka3b7391d2013-03-05 22:20:28 +00003244 bool IsVarArg, bool IsSoftFloat, const SDNode *CallNode,
3245 std::vector<ArgListEntry> &FuncArgs) {
Akira Hatanaka5001be52013-02-15 21:45:11 +00003246 assert((CallConv != CallingConv::Fast || !IsVarArg) &&
3247 "CallingConv::Fast shouldn't be used for vararg functions.");
3248
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003249 unsigned NumOpnds = Args.size();
Akira Hatanaka5001be52013-02-15 21:45:11 +00003250 llvm::CCAssignFn *FixedFn = fixedArgFn(), *VarFn = varArgFn();
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003251
3252 for (unsigned I = 0; I != NumOpnds; ++I) {
3253 MVT ArgVT = Args[I].VT;
3254 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
3255 bool R;
3256
3257 if (ArgFlags.isByVal()) {
3258 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
3259 continue;
3260 }
3261
Akira Hatanaka5001be52013-02-15 21:45:11 +00003262 if (IsVarArg && !Args[I].IsFixed)
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003263 R = VarFn(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
Akira Hatanaka3b7391d2013-03-05 22:20:28 +00003264 else {
3265 MVT RegVT = getRegVT(ArgVT, FuncArgs[Args[I].OrigArgIndex].Ty, CallNode,
3266 IsSoftFloat);
3267 R = FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo);
3268 }
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003269
3270 if (R) {
3271#ifndef NDEBUG
3272 dbgs() << "Call operand #" << I << " has unhandled type "
3273 << EVT(ArgVT).getEVTString();
3274#endif
Craig Toppere73658d2014-04-28 04:05:08 +00003275 llvm_unreachable(nullptr);
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003276 }
3277 }
3278}
3279
3280void MipsTargetLowering::MipsCC::
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003281analyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Args,
3282 bool IsSoftFloat, Function::const_arg_iterator FuncArg) {
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003283 unsigned NumArgs = Args.size();
Akira Hatanaka5001be52013-02-15 21:45:11 +00003284 llvm::CCAssignFn *FixedFn = fixedArgFn();
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003285 unsigned CurArgIdx = 0;
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003286
3287 for (unsigned I = 0; I != NumArgs; ++I) {
3288 MVT ArgVT = Args[I].VT;
3289 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003290 std::advance(FuncArg, Args[I].OrigArgIndex - CurArgIdx);
3291 CurArgIdx = Args[I].OrigArgIndex;
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003292
3293 if (ArgFlags.isByVal()) {
3294 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
3295 continue;
3296 }
3297
Craig Topper062a2ba2014-04-25 05:30:21 +00003298 MVT RegVT = getRegVT(ArgVT, FuncArg->getType(), nullptr, IsSoftFloat);
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003299
3300 if (!FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo))
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003301 continue;
3302
3303#ifndef NDEBUG
3304 dbgs() << "Formal Arg #" << I << " has unhandled type "
3305 << EVT(ArgVT).getEVTString();
3306#endif
Craig Toppere73658d2014-04-28 04:05:08 +00003307 llvm_unreachable(nullptr);
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003308 }
3309}
3310
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003311template<typename Ty>
3312void MipsTargetLowering::MipsCC::
3313analyzeReturn(const SmallVectorImpl<Ty> &RetVals, bool IsSoftFloat,
3314 const SDNode *CallNode, const Type *RetTy) const {
Akira Hatanakae092f722013-03-05 22:54:59 +00003315 CCAssignFn *Fn;
3316
3317 if (IsSoftFloat && originalTypeIsF128(RetTy, CallNode))
3318 Fn = RetCC_F128Soft;
3319 else
3320 Fn = RetCC_Mips;
3321
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003322 for (unsigned I = 0, E = RetVals.size(); I < E; ++I) {
3323 MVT VT = RetVals[I].VT;
3324 ISD::ArgFlagsTy Flags = RetVals[I].Flags;
3325 MVT RegVT = this->getRegVT(VT, RetTy, CallNode, IsSoftFloat);
3326
Akira Hatanakae092f722013-03-05 22:54:59 +00003327 if (Fn(I, VT, RegVT, CCValAssign::Full, Flags, this->CCInfo)) {
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003328#ifndef NDEBUG
3329 dbgs() << "Call result #" << I << " has unhandled type "
3330 << EVT(VT).getEVTString() << '\n';
3331#endif
Craig Toppere73658d2014-04-28 04:05:08 +00003332 llvm_unreachable(nullptr);
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003333 }
3334 }
3335}
3336
3337void MipsTargetLowering::MipsCC::
3338analyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins, bool IsSoftFloat,
3339 const SDNode *CallNode, const Type *RetTy) const {
3340 analyzeReturn(Ins, IsSoftFloat, CallNode, RetTy);
3341}
3342
3343void MipsTargetLowering::MipsCC::
3344analyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs, bool IsSoftFloat,
3345 const Type *RetTy) const {
Craig Topper062a2ba2014-04-25 05:30:21 +00003346 analyzeReturn(Outs, IsSoftFloat, nullptr, RetTy);
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003347}
3348
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003349void MipsTargetLowering::MipsCC::handleByValArg(unsigned ValNo, MVT ValVT,
3350 MVT LocVT,
3351 CCValAssign::LocInfo LocInfo,
3352 ISD::ArgFlagsTy ArgFlags) {
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003353 assert(ArgFlags.getByValSize() && "Byval argument's size shouldn't be 0.");
3354
3355 struct ByValArgInfo ByVal;
Akira Hatanaka5001be52013-02-15 21:45:11 +00003356 unsigned RegSize = regSize();
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003357 unsigned ByValSize = RoundUpToAlignment(ArgFlags.getByValSize(), RegSize);
3358 unsigned Align = std::min(std::max(ArgFlags.getByValAlign(), RegSize),
3359 RegSize * 2);
3360
Akira Hatanaka5001be52013-02-15 21:45:11 +00003361 if (useRegsForByval())
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003362 allocateRegs(ByVal, ByValSize, Align);
3363
3364 // Allocate space on caller's stack.
3365 ByVal.Address = CCInfo.AllocateStack(ByValSize - RegSize * ByVal.NumRegs,
3366 Align);
3367 CCInfo.addLoc(CCValAssign::getMem(ValNo, ValVT, ByVal.Address, LocVT,
3368 LocInfo));
3369 ByValArgs.push_back(ByVal);
3370}
3371
Akira Hatanaka5001be52013-02-15 21:45:11 +00003372unsigned MipsTargetLowering::MipsCC::numIntArgRegs() const {
3373 return IsO32 ? array_lengthof(O32IntRegs) : array_lengthof(Mips64IntRegs);
3374}
3375
3376unsigned MipsTargetLowering::MipsCC::reservedArgArea() const {
3377 return (IsO32 && (CallConv != CallingConv::Fast)) ? 16 : 0;
3378}
3379
Craig Topper840beec2014-04-04 05:16:06 +00003380const MCPhysReg *MipsTargetLowering::MipsCC::intArgRegs() const {
Akira Hatanaka5001be52013-02-15 21:45:11 +00003381 return IsO32 ? O32IntRegs : Mips64IntRegs;
3382}
3383
3384llvm::CCAssignFn *MipsTargetLowering::MipsCC::fixedArgFn() const {
3385 if (CallConv == CallingConv::Fast)
3386 return CC_Mips_FastCC;
3387
Reed Kotler783c7942013-05-10 22:25:39 +00003388 if (SpecialCallingConv == Mips16RetHelperConv)
3389 return CC_Mips16RetHelper;
Akira Hatanakabfb66242013-08-20 23:38:40 +00003390 return IsO32 ? (IsFP64 ? CC_MipsO32_FP64 : CC_MipsO32_FP32) : CC_MipsN;
Akira Hatanaka5001be52013-02-15 21:45:11 +00003391}
3392
3393llvm::CCAssignFn *MipsTargetLowering::MipsCC::varArgFn() const {
Akira Hatanakabfb66242013-08-20 23:38:40 +00003394 return IsO32 ? (IsFP64 ? CC_MipsO32_FP64 : CC_MipsO32_FP32) : CC_MipsN_VarArg;
Akira Hatanaka5001be52013-02-15 21:45:11 +00003395}
3396
Craig Topper840beec2014-04-04 05:16:06 +00003397const MCPhysReg *MipsTargetLowering::MipsCC::shadowRegs() const {
Akira Hatanaka5001be52013-02-15 21:45:11 +00003398 return IsO32 ? O32IntRegs : Mips64DPRegs;
3399}
3400
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003401void MipsTargetLowering::MipsCC::allocateRegs(ByValArgInfo &ByVal,
3402 unsigned ByValSize,
3403 unsigned Align) {
Akira Hatanaka5001be52013-02-15 21:45:11 +00003404 unsigned RegSize = regSize(), NumIntArgRegs = numIntArgRegs();
Craig Topper840beec2014-04-04 05:16:06 +00003405 const MCPhysReg *IntArgRegs = intArgRegs(), *ShadowRegs = shadowRegs();
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003406 assert(!(ByValSize % RegSize) && !(Align % RegSize) &&
3407 "Byval argument's size and alignment should be a multiple of"
3408 "RegSize.");
3409
3410 ByVal.FirstIdx = CCInfo.getFirstUnallocated(IntArgRegs, NumIntArgRegs);
3411
3412 // If Align > RegSize, the first arg register must be even.
3413 if ((Align > RegSize) && (ByVal.FirstIdx % 2)) {
3414 CCInfo.AllocateReg(IntArgRegs[ByVal.FirstIdx], ShadowRegs[ByVal.FirstIdx]);
3415 ++ByVal.FirstIdx;
3416 }
3417
3418 // Mark the registers allocated.
3419 for (unsigned I = ByVal.FirstIdx; ByValSize && (I < NumIntArgRegs);
3420 ByValSize -= RegSize, ++I, ++ByVal.NumRegs)
3421 CCInfo.AllocateReg(IntArgRegs[I], ShadowRegs[I]);
3422}
Akira Hatanaka25dad192012-10-27 00:10:18 +00003423
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003424MVT MipsTargetLowering::MipsCC::getRegVT(MVT VT, const Type *OrigTy,
3425 const SDNode *CallNode,
3426 bool IsSoftFloat) const {
3427 if (IsSoftFloat || IsO32)
3428 return VT;
3429
3430 // Check if the original type was fp128.
Akira Hatanakae092f722013-03-05 22:54:59 +00003431 if (originalTypeIsF128(OrigTy, CallNode)) {
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003432 assert(VT == MVT::i64);
3433 return MVT::f64;
3434 }
3435
3436 return VT;
3437}
3438
Akira Hatanaka25dad192012-10-27 00:10:18 +00003439void MipsTargetLowering::
Andrew Trickef9de2a2013-05-25 02:42:55 +00003440copyByValRegs(SDValue Chain, SDLoc DL, std::vector<SDValue> &OutChains,
Akira Hatanaka25dad192012-10-27 00:10:18 +00003441 SelectionDAG &DAG, const ISD::ArgFlagsTy &Flags,
3442 SmallVectorImpl<SDValue> &InVals, const Argument *FuncArg,
3443 const MipsCC &CC, const ByValArgInfo &ByVal) const {
3444 MachineFunction &MF = DAG.getMachineFunction();
3445 MachineFrameInfo *MFI = MF.getFrameInfo();
3446 unsigned RegAreaSize = ByVal.NumRegs * CC.regSize();
3447 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
3448 int FrameObjOffset;
3449
3450 if (RegAreaSize)
3451 FrameObjOffset = (int)CC.reservedArgArea() -
3452 (int)((CC.numIntArgRegs() - ByVal.FirstIdx) * CC.regSize());
3453 else
3454 FrameObjOffset = ByVal.Address;
3455
3456 // Create frame object.
3457 EVT PtrTy = getPointerTy();
3458 int FI = MFI->CreateFixedObject(FrameObjSize, FrameObjOffset, true);
3459 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
3460 InVals.push_back(FIN);
3461
3462 if (!ByVal.NumRegs)
3463 return;
3464
3465 // Copy arg registers.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00003466 MVT RegTy = MVT::getIntegerVT(CC.regSize() * 8);
Akira Hatanaka25dad192012-10-27 00:10:18 +00003467 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3468
3469 for (unsigned I = 0; I < ByVal.NumRegs; ++I) {
3470 unsigned ArgReg = CC.intArgRegs()[ByVal.FirstIdx + I];
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003471 unsigned VReg = addLiveIn(MF, ArgReg, RC);
Akira Hatanaka25dad192012-10-27 00:10:18 +00003472 unsigned Offset = I * CC.regSize();
3473 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
3474 DAG.getConstant(Offset, PtrTy));
3475 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
3476 StorePtr, MachinePointerInfo(FuncArg, Offset),
3477 false, false, 0);
3478 OutChains.push_back(Store);
3479 }
3480}
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003481
3482// Copy byVal arg to registers and stack.
3483void MipsTargetLowering::
Andrew Trickef9de2a2013-05-25 02:42:55 +00003484passByValArg(SDValue Chain, SDLoc DL,
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00003485 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
Craig Topperb94011f2013-07-14 04:42:23 +00003486 SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr,
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003487 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
3488 const MipsCC &CC, const ByValArgInfo &ByVal,
3489 const ISD::ArgFlagsTy &Flags, bool isLittle) const {
3490 unsigned ByValSize = Flags.getByValSize();
3491 unsigned Offset = 0; // Offset in # of bytes from the beginning of struct.
3492 unsigned RegSize = CC.regSize();
3493 unsigned Alignment = std::min(Flags.getByValAlign(), RegSize);
3494 EVT PtrTy = getPointerTy(), RegTy = MVT::getIntegerVT(RegSize * 8);
3495
3496 if (ByVal.NumRegs) {
Craig Topper840beec2014-04-04 05:16:06 +00003497 const MCPhysReg *ArgRegs = CC.intArgRegs();
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003498 bool LeftoverBytes = (ByVal.NumRegs * RegSize > ByValSize);
3499 unsigned I = 0;
3500
3501 // Copy words to registers.
3502 for (; I < ByVal.NumRegs - LeftoverBytes; ++I, Offset += RegSize) {
3503 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3504 DAG.getConstant(Offset, PtrTy));
3505 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
3506 MachinePointerInfo(), false, false, false,
3507 Alignment);
3508 MemOpChains.push_back(LoadVal.getValue(1));
3509 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3510 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
3511 }
3512
3513 // Return if the struct has been fully copied.
3514 if (ByValSize == Offset)
3515 return;
3516
3517 // Copy the remainder of the byval argument with sub-word loads and shifts.
3518 if (LeftoverBytes) {
3519 assert((ByValSize > Offset) && (ByValSize < Offset + RegSize) &&
3520 "Size of the remainder should be smaller than RegSize.");
3521 SDValue Val;
3522
3523 for (unsigned LoadSize = RegSize / 2, TotalSizeLoaded = 0;
3524 Offset < ByValSize; LoadSize /= 2) {
3525 unsigned RemSize = ByValSize - Offset;
3526
3527 if (RemSize < LoadSize)
3528 continue;
3529
3530 // Load subword.
3531 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3532 DAG.getConstant(Offset, PtrTy));
3533 SDValue LoadVal =
3534 DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr,
3535 MachinePointerInfo(), MVT::getIntegerVT(LoadSize * 8),
3536 false, false, Alignment);
3537 MemOpChains.push_back(LoadVal.getValue(1));
3538
3539 // Shift the loaded value.
3540 unsigned Shamt;
3541
3542 if (isLittle)
3543 Shamt = TotalSizeLoaded;
3544 else
3545 Shamt = (RegSize - (TotalSizeLoaded + LoadSize)) * 8;
3546
3547 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
3548 DAG.getConstant(Shamt, MVT::i32));
3549
3550 if (Val.getNode())
3551 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
3552 else
3553 Val = Shift;
3554
3555 Offset += LoadSize;
3556 TotalSizeLoaded += LoadSize;
3557 Alignment = std::min(Alignment, LoadSize);
3558 }
3559
3560 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3561 RegsToPass.push_back(std::make_pair(ArgReg, Val));
3562 return;
3563 }
3564 }
3565
3566 // Copy remainder of byval arg to it with memcpy.
3567 unsigned MemCpySize = ByValSize - Offset;
3568 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3569 DAG.getConstant(Offset, PtrTy));
3570 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
3571 DAG.getIntPtrConstant(ByVal.Address));
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003572 Chain = DAG.getMemcpy(Chain, DL, Dst, Src, DAG.getConstant(MemCpySize, PtrTy),
3573 Alignment, /*isVolatile=*/false, /*AlwaysInline=*/false,
Nick Lewyckyaad475b2014-04-15 07:22:52 +00003574 MachinePointerInfo(), MachinePointerInfo());
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003575 MemOpChains.push_back(Chain);
3576}
Akira Hatanaka2a134022012-10-27 00:21:13 +00003577
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003578void MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
3579 const MipsCC &CC, SDValue Chain,
3580 SDLoc DL, SelectionDAG &DAG) const {
Akira Hatanaka2a134022012-10-27 00:21:13 +00003581 unsigned NumRegs = CC.numIntArgRegs();
Craig Topper840beec2014-04-04 05:16:06 +00003582 const MCPhysReg *ArgRegs = CC.intArgRegs();
Akira Hatanaka2a134022012-10-27 00:21:13 +00003583 const CCState &CCInfo = CC.getCCInfo();
3584 unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs, NumRegs);
3585 unsigned RegSize = CC.regSize();
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00003586 MVT RegTy = MVT::getIntegerVT(RegSize * 8);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003587 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3588 MachineFunction &MF = DAG.getMachineFunction();
3589 MachineFrameInfo *MFI = MF.getFrameInfo();
3590 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3591
3592 // Offset of the first variable argument from stack pointer.
3593 int VaArgOffset;
3594
3595 if (NumRegs == Idx)
3596 VaArgOffset = RoundUpToAlignment(CCInfo.getNextStackOffset(), RegSize);
3597 else
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003598 VaArgOffset = (int)CC.reservedArgArea() - (int)(RegSize * (NumRegs - Idx));
Akira Hatanaka2a134022012-10-27 00:21:13 +00003599
3600 // Record the frame index of the first variable argument
3601 // which is a value necessary to VASTART.
3602 int FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
3603 MipsFI->setVarArgsFrameIndex(FI);
3604
3605 // Copy the integer registers that have not been used for argument passing
3606 // to the argument register save area. For O32, the save area is allocated
3607 // in the caller's stack frame, while for N32/64, it is allocated in the
3608 // callee's stack frame.
3609 for (unsigned I = Idx; I < NumRegs; ++I, VaArgOffset += RegSize) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003610 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003611 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
3612 FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
3613 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
3614 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
3615 MachinePointerInfo(), false, false, 0);
Craig Topper062a2ba2014-04-25 05:30:21 +00003616 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue((Value*)nullptr);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003617 OutChains.push_back(Store);
3618 }
3619}