blob: df73b6d3fb7cd8f6302e29ff525665c6db773e78 [file] [log] [blame]
Jia Liuf54f60f2012-02-28 07:46:26 +00001//===-- MipsISelLowering.cpp - Mips DAG Lowering Implementation -----------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00007//
Akira Hatanakae2489122011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00009//
10// This file defines the interfaces that Mips uses to lower LLVM code into a
11// selection DAG.
12//
Akira Hatanakae2489122011-04-15 21:51:11 +000013//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000014#include "MipsISelLowering.h"
Craig Topperb25fda92012-03-17 18:46:09 +000015#include "InstPrinter/MipsInstPrinter.h"
16#include "MCTargetDesc/MipsBaseInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "MipsMachineFunction.h"
18#include "MipsSubtarget.h"
19#include "MipsTargetMachine.h"
20#include "MipsTargetObjectFile.h"
Akira Hatanaka90131ac2012-10-19 21:47:33 +000021#include "llvm/ADT/Statistic.h"
Daniel Sanders8b59af12013-11-12 12:56:01 +000022#include "llvm/ADT/StringSwitch.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000023#include "llvm/CodeGen/CallingConvLower.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/MachineFunction.h"
26#include "llvm/CodeGen/MachineInstrBuilder.h"
Eric Christopher79cc1e32014-09-02 22:28:02 +000027#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000028#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000029#include "llvm/CodeGen/SelectionDAGISel.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000030#include "llvm/CodeGen/ValueTypes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000031#include "llvm/IR/CallingConv.h"
32#include "llvm/IR/DerivedTypes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000033#include "llvm/IR/GlobalVariable.h"
Akira Hatanaka90131ac2012-10-19 21:47:33 +000034#include "llvm/Support/CommandLine.h"
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000035#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000036#include "llvm/Support/ErrorHandling.h"
NAKAMURA Takumie30303f2012-04-21 15:31:45 +000037#include "llvm/Support/raw_ostream.h"
Akira Hatanaka7473b472013-08-14 00:21:25 +000038#include <cctype>
NAKAMURA Takumie30303f2012-04-21 15:31:45 +000039
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +000040using namespace llvm;
41
Chandler Carruth84e68b22014-04-22 02:41:26 +000042#define DEBUG_TYPE "mips-lower"
43
Akira Hatanaka90131ac2012-10-19 21:47:33 +000044STATISTIC(NumTailCalls, "Number of tail calls");
45
46static cl::opt<bool>
Akira Hatanaka59f299f2012-11-21 20:21:11 +000047LargeGOT("mxgot", cl::Hidden,
48 cl::desc("MIPS: Enable GOT larger than 64k."), cl::init(false));
49
Akira Hatanaka1cb02422013-05-20 18:07:43 +000050static cl::opt<bool>
Akira Hatanakabe76cd02013-05-21 17:17:59 +000051NoZeroDivCheck("mno-check-zero-division", cl::Hidden,
Akira Hatanaka1cb02422013-05-20 18:07:43 +000052 cl::desc("MIPS: Don't trap on integer division by zero."),
53 cl::init(false));
54
Reed Kotler720c5ca2014-04-17 22:15:34 +000055cl::opt<bool>
56EnableMipsFastISel("mips-fast-isel", cl::Hidden,
57 cl::desc("Allow mips-fast-isel to be used"),
58 cl::init(false));
59
Craig Topper840beec2014-04-04 05:16:06 +000060static const MCPhysReg O32IntRegs[4] = {
Akira Hatanakaac8c6692012-10-27 00:29:43 +000061 Mips::A0, Mips::A1, Mips::A2, Mips::A3
62};
63
Craig Topper840beec2014-04-04 05:16:06 +000064static const MCPhysReg Mips64IntRegs[8] = {
Akira Hatanakaac8c6692012-10-27 00:29:43 +000065 Mips::A0_64, Mips::A1_64, Mips::A2_64, Mips::A3_64,
66 Mips::T0_64, Mips::T1_64, Mips::T2_64, Mips::T3_64
67};
68
Craig Topper840beec2014-04-04 05:16:06 +000069static const MCPhysReg Mips64DPRegs[8] = {
Akira Hatanakaac8c6692012-10-27 00:29:43 +000070 Mips::D12_64, Mips::D13_64, Mips::D14_64, Mips::D15_64,
71 Mips::D16_64, Mips::D17_64, Mips::D18_64, Mips::D19_64
72};
73
Jia Liuf54f60f2012-02-28 07:46:26 +000074// If I is a shifted mask, set the size (Size) and the first bit of the
Akira Hatanaka73d78b72011-08-18 20:07:42 +000075// mask (Pos), and return true.
Jia Liuf54f60f2012-02-28 07:46:26 +000076// For example, if I is 0x003ff800, (Pos, Size) = (11, 11).
Akira Hatanaka0bb60d892013-03-12 00:16:36 +000077static bool isShiftedMask(uint64_t I, uint64_t &Pos, uint64_t &Size) {
Akira Hatanaka20cee2e2011-12-05 21:26:34 +000078 if (!isShiftedMask_64(I))
Akira Hatanaka4c0a7122013-10-07 19:33:02 +000079 return false;
Akira Hatanaka5360f882011-08-17 02:05:42 +000080
Akira Hatanaka20cee2e2011-12-05 21:26:34 +000081 Size = CountPopulation_64(I);
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +000082 Pos = countTrailingZeros(I);
Akira Hatanaka73d78b72011-08-18 20:07:42 +000083 return true;
Akira Hatanaka5360f882011-08-17 02:05:42 +000084}
85
Akira Hatanaka96ca1822013-03-13 00:54:29 +000086SDValue MipsTargetLowering::getGlobalReg(SelectionDAG &DAG, EVT Ty) const {
Akira Hatanakab049aef2012-02-24 22:34:47 +000087 MipsFunctionInfo *FI = DAG.getMachineFunction().getInfo<MipsFunctionInfo>();
88 return DAG.getRegister(FI->getGlobalBaseReg(), Ty);
89}
90
Akira Hatanakad8f10ce2013-09-27 19:51:35 +000091SDValue MipsTargetLowering::getTargetNode(GlobalAddressSDNode *N, EVT Ty,
92 SelectionDAG &DAG,
Akira Hatanaka96ca1822013-03-13 00:54:29 +000093 unsigned Flag) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +000094 return DAG.getTargetGlobalAddress(N->getGlobal(), SDLoc(N), Ty, 0, Flag);
Akira Hatanakafd04ad42012-11-21 20:26:38 +000095}
96
Akira Hatanakad8f10ce2013-09-27 19:51:35 +000097SDValue MipsTargetLowering::getTargetNode(ExternalSymbolSDNode *N, EVT Ty,
98 SelectionDAG &DAG,
99 unsigned Flag) const {
100 return DAG.getTargetExternalSymbol(N->getSymbol(), Ty, Flag);
101}
102
103SDValue MipsTargetLowering::getTargetNode(BlockAddressSDNode *N, EVT Ty,
104 SelectionDAG &DAG,
105 unsigned Flag) const {
106 return DAG.getTargetBlockAddress(N->getBlockAddress(), Ty, 0, Flag);
107}
108
109SDValue MipsTargetLowering::getTargetNode(JumpTableSDNode *N, EVT Ty,
110 SelectionDAG &DAG,
111 unsigned Flag) const {
112 return DAG.getTargetJumpTable(N->getIndex(), Ty, Flag);
113}
114
115SDValue MipsTargetLowering::getTargetNode(ConstantPoolSDNode *N, EVT Ty,
116 SelectionDAG &DAG,
117 unsigned Flag) const {
118 return DAG.getTargetConstantPool(N->getConstVal(), Ty, N->getAlignment(),
119 N->getOffset(), Flag);
Akira Hatanakafd04ad42012-11-21 20:26:38 +0000120}
121
Chris Lattner5e693ed2009-07-28 03:13:23 +0000122const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
123 switch (Opcode) {
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000124 case MipsISD::JmpLink: return "MipsISD::JmpLink";
Akira Hatanaka91318df2012-10-19 20:59:39 +0000125 case MipsISD::TailCall: return "MipsISD::TailCall";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000126 case MipsISD::Hi: return "MipsISD::Hi";
127 case MipsISD::Lo: return "MipsISD::Lo";
128 case MipsISD::GPRel: return "MipsISD::GPRel";
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +0000129 case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000130 case MipsISD::Ret: return "MipsISD::Ret";
Akira Hatanakac0b02062013-01-30 00:26:49 +0000131 case MipsISD::EH_RETURN: return "MipsISD::EH_RETURN";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000132 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
133 case MipsISD::FPCmp: return "MipsISD::FPCmp";
134 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
135 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000136 case MipsISD::TruncIntFP: return "MipsISD::TruncIntFP";
Akira Hatanakad98c99f2013-10-15 01:12:50 +0000137 case MipsISD::MFHI: return "MipsISD::MFHI";
138 case MipsISD::MFLO: return "MipsISD::MFLO";
139 case MipsISD::MTLOHI: return "MipsISD::MTLOHI";
Akira Hatanaka28721bd2013-03-30 01:14:04 +0000140 case MipsISD::Mult: return "MipsISD::Mult";
141 case MipsISD::Multu: return "MipsISD::Multu";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000142 case MipsISD::MAdd: return "MipsISD::MAdd";
143 case MipsISD::MAddu: return "MipsISD::MAddu";
144 case MipsISD::MSub: return "MipsISD::MSub";
145 case MipsISD::MSubu: return "MipsISD::MSubu";
146 case MipsISD::DivRem: return "MipsISD::DivRem";
147 case MipsISD::DivRemU: return "MipsISD::DivRemU";
Akira Hatanaka28721bd2013-03-30 01:14:04 +0000148 case MipsISD::DivRem16: return "MipsISD::DivRem16";
149 case MipsISD::DivRemU16: return "MipsISD::DivRemU16";
Akira Hatanaka9dbb45b2011-05-23 21:13:59 +0000150 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
151 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
Akira Hatanakafaa88c02011-12-12 22:38:19 +0000152 case MipsISD::Wrapper: return "MipsISD::Wrapper";
Akira Hatanakaa4c09bc2011-07-19 23:30:50 +0000153 case MipsISD::Sync: return "MipsISD::Sync";
Akira Hatanaka5360f882011-08-17 02:05:42 +0000154 case MipsISD::Ext: return "MipsISD::Ext";
155 case MipsISD::Ins: return "MipsISD::Ins";
Akira Hatanakab9ebf8d2012-06-02 00:03:12 +0000156 case MipsISD::LWL: return "MipsISD::LWL";
157 case MipsISD::LWR: return "MipsISD::LWR";
158 case MipsISD::SWL: return "MipsISD::SWL";
159 case MipsISD::SWR: return "MipsISD::SWR";
160 case MipsISD::LDL: return "MipsISD::LDL";
161 case MipsISD::LDR: return "MipsISD::LDR";
162 case MipsISD::SDL: return "MipsISD::SDL";
163 case MipsISD::SDR: return "MipsISD::SDR";
Akira Hatanaka233ac532012-09-21 23:52:47 +0000164 case MipsISD::EXTP: return "MipsISD::EXTP";
165 case MipsISD::EXTPDP: return "MipsISD::EXTPDP";
166 case MipsISD::EXTR_S_H: return "MipsISD::EXTR_S_H";
167 case MipsISD::EXTR_W: return "MipsISD::EXTR_W";
168 case MipsISD::EXTR_R_W: return "MipsISD::EXTR_R_W";
169 case MipsISD::EXTR_RS_W: return "MipsISD::EXTR_RS_W";
170 case MipsISD::SHILO: return "MipsISD::SHILO";
171 case MipsISD::MTHLIP: return "MipsISD::MTHLIP";
172 case MipsISD::MULT: return "MipsISD::MULT";
173 case MipsISD::MULTU: return "MipsISD::MULTU";
Jia Liu434874d2013-03-04 01:06:54 +0000174 case MipsISD::MADD_DSP: return "MipsISD::MADD_DSP";
Akira Hatanaka233ac532012-09-21 23:52:47 +0000175 case MipsISD::MADDU_DSP: return "MipsISD::MADDU_DSP";
176 case MipsISD::MSUB_DSP: return "MipsISD::MSUB_DSP";
177 case MipsISD::MSUBU_DSP: return "MipsISD::MSUBU_DSP";
Akira Hatanaka1ebb2a12013-04-19 23:21:32 +0000178 case MipsISD::SHLL_DSP: return "MipsISD::SHLL_DSP";
179 case MipsISD::SHRA_DSP: return "MipsISD::SHRA_DSP";
180 case MipsISD::SHRL_DSP: return "MipsISD::SHRL_DSP";
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000181 case MipsISD::SETCC_DSP: return "MipsISD::SETCC_DSP";
182 case MipsISD::SELECT_CC_DSP: return "MipsISD::SELECT_CC_DSP";
Daniel Sandersce09d072013-08-28 12:14:50 +0000183 case MipsISD::VALL_ZERO: return "MipsISD::VALL_ZERO";
184 case MipsISD::VANY_ZERO: return "MipsISD::VANY_ZERO";
185 case MipsISD::VALL_NONZERO: return "MipsISD::VALL_NONZERO";
186 case MipsISD::VANY_NONZERO: return "MipsISD::VANY_NONZERO";
Daniel Sandersfd538dc2013-09-24 10:46:19 +0000187 case MipsISD::VCEQ: return "MipsISD::VCEQ";
188 case MipsISD::VCLE_S: return "MipsISD::VCLE_S";
189 case MipsISD::VCLE_U: return "MipsISD::VCLE_U";
190 case MipsISD::VCLT_S: return "MipsISD::VCLT_S";
191 case MipsISD::VCLT_U: return "MipsISD::VCLT_U";
Daniel Sanders3ce56622013-09-24 12:18:31 +0000192 case MipsISD::VSMAX: return "MipsISD::VSMAX";
193 case MipsISD::VSMIN: return "MipsISD::VSMIN";
194 case MipsISD::VUMAX: return "MipsISD::VUMAX";
195 case MipsISD::VUMIN: return "MipsISD::VUMIN";
Daniel Sandersa4c8f3a2013-09-23 14:03:12 +0000196 case MipsISD::VEXTRACT_SEXT_ELT: return "MipsISD::VEXTRACT_SEXT_ELT";
197 case MipsISD::VEXTRACT_ZEXT_ELT: return "MipsISD::VEXTRACT_ZEXT_ELT";
Daniel Sandersf7456c72013-09-23 13:22:24 +0000198 case MipsISD::VNOR: return "MipsISD::VNOR";
Daniel Sanderse5087042013-09-24 14:02:15 +0000199 case MipsISD::VSHF: return "MipsISD::VSHF";
Daniel Sanders26307182013-09-24 14:20:00 +0000200 case MipsISD::SHF: return "MipsISD::SHF";
Daniel Sanders2ed228b2013-09-24 14:36:12 +0000201 case MipsISD::ILVEV: return "MipsISD::ILVEV";
202 case MipsISD::ILVOD: return "MipsISD::ILVOD";
203 case MipsISD::ILVL: return "MipsISD::ILVL";
204 case MipsISD::ILVR: return "MipsISD::ILVR";
Daniel Sandersfae5f2a2013-09-24 14:53:25 +0000205 case MipsISD::PCKEV: return "MipsISD::PCKEV";
206 case MipsISD::PCKOD: return "MipsISD::PCKOD";
Daniel Sandersb50ccf82014-04-01 10:35:28 +0000207 case MipsISD::INSVE: return "MipsISD::INSVE";
Craig Topper062a2ba2014-04-25 05:30:21 +0000208 default: return nullptr;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000209 }
210}
211
Eric Christopherb1526602014-09-19 23:30:42 +0000212MipsTargetLowering::MipsTargetLowering(const MipsTargetMachine &TM,
Eric Christopher8924d272014-07-18 23:25:04 +0000213 const MipsSubtarget &STI)
214 : TargetLowering(TM, new MipsTargetObjectFile()), Subtarget(STI) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000215 // Mips does not have i1 type, so use i32 for
Wesley Peck527da1b2010-11-23 03:31:01 +0000216 // setcc operations results (slt, sgt, ...).
Duncan Sands8d6e2e12008-11-23 15:47:28 +0000217 setBooleanContents(ZeroOrOneBooleanContent);
Akira Hatanaka68741cc2013-04-30 22:37:26 +0000218 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Daniel Sanderscbd44c52014-07-10 10:18:12 +0000219 // The cmp.cond.fmt instruction in MIPS32r6/MIPS64r6 uses 0 and -1 like MSA
220 // does. Integer booleans still use 0 and 1.
Eric Christopher1c29a652014-07-18 22:55:25 +0000221 if (Subtarget.hasMips32r6())
Daniel Sanderscbd44c52014-07-10 10:18:12 +0000222 setBooleanContents(ZeroOrOneBooleanContent,
223 ZeroOrNegativeOneBooleanContent);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000224
Wesley Peck527da1b2010-11-23 03:31:01 +0000225 // Load extented operations for i1 types must be promoted
Owen Anderson9f944592009-08-11 20:47:22 +0000226 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
227 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
228 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000229
Eli Friedman1fa07e12009-07-17 04:07:24 +0000230 // MIPS doesn't have extending float->double load/store
Owen Anderson9f944592009-08-11 20:47:22 +0000231 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
232 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman39d6faa2009-07-17 02:28:12 +0000233
Wesley Peck527da1b2010-11-23 03:31:01 +0000234 // Used by legalize types to correctly generate the setcc result.
235 // Without this, every float setcc comes with a AND/OR with the result,
236 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +0000237 // which is used implicitly by brcond and select operations.
Owen Anderson9f944592009-08-11 20:47:22 +0000238 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +0000239
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000240 // Mips Custom Operations
Akira Hatanaka0f693a82013-03-06 21:32:03 +0000241 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000242 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +0000243 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000244 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
245 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
246 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
247 setOperationAction(ISD::SELECT, MVT::f32, Custom);
248 setOperationAction(ISD::SELECT, MVT::f64, Custom);
249 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Akira Hatanaka24cf4e32012-07-11 19:32:27 +0000250 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
251 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Akira Hatanakab7f78592012-03-09 23:46:03 +0000252 setOperationAction(ISD::SETCC, MVT::f32, Custom);
253 setOperationAction(ISD::SETCC, MVT::f64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000254 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000255 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
256 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000257 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000258
Eric Christopher1c29a652014-07-18 22:55:25 +0000259 if (Subtarget.isGP64bit()) {
Akira Hatanakada00aa82012-03-10 00:03:50 +0000260 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
261 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
262 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
263 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
264 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
265 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Akira Hatanaka019e5922012-06-02 00:04:42 +0000266 setOperationAction(ISD::LOAD, MVT::i64, Custom);
267 setOperationAction(ISD::STORE, MVT::i64, Custom);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000268 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
Akira Hatanakada00aa82012-03-10 00:03:50 +0000269 }
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +0000270
Eric Christopher1c29a652014-07-18 22:55:25 +0000271 if (!Subtarget.isGP64bit()) {
Akira Hatanaka0a8ab712012-05-09 00:55:21 +0000272 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
273 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
274 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
275 }
276
Akira Hatanaka28e02ec2012-11-07 19:10:58 +0000277 setOperationAction(ISD::ADD, MVT::i32, Custom);
Eric Christopher1c29a652014-07-18 22:55:25 +0000278 if (Subtarget.isGP64bit())
Akira Hatanaka28e02ec2012-11-07 19:10:58 +0000279 setOperationAction(ISD::ADD, MVT::i64, Custom);
280
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000281 setOperationAction(ISD::SDIV, MVT::i32, Expand);
282 setOperationAction(ISD::SREM, MVT::i32, Expand);
283 setOperationAction(ISD::UDIV, MVT::i32, Expand);
284 setOperationAction(ISD::UREM, MVT::i32, Expand);
Akira Hatanakab1538f92011-10-03 21:06:13 +0000285 setOperationAction(ISD::SDIV, MVT::i64, Expand);
286 setOperationAction(ISD::SREM, MVT::i64, Expand);
287 setOperationAction(ISD::UDIV, MVT::i64, Expand);
288 setOperationAction(ISD::UREM, MVT::i64, Expand);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000289
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000290 // Operations not directly supported by Mips.
Tom Stellardb1588fc2013-03-08 15:36:57 +0000291 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
292 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
293 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
294 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
Tom Stellard3787b122014-06-10 16:01:29 +0000295 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
296 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000297 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Akira Hatanaka79aed152011-12-20 23:40:56 +0000298 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000299 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Akira Hatanaka79aed152011-12-20 23:40:56 +0000300 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000301 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Eric Christopher1c29a652014-07-18 22:55:25 +0000302 if (Subtarget.hasCnMips()) {
Kai Nacke93fe5e82014-03-20 11:51:58 +0000303 setOperationAction(ISD::CTPOP, MVT::i32, Legal);
304 setOperationAction(ISD::CTPOP, MVT::i64, Legal);
305 } else {
306 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
307 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
308 }
Owen Anderson9f944592009-08-11 20:47:22 +0000309 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
Akira Hatanaka410ce9c2011-12-21 00:14:05 +0000310 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000311 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
312 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
313 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
314 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000315 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Akira Hatanaka7ba8a8d2011-09-30 18:51:46 +0000316 setOperationAction(ISD::ROTL, MVT::i64, Expand);
Akira Hatanaka33a25af2012-07-31 20:54:48 +0000317 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
318 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Bruno Cardoso Lopesd47180e2010-12-09 17:32:30 +0000319
Eric Christopher1c29a652014-07-18 22:55:25 +0000320 if (!Subtarget.hasMips32r2())
Bruno Cardoso Lopesd47180e2010-12-09 17:32:30 +0000321 setOperationAction(ISD::ROTR, MVT::i32, Expand);
322
Eric Christopher1c29a652014-07-18 22:55:25 +0000323 if (!Subtarget.hasMips64r2())
Akira Hatanaka7ba8a8d2011-09-30 18:51:46 +0000324 setOperationAction(ISD::ROTR, MVT::i64, Expand);
325
Owen Anderson9f944592009-08-11 20:47:22 +0000326 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes22b69db2011-03-04 18:54:14 +0000327 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000328 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes22b69db2011-03-04 18:54:14 +0000329 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000330 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
331 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000332 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
333 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanakadfb8cda2011-05-23 22:23:58 +0000334 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000335 setOperationAction(ISD::FLOG, MVT::f32, Expand);
336 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
337 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
338 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000339 setOperationAction(ISD::FMA, MVT::f32, Expand);
340 setOperationAction(ISD::FMA, MVT::f64, Expand);
Akira Hatanaka0603ad82012-03-29 18:43:11 +0000341 setOperationAction(ISD::FREM, MVT::f32, Expand);
342 setOperationAction(ISD::FREM, MVT::f64, Expand);
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000343
Akira Hatanakac0b02062013-01-30 00:26:49 +0000344 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
345
Daniel Sanders2b553d42014-08-01 09:17:39 +0000346 setOperationAction(ISD::VASTART, MVT::Other, Custom);
347 setOperationAction(ISD::VAARG, MVT::Other, Custom);
Bruno Cardoso Lopes048ffab2011-03-09 19:22:22 +0000348 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
349 setOperationAction(ISD::VAEND, MVT::Other, Expand);
350
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +0000351 // Use the default for now
Owen Anderson9f944592009-08-11 20:47:22 +0000352 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
353 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman26a48482011-07-27 22:21:52 +0000354
Jia Liuf54f60f2012-02-28 07:46:26 +0000355 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
356 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
357 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
358 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
Eli Friedman7dfa7912011-08-29 18:23:02 +0000359
Eli Friedman30a49e92011-08-03 21:06:02 +0000360 setInsertFencesForAtomic(true);
361
Eric Christopher1c29a652014-07-18 22:55:25 +0000362 if (!Subtarget.hasMips32r2()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000363 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
364 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +0000365 }
366
Daniel Sanders070fd1c2014-05-12 12:41:59 +0000367 // MIPS16 lacks MIPS32's clz and clo instructions.
Eric Christopher1c29a652014-07-18 22:55:25 +0000368 if (!Subtarget.hasMips32() || Subtarget.inMips16Mode())
Owen Anderson9f944592009-08-11 20:47:22 +0000369 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Eric Christopher1c29a652014-07-18 22:55:25 +0000370 if (!Subtarget.hasMips64())
Akira Hatanaka1d8efab2011-12-21 00:20:27 +0000371 setOperationAction(ISD::CTLZ, MVT::i64, Expand);
Bruno Cardoso Lopes93da7e62008-08-08 06:16:31 +0000372
Eric Christopher1c29a652014-07-18 22:55:25 +0000373 if (!Subtarget.hasMips32r2())
Owen Anderson9f944592009-08-11 20:47:22 +0000374 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Eric Christopher1c29a652014-07-18 22:55:25 +0000375 if (!Subtarget.hasMips64r2())
Akira Hatanaka4706ac92011-12-20 23:56:43 +0000376 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +0000377
Eric Christopher1c29a652014-07-18 22:55:25 +0000378 if (Subtarget.isGP64bit()) {
Akira Hatanaka019e5922012-06-02 00:04:42 +0000379 setLoadExtAction(ISD::SEXTLOAD, MVT::i32, Custom);
380 setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, Custom);
381 setLoadExtAction(ISD::EXTLOAD, MVT::i32, Custom);
382 setTruncStoreAction(MVT::i64, MVT::i32, Custom);
383 }
384
Akira Hatanakaa3d9ab92013-07-26 20:58:55 +0000385 setOperationAction(ISD::TRAP, MVT::Other, Legal);
386
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000387 setTargetDAGCombine(ISD::SDIVREM);
388 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanaka5e152182012-03-08 03:26:37 +0000389 setTargetDAGCombine(ISD::SELECT);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000390 setTargetDAGCombine(ISD::AND);
391 setTargetDAGCombine(ISD::OR);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000392 setTargetDAGCombine(ISD::ADD);
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000393
Eric Christopher1c29a652014-07-18 22:55:25 +0000394 setMinFunctionAlignment(Subtarget.isGP64bit() ? 3 : 2);
Eli Friedman2518f832011-05-06 20:34:06 +0000395
Daniel Sanders2b553d42014-08-01 09:17:39 +0000396 // The arguments on the stack are defined in terms of 4-byte slots on O32
397 // and 8-byte slots on N32/N64.
398 setMinStackArgumentAlignment(
399 (Subtarget.isABI_N32() || Subtarget.isABI_N64()) ? 8 : 4);
400
Eric Christopher1c29a652014-07-18 22:55:25 +0000401 setStackPointerRegisterToSaveRestore(Subtarget.isABI_N64() ? Mips::SP_64
402 : Mips::SP);
Akira Hatanakaaa560002011-05-26 18:59:03 +0000403
Eric Christopher1c29a652014-07-18 22:55:25 +0000404 setExceptionPointerRegister(Subtarget.isABI_N64() ? Mips::A0_64 : Mips::A0);
405 setExceptionSelectorRegister(Subtarget.isABI_N64() ? Mips::A1_64 : Mips::A1);
Akira Hatanaka1daf8c22012-06-13 19:33:32 +0000406
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000407 MaxStoresPerMemcpy = 16;
Zoran Jovanovicff9d5f32013-12-19 16:12:56 +0000408
Eric Christopher1c29a652014-07-18 22:55:25 +0000409 isMicroMips = Subtarget.inMicroMipsMode();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000410}
411
Eric Christopherb1526602014-09-19 23:30:42 +0000412const MipsTargetLowering *MipsTargetLowering::create(const MipsTargetMachine &TM,
Eric Christopher8924d272014-07-18 23:25:04 +0000413 const MipsSubtarget &STI) {
414 if (STI.inMips16Mode())
415 return llvm::createMips16TargetLowering(TM, STI);
Jia Liuf54f60f2012-02-28 07:46:26 +0000416
Eric Christopher8924d272014-07-18 23:25:04 +0000417 return llvm::createMipsSETargetLowering(TM, STI);
Akira Hatanaka2fcc1cf2011-08-12 21:30:06 +0000418}
419
Reed Kotler720c5ca2014-04-17 22:15:34 +0000420// Create a fast isel object.
421FastISel *
422MipsTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
423 const TargetLibraryInfo *libInfo) const {
424 if (!EnableMipsFastISel)
425 return TargetLowering::createFastISel(funcInfo, libInfo);
426 return Mips::createFastISel(funcInfo, libInfo);
427}
428
Matt Arsenault758659232013-05-18 00:21:46 +0000429EVT MipsTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Akira Hatanakab13b3332013-01-04 20:06:01 +0000430 if (!VT.isVector())
431 return MVT::i32;
432 return VT.changeVectorElementTypeToInteger();
Scott Michela6729e82008-03-10 15:42:14 +0000433}
434
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000435static SDValue performDivRemCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000436 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000437 const MipsSubtarget &Subtarget) {
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000438 if (DCI.isBeforeLegalizeOps())
439 return SDValue();
440
Akira Hatanakab1538f92011-10-03 21:06:13 +0000441 EVT Ty = N->getValueType(0);
Akira Hatanaka8002a3f2013-08-14 00:47:08 +0000442 unsigned LO = (Ty == MVT::i32) ? Mips::LO0 : Mips::LO0_64;
443 unsigned HI = (Ty == MVT::i32) ? Mips::HI0 : Mips::HI0_64;
Akira Hatanakabe8612f2013-03-30 01:36:35 +0000444 unsigned Opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem16 :
445 MipsISD::DivRemU16;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000446 SDLoc DL(N);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000447
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000448 SDValue DivRem = DAG.getNode(Opc, DL, MVT::Glue,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000449 N->getOperand(0), N->getOperand(1));
450 SDValue InChain = DAG.getEntryNode();
451 SDValue InGlue = DivRem;
452
453 // insert MFLO
454 if (N->hasAnyUseOfValue(0)) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000455 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, DL, LO, Ty,
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000456 InGlue);
457 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
458 InChain = CopyFromLo.getValue(1);
459 InGlue = CopyFromLo.getValue(2);
460 }
461
462 // insert MFHI
463 if (N->hasAnyUseOfValue(1)) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000464 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, DL,
Akira Hatanakab1538f92011-10-03 21:06:13 +0000465 HI, Ty, InGlue);
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000466 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
467 }
468
469 return SDValue();
470}
471
Akira Hatanaka89af5892013-04-18 01:00:46 +0000472static Mips::CondCode condCodeToFCC(ISD::CondCode CC) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000473 switch (CC) {
474 default: llvm_unreachable("Unknown fp condition code!");
475 case ISD::SETEQ:
476 case ISD::SETOEQ: return Mips::FCOND_OEQ;
477 case ISD::SETUNE: return Mips::FCOND_UNE;
478 case ISD::SETLT:
479 case ISD::SETOLT: return Mips::FCOND_OLT;
480 case ISD::SETGT:
481 case ISD::SETOGT: return Mips::FCOND_OGT;
482 case ISD::SETLE:
483 case ISD::SETOLE: return Mips::FCOND_OLE;
484 case ISD::SETGE:
485 case ISD::SETOGE: return Mips::FCOND_OGE;
486 case ISD::SETULT: return Mips::FCOND_ULT;
487 case ISD::SETULE: return Mips::FCOND_ULE;
488 case ISD::SETUGT: return Mips::FCOND_UGT;
489 case ISD::SETUGE: return Mips::FCOND_UGE;
490 case ISD::SETUO: return Mips::FCOND_UN;
491 case ISD::SETO: return Mips::FCOND_OR;
492 case ISD::SETNE:
493 case ISD::SETONE: return Mips::FCOND_ONE;
494 case ISD::SETUEQ: return Mips::FCOND_UEQ;
495 }
496}
497
498
Akira Hatanakaf0ea5002013-03-30 01:16:38 +0000499/// This function returns true if the floating point conditional branches and
500/// conditional moves which use condition code CC should be inverted.
501static bool invertFPCondCodeUser(Mips::CondCode CC) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000502 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
503 return false;
504
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000505 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
506 "Illegal Condition Code");
Akira Hatanakaa5352702011-03-31 18:26:17 +0000507
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000508 return true;
Akira Hatanakaa5352702011-03-31 18:26:17 +0000509}
510
511// Creates and returns an FPCmp node from a setcc node.
512// Returns Op if setcc is not a floating point comparison.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000513static SDValue createFPCmp(SelectionDAG &DAG, const SDValue &Op) {
Akira Hatanakaa5352702011-03-31 18:26:17 +0000514 // must be a SETCC node
515 if (Op.getOpcode() != ISD::SETCC)
516 return Op;
517
518 SDValue LHS = Op.getOperand(0);
519
520 if (!LHS.getValueType().isFloatingPoint())
521 return Op;
522
523 SDValue RHS = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000524 SDLoc DL(Op);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000525
Akira Hatanakaaef55c82011-04-15 21:00:26 +0000526 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
527 // node if necessary.
Akira Hatanakaa5352702011-03-31 18:26:17 +0000528 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
529
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000530 return DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS,
Akira Hatanaka89af5892013-04-18 01:00:46 +0000531 DAG.getConstant(condCodeToFCC(CC), MVT::i32));
Akira Hatanakaa5352702011-03-31 18:26:17 +0000532}
533
534// Creates and returns a CMovFPT/F node.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000535static SDValue createCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000536 SDValue False, SDLoc DL) {
Akira Hatanakaf0ea5002013-03-30 01:16:38 +0000537 ConstantSDNode *CC = cast<ConstantSDNode>(Cond.getOperand(2));
538 bool invert = invertFPCondCodeUser((Mips::CondCode)CC->getSExtValue());
Akira Hatanaka8bce21c2013-07-26 20:51:20 +0000539 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000540
541 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
Akira Hatanaka8bce21c2013-07-26 20:51:20 +0000542 True.getValueType(), True, FCC0, False, Cond);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000543}
544
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000545static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000546 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000547 const MipsSubtarget &Subtarget) {
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000548 if (DCI.isBeforeLegalizeOps())
549 return SDValue();
550
551 SDValue SetCC = N->getOperand(0);
552
553 if ((SetCC.getOpcode() != ISD::SETCC) ||
554 !SetCC.getOperand(0).getValueType().isInteger())
555 return SDValue();
556
557 SDValue False = N->getOperand(2);
558 EVT FalseTy = False.getValueType();
559
560 if (!FalseTy.isInteger())
561 return SDValue();
562
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000563 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(False);
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000564
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000565 // If the RHS (False) is 0, we swap the order of the operands
566 // of ISD::SELECT (obviously also inverting the condition) so that we can
567 // take advantage of conditional moves using the $0 register.
568 // Example:
569 // return (a != 0) ? x : 0;
570 // load $reg, x
571 // movz $reg, $0, a
572 if (!FalseC)
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000573 return SDValue();
574
Andrew Trickef9de2a2013-05-25 02:42:55 +0000575 const SDLoc DL(N);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000576
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000577 if (!FalseC->getZExtValue()) {
578 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
579 SDValue True = N->getOperand(1);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000580
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000581 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
582 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
583
584 return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True);
585 }
586
Matheus Almeidaa6beac12013-12-05 12:07:05 +0000587 // If both operands are integer constants there's a possibility that we
588 // can do some interesting optimizations.
589 SDValue True = N->getOperand(1);
590 ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(True);
591
592 if (!TrueC || !True.getValueType().isInteger())
593 return SDValue();
594
595 // We'll also ignore MVT::i64 operands as this optimizations proves
596 // to be ineffective because of the required sign extensions as the result
597 // of a SETCC operator is always MVT::i32 for non-vector types.
598 if (True.getValueType() == MVT::i64)
599 return SDValue();
600
601 int64_t Diff = TrueC->getSExtValue() - FalseC->getSExtValue();
602
603 // 1) (a < x) ? y : y-1
604 // slti $reg1, a, x
605 // addiu $reg2, $reg1, y-1
606 if (Diff == 1)
607 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, False);
608
609 // 2) (a < x) ? y-1 : y
610 // slti $reg1, a, x
611 // xor $reg1, $reg1, 1
612 // addiu $reg2, $reg1, y-1
613 if (Diff == -1) {
614 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
615 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
616 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
617 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, True);
618 }
619
Matheus Almeidaa611c0f2013-12-05 11:56:56 +0000620 // Couldn't optimize.
621 return SDValue();
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000622}
623
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000624static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000625 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000626 const MipsSubtarget &Subtarget) {
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000627 // Pattern match EXT.
628 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
629 // => ext $dst, $src, size, pos
Eric Christopher1c29a652014-07-18 22:55:25 +0000630 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasExtractInsert())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000631 return SDValue();
632
633 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000634 unsigned ShiftRightOpc = ShiftRight.getOpcode();
635
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000636 // Op's first operand must be a shift right.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000637 if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL)
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000638 return SDValue();
639
640 // The second operand of the shift must be an immediate.
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000641 ConstantSDNode *CN;
642 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
643 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000644
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000645 uint64_t Pos = CN->getZExtValue();
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000646 uint64_t SMPos, SMSize;
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000647
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000648 // Op's second operand must be a shifted mask.
649 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000650 !isShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000651 return SDValue();
652
653 // Return if the shifted mask does not start at bit 0 or the sum of its size
654 // and Pos exceeds the word's size.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000655 EVT ValTy = N->getValueType(0);
656 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000657 return SDValue();
658
Andrew Trickef9de2a2013-05-25 02:42:55 +0000659 return DAG.getNode(MipsISD::Ext, SDLoc(N), ValTy,
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000660 ShiftRight.getOperand(0), DAG.getConstant(Pos, MVT::i32),
Akira Hatanakaeea541c2011-08-17 22:59:46 +0000661 DAG.getConstant(SMSize, MVT::i32));
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000662}
Jia Liuf54f60f2012-02-28 07:46:26 +0000663
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000664static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000665 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000666 const MipsSubtarget &Subtarget) {
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000667 // Pattern match INS.
668 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
Jia Liuf54f60f2012-02-28 07:46:26 +0000669 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000670 // => ins $dst, $src, size, pos, $src1
Eric Christopher1c29a652014-07-18 22:55:25 +0000671 if (DCI.isBeforeLegalizeOps() || !Subtarget.hasExtractInsert())
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000672 return SDValue();
673
674 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
675 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
676 ConstantSDNode *CN;
677
678 // See if Op's first operand matches (and $src1 , mask0).
679 if (And0.getOpcode() != ISD::AND)
680 return SDValue();
681
682 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000683 !isShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000684 return SDValue();
685
686 // See if Op's second operand matches (and (shl $src, pos), mask1).
687 if (And1.getOpcode() != ISD::AND)
688 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000689
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000690 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000691 !isShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000692 return SDValue();
693
694 // The shift masks must have the same position and size.
695 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
696 return SDValue();
697
698 SDValue Shl = And1.getOperand(0);
699 if (Shl.getOpcode() != ISD::SHL)
700 return SDValue();
701
702 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
703 return SDValue();
704
705 unsigned Shamt = CN->getZExtValue();
706
707 // Return if the shift amount and the first bit position of mask are not the
Jia Liuf54f60f2012-02-28 07:46:26 +0000708 // same.
Akira Hatanaka20cee2e2011-12-05 21:26:34 +0000709 EVT ValTy = N->getValueType(0);
710 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000711 return SDValue();
Jia Liuf54f60f2012-02-28 07:46:26 +0000712
Andrew Trickef9de2a2013-05-25 02:42:55 +0000713 return DAG.getNode(MipsISD::Ins, SDLoc(N), ValTy, Shl.getOperand(0),
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000714 DAG.getConstant(SMPos0, MVT::i32),
Akira Hatanaka9e1d3692011-12-19 19:52:25 +0000715 DAG.getConstant(SMSize0, MVT::i32), And0.getOperand(0));
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000716}
Jia Liuf54f60f2012-02-28 07:46:26 +0000717
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000718static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000719 TargetLowering::DAGCombinerInfo &DCI,
Eric Christopher1c29a652014-07-18 22:55:25 +0000720 const MipsSubtarget &Subtarget) {
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000721 // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt))
722
723 if (DCI.isBeforeLegalizeOps())
724 return SDValue();
725
726 SDValue Add = N->getOperand(1);
727
728 if (Add.getOpcode() != ISD::ADD)
729 return SDValue();
730
731 SDValue Lo = Add.getOperand(1);
732
733 if ((Lo.getOpcode() != MipsISD::Lo) ||
734 (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable))
735 return SDValue();
736
737 EVT ValTy = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000738 SDLoc DL(N);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000739
740 SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, N->getOperand(0),
741 Add.getOperand(0));
742 return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo);
743}
744
Bruno Cardoso Lopes61a61e92011-02-10 18:05:10 +0000745SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000746 const {
747 SelectionDAG &DAG = DCI.DAG;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000748 unsigned Opc = N->getOpcode();
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000749
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000750 switch (Opc) {
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000751 default: break;
Bruno Cardoso Lopes434248a62011-03-04 21:03:24 +0000752 case ISD::SDIVREM:
753 case ISD::UDIVREM:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000754 return performDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka7dd7c082012-03-08 02:14:24 +0000755 case ISD::SELECT:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000756 return performSELECTCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000757 case ISD::AND:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000758 return performANDCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka184b63d2011-08-17 17:45:08 +0000759 case ISD::OR:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000760 return performORCombine(N, DAG, DCI, Subtarget);
Akira Hatanakadf5205e2012-06-13 20:33:18 +0000761 case ISD::ADD:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000762 return performADDCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes4dc73fa2011-01-18 19:29:17 +0000763 }
764
765 return SDValue();
766}
767
Akira Hatanakafabb8cf2012-09-21 23:58:31 +0000768void
769MipsTargetLowering::LowerOperationWrapper(SDNode *N,
770 SmallVectorImpl<SDValue> &Results,
771 SelectionDAG &DAG) const {
772 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
773
774 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
775 Results.push_back(Res.getValue(I));
776}
777
778void
779MipsTargetLowering::ReplaceNodeResults(SDNode *N,
780 SmallVectorImpl<SDValue> &Results,
781 SelectionDAG &DAG) const {
Akira Hatanaka9da442f2013-04-30 21:17:07 +0000782 return LowerOperationWrapper(N, Results, DAG);
Akira Hatanakafabb8cf2012-09-21 23:58:31 +0000783}
784
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000785SDValue MipsTargetLowering::
Dan Gohman21cea8a2010-04-17 15:26:15 +0000786LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000787{
Wesley Peck527da1b2010-11-23 03:31:01 +0000788 switch (Op.getOpcode())
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000789 {
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000790 case ISD::BR_JT: return lowerBR_JT(Op, DAG);
791 case ISD::BRCOND: return lowerBRCOND(Op, DAG);
792 case ISD::ConstantPool: return lowerConstantPool(Op, DAG);
793 case ISD::GlobalAddress: return lowerGlobalAddress(Op, DAG);
794 case ISD::BlockAddress: return lowerBlockAddress(Op, DAG);
795 case ISD::GlobalTLSAddress: return lowerGlobalTLSAddress(Op, DAG);
796 case ISD::JumpTable: return lowerJumpTable(Op, DAG);
797 case ISD::SELECT: return lowerSELECT(Op, DAG);
798 case ISD::SELECT_CC: return lowerSELECT_CC(Op, DAG);
799 case ISD::SETCC: return lowerSETCC(Op, DAG);
800 case ISD::VASTART: return lowerVASTART(Op, DAG);
Daniel Sanders2b553d42014-08-01 09:17:39 +0000801 case ISD::VAARG: return lowerVAARG(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000802 case ISD::FCOPYSIGN: return lowerFCOPYSIGN(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000803 case ISD::FRAMEADDR: return lowerFRAMEADDR(Op, DAG);
804 case ISD::RETURNADDR: return lowerRETURNADDR(Op, DAG);
805 case ISD::EH_RETURN: return lowerEH_RETURN(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000806 case ISD::ATOMIC_FENCE: return lowerATOMIC_FENCE(Op, DAG);
807 case ISD::SHL_PARTS: return lowerShiftLeftParts(Op, DAG);
808 case ISD::SRA_PARTS: return lowerShiftRightParts(Op, DAG, true);
809 case ISD::SRL_PARTS: return lowerShiftRightParts(Op, DAG, false);
810 case ISD::LOAD: return lowerLOAD(Op, DAG);
811 case ISD::STORE: return lowerSTORE(Op, DAG);
Akira Hatanakad5a0e092013-03-30 01:15:17 +0000812 case ISD::ADD: return lowerADD(Op, DAG);
Akira Hatanaka252f54f2013-05-16 21:17:15 +0000813 case ISD::FP_TO_SINT: return lowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000814 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000815 return SDValue();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000816}
817
Akira Hatanakae2489122011-04-15 21:51:11 +0000818//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000819// Lower helper functions
Akira Hatanakae2489122011-04-15 21:51:11 +0000820//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000821
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000822// addLiveIn - This helper function adds the specified physical register to the
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000823// MachineFunction as a live in value. It also creates a corresponding
824// virtual register for it.
825static unsigned
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000826addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000827{
Chris Lattnera10fff52007-12-31 04:13:23 +0000828 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
829 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +0000830 return VReg;
831}
832
Daniel Sanders308181e2014-06-12 10:44:10 +0000833static MachineBasicBlock *insertDivByZeroTrap(MachineInstr *MI,
834 MachineBasicBlock &MBB,
835 const TargetInstrInfo &TII,
836 bool Is64Bit) {
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000837 if (NoZeroDivCheck)
838 return &MBB;
839
840 // Insert instruction "teq $divisor_reg, $zero, 7".
841 MachineBasicBlock::iterator I(MI);
842 MachineInstrBuilder MIB;
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000843 MachineOperand &Divisor = MI->getOperand(2);
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000844 MIB = BuildMI(MBB, std::next(I), MI->getDebugLoc(), TII.get(Mips::TEQ))
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000845 .addReg(Divisor.getReg(), getKillRegState(Divisor.isKill()))
846 .addReg(Mips::ZERO).addImm(7);
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000847
848 // Use the 32-bit sub-register if this is a 64-bit division.
849 if (Is64Bit)
850 MIB->getOperand(0).setSubReg(Mips::sub_32);
851
Akira Hatanaka86c3c792013-10-15 01:06:30 +0000852 // Clear Divisor's kill flag.
853 Divisor.setIsKill(false);
Daniel Sanders308181e2014-06-12 10:44:10 +0000854
855 // We would normally delete the original instruction here but in this case
856 // we only needed to inject an additional instruction rather than replace it.
857
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000858 return &MBB;
859}
860
Akira Hatanakae4bd0542012-09-27 02:15:57 +0000861MachineBasicBlock *
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +0000862MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +0000863 MachineBasicBlock *BB) const {
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +0000864 switch (MI->getOpcode()) {
Reed Kotler97ba5f22013-02-21 04:22:38 +0000865 default:
866 llvm_unreachable("Unexpected instr type to insert");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000867 case Mips::ATOMIC_LOAD_ADD_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000868 return emitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000869 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000870 return emitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000871 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000872 return emitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000873 case Mips::ATOMIC_LOAD_ADD_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000874 return emitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000875
876 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000877 return emitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000878 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000879 return emitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000880 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000881 return emitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000882 case Mips::ATOMIC_LOAD_AND_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000883 return emitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000884
885 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000886 return emitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000887 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000888 return emitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000889 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000890 return emitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000891 case Mips::ATOMIC_LOAD_OR_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000892 return emitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000893
894 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000895 return emitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000896 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000897 return emitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000898 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000899 return emitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000900 case Mips::ATOMIC_LOAD_XOR_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000901 return emitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000902
903 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000904 return emitAtomicBinaryPartword(MI, BB, 1, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000905 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000906 return emitAtomicBinaryPartword(MI, BB, 2, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000907 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000908 return emitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000909 case Mips::ATOMIC_LOAD_NAND_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000910 return emitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000911
912 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000913 return emitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000914 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000915 return emitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000916 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000917 return emitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000918 case Mips::ATOMIC_LOAD_SUB_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000919 return emitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000920
921 case Mips::ATOMIC_SWAP_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000922 return emitAtomicBinaryPartword(MI, BB, 1, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000923 case Mips::ATOMIC_SWAP_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000924 return emitAtomicBinaryPartword(MI, BB, 2, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000925 case Mips::ATOMIC_SWAP_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000926 return emitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000927 case Mips::ATOMIC_SWAP_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000928 return emitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000929
930 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000931 return emitAtomicCmpSwapPartword(MI, BB, 1);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000932 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000933 return emitAtomicCmpSwapPartword(MI, BB, 2);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000934 case Mips::ATOMIC_CMP_SWAP_I32:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000935 return emitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000936 case Mips::ATOMIC_CMP_SWAP_I64:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000937 return emitAtomicCmpSwap(MI, BB, 8);
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000938 case Mips::PseudoSDIV:
939 case Mips::PseudoUDIV:
Daniel Sanders308181e2014-06-12 10:44:10 +0000940 case Mips::DIV:
941 case Mips::DIVU:
942 case Mips::MOD:
943 case Mips::MODU:
Eric Christopherd9134482014-08-04 21:25:23 +0000944 return insertDivByZeroTrap(
945 MI, *BB, *getTargetMachine().getSubtargetImpl()->getInstrInfo(), false);
Akira Hatanaka1cb02422013-05-20 18:07:43 +0000946 case Mips::PseudoDSDIV:
947 case Mips::PseudoDUDIV:
Daniel Sanders308181e2014-06-12 10:44:10 +0000948 case Mips::DDIV:
949 case Mips::DDIVU:
950 case Mips::DMOD:
951 case Mips::DMODU:
Eric Christopherd9134482014-08-04 21:25:23 +0000952 return insertDivByZeroTrap(
953 MI, *BB, *getTargetMachine().getSubtargetImpl()->getInstrInfo(), true);
Daniel Sanders0fa60412014-06-12 13:39:06 +0000954 case Mips::SEL_D:
955 return emitSEL_D(MI, BB);
Akira Hatanakaa5352702011-03-31 18:26:17 +0000956 }
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +0000957}
958
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000959// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
960// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
961MachineBasicBlock *
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000962MipsTargetLowering::emitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher0713a9d2011-06-08 23:55:35 +0000963 unsigned Size, unsigned BinOpcode,
Akira Hatanaka15506782011-06-07 18:58:42 +0000964 bool Nand) const {
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000965 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000966
967 MachineFunction *MF = BB->getParent();
968 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000969 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Eric Christopherd9134482014-08-04 21:25:23 +0000970 const TargetInstrInfo *TII =
971 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +0000972 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000973 unsigned LL, SC, AND, NOR, ZERO, BEQ;
974
975 if (Size == 4) {
Daniel Sanders6a803f62014-06-16 13:13:03 +0000976 if (isMicroMips) {
977 LL = Mips::LL_MM;
978 SC = Mips::SC_MM;
979 } else {
Daniel Sandersbdcfab12014-07-24 09:47:14 +0000980 LL = Subtarget.hasMips32r6() ? Mips::LL_R6 : Mips::LL;
981 SC = Subtarget.hasMips32r6() ? Mips::SC_R6 : Mips::SC;
Daniel Sanders6a803f62014-06-16 13:13:03 +0000982 }
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000983 AND = Mips::AND;
984 NOR = Mips::NOR;
985 ZERO = Mips::ZERO;
986 BEQ = Mips::BEQ;
Daniel Sanders6a803f62014-06-16 13:13:03 +0000987 } else {
Daniel Sandersbdcfab12014-07-24 09:47:14 +0000988 LL = Subtarget.hasMips64r6() ? Mips::LLD_R6 : Mips::LLD;
989 SC = Subtarget.hasMips64r6() ? Mips::SCD_R6 : Mips::SCD;
Akira Hatanaka21cbc252011-11-11 04:14:30 +0000990 AND = Mips::AND64;
991 NOR = Mips::NOR64;
992 ZERO = Mips::ZERO_64;
993 BEQ = Mips::BEQ64;
994 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000995
Akira Hatanaka0e019592011-07-19 20:11:17 +0000996 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +0000997 unsigned Ptr = MI->getOperand(1).getReg();
998 unsigned Incr = MI->getOperand(2).getReg();
999
Akira Hatanaka0e019592011-07-19 20:11:17 +00001000 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1001 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1002 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001003
1004 // insert new blocks after the current block
1005 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1006 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1007 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1008 MachineFunction::iterator It = BB;
1009 ++It;
1010 MF->insert(It, loopMBB);
1011 MF->insert(It, exitMBB);
1012
1013 // Transfer the remainder of BB and its successor edges to exitMBB.
1014 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001015 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001016 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1017
1018 // thisMBB:
1019 // ...
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001020 // fallthrough --> loopMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001021 BB->addSuccessor(loopMBB);
Akira Hatanaka08636b42011-07-19 17:09:53 +00001022 loopMBB->addSuccessor(loopMBB);
1023 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001024
1025 // loopMBB:
1026 // ll oldval, 0(ptr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001027 // <binop> storeval, oldval, incr
1028 // sc success, storeval, 0(ptr)
1029 // beq success, $0, loopMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001030 BB = loopMBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001031 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001032 if (Nand) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001033 // and andres, oldval, incr
1034 // nor storeval, $0, andres
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001035 BuildMI(BB, DL, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
1036 BuildMI(BB, DL, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001037 } else if (BinOpcode) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001038 // <binop> storeval, oldval, incr
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001039 BuildMI(BB, DL, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001040 } else {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001041 StoreVal = Incr;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001042 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001043 BuildMI(BB, DL, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
1044 BuildMI(BB, DL, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001045
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001046 MI->eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001047
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001048 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001049}
1050
Daniel Sanders6a803f62014-06-16 13:13:03 +00001051MachineBasicBlock *MipsTargetLowering::emitSignExtendToI32InReg(
1052 MachineInstr *MI, MachineBasicBlock *BB, unsigned Size, unsigned DstReg,
1053 unsigned SrcReg) const {
Eric Christopherd9134482014-08-04 21:25:23 +00001054 const TargetInstrInfo *TII =
1055 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Daniel Sanders6a803f62014-06-16 13:13:03 +00001056 DebugLoc DL = MI->getDebugLoc();
1057
Eric Christopher1c29a652014-07-18 22:55:25 +00001058 if (Subtarget.hasMips32r2() && Size == 1) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001059 BuildMI(BB, DL, TII->get(Mips::SEB), DstReg).addReg(SrcReg);
1060 return BB;
1061 }
1062
Eric Christopher1c29a652014-07-18 22:55:25 +00001063 if (Subtarget.hasMips32r2() && Size == 2) {
Daniel Sanders6a803f62014-06-16 13:13:03 +00001064 BuildMI(BB, DL, TII->get(Mips::SEH), DstReg).addReg(SrcReg);
1065 return BB;
1066 }
1067
1068 MachineFunction *MF = BB->getParent();
1069 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1070 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1071 unsigned ScrReg = RegInfo.createVirtualRegister(RC);
1072
1073 assert(Size < 32);
1074 int64_t ShiftImm = 32 - (Size * 8);
1075
1076 BuildMI(BB, DL, TII->get(Mips::SLL), ScrReg).addReg(SrcReg).addImm(ShiftImm);
1077 BuildMI(BB, DL, TII->get(Mips::SRA), DstReg).addReg(ScrReg).addImm(ShiftImm);
1078
1079 return BB;
1080}
1081
1082MachineBasicBlock *MipsTargetLowering::emitAtomicBinaryPartword(
1083 MachineInstr *MI, MachineBasicBlock *BB, unsigned Size, unsigned BinOpcode,
1084 bool Nand) const {
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001085 assert((Size == 1 || Size == 2) &&
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001086 "Unsupported size for EmitAtomicBinaryPartial.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001087
1088 MachineFunction *MF = BB->getParent();
1089 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1090 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
Eric Christopherd9134482014-08-04 21:25:23 +00001091 const TargetInstrInfo *TII =
1092 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001093 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001094
1095 unsigned Dest = MI->getOperand(0).getReg();
1096 unsigned Ptr = MI->getOperand(1).getReg();
1097 unsigned Incr = MI->getOperand(2).getReg();
1098
Akira Hatanaka0e019592011-07-19 20:11:17 +00001099 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1100 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001101 unsigned Mask = RegInfo.createVirtualRegister(RC);
1102 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001103 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1104 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001105 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001106 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1107 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1108 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1109 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1110 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka9663dd32011-07-19 20:56:53 +00001111 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001112 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1113 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1114 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001115 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001116
1117 // insert new blocks after the current block
1118 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1119 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001120 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001121 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1122 MachineFunction::iterator It = BB;
1123 ++It;
1124 MF->insert(It, loopMBB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001125 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001126 MF->insert(It, exitMBB);
1127
1128 // Transfer the remainder of BB and its successor edges to exitMBB.
1129 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001130 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001131 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1132
Akira Hatanaka08636b42011-07-19 17:09:53 +00001133 BB->addSuccessor(loopMBB);
1134 loopMBB->addSuccessor(loopMBB);
1135 loopMBB->addSuccessor(sinkMBB);
1136 sinkMBB->addSuccessor(exitMBB);
1137
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001138 // thisMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001139 // addiu masklsb2,$0,-4 # 0xfffffffc
1140 // and alignedaddr,ptr,masklsb2
1141 // andi ptrlsb2,ptr,3
1142 // sll shiftamt,ptrlsb2,3
1143 // ori maskupper,$0,255 # 0xff
1144 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001145 // nor mask2,$0,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001146 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001147
1148 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001149 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001150 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001151 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001152 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001153 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Eric Christopher1c29a652014-07-18 22:55:25 +00001154 if (Subtarget.isLittle()) {
Akira Hatanaka2bf97332013-05-31 03:25:44 +00001155 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1156 } else {
1157 unsigned Off = RegInfo.createVirtualRegister(RC);
1158 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1159 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1160 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1161 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001162 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001163 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001164 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001165 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001166 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001167 BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(Incr).addReg(ShiftAmt);
Bruno Cardoso Lopesf771a0f2011-05-31 20:25:26 +00001168
Akira Hatanaka27292632011-07-18 18:52:12 +00001169 // atomic.load.binop
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001170 // loopMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001171 // ll oldval,0(alignedaddr)
1172 // binop binopres,oldval,incr2
1173 // and newval,binopres,mask
1174 // and maskedoldval0,oldval,mask2
1175 // or storeval,maskedoldval0,newval
1176 // sc success,storeval,0(alignedaddr)
1177 // beq success,$0,loopMBB
1178
Akira Hatanaka27292632011-07-18 18:52:12 +00001179 // atomic.swap
1180 // loopMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001181 // ll oldval,0(alignedaddr)
Akira Hatanakae4503582011-07-19 18:14:26 +00001182 // and newval,incr2,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001183 // and maskedoldval0,oldval,mask2
1184 // or storeval,maskedoldval0,newval
1185 // sc success,storeval,0(alignedaddr)
1186 // beq success,$0,loopMBB
Akira Hatanaka27292632011-07-18 18:52:12 +00001187
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001188 BB = loopMBB;
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001189 BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001190 if (Nand) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001191 // and andres, oldval, incr2
1192 // nor binopres, $0, andres
1193 // and newval, binopres, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001194 BuildMI(BB, DL, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1195 BuildMI(BB, DL, TII->get(Mips::NOR), BinOpRes)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001196 .addReg(Mips::ZERO).addReg(AndRes);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001197 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001198 } else if (BinOpcode) {
Akira Hatanaka0e019592011-07-19 20:11:17 +00001199 // <binop> binopres, oldval, incr2
1200 // and newval, binopres, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001201 BuildMI(BB, DL, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1202 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001203 } else { // atomic.swap
Akira Hatanaka0e019592011-07-19 20:11:17 +00001204 // and newval, incr2, mask
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001205 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanakae4503582011-07-19 18:14:26 +00001206 }
Jia Liuf54f60f2012-02-28 07:46:26 +00001207
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001208 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001209 .addReg(OldVal).addReg(Mask2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001210 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka9663dd32011-07-19 20:56:53 +00001211 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001212 BuildMI(BB, DL, TII->get(Mips::SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001213 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001214 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001215 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001216
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001217 // sinkMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001218 // and maskedoldval1,oldval,mask
1219 // srl srlres,maskedoldval1,shiftamt
Daniel Sanders6a803f62014-06-16 13:13:03 +00001220 // sign_extend dest,srlres
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001221 BB = sinkMBB;
Akira Hatanakae97bd812011-07-19 03:14:58 +00001222
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001223 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001224 .addReg(OldVal).addReg(Mask);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001225 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001226 .addReg(MaskedOldVal1).addReg(ShiftAmt);
Daniel Sanders6a803f62014-06-16 13:13:03 +00001227 BB = emitSignExtendToI32InReg(MI, BB, Size, Dest, SrlRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001228
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001229 MI->eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001230
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001231 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001232}
1233
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001234MachineBasicBlock * MipsTargetLowering::emitAtomicCmpSwap(MachineInstr *MI,
1235 MachineBasicBlock *BB,
1236 unsigned Size) const {
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001237 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001238
1239 MachineFunction *MF = BB->getParent();
1240 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001241 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Eric Christopherd9134482014-08-04 21:25:23 +00001242 const TargetInstrInfo *TII =
1243 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001244 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001245 unsigned LL, SC, ZERO, BNE, BEQ;
1246
1247 if (Size == 4) {
Zoran Jovanovicff9d5f32013-12-19 16:12:56 +00001248 LL = isMicroMips ? Mips::LL_MM : Mips::LL;
1249 SC = isMicroMips ? Mips::SC_MM : Mips::SC;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001250 ZERO = Mips::ZERO;
1251 BNE = Mips::BNE;
1252 BEQ = Mips::BEQ;
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001253 } else {
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001254 LL = Mips::LLD;
1255 SC = Mips::SCD;
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001256 ZERO = Mips::ZERO_64;
1257 BNE = Mips::BNE64;
1258 BEQ = Mips::BEQ64;
1259 }
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001260
1261 unsigned Dest = MI->getOperand(0).getReg();
1262 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka0e019592011-07-19 20:11:17 +00001263 unsigned OldVal = MI->getOperand(2).getReg();
1264 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001265
Akira Hatanaka0e019592011-07-19 20:11:17 +00001266 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001267
1268 // insert new blocks after the current block
1269 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1270 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1271 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1272 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1273 MachineFunction::iterator It = BB;
1274 ++It;
1275 MF->insert(It, loop1MBB);
1276 MF->insert(It, loop2MBB);
1277 MF->insert(It, exitMBB);
1278
1279 // Transfer the remainder of BB and its successor edges to exitMBB.
1280 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001281 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001282 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1283
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001284 // thisMBB:
1285 // ...
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001286 // fallthrough --> loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001287 BB->addSuccessor(loop1MBB);
Akira Hatanaka08636b42011-07-19 17:09:53 +00001288 loop1MBB->addSuccessor(exitMBB);
1289 loop1MBB->addSuccessor(loop2MBB);
1290 loop2MBB->addSuccessor(loop1MBB);
1291 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001292
1293 // loop1MBB:
1294 // ll dest, 0(ptr)
1295 // bne dest, oldval, exitMBB
1296 BB = loop1MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001297 BuildMI(BB, DL, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1298 BuildMI(BB, DL, TII->get(BNE))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001299 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001300
1301 // loop2MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001302 // sc success, newval, 0(ptr)
1303 // beq success, $0, loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001304 BB = loop2MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001305 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001306 .addReg(NewVal).addReg(Ptr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001307 BuildMI(BB, DL, TII->get(BEQ))
Akira Hatanaka21cbc252011-11-11 04:14:30 +00001308 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001309
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001310 MI->eraseFromParent(); // The instruction is gone now.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001311
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001312 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001313}
1314
1315MachineBasicBlock *
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001316MipsTargetLowering::emitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka15506782011-06-07 18:58:42 +00001317 MachineBasicBlock *BB,
1318 unsigned Size) const {
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001319 assert((Size == 1 || Size == 2) &&
1320 "Unsupported size for EmitAtomicCmpSwapPartial.");
1321
1322 MachineFunction *MF = BB->getParent();
1323 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1324 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
Eric Christopherd9134482014-08-04 21:25:23 +00001325 const TargetInstrInfo *TII =
1326 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001327 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001328
1329 unsigned Dest = MI->getOperand(0).getReg();
1330 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka0e019592011-07-19 20:11:17 +00001331 unsigned CmpVal = MI->getOperand(2).getReg();
1332 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001333
Akira Hatanaka0e019592011-07-19 20:11:17 +00001334 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1335 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001336 unsigned Mask = RegInfo.createVirtualRegister(RC);
1337 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001338 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1339 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1340 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1341 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1342 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1343 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1344 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1345 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1346 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1347 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1348 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1349 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
Akira Hatanaka0e019592011-07-19 20:11:17 +00001350 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001351
1352 // insert new blocks after the current block
1353 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1354 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1355 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001356 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001357 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1358 MachineFunction::iterator It = BB;
1359 ++It;
1360 MF->insert(It, loop1MBB);
1361 MF->insert(It, loop2MBB);
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001362 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001363 MF->insert(It, exitMBB);
1364
1365 // Transfer the remainder of BB and its successor edges to exitMBB.
1366 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001367 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001368 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1369
Akira Hatanaka08636b42011-07-19 17:09:53 +00001370 BB->addSuccessor(loop1MBB);
1371 loop1MBB->addSuccessor(sinkMBB);
1372 loop1MBB->addSuccessor(loop2MBB);
1373 loop2MBB->addSuccessor(loop1MBB);
1374 loop2MBB->addSuccessor(sinkMBB);
1375 sinkMBB->addSuccessor(exitMBB);
1376
Akira Hatanakae4503582011-07-19 18:14:26 +00001377 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001378 // thisMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001379 // addiu masklsb2,$0,-4 # 0xfffffffc
1380 // and alignedaddr,ptr,masklsb2
1381 // andi ptrlsb2,ptr,3
1382 // sll shiftamt,ptrlsb2,3
1383 // ori maskupper,$0,255 # 0xff
1384 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001385 // nor mask2,$0,mask
Akira Hatanaka0e019592011-07-19 20:11:17 +00001386 // andi maskedcmpval,cmpval,255
1387 // sll shiftedcmpval,maskedcmpval,shiftamt
1388 // andi maskednewval,newval,255
1389 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001390 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001391 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001392 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001393 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001394 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001395 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Eric Christopher1c29a652014-07-18 22:55:25 +00001396 if (Subtarget.isLittle()) {
Akira Hatanaka2bf97332013-05-31 03:25:44 +00001397 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1398 } else {
1399 unsigned Off = RegInfo.createVirtualRegister(RC);
1400 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1401 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1402 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1403 }
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001404 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001405 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001406 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001407 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001408 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1409 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001410 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001411 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001412 .addReg(MaskedCmpVal).addReg(ShiftAmt);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001413 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001414 .addReg(NewVal).addImm(MaskImm);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001415 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001416 .addReg(MaskedNewVal).addReg(ShiftAmt);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001417
1418 // loop1MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001419 // ll oldval,0(alginedaddr)
1420 // and maskedoldval0,oldval,mask
1421 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001422 BB = loop1MBB;
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001423 BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001424 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001425 .addReg(OldVal).addReg(Mask);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001426 BuildMI(BB, DL, TII->get(Mips::BNE))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001427 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001428
1429 // loop2MBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001430 // and maskedoldval1,oldval,mask2
1431 // or storeval,maskedoldval1,shiftednewval
1432 // sc success,storeval,0(alignedaddr)
1433 // beq success,$0,loop1MBB
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001434 BB = loop2MBB;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001435 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001436 .addReg(OldVal).addReg(Mask2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001437 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001438 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
Akira Hatanaka6781fc12013-08-20 21:08:22 +00001439 BuildMI(BB, DL, TII->get(Mips::SC), Success)
Akira Hatanaka0e019592011-07-19 20:11:17 +00001440 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001441 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka0e019592011-07-19 20:11:17 +00001442 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001443
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001444 // sinkMBB:
Akira Hatanaka0e019592011-07-19 20:11:17 +00001445 // srl srlres,maskedoldval0,shiftamt
Daniel Sanders6a803f62014-06-16 13:13:03 +00001446 // sign_extend dest,srlres
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001447 BB = sinkMBB;
Akira Hatanakae97bd812011-07-19 03:14:58 +00001448
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001449 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka1af66c92013-07-01 20:39:53 +00001450 .addReg(MaskedOldVal0).addReg(ShiftAmt);
Daniel Sanders6a803f62014-06-16 13:13:03 +00001451 BB = emitSignExtendToI32InReg(MI, BB, Size, Dest, SrlRes);
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001452
1453 MI->eraseFromParent(); // The instruction is gone now.
1454
Akira Hatanakae4e9a592011-07-19 03:42:13 +00001455 return exitMBB;
Bruno Cardoso Lopes98fc4c82011-05-31 02:54:07 +00001456}
1457
Daniel Sanders0fa60412014-06-12 13:39:06 +00001458MachineBasicBlock *MipsTargetLowering::emitSEL_D(MachineInstr *MI,
1459 MachineBasicBlock *BB) const {
1460 MachineFunction *MF = BB->getParent();
Eric Christopherd9134482014-08-04 21:25:23 +00001461 const TargetRegisterInfo *TRI =
1462 getTargetMachine().getSubtargetImpl()->getRegisterInfo();
1463 const TargetInstrInfo *TII =
1464 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Daniel Sanders0fa60412014-06-12 13:39:06 +00001465 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1466 DebugLoc DL = MI->getDebugLoc();
1467 MachineBasicBlock::iterator II(MI);
1468
1469 unsigned Fc = MI->getOperand(1).getReg();
1470 const auto &FGR64RegClass = TRI->getRegClass(Mips::FGR64RegClassID);
1471
1472 unsigned Fc2 = RegInfo.createVirtualRegister(FGR64RegClass);
1473
1474 BuildMI(*BB, II, DL, TII->get(Mips::SUBREG_TO_REG), Fc2)
1475 .addImm(0)
1476 .addReg(Fc)
1477 .addImm(Mips::sub_lo);
1478
1479 // We don't erase the original instruction, we just replace the condition
1480 // register with the 64-bit super-register.
1481 MI->getOperand(1).setReg(Fc2);
1482
1483 return BB;
1484}
1485
Akira Hatanakae2489122011-04-15 21:51:11 +00001486//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00001487// Misc Lower Operation implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00001488//===----------------------------------------------------------------------===//
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001489SDValue MipsTargetLowering::lowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001490 SDValue Chain = Op.getOperand(0);
1491 SDValue Table = Op.getOperand(1);
1492 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001493 SDLoc DL(Op);
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001494 EVT PTy = getPointerTy();
1495 unsigned EntrySize =
1496 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(*getDataLayout());
1497
1498 Index = DAG.getNode(ISD::MUL, DL, PTy, Index,
1499 DAG.getConstant(EntrySize, PTy));
1500 SDValue Addr = DAG.getNode(ISD::ADD, DL, PTy, Index, Table);
1501
1502 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
1503 Addr = DAG.getExtLoad(ISD::SEXTLOAD, DL, PTy, Chain, Addr,
1504 MachinePointerInfo::getJumpTable(), MemVT, false, false,
Louis Gerbarg67474e32014-07-31 21:45:05 +00001505 false, 0);
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001506 Chain = Addr.getValue(1);
1507
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001508 if ((getTargetMachine().getRelocationModel() == Reloc::PIC_) ||
Eric Christopher1c29a652014-07-18 22:55:25 +00001509 Subtarget.isABI_N64()) {
Akira Hatanaka0f693a82013-03-06 21:32:03 +00001510 // For PIC, the sequence is:
1511 // BRIND(load(Jumptable + index) + RelocBase)
1512 // RelocBase can be JumpTable, GOT or some sort of global base.
1513 Addr = DAG.getNode(ISD::ADD, DL, PTy, Addr,
1514 getPICJumpTableRelocBase(Table, DAG));
1515 }
1516
1517 return DAG.getNode(ISD::BRIND, DL, MVT::Other, Chain, Addr);
1518}
1519
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00001520SDValue MipsTargetLowering::lowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Wesley Peck527da1b2010-11-23 03:31:01 +00001521 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001522 // the block to branch to if the condition is true.
1523 SDValue Chain = Op.getOperand(0);
1524 SDValue Dest = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001525 SDLoc DL(Op);
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001526
Eric Christopher1c29a652014-07-18 22:55:25 +00001527 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001528 SDValue CondRes = createFPCmp(DAG, Op.getOperand(1));
Akira Hatanakaa5352702011-03-31 18:26:17 +00001529
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001530 // Return if flag is not set by a floating point comparison.
Akira Hatanakaa5352702011-03-31 18:26:17 +00001531 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopesa9504222008-07-30 17:06:13 +00001532 return Op;
Wesley Peck527da1b2010-11-23 03:31:01 +00001533
Bruno Cardoso Lopes23471042008-07-31 18:31:28 +00001534 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmaneffb8942008-09-12 16:56:44 +00001535 Mips::CondCode CC =
1536 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Akira Hatanakaf0ea5002013-03-30 01:16:38 +00001537 unsigned Opc = invertFPCondCodeUser(CC) ? Mips::BRANCH_F : Mips::BRANCH_T;
1538 SDValue BrCode = DAG.getConstant(Opc, MVT::i32);
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +00001539 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001540 return DAG.getNode(MipsISD::FPBrcond, DL, Op.getValueType(), Chain, BrCode,
Akira Hatanaka1fb1b8b2013-07-26 20:13:47 +00001541 FCC0, Dest, CondRes);
Bruno Cardoso Lopesbcaf6e52008-07-28 19:11:24 +00001542}
1543
1544SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001545lowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001546{
Eric Christopher1c29a652014-07-18 22:55:25 +00001547 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001548 SDValue Cond = createFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001549
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001550 // Return if flag is not set by a floating point comparison.
Akira Hatanakaa5352702011-03-31 18:26:17 +00001551 if (Cond.getOpcode() != MipsISD::FPCmp)
1552 return Op;
Bruno Cardoso Lopes92c64ae2008-08-13 07:13:40 +00001553
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001554 return createCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
Andrew Trickef9de2a2013-05-25 02:42:55 +00001555 SDLoc(Op));
Bruno Cardoso Lopese683bba2008-07-29 19:05:28 +00001556}
1557
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001558SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001559lowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001560{
Andrew Trickef9de2a2013-05-25 02:42:55 +00001561 SDLoc DL(Op);
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001562 EVT Ty = Op.getOperand(0).getValueType();
Matt Arsenault758659232013-05-18 00:21:46 +00001563 SDValue Cond = DAG.getNode(ISD::SETCC, DL,
1564 getSetCCResultType(*DAG.getContext(), Ty),
Akira Hatanaka24cf4e32012-07-11 19:32:27 +00001565 Op.getOperand(0), Op.getOperand(1),
1566 Op.getOperand(4));
1567
1568 return DAG.getNode(ISD::SELECT, DL, Op.getValueType(), Cond, Op.getOperand(2),
1569 Op.getOperand(3));
1570}
1571
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001572SDValue MipsTargetLowering::lowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00001573 assert(!Subtarget.hasMips32r6() && !Subtarget.hasMips64r6());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001574 SDValue Cond = createFPCmp(DAG, Op);
Akira Hatanakab7f78592012-03-09 23:46:03 +00001575
1576 assert(Cond.getOpcode() == MipsISD::FPCmp &&
1577 "Floating point operand expected.");
1578
1579 SDValue True = DAG.getConstant(1, MVT::i32);
1580 SDValue False = DAG.getConstant(0, MVT::i32);
1581
Andrew Trickef9de2a2013-05-25 02:42:55 +00001582 return createCMovFP(DAG, Cond, True, False, SDLoc(Op));
Akira Hatanakab7f78592012-03-09 23:46:03 +00001583}
1584
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001585SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001586 SelectionDAG &DAG) const {
Dale Johannesen400dc2e2009-02-06 21:50:26 +00001587 // FIXME there isn't actually debug info here
Andrew Trickef9de2a2013-05-25 02:42:55 +00001588 SDLoc DL(Op);
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001589 EVT Ty = Op.getValueType();
1590 GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
1591 const GlobalValue *GV = N->getGlobal();
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001592
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001593 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ &&
Eric Christopher1c29a652014-07-18 22:55:25 +00001594 !Subtarget.isABI_N64()) {
Akira Hatanaka92a96e12012-09-12 23:27:55 +00001595 const MipsTargetObjectFile &TLOF =
1596 (const MipsTargetObjectFile&)getObjFileLowering();
Wesley Peck527da1b2010-11-23 03:31:01 +00001597
Chris Lattner58e8be82009-08-13 05:41:27 +00001598 // %gp_rel relocation
Wesley Peck527da1b2010-11-23 03:31:01 +00001599 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001600 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, 0,
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +00001601 MipsII::MO_GPREL);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001602 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, DL,
Craig Topper48d114b2014-04-26 18:35:24 +00001603 DAG.getVTList(MVT::i32), GA);
Akira Hatanakaad495022012-08-22 03:18:13 +00001604 SDValue GPReg = DAG.getRegister(Mips::GP, MVT::i32);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001605 return DAG.getNode(ISD::ADD, DL, MVT::i32, GPReg, GPRelNode);
Chris Lattner58e8be82009-08-13 05:41:27 +00001606 }
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001607
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001608 // %hi/%lo relocation
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001609 return getAddrNonPIC(N, Ty, DAG);
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001610 }
1611
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001612 if (GV->hasInternalLinkage() || (GV->hasLocalLinkage() && !isa<Function>(GV)))
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001613 return getAddrLocal(N, Ty, DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +00001614 Subtarget.isABI_N32() || Subtarget.isABI_N64());
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00001615
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00001616 if (LargeGOT)
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001617 return getAddrGlobalLargeGOT(N, Ty, DAG, MipsII::MO_GOT_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00001618 MipsII::MO_GOT_LO16, DAG.getEntryNode(),
1619 MachinePointerInfo::getGOT());
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00001620
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001621 return getAddrGlobal(N, Ty, DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +00001622 (Subtarget.isABI_N32() || Subtarget.isABI_N64())
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001623 ? MipsII::MO_GOT_DISP
1624 : MipsII::MO_GOT16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00001625 DAG.getEntryNode(), MachinePointerInfo::getGOT());
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001626}
1627
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001628SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op,
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +00001629 SelectionDAG &DAG) const {
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001630 BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op);
1631 EVT Ty = Op.getValueType();
Akira Hatanaka30f97cf2013-09-25 00:30:25 +00001632
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001633 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ &&
Eric Christopher1c29a652014-07-18 22:55:25 +00001634 !Subtarget.isABI_N64())
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001635 return getAddrNonPIC(N, Ty, DAG);
1636
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001637 return getAddrLocal(N, Ty, DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +00001638 Subtarget.isABI_N32() || Subtarget.isABI_N64());
Bruno Cardoso Lopesf8198e42011-03-04 20:01:52 +00001639}
1640
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001641SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001642lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001643{
Akira Hatanakabff84e12011-12-14 18:26:41 +00001644 // If the relocation model is PIC, use the General Dynamic TLS Model or
1645 // Local Dynamic TLS model, otherwise use the Initial Exec or
1646 // Local Exec TLS Model.
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001647
1648 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001649 SDLoc DL(GA);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001650 const GlobalValue *GV = GA->getGlobal();
1651 EVT PtrVT = getPointerTy();
1652
Hans Wennborgaea41202012-05-04 09:40:39 +00001653 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
1654
1655 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Hans Wennborg245917b2012-06-04 14:02:08 +00001656 // General Dynamic and Local Dynamic TLS Model.
1657 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
1658 : MipsII::MO_TLSGD;
1659
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001660 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag);
1661 SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT,
1662 getGlobalReg(DAG, PtrVT), TGA);
Akira Hatanakaf10ee842011-12-08 21:05:38 +00001663 unsigned PtrSize = PtrVT.getSizeInBits();
1664 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1665
Benjamin Kramer64ba50a2011-12-11 12:21:34 +00001666 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001667
1668 ArgListTy Args;
1669 ArgListEntry Entry;
1670 Entry.Node = Argument;
Akira Hatanakadee6c822011-12-08 20:34:32 +00001671 Entry.Ty = PtrTy;
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001672 Args.push_back(Entry);
Jia Liuf54f60f2012-02-28 07:46:26 +00001673
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00001674 TargetLowering::CallLoweringInfo CLI(DAG);
1675 CLI.setDebugLoc(DL).setChain(DAG.getEntryNode())
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00001676 .setCallee(CallingConv::C, PtrTy, TlsGetAddr, std::move(Args), 0);
Justin Holewinskiaa583972012-05-25 16:35:28 +00001677 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001678
Akira Hatanakabff84e12011-12-14 18:26:41 +00001679 SDValue Ret = CallResult.first;
1680
Hans Wennborgaea41202012-05-04 09:40:39 +00001681 if (model != TLSModel::LocalDynamic)
Akira Hatanakabff84e12011-12-14 18:26:41 +00001682 return Ret;
1683
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001684 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanakabff84e12011-12-14 18:26:41 +00001685 MipsII::MO_DTPREL_HI);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001686 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1687 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanakabff84e12011-12-14 18:26:41 +00001688 MipsII::MO_DTPREL_LO);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001689 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1690 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret);
1691 return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo);
Bruno Cardoso Lopesbf3c1252011-05-31 02:53:58 +00001692 }
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001693
1694 SDValue Offset;
Hans Wennborgaea41202012-05-04 09:40:39 +00001695 if (model == TLSModel::InitialExec) {
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001696 // Initial Exec TLS Model
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001697 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001698 MipsII::MO_GOTTPREL);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001699 TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT),
Akira Hatanakab049aef2012-02-24 22:34:47 +00001700 TGA);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001701 Offset = DAG.getLoad(PtrVT, DL,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001702 DAG.getEntryNode(), TGA, MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001703 false, false, false, 0);
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001704 } else {
1705 // Local Exec TLS Model
Hans Wennborgaea41202012-05-04 09:40:39 +00001706 assert(model == TLSModel::LocalExec);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001707 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001708 MipsII::MO_TPREL_HI);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001709 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001710 MipsII::MO_TPREL_LO);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001711 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1712 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1713 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
Akira Hatanaka5b350be2011-06-21 01:02:03 +00001714 }
1715
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001716 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
1717 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes2a241572008-07-29 19:29:50 +00001718}
1719
1720SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001721lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +00001722{
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001723 JumpTableSDNode *N = cast<JumpTableSDNode>(Op);
1724 EVT Ty = Op.getValueType();
Akira Hatanaka30f97cf2013-09-25 00:30:25 +00001725
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001726 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ &&
Eric Christopher1c29a652014-07-18 22:55:25 +00001727 !Subtarget.isABI_N64())
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001728 return getAddrNonPIC(N, Ty, DAG);
1729
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001730 return getAddrLocal(N, Ty, DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +00001731 Subtarget.isABI_N32() || Subtarget.isABI_N64());
Bruno Cardoso Lopesb4391322007-11-12 19:49:57 +00001732}
1733
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001734SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001735lowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +00001736{
Bruno Cardoso Lopesfdb4cec2008-07-23 16:01:50 +00001737 // gp_rel relocation
Wesley Peck527da1b2010-11-23 03:31:01 +00001738 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001739 // but the asm printer currently doesn't support this feature without
Wesley Peck527da1b2010-11-23 03:31:01 +00001740 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopes98bda582008-07-28 19:26:25 +00001741 // stuff below.
Eli Friedman57c11da2009-08-03 02:22:28 +00001742 //if (IsInSmallSection(C->getType())) {
Owen Anderson9f944592009-08-11 20:47:22 +00001743 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1744 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peck527da1b2010-11-23 03:31:01 +00001745 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001746 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
1747 EVT Ty = Op.getValueType();
Bruno Cardoso Lopes2db07582009-11-25 12:17:58 +00001748
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001749 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ &&
Eric Christopher1c29a652014-07-18 22:55:25 +00001750 !Subtarget.isABI_N64())
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00001751 return getAddrNonPIC(N, Ty, DAG);
Bruno Cardoso Lopesfdb4cec2008-07-23 16:01:50 +00001752
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001753 return getAddrLocal(N, Ty, DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +00001754 Subtarget.isABI_N32() || Subtarget.isABI_N64());
Bruno Cardoso Lopesa6ce3ce2008-07-09 04:15:08 +00001755}
1756
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001757SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman31ae5862010-04-17 14:41:14 +00001758 MachineFunction &MF = DAG.getMachineFunction();
1759 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1760
Andrew Trickef9de2a2013-05-25 02:42:55 +00001761 SDLoc DL(Op);
Dan Gohman31ae5862010-04-17 14:41:14 +00001762 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1763 getPointerTy());
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +00001764
1765 // vastart just stores the address of the VarArgsFrameIndex slot into the
1766 // memory location argument.
1767 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001768 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00001769 MachinePointerInfo(SV), false, false, 0);
Bruno Cardoso Lopesd59cddc2010-02-06 21:00:02 +00001770}
Jia Liuf54f60f2012-02-28 07:46:26 +00001771
Daniel Sanders2b553d42014-08-01 09:17:39 +00001772SDValue MipsTargetLowering::lowerVAARG(SDValue Op, SelectionDAG &DAG) const {
1773 SDNode *Node = Op.getNode();
1774 EVT VT = Node->getValueType(0);
1775 SDValue Chain = Node->getOperand(0);
1776 SDValue VAListPtr = Node->getOperand(1);
1777 unsigned Align = Node->getConstantOperandVal(3);
1778 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
1779 SDLoc DL(Node);
1780 unsigned ArgSlotSizeInBytes =
1781 (Subtarget.isABI_N32() || Subtarget.isABI_N64()) ? 8 : 4;
1782
1783 SDValue VAListLoad = DAG.getLoad(getPointerTy(), DL, Chain, VAListPtr,
1784 MachinePointerInfo(SV), false, false, false,
1785 0);
1786 SDValue VAList = VAListLoad;
1787
1788 // Re-align the pointer if necessary.
1789 // It should only ever be necessary for 64-bit types on O32 since the minimum
1790 // argument alignment is the same as the maximum type alignment for N32/N64.
1791 //
1792 // FIXME: We currently align too often. The code generator doesn't notice
1793 // when the pointer is still aligned from the last va_arg (or pair of
1794 // va_args for the i64 on O32 case).
1795 if (Align > getMinStackArgumentAlignment()) {
1796 assert(((Align & (Align-1)) == 0) && "Expected Align to be a power of 2");
1797
1798 VAList = DAG.getNode(ISD::ADD, DL, VAList.getValueType(), VAList,
1799 DAG.getConstant(Align - 1,
1800 VAList.getValueType()));
1801
1802 VAList = DAG.getNode(ISD::AND, DL, VAList.getValueType(), VAList,
1803 DAG.getConstant(-(int64_t)Align,
1804 VAList.getValueType()));
1805 }
1806
1807 // Increment the pointer, VAList, to the next vaarg.
1808 unsigned ArgSizeInBytes = getDataLayout()->getTypeAllocSize(VT.getTypeForEVT(*DAG.getContext()));
1809 SDValue Tmp3 = DAG.getNode(ISD::ADD, DL, VAList.getValueType(), VAList,
1810 DAG.getConstant(RoundUpToAlignment(ArgSizeInBytes, ArgSlotSizeInBytes),
1811 VAList.getValueType()));
1812 // Store the incremented VAList to the legalized pointer
1813 Chain = DAG.getStore(VAListLoad.getValue(1), DL, Tmp3, VAListPtr,
1814 MachinePointerInfo(SV), false, false, 0);
1815
1816 // In big-endian mode we must adjust the pointer when the load size is smaller
1817 // than the argument slot size. We must also reduce the known alignment to
1818 // match. For example in the N64 ABI, we must add 4 bytes to the offset to get
1819 // the correct half of the slot, and reduce the alignment from 8 (slot
1820 // alignment) down to 4 (type alignment).
1821 if (!Subtarget.isLittle() && ArgSizeInBytes < ArgSlotSizeInBytes) {
1822 unsigned Adjustment = ArgSlotSizeInBytes - ArgSizeInBytes;
1823 VAList = DAG.getNode(ISD::ADD, DL, VAListPtr.getValueType(), VAList,
1824 DAG.getIntPtrConstant(Adjustment));
1825 }
1826 // Load the actual argument out of the pointer VAList
1827 return DAG.getLoad(VT, DL, Chain, VAList, MachinePointerInfo(), false, false,
1828 false, 0);
1829}
1830
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001831static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG,
1832 bool HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001833 EVT TyX = Op.getOperand(0).getValueType();
1834 EVT TyY = Op.getOperand(1).getValueType();
1835 SDValue Const1 = DAG.getConstant(1, MVT::i32);
1836 SDValue Const31 = DAG.getConstant(31, MVT::i32);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001837 SDLoc DL(Op);
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001838 SDValue Res;
1839
1840 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1841 // to i32.
1842 SDValue X = (TyX == MVT::f32) ?
1843 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1844 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1845 Const1);
1846 SDValue Y = (TyY == MVT::f32) ?
1847 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
1848 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
1849 Const1);
1850
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001851 if (HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001852 // ext E, Y, 31, 1 ; extract bit31 of Y
1853 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
1854 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
1855 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
1856 } else {
1857 // sll SllX, X, 1
1858 // srl SrlX, SllX, 1
1859 // srl SrlY, Y, 31
1860 // sll SllY, SrlX, 31
1861 // or Or, SrlX, SllY
1862 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1863 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1864 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
1865 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
1866 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
1867 }
1868
1869 if (TyX == MVT::f32)
1870 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
1871
1872 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1873 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1874 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001875}
1876
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001877static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG,
1878 bool HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001879 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
1880 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
1881 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
1882 SDValue Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001883 SDLoc DL(Op);
Eric Christopher0713a9d2011-06-08 23:55:35 +00001884
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001885 // Bitcast to integer nodes.
1886 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
1887 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001888
Akira Hatanaka4a3836b2013-10-09 23:36:17 +00001889 if (HasExtractInsert) {
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001890 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
1891 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
1892 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
1893 DAG.getConstant(WidthY - 1, MVT::i32), Const1);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001894
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001895 if (WidthX > WidthY)
1896 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
1897 else if (WidthY > WidthX)
1898 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001899
Akira Hatanaka4f5c8422012-04-11 22:13:04 +00001900 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
1901 DAG.getConstant(WidthX - 1, MVT::i32), Const1, X);
1902 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
1903 }
1904
1905 // (d)sll SllX, X, 1
1906 // (d)srl SrlX, SllX, 1
1907 // (d)srl SrlY, Y, width(Y)-1
1908 // (d)sll SllY, SrlX, width(Y)-1
1909 // or Or, SrlX, SllY
1910 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
1911 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
1912 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
1913 DAG.getConstant(WidthY - 1, MVT::i32));
1914
1915 if (WidthX > WidthY)
1916 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
1917 else if (WidthY > WidthX)
1918 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
1919
1920 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
1921 DAG.getConstant(WidthX - 1, MVT::i32));
1922 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
1923 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001924}
1925
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00001926SDValue
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001927MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00001928 if (Subtarget.isGP64bit())
1929 return lowerFCOPYSIGN64(Op, DAG, Subtarget.hasExtractInsert());
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001930
Eric Christopher1c29a652014-07-18 22:55:25 +00001931 return lowerFCOPYSIGN32(Op, DAG, Subtarget.hasExtractInsert());
Akira Hatanaka44eba3a2011-05-25 19:32:07 +00001932}
1933
Akira Hatanaka66277522011-06-02 00:24:44 +00001934SDValue MipsTargetLowering::
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001935lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopes5444a7b2011-06-16 00:40:02 +00001936 // check the depth
1937 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka15506782011-06-07 18:58:42 +00001938 "Frame address can only be determined for current frame.");
Akira Hatanaka66277522011-06-02 00:24:44 +00001939
1940 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1941 MFI->setFrameAddressIsTaken(true);
1942 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001943 SDLoc DL(Op);
Eric Christopherbf33a3c2014-07-02 23:18:40 +00001944 SDValue FrameAddr =
1945 DAG.getCopyFromReg(DAG.getEntryNode(), DL,
Eric Christopher1c29a652014-07-18 22:55:25 +00001946 Subtarget.isABI_N64() ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka66277522011-06-02 00:24:44 +00001947 return FrameAddr;
1948}
1949
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001950SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op,
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00001951 SelectionDAG &DAG) const {
Bill Wendling908bf812014-01-06 00:43:20 +00001952 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001953 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001954
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00001955 // check the depth
1956 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
1957 "Return address can be determined only for current frame.");
1958
1959 MachineFunction &MF = DAG.getMachineFunction();
1960 MachineFrameInfo *MFI = MF.getFrameInfo();
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001961 MVT VT = Op.getSimpleValueType();
Eric Christopher1c29a652014-07-18 22:55:25 +00001962 unsigned RA = Subtarget.isABI_N64() ? Mips::RA_64 : Mips::RA;
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00001963 MFI->setReturnAddressIsTaken(true);
1964
1965 // Return RA, which contains the return address. Mark it an implicit live-in.
1966 unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00001967 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT);
Akira Hatanaka878ad8b2012-07-11 00:53:32 +00001968}
1969
Akira Hatanakac0b02062013-01-30 00:26:49 +00001970// An EH_RETURN is the result of lowering llvm.eh.return which in turn is
1971// generated from __builtin_eh_return (offset, handler)
1972// The effect of this is to adjust the stack pointer by "offset"
1973// and then branch to "handler".
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001974SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Akira Hatanakac0b02062013-01-30 00:26:49 +00001975 const {
1976 MachineFunction &MF = DAG.getMachineFunction();
1977 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1978
1979 MipsFI->setCallsEhReturn();
1980 SDValue Chain = Op.getOperand(0);
1981 SDValue Offset = Op.getOperand(1);
1982 SDValue Handler = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001983 SDLoc DL(Op);
Eric Christopher1c29a652014-07-18 22:55:25 +00001984 EVT Ty = Subtarget.isABI_N64() ? MVT::i64 : MVT::i32;
Akira Hatanakac0b02062013-01-30 00:26:49 +00001985
1986 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
1987 // EH_RETURN nodes, so that instructions are emitted back-to-back.
Eric Christopher1c29a652014-07-18 22:55:25 +00001988 unsigned OffsetReg = Subtarget.isABI_N64() ? Mips::V1_64 : Mips::V1;
1989 unsigned AddrReg = Subtarget.isABI_N64() ? Mips::V0_64 : Mips::V0;
Akira Hatanakac0b02062013-01-30 00:26:49 +00001990 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
1991 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
1992 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
1993 DAG.getRegister(OffsetReg, Ty),
1994 DAG.getRegister(AddrReg, getPointerTy()),
1995 Chain.getValue(1));
1996}
1997
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00001998SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
Akira Hatanaka5fd22482012-06-14 21:10:56 +00001999 SelectionDAG &DAG) const {
Eli Friedman26a48482011-07-27 22:21:52 +00002000 // FIXME: Need pseudo-fence for 'singlethread' fences
2001 // FIXME: Set SType for weaker fences where supported/appropriate.
2002 unsigned SType = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002003 SDLoc DL(Op);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002004 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
Eli Friedman26a48482011-07-27 22:21:52 +00002005 DAG.getConstant(SType, MVT::i32));
2006}
2007
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002008SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
Akira Hatanaka5fd22482012-06-14 21:10:56 +00002009 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002010 SDLoc DL(Op);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002011 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2012 SDValue Shamt = Op.getOperand(2);
2013
2014 // if shamt < 32:
2015 // lo = (shl lo, shamt)
2016 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
2017 // else:
2018 // lo = 0
2019 // hi = (shl lo, shamt[4:0])
2020 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
2021 DAG.getConstant(-1, MVT::i32));
2022 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo,
2023 DAG.getConstant(1, MVT::i32));
2024 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, ShiftRight1Lo,
2025 Not);
2026 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi, Shamt);
2027 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
2028 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, MVT::i32, Lo, Shamt);
2029 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
2030 DAG.getConstant(0x20, MVT::i32));
Akira Hatanaka5fd22482012-06-14 21:10:56 +00002031 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
2032 DAG.getConstant(0, MVT::i32), ShiftLeftLo);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002033 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftLeftLo, Or);
2034
2035 SDValue Ops[2] = {Lo, Hi};
Craig Topper64941d92014-04-27 19:20:57 +00002036 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002037}
2038
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002039SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002040 bool IsSRA) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002041 SDLoc DL(Op);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002042 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2043 SDValue Shamt = Op.getOperand(2);
2044
2045 // if shamt < 32:
2046 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
2047 // if isSRA:
2048 // hi = (sra hi, shamt)
2049 // else:
2050 // hi = (srl hi, shamt)
2051 // else:
2052 // if isSRA:
2053 // lo = (sra hi, shamt[4:0])
2054 // hi = (sra hi, 31)
2055 // else:
2056 // lo = (srl hi, shamt[4:0])
2057 // hi = 0
2058 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
2059 DAG.getConstant(-1, MVT::i32));
2060 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi,
2061 DAG.getConstant(1, MVT::i32));
2062 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, ShiftLeft1Hi, Not);
2063 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo, Shamt);
2064 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
2065 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL, DL, MVT::i32,
2066 Hi, Shamt);
2067 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
2068 DAG.getConstant(0x20, MVT::i32));
2069 SDValue Shift31 = DAG.getNode(ISD::SRA, DL, MVT::i32, Hi,
2070 DAG.getConstant(31, MVT::i32));
2071 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftRightHi, Or);
2072 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
2073 IsSRA ? Shift31 : DAG.getConstant(0, MVT::i32),
2074 ShiftRightHi);
2075
2076 SDValue Ops[2] = {Lo, Hi};
Craig Topper64941d92014-04-27 19:20:57 +00002077 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka0a8ab712012-05-09 00:55:21 +00002078}
2079
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002080static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002081 SDValue Chain, SDValue Src, unsigned Offset) {
Akira Hatanaka95866182012-06-13 19:06:08 +00002082 SDValue Ptr = LD->getBasePtr();
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002083 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
Akira Hatanaka95866182012-06-13 19:06:08 +00002084 EVT BasePtrVT = Ptr.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002085 SDLoc DL(LD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002086 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
2087
2088 if (Offset)
Akira Hatanaka95866182012-06-13 19:06:08 +00002089 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002090 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002091
2092 SDValue Ops[] = { Chain, Ptr, Src };
Craig Topper206fcd42014-04-26 19:29:41 +00002093 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002094 LD->getMemOperand());
2095}
2096
2097// Expand an unaligned 32 or 64-bit integer load node.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002098SDValue MipsTargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002099 LoadSDNode *LD = cast<LoadSDNode>(Op);
2100 EVT MemVT = LD->getMemoryVT();
2101
Eric Christopher1c29a652014-07-18 22:55:25 +00002102 if (Subtarget.systemSupportsUnalignedAccess())
Daniel Sandersac272632014-05-23 13:18:02 +00002103 return Op;
2104
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002105 // Return if load is aligned or if MemVT is neither i32 nor i64.
2106 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
2107 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
2108 return SDValue();
2109
Eric Christopher1c29a652014-07-18 22:55:25 +00002110 bool IsLittle = Subtarget.isLittle();
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002111 EVT VT = Op.getValueType();
2112 ISD::LoadExtType ExtType = LD->getExtensionType();
2113 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
2114
2115 assert((VT == MVT::i32) || (VT == MVT::i64));
2116
2117 // Expand
2118 // (set dst, (i64 (load baseptr)))
2119 // to
2120 // (set tmp, (ldl (add baseptr, 7), undef))
2121 // (set dst, (ldr baseptr, tmp))
2122 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002123 SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002124 IsLittle ? 7 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002125 return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002126 IsLittle ? 0 : 7);
2127 }
2128
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002129 SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002130 IsLittle ? 3 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002131 SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002132 IsLittle ? 0 : 3);
2133
2134 // Expand
2135 // (set dst, (i32 (load baseptr))) or
2136 // (set dst, (i64 (sextload baseptr))) or
2137 // (set dst, (i64 (extload baseptr)))
2138 // to
2139 // (set tmp, (lwl (add baseptr, 3), undef))
2140 // (set dst, (lwr baseptr, tmp))
2141 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
2142 (ExtType == ISD::EXTLOAD))
2143 return LWR;
2144
2145 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
2146
2147 // Expand
2148 // (set dst, (i64 (zextload baseptr)))
2149 // to
2150 // (set tmp0, (lwl (add baseptr, 3), undef))
2151 // (set tmp1, (lwr baseptr, tmp0))
2152 // (set tmp2, (shl tmp1, 32))
2153 // (set dst, (srl tmp2, 32))
Andrew Trickef9de2a2013-05-25 02:42:55 +00002154 SDLoc DL(LD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002155 SDValue Const32 = DAG.getConstant(32, MVT::i32);
2156 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
Akira Hatanaka67346852012-06-04 17:46:29 +00002157 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
2158 SDValue Ops[] = { SRL, LWR.getValue(1) };
Craig Topper64941d92014-04-27 19:20:57 +00002159 return DAG.getMergeValues(Ops, DL);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002160}
2161
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002162static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002163 SDValue Chain, unsigned Offset) {
Akira Hatanaka95866182012-06-13 19:06:08 +00002164 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
2165 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002166 SDLoc DL(SD);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002167 SDVTList VTList = DAG.getVTList(MVT::Other);
2168
2169 if (Offset)
Akira Hatanaka95866182012-06-13 19:06:08 +00002170 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002171 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002172
2173 SDValue Ops[] = { Chain, Value, Ptr };
Craig Topper206fcd42014-04-26 19:29:41 +00002174 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002175 SD->getMemOperand());
2176}
2177
2178// Expand an unaligned 32 or 64-bit integer store node.
Akira Hatanakad82ee942013-05-16 20:45:17 +00002179static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG,
2180 bool IsLittle) {
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002181 SDValue Value = SD->getValue(), Chain = SD->getChain();
2182 EVT VT = Value.getValueType();
2183
2184 // Expand
2185 // (store val, baseptr) or
2186 // (truncstore val, baseptr)
2187 // to
2188 // (swl val, (add baseptr, 3))
2189 // (swr val, baseptr)
2190 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002191 SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain,
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002192 IsLittle ? 3 : 0);
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002193 return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002194 }
2195
2196 assert(VT == MVT::i64);
2197
2198 // Expand
2199 // (store val, baseptr)
2200 // to
2201 // (sdl val, (add baseptr, 7))
2202 // (sdr val, baseptr)
Akira Hatanaka52f79fc2013-04-11 19:07:14 +00002203 SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2204 return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
Akira Hatanaka8f1db772012-06-02 00:03:49 +00002205}
2206
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002207// Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr).
2208static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG) {
2209 SDValue Val = SD->getValue();
2210
2211 if (Val.getOpcode() != ISD::FP_TO_SINT)
2212 return SDValue();
2213
2214 EVT FPTy = EVT::getFloatingPointVT(Val.getValueSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002215 SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy,
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002216 Val.getOperand(0));
2217
Andrew Trickef9de2a2013-05-25 02:42:55 +00002218 return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(),
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002219 SD->getPointerInfo(), SD->isVolatile(),
2220 SD->isNonTemporal(), SD->getAlignment());
2221}
2222
Akira Hatanakad82ee942013-05-16 20:45:17 +00002223SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2224 StoreSDNode *SD = cast<StoreSDNode>(Op);
2225 EVT MemVT = SD->getMemoryVT();
2226
2227 // Lower unaligned integer stores.
Eric Christopher1c29a652014-07-18 22:55:25 +00002228 if (!Subtarget.systemSupportsUnalignedAccess() &&
Daniel Sandersac272632014-05-23 13:18:02 +00002229 (SD->getAlignment() < MemVT.getSizeInBits() / 8) &&
Akira Hatanakad82ee942013-05-16 20:45:17 +00002230 ((MemVT == MVT::i32) || (MemVT == MVT::i64)))
Eric Christopher1c29a652014-07-18 22:55:25 +00002231 return lowerUnalignedIntStore(SD, DAG, Subtarget.isLittle());
Akira Hatanakad82ee942013-05-16 20:45:17 +00002232
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002233 return lowerFP_TO_SINT_STORE(SD, DAG);
Akira Hatanakad82ee942013-05-16 20:45:17 +00002234}
2235
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002236SDValue MipsTargetLowering::lowerADD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002237 if (Op->getOperand(0).getOpcode() != ISD::FRAMEADDR
2238 || cast<ConstantSDNode>
2239 (Op->getOperand(0).getOperand(0))->getZExtValue() != 0
2240 || Op->getOperand(1).getOpcode() != ISD::FRAME_TO_ARGS_OFFSET)
2241 return SDValue();
2242
2243 // The pattern
2244 // (add (frameaddr 0), (frame_to_args_offset))
2245 // results from lowering llvm.eh.dwarf.cfa intrinsic. Transform it to
2246 // (add FrameObject, 0)
2247 // where FrameObject is a fixed StackObject with offset 0 which points to
2248 // the old stack pointer.
2249 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2250 EVT ValTy = Op->getValueType(0);
2251 int FI = MFI->CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
2252 SDValue InArgsAddr = DAG.getFrameIndex(FI, ValTy);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002253 return DAG.getNode(ISD::ADD, SDLoc(Op), ValTy, InArgsAddr,
Akira Hatanaka28e02ec2012-11-07 19:10:58 +00002254 DAG.getConstant(0, ValTy));
2255}
2256
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002257SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op,
2258 SelectionDAG &DAG) const {
2259 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002260 SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy,
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002261 Op.getOperand(0));
Andrew Trickef9de2a2013-05-25 02:42:55 +00002262 return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc);
Akira Hatanaka252f54f2013-05-16 21:17:15 +00002263}
2264
Akira Hatanakae2489122011-04-15 21:51:11 +00002265//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002266// Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002267//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002268
Akira Hatanakae2489122011-04-15 21:51:11 +00002269//===----------------------------------------------------------------------===//
Wesley Peck527da1b2010-11-23 03:31:01 +00002270// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002271// Mips O32 ABI rules:
2272// ---
2273// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peck527da1b2010-11-23 03:31:01 +00002274// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002275// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peck527da1b2010-11-23 03:31:01 +00002276// f64 - Only passed in two aliased f32 registers if no int reg has been used
2277// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Sylvestre Ledru469de192014-08-11 18:04:46 +00002278// not used, it must be shadowed. If only A3 is available, shadow it and
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002279// go to stack.
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002280//
2281// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanakae2489122011-04-15 21:51:11 +00002282//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002283
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00002284static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2285 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
Craig Topper840beec2014-04-04 05:16:06 +00002286 CCState &State, const MCPhysReg *F64Regs) {
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002287
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00002288 static const unsigned IntRegsSize = 4, FloatRegsSize = 2;
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002289
Craig Topper840beec2014-04-04 05:16:06 +00002290 static const MCPhysReg IntRegs[] = { Mips::A0, Mips::A1, Mips::A2, Mips::A3 };
2291 static const MCPhysReg F32Regs[] = { Mips::F12, Mips::F14 };
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002292
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002293 // Do not process byval args here.
2294 if (ArgFlags.isByVal())
2295 return true;
Akira Hatanaka5e16c6a2011-05-24 19:18:33 +00002296
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002297 // Promote i8 and i16
2298 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
2299 LocVT = MVT::i32;
2300 if (ArgFlags.isSExt())
2301 LocInfo = CCValAssign::SExt;
2302 else if (ArgFlags.isZExt())
2303 LocInfo = CCValAssign::ZExt;
2304 else
2305 LocInfo = CCValAssign::AExt;
2306 }
2307
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002308 unsigned Reg;
2309
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002310 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
2311 // is true: function is vararg, argument is 3rd or higher, there is previous
2312 // argument which is not f32 or f64.
2313 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
2314 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanaka9e6a8cc2011-05-19 20:29:48 +00002315 unsigned OrigAlign = ArgFlags.getOrigAlign();
2316 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002317
2318 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002319 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanaka9e6a8cc2011-05-19 20:29:48 +00002320 // If this is the first part of an i64 arg,
2321 // the allocated register must be either A0 or A2.
2322 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
2323 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002324 LocVT = MVT::i32;
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002325 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
2326 // Allocate int register and shadow next int register. If first
2327 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002328 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2329 if (Reg == Mips::A1 || Reg == Mips::A3)
2330 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2331 State.AllocateReg(IntRegs, IntRegsSize);
2332 LocVT = MVT::i32;
Akira Hatanaka92ab6db2011-05-19 18:06:05 +00002333 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
2334 // we are guaranteed to find an available float register
2335 if (ValVT == MVT::f32) {
2336 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
2337 // Shadow int register
2338 State.AllocateReg(IntRegs, IntRegsSize);
2339 } else {
2340 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
2341 // Shadow int registers
2342 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
2343 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
2344 State.AllocateReg(IntRegs, IntRegsSize);
2345 State.AllocateReg(IntRegs, IntRegsSize);
2346 }
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002347 } else
2348 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002349
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002350 if (!Reg) {
2351 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3,
2352 OrigAlign);
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002353 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002354 } else
Bruno Cardoso Lopes8887d652011-03-04 20:27:44 +00002355 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002356
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002357 return false;
Akira Hatanaka202f6402011-11-12 02:20:46 +00002358}
2359
Akira Hatanakabfb66242013-08-20 23:38:40 +00002360static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT,
2361 MVT LocVT, CCValAssign::LocInfo LocInfo,
2362 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002363 static const MCPhysReg F64Regs[] = { Mips::D6, Mips::D7 };
Akira Hatanakabfb66242013-08-20 23:38:40 +00002364
2365 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2366}
2367
2368static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT,
2369 MVT LocVT, CCValAssign::LocInfo LocInfo,
2370 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Craig Topper840beec2014-04-04 05:16:06 +00002371 static const MCPhysReg F64Regs[] = { Mips::D12_64, Mips::D14_64 };
Akira Hatanakabfb66242013-08-20 23:38:40 +00002372
2373 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2374}
2375
Daniel Sanderse7473622014-09-18 08:28:39 +00002376static bool originalTypeIsF128(const Type *Ty, const SDNode *CallNode);
2377
Akira Hatanaka202f6402011-11-12 02:20:46 +00002378#include "MipsGenCallingConv.inc"
2379
Akira Hatanakae2489122011-04-15 21:51:11 +00002380//===----------------------------------------------------------------------===//
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002381// Call Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002382//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002383
Akira Hatanaka61bbcce2011-09-23 00:58:33 +00002384// Return next O32 integer argument register.
2385static unsigned getNextIntArgReg(unsigned Reg) {
2386 assert((Reg == Mips::A0) || (Reg == Mips::A2));
2387 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
2388}
2389
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002390SDValue
2391MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002392 SDValue Chain, SDValue Arg, SDLoc DL,
Akira Hatanaka6233cf52012-10-30 19:23:25 +00002393 bool IsTailCall, SelectionDAG &DAG) const {
2394 if (!IsTailCall) {
2395 SDValue PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr,
2396 DAG.getIntPtrConstant(Offset));
2397 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo(), false,
2398 false, 0);
2399 }
2400
2401 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2402 int FI = MFI->CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
2403 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2404 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(),
2405 /*isVolatile=*/ true, false, 0);
2406}
2407
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002408void MipsTargetLowering::
2409getOpndList(SmallVectorImpl<SDValue> &Ops,
2410 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
2411 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
2412 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
2413 // Insert node "GP copy globalreg" before call to function.
2414 //
2415 // R_MIPS_CALL* operators (emitted when non-internal functions are called
2416 // in PIC mode) allow symbols to be resolved via lazy binding.
2417 // The lazy binding stub requires GP to point to the GOT.
2418 if (IsPICCall && !InternalLinkage) {
Eric Christopher1c29a652014-07-18 22:55:25 +00002419 unsigned GPReg = Subtarget.isABI_N64() ? Mips::GP_64 : Mips::GP;
2420 EVT Ty = Subtarget.isABI_N64() ? MVT::i64 : MVT::i32;
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002421 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty)));
2422 }
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002423
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002424 // Build a sequence of copy-to-reg nodes chained together with token
2425 // chain and flag operands which copy the outgoing args into registers.
2426 // The InFlag in necessary since all emitted instructions must be
2427 // stuck together.
2428 SDValue InFlag;
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002429
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002430 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2431 Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, RegsToPass[i].first,
2432 RegsToPass[i].second, InFlag);
2433 InFlag = Chain.getValue(1);
2434 }
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002435
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002436 // Add argument registers to the end of the list so that they are
2437 // known live into the call.
2438 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2439 Ops.push_back(CLI.DAG.getRegister(RegsToPass[i].first,
2440 RegsToPass[i].second.getValueType()));
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002441
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002442 // Add a register mask operand representing the call-preserved registers.
Eric Christopherd9134482014-08-04 21:25:23 +00002443 const TargetRegisterInfo *TRI =
2444 getTargetMachine().getSubtargetImpl()->getRegisterInfo();
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002445 const uint32_t *Mask = TRI->getCallPreservedMask(CLI.CallConv);
2446 assert(Mask && "Missing call preserved mask for calling convention");
Eric Christopher1c29a652014-07-18 22:55:25 +00002447 if (Subtarget.inMips16HardFloat()) {
Reed Kotler783c7942013-05-10 22:25:39 +00002448 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
2449 llvm::StringRef Sym = G->getGlobal()->getName();
2450 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
Reed Kotler3230e722013-12-12 02:41:11 +00002451 if (F && F->hasFnAttribute("__Mips16RetHelper")) {
Reed Kotler783c7942013-05-10 22:25:39 +00002452 Mask = MipsRegisterInfo::getMips16RetHelperMask();
2453 }
2454 }
2455 }
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002456 Ops.push_back(CLI.DAG.getRegisterMask(Mask));
2457
2458 if (InFlag.getNode())
2459 Ops.push_back(InFlag);
Reed Kotlera2d76bc2013-01-24 04:24:02 +00002460}
2461
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002462/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman624801e2009-01-26 03:15:54 +00002463/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002464SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00002465MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002466 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00002467 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002468 SDLoc DL = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00002469 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
2470 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
2471 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Akira Hatanakabeda2242012-07-31 18:46:41 +00002472 SDValue Chain = CLI.Chain;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002473 SDValue Callee = CLI.Callee;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002474 bool &IsTailCall = CLI.IsTailCall;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002475 CallingConv::ID CallConv = CLI.CallConv;
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002476 bool IsVarArg = CLI.IsVarArg;
Justin Holewinskiaa583972012-05-25 16:35:28 +00002477
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002478 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002479 MachineFrameInfo *MFI = MF.getFrameInfo();
Eric Christopherfc6de422014-08-05 02:39:49 +00002480 const TargetFrameLowering *TFL = MF.getSubtarget().getFrameLowering();
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002481 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes0f20a5b2009-09-01 17:27:58 +00002482 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002483
2484 // Analyze operands of the call, assigning locations to each operand.
2485 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002486 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
2487 *DAG.getContext());
Reed Kotler783c7942013-05-10 22:25:39 +00002488 MipsCC::SpecialCallingConvType SpecialCallingConv =
2489 getSpecialCallingConv(Callee);
Daniel Sanders4abcfe22014-09-09 10:46:48 +00002490 MipsCC MipsCCInfo(CallConv, Subtarget, CCInfo, SpecialCallingConv);
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002491
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002492 MipsCCInfo.analyzeCallOperands(Outs, IsVarArg,
Eric Christopher1c29a652014-07-18 22:55:25 +00002493 Subtarget.abiUsesSoftFloat(),
Saleem Abdulrasool9f664c12014-05-17 21:50:01 +00002494 Callee.getNode(), CLI.getArgs());
Wesley Peck527da1b2010-11-23 03:31:01 +00002495
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002496 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka195a1e22011-06-08 17:39:33 +00002497 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka97ba7692012-07-26 23:27:01 +00002498
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002499 // Check if it's really possible to do a tail call.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002500 if (IsTailCall)
2501 IsTailCall =
2502 isEligibleForTailCallOptimization(MipsCCInfo, NextStackOffset,
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002503 *MF.getInfo<MipsFunctionInfo>());
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002504
Reid Kleckner5772b772014-04-24 20:14:34 +00002505 if (!IsTailCall && CLI.CS && CLI.CS->isMustTailCall())
2506 report_fatal_error("failed to perform tail call elimination on a call "
2507 "site marked musttail");
2508
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002509 if (IsTailCall)
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002510 ++NumTailCalls;
2511
Akira Hatanaka79738332011-09-19 20:26:02 +00002512 // Chain is the output chain of the last Load/Store or CopyToReg node.
2513 // ByValChain is the output chain of the last Memcpy node created for copying
2514 // byval arguments to the stack.
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002515 unsigned StackAlignment = TFL->getStackAlignment();
2516 NextStackOffset = RoundUpToAlignment(NextStackOffset, StackAlignment);
Akira Hatanaka79738332011-09-19 20:26:02 +00002517 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002518
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002519 if (!IsTailCall)
Andrew Trickad6d08a2013-05-29 22:03:55 +00002520 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffsetVal, DL);
Akira Hatanakabeda2242012-07-31 18:46:41 +00002521
Daniel Sandersd897b562014-03-27 10:46:12 +00002522 SDValue StackPtr = DAG.getCopyFromReg(
Eric Christopher1c29a652014-07-18 22:55:25 +00002523 Chain, DL, Subtarget.isABI_N64() ? Mips::SP_64 : Mips::SP,
Eric Christopherbf33a3c2014-07-02 23:18:40 +00002524 getPointerTy());
Akira Hatanaka195a1e22011-06-08 17:39:33 +00002525
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002526 // With EABI is it possible to have 16 args on registers.
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002527 std::deque< std::pair<unsigned, SDValue> > RegsToPass;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002528 SmallVector<SDValue, 8> MemOpChains;
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002529 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002530
2531 // Walk the register/memloc assignments, inserting copies/loads.
2532 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002533 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002534 CCValAssign &VA = ArgLocs[i];
Akira Hatanakab20a3252011-10-28 19:49:00 +00002535 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka19891f82011-11-12 02:34:50 +00002536 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2537
2538 // ByVal Arg.
2539 if (Flags.isByVal()) {
2540 assert(Flags.getByValSize() &&
2541 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002542 assert(ByValArg != MipsCCInfo.byval_end());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002543 assert(!IsTailCall &&
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002544 "Do not tail-call optimize if there is a byval argument.");
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002545 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
Eric Christopher1c29a652014-07-18 22:55:25 +00002546 MipsCCInfo, *ByValArg, Flags, Subtarget.isLittle());
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002547 ++ByValArg;
Akira Hatanaka19891f82011-11-12 02:34:50 +00002548 continue;
2549 }
Jia Liuf54f60f2012-02-28 07:46:26 +00002550
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002551 // Promote the value if needed.
2552 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002553 default: llvm_unreachable("Unknown loc info!");
Wesley Peck527da1b2010-11-23 03:31:01 +00002554 case CCValAssign::Full:
Akira Hatanakab20a3252011-10-28 19:49:00 +00002555 if (VA.isRegLoc()) {
2556 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
Akira Hatanaka3b7391d2013-03-05 22:20:28 +00002557 (ValVT == MVT::f64 && LocVT == MVT::i64) ||
2558 (ValVT == MVT::i64 && LocVT == MVT::f64))
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002559 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
Akira Hatanakab20a3252011-10-28 19:49:00 +00002560 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002561 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanakae2489122011-04-15 21:51:11 +00002562 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002563 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanakaaef55c82011-04-15 21:00:26 +00002564 Arg, DAG.getConstant(1, MVT::i32));
Eric Christopher1c29a652014-07-18 22:55:25 +00002565 if (!Subtarget.isLittle())
Akira Hatanaka27916972011-04-15 19:52:08 +00002566 std::swap(Lo, Hi);
Jia Liuf54f60f2012-02-28 07:46:26 +00002567 unsigned LocRegLo = VA.getLocReg();
Akira Hatanaka61bbcce2011-09-23 00:58:33 +00002568 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2569 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2570 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002571 continue;
Wesley Peck527da1b2010-11-23 03:31:01 +00002572 }
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002573 }
2574 break;
Chris Lattner52f16de2008-03-17 06:57:02 +00002575 case CCValAssign::SExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002576 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002577 break;
2578 case CCValAssign::ZExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002579 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002580 break;
2581 case CCValAssign::AExt:
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002582 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg);
Chris Lattner52f16de2008-03-17 06:57:02 +00002583 break;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002584 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002585
2586 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +00002587 // RegsToPass vector
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002588 if (VA.isRegLoc()) {
2589 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattner52f16de2008-03-17 06:57:02 +00002590 continue;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002591 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002592
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002593 // Register can't get to this point...
Chris Lattner52f16de2008-03-17 06:57:02 +00002594 assert(VA.isMemLoc());
Wesley Peck527da1b2010-11-23 03:31:01 +00002595
Wesley Peck527da1b2010-11-23 03:31:01 +00002596 // emit ISD::STORE whichs stores the
Chris Lattner52f16de2008-03-17 06:57:02 +00002597 // parameter value to a stack Location
Akira Hatanaka9c962c02012-10-30 20:16:31 +00002598 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002599 Chain, Arg, DL, IsTailCall, DAG));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002600 }
2601
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002602 // Transform all store nodes into one single node because all store
2603 // nodes are independent of each other.
Wesley Peck527da1b2010-11-23 03:31:01 +00002604 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00002605 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002606
Bill Wendling24c79f22008-09-16 21:48:12 +00002607 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peck527da1b2010-11-23 03:31:01 +00002608 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2609 // node so that legalize doesn't hack it.
Eric Christopherbf33a3c2014-07-02 23:18:40 +00002610 bool IsPICCall =
Eric Christopher1c29a652014-07-18 22:55:25 +00002611 (Subtarget.isABI_N64() || IsPIC); // true if calls are translated to
Eric Christopherbf33a3c2014-07-02 23:18:40 +00002612 // jalr $25
Akira Hatanakacf9a61b2012-12-13 03:17:29 +00002613 bool GlobalOrExternal = false, InternalLinkage = false;
Akira Hatanakad6f1c582011-04-07 19:51:44 +00002614 SDValue CalleeLo;
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002615 EVT Ty = Callee.getValueType();
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002616
2617 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002618 if (IsPICCall) {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002619 const GlobalValue *Val = G->getGlobal();
2620 InternalLinkage = Val->hasInternalLinkage();
Akira Hatanakacf9a61b2012-12-13 03:17:29 +00002621
2622 if (InternalLinkage)
Eric Christopherbf33a3c2014-07-02 23:18:40 +00002623 Callee = getAddrLocal(G, Ty, DAG,
Eric Christopher1c29a652014-07-18 22:55:25 +00002624 Subtarget.isABI_N32() || Subtarget.isABI_N64());
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00002625 else if (LargeGOT)
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002626 Callee = getAddrGlobalLargeGOT(G, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002627 MipsII::MO_CALL_LO16, Chain,
2628 FuncInfo->callPtrInfo(Val));
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002629 else
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002630 Callee = getAddrGlobal(G, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
2631 FuncInfo->callPtrInfo(Val));
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002632 } else
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002633 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, getPointerTy(), 0,
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002634 MipsII::MO_NO_FLAG);
Akira Hatanaka8e16aac2011-12-09 01:45:12 +00002635 GlobalOrExternal = true;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002636 }
2637 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002638 const char *Sym = S->getSymbol();
2639
Eric Christopher1c29a652014-07-18 22:55:25 +00002640 if (!Subtarget.isABI_N64() && !IsPIC) // !N64 && static
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002641 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(),
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002642 MipsII::MO_NO_FLAG);
Akira Hatanakabb6e74a2012-11-21 20:40:38 +00002643 else if (LargeGOT)
Akira Hatanakad8f10ce2013-09-27 19:51:35 +00002644 Callee = getAddrGlobalLargeGOT(S, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002645 MipsII::MO_CALL_LO16, Chain,
2646 FuncInfo->callPtrInfo(Sym));
Akira Hatanaka02b0e482013-02-22 21:10:03 +00002647 else // N64 || PIC
Akira Hatanakaaf4211a2013-09-28 00:12:32 +00002648 Callee = getAddrGlobal(S, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
2649 FuncInfo->callPtrInfo(Sym));
Akira Hatanaka56d5f1b2012-11-21 20:30:40 +00002650
Akira Hatanaka8e16aac2011-12-09 01:45:12 +00002651 GlobalOrExternal = true;
Akira Hatanaka5ec2ead2011-04-04 17:11:07 +00002652 }
2653
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002654 SmallVector<SDValue, 8> Ops(1, Chain);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002655 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00002656
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002657 getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal, InternalLinkage,
2658 CLI, Callee, Chain);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002659
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002660 if (IsTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +00002661 return DAG.getNode(MipsISD::TailCall, DL, MVT::Other, Ops);
Akira Hatanaka90131ac2012-10-19 21:47:33 +00002662
Craig Topper48d114b2014-04-26 18:35:24 +00002663 Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, Ops);
Akira Hatanaka96ca1822013-03-13 00:54:29 +00002664 SDValue InFlag = Chain.getValue(1);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002665
Bruno Cardoso Lopes193e64c2010-01-30 18:32:07 +00002666 // Create the CALLSEQ_END node.
Akira Hatanaka97ba7692012-07-26 23:27:01 +00002667 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal,
Andrew Trickad6d08a2013-05-29 22:03:55 +00002668 DAG.getIntPtrConstant(0, true), InFlag, DL);
Bruno Cardoso Lopes193e64c2010-01-30 18:32:07 +00002669 InFlag = Chain.getValue(1);
2670
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002671 // Handle result values, copying them out of physregs into vregs that we
2672 // return.
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002673 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg,
2674 Ins, DL, DAG, InVals, CLI.Callee.getNode(), CLI.RetTy);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002675}
2676
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002677/// LowerCallResult - Lower the result values of a call into the
2678/// appropriate copies out of appropriate physical registers.
2679SDValue
2680MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002681 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002682 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002683 SDLoc DL, SelectionDAG &DAG,
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002684 SmallVectorImpl<SDValue> &InVals,
2685 const SDNode *CallNode,
2686 const Type *RetTy) const {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002687 // Assign locations to each value returned by this call.
2688 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002689 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
2690 *DAG.getContext());
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002691
Daniel Sanderse7473622014-09-18 08:28:39 +00002692 if (originalTypeIsF128(RetTy, CallNode))
2693 CCInfo.AnalyzeCallResult(Ins, RetCC_F128);
2694 else
2695 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002696
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002697 // Copy all of the result registers out of their specified physreg.
2698 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Daniel Sandersae275e32014-09-25 12:15:05 +00002699 CCValAssign &VA = RVLocs[i];
2700 assert(VA.isRegLoc() && "Can only return in registers!");
2701
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002702 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(),
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002703 RVLocs[i].getLocVT(), InFlag);
2704 Chain = Val.getValue(1);
2705 InFlag = Val.getValue(2);
2706
Daniel Sandersae275e32014-09-25 12:15:05 +00002707 if (VA.isUpperBitsInLoc()) {
2708 unsigned ValSizeInBits = Ins[i].ArgVT.getSizeInBits();
2709 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
2710 unsigned Shift =
2711 VA.getLocInfo() == CCValAssign::ZExtUpper ? ISD::SRL : ISD::SRA;
2712 Val = DAG.getNode(
2713 Shift, DL, VA.getLocVT(), Val,
2714 DAG.getConstant(LocSizeInBits - ValSizeInBits, VA.getLocVT()));
2715 }
2716
2717 switch (VA.getLocInfo()) {
2718 default:
2719 llvm_unreachable("Unknown loc info!");
2720 case CCValAssign::Full:
2721 break;
2722 case CCValAssign::BCvt:
2723 Val = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Val);
2724 break;
2725 case CCValAssign::AExt:
2726 case CCValAssign::AExtUpper:
2727 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2728 break;
2729 case CCValAssign::ZExt:
2730 case CCValAssign::ZExtUpper:
2731 Val = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Val,
2732 DAG.getValueType(VA.getValVT()));
2733 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2734 break;
2735 case CCValAssign::SExt:
2736 case CCValAssign::SExtUpper:
2737 Val = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Val,
2738 DAG.getValueType(VA.getValVT()));
2739 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val);
2740 break;
2741 }
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002742
2743 InVals.push_back(Val);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002744 }
Bruno Cardoso Lopes3e0d0302007-11-05 03:02:32 +00002745
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002746 return Chain;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002747}
2748
Akira Hatanakae2489122011-04-15 21:51:11 +00002749//===----------------------------------------------------------------------===//
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002750// Formal Arguments Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002751//===----------------------------------------------------------------------===//
Wesley Peck527da1b2010-11-23 03:31:01 +00002752/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002753/// and generate load operations for arguments places on the stack.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002754SDValue
2755MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanakaaef55c82011-04-15 21:00:26 +00002756 CallingConv::ID CallConv,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002757 bool IsVarArg,
Akira Hatanaka9e1d3692011-12-19 19:52:25 +00002758 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002759 SDLoc DL, SelectionDAG &DAG,
Akira Hatanakaaef55c82011-04-15 21:00:26 +00002760 SmallVectorImpl<SDValue> &InVals)
Akira Hatanakae2489122011-04-15 21:51:11 +00002761 const {
Bruno Cardoso Lopesa01ede22008-08-04 07:12:52 +00002762 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002763 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopes14033fb2007-08-28 05:08:16 +00002764 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002765
Dan Gohman31ae5862010-04-17 14:41:14 +00002766 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002767
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002768 // Used with vargs to acumulate store chains.
2769 std::vector<SDValue> OutChains;
2770
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002771 // Assign locations to all of the incoming arguments.
2772 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002773 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), ArgLocs,
2774 *DAG.getContext());
Daniel Sanders4abcfe22014-09-09 10:46:48 +00002775 MipsCC MipsCCInfo(CallConv, Subtarget, CCInfo);
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002776 Function::const_arg_iterator FuncArg =
2777 DAG.getMachineFunction().getFunction()->arg_begin();
Eric Christopher1c29a652014-07-18 22:55:25 +00002778 bool UseSoftFloat = Subtarget.abiUsesSoftFloat();
Bruno Cardoso Lopes4449e5d2007-07-11 23:16:16 +00002779
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002780 MipsCCInfo.analyzeFormalArguments(Ins, UseSoftFloat, FuncArg);
Akira Hatanaka4866fe12012-10-30 19:37:25 +00002781 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(),
2782 MipsCCInfo.hasByValArg());
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002783
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00002784 unsigned CurArgIdx = 0;
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002785 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002786
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00002787 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002788 CCValAssign &VA = ArgLocs[i];
Akira Hatanaka2c07f1f2012-10-27 00:44:39 +00002789 std::advance(FuncArg, Ins[i].OrigArgIndex - CurArgIdx);
2790 CurArgIdx = Ins[i].OrigArgIndex;
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002791 EVT ValVT = VA.getValVT();
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002792 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2793 bool IsRegLoc = VA.isRegLoc();
2794
2795 if (Flags.isByVal()) {
2796 assert(Flags.getByValSize() &&
2797 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002798 assert(ByValArg != MipsCCInfo.byval_end());
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002799 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg,
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002800 MipsCCInfo, *ByValArg);
2801 ++ByValArg;
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002802 continue;
2803 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002804
2805 // Arguments stored on registers
Akira Hatanakafb9bae32011-11-12 02:29:58 +00002806 if (IsRegLoc) {
Akira Hatanaka7d822522013-10-28 21:21:36 +00002807 MVT RegVT = VA.getLocVT();
Akira Hatanakacb4a1a82011-05-24 00:23:52 +00002808 unsigned ArgReg = VA.getLocReg();
Akira Hatanaka7d822522013-10-28 21:21:36 +00002809 const TargetRegisterClass *RC = getRegClassFor(RegVT);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002810
Wesley Peck527da1b2010-11-23 03:31:01 +00002811 // Transform the arguments stored on
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002812 // physical registers into virtual ones
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002813 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC);
2814 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
Wesley Peck527da1b2010-11-23 03:31:01 +00002815
2816 // If this is an 8 or 16-bit value, it has been passed promoted
2817 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002818 // truncate to the right size.
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002819 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattner3c049702009-03-26 05:28:14 +00002820 unsigned Opcode = 0;
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002821 if (VA.getLocInfo() == CCValAssign::SExt)
2822 Opcode = ISD::AssertSext;
2823 else if (VA.getLocInfo() == CCValAssign::ZExt)
2824 Opcode = ISD::AssertZext;
Chris Lattner3c049702009-03-26 05:28:14 +00002825 if (Opcode)
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002826 ArgValue = DAG.getNode(Opcode, DL, RegVT, ArgValue,
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002827 DAG.getValueType(ValVT));
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002828 ArgValue = DAG.getNode(ISD::TRUNCATE, DL, ValVT, ArgValue);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002829 }
2830
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002831 // Handle floating point arguments passed in integer registers and
2832 // long double arguments passed in floating point registers.
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002833 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00002834 (RegVT == MVT::i64 && ValVT == MVT::f64) ||
2835 (RegVT == MVT::f64 && ValVT == MVT::i64))
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002836 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
Eric Christopher1c29a652014-07-18 22:55:25 +00002837 else if (Subtarget.isABI_O32() && RegVT == MVT::i32 &&
Eric Christopherbf33a3c2014-07-02 23:18:40 +00002838 ValVT == MVT::f64) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002839 unsigned Reg2 = addLiveIn(DAG.getMachineFunction(),
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002840 getNextIntArgReg(ArgReg), RC);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002841 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT);
Eric Christopher1c29a652014-07-18 22:55:25 +00002842 if (!Subtarget.isLittle())
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002843 std::swap(ArgValue, ArgValue2);
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002844 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64,
Akira Hatanaka104b7e32011-10-28 19:55:48 +00002845 ArgValue, ArgValue2);
Bruno Cardoso Lopes3b7b3012009-03-19 02:12:28 +00002846 }
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002847
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002848 InVals.push_back(ArgValue);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002849 } else { // VA.isRegLoc()
2850
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002851 // sanity check
2852 assert(VA.isMemLoc());
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002853
Wesley Peck527da1b2010-11-23 03:31:01 +00002854 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002855 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakacb4a1a82011-05-24 00:23:52 +00002856 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002857
2858 // Create load nodes to retrieve arguments from the stack
Akira Hatanakaac8c6692012-10-27 00:29:43 +00002859 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Akira Hatanakad1c58ed2013-11-09 02:38:51 +00002860 SDValue Load = DAG.getLoad(ValVT, DL, Chain, FIN,
2861 MachinePointerInfo::getFixedStack(FI),
2862 false, false, false, 0);
2863 InVals.push_back(Load);
2864 OutChains.push_back(Load.getValue(1));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002865 }
Reid Kleckner7a59e082014-05-12 22:01:27 +00002866 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002867
Reid Kleckner7a59e082014-05-12 22:01:27 +00002868 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Reid Kleckner79418562014-05-09 22:32:13 +00002869 // The mips ABIs for returning structs by value requires that we copy
2870 // the sret argument into $v0 for the return. Save the argument into
2871 // a virtual register so that we can access it from the return points.
Reid Kleckner7a59e082014-05-12 22:01:27 +00002872 if (Ins[i].Flags.isSRet()) {
Reid Kleckner79418562014-05-09 22:32:13 +00002873 unsigned Reg = MipsFI->getSRetReturnReg();
2874 if (!Reg) {
2875 Reg = MF.getRegInfo().createVirtualRegister(
Eric Christopher1c29a652014-07-18 22:55:25 +00002876 getRegClassFor(Subtarget.isABI_N64() ? MVT::i64 : MVT::i32));
Reid Kleckner79418562014-05-09 22:32:13 +00002877 MipsFI->setSRetReturnReg(Reg);
2878 }
Reid Kleckner7a59e082014-05-12 22:01:27 +00002879 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[i]);
Reid Kleckner79418562014-05-09 22:32:13 +00002880 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
Reid Kleckner7a59e082014-05-12 22:01:27 +00002881 break;
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002882 }
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002883 }
2884
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002885 if (IsVarArg)
2886 writeVarArgRegs(OutChains, MipsCCInfo, Chain, DL, DAG);
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002887
Wesley Peck527da1b2010-11-23 03:31:01 +00002888 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002889 // the size of Ins and InVals. This only happens when on varg functions
2890 if (!OutChains.empty()) {
2891 OutChains.push_back(Chain);
Craig Topper48d114b2014-04-26 18:35:24 +00002892 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
Bruno Cardoso Lopesd6fff552010-02-06 19:20:49 +00002893 }
2894
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002895 return Chain;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002896}
2897
Akira Hatanakae2489122011-04-15 21:51:11 +00002898//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002899// Return Value Calling Convention Implementation
Akira Hatanakae2489122011-04-15 21:51:11 +00002900//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002901
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00002902bool
2903MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002904 MachineFunction &MF, bool IsVarArg,
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00002905 const SmallVectorImpl<ISD::OutputArg> &Outs,
2906 LLVMContext &Context) const {
2907 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002908 CCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
Akira Hatanaka9c8dcfc2012-10-10 01:27:09 +00002909 return CCInfo.CheckReturn(Outs, RetCC_Mips);
2910}
2911
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002912SDValue
2913MipsTargetLowering::LowerReturn(SDValue Chain,
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002914 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002915 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002916 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002917 SDLoc DL, SelectionDAG &DAG) const {
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002918 // CCValAssign - represent the assignment of
2919 // the return value to a location
2920 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002921 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002922
2923 // CCState - Info about the registers and stack slot.
Eric Christopherb5217502014-08-06 18:45:26 +00002924 CCState CCInfo(CallConv, IsVarArg, MF, RVLocs, *DAG.getContext());
Daniel Sanders4abcfe22014-09-09 10:46:48 +00002925 MipsCC MipsCCInfo(CallConv, Subtarget, CCInfo);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002926
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002927 // Analyze return values.
Daniel Sanderse7473622014-09-18 08:28:39 +00002928 if (originalTypeIsF128(MF.getFunction()->getReturnType(), nullptr))
2929 CCInfo.AnalyzeReturn(Outs, RetCC_F128);
2930 else
2931 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002932
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002933 SDValue Flag;
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00002934 SmallVector<SDValue, 4> RetOps(1, Chain);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002935
2936 // Copy the result values into the output registers.
2937 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002938 SDValue Val = OutVals[i];
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002939 CCValAssign &VA = RVLocs[i];
2940 assert(VA.isRegLoc() && "Can only return in registers!");
Daniel Sandersae275e32014-09-25 12:15:05 +00002941 bool UseUpperBits = false;
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002942
Daniel Sandersae275e32014-09-25 12:15:05 +00002943 switch (VA.getLocInfo()) {
2944 default:
2945 llvm_unreachable("Unknown loc info!");
2946 case CCValAssign::Full:
2947 break;
2948 case CCValAssign::BCvt:
2949 Val = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Val);
2950 break;
2951 case CCValAssign::AExtUpper:
2952 UseUpperBits = true;
2953 // Fallthrough
2954 case CCValAssign::AExt:
2955 Val = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Val);
2956 break;
2957 case CCValAssign::ZExtUpper:
2958 UseUpperBits = true;
2959 // Fallthrough
2960 case CCValAssign::ZExt:
2961 Val = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Val);
2962 break;
2963 case CCValAssign::SExtUpper:
2964 UseUpperBits = true;
2965 // Fallthrough
2966 case CCValAssign::SExt:
2967 Val = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Val);
2968 break;
2969 }
2970
2971 if (UseUpperBits) {
2972 unsigned ValSizeInBits = Outs[i].ArgVT.getSizeInBits();
2973 unsigned LocSizeInBits = VA.getLocVT().getSizeInBits();
2974 Val = DAG.getNode(
2975 ISD::SHL, DL, VA.getLocVT(), Val,
2976 DAG.getConstant(LocSizeInBits - ValSizeInBits, VA.getLocVT()));
2977 }
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002978
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002979 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002980
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00002981 // Guarantee that all emitted copies are stuck together with flags.
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002982 Flag = Chain.getValue(1);
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00002983 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00002984 }
2985
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002986 // The mips ABIs for returning structs by value requires that we copy
2987 // the sret argument into $v0 for the return. We saved the argument into
2988 // a virtual register in the entry block, so now we copy the value out
2989 // and into $v0.
Akira Hatanaka5f3ba9e2013-03-05 22:41:55 +00002990 if (MF.getFunction()->hasStructRetAttr()) {
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002991 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2992 unsigned Reg = MipsFI->getSRetReturnReg();
2993
Wesley Peck527da1b2010-11-23 03:31:01 +00002994 if (!Reg)
Torok Edwinfbcc6632009-07-14 16:55:14 +00002995 llvm_unreachable("sret virtual register not created in the entry block");
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002996 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Eric Christopher1c29a652014-07-18 22:55:25 +00002997 unsigned V0 = Subtarget.isABI_N64() ? Mips::V0_64 : Mips::V0;
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00002998
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00002999 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag);
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003000 Flag = Chain.getValue(1);
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003001 RetOps.push_back(DAG.getRegister(V0, getPointerTy()));
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003002 }
3003
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003004 RetOps[0] = Chain; // Update chain.
Akira Hatanakaefff7b72012-07-10 00:19:06 +00003005
Jakob Stoklund Olesena2060502013-02-05 18:12:03 +00003006 // Add the flag if we have it.
3007 if (Flag.getNode())
3008 RetOps.push_back(Flag);
3009
3010 // Return on Mips is always a "jr $ra"
Craig Topper48d114b2014-04-26 18:35:24 +00003011 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, RetOps);
Bruno Cardoso Lopes35e43c42007-06-06 07:42:06 +00003012}
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003013
Akira Hatanakae2489122011-04-15 21:51:11 +00003014//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003015// Mips Inline Assembly Support
Akira Hatanakae2489122011-04-15 21:51:11 +00003016//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003017
3018/// getConstraintType - Given a constraint letter, return the type of
3019/// constraint it is for this target.
3020MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peck527da1b2010-11-23 03:31:01 +00003021getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003022{
Daniel Sanders8b59af12013-11-12 12:56:01 +00003023 // Mips specific constraints
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003024 // GCC config/mips/constraints.md
3025 //
Wesley Peck527da1b2010-11-23 03:31:01 +00003026 // 'd' : An address register. Equivalent to r
3027 // unless generating MIPS16 code.
3028 // 'y' : Equivalent to r; retained for
3029 // backwards compatibility.
Eric Christophere3c494d2012-05-07 06:25:10 +00003030 // 'c' : A register suitable for use in an indirect
3031 // jump. This will always be $25 for -mabicalls.
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003032 // 'l' : The lo register. 1 word storage.
3033 // 'x' : The hilo register pair. Double word storage.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003034 if (Constraint.size() == 1) {
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003035 switch (Constraint[0]) {
3036 default : break;
Wesley Peck527da1b2010-11-23 03:31:01 +00003037 case 'd':
3038 case 'y':
Bruno Cardoso Lopesc9c3f492008-07-05 19:05:21 +00003039 case 'f':
Eric Christophere3c494d2012-05-07 06:25:10 +00003040 case 'c':
Eric Christopher9c492e62012-05-07 06:25:15 +00003041 case 'l':
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003042 case 'x':
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003043 return C_RegisterClass;
Jack Carter0e149b02013-03-04 21:33:15 +00003044 case 'R':
3045 return C_Memory;
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003046 }
3047 }
3048 return TargetLowering::getConstraintType(Constraint);
3049}
3050
John Thompsone8360b72010-10-29 17:29:13 +00003051/// Examine constraint type and operand type and determine a weight value.
3052/// This object must already have been set up with the operand type
3053/// and the current alternative constraint selected.
3054TargetLowering::ConstraintWeight
3055MipsTargetLowering::getSingleConstraintMatchWeight(
3056 AsmOperandInfo &info, const char *constraint) const {
3057 ConstraintWeight weight = CW_Invalid;
3058 Value *CallOperandVal = info.CallOperandVal;
3059 // If we don't have a value, we can't do a match,
3060 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +00003061 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +00003062 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +00003063 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +00003064 // Look at the constraint type.
3065 switch (*constraint) {
3066 default:
3067 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
3068 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00003069 case 'd':
3070 case 'y':
John Thompsone8360b72010-10-29 17:29:13 +00003071 if (type->isIntegerTy())
3072 weight = CW_Register;
3073 break;
Daniel Sanders8b59af12013-11-12 12:56:01 +00003074 case 'f': // FPU or MSA register
Eric Christopher1c29a652014-07-18 22:55:25 +00003075 if (Subtarget.hasMSA() && type->isVectorTy() &&
Daniel Sanders8b59af12013-11-12 12:56:01 +00003076 cast<VectorType>(type)->getBitWidth() == 128)
3077 weight = CW_Register;
3078 else if (type->isFloatTy())
John Thompsone8360b72010-10-29 17:29:13 +00003079 weight = CW_Register;
3080 break;
Eric Christophere3c494d2012-05-07 06:25:10 +00003081 case 'c': // $25 for indirect jumps
Eric Christopher9c492e62012-05-07 06:25:15 +00003082 case 'l': // lo register
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003083 case 'x': // hilo register pair
Daniel Sanders8b59af12013-11-12 12:56:01 +00003084 if (type->isIntegerTy())
Eric Christophere3c494d2012-05-07 06:25:10 +00003085 weight = CW_SpecificReg;
Daniel Sanders8b59af12013-11-12 12:56:01 +00003086 break;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003087 case 'I': // signed 16 bit immediate
Eric Christopher7201e1b2012-05-07 03:13:42 +00003088 case 'J': // integer zero
Eric Christopher3ff88a02012-05-07 05:46:29 +00003089 case 'K': // unsigned 16 bit immediate
Eric Christopher1109b342012-05-07 05:46:37 +00003090 case 'L': // signed 32 bit immediate where lower 16 bits are 0
Eric Christophere07aa432012-05-07 05:46:43 +00003091 case 'N': // immediate in the range of -65535 to -1 (inclusive)
Eric Christopher470578a2012-05-07 05:46:48 +00003092 case 'O': // signed 15 bit immediate (+- 16383)
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003093 case 'P': // immediate in the range of 65535 to 1 (inclusive)
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003094 if (isa<ConstantInt>(CallOperandVal))
3095 weight = CW_Constant;
3096 break;
Jack Carter0e149b02013-03-04 21:33:15 +00003097 case 'R':
3098 weight = CW_Memory;
3099 break;
John Thompsone8360b72010-10-29 17:29:13 +00003100 }
3101 return weight;
3102}
3103
Akira Hatanaka7473b472013-08-14 00:21:25 +00003104/// This is a helper function to parse a physical register string and split it
3105/// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag
3106/// that is returned indicates whether parsing was successful. The second flag
3107/// is true if the numeric part exists.
3108static std::pair<bool, bool>
Craig Topper6dc4a8bc2014-08-30 16:48:02 +00003109parsePhysicalReg(StringRef C, std::string &Prefix,
Akira Hatanaka7473b472013-08-14 00:21:25 +00003110 unsigned long long &Reg) {
3111 if (C.front() != '{' || C.back() != '}')
3112 return std::make_pair(false, false);
3113
3114 // Search for the first numeric character.
3115 StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1;
3116 I = std::find_if(B, E, std::ptr_fun(isdigit));
3117
3118 Prefix.assign(B, I - B);
3119
3120 // The second flag is set to false if no numeric characters were found.
3121 if (I == E)
3122 return std::make_pair(true, false);
3123
3124 // Parse the numeric characters.
3125 return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg),
3126 true);
3127}
3128
3129std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering::
Craig Topper6dc4a8bc2014-08-30 16:48:02 +00003130parseRegForInlineAsmConstraint(StringRef C, MVT VT) const {
Eric Christopherd9134482014-08-04 21:25:23 +00003131 const TargetRegisterInfo *TRI =
3132 getTargetMachine().getSubtargetImpl()->getRegisterInfo();
Akira Hatanaka7473b472013-08-14 00:21:25 +00003133 const TargetRegisterClass *RC;
3134 std::string Prefix;
3135 unsigned long long Reg;
3136
3137 std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg);
3138
3139 if (!R.first)
Craig Topper062a2ba2014-04-25 05:30:21 +00003140 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003141
3142 if ((Prefix == "hi" || Prefix == "lo")) { // Parse hi/lo.
3143 // No numeric characters follow "hi" or "lo".
3144 if (R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003145 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003146
3147 RC = TRI->getRegClass(Prefix == "hi" ?
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00003148 Mips::HI32RegClassID : Mips::LO32RegClassID);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003149 return std::make_pair(*(RC->begin()), RC);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003150 } else if (Prefix.compare(0, 4, "$msa") == 0) {
3151 // Parse $msa(ir|csr|access|save|modify|request|map|unmap)
3152
3153 // No numeric characters follow the name.
3154 if (R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003155 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003156
3157 Reg = StringSwitch<unsigned long long>(Prefix)
3158 .Case("$msair", Mips::MSAIR)
3159 .Case("$msacsr", Mips::MSACSR)
3160 .Case("$msaaccess", Mips::MSAAccess)
3161 .Case("$msasave", Mips::MSASave)
3162 .Case("$msamodify", Mips::MSAModify)
3163 .Case("$msarequest", Mips::MSARequest)
3164 .Case("$msamap", Mips::MSAMap)
3165 .Case("$msaunmap", Mips::MSAUnmap)
3166 .Default(0);
3167
3168 if (!Reg)
Craig Topper062a2ba2014-04-25 05:30:21 +00003169 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003170
3171 RC = TRI->getRegClass(Mips::MSACtrlRegClassID);
3172 return std::make_pair(Reg, RC);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003173 }
3174
3175 if (!R.second)
Craig Topper062a2ba2014-04-25 05:30:21 +00003176 return std::make_pair(0U, nullptr);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003177
3178 if (Prefix == "$f") { // Parse $f0-$f31.
3179 // If the size of FP registers is 64-bit or Reg is an even number, select
3180 // the 64-bit register class. Otherwise, select the 32-bit register class.
3181 if (VT == MVT::Other)
Eric Christopher1c29a652014-07-18 22:55:25 +00003182 VT = (Subtarget.isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32;
Akira Hatanaka7473b472013-08-14 00:21:25 +00003183
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003184 RC = getRegClassFor(VT);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003185
3186 if (RC == &Mips::AFGR64RegClass) {
3187 assert(Reg % 2 == 0);
3188 Reg >>= 1;
3189 }
Daniel Sanders8b59af12013-11-12 12:56:01 +00003190 } else if (Prefix == "$fcc") // Parse $fcc0-$fcc7.
Akira Hatanaka7473b472013-08-14 00:21:25 +00003191 RC = TRI->getRegClass(Mips::FCCRegClassID);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003192 else if (Prefix == "$w") { // Parse $w0-$w31.
3193 RC = getRegClassFor((VT == MVT::Other) ? MVT::v16i8 : VT);
Akira Hatanaka7473b472013-08-14 00:21:25 +00003194 } else { // Parse $0-$31.
3195 assert(Prefix == "$");
3196 RC = getRegClassFor((VT == MVT::Other) ? MVT::i32 : VT);
3197 }
3198
3199 assert(Reg < RC->getNumRegs());
3200 return std::make_pair(*(RC->begin() + Reg), RC);
3201}
3202
Eric Christophereaf77dc2011-06-29 19:33:04 +00003203/// Given a register class constraint, like 'r', if this corresponds directly
3204/// to an LLVM register class, return a register of 0 and the register class
3205/// pointer.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003206std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Chad Rosier295bd432013-06-22 18:37:38 +00003207getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003208{
3209 if (Constraint.size() == 1) {
3210 switch (Constraint[0]) {
Eric Christopher9519c082011-06-29 19:04:31 +00003211 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
3212 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003213 case 'r':
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003214 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
Eric Christopher1c29a652014-07-18 22:55:25 +00003215 if (Subtarget.inMips16Mode())
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003216 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003217 return std::make_pair(0U, &Mips::GPR32RegClass);
Akira Hatanaka92a96e12012-09-12 23:27:55 +00003218 }
Eric Christopher1c29a652014-07-18 22:55:25 +00003219 if (VT == MVT::i64 && !Subtarget.isGP64bit())
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003220 return std::make_pair(0U, &Mips::GPR32RegClass);
Eric Christopher1c29a652014-07-18 22:55:25 +00003221 if (VT == MVT::i64 && Subtarget.isGP64bit())
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003222 return std::make_pair(0U, &Mips::GPR64RegClass);
Eric Christopher58daf042012-05-07 03:13:22 +00003223 // This will generate an error message
Craig Topper062a2ba2014-04-25 05:30:21 +00003224 return std::make_pair(0U, nullptr);
Daniel Sanders8b59af12013-11-12 12:56:01 +00003225 case 'f': // FPU or MSA register
3226 if (VT == MVT::v16i8)
3227 return std::make_pair(0U, &Mips::MSA128BRegClass);
3228 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
3229 return std::make_pair(0U, &Mips::MSA128HRegClass);
3230 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
3231 return std::make_pair(0U, &Mips::MSA128WRegClass);
3232 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
3233 return std::make_pair(0U, &Mips::MSA128DRegClass);
3234 else if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00003235 return std::make_pair(0U, &Mips::FGR32RegClass);
Eric Christopher1c29a652014-07-18 22:55:25 +00003236 else if ((VT == MVT::f64) && (!Subtarget.isSingleFloat())) {
3237 if (Subtarget.isFP64bit())
Craig Topperc7242e02012-04-20 07:30:17 +00003238 return std::make_pair(0U, &Mips::FGR64RegClass);
3239 return std::make_pair(0U, &Mips::AFGR64RegClass);
Akira Hatanakac669d7a2012-01-04 02:45:01 +00003240 }
Eric Christophere3c494d2012-05-07 06:25:10 +00003241 break;
3242 case 'c': // register suitable for indirect jump
3243 if (VT == MVT::i32)
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003244 return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass);
Eric Christophere3c494d2012-05-07 06:25:10 +00003245 assert(VT == MVT::i64 && "Unexpected type.");
Akira Hatanaka13e6ccf2013-08-06 23:08:38 +00003246 return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass);
Eric Christopher9c492e62012-05-07 06:25:15 +00003247 case 'l': // register suitable for indirect jump
3248 if (VT == MVT::i32)
Akira Hatanaka8002a3f2013-08-14 00:47:08 +00003249 return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass);
3250 return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass);
Eric Christopher0d8c15d2012-05-07 06:25:19 +00003251 case 'x': // register suitable for indirect jump
3252 // Fixme: Not triggering the use of both hi and low
3253 // This will generate an error message
Craig Topper062a2ba2014-04-25 05:30:21 +00003254 return std::make_pair(0U, nullptr);
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003255 }
3256 }
Akira Hatanaka7473b472013-08-14 00:21:25 +00003257
3258 std::pair<unsigned, const TargetRegisterClass *> R;
3259 R = parseRegForInlineAsmConstraint(Constraint, VT);
3260
3261 if (R.second)
3262 return R;
3263
Bruno Cardoso Lopesb10580a2007-08-21 16:09:25 +00003264 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3265}
3266
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003267/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3268/// vector. If it is invalid, don't add anything to Ops.
3269void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3270 std::string &Constraint,
3271 std::vector<SDValue>&Ops,
3272 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +00003273 SDValue Result;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003274
3275 // Only support length 1 constraints for now.
3276 if (Constraint.length() > 1) return;
3277
3278 char ConstraintLetter = Constraint[0];
3279 switch (ConstraintLetter) {
3280 default: break; // This will fall through to the generic implementation
3281 case 'I': // Signed 16 bit constant
3282 // If this fails, the parent routine will give an error
3283 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3284 EVT Type = Op.getValueType();
3285 int64_t Val = C->getSExtValue();
3286 if (isInt<16>(Val)) {
3287 Result = DAG.getTargetConstant(Val, Type);
3288 break;
3289 }
3290 }
3291 return;
Eric Christopher7201e1b2012-05-07 03:13:42 +00003292 case 'J': // integer zero
3293 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3294 EVT Type = Op.getValueType();
3295 int64_t Val = C->getZExtValue();
3296 if (Val == 0) {
3297 Result = DAG.getTargetConstant(0, Type);
3298 break;
3299 }
3300 }
3301 return;
Eric Christopher3ff88a02012-05-07 05:46:29 +00003302 case 'K': // unsigned 16 bit immediate
3303 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3304 EVT Type = Op.getValueType();
3305 uint64_t Val = (uint64_t)C->getZExtValue();
3306 if (isUInt<16>(Val)) {
3307 Result = DAG.getTargetConstant(Val, Type);
3308 break;
3309 }
3310 }
3311 return;
Eric Christopher1109b342012-05-07 05:46:37 +00003312 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3313 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3314 EVT Type = Op.getValueType();
3315 int64_t Val = C->getSExtValue();
3316 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
3317 Result = DAG.getTargetConstant(Val, Type);
3318 break;
3319 }
3320 }
3321 return;
Eric Christophere07aa432012-05-07 05:46:43 +00003322 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3323 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3324 EVT Type = Op.getValueType();
3325 int64_t Val = C->getSExtValue();
3326 if ((Val >= -65535) && (Val <= -1)) {
3327 Result = DAG.getTargetConstant(Val, Type);
3328 break;
3329 }
3330 }
3331 return;
Eric Christopher470578a2012-05-07 05:46:48 +00003332 case 'O': // signed 15 bit immediate
3333 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3334 EVT Type = Op.getValueType();
3335 int64_t Val = C->getSExtValue();
3336 if ((isInt<15>(Val))) {
3337 Result = DAG.getTargetConstant(Val, Type);
3338 break;
3339 }
3340 }
3341 return;
Eric Christopherc18ae4a2012-05-07 06:25:02 +00003342 case 'P': // immediate in the range of 1 to 65535 (inclusive)
3343 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3344 EVT Type = Op.getValueType();
3345 int64_t Val = C->getSExtValue();
3346 if ((Val <= 65535) && (Val >= 1)) {
3347 Result = DAG.getTargetConstant(Val, Type);
3348 break;
3349 }
3350 }
3351 return;
Eric Christopher1d6c89e2012-05-07 03:13:32 +00003352 }
3353
3354 if (Result.getNode()) {
3355 Ops.push_back(Result);
3356 return;
3357 }
3358
3359 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3360}
3361
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003362bool MipsTargetLowering::isLegalAddressingMode(const AddrMode &AM,
3363 Type *Ty) const {
Akira Hatanakaef839192012-11-17 00:25:41 +00003364 // No global is ever allowed as a base.
3365 if (AM.BaseGV)
3366 return false;
3367
3368 switch (AM.Scale) {
3369 case 0: // "r+i" or just "i", depending on HasBaseReg.
3370 break;
3371 case 1:
3372 if (!AM.HasBaseReg) // allow "r+i".
3373 break;
3374 return false; // disallow "r+r" or "r+r+i".
3375 default:
3376 return false;
3377 }
3378
3379 return true;
3380}
3381
3382bool
Dan Gohman2fe6bee2008-10-18 02:06:02 +00003383MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3384 // The Mips target isn't yet aware of offsets.
3385 return false;
3386}
Evan Cheng16993aa2009-10-27 19:56:55 +00003387
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003388EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003389 unsigned SrcAlign,
3390 bool IsMemset, bool ZeroMemset,
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003391 bool MemcpyStrSrc,
3392 MachineFunction &MF) const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003393 if (Subtarget.hasMips64())
Akira Hatanaka1daf8c22012-06-13 19:33:32 +00003394 return MVT::i64;
3395
3396 return MVT::i32;
3397}
3398
Evan Cheng83896a52009-10-28 01:43:28 +00003399bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
3400 if (VT != MVT::f32 && VT != MVT::f64)
3401 return false;
Bruno Cardoso Lopesb02a9df2011-01-18 19:41:41 +00003402 if (Imm.isNegZero())
3403 return false;
Evan Cheng16993aa2009-10-27 19:56:55 +00003404 return Imm.isZero();
3405}
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003406
3407unsigned MipsTargetLowering::getJumpTableEncoding() const {
Eric Christopher1c29a652014-07-18 22:55:25 +00003408 if (Subtarget.isABI_N64())
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003409 return MachineJumpTableInfo::EK_GPRel64BlockAddress;
Jia Liuf54f60f2012-02-28 07:46:26 +00003410
Akira Hatanakaf0b08442012-02-03 04:33:00 +00003411 return TargetLowering::getJumpTableEncoding();
3412}
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003413
Akira Hatanakae092f722013-03-05 22:54:59 +00003414/// This function returns true if CallSym is a long double emulation routine.
3415static bool isF128SoftLibCall(const char *CallSym) {
3416 const char *const LibCalls[] =
3417 {"__addtf3", "__divtf3", "__eqtf2", "__extenddftf2", "__extendsftf2",
3418 "__fixtfdi", "__fixtfsi", "__fixtfti", "__fixunstfdi", "__fixunstfsi",
3419 "__fixunstfti", "__floatditf", "__floatsitf", "__floattitf",
3420 "__floatunditf", "__floatunsitf", "__floatuntitf", "__getf2", "__gttf2",
3421 "__letf2", "__lttf2", "__multf3", "__netf2", "__powitf2", "__subtf3",
3422 "__trunctfdf2", "__trunctfsf2", "__unordtf2",
3423 "ceill", "copysignl", "cosl", "exp2l", "expl", "floorl", "fmal", "fmodl",
3424 "log10l", "log2l", "logl", "nearbyintl", "powl", "rintl", "sinl", "sqrtl",
3425 "truncl"};
3426
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003427 const char *const *End = LibCalls + array_lengthof(LibCalls);
Akira Hatanakae092f722013-03-05 22:54:59 +00003428
3429 // Check that LibCalls is sorted alphabetically.
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003430 MipsTargetLowering::LTStr Comp;
Akira Hatanakae092f722013-03-05 22:54:59 +00003431
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003432#ifndef NDEBUG
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003433 for (const char *const *I = LibCalls; I < End - 1; ++I)
Akira Hatanakae092f722013-03-05 22:54:59 +00003434 assert(Comp(*I, *(I + 1)));
3435#endif
3436
Akira Hatanaka96ca1822013-03-13 00:54:29 +00003437 return std::binary_search(LibCalls, End, CallSym, Comp);
Akira Hatanakae092f722013-03-05 22:54:59 +00003438}
3439
3440/// This function returns true if Ty is fp128 or i128 which was originally a
3441/// fp128.
3442static bool originalTypeIsF128(const Type *Ty, const SDNode *CallNode) {
3443 if (Ty->isFP128Ty())
3444 return true;
3445
3446 const ExternalSymbolSDNode *ES =
3447 dyn_cast_or_null<const ExternalSymbolSDNode>(CallNode);
3448
3449 // If the Ty is i128 and the function being called is a long double emulation
3450 // routine, then the original type is f128.
3451 return (ES && Ty->isIntegerTy(128) && isF128SoftLibCall(ES->getSymbol()));
3452}
3453
Reed Kotler783c7942013-05-10 22:25:39 +00003454MipsTargetLowering::MipsCC::SpecialCallingConvType
3455 MipsTargetLowering::getSpecialCallingConv(SDValue Callee) const {
3456 MipsCC::SpecialCallingConvType SpecialCallingConv =
Alp Toker98444342014-04-19 23:56:35 +00003457 MipsCC::NoSpecialCallingConv;
Eric Christopher1c29a652014-07-18 22:55:25 +00003458 if (Subtarget.inMips16HardFloat()) {
Reed Kotler783c7942013-05-10 22:25:39 +00003459 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3460 llvm::StringRef Sym = G->getGlobal()->getName();
3461 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
Reed Kotler3230e722013-12-12 02:41:11 +00003462 if (F && F->hasFnAttribute("__Mips16RetHelper")) {
Reed Kotler783c7942013-05-10 22:25:39 +00003463 SpecialCallingConv = MipsCC::Mips16RetHelperConv;
3464 }
3465 }
3466 }
3467 return SpecialCallingConv;
3468}
3469
3470MipsTargetLowering::MipsCC::MipsCC(
Daniel Sanders4abcfe22014-09-09 10:46:48 +00003471 CallingConv::ID CC, const MipsSubtarget &Subtarget_, CCState &Info,
3472 MipsCC::SpecialCallingConvType SpecialCallingConv_)
3473 : CCInfo(Info), CallConv(CC), Subtarget(Subtarget_),
3474 SpecialCallingConv(SpecialCallingConv_) {
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003475 // Pre-allocate reserved argument area.
Akira Hatanaka5001be52013-02-15 21:45:11 +00003476 CCInfo.AllocateStack(reservedArgArea(), 1);
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003477}
3478
Reed Kotler783c7942013-05-10 22:25:39 +00003479
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003480void MipsTargetLowering::MipsCC::
Akira Hatanaka5001be52013-02-15 21:45:11 +00003481analyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Args,
Akira Hatanaka3b7391d2013-03-05 22:20:28 +00003482 bool IsVarArg, bool IsSoftFloat, const SDNode *CallNode,
3483 std::vector<ArgListEntry> &FuncArgs) {
Akira Hatanaka5001be52013-02-15 21:45:11 +00003484 assert((CallConv != CallingConv::Fast || !IsVarArg) &&
3485 "CallingConv::Fast shouldn't be used for vararg functions.");
3486
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003487 unsigned NumOpnds = Args.size();
Akira Hatanaka5001be52013-02-15 21:45:11 +00003488 llvm::CCAssignFn *FixedFn = fixedArgFn(), *VarFn = varArgFn();
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003489
3490 for (unsigned I = 0; I != NumOpnds; ++I) {
3491 MVT ArgVT = Args[I].VT;
3492 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
3493 bool R;
3494
3495 if (ArgFlags.isByVal()) {
3496 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
3497 continue;
3498 }
3499
Akira Hatanaka5001be52013-02-15 21:45:11 +00003500 if (IsVarArg && !Args[I].IsFixed)
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003501 R = VarFn(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
Akira Hatanaka3b7391d2013-03-05 22:20:28 +00003502 else {
3503 MVT RegVT = getRegVT(ArgVT, FuncArgs[Args[I].OrigArgIndex].Ty, CallNode,
3504 IsSoftFloat);
3505 R = FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo);
3506 }
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003507
3508 if (R) {
3509#ifndef NDEBUG
3510 dbgs() << "Call operand #" << I << " has unhandled type "
3511 << EVT(ArgVT).getEVTString();
3512#endif
Craig Toppere73658d2014-04-28 04:05:08 +00003513 llvm_unreachable(nullptr);
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003514 }
3515 }
3516}
3517
3518void MipsTargetLowering::MipsCC::
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003519analyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Args,
3520 bool IsSoftFloat, Function::const_arg_iterator FuncArg) {
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003521 unsigned NumArgs = Args.size();
Akira Hatanaka5001be52013-02-15 21:45:11 +00003522 llvm::CCAssignFn *FixedFn = fixedArgFn();
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003523 unsigned CurArgIdx = 0;
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003524
3525 for (unsigned I = 0; I != NumArgs; ++I) {
3526 MVT ArgVT = Args[I].VT;
3527 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003528 std::advance(FuncArg, Args[I].OrigArgIndex - CurArgIdx);
3529 CurArgIdx = Args[I].OrigArgIndex;
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003530
3531 if (ArgFlags.isByVal()) {
3532 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
3533 continue;
3534 }
3535
Craig Topper062a2ba2014-04-25 05:30:21 +00003536 MVT RegVT = getRegVT(ArgVT, FuncArg->getType(), nullptr, IsSoftFloat);
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003537
3538 if (!FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo))
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003539 continue;
3540
3541#ifndef NDEBUG
3542 dbgs() << "Formal Arg #" << I << " has unhandled type "
3543 << EVT(ArgVT).getEVTString();
3544#endif
Craig Toppere73658d2014-04-28 04:05:08 +00003545 llvm_unreachable(nullptr);
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003546 }
3547}
3548
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003549void MipsTargetLowering::MipsCC::handleByValArg(unsigned ValNo, MVT ValVT,
3550 MVT LocVT,
3551 CCValAssign::LocInfo LocInfo,
3552 ISD::ArgFlagsTy ArgFlags) {
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003553 assert(ArgFlags.getByValSize() && "Byval argument's size shouldn't be 0.");
3554
3555 struct ByValArgInfo ByVal;
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003556 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
3557 unsigned ByValSize =
3558 RoundUpToAlignment(ArgFlags.getByValSize(), RegSizeInBytes);
3559 unsigned Align = std::min(std::max(ArgFlags.getByValAlign(), RegSizeInBytes),
3560 RegSizeInBytes * 2);
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003561
Akira Hatanaka5001be52013-02-15 21:45:11 +00003562 if (useRegsForByval())
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003563 allocateRegs(ByVal, ByValSize, Align);
3564
3565 // Allocate space on caller's stack.
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003566 ByVal.Address =
3567 CCInfo.AllocateStack(ByValSize - RegSizeInBytes * ByVal.NumRegs, Align);
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003568 CCInfo.addLoc(CCValAssign::getMem(ValNo, ValVT, ByVal.Address, LocVT,
3569 LocInfo));
3570 ByValArgs.push_back(ByVal);
3571}
3572
Akira Hatanaka5001be52013-02-15 21:45:11 +00003573unsigned MipsTargetLowering::MipsCC::reservedArgArea() const {
Daniel Sanders4abcfe22014-09-09 10:46:48 +00003574 return (Subtarget.isABI_O32() && (CallConv != CallingConv::Fast)) ? 16 : 0;
Akira Hatanaka5001be52013-02-15 21:45:11 +00003575}
3576
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003577const ArrayRef<MCPhysReg> MipsTargetLowering::MipsCC::intArgRegs() const {
3578 if (Subtarget.isABI_O32())
3579 return makeArrayRef(O32IntRegs);
3580 return makeArrayRef(Mips64IntRegs);
Akira Hatanaka5001be52013-02-15 21:45:11 +00003581}
3582
3583llvm::CCAssignFn *MipsTargetLowering::MipsCC::fixedArgFn() const {
3584 if (CallConv == CallingConv::Fast)
3585 return CC_Mips_FastCC;
3586
Reed Kotler783c7942013-05-10 22:25:39 +00003587 if (SpecialCallingConv == Mips16RetHelperConv)
3588 return CC_Mips16RetHelper;
Daniel Sanders4abcfe22014-09-09 10:46:48 +00003589 return Subtarget.isABI_O32()
3590 ? (Subtarget.isFP64bit() ? CC_MipsO32_FP64 : CC_MipsO32_FP32)
3591 : CC_MipsN;
Akira Hatanaka5001be52013-02-15 21:45:11 +00003592}
3593
3594llvm::CCAssignFn *MipsTargetLowering::MipsCC::varArgFn() const {
Daniel Sanders4abcfe22014-09-09 10:46:48 +00003595 return Subtarget.isABI_O32()
3596 ? (Subtarget.isFP64bit() ? CC_MipsO32_FP64 : CC_MipsO32_FP32)
3597 : CC_MipsN_VarArg;
Akira Hatanaka5001be52013-02-15 21:45:11 +00003598}
3599
Craig Topper840beec2014-04-04 05:16:06 +00003600const MCPhysReg *MipsTargetLowering::MipsCC::shadowRegs() const {
Daniel Sanders4abcfe22014-09-09 10:46:48 +00003601 return Subtarget.isABI_O32() ? O32IntRegs : Mips64DPRegs;
Akira Hatanaka5001be52013-02-15 21:45:11 +00003602}
3603
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003604void MipsTargetLowering::MipsCC::allocateRegs(ByValArgInfo &ByVal,
3605 unsigned ByValSize,
3606 unsigned Align) {
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003607 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003608 const ArrayRef<MCPhysReg> IntArgRegs = intArgRegs();
3609 const MCPhysReg *ShadowRegs = shadowRegs();
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003610 assert(!(ByValSize % RegSizeInBytes) && !(Align % RegSizeInBytes) &&
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003611 "Byval argument's size and alignment should be a multiple of"
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003612 "RegSizeInBytes.");
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003613
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003614 ByVal.FirstIdx =
3615 CCInfo.getFirstUnallocated(IntArgRegs.data(), IntArgRegs.size());
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003616
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003617 // If Align > RegSizeInBytes, the first arg register must be even.
3618 if ((Align > RegSizeInBytes) && (ByVal.FirstIdx % 2)) {
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003619 CCInfo.AllocateReg(IntArgRegs[ByVal.FirstIdx], ShadowRegs[ByVal.FirstIdx]);
3620 ++ByVal.FirstIdx;
3621 }
3622
3623 // Mark the registers allocated.
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003624 for (unsigned I = ByVal.FirstIdx; ByValSize && (I < IntArgRegs.size());
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003625 ByValSize -= RegSizeInBytes, ++I, ++ByVal.NumRegs)
Akira Hatanaka4a3711d2012-10-26 23:56:38 +00003626 CCInfo.AllocateReg(IntArgRegs[I], ShadowRegs[I]);
3627}
Akira Hatanaka25dad192012-10-27 00:10:18 +00003628
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003629MVT MipsTargetLowering::MipsCC::getRegVT(MVT VT, const Type *OrigTy,
3630 const SDNode *CallNode,
3631 bool IsSoftFloat) const {
Daniel Sanders4abcfe22014-09-09 10:46:48 +00003632 if (IsSoftFloat || Subtarget.isABI_O32())
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003633 return VT;
3634
3635 // Check if the original type was fp128.
Akira Hatanakae092f722013-03-05 22:54:59 +00003636 if (originalTypeIsF128(OrigTy, CallNode)) {
Akira Hatanaka4b634fa2013-03-05 22:13:04 +00003637 assert(VT == MVT::i64);
3638 return MVT::f64;
3639 }
3640
3641 return VT;
3642}
3643
Akira Hatanaka25dad192012-10-27 00:10:18 +00003644void MipsTargetLowering::
Andrew Trickef9de2a2013-05-25 02:42:55 +00003645copyByValRegs(SDValue Chain, SDLoc DL, std::vector<SDValue> &OutChains,
Akira Hatanaka25dad192012-10-27 00:10:18 +00003646 SelectionDAG &DAG, const ISD::ArgFlagsTy &Flags,
3647 SmallVectorImpl<SDValue> &InVals, const Argument *FuncArg,
3648 const MipsCC &CC, const ByValArgInfo &ByVal) const {
3649 MachineFunction &MF = DAG.getMachineFunction();
3650 MachineFrameInfo *MFI = MF.getFrameInfo();
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003651 unsigned GPRSizeInBytes = Subtarget.getGPRSizeInBytes();
3652 unsigned RegAreaSize = ByVal.NumRegs * GPRSizeInBytes;
Akira Hatanaka25dad192012-10-27 00:10:18 +00003653 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
3654 int FrameObjOffset;
3655
3656 if (RegAreaSize)
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003657 FrameObjOffset =
3658 (int)CC.reservedArgArea() -
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003659 (int)((CC.intArgRegs().size() - ByVal.FirstIdx) * GPRSizeInBytes);
Akira Hatanaka25dad192012-10-27 00:10:18 +00003660 else
3661 FrameObjOffset = ByVal.Address;
3662
3663 // Create frame object.
3664 EVT PtrTy = getPointerTy();
3665 int FI = MFI->CreateFixedObject(FrameObjSize, FrameObjOffset, true);
3666 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
3667 InVals.push_back(FIN);
3668
3669 if (!ByVal.NumRegs)
3670 return;
3671
3672 // Copy arg registers.
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003673 MVT RegTy = MVT::getIntegerVT(GPRSizeInBytes * 8);
Akira Hatanaka25dad192012-10-27 00:10:18 +00003674 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3675
3676 for (unsigned I = 0; I < ByVal.NumRegs; ++I) {
3677 unsigned ArgReg = CC.intArgRegs()[ByVal.FirstIdx + I];
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003678 unsigned VReg = addLiveIn(MF, ArgReg, RC);
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003679 unsigned Offset = I * GPRSizeInBytes;
Akira Hatanaka25dad192012-10-27 00:10:18 +00003680 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
3681 DAG.getConstant(Offset, PtrTy));
3682 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
3683 StorePtr, MachinePointerInfo(FuncArg, Offset),
3684 false, false, 0);
3685 OutChains.push_back(Store);
3686 }
3687}
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003688
3689// Copy byVal arg to registers and stack.
3690void MipsTargetLowering::
Andrew Trickef9de2a2013-05-25 02:42:55 +00003691passByValArg(SDValue Chain, SDLoc DL,
Akira Hatanakaf7d16d02013-01-22 20:05:56 +00003692 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
Craig Topperb94011f2013-07-14 04:42:23 +00003693 SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr,
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003694 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
3695 const MipsCC &CC, const ByValArgInfo &ByVal,
3696 const ISD::ArgFlagsTy &Flags, bool isLittle) const {
Daniel Sandersac272632014-05-23 13:18:02 +00003697 unsigned ByValSizeInBytes = Flags.getByValSize();
3698 unsigned OffsetInBytes = 0; // From beginning of struct
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003699 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
Daniel Sandersac272632014-05-23 13:18:02 +00003700 unsigned Alignment = std::min(Flags.getByValAlign(), RegSizeInBytes);
3701 EVT PtrTy = getPointerTy(), RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003702
3703 if (ByVal.NumRegs) {
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003704 const ArrayRef<MCPhysReg> ArgRegs = CC.intArgRegs();
Daniel Sandersac272632014-05-23 13:18:02 +00003705 bool LeftoverBytes = (ByVal.NumRegs * RegSizeInBytes > ByValSizeInBytes);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003706 unsigned I = 0;
3707
3708 // Copy words to registers.
Daniel Sandersac272632014-05-23 13:18:02 +00003709 for (; I < ByVal.NumRegs - LeftoverBytes;
3710 ++I, OffsetInBytes += RegSizeInBytes) {
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003711 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Daniel Sandersac272632014-05-23 13:18:02 +00003712 DAG.getConstant(OffsetInBytes, PtrTy));
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003713 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
3714 MachinePointerInfo(), false, false, false,
3715 Alignment);
3716 MemOpChains.push_back(LoadVal.getValue(1));
3717 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3718 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
3719 }
3720
3721 // Return if the struct has been fully copied.
Daniel Sandersac272632014-05-23 13:18:02 +00003722 if (ByValSizeInBytes == OffsetInBytes)
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003723 return;
3724
3725 // Copy the remainder of the byval argument with sub-word loads and shifts.
3726 if (LeftoverBytes) {
Daniel Sandersac272632014-05-23 13:18:02 +00003727 assert((ByValSizeInBytes > OffsetInBytes) &&
3728 (ByValSizeInBytes < OffsetInBytes + RegSizeInBytes) &&
3729 "Size of the remainder should be smaller than RegSizeInBytes.");
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003730 SDValue Val;
3731
Daniel Sandersac272632014-05-23 13:18:02 +00003732 for (unsigned LoadSizeInBytes = RegSizeInBytes / 2, TotalBytesLoaded = 0;
3733 OffsetInBytes < ByValSizeInBytes; LoadSizeInBytes /= 2) {
3734 unsigned RemainingSizeInBytes = ByValSizeInBytes - OffsetInBytes;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003735
Daniel Sandersac272632014-05-23 13:18:02 +00003736 if (RemainingSizeInBytes < LoadSizeInBytes)
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003737 continue;
3738
3739 // Load subword.
3740 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Daniel Sandersac272632014-05-23 13:18:02 +00003741 DAG.getConstant(OffsetInBytes, PtrTy));
3742 SDValue LoadVal = DAG.getExtLoad(
3743 ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr, MachinePointerInfo(),
Louis Gerbarg67474e32014-07-31 21:45:05 +00003744 MVT::getIntegerVT(LoadSizeInBytes * 8), false, false, false,
3745 Alignment);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003746 MemOpChains.push_back(LoadVal.getValue(1));
3747
3748 // Shift the loaded value.
3749 unsigned Shamt;
3750
3751 if (isLittle)
Daniel Sandersac272632014-05-23 13:18:02 +00003752 Shamt = TotalBytesLoaded * 8;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003753 else
Daniel Sandersac272632014-05-23 13:18:02 +00003754 Shamt = (RegSizeInBytes - (TotalBytesLoaded + LoadSizeInBytes)) * 8;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003755
3756 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
3757 DAG.getConstant(Shamt, MVT::i32));
3758
3759 if (Val.getNode())
3760 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
3761 else
3762 Val = Shift;
3763
Daniel Sandersac272632014-05-23 13:18:02 +00003764 OffsetInBytes += LoadSizeInBytes;
3765 TotalBytesLoaded += LoadSizeInBytes;
3766 Alignment = std::min(Alignment, LoadSizeInBytes);
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003767 }
3768
3769 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3770 RegsToPass.push_back(std::make_pair(ArgReg, Val));
3771 return;
3772 }
3773 }
3774
3775 // Copy remainder of byval arg to it with memcpy.
Daniel Sandersac272632014-05-23 13:18:02 +00003776 unsigned MemCpySize = ByValSizeInBytes - OffsetInBytes;
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003777 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
Daniel Sandersac272632014-05-23 13:18:02 +00003778 DAG.getConstant(OffsetInBytes, PtrTy));
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003779 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
3780 DAG.getIntPtrConstant(ByVal.Address));
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003781 Chain = DAG.getMemcpy(Chain, DL, Dst, Src, DAG.getConstant(MemCpySize, PtrTy),
3782 Alignment, /*isVolatile=*/false, /*AlwaysInline=*/false,
Nick Lewyckyaad475b2014-04-15 07:22:52 +00003783 MachinePointerInfo(), MachinePointerInfo());
Akira Hatanaka35f55b12012-10-27 00:16:36 +00003784 MemOpChains.push_back(Chain);
3785}
Akira Hatanaka2a134022012-10-27 00:21:13 +00003786
Akira Hatanaka4c0a7122013-10-07 19:33:02 +00003787void MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
3788 const MipsCC &CC, SDValue Chain,
3789 SDLoc DL, SelectionDAG &DAG) const {
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003790 const ArrayRef<MCPhysReg> ArgRegs = CC.intArgRegs();
Akira Hatanaka2a134022012-10-27 00:21:13 +00003791 const CCState &CCInfo = CC.getCCInfo();
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003792 unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs.data(), ArgRegs.size());
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003793 unsigned RegSizeInBytes = Subtarget.getGPRSizeInBytes();
3794 MVT RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003795 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3796 MachineFunction &MF = DAG.getMachineFunction();
3797 MachineFrameInfo *MFI = MF.getFrameInfo();
3798 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3799
3800 // Offset of the first variable argument from stack pointer.
3801 int VaArgOffset;
3802
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003803 if (ArgRegs.size() == Idx)
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003804 VaArgOffset =
3805 RoundUpToAlignment(CCInfo.getNextStackOffset(), RegSizeInBytes);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003806 else
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003807 VaArgOffset = (int)CC.reservedArgArea() -
3808 (int)(RegSizeInBytes * (ArgRegs.size() - Idx));
Akira Hatanaka2a134022012-10-27 00:21:13 +00003809
3810 // Record the frame index of the first variable argument
3811 // which is a value necessary to VASTART.
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003812 int FI = MFI->CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003813 MipsFI->setVarArgsFrameIndex(FI);
3814
3815 // Copy the integer registers that have not been used for argument passing
3816 // to the argument register save area. For O32, the save area is allocated
3817 // in the caller's stack frame, while for N32/64, it is allocated in the
3818 // callee's stack frame.
Daniel Sanders75ee6b42014-09-10 10:37:03 +00003819 for (unsigned I = Idx; I < ArgRegs.size();
3820 ++I, VaArgOffset += RegSizeInBytes) {
Akira Hatanaka0bb60d892013-03-12 00:16:36 +00003821 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003822 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
Daniel Sanders2b746bc2014-09-09 12:11:16 +00003823 FI = MFI->CreateFixedObject(RegSizeInBytes, VaArgOffset, true);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003824 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
3825 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
3826 MachinePointerInfo(), false, false, 0);
Eric Christopher1c29a652014-07-18 22:55:25 +00003827 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(
3828 (Value *)nullptr);
Akira Hatanaka2a134022012-10-27 00:21:13 +00003829 OutChains.push_back(Store);
3830 }
3831}