blob: 714d51f51eb1b547966c048741b26bee2596e00e [file] [log] [blame]
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001//===-- HexagonISelLowering.cpp - Hexagon DAG Lowering Implementation -----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the interfaces that Hexagon uses to lower LLVM code
11// into a selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "HexagonISelLowering.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000016#include "HexagonMachineFunctionInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000017#include "HexagonSubtarget.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "HexagonTargetMachine.h"
19#include "HexagonTargetObjectFile.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000020#include "llvm/CodeGen/CallingConvLower.h"
21#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/MachineFunction.h"
23#include "llvm/CodeGen/MachineInstrBuilder.h"
Craig Topperb25fda92012-03-17 18:46:09 +000024#include "llvm/CodeGen/MachineJumpTableInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000025#include "llvm/CodeGen/MachineRegisterInfo.h"
26#include "llvm/CodeGen/SelectionDAGISel.h"
27#include "llvm/CodeGen/ValueTypes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/CallingConv.h"
29#include "llvm/IR/DerivedTypes.h"
30#include "llvm/IR/Function.h"
31#include "llvm/IR/GlobalAlias.h"
32#include "llvm/IR/GlobalVariable.h"
33#include "llvm/IR/InlineAsm.h"
34#include "llvm/IR/Intrinsics.h"
NAKAMURA Takumi54eed762012-04-21 15:31:36 +000035#include "llvm/Support/CommandLine.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000036#include "llvm/Support/Debug.h"
37#include "llvm/Support/ErrorHandling.h"
NAKAMURA Takumie30303f2012-04-21 15:31:45 +000038#include "llvm/Support/raw_ostream.h"
NAKAMURA Takumi54eed762012-04-21 15:31:36 +000039
Craig Topperb25fda92012-03-17 18:46:09 +000040using namespace llvm;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000041
Chandler Carruthe96dd892014-04-21 22:55:11 +000042#define DEBUG_TYPE "hexagon-lowering"
43
Tony Linthicum1213a7a2011-12-12 21:14:40 +000044static cl::opt<bool>
45EmitJumpTables("hexagon-emit-jump-tables", cl::init(true), cl::Hidden,
46 cl::desc("Control jump table emission on Hexagon target"));
47
Benjamin Kramer602bb4a2013-10-27 11:16:09 +000048namespace {
49class HexagonCCState : public CCState {
50 int NumNamedVarArgParams;
51
52public:
53 HexagonCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
54 const TargetMachine &TM, SmallVectorImpl<CCValAssign> &locs,
55 LLVMContext &C, int NumNamedVarArgParams)
56 : CCState(CC, isVarArg, MF, TM, locs, C),
57 NumNamedVarArgParams(NumNamedVarArgParams) {}
58
59 int getNumNamedVarArgParams() const { return NumNamedVarArgParams; }
60};
61}
Tony Linthicum1213a7a2011-12-12 21:14:40 +000062
63// Implement calling convention for Hexagon.
64static bool
65CC_Hexagon(unsigned ValNo, MVT ValVT,
66 MVT LocVT, CCValAssign::LocInfo LocInfo,
67 ISD::ArgFlagsTy ArgFlags, CCState &State);
68
69static bool
70CC_Hexagon32(unsigned ValNo, MVT ValVT,
71 MVT LocVT, CCValAssign::LocInfo LocInfo,
72 ISD::ArgFlagsTy ArgFlags, CCState &State);
73
74static bool
75CC_Hexagon64(unsigned ValNo, MVT ValVT,
76 MVT LocVT, CCValAssign::LocInfo LocInfo,
77 ISD::ArgFlagsTy ArgFlags, CCState &State);
78
79static bool
80RetCC_Hexagon(unsigned ValNo, MVT ValVT,
81 MVT LocVT, CCValAssign::LocInfo LocInfo,
82 ISD::ArgFlagsTy ArgFlags, CCState &State);
83
84static bool
85RetCC_Hexagon32(unsigned ValNo, MVT ValVT,
86 MVT LocVT, CCValAssign::LocInfo LocInfo,
87 ISD::ArgFlagsTy ArgFlags, CCState &State);
88
89static bool
90RetCC_Hexagon64(unsigned ValNo, MVT ValVT,
91 MVT LocVT, CCValAssign::LocInfo LocInfo,
92 ISD::ArgFlagsTy ArgFlags, CCState &State);
93
94static bool
95CC_Hexagon_VarArg (unsigned ValNo, MVT ValVT,
96 MVT LocVT, CCValAssign::LocInfo LocInfo,
97 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Benjamin Kramer602bb4a2013-10-27 11:16:09 +000098 HexagonCCState &HState = static_cast<HexagonCCState &>(State);
Tony Linthicum1213a7a2011-12-12 21:14:40 +000099
100 // NumNamedVarArgParams can not be zero for a VarArg function.
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000101 assert((HState.getNumNamedVarArgParams() > 0) &&
102 "NumNamedVarArgParams is not bigger than zero.");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000103
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000104 if ((int)ValNo < HState.getNumNamedVarArgParams()) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000105 // Deal with named arguments.
106 return CC_Hexagon(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State);
107 }
108
109 // Deal with un-named arguments.
110 unsigned ofst;
111 if (ArgFlags.isByVal()) {
112 // If pass-by-value, the size allocated on stack is decided
113 // by ArgFlags.getByValSize(), not by the size of LocVT.
114 assert ((ArgFlags.getByValSize() > 8) &&
115 "ByValSize must be bigger than 8 bytes");
116 ofst = State.AllocateStack(ArgFlags.getByValSize(), 4);
117 State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
118 return false;
119 }
Jyotsna Vermac7dcc2f2013-03-07 20:28:34 +0000120 if (LocVT == MVT::i1 || LocVT == MVT::i8 || LocVT == MVT::i16) {
121 LocVT = MVT::i32;
122 ValVT = MVT::i32;
123 if (ArgFlags.isSExt())
124 LocInfo = CCValAssign::SExt;
125 else if (ArgFlags.isZExt())
126 LocInfo = CCValAssign::ZExt;
127 else
128 LocInfo = CCValAssign::AExt;
129 }
Sirish Pande69295b82012-05-10 20:20:25 +0000130 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000131 ofst = State.AllocateStack(4, 4);
132 State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
133 return false;
134 }
Sirish Pande69295b82012-05-10 20:20:25 +0000135 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000136 ofst = State.AllocateStack(8, 8);
137 State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
138 return false;
139 }
Craig Toppere73658d2014-04-28 04:05:08 +0000140 llvm_unreachable(nullptr);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000141}
142
143
144static bool
145CC_Hexagon (unsigned ValNo, MVT ValVT,
146 MVT LocVT, CCValAssign::LocInfo LocInfo,
147 ISD::ArgFlagsTy ArgFlags, CCState &State) {
148
149 if (ArgFlags.isByVal()) {
150 // Passed on stack.
151 assert ((ArgFlags.getByValSize() > 8) &&
152 "ByValSize must be bigger than 8 bytes");
153 unsigned Offset = State.AllocateStack(ArgFlags.getByValSize(), 4);
154 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
155 return false;
156 }
157
158 if (LocVT == MVT::i1 || LocVT == MVT::i8 || LocVT == MVT::i16) {
159 LocVT = MVT::i32;
160 ValVT = MVT::i32;
161 if (ArgFlags.isSExt())
162 LocInfo = CCValAssign::SExt;
163 else if (ArgFlags.isZExt())
164 LocInfo = CCValAssign::ZExt;
165 else
166 LocInfo = CCValAssign::AExt;
167 }
168
Sirish Pande69295b82012-05-10 20:20:25 +0000169 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000170 if (!CC_Hexagon32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
171 return false;
172 }
173
Sirish Pande69295b82012-05-10 20:20:25 +0000174 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000175 if (!CC_Hexagon64(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
176 return false;
177 }
178
179 return true; // CC didn't match.
180}
181
182
183static bool CC_Hexagon32(unsigned ValNo, MVT ValVT,
184 MVT LocVT, CCValAssign::LocInfo LocInfo,
185 ISD::ArgFlagsTy ArgFlags, CCState &State) {
186
Craig Topper840beec2014-04-04 05:16:06 +0000187 static const MCPhysReg RegList[] = {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000188 Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3, Hexagon::R4,
189 Hexagon::R5
190 };
191 if (unsigned Reg = State.AllocateReg(RegList, 6)) {
192 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
193 return false;
194 }
195
196 unsigned Offset = State.AllocateStack(4, 4);
197 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
198 return false;
199}
200
201static bool CC_Hexagon64(unsigned ValNo, MVT ValVT,
202 MVT LocVT, CCValAssign::LocInfo LocInfo,
203 ISD::ArgFlagsTy ArgFlags, CCState &State) {
204
205 if (unsigned Reg = State.AllocateReg(Hexagon::D0)) {
206 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
207 return false;
208 }
209
Craig Topper840beec2014-04-04 05:16:06 +0000210 static const MCPhysReg RegList1[] = {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000211 Hexagon::D1, Hexagon::D2
212 };
Craig Topper840beec2014-04-04 05:16:06 +0000213 static const MCPhysReg RegList2[] = {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000214 Hexagon::R1, Hexagon::R3
215 };
216 if (unsigned Reg = State.AllocateReg(RegList1, RegList2, 2)) {
217 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
218 return false;
219 }
220
221 unsigned Offset = State.AllocateStack(8, 8, Hexagon::D2);
222 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
223 return false;
224}
225
226static bool RetCC_Hexagon(unsigned ValNo, MVT ValVT,
227 MVT LocVT, CCValAssign::LocInfo LocInfo,
228 ISD::ArgFlagsTy ArgFlags, CCState &State) {
229
230
231 if (LocVT == MVT::i1 ||
232 LocVT == MVT::i8 ||
233 LocVT == MVT::i16) {
234 LocVT = MVT::i32;
235 ValVT = MVT::i32;
236 if (ArgFlags.isSExt())
237 LocInfo = CCValAssign::SExt;
238 else if (ArgFlags.isZExt())
239 LocInfo = CCValAssign::ZExt;
240 else
241 LocInfo = CCValAssign::AExt;
242 }
243
Sirish Pande69295b82012-05-10 20:20:25 +0000244 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000245 if (!RetCC_Hexagon32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
246 return false;
247 }
248
Sirish Pande69295b82012-05-10 20:20:25 +0000249 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000250 if (!RetCC_Hexagon64(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
251 return false;
252 }
253
254 return true; // CC didn't match.
255}
256
257static bool RetCC_Hexagon32(unsigned ValNo, MVT ValVT,
258 MVT LocVT, CCValAssign::LocInfo LocInfo,
259 ISD::ArgFlagsTy ArgFlags, CCState &State) {
260
Sirish Pande69295b82012-05-10 20:20:25 +0000261 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000262 if (unsigned Reg = State.AllocateReg(Hexagon::R0)) {
263 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
264 return false;
265 }
266 }
267
268 unsigned Offset = State.AllocateStack(4, 4);
269 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
270 return false;
271}
272
273static bool RetCC_Hexagon64(unsigned ValNo, MVT ValVT,
274 MVT LocVT, CCValAssign::LocInfo LocInfo,
275 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Sirish Pande69295b82012-05-10 20:20:25 +0000276 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000277 if (unsigned Reg = State.AllocateReg(Hexagon::D0)) {
278 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
279 return false;
280 }
281 }
282
283 unsigned Offset = State.AllocateStack(8, 8);
284 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
285 return false;
286}
287
288SDValue
289HexagonTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG)
290const {
291 return SDValue();
292}
293
294/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
295/// by "Src" to address "Dst" of size "Size". Alignment information is
296/// specified by the specific parameter attribute. The copy will be passed as
297/// a byval function parameter. Sometimes what we are copying is the end of a
298/// larger object, the part that does not fit in registers.
299static SDValue
300CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
301 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000302 SDLoc dl) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000303
304 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
305 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
306 /*isVolatile=*/false, /*AlwaysInline=*/false,
307 MachinePointerInfo(), MachinePointerInfo());
308}
309
310
311// LowerReturn - Lower ISD::RET. If a struct is larger than 8 bytes and is
312// passed by value, the function prototype is modified to return void and
313// the value is stored in memory pointed by a pointer passed by caller.
314SDValue
315HexagonTargetLowering::LowerReturn(SDValue Chain,
316 CallingConv::ID CallConv, bool isVarArg,
317 const SmallVectorImpl<ISD::OutputArg> &Outs,
318 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000319 SDLoc dl, SelectionDAG &DAG) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000320
321 // CCValAssign - represent the assignment of the return value to locations.
322 SmallVector<CCValAssign, 16> RVLocs;
323
324 // CCState - Info about the registers and stack slot.
325 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000326 getTargetMachine(), RVLocs, *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000327
328 // Analyze return values of ISD::RET
329 CCInfo.AnalyzeReturn(Outs, RetCC_Hexagon);
330
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000331 SDValue Flag;
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000332 SmallVector<SDValue, 4> RetOps(1, Chain);
333
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000334 // Copy the result values into the output registers.
335 for (unsigned i = 0; i != RVLocs.size(); ++i) {
336 CCValAssign &VA = RVLocs[i];
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000337
338 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), OutVals[i], Flag);
339
340 // Guarantee that all emitted copies are stuck together with flags.
341 Flag = Chain.getValue(1);
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000342 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000343 }
344
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000345 RetOps[0] = Chain; // Update chain.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000346
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000347 // Add the flag if we have it.
348 if (Flag.getNode())
349 RetOps.push_back(Flag);
350
Craig Topper48d114b2014-04-26 18:35:24 +0000351 return DAG.getNode(HexagonISD::RET_FLAG, dl, MVT::Other, RetOps);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000352}
353
354
355
356
357/// LowerCallResult - Lower the result values of an ISD::CALL into the
358/// appropriate copies out of appropriate physical registers. This assumes that
359/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
360/// being lowered. Returns a SDNode with the same number of values as the
361/// ISD::CALL.
362SDValue
363HexagonTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
364 CallingConv::ID CallConv, bool isVarArg,
365 const
366 SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000367 SDLoc dl, SelectionDAG &DAG,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000368 SmallVectorImpl<SDValue> &InVals,
369 const SmallVectorImpl<SDValue> &OutVals,
370 SDValue Callee) const {
371
372 // Assign locations to each value returned by this call.
373 SmallVector<CCValAssign, 16> RVLocs;
374
375 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000376 getTargetMachine(), RVLocs, *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000377
378 CCInfo.AnalyzeCallResult(Ins, RetCC_Hexagon);
379
380 // Copy all of the result registers out of their specified physreg.
381 for (unsigned i = 0; i != RVLocs.size(); ++i) {
382 Chain = DAG.getCopyFromReg(Chain, dl,
383 RVLocs[i].getLocReg(),
384 RVLocs[i].getValVT(), InFlag).getValue(1);
385 InFlag = Chain.getValue(2);
386 InVals.push_back(Chain.getValue(0));
387 }
388
389 return Chain;
390}
391
392/// LowerCall - Functions arguments are copied from virtual regs to
393/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
394SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +0000395HexagonTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000396 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +0000397 SelectionDAG &DAG = CLI.DAG;
Craig Topperb94011f2013-07-14 04:42:23 +0000398 SDLoc &dl = CLI.DL;
399 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
400 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
401 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000402 SDValue Chain = CLI.Chain;
403 SDValue Callee = CLI.Callee;
404 bool &isTailCall = CLI.IsTailCall;
405 CallingConv::ID CallConv = CLI.CallConv;
406 bool isVarArg = CLI.IsVarArg;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000407
408 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
409
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000410 // Check for varargs.
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000411 int NumNamedVarArgParams = -1;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000412 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Callee))
413 {
Craig Topper062a2ba2014-04-25 05:30:21 +0000414 const Function* CalleeFn = nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000415 Callee = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, MVT::i32);
416 if ((CalleeFn = dyn_cast<Function>(GA->getGlobal())))
417 {
418 // If a function has zero args and is a vararg function, that's
419 // disallowed so it must be an undeclared function. Do not assume
420 // varargs if the callee is undefined.
421 if (CalleeFn->isVarArg() &&
422 CalleeFn->getFunctionType()->getNumParams() != 0) {
423 NumNamedVarArgParams = CalleeFn->getFunctionType()->getNumParams();
424 }
425 }
426 }
427
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000428 // Analyze operands of the call, assigning locations to each operand.
429 SmallVector<CCValAssign, 16> ArgLocs;
430 HexagonCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
431 getTargetMachine(), ArgLocs, *DAG.getContext(),
432 NumNamedVarArgParams);
433
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000434 if (NumNamedVarArgParams > 0)
435 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon_VarArg);
436 else
437 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon);
438
439
440 if(isTailCall) {
441 bool StructAttrFlag =
442 DAG.getMachineFunction().getFunction()->hasStructRetAttr();
443 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
444 isVarArg, IsStructRet,
445 StructAttrFlag,
446 Outs, OutVals, Ins, DAG);
447 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i){
448 CCValAssign &VA = ArgLocs[i];
449 if (VA.isMemLoc()) {
450 isTailCall = false;
451 break;
452 }
453 }
454 if (isTailCall) {
455 DEBUG(dbgs () << "Eligible for Tail Call\n");
456 } else {
457 DEBUG(dbgs () <<
458 "Argument must be passed on stack. Not eligible for Tail Call\n");
459 }
460 }
461 // Get a count of how many bytes are to be pushed on the stack.
462 unsigned NumBytes = CCInfo.getNextStackOffset();
463 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
464 SmallVector<SDValue, 8> MemOpChains;
465
Eric Christopherdbe1cb02014-06-27 00:13:52 +0000466 const HexagonRegisterInfo *QRI = static_cast<const HexagonRegisterInfo *>(
Eric Christopherfc6de422014-08-05 02:39:49 +0000467 DAG.getSubtarget().getRegisterInfo());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000468 SDValue StackPtr =
Eric Christopherdbe1cb02014-06-27 00:13:52 +0000469 DAG.getCopyFromReg(Chain, dl, QRI->getStackRegister(), getPointerTy());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000470
471 // Walk the register/memloc assignments, inserting copies/loads.
472 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
473 CCValAssign &VA = ArgLocs[i];
474 SDValue Arg = OutVals[i];
475 ISD::ArgFlagsTy Flags = Outs[i].Flags;
476
477 // Promote the value if needed.
478 switch (VA.getLocInfo()) {
479 default:
480 // Loc info must be one of Full, SExt, ZExt, or AExt.
Craig Toppere55c5562012-02-07 02:50:20 +0000481 llvm_unreachable("Unknown loc info!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000482 case CCValAssign::Full:
483 break;
484 case CCValAssign::SExt:
485 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
486 break;
487 case CCValAssign::ZExt:
488 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
489 break;
490 case CCValAssign::AExt:
491 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
492 break;
493 }
494
495 if (VA.isMemLoc()) {
496 unsigned LocMemOffset = VA.getLocMemOffset();
497 SDValue PtrOff = DAG.getConstant(LocMemOffset, StackPtr.getValueType());
498 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
499
500 if (Flags.isByVal()) {
501 // The argument is a struct passed by value. According to LLVM, "Arg"
502 // is is pointer.
503 MemOpChains.push_back(CreateCopyOfByValArgument(Arg, PtrOff, Chain,
504 Flags, DAG, dl));
505 } else {
506 // The argument is not passed by value. "Arg" is a buildin type. It is
507 // not a pointer.
508 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
509 MachinePointerInfo(),false, false,
510 0));
511 }
512 continue;
513 }
514
515 // Arguments that can be passed on register must be kept at RegsToPass
516 // vector.
517 if (VA.isRegLoc()) {
518 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
519 }
520 }
521
522 // Transform all store nodes into one single node because all store
523 // nodes are independent of each other.
524 if (!MemOpChains.empty()) {
Craig Topper48d114b2014-04-26 18:35:24 +0000525 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000526 }
527
528 if (!isTailCall)
529 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(NumBytes,
Andrew Trickad6d08a2013-05-29 22:03:55 +0000530 getPointerTy(), true),
531 dl);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000532
533 // Build a sequence of copy-to-reg nodes chained together with token
534 // chain and flag operands which copy the outgoing args into registers.
Benjamin Kramerbde91762012-06-02 10:20:22 +0000535 // The InFlag in necessary since all emitted instructions must be
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000536 // stuck together.
537 SDValue InFlag;
538 if (!isTailCall) {
539 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
540 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
541 RegsToPass[i].second, InFlag);
542 InFlag = Chain.getValue(1);
543 }
544 }
545
546 // For tail calls lower the arguments to the 'real' stack slot.
547 if (isTailCall) {
548 // Force all the incoming stack arguments to be loaded from the stack
549 // before any new outgoing arguments are stored to the stack, because the
550 // outgoing stack slots may alias the incoming argument stack slots, and
551 // the alias isn't otherwise explicit. This is slightly more conservative
552 // than necessary, because it means that each store effectively depends
553 // on every argument instead of just those arguments it would clobber.
554 //
Benjamin Kramerbde91762012-06-02 10:20:22 +0000555 // Do not flag preceding copytoreg stuff together with the following stuff.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000556 InFlag = SDValue();
557 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
558 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
559 RegsToPass[i].second, InFlag);
560 InFlag = Chain.getValue(1);
561 }
562 InFlag =SDValue();
563 }
564
565 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
566 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
567 // node so that legalize doesn't hack it.
568 if (flag_aligned_memcpy) {
569 const char *MemcpyName =
570 "__hexagon_memcpy_likely_aligned_min32bytes_mult8bytes";
571 Callee =
572 DAG.getTargetExternalSymbol(MemcpyName, getPointerTy());
573 flag_aligned_memcpy = false;
574 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
575 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy());
576 } else if (ExternalSymbolSDNode *S =
577 dyn_cast<ExternalSymbolSDNode>(Callee)) {
578 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
579 }
580
581 // Returns a chain & a flag for retval copy to use.
582 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
583 SmallVector<SDValue, 8> Ops;
584 Ops.push_back(Chain);
585 Ops.push_back(Callee);
586
587 // Add argument registers to the end of the list so that they are
588 // known live into the call.
589 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
590 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
591 RegsToPass[i].second.getValueType()));
592 }
593
594 if (InFlag.getNode()) {
595 Ops.push_back(InFlag);
596 }
597
598 if (isTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +0000599 return DAG.getNode(HexagonISD::TC_RETURN, dl, NodeTys, Ops);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000600
Craig Topper48d114b2014-04-26 18:35:24 +0000601 Chain = DAG.getNode(HexagonISD::CALL, dl, NodeTys, Ops);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000602 InFlag = Chain.getValue(1);
603
604 // Create the CALLSEQ_END node.
605 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +0000606 DAG.getIntPtrConstant(0, true), InFlag, dl);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000607 InFlag = Chain.getValue(1);
608
609 // Handle result values, copying them out of physregs into vregs that we
610 // return.
611 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
612 InVals, OutVals, Callee);
613}
614
615static bool getIndexedAddressParts(SDNode *Ptr, EVT VT,
616 bool isSEXTLoad, SDValue &Base,
617 SDValue &Offset, bool &isInc,
618 SelectionDAG &DAG) {
619 if (Ptr->getOpcode() != ISD::ADD)
620 return false;
621
622 if (VT == MVT::i64 || VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
623 isInc = (Ptr->getOpcode() == ISD::ADD);
624 Base = Ptr->getOperand(0);
625 Offset = Ptr->getOperand(1);
626 // Ensure that Offset is a constant.
627 return (isa<ConstantSDNode>(Offset));
628 }
629
630 return false;
631}
632
633// TODO: Put this function along with the other isS* functions in
634// HexagonISelDAGToDAG.cpp into a common file. Or better still, use the
Rafael Espindolab90c5f12012-11-21 16:56:33 +0000635// functions defined in HexagonOperands.td.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000636static bool Is_PostInc_S4_Offset(SDNode * S, int ShiftAmount) {
637 ConstantSDNode *N = cast<ConstantSDNode>(S);
638
639 // immS4 predicate - True if the immediate fits in a 4-bit sign extended.
640 // field.
641 int64_t v = (int64_t)N->getSExtValue();
642 int64_t m = 0;
643 if (ShiftAmount > 0) {
644 m = v % ShiftAmount;
645 v = v >> ShiftAmount;
646 }
647 return (v <= 7) && (v >= -8) && (m == 0);
648}
649
650/// getPostIndexedAddressParts - returns true by value, base pointer and
651/// offset pointer and addressing mode by reference if this node can be
652/// combined with a load / store to form a post-indexed load / store.
653bool HexagonTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
654 SDValue &Base,
655 SDValue &Offset,
656 ISD::MemIndexedMode &AM,
657 SelectionDAG &DAG) const
658{
659 EVT VT;
660 SDValue Ptr;
661 bool isSEXTLoad = false;
662
663 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
664 VT = LD->getMemoryVT();
665 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
666 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
667 VT = ST->getMemoryVT();
668 if (ST->getValue().getValueType() == MVT::i64 && ST->isTruncatingStore()) {
669 return false;
670 }
671 } else {
672 return false;
673 }
674
Chad Rosier64dc8aa2012-01-06 20:11:59 +0000675 bool isInc = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000676 bool isLegal = getIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
677 isInc, DAG);
678 // ShiftAmount = number of left-shifted bits in the Hexagon instruction.
679 int ShiftAmount = VT.getSizeInBits() / 16;
680 if (isLegal && Is_PostInc_S4_Offset(Offset.getNode(), ShiftAmount)) {
681 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
682 return true;
683 }
684
685 return false;
686}
687
688SDValue HexagonTargetLowering::LowerINLINEASM(SDValue Op,
689 SelectionDAG &DAG) const {
690 SDNode *Node = Op.getNode();
691 MachineFunction &MF = DAG.getMachineFunction();
692 HexagonMachineFunctionInfo *FuncInfo =
693 MF.getInfo<HexagonMachineFunctionInfo>();
694 switch (Node->getOpcode()) {
695 case ISD::INLINEASM: {
696 unsigned NumOps = Node->getNumOperands();
697 if (Node->getOperand(NumOps-1).getValueType() == MVT::Glue)
698 --NumOps; // Ignore the flag operand.
699
700 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
701 if (FuncInfo->hasClobberLR())
702 break;
703 unsigned Flags =
704 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
705 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
706 ++i; // Skip the ID value.
707
708 switch (InlineAsm::getKind(Flags)) {
709 default: llvm_unreachable("Bad flags!");
710 case InlineAsm::Kind_RegDef:
711 case InlineAsm::Kind_RegUse:
712 case InlineAsm::Kind_Imm:
713 case InlineAsm::Kind_Clobber:
714 case InlineAsm::Kind_Mem: {
715 for (; NumVals; --NumVals, ++i) {}
716 break;
717 }
718 case InlineAsm::Kind_RegDefEarlyClobber: {
719 for (; NumVals; --NumVals, ++i) {
720 unsigned Reg =
721 cast<RegisterSDNode>(Node->getOperand(i))->getReg();
722
723 // Check it to be lr
Eric Christopherdbe1cb02014-06-27 00:13:52 +0000724 const HexagonRegisterInfo *QRI =
725 static_cast<const HexagonRegisterInfo *>(
Eric Christopherfc6de422014-08-05 02:39:49 +0000726 DAG.getSubtarget().getRegisterInfo());
Eric Christopherdbe1cb02014-06-27 00:13:52 +0000727 if (Reg == QRI->getRARegister()) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000728 FuncInfo->setHasClobberLR(true);
729 break;
730 }
731 }
732 break;
733 }
734 }
735 }
736 }
737 } // Node->getOpcode
738 return Op;
739}
740
741
742//
743// Taken from the XCore backend.
744//
745SDValue HexagonTargetLowering::
746LowerBR_JT(SDValue Op, SelectionDAG &DAG) const
747{
748 SDValue Chain = Op.getOperand(0);
749 SDValue Table = Op.getOperand(1);
750 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000751 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000752 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
753 unsigned JTI = JT->getIndex();
754 MachineFunction &MF = DAG.getMachineFunction();
755 const MachineJumpTableInfo *MJTI = MF.getJumpTableInfo();
756 SDValue TargetJT = DAG.getTargetJumpTable(JT->getIndex(), MVT::i32);
757
758 // Mark all jump table targets as address taken.
759 const std::vector<MachineJumpTableEntry> &JTE = MJTI->getJumpTables();
760 const std::vector<MachineBasicBlock*> &JTBBs = JTE[JTI].MBBs;
761 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
762 MachineBasicBlock *MBB = JTBBs[i];
763 MBB->setHasAddressTaken();
764 // This line is needed to set the hasAddressTaken flag on the BasicBlock
765 // object.
766 BlockAddress::get(const_cast<BasicBlock *>(MBB->getBasicBlock()));
767 }
768
769 SDValue JumpTableBase = DAG.getNode(HexagonISD::WrapperJT, dl,
770 getPointerTy(), TargetJT);
771 SDValue ShiftIndex = DAG.getNode(ISD::SHL, dl, MVT::i32, Index,
772 DAG.getConstant(2, MVT::i32));
773 SDValue JTAddress = DAG.getNode(ISD::ADD, dl, MVT::i32, JumpTableBase,
774 ShiftIndex);
775 SDValue LoadTarget = DAG.getLoad(MVT::i32, dl, Chain, JTAddress,
776 MachinePointerInfo(), false, false, false,
777 0);
778 return DAG.getNode(HexagonISD::BR_JT, dl, MVT::Other, Chain, LoadTarget);
779}
780
781
782SDValue
783HexagonTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
784 SelectionDAG &DAG) const {
785 SDValue Chain = Op.getOperand(0);
786 SDValue Size = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000787 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000788
789 unsigned SPReg = getStackPointerRegisterToSaveRestore();
790
791 // Get a reference to the stack pointer.
792 SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, SPReg, MVT::i32);
793
794 // Subtract the dynamic size from the actual stack size to
795 // obtain the new stack size.
796 SDValue Sub = DAG.getNode(ISD::SUB, dl, MVT::i32, StackPointer, Size);
797
798 //
799 // For Hexagon, the outgoing memory arguments area should be on top of the
800 // alloca area on the stack i.e., the outgoing memory arguments should be
801 // at a lower address than the alloca area. Move the alloca area down the
802 // stack by adding back the space reserved for outgoing arguments to SP
803 // here.
804 //
805 // We do not know what the size of the outgoing args is at this point.
806 // So, we add a pseudo instruction ADJDYNALLOC that will adjust the
807 // stack pointer. We patch this instruction with the correct, known
808 // offset in emitPrologue().
809 //
810 // Use a placeholder immediate (zero) for now. This will be patched up
811 // by emitPrologue().
812 SDValue ArgAdjust = DAG.getNode(HexagonISD::ADJDYNALLOC, dl,
813 MVT::i32,
814 Sub,
815 DAG.getConstant(0, MVT::i32));
816
817 // The Sub result contains the new stack start address, so it
818 // must be placed in the stack pointer register.
Eric Christopherdbe1cb02014-06-27 00:13:52 +0000819 const HexagonRegisterInfo *QRI = static_cast<const HexagonRegisterInfo *>(
Eric Christopherfc6de422014-08-05 02:39:49 +0000820 DAG.getSubtarget().getRegisterInfo());
Eric Christopherdbe1cb02014-06-27 00:13:52 +0000821 SDValue CopyChain = DAG.getCopyToReg(Chain, dl, QRI->getStackRegister(), Sub);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000822
823 SDValue Ops[2] = { ArgAdjust, CopyChain };
Craig Topper64941d92014-04-27 19:20:57 +0000824 return DAG.getMergeValues(Ops, dl);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000825}
826
827SDValue
828HexagonTargetLowering::LowerFormalArguments(SDValue Chain,
829 CallingConv::ID CallConv,
830 bool isVarArg,
831 const
832 SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000833 SDLoc dl, SelectionDAG &DAG,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000834 SmallVectorImpl<SDValue> &InVals)
835const {
836
837 MachineFunction &MF = DAG.getMachineFunction();
838 MachineFrameInfo *MFI = MF.getFrameInfo();
839 MachineRegisterInfo &RegInfo = MF.getRegInfo();
840 HexagonMachineFunctionInfo *FuncInfo =
841 MF.getInfo<HexagonMachineFunctionInfo>();
842
843
844 // Assign locations to all of the incoming arguments.
845 SmallVector<CCValAssign, 16> ArgLocs;
846 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000847 getTargetMachine(), ArgLocs, *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000848
849 CCInfo.AnalyzeFormalArguments(Ins, CC_Hexagon);
850
851 // For LLVM, in the case when returning a struct by value (>8byte),
852 // the first argument is a pointer that points to the location on caller's
853 // stack where the return value will be stored. For Hexagon, the location on
854 // caller's stack is passed only when the struct size is smaller than (and
855 // equal to) 8 bytes. If not, no address will be passed into callee and
856 // callee return the result direclty through R0/R1.
857
858 SmallVector<SDValue, 4> MemOps;
859
860 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
861 CCValAssign &VA = ArgLocs[i];
862 ISD::ArgFlagsTy Flags = Ins[i].Flags;
863 unsigned ObjSize;
864 unsigned StackLocation;
865 int FI;
866
867 if ( (VA.isRegLoc() && !Flags.isByVal())
868 || (VA.isRegLoc() && Flags.isByVal() && Flags.getByValSize() > 8)) {
869 // Arguments passed in registers
870 // 1. int, long long, ptr args that get allocated in register.
871 // 2. Large struct that gets an register to put its address in.
872 EVT RegVT = VA.getLocVT();
Sirish Pande69295b82012-05-10 20:20:25 +0000873 if (RegVT == MVT::i8 || RegVT == MVT::i16 ||
874 RegVT == MVT::i32 || RegVT == MVT::f32) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000875 unsigned VReg =
Craig Topperc7242e02012-04-20 07:30:17 +0000876 RegInfo.createVirtualRegister(&Hexagon::IntRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000877 RegInfo.addLiveIn(VA.getLocReg(), VReg);
878 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
Chandler Carruthb415bf982012-04-18 21:31:19 +0000879 } else if (RegVT == MVT::i64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000880 unsigned VReg =
Craig Topperc7242e02012-04-20 07:30:17 +0000881 RegInfo.createVirtualRegister(&Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000882 RegInfo.addLiveIn(VA.getLocReg(), VReg);
883 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
884 } else {
885 assert (0);
886 }
887 } else if (VA.isRegLoc() && Flags.isByVal() && Flags.getByValSize() <= 8) {
888 assert (0 && "ByValSize must be bigger than 8 bytes");
889 } else {
890 // Sanity check.
891 assert(VA.isMemLoc());
892
893 if (Flags.isByVal()) {
894 // If it's a byval parameter, then we need to compute the
895 // "real" size, not the size of the pointer.
896 ObjSize = Flags.getByValSize();
897 } else {
898 ObjSize = VA.getLocVT().getStoreSizeInBits() >> 3;
899 }
900
901 StackLocation = HEXAGON_LRFP_SIZE + VA.getLocMemOffset();
902 // Create the frame index object for this incoming parameter...
903 FI = MFI->CreateFixedObject(ObjSize, StackLocation, true);
904
905 // Create the SelectionDAG nodes cordl, responding to a load
906 // from this parameter.
907 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
908
909 if (Flags.isByVal()) {
910 // If it's a pass-by-value aggregate, then do not dereference the stack
911 // location. Instead, we should generate a reference to the stack
912 // location.
913 InVals.push_back(FIN);
914 } else {
915 InVals.push_back(DAG.getLoad(VA.getLocVT(), dl, Chain, FIN,
916 MachinePointerInfo(), false, false,
917 false, 0));
918 }
919 }
920 }
921
922 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +0000923 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000924
925 if (isVarArg) {
926 // This will point to the next argument passed via stack.
927 int FrameIndex = MFI->CreateFixedObject(Hexagon_PointerSize,
928 HEXAGON_LRFP_SIZE +
929 CCInfo.getNextStackOffset(),
930 true);
931 FuncInfo->setVarArgsFrameIndex(FrameIndex);
932 }
933
934 return Chain;
935}
936
937SDValue
938HexagonTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
939 // VASTART stores the address of the VarArgsFrameIndex slot into the
940 // memory location argument.
941 MachineFunction &MF = DAG.getMachineFunction();
942 HexagonMachineFunctionInfo *QFI = MF.getInfo<HexagonMachineFunctionInfo>();
943 SDValue Addr = DAG.getFrameIndex(QFI->getVarArgsFrameIndex(), MVT::i32);
944 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000945 return DAG.getStore(Op.getOperand(0), SDLoc(Op), Addr,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000946 Op.getOperand(1), MachinePointerInfo(SV), false,
947 false, 0);
948}
949
950SDValue
Sirish Pande69295b82012-05-10 20:20:25 +0000951HexagonTargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
952 EVT ValTy = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000953 SDLoc dl(Op);
Sirish Pande69295b82012-05-10 20:20:25 +0000954 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
955 SDValue Res;
956 if (CP->isMachineConstantPoolEntry())
957 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), ValTy,
958 CP->getAlignment());
959 else
960 Res = DAG.getTargetConstantPool(CP->getConstVal(), ValTy,
961 CP->getAlignment());
962 return DAG.getNode(HexagonISD::CONST32, dl, ValTy, Res);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000963}
964
965SDValue
966HexagonTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const {
Eric Christopherfc6de422014-08-05 02:39:49 +0000967 const TargetRegisterInfo *TRI = DAG.getSubtarget().getRegisterInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000968 MachineFunction &MF = DAG.getMachineFunction();
969 MachineFrameInfo *MFI = MF.getFrameInfo();
970 MFI->setReturnAddressIsTaken(true);
971
Bill Wendling908bf812014-01-06 00:43:20 +0000972 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +0000973 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +0000974
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000975 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000976 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000977 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
978 if (Depth) {
979 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
980 SDValue Offset = DAG.getConstant(4, MVT::i32);
981 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
982 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
983 MachinePointerInfo(), false, false, false, 0);
984 }
985
986 // Return LR, which contains the return address. Mark it an implicit live-in.
987 unsigned Reg = MF.addLiveIn(TRI->getRARegister(), getRegClassFor(MVT::i32));
988 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
989}
990
991SDValue
992HexagonTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Eric Christopherd9134482014-08-04 21:25:23 +0000993 const HexagonRegisterInfo *TRI = static_cast<const HexagonRegisterInfo *>(
Eric Christopherfc6de422014-08-05 02:39:49 +0000994 DAG.getSubtarget().getRegisterInfo());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000995 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
996 MFI->setFrameAddressIsTaken(true);
997
998 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000999 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001000 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1001 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
1002 TRI->getFrameRegister(), VT);
1003 while (Depth--)
1004 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
1005 MachinePointerInfo(),
1006 false, false, false, 0);
1007 return FrameAddr;
1008}
1009
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001010SDValue HexagonTargetLowering::LowerATOMIC_FENCE(SDValue Op,
1011 SelectionDAG& DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001012 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001013 return DAG.getNode(HexagonISD::BARRIER, dl, MVT::Other, Op.getOperand(0));
1014}
1015
1016
1017SDValue HexagonTargetLowering::LowerGLOBALADDRESS(SDValue Op,
1018 SelectionDAG &DAG) const {
1019 SDValue Result;
1020 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1021 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001022 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001023 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
1024
Dmitri Gribenkof24e57f2013-01-14 22:18:18 +00001025 const HexagonTargetObjectFile &TLOF =
1026 static_cast<const HexagonTargetObjectFile &>(getObjFileLowering());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001027 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
1028 return DAG.getNode(HexagonISD::CONST32_GP, dl, getPointerTy(), Result);
1029 }
1030
1031 return DAG.getNode(HexagonISD::CONST32, dl, getPointerTy(), Result);
1032}
1033
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001034SDValue
1035HexagonTargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
1036 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
1037 SDValue BA_SD = DAG.getTargetBlockAddress(BA, MVT::i32);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001038 SDLoc dl(Op);
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001039 return DAG.getNode(HexagonISD::CONST32_GP, dl, getPointerTy(), BA_SD);
1040}
1041
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001042//===----------------------------------------------------------------------===//
1043// TargetLowering Implementation
1044//===----------------------------------------------------------------------===//
1045
Eric Christopherdbe1cb02014-06-27 00:13:52 +00001046HexagonTargetLowering::HexagonTargetLowering(const TargetMachine &targetmachine)
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001047 : TargetLowering(targetmachine, new HexagonTargetObjectFile()),
1048 TM(targetmachine) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001049
Eric Christopherdbe1cb02014-06-27 00:13:52 +00001050 const HexagonSubtarget &Subtarget = TM.getSubtarget<HexagonSubtarget>();
Sirish Pande69295b82012-05-10 20:20:25 +00001051
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001052 // Set up the register classes.
1053 addRegisterClass(MVT::i32, &Hexagon::IntRegsRegClass);
1054 addRegisterClass(MVT::i64, &Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001055
Eric Christopherdbe1cb02014-06-27 00:13:52 +00001056 if (Subtarget.hasV5TOps()) {
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001057 addRegisterClass(MVT::f32, &Hexagon::IntRegsRegClass);
1058 addRegisterClass(MVT::f64, &Hexagon::DoubleRegsRegClass);
1059 }
Sirish Pande69295b82012-05-10 20:20:25 +00001060
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001061 addRegisterClass(MVT::i1, &Hexagon::PredRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001062
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001063 computeRegisterProperties();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001064
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001065 // Align loop entry
1066 setPrefLoopAlignment(4);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001067
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001068 // Limits for inline expansion of memcpy/memmove
1069 MaxStoresPerMemcpy = 6;
1070 MaxStoresPerMemmove = 6;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001071
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001072 //
1073 // Library calls for unsupported operations
1074 //
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001075
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001076 setLibcallName(RTLIB::SINTTOFP_I128_F64, "__hexagon_floattidf");
1077 setLibcallName(RTLIB::SINTTOFP_I128_F32, "__hexagon_floattisf");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001078
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001079 setLibcallName(RTLIB::FPTOUINT_F32_I128, "__hexagon_fixunssfti");
1080 setLibcallName(RTLIB::FPTOUINT_F64_I128, "__hexagon_fixunsdfti");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001081
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001082 setLibcallName(RTLIB::FPTOSINT_F32_I128, "__hexagon_fixsfti");
1083 setLibcallName(RTLIB::FPTOSINT_F64_I128, "__hexagon_fixdfti");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001084
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001085 setLibcallName(RTLIB::SDIV_I32, "__hexagon_divsi3");
1086 setOperationAction(ISD::SDIV, MVT::i32, Expand);
1087 setLibcallName(RTLIB::SREM_I32, "__hexagon_umodsi3");
1088 setOperationAction(ISD::SREM, MVT::i32, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001089
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001090 setLibcallName(RTLIB::SDIV_I64, "__hexagon_divdi3");
1091 setOperationAction(ISD::SDIV, MVT::i64, Expand);
1092 setLibcallName(RTLIB::SREM_I64, "__hexagon_moddi3");
1093 setOperationAction(ISD::SREM, MVT::i64, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001094
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001095 setLibcallName(RTLIB::UDIV_I32, "__hexagon_udivsi3");
1096 setOperationAction(ISD::UDIV, MVT::i32, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001097
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001098 setLibcallName(RTLIB::UDIV_I64, "__hexagon_udivdi3");
1099 setOperationAction(ISD::UDIV, MVT::i64, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001100
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001101 setLibcallName(RTLIB::UREM_I32, "__hexagon_umodsi3");
1102 setOperationAction(ISD::UREM, MVT::i32, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001103
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001104 setLibcallName(RTLIB::UREM_I64, "__hexagon_umoddi3");
1105 setOperationAction(ISD::UREM, MVT::i64, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001106
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001107 setLibcallName(RTLIB::DIV_F32, "__hexagon_divsf3");
1108 setOperationAction(ISD::FDIV, MVT::f32, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001109
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001110 setLibcallName(RTLIB::DIV_F64, "__hexagon_divdf3");
1111 setOperationAction(ISD::FDIV, MVT::f64, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001112
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001113 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
1114 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
1115 setOperationAction(ISD::FSIN, MVT::f32, Expand);
1116 setOperationAction(ISD::FSIN, MVT::f64, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001117
Eric Christopherdbe1cb02014-06-27 00:13:52 +00001118 if (Subtarget.hasV5TOps()) {
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001119 // Hexagon V5 Support.
1120 setOperationAction(ISD::FADD, MVT::f32, Legal);
1121 setOperationAction(ISD::FADD, MVT::f64, Legal);
1122 setOperationAction(ISD::FP_EXTEND, MVT::f32, Legal);
1123 setCondCodeAction(ISD::SETOEQ, MVT::f32, Legal);
1124 setCondCodeAction(ISD::SETOEQ, MVT::f64, Legal);
1125 setCondCodeAction(ISD::SETUEQ, MVT::f32, Legal);
1126 setCondCodeAction(ISD::SETUEQ, MVT::f64, Legal);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001127
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001128 setCondCodeAction(ISD::SETOGE, MVT::f32, Legal);
1129 setCondCodeAction(ISD::SETOGE, MVT::f64, Legal);
1130 setCondCodeAction(ISD::SETUGE, MVT::f32, Legal);
1131 setCondCodeAction(ISD::SETUGE, MVT::f64, Legal);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001132
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001133 setCondCodeAction(ISD::SETOGT, MVT::f32, Legal);
1134 setCondCodeAction(ISD::SETOGT, MVT::f64, Legal);
1135 setCondCodeAction(ISD::SETUGT, MVT::f32, Legal);
1136 setCondCodeAction(ISD::SETUGT, MVT::f64, Legal);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001137
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001138 setCondCodeAction(ISD::SETOLE, MVT::f32, Legal);
1139 setCondCodeAction(ISD::SETOLE, MVT::f64, Legal);
1140 setCondCodeAction(ISD::SETOLT, MVT::f32, Legal);
1141 setCondCodeAction(ISD::SETOLT, MVT::f64, Legal);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001142
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001143 setOperationAction(ISD::ConstantFP, MVT::f32, Legal);
1144 setOperationAction(ISD::ConstantFP, MVT::f64, Legal);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001145
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001146 setOperationAction(ISD::FP_TO_UINT, MVT::i1, Promote);
1147 setOperationAction(ISD::FP_TO_SINT, MVT::i1, Promote);
1148 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Promote);
1149 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Promote);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001150
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001151 setOperationAction(ISD::FP_TO_UINT, MVT::i8, Promote);
1152 setOperationAction(ISD::FP_TO_SINT, MVT::i8, Promote);
1153 setOperationAction(ISD::UINT_TO_FP, MVT::i8, Promote);
1154 setOperationAction(ISD::SINT_TO_FP, MVT::i8, Promote);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001155
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001156 setOperationAction(ISD::FP_TO_UINT, MVT::i16, Promote);
1157 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
1158 setOperationAction(ISD::UINT_TO_FP, MVT::i16, Promote);
1159 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001160
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001161 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Legal);
1162 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Legal);
1163 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Legal);
1164 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Legal);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001165
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001166 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Legal);
1167 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Legal);
1168 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Legal);
1169 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Legal);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001170
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001171 setOperationAction(ISD::FABS, MVT::f32, Legal);
1172 setOperationAction(ISD::FABS, MVT::f64, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001173
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001174 setOperationAction(ISD::FNEG, MVT::f32, Legal);
1175 setOperationAction(ISD::FNEG, MVT::f64, Expand);
1176 } else {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001177
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001178 // Expand fp<->uint.
1179 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Expand);
1180 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001181
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001182 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
1183 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001184
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001185 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__hexagon_floatdisf");
1186 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__hexagon_floatundisf");
Sirish Pande69295b82012-05-10 20:20:25 +00001187
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001188 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__hexagon_floatunsisf");
1189 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__hexagon_floatsisf");
Sirish Pande69295b82012-05-10 20:20:25 +00001190
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001191 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__hexagon_floatdidf");
1192 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__hexagon_floatundidf");
Sirish Pande69295b82012-05-10 20:20:25 +00001193
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001194 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__hexagon_floatunsidf");
1195 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__hexagon_floatsidf");
Sirish Pande69295b82012-05-10 20:20:25 +00001196
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001197 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__hexagon_fixunssfsi");
1198 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__hexagon_fixunssfdi");
Sirish Pande69295b82012-05-10 20:20:25 +00001199
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001200 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__hexagon_fixdfdi");
1201 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__hexagon_fixsfdi");
Sirish Pande69295b82012-05-10 20:20:25 +00001202
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001203 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__hexagon_fixunsdfsi");
1204 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__hexagon_fixunsdfdi");
Sirish Pande69295b82012-05-10 20:20:25 +00001205
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001206 setLibcallName(RTLIB::ADD_F64, "__hexagon_adddf3");
1207 setOperationAction(ISD::FADD, MVT::f64, Expand);
Sirish Pande69295b82012-05-10 20:20:25 +00001208
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001209 setLibcallName(RTLIB::ADD_F32, "__hexagon_addsf3");
1210 setOperationAction(ISD::FADD, MVT::f32, Expand);
Sirish Pande69295b82012-05-10 20:20:25 +00001211
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001212 setLibcallName(RTLIB::FPEXT_F32_F64, "__hexagon_extendsfdf2");
1213 setOperationAction(ISD::FP_EXTEND, MVT::f32, Expand);
Sirish Pande69295b82012-05-10 20:20:25 +00001214
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001215 setLibcallName(RTLIB::OEQ_F32, "__hexagon_eqsf2");
1216 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
Sirish Pande69295b82012-05-10 20:20:25 +00001217
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001218 setLibcallName(RTLIB::OEQ_F64, "__hexagon_eqdf2");
1219 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
Sirish Pande69295b82012-05-10 20:20:25 +00001220
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001221 setLibcallName(RTLIB::OGE_F32, "__hexagon_gesf2");
1222 setCondCodeAction(ISD::SETOGE, MVT::f32, Expand);
Sirish Pande69295b82012-05-10 20:20:25 +00001223
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001224 setLibcallName(RTLIB::OGE_F64, "__hexagon_gedf2");
1225 setCondCodeAction(ISD::SETOGE, MVT::f64, Expand);
Sirish Pande69295b82012-05-10 20:20:25 +00001226
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001227 setLibcallName(RTLIB::OGT_F32, "__hexagon_gtsf2");
1228 setCondCodeAction(ISD::SETOGT, MVT::f32, Expand);
Sirish Pande69295b82012-05-10 20:20:25 +00001229
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001230 setLibcallName(RTLIB::OGT_F64, "__hexagon_gtdf2");
1231 setCondCodeAction(ISD::SETOGT, MVT::f64, Expand);
Sirish Pande69295b82012-05-10 20:20:25 +00001232
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001233 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__hexagon_fixdfsi");
1234 setOperationAction(ISD::FP_TO_SINT, MVT::f64, Expand);
Sirish Pande69295b82012-05-10 20:20:25 +00001235
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001236 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__hexagon_fixsfsi");
1237 setOperationAction(ISD::FP_TO_SINT, MVT::f32, Expand);
Sirish Pande69295b82012-05-10 20:20:25 +00001238
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001239 setLibcallName(RTLIB::OLE_F64, "__hexagon_ledf2");
1240 setCondCodeAction(ISD::SETOLE, MVT::f64, Expand);
Sirish Pande69295b82012-05-10 20:20:25 +00001241
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001242 setLibcallName(RTLIB::OLE_F32, "__hexagon_lesf2");
1243 setCondCodeAction(ISD::SETOLE, MVT::f32, Expand);
Sirish Pande69295b82012-05-10 20:20:25 +00001244
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001245 setLibcallName(RTLIB::OLT_F64, "__hexagon_ltdf2");
1246 setCondCodeAction(ISD::SETOLT, MVT::f64, Expand);
Sirish Pande69295b82012-05-10 20:20:25 +00001247
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001248 setLibcallName(RTLIB::OLT_F32, "__hexagon_ltsf2");
1249 setCondCodeAction(ISD::SETOLT, MVT::f32, Expand);
Sirish Pande69295b82012-05-10 20:20:25 +00001250
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001251 setLibcallName(RTLIB::MUL_F64, "__hexagon_muldf3");
1252 setOperationAction(ISD::FMUL, MVT::f64, Expand);
Sirish Pande69295b82012-05-10 20:20:25 +00001253
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001254 setLibcallName(RTLIB::MUL_F32, "__hexagon_mulsf3");
1255 setOperationAction(ISD::MUL, MVT::f32, Expand);
Sirish Pande69295b82012-05-10 20:20:25 +00001256
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001257 setLibcallName(RTLIB::UNE_F64, "__hexagon_nedf2");
1258 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
Sirish Pande69295b82012-05-10 20:20:25 +00001259
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001260 setLibcallName(RTLIB::UNE_F32, "__hexagon_nesf2");
Sirish Pande69295b82012-05-10 20:20:25 +00001261
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001262 setLibcallName(RTLIB::SUB_F64, "__hexagon_subdf3");
1263 setOperationAction(ISD::SUB, MVT::f64, Expand);
Sirish Pande69295b82012-05-10 20:20:25 +00001264
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001265 setLibcallName(RTLIB::SUB_F32, "__hexagon_subsf3");
1266 setOperationAction(ISD::SUB, MVT::f32, Expand);
Sirish Pande69295b82012-05-10 20:20:25 +00001267
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001268 setLibcallName(RTLIB::FPROUND_F64_F32, "__hexagon_truncdfsf2");
1269 setOperationAction(ISD::FP_ROUND, MVT::f64, Expand);
Sirish Pande69295b82012-05-10 20:20:25 +00001270
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001271 setLibcallName(RTLIB::UO_F64, "__hexagon_unorddf2");
1272 setCondCodeAction(ISD::SETUO, MVT::f64, Expand);
Sirish Pande69295b82012-05-10 20:20:25 +00001273
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001274 setLibcallName(RTLIB::O_F64, "__hexagon_unorddf2");
1275 setCondCodeAction(ISD::SETO, MVT::f64, Expand);
Sirish Pande69295b82012-05-10 20:20:25 +00001276
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001277 setLibcallName(RTLIB::O_F32, "__hexagon_unordsf2");
1278 setCondCodeAction(ISD::SETO, MVT::f32, Expand);
Sirish Pande69295b82012-05-10 20:20:25 +00001279
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001280 setLibcallName(RTLIB::UO_F32, "__hexagon_unordsf2");
1281 setCondCodeAction(ISD::SETUO, MVT::f32, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001282
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001283 setOperationAction(ISD::FABS, MVT::f32, Expand);
1284 setOperationAction(ISD::FABS, MVT::f64, Expand);
1285 setOperationAction(ISD::FNEG, MVT::f32, Expand);
1286 setOperationAction(ISD::FNEG, MVT::f64, Expand);
1287 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001288
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001289 setLibcallName(RTLIB::SREM_I32, "__hexagon_modsi3");
1290 setOperationAction(ISD::SREM, MVT::i32, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001291
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001292 setIndexedLoadAction(ISD::POST_INC, MVT::i8, Legal);
1293 setIndexedLoadAction(ISD::POST_INC, MVT::i16, Legal);
1294 setIndexedLoadAction(ISD::POST_INC, MVT::i32, Legal);
1295 setIndexedLoadAction(ISD::POST_INC, MVT::i64, Legal);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001296
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001297 setIndexedStoreAction(ISD::POST_INC, MVT::i8, Legal);
1298 setIndexedStoreAction(ISD::POST_INC, MVT::i16, Legal);
1299 setIndexedStoreAction(ISD::POST_INC, MVT::i32, Legal);
1300 setIndexedStoreAction(ISD::POST_INC, MVT::i64, Legal);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001301
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001302 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001303
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001304 // Turn FP extload into load/fextend.
1305 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
1306 // Hexagon has a i1 sign extending load.
1307 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Expand);
1308 // Turn FP truncstore into trunc + store.
1309 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001310
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001311 // Custom legalize GlobalAddress nodes into CONST32.
1312 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
1313 setOperationAction(ISD::GlobalAddress, MVT::i8, Custom);
1314 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
1315 // Truncate action?
1316 setOperationAction(ISD::TRUNCATE, MVT::i64, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001317
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001318 // Hexagon doesn't have sext_inreg, replace them with shl/sra.
1319 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001320
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001321 // Hexagon has no REM or DIVREM operations.
1322 setOperationAction(ISD::UREM, MVT::i32, Expand);
1323 setOperationAction(ISD::SREM, MVT::i32, Expand);
1324 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
1325 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
1326 setOperationAction(ISD::SREM, MVT::i64, Expand);
1327 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
1328 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001329
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001330 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
Sirish Pande69295b82012-05-10 20:20:25 +00001331
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001332 // Lower SELECT_CC to SETCC and SELECT.
1333 setOperationAction(ISD::SELECT_CC, MVT::i1, Expand);
1334 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
1335 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
Sirish Pande69295b82012-05-10 20:20:25 +00001336
Eric Christopherdbe1cb02014-06-27 00:13:52 +00001337 if (Subtarget.hasV5TOps()) {
Sirish Pande69295b82012-05-10 20:20:25 +00001338
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001339 // We need to make the operation type of SELECT node to be Custom,
1340 // such that we don't go into the infinite loop of
1341 // select -> setcc -> select_cc -> select loop.
1342 setOperationAction(ISD::SELECT, MVT::f32, Custom);
1343 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Sirish Pande69295b82012-05-10 20:20:25 +00001344
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001345 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
1346 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Sirish Pande69295b82012-05-10 20:20:25 +00001347
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001348 } else {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001349
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001350 // Hexagon has no select or setcc: expand to SELECT_CC.
1351 setOperationAction(ISD::SELECT, MVT::f32, Expand);
1352 setOperationAction(ISD::SELECT, MVT::f64, Expand);
1353 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001354
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001355 if (EmitJumpTables) {
1356 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
1357 } else {
1358 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
1359 }
1360 // Increase jump tables cutover to 5, was 4.
1361 setMinimumJumpTableEntries(5);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001362
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001363 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
1364 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
1365 setOperationAction(ISD::BR_CC, MVT::i1, Expand);
1366 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
1367 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001368
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001369 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Jyotsna Verma0eeea142013-03-05 19:04:47 +00001370
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001371 setOperationAction(ISD::FSIN, MVT::f64, Expand);
1372 setOperationAction(ISD::FCOS, MVT::f64, Expand);
1373 setOperationAction(ISD::FREM, MVT::f64, Expand);
1374 setOperationAction(ISD::FSIN, MVT::f32, Expand);
1375 setOperationAction(ISD::FCOS, MVT::f32, Expand);
1376 setOperationAction(ISD::FREM, MVT::f32, Expand);
1377 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
1378 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Jyotsna Verma0eeea142013-03-05 19:04:47 +00001379
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001380 // In V4, we have double word add/sub with carry. The problem with
1381 // modelling this instruction is that it produces 2 results - Rdd and Px.
1382 // To model update of Px, we will have to use Defs[p0..p3] which will
1383 // cause any predicate live range to spill. So, we pretend we dont't
1384 // have these instructions.
1385 setOperationAction(ISD::ADDE, MVT::i8, Expand);
1386 setOperationAction(ISD::ADDE, MVT::i16, Expand);
1387 setOperationAction(ISD::ADDE, MVT::i32, Expand);
1388 setOperationAction(ISD::ADDE, MVT::i64, Expand);
1389 setOperationAction(ISD::SUBE, MVT::i8, Expand);
1390 setOperationAction(ISD::SUBE, MVT::i16, Expand);
1391 setOperationAction(ISD::SUBE, MVT::i32, Expand);
1392 setOperationAction(ISD::SUBE, MVT::i64, Expand);
1393 setOperationAction(ISD::ADDC, MVT::i8, Expand);
1394 setOperationAction(ISD::ADDC, MVT::i16, Expand);
1395 setOperationAction(ISD::ADDC, MVT::i32, Expand);
1396 setOperationAction(ISD::ADDC, MVT::i64, Expand);
1397 setOperationAction(ISD::SUBC, MVT::i8, Expand);
1398 setOperationAction(ISD::SUBC, MVT::i16, Expand);
1399 setOperationAction(ISD::SUBC, MVT::i32, Expand);
1400 setOperationAction(ISD::SUBC, MVT::i64, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001401
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001402 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
1403 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
1404 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
1405 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
1406 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
1407 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
1408 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
1409 setOperationAction(ISD::CTLZ, MVT::i64, Expand);
1410 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
1411 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
1412 setOperationAction(ISD::ROTL, MVT::i32, Expand);
1413 setOperationAction(ISD::ROTR, MVT::i32, Expand);
1414 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
1415 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
1416 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
1417 setOperationAction(ISD::FPOW, MVT::f64, Expand);
1418 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001419
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001420 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
1421 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
1422 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001423
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001424 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
1425 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001426
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001427 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
1428 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001429
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001430 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001431
Eric Christopherdbe1cb02014-06-27 00:13:52 +00001432 if (Subtarget.isSubtargetV2()) {
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001433 setExceptionPointerRegister(Hexagon::R20);
1434 setExceptionSelectorRegister(Hexagon::R21);
1435 } else {
1436 setExceptionPointerRegister(Hexagon::R0);
1437 setExceptionSelectorRegister(Hexagon::R1);
1438 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001439
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001440 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
1441 setOperationAction(ISD::VASTART, MVT::Other, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001442
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001443 // Use the default implementation.
1444 setOperationAction(ISD::VAARG, MVT::Other, Expand);
1445 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
1446 setOperationAction(ISD::VAEND, MVT::Other, Expand);
1447 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
1448 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001449
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001450 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
1451 setOperationAction(ISD::INLINEASM, MVT::Other, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001452
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001453 setMinFunctionAlignment(2);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001454
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001455 // Needed for DYNAMIC_STACKALLOC expansion.
Eric Christopherd9134482014-08-04 21:25:23 +00001456 const HexagonRegisterInfo *QRI = static_cast<const HexagonRegisterInfo *>(
1457 TM.getSubtargetImpl()->getRegisterInfo());
Eric Christopherdbe1cb02014-06-27 00:13:52 +00001458 setStackPointerRegisterToSaveRestore(QRI->getStackRegister());
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001459 setSchedulingPreference(Sched::VLIW);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001460}
1461
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001462const char*
1463HexagonTargetLowering::getTargetNodeName(unsigned Opcode) const {
1464 switch (Opcode) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001465 default: return nullptr;
Sirish Pande69295b82012-05-10 20:20:25 +00001466 case HexagonISD::CONST32: return "HexagonISD::CONST32";
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001467 case HexagonISD::CONST32_GP: return "HexagonISD::CONST32_GP";
1468 case HexagonISD::CONST32_Int_Real: return "HexagonISD::CONST32_Int_Real";
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001469 case HexagonISD::ADJDYNALLOC: return "HexagonISD::ADJDYNALLOC";
Sirish Pande69295b82012-05-10 20:20:25 +00001470 case HexagonISD::CMPICC: return "HexagonISD::CMPICC";
1471 case HexagonISD::CMPFCC: return "HexagonISD::CMPFCC";
1472 case HexagonISD::BRICC: return "HexagonISD::BRICC";
1473 case HexagonISD::BRFCC: return "HexagonISD::BRFCC";
1474 case HexagonISD::SELECT_ICC: return "HexagonISD::SELECT_ICC";
1475 case HexagonISD::SELECT_FCC: return "HexagonISD::SELECT_FCC";
1476 case HexagonISD::Hi: return "HexagonISD::Hi";
1477 case HexagonISD::Lo: return "HexagonISD::Lo";
1478 case HexagonISD::FTOI: return "HexagonISD::FTOI";
1479 case HexagonISD::ITOF: return "HexagonISD::ITOF";
1480 case HexagonISD::CALL: return "HexagonISD::CALL";
1481 case HexagonISD::RET_FLAG: return "HexagonISD::RET_FLAG";
1482 case HexagonISD::BR_JT: return "HexagonISD::BR_JT";
1483 case HexagonISD::TC_RETURN: return "HexagonISD::TC_RETURN";
Jyotsna Verma5ed51812013-05-01 21:37:34 +00001484 case HexagonISD::EH_RETURN: return "HexagonISD::EH_RETURN";
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001485 }
1486}
1487
1488bool
1489HexagonTargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
1490 EVT MTy1 = EVT::getEVT(Ty1);
1491 EVT MTy2 = EVT::getEVT(Ty2);
1492 if (!MTy1.isSimple() || !MTy2.isSimple()) {
1493 return false;
1494 }
1495 return ((MTy1.getSimpleVT() == MVT::i64) && (MTy2.getSimpleVT() == MVT::i32));
1496}
1497
1498bool HexagonTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
1499 if (!VT1.isSimple() || !VT2.isSimple()) {
1500 return false;
1501 }
1502 return ((VT1.getSimpleVT() == MVT::i64) && (VT2.getSimpleVT() == MVT::i32));
1503}
1504
Tim Northovera4415852013-08-06 09:12:35 +00001505bool
1506HexagonTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
1507 // Assuming the caller does not have either a signext or zeroext modifier, and
1508 // only one value is accepted, any reasonable truncation is allowed.
1509 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
1510 return false;
1511
1512 // FIXME: in principle up to 64-bit could be made safe, but it would be very
1513 // fragile at the moment: any support for multiple value returns would be
1514 // liable to disallow tail calls involving i64 -> iN truncation in many cases.
1515 return Ty1->getPrimitiveSizeInBits() <= 32;
1516}
1517
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001518SDValue
Jyotsna Verma5ed51812013-05-01 21:37:34 +00001519HexagonTargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
1520 SDValue Chain = Op.getOperand(0);
1521 SDValue Offset = Op.getOperand(1);
1522 SDValue Handler = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001523 SDLoc dl(Op);
Jyotsna Verma5ed51812013-05-01 21:37:34 +00001524
1525 // Mark function as containing a call to EH_RETURN.
1526 HexagonMachineFunctionInfo *FuncInfo =
1527 DAG.getMachineFunction().getInfo<HexagonMachineFunctionInfo>();
1528 FuncInfo->setHasEHReturn();
1529
1530 unsigned OffsetReg = Hexagon::R28;
1531
1532 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(),
1533 DAG.getRegister(Hexagon::R30, getPointerTy()),
1534 DAG.getIntPtrConstant(4));
1535 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
1536 false, false, 0);
1537 Chain = DAG.getCopyToReg(Chain, dl, OffsetReg, Offset);
1538
1539 // Not needed we already use it as explict input to EH_RETURN.
1540 // MF.getRegInfo().addLiveOut(OffsetReg);
1541
1542 return DAG.getNode(HexagonISD::EH_RETURN, dl, MVT::Other, Chain);
1543}
1544
1545SDValue
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001546HexagonTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
1547 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00001548 default: llvm_unreachable("Should not custom lower this!");
Sirish Pande69295b82012-05-10 20:20:25 +00001549 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Jyotsna Verma5ed51812013-05-01 21:37:34 +00001550 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001551 // Frame & Return address. Currently unimplemented.
Sirish Pande69295b82012-05-10 20:20:25 +00001552 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
1553 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001554 case ISD::GlobalTLSAddress:
Craig Toppere55c5562012-02-07 02:50:20 +00001555 llvm_unreachable("TLS not implemented for Hexagon.");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001556 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
1557 case ISD::GlobalAddress: return LowerGLOBALADDRESS(Op, DAG);
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001558 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001559 case ISD::VASTART: return LowerVASTART(Op, DAG);
1560 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
1561
1562 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Sirish Pande69295b82012-05-10 20:20:25 +00001563 case ISD::SELECT: return Op;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001564 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Sirish Pande69295b82012-05-10 20:20:25 +00001565 case ISD::INLINEASM: return LowerINLINEASM(Op, DAG);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001566
1567 }
1568}
1569
1570
1571
1572//===----------------------------------------------------------------------===//
1573// Hexagon Scheduler Hooks
1574//===----------------------------------------------------------------------===//
1575MachineBasicBlock *
1576HexagonTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
1577 MachineBasicBlock *BB)
1578const {
1579 switch (MI->getOpcode()) {
1580 case Hexagon::ADJDYNALLOC: {
1581 MachineFunction *MF = BB->getParent();
1582 HexagonMachineFunctionInfo *FuncInfo =
1583 MF->getInfo<HexagonMachineFunctionInfo>();
1584 FuncInfo->addAllocaAdjustInst(MI);
1585 return BB;
1586 }
Craig Toppere55c5562012-02-07 02:50:20 +00001587 default: llvm_unreachable("Unexpected instr type to insert");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001588 } // switch
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001589}
1590
1591//===----------------------------------------------------------------------===//
1592// Inline Assembly Support
1593//===----------------------------------------------------------------------===//
1594
1595std::pair<unsigned, const TargetRegisterClass*>
1596HexagonTargetLowering::getRegForInlineAsmConstraint(const
1597 std::string &Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +00001598 MVT VT) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001599 if (Constraint.size() == 1) {
1600 switch (Constraint[0]) {
1601 case 'r': // R0-R31
Chad Rosier295bd432013-06-22 18:37:38 +00001602 switch (VT.SimpleTy) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001603 default:
Craig Toppere55c5562012-02-07 02:50:20 +00001604 llvm_unreachable("getRegForInlineAsmConstraint Unhandled data type");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001605 case MVT::i32:
1606 case MVT::i16:
1607 case MVT::i8:
Sirish Pande69295b82012-05-10 20:20:25 +00001608 case MVT::f32:
Craig Topperc7242e02012-04-20 07:30:17 +00001609 return std::make_pair(0U, &Hexagon::IntRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001610 case MVT::i64:
Sirish Pande69295b82012-05-10 20:20:25 +00001611 case MVT::f64:
Craig Topperc7242e02012-04-20 07:30:17 +00001612 return std::make_pair(0U, &Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001613 }
1614 default:
Craig Toppere55c5562012-02-07 02:50:20 +00001615 llvm_unreachable("Unknown asm register class");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001616 }
1617 }
1618
1619 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1620}
1621
Sirish Pande69295b82012-05-10 20:20:25 +00001622/// isFPImmLegal - Returns true if the target can instruction select the
1623/// specified FP immediate natively. If false, the legalizer will
1624/// materialize the FP immediate as a load from a constant pool.
1625bool HexagonTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Eric Christopherdbe1cb02014-06-27 00:13:52 +00001626 return TM.getSubtarget<HexagonSubtarget>().hasV5TOps();
Sirish Pande69295b82012-05-10 20:20:25 +00001627}
1628
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001629/// isLegalAddressingMode - Return true if the addressing mode represented by
1630/// AM is legal for this target, for a load/store of the specified type.
1631bool HexagonTargetLowering::isLegalAddressingMode(const AddrMode &AM,
1632 Type *Ty) const {
1633 // Allows a signed-extended 11-bit immediate field.
1634 if (AM.BaseOffs <= -(1LL << 13) || AM.BaseOffs >= (1LL << 13)-1) {
1635 return false;
1636 }
1637
1638 // No global is ever allowed as a base.
1639 if (AM.BaseGV) {
1640 return false;
1641 }
1642
1643 int Scale = AM.Scale;
1644 if (Scale < 0) Scale = -Scale;
1645 switch (Scale) {
1646 case 0: // No scale reg, "r+i", "r", or just "i".
1647 break;
1648 default: // No scaled addressing mode.
1649 return false;
1650 }
1651 return true;
1652}
1653
1654/// isLegalICmpImmediate - Return true if the specified immediate is legal
1655/// icmp immediate, that is the target has icmp instructions which can compare
1656/// a register against the immediate without having to materialize the
1657/// immediate into a register.
1658bool HexagonTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
1659 return Imm >= -512 && Imm <= 511;
1660}
1661
1662/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1663/// for tail call optimization. Targets which want to do tail call
1664/// optimization should implement this function.
1665bool HexagonTargetLowering::IsEligibleForTailCallOptimization(
1666 SDValue Callee,
1667 CallingConv::ID CalleeCC,
1668 bool isVarArg,
1669 bool isCalleeStructRet,
1670 bool isCallerStructRet,
1671 const SmallVectorImpl<ISD::OutputArg> &Outs,
1672 const SmallVectorImpl<SDValue> &OutVals,
1673 const SmallVectorImpl<ISD::InputArg> &Ins,
1674 SelectionDAG& DAG) const {
1675 const Function *CallerF = DAG.getMachineFunction().getFunction();
1676 CallingConv::ID CallerCC = CallerF->getCallingConv();
1677 bool CCMatch = CallerCC == CalleeCC;
1678
1679 // ***************************************************************************
1680 // Look for obvious safe cases to perform tail call optimization that do not
1681 // require ABI changes.
1682 // ***************************************************************************
1683
1684 // If this is a tail call via a function pointer, then don't do it!
1685 if (!(dyn_cast<GlobalAddressSDNode>(Callee))
1686 && !(dyn_cast<ExternalSymbolSDNode>(Callee))) {
1687 return false;
1688 }
1689
1690 // Do not optimize if the calling conventions do not match.
1691 if (!CCMatch)
1692 return false;
1693
1694 // Do not tail call optimize vararg calls.
1695 if (isVarArg)
1696 return false;
1697
1698 // Also avoid tail call optimization if either caller or callee uses struct
1699 // return semantics.
1700 if (isCalleeStructRet || isCallerStructRet)
1701 return false;
1702
1703 // In addition to the cases above, we also disable Tail Call Optimization if
1704 // the calling convention code that at least one outgoing argument needs to
1705 // go on the stack. We cannot check that here because at this point that
1706 // information is not available.
1707 return true;
1708}