blob: 9055e7ee532e05dd428b88c6bc2464f78600fe9d [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,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +000046 cl::desc("Control jump table emission on Hexagon target"));
47
48static cl::opt<bool> EnableHexSDNodeSched("enable-hexagon-sdnode-sched",
49 cl::Hidden, cl::ZeroOrMore, cl::init(false),
50 cl::desc("Enable Hexagon SDNode scheduling"));
51
52static cl::opt<bool> EnableFastMath("ffast-math",
53 cl::Hidden, cl::ZeroOrMore, cl::init(false),
54 cl::desc("Enable Fast Math processing"));
55
56static cl::opt<int> MinimumJumpTables("minimum-jump-tables",
57 cl::Hidden, cl::ZeroOrMore, cl::init(5),
58 cl::desc("Set minimum jump tables"));
59
60static cl::opt<int> MaxStoresPerMemcpyCL("max-store-memcpy",
61 cl::Hidden, cl::ZeroOrMore, cl::init(6),
62 cl::desc("Max #stores to inline memcpy"));
63
64static cl::opt<int> MaxStoresPerMemcpyOptSizeCL("max-store-memcpy-Os",
65 cl::Hidden, cl::ZeroOrMore, cl::init(4),
66 cl::desc("Max #stores to inline memcpy"));
67
68static cl::opt<int> MaxStoresPerMemmoveCL("max-store-memmove",
69 cl::Hidden, cl::ZeroOrMore, cl::init(6),
70 cl::desc("Max #stores to inline memmove"));
71
72static cl::opt<int> MaxStoresPerMemmoveOptSizeCL("max-store-memmove-Os",
73 cl::Hidden, cl::ZeroOrMore, cl::init(4),
74 cl::desc("Max #stores to inline memmove"));
75
76static cl::opt<int> MaxStoresPerMemsetCL("max-store-memset",
77 cl::Hidden, cl::ZeroOrMore, cl::init(8),
78 cl::desc("Max #stores to inline memset"));
79
80static cl::opt<int> MaxStoresPerMemsetOptSizeCL("max-store-memset-Os",
81 cl::Hidden, cl::ZeroOrMore, cl::init(4),
82 cl::desc("Max #stores to inline memset"));
83
Tony Linthicum1213a7a2011-12-12 21:14:40 +000084
Benjamin Kramer602bb4a2013-10-27 11:16:09 +000085namespace {
86class HexagonCCState : public CCState {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +000087 unsigned NumNamedVarArgParams;
Benjamin Kramer602bb4a2013-10-27 11:16:09 +000088
89public:
90 HexagonCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
Eric Christopherb5217502014-08-06 18:45:26 +000091 SmallVectorImpl<CCValAssign> &locs, LLVMContext &C,
92 int NumNamedVarArgParams)
93 : CCState(CC, isVarArg, MF, locs, C),
Benjamin Kramer602bb4a2013-10-27 11:16:09 +000094 NumNamedVarArgParams(NumNamedVarArgParams) {}
95
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +000096 unsigned getNumNamedVarArgParams() const { return NumNamedVarArgParams; }
Benjamin Kramer602bb4a2013-10-27 11:16:09 +000097};
98}
Tony Linthicum1213a7a2011-12-12 21:14:40 +000099
100// Implement calling convention for Hexagon.
101static bool
102CC_Hexagon(unsigned ValNo, MVT ValVT,
103 MVT LocVT, CCValAssign::LocInfo LocInfo,
104 ISD::ArgFlagsTy ArgFlags, CCState &State);
105
106static bool
107CC_Hexagon32(unsigned ValNo, MVT ValVT,
108 MVT LocVT, CCValAssign::LocInfo LocInfo,
109 ISD::ArgFlagsTy ArgFlags, CCState &State);
110
111static bool
112CC_Hexagon64(unsigned ValNo, MVT ValVT,
113 MVT LocVT, CCValAssign::LocInfo LocInfo,
114 ISD::ArgFlagsTy ArgFlags, CCState &State);
115
116static bool
117RetCC_Hexagon(unsigned ValNo, MVT ValVT,
118 MVT LocVT, CCValAssign::LocInfo LocInfo,
119 ISD::ArgFlagsTy ArgFlags, CCState &State);
120
121static bool
122RetCC_Hexagon32(unsigned ValNo, MVT ValVT,
123 MVT LocVT, CCValAssign::LocInfo LocInfo,
124 ISD::ArgFlagsTy ArgFlags, CCState &State);
125
126static bool
127RetCC_Hexagon64(unsigned ValNo, MVT ValVT,
128 MVT LocVT, CCValAssign::LocInfo LocInfo,
129 ISD::ArgFlagsTy ArgFlags, CCState &State);
130
131static bool
132CC_Hexagon_VarArg (unsigned ValNo, MVT ValVT,
133 MVT LocVT, CCValAssign::LocInfo LocInfo,
134 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000135 HexagonCCState &HState = static_cast<HexagonCCState &>(State);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000136
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000137 if (ValNo < HState.getNumNamedVarArgParams()) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000138 // Deal with named arguments.
139 return CC_Hexagon(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State);
140 }
141
142 // Deal with un-named arguments.
143 unsigned ofst;
144 if (ArgFlags.isByVal()) {
145 // If pass-by-value, the size allocated on stack is decided
146 // by ArgFlags.getByValSize(), not by the size of LocVT.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000147 ofst = State.AllocateStack(ArgFlags.getByValSize(),
148 ArgFlags.getByValAlign());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000149 State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
150 return false;
151 }
Jyotsna Vermac7dcc2f2013-03-07 20:28:34 +0000152 if (LocVT == MVT::i1 || LocVT == MVT::i8 || LocVT == MVT::i16) {
153 LocVT = MVT::i32;
154 ValVT = MVT::i32;
155 if (ArgFlags.isSExt())
156 LocInfo = CCValAssign::SExt;
157 else if (ArgFlags.isZExt())
158 LocInfo = CCValAssign::ZExt;
159 else
160 LocInfo = CCValAssign::AExt;
161 }
Sirish Pande69295b82012-05-10 20:20:25 +0000162 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000163 ofst = State.AllocateStack(4, 4);
164 State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
165 return false;
166 }
Sirish Pande69295b82012-05-10 20:20:25 +0000167 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000168 ofst = State.AllocateStack(8, 8);
169 State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
170 return false;
171 }
Craig Toppere73658d2014-04-28 04:05:08 +0000172 llvm_unreachable(nullptr);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000173}
174
175
176static bool
177CC_Hexagon (unsigned ValNo, MVT ValVT,
178 MVT LocVT, CCValAssign::LocInfo LocInfo,
179 ISD::ArgFlagsTy ArgFlags, CCState &State) {
180
181 if (ArgFlags.isByVal()) {
182 // Passed on stack.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000183 unsigned Offset = State.AllocateStack(ArgFlags.getByValSize(),
184 ArgFlags.getByValAlign());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000185 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
186 return false;
187 }
188
189 if (LocVT == MVT::i1 || LocVT == MVT::i8 || LocVT == MVT::i16) {
190 LocVT = MVT::i32;
191 ValVT = MVT::i32;
192 if (ArgFlags.isSExt())
193 LocInfo = CCValAssign::SExt;
194 else if (ArgFlags.isZExt())
195 LocInfo = CCValAssign::ZExt;
196 else
197 LocInfo = CCValAssign::AExt;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000198 } else if (LocVT == MVT::v4i8 || LocVT == MVT::v2i16) {
199 LocVT = MVT::i32;
200 LocInfo = CCValAssign::BCvt;
201 } else if (LocVT == MVT::v8i8 || LocVT == MVT::v4i16 || LocVT == MVT::v2i32) {
202 LocVT = MVT::i64;
203 LocInfo = CCValAssign::BCvt;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000204 }
205
Sirish Pande69295b82012-05-10 20:20:25 +0000206 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000207 if (!CC_Hexagon32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
208 return false;
209 }
210
Sirish Pande69295b82012-05-10 20:20:25 +0000211 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000212 if (!CC_Hexagon64(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
213 return false;
214 }
215
216 return true; // CC didn't match.
217}
218
219
220static bool CC_Hexagon32(unsigned ValNo, MVT ValVT,
221 MVT LocVT, CCValAssign::LocInfo LocInfo,
222 ISD::ArgFlagsTy ArgFlags, CCState &State) {
223
Craig Topper840beec2014-04-04 05:16:06 +0000224 static const MCPhysReg RegList[] = {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000225 Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3, Hexagon::R4,
226 Hexagon::R5
227 };
Tim Northover3b6b7ca2015-02-21 02:11:17 +0000228 if (unsigned Reg = State.AllocateReg(RegList)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000229 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
230 return false;
231 }
232
233 unsigned Offset = State.AllocateStack(4, 4);
234 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
235 return false;
236}
237
238static bool CC_Hexagon64(unsigned ValNo, MVT ValVT,
239 MVT LocVT, CCValAssign::LocInfo LocInfo,
240 ISD::ArgFlagsTy ArgFlags, CCState &State) {
241
242 if (unsigned Reg = State.AllocateReg(Hexagon::D0)) {
243 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
244 return false;
245 }
246
Craig Topper840beec2014-04-04 05:16:06 +0000247 static const MCPhysReg RegList1[] = {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000248 Hexagon::D1, Hexagon::D2
249 };
Craig Topper840beec2014-04-04 05:16:06 +0000250 static const MCPhysReg RegList2[] = {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000251 Hexagon::R1, Hexagon::R3
252 };
Tim Northover3b6b7ca2015-02-21 02:11:17 +0000253 if (unsigned Reg = State.AllocateReg(RegList1, RegList2)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000254 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
255 return false;
256 }
257
258 unsigned Offset = State.AllocateStack(8, 8, Hexagon::D2);
259 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
260 return false;
261}
262
263static bool RetCC_Hexagon(unsigned ValNo, MVT ValVT,
264 MVT LocVT, CCValAssign::LocInfo LocInfo,
265 ISD::ArgFlagsTy ArgFlags, CCState &State) {
266
267
268 if (LocVT == MVT::i1 ||
269 LocVT == MVT::i8 ||
270 LocVT == MVT::i16) {
271 LocVT = MVT::i32;
272 ValVT = MVT::i32;
273 if (ArgFlags.isSExt())
274 LocInfo = CCValAssign::SExt;
275 else if (ArgFlags.isZExt())
276 LocInfo = CCValAssign::ZExt;
277 else
278 LocInfo = CCValAssign::AExt;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000279 } else if (LocVT == MVT::v4i8 || LocVT == MVT::v2i16) {
280 LocVT = MVT::i32;
281 LocInfo = CCValAssign::BCvt;
282 } else if (LocVT == MVT::v8i8 || LocVT == MVT::v4i16 || LocVT == MVT::v2i32) {
283 LocVT = MVT::i64;
284 LocInfo = CCValAssign::BCvt;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000285 }
286
Sirish Pande69295b82012-05-10 20:20:25 +0000287 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000288 if (!RetCC_Hexagon32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
289 return false;
290 }
291
Sirish Pande69295b82012-05-10 20:20:25 +0000292 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000293 if (!RetCC_Hexagon64(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
294 return false;
295 }
296
297 return true; // CC didn't match.
298}
299
300static bool RetCC_Hexagon32(unsigned ValNo, MVT ValVT,
301 MVT LocVT, CCValAssign::LocInfo LocInfo,
302 ISD::ArgFlagsTy ArgFlags, CCState &State) {
303
Sirish Pande69295b82012-05-10 20:20:25 +0000304 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000305 if (unsigned Reg = State.AllocateReg(Hexagon::R0)) {
306 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
307 return false;
308 }
309 }
310
311 unsigned Offset = State.AllocateStack(4, 4);
312 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
313 return false;
314}
315
316static bool RetCC_Hexagon64(unsigned ValNo, MVT ValVT,
317 MVT LocVT, CCValAssign::LocInfo LocInfo,
318 ISD::ArgFlagsTy ArgFlags, CCState &State) {
Sirish Pande69295b82012-05-10 20:20:25 +0000319 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000320 if (unsigned Reg = State.AllocateReg(Hexagon::D0)) {
321 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
322 return false;
323 }
324 }
325
326 unsigned Offset = State.AllocateStack(8, 8);
327 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
328 return false;
329}
330
331SDValue
332HexagonTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG)
333const {
334 return SDValue();
335}
336
337/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
338/// by "Src" to address "Dst" of size "Size". Alignment information is
339/// specified by the specific parameter attribute. The copy will be passed as
340/// a byval function parameter. Sometimes what we are copying is the end of a
341/// larger object, the part that does not fit in registers.
342static SDValue
343CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
344 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000345 SDLoc dl) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000346
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000347 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000348 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
349 /*isVolatile=*/false, /*AlwaysInline=*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +0000350 /*isTailCall=*/false,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000351 MachinePointerInfo(), MachinePointerInfo());
352}
353
354
355// LowerReturn - Lower ISD::RET. If a struct is larger than 8 bytes and is
356// passed by value, the function prototype is modified to return void and
357// the value is stored in memory pointed by a pointer passed by caller.
358SDValue
359HexagonTargetLowering::LowerReturn(SDValue Chain,
360 CallingConv::ID CallConv, bool isVarArg,
361 const SmallVectorImpl<ISD::OutputArg> &Outs,
362 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000363 SDLoc dl, SelectionDAG &DAG) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000364
365 // CCValAssign - represent the assignment of the return value to locations.
366 SmallVector<CCValAssign, 16> RVLocs;
367
368 // CCState - Info about the registers and stack slot.
Eric Christopherb5217502014-08-06 18:45:26 +0000369 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
370 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000371
372 // Analyze return values of ISD::RET
373 CCInfo.AnalyzeReturn(Outs, RetCC_Hexagon);
374
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000375 SDValue Flag;
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000376 SmallVector<SDValue, 4> RetOps(1, Chain);
377
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000378 // Copy the result values into the output registers.
379 for (unsigned i = 0; i != RVLocs.size(); ++i) {
380 CCValAssign &VA = RVLocs[i];
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000381
382 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), OutVals[i], Flag);
383
384 // Guarantee that all emitted copies are stuck together with flags.
385 Flag = Chain.getValue(1);
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000386 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000387 }
388
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000389 RetOps[0] = Chain; // Update chain.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000390
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000391 // Add the flag if we have it.
392 if (Flag.getNode())
393 RetOps.push_back(Flag);
394
Craig Topper48d114b2014-04-26 18:35:24 +0000395 return DAG.getNode(HexagonISD::RET_FLAG, dl, MVT::Other, RetOps);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000396}
397
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000398bool HexagonTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
399 // If either no tail call or told not to tail call at all, don't.
400 if (!CI->isTailCall() || HTM.Options.DisableTailCalls)
401 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000402
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000403 return true;
404}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000405
406/// LowerCallResult - Lower the result values of an ISD::CALL into the
407/// appropriate copies out of appropriate physical registers. This assumes that
408/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
409/// being lowered. Returns a SDNode with the same number of values as the
410/// ISD::CALL.
411SDValue
412HexagonTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
413 CallingConv::ID CallConv, bool isVarArg,
414 const
415 SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000416 SDLoc dl, SelectionDAG &DAG,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000417 SmallVectorImpl<SDValue> &InVals,
418 const SmallVectorImpl<SDValue> &OutVals,
419 SDValue Callee) const {
420
421 // Assign locations to each value returned by this call.
422 SmallVector<CCValAssign, 16> RVLocs;
423
Eric Christopherb5217502014-08-06 18:45:26 +0000424 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
425 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000426
427 CCInfo.AnalyzeCallResult(Ins, RetCC_Hexagon);
428
429 // Copy all of the result registers out of their specified physreg.
430 for (unsigned i = 0; i != RVLocs.size(); ++i) {
431 Chain = DAG.getCopyFromReg(Chain, dl,
432 RVLocs[i].getLocReg(),
433 RVLocs[i].getValVT(), InFlag).getValue(1);
434 InFlag = Chain.getValue(2);
435 InVals.push_back(Chain.getValue(0));
436 }
437
438 return Chain;
439}
440
441/// LowerCall - Functions arguments are copied from virtual regs to
442/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
443SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +0000444HexagonTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000445 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +0000446 SelectionDAG &DAG = CLI.DAG;
Craig Topperb94011f2013-07-14 04:42:23 +0000447 SDLoc &dl = CLI.DL;
448 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
449 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
450 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000451 SDValue Chain = CLI.Chain;
452 SDValue Callee = CLI.Callee;
453 bool &isTailCall = CLI.IsTailCall;
454 CallingConv::ID CallConv = CLI.CallConv;
455 bool isVarArg = CLI.IsVarArg;
Colin LeMahieu2e3a26d2015-01-16 17:05:27 +0000456 bool doesNotReturn = CLI.DoesNotReturn;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000457
458 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000459 MachineFunction &MF = DAG.getMachineFunction();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000460
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000461 // Check for varargs.
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000462 int NumNamedVarArgParams = -1;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000463 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Callee))
464 {
Craig Topper062a2ba2014-04-25 05:30:21 +0000465 const Function* CalleeFn = nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000466 Callee = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, MVT::i32);
467 if ((CalleeFn = dyn_cast<Function>(GA->getGlobal())))
468 {
469 // If a function has zero args and is a vararg function, that's
470 // disallowed so it must be an undeclared function. Do not assume
471 // varargs if the callee is undefined.
472 if (CalleeFn->isVarArg() &&
473 CalleeFn->getFunctionType()->getNumParams() != 0) {
474 NumNamedVarArgParams = CalleeFn->getFunctionType()->getNumParams();
475 }
476 }
477 }
478
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000479 // Analyze operands of the call, assigning locations to each operand.
480 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000481 HexagonCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
482 *DAG.getContext(), NumNamedVarArgParams);
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000483
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000484 if (isVarArg)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000485 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon_VarArg);
486 else
487 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon);
488
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000489 if (DAG.getTarget().Options.DisableTailCalls)
490 isTailCall = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000491
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000492 if (isTailCall) {
493 bool StructAttrFlag = MF.getFunction()->hasStructRetAttr();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000494 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
495 isVarArg, IsStructRet,
496 StructAttrFlag,
497 Outs, OutVals, Ins, DAG);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000498 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000499 CCValAssign &VA = ArgLocs[i];
500 if (VA.isMemLoc()) {
501 isTailCall = false;
502 break;
503 }
504 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000505 DEBUG(dbgs() << (isTailCall ? "Eligible for Tail Call\n"
506 : "Argument must be passed on stack. "
507 "Not eligible for Tail Call\n"));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000508 }
509 // Get a count of how many bytes are to be pushed on the stack.
510 unsigned NumBytes = CCInfo.getNextStackOffset();
511 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
512 SmallVector<SDValue, 8> MemOpChains;
513
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000514 auto &HRI = *Subtarget.getRegisterInfo();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000515 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, HRI.getStackRegister(),
516 getPointerTy());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000517
518 // Walk the register/memloc assignments, inserting copies/loads.
519 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
520 CCValAssign &VA = ArgLocs[i];
521 SDValue Arg = OutVals[i];
522 ISD::ArgFlagsTy Flags = Outs[i].Flags;
523
524 // Promote the value if needed.
525 switch (VA.getLocInfo()) {
526 default:
527 // Loc info must be one of Full, SExt, ZExt, or AExt.
Craig Toppere55c5562012-02-07 02:50:20 +0000528 llvm_unreachable("Unknown loc info!");
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000529 case CCValAssign::BCvt:
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000530 case CCValAssign::Full:
531 break;
532 case CCValAssign::SExt:
533 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
534 break;
535 case CCValAssign::ZExt:
536 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
537 break;
538 case CCValAssign::AExt:
539 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
540 break;
541 }
542
543 if (VA.isMemLoc()) {
544 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000545 SDValue MemAddr = DAG.getConstant(LocMemOffset, dl,
546 StackPtr.getValueType());
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000547 MemAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, MemAddr);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000548 if (Flags.isByVal()) {
549 // The argument is a struct passed by value. According to LLVM, "Arg"
550 // is is pointer.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000551 MemOpChains.push_back(CreateCopyOfByValArgument(Arg, MemAddr, Chain,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000552 Flags, DAG, dl));
553 } else {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000554 MachinePointerInfo LocPI = MachinePointerInfo::getStack(LocMemOffset);
555 SDValue S = DAG.getStore(Chain, dl, Arg, MemAddr, LocPI, false,
556 false, 0);
557 MemOpChains.push_back(S);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000558 }
559 continue;
560 }
561
562 // Arguments that can be passed on register must be kept at RegsToPass
563 // vector.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000564 if (VA.isRegLoc())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000565 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000566 }
567
568 // Transform all store nodes into one single node because all store
569 // nodes are independent of each other.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000570 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +0000571 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000572
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000573 if (!isTailCall) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000574 SDValue C = DAG.getConstant(NumBytes, dl, getPointerTy(), true);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000575 Chain = DAG.getCALLSEQ_START(Chain, C, dl);
576 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000577
578 // Build a sequence of copy-to-reg nodes chained together with token
579 // chain and flag operands which copy the outgoing args into registers.
Benjamin Kramerbde91762012-06-02 10:20:22 +0000580 // The InFlag in necessary since all emitted instructions must be
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000581 // stuck together.
582 SDValue InFlag;
583 if (!isTailCall) {
584 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
585 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
586 RegsToPass[i].second, InFlag);
587 InFlag = Chain.getValue(1);
588 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000589 } else {
590 // For tail calls lower the arguments to the 'real' stack slot.
591 //
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000592 // Force all the incoming stack arguments to be loaded from the stack
593 // before any new outgoing arguments are stored to the stack, because the
594 // outgoing stack slots may alias the incoming argument stack slots, and
595 // the alias isn't otherwise explicit. This is slightly more conservative
596 // than necessary, because it means that each store effectively depends
597 // on every argument instead of just those arguments it would clobber.
598 //
Benjamin Kramerbde91762012-06-02 10:20:22 +0000599 // Do not flag preceding copytoreg stuff together with the following stuff.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000600 InFlag = SDValue();
601 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
602 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
603 RegsToPass[i].second, InFlag);
604 InFlag = Chain.getValue(1);
605 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000606 InFlag = SDValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000607 }
608
609 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
610 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
611 // node so that legalize doesn't hack it.
612 if (flag_aligned_memcpy) {
613 const char *MemcpyName =
614 "__hexagon_memcpy_likely_aligned_min32bytes_mult8bytes";
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000615 Callee = DAG.getTargetExternalSymbol(MemcpyName, getPointerTy());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000616 flag_aligned_memcpy = false;
617 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
618 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy());
619 } else if (ExternalSymbolSDNode *S =
620 dyn_cast<ExternalSymbolSDNode>(Callee)) {
621 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
622 }
623
624 // Returns a chain & a flag for retval copy to use.
625 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
626 SmallVector<SDValue, 8> Ops;
627 Ops.push_back(Chain);
628 Ops.push_back(Callee);
629
630 // Add argument registers to the end of the list so that they are
631 // known live into the call.
632 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
633 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
634 RegsToPass[i].second.getValueType()));
635 }
636
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000637 if (InFlag.getNode())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000638 Ops.push_back(InFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000639
640 if (isTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +0000641 return DAG.getNode(HexagonISD::TC_RETURN, dl, NodeTys, Ops);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000642
Colin LeMahieu2e3a26d2015-01-16 17:05:27 +0000643 int OpCode = doesNotReturn ? HexagonISD::CALLv3nr : HexagonISD::CALLv3;
644 Chain = DAG.getNode(OpCode, dl, NodeTys, Ops);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000645 InFlag = Chain.getValue(1);
646
647 // Create the CALLSEQ_END node.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000648 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
649 DAG.getIntPtrConstant(0, dl, true), InFlag, dl);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000650 InFlag = Chain.getValue(1);
651
652 // Handle result values, copying them out of physregs into vregs that we
653 // return.
654 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
655 InVals, OutVals, Callee);
656}
657
658static bool getIndexedAddressParts(SDNode *Ptr, EVT VT,
659 bool isSEXTLoad, SDValue &Base,
660 SDValue &Offset, bool &isInc,
661 SelectionDAG &DAG) {
662 if (Ptr->getOpcode() != ISD::ADD)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000663 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000664
665 if (VT == MVT::i64 || VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
666 isInc = (Ptr->getOpcode() == ISD::ADD);
667 Base = Ptr->getOperand(0);
668 Offset = Ptr->getOperand(1);
669 // Ensure that Offset is a constant.
670 return (isa<ConstantSDNode>(Offset));
671 }
672
673 return false;
674}
675
676// TODO: Put this function along with the other isS* functions in
677// HexagonISelDAGToDAG.cpp into a common file. Or better still, use the
Rafael Espindolab90c5f12012-11-21 16:56:33 +0000678// functions defined in HexagonOperands.td.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000679static bool Is_PostInc_S4_Offset(SDNode * S, int ShiftAmount) {
680 ConstantSDNode *N = cast<ConstantSDNode>(S);
681
682 // immS4 predicate - True if the immediate fits in a 4-bit sign extended.
683 // field.
684 int64_t v = (int64_t)N->getSExtValue();
685 int64_t m = 0;
686 if (ShiftAmount > 0) {
687 m = v % ShiftAmount;
688 v = v >> ShiftAmount;
689 }
690 return (v <= 7) && (v >= -8) && (m == 0);
691}
692
693/// getPostIndexedAddressParts - returns true by value, base pointer and
694/// offset pointer and addressing mode by reference if this node can be
695/// combined with a load / store to form a post-indexed load / store.
696bool HexagonTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
697 SDValue &Base,
698 SDValue &Offset,
699 ISD::MemIndexedMode &AM,
700 SelectionDAG &DAG) const
701{
702 EVT VT;
703 SDValue Ptr;
704 bool isSEXTLoad = false;
705
706 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
707 VT = LD->getMemoryVT();
708 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
709 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
710 VT = ST->getMemoryVT();
711 if (ST->getValue().getValueType() == MVT::i64 && ST->isTruncatingStore()) {
712 return false;
713 }
714 } else {
715 return false;
716 }
717
Chad Rosier64dc8aa2012-01-06 20:11:59 +0000718 bool isInc = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000719 bool isLegal = getIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
720 isInc, DAG);
721 // ShiftAmount = number of left-shifted bits in the Hexagon instruction.
722 int ShiftAmount = VT.getSizeInBits() / 16;
723 if (isLegal && Is_PostInc_S4_Offset(Offset.getNode(), ShiftAmount)) {
724 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
725 return true;
726 }
727
728 return false;
729}
730
731SDValue HexagonTargetLowering::LowerINLINEASM(SDValue Op,
732 SelectionDAG &DAG) const {
733 SDNode *Node = Op.getNode();
734 MachineFunction &MF = DAG.getMachineFunction();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000735 auto &FuncInfo = *MF.getInfo<HexagonMachineFunctionInfo>();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000736 switch (Node->getOpcode()) {
737 case ISD::INLINEASM: {
738 unsigned NumOps = Node->getNumOperands();
739 if (Node->getOperand(NumOps-1).getValueType() == MVT::Glue)
740 --NumOps; // Ignore the flag operand.
741
742 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000743 if (FuncInfo.hasClobberLR())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000744 break;
745 unsigned Flags =
746 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
747 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
748 ++i; // Skip the ID value.
749
750 switch (InlineAsm::getKind(Flags)) {
751 default: llvm_unreachable("Bad flags!");
752 case InlineAsm::Kind_RegDef:
753 case InlineAsm::Kind_RegUse:
754 case InlineAsm::Kind_Imm:
755 case InlineAsm::Kind_Clobber:
756 case InlineAsm::Kind_Mem: {
757 for (; NumVals; --NumVals, ++i) {}
758 break;
759 }
760 case InlineAsm::Kind_RegDefEarlyClobber: {
761 for (; NumVals; --NumVals, ++i) {
762 unsigned Reg =
763 cast<RegisterSDNode>(Node->getOperand(i))->getReg();
764
765 // Check it to be lr
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000766 const HexagonRegisterInfo *QRI = Subtarget.getRegisterInfo();
Eric Christopherdbe1cb02014-06-27 00:13:52 +0000767 if (Reg == QRI->getRARegister()) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000768 FuncInfo.setHasClobberLR(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000769 break;
770 }
771 }
772 break;
773 }
774 }
775 }
776 }
777 } // Node->getOpcode
778 return Op;
779}
780
781
782//
783// Taken from the XCore backend.
784//
785SDValue HexagonTargetLowering::
786LowerBR_JT(SDValue Op, SelectionDAG &DAG) const
787{
788 SDValue Chain = Op.getOperand(0);
789 SDValue Table = Op.getOperand(1);
790 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000791 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000792 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
793 unsigned JTI = JT->getIndex();
794 MachineFunction &MF = DAG.getMachineFunction();
795 const MachineJumpTableInfo *MJTI = MF.getJumpTableInfo();
796 SDValue TargetJT = DAG.getTargetJumpTable(JT->getIndex(), MVT::i32);
797
798 // Mark all jump table targets as address taken.
799 const std::vector<MachineJumpTableEntry> &JTE = MJTI->getJumpTables();
800 const std::vector<MachineBasicBlock*> &JTBBs = JTE[JTI].MBBs;
801 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
802 MachineBasicBlock *MBB = JTBBs[i];
803 MBB->setHasAddressTaken();
804 // This line is needed to set the hasAddressTaken flag on the BasicBlock
805 // object.
806 BlockAddress::get(const_cast<BasicBlock *>(MBB->getBasicBlock()));
807 }
808
Colin LeMahieuee776452015-03-10 19:29:53 +0000809 SDValue JumpTableBase = DAG.getNode(HexagonISD::JT, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000810 getPointerTy(), TargetJT);
811 SDValue ShiftIndex = DAG.getNode(ISD::SHL, dl, MVT::i32, Index,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000812 DAG.getConstant(2, dl, MVT::i32));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000813 SDValue JTAddress = DAG.getNode(ISD::ADD, dl, MVT::i32, JumpTableBase,
814 ShiftIndex);
815 SDValue LoadTarget = DAG.getLoad(MVT::i32, dl, Chain, JTAddress,
816 MachinePointerInfo(), false, false, false,
817 0);
818 return DAG.getNode(HexagonISD::BR_JT, dl, MVT::Other, Chain, LoadTarget);
819}
820
821
822SDValue
823HexagonTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
824 SelectionDAG &DAG) const {
825 SDValue Chain = Op.getOperand(0);
826 SDValue Size = Op.getOperand(1);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000827 SDValue Align = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000828 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000829
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000830 ConstantSDNode *AlignConst = dyn_cast<ConstantSDNode>(Align);
831 assert(AlignConst && "Non-constant Align in LowerDYNAMIC_STACKALLOC");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000832
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000833 unsigned A = AlignConst->getSExtValue();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000834 auto &HFI = *Subtarget.getFrameLowering();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000835 // "Zero" means natural stack alignment.
836 if (A == 0)
837 A = HFI.getStackAlignment();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000838
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000839 DEBUG({
Krzysztof Parzyszek9ee04e42015-04-22 17:19:44 +0000840 dbgs () << LLVM_FUNCTION_NAME << " Align: " << A << " Size: ";
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000841 Size.getNode()->dump(&DAG);
842 dbgs() << "\n";
843 });
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000844
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000845 SDValue AC = DAG.getConstant(A, dl, MVT::i32);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000846 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other);
847 return DAG.getNode(HexagonISD::ALLOCA, dl, VTs, Chain, Size, AC);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000848}
849
850SDValue
851HexagonTargetLowering::LowerFormalArguments(SDValue Chain,
852 CallingConv::ID CallConv,
853 bool isVarArg,
854 const
855 SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000856 SDLoc dl, SelectionDAG &DAG,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000857 SmallVectorImpl<SDValue> &InVals)
858const {
859
860 MachineFunction &MF = DAG.getMachineFunction();
861 MachineFrameInfo *MFI = MF.getFrameInfo();
862 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000863 auto &FuncInfo = *MF.getInfo<HexagonMachineFunctionInfo>();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000864
865 // Assign locations to all of the incoming arguments.
866 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000867 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
868 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000869
870 CCInfo.AnalyzeFormalArguments(Ins, CC_Hexagon);
871
872 // For LLVM, in the case when returning a struct by value (>8byte),
873 // the first argument is a pointer that points to the location on caller's
874 // stack where the return value will be stored. For Hexagon, the location on
875 // caller's stack is passed only when the struct size is smaller than (and
876 // equal to) 8 bytes. If not, no address will be passed into callee and
877 // callee return the result direclty through R0/R1.
878
879 SmallVector<SDValue, 4> MemOps;
880
881 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
882 CCValAssign &VA = ArgLocs[i];
883 ISD::ArgFlagsTy Flags = Ins[i].Flags;
884 unsigned ObjSize;
885 unsigned StackLocation;
886 int FI;
887
888 if ( (VA.isRegLoc() && !Flags.isByVal())
889 || (VA.isRegLoc() && Flags.isByVal() && Flags.getByValSize() > 8)) {
890 // Arguments passed in registers
891 // 1. int, long long, ptr args that get allocated in register.
892 // 2. Large struct that gets an register to put its address in.
893 EVT RegVT = VA.getLocVT();
Sirish Pande69295b82012-05-10 20:20:25 +0000894 if (RegVT == MVT::i8 || RegVT == MVT::i16 ||
895 RegVT == MVT::i32 || RegVT == MVT::f32) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000896 unsigned VReg =
Craig Topperc7242e02012-04-20 07:30:17 +0000897 RegInfo.createVirtualRegister(&Hexagon::IntRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000898 RegInfo.addLiveIn(VA.getLocReg(), VReg);
899 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
Colin LeMahieu4379d102015-01-28 22:08:16 +0000900 } else if (RegVT == MVT::i64 || RegVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000901 unsigned VReg =
Craig Topperc7242e02012-04-20 07:30:17 +0000902 RegInfo.createVirtualRegister(&Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000903 RegInfo.addLiveIn(VA.getLocReg(), VReg);
904 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
905 } else {
906 assert (0);
907 }
908 } else if (VA.isRegLoc() && Flags.isByVal() && Flags.getByValSize() <= 8) {
909 assert (0 && "ByValSize must be bigger than 8 bytes");
910 } else {
911 // Sanity check.
912 assert(VA.isMemLoc());
913
914 if (Flags.isByVal()) {
915 // If it's a byval parameter, then we need to compute the
916 // "real" size, not the size of the pointer.
917 ObjSize = Flags.getByValSize();
918 } else {
919 ObjSize = VA.getLocVT().getStoreSizeInBits() >> 3;
920 }
921
922 StackLocation = HEXAGON_LRFP_SIZE + VA.getLocMemOffset();
923 // Create the frame index object for this incoming parameter...
924 FI = MFI->CreateFixedObject(ObjSize, StackLocation, true);
925
926 // Create the SelectionDAG nodes cordl, responding to a load
927 // from this parameter.
928 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
929
930 if (Flags.isByVal()) {
931 // If it's a pass-by-value aggregate, then do not dereference the stack
932 // location. Instead, we should generate a reference to the stack
933 // location.
934 InVals.push_back(FIN);
935 } else {
936 InVals.push_back(DAG.getLoad(VA.getLocVT(), dl, Chain, FIN,
937 MachinePointerInfo(), false, false,
938 false, 0));
939 }
940 }
941 }
942
943 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +0000944 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000945
946 if (isVarArg) {
947 // This will point to the next argument passed via stack.
948 int FrameIndex = MFI->CreateFixedObject(Hexagon_PointerSize,
949 HEXAGON_LRFP_SIZE +
950 CCInfo.getNextStackOffset(),
951 true);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000952 FuncInfo.setVarArgsFrameIndex(FrameIndex);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000953 }
954
955 return Chain;
956}
957
958SDValue
959HexagonTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
960 // VASTART stores the address of the VarArgsFrameIndex slot into the
961 // memory location argument.
962 MachineFunction &MF = DAG.getMachineFunction();
963 HexagonMachineFunctionInfo *QFI = MF.getInfo<HexagonMachineFunctionInfo>();
964 SDValue Addr = DAG.getFrameIndex(QFI->getVarArgsFrameIndex(), MVT::i32);
965 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000966 return DAG.getStore(Op.getOperand(0), SDLoc(Op), Addr,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000967 Op.getOperand(1), MachinePointerInfo(SV), false,
968 false, 0);
969}
970
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000971// Creates a SPLAT instruction for a constant value VAL.
972static SDValue createSplat(SelectionDAG &DAG, SDLoc dl, EVT VT, SDValue Val) {
973 if (VT.getSimpleVT() == MVT::v4i8)
974 return DAG.getNode(HexagonISD::VSPLATB, dl, VT, Val);
975
976 if (VT.getSimpleVT() == MVT::v4i16)
977 return DAG.getNode(HexagonISD::VSPLATH, dl, VT, Val);
978
979 return SDValue();
980}
981
982static bool isSExtFree(SDValue N) {
983 // A sign-extend of a truncate of a sign-extend is free.
984 if (N.getOpcode() == ISD::TRUNCATE &&
985 N.getOperand(0).getOpcode() == ISD::AssertSext)
986 return true;
987 // We have sign-extended loads.
988 if (N.getOpcode() == ISD::LOAD)
989 return true;
990 return false;
991}
992
993SDValue HexagonTargetLowering::LowerCTPOP(SDValue Op, SelectionDAG &DAG) const {
994 SDLoc dl(Op);
995 SDValue InpVal = Op.getOperand(0);
996 if (isa<ConstantSDNode>(InpVal)) {
997 uint64_t V = cast<ConstantSDNode>(InpVal)->getZExtValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000998 return DAG.getTargetConstant(countPopulation(V), dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000999 }
1000 SDValue PopOut = DAG.getNode(HexagonISD::POPCOUNT, dl, MVT::i32, InpVal);
1001 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, PopOut);
1002}
1003
1004SDValue HexagonTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
1005 SDLoc dl(Op);
1006
1007 SDValue LHS = Op.getOperand(0);
1008 SDValue RHS = Op.getOperand(1);
1009 SDValue Cmp = Op.getOperand(2);
1010 ISD::CondCode CC = cast<CondCodeSDNode>(Cmp)->get();
1011
1012 EVT VT = Op.getValueType();
1013 EVT LHSVT = LHS.getValueType();
1014 EVT RHSVT = RHS.getValueType();
1015
1016 if (LHSVT == MVT::v2i16) {
1017 assert(ISD::isSignedIntSetCC(CC) || ISD::isUnsignedIntSetCC(CC));
1018 unsigned ExtOpc = ISD::isSignedIntSetCC(CC) ? ISD::SIGN_EXTEND
1019 : ISD::ZERO_EXTEND;
1020 SDValue LX = DAG.getNode(ExtOpc, dl, MVT::v2i32, LHS);
1021 SDValue RX = DAG.getNode(ExtOpc, dl, MVT::v2i32, RHS);
1022 SDValue SC = DAG.getNode(ISD::SETCC, dl, MVT::v2i1, LX, RX, Cmp);
1023 return SC;
1024 }
1025
1026 // Treat all other vector types as legal.
1027 if (VT.isVector())
1028 return Op;
1029
1030 // Equals and not equals should use sign-extend, not zero-extend, since
1031 // we can represent small negative values in the compare instructions.
1032 // The LLVM default is to use zero-extend arbitrarily in these cases.
1033 if ((CC == ISD::SETEQ || CC == ISD::SETNE) &&
1034 (RHSVT == MVT::i8 || RHSVT == MVT::i16) &&
1035 (LHSVT == MVT::i8 || LHSVT == MVT::i16)) {
1036 ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS);
1037 if (C && C->getAPIntValue().isNegative()) {
1038 LHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, LHS);
1039 RHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, RHS);
1040 return DAG.getNode(ISD::SETCC, dl, Op.getValueType(),
1041 LHS, RHS, Op.getOperand(2));
1042 }
1043 if (isSExtFree(LHS) || isSExtFree(RHS)) {
1044 LHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, LHS);
1045 RHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, RHS);
1046 return DAG.getNode(ISD::SETCC, dl, Op.getValueType(),
1047 LHS, RHS, Op.getOperand(2));
1048 }
1049 }
1050 return SDValue();
1051}
1052
1053SDValue HexagonTargetLowering::LowerVSELECT(SDValue Op, SelectionDAG &DAG)
1054 const {
1055 SDValue PredOp = Op.getOperand(0);
1056 SDValue Op1 = Op.getOperand(1), Op2 = Op.getOperand(2);
1057 EVT OpVT = Op1.getValueType();
1058 SDLoc DL(Op);
1059
1060 if (OpVT == MVT::v2i16) {
1061 SDValue X1 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i32, Op1);
1062 SDValue X2 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i32, Op2);
1063 SDValue SL = DAG.getNode(ISD::VSELECT, DL, MVT::v2i32, PredOp, X1, X2);
1064 SDValue TR = DAG.getNode(ISD::TRUNCATE, DL, MVT::v2i16, SL);
1065 return TR;
1066 }
1067
1068 return SDValue();
1069}
1070
1071// Handle only specific vector loads.
1072SDValue HexagonTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1073 EVT VT = Op.getValueType();
1074 SDLoc DL(Op);
1075 LoadSDNode *LoadNode = cast<LoadSDNode>(Op);
1076 SDValue Chain = LoadNode->getChain();
1077 SDValue Ptr = Op.getOperand(1);
1078 SDValue LoweredLoad;
1079 SDValue Result;
1080 SDValue Base = LoadNode->getBasePtr();
1081 ISD::LoadExtType Ext = LoadNode->getExtensionType();
1082 unsigned Alignment = LoadNode->getAlignment();
1083 SDValue LoadChain;
1084
1085 if(Ext == ISD::NON_EXTLOAD)
1086 Ext = ISD::ZEXTLOAD;
1087
1088 if (VT == MVT::v4i16) {
1089 if (Alignment == 2) {
1090 SDValue Loads[4];
1091 // Base load.
1092 Loads[0] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Base,
1093 LoadNode->getPointerInfo(), MVT::i16,
1094 LoadNode->isVolatile(),
1095 LoadNode->isNonTemporal(),
1096 LoadNode->isInvariant(),
1097 Alignment);
1098 // Base+2 load.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001099 SDValue Increment = DAG.getConstant(2, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001100 Ptr = DAG.getNode(ISD::ADD, DL, Base.getValueType(), Base, Increment);
1101 Loads[1] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Ptr,
1102 LoadNode->getPointerInfo(), MVT::i16,
1103 LoadNode->isVolatile(),
1104 LoadNode->isNonTemporal(),
1105 LoadNode->isInvariant(),
1106 Alignment);
1107 // SHL 16, then OR base and base+2.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001108 SDValue ShiftAmount = DAG.getConstant(16, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001109 SDValue Tmp1 = DAG.getNode(ISD::SHL, DL, MVT::i32, Loads[1], ShiftAmount);
1110 SDValue Tmp2 = DAG.getNode(ISD::OR, DL, MVT::i32, Tmp1, Loads[0]);
1111 // Base + 4.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001112 Increment = DAG.getConstant(4, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001113 Ptr = DAG.getNode(ISD::ADD, DL, Base.getValueType(), Base, Increment);
1114 Loads[2] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Ptr,
1115 LoadNode->getPointerInfo(), MVT::i16,
1116 LoadNode->isVolatile(),
1117 LoadNode->isNonTemporal(),
1118 LoadNode->isInvariant(),
1119 Alignment);
1120 // Base + 6.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001121 Increment = DAG.getConstant(6, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001122 Ptr = DAG.getNode(ISD::ADD, DL, Base.getValueType(), Base, Increment);
1123 Loads[3] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Ptr,
1124 LoadNode->getPointerInfo(), MVT::i16,
1125 LoadNode->isVolatile(),
1126 LoadNode->isNonTemporal(),
1127 LoadNode->isInvariant(),
1128 Alignment);
1129 // SHL 16, then OR base+4 and base+6.
1130 Tmp1 = DAG.getNode(ISD::SHL, DL, MVT::i32, Loads[3], ShiftAmount);
1131 SDValue Tmp4 = DAG.getNode(ISD::OR, DL, MVT::i32, Tmp1, Loads[2]);
1132 // Combine to i64. This could be optimised out later if we can
1133 // affect reg allocation of this code.
1134 Result = DAG.getNode(HexagonISD::COMBINE, DL, MVT::i64, Tmp4, Tmp2);
1135 LoadChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
1136 Loads[0].getValue(1), Loads[1].getValue(1),
1137 Loads[2].getValue(1), Loads[3].getValue(1));
1138 } else {
1139 // Perform default type expansion.
1140 Result = DAG.getLoad(MVT::i64, DL, Chain, Ptr, LoadNode->getPointerInfo(),
1141 LoadNode->isVolatile(), LoadNode->isNonTemporal(),
1142 LoadNode->isInvariant(), LoadNode->getAlignment());
1143 LoadChain = Result.getValue(1);
1144 }
1145 } else
1146 llvm_unreachable("Custom lowering unsupported load");
1147
1148 Result = DAG.getNode(ISD::BITCAST, DL, VT, Result);
1149 // Since we pretend to lower a load, we need the original chain
1150 // info attached to the result.
1151 SDValue Ops[] = { Result, LoadChain };
1152
1153 return DAG.getMergeValues(Ops, DL);
1154}
1155
1156
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001157SDValue
Sirish Pande69295b82012-05-10 20:20:25 +00001158HexagonTargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
1159 EVT ValTy = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001160 SDLoc dl(Op);
Sirish Pande69295b82012-05-10 20:20:25 +00001161 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
1162 SDValue Res;
1163 if (CP->isMachineConstantPoolEntry())
1164 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), ValTy,
1165 CP->getAlignment());
1166 else
1167 Res = DAG.getTargetConstantPool(CP->getConstVal(), ValTy,
1168 CP->getAlignment());
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001169 return DAG.getNode(HexagonISD::CP, dl, ValTy, Res);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001170}
1171
1172SDValue
1173HexagonTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001174 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001175 MachineFunction &MF = DAG.getMachineFunction();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001176 MachineFrameInfo &MFI = *MF.getFrameInfo();
1177 MFI.setReturnAddressIsTaken(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001178
Bill Wendling908bf812014-01-06 00:43:20 +00001179 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001180 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001181
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001182 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001183 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001184 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1185 if (Depth) {
1186 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001187 SDValue Offset = DAG.getConstant(4, dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001188 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
1189 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
1190 MachinePointerInfo(), false, false, false, 0);
1191 }
1192
1193 // Return LR, which contains the return address. Mark it an implicit live-in.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001194 unsigned Reg = MF.addLiveIn(HRI.getRARegister(), getRegClassFor(MVT::i32));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001195 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
1196}
1197
1198SDValue
1199HexagonTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001200 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
1201 MachineFrameInfo &MFI = *DAG.getMachineFunction().getFrameInfo();
1202 MFI.setFrameAddressIsTaken(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001203
1204 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001205 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001206 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1207 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001208 HRI.getFrameRegister(), VT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001209 while (Depth--)
1210 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
1211 MachinePointerInfo(),
1212 false, false, false, 0);
1213 return FrameAddr;
1214}
1215
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001216SDValue HexagonTargetLowering::LowerATOMIC_FENCE(SDValue Op,
1217 SelectionDAG& DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001218 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001219 return DAG.getNode(HexagonISD::BARRIER, dl, MVT::Other, Op.getOperand(0));
1220}
1221
1222
1223SDValue HexagonTargetLowering::LowerGLOBALADDRESS(SDValue Op,
1224 SelectionDAG &DAG) const {
1225 SDValue Result;
1226 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1227 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001228 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001229 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
1230
Eric Christopher36fe0282015-02-03 07:22:52 +00001231 const HexagonTargetObjectFile *TLOF =
1232 static_cast<const HexagonTargetObjectFile *>(
1233 getTargetMachine().getObjFileLowering());
1234 if (TLOF->IsGlobalInSmallSection(GV, getTargetMachine())) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001235 return DAG.getNode(HexagonISD::CONST32_GP, dl, getPointerTy(), Result);
1236 }
1237
1238 return DAG.getNode(HexagonISD::CONST32, dl, getPointerTy(), Result);
1239}
1240
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001241// Specifies that for loads and stores VT can be promoted to PromotedLdStVT.
1242void HexagonTargetLowering::promoteLdStType(EVT VT, EVT PromotedLdStVT) {
1243 if (VT != PromotedLdStVT) {
1244 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
1245 AddPromotedToType(ISD::LOAD, VT.getSimpleVT(),
1246 PromotedLdStVT.getSimpleVT());
1247
1248 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
1249 AddPromotedToType(ISD::STORE, VT.getSimpleVT(),
1250 PromotedLdStVT.getSimpleVT());
1251 }
1252}
1253
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001254SDValue
1255HexagonTargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
1256 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
1257 SDValue BA_SD = DAG.getTargetBlockAddress(BA, MVT::i32);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001258 SDLoc dl(Op);
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001259 return DAG.getNode(HexagonISD::CONST32_GP, dl, getPointerTy(), BA_SD);
1260}
1261
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001262//===----------------------------------------------------------------------===//
1263// TargetLowering Implementation
1264//===----------------------------------------------------------------------===//
1265
Eric Christopherd737b762015-02-02 22:11:36 +00001266HexagonTargetLowering::HexagonTargetLowering(const TargetMachine &TM,
1267 const HexagonSubtarget &STI)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001268 : TargetLowering(TM), HTM(static_cast<const HexagonTargetMachine&>(TM)),
1269 Subtarget(STI) {
1270 bool IsV4 = !Subtarget.hasV5TOps();
1271 auto &HRI = *Subtarget.getRegisterInfo();
Sirish Pande69295b82012-05-10 20:20:25 +00001272
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001273 setPrefLoopAlignment(4);
1274 setPrefFunctionAlignment(4);
1275 setMinFunctionAlignment(2);
1276 setInsertFencesForAtomic(false);
1277 setExceptionPointerRegister(Hexagon::R0);
1278 setExceptionSelectorRegister(Hexagon::R1);
1279 setStackPointerRegisterToSaveRestore(HRI.getStackRegister());
1280
1281 if (EnableHexSDNodeSched)
1282 setSchedulingPreference(Sched::VLIW);
1283 else
1284 setSchedulingPreference(Sched::Source);
1285
1286 // Limits for inline expansion of memcpy/memmove
1287 MaxStoresPerMemcpy = MaxStoresPerMemcpyCL;
1288 MaxStoresPerMemcpyOptSize = MaxStoresPerMemcpyOptSizeCL;
1289 MaxStoresPerMemmove = MaxStoresPerMemmoveCL;
1290 MaxStoresPerMemmoveOptSize = MaxStoresPerMemmoveOptSizeCL;
1291 MaxStoresPerMemset = MaxStoresPerMemsetCL;
1292 MaxStoresPerMemsetOptSize = MaxStoresPerMemsetOptSizeCL;
1293
1294 //
1295 // Set up register classes.
1296 //
1297
1298 addRegisterClass(MVT::i1, &Hexagon::PredRegsRegClass);
1299 addRegisterClass(MVT::v2i1, &Hexagon::PredRegsRegClass); // bbbbaaaa
1300 addRegisterClass(MVT::v4i1, &Hexagon::PredRegsRegClass); // ddccbbaa
1301 addRegisterClass(MVT::v8i1, &Hexagon::PredRegsRegClass); // hgfedcba
1302 addRegisterClass(MVT::i32, &Hexagon::IntRegsRegClass);
1303 addRegisterClass(MVT::v4i8, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001304 addRegisterClass(MVT::v2i16, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001305 addRegisterClass(MVT::i64, &Hexagon::DoubleRegsRegClass);
1306 addRegisterClass(MVT::v8i8, &Hexagon::DoubleRegsRegClass);
1307 addRegisterClass(MVT::v4i16, &Hexagon::DoubleRegsRegClass);
1308 addRegisterClass(MVT::v2i32, &Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001309
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001310 if (Subtarget.hasV5TOps()) {
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001311 addRegisterClass(MVT::f32, &Hexagon::IntRegsRegClass);
1312 addRegisterClass(MVT::f64, &Hexagon::DoubleRegsRegClass);
1313 }
Sirish Pande69295b82012-05-10 20:20:25 +00001314
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001315 //
1316 // Handling of scalar operations.
1317 //
1318 // All operations default to "legal", except:
1319 // - indexed loads and stores (pre-/post-incremented),
1320 // - ANY_EXTEND_VECTOR_INREG, ATOMIC_CMP_SWAP_WITH_SUCCESS, CONCAT_VECTORS,
1321 // ConstantFP, DEBUGTRAP, FCEIL, FCOPYSIGN, FEXP, FEXP2, FFLOOR, FGETSIGN,
1322 // FLOG, FLOG2, FLOG10, FMAXNUM, FMINNUM, FNEARBYINT, FRINT, FROUND, TRAP,
1323 // FTRUNC, PREFETCH, SIGN_EXTEND_VECTOR_INREG, ZERO_EXTEND_VECTOR_INREG,
1324 // which default to "expand" for at least one type.
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001325
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001326 // Misc operations.
1327 setOperationAction(ISD::ConstantFP, MVT::f32, Legal); // Default: expand
1328 setOperationAction(ISD::ConstantFP, MVT::f64, Legal); // Default: expand
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001329
1330 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001331 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001332 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
1333 setOperationAction(ISD::INLINEASM, MVT::Other, Custom);
1334 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
1335 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001336
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001337 // Custom legalize GlobalAddress nodes into CONST32.
1338 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001339 setOperationAction(ISD::GlobalAddress, MVT::i8, Custom);
1340 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001341
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001342 // Hexagon needs to optimize cases with negative constants.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001343 setOperationAction(ISD::SETCC, MVT::i8, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001344 setOperationAction(ISD::SETCC, MVT::i16, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001345
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001346 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
1347 setOperationAction(ISD::VASTART, MVT::Other, Custom);
1348 setOperationAction(ISD::VAEND, MVT::Other, Expand);
1349 setOperationAction(ISD::VAARG, MVT::Other, Expand);
1350
1351 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
1352 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
1353 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
1354
1355 if (EmitJumpTables)
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001356 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001357 else
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001358 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001359 // Increase jump tables cutover to 5, was 4.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001360 setMinimumJumpTableEntries(MinimumJumpTables);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001361
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001362 // Hexagon has instructions for add/sub with carry. The problem with
1363 // modeling these instructions is that they produce 2 results: Rdd and Px.
1364 // To model the update of Px, we will have to use Defs[p0..p3] which will
1365 // cause any predicate live range to spill. So, we pretend we dont't have
1366 // these instructions.
1367 setOperationAction(ISD::ADDE, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001368 setOperationAction(ISD::ADDE, MVT::i16, Expand);
1369 setOperationAction(ISD::ADDE, MVT::i32, Expand);
1370 setOperationAction(ISD::ADDE, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001371 setOperationAction(ISD::SUBE, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001372 setOperationAction(ISD::SUBE, MVT::i16, Expand);
1373 setOperationAction(ISD::SUBE, MVT::i32, Expand);
1374 setOperationAction(ISD::SUBE, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001375 setOperationAction(ISD::ADDC, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001376 setOperationAction(ISD::ADDC, MVT::i16, Expand);
1377 setOperationAction(ISD::ADDC, MVT::i32, Expand);
1378 setOperationAction(ISD::ADDC, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001379 setOperationAction(ISD::SUBC, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001380 setOperationAction(ISD::SUBC, MVT::i16, Expand);
1381 setOperationAction(ISD::SUBC, MVT::i32, Expand);
1382 setOperationAction(ISD::SUBC, MVT::i64, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001383
Krzysztof Parzyszek2c4487d2015-04-13 20:37:01 +00001384 // Only add and sub that detect overflow are the saturating ones.
1385 for (MVT VT : MVT::integer_valuetypes()) {
1386 setOperationAction(ISD::UADDO, VT, Expand);
1387 setOperationAction(ISD::SADDO, VT, Expand);
1388 setOperationAction(ISD::USUBO, VT, Expand);
1389 setOperationAction(ISD::SSUBO, VT, Expand);
1390 }
1391
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001392 setOperationAction(ISD::CTLZ, MVT::i8, Promote);
1393 setOperationAction(ISD::CTLZ, MVT::i16, Promote);
1394 setOperationAction(ISD::CTTZ, MVT::i8, Promote);
1395 setOperationAction(ISD::CTTZ, MVT::i16, Promote);
1396 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8, Promote);
1397 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16, Promote);
1398 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i8, Promote);
1399 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16, Promote);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001400
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001401 // In V5, popcount can count # of 1s in i64 but returns i32.
1402 // On V4 it will be expanded (set later).
1403 setOperationAction(ISD::CTPOP, MVT::i8, Promote);
1404 setOperationAction(ISD::CTPOP, MVT::i16, Promote);
1405 setOperationAction(ISD::CTPOP, MVT::i32, Promote);
1406 setOperationAction(ISD::CTPOP, MVT::i64, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001407
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001408 // We custom lower i64 to i64 mul, so that it is not considered as a legal
1409 // operation. There is a pattern that will match i64 mul and transform it
1410 // to a series of instructions.
1411 setOperationAction(ISD::MUL, MVT::i64, Expand);
Colin LeMahieude68b662015-02-05 21:13:25 +00001412 setOperationAction(ISD::MULHS, MVT::i64, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001413
Benjamin Kramer62460692015-04-25 14:46:53 +00001414 for (unsigned IntExpOp :
1415 {ISD::SDIV, ISD::UDIV, ISD::SREM, ISD::UREM, ISD::SDIVREM, ISD::UDIVREM,
1416 ISD::ROTL, ISD::ROTR, ISD::BSWAP, ISD::SHL_PARTS, ISD::SRA_PARTS,
1417 ISD::SRL_PARTS, ISD::SMUL_LOHI, ISD::UMUL_LOHI}) {
1418 setOperationAction(IntExpOp, MVT::i32, Expand);
1419 setOperationAction(IntExpOp, MVT::i64, Expand);
1420 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001421
Benjamin Kramer62460692015-04-25 14:46:53 +00001422 for (unsigned FPExpOp :
1423 {ISD::FDIV, ISD::FREM, ISD::FSQRT, ISD::FSIN, ISD::FCOS, ISD::FSINCOS,
1424 ISD::FPOW, ISD::FCOPYSIGN}) {
1425 setOperationAction(FPExpOp, MVT::f32, Expand);
1426 setOperationAction(FPExpOp, MVT::f64, Expand);
1427 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001428
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001429 // No extending loads from i32.
1430 for (MVT VT : MVT::integer_valuetypes()) {
1431 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand);
1432 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand);
1433 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand);
1434 }
1435 // Turn FP truncstore into trunc + store.
1436 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
1437 // Turn FP extload into load/fextend.
1438 for (MVT VT : MVT::fp_valuetypes())
1439 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001440
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001441 // Expand BR_CC and SELECT_CC for all integer and fp types.
1442 for (MVT VT : MVT::integer_valuetypes()) {
1443 setOperationAction(ISD::BR_CC, VT, Expand);
1444 setOperationAction(ISD::SELECT_CC, VT, Expand);
1445 }
1446 for (MVT VT : MVT::fp_valuetypes()) {
1447 setOperationAction(ISD::BR_CC, VT, Expand);
1448 setOperationAction(ISD::SELECT_CC, VT, Expand);
1449 }
1450 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001451
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001452 //
1453 // Handling of vector operations.
1454 //
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001455
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001456 // Custom lower v4i16 load only. Let v4i16 store to be
1457 // promoted for now.
1458 promoteLdStType(MVT::v4i8, MVT::i32);
1459 promoteLdStType(MVT::v2i16, MVT::i32);
1460 promoteLdStType(MVT::v8i8, MVT::i64);
1461 promoteLdStType(MVT::v2i32, MVT::i64);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001462
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001463 setOperationAction(ISD::LOAD, MVT::v4i16, Custom);
1464 setOperationAction(ISD::STORE, MVT::v4i16, Promote);
1465 AddPromotedToType(ISD::LOAD, MVT::v4i16, MVT::i64);
1466 AddPromotedToType(ISD::STORE, MVT::v4i16, MVT::i64);
1467
1468 // Set the action for vector operations to "expand", then override it with
1469 // either "custom" or "legal" for specific cases.
1470 static unsigned VectExpOps[] = {
1471 // Integer arithmetic:
1472 ISD::ADD, ISD::SUB, ISD::MUL, ISD::SDIV, ISD::UDIV,
1473 ISD::SREM, ISD::UREM, ISD::SDIVREM, ISD::UDIVREM, ISD::ADDC,
1474 ISD::SUBC, ISD::SADDO, ISD::UADDO, ISD::SSUBO, ISD::USUBO,
1475 ISD::SMUL_LOHI, ISD::UMUL_LOHI,
1476 // Logical/bit:
1477 ISD::AND, ISD::OR, ISD::XOR, ISD::ROTL, ISD::ROTR,
1478 ISD::CTPOP, ISD::CTLZ, ISD::CTTZ, ISD::CTLZ_ZERO_UNDEF,
1479 ISD::CTTZ_ZERO_UNDEF,
1480 // Floating point arithmetic/math functions:
1481 ISD::FADD, ISD::FSUB, ISD::FMUL, ISD::FMA, ISD::FDIV,
1482 ISD::FREM, ISD::FNEG, ISD::FABS, ISD::FSQRT, ISD::FSIN,
1483 ISD::FCOS, ISD::FPOWI, ISD::FPOW, ISD::FLOG, ISD::FLOG2,
1484 ISD::FLOG10, ISD::FEXP, ISD::FEXP2, ISD::FCEIL, ISD::FTRUNC,
1485 ISD::FRINT, ISD::FNEARBYINT, ISD::FROUND, ISD::FFLOOR,
1486 ISD::FMINNUM, ISD::FMAXNUM, ISD::FSINCOS,
1487 // Misc:
1488 ISD::SELECT, ISD::ConstantPool,
1489 // Vector:
1490 ISD::BUILD_VECTOR, ISD::SCALAR_TO_VECTOR,
1491 ISD::EXTRACT_VECTOR_ELT, ISD::INSERT_VECTOR_ELT,
1492 ISD::EXTRACT_SUBVECTOR, ISD::INSERT_SUBVECTOR,
1493 ISD::CONCAT_VECTORS, ISD::VECTOR_SHUFFLE
1494 };
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001495
1496 for (MVT VT : MVT::vector_valuetypes()) {
Benjamin Kramer62460692015-04-25 14:46:53 +00001497 for (unsigned VectExpOp : VectExpOps)
1498 setOperationAction(VectExpOp, VT, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001499
1500 // Expand all extended loads and truncating stores:
1501 for (MVT TargetVT : MVT::vector_valuetypes()) {
1502 setLoadExtAction(ISD::EXTLOAD, TargetVT, VT, Expand);
1503 setTruncStoreAction(VT, TargetVT, Expand);
1504 }
1505
1506 setOperationAction(ISD::SRA, VT, Custom);
1507 setOperationAction(ISD::SHL, VT, Custom);
1508 setOperationAction(ISD::SRL, VT, Custom);
1509 }
1510
1511 // Types natively supported:
Benjamin Kramer62460692015-04-25 14:46:53 +00001512 for (MVT NativeVT : {MVT::v2i1, MVT::v4i1, MVT::v8i1, MVT::v32i1, MVT::v64i1,
1513 MVT::v4i8, MVT::v8i8, MVT::v2i16, MVT::v4i16, MVT::v1i32,
1514 MVT::v2i32, MVT::v1i64}) {
1515 setOperationAction(ISD::BUILD_VECTOR, NativeVT, Custom);
1516 setOperationAction(ISD::EXTRACT_VECTOR_ELT, NativeVT, Custom);
1517 setOperationAction(ISD::INSERT_VECTOR_ELT, NativeVT, Custom);
1518 setOperationAction(ISD::EXTRACT_SUBVECTOR, NativeVT, Custom);
1519 setOperationAction(ISD::INSERT_SUBVECTOR, NativeVT, Custom);
1520 setOperationAction(ISD::CONCAT_VECTORS, NativeVT, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001521
Benjamin Kramer62460692015-04-25 14:46:53 +00001522 setOperationAction(ISD::ADD, NativeVT, Legal);
1523 setOperationAction(ISD::SUB, NativeVT, Legal);
1524 setOperationAction(ISD::MUL, NativeVT, Legal);
1525 setOperationAction(ISD::AND, NativeVT, Legal);
1526 setOperationAction(ISD::OR, NativeVT, Legal);
1527 setOperationAction(ISD::XOR, NativeVT, Legal);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001528 }
1529
1530 setOperationAction(ISD::SETCC, MVT::v2i16, Custom);
1531 setOperationAction(ISD::VSELECT, MVT::v2i16, Custom);
1532 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
1533 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
1534
1535 // Subtarget-specific operation actions.
1536 //
1537 if (Subtarget.hasV5TOps()) {
1538 setOperationAction(ISD::FMA, MVT::f64, Expand);
1539 setOperationAction(ISD::FADD, MVT::f64, Expand);
1540 setOperationAction(ISD::FSUB, MVT::f64, Expand);
1541 setOperationAction(ISD::FMUL, MVT::f64, Expand);
1542
1543 setOperationAction(ISD::FP_TO_UINT, MVT::i1, Promote);
1544 setOperationAction(ISD::FP_TO_UINT, MVT::i8, Promote);
1545 setOperationAction(ISD::FP_TO_UINT, MVT::i16, Promote);
1546 setOperationAction(ISD::FP_TO_SINT, MVT::i1, Promote);
1547 setOperationAction(ISD::FP_TO_SINT, MVT::i8, Promote);
1548 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
1549 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Promote);
1550 setOperationAction(ISD::UINT_TO_FP, MVT::i8, Promote);
1551 setOperationAction(ISD::UINT_TO_FP, MVT::i16, Promote);
1552 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Promote);
1553 setOperationAction(ISD::SINT_TO_FP, MVT::i8, Promote);
1554 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
1555
1556 } else { // V4
1557 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
1558 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Expand);
1559 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
1560 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
1561 setOperationAction(ISD::FP_TO_SINT, MVT::f64, Expand);
1562 setOperationAction(ISD::FP_TO_SINT, MVT::f32, Expand);
1563 setOperationAction(ISD::FP_EXTEND, MVT::f32, Expand);
1564 setOperationAction(ISD::FP_ROUND, MVT::f64, Expand);
1565 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
1566
1567 setOperationAction(ISD::CTPOP, MVT::i8, Expand);
1568 setOperationAction(ISD::CTPOP, MVT::i16, Expand);
1569 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
1570 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
1571
1572 // Expand these operations for both f32 and f64:
Benjamin Kramer62460692015-04-25 14:46:53 +00001573 for (unsigned FPExpOpV4 :
1574 {ISD::FADD, ISD::FSUB, ISD::FMUL, ISD::FABS, ISD::FNEG, ISD::FMA}) {
1575 setOperationAction(FPExpOpV4, MVT::f32, Expand);
1576 setOperationAction(FPExpOpV4, MVT::f64, Expand);
1577 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001578
Benjamin Kramer62460692015-04-25 14:46:53 +00001579 for (ISD::CondCode FPExpCCV4 :
1580 {ISD::SETOEQ, ISD::SETOGT, ISD::SETOLT, ISD::SETOGE, ISD::SETOLE,
1581 ISD::SETUO, ISD::SETO}) {
1582 setCondCodeAction(FPExpCCV4, MVT::f32, Expand);
1583 setCondCodeAction(FPExpCCV4, MVT::f64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001584 }
1585 }
1586
1587 // Handling of indexed loads/stores: default is "expand".
1588 //
Benjamin Kramer62460692015-04-25 14:46:53 +00001589 for (MVT LSXTy : {MVT::i8, MVT::i16, MVT::i32, MVT::i64}) {
1590 setIndexedLoadAction(ISD::POST_INC, LSXTy, Legal);
1591 setIndexedStoreAction(ISD::POST_INC, LSXTy, Legal);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001592 }
1593
1594 computeRegisterProperties(&HRI);
1595
1596 //
1597 // Library calls for unsupported operations
1598 //
1599 bool FastMath = EnableFastMath;
1600
Benjamin Kramera37c8092015-04-25 14:46:46 +00001601 setLibcallName(RTLIB::SDIV_I32, "__hexagon_divsi3");
1602 setLibcallName(RTLIB::SDIV_I64, "__hexagon_divdi3");
1603 setLibcallName(RTLIB::UDIV_I32, "__hexagon_udivsi3");
1604 setLibcallName(RTLIB::UDIV_I64, "__hexagon_udivdi3");
1605 setLibcallName(RTLIB::SREM_I32, "__hexagon_modsi3");
1606 setLibcallName(RTLIB::SREM_I64, "__hexagon_moddi3");
1607 setLibcallName(RTLIB::UREM_I32, "__hexagon_umodsi3");
1608 setLibcallName(RTLIB::UREM_I64, "__hexagon_umoddi3");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001609
Benjamin Kramera37c8092015-04-25 14:46:46 +00001610 setLibcallName(RTLIB::SINTTOFP_I128_F64, "__hexagon_floattidf");
1611 setLibcallName(RTLIB::SINTTOFP_I128_F32, "__hexagon_floattisf");
1612 setLibcallName(RTLIB::FPTOUINT_F32_I128, "__hexagon_fixunssfti");
1613 setLibcallName(RTLIB::FPTOUINT_F64_I128, "__hexagon_fixunsdfti");
1614 setLibcallName(RTLIB::FPTOSINT_F32_I128, "__hexagon_fixsfti");
1615 setLibcallName(RTLIB::FPTOSINT_F64_I128, "__hexagon_fixdfti");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001616
1617 if (IsV4) {
1618 // Handle single-precision floating point operations on V4.
Benjamin Kramera37c8092015-04-25 14:46:46 +00001619 if (FastMath) {
1620 setLibcallName(RTLIB::ADD_F32, "__hexagon_fast_addsf3");
1621 setLibcallName(RTLIB::SUB_F32, "__hexagon_fast_subsf3");
1622 setLibcallName(RTLIB::MUL_F32, "__hexagon_fast_mulsf3");
1623 setLibcallName(RTLIB::OGT_F32, "__hexagon_fast_gtsf2");
1624 setLibcallName(RTLIB::OLT_F32, "__hexagon_fast_ltsf2");
1625 // Double-precision compares.
1626 setLibcallName(RTLIB::OGT_F64, "__hexagon_fast_gtdf2");
1627 setLibcallName(RTLIB::OLT_F64, "__hexagon_fast_ltdf2");
1628 } else {
1629 setLibcallName(RTLIB::ADD_F32, "__hexagon_addsf3");
1630 setLibcallName(RTLIB::SUB_F32, "__hexagon_subsf3");
1631 setLibcallName(RTLIB::MUL_F32, "__hexagon_mulsf3");
1632 setLibcallName(RTLIB::OGT_F32, "__hexagon_gtsf2");
1633 setLibcallName(RTLIB::OLT_F32, "__hexagon_ltsf2");
1634 // Double-precision compares.
1635 setLibcallName(RTLIB::OGT_F64, "__hexagon_gtdf2");
1636 setLibcallName(RTLIB::OLT_F64, "__hexagon_ltdf2");
1637 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001638 }
1639
1640 // This is the only fast library function for sqrtd.
1641 if (FastMath)
Benjamin Kramera37c8092015-04-25 14:46:46 +00001642 setLibcallName(RTLIB::SQRT_F64, "__hexagon_fast2_sqrtdf2");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001643
Benjamin Kramera37c8092015-04-25 14:46:46 +00001644 // Prefix is: nothing for "slow-math",
1645 // "fast2_" for V4 fast-math and V5+ fast-math double-precision
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001646 // (actually, keep fast-math and fast-math2 separate for now)
Benjamin Kramera37c8092015-04-25 14:46:46 +00001647 if (FastMath) {
1648 setLibcallName(RTLIB::ADD_F64, "__hexagon_fast_adddf3");
1649 setLibcallName(RTLIB::SUB_F64, "__hexagon_fast_subdf3");
1650 setLibcallName(RTLIB::MUL_F64, "__hexagon_fast_muldf3");
1651 setLibcallName(RTLIB::DIV_F64, "__hexagon_fast_divdf3");
1652 // Calling __hexagon_fast2_divsf3 with fast-math on V5 (ok).
1653 setLibcallName(RTLIB::DIV_F32, "__hexagon_fast_divsf3");
1654 } else {
1655 setLibcallName(RTLIB::ADD_F64, "__hexagon_adddf3");
1656 setLibcallName(RTLIB::SUB_F64, "__hexagon_subdf3");
1657 setLibcallName(RTLIB::MUL_F64, "__hexagon_muldf3");
1658 setLibcallName(RTLIB::DIV_F64, "__hexagon_divdf3");
1659 setLibcallName(RTLIB::DIV_F32, "__hexagon_divsf3");
1660 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001661
1662 if (Subtarget.hasV5TOps()) {
1663 if (FastMath)
Benjamin Kramera37c8092015-04-25 14:46:46 +00001664 setLibcallName(RTLIB::SQRT_F32, "__hexagon_fast2_sqrtf");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001665 else
Benjamin Kramera37c8092015-04-25 14:46:46 +00001666 setLibcallName(RTLIB::SQRT_F32, "__hexagon_sqrtf");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001667 } else {
1668 // V4
Benjamin Kramera37c8092015-04-25 14:46:46 +00001669 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__hexagon_floatsisf");
1670 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__hexagon_floatsidf");
1671 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__hexagon_floatdisf");
1672 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__hexagon_floatdidf");
1673 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__hexagon_floatunsisf");
1674 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__hexagon_floatunsidf");
1675 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__hexagon_floatundisf");
1676 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__hexagon_floatundidf");
1677 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__hexagon_fixunssfsi");
1678 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__hexagon_fixunssfdi");
1679 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__hexagon_fixunsdfsi");
1680 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__hexagon_fixunsdfdi");
1681 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__hexagon_fixsfsi");
1682 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__hexagon_fixsfdi");
1683 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__hexagon_fixdfsi");
1684 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__hexagon_fixdfdi");
1685 setLibcallName(RTLIB::FPEXT_F32_F64, "__hexagon_extendsfdf2");
1686 setLibcallName(RTLIB::FPROUND_F64_F32, "__hexagon_truncdfsf2");
1687 setLibcallName(RTLIB::OEQ_F32, "__hexagon_eqsf2");
1688 setLibcallName(RTLIB::OEQ_F64, "__hexagon_eqdf2");
1689 setLibcallName(RTLIB::OGE_F32, "__hexagon_gesf2");
1690 setLibcallName(RTLIB::OGE_F64, "__hexagon_gedf2");
1691 setLibcallName(RTLIB::OLE_F32, "__hexagon_lesf2");
1692 setLibcallName(RTLIB::OLE_F64, "__hexagon_ledf2");
1693 setLibcallName(RTLIB::UNE_F32, "__hexagon_nesf2");
1694 setLibcallName(RTLIB::UNE_F64, "__hexagon_nedf2");
1695 setLibcallName(RTLIB::UO_F32, "__hexagon_unordsf2");
1696 setLibcallName(RTLIB::UO_F64, "__hexagon_unorddf2");
1697 setLibcallName(RTLIB::O_F32, "__hexagon_unordsf2");
1698 setLibcallName(RTLIB::O_F64, "__hexagon_unorddf2");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001699 }
1700
1701 // These cause problems when the shift amount is non-constant.
1702 setLibcallName(RTLIB::SHL_I128, nullptr);
1703 setLibcallName(RTLIB::SRL_I128, nullptr);
1704 setLibcallName(RTLIB::SRA_I128, nullptr);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001705}
1706
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001707
1708const char* HexagonTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00001709 switch ((HexagonISD::NodeType)Opcode) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001710 case HexagonISD::ALLOCA: return "HexagonISD::ALLOCA";
1711 case HexagonISD::ARGEXTEND: return "HexagonISD::ARGEXTEND";
1712 case HexagonISD::AT_GOT: return "HexagonISD::AT_GOT";
1713 case HexagonISD::AT_PCREL: return "HexagonISD::AT_PCREL";
1714 case HexagonISD::BARRIER: return "HexagonISD::BARRIER";
1715 case HexagonISD::BR_JT: return "HexagonISD::BR_JT";
1716 case HexagonISD::CALLR: return "HexagonISD::CALLR";
1717 case HexagonISD::CALLv3nr: return "HexagonISD::CALLv3nr";
1718 case HexagonISD::CALLv3: return "HexagonISD::CALLv3";
1719 case HexagonISD::COMBINE: return "HexagonISD::COMBINE";
1720 case HexagonISD::CONST32_GP: return "HexagonISD::CONST32_GP";
1721 case HexagonISD::CONST32: return "HexagonISD::CONST32";
1722 case HexagonISD::CP: return "HexagonISD::CP";
1723 case HexagonISD::DCFETCH: return "HexagonISD::DCFETCH";
1724 case HexagonISD::EH_RETURN: return "HexagonISD::EH_RETURN";
1725 case HexagonISD::EXTRACTU: return "HexagonISD::EXTRACTU";
1726 case HexagonISD::EXTRACTURP: return "HexagonISD::EXTRACTURP";
1727 case HexagonISD::FCONST32: return "HexagonISD::FCONST32";
1728 case HexagonISD::INSERT: return "HexagonISD::INSERT";
1729 case HexagonISD::INSERTRP: return "HexagonISD::INSERTRP";
1730 case HexagonISD::JT: return "HexagonISD::JT";
1731 case HexagonISD::PACKHL: return "HexagonISD::PACKHL";
1732 case HexagonISD::PIC_ADD: return "HexagonISD::PIC_ADD";
1733 case HexagonISD::POPCOUNT: return "HexagonISD::POPCOUNT";
1734 case HexagonISD::RET_FLAG: return "HexagonISD::RET_FLAG";
1735 case HexagonISD::SHUFFEB: return "HexagonISD::SHUFFEB";
1736 case HexagonISD::SHUFFEH: return "HexagonISD::SHUFFEH";
1737 case HexagonISD::SHUFFOB: return "HexagonISD::SHUFFOB";
1738 case HexagonISD::SHUFFOH: return "HexagonISD::SHUFFOH";
1739 case HexagonISD::TC_RETURN: return "HexagonISD::TC_RETURN";
1740 case HexagonISD::VCMPBEQ: return "HexagonISD::VCMPBEQ";
1741 case HexagonISD::VCMPBGT: return "HexagonISD::VCMPBGT";
1742 case HexagonISD::VCMPBGTU: return "HexagonISD::VCMPBGTU";
1743 case HexagonISD::VCMPHEQ: return "HexagonISD::VCMPHEQ";
1744 case HexagonISD::VCMPHGT: return "HexagonISD::VCMPHGT";
1745 case HexagonISD::VCMPHGTU: return "HexagonISD::VCMPHGTU";
1746 case HexagonISD::VCMPWEQ: return "HexagonISD::VCMPWEQ";
1747 case HexagonISD::VCMPWGT: return "HexagonISD::VCMPWGT";
1748 case HexagonISD::VCMPWGTU: return "HexagonISD::VCMPWGTU";
1749 case HexagonISD::VSHLH: return "HexagonISD::VSHLH";
1750 case HexagonISD::VSHLW: return "HexagonISD::VSHLW";
1751 case HexagonISD::VSPLATB: return "HexagonISD::VSPLTB";
1752 case HexagonISD::VSPLATH: return "HexagonISD::VSPLATH";
1753 case HexagonISD::VSRAH: return "HexagonISD::VSRAH";
1754 case HexagonISD::VSRAW: return "HexagonISD::VSRAW";
1755 case HexagonISD::VSRLH: return "HexagonISD::VSRLH";
1756 case HexagonISD::VSRLW: return "HexagonISD::VSRLW";
1757 case HexagonISD::VSXTBH: return "HexagonISD::VSXTBH";
1758 case HexagonISD::VSXTBW: return "HexagonISD::VSXTBW";
Matthias Braund04893f2015-05-07 21:33:59 +00001759 case HexagonISD::OP_END: break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001760 }
Matthias Braund04893f2015-05-07 21:33:59 +00001761 return nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001762}
1763
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001764bool HexagonTargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001765 EVT MTy1 = EVT::getEVT(Ty1);
1766 EVT MTy2 = EVT::getEVT(Ty2);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001767 if (!MTy1.isSimple() || !MTy2.isSimple())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001768 return false;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001769 return (MTy1.getSimpleVT() == MVT::i64) && (MTy2.getSimpleVT() == MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001770}
1771
1772bool HexagonTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001773 if (!VT1.isSimple() || !VT2.isSimple())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001774 return false;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001775 return (VT1.getSimpleVT() == MVT::i64) && (VT2.getSimpleVT() == MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001776}
1777
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001778// shouldExpandBuildVectorWithShuffles
1779// Should we expand the build vector with shuffles?
1780bool
1781HexagonTargetLowering::shouldExpandBuildVectorWithShuffles(EVT VT,
1782 unsigned DefinedValues) const {
1783
1784 // Hexagon vector shuffle operates on element sizes of bytes or halfwords
1785 EVT EltVT = VT.getVectorElementType();
1786 int EltBits = EltVT.getSizeInBits();
1787 if ((EltBits != 8) && (EltBits != 16))
1788 return false;
1789
1790 return TargetLowering::shouldExpandBuildVectorWithShuffles(VT, DefinedValues);
1791}
1792
1793// LowerVECTOR_SHUFFLE - Lower a vector shuffle (V1, V2, V3). V1 and
1794// V2 are the two vectors to select data from, V3 is the permutation.
1795static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
1796 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
1797 SDValue V1 = Op.getOperand(0);
1798 SDValue V2 = Op.getOperand(1);
1799 SDLoc dl(Op);
1800 EVT VT = Op.getValueType();
1801
1802 if (V2.getOpcode() == ISD::UNDEF)
1803 V2 = V1;
1804
1805 if (SVN->isSplat()) {
1806 int Lane = SVN->getSplatIndex();
1807 if (Lane == -1) Lane = 0;
1808
1809 // Test if V1 is a SCALAR_TO_VECTOR.
1810 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR)
1811 return createSplat(DAG, dl, VT, V1.getOperand(0));
1812
1813 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
1814 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
1815 // reaches it).
1816 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
1817 !isa<ConstantSDNode>(V1.getOperand(0))) {
1818 bool IsScalarToVector = true;
1819 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
1820 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
1821 IsScalarToVector = false;
1822 break;
1823 }
1824 if (IsScalarToVector)
1825 return createSplat(DAG, dl, VT, V1.getOperand(0));
1826 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001827 return createSplat(DAG, dl, VT, DAG.getConstant(Lane, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001828 }
1829
1830 // FIXME: We need to support more general vector shuffles. See
1831 // below the comment from the ARM backend that deals in the general
1832 // case with the vector shuffles. For now, let expand handle these.
1833 return SDValue();
1834
1835 // If the shuffle is not directly supported and it has 4 elements, use
1836 // the PerfectShuffle-generated table to synthesize it from other shuffles.
1837}
1838
1839// If BUILD_VECTOR has same base element repeated several times,
1840// report true.
1841static bool isCommonSplatElement(BuildVectorSDNode *BVN) {
1842 unsigned NElts = BVN->getNumOperands();
1843 SDValue V0 = BVN->getOperand(0);
1844
1845 for (unsigned i = 1, e = NElts; i != e; ++i) {
1846 if (BVN->getOperand(i) != V0)
1847 return false;
1848 }
1849 return true;
1850}
1851
1852// LowerVECTOR_SHIFT - Lower a vector shift. Try to convert
1853// <VT> = SHL/SRA/SRL <VT> by <VT> to Hexagon specific
1854// <VT> = SHL/SRA/SRL <VT> by <IT/i32>.
1855static SDValue LowerVECTOR_SHIFT(SDValue Op, SelectionDAG &DAG) {
1856 BuildVectorSDNode *BVN = 0;
1857 SDValue V1 = Op.getOperand(0);
1858 SDValue V2 = Op.getOperand(1);
1859 SDValue V3;
1860 SDLoc dl(Op);
1861 EVT VT = Op.getValueType();
1862
1863 if ((BVN = dyn_cast<BuildVectorSDNode>(V1.getNode())) &&
1864 isCommonSplatElement(BVN))
1865 V3 = V2;
1866 else if ((BVN = dyn_cast<BuildVectorSDNode>(V2.getNode())) &&
1867 isCommonSplatElement(BVN))
1868 V3 = V1;
1869 else
1870 return SDValue();
1871
1872 SDValue CommonSplat = BVN->getOperand(0);
1873 SDValue Result;
1874
1875 if (VT.getSimpleVT() == MVT::v4i16) {
1876 switch (Op.getOpcode()) {
1877 case ISD::SRA:
1878 Result = DAG.getNode(HexagonISD::VSRAH, dl, VT, V3, CommonSplat);
1879 break;
1880 case ISD::SHL:
1881 Result = DAG.getNode(HexagonISD::VSHLH, dl, VT, V3, CommonSplat);
1882 break;
1883 case ISD::SRL:
1884 Result = DAG.getNode(HexagonISD::VSRLH, dl, VT, V3, CommonSplat);
1885 break;
1886 default:
1887 return SDValue();
1888 }
1889 } else if (VT.getSimpleVT() == MVT::v2i32) {
1890 switch (Op.getOpcode()) {
1891 case ISD::SRA:
1892 Result = DAG.getNode(HexagonISD::VSRAW, dl, VT, V3, CommonSplat);
1893 break;
1894 case ISD::SHL:
1895 Result = DAG.getNode(HexagonISD::VSHLW, dl, VT, V3, CommonSplat);
1896 break;
1897 case ISD::SRL:
1898 Result = DAG.getNode(HexagonISD::VSRLW, dl, VT, V3, CommonSplat);
1899 break;
1900 default:
1901 return SDValue();
1902 }
1903 } else {
1904 return SDValue();
1905 }
1906
1907 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
1908}
1909
1910SDValue
1911HexagonTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
1912 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
1913 SDLoc dl(Op);
1914 EVT VT = Op.getValueType();
1915
1916 unsigned Size = VT.getSizeInBits();
1917
1918 // A vector larger than 64 bits cannot be represented in Hexagon.
1919 // Expand will split the vector.
1920 if (Size > 64)
1921 return SDValue();
1922
1923 APInt APSplatBits, APSplatUndef;
1924 unsigned SplatBitSize;
1925 bool HasAnyUndefs;
1926 unsigned NElts = BVN->getNumOperands();
1927
1928 // Try to generate a SPLAT instruction.
1929 if ((VT.getSimpleVT() == MVT::v4i8 || VT.getSimpleVT() == MVT::v4i16) &&
1930 (BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
1931 HasAnyUndefs, 0, true) && SplatBitSize <= 16)) {
1932 unsigned SplatBits = APSplatBits.getZExtValue();
1933 int32_t SextVal = ((int32_t) (SplatBits << (32 - SplatBitSize)) >>
1934 (32 - SplatBitSize));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001935 return createSplat(DAG, dl, VT, DAG.getConstant(SextVal, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001936 }
1937
1938 // Try to generate COMBINE to build v2i32 vectors.
1939 if (VT.getSimpleVT() == MVT::v2i32) {
1940 SDValue V0 = BVN->getOperand(0);
1941 SDValue V1 = BVN->getOperand(1);
1942
1943 if (V0.getOpcode() == ISD::UNDEF)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001944 V0 = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001945 if (V1.getOpcode() == ISD::UNDEF)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001946 V1 = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001947
1948 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(V0);
1949 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(V1);
1950 // If the element isn't a constant, it is in a register:
1951 // generate a COMBINE Register Register instruction.
1952 if (!C0 || !C1)
1953 return DAG.getNode(HexagonISD::COMBINE, dl, VT, V1, V0);
1954
1955 // If one of the operands is an 8 bit integer constant, generate
1956 // a COMBINE Immediate Immediate instruction.
1957 if (isInt<8>(C0->getSExtValue()) ||
1958 isInt<8>(C1->getSExtValue()))
1959 return DAG.getNode(HexagonISD::COMBINE, dl, VT, V1, V0);
1960 }
1961
1962 // Try to generate a S2_packhl to build v2i16 vectors.
1963 if (VT.getSimpleVT() == MVT::v2i16) {
1964 for (unsigned i = 0, e = NElts; i != e; ++i) {
1965 if (BVN->getOperand(i).getOpcode() == ISD::UNDEF)
1966 continue;
1967 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(BVN->getOperand(i));
1968 // If the element isn't a constant, it is in a register:
1969 // generate a S2_packhl instruction.
1970 if (!Cst) {
1971 SDValue pack = DAG.getNode(HexagonISD::PACKHL, dl, MVT::v4i16,
1972 BVN->getOperand(1), BVN->getOperand(0));
1973
1974 return DAG.getTargetExtractSubreg(Hexagon::subreg_loreg, dl, MVT::v2i16,
1975 pack);
1976 }
1977 }
1978 }
1979
1980 // In the general case, generate a CONST32 or a CONST64 for constant vectors,
1981 // and insert_vector_elt for all the other cases.
1982 uint64_t Res = 0;
1983 unsigned EltSize = Size / NElts;
1984 SDValue ConstVal;
1985 uint64_t Mask = ~uint64_t(0ULL) >> (64 - EltSize);
1986 bool HasNonConstantElements = false;
1987
1988 for (unsigned i = 0, e = NElts; i != e; ++i) {
1989 // LLVM's BUILD_VECTOR operands are in Little Endian mode, whereas Hexagon's
1990 // combine, const64, etc. are Big Endian.
1991 unsigned OpIdx = NElts - i - 1;
1992 SDValue Operand = BVN->getOperand(OpIdx);
1993 if (Operand.getOpcode() == ISD::UNDEF)
1994 continue;
1995
1996 int64_t Val = 0;
1997 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Operand))
1998 Val = Cst->getSExtValue();
1999 else
2000 HasNonConstantElements = true;
2001
2002 Val &= Mask;
2003 Res = (Res << EltSize) | Val;
2004 }
2005
2006 if (Size == 64)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002007 ConstVal = DAG.getConstant(Res, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002008 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002009 ConstVal = DAG.getConstant(Res, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002010
2011 // When there are non constant operands, add them with INSERT_VECTOR_ELT to
2012 // ConstVal, the constant part of the vector.
2013 if (HasNonConstantElements) {
2014 EVT EltVT = VT.getVectorElementType();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002015 SDValue Width = DAG.getConstant(EltVT.getSizeInBits(), dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002016 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002017 DAG.getConstant(32, dl, MVT::i64));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002018
2019 for (unsigned i = 0, e = NElts; i != e; ++i) {
2020 // LLVM's BUILD_VECTOR operands are in Little Endian mode, whereas Hexagon
2021 // is Big Endian.
2022 unsigned OpIdx = NElts - i - 1;
2023 SDValue Operand = BVN->getOperand(OpIdx);
Benjamin Kramer619c4e52015-04-10 11:24:51 +00002024 if (isa<ConstantSDNode>(Operand))
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002025 // This operand is already in ConstVal.
2026 continue;
2027
2028 if (VT.getSizeInBits() == 64 &&
2029 Operand.getValueType().getSizeInBits() == 32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002030 SDValue C = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002031 Operand = DAG.getNode(HexagonISD::COMBINE, dl, VT, C, Operand);
2032 }
2033
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002034 SDValue Idx = DAG.getConstant(OpIdx, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002035 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i64, Idx, Width);
2036 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2037 const SDValue Ops[] = {ConstVal, Operand, Combined};
2038
2039 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002040 ConstVal = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002041 else
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002042 ConstVal = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002043 }
2044 }
2045
2046 return DAG.getNode(ISD::BITCAST, dl, VT, ConstVal);
2047}
2048
2049SDValue
2050HexagonTargetLowering::LowerCONCAT_VECTORS(SDValue Op,
2051 SelectionDAG &DAG) const {
2052 SDLoc dl(Op);
2053 EVT VT = Op.getValueType();
2054 unsigned NElts = Op.getNumOperands();
2055 SDValue Vec = Op.getOperand(0);
2056 EVT VecVT = Vec.getValueType();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002057 SDValue Width = DAG.getConstant(VecVT.getSizeInBits(), dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002058 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002059 DAG.getConstant(32, dl, MVT::i64));
2060 SDValue ConstVal = DAG.getConstant(0, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002061
2062 ConstantSDNode *W = dyn_cast<ConstantSDNode>(Width);
2063 ConstantSDNode *S = dyn_cast<ConstantSDNode>(Shifted);
2064
2065 if ((VecVT.getSimpleVT() == MVT::v2i16) && (NElts == 2) && W && S) {
2066 if ((W->getZExtValue() == 32) && ((S->getZExtValue() >> 32) == 32)) {
2067 // We are trying to concat two v2i16 to a single v4i16.
2068 SDValue Vec0 = Op.getOperand(1);
2069 SDValue Combined = DAG.getNode(HexagonISD::COMBINE, dl, VT, Vec0, Vec);
2070 return DAG.getNode(ISD::BITCAST, dl, VT, Combined);
2071 }
2072 }
2073
2074 if ((VecVT.getSimpleVT() == MVT::v4i8) && (NElts == 2) && W && S) {
2075 if ((W->getZExtValue() == 32) && ((S->getZExtValue() >> 32) == 32)) {
2076 // We are trying to concat two v4i8 to a single v8i8.
2077 SDValue Vec0 = Op.getOperand(1);
2078 SDValue Combined = DAG.getNode(HexagonISD::COMBINE, dl, VT, Vec0, Vec);
2079 return DAG.getNode(ISD::BITCAST, dl, VT, Combined);
2080 }
2081 }
2082
2083 for (unsigned i = 0, e = NElts; i != e; ++i) {
2084 unsigned OpIdx = NElts - i - 1;
2085 SDValue Operand = Op.getOperand(OpIdx);
2086
2087 if (VT.getSizeInBits() == 64 &&
2088 Operand.getValueType().getSizeInBits() == 32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002089 SDValue C = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002090 Operand = DAG.getNode(HexagonISD::COMBINE, dl, VT, C, Operand);
2091 }
2092
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002093 SDValue Idx = DAG.getConstant(OpIdx, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002094 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i64, Idx, Width);
2095 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2096 const SDValue Ops[] = {ConstVal, Operand, Combined};
2097
2098 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002099 ConstVal = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002100 else
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002101 ConstVal = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002102 }
2103
2104 return DAG.getNode(ISD::BITCAST, dl, VT, ConstVal);
2105}
2106
2107SDValue
2108HexagonTargetLowering::LowerEXTRACT_VECTOR(SDValue Op,
2109 SelectionDAG &DAG) const {
2110 EVT VT = Op.getValueType();
2111 int VTN = VT.isVector() ? VT.getVectorNumElements() : 1;
2112 SDLoc dl(Op);
2113 SDValue Idx = Op.getOperand(1);
2114 SDValue Vec = Op.getOperand(0);
2115 EVT VecVT = Vec.getValueType();
2116 EVT EltVT = VecVT.getVectorElementType();
2117 int EltSize = EltVT.getSizeInBits();
2118 SDValue Width = DAG.getConstant(Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT ?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002119 EltSize : VTN * EltSize, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002120
2121 // Constant element number.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002122 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Idx)) {
2123 uint64_t X = CI->getZExtValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002124 SDValue Offset = DAG.getConstant(X * EltSize, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002125 const SDValue Ops[] = {Vec, Width, Offset};
2126
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002127 ConstantSDNode *CW = dyn_cast<ConstantSDNode>(Width);
2128 assert(CW && "Non constant width in LowerEXTRACT_VECTOR");
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002129
2130 SDValue N;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002131 MVT SVT = VecVT.getSimpleVT();
2132 uint64_t W = CW->getZExtValue();
2133
2134 if (W == 32) {
2135 // Translate this node into EXTRACT_SUBREG.
2136 unsigned Subreg = (X == 0) ? Hexagon::subreg_loreg : 0;
2137
2138 if (X == 0)
2139 Subreg = Hexagon::subreg_loreg;
2140 else if (SVT == MVT::v2i32 && X == 1)
2141 Subreg = Hexagon::subreg_hireg;
2142 else if (SVT == MVT::v4i16 && X == 2)
2143 Subreg = Hexagon::subreg_hireg;
2144 else if (SVT == MVT::v8i8 && X == 4)
2145 Subreg = Hexagon::subreg_hireg;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002146 else
2147 llvm_unreachable("Bad offset");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002148 N = DAG.getTargetExtractSubreg(Subreg, dl, MVT::i32, Vec);
2149
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002150 } else if (VecVT.getSizeInBits() == 32) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002151 N = DAG.getNode(HexagonISD::EXTRACTU, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002152 } else {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002153 N = DAG.getNode(HexagonISD::EXTRACTU, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002154 if (VT.getSizeInBits() == 32)
2155 N = DAG.getTargetExtractSubreg(Hexagon::subreg_loreg, dl, MVT::i32, N);
2156 }
2157
2158 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2159 }
2160
2161 // Variable element number.
2162 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i32, Idx,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002163 DAG.getConstant(EltSize, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002164 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002165 DAG.getConstant(32, dl, MVT::i64));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002166 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2167
2168 const SDValue Ops[] = {Vec, Combined};
2169
2170 SDValue N;
2171 if (VecVT.getSizeInBits() == 32) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002172 N = DAG.getNode(HexagonISD::EXTRACTURP, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002173 } else {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002174 N = DAG.getNode(HexagonISD::EXTRACTURP, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002175 if (VT.getSizeInBits() == 32)
2176 N = DAG.getTargetExtractSubreg(Hexagon::subreg_loreg, dl, MVT::i32, N);
2177 }
2178 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2179}
2180
2181SDValue
2182HexagonTargetLowering::LowerINSERT_VECTOR(SDValue Op,
2183 SelectionDAG &DAG) const {
2184 EVT VT = Op.getValueType();
2185 int VTN = VT.isVector() ? VT.getVectorNumElements() : 1;
2186 SDLoc dl(Op);
2187 SDValue Vec = Op.getOperand(0);
2188 SDValue Val = Op.getOperand(1);
2189 SDValue Idx = Op.getOperand(2);
2190 EVT VecVT = Vec.getValueType();
2191 EVT EltVT = VecVT.getVectorElementType();
2192 int EltSize = EltVT.getSizeInBits();
2193 SDValue Width = DAG.getConstant(Op.getOpcode() == ISD::INSERT_VECTOR_ELT ?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002194 EltSize : VTN * EltSize, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002195
2196 if (ConstantSDNode *C = cast<ConstantSDNode>(Idx)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002197 SDValue Offset = DAG.getConstant(C->getSExtValue() * EltSize, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002198 const SDValue Ops[] = {Vec, Val, Width, Offset};
2199
2200 SDValue N;
2201 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002202 N = DAG.getNode(HexagonISD::INSERT, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002203 else
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002204 N = DAG.getNode(HexagonISD::INSERT, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002205
2206 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2207 }
2208
2209 // Variable element number.
2210 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i32, Idx,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002211 DAG.getConstant(EltSize, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002212 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002213 DAG.getConstant(32, dl, MVT::i64));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002214 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2215
2216 if (VT.getSizeInBits() == 64 &&
2217 Val.getValueType().getSizeInBits() == 32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002218 SDValue C = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002219 Val = DAG.getNode(HexagonISD::COMBINE, dl, VT, C, Val);
2220 }
2221
2222 const SDValue Ops[] = {Vec, Val, Combined};
2223
2224 SDValue N;
2225 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002226 N = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002227 else
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002228 N = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002229
2230 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2231}
2232
Tim Northovera4415852013-08-06 09:12:35 +00002233bool
2234HexagonTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
2235 // Assuming the caller does not have either a signext or zeroext modifier, and
2236 // only one value is accepted, any reasonable truncation is allowed.
2237 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
2238 return false;
2239
2240 // FIXME: in principle up to 64-bit could be made safe, but it would be very
2241 // fragile at the moment: any support for multiple value returns would be
2242 // liable to disallow tail calls involving i64 -> iN truncation in many cases.
2243 return Ty1->getPrimitiveSizeInBits() <= 32;
2244}
2245
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002246SDValue
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002247HexagonTargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
2248 SDValue Chain = Op.getOperand(0);
2249 SDValue Offset = Op.getOperand(1);
2250 SDValue Handler = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002251 SDLoc dl(Op);
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002252
2253 // Mark function as containing a call to EH_RETURN.
2254 HexagonMachineFunctionInfo *FuncInfo =
2255 DAG.getMachineFunction().getInfo<HexagonMachineFunctionInfo>();
2256 FuncInfo->setHasEHReturn();
2257
2258 unsigned OffsetReg = Hexagon::R28;
2259
2260 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(),
2261 DAG.getRegister(Hexagon::R30, getPointerTy()),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002262 DAG.getIntPtrConstant(4, dl));
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002263 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
2264 false, false, 0);
2265 Chain = DAG.getCopyToReg(Chain, dl, OffsetReg, Offset);
2266
2267 // Not needed we already use it as explict input to EH_RETURN.
2268 // MF.getRegInfo().addLiveOut(OffsetReg);
2269
2270 return DAG.getNode(HexagonISD::EH_RETURN, dl, MVT::Other, Chain);
2271}
2272
2273SDValue
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002274HexagonTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002275 unsigned Opc = Op.getOpcode();
2276 switch (Opc) {
2277 default:
2278#ifndef NDEBUG
2279 Op.getNode()->dumpr(&DAG);
2280 if (Opc > HexagonISD::OP_BEGIN && Opc < HexagonISD::OP_END)
2281 errs() << "Check for a non-legal type in this operation\n";
2282#endif
2283 llvm_unreachable("Should not custom lower this!");
2284 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
2285 case ISD::INSERT_SUBVECTOR: return LowerINSERT_VECTOR(Op, DAG);
2286 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR(Op, DAG);
2287 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_VECTOR(Op, DAG);
2288 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR(Op, DAG);
2289 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
2290 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002291 case ISD::SRA:
2292 case ISD::SHL:
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002293 case ISD::SRL: return LowerVECTOR_SHIFT(Op, DAG);
2294 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
2295 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
2296 // Frame & Return address. Currently unimplemented.
2297 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
2298 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
2299 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
2300 case ISD::GlobalAddress: return LowerGLOBALADDRESS(Op, DAG);
2301 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
2302 case ISD::VASTART: return LowerVASTART(Op, DAG);
2303 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002304 // Custom lower some vector loads.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002305 case ISD::LOAD: return LowerLOAD(Op, DAG);
2306 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
2307 case ISD::SETCC: return LowerSETCC(Op, DAG);
2308 case ISD::VSELECT: return LowerVSELECT(Op, DAG);
2309 case ISD::CTPOP: return LowerCTPOP(Op, DAG);
2310 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
2311 case ISD::INLINEASM: return LowerINLINEASM(Op, DAG);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002312 }
2313}
2314
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002315MachineBasicBlock *
2316HexagonTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
2317 MachineBasicBlock *BB)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00002318 const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002319 switch (MI->getOpcode()) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00002320 case Hexagon::ALLOCA: {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002321 MachineFunction *MF = BB->getParent();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00002322 auto *FuncInfo = MF->getInfo<HexagonMachineFunctionInfo>();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002323 FuncInfo->addAllocaAdjustInst(MI);
2324 return BB;
2325 }
Craig Toppere55c5562012-02-07 02:50:20 +00002326 default: llvm_unreachable("Unexpected instr type to insert");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002327 } // switch
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002328}
2329
2330//===----------------------------------------------------------------------===//
2331// Inline Assembly Support
2332//===----------------------------------------------------------------------===//
2333
Eric Christopher11e4df72015-02-26 22:38:43 +00002334std::pair<unsigned, const TargetRegisterClass *>
2335HexagonTargetLowering::getRegForInlineAsmConstraint(
2336 const TargetRegisterInfo *TRI, const std::string &Constraint,
2337 MVT VT) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002338 if (Constraint.size() == 1) {
2339 switch (Constraint[0]) {
2340 case 'r': // R0-R31
Chad Rosier295bd432013-06-22 18:37:38 +00002341 switch (VT.SimpleTy) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002342 default:
Craig Toppere55c5562012-02-07 02:50:20 +00002343 llvm_unreachable("getRegForInlineAsmConstraint Unhandled data type");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002344 case MVT::i32:
2345 case MVT::i16:
2346 case MVT::i8:
Sirish Pande69295b82012-05-10 20:20:25 +00002347 case MVT::f32:
Craig Topperc7242e02012-04-20 07:30:17 +00002348 return std::make_pair(0U, &Hexagon::IntRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002349 case MVT::i64:
Sirish Pande69295b82012-05-10 20:20:25 +00002350 case MVT::f64:
Craig Topperc7242e02012-04-20 07:30:17 +00002351 return std::make_pair(0U, &Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002352 }
2353 default:
Craig Toppere55c5562012-02-07 02:50:20 +00002354 llvm_unreachable("Unknown asm register class");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002355 }
2356 }
2357
Eric Christopher11e4df72015-02-26 22:38:43 +00002358 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002359}
2360
Sirish Pande69295b82012-05-10 20:20:25 +00002361/// isFPImmLegal - Returns true if the target can instruction select the
2362/// specified FP immediate natively. If false, the legalizer will
2363/// materialize the FP immediate as a load from a constant pool.
2364bool HexagonTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002365 return Subtarget.hasV5TOps();
Sirish Pande69295b82012-05-10 20:20:25 +00002366}
2367
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002368/// isLegalAddressingMode - Return true if the addressing mode represented by
2369/// AM is legal for this target, for a load/store of the specified type.
2370bool HexagonTargetLowering::isLegalAddressingMode(const AddrMode &AM,
2371 Type *Ty) const {
2372 // Allows a signed-extended 11-bit immediate field.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002373 if (AM.BaseOffs <= -(1LL << 13) || AM.BaseOffs >= (1LL << 13)-1)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002374 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002375
2376 // No global is ever allowed as a base.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002377 if (AM.BaseGV)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002378 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002379
2380 int Scale = AM.Scale;
2381 if (Scale < 0) Scale = -Scale;
2382 switch (Scale) {
2383 case 0: // No scale reg, "r+i", "r", or just "i".
2384 break;
2385 default: // No scaled addressing mode.
2386 return false;
2387 }
2388 return true;
2389}
2390
2391/// isLegalICmpImmediate - Return true if the specified immediate is legal
2392/// icmp immediate, that is the target has icmp instructions which can compare
2393/// a register against the immediate without having to materialize the
2394/// immediate into a register.
2395bool HexagonTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
2396 return Imm >= -512 && Imm <= 511;
2397}
2398
2399/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2400/// for tail call optimization. Targets which want to do tail call
2401/// optimization should implement this function.
2402bool HexagonTargetLowering::IsEligibleForTailCallOptimization(
2403 SDValue Callee,
2404 CallingConv::ID CalleeCC,
2405 bool isVarArg,
2406 bool isCalleeStructRet,
2407 bool isCallerStructRet,
2408 const SmallVectorImpl<ISD::OutputArg> &Outs,
2409 const SmallVectorImpl<SDValue> &OutVals,
2410 const SmallVectorImpl<ISD::InputArg> &Ins,
2411 SelectionDAG& DAG) const {
2412 const Function *CallerF = DAG.getMachineFunction().getFunction();
2413 CallingConv::ID CallerCC = CallerF->getCallingConv();
2414 bool CCMatch = CallerCC == CalleeCC;
2415
2416 // ***************************************************************************
2417 // Look for obvious safe cases to perform tail call optimization that do not
2418 // require ABI changes.
2419 // ***************************************************************************
2420
2421 // If this is a tail call via a function pointer, then don't do it!
2422 if (!(dyn_cast<GlobalAddressSDNode>(Callee))
2423 && !(dyn_cast<ExternalSymbolSDNode>(Callee))) {
2424 return false;
2425 }
2426
2427 // Do not optimize if the calling conventions do not match.
2428 if (!CCMatch)
2429 return false;
2430
2431 // Do not tail call optimize vararg calls.
2432 if (isVarArg)
2433 return false;
2434
2435 // Also avoid tail call optimization if either caller or callee uses struct
2436 // return semantics.
2437 if (isCalleeStructRet || isCallerStructRet)
2438 return false;
2439
2440 // In addition to the cases above, we also disable Tail Call Optimization if
2441 // the calling convention code that at least one outgoing argument needs to
2442 // go on the stack. We cannot check that here because at this point that
2443 // information is not available.
2444 return true;
2445}
Colin LeMahieu025f8602014-12-08 21:19:18 +00002446
2447// Return true when the given node fits in a positive half word.
2448bool llvm::isPositiveHalfWord(SDNode *N) {
2449 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N);
2450 if (CN && CN->getSExtValue() > 0 && isInt<16>(CN->getSExtValue()))
2451 return true;
2452
2453 switch (N->getOpcode()) {
2454 default:
2455 return false;
2456 case ISD::SIGN_EXTEND_INREG:
2457 return true;
2458 }
2459}