blob: 8ae80d27315a20018657b557f1b50d672ead8346 [file] [log] [blame]
Jia Liuf54f60f2012-02-28 07:46:26 +00001//===-- MipsISelLowering.cpp - Mips DAG Lowering Implementation -----------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00007//
Akira Hatanakae2489122011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00009//
10// This file defines the interfaces that Mips uses to lower LLVM code into a
11// selection DAG.
12//
Akira Hatanakae2489122011-04-15 21:51:11 +000013//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000014#include "MipsISelLowering.h"
Craig Topperb25fda92012-03-17 18:46:09 +000015#include "InstPrinter/MipsInstPrinter.h"
16#include "MCTargetDesc/MipsBaseInfo.h"
Daniel Sanders0456c152014-11-07 14:24:31 +000017#include "MipsCCState.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"
Eric Christopher79cc1e32014-09-02 22:28:02 +000028#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000029#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000030#include "llvm/CodeGen/SelectionDAGISel.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000031#include "llvm/CodeGen/ValueTypes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000032#include "llvm/IR/CallingConv.h"
33#include "llvm/IR/DerivedTypes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000034#include "llvm/IR/GlobalVariable.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
Chandler Carruth84e68b22014-04-22 02:41:26 +000043#define DEBUG_TYPE "mips-lower"
44
Akira Hatanaka90131ac2012-10-19 21:47:33 +000045STATISTIC(NumTailCalls, "Number of tail calls");
46
47static cl::opt<bool>
Akira Hatanaka59f299f2012-11-21 20:21:11 +000048LargeGOT("mxgot", cl::Hidden,
49 cl::desc("MIPS: Enable GOT larger than 64k."), cl::init(false));
50
Akira Hatanaka1cb02422013-05-20 18:07:43 +000051static cl::opt<bool>
Akira Hatanakabe76cd02013-05-21 17:17:59 +000052NoZeroDivCheck("mno-check-zero-division", cl::Hidden,
Akira Hatanaka1cb02422013-05-20 18:07:43 +000053 cl::desc("MIPS: Don't trap on integer division by zero."),
54 cl::init(false));
55
Reed Kotler720c5ca2014-04-17 22:15:34 +000056cl::opt<bool>
57EnableMipsFastISel("mips-fast-isel", cl::Hidden,
58 cl::desc("Allow mips-fast-isel to be used"),
59 cl::init(false));
60
Craig Topper840beec2014-04-04 05:16:06 +000061static const MCPhysReg Mips64DPRegs[8] = {
Akira Hatanakaac8c6692012-10-27 00:29:43 +000062 Mips::D12_64, Mips::D13_64, Mips::D14_64, Mips::D15_64,
63 Mips::D16_64, Mips::D17_64, Mips::D18_64, Mips::D19_64
64};
65
Jia Liuf54f60f2012-02-28 07:46:26 +000066// If I is a shifted mask, set the size (Size) and the first bit of the
Akira Hatanaka73d78b72011-08-18 20:07:42 +000067// mask (Pos), and return true.
Jia Liuf54f60f2012-02-28 07:46:26 +000068// For example, if I is 0x003ff800, (Pos, Size) = (11, 11).
Akira Hatanaka0bb60d892013-03-12 00:16:36 +000069static bool isShiftedMask(uint64_t I, uint64_t &Pos, uint64_t &Size) {
Akira Hatanaka20cee2e2011-12-05 21:26:34 +000070 if (!isShiftedMask_64(I))
Akira Hatanaka4c0a7122013-10-07 19:33:02 +000071 return false;
Akira Hatanaka5360f882011-08-17 02:05:42 +000072
Akira Hatanaka20cee2e2011-12-05 21:26:34 +000073 Size = CountPopulation_64(I);
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +000074 Pos = countTrailingZeros(I);
Akira Hatanaka73d78b72011-08-18 20:07:42 +000075 return true;
Akira Hatanaka5360f882011-08-17 02:05:42 +000076}
77
Akira Hatanaka96ca1822013-03-13 00:54:29 +000078SDValue MipsTargetLowering::getGlobalReg(SelectionDAG &DAG, EVT Ty) const {
Akira Hatanakab049aef2012-02-24 22:34:47 +000079 MipsFunctionInfo *FI = DAG.getMachineFunction().getInfo<MipsFunctionInfo>();
80 return DAG.getRegister(FI->getGlobalBaseReg(), Ty);
81}
82
Akira Hatanakad8f10ce2013-09-27 19:51:35 +000083SDValue MipsTargetLowering::getTargetNode(GlobalAddressSDNode *N, EVT Ty,
84 SelectionDAG &DAG,
Akira Hatanaka96ca1822013-03-13 00:54:29 +000085 unsigned Flag) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +000086 return DAG.getTargetGlobalAddress(N->getGlobal(), SDLoc(N), Ty, 0, Flag);
Akira Hatanakafd04ad42012-11-21 20:26:38 +000087}
88
Akira Hatanakad8f10ce2013-09-27 19:51:35 +000089SDValue MipsTargetLowering::getTargetNode(ExternalSymbolSDNode *N, EVT Ty,
90 SelectionDAG &DAG,
91 unsigned Flag) const {
92 return DAG.getTargetExternalSymbol(N->getSymbol(), Ty, Flag);
93}
94
95SDValue MipsTargetLowering::getTargetNode(BlockAddressSDNode *N, EVT Ty,
96 SelectionDAG &DAG,
97 unsigned Flag) const {
98 return DAG.getTargetBlockAddress(N->getBlockAddress(), Ty, 0, Flag);
99}
100
101SDValue MipsTargetLowering::getTargetNode(JumpTableSDNode *N, EVT Ty,
102 SelectionDAG &DAG,
103 unsigned Flag) const {
104 return DAG.getTargetJumpTable(N->getIndex(), Ty, Flag);
105}
106
107SDValue MipsTargetLowering::getTargetNode(ConstantPoolSDNode *N, EVT Ty,
108 SelectionDAG &DAG,
109 unsigned Flag) const {
110 return DAG.getTargetConstantPool(N->getConstVal(), Ty, N->getAlignment(),
111 N->getOffset(), Flag);
Akira Hatanakafd04ad42012-11-21 20:26:38 +0000112}
113
Chris Lattner5e693ed2009-07-28 03:13:23 +0000114const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
115 switch (Opcode) {
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000116 case MipsISD::JmpLink: return "MipsISD::JmpLink";
Akira Hatanaka91318df2012-10-19 20:59:39 +0000117 case MipsISD::TailCall: return "MipsISD::TailCall";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000118 case MipsISD::Hi: return "MipsISD::Hi";
119 case MipsISD::Lo: return "MipsISD::Lo";
120 case MipsISD::GPRel: return "MipsISD::GPRel";
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +0000121 case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000122 case MipsISD::Ret: return "MipsISD::Ret";
Akira Hatanakac0b02062013-01-30 00:26:49 +0000123 case MipsISD::EH_RETURN: return "MipsISD::EH_RETURN";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000124 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
125 case MipsISD::FPCmp: return "MipsISD::FPCmp";
126 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
127 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000128 case MipsISD::TruncIntFP: return "MipsISD::TruncIntFP";
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000129 case MipsISD::MFHI: return "MipsISD::MFHI";
130 case MipsISD::MFLO: return "MipsISD::MFLO";
131 case MipsISD::MTLOHI: return "MipsISD::MTLOHI";
Akira Hatanaka28721bd2013-03-30 01:14:04 +0000132 case MipsISD::Mult: return "MipsISD::Mult";
133 case MipsISD::Multu: return "MipsISD::Multu";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000134 case MipsISD::MAdd: return "MipsISD::MAdd";
135 case MipsISD::MAddu: return "MipsISD::MAddu";
136 case MipsISD::MSub: return "MipsISD::MSub";
137 case MipsISD::MSubu: return "MipsISD::MSubu";
138 case MipsISD::DivRem: return "MipsISD::DivRem";
139 case MipsISD::DivRemU: return "MipsISD::DivRemU";
Akira Hatanaka28721bd2013-03-30 01:14:04 +0000140 case MipsISD::DivRem16: return "MipsISD::DivRem16";
141 case MipsISD::DivRemU16: return "MipsISD::DivRemU16";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000142 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
143 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
Akira Hatanakafaa88c02011-12-12 22:38:19 +0000144 case MipsISD::Wrapper: return "MipsISD::Wrapper";
Akira Hatanakaa4c09bc2011-07-19 23:30:50 +0000145 case MipsISD::Sync: return "MipsISD::Sync";
Akira Hatanaka5360f882011-08-17 02:05:42 +0000146 case MipsISD::Ext: return "MipsISD::Ext";
147 case MipsISD::Ins: return "MipsISD::Ins";
Akira Hatanakab9ebf8d2012-06-02 00:03:12 +0000148 case MipsISD::LWL: return "MipsISD::LWL";
149 case MipsISD::LWR: return "MipsISD::LWR";
150 case MipsISD::SWL: return "MipsISD::SWL";
151 case MipsISD::SWR: return "MipsISD::SWR";
152 case MipsISD::LDL: return "MipsISD::LDL";
153 case MipsISD::LDR: return "MipsISD::LDR";
154 case MipsISD::SDL: return "MipsISD::SDL";
155 case MipsISD::SDR: return "MipsISD::SDR";
Akira Hatanaka233ac532012-09-21 23:52:47 +0000156 case MipsISD::EXTP: return "MipsISD::EXTP";
157 case MipsISD::EXTPDP: return "MipsISD::EXTPDP";
158 case MipsISD::EXTR_S_H: return "MipsISD::EXTR_S_H";
159 case MipsISD::EXTR_W: return "MipsISD::EXTR_W";
160 case MipsISD::EXTR_R_W: return "MipsISD::EXTR_R_W";
161 case MipsISD::EXTR_RS_W: return "MipsISD::EXTR_RS_W";
162 case MipsISD::SHILO: return "MipsISD::SHILO";
163 case MipsISD::MTHLIP: return "MipsISD::MTHLIP";
164 case MipsISD::MULT: return "MipsISD::MULT";
165 case MipsISD::MULTU: return "MipsISD::MULTU";
Jia Liu434874d2013-03-04 01:06:54 +0000166 case MipsISD::MADD_DSP: return "MipsISD::MADD_DSP";
Akira Hatanaka233ac532012-09-21 23:52:47 +0000167 case MipsISD::MADDU_DSP: return "MipsISD::MADDU_DSP";
168 case MipsISD::MSUB_DSP: return "MipsISD::MSUB_DSP";
169 case MipsISD::MSUBU_DSP: return "MipsISD::MSUBU_DSP";
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000170 case MipsISD::SHLL_DSP: return "MipsISD::SHLL_DSP";
171 case MipsISD::SHRA_DSP: return "MipsISD::SHRA_DSP";
172 case MipsISD::SHRL_DSP: return "MipsISD::SHRL_DSP";
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000173 case MipsISD::SETCC_DSP: return "MipsISD::SETCC_DSP";
174 case MipsISD::SELECT_CC_DSP: return "MipsISD::SELECT_CC_DSP";
Daniel Sandersce09d072013-08-28 12:14:50 +0000175 case MipsISD::VALL_ZERO: return "MipsISD::VALL_ZERO";
176 case MipsISD::VANY_ZERO: return "MipsISD::VANY_ZERO";
177 case MipsISD::VALL_NONZERO: return "MipsISD::VALL_NONZERO";
178 case MipsISD::VANY_NONZERO: return "MipsISD::VANY_NONZERO";
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000179 case MipsISD::VCEQ: return "MipsISD::VCEQ";
180 case MipsISD::VCLE_S: return "MipsISD::VCLE_S";
181 case MipsISD::VCLE_U: return "MipsISD::VCLE_U";
182 case MipsISD::VCLT_S: return "MipsISD::VCLT_S";
183 case MipsISD::VCLT_U: return "MipsISD::VCLT_U";
Daniel Sanders3ce56622013-09-24 12:18:31 +0000184 case MipsISD::VSMAX: return "MipsISD::VSMAX";
185 case MipsISD::VSMIN: return "MipsISD::VSMIN";
186 case MipsISD::VUMAX: return "MipsISD::VUMAX";
187 case MipsISD::VUMIN: return "MipsISD::VUMIN";
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000188 case MipsISD::VEXTRACT_SEXT_ELT: return "MipsISD::VEXTRACT_SEXT_ELT";
189 case MipsISD::VEXTRACT_ZEXT_ELT: return "MipsISD::VEXTRACT_ZEXT_ELT";
Daniel Sandersf7456c72013-09-23 13:22:24 +0000190 case MipsISD::VNOR: return "MipsISD::VNOR";
Daniel Sanderse5087042013-09-24 14:02:15 +0000191 case MipsISD::VSHF: return "MipsISD::VSHF";
Daniel Sanders26307182013-09-24 14:20:00 +0000192 case MipsISD::SHF: return "MipsISD::SHF";
Daniel Sanders2ed228b2013-09-24 14:36:12 +0000193 case MipsISD::ILVEV: return "MipsISD::ILVEV";
194 case MipsISD::ILVOD: return "MipsISD::ILVOD";
195 case MipsISD::ILVL: return "MipsISD::ILVL";
196 case MipsISD::ILVR: return "MipsISD::ILVR";
Daniel Sandersfae5f2a2013-09-24 14:53:25 +0000197 case MipsISD::PCKEV: return "MipsISD::PCKEV";
198 case MipsISD::PCKOD: return "MipsISD::PCKOD";
Daniel Sandersb50ccf82014-04-01 10:35:28 +0000199 case MipsISD::INSVE: return "MipsISD::INSVE";
Craig Topper062a2ba2014-04-25 05:30:21 +0000200 default: return nullptr;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000201 }
202}
203
Eric Christopherb1526602014-09-19 23:30:42 +0000204MipsTargetLowering::MipsTargetLowering(const MipsTargetMachine &TM,
Eric Christopher8924d272014-07-18 23:25:04 +0000205 const MipsSubtarget &STI)
Aditya Nandakumar30531552014-11-13 21:29:21 +0000206 : TargetLowering(TM), Subtarget(STI) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000207 // Mips does not have i1 type, so use i32 for
Wesley Peck527da1b2010-11-23 03:31:01 +0000208 // setcc operations results (slt, sgt, ...).
Duncan Sands8d6e2e12008-11-23 15:47:28 +0000209 setBooleanContents(ZeroOrOneBooleanContent);
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000210 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Daniel Sanderscbd44c52014-07-10 10:18:12 +0000211 // The cmp.cond.fmt instruction in MIPS32r6/MIPS64r6 uses 0 and -1 like MSA
212 // does. Integer booleans still use 0 and 1.
Eric Christopher1c29a652014-07-18 22:55:25 +0000213 if (Subtarget.hasMips32r6())
Daniel Sanderscbd44c52014-07-10 10:18:12 +0000214 setBooleanContents(ZeroOrOneBooleanContent,
215 ZeroOrNegativeOneBooleanContent);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000216
Wesley Peck527da1b2010-11-23 03:31:01 +0000217 // Load extented operations for i1 types must be promoted
Owen Anderson9f944592009-08-11 20:47:22 +0000218 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
219 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
220 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000221
Eli Friedman1fa07e12009-07-17 04:07:24 +0000222 // MIPS doesn't have extending float->double load/store
Owen Anderson9f944592009-08-11 20:47:22 +0000223 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
224 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman39d6faa2009-07-17 02:28:12 +0000225
Wesley Peck527da1b2010-11-23 03:31:01 +0000226 // Used by legalize types to correctly generate the setcc result.
227 // Without this, every float setcc comes with a AND/OR with the result,
228 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +0000229 // which is used implicitly by brcond and select operations.
Owen Anderson9f944592009-08-11 20:47:22 +0000230 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +0000231
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000232 // Mips Custom Operations
Akira Hatanaka0f693a82013-03-06 21:32:03 +0000233 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000234 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000235 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000236 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
237 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
238 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
239 setOperationAction(ISD::SELECT, MVT::f32, Custom);
240 setOperationAction(ISD::SELECT, MVT::f64, Custom);
241 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Akira Hatanaka24cf4e32012-07-11 19:32:27 +0000242 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
243 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Akira Hatanakab7f78592012-03-09 23:46:03 +0000244 setOperationAction(ISD::SETCC, MVT::f32, Custom);
245 setOperationAction(ISD::SETCC, MVT::f64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000246 setOperationAction(ISD::BRCOND, 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
Eric Christopher1c29a652014-07-18 22:55:25 +0000251 if (Subtarget.isGP64bit()) {
Akira Hatanakada00aa82012-03-10 00:03:50 +0000252 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
253 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
254 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
255 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
256 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
257 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Akira Hatanaka019e5922012-06-02 00:04:42 +0000258 setOperationAction(ISD::LOAD, MVT::i64, Custom);
259 setOperationAction(ISD::STORE, MVT::i64, Custom);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000260 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000261 }
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +0000262
Eric Christopher1c29a652014-07-18 22:55:25 +0000263 if (!Subtarget.isGP64bit()) {
Akira Hatanaka0a8ab712012-05-09 00:55:21 +0000264 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
265 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
266 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
267 }
268
Akira Hatanaka28e02ec2012-11-07 19:10:58 +0000269 setOperationAction(ISD::ADD, MVT::i32, Custom);
Eric Christopher1c29a652014-07-18 22:55:25 +0000270 if (Subtarget.isGP64bit())
Akira Hatanaka28e02ec2012-11-07 19:10:58 +0000271 setOperationAction(ISD::ADD, MVT::i64, Custom);
272
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000273 setOperationAction(ISD::SDIV, MVT::i32, Expand);
274 setOperationAction(ISD::SREM, MVT::i32, Expand);
275 setOperationAction(ISD::UDIV, MVT::i32, Expand);
276 setOperationAction(ISD::UREM, MVT::i32, Expand);
Akira Hatanakab1538f92011-10-03 21:06:13 +0000277 setOperationAction(ISD::SDIV, MVT::i64, Expand);
278 setOperationAction(ISD::SREM, MVT::i64, Expand);
279 setOperationAction(ISD::UDIV, MVT::i64, Expand);
280 setOperationAction(ISD::UREM, MVT::i64, Expand);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000281
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000282 // Operations not directly supported by Mips.
Tom Stellardb1588fc2013-03-08 15:36:57 +0000283 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
284 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
285 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
286 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
Tom Stellard3787b122014-06-10 16:01:29 +0000287 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
288 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000289 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Akira Hatanaka79aed152011-12-20 23:40:56 +0000290 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000291 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Akira Hatanaka79aed152011-12-20 23:40:56 +0000292 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000293 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Eric Christopher1c29a652014-07-18 22:55:25 +0000294 if (Subtarget.hasCnMips()) {
Kai Nacke93fe5e82014-03-20 11:51:58 +0000295 setOperationAction(ISD::CTPOP, MVT::i32, Legal);
296 setOperationAction(ISD::CTPOP, MVT::i64, Legal);
297 } else {
298 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
299 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
300 }
Owen Anderson9f944592009-08-11 20:47:22 +0000301 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
Akira Hatanaka410ce9c2011-12-21 00:14:05 +0000302 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000303 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
304 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
305 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
306 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000307 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Akira Hatanaka7ba8a8d2011-09-30 18:51:46 +0000308 setOperationAction(ISD::ROTL, MVT::i64, Expand);
Akira Hatanaka33a25af2012-07-31 20:54:48 +0000309 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
310 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Bruno Cardoso Lopesd47180e2010-12-09 17:32:30 +0000311
Eric Christopher1c29a652014-07-18 22:55:25 +0000312 if (!Subtarget.hasMips32r2())
Bruno Cardoso Lopesd47180e2010-12-09 17:32:30 +0000313 setOperationAction(ISD::ROTR, MVT::i32, Expand);
314
Eric Christopher1c29a652014-07-18 22:55:25 +0000315 if (!Subtarget.hasMips64r2())
Akira Hatanaka7ba8a8d2011-09-30 18:51:46 +0000316 setOperationAction(ISD::ROTR, MVT::i64, Expand);
317
Owen Anderson9f944592009-08-11 20:47:22 +0000318 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes22b69db2011-03-04 18:54:14 +0000319 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000320 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes22b69db2011-03-04 18:54:14 +0000321 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000322 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
323 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000324 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
325 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanakadfb8cda2011-05-23 22:23:58 +0000326 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000327 setOperationAction(ISD::FLOG, MVT::f32, Expand);
328 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
329 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
330 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000331 setOperationAction(ISD::FMA, MVT::f32, Expand);
332 setOperationAction(ISD::FMA, MVT::f64, Expand);
Akira Hatanaka0603ad82012-03-29 18:43:11 +0000333 setOperationAction(ISD::FREM, MVT::f32, Expand);
334 setOperationAction(ISD::FREM, MVT::f64, Expand);
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000335
Akira Hatanakac0b02062013-01-30 00:26:49 +0000336 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
337
Daniel Sanders2b553d42014-08-01 09:17:39 +0000338 setOperationAction(ISD::VASTART, MVT::Other, Custom);
339 setOperationAction(ISD::VAARG, MVT::Other, Custom);
Bruno Cardoso Lopes048ffab2011-03-09 19:22:22 +0000340 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
341 setOperationAction(ISD::VAEND, MVT::Other, Expand);
342
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000343 // Use the default for now
Owen Anderson9f944592009-08-11 20:47:22 +0000344 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
345 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman26a48482011-07-27 22:21:52 +0000346
Jia Liuf54f60f2012-02-28 07:46:26 +0000347 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
348 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
349 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
350 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
Eli Friedman7dfa7912011-08-29 18:23:02 +0000351
Eli Friedman30a49e92011-08-03 21:06:02 +0000352 setInsertFencesForAtomic(true);
353
Eric Christopher1c29a652014-07-18 22:55:25 +0000354 if (!Subtarget.hasMips32r2()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000355 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
356 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000357 }
358
Daniel Sanders070fd1c2014-05-12 12:41:59 +0000359 // MIPS16 lacks MIPS32's clz and clo instructions.
Eric Christopher1c29a652014-07-18 22:55:25 +0000360 if (!Subtarget.hasMips32() || Subtarget.inMips16Mode())
Owen Anderson9f944592009-08-11 20:47:22 +0000361 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Eric Christopher1c29a652014-07-18 22:55:25 +0000362 if (!Subtarget.hasMips64())
Akira Hatanaka1d8efab2011-12-21 00:20:27 +0000363 setOperationAction(ISD::CTLZ, MVT::i64, Expand);
Bruno Cardoso Lopes93da7e62008-08-08 06:16:31 +0000364
Eric Christopher1c29a652014-07-18 22:55:25 +0000365 if (!Subtarget.hasMips32r2())
Owen Anderson9f944592009-08-11 20:47:22 +0000366 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Eric Christopher1c29a652014-07-18 22:55:25 +0000367 if (!Subtarget.hasMips64r2())
Akira Hatanaka4706ac92011-12-20 23:56:43 +0000368 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000369
Eric Christopher1c29a652014-07-18 22:55:25 +0000370 if (Subtarget.isGP64bit()) {
Akira Hatanaka019e5922012-06-02 00:04:42 +0000371 setLoadExtAction(ISD::SEXTLOAD, MVT::i32, Custom);
372 setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, Custom);
373 setLoadExtAction(ISD::EXTLOAD, MVT::i32, Custom);
374 setTruncStoreAction(MVT::i64, MVT::i32, Custom);
375 }
376
Akira Hatanakaa3d9ab92013-07-26 20:58:55 +0000377 setOperationAction(ISD::TRAP, MVT::Other, Legal);
378
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000379 setTargetDAGCombine(ISD::SDIVREM);
380 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka5e152182012-03-08 03:26:37 +0000381 setTargetDAGCombine(ISD::SELECT);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000382 setTargetDAGCombine(ISD::AND);
383 setTargetDAGCombine(ISD::OR);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000384 setTargetDAGCombine(ISD::ADD);
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000385
Eric Christopher1c29a652014-07-18 22:55:25 +0000386 setMinFunctionAlignment(Subtarget.isGP64bit() ? 3 : 2);
Eli Friedman2518f832011-05-06 20:34:06 +0000387
Daniel Sanders2b553d42014-08-01 09:17:39 +0000388 // The arguments on the stack are defined in terms of 4-byte slots on O32
389 // and 8-byte slots on N32/N64.
390 setMinStackArgumentAlignment(
391 (Subtarget.isABI_N32() || Subtarget.isABI_N64()) ? 8 : 4);
392
Eric Christopher1c29a652014-07-18 22:55:25 +0000393 setStackPointerRegisterToSaveRestore(Subtarget.isABI_N64() ? Mips::SP_64
394 : Mips::SP);
Akira Hatanakaaa560002011-05-26 18:59:03 +0000395
Eric Christopher1c29a652014-07-18 22:55:25 +0000396 setExceptionPointerRegister(Subtarget.isABI_N64() ? Mips::A0_64 : Mips::A0);
397 setExceptionSelectorRegister(Subtarget.isABI_N64() ? Mips::A1_64 : Mips::A1);
Akira Hatanaka1daf8c22012-06-13 19:33:32 +0000398
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000399 MaxStoresPerMemcpy = 16;
Zoran Jovanovicff9d5f32013-12-19 16:12:56 +0000400
Eric Christopher1c29a652014-07-18 22:55:25 +0000401 isMicroMips = Subtarget.inMicroMipsMode();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000402}
403
Eric Christopherb1526602014-09-19 23:30:42 +0000404const MipsTargetLowering *MipsTargetLowering::create(const MipsTargetMachine &TM,
Eric Christopher8924d272014-07-18 23:25:04 +0000405 const MipsSubtarget &STI) {
406 if (STI.inMips16Mode())
407 return llvm::createMips16TargetLowering(TM, STI);
Jia Liuf54f60f2012-02-28 07:46:26 +0000408
Eric Christopher8924d272014-07-18 23:25:04 +0000409 return llvm::createMipsSETargetLowering(TM, STI);
Akira Hatanaka2fcc1cf2011-08-12 21:30:06 +0000410}
411
Reed Kotler720c5ca2014-04-17 22:15:34 +0000412// Create a fast isel object.
413FastISel *
414MipsTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
415 const TargetLibraryInfo *libInfo) const {
416 if (!EnableMipsFastISel)
417 return TargetLowering::createFastISel(funcInfo, libInfo);
418 return Mips::createFastISel(funcInfo, libInfo);
419}
420
Matt Arsenault758659232013-05-18 00:21:46 +0000421EVT MipsTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Akira Hatanakab13b3332013-01-04 20:06:01 +0000422 if (!VT.isVector())
423 return MVT::i32;
424 return VT.changeVectorElementTypeToInteger();
Scott Michela6729e82008-03-10 15:42:14 +0000425}
426
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000427static SDValue performDivRemCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000428 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000429 const MipsSubtarget &Subtarget) {
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000430 if (DCI.isBeforeLegalizeOps())
431 return SDValue();
432
Akira Hatanakab1538f92011-10-03 21:06:13 +0000433 EVT Ty = N->getValueType(0);
Akira Hatanaka8002a3f2013-08-14 00:47:08 +0000434 unsigned LO = (Ty == MVT::i32) ? Mips::LO0 : Mips::LO0_64;
435 unsigned HI = (Ty == MVT::i32) ? Mips::HI0 : Mips::HI0_64;
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000436 unsigned Opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem16 :
437 MipsISD::DivRemU16;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000438 SDLoc DL(N);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000439
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000440 SDValue DivRem = DAG.getNode(Opc, DL, MVT::Glue,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000441 N->getOperand(0), N->getOperand(1));
442 SDValue InChain = DAG.getEntryNode();
443 SDValue InGlue = DivRem;
444
445 // insert MFLO
446 if (N->hasAnyUseOfValue(0)) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000447 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, DL, LO, Ty,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000448 InGlue);
449 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
450 InChain = CopyFromLo.getValue(1);
451 InGlue = CopyFromLo.getValue(2);
452 }
453
454 // insert MFHI
455 if (N->hasAnyUseOfValue(1)) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000456 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, DL,
Akira Hatanakab1538f92011-10-03 21:06:13 +0000457 HI, Ty, InGlue);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000458 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
459 }
460
461 return SDValue();
462}
463
Akira Hatanaka89af5892013-04-18 01:00:46 +0000464static Mips::CondCode condCodeToFCC(ISD::CondCode CC) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000465 switch (CC) {
466 default: llvm_unreachable("Unknown fp condition code!");
467 case ISD::SETEQ:
468 case ISD::SETOEQ: return Mips::FCOND_OEQ;
469 case ISD::SETUNE: return Mips::FCOND_UNE;
470 case ISD::SETLT:
471 case ISD::SETOLT: return Mips::FCOND_OLT;
472 case ISD::SETGT:
473 case ISD::SETOGT: return Mips::FCOND_OGT;
474 case ISD::SETLE:
475 case ISD::SETOLE: return Mips::FCOND_OLE;
476 case ISD::SETGE:
477 case ISD::SETOGE: return Mips::FCOND_OGE;
478 case ISD::SETULT: return Mips::FCOND_ULT;
479 case ISD::SETULE: return Mips::FCOND_ULE;
480 case ISD::SETUGT: return Mips::FCOND_UGT;
481 case ISD::SETUGE: return Mips::FCOND_UGE;
482 case ISD::SETUO: return Mips::FCOND_UN;
483 case ISD::SETO: return Mips::FCOND_OR;
484 case ISD::SETNE:
485 case ISD::SETONE: return Mips::FCOND_ONE;
486 case ISD::SETUEQ: return Mips::FCOND_UEQ;
487 }
488}
489
490
Akira Hatanakaf0ea5002013-03-30 01:16:38 +0000491/// This function returns true if the floating point conditional branches and
492/// conditional moves which use condition code CC should be inverted.
493static bool invertFPCondCodeUser(Mips::CondCode CC) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000494 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
495 return false;
496
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000497 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
498 "Illegal Condition Code");
Akira Hatanakaa5352702011-03-31 18:26:17 +0000499
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000500 return true;
Akira Hatanakaa5352702011-03-31 18:26:17 +0000501}
502
503// Creates and returns an FPCmp node from a setcc node.
504// Returns Op if setcc is not a floating point comparison.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000505static SDValue createFPCmp(SelectionDAG &DAG, const SDValue &Op) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000506 // must be a SETCC node
507 if (Op.getOpcode() != ISD::SETCC)
508 return Op;
509
510 SDValue LHS = Op.getOperand(0);
511
512 if (!LHS.getValueType().isFloatingPoint())
513 return Op;
514
515 SDValue RHS = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000516 SDLoc DL(Op);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000517
Akira Hatanakaaef55c82011-04-15 21:00:26 +0000518 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
519 // node if necessary.
Akira Hatanakaa5352702011-03-31 18:26:17 +0000520 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
521
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000522 return DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS,
Akira Hatanaka89af5892013-04-18 01:00:46 +0000523 DAG.getConstant(condCodeToFCC(CC), MVT::i32));
Akira Hatanakaa5352702011-03-31 18:26:17 +0000524}
525
526// Creates and returns a CMovFPT/F node.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000527static SDValue createCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000528 SDValue False, SDLoc DL) {
Akira Hatanakaf0ea5002013-03-30 01:16:38 +0000529 ConstantSDNode *CC = cast<ConstantSDNode>(Cond.getOperand(2));
530 bool invert = invertFPCondCodeUser((Mips::CondCode)CC->getSExtValue());
Akira Hatanaka8bce21c2013-07-26 20:51:20 +0000531 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000532
533 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
Akira Hatanaka8bce21c2013-07-26 20:51:20 +0000534 True.getValueType(), True, FCC0, False, Cond);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000535}
536
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000537static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000538 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000539 const MipsSubtarget &Subtarget) {
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000540 if (DCI.isBeforeLegalizeOps())
541 return SDValue();
542
543 SDValue SetCC = N->getOperand(0);
544
545 if ((SetCC.getOpcode() != ISD::SETCC) ||
546 !SetCC.getOperand(0).getValueType().isInteger())
547 return SDValue();
548
549 SDValue False = N->getOperand(2);
550 EVT FalseTy = False.getValueType();
551
552 if (!FalseTy.isInteger())
553 return SDValue();
554
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000555 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(False);
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000556
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000557 // If the RHS (False) is 0, we swap the order of the operands
558 // of ISD::SELECT (obviously also inverting the condition) so that we can
559 // take advantage of conditional moves using the $0 register.
560 // Example:
561 // return (a != 0) ? x : 0;
562 // load $reg, x
563 // movz $reg, $0, a
564 if (!FalseC)
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000565 return SDValue();
566
Andrew Trickef9de2a2013-05-25 02:42:55 +0000567 const SDLoc DL(N);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000568
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000569 if (!FalseC->getZExtValue()) {
570 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
571 SDValue True = N->getOperand(1);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000572
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000573 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
574 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
575
576 return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True);
577 }
578
Matheus Almeidaa6beac12013-12-05 12:07:05 +0000579 // If both operands are integer constants there's a possibility that we
580 // can do some interesting optimizations.
581 SDValue True = N->getOperand(1);
582 ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(True);
583
584 if (!TrueC || !True.getValueType().isInteger())
585 return SDValue();
586
587 // We'll also ignore MVT::i64 operands as this optimizations proves
588 // to be ineffective because of the required sign extensions as the result
589 // of a SETCC operator is always MVT::i32 for non-vector types.
590 if (True.getValueType() == MVT::i64)
591 return SDValue();
592
593 int64_t Diff = TrueC->getSExtValue() - FalseC->getSExtValue();
594
595 // 1) (a < x) ? y : y-1
596 // slti $reg1, a, x
597 // addiu $reg2, $reg1, y-1
598 if (Diff == 1)
599 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, False);
600
601 // 2) (a < x) ? y-1 : y
602 // slti $reg1, a, x
603 // xor $reg1, $reg1, 1
604 // addiu $reg2, $reg1, y-1
605 if (Diff == -1) {
606 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
607 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
608 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
609 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, True);
610 }
611
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000612 // Couldn't optimize.
613 return SDValue();
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000614}
615
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000616static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000617 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000618 const MipsSubtarget &Subtarget) {
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000619 // Pattern match EXT.
620 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
621 // => ext $dst, $src, size, pos
Eric Christopher1c29a652014-07-18 22:55:25 +0000622 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasExtractInsert())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000623 return SDValue();
624
625 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000626 unsigned ShiftRightOpc = ShiftRight.getOpcode();
627
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000628 // Op's first operand must be a shift right.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000629 if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL)
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000630 return SDValue();
631
632 // The second operand of the shift must be an immediate.
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000633 ConstantSDNode *CN;
634 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
635 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000636
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000637 uint64_t Pos = CN->getZExtValue();
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000638 uint64_t SMPos, SMSize;
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000639
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000640 // Op's second operand must be a shifted mask.
641 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000642 !isShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000643 return SDValue();
644
645 // Return if the shifted mask does not start at bit 0 or the sum of its size
646 // and Pos exceeds the word's size.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000647 EVT ValTy = N->getValueType(0);
648 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000649 return SDValue();
650
Andrew Trickef9de2a2013-05-25 02:42:55 +0000651 return DAG.getNode(MipsISD::Ext, SDLoc(N), ValTy,
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000652 ShiftRight.getOperand(0), DAG.getConstant(Pos, MVT::i32),
Akira Hatanakaeea541c2011-08-17 22:59:46 +0000653 DAG.getConstant(SMSize, MVT::i32));
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000654}
Jia Liuf54f60f2012-02-28 07:46:26 +0000655
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000656static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000657 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000658 const MipsSubtarget &Subtarget) {
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000659 // Pattern match INS.
660 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
Jia Liuf54f60f2012-02-28 07:46:26 +0000661 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000662 // => ins $dst, $src, size, pos, $src1
Eric Christopher1c29a652014-07-18 22:55:25 +0000663 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasExtractInsert())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000664 return SDValue();
665
666 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
667 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
668 ConstantSDNode *CN;
669
670 // See if Op's first operand matches (and $src1 , mask0).
671 if (And0.getOpcode() != ISD::AND)
672 return SDValue();
673
674 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000675 !isShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000676 return SDValue();
677
678 // See if Op's second operand matches (and (shl $src, pos), mask1).
679 if (And1.getOpcode() != ISD::AND)
680 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000681
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000682 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000683 !isShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000684 return SDValue();
685
686 // The shift masks must have the same position and size.
687 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
688 return SDValue();
689
690 SDValue Shl = And1.getOperand(0);
691 if (Shl.getOpcode() != ISD::SHL)
692 return SDValue();
693
694 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
695 return SDValue();
696
697 unsigned Shamt = CN->getZExtValue();
698
699 // Return if the shift amount and the first bit position of mask are not the
Jia Liuf54f60f2012-02-28 07:46:26 +0000700 // same.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000701 EVT ValTy = N->getValueType(0);
702 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000703 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000704
Andrew Trickef9de2a2013-05-25 02:42:55 +0000705 return DAG.getNode(MipsISD::Ins, SDLoc(N), ValTy, Shl.getOperand(0),
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000706 DAG.getConstant(SMPos0, MVT::i32),
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000707 DAG.getConstant(SMSize0, MVT::i32), And0.getOperand(0));
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000708}
Jia Liuf54f60f2012-02-28 07:46:26 +0000709
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000710static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000711 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000712 const MipsSubtarget &Subtarget) {
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000713 // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt))
714
715 if (DCI.isBeforeLegalizeOps())
716 return SDValue();
717
718 SDValue Add = N->getOperand(1);
719
720 if (Add.getOpcode() != ISD::ADD)
721 return SDValue();
722
723 SDValue Lo = Add.getOperand(1);
724
725 if ((Lo.getOpcode() != MipsISD::Lo) ||
726 (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable))
727 return SDValue();
728
729 EVT ValTy = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000730 SDLoc DL(N);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000731
732 SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, N->getOperand(0),
733 Add.getOperand(0));
734 return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo);
735}
736
Bruno Cardoso Lopes61a61e92011-02-10 18:05:10 +0000737SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000738 const {
739 SelectionDAG &DAG = DCI.DAG;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000740 unsigned Opc = N->getOpcode();
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000741
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000742 switch (Opc) {
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000743 default: break;
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000744 case ISD::SDIVREM:
745 case ISD::UDIVREM:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000746 return performDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000747 case ISD::SELECT:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000748 return performSELECTCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000749 case ISD::AND:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000750 return performANDCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000751 case ISD::OR:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000752 return performORCombine(N, DAG, DCI, Subtarget);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000753 case ISD::ADD:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000754 return performADDCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000755 }
756
757 return SDValue();
758}
759
Akira Hatanakafabb8cf2012-09-21 23:58:31 +0000760void
761MipsTargetLowering::LowerOperationWrapper(SDNode *N,
762 SmallVectorImpl<SDValue> &Results,
763 SelectionDAG &DAG) const {
764 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
765
766 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
767 Results.push_back(Res.getValue(I));
768}
769
770void
771MipsTargetLowering::ReplaceNodeResults(SDNode *N,
772 SmallVectorImpl<SDValue> &Results,
773 SelectionDAG &DAG) const {
Akira Hatanaka9da442f2013-04-30 21:17:07 +0000774 return LowerOperationWrapper(N, Results, DAG);
Akira Hatanakafabb8cf2012-09-21 23:58:31 +0000775}
776
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000777SDValue MipsTargetLowering::
Dan Gohman21cea8a2010-04-17 15:26:15 +0000778LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000779{
Wesley Peck527da1b2010-11-23 03:31:01 +0000780 switch (Op.getOpcode())
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000781 {
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000782 case ISD::BR_JT: return lowerBR_JT(Op, DAG);
783 case ISD::BRCOND: return lowerBRCOND(Op, DAG);
784 case ISD::ConstantPool: return lowerConstantPool(Op, DAG);
785 case ISD::GlobalAddress: return lowerGlobalAddress(Op, DAG);
786 case ISD::BlockAddress: return lowerBlockAddress(Op, DAG);
787 case ISD::GlobalTLSAddress: return lowerGlobalTLSAddress(Op, DAG);
788 case ISD::JumpTable: return lowerJumpTable(Op, DAG);
789 case ISD::SELECT: return lowerSELECT(Op, DAG);
790 case ISD::SELECT_CC: return lowerSELECT_CC(Op, DAG);
791 case ISD::SETCC: return lowerSETCC(Op, DAG);
792 case ISD::VASTART: return lowerVASTART(Op, DAG);
Daniel Sanders2b553d42014-08-01 09:17:39 +0000793 case ISD::VAARG: return lowerVAARG(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000794 case ISD::FCOPYSIGN: return lowerFCOPYSIGN(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000795 case ISD::FRAMEADDR: return lowerFRAMEADDR(Op, DAG);
796 case ISD::RETURNADDR: return lowerRETURNADDR(Op, DAG);
797 case ISD::EH_RETURN: return lowerEH_RETURN(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000798 case ISD::ATOMIC_FENCE: return lowerATOMIC_FENCE(Op, DAG);
799 case ISD::SHL_PARTS: return lowerShiftLeftParts(Op, DAG);
800 case ISD::SRA_PARTS: return lowerShiftRightParts(Op, DAG, true);
801 case ISD::SRL_PARTS: return lowerShiftRightParts(Op, DAG, false);
802 case ISD::LOAD: return lowerLOAD(Op, DAG);
803 case ISD::STORE: return lowerSTORE(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000804 case ISD::ADD: return lowerADD(Op, DAG);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000805 case ISD::FP_TO_SINT: return lowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000806 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000807 return SDValue();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000808}
809
Akira Hatanakae2489122011-04-15 21:51:11 +0000810//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000811// Lower helper functions
Akira Hatanakae2489122011-04-15 21:51:11 +0000812//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000813
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000814// addLiveIn - This helper function adds the specified physical register to the
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000815// MachineFunction as a live in value. It also creates a corresponding
816// virtual register for it.
817static unsigned
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000818addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000819{
Chris Lattnera10fff52007-12-31 04:13:23 +0000820 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
821 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000822 return VReg;
823}
824
Daniel Sanders308181e2014-06-12 10:44:10 +0000825static MachineBasicBlock *insertDivByZeroTrap(MachineInstr *MI,
826 MachineBasicBlock &MBB,
827 const TargetInstrInfo &TII,
828 bool Is64Bit) {
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000829 if (NoZeroDivCheck)
830 return &MBB;
831
832 // Insert instruction "teq $divisor_reg, $zero, 7".
833 MachineBasicBlock::iterator I(MI);
834 MachineInstrBuilder MIB;
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000835 MachineOperand &Divisor = MI->getOperand(2);
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000836 MIB = BuildMI(MBB, std::next(I), MI->getDebugLoc(), TII.get(Mips::TEQ))
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000837 .addReg(Divisor.getReg(), getKillRegState(Divisor.isKill()))
838 .addReg(Mips::ZERO).addImm(7);
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000839
840 // Use the 32-bit sub-register if this is a 64-bit division.
841 if (Is64Bit)
842 MIB->getOperand(0).setSubReg(Mips::sub_32);
843
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000844 // Clear Divisor's kill flag.
845 Divisor.setIsKill(false);
Daniel Sanders308181e2014-06-12 10:44:10 +0000846
847 // We would normally delete the original instruction here but in this case
848 // we only needed to inject an additional instruction rather than replace it.
849
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000850 return &MBB;
851}
852
Akira Hatanakae4bd0542012-09-27 02:15:57 +0000853MachineBasicBlock *
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +0000854MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +0000855 MachineBasicBlock *BB) const {
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +0000856 switch (MI->getOpcode()) {
Reed Kotler97ba5f22013-02-21 04:22:38 +0000857 default:
858 llvm_unreachable("Unexpected instr type to insert");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000859 case Mips::ATOMIC_LOAD_ADD_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000860 return emitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000861 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000862 return emitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000863 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000864 return emitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000865 case Mips::ATOMIC_LOAD_ADD_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000866 return emitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000867
868 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000869 return emitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000870 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000871 return emitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000872 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000873 return emitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000874 case Mips::ATOMIC_LOAD_AND_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000875 return emitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000876
877 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000878 return emitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000879 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000880 return emitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000881 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000882 return emitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000883 case Mips::ATOMIC_LOAD_OR_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000884 return emitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000885
886 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000887 return emitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000888 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000889 return emitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000890 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000891 return emitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000892 case Mips::ATOMIC_LOAD_XOR_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000893 return emitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000894
895 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000896 return emitAtomicBinaryPartword(MI, BB, 1, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000897 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000898 return emitAtomicBinaryPartword(MI, BB, 2, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000899 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000900 return emitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000901 case Mips::ATOMIC_LOAD_NAND_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000902 return emitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000903
904 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000905 return emitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000906 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000907 return emitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000908 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000909 return emitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000910 case Mips::ATOMIC_LOAD_SUB_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000911 return emitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000912
913 case Mips::ATOMIC_SWAP_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000914 return emitAtomicBinaryPartword(MI, BB, 1, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000915 case Mips::ATOMIC_SWAP_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000916 return emitAtomicBinaryPartword(MI, BB, 2, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000917 case Mips::ATOMIC_SWAP_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000918 return emitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000919 case Mips::ATOMIC_SWAP_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000920 return emitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000921
922 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000923 return emitAtomicCmpSwapPartword(MI, BB, 1);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000924 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000925 return emitAtomicCmpSwapPartword(MI, BB, 2);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000926 case Mips::ATOMIC_CMP_SWAP_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000927 return emitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000928 case Mips::ATOMIC_CMP_SWAP_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000929 return emitAtomicCmpSwap(MI, BB, 8);
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000930 case Mips::PseudoSDIV:
931 case Mips::PseudoUDIV:
Daniel Sanders308181e2014-06-12 10:44:10 +0000932 case Mips::DIV:
933 case Mips::DIVU:
934 case Mips::MOD:
935 case Mips::MODU:
Eric Christopherd9134482014-08-04 21:25:23 +0000936 return insertDivByZeroTrap(
937 MI, *BB, *getTargetMachine().getSubtargetImpl()->getInstrInfo(), false);
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000938 case Mips::PseudoDSDIV:
939 case Mips::PseudoDUDIV:
Daniel Sanders308181e2014-06-12 10:44:10 +0000940 case Mips::DDIV:
941 case Mips::DDIVU:
942 case Mips::DMOD:
943 case Mips::DMODU:
Eric Christopherd9134482014-08-04 21:25:23 +0000944 return insertDivByZeroTrap(
945 MI, *BB, *getTargetMachine().getSubtargetImpl()->getInstrInfo(), true);
Daniel Sanders0fa60412014-06-12 13:39:06 +0000946 case Mips::SEL_D:
947 return emitSEL_D(MI, BB);
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +0000948
949 case Mips::PseudoSELECT_I:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +0000950 case Mips::PseudoSELECT_I64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +0000951 case Mips::PseudoSELECT_S:
952 case Mips::PseudoSELECT_D32:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +0000953 case Mips::PseudoSELECT_D64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +0000954 return emitPseudoSELECT(MI, BB, false, Mips::BNE);
955 case Mips::PseudoSELECTFP_F_I:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +0000956 case Mips::PseudoSELECTFP_F_I64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +0000957 case Mips::PseudoSELECTFP_F_S:
958 case Mips::PseudoSELECTFP_F_D32:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +0000959 case Mips::PseudoSELECTFP_F_D64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +0000960 return emitPseudoSELECT(MI, BB, true, Mips::BC1F);
961 case Mips::PseudoSELECTFP_T_I:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +0000962 case Mips::PseudoSELECTFP_T_I64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +0000963 case Mips::PseudoSELECTFP_T_S:
964 case Mips::PseudoSELECTFP_T_D32:
Vasileios Kalintiris8edbcad2014-12-12 15:16:46 +0000965 case Mips::PseudoSELECTFP_T_D64:
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +0000966 return emitPseudoSELECT(MI, BB, true, Mips::BC1T);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000967 }
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +0000968}
969
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000970// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
971// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
972MachineBasicBlock *
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000973MipsTargetLowering::emitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher0713a9d2011-06-08 23:55:35 +0000974 unsigned Size, unsigned BinOpcode,
Akira Hatanaka15506782011-06-07 18:58:42 +0000975 bool Nand) const {
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000976 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000977
978 MachineFunction *MF = BB->getParent();
979 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000980 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Eric Christopherd9134482014-08-04 21:25:23 +0000981 const TargetInstrInfo *TII =
982 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000983 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000984 unsigned LL, SC, AND, NOR, ZERO, BEQ;
985
986 if (Size == 4) {
Daniel Sanders6a803f62014-06-16 13:13:03 +0000987 if (isMicroMips) {
988 LL = Mips::LL_MM;
989 SC = Mips::SC_MM;
990 } else {
Daniel Sandersbdcfab12014-07-24 09:47:14 +0000991 LL = Subtarget.hasMips32r6() ? Mips::LL_R6 : Mips::LL;
992 SC = Subtarget.hasMips32r6() ? Mips::SC_R6 : Mips::SC;
Daniel Sanders6a803f62014-06-16 13:13:03 +0000993 }
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000994 AND = Mips::AND;
995 NOR = Mips::NOR;
996 ZERO = Mips::ZERO;
997 BEQ = Mips::BEQ;
Daniel Sanders6a803f62014-06-16 13:13:03 +0000998 } else {
Daniel Sandersbdcfab12014-07-24 09:47:14 +0000999 LL = Subtarget.hasMips64r6() ? Mips::LLD_R6 : Mips::LLD;
1000 SC = Subtarget.hasMips64r6() ? Mips::SCD_R6 : Mips::SCD;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001001 AND = Mips::AND64;
1002 NOR = Mips::NOR64;
1003 ZERO = Mips::ZERO_64;
1004 BEQ = Mips::BEQ64;
1005 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001006
Akira Hatanaka0e019592011-07-19 20:11:17 +00001007 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001008 unsigned Ptr = MI->getOperand(1).getReg();
1009 unsigned Incr = MI->getOperand(2).getReg();
1010
Akira Hatanaka0e019592011-07-19 20:11:17 +00001011 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1012 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1013 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001014
1015 // insert new blocks after the current block
1016 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1017 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1018 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1019 MachineFunction::iterator It = BB;
1020 ++It;
1021 MF->insert(It, loopMBB);
1022 MF->insert(It, exitMBB);
1023
1024 // Transfer the remainder of BB and its successor edges to exitMBB.
1025 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001026 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001027 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1028
1029 // thisMBB:
1030 // ...
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001031 // fallthrough --> loopMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001032 BB->addSuccessor(loopMBB);
Akira Hatanaka08636b42011-07-19 17:09:53 +00001033 loopMBB->addSuccessor(loopMBB);
1034 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001035
1036 // loopMBB:
1037 // ll oldval, 0(ptr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001038 // <binop> storeval, oldval, incr
1039 // sc success, storeval, 0(ptr)
1040 // beq success, $0, loopMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001041 BB = loopMBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001042 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001043 if (Nand) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001044 // and andres, oldval, incr
1045 // nor storeval, $0, andres
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001046 BuildMI(BB, DL, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
1047 BuildMI(BB, DL, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001048 } else if (BinOpcode) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001049 // <binop> storeval, oldval, incr
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001050 BuildMI(BB, DL, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001051 } else {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001052 StoreVal = Incr;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001053 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001054 BuildMI(BB, DL, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
1055 BuildMI(BB, DL, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001056
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001057 MI->eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001058
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001059 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001060}
1061
Daniel Sanders6a803f62014-06-16 13:13:03 +00001062MachineBasicBlock *MipsTargetLowering::emitSignExtendToI32InReg(
1063 MachineInstr *MI, MachineBasicBlock *BB, unsigned Size, unsigned DstReg,
1064 unsigned SrcReg) const {
Eric Christopherd9134482014-08-04 21:25:23 +00001065 const TargetInstrInfo *TII =
1066 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Daniel Sanders6a803f62014-06-16 13:13:03 +00001067 DebugLoc DL = MI->getDebugLoc();
1068
Eric Christopher1c29a652014-07-18 22:55:25 +00001069 if (Subtarget.hasMips32r2() && Size == 1) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001070 BuildMI(BB, DL, TII->get(Mips::SEB), DstReg).addReg(SrcReg);
1071 return BB;
1072 }
1073
Eric Christopher1c29a652014-07-18 22:55:25 +00001074 if (Subtarget.hasMips32r2() && Size == 2) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001075 BuildMI(BB, DL, TII->get(Mips::SEH), DstReg).addReg(SrcReg);
1076 return BB;
1077 }
1078
1079 MachineFunction *MF = BB->getParent();
1080 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1081 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1082 unsigned ScrReg = RegInfo.createVirtualRegister(RC);
1083
1084 assert(Size < 32);
1085 int64_t ShiftImm = 32 - (Size * 8);
1086
1087 BuildMI(BB, DL, TII->get(Mips::SLL), ScrReg).addReg(SrcReg).addImm(ShiftImm);
1088 BuildMI(BB, DL, TII->get(Mips::SRA), DstReg).addReg(ScrReg).addImm(ShiftImm);
1089
1090 return BB;
1091}
1092
1093MachineBasicBlock *MipsTargetLowering::emitAtomicBinaryPartword(
1094 MachineInstr *MI, MachineBasicBlock *BB, unsigned Size, unsigned BinOpcode,
1095 bool Nand) const {
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001096 assert((Size == 1 || Size == 2) &&
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001097 "Unsupported size for EmitAtomicBinaryPartial.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001098
1099 MachineFunction *MF = BB->getParent();
1100 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1101 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
Eric Christopherd9134482014-08-04 21:25:23 +00001102 const TargetInstrInfo *TII =
1103 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001104 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001105
1106 unsigned Dest = MI->getOperand(0).getReg();
1107 unsigned Ptr = MI->getOperand(1).getReg();
1108 unsigned Incr = MI->getOperand(2).getReg();
1109
Akira Hatanaka0e019592011-07-19 20:11:17 +00001110 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1111 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001112 unsigned Mask = RegInfo.createVirtualRegister(RC);
1113 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001114 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1115 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001116 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001117 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1118 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1119 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1120 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1121 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka9663dd32011-07-19 20:56:53 +00001122 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001123 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1124 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1125 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001126 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001127
1128 // insert new blocks after the current block
1129 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1130 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001131 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001132 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1133 MachineFunction::iterator It = BB;
1134 ++It;
1135 MF->insert(It, loopMBB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001136 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001137 MF->insert(It, exitMBB);
1138
1139 // Transfer the remainder of BB and its successor edges to exitMBB.
1140 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001141 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001142 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1143
Akira Hatanaka08636b42011-07-19 17:09:53 +00001144 BB->addSuccessor(loopMBB);
1145 loopMBB->addSuccessor(loopMBB);
1146 loopMBB->addSuccessor(sinkMBB);
1147 sinkMBB->addSuccessor(exitMBB);
1148
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001149 // thisMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001150 // addiu masklsb2,$0,-4 # 0xfffffffc
1151 // and alignedaddr,ptr,masklsb2
1152 // andi ptrlsb2,ptr,3
1153 // sll shiftamt,ptrlsb2,3
1154 // ori maskupper,$0,255 # 0xff
1155 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001156 // nor mask2,$0,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001157 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001158
1159 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001160 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001161 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001162 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001163 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001164 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Eric Christopher1c29a652014-07-18 22:55:25 +00001165 if (Subtarget.isLittle()) {
Akira Hatanaka2bf97332013-05-31 03:25:44 +00001166 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1167 } else {
1168 unsigned Off = RegInfo.createVirtualRegister(RC);
1169 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1170 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1171 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1172 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001173 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001174 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001175 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001176 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001177 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001178 BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(Incr).addReg(ShiftAmt);
Bruno Cardoso Lopesf771a0f2011-05-31 20:25:26 +00001179
Akira Hatanaka27292632011-07-18 18:52:12 +00001180 // atomic.load.binop
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001181 // loopMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001182 // ll oldval,0(alignedaddr)
1183 // binop binopres,oldval,incr2
1184 // and newval,binopres,mask
1185 // and maskedoldval0,oldval,mask2
1186 // or storeval,maskedoldval0,newval
1187 // sc success,storeval,0(alignedaddr)
1188 // beq success,$0,loopMBB
1189
Akira Hatanaka27292632011-07-18 18:52:12 +00001190 // atomic.swap
1191 // loopMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001192 // ll oldval,0(alignedaddr)
Akira Hatanakae4503582011-07-19 18:14:26 +00001193 // and newval,incr2,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001194 // and maskedoldval0,oldval,mask2
1195 // or storeval,maskedoldval0,newval
1196 // sc success,storeval,0(alignedaddr)
1197 // beq success,$0,loopMBB
Akira Hatanaka27292632011-07-18 18:52:12 +00001198
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001199 BB = loopMBB;
Jozef Kolek2f27d572014-12-18 16:39:29 +00001200 unsigned LL = isMicroMips ? Mips::LL_MM : Mips::LL;
1201 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001202 if (Nand) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001203 // and andres, oldval, incr2
1204 // nor binopres, $0, andres
1205 // and newval, binopres, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001206 BuildMI(BB, DL, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1207 BuildMI(BB, DL, TII->get(Mips::NOR), BinOpRes)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001208 .addReg(Mips::ZERO).addReg(AndRes);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001209 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001210 } else if (BinOpcode) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001211 // <binop> binopres, oldval, incr2
1212 // and newval, binopres, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001213 BuildMI(BB, DL, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1214 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001215 } else { // atomic.swap
Akira Hatanaka0e019592011-07-19 20:11:17 +00001216 // and newval, incr2, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001217 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanakae4503582011-07-19 18:14:26 +00001218 }
Jia Liuf54f60f2012-02-28 07:46:26 +00001219
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001220 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001221 .addReg(OldVal).addReg(Mask2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001222 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka9663dd32011-07-19 20:56:53 +00001223 .addReg(MaskedOldVal0).addReg(NewVal);
Jozef Kolek2f27d572014-12-18 16:39:29 +00001224 unsigned SC = isMicroMips ? Mips::SC_MM : Mips::SC;
1225 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001226 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001227 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001228 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001229
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001230 // sinkMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001231 // and maskedoldval1,oldval,mask
1232 // srl srlres,maskedoldval1,shiftamt
Daniel Sanders6a803f62014-06-16 13:13:03 +00001233 // sign_extend dest,srlres
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001234 BB = sinkMBB;
Akira Hatanakae97bd812011-07-19 03:14:58 +00001235
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001236 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001237 .addReg(OldVal).addReg(Mask);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001238 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001239 .addReg(MaskedOldVal1).addReg(ShiftAmt);
Daniel Sanders6a803f62014-06-16 13:13:03 +00001240 BB = emitSignExtendToI32InReg(MI, BB, Size, Dest, SrlRes);
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
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001247MachineBasicBlock * MipsTargetLowering::emitAtomicCmpSwap(MachineInstr *MI,
1248 MachineBasicBlock *BB,
1249 unsigned Size) const {
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001250 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001251
1252 MachineFunction *MF = BB->getParent();
1253 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001254 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Eric Christopherd9134482014-08-04 21:25:23 +00001255 const TargetInstrInfo *TII =
1256 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001257 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001258 unsigned LL, SC, ZERO, BNE, BEQ;
1259
1260 if (Size == 4) {
Zoran Jovanovicff9d5f32013-12-19 16:12:56 +00001261 LL = isMicroMips ? Mips::LL_MM : Mips::LL;
1262 SC = isMicroMips ? Mips::SC_MM : Mips::SC;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001263 ZERO = Mips::ZERO;
1264 BNE = Mips::BNE;
1265 BEQ = Mips::BEQ;
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001266 } else {
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001267 LL = Mips::LLD;
1268 SC = Mips::SCD;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001269 ZERO = Mips::ZERO_64;
1270 BNE = Mips::BNE64;
1271 BEQ = Mips::BEQ64;
1272 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001273
1274 unsigned Dest = MI->getOperand(0).getReg();
1275 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka0e019592011-07-19 20:11:17 +00001276 unsigned OldVal = MI->getOperand(2).getReg();
1277 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001278
Akira Hatanaka0e019592011-07-19 20:11:17 +00001279 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001280
1281 // insert new blocks after the current block
1282 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1283 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1284 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1285 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1286 MachineFunction::iterator It = BB;
1287 ++It;
1288 MF->insert(It, loop1MBB);
1289 MF->insert(It, loop2MBB);
1290 MF->insert(It, exitMBB);
1291
1292 // Transfer the remainder of BB and its successor edges to exitMBB.
1293 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001294 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001295 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1296
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001297 // thisMBB:
1298 // ...
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001299 // fallthrough --> loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001300 BB->addSuccessor(loop1MBB);
Akira Hatanaka08636b42011-07-19 17:09:53 +00001301 loop1MBB->addSuccessor(exitMBB);
1302 loop1MBB->addSuccessor(loop2MBB);
1303 loop2MBB->addSuccessor(loop1MBB);
1304 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001305
1306 // loop1MBB:
1307 // ll dest, 0(ptr)
1308 // bne dest, oldval, exitMBB
1309 BB = loop1MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001310 BuildMI(BB, DL, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1311 BuildMI(BB, DL, TII->get(BNE))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001312 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001313
1314 // loop2MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001315 // sc success, newval, 0(ptr)
1316 // beq success, $0, loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001317 BB = loop2MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001318 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001319 .addReg(NewVal).addReg(Ptr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001320 BuildMI(BB, DL, TII->get(BEQ))
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001321 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001322
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001323 MI->eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001324
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001325 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001326}
1327
1328MachineBasicBlock *
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001329MipsTargetLowering::emitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka15506782011-06-07 18:58:42 +00001330 MachineBasicBlock *BB,
1331 unsigned Size) const {
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001332 assert((Size == 1 || Size == 2) &&
1333 "Unsupported size for EmitAtomicCmpSwapPartial.");
1334
1335 MachineFunction *MF = BB->getParent();
1336 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1337 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
Eric Christopherd9134482014-08-04 21:25:23 +00001338 const TargetInstrInfo *TII =
1339 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001340 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001341
1342 unsigned Dest = MI->getOperand(0).getReg();
1343 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka0e019592011-07-19 20:11:17 +00001344 unsigned CmpVal = MI->getOperand(2).getReg();
1345 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001346
Akira Hatanaka0e019592011-07-19 20:11:17 +00001347 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1348 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001349 unsigned Mask = RegInfo.createVirtualRegister(RC);
1350 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001351 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1352 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1353 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1354 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1355 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1356 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1357 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1358 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1359 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1360 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1361 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1362 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001363 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001364
1365 // insert new blocks after the current block
1366 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1367 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1368 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001369 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001370 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1371 MachineFunction::iterator It = BB;
1372 ++It;
1373 MF->insert(It, loop1MBB);
1374 MF->insert(It, loop2MBB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001375 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001376 MF->insert(It, exitMBB);
1377
1378 // Transfer the remainder of BB and its successor edges to exitMBB.
1379 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001380 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001381 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1382
Akira Hatanaka08636b42011-07-19 17:09:53 +00001383 BB->addSuccessor(loop1MBB);
1384 loop1MBB->addSuccessor(sinkMBB);
1385 loop1MBB->addSuccessor(loop2MBB);
1386 loop2MBB->addSuccessor(loop1MBB);
1387 loop2MBB->addSuccessor(sinkMBB);
1388 sinkMBB->addSuccessor(exitMBB);
1389
Akira Hatanakae4503582011-07-19 18:14:26 +00001390 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001391 // thisMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001392 // addiu masklsb2,$0,-4 # 0xfffffffc
1393 // and alignedaddr,ptr,masklsb2
1394 // andi ptrlsb2,ptr,3
1395 // sll shiftamt,ptrlsb2,3
1396 // ori maskupper,$0,255 # 0xff
1397 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001398 // nor mask2,$0,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001399 // andi maskedcmpval,cmpval,255
1400 // sll shiftedcmpval,maskedcmpval,shiftamt
1401 // andi maskednewval,newval,255
1402 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001403 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001404 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001405 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001406 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001407 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001408 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Eric Christopher1c29a652014-07-18 22:55:25 +00001409 if (Subtarget.isLittle()) {
Akira Hatanaka2bf97332013-05-31 03:25:44 +00001410 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1411 } else {
1412 unsigned Off = RegInfo.createVirtualRegister(RC);
1413 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1414 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1415 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1416 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001417 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001418 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001419 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001420 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001421 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1422 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001423 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001424 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001425 .addReg(MaskedCmpVal).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001426 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001427 .addReg(NewVal).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001428 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001429 .addReg(MaskedNewVal).addReg(ShiftAmt);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001430
1431 // loop1MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001432 // ll oldval,0(alginedaddr)
1433 // and maskedoldval0,oldval,mask
1434 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001435 BB = loop1MBB;
Jozef Kolek2f27d572014-12-18 16:39:29 +00001436 unsigned LL = isMicroMips ? Mips::LL_MM : Mips::LL;
1437 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001438 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001439 .addReg(OldVal).addReg(Mask);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001440 BuildMI(BB, DL, TII->get(Mips::BNE))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001441 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001442
1443 // loop2MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001444 // and maskedoldval1,oldval,mask2
1445 // or storeval,maskedoldval1,shiftednewval
1446 // sc success,storeval,0(alignedaddr)
1447 // beq success,$0,loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001448 BB = loop2MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001449 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001450 .addReg(OldVal).addReg(Mask2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001451 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001452 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
Jozef Kolek2f27d572014-12-18 16:39:29 +00001453 unsigned SC = isMicroMips ? Mips::SC_MM : Mips::SC;
1454 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001455 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001456 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001457 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001458
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001459 // sinkMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001460 // srl srlres,maskedoldval0,shiftamt
Daniel Sanders6a803f62014-06-16 13:13:03 +00001461 // sign_extend dest,srlres
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001462 BB = sinkMBB;
Akira Hatanakae97bd812011-07-19 03:14:58 +00001463
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001464 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001465 .addReg(MaskedOldVal0).addReg(ShiftAmt);
Daniel Sanders6a803f62014-06-16 13:13:03 +00001466 BB = emitSignExtendToI32InReg(MI, BB, Size, Dest, SrlRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001467
1468 MI->eraseFromParent(); // The instruction is gone now.
1469
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001470 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001471}
1472
Daniel Sanders0fa60412014-06-12 13:39:06 +00001473MachineBasicBlock *MipsTargetLowering::emitSEL_D(MachineInstr *MI,
1474 MachineBasicBlock *BB) const {
1475 MachineFunction *MF = BB->getParent();
Eric Christopherd9134482014-08-04 21:25:23 +00001476 const TargetRegisterInfo *TRI =
1477 getTargetMachine().getSubtargetImpl()->getRegisterInfo();
1478 const TargetInstrInfo *TII =
1479 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Daniel Sanders0fa60412014-06-12 13:39:06 +00001480 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1481 DebugLoc DL = MI->getDebugLoc();
1482 MachineBasicBlock::iterator II(MI);
1483
1484 unsigned Fc = MI->getOperand(1).getReg();
1485 const auto &FGR64RegClass = TRI->getRegClass(Mips::FGR64RegClassID);
1486
1487 unsigned Fc2 = RegInfo.createVirtualRegister(FGR64RegClass);
1488
1489 BuildMI(*BB, II, DL, TII->get(Mips::SUBREG_TO_REG), Fc2)
1490 .addImm(0)
1491 .addReg(Fc)
1492 .addImm(Mips::sub_lo);
1493
1494 // We don't erase the original instruction, we just replace the condition
1495 // register with the 64-bit super-register.
1496 MI->getOperand(1).setReg(Fc2);
1497
1498 return BB;
1499}
1500
Akira Hatanakae2489122011-04-15 21:51:11 +00001501//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001502// Misc Lower Operation implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00001503//===----------------------------------------------------------------------===//
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001504SDValue MipsTargetLowering::lowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001505 SDValue Chain = Op.getOperand(0);
1506 SDValue Table = Op.getOperand(1);
1507 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001508 SDLoc DL(Op);
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001509 EVT PTy = getPointerTy();
1510 unsigned EntrySize =
1511 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(*getDataLayout());
1512
1513 Index = DAG.getNode(ISD::MUL, DL, PTy, Index,
1514 DAG.getConstant(EntrySize, PTy));
1515 SDValue Addr = DAG.getNode(ISD::ADD, DL, PTy, Index, Table);
1516
1517 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
1518 Addr = DAG.getExtLoad(ISD::SEXTLOAD, DL, PTy, Chain, Addr,
1519 MachinePointerInfo::getJumpTable(), MemVT, false, false,
Louis Gerbarg67474e32014-07-31 21:45:05 +00001520 false, 0);
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001521 Chain = Addr.getValue(1);
1522
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001523 if ((getTargetMachine().getRelocationModel() == Reloc::PIC_) ||
Eric Christopher1c29a652014-07-18 22:55:25 +00001524 Subtarget.isABI_N64()) {
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001525 // For PIC, the sequence is:
1526 // BRIND(load(Jumptable + index) + RelocBase)
1527 // RelocBase can be JumpTable, GOT or some sort of global base.
1528 Addr = DAG.getNode(ISD::ADD, DL, PTy, Addr,
1529 getPICJumpTableRelocBase(Table, DAG));
1530 }
1531
1532 return DAG.getNode(ISD::BRIND, DL, MVT::Other, Chain, Addr);
1533}
1534
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001535SDValue MipsTargetLowering::lowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Wesley Peck527da1b2010-11-23 03:31:01 +00001536 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001537 // the block to branch to if the condition is true.
1538 SDValue Chain = Op.getOperand(0);
1539 SDValue Dest = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001540 SDLoc DL(Op);
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001541
Eric Christopher1c29a652014-07-18 22:55:25 +00001542 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001543 SDValue CondRes = createFPCmp(DAG, Op.getOperand(1));
Akira Hatanakaa5352702011-03-31 18:26:17 +00001544
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001545 // Return if flag is not set by a floating point comparison.
Akira Hatanakaa5352702011-03-31 18:26:17 +00001546 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopesa9504222008-07-30 17:06:13 +00001547 return Op;
Wesley Peck527da1b2010-11-23 03:31:01 +00001548
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +00001549 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmaneffb8942008-09-12 16:56:44 +00001550 Mips::CondCode CC =
1551 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Akira Hatanakaf0ea5002013-03-30 01:16:38 +00001552 unsigned Opc = invertFPCondCodeUser(CC) ? Mips::BRANCH_F : Mips::BRANCH_T;
1553 SDValue BrCode = DAG.getConstant(Opc, MVT::i32);
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +00001554 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001555 return DAG.getNode(MipsISD::FPBrcond, DL, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +00001556 FCC0, Dest, CondRes);
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001557}
1558
1559SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001560lowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001561{
Eric Christopher1c29a652014-07-18 22:55:25 +00001562 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001563 SDValue Cond = createFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001564
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001565 // Return if flag is not set by a floating point comparison.
Akira Hatanakaa5352702011-03-31 18:26:17 +00001566 if (Cond.getOpcode() != MipsISD::FPCmp)
1567 return Op;
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +00001568
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001569 return createCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
Andrew Trickef9de2a2013-05-25 02:42:55 +00001570 SDLoc(Op));
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001571}
1572
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001573SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001574lowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001575{
Andrew Trickef9de2a2013-05-25 02:42:55 +00001576 SDLoc DL(Op);
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001577 EVT Ty = Op.getOperand(0).getValueType();
Matt Arsenault758659232013-05-18 00:21:46 +00001578 SDValue Cond = DAG.getNode(ISD::SETCC, DL,
1579 getSetCCResultType(*DAG.getContext(), Ty),
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001580 Op.getOperand(0), Op.getOperand(1),
1581 Op.getOperand(4));
1582
1583 return DAG.getNode(ISD::SELECT, DL, Op.getValueType(), Cond, Op.getOperand(2),
1584 Op.getOperand(3));
1585}
1586
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001587SDValue MipsTargetLowering::lowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00001588 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001589 SDValue Cond = createFPCmp(DAG, Op);
Akira Hatanakab7f78592012-03-09 23:46:03 +00001590
1591 assert(Cond.getOpcode() == MipsISD::FPCmp &&
1592 "Floating point operand expected.");
1593
1594 SDValue True = DAG.getConstant(1, MVT::i32);
1595 SDValue False = DAG.getConstant(0, MVT::i32);
1596
Andrew Trickef9de2a2013-05-25 02:42:55 +00001597 return createCMovFP(DAG, Cond, True, False, SDLoc(Op));
Akira Hatanakab7f78592012-03-09 23:46:03 +00001598}
1599
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001600SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001601 SelectionDAG &DAG) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001602 EVT Ty = Op.getValueType();
1603 GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
1604 const GlobalValue *GV = N->getGlobal();
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001605
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001606 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ &&
Eric Christopher1c29a652014-07-18 22:55:25 +00001607 !Subtarget.isABI_N64()) {
Akira Hatanaka92a96e12012-09-12 23:27:55 +00001608 const MipsTargetObjectFile &TLOF =
1609 (const MipsTargetObjectFile&)getObjFileLowering();
Wesley Peck527da1b2010-11-23 03:31:01 +00001610
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001611 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine()))
1612 // %gp_rel relocation
1613 return getAddrGPRel(N, Ty, DAG);
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001614
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001615 // %hi/%lo relocation
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001616 return getAddrNonPIC(N, Ty, DAG);
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001617 }
1618
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001619 if (GV->hasInternalLinkage() || (GV->hasLocalLinkage() && !isa<Function>(GV)))
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001620 return getAddrLocal(N, Ty, DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +00001621 Subtarget.isABI_N32() || Subtarget.isABI_N64());
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001622
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00001623 if (LargeGOT)
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001624 return getAddrGlobalLargeGOT(N, Ty, DAG, MipsII::MO_GOT_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00001625 MipsII::MO_GOT_LO16, DAG.getEntryNode(),
1626 MachinePointerInfo::getGOT());
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00001627
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001628 return getAddrGlobal(N, Ty, DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +00001629 (Subtarget.isABI_N32() || Subtarget.isABI_N64())
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001630 ? MipsII::MO_GOT_DISP
1631 : MipsII::MO_GOT16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00001632 DAG.getEntryNode(), MachinePointerInfo::getGOT());
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001633}
1634
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001635SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op,
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +00001636 SelectionDAG &DAG) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001637 BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op);
1638 EVT Ty = Op.getValueType();
Akira Hatanaka30f97cf2013-09-25 00:30:25 +00001639
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001640 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ &&
Eric Christopher1c29a652014-07-18 22:55:25 +00001641 !Subtarget.isABI_N64())
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001642 return getAddrNonPIC(N, Ty, DAG);
1643
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001644 return getAddrLocal(N, Ty, DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +00001645 Subtarget.isABI_N32() || Subtarget.isABI_N64());
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +00001646}
1647
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001648SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001649lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001650{
Akira Hatanakabff84e12011-12-14 18:26:41 +00001651 // If the relocation model is PIC, use the General Dynamic TLS Model or
1652 // Local Dynamic TLS model, otherwise use the Initial Exec or
1653 // Local Exec TLS Model.
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001654
1655 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001656 SDLoc DL(GA);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001657 const GlobalValue *GV = GA->getGlobal();
1658 EVT PtrVT = getPointerTy();
1659
Hans Wennborgaea41202012-05-04 09:40:39 +00001660 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
1661
1662 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Hans Wennborg245917b2012-06-04 14:02:08 +00001663 // General Dynamic and Local Dynamic TLS Model.
1664 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
1665 : MipsII::MO_TLSGD;
1666
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001667 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag);
1668 SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT,
1669 getGlobalReg(DAG, PtrVT), TGA);
Akira Hatanakaf10ee842011-12-08 21:05:38 +00001670 unsigned PtrSize = PtrVT.getSizeInBits();
1671 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1672
Benjamin Kramer64ba50a2011-12-11 12:21:34 +00001673 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001674
1675 ArgListTy Args;
1676 ArgListEntry Entry;
1677 Entry.Node = Argument;
Akira Hatanakadee6c822011-12-08 20:34:32 +00001678 Entry.Ty = PtrTy;
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001679 Args.push_back(Entry);
Jia Liuf54f60f2012-02-28 07:46:26 +00001680
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00001681 TargetLowering::CallLoweringInfo CLI(DAG);
1682 CLI.setDebugLoc(DL).setChain(DAG.getEntryNode())
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00001683 .setCallee(CallingConv::C, PtrTy, TlsGetAddr, std::move(Args), 0);
Justin Holewinskiaa583972012-05-25 16:35:28 +00001684 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001685
Akira Hatanakabff84e12011-12-14 18:26:41 +00001686 SDValue Ret = CallResult.first;
1687
Hans Wennborgaea41202012-05-04 09:40:39 +00001688 if (model != TLSModel::LocalDynamic)
Akira Hatanakabff84e12011-12-14 18:26:41 +00001689 return Ret;
1690
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001691 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanakabff84e12011-12-14 18:26:41 +00001692 MipsII::MO_DTPREL_HI);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001693 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1694 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanakabff84e12011-12-14 18:26:41 +00001695 MipsII::MO_DTPREL_LO);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001696 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1697 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret);
1698 return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001699 }
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001700
1701 SDValue Offset;
Hans Wennborgaea41202012-05-04 09:40:39 +00001702 if (model == TLSModel::InitialExec) {
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001703 // Initial Exec TLS Model
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001704 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001705 MipsII::MO_GOTTPREL);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001706 TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT),
Akira Hatanakab049aef2012-02-24 22:34:47 +00001707 TGA);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001708 Offset = DAG.getLoad(PtrVT, DL,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001709 DAG.getEntryNode(), TGA, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001710 false, false, false, 0);
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001711 } else {
1712 // Local Exec TLS Model
Hans Wennborgaea41202012-05-04 09:40:39 +00001713 assert(model == TLSModel::LocalExec);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001714 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001715 MipsII::MO_TPREL_HI);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001716 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001717 MipsII::MO_TPREL_LO);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001718 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1719 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1720 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001721 }
1722
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001723 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
1724 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001725}
1726
1727SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001728lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +00001729{
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001730 JumpTableSDNode *N = cast<JumpTableSDNode>(Op);
1731 EVT Ty = Op.getValueType();
Akira Hatanaka30f97cf2013-09-25 00:30:25 +00001732
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001733 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ &&
Eric Christopher1c29a652014-07-18 22:55:25 +00001734 !Subtarget.isABI_N64())
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001735 return getAddrNonPIC(N, Ty, DAG);
1736
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001737 return getAddrLocal(N, Ty, DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +00001738 Subtarget.isABI_N32() || Subtarget.isABI_N64());
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +00001739}
1740
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001741SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001742lowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +00001743{
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001744 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
1745 EVT Ty = Op.getValueType();
Bruno Cardoso Lopes2db07582009-11-25 12:17:58 +00001746
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001747 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ &&
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001748 !Subtarget.isABI_N64()) {
1749 const MipsTargetObjectFile &TLOF =
1750 (const MipsTargetObjectFile&)getObjFileLowering();
1751
1752 if (TLOF.IsConstantInSmallSection(N->getConstVal(), getTargetMachine()))
1753 // %gp_rel relocation
1754 return getAddrGPRel(N, Ty, DAG);
1755
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001756 return getAddrNonPIC(N, Ty, DAG);
Sasa Stankovicb38db1e2014-11-06 13:20:12 +00001757 }
Bruno Cardoso Lopesfdb4cec2008-07-23 16:01:50 +00001758
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001759 return getAddrLocal(N, Ty, DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +00001760 Subtarget.isABI_N32() || Subtarget.isABI_N64());
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +00001761}
1762
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001763SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman31ae5862010-04-17 14:41:14 +00001764 MachineFunction &MF = DAG.getMachineFunction();
1765 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1766
Andrew Trickef9de2a2013-05-25 02:42:55 +00001767 SDLoc DL(Op);
Dan Gohman31ae5862010-04-17 14:41:14 +00001768 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1769 getPointerTy());
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +00001770
1771 // vastart just stores the address of the VarArgsFrameIndex slot into the
1772 // memory location argument.
1773 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001774 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00001775 MachinePointerInfo(SV), false, false, 0);
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +00001776}
Jia Liuf54f60f2012-02-28 07:46:26 +00001777
Daniel Sanders2b553d42014-08-01 09:17:39 +00001778SDValue MipsTargetLowering::lowerVAARG(SDValue Op, SelectionDAG &DAG) const {
1779 SDNode *Node = Op.getNode();
1780 EVT VT = Node->getValueType(0);
1781 SDValue Chain = Node->getOperand(0);
1782 SDValue VAListPtr = Node->getOperand(1);
1783 unsigned Align = Node->getConstantOperandVal(3);
1784 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
1785 SDLoc DL(Node);
1786 unsigned ArgSlotSizeInBytes =
1787 (Subtarget.isABI_N32() || Subtarget.isABI_N64()) ? 8 : 4;
1788
1789 SDValue VAListLoad = DAG.getLoad(getPointerTy(), DL, Chain, VAListPtr,
1790 MachinePointerInfo(SV), false, false, false,
1791 0);
1792 SDValue VAList = VAListLoad;
1793
1794 // Re-align the pointer if necessary.
1795 // It should only ever be necessary for 64-bit types on O32 since the minimum
1796 // argument alignment is the same as the maximum type alignment for N32/N64.
1797 //
1798 // FIXME: We currently align too often. The code generator doesn't notice
1799 // when the pointer is still aligned from the last va_arg (or pair of
1800 // va_args for the i64 on O32 case).
1801 if (Align > getMinStackArgumentAlignment()) {
1802 assert(((Align & (Align-1)) == 0) && "Expected Align to be a power of 2");
1803
1804 VAList = DAG.getNode(ISD::ADD, DL, VAList.getValueType(), VAList,
1805 DAG.getConstant(Align - 1,
1806 VAList.getValueType()));
1807
1808 VAList = DAG.getNode(ISD::AND, DL, VAList.getValueType(), VAList,
1809 DAG.getConstant(-(int64_t)Align,
1810 VAList.getValueType()));
1811 }
1812
1813 // Increment the pointer, VAList, to the next vaarg.
1814 unsigned ArgSizeInBytes = getDataLayout()->getTypeAllocSize(VT.getTypeForEVT(*DAG.getContext()));
1815 SDValue Tmp3 = DAG.getNode(ISD::ADD, DL, VAList.getValueType(), VAList,
1816 DAG.getConstant(RoundUpToAlignment(ArgSizeInBytes, ArgSlotSizeInBytes),
1817 VAList.getValueType()));
1818 // Store the incremented VAList to the legalized pointer
1819 Chain = DAG.getStore(VAListLoad.getValue(1), DL, Tmp3, VAListPtr,
1820 MachinePointerInfo(SV), false, false, 0);
1821
1822 // In big-endian mode we must adjust the pointer when the load size is smaller
1823 // than the argument slot size. We must also reduce the known alignment to
1824 // match. For example in the N64 ABI, we must add 4 bytes to the offset to get
1825 // the correct half of the slot, and reduce the alignment from 8 (slot
1826 // alignment) down to 4 (type alignment).
1827 if (!Subtarget.isLittle() && ArgSizeInBytes < ArgSlotSizeInBytes) {
1828 unsigned Adjustment = ArgSlotSizeInBytes - ArgSizeInBytes;
1829 VAList = DAG.getNode(ISD::ADD, DL, VAListPtr.getValueType(), VAList,
1830 DAG.getIntPtrConstant(Adjustment));
1831 }
1832 // Load the actual argument out of the pointer VAList
1833 return DAG.getLoad(VT, DL, Chain, VAList, MachinePointerInfo(), false, false,
1834 false, 0);
1835}
1836
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001837static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG,
1838 bool HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001839 EVT TyX = Op.getOperand(0).getValueType();
1840 EVT TyY = Op.getOperand(1).getValueType();
1841 SDValue Const1 = DAG.getConstant(1, MVT::i32);
1842 SDValue Const31 = DAG.getConstant(31, MVT::i32);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001843 SDLoc DL(Op);
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001844 SDValue Res;
1845
1846 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1847 // to i32.
1848 SDValue X = (TyX == MVT::f32) ?
1849 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1850 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1851 Const1);
1852 SDValue Y = (TyY == MVT::f32) ?
1853 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
1854 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
1855 Const1);
1856
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001857 if (HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001858 // ext E, Y, 31, 1 ; extract bit31 of Y
1859 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
1860 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
1861 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
1862 } else {
1863 // sll SllX, X, 1
1864 // srl SrlX, SllX, 1
1865 // srl SrlY, Y, 31
1866 // sll SllY, SrlX, 31
1867 // or Or, SrlX, SllY
1868 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1869 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1870 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
1871 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
1872 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
1873 }
1874
1875 if (TyX == MVT::f32)
1876 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
1877
1878 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1879 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1880 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001881}
1882
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001883static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG,
1884 bool HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001885 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
1886 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
1887 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
1888 SDValue Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001889 SDLoc DL(Op);
Eric Christopher0713a9d2011-06-08 23:55:35 +00001890
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001891 // Bitcast to integer nodes.
1892 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
1893 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001894
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001895 if (HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001896 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
1897 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
1898 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
1899 DAG.getConstant(WidthY - 1, MVT::i32), Const1);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001900
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001901 if (WidthX > WidthY)
1902 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
1903 else if (WidthY > WidthX)
1904 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001905
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001906 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
1907 DAG.getConstant(WidthX - 1, MVT::i32), Const1, X);
1908 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
1909 }
1910
1911 // (d)sll SllX, X, 1
1912 // (d)srl SrlX, SllX, 1
1913 // (d)srl SrlY, Y, width(Y)-1
1914 // (d)sll SllY, SrlX, width(Y)-1
1915 // or Or, SrlX, SllY
1916 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
1917 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
1918 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
1919 DAG.getConstant(WidthY - 1, MVT::i32));
1920
1921 if (WidthX > WidthY)
1922 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
1923 else if (WidthY > WidthX)
1924 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
1925
1926 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
1927 DAG.getConstant(WidthX - 1, MVT::i32));
1928 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
1929 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001930}
1931
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00001932SDValue
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001933MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00001934 if (Subtarget.isGP64bit())
1935 return lowerFCOPYSIGN64(Op, DAG, Subtarget.hasExtractInsert());
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001936
Eric Christopher1c29a652014-07-18 22:55:25 +00001937 return lowerFCOPYSIGN32(Op, DAG, Subtarget.hasExtractInsert());
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001938}
1939
Akira Hatanaka66277522011-06-02 00:24:44 +00001940SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001941lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes5444a7b2011-06-16 00:40:02 +00001942 // check the depth
1943 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka15506782011-06-07 18:58:42 +00001944 "Frame address can only be determined for current frame.");
Akira Hatanaka66277522011-06-02 00:24:44 +00001945
1946 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1947 MFI->setFrameAddressIsTaken(true);
1948 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001949 SDLoc DL(Op);
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001950 SDValue FrameAddr =
1951 DAG.getCopyFromReg(DAG.getEntryNode(), DL,
Eric Christopher1c29a652014-07-18 22:55:25 +00001952 Subtarget.isABI_N64() ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka66277522011-06-02 00:24:44 +00001953 return FrameAddr;
1954}
1955
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001956SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op,
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00001957 SelectionDAG &DAG) const {
Bill Wendling908bf812014-01-06 00:43:20 +00001958 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001959 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001960
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00001961 // check the depth
1962 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
1963 "Return address can be determined only for current frame.");
1964
1965 MachineFunction &MF = DAG.getMachineFunction();
1966 MachineFrameInfo *MFI = MF.getFrameInfo();
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001967 MVT VT = Op.getSimpleValueType();
Eric Christopher1c29a652014-07-18 22:55:25 +00001968 unsigned RA = Subtarget.isABI_N64() ? Mips::RA_64 : Mips::RA;
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00001969 MFI->setReturnAddressIsTaken(true);
1970
1971 // Return RA, which contains the return address. Mark it an implicit live-in.
1972 unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00001973 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT);
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00001974}
1975
Akira Hatanakac0b02062013-01-30 00:26:49 +00001976// An EH_RETURN is the result of lowering llvm.eh.return which in turn is
1977// generated from __builtin_eh_return (offset, handler)
1978// The effect of this is to adjust the stack pointer by "offset"
1979// and then branch to "handler".
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001980SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Akira Hatanakac0b02062013-01-30 00:26:49 +00001981 const {
1982 MachineFunction &MF = DAG.getMachineFunction();
1983 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1984
1985 MipsFI->setCallsEhReturn();
1986 SDValue Chain = Op.getOperand(0);
1987 SDValue Offset = Op.getOperand(1);
1988 SDValue Handler = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001989 SDLoc DL(Op);
Eric Christopher1c29a652014-07-18 22:55:25 +00001990 EVT Ty = Subtarget.isABI_N64() ? MVT::i64 : MVT::i32;
Akira Hatanakac0b02062013-01-30 00:26:49 +00001991
1992 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
1993 // EH_RETURN nodes, so that instructions are emitted back-to-back.
Eric Christopher1c29a652014-07-18 22:55:25 +00001994 unsigned OffsetReg = Subtarget.isABI_N64() ? Mips::V1_64 : Mips::V1;
1995 unsigned AddrReg = Subtarget.isABI_N64() ? Mips::V0_64 : Mips::V0;
Akira Hatanakac0b02062013-01-30 00:26:49 +00001996 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
1997 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
1998 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
1999 DAG.getRegister(OffsetReg, Ty),
2000 DAG.getRegister(AddrReg, getPointerTy()),
2001 Chain.getValue(1));
2002}
2003
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002004SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
Akira Hatanaka5fd22482012-06-14 21:10:56 +00002005 SelectionDAG &DAG) const {
Eli Friedman26a48482011-07-27 22:21:52 +00002006 // FIXME: Need pseudo-fence for 'singlethread' fences
2007 // FIXME: Set SType for weaker fences where supported/appropriate.
2008 unsigned SType = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002009 SDLoc DL(Op);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002010 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
Eli Friedman26a48482011-07-27 22:21:52 +00002011 DAG.getConstant(SType, MVT::i32));
2012}
2013
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002014SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
Akira Hatanaka5fd22482012-06-14 21:10:56 +00002015 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002016 SDLoc DL(Op);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002017 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2018 SDValue Shamt = Op.getOperand(2);
2019
2020 // if shamt < 32:
2021 // lo = (shl lo, shamt)
2022 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
2023 // else:
2024 // lo = 0
2025 // hi = (shl lo, shamt[4:0])
2026 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
2027 DAG.getConstant(-1, MVT::i32));
2028 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo,
2029 DAG.getConstant(1, MVT::i32));
2030 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, ShiftRight1Lo,
2031 Not);
2032 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi, Shamt);
2033 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
2034 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, MVT::i32, Lo, Shamt);
2035 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
2036 DAG.getConstant(0x20, MVT::i32));
Akira Hatanaka5fd22482012-06-14 21:10:56 +00002037 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
2038 DAG.getConstant(0, MVT::i32), ShiftLeftLo);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002039 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftLeftLo, Or);
2040
2041 SDValue Ops[2] = {Lo, Hi};
Craig Topper64941d92014-04-27 19:20:57 +00002042 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002043}
2044
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002045SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002046 bool IsSRA) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002047 SDLoc DL(Op);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002048 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2049 SDValue Shamt = Op.getOperand(2);
2050
2051 // if shamt < 32:
2052 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
2053 // if isSRA:
2054 // hi = (sra hi, shamt)
2055 // else:
2056 // hi = (srl hi, shamt)
2057 // else:
2058 // if isSRA:
2059 // lo = (sra hi, shamt[4:0])
2060 // hi = (sra hi, 31)
2061 // else:
2062 // lo = (srl hi, shamt[4:0])
2063 // hi = 0
2064 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
2065 DAG.getConstant(-1, MVT::i32));
2066 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi,
2067 DAG.getConstant(1, MVT::i32));
2068 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, ShiftLeft1Hi, Not);
2069 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo, Shamt);
2070 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
2071 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL, DL, MVT::i32,
2072 Hi, Shamt);
2073 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
2074 DAG.getConstant(0x20, MVT::i32));
2075 SDValue Shift31 = DAG.getNode(ISD::SRA, DL, MVT::i32, Hi,
2076 DAG.getConstant(31, MVT::i32));
2077 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftRightHi, Or);
2078 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
2079 IsSRA ? Shift31 : DAG.getConstant(0, MVT::i32),
2080 ShiftRightHi);
2081
2082 SDValue Ops[2] = {Lo, Hi};
Craig Topper64941d92014-04-27 19:20:57 +00002083 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002084}
2085
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002086static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002087 SDValue Chain, SDValue Src, unsigned Offset) {
Akira Hatanaka95866182012-06-13 19:06:08 +00002088 SDValue Ptr = LD->getBasePtr();
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002089 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
Akira Hatanaka95866182012-06-13 19:06:08 +00002090 EVT BasePtrVT = Ptr.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002091 SDLoc DL(LD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002092 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
2093
2094 if (Offset)
Akira Hatanaka95866182012-06-13 19:06:08 +00002095 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002096 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002097
2098 SDValue Ops[] = { Chain, Ptr, Src };
Craig Topper206fcd42014-04-26 19:29:41 +00002099 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002100 LD->getMemOperand());
2101}
2102
2103// Expand an unaligned 32 or 64-bit integer load node.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002104SDValue MipsTargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002105 LoadSDNode *LD = cast<LoadSDNode>(Op);
2106 EVT MemVT = LD->getMemoryVT();
2107
Eric Christopher1c29a652014-07-18 22:55:25 +00002108 if (Subtarget.systemSupportsUnalignedAccess())
Daniel Sandersac272632014-05-23 13:18:02 +00002109 return Op;
2110
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002111 // Return if load is aligned or if MemVT is neither i32 nor i64.
2112 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
2113 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
2114 return SDValue();
2115
Eric Christopher1c29a652014-07-18 22:55:25 +00002116 bool IsLittle = Subtarget.isLittle();
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002117 EVT VT = Op.getValueType();
2118 ISD::LoadExtType ExtType = LD->getExtensionType();
2119 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
2120
2121 assert((VT == MVT::i32) || (VT == MVT::i64));
2122
2123 // Expand
2124 // (set dst, (i64 (load baseptr)))
2125 // to
2126 // (set tmp, (ldl (add baseptr, 7), undef))
2127 // (set dst, (ldr baseptr, tmp))
2128 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002129 SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002130 IsLittle ? 7 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002131 return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002132 IsLittle ? 0 : 7);
2133 }
2134
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002135 SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002136 IsLittle ? 3 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002137 SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002138 IsLittle ? 0 : 3);
2139
2140 // Expand
2141 // (set dst, (i32 (load baseptr))) or
2142 // (set dst, (i64 (sextload baseptr))) or
2143 // (set dst, (i64 (extload baseptr)))
2144 // to
2145 // (set tmp, (lwl (add baseptr, 3), undef))
2146 // (set dst, (lwr baseptr, tmp))
2147 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
2148 (ExtType == ISD::EXTLOAD))
2149 return LWR;
2150
2151 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
2152
2153 // Expand
2154 // (set dst, (i64 (zextload baseptr)))
2155 // to
2156 // (set tmp0, (lwl (add baseptr, 3), undef))
2157 // (set tmp1, (lwr baseptr, tmp0))
2158 // (set tmp2, (shl tmp1, 32))
2159 // (set dst, (srl tmp2, 32))
Andrew Trickef9de2a2013-05-25 02:42:55 +00002160 SDLoc DL(LD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002161 SDValue Const32 = DAG.getConstant(32, MVT::i32);
2162 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
Akira Hatanaka67346852012-06-04 17:46:29 +00002163 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
2164 SDValue Ops[] = { SRL, LWR.getValue(1) };
Craig Topper64941d92014-04-27 19:20:57 +00002165 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002166}
2167
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002168static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002169 SDValue Chain, unsigned Offset) {
Akira Hatanaka95866182012-06-13 19:06:08 +00002170 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
2171 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002172 SDLoc DL(SD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002173 SDVTList VTList = DAG.getVTList(MVT::Other);
2174
2175 if (Offset)
Akira Hatanaka95866182012-06-13 19:06:08 +00002176 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002177 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002178
2179 SDValue Ops[] = { Chain, Value, Ptr };
Craig Topper206fcd42014-04-26 19:29:41 +00002180 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002181 SD->getMemOperand());
2182}
2183
2184// Expand an unaligned 32 or 64-bit integer store node.
Akira Hatanakad82ee942013-05-16 20:45:17 +00002185static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG,
2186 bool IsLittle) {
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002187 SDValue Value = SD->getValue(), Chain = SD->getChain();
2188 EVT VT = Value.getValueType();
2189
2190 // Expand
2191 // (store val, baseptr) or
2192 // (truncstore val, baseptr)
2193 // to
2194 // (swl val, (add baseptr, 3))
2195 // (swr val, baseptr)
2196 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002197 SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002198 IsLittle ? 3 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002199 return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002200 }
2201
2202 assert(VT == MVT::i64);
2203
2204 // Expand
2205 // (store val, baseptr)
2206 // to
2207 // (sdl val, (add baseptr, 7))
2208 // (sdr val, baseptr)
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002209 SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2210 return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002211}
2212
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002213// Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr).
2214static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG) {
2215 SDValue Val = SD->getValue();
2216
2217 if (Val.getOpcode() != ISD::FP_TO_SINT)
2218 return SDValue();
2219
2220 EVT FPTy = EVT::getFloatingPointVT(Val.getValueSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002221 SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy,
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002222 Val.getOperand(0));
2223
Andrew Trickef9de2a2013-05-25 02:42:55 +00002224 return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(),
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002225 SD->getPointerInfo(), SD->isVolatile(),
2226 SD->isNonTemporal(), SD->getAlignment());
2227}
2228
Akira Hatanakad82ee942013-05-16 20:45:17 +00002229SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2230 StoreSDNode *SD = cast<StoreSDNode>(Op);
2231 EVT MemVT = SD->getMemoryVT();
2232
2233 // Lower unaligned integer stores.
Eric Christopher1c29a652014-07-18 22:55:25 +00002234 if (!Subtarget.systemSupportsUnalignedAccess() &&
Daniel Sandersac272632014-05-23 13:18:02 +00002235 (SD->getAlignment() < MemVT.getSizeInBits() / 8) &&
Akira Hatanakad82ee942013-05-16 20:45:17 +00002236 ((MemVT == MVT::i32) || (MemVT == MVT::i64)))
Eric Christopher1c29a652014-07-18 22:55:25 +00002237 return lowerUnalignedIntStore(SD, DAG, Subtarget.isLittle());
Akira Hatanakad82ee942013-05-16 20:45:17 +00002238
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002239 return lowerFP_TO_SINT_STORE(SD, DAG);
Akira Hatanakad82ee942013-05-16 20:45:17 +00002240}
2241
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002242SDValue MipsTargetLowering::lowerADD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002243 if (Op->getOperand(0).getOpcode() != ISD::FRAMEADDR
2244 || cast<ConstantSDNode>
2245 (Op->getOperand(0).getOperand(0))->getZExtValue() != 0
2246 || Op->getOperand(1).getOpcode() != ISD::FRAME_TO_ARGS_OFFSET)
2247 return SDValue();
2248
2249 // The pattern
2250 // (add (frameaddr 0), (frame_to_args_offset))
2251 // results from lowering llvm.eh.dwarf.cfa intrinsic. Transform it to
2252 // (add FrameObject, 0)
2253 // where FrameObject is a fixed StackObject with offset 0 which points to
2254 // the old stack pointer.
2255 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2256 EVT ValTy = Op->getValueType(0);
2257 int FI = MFI->CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
2258 SDValue InArgsAddr = DAG.getFrameIndex(FI, ValTy);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002259 return DAG.getNode(ISD::ADD, SDLoc(Op), ValTy, InArgsAddr,
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002260 DAG.getConstant(0, ValTy));
2261}
2262
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002263SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op,
2264 SelectionDAG &DAG) const {
2265 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002266 SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy,
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002267 Op.getOperand(0));
Andrew Trickef9de2a2013-05-25 02:42:55 +00002268 return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc);
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002269}
2270
Akira Hatanakae2489122011-04-15 21:51:11 +00002271//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002272// Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002273//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002274
Akira Hatanakae2489122011-04-15 21:51:11 +00002275//===----------------------------------------------------------------------===//
Wesley Peck527da1b2010-11-23 03:31:01 +00002276// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002277// Mips O32 ABI rules:
2278// ---
2279// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peck527da1b2010-11-23 03:31:01 +00002280// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002281// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peck527da1b2010-11-23 03:31:01 +00002282// f64 - Only passed in two aliased f32 registers if no int reg has been used
2283// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Sylvestre Ledru469de192014-08-11 18:04:46 +00002284// not used, it must be shadowed. If only A3 is available, shadow it and
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002285// go to stack.
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002286//
2287// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanakae2489122011-04-15 21:51:11 +00002288//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002289
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00002290static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2291 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
Craig Topper840beec2014-04-04 05:16:06 +00002292 CCState &State, const MCPhysReg *F64Regs) {
Daniel Sandersd134c9d2014-12-02 20:40:27 +00002293 const MipsSubtarget &Subtarget =
2294 State.getMachineFunction().getTarget()
2295 .getSubtarget<const MipsSubtarget>();
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002296
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00002297 static const unsigned IntRegsSize = 4, FloatRegsSize = 2;
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002298
Craig Topper840beec2014-04-04 05:16:06 +00002299 static const MCPhysReg IntRegs[] = { Mips::A0, Mips::A1, Mips::A2, Mips::A3 };
2300 static const MCPhysReg F32Regs[] = { Mips::F12, Mips::F14 };
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002301
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002302 // Do not process byval args here.
2303 if (ArgFlags.isByVal())
2304 return true;
Akira Hatanaka5e16c6a2011-05-24 19:18:33 +00002305
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002306 // Promote i8 and i16
Daniel Sandersd134c9d2014-12-02 20:40:27 +00002307 if (ArgFlags.isInReg() && !Subtarget.isLittle()) {
2308 if (LocVT == MVT::i8 || LocVT == MVT::i16 || LocVT == MVT::i32) {
2309 LocVT = MVT::i32;
2310 if (ArgFlags.isSExt())
2311 LocInfo = CCValAssign::SExtUpper;
2312 else if (ArgFlags.isZExt())
2313 LocInfo = CCValAssign::ZExtUpper;
2314 else
2315 LocInfo = CCValAssign::AExtUpper;
2316 }
2317 }
2318
2319 // Promote i8 and i16
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002320 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
2321 LocVT = MVT::i32;
2322 if (ArgFlags.isSExt())
2323 LocInfo = CCValAssign::SExt;
2324 else if (ArgFlags.isZExt())
2325 LocInfo = CCValAssign::ZExt;
2326 else
2327 LocInfo = CCValAssign::AExt;
2328 }
2329
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002330 unsigned Reg;
2331
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002332 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
2333 // is true: function is vararg, argument is 3rd or higher, there is previous
2334 // argument which is not f32 or f64.
2335 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
2336 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanaka9e6a8cc2011-05-19 20:29:48 +00002337 unsigned OrigAlign = ArgFlags.getOrigAlign();
2338 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002339
2340 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002341 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanaka9e6a8cc2011-05-19 20:29:48 +00002342 // If this is the first part of an i64 arg,
2343 // the allocated register must be either A0 or A2.
2344 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
2345 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002346 LocVT = MVT::i32;
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002347 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
2348 // Allocate int register and shadow next int register. If first
2349 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002350 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2351 if (Reg == Mips::A1 || Reg == Mips::A3)
2352 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2353 State.AllocateReg(IntRegs, IntRegsSize);
2354 LocVT = MVT::i32;
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002355 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
2356 // we are guaranteed to find an available float register
2357 if (ValVT == MVT::f32) {
2358 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
2359 // Shadow int register
2360 State.AllocateReg(IntRegs, IntRegsSize);
2361 } else {
2362 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
2363 // Shadow int registers
2364 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
2365 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
2366 State.AllocateReg(IntRegs, IntRegsSize);
2367 State.AllocateReg(IntRegs, IntRegsSize);
2368 }
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002369 } else
2370 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002371
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002372 if (!Reg) {
2373 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3,
2374 OrigAlign);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002375 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002376 } else
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002377 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002378
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002379 return false;
Akira Hatanaka202f6402011-11-12 02:20:46 +00002380}
2381
Akira Hatanakabfb66242013-08-20 23:38:40 +00002382static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT,
2383 MVT LocVT, CCValAssign::LocInfo LocInfo,
2384 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002385 static const MCPhysReg F64Regs[] = { Mips::D6, Mips::D7 };
Akira Hatanakabfb66242013-08-20 23:38:40 +00002386
2387 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2388}
2389
2390static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT,
2391 MVT LocVT, CCValAssign::LocInfo LocInfo,
2392 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002393 static const MCPhysReg F64Regs[] = { Mips::D12_64, Mips::D14_64 };
Akira Hatanakabfb66242013-08-20 23:38:40 +00002394
2395 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2396}
2397
Reid Klecknerd3781742014-11-14 00:39:33 +00002398static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2399 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
2400 CCState &State) LLVM_ATTRIBUTE_UNUSED;
Reed Kotlerd5c41962014-11-13 23:37:45 +00002401
Akira Hatanaka202f6402011-11-12 02:20:46 +00002402#include "MipsGenCallingConv.inc"
2403
Akira Hatanakae2489122011-04-15 21:51:11 +00002404//===----------------------------------------------------------------------===//
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002405// Call Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002406//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002407
Akira Hatanaka61bbcce2011-09-23 00:58:33 +00002408// Return next O32 integer argument register.
2409static unsigned getNextIntArgReg(unsigned Reg) {
2410 assert((Reg == Mips::A0) || (Reg == Mips::A2));
2411 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
2412}
2413
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002414SDValue
2415MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002416 SDValue Chain, SDValue Arg, SDLoc DL,
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002417 bool IsTailCall, SelectionDAG &DAG) const {
2418 if (!IsTailCall) {
2419 SDValue PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr,
2420 DAG.getIntPtrConstant(Offset));
2421 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo(), false,
2422 false, 0);
2423 }
2424
2425 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2426 int FI = MFI->CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
2427 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2428 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(),
2429 /*isVolatile=*/ true, false, 0);
2430}
2431
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002432void MipsTargetLowering::
2433getOpndList(SmallVectorImpl<SDValue> &Ops,
2434 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
2435 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
Sasa Stankovic7072a792014-10-01 08:22:21 +00002436 bool IsCallReloc, CallLoweringInfo &CLI, SDValue Callee,
2437 SDValue Chain) const {
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002438 // Insert node "GP copy globalreg" before call to function.
2439 //
2440 // R_MIPS_CALL* operators (emitted when non-internal functions are called
2441 // in PIC mode) allow symbols to be resolved via lazy binding.
2442 // The lazy binding stub requires GP to point to the GOT.
Sasa Stankovic7072a792014-10-01 08:22:21 +00002443 // Note that we don't need GP to point to the GOT for indirect calls
2444 // (when R_MIPS_CALL* is not used for the call) because Mips linker generates
2445 // lazy binding stub for a function only when R_MIPS_CALL* are the only relocs
2446 // used for the function (that is, Mips linker doesn't generate lazy binding
2447 // stub for a function whose address is taken in the program).
2448 if (IsPICCall && !InternalLinkage && IsCallReloc) {
Eric Christopher1c29a652014-07-18 22:55:25 +00002449 unsigned GPReg = Subtarget.isABI_N64() ? Mips::GP_64 : Mips::GP;
2450 EVT Ty = Subtarget.isABI_N64() ? MVT::i64 : MVT::i32;
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002451 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty)));
2452 }
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002453
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002454 // Build a sequence of copy-to-reg nodes chained together with token
2455 // chain and flag operands which copy the outgoing args into registers.
2456 // The InFlag in necessary since all emitted instructions must be
2457 // stuck together.
2458 SDValue InFlag;
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002459
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002460 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2461 Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, RegsToPass[i].first,
2462 RegsToPass[i].second, InFlag);
2463 InFlag = Chain.getValue(1);
2464 }
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002465
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002466 // Add argument registers to the end of the list so that they are
2467 // known live into the call.
2468 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2469 Ops.push_back(CLI.DAG.getRegister(RegsToPass[i].first,
2470 RegsToPass[i].second.getValueType()));
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002471
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002472 // Add a register mask operand representing the call-preserved registers.
Eric Christopherd9134482014-08-04 21:25:23 +00002473 const TargetRegisterInfo *TRI =
2474 getTargetMachine().getSubtargetImpl()->getRegisterInfo();
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002475 const uint32_t *Mask = TRI->getCallPreservedMask(CLI.CallConv);
2476 assert(Mask && "Missing call preserved mask for calling convention");
Eric Christopher1c29a652014-07-18 22:55:25 +00002477 if (Subtarget.inMips16HardFloat()) {
Reed Kotler783c7942013-05-10 22:25:39 +00002478 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
2479 llvm::StringRef Sym = G->getGlobal()->getName();
2480 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
Reed Kotler3230e722013-12-12 02:41:11 +00002481 if (F && F->hasFnAttribute("__Mips16RetHelper")) {
Reed Kotler783c7942013-05-10 22:25:39 +00002482 Mask = MipsRegisterInfo::getMips16RetHelperMask();
2483 }
2484 }
2485 }
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002486 Ops.push_back(CLI.DAG.getRegisterMask(Mask));
2487
2488 if (InFlag.getNode())
2489 Ops.push_back(InFlag);
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002490}
2491
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002492/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman624801e2009-01-26 03:15:54 +00002493/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002494SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00002495MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002496 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00002497 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002498 SDLoc DL = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00002499 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
2500 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
2501 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Akira Hatanakabeda2242012-07-31 18:46:41 +00002502 SDValue Chain = CLI.Chain;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002503 SDValue Callee = CLI.Callee;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002504 bool &IsTailCall = CLI.IsTailCall;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002505 CallingConv::ID CallConv = CLI.CallConv;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002506 bool IsVarArg = CLI.IsVarArg;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002507
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002508 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002509 MachineFrameInfo *MFI = MF.getFrameInfo();
Eric Christopherfc6de422014-08-05 02:39:49 +00002510 const TargetFrameLowering *TFL = MF.getSubtarget().getFrameLowering();
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002511 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +00002512 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002513
2514 // Analyze operands of the call, assigning locations to each operand.
2515 SmallVector<CCValAssign, 16> ArgLocs;
Daniel Sanders41a64c42014-11-07 11:10:48 +00002516 MipsCCState CCInfo(
2517 CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs, *DAG.getContext(),
2518 MipsCCState::getSpecialCallingConvForCallee(Callee.getNode(), Subtarget));
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002519
2520 // Allocate the reserved argument area. It seems strange to do this from the
2521 // caller side but removing it breaks the frame size calculation.
2522 const MipsABIInfo &ABI = Subtarget.getABI();
2523 CCInfo.AllocateStack(ABI.GetCalleeAllocdArgSizeInBytes(CallConv), 1);
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002524
Daniel Sanderscfad1e32014-11-07 11:43:49 +00002525 CCInfo.AnalyzeCallOperands(Outs, CC_Mips, CLI.getArgs(), Callee.getNode());
Wesley Peck527da1b2010-11-23 03:31:01 +00002526
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002527 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka195a1e22011-06-08 17:39:33 +00002528 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka97ba7692012-07-26 23:27:01 +00002529
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002530 // Check if it's really possible to do a tail call.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002531 if (IsTailCall)
Daniel Sanders23e98772014-11-02 16:09:29 +00002532 IsTailCall = isEligibleForTailCallOptimization(
2533 CCInfo, NextStackOffset, *MF.getInfo<MipsFunctionInfo>());
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002534
Reid Kleckner5772b772014-04-24 20:14:34 +00002535 if (!IsTailCall && CLI.CS && CLI.CS->isMustTailCall())
2536 report_fatal_error("failed to perform tail call elimination on a call "
2537 "site marked musttail");
2538
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002539 if (IsTailCall)
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002540 ++NumTailCalls;
2541
Akira Hatanaka79738332011-09-19 20:26:02 +00002542 // Chain is the output chain of the last Load/Store or CopyToReg node.
2543 // ByValChain is the output chain of the last Memcpy node created for copying
2544 // byval arguments to the stack.
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002545 unsigned StackAlignment = TFL->getStackAlignment();
2546 NextStackOffset = RoundUpToAlignment(NextStackOffset, StackAlignment);
Akira Hatanaka79738332011-09-19 20:26:02 +00002547 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002548
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002549 if (!IsTailCall)
Andrew Trickad6d08a2013-05-29 22:03:55 +00002550 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffsetVal, DL);
Akira Hatanakabeda2242012-07-31 18:46:41 +00002551
Daniel Sandersd897b562014-03-27 10:46:12 +00002552 SDValue StackPtr = DAG.getCopyFromReg(
Eric Christopher1c29a652014-07-18 22:55:25 +00002553 Chain, DL, Subtarget.isABI_N64() ? Mips::SP_64 : Mips::SP,
Eric Christopherbf33a3c2014-07-02 23:18:40 +00002554 getPointerTy());
Akira Hatanaka195a1e22011-06-08 17:39:33 +00002555
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002556 // With EABI is it possible to have 16 args on registers.
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002557 std::deque< std::pair<unsigned, SDValue> > RegsToPass;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002558 SmallVector<SDValue, 8> MemOpChains;
Daniel Sanders23e98772014-11-02 16:09:29 +00002559
2560 CCInfo.rewindByValRegsInfo();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002561
2562 // Walk the register/memloc assignments, inserting copies/loads.
2563 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002564 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002565 CCValAssign &VA = ArgLocs[i];
Akira Hatanakab20a3252011-10-28 19:49:00 +00002566 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka19891f82011-11-12 02:34:50 +00002567 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002568 bool UseUpperBits = false;
Akira Hatanaka19891f82011-11-12 02:34:50 +00002569
2570 // ByVal Arg.
2571 if (Flags.isByVal()) {
Daniel Sanders23e98772014-11-02 16:09:29 +00002572 unsigned FirstByValReg, LastByValReg;
2573 unsigned ByValIdx = CCInfo.getInRegsParamsProcessed();
2574 CCInfo.getInRegsParamInfo(ByValIdx, FirstByValReg, LastByValReg);
2575
Akira Hatanaka19891f82011-11-12 02:34:50 +00002576 assert(Flags.getByValSize() &&
2577 "ByVal args of size 0 should have been ignored by front-end.");
Daniel Sanders23e98772014-11-02 16:09:29 +00002578 assert(ByValIdx < CCInfo.getInRegsParamsCount());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002579 assert(!IsTailCall &&
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002580 "Do not tail-call optimize if there is a byval argument.");
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002581 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002582 FirstByValReg, LastByValReg, Flags, Subtarget.isLittle(),
2583 VA);
Daniel Sanders23e98772014-11-02 16:09:29 +00002584 CCInfo.nextInRegsParam();
Akira Hatanaka19891f82011-11-12 02:34:50 +00002585 continue;
2586 }
Jia Liuf54f60f2012-02-28 07:46:26 +00002587
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002588 // Promote the value if needed.
2589 switch (VA.getLocInfo()) {
Daniel Sandersc43cda82014-11-07 16:54:21 +00002590 default:
2591 llvm_unreachable("Unknown loc info!");
Wesley Peck527da1b2010-11-23 03:31:01 +00002592 case CCValAssign::Full:
Akira Hatanakab20a3252011-10-28 19:49:00 +00002593 if (VA.isRegLoc()) {
2594 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
Akira Hatanaka3b7391d2013-03-05 22:20:28 +00002595 (ValVT == MVT::f64 && LocVT == MVT::i64) ||
2596 (ValVT == MVT::i64 && LocVT == MVT::f64))
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002597 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
Akira Hatanakab20a3252011-10-28 19:49:00 +00002598 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002599 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanakae2489122011-04-15 21:51:11 +00002600 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002601 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanakaaef55c82011-04-15 21:00:26 +00002602 Arg, DAG.getConstant(1, MVT::i32));
Eric Christopher1c29a652014-07-18 22:55:25 +00002603 if (!Subtarget.isLittle())
Akira Hatanaka27916972011-04-15 19:52:08 +00002604 std::swap(Lo, Hi);
Jia Liuf54f60f2012-02-28 07:46:26 +00002605 unsigned LocRegLo = VA.getLocReg();
Akira Hatanaka61bbcce2011-09-23 00:58:33 +00002606 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2607 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2608 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002609 continue;
Wesley Peck527da1b2010-11-23 03:31:01 +00002610 }
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002611 }
2612 break;
Daniel Sanders23e98772014-11-02 16:09:29 +00002613 case CCValAssign::BCvt:
2614 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
2615 break;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002616 case CCValAssign::SExtUpper:
2617 UseUpperBits = true;
2618 // Fallthrough
Chris Lattner52f16de2008-03-17 06:57:02 +00002619 case CCValAssign::SExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002620 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002621 break;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002622 case CCValAssign::ZExtUpper:
2623 UseUpperBits = true;
2624 // Fallthrough
Chris Lattner52f16de2008-03-17 06:57:02 +00002625 case CCValAssign::ZExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002626 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002627 break;
Daniel Sandersc43cda82014-11-07 16:54:21 +00002628 case CCValAssign::AExtUpper:
2629 UseUpperBits = true;
2630 // Fallthrough
Chris Lattner52f16de2008-03-17 06:57:02 +00002631 case CCValAssign::AExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002632 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002633 break;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002634 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002635
Daniel Sandersc43cda82014-11-07 16:54:21 +00002636 if (UseUpperBits) {
2637 unsigned ValSizeInBits = Outs[i].ArgVT.getSizeInBits();
2638 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
2639 Arg = DAG.getNode(
2640 ISD::SHL, DL, VA.getLocVT(), Arg,
2641 DAG.getConstant(LocSizeInBits - ValSizeInBits, VA.getLocVT()));
2642 }
2643
Wesley Peck527da1b2010-11-23 03:31:01 +00002644 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +00002645 // RegsToPass vector
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002646 if (VA.isRegLoc()) {
2647 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattner52f16de2008-03-17 06:57:02 +00002648 continue;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002649 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002650
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002651 // Register can't get to this point...
Chris Lattner52f16de2008-03-17 06:57:02 +00002652 assert(VA.isMemLoc());
Wesley Peck527da1b2010-11-23 03:31:01 +00002653
Wesley Peck527da1b2010-11-23 03:31:01 +00002654 // emit ISD::STORE whichs stores the
Chris Lattner52f16de2008-03-17 06:57:02 +00002655 // parameter value to a stack Location
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002656 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002657 Chain, Arg, DL, IsTailCall, DAG));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002658 }
2659
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002660 // Transform all store nodes into one single node because all store
2661 // nodes are independent of each other.
Wesley Peck527da1b2010-11-23 03:31:01 +00002662 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00002663 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002664
Bill Wendling24c79f22008-09-16 21:48:12 +00002665 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peck527da1b2010-11-23 03:31:01 +00002666 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2667 // node so that legalize doesn't hack it.
Eric Christopherbf33a3c2014-07-02 23:18:40 +00002668 bool IsPICCall =
Eric Christopher1c29a652014-07-18 22:55:25 +00002669 (Subtarget.isABI_N64() || IsPIC); // true if calls are translated to
Eric Christopherbf33a3c2014-07-02 23:18:40 +00002670 // jalr $25
Sasa Stankovic7072a792014-10-01 08:22:21 +00002671 bool GlobalOrExternal = false, InternalLinkage = false, IsCallReloc = false;
Akira Hatanakad6f1c582011-04-07 19:51:44 +00002672 SDValue CalleeLo;
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002673 EVT Ty = Callee.getValueType();
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002674
2675 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002676 if (IsPICCall) {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002677 const GlobalValue *Val = G->getGlobal();
2678 InternalLinkage = Val->hasInternalLinkage();
Akira Hatanakacf9a61b2012-12-13 03:17:29 +00002679
2680 if (InternalLinkage)
Eric Christopherbf33a3c2014-07-02 23:18:40 +00002681 Callee = getAddrLocal(G, Ty, DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +00002682 Subtarget.isABI_N32() || Subtarget.isABI_N64());
Sasa Stankovic7072a792014-10-01 08:22:21 +00002683 else if (LargeGOT) {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002684 Callee = getAddrGlobalLargeGOT(G, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002685 MipsII::MO_CALL_LO16, Chain,
2686 FuncInfo->callPtrInfo(Val));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002687 IsCallReloc = true;
2688 } else {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002689 Callee = getAddrGlobal(G, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
2690 FuncInfo->callPtrInfo(Val));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002691 IsCallReloc = true;
2692 }
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002693 } else
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002694 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, getPointerTy(), 0,
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002695 MipsII::MO_NO_FLAG);
Akira Hatanaka8e16aac2011-12-09 01:45:12 +00002696 GlobalOrExternal = true;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002697 }
2698 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002699 const char *Sym = S->getSymbol();
2700
Eric Christopher1c29a652014-07-18 22:55:25 +00002701 if (!Subtarget.isABI_N64() && !IsPIC) // !N64 && static
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002702 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(),
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002703 MipsII::MO_NO_FLAG);
Sasa Stankovic7072a792014-10-01 08:22:21 +00002704 else if (LargeGOT) {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002705 Callee = getAddrGlobalLargeGOT(S, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002706 MipsII::MO_CALL_LO16, Chain,
2707 FuncInfo->callPtrInfo(Sym));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002708 IsCallReloc = true;
2709 } else { // N64 || PIC
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002710 Callee = getAddrGlobal(S, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
2711 FuncInfo->callPtrInfo(Sym));
Sasa Stankovic7072a792014-10-01 08:22:21 +00002712 IsCallReloc = true;
2713 }
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002714
Akira Hatanaka8e16aac2011-12-09 01:45:12 +00002715 GlobalOrExternal = true;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002716 }
2717
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002718 SmallVector<SDValue, 8> Ops(1, Chain);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002719 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002720
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002721 getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal, InternalLinkage,
Sasa Stankovic7072a792014-10-01 08:22:21 +00002722 IsCallReloc, CLI, Callee, Chain);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002723
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002724 if (IsTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +00002725 return DAG.getNode(MipsISD::TailCall, DL, MVT::Other, Ops);
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002726
Craig Topper48d114b2014-04-26 18:35:24 +00002727 Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, Ops);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002728 SDValue InFlag = Chain.getValue(1);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002729
Bruno Cardoso Lopes193e64c2010-01-30 18:32:07 +00002730 // Create the CALLSEQ_END node.
Akira Hatanaka97ba7692012-07-26 23:27:01 +00002731 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal,
Andrew Trickad6d08a2013-05-29 22:03:55 +00002732 DAG.getIntPtrConstant(0, true), InFlag, DL);
Bruno Cardoso Lopes193e64c2010-01-30 18:32:07 +00002733 InFlag = Chain.getValue(1);
2734
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002735 // Handle result values, copying them out of physregs into vregs that we
2736 // return.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002737 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg, Ins, DL, DAG,
2738 InVals, CLI);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002739}
2740
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002741/// LowerCallResult - Lower the result values of a call into the
2742/// appropriate copies out of appropriate physical registers.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002743SDValue MipsTargetLowering::LowerCallResult(
2744 SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool IsVarArg,
2745 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG,
2746 SmallVectorImpl<SDValue> &InVals,
2747 TargetLowering::CallLoweringInfo &CLI) const {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002748 // Assign locations to each value returned by this call.
2749 SmallVector<CCValAssign, 16> RVLocs;
Daniel Sandersb3ca3382014-09-26 10:06:12 +00002750 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
2751 *DAG.getContext());
2752 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips, CLI);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002753
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002754 // Copy all of the result registers out of their specified physreg.
2755 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Daniel Sandersae275e32014-09-25 12:15:05 +00002756 CCValAssign &VA = RVLocs[i];
2757 assert(VA.isRegLoc() && "Can only return in registers!");
2758
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002759 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(),
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002760 RVLocs[i].getLocVT(), InFlag);
2761 Chain = Val.getValue(1);
2762 InFlag = Val.getValue(2);
2763
Daniel Sandersae275e32014-09-25 12:15:05 +00002764 if (VA.isUpperBitsInLoc()) {
2765 unsigned ValSizeInBits = Ins[i].ArgVT.getSizeInBits();
2766 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
2767 unsigned Shift =
2768 VA.getLocInfo() == CCValAssign::ZExtUpper ? ISD::SRL : ISD::SRA;
2769 Val = DAG.getNode(
2770 Shift, DL, VA.getLocVT(), Val,
2771 DAG.getConstant(LocSizeInBits - ValSizeInBits, VA.getLocVT()));
2772 }
2773
2774 switch (VA.getLocInfo()) {
2775 default:
2776 llvm_unreachable("Unknown loc info!");
2777 case CCValAssign::Full:
2778 break;
2779 case CCValAssign::BCvt:
2780 Val = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Val);
2781 break;
2782 case CCValAssign::AExt:
2783 case CCValAssign::AExtUpper:
2784 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2785 break;
2786 case CCValAssign::ZExt:
2787 case CCValAssign::ZExtUpper:
2788 Val = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Val,
2789 DAG.getValueType(VA.getValVT()));
2790 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2791 break;
2792 case CCValAssign::SExt:
2793 case CCValAssign::SExtUpper:
2794 Val = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Val,
2795 DAG.getValueType(VA.getValVT()));
2796 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2797 break;
2798 }
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002799
2800 InVals.push_back(Val);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002801 }
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +00002802
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002803 return Chain;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002804}
2805
Daniel Sandersc43cda82014-11-07 16:54:21 +00002806static SDValue UnpackFromArgumentSlot(SDValue Val, const CCValAssign &VA,
2807 EVT ArgVT, SDLoc DL, SelectionDAG &DAG) {
2808 MVT LocVT = VA.getLocVT();
2809 EVT ValVT = VA.getValVT();
2810
2811 // Shift into the upper bits if necessary.
2812 switch (VA.getLocInfo()) {
2813 default:
2814 break;
2815 case CCValAssign::AExtUpper:
2816 case CCValAssign::SExtUpper:
2817 case CCValAssign::ZExtUpper: {
2818 unsigned ValSizeInBits = ArgVT.getSizeInBits();
2819 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
2820 unsigned Opcode =
2821 VA.getLocInfo() == CCValAssign::ZExtUpper ? ISD::SRL : ISD::SRA;
2822 Val = DAG.getNode(
2823 Opcode, DL, VA.getLocVT(), Val,
2824 DAG.getConstant(LocSizeInBits - ValSizeInBits, VA.getLocVT()));
2825 break;
2826 }
2827 }
2828
2829 // If this is an value smaller than the argument slot size (32-bit for O32,
2830 // 64-bit for N32/N64), it has been promoted in some way to the argument slot
2831 // size. Extract the value and insert any appropriate assertions regarding
2832 // sign/zero extension.
2833 switch (VA.getLocInfo()) {
2834 default:
2835 llvm_unreachable("Unknown loc info!");
2836 case CCValAssign::Full:
2837 break;
2838 case CCValAssign::AExtUpper:
2839 case CCValAssign::AExt:
2840 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
2841 break;
2842 case CCValAssign::SExtUpper:
2843 case CCValAssign::SExt:
2844 Val = DAG.getNode(ISD::AssertSext, DL, LocVT, Val, DAG.getValueType(ValVT));
2845 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
2846 break;
2847 case CCValAssign::ZExtUpper:
2848 case CCValAssign::ZExt:
2849 Val = DAG.getNode(ISD::AssertZext, DL, LocVT, Val, DAG.getValueType(ValVT));
2850 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val);
2851 break;
2852 case CCValAssign::BCvt:
2853 Val = DAG.getNode(ISD::BITCAST, DL, ValVT, Val);
2854 break;
2855 }
2856
2857 return Val;
2858}
2859
Akira Hatanakae2489122011-04-15 21:51:11 +00002860//===----------------------------------------------------------------------===//
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002861// Formal Arguments Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002862//===----------------------------------------------------------------------===//
Wesley Peck527da1b2010-11-23 03:31:01 +00002863/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002864/// and generate load operations for arguments places on the stack.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002865SDValue
2866MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanakaaef55c82011-04-15 21:00:26 +00002867 CallingConv::ID CallConv,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002868 bool IsVarArg,
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00002869 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002870 SDLoc DL, SelectionDAG &DAG,
Akira Hatanakaaef55c82011-04-15 21:00:26 +00002871 SmallVectorImpl<SDValue> &InVals)
Akira Hatanakae2489122011-04-15 21:51:11 +00002872 const {
Bruno Cardoso Lopesa01ede22008-08-04 07:12:52 +00002873 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002874 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopes14033fb2007-08-28 05:08:16 +00002875 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002876
Dan Gohman31ae5862010-04-17 14:41:14 +00002877 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002878
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002879 // Used with vargs to acumulate store chains.
2880 std::vector<SDValue> OutChains;
2881
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002882 // Assign locations to all of the incoming arguments.
2883 SmallVector<CCValAssign, 16> ArgLocs;
Daniel Sanders23e98772014-11-02 16:09:29 +00002884 MipsCCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
2885 *DAG.getContext());
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002886 const MipsABIInfo &ABI = Subtarget.getABI();
2887 CCInfo.AllocateStack(ABI.GetCalleeAllocdArgSizeInBytes(CallConv), 1);
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002888 Function::const_arg_iterator FuncArg =
2889 DAG.getMachineFunction().getFunction()->arg_begin();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002890
Daniel Sandersb70e27c2014-11-06 16:36:30 +00002891 CCInfo.AnalyzeFormalArguments(Ins, CC_Mips_FixedArg);
Akira Hatanaka4866fe12012-10-30 19:37:25 +00002892 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(),
Daniel Sanders23e98772014-11-02 16:09:29 +00002893 CCInfo.getInRegsParamsCount() > 0);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002894
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00002895 unsigned CurArgIdx = 0;
Daniel Sanders23e98772014-11-02 16:09:29 +00002896 CCInfo.rewindByValRegsInfo();
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002897
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00002898 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002899 CCValAssign &VA = ArgLocs[i];
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00002900 std::advance(FuncArg, Ins[i].OrigArgIndex - CurArgIdx);
2901 CurArgIdx = Ins[i].OrigArgIndex;
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002902 EVT ValVT = VA.getValVT();
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002903 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2904 bool IsRegLoc = VA.isRegLoc();
2905
2906 if (Flags.isByVal()) {
Daniel Sanders23e98772014-11-02 16:09:29 +00002907 unsigned FirstByValReg, LastByValReg;
2908 unsigned ByValIdx = CCInfo.getInRegsParamsProcessed();
2909 CCInfo.getInRegsParamInfo(ByValIdx, FirstByValReg, LastByValReg);
2910
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002911 assert(Flags.getByValSize() &&
2912 "ByVal args of size 0 should have been ignored by front-end.");
Daniel Sanders23e98772014-11-02 16:09:29 +00002913 assert(ByValIdx < CCInfo.getInRegsParamsCount());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002914 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00002915 FirstByValReg, LastByValReg, VA, CCInfo);
Daniel Sanders23e98772014-11-02 16:09:29 +00002916 CCInfo.nextInRegsParam();
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002917 continue;
2918 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002919
2920 // Arguments stored on registers
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002921 if (IsRegLoc) {
Akira Hatanaka7d822522013-10-28 21:21:36 +00002922 MVT RegVT = VA.getLocVT();
Akira Hatanakacb4a1a82011-05-24 00:23:52 +00002923 unsigned ArgReg = VA.getLocReg();
Akira Hatanaka7d822522013-10-28 21:21:36 +00002924 const TargetRegisterClass *RC = getRegClassFor(RegVT);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002925
Wesley Peck527da1b2010-11-23 03:31:01 +00002926 // Transform the arguments stored on
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002927 // physical registers into virtual ones
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002928 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC);
2929 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
Wesley Peck527da1b2010-11-23 03:31:01 +00002930
Daniel Sandersc43cda82014-11-07 16:54:21 +00002931 ArgValue = UnpackFromArgumentSlot(ArgValue, VA, Ins[i].ArgVT, DL, DAG);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002932
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002933 // Handle floating point arguments passed in integer registers and
2934 // long double arguments passed in floating point registers.
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002935 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002936 (RegVT == MVT::i64 && ValVT == MVT::f64) ||
2937 (RegVT == MVT::f64 && ValVT == MVT::i64))
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002938 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
Eric Christopher1c29a652014-07-18 22:55:25 +00002939 else if (Subtarget.isABI_O32() && RegVT == MVT::i32 &&
Eric Christopherbf33a3c2014-07-02 23:18:40 +00002940 ValVT == MVT::f64) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002941 unsigned Reg2 = addLiveIn(DAG.getMachineFunction(),
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002942 getNextIntArgReg(ArgReg), RC);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002943 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT);
Eric Christopher1c29a652014-07-18 22:55:25 +00002944 if (!Subtarget.isLittle())
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002945 std::swap(ArgValue, ArgValue2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002946 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64,
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002947 ArgValue, ArgValue2);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002948 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002949
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002950 InVals.push_back(ArgValue);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002951 } else { // VA.isRegLoc()
Daniel Sandersc43cda82014-11-07 16:54:21 +00002952 MVT LocVT = VA.getLocVT();
2953
2954 if (Subtarget.isABI_O32()) {
2955 // We ought to be able to use LocVT directly but O32 sets it to i32
2956 // when allocating floating point values to integer registers.
2957 // This shouldn't influence how we load the value into registers unless
2958 // we are targetting softfloat.
2959 if (VA.getValVT().isFloatingPoint() && !Subtarget.abiUsesSoftFloat())
2960 LocVT = VA.getValVT();
2961 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002962
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002963 // sanity check
2964 assert(VA.isMemLoc());
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002965
Wesley Peck527da1b2010-11-23 03:31:01 +00002966 // The stack pointer offset is relative to the caller stack frame.
Daniel Sandersc43cda82014-11-07 16:54:21 +00002967 int FI = MFI->CreateFixedObject(LocVT.getSizeInBits() / 8,
Akira Hatanakacb4a1a82011-05-24 00:23:52 +00002968 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002969
2970 // Create load nodes to retrieve arguments from the stack
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002971 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Daniel Sandersc43cda82014-11-07 16:54:21 +00002972 SDValue ArgValue = DAG.getLoad(LocVT, DL, Chain, FIN,
2973 MachinePointerInfo::getFixedStack(FI),
2974 false, false, false, 0);
2975 OutChains.push_back(ArgValue.getValue(1));
2976
2977 ArgValue = UnpackFromArgumentSlot(ArgValue, VA, Ins[i].ArgVT, DL, DAG);
2978
2979 InVals.push_back(ArgValue);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002980 }
Reid Kleckner7a59e082014-05-12 22:01:27 +00002981 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002982
Reid Kleckner7a59e082014-05-12 22:01:27 +00002983 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Reid Kleckner79418562014-05-09 22:32:13 +00002984 // The mips ABIs for returning structs by value requires that we copy
2985 // the sret argument into $v0 for the return. Save the argument into
2986 // a virtual register so that we can access it from the return points.
Reid Kleckner7a59e082014-05-12 22:01:27 +00002987 if (Ins[i].Flags.isSRet()) {
Reid Kleckner79418562014-05-09 22:32:13 +00002988 unsigned Reg = MipsFI->getSRetReturnReg();
2989 if (!Reg) {
2990 Reg = MF.getRegInfo().createVirtualRegister(
Eric Christopher1c29a652014-07-18 22:55:25 +00002991 getRegClassFor(Subtarget.isABI_N64() ? MVT::i64 : MVT::i32));
Reid Kleckner79418562014-05-09 22:32:13 +00002992 MipsFI->setSRetReturnReg(Reg);
2993 }
Reid Kleckner7a59e082014-05-12 22:01:27 +00002994 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[i]);
Reid Kleckner79418562014-05-09 22:32:13 +00002995 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
Reid Kleckner7a59e082014-05-12 22:01:27 +00002996 break;
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002997 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002998 }
2999
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003000 if (IsVarArg)
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003001 writeVarArgRegs(OutChains, Chain, DL, DAG, CCInfo);
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003002
Wesley Peck527da1b2010-11-23 03:31:01 +00003003 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003004 // the size of Ins and InVals. This only happens when on varg functions
3005 if (!OutChains.empty()) {
3006 OutChains.push_back(Chain);
Craig Topper48d114b2014-04-26 18:35:24 +00003007 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00003008 }
3009
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003010 return Chain;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003011}
3012
Akira Hatanakae2489122011-04-15 21:51:11 +00003013//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003014// Return Value Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00003015//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003016
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003017bool
3018MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003019 MachineFunction &MF, bool IsVarArg,
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003020 const SmallVectorImpl<ISD::OutputArg> &Outs,
3021 LLVMContext &Context) const {
3022 SmallVector<CCValAssign, 16> RVLocs;
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003023 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00003024 return CCInfo.CheckReturn(Outs, RetCC_Mips);
3025}
3026
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003027SDValue
3028MipsTargetLowering::LowerReturn(SDValue Chain,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003029 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003030 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00003031 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003032 SDLoc DL, SelectionDAG &DAG) const {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003033 // CCValAssign - represent the assignment of
3034 // the return value to a location
3035 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003036 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003037
3038 // CCState - Info about the registers and stack slot.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003039 MipsCCState CCInfo(CallConv, IsVarArg, MF, RVLocs, *DAG.getContext());
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003040
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003041 // Analyze return values.
Daniel Sandersb3ca3382014-09-26 10:06:12 +00003042 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003043
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003044 SDValue Flag;
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003045 SmallVector<SDValue, 4> RetOps(1, Chain);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003046
3047 // Copy the result values into the output registers.
3048 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003049 SDValue Val = OutVals[i];
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003050 CCValAssign &VA = RVLocs[i];
3051 assert(VA.isRegLoc() && "Can only return in registers!");
Daniel Sandersae275e32014-09-25 12:15:05 +00003052 bool UseUpperBits = false;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003053
Daniel Sandersae275e32014-09-25 12:15:05 +00003054 switch (VA.getLocInfo()) {
3055 default:
3056 llvm_unreachable("Unknown loc info!");
3057 case CCValAssign::Full:
3058 break;
3059 case CCValAssign::BCvt:
3060 Val = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Val);
3061 break;
3062 case CCValAssign::AExtUpper:
3063 UseUpperBits = true;
3064 // Fallthrough
3065 case CCValAssign::AExt:
3066 Val = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Val);
3067 break;
3068 case CCValAssign::ZExtUpper:
3069 UseUpperBits = true;
3070 // Fallthrough
3071 case CCValAssign::ZExt:
3072 Val = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Val);
3073 break;
3074 case CCValAssign::SExtUpper:
3075 UseUpperBits = true;
3076 // Fallthrough
3077 case CCValAssign::SExt:
3078 Val = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Val);
3079 break;
3080 }
3081
3082 if (UseUpperBits) {
3083 unsigned ValSizeInBits = Outs[i].ArgVT.getSizeInBits();
3084 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
3085 Val = DAG.getNode(
3086 ISD::SHL, DL, VA.getLocVT(), Val,
3087 DAG.getConstant(LocSizeInBits - ValSizeInBits, VA.getLocVT()));
3088 }
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003089
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003090 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003091
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003092 // Guarantee that all emitted copies are stuck together with flags.
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003093 Flag = Chain.getValue(1);
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003094 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003095 }
3096
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003097 // The mips ABIs for returning structs by value requires that we copy
3098 // the sret argument into $v0 for the return. We saved the argument into
3099 // a virtual register in the entry block, so now we copy the value out
3100 // and into $v0.
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00003101 if (MF.getFunction()->hasStructRetAttr()) {
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003102 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3103 unsigned Reg = MipsFI->getSRetReturnReg();
3104
Wesley Peck527da1b2010-11-23 03:31:01 +00003105 if (!Reg)
Torok Edwinfbcc6632009-07-14 16:55:14 +00003106 llvm_unreachable("sret virtual register not created in the entry block");
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003107 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Eric Christopher1c29a652014-07-18 22:55:25 +00003108 unsigned V0 = Subtarget.isABI_N64() ? Mips::V0_64 : Mips::V0;
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003109
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003110 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003111 Flag = Chain.getValue(1);
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003112 RetOps.push_back(DAG.getRegister(V0, getPointerTy()));
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003113 }
3114
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003115 RetOps[0] = Chain; // Update chain.
Akira Hatanakaefff7b72012-07-10 00:19:06 +00003116
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003117 // Add the flag if we have it.
3118 if (Flag.getNode())
3119 RetOps.push_back(Flag);
3120
3121 // Return on Mips is always a "jr $ra"
Craig Topper48d114b2014-04-26 18:35:24 +00003122 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, RetOps);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003123}
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003124
Akira Hatanakae2489122011-04-15 21:51:11 +00003125//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003126// Mips Inline Assembly Support
Akira Hatanakae2489122011-04-15 21:51:11 +00003127//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003128
3129/// getConstraintType - Given a constraint letter, return the type of
3130/// constraint it is for this target.
3131MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peck527da1b2010-11-23 03:31:01 +00003132getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003133{
Daniel Sanders8b59af12013-11-12 12:56:01 +00003134 // Mips specific constraints
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003135 // GCC config/mips/constraints.md
3136 //
Wesley Peck527da1b2010-11-23 03:31:01 +00003137 // 'd' : An address register. Equivalent to r
3138 // unless generating MIPS16 code.
3139 // 'y' : Equivalent to r; retained for
3140 // backwards compatibility.
Eric Christophere3c494d2012-05-07 06:25:10 +00003141 // 'c' : A register suitable for use in an indirect
3142 // jump. This will always be $25 for -mabicalls.
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003143 // 'l' : The lo register. 1 word storage.
3144 // 'x' : The hilo register pair. Double word storage.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003145 if (Constraint.size() == 1) {
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003146 switch (Constraint[0]) {
3147 default : break;
Wesley Peck527da1b2010-11-23 03:31:01 +00003148 case 'd':
3149 case 'y':
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003150 case 'f':
Eric Christophere3c494d2012-05-07 06:25:10 +00003151 case 'c':
Eric Christopher9c492e62012-05-07 06:25:15 +00003152 case 'l':
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003153 case 'x':
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003154 return C_RegisterClass;
Jack Carter0e149b02013-03-04 21:33:15 +00003155 case 'R':
3156 return C_Memory;
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003157 }
3158 }
3159 return TargetLowering::getConstraintType(Constraint);
3160}
3161
John Thompsone8360b72010-10-29 17:29:13 +00003162/// Examine constraint type and operand type and determine a weight value.
3163/// This object must already have been set up with the operand type
3164/// and the current alternative constraint selected.
3165TargetLowering::ConstraintWeight
3166MipsTargetLowering::getSingleConstraintMatchWeight(
3167 AsmOperandInfo &info, const char *constraint) const {
3168 ConstraintWeight weight = CW_Invalid;
3169 Value *CallOperandVal = info.CallOperandVal;
3170 // If we don't have a value, we can't do a match,
3171 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +00003172 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +00003173 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +00003174 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +00003175 // Look at the constraint type.
3176 switch (*constraint) {
3177 default:
3178 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
3179 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00003180 case 'd':
3181 case 'y':
John Thompsone8360b72010-10-29 17:29:13 +00003182 if (type->isIntegerTy())
3183 weight = CW_Register;
3184 break;
Daniel Sanders8b59af12013-11-12 12:56:01 +00003185 case 'f': // FPU or MSA register
Eric Christopher1c29a652014-07-18 22:55:25 +00003186 if (Subtarget.hasMSA() && type->isVectorTy() &&
Daniel Sanders8b59af12013-11-12 12:56:01 +00003187 cast<VectorType>(type)->getBitWidth() == 128)
3188 weight = CW_Register;
3189 else if (type->isFloatTy())
John Thompsone8360b72010-10-29 17:29:13 +00003190 weight = CW_Register;
3191 break;
Eric Christophere3c494d2012-05-07 06:25:10 +00003192 case 'c': // $25 for indirect jumps
Eric Christopher9c492e62012-05-07 06:25:15 +00003193 case 'l': // lo register
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003194 case 'x': // hilo register pair
Daniel Sanders8b59af12013-11-12 12:56:01 +00003195 if (type->isIntegerTy())
Eric Christophere3c494d2012-05-07 06:25:10 +00003196 weight = CW_SpecificReg;
Daniel Sanders8b59af12013-11-12 12:56:01 +00003197 break;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003198 case 'I': // signed 16 bit immediate
Eric Christopher7201e1b2012-05-07 03:13:42 +00003199 case 'J': // integer zero
Eric Christopher3ff88a02012-05-07 05:46:29 +00003200 case 'K': // unsigned 16 bit immediate
Eric Christopher1109b342012-05-07 05:46:37 +00003201 case 'L': // signed 32 bit immediate where lower 16 bits are 0
Eric Christophere07aa432012-05-07 05:46:43 +00003202 case 'N': // immediate in the range of -65535 to -1 (inclusive)
Eric Christopher470578a2012-05-07 05:46:48 +00003203 case 'O': // signed 15 bit immediate (+- 16383)
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003204 case 'P': // immediate in the range of 65535 to 1 (inclusive)
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003205 if (isa<ConstantInt>(CallOperandVal))
3206 weight = CW_Constant;
3207 break;
Jack Carter0e149b02013-03-04 21:33:15 +00003208 case 'R':
3209 weight = CW_Memory;
3210 break;
John Thompsone8360b72010-10-29 17:29:13 +00003211 }
3212 return weight;
3213}
3214
Akira Hatanaka7473b472013-08-14 00:21:25 +00003215/// This is a helper function to parse a physical register string and split it
3216/// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag
3217/// that is returned indicates whether parsing was successful. The second flag
3218/// is true if the numeric part exists.
3219static std::pair<bool, bool>
Craig Topper6dc4a8bc2014-08-30 16:48:02 +00003220parsePhysicalReg(StringRef C, std::string &Prefix,
Akira Hatanaka7473b472013-08-14 00:21:25 +00003221 unsigned long long &Reg) {
3222 if (C.front() != '{' || C.back() != '}')
3223 return std::make_pair(false, false);
3224
3225 // Search for the first numeric character.
3226 StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1;
3227 I = std::find_if(B, E, std::ptr_fun(isdigit));
3228
3229 Prefix.assign(B, I - B);
3230
3231 // The second flag is set to false if no numeric characters were found.
3232 if (I == E)
3233 return std::make_pair(true, false);
3234
3235 // Parse the numeric characters.
3236 return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg),
3237 true);
3238}
3239
3240std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering::
Craig Topper6dc4a8bc2014-08-30 16:48:02 +00003241parseRegForInlineAsmConstraint(StringRef C, MVT VT) const {
Eric Christopherd9134482014-08-04 21:25:23 +00003242 const TargetRegisterInfo *TRI =
3243 getTargetMachine().getSubtargetImpl()->getRegisterInfo();
Akira Hatanaka7473b472013-08-14 00:21:25 +00003244 const TargetRegisterClass *RC;
3245 std::string Prefix;
3246 unsigned long long Reg;
3247
3248 std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg);
3249
3250 if (!R.first)
Craig Topper062a2ba2014-04-25 05:30:21 +00003251 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003252
3253 if ((Prefix == "hi" || Prefix == "lo")) { // Parse hi/lo.
3254 // No numeric characters follow "hi" or "lo".
3255 if (R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003256 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003257
3258 RC = TRI->getRegClass(Prefix == "hi" ?
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00003259 Mips::HI32RegClassID : Mips::LO32RegClassID);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003260 return std::make_pair(*(RC->begin()), RC);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003261 } else if (Prefix.compare(0, 4, "$msa") == 0) {
3262 // Parse $msa(ir|csr|access|save|modify|request|map|unmap)
3263
3264 // No numeric characters follow the name.
3265 if (R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003266 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003267
3268 Reg = StringSwitch<unsigned long long>(Prefix)
3269 .Case("$msair", Mips::MSAIR)
3270 .Case("$msacsr", Mips::MSACSR)
3271 .Case("$msaaccess", Mips::MSAAccess)
3272 .Case("$msasave", Mips::MSASave)
3273 .Case("$msamodify", Mips::MSAModify)
3274 .Case("$msarequest", Mips::MSARequest)
3275 .Case("$msamap", Mips::MSAMap)
3276 .Case("$msaunmap", Mips::MSAUnmap)
3277 .Default(0);
3278
3279 if (!Reg)
Craig Topper062a2ba2014-04-25 05:30:21 +00003280 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003281
3282 RC = TRI->getRegClass(Mips::MSACtrlRegClassID);
3283 return std::make_pair(Reg, RC);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003284 }
3285
3286 if (!R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003287 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003288
3289 if (Prefix == "$f") { // Parse $f0-$f31.
3290 // If the size of FP registers is 64-bit or Reg is an even number, select
3291 // the 64-bit register class. Otherwise, select the 32-bit register class.
3292 if (VT == MVT::Other)
Eric Christopher1c29a652014-07-18 22:55:25 +00003293 VT = (Subtarget.isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32;
Akira Hatanaka7473b472013-08-14 00:21:25 +00003294
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003295 RC = getRegClassFor(VT);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003296
3297 if (RC == &Mips::AFGR64RegClass) {
3298 assert(Reg % 2 == 0);
3299 Reg >>= 1;
3300 }
Daniel Sanders8b59af12013-11-12 12:56:01 +00003301 } else if (Prefix == "$fcc") // Parse $fcc0-$fcc7.
Akira Hatanaka7473b472013-08-14 00:21:25 +00003302 RC = TRI->getRegClass(Mips::FCCRegClassID);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003303 else if (Prefix == "$w") { // Parse $w0-$w31.
3304 RC = getRegClassFor((VT == MVT::Other) ? MVT::v16i8 : VT);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003305 } else { // Parse $0-$31.
3306 assert(Prefix == "$");
3307 RC = getRegClassFor((VT == MVT::Other) ? MVT::i32 : VT);
3308 }
3309
3310 assert(Reg < RC->getNumRegs());
3311 return std::make_pair(*(RC->begin() + Reg), RC);
3312}
3313
Eric Christophereaf77dc2011-06-29 19:33:04 +00003314/// Given a register class constraint, like 'r', if this corresponds directly
3315/// to an LLVM register class, return a register of 0 and the register class
3316/// pointer.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003317std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Chad Rosier295bd432013-06-22 18:37:38 +00003318getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003319{
3320 if (Constraint.size() == 1) {
3321 switch (Constraint[0]) {
Eric Christopher9519c082011-06-29 19:04:31 +00003322 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
3323 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003324 case 'r':
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003325 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
Eric Christopher1c29a652014-07-18 22:55:25 +00003326 if (Subtarget.inMips16Mode())
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003327 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003328 return std::make_pair(0U, &Mips::GPR32RegClass);
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003329 }
Eric Christopher1c29a652014-07-18 22:55:25 +00003330 if (VT == MVT::i64 && !Subtarget.isGP64bit())
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003331 return std::make_pair(0U, &Mips::GPR32RegClass);
Eric Christopher1c29a652014-07-18 22:55:25 +00003332 if (VT == MVT::i64 && Subtarget.isGP64bit())
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003333 return std::make_pair(0U, &Mips::GPR64RegClass);
Eric Christopher58daf042012-05-07 03:13:22 +00003334 // This will generate an error message
Craig Topper062a2ba2014-04-25 05:30:21 +00003335 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003336 case 'f': // FPU or MSA register
3337 if (VT == MVT::v16i8)
3338 return std::make_pair(0U, &Mips::MSA128BRegClass);
3339 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
3340 return std::make_pair(0U, &Mips::MSA128HRegClass);
3341 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
3342 return std::make_pair(0U, &Mips::MSA128WRegClass);
3343 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
3344 return std::make_pair(0U, &Mips::MSA128DRegClass);
3345 else if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00003346 return std::make_pair(0U, &Mips::FGR32RegClass);
Eric Christopher1c29a652014-07-18 22:55:25 +00003347 else if ((VT == MVT::f64) && (!Subtarget.isSingleFloat())) {
3348 if (Subtarget.isFP64bit())
Craig Topperc7242e02012-04-20 07:30:17 +00003349 return std::make_pair(0U, &Mips::FGR64RegClass);
3350 return std::make_pair(0U, &Mips::AFGR64RegClass);
Akira Hatanakac669d7a2012-01-04 02:45:01 +00003351 }
Eric Christophere3c494d2012-05-07 06:25:10 +00003352 break;
3353 case 'c': // register suitable for indirect jump
3354 if (VT == MVT::i32)
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003355 return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass);
Eric Christophere3c494d2012-05-07 06:25:10 +00003356 assert(VT == MVT::i64 && "Unexpected type.");
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003357 return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass);
Eric Christopher9c492e62012-05-07 06:25:15 +00003358 case 'l': // register suitable for indirect jump
3359 if (VT == MVT::i32)
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00003360 return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass);
3361 return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass);
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003362 case 'x': // register suitable for indirect jump
3363 // Fixme: Not triggering the use of both hi and low
3364 // This will generate an error message
Craig Topper062a2ba2014-04-25 05:30:21 +00003365 return std::make_pair(0U, nullptr);
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003366 }
3367 }
Akira Hatanaka7473b472013-08-14 00:21:25 +00003368
3369 std::pair<unsigned, const TargetRegisterClass *> R;
3370 R = parseRegForInlineAsmConstraint(Constraint, VT);
3371
3372 if (R.second)
3373 return R;
3374
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003375 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3376}
3377
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003378/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3379/// vector. If it is invalid, don't add anything to Ops.
3380void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3381 std::string &Constraint,
3382 std::vector<SDValue>&Ops,
3383 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +00003384 SDValue Result;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003385
3386 // Only support length 1 constraints for now.
3387 if (Constraint.length() > 1) return;
3388
3389 char ConstraintLetter = Constraint[0];
3390 switch (ConstraintLetter) {
3391 default: break; // This will fall through to the generic implementation
3392 case 'I': // Signed 16 bit constant
3393 // If this fails, the parent routine will give an error
3394 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3395 EVT Type = Op.getValueType();
3396 int64_t Val = C->getSExtValue();
3397 if (isInt<16>(Val)) {
3398 Result = DAG.getTargetConstant(Val, Type);
3399 break;
3400 }
3401 }
3402 return;
Eric Christopher7201e1b2012-05-07 03:13:42 +00003403 case 'J': // integer zero
3404 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3405 EVT Type = Op.getValueType();
3406 int64_t Val = C->getZExtValue();
3407 if (Val == 0) {
3408 Result = DAG.getTargetConstant(0, Type);
3409 break;
3410 }
3411 }
3412 return;
Eric Christopher3ff88a02012-05-07 05:46:29 +00003413 case 'K': // unsigned 16 bit immediate
3414 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3415 EVT Type = Op.getValueType();
3416 uint64_t Val = (uint64_t)C->getZExtValue();
3417 if (isUInt<16>(Val)) {
3418 Result = DAG.getTargetConstant(Val, Type);
3419 break;
3420 }
3421 }
3422 return;
Eric Christopher1109b342012-05-07 05:46:37 +00003423 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3424 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3425 EVT Type = Op.getValueType();
3426 int64_t Val = C->getSExtValue();
3427 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
3428 Result = DAG.getTargetConstant(Val, Type);
3429 break;
3430 }
3431 }
3432 return;
Eric Christophere07aa432012-05-07 05:46:43 +00003433 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3434 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3435 EVT Type = Op.getValueType();
3436 int64_t Val = C->getSExtValue();
3437 if ((Val >= -65535) && (Val <= -1)) {
3438 Result = DAG.getTargetConstant(Val, Type);
3439 break;
3440 }
3441 }
3442 return;
Eric Christopher470578a2012-05-07 05:46:48 +00003443 case 'O': // signed 15 bit immediate
3444 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3445 EVT Type = Op.getValueType();
3446 int64_t Val = C->getSExtValue();
3447 if ((isInt<15>(Val))) {
3448 Result = DAG.getTargetConstant(Val, Type);
3449 break;
3450 }
3451 }
3452 return;
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003453 case 'P': // immediate in the range of 1 to 65535 (inclusive)
3454 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3455 EVT Type = Op.getValueType();
3456 int64_t Val = C->getSExtValue();
3457 if ((Val <= 65535) && (Val >= 1)) {
3458 Result = DAG.getTargetConstant(Val, Type);
3459 break;
3460 }
3461 }
3462 return;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003463 }
3464
3465 if (Result.getNode()) {
3466 Ops.push_back(Result);
3467 return;
3468 }
3469
3470 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3471}
3472
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003473bool MipsTargetLowering::isLegalAddressingMode(const AddrMode &AM,
3474 Type *Ty) const {
Akira Hatanakaef839192012-11-17 00:25:41 +00003475 // No global is ever allowed as a base.
3476 if (AM.BaseGV)
3477 return false;
3478
3479 switch (AM.Scale) {
3480 case 0: // "r+i" or just "i", depending on HasBaseReg.
3481 break;
3482 case 1:
3483 if (!AM.HasBaseReg) // allow "r+i".
3484 break;
3485 return false; // disallow "r+r" or "r+r+i".
3486 default:
3487 return false;
3488 }
3489
3490 return true;
3491}
3492
3493bool
Dan Gohman2fe6bee2008-10-18 02:06:02 +00003494MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3495 // The Mips target isn't yet aware of offsets.
3496 return false;
3497}
Evan Cheng16993aa2009-10-27 19:56:55 +00003498
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003499EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003500 unsigned SrcAlign,
3501 bool IsMemset, bool ZeroMemset,
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003502 bool MemcpyStrSrc,
3503 MachineFunction &MF) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003504 if (Subtarget.hasMips64())
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003505 return MVT::i64;
3506
3507 return MVT::i32;
3508}
3509
Evan Cheng83896a52009-10-28 01:43:28 +00003510bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
3511 if (VT != MVT::f32 && VT != MVT::f64)
3512 return false;
Bruno Cardoso Lopesb02a9df2011-01-18 19:41:41 +00003513 if (Imm.isNegZero())
3514 return false;
Evan Cheng16993aa2009-10-27 19:56:55 +00003515 return Imm.isZero();
3516}
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003517
3518unsigned MipsTargetLowering::getJumpTableEncoding() const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003519 if (Subtarget.isABI_N64())
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003520 return MachineJumpTableInfo::EK_GPRel64BlockAddress;
Jia Liuf54f60f2012-02-28 07:46:26 +00003521
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003522 return TargetLowering::getJumpTableEncoding();
3523}
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003524
Daniel Sandersf43e6872014-11-01 18:44:56 +00003525void MipsTargetLowering::copyByValRegs(
3526 SDValue Chain, SDLoc DL, std::vector<SDValue> &OutChains, SelectionDAG &DAG,
3527 const ISD::ArgFlagsTy &Flags, SmallVectorImpl<SDValue> &InVals,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003528 const Argument *FuncArg, unsigned FirstReg, unsigned LastReg,
3529 const CCValAssign &VA, MipsCCState &State) const {
Akira Hatanaka25dad192012-10-27 00:10:18 +00003530 MachineFunction &MF = DAG.getMachineFunction();
3531 MachineFrameInfo *MFI = MF.getFrameInfo();
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003532 unsigned GPRSizeInBytes = Subtarget.getGPRSizeInBytes();
Daniel Sanders23e98772014-11-02 16:09:29 +00003533 unsigned NumRegs = LastReg - FirstReg;
3534 unsigned RegAreaSize = NumRegs * GPRSizeInBytes;
Akira Hatanaka25dad192012-10-27 00:10:18 +00003535 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
3536 int FrameObjOffset;
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003537 const MipsABIInfo &ABI = Subtarget.getABI();
3538 ArrayRef<MCPhysReg> ByValArgRegs = ABI.GetByValArgRegs();
Akira Hatanaka25dad192012-10-27 00:10:18 +00003539
3540 if (RegAreaSize)
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003541 FrameObjOffset =
3542 (int)ABI.GetCalleeAllocdArgSizeInBytes(State.getCallingConv()) -
3543 (int)((ByValArgRegs.size() - FirstReg) * GPRSizeInBytes);
Akira Hatanaka25dad192012-10-27 00:10:18 +00003544 else
Daniel Sandersf43e6872014-11-01 18:44:56 +00003545 FrameObjOffset = VA.getLocMemOffset();
Akira Hatanaka25dad192012-10-27 00:10:18 +00003546
3547 // Create frame object.
3548 EVT PtrTy = getPointerTy();
3549 int FI = MFI->CreateFixedObject(FrameObjSize, FrameObjOffset, true);
3550 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
3551 InVals.push_back(FIN);
3552
Daniel Sanders23e98772014-11-02 16:09:29 +00003553 if (!NumRegs)
Akira Hatanaka25dad192012-10-27 00:10:18 +00003554 return;
3555
3556 // Copy arg registers.
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003557 MVT RegTy = MVT::getIntegerVT(GPRSizeInBytes * 8);
Akira Hatanaka25dad192012-10-27 00:10:18 +00003558 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3559
Daniel Sanders23e98772014-11-02 16:09:29 +00003560 for (unsigned I = 0; I < NumRegs; ++I) {
Daniel Sandersd7eba312014-11-07 12:21:37 +00003561 unsigned ArgReg = ByValArgRegs[FirstReg + I];
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003562 unsigned VReg = addLiveIn(MF, ArgReg, RC);
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003563 unsigned Offset = I * GPRSizeInBytes;
Akira Hatanaka25dad192012-10-27 00:10:18 +00003564 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
3565 DAG.getConstant(Offset, PtrTy));
3566 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
3567 StorePtr, MachinePointerInfo(FuncArg, Offset),
3568 false, false, 0);
3569 OutChains.push_back(Store);
3570 }
3571}
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003572
3573// Copy byVal arg to registers and stack.
Daniel Sandersf43e6872014-11-01 18:44:56 +00003574void MipsTargetLowering::passByValArg(
3575 SDValue Chain, SDLoc DL,
3576 std::deque<std::pair<unsigned, SDValue>> &RegsToPass,
3577 SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003578 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg, unsigned FirstReg,
3579 unsigned LastReg, const ISD::ArgFlagsTy &Flags, bool isLittle,
3580 const CCValAssign &VA) const {
Daniel Sandersac272632014-05-23 13:18:02 +00003581 unsigned ByValSizeInBytes = Flags.getByValSize();
3582 unsigned OffsetInBytes = 0; // From beginning of struct
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003583 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
Daniel Sandersac272632014-05-23 13:18:02 +00003584 unsigned Alignment = std::min(Flags.getByValAlign(), RegSizeInBytes);
3585 EVT PtrTy = getPointerTy(), RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
Daniel Sanders23e98772014-11-02 16:09:29 +00003586 unsigned NumRegs = LastReg - FirstReg;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003587
Daniel Sanders23e98772014-11-02 16:09:29 +00003588 if (NumRegs) {
Daniel Sandersd7eba312014-11-07 12:21:37 +00003589 const ArrayRef<MCPhysReg> ArgRegs = Subtarget.getABI().GetByValArgRegs();
Daniel Sanders23e98772014-11-02 16:09:29 +00003590 bool LeftoverBytes = (NumRegs * RegSizeInBytes > ByValSizeInBytes);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003591 unsigned I = 0;
3592
3593 // Copy words to registers.
Daniel Sanders23e98772014-11-02 16:09:29 +00003594 for (; I < NumRegs - LeftoverBytes; ++I, OffsetInBytes += RegSizeInBytes) {
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003595 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Daniel Sandersac272632014-05-23 13:18:02 +00003596 DAG.getConstant(OffsetInBytes, PtrTy));
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003597 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
3598 MachinePointerInfo(), false, false, false,
3599 Alignment);
3600 MemOpChains.push_back(LoadVal.getValue(1));
Daniel Sanders23e98772014-11-02 16:09:29 +00003601 unsigned ArgReg = ArgRegs[FirstReg + I];
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003602 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
3603 }
3604
3605 // Return if the struct has been fully copied.
Daniel Sandersac272632014-05-23 13:18:02 +00003606 if (ByValSizeInBytes == OffsetInBytes)
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003607 return;
3608
3609 // Copy the remainder of the byval argument with sub-word loads and shifts.
3610 if (LeftoverBytes) {
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003611 SDValue Val;
3612
Daniel Sandersac272632014-05-23 13:18:02 +00003613 for (unsigned LoadSizeInBytes = RegSizeInBytes / 2, TotalBytesLoaded = 0;
3614 OffsetInBytes < ByValSizeInBytes; LoadSizeInBytes /= 2) {
3615 unsigned RemainingSizeInBytes = ByValSizeInBytes - OffsetInBytes;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003616
Daniel Sandersac272632014-05-23 13:18:02 +00003617 if (RemainingSizeInBytes < LoadSizeInBytes)
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003618 continue;
3619
3620 // Load subword.
3621 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Daniel Sandersac272632014-05-23 13:18:02 +00003622 DAG.getConstant(OffsetInBytes, PtrTy));
3623 SDValue LoadVal = DAG.getExtLoad(
3624 ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr, MachinePointerInfo(),
Louis Gerbarg67474e32014-07-31 21:45:05 +00003625 MVT::getIntegerVT(LoadSizeInBytes * 8), false, false, false,
3626 Alignment);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003627 MemOpChains.push_back(LoadVal.getValue(1));
3628
3629 // Shift the loaded value.
3630 unsigned Shamt;
3631
3632 if (isLittle)
Daniel Sandersac272632014-05-23 13:18:02 +00003633 Shamt = TotalBytesLoaded * 8;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003634 else
Daniel Sandersac272632014-05-23 13:18:02 +00003635 Shamt = (RegSizeInBytes - (TotalBytesLoaded + LoadSizeInBytes)) * 8;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003636
3637 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
3638 DAG.getConstant(Shamt, MVT::i32));
3639
3640 if (Val.getNode())
3641 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
3642 else
3643 Val = Shift;
3644
Daniel Sandersac272632014-05-23 13:18:02 +00003645 OffsetInBytes += LoadSizeInBytes;
3646 TotalBytesLoaded += LoadSizeInBytes;
3647 Alignment = std::min(Alignment, LoadSizeInBytes);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003648 }
3649
Daniel Sanders23e98772014-11-02 16:09:29 +00003650 unsigned ArgReg = ArgRegs[FirstReg + I];
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003651 RegsToPass.push_back(std::make_pair(ArgReg, Val));
3652 return;
3653 }
3654 }
3655
3656 // Copy remainder of byval arg to it with memcpy.
Daniel Sandersac272632014-05-23 13:18:02 +00003657 unsigned MemCpySize = ByValSizeInBytes - OffsetInBytes;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003658 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Daniel Sandersac272632014-05-23 13:18:02 +00003659 DAG.getConstant(OffsetInBytes, PtrTy));
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003660 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
Daniel Sandersf43e6872014-11-01 18:44:56 +00003661 DAG.getIntPtrConstant(VA.getLocMemOffset()));
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003662 Chain = DAG.getMemcpy(Chain, DL, Dst, Src, DAG.getConstant(MemCpySize, PtrTy),
3663 Alignment, /*isVolatile=*/false, /*AlwaysInline=*/false,
Nick Lewyckyaad475b2014-04-15 07:22:52 +00003664 MachinePointerInfo(), MachinePointerInfo());
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003665 MemOpChains.push_back(Chain);
3666}
Akira Hatanaka2a134022012-10-27 00:21:13 +00003667
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003668void MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
Daniel Sandersb315c8c2014-11-07 15:33:08 +00003669 SDValue Chain, SDLoc DL,
3670 SelectionDAG &DAG,
Daniel Sanders853c2432014-11-01 18:13:52 +00003671 CCState &State) const {
Daniel Sandersd7eba312014-11-07 12:21:37 +00003672 const ArrayRef<MCPhysReg> ArgRegs = Subtarget.getABI().GetVarArgRegs();
Daniel Sanders853c2432014-11-01 18:13:52 +00003673 unsigned Idx = State.getFirstUnallocated(ArgRegs.data(), ArgRegs.size());
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003674 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
3675 MVT RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003676 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3677 MachineFunction &MF = DAG.getMachineFunction();
3678 MachineFrameInfo *MFI = MF.getFrameInfo();
3679 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3680
3681 // Offset of the first variable argument from stack pointer.
3682 int VaArgOffset;
3683
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003684 if (ArgRegs.size() == Idx)
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003685 VaArgOffset =
Daniel Sanders853c2432014-11-01 18:13:52 +00003686 RoundUpToAlignment(State.getNextStackOffset(), RegSizeInBytes);
Daniel Sanders2c6f4b42014-11-07 15:03:53 +00003687 else {
3688 const MipsABIInfo &ABI = Subtarget.getABI();
3689 VaArgOffset =
3690 (int)ABI.GetCalleeAllocdArgSizeInBytes(State.getCallingConv()) -
3691 (int)(RegSizeInBytes * (ArgRegs.size() - Idx));
3692 }
Akira Hatanaka2a134022012-10-27 00:21:13 +00003693
3694 // Record the frame index of the first variable argument
3695 // which is a value necessary to VASTART.
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003696 int FI = MFI->CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003697 MipsFI->setVarArgsFrameIndex(FI);
3698
3699 // Copy the integer registers that have not been used for argument passing
3700 // to the argument register save area. For O32, the save area is allocated
3701 // in the caller's stack frame, while for N32/64, it is allocated in the
3702 // callee's stack frame.
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003703 for (unsigned I = Idx; I < ArgRegs.size();
3704 ++I, VaArgOffset += RegSizeInBytes) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003705 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003706 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003707 FI = MFI->CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003708 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
3709 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
3710 MachinePointerInfo(), false, false, 0);
Eric Christopher1c29a652014-07-18 22:55:25 +00003711 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(
3712 (Value *)nullptr);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003713 OutChains.push_back(Store);
3714 }
3715}
Daniel Sanders23e98772014-11-02 16:09:29 +00003716
3717void MipsTargetLowering::HandleByVal(CCState *State, unsigned &Size,
3718 unsigned Align) const {
3719 MachineFunction &MF = State->getMachineFunction();
3720 const TargetFrameLowering *TFL = MF.getSubtarget().getFrameLowering();
3721
3722 assert(Size && "Byval argument's size shouldn't be 0.");
3723
3724 Align = std::min(Align, TFL->getStackAlignment());
3725
3726 unsigned FirstReg = 0;
3727 unsigned NumRegs = 0;
3728
3729 if (State->getCallingConv() != CallingConv::Fast) {
3730 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
3731 const ArrayRef<MCPhysReg> IntArgRegs = Subtarget.getABI().GetByValArgRegs();
3732 // FIXME: The O32 case actually describes no shadow registers.
3733 const MCPhysReg *ShadowRegs =
3734 Subtarget.isABI_O32() ? IntArgRegs.data() : Mips64DPRegs;
3735
3736 // We used to check the size as well but we can't do that anymore since
3737 // CCState::HandleByVal() rounds up the size after calling this function.
3738 assert(!(Align % RegSizeInBytes) &&
3739 "Byval argument's alignment should be a multiple of"
3740 "RegSizeInBytes.");
3741
3742 FirstReg = State->getFirstUnallocated(IntArgRegs.data(), IntArgRegs.size());
3743
3744 // If Align > RegSizeInBytes, the first arg register must be even.
3745 // FIXME: This condition happens to do the right thing but it's not the
3746 // right way to test it. We want to check that the stack frame offset
3747 // of the register is aligned.
3748 if ((Align > RegSizeInBytes) && (FirstReg % 2)) {
3749 State->AllocateReg(IntArgRegs[FirstReg], ShadowRegs[FirstReg]);
3750 ++FirstReg;
3751 }
3752
3753 // Mark the registers allocated.
3754 Size = RoundUpToAlignment(Size, RegSizeInBytes);
3755 for (unsigned I = FirstReg; Size > 0 && (I < IntArgRegs.size());
3756 Size -= RegSizeInBytes, ++I, ++NumRegs)
3757 State->AllocateReg(IntArgRegs[I], ShadowRegs[I]);
3758 }
3759
3760 State->addInRegsParamInfo(FirstReg, FirstReg + NumRegs);
3761}
Vasileios Kalintirisf53f7852014-12-12 14:41:37 +00003762
3763MachineBasicBlock *
3764MipsTargetLowering::emitPseudoSELECT(MachineInstr *MI, MachineBasicBlock *BB,
3765 bool isFPCmp, unsigned Opc) const {
3766 assert(!(Subtarget.hasMips4() || Subtarget.hasMips32()) &&
3767 "Subtarget already supports SELECT nodes with the use of"
3768 "conditional-move instructions.");
3769
3770 const TargetInstrInfo *TII =
3771 getTargetMachine().getSubtargetImpl()->getInstrInfo();
3772 DebugLoc DL = MI->getDebugLoc();
3773
3774 // To "insert" a SELECT instruction, we actually have to insert the
3775 // diamond control-flow pattern. The incoming instruction knows the
3776 // destination vreg to set, the condition code register to branch on, the
3777 // true/false values to select between, and a branch opcode to use.
3778 const BasicBlock *LLVM_BB = BB->getBasicBlock();
3779 MachineFunction::iterator It = BB;
3780 ++It;
3781
3782 // thisMBB:
3783 // ...
3784 // TrueVal = ...
3785 // setcc r1, r2, r3
3786 // bNE r1, r0, copy1MBB
3787 // fallthrough --> copy0MBB
3788 MachineBasicBlock *thisMBB = BB;
3789 MachineFunction *F = BB->getParent();
3790 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
3791 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
3792 F->insert(It, copy0MBB);
3793 F->insert(It, sinkMBB);
3794
3795 // Transfer the remainder of BB and its successor edges to sinkMBB.
3796 sinkMBB->splice(sinkMBB->begin(), BB,
3797 std::next(MachineBasicBlock::iterator(MI)), BB->end());
3798 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
3799
3800 // Next, add the true and fallthrough blocks as its successors.
3801 BB->addSuccessor(copy0MBB);
3802 BB->addSuccessor(sinkMBB);
3803
3804 if (isFPCmp) {
3805 // bc1[tf] cc, sinkMBB
3806 BuildMI(BB, DL, TII->get(Opc))
3807 .addReg(MI->getOperand(1).getReg())
3808 .addMBB(sinkMBB);
3809 } else {
3810 // bne rs, $0, sinkMBB
3811 BuildMI(BB, DL, TII->get(Opc))
3812 .addReg(MI->getOperand(1).getReg())
3813 .addReg(Mips::ZERO)
3814 .addMBB(sinkMBB);
3815 }
3816
3817 // copy0MBB:
3818 // %FalseValue = ...
3819 // # fallthrough to sinkMBB
3820 BB = copy0MBB;
3821
3822 // Update machine-CFG edges
3823 BB->addSuccessor(sinkMBB);
3824
3825 // sinkMBB:
3826 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
3827 // ...
3828 BB = sinkMBB;
3829
3830 BuildMI(*BB, BB->begin(), DL,
3831 TII->get(Mips::PHI), MI->getOperand(0).getReg())
3832 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
3833 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB);
3834
3835 MI->eraseFromParent(); // The pseudo instruction is gone now.
3836
3837 return BB;
3838}