blob: b1a25dd46392099ec26915075e1ef5730967dd7e [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;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000395}
396
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000397const MipsTargetLowering *MipsTargetLowering::create(MipsTargetMachine &TM) {
398 if (TM.getSubtargetImpl()->inMips16Mode())
399 return llvm::createMips16TargetLowering(TM);
Jia Liuf54f60f2012-02-28 07:46:26 +0000400
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000401 return llvm::createMipsSETargetLowering(TM);
Akira Hatanaka2fcc1cf2011-08-12 21:30:06 +0000402}
403
Matt Arsenault758659232013-05-18 00:21:46 +0000404EVT MipsTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Akira Hatanakab13b3332013-01-04 20:06:01 +0000405 if (!VT.isVector())
406 return MVT::i32;
407 return VT.changeVectorElementTypeToInteger();
Scott Michela6729e82008-03-10 15:42:14 +0000408}
409
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000410static SDValue performDivRemCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000411 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000412 const MipsSubtarget *Subtarget) {
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000413 if (DCI.isBeforeLegalizeOps())
414 return SDValue();
415
Akira Hatanakab1538f92011-10-03 21:06:13 +0000416 EVT Ty = N->getValueType(0);
Akira Hatanaka8002a3f2013-08-14 00:47:08 +0000417 unsigned LO = (Ty == MVT::i32) ? Mips::LO0 : Mips::LO0_64;
418 unsigned HI = (Ty == MVT::i32) ? Mips::HI0 : Mips::HI0_64;
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000419 unsigned Opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem16 :
420 MipsISD::DivRemU16;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000421 SDLoc DL(N);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000422
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000423 SDValue DivRem = DAG.getNode(Opc, DL, MVT::Glue,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000424 N->getOperand(0), N->getOperand(1));
425 SDValue InChain = DAG.getEntryNode();
426 SDValue InGlue = DivRem;
427
428 // insert MFLO
429 if (N->hasAnyUseOfValue(0)) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000430 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, DL, LO, Ty,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000431 InGlue);
432 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
433 InChain = CopyFromLo.getValue(1);
434 InGlue = CopyFromLo.getValue(2);
435 }
436
437 // insert MFHI
438 if (N->hasAnyUseOfValue(1)) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000439 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, DL,
Akira Hatanakab1538f92011-10-03 21:06:13 +0000440 HI, Ty, InGlue);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000441 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
442 }
443
444 return SDValue();
445}
446
Akira Hatanaka89af5892013-04-18 01:00:46 +0000447static Mips::CondCode condCodeToFCC(ISD::CondCode CC) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000448 switch (CC) {
449 default: llvm_unreachable("Unknown fp condition code!");
450 case ISD::SETEQ:
451 case ISD::SETOEQ: return Mips::FCOND_OEQ;
452 case ISD::SETUNE: return Mips::FCOND_UNE;
453 case ISD::SETLT:
454 case ISD::SETOLT: return Mips::FCOND_OLT;
455 case ISD::SETGT:
456 case ISD::SETOGT: return Mips::FCOND_OGT;
457 case ISD::SETLE:
458 case ISD::SETOLE: return Mips::FCOND_OLE;
459 case ISD::SETGE:
460 case ISD::SETOGE: return Mips::FCOND_OGE;
461 case ISD::SETULT: return Mips::FCOND_ULT;
462 case ISD::SETULE: return Mips::FCOND_ULE;
463 case ISD::SETUGT: return Mips::FCOND_UGT;
464 case ISD::SETUGE: return Mips::FCOND_UGE;
465 case ISD::SETUO: return Mips::FCOND_UN;
466 case ISD::SETO: return Mips::FCOND_OR;
467 case ISD::SETNE:
468 case ISD::SETONE: return Mips::FCOND_ONE;
469 case ISD::SETUEQ: return Mips::FCOND_UEQ;
470 }
471}
472
473
Akira Hatanakaf0ea5002013-03-30 01:16:38 +0000474/// This function returns true if the floating point conditional branches and
475/// conditional moves which use condition code CC should be inverted.
476static bool invertFPCondCodeUser(Mips::CondCode CC) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000477 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
478 return false;
479
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000480 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
481 "Illegal Condition Code");
Akira Hatanakaa5352702011-03-31 18:26:17 +0000482
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000483 return true;
Akira Hatanakaa5352702011-03-31 18:26:17 +0000484}
485
486// Creates and returns an FPCmp node from a setcc node.
487// Returns Op if setcc is not a floating point comparison.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000488static SDValue createFPCmp(SelectionDAG &DAG, const SDValue &Op) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000489 // must be a SETCC node
490 if (Op.getOpcode() != ISD::SETCC)
491 return Op;
492
493 SDValue LHS = Op.getOperand(0);
494
495 if (!LHS.getValueType().isFloatingPoint())
496 return Op;
497
498 SDValue RHS = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000499 SDLoc DL(Op);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000500
Akira Hatanakaaef55c82011-04-15 21:00:26 +0000501 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
502 // node if necessary.
Akira Hatanakaa5352702011-03-31 18:26:17 +0000503 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
504
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000505 return DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS,
Akira Hatanaka89af5892013-04-18 01:00:46 +0000506 DAG.getConstant(condCodeToFCC(CC), MVT::i32));
Akira Hatanakaa5352702011-03-31 18:26:17 +0000507}
508
509// Creates and returns a CMovFPT/F node.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000510static SDValue createCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000511 SDValue False, SDLoc DL) {
Akira Hatanakaf0ea5002013-03-30 01:16:38 +0000512 ConstantSDNode *CC = cast<ConstantSDNode>(Cond.getOperand(2));
513 bool invert = invertFPCondCodeUser((Mips::CondCode)CC->getSExtValue());
Akira Hatanaka8bce21c2013-07-26 20:51:20 +0000514 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000515
516 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
Akira Hatanaka8bce21c2013-07-26 20:51:20 +0000517 True.getValueType(), True, FCC0, False, Cond);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000518}
519
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000520static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000521 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000522 const MipsSubtarget *Subtarget) {
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000523 if (DCI.isBeforeLegalizeOps())
524 return SDValue();
525
526 SDValue SetCC = N->getOperand(0);
527
528 if ((SetCC.getOpcode() != ISD::SETCC) ||
529 !SetCC.getOperand(0).getValueType().isInteger())
530 return SDValue();
531
532 SDValue False = N->getOperand(2);
533 EVT FalseTy = False.getValueType();
534
535 if (!FalseTy.isInteger())
536 return SDValue();
537
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000538 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(False);
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000539
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000540 // If the RHS (False) is 0, we swap the order of the operands
541 // of ISD::SELECT (obviously also inverting the condition) so that we can
542 // take advantage of conditional moves using the $0 register.
543 // Example:
544 // return (a != 0) ? x : 0;
545 // load $reg, x
546 // movz $reg, $0, a
547 if (!FalseC)
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000548 return SDValue();
549
Andrew Trickef9de2a2013-05-25 02:42:55 +0000550 const SDLoc DL(N);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000551
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000552 if (!FalseC->getZExtValue()) {
553 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
554 SDValue True = N->getOperand(1);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000555
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000556 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
557 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
558
559 return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True);
560 }
561
Matheus Almeidaa6beac12013-12-05 12:07:05 +0000562 // If both operands are integer constants there's a possibility that we
563 // can do some interesting optimizations.
564 SDValue True = N->getOperand(1);
565 ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(True);
566
567 if (!TrueC || !True.getValueType().isInteger())
568 return SDValue();
569
570 // We'll also ignore MVT::i64 operands as this optimizations proves
571 // to be ineffective because of the required sign extensions as the result
572 // of a SETCC operator is always MVT::i32 for non-vector types.
573 if (True.getValueType() == MVT::i64)
574 return SDValue();
575
576 int64_t Diff = TrueC->getSExtValue() - FalseC->getSExtValue();
577
578 // 1) (a < x) ? y : y-1
579 // slti $reg1, a, x
580 // addiu $reg2, $reg1, y-1
581 if (Diff == 1)
582 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, False);
583
584 // 2) (a < x) ? y-1 : y
585 // slti $reg1, a, x
586 // xor $reg1, $reg1, 1
587 // addiu $reg2, $reg1, y-1
588 if (Diff == -1) {
589 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
590 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
591 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
592 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, True);
593 }
594
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000595 // Couldn't optimize.
596 return SDValue();
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000597}
598
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000599static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000600 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000601 const MipsSubtarget *Subtarget) {
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000602 // Pattern match EXT.
603 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
604 // => ext $dst, $src, size, pos
Akira Hatanaka4a3836b2013-10-09 23:36:17 +0000605 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasExtractInsert())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000606 return SDValue();
607
608 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000609 unsigned ShiftRightOpc = ShiftRight.getOpcode();
610
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000611 // Op's first operand must be a shift right.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000612 if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL)
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000613 return SDValue();
614
615 // The second operand of the shift must be an immediate.
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000616 ConstantSDNode *CN;
617 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
618 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000619
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000620 uint64_t Pos = CN->getZExtValue();
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000621 uint64_t SMPos, SMSize;
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000622
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000623 // Op's second operand must be a shifted mask.
624 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000625 !isShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000626 return SDValue();
627
628 // Return if the shifted mask does not start at bit 0 or the sum of its size
629 // and Pos exceeds the word's size.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000630 EVT ValTy = N->getValueType(0);
631 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000632 return SDValue();
633
Andrew Trickef9de2a2013-05-25 02:42:55 +0000634 return DAG.getNode(MipsISD::Ext, SDLoc(N), ValTy,
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000635 ShiftRight.getOperand(0), DAG.getConstant(Pos, MVT::i32),
Akira Hatanakaeea541c2011-08-17 22:59:46 +0000636 DAG.getConstant(SMSize, MVT::i32));
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000637}
Jia Liuf54f60f2012-02-28 07:46:26 +0000638
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000639static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000640 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000641 const MipsSubtarget *Subtarget) {
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000642 // Pattern match INS.
643 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
Jia Liuf54f60f2012-02-28 07:46:26 +0000644 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000645 // => ins $dst, $src, size, pos, $src1
Akira Hatanaka4a3836b2013-10-09 23:36:17 +0000646 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasExtractInsert())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000647 return SDValue();
648
649 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
650 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
651 ConstantSDNode *CN;
652
653 // See if Op's first operand matches (and $src1 , mask0).
654 if (And0.getOpcode() != ISD::AND)
655 return SDValue();
656
657 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000658 !isShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000659 return SDValue();
660
661 // See if Op's second operand matches (and (shl $src, pos), mask1).
662 if (And1.getOpcode() != ISD::AND)
663 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000664
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000665 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000666 !isShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000667 return SDValue();
668
669 // The shift masks must have the same position and size.
670 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
671 return SDValue();
672
673 SDValue Shl = And1.getOperand(0);
674 if (Shl.getOpcode() != ISD::SHL)
675 return SDValue();
676
677 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
678 return SDValue();
679
680 unsigned Shamt = CN->getZExtValue();
681
682 // Return if the shift amount and the first bit position of mask are not the
Jia Liuf54f60f2012-02-28 07:46:26 +0000683 // same.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000684 EVT ValTy = N->getValueType(0);
685 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000686 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000687
Andrew Trickef9de2a2013-05-25 02:42:55 +0000688 return DAG.getNode(MipsISD::Ins, SDLoc(N), ValTy, Shl.getOperand(0),
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000689 DAG.getConstant(SMPos0, MVT::i32),
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000690 DAG.getConstant(SMSize0, MVT::i32), And0.getOperand(0));
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000691}
Jia Liuf54f60f2012-02-28 07:46:26 +0000692
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000693static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000694 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000695 const MipsSubtarget *Subtarget) {
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000696 // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt))
697
698 if (DCI.isBeforeLegalizeOps())
699 return SDValue();
700
701 SDValue Add = N->getOperand(1);
702
703 if (Add.getOpcode() != ISD::ADD)
704 return SDValue();
705
706 SDValue Lo = Add.getOperand(1);
707
708 if ((Lo.getOpcode() != MipsISD::Lo) ||
709 (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable))
710 return SDValue();
711
712 EVT ValTy = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000713 SDLoc DL(N);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000714
715 SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, N->getOperand(0),
716 Add.getOperand(0));
717 return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo);
718}
719
Bruno Cardoso Lopes61a61e92011-02-10 18:05:10 +0000720SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000721 const {
722 SelectionDAG &DAG = DCI.DAG;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000723 unsigned Opc = N->getOpcode();
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000724
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000725 switch (Opc) {
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000726 default: break;
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000727 case ISD::SDIVREM:
728 case ISD::UDIVREM:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000729 return performDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000730 case ISD::SELECT:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000731 return performSELECTCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000732 case ISD::AND:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000733 return performANDCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000734 case ISD::OR:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000735 return performORCombine(N, DAG, DCI, Subtarget);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000736 case ISD::ADD:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000737 return performADDCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000738 }
739
740 return SDValue();
741}
742
Akira Hatanakafabb8cf2012-09-21 23:58:31 +0000743void
744MipsTargetLowering::LowerOperationWrapper(SDNode *N,
745 SmallVectorImpl<SDValue> &Results,
746 SelectionDAG &DAG) const {
747 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
748
749 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
750 Results.push_back(Res.getValue(I));
751}
752
753void
754MipsTargetLowering::ReplaceNodeResults(SDNode *N,
755 SmallVectorImpl<SDValue> &Results,
756 SelectionDAG &DAG) const {
Akira Hatanaka9da442f2013-04-30 21:17:07 +0000757 return LowerOperationWrapper(N, Results, DAG);
Akira Hatanakafabb8cf2012-09-21 23:58:31 +0000758}
759
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000760SDValue MipsTargetLowering::
Dan Gohman21cea8a2010-04-17 15:26:15 +0000761LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000762{
Wesley Peck527da1b2010-11-23 03:31:01 +0000763 switch (Op.getOpcode())
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000764 {
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000765 case ISD::BR_JT: return lowerBR_JT(Op, DAG);
766 case ISD::BRCOND: return lowerBRCOND(Op, DAG);
767 case ISD::ConstantPool: return lowerConstantPool(Op, DAG);
768 case ISD::GlobalAddress: return lowerGlobalAddress(Op, DAG);
769 case ISD::BlockAddress: return lowerBlockAddress(Op, DAG);
770 case ISD::GlobalTLSAddress: return lowerGlobalTLSAddress(Op, DAG);
771 case ISD::JumpTable: return lowerJumpTable(Op, DAG);
772 case ISD::SELECT: return lowerSELECT(Op, DAG);
773 case ISD::SELECT_CC: return lowerSELECT_CC(Op, DAG);
774 case ISD::SETCC: return lowerSETCC(Op, DAG);
775 case ISD::VASTART: return lowerVASTART(Op, DAG);
776 case ISD::FCOPYSIGN: return lowerFCOPYSIGN(Op, DAG);
777 case ISD::FABS: return lowerFABS(Op, DAG);
778 case ISD::FRAMEADDR: return lowerFRAMEADDR(Op, DAG);
779 case ISD::RETURNADDR: return lowerRETURNADDR(Op, DAG);
780 case ISD::EH_RETURN: return lowerEH_RETURN(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000781 case ISD::ATOMIC_FENCE: return lowerATOMIC_FENCE(Op, DAG);
782 case ISD::SHL_PARTS: return lowerShiftLeftParts(Op, DAG);
783 case ISD::SRA_PARTS: return lowerShiftRightParts(Op, DAG, true);
784 case ISD::SRL_PARTS: return lowerShiftRightParts(Op, DAG, false);
785 case ISD::LOAD: return lowerLOAD(Op, DAG);
786 case ISD::STORE: return lowerSTORE(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000787 case ISD::ADD: return lowerADD(Op, DAG);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000788 case ISD::FP_TO_SINT: return lowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000789 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000790 return SDValue();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000791}
792
Akira Hatanakae2489122011-04-15 21:51:11 +0000793//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000794// Lower helper functions
Akira Hatanakae2489122011-04-15 21:51:11 +0000795//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000796
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000797// addLiveIn - This helper function adds the specified physical register to the
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000798// MachineFunction as a live in value. It also creates a corresponding
799// virtual register for it.
800static unsigned
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000801addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000802{
Chris Lattnera10fff52007-12-31 04:13:23 +0000803 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
804 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000805 return VReg;
806}
807
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000808static MachineBasicBlock *expandPseudoDIV(MachineInstr *MI,
809 MachineBasicBlock &MBB,
810 const TargetInstrInfo &TII,
811 bool Is64Bit) {
812 if (NoZeroDivCheck)
813 return &MBB;
814
815 // Insert instruction "teq $divisor_reg, $zero, 7".
816 MachineBasicBlock::iterator I(MI);
817 MachineInstrBuilder MIB;
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000818 MachineOperand &Divisor = MI->getOperand(2);
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000819 MIB = BuildMI(MBB, llvm::next(I), MI->getDebugLoc(), TII.get(Mips::TEQ))
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000820 .addReg(Divisor.getReg(), getKillRegState(Divisor.isKill()))
821 .addReg(Mips::ZERO).addImm(7);
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000822
823 // Use the 32-bit sub-register if this is a 64-bit division.
824 if (Is64Bit)
825 MIB->getOperand(0).setSubReg(Mips::sub_32);
826
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000827 // Clear Divisor's kill flag.
828 Divisor.setIsKill(false);
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000829 return &MBB;
830}
831
Akira Hatanakae4bd0542012-09-27 02:15:57 +0000832MachineBasicBlock *
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +0000833MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +0000834 MachineBasicBlock *BB) const {
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +0000835 switch (MI->getOpcode()) {
Reed Kotler97ba5f22013-02-21 04:22:38 +0000836 default:
837 llvm_unreachable("Unexpected instr type to insert");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000838 case Mips::ATOMIC_LOAD_ADD_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000839 return emitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000840 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000841 return emitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000842 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000843 return emitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000844 case Mips::ATOMIC_LOAD_ADD_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000845 return emitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000846
847 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000848 return emitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000849 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000850 return emitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000851 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000852 return emitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000853 case Mips::ATOMIC_LOAD_AND_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000854 return emitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000855
856 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000857 return emitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000858 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000859 return emitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000860 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000861 return emitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000862 case Mips::ATOMIC_LOAD_OR_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000863 return emitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000864
865 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000866 return emitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000867 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000868 return emitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000869 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000870 return emitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000871 case Mips::ATOMIC_LOAD_XOR_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000872 return emitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000873
874 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000875 return emitAtomicBinaryPartword(MI, BB, 1, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000876 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000877 return emitAtomicBinaryPartword(MI, BB, 2, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000878 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000879 return emitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000880 case Mips::ATOMIC_LOAD_NAND_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000881 return emitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000882
883 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000884 return emitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000885 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000886 return emitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000887 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000888 return emitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000889 case Mips::ATOMIC_LOAD_SUB_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000890 return emitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000891
892 case Mips::ATOMIC_SWAP_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000893 return emitAtomicBinaryPartword(MI, BB, 1, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000894 case Mips::ATOMIC_SWAP_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000895 return emitAtomicBinaryPartword(MI, BB, 2, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000896 case Mips::ATOMIC_SWAP_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000897 return emitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000898 case Mips::ATOMIC_SWAP_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000899 return emitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000900
901 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000902 return emitAtomicCmpSwapPartword(MI, BB, 1);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000903 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000904 return emitAtomicCmpSwapPartword(MI, BB, 2);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000905 case Mips::ATOMIC_CMP_SWAP_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000906 return emitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000907 case Mips::ATOMIC_CMP_SWAP_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000908 return emitAtomicCmpSwap(MI, BB, 8);
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000909 case Mips::PseudoSDIV:
910 case Mips::PseudoUDIV:
911 return expandPseudoDIV(MI, *BB, *getTargetMachine().getInstrInfo(), false);
912 case Mips::PseudoDSDIV:
913 case Mips::PseudoDUDIV:
914 return expandPseudoDIV(MI, *BB, *getTargetMachine().getInstrInfo(), true);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000915 }
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +0000916}
917
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000918// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
919// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
920MachineBasicBlock *
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000921MipsTargetLowering::emitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher0713a9d2011-06-08 23:55:35 +0000922 unsigned Size, unsigned BinOpcode,
Akira Hatanaka15506782011-06-07 18:58:42 +0000923 bool Nand) const {
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000924 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000925
926 MachineFunction *MF = BB->getParent();
927 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000928 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000929 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000930 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000931 unsigned LL, SC, AND, NOR, ZERO, BEQ;
932
933 if (Size == 4) {
Akira Hatanaka6781fc12013-08-20 21:08:22 +0000934 LL = Mips::LL;
935 SC = Mips::SC;
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000936 AND = Mips::AND;
937 NOR = Mips::NOR;
938 ZERO = Mips::ZERO;
939 BEQ = Mips::BEQ;
940 }
941 else {
Akira Hatanaka6781fc12013-08-20 21:08:22 +0000942 LL = Mips::LLD;
943 SC = Mips::SCD;
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000944 AND = Mips::AND64;
945 NOR = Mips::NOR64;
946 ZERO = Mips::ZERO_64;
947 BEQ = Mips::BEQ64;
948 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000949
Akira Hatanaka0e019592011-07-19 20:11:17 +0000950 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000951 unsigned Ptr = MI->getOperand(1).getReg();
952 unsigned Incr = MI->getOperand(2).getReg();
953
Akira Hatanaka0e019592011-07-19 20:11:17 +0000954 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
955 unsigned AndRes = RegInfo.createVirtualRegister(RC);
956 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000957
958 // insert new blocks after the current block
959 const BasicBlock *LLVM_BB = BB->getBasicBlock();
960 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
961 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
962 MachineFunction::iterator It = BB;
963 ++It;
964 MF->insert(It, loopMBB);
965 MF->insert(It, exitMBB);
966
967 // Transfer the remainder of BB and its successor edges to exitMBB.
968 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka4c0a7122013-10-07 19:33:02 +0000969 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000970 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
971
972 // thisMBB:
973 // ...
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000974 // fallthrough --> loopMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000975 BB->addSuccessor(loopMBB);
Akira Hatanaka08636b42011-07-19 17:09:53 +0000976 loopMBB->addSuccessor(loopMBB);
977 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000978
979 // loopMBB:
980 // ll oldval, 0(ptr)
Akira Hatanaka0e019592011-07-19 20:11:17 +0000981 // <binop> storeval, oldval, incr
982 // sc success, storeval, 0(ptr)
983 // beq success, $0, loopMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000984 BB = loopMBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000985 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000986 if (Nand) {
Akira Hatanaka0e019592011-07-19 20:11:17 +0000987 // and andres, oldval, incr
988 // nor storeval, $0, andres
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000989 BuildMI(BB, DL, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
990 BuildMI(BB, DL, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000991 } else if (BinOpcode) {
Akira Hatanaka0e019592011-07-19 20:11:17 +0000992 // <binop> storeval, oldval, incr
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000993 BuildMI(BB, DL, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000994 } else {
Akira Hatanaka0e019592011-07-19 20:11:17 +0000995 StoreVal = Incr;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000996 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000997 BuildMI(BB, DL, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
998 BuildMI(BB, DL, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000999
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001000 MI->eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001001
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001002 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001003}
1004
1005MachineBasicBlock *
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001006MipsTargetLowering::emitAtomicBinaryPartword(MachineInstr *MI,
Akira Hatanaka15506782011-06-07 18:58:42 +00001007 MachineBasicBlock *BB,
1008 unsigned Size, unsigned BinOpcode,
1009 bool Nand) const {
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001010 assert((Size == 1 || Size == 2) &&
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001011 "Unsupported size for EmitAtomicBinaryPartial.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001012
1013 MachineFunction *MF = BB->getParent();
1014 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1015 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1016 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001017 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001018
1019 unsigned Dest = MI->getOperand(0).getReg();
1020 unsigned Ptr = MI->getOperand(1).getReg();
1021 unsigned Incr = MI->getOperand(2).getReg();
1022
Akira Hatanaka0e019592011-07-19 20:11:17 +00001023 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1024 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001025 unsigned Mask = RegInfo.createVirtualRegister(RC);
1026 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001027 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1028 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001029 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001030 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1031 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1032 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1033 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1034 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka9663dd32011-07-19 20:56:53 +00001035 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001036 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1037 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1038 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1039 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1040 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001041
1042 // insert new blocks after the current block
1043 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1044 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001045 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001046 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1047 MachineFunction::iterator It = BB;
1048 ++It;
1049 MF->insert(It, loopMBB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001050 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001051 MF->insert(It, exitMBB);
1052
1053 // Transfer the remainder of BB and its successor edges to exitMBB.
1054 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00001055 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001056 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1057
Akira Hatanaka08636b42011-07-19 17:09:53 +00001058 BB->addSuccessor(loopMBB);
1059 loopMBB->addSuccessor(loopMBB);
1060 loopMBB->addSuccessor(sinkMBB);
1061 sinkMBB->addSuccessor(exitMBB);
1062
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001063 // thisMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001064 // addiu masklsb2,$0,-4 # 0xfffffffc
1065 // and alignedaddr,ptr,masklsb2
1066 // andi ptrlsb2,ptr,3
1067 // sll shiftamt,ptrlsb2,3
1068 // ori maskupper,$0,255 # 0xff
1069 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001070 // nor mask2,$0,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001071 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001072
1073 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001074 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001075 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001076 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001077 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001078 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Akira Hatanaka2bf97332013-05-31 03:25:44 +00001079 if (Subtarget->isLittle()) {
1080 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1081 } else {
1082 unsigned Off = RegInfo.createVirtualRegister(RC);
1083 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1084 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1085 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1086 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001087 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001088 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001089 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001090 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001091 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001092 BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(Incr).addReg(ShiftAmt);
Bruno Cardoso Lopesf771a0f2011-05-31 20:25:26 +00001093
Akira Hatanaka27292632011-07-18 18:52:12 +00001094 // atomic.load.binop
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001095 // loopMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001096 // ll oldval,0(alignedaddr)
1097 // binop binopres,oldval,incr2
1098 // and newval,binopres,mask
1099 // and maskedoldval0,oldval,mask2
1100 // or storeval,maskedoldval0,newval
1101 // sc success,storeval,0(alignedaddr)
1102 // beq success,$0,loopMBB
1103
Akira Hatanaka27292632011-07-18 18:52:12 +00001104 // atomic.swap
1105 // loopMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001106 // ll oldval,0(alignedaddr)
Akira Hatanakae4503582011-07-19 18:14:26 +00001107 // and newval,incr2,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001108 // and maskedoldval0,oldval,mask2
1109 // or storeval,maskedoldval0,newval
1110 // sc success,storeval,0(alignedaddr)
1111 // beq success,$0,loopMBB
Akira Hatanaka27292632011-07-18 18:52:12 +00001112
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001113 BB = loopMBB;
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001114 BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001115 if (Nand) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001116 // and andres, oldval, incr2
1117 // nor binopres, $0, andres
1118 // and newval, binopres, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001119 BuildMI(BB, DL, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1120 BuildMI(BB, DL, TII->get(Mips::NOR), BinOpRes)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001121 .addReg(Mips::ZERO).addReg(AndRes);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001122 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001123 } else if (BinOpcode) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001124 // <binop> binopres, oldval, incr2
1125 // and newval, binopres, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001126 BuildMI(BB, DL, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1127 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001128 } else { // atomic.swap
Akira Hatanaka0e019592011-07-19 20:11:17 +00001129 // and newval, incr2, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001130 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanakae4503582011-07-19 18:14:26 +00001131 }
Jia Liuf54f60f2012-02-28 07:46:26 +00001132
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001133 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001134 .addReg(OldVal).addReg(Mask2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001135 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka9663dd32011-07-19 20:56:53 +00001136 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001137 BuildMI(BB, DL, TII->get(Mips::SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001138 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001139 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001140 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001141
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001142 // sinkMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001143 // and maskedoldval1,oldval,mask
1144 // srl srlres,maskedoldval1,shiftamt
1145 // sll sllres,srlres,24
1146 // sra dest,sllres,24
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001147 BB = sinkMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001148 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakae97bd812011-07-19 03:14:58 +00001149
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001150 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001151 .addReg(OldVal).addReg(Mask);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001152 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001153 .addReg(MaskedOldVal1).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001154 BuildMI(BB, DL, TII->get(Mips::SLL), SllRes)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001155 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001156 BuildMI(BB, DL, TII->get(Mips::SRA), Dest)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001157 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001158
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001159 MI->eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001160
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001161 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001162}
1163
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001164MachineBasicBlock * MipsTargetLowering::emitAtomicCmpSwap(MachineInstr *MI,
1165 MachineBasicBlock *BB,
1166 unsigned Size) const {
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001167 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001168
1169 MachineFunction *MF = BB->getParent();
1170 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001171 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001172 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001173 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001174 unsigned LL, SC, ZERO, BNE, BEQ;
1175
1176 if (Size == 4) {
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001177 LL = Mips::LL;
1178 SC = Mips::SC;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001179 ZERO = Mips::ZERO;
1180 BNE = Mips::BNE;
1181 BEQ = Mips::BEQ;
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001182 } else {
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001183 LL = Mips::LLD;
1184 SC = Mips::SCD;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001185 ZERO = Mips::ZERO_64;
1186 BNE = Mips::BNE64;
1187 BEQ = Mips::BEQ64;
1188 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001189
1190 unsigned Dest = MI->getOperand(0).getReg();
1191 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka0e019592011-07-19 20:11:17 +00001192 unsigned OldVal = MI->getOperand(2).getReg();
1193 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001194
Akira Hatanaka0e019592011-07-19 20:11:17 +00001195 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001196
1197 // insert new blocks after the current block
1198 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1199 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1200 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1201 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1202 MachineFunction::iterator It = BB;
1203 ++It;
1204 MF->insert(It, loop1MBB);
1205 MF->insert(It, loop2MBB);
1206 MF->insert(It, exitMBB);
1207
1208 // Transfer the remainder of BB and its successor edges to exitMBB.
1209 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00001210 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001211 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1212
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001213 // thisMBB:
1214 // ...
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001215 // fallthrough --> loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001216 BB->addSuccessor(loop1MBB);
Akira Hatanaka08636b42011-07-19 17:09:53 +00001217 loop1MBB->addSuccessor(exitMBB);
1218 loop1MBB->addSuccessor(loop2MBB);
1219 loop2MBB->addSuccessor(loop1MBB);
1220 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001221
1222 // loop1MBB:
1223 // ll dest, 0(ptr)
1224 // bne dest, oldval, exitMBB
1225 BB = loop1MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001226 BuildMI(BB, DL, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1227 BuildMI(BB, DL, TII->get(BNE))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001228 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001229
1230 // loop2MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001231 // sc success, newval, 0(ptr)
1232 // beq success, $0, loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001233 BB = loop2MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001234 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001235 .addReg(NewVal).addReg(Ptr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001236 BuildMI(BB, DL, TII->get(BEQ))
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001237 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001238
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001239 MI->eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001240
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001241 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001242}
1243
1244MachineBasicBlock *
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001245MipsTargetLowering::emitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka15506782011-06-07 18:58:42 +00001246 MachineBasicBlock *BB,
1247 unsigned Size) const {
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001248 assert((Size == 1 || Size == 2) &&
1249 "Unsupported size for EmitAtomicCmpSwapPartial.");
1250
1251 MachineFunction *MF = BB->getParent();
1252 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1253 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1254 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001255 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001256
1257 unsigned Dest = MI->getOperand(0).getReg();
1258 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka0e019592011-07-19 20:11:17 +00001259 unsigned CmpVal = MI->getOperand(2).getReg();
1260 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001261
Akira Hatanaka0e019592011-07-19 20:11:17 +00001262 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1263 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001264 unsigned Mask = RegInfo.createVirtualRegister(RC);
1265 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001266 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1267 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1268 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1269 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1270 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1271 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1272 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1273 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1274 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1275 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1276 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1277 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1278 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1279 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001280
1281 // insert new blocks after the current block
1282 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1283 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1284 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001285 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001286 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1287 MachineFunction::iterator It = BB;
1288 ++It;
1289 MF->insert(It, loop1MBB);
1290 MF->insert(It, loop2MBB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001291 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001292 MF->insert(It, exitMBB);
1293
1294 // Transfer the remainder of BB and its successor edges to exitMBB.
1295 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00001296 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001297 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1298
Akira Hatanaka08636b42011-07-19 17:09:53 +00001299 BB->addSuccessor(loop1MBB);
1300 loop1MBB->addSuccessor(sinkMBB);
1301 loop1MBB->addSuccessor(loop2MBB);
1302 loop2MBB->addSuccessor(loop1MBB);
1303 loop2MBB->addSuccessor(sinkMBB);
1304 sinkMBB->addSuccessor(exitMBB);
1305
Akira Hatanakae4503582011-07-19 18:14:26 +00001306 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001307 // thisMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001308 // addiu masklsb2,$0,-4 # 0xfffffffc
1309 // and alignedaddr,ptr,masklsb2
1310 // andi ptrlsb2,ptr,3
1311 // sll shiftamt,ptrlsb2,3
1312 // ori maskupper,$0,255 # 0xff
1313 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001314 // nor mask2,$0,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001315 // andi maskedcmpval,cmpval,255
1316 // sll shiftedcmpval,maskedcmpval,shiftamt
1317 // andi maskednewval,newval,255
1318 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001319 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001320 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001321 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001322 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001323 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001324 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Akira Hatanaka2bf97332013-05-31 03:25:44 +00001325 if (Subtarget->isLittle()) {
1326 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1327 } else {
1328 unsigned Off = RegInfo.createVirtualRegister(RC);
1329 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1330 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1331 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1332 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001333 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001334 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001335 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001336 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001337 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1338 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001339 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001340 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001341 .addReg(MaskedCmpVal).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001342 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001343 .addReg(NewVal).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001344 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001345 .addReg(MaskedNewVal).addReg(ShiftAmt);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001346
1347 // loop1MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001348 // ll oldval,0(alginedaddr)
1349 // and maskedoldval0,oldval,mask
1350 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001351 BB = loop1MBB;
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001352 BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001353 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001354 .addReg(OldVal).addReg(Mask);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001355 BuildMI(BB, DL, TII->get(Mips::BNE))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001356 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001357
1358 // loop2MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001359 // and maskedoldval1,oldval,mask2
1360 // or storeval,maskedoldval1,shiftednewval
1361 // sc success,storeval,0(alignedaddr)
1362 // beq success,$0,loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001363 BB = loop2MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001364 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001365 .addReg(OldVal).addReg(Mask2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001366 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001367 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001368 BuildMI(BB, DL, TII->get(Mips::SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001369 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001370 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001371 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001372
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001373 // sinkMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001374 // srl srlres,maskedoldval0,shiftamt
1375 // sll sllres,srlres,24
1376 // sra dest,sllres,24
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001377 BB = sinkMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001378 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakae97bd812011-07-19 03:14:58 +00001379
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001380 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001381 .addReg(MaskedOldVal0).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001382 BuildMI(BB, DL, TII->get(Mips::SLL), SllRes)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001383 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001384 BuildMI(BB, DL, TII->get(Mips::SRA), Dest)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001385 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001386
1387 MI->eraseFromParent(); // The instruction is gone now.
1388
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001389 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001390}
1391
Akira Hatanakae2489122011-04-15 21:51:11 +00001392//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001393// Misc Lower Operation implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00001394//===----------------------------------------------------------------------===//
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001395SDValue MipsTargetLowering::lowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001396 SDValue Chain = Op.getOperand(0);
1397 SDValue Table = Op.getOperand(1);
1398 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001399 SDLoc DL(Op);
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001400 EVT PTy = getPointerTy();
1401 unsigned EntrySize =
1402 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(*getDataLayout());
1403
1404 Index = DAG.getNode(ISD::MUL, DL, PTy, Index,
1405 DAG.getConstant(EntrySize, PTy));
1406 SDValue Addr = DAG.getNode(ISD::ADD, DL, PTy, Index, Table);
1407
1408 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
1409 Addr = DAG.getExtLoad(ISD::SEXTLOAD, DL, PTy, Chain, Addr,
1410 MachinePointerInfo::getJumpTable(), MemVT, false, false,
1411 0);
1412 Chain = Addr.getValue(1);
1413
1414 if ((getTargetMachine().getRelocationModel() == Reloc::PIC_) || IsN64) {
1415 // For PIC, the sequence is:
1416 // BRIND(load(Jumptable + index) + RelocBase)
1417 // RelocBase can be JumpTable, GOT or some sort of global base.
1418 Addr = DAG.getNode(ISD::ADD, DL, PTy, Addr,
1419 getPICJumpTableRelocBase(Table, DAG));
1420 }
1421
1422 return DAG.getNode(ISD::BRIND, DL, MVT::Other, Chain, Addr);
1423}
1424
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001425SDValue MipsTargetLowering::lowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Wesley Peck527da1b2010-11-23 03:31:01 +00001426 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001427 // the block to branch to if the condition is true.
1428 SDValue Chain = Op.getOperand(0);
1429 SDValue Dest = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001430 SDLoc DL(Op);
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001431
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001432 SDValue CondRes = createFPCmp(DAG, Op.getOperand(1));
Akira Hatanakaa5352702011-03-31 18:26:17 +00001433
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001434 // Return if flag is not set by a floating point comparison.
Akira Hatanakaa5352702011-03-31 18:26:17 +00001435 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopesa9504222008-07-30 17:06:13 +00001436 return Op;
Wesley Peck527da1b2010-11-23 03:31:01 +00001437
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +00001438 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmaneffb8942008-09-12 16:56:44 +00001439 Mips::CondCode CC =
1440 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Akira Hatanakaf0ea5002013-03-30 01:16:38 +00001441 unsigned Opc = invertFPCondCodeUser(CC) ? Mips::BRANCH_F : Mips::BRANCH_T;
1442 SDValue BrCode = DAG.getConstant(Opc, MVT::i32);
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +00001443 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001444 return DAG.getNode(MipsISD::FPBrcond, DL, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +00001445 FCC0, Dest, CondRes);
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001446}
1447
1448SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001449lowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001450{
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001451 SDValue Cond = createFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001452
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001453 // Return if flag is not set by a floating point comparison.
Akira Hatanakaa5352702011-03-31 18:26:17 +00001454 if (Cond.getOpcode() != MipsISD::FPCmp)
1455 return Op;
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +00001456
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001457 return createCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
Andrew Trickef9de2a2013-05-25 02:42:55 +00001458 SDLoc(Op));
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001459}
1460
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001461SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001462lowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001463{
Andrew Trickef9de2a2013-05-25 02:42:55 +00001464 SDLoc DL(Op);
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001465 EVT Ty = Op.getOperand(0).getValueType();
Matt Arsenault758659232013-05-18 00:21:46 +00001466 SDValue Cond = DAG.getNode(ISD::SETCC, DL,
1467 getSetCCResultType(*DAG.getContext(), Ty),
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001468 Op.getOperand(0), Op.getOperand(1),
1469 Op.getOperand(4));
1470
1471 return DAG.getNode(ISD::SELECT, DL, Op.getValueType(), Cond, Op.getOperand(2),
1472 Op.getOperand(3));
1473}
1474
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001475SDValue MipsTargetLowering::lowerSETCC(SDValue Op, SelectionDAG &DAG) const {
1476 SDValue Cond = createFPCmp(DAG, Op);
Akira Hatanakab7f78592012-03-09 23:46:03 +00001477
1478 assert(Cond.getOpcode() == MipsISD::FPCmp &&
1479 "Floating point operand expected.");
1480
1481 SDValue True = DAG.getConstant(1, MVT::i32);
1482 SDValue False = DAG.getConstant(0, MVT::i32);
1483
Andrew Trickef9de2a2013-05-25 02:42:55 +00001484 return createCMovFP(DAG, Cond, True, False, SDLoc(Op));
Akira Hatanakab7f78592012-03-09 23:46:03 +00001485}
1486
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001487SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001488 SelectionDAG &DAG) const {
Dale Johannesen400dc2e2009-02-06 21:50:26 +00001489 // FIXME there isn't actually debug info here
Andrew Trickef9de2a2013-05-25 02:42:55 +00001490 SDLoc DL(Op);
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001491 EVT Ty = Op.getValueType();
1492 GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
1493 const GlobalValue *GV = N->getGlobal();
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001494
Akira Hatanaka09b23eb2011-10-11 00:55:05 +00001495 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
Akira Hatanaka92a96e12012-09-12 23:27:55 +00001496 const MipsTargetObjectFile &TLOF =
1497 (const MipsTargetObjectFile&)getObjFileLowering();
Wesley Peck527da1b2010-11-23 03:31:01 +00001498
Chris Lattner58e8be82009-08-13 05:41:27 +00001499 // %gp_rel relocation
Wesley Peck527da1b2010-11-23 03:31:01 +00001500 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001501 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, 0,
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +00001502 MipsII::MO_GPREL);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001503 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, DL,
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001504 DAG.getVTList(MVT::i32), &GA, 1);
Akira Hatanakaad495022012-08-22 03:18:13 +00001505 SDValue GPReg = DAG.getRegister(Mips::GP, MVT::i32);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001506 return DAG.getNode(ISD::ADD, DL, MVT::i32, GPReg, GPRelNode);
Chris Lattner58e8be82009-08-13 05:41:27 +00001507 }
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001508
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001509 // %hi/%lo relocation
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001510 return getAddrNonPIC(N, Ty, DAG);
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001511 }
1512
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001513 if (GV->hasInternalLinkage() || (GV->hasLocalLinkage() && !isa<Function>(GV)))
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001514 return getAddrLocal(N, Ty, DAG, HasMips64);
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001515
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00001516 if (LargeGOT)
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001517 return getAddrGlobalLargeGOT(N, Ty, DAG, MipsII::MO_GOT_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00001518 MipsII::MO_GOT_LO16, DAG.getEntryNode(),
1519 MachinePointerInfo::getGOT());
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00001520
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001521 return getAddrGlobal(N, Ty, DAG,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00001522 HasMips64 ? MipsII::MO_GOT_DISP : MipsII::MO_GOT16,
1523 DAG.getEntryNode(), MachinePointerInfo::getGOT());
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001524}
1525
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001526SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op,
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +00001527 SelectionDAG &DAG) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001528 BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op);
1529 EVT Ty = Op.getValueType();
Akira Hatanaka30f97cf2013-09-25 00:30:25 +00001530
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001531 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
1532 return getAddrNonPIC(N, Ty, DAG);
1533
1534 return getAddrLocal(N, Ty, DAG, HasMips64);
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +00001535}
1536
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001537SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001538lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001539{
Akira Hatanakabff84e12011-12-14 18:26:41 +00001540 // If the relocation model is PIC, use the General Dynamic TLS Model or
1541 // Local Dynamic TLS model, otherwise use the Initial Exec or
1542 // Local Exec TLS Model.
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001543
1544 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001545 SDLoc DL(GA);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001546 const GlobalValue *GV = GA->getGlobal();
1547 EVT PtrVT = getPointerTy();
1548
Hans Wennborgaea41202012-05-04 09:40:39 +00001549 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
1550
1551 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Hans Wennborg245917b2012-06-04 14:02:08 +00001552 // General Dynamic and Local Dynamic TLS Model.
1553 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
1554 : MipsII::MO_TLSGD;
1555
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001556 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag);
1557 SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT,
1558 getGlobalReg(DAG, PtrVT), TGA);
Akira Hatanakaf10ee842011-12-08 21:05:38 +00001559 unsigned PtrSize = PtrVT.getSizeInBits();
1560 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1561
Benjamin Kramer64ba50a2011-12-11 12:21:34 +00001562 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001563
1564 ArgListTy Args;
1565 ArgListEntry Entry;
1566 Entry.Node = Argument;
Akira Hatanakadee6c822011-12-08 20:34:32 +00001567 Entry.Ty = PtrTy;
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001568 Args.push_back(Entry);
Jia Liuf54f60f2012-02-28 07:46:26 +00001569
Justin Holewinskiaa583972012-05-25 16:35:28 +00001570 TargetLowering::CallLoweringInfo CLI(DAG.getEntryNode(), PtrTy,
Evan Cheng65f9d192012-02-28 18:51:51 +00001571 false, false, false, false, 0, CallingConv::C,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001572 /*IsTailCall=*/false, /*doesNotRet=*/false,
Evan Cheng65f9d192012-02-28 18:51:51 +00001573 /*isReturnValueUsed=*/true,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001574 TlsGetAddr, Args, DAG, DL);
Justin Holewinskiaa583972012-05-25 16:35:28 +00001575 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001576
Akira Hatanakabff84e12011-12-14 18:26:41 +00001577 SDValue Ret = CallResult.first;
1578
Hans Wennborgaea41202012-05-04 09:40:39 +00001579 if (model != TLSModel::LocalDynamic)
Akira Hatanakabff84e12011-12-14 18:26:41 +00001580 return Ret;
1581
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001582 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanakabff84e12011-12-14 18:26:41 +00001583 MipsII::MO_DTPREL_HI);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001584 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1585 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanakabff84e12011-12-14 18:26:41 +00001586 MipsII::MO_DTPREL_LO);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001587 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1588 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret);
1589 return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001590 }
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001591
1592 SDValue Offset;
Hans Wennborgaea41202012-05-04 09:40:39 +00001593 if (model == TLSModel::InitialExec) {
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001594 // Initial Exec TLS Model
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001595 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001596 MipsII::MO_GOTTPREL);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001597 TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT),
Akira Hatanakab049aef2012-02-24 22:34:47 +00001598 TGA);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001599 Offset = DAG.getLoad(PtrVT, DL,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001600 DAG.getEntryNode(), TGA, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001601 false, false, false, 0);
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001602 } else {
1603 // Local Exec TLS Model
Hans Wennborgaea41202012-05-04 09:40:39 +00001604 assert(model == TLSModel::LocalExec);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001605 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001606 MipsII::MO_TPREL_HI);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001607 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001608 MipsII::MO_TPREL_LO);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001609 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1610 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1611 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001612 }
1613
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001614 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
1615 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001616}
1617
1618SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001619lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +00001620{
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001621 JumpTableSDNode *N = cast<JumpTableSDNode>(Op);
1622 EVT Ty = Op.getValueType();
Akira Hatanaka30f97cf2013-09-25 00:30:25 +00001623
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001624 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
1625 return getAddrNonPIC(N, Ty, DAG);
1626
1627 return getAddrLocal(N, Ty, DAG, HasMips64);
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +00001628}
1629
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001630SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001631lowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +00001632{
Bruno Cardoso Lopesfdb4cec2008-07-23 16:01:50 +00001633 // gp_rel relocation
Wesley Peck527da1b2010-11-23 03:31:01 +00001634 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001635 // but the asm printer currently doesn't support this feature without
Wesley Peck527da1b2010-11-23 03:31:01 +00001636 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopes98bda582008-07-28 19:26:25 +00001637 // stuff below.
Eli Friedman57c11da2009-08-03 02:22:28 +00001638 //if (IsInSmallSection(C->getType())) {
Owen Anderson9f944592009-08-11 20:47:22 +00001639 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1640 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peck527da1b2010-11-23 03:31:01 +00001641 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001642 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
1643 EVT Ty = Op.getValueType();
Bruno Cardoso Lopes2db07582009-11-25 12:17:58 +00001644
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001645 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001646 return getAddrNonPIC(N, Ty, DAG);
Bruno Cardoso Lopesfdb4cec2008-07-23 16:01:50 +00001647
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001648 return getAddrLocal(N, Ty, DAG, HasMips64);
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +00001649}
1650
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001651SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman31ae5862010-04-17 14:41:14 +00001652 MachineFunction &MF = DAG.getMachineFunction();
1653 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1654
Andrew Trickef9de2a2013-05-25 02:42:55 +00001655 SDLoc DL(Op);
Dan Gohman31ae5862010-04-17 14:41:14 +00001656 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1657 getPointerTy());
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +00001658
1659 // vastart just stores the address of the VarArgsFrameIndex slot into the
1660 // memory location argument.
1661 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001662 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00001663 MachinePointerInfo(SV), false, false, 0);
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +00001664}
Jia Liuf54f60f2012-02-28 07:46:26 +00001665
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001666static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG,
1667 bool HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001668 EVT TyX = Op.getOperand(0).getValueType();
1669 EVT TyY = Op.getOperand(1).getValueType();
1670 SDValue Const1 = DAG.getConstant(1, MVT::i32);
1671 SDValue Const31 = DAG.getConstant(31, MVT::i32);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001672 SDLoc DL(Op);
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001673 SDValue Res;
1674
1675 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1676 // to i32.
1677 SDValue X = (TyX == MVT::f32) ?
1678 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1679 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1680 Const1);
1681 SDValue Y = (TyY == MVT::f32) ?
1682 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
1683 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
1684 Const1);
1685
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001686 if (HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001687 // ext E, Y, 31, 1 ; extract bit31 of Y
1688 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
1689 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
1690 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
1691 } else {
1692 // sll SllX, X, 1
1693 // srl SrlX, SllX, 1
1694 // srl SrlY, Y, 31
1695 // sll SllY, SrlX, 31
1696 // or Or, SrlX, SllY
1697 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1698 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1699 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
1700 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
1701 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
1702 }
1703
1704 if (TyX == MVT::f32)
1705 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
1706
1707 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1708 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1709 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001710}
1711
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001712static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG,
1713 bool HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001714 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
1715 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
1716 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
1717 SDValue Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001718 SDLoc DL(Op);
Eric Christopher0713a9d2011-06-08 23:55:35 +00001719
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001720 // Bitcast to integer nodes.
1721 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
1722 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001723
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001724 if (HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001725 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
1726 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
1727 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
1728 DAG.getConstant(WidthY - 1, MVT::i32), Const1);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001729
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001730 if (WidthX > WidthY)
1731 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
1732 else if (WidthY > WidthX)
1733 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001734
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001735 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
1736 DAG.getConstant(WidthX - 1, MVT::i32), Const1, X);
1737 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
1738 }
1739
1740 // (d)sll SllX, X, 1
1741 // (d)srl SrlX, SllX, 1
1742 // (d)srl SrlY, Y, width(Y)-1
1743 // (d)sll SllY, SrlX, width(Y)-1
1744 // or Or, SrlX, SllY
1745 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
1746 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
1747 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
1748 DAG.getConstant(WidthY - 1, MVT::i32));
1749
1750 if (WidthX > WidthY)
1751 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
1752 else if (WidthY > WidthX)
1753 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
1754
1755 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
1756 DAG.getConstant(WidthX - 1, MVT::i32));
1757 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
1758 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001759}
1760
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00001761SDValue
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001762MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001763 if (Subtarget->hasMips64())
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001764 return lowerFCOPYSIGN64(Op, DAG, Subtarget->hasExtractInsert());
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001765
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001766 return lowerFCOPYSIGN32(Op, DAG, Subtarget->hasExtractInsert());
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001767}
1768
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001769static SDValue lowerFABS32(SDValue Op, SelectionDAG &DAG,
1770 bool HasExtractInsert) {
Akira Hatanaka7f4c9d12012-04-11 22:49:04 +00001771 SDValue Res, Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001772 SDLoc DL(Op);
Akira Hatanaka7f4c9d12012-04-11 22:49:04 +00001773
1774 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1775 // to i32.
1776 SDValue X = (Op.getValueType() == MVT::f32) ?
1777 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1778 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1779 Const1);
1780
1781 // Clear MSB.
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001782 if (HasExtractInsert)
Akira Hatanaka7f4c9d12012-04-11 22:49:04 +00001783 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32,
1784 DAG.getRegister(Mips::ZERO, MVT::i32),
1785 DAG.getConstant(31, MVT::i32), Const1, X);
1786 else {
1787 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1788 Res = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1789 }
1790
1791 if (Op.getValueType() == MVT::f32)
1792 return DAG.getNode(ISD::BITCAST, DL, MVT::f32, Res);
1793
1794 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1795 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1796 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
1797}
1798
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001799static SDValue lowerFABS64(SDValue Op, SelectionDAG &DAG,
1800 bool HasExtractInsert) {
Akira Hatanaka7f4c9d12012-04-11 22:49:04 +00001801 SDValue Res, Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001802 SDLoc DL(Op);
Akira Hatanaka7f4c9d12012-04-11 22:49:04 +00001803
1804 // Bitcast to integer node.
1805 SDValue X = DAG.getNode(ISD::BITCAST, DL, MVT::i64, Op.getOperand(0));
1806
1807 // Clear MSB.
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001808 if (HasExtractInsert)
Akira Hatanaka7f4c9d12012-04-11 22:49:04 +00001809 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i64,
1810 DAG.getRegister(Mips::ZERO_64, MVT::i64),
1811 DAG.getConstant(63, MVT::i32), Const1, X);
1812 else {
1813 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i64, X, Const1);
1814 Res = DAG.getNode(ISD::SRL, DL, MVT::i64, SllX, Const1);
1815 }
1816
1817 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, Res);
1818}
1819
1820SDValue
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001821MipsTargetLowering::lowerFABS(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka7f4c9d12012-04-11 22:49:04 +00001822 if (Subtarget->hasMips64() && (Op.getValueType() == MVT::f64))
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001823 return lowerFABS64(Op, DAG, Subtarget->hasExtractInsert());
Akira Hatanaka7f4c9d12012-04-11 22:49:04 +00001824
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001825 return lowerFABS32(Op, DAG, Subtarget->hasExtractInsert());
Akira Hatanaka7f4c9d12012-04-11 22:49:04 +00001826}
1827
Akira Hatanaka66277522011-06-02 00:24:44 +00001828SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001829lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes5444a7b2011-06-16 00:40:02 +00001830 // check the depth
1831 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka15506782011-06-07 18:58:42 +00001832 "Frame address can only be determined for current frame.");
Akira Hatanaka66277522011-06-02 00:24:44 +00001833
1834 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1835 MFI->setFrameAddressIsTaken(true);
1836 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001837 SDLoc DL(Op);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001838 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), DL,
Akira Hatanaka9189d712011-11-11 04:11:56 +00001839 IsN64 ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka66277522011-06-02 00:24:44 +00001840 return FrameAddr;
1841}
1842
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001843SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op,
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00001844 SelectionDAG &DAG) const {
1845 // check the depth
1846 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
1847 "Return address can be determined only for current frame.");
1848
1849 MachineFunction &MF = DAG.getMachineFunction();
1850 MachineFrameInfo *MFI = MF.getFrameInfo();
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001851 MVT VT = Op.getSimpleValueType();
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00001852 unsigned RA = IsN64 ? Mips::RA_64 : Mips::RA;
1853 MFI->setReturnAddressIsTaken(true);
1854
1855 // Return RA, which contains the return address. Mark it an implicit live-in.
1856 unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00001857 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT);
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00001858}
1859
Akira Hatanakac0b02062013-01-30 00:26:49 +00001860// An EH_RETURN is the result of lowering llvm.eh.return which in turn is
1861// generated from __builtin_eh_return (offset, handler)
1862// The effect of this is to adjust the stack pointer by "offset"
1863// and then branch to "handler".
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001864SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Akira Hatanakac0b02062013-01-30 00:26:49 +00001865 const {
1866 MachineFunction &MF = DAG.getMachineFunction();
1867 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1868
1869 MipsFI->setCallsEhReturn();
1870 SDValue Chain = Op.getOperand(0);
1871 SDValue Offset = Op.getOperand(1);
1872 SDValue Handler = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001873 SDLoc DL(Op);
Akira Hatanakac0b02062013-01-30 00:26:49 +00001874 EVT Ty = IsN64 ? MVT::i64 : MVT::i32;
1875
1876 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
1877 // EH_RETURN nodes, so that instructions are emitted back-to-back.
1878 unsigned OffsetReg = IsN64 ? Mips::V1_64 : Mips::V1;
1879 unsigned AddrReg = IsN64 ? Mips::V0_64 : Mips::V0;
1880 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
1881 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
1882 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
1883 DAG.getRegister(OffsetReg, Ty),
1884 DAG.getRegister(AddrReg, getPointerTy()),
1885 Chain.getValue(1));
1886}
1887
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001888SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
Akira Hatanaka5fd22482012-06-14 21:10:56 +00001889 SelectionDAG &DAG) const {
Eli Friedman26a48482011-07-27 22:21:52 +00001890 // FIXME: Need pseudo-fence for 'singlethread' fences
1891 // FIXME: Set SType for weaker fences where supported/appropriate.
1892 unsigned SType = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001893 SDLoc DL(Op);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001894 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
Eli Friedman26a48482011-07-27 22:21:52 +00001895 DAG.getConstant(SType, MVT::i32));
1896}
1897
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001898SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
Akira Hatanaka5fd22482012-06-14 21:10:56 +00001899 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001900 SDLoc DL(Op);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00001901 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
1902 SDValue Shamt = Op.getOperand(2);
1903
1904 // if shamt < 32:
1905 // lo = (shl lo, shamt)
1906 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
1907 // else:
1908 // lo = 0
1909 // hi = (shl lo, shamt[4:0])
1910 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
1911 DAG.getConstant(-1, MVT::i32));
1912 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo,
1913 DAG.getConstant(1, MVT::i32));
1914 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, ShiftRight1Lo,
1915 Not);
1916 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi, Shamt);
1917 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
1918 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, MVT::i32, Lo, Shamt);
1919 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
1920 DAG.getConstant(0x20, MVT::i32));
Akira Hatanaka5fd22482012-06-14 21:10:56 +00001921 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
1922 DAG.getConstant(0, MVT::i32), ShiftLeftLo);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00001923 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftLeftLo, Or);
1924
1925 SDValue Ops[2] = {Lo, Hi};
1926 return DAG.getMergeValues(Ops, 2, DL);
1927}
1928
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001929SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00001930 bool IsSRA) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001931 SDLoc DL(Op);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00001932 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
1933 SDValue Shamt = Op.getOperand(2);
1934
1935 // if shamt < 32:
1936 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
1937 // if isSRA:
1938 // hi = (sra hi, shamt)
1939 // else:
1940 // hi = (srl hi, shamt)
1941 // else:
1942 // if isSRA:
1943 // lo = (sra hi, shamt[4:0])
1944 // hi = (sra hi, 31)
1945 // else:
1946 // lo = (srl hi, shamt[4:0])
1947 // hi = 0
1948 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
1949 DAG.getConstant(-1, MVT::i32));
1950 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi,
1951 DAG.getConstant(1, MVT::i32));
1952 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, ShiftLeft1Hi, Not);
1953 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo, Shamt);
1954 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
1955 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL, DL, MVT::i32,
1956 Hi, Shamt);
1957 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
1958 DAG.getConstant(0x20, MVT::i32));
1959 SDValue Shift31 = DAG.getNode(ISD::SRA, DL, MVT::i32, Hi,
1960 DAG.getConstant(31, MVT::i32));
1961 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftRightHi, Or);
1962 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
1963 IsSRA ? Shift31 : DAG.getConstant(0, MVT::i32),
1964 ShiftRightHi);
1965
1966 SDValue Ops[2] = {Lo, Hi};
1967 return DAG.getMergeValues(Ops, 2, DL);
1968}
1969
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00001970static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00001971 SDValue Chain, SDValue Src, unsigned Offset) {
Akira Hatanaka95866182012-06-13 19:06:08 +00001972 SDValue Ptr = LD->getBasePtr();
Akira Hatanaka8f1db772012-06-02 00:03:49 +00001973 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
Akira Hatanaka95866182012-06-13 19:06:08 +00001974 EVT BasePtrVT = Ptr.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001975 SDLoc DL(LD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00001976 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
1977
1978 if (Offset)
Akira Hatanaka95866182012-06-13 19:06:08 +00001979 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00001980 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka8f1db772012-06-02 00:03:49 +00001981
1982 SDValue Ops[] = { Chain, Ptr, Src };
1983 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT,
1984 LD->getMemOperand());
1985}
1986
1987// Expand an unaligned 32 or 64-bit integer load node.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001988SDValue MipsTargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka8f1db772012-06-02 00:03:49 +00001989 LoadSDNode *LD = cast<LoadSDNode>(Op);
1990 EVT MemVT = LD->getMemoryVT();
1991
1992 // Return if load is aligned or if MemVT is neither i32 nor i64.
1993 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
1994 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
1995 return SDValue();
1996
1997 bool IsLittle = Subtarget->isLittle();
1998 EVT VT = Op.getValueType();
1999 ISD::LoadExtType ExtType = LD->getExtensionType();
2000 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
2001
2002 assert((VT == MVT::i32) || (VT == MVT::i64));
2003
2004 // Expand
2005 // (set dst, (i64 (load baseptr)))
2006 // to
2007 // (set tmp, (ldl (add baseptr, 7), undef))
2008 // (set dst, (ldr baseptr, tmp))
2009 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002010 SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002011 IsLittle ? 7 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002012 return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002013 IsLittle ? 0 : 7);
2014 }
2015
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002016 SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002017 IsLittle ? 3 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002018 SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002019 IsLittle ? 0 : 3);
2020
2021 // Expand
2022 // (set dst, (i32 (load baseptr))) or
2023 // (set dst, (i64 (sextload baseptr))) or
2024 // (set dst, (i64 (extload baseptr)))
2025 // to
2026 // (set tmp, (lwl (add baseptr, 3), undef))
2027 // (set dst, (lwr baseptr, tmp))
2028 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
2029 (ExtType == ISD::EXTLOAD))
2030 return LWR;
2031
2032 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
2033
2034 // Expand
2035 // (set dst, (i64 (zextload baseptr)))
2036 // to
2037 // (set tmp0, (lwl (add baseptr, 3), undef))
2038 // (set tmp1, (lwr baseptr, tmp0))
2039 // (set tmp2, (shl tmp1, 32))
2040 // (set dst, (srl tmp2, 32))
Andrew Trickef9de2a2013-05-25 02:42:55 +00002041 SDLoc DL(LD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002042 SDValue Const32 = DAG.getConstant(32, MVT::i32);
2043 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
Akira Hatanaka67346852012-06-04 17:46:29 +00002044 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
2045 SDValue Ops[] = { SRL, LWR.getValue(1) };
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002046 return DAG.getMergeValues(Ops, 2, DL);
2047}
2048
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002049static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002050 SDValue Chain, unsigned Offset) {
Akira Hatanaka95866182012-06-13 19:06:08 +00002051 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
2052 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002053 SDLoc DL(SD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002054 SDVTList VTList = DAG.getVTList(MVT::Other);
2055
2056 if (Offset)
Akira Hatanaka95866182012-06-13 19:06:08 +00002057 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002058 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002059
2060 SDValue Ops[] = { Chain, Value, Ptr };
2061 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT,
2062 SD->getMemOperand());
2063}
2064
2065// Expand an unaligned 32 or 64-bit integer store node.
Akira Hatanakad82ee942013-05-16 20:45:17 +00002066static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG,
2067 bool IsLittle) {
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002068 SDValue Value = SD->getValue(), Chain = SD->getChain();
2069 EVT VT = Value.getValueType();
2070
2071 // Expand
2072 // (store val, baseptr) or
2073 // (truncstore val, baseptr)
2074 // to
2075 // (swl val, (add baseptr, 3))
2076 // (swr val, baseptr)
2077 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002078 SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002079 IsLittle ? 3 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002080 return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002081 }
2082
2083 assert(VT == MVT::i64);
2084
2085 // Expand
2086 // (store val, baseptr)
2087 // to
2088 // (sdl val, (add baseptr, 7))
2089 // (sdr val, baseptr)
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002090 SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2091 return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002092}
2093
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002094// Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr).
2095static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG) {
2096 SDValue Val = SD->getValue();
2097
2098 if (Val.getOpcode() != ISD::FP_TO_SINT)
2099 return SDValue();
2100
2101 EVT FPTy = EVT::getFloatingPointVT(Val.getValueSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002102 SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy,
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002103 Val.getOperand(0));
2104
Andrew Trickef9de2a2013-05-25 02:42:55 +00002105 return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(),
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002106 SD->getPointerInfo(), SD->isVolatile(),
2107 SD->isNonTemporal(), SD->getAlignment());
2108}
2109
Akira Hatanakad82ee942013-05-16 20:45:17 +00002110SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2111 StoreSDNode *SD = cast<StoreSDNode>(Op);
2112 EVT MemVT = SD->getMemoryVT();
2113
2114 // Lower unaligned integer stores.
2115 if ((SD->getAlignment() < MemVT.getSizeInBits() / 8) &&
2116 ((MemVT == MVT::i32) || (MemVT == MVT::i64)))
2117 return lowerUnalignedIntStore(SD, DAG, Subtarget->isLittle());
2118
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002119 return lowerFP_TO_SINT_STORE(SD, DAG);
Akira Hatanakad82ee942013-05-16 20:45:17 +00002120}
2121
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002122SDValue MipsTargetLowering::lowerADD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002123 if (Op->getOperand(0).getOpcode() != ISD::FRAMEADDR
2124 || cast<ConstantSDNode>
2125 (Op->getOperand(0).getOperand(0))->getZExtValue() != 0
2126 || Op->getOperand(1).getOpcode() != ISD::FRAME_TO_ARGS_OFFSET)
2127 return SDValue();
2128
2129 // The pattern
2130 // (add (frameaddr 0), (frame_to_args_offset))
2131 // results from lowering llvm.eh.dwarf.cfa intrinsic. Transform it to
2132 // (add FrameObject, 0)
2133 // where FrameObject is a fixed StackObject with offset 0 which points to
2134 // the old stack pointer.
2135 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2136 EVT ValTy = Op->getValueType(0);
2137 int FI = MFI->CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
2138 SDValue InArgsAddr = DAG.getFrameIndex(FI, ValTy);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002139 return DAG.getNode(ISD::ADD, SDLoc(Op), ValTy, InArgsAddr,
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002140 DAG.getConstant(0, ValTy));
2141}
2142
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002143SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op,
2144 SelectionDAG &DAG) const {
2145 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002146 SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy,
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002147 Op.getOperand(0));
Andrew Trickef9de2a2013-05-25 02:42:55 +00002148 return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc);
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002149}
2150
Akira Hatanakae2489122011-04-15 21:51:11 +00002151//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002152// Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002153//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002154
Akira Hatanakae2489122011-04-15 21:51:11 +00002155//===----------------------------------------------------------------------===//
Wesley Peck527da1b2010-11-23 03:31:01 +00002156// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002157// Mips O32 ABI rules:
2158// ---
2159// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peck527da1b2010-11-23 03:31:01 +00002160// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002161// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peck527da1b2010-11-23 03:31:01 +00002162// f64 - Only passed in two aliased f32 registers if no int reg has been used
2163// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002164// not used, it must be shadowed. If only A3 is avaiable, shadow it and
2165// go to stack.
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002166//
2167// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanakae2489122011-04-15 21:51:11 +00002168//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002169
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00002170static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2171 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
2172 CCState &State, const uint16_t *F64Regs) {
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002173
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00002174 static const unsigned IntRegsSize = 4, FloatRegsSize = 2;
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002175
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00002176 static const uint16_t IntRegs[] = { Mips::A0, Mips::A1, Mips::A2, Mips::A3 };
2177 static const uint16_t F32Regs[] = { Mips::F12, Mips::F14 };
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002178
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002179 // Do not process byval args here.
2180 if (ArgFlags.isByVal())
2181 return true;
Akira Hatanaka5e16c6a2011-05-24 19:18:33 +00002182
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002183 // Promote i8 and i16
2184 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
2185 LocVT = MVT::i32;
2186 if (ArgFlags.isSExt())
2187 LocInfo = CCValAssign::SExt;
2188 else if (ArgFlags.isZExt())
2189 LocInfo = CCValAssign::ZExt;
2190 else
2191 LocInfo = CCValAssign::AExt;
2192 }
2193
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002194 unsigned Reg;
2195
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002196 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
2197 // is true: function is vararg, argument is 3rd or higher, there is previous
2198 // argument which is not f32 or f64.
2199 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
2200 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanaka9e6a8cc2011-05-19 20:29:48 +00002201 unsigned OrigAlign = ArgFlags.getOrigAlign();
2202 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002203
2204 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002205 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanaka9e6a8cc2011-05-19 20:29:48 +00002206 // If this is the first part of an i64 arg,
2207 // the allocated register must be either A0 or A2.
2208 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
2209 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002210 LocVT = MVT::i32;
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002211 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
2212 // Allocate int register and shadow next int register. If first
2213 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002214 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2215 if (Reg == Mips::A1 || Reg == Mips::A3)
2216 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2217 State.AllocateReg(IntRegs, IntRegsSize);
2218 LocVT = MVT::i32;
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002219 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
2220 // we are guaranteed to find an available float register
2221 if (ValVT == MVT::f32) {
2222 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
2223 // Shadow int register
2224 State.AllocateReg(IntRegs, IntRegsSize);
2225 } else {
2226 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
2227 // Shadow int registers
2228 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
2229 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
2230 State.AllocateReg(IntRegs, IntRegsSize);
2231 State.AllocateReg(IntRegs, IntRegsSize);
2232 }
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002233 } else
2234 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002235
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002236 if (!Reg) {
2237 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3,
2238 OrigAlign);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002239 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002240 } else
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002241 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002242
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002243 return false;
Akira Hatanaka202f6402011-11-12 02:20:46 +00002244}
2245
Akira Hatanakabfb66242013-08-20 23:38:40 +00002246static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT,
2247 MVT LocVT, CCValAssign::LocInfo LocInfo,
2248 ISD::ArgFlagsTy ArgFlags, CCState &State) {
2249 static const uint16_t F64Regs[] = { Mips::D6, Mips::D7 };
2250
2251 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2252}
2253
2254static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT,
2255 MVT LocVT, CCValAssign::LocInfo LocInfo,
2256 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Akira Hatanakad6c9f6e2013-11-12 22:16:18 +00002257 static const uint16_t F64Regs[] = { Mips::D12_64, Mips::D14_64 };
Akira Hatanakabfb66242013-08-20 23:38:40 +00002258
2259 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2260}
2261
Akira Hatanaka202f6402011-11-12 02:20:46 +00002262#include "MipsGenCallingConv.inc"
2263
Akira Hatanakae2489122011-04-15 21:51:11 +00002264//===----------------------------------------------------------------------===//
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002265// Call Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002266//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002267
Akira Hatanaka61bbcce2011-09-23 00:58:33 +00002268// Return next O32 integer argument register.
2269static unsigned getNextIntArgReg(unsigned Reg) {
2270 assert((Reg == Mips::A0) || (Reg == Mips::A2));
2271 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
2272}
2273
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002274SDValue
2275MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002276 SDValue Chain, SDValue Arg, SDLoc DL,
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002277 bool IsTailCall, SelectionDAG &DAG) const {
2278 if (!IsTailCall) {
2279 SDValue PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr,
2280 DAG.getIntPtrConstant(Offset));
2281 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo(), false,
2282 false, 0);
2283 }
2284
2285 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2286 int FI = MFI->CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
2287 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2288 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(),
2289 /*isVolatile=*/ true, false, 0);
2290}
2291
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002292void MipsTargetLowering::
2293getOpndList(SmallVectorImpl<SDValue> &Ops,
2294 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
2295 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
2296 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
2297 // Insert node "GP copy globalreg" before call to function.
2298 //
2299 // R_MIPS_CALL* operators (emitted when non-internal functions are called
2300 // in PIC mode) allow symbols to be resolved via lazy binding.
2301 // The lazy binding stub requires GP to point to the GOT.
2302 if (IsPICCall && !InternalLinkage) {
2303 unsigned GPReg = IsN64 ? Mips::GP_64 : Mips::GP;
2304 EVT Ty = IsN64 ? MVT::i64 : MVT::i32;
2305 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty)));
2306 }
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002307
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002308 // Build a sequence of copy-to-reg nodes chained together with token
2309 // chain and flag operands which copy the outgoing args into registers.
2310 // The InFlag in necessary since all emitted instructions must be
2311 // stuck together.
2312 SDValue InFlag;
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002313
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002314 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2315 Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, RegsToPass[i].first,
2316 RegsToPass[i].second, InFlag);
2317 InFlag = Chain.getValue(1);
2318 }
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002319
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002320 // Add argument registers to the end of the list so that they are
2321 // known live into the call.
2322 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2323 Ops.push_back(CLI.DAG.getRegister(RegsToPass[i].first,
2324 RegsToPass[i].second.getValueType()));
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002325
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002326 // Add a register mask operand representing the call-preserved registers.
2327 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2328 const uint32_t *Mask = TRI->getCallPreservedMask(CLI.CallConv);
2329 assert(Mask && "Missing call preserved mask for calling convention");
Reed Kotler783c7942013-05-10 22:25:39 +00002330 if (Subtarget->inMips16HardFloat()) {
2331 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
2332 llvm::StringRef Sym = G->getGlobal()->getName();
2333 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
2334 if (F->hasFnAttribute("__Mips16RetHelper")) {
2335 Mask = MipsRegisterInfo::getMips16RetHelperMask();
2336 }
2337 }
2338 }
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002339 Ops.push_back(CLI.DAG.getRegisterMask(Mask));
2340
2341 if (InFlag.getNode())
2342 Ops.push_back(InFlag);
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002343}
2344
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002345/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman624801e2009-01-26 03:15:54 +00002346/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002347SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00002348MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002349 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00002350 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002351 SDLoc DL = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00002352 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
2353 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
2354 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Akira Hatanakabeda2242012-07-31 18:46:41 +00002355 SDValue Chain = CLI.Chain;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002356 SDValue Callee = CLI.Callee;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002357 bool &IsTailCall = CLI.IsTailCall;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002358 CallingConv::ID CallConv = CLI.CallConv;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002359 bool IsVarArg = CLI.IsVarArg;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002360
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002361 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002362 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanaka7c619f12011-05-20 21:39:54 +00002363 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002364 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +00002365 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002366
2367 // Analyze operands of the call, assigning locations to each operand.
2368 SmallVector<CCValAssign, 16> ArgLocs;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002369 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00002370 getTargetMachine(), ArgLocs, *DAG.getContext());
Reed Kotler783c7942013-05-10 22:25:39 +00002371 MipsCC::SpecialCallingConvType SpecialCallingConv =
2372 getSpecialCallingConv(Callee);
Akira Hatanakabfb66242013-08-20 23:38:40 +00002373 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo,
2374 SpecialCallingConv);
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002375
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002376 MipsCCInfo.analyzeCallOperands(Outs, IsVarArg,
Reed Kotlerc03807a2013-08-30 19:40:56 +00002377 Subtarget->mipsSEUsesSoftFloat(),
Akira Hatanaka3b7391d2013-03-05 22:20:28 +00002378 Callee.getNode(), CLI.Args);
Wesley Peck527da1b2010-11-23 03:31:01 +00002379
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002380 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka195a1e22011-06-08 17:39:33 +00002381 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka97ba7692012-07-26 23:27:01 +00002382
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002383 // Check if it's really possible to do a tail call.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002384 if (IsTailCall)
2385 IsTailCall =
2386 isEligibleForTailCallOptimization(MipsCCInfo, NextStackOffset,
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002387 *MF.getInfo<MipsFunctionInfo>());
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002388
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002389 if (IsTailCall)
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002390 ++NumTailCalls;
2391
Akira Hatanaka79738332011-09-19 20:26:02 +00002392 // Chain is the output chain of the last Load/Store or CopyToReg node.
2393 // ByValChain is the output chain of the last Memcpy node created for copying
2394 // byval arguments to the stack.
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002395 unsigned StackAlignment = TFL->getStackAlignment();
2396 NextStackOffset = RoundUpToAlignment(NextStackOffset, StackAlignment);
Akira Hatanaka79738332011-09-19 20:26:02 +00002397 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002398
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002399 if (!IsTailCall)
Andrew Trickad6d08a2013-05-29 22:03:55 +00002400 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffsetVal, DL);
Akira Hatanakabeda2242012-07-31 18:46:41 +00002401
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002402 SDValue StackPtr = DAG.getCopyFromReg(Chain, DL,
Akira Hatanakabeda2242012-07-31 18:46:41 +00002403 IsN64 ? Mips::SP_64 : Mips::SP,
2404 getPointerTy());
Akira Hatanaka195a1e22011-06-08 17:39:33 +00002405
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002406 // With EABI is it possible to have 16 args on registers.
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002407 std::deque< std::pair<unsigned, SDValue> > RegsToPass;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002408 SmallVector<SDValue, 8> MemOpChains;
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002409 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002410
2411 // Walk the register/memloc assignments, inserting copies/loads.
2412 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002413 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002414 CCValAssign &VA = ArgLocs[i];
Akira Hatanakab20a3252011-10-28 19:49:00 +00002415 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka19891f82011-11-12 02:34:50 +00002416 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2417
2418 // ByVal Arg.
2419 if (Flags.isByVal()) {
2420 assert(Flags.getByValSize() &&
2421 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002422 assert(ByValArg != MipsCCInfo.byval_end());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002423 assert(!IsTailCall &&
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002424 "Do not tail-call optimize if there is a byval argument.");
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002425 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002426 MipsCCInfo, *ByValArg, Flags, Subtarget->isLittle());
2427 ++ByValArg;
Akira Hatanaka19891f82011-11-12 02:34:50 +00002428 continue;
2429 }
Jia Liuf54f60f2012-02-28 07:46:26 +00002430
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002431 // Promote the value if needed.
2432 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002433 default: llvm_unreachable("Unknown loc info!");
Wesley Peck527da1b2010-11-23 03:31:01 +00002434 case CCValAssign::Full:
Akira Hatanakab20a3252011-10-28 19:49:00 +00002435 if (VA.isRegLoc()) {
2436 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
Akira Hatanaka3b7391d2013-03-05 22:20:28 +00002437 (ValVT == MVT::f64 && LocVT == MVT::i64) ||
2438 (ValVT == MVT::i64 && LocVT == MVT::f64))
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002439 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
Akira Hatanakab20a3252011-10-28 19:49:00 +00002440 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002441 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanakae2489122011-04-15 21:51:11 +00002442 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002443 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanakaaef55c82011-04-15 21:00:26 +00002444 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka27916972011-04-15 19:52:08 +00002445 if (!Subtarget->isLittle())
2446 std::swap(Lo, Hi);
Jia Liuf54f60f2012-02-28 07:46:26 +00002447 unsigned LocRegLo = VA.getLocReg();
Akira Hatanaka61bbcce2011-09-23 00:58:33 +00002448 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2449 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2450 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002451 continue;
Wesley Peck527da1b2010-11-23 03:31:01 +00002452 }
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002453 }
2454 break;
Chris Lattner52f16de2008-03-17 06:57:02 +00002455 case CCValAssign::SExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002456 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002457 break;
2458 case CCValAssign::ZExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002459 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002460 break;
2461 case CCValAssign::AExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002462 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002463 break;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002464 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002465
2466 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +00002467 // RegsToPass vector
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002468 if (VA.isRegLoc()) {
2469 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattner52f16de2008-03-17 06:57:02 +00002470 continue;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002471 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002472
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002473 // Register can't get to this point...
Chris Lattner52f16de2008-03-17 06:57:02 +00002474 assert(VA.isMemLoc());
Wesley Peck527da1b2010-11-23 03:31:01 +00002475
Wesley Peck527da1b2010-11-23 03:31:01 +00002476 // emit ISD::STORE whichs stores the
Chris Lattner52f16de2008-03-17 06:57:02 +00002477 // parameter value to a stack Location
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002478 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002479 Chain, Arg, DL, IsTailCall, DAG));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002480 }
2481
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002482 // Transform all store nodes into one single node because all store
2483 // nodes are independent of each other.
Wesley Peck527da1b2010-11-23 03:31:01 +00002484 if (!MemOpChains.empty())
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002485 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002486 &MemOpChains[0], MemOpChains.size());
2487
Bill Wendling24c79f22008-09-16 21:48:12 +00002488 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peck527da1b2010-11-23 03:31:01 +00002489 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2490 // node so that legalize doesn't hack it.
Akira Hatanakab20a3252011-10-28 19:49:00 +00002491 bool IsPICCall = (IsN64 || IsPIC); // true if calls are translated to jalr $25
Akira Hatanakacf9a61b2012-12-13 03:17:29 +00002492 bool GlobalOrExternal = false, InternalLinkage = false;
Akira Hatanakad6f1c582011-04-07 19:51:44 +00002493 SDValue CalleeLo;
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002494 EVT Ty = Callee.getValueType();
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002495
2496 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002497 if (IsPICCall) {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002498 const GlobalValue *Val = G->getGlobal();
2499 InternalLinkage = Val->hasInternalLinkage();
Akira Hatanakacf9a61b2012-12-13 03:17:29 +00002500
2501 if (InternalLinkage)
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002502 Callee = getAddrLocal(G, Ty, DAG, HasMips64);
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00002503 else if (LargeGOT)
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002504 Callee = getAddrGlobalLargeGOT(G, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002505 MipsII::MO_CALL_LO16, Chain,
2506 FuncInfo->callPtrInfo(Val));
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002507 else
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002508 Callee = getAddrGlobal(G, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
2509 FuncInfo->callPtrInfo(Val));
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002510 } else
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002511 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, getPointerTy(), 0,
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002512 MipsII::MO_NO_FLAG);
Akira Hatanaka8e16aac2011-12-09 01:45:12 +00002513 GlobalOrExternal = true;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002514 }
2515 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002516 const char *Sym = S->getSymbol();
2517
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00002518 if (!IsN64 && !IsPIC) // !N64 && static
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002519 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(),
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002520 MipsII::MO_NO_FLAG);
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00002521 else if (LargeGOT)
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002522 Callee = getAddrGlobalLargeGOT(S, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002523 MipsII::MO_CALL_LO16, Chain,
2524 FuncInfo->callPtrInfo(Sym));
Akira Hatanaka02b0e482013-02-22 21:10:03 +00002525 else // N64 || PIC
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002526 Callee = getAddrGlobal(S, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
2527 FuncInfo->callPtrInfo(Sym));
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002528
Akira Hatanaka8e16aac2011-12-09 01:45:12 +00002529 GlobalOrExternal = true;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002530 }
2531
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002532 SmallVector<SDValue, 8> Ops(1, Chain);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002533 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002534
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002535 getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal, InternalLinkage,
2536 CLI, Callee, Chain);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002537
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002538 if (IsTailCall)
2539 return DAG.getNode(MipsISD::TailCall, DL, MVT::Other, &Ops[0], Ops.size());
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002540
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002541 Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, &Ops[0], Ops.size());
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002542 SDValue InFlag = Chain.getValue(1);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002543
Bruno Cardoso Lopes193e64c2010-01-30 18:32:07 +00002544 // Create the CALLSEQ_END node.
Akira Hatanaka97ba7692012-07-26 23:27:01 +00002545 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal,
Andrew Trickad6d08a2013-05-29 22:03:55 +00002546 DAG.getIntPtrConstant(0, true), InFlag, DL);
Bruno Cardoso Lopes193e64c2010-01-30 18:32:07 +00002547 InFlag = Chain.getValue(1);
2548
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002549 // Handle result values, copying them out of physregs into vregs that we
2550 // return.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002551 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg,
2552 Ins, DL, DAG, InVals, CLI.Callee.getNode(), CLI.RetTy);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002553}
2554
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002555/// LowerCallResult - Lower the result values of a call into the
2556/// appropriate copies out of appropriate physical registers.
2557SDValue
2558MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002559 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002560 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002561 SDLoc DL, SelectionDAG &DAG,
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002562 SmallVectorImpl<SDValue> &InVals,
2563 const SDNode *CallNode,
2564 const Type *RetTy) const {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002565 // Assign locations to each value returned by this call.
2566 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002567 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka5fd22482012-06-14 21:10:56 +00002568 getTargetMachine(), RVLocs, *DAG.getContext());
Akira Hatanakabfb66242013-08-20 23:38:40 +00002569 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo);
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002570
Reed Kotlerc03807a2013-08-30 19:40:56 +00002571 MipsCCInfo.analyzeCallResult(Ins, Subtarget->mipsSEUsesSoftFloat(),
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002572 CallNode, RetTy);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002573
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002574 // Copy all of the result registers out of their specified physreg.
2575 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002576 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(),
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002577 RVLocs[i].getLocVT(), InFlag);
2578 Chain = Val.getValue(1);
2579 InFlag = Val.getValue(2);
2580
2581 if (RVLocs[i].getValVT() != RVLocs[i].getLocVT())
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002582 Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getValVT(), Val);
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002583
2584 InVals.push_back(Val);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002585 }
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +00002586
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002587 return Chain;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002588}
2589
Akira Hatanakae2489122011-04-15 21:51:11 +00002590//===----------------------------------------------------------------------===//
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002591// Formal Arguments Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002592//===----------------------------------------------------------------------===//
Wesley Peck527da1b2010-11-23 03:31:01 +00002593/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002594/// and generate load operations for arguments places on the stack.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002595SDValue
2596MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanakaaef55c82011-04-15 21:00:26 +00002597 CallingConv::ID CallConv,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002598 bool IsVarArg,
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00002599 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002600 SDLoc DL, SelectionDAG &DAG,
Akira Hatanakaaef55c82011-04-15 21:00:26 +00002601 SmallVectorImpl<SDValue> &InVals)
Akira Hatanakae2489122011-04-15 21:51:11 +00002602 const {
Bruno Cardoso Lopesa01ede22008-08-04 07:12:52 +00002603 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002604 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopes14033fb2007-08-28 05:08:16 +00002605 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002606
Dan Gohman31ae5862010-04-17 14:41:14 +00002607 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002608
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002609 // Used with vargs to acumulate store chains.
2610 std::vector<SDValue> OutChains;
2611
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002612 // Assign locations to all of the incoming arguments.
2613 SmallVector<CCValAssign, 16> ArgLocs;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002614 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00002615 getTargetMachine(), ArgLocs, *DAG.getContext());
Akira Hatanakabfb66242013-08-20 23:38:40 +00002616 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo);
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002617 Function::const_arg_iterator FuncArg =
2618 DAG.getMachineFunction().getFunction()->arg_begin();
Reed Kotlerc03807a2013-08-30 19:40:56 +00002619 bool UseSoftFloat = Subtarget->mipsSEUsesSoftFloat();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002620
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002621 MipsCCInfo.analyzeFormalArguments(Ins, UseSoftFloat, FuncArg);
Akira Hatanaka4866fe12012-10-30 19:37:25 +00002622 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(),
2623 MipsCCInfo.hasByValArg());
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002624
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00002625 unsigned CurArgIdx = 0;
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002626 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002627
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00002628 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002629 CCValAssign &VA = ArgLocs[i];
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00002630 std::advance(FuncArg, Ins[i].OrigArgIndex - CurArgIdx);
2631 CurArgIdx = Ins[i].OrigArgIndex;
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002632 EVT ValVT = VA.getValVT();
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002633 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2634 bool IsRegLoc = VA.isRegLoc();
2635
2636 if (Flags.isByVal()) {
2637 assert(Flags.getByValSize() &&
2638 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002639 assert(ByValArg != MipsCCInfo.byval_end());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002640 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg,
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002641 MipsCCInfo, *ByValArg);
2642 ++ByValArg;
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002643 continue;
2644 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002645
2646 // Arguments stored on registers
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002647 if (IsRegLoc) {
Akira Hatanaka7d822522013-10-28 21:21:36 +00002648 MVT RegVT = VA.getLocVT();
Akira Hatanakacb4a1a82011-05-24 00:23:52 +00002649 unsigned ArgReg = VA.getLocReg();
Akira Hatanaka7d822522013-10-28 21:21:36 +00002650 const TargetRegisterClass *RC = getRegClassFor(RegVT);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002651
Wesley Peck527da1b2010-11-23 03:31:01 +00002652 // Transform the arguments stored on
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002653 // physical registers into virtual ones
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002654 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC);
2655 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
Wesley Peck527da1b2010-11-23 03:31:01 +00002656
2657 // If this is an 8 or 16-bit value, it has been passed promoted
2658 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002659 // truncate to the right size.
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002660 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattner3c049702009-03-26 05:28:14 +00002661 unsigned Opcode = 0;
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002662 if (VA.getLocInfo() == CCValAssign::SExt)
2663 Opcode = ISD::AssertSext;
2664 else if (VA.getLocInfo() == CCValAssign::ZExt)
2665 Opcode = ISD::AssertZext;
Chris Lattner3c049702009-03-26 05:28:14 +00002666 if (Opcode)
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002667 ArgValue = DAG.getNode(Opcode, DL, RegVT, ArgValue,
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002668 DAG.getValueType(ValVT));
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002669 ArgValue = DAG.getNode(ISD::TRUNCATE, DL, ValVT, ArgValue);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002670 }
2671
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002672 // Handle floating point arguments passed in integer registers and
2673 // long double arguments passed in floating point registers.
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002674 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002675 (RegVT == MVT::i64 && ValVT == MVT::f64) ||
2676 (RegVT == MVT::f64 && ValVT == MVT::i64))
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002677 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002678 else if (IsO32 && RegVT == MVT::i32 && ValVT == MVT::f64) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002679 unsigned Reg2 = addLiveIn(DAG.getMachineFunction(),
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002680 getNextIntArgReg(ArgReg), RC);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002681 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT);
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002682 if (!Subtarget->isLittle())
2683 std::swap(ArgValue, ArgValue2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002684 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64,
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002685 ArgValue, ArgValue2);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002686 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002687
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002688 InVals.push_back(ArgValue);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002689 } else { // VA.isRegLoc()
2690
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002691 // sanity check
2692 assert(VA.isMemLoc());
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002693
Wesley Peck527da1b2010-11-23 03:31:01 +00002694 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002695 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakacb4a1a82011-05-24 00:23:52 +00002696 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002697
2698 // Create load nodes to retrieve arguments from the stack
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002699 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Akira Hatanakad1c58ed2013-11-09 02:38:51 +00002700 SDValue Load = DAG.getLoad(ValVT, DL, Chain, FIN,
2701 MachinePointerInfo::getFixedStack(FI),
2702 false, false, false, 0);
2703 InVals.push_back(Load);
2704 OutChains.push_back(Load.getValue(1));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002705 }
2706 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002707
2708 // The mips ABIs for returning structs by value requires that we copy
2709 // the sret argument into $v0 for the return. Save the argument into
2710 // a virtual register so that we can access it from the return points.
2711 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2712 unsigned Reg = MipsFI->getSRetReturnReg();
2713 if (!Reg) {
Akira Hatanaka0c7d1312012-10-19 22:11:40 +00002714 Reg = MF.getRegInfo().
2715 createVirtualRegister(getRegClassFor(IsN64 ? MVT::i64 : MVT::i32));
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002716 MipsFI->setSRetReturnReg(Reg);
2717 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002718 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[0]);
2719 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002720 }
2721
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002722 if (IsVarArg)
2723 writeVarArgRegs(OutChains, MipsCCInfo, Chain, DL, DAG);
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002724
Wesley Peck527da1b2010-11-23 03:31:01 +00002725 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002726 // the size of Ins and InVals. This only happens when on varg functions
2727 if (!OutChains.empty()) {
2728 OutChains.push_back(Chain);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002729 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002730 &OutChains[0], OutChains.size());
2731 }
2732
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002733 return Chain;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002734}
2735
Akira Hatanakae2489122011-04-15 21:51:11 +00002736//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002737// Return Value Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002738//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002739
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00002740bool
2741MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002742 MachineFunction &MF, bool IsVarArg,
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00002743 const SmallVectorImpl<ISD::OutputArg> &Outs,
2744 LLVMContext &Context) const {
2745 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002746 CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(),
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00002747 RVLocs, Context);
2748 return CCInfo.CheckReturn(Outs, RetCC_Mips);
2749}
2750
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002751SDValue
2752MipsTargetLowering::LowerReturn(SDValue Chain,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002753 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002754 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002755 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002756 SDLoc DL, SelectionDAG &DAG) const {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002757 // CCValAssign - represent the assignment of
2758 // the return value to a location
2759 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002760 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002761
2762 // CCState - Info about the registers and stack slot.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002763 CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(), RVLocs,
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002764 *DAG.getContext());
Akira Hatanakabfb66242013-08-20 23:38:40 +00002765 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002766
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002767 // Analyze return values.
Reed Kotlerc03807a2013-08-30 19:40:56 +00002768 MipsCCInfo.analyzeReturn(Outs, Subtarget->mipsSEUsesSoftFloat(),
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002769 MF.getFunction()->getReturnType());
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002770
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002771 SDValue Flag;
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00002772 SmallVector<SDValue, 4> RetOps(1, Chain);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002773
2774 // Copy the result values into the output registers.
2775 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002776 SDValue Val = OutVals[i];
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002777 CCValAssign &VA = RVLocs[i];
2778 assert(VA.isRegLoc() && "Can only return in registers!");
2779
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002780 if (RVLocs[i].getValVT() != RVLocs[i].getLocVT())
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002781 Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getLocVT(), Val);
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002782
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002783 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002784
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00002785 // Guarantee that all emitted copies are stuck together with flags.
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002786 Flag = Chain.getValue(1);
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00002787 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002788 }
2789
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002790 // The mips ABIs for returning structs by value requires that we copy
2791 // the sret argument into $v0 for the return. We saved the argument into
2792 // a virtual register in the entry block, so now we copy the value out
2793 // and into $v0.
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002794 if (MF.getFunction()->hasStructRetAttr()) {
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002795 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2796 unsigned Reg = MipsFI->getSRetReturnReg();
2797
Wesley Peck527da1b2010-11-23 03:31:01 +00002798 if (!Reg)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002799 llvm_unreachable("sret virtual register not created in the entry block");
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002800 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Akira Hatanaka868b3a32012-10-24 02:10:54 +00002801 unsigned V0 = IsN64 ? Mips::V0_64 : Mips::V0;
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002802
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002803 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002804 Flag = Chain.getValue(1);
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00002805 RetOps.push_back(DAG.getRegister(V0, getPointerTy()));
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002806 }
2807
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00002808 RetOps[0] = Chain; // Update chain.
Akira Hatanakaefff7b72012-07-10 00:19:06 +00002809
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00002810 // Add the flag if we have it.
2811 if (Flag.getNode())
2812 RetOps.push_back(Flag);
2813
2814 // Return on Mips is always a "jr $ra"
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002815 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, &RetOps[0], RetOps.size());
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002816}
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002817
Akira Hatanakae2489122011-04-15 21:51:11 +00002818//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002819// Mips Inline Assembly Support
Akira Hatanakae2489122011-04-15 21:51:11 +00002820//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002821
2822/// getConstraintType - Given a constraint letter, return the type of
2823/// constraint it is for this target.
2824MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peck527da1b2010-11-23 03:31:01 +00002825getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002826{
Daniel Sanders8b59af12013-11-12 12:56:01 +00002827 // Mips specific constraints
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002828 // GCC config/mips/constraints.md
2829 //
Wesley Peck527da1b2010-11-23 03:31:01 +00002830 // 'd' : An address register. Equivalent to r
2831 // unless generating MIPS16 code.
2832 // 'y' : Equivalent to r; retained for
2833 // backwards compatibility.
Eric Christophere3c494d2012-05-07 06:25:10 +00002834 // 'c' : A register suitable for use in an indirect
2835 // jump. This will always be $25 for -mabicalls.
Eric Christopher0d8c15d2012-05-07 06:25:19 +00002836 // 'l' : The lo register. 1 word storage.
2837 // 'x' : The hilo register pair. Double word storage.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002838 if (Constraint.size() == 1) {
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002839 switch (Constraint[0]) {
2840 default : break;
Wesley Peck527da1b2010-11-23 03:31:01 +00002841 case 'd':
2842 case 'y':
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002843 case 'f':
Eric Christophere3c494d2012-05-07 06:25:10 +00002844 case 'c':
Eric Christopher9c492e62012-05-07 06:25:15 +00002845 case 'l':
Eric Christopher0d8c15d2012-05-07 06:25:19 +00002846 case 'x':
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002847 return C_RegisterClass;
Jack Carter0e149b02013-03-04 21:33:15 +00002848 case 'R':
2849 return C_Memory;
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002850 }
2851 }
2852 return TargetLowering::getConstraintType(Constraint);
2853}
2854
John Thompsone8360b72010-10-29 17:29:13 +00002855/// Examine constraint type and operand type and determine a weight value.
2856/// This object must already have been set up with the operand type
2857/// and the current alternative constraint selected.
2858TargetLowering::ConstraintWeight
2859MipsTargetLowering::getSingleConstraintMatchWeight(
2860 AsmOperandInfo &info, const char *constraint) const {
2861 ConstraintWeight weight = CW_Invalid;
2862 Value *CallOperandVal = info.CallOperandVal;
2863 // If we don't have a value, we can't do a match,
2864 // but allow it at the lowest weight.
2865 if (CallOperandVal == NULL)
2866 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +00002867 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +00002868 // Look at the constraint type.
2869 switch (*constraint) {
2870 default:
2871 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2872 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00002873 case 'd':
2874 case 'y':
John Thompsone8360b72010-10-29 17:29:13 +00002875 if (type->isIntegerTy())
2876 weight = CW_Register;
2877 break;
Daniel Sanders8b59af12013-11-12 12:56:01 +00002878 case 'f': // FPU or MSA register
2879 if (Subtarget->hasMSA() && type->isVectorTy() &&
2880 cast<VectorType>(type)->getBitWidth() == 128)
2881 weight = CW_Register;
2882 else if (type->isFloatTy())
John Thompsone8360b72010-10-29 17:29:13 +00002883 weight = CW_Register;
2884 break;
Eric Christophere3c494d2012-05-07 06:25:10 +00002885 case 'c': // $25 for indirect jumps
Eric Christopher9c492e62012-05-07 06:25:15 +00002886 case 'l': // lo register
Eric Christopher0d8c15d2012-05-07 06:25:19 +00002887 case 'x': // hilo register pair
Daniel Sanders8b59af12013-11-12 12:56:01 +00002888 if (type->isIntegerTy())
Eric Christophere3c494d2012-05-07 06:25:10 +00002889 weight = CW_SpecificReg;
Daniel Sanders8b59af12013-11-12 12:56:01 +00002890 break;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00002891 case 'I': // signed 16 bit immediate
Eric Christopher7201e1b2012-05-07 03:13:42 +00002892 case 'J': // integer zero
Eric Christopher3ff88a02012-05-07 05:46:29 +00002893 case 'K': // unsigned 16 bit immediate
Eric Christopher1109b342012-05-07 05:46:37 +00002894 case 'L': // signed 32 bit immediate where lower 16 bits are 0
Eric Christophere07aa432012-05-07 05:46:43 +00002895 case 'N': // immediate in the range of -65535 to -1 (inclusive)
Eric Christopher470578a2012-05-07 05:46:48 +00002896 case 'O': // signed 15 bit immediate (+- 16383)
Eric Christopherc18ae4a2012-05-07 06:25:02 +00002897 case 'P': // immediate in the range of 65535 to 1 (inclusive)
Eric Christopher1d6c89e2012-05-07 03:13:32 +00002898 if (isa<ConstantInt>(CallOperandVal))
2899 weight = CW_Constant;
2900 break;
Jack Carter0e149b02013-03-04 21:33:15 +00002901 case 'R':
2902 weight = CW_Memory;
2903 break;
John Thompsone8360b72010-10-29 17:29:13 +00002904 }
2905 return weight;
2906}
2907
Akira Hatanaka7473b472013-08-14 00:21:25 +00002908/// This is a helper function to parse a physical register string and split it
2909/// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag
2910/// that is returned indicates whether parsing was successful. The second flag
2911/// is true if the numeric part exists.
2912static std::pair<bool, bool>
2913parsePhysicalReg(const StringRef &C, std::string &Prefix,
2914 unsigned long long &Reg) {
2915 if (C.front() != '{' || C.back() != '}')
2916 return std::make_pair(false, false);
2917
2918 // Search for the first numeric character.
2919 StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1;
2920 I = std::find_if(B, E, std::ptr_fun(isdigit));
2921
2922 Prefix.assign(B, I - B);
2923
2924 // The second flag is set to false if no numeric characters were found.
2925 if (I == E)
2926 return std::make_pair(true, false);
2927
2928 // Parse the numeric characters.
2929 return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg),
2930 true);
2931}
2932
2933std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering::
2934parseRegForInlineAsmConstraint(const StringRef &C, MVT VT) const {
2935 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2936 const TargetRegisterClass *RC;
2937 std::string Prefix;
2938 unsigned long long Reg;
2939
2940 std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg);
2941
2942 if (!R.first)
2943 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2944
2945 if ((Prefix == "hi" || Prefix == "lo")) { // Parse hi/lo.
2946 // No numeric characters follow "hi" or "lo".
2947 if (R.second)
2948 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2949
2950 RC = TRI->getRegClass(Prefix == "hi" ?
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00002951 Mips::HI32RegClassID : Mips::LO32RegClassID);
Akira Hatanaka7473b472013-08-14 00:21:25 +00002952 return std::make_pair(*(RC->begin()), RC);
Daniel Sanders8b59af12013-11-12 12:56:01 +00002953 } else if (Prefix.compare(0, 4, "$msa") == 0) {
2954 // Parse $msa(ir|csr|access|save|modify|request|map|unmap)
2955
2956 // No numeric characters follow the name.
2957 if (R.second)
2958 return std::make_pair((unsigned)0, (const TargetRegisterClass *)0);
2959
2960 Reg = StringSwitch<unsigned long long>(Prefix)
2961 .Case("$msair", Mips::MSAIR)
2962 .Case("$msacsr", Mips::MSACSR)
2963 .Case("$msaaccess", Mips::MSAAccess)
2964 .Case("$msasave", Mips::MSASave)
2965 .Case("$msamodify", Mips::MSAModify)
2966 .Case("$msarequest", Mips::MSARequest)
2967 .Case("$msamap", Mips::MSAMap)
2968 .Case("$msaunmap", Mips::MSAUnmap)
2969 .Default(0);
2970
2971 if (!Reg)
2972 return std::make_pair((unsigned)0, (const TargetRegisterClass *)0);
2973
2974 RC = TRI->getRegClass(Mips::MSACtrlRegClassID);
2975 return std::make_pair(Reg, RC);
Akira Hatanaka7473b472013-08-14 00:21:25 +00002976 }
2977
2978 if (!R.second)
2979 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2980
2981 if (Prefix == "$f") { // Parse $f0-$f31.
2982 // If the size of FP registers is 64-bit or Reg is an even number, select
2983 // the 64-bit register class. Otherwise, select the 32-bit register class.
2984 if (VT == MVT::Other)
2985 VT = (Subtarget->isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32;
2986
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00002987 RC = getRegClassFor(VT);
Akira Hatanaka7473b472013-08-14 00:21:25 +00002988
2989 if (RC == &Mips::AFGR64RegClass) {
2990 assert(Reg % 2 == 0);
2991 Reg >>= 1;
2992 }
Daniel Sanders8b59af12013-11-12 12:56:01 +00002993 } else if (Prefix == "$fcc") // Parse $fcc0-$fcc7.
Akira Hatanaka7473b472013-08-14 00:21:25 +00002994 RC = TRI->getRegClass(Mips::FCCRegClassID);
Daniel Sanders8b59af12013-11-12 12:56:01 +00002995 else if (Prefix == "$w") { // Parse $w0-$w31.
2996 RC = getRegClassFor((VT == MVT::Other) ? MVT::v16i8 : VT);
Akira Hatanaka7473b472013-08-14 00:21:25 +00002997 } else { // Parse $0-$31.
2998 assert(Prefix == "$");
2999 RC = getRegClassFor((VT == MVT::Other) ? MVT::i32 : VT);
3000 }
3001
3002 assert(Reg < RC->getNumRegs());
3003 return std::make_pair(*(RC->begin() + Reg), RC);
3004}
3005
Eric Christophereaf77dc2011-06-29 19:33:04 +00003006/// Given a register class constraint, like 'r', if this corresponds directly
3007/// to an LLVM register class, return a register of 0 and the register class
3008/// pointer.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003009std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Chad Rosier295bd432013-06-22 18:37:38 +00003010getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003011{
3012 if (Constraint.size() == 1) {
3013 switch (Constraint[0]) {
Eric Christopher9519c082011-06-29 19:04:31 +00003014 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
3015 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003016 case 'r':
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003017 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
3018 if (Subtarget->inMips16Mode())
3019 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003020 return std::make_pair(0U, &Mips::GPR32RegClass);
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003021 }
Jack Carterb3530942012-07-02 23:35:23 +00003022 if (VT == MVT::i64 && !HasMips64)
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003023 return std::make_pair(0U, &Mips::GPR32RegClass);
Eric Christopher58daf042012-05-07 03:13:22 +00003024 if (VT == MVT::i64 && HasMips64)
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003025 return std::make_pair(0U, &Mips::GPR64RegClass);
Eric Christopher58daf042012-05-07 03:13:22 +00003026 // This will generate an error message
3027 return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
Daniel Sanders8b59af12013-11-12 12:56:01 +00003028 case 'f': // FPU or MSA register
3029 if (VT == MVT::v16i8)
3030 return std::make_pair(0U, &Mips::MSA128BRegClass);
3031 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
3032 return std::make_pair(0U, &Mips::MSA128HRegClass);
3033 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
3034 return std::make_pair(0U, &Mips::MSA128WRegClass);
3035 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
3036 return std::make_pair(0U, &Mips::MSA128DRegClass);
3037 else if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00003038 return std::make_pair(0U, &Mips::FGR32RegClass);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003039 else if ((VT == MVT::f64) && (!Subtarget->isSingleFloat())) {
Akira Hatanakac669d7a2012-01-04 02:45:01 +00003040 if (Subtarget->isFP64bit())
Craig Topperc7242e02012-04-20 07:30:17 +00003041 return std::make_pair(0U, &Mips::FGR64RegClass);
3042 return std::make_pair(0U, &Mips::AFGR64RegClass);
Akira Hatanakac669d7a2012-01-04 02:45:01 +00003043 }
Eric Christophere3c494d2012-05-07 06:25:10 +00003044 break;
3045 case 'c': // register suitable for indirect jump
3046 if (VT == MVT::i32)
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003047 return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass);
Eric Christophere3c494d2012-05-07 06:25:10 +00003048 assert(VT == MVT::i64 && "Unexpected type.");
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003049 return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass);
Eric Christopher9c492e62012-05-07 06:25:15 +00003050 case 'l': // register suitable for indirect jump
3051 if (VT == MVT::i32)
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00003052 return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass);
3053 return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass);
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003054 case 'x': // register suitable for indirect jump
3055 // Fixme: Not triggering the use of both hi and low
3056 // This will generate an error message
3057 return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003058 }
3059 }
Akira Hatanaka7473b472013-08-14 00:21:25 +00003060
3061 std::pair<unsigned, const TargetRegisterClass *> R;
3062 R = parseRegForInlineAsmConstraint(Constraint, VT);
3063
3064 if (R.second)
3065 return R;
3066
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003067 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3068}
3069
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003070/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3071/// vector. If it is invalid, don't add anything to Ops.
3072void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3073 std::string &Constraint,
3074 std::vector<SDValue>&Ops,
3075 SelectionDAG &DAG) const {
3076 SDValue Result(0, 0);
3077
3078 // Only support length 1 constraints for now.
3079 if (Constraint.length() > 1) return;
3080
3081 char ConstraintLetter = Constraint[0];
3082 switch (ConstraintLetter) {
3083 default: break; // This will fall through to the generic implementation
3084 case 'I': // Signed 16 bit constant
3085 // If this fails, the parent routine will give an error
3086 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3087 EVT Type = Op.getValueType();
3088 int64_t Val = C->getSExtValue();
3089 if (isInt<16>(Val)) {
3090 Result = DAG.getTargetConstant(Val, Type);
3091 break;
3092 }
3093 }
3094 return;
Eric Christopher7201e1b2012-05-07 03:13:42 +00003095 case 'J': // integer zero
3096 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3097 EVT Type = Op.getValueType();
3098 int64_t Val = C->getZExtValue();
3099 if (Val == 0) {
3100 Result = DAG.getTargetConstant(0, Type);
3101 break;
3102 }
3103 }
3104 return;
Eric Christopher3ff88a02012-05-07 05:46:29 +00003105 case 'K': // unsigned 16 bit immediate
3106 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3107 EVT Type = Op.getValueType();
3108 uint64_t Val = (uint64_t)C->getZExtValue();
3109 if (isUInt<16>(Val)) {
3110 Result = DAG.getTargetConstant(Val, Type);
3111 break;
3112 }
3113 }
3114 return;
Eric Christopher1109b342012-05-07 05:46:37 +00003115 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3116 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3117 EVT Type = Op.getValueType();
3118 int64_t Val = C->getSExtValue();
3119 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
3120 Result = DAG.getTargetConstant(Val, Type);
3121 break;
3122 }
3123 }
3124 return;
Eric Christophere07aa432012-05-07 05:46:43 +00003125 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3126 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3127 EVT Type = Op.getValueType();
3128 int64_t Val = C->getSExtValue();
3129 if ((Val >= -65535) && (Val <= -1)) {
3130 Result = DAG.getTargetConstant(Val, Type);
3131 break;
3132 }
3133 }
3134 return;
Eric Christopher470578a2012-05-07 05:46:48 +00003135 case 'O': // signed 15 bit immediate
3136 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3137 EVT Type = Op.getValueType();
3138 int64_t Val = C->getSExtValue();
3139 if ((isInt<15>(Val))) {
3140 Result = DAG.getTargetConstant(Val, Type);
3141 break;
3142 }
3143 }
3144 return;
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003145 case 'P': // immediate in the range of 1 to 65535 (inclusive)
3146 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3147 EVT Type = Op.getValueType();
3148 int64_t Val = C->getSExtValue();
3149 if ((Val <= 65535) && (Val >= 1)) {
3150 Result = DAG.getTargetConstant(Val, Type);
3151 break;
3152 }
3153 }
3154 return;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003155 }
3156
3157 if (Result.getNode()) {
3158 Ops.push_back(Result);
3159 return;
3160 }
3161
3162 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3163}
3164
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003165bool MipsTargetLowering::isLegalAddressingMode(const AddrMode &AM,
3166 Type *Ty) const {
Akira Hatanakaef839192012-11-17 00:25:41 +00003167 // No global is ever allowed as a base.
3168 if (AM.BaseGV)
3169 return false;
3170
3171 switch (AM.Scale) {
3172 case 0: // "r+i" or just "i", depending on HasBaseReg.
3173 break;
3174 case 1:
3175 if (!AM.HasBaseReg) // allow "r+i".
3176 break;
3177 return false; // disallow "r+r" or "r+r+i".
3178 default:
3179 return false;
3180 }
3181
3182 return true;
3183}
3184
3185bool
Dan Gohman2fe6bee2008-10-18 02:06:02 +00003186MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3187 // The Mips target isn't yet aware of offsets.
3188 return false;
3189}
Evan Cheng16993aa2009-10-27 19:56:55 +00003190
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003191EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003192 unsigned SrcAlign,
3193 bool IsMemset, bool ZeroMemset,
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003194 bool MemcpyStrSrc,
3195 MachineFunction &MF) const {
3196 if (Subtarget->hasMips64())
3197 return MVT::i64;
3198
3199 return MVT::i32;
3200}
3201
Evan Cheng83896a52009-10-28 01:43:28 +00003202bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
3203 if (VT != MVT::f32 && VT != MVT::f64)
3204 return false;
Bruno Cardoso Lopesb02a9df2011-01-18 19:41:41 +00003205 if (Imm.isNegZero())
3206 return false;
Evan Cheng16993aa2009-10-27 19:56:55 +00003207 return Imm.isZero();
3208}
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003209
3210unsigned MipsTargetLowering::getJumpTableEncoding() const {
3211 if (IsN64)
3212 return MachineJumpTableInfo::EK_GPRel64BlockAddress;
Jia Liuf54f60f2012-02-28 07:46:26 +00003213
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003214 return TargetLowering::getJumpTableEncoding();
3215}
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003216
Akira Hatanakae092f722013-03-05 22:54:59 +00003217/// This function returns true if CallSym is a long double emulation routine.
3218static bool isF128SoftLibCall(const char *CallSym) {
3219 const char *const LibCalls[] =
3220 {"__addtf3", "__divtf3", "__eqtf2", "__extenddftf2", "__extendsftf2",
3221 "__fixtfdi", "__fixtfsi", "__fixtfti", "__fixunstfdi", "__fixunstfsi",
3222 "__fixunstfti", "__floatditf", "__floatsitf", "__floattitf",
3223 "__floatunditf", "__floatunsitf", "__floatuntitf", "__getf2", "__gttf2",
3224 "__letf2", "__lttf2", "__multf3", "__netf2", "__powitf2", "__subtf3",
3225 "__trunctfdf2", "__trunctfsf2", "__unordtf2",
3226 "ceill", "copysignl", "cosl", "exp2l", "expl", "floorl", "fmal", "fmodl",
3227 "log10l", "log2l", "logl", "nearbyintl", "powl", "rintl", "sinl", "sqrtl",
3228 "truncl"};
3229
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003230 const char *const *End = LibCalls + array_lengthof(LibCalls);
Akira Hatanakae092f722013-03-05 22:54:59 +00003231
3232 // Check that LibCalls is sorted alphabetically.
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003233 MipsTargetLowering::LTStr Comp;
Akira Hatanakae092f722013-03-05 22:54:59 +00003234
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003235#ifndef NDEBUG
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003236 for (const char *const *I = LibCalls; I < End - 1; ++I)
Akira Hatanakae092f722013-03-05 22:54:59 +00003237 assert(Comp(*I, *(I + 1)));
3238#endif
3239
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003240 return std::binary_search(LibCalls, End, CallSym, Comp);
Akira Hatanakae092f722013-03-05 22:54:59 +00003241}
3242
3243/// This function returns true if Ty is fp128 or i128 which was originally a
3244/// fp128.
3245static bool originalTypeIsF128(const Type *Ty, const SDNode *CallNode) {
3246 if (Ty->isFP128Ty())
3247 return true;
3248
3249 const ExternalSymbolSDNode *ES =
3250 dyn_cast_or_null<const ExternalSymbolSDNode>(CallNode);
3251
3252 // If the Ty is i128 and the function being called is a long double emulation
3253 // routine, then the original type is f128.
3254 return (ES && Ty->isIntegerTy(128) && isF128SoftLibCall(ES->getSymbol()));
3255}
3256
Reed Kotler783c7942013-05-10 22:25:39 +00003257MipsTargetLowering::MipsCC::SpecialCallingConvType
3258 MipsTargetLowering::getSpecialCallingConv(SDValue Callee) const {
3259 MipsCC::SpecialCallingConvType SpecialCallingConv =
3260 MipsCC::NoSpecialCallingConv;;
3261 if (Subtarget->inMips16HardFloat()) {
3262 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3263 llvm::StringRef Sym = G->getGlobal()->getName();
3264 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
3265 if (F->hasFnAttribute("__Mips16RetHelper")) {
3266 SpecialCallingConv = MipsCC::Mips16RetHelperConv;
3267 }
3268 }
3269 }
3270 return SpecialCallingConv;
3271}
3272
3273MipsTargetLowering::MipsCC::MipsCC(
Akira Hatanakabfb66242013-08-20 23:38:40 +00003274 CallingConv::ID CC, bool IsO32_, bool IsFP64_, CCState &Info,
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003275 MipsCC::SpecialCallingConvType SpecialCallingConv_)
Akira Hatanakabfb66242013-08-20 23:38:40 +00003276 : CCInfo(Info), CallConv(CC), IsO32(IsO32_), IsFP64(IsFP64_),
Reed Kotler783c7942013-05-10 22:25:39 +00003277 SpecialCallingConv(SpecialCallingConv_){
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003278 // Pre-allocate reserved argument area.
Akira Hatanaka5001be52013-02-15 21:45:11 +00003279 CCInfo.AllocateStack(reservedArgArea(), 1);
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003280}
3281
Reed Kotler783c7942013-05-10 22:25:39 +00003282
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003283void MipsTargetLowering::MipsCC::
Akira Hatanaka5001be52013-02-15 21:45:11 +00003284analyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Args,
Akira Hatanaka3b7391d2013-03-05 22:20:28 +00003285 bool IsVarArg, bool IsSoftFloat, const SDNode *CallNode,
3286 std::vector<ArgListEntry> &FuncArgs) {
Akira Hatanaka5001be52013-02-15 21:45:11 +00003287 assert((CallConv != CallingConv::Fast || !IsVarArg) &&
3288 "CallingConv::Fast shouldn't be used for vararg functions.");
3289
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003290 unsigned NumOpnds = Args.size();
Akira Hatanaka5001be52013-02-15 21:45:11 +00003291 llvm::CCAssignFn *FixedFn = fixedArgFn(), *VarFn = varArgFn();
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003292
3293 for (unsigned I = 0; I != NumOpnds; ++I) {
3294 MVT ArgVT = Args[I].VT;
3295 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
3296 bool R;
3297
3298 if (ArgFlags.isByVal()) {
3299 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
3300 continue;
3301 }
3302
Akira Hatanaka5001be52013-02-15 21:45:11 +00003303 if (IsVarArg && !Args[I].IsFixed)
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003304 R = VarFn(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
Akira Hatanaka3b7391d2013-03-05 22:20:28 +00003305 else {
3306 MVT RegVT = getRegVT(ArgVT, FuncArgs[Args[I].OrigArgIndex].Ty, CallNode,
3307 IsSoftFloat);
3308 R = FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo);
3309 }
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003310
3311 if (R) {
3312#ifndef NDEBUG
3313 dbgs() << "Call operand #" << I << " has unhandled type "
3314 << EVT(ArgVT).getEVTString();
3315#endif
3316 llvm_unreachable(0);
3317 }
3318 }
3319}
3320
3321void MipsTargetLowering::MipsCC::
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003322analyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Args,
3323 bool IsSoftFloat, Function::const_arg_iterator FuncArg) {
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003324 unsigned NumArgs = Args.size();
Akira Hatanaka5001be52013-02-15 21:45:11 +00003325 llvm::CCAssignFn *FixedFn = fixedArgFn();
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003326 unsigned CurArgIdx = 0;
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003327
3328 for (unsigned I = 0; I != NumArgs; ++I) {
3329 MVT ArgVT = Args[I].VT;
3330 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003331 std::advance(FuncArg, Args[I].OrigArgIndex - CurArgIdx);
3332 CurArgIdx = Args[I].OrigArgIndex;
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003333
3334 if (ArgFlags.isByVal()) {
3335 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
3336 continue;
3337 }
3338
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003339 MVT RegVT = getRegVT(ArgVT, FuncArg->getType(), 0, IsSoftFloat);
3340
3341 if (!FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo))
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003342 continue;
3343
3344#ifndef NDEBUG
3345 dbgs() << "Formal Arg #" << I << " has unhandled type "
3346 << EVT(ArgVT).getEVTString();
3347#endif
3348 llvm_unreachable(0);
3349 }
3350}
3351
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003352template<typename Ty>
3353void MipsTargetLowering::MipsCC::
3354analyzeReturn(const SmallVectorImpl<Ty> &RetVals, bool IsSoftFloat,
3355 const SDNode *CallNode, const Type *RetTy) const {
Akira Hatanakae092f722013-03-05 22:54:59 +00003356 CCAssignFn *Fn;
3357
3358 if (IsSoftFloat && originalTypeIsF128(RetTy, CallNode))
3359 Fn = RetCC_F128Soft;
3360 else
3361 Fn = RetCC_Mips;
3362
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003363 for (unsigned I = 0, E = RetVals.size(); I < E; ++I) {
3364 MVT VT = RetVals[I].VT;
3365 ISD::ArgFlagsTy Flags = RetVals[I].Flags;
3366 MVT RegVT = this->getRegVT(VT, RetTy, CallNode, IsSoftFloat);
3367
Akira Hatanakae092f722013-03-05 22:54:59 +00003368 if (Fn(I, VT, RegVT, CCValAssign::Full, Flags, this->CCInfo)) {
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003369#ifndef NDEBUG
3370 dbgs() << "Call result #" << I << " has unhandled type "
3371 << EVT(VT).getEVTString() << '\n';
3372#endif
3373 llvm_unreachable(0);
3374 }
3375 }
3376}
3377
3378void MipsTargetLowering::MipsCC::
3379analyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins, bool IsSoftFloat,
3380 const SDNode *CallNode, const Type *RetTy) const {
3381 analyzeReturn(Ins, IsSoftFloat, CallNode, RetTy);
3382}
3383
3384void MipsTargetLowering::MipsCC::
3385analyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs, bool IsSoftFloat,
3386 const Type *RetTy) const {
3387 analyzeReturn(Outs, IsSoftFloat, 0, RetTy);
3388}
3389
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003390void MipsTargetLowering::MipsCC::handleByValArg(unsigned ValNo, MVT ValVT,
3391 MVT LocVT,
3392 CCValAssign::LocInfo LocInfo,
3393 ISD::ArgFlagsTy ArgFlags) {
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003394 assert(ArgFlags.getByValSize() && "Byval argument's size shouldn't be 0.");
3395
3396 struct ByValArgInfo ByVal;
Akira Hatanaka5001be52013-02-15 21:45:11 +00003397 unsigned RegSize = regSize();
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003398 unsigned ByValSize = RoundUpToAlignment(ArgFlags.getByValSize(), RegSize);
3399 unsigned Align = std::min(std::max(ArgFlags.getByValAlign(), RegSize),
3400 RegSize * 2);
3401
Akira Hatanaka5001be52013-02-15 21:45:11 +00003402 if (useRegsForByval())
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003403 allocateRegs(ByVal, ByValSize, Align);
3404
3405 // Allocate space on caller's stack.
3406 ByVal.Address = CCInfo.AllocateStack(ByValSize - RegSize * ByVal.NumRegs,
3407 Align);
3408 CCInfo.addLoc(CCValAssign::getMem(ValNo, ValVT, ByVal.Address, LocVT,
3409 LocInfo));
3410 ByValArgs.push_back(ByVal);
3411}
3412
Akira Hatanaka5001be52013-02-15 21:45:11 +00003413unsigned MipsTargetLowering::MipsCC::numIntArgRegs() const {
3414 return IsO32 ? array_lengthof(O32IntRegs) : array_lengthof(Mips64IntRegs);
3415}
3416
3417unsigned MipsTargetLowering::MipsCC::reservedArgArea() const {
3418 return (IsO32 && (CallConv != CallingConv::Fast)) ? 16 : 0;
3419}
3420
3421const uint16_t *MipsTargetLowering::MipsCC::intArgRegs() const {
3422 return IsO32 ? O32IntRegs : Mips64IntRegs;
3423}
3424
3425llvm::CCAssignFn *MipsTargetLowering::MipsCC::fixedArgFn() const {
3426 if (CallConv == CallingConv::Fast)
3427 return CC_Mips_FastCC;
3428
Reed Kotler783c7942013-05-10 22:25:39 +00003429 if (SpecialCallingConv == Mips16RetHelperConv)
3430 return CC_Mips16RetHelper;
Akira Hatanakabfb66242013-08-20 23:38:40 +00003431 return IsO32 ? (IsFP64 ? CC_MipsO32_FP64 : CC_MipsO32_FP32) : CC_MipsN;
Akira Hatanaka5001be52013-02-15 21:45:11 +00003432}
3433
3434llvm::CCAssignFn *MipsTargetLowering::MipsCC::varArgFn() const {
Akira Hatanakabfb66242013-08-20 23:38:40 +00003435 return IsO32 ? (IsFP64 ? CC_MipsO32_FP64 : CC_MipsO32_FP32) : CC_MipsN_VarArg;
Akira Hatanaka5001be52013-02-15 21:45:11 +00003436}
3437
3438const uint16_t *MipsTargetLowering::MipsCC::shadowRegs() const {
3439 return IsO32 ? O32IntRegs : Mips64DPRegs;
3440}
3441
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003442void MipsTargetLowering::MipsCC::allocateRegs(ByValArgInfo &ByVal,
3443 unsigned ByValSize,
3444 unsigned Align) {
Akira Hatanaka5001be52013-02-15 21:45:11 +00003445 unsigned RegSize = regSize(), NumIntArgRegs = numIntArgRegs();
3446 const uint16_t *IntArgRegs = intArgRegs(), *ShadowRegs = shadowRegs();
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003447 assert(!(ByValSize % RegSize) && !(Align % RegSize) &&
3448 "Byval argument's size and alignment should be a multiple of"
3449 "RegSize.");
3450
3451 ByVal.FirstIdx = CCInfo.getFirstUnallocated(IntArgRegs, NumIntArgRegs);
3452
3453 // If Align > RegSize, the first arg register must be even.
3454 if ((Align > RegSize) && (ByVal.FirstIdx % 2)) {
3455 CCInfo.AllocateReg(IntArgRegs[ByVal.FirstIdx], ShadowRegs[ByVal.FirstIdx]);
3456 ++ByVal.FirstIdx;
3457 }
3458
3459 // Mark the registers allocated.
3460 for (unsigned I = ByVal.FirstIdx; ByValSize && (I < NumIntArgRegs);
3461 ByValSize -= RegSize, ++I, ++ByVal.NumRegs)
3462 CCInfo.AllocateReg(IntArgRegs[I], ShadowRegs[I]);
3463}
Akira Hatanaka25dad192012-10-27 00:10:18 +00003464
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003465MVT MipsTargetLowering::MipsCC::getRegVT(MVT VT, const Type *OrigTy,
3466 const SDNode *CallNode,
3467 bool IsSoftFloat) const {
3468 if (IsSoftFloat || IsO32)
3469 return VT;
3470
3471 // Check if the original type was fp128.
Akira Hatanakae092f722013-03-05 22:54:59 +00003472 if (originalTypeIsF128(OrigTy, CallNode)) {
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003473 assert(VT == MVT::i64);
3474 return MVT::f64;
3475 }
3476
3477 return VT;
3478}
3479
Akira Hatanaka25dad192012-10-27 00:10:18 +00003480void MipsTargetLowering::
Andrew Trickef9de2a2013-05-25 02:42:55 +00003481copyByValRegs(SDValue Chain, SDLoc DL, std::vector<SDValue> &OutChains,
Akira Hatanaka25dad192012-10-27 00:10:18 +00003482 SelectionDAG &DAG, const ISD::ArgFlagsTy &Flags,
3483 SmallVectorImpl<SDValue> &InVals, const Argument *FuncArg,
3484 const MipsCC &CC, const ByValArgInfo &ByVal) const {
3485 MachineFunction &MF = DAG.getMachineFunction();
3486 MachineFrameInfo *MFI = MF.getFrameInfo();
3487 unsigned RegAreaSize = ByVal.NumRegs * CC.regSize();
3488 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
3489 int FrameObjOffset;
3490
3491 if (RegAreaSize)
3492 FrameObjOffset = (int)CC.reservedArgArea() -
3493 (int)((CC.numIntArgRegs() - ByVal.FirstIdx) * CC.regSize());
3494 else
3495 FrameObjOffset = ByVal.Address;
3496
3497 // Create frame object.
3498 EVT PtrTy = getPointerTy();
3499 int FI = MFI->CreateFixedObject(FrameObjSize, FrameObjOffset, true);
3500 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
3501 InVals.push_back(FIN);
3502
3503 if (!ByVal.NumRegs)
3504 return;
3505
3506 // Copy arg registers.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00003507 MVT RegTy = MVT::getIntegerVT(CC.regSize() * 8);
Akira Hatanaka25dad192012-10-27 00:10:18 +00003508 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3509
3510 for (unsigned I = 0; I < ByVal.NumRegs; ++I) {
3511 unsigned ArgReg = CC.intArgRegs()[ByVal.FirstIdx + I];
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003512 unsigned VReg = addLiveIn(MF, ArgReg, RC);
Akira Hatanaka25dad192012-10-27 00:10:18 +00003513 unsigned Offset = I * CC.regSize();
3514 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
3515 DAG.getConstant(Offset, PtrTy));
3516 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
3517 StorePtr, MachinePointerInfo(FuncArg, Offset),
3518 false, false, 0);
3519 OutChains.push_back(Store);
3520 }
3521}
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003522
3523// Copy byVal arg to registers and stack.
3524void MipsTargetLowering::
Andrew Trickef9de2a2013-05-25 02:42:55 +00003525passByValArg(SDValue Chain, SDLoc DL,
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00003526 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
Craig Topperb94011f2013-07-14 04:42:23 +00003527 SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr,
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003528 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
3529 const MipsCC &CC, const ByValArgInfo &ByVal,
3530 const ISD::ArgFlagsTy &Flags, bool isLittle) const {
3531 unsigned ByValSize = Flags.getByValSize();
3532 unsigned Offset = 0; // Offset in # of bytes from the beginning of struct.
3533 unsigned RegSize = CC.regSize();
3534 unsigned Alignment = std::min(Flags.getByValAlign(), RegSize);
3535 EVT PtrTy = getPointerTy(), RegTy = MVT::getIntegerVT(RegSize * 8);
3536
3537 if (ByVal.NumRegs) {
3538 const uint16_t *ArgRegs = CC.intArgRegs();
3539 bool LeftoverBytes = (ByVal.NumRegs * RegSize > ByValSize);
3540 unsigned I = 0;
3541
3542 // Copy words to registers.
3543 for (; I < ByVal.NumRegs - LeftoverBytes; ++I, Offset += RegSize) {
3544 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3545 DAG.getConstant(Offset, PtrTy));
3546 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
3547 MachinePointerInfo(), false, false, false,
3548 Alignment);
3549 MemOpChains.push_back(LoadVal.getValue(1));
3550 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3551 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
3552 }
3553
3554 // Return if the struct has been fully copied.
3555 if (ByValSize == Offset)
3556 return;
3557
3558 // Copy the remainder of the byval argument with sub-word loads and shifts.
3559 if (LeftoverBytes) {
3560 assert((ByValSize > Offset) && (ByValSize < Offset + RegSize) &&
3561 "Size of the remainder should be smaller than RegSize.");
3562 SDValue Val;
3563
3564 for (unsigned LoadSize = RegSize / 2, TotalSizeLoaded = 0;
3565 Offset < ByValSize; LoadSize /= 2) {
3566 unsigned RemSize = ByValSize - Offset;
3567
3568 if (RemSize < LoadSize)
3569 continue;
3570
3571 // Load subword.
3572 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3573 DAG.getConstant(Offset, PtrTy));
3574 SDValue LoadVal =
3575 DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr,
3576 MachinePointerInfo(), MVT::getIntegerVT(LoadSize * 8),
3577 false, false, Alignment);
3578 MemOpChains.push_back(LoadVal.getValue(1));
3579
3580 // Shift the loaded value.
3581 unsigned Shamt;
3582
3583 if (isLittle)
3584 Shamt = TotalSizeLoaded;
3585 else
3586 Shamt = (RegSize - (TotalSizeLoaded + LoadSize)) * 8;
3587
3588 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
3589 DAG.getConstant(Shamt, MVT::i32));
3590
3591 if (Val.getNode())
3592 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
3593 else
3594 Val = Shift;
3595
3596 Offset += LoadSize;
3597 TotalSizeLoaded += LoadSize;
3598 Alignment = std::min(Alignment, LoadSize);
3599 }
3600
3601 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3602 RegsToPass.push_back(std::make_pair(ArgReg, Val));
3603 return;
3604 }
3605 }
3606
3607 // Copy remainder of byval arg to it with memcpy.
3608 unsigned MemCpySize = ByValSize - Offset;
3609 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3610 DAG.getConstant(Offset, PtrTy));
3611 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
3612 DAG.getIntPtrConstant(ByVal.Address));
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003613 Chain = DAG.getMemcpy(Chain, DL, Dst, Src, DAG.getConstant(MemCpySize, PtrTy),
3614 Alignment, /*isVolatile=*/false, /*AlwaysInline=*/false,
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003615 MachinePointerInfo(0), MachinePointerInfo(0));
3616 MemOpChains.push_back(Chain);
3617}
Akira Hatanaka2a134022012-10-27 00:21:13 +00003618
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003619void MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
3620 const MipsCC &CC, SDValue Chain,
3621 SDLoc DL, SelectionDAG &DAG) const {
Akira Hatanaka2a134022012-10-27 00:21:13 +00003622 unsigned NumRegs = CC.numIntArgRegs();
3623 const uint16_t *ArgRegs = CC.intArgRegs();
3624 const CCState &CCInfo = CC.getCCInfo();
3625 unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs, NumRegs);
3626 unsigned RegSize = CC.regSize();
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00003627 MVT RegTy = MVT::getIntegerVT(RegSize * 8);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003628 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3629 MachineFunction &MF = DAG.getMachineFunction();
3630 MachineFrameInfo *MFI = MF.getFrameInfo();
3631 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3632
3633 // Offset of the first variable argument from stack pointer.
3634 int VaArgOffset;
3635
3636 if (NumRegs == Idx)
3637 VaArgOffset = RoundUpToAlignment(CCInfo.getNextStackOffset(), RegSize);
3638 else
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003639 VaArgOffset = (int)CC.reservedArgArea() - (int)(RegSize * (NumRegs - Idx));
Akira Hatanaka2a134022012-10-27 00:21:13 +00003640
3641 // Record the frame index of the first variable argument
3642 // which is a value necessary to VASTART.
3643 int FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
3644 MipsFI->setVarArgsFrameIndex(FI);
3645
3646 // Copy the integer registers that have not been used for argument passing
3647 // to the argument register save area. For O32, the save area is allocated
3648 // in the caller's stack frame, while for N32/64, it is allocated in the
3649 // callee's stack frame.
3650 for (unsigned I = Idx; I < NumRegs; ++I, VaArgOffset += RegSize) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003651 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003652 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
3653 FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
3654 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
3655 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
3656 MachinePointerInfo(), false, false, 0);
3657 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(0);
3658 OutChains.push_back(Store);
3659 }
3660}