blob: 6e9e69f5a2c789d41e162575e366dc671257df92 [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.
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000400 auto Attr =
401 CI->getParent()->getParent()->getFnAttribute("disable-tail-calls");
402 if (!CI->isTailCall() || Attr.getValueAsString() == "true")
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000403 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000404
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000405 return true;
406}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000407
408/// LowerCallResult - Lower the result values of an ISD::CALL into the
409/// appropriate copies out of appropriate physical registers. This assumes that
410/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
411/// being lowered. Returns a SDNode with the same number of values as the
412/// ISD::CALL.
413SDValue
414HexagonTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
415 CallingConv::ID CallConv, bool isVarArg,
416 const
417 SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000418 SDLoc dl, SelectionDAG &DAG,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000419 SmallVectorImpl<SDValue> &InVals,
420 const SmallVectorImpl<SDValue> &OutVals,
421 SDValue Callee) const {
422
423 // Assign locations to each value returned by this call.
424 SmallVector<CCValAssign, 16> RVLocs;
425
Eric Christopherb5217502014-08-06 18:45:26 +0000426 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
427 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000428
429 CCInfo.AnalyzeCallResult(Ins, RetCC_Hexagon);
430
431 // Copy all of the result registers out of their specified physreg.
432 for (unsigned i = 0; i != RVLocs.size(); ++i) {
433 Chain = DAG.getCopyFromReg(Chain, dl,
434 RVLocs[i].getLocReg(),
435 RVLocs[i].getValVT(), InFlag).getValue(1);
436 InFlag = Chain.getValue(2);
437 InVals.push_back(Chain.getValue(0));
438 }
439
440 return Chain;
441}
442
443/// LowerCall - Functions arguments are copied from virtual regs to
444/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
445SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +0000446HexagonTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000447 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +0000448 SelectionDAG &DAG = CLI.DAG;
Craig Topperb94011f2013-07-14 04:42:23 +0000449 SDLoc &dl = CLI.DL;
450 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
451 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
452 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000453 SDValue Chain = CLI.Chain;
454 SDValue Callee = CLI.Callee;
455 bool &isTailCall = CLI.IsTailCall;
456 CallingConv::ID CallConv = CLI.CallConv;
457 bool isVarArg = CLI.IsVarArg;
Colin LeMahieu2e3a26d2015-01-16 17:05:27 +0000458 bool doesNotReturn = CLI.DoesNotReturn;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000459
460 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000461 MachineFunction &MF = DAG.getMachineFunction();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000462
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000463 // Check for varargs.
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000464 int NumNamedVarArgParams = -1;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000465 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Callee))
466 {
Craig Topper062a2ba2014-04-25 05:30:21 +0000467 const Function* CalleeFn = nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000468 Callee = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, MVT::i32);
469 if ((CalleeFn = dyn_cast<Function>(GA->getGlobal())))
470 {
471 // If a function has zero args and is a vararg function, that's
472 // disallowed so it must be an undeclared function. Do not assume
473 // varargs if the callee is undefined.
474 if (CalleeFn->isVarArg() &&
475 CalleeFn->getFunctionType()->getNumParams() != 0) {
476 NumNamedVarArgParams = CalleeFn->getFunctionType()->getNumParams();
477 }
478 }
479 }
480
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000481 // Analyze operands of the call, assigning locations to each operand.
482 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000483 HexagonCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
484 *DAG.getContext(), NumNamedVarArgParams);
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000485
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000486 if (isVarArg)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000487 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon_VarArg);
488 else
489 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon);
490
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000491 auto Attr = MF.getFunction()->getFnAttribute("disable-tail-calls");
492 if (Attr.getValueAsString() == "true")
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000493 isTailCall = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000494
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000495 if (isTailCall) {
496 bool StructAttrFlag = MF.getFunction()->hasStructRetAttr();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000497 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
498 isVarArg, IsStructRet,
499 StructAttrFlag,
500 Outs, OutVals, Ins, DAG);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000501 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000502 CCValAssign &VA = ArgLocs[i];
503 if (VA.isMemLoc()) {
504 isTailCall = false;
505 break;
506 }
507 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000508 DEBUG(dbgs() << (isTailCall ? "Eligible for Tail Call\n"
509 : "Argument must be passed on stack. "
510 "Not eligible for Tail Call\n"));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000511 }
512 // Get a count of how many bytes are to be pushed on the stack.
513 unsigned NumBytes = CCInfo.getNextStackOffset();
514 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
515 SmallVector<SDValue, 8> MemOpChains;
516
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000517 auto &HRI = *Subtarget.getRegisterInfo();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000518 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, HRI.getStackRegister(),
519 getPointerTy());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000520
521 // Walk the register/memloc assignments, inserting copies/loads.
522 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
523 CCValAssign &VA = ArgLocs[i];
524 SDValue Arg = OutVals[i];
525 ISD::ArgFlagsTy Flags = Outs[i].Flags;
526
527 // Promote the value if needed.
528 switch (VA.getLocInfo()) {
529 default:
530 // Loc info must be one of Full, SExt, ZExt, or AExt.
Craig Toppere55c5562012-02-07 02:50:20 +0000531 llvm_unreachable("Unknown loc info!");
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000532 case CCValAssign::BCvt:
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000533 case CCValAssign::Full:
534 break;
535 case CCValAssign::SExt:
536 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
537 break;
538 case CCValAssign::ZExt:
539 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
540 break;
541 case CCValAssign::AExt:
542 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
543 break;
544 }
545
546 if (VA.isMemLoc()) {
547 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000548 SDValue MemAddr = DAG.getConstant(LocMemOffset, dl,
549 StackPtr.getValueType());
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000550 MemAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, MemAddr);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000551 if (Flags.isByVal()) {
552 // The argument is a struct passed by value. According to LLVM, "Arg"
553 // is is pointer.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000554 MemOpChains.push_back(CreateCopyOfByValArgument(Arg, MemAddr, Chain,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000555 Flags, DAG, dl));
556 } else {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000557 MachinePointerInfo LocPI = MachinePointerInfo::getStack(LocMemOffset);
558 SDValue S = DAG.getStore(Chain, dl, Arg, MemAddr, LocPI, false,
559 false, 0);
560 MemOpChains.push_back(S);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000561 }
562 continue;
563 }
564
565 // Arguments that can be passed on register must be kept at RegsToPass
566 // vector.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000567 if (VA.isRegLoc())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000568 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000569 }
570
571 // Transform all store nodes into one single node because all store
572 // nodes are independent of each other.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000573 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +0000574 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000575
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000576 if (!isTailCall) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000577 SDValue C = DAG.getConstant(NumBytes, dl, getPointerTy(), true);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000578 Chain = DAG.getCALLSEQ_START(Chain, C, dl);
579 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000580
581 // Build a sequence of copy-to-reg nodes chained together with token
582 // chain and flag operands which copy the outgoing args into registers.
Benjamin Kramerbde91762012-06-02 10:20:22 +0000583 // The InFlag in necessary since all emitted instructions must be
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000584 // stuck together.
585 SDValue InFlag;
586 if (!isTailCall) {
587 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
588 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
589 RegsToPass[i].second, InFlag);
590 InFlag = Chain.getValue(1);
591 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000592 } else {
593 // For tail calls lower the arguments to the 'real' stack slot.
594 //
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000595 // Force all the incoming stack arguments to be loaded from the stack
596 // before any new outgoing arguments are stored to the stack, because the
597 // outgoing stack slots may alias the incoming argument stack slots, and
598 // the alias isn't otherwise explicit. This is slightly more conservative
599 // than necessary, because it means that each store effectively depends
600 // on every argument instead of just those arguments it would clobber.
601 //
Benjamin Kramerbde91762012-06-02 10:20:22 +0000602 // Do not flag preceding copytoreg stuff together with the following stuff.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000603 InFlag = SDValue();
604 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
605 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
606 RegsToPass[i].second, InFlag);
607 InFlag = Chain.getValue(1);
608 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000609 InFlag = SDValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000610 }
611
612 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
613 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
614 // node so that legalize doesn't hack it.
615 if (flag_aligned_memcpy) {
616 const char *MemcpyName =
617 "__hexagon_memcpy_likely_aligned_min32bytes_mult8bytes";
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000618 Callee = DAG.getTargetExternalSymbol(MemcpyName, getPointerTy());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000619 flag_aligned_memcpy = false;
620 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
621 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy());
622 } else if (ExternalSymbolSDNode *S =
623 dyn_cast<ExternalSymbolSDNode>(Callee)) {
624 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
625 }
626
627 // Returns a chain & a flag for retval copy to use.
628 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
629 SmallVector<SDValue, 8> Ops;
630 Ops.push_back(Chain);
631 Ops.push_back(Callee);
632
633 // Add argument registers to the end of the list so that they are
634 // known live into the call.
635 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
636 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
637 RegsToPass[i].second.getValueType()));
638 }
639
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000640 if (InFlag.getNode())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000641 Ops.push_back(InFlag);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000642
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +0000643 if (isTailCall) {
644 MF.getFrameInfo()->setHasTailCall();
Craig Topper48d114b2014-04-26 18:35:24 +0000645 return DAG.getNode(HexagonISD::TC_RETURN, dl, NodeTys, Ops);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +0000646 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000647
Colin LeMahieu2e3a26d2015-01-16 17:05:27 +0000648 int OpCode = doesNotReturn ? HexagonISD::CALLv3nr : HexagonISD::CALLv3;
649 Chain = DAG.getNode(OpCode, dl, NodeTys, Ops);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000650 InFlag = Chain.getValue(1);
651
652 // Create the CALLSEQ_END node.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000653 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
654 DAG.getIntPtrConstant(0, dl, true), InFlag, dl);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000655 InFlag = Chain.getValue(1);
656
657 // Handle result values, copying them out of physregs into vregs that we
658 // return.
659 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
660 InVals, OutVals, Callee);
661}
662
663static bool getIndexedAddressParts(SDNode *Ptr, EVT VT,
664 bool isSEXTLoad, SDValue &Base,
665 SDValue &Offset, bool &isInc,
666 SelectionDAG &DAG) {
667 if (Ptr->getOpcode() != ISD::ADD)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000668 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000669
670 if (VT == MVT::i64 || VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
671 isInc = (Ptr->getOpcode() == ISD::ADD);
672 Base = Ptr->getOperand(0);
673 Offset = Ptr->getOperand(1);
674 // Ensure that Offset is a constant.
675 return (isa<ConstantSDNode>(Offset));
676 }
677
678 return false;
679}
680
681// TODO: Put this function along with the other isS* functions in
682// HexagonISelDAGToDAG.cpp into a common file. Or better still, use the
Rafael Espindolab90c5f12012-11-21 16:56:33 +0000683// functions defined in HexagonOperands.td.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000684static bool Is_PostInc_S4_Offset(SDNode * S, int ShiftAmount) {
685 ConstantSDNode *N = cast<ConstantSDNode>(S);
686
687 // immS4 predicate - True if the immediate fits in a 4-bit sign extended.
688 // field.
689 int64_t v = (int64_t)N->getSExtValue();
690 int64_t m = 0;
691 if (ShiftAmount > 0) {
692 m = v % ShiftAmount;
693 v = v >> ShiftAmount;
694 }
695 return (v <= 7) && (v >= -8) && (m == 0);
696}
697
698/// getPostIndexedAddressParts - returns true by value, base pointer and
699/// offset pointer and addressing mode by reference if this node can be
700/// combined with a load / store to form a post-indexed load / store.
701bool HexagonTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
702 SDValue &Base,
703 SDValue &Offset,
704 ISD::MemIndexedMode &AM,
705 SelectionDAG &DAG) const
706{
707 EVT VT;
708 SDValue Ptr;
709 bool isSEXTLoad = false;
710
711 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
712 VT = LD->getMemoryVT();
713 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
714 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
715 VT = ST->getMemoryVT();
716 if (ST->getValue().getValueType() == MVT::i64 && ST->isTruncatingStore()) {
717 return false;
718 }
719 } else {
720 return false;
721 }
722
Chad Rosier64dc8aa2012-01-06 20:11:59 +0000723 bool isInc = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000724 bool isLegal = getIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
725 isInc, DAG);
726 // ShiftAmount = number of left-shifted bits in the Hexagon instruction.
727 int ShiftAmount = VT.getSizeInBits() / 16;
728 if (isLegal && Is_PostInc_S4_Offset(Offset.getNode(), ShiftAmount)) {
729 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
730 return true;
731 }
732
733 return false;
734}
735
736SDValue HexagonTargetLowering::LowerINLINEASM(SDValue Op,
737 SelectionDAG &DAG) const {
738 SDNode *Node = Op.getNode();
739 MachineFunction &MF = DAG.getMachineFunction();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000740 auto &FuncInfo = *MF.getInfo<HexagonMachineFunctionInfo>();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000741 switch (Node->getOpcode()) {
742 case ISD::INLINEASM: {
743 unsigned NumOps = Node->getNumOperands();
744 if (Node->getOperand(NumOps-1).getValueType() == MVT::Glue)
745 --NumOps; // Ignore the flag operand.
746
747 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000748 if (FuncInfo.hasClobberLR())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000749 break;
750 unsigned Flags =
751 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
752 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
753 ++i; // Skip the ID value.
754
755 switch (InlineAsm::getKind(Flags)) {
756 default: llvm_unreachable("Bad flags!");
757 case InlineAsm::Kind_RegDef:
758 case InlineAsm::Kind_RegUse:
759 case InlineAsm::Kind_Imm:
760 case InlineAsm::Kind_Clobber:
761 case InlineAsm::Kind_Mem: {
762 for (; NumVals; --NumVals, ++i) {}
763 break;
764 }
765 case InlineAsm::Kind_RegDefEarlyClobber: {
766 for (; NumVals; --NumVals, ++i) {
767 unsigned Reg =
768 cast<RegisterSDNode>(Node->getOperand(i))->getReg();
769
770 // Check it to be lr
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000771 const HexagonRegisterInfo *QRI = Subtarget.getRegisterInfo();
Eric Christopherdbe1cb02014-06-27 00:13:52 +0000772 if (Reg == QRI->getRARegister()) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000773 FuncInfo.setHasClobberLR(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000774 break;
775 }
776 }
777 break;
778 }
779 }
780 }
781 }
782 } // Node->getOpcode
783 return Op;
784}
785
786
787//
788// Taken from the XCore backend.
789//
790SDValue HexagonTargetLowering::
791LowerBR_JT(SDValue Op, SelectionDAG &DAG) const
792{
793 SDValue Chain = Op.getOperand(0);
794 SDValue Table = Op.getOperand(1);
795 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000796 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000797 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
798 unsigned JTI = JT->getIndex();
799 MachineFunction &MF = DAG.getMachineFunction();
800 const MachineJumpTableInfo *MJTI = MF.getJumpTableInfo();
801 SDValue TargetJT = DAG.getTargetJumpTable(JT->getIndex(), MVT::i32);
802
803 // Mark all jump table targets as address taken.
804 const std::vector<MachineJumpTableEntry> &JTE = MJTI->getJumpTables();
805 const std::vector<MachineBasicBlock*> &JTBBs = JTE[JTI].MBBs;
806 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
807 MachineBasicBlock *MBB = JTBBs[i];
808 MBB->setHasAddressTaken();
809 // This line is needed to set the hasAddressTaken flag on the BasicBlock
810 // object.
811 BlockAddress::get(const_cast<BasicBlock *>(MBB->getBasicBlock()));
812 }
813
Colin LeMahieuee776452015-03-10 19:29:53 +0000814 SDValue JumpTableBase = DAG.getNode(HexagonISD::JT, dl,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000815 getPointerTy(), TargetJT);
816 SDValue ShiftIndex = DAG.getNode(ISD::SHL, dl, MVT::i32, Index,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000817 DAG.getConstant(2, dl, MVT::i32));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000818 SDValue JTAddress = DAG.getNode(ISD::ADD, dl, MVT::i32, JumpTableBase,
819 ShiftIndex);
820 SDValue LoadTarget = DAG.getLoad(MVT::i32, dl, Chain, JTAddress,
821 MachinePointerInfo(), false, false, false,
822 0);
823 return DAG.getNode(HexagonISD::BR_JT, dl, MVT::Other, Chain, LoadTarget);
824}
825
826
827SDValue
828HexagonTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
829 SelectionDAG &DAG) const {
830 SDValue Chain = Op.getOperand(0);
831 SDValue Size = Op.getOperand(1);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000832 SDValue Align = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000833 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000834
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000835 ConstantSDNode *AlignConst = dyn_cast<ConstantSDNode>(Align);
836 assert(AlignConst && "Non-constant Align in LowerDYNAMIC_STACKALLOC");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000837
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000838 unsigned A = AlignConst->getSExtValue();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000839 auto &HFI = *Subtarget.getFrameLowering();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000840 // "Zero" means natural stack alignment.
841 if (A == 0)
842 A = HFI.getStackAlignment();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000843
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000844 DEBUG({
Krzysztof Parzyszek9ee04e42015-04-22 17:19:44 +0000845 dbgs () << LLVM_FUNCTION_NAME << " Align: " << A << " Size: ";
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000846 Size.getNode()->dump(&DAG);
847 dbgs() << "\n";
848 });
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000849
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000850 SDValue AC = DAG.getConstant(A, dl, MVT::i32);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000851 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other);
852 return DAG.getNode(HexagonISD::ALLOCA, dl, VTs, Chain, Size, AC);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000853}
854
855SDValue
856HexagonTargetLowering::LowerFormalArguments(SDValue Chain,
857 CallingConv::ID CallConv,
858 bool isVarArg,
859 const
860 SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000861 SDLoc dl, SelectionDAG &DAG,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000862 SmallVectorImpl<SDValue> &InVals)
863const {
864
865 MachineFunction &MF = DAG.getMachineFunction();
866 MachineFrameInfo *MFI = MF.getFrameInfo();
867 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000868 auto &FuncInfo = *MF.getInfo<HexagonMachineFunctionInfo>();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000869
870 // Assign locations to all of the incoming arguments.
871 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000872 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
873 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000874
875 CCInfo.AnalyzeFormalArguments(Ins, CC_Hexagon);
876
877 // For LLVM, in the case when returning a struct by value (>8byte),
878 // the first argument is a pointer that points to the location on caller's
879 // stack where the return value will be stored. For Hexagon, the location on
880 // caller's stack is passed only when the struct size is smaller than (and
881 // equal to) 8 bytes. If not, no address will be passed into callee and
882 // callee return the result direclty through R0/R1.
883
884 SmallVector<SDValue, 4> MemOps;
885
886 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
887 CCValAssign &VA = ArgLocs[i];
888 ISD::ArgFlagsTy Flags = Ins[i].Flags;
889 unsigned ObjSize;
890 unsigned StackLocation;
891 int FI;
892
893 if ( (VA.isRegLoc() && !Flags.isByVal())
894 || (VA.isRegLoc() && Flags.isByVal() && Flags.getByValSize() > 8)) {
895 // Arguments passed in registers
896 // 1. int, long long, ptr args that get allocated in register.
897 // 2. Large struct that gets an register to put its address in.
898 EVT RegVT = VA.getLocVT();
Sirish Pande69295b82012-05-10 20:20:25 +0000899 if (RegVT == MVT::i8 || RegVT == MVT::i16 ||
900 RegVT == MVT::i32 || RegVT == MVT::f32) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000901 unsigned VReg =
Craig Topperc7242e02012-04-20 07:30:17 +0000902 RegInfo.createVirtualRegister(&Hexagon::IntRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000903 RegInfo.addLiveIn(VA.getLocReg(), VReg);
904 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
Colin LeMahieu4379d102015-01-28 22:08:16 +0000905 } else if (RegVT == MVT::i64 || RegVT == MVT::f64) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000906 unsigned VReg =
Craig Topperc7242e02012-04-20 07:30:17 +0000907 RegInfo.createVirtualRegister(&Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000908 RegInfo.addLiveIn(VA.getLocReg(), VReg);
909 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
910 } else {
911 assert (0);
912 }
913 } else if (VA.isRegLoc() && Flags.isByVal() && Flags.getByValSize() <= 8) {
914 assert (0 && "ByValSize must be bigger than 8 bytes");
915 } else {
916 // Sanity check.
917 assert(VA.isMemLoc());
918
919 if (Flags.isByVal()) {
920 // If it's a byval parameter, then we need to compute the
921 // "real" size, not the size of the pointer.
922 ObjSize = Flags.getByValSize();
923 } else {
924 ObjSize = VA.getLocVT().getStoreSizeInBits() >> 3;
925 }
926
927 StackLocation = HEXAGON_LRFP_SIZE + VA.getLocMemOffset();
928 // Create the frame index object for this incoming parameter...
929 FI = MFI->CreateFixedObject(ObjSize, StackLocation, true);
930
931 // Create the SelectionDAG nodes cordl, responding to a load
932 // from this parameter.
933 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
934
935 if (Flags.isByVal()) {
936 // If it's a pass-by-value aggregate, then do not dereference the stack
937 // location. Instead, we should generate a reference to the stack
938 // location.
939 InVals.push_back(FIN);
940 } else {
941 InVals.push_back(DAG.getLoad(VA.getLocVT(), dl, Chain, FIN,
942 MachinePointerInfo(), false, false,
943 false, 0));
944 }
945 }
946 }
947
948 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +0000949 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000950
951 if (isVarArg) {
952 // This will point to the next argument passed via stack.
953 int FrameIndex = MFI->CreateFixedObject(Hexagon_PointerSize,
954 HEXAGON_LRFP_SIZE +
955 CCInfo.getNextStackOffset(),
956 true);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000957 FuncInfo.setVarArgsFrameIndex(FrameIndex);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000958 }
959
960 return Chain;
961}
962
963SDValue
964HexagonTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
965 // VASTART stores the address of the VarArgsFrameIndex slot into the
966 // memory location argument.
967 MachineFunction &MF = DAG.getMachineFunction();
968 HexagonMachineFunctionInfo *QFI = MF.getInfo<HexagonMachineFunctionInfo>();
969 SDValue Addr = DAG.getFrameIndex(QFI->getVarArgsFrameIndex(), MVT::i32);
970 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000971 return DAG.getStore(Op.getOperand(0), SDLoc(Op), Addr,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000972 Op.getOperand(1), MachinePointerInfo(SV), false,
973 false, 0);
974}
975
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000976// Creates a SPLAT instruction for a constant value VAL.
977static SDValue createSplat(SelectionDAG &DAG, SDLoc dl, EVT VT, SDValue Val) {
978 if (VT.getSimpleVT() == MVT::v4i8)
979 return DAG.getNode(HexagonISD::VSPLATB, dl, VT, Val);
980
981 if (VT.getSimpleVT() == MVT::v4i16)
982 return DAG.getNode(HexagonISD::VSPLATH, dl, VT, Val);
983
984 return SDValue();
985}
986
987static bool isSExtFree(SDValue N) {
988 // A sign-extend of a truncate of a sign-extend is free.
989 if (N.getOpcode() == ISD::TRUNCATE &&
990 N.getOperand(0).getOpcode() == ISD::AssertSext)
991 return true;
992 // We have sign-extended loads.
993 if (N.getOpcode() == ISD::LOAD)
994 return true;
995 return false;
996}
997
998SDValue HexagonTargetLowering::LowerCTPOP(SDValue Op, SelectionDAG &DAG) const {
999 SDLoc dl(Op);
1000 SDValue InpVal = Op.getOperand(0);
1001 if (isa<ConstantSDNode>(InpVal)) {
1002 uint64_t V = cast<ConstantSDNode>(InpVal)->getZExtValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001003 return DAG.getTargetConstant(countPopulation(V), dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001004 }
1005 SDValue PopOut = DAG.getNode(HexagonISD::POPCOUNT, dl, MVT::i32, InpVal);
1006 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, PopOut);
1007}
1008
1009SDValue HexagonTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
1010 SDLoc dl(Op);
1011
1012 SDValue LHS = Op.getOperand(0);
1013 SDValue RHS = Op.getOperand(1);
1014 SDValue Cmp = Op.getOperand(2);
1015 ISD::CondCode CC = cast<CondCodeSDNode>(Cmp)->get();
1016
1017 EVT VT = Op.getValueType();
1018 EVT LHSVT = LHS.getValueType();
1019 EVT RHSVT = RHS.getValueType();
1020
1021 if (LHSVT == MVT::v2i16) {
1022 assert(ISD::isSignedIntSetCC(CC) || ISD::isUnsignedIntSetCC(CC));
1023 unsigned ExtOpc = ISD::isSignedIntSetCC(CC) ? ISD::SIGN_EXTEND
1024 : ISD::ZERO_EXTEND;
1025 SDValue LX = DAG.getNode(ExtOpc, dl, MVT::v2i32, LHS);
1026 SDValue RX = DAG.getNode(ExtOpc, dl, MVT::v2i32, RHS);
1027 SDValue SC = DAG.getNode(ISD::SETCC, dl, MVT::v2i1, LX, RX, Cmp);
1028 return SC;
1029 }
1030
1031 // Treat all other vector types as legal.
1032 if (VT.isVector())
1033 return Op;
1034
1035 // Equals and not equals should use sign-extend, not zero-extend, since
1036 // we can represent small negative values in the compare instructions.
1037 // The LLVM default is to use zero-extend arbitrarily in these cases.
1038 if ((CC == ISD::SETEQ || CC == ISD::SETNE) &&
1039 (RHSVT == MVT::i8 || RHSVT == MVT::i16) &&
1040 (LHSVT == MVT::i8 || LHSVT == MVT::i16)) {
1041 ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS);
1042 if (C && C->getAPIntValue().isNegative()) {
1043 LHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, LHS);
1044 RHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, RHS);
1045 return DAG.getNode(ISD::SETCC, dl, Op.getValueType(),
1046 LHS, RHS, Op.getOperand(2));
1047 }
1048 if (isSExtFree(LHS) || isSExtFree(RHS)) {
1049 LHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, LHS);
1050 RHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, RHS);
1051 return DAG.getNode(ISD::SETCC, dl, Op.getValueType(),
1052 LHS, RHS, Op.getOperand(2));
1053 }
1054 }
1055 return SDValue();
1056}
1057
1058SDValue HexagonTargetLowering::LowerVSELECT(SDValue Op, SelectionDAG &DAG)
1059 const {
1060 SDValue PredOp = Op.getOperand(0);
1061 SDValue Op1 = Op.getOperand(1), Op2 = Op.getOperand(2);
1062 EVT OpVT = Op1.getValueType();
1063 SDLoc DL(Op);
1064
1065 if (OpVT == MVT::v2i16) {
1066 SDValue X1 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i32, Op1);
1067 SDValue X2 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i32, Op2);
1068 SDValue SL = DAG.getNode(ISD::VSELECT, DL, MVT::v2i32, PredOp, X1, X2);
1069 SDValue TR = DAG.getNode(ISD::TRUNCATE, DL, MVT::v2i16, SL);
1070 return TR;
1071 }
1072
1073 return SDValue();
1074}
1075
1076// Handle only specific vector loads.
1077SDValue HexagonTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1078 EVT VT = Op.getValueType();
1079 SDLoc DL(Op);
1080 LoadSDNode *LoadNode = cast<LoadSDNode>(Op);
1081 SDValue Chain = LoadNode->getChain();
1082 SDValue Ptr = Op.getOperand(1);
1083 SDValue LoweredLoad;
1084 SDValue Result;
1085 SDValue Base = LoadNode->getBasePtr();
1086 ISD::LoadExtType Ext = LoadNode->getExtensionType();
1087 unsigned Alignment = LoadNode->getAlignment();
1088 SDValue LoadChain;
1089
1090 if(Ext == ISD::NON_EXTLOAD)
1091 Ext = ISD::ZEXTLOAD;
1092
1093 if (VT == MVT::v4i16) {
1094 if (Alignment == 2) {
1095 SDValue Loads[4];
1096 // Base load.
1097 Loads[0] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Base,
1098 LoadNode->getPointerInfo(), MVT::i16,
1099 LoadNode->isVolatile(),
1100 LoadNode->isNonTemporal(),
1101 LoadNode->isInvariant(),
1102 Alignment);
1103 // Base+2 load.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001104 SDValue Increment = DAG.getConstant(2, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001105 Ptr = DAG.getNode(ISD::ADD, DL, Base.getValueType(), Base, Increment);
1106 Loads[1] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Ptr,
1107 LoadNode->getPointerInfo(), MVT::i16,
1108 LoadNode->isVolatile(),
1109 LoadNode->isNonTemporal(),
1110 LoadNode->isInvariant(),
1111 Alignment);
1112 // SHL 16, then OR base and base+2.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001113 SDValue ShiftAmount = DAG.getConstant(16, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001114 SDValue Tmp1 = DAG.getNode(ISD::SHL, DL, MVT::i32, Loads[1], ShiftAmount);
1115 SDValue Tmp2 = DAG.getNode(ISD::OR, DL, MVT::i32, Tmp1, Loads[0]);
1116 // Base + 4.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001117 Increment = DAG.getConstant(4, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001118 Ptr = DAG.getNode(ISD::ADD, DL, Base.getValueType(), Base, Increment);
1119 Loads[2] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Ptr,
1120 LoadNode->getPointerInfo(), MVT::i16,
1121 LoadNode->isVolatile(),
1122 LoadNode->isNonTemporal(),
1123 LoadNode->isInvariant(),
1124 Alignment);
1125 // Base + 6.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001126 Increment = DAG.getConstant(6, DL, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001127 Ptr = DAG.getNode(ISD::ADD, DL, Base.getValueType(), Base, Increment);
1128 Loads[3] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Ptr,
1129 LoadNode->getPointerInfo(), MVT::i16,
1130 LoadNode->isVolatile(),
1131 LoadNode->isNonTemporal(),
1132 LoadNode->isInvariant(),
1133 Alignment);
1134 // SHL 16, then OR base+4 and base+6.
1135 Tmp1 = DAG.getNode(ISD::SHL, DL, MVT::i32, Loads[3], ShiftAmount);
1136 SDValue Tmp4 = DAG.getNode(ISD::OR, DL, MVT::i32, Tmp1, Loads[2]);
1137 // Combine to i64. This could be optimised out later if we can
1138 // affect reg allocation of this code.
1139 Result = DAG.getNode(HexagonISD::COMBINE, DL, MVT::i64, Tmp4, Tmp2);
1140 LoadChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
1141 Loads[0].getValue(1), Loads[1].getValue(1),
1142 Loads[2].getValue(1), Loads[3].getValue(1));
1143 } else {
1144 // Perform default type expansion.
1145 Result = DAG.getLoad(MVT::i64, DL, Chain, Ptr, LoadNode->getPointerInfo(),
1146 LoadNode->isVolatile(), LoadNode->isNonTemporal(),
1147 LoadNode->isInvariant(), LoadNode->getAlignment());
1148 LoadChain = Result.getValue(1);
1149 }
1150 } else
1151 llvm_unreachable("Custom lowering unsupported load");
1152
1153 Result = DAG.getNode(ISD::BITCAST, DL, VT, Result);
1154 // Since we pretend to lower a load, we need the original chain
1155 // info attached to the result.
1156 SDValue Ops[] = { Result, LoadChain };
1157
1158 return DAG.getMergeValues(Ops, DL);
1159}
1160
1161
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001162SDValue
Sirish Pande69295b82012-05-10 20:20:25 +00001163HexagonTargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
1164 EVT ValTy = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001165 SDLoc dl(Op);
Sirish Pande69295b82012-05-10 20:20:25 +00001166 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
1167 SDValue Res;
1168 if (CP->isMachineConstantPoolEntry())
1169 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), ValTy,
1170 CP->getAlignment());
1171 else
1172 Res = DAG.getTargetConstantPool(CP->getConstVal(), ValTy,
1173 CP->getAlignment());
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001174 return DAG.getNode(HexagonISD::CP, dl, ValTy, Res);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001175}
1176
1177SDValue
1178HexagonTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001179 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001180 MachineFunction &MF = DAG.getMachineFunction();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001181 MachineFrameInfo &MFI = *MF.getFrameInfo();
1182 MFI.setReturnAddressIsTaken(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001183
Bill Wendling908bf812014-01-06 00:43:20 +00001184 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001185 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00001186
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001187 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001188 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001189 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1190 if (Depth) {
1191 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001192 SDValue Offset = DAG.getConstant(4, dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001193 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
1194 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
1195 MachinePointerInfo(), false, false, false, 0);
1196 }
1197
1198 // Return LR, which contains the return address. Mark it an implicit live-in.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001199 unsigned Reg = MF.addLiveIn(HRI.getRARegister(), getRegClassFor(MVT::i32));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001200 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
1201}
1202
1203SDValue
1204HexagonTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001205 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
1206 MachineFrameInfo &MFI = *DAG.getMachineFunction().getFrameInfo();
1207 MFI.setFrameAddressIsTaken(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001208
1209 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001210 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001211 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1212 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001213 HRI.getFrameRegister(), VT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001214 while (Depth--)
1215 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
1216 MachinePointerInfo(),
1217 false, false, false, 0);
1218 return FrameAddr;
1219}
1220
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001221SDValue HexagonTargetLowering::LowerATOMIC_FENCE(SDValue Op,
1222 SelectionDAG& DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001223 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001224 return DAG.getNode(HexagonISD::BARRIER, dl, MVT::Other, Op.getOperand(0));
1225}
1226
1227
1228SDValue HexagonTargetLowering::LowerGLOBALADDRESS(SDValue Op,
1229 SelectionDAG &DAG) const {
1230 SDValue Result;
1231 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1232 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001233 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001234 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
1235
Eric Christopher36fe0282015-02-03 07:22:52 +00001236 const HexagonTargetObjectFile *TLOF =
1237 static_cast<const HexagonTargetObjectFile *>(
1238 getTargetMachine().getObjFileLowering());
1239 if (TLOF->IsGlobalInSmallSection(GV, getTargetMachine())) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001240 return DAG.getNode(HexagonISD::CONST32_GP, dl, getPointerTy(), Result);
1241 }
1242
1243 return DAG.getNode(HexagonISD::CONST32, dl, getPointerTy(), Result);
1244}
1245
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001246// Specifies that for loads and stores VT can be promoted to PromotedLdStVT.
1247void HexagonTargetLowering::promoteLdStType(EVT VT, EVT PromotedLdStVT) {
1248 if (VT != PromotedLdStVT) {
1249 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
1250 AddPromotedToType(ISD::LOAD, VT.getSimpleVT(),
1251 PromotedLdStVT.getSimpleVT());
1252
1253 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
1254 AddPromotedToType(ISD::STORE, VT.getSimpleVT(),
1255 PromotedLdStVT.getSimpleVT());
1256 }
1257}
1258
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001259SDValue
1260HexagonTargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
1261 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
1262 SDValue BA_SD = DAG.getTargetBlockAddress(BA, MVT::i32);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001263 SDLoc dl(Op);
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001264 return DAG.getNode(HexagonISD::CONST32_GP, dl, getPointerTy(), BA_SD);
1265}
1266
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001267//===----------------------------------------------------------------------===//
1268// TargetLowering Implementation
1269//===----------------------------------------------------------------------===//
1270
Eric Christopherd737b762015-02-02 22:11:36 +00001271HexagonTargetLowering::HexagonTargetLowering(const TargetMachine &TM,
1272 const HexagonSubtarget &STI)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001273 : TargetLowering(TM), HTM(static_cast<const HexagonTargetMachine&>(TM)),
1274 Subtarget(STI) {
1275 bool IsV4 = !Subtarget.hasV5TOps();
1276 auto &HRI = *Subtarget.getRegisterInfo();
Sirish Pande69295b82012-05-10 20:20:25 +00001277
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001278 setPrefLoopAlignment(4);
1279 setPrefFunctionAlignment(4);
1280 setMinFunctionAlignment(2);
1281 setInsertFencesForAtomic(false);
1282 setExceptionPointerRegister(Hexagon::R0);
1283 setExceptionSelectorRegister(Hexagon::R1);
1284 setStackPointerRegisterToSaveRestore(HRI.getStackRegister());
1285
1286 if (EnableHexSDNodeSched)
1287 setSchedulingPreference(Sched::VLIW);
1288 else
1289 setSchedulingPreference(Sched::Source);
1290
1291 // Limits for inline expansion of memcpy/memmove
1292 MaxStoresPerMemcpy = MaxStoresPerMemcpyCL;
1293 MaxStoresPerMemcpyOptSize = MaxStoresPerMemcpyOptSizeCL;
1294 MaxStoresPerMemmove = MaxStoresPerMemmoveCL;
1295 MaxStoresPerMemmoveOptSize = MaxStoresPerMemmoveOptSizeCL;
1296 MaxStoresPerMemset = MaxStoresPerMemsetCL;
1297 MaxStoresPerMemsetOptSize = MaxStoresPerMemsetOptSizeCL;
1298
1299 //
1300 // Set up register classes.
1301 //
1302
1303 addRegisterClass(MVT::i1, &Hexagon::PredRegsRegClass);
1304 addRegisterClass(MVT::v2i1, &Hexagon::PredRegsRegClass); // bbbbaaaa
1305 addRegisterClass(MVT::v4i1, &Hexagon::PredRegsRegClass); // ddccbbaa
1306 addRegisterClass(MVT::v8i1, &Hexagon::PredRegsRegClass); // hgfedcba
1307 addRegisterClass(MVT::i32, &Hexagon::IntRegsRegClass);
1308 addRegisterClass(MVT::v4i8, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001309 addRegisterClass(MVT::v2i16, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001310 addRegisterClass(MVT::i64, &Hexagon::DoubleRegsRegClass);
1311 addRegisterClass(MVT::v8i8, &Hexagon::DoubleRegsRegClass);
1312 addRegisterClass(MVT::v4i16, &Hexagon::DoubleRegsRegClass);
1313 addRegisterClass(MVT::v2i32, &Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001314
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001315 if (Subtarget.hasV5TOps()) {
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001316 addRegisterClass(MVT::f32, &Hexagon::IntRegsRegClass);
1317 addRegisterClass(MVT::f64, &Hexagon::DoubleRegsRegClass);
1318 }
Sirish Pande69295b82012-05-10 20:20:25 +00001319
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001320 //
1321 // Handling of scalar operations.
1322 //
1323 // All operations default to "legal", except:
1324 // - indexed loads and stores (pre-/post-incremented),
1325 // - ANY_EXTEND_VECTOR_INREG, ATOMIC_CMP_SWAP_WITH_SUCCESS, CONCAT_VECTORS,
1326 // ConstantFP, DEBUGTRAP, FCEIL, FCOPYSIGN, FEXP, FEXP2, FFLOOR, FGETSIGN,
1327 // FLOG, FLOG2, FLOG10, FMAXNUM, FMINNUM, FNEARBYINT, FRINT, FROUND, TRAP,
1328 // FTRUNC, PREFETCH, SIGN_EXTEND_VECTOR_INREG, ZERO_EXTEND_VECTOR_INREG,
1329 // which default to "expand" for at least one type.
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001330
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001331 // Misc operations.
1332 setOperationAction(ISD::ConstantFP, MVT::f32, Legal); // Default: expand
1333 setOperationAction(ISD::ConstantFP, MVT::f64, Legal); // Default: expand
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001334
1335 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001336 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001337 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
1338 setOperationAction(ISD::INLINEASM, MVT::Other, Custom);
1339 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
1340 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001341
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001342 // Custom legalize GlobalAddress nodes into CONST32.
1343 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001344 setOperationAction(ISD::GlobalAddress, MVT::i8, Custom);
1345 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001346
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001347 // Hexagon needs to optimize cases with negative constants.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001348 setOperationAction(ISD::SETCC, MVT::i8, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001349 setOperationAction(ISD::SETCC, MVT::i16, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001350
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001351 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
1352 setOperationAction(ISD::VASTART, MVT::Other, Custom);
1353 setOperationAction(ISD::VAEND, MVT::Other, Expand);
1354 setOperationAction(ISD::VAARG, MVT::Other, Expand);
1355
1356 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
1357 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
1358 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
1359
1360 if (EmitJumpTables)
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001361 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001362 else
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001363 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001364 // Increase jump tables cutover to 5, was 4.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001365 setMinimumJumpTableEntries(MinimumJumpTables);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001366
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001367 // Hexagon has instructions for add/sub with carry. The problem with
1368 // modeling these instructions is that they produce 2 results: Rdd and Px.
1369 // To model the update of Px, we will have to use Defs[p0..p3] which will
1370 // cause any predicate live range to spill. So, we pretend we dont't have
1371 // these instructions.
1372 setOperationAction(ISD::ADDE, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001373 setOperationAction(ISD::ADDE, MVT::i16, Expand);
1374 setOperationAction(ISD::ADDE, MVT::i32, Expand);
1375 setOperationAction(ISD::ADDE, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001376 setOperationAction(ISD::SUBE, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001377 setOperationAction(ISD::SUBE, MVT::i16, Expand);
1378 setOperationAction(ISD::SUBE, MVT::i32, Expand);
1379 setOperationAction(ISD::SUBE, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001380 setOperationAction(ISD::ADDC, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001381 setOperationAction(ISD::ADDC, MVT::i16, Expand);
1382 setOperationAction(ISD::ADDC, MVT::i32, Expand);
1383 setOperationAction(ISD::ADDC, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001384 setOperationAction(ISD::SUBC, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001385 setOperationAction(ISD::SUBC, MVT::i16, Expand);
1386 setOperationAction(ISD::SUBC, MVT::i32, Expand);
1387 setOperationAction(ISD::SUBC, MVT::i64, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001388
Krzysztof Parzyszek2c4487d2015-04-13 20:37:01 +00001389 // Only add and sub that detect overflow are the saturating ones.
1390 for (MVT VT : MVT::integer_valuetypes()) {
1391 setOperationAction(ISD::UADDO, VT, Expand);
1392 setOperationAction(ISD::SADDO, VT, Expand);
1393 setOperationAction(ISD::USUBO, VT, Expand);
1394 setOperationAction(ISD::SSUBO, VT, Expand);
1395 }
1396
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001397 setOperationAction(ISD::CTLZ, MVT::i8, Promote);
1398 setOperationAction(ISD::CTLZ, MVT::i16, Promote);
1399 setOperationAction(ISD::CTTZ, MVT::i8, Promote);
1400 setOperationAction(ISD::CTTZ, MVT::i16, Promote);
1401 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8, Promote);
1402 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16, Promote);
1403 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i8, Promote);
1404 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16, Promote);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001405
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001406 // In V5, popcount can count # of 1s in i64 but returns i32.
1407 // On V4 it will be expanded (set later).
1408 setOperationAction(ISD::CTPOP, MVT::i8, Promote);
1409 setOperationAction(ISD::CTPOP, MVT::i16, Promote);
1410 setOperationAction(ISD::CTPOP, MVT::i32, Promote);
1411 setOperationAction(ISD::CTPOP, MVT::i64, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001412
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001413 // We custom lower i64 to i64 mul, so that it is not considered as a legal
1414 // operation. There is a pattern that will match i64 mul and transform it
1415 // to a series of instructions.
1416 setOperationAction(ISD::MUL, MVT::i64, Expand);
Colin LeMahieude68b662015-02-05 21:13:25 +00001417 setOperationAction(ISD::MULHS, MVT::i64, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001418
Benjamin Kramer62460692015-04-25 14:46:53 +00001419 for (unsigned IntExpOp :
1420 {ISD::SDIV, ISD::UDIV, ISD::SREM, ISD::UREM, ISD::SDIVREM, ISD::UDIVREM,
1421 ISD::ROTL, ISD::ROTR, ISD::BSWAP, ISD::SHL_PARTS, ISD::SRA_PARTS,
1422 ISD::SRL_PARTS, ISD::SMUL_LOHI, ISD::UMUL_LOHI}) {
1423 setOperationAction(IntExpOp, MVT::i32, Expand);
1424 setOperationAction(IntExpOp, MVT::i64, Expand);
1425 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001426
Benjamin Kramer62460692015-04-25 14:46:53 +00001427 for (unsigned FPExpOp :
1428 {ISD::FDIV, ISD::FREM, ISD::FSQRT, ISD::FSIN, ISD::FCOS, ISD::FSINCOS,
1429 ISD::FPOW, ISD::FCOPYSIGN}) {
1430 setOperationAction(FPExpOp, MVT::f32, Expand);
1431 setOperationAction(FPExpOp, MVT::f64, Expand);
1432 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001433
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001434 // No extending loads from i32.
1435 for (MVT VT : MVT::integer_valuetypes()) {
1436 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand);
1437 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand);
1438 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand);
1439 }
1440 // Turn FP truncstore into trunc + store.
1441 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
1442 // Turn FP extload into load/fextend.
1443 for (MVT VT : MVT::fp_valuetypes())
1444 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001445
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001446 // Expand BR_CC and SELECT_CC for all integer and fp types.
1447 for (MVT VT : MVT::integer_valuetypes()) {
1448 setOperationAction(ISD::BR_CC, VT, Expand);
1449 setOperationAction(ISD::SELECT_CC, VT, Expand);
1450 }
1451 for (MVT VT : MVT::fp_valuetypes()) {
1452 setOperationAction(ISD::BR_CC, VT, Expand);
1453 setOperationAction(ISD::SELECT_CC, VT, Expand);
1454 }
1455 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001456
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001457 //
1458 // Handling of vector operations.
1459 //
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001460
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001461 // Custom lower v4i16 load only. Let v4i16 store to be
1462 // promoted for now.
1463 promoteLdStType(MVT::v4i8, MVT::i32);
1464 promoteLdStType(MVT::v2i16, MVT::i32);
1465 promoteLdStType(MVT::v8i8, MVT::i64);
1466 promoteLdStType(MVT::v2i32, MVT::i64);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001467
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001468 setOperationAction(ISD::LOAD, MVT::v4i16, Custom);
1469 setOperationAction(ISD::STORE, MVT::v4i16, Promote);
1470 AddPromotedToType(ISD::LOAD, MVT::v4i16, MVT::i64);
1471 AddPromotedToType(ISD::STORE, MVT::v4i16, MVT::i64);
1472
1473 // Set the action for vector operations to "expand", then override it with
1474 // either "custom" or "legal" for specific cases.
1475 static unsigned VectExpOps[] = {
1476 // Integer arithmetic:
1477 ISD::ADD, ISD::SUB, ISD::MUL, ISD::SDIV, ISD::UDIV,
1478 ISD::SREM, ISD::UREM, ISD::SDIVREM, ISD::UDIVREM, ISD::ADDC,
1479 ISD::SUBC, ISD::SADDO, ISD::UADDO, ISD::SSUBO, ISD::USUBO,
1480 ISD::SMUL_LOHI, ISD::UMUL_LOHI,
1481 // Logical/bit:
1482 ISD::AND, ISD::OR, ISD::XOR, ISD::ROTL, ISD::ROTR,
1483 ISD::CTPOP, ISD::CTLZ, ISD::CTTZ, ISD::CTLZ_ZERO_UNDEF,
1484 ISD::CTTZ_ZERO_UNDEF,
1485 // Floating point arithmetic/math functions:
1486 ISD::FADD, ISD::FSUB, ISD::FMUL, ISD::FMA, ISD::FDIV,
1487 ISD::FREM, ISD::FNEG, ISD::FABS, ISD::FSQRT, ISD::FSIN,
1488 ISD::FCOS, ISD::FPOWI, ISD::FPOW, ISD::FLOG, ISD::FLOG2,
1489 ISD::FLOG10, ISD::FEXP, ISD::FEXP2, ISD::FCEIL, ISD::FTRUNC,
1490 ISD::FRINT, ISD::FNEARBYINT, ISD::FROUND, ISD::FFLOOR,
1491 ISD::FMINNUM, ISD::FMAXNUM, ISD::FSINCOS,
1492 // Misc:
1493 ISD::SELECT, ISD::ConstantPool,
1494 // Vector:
1495 ISD::BUILD_VECTOR, ISD::SCALAR_TO_VECTOR,
1496 ISD::EXTRACT_VECTOR_ELT, ISD::INSERT_VECTOR_ELT,
1497 ISD::EXTRACT_SUBVECTOR, ISD::INSERT_SUBVECTOR,
1498 ISD::CONCAT_VECTORS, ISD::VECTOR_SHUFFLE
1499 };
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001500
1501 for (MVT VT : MVT::vector_valuetypes()) {
Benjamin Kramer62460692015-04-25 14:46:53 +00001502 for (unsigned VectExpOp : VectExpOps)
1503 setOperationAction(VectExpOp, VT, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001504
1505 // Expand all extended loads and truncating stores:
1506 for (MVT TargetVT : MVT::vector_valuetypes()) {
1507 setLoadExtAction(ISD::EXTLOAD, TargetVT, VT, Expand);
1508 setTruncStoreAction(VT, TargetVT, Expand);
1509 }
1510
1511 setOperationAction(ISD::SRA, VT, Custom);
1512 setOperationAction(ISD::SHL, VT, Custom);
1513 setOperationAction(ISD::SRL, VT, Custom);
1514 }
1515
1516 // Types natively supported:
Benjamin Kramer62460692015-04-25 14:46:53 +00001517 for (MVT NativeVT : {MVT::v2i1, MVT::v4i1, MVT::v8i1, MVT::v32i1, MVT::v64i1,
1518 MVT::v4i8, MVT::v8i8, MVT::v2i16, MVT::v4i16, MVT::v1i32,
1519 MVT::v2i32, MVT::v1i64}) {
1520 setOperationAction(ISD::BUILD_VECTOR, NativeVT, Custom);
1521 setOperationAction(ISD::EXTRACT_VECTOR_ELT, NativeVT, Custom);
1522 setOperationAction(ISD::INSERT_VECTOR_ELT, NativeVT, Custom);
1523 setOperationAction(ISD::EXTRACT_SUBVECTOR, NativeVT, Custom);
1524 setOperationAction(ISD::INSERT_SUBVECTOR, NativeVT, Custom);
1525 setOperationAction(ISD::CONCAT_VECTORS, NativeVT, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001526
Benjamin Kramer62460692015-04-25 14:46:53 +00001527 setOperationAction(ISD::ADD, NativeVT, Legal);
1528 setOperationAction(ISD::SUB, NativeVT, Legal);
1529 setOperationAction(ISD::MUL, NativeVT, Legal);
1530 setOperationAction(ISD::AND, NativeVT, Legal);
1531 setOperationAction(ISD::OR, NativeVT, Legal);
1532 setOperationAction(ISD::XOR, NativeVT, Legal);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001533 }
1534
1535 setOperationAction(ISD::SETCC, MVT::v2i16, Custom);
1536 setOperationAction(ISD::VSELECT, MVT::v2i16, Custom);
1537 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
1538 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
1539
1540 // Subtarget-specific operation actions.
1541 //
1542 if (Subtarget.hasV5TOps()) {
1543 setOperationAction(ISD::FMA, MVT::f64, Expand);
1544 setOperationAction(ISD::FADD, MVT::f64, Expand);
1545 setOperationAction(ISD::FSUB, MVT::f64, Expand);
1546 setOperationAction(ISD::FMUL, MVT::f64, Expand);
1547
1548 setOperationAction(ISD::FP_TO_UINT, MVT::i1, Promote);
1549 setOperationAction(ISD::FP_TO_UINT, MVT::i8, Promote);
1550 setOperationAction(ISD::FP_TO_UINT, MVT::i16, Promote);
1551 setOperationAction(ISD::FP_TO_SINT, MVT::i1, Promote);
1552 setOperationAction(ISD::FP_TO_SINT, MVT::i8, Promote);
1553 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
1554 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Promote);
1555 setOperationAction(ISD::UINT_TO_FP, MVT::i8, Promote);
1556 setOperationAction(ISD::UINT_TO_FP, MVT::i16, Promote);
1557 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Promote);
1558 setOperationAction(ISD::SINT_TO_FP, MVT::i8, Promote);
1559 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
1560
1561 } else { // V4
1562 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
1563 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Expand);
1564 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
1565 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
1566 setOperationAction(ISD::FP_TO_SINT, MVT::f64, Expand);
1567 setOperationAction(ISD::FP_TO_SINT, MVT::f32, Expand);
1568 setOperationAction(ISD::FP_EXTEND, MVT::f32, Expand);
1569 setOperationAction(ISD::FP_ROUND, MVT::f64, Expand);
1570 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
1571
1572 setOperationAction(ISD::CTPOP, MVT::i8, Expand);
1573 setOperationAction(ISD::CTPOP, MVT::i16, Expand);
1574 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
1575 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
1576
1577 // Expand these operations for both f32 and f64:
Benjamin Kramer62460692015-04-25 14:46:53 +00001578 for (unsigned FPExpOpV4 :
1579 {ISD::FADD, ISD::FSUB, ISD::FMUL, ISD::FABS, ISD::FNEG, ISD::FMA}) {
1580 setOperationAction(FPExpOpV4, MVT::f32, Expand);
1581 setOperationAction(FPExpOpV4, MVT::f64, Expand);
1582 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001583
Benjamin Kramer62460692015-04-25 14:46:53 +00001584 for (ISD::CondCode FPExpCCV4 :
1585 {ISD::SETOEQ, ISD::SETOGT, ISD::SETOLT, ISD::SETOGE, ISD::SETOLE,
1586 ISD::SETUO, ISD::SETO}) {
1587 setCondCodeAction(FPExpCCV4, MVT::f32, Expand);
1588 setCondCodeAction(FPExpCCV4, MVT::f64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001589 }
1590 }
1591
1592 // Handling of indexed loads/stores: default is "expand".
1593 //
Benjamin Kramer62460692015-04-25 14:46:53 +00001594 for (MVT LSXTy : {MVT::i8, MVT::i16, MVT::i32, MVT::i64}) {
1595 setIndexedLoadAction(ISD::POST_INC, LSXTy, Legal);
1596 setIndexedStoreAction(ISD::POST_INC, LSXTy, Legal);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001597 }
1598
1599 computeRegisterProperties(&HRI);
1600
1601 //
1602 // Library calls for unsupported operations
1603 //
1604 bool FastMath = EnableFastMath;
1605
Benjamin Kramera37c8092015-04-25 14:46:46 +00001606 setLibcallName(RTLIB::SDIV_I32, "__hexagon_divsi3");
1607 setLibcallName(RTLIB::SDIV_I64, "__hexagon_divdi3");
1608 setLibcallName(RTLIB::UDIV_I32, "__hexagon_udivsi3");
1609 setLibcallName(RTLIB::UDIV_I64, "__hexagon_udivdi3");
1610 setLibcallName(RTLIB::SREM_I32, "__hexagon_modsi3");
1611 setLibcallName(RTLIB::SREM_I64, "__hexagon_moddi3");
1612 setLibcallName(RTLIB::UREM_I32, "__hexagon_umodsi3");
1613 setLibcallName(RTLIB::UREM_I64, "__hexagon_umoddi3");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001614
Benjamin Kramera37c8092015-04-25 14:46:46 +00001615 setLibcallName(RTLIB::SINTTOFP_I128_F64, "__hexagon_floattidf");
1616 setLibcallName(RTLIB::SINTTOFP_I128_F32, "__hexagon_floattisf");
1617 setLibcallName(RTLIB::FPTOUINT_F32_I128, "__hexagon_fixunssfti");
1618 setLibcallName(RTLIB::FPTOUINT_F64_I128, "__hexagon_fixunsdfti");
1619 setLibcallName(RTLIB::FPTOSINT_F32_I128, "__hexagon_fixsfti");
1620 setLibcallName(RTLIB::FPTOSINT_F64_I128, "__hexagon_fixdfti");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001621
1622 if (IsV4) {
1623 // Handle single-precision floating point operations on V4.
Benjamin Kramera37c8092015-04-25 14:46:46 +00001624 if (FastMath) {
1625 setLibcallName(RTLIB::ADD_F32, "__hexagon_fast_addsf3");
1626 setLibcallName(RTLIB::SUB_F32, "__hexagon_fast_subsf3");
1627 setLibcallName(RTLIB::MUL_F32, "__hexagon_fast_mulsf3");
1628 setLibcallName(RTLIB::OGT_F32, "__hexagon_fast_gtsf2");
1629 setLibcallName(RTLIB::OLT_F32, "__hexagon_fast_ltsf2");
1630 // Double-precision compares.
1631 setLibcallName(RTLIB::OGT_F64, "__hexagon_fast_gtdf2");
1632 setLibcallName(RTLIB::OLT_F64, "__hexagon_fast_ltdf2");
1633 } else {
1634 setLibcallName(RTLIB::ADD_F32, "__hexagon_addsf3");
1635 setLibcallName(RTLIB::SUB_F32, "__hexagon_subsf3");
1636 setLibcallName(RTLIB::MUL_F32, "__hexagon_mulsf3");
1637 setLibcallName(RTLIB::OGT_F32, "__hexagon_gtsf2");
1638 setLibcallName(RTLIB::OLT_F32, "__hexagon_ltsf2");
1639 // Double-precision compares.
1640 setLibcallName(RTLIB::OGT_F64, "__hexagon_gtdf2");
1641 setLibcallName(RTLIB::OLT_F64, "__hexagon_ltdf2");
1642 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001643 }
1644
1645 // This is the only fast library function for sqrtd.
1646 if (FastMath)
Benjamin Kramera37c8092015-04-25 14:46:46 +00001647 setLibcallName(RTLIB::SQRT_F64, "__hexagon_fast2_sqrtdf2");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001648
Benjamin Kramera37c8092015-04-25 14:46:46 +00001649 // Prefix is: nothing for "slow-math",
1650 // "fast2_" for V4 fast-math and V5+ fast-math double-precision
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001651 // (actually, keep fast-math and fast-math2 separate for now)
Benjamin Kramera37c8092015-04-25 14:46:46 +00001652 if (FastMath) {
1653 setLibcallName(RTLIB::ADD_F64, "__hexagon_fast_adddf3");
1654 setLibcallName(RTLIB::SUB_F64, "__hexagon_fast_subdf3");
1655 setLibcallName(RTLIB::MUL_F64, "__hexagon_fast_muldf3");
1656 setLibcallName(RTLIB::DIV_F64, "__hexagon_fast_divdf3");
1657 // Calling __hexagon_fast2_divsf3 with fast-math on V5 (ok).
1658 setLibcallName(RTLIB::DIV_F32, "__hexagon_fast_divsf3");
1659 } else {
1660 setLibcallName(RTLIB::ADD_F64, "__hexagon_adddf3");
1661 setLibcallName(RTLIB::SUB_F64, "__hexagon_subdf3");
1662 setLibcallName(RTLIB::MUL_F64, "__hexagon_muldf3");
1663 setLibcallName(RTLIB::DIV_F64, "__hexagon_divdf3");
1664 setLibcallName(RTLIB::DIV_F32, "__hexagon_divsf3");
1665 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001666
1667 if (Subtarget.hasV5TOps()) {
1668 if (FastMath)
Benjamin Kramera37c8092015-04-25 14:46:46 +00001669 setLibcallName(RTLIB::SQRT_F32, "__hexagon_fast2_sqrtf");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001670 else
Benjamin Kramera37c8092015-04-25 14:46:46 +00001671 setLibcallName(RTLIB::SQRT_F32, "__hexagon_sqrtf");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001672 } else {
1673 // V4
Benjamin Kramera37c8092015-04-25 14:46:46 +00001674 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__hexagon_floatsisf");
1675 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__hexagon_floatsidf");
1676 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__hexagon_floatdisf");
1677 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__hexagon_floatdidf");
1678 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__hexagon_floatunsisf");
1679 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__hexagon_floatunsidf");
1680 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__hexagon_floatundisf");
1681 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__hexagon_floatundidf");
1682 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__hexagon_fixunssfsi");
1683 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__hexagon_fixunssfdi");
1684 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__hexagon_fixunsdfsi");
1685 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__hexagon_fixunsdfdi");
1686 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__hexagon_fixsfsi");
1687 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__hexagon_fixsfdi");
1688 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__hexagon_fixdfsi");
1689 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__hexagon_fixdfdi");
1690 setLibcallName(RTLIB::FPEXT_F32_F64, "__hexagon_extendsfdf2");
1691 setLibcallName(RTLIB::FPROUND_F64_F32, "__hexagon_truncdfsf2");
1692 setLibcallName(RTLIB::OEQ_F32, "__hexagon_eqsf2");
1693 setLibcallName(RTLIB::OEQ_F64, "__hexagon_eqdf2");
1694 setLibcallName(RTLIB::OGE_F32, "__hexagon_gesf2");
1695 setLibcallName(RTLIB::OGE_F64, "__hexagon_gedf2");
1696 setLibcallName(RTLIB::OLE_F32, "__hexagon_lesf2");
1697 setLibcallName(RTLIB::OLE_F64, "__hexagon_ledf2");
1698 setLibcallName(RTLIB::UNE_F32, "__hexagon_nesf2");
1699 setLibcallName(RTLIB::UNE_F64, "__hexagon_nedf2");
1700 setLibcallName(RTLIB::UO_F32, "__hexagon_unordsf2");
1701 setLibcallName(RTLIB::UO_F64, "__hexagon_unorddf2");
1702 setLibcallName(RTLIB::O_F32, "__hexagon_unordsf2");
1703 setLibcallName(RTLIB::O_F64, "__hexagon_unorddf2");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001704 }
1705
1706 // These cause problems when the shift amount is non-constant.
1707 setLibcallName(RTLIB::SHL_I128, nullptr);
1708 setLibcallName(RTLIB::SRL_I128, nullptr);
1709 setLibcallName(RTLIB::SRA_I128, nullptr);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001710}
1711
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001712
1713const char* HexagonTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00001714 switch ((HexagonISD::NodeType)Opcode) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001715 case HexagonISD::ALLOCA: return "HexagonISD::ALLOCA";
1716 case HexagonISD::ARGEXTEND: return "HexagonISD::ARGEXTEND";
1717 case HexagonISD::AT_GOT: return "HexagonISD::AT_GOT";
1718 case HexagonISD::AT_PCREL: return "HexagonISD::AT_PCREL";
1719 case HexagonISD::BARRIER: return "HexagonISD::BARRIER";
1720 case HexagonISD::BR_JT: return "HexagonISD::BR_JT";
1721 case HexagonISD::CALLR: return "HexagonISD::CALLR";
1722 case HexagonISD::CALLv3nr: return "HexagonISD::CALLv3nr";
1723 case HexagonISD::CALLv3: return "HexagonISD::CALLv3";
1724 case HexagonISD::COMBINE: return "HexagonISD::COMBINE";
1725 case HexagonISD::CONST32_GP: return "HexagonISD::CONST32_GP";
1726 case HexagonISD::CONST32: return "HexagonISD::CONST32";
1727 case HexagonISD::CP: return "HexagonISD::CP";
1728 case HexagonISD::DCFETCH: return "HexagonISD::DCFETCH";
1729 case HexagonISD::EH_RETURN: return "HexagonISD::EH_RETURN";
1730 case HexagonISD::EXTRACTU: return "HexagonISD::EXTRACTU";
1731 case HexagonISD::EXTRACTURP: return "HexagonISD::EXTRACTURP";
1732 case HexagonISD::FCONST32: return "HexagonISD::FCONST32";
1733 case HexagonISD::INSERT: return "HexagonISD::INSERT";
1734 case HexagonISD::INSERTRP: return "HexagonISD::INSERTRP";
1735 case HexagonISD::JT: return "HexagonISD::JT";
1736 case HexagonISD::PACKHL: return "HexagonISD::PACKHL";
1737 case HexagonISD::PIC_ADD: return "HexagonISD::PIC_ADD";
1738 case HexagonISD::POPCOUNT: return "HexagonISD::POPCOUNT";
1739 case HexagonISD::RET_FLAG: return "HexagonISD::RET_FLAG";
1740 case HexagonISD::SHUFFEB: return "HexagonISD::SHUFFEB";
1741 case HexagonISD::SHUFFEH: return "HexagonISD::SHUFFEH";
1742 case HexagonISD::SHUFFOB: return "HexagonISD::SHUFFOB";
1743 case HexagonISD::SHUFFOH: return "HexagonISD::SHUFFOH";
1744 case HexagonISD::TC_RETURN: return "HexagonISD::TC_RETURN";
1745 case HexagonISD::VCMPBEQ: return "HexagonISD::VCMPBEQ";
1746 case HexagonISD::VCMPBGT: return "HexagonISD::VCMPBGT";
1747 case HexagonISD::VCMPBGTU: return "HexagonISD::VCMPBGTU";
1748 case HexagonISD::VCMPHEQ: return "HexagonISD::VCMPHEQ";
1749 case HexagonISD::VCMPHGT: return "HexagonISD::VCMPHGT";
1750 case HexagonISD::VCMPHGTU: return "HexagonISD::VCMPHGTU";
1751 case HexagonISD::VCMPWEQ: return "HexagonISD::VCMPWEQ";
1752 case HexagonISD::VCMPWGT: return "HexagonISD::VCMPWGT";
1753 case HexagonISD::VCMPWGTU: return "HexagonISD::VCMPWGTU";
1754 case HexagonISD::VSHLH: return "HexagonISD::VSHLH";
1755 case HexagonISD::VSHLW: return "HexagonISD::VSHLW";
1756 case HexagonISD::VSPLATB: return "HexagonISD::VSPLTB";
1757 case HexagonISD::VSPLATH: return "HexagonISD::VSPLATH";
1758 case HexagonISD::VSRAH: return "HexagonISD::VSRAH";
1759 case HexagonISD::VSRAW: return "HexagonISD::VSRAW";
1760 case HexagonISD::VSRLH: return "HexagonISD::VSRLH";
1761 case HexagonISD::VSRLW: return "HexagonISD::VSRLW";
1762 case HexagonISD::VSXTBH: return "HexagonISD::VSXTBH";
1763 case HexagonISD::VSXTBW: return "HexagonISD::VSXTBW";
Matthias Braund04893f2015-05-07 21:33:59 +00001764 case HexagonISD::OP_END: break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001765 }
Matthias Braund04893f2015-05-07 21:33:59 +00001766 return nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001767}
1768
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001769bool HexagonTargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001770 EVT MTy1 = EVT::getEVT(Ty1);
1771 EVT MTy2 = EVT::getEVT(Ty2);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001772 if (!MTy1.isSimple() || !MTy2.isSimple())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001773 return false;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001774 return (MTy1.getSimpleVT() == MVT::i64) && (MTy2.getSimpleVT() == MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001775}
1776
1777bool HexagonTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001778 if (!VT1.isSimple() || !VT2.isSimple())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001779 return false;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001780 return (VT1.getSimpleVT() == MVT::i64) && (VT2.getSimpleVT() == MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001781}
1782
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001783// shouldExpandBuildVectorWithShuffles
1784// Should we expand the build vector with shuffles?
1785bool
1786HexagonTargetLowering::shouldExpandBuildVectorWithShuffles(EVT VT,
1787 unsigned DefinedValues) const {
1788
1789 // Hexagon vector shuffle operates on element sizes of bytes or halfwords
1790 EVT EltVT = VT.getVectorElementType();
1791 int EltBits = EltVT.getSizeInBits();
1792 if ((EltBits != 8) && (EltBits != 16))
1793 return false;
1794
1795 return TargetLowering::shouldExpandBuildVectorWithShuffles(VT, DefinedValues);
1796}
1797
1798// LowerVECTOR_SHUFFLE - Lower a vector shuffle (V1, V2, V3). V1 and
1799// V2 are the two vectors to select data from, V3 is the permutation.
1800static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
1801 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
1802 SDValue V1 = Op.getOperand(0);
1803 SDValue V2 = Op.getOperand(1);
1804 SDLoc dl(Op);
1805 EVT VT = Op.getValueType();
1806
1807 if (V2.getOpcode() == ISD::UNDEF)
1808 V2 = V1;
1809
1810 if (SVN->isSplat()) {
1811 int Lane = SVN->getSplatIndex();
1812 if (Lane == -1) Lane = 0;
1813
1814 // Test if V1 is a SCALAR_TO_VECTOR.
1815 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR)
1816 return createSplat(DAG, dl, VT, V1.getOperand(0));
1817
1818 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
1819 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
1820 // reaches it).
1821 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
1822 !isa<ConstantSDNode>(V1.getOperand(0))) {
1823 bool IsScalarToVector = true;
1824 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
1825 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
1826 IsScalarToVector = false;
1827 break;
1828 }
1829 if (IsScalarToVector)
1830 return createSplat(DAG, dl, VT, V1.getOperand(0));
1831 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001832 return createSplat(DAG, dl, VT, DAG.getConstant(Lane, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001833 }
1834
1835 // FIXME: We need to support more general vector shuffles. See
1836 // below the comment from the ARM backend that deals in the general
1837 // case with the vector shuffles. For now, let expand handle these.
1838 return SDValue();
1839
1840 // If the shuffle is not directly supported and it has 4 elements, use
1841 // the PerfectShuffle-generated table to synthesize it from other shuffles.
1842}
1843
1844// If BUILD_VECTOR has same base element repeated several times,
1845// report true.
1846static bool isCommonSplatElement(BuildVectorSDNode *BVN) {
1847 unsigned NElts = BVN->getNumOperands();
1848 SDValue V0 = BVN->getOperand(0);
1849
1850 for (unsigned i = 1, e = NElts; i != e; ++i) {
1851 if (BVN->getOperand(i) != V0)
1852 return false;
1853 }
1854 return true;
1855}
1856
1857// LowerVECTOR_SHIFT - Lower a vector shift. Try to convert
1858// <VT> = SHL/SRA/SRL <VT> by <VT> to Hexagon specific
1859// <VT> = SHL/SRA/SRL <VT> by <IT/i32>.
1860static SDValue LowerVECTOR_SHIFT(SDValue Op, SelectionDAG &DAG) {
1861 BuildVectorSDNode *BVN = 0;
1862 SDValue V1 = Op.getOperand(0);
1863 SDValue V2 = Op.getOperand(1);
1864 SDValue V3;
1865 SDLoc dl(Op);
1866 EVT VT = Op.getValueType();
1867
1868 if ((BVN = dyn_cast<BuildVectorSDNode>(V1.getNode())) &&
1869 isCommonSplatElement(BVN))
1870 V3 = V2;
1871 else if ((BVN = dyn_cast<BuildVectorSDNode>(V2.getNode())) &&
1872 isCommonSplatElement(BVN))
1873 V3 = V1;
1874 else
1875 return SDValue();
1876
1877 SDValue CommonSplat = BVN->getOperand(0);
1878 SDValue Result;
1879
1880 if (VT.getSimpleVT() == MVT::v4i16) {
1881 switch (Op.getOpcode()) {
1882 case ISD::SRA:
1883 Result = DAG.getNode(HexagonISD::VSRAH, dl, VT, V3, CommonSplat);
1884 break;
1885 case ISD::SHL:
1886 Result = DAG.getNode(HexagonISD::VSHLH, dl, VT, V3, CommonSplat);
1887 break;
1888 case ISD::SRL:
1889 Result = DAG.getNode(HexagonISD::VSRLH, dl, VT, V3, CommonSplat);
1890 break;
1891 default:
1892 return SDValue();
1893 }
1894 } else if (VT.getSimpleVT() == MVT::v2i32) {
1895 switch (Op.getOpcode()) {
1896 case ISD::SRA:
1897 Result = DAG.getNode(HexagonISD::VSRAW, dl, VT, V3, CommonSplat);
1898 break;
1899 case ISD::SHL:
1900 Result = DAG.getNode(HexagonISD::VSHLW, dl, VT, V3, CommonSplat);
1901 break;
1902 case ISD::SRL:
1903 Result = DAG.getNode(HexagonISD::VSRLW, dl, VT, V3, CommonSplat);
1904 break;
1905 default:
1906 return SDValue();
1907 }
1908 } else {
1909 return SDValue();
1910 }
1911
1912 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
1913}
1914
1915SDValue
1916HexagonTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
1917 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
1918 SDLoc dl(Op);
1919 EVT VT = Op.getValueType();
1920
1921 unsigned Size = VT.getSizeInBits();
1922
1923 // A vector larger than 64 bits cannot be represented in Hexagon.
1924 // Expand will split the vector.
1925 if (Size > 64)
1926 return SDValue();
1927
1928 APInt APSplatBits, APSplatUndef;
1929 unsigned SplatBitSize;
1930 bool HasAnyUndefs;
1931 unsigned NElts = BVN->getNumOperands();
1932
1933 // Try to generate a SPLAT instruction.
1934 if ((VT.getSimpleVT() == MVT::v4i8 || VT.getSimpleVT() == MVT::v4i16) &&
1935 (BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
1936 HasAnyUndefs, 0, true) && SplatBitSize <= 16)) {
1937 unsigned SplatBits = APSplatBits.getZExtValue();
1938 int32_t SextVal = ((int32_t) (SplatBits << (32 - SplatBitSize)) >>
1939 (32 - SplatBitSize));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001940 return createSplat(DAG, dl, VT, DAG.getConstant(SextVal, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001941 }
1942
1943 // Try to generate COMBINE to build v2i32 vectors.
1944 if (VT.getSimpleVT() == MVT::v2i32) {
1945 SDValue V0 = BVN->getOperand(0);
1946 SDValue V1 = BVN->getOperand(1);
1947
1948 if (V0.getOpcode() == ISD::UNDEF)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001949 V0 = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001950 if (V1.getOpcode() == ISD::UNDEF)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001951 V1 = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001952
1953 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(V0);
1954 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(V1);
1955 // If the element isn't a constant, it is in a register:
1956 // generate a COMBINE Register Register instruction.
1957 if (!C0 || !C1)
1958 return DAG.getNode(HexagonISD::COMBINE, dl, VT, V1, V0);
1959
1960 // If one of the operands is an 8 bit integer constant, generate
1961 // a COMBINE Immediate Immediate instruction.
1962 if (isInt<8>(C0->getSExtValue()) ||
1963 isInt<8>(C1->getSExtValue()))
1964 return DAG.getNode(HexagonISD::COMBINE, dl, VT, V1, V0);
1965 }
1966
1967 // Try to generate a S2_packhl to build v2i16 vectors.
1968 if (VT.getSimpleVT() == MVT::v2i16) {
1969 for (unsigned i = 0, e = NElts; i != e; ++i) {
1970 if (BVN->getOperand(i).getOpcode() == ISD::UNDEF)
1971 continue;
1972 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(BVN->getOperand(i));
1973 // If the element isn't a constant, it is in a register:
1974 // generate a S2_packhl instruction.
1975 if (!Cst) {
1976 SDValue pack = DAG.getNode(HexagonISD::PACKHL, dl, MVT::v4i16,
1977 BVN->getOperand(1), BVN->getOperand(0));
1978
1979 return DAG.getTargetExtractSubreg(Hexagon::subreg_loreg, dl, MVT::v2i16,
1980 pack);
1981 }
1982 }
1983 }
1984
1985 // In the general case, generate a CONST32 or a CONST64 for constant vectors,
1986 // and insert_vector_elt for all the other cases.
1987 uint64_t Res = 0;
1988 unsigned EltSize = Size / NElts;
1989 SDValue ConstVal;
1990 uint64_t Mask = ~uint64_t(0ULL) >> (64 - EltSize);
1991 bool HasNonConstantElements = false;
1992
1993 for (unsigned i = 0, e = NElts; i != e; ++i) {
1994 // LLVM's BUILD_VECTOR operands are in Little Endian mode, whereas Hexagon's
1995 // combine, const64, etc. are Big Endian.
1996 unsigned OpIdx = NElts - i - 1;
1997 SDValue Operand = BVN->getOperand(OpIdx);
1998 if (Operand.getOpcode() == ISD::UNDEF)
1999 continue;
2000
2001 int64_t Val = 0;
2002 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Operand))
2003 Val = Cst->getSExtValue();
2004 else
2005 HasNonConstantElements = true;
2006
2007 Val &= Mask;
2008 Res = (Res << EltSize) | Val;
2009 }
2010
2011 if (Size == 64)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002012 ConstVal = DAG.getConstant(Res, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002013 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002014 ConstVal = DAG.getConstant(Res, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002015
2016 // When there are non constant operands, add them with INSERT_VECTOR_ELT to
2017 // ConstVal, the constant part of the vector.
2018 if (HasNonConstantElements) {
2019 EVT EltVT = VT.getVectorElementType();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002020 SDValue Width = DAG.getConstant(EltVT.getSizeInBits(), dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002021 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002022 DAG.getConstant(32, dl, MVT::i64));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002023
2024 for (unsigned i = 0, e = NElts; i != e; ++i) {
2025 // LLVM's BUILD_VECTOR operands are in Little Endian mode, whereas Hexagon
2026 // is Big Endian.
2027 unsigned OpIdx = NElts - i - 1;
2028 SDValue Operand = BVN->getOperand(OpIdx);
Benjamin Kramer619c4e52015-04-10 11:24:51 +00002029 if (isa<ConstantSDNode>(Operand))
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002030 // This operand is already in ConstVal.
2031 continue;
2032
2033 if (VT.getSizeInBits() == 64 &&
2034 Operand.getValueType().getSizeInBits() == 32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002035 SDValue C = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002036 Operand = DAG.getNode(HexagonISD::COMBINE, dl, VT, C, Operand);
2037 }
2038
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002039 SDValue Idx = DAG.getConstant(OpIdx, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002040 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i64, Idx, Width);
2041 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2042 const SDValue Ops[] = {ConstVal, Operand, Combined};
2043
2044 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002045 ConstVal = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002046 else
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002047 ConstVal = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002048 }
2049 }
2050
2051 return DAG.getNode(ISD::BITCAST, dl, VT, ConstVal);
2052}
2053
2054SDValue
2055HexagonTargetLowering::LowerCONCAT_VECTORS(SDValue Op,
2056 SelectionDAG &DAG) const {
2057 SDLoc dl(Op);
2058 EVT VT = Op.getValueType();
2059 unsigned NElts = Op.getNumOperands();
2060 SDValue Vec = Op.getOperand(0);
2061 EVT VecVT = Vec.getValueType();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002062 SDValue Width = DAG.getConstant(VecVT.getSizeInBits(), dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002063 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002064 DAG.getConstant(32, dl, MVT::i64));
2065 SDValue ConstVal = DAG.getConstant(0, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002066
2067 ConstantSDNode *W = dyn_cast<ConstantSDNode>(Width);
2068 ConstantSDNode *S = dyn_cast<ConstantSDNode>(Shifted);
2069
2070 if ((VecVT.getSimpleVT() == MVT::v2i16) && (NElts == 2) && W && S) {
2071 if ((W->getZExtValue() == 32) && ((S->getZExtValue() >> 32) == 32)) {
2072 // We are trying to concat two v2i16 to a single v4i16.
2073 SDValue Vec0 = Op.getOperand(1);
2074 SDValue Combined = DAG.getNode(HexagonISD::COMBINE, dl, VT, Vec0, Vec);
2075 return DAG.getNode(ISD::BITCAST, dl, VT, Combined);
2076 }
2077 }
2078
2079 if ((VecVT.getSimpleVT() == MVT::v4i8) && (NElts == 2) && W && S) {
2080 if ((W->getZExtValue() == 32) && ((S->getZExtValue() >> 32) == 32)) {
2081 // We are trying to concat two v4i8 to a single v8i8.
2082 SDValue Vec0 = Op.getOperand(1);
2083 SDValue Combined = DAG.getNode(HexagonISD::COMBINE, dl, VT, Vec0, Vec);
2084 return DAG.getNode(ISD::BITCAST, dl, VT, Combined);
2085 }
2086 }
2087
2088 for (unsigned i = 0, e = NElts; i != e; ++i) {
2089 unsigned OpIdx = NElts - i - 1;
2090 SDValue Operand = Op.getOperand(OpIdx);
2091
2092 if (VT.getSizeInBits() == 64 &&
2093 Operand.getValueType().getSizeInBits() == 32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002094 SDValue C = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002095 Operand = DAG.getNode(HexagonISD::COMBINE, dl, VT, C, Operand);
2096 }
2097
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002098 SDValue Idx = DAG.getConstant(OpIdx, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002099 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i64, Idx, Width);
2100 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2101 const SDValue Ops[] = {ConstVal, Operand, Combined};
2102
2103 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002104 ConstVal = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002105 else
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002106 ConstVal = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002107 }
2108
2109 return DAG.getNode(ISD::BITCAST, dl, VT, ConstVal);
2110}
2111
2112SDValue
2113HexagonTargetLowering::LowerEXTRACT_VECTOR(SDValue Op,
2114 SelectionDAG &DAG) const {
2115 EVT VT = Op.getValueType();
2116 int VTN = VT.isVector() ? VT.getVectorNumElements() : 1;
2117 SDLoc dl(Op);
2118 SDValue Idx = Op.getOperand(1);
2119 SDValue Vec = Op.getOperand(0);
2120 EVT VecVT = Vec.getValueType();
2121 EVT EltVT = VecVT.getVectorElementType();
2122 int EltSize = EltVT.getSizeInBits();
2123 SDValue Width = DAG.getConstant(Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT ?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002124 EltSize : VTN * EltSize, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002125
2126 // Constant element number.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002127 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Idx)) {
2128 uint64_t X = CI->getZExtValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002129 SDValue Offset = DAG.getConstant(X * EltSize, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002130 const SDValue Ops[] = {Vec, Width, Offset};
2131
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002132 ConstantSDNode *CW = dyn_cast<ConstantSDNode>(Width);
2133 assert(CW && "Non constant width in LowerEXTRACT_VECTOR");
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002134
2135 SDValue N;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002136 MVT SVT = VecVT.getSimpleVT();
2137 uint64_t W = CW->getZExtValue();
2138
2139 if (W == 32) {
2140 // Translate this node into EXTRACT_SUBREG.
2141 unsigned Subreg = (X == 0) ? Hexagon::subreg_loreg : 0;
2142
2143 if (X == 0)
2144 Subreg = Hexagon::subreg_loreg;
2145 else if (SVT == MVT::v2i32 && X == 1)
2146 Subreg = Hexagon::subreg_hireg;
2147 else if (SVT == MVT::v4i16 && X == 2)
2148 Subreg = Hexagon::subreg_hireg;
2149 else if (SVT == MVT::v8i8 && X == 4)
2150 Subreg = Hexagon::subreg_hireg;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002151 else
2152 llvm_unreachable("Bad offset");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002153 N = DAG.getTargetExtractSubreg(Subreg, dl, MVT::i32, Vec);
2154
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002155 } else if (VecVT.getSizeInBits() == 32) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002156 N = DAG.getNode(HexagonISD::EXTRACTU, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002157 } else {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002158 N = DAG.getNode(HexagonISD::EXTRACTU, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002159 if (VT.getSizeInBits() == 32)
2160 N = DAG.getTargetExtractSubreg(Hexagon::subreg_loreg, dl, MVT::i32, N);
2161 }
2162
2163 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2164 }
2165
2166 // Variable element number.
2167 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i32, Idx,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002168 DAG.getConstant(EltSize, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002169 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002170 DAG.getConstant(32, dl, MVT::i64));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002171 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2172
2173 const SDValue Ops[] = {Vec, Combined};
2174
2175 SDValue N;
2176 if (VecVT.getSizeInBits() == 32) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002177 N = DAG.getNode(HexagonISD::EXTRACTURP, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002178 } else {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002179 N = DAG.getNode(HexagonISD::EXTRACTURP, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002180 if (VT.getSizeInBits() == 32)
2181 N = DAG.getTargetExtractSubreg(Hexagon::subreg_loreg, dl, MVT::i32, N);
2182 }
2183 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2184}
2185
2186SDValue
2187HexagonTargetLowering::LowerINSERT_VECTOR(SDValue Op,
2188 SelectionDAG &DAG) const {
2189 EVT VT = Op.getValueType();
2190 int VTN = VT.isVector() ? VT.getVectorNumElements() : 1;
2191 SDLoc dl(Op);
2192 SDValue Vec = Op.getOperand(0);
2193 SDValue Val = Op.getOperand(1);
2194 SDValue Idx = Op.getOperand(2);
2195 EVT VecVT = Vec.getValueType();
2196 EVT EltVT = VecVT.getVectorElementType();
2197 int EltSize = EltVT.getSizeInBits();
2198 SDValue Width = DAG.getConstant(Op.getOpcode() == ISD::INSERT_VECTOR_ELT ?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002199 EltSize : VTN * EltSize, dl, MVT::i64);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002200
2201 if (ConstantSDNode *C = cast<ConstantSDNode>(Idx)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002202 SDValue Offset = DAG.getConstant(C->getSExtValue() * EltSize, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002203 const SDValue Ops[] = {Vec, Val, Width, Offset};
2204
2205 SDValue N;
2206 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002207 N = DAG.getNode(HexagonISD::INSERT, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002208 else
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002209 N = DAG.getNode(HexagonISD::INSERT, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002210
2211 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2212 }
2213
2214 // Variable element number.
2215 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i32, Idx,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002216 DAG.getConstant(EltSize, dl, MVT::i32));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002217 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002218 DAG.getConstant(32, dl, MVT::i64));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002219 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2220
2221 if (VT.getSizeInBits() == 64 &&
2222 Val.getValueType().getSizeInBits() == 32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002223 SDValue C = DAG.getConstant(0, dl, MVT::i32);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002224 Val = DAG.getNode(HexagonISD::COMBINE, dl, VT, C, Val);
2225 }
2226
2227 const SDValue Ops[] = {Vec, Val, Combined};
2228
2229 SDValue N;
2230 if (VT.getSizeInBits() == 32)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002231 N = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i32, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002232 else
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002233 N = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i64, Ops);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002234
2235 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2236}
2237
Tim Northovera4415852013-08-06 09:12:35 +00002238bool
2239HexagonTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
2240 // Assuming the caller does not have either a signext or zeroext modifier, and
2241 // only one value is accepted, any reasonable truncation is allowed.
2242 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
2243 return false;
2244
2245 // FIXME: in principle up to 64-bit could be made safe, but it would be very
2246 // fragile at the moment: any support for multiple value returns would be
2247 // liable to disallow tail calls involving i64 -> iN truncation in many cases.
2248 return Ty1->getPrimitiveSizeInBits() <= 32;
2249}
2250
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002251SDValue
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002252HexagonTargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
2253 SDValue Chain = Op.getOperand(0);
2254 SDValue Offset = Op.getOperand(1);
2255 SDValue Handler = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002256 SDLoc dl(Op);
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002257
2258 // Mark function as containing a call to EH_RETURN.
2259 HexagonMachineFunctionInfo *FuncInfo =
2260 DAG.getMachineFunction().getInfo<HexagonMachineFunctionInfo>();
2261 FuncInfo->setHasEHReturn();
2262
2263 unsigned OffsetReg = Hexagon::R28;
2264
2265 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(),
2266 DAG.getRegister(Hexagon::R30, getPointerTy()),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002267 DAG.getIntPtrConstant(4, dl));
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002268 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
2269 false, false, 0);
2270 Chain = DAG.getCopyToReg(Chain, dl, OffsetReg, Offset);
2271
2272 // Not needed we already use it as explict input to EH_RETURN.
2273 // MF.getRegInfo().addLiveOut(OffsetReg);
2274
2275 return DAG.getNode(HexagonISD::EH_RETURN, dl, MVT::Other, Chain);
2276}
2277
2278SDValue
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002279HexagonTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002280 unsigned Opc = Op.getOpcode();
2281 switch (Opc) {
2282 default:
2283#ifndef NDEBUG
2284 Op.getNode()->dumpr(&DAG);
2285 if (Opc > HexagonISD::OP_BEGIN && Opc < HexagonISD::OP_END)
2286 errs() << "Check for a non-legal type in this operation\n";
2287#endif
2288 llvm_unreachable("Should not custom lower this!");
2289 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
2290 case ISD::INSERT_SUBVECTOR: return LowerINSERT_VECTOR(Op, DAG);
2291 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR(Op, DAG);
2292 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_VECTOR(Op, DAG);
2293 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR(Op, DAG);
2294 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
2295 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002296 case ISD::SRA:
2297 case ISD::SHL:
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002298 case ISD::SRL: return LowerVECTOR_SHIFT(Op, DAG);
2299 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
2300 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
2301 // Frame & Return address. Currently unimplemented.
2302 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
2303 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
2304 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
2305 case ISD::GlobalAddress: return LowerGLOBALADDRESS(Op, DAG);
2306 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
2307 case ISD::VASTART: return LowerVASTART(Op, DAG);
2308 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002309 // Custom lower some vector loads.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002310 case ISD::LOAD: return LowerLOAD(Op, DAG);
2311 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
2312 case ISD::SETCC: return LowerSETCC(Op, DAG);
2313 case ISD::VSELECT: return LowerVSELECT(Op, DAG);
2314 case ISD::CTPOP: return LowerCTPOP(Op, DAG);
2315 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
2316 case ISD::INLINEASM: return LowerINLINEASM(Op, DAG);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002317 }
2318}
2319
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002320MachineBasicBlock *
2321HexagonTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
2322 MachineBasicBlock *BB)
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00002323 const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002324 switch (MI->getOpcode()) {
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00002325 case Hexagon::ALLOCA: {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002326 MachineFunction *MF = BB->getParent();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +00002327 auto *FuncInfo = MF->getInfo<HexagonMachineFunctionInfo>();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002328 FuncInfo->addAllocaAdjustInst(MI);
2329 return BB;
2330 }
Craig Toppere55c5562012-02-07 02:50:20 +00002331 default: llvm_unreachable("Unexpected instr type to insert");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002332 } // switch
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002333}
2334
2335//===----------------------------------------------------------------------===//
2336// Inline Assembly Support
2337//===----------------------------------------------------------------------===//
2338
Eric Christopher11e4df72015-02-26 22:38:43 +00002339std::pair<unsigned, const TargetRegisterClass *>
2340HexagonTargetLowering::getRegForInlineAsmConstraint(
2341 const TargetRegisterInfo *TRI, const std::string &Constraint,
2342 MVT VT) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002343 if (Constraint.size() == 1) {
2344 switch (Constraint[0]) {
2345 case 'r': // R0-R31
Chad Rosier295bd432013-06-22 18:37:38 +00002346 switch (VT.SimpleTy) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002347 default:
Craig Toppere55c5562012-02-07 02:50:20 +00002348 llvm_unreachable("getRegForInlineAsmConstraint Unhandled data type");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002349 case MVT::i32:
2350 case MVT::i16:
2351 case MVT::i8:
Sirish Pande69295b82012-05-10 20:20:25 +00002352 case MVT::f32:
Craig Topperc7242e02012-04-20 07:30:17 +00002353 return std::make_pair(0U, &Hexagon::IntRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002354 case MVT::i64:
Sirish Pande69295b82012-05-10 20:20:25 +00002355 case MVT::f64:
Craig Topperc7242e02012-04-20 07:30:17 +00002356 return std::make_pair(0U, &Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002357 }
2358 default:
Craig Toppere55c5562012-02-07 02:50:20 +00002359 llvm_unreachable("Unknown asm register class");
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002360 }
2361 }
2362
Eric Christopher11e4df72015-02-26 22:38:43 +00002363 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002364}
2365
Sirish Pande69295b82012-05-10 20:20:25 +00002366/// isFPImmLegal - Returns true if the target can instruction select the
2367/// specified FP immediate natively. If false, the legalizer will
2368/// materialize the FP immediate as a load from a constant pool.
2369bool HexagonTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002370 return Subtarget.hasV5TOps();
Sirish Pande69295b82012-05-10 20:20:25 +00002371}
2372
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002373/// isLegalAddressingMode - Return true if the addressing mode represented by
2374/// AM is legal for this target, for a load/store of the specified type.
2375bool HexagonTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +00002376 Type *Ty,
2377 unsigned AS) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002378 // Allows a signed-extended 11-bit immediate field.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002379 if (AM.BaseOffs <= -(1LL << 13) || AM.BaseOffs >= (1LL << 13)-1)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002380 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002381
2382 // No global is ever allowed as a base.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002383 if (AM.BaseGV)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002384 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002385
2386 int Scale = AM.Scale;
2387 if (Scale < 0) Scale = -Scale;
2388 switch (Scale) {
2389 case 0: // No scale reg, "r+i", "r", or just "i".
2390 break;
2391 default: // No scaled addressing mode.
2392 return false;
2393 }
2394 return true;
2395}
2396
2397/// isLegalICmpImmediate - Return true if the specified immediate is legal
2398/// icmp immediate, that is the target has icmp instructions which can compare
2399/// a register against the immediate without having to materialize the
2400/// immediate into a register.
2401bool HexagonTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
2402 return Imm >= -512 && Imm <= 511;
2403}
2404
2405/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2406/// for tail call optimization. Targets which want to do tail call
2407/// optimization should implement this function.
2408bool HexagonTargetLowering::IsEligibleForTailCallOptimization(
2409 SDValue Callee,
2410 CallingConv::ID CalleeCC,
2411 bool isVarArg,
2412 bool isCalleeStructRet,
2413 bool isCallerStructRet,
2414 const SmallVectorImpl<ISD::OutputArg> &Outs,
2415 const SmallVectorImpl<SDValue> &OutVals,
2416 const SmallVectorImpl<ISD::InputArg> &Ins,
2417 SelectionDAG& DAG) const {
2418 const Function *CallerF = DAG.getMachineFunction().getFunction();
2419 CallingConv::ID CallerCC = CallerF->getCallingConv();
2420 bool CCMatch = CallerCC == CalleeCC;
2421
2422 // ***************************************************************************
2423 // Look for obvious safe cases to perform tail call optimization that do not
2424 // require ABI changes.
2425 // ***************************************************************************
2426
2427 // If this is a tail call via a function pointer, then don't do it!
2428 if (!(dyn_cast<GlobalAddressSDNode>(Callee))
2429 && !(dyn_cast<ExternalSymbolSDNode>(Callee))) {
2430 return false;
2431 }
2432
2433 // Do not optimize if the calling conventions do not match.
2434 if (!CCMatch)
2435 return false;
2436
2437 // Do not tail call optimize vararg calls.
2438 if (isVarArg)
2439 return false;
2440
2441 // Also avoid tail call optimization if either caller or callee uses struct
2442 // return semantics.
2443 if (isCalleeStructRet || isCallerStructRet)
2444 return false;
2445
2446 // In addition to the cases above, we also disable Tail Call Optimization if
2447 // the calling convention code that at least one outgoing argument needs to
2448 // go on the stack. We cannot check that here because at this point that
2449 // information is not available.
2450 return true;
2451}
Colin LeMahieu025f8602014-12-08 21:19:18 +00002452
2453// Return true when the given node fits in a positive half word.
2454bool llvm::isPositiveHalfWord(SDNode *N) {
2455 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N);
2456 if (CN && CN->getSExtValue() > 0 && isInt<16>(CN->getSExtValue()))
2457 return true;
2458
2459 switch (N->getOpcode()) {
2460 default:
2461 return false;
2462 case ISD::SIGN_EXTEND_INREG:
2463 return true;
2464 }
2465}