blob: 8f93ed3a52b753c6c8eb678668741906b015cac1 [file] [log] [blame]
Jia Liubb481f82012-02-28 07:46:26 +00001//===-- MipsISelLowering.cpp - Mips DAG Lowering Implementation -----------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Lopes972f5892007-06-06 07:42:06 +00007//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-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 Hatanaka4552c9a2011-04-15 21:51:11 +000013//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000014#define DEBUG_TYPE "mips-lower"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000015#include "MipsISelLowering.h"
Craig Topper79aa3412012-03-17 18:46:09 +000016#include "InstPrinter/MipsInstPrinter.h"
17#include "MCTargetDesc/MipsBaseInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000018#include "MipsMachineFunction.h"
19#include "MipsSubtarget.h"
20#include "MipsTargetMachine.h"
21#include "MipsTargetObjectFile.h"
Akira Hatanaka2b861be2012-10-19 21:47:33 +000022#include "llvm/ADT/Statistic.h"
Bruno Cardoso Lopes972f5892007-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"
Chris Lattner84bc5422007-12-31 04:13:23 +000027#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000028#include "llvm/CodeGen/SelectionDAGISel.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000029#include "llvm/CodeGen/ValueTypes.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000030#include "llvm/IR/CallingConv.h"
31#include "llvm/IR/DerivedTypes.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000032#include "llvm/IR/GlobalVariable.h"
Akira Hatanaka2b861be2012-10-19 21:47:33 +000033#include "llvm/Support/CommandLine.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000034#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000035#include "llvm/Support/ErrorHandling.h"
NAKAMURA Takumi89593932012-04-21 15:31:45 +000036#include "llvm/Support/raw_ostream.h"
Akira Hatanakabfb07b12013-08-14 00:21:25 +000037#include <cctype>
NAKAMURA Takumi89593932012-04-21 15:31:45 +000038
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000039using namespace llvm;
40
Akira Hatanaka2b861be2012-10-19 21:47:33 +000041STATISTIC(NumTailCalls, "Number of tail calls");
42
43static cl::opt<bool>
Akira Hatanaka81784cb2012-11-21 20:21:11 +000044LargeGOT("mxgot", cl::Hidden,
45 cl::desc("MIPS: Enable GOT larger than 64k."), cl::init(false));
46
Akira Hatanakaf8941992013-05-20 18:07:43 +000047static cl::opt<bool>
Akira Hatanaka2591b5c2013-05-21 17:17:59 +000048NoZeroDivCheck("mno-check-zero-division", cl::Hidden,
Akira Hatanakaf8941992013-05-20 18:07:43 +000049 cl::desc("MIPS: Don't trap on integer division by zero."),
50 cl::init(false));
51
Akira Hatanakafe30a9b2012-10-27 00:29:43 +000052static const uint16_t O32IntRegs[4] = {
53 Mips::A0, Mips::A1, Mips::A2, Mips::A3
54};
55
56static const uint16_t Mips64IntRegs[8] = {
57 Mips::A0_64, Mips::A1_64, Mips::A2_64, Mips::A3_64,
58 Mips::T0_64, Mips::T1_64, Mips::T2_64, Mips::T3_64
59};
60
61static const uint16_t Mips64DPRegs[8] = {
62 Mips::D12_64, Mips::D13_64, Mips::D14_64, Mips::D15_64,
63 Mips::D16_64, Mips::D17_64, Mips::D18_64, Mips::D19_64
64};
65
Jia Liubb481f82012-02-28 07:46:26 +000066// If I is a shifted mask, set the size (Size) and the first bit of the
Akira Hatanakadbe9a312011-08-18 20:07:42 +000067// mask (Pos), and return true.
Jia Liubb481f82012-02-28 07:46:26 +000068// For example, if I is 0x003ff800, (Pos, Size) = (11, 11).
Akira Hatanakaf635ef42013-03-12 00:16:36 +000069static bool isShiftedMask(uint64_t I, uint64_t &Pos, uint64_t &Size) {
Akira Hatanakad6bc5232011-12-05 21:26:34 +000070 if (!isShiftedMask_64(I))
Akira Hatanaka854a7db2011-08-19 22:59:00 +000071 return false;
Akira Hatanakabb15e112011-08-17 02:05:42 +000072
Akira Hatanakad6bc5232011-12-05 21:26:34 +000073 Size = CountPopulation_64(I);
Michael J. Spencerc6af2432013-05-24 22:23:49 +000074 Pos = countTrailingZeros(I);
Akira Hatanakadbe9a312011-08-18 20:07:42 +000075 return true;
Akira Hatanakabb15e112011-08-17 02:05:42 +000076}
77
Akira Hatanaka5ac065a2013-03-13 00:54:29 +000078SDValue MipsTargetLowering::getGlobalReg(SelectionDAG &DAG, EVT Ty) const {
Akira Hatanaka648f00c2012-02-24 22:34:47 +000079 MipsFunctionInfo *FI = DAG.getMachineFunction().getInfo<MipsFunctionInfo>();
80 return DAG.getRegister(FI->getGlobalBaseReg(), Ty);
81}
82
Akira Hatanaka79380342013-09-25 00:30:25 +000083template<class NodeTy>
84static SDValue getTargetNode(NodeTy *Node, EVT Ty, SelectionDAG &DAG,
85 unsigned Flag) {
Akira Hatanaka6b28b802012-11-21 20:26:38 +000086 llvm_unreachable("Unexpected node type.");
87 return SDValue();
88}
89
Akira Hatanaka79380342013-09-25 00:30:25 +000090template<>
91SDValue getTargetNode<GlobalAddressSDNode>(GlobalAddressSDNode *N, EVT Ty,
92 SelectionDAG &DAG, unsigned Flag) {
93 return DAG.getTargetGlobalAddress(N->getGlobal(), SDLoc(N), Ty, 0, Flag);
94}
95
96template<>
97SDValue getTargetNode<ExternalSymbolSDNode>(ExternalSymbolSDNode *N, EVT Ty,
98 SelectionDAG &DAG, unsigned Flag) {
99 return DAG.getTargetExternalSymbol(N->getSymbol(), Ty, Flag);
100}
101
102template<>
103SDValue getTargetNode<BlockAddressSDNode>(BlockAddressSDNode *N, EVT Ty,
104 SelectionDAG &DAG, unsigned Flag) {
105 return DAG.getTargetBlockAddress(N->getBlockAddress(), Ty, 0, Flag);
106}
107
108template<>
109SDValue getTargetNode<JumpTableSDNode>(JumpTableSDNode *N, EVT Ty,
110 SelectionDAG &DAG, unsigned Flag) {
111 return DAG.getTargetJumpTable(N->getIndex(), Ty, Flag);
112}
113
114template<>
115SDValue getTargetNode<ConstantPoolSDNode>(ConstantPoolSDNode *N, EVT Ty,
116 SelectionDAG &DAG, unsigned Flag) {
117 return DAG.getTargetConstantPool(N->getConstVal(), Ty, N->getAlignment(),
118 N->getOffset(), Flag);
119}
120
121template<class NodeTy>
122static SDValue getAddrNonPIC(NodeTy *N, EVT Ty, SelectionDAG &DAG) {
123 SDLoc DL(N);
124 SDValue Hi = getTargetNode(N, Ty, DAG, MipsII::MO_ABS_HI);
125 SDValue Lo = getTargetNode(N, Ty, DAG, MipsII::MO_ABS_LO);
Akira Hatanaka6b28b802012-11-21 20:26:38 +0000126 return DAG.getNode(ISD::ADD, DL, Ty,
127 DAG.getNode(MipsISD::Hi, DL, Ty, Hi),
128 DAG.getNode(MipsISD::Lo, DL, Ty, Lo));
129}
130
Akira Hatanaka79380342013-09-25 00:30:25 +0000131template<class NodeTy>
132SDValue MipsTargetLowering::getAddrLocal(NodeTy *N, EVT Ty, SelectionDAG &DAG,
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000133 bool HasMips64) const {
Akira Hatanaka79380342013-09-25 00:30:25 +0000134 SDLoc DL(N);
Akira Hatanaka6b28b802012-11-21 20:26:38 +0000135 unsigned GOTFlag = HasMips64 ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT;
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000136 SDValue GOT = DAG.getNode(MipsISD::Wrapper, DL, Ty, getGlobalReg(DAG, Ty),
Akira Hatanaka79380342013-09-25 00:30:25 +0000137 getTargetNode(N, Ty, DAG, GOTFlag));
Akira Hatanaka6b28b802012-11-21 20:26:38 +0000138 SDValue Load = DAG.getLoad(Ty, DL, DAG.getEntryNode(), GOT,
139 MachinePointerInfo::getGOT(), false, false, false,
140 0);
141 unsigned LoFlag = HasMips64 ? MipsII::MO_GOT_OFST : MipsII::MO_ABS_LO;
Akira Hatanaka79380342013-09-25 00:30:25 +0000142 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, Ty,
143 getTargetNode(N, Ty, DAG, LoFlag));
Akira Hatanaka6b28b802012-11-21 20:26:38 +0000144 return DAG.getNode(ISD::ADD, DL, Ty, Load, Lo);
145}
146
Akira Hatanaka79380342013-09-25 00:30:25 +0000147template<class NodeTy>
148SDValue MipsTargetLowering::getAddrGlobal(NodeTy *N, EVT Ty, SelectionDAG &DAG,
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000149 unsigned Flag) const {
Akira Hatanaka79380342013-09-25 00:30:25 +0000150 SDLoc DL(N);
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000151 SDValue Tgt = DAG.getNode(MipsISD::Wrapper, DL, Ty, getGlobalReg(DAG, Ty),
Akira Hatanaka79380342013-09-25 00:30:25 +0000152 getTargetNode(N, Ty, DAG, Flag));
Akira Hatanaka6b28b802012-11-21 20:26:38 +0000153 return DAG.getLoad(Ty, DL, DAG.getEntryNode(), Tgt,
154 MachinePointerInfo::getGOT(), false, false, false, 0);
155}
156
Akira Hatanaka79380342013-09-25 00:30:25 +0000157template<class NodeTy>
158SDValue MipsTargetLowering::getAddrGlobalLargeGOT(NodeTy *N, EVT Ty,
159 SelectionDAG &DAG,
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000160 unsigned HiFlag,
161 unsigned LoFlag) const {
Akira Hatanaka79380342013-09-25 00:30:25 +0000162 SDLoc DL(N);
163 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, Ty,
164 getTargetNode(N, Ty, DAG, HiFlag));
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000165 Hi = DAG.getNode(ISD::ADD, DL, Ty, Hi, getGlobalReg(DAG, Ty));
Akira Hatanaka6b28b802012-11-21 20:26:38 +0000166 SDValue Wrapper = DAG.getNode(MipsISD::Wrapper, DL, Ty, Hi,
Akira Hatanaka79380342013-09-25 00:30:25 +0000167 getTargetNode(N, Ty, DAG, LoFlag));
Akira Hatanaka6b28b802012-11-21 20:26:38 +0000168 return DAG.getLoad(Ty, DL, DAG.getEntryNode(), Wrapper,
169 MachinePointerInfo::getGOT(), false, false, false, 0);
170}
171
Chris Lattnerf0144122009-07-28 03:13:23 +0000172const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
173 switch (Opcode) {
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000174 case MipsISD::JmpLink: return "MipsISD::JmpLink";
Akira Hatanaka58d1e3f2012-10-19 20:59:39 +0000175 case MipsISD::TailCall: return "MipsISD::TailCall";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000176 case MipsISD::Hi: return "MipsISD::Hi";
177 case MipsISD::Lo: return "MipsISD::Lo";
178 case MipsISD::GPRel: return "MipsISD::GPRel";
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +0000179 case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000180 case MipsISD::Ret: return "MipsISD::Ret";
Akira Hatanaka544cc212013-01-30 00:26:49 +0000181 case MipsISD::EH_RETURN: return "MipsISD::EH_RETURN";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000182 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
183 case MipsISD::FPCmp: return "MipsISD::FPCmp";
184 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
185 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +0000186 case MipsISD::TruncIntFP: return "MipsISD::TruncIntFP";
Akira Hatanakadd958922013-03-30 01:14:04 +0000187 case MipsISD::ExtractLOHI: return "MipsISD::ExtractLOHI";
188 case MipsISD::InsertLOHI: return "MipsISD::InsertLOHI";
189 case MipsISD::Mult: return "MipsISD::Mult";
190 case MipsISD::Multu: return "MipsISD::Multu";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000191 case MipsISD::MAdd: return "MipsISD::MAdd";
192 case MipsISD::MAddu: return "MipsISD::MAddu";
193 case MipsISD::MSub: return "MipsISD::MSub";
194 case MipsISD::MSubu: return "MipsISD::MSubu";
195 case MipsISD::DivRem: return "MipsISD::DivRem";
196 case MipsISD::DivRemU: return "MipsISD::DivRemU";
Akira Hatanakadd958922013-03-30 01:14:04 +0000197 case MipsISD::DivRem16: return "MipsISD::DivRem16";
198 case MipsISD::DivRemU16: return "MipsISD::DivRemU16";
Akira Hatanakabdd2ce92011-05-23 21:13:59 +0000199 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
200 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
Akira Hatanakabfcb83f2011-12-12 22:38:19 +0000201 case MipsISD::Wrapper: return "MipsISD::Wrapper";
Akira Hatanakadb548262011-07-19 23:30:50 +0000202 case MipsISD::Sync: return "MipsISD::Sync";
Akira Hatanakabb15e112011-08-17 02:05:42 +0000203 case MipsISD::Ext: return "MipsISD::Ext";
204 case MipsISD::Ins: return "MipsISD::Ins";
Akira Hatanakab6f1dc22012-06-02 00:03:12 +0000205 case MipsISD::LWL: return "MipsISD::LWL";
206 case MipsISD::LWR: return "MipsISD::LWR";
207 case MipsISD::SWL: return "MipsISD::SWL";
208 case MipsISD::SWR: return "MipsISD::SWR";
209 case MipsISD::LDL: return "MipsISD::LDL";
210 case MipsISD::LDR: return "MipsISD::LDR";
211 case MipsISD::SDL: return "MipsISD::SDL";
212 case MipsISD::SDR: return "MipsISD::SDR";
Akira Hatanaka6fad5e72012-09-21 23:52:47 +0000213 case MipsISD::EXTP: return "MipsISD::EXTP";
214 case MipsISD::EXTPDP: return "MipsISD::EXTPDP";
215 case MipsISD::EXTR_S_H: return "MipsISD::EXTR_S_H";
216 case MipsISD::EXTR_W: return "MipsISD::EXTR_W";
217 case MipsISD::EXTR_R_W: return "MipsISD::EXTR_R_W";
218 case MipsISD::EXTR_RS_W: return "MipsISD::EXTR_RS_W";
219 case MipsISD::SHILO: return "MipsISD::SHILO";
220 case MipsISD::MTHLIP: return "MipsISD::MTHLIP";
221 case MipsISD::MULT: return "MipsISD::MULT";
222 case MipsISD::MULTU: return "MipsISD::MULTU";
Jia Liub3ea8802013-03-04 01:06:54 +0000223 case MipsISD::MADD_DSP: return "MipsISD::MADD_DSP";
Akira Hatanaka6fad5e72012-09-21 23:52:47 +0000224 case MipsISD::MADDU_DSP: return "MipsISD::MADDU_DSP";
225 case MipsISD::MSUB_DSP: return "MipsISD::MSUB_DSP";
226 case MipsISD::MSUBU_DSP: return "MipsISD::MSUBU_DSP";
Akira Hatanaka97a62bf2013-04-19 23:21:32 +0000227 case MipsISD::SHLL_DSP: return "MipsISD::SHLL_DSP";
228 case MipsISD::SHRA_DSP: return "MipsISD::SHRA_DSP";
229 case MipsISD::SHRL_DSP: return "MipsISD::SHRL_DSP";
Akira Hatanakacd6c5792013-04-30 22:37:26 +0000230 case MipsISD::SETCC_DSP: return "MipsISD::SETCC_DSP";
231 case MipsISD::SELECT_CC_DSP: return "MipsISD::SELECT_CC_DSP";
Daniel Sanders3c380d52013-08-28 12:14:50 +0000232 case MipsISD::VALL_ZERO: return "MipsISD::VALL_ZERO";
233 case MipsISD::VANY_ZERO: return "MipsISD::VANY_ZERO";
234 case MipsISD::VALL_NONZERO: return "MipsISD::VALL_NONZERO";
235 case MipsISD::VANY_NONZERO: return "MipsISD::VANY_NONZERO";
Daniel Sandersae1fb8f2013-09-24 10:46:19 +0000236 case MipsISD::VCEQ: return "MipsISD::VCEQ";
237 case MipsISD::VCLE_S: return "MipsISD::VCLE_S";
238 case MipsISD::VCLE_U: return "MipsISD::VCLE_U";
239 case MipsISD::VCLT_S: return "MipsISD::VCLT_S";
240 case MipsISD::VCLT_U: return "MipsISD::VCLT_U";
Daniel Sanders89d13c12013-09-24 12:18:31 +0000241 case MipsISD::VSMAX: return "MipsISD::VSMAX";
242 case MipsISD::VSMIN: return "MipsISD::VSMIN";
243 case MipsISD::VUMAX: return "MipsISD::VUMAX";
244 case MipsISD::VUMIN: return "MipsISD::VUMIN";
Daniel Sanders9a1aaeb2013-09-23 14:03:12 +0000245 case MipsISD::VEXTRACT_SEXT_ELT: return "MipsISD::VEXTRACT_SEXT_ELT";
246 case MipsISD::VEXTRACT_ZEXT_ELT: return "MipsISD::VEXTRACT_ZEXT_ELT";
Daniel Sanders915432c2013-09-23 13:22:24 +0000247 case MipsISD::VNOR: return "MipsISD::VNOR";
Daniel Sanders7e0df9a2013-09-24 14:02:15 +0000248 case MipsISD::VSHF: return "MipsISD::VSHF";
Daniel Sanders93d99572013-09-24 14:20:00 +0000249 case MipsISD::SHF: return "MipsISD::SHF";
Daniel Sandersf5159642013-09-24 14:36:12 +0000250 case MipsISD::ILVEV: return "MipsISD::ILVEV";
251 case MipsISD::ILVOD: return "MipsISD::ILVOD";
252 case MipsISD::ILVL: return "MipsISD::ILVL";
253 case MipsISD::ILVR: return "MipsISD::ILVR";
Daniel Sanders3706eda2013-09-24 14:53:25 +0000254 case MipsISD::PCKEV: return "MipsISD::PCKEV";
255 case MipsISD::PCKOD: return "MipsISD::PCKOD";
Akira Hatanaka0f843822011-06-07 18:58:42 +0000256 default: return NULL;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000257 }
258}
259
260MipsTargetLowering::
Chris Lattnerf0144122009-07-28 03:13:23 +0000261MipsTargetLowering(MipsTargetMachine &TM)
Akira Hatanaka8b4198d2011-09-26 21:47:02 +0000262 : TargetLowering(TM, new MipsTargetObjectFile()),
263 Subtarget(&TM.getSubtarget<MipsSubtarget>()),
Akira Hatanaka2ec69fa2011-10-28 18:47:24 +0000264 HasMips64(Subtarget->hasMips64()), IsN64(Subtarget->isABI_N64()),
265 IsO32(Subtarget->isABI_O32()) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000266 // Mips does not have i1 type, so use i32 for
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000267 // setcc operations results (slt, sgt, ...).
Duncan Sands03228082008-11-23 15:47:28 +0000268 setBooleanContents(ZeroOrOneBooleanContent);
Akira Hatanakacd6c5792013-04-30 22:37:26 +0000269 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000270
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000271 // Load extented operations for i1 types must be promoted
Owen Anderson825b72b2009-08-11 20:47:22 +0000272 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
273 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
274 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000275
Eli Friedman6055a6a2009-07-17 04:07:24 +0000276 // MIPS doesn't have extending float->double load/store
Owen Anderson825b72b2009-08-11 20:47:22 +0000277 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
278 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Eli Friedman10a36592009-07-17 02:28:12 +0000279
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000280 // Used by legalize types to correctly generate the setcc result.
281 // Without this, every float setcc comes with a AND/OR with the result,
282 // we don't want this, since the fpcmp result goes to a flag register,
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000283 // which is used implicitly by brcond and select operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000284 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +0000285
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000286 // Mips Custom Operations
Akira Hatanakab7656a92013-03-06 21:32:03 +0000287 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000288 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +0000289 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000290 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
291 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
292 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
293 setOperationAction(ISD::SELECT, MVT::f32, Custom);
294 setOperationAction(ISD::SELECT, MVT::f64, Custom);
295 setOperationAction(ISD::SELECT, MVT::i32, Custom);
Akira Hatanaka3fef29d2012-07-11 19:32:27 +0000296 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
297 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Akira Hatanaka0a40c232012-03-09 23:46:03 +0000298 setOperationAction(ISD::SETCC, MVT::f32, Custom);
299 setOperationAction(ISD::SETCC, MVT::f64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000300 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000301 setOperationAction(ISD::VASTART, MVT::Other, Custom);
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000302 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
303 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +0000304 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000305
Akira Hatanakac12a6e62012-04-11 22:49:04 +0000306 if (!TM.Options.NoNaNsFPMath) {
307 setOperationAction(ISD::FABS, MVT::f32, Custom);
308 setOperationAction(ISD::FABS, MVT::f64, Custom);
309 }
310
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000311 if (HasMips64) {
312 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
313 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
314 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
315 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
316 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
317 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Akira Hatanaka7664f052012-06-02 00:04:42 +0000318 setOperationAction(ISD::LOAD, MVT::i64, Custom);
319 setOperationAction(ISD::STORE, MVT::i64, Custom);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +0000320 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
Akira Hatanakad229b7b2012-03-10 00:03:50 +0000321 }
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +0000322
Akira Hatanakaa284acb2012-05-09 00:55:21 +0000323 if (!HasMips64) {
324 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
325 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
326 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
327 }
328
Akira Hatanakae90a3bc2012-11-07 19:10:58 +0000329 setOperationAction(ISD::ADD, MVT::i32, Custom);
330 if (HasMips64)
331 setOperationAction(ISD::ADD, MVT::i64, Custom);
332
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000333 setOperationAction(ISD::SDIV, MVT::i32, Expand);
334 setOperationAction(ISD::SREM, MVT::i32, Expand);
335 setOperationAction(ISD::UDIV, MVT::i32, Expand);
336 setOperationAction(ISD::UREM, MVT::i32, Expand);
Akira Hatanakadda4a072011-10-03 21:06:13 +0000337 setOperationAction(ISD::SDIV, MVT::i64, Expand);
338 setOperationAction(ISD::SREM, MVT::i64, Expand);
339 setOperationAction(ISD::UDIV, MVT::i64, Expand);
340 setOperationAction(ISD::UREM, MVT::i64, Expand);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000341
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000342 // Operations not directly supported by Mips.
Tom Stellard3ef53832013-03-08 15:36:57 +0000343 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
344 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
345 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
346 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000347 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
348 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Akira Hatanakae1bcd6b2011-12-20 23:40:56 +0000349 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000350 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Akira Hatanakae1bcd6b2011-12-20 23:40:56 +0000351 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000352 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
353 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
Akira Hatanaka7f162742011-12-21 00:14:05 +0000354 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000355 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
Akira Hatanaka7f162742011-12-21 00:14:05 +0000356 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000357 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
358 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
359 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
360 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000361 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000362 setOperationAction(ISD::ROTL, MVT::i64, Expand);
Akira Hatanaka1d165f12012-07-31 20:54:48 +0000363 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
364 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000365
Akira Hatanaka56633442011-09-20 23:53:09 +0000366 if (!Subtarget->hasMips32r2())
Bruno Cardoso Lopes908b6dd2010-12-09 17:32:30 +0000367 setOperationAction(ISD::ROTR, MVT::i32, Expand);
368
Akira Hatanakac7bafe92011-09-30 18:51:46 +0000369 if (!Subtarget->hasMips64r2())
370 setOperationAction(ISD::ROTR, MVT::i64, Expand);
371
Owen Anderson825b72b2009-08-11 20:47:22 +0000372 setOperationAction(ISD::FSIN, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000373 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000374 setOperationAction(ISD::FCOS, MVT::f32, Expand);
Bruno Cardoso Lopes5d6fb5d2011-03-04 18:54:14 +0000375 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng8688a582013-01-29 02:32:37 +0000376 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
377 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000378 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
379 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Akira Hatanaka46da1362011-05-23 22:23:58 +0000380 setOperationAction(ISD::FPOW, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000381 setOperationAction(ISD::FLOG, MVT::f32, Expand);
382 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
383 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
384 setOperationAction(ISD::FEXP, MVT::f32, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000385 setOperationAction(ISD::FMA, MVT::f32, Expand);
386 setOperationAction(ISD::FMA, MVT::f64, Expand);
Akira Hatanaka21ecc2f2012-03-29 18:43:11 +0000387 setOperationAction(ISD::FREM, MVT::f32, Expand);
388 setOperationAction(ISD::FREM, MVT::f64, Expand);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000389
Akira Hatanaka1cc63332012-04-11 22:59:08 +0000390 if (!TM.Options.NoNaNsFPMath) {
391 setOperationAction(ISD::FNEG, MVT::f32, Expand);
392 setOperationAction(ISD::FNEG, MVT::f64, Expand);
393 }
394
Akira Hatanaka544cc212013-01-30 00:26:49 +0000395 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
396
Bruno Cardoso Lopes954dac02011-03-09 19:22:22 +0000397 setOperationAction(ISD::VAARG, MVT::Other, Expand);
398 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
399 setOperationAction(ISD::VAEND, MVT::Other, Expand);
400
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +0000401 // Use the default for now
Owen Anderson825b72b2009-08-11 20:47:22 +0000402 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
403 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000404
Jia Liubb481f82012-02-28 07:46:26 +0000405 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
406 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
407 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
408 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
Eli Friedman4db5aca2011-08-29 18:23:02 +0000409
Eli Friedman26689ac2011-08-03 21:06:02 +0000410 setInsertFencesForAtomic(true);
411
Bruno Cardoso Lopes7728f7e2008-07-09 05:32:22 +0000412 if (!Subtarget->hasSEInReg()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000413 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
414 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +0000415 }
416
Akira Hatanakac79507a2011-12-21 00:20:27 +0000417 if (!Subtarget->hasBitCount()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000418 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Akira Hatanakac79507a2011-12-21 00:20:27 +0000419 setOperationAction(ISD::CTLZ, MVT::i64, Expand);
420 }
Bruno Cardoso Lopes65ad4522008-08-08 06:16:31 +0000421
Akira Hatanakac0ea0432011-12-20 23:56:43 +0000422 if (!Subtarget->hasSwap()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000423 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Akira Hatanakac0ea0432011-12-20 23:56:43 +0000424 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
425 }
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +0000426
Akira Hatanaka7664f052012-06-02 00:04:42 +0000427 if (HasMips64) {
428 setLoadExtAction(ISD::SEXTLOAD, MVT::i32, Custom);
429 setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, Custom);
430 setLoadExtAction(ISD::EXTLOAD, MVT::i32, Custom);
431 setTruncStoreAction(MVT::i64, MVT::i32, Custom);
432 }
433
Akira Hatanaka97585622013-07-26 20:58:55 +0000434 setOperationAction(ISD::TRAP, MVT::Other, Legal);
435
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000436 setTargetDAGCombine(ISD::SDIVREM);
437 setTargetDAGCombine(ISD::UDIVREM);
Akira Hatanakaee8c3b02012-03-08 03:26:37 +0000438 setTargetDAGCombine(ISD::SELECT);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000439 setTargetDAGCombine(ISD::AND);
440 setTargetDAGCombine(ISD::OR);
Akira Hatanaka87827072012-06-13 20:33:18 +0000441 setTargetDAGCombine(ISD::ADD);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000442
Akira Hatanaka5fdf5002012-03-08 01:59:33 +0000443 setMinFunctionAlignment(HasMips64 ? 3 : 2);
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000444
Akira Hatanaka3f5b1072012-02-02 03:17:04 +0000445 setStackPointerRegisterToSaveRestore(IsN64 ? Mips::SP_64 : Mips::SP);
Akira Hatanakacf0cd802011-05-26 18:59:03 +0000446
Akira Hatanaka590baca2012-02-02 03:13:40 +0000447 setExceptionPointerRegister(IsN64 ? Mips::A0_64 : Mips::A0);
448 setExceptionSelectorRegister(IsN64 ? Mips::A1_64 : Mips::A1);
Akira Hatanakae193b322012-06-13 19:33:32 +0000449
Jim Grosbach3450f802013-02-20 21:13:59 +0000450 MaxStoresPerMemcpy = 16;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000451}
452
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000453const MipsTargetLowering *MipsTargetLowering::create(MipsTargetMachine &TM) {
454 if (TM.getSubtargetImpl()->inMips16Mode())
455 return llvm::createMips16TargetLowering(TM);
Jia Liubb481f82012-02-28 07:46:26 +0000456
Akira Hatanaka5ac065a2013-03-13 00:54:29 +0000457 return llvm::createMipsSETargetLowering(TM);
Akira Hatanaka5c21c9e2011-08-12 21:30:06 +0000458}
459
Matt Arsenault225ed702013-05-18 00:21:46 +0000460EVT MipsTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Akira Hatanakae13f4412013-01-04 20:06:01 +0000461 if (!VT.isVector())
462 return MVT::i32;
463 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +0000464}
465
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000466static SDValue performDivRemCombine(SDNode *N, SelectionDAG &DAG,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000467 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000468 const MipsSubtarget *Subtarget) {
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000469 if (DCI.isBeforeLegalizeOps())
470 return SDValue();
471
Akira Hatanakadda4a072011-10-03 21:06:13 +0000472 EVT Ty = N->getValueType(0);
Akira Hatanakacbaf6d02013-08-14 00:47:08 +0000473 unsigned LO = (Ty == MVT::i32) ? Mips::LO0 : Mips::LO0_64;
474 unsigned HI = (Ty == MVT::i32) ? Mips::HI0 : Mips::HI0_64;
Akira Hatanakaf5926fd2013-03-30 01:36:35 +0000475 unsigned Opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem16 :
476 MipsISD::DivRemU16;
Andrew Trickac6d9be2013-05-25 02:42:55 +0000477 SDLoc DL(N);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000478
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000479 SDValue DivRem = DAG.getNode(Opc, DL, MVT::Glue,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000480 N->getOperand(0), N->getOperand(1));
481 SDValue InChain = DAG.getEntryNode();
482 SDValue InGlue = DivRem;
483
484 // insert MFLO
485 if (N->hasAnyUseOfValue(0)) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000486 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, DL, LO, Ty,
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000487 InGlue);
488 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
489 InChain = CopyFromLo.getValue(1);
490 InGlue = CopyFromLo.getValue(2);
491 }
492
493 // insert MFHI
494 if (N->hasAnyUseOfValue(1)) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000495 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, DL,
Akira Hatanakadda4a072011-10-03 21:06:13 +0000496 HI, Ty, InGlue);
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000497 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
498 }
499
500 return SDValue();
501}
502
Akira Hatanaka2fbe90c2013-04-18 01:00:46 +0000503static Mips::CondCode condCodeToFCC(ISD::CondCode CC) {
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000504 switch (CC) {
505 default: llvm_unreachable("Unknown fp condition code!");
506 case ISD::SETEQ:
507 case ISD::SETOEQ: return Mips::FCOND_OEQ;
508 case ISD::SETUNE: return Mips::FCOND_UNE;
509 case ISD::SETLT:
510 case ISD::SETOLT: return Mips::FCOND_OLT;
511 case ISD::SETGT:
512 case ISD::SETOGT: return Mips::FCOND_OGT;
513 case ISD::SETLE:
514 case ISD::SETOLE: return Mips::FCOND_OLE;
515 case ISD::SETGE:
516 case ISD::SETOGE: return Mips::FCOND_OGE;
517 case ISD::SETULT: return Mips::FCOND_ULT;
518 case ISD::SETULE: return Mips::FCOND_ULE;
519 case ISD::SETUGT: return Mips::FCOND_UGT;
520 case ISD::SETUGE: return Mips::FCOND_UGE;
521 case ISD::SETUO: return Mips::FCOND_UN;
522 case ISD::SETO: return Mips::FCOND_OR;
523 case ISD::SETNE:
524 case ISD::SETONE: return Mips::FCOND_ONE;
525 case ISD::SETUEQ: return Mips::FCOND_UEQ;
526 }
527}
528
529
Akira Hatanaka9cf07242013-03-30 01:16:38 +0000530/// This function returns true if the floating point conditional branches and
531/// conditional moves which use condition code CC should be inverted.
532static bool invertFPCondCodeUser(Mips::CondCode CC) {
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000533 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
534 return false;
535
Akira Hatanaka82099682011-12-19 19:52:25 +0000536 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
537 "Illegal Condition Code");
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000538
Akira Hatanaka82099682011-12-19 19:52:25 +0000539 return true;
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000540}
541
542// Creates and returns an FPCmp node from a setcc node.
543// Returns Op if setcc is not a floating point comparison.
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000544static SDValue createFPCmp(SelectionDAG &DAG, const SDValue &Op) {
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000545 // must be a SETCC node
546 if (Op.getOpcode() != ISD::SETCC)
547 return Op;
548
549 SDValue LHS = Op.getOperand(0);
550
551 if (!LHS.getValueType().isFloatingPoint())
552 return Op;
553
554 SDValue RHS = Op.getOperand(1);
Andrew Trickac6d9be2013-05-25 02:42:55 +0000555 SDLoc DL(Op);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000556
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +0000557 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
558 // node if necessary.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000559 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
560
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000561 return DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS,
Akira Hatanaka2fbe90c2013-04-18 01:00:46 +0000562 DAG.getConstant(condCodeToFCC(CC), MVT::i32));
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000563}
564
565// Creates and returns a CMovFPT/F node.
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000566static SDValue createCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000567 SDValue False, SDLoc DL) {
Akira Hatanaka9cf07242013-03-30 01:16:38 +0000568 ConstantSDNode *CC = cast<ConstantSDNode>(Cond.getOperand(2));
569 bool invert = invertFPCondCodeUser((Mips::CondCode)CC->getSExtValue());
Akira Hatanaka407883b2013-07-26 20:51:20 +0000570 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000571
572 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
Akira Hatanaka407883b2013-07-26 20:51:20 +0000573 True.getValueType(), True, FCC0, False, Cond);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000574}
575
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000576static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000577 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000578 const MipsSubtarget *Subtarget) {
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000579 if (DCI.isBeforeLegalizeOps())
580 return SDValue();
581
582 SDValue SetCC = N->getOperand(0);
583
584 if ((SetCC.getOpcode() != ISD::SETCC) ||
585 !SetCC.getOperand(0).getValueType().isInteger())
586 return SDValue();
587
588 SDValue False = N->getOperand(2);
589 EVT FalseTy = False.getValueType();
590
591 if (!FalseTy.isInteger())
592 return SDValue();
593
594 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(False);
595
596 if (!CN || CN->getZExtValue())
597 return SDValue();
598
Andrew Trickac6d9be2013-05-25 02:42:55 +0000599 const SDLoc DL(N);
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000600 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
601 SDValue True = N->getOperand(1);
Akira Hatanaka864f6602012-06-14 21:10:56 +0000602
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000603 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
604 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
Akira Hatanaka864f6602012-06-14 21:10:56 +0000605
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000606 return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True);
607}
608
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000609static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000610 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000611 const MipsSubtarget *Subtarget) {
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000612 // Pattern match EXT.
613 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
614 // => ext $dst, $src, size, pos
Akira Hatanaka56633442011-09-20 23:53:09 +0000615 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000616 return SDValue();
617
618 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000619 unsigned ShiftRightOpc = ShiftRight.getOpcode();
620
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000621 // Op's first operand must be a shift right.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000622 if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL)
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000623 return SDValue();
624
625 // The second operand of the shift must be an immediate.
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000626 ConstantSDNode *CN;
627 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
628 return SDValue();
Jia Liubb481f82012-02-28 07:46:26 +0000629
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000630 uint64_t Pos = CN->getZExtValue();
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000631 uint64_t SMPos, SMSize;
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000632
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000633 // Op's second operand must be a shifted mask.
634 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000635 !isShiftedMask(CN->getZExtValue(), SMPos, SMSize))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000636 return SDValue();
637
638 // Return if the shifted mask does not start at bit 0 or the sum of its size
639 // and Pos exceeds the word's size.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000640 EVT ValTy = N->getValueType(0);
641 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000642 return SDValue();
643
Andrew Trickac6d9be2013-05-25 02:42:55 +0000644 return DAG.getNode(MipsISD::Ext, SDLoc(N), ValTy,
Akira Hatanaka82099682011-12-19 19:52:25 +0000645 ShiftRight.getOperand(0), DAG.getConstant(Pos, MVT::i32),
Akira Hatanaka667645f2011-08-17 22:59:46 +0000646 DAG.getConstant(SMSize, MVT::i32));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000647}
Jia Liubb481f82012-02-28 07:46:26 +0000648
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000649static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000650 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000651 const MipsSubtarget *Subtarget) {
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000652 // Pattern match INS.
653 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
Jia Liubb481f82012-02-28 07:46:26 +0000654 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000655 // => ins $dst, $src, size, pos, $src1
Akira Hatanaka56633442011-09-20 23:53:09 +0000656 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000657 return SDValue();
658
659 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
660 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
661 ConstantSDNode *CN;
662
663 // See if Op's first operand matches (and $src1 , mask0).
664 if (And0.getOpcode() != ISD::AND)
665 return SDValue();
666
667 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000668 !isShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000669 return SDValue();
670
671 // See if Op's second operand matches (and (shl $src, pos), mask1).
672 if (And1.getOpcode() != ISD::AND)
673 return SDValue();
Jia Liubb481f82012-02-28 07:46:26 +0000674
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000675 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000676 !isShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000677 return SDValue();
678
679 // The shift masks must have the same position and size.
680 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
681 return SDValue();
682
683 SDValue Shl = And1.getOperand(0);
684 if (Shl.getOpcode() != ISD::SHL)
685 return SDValue();
686
687 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
688 return SDValue();
689
690 unsigned Shamt = CN->getZExtValue();
691
692 // Return if the shift amount and the first bit position of mask are not the
Jia Liubb481f82012-02-28 07:46:26 +0000693 // same.
Akira Hatanakad6bc5232011-12-05 21:26:34 +0000694 EVT ValTy = N->getValueType(0);
695 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000696 return SDValue();
Jia Liubb481f82012-02-28 07:46:26 +0000697
Andrew Trickac6d9be2013-05-25 02:42:55 +0000698 return DAG.getNode(MipsISD::Ins, SDLoc(N), ValTy, Shl.getOperand(0),
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000699 DAG.getConstant(SMPos0, MVT::i32),
Akira Hatanaka82099682011-12-19 19:52:25 +0000700 DAG.getConstant(SMSize0, MVT::i32), And0.getOperand(0));
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000701}
Jia Liubb481f82012-02-28 07:46:26 +0000702
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000703static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG,
Akira Hatanaka87827072012-06-13 20:33:18 +0000704 TargetLowering::DAGCombinerInfo &DCI,
Akira Hatanaka864f6602012-06-14 21:10:56 +0000705 const MipsSubtarget *Subtarget) {
Akira Hatanaka87827072012-06-13 20:33:18 +0000706 // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt))
707
708 if (DCI.isBeforeLegalizeOps())
709 return SDValue();
710
711 SDValue Add = N->getOperand(1);
712
713 if (Add.getOpcode() != ISD::ADD)
714 return SDValue();
715
716 SDValue Lo = Add.getOperand(1);
717
718 if ((Lo.getOpcode() != MipsISD::Lo) ||
719 (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable))
720 return SDValue();
721
722 EVT ValTy = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +0000723 SDLoc DL(N);
Akira Hatanaka87827072012-06-13 20:33:18 +0000724
725 SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, N->getOperand(0),
726 Add.getOperand(0));
727 return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo);
728}
729
Bruno Cardoso Lopes8e826e62011-02-10 18:05:10 +0000730SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000731 const {
732 SelectionDAG &DAG = DCI.DAG;
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000733 unsigned Opc = N->getOpcode();
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000734
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000735 switch (Opc) {
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000736 default: break;
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000737 case ISD::SDIVREM:
738 case ISD::UDIVREM:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000739 return performDivRemCombine(N, DAG, DCI, Subtarget);
Akira Hatanakae2bdf7f2012-03-08 02:14:24 +0000740 case ISD::SELECT:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000741 return performSELECTCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000742 case ISD::AND:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000743 return performANDCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka77b85b62011-08-17 17:45:08 +0000744 case ISD::OR:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000745 return performORCombine(N, DAG, DCI, Subtarget);
Akira Hatanaka87827072012-06-13 20:33:18 +0000746 case ISD::ADD:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000747 return performADDCombine(N, DAG, DCI, Subtarget);
Bruno Cardoso Lopes8be76112011-01-18 19:29:17 +0000748 }
749
750 return SDValue();
751}
752
Akira Hatanakab430cec2012-09-21 23:58:31 +0000753void
754MipsTargetLowering::LowerOperationWrapper(SDNode *N,
755 SmallVectorImpl<SDValue> &Results,
756 SelectionDAG &DAG) const {
757 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
758
759 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
760 Results.push_back(Res.getValue(I));
761}
762
763void
764MipsTargetLowering::ReplaceNodeResults(SDNode *N,
765 SmallVectorImpl<SDValue> &Results,
766 SelectionDAG &DAG) const {
Akira Hatanaka13ec4812013-04-30 21:17:07 +0000767 return LowerOperationWrapper(N, Results, DAG);
Akira Hatanakab430cec2012-09-21 23:58:31 +0000768}
769
Dan Gohman475871a2008-07-27 21:46:04 +0000770SDValue MipsTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000771LowerOperation(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000772{
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000773 switch (Op.getOpcode())
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000774 {
Akira Hatanaka2459afe2013-03-30 01:15:17 +0000775 case ISD::BR_JT: return lowerBR_JT(Op, DAG);
776 case ISD::BRCOND: return lowerBRCOND(Op, DAG);
777 case ISD::ConstantPool: return lowerConstantPool(Op, DAG);
778 case ISD::GlobalAddress: return lowerGlobalAddress(Op, DAG);
779 case ISD::BlockAddress: return lowerBlockAddress(Op, DAG);
780 case ISD::GlobalTLSAddress: return lowerGlobalTLSAddress(Op, DAG);
781 case ISD::JumpTable: return lowerJumpTable(Op, DAG);
782 case ISD::SELECT: return lowerSELECT(Op, DAG);
783 case ISD::SELECT_CC: return lowerSELECT_CC(Op, DAG);
784 case ISD::SETCC: return lowerSETCC(Op, DAG);
785 case ISD::VASTART: return lowerVASTART(Op, DAG);
786 case ISD::FCOPYSIGN: return lowerFCOPYSIGN(Op, DAG);
787 case ISD::FABS: return lowerFABS(Op, DAG);
788 case ISD::FRAMEADDR: return lowerFRAMEADDR(Op, DAG);
789 case ISD::RETURNADDR: return lowerRETURNADDR(Op, DAG);
790 case ISD::EH_RETURN: return lowerEH_RETURN(Op, DAG);
Akira Hatanaka2459afe2013-03-30 01:15:17 +0000791 case ISD::ATOMIC_FENCE: return lowerATOMIC_FENCE(Op, DAG);
792 case ISD::SHL_PARTS: return lowerShiftLeftParts(Op, DAG);
793 case ISD::SRA_PARTS: return lowerShiftRightParts(Op, DAG, true);
794 case ISD::SRL_PARTS: return lowerShiftRightParts(Op, DAG, false);
795 case ISD::LOAD: return lowerLOAD(Op, DAG);
796 case ISD::STORE: return lowerSTORE(Op, DAG);
Akira Hatanaka2459afe2013-03-30 01:15:17 +0000797 case ISD::ADD: return lowerADD(Op, DAG);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +0000798 case ISD::FP_TO_SINT: return lowerFP_TO_SINT(Op, DAG);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000799 }
Dan Gohman475871a2008-07-27 21:46:04 +0000800 return SDValue();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000801}
802
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000803//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000804// Lower helper functions
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000805//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000806
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000807// addLiveIn - This helper function adds the specified physical register to the
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000808// MachineFunction as a live in value. It also creates a corresponding
809// virtual register for it.
810static unsigned
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000811addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000812{
Chris Lattner84bc5422007-12-31 04:13:23 +0000813 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
814 MF.getRegInfo().addLiveIn(PReg, VReg);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000815 return VReg;
816}
817
Akira Hatanakaf8941992013-05-20 18:07:43 +0000818static MachineBasicBlock *expandPseudoDIV(MachineInstr *MI,
819 MachineBasicBlock &MBB,
820 const TargetInstrInfo &TII,
821 bool Is64Bit) {
822 if (NoZeroDivCheck)
823 return &MBB;
824
825 // Insert instruction "teq $divisor_reg, $zero, 7".
826 MachineBasicBlock::iterator I(MI);
827 MachineInstrBuilder MIB;
828 MIB = BuildMI(MBB, llvm::next(I), MI->getDebugLoc(), TII.get(Mips::TEQ))
829 .addOperand(MI->getOperand(2)).addReg(Mips::ZERO).addImm(7);
830
831 // Use the 32-bit sub-register if this is a 64-bit division.
832 if (Is64Bit)
833 MIB->getOperand(0).setSubReg(Mips::sub_32);
834
835 return &MBB;
836}
837
Akira Hatanaka01f70892012-09-27 02:15:57 +0000838MachineBasicBlock *
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000839MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000840 MachineBasicBlock *BB) const {
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000841 switch (MI->getOpcode()) {
Reed Kotlerffbe4322013-02-21 04:22:38 +0000842 default:
843 llvm_unreachable("Unexpected instr type to insert");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000844 case Mips::ATOMIC_LOAD_ADD_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000845 return emitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000846 case Mips::ATOMIC_LOAD_ADD_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000847 return emitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000848 case Mips::ATOMIC_LOAD_ADD_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000849 return emitAtomicBinary(MI, BB, 4, Mips::ADDu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000850 case Mips::ATOMIC_LOAD_ADD_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000851 return emitAtomicBinary(MI, BB, 8, Mips::DADDu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000852
853 case Mips::ATOMIC_LOAD_AND_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000854 return emitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000855 case Mips::ATOMIC_LOAD_AND_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000856 return emitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000857 case Mips::ATOMIC_LOAD_AND_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000858 return emitAtomicBinary(MI, BB, 4, Mips::AND);
Akira Hatanaka59068062011-11-11 04:14:30 +0000859 case Mips::ATOMIC_LOAD_AND_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000860 return emitAtomicBinary(MI, BB, 8, Mips::AND64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000861
862 case Mips::ATOMIC_LOAD_OR_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000863 return emitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000864 case Mips::ATOMIC_LOAD_OR_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000865 return emitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000866 case Mips::ATOMIC_LOAD_OR_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000867 return emitAtomicBinary(MI, BB, 4, Mips::OR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000868 case Mips::ATOMIC_LOAD_OR_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000869 return emitAtomicBinary(MI, BB, 8, Mips::OR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000870
871 case Mips::ATOMIC_LOAD_XOR_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000872 return emitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000873 case Mips::ATOMIC_LOAD_XOR_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000874 return emitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000875 case Mips::ATOMIC_LOAD_XOR_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000876 return emitAtomicBinary(MI, BB, 4, Mips::XOR);
Akira Hatanaka59068062011-11-11 04:14:30 +0000877 case Mips::ATOMIC_LOAD_XOR_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000878 return emitAtomicBinary(MI, BB, 8, Mips::XOR64);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000879
880 case Mips::ATOMIC_LOAD_NAND_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000881 return emitAtomicBinaryPartword(MI, BB, 1, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000882 case Mips::ATOMIC_LOAD_NAND_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000883 return emitAtomicBinaryPartword(MI, BB, 2, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000884 case Mips::ATOMIC_LOAD_NAND_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000885 return emitAtomicBinary(MI, BB, 4, 0, true);
Akira Hatanaka59068062011-11-11 04:14:30 +0000886 case Mips::ATOMIC_LOAD_NAND_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000887 return emitAtomicBinary(MI, BB, 8, 0, true);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000888
889 case Mips::ATOMIC_LOAD_SUB_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000890 return emitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000891 case Mips::ATOMIC_LOAD_SUB_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000892 return emitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000893 case Mips::ATOMIC_LOAD_SUB_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000894 return emitAtomicBinary(MI, BB, 4, Mips::SUBu);
Akira Hatanaka59068062011-11-11 04:14:30 +0000895 case Mips::ATOMIC_LOAD_SUB_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000896 return emitAtomicBinary(MI, BB, 8, Mips::DSUBu);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000897
898 case Mips::ATOMIC_SWAP_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000899 return emitAtomicBinaryPartword(MI, BB, 1, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000900 case Mips::ATOMIC_SWAP_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000901 return emitAtomicBinaryPartword(MI, BB, 2, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000902 case Mips::ATOMIC_SWAP_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000903 return emitAtomicBinary(MI, BB, 4, 0);
Akira Hatanaka59068062011-11-11 04:14:30 +0000904 case Mips::ATOMIC_SWAP_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000905 return emitAtomicBinary(MI, BB, 8, 0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000906
907 case Mips::ATOMIC_CMP_SWAP_I8:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000908 return emitAtomicCmpSwapPartword(MI, BB, 1);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000909 case Mips::ATOMIC_CMP_SWAP_I16:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000910 return emitAtomicCmpSwapPartword(MI, BB, 2);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000911 case Mips::ATOMIC_CMP_SWAP_I32:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000912 return emitAtomicCmpSwap(MI, BB, 4);
Akira Hatanaka59068062011-11-11 04:14:30 +0000913 case Mips::ATOMIC_CMP_SWAP_I64:
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000914 return emitAtomicCmpSwap(MI, BB, 8);
Akira Hatanakaf8941992013-05-20 18:07:43 +0000915 case Mips::PseudoSDIV:
916 case Mips::PseudoUDIV:
917 return expandPseudoDIV(MI, *BB, *getTargetMachine().getInstrInfo(), false);
918 case Mips::PseudoDSDIV:
919 case Mips::PseudoDUDIV:
920 return expandPseudoDIV(MI, *BB, *getTargetMachine().getInstrInfo(), true);
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +0000921 }
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +0000922}
923
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000924// This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
925// Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
926MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000927MipsTargetLowering::emitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Eric Christopher471e4222011-06-08 23:55:35 +0000928 unsigned Size, unsigned BinOpcode,
Akira Hatanaka0f843822011-06-07 18:58:42 +0000929 bool Nand) const {
Akira Hatanaka59068062011-11-11 04:14:30 +0000930 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000931
932 MachineFunction *MF = BB->getParent();
933 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +0000934 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000935 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000936 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +0000937 unsigned LL, SC, AND, NOR, ZERO, BEQ;
938
939 if (Size == 4) {
Akira Hatanakaa98a4862013-08-20 21:08:22 +0000940 LL = Mips::LL;
941 SC = Mips::SC;
Akira Hatanaka59068062011-11-11 04:14:30 +0000942 AND = Mips::AND;
943 NOR = Mips::NOR;
944 ZERO = Mips::ZERO;
945 BEQ = Mips::BEQ;
946 }
947 else {
Akira Hatanakaa98a4862013-08-20 21:08:22 +0000948 LL = Mips::LLD;
949 SC = Mips::SCD;
Akira Hatanaka59068062011-11-11 04:14:30 +0000950 AND = Mips::AND64;
951 NOR = Mips::NOR64;
952 ZERO = Mips::ZERO_64;
953 BEQ = Mips::BEQ64;
954 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000955
Akira Hatanaka4061da12011-07-19 20:11:17 +0000956 unsigned OldVal = MI->getOperand(0).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000957 unsigned Ptr = MI->getOperand(1).getReg();
958 unsigned Incr = MI->getOperand(2).getReg();
959
Akira Hatanaka4061da12011-07-19 20:11:17 +0000960 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
961 unsigned AndRes = RegInfo.createVirtualRegister(RC);
962 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000963
964 // insert new blocks after the current block
965 const BasicBlock *LLVM_BB = BB->getBasicBlock();
966 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
967 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
968 MachineFunction::iterator It = BB;
969 ++It;
970 MF->insert(It, loopMBB);
971 MF->insert(It, exitMBB);
972
973 // Transfer the remainder of BB and its successor edges to exitMBB.
974 exitMBB->splice(exitMBB->begin(), BB,
975 llvm::next(MachineBasicBlock::iterator(MI)),
976 BB->end());
977 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
978
979 // thisMBB:
980 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000981 // fallthrough --> loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000982 BB->addSuccessor(loopMBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +0000983 loopMBB->addSuccessor(loopMBB);
984 loopMBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000985
986 // loopMBB:
987 // ll oldval, 0(ptr)
Akira Hatanaka4061da12011-07-19 20:11:17 +0000988 // <binop> storeval, oldval, incr
989 // sc success, storeval, 0(ptr)
990 // beq success, $0, loopMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000991 BB = loopMBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000992 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000993 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000994 // and andres, oldval, incr
995 // nor storeval, $0, andres
Akira Hatanakaf635ef42013-03-12 00:16:36 +0000996 BuildMI(BB, DL, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
997 BuildMI(BB, DL, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +0000998 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +0000999 // <binop> storeval, oldval, incr
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001000 BuildMI(BB, DL, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001001 } else {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001002 StoreVal = Incr;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001003 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001004 BuildMI(BB, DL, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
1005 BuildMI(BB, DL, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001006
1007 MI->eraseFromParent(); // The instruction is gone now.
1008
Akira Hatanaka939ece12011-07-19 03:42:13 +00001009 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001010}
1011
1012MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001013MipsTargetLowering::emitAtomicBinaryPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001014 MachineBasicBlock *BB,
1015 unsigned Size, unsigned BinOpcode,
1016 bool Nand) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001017 assert((Size == 1 || Size == 2) &&
1018 "Unsupported size for EmitAtomicBinaryPartial.");
1019
1020 MachineFunction *MF = BB->getParent();
1021 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1022 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1023 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001024 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001025
1026 unsigned Dest = MI->getOperand(0).getReg();
1027 unsigned Ptr = MI->getOperand(1).getReg();
1028 unsigned Incr = MI->getOperand(2).getReg();
1029
Akira Hatanaka4061da12011-07-19 20:11:17 +00001030 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1031 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001032 unsigned Mask = RegInfo.createVirtualRegister(RC);
1033 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001034 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1035 unsigned OldVal = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001036 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001037 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1038 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1039 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1040 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1041 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001042 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001043 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1044 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1045 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1046 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1047 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001048
1049 // insert new blocks after the current block
1050 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1051 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001052 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001053 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1054 MachineFunction::iterator It = BB;
1055 ++It;
1056 MF->insert(It, loopMBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001057 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001058 MF->insert(It, exitMBB);
1059
1060 // Transfer the remainder of BB and its successor edges to exitMBB.
1061 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001062 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001063 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1064
Akira Hatanaka81b44112011-07-19 17:09:53 +00001065 BB->addSuccessor(loopMBB);
1066 loopMBB->addSuccessor(loopMBB);
1067 loopMBB->addSuccessor(sinkMBB);
1068 sinkMBB->addSuccessor(exitMBB);
1069
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001070 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001071 // addiu masklsb2,$0,-4 # 0xfffffffc
1072 // and alignedaddr,ptr,masklsb2
1073 // andi ptrlsb2,ptr,3
1074 // sll shiftamt,ptrlsb2,3
1075 // ori maskupper,$0,255 # 0xff
1076 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001077 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001078 // sll incr2,incr,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001079
1080 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001081 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001082 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001083 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001084 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001085 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Akira Hatanakaaffed7e2013-05-31 03:25:44 +00001086 if (Subtarget->isLittle()) {
1087 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1088 } else {
1089 unsigned Off = RegInfo.createVirtualRegister(RC);
1090 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1091 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1092 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1093 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001094 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001095 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001096 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka51122432013-07-01 20:39:53 +00001097 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001098 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
Akira Hatanaka51122432013-07-01 20:39:53 +00001099 BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(Incr).addReg(ShiftAmt);
Bruno Cardoso Lopescada2d02011-05-31 20:25:26 +00001100
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001101 // atomic.load.binop
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001102 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001103 // ll oldval,0(alignedaddr)
1104 // binop binopres,oldval,incr2
1105 // and newval,binopres,mask
1106 // and maskedoldval0,oldval,mask2
1107 // or storeval,maskedoldval0,newval
1108 // sc success,storeval,0(alignedaddr)
1109 // beq success,$0,loopMBB
1110
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001111 // atomic.swap
1112 // loopMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001113 // ll oldval,0(alignedaddr)
Akira Hatanaka70564a92011-07-19 18:14:26 +00001114 // and newval,incr2,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001115 // and maskedoldval0,oldval,mask2
1116 // or storeval,maskedoldval0,newval
1117 // sc success,storeval,0(alignedaddr)
1118 // beq success,$0,loopMBB
Akira Hatanaka0d7d0b52011-07-18 18:52:12 +00001119
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001120 BB = loopMBB;
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001121 BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001122 if (Nand) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001123 // and andres, oldval, incr2
1124 // nor binopres, $0, andres
1125 // and newval, binopres, mask
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001126 BuildMI(BB, DL, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1127 BuildMI(BB, DL, TII->get(Mips::NOR), BinOpRes)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001128 .addReg(Mips::ZERO).addReg(AndRes);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001129 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001130 } else if (BinOpcode) {
Akira Hatanaka4061da12011-07-19 20:11:17 +00001131 // <binop> binopres, oldval, incr2
1132 // and newval, binopres, mask
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001133 BuildMI(BB, DL, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1134 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001135 } else {// atomic.swap
Akira Hatanaka4061da12011-07-19 20:11:17 +00001136 // and newval, incr2, mask
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001137 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
Akira Hatanaka70564a92011-07-19 18:14:26 +00001138 }
Jia Liubb481f82012-02-28 07:46:26 +00001139
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001140 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001141 .addReg(OldVal).addReg(Mask2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001142 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanakabdd83fe2011-07-19 20:56:53 +00001143 .addReg(MaskedOldVal0).addReg(NewVal);
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001144 BuildMI(BB, DL, TII->get(Mips::SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001145 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001146 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001147 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001148
Akira Hatanaka939ece12011-07-19 03:42:13 +00001149 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001150 // and maskedoldval1,oldval,mask
1151 // srl srlres,maskedoldval1,shiftamt
1152 // sll sllres,srlres,24
1153 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001154 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001155 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001156
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001157 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001158 .addReg(OldVal).addReg(Mask);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001159 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka51122432013-07-01 20:39:53 +00001160 .addReg(MaskedOldVal1).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001161 BuildMI(BB, DL, TII->get(Mips::SLL), SllRes)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001162 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001163 BuildMI(BB, DL, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001164 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001165
1166 MI->eraseFromParent(); // The instruction is gone now.
1167
Akira Hatanaka939ece12011-07-19 03:42:13 +00001168 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001169}
1170
1171MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001172MipsTargetLowering::emitAtomicCmpSwap(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001173 MachineBasicBlock *BB,
1174 unsigned Size) const {
Akira Hatanaka59068062011-11-11 04:14:30 +00001175 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001176
1177 MachineFunction *MF = BB->getParent();
1178 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Akira Hatanaka59068062011-11-11 04:14:30 +00001179 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001180 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001181 DebugLoc DL = MI->getDebugLoc();
Akira Hatanaka59068062011-11-11 04:14:30 +00001182 unsigned LL, SC, ZERO, BNE, BEQ;
1183
1184 if (Size == 4) {
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001185 LL = Mips::LL;
1186 SC = Mips::SC;
Akira Hatanaka59068062011-11-11 04:14:30 +00001187 ZERO = Mips::ZERO;
1188 BNE = Mips::BNE;
1189 BEQ = Mips::BEQ;
1190 }
1191 else {
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001192 LL = Mips::LLD;
1193 SC = Mips::SCD;
Akira Hatanaka59068062011-11-11 04:14:30 +00001194 ZERO = Mips::ZERO_64;
1195 BNE = Mips::BNE64;
1196 BEQ = Mips::BEQ64;
1197 }
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001198
1199 unsigned Dest = MI->getOperand(0).getReg();
1200 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001201 unsigned OldVal = MI->getOperand(2).getReg();
1202 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001203
Akira Hatanaka4061da12011-07-19 20:11:17 +00001204 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001205
1206 // insert new blocks after the current block
1207 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1208 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1209 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1210 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1211 MachineFunction::iterator It = BB;
1212 ++It;
1213 MF->insert(It, loop1MBB);
1214 MF->insert(It, loop2MBB);
1215 MF->insert(It, exitMBB);
1216
1217 // Transfer the remainder of BB and its successor edges to exitMBB.
1218 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001219 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001220 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1221
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001222 // thisMBB:
1223 // ...
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001224 // fallthrough --> loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001225 BB->addSuccessor(loop1MBB);
Akira Hatanaka81b44112011-07-19 17:09:53 +00001226 loop1MBB->addSuccessor(exitMBB);
1227 loop1MBB->addSuccessor(loop2MBB);
1228 loop2MBB->addSuccessor(loop1MBB);
1229 loop2MBB->addSuccessor(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001230
1231 // loop1MBB:
1232 // ll dest, 0(ptr)
1233 // bne dest, oldval, exitMBB
1234 BB = loop1MBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001235 BuildMI(BB, DL, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1236 BuildMI(BB, DL, TII->get(BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001237 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001238
1239 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001240 // sc success, newval, 0(ptr)
1241 // beq success, $0, loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001242 BB = loop2MBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001243 BuildMI(BB, DL, TII->get(SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001244 .addReg(NewVal).addReg(Ptr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001245 BuildMI(BB, DL, TII->get(BEQ))
Akira Hatanaka59068062011-11-11 04:14:30 +00001246 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001247
1248 MI->eraseFromParent(); // The instruction is gone now.
1249
Akira Hatanaka939ece12011-07-19 03:42:13 +00001250 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001251}
1252
1253MachineBasicBlock *
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001254MipsTargetLowering::emitAtomicCmpSwapPartword(MachineInstr *MI,
Akira Hatanaka0f843822011-06-07 18:58:42 +00001255 MachineBasicBlock *BB,
1256 unsigned Size) const {
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001257 assert((Size == 1 || Size == 2) &&
1258 "Unsupported size for EmitAtomicCmpSwapPartial.");
1259
1260 MachineFunction *MF = BB->getParent();
1261 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1262 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1263 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001264 DebugLoc DL = MI->getDebugLoc();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001265
1266 unsigned Dest = MI->getOperand(0).getReg();
1267 unsigned Ptr = MI->getOperand(1).getReg();
Akira Hatanaka4061da12011-07-19 20:11:17 +00001268 unsigned CmpVal = MI->getOperand(2).getReg();
1269 unsigned NewVal = MI->getOperand(3).getReg();
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001270
Akira Hatanaka4061da12011-07-19 20:11:17 +00001271 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1272 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001273 unsigned Mask = RegInfo.createVirtualRegister(RC);
1274 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka4061da12011-07-19 20:11:17 +00001275 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1276 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1277 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1278 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1279 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1280 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1281 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1282 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1283 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1284 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1285 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1286 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1287 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1288 unsigned Success = RegInfo.createVirtualRegister(RC);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001289
1290 // insert new blocks after the current block
1291 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1292 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1293 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001294 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001295 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1296 MachineFunction::iterator It = BB;
1297 ++It;
1298 MF->insert(It, loop1MBB);
1299 MF->insert(It, loop2MBB);
Akira Hatanaka939ece12011-07-19 03:42:13 +00001300 MF->insert(It, sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001301 MF->insert(It, exitMBB);
1302
1303 // Transfer the remainder of BB and its successor edges to exitMBB.
1304 exitMBB->splice(exitMBB->begin(), BB,
Akira Hatanaka82099682011-12-19 19:52:25 +00001305 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001306 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1307
Akira Hatanaka81b44112011-07-19 17:09:53 +00001308 BB->addSuccessor(loop1MBB);
1309 loop1MBB->addSuccessor(sinkMBB);
1310 loop1MBB->addSuccessor(loop2MBB);
1311 loop2MBB->addSuccessor(loop1MBB);
1312 loop2MBB->addSuccessor(sinkMBB);
1313 sinkMBB->addSuccessor(exitMBB);
1314
Akira Hatanaka70564a92011-07-19 18:14:26 +00001315 // FIXME: computation of newval2 can be moved to loop2MBB.
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001316 // thisMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001317 // addiu masklsb2,$0,-4 # 0xfffffffc
1318 // and alignedaddr,ptr,masklsb2
1319 // andi ptrlsb2,ptr,3
1320 // sll shiftamt,ptrlsb2,3
1321 // ori maskupper,$0,255 # 0xff
1322 // sll mask,maskupper,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001323 // nor mask2,$0,mask
Akira Hatanaka4061da12011-07-19 20:11:17 +00001324 // andi maskedcmpval,cmpval,255
1325 // sll shiftedcmpval,maskedcmpval,shiftamt
1326 // andi maskednewval,newval,255
1327 // sll shiftednewval,maskednewval,shiftamt
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001328 int64_t MaskImm = (Size == 1) ? 255 : 65535;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001329 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001330 .addReg(Mips::ZERO).addImm(-4);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001331 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001332 .addReg(Ptr).addReg(MaskLSB2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001333 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
Akira Hatanakaaffed7e2013-05-31 03:25:44 +00001334 if (Subtarget->isLittle()) {
1335 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1336 } else {
1337 unsigned Off = RegInfo.createVirtualRegister(RC);
1338 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1339 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1340 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1341 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001342 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001343 .addReg(Mips::ZERO).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001344 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
Akira Hatanaka51122432013-07-01 20:39:53 +00001345 .addReg(MaskUpper).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001346 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1347 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001348 .addReg(CmpVal).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001349 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal)
Akira Hatanaka51122432013-07-01 20:39:53 +00001350 .addReg(MaskedCmpVal).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001351 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001352 .addReg(NewVal).addImm(MaskImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001353 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal)
Akira Hatanaka51122432013-07-01 20:39:53 +00001354 .addReg(MaskedNewVal).addReg(ShiftAmt);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001355
1356 // loop1MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001357 // ll oldval,0(alginedaddr)
1358 // and maskedoldval0,oldval,mask
1359 // bne maskedoldval0,shiftedcmpval,sinkMBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001360 BB = loop1MBB;
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001361 BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001362 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001363 .addReg(OldVal).addReg(Mask);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001364 BuildMI(BB, DL, TII->get(Mips::BNE))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001365 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001366
1367 // loop2MBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001368 // and maskedoldval1,oldval,mask2
1369 // or storeval,maskedoldval1,shiftednewval
1370 // sc success,storeval,0(alignedaddr)
1371 // beq success,$0,loop1MBB
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001372 BB = loop2MBB;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001373 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001374 .addReg(OldVal).addReg(Mask2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001375 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001376 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
Akira Hatanakaa98a4862013-08-20 21:08:22 +00001377 BuildMI(BB, DL, TII->get(Mips::SC), Success)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001378 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001379 BuildMI(BB, DL, TII->get(Mips::BEQ))
Akira Hatanaka4061da12011-07-19 20:11:17 +00001380 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001381
Akira Hatanaka939ece12011-07-19 03:42:13 +00001382 // sinkMBB:
Akira Hatanaka4061da12011-07-19 20:11:17 +00001383 // srl srlres,maskedoldval0,shiftamt
1384 // sll sllres,srlres,24
1385 // sra dest,sllres,24
Akira Hatanaka939ece12011-07-19 03:42:13 +00001386 BB = sinkMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001387 int64_t ShiftImm = (Size == 1) ? 24 : 16;
Akira Hatanakaa308c672011-07-19 03:14:58 +00001388
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001389 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
Akira Hatanaka51122432013-07-01 20:39:53 +00001390 .addReg(MaskedOldVal0).addReg(ShiftAmt);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001391 BuildMI(BB, DL, TII->get(Mips::SLL), SllRes)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001392 .addReg(SrlRes).addImm(ShiftImm);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001393 BuildMI(BB, DL, TII->get(Mips::SRA), Dest)
Akira Hatanaka4061da12011-07-19 20:11:17 +00001394 .addReg(SllRes).addImm(ShiftImm);
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001395
1396 MI->eraseFromParent(); // The instruction is gone now.
1397
Akira Hatanaka939ece12011-07-19 03:42:13 +00001398 return exitMBB;
Bruno Cardoso Lopes4e694c92011-05-31 02:54:07 +00001399}
1400
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001401//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001402// Misc Lower Operation implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001403//===----------------------------------------------------------------------===//
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001404SDValue MipsTargetLowering::lowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanakab7656a92013-03-06 21:32:03 +00001405 SDValue Chain = Op.getOperand(0);
1406 SDValue Table = Op.getOperand(1);
1407 SDValue Index = Op.getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001408 SDLoc DL(Op);
Akira Hatanakab7656a92013-03-06 21:32:03 +00001409 EVT PTy = getPointerTy();
1410 unsigned EntrySize =
1411 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(*getDataLayout());
1412
1413 Index = DAG.getNode(ISD::MUL, DL, PTy, Index,
1414 DAG.getConstant(EntrySize, PTy));
1415 SDValue Addr = DAG.getNode(ISD::ADD, DL, PTy, Index, Table);
1416
1417 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
1418 Addr = DAG.getExtLoad(ISD::SEXTLOAD, DL, PTy, Chain, Addr,
1419 MachinePointerInfo::getJumpTable(), MemVT, false, false,
1420 0);
1421 Chain = Addr.getValue(1);
1422
1423 if ((getTargetMachine().getRelocationModel() == Reloc::PIC_) || IsN64) {
1424 // For PIC, the sequence is:
1425 // BRIND(load(Jumptable + index) + RelocBase)
1426 // RelocBase can be JumpTable, GOT or some sort of global base.
1427 Addr = DAG.getNode(ISD::ADD, DL, PTy, Addr,
1428 getPICJumpTableRelocBase(Table, DAG));
1429 }
1430
1431 return DAG.getNode(ISD::BRIND, DL, MVT::Other, Chain, Addr);
1432}
1433
Bruno Cardoso Lopesd3bdf192009-05-27 17:23:44 +00001434SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001435lowerBRCOND(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001436{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001437 // The first operand is the chain, the second is the condition, the third is
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001438 // the block to branch to if the condition is true.
1439 SDValue Chain = Op.getOperand(0);
1440 SDValue Dest = Op.getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001441 SDLoc DL(Op);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001442
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001443 SDValue CondRes = createFPCmp(DAG, Op.getOperand(1));
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001444
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001445 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001446 if (CondRes.getOpcode() != MipsISD::FPCmp)
Bruno Cardoso Lopes4b877ca2008-07-30 17:06:13 +00001447 return Op;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001448
Bruno Cardoso Lopes77283772008-07-31 18:31:28 +00001449 SDValue CCNode = CondRes.getOperand(2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001450 Mips::CondCode CC =
1451 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
Akira Hatanaka9cf07242013-03-30 01:16:38 +00001452 unsigned Opc = invertFPCondCodeUser(CC) ? Mips::BRANCH_F : Mips::BRANCH_T;
1453 SDValue BrCode = DAG.getConstant(Opc, MVT::i32);
Akira Hatanaka83d8ef12013-07-26 20:13:47 +00001454 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001455 return DAG.getNode(MipsISD::FPBrcond, DL, Op.getValueType(), Chain, BrCode,
Akira Hatanaka83d8ef12013-07-26 20:13:47 +00001456 FCC0, Dest, CondRes);
Bruno Cardoso Lopes85e31e32008-07-28 19:11:24 +00001457}
1458
1459SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001460lowerSELECT(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001461{
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001462 SDValue Cond = createFPCmp(DAG, Op.getOperand(0));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001463
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001464 // Return if flag is not set by a floating point comparison.
Akira Hatanaka1d6b38d2011-03-31 18:26:17 +00001465 if (Cond.getOpcode() != MipsISD::FPCmp)
1466 return Op;
Bruno Cardoso Lopes739e4412008-08-13 07:13:40 +00001467
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001468 return createCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001469 SDLoc(Op));
Bruno Cardoso Lopes6d399bd2008-07-29 19:05:28 +00001470}
1471
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001472SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001473lowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001474{
Andrew Trickac6d9be2013-05-25 02:42:55 +00001475 SDLoc DL(Op);
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001476 EVT Ty = Op.getOperand(0).getValueType();
Matt Arsenault225ed702013-05-18 00:21:46 +00001477 SDValue Cond = DAG.getNode(ISD::SETCC, DL,
1478 getSetCCResultType(*DAG.getContext(), Ty),
Akira Hatanaka3fef29d2012-07-11 19:32:27 +00001479 Op.getOperand(0), Op.getOperand(1),
1480 Op.getOperand(4));
1481
1482 return DAG.getNode(ISD::SELECT, DL, Op.getValueType(), Cond, Op.getOperand(2),
1483 Op.getOperand(3));
1484}
1485
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001486SDValue MipsTargetLowering::lowerSETCC(SDValue Op, SelectionDAG &DAG) const {
1487 SDValue Cond = createFPCmp(DAG, Op);
Akira Hatanaka0a40c232012-03-09 23:46:03 +00001488
1489 assert(Cond.getOpcode() == MipsISD::FPCmp &&
1490 "Floating point operand expected.");
1491
1492 SDValue True = DAG.getConstant(1, MVT::i32);
1493 SDValue False = DAG.getConstant(0, MVT::i32);
1494
Andrew Trickac6d9be2013-05-25 02:42:55 +00001495 return createCMovFP(DAG, Cond, True, False, SDLoc(Op));
Akira Hatanaka0a40c232012-03-09 23:46:03 +00001496}
1497
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001498SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001499 SelectionDAG &DAG) const {
Dale Johannesende064702009-02-06 21:50:26 +00001500 // FIXME there isn't actually debug info here
Andrew Trickac6d9be2013-05-25 02:42:55 +00001501 SDLoc DL(Op);
Akira Hatanaka79380342013-09-25 00:30:25 +00001502 EVT Ty = Op.getValueType();
1503 GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
1504 const GlobalValue *GV = N->getGlobal();
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001505
Akira Hatanakaa5903ac2011-10-11 00:55:05 +00001506 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
Akira Hatanakaafc945b2012-09-12 23:27:55 +00001507 const MipsTargetObjectFile &TLOF =
1508 (const MipsTargetObjectFile&)getObjFileLowering();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001509
Chris Lattnere3736f82009-08-13 05:41:27 +00001510 // %gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001511 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001512 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, 0,
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00001513 MipsII::MO_GPREL);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001514 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, DL,
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001515 DAG.getVTList(MVT::i32), &GA, 1);
Akira Hatanakae7338cd2012-08-22 03:18:13 +00001516 SDValue GPReg = DAG.getRegister(Mips::GP, MVT::i32);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001517 return DAG.getNode(ISD::ADD, DL, MVT::i32, GPReg, GPRelNode);
Chris Lattnere3736f82009-08-13 05:41:27 +00001518 }
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001519
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001520 // %hi/%lo relocation
Akira Hatanaka79380342013-09-25 00:30:25 +00001521 return getAddrNonPIC(N, Ty, DAG);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001522 }
1523
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001524 if (GV->hasInternalLinkage() || (GV->hasLocalLinkage() && !isa<Function>(GV)))
Akira Hatanaka79380342013-09-25 00:30:25 +00001525 return getAddrLocal(N, Ty, DAG, HasMips64);
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001526
Akira Hatanakaf09a0372012-11-21 20:40:38 +00001527 if (LargeGOT)
Akira Hatanaka79380342013-09-25 00:30:25 +00001528 return getAddrGlobalLargeGOT(N, Ty, DAG, MipsII::MO_GOT_HI16,
Akira Hatanakaf09a0372012-11-21 20:40:38 +00001529 MipsII::MO_GOT_LO16);
1530
Akira Hatanaka79380342013-09-25 00:30:25 +00001531 return getAddrGlobal(N, Ty, DAG,
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001532 HasMips64 ? MipsII::MO_GOT_DISP : MipsII::MO_GOT16);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001533}
1534
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001535SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op,
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001536 SelectionDAG &DAG) const {
Akira Hatanaka79380342013-09-25 00:30:25 +00001537 BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op);
1538 EVT Ty = Op.getValueType();
Akira Hatanakaf48eb532011-04-25 17:10:45 +00001539
Akira Hatanaka79380342013-09-25 00:30:25 +00001540 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
1541 return getAddrNonPIC(N, Ty, DAG);
1542
1543 return getAddrLocal(N, Ty, DAG, HasMips64);
Bruno Cardoso Lopesca8a2aa2011-03-04 20:01:52 +00001544}
1545
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001546SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001547lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001548{
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001549 // If the relocation model is PIC, use the General Dynamic TLS Model or
1550 // Local Dynamic TLS model, otherwise use the Initial Exec or
1551 // Local Exec TLS Model.
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001552
1553 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001554 SDLoc DL(GA);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001555 const GlobalValue *GV = GA->getGlobal();
1556 EVT PtrVT = getPointerTy();
1557
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001558 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
1559
1560 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Hans Wennborg70a07c72012-06-04 14:02:08 +00001561 // General Dynamic and Local Dynamic TLS Model.
1562 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
1563 : MipsII::MO_TLSGD;
1564
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001565 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag);
1566 SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT,
1567 getGlobalReg(DAG, PtrVT), TGA);
Akira Hatanaka7a7194b2011-12-08 21:05:38 +00001568 unsigned PtrSize = PtrVT.getSizeInBits();
1569 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1570
Benjamin Kramer5eccf672011-12-11 12:21:34 +00001571 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001572
1573 ArgListTy Args;
1574 ArgListEntry Entry;
1575 Entry.Node = Argument;
Akira Hatanakaca074792011-12-08 20:34:32 +00001576 Entry.Ty = PtrTy;
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001577 Args.push_back(Entry);
Jia Liubb481f82012-02-28 07:46:26 +00001578
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001579 TargetLowering::CallLoweringInfo CLI(DAG.getEntryNode(), PtrTy,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001580 false, false, false, false, 0, CallingConv::C,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001581 /*IsTailCall=*/false, /*doesNotRet=*/false,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001582 /*isReturnValueUsed=*/true,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001583 TlsGetAddr, Args, DAG, DL);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001584 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001585
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001586 SDValue Ret = CallResult.first;
1587
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001588 if (model != TLSModel::LocalDynamic)
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001589 return Ret;
1590
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001591 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001592 MipsII::MO_DTPREL_HI);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001593 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1594 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka3faac0a2011-12-14 18:26:41 +00001595 MipsII::MO_DTPREL_LO);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001596 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1597 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret);
1598 return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +00001599 }
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001600
1601 SDValue Offset;
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001602 if (model == TLSModel::InitialExec) {
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001603 // Initial Exec TLS Model
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001604 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001605 MipsII::MO_GOTTPREL);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001606 TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT),
Akira Hatanaka648f00c2012-02-24 22:34:47 +00001607 TGA);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001608 Offset = DAG.getLoad(PtrVT, DL,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001609 DAG.getEntryNode(), TGA, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001610 false, false, false, 0);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001611 } else {
1612 // Local Exec TLS Model
Hans Wennborgfd5abd52012-05-04 09:40:39 +00001613 assert(model == TLSModel::LocalExec);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001614 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001615 MipsII::MO_TPREL_HI);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001616 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001617 MipsII::MO_TPREL_LO);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001618 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1619 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1620 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
Akira Hatanaka5f7451f2011-06-21 01:02:03 +00001621 }
1622
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001623 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
1624 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset);
Bruno Cardoso Lopes97843cd2008-07-29 19:29:50 +00001625}
1626
1627SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001628lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001629{
Akira Hatanaka79380342013-09-25 00:30:25 +00001630 JumpTableSDNode *N = cast<JumpTableSDNode>(Op);
1631 EVT Ty = Op.getValueType();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001632
Akira Hatanaka79380342013-09-25 00:30:25 +00001633 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
1634 return getAddrNonPIC(N, Ty, DAG);
1635
1636 return getAddrLocal(N, Ty, DAG, HasMips64);
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +00001637}
1638
Dan Gohman475871a2008-07-27 21:46:04 +00001639SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001640lowerConstantPool(SDValue Op, SelectionDAG &DAG) const
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001641{
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001642 // gp_rel relocation
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001643 // FIXME: we should reference the constant pool using small data sections,
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001644 // but the asm printer currently doesn't support this feature without
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001645 // hacking it. This feature should come soon so we can uncomment the
Bruno Cardoso Lopesf33bc432008-07-28 19:26:25 +00001646 // stuff below.
Eli Friedmane2c74082009-08-03 02:22:28 +00001647 //if (IsInSmallSection(C->getType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001648 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1649 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001650 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
Akira Hatanaka79380342013-09-25 00:30:25 +00001651 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
1652 EVT Ty = Op.getValueType();
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +00001653
Akira Hatanakad43e06d2012-11-21 20:30:40 +00001654 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
Akira Hatanaka79380342013-09-25 00:30:25 +00001655 return getAddrNonPIC(N, Ty, DAG);
Bruno Cardoso Lopes92e87f22008-07-23 16:01:50 +00001656
Akira Hatanaka79380342013-09-25 00:30:25 +00001657 return getAddrLocal(N, Ty, DAG, HasMips64);
Bruno Cardoso Lopes97c25372008-07-09 04:15:08 +00001658}
1659
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001660SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001661 MachineFunction &MF = DAG.getMachineFunction();
1662 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1663
Andrew Trickac6d9be2013-05-25 02:42:55 +00001664 SDLoc DL(Op);
Dan Gohman1e93df62010-04-17 14:41:14 +00001665 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1666 getPointerTy());
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001667
1668 // vastart just stores the address of the VarArgsFrameIndex slot into the
1669 // memory location argument.
1670 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001671 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
Akira Hatanaka82099682011-12-19 19:52:25 +00001672 MachinePointerInfo(SV), false, false, 0);
Bruno Cardoso Lopes6059b852010-02-06 21:00:02 +00001673}
Jia Liubb481f82012-02-28 07:46:26 +00001674
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001675static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001676 EVT TyX = Op.getOperand(0).getValueType();
1677 EVT TyY = Op.getOperand(1).getValueType();
1678 SDValue Const1 = DAG.getConstant(1, MVT::i32);
1679 SDValue Const31 = DAG.getConstant(31, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001680 SDLoc DL(Op);
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001681 SDValue Res;
1682
1683 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1684 // to i32.
1685 SDValue X = (TyX == MVT::f32) ?
1686 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1687 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1688 Const1);
1689 SDValue Y = (TyY == MVT::f32) ?
1690 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
1691 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
1692 Const1);
1693
1694 if (HasR2) {
1695 // ext E, Y, 31, 1 ; extract bit31 of Y
1696 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
1697 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
1698 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
1699 } else {
1700 // sll SllX, X, 1
1701 // srl SrlX, SllX, 1
1702 // srl SrlY, Y, 31
1703 // sll SllY, SrlX, 31
1704 // or Or, SrlX, SllY
1705 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1706 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1707 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
1708 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
1709 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
1710 }
1711
1712 if (TyX == MVT::f32)
1713 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
1714
1715 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1716 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1717 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001718}
1719
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001720static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001721 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
1722 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
1723 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
1724 SDValue Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001725 SDLoc DL(Op);
Eric Christopher471e4222011-06-08 23:55:35 +00001726
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001727 // Bitcast to integer nodes.
1728 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
1729 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001730
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001731 if (HasR2) {
1732 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
1733 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
1734 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
1735 DAG.getConstant(WidthY - 1, MVT::i32), Const1);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001736
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001737 if (WidthX > WidthY)
1738 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
1739 else if (WidthY > WidthX)
1740 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001741
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001742 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
1743 DAG.getConstant(WidthX - 1, MVT::i32), Const1, X);
1744 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
1745 }
1746
1747 // (d)sll SllX, X, 1
1748 // (d)srl SrlX, SllX, 1
1749 // (d)srl SrlY, Y, width(Y)-1
1750 // (d)sll SllY, SrlX, width(Y)-1
1751 // or Or, SrlX, SllY
1752 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
1753 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
1754 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
1755 DAG.getConstant(WidthY - 1, MVT::i32));
1756
1757 if (WidthX > WidthY)
1758 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
1759 else if (WidthY > WidthX)
1760 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
1761
1762 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
1763 DAG.getConstant(WidthX - 1, MVT::i32));
1764 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
1765 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001766}
1767
Akira Hatanaka82099682011-12-19 19:52:25 +00001768SDValue
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001769MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka056c51e2012-04-11 22:13:04 +00001770 if (Subtarget->hasMips64())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001771 return lowerFCOPYSIGN64(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001772
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001773 return lowerFCOPYSIGN32(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanaka9c3d57c2011-05-25 19:32:07 +00001774}
1775
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001776static SDValue lowerFABS32(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001777 SDValue Res, Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001778 SDLoc DL(Op);
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001779
1780 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1781 // to i32.
1782 SDValue X = (Op.getValueType() == MVT::f32) ?
1783 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1784 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1785 Const1);
1786
1787 // Clear MSB.
1788 if (HasR2)
1789 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32,
1790 DAG.getRegister(Mips::ZERO, MVT::i32),
1791 DAG.getConstant(31, MVT::i32), Const1, X);
1792 else {
1793 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1794 Res = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1795 }
1796
1797 if (Op.getValueType() == MVT::f32)
1798 return DAG.getNode(ISD::BITCAST, DL, MVT::f32, Res);
1799
1800 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1801 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1802 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
1803}
1804
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001805static SDValue lowerFABS64(SDValue Op, SelectionDAG &DAG, bool HasR2) {
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001806 SDValue Res, Const1 = DAG.getConstant(1, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001807 SDLoc DL(Op);
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001808
1809 // Bitcast to integer node.
1810 SDValue X = DAG.getNode(ISD::BITCAST, DL, MVT::i64, Op.getOperand(0));
1811
1812 // Clear MSB.
1813 if (HasR2)
1814 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i64,
1815 DAG.getRegister(Mips::ZERO_64, MVT::i64),
1816 DAG.getConstant(63, MVT::i32), Const1, X);
1817 else {
1818 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i64, X, Const1);
1819 Res = DAG.getNode(ISD::SRL, DL, MVT::i64, SllX, Const1);
1820 }
1821
1822 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, Res);
1823}
1824
1825SDValue
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001826MipsTargetLowering::lowerFABS(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001827 if (Subtarget->hasMips64() && (Op.getValueType() == MVT::f64))
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001828 return lowerFABS64(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001829
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001830 return lowerFABS32(Op, DAG, Subtarget->hasMips32r2());
Akira Hatanakac12a6e62012-04-11 22:49:04 +00001831}
1832
Akira Hatanaka2e591472011-06-02 00:24:44 +00001833SDValue MipsTargetLowering::
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001834lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Bruno Cardoso Lopese0b5cfc2011-06-16 00:40:02 +00001835 // check the depth
1836 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
Akira Hatanaka0f843822011-06-07 18:58:42 +00001837 "Frame address can only be determined for current frame.");
Akira Hatanaka2e591472011-06-02 00:24:44 +00001838
1839 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1840 MFI->setFrameAddressIsTaken(true);
1841 EVT VT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001842 SDLoc DL(Op);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001843 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), DL,
Akira Hatanaka46ac4392011-11-11 04:11:56 +00001844 IsN64 ? Mips::FP_64 : Mips::FP, VT);
Akira Hatanaka2e591472011-06-02 00:24:44 +00001845 return FrameAddr;
1846}
1847
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001848SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op,
Akira Hatanakaba584fe2012-07-11 00:53:32 +00001849 SelectionDAG &DAG) const {
1850 // check the depth
1851 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
1852 "Return address can be determined only for current frame.");
1853
1854 MachineFunction &MF = DAG.getMachineFunction();
1855 MachineFrameInfo *MFI = MF.getFrameInfo();
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00001856 MVT VT = Op.getSimpleValueType();
Akira Hatanakaba584fe2012-07-11 00:53:32 +00001857 unsigned RA = IsN64 ? Mips::RA_64 : Mips::RA;
1858 MFI->setReturnAddressIsTaken(true);
1859
1860 // Return RA, which contains the return address. Mark it an implicit live-in.
1861 unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00001862 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT);
Akira Hatanakaba584fe2012-07-11 00:53:32 +00001863}
1864
Akira Hatanaka544cc212013-01-30 00:26:49 +00001865// An EH_RETURN is the result of lowering llvm.eh.return which in turn is
1866// generated from __builtin_eh_return (offset, handler)
1867// The effect of this is to adjust the stack pointer by "offset"
1868// and then branch to "handler".
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001869SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Akira Hatanaka544cc212013-01-30 00:26:49 +00001870 const {
1871 MachineFunction &MF = DAG.getMachineFunction();
1872 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1873
1874 MipsFI->setCallsEhReturn();
1875 SDValue Chain = Op.getOperand(0);
1876 SDValue Offset = Op.getOperand(1);
1877 SDValue Handler = Op.getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001878 SDLoc DL(Op);
Akira Hatanaka544cc212013-01-30 00:26:49 +00001879 EVT Ty = IsN64 ? MVT::i64 : MVT::i32;
1880
1881 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
1882 // EH_RETURN nodes, so that instructions are emitted back-to-back.
1883 unsigned OffsetReg = IsN64 ? Mips::V1_64 : Mips::V1;
1884 unsigned AddrReg = IsN64 ? Mips::V0_64 : Mips::V0;
1885 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
1886 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
1887 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
1888 DAG.getRegister(OffsetReg, Ty),
1889 DAG.getRegister(AddrReg, getPointerTy()),
1890 Chain.getValue(1));
1891}
1892
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001893SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
Akira Hatanaka864f6602012-06-14 21:10:56 +00001894 SelectionDAG &DAG) const {
Eli Friedman14648462011-07-27 22:21:52 +00001895 // FIXME: Need pseudo-fence for 'singlethread' fences
1896 // FIXME: Set SType for weaker fences where supported/appropriate.
1897 unsigned SType = 0;
Andrew Trickac6d9be2013-05-25 02:42:55 +00001898 SDLoc DL(Op);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001899 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
Eli Friedman14648462011-07-27 22:21:52 +00001900 DAG.getConstant(SType, MVT::i32));
1901}
1902
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001903SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
Akira Hatanaka864f6602012-06-14 21:10:56 +00001904 SelectionDAG &DAG) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001905 SDLoc DL(Op);
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001906 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
1907 SDValue Shamt = Op.getOperand(2);
1908
1909 // if shamt < 32:
1910 // lo = (shl lo, shamt)
1911 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
1912 // else:
1913 // lo = 0
1914 // hi = (shl lo, shamt[4:0])
1915 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
1916 DAG.getConstant(-1, MVT::i32));
1917 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo,
1918 DAG.getConstant(1, MVT::i32));
1919 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, ShiftRight1Lo,
1920 Not);
1921 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi, Shamt);
1922 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
1923 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, MVT::i32, Lo, Shamt);
1924 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
1925 DAG.getConstant(0x20, MVT::i32));
Akira Hatanaka864f6602012-06-14 21:10:56 +00001926 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
1927 DAG.getConstant(0, MVT::i32), ShiftLeftLo);
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001928 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftLeftLo, Or);
1929
1930 SDValue Ops[2] = {Lo, Hi};
1931 return DAG.getMergeValues(Ops, 2, DL);
1932}
1933
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001934SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001935 bool IsSRA) const {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001936 SDLoc DL(Op);
Akira Hatanakaa284acb2012-05-09 00:55:21 +00001937 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
1938 SDValue Shamt = Op.getOperand(2);
1939
1940 // if shamt < 32:
1941 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
1942 // if isSRA:
1943 // hi = (sra hi, shamt)
1944 // else:
1945 // hi = (srl hi, shamt)
1946 // else:
1947 // if isSRA:
1948 // lo = (sra hi, shamt[4:0])
1949 // hi = (sra hi, 31)
1950 // else:
1951 // lo = (srl hi, shamt[4:0])
1952 // hi = 0
1953 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
1954 DAG.getConstant(-1, MVT::i32));
1955 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi,
1956 DAG.getConstant(1, MVT::i32));
1957 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, ShiftLeft1Hi, Not);
1958 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo, Shamt);
1959 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
1960 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL, DL, MVT::i32,
1961 Hi, Shamt);
1962 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
1963 DAG.getConstant(0x20, MVT::i32));
1964 SDValue Shift31 = DAG.getNode(ISD::SRA, DL, MVT::i32, Hi,
1965 DAG.getConstant(31, MVT::i32));
1966 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftRightHi, Or);
1967 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
1968 IsSRA ? Shift31 : DAG.getConstant(0, MVT::i32),
1969 ShiftRightHi);
1970
1971 SDValue Ops[2] = {Lo, Hi};
1972 return DAG.getMergeValues(Ops, 2, DL);
1973}
1974
Akira Hatanakafee62c12013-04-11 19:07:14 +00001975static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001976 SDValue Chain, SDValue Src, unsigned Offset) {
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00001977 SDValue Ptr = LD->getBasePtr();
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001978 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00001979 EVT BasePtrVT = Ptr.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001980 SDLoc DL(LD);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001981 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
1982
1983 if (Offset)
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00001984 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001985 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001986
1987 SDValue Ops[] = { Chain, Ptr, Src };
1988 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT,
1989 LD->getMemOperand());
1990}
1991
1992// Expand an unaligned 32 or 64-bit integer load node.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00001993SDValue MipsTargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00001994 LoadSDNode *LD = cast<LoadSDNode>(Op);
1995 EVT MemVT = LD->getMemoryVT();
1996
1997 // Return if load is aligned or if MemVT is neither i32 nor i64.
1998 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
1999 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
2000 return SDValue();
2001
2002 bool IsLittle = Subtarget->isLittle();
2003 EVT VT = Op.getValueType();
2004 ISD::LoadExtType ExtType = LD->getExtensionType();
2005 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
2006
2007 assert((VT == MVT::i32) || (VT == MVT::i64));
2008
2009 // Expand
2010 // (set dst, (i64 (load baseptr)))
2011 // to
2012 // (set tmp, (ldl (add baseptr, 7), undef))
2013 // (set dst, (ldr baseptr, tmp))
2014 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
Akira Hatanakafee62c12013-04-11 19:07:14 +00002015 SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002016 IsLittle ? 7 : 0);
Akira Hatanakafee62c12013-04-11 19:07:14 +00002017 return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002018 IsLittle ? 0 : 7);
2019 }
2020
Akira Hatanakafee62c12013-04-11 19:07:14 +00002021 SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002022 IsLittle ? 3 : 0);
Akira Hatanakafee62c12013-04-11 19:07:14 +00002023 SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002024 IsLittle ? 0 : 3);
2025
2026 // Expand
2027 // (set dst, (i32 (load baseptr))) or
2028 // (set dst, (i64 (sextload baseptr))) or
2029 // (set dst, (i64 (extload baseptr)))
2030 // to
2031 // (set tmp, (lwl (add baseptr, 3), undef))
2032 // (set dst, (lwr baseptr, tmp))
2033 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
2034 (ExtType == ISD::EXTLOAD))
2035 return LWR;
2036
2037 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
2038
2039 // Expand
2040 // (set dst, (i64 (zextload baseptr)))
2041 // to
2042 // (set tmp0, (lwl (add baseptr, 3), undef))
2043 // (set tmp1, (lwr baseptr, tmp0))
2044 // (set tmp2, (shl tmp1, 32))
2045 // (set dst, (srl tmp2, 32))
Andrew Trickac6d9be2013-05-25 02:42:55 +00002046 SDLoc DL(LD);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002047 SDValue Const32 = DAG.getConstant(32, MVT::i32);
2048 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
Akira Hatanaka94ccee22012-06-04 17:46:29 +00002049 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
2050 SDValue Ops[] = { SRL, LWR.getValue(1) };
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002051 return DAG.getMergeValues(Ops, 2, DL);
2052}
2053
Akira Hatanakafee62c12013-04-11 19:07:14 +00002054static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002055 SDValue Chain, unsigned Offset) {
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00002056 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
2057 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00002058 SDLoc DL(SD);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002059 SDVTList VTList = DAG.getVTList(MVT::Other);
2060
2061 if (Offset)
Akira Hatanaka2bd7e532012-06-13 19:06:08 +00002062 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002063 DAG.getConstant(Offset, BasePtrVT));
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002064
2065 SDValue Ops[] = { Chain, Value, Ptr };
2066 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT,
2067 SD->getMemOperand());
2068}
2069
2070// Expand an unaligned 32 or 64-bit integer store node.
Akira Hatanaka63451432013-05-16 20:45:17 +00002071static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG,
2072 bool IsLittle) {
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002073 SDValue Value = SD->getValue(), Chain = SD->getChain();
2074 EVT VT = Value.getValueType();
2075
2076 // Expand
2077 // (store val, baseptr) or
2078 // (truncstore val, baseptr)
2079 // to
2080 // (swl val, (add baseptr, 3))
2081 // (swr val, baseptr)
2082 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
Akira Hatanakafee62c12013-04-11 19:07:14 +00002083 SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain,
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002084 IsLittle ? 3 : 0);
Akira Hatanakafee62c12013-04-11 19:07:14 +00002085 return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002086 }
2087
2088 assert(VT == MVT::i64);
2089
2090 // Expand
2091 // (store val, baseptr)
2092 // to
2093 // (sdl val, (add baseptr, 7))
2094 // (sdr val, baseptr)
Akira Hatanakafee62c12013-04-11 19:07:14 +00002095 SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2096 return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
Akira Hatanaka1cd0ec02012-06-02 00:03:49 +00002097}
2098
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002099// Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr).
2100static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG) {
2101 SDValue Val = SD->getValue();
2102
2103 if (Val.getOpcode() != ISD::FP_TO_SINT)
2104 return SDValue();
2105
2106 EVT FPTy = EVT::getFloatingPointVT(Val.getValueSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002107 SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy,
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002108 Val.getOperand(0));
2109
Andrew Trickac6d9be2013-05-25 02:42:55 +00002110 return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(),
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002111 SD->getPointerInfo(), SD->isVolatile(),
2112 SD->isNonTemporal(), SD->getAlignment());
2113}
2114
Akira Hatanaka63451432013-05-16 20:45:17 +00002115SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2116 StoreSDNode *SD = cast<StoreSDNode>(Op);
2117 EVT MemVT = SD->getMemoryVT();
2118
2119 // Lower unaligned integer stores.
2120 if ((SD->getAlignment() < MemVT.getSizeInBits() / 8) &&
2121 ((MemVT == MVT::i32) || (MemVT == MVT::i64)))
2122 return lowerUnalignedIntStore(SD, DAG, Subtarget->isLittle());
2123
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002124 return lowerFP_TO_SINT_STORE(SD, DAG);
Akira Hatanaka63451432013-05-16 20:45:17 +00002125}
2126
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002127SDValue MipsTargetLowering::lowerADD(SDValue Op, SelectionDAG &DAG) const {
Akira Hatanakae90a3bc2012-11-07 19:10:58 +00002128 if (Op->getOperand(0).getOpcode() != ISD::FRAMEADDR
2129 || cast<ConstantSDNode>
2130 (Op->getOperand(0).getOperand(0))->getZExtValue() != 0
2131 || Op->getOperand(1).getOpcode() != ISD::FRAME_TO_ARGS_OFFSET)
2132 return SDValue();
2133
2134 // The pattern
2135 // (add (frameaddr 0), (frame_to_args_offset))
2136 // results from lowering llvm.eh.dwarf.cfa intrinsic. Transform it to
2137 // (add FrameObject, 0)
2138 // where FrameObject is a fixed StackObject with offset 0 which points to
2139 // the old stack pointer.
2140 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2141 EVT ValTy = Op->getValueType(0);
2142 int FI = MFI->CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
2143 SDValue InArgsAddr = DAG.getFrameIndex(FI, ValTy);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002144 return DAG.getNode(ISD::ADD, SDLoc(Op), ValTy, InArgsAddr,
Akira Hatanakae90a3bc2012-11-07 19:10:58 +00002145 DAG.getConstant(0, ValTy));
2146}
2147
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002148SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op,
2149 SelectionDAG &DAG) const {
2150 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002151 SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy,
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002152 Op.getOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00002153 return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc);
Akira Hatanakaae7e7cb2013-05-16 21:17:15 +00002154}
2155
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002156//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002157// Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002158//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002159
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002160//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002161// TODO: Implement a generic logic using tblgen that can support this.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002162// Mips O32 ABI rules:
2163// ---
2164// i32 - Passed in A0, A1, A2, A3 and stack
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002165// f32 - Only passed in f32 registers if no int reg has been used yet to hold
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002166// an argument. Otherwise, passed in A1, A2, A3 and stack.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002167// f64 - Only passed in two aliased f32 registers if no int reg has been used
2168// yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002169// not used, it must be shadowed. If only A3 is avaiable, shadow it and
2170// go to stack.
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002171//
2172// For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002173//===----------------------------------------------------------------------===//
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002174
Duncan Sands1e96bab2010-11-04 10:49:57 +00002175static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +00002176 MVT LocVT, CCValAssign::LocInfo LocInfo,
Akira Hatanakaad341d42013-08-20 23:38:40 +00002177 ISD::ArgFlagsTy ArgFlags, CCState &State,
2178 const uint16_t *F64Regs) {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002179
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002180 static const unsigned IntRegsSize=4, FloatRegsSize=2;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002181
Craig Topperc5eaae42012-03-11 07:57:25 +00002182 static const uint16_t IntRegs[] = {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002183 Mips::A0, Mips::A1, Mips::A2, Mips::A3
2184 };
Craig Topperc5eaae42012-03-11 07:57:25 +00002185 static const uint16_t F32Regs[] = {
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002186 Mips::F12, Mips::F14
2187 };
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002188
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002189 // Do not process byval args here.
2190 if (ArgFlags.isByVal())
2191 return true;
Akira Hatanaka4231c7e2011-05-24 19:18:33 +00002192
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002193 // Promote i8 and i16
2194 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
2195 LocVT = MVT::i32;
2196 if (ArgFlags.isSExt())
2197 LocInfo = CCValAssign::SExt;
2198 else if (ArgFlags.isZExt())
2199 LocInfo = CCValAssign::ZExt;
2200 else
2201 LocInfo = CCValAssign::AExt;
2202 }
2203
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002204 unsigned Reg;
2205
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002206 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
2207 // is true: function is vararg, argument is 3rd or higher, there is previous
2208 // argument which is not f32 or f64.
2209 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
2210 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00002211 unsigned OrigAlign = ArgFlags.getOrigAlign();
2212 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002213
2214 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002215 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Akira Hatanakaa1a7ba82011-05-19 20:29:48 +00002216 // If this is the first part of an i64 arg,
2217 // the allocated register must be either A0 or A2.
2218 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
2219 Reg = State.AllocateReg(IntRegs, IntRegsSize);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002220 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002221 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
2222 // Allocate int register and shadow next int register. If first
2223 // available register is Mips::A1 or Mips::A3, shadow it too.
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002224 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2225 if (Reg == Mips::A1 || Reg == Mips::A3)
2226 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2227 State.AllocateReg(IntRegs, IntRegsSize);
2228 LocVT = MVT::i32;
Akira Hatanaka95b8ae12011-05-19 18:06:05 +00002229 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
2230 // we are guaranteed to find an available float register
2231 if (ValVT == MVT::f32) {
2232 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
2233 // Shadow int register
2234 State.AllocateReg(IntRegs, IntRegsSize);
2235 } else {
2236 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
2237 // Shadow int registers
2238 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
2239 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
2240 State.AllocateReg(IntRegs, IntRegsSize);
2241 State.AllocateReg(IntRegs, IntRegsSize);
2242 }
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002243 } else
2244 llvm_unreachable("Cannot handle this ValVT.");
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002245
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002246 if (!Reg) {
2247 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3,
2248 OrigAlign);
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002249 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002250 } else
Bruno Cardoso Lopesc42fb5f2011-03-04 20:27:44 +00002251 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002252
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002253 return false;
Akira Hatanaka2c5d6522011-11-12 02:20:46 +00002254}
2255
Akira Hatanakaad341d42013-08-20 23:38:40 +00002256static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT,
2257 MVT LocVT, CCValAssign::LocInfo LocInfo,
2258 ISD::ArgFlagsTy ArgFlags, CCState &State) {
2259 static const uint16_t F64Regs[] = { Mips::D6, Mips::D7 };
2260
2261 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2262}
2263
2264static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT,
2265 MVT LocVT, CCValAssign::LocInfo LocInfo,
2266 ISD::ArgFlagsTy ArgFlags, CCState &State) {
2267 static const uint16_t F64Regs[] = { Mips::D12_64, Mips::D12_64 };
2268
2269 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2270}
2271
Akira Hatanaka2c5d6522011-11-12 02:20:46 +00002272#include "MipsGenCallingConv.inc"
2273
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002274//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002275// Call Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002276//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002277
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002278// Return next O32 integer argument register.
2279static unsigned getNextIntArgReg(unsigned Reg) {
2280 assert((Reg == Mips::A0) || (Reg == Mips::A2));
2281 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
2282}
2283
Akira Hatanaka7d712092012-10-30 19:23:25 +00002284SDValue
2285MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002286 SDValue Chain, SDValue Arg, SDLoc DL,
Akira Hatanaka7d712092012-10-30 19:23:25 +00002287 bool IsTailCall, SelectionDAG &DAG) const {
2288 if (!IsTailCall) {
2289 SDValue PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr,
2290 DAG.getIntPtrConstant(Offset));
2291 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo(), false,
2292 false, 0);
2293 }
2294
2295 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2296 int FI = MFI->CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
2297 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2298 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(),
2299 /*isVolatile=*/ true, false, 0);
2300}
2301
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002302void MipsTargetLowering::
2303getOpndList(SmallVectorImpl<SDValue> &Ops,
2304 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
2305 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
2306 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
2307 // Insert node "GP copy globalreg" before call to function.
2308 //
2309 // R_MIPS_CALL* operators (emitted when non-internal functions are called
2310 // in PIC mode) allow symbols to be resolved via lazy binding.
2311 // The lazy binding stub requires GP to point to the GOT.
2312 if (IsPICCall && !InternalLinkage) {
2313 unsigned GPReg = IsN64 ? Mips::GP_64 : Mips::GP;
2314 EVT Ty = IsN64 ? MVT::i64 : MVT::i32;
2315 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty)));
2316 }
Reed Kotler8453b3f2013-01-24 04:24:02 +00002317
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002318 // Build a sequence of copy-to-reg nodes chained together with token
2319 // chain and flag operands which copy the outgoing args into registers.
2320 // The InFlag in necessary since all emitted instructions must be
2321 // stuck together.
2322 SDValue InFlag;
Reed Kotler8453b3f2013-01-24 04:24:02 +00002323
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002324 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2325 Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, RegsToPass[i].first,
2326 RegsToPass[i].second, InFlag);
2327 InFlag = Chain.getValue(1);
2328 }
Reed Kotler8453b3f2013-01-24 04:24:02 +00002329
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002330 // Add argument registers to the end of the list so that they are
2331 // known live into the call.
2332 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2333 Ops.push_back(CLI.DAG.getRegister(RegsToPass[i].first,
2334 RegsToPass[i].second.getValueType()));
Reed Kotler8453b3f2013-01-24 04:24:02 +00002335
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002336 // Add a register mask operand representing the call-preserved registers.
2337 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2338 const uint32_t *Mask = TRI->getCallPreservedMask(CLI.CallConv);
2339 assert(Mask && "Missing call preserved mask for calling convention");
Reed Kotler46090912013-05-10 22:25:39 +00002340 if (Subtarget->inMips16HardFloat()) {
2341 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
2342 llvm::StringRef Sym = G->getGlobal()->getName();
2343 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
2344 if (F->hasFnAttribute("__Mips16RetHelper")) {
2345 Mask = MipsRegisterInfo::getMips16RetHelperMask();
2346 }
2347 }
2348 }
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002349 Ops.push_back(CLI.DAG.getRegisterMask(Mask));
2350
2351 if (InFlag.getNode())
2352 Ops.push_back(InFlag);
Reed Kotler8453b3f2013-01-24 04:24:02 +00002353}
2354
Dan Gohman98ca4f22009-08-05 01:29:28 +00002355/// LowerCall - functions arguments are copied from virtual regs to
Nate Begeman5bf4b752009-01-26 03:15:54 +00002356/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002357SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002358MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00002359 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002360 SelectionDAG &DAG = CLI.DAG;
Andrew Trickac6d9be2013-05-25 02:42:55 +00002361 SDLoc DL = CLI.DL;
Craig Toppera0ec3f92013-07-14 04:42:23 +00002362 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
2363 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
2364 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Akira Hatanakae2d529a2012-07-31 18:46:41 +00002365 SDValue Chain = CLI.Chain;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002366 SDValue Callee = CLI.Callee;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002367 bool &IsTailCall = CLI.IsTailCall;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002368 CallingConv::ID CallConv = CLI.CallConv;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002369 bool IsVarArg = CLI.IsVarArg;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00002370
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002371 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002372 MachineFrameInfo *MFI = MF.getFrameInfo();
Akira Hatanakad37776d2011-05-20 21:39:54 +00002373 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
Bruno Cardoso Lopesc517cb02009-09-01 17:27:58 +00002374 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002375
2376 // Analyze operands of the call, assigning locations to each operand.
2377 SmallVector<CCValAssign, 16> ArgLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002378 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka82099682011-12-19 19:52:25 +00002379 getTargetMachine(), ArgLocs, *DAG.getContext());
Reed Kotler46090912013-05-10 22:25:39 +00002380 MipsCC::SpecialCallingConvType SpecialCallingConv =
2381 getSpecialCallingConv(Callee);
Akira Hatanakaad341d42013-08-20 23:38:40 +00002382 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo,
2383 SpecialCallingConv);
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002384
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002385 MipsCCInfo.analyzeCallOperands(Outs, IsVarArg,
Reed Kotlerc673f9c2013-08-30 19:40:56 +00002386 Subtarget->mipsSEUsesSoftFloat(),
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00002387 Callee.getNode(), CLI.Args);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002388
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002389 // Get a count of how many bytes are to be pushed on the stack.
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002390 unsigned NextStackOffset = CCInfo.getNextStackOffset();
Akira Hatanaka480eeb52012-07-26 23:27:01 +00002391
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002392 // Check if it's really possible to do a tail call.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002393 if (IsTailCall)
2394 IsTailCall =
2395 isEligibleForTailCallOptimization(MipsCCInfo, NextStackOffset,
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002396 *MF.getInfo<MipsFunctionInfo>());
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002397
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002398 if (IsTailCall)
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002399 ++NumTailCalls;
2400
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002401 // Chain is the output chain of the last Load/Store or CopyToReg node.
2402 // ByValChain is the output chain of the last Memcpy node created for copying
2403 // byval arguments to the stack.
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002404 unsigned StackAlignment = TFL->getStackAlignment();
2405 NextStackOffset = RoundUpToAlignment(NextStackOffset, StackAlignment);
Akira Hatanakada7f5f12011-09-19 20:26:02 +00002406 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002407
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002408 if (!IsTailCall)
Andrew Trick6e0b2a02013-05-29 22:03:55 +00002409 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffsetVal, DL);
Akira Hatanakae2d529a2012-07-31 18:46:41 +00002410
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002411 SDValue StackPtr = DAG.getCopyFromReg(Chain, DL,
Akira Hatanakae2d529a2012-07-31 18:46:41 +00002412 IsN64 ? Mips::SP_64 : Mips::SP,
2413 getPointerTy());
Akira Hatanaka3d21c242011-06-08 17:39:33 +00002414
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002415 // With EABI is it possible to have 16 args on registers.
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00002416 std::deque< std::pair<unsigned, SDValue> > RegsToPass;
Dan Gohman475871a2008-07-27 21:46:04 +00002417 SmallVector<SDValue, 8> MemOpChains;
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002418 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002419
2420 // Walk the register/memloc assignments, inserting copies/loads.
2421 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00002422 SDValue Arg = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002423 CCValAssign &VA = ArgLocs[i];
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002424 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002425 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2426
2427 // ByVal Arg.
2428 if (Flags.isByVal()) {
2429 assert(Flags.getByValSize() &&
2430 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002431 assert(ByValArg != MipsCCInfo.byval_end());
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002432 assert(!IsTailCall &&
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002433 "Do not tail-call optimize if there is a byval argument.");
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002434 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002435 MipsCCInfo, *ByValArg, Flags, Subtarget->isLittle());
2436 ++ByValArg;
Akira Hatanaka6df3e7b2011-11-12 02:34:50 +00002437 continue;
2438 }
Jia Liubb481f82012-02-28 07:46:26 +00002439
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002440 // Promote the value if needed.
2441 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002442 default: llvm_unreachable("Unknown loc info!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002443 case CCValAssign::Full:
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002444 if (VA.isRegLoc()) {
2445 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00002446 (ValVT == MVT::f64 && LocVT == MVT::i64) ||
2447 (ValVT == MVT::i64 && LocVT == MVT::f64))
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002448 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002449 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002450 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002451 Arg, DAG.getConstant(0, MVT::i32));
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002452 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002453 Arg, DAG.getConstant(1, MVT::i32));
Akira Hatanaka99a2e982011-04-15 19:52:08 +00002454 if (!Subtarget->isLittle())
2455 std::swap(Lo, Hi);
Jia Liubb481f82012-02-28 07:46:26 +00002456 unsigned LocRegLo = VA.getLocReg();
Akira Hatanaka373e3a42011-09-23 00:58:33 +00002457 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2458 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2459 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002460 continue;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002461 }
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002462 }
2463 break;
Chris Lattnere0b12152008-03-17 06:57:02 +00002464 case CCValAssign::SExt:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002465 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002466 break;
2467 case CCValAssign::ZExt:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002468 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002469 break;
2470 case CCValAssign::AExt:
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002471 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg);
Chris Lattnere0b12152008-03-17 06:57:02 +00002472 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002473 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002474
2475 // Arguments that can be passed on register must be kept at
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002476 // RegsToPass vector
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002477 if (VA.isRegLoc()) {
2478 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Chris Lattnere0b12152008-03-17 06:57:02 +00002479 continue;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002480 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002481
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002482 // Register can't get to this point...
Chris Lattnere0b12152008-03-17 06:57:02 +00002483 assert(VA.isMemLoc());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002484
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002485 // emit ISD::STORE whichs stores the
Chris Lattnere0b12152008-03-17 06:57:02 +00002486 // parameter value to a stack Location
Akira Hatanaka2f34d752012-10-30 20:16:31 +00002487 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002488 Chain, Arg, DL, IsTailCall, DAG));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002489 }
2490
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002491 // Transform all store nodes into one single node because all store
2492 // nodes are independent of each other.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002493 if (!MemOpChains.empty())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002494 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002495 &MemOpChains[0], MemOpChains.size());
2496
Bill Wendling056292f2008-09-16 21:48:12 +00002497 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002498 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2499 // node so that legalize doesn't hack it.
Akira Hatanakae42f33b2011-10-28 19:49:00 +00002500 bool IsPICCall = (IsN64 || IsPIC); // true if calls are translated to jalr $25
Akira Hatanakaed185da2012-12-13 03:17:29 +00002501 bool GlobalOrExternal = false, InternalLinkage = false;
Akira Hatanaka9777e7a2011-04-07 19:51:44 +00002502 SDValue CalleeLo;
Akira Hatanaka79380342013-09-25 00:30:25 +00002503 EVT Ty = Callee.getValueType();
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002504
2505 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002506 if (IsPICCall) {
Akira Hatanakaed185da2012-12-13 03:17:29 +00002507 InternalLinkage = G->getGlobal()->hasInternalLinkage();
2508
2509 if (InternalLinkage)
Akira Hatanaka79380342013-09-25 00:30:25 +00002510 Callee = getAddrLocal(G, Ty, DAG, HasMips64);
Akira Hatanakaf09a0372012-11-21 20:40:38 +00002511 else if (LargeGOT)
Akira Hatanaka79380342013-09-25 00:30:25 +00002512 Callee = getAddrGlobalLargeGOT(G, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaf09a0372012-11-21 20:40:38 +00002513 MipsII::MO_CALL_LO16);
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002514 else
Akira Hatanaka79380342013-09-25 00:30:25 +00002515 Callee = getAddrGlobal(G, Ty, DAG, MipsII::MO_GOT_CALL);
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002516 } else
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002517 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, getPointerTy(), 0,
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002518 MipsII::MO_NO_FLAG);
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002519 GlobalOrExternal = true;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002520 }
2521 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Akira Hatanakaf09a0372012-11-21 20:40:38 +00002522 if (!IsN64 && !IsPIC) // !N64 && static
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002523 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2524 MipsII::MO_NO_FLAG);
Akira Hatanakaf09a0372012-11-21 20:40:38 +00002525 else if (LargeGOT)
Akira Hatanaka79380342013-09-25 00:30:25 +00002526 Callee = getAddrGlobalLargeGOT(S, Ty, DAG, MipsII::MO_CALL_HI16,
Akira Hatanakaf09a0372012-11-21 20:40:38 +00002527 MipsII::MO_CALL_LO16);
Akira Hatanaka60689322013-02-22 21:10:03 +00002528 else // N64 || PIC
Akira Hatanaka79380342013-09-25 00:30:25 +00002529 Callee = getAddrGlobal(S, Ty, DAG, MipsII::MO_GOT_CALL);
Akira Hatanakad43e06d2012-11-21 20:30:40 +00002530
Akira Hatanaka0dca9452011-12-09 01:45:12 +00002531 GlobalOrExternal = true;
Akira Hatanakaf49fde22011-04-04 17:11:07 +00002532 }
2533
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00002534 SmallVector<SDValue, 8> Ops(1, Chain);
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002535 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00002536
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002537 getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal, InternalLinkage,
2538 CLI, Callee, Chain);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002539
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002540 if (IsTailCall)
2541 return DAG.getNode(MipsISD::TailCall, DL, MVT::Other, &Ops[0], Ops.size());
Akira Hatanaka2b861be2012-10-19 21:47:33 +00002542
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002543 Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, &Ops[0], Ops.size());
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00002544 SDValue InFlag = Chain.getValue(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002545
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002546 // Create the CALLSEQ_END node.
Akira Hatanaka480eeb52012-07-26 23:27:01 +00002547 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal,
Andrew Trick6e0b2a02013-05-29 22:03:55 +00002548 DAG.getIntPtrConstant(0, true), InFlag, DL);
Bruno Cardoso Lopes3ed6f872010-01-30 18:32:07 +00002549 InFlag = Chain.getValue(1);
2550
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002551 // Handle result values, copying them out of physregs into vregs that we
2552 // return.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002553 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg,
2554 Ins, DL, DAG, InVals, CLI.Callee.getNode(), CLI.RetTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002555}
2556
Dan Gohman98ca4f22009-08-05 01:29:28 +00002557/// LowerCallResult - Lower the result values of a call into the
2558/// appropriate copies out of appropriate physical registers.
2559SDValue
2560MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002561 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002562 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002563 SDLoc DL, SelectionDAG &DAG,
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002564 SmallVectorImpl<SDValue> &InVals,
2565 const SDNode *CallNode,
2566 const Type *RetTy) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002567 // Assign locations to each value returned by this call.
2568 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002569 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka864f6602012-06-14 21:10:56 +00002570 getTargetMachine(), RVLocs, *DAG.getContext());
Akira Hatanakaad341d42013-08-20 23:38:40 +00002571 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo);
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002572
Reed Kotlerc673f9c2013-08-30 19:40:56 +00002573 MipsCCInfo.analyzeCallResult(Ins, Subtarget->mipsSEUsesSoftFloat(),
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002574 CallNode, RetTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002575
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002576 // Copy all of the result registers out of their specified physreg.
2577 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002578 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(),
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002579 RVLocs[i].getLocVT(), InFlag);
2580 Chain = Val.getValue(1);
2581 InFlag = Val.getValue(2);
2582
2583 if (RVLocs[i].getValVT() != RVLocs[i].getLocVT())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002584 Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getValVT(), Val);
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002585
2586 InVals.push_back(Val);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002587 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +00002588
Dan Gohman98ca4f22009-08-05 01:29:28 +00002589 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002590}
2591
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002592//===----------------------------------------------------------------------===//
Dan Gohman98ca4f22009-08-05 01:29:28 +00002593// Formal Arguments Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002594//===----------------------------------------------------------------------===//
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002595/// LowerFormalArguments - transform physical registers into virtual registers
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002596/// and generate load operations for arguments places on the stack.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002597SDValue
2598MipsTargetLowering::LowerFormalArguments(SDValue Chain,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002599 CallingConv::ID CallConv,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002600 bool IsVarArg,
Akira Hatanaka82099682011-12-19 19:52:25 +00002601 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002602 SDLoc DL, SelectionDAG &DAG,
Akira Hatanaka0bf3dfb2011-04-15 21:00:26 +00002603 SmallVectorImpl<SDValue> &InVals)
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002604 const {
Bruno Cardoso Lopesf7f3b502008-08-04 07:12:52 +00002605 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002606 MachineFrameInfo *MFI = MF.getFrameInfo();
Bruno Cardoso Lopesa2b1bb52007-08-28 05:08:16 +00002607 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002608
Dan Gohman1e93df62010-04-17 14:41:14 +00002609 MipsFI->setVarArgsFrameIndex(0);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002610
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002611 // Used with vargs to acumulate store chains.
2612 std::vector<SDValue> OutChains;
2613
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002614 // Assign locations to all of the incoming arguments.
2615 SmallVector<CCValAssign, 16> ArgLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002616 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Akira Hatanaka82099682011-12-19 19:52:25 +00002617 getTargetMachine(), ArgLocs, *DAG.getContext());
Akira Hatanakaad341d42013-08-20 23:38:40 +00002618 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo);
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002619 Function::const_arg_iterator FuncArg =
2620 DAG.getMachineFunction().getFunction()->arg_begin();
Reed Kotlerc673f9c2013-08-30 19:40:56 +00002621 bool UseSoftFloat = Subtarget->mipsSEUsesSoftFloat();
Bruno Cardoso Lopes2ab22d12007-07-11 23:16:16 +00002622
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002623 MipsCCInfo.analyzeFormalArguments(Ins, UseSoftFloat, FuncArg);
Akira Hatanakab33b34a2012-10-30 19:37:25 +00002624 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(),
2625 MipsCCInfo.hasByValArg());
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002626
Akira Hatanaka4618e0b2012-10-27 00:44:39 +00002627 unsigned CurArgIdx = 0;
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002628 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002629
Akira Hatanaka4618e0b2012-10-27 00:44:39 +00002630 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002631 CCValAssign &VA = ArgLocs[i];
Akira Hatanaka4618e0b2012-10-27 00:44:39 +00002632 std::advance(FuncArg, Ins[i].OrigArgIndex - CurArgIdx);
2633 CurArgIdx = Ins[i].OrigArgIndex;
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002634 EVT ValVT = VA.getValVT();
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002635 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2636 bool IsRegLoc = VA.isRegLoc();
2637
2638 if (Flags.isByVal()) {
2639 assert(Flags.getByValSize() &&
2640 "ByVal args of size 0 should have been ignored by front-end.");
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002641 assert(ByValArg != MipsCCInfo.byval_end());
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002642 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg,
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002643 MipsCCInfo, *ByValArg);
2644 ++ByValArg;
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002645 continue;
2646 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002647
2648 // Arguments stored on registers
Akira Hatanaka3a5257d2011-11-12 02:29:58 +00002649 if (IsRegLoc) {
Owen Andersone50ed302009-08-10 22:56:29 +00002650 EVT RegVT = VA.getLocVT();
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002651 unsigned ArgReg = VA.getLocReg();
Craig Topper44d23822012-02-22 05:59:10 +00002652 const TargetRegisterClass *RC;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002653
Owen Anderson825b72b2009-08-11 20:47:22 +00002654 if (RegVT == MVT::i32)
Reed Kotlerbacbf1c2012-12-20 06:06:35 +00002655 RC = Subtarget->inMips16Mode()? &Mips::CPU16RegsRegClass :
Akira Hatanaka18587862013-08-06 23:08:38 +00002656 &Mips::GPR32RegClass;
Akira Hatanaka95934842011-09-24 01:34:44 +00002657 else if (RegVT == MVT::i64)
Akira Hatanaka18587862013-08-06 23:08:38 +00002658 RC = &Mips::GPR64RegClass;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002659 else if (RegVT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00002660 RC = &Mips::FGR32RegClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002661 else if (RegVT == MVT::f64)
Akira Hatanakaad341d42013-08-20 23:38:40 +00002662 RC = Subtarget->isFP64bit() ? &Mips::FGR64RegClass :
2663 &Mips::AFGR64RegClass;
Akira Hatanaka09dd60f2011-09-26 21:37:50 +00002664 else
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002665 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002666
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002667 // Transform the arguments stored on
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002668 // physical registers into virtual ones
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002669 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC);
2670 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002671
2672 // If this is an 8 or 16-bit value, it has been passed promoted
2673 // to 32 bits. Insert an assert[sz]ext to capture this, then
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002674 // truncate to the right size.
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002675 if (VA.getLocInfo() != CCValAssign::Full) {
Chris Lattnerd4015072009-03-26 05:28:14 +00002676 unsigned Opcode = 0;
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002677 if (VA.getLocInfo() == CCValAssign::SExt)
2678 Opcode = ISD::AssertSext;
2679 else if (VA.getLocInfo() == CCValAssign::ZExt)
2680 Opcode = ISD::AssertZext;
Chris Lattnerd4015072009-03-26 05:28:14 +00002681 if (Opcode)
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002682 ArgValue = DAG.getNode(Opcode, DL, RegVT, ArgValue,
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002683 DAG.getValueType(ValVT));
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002684 ArgValue = DAG.getNode(ISD::TRUNCATE, DL, ValVT, ArgValue);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002685 }
2686
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002687 // Handle floating point arguments passed in integer registers and
2688 // long double arguments passed in floating point registers.
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002689 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00002690 (RegVT == MVT::i64 && ValVT == MVT::f64) ||
2691 (RegVT == MVT::f64 && ValVT == MVT::i64))
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002692 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002693 else if (IsO32 && RegVT == MVT::i32 && ValVT == MVT::f64) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002694 unsigned Reg2 = addLiveIn(DAG.getMachineFunction(),
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002695 getNextIntArgReg(ArgReg), RC);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002696 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT);
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002697 if (!Subtarget->isLittle())
2698 std::swap(ArgValue, ArgValue2);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002699 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64,
Akira Hatanakafeaa4c32011-10-28 19:55:48 +00002700 ArgValue, ArgValue2);
Bruno Cardoso Lopesb53db4f2009-03-19 02:12:28 +00002701 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002702
Dan Gohman98ca4f22009-08-05 01:29:28 +00002703 InVals.push_back(ArgValue);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002704 } else { // VA.isRegLoc()
2705
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002706 // sanity check
2707 assert(VA.isMemLoc());
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002708
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002709 // The stack pointer offset is relative to the caller stack frame.
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002710 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Akira Hatanakab4d8d312011-05-24 00:23:52 +00002711 VA.getLocMemOffset(), true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002712
2713 // Create load nodes to retrieve arguments from the stack
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002714 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002715 InVals.push_back(DAG.getLoad(ValVT, DL, Chain, FIN,
Akira Hatanakafe30a9b2012-10-27 00:29:43 +00002716 MachinePointerInfo::getFixedStack(FI),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002717 false, false, false, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002718 }
2719 }
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002720
2721 // The mips ABIs for returning structs by value requires that we copy
2722 // the sret argument into $v0 for the return. Save the argument into
2723 // a virtual register so that we can access it from the return points.
2724 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2725 unsigned Reg = MipsFI->getSRetReturnReg();
2726 if (!Reg) {
Akira Hatanaka30580ce2012-10-19 22:11:40 +00002727 Reg = MF.getRegInfo().
2728 createVirtualRegister(getRegClassFor(IsN64 ? MVT::i64 : MVT::i32));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002729 MipsFI->setSRetReturnReg(Reg);
2730 }
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002731 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[0]);
2732 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002733 }
2734
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002735 if (IsVarArg)
2736 writeVarArgRegs(OutChains, MipsCCInfo, Chain, DL, DAG);
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002737
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002738 // All stores are grouped in one node to allow the matching between
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002739 // the size of Ins and InVals. This only happens when on varg functions
2740 if (!OutChains.empty()) {
2741 OutChains.push_back(Chain);
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002742 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Bruno Cardoso Lopesb37a7422010-02-06 19:20:49 +00002743 &OutChains[0], OutChains.size());
2744 }
2745
Dan Gohman98ca4f22009-08-05 01:29:28 +00002746 return Chain;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002747}
2748
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002749//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002750// Return Value Calling Convention Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002751//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002752
Akira Hatanaka97d9f082012-10-10 01:27:09 +00002753bool
2754MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002755 MachineFunction &MF, bool IsVarArg,
Akira Hatanaka97d9f082012-10-10 01:27:09 +00002756 const SmallVectorImpl<ISD::OutputArg> &Outs,
2757 LLVMContext &Context) const {
2758 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002759 CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(),
Akira Hatanaka97d9f082012-10-10 01:27:09 +00002760 RVLocs, Context);
2761 return CCInfo.CheckReturn(Outs, RetCC_Mips);
2762}
2763
Dan Gohman98ca4f22009-08-05 01:29:28 +00002764SDValue
2765MipsTargetLowering::LowerReturn(SDValue Chain,
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002766 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002767 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002768 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002769 SDLoc DL, SelectionDAG &DAG) const {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002770 // CCValAssign - represent the assignment of
2771 // the return value to a location
2772 SmallVector<CCValAssign, 16> RVLocs;
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002773 MachineFunction &MF = DAG.getMachineFunction();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002774
2775 // CCState - Info about the registers and stack slot.
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002776 CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(), RVLocs,
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002777 *DAG.getContext());
Akira Hatanakaad341d42013-08-20 23:38:40 +00002778 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002779
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002780 // Analyze return values.
Reed Kotlerc673f9c2013-08-30 19:40:56 +00002781 MipsCCInfo.analyzeReturn(Outs, Subtarget->mipsSEUsesSoftFloat(),
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002782 MF.getFunction()->getReturnType());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002783
Dan Gohman475871a2008-07-27 21:46:04 +00002784 SDValue Flag;
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002785 SmallVector<SDValue, 4> RetOps(1, Chain);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002786
2787 // Copy the result values into the output registers.
2788 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002789 SDValue Val = OutVals[i];
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002790 CCValAssign &VA = RVLocs[i];
2791 assert(VA.isRegLoc() && "Can only return in registers!");
2792
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002793 if (RVLocs[i].getValVT() != RVLocs[i].getLocVT())
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002794 Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getLocVT(), Val);
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002795
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002796 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002797
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002798 // Guarantee that all emitted copies are stuck together with flags.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002799 Flag = Chain.getValue(1);
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002800 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002801 }
2802
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002803 // The mips ABIs for returning structs by value requires that we copy
2804 // the sret argument into $v0 for the return. We saved the argument into
2805 // a virtual register in the entry block, so now we copy the value out
2806 // and into $v0.
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00002807 if (MF.getFunction()->hasStructRetAttr()) {
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002808 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2809 unsigned Reg = MipsFI->getSRetReturnReg();
2810
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002811 if (!Reg)
Torok Edwinc23197a2009-07-14 16:55:14 +00002812 llvm_unreachable("sret virtual register not created in the entry block");
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002813 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Akira Hatanaka2ef5bd32012-10-24 02:10:54 +00002814 unsigned V0 = IsN64 ? Mips::V0_64 : Mips::V0;
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002815
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002816 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag);
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002817 Flag = Chain.getValue(1);
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002818 RetOps.push_back(DAG.getRegister(V0, getPointerTy()));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002819 }
2820
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002821 RetOps[0] = Chain; // Update chain.
Akira Hatanaka182ef6f2012-07-10 00:19:06 +00002822
Jakob Stoklund Olesend0735962013-02-05 18:12:03 +00002823 // Add the flag if we have it.
2824 if (Flag.getNode())
2825 RetOps.push_back(Flag);
2826
2827 // Return on Mips is always a "jr $ra"
Akira Hatanakaf635ef42013-03-12 00:16:36 +00002828 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, &RetOps[0], RetOps.size());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002829}
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002830
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002831//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002832// Mips Inline Assembly Support
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00002833//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002834
2835/// getConstraintType - Given a constraint letter, return the type of
2836/// constraint it is for this target.
2837MipsTargetLowering::ConstraintType MipsTargetLowering::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002838getConstraintType(const std::string &Constraint) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002839{
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002840 // Mips specific constrainy
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002841 // GCC config/mips/constraints.md
2842 //
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002843 // 'd' : An address register. Equivalent to r
2844 // unless generating MIPS16 code.
2845 // 'y' : Equivalent to r; retained for
2846 // backwards compatibility.
Eric Christopher1d5a3922012-05-07 06:25:10 +00002847 // 'c' : A register suitable for use in an indirect
2848 // jump. This will always be $25 for -mabicalls.
Eric Christopheraf97f732012-05-07 06:25:19 +00002849 // 'l' : The lo register. 1 word storage.
2850 // 'x' : The hilo register pair. Double word storage.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002851 if (Constraint.size() == 1) {
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002852 switch (Constraint[0]) {
2853 default : break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002854 case 'd':
2855 case 'y':
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00002856 case 'f':
Eric Christopher1d5a3922012-05-07 06:25:10 +00002857 case 'c':
Eric Christopher4adbefe2012-05-07 06:25:15 +00002858 case 'l':
Eric Christopheraf97f732012-05-07 06:25:19 +00002859 case 'x':
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002860 return C_RegisterClass;
Jack Carter0b9675d2013-03-04 21:33:15 +00002861 case 'R':
2862 return C_Memory;
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002863 }
2864 }
2865 return TargetLowering::getConstraintType(Constraint);
2866}
2867
John Thompson44ab89e2010-10-29 17:29:13 +00002868/// Examine constraint type and operand type and determine a weight value.
2869/// This object must already have been set up with the operand type
2870/// and the current alternative constraint selected.
2871TargetLowering::ConstraintWeight
2872MipsTargetLowering::getSingleConstraintMatchWeight(
2873 AsmOperandInfo &info, const char *constraint) const {
2874 ConstraintWeight weight = CW_Invalid;
2875 Value *CallOperandVal = info.CallOperandVal;
2876 // If we don't have a value, we can't do a match,
2877 // but allow it at the lowest weight.
2878 if (CallOperandVal == NULL)
2879 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002880 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00002881 // Look at the constraint type.
2882 switch (*constraint) {
2883 default:
2884 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2885 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002886 case 'd':
2887 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +00002888 if (type->isIntegerTy())
2889 weight = CW_Register;
2890 break;
2891 case 'f':
2892 if (type->isFloatTy())
2893 weight = CW_Register;
2894 break;
Eric Christopher1d5a3922012-05-07 06:25:10 +00002895 case 'c': // $25 for indirect jumps
Eric Christopher4adbefe2012-05-07 06:25:15 +00002896 case 'l': // lo register
Eric Christopheraf97f732012-05-07 06:25:19 +00002897 case 'x': // hilo register pair
Eric Christopher1d5a3922012-05-07 06:25:10 +00002898 if (type->isIntegerTy())
2899 weight = CW_SpecificReg;
2900 break;
Eric Christopher50ab0392012-05-07 03:13:32 +00002901 case 'I': // signed 16 bit immediate
Eric Christophere5076d42012-05-07 03:13:42 +00002902 case 'J': // integer zero
Eric Christopherf49f8462012-05-07 05:46:29 +00002903 case 'K': // unsigned 16 bit immediate
Eric Christopher5ac47bb2012-05-07 05:46:37 +00002904 case 'L': // signed 32 bit immediate where lower 16 bits are 0
Eric Christopher60cfc792012-05-07 05:46:43 +00002905 case 'N': // immediate in the range of -65535 to -1 (inclusive)
Eric Christopher1ce20342012-05-07 05:46:48 +00002906 case 'O': // signed 15 bit immediate (+- 16383)
Eric Christopher54412a72012-05-07 06:25:02 +00002907 case 'P': // immediate in the range of 65535 to 1 (inclusive)
Eric Christopher50ab0392012-05-07 03:13:32 +00002908 if (isa<ConstantInt>(CallOperandVal))
2909 weight = CW_Constant;
2910 break;
Jack Carter0b9675d2013-03-04 21:33:15 +00002911 case 'R':
2912 weight = CW_Memory;
2913 break;
John Thompson44ab89e2010-10-29 17:29:13 +00002914 }
2915 return weight;
2916}
2917
Akira Hatanakabfb07b12013-08-14 00:21:25 +00002918/// This is a helper function to parse a physical register string and split it
2919/// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag
2920/// that is returned indicates whether parsing was successful. The second flag
2921/// is true if the numeric part exists.
2922static std::pair<bool, bool>
2923parsePhysicalReg(const StringRef &C, std::string &Prefix,
2924 unsigned long long &Reg) {
2925 if (C.front() != '{' || C.back() != '}')
2926 return std::make_pair(false, false);
2927
2928 // Search for the first numeric character.
2929 StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1;
2930 I = std::find_if(B, E, std::ptr_fun(isdigit));
2931
2932 Prefix.assign(B, I - B);
2933
2934 // The second flag is set to false if no numeric characters were found.
2935 if (I == E)
2936 return std::make_pair(true, false);
2937
2938 // Parse the numeric characters.
2939 return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg),
2940 true);
2941}
2942
2943std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering::
2944parseRegForInlineAsmConstraint(const StringRef &C, MVT VT) const {
2945 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2946 const TargetRegisterClass *RC;
2947 std::string Prefix;
2948 unsigned long long Reg;
2949
2950 std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg);
2951
2952 if (!R.first)
2953 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2954
2955 if ((Prefix == "hi" || Prefix == "lo")) { // Parse hi/lo.
2956 // No numeric characters follow "hi" or "lo".
2957 if (R.second)
2958 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2959
2960 RC = TRI->getRegClass(Prefix == "hi" ?
Akira Hatanakacbaf6d02013-08-14 00:47:08 +00002961 Mips::HI32RegClassID : Mips::LO32RegClassID);
Akira Hatanakabfb07b12013-08-14 00:21:25 +00002962 return std::make_pair(*(RC->begin()), RC);
2963 }
2964
2965 if (!R.second)
2966 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2967
2968 if (Prefix == "$f") { // Parse $f0-$f31.
2969 // If the size of FP registers is 64-bit or Reg is an even number, select
2970 // the 64-bit register class. Otherwise, select the 32-bit register class.
2971 if (VT == MVT::Other)
2972 VT = (Subtarget->isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32;
2973
2974 RC= getRegClassFor(VT);
2975
2976 if (RC == &Mips::AFGR64RegClass) {
2977 assert(Reg % 2 == 0);
2978 Reg >>= 1;
2979 }
2980 } else if (Prefix == "$fcc") { // Parse $fcc0-$fcc7.
2981 RC = TRI->getRegClass(Mips::FCCRegClassID);
2982 } else { // Parse $0-$31.
2983 assert(Prefix == "$");
2984 RC = getRegClassFor((VT == MVT::Other) ? MVT::i32 : VT);
2985 }
2986
2987 assert(Reg < RC->getNumRegs());
2988 return std::make_pair(*(RC->begin() + Reg), RC);
2989}
2990
Eric Christopher38d64262011-06-29 19:33:04 +00002991/// Given a register class constraint, like 'r', if this corresponds directly
2992/// to an LLVM register class, return a register of 0 and the register class
2993/// pointer.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002994std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
Chad Rosier5b3fca52013-06-22 18:37:38 +00002995getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00002996{
2997 if (Constraint.size() == 1) {
2998 switch (Constraint[0]) {
Eric Christopher314aff12011-06-29 19:04:31 +00002999 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
3000 case 'y': // Same as 'r'. Exists for compatibility.
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00003001 case 'r':
Akira Hatanakaafc945b2012-09-12 23:27:55 +00003002 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
3003 if (Subtarget->inMips16Mode())
3004 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
Akira Hatanaka18587862013-08-06 23:08:38 +00003005 return std::make_pair(0U, &Mips::GPR32RegClass);
Akira Hatanakaafc945b2012-09-12 23:27:55 +00003006 }
Jack Carter10de0252012-07-02 23:35:23 +00003007 if (VT == MVT::i64 && !HasMips64)
Akira Hatanaka18587862013-08-06 23:08:38 +00003008 return std::make_pair(0U, &Mips::GPR32RegClass);
Eric Christopher0ed1f762012-05-07 03:13:22 +00003009 if (VT == MVT::i64 && HasMips64)
Akira Hatanaka18587862013-08-06 23:08:38 +00003010 return std::make_pair(0U, &Mips::GPR64RegClass);
Eric Christopher0ed1f762012-05-07 03:13:22 +00003011 // This will generate an error message
3012 return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +00003013 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00003014 if (VT == MVT::f32)
Craig Topper420761a2012-04-20 07:30:17 +00003015 return std::make_pair(0U, &Mips::FGR32RegClass);
Akira Hatanakacb9dd722012-01-04 02:45:01 +00003016 if ((VT == MVT::f64) && (!Subtarget->isSingleFloat())) {
3017 if (Subtarget->isFP64bit())
Craig Topper420761a2012-04-20 07:30:17 +00003018 return std::make_pair(0U, &Mips::FGR64RegClass);
3019 return std::make_pair(0U, &Mips::AFGR64RegClass);
Akira Hatanakacb9dd722012-01-04 02:45:01 +00003020 }
Eric Christopher1d5a3922012-05-07 06:25:10 +00003021 break;
3022 case 'c': // register suitable for indirect jump
3023 if (VT == MVT::i32)
Akira Hatanaka18587862013-08-06 23:08:38 +00003024 return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass);
Eric Christopher1d5a3922012-05-07 06:25:10 +00003025 assert(VT == MVT::i64 && "Unexpected type.");
Akira Hatanaka18587862013-08-06 23:08:38 +00003026 return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass);
Eric Christopher4adbefe2012-05-07 06:25:15 +00003027 case 'l': // register suitable for indirect jump
3028 if (VT == MVT::i32)
Akira Hatanakacbaf6d02013-08-14 00:47:08 +00003029 return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass);
3030 return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass);
Eric Christopheraf97f732012-05-07 06:25:19 +00003031 case 'x': // register suitable for indirect jump
3032 // Fixme: Not triggering the use of both hi and low
3033 // This will generate an error message
3034 return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00003035 }
3036 }
Akira Hatanakabfb07b12013-08-14 00:21:25 +00003037
3038 std::pair<unsigned, const TargetRegisterClass *> R;
3039 R = parseRegForInlineAsmConstraint(Constraint, VT);
3040
3041 if (R.second)
3042 return R;
3043
Bruno Cardoso Lopes84f47c52007-08-21 16:09:25 +00003044 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3045}
3046
Eric Christopher50ab0392012-05-07 03:13:32 +00003047/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3048/// vector. If it is invalid, don't add anything to Ops.
3049void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3050 std::string &Constraint,
3051 std::vector<SDValue>&Ops,
3052 SelectionDAG &DAG) const {
3053 SDValue Result(0, 0);
3054
3055 // Only support length 1 constraints for now.
3056 if (Constraint.length() > 1) return;
3057
3058 char ConstraintLetter = Constraint[0];
3059 switch (ConstraintLetter) {
3060 default: break; // This will fall through to the generic implementation
3061 case 'I': // Signed 16 bit constant
3062 // If this fails, the parent routine will give an error
3063 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3064 EVT Type = Op.getValueType();
3065 int64_t Val = C->getSExtValue();
3066 if (isInt<16>(Val)) {
3067 Result = DAG.getTargetConstant(Val, Type);
3068 break;
3069 }
3070 }
3071 return;
Eric Christophere5076d42012-05-07 03:13:42 +00003072 case 'J': // integer zero
3073 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3074 EVT Type = Op.getValueType();
3075 int64_t Val = C->getZExtValue();
3076 if (Val == 0) {
3077 Result = DAG.getTargetConstant(0, Type);
3078 break;
3079 }
3080 }
3081 return;
Eric Christopherf49f8462012-05-07 05:46:29 +00003082 case 'K': // unsigned 16 bit immediate
3083 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3084 EVT Type = Op.getValueType();
3085 uint64_t Val = (uint64_t)C->getZExtValue();
3086 if (isUInt<16>(Val)) {
3087 Result = DAG.getTargetConstant(Val, Type);
3088 break;
3089 }
3090 }
3091 return;
Eric Christopher5ac47bb2012-05-07 05:46:37 +00003092 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3093 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3094 EVT Type = Op.getValueType();
3095 int64_t Val = C->getSExtValue();
3096 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
3097 Result = DAG.getTargetConstant(Val, Type);
3098 break;
3099 }
3100 }
3101 return;
Eric Christopher60cfc792012-05-07 05:46:43 +00003102 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3103 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3104 EVT Type = Op.getValueType();
3105 int64_t Val = C->getSExtValue();
3106 if ((Val >= -65535) && (Val <= -1)) {
3107 Result = DAG.getTargetConstant(Val, Type);
3108 break;
3109 }
3110 }
3111 return;
Eric Christopher1ce20342012-05-07 05:46:48 +00003112 case 'O': // signed 15 bit immediate
3113 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3114 EVT Type = Op.getValueType();
3115 int64_t Val = C->getSExtValue();
3116 if ((isInt<15>(Val))) {
3117 Result = DAG.getTargetConstant(Val, Type);
3118 break;
3119 }
3120 }
3121 return;
Eric Christopher54412a72012-05-07 06:25:02 +00003122 case 'P': // immediate in the range of 1 to 65535 (inclusive)
3123 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3124 EVT Type = Op.getValueType();
3125 int64_t Val = C->getSExtValue();
3126 if ((Val <= 65535) && (Val >= 1)) {
3127 Result = DAG.getTargetConstant(Val, Type);
3128 break;
3129 }
3130 }
3131 return;
Eric Christopher50ab0392012-05-07 03:13:32 +00003132 }
3133
3134 if (Result.getNode()) {
3135 Ops.push_back(Result);
3136 return;
3137 }
3138
3139 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3140}
3141
Dan Gohman6520e202008-10-18 02:06:02 +00003142bool
Akira Hatanaka94e47282012-11-17 00:25:41 +00003143MipsTargetLowering::isLegalAddressingMode(const AddrMode &AM, Type *Ty) const {
3144 // No global is ever allowed as a base.
3145 if (AM.BaseGV)
3146 return false;
3147
3148 switch (AM.Scale) {
3149 case 0: // "r+i" or just "i", depending on HasBaseReg.
3150 break;
3151 case 1:
3152 if (!AM.HasBaseReg) // allow "r+i".
3153 break;
3154 return false; // disallow "r+r" or "r+r+i".
3155 default:
3156 return false;
3157 }
3158
3159 return true;
3160}
3161
3162bool
Dan Gohman6520e202008-10-18 02:06:02 +00003163MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3164 // The Mips target isn't yet aware of offsets.
3165 return false;
3166}
Evan Chengeb2f9692009-10-27 19:56:55 +00003167
Akira Hatanakae193b322012-06-13 19:33:32 +00003168EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00003169 unsigned SrcAlign,
3170 bool IsMemset, bool ZeroMemset,
Akira Hatanakae193b322012-06-13 19:33:32 +00003171 bool MemcpyStrSrc,
3172 MachineFunction &MF) const {
3173 if (Subtarget->hasMips64())
3174 return MVT::i64;
3175
3176 return MVT::i32;
3177}
3178
Evan Chenga1eaa3c2009-10-28 01:43:28 +00003179bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
3180 if (VT != MVT::f32 && VT != MVT::f64)
3181 return false;
Bruno Cardoso Lopes6b902822011-01-18 19:41:41 +00003182 if (Imm.isNegZero())
3183 return false;
Evan Chengeb2f9692009-10-27 19:56:55 +00003184 return Imm.isZero();
3185}
Akira Hatanaka6c2cf8b2012-02-03 04:33:00 +00003186
3187unsigned MipsTargetLowering::getJumpTableEncoding() const {
3188 if (IsN64)
3189 return MachineJumpTableInfo::EK_GPRel64BlockAddress;
Jia Liubb481f82012-02-28 07:46:26 +00003190
Akira Hatanaka6c2cf8b2012-02-03 04:33:00 +00003191 return TargetLowering::getJumpTableEncoding();
3192}
Akira Hatanaka7887c902012-10-26 23:56:38 +00003193
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003194/// This function returns true if CallSym is a long double emulation routine.
3195static bool isF128SoftLibCall(const char *CallSym) {
3196 const char *const LibCalls[] =
3197 {"__addtf3", "__divtf3", "__eqtf2", "__extenddftf2", "__extendsftf2",
3198 "__fixtfdi", "__fixtfsi", "__fixtfti", "__fixunstfdi", "__fixunstfsi",
3199 "__fixunstfti", "__floatditf", "__floatsitf", "__floattitf",
3200 "__floatunditf", "__floatunsitf", "__floatuntitf", "__getf2", "__gttf2",
3201 "__letf2", "__lttf2", "__multf3", "__netf2", "__powitf2", "__subtf3",
3202 "__trunctfdf2", "__trunctfsf2", "__unordtf2",
3203 "ceill", "copysignl", "cosl", "exp2l", "expl", "floorl", "fmal", "fmodl",
3204 "log10l", "log2l", "logl", "nearbyintl", "powl", "rintl", "sinl", "sqrtl",
3205 "truncl"};
3206
3207 const char * const *End = LibCalls + array_lengthof(LibCalls);
3208
3209 // Check that LibCalls is sorted alphabetically.
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00003210 MipsTargetLowering::LTStr Comp;
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003211
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00003212#ifndef NDEBUG
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003213 for (const char * const *I = LibCalls; I < End - 1; ++I)
3214 assert(Comp(*I, *(I + 1)));
3215#endif
3216
Akira Hatanaka5ac065a2013-03-13 00:54:29 +00003217 return std::binary_search(LibCalls, End, CallSym, Comp);
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003218}
3219
3220/// This function returns true if Ty is fp128 or i128 which was originally a
3221/// fp128.
3222static bool originalTypeIsF128(const Type *Ty, const SDNode *CallNode) {
3223 if (Ty->isFP128Ty())
3224 return true;
3225
3226 const ExternalSymbolSDNode *ES =
3227 dyn_cast_or_null<const ExternalSymbolSDNode>(CallNode);
3228
3229 // If the Ty is i128 and the function being called is a long double emulation
3230 // routine, then the original type is f128.
3231 return (ES && Ty->isIntegerTy(128) && isF128SoftLibCall(ES->getSymbol()));
3232}
3233
Reed Kotler46090912013-05-10 22:25:39 +00003234MipsTargetLowering::MipsCC::SpecialCallingConvType
3235 MipsTargetLowering::getSpecialCallingConv(SDValue Callee) const {
3236 MipsCC::SpecialCallingConvType SpecialCallingConv =
3237 MipsCC::NoSpecialCallingConv;;
3238 if (Subtarget->inMips16HardFloat()) {
3239 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3240 llvm::StringRef Sym = G->getGlobal()->getName();
3241 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
3242 if (F->hasFnAttribute("__Mips16RetHelper")) {
3243 SpecialCallingConv = MipsCC::Mips16RetHelperConv;
3244 }
3245 }
3246 }
3247 return SpecialCallingConv;
3248}
3249
3250MipsTargetLowering::MipsCC::MipsCC(
Akira Hatanakaad341d42013-08-20 23:38:40 +00003251 CallingConv::ID CC, bool IsO32_, bool IsFP64_, CCState &Info,
Reed Kotler46090912013-05-10 22:25:39 +00003252 MipsCC::SpecialCallingConvType SpecialCallingConv_)
Akira Hatanakaad341d42013-08-20 23:38:40 +00003253 : CCInfo(Info), CallConv(CC), IsO32(IsO32_), IsFP64(IsFP64_),
Reed Kotler46090912013-05-10 22:25:39 +00003254 SpecialCallingConv(SpecialCallingConv_){
Akira Hatanaka7887c902012-10-26 23:56:38 +00003255 // Pre-allocate reserved argument area.
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003256 CCInfo.AllocateStack(reservedArgArea(), 1);
Akira Hatanaka7887c902012-10-26 23:56:38 +00003257}
3258
Reed Kotler46090912013-05-10 22:25:39 +00003259
Akira Hatanaka7887c902012-10-26 23:56:38 +00003260void MipsTargetLowering::MipsCC::
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003261analyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Args,
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00003262 bool IsVarArg, bool IsSoftFloat, const SDNode *CallNode,
3263 std::vector<ArgListEntry> &FuncArgs) {
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003264 assert((CallConv != CallingConv::Fast || !IsVarArg) &&
3265 "CallingConv::Fast shouldn't be used for vararg functions.");
3266
Akira Hatanaka7887c902012-10-26 23:56:38 +00003267 unsigned NumOpnds = Args.size();
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003268 llvm::CCAssignFn *FixedFn = fixedArgFn(), *VarFn = varArgFn();
Akira Hatanaka7887c902012-10-26 23:56:38 +00003269
3270 for (unsigned I = 0; I != NumOpnds; ++I) {
3271 MVT ArgVT = Args[I].VT;
3272 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
3273 bool R;
3274
3275 if (ArgFlags.isByVal()) {
3276 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
3277 continue;
3278 }
3279
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003280 if (IsVarArg && !Args[I].IsFixed)
Akira Hatanaka7887c902012-10-26 23:56:38 +00003281 R = VarFn(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
Akira Hatanakacb2eafd2013-03-05 22:20:28 +00003282 else {
3283 MVT RegVT = getRegVT(ArgVT, FuncArgs[Args[I].OrigArgIndex].Ty, CallNode,
3284 IsSoftFloat);
3285 R = FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo);
3286 }
Akira Hatanaka7887c902012-10-26 23:56:38 +00003287
3288 if (R) {
3289#ifndef NDEBUG
3290 dbgs() << "Call operand #" << I << " has unhandled type "
3291 << EVT(ArgVT).getEVTString();
3292#endif
3293 llvm_unreachable(0);
3294 }
3295 }
3296}
3297
3298void MipsTargetLowering::MipsCC::
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003299analyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Args,
3300 bool IsSoftFloat, Function::const_arg_iterator FuncArg) {
Akira Hatanaka7887c902012-10-26 23:56:38 +00003301 unsigned NumArgs = Args.size();
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003302 llvm::CCAssignFn *FixedFn = fixedArgFn();
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003303 unsigned CurArgIdx = 0;
Akira Hatanaka7887c902012-10-26 23:56:38 +00003304
3305 for (unsigned I = 0; I != NumArgs; ++I) {
3306 MVT ArgVT = Args[I].VT;
3307 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003308 std::advance(FuncArg, Args[I].OrigArgIndex - CurArgIdx);
3309 CurArgIdx = Args[I].OrigArgIndex;
Akira Hatanaka7887c902012-10-26 23:56:38 +00003310
3311 if (ArgFlags.isByVal()) {
3312 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
3313 continue;
3314 }
3315
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003316 MVT RegVT = getRegVT(ArgVT, FuncArg->getType(), 0, IsSoftFloat);
3317
3318 if (!FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo))
Akira Hatanaka7887c902012-10-26 23:56:38 +00003319 continue;
3320
3321#ifndef NDEBUG
3322 dbgs() << "Formal Arg #" << I << " has unhandled type "
3323 << EVT(ArgVT).getEVTString();
3324#endif
3325 llvm_unreachable(0);
3326 }
3327}
3328
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003329template<typename Ty>
3330void MipsTargetLowering::MipsCC::
3331analyzeReturn(const SmallVectorImpl<Ty> &RetVals, bool IsSoftFloat,
3332 const SDNode *CallNode, const Type *RetTy) const {
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003333 CCAssignFn *Fn;
3334
3335 if (IsSoftFloat && originalTypeIsF128(RetTy, CallNode))
3336 Fn = RetCC_F128Soft;
3337 else
3338 Fn = RetCC_Mips;
3339
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003340 for (unsigned I = 0, E = RetVals.size(); I < E; ++I) {
3341 MVT VT = RetVals[I].VT;
3342 ISD::ArgFlagsTy Flags = RetVals[I].Flags;
3343 MVT RegVT = this->getRegVT(VT, RetTy, CallNode, IsSoftFloat);
3344
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003345 if (Fn(I, VT, RegVT, CCValAssign::Full, Flags, this->CCInfo)) {
Akira Hatanaka7433b2e2013-03-05 22:41:55 +00003346#ifndef NDEBUG
3347 dbgs() << "Call result #" << I << " has unhandled type "
3348 << EVT(VT).getEVTString() << '\n';
3349#endif
3350 llvm_unreachable(0);
3351 }
3352 }
3353}
3354
3355void MipsTargetLowering::MipsCC::
3356analyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins, bool IsSoftFloat,
3357 const SDNode *CallNode, const Type *RetTy) const {
3358 analyzeReturn(Ins, IsSoftFloat, CallNode, RetTy);
3359}
3360
3361void MipsTargetLowering::MipsCC::
3362analyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs, bool IsSoftFloat,
3363 const Type *RetTy) const {
3364 analyzeReturn(Outs, IsSoftFloat, 0, RetTy);
3365}
3366
Akira Hatanaka7887c902012-10-26 23:56:38 +00003367void
3368MipsTargetLowering::MipsCC::handleByValArg(unsigned ValNo, MVT ValVT,
3369 MVT LocVT,
3370 CCValAssign::LocInfo LocInfo,
3371 ISD::ArgFlagsTy ArgFlags) {
3372 assert(ArgFlags.getByValSize() && "Byval argument's size shouldn't be 0.");
3373
3374 struct ByValArgInfo ByVal;
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003375 unsigned RegSize = regSize();
Akira Hatanaka7887c902012-10-26 23:56:38 +00003376 unsigned ByValSize = RoundUpToAlignment(ArgFlags.getByValSize(), RegSize);
3377 unsigned Align = std::min(std::max(ArgFlags.getByValAlign(), RegSize),
3378 RegSize * 2);
3379
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003380 if (useRegsForByval())
Akira Hatanaka7887c902012-10-26 23:56:38 +00003381 allocateRegs(ByVal, ByValSize, Align);
3382
3383 // Allocate space on caller's stack.
3384 ByVal.Address = CCInfo.AllocateStack(ByValSize - RegSize * ByVal.NumRegs,
3385 Align);
3386 CCInfo.addLoc(CCValAssign::getMem(ValNo, ValVT, ByVal.Address, LocVT,
3387 LocInfo));
3388 ByValArgs.push_back(ByVal);
3389}
3390
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003391unsigned MipsTargetLowering::MipsCC::numIntArgRegs() const {
3392 return IsO32 ? array_lengthof(O32IntRegs) : array_lengthof(Mips64IntRegs);
3393}
3394
3395unsigned MipsTargetLowering::MipsCC::reservedArgArea() const {
3396 return (IsO32 && (CallConv != CallingConv::Fast)) ? 16 : 0;
3397}
3398
3399const uint16_t *MipsTargetLowering::MipsCC::intArgRegs() const {
3400 return IsO32 ? O32IntRegs : Mips64IntRegs;
3401}
3402
3403llvm::CCAssignFn *MipsTargetLowering::MipsCC::fixedArgFn() const {
3404 if (CallConv == CallingConv::Fast)
3405 return CC_Mips_FastCC;
3406
Reed Kotler46090912013-05-10 22:25:39 +00003407 if (SpecialCallingConv == Mips16RetHelperConv)
3408 return CC_Mips16RetHelper;
Akira Hatanakaad341d42013-08-20 23:38:40 +00003409 return IsO32 ? (IsFP64 ? CC_MipsO32_FP64 : CC_MipsO32_FP32) : CC_MipsN;
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003410}
3411
3412llvm::CCAssignFn *MipsTargetLowering::MipsCC::varArgFn() const {
Akira Hatanakaad341d42013-08-20 23:38:40 +00003413 return IsO32 ? (IsFP64 ? CC_MipsO32_FP64 : CC_MipsO32_FP32) : CC_MipsN_VarArg;
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003414}
3415
3416const uint16_t *MipsTargetLowering::MipsCC::shadowRegs() const {
3417 return IsO32 ? O32IntRegs : Mips64DPRegs;
3418}
3419
Akira Hatanaka7887c902012-10-26 23:56:38 +00003420void MipsTargetLowering::MipsCC::allocateRegs(ByValArgInfo &ByVal,
3421 unsigned ByValSize,
3422 unsigned Align) {
Akira Hatanakaffd28a42013-02-15 21:45:11 +00003423 unsigned RegSize = regSize(), NumIntArgRegs = numIntArgRegs();
3424 const uint16_t *IntArgRegs = intArgRegs(), *ShadowRegs = shadowRegs();
Akira Hatanaka7887c902012-10-26 23:56:38 +00003425 assert(!(ByValSize % RegSize) && !(Align % RegSize) &&
3426 "Byval argument's size and alignment should be a multiple of"
3427 "RegSize.");
3428
3429 ByVal.FirstIdx = CCInfo.getFirstUnallocated(IntArgRegs, NumIntArgRegs);
3430
3431 // If Align > RegSize, the first arg register must be even.
3432 if ((Align > RegSize) && (ByVal.FirstIdx % 2)) {
3433 CCInfo.AllocateReg(IntArgRegs[ByVal.FirstIdx], ShadowRegs[ByVal.FirstIdx]);
3434 ++ByVal.FirstIdx;
3435 }
3436
3437 // Mark the registers allocated.
3438 for (unsigned I = ByVal.FirstIdx; ByValSize && (I < NumIntArgRegs);
3439 ByValSize -= RegSize, ++I, ++ByVal.NumRegs)
3440 CCInfo.AllocateReg(IntArgRegs[I], ShadowRegs[I]);
3441}
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003442
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003443MVT MipsTargetLowering::MipsCC::getRegVT(MVT VT, const Type *OrigTy,
3444 const SDNode *CallNode,
3445 bool IsSoftFloat) const {
3446 if (IsSoftFloat || IsO32)
3447 return VT;
3448
3449 // Check if the original type was fp128.
Akira Hatanaka1e3e8692013-03-05 22:54:59 +00003450 if (originalTypeIsF128(OrigTy, CallNode)) {
Akira Hatanaka5fdee6d2013-03-05 22:13:04 +00003451 assert(VT == MVT::i64);
3452 return MVT::f64;
3453 }
3454
3455 return VT;
3456}
3457
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003458void MipsTargetLowering::
Andrew Trickac6d9be2013-05-25 02:42:55 +00003459copyByValRegs(SDValue Chain, SDLoc DL, std::vector<SDValue> &OutChains,
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003460 SelectionDAG &DAG, const ISD::ArgFlagsTy &Flags,
3461 SmallVectorImpl<SDValue> &InVals, const Argument *FuncArg,
3462 const MipsCC &CC, const ByValArgInfo &ByVal) const {
3463 MachineFunction &MF = DAG.getMachineFunction();
3464 MachineFrameInfo *MFI = MF.getFrameInfo();
3465 unsigned RegAreaSize = ByVal.NumRegs * CC.regSize();
3466 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
3467 int FrameObjOffset;
3468
3469 if (RegAreaSize)
3470 FrameObjOffset = (int)CC.reservedArgArea() -
3471 (int)((CC.numIntArgRegs() - ByVal.FirstIdx) * CC.regSize());
3472 else
3473 FrameObjOffset = ByVal.Address;
3474
3475 // Create frame object.
3476 EVT PtrTy = getPointerTy();
3477 int FI = MFI->CreateFixedObject(FrameObjSize, FrameObjOffset, true);
3478 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
3479 InVals.push_back(FIN);
3480
3481 if (!ByVal.NumRegs)
3482 return;
3483
3484 // Copy arg registers.
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00003485 MVT RegTy = MVT::getIntegerVT(CC.regSize() * 8);
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003486 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3487
3488 for (unsigned I = 0; I < ByVal.NumRegs; ++I) {
3489 unsigned ArgReg = CC.intArgRegs()[ByVal.FirstIdx + I];
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003490 unsigned VReg = addLiveIn(MF, ArgReg, RC);
Akira Hatanakaeb98ae42012-10-27 00:10:18 +00003491 unsigned Offset = I * CC.regSize();
3492 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
3493 DAG.getConstant(Offset, PtrTy));
3494 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
3495 StorePtr, MachinePointerInfo(FuncArg, Offset),
3496 false, false, 0);
3497 OutChains.push_back(Store);
3498 }
3499}
Akira Hatanakadb40ede2012-10-27 00:16:36 +00003500
3501// Copy byVal arg to registers and stack.
3502void MipsTargetLowering::
Andrew Trickac6d9be2013-05-25 02:42:55 +00003503passByValArg(SDValue Chain, SDLoc DL,
Akira Hatanakabf6a77b2013-01-22 20:05:56 +00003504 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
Craig Toppera0ec3f92013-07-14 04:42:23 +00003505 SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr,
Akira Hatanakadb40ede2012-10-27 00:16:36 +00003506 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
3507 const MipsCC &CC, const ByValArgInfo &ByVal,
3508 const ISD::ArgFlagsTy &Flags, bool isLittle) const {
3509 unsigned ByValSize = Flags.getByValSize();
3510 unsigned Offset = 0; // Offset in # of bytes from the beginning of struct.
3511 unsigned RegSize = CC.regSize();
3512 unsigned Alignment = std::min(Flags.getByValAlign(), RegSize);
3513 EVT PtrTy = getPointerTy(), RegTy = MVT::getIntegerVT(RegSize * 8);
3514
3515 if (ByVal.NumRegs) {
3516 const uint16_t *ArgRegs = CC.intArgRegs();
3517 bool LeftoverBytes = (ByVal.NumRegs * RegSize > ByValSize);
3518 unsigned I = 0;
3519
3520 // Copy words to registers.
3521 for (; I < ByVal.NumRegs - LeftoverBytes; ++I, Offset += RegSize) {
3522 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3523 DAG.getConstant(Offset, PtrTy));
3524 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
3525 MachinePointerInfo(), false, false, false,
3526 Alignment);
3527 MemOpChains.push_back(LoadVal.getValue(1));
3528 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3529 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
3530 }
3531
3532 // Return if the struct has been fully copied.
3533 if (ByValSize == Offset)
3534 return;
3535
3536 // Copy the remainder of the byval argument with sub-word loads and shifts.
3537 if (LeftoverBytes) {
3538 assert((ByValSize > Offset) && (ByValSize < Offset + RegSize) &&
3539 "Size of the remainder should be smaller than RegSize.");
3540 SDValue Val;
3541
3542 for (unsigned LoadSize = RegSize / 2, TotalSizeLoaded = 0;
3543 Offset < ByValSize; LoadSize /= 2) {
3544 unsigned RemSize = ByValSize - Offset;
3545
3546 if (RemSize < LoadSize)
3547 continue;
3548
3549 // Load subword.
3550 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3551 DAG.getConstant(Offset, PtrTy));
3552 SDValue LoadVal =
3553 DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr,
3554 MachinePointerInfo(), MVT::getIntegerVT(LoadSize * 8),
3555 false, false, Alignment);
3556 MemOpChains.push_back(LoadVal.getValue(1));
3557
3558 // Shift the loaded value.
3559 unsigned Shamt;
3560
3561 if (isLittle)
3562 Shamt = TotalSizeLoaded;
3563 else
3564 Shamt = (RegSize - (TotalSizeLoaded + LoadSize)) * 8;
3565
3566 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
3567 DAG.getConstant(Shamt, MVT::i32));
3568
3569 if (Val.getNode())
3570 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
3571 else
3572 Val = Shift;
3573
3574 Offset += LoadSize;
3575 TotalSizeLoaded += LoadSize;
3576 Alignment = std::min(Alignment, LoadSize);
3577 }
3578
3579 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3580 RegsToPass.push_back(std::make_pair(ArgReg, Val));
3581 return;
3582 }
3583 }
3584
3585 // Copy remainder of byval arg to it with memcpy.
3586 unsigned MemCpySize = ByValSize - Offset;
3587 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3588 DAG.getConstant(Offset, PtrTy));
3589 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
3590 DAG.getIntPtrConstant(ByVal.Address));
3591 Chain = DAG.getMemcpy(Chain, DL, Dst, Src,
3592 DAG.getConstant(MemCpySize, PtrTy), Alignment,
3593 /*isVolatile=*/false, /*AlwaysInline=*/false,
3594 MachinePointerInfo(0), MachinePointerInfo(0));
3595 MemOpChains.push_back(Chain);
3596}
Akira Hatanakaf0848472012-10-27 00:21:13 +00003597
3598void
3599MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
3600 const MipsCC &CC, SDValue Chain,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003601 SDLoc DL, SelectionDAG &DAG) const {
Akira Hatanakaf0848472012-10-27 00:21:13 +00003602 unsigned NumRegs = CC.numIntArgRegs();
3603 const uint16_t *ArgRegs = CC.intArgRegs();
3604 const CCState &CCInfo = CC.getCCInfo();
3605 unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs, NumRegs);
3606 unsigned RegSize = CC.regSize();
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00003607 MVT RegTy = MVT::getIntegerVT(RegSize * 8);
Akira Hatanakaf0848472012-10-27 00:21:13 +00003608 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3609 MachineFunction &MF = DAG.getMachineFunction();
3610 MachineFrameInfo *MFI = MF.getFrameInfo();
3611 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3612
3613 // Offset of the first variable argument from stack pointer.
3614 int VaArgOffset;
3615
3616 if (NumRegs == Idx)
3617 VaArgOffset = RoundUpToAlignment(CCInfo.getNextStackOffset(), RegSize);
3618 else
3619 VaArgOffset =
3620 (int)CC.reservedArgArea() - (int)(RegSize * (NumRegs - Idx));
3621
3622 // Record the frame index of the first variable argument
3623 // which is a value necessary to VASTART.
3624 int FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
3625 MipsFI->setVarArgsFrameIndex(FI);
3626
3627 // Copy the integer registers that have not been used for argument passing
3628 // to the argument register save area. For O32, the save area is allocated
3629 // in the caller's stack frame, while for N32/64, it is allocated in the
3630 // callee's stack frame.
3631 for (unsigned I = Idx; I < NumRegs; ++I, VaArgOffset += RegSize) {
Akira Hatanakaf635ef42013-03-12 00:16:36 +00003632 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC);
Akira Hatanakaf0848472012-10-27 00:21:13 +00003633 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
3634 FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
3635 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
3636 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
3637 MachinePointerInfo(), false, false, 0);
3638 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(0);
3639 OutChains.push_back(Store);
3640 }
3641}