blob: bee470062564b1a4c1308ca38713a3c1353a6239 [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#define DEBUG_TYPE "mips-lower"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000015#include "MipsISelLowering.h"
Craig Topperb25fda92012-03-17 18:46:09 +000016#include "InstPrinter/MipsInstPrinter.h"
17#include "MCTargetDesc/MipsBaseInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "MipsMachineFunction.h"
19#include "MipsSubtarget.h"
20#include "MipsTargetMachine.h"
21#include "MipsTargetObjectFile.h"
Akira Hatanaka90131ac2012-10-19 21:47:33 +000022#include "llvm/ADT/Statistic.h"
Daniel Sanders8b59af12013-11-12 12:56:01 +000023#include "llvm/ADT/StringSwitch.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000024#include "llvm/CodeGen/CallingConvLower.h"
25#include "llvm/CodeGen/MachineFrameInfo.h"
26#include "llvm/CodeGen/MachineFunction.h"
27#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000028#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000029#include "llvm/CodeGen/SelectionDAGISel.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000030#include "llvm/CodeGen/ValueTypes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000031#include "llvm/IR/CallingConv.h"
32#include "llvm/IR/DerivedTypes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000033#include "llvm/IR/GlobalVariable.h"
Akira Hatanaka90131ac2012-10-19 21:47:33 +000034#include "llvm/Support/CommandLine.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000035#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000036#include "llvm/Support/ErrorHandling.h"
NAKAMURA Takumie30303f2012-04-21 15:31:45 +000037#include "llvm/Support/raw_ostream.h"
Akira Hatanaka7473b472013-08-14 00:21:25 +000038#include <cctype>
NAKAMURA Takumie30303f2012-04-21 15:31:45 +000039
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000040using namespace llvm;
41
Akira Hatanaka90131ac2012-10-19 21:47:33 +000042STATISTIC(NumTailCalls, "Number of tail calls");
43
44static cl::opt<bool>
Akira Hatanaka59f299f2012-11-21 20:21:11 +000045LargeGOT("mxgot", cl::Hidden,
46 cl::desc("MIPS: Enable GOT larger than 64k."), cl::init(false));
47
Akira Hatanaka1cb02422013-05-20 18:07:43 +000048static cl::opt<bool>
Akira Hatanakabe76cd02013-05-21 17:17:59 +000049NoZeroDivCheck("mno-check-zero-division", cl::Hidden,
Akira Hatanaka1cb02422013-05-20 18:07:43 +000050 cl::desc("MIPS: Don't trap on integer division by zero."),
51 cl::init(false));
52
Akira Hatanakaac8c6692012-10-27 00:29:43 +000053static const uint16_t O32IntRegs[4] = {
54 Mips::A0, Mips::A1, Mips::A2, Mips::A3
55};
56
57static const uint16_t Mips64IntRegs[8] = {
58 Mips::A0_64, Mips::A1_64, Mips::A2_64, Mips::A3_64,
59 Mips::T0_64, Mips::T1_64, Mips::T2_64, Mips::T3_64
60};
61
62static const uint16_t Mips64DPRegs[8] = {
63 Mips::D12_64, Mips::D13_64, Mips::D14_64, Mips::D15_64,
64 Mips::D16_64, Mips::D17_64, Mips::D18_64, Mips::D19_64
65};
66
Jia Liuf54f60f2012-02-28 07:46:26 +000067// If I is a shifted mask, set the size (Size) and the first bit of the
Akira Hatanaka73d78b72011-08-18 20:07:42 +000068// mask (Pos), and return true.
Jia Liuf54f60f2012-02-28 07:46:26 +000069// For example, if I is 0x003ff800, (Pos, Size) = (11, 11).
Akira Hatanaka0bb60d892013-03-12 00:16:36 +000070static bool isShiftedMask(uint64_t I, uint64_t &Pos, uint64_t &Size) {
Akira Hatanaka20cee2e2011-12-05 21:26:34 +000071 if (!isShiftedMask_64(I))
Akira Hatanaka4c0a7122013-10-07 19:33:02 +000072 return false;
Akira Hatanaka5360f882011-08-17 02:05:42 +000073
Akira Hatanaka20cee2e2011-12-05 21:26:34 +000074 Size = CountPopulation_64(I);
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +000075 Pos = countTrailingZeros(I);
Akira Hatanaka73d78b72011-08-18 20:07:42 +000076 return true;
Akira Hatanaka5360f882011-08-17 02:05:42 +000077}
78
Akira Hatanaka96ca1822013-03-13 00:54:29 +000079SDValue MipsTargetLowering::getGlobalReg(SelectionDAG &DAG, EVT Ty) const {
Akira Hatanakab049aef2012-02-24 22:34:47 +000080 MipsFunctionInfo *FI = DAG.getMachineFunction().getInfo<MipsFunctionInfo>();
81 return DAG.getRegister(FI->getGlobalBaseReg(), Ty);
82}
83
Akira Hatanakad8f10ce2013-09-27 19:51:35 +000084SDValue MipsTargetLowering::getTargetNode(GlobalAddressSDNode *N, EVT Ty,
85 SelectionDAG &DAG,
Akira Hatanaka96ca1822013-03-13 00:54:29 +000086 unsigned Flag) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +000087 return DAG.getTargetGlobalAddress(N->getGlobal(), SDLoc(N), Ty, 0, Flag);
Akira Hatanakafd04ad42012-11-21 20:26:38 +000088}
89
Akira Hatanakad8f10ce2013-09-27 19:51:35 +000090SDValue MipsTargetLowering::getTargetNode(ExternalSymbolSDNode *N, EVT Ty,
91 SelectionDAG &DAG,
92 unsigned Flag) const {
93 return DAG.getTargetExternalSymbol(N->getSymbol(), Ty, Flag);
94}
95
96SDValue MipsTargetLowering::getTargetNode(BlockAddressSDNode *N, EVT Ty,
97 SelectionDAG &DAG,
98 unsigned Flag) const {
99 return DAG.getTargetBlockAddress(N->getBlockAddress(), Ty, 0, Flag);
100}
101
102SDValue MipsTargetLowering::getTargetNode(JumpTableSDNode *N, EVT Ty,
103 SelectionDAG &DAG,
104 unsigned Flag) const {
105 return DAG.getTargetJumpTable(N->getIndex(), Ty, Flag);
106}
107
108SDValue MipsTargetLowering::getTargetNode(ConstantPoolSDNode *N, EVT Ty,
109 SelectionDAG &DAG,
110 unsigned Flag) const {
111 return DAG.getTargetConstantPool(N->getConstVal(), Ty, N->getAlignment(),
112 N->getOffset(), Flag);
Akira Hatanakafd04ad42012-11-21 20:26:38 +0000113}
114
Chris Lattner5e693ed2009-07-28 03:13:23 +0000115const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
116 switch (Opcode) {
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000117 case MipsISD::JmpLink: return "MipsISD::JmpLink";
Akira Hatanaka91318df2012-10-19 20:59:39 +0000118 case MipsISD::TailCall: return "MipsISD::TailCall";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000119 case MipsISD::Hi: return "MipsISD::Hi";
120 case MipsISD::Lo: return "MipsISD::Lo";
121 case MipsISD::GPRel: return "MipsISD::GPRel";
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +0000122 case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000123 case MipsISD::Ret: return "MipsISD::Ret";
Akira Hatanakac0b02062013-01-30 00:26:49 +0000124 case MipsISD::EH_RETURN: return "MipsISD::EH_RETURN";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000125 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
126 case MipsISD::FPCmp: return "MipsISD::FPCmp";
127 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
128 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000129 case MipsISD::TruncIntFP: return "MipsISD::TruncIntFP";
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000130 case MipsISD::MFHI: return "MipsISD::MFHI";
131 case MipsISD::MFLO: return "MipsISD::MFLO";
132 case MipsISD::MTLOHI: return "MipsISD::MTLOHI";
Akira Hatanaka28721bd2013-03-30 01:14:04 +0000133 case MipsISD::Mult: return "MipsISD::Mult";
134 case MipsISD::Multu: return "MipsISD::Multu";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000135 case MipsISD::MAdd: return "MipsISD::MAdd";
136 case MipsISD::MAddu: return "MipsISD::MAddu";
137 case MipsISD::MSub: return "MipsISD::MSub";
138 case MipsISD::MSubu: return "MipsISD::MSubu";
139 case MipsISD::DivRem: return "MipsISD::DivRem";
140 case MipsISD::DivRemU: return "MipsISD::DivRemU";
Akira Hatanaka28721bd2013-03-30 01:14:04 +0000141 case MipsISD::DivRem16: return "MipsISD::DivRem16";
142 case MipsISD::DivRemU16: return "MipsISD::DivRemU16";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000143 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
144 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
Akira Hatanakafaa88c02011-12-12 22:38:19 +0000145 case MipsISD::Wrapper: return "MipsISD::Wrapper";
Akira Hatanakaa4c09bc2011-07-19 23:30:50 +0000146 case MipsISD::Sync: return "MipsISD::Sync";
Akira Hatanaka5360f882011-08-17 02:05:42 +0000147 case MipsISD::Ext: return "MipsISD::Ext";
148 case MipsISD::Ins: return "MipsISD::Ins";
Akira Hatanakab9ebf8d2012-06-02 00:03:12 +0000149 case MipsISD::LWL: return "MipsISD::LWL";
150 case MipsISD::LWR: return "MipsISD::LWR";
151 case MipsISD::SWL: return "MipsISD::SWL";
152 case MipsISD::SWR: return "MipsISD::SWR";
153 case MipsISD::LDL: return "MipsISD::LDL";
154 case MipsISD::LDR: return "MipsISD::LDR";
155 case MipsISD::SDL: return "MipsISD::SDL";
156 case MipsISD::SDR: return "MipsISD::SDR";
Akira Hatanaka233ac532012-09-21 23:52:47 +0000157 case MipsISD::EXTP: return "MipsISD::EXTP";
158 case MipsISD::EXTPDP: return "MipsISD::EXTPDP";
159 case MipsISD::EXTR_S_H: return "MipsISD::EXTR_S_H";
160 case MipsISD::EXTR_W: return "MipsISD::EXTR_W";
161 case MipsISD::EXTR_R_W: return "MipsISD::EXTR_R_W";
162 case MipsISD::EXTR_RS_W: return "MipsISD::EXTR_RS_W";
163 case MipsISD::SHILO: return "MipsISD::SHILO";
164 case MipsISD::MTHLIP: return "MipsISD::MTHLIP";
165 case MipsISD::MULT: return "MipsISD::MULT";
166 case MipsISD::MULTU: return "MipsISD::MULTU";
Jia Liu434874d2013-03-04 01:06:54 +0000167 case MipsISD::MADD_DSP: return "MipsISD::MADD_DSP";
Akira Hatanaka233ac532012-09-21 23:52:47 +0000168 case MipsISD::MADDU_DSP: return "MipsISD::MADDU_DSP";
169 case MipsISD::MSUB_DSP: return "MipsISD::MSUB_DSP";
170 case MipsISD::MSUBU_DSP: return "MipsISD::MSUBU_DSP";
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000171 case MipsISD::SHLL_DSP: return "MipsISD::SHLL_DSP";
172 case MipsISD::SHRA_DSP: return "MipsISD::SHRA_DSP";
173 case MipsISD::SHRL_DSP: return "MipsISD::SHRL_DSP";
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000174 case MipsISD::SETCC_DSP: return "MipsISD::SETCC_DSP";
175 case MipsISD::SELECT_CC_DSP: return "MipsISD::SELECT_CC_DSP";
Daniel Sandersce09d072013-08-28 12:14:50 +0000176 case MipsISD::VALL_ZERO: return "MipsISD::VALL_ZERO";
177 case MipsISD::VANY_ZERO: return "MipsISD::VANY_ZERO";
178 case MipsISD::VALL_NONZERO: return "MipsISD::VALL_NONZERO";
179 case MipsISD::VANY_NONZERO: return "MipsISD::VANY_NONZERO";
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000180 case MipsISD::VCEQ: return "MipsISD::VCEQ";
181 case MipsISD::VCLE_S: return "MipsISD::VCLE_S";
182 case MipsISD::VCLE_U: return "MipsISD::VCLE_U";
183 case MipsISD::VCLT_S: return "MipsISD::VCLT_S";
184 case MipsISD::VCLT_U: return "MipsISD::VCLT_U";
Daniel Sanders3ce56622013-09-24 12:18:31 +0000185 case MipsISD::VSMAX: return "MipsISD::VSMAX";
186 case MipsISD::VSMIN: return "MipsISD::VSMIN";
187 case MipsISD::VUMAX: return "MipsISD::VUMAX";
188 case MipsISD::VUMIN: return "MipsISD::VUMIN";
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000189 case MipsISD::VEXTRACT_SEXT_ELT: return "MipsISD::VEXTRACT_SEXT_ELT";
190 case MipsISD::VEXTRACT_ZEXT_ELT: return "MipsISD::VEXTRACT_ZEXT_ELT";
Daniel Sandersf7456c72013-09-23 13:22:24 +0000191 case MipsISD::VNOR: return "MipsISD::VNOR";
Daniel Sanderse5087042013-09-24 14:02:15 +0000192 case MipsISD::VSHF: return "MipsISD::VSHF";
Daniel Sanders26307182013-09-24 14:20:00 +0000193 case MipsISD::SHF: return "MipsISD::SHF";
Daniel Sanders2ed228b2013-09-24 14:36:12 +0000194 case MipsISD::ILVEV: return "MipsISD::ILVEV";
195 case MipsISD::ILVOD: return "MipsISD::ILVOD";
196 case MipsISD::ILVL: return "MipsISD::ILVL";
197 case MipsISD::ILVR: return "MipsISD::ILVR";
Daniel Sandersfae5f2a2013-09-24 14:53:25 +0000198 case MipsISD::PCKEV: return "MipsISD::PCKEV";
199 case MipsISD::PCKOD: return "MipsISD::PCKOD";
Akira Hatanaka15506782011-06-07 18:58:42 +0000200 default: return NULL;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000201 }
202}
203
204MipsTargetLowering::
Chris Lattner5e693ed2009-07-28 03:13:23 +0000205MipsTargetLowering(MipsTargetMachine &TM)
Akira Hatanaka7b502922011-09-26 21:47:02 +0000206 : TargetLowering(TM, new MipsTargetObjectFile()),
207 Subtarget(&TM.getSubtarget<MipsSubtarget>()),
Akira Hatanaka7989f152011-10-28 18:47:24 +0000208 HasMips64(Subtarget->hasMips64()), IsN64(Subtarget->isABI_N64()),
209 IsO32(Subtarget->isABI_O32()) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000210 // Mips does not have i1 type, so use i32 for
Wesley Peck527da1b2010-11-23 03:31:01 +0000211 // setcc operations results (slt, sgt, ...).
Duncan Sands8d6e2e12008-11-23 15:47:28 +0000212 setBooleanContents(ZeroOrOneBooleanContent);
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000213 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000214
Wesley Peck527da1b2010-11-23 03:31:01 +0000215 // Load extented operations for i1 types must be promoted
Owen Anderson9f944592009-08-11 20:47:22 +0000216 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
217 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
218 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000219
Eli Friedman1fa07e12009-07-17 04:07:24 +0000220 // MIPS doesn't have extending float->double load/store
Owen Anderson9f944592009-08-11 20:47:22 +0000221 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
222 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman39d6faa2009-07-17 02:28:12 +0000223
Wesley Peck527da1b2010-11-23 03:31:01 +0000224 // Used by legalize types to correctly generate the setcc result.
225 // Without this, every float setcc comes with a AND/OR with the result,
226 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +0000227 // which is used implicitly by brcond and select operations.
Owen Anderson9f944592009-08-11 20:47:22 +0000228 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +0000229
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000230 // Mips Custom Operations
Akira Hatanaka0f693a82013-03-06 21:32:03 +0000231 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000232 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000233 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000234 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
235 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
236 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
237 setOperationAction(ISD::SELECT, MVT::f32, Custom);
238 setOperationAction(ISD::SELECT, MVT::f64, Custom);
239 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Akira Hatanaka24cf4e32012-07-11 19:32:27 +0000240 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
241 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Akira Hatanakab7f78592012-03-09 23:46:03 +0000242 setOperationAction(ISD::SETCC, MVT::f32, Custom);
243 setOperationAction(ISD::SETCC, MVT::f64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000244 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +0000245 setOperationAction(ISD::VASTART, MVT::Other, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000246 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
247 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000248 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000249
Akira Hatanaka7f4c9d12012-04-11 22:49:04 +0000250 if (!TM.Options.NoNaNsFPMath) {
251 setOperationAction(ISD::FABS, MVT::f32, Custom);
252 setOperationAction(ISD::FABS, MVT::f64, Custom);
253 }
254
Akira Hatanakada00aa82012-03-10 00:03:50 +0000255 if (HasMips64) {
256 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
257 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
258 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
259 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
260 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
261 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Akira Hatanaka019e5922012-06-02 00:04:42 +0000262 setOperationAction(ISD::LOAD, MVT::i64, Custom);
263 setOperationAction(ISD::STORE, MVT::i64, Custom);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000264 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000265 }
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +0000266
Akira Hatanaka0a8ab712012-05-09 00:55:21 +0000267 if (!HasMips64) {
268 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
269 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
270 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
271 }
272
Akira Hatanaka28e02ec2012-11-07 19:10:58 +0000273 setOperationAction(ISD::ADD, MVT::i32, Custom);
274 if (HasMips64)
275 setOperationAction(ISD::ADD, MVT::i64, Custom);
276
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000277 setOperationAction(ISD::SDIV, MVT::i32, Expand);
278 setOperationAction(ISD::SREM, MVT::i32, Expand);
279 setOperationAction(ISD::UDIV, MVT::i32, Expand);
280 setOperationAction(ISD::UREM, MVT::i32, Expand);
Akira Hatanakab1538f92011-10-03 21:06:13 +0000281 setOperationAction(ISD::SDIV, MVT::i64, Expand);
282 setOperationAction(ISD::SREM, MVT::i64, Expand);
283 setOperationAction(ISD::UDIV, MVT::i64, Expand);
284 setOperationAction(ISD::UREM, MVT::i64, Expand);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000285
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000286 // Operations not directly supported by Mips.
Tom Stellardb1588fc2013-03-08 15:36:57 +0000287 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
288 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
289 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
290 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000291 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
292 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Akira Hatanaka79aed152011-12-20 23:40:56 +0000293 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000294 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Akira Hatanaka79aed152011-12-20 23:40:56 +0000295 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000296 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
297 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
Akira Hatanaka410ce9c2011-12-21 00:14:05 +0000298 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000299 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
Akira Hatanaka410ce9c2011-12-21 00:14:05 +0000300 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000301 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
302 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
303 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
304 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000305 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Akira Hatanaka7ba8a8d2011-09-30 18:51:46 +0000306 setOperationAction(ISD::ROTL, MVT::i64, Expand);
Akira Hatanaka33a25af2012-07-31 20:54:48 +0000307 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
308 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Bruno Cardoso Lopesd47180e2010-12-09 17:32:30 +0000309
Akira Hatanakabb49e722011-09-20 23:53:09 +0000310 if (!Subtarget->hasMips32r2())
Bruno Cardoso Lopesd47180e2010-12-09 17:32:30 +0000311 setOperationAction(ISD::ROTR, MVT::i32, Expand);
312
Akira Hatanaka7ba8a8d2011-09-30 18:51:46 +0000313 if (!Subtarget->hasMips64r2())
314 setOperationAction(ISD::ROTR, MVT::i64, Expand);
315
Owen Anderson9f944592009-08-11 20:47:22 +0000316 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes22b69db2011-03-04 18:54:14 +0000317 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000318 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes22b69db2011-03-04 18:54:14 +0000319 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000320 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
321 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000322 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
323 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanakadfb8cda2011-05-23 22:23:58 +0000324 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000325 setOperationAction(ISD::FLOG, MVT::f32, Expand);
326 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
327 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
328 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000329 setOperationAction(ISD::FMA, MVT::f32, Expand);
330 setOperationAction(ISD::FMA, MVT::f64, Expand);
Akira Hatanaka0603ad82012-03-29 18:43:11 +0000331 setOperationAction(ISD::FREM, MVT::f32, Expand);
332 setOperationAction(ISD::FREM, MVT::f64, Expand);
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000333
Akira Hatanaka47ad6742012-04-11 22:59:08 +0000334 if (!TM.Options.NoNaNsFPMath) {
335 setOperationAction(ISD::FNEG, MVT::f32, Expand);
336 setOperationAction(ISD::FNEG, MVT::f64, Expand);
337 }
338
Akira Hatanakac0b02062013-01-30 00:26:49 +0000339 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
340
Bruno Cardoso Lopes048ffab2011-03-09 19:22:22 +0000341 setOperationAction(ISD::VAARG, MVT::Other, Expand);
342 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
343 setOperationAction(ISD::VAEND, MVT::Other, Expand);
344
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000345 // Use the default for now
Owen Anderson9f944592009-08-11 20:47:22 +0000346 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
347 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman26a48482011-07-27 22:21:52 +0000348
Jia Liuf54f60f2012-02-28 07:46:26 +0000349 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
350 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
351 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
352 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
Eli Friedman7dfa7912011-08-29 18:23:02 +0000353
Eli Friedman30a49e92011-08-03 21:06:02 +0000354 setInsertFencesForAtomic(true);
355
Bruno Cardoso Lopesbcc21392008-07-09 05:32:22 +0000356 if (!Subtarget->hasSEInReg()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000357 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
358 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000359 }
360
Akira Hatanaka1d8efab2011-12-21 00:20:27 +0000361 if (!Subtarget->hasBitCount()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000362 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Akira Hatanaka1d8efab2011-12-21 00:20:27 +0000363 setOperationAction(ISD::CTLZ, MVT::i64, Expand);
364 }
Bruno Cardoso Lopes93da7e62008-08-08 06:16:31 +0000365
Akira Hatanaka4706ac92011-12-20 23:56:43 +0000366 if (!Subtarget->hasSwap()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000367 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Akira Hatanaka4706ac92011-12-20 23:56:43 +0000368 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
369 }
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000370
Akira Hatanaka019e5922012-06-02 00:04:42 +0000371 if (HasMips64) {
372 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
Akira Hatanaka956dd222012-03-08 01:59:33 +0000387 setMinFunctionAlignment(HasMips64 ? 3 : 2);
Eli Friedman2518f832011-05-06 20:34:06 +0000388
Akira Hatanaka961883c2012-02-02 03:17:04 +0000389 setStackPointerRegisterToSaveRestore(IsN64 ? Mips::SP_64 : Mips::SP);
Akira Hatanakaaa560002011-05-26 18:59:03 +0000390
Akira Hatanakaf0295372012-02-02 03:13:40 +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
Matt Arsenault758659232013-05-18 00:21:46 +0000406EVT MipsTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Akira Hatanakab13b3332013-01-04 20:06:01 +0000407 if (!VT.isVector())
408 return MVT::i32;
409 return VT.changeVectorElementTypeToInteger();
Scott Michela6729e82008-03-10 15:42:14 +0000410}
411
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000412static SDValue performDivRemCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000413 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000414 const MipsSubtarget *Subtarget) {
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000415 if (DCI.isBeforeLegalizeOps())
416 return SDValue();
417
Akira Hatanakab1538f92011-10-03 21:06:13 +0000418 EVT Ty = N->getValueType(0);
Akira Hatanaka8002a3f2013-08-14 00:47:08 +0000419 unsigned LO = (Ty == MVT::i32) ? Mips::LO0 : Mips::LO0_64;
420 unsigned HI = (Ty == MVT::i32) ? Mips::HI0 : Mips::HI0_64;
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000421 unsigned Opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem16 :
422 MipsISD::DivRemU16;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000423 SDLoc DL(N);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000424
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000425 SDValue DivRem = DAG.getNode(Opc, DL, MVT::Glue,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000426 N->getOperand(0), N->getOperand(1));
427 SDValue InChain = DAG.getEntryNode();
428 SDValue InGlue = DivRem;
429
430 // insert MFLO
431 if (N->hasAnyUseOfValue(0)) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000432 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, DL, LO, Ty,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000433 InGlue);
434 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
435 InChain = CopyFromLo.getValue(1);
436 InGlue = CopyFromLo.getValue(2);
437 }
438
439 // insert MFHI
440 if (N->hasAnyUseOfValue(1)) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000441 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, DL,
Akira Hatanakab1538f92011-10-03 21:06:13 +0000442 HI, Ty, InGlue);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000443 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
444 }
445
446 return SDValue();
447}
448
Akira Hatanaka89af5892013-04-18 01:00:46 +0000449static Mips::CondCode condCodeToFCC(ISD::CondCode CC) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000450 switch (CC) {
451 default: llvm_unreachable("Unknown fp condition code!");
452 case ISD::SETEQ:
453 case ISD::SETOEQ: return Mips::FCOND_OEQ;
454 case ISD::SETUNE: return Mips::FCOND_UNE;
455 case ISD::SETLT:
456 case ISD::SETOLT: return Mips::FCOND_OLT;
457 case ISD::SETGT:
458 case ISD::SETOGT: return Mips::FCOND_OGT;
459 case ISD::SETLE:
460 case ISD::SETOLE: return Mips::FCOND_OLE;
461 case ISD::SETGE:
462 case ISD::SETOGE: return Mips::FCOND_OGE;
463 case ISD::SETULT: return Mips::FCOND_ULT;
464 case ISD::SETULE: return Mips::FCOND_ULE;
465 case ISD::SETUGT: return Mips::FCOND_UGT;
466 case ISD::SETUGE: return Mips::FCOND_UGE;
467 case ISD::SETUO: return Mips::FCOND_UN;
468 case ISD::SETO: return Mips::FCOND_OR;
469 case ISD::SETNE:
470 case ISD::SETONE: return Mips::FCOND_ONE;
471 case ISD::SETUEQ: return Mips::FCOND_UEQ;
472 }
473}
474
475
Akira Hatanakaf0ea5002013-03-30 01:16:38 +0000476/// This function returns true if the floating point conditional branches and
477/// conditional moves which use condition code CC should be inverted.
478static bool invertFPCondCodeUser(Mips::CondCode CC) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000479 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
480 return false;
481
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000482 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
483 "Illegal Condition Code");
Akira Hatanakaa5352702011-03-31 18:26:17 +0000484
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000485 return true;
Akira Hatanakaa5352702011-03-31 18:26:17 +0000486}
487
488// Creates and returns an FPCmp node from a setcc node.
489// Returns Op if setcc is not a floating point comparison.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000490static SDValue createFPCmp(SelectionDAG &DAG, const SDValue &Op) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000491 // must be a SETCC node
492 if (Op.getOpcode() != ISD::SETCC)
493 return Op;
494
495 SDValue LHS = Op.getOperand(0);
496
497 if (!LHS.getValueType().isFloatingPoint())
498 return Op;
499
500 SDValue RHS = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000501 SDLoc DL(Op);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000502
Akira Hatanakaaef55c82011-04-15 21:00:26 +0000503 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
504 // node if necessary.
Akira Hatanakaa5352702011-03-31 18:26:17 +0000505 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
506
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000507 return DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS,
Akira Hatanaka89af5892013-04-18 01:00:46 +0000508 DAG.getConstant(condCodeToFCC(CC), MVT::i32));
Akira Hatanakaa5352702011-03-31 18:26:17 +0000509}
510
511// Creates and returns a CMovFPT/F node.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000512static SDValue createCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000513 SDValue False, SDLoc DL) {
Akira Hatanakaf0ea5002013-03-30 01:16:38 +0000514 ConstantSDNode *CC = cast<ConstantSDNode>(Cond.getOperand(2));
515 bool invert = invertFPCondCodeUser((Mips::CondCode)CC->getSExtValue());
Akira Hatanaka8bce21c2013-07-26 20:51:20 +0000516 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000517
518 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
Akira Hatanaka8bce21c2013-07-26 20:51:20 +0000519 True.getValueType(), True, FCC0, False, Cond);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000520}
521
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000522static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000523 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000524 const MipsSubtarget *Subtarget) {
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000525 if (DCI.isBeforeLegalizeOps())
526 return SDValue();
527
528 SDValue SetCC = N->getOperand(0);
529
530 if ((SetCC.getOpcode() != ISD::SETCC) ||
531 !SetCC.getOperand(0).getValueType().isInteger())
532 return SDValue();
533
534 SDValue False = N->getOperand(2);
535 EVT FalseTy = False.getValueType();
536
537 if (!FalseTy.isInteger())
538 return SDValue();
539
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000540 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(False);
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000541
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000542 // If the RHS (False) is 0, we swap the order of the operands
543 // of ISD::SELECT (obviously also inverting the condition) so that we can
544 // take advantage of conditional moves using the $0 register.
545 // Example:
546 // return (a != 0) ? x : 0;
547 // load $reg, x
548 // movz $reg, $0, a
549 if (!FalseC)
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000550 return SDValue();
551
Andrew Trickef9de2a2013-05-25 02:42:55 +0000552 const SDLoc DL(N);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000553
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000554 if (!FalseC->getZExtValue()) {
555 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
556 SDValue True = N->getOperand(1);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000557
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000558 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
559 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
560
561 return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True);
562 }
563
Matheus Almeidaa6beac12013-12-05 12:07:05 +0000564 // If both operands are integer constants there's a possibility that we
565 // can do some interesting optimizations.
566 SDValue True = N->getOperand(1);
567 ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(True);
568
569 if (!TrueC || !True.getValueType().isInteger())
570 return SDValue();
571
572 // We'll also ignore MVT::i64 operands as this optimizations proves
573 // to be ineffective because of the required sign extensions as the result
574 // of a SETCC operator is always MVT::i32 for non-vector types.
575 if (True.getValueType() == MVT::i64)
576 return SDValue();
577
578 int64_t Diff = TrueC->getSExtValue() - FalseC->getSExtValue();
579
580 // 1) (a < x) ? y : y-1
581 // slti $reg1, a, x
582 // addiu $reg2, $reg1, y-1
583 if (Diff == 1)
584 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, False);
585
586 // 2) (a < x) ? y-1 : y
587 // slti $reg1, a, x
588 // xor $reg1, $reg1, 1
589 // addiu $reg2, $reg1, y-1
590 if (Diff == -1) {
591 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
592 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
593 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
594 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, True);
595 }
596
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000597 // Couldn't optimize.
598 return SDValue();
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000599}
600
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000601static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000602 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000603 const MipsSubtarget *Subtarget) {
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000604 // Pattern match EXT.
605 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
606 // => ext $dst, $src, size, pos
Akira Hatanaka4a3836b2013-10-09 23:36:17 +0000607 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasExtractInsert())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000608 return SDValue();
609
610 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000611 unsigned ShiftRightOpc = ShiftRight.getOpcode();
612
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000613 // Op's first operand must be a shift right.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000614 if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL)
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000615 return SDValue();
616
617 // The second operand of the shift must be an immediate.
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000618 ConstantSDNode *CN;
619 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
620 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000621
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000622 uint64_t Pos = CN->getZExtValue();
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000623 uint64_t SMPos, SMSize;
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000624
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000625 // Op's second operand must be a shifted mask.
626 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000627 !isShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000628 return SDValue();
629
630 // Return if the shifted mask does not start at bit 0 or the sum of its size
631 // and Pos exceeds the word's size.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000632 EVT ValTy = N->getValueType(0);
633 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000634 return SDValue();
635
Andrew Trickef9de2a2013-05-25 02:42:55 +0000636 return DAG.getNode(MipsISD::Ext, SDLoc(N), ValTy,
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000637 ShiftRight.getOperand(0), DAG.getConstant(Pos, MVT::i32),
Akira Hatanakaeea541c2011-08-17 22:59:46 +0000638 DAG.getConstant(SMSize, MVT::i32));
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000639}
Jia Liuf54f60f2012-02-28 07:46:26 +0000640
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000641static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000642 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000643 const MipsSubtarget *Subtarget) {
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000644 // Pattern match INS.
645 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
Jia Liuf54f60f2012-02-28 07:46:26 +0000646 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000647 // => ins $dst, $src, size, pos, $src1
Akira Hatanaka4a3836b2013-10-09 23:36:17 +0000648 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasExtractInsert())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000649 return SDValue();
650
651 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
652 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
653 ConstantSDNode *CN;
654
655 // See if Op's first operand matches (and $src1 , mask0).
656 if (And0.getOpcode() != ISD::AND)
657 return SDValue();
658
659 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000660 !isShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000661 return SDValue();
662
663 // See if Op's second operand matches (and (shl $src, pos), mask1).
664 if (And1.getOpcode() != ISD::AND)
665 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000666
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000667 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000668 !isShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000669 return SDValue();
670
671 // The shift masks must have the same position and size.
672 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
673 return SDValue();
674
675 SDValue Shl = And1.getOperand(0);
676 if (Shl.getOpcode() != ISD::SHL)
677 return SDValue();
678
679 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
680 return SDValue();
681
682 unsigned Shamt = CN->getZExtValue();
683
684 // Return if the shift amount and the first bit position of mask are not the
Jia Liuf54f60f2012-02-28 07:46:26 +0000685 // same.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000686 EVT ValTy = N->getValueType(0);
687 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000688 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000689
Andrew Trickef9de2a2013-05-25 02:42:55 +0000690 return DAG.getNode(MipsISD::Ins, SDLoc(N), ValTy, Shl.getOperand(0),
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000691 DAG.getConstant(SMPos0, MVT::i32),
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000692 DAG.getConstant(SMSize0, MVT::i32), And0.getOperand(0));
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000693}
Jia Liuf54f60f2012-02-28 07:46:26 +0000694
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000695static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000696 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000697 const MipsSubtarget *Subtarget) {
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000698 // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt))
699
700 if (DCI.isBeforeLegalizeOps())
701 return SDValue();
702
703 SDValue Add = N->getOperand(1);
704
705 if (Add.getOpcode() != ISD::ADD)
706 return SDValue();
707
708 SDValue Lo = Add.getOperand(1);
709
710 if ((Lo.getOpcode() != MipsISD::Lo) ||
711 (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable))
712 return SDValue();
713
714 EVT ValTy = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000715 SDLoc DL(N);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000716
717 SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, N->getOperand(0),
718 Add.getOperand(0));
719 return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo);
720}
721
Bruno Cardoso Lopes61a61e92011-02-10 18:05:10 +0000722SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000723 const {
724 SelectionDAG &DAG = DCI.DAG;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000725 unsigned Opc = N->getOpcode();
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000726
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000727 switch (Opc) {
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000728 default: break;
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000729 case ISD::SDIVREM:
730 case ISD::UDIVREM:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000731 return performDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000732 case ISD::SELECT:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000733 return performSELECTCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000734 case ISD::AND:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000735 return performANDCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000736 case ISD::OR:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000737 return performORCombine(N, DAG, DCI, Subtarget);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000738 case ISD::ADD:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000739 return performADDCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000740 }
741
742 return SDValue();
743}
744
Akira Hatanakafabb8cf2012-09-21 23:58:31 +0000745void
746MipsTargetLowering::LowerOperationWrapper(SDNode *N,
747 SmallVectorImpl<SDValue> &Results,
748 SelectionDAG &DAG) const {
749 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
750
751 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
752 Results.push_back(Res.getValue(I));
753}
754
755void
756MipsTargetLowering::ReplaceNodeResults(SDNode *N,
757 SmallVectorImpl<SDValue> &Results,
758 SelectionDAG &DAG) const {
Akira Hatanaka9da442f2013-04-30 21:17:07 +0000759 return LowerOperationWrapper(N, Results, DAG);
Akira Hatanakafabb8cf2012-09-21 23:58:31 +0000760}
761
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000762SDValue MipsTargetLowering::
Dan Gohman21cea8a2010-04-17 15:26:15 +0000763LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000764{
Wesley Peck527da1b2010-11-23 03:31:01 +0000765 switch (Op.getOpcode())
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000766 {
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000767 case ISD::BR_JT: return lowerBR_JT(Op, DAG);
768 case ISD::BRCOND: return lowerBRCOND(Op, DAG);
769 case ISD::ConstantPool: return lowerConstantPool(Op, DAG);
770 case ISD::GlobalAddress: return lowerGlobalAddress(Op, DAG);
771 case ISD::BlockAddress: return lowerBlockAddress(Op, DAG);
772 case ISD::GlobalTLSAddress: return lowerGlobalTLSAddress(Op, DAG);
773 case ISD::JumpTable: return lowerJumpTable(Op, DAG);
774 case ISD::SELECT: return lowerSELECT(Op, DAG);
775 case ISD::SELECT_CC: return lowerSELECT_CC(Op, DAG);
776 case ISD::SETCC: return lowerSETCC(Op, DAG);
777 case ISD::VASTART: return lowerVASTART(Op, DAG);
778 case ISD::FCOPYSIGN: return lowerFCOPYSIGN(Op, DAG);
779 case ISD::FABS: return lowerFABS(Op, DAG);
780 case ISD::FRAMEADDR: return lowerFRAMEADDR(Op, DAG);
781 case ISD::RETURNADDR: return lowerRETURNADDR(Op, DAG);
782 case ISD::EH_RETURN: return lowerEH_RETURN(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000783 case ISD::ATOMIC_FENCE: return lowerATOMIC_FENCE(Op, DAG);
784 case ISD::SHL_PARTS: return lowerShiftLeftParts(Op, DAG);
785 case ISD::SRA_PARTS: return lowerShiftRightParts(Op, DAG, true);
786 case ISD::SRL_PARTS: return lowerShiftRightParts(Op, DAG, false);
787 case ISD::LOAD: return lowerLOAD(Op, DAG);
788 case ISD::STORE: return lowerSTORE(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000789 case ISD::ADD: return lowerADD(Op, DAG);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000790 case ISD::FP_TO_SINT: return lowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000791 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000792 return SDValue();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000793}
794
Akira Hatanakae2489122011-04-15 21:51:11 +0000795//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000796// Lower helper functions
Akira Hatanakae2489122011-04-15 21:51:11 +0000797//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000798
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000799// addLiveIn - This helper function adds the specified physical register to the
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000800// MachineFunction as a live in value. It also creates a corresponding
801// virtual register for it.
802static unsigned
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000803addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000804{
Chris Lattnera10fff52007-12-31 04:13:23 +0000805 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
806 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000807 return VReg;
808}
809
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000810static MachineBasicBlock *expandPseudoDIV(MachineInstr *MI,
811 MachineBasicBlock &MBB,
812 const TargetInstrInfo &TII,
813 bool Is64Bit) {
814 if (NoZeroDivCheck)
815 return &MBB;
816
817 // Insert instruction "teq $divisor_reg, $zero, 7".
818 MachineBasicBlock::iterator I(MI);
819 MachineInstrBuilder MIB;
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000820 MachineOperand &Divisor = MI->getOperand(2);
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000821 MIB = BuildMI(MBB, std::next(I), MI->getDebugLoc(), TII.get(Mips::TEQ))
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000822 .addReg(Divisor.getReg(), getKillRegState(Divisor.isKill()))
823 .addReg(Mips::ZERO).addImm(7);
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000824
825 // Use the 32-bit sub-register if this is a 64-bit division.
826 if (Is64Bit)
827 MIB->getOperand(0).setSubReg(Mips::sub_32);
828
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000829 // Clear Divisor's kill flag.
830 Divisor.setIsKill(false);
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000831 return &MBB;
832}
833
Akira Hatanakae4bd0542012-09-27 02:15:57 +0000834MachineBasicBlock *
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +0000835MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +0000836 MachineBasicBlock *BB) const {
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +0000837 switch (MI->getOpcode()) {
Reed Kotler97ba5f22013-02-21 04:22:38 +0000838 default:
839 llvm_unreachable("Unexpected instr type to insert");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000840 case Mips::ATOMIC_LOAD_ADD_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000841 return emitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000842 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000843 return emitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000844 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000845 return emitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000846 case Mips::ATOMIC_LOAD_ADD_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000847 return emitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000848
849 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000850 return emitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000851 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000852 return emitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000853 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000854 return emitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000855 case Mips::ATOMIC_LOAD_AND_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000856 return emitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000857
858 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000859 return emitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000860 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000861 return emitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000862 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000863 return emitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000864 case Mips::ATOMIC_LOAD_OR_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000865 return emitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000866
867 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000868 return emitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000869 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000870 return emitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000871 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000872 return emitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000873 case Mips::ATOMIC_LOAD_XOR_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000874 return emitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000875
876 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000877 return emitAtomicBinaryPartword(MI, BB, 1, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000878 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000879 return emitAtomicBinaryPartword(MI, BB, 2, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000880 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000881 return emitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000882 case Mips::ATOMIC_LOAD_NAND_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000883 return emitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000884
885 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000886 return emitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000887 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000888 return emitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000889 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000890 return emitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000891 case Mips::ATOMIC_LOAD_SUB_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000892 return emitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000893
894 case Mips::ATOMIC_SWAP_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000895 return emitAtomicBinaryPartword(MI, BB, 1, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000896 case Mips::ATOMIC_SWAP_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000897 return emitAtomicBinaryPartword(MI, BB, 2, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000898 case Mips::ATOMIC_SWAP_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000899 return emitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000900 case Mips::ATOMIC_SWAP_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000901 return emitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000902
903 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000904 return emitAtomicCmpSwapPartword(MI, BB, 1);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000905 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000906 return emitAtomicCmpSwapPartword(MI, BB, 2);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000907 case Mips::ATOMIC_CMP_SWAP_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000908 return emitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000909 case Mips::ATOMIC_CMP_SWAP_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000910 return emitAtomicCmpSwap(MI, BB, 8);
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000911 case Mips::PseudoSDIV:
912 case Mips::PseudoUDIV:
913 return expandPseudoDIV(MI, *BB, *getTargetMachine().getInstrInfo(), false);
914 case Mips::PseudoDSDIV:
915 case Mips::PseudoDUDIV:
916 return expandPseudoDIV(MI, *BB, *getTargetMachine().getInstrInfo(), true);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000917 }
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +0000918}
919
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000920// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
921// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
922MachineBasicBlock *
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000923MipsTargetLowering::emitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher0713a9d2011-06-08 23:55:35 +0000924 unsigned Size, unsigned BinOpcode,
Akira Hatanaka15506782011-06-07 18:58:42 +0000925 bool Nand) const {
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000926 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000927
928 MachineFunction *MF = BB->getParent();
929 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000930 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000931 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000932 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000933 unsigned LL, SC, AND, NOR, ZERO, BEQ;
934
935 if (Size == 4) {
Zoran Jovanovicff9d5f32013-12-19 16:12:56 +0000936 LL = isMicroMips ? Mips::LL_MM : Mips::LL;
937 SC = isMicroMips ? Mips::SC_MM : Mips::SC;
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000938 AND = Mips::AND;
939 NOR = Mips::NOR;
940 ZERO = Mips::ZERO;
941 BEQ = Mips::BEQ;
942 }
943 else {
Akira Hatanaka6781fc12013-08-20 21:08:22 +0000944 LL = Mips::LLD;
945 SC = Mips::SCD;
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000946 AND = Mips::AND64;
947 NOR = Mips::NOR64;
948 ZERO = Mips::ZERO_64;
949 BEQ = Mips::BEQ64;
950 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000951
Akira Hatanaka0e019592011-07-19 20:11:17 +0000952 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000953 unsigned Ptr = MI->getOperand(1).getReg();
954 unsigned Incr = MI->getOperand(2).getReg();
955
Akira Hatanaka0e019592011-07-19 20:11:17 +0000956 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
957 unsigned AndRes = RegInfo.createVirtualRegister(RC);
958 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000959
960 // insert new blocks after the current block
961 const BasicBlock *LLVM_BB = BB->getBasicBlock();
962 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
963 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
964 MachineFunction::iterator It = BB;
965 ++It;
966 MF->insert(It, loopMBB);
967 MF->insert(It, exitMBB);
968
969 // Transfer the remainder of BB and its successor edges to exitMBB.
970 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000971 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000972 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
973
974 // thisMBB:
975 // ...
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000976 // fallthrough --> loopMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000977 BB->addSuccessor(loopMBB);
Akira Hatanaka08636b42011-07-19 17:09:53 +0000978 loopMBB->addSuccessor(loopMBB);
979 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000980
981 // loopMBB:
982 // ll oldval, 0(ptr)
Akira Hatanaka0e019592011-07-19 20:11:17 +0000983 // <binop> storeval, oldval, incr
984 // sc success, storeval, 0(ptr)
985 // beq success, $0, loopMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000986 BB = loopMBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000987 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000988 if (Nand) {
Akira Hatanaka0e019592011-07-19 20:11:17 +0000989 // and andres, oldval, incr
990 // nor storeval, $0, andres
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000991 BuildMI(BB, DL, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
992 BuildMI(BB, DL, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000993 } else if (BinOpcode) {
Akira Hatanaka0e019592011-07-19 20:11:17 +0000994 // <binop> storeval, oldval, incr
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000995 BuildMI(BB, DL, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000996 } else {
Akira Hatanaka0e019592011-07-19 20:11:17 +0000997 StoreVal = Incr;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000998 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000999 BuildMI(BB, DL, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
1000 BuildMI(BB, DL, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001001
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001002 MI->eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001003
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001004 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001005}
1006
1007MachineBasicBlock *
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001008MipsTargetLowering::emitAtomicBinaryPartword(MachineInstr *MI,
Akira Hatanaka15506782011-06-07 18:58:42 +00001009 MachineBasicBlock *BB,
1010 unsigned Size, unsigned BinOpcode,
1011 bool Nand) const {
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001012 assert((Size == 1 || Size == 2) &&
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001013 "Unsupported size for EmitAtomicBinaryPartial.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001014
1015 MachineFunction *MF = BB->getParent();
1016 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1017 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1018 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001019 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001020
1021 unsigned Dest = MI->getOperand(0).getReg();
1022 unsigned Ptr = MI->getOperand(1).getReg();
1023 unsigned Incr = MI->getOperand(2).getReg();
1024
Akira Hatanaka0e019592011-07-19 20:11:17 +00001025 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1026 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001027 unsigned Mask = RegInfo.createVirtualRegister(RC);
1028 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001029 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1030 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001031 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001032 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1033 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1034 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1035 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1036 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka9663dd32011-07-19 20:56:53 +00001037 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001038 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1039 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1040 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1041 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1042 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001043
1044 // insert new blocks after the current block
1045 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1046 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001047 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001048 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1049 MachineFunction::iterator It = BB;
1050 ++It;
1051 MF->insert(It, loopMBB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001052 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001053 MF->insert(It, exitMBB);
1054
1055 // Transfer the remainder of BB and its successor edges to exitMBB.
1056 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001057 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001058 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1059
Akira Hatanaka08636b42011-07-19 17:09:53 +00001060 BB->addSuccessor(loopMBB);
1061 loopMBB->addSuccessor(loopMBB);
1062 loopMBB->addSuccessor(sinkMBB);
1063 sinkMBB->addSuccessor(exitMBB);
1064
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001065 // thisMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001066 // addiu masklsb2,$0,-4 # 0xfffffffc
1067 // and alignedaddr,ptr,masklsb2
1068 // andi ptrlsb2,ptr,3
1069 // sll shiftamt,ptrlsb2,3
1070 // ori maskupper,$0,255 # 0xff
1071 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001072 // nor mask2,$0,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001073 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001074
1075 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001076 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001077 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001078 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001079 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001080 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Akira Hatanaka2bf97332013-05-31 03:25:44 +00001081 if (Subtarget->isLittle()) {
1082 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1083 } else {
1084 unsigned Off = RegInfo.createVirtualRegister(RC);
1085 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1086 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1087 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1088 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001089 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001090 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001091 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001092 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001093 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001094 BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(Incr).addReg(ShiftAmt);
Bruno Cardoso Lopesf771a0f2011-05-31 20:25:26 +00001095
Akira Hatanaka27292632011-07-18 18:52:12 +00001096 // atomic.load.binop
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001097 // loopMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001098 // ll oldval,0(alignedaddr)
1099 // binop binopres,oldval,incr2
1100 // and newval,binopres,mask
1101 // and maskedoldval0,oldval,mask2
1102 // or storeval,maskedoldval0,newval
1103 // sc success,storeval,0(alignedaddr)
1104 // beq success,$0,loopMBB
1105
Akira Hatanaka27292632011-07-18 18:52:12 +00001106 // atomic.swap
1107 // loopMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001108 // ll oldval,0(alignedaddr)
Akira Hatanakae4503582011-07-19 18:14:26 +00001109 // and newval,incr2,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001110 // and maskedoldval0,oldval,mask2
1111 // or storeval,maskedoldval0,newval
1112 // sc success,storeval,0(alignedaddr)
1113 // beq success,$0,loopMBB
Akira Hatanaka27292632011-07-18 18:52:12 +00001114
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001115 BB = loopMBB;
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001116 BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001117 if (Nand) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001118 // and andres, oldval, incr2
1119 // nor binopres, $0, andres
1120 // and newval, binopres, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001121 BuildMI(BB, DL, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1122 BuildMI(BB, DL, TII->get(Mips::NOR), BinOpRes)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001123 .addReg(Mips::ZERO).addReg(AndRes);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001124 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001125 } else if (BinOpcode) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001126 // <binop> binopres, oldval, incr2
1127 // and newval, binopres, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001128 BuildMI(BB, DL, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1129 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001130 } else { // atomic.swap
Akira Hatanaka0e019592011-07-19 20:11:17 +00001131 // and newval, incr2, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001132 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanakae4503582011-07-19 18:14:26 +00001133 }
Jia Liuf54f60f2012-02-28 07:46:26 +00001134
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001135 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001136 .addReg(OldVal).addReg(Mask2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001137 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka9663dd32011-07-19 20:56:53 +00001138 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001139 BuildMI(BB, DL, TII->get(Mips::SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001140 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001141 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001142 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001143
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001144 // sinkMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001145 // and maskedoldval1,oldval,mask
1146 // srl srlres,maskedoldval1,shiftamt
1147 // sll sllres,srlres,24
1148 // sra dest,sllres,24
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001149 BB = sinkMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001150 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakae97bd812011-07-19 03:14:58 +00001151
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001152 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001153 .addReg(OldVal).addReg(Mask);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001154 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001155 .addReg(MaskedOldVal1).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001156 BuildMI(BB, DL, TII->get(Mips::SLL), SllRes)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001157 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001158 BuildMI(BB, DL, TII->get(Mips::SRA), Dest)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001159 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001160
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001161 MI->eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001162
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001163 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001164}
1165
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001166MachineBasicBlock * MipsTargetLowering::emitAtomicCmpSwap(MachineInstr *MI,
1167 MachineBasicBlock *BB,
1168 unsigned Size) const {
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001169 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001170
1171 MachineFunction *MF = BB->getParent();
1172 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001173 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001174 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001175 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001176 unsigned LL, SC, ZERO, BNE, BEQ;
1177
1178 if (Size == 4) {
Zoran Jovanovicff9d5f32013-12-19 16:12:56 +00001179 LL = isMicroMips ? Mips::LL_MM : Mips::LL;
1180 SC = isMicroMips ? Mips::SC_MM : Mips::SC;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001181 ZERO = Mips::ZERO;
1182 BNE = Mips::BNE;
1183 BEQ = Mips::BEQ;
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001184 } else {
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001185 LL = Mips::LLD;
1186 SC = Mips::SCD;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001187 ZERO = Mips::ZERO_64;
1188 BNE = Mips::BNE64;
1189 BEQ = Mips::BEQ64;
1190 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001191
1192 unsigned Dest = MI->getOperand(0).getReg();
1193 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka0e019592011-07-19 20:11:17 +00001194 unsigned OldVal = MI->getOperand(2).getReg();
1195 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001196
Akira Hatanaka0e019592011-07-19 20:11:17 +00001197 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001198
1199 // insert new blocks after the current block
1200 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1201 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1202 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1203 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1204 MachineFunction::iterator It = BB;
1205 ++It;
1206 MF->insert(It, loop1MBB);
1207 MF->insert(It, loop2MBB);
1208 MF->insert(It, exitMBB);
1209
1210 // Transfer the remainder of BB and its successor edges to exitMBB.
1211 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001212 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001213 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1214
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001215 // thisMBB:
1216 // ...
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001217 // fallthrough --> loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001218 BB->addSuccessor(loop1MBB);
Akira Hatanaka08636b42011-07-19 17:09:53 +00001219 loop1MBB->addSuccessor(exitMBB);
1220 loop1MBB->addSuccessor(loop2MBB);
1221 loop2MBB->addSuccessor(loop1MBB);
1222 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001223
1224 // loop1MBB:
1225 // ll dest, 0(ptr)
1226 // bne dest, oldval, exitMBB
1227 BB = loop1MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001228 BuildMI(BB, DL, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1229 BuildMI(BB, DL, TII->get(BNE))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001230 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001231
1232 // loop2MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001233 // sc success, newval, 0(ptr)
1234 // beq success, $0, loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001235 BB = loop2MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001236 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001237 .addReg(NewVal).addReg(Ptr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001238 BuildMI(BB, DL, TII->get(BEQ))
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001239 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001240
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001241 MI->eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001242
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001243 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001244}
1245
1246MachineBasicBlock *
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001247MipsTargetLowering::emitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka15506782011-06-07 18:58:42 +00001248 MachineBasicBlock *BB,
1249 unsigned Size) const {
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001250 assert((Size == 1 || Size == 2) &&
1251 "Unsupported size for EmitAtomicCmpSwapPartial.");
1252
1253 MachineFunction *MF = BB->getParent();
1254 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1255 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1256 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001257 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001258
1259 unsigned Dest = MI->getOperand(0).getReg();
1260 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka0e019592011-07-19 20:11:17 +00001261 unsigned CmpVal = MI->getOperand(2).getReg();
1262 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001263
Akira Hatanaka0e019592011-07-19 20:11:17 +00001264 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1265 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001266 unsigned Mask = RegInfo.createVirtualRegister(RC);
1267 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001268 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1269 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1270 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1271 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1272 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1273 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1274 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1275 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1276 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1277 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1278 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1279 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1280 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1281 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001282
1283 // insert new blocks after the current block
1284 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1285 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1286 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001287 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001288 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1289 MachineFunction::iterator It = BB;
1290 ++It;
1291 MF->insert(It, loop1MBB);
1292 MF->insert(It, loop2MBB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001293 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001294 MF->insert(It, exitMBB);
1295
1296 // Transfer the remainder of BB and its successor edges to exitMBB.
1297 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001298 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001299 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1300
Akira Hatanaka08636b42011-07-19 17:09:53 +00001301 BB->addSuccessor(loop1MBB);
1302 loop1MBB->addSuccessor(sinkMBB);
1303 loop1MBB->addSuccessor(loop2MBB);
1304 loop2MBB->addSuccessor(loop1MBB);
1305 loop2MBB->addSuccessor(sinkMBB);
1306 sinkMBB->addSuccessor(exitMBB);
1307
Akira Hatanakae4503582011-07-19 18:14:26 +00001308 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001309 // thisMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001310 // addiu masklsb2,$0,-4 # 0xfffffffc
1311 // and alignedaddr,ptr,masklsb2
1312 // andi ptrlsb2,ptr,3
1313 // sll shiftamt,ptrlsb2,3
1314 // ori maskupper,$0,255 # 0xff
1315 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001316 // nor mask2,$0,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001317 // andi maskedcmpval,cmpval,255
1318 // sll shiftedcmpval,maskedcmpval,shiftamt
1319 // andi maskednewval,newval,255
1320 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001321 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001322 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001323 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001324 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001325 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001326 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Akira Hatanaka2bf97332013-05-31 03:25:44 +00001327 if (Subtarget->isLittle()) {
1328 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1329 } else {
1330 unsigned Off = RegInfo.createVirtualRegister(RC);
1331 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1332 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1333 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1334 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001335 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001336 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001337 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001338 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001339 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1340 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001341 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001342 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001343 .addReg(MaskedCmpVal).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001344 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001345 .addReg(NewVal).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001346 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001347 .addReg(MaskedNewVal).addReg(ShiftAmt);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001348
1349 // loop1MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001350 // ll oldval,0(alginedaddr)
1351 // and maskedoldval0,oldval,mask
1352 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001353 BB = loop1MBB;
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001354 BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001355 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001356 .addReg(OldVal).addReg(Mask);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001357 BuildMI(BB, DL, TII->get(Mips::BNE))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001358 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001359
1360 // loop2MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001361 // and maskedoldval1,oldval,mask2
1362 // or storeval,maskedoldval1,shiftednewval
1363 // sc success,storeval,0(alignedaddr)
1364 // beq success,$0,loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001365 BB = loop2MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001366 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001367 .addReg(OldVal).addReg(Mask2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001368 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001369 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001370 BuildMI(BB, DL, TII->get(Mips::SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001371 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001372 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001373 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001374
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001375 // sinkMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001376 // srl srlres,maskedoldval0,shiftamt
1377 // sll sllres,srlres,24
1378 // sra dest,sllres,24
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001379 BB = sinkMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001380 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakae97bd812011-07-19 03:14:58 +00001381
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001382 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001383 .addReg(MaskedOldVal0).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001384 BuildMI(BB, DL, TII->get(Mips::SLL), SllRes)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001385 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001386 BuildMI(BB, DL, TII->get(Mips::SRA), Dest)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001387 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001388
1389 MI->eraseFromParent(); // The instruction is gone now.
1390
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001391 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001392}
1393
Akira Hatanakae2489122011-04-15 21:51:11 +00001394//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001395// Misc Lower Operation implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00001396//===----------------------------------------------------------------------===//
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001397SDValue MipsTargetLowering::lowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001398 SDValue Chain = Op.getOperand(0);
1399 SDValue Table = Op.getOperand(1);
1400 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001401 SDLoc DL(Op);
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001402 EVT PTy = getPointerTy();
1403 unsigned EntrySize =
1404 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(*getDataLayout());
1405
1406 Index = DAG.getNode(ISD::MUL, DL, PTy, Index,
1407 DAG.getConstant(EntrySize, PTy));
1408 SDValue Addr = DAG.getNode(ISD::ADD, DL, PTy, Index, Table);
1409
1410 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
1411 Addr = DAG.getExtLoad(ISD::SEXTLOAD, DL, PTy, Chain, Addr,
1412 MachinePointerInfo::getJumpTable(), MemVT, false, false,
1413 0);
1414 Chain = Addr.getValue(1);
1415
1416 if ((getTargetMachine().getRelocationModel() == Reloc::PIC_) || IsN64) {
1417 // For PIC, the sequence is:
1418 // BRIND(load(Jumptable + index) + RelocBase)
1419 // RelocBase can be JumpTable, GOT or some sort of global base.
1420 Addr = DAG.getNode(ISD::ADD, DL, PTy, Addr,
1421 getPICJumpTableRelocBase(Table, DAG));
1422 }
1423
1424 return DAG.getNode(ISD::BRIND, DL, MVT::Other, Chain, Addr);
1425}
1426
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001427SDValue MipsTargetLowering::lowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Wesley Peck527da1b2010-11-23 03:31:01 +00001428 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001429 // the block to branch to if the condition is true.
1430 SDValue Chain = Op.getOperand(0);
1431 SDValue Dest = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001432 SDLoc DL(Op);
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001433
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001434 SDValue CondRes = createFPCmp(DAG, Op.getOperand(1));
Akira Hatanakaa5352702011-03-31 18:26:17 +00001435
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001436 // Return if flag is not set by a floating point comparison.
Akira Hatanakaa5352702011-03-31 18:26:17 +00001437 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopesa9504222008-07-30 17:06:13 +00001438 return Op;
Wesley Peck527da1b2010-11-23 03:31:01 +00001439
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +00001440 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmaneffb8942008-09-12 16:56:44 +00001441 Mips::CondCode CC =
1442 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Akira Hatanakaf0ea5002013-03-30 01:16:38 +00001443 unsigned Opc = invertFPCondCodeUser(CC) ? Mips::BRANCH_F : Mips::BRANCH_T;
1444 SDValue BrCode = DAG.getConstant(Opc, MVT::i32);
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +00001445 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001446 return DAG.getNode(MipsISD::FPBrcond, DL, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +00001447 FCC0, Dest, CondRes);
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001448}
1449
1450SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001451lowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001452{
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001453 SDValue Cond = createFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001454
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001455 // Return if flag is not set by a floating point comparison.
Akira Hatanakaa5352702011-03-31 18:26:17 +00001456 if (Cond.getOpcode() != MipsISD::FPCmp)
1457 return Op;
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +00001458
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001459 return createCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
Andrew Trickef9de2a2013-05-25 02:42:55 +00001460 SDLoc(Op));
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001461}
1462
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001463SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001464lowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001465{
Andrew Trickef9de2a2013-05-25 02:42:55 +00001466 SDLoc DL(Op);
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001467 EVT Ty = Op.getOperand(0).getValueType();
Matt Arsenault758659232013-05-18 00:21:46 +00001468 SDValue Cond = DAG.getNode(ISD::SETCC, DL,
1469 getSetCCResultType(*DAG.getContext(), Ty),
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001470 Op.getOperand(0), Op.getOperand(1),
1471 Op.getOperand(4));
1472
1473 return DAG.getNode(ISD::SELECT, DL, Op.getValueType(), Cond, Op.getOperand(2),
1474 Op.getOperand(3));
1475}
1476
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001477SDValue MipsTargetLowering::lowerSETCC(SDValue Op, SelectionDAG &DAG) const {
1478 SDValue Cond = createFPCmp(DAG, Op);
Akira Hatanakab7f78592012-03-09 23:46:03 +00001479
1480 assert(Cond.getOpcode() == MipsISD::FPCmp &&
1481 "Floating point operand expected.");
1482
1483 SDValue True = DAG.getConstant(1, MVT::i32);
1484 SDValue False = DAG.getConstant(0, MVT::i32);
1485
Andrew Trickef9de2a2013-05-25 02:42:55 +00001486 return createCMovFP(DAG, Cond, True, False, SDLoc(Op));
Akira Hatanakab7f78592012-03-09 23:46:03 +00001487}
1488
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001489SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001490 SelectionDAG &DAG) const {
Dale Johannesen400dc2e2009-02-06 21:50:26 +00001491 // FIXME there isn't actually debug info here
Andrew Trickef9de2a2013-05-25 02:42:55 +00001492 SDLoc DL(Op);
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001493 EVT Ty = Op.getValueType();
1494 GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
1495 const GlobalValue *GV = N->getGlobal();
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001496
Akira Hatanaka09b23eb2011-10-11 00:55:05 +00001497 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
Akira Hatanaka92a96e12012-09-12 23:27:55 +00001498 const MipsTargetObjectFile &TLOF =
1499 (const MipsTargetObjectFile&)getObjFileLowering();
Wesley Peck527da1b2010-11-23 03:31:01 +00001500
Chris Lattner58e8be82009-08-13 05:41:27 +00001501 // %gp_rel relocation
Wesley Peck527da1b2010-11-23 03:31:01 +00001502 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001503 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, 0,
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +00001504 MipsII::MO_GPREL);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001505 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, DL,
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001506 DAG.getVTList(MVT::i32), &GA, 1);
Akira Hatanakaad495022012-08-22 03:18:13 +00001507 SDValue GPReg = DAG.getRegister(Mips::GP, MVT::i32);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001508 return DAG.getNode(ISD::ADD, DL, MVT::i32, GPReg, GPRelNode);
Chris Lattner58e8be82009-08-13 05:41:27 +00001509 }
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001510
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001511 // %hi/%lo relocation
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001512 return getAddrNonPIC(N, Ty, DAG);
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001513 }
1514
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001515 if (GV->hasInternalLinkage() || (GV->hasLocalLinkage() && !isa<Function>(GV)))
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001516 return getAddrLocal(N, Ty, DAG, HasMips64);
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001517
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00001518 if (LargeGOT)
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001519 return getAddrGlobalLargeGOT(N, Ty, DAG, MipsII::MO_GOT_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00001520 MipsII::MO_GOT_LO16, DAG.getEntryNode(),
1521 MachinePointerInfo::getGOT());
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00001522
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001523 return getAddrGlobal(N, Ty, DAG,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00001524 HasMips64 ? MipsII::MO_GOT_DISP : MipsII::MO_GOT16,
1525 DAG.getEntryNode(), MachinePointerInfo::getGOT());
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001526}
1527
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001528SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op,
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +00001529 SelectionDAG &DAG) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001530 BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op);
1531 EVT Ty = Op.getValueType();
Akira Hatanaka30f97cf2013-09-25 00:30:25 +00001532
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001533 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
1534 return getAddrNonPIC(N, Ty, DAG);
1535
1536 return getAddrLocal(N, Ty, DAG, HasMips64);
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +00001537}
1538
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001539SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001540lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001541{
Akira Hatanakabff84e12011-12-14 18:26:41 +00001542 // If the relocation model is PIC, use the General Dynamic TLS Model or
1543 // Local Dynamic TLS model, otherwise use the Initial Exec or
1544 // Local Exec TLS Model.
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001545
1546 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001547 SDLoc DL(GA);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001548 const GlobalValue *GV = GA->getGlobal();
1549 EVT PtrVT = getPointerTy();
1550
Hans Wennborgaea41202012-05-04 09:40:39 +00001551 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
1552
1553 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Hans Wennborg245917b2012-06-04 14:02:08 +00001554 // General Dynamic and Local Dynamic TLS Model.
1555 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
1556 : MipsII::MO_TLSGD;
1557
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001558 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag);
1559 SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT,
1560 getGlobalReg(DAG, PtrVT), TGA);
Akira Hatanakaf10ee842011-12-08 21:05:38 +00001561 unsigned PtrSize = PtrVT.getSizeInBits();
1562 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1563
Benjamin Kramer64ba50a2011-12-11 12:21:34 +00001564 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001565
1566 ArgListTy Args;
1567 ArgListEntry Entry;
1568 Entry.Node = Argument;
Akira Hatanakadee6c822011-12-08 20:34:32 +00001569 Entry.Ty = PtrTy;
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001570 Args.push_back(Entry);
Jia Liuf54f60f2012-02-28 07:46:26 +00001571
Justin Holewinskiaa583972012-05-25 16:35:28 +00001572 TargetLowering::CallLoweringInfo CLI(DAG.getEntryNode(), PtrTy,
Evan Cheng65f9d192012-02-28 18:51:51 +00001573 false, false, false, false, 0, CallingConv::C,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001574 /*IsTailCall=*/false, /*doesNotRet=*/false,
Evan Cheng65f9d192012-02-28 18:51:51 +00001575 /*isReturnValueUsed=*/true,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001576 TlsGetAddr, Args, DAG, DL);
Justin Holewinskiaa583972012-05-25 16:35:28 +00001577 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001578
Akira Hatanakabff84e12011-12-14 18:26:41 +00001579 SDValue Ret = CallResult.first;
1580
Hans Wennborgaea41202012-05-04 09:40:39 +00001581 if (model != TLSModel::LocalDynamic)
Akira Hatanakabff84e12011-12-14 18:26:41 +00001582 return Ret;
1583
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001584 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanakabff84e12011-12-14 18:26:41 +00001585 MipsII::MO_DTPREL_HI);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001586 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1587 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanakabff84e12011-12-14 18:26:41 +00001588 MipsII::MO_DTPREL_LO);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001589 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1590 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret);
1591 return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001592 }
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001593
1594 SDValue Offset;
Hans Wennborgaea41202012-05-04 09:40:39 +00001595 if (model == TLSModel::InitialExec) {
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001596 // Initial Exec TLS Model
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001597 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001598 MipsII::MO_GOTTPREL);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001599 TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT),
Akira Hatanakab049aef2012-02-24 22:34:47 +00001600 TGA);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001601 Offset = DAG.getLoad(PtrVT, DL,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001602 DAG.getEntryNode(), TGA, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001603 false, false, false, 0);
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001604 } else {
1605 // Local Exec TLS Model
Hans Wennborgaea41202012-05-04 09:40:39 +00001606 assert(model == TLSModel::LocalExec);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001607 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001608 MipsII::MO_TPREL_HI);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001609 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001610 MipsII::MO_TPREL_LO);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001611 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1612 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1613 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001614 }
1615
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001616 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
1617 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001618}
1619
1620SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001621lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +00001622{
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001623 JumpTableSDNode *N = cast<JumpTableSDNode>(Op);
1624 EVT Ty = Op.getValueType();
Akira Hatanaka30f97cf2013-09-25 00:30:25 +00001625
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001626 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
1627 return getAddrNonPIC(N, Ty, DAG);
1628
1629 return getAddrLocal(N, Ty, DAG, HasMips64);
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +00001630}
1631
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001632SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001633lowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +00001634{
Bruno Cardoso Lopesfdb4cec2008-07-23 16:01:50 +00001635 // gp_rel relocation
Wesley Peck527da1b2010-11-23 03:31:01 +00001636 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001637 // but the asm printer currently doesn't support this feature without
Wesley Peck527da1b2010-11-23 03:31:01 +00001638 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopes98bda582008-07-28 19:26:25 +00001639 // stuff below.
Eli Friedman57c11da2009-08-03 02:22:28 +00001640 //if (IsInSmallSection(C->getType())) {
Owen Anderson9f944592009-08-11 20:47:22 +00001641 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1642 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peck527da1b2010-11-23 03:31:01 +00001643 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001644 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
1645 EVT Ty = Op.getValueType();
Bruno Cardoso Lopes2db07582009-11-25 12:17:58 +00001646
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001647 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001648 return getAddrNonPIC(N, Ty, DAG);
Bruno Cardoso Lopesfdb4cec2008-07-23 16:01:50 +00001649
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001650 return getAddrLocal(N, Ty, DAG, HasMips64);
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +00001651}
1652
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001653SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman31ae5862010-04-17 14:41:14 +00001654 MachineFunction &MF = DAG.getMachineFunction();
1655 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1656
Andrew Trickef9de2a2013-05-25 02:42:55 +00001657 SDLoc DL(Op);
Dan Gohman31ae5862010-04-17 14:41:14 +00001658 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1659 getPointerTy());
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +00001660
1661 // vastart just stores the address of the VarArgsFrameIndex slot into the
1662 // memory location argument.
1663 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001664 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00001665 MachinePointerInfo(SV), false, false, 0);
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +00001666}
Jia Liuf54f60f2012-02-28 07:46:26 +00001667
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001668static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG,
1669 bool HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001670 EVT TyX = Op.getOperand(0).getValueType();
1671 EVT TyY = Op.getOperand(1).getValueType();
1672 SDValue Const1 = DAG.getConstant(1, MVT::i32);
1673 SDValue Const31 = DAG.getConstant(31, MVT::i32);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001674 SDLoc DL(Op);
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001675 SDValue Res;
1676
1677 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1678 // to i32.
1679 SDValue X = (TyX == MVT::f32) ?
1680 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1681 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1682 Const1);
1683 SDValue Y = (TyY == MVT::f32) ?
1684 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
1685 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
1686 Const1);
1687
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001688 if (HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001689 // ext E, Y, 31, 1 ; extract bit31 of Y
1690 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
1691 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
1692 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
1693 } else {
1694 // sll SllX, X, 1
1695 // srl SrlX, SllX, 1
1696 // srl SrlY, Y, 31
1697 // sll SllY, SrlX, 31
1698 // or Or, SrlX, SllY
1699 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1700 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1701 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
1702 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
1703 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
1704 }
1705
1706 if (TyX == MVT::f32)
1707 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
1708
1709 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1710 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1711 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001712}
1713
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001714static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG,
1715 bool HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001716 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
1717 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
1718 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
1719 SDValue Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001720 SDLoc DL(Op);
Eric Christopher0713a9d2011-06-08 23:55:35 +00001721
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001722 // Bitcast to integer nodes.
1723 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
1724 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001725
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001726 if (HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001727 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
1728 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
1729 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
1730 DAG.getConstant(WidthY - 1, MVT::i32), Const1);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001731
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001732 if (WidthX > WidthY)
1733 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
1734 else if (WidthY > WidthX)
1735 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001736
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001737 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
1738 DAG.getConstant(WidthX - 1, MVT::i32), Const1, X);
1739 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
1740 }
1741
1742 // (d)sll SllX, X, 1
1743 // (d)srl SrlX, SllX, 1
1744 // (d)srl SrlY, Y, width(Y)-1
1745 // (d)sll SllY, SrlX, width(Y)-1
1746 // or Or, SrlX, SllY
1747 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
1748 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
1749 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
1750 DAG.getConstant(WidthY - 1, MVT::i32));
1751
1752 if (WidthX > WidthY)
1753 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
1754 else if (WidthY > WidthX)
1755 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
1756
1757 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
1758 DAG.getConstant(WidthX - 1, MVT::i32));
1759 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
1760 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001761}
1762
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00001763SDValue
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001764MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001765 if (Subtarget->hasMips64())
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001766 return lowerFCOPYSIGN64(Op, DAG, Subtarget->hasExtractInsert());
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001767
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001768 return lowerFCOPYSIGN32(Op, DAG, Subtarget->hasExtractInsert());
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001769}
1770
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001771static SDValue lowerFABS32(SDValue Op, SelectionDAG &DAG,
1772 bool HasExtractInsert) {
Akira Hatanaka7f4c9d12012-04-11 22:49:04 +00001773 SDValue Res, Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001774 SDLoc DL(Op);
Akira Hatanaka7f4c9d12012-04-11 22:49:04 +00001775
1776 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1777 // to i32.
1778 SDValue X = (Op.getValueType() == MVT::f32) ?
1779 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1780 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1781 Const1);
1782
1783 // Clear MSB.
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001784 if (HasExtractInsert)
Akira Hatanaka7f4c9d12012-04-11 22:49:04 +00001785 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32,
1786 DAG.getRegister(Mips::ZERO, MVT::i32),
1787 DAG.getConstant(31, MVT::i32), Const1, X);
1788 else {
1789 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1790 Res = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1791 }
1792
1793 if (Op.getValueType() == MVT::f32)
1794 return DAG.getNode(ISD::BITCAST, DL, MVT::f32, Res);
1795
1796 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1797 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1798 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
1799}
1800
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001801static SDValue lowerFABS64(SDValue Op, SelectionDAG &DAG,
1802 bool HasExtractInsert) {
Akira Hatanaka7f4c9d12012-04-11 22:49:04 +00001803 SDValue Res, Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001804 SDLoc DL(Op);
Akira Hatanaka7f4c9d12012-04-11 22:49:04 +00001805
1806 // Bitcast to integer node.
1807 SDValue X = DAG.getNode(ISD::BITCAST, DL, MVT::i64, Op.getOperand(0));
1808
1809 // Clear MSB.
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001810 if (HasExtractInsert)
Akira Hatanaka7f4c9d12012-04-11 22:49:04 +00001811 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i64,
1812 DAG.getRegister(Mips::ZERO_64, MVT::i64),
1813 DAG.getConstant(63, MVT::i32), Const1, X);
1814 else {
1815 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i64, X, Const1);
1816 Res = DAG.getNode(ISD::SRL, DL, MVT::i64, SllX, Const1);
1817 }
1818
1819 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, Res);
1820}
1821
1822SDValue
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001823MipsTargetLowering::lowerFABS(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka7f4c9d12012-04-11 22:49:04 +00001824 if (Subtarget->hasMips64() && (Op.getValueType() == MVT::f64))
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001825 return lowerFABS64(Op, DAG, Subtarget->hasExtractInsert());
Akira Hatanaka7f4c9d12012-04-11 22:49:04 +00001826
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001827 return lowerFABS32(Op, DAG, Subtarget->hasExtractInsert());
Akira Hatanaka7f4c9d12012-04-11 22:49:04 +00001828}
1829
Akira Hatanaka66277522011-06-02 00:24:44 +00001830SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001831lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes5444a7b2011-06-16 00:40:02 +00001832 // check the depth
1833 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka15506782011-06-07 18:58:42 +00001834 "Frame address can only be determined for current frame.");
Akira Hatanaka66277522011-06-02 00:24:44 +00001835
1836 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1837 MFI->setFrameAddressIsTaken(true);
1838 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001839 SDLoc DL(Op);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001840 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), DL,
Akira Hatanaka9189d712011-11-11 04:11:56 +00001841 IsN64 ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka66277522011-06-02 00:24:44 +00001842 return FrameAddr;
1843}
1844
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001845SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op,
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00001846 SelectionDAG &DAG) const {
Bill Wendling908bf812014-01-06 00:43:20 +00001847 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001848 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001849
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00001850 // check the depth
1851 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
1852 "Return address can be determined only for current frame.");
1853
1854 MachineFunction &MF = DAG.getMachineFunction();
1855 MachineFrameInfo *MFI = MF.getFrameInfo();
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001856 MVT VT = Op.getSimpleValueType();
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00001857 unsigned RA = IsN64 ? Mips::RA_64 : Mips::RA;
1858 MFI->setReturnAddressIsTaken(true);
1859
1860 // Return RA, which contains the return address. Mark it an implicit live-in.
1861 unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00001862 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT);
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00001863}
1864
Akira Hatanakac0b02062013-01-30 00:26:49 +00001865// An EH_RETURN is the result of lowering llvm.eh.return which in turn is
1866// generated from __builtin_eh_return (offset, handler)
1867// The effect of this is to adjust the stack pointer by "offset"
1868// and then branch to "handler".
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001869SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Akira Hatanakac0b02062013-01-30 00:26:49 +00001870 const {
1871 MachineFunction &MF = DAG.getMachineFunction();
1872 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1873
1874 MipsFI->setCallsEhReturn();
1875 SDValue Chain = Op.getOperand(0);
1876 SDValue Offset = Op.getOperand(1);
1877 SDValue Handler = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001878 SDLoc DL(Op);
Akira Hatanakac0b02062013-01-30 00:26:49 +00001879 EVT Ty = IsN64 ? MVT::i64 : MVT::i32;
1880
1881 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
1882 // EH_RETURN nodes, so that instructions are emitted back-to-back.
1883 unsigned OffsetReg = IsN64 ? Mips::V1_64 : Mips::V1;
1884 unsigned AddrReg = IsN64 ? Mips::V0_64 : Mips::V0;
1885 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
1886 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
1887 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
1888 DAG.getRegister(OffsetReg, Ty),
1889 DAG.getRegister(AddrReg, getPointerTy()),
1890 Chain.getValue(1));
1891}
1892
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001893SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
Akira Hatanaka5fd22482012-06-14 21:10:56 +00001894 SelectionDAG &DAG) const {
Eli Friedman26a48482011-07-27 22:21:52 +00001895 // FIXME: Need pseudo-fence for 'singlethread' fences
1896 // FIXME: Set SType for weaker fences where supported/appropriate.
1897 unsigned SType = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001898 SDLoc DL(Op);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001899 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
Eli Friedman26a48482011-07-27 22:21:52 +00001900 DAG.getConstant(SType, MVT::i32));
1901}
1902
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001903SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
Akira Hatanaka5fd22482012-06-14 21:10:56 +00001904 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001905 SDLoc DL(Op);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00001906 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
1907 SDValue Shamt = Op.getOperand(2);
1908
1909 // if shamt < 32:
1910 // lo = (shl lo, shamt)
1911 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
1912 // else:
1913 // lo = 0
1914 // hi = (shl lo, shamt[4:0])
1915 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
1916 DAG.getConstant(-1, MVT::i32));
1917 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo,
1918 DAG.getConstant(1, MVT::i32));
1919 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, ShiftRight1Lo,
1920 Not);
1921 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi, Shamt);
1922 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
1923 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, MVT::i32, Lo, Shamt);
1924 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
1925 DAG.getConstant(0x20, MVT::i32));
Akira Hatanaka5fd22482012-06-14 21:10:56 +00001926 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
1927 DAG.getConstant(0, MVT::i32), ShiftLeftLo);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00001928 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftLeftLo, Or);
1929
1930 SDValue Ops[2] = {Lo, Hi};
1931 return DAG.getMergeValues(Ops, 2, DL);
1932}
1933
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001934SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00001935 bool IsSRA) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001936 SDLoc DL(Op);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00001937 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
1938 SDValue Shamt = Op.getOperand(2);
1939
1940 // if shamt < 32:
1941 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
1942 // if isSRA:
1943 // hi = (sra hi, shamt)
1944 // else:
1945 // hi = (srl hi, shamt)
1946 // else:
1947 // if isSRA:
1948 // lo = (sra hi, shamt[4:0])
1949 // hi = (sra hi, 31)
1950 // else:
1951 // lo = (srl hi, shamt[4:0])
1952 // hi = 0
1953 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
1954 DAG.getConstant(-1, MVT::i32));
1955 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi,
1956 DAG.getConstant(1, MVT::i32));
1957 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, ShiftLeft1Hi, Not);
1958 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo, Shamt);
1959 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
1960 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL, DL, MVT::i32,
1961 Hi, Shamt);
1962 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
1963 DAG.getConstant(0x20, MVT::i32));
1964 SDValue Shift31 = DAG.getNode(ISD::SRA, DL, MVT::i32, Hi,
1965 DAG.getConstant(31, MVT::i32));
1966 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftRightHi, Or);
1967 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
1968 IsSRA ? Shift31 : DAG.getConstant(0, MVT::i32),
1969 ShiftRightHi);
1970
1971 SDValue Ops[2] = {Lo, Hi};
1972 return DAG.getMergeValues(Ops, 2, DL);
1973}
1974
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00001975static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00001976 SDValue Chain, SDValue Src, unsigned Offset) {
Akira Hatanaka95866182012-06-13 19:06:08 +00001977 SDValue Ptr = LD->getBasePtr();
Akira Hatanaka8f1db772012-06-02 00:03:49 +00001978 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
Akira Hatanaka95866182012-06-13 19:06:08 +00001979 EVT BasePtrVT = Ptr.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001980 SDLoc DL(LD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00001981 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
1982
1983 if (Offset)
Akira Hatanaka95866182012-06-13 19:06:08 +00001984 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00001985 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka8f1db772012-06-02 00:03:49 +00001986
1987 SDValue Ops[] = { Chain, Ptr, Src };
1988 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT,
1989 LD->getMemOperand());
1990}
1991
1992// Expand an unaligned 32 or 64-bit integer load node.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001993SDValue MipsTargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka8f1db772012-06-02 00:03:49 +00001994 LoadSDNode *LD = cast<LoadSDNode>(Op);
1995 EVT MemVT = LD->getMemoryVT();
1996
1997 // Return if load is aligned or if MemVT is neither i32 nor i64.
1998 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
1999 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
2000 return SDValue();
2001
2002 bool IsLittle = Subtarget->isLittle();
2003 EVT VT = Op.getValueType();
2004 ISD::LoadExtType ExtType = LD->getExtensionType();
2005 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
2006
2007 assert((VT == MVT::i32) || (VT == MVT::i64));
2008
2009 // Expand
2010 // (set dst, (i64 (load baseptr)))
2011 // to
2012 // (set tmp, (ldl (add baseptr, 7), undef))
2013 // (set dst, (ldr baseptr, tmp))
2014 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002015 SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002016 IsLittle ? 7 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002017 return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002018 IsLittle ? 0 : 7);
2019 }
2020
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002021 SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002022 IsLittle ? 3 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002023 SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002024 IsLittle ? 0 : 3);
2025
2026 // Expand
2027 // (set dst, (i32 (load baseptr))) or
2028 // (set dst, (i64 (sextload baseptr))) or
2029 // (set dst, (i64 (extload baseptr)))
2030 // to
2031 // (set tmp, (lwl (add baseptr, 3), undef))
2032 // (set dst, (lwr baseptr, tmp))
2033 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
2034 (ExtType == ISD::EXTLOAD))
2035 return LWR;
2036
2037 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
2038
2039 // Expand
2040 // (set dst, (i64 (zextload baseptr)))
2041 // to
2042 // (set tmp0, (lwl (add baseptr, 3), undef))
2043 // (set tmp1, (lwr baseptr, tmp0))
2044 // (set tmp2, (shl tmp1, 32))
2045 // (set dst, (srl tmp2, 32))
Andrew Trickef9de2a2013-05-25 02:42:55 +00002046 SDLoc DL(LD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002047 SDValue Const32 = DAG.getConstant(32, MVT::i32);
2048 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
Akira Hatanaka67346852012-06-04 17:46:29 +00002049 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
2050 SDValue Ops[] = { SRL, LWR.getValue(1) };
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002051 return DAG.getMergeValues(Ops, 2, DL);
2052}
2053
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002054static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002055 SDValue Chain, unsigned Offset) {
Akira Hatanaka95866182012-06-13 19:06:08 +00002056 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
2057 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002058 SDLoc DL(SD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002059 SDVTList VTList = DAG.getVTList(MVT::Other);
2060
2061 if (Offset)
Akira Hatanaka95866182012-06-13 19:06:08 +00002062 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002063 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002064
2065 SDValue Ops[] = { Chain, Value, Ptr };
2066 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT,
2067 SD->getMemOperand());
2068}
2069
2070// Expand an unaligned 32 or 64-bit integer store node.
Akira Hatanakad82ee942013-05-16 20:45:17 +00002071static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG,
2072 bool IsLittle) {
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002073 SDValue Value = SD->getValue(), Chain = SD->getChain();
2074 EVT VT = Value.getValueType();
2075
2076 // Expand
2077 // (store val, baseptr) or
2078 // (truncstore val, baseptr)
2079 // to
2080 // (swl val, (add baseptr, 3))
2081 // (swr val, baseptr)
2082 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002083 SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002084 IsLittle ? 3 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002085 return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002086 }
2087
2088 assert(VT == MVT::i64);
2089
2090 // Expand
2091 // (store val, baseptr)
2092 // to
2093 // (sdl val, (add baseptr, 7))
2094 // (sdr val, baseptr)
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002095 SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2096 return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002097}
2098
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002099// Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr).
2100static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG) {
2101 SDValue Val = SD->getValue();
2102
2103 if (Val.getOpcode() != ISD::FP_TO_SINT)
2104 return SDValue();
2105
2106 EVT FPTy = EVT::getFloatingPointVT(Val.getValueSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002107 SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy,
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002108 Val.getOperand(0));
2109
Andrew Trickef9de2a2013-05-25 02:42:55 +00002110 return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(),
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002111 SD->getPointerInfo(), SD->isVolatile(),
2112 SD->isNonTemporal(), SD->getAlignment());
2113}
2114
Akira Hatanakad82ee942013-05-16 20:45:17 +00002115SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2116 StoreSDNode *SD = cast<StoreSDNode>(Op);
2117 EVT MemVT = SD->getMemoryVT();
2118
2119 // Lower unaligned integer stores.
2120 if ((SD->getAlignment() < MemVT.getSizeInBits() / 8) &&
2121 ((MemVT == MVT::i32) || (MemVT == MVT::i64)))
2122 return lowerUnalignedIntStore(SD, DAG, Subtarget->isLittle());
2123
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002124 return lowerFP_TO_SINT_STORE(SD, DAG);
Akira Hatanakad82ee942013-05-16 20:45:17 +00002125}
2126
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002127SDValue MipsTargetLowering::lowerADD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002128 if (Op->getOperand(0).getOpcode() != ISD::FRAMEADDR
2129 || cast<ConstantSDNode>
2130 (Op->getOperand(0).getOperand(0))->getZExtValue() != 0
2131 || Op->getOperand(1).getOpcode() != ISD::FRAME_TO_ARGS_OFFSET)
2132 return SDValue();
2133
2134 // The pattern
2135 // (add (frameaddr 0), (frame_to_args_offset))
2136 // results from lowering llvm.eh.dwarf.cfa intrinsic. Transform it to
2137 // (add FrameObject, 0)
2138 // where FrameObject is a fixed StackObject with offset 0 which points to
2139 // the old stack pointer.
2140 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2141 EVT ValTy = Op->getValueType(0);
2142 int FI = MFI->CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
2143 SDValue InArgsAddr = DAG.getFrameIndex(FI, ValTy);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002144 return DAG.getNode(ISD::ADD, SDLoc(Op), ValTy, InArgsAddr,
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002145 DAG.getConstant(0, ValTy));
2146}
2147
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002148SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op,
2149 SelectionDAG &DAG) const {
2150 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002151 SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy,
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002152 Op.getOperand(0));
Andrew Trickef9de2a2013-05-25 02:42:55 +00002153 return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc);
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002154}
2155
Akira Hatanakae2489122011-04-15 21:51:11 +00002156//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002157// Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002158//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002159
Akira Hatanakae2489122011-04-15 21:51:11 +00002160//===----------------------------------------------------------------------===//
Wesley Peck527da1b2010-11-23 03:31:01 +00002161// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002162// Mips O32 ABI rules:
2163// ---
2164// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peck527da1b2010-11-23 03:31:01 +00002165// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002166// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peck527da1b2010-11-23 03:31:01 +00002167// f64 - Only passed in two aliased f32 registers if no int reg has been used
2168// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002169// not used, it must be shadowed. If only A3 is avaiable, shadow it and
2170// go to stack.
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002171//
2172// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanakae2489122011-04-15 21:51:11 +00002173//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002174
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00002175static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2176 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
2177 CCState &State, const uint16_t *F64Regs) {
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002178
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00002179 static const unsigned IntRegsSize = 4, FloatRegsSize = 2;
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002180
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00002181 static const uint16_t IntRegs[] = { Mips::A0, Mips::A1, Mips::A2, Mips::A3 };
2182 static const uint16_t F32Regs[] = { Mips::F12, Mips::F14 };
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002183
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002184 // Do not process byval args here.
2185 if (ArgFlags.isByVal())
2186 return true;
Akira Hatanaka5e16c6a2011-05-24 19:18:33 +00002187
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002188 // Promote i8 and i16
2189 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
2190 LocVT = MVT::i32;
2191 if (ArgFlags.isSExt())
2192 LocInfo = CCValAssign::SExt;
2193 else if (ArgFlags.isZExt())
2194 LocInfo = CCValAssign::ZExt;
2195 else
2196 LocInfo = CCValAssign::AExt;
2197 }
2198
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002199 unsigned Reg;
2200
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002201 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
2202 // is true: function is vararg, argument is 3rd or higher, there is previous
2203 // argument which is not f32 or f64.
2204 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
2205 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanaka9e6a8cc2011-05-19 20:29:48 +00002206 unsigned OrigAlign = ArgFlags.getOrigAlign();
2207 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002208
2209 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002210 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanaka9e6a8cc2011-05-19 20:29:48 +00002211 // If this is the first part of an i64 arg,
2212 // the allocated register must be either A0 or A2.
2213 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
2214 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002215 LocVT = MVT::i32;
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002216 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
2217 // Allocate int register and shadow next int register. If first
2218 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002219 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2220 if (Reg == Mips::A1 || Reg == Mips::A3)
2221 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2222 State.AllocateReg(IntRegs, IntRegsSize);
2223 LocVT = MVT::i32;
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002224 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
2225 // we are guaranteed to find an available float register
2226 if (ValVT == MVT::f32) {
2227 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
2228 // Shadow int register
2229 State.AllocateReg(IntRegs, IntRegsSize);
2230 } else {
2231 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
2232 // Shadow int registers
2233 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
2234 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
2235 State.AllocateReg(IntRegs, IntRegsSize);
2236 State.AllocateReg(IntRegs, IntRegsSize);
2237 }
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002238 } else
2239 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002240
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002241 if (!Reg) {
2242 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3,
2243 OrigAlign);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002244 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002245 } else
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002246 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002247
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002248 return false;
Akira Hatanaka202f6402011-11-12 02:20:46 +00002249}
2250
Akira Hatanakabfb66242013-08-20 23:38:40 +00002251static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT,
2252 MVT LocVT, CCValAssign::LocInfo LocInfo,
2253 ISD::ArgFlagsTy ArgFlags, CCState &State) {
2254 static const uint16_t F64Regs[] = { Mips::D6, Mips::D7 };
2255
2256 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2257}
2258
2259static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT,
2260 MVT LocVT, CCValAssign::LocInfo LocInfo,
2261 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Akira Hatanakad6c9f6e2013-11-12 22:16:18 +00002262 static const uint16_t F64Regs[] = { Mips::D12_64, Mips::D14_64 };
Akira Hatanakabfb66242013-08-20 23:38:40 +00002263
2264 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2265}
2266
Akira Hatanaka202f6402011-11-12 02:20:46 +00002267#include "MipsGenCallingConv.inc"
2268
Akira Hatanakae2489122011-04-15 21:51:11 +00002269//===----------------------------------------------------------------------===//
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002270// Call Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002271//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002272
Akira Hatanaka61bbcce2011-09-23 00:58:33 +00002273// Return next O32 integer argument register.
2274static unsigned getNextIntArgReg(unsigned Reg) {
2275 assert((Reg == Mips::A0) || (Reg == Mips::A2));
2276 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
2277}
2278
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002279SDValue
2280MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002281 SDValue Chain, SDValue Arg, SDLoc DL,
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002282 bool IsTailCall, SelectionDAG &DAG) const {
2283 if (!IsTailCall) {
2284 SDValue PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr,
2285 DAG.getIntPtrConstant(Offset));
2286 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo(), false,
2287 false, 0);
2288 }
2289
2290 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2291 int FI = MFI->CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
2292 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2293 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(),
2294 /*isVolatile=*/ true, false, 0);
2295}
2296
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002297void MipsTargetLowering::
2298getOpndList(SmallVectorImpl<SDValue> &Ops,
2299 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
2300 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
2301 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
2302 // Insert node "GP copy globalreg" before call to function.
2303 //
2304 // R_MIPS_CALL* operators (emitted when non-internal functions are called
2305 // in PIC mode) allow symbols to be resolved via lazy binding.
2306 // The lazy binding stub requires GP to point to the GOT.
2307 if (IsPICCall && !InternalLinkage) {
2308 unsigned GPReg = IsN64 ? Mips::GP_64 : Mips::GP;
2309 EVT Ty = IsN64 ? MVT::i64 : MVT::i32;
2310 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty)));
2311 }
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002312
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002313 // Build a sequence of copy-to-reg nodes chained together with token
2314 // chain and flag operands which copy the outgoing args into registers.
2315 // The InFlag in necessary since all emitted instructions must be
2316 // stuck together.
2317 SDValue InFlag;
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002318
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002319 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2320 Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, RegsToPass[i].first,
2321 RegsToPass[i].second, InFlag);
2322 InFlag = Chain.getValue(1);
2323 }
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002324
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002325 // Add argument registers to the end of the list so that they are
2326 // known live into the call.
2327 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2328 Ops.push_back(CLI.DAG.getRegister(RegsToPass[i].first,
2329 RegsToPass[i].second.getValueType()));
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002330
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002331 // Add a register mask operand representing the call-preserved registers.
2332 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2333 const uint32_t *Mask = TRI->getCallPreservedMask(CLI.CallConv);
2334 assert(Mask && "Missing call preserved mask for calling convention");
Reed Kotler783c7942013-05-10 22:25:39 +00002335 if (Subtarget->inMips16HardFloat()) {
2336 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
2337 llvm::StringRef Sym = G->getGlobal()->getName();
2338 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
Reed Kotler3230e722013-12-12 02:41:11 +00002339 if (F && F->hasFnAttribute("__Mips16RetHelper")) {
Reed Kotler783c7942013-05-10 22:25:39 +00002340 Mask = MipsRegisterInfo::getMips16RetHelperMask();
2341 }
2342 }
2343 }
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002344 Ops.push_back(CLI.DAG.getRegisterMask(Mask));
2345
2346 if (InFlag.getNode())
2347 Ops.push_back(InFlag);
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002348}
2349
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002350/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman624801e2009-01-26 03:15:54 +00002351/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002352SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00002353MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002354 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00002355 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002356 SDLoc DL = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00002357 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
2358 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
2359 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Akira Hatanakabeda2242012-07-31 18:46:41 +00002360 SDValue Chain = CLI.Chain;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002361 SDValue Callee = CLI.Callee;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002362 bool &IsTailCall = CLI.IsTailCall;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002363 CallingConv::ID CallConv = CLI.CallConv;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002364 bool IsVarArg = CLI.IsVarArg;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002365
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002366 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002367 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanaka7c619f12011-05-20 21:39:54 +00002368 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002369 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +00002370 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002371
2372 // Analyze operands of the call, assigning locations to each operand.
2373 SmallVector<CCValAssign, 16> ArgLocs;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002374 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00002375 getTargetMachine(), ArgLocs, *DAG.getContext());
Reed Kotler783c7942013-05-10 22:25:39 +00002376 MipsCC::SpecialCallingConvType SpecialCallingConv =
2377 getSpecialCallingConv(Callee);
Akira Hatanakabfb66242013-08-20 23:38:40 +00002378 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo,
2379 SpecialCallingConv);
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002380
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002381 MipsCCInfo.analyzeCallOperands(Outs, IsVarArg,
Reed Kotlerc03807a2013-08-30 19:40:56 +00002382 Subtarget->mipsSEUsesSoftFloat(),
Akira Hatanaka3b7391d2013-03-05 22:20:28 +00002383 Callee.getNode(), CLI.Args);
Wesley Peck527da1b2010-11-23 03:31:01 +00002384
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002385 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka195a1e22011-06-08 17:39:33 +00002386 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka97ba7692012-07-26 23:27:01 +00002387
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002388 // Check if it's really possible to do a tail call.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002389 if (IsTailCall)
2390 IsTailCall =
2391 isEligibleForTailCallOptimization(MipsCCInfo, NextStackOffset,
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002392 *MF.getInfo<MipsFunctionInfo>());
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002393
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002394 if (IsTailCall)
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002395 ++NumTailCalls;
2396
Akira Hatanaka79738332011-09-19 20:26:02 +00002397 // Chain is the output chain of the last Load/Store or CopyToReg node.
2398 // ByValChain is the output chain of the last Memcpy node created for copying
2399 // byval arguments to the stack.
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002400 unsigned StackAlignment = TFL->getStackAlignment();
2401 NextStackOffset = RoundUpToAlignment(NextStackOffset, StackAlignment);
Akira Hatanaka79738332011-09-19 20:26:02 +00002402 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002403
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002404 if (!IsTailCall)
Andrew Trickad6d08a2013-05-29 22:03:55 +00002405 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffsetVal, DL);
Akira Hatanakabeda2242012-07-31 18:46:41 +00002406
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002407 SDValue StackPtr = DAG.getCopyFromReg(Chain, DL,
Akira Hatanakabeda2242012-07-31 18:46:41 +00002408 IsN64 ? Mips::SP_64 : Mips::SP,
2409 getPointerTy());
Akira Hatanaka195a1e22011-06-08 17:39:33 +00002410
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002411 // With EABI is it possible to have 16 args on registers.
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002412 std::deque< std::pair<unsigned, SDValue> > RegsToPass;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002413 SmallVector<SDValue, 8> MemOpChains;
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002414 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002415
2416 // Walk the register/memloc assignments, inserting copies/loads.
2417 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002418 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002419 CCValAssign &VA = ArgLocs[i];
Akira Hatanakab20a3252011-10-28 19:49:00 +00002420 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka19891f82011-11-12 02:34:50 +00002421 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2422
2423 // ByVal Arg.
2424 if (Flags.isByVal()) {
2425 assert(Flags.getByValSize() &&
2426 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002427 assert(ByValArg != MipsCCInfo.byval_end());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002428 assert(!IsTailCall &&
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002429 "Do not tail-call optimize if there is a byval argument.");
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002430 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002431 MipsCCInfo, *ByValArg, Flags, Subtarget->isLittle());
2432 ++ByValArg;
Akira Hatanaka19891f82011-11-12 02:34:50 +00002433 continue;
2434 }
Jia Liuf54f60f2012-02-28 07:46:26 +00002435
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002436 // Promote the value if needed.
2437 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002438 default: llvm_unreachable("Unknown loc info!");
Wesley Peck527da1b2010-11-23 03:31:01 +00002439 case CCValAssign::Full:
Akira Hatanakab20a3252011-10-28 19:49:00 +00002440 if (VA.isRegLoc()) {
2441 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
Akira Hatanaka3b7391d2013-03-05 22:20:28 +00002442 (ValVT == MVT::f64 && LocVT == MVT::i64) ||
2443 (ValVT == MVT::i64 && LocVT == MVT::f64))
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002444 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
Akira Hatanakab20a3252011-10-28 19:49:00 +00002445 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002446 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanakae2489122011-04-15 21:51:11 +00002447 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002448 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanakaaef55c82011-04-15 21:00:26 +00002449 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka27916972011-04-15 19:52:08 +00002450 if (!Subtarget->isLittle())
2451 std::swap(Lo, Hi);
Jia Liuf54f60f2012-02-28 07:46:26 +00002452 unsigned LocRegLo = VA.getLocReg();
Akira Hatanaka61bbcce2011-09-23 00:58:33 +00002453 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2454 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2455 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002456 continue;
Wesley Peck527da1b2010-11-23 03:31:01 +00002457 }
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002458 }
2459 break;
Chris Lattner52f16de2008-03-17 06:57:02 +00002460 case CCValAssign::SExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002461 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002462 break;
2463 case CCValAssign::ZExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002464 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002465 break;
2466 case CCValAssign::AExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002467 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002468 break;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002469 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002470
2471 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +00002472 // RegsToPass vector
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002473 if (VA.isRegLoc()) {
2474 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattner52f16de2008-03-17 06:57:02 +00002475 continue;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002476 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002477
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002478 // Register can't get to this point...
Chris Lattner52f16de2008-03-17 06:57:02 +00002479 assert(VA.isMemLoc());
Wesley Peck527da1b2010-11-23 03:31:01 +00002480
Wesley Peck527da1b2010-11-23 03:31:01 +00002481 // emit ISD::STORE whichs stores the
Chris Lattner52f16de2008-03-17 06:57:02 +00002482 // parameter value to a stack Location
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002483 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002484 Chain, Arg, DL, IsTailCall, DAG));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002485 }
2486
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002487 // Transform all store nodes into one single node because all store
2488 // nodes are independent of each other.
Wesley Peck527da1b2010-11-23 03:31:01 +00002489 if (!MemOpChains.empty())
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002490 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002491 &MemOpChains[0], MemOpChains.size());
2492
Bill Wendling24c79f22008-09-16 21:48:12 +00002493 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peck527da1b2010-11-23 03:31:01 +00002494 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2495 // node so that legalize doesn't hack it.
Akira Hatanakab20a3252011-10-28 19:49:00 +00002496 bool IsPICCall = (IsN64 || IsPIC); // true if calls are translated to jalr $25
Akira Hatanakacf9a61b2012-12-13 03:17:29 +00002497 bool GlobalOrExternal = false, InternalLinkage = false;
Akira Hatanakad6f1c582011-04-07 19:51:44 +00002498 SDValue CalleeLo;
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002499 EVT Ty = Callee.getValueType();
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002500
2501 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002502 if (IsPICCall) {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002503 const GlobalValue *Val = G->getGlobal();
2504 InternalLinkage = Val->hasInternalLinkage();
Akira Hatanakacf9a61b2012-12-13 03:17:29 +00002505
2506 if (InternalLinkage)
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002507 Callee = getAddrLocal(G, Ty, DAG, HasMips64);
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00002508 else if (LargeGOT)
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002509 Callee = getAddrGlobalLargeGOT(G, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002510 MipsII::MO_CALL_LO16, Chain,
2511 FuncInfo->callPtrInfo(Val));
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002512 else
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002513 Callee = getAddrGlobal(G, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
2514 FuncInfo->callPtrInfo(Val));
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002515 } else
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002516 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, getPointerTy(), 0,
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002517 MipsII::MO_NO_FLAG);
Akira Hatanaka8e16aac2011-12-09 01:45:12 +00002518 GlobalOrExternal = true;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002519 }
2520 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002521 const char *Sym = S->getSymbol();
2522
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00002523 if (!IsN64 && !IsPIC) // !N64 && static
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002524 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(),
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002525 MipsII::MO_NO_FLAG);
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00002526 else if (LargeGOT)
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002527 Callee = getAddrGlobalLargeGOT(S, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002528 MipsII::MO_CALL_LO16, Chain,
2529 FuncInfo->callPtrInfo(Sym));
Akira Hatanaka02b0e482013-02-22 21:10:03 +00002530 else // N64 || PIC
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002531 Callee = getAddrGlobal(S, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
2532 FuncInfo->callPtrInfo(Sym));
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002533
Akira Hatanaka8e16aac2011-12-09 01:45:12 +00002534 GlobalOrExternal = true;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002535 }
2536
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002537 SmallVector<SDValue, 8> Ops(1, Chain);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002538 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002539
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002540 getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal, InternalLinkage,
2541 CLI, Callee, Chain);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002542
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002543 if (IsTailCall)
2544 return DAG.getNode(MipsISD::TailCall, DL, MVT::Other, &Ops[0], Ops.size());
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002545
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002546 Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, &Ops[0], Ops.size());
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002547 SDValue InFlag = Chain.getValue(1);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002548
Bruno Cardoso Lopes193e64c2010-01-30 18:32:07 +00002549 // Create the CALLSEQ_END node.
Akira Hatanaka97ba7692012-07-26 23:27:01 +00002550 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal,
Andrew Trickad6d08a2013-05-29 22:03:55 +00002551 DAG.getIntPtrConstant(0, true), InFlag, DL);
Bruno Cardoso Lopes193e64c2010-01-30 18:32:07 +00002552 InFlag = Chain.getValue(1);
2553
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002554 // Handle result values, copying them out of physregs into vregs that we
2555 // return.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002556 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg,
2557 Ins, DL, DAG, InVals, CLI.Callee.getNode(), CLI.RetTy);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002558}
2559
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002560/// LowerCallResult - Lower the result values of a call into the
2561/// appropriate copies out of appropriate physical registers.
2562SDValue
2563MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002564 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002565 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002566 SDLoc DL, SelectionDAG &DAG,
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002567 SmallVectorImpl<SDValue> &InVals,
2568 const SDNode *CallNode,
2569 const Type *RetTy) const {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002570 // Assign locations to each value returned by this call.
2571 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002572 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka5fd22482012-06-14 21:10:56 +00002573 getTargetMachine(), RVLocs, *DAG.getContext());
Akira Hatanakabfb66242013-08-20 23:38:40 +00002574 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo);
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002575
Reed Kotlerc03807a2013-08-30 19:40:56 +00002576 MipsCCInfo.analyzeCallResult(Ins, Subtarget->mipsSEUsesSoftFloat(),
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002577 CallNode, RetTy);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002578
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002579 // Copy all of the result registers out of their specified physreg.
2580 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002581 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(),
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002582 RVLocs[i].getLocVT(), InFlag);
2583 Chain = Val.getValue(1);
2584 InFlag = Val.getValue(2);
2585
2586 if (RVLocs[i].getValVT() != RVLocs[i].getLocVT())
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002587 Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getValVT(), Val);
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002588
2589 InVals.push_back(Val);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002590 }
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +00002591
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002592 return Chain;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002593}
2594
Akira Hatanakae2489122011-04-15 21:51:11 +00002595//===----------------------------------------------------------------------===//
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002596// Formal Arguments Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002597//===----------------------------------------------------------------------===//
Wesley Peck527da1b2010-11-23 03:31:01 +00002598/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002599/// and generate load operations for arguments places on the stack.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002600SDValue
2601MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanakaaef55c82011-04-15 21:00:26 +00002602 CallingConv::ID CallConv,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002603 bool IsVarArg,
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00002604 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002605 SDLoc DL, SelectionDAG &DAG,
Akira Hatanakaaef55c82011-04-15 21:00:26 +00002606 SmallVectorImpl<SDValue> &InVals)
Akira Hatanakae2489122011-04-15 21:51:11 +00002607 const {
Bruno Cardoso Lopesa01ede22008-08-04 07:12:52 +00002608 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002609 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopes14033fb2007-08-28 05:08:16 +00002610 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002611
Dan Gohman31ae5862010-04-17 14:41:14 +00002612 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002613
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002614 // Used with vargs to acumulate store chains.
2615 std::vector<SDValue> OutChains;
2616
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002617 // Assign locations to all of the incoming arguments.
2618 SmallVector<CCValAssign, 16> ArgLocs;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002619 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00002620 getTargetMachine(), ArgLocs, *DAG.getContext());
Akira Hatanakabfb66242013-08-20 23:38:40 +00002621 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo);
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002622 Function::const_arg_iterator FuncArg =
2623 DAG.getMachineFunction().getFunction()->arg_begin();
Reed Kotlerc03807a2013-08-30 19:40:56 +00002624 bool UseSoftFloat = Subtarget->mipsSEUsesSoftFloat();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002625
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002626 MipsCCInfo.analyzeFormalArguments(Ins, UseSoftFloat, FuncArg);
Akira Hatanaka4866fe12012-10-30 19:37:25 +00002627 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(),
2628 MipsCCInfo.hasByValArg());
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002629
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00002630 unsigned CurArgIdx = 0;
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002631 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002632
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00002633 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002634 CCValAssign &VA = ArgLocs[i];
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00002635 std::advance(FuncArg, Ins[i].OrigArgIndex - CurArgIdx);
2636 CurArgIdx = Ins[i].OrigArgIndex;
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002637 EVT ValVT = VA.getValVT();
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002638 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2639 bool IsRegLoc = VA.isRegLoc();
2640
2641 if (Flags.isByVal()) {
2642 assert(Flags.getByValSize() &&
2643 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002644 assert(ByValArg != MipsCCInfo.byval_end());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002645 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg,
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002646 MipsCCInfo, *ByValArg);
2647 ++ByValArg;
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002648 continue;
2649 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002650
2651 // Arguments stored on registers
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002652 if (IsRegLoc) {
Akira Hatanaka7d822522013-10-28 21:21:36 +00002653 MVT RegVT = VA.getLocVT();
Akira Hatanakacb4a1a82011-05-24 00:23:52 +00002654 unsigned ArgReg = VA.getLocReg();
Akira Hatanaka7d822522013-10-28 21:21:36 +00002655 const TargetRegisterClass *RC = getRegClassFor(RegVT);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002656
Wesley Peck527da1b2010-11-23 03:31:01 +00002657 // Transform the arguments stored on
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002658 // physical registers into virtual ones
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002659 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC);
2660 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
Wesley Peck527da1b2010-11-23 03:31:01 +00002661
2662 // If this is an 8 or 16-bit value, it has been passed promoted
2663 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002664 // truncate to the right size.
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002665 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattner3c049702009-03-26 05:28:14 +00002666 unsigned Opcode = 0;
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002667 if (VA.getLocInfo() == CCValAssign::SExt)
2668 Opcode = ISD::AssertSext;
2669 else if (VA.getLocInfo() == CCValAssign::ZExt)
2670 Opcode = ISD::AssertZext;
Chris Lattner3c049702009-03-26 05:28:14 +00002671 if (Opcode)
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002672 ArgValue = DAG.getNode(Opcode, DL, RegVT, ArgValue,
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002673 DAG.getValueType(ValVT));
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002674 ArgValue = DAG.getNode(ISD::TRUNCATE, DL, ValVT, ArgValue);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002675 }
2676
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002677 // Handle floating point arguments passed in integer registers and
2678 // long double arguments passed in floating point registers.
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002679 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002680 (RegVT == MVT::i64 && ValVT == MVT::f64) ||
2681 (RegVT == MVT::f64 && ValVT == MVT::i64))
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002682 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002683 else if (IsO32 && RegVT == MVT::i32 && ValVT == MVT::f64) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002684 unsigned Reg2 = addLiveIn(DAG.getMachineFunction(),
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002685 getNextIntArgReg(ArgReg), RC);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002686 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT);
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002687 if (!Subtarget->isLittle())
2688 std::swap(ArgValue, ArgValue2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002689 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64,
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002690 ArgValue, ArgValue2);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002691 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002692
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002693 InVals.push_back(ArgValue);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002694 } else { // VA.isRegLoc()
2695
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002696 // sanity check
2697 assert(VA.isMemLoc());
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002698
Wesley Peck527da1b2010-11-23 03:31:01 +00002699 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002700 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakacb4a1a82011-05-24 00:23:52 +00002701 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002702
2703 // Create load nodes to retrieve arguments from the stack
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002704 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Akira Hatanakad1c58ed2013-11-09 02:38:51 +00002705 SDValue Load = DAG.getLoad(ValVT, DL, Chain, FIN,
2706 MachinePointerInfo::getFixedStack(FI),
2707 false, false, false, 0);
2708 InVals.push_back(Load);
2709 OutChains.push_back(Load.getValue(1));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002710 }
2711 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002712
2713 // The mips ABIs for returning structs by value requires that we copy
2714 // the sret argument into $v0 for the return. Save the argument into
2715 // a virtual register so that we can access it from the return points.
2716 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2717 unsigned Reg = MipsFI->getSRetReturnReg();
2718 if (!Reg) {
Akira Hatanaka0c7d1312012-10-19 22:11:40 +00002719 Reg = MF.getRegInfo().
2720 createVirtualRegister(getRegClassFor(IsN64 ? MVT::i64 : MVT::i32));
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002721 MipsFI->setSRetReturnReg(Reg);
2722 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002723 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[0]);
2724 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002725 }
2726
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002727 if (IsVarArg)
2728 writeVarArgRegs(OutChains, MipsCCInfo, Chain, DL, DAG);
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002729
Wesley Peck527da1b2010-11-23 03:31:01 +00002730 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002731 // the size of Ins and InVals. This only happens when on varg functions
2732 if (!OutChains.empty()) {
2733 OutChains.push_back(Chain);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002734 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002735 &OutChains[0], OutChains.size());
2736 }
2737
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002738 return Chain;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002739}
2740
Akira Hatanakae2489122011-04-15 21:51:11 +00002741//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002742// Return Value Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002743//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002744
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00002745bool
2746MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002747 MachineFunction &MF, bool IsVarArg,
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00002748 const SmallVectorImpl<ISD::OutputArg> &Outs,
2749 LLVMContext &Context) const {
2750 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002751 CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(),
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00002752 RVLocs, Context);
2753 return CCInfo.CheckReturn(Outs, RetCC_Mips);
2754}
2755
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002756SDValue
2757MipsTargetLowering::LowerReturn(SDValue Chain,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002758 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002759 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002760 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002761 SDLoc DL, SelectionDAG &DAG) const {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002762 // CCValAssign - represent the assignment of
2763 // the return value to a location
2764 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002765 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002766
2767 // CCState - Info about the registers and stack slot.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002768 CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(), RVLocs,
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002769 *DAG.getContext());
Akira Hatanakabfb66242013-08-20 23:38:40 +00002770 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002771
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002772 // Analyze return values.
Reed Kotlerc03807a2013-08-30 19:40:56 +00002773 MipsCCInfo.analyzeReturn(Outs, Subtarget->mipsSEUsesSoftFloat(),
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002774 MF.getFunction()->getReturnType());
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002775
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002776 SDValue Flag;
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00002777 SmallVector<SDValue, 4> RetOps(1, Chain);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002778
2779 // Copy the result values into the output registers.
2780 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002781 SDValue Val = OutVals[i];
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002782 CCValAssign &VA = RVLocs[i];
2783 assert(VA.isRegLoc() && "Can only return in registers!");
2784
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002785 if (RVLocs[i].getValVT() != RVLocs[i].getLocVT())
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002786 Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getLocVT(), Val);
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002787
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002788 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002789
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00002790 // Guarantee that all emitted copies are stuck together with flags.
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002791 Flag = Chain.getValue(1);
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00002792 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002793 }
2794
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002795 // The mips ABIs for returning structs by value requires that we copy
2796 // the sret argument into $v0 for the return. We saved the argument into
2797 // a virtual register in the entry block, so now we copy the value out
2798 // and into $v0.
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002799 if (MF.getFunction()->hasStructRetAttr()) {
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002800 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2801 unsigned Reg = MipsFI->getSRetReturnReg();
2802
Wesley Peck527da1b2010-11-23 03:31:01 +00002803 if (!Reg)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002804 llvm_unreachable("sret virtual register not created in the entry block");
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002805 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Akira Hatanaka868b3a32012-10-24 02:10:54 +00002806 unsigned V0 = IsN64 ? Mips::V0_64 : Mips::V0;
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002807
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002808 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002809 Flag = Chain.getValue(1);
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00002810 RetOps.push_back(DAG.getRegister(V0, getPointerTy()));
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002811 }
2812
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00002813 RetOps[0] = Chain; // Update chain.
Akira Hatanakaefff7b72012-07-10 00:19:06 +00002814
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00002815 // Add the flag if we have it.
2816 if (Flag.getNode())
2817 RetOps.push_back(Flag);
2818
2819 // Return on Mips is always a "jr $ra"
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002820 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, &RetOps[0], RetOps.size());
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002821}
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002822
Akira Hatanakae2489122011-04-15 21:51:11 +00002823//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002824// Mips Inline Assembly Support
Akira Hatanakae2489122011-04-15 21:51:11 +00002825//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002826
2827/// getConstraintType - Given a constraint letter, return the type of
2828/// constraint it is for this target.
2829MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peck527da1b2010-11-23 03:31:01 +00002830getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002831{
Daniel Sanders8b59af12013-11-12 12:56:01 +00002832 // Mips specific constraints
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002833 // GCC config/mips/constraints.md
2834 //
Wesley Peck527da1b2010-11-23 03:31:01 +00002835 // 'd' : An address register. Equivalent to r
2836 // unless generating MIPS16 code.
2837 // 'y' : Equivalent to r; retained for
2838 // backwards compatibility.
Eric Christophere3c494d2012-05-07 06:25:10 +00002839 // 'c' : A register suitable for use in an indirect
2840 // jump. This will always be $25 for -mabicalls.
Eric Christopher0d8c15d2012-05-07 06:25:19 +00002841 // 'l' : The lo register. 1 word storage.
2842 // 'x' : The hilo register pair. Double word storage.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002843 if (Constraint.size() == 1) {
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002844 switch (Constraint[0]) {
2845 default : break;
Wesley Peck527da1b2010-11-23 03:31:01 +00002846 case 'd':
2847 case 'y':
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002848 case 'f':
Eric Christophere3c494d2012-05-07 06:25:10 +00002849 case 'c':
Eric Christopher9c492e62012-05-07 06:25:15 +00002850 case 'l':
Eric Christopher0d8c15d2012-05-07 06:25:19 +00002851 case 'x':
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002852 return C_RegisterClass;
Jack Carter0e149b02013-03-04 21:33:15 +00002853 case 'R':
2854 return C_Memory;
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002855 }
2856 }
2857 return TargetLowering::getConstraintType(Constraint);
2858}
2859
John Thompsone8360b72010-10-29 17:29:13 +00002860/// Examine constraint type and operand type and determine a weight value.
2861/// This object must already have been set up with the operand type
2862/// and the current alternative constraint selected.
2863TargetLowering::ConstraintWeight
2864MipsTargetLowering::getSingleConstraintMatchWeight(
2865 AsmOperandInfo &info, const char *constraint) const {
2866 ConstraintWeight weight = CW_Invalid;
2867 Value *CallOperandVal = info.CallOperandVal;
2868 // If we don't have a value, we can't do a match,
2869 // but allow it at the lowest weight.
2870 if (CallOperandVal == NULL)
2871 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +00002872 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +00002873 // Look at the constraint type.
2874 switch (*constraint) {
2875 default:
2876 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2877 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00002878 case 'd':
2879 case 'y':
John Thompsone8360b72010-10-29 17:29:13 +00002880 if (type->isIntegerTy())
2881 weight = CW_Register;
2882 break;
Daniel Sanders8b59af12013-11-12 12:56:01 +00002883 case 'f': // FPU or MSA register
2884 if (Subtarget->hasMSA() && type->isVectorTy() &&
2885 cast<VectorType>(type)->getBitWidth() == 128)
2886 weight = CW_Register;
2887 else if (type->isFloatTy())
John Thompsone8360b72010-10-29 17:29:13 +00002888 weight = CW_Register;
2889 break;
Eric Christophere3c494d2012-05-07 06:25:10 +00002890 case 'c': // $25 for indirect jumps
Eric Christopher9c492e62012-05-07 06:25:15 +00002891 case 'l': // lo register
Eric Christopher0d8c15d2012-05-07 06:25:19 +00002892 case 'x': // hilo register pair
Daniel Sanders8b59af12013-11-12 12:56:01 +00002893 if (type->isIntegerTy())
Eric Christophere3c494d2012-05-07 06:25:10 +00002894 weight = CW_SpecificReg;
Daniel Sanders8b59af12013-11-12 12:56:01 +00002895 break;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00002896 case 'I': // signed 16 bit immediate
Eric Christopher7201e1b2012-05-07 03:13:42 +00002897 case 'J': // integer zero
Eric Christopher3ff88a02012-05-07 05:46:29 +00002898 case 'K': // unsigned 16 bit immediate
Eric Christopher1109b342012-05-07 05:46:37 +00002899 case 'L': // signed 32 bit immediate where lower 16 bits are 0
Eric Christophere07aa432012-05-07 05:46:43 +00002900 case 'N': // immediate in the range of -65535 to -1 (inclusive)
Eric Christopher470578a2012-05-07 05:46:48 +00002901 case 'O': // signed 15 bit immediate (+- 16383)
Eric Christopherc18ae4a2012-05-07 06:25:02 +00002902 case 'P': // immediate in the range of 65535 to 1 (inclusive)
Eric Christopher1d6c89e2012-05-07 03:13:32 +00002903 if (isa<ConstantInt>(CallOperandVal))
2904 weight = CW_Constant;
2905 break;
Jack Carter0e149b02013-03-04 21:33:15 +00002906 case 'R':
2907 weight = CW_Memory;
2908 break;
John Thompsone8360b72010-10-29 17:29:13 +00002909 }
2910 return weight;
2911}
2912
Akira Hatanaka7473b472013-08-14 00:21:25 +00002913/// This is a helper function to parse a physical register string and split it
2914/// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag
2915/// that is returned indicates whether parsing was successful. The second flag
2916/// is true if the numeric part exists.
2917static std::pair<bool, bool>
2918parsePhysicalReg(const StringRef &C, std::string &Prefix,
2919 unsigned long long &Reg) {
2920 if (C.front() != '{' || C.back() != '}')
2921 return std::make_pair(false, false);
2922
2923 // Search for the first numeric character.
2924 StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1;
2925 I = std::find_if(B, E, std::ptr_fun(isdigit));
2926
2927 Prefix.assign(B, I - B);
2928
2929 // The second flag is set to false if no numeric characters were found.
2930 if (I == E)
2931 return std::make_pair(true, false);
2932
2933 // Parse the numeric characters.
2934 return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg),
2935 true);
2936}
2937
2938std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering::
2939parseRegForInlineAsmConstraint(const StringRef &C, MVT VT) const {
2940 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2941 const TargetRegisterClass *RC;
2942 std::string Prefix;
2943 unsigned long long Reg;
2944
2945 std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg);
2946
2947 if (!R.first)
2948 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2949
2950 if ((Prefix == "hi" || Prefix == "lo")) { // Parse hi/lo.
2951 // No numeric characters follow "hi" or "lo".
2952 if (R.second)
2953 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2954
2955 RC = TRI->getRegClass(Prefix == "hi" ?
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00002956 Mips::HI32RegClassID : Mips::LO32RegClassID);
Akira Hatanaka7473b472013-08-14 00:21:25 +00002957 return std::make_pair(*(RC->begin()), RC);
Daniel Sanders8b59af12013-11-12 12:56:01 +00002958 } else if (Prefix.compare(0, 4, "$msa") == 0) {
2959 // Parse $msa(ir|csr|access|save|modify|request|map|unmap)
2960
2961 // No numeric characters follow the name.
2962 if (R.second)
2963 return std::make_pair((unsigned)0, (const TargetRegisterClass *)0);
2964
2965 Reg = StringSwitch<unsigned long long>(Prefix)
2966 .Case("$msair", Mips::MSAIR)
2967 .Case("$msacsr", Mips::MSACSR)
2968 .Case("$msaaccess", Mips::MSAAccess)
2969 .Case("$msasave", Mips::MSASave)
2970 .Case("$msamodify", Mips::MSAModify)
2971 .Case("$msarequest", Mips::MSARequest)
2972 .Case("$msamap", Mips::MSAMap)
2973 .Case("$msaunmap", Mips::MSAUnmap)
2974 .Default(0);
2975
2976 if (!Reg)
2977 return std::make_pair((unsigned)0, (const TargetRegisterClass *)0);
2978
2979 RC = TRI->getRegClass(Mips::MSACtrlRegClassID);
2980 return std::make_pair(Reg, RC);
Akira Hatanaka7473b472013-08-14 00:21:25 +00002981 }
2982
2983 if (!R.second)
2984 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2985
2986 if (Prefix == "$f") { // Parse $f0-$f31.
2987 // If the size of FP registers is 64-bit or Reg is an even number, select
2988 // the 64-bit register class. Otherwise, select the 32-bit register class.
2989 if (VT == MVT::Other)
2990 VT = (Subtarget->isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32;
2991
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00002992 RC = getRegClassFor(VT);
Akira Hatanaka7473b472013-08-14 00:21:25 +00002993
2994 if (RC == &Mips::AFGR64RegClass) {
2995 assert(Reg % 2 == 0);
2996 Reg >>= 1;
2997 }
Daniel Sanders8b59af12013-11-12 12:56:01 +00002998 } else if (Prefix == "$fcc") // Parse $fcc0-$fcc7.
Akira Hatanaka7473b472013-08-14 00:21:25 +00002999 RC = TRI->getRegClass(Mips::FCCRegClassID);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003000 else if (Prefix == "$w") { // Parse $w0-$w31.
3001 RC = getRegClassFor((VT == MVT::Other) ? MVT::v16i8 : VT);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003002 } else { // Parse $0-$31.
3003 assert(Prefix == "$");
3004 RC = getRegClassFor((VT == MVT::Other) ? MVT::i32 : VT);
3005 }
3006
3007 assert(Reg < RC->getNumRegs());
3008 return std::make_pair(*(RC->begin() + Reg), RC);
3009}
3010
Eric Christophereaf77dc2011-06-29 19:33:04 +00003011/// Given a register class constraint, like 'r', if this corresponds directly
3012/// to an LLVM register class, return a register of 0 and the register class
3013/// pointer.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003014std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Chad Rosier295bd432013-06-22 18:37:38 +00003015getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003016{
3017 if (Constraint.size() == 1) {
3018 switch (Constraint[0]) {
Eric Christopher9519c082011-06-29 19:04:31 +00003019 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
3020 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003021 case 'r':
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003022 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
3023 if (Subtarget->inMips16Mode())
3024 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003025 return std::make_pair(0U, &Mips::GPR32RegClass);
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003026 }
Jack Carterb3530942012-07-02 23:35:23 +00003027 if (VT == MVT::i64 && !HasMips64)
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003028 return std::make_pair(0U, &Mips::GPR32RegClass);
Eric Christopher58daf042012-05-07 03:13:22 +00003029 if (VT == MVT::i64 && HasMips64)
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003030 return std::make_pair(0U, &Mips::GPR64RegClass);
Eric Christopher58daf042012-05-07 03:13:22 +00003031 // This will generate an error message
3032 return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
Daniel Sanders8b59af12013-11-12 12:56:01 +00003033 case 'f': // FPU or MSA register
3034 if (VT == MVT::v16i8)
3035 return std::make_pair(0U, &Mips::MSA128BRegClass);
3036 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
3037 return std::make_pair(0U, &Mips::MSA128HRegClass);
3038 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
3039 return std::make_pair(0U, &Mips::MSA128WRegClass);
3040 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
3041 return std::make_pair(0U, &Mips::MSA128DRegClass);
3042 else if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00003043 return std::make_pair(0U, &Mips::FGR32RegClass);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003044 else if ((VT == MVT::f64) && (!Subtarget->isSingleFloat())) {
Akira Hatanakac669d7a2012-01-04 02:45:01 +00003045 if (Subtarget->isFP64bit())
Craig Topperc7242e02012-04-20 07:30:17 +00003046 return std::make_pair(0U, &Mips::FGR64RegClass);
3047 return std::make_pair(0U, &Mips::AFGR64RegClass);
Akira Hatanakac669d7a2012-01-04 02:45:01 +00003048 }
Eric Christophere3c494d2012-05-07 06:25:10 +00003049 break;
3050 case 'c': // register suitable for indirect jump
3051 if (VT == MVT::i32)
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003052 return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass);
Eric Christophere3c494d2012-05-07 06:25:10 +00003053 assert(VT == MVT::i64 && "Unexpected type.");
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003054 return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass);
Eric Christopher9c492e62012-05-07 06:25:15 +00003055 case 'l': // register suitable for indirect jump
3056 if (VT == MVT::i32)
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00003057 return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass);
3058 return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass);
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003059 case 'x': // register suitable for indirect jump
3060 // Fixme: Not triggering the use of both hi and low
3061 // This will generate an error message
3062 return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003063 }
3064 }
Akira Hatanaka7473b472013-08-14 00:21:25 +00003065
3066 std::pair<unsigned, const TargetRegisterClass *> R;
3067 R = parseRegForInlineAsmConstraint(Constraint, VT);
3068
3069 if (R.second)
3070 return R;
3071
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003072 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3073}
3074
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003075/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3076/// vector. If it is invalid, don't add anything to Ops.
3077void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3078 std::string &Constraint,
3079 std::vector<SDValue>&Ops,
3080 SelectionDAG &DAG) const {
3081 SDValue Result(0, 0);
3082
3083 // Only support length 1 constraints for now.
3084 if (Constraint.length() > 1) return;
3085
3086 char ConstraintLetter = Constraint[0];
3087 switch (ConstraintLetter) {
3088 default: break; // This will fall through to the generic implementation
3089 case 'I': // Signed 16 bit constant
3090 // If this fails, the parent routine will give an error
3091 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3092 EVT Type = Op.getValueType();
3093 int64_t Val = C->getSExtValue();
3094 if (isInt<16>(Val)) {
3095 Result = DAG.getTargetConstant(Val, Type);
3096 break;
3097 }
3098 }
3099 return;
Eric Christopher7201e1b2012-05-07 03:13:42 +00003100 case 'J': // integer zero
3101 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3102 EVT Type = Op.getValueType();
3103 int64_t Val = C->getZExtValue();
3104 if (Val == 0) {
3105 Result = DAG.getTargetConstant(0, Type);
3106 break;
3107 }
3108 }
3109 return;
Eric Christopher3ff88a02012-05-07 05:46:29 +00003110 case 'K': // unsigned 16 bit immediate
3111 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3112 EVT Type = Op.getValueType();
3113 uint64_t Val = (uint64_t)C->getZExtValue();
3114 if (isUInt<16>(Val)) {
3115 Result = DAG.getTargetConstant(Val, Type);
3116 break;
3117 }
3118 }
3119 return;
Eric Christopher1109b342012-05-07 05:46:37 +00003120 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3121 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3122 EVT Type = Op.getValueType();
3123 int64_t Val = C->getSExtValue();
3124 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
3125 Result = DAG.getTargetConstant(Val, Type);
3126 break;
3127 }
3128 }
3129 return;
Eric Christophere07aa432012-05-07 05:46:43 +00003130 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3131 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3132 EVT Type = Op.getValueType();
3133 int64_t Val = C->getSExtValue();
3134 if ((Val >= -65535) && (Val <= -1)) {
3135 Result = DAG.getTargetConstant(Val, Type);
3136 break;
3137 }
3138 }
3139 return;
Eric Christopher470578a2012-05-07 05:46:48 +00003140 case 'O': // signed 15 bit immediate
3141 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3142 EVT Type = Op.getValueType();
3143 int64_t Val = C->getSExtValue();
3144 if ((isInt<15>(Val))) {
3145 Result = DAG.getTargetConstant(Val, Type);
3146 break;
3147 }
3148 }
3149 return;
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003150 case 'P': // immediate in the range of 1 to 65535 (inclusive)
3151 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3152 EVT Type = Op.getValueType();
3153 int64_t Val = C->getSExtValue();
3154 if ((Val <= 65535) && (Val >= 1)) {
3155 Result = DAG.getTargetConstant(Val, Type);
3156 break;
3157 }
3158 }
3159 return;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003160 }
3161
3162 if (Result.getNode()) {
3163 Ops.push_back(Result);
3164 return;
3165 }
3166
3167 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3168}
3169
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003170bool MipsTargetLowering::isLegalAddressingMode(const AddrMode &AM,
3171 Type *Ty) const {
Akira Hatanakaef839192012-11-17 00:25:41 +00003172 // No global is ever allowed as a base.
3173 if (AM.BaseGV)
3174 return false;
3175
3176 switch (AM.Scale) {
3177 case 0: // "r+i" or just "i", depending on HasBaseReg.
3178 break;
3179 case 1:
3180 if (!AM.HasBaseReg) // allow "r+i".
3181 break;
3182 return false; // disallow "r+r" or "r+r+i".
3183 default:
3184 return false;
3185 }
3186
3187 return true;
3188}
3189
3190bool
Dan Gohman2fe6bee2008-10-18 02:06:02 +00003191MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3192 // The Mips target isn't yet aware of offsets.
3193 return false;
3194}
Evan Cheng16993aa2009-10-27 19:56:55 +00003195
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003196EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003197 unsigned SrcAlign,
3198 bool IsMemset, bool ZeroMemset,
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003199 bool MemcpyStrSrc,
3200 MachineFunction &MF) const {
3201 if (Subtarget->hasMips64())
3202 return MVT::i64;
3203
3204 return MVT::i32;
3205}
3206
Evan Cheng83896a52009-10-28 01:43:28 +00003207bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
3208 if (VT != MVT::f32 && VT != MVT::f64)
3209 return false;
Bruno Cardoso Lopesb02a9df2011-01-18 19:41:41 +00003210 if (Imm.isNegZero())
3211 return false;
Evan Cheng16993aa2009-10-27 19:56:55 +00003212 return Imm.isZero();
3213}
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003214
3215unsigned MipsTargetLowering::getJumpTableEncoding() const {
3216 if (IsN64)
3217 return MachineJumpTableInfo::EK_GPRel64BlockAddress;
Jia Liuf54f60f2012-02-28 07:46:26 +00003218
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003219 return TargetLowering::getJumpTableEncoding();
3220}
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003221
Akira Hatanakae092f722013-03-05 22:54:59 +00003222/// This function returns true if CallSym is a long double emulation routine.
3223static bool isF128SoftLibCall(const char *CallSym) {
3224 const char *const LibCalls[] =
3225 {"__addtf3", "__divtf3", "__eqtf2", "__extenddftf2", "__extendsftf2",
3226 "__fixtfdi", "__fixtfsi", "__fixtfti", "__fixunstfdi", "__fixunstfsi",
3227 "__fixunstfti", "__floatditf", "__floatsitf", "__floattitf",
3228 "__floatunditf", "__floatunsitf", "__floatuntitf", "__getf2", "__gttf2",
3229 "__letf2", "__lttf2", "__multf3", "__netf2", "__powitf2", "__subtf3",
3230 "__trunctfdf2", "__trunctfsf2", "__unordtf2",
3231 "ceill", "copysignl", "cosl", "exp2l", "expl", "floorl", "fmal", "fmodl",
3232 "log10l", "log2l", "logl", "nearbyintl", "powl", "rintl", "sinl", "sqrtl",
3233 "truncl"};
3234
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003235 const char *const *End = LibCalls + array_lengthof(LibCalls);
Akira Hatanakae092f722013-03-05 22:54:59 +00003236
3237 // Check that LibCalls is sorted alphabetically.
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003238 MipsTargetLowering::LTStr Comp;
Akira Hatanakae092f722013-03-05 22:54:59 +00003239
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003240#ifndef NDEBUG
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003241 for (const char *const *I = LibCalls; I < End - 1; ++I)
Akira Hatanakae092f722013-03-05 22:54:59 +00003242 assert(Comp(*I, *(I + 1)));
3243#endif
3244
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003245 return std::binary_search(LibCalls, End, CallSym, Comp);
Akira Hatanakae092f722013-03-05 22:54:59 +00003246}
3247
3248/// This function returns true if Ty is fp128 or i128 which was originally a
3249/// fp128.
3250static bool originalTypeIsF128(const Type *Ty, const SDNode *CallNode) {
3251 if (Ty->isFP128Ty())
3252 return true;
3253
3254 const ExternalSymbolSDNode *ES =
3255 dyn_cast_or_null<const ExternalSymbolSDNode>(CallNode);
3256
3257 // If the Ty is i128 and the function being called is a long double emulation
3258 // routine, then the original type is f128.
3259 return (ES && Ty->isIntegerTy(128) && isF128SoftLibCall(ES->getSymbol()));
3260}
3261
Reed Kotler783c7942013-05-10 22:25:39 +00003262MipsTargetLowering::MipsCC::SpecialCallingConvType
3263 MipsTargetLowering::getSpecialCallingConv(SDValue Callee) const {
3264 MipsCC::SpecialCallingConvType SpecialCallingConv =
3265 MipsCC::NoSpecialCallingConv;;
3266 if (Subtarget->inMips16HardFloat()) {
3267 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3268 llvm::StringRef Sym = G->getGlobal()->getName();
3269 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
Reed Kotler3230e722013-12-12 02:41:11 +00003270 if (F && F->hasFnAttribute("__Mips16RetHelper")) {
Reed Kotler783c7942013-05-10 22:25:39 +00003271 SpecialCallingConv = MipsCC::Mips16RetHelperConv;
3272 }
3273 }
3274 }
3275 return SpecialCallingConv;
3276}
3277
3278MipsTargetLowering::MipsCC::MipsCC(
Akira Hatanakabfb66242013-08-20 23:38:40 +00003279 CallingConv::ID CC, bool IsO32_, bool IsFP64_, CCState &Info,
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003280 MipsCC::SpecialCallingConvType SpecialCallingConv_)
Akira Hatanakabfb66242013-08-20 23:38:40 +00003281 : CCInfo(Info), CallConv(CC), IsO32(IsO32_), IsFP64(IsFP64_),
Reed Kotler783c7942013-05-10 22:25:39 +00003282 SpecialCallingConv(SpecialCallingConv_){
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003283 // Pre-allocate reserved argument area.
Akira Hatanaka5001be52013-02-15 21:45:11 +00003284 CCInfo.AllocateStack(reservedArgArea(), 1);
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003285}
3286
Reed Kotler783c7942013-05-10 22:25:39 +00003287
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003288void MipsTargetLowering::MipsCC::
Akira Hatanaka5001be52013-02-15 21:45:11 +00003289analyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Args,
Akira Hatanaka3b7391d2013-03-05 22:20:28 +00003290 bool IsVarArg, bool IsSoftFloat, const SDNode *CallNode,
3291 std::vector<ArgListEntry> &FuncArgs) {
Akira Hatanaka5001be52013-02-15 21:45:11 +00003292 assert((CallConv != CallingConv::Fast || !IsVarArg) &&
3293 "CallingConv::Fast shouldn't be used for vararg functions.");
3294
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003295 unsigned NumOpnds = Args.size();
Akira Hatanaka5001be52013-02-15 21:45:11 +00003296 llvm::CCAssignFn *FixedFn = fixedArgFn(), *VarFn = varArgFn();
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003297
3298 for (unsigned I = 0; I != NumOpnds; ++I) {
3299 MVT ArgVT = Args[I].VT;
3300 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
3301 bool R;
3302
3303 if (ArgFlags.isByVal()) {
3304 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
3305 continue;
3306 }
3307
Akira Hatanaka5001be52013-02-15 21:45:11 +00003308 if (IsVarArg && !Args[I].IsFixed)
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003309 R = VarFn(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
Akira Hatanaka3b7391d2013-03-05 22:20:28 +00003310 else {
3311 MVT RegVT = getRegVT(ArgVT, FuncArgs[Args[I].OrigArgIndex].Ty, CallNode,
3312 IsSoftFloat);
3313 R = FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo);
3314 }
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003315
3316 if (R) {
3317#ifndef NDEBUG
3318 dbgs() << "Call operand #" << I << " has unhandled type "
3319 << EVT(ArgVT).getEVTString();
3320#endif
3321 llvm_unreachable(0);
3322 }
3323 }
3324}
3325
3326void MipsTargetLowering::MipsCC::
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003327analyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Args,
3328 bool IsSoftFloat, Function::const_arg_iterator FuncArg) {
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003329 unsigned NumArgs = Args.size();
Akira Hatanaka5001be52013-02-15 21:45:11 +00003330 llvm::CCAssignFn *FixedFn = fixedArgFn();
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003331 unsigned CurArgIdx = 0;
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003332
3333 for (unsigned I = 0; I != NumArgs; ++I) {
3334 MVT ArgVT = Args[I].VT;
3335 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003336 std::advance(FuncArg, Args[I].OrigArgIndex - CurArgIdx);
3337 CurArgIdx = Args[I].OrigArgIndex;
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003338
3339 if (ArgFlags.isByVal()) {
3340 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
3341 continue;
3342 }
3343
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003344 MVT RegVT = getRegVT(ArgVT, FuncArg->getType(), 0, IsSoftFloat);
3345
3346 if (!FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo))
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003347 continue;
3348
3349#ifndef NDEBUG
3350 dbgs() << "Formal Arg #" << I << " has unhandled type "
3351 << EVT(ArgVT).getEVTString();
3352#endif
3353 llvm_unreachable(0);
3354 }
3355}
3356
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003357template<typename Ty>
3358void MipsTargetLowering::MipsCC::
3359analyzeReturn(const SmallVectorImpl<Ty> &RetVals, bool IsSoftFloat,
3360 const SDNode *CallNode, const Type *RetTy) const {
Akira Hatanakae092f722013-03-05 22:54:59 +00003361 CCAssignFn *Fn;
3362
3363 if (IsSoftFloat && originalTypeIsF128(RetTy, CallNode))
3364 Fn = RetCC_F128Soft;
3365 else
3366 Fn = RetCC_Mips;
3367
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003368 for (unsigned I = 0, E = RetVals.size(); I < E; ++I) {
3369 MVT VT = RetVals[I].VT;
3370 ISD::ArgFlagsTy Flags = RetVals[I].Flags;
3371 MVT RegVT = this->getRegVT(VT, RetTy, CallNode, IsSoftFloat);
3372
Akira Hatanakae092f722013-03-05 22:54:59 +00003373 if (Fn(I, VT, RegVT, CCValAssign::Full, Flags, this->CCInfo)) {
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003374#ifndef NDEBUG
3375 dbgs() << "Call result #" << I << " has unhandled type "
3376 << EVT(VT).getEVTString() << '\n';
3377#endif
3378 llvm_unreachable(0);
3379 }
3380 }
3381}
3382
3383void MipsTargetLowering::MipsCC::
3384analyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins, bool IsSoftFloat,
3385 const SDNode *CallNode, const Type *RetTy) const {
3386 analyzeReturn(Ins, IsSoftFloat, CallNode, RetTy);
3387}
3388
3389void MipsTargetLowering::MipsCC::
3390analyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs, bool IsSoftFloat,
3391 const Type *RetTy) const {
3392 analyzeReturn(Outs, IsSoftFloat, 0, RetTy);
3393}
3394
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003395void MipsTargetLowering::MipsCC::handleByValArg(unsigned ValNo, MVT ValVT,
3396 MVT LocVT,
3397 CCValAssign::LocInfo LocInfo,
3398 ISD::ArgFlagsTy ArgFlags) {
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003399 assert(ArgFlags.getByValSize() && "Byval argument's size shouldn't be 0.");
3400
3401 struct ByValArgInfo ByVal;
Akira Hatanaka5001be52013-02-15 21:45:11 +00003402 unsigned RegSize = regSize();
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003403 unsigned ByValSize = RoundUpToAlignment(ArgFlags.getByValSize(), RegSize);
3404 unsigned Align = std::min(std::max(ArgFlags.getByValAlign(), RegSize),
3405 RegSize * 2);
3406
Akira Hatanaka5001be52013-02-15 21:45:11 +00003407 if (useRegsForByval())
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003408 allocateRegs(ByVal, ByValSize, Align);
3409
3410 // Allocate space on caller's stack.
3411 ByVal.Address = CCInfo.AllocateStack(ByValSize - RegSize * ByVal.NumRegs,
3412 Align);
3413 CCInfo.addLoc(CCValAssign::getMem(ValNo, ValVT, ByVal.Address, LocVT,
3414 LocInfo));
3415 ByValArgs.push_back(ByVal);
3416}
3417
Akira Hatanaka5001be52013-02-15 21:45:11 +00003418unsigned MipsTargetLowering::MipsCC::numIntArgRegs() const {
3419 return IsO32 ? array_lengthof(O32IntRegs) : array_lengthof(Mips64IntRegs);
3420}
3421
3422unsigned MipsTargetLowering::MipsCC::reservedArgArea() const {
3423 return (IsO32 && (CallConv != CallingConv::Fast)) ? 16 : 0;
3424}
3425
3426const uint16_t *MipsTargetLowering::MipsCC::intArgRegs() const {
3427 return IsO32 ? O32IntRegs : Mips64IntRegs;
3428}
3429
3430llvm::CCAssignFn *MipsTargetLowering::MipsCC::fixedArgFn() const {
3431 if (CallConv == CallingConv::Fast)
3432 return CC_Mips_FastCC;
3433
Reed Kotler783c7942013-05-10 22:25:39 +00003434 if (SpecialCallingConv == Mips16RetHelperConv)
3435 return CC_Mips16RetHelper;
Akira Hatanakabfb66242013-08-20 23:38:40 +00003436 return IsO32 ? (IsFP64 ? CC_MipsO32_FP64 : CC_MipsO32_FP32) : CC_MipsN;
Akira Hatanaka5001be52013-02-15 21:45:11 +00003437}
3438
3439llvm::CCAssignFn *MipsTargetLowering::MipsCC::varArgFn() const {
Akira Hatanakabfb66242013-08-20 23:38:40 +00003440 return IsO32 ? (IsFP64 ? CC_MipsO32_FP64 : CC_MipsO32_FP32) : CC_MipsN_VarArg;
Akira Hatanaka5001be52013-02-15 21:45:11 +00003441}
3442
3443const uint16_t *MipsTargetLowering::MipsCC::shadowRegs() const {
3444 return IsO32 ? O32IntRegs : Mips64DPRegs;
3445}
3446
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003447void MipsTargetLowering::MipsCC::allocateRegs(ByValArgInfo &ByVal,
3448 unsigned ByValSize,
3449 unsigned Align) {
Akira Hatanaka5001be52013-02-15 21:45:11 +00003450 unsigned RegSize = regSize(), NumIntArgRegs = numIntArgRegs();
3451 const uint16_t *IntArgRegs = intArgRegs(), *ShadowRegs = shadowRegs();
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003452 assert(!(ByValSize % RegSize) && !(Align % RegSize) &&
3453 "Byval argument's size and alignment should be a multiple of"
3454 "RegSize.");
3455
3456 ByVal.FirstIdx = CCInfo.getFirstUnallocated(IntArgRegs, NumIntArgRegs);
3457
3458 // If Align > RegSize, the first arg register must be even.
3459 if ((Align > RegSize) && (ByVal.FirstIdx % 2)) {
3460 CCInfo.AllocateReg(IntArgRegs[ByVal.FirstIdx], ShadowRegs[ByVal.FirstIdx]);
3461 ++ByVal.FirstIdx;
3462 }
3463
3464 // Mark the registers allocated.
3465 for (unsigned I = ByVal.FirstIdx; ByValSize && (I < NumIntArgRegs);
3466 ByValSize -= RegSize, ++I, ++ByVal.NumRegs)
3467 CCInfo.AllocateReg(IntArgRegs[I], ShadowRegs[I]);
3468}
Akira Hatanaka25dad192012-10-27 00:10:18 +00003469
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003470MVT MipsTargetLowering::MipsCC::getRegVT(MVT VT, const Type *OrigTy,
3471 const SDNode *CallNode,
3472 bool IsSoftFloat) const {
3473 if (IsSoftFloat || IsO32)
3474 return VT;
3475
3476 // Check if the original type was fp128.
Akira Hatanakae092f722013-03-05 22:54:59 +00003477 if (originalTypeIsF128(OrigTy, CallNode)) {
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003478 assert(VT == MVT::i64);
3479 return MVT::f64;
3480 }
3481
3482 return VT;
3483}
3484
Akira Hatanaka25dad192012-10-27 00:10:18 +00003485void MipsTargetLowering::
Andrew Trickef9de2a2013-05-25 02:42:55 +00003486copyByValRegs(SDValue Chain, SDLoc DL, std::vector<SDValue> &OutChains,
Akira Hatanaka25dad192012-10-27 00:10:18 +00003487 SelectionDAG &DAG, const ISD::ArgFlagsTy &Flags,
3488 SmallVectorImpl<SDValue> &InVals, const Argument *FuncArg,
3489 const MipsCC &CC, const ByValArgInfo &ByVal) const {
3490 MachineFunction &MF = DAG.getMachineFunction();
3491 MachineFrameInfo *MFI = MF.getFrameInfo();
3492 unsigned RegAreaSize = ByVal.NumRegs * CC.regSize();
3493 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
3494 int FrameObjOffset;
3495
3496 if (RegAreaSize)
3497 FrameObjOffset = (int)CC.reservedArgArea() -
3498 (int)((CC.numIntArgRegs() - ByVal.FirstIdx) * CC.regSize());
3499 else
3500 FrameObjOffset = ByVal.Address;
3501
3502 // Create frame object.
3503 EVT PtrTy = getPointerTy();
3504 int FI = MFI->CreateFixedObject(FrameObjSize, FrameObjOffset, true);
3505 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
3506 InVals.push_back(FIN);
3507
3508 if (!ByVal.NumRegs)
3509 return;
3510
3511 // Copy arg registers.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00003512 MVT RegTy = MVT::getIntegerVT(CC.regSize() * 8);
Akira Hatanaka25dad192012-10-27 00:10:18 +00003513 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3514
3515 for (unsigned I = 0; I < ByVal.NumRegs; ++I) {
3516 unsigned ArgReg = CC.intArgRegs()[ByVal.FirstIdx + I];
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003517 unsigned VReg = addLiveIn(MF, ArgReg, RC);
Akira Hatanaka25dad192012-10-27 00:10:18 +00003518 unsigned Offset = I * CC.regSize();
3519 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
3520 DAG.getConstant(Offset, PtrTy));
3521 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
3522 StorePtr, MachinePointerInfo(FuncArg, Offset),
3523 false, false, 0);
3524 OutChains.push_back(Store);
3525 }
3526}
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003527
3528// Copy byVal arg to registers and stack.
3529void MipsTargetLowering::
Andrew Trickef9de2a2013-05-25 02:42:55 +00003530passByValArg(SDValue Chain, SDLoc DL,
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00003531 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
Craig Topperb94011f2013-07-14 04:42:23 +00003532 SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr,
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003533 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
3534 const MipsCC &CC, const ByValArgInfo &ByVal,
3535 const ISD::ArgFlagsTy &Flags, bool isLittle) const {
3536 unsigned ByValSize = Flags.getByValSize();
3537 unsigned Offset = 0; // Offset in # of bytes from the beginning of struct.
3538 unsigned RegSize = CC.regSize();
3539 unsigned Alignment = std::min(Flags.getByValAlign(), RegSize);
3540 EVT PtrTy = getPointerTy(), RegTy = MVT::getIntegerVT(RegSize * 8);
3541
3542 if (ByVal.NumRegs) {
3543 const uint16_t *ArgRegs = CC.intArgRegs();
3544 bool LeftoverBytes = (ByVal.NumRegs * RegSize > ByValSize);
3545 unsigned I = 0;
3546
3547 // Copy words to registers.
3548 for (; I < ByVal.NumRegs - LeftoverBytes; ++I, Offset += RegSize) {
3549 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3550 DAG.getConstant(Offset, PtrTy));
3551 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
3552 MachinePointerInfo(), false, false, false,
3553 Alignment);
3554 MemOpChains.push_back(LoadVal.getValue(1));
3555 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3556 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
3557 }
3558
3559 // Return if the struct has been fully copied.
3560 if (ByValSize == Offset)
3561 return;
3562
3563 // Copy the remainder of the byval argument with sub-word loads and shifts.
3564 if (LeftoverBytes) {
3565 assert((ByValSize > Offset) && (ByValSize < Offset + RegSize) &&
3566 "Size of the remainder should be smaller than RegSize.");
3567 SDValue Val;
3568
3569 for (unsigned LoadSize = RegSize / 2, TotalSizeLoaded = 0;
3570 Offset < ByValSize; LoadSize /= 2) {
3571 unsigned RemSize = ByValSize - Offset;
3572
3573 if (RemSize < LoadSize)
3574 continue;
3575
3576 // Load subword.
3577 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3578 DAG.getConstant(Offset, PtrTy));
3579 SDValue LoadVal =
3580 DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr,
3581 MachinePointerInfo(), MVT::getIntegerVT(LoadSize * 8),
3582 false, false, Alignment);
3583 MemOpChains.push_back(LoadVal.getValue(1));
3584
3585 // Shift the loaded value.
3586 unsigned Shamt;
3587
3588 if (isLittle)
3589 Shamt = TotalSizeLoaded;
3590 else
3591 Shamt = (RegSize - (TotalSizeLoaded + LoadSize)) * 8;
3592
3593 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
3594 DAG.getConstant(Shamt, MVT::i32));
3595
3596 if (Val.getNode())
3597 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
3598 else
3599 Val = Shift;
3600
3601 Offset += LoadSize;
3602 TotalSizeLoaded += LoadSize;
3603 Alignment = std::min(Alignment, LoadSize);
3604 }
3605
3606 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3607 RegsToPass.push_back(std::make_pair(ArgReg, Val));
3608 return;
3609 }
3610 }
3611
3612 // Copy remainder of byval arg to it with memcpy.
3613 unsigned MemCpySize = ByValSize - Offset;
3614 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3615 DAG.getConstant(Offset, PtrTy));
3616 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
3617 DAG.getIntPtrConstant(ByVal.Address));
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003618 Chain = DAG.getMemcpy(Chain, DL, Dst, Src, DAG.getConstant(MemCpySize, PtrTy),
3619 Alignment, /*isVolatile=*/false, /*AlwaysInline=*/false,
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003620 MachinePointerInfo(0), MachinePointerInfo(0));
3621 MemOpChains.push_back(Chain);
3622}
Akira Hatanaka2a134022012-10-27 00:21:13 +00003623
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003624void MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
3625 const MipsCC &CC, SDValue Chain,
3626 SDLoc DL, SelectionDAG &DAG) const {
Akira Hatanaka2a134022012-10-27 00:21:13 +00003627 unsigned NumRegs = CC.numIntArgRegs();
3628 const uint16_t *ArgRegs = CC.intArgRegs();
3629 const CCState &CCInfo = CC.getCCInfo();
3630 unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs, NumRegs);
3631 unsigned RegSize = CC.regSize();
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00003632 MVT RegTy = MVT::getIntegerVT(RegSize * 8);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003633 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3634 MachineFunction &MF = DAG.getMachineFunction();
3635 MachineFrameInfo *MFI = MF.getFrameInfo();
3636 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3637
3638 // Offset of the first variable argument from stack pointer.
3639 int VaArgOffset;
3640
3641 if (NumRegs == Idx)
3642 VaArgOffset = RoundUpToAlignment(CCInfo.getNextStackOffset(), RegSize);
3643 else
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003644 VaArgOffset = (int)CC.reservedArgArea() - (int)(RegSize * (NumRegs - Idx));
Akira Hatanaka2a134022012-10-27 00:21:13 +00003645
3646 // Record the frame index of the first variable argument
3647 // which is a value necessary to VASTART.
3648 int FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
3649 MipsFI->setVarArgsFrameIndex(FI);
3650
3651 // Copy the integer registers that have not been used for argument passing
3652 // to the argument register save area. For O32, the save area is allocated
3653 // in the caller's stack frame, while for N32/64, it is allocated in the
3654 // callee's stack frame.
3655 for (unsigned I = Idx; I < NumRegs; ++I, VaArgOffset += RegSize) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003656 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003657 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
3658 FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
3659 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
3660 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
3661 MachinePointerInfo(), false, false, 0);
3662 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(0);
3663 OutChains.push_back(Store);
3664 }
3665}