blob: 220b1c4a3507766395ec3ca6c4ac934904abd5d6 [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"
Bill Wendlingdf7dd282014-01-05 01:47:20 +000034#include "llvm/IR/LLVMContext.h"
Akira Hatanaka90131ac2012-10-19 21:47:33 +000035#include "llvm/Support/CommandLine.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000036#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000037#include "llvm/Support/ErrorHandling.h"
NAKAMURA Takumie30303f2012-04-21 15:31:45 +000038#include "llvm/Support/raw_ostream.h"
Akira Hatanaka7473b472013-08-14 00:21:25 +000039#include <cctype>
NAKAMURA Takumie30303f2012-04-21 15:31:45 +000040
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000041using namespace llvm;
42
Akira Hatanaka90131ac2012-10-19 21:47:33 +000043STATISTIC(NumTailCalls, "Number of tail calls");
44
45static cl::opt<bool>
Akira Hatanaka59f299f2012-11-21 20:21:11 +000046LargeGOT("mxgot", cl::Hidden,
47 cl::desc("MIPS: Enable GOT larger than 64k."), cl::init(false));
48
Akira Hatanaka1cb02422013-05-20 18:07:43 +000049static cl::opt<bool>
Akira Hatanakabe76cd02013-05-21 17:17:59 +000050NoZeroDivCheck("mno-check-zero-division", cl::Hidden,
Akira Hatanaka1cb02422013-05-20 18:07:43 +000051 cl::desc("MIPS: Don't trap on integer division by zero."),
52 cl::init(false));
53
Akira Hatanakaac8c6692012-10-27 00:29:43 +000054static const uint16_t O32IntRegs[4] = {
55 Mips::A0, Mips::A1, Mips::A2, Mips::A3
56};
57
58static const uint16_t Mips64IntRegs[8] = {
59 Mips::A0_64, Mips::A1_64, Mips::A2_64, Mips::A3_64,
60 Mips::T0_64, Mips::T1_64, Mips::T2_64, Mips::T3_64
61};
62
63static const uint16_t Mips64DPRegs[8] = {
64 Mips::D12_64, Mips::D13_64, Mips::D14_64, Mips::D15_64,
65 Mips::D16_64, Mips::D17_64, Mips::D18_64, Mips::D19_64
66};
67
Jia Liuf54f60f2012-02-28 07:46:26 +000068// If I is a shifted mask, set the size (Size) and the first bit of the
Akira Hatanaka73d78b72011-08-18 20:07:42 +000069// mask (Pos), and return true.
Jia Liuf54f60f2012-02-28 07:46:26 +000070// For example, if I is 0x003ff800, (Pos, Size) = (11, 11).
Akira Hatanaka0bb60d892013-03-12 00:16:36 +000071static bool isShiftedMask(uint64_t I, uint64_t &Pos, uint64_t &Size) {
Akira Hatanaka20cee2e2011-12-05 21:26:34 +000072 if (!isShiftedMask_64(I))
Akira Hatanaka4c0a7122013-10-07 19:33:02 +000073 return false;
Akira Hatanaka5360f882011-08-17 02:05:42 +000074
Akira Hatanaka20cee2e2011-12-05 21:26:34 +000075 Size = CountPopulation_64(I);
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +000076 Pos = countTrailingZeros(I);
Akira Hatanaka73d78b72011-08-18 20:07:42 +000077 return true;
Akira Hatanaka5360f882011-08-17 02:05:42 +000078}
79
Akira Hatanaka96ca1822013-03-13 00:54:29 +000080SDValue MipsTargetLowering::getGlobalReg(SelectionDAG &DAG, EVT Ty) const {
Akira Hatanakab049aef2012-02-24 22:34:47 +000081 MipsFunctionInfo *FI = DAG.getMachineFunction().getInfo<MipsFunctionInfo>();
82 return DAG.getRegister(FI->getGlobalBaseReg(), Ty);
83}
84
Akira Hatanakad8f10ce2013-09-27 19:51:35 +000085SDValue MipsTargetLowering::getTargetNode(GlobalAddressSDNode *N, EVT Ty,
86 SelectionDAG &DAG,
Akira Hatanaka96ca1822013-03-13 00:54:29 +000087 unsigned Flag) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +000088 return DAG.getTargetGlobalAddress(N->getGlobal(), SDLoc(N), Ty, 0, Flag);
Akira Hatanakafd04ad42012-11-21 20:26:38 +000089}
90
Akira Hatanakad8f10ce2013-09-27 19:51:35 +000091SDValue MipsTargetLowering::getTargetNode(ExternalSymbolSDNode *N, EVT Ty,
92 SelectionDAG &DAG,
93 unsigned Flag) const {
94 return DAG.getTargetExternalSymbol(N->getSymbol(), Ty, Flag);
95}
96
97SDValue MipsTargetLowering::getTargetNode(BlockAddressSDNode *N, EVT Ty,
98 SelectionDAG &DAG,
99 unsigned Flag) const {
100 return DAG.getTargetBlockAddress(N->getBlockAddress(), Ty, 0, Flag);
101}
102
103SDValue MipsTargetLowering::getTargetNode(JumpTableSDNode *N, EVT Ty,
104 SelectionDAG &DAG,
105 unsigned Flag) const {
106 return DAG.getTargetJumpTable(N->getIndex(), Ty, Flag);
107}
108
109SDValue MipsTargetLowering::getTargetNode(ConstantPoolSDNode *N, EVT Ty,
110 SelectionDAG &DAG,
111 unsigned Flag) const {
112 return DAG.getTargetConstantPool(N->getConstVal(), Ty, N->getAlignment(),
113 N->getOffset(), Flag);
Akira Hatanakafd04ad42012-11-21 20:26:38 +0000114}
115
Chris Lattner5e693ed2009-07-28 03:13:23 +0000116const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
117 switch (Opcode) {
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000118 case MipsISD::JmpLink: return "MipsISD::JmpLink";
Akira Hatanaka91318df2012-10-19 20:59:39 +0000119 case MipsISD::TailCall: return "MipsISD::TailCall";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000120 case MipsISD::Hi: return "MipsISD::Hi";
121 case MipsISD::Lo: return "MipsISD::Lo";
122 case MipsISD::GPRel: return "MipsISD::GPRel";
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +0000123 case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000124 case MipsISD::Ret: return "MipsISD::Ret";
Akira Hatanakac0b02062013-01-30 00:26:49 +0000125 case MipsISD::EH_RETURN: return "MipsISD::EH_RETURN";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000126 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
127 case MipsISD::FPCmp: return "MipsISD::FPCmp";
128 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
129 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000130 case MipsISD::TruncIntFP: return "MipsISD::TruncIntFP";
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000131 case MipsISD::MFHI: return "MipsISD::MFHI";
132 case MipsISD::MFLO: return "MipsISD::MFLO";
133 case MipsISD::MTLOHI: return "MipsISD::MTLOHI";
Akira Hatanaka28721bd2013-03-30 01:14:04 +0000134 case MipsISD::Mult: return "MipsISD::Mult";
135 case MipsISD::Multu: return "MipsISD::Multu";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000136 case MipsISD::MAdd: return "MipsISD::MAdd";
137 case MipsISD::MAddu: return "MipsISD::MAddu";
138 case MipsISD::MSub: return "MipsISD::MSub";
139 case MipsISD::MSubu: return "MipsISD::MSubu";
140 case MipsISD::DivRem: return "MipsISD::DivRem";
141 case MipsISD::DivRemU: return "MipsISD::DivRemU";
Akira Hatanaka28721bd2013-03-30 01:14:04 +0000142 case MipsISD::DivRem16: return "MipsISD::DivRem16";
143 case MipsISD::DivRemU16: return "MipsISD::DivRemU16";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000144 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
145 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
Akira Hatanakafaa88c02011-12-12 22:38:19 +0000146 case MipsISD::Wrapper: return "MipsISD::Wrapper";
Akira Hatanakaa4c09bc2011-07-19 23:30:50 +0000147 case MipsISD::Sync: return "MipsISD::Sync";
Akira Hatanaka5360f882011-08-17 02:05:42 +0000148 case MipsISD::Ext: return "MipsISD::Ext";
149 case MipsISD::Ins: return "MipsISD::Ins";
Akira Hatanakab9ebf8d2012-06-02 00:03:12 +0000150 case MipsISD::LWL: return "MipsISD::LWL";
151 case MipsISD::LWR: return "MipsISD::LWR";
152 case MipsISD::SWL: return "MipsISD::SWL";
153 case MipsISD::SWR: return "MipsISD::SWR";
154 case MipsISD::LDL: return "MipsISD::LDL";
155 case MipsISD::LDR: return "MipsISD::LDR";
156 case MipsISD::SDL: return "MipsISD::SDL";
157 case MipsISD::SDR: return "MipsISD::SDR";
Akira Hatanaka233ac532012-09-21 23:52:47 +0000158 case MipsISD::EXTP: return "MipsISD::EXTP";
159 case MipsISD::EXTPDP: return "MipsISD::EXTPDP";
160 case MipsISD::EXTR_S_H: return "MipsISD::EXTR_S_H";
161 case MipsISD::EXTR_W: return "MipsISD::EXTR_W";
162 case MipsISD::EXTR_R_W: return "MipsISD::EXTR_R_W";
163 case MipsISD::EXTR_RS_W: return "MipsISD::EXTR_RS_W";
164 case MipsISD::SHILO: return "MipsISD::SHILO";
165 case MipsISD::MTHLIP: return "MipsISD::MTHLIP";
166 case MipsISD::MULT: return "MipsISD::MULT";
167 case MipsISD::MULTU: return "MipsISD::MULTU";
Jia Liu434874d2013-03-04 01:06:54 +0000168 case MipsISD::MADD_DSP: return "MipsISD::MADD_DSP";
Akira Hatanaka233ac532012-09-21 23:52:47 +0000169 case MipsISD::MADDU_DSP: return "MipsISD::MADDU_DSP";
170 case MipsISD::MSUB_DSP: return "MipsISD::MSUB_DSP";
171 case MipsISD::MSUBU_DSP: return "MipsISD::MSUBU_DSP";
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000172 case MipsISD::SHLL_DSP: return "MipsISD::SHLL_DSP";
173 case MipsISD::SHRA_DSP: return "MipsISD::SHRA_DSP";
174 case MipsISD::SHRL_DSP: return "MipsISD::SHRL_DSP";
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000175 case MipsISD::SETCC_DSP: return "MipsISD::SETCC_DSP";
176 case MipsISD::SELECT_CC_DSP: return "MipsISD::SELECT_CC_DSP";
Daniel Sandersce09d072013-08-28 12:14:50 +0000177 case MipsISD::VALL_ZERO: return "MipsISD::VALL_ZERO";
178 case MipsISD::VANY_ZERO: return "MipsISD::VANY_ZERO";
179 case MipsISD::VALL_NONZERO: return "MipsISD::VALL_NONZERO";
180 case MipsISD::VANY_NONZERO: return "MipsISD::VANY_NONZERO";
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000181 case MipsISD::VCEQ: return "MipsISD::VCEQ";
182 case MipsISD::VCLE_S: return "MipsISD::VCLE_S";
183 case MipsISD::VCLE_U: return "MipsISD::VCLE_U";
184 case MipsISD::VCLT_S: return "MipsISD::VCLT_S";
185 case MipsISD::VCLT_U: return "MipsISD::VCLT_U";
Daniel Sanders3ce56622013-09-24 12:18:31 +0000186 case MipsISD::VSMAX: return "MipsISD::VSMAX";
187 case MipsISD::VSMIN: return "MipsISD::VSMIN";
188 case MipsISD::VUMAX: return "MipsISD::VUMAX";
189 case MipsISD::VUMIN: return "MipsISD::VUMIN";
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000190 case MipsISD::VEXTRACT_SEXT_ELT: return "MipsISD::VEXTRACT_SEXT_ELT";
191 case MipsISD::VEXTRACT_ZEXT_ELT: return "MipsISD::VEXTRACT_ZEXT_ELT";
Daniel Sandersf7456c72013-09-23 13:22:24 +0000192 case MipsISD::VNOR: return "MipsISD::VNOR";
Daniel Sanderse5087042013-09-24 14:02:15 +0000193 case MipsISD::VSHF: return "MipsISD::VSHF";
Daniel Sanders26307182013-09-24 14:20:00 +0000194 case MipsISD::SHF: return "MipsISD::SHF";
Daniel Sanders2ed228b2013-09-24 14:36:12 +0000195 case MipsISD::ILVEV: return "MipsISD::ILVEV";
196 case MipsISD::ILVOD: return "MipsISD::ILVOD";
197 case MipsISD::ILVL: return "MipsISD::ILVL";
198 case MipsISD::ILVR: return "MipsISD::ILVR";
Daniel Sandersfae5f2a2013-09-24 14:53:25 +0000199 case MipsISD::PCKEV: return "MipsISD::PCKEV";
200 case MipsISD::PCKOD: return "MipsISD::PCKOD";
Akira Hatanaka15506782011-06-07 18:58:42 +0000201 default: return NULL;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000202 }
203}
204
205MipsTargetLowering::
Chris Lattner5e693ed2009-07-28 03:13:23 +0000206MipsTargetLowering(MipsTargetMachine &TM)
Akira Hatanaka7b502922011-09-26 21:47:02 +0000207 : TargetLowering(TM, new MipsTargetObjectFile()),
208 Subtarget(&TM.getSubtarget<MipsSubtarget>()),
Akira Hatanaka7989f152011-10-28 18:47:24 +0000209 HasMips64(Subtarget->hasMips64()), IsN64(Subtarget->isABI_N64()),
210 IsO32(Subtarget->isABI_O32()) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000211 // Mips does not have i1 type, so use i32 for
Wesley Peck527da1b2010-11-23 03:31:01 +0000212 // setcc operations results (slt, sgt, ...).
Duncan Sands8d6e2e12008-11-23 15:47:28 +0000213 setBooleanContents(ZeroOrOneBooleanContent);
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000214 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000215
Wesley Peck527da1b2010-11-23 03:31:01 +0000216 // Load extented operations for i1 types must be promoted
Owen Anderson9f944592009-08-11 20:47:22 +0000217 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
218 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
219 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000220
Eli Friedman1fa07e12009-07-17 04:07:24 +0000221 // MIPS doesn't have extending float->double load/store
Owen Anderson9f944592009-08-11 20:47:22 +0000222 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
223 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman39d6faa2009-07-17 02:28:12 +0000224
Wesley Peck527da1b2010-11-23 03:31:01 +0000225 // Used by legalize types to correctly generate the setcc result.
226 // Without this, every float setcc comes with a AND/OR with the result,
227 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +0000228 // which is used implicitly by brcond and select operations.
Owen Anderson9f944592009-08-11 20:47:22 +0000229 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +0000230
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000231 // Mips Custom Operations
Akira Hatanaka0f693a82013-03-06 21:32:03 +0000232 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000233 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000234 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000235 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
236 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
237 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
238 setOperationAction(ISD::SELECT, MVT::f32, Custom);
239 setOperationAction(ISD::SELECT, MVT::f64, Custom);
240 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Akira Hatanaka24cf4e32012-07-11 19:32:27 +0000241 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
242 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Akira Hatanakab7f78592012-03-09 23:46:03 +0000243 setOperationAction(ISD::SETCC, MVT::f32, Custom);
244 setOperationAction(ISD::SETCC, MVT::f64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000245 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +0000246 setOperationAction(ISD::VASTART, MVT::Other, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000247 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
248 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000249 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000250
Akira Hatanaka7f4c9d12012-04-11 22:49:04 +0000251 if (!TM.Options.NoNaNsFPMath) {
252 setOperationAction(ISD::FABS, MVT::f32, Custom);
253 setOperationAction(ISD::FABS, MVT::f64, Custom);
254 }
255
Akira Hatanakada00aa82012-03-10 00:03:50 +0000256 if (HasMips64) {
257 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
258 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
259 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
260 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
261 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
262 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Akira Hatanaka019e5922012-06-02 00:04:42 +0000263 setOperationAction(ISD::LOAD, MVT::i64, Custom);
264 setOperationAction(ISD::STORE, MVT::i64, Custom);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000265 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000266 }
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +0000267
Akira Hatanaka0a8ab712012-05-09 00:55:21 +0000268 if (!HasMips64) {
269 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
270 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
271 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
272 }
273
Akira Hatanaka28e02ec2012-11-07 19:10:58 +0000274 setOperationAction(ISD::ADD, MVT::i32, Custom);
275 if (HasMips64)
276 setOperationAction(ISD::ADD, MVT::i64, Custom);
277
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000278 setOperationAction(ISD::SDIV, MVT::i32, Expand);
279 setOperationAction(ISD::SREM, MVT::i32, Expand);
280 setOperationAction(ISD::UDIV, MVT::i32, Expand);
281 setOperationAction(ISD::UREM, MVT::i32, Expand);
Akira Hatanakab1538f92011-10-03 21:06:13 +0000282 setOperationAction(ISD::SDIV, MVT::i64, Expand);
283 setOperationAction(ISD::SREM, MVT::i64, Expand);
284 setOperationAction(ISD::UDIV, MVT::i64, Expand);
285 setOperationAction(ISD::UREM, MVT::i64, Expand);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000286
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000287 // Operations not directly supported by Mips.
Tom Stellardb1588fc2013-03-08 15:36:57 +0000288 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
289 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
290 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
291 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000292 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
293 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Akira Hatanaka79aed152011-12-20 23:40:56 +0000294 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000295 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Akira Hatanaka79aed152011-12-20 23:40:56 +0000296 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000297 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
298 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
Akira Hatanaka410ce9c2011-12-21 00:14:05 +0000299 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000300 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
Akira Hatanaka410ce9c2011-12-21 00:14:05 +0000301 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000302 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
303 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
304 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
305 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000306 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Akira Hatanaka7ba8a8d2011-09-30 18:51:46 +0000307 setOperationAction(ISD::ROTL, MVT::i64, Expand);
Akira Hatanaka33a25af2012-07-31 20:54:48 +0000308 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
309 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Bruno Cardoso Lopesd47180e2010-12-09 17:32:30 +0000310
Akira Hatanakabb49e722011-09-20 23:53:09 +0000311 if (!Subtarget->hasMips32r2())
Bruno Cardoso Lopesd47180e2010-12-09 17:32:30 +0000312 setOperationAction(ISD::ROTR, MVT::i32, Expand);
313
Akira Hatanaka7ba8a8d2011-09-30 18:51:46 +0000314 if (!Subtarget->hasMips64r2())
315 setOperationAction(ISD::ROTR, MVT::i64, Expand);
316
Owen Anderson9f944592009-08-11 20:47:22 +0000317 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes22b69db2011-03-04 18:54:14 +0000318 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000319 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes22b69db2011-03-04 18:54:14 +0000320 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000321 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
322 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000323 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
324 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanakadfb8cda2011-05-23 22:23:58 +0000325 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000326 setOperationAction(ISD::FLOG, MVT::f32, Expand);
327 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
328 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
329 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000330 setOperationAction(ISD::FMA, MVT::f32, Expand);
331 setOperationAction(ISD::FMA, MVT::f64, Expand);
Akira Hatanaka0603ad82012-03-29 18:43:11 +0000332 setOperationAction(ISD::FREM, MVT::f32, Expand);
333 setOperationAction(ISD::FREM, MVT::f64, Expand);
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000334
Akira Hatanaka47ad6742012-04-11 22:59:08 +0000335 if (!TM.Options.NoNaNsFPMath) {
336 setOperationAction(ISD::FNEG, MVT::f32, Expand);
337 setOperationAction(ISD::FNEG, MVT::f64, Expand);
338 }
339
Akira Hatanakac0b02062013-01-30 00:26:49 +0000340 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
341
Bruno Cardoso Lopes048ffab2011-03-09 19:22:22 +0000342 setOperationAction(ISD::VAARG, MVT::Other, Expand);
343 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
344 setOperationAction(ISD::VAEND, MVT::Other, Expand);
345
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000346 // Use the default for now
Owen Anderson9f944592009-08-11 20:47:22 +0000347 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
348 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman26a48482011-07-27 22:21:52 +0000349
Jia Liuf54f60f2012-02-28 07:46:26 +0000350 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
351 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
352 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
353 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
Eli Friedman7dfa7912011-08-29 18:23:02 +0000354
Eli Friedman30a49e92011-08-03 21:06:02 +0000355 setInsertFencesForAtomic(true);
356
Bruno Cardoso Lopesbcc21392008-07-09 05:32:22 +0000357 if (!Subtarget->hasSEInReg()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000358 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
359 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000360 }
361
Akira Hatanaka1d8efab2011-12-21 00:20:27 +0000362 if (!Subtarget->hasBitCount()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000363 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Akira Hatanaka1d8efab2011-12-21 00:20:27 +0000364 setOperationAction(ISD::CTLZ, MVT::i64, Expand);
365 }
Bruno Cardoso Lopes93da7e62008-08-08 06:16:31 +0000366
Akira Hatanaka4706ac92011-12-20 23:56:43 +0000367 if (!Subtarget->hasSwap()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000368 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Akira Hatanaka4706ac92011-12-20 23:56:43 +0000369 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
370 }
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000371
Akira Hatanaka019e5922012-06-02 00:04:42 +0000372 if (HasMips64) {
373 setLoadExtAction(ISD::SEXTLOAD, MVT::i32, Custom);
374 setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, Custom);
375 setLoadExtAction(ISD::EXTLOAD, MVT::i32, Custom);
376 setTruncStoreAction(MVT::i64, MVT::i32, Custom);
377 }
378
Akira Hatanakaa3d9ab92013-07-26 20:58:55 +0000379 setOperationAction(ISD::TRAP, MVT::Other, Legal);
380
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000381 setTargetDAGCombine(ISD::SDIVREM);
382 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka5e152182012-03-08 03:26:37 +0000383 setTargetDAGCombine(ISD::SELECT);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000384 setTargetDAGCombine(ISD::AND);
385 setTargetDAGCombine(ISD::OR);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000386 setTargetDAGCombine(ISD::ADD);
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000387
Akira Hatanaka956dd222012-03-08 01:59:33 +0000388 setMinFunctionAlignment(HasMips64 ? 3 : 2);
Eli Friedman2518f832011-05-06 20:34:06 +0000389
Akira Hatanaka961883c2012-02-02 03:17:04 +0000390 setStackPointerRegisterToSaveRestore(IsN64 ? Mips::SP_64 : Mips::SP);
Akira Hatanakaaa560002011-05-26 18:59:03 +0000391
Akira Hatanakaf0295372012-02-02 03:13:40 +0000392 setExceptionPointerRegister(IsN64 ? Mips::A0_64 : Mips::A0);
393 setExceptionSelectorRegister(IsN64 ? Mips::A1_64 : Mips::A1);
Akira Hatanaka1daf8c22012-06-13 19:33:32 +0000394
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000395 MaxStoresPerMemcpy = 16;
Zoran Jovanovicff9d5f32013-12-19 16:12:56 +0000396
397 isMicroMips = Subtarget->inMicroMipsMode();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000398}
399
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000400const MipsTargetLowering *MipsTargetLowering::create(MipsTargetMachine &TM) {
401 if (TM.getSubtargetImpl()->inMips16Mode())
402 return llvm::createMips16TargetLowering(TM);
Jia Liuf54f60f2012-02-28 07:46:26 +0000403
Akira Hatanaka96ca1822013-03-13 00:54:29 +0000404 return llvm::createMipsSETargetLowering(TM);
Akira Hatanaka2fcc1cf2011-08-12 21:30:06 +0000405}
406
Matt Arsenault758659232013-05-18 00:21:46 +0000407EVT MipsTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Akira Hatanakab13b3332013-01-04 20:06:01 +0000408 if (!VT.isVector())
409 return MVT::i32;
410 return VT.changeVectorElementTypeToInteger();
Scott Michela6729e82008-03-10 15:42:14 +0000411}
412
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000413static SDValue performDivRemCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000414 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000415 const MipsSubtarget *Subtarget) {
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000416 if (DCI.isBeforeLegalizeOps())
417 return SDValue();
418
Akira Hatanakab1538f92011-10-03 21:06:13 +0000419 EVT Ty = N->getValueType(0);
Akira Hatanaka8002a3f2013-08-14 00:47:08 +0000420 unsigned LO = (Ty == MVT::i32) ? Mips::LO0 : Mips::LO0_64;
421 unsigned HI = (Ty == MVT::i32) ? Mips::HI0 : Mips::HI0_64;
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000422 unsigned Opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem16 :
423 MipsISD::DivRemU16;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000424 SDLoc DL(N);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000425
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000426 SDValue DivRem = DAG.getNode(Opc, DL, MVT::Glue,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000427 N->getOperand(0), N->getOperand(1));
428 SDValue InChain = DAG.getEntryNode();
429 SDValue InGlue = DivRem;
430
431 // insert MFLO
432 if (N->hasAnyUseOfValue(0)) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000433 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, DL, LO, Ty,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000434 InGlue);
435 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
436 InChain = CopyFromLo.getValue(1);
437 InGlue = CopyFromLo.getValue(2);
438 }
439
440 // insert MFHI
441 if (N->hasAnyUseOfValue(1)) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000442 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, DL,
Akira Hatanakab1538f92011-10-03 21:06:13 +0000443 HI, Ty, InGlue);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000444 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
445 }
446
447 return SDValue();
448}
449
Akira Hatanaka89af5892013-04-18 01:00:46 +0000450static Mips::CondCode condCodeToFCC(ISD::CondCode CC) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000451 switch (CC) {
452 default: llvm_unreachable("Unknown fp condition code!");
453 case ISD::SETEQ:
454 case ISD::SETOEQ: return Mips::FCOND_OEQ;
455 case ISD::SETUNE: return Mips::FCOND_UNE;
456 case ISD::SETLT:
457 case ISD::SETOLT: return Mips::FCOND_OLT;
458 case ISD::SETGT:
459 case ISD::SETOGT: return Mips::FCOND_OGT;
460 case ISD::SETLE:
461 case ISD::SETOLE: return Mips::FCOND_OLE;
462 case ISD::SETGE:
463 case ISD::SETOGE: return Mips::FCOND_OGE;
464 case ISD::SETULT: return Mips::FCOND_ULT;
465 case ISD::SETULE: return Mips::FCOND_ULE;
466 case ISD::SETUGT: return Mips::FCOND_UGT;
467 case ISD::SETUGE: return Mips::FCOND_UGE;
468 case ISD::SETUO: return Mips::FCOND_UN;
469 case ISD::SETO: return Mips::FCOND_OR;
470 case ISD::SETNE:
471 case ISD::SETONE: return Mips::FCOND_ONE;
472 case ISD::SETUEQ: return Mips::FCOND_UEQ;
473 }
474}
475
476
Akira Hatanakaf0ea5002013-03-30 01:16:38 +0000477/// This function returns true if the floating point conditional branches and
478/// conditional moves which use condition code CC should be inverted.
479static bool invertFPCondCodeUser(Mips::CondCode CC) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000480 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
481 return false;
482
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000483 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
484 "Illegal Condition Code");
Akira Hatanakaa5352702011-03-31 18:26:17 +0000485
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000486 return true;
Akira Hatanakaa5352702011-03-31 18:26:17 +0000487}
488
489// Creates and returns an FPCmp node from a setcc node.
490// Returns Op if setcc is not a floating point comparison.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000491static SDValue createFPCmp(SelectionDAG &DAG, const SDValue &Op) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000492 // must be a SETCC node
493 if (Op.getOpcode() != ISD::SETCC)
494 return Op;
495
496 SDValue LHS = Op.getOperand(0);
497
498 if (!LHS.getValueType().isFloatingPoint())
499 return Op;
500
501 SDValue RHS = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000502 SDLoc DL(Op);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000503
Akira Hatanakaaef55c82011-04-15 21:00:26 +0000504 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
505 // node if necessary.
Akira Hatanakaa5352702011-03-31 18:26:17 +0000506 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
507
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000508 return DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS,
Akira Hatanaka89af5892013-04-18 01:00:46 +0000509 DAG.getConstant(condCodeToFCC(CC), MVT::i32));
Akira Hatanakaa5352702011-03-31 18:26:17 +0000510}
511
512// Creates and returns a CMovFPT/F node.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000513static SDValue createCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000514 SDValue False, SDLoc DL) {
Akira Hatanakaf0ea5002013-03-30 01:16:38 +0000515 ConstantSDNode *CC = cast<ConstantSDNode>(Cond.getOperand(2));
516 bool invert = invertFPCondCodeUser((Mips::CondCode)CC->getSExtValue());
Akira Hatanaka8bce21c2013-07-26 20:51:20 +0000517 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000518
519 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
Akira Hatanaka8bce21c2013-07-26 20:51:20 +0000520 True.getValueType(), True, FCC0, False, Cond);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000521}
522
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000523static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000524 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000525 const MipsSubtarget *Subtarget) {
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000526 if (DCI.isBeforeLegalizeOps())
527 return SDValue();
528
529 SDValue SetCC = N->getOperand(0);
530
531 if ((SetCC.getOpcode() != ISD::SETCC) ||
532 !SetCC.getOperand(0).getValueType().isInteger())
533 return SDValue();
534
535 SDValue False = N->getOperand(2);
536 EVT FalseTy = False.getValueType();
537
538 if (!FalseTy.isInteger())
539 return SDValue();
540
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000541 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(False);
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000542
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000543 // If the RHS (False) is 0, we swap the order of the operands
544 // of ISD::SELECT (obviously also inverting the condition) so that we can
545 // take advantage of conditional moves using the $0 register.
546 // Example:
547 // return (a != 0) ? x : 0;
548 // load $reg, x
549 // movz $reg, $0, a
550 if (!FalseC)
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000551 return SDValue();
552
Andrew Trickef9de2a2013-05-25 02:42:55 +0000553 const SDLoc DL(N);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000554
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000555 if (!FalseC->getZExtValue()) {
556 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
557 SDValue True = N->getOperand(1);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000558
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000559 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
560 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
561
562 return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True);
563 }
564
Matheus Almeidaa6beac12013-12-05 12:07:05 +0000565 // If both operands are integer constants there's a possibility that we
566 // can do some interesting optimizations.
567 SDValue True = N->getOperand(1);
568 ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(True);
569
570 if (!TrueC || !True.getValueType().isInteger())
571 return SDValue();
572
573 // We'll also ignore MVT::i64 operands as this optimizations proves
574 // to be ineffective because of the required sign extensions as the result
575 // of a SETCC operator is always MVT::i32 for non-vector types.
576 if (True.getValueType() == MVT::i64)
577 return SDValue();
578
579 int64_t Diff = TrueC->getSExtValue() - FalseC->getSExtValue();
580
581 // 1) (a < x) ? y : y-1
582 // slti $reg1, a, x
583 // addiu $reg2, $reg1, y-1
584 if (Diff == 1)
585 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, False);
586
587 // 2) (a < x) ? y-1 : y
588 // slti $reg1, a, x
589 // xor $reg1, $reg1, 1
590 // addiu $reg2, $reg1, y-1
591 if (Diff == -1) {
592 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
593 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
594 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
595 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, True);
596 }
597
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000598 // Couldn't optimize.
599 return SDValue();
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000600}
601
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000602static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000603 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000604 const MipsSubtarget *Subtarget) {
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000605 // Pattern match EXT.
606 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
607 // => ext $dst, $src, size, pos
Akira Hatanaka4a3836b2013-10-09 23:36:17 +0000608 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasExtractInsert())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000609 return SDValue();
610
611 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000612 unsigned ShiftRightOpc = ShiftRight.getOpcode();
613
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000614 // Op's first operand must be a shift right.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000615 if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL)
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000616 return SDValue();
617
618 // The second operand of the shift must be an immediate.
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000619 ConstantSDNode *CN;
620 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
621 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000622
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000623 uint64_t Pos = CN->getZExtValue();
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000624 uint64_t SMPos, SMSize;
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000625
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000626 // Op's second operand must be a shifted mask.
627 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000628 !isShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000629 return SDValue();
630
631 // Return if the shifted mask does not start at bit 0 or the sum of its size
632 // and Pos exceeds the word's size.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000633 EVT ValTy = N->getValueType(0);
634 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000635 return SDValue();
636
Andrew Trickef9de2a2013-05-25 02:42:55 +0000637 return DAG.getNode(MipsISD::Ext, SDLoc(N), ValTy,
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000638 ShiftRight.getOperand(0), DAG.getConstant(Pos, MVT::i32),
Akira Hatanakaeea541c2011-08-17 22:59:46 +0000639 DAG.getConstant(SMSize, MVT::i32));
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000640}
Jia Liuf54f60f2012-02-28 07:46:26 +0000641
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000642static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000643 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000644 const MipsSubtarget *Subtarget) {
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000645 // Pattern match INS.
646 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
Jia Liuf54f60f2012-02-28 07:46:26 +0000647 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000648 // => ins $dst, $src, size, pos, $src1
Akira Hatanaka4a3836b2013-10-09 23:36:17 +0000649 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasExtractInsert())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000650 return SDValue();
651
652 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
653 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
654 ConstantSDNode *CN;
655
656 // See if Op's first operand matches (and $src1 , mask0).
657 if (And0.getOpcode() != ISD::AND)
658 return SDValue();
659
660 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000661 !isShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000662 return SDValue();
663
664 // See if Op's second operand matches (and (shl $src, pos), mask1).
665 if (And1.getOpcode() != ISD::AND)
666 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000667
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000668 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000669 !isShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000670 return SDValue();
671
672 // The shift masks must have the same position and size.
673 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
674 return SDValue();
675
676 SDValue Shl = And1.getOperand(0);
677 if (Shl.getOpcode() != ISD::SHL)
678 return SDValue();
679
680 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
681 return SDValue();
682
683 unsigned Shamt = CN->getZExtValue();
684
685 // Return if the shift amount and the first bit position of mask are not the
Jia Liuf54f60f2012-02-28 07:46:26 +0000686 // same.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000687 EVT ValTy = N->getValueType(0);
688 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000689 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000690
Andrew Trickef9de2a2013-05-25 02:42:55 +0000691 return DAG.getNode(MipsISD::Ins, SDLoc(N), ValTy, Shl.getOperand(0),
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000692 DAG.getConstant(SMPos0, MVT::i32),
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000693 DAG.getConstant(SMSize0, MVT::i32), And0.getOperand(0));
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000694}
Jia Liuf54f60f2012-02-28 07:46:26 +0000695
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000696static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000697 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000698 const MipsSubtarget *Subtarget) {
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000699 // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt))
700
701 if (DCI.isBeforeLegalizeOps())
702 return SDValue();
703
704 SDValue Add = N->getOperand(1);
705
706 if (Add.getOpcode() != ISD::ADD)
707 return SDValue();
708
709 SDValue Lo = Add.getOperand(1);
710
711 if ((Lo.getOpcode() != MipsISD::Lo) ||
712 (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable))
713 return SDValue();
714
715 EVT ValTy = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000716 SDLoc DL(N);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000717
718 SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, N->getOperand(0),
719 Add.getOperand(0));
720 return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo);
721}
722
Bruno Cardoso Lopes61a61e92011-02-10 18:05:10 +0000723SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000724 const {
725 SelectionDAG &DAG = DCI.DAG;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000726 unsigned Opc = N->getOpcode();
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000727
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000728 switch (Opc) {
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000729 default: break;
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000730 case ISD::SDIVREM:
731 case ISD::UDIVREM:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000732 return performDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000733 case ISD::SELECT:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000734 return performSELECTCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000735 case ISD::AND:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000736 return performANDCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000737 case ISD::OR:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000738 return performORCombine(N, DAG, DCI, Subtarget);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000739 case ISD::ADD:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000740 return performADDCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000741 }
742
743 return SDValue();
744}
745
Akira Hatanakafabb8cf2012-09-21 23:58:31 +0000746void
747MipsTargetLowering::LowerOperationWrapper(SDNode *N,
748 SmallVectorImpl<SDValue> &Results,
749 SelectionDAG &DAG) const {
750 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
751
752 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
753 Results.push_back(Res.getValue(I));
754}
755
756void
757MipsTargetLowering::ReplaceNodeResults(SDNode *N,
758 SmallVectorImpl<SDValue> &Results,
759 SelectionDAG &DAG) const {
Akira Hatanaka9da442f2013-04-30 21:17:07 +0000760 return LowerOperationWrapper(N, Results, DAG);
Akira Hatanakafabb8cf2012-09-21 23:58:31 +0000761}
762
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000763SDValue MipsTargetLowering::
Dan Gohman21cea8a2010-04-17 15:26:15 +0000764LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000765{
Wesley Peck527da1b2010-11-23 03:31:01 +0000766 switch (Op.getOpcode())
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000767 {
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000768 case ISD::BR_JT: return lowerBR_JT(Op, DAG);
769 case ISD::BRCOND: return lowerBRCOND(Op, DAG);
770 case ISD::ConstantPool: return lowerConstantPool(Op, DAG);
771 case ISD::GlobalAddress: return lowerGlobalAddress(Op, DAG);
772 case ISD::BlockAddress: return lowerBlockAddress(Op, DAG);
773 case ISD::GlobalTLSAddress: return lowerGlobalTLSAddress(Op, DAG);
774 case ISD::JumpTable: return lowerJumpTable(Op, DAG);
775 case ISD::SELECT: return lowerSELECT(Op, DAG);
776 case ISD::SELECT_CC: return lowerSELECT_CC(Op, DAG);
777 case ISD::SETCC: return lowerSETCC(Op, DAG);
778 case ISD::VASTART: return lowerVASTART(Op, DAG);
779 case ISD::FCOPYSIGN: return lowerFCOPYSIGN(Op, DAG);
780 case ISD::FABS: return lowerFABS(Op, DAG);
781 case ISD::FRAMEADDR: return lowerFRAMEADDR(Op, DAG);
782 case ISD::RETURNADDR: return lowerRETURNADDR(Op, DAG);
783 case ISD::EH_RETURN: return lowerEH_RETURN(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000784 case ISD::ATOMIC_FENCE: return lowerATOMIC_FENCE(Op, DAG);
785 case ISD::SHL_PARTS: return lowerShiftLeftParts(Op, DAG);
786 case ISD::SRA_PARTS: return lowerShiftRightParts(Op, DAG, true);
787 case ISD::SRL_PARTS: return lowerShiftRightParts(Op, DAG, false);
788 case ISD::LOAD: return lowerLOAD(Op, DAG);
789 case ISD::STORE: return lowerSTORE(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000790 case ISD::ADD: return lowerADD(Op, DAG);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000791 case ISD::FP_TO_SINT: return lowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000792 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000793 return SDValue();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000794}
795
Akira Hatanakae2489122011-04-15 21:51:11 +0000796//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000797// Lower helper functions
Akira Hatanakae2489122011-04-15 21:51:11 +0000798//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000799
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000800// addLiveIn - This helper function adds the specified physical register to the
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000801// MachineFunction as a live in value. It also creates a corresponding
802// virtual register for it.
803static unsigned
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000804addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000805{
Chris Lattnera10fff52007-12-31 04:13:23 +0000806 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
807 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000808 return VReg;
809}
810
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000811static MachineBasicBlock *expandPseudoDIV(MachineInstr *MI,
812 MachineBasicBlock &MBB,
813 const TargetInstrInfo &TII,
814 bool Is64Bit) {
815 if (NoZeroDivCheck)
816 return &MBB;
817
818 // Insert instruction "teq $divisor_reg, $zero, 7".
819 MachineBasicBlock::iterator I(MI);
820 MachineInstrBuilder MIB;
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000821 MachineOperand &Divisor = MI->getOperand(2);
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000822 MIB = BuildMI(MBB, llvm::next(I), MI->getDebugLoc(), TII.get(Mips::TEQ))
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000823 .addReg(Divisor.getReg(), getKillRegState(Divisor.isKill()))
824 .addReg(Mips::ZERO).addImm(7);
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000825
826 // Use the 32-bit sub-register if this is a 64-bit division.
827 if (Is64Bit)
828 MIB->getOperand(0).setSubReg(Mips::sub_32);
829
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000830 // Clear Divisor's kill flag.
831 Divisor.setIsKill(false);
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000832 return &MBB;
833}
834
Akira Hatanakae4bd0542012-09-27 02:15:57 +0000835MachineBasicBlock *
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +0000836MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +0000837 MachineBasicBlock *BB) const {
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +0000838 switch (MI->getOpcode()) {
Reed Kotler97ba5f22013-02-21 04:22:38 +0000839 default:
840 llvm_unreachable("Unexpected instr type to insert");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000841 case Mips::ATOMIC_LOAD_ADD_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000842 return emitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000843 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000844 return emitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000845 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000846 return emitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000847 case Mips::ATOMIC_LOAD_ADD_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000848 return emitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000849
850 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000851 return emitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000852 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000853 return emitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000854 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000855 return emitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000856 case Mips::ATOMIC_LOAD_AND_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000857 return emitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000858
859 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000860 return emitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000861 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000862 return emitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000863 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000864 return emitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000865 case Mips::ATOMIC_LOAD_OR_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000866 return emitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000867
868 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000869 return emitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000870 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000871 return emitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000872 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000873 return emitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000874 case Mips::ATOMIC_LOAD_XOR_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000875 return emitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000876
877 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000878 return emitAtomicBinaryPartword(MI, BB, 1, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000879 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000880 return emitAtomicBinaryPartword(MI, BB, 2, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000881 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000882 return emitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000883 case Mips::ATOMIC_LOAD_NAND_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000884 return emitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000885
886 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000887 return emitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000888 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000889 return emitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000890 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000891 return emitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000892 case Mips::ATOMIC_LOAD_SUB_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000893 return emitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000894
895 case Mips::ATOMIC_SWAP_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000896 return emitAtomicBinaryPartword(MI, BB, 1, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000897 case Mips::ATOMIC_SWAP_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000898 return emitAtomicBinaryPartword(MI, BB, 2, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000899 case Mips::ATOMIC_SWAP_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000900 return emitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000901 case Mips::ATOMIC_SWAP_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000902 return emitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000903
904 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000905 return emitAtomicCmpSwapPartword(MI, BB, 1);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000906 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000907 return emitAtomicCmpSwapPartword(MI, BB, 2);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000908 case Mips::ATOMIC_CMP_SWAP_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000909 return emitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000910 case Mips::ATOMIC_CMP_SWAP_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000911 return emitAtomicCmpSwap(MI, BB, 8);
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000912 case Mips::PseudoSDIV:
913 case Mips::PseudoUDIV:
914 return expandPseudoDIV(MI, *BB, *getTargetMachine().getInstrInfo(), false);
915 case Mips::PseudoDSDIV:
916 case Mips::PseudoDUDIV:
917 return expandPseudoDIV(MI, *BB, *getTargetMachine().getInstrInfo(), true);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000918 }
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +0000919}
920
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000921// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
922// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
923MachineBasicBlock *
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000924MipsTargetLowering::emitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher0713a9d2011-06-08 23:55:35 +0000925 unsigned Size, unsigned BinOpcode,
Akira Hatanaka15506782011-06-07 18:58:42 +0000926 bool Nand) const {
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000927 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000928
929 MachineFunction *MF = BB->getParent();
930 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000931 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000932 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000933 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000934 unsigned LL, SC, AND, NOR, ZERO, BEQ;
935
936 if (Size == 4) {
Zoran Jovanovicff9d5f32013-12-19 16:12:56 +0000937 LL = isMicroMips ? Mips::LL_MM : Mips::LL;
938 SC = isMicroMips ? Mips::SC_MM : Mips::SC;
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000939 AND = Mips::AND;
940 NOR = Mips::NOR;
941 ZERO = Mips::ZERO;
942 BEQ = Mips::BEQ;
943 }
944 else {
Akira Hatanaka6781fc12013-08-20 21:08:22 +0000945 LL = Mips::LLD;
946 SC = Mips::SCD;
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000947 AND = Mips::AND64;
948 NOR = Mips::NOR64;
949 ZERO = Mips::ZERO_64;
950 BEQ = Mips::BEQ64;
951 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000952
Akira Hatanaka0e019592011-07-19 20:11:17 +0000953 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000954 unsigned Ptr = MI->getOperand(1).getReg();
955 unsigned Incr = MI->getOperand(2).getReg();
956
Akira Hatanaka0e019592011-07-19 20:11:17 +0000957 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
958 unsigned AndRes = RegInfo.createVirtualRegister(RC);
959 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000960
961 // insert new blocks after the current block
962 const BasicBlock *LLVM_BB = BB->getBasicBlock();
963 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
964 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
965 MachineFunction::iterator It = BB;
966 ++It;
967 MF->insert(It, loopMBB);
968 MF->insert(It, exitMBB);
969
970 // Transfer the remainder of BB and its successor edges to exitMBB.
971 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka4c0a7122013-10-07 19:33:02 +0000972 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000973 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
974
975 // thisMBB:
976 // ...
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000977 // fallthrough --> loopMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000978 BB->addSuccessor(loopMBB);
Akira Hatanaka08636b42011-07-19 17:09:53 +0000979 loopMBB->addSuccessor(loopMBB);
980 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000981
982 // loopMBB:
983 // ll oldval, 0(ptr)
Akira Hatanaka0e019592011-07-19 20:11:17 +0000984 // <binop> storeval, oldval, incr
985 // sc success, storeval, 0(ptr)
986 // beq success, $0, loopMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000987 BB = loopMBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000988 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000989 if (Nand) {
Akira Hatanaka0e019592011-07-19 20:11:17 +0000990 // and andres, oldval, incr
991 // nor storeval, $0, andres
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000992 BuildMI(BB, DL, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
993 BuildMI(BB, DL, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000994 } else if (BinOpcode) {
Akira Hatanaka0e019592011-07-19 20:11:17 +0000995 // <binop> storeval, oldval, incr
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000996 BuildMI(BB, DL, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000997 } else {
Akira Hatanaka0e019592011-07-19 20:11:17 +0000998 StoreVal = Incr;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000999 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001000 BuildMI(BB, DL, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
1001 BuildMI(BB, DL, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001002
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001003 MI->eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001004
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001005 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001006}
1007
1008MachineBasicBlock *
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001009MipsTargetLowering::emitAtomicBinaryPartword(MachineInstr *MI,
Akira Hatanaka15506782011-06-07 18:58:42 +00001010 MachineBasicBlock *BB,
1011 unsigned Size, unsigned BinOpcode,
1012 bool Nand) const {
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001013 assert((Size == 1 || Size == 2) &&
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001014 "Unsupported size for EmitAtomicBinaryPartial.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001015
1016 MachineFunction *MF = BB->getParent();
1017 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1018 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1019 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001020 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001021
1022 unsigned Dest = MI->getOperand(0).getReg();
1023 unsigned Ptr = MI->getOperand(1).getReg();
1024 unsigned Incr = MI->getOperand(2).getReg();
1025
Akira Hatanaka0e019592011-07-19 20:11:17 +00001026 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1027 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001028 unsigned Mask = RegInfo.createVirtualRegister(RC);
1029 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001030 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1031 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001032 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001033 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1034 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1035 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1036 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1037 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka9663dd32011-07-19 20:56:53 +00001038 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001039 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1040 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1041 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1042 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1043 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001044
1045 // insert new blocks after the current block
1046 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1047 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001048 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001049 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1050 MachineFunction::iterator It = BB;
1051 ++It;
1052 MF->insert(It, loopMBB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001053 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001054 MF->insert(It, exitMBB);
1055
1056 // Transfer the remainder of BB and its successor edges to exitMBB.
1057 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00001058 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001059 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1060
Akira Hatanaka08636b42011-07-19 17:09:53 +00001061 BB->addSuccessor(loopMBB);
1062 loopMBB->addSuccessor(loopMBB);
1063 loopMBB->addSuccessor(sinkMBB);
1064 sinkMBB->addSuccessor(exitMBB);
1065
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001066 // thisMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001067 // addiu masklsb2,$0,-4 # 0xfffffffc
1068 // and alignedaddr,ptr,masklsb2
1069 // andi ptrlsb2,ptr,3
1070 // sll shiftamt,ptrlsb2,3
1071 // ori maskupper,$0,255 # 0xff
1072 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001073 // nor mask2,$0,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001074 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001075
1076 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001077 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001078 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001079 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001080 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001081 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Akira Hatanaka2bf97332013-05-31 03:25:44 +00001082 if (Subtarget->isLittle()) {
1083 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1084 } else {
1085 unsigned Off = RegInfo.createVirtualRegister(RC);
1086 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1087 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1088 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1089 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001090 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001091 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001092 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001093 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001094 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001095 BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(Incr).addReg(ShiftAmt);
Bruno Cardoso Lopesf771a0f2011-05-31 20:25:26 +00001096
Akira Hatanaka27292632011-07-18 18:52:12 +00001097 // atomic.load.binop
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001098 // loopMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001099 // ll oldval,0(alignedaddr)
1100 // binop binopres,oldval,incr2
1101 // and newval,binopres,mask
1102 // and maskedoldval0,oldval,mask2
1103 // or storeval,maskedoldval0,newval
1104 // sc success,storeval,0(alignedaddr)
1105 // beq success,$0,loopMBB
1106
Akira Hatanaka27292632011-07-18 18:52:12 +00001107 // atomic.swap
1108 // loopMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001109 // ll oldval,0(alignedaddr)
Akira Hatanakae4503582011-07-19 18:14:26 +00001110 // and newval,incr2,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001111 // and maskedoldval0,oldval,mask2
1112 // or storeval,maskedoldval0,newval
1113 // sc success,storeval,0(alignedaddr)
1114 // beq success,$0,loopMBB
Akira Hatanaka27292632011-07-18 18:52:12 +00001115
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001116 BB = loopMBB;
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001117 BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001118 if (Nand) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001119 // and andres, oldval, incr2
1120 // nor binopres, $0, andres
1121 // and newval, binopres, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001122 BuildMI(BB, DL, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1123 BuildMI(BB, DL, TII->get(Mips::NOR), BinOpRes)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001124 .addReg(Mips::ZERO).addReg(AndRes);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001125 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001126 } else if (BinOpcode) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001127 // <binop> binopres, oldval, incr2
1128 // and newval, binopres, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001129 BuildMI(BB, DL, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1130 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001131 } else { // atomic.swap
Akira Hatanaka0e019592011-07-19 20:11:17 +00001132 // and newval, incr2, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001133 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanakae4503582011-07-19 18:14:26 +00001134 }
Jia Liuf54f60f2012-02-28 07:46:26 +00001135
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001136 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001137 .addReg(OldVal).addReg(Mask2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001138 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka9663dd32011-07-19 20:56:53 +00001139 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001140 BuildMI(BB, DL, TII->get(Mips::SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001141 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001142 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001143 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001144
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001145 // sinkMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001146 // and maskedoldval1,oldval,mask
1147 // srl srlres,maskedoldval1,shiftamt
1148 // sll sllres,srlres,24
1149 // sra dest,sllres,24
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001150 BB = sinkMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001151 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakae97bd812011-07-19 03:14:58 +00001152
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001153 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001154 .addReg(OldVal).addReg(Mask);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001155 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001156 .addReg(MaskedOldVal1).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001157 BuildMI(BB, DL, TII->get(Mips::SLL), SllRes)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001158 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001159 BuildMI(BB, DL, TII->get(Mips::SRA), Dest)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001160 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001161
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001162 MI->eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001163
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001164 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001165}
1166
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001167MachineBasicBlock * MipsTargetLowering::emitAtomicCmpSwap(MachineInstr *MI,
1168 MachineBasicBlock *BB,
1169 unsigned Size) const {
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001170 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001171
1172 MachineFunction *MF = BB->getParent();
1173 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001174 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001175 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001176 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001177 unsigned LL, SC, ZERO, BNE, BEQ;
1178
1179 if (Size == 4) {
Zoran Jovanovicff9d5f32013-12-19 16:12:56 +00001180 LL = isMicroMips ? Mips::LL_MM : Mips::LL;
1181 SC = isMicroMips ? Mips::SC_MM : Mips::SC;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001182 ZERO = Mips::ZERO;
1183 BNE = Mips::BNE;
1184 BEQ = Mips::BEQ;
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001185 } else {
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001186 LL = Mips::LLD;
1187 SC = Mips::SCD;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001188 ZERO = Mips::ZERO_64;
1189 BNE = Mips::BNE64;
1190 BEQ = Mips::BEQ64;
1191 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001192
1193 unsigned Dest = MI->getOperand(0).getReg();
1194 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka0e019592011-07-19 20:11:17 +00001195 unsigned OldVal = MI->getOperand(2).getReg();
1196 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001197
Akira Hatanaka0e019592011-07-19 20:11:17 +00001198 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001199
1200 // insert new blocks after the current block
1201 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1202 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1203 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1204 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1205 MachineFunction::iterator It = BB;
1206 ++It;
1207 MF->insert(It, loop1MBB);
1208 MF->insert(It, loop2MBB);
1209 MF->insert(It, exitMBB);
1210
1211 // Transfer the remainder of BB and its successor edges to exitMBB.
1212 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00001213 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001214 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1215
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001216 // thisMBB:
1217 // ...
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001218 // fallthrough --> loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001219 BB->addSuccessor(loop1MBB);
Akira Hatanaka08636b42011-07-19 17:09:53 +00001220 loop1MBB->addSuccessor(exitMBB);
1221 loop1MBB->addSuccessor(loop2MBB);
1222 loop2MBB->addSuccessor(loop1MBB);
1223 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001224
1225 // loop1MBB:
1226 // ll dest, 0(ptr)
1227 // bne dest, oldval, exitMBB
1228 BB = loop1MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001229 BuildMI(BB, DL, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1230 BuildMI(BB, DL, TII->get(BNE))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001231 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001232
1233 // loop2MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001234 // sc success, newval, 0(ptr)
1235 // beq success, $0, loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001236 BB = loop2MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001237 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001238 .addReg(NewVal).addReg(Ptr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001239 BuildMI(BB, DL, TII->get(BEQ))
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001240 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001241
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001242 MI->eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001243
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001244 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001245}
1246
1247MachineBasicBlock *
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001248MipsTargetLowering::emitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka15506782011-06-07 18:58:42 +00001249 MachineBasicBlock *BB,
1250 unsigned Size) const {
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001251 assert((Size == 1 || Size == 2) &&
1252 "Unsupported size for EmitAtomicCmpSwapPartial.");
1253
1254 MachineFunction *MF = BB->getParent();
1255 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1256 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1257 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001258 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001259
1260 unsigned Dest = MI->getOperand(0).getReg();
1261 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka0e019592011-07-19 20:11:17 +00001262 unsigned CmpVal = MI->getOperand(2).getReg();
1263 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001264
Akira Hatanaka0e019592011-07-19 20:11:17 +00001265 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1266 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001267 unsigned Mask = RegInfo.createVirtualRegister(RC);
1268 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001269 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1270 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1271 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1272 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1273 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1274 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1275 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1276 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1277 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1278 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1279 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1280 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1281 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1282 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001283
1284 // insert new blocks after the current block
1285 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1286 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1287 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001288 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001289 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1290 MachineFunction::iterator It = BB;
1291 ++It;
1292 MF->insert(It, loop1MBB);
1293 MF->insert(It, loop2MBB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001294 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001295 MF->insert(It, exitMBB);
1296
1297 // Transfer the remainder of BB and its successor edges to exitMBB.
1298 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00001299 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001300 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1301
Akira Hatanaka08636b42011-07-19 17:09:53 +00001302 BB->addSuccessor(loop1MBB);
1303 loop1MBB->addSuccessor(sinkMBB);
1304 loop1MBB->addSuccessor(loop2MBB);
1305 loop2MBB->addSuccessor(loop1MBB);
1306 loop2MBB->addSuccessor(sinkMBB);
1307 sinkMBB->addSuccessor(exitMBB);
1308
Akira Hatanakae4503582011-07-19 18:14:26 +00001309 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001310 // thisMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001311 // addiu masklsb2,$0,-4 # 0xfffffffc
1312 // and alignedaddr,ptr,masklsb2
1313 // andi ptrlsb2,ptr,3
1314 // sll shiftamt,ptrlsb2,3
1315 // ori maskupper,$0,255 # 0xff
1316 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001317 // nor mask2,$0,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001318 // andi maskedcmpval,cmpval,255
1319 // sll shiftedcmpval,maskedcmpval,shiftamt
1320 // andi maskednewval,newval,255
1321 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001322 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001323 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001324 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001325 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001326 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001327 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Akira Hatanaka2bf97332013-05-31 03:25:44 +00001328 if (Subtarget->isLittle()) {
1329 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1330 } else {
1331 unsigned Off = RegInfo.createVirtualRegister(RC);
1332 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1333 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1334 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1335 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001336 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001337 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001338 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001339 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001340 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1341 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001342 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001343 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001344 .addReg(MaskedCmpVal).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001345 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001346 .addReg(NewVal).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001347 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001348 .addReg(MaskedNewVal).addReg(ShiftAmt);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001349
1350 // loop1MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001351 // ll oldval,0(alginedaddr)
1352 // and maskedoldval0,oldval,mask
1353 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001354 BB = loop1MBB;
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001355 BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001356 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001357 .addReg(OldVal).addReg(Mask);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001358 BuildMI(BB, DL, TII->get(Mips::BNE))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001359 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001360
1361 // loop2MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001362 // and maskedoldval1,oldval,mask2
1363 // or storeval,maskedoldval1,shiftednewval
1364 // sc success,storeval,0(alignedaddr)
1365 // beq success,$0,loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001366 BB = loop2MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001367 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001368 .addReg(OldVal).addReg(Mask2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001369 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001370 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001371 BuildMI(BB, DL, TII->get(Mips::SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001372 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001373 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001374 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001375
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001376 // sinkMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001377 // srl srlres,maskedoldval0,shiftamt
1378 // sll sllres,srlres,24
1379 // sra dest,sllres,24
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001380 BB = sinkMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001381 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakae97bd812011-07-19 03:14:58 +00001382
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001383 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001384 .addReg(MaskedOldVal0).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001385 BuildMI(BB, DL, TII->get(Mips::SLL), SllRes)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001386 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001387 BuildMI(BB, DL, TII->get(Mips::SRA), Dest)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001388 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001389
1390 MI->eraseFromParent(); // The instruction is gone now.
1391
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001392 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001393}
1394
Akira Hatanakae2489122011-04-15 21:51:11 +00001395//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001396// Misc Lower Operation implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00001397//===----------------------------------------------------------------------===//
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001398SDValue MipsTargetLowering::lowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001399 SDValue Chain = Op.getOperand(0);
1400 SDValue Table = Op.getOperand(1);
1401 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001402 SDLoc DL(Op);
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001403 EVT PTy = getPointerTy();
1404 unsigned EntrySize =
1405 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(*getDataLayout());
1406
1407 Index = DAG.getNode(ISD::MUL, DL, PTy, Index,
1408 DAG.getConstant(EntrySize, PTy));
1409 SDValue Addr = DAG.getNode(ISD::ADD, DL, PTy, Index, Table);
1410
1411 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
1412 Addr = DAG.getExtLoad(ISD::SEXTLOAD, DL, PTy, Chain, Addr,
1413 MachinePointerInfo::getJumpTable(), MemVT, false, false,
1414 0);
1415 Chain = Addr.getValue(1);
1416
1417 if ((getTargetMachine().getRelocationModel() == Reloc::PIC_) || IsN64) {
1418 // For PIC, the sequence is:
1419 // BRIND(load(Jumptable + index) + RelocBase)
1420 // RelocBase can be JumpTable, GOT or some sort of global base.
1421 Addr = DAG.getNode(ISD::ADD, DL, PTy, Addr,
1422 getPICJumpTableRelocBase(Table, DAG));
1423 }
1424
1425 return DAG.getNode(ISD::BRIND, DL, MVT::Other, Chain, Addr);
1426}
1427
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001428SDValue MipsTargetLowering::lowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Wesley Peck527da1b2010-11-23 03:31:01 +00001429 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001430 // the block to branch to if the condition is true.
1431 SDValue Chain = Op.getOperand(0);
1432 SDValue Dest = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001433 SDLoc DL(Op);
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001434
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001435 SDValue CondRes = createFPCmp(DAG, Op.getOperand(1));
Akira Hatanakaa5352702011-03-31 18:26:17 +00001436
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001437 // Return if flag is not set by a floating point comparison.
Akira Hatanakaa5352702011-03-31 18:26:17 +00001438 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopesa9504222008-07-30 17:06:13 +00001439 return Op;
Wesley Peck527da1b2010-11-23 03:31:01 +00001440
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +00001441 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmaneffb8942008-09-12 16:56:44 +00001442 Mips::CondCode CC =
1443 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Akira Hatanakaf0ea5002013-03-30 01:16:38 +00001444 unsigned Opc = invertFPCondCodeUser(CC) ? Mips::BRANCH_F : Mips::BRANCH_T;
1445 SDValue BrCode = DAG.getConstant(Opc, MVT::i32);
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +00001446 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001447 return DAG.getNode(MipsISD::FPBrcond, DL, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +00001448 FCC0, Dest, CondRes);
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001449}
1450
1451SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001452lowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001453{
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001454 SDValue Cond = createFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001455
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001456 // Return if flag is not set by a floating point comparison.
Akira Hatanakaa5352702011-03-31 18:26:17 +00001457 if (Cond.getOpcode() != MipsISD::FPCmp)
1458 return Op;
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +00001459
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001460 return createCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
Andrew Trickef9de2a2013-05-25 02:42:55 +00001461 SDLoc(Op));
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001462}
1463
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001464SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001465lowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001466{
Andrew Trickef9de2a2013-05-25 02:42:55 +00001467 SDLoc DL(Op);
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001468 EVT Ty = Op.getOperand(0).getValueType();
Matt Arsenault758659232013-05-18 00:21:46 +00001469 SDValue Cond = DAG.getNode(ISD::SETCC, DL,
1470 getSetCCResultType(*DAG.getContext(), Ty),
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001471 Op.getOperand(0), Op.getOperand(1),
1472 Op.getOperand(4));
1473
1474 return DAG.getNode(ISD::SELECT, DL, Op.getValueType(), Cond, Op.getOperand(2),
1475 Op.getOperand(3));
1476}
1477
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001478SDValue MipsTargetLowering::lowerSETCC(SDValue Op, SelectionDAG &DAG) const {
1479 SDValue Cond = createFPCmp(DAG, Op);
Akira Hatanakab7f78592012-03-09 23:46:03 +00001480
1481 assert(Cond.getOpcode() == MipsISD::FPCmp &&
1482 "Floating point operand expected.");
1483
1484 SDValue True = DAG.getConstant(1, MVT::i32);
1485 SDValue False = DAG.getConstant(0, MVT::i32);
1486
Andrew Trickef9de2a2013-05-25 02:42:55 +00001487 return createCMovFP(DAG, Cond, True, False, SDLoc(Op));
Akira Hatanakab7f78592012-03-09 23:46:03 +00001488}
1489
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001490SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001491 SelectionDAG &DAG) const {
Dale Johannesen400dc2e2009-02-06 21:50:26 +00001492 // FIXME there isn't actually debug info here
Andrew Trickef9de2a2013-05-25 02:42:55 +00001493 SDLoc DL(Op);
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001494 EVT Ty = Op.getValueType();
1495 GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
1496 const GlobalValue *GV = N->getGlobal();
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001497
Akira Hatanaka09b23eb2011-10-11 00:55:05 +00001498 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
Akira Hatanaka92a96e12012-09-12 23:27:55 +00001499 const MipsTargetObjectFile &TLOF =
1500 (const MipsTargetObjectFile&)getObjFileLowering();
Wesley Peck527da1b2010-11-23 03:31:01 +00001501
Chris Lattner58e8be82009-08-13 05:41:27 +00001502 // %gp_rel relocation
Wesley Peck527da1b2010-11-23 03:31:01 +00001503 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001504 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, 0,
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +00001505 MipsII::MO_GPREL);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001506 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, DL,
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001507 DAG.getVTList(MVT::i32), &GA, 1);
Akira Hatanakaad495022012-08-22 03:18:13 +00001508 SDValue GPReg = DAG.getRegister(Mips::GP, MVT::i32);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001509 return DAG.getNode(ISD::ADD, DL, MVT::i32, GPReg, GPRelNode);
Chris Lattner58e8be82009-08-13 05:41:27 +00001510 }
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001511
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001512 // %hi/%lo relocation
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001513 return getAddrNonPIC(N, Ty, DAG);
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001514 }
1515
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001516 if (GV->hasInternalLinkage() || (GV->hasLocalLinkage() && !isa<Function>(GV)))
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001517 return getAddrLocal(N, Ty, DAG, HasMips64);
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001518
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00001519 if (LargeGOT)
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001520 return getAddrGlobalLargeGOT(N, Ty, DAG, MipsII::MO_GOT_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00001521 MipsII::MO_GOT_LO16, DAG.getEntryNode(),
1522 MachinePointerInfo::getGOT());
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00001523
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001524 return getAddrGlobal(N, Ty, DAG,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00001525 HasMips64 ? MipsII::MO_GOT_DISP : MipsII::MO_GOT16,
1526 DAG.getEntryNode(), MachinePointerInfo::getGOT());
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001527}
1528
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001529SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op,
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +00001530 SelectionDAG &DAG) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001531 BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op);
1532 EVT Ty = Op.getValueType();
Akira Hatanaka30f97cf2013-09-25 00:30:25 +00001533
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001534 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
1535 return getAddrNonPIC(N, Ty, DAG);
1536
1537 return getAddrLocal(N, Ty, DAG, HasMips64);
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +00001538}
1539
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001540SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001541lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001542{
Akira Hatanakabff84e12011-12-14 18:26:41 +00001543 // If the relocation model is PIC, use the General Dynamic TLS Model or
1544 // Local Dynamic TLS model, otherwise use the Initial Exec or
1545 // Local Exec TLS Model.
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001546
1547 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001548 SDLoc DL(GA);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001549 const GlobalValue *GV = GA->getGlobal();
1550 EVT PtrVT = getPointerTy();
1551
Hans Wennborgaea41202012-05-04 09:40:39 +00001552 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
1553
1554 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Hans Wennborg245917b2012-06-04 14:02:08 +00001555 // General Dynamic and Local Dynamic TLS Model.
1556 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
1557 : MipsII::MO_TLSGD;
1558
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001559 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag);
1560 SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT,
1561 getGlobalReg(DAG, PtrVT), TGA);
Akira Hatanakaf10ee842011-12-08 21:05:38 +00001562 unsigned PtrSize = PtrVT.getSizeInBits();
1563 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1564
Benjamin Kramer64ba50a2011-12-11 12:21:34 +00001565 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001566
1567 ArgListTy Args;
1568 ArgListEntry Entry;
1569 Entry.Node = Argument;
Akira Hatanakadee6c822011-12-08 20:34:32 +00001570 Entry.Ty = PtrTy;
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001571 Args.push_back(Entry);
Jia Liuf54f60f2012-02-28 07:46:26 +00001572
Justin Holewinskiaa583972012-05-25 16:35:28 +00001573 TargetLowering::CallLoweringInfo CLI(DAG.getEntryNode(), PtrTy,
Evan Cheng65f9d192012-02-28 18:51:51 +00001574 false, false, false, false, 0, CallingConv::C,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001575 /*IsTailCall=*/false, /*doesNotRet=*/false,
Evan Cheng65f9d192012-02-28 18:51:51 +00001576 /*isReturnValueUsed=*/true,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001577 TlsGetAddr, Args, DAG, DL);
Justin Holewinskiaa583972012-05-25 16:35:28 +00001578 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001579
Akira Hatanakabff84e12011-12-14 18:26:41 +00001580 SDValue Ret = CallResult.first;
1581
Hans Wennborgaea41202012-05-04 09:40:39 +00001582 if (model != TLSModel::LocalDynamic)
Akira Hatanakabff84e12011-12-14 18:26:41 +00001583 return Ret;
1584
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001585 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanakabff84e12011-12-14 18:26:41 +00001586 MipsII::MO_DTPREL_HI);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001587 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1588 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanakabff84e12011-12-14 18:26:41 +00001589 MipsII::MO_DTPREL_LO);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001590 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1591 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret);
1592 return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001593 }
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001594
1595 SDValue Offset;
Hans Wennborgaea41202012-05-04 09:40:39 +00001596 if (model == TLSModel::InitialExec) {
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001597 // Initial Exec TLS Model
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001598 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001599 MipsII::MO_GOTTPREL);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001600 TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT),
Akira Hatanakab049aef2012-02-24 22:34:47 +00001601 TGA);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001602 Offset = DAG.getLoad(PtrVT, DL,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001603 DAG.getEntryNode(), TGA, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001604 false, false, false, 0);
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001605 } else {
1606 // Local Exec TLS Model
Hans Wennborgaea41202012-05-04 09:40:39 +00001607 assert(model == TLSModel::LocalExec);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001608 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001609 MipsII::MO_TPREL_HI);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001610 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001611 MipsII::MO_TPREL_LO);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001612 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1613 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1614 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001615 }
1616
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001617 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
1618 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001619}
1620
1621SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001622lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +00001623{
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001624 JumpTableSDNode *N = cast<JumpTableSDNode>(Op);
1625 EVT Ty = Op.getValueType();
Akira Hatanaka30f97cf2013-09-25 00:30:25 +00001626
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001627 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
1628 return getAddrNonPIC(N, Ty, DAG);
1629
1630 return getAddrLocal(N, Ty, DAG, HasMips64);
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +00001631}
1632
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001633SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001634lowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +00001635{
Bruno Cardoso Lopesfdb4cec2008-07-23 16:01:50 +00001636 // gp_rel relocation
Wesley Peck527da1b2010-11-23 03:31:01 +00001637 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001638 // but the asm printer currently doesn't support this feature without
Wesley Peck527da1b2010-11-23 03:31:01 +00001639 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopes98bda582008-07-28 19:26:25 +00001640 // stuff below.
Eli Friedman57c11da2009-08-03 02:22:28 +00001641 //if (IsInSmallSection(C->getType())) {
Owen Anderson9f944592009-08-11 20:47:22 +00001642 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1643 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peck527da1b2010-11-23 03:31:01 +00001644 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001645 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
1646 EVT Ty = Op.getValueType();
Bruno Cardoso Lopes2db07582009-11-25 12:17:58 +00001647
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001648 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001649 return getAddrNonPIC(N, Ty, DAG);
Bruno Cardoso Lopesfdb4cec2008-07-23 16:01:50 +00001650
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001651 return getAddrLocal(N, Ty, DAG, HasMips64);
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +00001652}
1653
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001654SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman31ae5862010-04-17 14:41:14 +00001655 MachineFunction &MF = DAG.getMachineFunction();
1656 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1657
Andrew Trickef9de2a2013-05-25 02:42:55 +00001658 SDLoc DL(Op);
Dan Gohman31ae5862010-04-17 14:41:14 +00001659 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1660 getPointerTy());
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +00001661
1662 // vastart just stores the address of the VarArgsFrameIndex slot into the
1663 // memory location argument.
1664 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001665 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00001666 MachinePointerInfo(SV), false, false, 0);
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +00001667}
Jia Liuf54f60f2012-02-28 07:46:26 +00001668
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001669static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG,
1670 bool HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001671 EVT TyX = Op.getOperand(0).getValueType();
1672 EVT TyY = Op.getOperand(1).getValueType();
1673 SDValue Const1 = DAG.getConstant(1, MVT::i32);
1674 SDValue Const31 = DAG.getConstant(31, MVT::i32);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001675 SDLoc DL(Op);
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001676 SDValue Res;
1677
1678 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1679 // to i32.
1680 SDValue X = (TyX == MVT::f32) ?
1681 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1682 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1683 Const1);
1684 SDValue Y = (TyY == MVT::f32) ?
1685 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
1686 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
1687 Const1);
1688
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001689 if (HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001690 // ext E, Y, 31, 1 ; extract bit31 of Y
1691 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
1692 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
1693 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
1694 } else {
1695 // sll SllX, X, 1
1696 // srl SrlX, SllX, 1
1697 // srl SrlY, Y, 31
1698 // sll SllY, SrlX, 31
1699 // or Or, SrlX, SllY
1700 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1701 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1702 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
1703 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
1704 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
1705 }
1706
1707 if (TyX == MVT::f32)
1708 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
1709
1710 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1711 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1712 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001713}
1714
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001715static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG,
1716 bool HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001717 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
1718 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
1719 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
1720 SDValue Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001721 SDLoc DL(Op);
Eric Christopher0713a9d2011-06-08 23:55:35 +00001722
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001723 // Bitcast to integer nodes.
1724 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
1725 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001726
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001727 if (HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001728 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
1729 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
1730 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
1731 DAG.getConstant(WidthY - 1, MVT::i32), Const1);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001732
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001733 if (WidthX > WidthY)
1734 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
1735 else if (WidthY > WidthX)
1736 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001737
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001738 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
1739 DAG.getConstant(WidthX - 1, MVT::i32), Const1, X);
1740 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
1741 }
1742
1743 // (d)sll SllX, X, 1
1744 // (d)srl SrlX, SllX, 1
1745 // (d)srl SrlY, Y, width(Y)-1
1746 // (d)sll SllY, SrlX, width(Y)-1
1747 // or Or, SrlX, SllY
1748 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
1749 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
1750 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
1751 DAG.getConstant(WidthY - 1, MVT::i32));
1752
1753 if (WidthX > WidthY)
1754 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
1755 else if (WidthY > WidthX)
1756 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
1757
1758 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
1759 DAG.getConstant(WidthX - 1, MVT::i32));
1760 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
1761 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001762}
1763
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00001764SDValue
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001765MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001766 if (Subtarget->hasMips64())
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001767 return lowerFCOPYSIGN64(Op, DAG, Subtarget->hasExtractInsert());
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001768
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001769 return lowerFCOPYSIGN32(Op, DAG, Subtarget->hasExtractInsert());
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001770}
1771
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001772static SDValue lowerFABS32(SDValue Op, SelectionDAG &DAG,
1773 bool HasExtractInsert) {
Akira Hatanaka7f4c9d12012-04-11 22:49:04 +00001774 SDValue Res, Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001775 SDLoc DL(Op);
Akira Hatanaka7f4c9d12012-04-11 22:49:04 +00001776
1777 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1778 // to i32.
1779 SDValue X = (Op.getValueType() == MVT::f32) ?
1780 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1781 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1782 Const1);
1783
1784 // Clear MSB.
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001785 if (HasExtractInsert)
Akira Hatanaka7f4c9d12012-04-11 22:49:04 +00001786 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32,
1787 DAG.getRegister(Mips::ZERO, MVT::i32),
1788 DAG.getConstant(31, MVT::i32), Const1, X);
1789 else {
1790 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1791 Res = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1792 }
1793
1794 if (Op.getValueType() == MVT::f32)
1795 return DAG.getNode(ISD::BITCAST, DL, MVT::f32, Res);
1796
1797 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1798 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1799 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
1800}
1801
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001802static SDValue lowerFABS64(SDValue Op, SelectionDAG &DAG,
1803 bool HasExtractInsert) {
Akira Hatanaka7f4c9d12012-04-11 22:49:04 +00001804 SDValue Res, Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001805 SDLoc DL(Op);
Akira Hatanaka7f4c9d12012-04-11 22:49:04 +00001806
1807 // Bitcast to integer node.
1808 SDValue X = DAG.getNode(ISD::BITCAST, DL, MVT::i64, Op.getOperand(0));
1809
1810 // Clear MSB.
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001811 if (HasExtractInsert)
Akira Hatanaka7f4c9d12012-04-11 22:49:04 +00001812 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i64,
1813 DAG.getRegister(Mips::ZERO_64, MVT::i64),
1814 DAG.getConstant(63, MVT::i32), Const1, X);
1815 else {
1816 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i64, X, Const1);
1817 Res = DAG.getNode(ISD::SRL, DL, MVT::i64, SllX, Const1);
1818 }
1819
1820 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, Res);
1821}
1822
1823SDValue
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001824MipsTargetLowering::lowerFABS(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka7f4c9d12012-04-11 22:49:04 +00001825 if (Subtarget->hasMips64() && (Op.getValueType() == MVT::f64))
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001826 return lowerFABS64(Op, DAG, Subtarget->hasExtractInsert());
Akira Hatanaka7f4c9d12012-04-11 22:49:04 +00001827
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001828 return lowerFABS32(Op, DAG, Subtarget->hasExtractInsert());
Akira Hatanaka7f4c9d12012-04-11 22:49:04 +00001829}
1830
Akira Hatanaka66277522011-06-02 00:24:44 +00001831SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001832lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes5444a7b2011-06-16 00:40:02 +00001833 // check the depth
1834 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka15506782011-06-07 18:58:42 +00001835 "Frame address can only be determined for current frame.");
Akira Hatanaka66277522011-06-02 00:24:44 +00001836
1837 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1838 MFI->setFrameAddressIsTaken(true);
1839 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001840 SDLoc DL(Op);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001841 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), DL,
Akira Hatanaka9189d712011-11-11 04:11:56 +00001842 IsN64 ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka66277522011-06-02 00:24:44 +00001843 return FrameAddr;
1844}
1845
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001846SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op,
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00001847 SelectionDAG &DAG) const {
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001848 if (!isa<ConstantSDNode>(Op.getOperand(0))) {
1849 DAG.getContext()->emitError("argument to '__builtin_return_address' must "
1850 "be a constant integer");
1851 return SDValue();
1852 }
1853
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00001854 // check the depth
1855 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
1856 "Return address can be determined only for current frame.");
1857
1858 MachineFunction &MF = DAG.getMachineFunction();
1859 MachineFrameInfo *MFI = MF.getFrameInfo();
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001860 MVT VT = Op.getSimpleValueType();
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00001861 unsigned RA = IsN64 ? Mips::RA_64 : Mips::RA;
1862 MFI->setReturnAddressIsTaken(true);
1863
1864 // Return RA, which contains the return address. Mark it an implicit live-in.
1865 unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00001866 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT);
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00001867}
1868
Akira Hatanakac0b02062013-01-30 00:26:49 +00001869// An EH_RETURN is the result of lowering llvm.eh.return which in turn is
1870// generated from __builtin_eh_return (offset, handler)
1871// The effect of this is to adjust the stack pointer by "offset"
1872// and then branch to "handler".
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001873SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Akira Hatanakac0b02062013-01-30 00:26:49 +00001874 const {
1875 MachineFunction &MF = DAG.getMachineFunction();
1876 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1877
1878 MipsFI->setCallsEhReturn();
1879 SDValue Chain = Op.getOperand(0);
1880 SDValue Offset = Op.getOperand(1);
1881 SDValue Handler = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001882 SDLoc DL(Op);
Akira Hatanakac0b02062013-01-30 00:26:49 +00001883 EVT Ty = IsN64 ? MVT::i64 : MVT::i32;
1884
1885 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
1886 // EH_RETURN nodes, so that instructions are emitted back-to-back.
1887 unsigned OffsetReg = IsN64 ? Mips::V1_64 : Mips::V1;
1888 unsigned AddrReg = IsN64 ? Mips::V0_64 : Mips::V0;
1889 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
1890 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
1891 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
1892 DAG.getRegister(OffsetReg, Ty),
1893 DAG.getRegister(AddrReg, getPointerTy()),
1894 Chain.getValue(1));
1895}
1896
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001897SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
Akira Hatanaka5fd22482012-06-14 21:10:56 +00001898 SelectionDAG &DAG) const {
Eli Friedman26a48482011-07-27 22:21:52 +00001899 // FIXME: Need pseudo-fence for 'singlethread' fences
1900 // FIXME: Set SType for weaker fences where supported/appropriate.
1901 unsigned SType = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001902 SDLoc DL(Op);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001903 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
Eli Friedman26a48482011-07-27 22:21:52 +00001904 DAG.getConstant(SType, MVT::i32));
1905}
1906
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001907SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
Akira Hatanaka5fd22482012-06-14 21:10:56 +00001908 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001909 SDLoc DL(Op);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00001910 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
1911 SDValue Shamt = Op.getOperand(2);
1912
1913 // if shamt < 32:
1914 // lo = (shl lo, shamt)
1915 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
1916 // else:
1917 // lo = 0
1918 // hi = (shl lo, shamt[4:0])
1919 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
1920 DAG.getConstant(-1, MVT::i32));
1921 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo,
1922 DAG.getConstant(1, MVT::i32));
1923 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, ShiftRight1Lo,
1924 Not);
1925 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi, Shamt);
1926 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
1927 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, MVT::i32, Lo, Shamt);
1928 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
1929 DAG.getConstant(0x20, MVT::i32));
Akira Hatanaka5fd22482012-06-14 21:10:56 +00001930 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
1931 DAG.getConstant(0, MVT::i32), ShiftLeftLo);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00001932 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftLeftLo, Or);
1933
1934 SDValue Ops[2] = {Lo, Hi};
1935 return DAG.getMergeValues(Ops, 2, DL);
1936}
1937
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001938SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00001939 bool IsSRA) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001940 SDLoc DL(Op);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00001941 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
1942 SDValue Shamt = Op.getOperand(2);
1943
1944 // if shamt < 32:
1945 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
1946 // if isSRA:
1947 // hi = (sra hi, shamt)
1948 // else:
1949 // hi = (srl hi, shamt)
1950 // else:
1951 // if isSRA:
1952 // lo = (sra hi, shamt[4:0])
1953 // hi = (sra hi, 31)
1954 // else:
1955 // lo = (srl hi, shamt[4:0])
1956 // hi = 0
1957 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
1958 DAG.getConstant(-1, MVT::i32));
1959 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi,
1960 DAG.getConstant(1, MVT::i32));
1961 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, ShiftLeft1Hi, Not);
1962 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo, Shamt);
1963 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
1964 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL, DL, MVT::i32,
1965 Hi, Shamt);
1966 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
1967 DAG.getConstant(0x20, MVT::i32));
1968 SDValue Shift31 = DAG.getNode(ISD::SRA, DL, MVT::i32, Hi,
1969 DAG.getConstant(31, MVT::i32));
1970 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftRightHi, Or);
1971 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
1972 IsSRA ? Shift31 : DAG.getConstant(0, MVT::i32),
1973 ShiftRightHi);
1974
1975 SDValue Ops[2] = {Lo, Hi};
1976 return DAG.getMergeValues(Ops, 2, DL);
1977}
1978
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00001979static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00001980 SDValue Chain, SDValue Src, unsigned Offset) {
Akira Hatanaka95866182012-06-13 19:06:08 +00001981 SDValue Ptr = LD->getBasePtr();
Akira Hatanaka8f1db772012-06-02 00:03:49 +00001982 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
Akira Hatanaka95866182012-06-13 19:06:08 +00001983 EVT BasePtrVT = Ptr.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001984 SDLoc DL(LD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00001985 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
1986
1987 if (Offset)
Akira Hatanaka95866182012-06-13 19:06:08 +00001988 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00001989 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka8f1db772012-06-02 00:03:49 +00001990
1991 SDValue Ops[] = { Chain, Ptr, Src };
1992 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT,
1993 LD->getMemOperand());
1994}
1995
1996// Expand an unaligned 32 or 64-bit integer load node.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001997SDValue MipsTargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka8f1db772012-06-02 00:03:49 +00001998 LoadSDNode *LD = cast<LoadSDNode>(Op);
1999 EVT MemVT = LD->getMemoryVT();
2000
2001 // Return if load is aligned or if MemVT is neither i32 nor i64.
2002 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
2003 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
2004 return SDValue();
2005
2006 bool IsLittle = Subtarget->isLittle();
2007 EVT VT = Op.getValueType();
2008 ISD::LoadExtType ExtType = LD->getExtensionType();
2009 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
2010
2011 assert((VT == MVT::i32) || (VT == MVT::i64));
2012
2013 // Expand
2014 // (set dst, (i64 (load baseptr)))
2015 // to
2016 // (set tmp, (ldl (add baseptr, 7), undef))
2017 // (set dst, (ldr baseptr, tmp))
2018 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002019 SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002020 IsLittle ? 7 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002021 return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002022 IsLittle ? 0 : 7);
2023 }
2024
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002025 SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002026 IsLittle ? 3 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002027 SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002028 IsLittle ? 0 : 3);
2029
2030 // Expand
2031 // (set dst, (i32 (load baseptr))) or
2032 // (set dst, (i64 (sextload baseptr))) or
2033 // (set dst, (i64 (extload baseptr)))
2034 // to
2035 // (set tmp, (lwl (add baseptr, 3), undef))
2036 // (set dst, (lwr baseptr, tmp))
2037 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
2038 (ExtType == ISD::EXTLOAD))
2039 return LWR;
2040
2041 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
2042
2043 // Expand
2044 // (set dst, (i64 (zextload baseptr)))
2045 // to
2046 // (set tmp0, (lwl (add baseptr, 3), undef))
2047 // (set tmp1, (lwr baseptr, tmp0))
2048 // (set tmp2, (shl tmp1, 32))
2049 // (set dst, (srl tmp2, 32))
Andrew Trickef9de2a2013-05-25 02:42:55 +00002050 SDLoc DL(LD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002051 SDValue Const32 = DAG.getConstant(32, MVT::i32);
2052 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
Akira Hatanaka67346852012-06-04 17:46:29 +00002053 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
2054 SDValue Ops[] = { SRL, LWR.getValue(1) };
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002055 return DAG.getMergeValues(Ops, 2, DL);
2056}
2057
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002058static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002059 SDValue Chain, unsigned Offset) {
Akira Hatanaka95866182012-06-13 19:06:08 +00002060 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
2061 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002062 SDLoc DL(SD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002063 SDVTList VTList = DAG.getVTList(MVT::Other);
2064
2065 if (Offset)
Akira Hatanaka95866182012-06-13 19:06:08 +00002066 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002067 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002068
2069 SDValue Ops[] = { Chain, Value, Ptr };
2070 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT,
2071 SD->getMemOperand());
2072}
2073
2074// Expand an unaligned 32 or 64-bit integer store node.
Akira Hatanakad82ee942013-05-16 20:45:17 +00002075static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG,
2076 bool IsLittle) {
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002077 SDValue Value = SD->getValue(), Chain = SD->getChain();
2078 EVT VT = Value.getValueType();
2079
2080 // Expand
2081 // (store val, baseptr) or
2082 // (truncstore val, baseptr)
2083 // to
2084 // (swl val, (add baseptr, 3))
2085 // (swr val, baseptr)
2086 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002087 SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002088 IsLittle ? 3 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002089 return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002090 }
2091
2092 assert(VT == MVT::i64);
2093
2094 // Expand
2095 // (store val, baseptr)
2096 // to
2097 // (sdl val, (add baseptr, 7))
2098 // (sdr val, baseptr)
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002099 SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2100 return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002101}
2102
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002103// Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr).
2104static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG) {
2105 SDValue Val = SD->getValue();
2106
2107 if (Val.getOpcode() != ISD::FP_TO_SINT)
2108 return SDValue();
2109
2110 EVT FPTy = EVT::getFloatingPointVT(Val.getValueSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002111 SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy,
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002112 Val.getOperand(0));
2113
Andrew Trickef9de2a2013-05-25 02:42:55 +00002114 return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(),
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002115 SD->getPointerInfo(), SD->isVolatile(),
2116 SD->isNonTemporal(), SD->getAlignment());
2117}
2118
Akira Hatanakad82ee942013-05-16 20:45:17 +00002119SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2120 StoreSDNode *SD = cast<StoreSDNode>(Op);
2121 EVT MemVT = SD->getMemoryVT();
2122
2123 // Lower unaligned integer stores.
2124 if ((SD->getAlignment() < MemVT.getSizeInBits() / 8) &&
2125 ((MemVT == MVT::i32) || (MemVT == MVT::i64)))
2126 return lowerUnalignedIntStore(SD, DAG, Subtarget->isLittle());
2127
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002128 return lowerFP_TO_SINT_STORE(SD, DAG);
Akira Hatanakad82ee942013-05-16 20:45:17 +00002129}
2130
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002131SDValue MipsTargetLowering::lowerADD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002132 if (Op->getOperand(0).getOpcode() != ISD::FRAMEADDR
2133 || cast<ConstantSDNode>
2134 (Op->getOperand(0).getOperand(0))->getZExtValue() != 0
2135 || Op->getOperand(1).getOpcode() != ISD::FRAME_TO_ARGS_OFFSET)
2136 return SDValue();
2137
2138 // The pattern
2139 // (add (frameaddr 0), (frame_to_args_offset))
2140 // results from lowering llvm.eh.dwarf.cfa intrinsic. Transform it to
2141 // (add FrameObject, 0)
2142 // where FrameObject is a fixed StackObject with offset 0 which points to
2143 // the old stack pointer.
2144 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2145 EVT ValTy = Op->getValueType(0);
2146 int FI = MFI->CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
2147 SDValue InArgsAddr = DAG.getFrameIndex(FI, ValTy);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002148 return DAG.getNode(ISD::ADD, SDLoc(Op), ValTy, InArgsAddr,
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002149 DAG.getConstant(0, ValTy));
2150}
2151
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002152SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op,
2153 SelectionDAG &DAG) const {
2154 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002155 SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy,
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002156 Op.getOperand(0));
Andrew Trickef9de2a2013-05-25 02:42:55 +00002157 return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc);
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002158}
2159
Akira Hatanakae2489122011-04-15 21:51:11 +00002160//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002161// Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002162//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002163
Akira Hatanakae2489122011-04-15 21:51:11 +00002164//===----------------------------------------------------------------------===//
Wesley Peck527da1b2010-11-23 03:31:01 +00002165// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002166// Mips O32 ABI rules:
2167// ---
2168// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peck527da1b2010-11-23 03:31:01 +00002169// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002170// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peck527da1b2010-11-23 03:31:01 +00002171// f64 - Only passed in two aliased f32 registers if no int reg has been used
2172// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002173// not used, it must be shadowed. If only A3 is avaiable, shadow it and
2174// go to stack.
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002175//
2176// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanakae2489122011-04-15 21:51:11 +00002177//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002178
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00002179static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2180 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
2181 CCState &State, const uint16_t *F64Regs) {
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002182
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00002183 static const unsigned IntRegsSize = 4, FloatRegsSize = 2;
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002184
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00002185 static const uint16_t IntRegs[] = { Mips::A0, Mips::A1, Mips::A2, Mips::A3 };
2186 static const uint16_t F32Regs[] = { Mips::F12, Mips::F14 };
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002187
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002188 // Do not process byval args here.
2189 if (ArgFlags.isByVal())
2190 return true;
Akira Hatanaka5e16c6a2011-05-24 19:18:33 +00002191
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002192 // Promote i8 and i16
2193 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
2194 LocVT = MVT::i32;
2195 if (ArgFlags.isSExt())
2196 LocInfo = CCValAssign::SExt;
2197 else if (ArgFlags.isZExt())
2198 LocInfo = CCValAssign::ZExt;
2199 else
2200 LocInfo = CCValAssign::AExt;
2201 }
2202
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002203 unsigned Reg;
2204
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002205 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
2206 // is true: function is vararg, argument is 3rd or higher, there is previous
2207 // argument which is not f32 or f64.
2208 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
2209 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanaka9e6a8cc2011-05-19 20:29:48 +00002210 unsigned OrigAlign = ArgFlags.getOrigAlign();
2211 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002212
2213 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002214 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanaka9e6a8cc2011-05-19 20:29:48 +00002215 // If this is the first part of an i64 arg,
2216 // the allocated register must be either A0 or A2.
2217 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
2218 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002219 LocVT = MVT::i32;
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002220 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
2221 // Allocate int register and shadow next int register. If first
2222 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002223 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2224 if (Reg == Mips::A1 || Reg == Mips::A3)
2225 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2226 State.AllocateReg(IntRegs, IntRegsSize);
2227 LocVT = MVT::i32;
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002228 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
2229 // we are guaranteed to find an available float register
2230 if (ValVT == MVT::f32) {
2231 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
2232 // Shadow int register
2233 State.AllocateReg(IntRegs, IntRegsSize);
2234 } else {
2235 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
2236 // Shadow int registers
2237 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
2238 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
2239 State.AllocateReg(IntRegs, IntRegsSize);
2240 State.AllocateReg(IntRegs, IntRegsSize);
2241 }
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002242 } else
2243 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002244
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002245 if (!Reg) {
2246 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3,
2247 OrigAlign);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002248 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002249 } else
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002250 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002251
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002252 return false;
Akira Hatanaka202f6402011-11-12 02:20:46 +00002253}
2254
Akira Hatanakabfb66242013-08-20 23:38:40 +00002255static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT,
2256 MVT LocVT, CCValAssign::LocInfo LocInfo,
2257 ISD::ArgFlagsTy ArgFlags, CCState &State) {
2258 static const uint16_t F64Regs[] = { Mips::D6, Mips::D7 };
2259
2260 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2261}
2262
2263static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT,
2264 MVT LocVT, CCValAssign::LocInfo LocInfo,
2265 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Akira Hatanakad6c9f6e2013-11-12 22:16:18 +00002266 static const uint16_t F64Regs[] = { Mips::D12_64, Mips::D14_64 };
Akira Hatanakabfb66242013-08-20 23:38:40 +00002267
2268 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2269}
2270
Akira Hatanaka202f6402011-11-12 02:20:46 +00002271#include "MipsGenCallingConv.inc"
2272
Akira Hatanakae2489122011-04-15 21:51:11 +00002273//===----------------------------------------------------------------------===//
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002274// Call Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002275//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002276
Akira Hatanaka61bbcce2011-09-23 00:58:33 +00002277// Return next O32 integer argument register.
2278static unsigned getNextIntArgReg(unsigned Reg) {
2279 assert((Reg == Mips::A0) || (Reg == Mips::A2));
2280 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
2281}
2282
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002283SDValue
2284MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002285 SDValue Chain, SDValue Arg, SDLoc DL,
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002286 bool IsTailCall, SelectionDAG &DAG) const {
2287 if (!IsTailCall) {
2288 SDValue PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr,
2289 DAG.getIntPtrConstant(Offset));
2290 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo(), false,
2291 false, 0);
2292 }
2293
2294 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2295 int FI = MFI->CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
2296 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2297 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(),
2298 /*isVolatile=*/ true, false, 0);
2299}
2300
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002301void MipsTargetLowering::
2302getOpndList(SmallVectorImpl<SDValue> &Ops,
2303 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
2304 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
2305 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
2306 // Insert node "GP copy globalreg" before call to function.
2307 //
2308 // R_MIPS_CALL* operators (emitted when non-internal functions are called
2309 // in PIC mode) allow symbols to be resolved via lazy binding.
2310 // The lazy binding stub requires GP to point to the GOT.
2311 if (IsPICCall && !InternalLinkage) {
2312 unsigned GPReg = IsN64 ? Mips::GP_64 : Mips::GP;
2313 EVT Ty = IsN64 ? MVT::i64 : MVT::i32;
2314 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty)));
2315 }
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002316
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002317 // Build a sequence of copy-to-reg nodes chained together with token
2318 // chain and flag operands which copy the outgoing args into registers.
2319 // The InFlag in necessary since all emitted instructions must be
2320 // stuck together.
2321 SDValue InFlag;
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002322
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002323 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2324 Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, RegsToPass[i].first,
2325 RegsToPass[i].second, InFlag);
2326 InFlag = Chain.getValue(1);
2327 }
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002328
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002329 // Add argument registers to the end of the list so that they are
2330 // known live into the call.
2331 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2332 Ops.push_back(CLI.DAG.getRegister(RegsToPass[i].first,
2333 RegsToPass[i].second.getValueType()));
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002334
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002335 // Add a register mask operand representing the call-preserved registers.
2336 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2337 const uint32_t *Mask = TRI->getCallPreservedMask(CLI.CallConv);
2338 assert(Mask && "Missing call preserved mask for calling convention");
Reed Kotler783c7942013-05-10 22:25:39 +00002339 if (Subtarget->inMips16HardFloat()) {
2340 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
2341 llvm::StringRef Sym = G->getGlobal()->getName();
2342 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
Reed Kotler3230e722013-12-12 02:41:11 +00002343 if (F && F->hasFnAttribute("__Mips16RetHelper")) {
Reed Kotler783c7942013-05-10 22:25:39 +00002344 Mask = MipsRegisterInfo::getMips16RetHelperMask();
2345 }
2346 }
2347 }
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002348 Ops.push_back(CLI.DAG.getRegisterMask(Mask));
2349
2350 if (InFlag.getNode())
2351 Ops.push_back(InFlag);
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002352}
2353
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002354/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman624801e2009-01-26 03:15:54 +00002355/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002356SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00002357MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002358 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00002359 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002360 SDLoc DL = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00002361 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
2362 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
2363 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Akira Hatanakabeda2242012-07-31 18:46:41 +00002364 SDValue Chain = CLI.Chain;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002365 SDValue Callee = CLI.Callee;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002366 bool &IsTailCall = CLI.IsTailCall;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002367 CallingConv::ID CallConv = CLI.CallConv;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002368 bool IsVarArg = CLI.IsVarArg;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002369
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002370 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002371 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanaka7c619f12011-05-20 21:39:54 +00002372 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002373 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +00002374 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002375
2376 // Analyze operands of the call, assigning locations to each operand.
2377 SmallVector<CCValAssign, 16> ArgLocs;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002378 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00002379 getTargetMachine(), ArgLocs, *DAG.getContext());
Reed Kotler783c7942013-05-10 22:25:39 +00002380 MipsCC::SpecialCallingConvType SpecialCallingConv =
2381 getSpecialCallingConv(Callee);
Akira Hatanakabfb66242013-08-20 23:38:40 +00002382 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo,
2383 SpecialCallingConv);
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002384
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002385 MipsCCInfo.analyzeCallOperands(Outs, IsVarArg,
Reed Kotlerc03807a2013-08-30 19:40:56 +00002386 Subtarget->mipsSEUsesSoftFloat(),
Akira Hatanaka3b7391d2013-03-05 22:20:28 +00002387 Callee.getNode(), CLI.Args);
Wesley Peck527da1b2010-11-23 03:31:01 +00002388
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002389 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka195a1e22011-06-08 17:39:33 +00002390 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka97ba7692012-07-26 23:27:01 +00002391
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002392 // Check if it's really possible to do a tail call.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002393 if (IsTailCall)
2394 IsTailCall =
2395 isEligibleForTailCallOptimization(MipsCCInfo, NextStackOffset,
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002396 *MF.getInfo<MipsFunctionInfo>());
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002397
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002398 if (IsTailCall)
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002399 ++NumTailCalls;
2400
Akira Hatanaka79738332011-09-19 20:26:02 +00002401 // Chain is the output chain of the last Load/Store or CopyToReg node.
2402 // ByValChain is the output chain of the last Memcpy node created for copying
2403 // byval arguments to the stack.
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002404 unsigned StackAlignment = TFL->getStackAlignment();
2405 NextStackOffset = RoundUpToAlignment(NextStackOffset, StackAlignment);
Akira Hatanaka79738332011-09-19 20:26:02 +00002406 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002407
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002408 if (!IsTailCall)
Andrew Trickad6d08a2013-05-29 22:03:55 +00002409 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffsetVal, DL);
Akira Hatanakabeda2242012-07-31 18:46:41 +00002410
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002411 SDValue StackPtr = DAG.getCopyFromReg(Chain, DL,
Akira Hatanakabeda2242012-07-31 18:46:41 +00002412 IsN64 ? Mips::SP_64 : Mips::SP,
2413 getPointerTy());
Akira Hatanaka195a1e22011-06-08 17:39:33 +00002414
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002415 // With EABI is it possible to have 16 args on registers.
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002416 std::deque< std::pair<unsigned, SDValue> > RegsToPass;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002417 SmallVector<SDValue, 8> MemOpChains;
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002418 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002419
2420 // Walk the register/memloc assignments, inserting copies/loads.
2421 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002422 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002423 CCValAssign &VA = ArgLocs[i];
Akira Hatanakab20a3252011-10-28 19:49:00 +00002424 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka19891f82011-11-12 02:34:50 +00002425 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2426
2427 // ByVal Arg.
2428 if (Flags.isByVal()) {
2429 assert(Flags.getByValSize() &&
2430 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002431 assert(ByValArg != MipsCCInfo.byval_end());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002432 assert(!IsTailCall &&
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002433 "Do not tail-call optimize if there is a byval argument.");
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002434 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002435 MipsCCInfo, *ByValArg, Flags, Subtarget->isLittle());
2436 ++ByValArg;
Akira Hatanaka19891f82011-11-12 02:34:50 +00002437 continue;
2438 }
Jia Liuf54f60f2012-02-28 07:46:26 +00002439
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002440 // Promote the value if needed.
2441 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002442 default: llvm_unreachable("Unknown loc info!");
Wesley Peck527da1b2010-11-23 03:31:01 +00002443 case CCValAssign::Full:
Akira Hatanakab20a3252011-10-28 19:49:00 +00002444 if (VA.isRegLoc()) {
2445 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
Akira Hatanaka3b7391d2013-03-05 22:20:28 +00002446 (ValVT == MVT::f64 && LocVT == MVT::i64) ||
2447 (ValVT == MVT::i64 && LocVT == MVT::f64))
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002448 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
Akira Hatanakab20a3252011-10-28 19:49:00 +00002449 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002450 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanakae2489122011-04-15 21:51:11 +00002451 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002452 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanakaaef55c82011-04-15 21:00:26 +00002453 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka27916972011-04-15 19:52:08 +00002454 if (!Subtarget->isLittle())
2455 std::swap(Lo, Hi);
Jia Liuf54f60f2012-02-28 07:46:26 +00002456 unsigned LocRegLo = VA.getLocReg();
Akira Hatanaka61bbcce2011-09-23 00:58:33 +00002457 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2458 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2459 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002460 continue;
Wesley Peck527da1b2010-11-23 03:31:01 +00002461 }
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002462 }
2463 break;
Chris Lattner52f16de2008-03-17 06:57:02 +00002464 case CCValAssign::SExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002465 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002466 break;
2467 case CCValAssign::ZExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002468 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002469 break;
2470 case CCValAssign::AExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002471 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002472 break;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002473 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002474
2475 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +00002476 // RegsToPass vector
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002477 if (VA.isRegLoc()) {
2478 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattner52f16de2008-03-17 06:57:02 +00002479 continue;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002480 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002481
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002482 // Register can't get to this point...
Chris Lattner52f16de2008-03-17 06:57:02 +00002483 assert(VA.isMemLoc());
Wesley Peck527da1b2010-11-23 03:31:01 +00002484
Wesley Peck527da1b2010-11-23 03:31:01 +00002485 // emit ISD::STORE whichs stores the
Chris Lattner52f16de2008-03-17 06:57:02 +00002486 // parameter value to a stack Location
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002487 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002488 Chain, Arg, DL, IsTailCall, DAG));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002489 }
2490
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002491 // Transform all store nodes into one single node because all store
2492 // nodes are independent of each other.
Wesley Peck527da1b2010-11-23 03:31:01 +00002493 if (!MemOpChains.empty())
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002494 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002495 &MemOpChains[0], MemOpChains.size());
2496
Bill Wendling24c79f22008-09-16 21:48:12 +00002497 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peck527da1b2010-11-23 03:31:01 +00002498 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2499 // node so that legalize doesn't hack it.
Akira Hatanakab20a3252011-10-28 19:49:00 +00002500 bool IsPICCall = (IsN64 || IsPIC); // true if calls are translated to jalr $25
Akira Hatanakacf9a61b2012-12-13 03:17:29 +00002501 bool GlobalOrExternal = false, InternalLinkage = false;
Akira Hatanakad6f1c582011-04-07 19:51:44 +00002502 SDValue CalleeLo;
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002503 EVT Ty = Callee.getValueType();
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002504
2505 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002506 if (IsPICCall) {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002507 const GlobalValue *Val = G->getGlobal();
2508 InternalLinkage = Val->hasInternalLinkage();
Akira Hatanakacf9a61b2012-12-13 03:17:29 +00002509
2510 if (InternalLinkage)
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002511 Callee = getAddrLocal(G, Ty, DAG, HasMips64);
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00002512 else if (LargeGOT)
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002513 Callee = getAddrGlobalLargeGOT(G, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002514 MipsII::MO_CALL_LO16, Chain,
2515 FuncInfo->callPtrInfo(Val));
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002516 else
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002517 Callee = getAddrGlobal(G, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
2518 FuncInfo->callPtrInfo(Val));
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002519 } else
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002520 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, getPointerTy(), 0,
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002521 MipsII::MO_NO_FLAG);
Akira Hatanaka8e16aac2011-12-09 01:45:12 +00002522 GlobalOrExternal = true;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002523 }
2524 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002525 const char *Sym = S->getSymbol();
2526
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00002527 if (!IsN64 && !IsPIC) // !N64 && static
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002528 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(),
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002529 MipsII::MO_NO_FLAG);
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00002530 else if (LargeGOT)
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002531 Callee = getAddrGlobalLargeGOT(S, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002532 MipsII::MO_CALL_LO16, Chain,
2533 FuncInfo->callPtrInfo(Sym));
Akira Hatanaka02b0e482013-02-22 21:10:03 +00002534 else // N64 || PIC
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002535 Callee = getAddrGlobal(S, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
2536 FuncInfo->callPtrInfo(Sym));
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002537
Akira Hatanaka8e16aac2011-12-09 01:45:12 +00002538 GlobalOrExternal = true;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002539 }
2540
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002541 SmallVector<SDValue, 8> Ops(1, Chain);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002542 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002543
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002544 getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal, InternalLinkage,
2545 CLI, Callee, Chain);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002546
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002547 if (IsTailCall)
2548 return DAG.getNode(MipsISD::TailCall, DL, MVT::Other, &Ops[0], Ops.size());
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002549
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002550 Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, &Ops[0], Ops.size());
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002551 SDValue InFlag = Chain.getValue(1);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002552
Bruno Cardoso Lopes193e64c2010-01-30 18:32:07 +00002553 // Create the CALLSEQ_END node.
Akira Hatanaka97ba7692012-07-26 23:27:01 +00002554 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal,
Andrew Trickad6d08a2013-05-29 22:03:55 +00002555 DAG.getIntPtrConstant(0, true), InFlag, DL);
Bruno Cardoso Lopes193e64c2010-01-30 18:32:07 +00002556 InFlag = Chain.getValue(1);
2557
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002558 // Handle result values, copying them out of physregs into vregs that we
2559 // return.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002560 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg,
2561 Ins, DL, DAG, InVals, CLI.Callee.getNode(), CLI.RetTy);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002562}
2563
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002564/// LowerCallResult - Lower the result values of a call into the
2565/// appropriate copies out of appropriate physical registers.
2566SDValue
2567MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002568 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002569 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002570 SDLoc DL, SelectionDAG &DAG,
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002571 SmallVectorImpl<SDValue> &InVals,
2572 const SDNode *CallNode,
2573 const Type *RetTy) const {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002574 // Assign locations to each value returned by this call.
2575 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002576 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka5fd22482012-06-14 21:10:56 +00002577 getTargetMachine(), RVLocs, *DAG.getContext());
Akira Hatanakabfb66242013-08-20 23:38:40 +00002578 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo);
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002579
Reed Kotlerc03807a2013-08-30 19:40:56 +00002580 MipsCCInfo.analyzeCallResult(Ins, Subtarget->mipsSEUsesSoftFloat(),
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002581 CallNode, RetTy);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002582
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002583 // Copy all of the result registers out of their specified physreg.
2584 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002585 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(),
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002586 RVLocs[i].getLocVT(), InFlag);
2587 Chain = Val.getValue(1);
2588 InFlag = Val.getValue(2);
2589
2590 if (RVLocs[i].getValVT() != RVLocs[i].getLocVT())
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002591 Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getValVT(), Val);
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002592
2593 InVals.push_back(Val);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002594 }
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +00002595
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002596 return Chain;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002597}
2598
Akira Hatanakae2489122011-04-15 21:51:11 +00002599//===----------------------------------------------------------------------===//
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002600// Formal Arguments Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002601//===----------------------------------------------------------------------===//
Wesley Peck527da1b2010-11-23 03:31:01 +00002602/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002603/// and generate load operations for arguments places on the stack.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002604SDValue
2605MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanakaaef55c82011-04-15 21:00:26 +00002606 CallingConv::ID CallConv,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002607 bool IsVarArg,
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00002608 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002609 SDLoc DL, SelectionDAG &DAG,
Akira Hatanakaaef55c82011-04-15 21:00:26 +00002610 SmallVectorImpl<SDValue> &InVals)
Akira Hatanakae2489122011-04-15 21:51:11 +00002611 const {
Bruno Cardoso Lopesa01ede22008-08-04 07:12:52 +00002612 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002613 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopes14033fb2007-08-28 05:08:16 +00002614 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002615
Dan Gohman31ae5862010-04-17 14:41:14 +00002616 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002617
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002618 // Used with vargs to acumulate store chains.
2619 std::vector<SDValue> OutChains;
2620
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002621 // Assign locations to all of the incoming arguments.
2622 SmallVector<CCValAssign, 16> ArgLocs;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002623 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00002624 getTargetMachine(), ArgLocs, *DAG.getContext());
Akira Hatanakabfb66242013-08-20 23:38:40 +00002625 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo);
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002626 Function::const_arg_iterator FuncArg =
2627 DAG.getMachineFunction().getFunction()->arg_begin();
Reed Kotlerc03807a2013-08-30 19:40:56 +00002628 bool UseSoftFloat = Subtarget->mipsSEUsesSoftFloat();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002629
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002630 MipsCCInfo.analyzeFormalArguments(Ins, UseSoftFloat, FuncArg);
Akira Hatanaka4866fe12012-10-30 19:37:25 +00002631 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(),
2632 MipsCCInfo.hasByValArg());
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002633
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00002634 unsigned CurArgIdx = 0;
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002635 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002636
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00002637 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002638 CCValAssign &VA = ArgLocs[i];
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00002639 std::advance(FuncArg, Ins[i].OrigArgIndex - CurArgIdx);
2640 CurArgIdx = Ins[i].OrigArgIndex;
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002641 EVT ValVT = VA.getValVT();
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002642 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2643 bool IsRegLoc = VA.isRegLoc();
2644
2645 if (Flags.isByVal()) {
2646 assert(Flags.getByValSize() &&
2647 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002648 assert(ByValArg != MipsCCInfo.byval_end());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002649 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg,
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002650 MipsCCInfo, *ByValArg);
2651 ++ByValArg;
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002652 continue;
2653 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002654
2655 // Arguments stored on registers
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002656 if (IsRegLoc) {
Akira Hatanaka7d822522013-10-28 21:21:36 +00002657 MVT RegVT = VA.getLocVT();
Akira Hatanakacb4a1a82011-05-24 00:23:52 +00002658 unsigned ArgReg = VA.getLocReg();
Akira Hatanaka7d822522013-10-28 21:21:36 +00002659 const TargetRegisterClass *RC = getRegClassFor(RegVT);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002660
Wesley Peck527da1b2010-11-23 03:31:01 +00002661 // Transform the arguments stored on
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002662 // physical registers into virtual ones
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002663 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC);
2664 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
Wesley Peck527da1b2010-11-23 03:31:01 +00002665
2666 // If this is an 8 or 16-bit value, it has been passed promoted
2667 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002668 // truncate to the right size.
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002669 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattner3c049702009-03-26 05:28:14 +00002670 unsigned Opcode = 0;
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002671 if (VA.getLocInfo() == CCValAssign::SExt)
2672 Opcode = ISD::AssertSext;
2673 else if (VA.getLocInfo() == CCValAssign::ZExt)
2674 Opcode = ISD::AssertZext;
Chris Lattner3c049702009-03-26 05:28:14 +00002675 if (Opcode)
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002676 ArgValue = DAG.getNode(Opcode, DL, RegVT, ArgValue,
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002677 DAG.getValueType(ValVT));
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002678 ArgValue = DAG.getNode(ISD::TRUNCATE, DL, ValVT, ArgValue);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002679 }
2680
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002681 // Handle floating point arguments passed in integer registers and
2682 // long double arguments passed in floating point registers.
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002683 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002684 (RegVT == MVT::i64 && ValVT == MVT::f64) ||
2685 (RegVT == MVT::f64 && ValVT == MVT::i64))
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002686 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002687 else if (IsO32 && RegVT == MVT::i32 && ValVT == MVT::f64) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002688 unsigned Reg2 = addLiveIn(DAG.getMachineFunction(),
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002689 getNextIntArgReg(ArgReg), RC);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002690 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT);
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002691 if (!Subtarget->isLittle())
2692 std::swap(ArgValue, ArgValue2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002693 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64,
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002694 ArgValue, ArgValue2);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002695 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002696
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002697 InVals.push_back(ArgValue);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002698 } else { // VA.isRegLoc()
2699
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002700 // sanity check
2701 assert(VA.isMemLoc());
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002702
Wesley Peck527da1b2010-11-23 03:31:01 +00002703 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002704 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakacb4a1a82011-05-24 00:23:52 +00002705 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002706
2707 // Create load nodes to retrieve arguments from the stack
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002708 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Akira Hatanakad1c58ed2013-11-09 02:38:51 +00002709 SDValue Load = DAG.getLoad(ValVT, DL, Chain, FIN,
2710 MachinePointerInfo::getFixedStack(FI),
2711 false, false, false, 0);
2712 InVals.push_back(Load);
2713 OutChains.push_back(Load.getValue(1));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002714 }
2715 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002716
2717 // The mips ABIs for returning structs by value requires that we copy
2718 // the sret argument into $v0 for the return. Save the argument into
2719 // a virtual register so that we can access it from the return points.
2720 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2721 unsigned Reg = MipsFI->getSRetReturnReg();
2722 if (!Reg) {
Akira Hatanaka0c7d1312012-10-19 22:11:40 +00002723 Reg = MF.getRegInfo().
2724 createVirtualRegister(getRegClassFor(IsN64 ? MVT::i64 : MVT::i32));
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002725 MipsFI->setSRetReturnReg(Reg);
2726 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002727 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[0]);
2728 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002729 }
2730
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002731 if (IsVarArg)
2732 writeVarArgRegs(OutChains, MipsCCInfo, Chain, DL, DAG);
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002733
Wesley Peck527da1b2010-11-23 03:31:01 +00002734 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002735 // the size of Ins and InVals. This only happens when on varg functions
2736 if (!OutChains.empty()) {
2737 OutChains.push_back(Chain);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002738 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002739 &OutChains[0], OutChains.size());
2740 }
2741
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002742 return Chain;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002743}
2744
Akira Hatanakae2489122011-04-15 21:51:11 +00002745//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002746// Return Value Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002747//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002748
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00002749bool
2750MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002751 MachineFunction &MF, bool IsVarArg,
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00002752 const SmallVectorImpl<ISD::OutputArg> &Outs,
2753 LLVMContext &Context) const {
2754 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002755 CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(),
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00002756 RVLocs, Context);
2757 return CCInfo.CheckReturn(Outs, RetCC_Mips);
2758}
2759
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002760SDValue
2761MipsTargetLowering::LowerReturn(SDValue Chain,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002762 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002763 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002764 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002765 SDLoc DL, SelectionDAG &DAG) const {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002766 // CCValAssign - represent the assignment of
2767 // the return value to a location
2768 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002769 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002770
2771 // CCState - Info about the registers and stack slot.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002772 CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(), RVLocs,
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002773 *DAG.getContext());
Akira Hatanakabfb66242013-08-20 23:38:40 +00002774 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002775
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002776 // Analyze return values.
Reed Kotlerc03807a2013-08-30 19:40:56 +00002777 MipsCCInfo.analyzeReturn(Outs, Subtarget->mipsSEUsesSoftFloat(),
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002778 MF.getFunction()->getReturnType());
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002779
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002780 SDValue Flag;
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00002781 SmallVector<SDValue, 4> RetOps(1, Chain);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002782
2783 // Copy the result values into the output registers.
2784 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002785 SDValue Val = OutVals[i];
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002786 CCValAssign &VA = RVLocs[i];
2787 assert(VA.isRegLoc() && "Can only return in registers!");
2788
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002789 if (RVLocs[i].getValVT() != RVLocs[i].getLocVT())
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002790 Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getLocVT(), Val);
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002791
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002792 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002793
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00002794 // Guarantee that all emitted copies are stuck together with flags.
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002795 Flag = Chain.getValue(1);
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00002796 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002797 }
2798
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002799 // The mips ABIs for returning structs by value requires that we copy
2800 // the sret argument into $v0 for the return. We saved the argument into
2801 // a virtual register in the entry block, so now we copy the value out
2802 // and into $v0.
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002803 if (MF.getFunction()->hasStructRetAttr()) {
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002804 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2805 unsigned Reg = MipsFI->getSRetReturnReg();
2806
Wesley Peck527da1b2010-11-23 03:31:01 +00002807 if (!Reg)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002808 llvm_unreachable("sret virtual register not created in the entry block");
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002809 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Akira Hatanaka868b3a32012-10-24 02:10:54 +00002810 unsigned V0 = IsN64 ? Mips::V0_64 : Mips::V0;
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002811
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002812 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002813 Flag = Chain.getValue(1);
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00002814 RetOps.push_back(DAG.getRegister(V0, getPointerTy()));
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002815 }
2816
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00002817 RetOps[0] = Chain; // Update chain.
Akira Hatanakaefff7b72012-07-10 00:19:06 +00002818
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00002819 // Add the flag if we have it.
2820 if (Flag.getNode())
2821 RetOps.push_back(Flag);
2822
2823 // Return on Mips is always a "jr $ra"
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002824 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, &RetOps[0], RetOps.size());
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002825}
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002826
Akira Hatanakae2489122011-04-15 21:51:11 +00002827//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002828// Mips Inline Assembly Support
Akira Hatanakae2489122011-04-15 21:51:11 +00002829//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002830
2831/// getConstraintType - Given a constraint letter, return the type of
2832/// constraint it is for this target.
2833MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peck527da1b2010-11-23 03:31:01 +00002834getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002835{
Daniel Sanders8b59af12013-11-12 12:56:01 +00002836 // Mips specific constraints
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002837 // GCC config/mips/constraints.md
2838 //
Wesley Peck527da1b2010-11-23 03:31:01 +00002839 // 'd' : An address register. Equivalent to r
2840 // unless generating MIPS16 code.
2841 // 'y' : Equivalent to r; retained for
2842 // backwards compatibility.
Eric Christophere3c494d2012-05-07 06:25:10 +00002843 // 'c' : A register suitable for use in an indirect
2844 // jump. This will always be $25 for -mabicalls.
Eric Christopher0d8c15d2012-05-07 06:25:19 +00002845 // 'l' : The lo register. 1 word storage.
2846 // 'x' : The hilo register pair. Double word storage.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002847 if (Constraint.size() == 1) {
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002848 switch (Constraint[0]) {
2849 default : break;
Wesley Peck527da1b2010-11-23 03:31:01 +00002850 case 'd':
2851 case 'y':
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002852 case 'f':
Eric Christophere3c494d2012-05-07 06:25:10 +00002853 case 'c':
Eric Christopher9c492e62012-05-07 06:25:15 +00002854 case 'l':
Eric Christopher0d8c15d2012-05-07 06:25:19 +00002855 case 'x':
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002856 return C_RegisterClass;
Jack Carter0e149b02013-03-04 21:33:15 +00002857 case 'R':
2858 return C_Memory;
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00002859 }
2860 }
2861 return TargetLowering::getConstraintType(Constraint);
2862}
2863
John Thompsone8360b72010-10-29 17:29:13 +00002864/// Examine constraint type and operand type and determine a weight value.
2865/// This object must already have been set up with the operand type
2866/// and the current alternative constraint selected.
2867TargetLowering::ConstraintWeight
2868MipsTargetLowering::getSingleConstraintMatchWeight(
2869 AsmOperandInfo &info, const char *constraint) const {
2870 ConstraintWeight weight = CW_Invalid;
2871 Value *CallOperandVal = info.CallOperandVal;
2872 // If we don't have a value, we can't do a match,
2873 // but allow it at the lowest weight.
2874 if (CallOperandVal == NULL)
2875 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +00002876 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +00002877 // Look at the constraint type.
2878 switch (*constraint) {
2879 default:
2880 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2881 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00002882 case 'd':
2883 case 'y':
John Thompsone8360b72010-10-29 17:29:13 +00002884 if (type->isIntegerTy())
2885 weight = CW_Register;
2886 break;
Daniel Sanders8b59af12013-11-12 12:56:01 +00002887 case 'f': // FPU or MSA register
2888 if (Subtarget->hasMSA() && type->isVectorTy() &&
2889 cast<VectorType>(type)->getBitWidth() == 128)
2890 weight = CW_Register;
2891 else if (type->isFloatTy())
John Thompsone8360b72010-10-29 17:29:13 +00002892 weight = CW_Register;
2893 break;
Eric Christophere3c494d2012-05-07 06:25:10 +00002894 case 'c': // $25 for indirect jumps
Eric Christopher9c492e62012-05-07 06:25:15 +00002895 case 'l': // lo register
Eric Christopher0d8c15d2012-05-07 06:25:19 +00002896 case 'x': // hilo register pair
Daniel Sanders8b59af12013-11-12 12:56:01 +00002897 if (type->isIntegerTy())
Eric Christophere3c494d2012-05-07 06:25:10 +00002898 weight = CW_SpecificReg;
Daniel Sanders8b59af12013-11-12 12:56:01 +00002899 break;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00002900 case 'I': // signed 16 bit immediate
Eric Christopher7201e1b2012-05-07 03:13:42 +00002901 case 'J': // integer zero
Eric Christopher3ff88a02012-05-07 05:46:29 +00002902 case 'K': // unsigned 16 bit immediate
Eric Christopher1109b342012-05-07 05:46:37 +00002903 case 'L': // signed 32 bit immediate where lower 16 bits are 0
Eric Christophere07aa432012-05-07 05:46:43 +00002904 case 'N': // immediate in the range of -65535 to -1 (inclusive)
Eric Christopher470578a2012-05-07 05:46:48 +00002905 case 'O': // signed 15 bit immediate (+- 16383)
Eric Christopherc18ae4a2012-05-07 06:25:02 +00002906 case 'P': // immediate in the range of 65535 to 1 (inclusive)
Eric Christopher1d6c89e2012-05-07 03:13:32 +00002907 if (isa<ConstantInt>(CallOperandVal))
2908 weight = CW_Constant;
2909 break;
Jack Carter0e149b02013-03-04 21:33:15 +00002910 case 'R':
2911 weight = CW_Memory;
2912 break;
John Thompsone8360b72010-10-29 17:29:13 +00002913 }
2914 return weight;
2915}
2916
Akira Hatanaka7473b472013-08-14 00:21:25 +00002917/// This is a helper function to parse a physical register string and split it
2918/// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag
2919/// that is returned indicates whether parsing was successful. The second flag
2920/// is true if the numeric part exists.
2921static std::pair<bool, bool>
2922parsePhysicalReg(const StringRef &C, std::string &Prefix,
2923 unsigned long long &Reg) {
2924 if (C.front() != '{' || C.back() != '}')
2925 return std::make_pair(false, false);
2926
2927 // Search for the first numeric character.
2928 StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1;
2929 I = std::find_if(B, E, std::ptr_fun(isdigit));
2930
2931 Prefix.assign(B, I - B);
2932
2933 // The second flag is set to false if no numeric characters were found.
2934 if (I == E)
2935 return std::make_pair(true, false);
2936
2937 // Parse the numeric characters.
2938 return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg),
2939 true);
2940}
2941
2942std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering::
2943parseRegForInlineAsmConstraint(const StringRef &C, MVT VT) const {
2944 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2945 const TargetRegisterClass *RC;
2946 std::string Prefix;
2947 unsigned long long Reg;
2948
2949 std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg);
2950
2951 if (!R.first)
2952 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2953
2954 if ((Prefix == "hi" || Prefix == "lo")) { // Parse hi/lo.
2955 // No numeric characters follow "hi" or "lo".
2956 if (R.second)
2957 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2958
2959 RC = TRI->getRegClass(Prefix == "hi" ?
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00002960 Mips::HI32RegClassID : Mips::LO32RegClassID);
Akira Hatanaka7473b472013-08-14 00:21:25 +00002961 return std::make_pair(*(RC->begin()), RC);
Daniel Sanders8b59af12013-11-12 12:56:01 +00002962 } else if (Prefix.compare(0, 4, "$msa") == 0) {
2963 // Parse $msa(ir|csr|access|save|modify|request|map|unmap)
2964
2965 // No numeric characters follow the name.
2966 if (R.second)
2967 return std::make_pair((unsigned)0, (const TargetRegisterClass *)0);
2968
2969 Reg = StringSwitch<unsigned long long>(Prefix)
2970 .Case("$msair", Mips::MSAIR)
2971 .Case("$msacsr", Mips::MSACSR)
2972 .Case("$msaaccess", Mips::MSAAccess)
2973 .Case("$msasave", Mips::MSASave)
2974 .Case("$msamodify", Mips::MSAModify)
2975 .Case("$msarequest", Mips::MSARequest)
2976 .Case("$msamap", Mips::MSAMap)
2977 .Case("$msaunmap", Mips::MSAUnmap)
2978 .Default(0);
2979
2980 if (!Reg)
2981 return std::make_pair((unsigned)0, (const TargetRegisterClass *)0);
2982
2983 RC = TRI->getRegClass(Mips::MSACtrlRegClassID);
2984 return std::make_pair(Reg, RC);
Akira Hatanaka7473b472013-08-14 00:21:25 +00002985 }
2986
2987 if (!R.second)
2988 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2989
2990 if (Prefix == "$f") { // Parse $f0-$f31.
2991 // If the size of FP registers is 64-bit or Reg is an even number, select
2992 // the 64-bit register class. Otherwise, select the 32-bit register class.
2993 if (VT == MVT::Other)
2994 VT = (Subtarget->isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32;
2995
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00002996 RC = getRegClassFor(VT);
Akira Hatanaka7473b472013-08-14 00:21:25 +00002997
2998 if (RC == &Mips::AFGR64RegClass) {
2999 assert(Reg % 2 == 0);
3000 Reg >>= 1;
3001 }
Daniel Sanders8b59af12013-11-12 12:56:01 +00003002 } else if (Prefix == "$fcc") // Parse $fcc0-$fcc7.
Akira Hatanaka7473b472013-08-14 00:21:25 +00003003 RC = TRI->getRegClass(Mips::FCCRegClassID);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003004 else if (Prefix == "$w") { // Parse $w0-$w31.
3005 RC = getRegClassFor((VT == MVT::Other) ? MVT::v16i8 : VT);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003006 } else { // Parse $0-$31.
3007 assert(Prefix == "$");
3008 RC = getRegClassFor((VT == MVT::Other) ? MVT::i32 : VT);
3009 }
3010
3011 assert(Reg < RC->getNumRegs());
3012 return std::make_pair(*(RC->begin() + Reg), RC);
3013}
3014
Eric Christophereaf77dc2011-06-29 19:33:04 +00003015/// Given a register class constraint, like 'r', if this corresponds directly
3016/// to an LLVM register class, return a register of 0 and the register class
3017/// pointer.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003018std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Chad Rosier295bd432013-06-22 18:37:38 +00003019getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003020{
3021 if (Constraint.size() == 1) {
3022 switch (Constraint[0]) {
Eric Christopher9519c082011-06-29 19:04:31 +00003023 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
3024 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003025 case 'r':
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003026 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
3027 if (Subtarget->inMips16Mode())
3028 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003029 return std::make_pair(0U, &Mips::GPR32RegClass);
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003030 }
Jack Carterb3530942012-07-02 23:35:23 +00003031 if (VT == MVT::i64 && !HasMips64)
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003032 return std::make_pair(0U, &Mips::GPR32RegClass);
Eric Christopher58daf042012-05-07 03:13:22 +00003033 if (VT == MVT::i64 && HasMips64)
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003034 return std::make_pair(0U, &Mips::GPR64RegClass);
Eric Christopher58daf042012-05-07 03:13:22 +00003035 // This will generate an error message
3036 return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
Daniel Sanders8b59af12013-11-12 12:56:01 +00003037 case 'f': // FPU or MSA register
3038 if (VT == MVT::v16i8)
3039 return std::make_pair(0U, &Mips::MSA128BRegClass);
3040 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
3041 return std::make_pair(0U, &Mips::MSA128HRegClass);
3042 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
3043 return std::make_pair(0U, &Mips::MSA128WRegClass);
3044 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
3045 return std::make_pair(0U, &Mips::MSA128DRegClass);
3046 else if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00003047 return std::make_pair(0U, &Mips::FGR32RegClass);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003048 else if ((VT == MVT::f64) && (!Subtarget->isSingleFloat())) {
Akira Hatanakac669d7a2012-01-04 02:45:01 +00003049 if (Subtarget->isFP64bit())
Craig Topperc7242e02012-04-20 07:30:17 +00003050 return std::make_pair(0U, &Mips::FGR64RegClass);
3051 return std::make_pair(0U, &Mips::AFGR64RegClass);
Akira Hatanakac669d7a2012-01-04 02:45:01 +00003052 }
Eric Christophere3c494d2012-05-07 06:25:10 +00003053 break;
3054 case 'c': // register suitable for indirect jump
3055 if (VT == MVT::i32)
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003056 return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass);
Eric Christophere3c494d2012-05-07 06:25:10 +00003057 assert(VT == MVT::i64 && "Unexpected type.");
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003058 return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass);
Eric Christopher9c492e62012-05-07 06:25:15 +00003059 case 'l': // register suitable for indirect jump
3060 if (VT == MVT::i32)
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00003061 return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass);
3062 return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass);
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003063 case 'x': // register suitable for indirect jump
3064 // Fixme: Not triggering the use of both hi and low
3065 // This will generate an error message
3066 return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003067 }
3068 }
Akira Hatanaka7473b472013-08-14 00:21:25 +00003069
3070 std::pair<unsigned, const TargetRegisterClass *> R;
3071 R = parseRegForInlineAsmConstraint(Constraint, VT);
3072
3073 if (R.second)
3074 return R;
3075
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003076 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3077}
3078
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003079/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3080/// vector. If it is invalid, don't add anything to Ops.
3081void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3082 std::string &Constraint,
3083 std::vector<SDValue>&Ops,
3084 SelectionDAG &DAG) const {
3085 SDValue Result(0, 0);
3086
3087 // Only support length 1 constraints for now.
3088 if (Constraint.length() > 1) return;
3089
3090 char ConstraintLetter = Constraint[0];
3091 switch (ConstraintLetter) {
3092 default: break; // This will fall through to the generic implementation
3093 case 'I': // Signed 16 bit constant
3094 // If this fails, the parent routine will give an error
3095 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3096 EVT Type = Op.getValueType();
3097 int64_t Val = C->getSExtValue();
3098 if (isInt<16>(Val)) {
3099 Result = DAG.getTargetConstant(Val, Type);
3100 break;
3101 }
3102 }
3103 return;
Eric Christopher7201e1b2012-05-07 03:13:42 +00003104 case 'J': // integer zero
3105 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3106 EVT Type = Op.getValueType();
3107 int64_t Val = C->getZExtValue();
3108 if (Val == 0) {
3109 Result = DAG.getTargetConstant(0, Type);
3110 break;
3111 }
3112 }
3113 return;
Eric Christopher3ff88a02012-05-07 05:46:29 +00003114 case 'K': // unsigned 16 bit immediate
3115 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3116 EVT Type = Op.getValueType();
3117 uint64_t Val = (uint64_t)C->getZExtValue();
3118 if (isUInt<16>(Val)) {
3119 Result = DAG.getTargetConstant(Val, Type);
3120 break;
3121 }
3122 }
3123 return;
Eric Christopher1109b342012-05-07 05:46:37 +00003124 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3125 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3126 EVT Type = Op.getValueType();
3127 int64_t Val = C->getSExtValue();
3128 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
3129 Result = DAG.getTargetConstant(Val, Type);
3130 break;
3131 }
3132 }
3133 return;
Eric Christophere07aa432012-05-07 05:46:43 +00003134 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3135 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3136 EVT Type = Op.getValueType();
3137 int64_t Val = C->getSExtValue();
3138 if ((Val >= -65535) && (Val <= -1)) {
3139 Result = DAG.getTargetConstant(Val, Type);
3140 break;
3141 }
3142 }
3143 return;
Eric Christopher470578a2012-05-07 05:46:48 +00003144 case 'O': // signed 15 bit immediate
3145 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3146 EVT Type = Op.getValueType();
3147 int64_t Val = C->getSExtValue();
3148 if ((isInt<15>(Val))) {
3149 Result = DAG.getTargetConstant(Val, Type);
3150 break;
3151 }
3152 }
3153 return;
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003154 case 'P': // immediate in the range of 1 to 65535 (inclusive)
3155 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3156 EVT Type = Op.getValueType();
3157 int64_t Val = C->getSExtValue();
3158 if ((Val <= 65535) && (Val >= 1)) {
3159 Result = DAG.getTargetConstant(Val, Type);
3160 break;
3161 }
3162 }
3163 return;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003164 }
3165
3166 if (Result.getNode()) {
3167 Ops.push_back(Result);
3168 return;
3169 }
3170
3171 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3172}
3173
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003174bool MipsTargetLowering::isLegalAddressingMode(const AddrMode &AM,
3175 Type *Ty) const {
Akira Hatanakaef839192012-11-17 00:25:41 +00003176 // No global is ever allowed as a base.
3177 if (AM.BaseGV)
3178 return false;
3179
3180 switch (AM.Scale) {
3181 case 0: // "r+i" or just "i", depending on HasBaseReg.
3182 break;
3183 case 1:
3184 if (!AM.HasBaseReg) // allow "r+i".
3185 break;
3186 return false; // disallow "r+r" or "r+r+i".
3187 default:
3188 return false;
3189 }
3190
3191 return true;
3192}
3193
3194bool
Dan Gohman2fe6bee2008-10-18 02:06:02 +00003195MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3196 // The Mips target isn't yet aware of offsets.
3197 return false;
3198}
Evan Cheng16993aa2009-10-27 19:56:55 +00003199
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003200EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003201 unsigned SrcAlign,
3202 bool IsMemset, bool ZeroMemset,
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003203 bool MemcpyStrSrc,
3204 MachineFunction &MF) const {
3205 if (Subtarget->hasMips64())
3206 return MVT::i64;
3207
3208 return MVT::i32;
3209}
3210
Evan Cheng83896a52009-10-28 01:43:28 +00003211bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
3212 if (VT != MVT::f32 && VT != MVT::f64)
3213 return false;
Bruno Cardoso Lopesb02a9df2011-01-18 19:41:41 +00003214 if (Imm.isNegZero())
3215 return false;
Evan Cheng16993aa2009-10-27 19:56:55 +00003216 return Imm.isZero();
3217}
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003218
3219unsigned MipsTargetLowering::getJumpTableEncoding() const {
3220 if (IsN64)
3221 return MachineJumpTableInfo::EK_GPRel64BlockAddress;
Jia Liuf54f60f2012-02-28 07:46:26 +00003222
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003223 return TargetLowering::getJumpTableEncoding();
3224}
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003225
Akira Hatanakae092f722013-03-05 22:54:59 +00003226/// This function returns true if CallSym is a long double emulation routine.
3227static bool isF128SoftLibCall(const char *CallSym) {
3228 const char *const LibCalls[] =
3229 {"__addtf3", "__divtf3", "__eqtf2", "__extenddftf2", "__extendsftf2",
3230 "__fixtfdi", "__fixtfsi", "__fixtfti", "__fixunstfdi", "__fixunstfsi",
3231 "__fixunstfti", "__floatditf", "__floatsitf", "__floattitf",
3232 "__floatunditf", "__floatunsitf", "__floatuntitf", "__getf2", "__gttf2",
3233 "__letf2", "__lttf2", "__multf3", "__netf2", "__powitf2", "__subtf3",
3234 "__trunctfdf2", "__trunctfsf2", "__unordtf2",
3235 "ceill", "copysignl", "cosl", "exp2l", "expl", "floorl", "fmal", "fmodl",
3236 "log10l", "log2l", "logl", "nearbyintl", "powl", "rintl", "sinl", "sqrtl",
3237 "truncl"};
3238
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003239 const char *const *End = LibCalls + array_lengthof(LibCalls);
Akira Hatanakae092f722013-03-05 22:54:59 +00003240
3241 // Check that LibCalls is sorted alphabetically.
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003242 MipsTargetLowering::LTStr Comp;
Akira Hatanakae092f722013-03-05 22:54:59 +00003243
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003244#ifndef NDEBUG
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003245 for (const char *const *I = LibCalls; I < End - 1; ++I)
Akira Hatanakae092f722013-03-05 22:54:59 +00003246 assert(Comp(*I, *(I + 1)));
3247#endif
3248
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003249 return std::binary_search(LibCalls, End, CallSym, Comp);
Akira Hatanakae092f722013-03-05 22:54:59 +00003250}
3251
3252/// This function returns true if Ty is fp128 or i128 which was originally a
3253/// fp128.
3254static bool originalTypeIsF128(const Type *Ty, const SDNode *CallNode) {
3255 if (Ty->isFP128Ty())
3256 return true;
3257
3258 const ExternalSymbolSDNode *ES =
3259 dyn_cast_or_null<const ExternalSymbolSDNode>(CallNode);
3260
3261 // If the Ty is i128 and the function being called is a long double emulation
3262 // routine, then the original type is f128.
3263 return (ES && Ty->isIntegerTy(128) && isF128SoftLibCall(ES->getSymbol()));
3264}
3265
Reed Kotler783c7942013-05-10 22:25:39 +00003266MipsTargetLowering::MipsCC::SpecialCallingConvType
3267 MipsTargetLowering::getSpecialCallingConv(SDValue Callee) const {
3268 MipsCC::SpecialCallingConvType SpecialCallingConv =
3269 MipsCC::NoSpecialCallingConv;;
3270 if (Subtarget->inMips16HardFloat()) {
3271 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3272 llvm::StringRef Sym = G->getGlobal()->getName();
3273 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
Reed Kotler3230e722013-12-12 02:41:11 +00003274 if (F && F->hasFnAttribute("__Mips16RetHelper")) {
Reed Kotler783c7942013-05-10 22:25:39 +00003275 SpecialCallingConv = MipsCC::Mips16RetHelperConv;
3276 }
3277 }
3278 }
3279 return SpecialCallingConv;
3280}
3281
3282MipsTargetLowering::MipsCC::MipsCC(
Akira Hatanakabfb66242013-08-20 23:38:40 +00003283 CallingConv::ID CC, bool IsO32_, bool IsFP64_, CCState &Info,
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003284 MipsCC::SpecialCallingConvType SpecialCallingConv_)
Akira Hatanakabfb66242013-08-20 23:38:40 +00003285 : CCInfo(Info), CallConv(CC), IsO32(IsO32_), IsFP64(IsFP64_),
Reed Kotler783c7942013-05-10 22:25:39 +00003286 SpecialCallingConv(SpecialCallingConv_){
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003287 // Pre-allocate reserved argument area.
Akira Hatanaka5001be52013-02-15 21:45:11 +00003288 CCInfo.AllocateStack(reservedArgArea(), 1);
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003289}
3290
Reed Kotler783c7942013-05-10 22:25:39 +00003291
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003292void MipsTargetLowering::MipsCC::
Akira Hatanaka5001be52013-02-15 21:45:11 +00003293analyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Args,
Akira Hatanaka3b7391d2013-03-05 22:20:28 +00003294 bool IsVarArg, bool IsSoftFloat, const SDNode *CallNode,
3295 std::vector<ArgListEntry> &FuncArgs) {
Akira Hatanaka5001be52013-02-15 21:45:11 +00003296 assert((CallConv != CallingConv::Fast || !IsVarArg) &&
3297 "CallingConv::Fast shouldn't be used for vararg functions.");
3298
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003299 unsigned NumOpnds = Args.size();
Akira Hatanaka5001be52013-02-15 21:45:11 +00003300 llvm::CCAssignFn *FixedFn = fixedArgFn(), *VarFn = varArgFn();
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003301
3302 for (unsigned I = 0; I != NumOpnds; ++I) {
3303 MVT ArgVT = Args[I].VT;
3304 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
3305 bool R;
3306
3307 if (ArgFlags.isByVal()) {
3308 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
3309 continue;
3310 }
3311
Akira Hatanaka5001be52013-02-15 21:45:11 +00003312 if (IsVarArg && !Args[I].IsFixed)
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003313 R = VarFn(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
Akira Hatanaka3b7391d2013-03-05 22:20:28 +00003314 else {
3315 MVT RegVT = getRegVT(ArgVT, FuncArgs[Args[I].OrigArgIndex].Ty, CallNode,
3316 IsSoftFloat);
3317 R = FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo);
3318 }
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003319
3320 if (R) {
3321#ifndef NDEBUG
3322 dbgs() << "Call operand #" << I << " has unhandled type "
3323 << EVT(ArgVT).getEVTString();
3324#endif
3325 llvm_unreachable(0);
3326 }
3327 }
3328}
3329
3330void MipsTargetLowering::MipsCC::
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003331analyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Args,
3332 bool IsSoftFloat, Function::const_arg_iterator FuncArg) {
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003333 unsigned NumArgs = Args.size();
Akira Hatanaka5001be52013-02-15 21:45:11 +00003334 llvm::CCAssignFn *FixedFn = fixedArgFn();
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003335 unsigned CurArgIdx = 0;
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003336
3337 for (unsigned I = 0; I != NumArgs; ++I) {
3338 MVT ArgVT = Args[I].VT;
3339 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003340 std::advance(FuncArg, Args[I].OrigArgIndex - CurArgIdx);
3341 CurArgIdx = Args[I].OrigArgIndex;
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003342
3343 if (ArgFlags.isByVal()) {
3344 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
3345 continue;
3346 }
3347
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003348 MVT RegVT = getRegVT(ArgVT, FuncArg->getType(), 0, IsSoftFloat);
3349
3350 if (!FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo))
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003351 continue;
3352
3353#ifndef NDEBUG
3354 dbgs() << "Formal Arg #" << I << " has unhandled type "
3355 << EVT(ArgVT).getEVTString();
3356#endif
3357 llvm_unreachable(0);
3358 }
3359}
3360
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003361template<typename Ty>
3362void MipsTargetLowering::MipsCC::
3363analyzeReturn(const SmallVectorImpl<Ty> &RetVals, bool IsSoftFloat,
3364 const SDNode *CallNode, const Type *RetTy) const {
Akira Hatanakae092f722013-03-05 22:54:59 +00003365 CCAssignFn *Fn;
3366
3367 if (IsSoftFloat && originalTypeIsF128(RetTy, CallNode))
3368 Fn = RetCC_F128Soft;
3369 else
3370 Fn = RetCC_Mips;
3371
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003372 for (unsigned I = 0, E = RetVals.size(); I < E; ++I) {
3373 MVT VT = RetVals[I].VT;
3374 ISD::ArgFlagsTy Flags = RetVals[I].Flags;
3375 MVT RegVT = this->getRegVT(VT, RetTy, CallNode, IsSoftFloat);
3376
Akira Hatanakae092f722013-03-05 22:54:59 +00003377 if (Fn(I, VT, RegVT, CCValAssign::Full, Flags, this->CCInfo)) {
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003378#ifndef NDEBUG
3379 dbgs() << "Call result #" << I << " has unhandled type "
3380 << EVT(VT).getEVTString() << '\n';
3381#endif
3382 llvm_unreachable(0);
3383 }
3384 }
3385}
3386
3387void MipsTargetLowering::MipsCC::
3388analyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins, bool IsSoftFloat,
3389 const SDNode *CallNode, const Type *RetTy) const {
3390 analyzeReturn(Ins, IsSoftFloat, CallNode, RetTy);
3391}
3392
3393void MipsTargetLowering::MipsCC::
3394analyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs, bool IsSoftFloat,
3395 const Type *RetTy) const {
3396 analyzeReturn(Outs, IsSoftFloat, 0, RetTy);
3397}
3398
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003399void MipsTargetLowering::MipsCC::handleByValArg(unsigned ValNo, MVT ValVT,
3400 MVT LocVT,
3401 CCValAssign::LocInfo LocInfo,
3402 ISD::ArgFlagsTy ArgFlags) {
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003403 assert(ArgFlags.getByValSize() && "Byval argument's size shouldn't be 0.");
3404
3405 struct ByValArgInfo ByVal;
Akira Hatanaka5001be52013-02-15 21:45:11 +00003406 unsigned RegSize = regSize();
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003407 unsigned ByValSize = RoundUpToAlignment(ArgFlags.getByValSize(), RegSize);
3408 unsigned Align = std::min(std::max(ArgFlags.getByValAlign(), RegSize),
3409 RegSize * 2);
3410
Akira Hatanaka5001be52013-02-15 21:45:11 +00003411 if (useRegsForByval())
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003412 allocateRegs(ByVal, ByValSize, Align);
3413
3414 // Allocate space on caller's stack.
3415 ByVal.Address = CCInfo.AllocateStack(ByValSize - RegSize * ByVal.NumRegs,
3416 Align);
3417 CCInfo.addLoc(CCValAssign::getMem(ValNo, ValVT, ByVal.Address, LocVT,
3418 LocInfo));
3419 ByValArgs.push_back(ByVal);
3420}
3421
Akira Hatanaka5001be52013-02-15 21:45:11 +00003422unsigned MipsTargetLowering::MipsCC::numIntArgRegs() const {
3423 return IsO32 ? array_lengthof(O32IntRegs) : array_lengthof(Mips64IntRegs);
3424}
3425
3426unsigned MipsTargetLowering::MipsCC::reservedArgArea() const {
3427 return (IsO32 && (CallConv != CallingConv::Fast)) ? 16 : 0;
3428}
3429
3430const uint16_t *MipsTargetLowering::MipsCC::intArgRegs() const {
3431 return IsO32 ? O32IntRegs : Mips64IntRegs;
3432}
3433
3434llvm::CCAssignFn *MipsTargetLowering::MipsCC::fixedArgFn() const {
3435 if (CallConv == CallingConv::Fast)
3436 return CC_Mips_FastCC;
3437
Reed Kotler783c7942013-05-10 22:25:39 +00003438 if (SpecialCallingConv == Mips16RetHelperConv)
3439 return CC_Mips16RetHelper;
Akira Hatanakabfb66242013-08-20 23:38:40 +00003440 return IsO32 ? (IsFP64 ? CC_MipsO32_FP64 : CC_MipsO32_FP32) : CC_MipsN;
Akira Hatanaka5001be52013-02-15 21:45:11 +00003441}
3442
3443llvm::CCAssignFn *MipsTargetLowering::MipsCC::varArgFn() const {
Akira Hatanakabfb66242013-08-20 23:38:40 +00003444 return IsO32 ? (IsFP64 ? CC_MipsO32_FP64 : CC_MipsO32_FP32) : CC_MipsN_VarArg;
Akira Hatanaka5001be52013-02-15 21:45:11 +00003445}
3446
3447const uint16_t *MipsTargetLowering::MipsCC::shadowRegs() const {
3448 return IsO32 ? O32IntRegs : Mips64DPRegs;
3449}
3450
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003451void MipsTargetLowering::MipsCC::allocateRegs(ByValArgInfo &ByVal,
3452 unsigned ByValSize,
3453 unsigned Align) {
Akira Hatanaka5001be52013-02-15 21:45:11 +00003454 unsigned RegSize = regSize(), NumIntArgRegs = numIntArgRegs();
3455 const uint16_t *IntArgRegs = intArgRegs(), *ShadowRegs = shadowRegs();
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003456 assert(!(ByValSize % RegSize) && !(Align % RegSize) &&
3457 "Byval argument's size and alignment should be a multiple of"
3458 "RegSize.");
3459
3460 ByVal.FirstIdx = CCInfo.getFirstUnallocated(IntArgRegs, NumIntArgRegs);
3461
3462 // If Align > RegSize, the first arg register must be even.
3463 if ((Align > RegSize) && (ByVal.FirstIdx % 2)) {
3464 CCInfo.AllocateReg(IntArgRegs[ByVal.FirstIdx], ShadowRegs[ByVal.FirstIdx]);
3465 ++ByVal.FirstIdx;
3466 }
3467
3468 // Mark the registers allocated.
3469 for (unsigned I = ByVal.FirstIdx; ByValSize && (I < NumIntArgRegs);
3470 ByValSize -= RegSize, ++I, ++ByVal.NumRegs)
3471 CCInfo.AllocateReg(IntArgRegs[I], ShadowRegs[I]);
3472}
Akira Hatanaka25dad192012-10-27 00:10:18 +00003473
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003474MVT MipsTargetLowering::MipsCC::getRegVT(MVT VT, const Type *OrigTy,
3475 const SDNode *CallNode,
3476 bool IsSoftFloat) const {
3477 if (IsSoftFloat || IsO32)
3478 return VT;
3479
3480 // Check if the original type was fp128.
Akira Hatanakae092f722013-03-05 22:54:59 +00003481 if (originalTypeIsF128(OrigTy, CallNode)) {
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003482 assert(VT == MVT::i64);
3483 return MVT::f64;
3484 }
3485
3486 return VT;
3487}
3488
Akira Hatanaka25dad192012-10-27 00:10:18 +00003489void MipsTargetLowering::
Andrew Trickef9de2a2013-05-25 02:42:55 +00003490copyByValRegs(SDValue Chain, SDLoc DL, std::vector<SDValue> &OutChains,
Akira Hatanaka25dad192012-10-27 00:10:18 +00003491 SelectionDAG &DAG, const ISD::ArgFlagsTy &Flags,
3492 SmallVectorImpl<SDValue> &InVals, const Argument *FuncArg,
3493 const MipsCC &CC, const ByValArgInfo &ByVal) const {
3494 MachineFunction &MF = DAG.getMachineFunction();
3495 MachineFrameInfo *MFI = MF.getFrameInfo();
3496 unsigned RegAreaSize = ByVal.NumRegs * CC.regSize();
3497 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
3498 int FrameObjOffset;
3499
3500 if (RegAreaSize)
3501 FrameObjOffset = (int)CC.reservedArgArea() -
3502 (int)((CC.numIntArgRegs() - ByVal.FirstIdx) * CC.regSize());
3503 else
3504 FrameObjOffset = ByVal.Address;
3505
3506 // Create frame object.
3507 EVT PtrTy = getPointerTy();
3508 int FI = MFI->CreateFixedObject(FrameObjSize, FrameObjOffset, true);
3509 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
3510 InVals.push_back(FIN);
3511
3512 if (!ByVal.NumRegs)
3513 return;
3514
3515 // Copy arg registers.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00003516 MVT RegTy = MVT::getIntegerVT(CC.regSize() * 8);
Akira Hatanaka25dad192012-10-27 00:10:18 +00003517 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3518
3519 for (unsigned I = 0; I < ByVal.NumRegs; ++I) {
3520 unsigned ArgReg = CC.intArgRegs()[ByVal.FirstIdx + I];
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003521 unsigned VReg = addLiveIn(MF, ArgReg, RC);
Akira Hatanaka25dad192012-10-27 00:10:18 +00003522 unsigned Offset = I * CC.regSize();
3523 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
3524 DAG.getConstant(Offset, PtrTy));
3525 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
3526 StorePtr, MachinePointerInfo(FuncArg, Offset),
3527 false, false, 0);
3528 OutChains.push_back(Store);
3529 }
3530}
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003531
3532// Copy byVal arg to registers and stack.
3533void MipsTargetLowering::
Andrew Trickef9de2a2013-05-25 02:42:55 +00003534passByValArg(SDValue Chain, SDLoc DL,
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00003535 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
Craig Topperb94011f2013-07-14 04:42:23 +00003536 SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr,
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003537 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
3538 const MipsCC &CC, const ByValArgInfo &ByVal,
3539 const ISD::ArgFlagsTy &Flags, bool isLittle) const {
3540 unsigned ByValSize = Flags.getByValSize();
3541 unsigned Offset = 0; // Offset in # of bytes from the beginning of struct.
3542 unsigned RegSize = CC.regSize();
3543 unsigned Alignment = std::min(Flags.getByValAlign(), RegSize);
3544 EVT PtrTy = getPointerTy(), RegTy = MVT::getIntegerVT(RegSize * 8);
3545
3546 if (ByVal.NumRegs) {
3547 const uint16_t *ArgRegs = CC.intArgRegs();
3548 bool LeftoverBytes = (ByVal.NumRegs * RegSize > ByValSize);
3549 unsigned I = 0;
3550
3551 // Copy words to registers.
3552 for (; I < ByVal.NumRegs - LeftoverBytes; ++I, Offset += RegSize) {
3553 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3554 DAG.getConstant(Offset, PtrTy));
3555 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
3556 MachinePointerInfo(), false, false, false,
3557 Alignment);
3558 MemOpChains.push_back(LoadVal.getValue(1));
3559 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3560 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
3561 }
3562
3563 // Return if the struct has been fully copied.
3564 if (ByValSize == Offset)
3565 return;
3566
3567 // Copy the remainder of the byval argument with sub-word loads and shifts.
3568 if (LeftoverBytes) {
3569 assert((ByValSize > Offset) && (ByValSize < Offset + RegSize) &&
3570 "Size of the remainder should be smaller than RegSize.");
3571 SDValue Val;
3572
3573 for (unsigned LoadSize = RegSize / 2, TotalSizeLoaded = 0;
3574 Offset < ByValSize; LoadSize /= 2) {
3575 unsigned RemSize = ByValSize - Offset;
3576
3577 if (RemSize < LoadSize)
3578 continue;
3579
3580 // Load subword.
3581 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3582 DAG.getConstant(Offset, PtrTy));
3583 SDValue LoadVal =
3584 DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr,
3585 MachinePointerInfo(), MVT::getIntegerVT(LoadSize * 8),
3586 false, false, Alignment);
3587 MemOpChains.push_back(LoadVal.getValue(1));
3588
3589 // Shift the loaded value.
3590 unsigned Shamt;
3591
3592 if (isLittle)
3593 Shamt = TotalSizeLoaded;
3594 else
3595 Shamt = (RegSize - (TotalSizeLoaded + LoadSize)) * 8;
3596
3597 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
3598 DAG.getConstant(Shamt, MVT::i32));
3599
3600 if (Val.getNode())
3601 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
3602 else
3603 Val = Shift;
3604
3605 Offset += LoadSize;
3606 TotalSizeLoaded += LoadSize;
3607 Alignment = std::min(Alignment, LoadSize);
3608 }
3609
3610 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3611 RegsToPass.push_back(std::make_pair(ArgReg, Val));
3612 return;
3613 }
3614 }
3615
3616 // Copy remainder of byval arg to it with memcpy.
3617 unsigned MemCpySize = ByValSize - Offset;
3618 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3619 DAG.getConstant(Offset, PtrTy));
3620 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
3621 DAG.getIntPtrConstant(ByVal.Address));
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003622 Chain = DAG.getMemcpy(Chain, DL, Dst, Src, DAG.getConstant(MemCpySize, PtrTy),
3623 Alignment, /*isVolatile=*/false, /*AlwaysInline=*/false,
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003624 MachinePointerInfo(0), MachinePointerInfo(0));
3625 MemOpChains.push_back(Chain);
3626}
Akira Hatanaka2a134022012-10-27 00:21:13 +00003627
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003628void MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
3629 const MipsCC &CC, SDValue Chain,
3630 SDLoc DL, SelectionDAG &DAG) const {
Akira Hatanaka2a134022012-10-27 00:21:13 +00003631 unsigned NumRegs = CC.numIntArgRegs();
3632 const uint16_t *ArgRegs = CC.intArgRegs();
3633 const CCState &CCInfo = CC.getCCInfo();
3634 unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs, NumRegs);
3635 unsigned RegSize = CC.regSize();
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00003636 MVT RegTy = MVT::getIntegerVT(RegSize * 8);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003637 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3638 MachineFunction &MF = DAG.getMachineFunction();
3639 MachineFrameInfo *MFI = MF.getFrameInfo();
3640 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3641
3642 // Offset of the first variable argument from stack pointer.
3643 int VaArgOffset;
3644
3645 if (NumRegs == Idx)
3646 VaArgOffset = RoundUpToAlignment(CCInfo.getNextStackOffset(), RegSize);
3647 else
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003648 VaArgOffset = (int)CC.reservedArgArea() - (int)(RegSize * (NumRegs - Idx));
Akira Hatanaka2a134022012-10-27 00:21:13 +00003649
3650 // Record the frame index of the first variable argument
3651 // which is a value necessary to VASTART.
3652 int FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
3653 MipsFI->setVarArgsFrameIndex(FI);
3654
3655 // Copy the integer registers that have not been used for argument passing
3656 // to the argument register save area. For O32, the save area is allocated
3657 // in the caller's stack frame, while for N32/64, it is allocated in the
3658 // callee's stack frame.
3659 for (unsigned I = Idx; I < NumRegs; ++I, VaArgOffset += RegSize) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003660 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003661 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
3662 FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
3663 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
3664 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
3665 MachinePointerInfo(), false, false, 0);
3666 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(0);
3667 OutChains.push_back(Store);
3668 }
3669}