blob: ce42a9bd70fcd555c8c7fef7d5d52906f5dc76a3 [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"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000016#include "Hexagon.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000017#include "HexagonMachineFunctionInfo.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000018#include "HexagonRegisterInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000019#include "HexagonSubtarget.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "HexagonTargetMachine.h"
21#include "HexagonTargetObjectFile.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000022#include "llvm/ADT/APInt.h"
23#include "llvm/ADT/ArrayRef.h"
24#include "llvm/ADT/SmallVector.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000025#include "llvm/CodeGen/CallingConvLower.h"
26#include "llvm/CodeGen/MachineFrameInfo.h"
27#include "llvm/CodeGen/MachineFunction.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000028#include "llvm/CodeGen/MachineMemOperand.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000029#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000030#include "llvm/CodeGen/RuntimeLibcalls.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000031#include "llvm/CodeGen/SelectionDAG.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000032#include "llvm/CodeGen/TargetCallingConv.h"
Craig Topper2fa14362018-03-29 17:21:10 +000033#include "llvm/CodeGen/ValueTypes.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000034#include "llvm/IR/BasicBlock.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000035#include "llvm/IR/CallingConv.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000036#include "llvm/IR/DataLayout.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000037#include "llvm/IR/DerivedTypes.h"
38#include "llvm/IR/Function.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000039#include "llvm/IR/GlobalValue.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000040#include "llvm/IR/InlineAsm.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000041#include "llvm/IR/Instructions.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000042#include "llvm/IR/Intrinsics.h"
Krzysztof Parzyszekdc7a5572018-03-29 13:52:46 +000043#include "llvm/IR/IntrinsicInst.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000044#include "llvm/IR/Module.h"
45#include "llvm/IR/Type.h"
46#include "llvm/IR/Value.h"
47#include "llvm/MC/MCRegisterInfo.h"
48#include "llvm/Support/Casting.h"
49#include "llvm/Support/CodeGen.h"
NAKAMURA Takumi54eed762012-04-21 15:31:36 +000050#include "llvm/Support/CommandLine.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000051#include "llvm/Support/Debug.h"
52#include "llvm/Support/ErrorHandling.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000053#include "llvm/Support/MathExtras.h"
NAKAMURA Takumie30303f2012-04-21 15:31:45 +000054#include "llvm/Support/raw_ostream.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000055#include "llvm/Target/TargetMachine.h"
56#include <algorithm>
57#include <cassert>
58#include <cstddef>
59#include <cstdint>
60#include <limits>
61#include <utility>
NAKAMURA Takumi54eed762012-04-21 15:31:36 +000062
Craig Topperb25fda92012-03-17 18:46:09 +000063using namespace llvm;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000064
Chandler Carruthe96dd892014-04-21 22:55:11 +000065#define DEBUG_TYPE "hexagon-lowering"
66
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +000067static cl::opt<bool> EmitJumpTables("hexagon-emit-jump-tables",
68 cl::init(true), cl::Hidden,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +000069 cl::desc("Control jump table emission on Hexagon target"));
70
71static cl::opt<bool> EnableHexSDNodeSched("enable-hexagon-sdnode-sched",
72 cl::Hidden, cl::ZeroOrMore, cl::init(false),
73 cl::desc("Enable Hexagon SDNode scheduling"));
74
75static cl::opt<bool> EnableFastMath("ffast-math",
76 cl::Hidden, cl::ZeroOrMore, cl::init(false),
77 cl::desc("Enable Fast Math processing"));
78
79static cl::opt<int> MinimumJumpTables("minimum-jump-tables",
80 cl::Hidden, cl::ZeroOrMore, cl::init(5),
81 cl::desc("Set minimum jump tables"));
82
83static cl::opt<int> MaxStoresPerMemcpyCL("max-store-memcpy",
84 cl::Hidden, cl::ZeroOrMore, cl::init(6),
85 cl::desc("Max #stores to inline memcpy"));
86
87static cl::opt<int> MaxStoresPerMemcpyOptSizeCL("max-store-memcpy-Os",
88 cl::Hidden, cl::ZeroOrMore, cl::init(4),
89 cl::desc("Max #stores to inline memcpy"));
90
91static cl::opt<int> MaxStoresPerMemmoveCL("max-store-memmove",
92 cl::Hidden, cl::ZeroOrMore, cl::init(6),
93 cl::desc("Max #stores to inline memmove"));
94
95static cl::opt<int> MaxStoresPerMemmoveOptSizeCL("max-store-memmove-Os",
96 cl::Hidden, cl::ZeroOrMore, cl::init(4),
97 cl::desc("Max #stores to inline memmove"));
98
99static cl::opt<int> MaxStoresPerMemsetCL("max-store-memset",
100 cl::Hidden, cl::ZeroOrMore, cl::init(8),
101 cl::desc("Max #stores to inline memset"));
102
103static cl::opt<int> MaxStoresPerMemsetOptSizeCL("max-store-memset-Os",
104 cl::Hidden, cl::ZeroOrMore, cl::init(4),
105 cl::desc("Max #stores to inline memset"));
106
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +0000107static cl::opt<bool> AlignLoads("hexagon-align-loads",
108 cl::Hidden, cl::init(false),
109 cl::desc("Rewrite unaligned loads as a pair of aligned loads"));
110
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000111
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000112namespace {
Eugene Zelenko58655bb2016-12-17 01:09:05 +0000113
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +0000114 class HexagonCCState : public CCState {
Krzysztof Parzyszek18e0d2a2018-02-15 15:47:53 +0000115 unsigned NumNamedVarArgParams = 0;
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000116
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +0000117 public:
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000118 HexagonCCState(CallingConv::ID CC, bool IsVarArg, MachineFunction &MF,
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +0000119 SmallVectorImpl<CCValAssign> &locs, LLVMContext &C,
Krzysztof Parzyszeke0d7de72018-02-15 17:20:07 +0000120 unsigned NumNamedArgs)
121 : CCState(CC, IsVarArg, MF, locs, C),
122 NumNamedVarArgParams(NumNamedArgs) {}
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +0000123 unsigned getNumNamedVarArgParams() const { return NumNamedVarArgParams; }
124 };
125
Eugene Zelenko58655bb2016-12-17 01:09:05 +0000126} // end anonymous namespace
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000127
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000128
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000129// Implement calling convention for Hexagon.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000130
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000131static bool CC_SkipOdd(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
132 CCValAssign::LocInfo &LocInfo,
133 ISD::ArgFlagsTy &ArgFlags, CCState &State) {
134 static const MCPhysReg ArgRegs[] = {
135 Hexagon::R0, Hexagon::R1, Hexagon::R2,
136 Hexagon::R3, Hexagon::R4, Hexagon::R5
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000137 };
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000138 const unsigned NumArgRegs = array_lengthof(ArgRegs);
139 unsigned RegNum = State.getFirstUnallocated(ArgRegs);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000140
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000141 // RegNum is an index into ArgRegs: skip a register if RegNum is odd.
142 if (RegNum != NumArgRegs && RegNum % 2 == 1)
143 State.AllocateReg(ArgRegs[RegNum]);
144
145 // Always return false here, as this function only makes sure that the first
146 // unallocated register has an even register number and does not actually
147 // allocate a register for the current argument.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000148 return false;
149}
150
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000151#include "HexagonGenCallingConv.inc"
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000152
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000153
Craig Topper18e69f42016-04-15 06:20:21 +0000154void HexagonTargetLowering::promoteLdStType(MVT VT, MVT PromotedLdStVT) {
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000155 if (VT != PromotedLdStVT) {
Craig Topper18e69f42016-04-15 06:20:21 +0000156 setOperationAction(ISD::LOAD, VT, Promote);
157 AddPromotedToType(ISD::LOAD, VT, PromotedLdStVT);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000158
Craig Topper18e69f42016-04-15 06:20:21 +0000159 setOperationAction(ISD::STORE, VT, Promote);
160 AddPromotedToType(ISD::STORE, VT, PromotedLdStVT);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000161 }
162}
163
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000164SDValue
165HexagonTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG)
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000166 const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000167 return SDValue();
168}
169
170/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
171/// by "Src" to address "Dst" of size "Size". Alignment information is
172/// specified by the specific parameter attribute. The copy will be passed as
173/// a byval function parameter. Sometimes what we are copying is the end of a
174/// larger object, the part that does not fit in registers.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000175static SDValue CreateCopyOfByValArgument(SDValue Src, SDValue Dst,
176 SDValue Chain, ISD::ArgFlagsTy Flags,
177 SelectionDAG &DAG, const SDLoc &dl) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000178 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000179 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
180 /*isVolatile=*/false, /*AlwaysInline=*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +0000181 /*isTailCall=*/false,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000182 MachinePointerInfo(), MachinePointerInfo());
183}
184
Krzysztof Parzyszek56199522017-04-13 15:05:51 +0000185bool
186HexagonTargetLowering::CanLowerReturn(
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000187 CallingConv::ID CallConv, MachineFunction &MF, bool IsVarArg,
Krzysztof Parzyszek56199522017-04-13 15:05:51 +0000188 const SmallVectorImpl<ISD::OutputArg> &Outs,
189 LLVMContext &Context) const {
190 SmallVector<CCValAssign, 16> RVLocs;
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000191 CCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
192
193 if (MF.getSubtarget<HexagonSubtarget>().useHVXOps())
194 return CCInfo.CheckReturn(Outs, RetCC_Hexagon_HVX);
Krzysztof Parzyszek56199522017-04-13 15:05:51 +0000195 return CCInfo.CheckReturn(Outs, RetCC_Hexagon);
196}
197
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000198// LowerReturn - Lower ISD::RET. If a struct is larger than 8 bytes and is
199// passed by value, the function prototype is modified to return void and
200// the value is stored in memory pointed by a pointer passed by caller.
201SDValue
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000202HexagonTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000203 bool IsVarArg,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000204 const SmallVectorImpl<ISD::OutputArg> &Outs,
205 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000206 const SDLoc &dl, SelectionDAG &DAG) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000207 // CCValAssign - represent the assignment of the return value to locations.
208 SmallVector<CCValAssign, 16> RVLocs;
209
210 // CCState - Info about the registers and stack slot.
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000211 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
Eric Christopherb5217502014-08-06 18:45:26 +0000212 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000213
214 // Analyze return values of ISD::RET
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000215 if (Subtarget.useHVXOps())
216 CCInfo.AnalyzeReturn(Outs, RetCC_Hexagon_HVX);
217 else
218 CCInfo.AnalyzeReturn(Outs, RetCC_Hexagon);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000219
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000220 SDValue Flag;
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000221 SmallVector<SDValue, 4> RetOps(1, Chain);
222
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000223 // Copy the result values into the output registers.
224 for (unsigned i = 0; i != RVLocs.size(); ++i) {
225 CCValAssign &VA = RVLocs[i];
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000226
227 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), OutVals[i], Flag);
228
229 // Guarantee that all emitted copies are stuck together with flags.
230 Flag = Chain.getValue(1);
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000231 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000232 }
233
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000234 RetOps[0] = Chain; // Update chain.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000235
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000236 // Add the flag if we have it.
237 if (Flag.getNode())
238 RetOps.push_back(Flag);
239
Craig Topper48d114b2014-04-26 18:35:24 +0000240 return DAG.getNode(HexagonISD::RET_FLAG, dl, MVT::Other, RetOps);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000241}
242
Matt Arsenault31380752017-04-18 21:16:46 +0000243bool HexagonTargetLowering::mayBeEmittedAsTailCall(const CallInst *CI) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000244 // If either no tail call or told not to tail call at all, don't.
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000245 auto Attr =
246 CI->getParent()->getParent()->getFnAttribute("disable-tail-calls");
247 if (!CI->isTailCall() || Attr.getValueAsString() == "true")
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000248 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000249
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000250 return true;
251}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000252
253/// LowerCallResult - Lower the result values of an ISD::CALL into the
254/// appropriate copies out of appropriate physical registers. This assumes that
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000255/// Chain/Glue are the input chain/glue to use, and that TheCall is the call
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000256/// being lowered. Returns a SDNode with the same number of values as the
257/// ISD::CALL.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000258SDValue HexagonTargetLowering::LowerCallResult(
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000259 SDValue Chain, SDValue Glue, CallingConv::ID CallConv, bool IsVarArg,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000260 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
261 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals,
262 const SmallVectorImpl<SDValue> &OutVals, SDValue Callee) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000263 // Assign locations to each value returned by this call.
264 SmallVector<CCValAssign, 16> RVLocs;
265
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000266 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
Eric Christopherb5217502014-08-06 18:45:26 +0000267 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000268
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000269 if (Subtarget.useHVXOps())
270 CCInfo.AnalyzeCallResult(Ins, RetCC_Hexagon_HVX);
271 else
272 CCInfo.AnalyzeCallResult(Ins, RetCC_Hexagon);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000273
274 // Copy all of the result registers out of their specified physreg.
275 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000276 SDValue RetVal;
277 if (RVLocs[i].getValVT() == MVT::i1) {
278 // Return values of type MVT::i1 require special handling. The reason
279 // is that MVT::i1 is associated with the PredRegs register class, but
280 // values of that type are still returned in R0. Generate an explicit
281 // copy into a predicate register from R0, and treat the value of the
282 // predicate register as the call result.
283 auto &MRI = DAG.getMachineFunction().getRegInfo();
284 SDValue FR0 = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000285 MVT::i32, Glue);
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000286 // FR0 = (Value, Chain, Glue)
287 unsigned PredR = MRI.createVirtualRegister(&Hexagon::PredRegsRegClass);
288 SDValue TPR = DAG.getCopyToReg(FR0.getValue(1), dl, PredR,
289 FR0.getValue(0), FR0.getValue(2));
290 // TPR = (Chain, Glue)
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000291 // Don't glue this CopyFromReg, because it copies from a virtual
292 // register. If it is glued to the call, InstrEmitter will add it
293 // as an implicit def to the call (EmitMachineNode).
294 RetVal = DAG.getCopyFromReg(TPR.getValue(0), dl, PredR, MVT::i1);
295 Glue = TPR.getValue(1);
Krzysztof Parzyszek6f06b6e2017-10-23 19:35:25 +0000296 Chain = TPR.getValue(0);
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000297 } else {
298 RetVal = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000299 RVLocs[i].getValVT(), Glue);
300 Glue = RetVal.getValue(2);
Krzysztof Parzyszek6f06b6e2017-10-23 19:35:25 +0000301 Chain = RetVal.getValue(1);
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000302 }
303 InVals.push_back(RetVal.getValue(0));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000304 }
305
306 return Chain;
307}
308
309/// LowerCall - Functions arguments are copied from virtual regs to
310/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
311SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +0000312HexagonTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000313 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +0000314 SelectionDAG &DAG = CLI.DAG;
Craig Topperb94011f2013-07-14 04:42:23 +0000315 SDLoc &dl = CLI.DL;
316 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
317 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
318 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000319 SDValue Chain = CLI.Chain;
320 SDValue Callee = CLI.Callee;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000321 CallingConv::ID CallConv = CLI.CallConv;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000322 bool IsVarArg = CLI.IsVarArg;
323 bool DoesNotReturn = CLI.DoesNotReturn;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000324
Krzysztof Parzyszeke0d7de72018-02-15 17:20:07 +0000325 bool IsStructRet = Outs.empty() ? false : Outs[0].Flags.isSRet();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000326 MachineFunction &MF = DAG.getMachineFunction();
Krzysztof Parzyszekf67cd822017-07-11 17:11:54 +0000327 MachineFrameInfo &MFI = MF.getFrameInfo();
Mehdi Amini44ede332015-07-09 02:09:04 +0000328 auto PtrVT = getPointerTy(MF.getDataLayout());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000329
Krzysztof Parzyszeke0d7de72018-02-15 17:20:07 +0000330 unsigned NumParams = CLI.CS.getInstruction()
331 ? CLI.CS.getFunctionType()->getNumParams()
332 : 0;
333 if (GlobalAddressSDNode *GAN = dyn_cast<GlobalAddressSDNode>(Callee))
334 Callee = DAG.getTargetGlobalAddress(GAN->getGlobal(), dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000335
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000336 // Analyze operands of the call, assigning locations to each operand.
337 SmallVector<CCValAssign, 16> ArgLocs;
Krzysztof Parzyszek18e0d2a2018-02-15 15:47:53 +0000338 HexagonCCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext(),
Krzysztof Parzyszeke0d7de72018-02-15 17:20:07 +0000339 NumParams);
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000340
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000341 if (Subtarget.useHVXOps())
342 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon_HVX);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000343 else
344 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon);
345
Matthias Braunf1caa282017-12-15 22:22:58 +0000346 auto Attr = MF.getFunction().getFnAttribute("disable-tail-calls");
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000347 if (Attr.getValueAsString() == "true")
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000348 CLI.IsTailCall = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000349
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000350 if (CLI.IsTailCall) {
Matthias Braunf1caa282017-12-15 22:22:58 +0000351 bool StructAttrFlag = MF.getFunction().hasStructRetAttr();
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000352 CLI.IsTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
353 IsVarArg, IsStructRet, StructAttrFlag, Outs,
354 OutVals, Ins, DAG);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000355 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000356 CCValAssign &VA = ArgLocs[i];
357 if (VA.isMemLoc()) {
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000358 CLI.IsTailCall = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000359 break;
360 }
361 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000362 LLVM_DEBUG(dbgs() << (CLI.IsTailCall ? "Eligible for Tail Call\n"
363 : "Argument must be passed on stack. "
364 "Not eligible for Tail Call\n"));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000365 }
366 // Get a count of how many bytes are to be pushed on the stack.
367 unsigned NumBytes = CCInfo.getNextStackOffset();
368 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
369 SmallVector<SDValue, 8> MemOpChains;
370
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000371 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Mehdi Amini44ede332015-07-09 02:09:04 +0000372 SDValue StackPtr =
373 DAG.getCopyFromReg(Chain, dl, HRI.getStackRegister(), PtrVT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000374
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000375 bool NeedsArgAlign = false;
376 unsigned LargestAlignSeen = 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000377 // Walk the register/memloc assignments, inserting copies/loads.
378 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
379 CCValAssign &VA = ArgLocs[i];
380 SDValue Arg = OutVals[i];
381 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000382 // Record if we need > 8 byte alignment on an argument.
Krzysztof Parzyszekac1966e2017-11-27 18:12:16 +0000383 bool ArgAlign = Subtarget.isHVXVectorType(VA.getValVT());
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000384 NeedsArgAlign |= ArgAlign;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000385
386 // Promote the value if needed.
387 switch (VA.getLocInfo()) {
388 default:
Krzysztof Parzyszek8f6b0c82017-12-20 14:44:05 +0000389 // Loc info must be one of Full, BCvt, SExt, ZExt, or AExt.
Craig Toppere55c5562012-02-07 02:50:20 +0000390 llvm_unreachable("Unknown loc info!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000391 case CCValAssign::Full:
392 break;
Krzysztof Parzyszek8f6b0c82017-12-20 14:44:05 +0000393 case CCValAssign::BCvt:
394 Arg = DAG.getBitcast(VA.getLocVT(), Arg);
395 break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000396 case CCValAssign::SExt:
397 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
398 break;
399 case CCValAssign::ZExt:
400 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
401 break;
402 case CCValAssign::AExt:
403 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
404 break;
405 }
406
407 if (VA.isMemLoc()) {
408 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000409 SDValue MemAddr = DAG.getConstant(LocMemOffset, dl,
410 StackPtr.getValueType());
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000411 MemAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, MemAddr);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000412 if (ArgAlign)
413 LargestAlignSeen = std::max(LargestAlignSeen,
414 VA.getLocVT().getStoreSizeInBits() >> 3);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000415 if (Flags.isByVal()) {
416 // The argument is a struct passed by value. According to LLVM, "Arg"
Fangrui Song956ee792018-03-30 22:22:31 +0000417 // is a pointer.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000418 MemOpChains.push_back(CreateCopyOfByValArgument(Arg, MemAddr, Chain,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000419 Flags, DAG, dl));
420 } else {
Alex Lorenze40c8a22015-08-11 23:09:45 +0000421 MachinePointerInfo LocPI = MachinePointerInfo::getStack(
422 DAG.getMachineFunction(), LocMemOffset);
Justin Lebar9c375812016-07-15 18:27:10 +0000423 SDValue S = DAG.getStore(Chain, dl, Arg, MemAddr, LocPI);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000424 MemOpChains.push_back(S);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000425 }
426 continue;
427 }
428
429 // Arguments that can be passed on register must be kept at RegsToPass
430 // vector.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000431 if (VA.isRegLoc())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000432 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000433 }
434
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000435 if (NeedsArgAlign && Subtarget.hasV60TOps()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000436 LLVM_DEBUG(dbgs() << "Function needs byte stack align due to call args\n");
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000437 unsigned VecAlign = HRI.getSpillAlignment(Hexagon::HvxVRRegClass);
438 LargestAlignSeen = std::max(LargestAlignSeen, VecAlign);
Matthias Braun941a7052016-07-28 18:40:00 +0000439 MFI.ensureMaxAlignment(LargestAlignSeen);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000440 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000441 // Transform all store nodes into one single node because all store
442 // nodes are independent of each other.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000443 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +0000444 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000445
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000446 SDValue Glue;
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000447 if (!CLI.IsTailCall) {
Serge Pavlovd526b132017-05-09 13:35:13 +0000448 Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, dl);
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000449 Glue = Chain.getValue(1);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000450 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000451
452 // Build a sequence of copy-to-reg nodes chained together with token
453 // chain and flag operands which copy the outgoing args into registers.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000454 // The Glue is necessary since all emitted instructions must be
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000455 // stuck together.
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000456 if (!CLI.IsTailCall) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000457 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
458 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000459 RegsToPass[i].second, Glue);
460 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000461 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000462 } else {
463 // For tail calls lower the arguments to the 'real' stack slot.
464 //
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000465 // Force all the incoming stack arguments to be loaded from the stack
466 // before any new outgoing arguments are stored to the stack, because the
467 // outgoing stack slots may alias the incoming argument stack slots, and
468 // the alias isn't otherwise explicit. This is slightly more conservative
469 // than necessary, because it means that each store effectively depends
470 // on every argument instead of just those arguments it would clobber.
471 //
Benjamin Kramerbde91762012-06-02 10:20:22 +0000472 // Do not flag preceding copytoreg stuff together with the following stuff.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000473 Glue = SDValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000474 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
475 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000476 RegsToPass[i].second, Glue);
477 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000478 }
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000479 Glue = SDValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000480 }
481
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000482 bool LongCalls = MF.getSubtarget<HexagonSubtarget>().useLongCalls();
483 unsigned Flags = LongCalls ? HexagonII::HMOTF_ConstExtended : 0;
484
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000485 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
486 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
487 // node so that legalize doesn't hack it.
Tobias Edler von Kochb51460c2015-12-16 17:29:37 +0000488 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000489 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, PtrVT, 0, Flags);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000490 } else if (ExternalSymbolSDNode *S =
491 dyn_cast<ExternalSymbolSDNode>(Callee)) {
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000492 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), PtrVT, Flags);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000493 }
494
495 // Returns a chain & a flag for retval copy to use.
496 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
497 SmallVector<SDValue, 8> Ops;
498 Ops.push_back(Chain);
499 Ops.push_back(Callee);
500
501 // Add argument registers to the end of the list so that they are
502 // known live into the call.
503 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
504 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
505 RegsToPass[i].second.getValueType()));
506 }
507
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000508 const uint32_t *Mask = HRI.getCallPreservedMask(MF, CallConv);
509 assert(Mask && "Missing call preserved mask for calling convention");
510 Ops.push_back(DAG.getRegisterMask(Mask));
511
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000512 if (Glue.getNode())
513 Ops.push_back(Glue);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000514
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000515 if (CLI.IsTailCall) {
Krzysztof Parzyszekf67cd822017-07-11 17:11:54 +0000516 MFI.setHasTailCall();
Craig Topper48d114b2014-04-26 18:35:24 +0000517 return DAG.getNode(HexagonISD::TC_RETURN, dl, NodeTys, Ops);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +0000518 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000519
Krzysztof Parzyszekf67cd822017-07-11 17:11:54 +0000520 // Set this here because we need to know this for "hasFP" in frame lowering.
521 // The target-independent code calls getFrameRegister before setting it, and
522 // getFrameRegister uses hasFP to determine whether the function has FP.
523 MFI.setHasCalls(true);
524
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +0000525 unsigned OpCode = DoesNotReturn ? HexagonISD::CALLnr : HexagonISD::CALL;
Colin LeMahieu2e3a26d2015-01-16 17:05:27 +0000526 Chain = DAG.getNode(OpCode, dl, NodeTys, Ops);
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000527 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000528
529 // Create the CALLSEQ_END node.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000530 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000531 DAG.getIntPtrConstant(0, dl, true), Glue, dl);
532 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000533
534 // Handle result values, copying them out of physregs into vregs that we
535 // return.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000536 return LowerCallResult(Chain, Glue, CallConv, IsVarArg, Ins, dl, DAG,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000537 InVals, OutVals, Callee);
538}
539
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +0000540/// Returns true by value, base pointer and offset pointer and addressing
541/// mode by reference if this node can be combined with a load / store to
542/// form a post-indexed load / store.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000543bool HexagonTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +0000544 SDValue &Base, SDValue &Offset, ISD::MemIndexedMode &AM,
545 SelectionDAG &DAG) const {
546 LSBaseSDNode *LSN = dyn_cast<LSBaseSDNode>(N);
547 if (!LSN)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000548 return false;
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +0000549 EVT VT = LSN->getMemoryVT();
550 if (!VT.isSimple())
551 return false;
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +0000552 bool IsLegalType = VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32 ||
Hans Wennborg13e8a852018-03-19 12:55:58 +0000553 VT == MVT::i64 || VT == MVT::v2i16 || VT == MVT::v2i32 ||
554 VT == MVT::v4i8 || VT == MVT::v4i16 || VT == MVT::v8i8 ||
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +0000555 Subtarget.isHVXVectorType(VT.getSimpleVT());
556 if (!IsLegalType)
557 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000558
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +0000559 if (Op->getOpcode() != ISD::ADD)
560 return false;
561 Base = Op->getOperand(0);
562 Offset = Op->getOperand(1);
563 if (!isa<ConstantSDNode>(Offset.getNode()))
564 return false;
565 AM = ISD::POST_INC;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000566
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +0000567 int32_t V = cast<ConstantSDNode>(Offset.getNode())->getSExtValue();
568 return Subtarget.getInstrInfo()->isValidAutoIncImm(VT, V);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000569}
570
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000571SDValue
572HexagonTargetLowering::LowerINLINEASM(SDValue Op, SelectionDAG &DAG) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000573 MachineFunction &MF = DAG.getMachineFunction();
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000574 auto &HMFI = *MF.getInfo<HexagonMachineFunctionInfo>();
575 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
576 unsigned LR = HRI.getRARegister();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000577
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000578 if (Op.getOpcode() != ISD::INLINEASM || HMFI.hasClobberLR())
579 return Op;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000580
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000581 unsigned NumOps = Op.getNumOperands();
582 if (Op.getOperand(NumOps-1).getValueType() == MVT::Glue)
583 --NumOps; // Ignore the flag operand.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000584
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000585 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
586 unsigned Flags = cast<ConstantSDNode>(Op.getOperand(i))->getZExtValue();
587 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
588 ++i; // Skip the ID value.
589
590 switch (InlineAsm::getKind(Flags)) {
591 default:
592 llvm_unreachable("Bad flags!");
593 case InlineAsm::Kind_RegUse:
594 case InlineAsm::Kind_Imm:
595 case InlineAsm::Kind_Mem:
596 i += NumVals;
597 break;
598 case InlineAsm::Kind_Clobber:
599 case InlineAsm::Kind_RegDef:
600 case InlineAsm::Kind_RegDefEarlyClobber: {
601 for (; NumVals; --NumVals, ++i) {
602 unsigned Reg = cast<RegisterSDNode>(Op.getOperand(i))->getReg();
603 if (Reg != LR)
604 continue;
605 HMFI.setHasClobberLR(true);
606 return Op;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000607 }
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000608 break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000609 }
610 }
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000611 }
612
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000613 return Op;
614}
615
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +0000616// Need to transform ISD::PREFETCH into something that doesn't inherit
617// all of the properties of ISD::PREFETCH, specifically SDNPMayLoad and
618// SDNPMayStore.
619SDValue HexagonTargetLowering::LowerPREFETCH(SDValue Op,
620 SelectionDAG &DAG) const {
621 SDValue Chain = Op.getOperand(0);
622 SDValue Addr = Op.getOperand(1);
623 // Lower it to DCFETCH($reg, #0). A "pat" will try to merge the offset in,
624 // if the "reg" is fed by an "add".
625 SDLoc DL(Op);
626 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
627 return DAG.getNode(HexagonISD::DCFETCH, DL, MVT::Other, Chain, Addr, Zero);
628}
629
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +0000630// Custom-handle ISD::READCYCLECOUNTER because the target-independent SDNode
631// is marked as having side-effects, while the register read on Hexagon does
632// not have any. TableGen refuses to accept the direct pattern from that node
633// to the A4_tfrcpp.
634SDValue HexagonTargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
635 SelectionDAG &DAG) const {
636 SDValue Chain = Op.getOperand(0);
637 SDLoc dl(Op);
638 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other);
639 return DAG.getNode(HexagonISD::READCYCLE, dl, VTs, Chain);
640}
641
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +0000642SDValue HexagonTargetLowering::LowerINTRINSIC_VOID(SDValue Op,
643 SelectionDAG &DAG) const {
644 SDValue Chain = Op.getOperand(0);
645 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
646 // Lower the hexagon_prefetch builtin to DCFETCH, as above.
647 if (IntNo == Intrinsic::hexagon_prefetch) {
648 SDValue Addr = Op.getOperand(2);
649 SDLoc DL(Op);
650 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
651 return DAG.getNode(HexagonISD::DCFETCH, DL, MVT::Other, Chain, Addr, Zero);
652 }
653 return SDValue();
654}
655
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000656SDValue
657HexagonTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
658 SelectionDAG &DAG) const {
659 SDValue Chain = Op.getOperand(0);
660 SDValue Size = Op.getOperand(1);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000661 SDValue Align = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000662 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000663
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000664 ConstantSDNode *AlignConst = dyn_cast<ConstantSDNode>(Align);
665 assert(AlignConst && "Non-constant Align in LowerDYNAMIC_STACKALLOC");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000666
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000667 unsigned A = AlignConst->getSExtValue();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000668 auto &HFI = *Subtarget.getFrameLowering();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000669 // "Zero" means natural stack alignment.
670 if (A == 0)
671 A = HFI.getStackAlignment();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000672
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000673 LLVM_DEBUG({
Reid Kleckner40d72302016-10-20 00:22:23 +0000674 dbgs () << __func__ << " Align: " << A << " Size: ";
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000675 Size.getNode()->dump(&DAG);
676 dbgs() << "\n";
677 });
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000678
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000679 SDValue AC = DAG.getConstant(A, dl, MVT::i32);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000680 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other);
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +0000681 SDValue AA = DAG.getNode(HexagonISD::ALLOCA, dl, VTs, Chain, Size, AC);
Nirav Davebfdb4832016-06-23 17:52:57 +0000682
683 DAG.ReplaceAllUsesOfValueWith(Op, AA);
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +0000684 return AA;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000685}
686
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000687SDValue HexagonTargetLowering::LowerFormalArguments(
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000688 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000689 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
690 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000691 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +0000692 MachineFrameInfo &MFI = MF.getFrameInfo();
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000693 MachineRegisterInfo &MRI = MF.getRegInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000694
695 // Assign locations to all of the incoming arguments.
696 SmallVector<CCValAssign, 16> ArgLocs;
Krzysztof Parzyszek18e0d2a2018-02-15 15:47:53 +0000697 HexagonCCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext(),
Krzysztof Parzyszeke0d7de72018-02-15 17:20:07 +0000698 MF.getFunction().getFunctionType()->getNumParams());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000699
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000700 if (Subtarget.useHVXOps())
701 CCInfo.AnalyzeFormalArguments(Ins, CC_Hexagon_HVX);
702 else
703 CCInfo.AnalyzeFormalArguments(Ins, CC_Hexagon);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000704
705 // For LLVM, in the case when returning a struct by value (>8byte),
706 // the first argument is a pointer that points to the location on caller's
707 // stack where the return value will be stored. For Hexagon, the location on
708 // caller's stack is passed only when the struct size is smaller than (and
709 // equal to) 8 bytes. If not, no address will be passed into callee and
710 // callee return the result direclty through R0/R1.
711
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000712 auto &HMFI = *MF.getInfo<HexagonMachineFunctionInfo>();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000713
714 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
715 CCValAssign &VA = ArgLocs[i];
716 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000717 bool ByVal = Flags.isByVal();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000718
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000719 // Arguments passed in registers:
720 // 1. 32- and 64-bit values and HVX vectors are passed directly,
721 // 2. Large structs are passed via an address, and the address is
722 // passed in a register.
723 if (VA.isRegLoc() && ByVal && Flags.getByValSize() <= 8)
724 llvm_unreachable("ByValSize must be bigger than 8 bytes");
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000725
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000726 bool InReg = VA.isRegLoc() &&
727 (!ByVal || (ByVal && Flags.getByValSize() > 8));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000728
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000729 if (InReg) {
730 MVT RegVT = VA.getLocVT();
731 if (VA.getLocInfo() == CCValAssign::BCvt)
732 RegVT = VA.getValVT();
733
734 const TargetRegisterClass *RC = getRegClassFor(RegVT);
735 unsigned VReg = MRI.createVirtualRegister(RC);
736 SDValue Copy = DAG.getCopyFromReg(Chain, dl, VReg, RegVT);
737
738 // Treat values of type MVT::i1 specially: they are passed in
739 // registers of type i32, but they need to remain as values of
740 // type i1 for consistency of the argument lowering.
741 if (VA.getValVT() == MVT::i1) {
742 assert(RegVT.getSizeInBits() <= 32);
743 SDValue T = DAG.getNode(ISD::AND, dl, RegVT,
744 Copy, DAG.getConstant(1, dl, RegVT));
745 Copy = DAG.getSetCC(dl, MVT::i1, T, DAG.getConstant(0, dl, RegVT),
746 ISD::SETNE);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000747 } else {
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000748#ifndef NDEBUG
749 unsigned RegSize = RegVT.getSizeInBits();
750 assert(RegSize == 32 || RegSize == 64 ||
751 Subtarget.isHVXVectorType(RegVT));
752#endif
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000753 }
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000754 InVals.push_back(Copy);
755 MRI.addLiveIn(VA.getLocReg(), VReg);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000756 } else {
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000757 assert(VA.isMemLoc() && "Argument should be passed in memory");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000758
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000759 // If it's a byval parameter, then we need to compute the
760 // "real" size, not the size of the pointer.
761 unsigned ObjSize = Flags.isByVal()
762 ? Flags.getByValSize()
763 : VA.getLocVT().getStoreSizeInBits() / 8;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000764
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000765 // Create the frame index object for this incoming parameter.
766 int Offset = HEXAGON_LRFP_SIZE + VA.getLocMemOffset();
767 int FI = MFI.CreateFixedObject(ObjSize, Offset, true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000768 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
769
770 if (Flags.isByVal()) {
771 // If it's a pass-by-value aggregate, then do not dereference the stack
772 // location. Instead, we should generate a reference to the stack
773 // location.
774 InVals.push_back(FIN);
775 } else {
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000776 SDValue L = DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
777 MachinePointerInfo::getFixedStack(MF, FI, 0));
778 InVals.push_back(L);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000779 }
780 }
781 }
782
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000783
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000784 if (IsVarArg) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000785 // This will point to the next argument passed via stack.
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000786 int Offset = HEXAGON_LRFP_SIZE + CCInfo.getNextStackOffset();
787 int FI = MFI.CreateFixedObject(Hexagon_PointerSize, Offset, true);
788 HMFI.setVarArgsFrameIndex(FI);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000789 }
790
791 return Chain;
792}
793
794SDValue
795HexagonTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
796 // VASTART stores the address of the VarArgsFrameIndex slot into the
797 // memory location argument.
798 MachineFunction &MF = DAG.getMachineFunction();
799 HexagonMachineFunctionInfo *QFI = MF.getInfo<HexagonMachineFunctionInfo>();
800 SDValue Addr = DAG.getFrameIndex(QFI->getVarArgsFrameIndex(), MVT::i32);
801 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Justin Lebar9c375812016-07-15 18:27:10 +0000802 return DAG.getStore(Op.getOperand(0), SDLoc(Op), Addr, Op.getOperand(1),
803 MachinePointerInfo(SV));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000804}
805
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000806SDValue HexagonTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek23bcf062018-04-19 14:24:31 +0000807 const SDLoc &dl(Op);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000808 SDValue LHS = Op.getOperand(0);
809 SDValue RHS = Op.getOperand(1);
Krzysztof Parzyszek23bcf062018-04-19 14:24:31 +0000810 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
811 MVT ResTy = ty(Op);
812 MVT OpTy = ty(LHS);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000813
Krzysztof Parzyszek23bcf062018-04-19 14:24:31 +0000814 if (OpTy == MVT::v2i16 || OpTy == MVT::v4i8) {
815 MVT ElemTy = OpTy.getVectorElementType();
816 assert(ElemTy.isScalarInteger());
817 MVT WideTy = MVT::getVectorVT(MVT::getIntegerVT(2*ElemTy.getSizeInBits()),
818 OpTy.getVectorNumElements());
819 return DAG.getSetCC(dl, ResTy,
820 DAG.getSExtOrTrunc(LHS, SDLoc(LHS), WideTy),
821 DAG.getSExtOrTrunc(RHS, SDLoc(RHS), WideTy), CC);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000822 }
823
824 // Treat all other vector types as legal.
Krzysztof Parzyszek23bcf062018-04-19 14:24:31 +0000825 if (ResTy.isVector())
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000826 return Op;
827
Krzysztof Parzyszek23bcf062018-04-19 14:24:31 +0000828 // Comparisons of short integers should use sign-extend, not zero-extend,
829 // since we can represent small negative values in the compare instructions.
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000830 // The LLVM default is to use zero-extend arbitrarily in these cases.
Krzysztof Parzyszek23bcf062018-04-19 14:24:31 +0000831 auto isSExtFree = [this](SDValue N) {
832 switch (N.getOpcode()) {
833 case ISD::TRUNCATE: {
834 // A sign-extend of a truncate of a sign-extend is free.
835 SDValue Op = N.getOperand(0);
836 if (Op.getOpcode() != ISD::AssertSext)
837 return false;
838 MVT OrigTy = cast<VTSDNode>(Op.getOperand(1))->getVT().getSimpleVT();
839 unsigned ThisBW = ty(N).getSizeInBits();
840 unsigned OrigBW = OrigTy.getSizeInBits();
841 // The type that was sign-extended to get the AssertSext must be
842 // narrower than the type of N (so that N has still the same value
843 // as the original).
844 return ThisBW >= OrigBW;
845 }
846 case ISD::LOAD:
847 // We have sign-extended loads.
848 return true;
849 }
850 return false;
851 };
852
853 if (OpTy == MVT::i8 || OpTy == MVT::i16) {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000854 ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS);
Krzysztof Parzyszek23bcf062018-04-19 14:24:31 +0000855 bool IsNegative = C && C->getAPIntValue().isNegative();
856 if (IsNegative || isSExtFree(LHS) || isSExtFree(RHS))
857 return DAG.getSetCC(dl, ResTy,
858 DAG.getSExtOrTrunc(LHS, SDLoc(LHS), MVT::i32),
859 DAG.getSExtOrTrunc(RHS, SDLoc(RHS), MVT::i32), CC);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000860 }
Krzysztof Parzyszek23bcf062018-04-19 14:24:31 +0000861
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000862 return SDValue();
863}
864
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000865SDValue
866HexagonTargetLowering::LowerVSELECT(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000867 SDValue PredOp = Op.getOperand(0);
868 SDValue Op1 = Op.getOperand(1), Op2 = Op.getOperand(2);
869 EVT OpVT = Op1.getValueType();
870 SDLoc DL(Op);
871
872 if (OpVT == MVT::v2i16) {
873 SDValue X1 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i32, Op1);
874 SDValue X2 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i32, Op2);
875 SDValue SL = DAG.getNode(ISD::VSELECT, DL, MVT::v2i32, PredOp, X1, X2);
876 SDValue TR = DAG.getNode(ISD::TRUNCATE, DL, MVT::v2i16, SL);
877 return TR;
878 }
879
880 return SDValue();
881}
882
Krzysztof Parzyszek91ff5c62017-08-01 13:12:53 +0000883static Constant *convert_i1_to_i8(const Constant *ConstVal) {
884 SmallVector<Constant *, 128> NewConst;
885 const ConstantVector *CV = dyn_cast<ConstantVector>(ConstVal);
886 if (!CV)
887 return nullptr;
888
889 LLVMContext &Ctx = ConstVal->getContext();
890 IRBuilder<> IRB(Ctx);
891 unsigned NumVectorElements = CV->getNumOperands();
892 assert(isPowerOf2_32(NumVectorElements) &&
893 "conversion only supported for pow2 VectorSize!");
894
895 for (unsigned i = 0; i < NumVectorElements / 8; ++i) {
896 uint8_t x = 0;
897 for (unsigned j = 0; j < 8; ++j) {
898 uint8_t y = CV->getOperand(i * 8 + j)->getUniqueInteger().getZExtValue();
899 x |= y << (7 - j);
900 }
901 assert((x == 0 || x == 255) && "Either all 0's or all 1's expected!");
902 NewConst.push_back(IRB.getInt8(x));
903 }
904 return ConstantVector::get(NewConst);
905}
906
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000907SDValue
Sirish Pande69295b82012-05-10 20:20:25 +0000908HexagonTargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
909 EVT ValTy = Op.getValueType();
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000910 ConstantPoolSDNode *CPN = cast<ConstantPoolSDNode>(Op);
Krzysztof Parzyszek91ff5c62017-08-01 13:12:53 +0000911 Constant *CVal = nullptr;
912 bool isVTi1Type = false;
913 if (const Constant *ConstVal = dyn_cast<Constant>(CPN->getConstVal())) {
914 Type *CValTy = ConstVal->getType();
915 if (CValTy->isVectorTy() &&
916 CValTy->getVectorElementType()->isIntegerTy(1)) {
917 CVal = convert_i1_to_i8(ConstVal);
918 isVTi1Type = (CVal != nullptr);
919 }
920 }
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000921 unsigned Align = CPN->getAlignment();
Rafael Espindola405e25a2016-06-26 22:24:01 +0000922 bool IsPositionIndependent = isPositionIndependent();
923 unsigned char TF = IsPositionIndependent ? HexagonII::MO_PCREL : 0;
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000924
Ron Lieberman822ee882016-08-13 23:41:11 +0000925 unsigned Offset = 0;
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000926 SDValue T;
927 if (CPN->isMachineConstantPoolEntry())
Ron Lieberman822ee882016-08-13 23:41:11 +0000928 T = DAG.getTargetConstantPool(CPN->getMachineCPVal(), ValTy, Align, Offset,
929 TF);
Krzysztof Parzyszek91ff5c62017-08-01 13:12:53 +0000930 else if (isVTi1Type)
931 T = DAG.getTargetConstantPool(CVal, ValTy, Align, Offset, TF);
Sirish Pande69295b82012-05-10 20:20:25 +0000932 else
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +0000933 T = DAG.getTargetConstantPool(CPN->getConstVal(), ValTy, Align, Offset, TF);
Ron Lieberman822ee882016-08-13 23:41:11 +0000934
935 assert(cast<ConstantPoolSDNode>(T)->getTargetFlags() == TF &&
936 "Inconsistent target flag encountered");
937
Rafael Espindola405e25a2016-06-26 22:24:01 +0000938 if (IsPositionIndependent)
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000939 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), ValTy, T);
940 return DAG.getNode(HexagonISD::CP, SDLoc(Op), ValTy, T);
941}
942
943SDValue
944HexagonTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
945 EVT VT = Op.getValueType();
946 int Idx = cast<JumpTableSDNode>(Op)->getIndex();
Rafael Espindola405e25a2016-06-26 22:24:01 +0000947 if (isPositionIndependent()) {
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000948 SDValue T = DAG.getTargetJumpTable(Idx, VT, HexagonII::MO_PCREL);
949 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), VT, T);
950 }
951
952 SDValue T = DAG.getTargetJumpTable(Idx, VT);
953 return DAG.getNode(HexagonISD::JT, SDLoc(Op), VT, T);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000954}
955
956SDValue
957HexagonTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000958 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000959 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +0000960 MachineFrameInfo &MFI = MF.getFrameInfo();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000961 MFI.setReturnAddressIsTaken(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000962
Bill Wendling908bf812014-01-06 00:43:20 +0000963 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +0000964 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +0000965
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000966 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000967 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000968 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
969 if (Depth) {
970 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000971 SDValue Offset = DAG.getConstant(4, dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000972 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
973 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Justin Lebar9c375812016-07-15 18:27:10 +0000974 MachinePointerInfo());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000975 }
976
977 // Return LR, which contains the return address. Mark it an implicit live-in.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000978 unsigned Reg = MF.addLiveIn(HRI.getRARegister(), getRegClassFor(MVT::i32));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000979 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
980}
981
982SDValue
983HexagonTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000984 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Matthias Braun941a7052016-07-28 18:40:00 +0000985 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000986 MFI.setFrameAddressIsTaken(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000987
988 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000989 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000990 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
991 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000992 HRI.getFrameRegister(), VT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000993 while (Depth--)
994 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
Justin Lebar9c375812016-07-15 18:27:10 +0000995 MachinePointerInfo());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000996 return FrameAddr;
997}
998
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000999SDValue
1000HexagonTargetLowering::LowerATOMIC_FENCE(SDValue Op, SelectionDAG& DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001001 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001002 return DAG.getNode(HexagonISD::BARRIER, dl, MVT::Other, Op.getOperand(0));
1003}
1004
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001005SDValue
1006HexagonTargetLowering::LowerGLOBALADDRESS(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001007 SDLoc dl(Op);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001008 auto *GAN = cast<GlobalAddressSDNode>(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00001009 auto PtrVT = getPointerTy(DAG.getDataLayout());
Krzysztof Parzyszek96a28412018-01-30 18:10:27 +00001010 auto *GV = GAN->getGlobal();
1011 int64_t Offset = GAN->getOffset();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001012
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001013 auto &HLOF = *HTM.getObjFileLowering();
1014 Reloc::Model RM = HTM.getRelocationModel();
1015
1016 if (RM == Reloc::Static) {
1017 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, Offset);
Peter Collingbourne67335642016-10-24 19:23:39 +00001018 const GlobalObject *GO = GV->getBaseObject();
Krzysztof Parzyszek44e180b2018-05-14 21:01:56 +00001019 if (GO && Subtarget.useSmallData() && HLOF.isGlobalInSmallSection(GO, HTM))
Krzysztof Parzyszek96a28412018-01-30 18:10:27 +00001020 return DAG.getNode(HexagonISD::CONST32_GP, dl, PtrVT, GA);
1021 return DAG.getNode(HexagonISD::CONST32, dl, PtrVT, GA);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001022 }
1023
Krzysztof Parzyszek96a28412018-01-30 18:10:27 +00001024 bool UsePCRel = getTargetMachine().shouldAssumeDSOLocal(*GV->getParent(), GV);
1025 if (UsePCRel) {
1026 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, Offset,
1027 HexagonII::MO_PCREL);
1028 return DAG.getNode(HexagonISD::AT_PCREL, dl, PtrVT, GA);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001029 }
1030
Krzysztof Parzyszek96a28412018-01-30 18:10:27 +00001031 // Use GOT index.
1032 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
1033 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, HexagonII::MO_GOT);
1034 SDValue Off = DAG.getConstant(Offset, dl, MVT::i32);
1035 return DAG.getNode(HexagonISD::AT_GOT, dl, PtrVT, GOT, GA, Off);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001036}
1037
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001038// Specifies that for loads and stores VT can be promoted to PromotedLdStVT.
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001039SDValue
1040HexagonTargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
1041 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001042 SDLoc dl(Op);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001043 EVT PtrVT = getPointerTy(DAG.getDataLayout());
1044
1045 Reloc::Model RM = HTM.getRelocationModel();
1046 if (RM == Reloc::Static) {
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001047 SDValue A = DAG.getTargetBlockAddress(BA, PtrVT);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001048 return DAG.getNode(HexagonISD::CONST32_GP, dl, PtrVT, A);
1049 }
1050
1051 SDValue A = DAG.getTargetBlockAddress(BA, PtrVT, 0, HexagonII::MO_PCREL);
1052 return DAG.getNode(HexagonISD::AT_PCREL, dl, PtrVT, A);
1053}
1054
1055SDValue
1056HexagonTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op, SelectionDAG &DAG)
1057 const {
1058 EVT PtrVT = getPointerTy(DAG.getDataLayout());
1059 SDValue GOTSym = DAG.getTargetExternalSymbol(HEXAGON_GOT_SYM_NAME, PtrVT,
1060 HexagonII::MO_PCREL);
1061 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), PtrVT, GOTSym);
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001062}
1063
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001064SDValue
1065HexagonTargetLowering::GetDynamicTLSAddr(SelectionDAG &DAG, SDValue Chain,
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001066 GlobalAddressSDNode *GA, SDValue Glue, EVT PtrVT, unsigned ReturnReg,
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001067 unsigned char OperandFlags) const {
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001068 MachineFunction &MF = DAG.getMachineFunction();
1069 MachineFrameInfo &MFI = MF.getFrameInfo();
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001070 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1071 SDLoc dl(GA);
1072 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
1073 GA->getValueType(0),
1074 GA->getOffset(),
1075 OperandFlags);
1076 // Create Operands for the call.The Operands should have the following:
1077 // 1. Chain SDValue
1078 // 2. Callee which in this case is the Global address value.
1079 // 3. Registers live into the call.In this case its R0, as we
1080 // have just one argument to be passed.
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001081 // 4. Glue.
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001082 // Note: The order is important.
1083
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001084 const auto &HRI = *Subtarget.getRegisterInfo();
1085 const uint32_t *Mask = HRI.getCallPreservedMask(MF, CallingConv::C);
1086 assert(Mask && "Missing call preserved mask for calling convention");
1087 SDValue Ops[] = { Chain, TGA, DAG.getRegister(Hexagon::R0, PtrVT),
1088 DAG.getRegisterMask(Mask), Glue };
1089 Chain = DAG.getNode(HexagonISD::CALL, dl, NodeTys, Ops);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001090
1091 // Inform MFI that function has calls.
Matthias Braun941a7052016-07-28 18:40:00 +00001092 MFI.setAdjustsStack(true);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001093
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001094 Glue = Chain.getValue(1);
1095 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Glue);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001096}
1097
1098//
1099// Lower using the intial executable model for TLS addresses
1100//
1101SDValue
1102HexagonTargetLowering::LowerToTLSInitialExecModel(GlobalAddressSDNode *GA,
1103 SelectionDAG &DAG) const {
1104 SDLoc dl(GA);
1105 int64_t Offset = GA->getOffset();
1106 auto PtrVT = getPointerTy(DAG.getDataLayout());
1107
1108 // Get the thread pointer.
1109 SDValue TP = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Hexagon::UGP, PtrVT);
1110
Rafael Espindola405e25a2016-06-26 22:24:01 +00001111 bool IsPositionIndependent = isPositionIndependent();
1112 unsigned char TF =
1113 IsPositionIndependent ? HexagonII::MO_IEGOT : HexagonII::MO_IE;
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001114
1115 // First generate the TLS symbol address
1116 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT,
1117 Offset, TF);
1118
1119 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1120
Rafael Espindola405e25a2016-06-26 22:24:01 +00001121 if (IsPositionIndependent) {
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001122 // Generate the GOT pointer in case of position independent code
1123 SDValue GOT = LowerGLOBAL_OFFSET_TABLE(Sym, DAG);
1124
1125 // Add the TLS Symbol address to GOT pointer.This gives
1126 // GOT relative relocation for the symbol.
1127 Sym = DAG.getNode(ISD::ADD, dl, PtrVT, GOT, Sym);
1128 }
1129
1130 // Load the offset value for TLS symbol.This offset is relative to
1131 // thread pointer.
Justin Lebar9c375812016-07-15 18:27:10 +00001132 SDValue LoadOffset =
1133 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Sym, MachinePointerInfo());
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001134
1135 // Address of the thread local variable is the add of thread
1136 // pointer and the offset of the variable.
1137 return DAG.getNode(ISD::ADD, dl, PtrVT, TP, LoadOffset);
1138}
1139
1140//
1141// Lower using the local executable model for TLS addresses
1142//
1143SDValue
1144HexagonTargetLowering::LowerToTLSLocalExecModel(GlobalAddressSDNode *GA,
1145 SelectionDAG &DAG) const {
1146 SDLoc dl(GA);
1147 int64_t Offset = GA->getOffset();
1148 auto PtrVT = getPointerTy(DAG.getDataLayout());
1149
1150 // Get the thread pointer.
1151 SDValue TP = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Hexagon::UGP, PtrVT);
1152 // Generate the TLS symbol address
1153 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT, Offset,
1154 HexagonII::MO_TPREL);
1155 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1156
1157 // Address of the thread local variable is the add of thread
1158 // pointer and the offset of the variable.
1159 return DAG.getNode(ISD::ADD, dl, PtrVT, TP, Sym);
1160}
1161
1162//
1163// Lower using the general dynamic model for TLS addresses
1164//
1165SDValue
1166HexagonTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
1167 SelectionDAG &DAG) const {
1168 SDLoc dl(GA);
1169 int64_t Offset = GA->getOffset();
1170 auto PtrVT = getPointerTy(DAG.getDataLayout());
1171
1172 // First generate the TLS symbol address
1173 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT, Offset,
1174 HexagonII::MO_GDGOT);
1175
1176 // Then, generate the GOT pointer
1177 SDValue GOT = LowerGLOBAL_OFFSET_TABLE(TGA, DAG);
1178
1179 // Add the TLS symbol and the GOT pointer
1180 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1181 SDValue Chain = DAG.getNode(ISD::ADD, dl, PtrVT, GOT, Sym);
1182
1183 // Copy over the argument to R0
1184 SDValue InFlag;
1185 Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, Hexagon::R0, Chain, InFlag);
1186 InFlag = Chain.getValue(1);
1187
Krzysztof Parzyszeka7503832017-05-02 18:15:33 +00001188 unsigned Flags =
1189 static_cast<const HexagonSubtarget &>(DAG.getSubtarget()).useLongCalls()
1190 ? HexagonII::MO_GDPLT | HexagonII::HMOTF_ConstExtended
1191 : HexagonII::MO_GDPLT;
1192
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001193 return GetDynamicTLSAddr(DAG, Chain, GA, InFlag, PtrVT,
Krzysztof Parzyszeka7503832017-05-02 18:15:33 +00001194 Hexagon::R0, Flags);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001195}
1196
1197//
1198// Lower TLS addresses.
1199//
1200// For now for dynamic models, we only support the general dynamic model.
1201//
1202SDValue
1203HexagonTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1204 SelectionDAG &DAG) const {
1205 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1206
1207 switch (HTM.getTLSModel(GA->getGlobal())) {
1208 case TLSModel::GeneralDynamic:
1209 case TLSModel::LocalDynamic:
1210 return LowerToTLSGeneralDynamicModel(GA, DAG);
1211 case TLSModel::InitialExec:
1212 return LowerToTLSInitialExecModel(GA, DAG);
1213 case TLSModel::LocalExec:
1214 return LowerToTLSLocalExecModel(GA, DAG);
1215 }
1216 llvm_unreachable("Bogus TLS model");
1217}
1218
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001219//===----------------------------------------------------------------------===//
1220// TargetLowering Implementation
1221//===----------------------------------------------------------------------===//
1222
Eric Christopherd737b762015-02-02 22:11:36 +00001223HexagonTargetLowering::HexagonTargetLowering(const TargetMachine &TM,
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001224 const HexagonSubtarget &ST)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001225 : TargetLowering(TM), HTM(static_cast<const HexagonTargetMachine&>(TM)),
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001226 Subtarget(ST) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001227 bool IsV4 = !Subtarget.hasV5TOps();
1228 auto &HRI = *Subtarget.getRegisterInfo();
Sirish Pande69295b82012-05-10 20:20:25 +00001229
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001230 setPrefLoopAlignment(4);
1231 setPrefFunctionAlignment(4);
1232 setMinFunctionAlignment(2);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001233 setStackPointerRegisterToSaveRestore(HRI.getStackRegister());
Krzysztof Parzyszekb3e50ac2018-01-05 20:41:50 +00001234 setBooleanContents(TargetLoweringBase::UndefinedBooleanContent);
1235 setBooleanVectorContents(TargetLoweringBase::UndefinedBooleanContent);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001236
Krzysztof Parzyszekf228c952016-06-22 16:07:10 +00001237 setMaxAtomicSizeInBitsSupported(64);
1238 setMinCmpXchgSizeInBits(32);
1239
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001240 if (EnableHexSDNodeSched)
1241 setSchedulingPreference(Sched::VLIW);
1242 else
1243 setSchedulingPreference(Sched::Source);
1244
1245 // Limits for inline expansion of memcpy/memmove
1246 MaxStoresPerMemcpy = MaxStoresPerMemcpyCL;
1247 MaxStoresPerMemcpyOptSize = MaxStoresPerMemcpyOptSizeCL;
1248 MaxStoresPerMemmove = MaxStoresPerMemmoveCL;
1249 MaxStoresPerMemmoveOptSize = MaxStoresPerMemmoveOptSizeCL;
1250 MaxStoresPerMemset = MaxStoresPerMemsetCL;
1251 MaxStoresPerMemsetOptSize = MaxStoresPerMemsetOptSizeCL;
1252
1253 //
1254 // Set up register classes.
1255 //
1256
1257 addRegisterClass(MVT::i1, &Hexagon::PredRegsRegClass);
1258 addRegisterClass(MVT::v2i1, &Hexagon::PredRegsRegClass); // bbbbaaaa
1259 addRegisterClass(MVT::v4i1, &Hexagon::PredRegsRegClass); // ddccbbaa
1260 addRegisterClass(MVT::v8i1, &Hexagon::PredRegsRegClass); // hgfedcba
1261 addRegisterClass(MVT::i32, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001262 addRegisterClass(MVT::v2i16, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00001263 addRegisterClass(MVT::v4i8, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001264 addRegisterClass(MVT::i64, &Hexagon::DoubleRegsRegClass);
1265 addRegisterClass(MVT::v8i8, &Hexagon::DoubleRegsRegClass);
1266 addRegisterClass(MVT::v4i16, &Hexagon::DoubleRegsRegClass);
1267 addRegisterClass(MVT::v2i32, &Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001268
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001269 if (Subtarget.hasV5TOps()) {
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001270 addRegisterClass(MVT::f32, &Hexagon::IntRegsRegClass);
1271 addRegisterClass(MVT::f64, &Hexagon::DoubleRegsRegClass);
1272 }
Sirish Pande69295b82012-05-10 20:20:25 +00001273
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001274 //
1275 // Handling of scalar operations.
1276 //
1277 // All operations default to "legal", except:
1278 // - indexed loads and stores (pre-/post-incremented),
1279 // - ANY_EXTEND_VECTOR_INREG, ATOMIC_CMP_SWAP_WITH_SUCCESS, CONCAT_VECTORS,
1280 // ConstantFP, DEBUGTRAP, FCEIL, FCOPYSIGN, FEXP, FEXP2, FFLOOR, FGETSIGN,
1281 // FLOG, FLOG2, FLOG10, FMAXNUM, FMINNUM, FNEARBYINT, FRINT, FROUND, TRAP,
1282 // FTRUNC, PREFETCH, SIGN_EXTEND_VECTOR_INREG, ZERO_EXTEND_VECTOR_INREG,
1283 // which default to "expand" for at least one type.
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001284
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001285 // Misc operations.
1286 setOperationAction(ISD::ConstantFP, MVT::f32, Legal); // Default: expand
1287 setOperationAction(ISD::ConstantFP, MVT::f64, Legal); // Default: expand
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001288
1289 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001290 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001291 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001292 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
1293 setOperationAction(ISD::INLINEASM, MVT::Other, Custom);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00001294 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +00001295 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00001296 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001297 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001298 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001299 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001300 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001301
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001302 // Custom legalize GlobalAddress nodes into CONST32.
1303 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001304 setOperationAction(ISD::GlobalAddress, MVT::i8, Custom);
1305 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001306
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001307 // Hexagon needs to optimize cases with negative constants.
Krzysztof Parzyszek23bcf062018-04-19 14:24:31 +00001308 setOperationAction(ISD::SETCC, MVT::i8, Custom);
1309 setOperationAction(ISD::SETCC, MVT::i16, Custom);
1310 setOperationAction(ISD::SETCC, MVT::v4i8, Custom);
1311 setOperationAction(ISD::SETCC, MVT::v2i16, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001312
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001313 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
1314 setOperationAction(ISD::VASTART, MVT::Other, Custom);
1315 setOperationAction(ISD::VAEND, MVT::Other, Expand);
1316 setOperationAction(ISD::VAARG, MVT::Other, Expand);
Krzysztof Parzyszekf6088122018-03-02 18:35:57 +00001317 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001318
1319 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
1320 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
1321 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
1322
1323 if (EmitJumpTables)
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001324 setMinimumJumpTableEntries(MinimumJumpTables);
Krzysztof Parzyszeka61f7da2016-01-13 21:43:13 +00001325 else
Eugene Zelenko58655bb2016-12-17 01:09:05 +00001326 setMinimumJumpTableEntries(std::numeric_limits<int>::max());
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001327 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001328
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001329 // Hexagon has instructions for add/sub with carry. The problem with
1330 // modeling these instructions is that they produce 2 results: Rdd and Px.
1331 // To model the update of Px, we will have to use Defs[p0..p3] which will
1332 // cause any predicate live range to spill. So, we pretend we dont't have
1333 // these instructions.
1334 setOperationAction(ISD::ADDE, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001335 setOperationAction(ISD::ADDE, MVT::i16, Expand);
1336 setOperationAction(ISD::ADDE, MVT::i32, Expand);
1337 setOperationAction(ISD::ADDE, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001338 setOperationAction(ISD::SUBE, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001339 setOperationAction(ISD::SUBE, MVT::i16, Expand);
1340 setOperationAction(ISD::SUBE, MVT::i32, Expand);
1341 setOperationAction(ISD::SUBE, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001342 setOperationAction(ISD::ADDC, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001343 setOperationAction(ISD::ADDC, MVT::i16, Expand);
1344 setOperationAction(ISD::ADDC, MVT::i32, Expand);
1345 setOperationAction(ISD::ADDC, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001346 setOperationAction(ISD::SUBC, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001347 setOperationAction(ISD::SUBC, MVT::i16, Expand);
1348 setOperationAction(ISD::SUBC, MVT::i32, Expand);
1349 setOperationAction(ISD::SUBC, MVT::i64, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001350
Krzysztof Parzyszek2c4487d2015-04-13 20:37:01 +00001351 // Only add and sub that detect overflow are the saturating ones.
1352 for (MVT VT : MVT::integer_valuetypes()) {
1353 setOperationAction(ISD::UADDO, VT, Expand);
1354 setOperationAction(ISD::SADDO, VT, Expand);
1355 setOperationAction(ISD::USUBO, VT, Expand);
1356 setOperationAction(ISD::SSUBO, VT, Expand);
1357 }
1358
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001359 setOperationAction(ISD::CTLZ, MVT::i8, Promote);
1360 setOperationAction(ISD::CTLZ, MVT::i16, Promote);
1361 setOperationAction(ISD::CTTZ, MVT::i8, Promote);
1362 setOperationAction(ISD::CTTZ, MVT::i16, Promote);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001363
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001364 // In V5, popcount can count # of 1s in i64 but returns i32.
1365 // On V4 it will be expanded (set later).
1366 setOperationAction(ISD::CTPOP, MVT::i8, Promote);
1367 setOperationAction(ISD::CTPOP, MVT::i16, Promote);
1368 setOperationAction(ISD::CTPOP, MVT::i32, Promote);
Krzysztof Parzyszekaf5ff652017-02-23 15:02:09 +00001369 setOperationAction(ISD::CTPOP, MVT::i64, Legal);
1370
1371 setOperationAction(ISD::BITREVERSE, MVT::i32, Legal);
1372 setOperationAction(ISD::BITREVERSE, MVT::i64, Legal);
1373 setOperationAction(ISD::BSWAP, MVT::i32, Legal);
1374 setOperationAction(ISD::BSWAP, MVT::i64, Legal);
Krzysztof Parzyszekef580172017-05-30 17:47:51 +00001375 setOperationAction(ISD::MUL, MVT::i64, Legal);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001376
Benjamin Kramer62460692015-04-25 14:46:53 +00001377 for (unsigned IntExpOp :
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001378 { ISD::SDIV, ISD::UDIV, ISD::SREM, ISD::UREM,
1379 ISD::SDIVREM, ISD::UDIVREM, ISD::ROTL, ISD::ROTR,
Krzysztof Parzyszekaf5ff652017-02-23 15:02:09 +00001380 ISD::SHL_PARTS, ISD::SRA_PARTS, ISD::SRL_PARTS,
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001381 ISD::SMUL_LOHI, ISD::UMUL_LOHI }) {
Benjamin Kramer62460692015-04-25 14:46:53 +00001382 setOperationAction(IntExpOp, MVT::i32, Expand);
1383 setOperationAction(IntExpOp, MVT::i64, Expand);
1384 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001385
Benjamin Kramer62460692015-04-25 14:46:53 +00001386 for (unsigned FPExpOp :
1387 {ISD::FDIV, ISD::FREM, ISD::FSQRT, ISD::FSIN, ISD::FCOS, ISD::FSINCOS,
1388 ISD::FPOW, ISD::FCOPYSIGN}) {
1389 setOperationAction(FPExpOp, MVT::f32, Expand);
1390 setOperationAction(FPExpOp, MVT::f64, Expand);
1391 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001392
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001393 // No extending loads from i32.
1394 for (MVT VT : MVT::integer_valuetypes()) {
1395 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand);
1396 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand);
1397 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand);
1398 }
1399 // Turn FP truncstore into trunc + store.
1400 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Michael Kuperstein2bc3d4d2016-08-18 20:08:15 +00001401 // Turn FP extload into load/fpextend.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001402 for (MVT VT : MVT::fp_valuetypes())
1403 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001404
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001405 // Expand BR_CC and SELECT_CC for all integer and fp types.
1406 for (MVT VT : MVT::integer_valuetypes()) {
1407 setOperationAction(ISD::BR_CC, VT, Expand);
1408 setOperationAction(ISD::SELECT_CC, VT, Expand);
1409 }
1410 for (MVT VT : MVT::fp_valuetypes()) {
1411 setOperationAction(ISD::BR_CC, VT, Expand);
1412 setOperationAction(ISD::SELECT_CC, VT, Expand);
1413 }
1414 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001415
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001416 //
1417 // Handling of vector operations.
1418 //
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001419
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001420 promoteLdStType(MVT::v4i8, MVT::i32);
1421 promoteLdStType(MVT::v2i16, MVT::i32);
1422 promoteLdStType(MVT::v8i8, MVT::i64);
Krzysztof Parzyszek5eef92e2017-07-17 15:45:45 +00001423 promoteLdStType(MVT::v4i16, MVT::i64);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001424 promoteLdStType(MVT::v2i32, MVT::i64);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001425
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001426 // Set the action for vector operations to "expand", then override it with
1427 // either "custom" or "legal" for specific cases.
Craig Topper26260942015-10-18 05:15:34 +00001428 static const unsigned VectExpOps[] = {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001429 // Integer arithmetic:
1430 ISD::ADD, ISD::SUB, ISD::MUL, ISD::SDIV, ISD::UDIV,
1431 ISD::SREM, ISD::UREM, ISD::SDIVREM, ISD::UDIVREM, ISD::ADDC,
1432 ISD::SUBC, ISD::SADDO, ISD::UADDO, ISD::SSUBO, ISD::USUBO,
1433 ISD::SMUL_LOHI, ISD::UMUL_LOHI,
1434 // Logical/bit:
1435 ISD::AND, ISD::OR, ISD::XOR, ISD::ROTL, ISD::ROTR,
Craig Topper33772c52016-04-28 03:34:31 +00001436 ISD::CTPOP, ISD::CTLZ, ISD::CTTZ,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001437 // Floating point arithmetic/math functions:
1438 ISD::FADD, ISD::FSUB, ISD::FMUL, ISD::FMA, ISD::FDIV,
1439 ISD::FREM, ISD::FNEG, ISD::FABS, ISD::FSQRT, ISD::FSIN,
Craig Topperf6d4dc52017-05-30 15:27:55 +00001440 ISD::FCOS, ISD::FPOW, ISD::FLOG, ISD::FLOG2,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001441 ISD::FLOG10, ISD::FEXP, ISD::FEXP2, ISD::FCEIL, ISD::FTRUNC,
1442 ISD::FRINT, ISD::FNEARBYINT, ISD::FROUND, ISD::FFLOOR,
1443 ISD::FMINNUM, ISD::FMAXNUM, ISD::FSINCOS,
1444 // Misc:
Krzysztof Parzyszek046da742016-10-27 14:30:16 +00001445 ISD::BR_CC, ISD::SELECT_CC, ISD::ConstantPool,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001446 // Vector:
1447 ISD::BUILD_VECTOR, ISD::SCALAR_TO_VECTOR,
1448 ISD::EXTRACT_VECTOR_ELT, ISD::INSERT_VECTOR_ELT,
1449 ISD::EXTRACT_SUBVECTOR, ISD::INSERT_SUBVECTOR,
1450 ISD::CONCAT_VECTORS, ISD::VECTOR_SHUFFLE
1451 };
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001452
1453 for (MVT VT : MVT::vector_valuetypes()) {
Benjamin Kramer62460692015-04-25 14:46:53 +00001454 for (unsigned VectExpOp : VectExpOps)
1455 setOperationAction(VectExpOp, VT, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001456
Krzysztof Parzyszeka696b1b2016-09-08 17:42:14 +00001457 // Expand all extending loads and truncating stores:
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001458 for (MVT TargetVT : MVT::vector_valuetypes()) {
Krzysztof Parzyszeka696b1b2016-09-08 17:42:14 +00001459 if (TargetVT == VT)
1460 continue;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001461 setLoadExtAction(ISD::EXTLOAD, TargetVT, VT, Expand);
Krzysztof Parzyszeka696b1b2016-09-08 17:42:14 +00001462 setLoadExtAction(ISD::ZEXTLOAD, TargetVT, VT, Expand);
1463 setLoadExtAction(ISD::SEXTLOAD, TargetVT, VT, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001464 setTruncStoreAction(VT, TargetVT, Expand);
1465 }
1466
Krzysztof Parzyszek046da742016-10-27 14:30:16 +00001467 // Normalize all inputs to SELECT to be vectors of i32.
1468 if (VT.getVectorElementType() != MVT::i32) {
1469 MVT VT32 = MVT::getVectorVT(MVT::i32, VT.getSizeInBits()/32);
1470 setOperationAction(ISD::SELECT, VT, Promote);
1471 AddPromotedToType(ISD::SELECT, VT, VT32);
1472 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001473 setOperationAction(ISD::SRA, VT, Custom);
1474 setOperationAction(ISD::SHL, VT, Custom);
1475 setOperationAction(ISD::SRL, VT, Custom);
1476 }
1477
Krzysztof Parzyszek64e5d7d2017-10-20 19:33:12 +00001478 // Extending loads from (native) vectors of i8 into (native) vectors of i16
1479 // are legal.
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001480 setLoadExtAction(ISD::EXTLOAD, MVT::v2i16, MVT::v2i8, Legal);
Krzysztof Parzyszek64e5d7d2017-10-20 19:33:12 +00001481 setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i16, MVT::v2i8, Legal);
1482 setLoadExtAction(ISD::SEXTLOAD, MVT::v2i16, MVT::v2i8, Legal);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001483 setLoadExtAction(ISD::EXTLOAD, MVT::v4i16, MVT::v4i8, Legal);
Krzysztof Parzyszek64e5d7d2017-10-20 19:33:12 +00001484 setLoadExtAction(ISD::ZEXTLOAD, MVT::v4i16, MVT::v4i8, Legal);
1485 setLoadExtAction(ISD::SEXTLOAD, MVT::v4i16, MVT::v4i8, Legal);
1486
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001487 // Types natively supported:
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001488 for (MVT NativeVT : {MVT::v8i1, MVT::v4i1, MVT::v2i1, MVT::v4i8,
1489 MVT::v8i8, MVT::v2i16, MVT::v4i16, MVT::v2i32}) {
Benjamin Kramer62460692015-04-25 14:46:53 +00001490 setOperationAction(ISD::BUILD_VECTOR, NativeVT, Custom);
1491 setOperationAction(ISD::EXTRACT_VECTOR_ELT, NativeVT, Custom);
1492 setOperationAction(ISD::INSERT_VECTOR_ELT, NativeVT, Custom);
1493 setOperationAction(ISD::EXTRACT_SUBVECTOR, NativeVT, Custom);
1494 setOperationAction(ISD::INSERT_SUBVECTOR, NativeVT, Custom);
1495 setOperationAction(ISD::CONCAT_VECTORS, NativeVT, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001496
Benjamin Kramer62460692015-04-25 14:46:53 +00001497 setOperationAction(ISD::ADD, NativeVT, Legal);
1498 setOperationAction(ISD::SUB, NativeVT, Legal);
1499 setOperationAction(ISD::MUL, NativeVT, Legal);
1500 setOperationAction(ISD::AND, NativeVT, Legal);
1501 setOperationAction(ISD::OR, NativeVT, Legal);
1502 setOperationAction(ISD::XOR, NativeVT, Legal);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001503 }
1504
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00001505 // Custom lower unaligned loads.
1506 for (MVT VecVT : {MVT::i32, MVT::v4i8, MVT::i64, MVT::v8i8,
1507 MVT::v2i16, MVT::v4i16, MVT::v2i32}) {
1508 setOperationAction(ISD::LOAD, VecVT, Custom);
1509 }
1510
Krzysztof Parzyszek99152912018-03-16 15:03:37 +00001511 for (MVT VT : {MVT::v2i16, MVT::v4i8, MVT::v2i32, MVT::v4i16, MVT::v2i32}) {
1512 setCondCodeAction(ISD::SETLT, VT, Expand);
1513 setCondCodeAction(ISD::SETLE, VT, Expand);
1514 setCondCodeAction(ISD::SETULT, VT, Expand);
1515 setCondCodeAction(ISD::SETULE, VT, Expand);
1516 }
1517
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001518 // Custom-lower bitcasts from i8 to v8i1.
1519 setOperationAction(ISD::BITCAST, MVT::i8, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001520 setOperationAction(ISD::SETCC, MVT::v2i16, Custom);
1521 setOperationAction(ISD::VSELECT, MVT::v2i16, Custom);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001522 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i8, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001523 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
1524 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00001525
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001526 // Subtarget-specific operation actions.
1527 //
1528 if (Subtarget.hasV5TOps()) {
1529 setOperationAction(ISD::FMA, MVT::f64, Expand);
1530 setOperationAction(ISD::FADD, MVT::f64, Expand);
1531 setOperationAction(ISD::FSUB, MVT::f64, Expand);
1532 setOperationAction(ISD::FMUL, MVT::f64, Expand);
1533
Krzysztof Parzyszekbd8ef4b2016-08-19 13:34:31 +00001534 setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
1535 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
1536
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001537 setOperationAction(ISD::FP_TO_UINT, MVT::i1, Promote);
1538 setOperationAction(ISD::FP_TO_UINT, MVT::i8, Promote);
1539 setOperationAction(ISD::FP_TO_UINT, MVT::i16, Promote);
1540 setOperationAction(ISD::FP_TO_SINT, MVT::i1, Promote);
1541 setOperationAction(ISD::FP_TO_SINT, MVT::i8, Promote);
1542 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
1543 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Promote);
1544 setOperationAction(ISD::UINT_TO_FP, MVT::i8, Promote);
1545 setOperationAction(ISD::UINT_TO_FP, MVT::i16, Promote);
1546 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Promote);
1547 setOperationAction(ISD::SINT_TO_FP, MVT::i8, Promote);
1548 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001549 } else { // V4
1550 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
1551 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Expand);
1552 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
1553 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
1554 setOperationAction(ISD::FP_TO_SINT, MVT::f64, Expand);
1555 setOperationAction(ISD::FP_TO_SINT, MVT::f32, Expand);
1556 setOperationAction(ISD::FP_EXTEND, MVT::f32, Expand);
1557 setOperationAction(ISD::FP_ROUND, MVT::f64, Expand);
1558 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
1559
1560 setOperationAction(ISD::CTPOP, MVT::i8, Expand);
1561 setOperationAction(ISD::CTPOP, MVT::i16, Expand);
1562 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
1563 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
1564
1565 // Expand these operations for both f32 and f64:
Benjamin Kramer62460692015-04-25 14:46:53 +00001566 for (unsigned FPExpOpV4 :
1567 {ISD::FADD, ISD::FSUB, ISD::FMUL, ISD::FABS, ISD::FNEG, ISD::FMA}) {
1568 setOperationAction(FPExpOpV4, MVT::f32, Expand);
1569 setOperationAction(FPExpOpV4, MVT::f64, Expand);
1570 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001571
Benjamin Kramer62460692015-04-25 14:46:53 +00001572 for (ISD::CondCode FPExpCCV4 :
1573 {ISD::SETOEQ, ISD::SETOGT, ISD::SETOLT, ISD::SETOGE, ISD::SETOLE,
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001574 ISD::SETUO, ISD::SETO}) {
Benjamin Kramer62460692015-04-25 14:46:53 +00001575 setCondCodeAction(FPExpCCV4, MVT::f32, Expand);
1576 setCondCodeAction(FPExpCCV4, MVT::f64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001577 }
1578 }
1579
1580 // Handling of indexed loads/stores: default is "expand".
1581 //
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00001582 for (MVT VT : {MVT::i8, MVT::i16, MVT::i32, MVT::i64, MVT::v2i16,
1583 MVT::v2i32, MVT::v4i8, MVT::v4i16, MVT::v8i8}) {
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +00001584 setIndexedLoadAction(ISD::POST_INC, VT, Legal);
1585 setIndexedStoreAction(ISD::POST_INC, VT, Legal);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001586 }
1587
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00001588 if (Subtarget.useHVXOps())
1589 initializeHVXLowering();
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00001590
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001591 computeRegisterProperties(&HRI);
1592
1593 //
1594 // Library calls for unsupported operations
1595 //
1596 bool FastMath = EnableFastMath;
1597
Benjamin Kramera37c8092015-04-25 14:46:46 +00001598 setLibcallName(RTLIB::SDIV_I32, "__hexagon_divsi3");
1599 setLibcallName(RTLIB::SDIV_I64, "__hexagon_divdi3");
1600 setLibcallName(RTLIB::UDIV_I32, "__hexagon_udivsi3");
1601 setLibcallName(RTLIB::UDIV_I64, "__hexagon_udivdi3");
1602 setLibcallName(RTLIB::SREM_I32, "__hexagon_modsi3");
1603 setLibcallName(RTLIB::SREM_I64, "__hexagon_moddi3");
1604 setLibcallName(RTLIB::UREM_I32, "__hexagon_umodsi3");
1605 setLibcallName(RTLIB::UREM_I64, "__hexagon_umoddi3");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001606
Benjamin Kramera37c8092015-04-25 14:46:46 +00001607 setLibcallName(RTLIB::SINTTOFP_I128_F64, "__hexagon_floattidf");
1608 setLibcallName(RTLIB::SINTTOFP_I128_F32, "__hexagon_floattisf");
1609 setLibcallName(RTLIB::FPTOUINT_F32_I128, "__hexagon_fixunssfti");
1610 setLibcallName(RTLIB::FPTOUINT_F64_I128, "__hexagon_fixunsdfti");
1611 setLibcallName(RTLIB::FPTOSINT_F32_I128, "__hexagon_fixsfti");
1612 setLibcallName(RTLIB::FPTOSINT_F64_I128, "__hexagon_fixdfti");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001613
1614 if (IsV4) {
1615 // Handle single-precision floating point operations on V4.
Benjamin Kramera37c8092015-04-25 14:46:46 +00001616 if (FastMath) {
1617 setLibcallName(RTLIB::ADD_F32, "__hexagon_fast_addsf3");
1618 setLibcallName(RTLIB::SUB_F32, "__hexagon_fast_subsf3");
1619 setLibcallName(RTLIB::MUL_F32, "__hexagon_fast_mulsf3");
1620 setLibcallName(RTLIB::OGT_F32, "__hexagon_fast_gtsf2");
1621 setLibcallName(RTLIB::OLT_F32, "__hexagon_fast_ltsf2");
1622 // Double-precision compares.
1623 setLibcallName(RTLIB::OGT_F64, "__hexagon_fast_gtdf2");
1624 setLibcallName(RTLIB::OLT_F64, "__hexagon_fast_ltdf2");
1625 } else {
1626 setLibcallName(RTLIB::ADD_F32, "__hexagon_addsf3");
1627 setLibcallName(RTLIB::SUB_F32, "__hexagon_subsf3");
1628 setLibcallName(RTLIB::MUL_F32, "__hexagon_mulsf3");
1629 setLibcallName(RTLIB::OGT_F32, "__hexagon_gtsf2");
1630 setLibcallName(RTLIB::OLT_F32, "__hexagon_ltsf2");
1631 // Double-precision compares.
1632 setLibcallName(RTLIB::OGT_F64, "__hexagon_gtdf2");
1633 setLibcallName(RTLIB::OLT_F64, "__hexagon_ltdf2");
1634 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001635 }
1636
1637 // This is the only fast library function for sqrtd.
1638 if (FastMath)
Benjamin Kramera37c8092015-04-25 14:46:46 +00001639 setLibcallName(RTLIB::SQRT_F64, "__hexagon_fast2_sqrtdf2");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001640
Benjamin Kramera37c8092015-04-25 14:46:46 +00001641 // Prefix is: nothing for "slow-math",
1642 // "fast2_" for V4 fast-math and V5+ fast-math double-precision
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001643 // (actually, keep fast-math and fast-math2 separate for now)
Benjamin Kramera37c8092015-04-25 14:46:46 +00001644 if (FastMath) {
1645 setLibcallName(RTLIB::ADD_F64, "__hexagon_fast_adddf3");
1646 setLibcallName(RTLIB::SUB_F64, "__hexagon_fast_subdf3");
1647 setLibcallName(RTLIB::MUL_F64, "__hexagon_fast_muldf3");
1648 setLibcallName(RTLIB::DIV_F64, "__hexagon_fast_divdf3");
1649 // Calling __hexagon_fast2_divsf3 with fast-math on V5 (ok).
1650 setLibcallName(RTLIB::DIV_F32, "__hexagon_fast_divsf3");
1651 } else {
1652 setLibcallName(RTLIB::ADD_F64, "__hexagon_adddf3");
1653 setLibcallName(RTLIB::SUB_F64, "__hexagon_subdf3");
1654 setLibcallName(RTLIB::MUL_F64, "__hexagon_muldf3");
1655 setLibcallName(RTLIB::DIV_F64, "__hexagon_divdf3");
1656 setLibcallName(RTLIB::DIV_F32, "__hexagon_divsf3");
1657 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001658
1659 if (Subtarget.hasV5TOps()) {
1660 if (FastMath)
Benjamin Kramera37c8092015-04-25 14:46:46 +00001661 setLibcallName(RTLIB::SQRT_F32, "__hexagon_fast2_sqrtf");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001662 else
Benjamin Kramera37c8092015-04-25 14:46:46 +00001663 setLibcallName(RTLIB::SQRT_F32, "__hexagon_sqrtf");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001664 } else {
1665 // V4
Benjamin Kramera37c8092015-04-25 14:46:46 +00001666 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__hexagon_floatsisf");
1667 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__hexagon_floatsidf");
1668 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__hexagon_floatdisf");
1669 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__hexagon_floatdidf");
1670 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__hexagon_floatunsisf");
1671 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__hexagon_floatunsidf");
1672 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__hexagon_floatundisf");
1673 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__hexagon_floatundidf");
1674 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__hexagon_fixunssfsi");
1675 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__hexagon_fixunssfdi");
1676 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__hexagon_fixunsdfsi");
1677 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__hexagon_fixunsdfdi");
1678 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__hexagon_fixsfsi");
1679 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__hexagon_fixsfdi");
1680 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__hexagon_fixdfsi");
1681 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__hexagon_fixdfdi");
1682 setLibcallName(RTLIB::FPEXT_F32_F64, "__hexagon_extendsfdf2");
1683 setLibcallName(RTLIB::FPROUND_F64_F32, "__hexagon_truncdfsf2");
1684 setLibcallName(RTLIB::OEQ_F32, "__hexagon_eqsf2");
1685 setLibcallName(RTLIB::OEQ_F64, "__hexagon_eqdf2");
1686 setLibcallName(RTLIB::OGE_F32, "__hexagon_gesf2");
1687 setLibcallName(RTLIB::OGE_F64, "__hexagon_gedf2");
1688 setLibcallName(RTLIB::OLE_F32, "__hexagon_lesf2");
1689 setLibcallName(RTLIB::OLE_F64, "__hexagon_ledf2");
1690 setLibcallName(RTLIB::UNE_F32, "__hexagon_nesf2");
1691 setLibcallName(RTLIB::UNE_F64, "__hexagon_nedf2");
1692 setLibcallName(RTLIB::UO_F32, "__hexagon_unordsf2");
1693 setLibcallName(RTLIB::UO_F64, "__hexagon_unorddf2");
1694 setLibcallName(RTLIB::O_F32, "__hexagon_unordsf2");
1695 setLibcallName(RTLIB::O_F64, "__hexagon_unorddf2");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001696 }
1697
1698 // These cause problems when the shift amount is non-constant.
1699 setLibcallName(RTLIB::SHL_I128, nullptr);
1700 setLibcallName(RTLIB::SRL_I128, nullptr);
1701 setLibcallName(RTLIB::SRA_I128, nullptr);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001702}
1703
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001704const char* HexagonTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00001705 switch ((HexagonISD::NodeType)Opcode) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001706 case HexagonISD::ALLOCA: return "HexagonISD::ALLOCA";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001707 case HexagonISD::AT_GOT: return "HexagonISD::AT_GOT";
1708 case HexagonISD::AT_PCREL: return "HexagonISD::AT_PCREL";
1709 case HexagonISD::BARRIER: return "HexagonISD::BARRIER";
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00001710 case HexagonISD::CALL: return "HexagonISD::CALL";
1711 case HexagonISD::CALLnr: return "HexagonISD::CALLnr";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001712 case HexagonISD::CALLR: return "HexagonISD::CALLR";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001713 case HexagonISD::COMBINE: return "HexagonISD::COMBINE";
1714 case HexagonISD::CONST32_GP: return "HexagonISD::CONST32_GP";
1715 case HexagonISD::CONST32: return "HexagonISD::CONST32";
1716 case HexagonISD::CP: return "HexagonISD::CP";
1717 case HexagonISD::DCFETCH: return "HexagonISD::DCFETCH";
1718 case HexagonISD::EH_RETURN: return "HexagonISD::EH_RETURN";
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001719 case HexagonISD::TSTBIT: return "HexagonISD::TSTBIT";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001720 case HexagonISD::EXTRACTU: return "HexagonISD::EXTRACTU";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001721 case HexagonISD::INSERT: return "HexagonISD::INSERT";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001722 case HexagonISD::JT: return "HexagonISD::JT";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001723 case HexagonISD::RET_FLAG: return "HexagonISD::RET_FLAG";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001724 case HexagonISD::TC_RETURN: return "HexagonISD::TC_RETURN";
Krzysztof Parzyszekf85dd9f2017-07-10 20:16:44 +00001725 case HexagonISD::VASL: return "HexagonISD::VASL";
1726 case HexagonISD::VASR: return "HexagonISD::VASR";
1727 case HexagonISD::VLSR: return "HexagonISD::VLSR";
1728 case HexagonISD::VSPLAT: return "HexagonISD::VSPLAT";
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001729 case HexagonISD::VEXTRACTW: return "HexagonISD::VEXTRACTW";
1730 case HexagonISD::VINSERTW0: return "HexagonISD::VINSERTW0";
1731 case HexagonISD::VROR: return "HexagonISD::VROR";
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +00001732 case HexagonISD::READCYCLE: return "HexagonISD::READCYCLE";
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00001733 case HexagonISD::VZERO: return "HexagonISD::VZERO";
Krzysztof Parzyszek41a24b72018-04-20 19:38:37 +00001734 case HexagonISD::VSPLATW: return "HexagonISD::VSPLATW";
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001735 case HexagonISD::D2P: return "HexagonISD::D2P";
1736 case HexagonISD::P2D: return "HexagonISD::P2D";
1737 case HexagonISD::V2Q: return "HexagonISD::V2Q";
1738 case HexagonISD::Q2V: return "HexagonISD::Q2V";
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00001739 case HexagonISD::QCAT: return "HexagonISD::QCAT";
Krzysztof Parzyszek69f1d7e2018-02-06 14:16:52 +00001740 case HexagonISD::QTRUE: return "HexagonISD::QTRUE";
1741 case HexagonISD::QFALSE: return "HexagonISD::QFALSE";
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001742 case HexagonISD::TYPECAST: return "HexagonISD::TYPECAST";
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00001743 case HexagonISD::VALIGN: return "HexagonISD::VALIGN";
Krzysztof Parzyszekad83ce42018-02-14 20:46:06 +00001744 case HexagonISD::VALIGNADDR: return "HexagonISD::VALIGNADDR";
Matthias Braund04893f2015-05-07 21:33:59 +00001745 case HexagonISD::OP_END: break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001746 }
Matthias Braund04893f2015-05-07 21:33:59 +00001747 return nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001748}
1749
Krzysztof Parzyszekdc7a5572018-03-29 13:52:46 +00001750// Bit-reverse Load Intrinsic: Check if the instruction is a bit reverse load
1751// intrinsic.
1752static bool isBrevLdIntrinsic(const Value *Inst) {
1753 unsigned ID = cast<IntrinsicInst>(Inst)->getIntrinsicID();
1754 return (ID == Intrinsic::hexagon_L2_loadrd_pbr ||
1755 ID == Intrinsic::hexagon_L2_loadri_pbr ||
1756 ID == Intrinsic::hexagon_L2_loadrh_pbr ||
1757 ID == Intrinsic::hexagon_L2_loadruh_pbr ||
1758 ID == Intrinsic::hexagon_L2_loadrb_pbr ||
1759 ID == Intrinsic::hexagon_L2_loadrub_pbr);
1760}
1761
1762// Bit-reverse Load Intrinsic :Crawl up and figure out the object from previous
1763// instruction. So far we only handle bitcast, extract value and bit reverse
1764// load intrinsic instructions. Should we handle CGEP ?
1765static Value *getBrevLdObject(Value *V) {
1766 if (Operator::getOpcode(V) == Instruction::ExtractValue ||
1767 Operator::getOpcode(V) == Instruction::BitCast)
1768 V = cast<Operator>(V)->getOperand(0);
1769 else if (isa<IntrinsicInst>(V) && isBrevLdIntrinsic(V))
1770 V = cast<Instruction>(V)->getOperand(0);
1771 return V;
1772}
1773
1774// Bit-reverse Load Intrinsic: For a PHI Node return either an incoming edge or
1775// a back edge. If the back edge comes from the intrinsic itself, the incoming
1776// edge is returned.
1777static Value *returnEdge(const PHINode *PN, Value *IntrBaseVal) {
1778 const BasicBlock *Parent = PN->getParent();
1779 int Idx = -1;
1780 for (unsigned i = 0, e = PN->getNumIncomingValues(); i < e; ++i) {
1781 BasicBlock *Blk = PN->getIncomingBlock(i);
1782 // Determine if the back edge is originated from intrinsic.
1783 if (Blk == Parent) {
1784 Value *BackEdgeVal = PN->getIncomingValue(i);
1785 Value *BaseVal;
1786 // Loop over till we return the same Value or we hit the IntrBaseVal.
1787 do {
1788 BaseVal = BackEdgeVal;
1789 BackEdgeVal = getBrevLdObject(BackEdgeVal);
1790 } while ((BaseVal != BackEdgeVal) && (IntrBaseVal != BackEdgeVal));
1791 // If the getBrevLdObject returns IntrBaseVal, we should return the
1792 // incoming edge.
1793 if (IntrBaseVal == BackEdgeVal)
1794 continue;
1795 Idx = i;
1796 break;
1797 } else // Set the node to incoming edge.
1798 Idx = i;
1799 }
1800 assert(Idx >= 0 && "Unexpected index to incoming argument in PHI");
1801 return PN->getIncomingValue(Idx);
1802}
1803
1804// Bit-reverse Load Intrinsic: Figure out the underlying object the base
1805// pointer points to, for the bit-reverse load intrinsic. Setting this to
1806// memoperand might help alias analysis to figure out the dependencies.
1807static Value *getUnderLyingObjectForBrevLdIntr(Value *V) {
1808 Value *IntrBaseVal = V;
1809 Value *BaseVal;
1810 // Loop over till we return the same Value, implies we either figure out
1811 // the object or we hit a PHI
1812 do {
1813 BaseVal = V;
1814 V = getBrevLdObject(V);
1815 } while (BaseVal != V);
1816
1817 // Identify the object from PHINode.
1818 if (const PHINode *PN = dyn_cast<PHINode>(V))
1819 return returnEdge(PN, IntrBaseVal);
1820 // For non PHI nodes, the object is the last value returned by getBrevLdObject
1821 else
1822 return V;
1823}
1824
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001825/// Given an intrinsic, checks if on the target the intrinsic will need to map
1826/// to a MemIntrinsicNode (touches memory). If this is the case, it returns
1827/// true and store the intrinsic information into the IntrinsicInfo that was
1828/// passed to the function.
1829bool HexagonTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
1830 const CallInst &I,
Matt Arsenault7d7adf42017-12-14 22:34:10 +00001831 MachineFunction &MF,
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001832 unsigned Intrinsic) const {
1833 switch (Intrinsic) {
Krzysztof Parzyszekdc7a5572018-03-29 13:52:46 +00001834 case Intrinsic::hexagon_L2_loadrd_pbr:
1835 case Intrinsic::hexagon_L2_loadri_pbr:
1836 case Intrinsic::hexagon_L2_loadrh_pbr:
1837 case Intrinsic::hexagon_L2_loadruh_pbr:
1838 case Intrinsic::hexagon_L2_loadrb_pbr:
1839 case Intrinsic::hexagon_L2_loadrub_pbr: {
1840 Info.opc = ISD::INTRINSIC_W_CHAIN;
1841 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
1842 auto &Cont = I.getCalledFunction()->getParent()->getContext();
1843 // The intrinsic function call is of the form { ElTy, i8* }
1844 // @llvm.hexagon.L2.loadXX.pbr(i8*, i32). The pointer and memory access type
1845 // should be derived from ElTy.
1846 PointerType *PtrTy = I.getCalledFunction()
1847 ->getReturnType()
1848 ->getContainedType(0)
1849 ->getPointerTo();
1850 Info.memVT = MVT::getVT(PtrTy->getElementType());
1851 llvm::Value *BasePtrVal = I.getOperand(0);
1852 Info.ptrVal = getUnderLyingObjectForBrevLdIntr(BasePtrVal);
1853 // The offset value comes through Modifier register. For now, assume the
1854 // offset is 0.
1855 Info.offset = 0;
1856 Info.align = DL.getABITypeAlignment(Info.memVT.getTypeForEVT(Cont));
1857 Info.flags = MachineMemOperand::MOLoad;
1858 return true;
1859 }
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001860 case Intrinsic::hexagon_V6_vgathermw:
1861 case Intrinsic::hexagon_V6_vgathermw_128B:
1862 case Intrinsic::hexagon_V6_vgathermh:
1863 case Intrinsic::hexagon_V6_vgathermh_128B:
1864 case Intrinsic::hexagon_V6_vgathermhw:
1865 case Intrinsic::hexagon_V6_vgathermhw_128B:
1866 case Intrinsic::hexagon_V6_vgathermwq:
1867 case Intrinsic::hexagon_V6_vgathermwq_128B:
1868 case Intrinsic::hexagon_V6_vgathermhq:
1869 case Intrinsic::hexagon_V6_vgathermhq_128B:
1870 case Intrinsic::hexagon_V6_vgathermhwq:
1871 case Intrinsic::hexagon_V6_vgathermhwq_128B: {
1872 const Module &M = *I.getParent()->getParent()->getParent();
1873 Info.opc = ISD::INTRINSIC_W_CHAIN;
1874 Type *VecTy = I.getArgOperand(1)->getType();
1875 Info.memVT = MVT::getVT(VecTy);
1876 Info.ptrVal = I.getArgOperand(0);
1877 Info.offset = 0;
1878 Info.align = M.getDataLayout().getTypeAllocSizeInBits(VecTy) / 8;
Matt Arsenault11171332017-12-14 21:39:51 +00001879 Info.flags = MachineMemOperand::MOLoad |
1880 MachineMemOperand::MOStore |
1881 MachineMemOperand::MOVolatile;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001882 return true;
1883 }
1884 default:
1885 break;
1886 }
1887 return false;
1888}
1889
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001890bool HexagonTargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00001891 return isTruncateFree(EVT::getEVT(Ty1), EVT::getEVT(Ty2));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001892}
1893
1894bool HexagonTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001895 if (!VT1.isSimple() || !VT2.isSimple())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001896 return false;
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00001897 return VT1.getSimpleVT() == MVT::i64 && VT2.getSimpleVT() == MVT::i32;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001898}
1899
Krzysztof Parzyszekbd8ef4b2016-08-19 13:34:31 +00001900bool HexagonTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
1901 return isOperationLegalOrCustom(ISD::FMA, VT);
1902}
1903
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001904// Should we expand the build vector with shuffles?
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00001905bool HexagonTargetLowering::shouldExpandBuildVectorWithShuffles(EVT VT,
1906 unsigned DefinedValues) const {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001907 return false;
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00001908}
1909
Zvi Rackover1b736822017-07-26 08:06:58 +00001910bool HexagonTargetLowering::isShuffleMaskLegal(ArrayRef<int> Mask,
1911 EVT VT) const {
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00001912 return true;
1913}
1914
Krzysztof Parzyszek5439a702017-12-18 18:21:01 +00001915TargetLoweringBase::LegalizeTypeAction
1916HexagonTargetLowering::getPreferredVectorAction(EVT VT) const {
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00001917 if (VT.getVectorNumElements() == 1)
1918 return TargetLoweringBase::TypeScalarizeVector;
1919
1920 // Always widen vectors of i1.
1921 MVT ElemTy = VT.getSimpleVT().getVectorElementType();
1922 if (ElemTy == MVT::i1)
1923 return TargetLoweringBase::TypeWidenVector;
1924
Krzysztof Parzyszek5439a702017-12-18 18:21:01 +00001925 if (Subtarget.useHVXOps()) {
1926 // If the size of VT is at least half of the vector length,
1927 // widen the vector. Note: the threshold was not selected in
1928 // any scientific way.
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00001929 ArrayRef<MVT> Tys = Subtarget.getHVXElementTypes();
1930 if (llvm::find(Tys, ElemTy) != Tys.end()) {
1931 unsigned HwWidth = 8*Subtarget.getVectorLength();
1932 unsigned VecWidth = VT.getSizeInBits();
1933 if (VecWidth >= HwWidth/2 && VecWidth < HwWidth)
1934 return TargetLoweringBase::TypeWidenVector;
1935 }
Krzysztof Parzyszek5439a702017-12-18 18:21:01 +00001936 }
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00001937 return TargetLoweringBase::TypeSplitVector;
Krzysztof Parzyszek5439a702017-12-18 18:21:01 +00001938}
1939
Krzysztof Parzyszekad83ce42018-02-14 20:46:06 +00001940std::pair<SDValue, int>
1941HexagonTargetLowering::getBaseAndOffset(SDValue Addr) const {
1942 if (Addr.getOpcode() == ISD::ADD) {
1943 SDValue Op1 = Addr.getOperand(1);
1944 if (auto *CN = dyn_cast<const ConstantSDNode>(Op1.getNode()))
1945 return { Addr.getOperand(0), CN->getSExtValue() };
1946 }
1947 return { Addr, 0 };
1948}
1949
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00001950// Lower a vector shuffle (V1, V2, V3). V1 and V2 are the two vectors
1951// to select data from, V3 is the permutation.
1952SDValue
1953HexagonTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG)
1954 const {
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001955 const auto *SVN = cast<ShuffleVectorSDNode>(Op);
1956 ArrayRef<int> AM = SVN->getMask();
1957 assert(AM.size() <= 8 && "Unexpected shuffle mask");
1958 unsigned VecLen = AM.size();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001959
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001960 MVT VecTy = ty(Op);
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00001961 assert(!Subtarget.isHVXVectorType(VecTy, true) &&
1962 "HVX shuffles should be legal");
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001963 assert(VecTy.getSizeInBits() <= 64 && "Unexpected vector length");
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001964
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001965 SDValue Op0 = Op.getOperand(0);
1966 SDValue Op1 = Op.getOperand(1);
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +00001967 const SDLoc &dl(Op);
1968
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001969 // If the inputs are not the same as the output, bail. This is not an
1970 // error situation, but complicates the handling and the default expansion
1971 // (into BUILD_VECTOR) should be adequate.
1972 if (ty(Op0) != VecTy || ty(Op1) != VecTy)
1973 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001974
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001975 // Normalize the mask so that the first non-negative index comes from
1976 // the first operand.
1977 SmallVector<int,8> Mask(AM.begin(), AM.end());
1978 unsigned F = llvm::find_if(AM, [](int M) { return M >= 0; }) - AM.data();
1979 if (F == AM.size())
1980 return DAG.getUNDEF(VecTy);
1981 if (AM[F] >= int(VecLen)) {
1982 ShuffleVectorSDNode::commuteMask(Mask);
1983 std::swap(Op0, Op1);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001984 }
1985
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001986 // Express the shuffle mask in terms of bytes.
1987 SmallVector<int,8> ByteMask;
1988 unsigned ElemBytes = VecTy.getVectorElementType().getSizeInBits() / 8;
1989 for (unsigned i = 0, e = Mask.size(); i != e; ++i) {
1990 int M = Mask[i];
1991 if (M < 0) {
1992 for (unsigned j = 0; j != ElemBytes; ++j)
1993 ByteMask.push_back(-1);
1994 } else {
1995 for (unsigned j = 0; j != ElemBytes; ++j)
1996 ByteMask.push_back(M*ElemBytes + j);
1997 }
1998 }
1999 assert(ByteMask.size() <= 8);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002000
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00002001 // All non-undef (non-negative) indexes are well within [0..127], so they
2002 // fit in a single byte. Build two 64-bit words:
2003 // - MaskIdx where each byte is the corresponding index (for non-negative
2004 // indexes), and 0xFF for negative indexes, and
2005 // - MaskUnd that has 0xFF for each negative index.
2006 uint64_t MaskIdx = 0;
2007 uint64_t MaskUnd = 0;
2008 for (unsigned i = 0, e = ByteMask.size(); i != e; ++i) {
2009 unsigned S = 8*i;
2010 uint64_t M = ByteMask[i] & 0xFF;
2011 if (M == 0xFF)
2012 MaskUnd |= M << S;
2013 MaskIdx |= M << S;
2014 }
2015
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00002016 if (ByteMask.size() == 4) {
2017 // Identity.
2018 if (MaskIdx == (0x03020100 | MaskUnd))
2019 return Op0;
2020 // Byte swap.
2021 if (MaskIdx == (0x00010203 | MaskUnd)) {
2022 SDValue T0 = DAG.getBitcast(MVT::i32, Op0);
2023 SDValue T1 = DAG.getNode(ISD::BSWAP, dl, MVT::i32, T0);
2024 return DAG.getBitcast(VecTy, T1);
2025 }
2026
2027 // Byte packs.
2028 SDValue Concat10 = DAG.getNode(HexagonISD::COMBINE, dl,
2029 typeJoin({ty(Op1), ty(Op0)}), {Op1, Op0});
2030 if (MaskIdx == (0x06040200 | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002031 return getInstr(Hexagon::S2_vtrunehb, dl, VecTy, {Concat10}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00002032 if (MaskIdx == (0x07050301 | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002033 return getInstr(Hexagon::S2_vtrunohb, dl, VecTy, {Concat10}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00002034
2035 SDValue Concat01 = DAG.getNode(HexagonISD::COMBINE, dl,
2036 typeJoin({ty(Op0), ty(Op1)}), {Op0, Op1});
2037 if (MaskIdx == (0x02000604 | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002038 return getInstr(Hexagon::S2_vtrunehb, dl, VecTy, {Concat01}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00002039 if (MaskIdx == (0x03010705 | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002040 return getInstr(Hexagon::S2_vtrunohb, dl, VecTy, {Concat01}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00002041 }
2042
2043 if (ByteMask.size() == 8) {
2044 // Identity.
2045 if (MaskIdx == (0x0706050403020100ull | MaskUnd))
2046 return Op0;
2047 // Byte swap.
2048 if (MaskIdx == (0x0001020304050607ull | MaskUnd)) {
2049 SDValue T0 = DAG.getBitcast(MVT::i64, Op0);
2050 SDValue T1 = DAG.getNode(ISD::BSWAP, dl, MVT::i64, T0);
2051 return DAG.getBitcast(VecTy, T1);
2052 }
2053
2054 // Halfword picks.
2055 if (MaskIdx == (0x0d0c050409080100ull | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002056 return getInstr(Hexagon::S2_shuffeh, dl, VecTy, {Op1, Op0}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00002057 if (MaskIdx == (0x0f0e07060b0a0302ull | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002058 return getInstr(Hexagon::S2_shuffoh, dl, VecTy, {Op1, Op0}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00002059 if (MaskIdx == (0x0d0c090805040100ull | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002060 return getInstr(Hexagon::S2_vtrunewh, dl, VecTy, {Op1, Op0}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00002061 if (MaskIdx == (0x0f0e0b0a07060302ull | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002062 return getInstr(Hexagon::S2_vtrunowh, dl, VecTy, {Op1, Op0}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00002063 if (MaskIdx == (0x0706030205040100ull | MaskUnd)) {
2064 VectorPair P = opSplit(Op0, dl, DAG);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002065 return getInstr(Hexagon::S2_packhl, dl, VecTy, {P.second, P.first}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00002066 }
2067
2068 // Byte packs.
2069 if (MaskIdx == (0x0e060c040a020800ull | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002070 return getInstr(Hexagon::S2_shuffeb, dl, VecTy, {Op1, Op0}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00002071 if (MaskIdx == (0x0f070d050b030901ull | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002072 return getInstr(Hexagon::S2_shuffob, dl, VecTy, {Op1, Op0}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00002073 }
2074
2075 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002076}
2077
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00002078// Create a Hexagon-specific node for shifting a vector by an integer.
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002079SDValue
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00002080HexagonTargetLowering::getVectorShiftByInt(SDValue Op, SelectionDAG &DAG)
2081 const {
Krzysztof Parzyszek1108ee22018-01-31 20:49:24 +00002082 if (auto *BVN = dyn_cast<BuildVectorSDNode>(Op.getOperand(1).getNode())) {
2083 if (SDValue S = BVN->getSplatValue()) {
2084 unsigned NewOpc;
2085 switch (Op.getOpcode()) {
2086 case ISD::SHL:
2087 NewOpc = HexagonISD::VASL;
2088 break;
2089 case ISD::SRA:
2090 NewOpc = HexagonISD::VASR;
2091 break;
2092 case ISD::SRL:
2093 NewOpc = HexagonISD::VLSR;
2094 break;
2095 default:
2096 llvm_unreachable("Unexpected shift opcode");
2097 }
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00002098 return DAG.getNode(NewOpc, SDLoc(Op), ty(Op), Op.getOperand(0), S);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002099 }
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002100 }
2101
Krzysztof Parzyszek1108ee22018-01-31 20:49:24 +00002102 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002103}
2104
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002105SDValue
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00002106HexagonTargetLowering::LowerVECTOR_SHIFT(SDValue Op, SelectionDAG &DAG) const {
2107 return getVectorShiftByInt(Op, DAG);
2108}
2109
2110SDValue
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002111HexagonTargetLowering::LowerBITCAST(SDValue Op, SelectionDAG &DAG) const {
2112 MVT ResTy = ty(Op);
2113 SDValue InpV = Op.getOperand(0);
2114 MVT InpTy = ty(InpV);
2115 assert(ResTy.getSizeInBits() == InpTy.getSizeInBits());
2116 const SDLoc &dl(Op);
2117
2118 // Handle conversion from i8 to v8i1.
2119 if (ResTy == MVT::v8i1) {
2120 SDValue Sc = DAG.getBitcast(tyScalar(InpTy), InpV);
2121 SDValue Ext = DAG.getZExtOrTrunc(Sc, dl, MVT::i32);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002122 return getInstr(Hexagon::C2_tfrrp, dl, ResTy, Ext, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002123 }
2124
2125 return SDValue();
2126}
2127
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002128bool
2129HexagonTargetLowering::getBuildVectorConstInts(ArrayRef<SDValue> Values,
2130 MVT VecTy, SelectionDAG &DAG,
2131 MutableArrayRef<ConstantInt*> Consts) const {
2132 MVT ElemTy = VecTy.getVectorElementType();
2133 unsigned ElemWidth = ElemTy.getSizeInBits();
2134 IntegerType *IntTy = IntegerType::get(*DAG.getContext(), ElemWidth);
2135 bool AllConst = true;
2136
2137 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
2138 SDValue V = Values[i];
2139 if (V.isUndef()) {
2140 Consts[i] = ConstantInt::get(IntTy, 0);
2141 continue;
2142 }
Krzysztof Parzyszek4ef6cff2018-01-11 18:03:23 +00002143 // Make sure to always cast to IntTy.
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002144 if (auto *CN = dyn_cast<ConstantSDNode>(V.getNode())) {
2145 const ConstantInt *CI = CN->getConstantIntValue();
Krzysztof Parzyszek4ef6cff2018-01-11 18:03:23 +00002146 Consts[i] = ConstantInt::get(IntTy, CI->getValue().getSExtValue());
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002147 } else if (auto *CN = dyn_cast<ConstantFPSDNode>(V.getNode())) {
2148 const ConstantFP *CF = CN->getConstantFPValue();
2149 APInt A = CF->getValueAPF().bitcastToAPInt();
2150 Consts[i] = ConstantInt::get(IntTy, A.getZExtValue());
2151 } else {
2152 AllConst = false;
2153 }
2154 }
2155 return AllConst;
2156}
2157
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002158SDValue
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002159HexagonTargetLowering::buildVector32(ArrayRef<SDValue> Elem, const SDLoc &dl,
2160 MVT VecTy, SelectionDAG &DAG) const {
2161 MVT ElemTy = VecTy.getVectorElementType();
2162 assert(VecTy.getVectorNumElements() == Elem.size());
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002163
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002164 SmallVector<ConstantInt*,4> Consts(Elem.size());
2165 bool AllConst = getBuildVectorConstInts(Elem, VecTy, DAG, Consts);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002166
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002167 unsigned First, Num = Elem.size();
2168 for (First = 0; First != Num; ++First)
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002169 if (!isUndef(Elem[First]))
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002170 break;
2171 if (First == Num)
2172 return DAG.getUNDEF(VecTy);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002173
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002174 if (AllConst &&
2175 llvm::all_of(Consts, [](ConstantInt *CI) { return CI->isZero(); }))
2176 return getZero(dl, VecTy, DAG);
2177
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002178 if (ElemTy == MVT::i16) {
2179 assert(Elem.size() == 2);
2180 if (AllConst) {
2181 uint32_t V = (Consts[0]->getZExtValue() & 0xFFFF) |
2182 Consts[1]->getZExtValue() << 16;
2183 return DAG.getBitcast(MVT::v2i16, DAG.getConstant(V, dl, MVT::i32));
Krzysztof Parzyszek89b2d7c2017-07-13 18:17:58 +00002184 }
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002185 SDValue N = getInstr(Hexagon::A2_combine_ll, dl, MVT::i32,
2186 {Elem[1], Elem[0]}, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002187 return DAG.getBitcast(MVT::v2i16, N);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002188 }
2189
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002190 if (ElemTy == MVT::i8) {
2191 // First try generating a constant.
2192 if (AllConst) {
2193 int32_t V = (Consts[0]->getZExtValue() & 0xFF) |
2194 (Consts[1]->getZExtValue() & 0xFF) << 8 |
2195 (Consts[1]->getZExtValue() & 0xFF) << 16 |
2196 Consts[2]->getZExtValue() << 24;
2197 return DAG.getBitcast(MVT::v4i8, DAG.getConstant(V, dl, MVT::i32));
2198 }
2199
2200 // Then try splat.
2201 bool IsSplat = true;
2202 for (unsigned i = 0; i != Num; ++i) {
2203 if (i == First)
2204 continue;
2205 if (Elem[i] == Elem[First] || isUndef(Elem[i]))
2206 continue;
2207 IsSplat = false;
2208 break;
2209 }
2210 if (IsSplat) {
2211 // Legalize the operand to VSPLAT.
2212 SDValue Ext = DAG.getZExtOrTrunc(Elem[First], dl, MVT::i32);
2213 return DAG.getNode(HexagonISD::VSPLAT, dl, VecTy, Ext);
2214 }
2215
2216 // Generate
2217 // (zxtb(Elem[0]) | (zxtb(Elem[1]) << 8)) |
2218 // (zxtb(Elem[2]) | (zxtb(Elem[3]) << 8)) << 16
2219 assert(Elem.size() == 4);
2220 SDValue Vs[4];
2221 for (unsigned i = 0; i != 4; ++i) {
2222 Vs[i] = DAG.getZExtOrTrunc(Elem[i], dl, MVT::i32);
2223 Vs[i] = DAG.getZeroExtendInReg(Vs[i], dl, MVT::i8);
2224 }
2225 SDValue S8 = DAG.getConstant(8, dl, MVT::i32);
2226 SDValue T0 = DAG.getNode(ISD::SHL, dl, MVT::i32, {Vs[1], S8});
2227 SDValue T1 = DAG.getNode(ISD::SHL, dl, MVT::i32, {Vs[3], S8});
2228 SDValue B0 = DAG.getNode(ISD::OR, dl, MVT::i32, {Vs[0], T0});
2229 SDValue B1 = DAG.getNode(ISD::OR, dl, MVT::i32, {Vs[2], T1});
2230
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002231 SDValue R = getInstr(Hexagon::A2_combine_ll, dl, MVT::i32, {B1, B0}, DAG);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002232 return DAG.getBitcast(MVT::v4i8, R);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002233 }
2234
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002235#ifndef NDEBUG
2236 dbgs() << "VecTy: " << EVT(VecTy).getEVTString() << '\n';
2237#endif
2238 llvm_unreachable("Unexpected vector element type");
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002239}
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002240
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002241SDValue
2242HexagonTargetLowering::buildVector64(ArrayRef<SDValue> Elem, const SDLoc &dl,
2243 MVT VecTy, SelectionDAG &DAG) const {
2244 MVT ElemTy = VecTy.getVectorElementType();
2245 assert(VecTy.getVectorNumElements() == Elem.size());
2246
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002247 SmallVector<ConstantInt*,8> Consts(Elem.size());
2248 bool AllConst = getBuildVectorConstInts(Elem, VecTy, DAG, Consts);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002249
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002250 unsigned First, Num = Elem.size();
2251 for (First = 0; First != Num; ++First)
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002252 if (!isUndef(Elem[First]))
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002253 break;
2254 if (First == Num)
2255 return DAG.getUNDEF(VecTy);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002256
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002257 if (AllConst &&
2258 llvm::all_of(Consts, [](ConstantInt *CI) { return CI->isZero(); }))
2259 return getZero(dl, VecTy, DAG);
2260
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002261 // First try splat if possible.
2262 if (ElemTy == MVT::i16) {
2263 bool IsSplat = true;
2264 for (unsigned i = 0; i != Num; ++i) {
2265 if (i == First)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002266 continue;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002267 if (Elem[i] == Elem[First] || isUndef(Elem[i]))
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002268 continue;
2269 IsSplat = false;
2270 break;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002271 }
Krzysztof Parzyszekfb0fcac2017-12-20 20:33:49 +00002272 if (IsSplat) {
2273 // Legalize the operand to VSPLAT.
2274 SDValue Ext = DAG.getZExtOrTrunc(Elem[First], dl, MVT::i32);
2275 return DAG.getNode(HexagonISD::VSPLAT, dl, VecTy, Ext);
2276 }
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002277 }
2278
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002279 // Then try constant.
2280 if (AllConst) {
2281 uint64_t Val = 0;
2282 unsigned W = ElemTy.getSizeInBits();
2283 uint64_t Mask = (ElemTy == MVT::i8) ? 0xFFull
2284 : (ElemTy == MVT::i16) ? 0xFFFFull : 0xFFFFFFFFull;
2285 for (unsigned i = 0; i != Num; ++i)
Krzysztof Parzyszek240df6f2018-01-11 18:30:41 +00002286 Val = (Val << W) | (Consts[Num-1-i]->getZExtValue() & Mask);
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002287 SDValue V0 = DAG.getConstant(Val, dl, MVT::i64);
2288 return DAG.getBitcast(VecTy, V0);
2289 }
2290
2291 // Build two 32-bit vectors and concatenate.
2292 MVT HalfTy = MVT::getVectorVT(ElemTy, Num/2);
2293 SDValue L = (ElemTy == MVT::i32)
2294 ? Elem[0]
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002295 : buildVector32(Elem.take_front(Num/2), dl, HalfTy, DAG);
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002296 SDValue H = (ElemTy == MVT::i32)
2297 ? Elem[1]
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002298 : buildVector32(Elem.drop_front(Num/2), dl, HalfTy, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002299 return DAG.getNode(HexagonISD::COMBINE, dl, VecTy, {H, L});
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002300}
2301
2302SDValue
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002303HexagonTargetLowering::extractVector(SDValue VecV, SDValue IdxV,
2304 const SDLoc &dl, MVT ValTy, MVT ResTy,
2305 SelectionDAG &DAG) const {
2306 MVT VecTy = ty(VecV);
2307 assert(!ValTy.isVector() ||
2308 VecTy.getVectorElementType() == ValTy.getVectorElementType());
2309 unsigned VecWidth = VecTy.getSizeInBits();
2310 unsigned ValWidth = ValTy.getSizeInBits();
2311 unsigned ElemWidth = VecTy.getVectorElementType().getSizeInBits();
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002312 assert((VecWidth % ElemWidth) == 0);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002313 auto *IdxN = dyn_cast<ConstantSDNode>(IdxV);
2314
2315 // Special case for v{8,4,2}i1 (the only boolean vectors legal in Hexagon
2316 // without any coprocessors).
2317 if (ElemWidth == 1) {
2318 assert(VecWidth == VecTy.getVectorNumElements() && "Sanity failure");
2319 assert(VecWidth == 8 || VecWidth == 4 || VecWidth == 2);
2320 // Check if this is an extract of the lowest bit.
2321 if (IdxN) {
2322 // Extracting the lowest bit is a no-op, but it changes the type,
2323 // so it must be kept as an operation to avoid errors related to
2324 // type mismatches.
2325 if (IdxN->isNullValue() && ValTy.getSizeInBits() == 1)
2326 return DAG.getNode(HexagonISD::TYPECAST, dl, MVT::i1, VecV);
2327 }
2328
2329 // If the value extracted is a single bit, use tstbit.
2330 if (ValWidth == 1) {
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002331 SDValue A0 = getInstr(Hexagon::C2_tfrpr, dl, MVT::i32, {VecV}, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002332 return DAG.getNode(HexagonISD::TSTBIT, dl, MVT::i1, A0, IdxV);
2333 }
2334
2335 // Each bool vector (v2i1, v4i1, v8i1) always occupies 8 bits in
2336 // a predicate register. The elements of the vector are repeated
2337 // in the register (if necessary) so that the total number is 8.
2338 // The extracted subvector will need to be expanded in such a way.
2339 unsigned Scale = VecWidth / ValWidth;
2340
2341 // Generate (p2d VecV) >> 8*Idx to move the interesting bytes to
2342 // position 0.
2343 assert(ty(IdxV) == MVT::i32);
2344 SDValue S0 = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV,
Krzysztof Parzyszek99152912018-03-16 15:03:37 +00002345 DAG.getConstant(8*Scale, dl, MVT::i32));
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002346 SDValue T0 = DAG.getNode(HexagonISD::P2D, dl, MVT::i64, VecV);
2347 SDValue T1 = DAG.getNode(ISD::SRL, dl, MVT::i64, T0, S0);
2348 while (Scale > 1) {
2349 // The longest possible subvector is at most 32 bits, so it is always
2350 // contained in the low subregister.
2351 T1 = DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::i32, T1);
2352 T1 = expandPredicate(T1, dl, DAG);
2353 Scale /= 2;
2354 }
2355
2356 return DAG.getNode(HexagonISD::D2P, dl, ResTy, T1);
2357 }
2358
2359 assert(VecWidth == 32 || VecWidth == 64);
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002360
2361 // Cast everything to scalar integer types.
2362 MVT ScalarTy = tyScalar(VecTy);
2363 VecV = DAG.getBitcast(ScalarTy, VecV);
2364
2365 SDValue WidthV = DAG.getConstant(ValWidth, dl, MVT::i32);
2366 SDValue ExtV;
2367
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002368 if (IdxN) {
2369 unsigned Off = IdxN->getZExtValue() * ElemWidth;
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002370 if (VecWidth == 64 && ValWidth == 32) {
2371 assert(Off == 0 || Off == 32);
2372 unsigned SubIdx = Off == 0 ? Hexagon::isub_lo : Hexagon::isub_hi;
2373 ExtV = DAG.getTargetExtractSubreg(SubIdx, dl, MVT::i32, VecV);
2374 } else if (Off == 0 && (ValWidth % 8) == 0) {
2375 ExtV = DAG.getZeroExtendInReg(VecV, dl, tyScalar(ValTy));
2376 } else {
2377 SDValue OffV = DAG.getConstant(Off, dl, MVT::i32);
2378 // The return type of EXTRACTU must be the same as the type of the
2379 // input vector.
2380 ExtV = DAG.getNode(HexagonISD::EXTRACTU, dl, ScalarTy,
2381 {VecV, WidthV, OffV});
2382 }
2383 } else {
2384 if (ty(IdxV) != MVT::i32)
2385 IdxV = DAG.getZExtOrTrunc(IdxV, dl, MVT::i32);
2386 SDValue OffV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV,
2387 DAG.getConstant(ElemWidth, dl, MVT::i32));
Krzysztof Parzyszekb1b29602018-01-04 13:56:04 +00002388 ExtV = DAG.getNode(HexagonISD::EXTRACTU, dl, ScalarTy,
2389 {VecV, WidthV, OffV});
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002390 }
2391
2392 // Cast ExtV to the requested result type.
2393 ExtV = DAG.getZExtOrTrunc(ExtV, dl, tyScalar(ResTy));
2394 ExtV = DAG.getBitcast(ResTy, ExtV);
2395 return ExtV;
2396}
2397
2398SDValue
2399HexagonTargetLowering::insertVector(SDValue VecV, SDValue ValV, SDValue IdxV,
2400 const SDLoc &dl, MVT ValTy,
2401 SelectionDAG &DAG) const {
2402 MVT VecTy = ty(VecV);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002403 if (VecTy.getVectorElementType() == MVT::i1) {
2404 MVT ValTy = ty(ValV);
2405 assert(ValTy.getVectorElementType() == MVT::i1);
2406 SDValue ValR = DAG.getNode(HexagonISD::P2D, dl, MVT::i64, ValV);
2407 unsigned VecLen = VecTy.getVectorNumElements();
2408 unsigned Scale = VecLen / ValTy.getVectorNumElements();
2409 assert(Scale > 1);
2410
2411 for (unsigned R = Scale; R > 1; R /= 2) {
2412 ValR = contractPredicate(ValR, dl, DAG);
2413 ValR = DAG.getNode(HexagonISD::COMBINE, dl, MVT::i64,
2414 DAG.getUNDEF(MVT::i32), ValR);
2415 }
2416 // The longest possible subvector is at most 32 bits, so it is always
2417 // contained in the low subregister.
2418 ValR = DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::i32, ValR);
2419
2420 unsigned ValBytes = 64 / Scale;
2421 SDValue Width = DAG.getConstant(ValBytes*8, dl, MVT::i32);
2422 SDValue Idx = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV,
2423 DAG.getConstant(8, dl, MVT::i32));
2424 SDValue VecR = DAG.getNode(HexagonISD::P2D, dl, MVT::i64, VecV);
2425 SDValue Ins = DAG.getNode(HexagonISD::INSERT, dl, MVT::i32,
2426 {VecR, ValR, Width, Idx});
2427 return DAG.getNode(HexagonISD::D2P, dl, VecTy, Ins);
2428 }
2429
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002430 unsigned VecWidth = VecTy.getSizeInBits();
2431 unsigned ValWidth = ValTy.getSizeInBits();
2432 assert(VecWidth == 32 || VecWidth == 64);
2433 assert((VecWidth % ValWidth) == 0);
2434
2435 // Cast everything to scalar integer types.
2436 MVT ScalarTy = MVT::getIntegerVT(VecWidth);
2437 // The actual type of ValV may be different than ValTy (which is related
2438 // to the vector type).
2439 unsigned VW = ty(ValV).getSizeInBits();
2440 ValV = DAG.getBitcast(MVT::getIntegerVT(VW), ValV);
2441 VecV = DAG.getBitcast(ScalarTy, VecV);
2442 if (VW != VecWidth)
2443 ValV = DAG.getAnyExtOrTrunc(ValV, dl, ScalarTy);
2444
2445 SDValue WidthV = DAG.getConstant(ValWidth, dl, MVT::i32);
2446 SDValue InsV;
2447
2448 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(IdxV)) {
2449 unsigned W = C->getZExtValue() * ValWidth;
2450 SDValue OffV = DAG.getConstant(W, dl, MVT::i32);
2451 InsV = DAG.getNode(HexagonISD::INSERT, dl, ScalarTy,
2452 {VecV, ValV, WidthV, OffV});
2453 } else {
2454 if (ty(IdxV) != MVT::i32)
2455 IdxV = DAG.getZExtOrTrunc(IdxV, dl, MVT::i32);
2456 SDValue OffV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV, WidthV);
Krzysztof Parzyszekb1b29602018-01-04 13:56:04 +00002457 InsV = DAG.getNode(HexagonISD::INSERT, dl, ScalarTy,
2458 {VecV, ValV, WidthV, OffV});
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002459 }
2460
2461 return DAG.getNode(ISD::BITCAST, dl, VecTy, InsV);
2462}
2463
2464SDValue
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002465HexagonTargetLowering::expandPredicate(SDValue Vec32, const SDLoc &dl,
2466 SelectionDAG &DAG) const {
2467 assert(ty(Vec32).getSizeInBits() == 32);
2468 if (isUndef(Vec32))
2469 return DAG.getUNDEF(MVT::i64);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002470 return getInstr(Hexagon::S2_vsxtbh, dl, MVT::i64, {Vec32}, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002471}
2472
2473SDValue
2474HexagonTargetLowering::contractPredicate(SDValue Vec64, const SDLoc &dl,
2475 SelectionDAG &DAG) const {
2476 assert(ty(Vec64).getSizeInBits() == 64);
2477 if (isUndef(Vec64))
2478 return DAG.getUNDEF(MVT::i32);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002479 return getInstr(Hexagon::S2_vtrunehb, dl, MVT::i32, {Vec64}, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002480}
2481
2482SDValue
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002483HexagonTargetLowering::getZero(const SDLoc &dl, MVT Ty, SelectionDAG &DAG)
2484 const {
2485 if (Ty.isVector()) {
2486 assert(Ty.isInteger() && "Only integer vectors are supported here");
2487 unsigned W = Ty.getSizeInBits();
2488 if (W <= 64)
2489 return DAG.getBitcast(Ty, DAG.getConstant(0, dl, MVT::getIntegerVT(W)));
2490 return DAG.getNode(HexagonISD::VZERO, dl, Ty);
2491 }
2492
2493 if (Ty.isInteger())
2494 return DAG.getConstant(0, dl, Ty);
2495 if (Ty.isFloatingPoint())
2496 return DAG.getConstantFP(0.0, dl, Ty);
2497 llvm_unreachable("Invalid type for zero");
2498}
2499
2500SDValue
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002501HexagonTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002502 MVT VecTy = ty(Op);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002503 unsigned BW = VecTy.getSizeInBits();
2504 const SDLoc &dl(Op);
2505 SmallVector<SDValue,8> Ops;
2506 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i)
2507 Ops.push_back(Op.getOperand(i));
2508
2509 if (BW == 32)
2510 return buildVector32(Ops, dl, VecTy, DAG);
2511 if (BW == 64)
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002512 return buildVector64(Ops, dl, VecTy, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002513
2514 if (VecTy == MVT::v8i1 || VecTy == MVT::v4i1 || VecTy == MVT::v2i1) {
2515 // For each i1 element in the resulting predicate register, put 1
2516 // shifted by the index of the element into a general-purpose register,
2517 // then or them together and transfer it back into a predicate register.
2518 SDValue Rs[8];
2519 SDValue Z = getZero(dl, MVT::i32, DAG);
2520 // Always produce 8 bits, repeat inputs if necessary.
2521 unsigned Rep = 8 / VecTy.getVectorNumElements();
2522 for (unsigned i = 0; i != 8; ++i) {
Simon Pilgrimc1e22902018-01-23 21:22:16 +00002523 SDValue S = DAG.getConstant(1ull << i, dl, MVT::i32);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002524 Rs[i] = DAG.getSelect(dl, MVT::i32, Ops[i/Rep], S, Z);
2525 }
2526 for (ArrayRef<SDValue> A(Rs); A.size() != 1; A = A.drop_back(A.size()/2)) {
2527 for (unsigned i = 0, e = A.size()/2; i != e; ++i)
2528 Rs[i] = DAG.getNode(ISD::OR, dl, MVT::i32, Rs[2*i], Rs[2*i+1]);
2529 }
2530 // Move the value directly to a predicate register.
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002531 return getInstr(Hexagon::C2_tfrrp, dl, VecTy, {Rs[0]}, DAG);
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002532 }
2533
2534 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002535}
2536
2537SDValue
2538HexagonTargetLowering::LowerCONCAT_VECTORS(SDValue Op,
2539 SelectionDAG &DAG) const {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002540 MVT VecTy = ty(Op);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002541 const SDLoc &dl(Op);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002542 if (VecTy.getSizeInBits() == 64) {
2543 assert(Op.getNumOperands() == 2);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002544 return DAG.getNode(HexagonISD::COMBINE, dl, VecTy, Op.getOperand(1),
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002545 Op.getOperand(0));
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002546 }
2547
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002548 MVT ElemTy = VecTy.getVectorElementType();
2549 if (ElemTy == MVT::i1) {
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002550 assert(VecTy == MVT::v2i1 || VecTy == MVT::v4i1 || VecTy == MVT::v8i1);
2551 MVT OpTy = ty(Op.getOperand(0));
2552 // Scale is how many times the operands need to be contracted to match
2553 // the representation in the target register.
2554 unsigned Scale = VecTy.getVectorNumElements() / OpTy.getVectorNumElements();
2555 assert(Scale == Op.getNumOperands() && Scale > 1);
2556
2557 // First, convert all bool vectors to integers, then generate pairwise
2558 // inserts to form values of doubled length. Up until there are only
2559 // two values left to concatenate, all of these values will fit in a
2560 // 32-bit integer, so keep them as i32 to use 32-bit inserts.
2561 SmallVector<SDValue,4> Words[2];
2562 unsigned IdxW = 0;
2563
2564 for (SDValue P : Op.getNode()->op_values()) {
2565 SDValue W = DAG.getNode(HexagonISD::P2D, dl, MVT::i64, P);
2566 for (unsigned R = Scale; R > 1; R /= 2) {
2567 W = contractPredicate(W, dl, DAG);
2568 W = DAG.getNode(HexagonISD::COMBINE, dl, MVT::i64,
2569 DAG.getUNDEF(MVT::i32), W);
2570 }
2571 W = DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::i32, W);
2572 Words[IdxW].push_back(W);
2573 }
2574
2575 while (Scale > 2) {
2576 SDValue WidthV = DAG.getConstant(64 / Scale, dl, MVT::i32);
2577 Words[IdxW ^ 1].clear();
2578
2579 for (unsigned i = 0, e = Words[IdxW].size(); i != e; i += 2) {
2580 SDValue W0 = Words[IdxW][i], W1 = Words[IdxW][i+1];
2581 // Insert W1 into W0 right next to the significant bits of W0.
2582 SDValue T = DAG.getNode(HexagonISD::INSERT, dl, MVT::i32,
2583 {W0, W1, WidthV, WidthV});
2584 Words[IdxW ^ 1].push_back(T);
2585 }
2586 IdxW ^= 1;
2587 Scale /= 2;
2588 }
2589
2590 // Another sanity check. At this point there should only be two words
2591 // left, and Scale should be 2.
2592 assert(Scale == 2 && Words[IdxW].size() == 2);
2593
2594 SDValue WW = DAG.getNode(HexagonISD::COMBINE, dl, MVT::i64,
2595 Words[IdxW][1], Words[IdxW][0]);
2596 return DAG.getNode(HexagonISD::D2P, dl, VecTy, WW);
2597 }
2598
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002599 return SDValue();
2600}
2601
2602SDValue
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002603HexagonTargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
2604 SelectionDAG &DAG) const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002605 SDValue Vec = Op.getOperand(0);
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002606 MVT ElemTy = ty(Vec).getVectorElementType();
2607 return extractVector(Vec, Op.getOperand(1), SDLoc(Op), ElemTy, ty(Op), DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002608}
2609
2610SDValue
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002611HexagonTargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op,
2612 SelectionDAG &DAG) const {
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00002613 return extractVector(Op.getOperand(0), Op.getOperand(1), SDLoc(Op),
2614 ty(Op), ty(Op), DAG);
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002615}
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002616
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002617SDValue
2618HexagonTargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op,
2619 SelectionDAG &DAG) const {
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002620 return insertVector(Op.getOperand(0), Op.getOperand(1), Op.getOperand(2),
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00002621 SDLoc(Op), ty(Op).getVectorElementType(), DAG);
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002622}
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002623
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002624SDValue
2625HexagonTargetLowering::LowerINSERT_SUBVECTOR(SDValue Op,
2626 SelectionDAG &DAG) const {
2627 SDValue ValV = Op.getOperand(1);
2628 return insertVector(Op.getOperand(0), ValV, Op.getOperand(2),
2629 SDLoc(Op), ty(ValV), DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002630}
2631
Tim Northovera4415852013-08-06 09:12:35 +00002632bool
2633HexagonTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
2634 // Assuming the caller does not have either a signext or zeroext modifier, and
2635 // only one value is accepted, any reasonable truncation is allowed.
2636 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
2637 return false;
2638
2639 // FIXME: in principle up to 64-bit could be made safe, but it would be very
2640 // fragile at the moment: any support for multiple value returns would be
2641 // liable to disallow tail calls involving i64 -> iN truncation in many cases.
2642 return Ty1->getPrimitiveSizeInBits() <= 32;
2643}
2644
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002645SDValue
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002646HexagonTargetLowering::LowerUnalignedLoad(SDValue Op, SelectionDAG &DAG)
2647 const {
2648 LoadSDNode *LN = cast<LoadSDNode>(Op.getNode());
2649 unsigned HaveAlign = LN->getAlignment();
2650 MVT LoadTy = ty(Op);
2651 unsigned NeedAlign = Subtarget.getTypeAlignment(LoadTy);
2652 if (HaveAlign >= NeedAlign)
2653 return Op;
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002654
2655 const SDLoc &dl(Op);
2656 const DataLayout &DL = DAG.getDataLayout();
2657 LLVMContext &Ctx = *DAG.getContext();
2658 unsigned AS = LN->getAddressSpace();
2659
2660 // If the load aligning is disabled or the load can be broken up into two
2661 // smaller legal loads, do the default (target-independent) expansion.
2662 bool DoDefault = false;
Krzysztof Parzyszek480ab2b2018-03-08 18:15:13 +00002663 // Handle it in the default way if this is an indexed load.
2664 if (!LN->isUnindexed())
2665 DoDefault = true;
2666
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002667 if (!AlignLoads) {
2668 if (allowsMemoryAccess(Ctx, DL, LN->getMemoryVT(), AS, HaveAlign))
2669 return Op;
2670 DoDefault = true;
2671 }
2672 if (!DoDefault && 2*HaveAlign == NeedAlign) {
2673 // The PartTy is the equivalent of "getLoadableTypeOfSize(HaveAlign)".
2674 MVT PartTy = HaveAlign <= 8 ? MVT::getIntegerVT(8*HaveAlign)
2675 : MVT::getVectorVT(MVT::i8, HaveAlign);
2676 DoDefault = allowsMemoryAccess(Ctx, DL, PartTy, AS, HaveAlign);
2677 }
2678 if (DoDefault) {
2679 std::pair<SDValue, SDValue> P = expandUnalignedLoad(LN, DAG);
2680 return DAG.getMergeValues({P.first, P.second}, dl);
2681 }
2682
2683 // The code below generates two loads, both aligned as NeedAlign, and
2684 // with the distance of NeedAlign between them. For that to cover the
2685 // bits that need to be loaded (and without overlapping), the size of
2686 // the loads should be equal to NeedAlign. This is true for all loadable
2687 // types, but add an assertion in case something changes in the future.
2688 assert(LoadTy.getSizeInBits() == 8*NeedAlign);
2689
2690 unsigned LoadLen = NeedAlign;
2691 SDValue Base = LN->getBasePtr();
2692 SDValue Chain = LN->getChain();
2693 auto BO = getBaseAndOffset(Base);
2694 unsigned BaseOpc = BO.first.getOpcode();
2695 if (BaseOpc == HexagonISD::VALIGNADDR && BO.second % LoadLen == 0)
2696 return Op;
2697
2698 if (BO.second % LoadLen != 0) {
2699 BO.first = DAG.getNode(ISD::ADD, dl, MVT::i32, BO.first,
2700 DAG.getConstant(BO.second % LoadLen, dl, MVT::i32));
2701 BO.second -= BO.second % LoadLen;
2702 }
2703 SDValue BaseNoOff = (BaseOpc != HexagonISD::VALIGNADDR)
2704 ? DAG.getNode(HexagonISD::VALIGNADDR, dl, MVT::i32, BO.first,
2705 DAG.getConstant(NeedAlign, dl, MVT::i32))
2706 : BO.first;
2707 SDValue Base0 = DAG.getMemBasePlusOffset(BaseNoOff, BO.second, dl);
2708 SDValue Base1 = DAG.getMemBasePlusOffset(BaseNoOff, BO.second+LoadLen, dl);
2709
2710 MachineMemOperand *WideMMO = nullptr;
2711 if (MachineMemOperand *MMO = LN->getMemOperand()) {
2712 MachineFunction &MF = DAG.getMachineFunction();
2713 WideMMO = MF.getMachineMemOperand(MMO->getPointerInfo(), MMO->getFlags(),
2714 2*LoadLen, LoadLen, MMO->getAAInfo(), MMO->getRanges(),
2715 MMO->getSyncScopeID(), MMO->getOrdering(),
2716 MMO->getFailureOrdering());
2717 }
2718
2719 SDValue Load0 = DAG.getLoad(LoadTy, dl, Chain, Base0, WideMMO);
2720 SDValue Load1 = DAG.getLoad(LoadTy, dl, Chain, Base1, WideMMO);
2721
2722 SDValue Aligned = DAG.getNode(HexagonISD::VALIGN, dl, LoadTy,
2723 {Load1, Load0, BaseNoOff.getOperand(0)});
2724 SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2725 Load0.getValue(1), Load1.getValue(1));
2726 SDValue M = DAG.getMergeValues({Aligned, NewChain}, dl);
2727 return M;
2728}
2729
2730SDValue
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002731HexagonTargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
2732 SDValue Chain = Op.getOperand(0);
2733 SDValue Offset = Op.getOperand(1);
2734 SDValue Handler = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002735 SDLoc dl(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00002736 auto PtrVT = getPointerTy(DAG.getDataLayout());
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002737
2738 // Mark function as containing a call to EH_RETURN.
2739 HexagonMachineFunctionInfo *FuncInfo =
2740 DAG.getMachineFunction().getInfo<HexagonMachineFunctionInfo>();
2741 FuncInfo->setHasEHReturn();
2742
2743 unsigned OffsetReg = Hexagon::R28;
2744
Mehdi Amini44ede332015-07-09 02:09:04 +00002745 SDValue StoreAddr =
2746 DAG.getNode(ISD::ADD, dl, PtrVT, DAG.getRegister(Hexagon::R30, PtrVT),
2747 DAG.getIntPtrConstant(4, dl));
Justin Lebar9c375812016-07-15 18:27:10 +00002748 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo());
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002749 Chain = DAG.getCopyToReg(Chain, dl, OffsetReg, Offset);
2750
2751 // Not needed we already use it as explict input to EH_RETURN.
2752 // MF.getRegInfo().addLiveOut(OffsetReg);
2753
2754 return DAG.getNode(HexagonISD::EH_RETURN, dl, MVT::Other, Chain);
2755}
2756
2757SDValue
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002758HexagonTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002759 unsigned Opc = Op.getOpcode();
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00002760
2761 // Handle INLINEASM first.
2762 if (Opc == ISD::INLINEASM)
2763 return LowerINLINEASM(Op, DAG);
2764
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002765 if (isHvxOperation(Op)) {
2766 // If HVX lowering returns nothing, try the default lowering.
2767 if (SDValue V = LowerHvxOperation(Op, DAG))
2768 return V;
2769 }
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00002770
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002771 switch (Opc) {
2772 default:
2773#ifndef NDEBUG
2774 Op.getNode()->dumpr(&DAG);
2775 if (Opc > HexagonISD::OP_BEGIN && Opc < HexagonISD::OP_END)
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002776 errs() << "Error: check for a non-legal type in this operation\n";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002777#endif
2778 llvm_unreachable("Should not custom lower this!");
2779 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002780 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
2781 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
2782 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
2783 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002784 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
2785 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002786 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002787 case ISD::LOAD: return LowerUnalignedLoad(Op, DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002788 case ISD::SRA:
2789 case ISD::SHL:
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002790 case ISD::SRL: return LowerVECTOR_SHIFT(Op, DAG);
2791 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002792 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002793 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002794 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
2795 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00002796 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002797 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
2798 case ISD::GlobalAddress: return LowerGLOBALADDRESS(Op, DAG);
2799 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002800 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002801 case ISD::VASTART: return LowerVASTART(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002802 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
2803 case ISD::SETCC: return LowerSETCC(Op, DAG);
2804 case ISD::VSELECT: return LowerVSELECT(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002805 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00002806 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00002807 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG);
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +00002808 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Krzysztof Parzyszek9eb085e2018-01-31 20:48:11 +00002809 break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002810 }
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00002811
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002812 return SDValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002813}
2814
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002815void
2816HexagonTargetLowering::ReplaceNodeResults(SDNode *N,
2817 SmallVectorImpl<SDValue> &Results,
2818 SelectionDAG &DAG) const {
2819 const SDLoc &dl(N);
2820 switch (N->getOpcode()) {
2821 case ISD::SRL:
2822 case ISD::SRA:
2823 case ISD::SHL:
2824 return;
2825 case ISD::BITCAST:
2826 // Handle a bitcast from v8i1 to i8.
2827 if (N->getValueType(0) == MVT::i8) {
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002828 SDValue P = getInstr(Hexagon::C2_tfrpr, dl, MVT::i32,
2829 N->getOperand(0), DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002830 Results.push_back(P);
2831 }
2832 break;
2833 }
2834}
2835
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002836/// Returns relocation base for the given PIC jumptable.
2837SDValue
2838HexagonTargetLowering::getPICJumpTableRelocBase(SDValue Table,
2839 SelectionDAG &DAG) const {
2840 int Idx = cast<JumpTableSDNode>(Table)->getIndex();
2841 EVT VT = Table.getValueType();
2842 SDValue T = DAG.getTargetJumpTable(Idx, VT, HexagonII::MO_PCREL);
2843 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Table), VT, T);
2844}
2845
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002846//===----------------------------------------------------------------------===//
2847// Inline Assembly Support
2848//===----------------------------------------------------------------------===//
2849
Krzysztof Parzyszekca3b5322016-05-18 14:34:51 +00002850TargetLowering::ConstraintType
2851HexagonTargetLowering::getConstraintType(StringRef Constraint) const {
2852 if (Constraint.size() == 1) {
2853 switch (Constraint[0]) {
2854 case 'q':
2855 case 'v':
2856 if (Subtarget.useHVXOps())
Krzysztof Parzyszek3ad0d012017-07-21 17:51:27 +00002857 return C_RegisterClass;
2858 break;
2859 case 'a':
2860 return C_RegisterClass;
2861 default:
Krzysztof Parzyszekca3b5322016-05-18 14:34:51 +00002862 break;
2863 }
2864 }
2865 return TargetLowering::getConstraintType(Constraint);
2866}
2867
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002868std::pair<unsigned, const TargetRegisterClass*>
Eric Christopher11e4df72015-02-26 22:38:43 +00002869HexagonTargetLowering::getRegForInlineAsmConstraint(
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00002870 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002871
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002872 if (Constraint.size() == 1) {
2873 switch (Constraint[0]) {
2874 case 'r': // R0-R31
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002875 switch (VT.SimpleTy) {
2876 default:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002877 return {0u, nullptr};
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002878 case MVT::i1:
2879 case MVT::i8:
2880 case MVT::i16:
2881 case MVT::i32:
2882 case MVT::f32:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002883 return {0u, &Hexagon::IntRegsRegClass};
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002884 case MVT::i64:
2885 case MVT::f64:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002886 return {0u, &Hexagon::DoubleRegsRegClass};
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002887 }
Krzysztof Parzyszek3ad0d012017-07-21 17:51:27 +00002888 break;
2889 case 'a': // M0-M1
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002890 if (VT != MVT::i32)
2891 return {0u, nullptr};
2892 return {0u, &Hexagon::ModRegsRegClass};
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002893 case 'q': // q0-q3
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002894 switch (VT.getSizeInBits()) {
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002895 default:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002896 return {0u, nullptr};
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002897 case 512:
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002898 case 1024:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002899 return {0u, &Hexagon::HvxQRRegClass};
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002900 }
Krzysztof Parzyszek3ad0d012017-07-21 17:51:27 +00002901 break;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002902 case 'v': // V0-V31
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002903 switch (VT.getSizeInBits()) {
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002904 default:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002905 return {0u, nullptr};
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002906 case 512:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002907 return {0u, &Hexagon::HvxVRRegClass};
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002908 case 1024:
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +00002909 if (Subtarget.hasV60TOps() && Subtarget.useHVX128BOps())
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002910 return {0u, &Hexagon::HvxVRRegClass};
2911 return {0u, &Hexagon::HvxWRRegClass};
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002912 case 2048:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002913 return {0u, &Hexagon::HvxWRRegClass};
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002914 }
Krzysztof Parzyszek3ad0d012017-07-21 17:51:27 +00002915 break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002916 default:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002917 return {0u, nullptr};
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002918 }
2919 }
2920
Eric Christopher11e4df72015-02-26 22:38:43 +00002921 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002922}
2923
Sirish Pande69295b82012-05-10 20:20:25 +00002924/// isFPImmLegal - Returns true if the target can instruction select the
2925/// specified FP immediate natively. If false, the legalizer will
2926/// materialize the FP immediate as a load from a constant pool.
2927bool HexagonTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002928 return Subtarget.hasV5TOps();
Sirish Pande69295b82012-05-10 20:20:25 +00002929}
2930
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002931/// isLegalAddressingMode - Return true if the addressing mode represented by
2932/// AM is legal for this target, for a load/store of the specified type.
Mehdi Amini0cdec1e2015-07-09 02:09:40 +00002933bool HexagonTargetLowering::isLegalAddressingMode(const DataLayout &DL,
2934 const AddrMode &AM, Type *Ty,
Jonas Paulsson024e3192017-07-21 11:59:37 +00002935 unsigned AS, Instruction *I) const {
Krzysztof Parzyszeked4e7822016-08-03 15:06:18 +00002936 if (Ty->isSized()) {
2937 // When LSR detects uses of the same base address to access different
2938 // types (e.g. unions), it will assume a conservative type for these
2939 // uses:
2940 // LSR Use: Kind=Address of void in addrspace(4294967295), ...
2941 // The type Ty passed here would then be "void". Skip the alignment
2942 // checks, but do not return false right away, since that confuses
2943 // LSR into crashing.
2944 unsigned A = DL.getABITypeAlignment(Ty);
2945 // The base offset must be a multiple of the alignment.
2946 if ((AM.BaseOffs % A) != 0)
2947 return false;
2948 // The shifted offset must fit in 11 bits.
2949 if (!isInt<11>(AM.BaseOffs >> Log2_32(A)))
2950 return false;
2951 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002952
2953 // No global is ever allowed as a base.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002954 if (AM.BaseGV)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002955 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002956
2957 int Scale = AM.Scale;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00002958 if (Scale < 0)
2959 Scale = -Scale;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002960 switch (Scale) {
2961 case 0: // No scale reg, "r+i", "r", or just "i".
2962 break;
2963 default: // No scaled addressing mode.
2964 return false;
2965 }
2966 return true;
2967}
2968
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002969/// Return true if folding a constant offset with the given GlobalAddress is
2970/// legal. It is frequently not legal in PIC relocation models.
2971bool HexagonTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA)
2972 const {
2973 return HTM.getRelocationModel() == Reloc::Static;
2974}
2975
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002976/// isLegalICmpImmediate - Return true if the specified immediate is legal
2977/// icmp immediate, that is the target has icmp instructions which can compare
2978/// a register against the immediate without having to materialize the
2979/// immediate into a register.
2980bool HexagonTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
2981 return Imm >= -512 && Imm <= 511;
2982}
2983
2984/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2985/// for tail call optimization. Targets which want to do tail call
2986/// optimization should implement this function.
2987bool HexagonTargetLowering::IsEligibleForTailCallOptimization(
2988 SDValue Callee,
2989 CallingConv::ID CalleeCC,
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00002990 bool IsVarArg,
2991 bool IsCalleeStructRet,
2992 bool IsCallerStructRet,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002993 const SmallVectorImpl<ISD::OutputArg> &Outs,
2994 const SmallVectorImpl<SDValue> &OutVals,
2995 const SmallVectorImpl<ISD::InputArg> &Ins,
2996 SelectionDAG& DAG) const {
Matthias Braunf1caa282017-12-15 22:22:58 +00002997 const Function &CallerF = DAG.getMachineFunction().getFunction();
2998 CallingConv::ID CallerCC = CallerF.getCallingConv();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002999 bool CCMatch = CallerCC == CalleeCC;
3000
3001 // ***************************************************************************
3002 // Look for obvious safe cases to perform tail call optimization that do not
3003 // require ABI changes.
3004 // ***************************************************************************
3005
3006 // If this is a tail call via a function pointer, then don't do it!
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00003007 if (!isa<GlobalAddressSDNode>(Callee) &&
3008 !isa<ExternalSymbolSDNode>(Callee)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003009 return false;
3010 }
3011
Krzysztof Parzyszek0ba97542016-08-19 15:02:18 +00003012 // Do not optimize if the calling conventions do not match and the conventions
3013 // used are not C or Fast.
3014 if (!CCMatch) {
3015 bool R = (CallerCC == CallingConv::C || CallerCC == CallingConv::Fast);
3016 bool E = (CalleeCC == CallingConv::C || CalleeCC == CallingConv::Fast);
3017 // If R & E, then ok.
3018 if (!R || !E)
3019 return false;
3020 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003021
3022 // Do not tail call optimize vararg calls.
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00003023 if (IsVarArg)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003024 return false;
3025
3026 // Also avoid tail call optimization if either caller or callee uses struct
3027 // return semantics.
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00003028 if (IsCalleeStructRet || IsCallerStructRet)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003029 return false;
3030
3031 // In addition to the cases above, we also disable Tail Call Optimization if
3032 // the calling convention code that at least one outgoing argument needs to
3033 // go on the stack. We cannot check that here because at this point that
3034 // information is not available.
3035 return true;
3036}
Colin LeMahieu025f8602014-12-08 21:19:18 +00003037
Krzysztof Parzyszek3e409e12016-08-02 18:34:31 +00003038/// Returns the target specific optimal type for load and store operations as
3039/// a result of memset, memcpy, and memmove lowering.
3040///
3041/// If DstAlign is zero that means it's safe to destination alignment can
3042/// satisfy any constraint. Similarly if SrcAlign is zero it means there isn't
3043/// a need to check it against alignment requirement, probably because the
3044/// source does not need to be loaded. If 'IsMemset' is true, that means it's
3045/// expanding a memset. If 'ZeroMemset' is true, that means it's a memset of
3046/// zero. 'MemcpyStrSrc' indicates whether the memcpy source is constant so it
3047/// does not need to be loaded. It returns EVT::Other if the type should be
3048/// determined using generic target-independent logic.
3049EVT HexagonTargetLowering::getOptimalMemOpType(uint64_t Size,
3050 unsigned DstAlign, unsigned SrcAlign, bool IsMemset, bool ZeroMemset,
3051 bool MemcpyStrSrc, MachineFunction &MF) const {
3052
3053 auto Aligned = [](unsigned GivenA, unsigned MinA) -> bool {
3054 return (GivenA % MinA) == 0;
3055 };
3056
3057 if (Size >= 8 && Aligned(DstAlign, 8) && (IsMemset || Aligned(SrcAlign, 8)))
3058 return MVT::i64;
3059 if (Size >= 4 && Aligned(DstAlign, 4) && (IsMemset || Aligned(SrcAlign, 4)))
3060 return MVT::i32;
3061 if (Size >= 2 && Aligned(DstAlign, 2) && (IsMemset || Aligned(SrcAlign, 2)))
3062 return MVT::i16;
3063
3064 return MVT::Other;
3065}
3066
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +00003067bool HexagonTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
3068 unsigned AS, unsigned Align, bool *Fast) const {
3069 if (Fast)
3070 *Fast = false;
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00003071 return Subtarget.isHVXVectorType(VT.getSimpleVT());
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +00003072}
3073
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003074std::pair<const TargetRegisterClass*, uint8_t>
3075HexagonTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI,
3076 MVT VT) const {
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00003077 if (Subtarget.isHVXVectorType(VT, true)) {
3078 unsigned BitWidth = VT.getSizeInBits();
3079 unsigned VecWidth = Subtarget.getVectorLength() * 8;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003080
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00003081 if (VT.getVectorElementType() == MVT::i1)
3082 return std::make_pair(&Hexagon::HvxQRRegClass, 1);
3083 if (BitWidth == VecWidth)
3084 return std::make_pair(&Hexagon::HvxVRRegClass, 1);
3085 assert(BitWidth == 2 * VecWidth);
3086 return std::make_pair(&Hexagon::HvxWRRegClass, 1);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003087 }
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00003088
3089 return TargetLowering::findRepresentativeClass(TRI, VT);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003090}
3091
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003092Value *HexagonTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
3093 AtomicOrdering Ord) const {
3094 BasicBlock *BB = Builder.GetInsertBlock();
3095 Module *M = BB->getParent()->getParent();
3096 Type *Ty = cast<PointerType>(Addr->getType())->getElementType();
3097 unsigned SZ = Ty->getPrimitiveSizeInBits();
3098 assert((SZ == 32 || SZ == 64) && "Only 32/64-bit atomic loads supported");
3099 Intrinsic::ID IntID = (SZ == 32) ? Intrinsic::hexagon_L2_loadw_locked
3100 : Intrinsic::hexagon_L4_loadd_locked;
3101 Value *Fn = Intrinsic::getDeclaration(M, IntID);
3102 return Builder.CreateCall(Fn, Addr, "larx");
3103}
3104
3105/// Perform a store-conditional operation to Addr. Return the status of the
3106/// store. This should be 0 if the store succeeded, non-zero otherwise.
3107Value *HexagonTargetLowering::emitStoreConditional(IRBuilder<> &Builder,
3108 Value *Val, Value *Addr, AtomicOrdering Ord) const {
3109 BasicBlock *BB = Builder.GetInsertBlock();
3110 Module *M = BB->getParent()->getParent();
3111 Type *Ty = Val->getType();
3112 unsigned SZ = Ty->getPrimitiveSizeInBits();
3113 assert((SZ == 32 || SZ == 64) && "Only 32/64-bit atomic stores supported");
3114 Intrinsic::ID IntID = (SZ == 32) ? Intrinsic::hexagon_S2_storew_locked
3115 : Intrinsic::hexagon_S4_stored_locked;
3116 Value *Fn = Intrinsic::getDeclaration(M, IntID);
3117 Value *Call = Builder.CreateCall(Fn, {Addr, Val}, "stcx");
3118 Value *Cmp = Builder.CreateICmpEQ(Call, Builder.getInt32(0), "");
3119 Value *Ext = Builder.CreateZExt(Cmp, Type::getInt32Ty(M->getContext()));
3120 return Ext;
3121}
3122
Ahmed Bougacha52468672015-09-11 17:08:28 +00003123TargetLowering::AtomicExpansionKind
3124HexagonTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const {
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003125 // Do not expand loads and stores that don't exceed 64 bits.
Ahmed Bougacha52468672015-09-11 17:08:28 +00003126 return LI->getType()->getPrimitiveSizeInBits() > 64
Tim Northoverf520eff2015-12-02 18:12:57 +00003127 ? AtomicExpansionKind::LLOnly
Ahmed Bougacha52468672015-09-11 17:08:28 +00003128 : AtomicExpansionKind::None;
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003129}
3130
3131bool HexagonTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const {
3132 // Do not expand loads and stores that don't exceed 64 bits.
3133 return SI->getValueOperand()->getType()->getPrimitiveSizeInBits() > 64;
3134}
Krzysztof Parzyszekf228c952016-06-22 16:07:10 +00003135
3136bool HexagonTargetLowering::shouldExpandAtomicCmpXchgInIR(
3137 AtomicCmpXchgInst *AI) const {
3138 const DataLayout &DL = AI->getModule()->getDataLayout();
3139 unsigned Size = DL.getTypeStoreSize(AI->getCompareOperand()->getType());
3140 return Size >= 4 && Size <= 8;
3141}