blob: fef5a98cdb00055eb1d8c5e4feff0f5c4167e468 [file] [log] [blame]
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001//===-- HexagonISelLowering.cpp - Hexagon DAG Lowering Implementation -----===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Tony Linthicum1213a7a2011-12-12 21:14:40 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the interfaces that Hexagon uses to lower LLVM code
10// into a selection DAG.
11//
12//===----------------------------------------------------------------------===//
13
14#include "HexagonISelLowering.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000015#include "Hexagon.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000016#include "HexagonMachineFunctionInfo.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000017#include "HexagonRegisterInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000018#include "HexagonSubtarget.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "HexagonTargetMachine.h"
20#include "HexagonTargetObjectFile.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000021#include "llvm/ADT/APInt.h"
22#include "llvm/ADT/ArrayRef.h"
23#include "llvm/ADT/SmallVector.h"
Sid Manning9ad0f022018-09-07 13:36:21 +000024#include "llvm/ADT/StringSwitch.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
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000154SDValue
155HexagonTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG)
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000156 const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000157 return SDValue();
158}
159
160/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
161/// by "Src" to address "Dst" of size "Size". Alignment information is
162/// specified by the specific parameter attribute. The copy will be passed as
163/// a byval function parameter. Sometimes what we are copying is the end of a
164/// larger object, the part that does not fit in registers.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000165static SDValue CreateCopyOfByValArgument(SDValue Src, SDValue Dst,
166 SDValue Chain, ISD::ArgFlagsTy Flags,
167 SelectionDAG &DAG, const SDLoc &dl) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000168 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000169 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
170 /*isVolatile=*/false, /*AlwaysInline=*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +0000171 /*isTailCall=*/false,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000172 MachinePointerInfo(), MachinePointerInfo());
173}
174
Krzysztof Parzyszek56199522017-04-13 15:05:51 +0000175bool
176HexagonTargetLowering::CanLowerReturn(
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000177 CallingConv::ID CallConv, MachineFunction &MF, bool IsVarArg,
Krzysztof Parzyszek56199522017-04-13 15:05:51 +0000178 const SmallVectorImpl<ISD::OutputArg> &Outs,
179 LLVMContext &Context) const {
180 SmallVector<CCValAssign, 16> RVLocs;
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000181 CCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
182
183 if (MF.getSubtarget<HexagonSubtarget>().useHVXOps())
184 return CCInfo.CheckReturn(Outs, RetCC_Hexagon_HVX);
Krzysztof Parzyszek56199522017-04-13 15:05:51 +0000185 return CCInfo.CheckReturn(Outs, RetCC_Hexagon);
186}
187
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000188// LowerReturn - Lower ISD::RET. If a struct is larger than 8 bytes and is
189// passed by value, the function prototype is modified to return void and
190// the value is stored in memory pointed by a pointer passed by caller.
191SDValue
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000192HexagonTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000193 bool IsVarArg,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000194 const SmallVectorImpl<ISD::OutputArg> &Outs,
195 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000196 const SDLoc &dl, SelectionDAG &DAG) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000197 // CCValAssign - represent the assignment of the return value to locations.
198 SmallVector<CCValAssign, 16> RVLocs;
199
200 // CCState - Info about the registers and stack slot.
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000201 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
Eric Christopherb5217502014-08-06 18:45:26 +0000202 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000203
204 // Analyze return values of ISD::RET
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000205 if (Subtarget.useHVXOps())
206 CCInfo.AnalyzeReturn(Outs, RetCC_Hexagon_HVX);
207 else
208 CCInfo.AnalyzeReturn(Outs, RetCC_Hexagon);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000209
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000210 SDValue Flag;
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000211 SmallVector<SDValue, 4> RetOps(1, Chain);
212
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000213 // Copy the result values into the output registers.
214 for (unsigned i = 0; i != RVLocs.size(); ++i) {
215 CCValAssign &VA = RVLocs[i];
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000216
217 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), OutVals[i], Flag);
218
219 // Guarantee that all emitted copies are stuck together with flags.
220 Flag = Chain.getValue(1);
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000221 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000222 }
223
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000224 RetOps[0] = Chain; // Update chain.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000225
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000226 // Add the flag if we have it.
227 if (Flag.getNode())
228 RetOps.push_back(Flag);
229
Craig Topper48d114b2014-04-26 18:35:24 +0000230 return DAG.getNode(HexagonISD::RET_FLAG, dl, MVT::Other, RetOps);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000231}
232
Matt Arsenault31380752017-04-18 21:16:46 +0000233bool HexagonTargetLowering::mayBeEmittedAsTailCall(const CallInst *CI) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000234 // If either no tail call or told not to tail call at all, don't.
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000235 auto Attr =
236 CI->getParent()->getParent()->getFnAttribute("disable-tail-calls");
237 if (!CI->isTailCall() || Attr.getValueAsString() == "true")
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000238 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000239
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000240 return true;
241}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000242
Sid Manning9ad0f022018-09-07 13:36:21 +0000243unsigned HexagonTargetLowering::getRegisterByName(const char* RegName, EVT VT,
244 SelectionDAG &DAG) const {
245 // Just support r19, the linux kernel uses it.
246 unsigned Reg = StringSwitch<unsigned>(RegName)
247 .Case("r19", Hexagon::R19)
248 .Default(0);
249 if (Reg)
250 return Reg;
251
252 report_fatal_error("Invalid register name global variable");
253}
254
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000255/// LowerCallResult - Lower the result values of an ISD::CALL into the
256/// appropriate copies out of appropriate physical registers. This assumes that
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000257/// Chain/Glue are the input chain/glue to use, and that TheCall is the call
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000258/// being lowered. Returns a SDNode with the same number of values as the
259/// ISD::CALL.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000260SDValue HexagonTargetLowering::LowerCallResult(
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000261 SDValue Chain, SDValue Glue, CallingConv::ID CallConv, bool IsVarArg,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000262 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
263 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals,
264 const SmallVectorImpl<SDValue> &OutVals, SDValue Callee) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000265 // Assign locations to each value returned by this call.
266 SmallVector<CCValAssign, 16> RVLocs;
267
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000268 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
Eric Christopherb5217502014-08-06 18:45:26 +0000269 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000270
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000271 if (Subtarget.useHVXOps())
272 CCInfo.AnalyzeCallResult(Ins, RetCC_Hexagon_HVX);
273 else
274 CCInfo.AnalyzeCallResult(Ins, RetCC_Hexagon);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000275
276 // Copy all of the result registers out of their specified physreg.
277 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000278 SDValue RetVal;
279 if (RVLocs[i].getValVT() == MVT::i1) {
280 // Return values of type MVT::i1 require special handling. The reason
281 // is that MVT::i1 is associated with the PredRegs register class, but
282 // values of that type are still returned in R0. Generate an explicit
283 // copy into a predicate register from R0, and treat the value of the
284 // predicate register as the call result.
285 auto &MRI = DAG.getMachineFunction().getRegInfo();
286 SDValue FR0 = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000287 MVT::i32, Glue);
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000288 // FR0 = (Value, Chain, Glue)
289 unsigned PredR = MRI.createVirtualRegister(&Hexagon::PredRegsRegClass);
290 SDValue TPR = DAG.getCopyToReg(FR0.getValue(1), dl, PredR,
291 FR0.getValue(0), FR0.getValue(2));
292 // TPR = (Chain, Glue)
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000293 // Don't glue this CopyFromReg, because it copies from a virtual
294 // register. If it is glued to the call, InstrEmitter will add it
295 // as an implicit def to the call (EmitMachineNode).
296 RetVal = DAG.getCopyFromReg(TPR.getValue(0), dl, PredR, MVT::i1);
297 Glue = TPR.getValue(1);
Krzysztof Parzyszek6f06b6e2017-10-23 19:35:25 +0000298 Chain = TPR.getValue(0);
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000299 } else {
300 RetVal = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000301 RVLocs[i].getValVT(), Glue);
302 Glue = RetVal.getValue(2);
Krzysztof Parzyszek6f06b6e2017-10-23 19:35:25 +0000303 Chain = RetVal.getValue(1);
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000304 }
305 InVals.push_back(RetVal.getValue(0));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000306 }
307
308 return Chain;
309}
310
311/// LowerCall - Functions arguments are copied from virtual regs to
312/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
313SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +0000314HexagonTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000315 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +0000316 SelectionDAG &DAG = CLI.DAG;
Craig Topperb94011f2013-07-14 04:42:23 +0000317 SDLoc &dl = CLI.DL;
318 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
319 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
320 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000321 SDValue Chain = CLI.Chain;
322 SDValue Callee = CLI.Callee;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000323 CallingConv::ID CallConv = CLI.CallConv;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000324 bool IsVarArg = CLI.IsVarArg;
325 bool DoesNotReturn = CLI.DoesNotReturn;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000326
Krzysztof Parzyszeke0d7de72018-02-15 17:20:07 +0000327 bool IsStructRet = Outs.empty() ? false : Outs[0].Flags.isSRet();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000328 MachineFunction &MF = DAG.getMachineFunction();
Krzysztof Parzyszekf67cd822017-07-11 17:11:54 +0000329 MachineFrameInfo &MFI = MF.getFrameInfo();
Mehdi Amini44ede332015-07-09 02:09:04 +0000330 auto PtrVT = getPointerTy(MF.getDataLayout());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000331
Krzysztof Parzyszeke0d7de72018-02-15 17:20:07 +0000332 unsigned NumParams = CLI.CS.getInstruction()
333 ? CLI.CS.getFunctionType()->getNumParams()
334 : 0;
335 if (GlobalAddressSDNode *GAN = dyn_cast<GlobalAddressSDNode>(Callee))
336 Callee = DAG.getTargetGlobalAddress(GAN->getGlobal(), dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000337
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000338 // Analyze operands of the call, assigning locations to each operand.
339 SmallVector<CCValAssign, 16> ArgLocs;
Krzysztof Parzyszek18e0d2a2018-02-15 15:47:53 +0000340 HexagonCCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext(),
Krzysztof Parzyszeke0d7de72018-02-15 17:20:07 +0000341 NumParams);
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000342
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000343 if (Subtarget.useHVXOps())
344 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon_HVX);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000345 else
346 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon);
347
Matthias Braunf1caa282017-12-15 22:22:58 +0000348 auto Attr = MF.getFunction().getFnAttribute("disable-tail-calls");
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000349 if (Attr.getValueAsString() == "true")
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000350 CLI.IsTailCall = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000351
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000352 if (CLI.IsTailCall) {
Matthias Braunf1caa282017-12-15 22:22:58 +0000353 bool StructAttrFlag = MF.getFunction().hasStructRetAttr();
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000354 CLI.IsTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
355 IsVarArg, IsStructRet, StructAttrFlag, Outs,
356 OutVals, Ins, DAG);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000357 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000358 CCValAssign &VA = ArgLocs[i];
359 if (VA.isMemLoc()) {
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000360 CLI.IsTailCall = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000361 break;
362 }
363 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000364 LLVM_DEBUG(dbgs() << (CLI.IsTailCall ? "Eligible for Tail Call\n"
365 : "Argument must be passed on stack. "
366 "Not eligible for Tail Call\n"));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000367 }
368 // Get a count of how many bytes are to be pushed on the stack.
369 unsigned NumBytes = CCInfo.getNextStackOffset();
370 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
371 SmallVector<SDValue, 8> MemOpChains;
372
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000373 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Mehdi Amini44ede332015-07-09 02:09:04 +0000374 SDValue StackPtr =
375 DAG.getCopyFromReg(Chain, dl, HRI.getStackRegister(), PtrVT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000376
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000377 bool NeedsArgAlign = false;
378 unsigned LargestAlignSeen = 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000379 // Walk the register/memloc assignments, inserting copies/loads.
380 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
381 CCValAssign &VA = ArgLocs[i];
382 SDValue Arg = OutVals[i];
383 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000384 // Record if we need > 8 byte alignment on an argument.
Krzysztof Parzyszekac1966e2017-11-27 18:12:16 +0000385 bool ArgAlign = Subtarget.isHVXVectorType(VA.getValVT());
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000386 NeedsArgAlign |= ArgAlign;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000387
388 // Promote the value if needed.
389 switch (VA.getLocInfo()) {
390 default:
Krzysztof Parzyszek8f6b0c82017-12-20 14:44:05 +0000391 // Loc info must be one of Full, BCvt, SExt, ZExt, or AExt.
Craig Toppere55c5562012-02-07 02:50:20 +0000392 llvm_unreachable("Unknown loc info!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000393 case CCValAssign::Full:
394 break;
Krzysztof Parzyszek8f6b0c82017-12-20 14:44:05 +0000395 case CCValAssign::BCvt:
396 Arg = DAG.getBitcast(VA.getLocVT(), Arg);
397 break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000398 case CCValAssign::SExt:
399 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
400 break;
401 case CCValAssign::ZExt:
402 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
403 break;
404 case CCValAssign::AExt:
405 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
406 break;
407 }
408
409 if (VA.isMemLoc()) {
410 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000411 SDValue MemAddr = DAG.getConstant(LocMemOffset, dl,
412 StackPtr.getValueType());
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000413 MemAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, MemAddr);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000414 if (ArgAlign)
415 LargestAlignSeen = std::max(LargestAlignSeen,
416 VA.getLocVT().getStoreSizeInBits() >> 3);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000417 if (Flags.isByVal()) {
418 // The argument is a struct passed by value. According to LLVM, "Arg"
Fangrui Song956ee792018-03-30 22:22:31 +0000419 // is a pointer.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000420 MemOpChains.push_back(CreateCopyOfByValArgument(Arg, MemAddr, Chain,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000421 Flags, DAG, dl));
422 } else {
Alex Lorenze40c8a22015-08-11 23:09:45 +0000423 MachinePointerInfo LocPI = MachinePointerInfo::getStack(
424 DAG.getMachineFunction(), LocMemOffset);
Justin Lebar9c375812016-07-15 18:27:10 +0000425 SDValue S = DAG.getStore(Chain, dl, Arg, MemAddr, LocPI);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000426 MemOpChains.push_back(S);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000427 }
428 continue;
429 }
430
431 // Arguments that can be passed on register must be kept at RegsToPass
432 // vector.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000433 if (VA.isRegLoc())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000434 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000435 }
436
Krzysztof Parzyszekd8b780d2018-06-20 13:56:09 +0000437 if (NeedsArgAlign && Subtarget.hasV60Ops()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000438 LLVM_DEBUG(dbgs() << "Function needs byte stack align due to call args\n");
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000439 unsigned VecAlign = HRI.getSpillAlignment(Hexagon::HvxVRRegClass);
440 LargestAlignSeen = std::max(LargestAlignSeen, VecAlign);
Matthias Braun941a7052016-07-28 18:40:00 +0000441 MFI.ensureMaxAlignment(LargestAlignSeen);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000442 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000443 // Transform all store nodes into one single node because all store
444 // nodes are independent of each other.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000445 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +0000446 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000447
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000448 SDValue Glue;
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000449 if (!CLI.IsTailCall) {
Serge Pavlovd526b132017-05-09 13:35:13 +0000450 Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, dl);
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000451 Glue = Chain.getValue(1);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000452 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000453
454 // Build a sequence of copy-to-reg nodes chained together with token
455 // chain and flag operands which copy the outgoing args into registers.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000456 // The Glue is necessary since all emitted instructions must be
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000457 // stuck together.
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000458 if (!CLI.IsTailCall) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000459 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
460 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000461 RegsToPass[i].second, Glue);
462 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000463 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000464 } else {
465 // For tail calls lower the arguments to the 'real' stack slot.
466 //
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000467 // Force all the incoming stack arguments to be loaded from the stack
468 // before any new outgoing arguments are stored to the stack, because the
469 // outgoing stack slots may alias the incoming argument stack slots, and
470 // the alias isn't otherwise explicit. This is slightly more conservative
471 // than necessary, because it means that each store effectively depends
472 // on every argument instead of just those arguments it would clobber.
473 //
Benjamin Kramerbde91762012-06-02 10:20:22 +0000474 // Do not flag preceding copytoreg stuff together with the following stuff.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000475 Glue = SDValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000476 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
477 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000478 RegsToPass[i].second, Glue);
479 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000480 }
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000481 Glue = SDValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000482 }
483
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000484 bool LongCalls = MF.getSubtarget<HexagonSubtarget>().useLongCalls();
485 unsigned Flags = LongCalls ? HexagonII::HMOTF_ConstExtended : 0;
486
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000487 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
488 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
489 // node so that legalize doesn't hack it.
Tobias Edler von Kochb51460c2015-12-16 17:29:37 +0000490 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000491 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, PtrVT, 0, Flags);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000492 } else if (ExternalSymbolSDNode *S =
493 dyn_cast<ExternalSymbolSDNode>(Callee)) {
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000494 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), PtrVT, Flags);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000495 }
496
497 // Returns a chain & a flag for retval copy to use.
498 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
499 SmallVector<SDValue, 8> Ops;
500 Ops.push_back(Chain);
501 Ops.push_back(Callee);
502
503 // Add argument registers to the end of the list so that they are
504 // known live into the call.
505 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
506 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
507 RegsToPass[i].second.getValueType()));
508 }
509
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000510 const uint32_t *Mask = HRI.getCallPreservedMask(MF, CallConv);
511 assert(Mask && "Missing call preserved mask for calling convention");
512 Ops.push_back(DAG.getRegisterMask(Mask));
513
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000514 if (Glue.getNode())
515 Ops.push_back(Glue);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000516
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000517 if (CLI.IsTailCall) {
Krzysztof Parzyszekf67cd822017-07-11 17:11:54 +0000518 MFI.setHasTailCall();
Craig Topper48d114b2014-04-26 18:35:24 +0000519 return DAG.getNode(HexagonISD::TC_RETURN, dl, NodeTys, Ops);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +0000520 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000521
Krzysztof Parzyszekf67cd822017-07-11 17:11:54 +0000522 // Set this here because we need to know this for "hasFP" in frame lowering.
523 // The target-independent code calls getFrameRegister before setting it, and
524 // getFrameRegister uses hasFP to determine whether the function has FP.
525 MFI.setHasCalls(true);
526
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +0000527 unsigned OpCode = DoesNotReturn ? HexagonISD::CALLnr : HexagonISD::CALL;
Colin LeMahieu2e3a26d2015-01-16 17:05:27 +0000528 Chain = DAG.getNode(OpCode, dl, NodeTys, Ops);
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000529 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000530
531 // Create the CALLSEQ_END node.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000532 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000533 DAG.getIntPtrConstant(0, dl, true), Glue, dl);
534 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000535
536 // Handle result values, copying them out of physregs into vregs that we
537 // return.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000538 return LowerCallResult(Chain, Glue, CallConv, IsVarArg, Ins, dl, DAG,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000539 InVals, OutVals, Callee);
540}
541
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +0000542/// Returns true by value, base pointer and offset pointer and addressing
543/// mode by reference if this node can be combined with a load / store to
544/// form a post-indexed load / store.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000545bool HexagonTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +0000546 SDValue &Base, SDValue &Offset, ISD::MemIndexedMode &AM,
547 SelectionDAG &DAG) const {
548 LSBaseSDNode *LSN = dyn_cast<LSBaseSDNode>(N);
549 if (!LSN)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000550 return false;
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +0000551 EVT VT = LSN->getMemoryVT();
552 if (!VT.isSimple())
553 return false;
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +0000554 bool IsLegalType = VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32 ||
Brendon Cahoone5ed5632018-05-18 18:14:44 +0000555 VT == MVT::i64 || VT == MVT::f32 || VT == MVT::f64 ||
556 VT == MVT::v2i16 || VT == MVT::v2i32 || VT == MVT::v4i8 ||
557 VT == MVT::v4i16 || VT == MVT::v8i8 ||
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +0000558 Subtarget.isHVXVectorType(VT.getSimpleVT());
559 if (!IsLegalType)
560 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000561
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +0000562 if (Op->getOpcode() != ISD::ADD)
563 return false;
564 Base = Op->getOperand(0);
565 Offset = Op->getOperand(1);
566 if (!isa<ConstantSDNode>(Offset.getNode()))
567 return false;
568 AM = ISD::POST_INC;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000569
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +0000570 int32_t V = cast<ConstantSDNode>(Offset.getNode())->getSExtValue();
571 return Subtarget.getInstrInfo()->isValidAutoIncImm(VT, V);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000572}
573
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000574SDValue
575HexagonTargetLowering::LowerINLINEASM(SDValue Op, SelectionDAG &DAG) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000576 MachineFunction &MF = DAG.getMachineFunction();
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000577 auto &HMFI = *MF.getInfo<HexagonMachineFunctionInfo>();
578 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
579 unsigned LR = HRI.getRARegister();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000580
Craig Topper784929d2019-02-08 20:48:56 +0000581 if ((Op.getOpcode() != ISD::INLINEASM &&
582 Op.getOpcode() != ISD::INLINEASM_BR) || HMFI.hasClobberLR())
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000583 return Op;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000584
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000585 unsigned NumOps = Op.getNumOperands();
586 if (Op.getOperand(NumOps-1).getValueType() == MVT::Glue)
587 --NumOps; // Ignore the flag operand.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000588
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000589 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
590 unsigned Flags = cast<ConstantSDNode>(Op.getOperand(i))->getZExtValue();
591 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
592 ++i; // Skip the ID value.
593
594 switch (InlineAsm::getKind(Flags)) {
595 default:
596 llvm_unreachable("Bad flags!");
597 case InlineAsm::Kind_RegUse:
598 case InlineAsm::Kind_Imm:
599 case InlineAsm::Kind_Mem:
600 i += NumVals;
601 break;
602 case InlineAsm::Kind_Clobber:
603 case InlineAsm::Kind_RegDef:
604 case InlineAsm::Kind_RegDefEarlyClobber: {
605 for (; NumVals; --NumVals, ++i) {
606 unsigned Reg = cast<RegisterSDNode>(Op.getOperand(i))->getReg();
607 if (Reg != LR)
608 continue;
609 HMFI.setHasClobberLR(true);
610 return Op;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000611 }
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000612 break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000613 }
614 }
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000615 }
616
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000617 return Op;
618}
619
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +0000620// Need to transform ISD::PREFETCH into something that doesn't inherit
621// all of the properties of ISD::PREFETCH, specifically SDNPMayLoad and
622// SDNPMayStore.
623SDValue HexagonTargetLowering::LowerPREFETCH(SDValue Op,
624 SelectionDAG &DAG) const {
625 SDValue Chain = Op.getOperand(0);
626 SDValue Addr = Op.getOperand(1);
627 // Lower it to DCFETCH($reg, #0). A "pat" will try to merge the offset in,
628 // if the "reg" is fed by an "add".
629 SDLoc DL(Op);
630 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
631 return DAG.getNode(HexagonISD::DCFETCH, DL, MVT::Other, Chain, Addr, Zero);
632}
633
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +0000634// Custom-handle ISD::READCYCLECOUNTER because the target-independent SDNode
635// is marked as having side-effects, while the register read on Hexagon does
636// not have any. TableGen refuses to accept the direct pattern from that node
637// to the A4_tfrcpp.
638SDValue HexagonTargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
639 SelectionDAG &DAG) const {
640 SDValue Chain = Op.getOperand(0);
641 SDLoc dl(Op);
642 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other);
643 return DAG.getNode(HexagonISD::READCYCLE, dl, VTs, Chain);
644}
645
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +0000646SDValue HexagonTargetLowering::LowerINTRINSIC_VOID(SDValue Op,
647 SelectionDAG &DAG) const {
648 SDValue Chain = Op.getOperand(0);
649 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
650 // Lower the hexagon_prefetch builtin to DCFETCH, as above.
651 if (IntNo == Intrinsic::hexagon_prefetch) {
652 SDValue Addr = Op.getOperand(2);
653 SDLoc DL(Op);
654 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
655 return DAG.getNode(HexagonISD::DCFETCH, DL, MVT::Other, Chain, Addr, Zero);
656 }
657 return SDValue();
658}
659
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000660SDValue
661HexagonTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
662 SelectionDAG &DAG) const {
663 SDValue Chain = Op.getOperand(0);
664 SDValue Size = Op.getOperand(1);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000665 SDValue Align = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000666 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000667
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000668 ConstantSDNode *AlignConst = dyn_cast<ConstantSDNode>(Align);
669 assert(AlignConst && "Non-constant Align in LowerDYNAMIC_STACKALLOC");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000670
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000671 unsigned A = AlignConst->getSExtValue();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000672 auto &HFI = *Subtarget.getFrameLowering();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000673 // "Zero" means natural stack alignment.
674 if (A == 0)
675 A = HFI.getStackAlignment();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000676
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000677 LLVM_DEBUG({
Reid Kleckner40d72302016-10-20 00:22:23 +0000678 dbgs () << __func__ << " Align: " << A << " Size: ";
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000679 Size.getNode()->dump(&DAG);
680 dbgs() << "\n";
681 });
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000682
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000683 SDValue AC = DAG.getConstant(A, dl, MVT::i32);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000684 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other);
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +0000685 SDValue AA = DAG.getNode(HexagonISD::ALLOCA, dl, VTs, Chain, Size, AC);
Nirav Davebfdb4832016-06-23 17:52:57 +0000686
687 DAG.ReplaceAllUsesOfValueWith(Op, AA);
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +0000688 return AA;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000689}
690
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000691SDValue HexagonTargetLowering::LowerFormalArguments(
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000692 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000693 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
694 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000695 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +0000696 MachineFrameInfo &MFI = MF.getFrameInfo();
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000697 MachineRegisterInfo &MRI = MF.getRegInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000698
699 // Assign locations to all of the incoming arguments.
700 SmallVector<CCValAssign, 16> ArgLocs;
Krzysztof Parzyszek18e0d2a2018-02-15 15:47:53 +0000701 HexagonCCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext(),
Krzysztof Parzyszeke0d7de72018-02-15 17:20:07 +0000702 MF.getFunction().getFunctionType()->getNumParams());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000703
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000704 if (Subtarget.useHVXOps())
705 CCInfo.AnalyzeFormalArguments(Ins, CC_Hexagon_HVX);
706 else
707 CCInfo.AnalyzeFormalArguments(Ins, CC_Hexagon);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000708
709 // For LLVM, in the case when returning a struct by value (>8byte),
710 // the first argument is a pointer that points to the location on caller's
711 // stack where the return value will be stored. For Hexagon, the location on
712 // caller's stack is passed only when the struct size is smaller than (and
713 // equal to) 8 bytes. If not, no address will be passed into callee and
714 // callee return the result direclty through R0/R1.
715
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000716 auto &HMFI = *MF.getInfo<HexagonMachineFunctionInfo>();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000717
718 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
719 CCValAssign &VA = ArgLocs[i];
720 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000721 bool ByVal = Flags.isByVal();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000722
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000723 // Arguments passed in registers:
724 // 1. 32- and 64-bit values and HVX vectors are passed directly,
725 // 2. Large structs are passed via an address, and the address is
726 // passed in a register.
727 if (VA.isRegLoc() && ByVal && Flags.getByValSize() <= 8)
728 llvm_unreachable("ByValSize must be bigger than 8 bytes");
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000729
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000730 bool InReg = VA.isRegLoc() &&
731 (!ByVal || (ByVal && Flags.getByValSize() > 8));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000732
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000733 if (InReg) {
734 MVT RegVT = VA.getLocVT();
735 if (VA.getLocInfo() == CCValAssign::BCvt)
736 RegVT = VA.getValVT();
737
738 const TargetRegisterClass *RC = getRegClassFor(RegVT);
739 unsigned VReg = MRI.createVirtualRegister(RC);
740 SDValue Copy = DAG.getCopyFromReg(Chain, dl, VReg, RegVT);
741
742 // Treat values of type MVT::i1 specially: they are passed in
743 // registers of type i32, but they need to remain as values of
744 // type i1 for consistency of the argument lowering.
745 if (VA.getValVT() == MVT::i1) {
746 assert(RegVT.getSizeInBits() <= 32);
747 SDValue T = DAG.getNode(ISD::AND, dl, RegVT,
748 Copy, DAG.getConstant(1, dl, RegVT));
749 Copy = DAG.getSetCC(dl, MVT::i1, T, DAG.getConstant(0, dl, RegVT),
750 ISD::SETNE);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000751 } else {
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000752#ifndef NDEBUG
753 unsigned RegSize = RegVT.getSizeInBits();
754 assert(RegSize == 32 || RegSize == 64 ||
755 Subtarget.isHVXVectorType(RegVT));
756#endif
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000757 }
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000758 InVals.push_back(Copy);
759 MRI.addLiveIn(VA.getLocReg(), VReg);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000760 } else {
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000761 assert(VA.isMemLoc() && "Argument should be passed in memory");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000762
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000763 // If it's a byval parameter, then we need to compute the
764 // "real" size, not the size of the pointer.
765 unsigned ObjSize = Flags.isByVal()
766 ? Flags.getByValSize()
767 : VA.getLocVT().getStoreSizeInBits() / 8;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000768
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000769 // Create the frame index object for this incoming parameter.
770 int Offset = HEXAGON_LRFP_SIZE + VA.getLocMemOffset();
771 int FI = MFI.CreateFixedObject(ObjSize, Offset, true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000772 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
773
774 if (Flags.isByVal()) {
775 // If it's a pass-by-value aggregate, then do not dereference the stack
776 // location. Instead, we should generate a reference to the stack
777 // location.
778 InVals.push_back(FIN);
779 } else {
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000780 SDValue L = DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
781 MachinePointerInfo::getFixedStack(MF, FI, 0));
782 InVals.push_back(L);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000783 }
784 }
785 }
786
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000787
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000788 if (IsVarArg) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000789 // This will point to the next argument passed via stack.
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000790 int Offset = HEXAGON_LRFP_SIZE + CCInfo.getNextStackOffset();
791 int FI = MFI.CreateFixedObject(Hexagon_PointerSize, Offset, true);
792 HMFI.setVarArgsFrameIndex(FI);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000793 }
794
795 return Chain;
796}
797
798SDValue
799HexagonTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
800 // VASTART stores the address of the VarArgsFrameIndex slot into the
801 // memory location argument.
802 MachineFunction &MF = DAG.getMachineFunction();
803 HexagonMachineFunctionInfo *QFI = MF.getInfo<HexagonMachineFunctionInfo>();
804 SDValue Addr = DAG.getFrameIndex(QFI->getVarArgsFrameIndex(), MVT::i32);
805 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Justin Lebar9c375812016-07-15 18:27:10 +0000806 return DAG.getStore(Op.getOperand(0), SDLoc(Op), Addr, Op.getOperand(1),
807 MachinePointerInfo(SV));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000808}
809
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000810SDValue HexagonTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek23bcf062018-04-19 14:24:31 +0000811 const SDLoc &dl(Op);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000812 SDValue LHS = Op.getOperand(0);
813 SDValue RHS = Op.getOperand(1);
Krzysztof Parzyszek23bcf062018-04-19 14:24:31 +0000814 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
815 MVT ResTy = ty(Op);
816 MVT OpTy = ty(LHS);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000817
Krzysztof Parzyszek23bcf062018-04-19 14:24:31 +0000818 if (OpTy == MVT::v2i16 || OpTy == MVT::v4i8) {
819 MVT ElemTy = OpTy.getVectorElementType();
820 assert(ElemTy.isScalarInteger());
821 MVT WideTy = MVT::getVectorVT(MVT::getIntegerVT(2*ElemTy.getSizeInBits()),
822 OpTy.getVectorNumElements());
823 return DAG.getSetCC(dl, ResTy,
824 DAG.getSExtOrTrunc(LHS, SDLoc(LHS), WideTy),
825 DAG.getSExtOrTrunc(RHS, SDLoc(RHS), WideTy), CC);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000826 }
827
828 // Treat all other vector types as legal.
Krzysztof Parzyszek23bcf062018-04-19 14:24:31 +0000829 if (ResTy.isVector())
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000830 return Op;
831
Krzysztof Parzyszek23bcf062018-04-19 14:24:31 +0000832 // Comparisons of short integers should use sign-extend, not zero-extend,
833 // since we can represent small negative values in the compare instructions.
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000834 // The LLVM default is to use zero-extend arbitrarily in these cases.
Krzysztof Parzyszek23bcf062018-04-19 14:24:31 +0000835 auto isSExtFree = [this](SDValue N) {
836 switch (N.getOpcode()) {
837 case ISD::TRUNCATE: {
838 // A sign-extend of a truncate of a sign-extend is free.
839 SDValue Op = N.getOperand(0);
840 if (Op.getOpcode() != ISD::AssertSext)
841 return false;
Eli Friedman0319c282018-07-11 23:26:35 +0000842 EVT OrigTy = cast<VTSDNode>(Op.getOperand(1))->getVT();
Krzysztof Parzyszek23bcf062018-04-19 14:24:31 +0000843 unsigned ThisBW = ty(N).getSizeInBits();
844 unsigned OrigBW = OrigTy.getSizeInBits();
845 // The type that was sign-extended to get the AssertSext must be
846 // narrower than the type of N (so that N has still the same value
847 // as the original).
848 return ThisBW >= OrigBW;
849 }
850 case ISD::LOAD:
851 // We have sign-extended loads.
852 return true;
853 }
854 return false;
855 };
856
857 if (OpTy == MVT::i8 || OpTy == MVT::i16) {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000858 ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS);
Krzysztof Parzyszek23bcf062018-04-19 14:24:31 +0000859 bool IsNegative = C && C->getAPIntValue().isNegative();
860 if (IsNegative || isSExtFree(LHS) || isSExtFree(RHS))
861 return DAG.getSetCC(dl, ResTy,
862 DAG.getSExtOrTrunc(LHS, SDLoc(LHS), MVT::i32),
863 DAG.getSExtOrTrunc(RHS, SDLoc(RHS), MVT::i32), CC);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000864 }
Krzysztof Parzyszek23bcf062018-04-19 14:24:31 +0000865
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000866 return SDValue();
867}
868
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000869SDValue
870HexagonTargetLowering::LowerVSELECT(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000871 SDValue PredOp = Op.getOperand(0);
872 SDValue Op1 = Op.getOperand(1), Op2 = Op.getOperand(2);
873 EVT OpVT = Op1.getValueType();
874 SDLoc DL(Op);
875
876 if (OpVT == MVT::v2i16) {
877 SDValue X1 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i32, Op1);
878 SDValue X2 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i32, Op2);
879 SDValue SL = DAG.getNode(ISD::VSELECT, DL, MVT::v2i32, PredOp, X1, X2);
880 SDValue TR = DAG.getNode(ISD::TRUNCATE, DL, MVT::v2i16, SL);
881 return TR;
882 }
883
884 return SDValue();
885}
886
Krzysztof Parzyszek91ff5c62017-08-01 13:12:53 +0000887static Constant *convert_i1_to_i8(const Constant *ConstVal) {
888 SmallVector<Constant *, 128> NewConst;
889 const ConstantVector *CV = dyn_cast<ConstantVector>(ConstVal);
890 if (!CV)
891 return nullptr;
892
893 LLVMContext &Ctx = ConstVal->getContext();
894 IRBuilder<> IRB(Ctx);
895 unsigned NumVectorElements = CV->getNumOperands();
896 assert(isPowerOf2_32(NumVectorElements) &&
897 "conversion only supported for pow2 VectorSize!");
898
899 for (unsigned i = 0; i < NumVectorElements / 8; ++i) {
900 uint8_t x = 0;
901 for (unsigned j = 0; j < 8; ++j) {
902 uint8_t y = CV->getOperand(i * 8 + j)->getUniqueInteger().getZExtValue();
903 x |= y << (7 - j);
904 }
905 assert((x == 0 || x == 255) && "Either all 0's or all 1's expected!");
906 NewConst.push_back(IRB.getInt8(x));
907 }
908 return ConstantVector::get(NewConst);
909}
910
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000911SDValue
Sirish Pande69295b82012-05-10 20:20:25 +0000912HexagonTargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
913 EVT ValTy = Op.getValueType();
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000914 ConstantPoolSDNode *CPN = cast<ConstantPoolSDNode>(Op);
Krzysztof Parzyszek91ff5c62017-08-01 13:12:53 +0000915 Constant *CVal = nullptr;
916 bool isVTi1Type = false;
917 if (const Constant *ConstVal = dyn_cast<Constant>(CPN->getConstVal())) {
918 Type *CValTy = ConstVal->getType();
919 if (CValTy->isVectorTy() &&
920 CValTy->getVectorElementType()->isIntegerTy(1)) {
921 CVal = convert_i1_to_i8(ConstVal);
922 isVTi1Type = (CVal != nullptr);
923 }
924 }
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000925 unsigned Align = CPN->getAlignment();
Rafael Espindola405e25a2016-06-26 22:24:01 +0000926 bool IsPositionIndependent = isPositionIndependent();
927 unsigned char TF = IsPositionIndependent ? HexagonII::MO_PCREL : 0;
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000928
Ron Lieberman822ee882016-08-13 23:41:11 +0000929 unsigned Offset = 0;
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000930 SDValue T;
931 if (CPN->isMachineConstantPoolEntry())
Ron Lieberman822ee882016-08-13 23:41:11 +0000932 T = DAG.getTargetConstantPool(CPN->getMachineCPVal(), ValTy, Align, Offset,
933 TF);
Krzysztof Parzyszek91ff5c62017-08-01 13:12:53 +0000934 else if (isVTi1Type)
935 T = DAG.getTargetConstantPool(CVal, ValTy, Align, Offset, TF);
Sirish Pande69295b82012-05-10 20:20:25 +0000936 else
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +0000937 T = DAG.getTargetConstantPool(CPN->getConstVal(), ValTy, Align, Offset, TF);
Ron Lieberman822ee882016-08-13 23:41:11 +0000938
939 assert(cast<ConstantPoolSDNode>(T)->getTargetFlags() == TF &&
940 "Inconsistent target flag encountered");
941
Rafael Espindola405e25a2016-06-26 22:24:01 +0000942 if (IsPositionIndependent)
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000943 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), ValTy, T);
944 return DAG.getNode(HexagonISD::CP, SDLoc(Op), ValTy, T);
945}
946
947SDValue
948HexagonTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
949 EVT VT = Op.getValueType();
950 int Idx = cast<JumpTableSDNode>(Op)->getIndex();
Rafael Espindola405e25a2016-06-26 22:24:01 +0000951 if (isPositionIndependent()) {
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000952 SDValue T = DAG.getTargetJumpTable(Idx, VT, HexagonII::MO_PCREL);
953 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), VT, T);
954 }
955
956 SDValue T = DAG.getTargetJumpTable(Idx, VT);
957 return DAG.getNode(HexagonISD::JT, SDLoc(Op), VT, T);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000958}
959
960SDValue
961HexagonTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000962 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000963 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +0000964 MachineFrameInfo &MFI = MF.getFrameInfo();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000965 MFI.setReturnAddressIsTaken(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000966
Bill Wendling908bf812014-01-06 00:43:20 +0000967 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +0000968 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +0000969
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000970 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000971 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000972 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
973 if (Depth) {
974 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000975 SDValue Offset = DAG.getConstant(4, dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000976 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
977 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Justin Lebar9c375812016-07-15 18:27:10 +0000978 MachinePointerInfo());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000979 }
980
981 // Return LR, which contains the return address. Mark it an implicit live-in.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000982 unsigned Reg = MF.addLiveIn(HRI.getRARegister(), getRegClassFor(MVT::i32));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000983 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
984}
985
986SDValue
987HexagonTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000988 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Matthias Braun941a7052016-07-28 18:40:00 +0000989 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000990 MFI.setFrameAddressIsTaken(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000991
992 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000993 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000994 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
995 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000996 HRI.getFrameRegister(), VT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000997 while (Depth--)
998 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
Justin Lebar9c375812016-07-15 18:27:10 +0000999 MachinePointerInfo());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001000 return FrameAddr;
1001}
1002
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001003SDValue
1004HexagonTargetLowering::LowerATOMIC_FENCE(SDValue Op, SelectionDAG& DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001005 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001006 return DAG.getNode(HexagonISD::BARRIER, dl, MVT::Other, Op.getOperand(0));
1007}
1008
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001009SDValue
1010HexagonTargetLowering::LowerGLOBALADDRESS(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001011 SDLoc dl(Op);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001012 auto *GAN = cast<GlobalAddressSDNode>(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00001013 auto PtrVT = getPointerTy(DAG.getDataLayout());
Krzysztof Parzyszek96a28412018-01-30 18:10:27 +00001014 auto *GV = GAN->getGlobal();
1015 int64_t Offset = GAN->getOffset();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001016
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001017 auto &HLOF = *HTM.getObjFileLowering();
1018 Reloc::Model RM = HTM.getRelocationModel();
1019
1020 if (RM == Reloc::Static) {
1021 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, Offset);
Peter Collingbourne67335642016-10-24 19:23:39 +00001022 const GlobalObject *GO = GV->getBaseObject();
Krzysztof Parzyszek44e180b2018-05-14 21:01:56 +00001023 if (GO && Subtarget.useSmallData() && HLOF.isGlobalInSmallSection(GO, HTM))
Krzysztof Parzyszek96a28412018-01-30 18:10:27 +00001024 return DAG.getNode(HexagonISD::CONST32_GP, dl, PtrVT, GA);
1025 return DAG.getNode(HexagonISD::CONST32, dl, PtrVT, GA);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001026 }
1027
Krzysztof Parzyszek96a28412018-01-30 18:10:27 +00001028 bool UsePCRel = getTargetMachine().shouldAssumeDSOLocal(*GV->getParent(), GV);
1029 if (UsePCRel) {
1030 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, Offset,
1031 HexagonII::MO_PCREL);
1032 return DAG.getNode(HexagonISD::AT_PCREL, dl, PtrVT, GA);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001033 }
1034
Krzysztof Parzyszek96a28412018-01-30 18:10:27 +00001035 // Use GOT index.
1036 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
1037 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, HexagonII::MO_GOT);
1038 SDValue Off = DAG.getConstant(Offset, dl, MVT::i32);
1039 return DAG.getNode(HexagonISD::AT_GOT, dl, PtrVT, GOT, GA, Off);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001040}
1041
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001042// Specifies that for loads and stores VT can be promoted to PromotedLdStVT.
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001043SDValue
1044HexagonTargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
1045 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001046 SDLoc dl(Op);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001047 EVT PtrVT = getPointerTy(DAG.getDataLayout());
1048
1049 Reloc::Model RM = HTM.getRelocationModel();
1050 if (RM == Reloc::Static) {
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001051 SDValue A = DAG.getTargetBlockAddress(BA, PtrVT);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001052 return DAG.getNode(HexagonISD::CONST32_GP, dl, PtrVT, A);
1053 }
1054
1055 SDValue A = DAG.getTargetBlockAddress(BA, PtrVT, 0, HexagonII::MO_PCREL);
1056 return DAG.getNode(HexagonISD::AT_PCREL, dl, PtrVT, A);
1057}
1058
1059SDValue
1060HexagonTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op, SelectionDAG &DAG)
1061 const {
1062 EVT PtrVT = getPointerTy(DAG.getDataLayout());
1063 SDValue GOTSym = DAG.getTargetExternalSymbol(HEXAGON_GOT_SYM_NAME, PtrVT,
1064 HexagonII::MO_PCREL);
1065 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), PtrVT, GOTSym);
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001066}
1067
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001068SDValue
1069HexagonTargetLowering::GetDynamicTLSAddr(SelectionDAG &DAG, SDValue Chain,
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001070 GlobalAddressSDNode *GA, SDValue Glue, EVT PtrVT, unsigned ReturnReg,
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001071 unsigned char OperandFlags) const {
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001072 MachineFunction &MF = DAG.getMachineFunction();
1073 MachineFrameInfo &MFI = MF.getFrameInfo();
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001074 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1075 SDLoc dl(GA);
1076 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
1077 GA->getValueType(0),
1078 GA->getOffset(),
1079 OperandFlags);
1080 // Create Operands for the call.The Operands should have the following:
1081 // 1. Chain SDValue
1082 // 2. Callee which in this case is the Global address value.
1083 // 3. Registers live into the call.In this case its R0, as we
1084 // have just one argument to be passed.
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001085 // 4. Glue.
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001086 // Note: The order is important.
1087
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001088 const auto &HRI = *Subtarget.getRegisterInfo();
1089 const uint32_t *Mask = HRI.getCallPreservedMask(MF, CallingConv::C);
1090 assert(Mask && "Missing call preserved mask for calling convention");
1091 SDValue Ops[] = { Chain, TGA, DAG.getRegister(Hexagon::R0, PtrVT),
1092 DAG.getRegisterMask(Mask), Glue };
1093 Chain = DAG.getNode(HexagonISD::CALL, dl, NodeTys, Ops);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001094
1095 // Inform MFI that function has calls.
Matthias Braun941a7052016-07-28 18:40:00 +00001096 MFI.setAdjustsStack(true);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001097
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001098 Glue = Chain.getValue(1);
1099 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Glue);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001100}
1101
1102//
1103// Lower using the intial executable model for TLS addresses
1104//
1105SDValue
1106HexagonTargetLowering::LowerToTLSInitialExecModel(GlobalAddressSDNode *GA,
1107 SelectionDAG &DAG) const {
1108 SDLoc dl(GA);
1109 int64_t Offset = GA->getOffset();
1110 auto PtrVT = getPointerTy(DAG.getDataLayout());
1111
1112 // Get the thread pointer.
1113 SDValue TP = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Hexagon::UGP, PtrVT);
1114
Rafael Espindola405e25a2016-06-26 22:24:01 +00001115 bool IsPositionIndependent = isPositionIndependent();
1116 unsigned char TF =
1117 IsPositionIndependent ? HexagonII::MO_IEGOT : HexagonII::MO_IE;
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001118
1119 // First generate the TLS symbol address
1120 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT,
1121 Offset, TF);
1122
1123 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1124
Rafael Espindola405e25a2016-06-26 22:24:01 +00001125 if (IsPositionIndependent) {
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001126 // Generate the GOT pointer in case of position independent code
1127 SDValue GOT = LowerGLOBAL_OFFSET_TABLE(Sym, DAG);
1128
1129 // Add the TLS Symbol address to GOT pointer.This gives
1130 // GOT relative relocation for the symbol.
1131 Sym = DAG.getNode(ISD::ADD, dl, PtrVT, GOT, Sym);
1132 }
1133
1134 // Load the offset value for TLS symbol.This offset is relative to
1135 // thread pointer.
Justin Lebar9c375812016-07-15 18:27:10 +00001136 SDValue LoadOffset =
1137 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Sym, MachinePointerInfo());
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001138
1139 // Address of the thread local variable is the add of thread
1140 // pointer and the offset of the variable.
1141 return DAG.getNode(ISD::ADD, dl, PtrVT, TP, LoadOffset);
1142}
1143
1144//
1145// Lower using the local executable model for TLS addresses
1146//
1147SDValue
1148HexagonTargetLowering::LowerToTLSLocalExecModel(GlobalAddressSDNode *GA,
1149 SelectionDAG &DAG) const {
1150 SDLoc dl(GA);
1151 int64_t Offset = GA->getOffset();
1152 auto PtrVT = getPointerTy(DAG.getDataLayout());
1153
1154 // Get the thread pointer.
1155 SDValue TP = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Hexagon::UGP, PtrVT);
1156 // Generate the TLS symbol address
1157 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT, Offset,
1158 HexagonII::MO_TPREL);
1159 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1160
1161 // Address of the thread local variable is the add of thread
1162 // pointer and the offset of the variable.
1163 return DAG.getNode(ISD::ADD, dl, PtrVT, TP, Sym);
1164}
1165
1166//
1167// Lower using the general dynamic model for TLS addresses
1168//
1169SDValue
1170HexagonTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
1171 SelectionDAG &DAG) const {
1172 SDLoc dl(GA);
1173 int64_t Offset = GA->getOffset();
1174 auto PtrVT = getPointerTy(DAG.getDataLayout());
1175
1176 // First generate the TLS symbol address
1177 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT, Offset,
1178 HexagonII::MO_GDGOT);
1179
1180 // Then, generate the GOT pointer
1181 SDValue GOT = LowerGLOBAL_OFFSET_TABLE(TGA, DAG);
1182
1183 // Add the TLS symbol and the GOT pointer
1184 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1185 SDValue Chain = DAG.getNode(ISD::ADD, dl, PtrVT, GOT, Sym);
1186
1187 // Copy over the argument to R0
1188 SDValue InFlag;
1189 Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, Hexagon::R0, Chain, InFlag);
1190 InFlag = Chain.getValue(1);
1191
Krzysztof Parzyszeka7503832017-05-02 18:15:33 +00001192 unsigned Flags =
1193 static_cast<const HexagonSubtarget &>(DAG.getSubtarget()).useLongCalls()
1194 ? HexagonII::MO_GDPLT | HexagonII::HMOTF_ConstExtended
1195 : HexagonII::MO_GDPLT;
1196
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001197 return GetDynamicTLSAddr(DAG, Chain, GA, InFlag, PtrVT,
Krzysztof Parzyszeka7503832017-05-02 18:15:33 +00001198 Hexagon::R0, Flags);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001199}
1200
1201//
1202// Lower TLS addresses.
1203//
1204// For now for dynamic models, we only support the general dynamic model.
1205//
1206SDValue
1207HexagonTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1208 SelectionDAG &DAG) const {
1209 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1210
1211 switch (HTM.getTLSModel(GA->getGlobal())) {
1212 case TLSModel::GeneralDynamic:
1213 case TLSModel::LocalDynamic:
1214 return LowerToTLSGeneralDynamicModel(GA, DAG);
1215 case TLSModel::InitialExec:
1216 return LowerToTLSInitialExecModel(GA, DAG);
1217 case TLSModel::LocalExec:
1218 return LowerToTLSLocalExecModel(GA, DAG);
1219 }
1220 llvm_unreachable("Bogus TLS model");
1221}
1222
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001223//===----------------------------------------------------------------------===//
1224// TargetLowering Implementation
1225//===----------------------------------------------------------------------===//
1226
Eric Christopherd737b762015-02-02 22:11:36 +00001227HexagonTargetLowering::HexagonTargetLowering(const TargetMachine &TM,
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001228 const HexagonSubtarget &ST)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001229 : TargetLowering(TM), HTM(static_cast<const HexagonTargetMachine&>(TM)),
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001230 Subtarget(ST) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001231 auto &HRI = *Subtarget.getRegisterInfo();
Sirish Pande69295b82012-05-10 20:20:25 +00001232
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001233 setPrefLoopAlignment(4);
1234 setPrefFunctionAlignment(4);
1235 setMinFunctionAlignment(2);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001236 setStackPointerRegisterToSaveRestore(HRI.getStackRegister());
Krzysztof Parzyszekb3e50ac2018-01-05 20:41:50 +00001237 setBooleanContents(TargetLoweringBase::UndefinedBooleanContent);
1238 setBooleanVectorContents(TargetLoweringBase::UndefinedBooleanContent);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001239
Krzysztof Parzyszekf228c952016-06-22 16:07:10 +00001240 setMaxAtomicSizeInBitsSupported(64);
1241 setMinCmpXchgSizeInBits(32);
1242
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001243 if (EnableHexSDNodeSched)
1244 setSchedulingPreference(Sched::VLIW);
1245 else
1246 setSchedulingPreference(Sched::Source);
1247
1248 // Limits for inline expansion of memcpy/memmove
1249 MaxStoresPerMemcpy = MaxStoresPerMemcpyCL;
1250 MaxStoresPerMemcpyOptSize = MaxStoresPerMemcpyOptSizeCL;
1251 MaxStoresPerMemmove = MaxStoresPerMemmoveCL;
1252 MaxStoresPerMemmoveOptSize = MaxStoresPerMemmoveOptSizeCL;
1253 MaxStoresPerMemset = MaxStoresPerMemsetCL;
1254 MaxStoresPerMemsetOptSize = MaxStoresPerMemsetOptSizeCL;
1255
1256 //
1257 // Set up register classes.
1258 //
1259
1260 addRegisterClass(MVT::i1, &Hexagon::PredRegsRegClass);
1261 addRegisterClass(MVT::v2i1, &Hexagon::PredRegsRegClass); // bbbbaaaa
1262 addRegisterClass(MVT::v4i1, &Hexagon::PredRegsRegClass); // ddccbbaa
1263 addRegisterClass(MVT::v8i1, &Hexagon::PredRegsRegClass); // hgfedcba
1264 addRegisterClass(MVT::i32, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001265 addRegisterClass(MVT::v2i16, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00001266 addRegisterClass(MVT::v4i8, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001267 addRegisterClass(MVT::i64, &Hexagon::DoubleRegsRegClass);
1268 addRegisterClass(MVT::v8i8, &Hexagon::DoubleRegsRegClass);
1269 addRegisterClass(MVT::v4i16, &Hexagon::DoubleRegsRegClass);
1270 addRegisterClass(MVT::v2i32, &Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001271
Krzysztof Parzyszek6bfc6572018-10-19 17:31:11 +00001272 addRegisterClass(MVT::f32, &Hexagon::IntRegsRegClass);
1273 addRegisterClass(MVT::f64, &Hexagon::DoubleRegsRegClass);
Sirish Pande69295b82012-05-10 20:20:25 +00001274
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001275 //
1276 // Handling of scalar operations.
1277 //
1278 // All operations default to "legal", except:
1279 // - indexed loads and stores (pre-/post-incremented),
1280 // - ANY_EXTEND_VECTOR_INREG, ATOMIC_CMP_SWAP_WITH_SUCCESS, CONCAT_VECTORS,
1281 // ConstantFP, DEBUGTRAP, FCEIL, FCOPYSIGN, FEXP, FEXP2, FFLOOR, FGETSIGN,
1282 // FLOG, FLOG2, FLOG10, FMAXNUM, FMINNUM, FNEARBYINT, FRINT, FROUND, TRAP,
1283 // FTRUNC, PREFETCH, SIGN_EXTEND_VECTOR_INREG, ZERO_EXTEND_VECTOR_INREG,
1284 // which default to "expand" for at least one type.
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001285
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001286 // Misc operations.
Krzysztof Parzyszek75c2ca32018-08-09 18:03:45 +00001287 setOperationAction(ISD::ConstantFP, MVT::f32, Legal);
1288 setOperationAction(ISD::ConstantFP, MVT::f64, Legal);
1289 setOperationAction(ISD::TRAP, MVT::Other, Legal);
1290 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
1291 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
1292 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
1293 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
1294 setOperationAction(ISD::INLINEASM, MVT::Other, Custom);
Craig Topper784929d2019-02-08 20:48:56 +00001295 setOperationAction(ISD::INLINEASM_BR, MVT::Other, Custom);
Krzysztof Parzyszek75c2ca32018-08-09 18:03:45 +00001296 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
1297 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
1298 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
1299 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
1300 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
1301 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
1302 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001303
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001304 // Custom legalize GlobalAddress nodes into CONST32.
1305 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001306 setOperationAction(ISD::GlobalAddress, MVT::i8, Custom);
1307 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001308
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001309 // Hexagon needs to optimize cases with negative constants.
Krzysztof Parzyszek23bcf062018-04-19 14:24:31 +00001310 setOperationAction(ISD::SETCC, MVT::i8, Custom);
1311 setOperationAction(ISD::SETCC, MVT::i16, Custom);
1312 setOperationAction(ISD::SETCC, MVT::v4i8, Custom);
1313 setOperationAction(ISD::SETCC, MVT::v2i16, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001314
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001315 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
1316 setOperationAction(ISD::VASTART, MVT::Other, Custom);
1317 setOperationAction(ISD::VAEND, MVT::Other, Expand);
1318 setOperationAction(ISD::VAARG, MVT::Other, Expand);
Krzysztof Parzyszekf6088122018-03-02 18:35:57 +00001319 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001320
1321 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
1322 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
1323 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
1324
1325 if (EmitJumpTables)
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001326 setMinimumJumpTableEntries(MinimumJumpTables);
Krzysztof Parzyszeka61f7da2016-01-13 21:43:13 +00001327 else
Evandro Menezes56c45e92019-06-19 16:12:01 +00001328 setMinimumJumpTableEntries(std::numeric_limits<unsigned>::max());
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001329 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001330
Krzysztof Parzyszek82d284c2018-06-12 21:51:49 +00001331 setOperationAction(ISD::ABS, MVT::i32, Legal);
1332 setOperationAction(ISD::ABS, MVT::i64, Legal);
1333
Krzysztof Parzyszek0b6187c2018-06-01 14:00:32 +00001334 // Hexagon has A4_addp_c and A4_subp_c that take and generate a carry bit,
1335 // but they only operate on i64.
Krzysztof Parzyszek2c4487d2015-04-13 20:37:01 +00001336 for (MVT VT : MVT::integer_valuetypes()) {
Krzysztof Parzyszek5abf80c2019-07-01 15:50:09 +00001337 setOperationAction(ISD::UADDO, VT, Custom);
1338 setOperationAction(ISD::USUBO, VT, Custom);
Krzysztof Parzyszek0b6187c2018-06-01 14:00:32 +00001339 setOperationAction(ISD::SADDO, VT, Expand);
1340 setOperationAction(ISD::SSUBO, VT, Expand);
1341 setOperationAction(ISD::ADDCARRY, VT, Expand);
1342 setOperationAction(ISD::SUBCARRY, VT, Expand);
Krzysztof Parzyszek2c4487d2015-04-13 20:37:01 +00001343 }
Krzysztof Parzyszek0b6187c2018-06-01 14:00:32 +00001344 setOperationAction(ISD::ADDCARRY, MVT::i64, Custom);
1345 setOperationAction(ISD::SUBCARRY, MVT::i64, Custom);
Krzysztof Parzyszek2c4487d2015-04-13 20:37:01 +00001346
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001347 setOperationAction(ISD::CTLZ, MVT::i8, Promote);
1348 setOperationAction(ISD::CTLZ, MVT::i16, Promote);
1349 setOperationAction(ISD::CTTZ, MVT::i8, Promote);
1350 setOperationAction(ISD::CTTZ, MVT::i16, Promote);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001351
Krzysztof Parzyszek6bfc6572018-10-19 17:31:11 +00001352 // Popcount can count # of 1s in i64 but returns i32.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001353 setOperationAction(ISD::CTPOP, MVT::i8, Promote);
1354 setOperationAction(ISD::CTPOP, MVT::i16, Promote);
1355 setOperationAction(ISD::CTPOP, MVT::i32, Promote);
Krzysztof Parzyszekaf5ff652017-02-23 15:02:09 +00001356 setOperationAction(ISD::CTPOP, MVT::i64, Legal);
1357
1358 setOperationAction(ISD::BITREVERSE, MVT::i32, Legal);
1359 setOperationAction(ISD::BITREVERSE, MVT::i64, Legal);
1360 setOperationAction(ISD::BSWAP, MVT::i32, Legal);
1361 setOperationAction(ISD::BSWAP, MVT::i64, Legal);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001362
Krzysztof Parzyszek30c42e22018-12-20 16:39:20 +00001363 setOperationAction(ISD::FSHL, MVT::i32, Legal);
1364 setOperationAction(ISD::FSHL, MVT::i64, Legal);
1365 setOperationAction(ISD::FSHR, MVT::i32, Legal);
1366 setOperationAction(ISD::FSHR, MVT::i64, Legal);
1367
Benjamin Kramer62460692015-04-25 14:46:53 +00001368 for (unsigned IntExpOp :
Krzysztof Parzyszekaafb8c22018-06-05 12:49:19 +00001369 {ISD::SDIV, ISD::UDIV, ISD::SREM, ISD::UREM,
1370 ISD::SDIVREM, ISD::UDIVREM, ISD::ROTL, ISD::ROTR,
1371 ISD::SHL_PARTS, ISD::SRA_PARTS, ISD::SRL_PARTS,
1372 ISD::SMUL_LOHI, ISD::UMUL_LOHI}) {
1373 for (MVT VT : MVT::integer_valuetypes())
1374 setOperationAction(IntExpOp, VT, Expand);
Benjamin Kramer62460692015-04-25 14:46:53 +00001375 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001376
Benjamin Kramer62460692015-04-25 14:46:53 +00001377 for (unsigned FPExpOp :
1378 {ISD::FDIV, ISD::FREM, ISD::FSQRT, ISD::FSIN, ISD::FCOS, ISD::FSINCOS,
1379 ISD::FPOW, ISD::FCOPYSIGN}) {
Krzysztof Parzyszekaafb8c22018-06-05 12:49:19 +00001380 for (MVT VT : MVT::fp_valuetypes())
1381 setOperationAction(FPExpOp, VT, Expand);
Benjamin Kramer62460692015-04-25 14:46:53 +00001382 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001383
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001384 // No extending loads from i32.
1385 for (MVT VT : MVT::integer_valuetypes()) {
1386 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand);
1387 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand);
1388 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand);
1389 }
1390 // Turn FP truncstore into trunc + store.
1391 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Michael Kuperstein2bc3d4d2016-08-18 20:08:15 +00001392 // Turn FP extload into load/fpextend.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001393 for (MVT VT : MVT::fp_valuetypes())
1394 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001395
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001396 // Expand BR_CC and SELECT_CC for all integer and fp types.
1397 for (MVT VT : MVT::integer_valuetypes()) {
1398 setOperationAction(ISD::BR_CC, VT, Expand);
1399 setOperationAction(ISD::SELECT_CC, VT, Expand);
1400 }
1401 for (MVT VT : MVT::fp_valuetypes()) {
1402 setOperationAction(ISD::BR_CC, VT, Expand);
1403 setOperationAction(ISD::SELECT_CC, VT, Expand);
1404 }
1405 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001406
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001407 //
1408 // Handling of vector operations.
1409 //
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001410
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001411 // Set the action for vector operations to "expand", then override it with
1412 // either "custom" or "legal" for specific cases.
Craig Topper26260942015-10-18 05:15:34 +00001413 static const unsigned VectExpOps[] = {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001414 // Integer arithmetic:
Amaury Sechet84674112018-06-01 13:21:33 +00001415 ISD::ADD, ISD::SUB, ISD::MUL, ISD::SDIV, ISD::UDIV,
1416 ISD::SREM, ISD::UREM, ISD::SDIVREM, ISD::UDIVREM, ISD::SADDO,
1417 ISD::UADDO, ISD::SSUBO, ISD::USUBO, ISD::SMUL_LOHI, ISD::UMUL_LOHI,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001418 // Logical/bit:
1419 ISD::AND, ISD::OR, ISD::XOR, ISD::ROTL, ISD::ROTR,
Craig Topper33772c52016-04-28 03:34:31 +00001420 ISD::CTPOP, ISD::CTLZ, ISD::CTTZ,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001421 // Floating point arithmetic/math functions:
1422 ISD::FADD, ISD::FSUB, ISD::FMUL, ISD::FMA, ISD::FDIV,
1423 ISD::FREM, ISD::FNEG, ISD::FABS, ISD::FSQRT, ISD::FSIN,
Craig Topperf6d4dc52017-05-30 15:27:55 +00001424 ISD::FCOS, ISD::FPOW, ISD::FLOG, ISD::FLOG2,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001425 ISD::FLOG10, ISD::FEXP, ISD::FEXP2, ISD::FCEIL, ISD::FTRUNC,
1426 ISD::FRINT, ISD::FNEARBYINT, ISD::FROUND, ISD::FFLOOR,
1427 ISD::FMINNUM, ISD::FMAXNUM, ISD::FSINCOS,
1428 // Misc:
Krzysztof Parzyszek046da742016-10-27 14:30:16 +00001429 ISD::BR_CC, ISD::SELECT_CC, ISD::ConstantPool,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001430 // Vector:
1431 ISD::BUILD_VECTOR, ISD::SCALAR_TO_VECTOR,
1432 ISD::EXTRACT_VECTOR_ELT, ISD::INSERT_VECTOR_ELT,
1433 ISD::EXTRACT_SUBVECTOR, ISD::INSERT_SUBVECTOR,
1434 ISD::CONCAT_VECTORS, ISD::VECTOR_SHUFFLE
1435 };
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001436
1437 for (MVT VT : MVT::vector_valuetypes()) {
Benjamin Kramer62460692015-04-25 14:46:53 +00001438 for (unsigned VectExpOp : VectExpOps)
1439 setOperationAction(VectExpOp, VT, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001440
Krzysztof Parzyszeka696b1b2016-09-08 17:42:14 +00001441 // Expand all extending loads and truncating stores:
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001442 for (MVT TargetVT : MVT::vector_valuetypes()) {
Krzysztof Parzyszeka696b1b2016-09-08 17:42:14 +00001443 if (TargetVT == VT)
1444 continue;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001445 setLoadExtAction(ISD::EXTLOAD, TargetVT, VT, Expand);
Krzysztof Parzyszeka696b1b2016-09-08 17:42:14 +00001446 setLoadExtAction(ISD::ZEXTLOAD, TargetVT, VT, Expand);
1447 setLoadExtAction(ISD::SEXTLOAD, TargetVT, VT, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001448 setTruncStoreAction(VT, TargetVT, Expand);
1449 }
1450
Krzysztof Parzyszek046da742016-10-27 14:30:16 +00001451 // Normalize all inputs to SELECT to be vectors of i32.
1452 if (VT.getVectorElementType() != MVT::i32) {
1453 MVT VT32 = MVT::getVectorVT(MVT::i32, VT.getSizeInBits()/32);
1454 setOperationAction(ISD::SELECT, VT, Promote);
1455 AddPromotedToType(ISD::SELECT, VT, VT32);
1456 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001457 setOperationAction(ISD::SRA, VT, Custom);
1458 setOperationAction(ISD::SHL, VT, Custom);
1459 setOperationAction(ISD::SRL, VT, Custom);
1460 }
1461
Krzysztof Parzyszek64e5d7d2017-10-20 19:33:12 +00001462 // Extending loads from (native) vectors of i8 into (native) vectors of i16
1463 // are legal.
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001464 setLoadExtAction(ISD::EXTLOAD, MVT::v2i16, MVT::v2i8, Legal);
Krzysztof Parzyszek64e5d7d2017-10-20 19:33:12 +00001465 setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i16, MVT::v2i8, Legal);
1466 setLoadExtAction(ISD::SEXTLOAD, MVT::v2i16, MVT::v2i8, Legal);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001467 setLoadExtAction(ISD::EXTLOAD, MVT::v4i16, MVT::v4i8, Legal);
Krzysztof Parzyszek64e5d7d2017-10-20 19:33:12 +00001468 setLoadExtAction(ISD::ZEXTLOAD, MVT::v4i16, MVT::v4i8, Legal);
1469 setLoadExtAction(ISD::SEXTLOAD, MVT::v4i16, MVT::v4i8, Legal);
1470
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001471 // Types natively supported:
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001472 for (MVT NativeVT : {MVT::v8i1, MVT::v4i1, MVT::v2i1, MVT::v4i8,
1473 MVT::v8i8, MVT::v2i16, MVT::v4i16, MVT::v2i32}) {
Benjamin Kramer62460692015-04-25 14:46:53 +00001474 setOperationAction(ISD::BUILD_VECTOR, NativeVT, Custom);
1475 setOperationAction(ISD::EXTRACT_VECTOR_ELT, NativeVT, Custom);
1476 setOperationAction(ISD::INSERT_VECTOR_ELT, NativeVT, Custom);
1477 setOperationAction(ISD::EXTRACT_SUBVECTOR, NativeVT, Custom);
1478 setOperationAction(ISD::INSERT_SUBVECTOR, NativeVT, Custom);
1479 setOperationAction(ISD::CONCAT_VECTORS, NativeVT, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001480
Benjamin Kramer62460692015-04-25 14:46:53 +00001481 setOperationAction(ISD::ADD, NativeVT, Legal);
1482 setOperationAction(ISD::SUB, NativeVT, Legal);
1483 setOperationAction(ISD::MUL, NativeVT, Legal);
1484 setOperationAction(ISD::AND, NativeVT, Legal);
1485 setOperationAction(ISD::OR, NativeVT, Legal);
1486 setOperationAction(ISD::XOR, NativeVT, Legal);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001487 }
1488
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00001489 // Custom lower unaligned loads.
Krzysztof Parzyszek1df70592018-08-08 17:00:09 +00001490 // Also, for both loads and stores, verify the alignment of the address
1491 // in case it is a compile-time constant. This is a usability feature to
1492 // provide a meaningful error message to users.
1493 for (MVT VT : {MVT::i16, MVT::i32, MVT::v4i8, MVT::i64, MVT::v8i8,
1494 MVT::v2i16, MVT::v4i16, MVT::v2i32}) {
1495 setOperationAction(ISD::LOAD, VT, Custom);
1496 setOperationAction(ISD::STORE, VT, Custom);
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00001497 }
1498
Krzysztof Parzyszek99152912018-03-16 15:03:37 +00001499 for (MVT VT : {MVT::v2i16, MVT::v4i8, MVT::v2i32, MVT::v4i16, MVT::v2i32}) {
1500 setCondCodeAction(ISD::SETLT, VT, Expand);
1501 setCondCodeAction(ISD::SETLE, VT, Expand);
1502 setCondCodeAction(ISD::SETULT, VT, Expand);
1503 setCondCodeAction(ISD::SETULE, VT, Expand);
1504 }
1505
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001506 // Custom-lower bitcasts from i8 to v8i1.
1507 setOperationAction(ISD::BITCAST, MVT::i8, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001508 setOperationAction(ISD::SETCC, MVT::v2i16, Custom);
1509 setOperationAction(ISD::VSELECT, MVT::v2i16, Custom);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001510 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i8, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001511 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
1512 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00001513
Krzysztof Parzyszek6bfc6572018-10-19 17:31:11 +00001514 // V5+.
1515 setOperationAction(ISD::FMA, MVT::f64, Expand);
1516 setOperationAction(ISD::FADD, MVT::f64, Expand);
1517 setOperationAction(ISD::FSUB, MVT::f64, Expand);
1518 setOperationAction(ISD::FMUL, MVT::f64, Expand);
Krzysztof Parzyszekbd8ef4b2016-08-19 13:34:31 +00001519
Krzysztof Parzyszek6bfc6572018-10-19 17:31:11 +00001520 setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
1521 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001522
Krzysztof Parzyszek6bfc6572018-10-19 17:31:11 +00001523 setOperationAction(ISD::FP_TO_UINT, MVT::i1, Promote);
1524 setOperationAction(ISD::FP_TO_UINT, MVT::i8, Promote);
1525 setOperationAction(ISD::FP_TO_UINT, MVT::i16, Promote);
1526 setOperationAction(ISD::FP_TO_SINT, MVT::i1, Promote);
1527 setOperationAction(ISD::FP_TO_SINT, MVT::i8, Promote);
1528 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
1529 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Promote);
1530 setOperationAction(ISD::UINT_TO_FP, MVT::i8, Promote);
1531 setOperationAction(ISD::UINT_TO_FP, MVT::i16, Promote);
1532 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Promote);
1533 setOperationAction(ISD::SINT_TO_FP, MVT::i8, Promote);
1534 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001535
1536 // Handling of indexed loads/stores: default is "expand".
1537 //
Brendon Cahoone5ed5632018-05-18 18:14:44 +00001538 for (MVT VT : {MVT::i8, MVT::i16, MVT::i32, MVT::i64, MVT::f32, MVT::f64,
1539 MVT::v2i16, MVT::v2i32, MVT::v4i8, MVT::v4i16, MVT::v8i8}) {
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +00001540 setIndexedLoadAction(ISD::POST_INC, VT, Legal);
1541 setIndexedStoreAction(ISD::POST_INC, VT, Legal);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001542 }
1543
Krzysztof Parzyszek545a68c2018-12-05 21:01:07 +00001544 // Subtarget-specific operation actions.
1545 //
1546 if (Subtarget.hasV60Ops()) {
Krzysztof Parzyszek30c42e22018-12-20 16:39:20 +00001547 setOperationAction(ISD::ROTL, MVT::i32, Legal);
1548 setOperationAction(ISD::ROTL, MVT::i64, Legal);
1549 setOperationAction(ISD::ROTR, MVT::i32, Legal);
1550 setOperationAction(ISD::ROTR, MVT::i64, Legal);
Krzysztof Parzyszek545a68c2018-12-05 21:01:07 +00001551 }
1552 if (Subtarget.hasV66Ops()) {
1553 setOperationAction(ISD::FADD, MVT::f64, Legal);
1554 setOperationAction(ISD::FSUB, MVT::f64, Legal);
1555 }
1556
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00001557 if (Subtarget.useHVXOps())
1558 initializeHVXLowering();
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00001559
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001560 computeRegisterProperties(&HRI);
1561
1562 //
1563 // Library calls for unsupported operations
1564 //
1565 bool FastMath = EnableFastMath;
1566
Benjamin Kramera37c8092015-04-25 14:46:46 +00001567 setLibcallName(RTLIB::SDIV_I32, "__hexagon_divsi3");
1568 setLibcallName(RTLIB::SDIV_I64, "__hexagon_divdi3");
1569 setLibcallName(RTLIB::UDIV_I32, "__hexagon_udivsi3");
1570 setLibcallName(RTLIB::UDIV_I64, "__hexagon_udivdi3");
1571 setLibcallName(RTLIB::SREM_I32, "__hexagon_modsi3");
1572 setLibcallName(RTLIB::SREM_I64, "__hexagon_moddi3");
1573 setLibcallName(RTLIB::UREM_I32, "__hexagon_umodsi3");
1574 setLibcallName(RTLIB::UREM_I64, "__hexagon_umoddi3");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001575
Benjamin Kramera37c8092015-04-25 14:46:46 +00001576 setLibcallName(RTLIB::SINTTOFP_I128_F64, "__hexagon_floattidf");
1577 setLibcallName(RTLIB::SINTTOFP_I128_F32, "__hexagon_floattisf");
1578 setLibcallName(RTLIB::FPTOUINT_F32_I128, "__hexagon_fixunssfti");
1579 setLibcallName(RTLIB::FPTOUINT_F64_I128, "__hexagon_fixunsdfti");
1580 setLibcallName(RTLIB::FPTOSINT_F32_I128, "__hexagon_fixsfti");
1581 setLibcallName(RTLIB::FPTOSINT_F64_I128, "__hexagon_fixdfti");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001582
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001583 // This is the only fast library function for sqrtd.
1584 if (FastMath)
Benjamin Kramera37c8092015-04-25 14:46:46 +00001585 setLibcallName(RTLIB::SQRT_F64, "__hexagon_fast2_sqrtdf2");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001586
Benjamin Kramera37c8092015-04-25 14:46:46 +00001587 // Prefix is: nothing for "slow-math",
Krzysztof Parzyszek6bfc6572018-10-19 17:31:11 +00001588 // "fast2_" for V5+ fast-math double-precision
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001589 // (actually, keep fast-math and fast-math2 separate for now)
Benjamin Kramera37c8092015-04-25 14:46:46 +00001590 if (FastMath) {
1591 setLibcallName(RTLIB::ADD_F64, "__hexagon_fast_adddf3");
1592 setLibcallName(RTLIB::SUB_F64, "__hexagon_fast_subdf3");
1593 setLibcallName(RTLIB::MUL_F64, "__hexagon_fast_muldf3");
1594 setLibcallName(RTLIB::DIV_F64, "__hexagon_fast_divdf3");
Benjamin Kramera37c8092015-04-25 14:46:46 +00001595 setLibcallName(RTLIB::DIV_F32, "__hexagon_fast_divsf3");
1596 } else {
1597 setLibcallName(RTLIB::ADD_F64, "__hexagon_adddf3");
1598 setLibcallName(RTLIB::SUB_F64, "__hexagon_subdf3");
1599 setLibcallName(RTLIB::MUL_F64, "__hexagon_muldf3");
1600 setLibcallName(RTLIB::DIV_F64, "__hexagon_divdf3");
1601 setLibcallName(RTLIB::DIV_F32, "__hexagon_divsf3");
1602 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001603
Krzysztof Parzyszek6bfc6572018-10-19 17:31:11 +00001604 if (FastMath)
1605 setLibcallName(RTLIB::SQRT_F32, "__hexagon_fast2_sqrtf");
1606 else
1607 setLibcallName(RTLIB::SQRT_F32, "__hexagon_sqrtf");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001608
1609 // These cause problems when the shift amount is non-constant.
1610 setLibcallName(RTLIB::SHL_I128, nullptr);
1611 setLibcallName(RTLIB::SRL_I128, nullptr);
1612 setLibcallName(RTLIB::SRA_I128, nullptr);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001613}
1614
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001615const char* HexagonTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00001616 switch ((HexagonISD::NodeType)Opcode) {
Krzysztof Parzyszek0b6187c2018-06-01 14:00:32 +00001617 case HexagonISD::ADDC: return "HexagonISD::ADDC";
1618 case HexagonISD::SUBC: return "HexagonISD::SUBC";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001619 case HexagonISD::ALLOCA: return "HexagonISD::ALLOCA";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001620 case HexagonISD::AT_GOT: return "HexagonISD::AT_GOT";
1621 case HexagonISD::AT_PCREL: return "HexagonISD::AT_PCREL";
1622 case HexagonISD::BARRIER: return "HexagonISD::BARRIER";
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00001623 case HexagonISD::CALL: return "HexagonISD::CALL";
1624 case HexagonISD::CALLnr: return "HexagonISD::CALLnr";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001625 case HexagonISD::CALLR: return "HexagonISD::CALLR";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001626 case HexagonISD::COMBINE: return "HexagonISD::COMBINE";
1627 case HexagonISD::CONST32_GP: return "HexagonISD::CONST32_GP";
1628 case HexagonISD::CONST32: return "HexagonISD::CONST32";
1629 case HexagonISD::CP: return "HexagonISD::CP";
1630 case HexagonISD::DCFETCH: return "HexagonISD::DCFETCH";
1631 case HexagonISD::EH_RETURN: return "HexagonISD::EH_RETURN";
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001632 case HexagonISD::TSTBIT: return "HexagonISD::TSTBIT";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001633 case HexagonISD::EXTRACTU: return "HexagonISD::EXTRACTU";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001634 case HexagonISD::INSERT: return "HexagonISD::INSERT";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001635 case HexagonISD::JT: return "HexagonISD::JT";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001636 case HexagonISD::RET_FLAG: return "HexagonISD::RET_FLAG";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001637 case HexagonISD::TC_RETURN: return "HexagonISD::TC_RETURN";
Krzysztof Parzyszekf85dd9f2017-07-10 20:16:44 +00001638 case HexagonISD::VASL: return "HexagonISD::VASL";
1639 case HexagonISD::VASR: return "HexagonISD::VASR";
1640 case HexagonISD::VLSR: return "HexagonISD::VLSR";
1641 case HexagonISD::VSPLAT: return "HexagonISD::VSPLAT";
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001642 case HexagonISD::VEXTRACTW: return "HexagonISD::VEXTRACTW";
1643 case HexagonISD::VINSERTW0: return "HexagonISD::VINSERTW0";
1644 case HexagonISD::VROR: return "HexagonISD::VROR";
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +00001645 case HexagonISD::READCYCLE: return "HexagonISD::READCYCLE";
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00001646 case HexagonISD::VZERO: return "HexagonISD::VZERO";
Krzysztof Parzyszek41a24b72018-04-20 19:38:37 +00001647 case HexagonISD::VSPLATW: return "HexagonISD::VSPLATW";
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001648 case HexagonISD::D2P: return "HexagonISD::D2P";
1649 case HexagonISD::P2D: return "HexagonISD::P2D";
1650 case HexagonISD::V2Q: return "HexagonISD::V2Q";
1651 case HexagonISD::Q2V: return "HexagonISD::Q2V";
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00001652 case HexagonISD::QCAT: return "HexagonISD::QCAT";
Krzysztof Parzyszek69f1d7e2018-02-06 14:16:52 +00001653 case HexagonISD::QTRUE: return "HexagonISD::QTRUE";
1654 case HexagonISD::QFALSE: return "HexagonISD::QFALSE";
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001655 case HexagonISD::TYPECAST: return "HexagonISD::TYPECAST";
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00001656 case HexagonISD::VALIGN: return "HexagonISD::VALIGN";
Krzysztof Parzyszekad83ce42018-02-14 20:46:06 +00001657 case HexagonISD::VALIGNADDR: return "HexagonISD::VALIGNADDR";
Matthias Braund04893f2015-05-07 21:33:59 +00001658 case HexagonISD::OP_END: break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001659 }
Matthias Braund04893f2015-05-07 21:33:59 +00001660 return nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001661}
1662
Krzysztof Parzyszek1df70592018-08-08 17:00:09 +00001663void
1664HexagonTargetLowering::validateConstPtrAlignment(SDValue Ptr, const SDLoc &dl,
1665 unsigned NeedAlign) const {
1666 auto *CA = dyn_cast<ConstantSDNode>(Ptr);
1667 if (!CA)
1668 return;
1669 unsigned Addr = CA->getZExtValue();
1670 unsigned HaveAlign = Addr != 0 ? 1u << countTrailingZeros(Addr) : NeedAlign;
1671 if (HaveAlign < NeedAlign) {
1672 std::string ErrMsg;
1673 raw_string_ostream O(ErrMsg);
1674 O << "Misaligned constant address: " << format_hex(Addr, 10)
1675 << " has alignment " << HaveAlign
1676 << ", but the memory access requires " << NeedAlign;
1677 if (DebugLoc DL = dl.getDebugLoc())
1678 DL.print(O << ", at ");
1679 report_fatal_error(O.str());
1680 }
1681}
1682
Krzysztof Parzyszekdc7a5572018-03-29 13:52:46 +00001683// Bit-reverse Load Intrinsic: Check if the instruction is a bit reverse load
1684// intrinsic.
1685static bool isBrevLdIntrinsic(const Value *Inst) {
1686 unsigned ID = cast<IntrinsicInst>(Inst)->getIntrinsicID();
1687 return (ID == Intrinsic::hexagon_L2_loadrd_pbr ||
1688 ID == Intrinsic::hexagon_L2_loadri_pbr ||
1689 ID == Intrinsic::hexagon_L2_loadrh_pbr ||
1690 ID == Intrinsic::hexagon_L2_loadruh_pbr ||
1691 ID == Intrinsic::hexagon_L2_loadrb_pbr ||
1692 ID == Intrinsic::hexagon_L2_loadrub_pbr);
1693}
1694
1695// Bit-reverse Load Intrinsic :Crawl up and figure out the object from previous
1696// instruction. So far we only handle bitcast, extract value and bit reverse
1697// load intrinsic instructions. Should we handle CGEP ?
1698static Value *getBrevLdObject(Value *V) {
1699 if (Operator::getOpcode(V) == Instruction::ExtractValue ||
1700 Operator::getOpcode(V) == Instruction::BitCast)
1701 V = cast<Operator>(V)->getOperand(0);
1702 else if (isa<IntrinsicInst>(V) && isBrevLdIntrinsic(V))
1703 V = cast<Instruction>(V)->getOperand(0);
1704 return V;
1705}
1706
1707// Bit-reverse Load Intrinsic: For a PHI Node return either an incoming edge or
1708// a back edge. If the back edge comes from the intrinsic itself, the incoming
1709// edge is returned.
1710static Value *returnEdge(const PHINode *PN, Value *IntrBaseVal) {
1711 const BasicBlock *Parent = PN->getParent();
1712 int Idx = -1;
1713 for (unsigned i = 0, e = PN->getNumIncomingValues(); i < e; ++i) {
1714 BasicBlock *Blk = PN->getIncomingBlock(i);
1715 // Determine if the back edge is originated from intrinsic.
1716 if (Blk == Parent) {
1717 Value *BackEdgeVal = PN->getIncomingValue(i);
1718 Value *BaseVal;
1719 // Loop over till we return the same Value or we hit the IntrBaseVal.
1720 do {
1721 BaseVal = BackEdgeVal;
1722 BackEdgeVal = getBrevLdObject(BackEdgeVal);
1723 } while ((BaseVal != BackEdgeVal) && (IntrBaseVal != BackEdgeVal));
1724 // If the getBrevLdObject returns IntrBaseVal, we should return the
1725 // incoming edge.
1726 if (IntrBaseVal == BackEdgeVal)
1727 continue;
1728 Idx = i;
1729 break;
1730 } else // Set the node to incoming edge.
1731 Idx = i;
1732 }
1733 assert(Idx >= 0 && "Unexpected index to incoming argument in PHI");
1734 return PN->getIncomingValue(Idx);
1735}
1736
1737// Bit-reverse Load Intrinsic: Figure out the underlying object the base
1738// pointer points to, for the bit-reverse load intrinsic. Setting this to
1739// memoperand might help alias analysis to figure out the dependencies.
1740static Value *getUnderLyingObjectForBrevLdIntr(Value *V) {
1741 Value *IntrBaseVal = V;
1742 Value *BaseVal;
1743 // Loop over till we return the same Value, implies we either figure out
1744 // the object or we hit a PHI
1745 do {
1746 BaseVal = V;
1747 V = getBrevLdObject(V);
1748 } while (BaseVal != V);
1749
1750 // Identify the object from PHINode.
1751 if (const PHINode *PN = dyn_cast<PHINode>(V))
1752 return returnEdge(PN, IntrBaseVal);
1753 // For non PHI nodes, the object is the last value returned by getBrevLdObject
1754 else
1755 return V;
1756}
1757
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001758/// Given an intrinsic, checks if on the target the intrinsic will need to map
1759/// to a MemIntrinsicNode (touches memory). If this is the case, it returns
1760/// true and store the intrinsic information into the IntrinsicInfo that was
1761/// passed to the function.
1762bool HexagonTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
1763 const CallInst &I,
Matt Arsenault7d7adf42017-12-14 22:34:10 +00001764 MachineFunction &MF,
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001765 unsigned Intrinsic) const {
1766 switch (Intrinsic) {
Krzysztof Parzyszekdc7a5572018-03-29 13:52:46 +00001767 case Intrinsic::hexagon_L2_loadrd_pbr:
1768 case Intrinsic::hexagon_L2_loadri_pbr:
1769 case Intrinsic::hexagon_L2_loadrh_pbr:
1770 case Intrinsic::hexagon_L2_loadruh_pbr:
1771 case Intrinsic::hexagon_L2_loadrb_pbr:
1772 case Intrinsic::hexagon_L2_loadrub_pbr: {
1773 Info.opc = ISD::INTRINSIC_W_CHAIN;
1774 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
1775 auto &Cont = I.getCalledFunction()->getParent()->getContext();
1776 // The intrinsic function call is of the form { ElTy, i8* }
1777 // @llvm.hexagon.L2.loadXX.pbr(i8*, i32). The pointer and memory access type
1778 // should be derived from ElTy.
James Y Knight62df5ee2019-01-10 16:07:20 +00001779 Type *ElTy = I.getCalledFunction()->getReturnType()->getStructElementType(0);
1780 Info.memVT = MVT::getVT(ElTy);
Krzysztof Parzyszekdc7a5572018-03-29 13:52:46 +00001781 llvm::Value *BasePtrVal = I.getOperand(0);
1782 Info.ptrVal = getUnderLyingObjectForBrevLdIntr(BasePtrVal);
1783 // The offset value comes through Modifier register. For now, assume the
1784 // offset is 0.
1785 Info.offset = 0;
1786 Info.align = DL.getABITypeAlignment(Info.memVT.getTypeForEVT(Cont));
1787 Info.flags = MachineMemOperand::MOLoad;
1788 return true;
1789 }
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001790 case Intrinsic::hexagon_V6_vgathermw:
1791 case Intrinsic::hexagon_V6_vgathermw_128B:
1792 case Intrinsic::hexagon_V6_vgathermh:
1793 case Intrinsic::hexagon_V6_vgathermh_128B:
1794 case Intrinsic::hexagon_V6_vgathermhw:
1795 case Intrinsic::hexagon_V6_vgathermhw_128B:
1796 case Intrinsic::hexagon_V6_vgathermwq:
1797 case Intrinsic::hexagon_V6_vgathermwq_128B:
1798 case Intrinsic::hexagon_V6_vgathermhq:
1799 case Intrinsic::hexagon_V6_vgathermhq_128B:
1800 case Intrinsic::hexagon_V6_vgathermhwq:
1801 case Intrinsic::hexagon_V6_vgathermhwq_128B: {
1802 const Module &M = *I.getParent()->getParent()->getParent();
1803 Info.opc = ISD::INTRINSIC_W_CHAIN;
1804 Type *VecTy = I.getArgOperand(1)->getType();
1805 Info.memVT = MVT::getVT(VecTy);
1806 Info.ptrVal = I.getArgOperand(0);
1807 Info.offset = 0;
1808 Info.align = M.getDataLayout().getTypeAllocSizeInBits(VecTy) / 8;
Matt Arsenault11171332017-12-14 21:39:51 +00001809 Info.flags = MachineMemOperand::MOLoad |
1810 MachineMemOperand::MOStore |
1811 MachineMemOperand::MOVolatile;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001812 return true;
1813 }
1814 default:
1815 break;
1816 }
1817 return false;
1818}
1819
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001820bool HexagonTargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00001821 return isTruncateFree(EVT::getEVT(Ty1), EVT::getEVT(Ty2));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001822}
1823
1824bool HexagonTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001825 if (!VT1.isSimple() || !VT2.isSimple())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001826 return false;
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00001827 return VT1.getSimpleVT() == MVT::i64 && VT2.getSimpleVT() == MVT::i32;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001828}
1829
Krzysztof Parzyszekbd8ef4b2016-08-19 13:34:31 +00001830bool HexagonTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
1831 return isOperationLegalOrCustom(ISD::FMA, VT);
1832}
1833
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001834// Should we expand the build vector with shuffles?
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00001835bool HexagonTargetLowering::shouldExpandBuildVectorWithShuffles(EVT VT,
1836 unsigned DefinedValues) const {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001837 return false;
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00001838}
1839
Zvi Rackover1b736822017-07-26 08:06:58 +00001840bool HexagonTargetLowering::isShuffleMaskLegal(ArrayRef<int> Mask,
1841 EVT VT) const {
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00001842 return true;
1843}
1844
Krzysztof Parzyszek5439a702017-12-18 18:21:01 +00001845TargetLoweringBase::LegalizeTypeAction
Craig Topper0b5f8162018-11-05 23:26:13 +00001846HexagonTargetLowering::getPreferredVectorAction(MVT VT) const {
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00001847 if (VT.getVectorNumElements() == 1)
1848 return TargetLoweringBase::TypeScalarizeVector;
1849
1850 // Always widen vectors of i1.
Craig Topper0b5f8162018-11-05 23:26:13 +00001851 MVT ElemTy = VT.getVectorElementType();
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00001852 if (ElemTy == MVT::i1)
1853 return TargetLoweringBase::TypeWidenVector;
1854
Krzysztof Parzyszek5439a702017-12-18 18:21:01 +00001855 if (Subtarget.useHVXOps()) {
1856 // If the size of VT is at least half of the vector length,
1857 // widen the vector. Note: the threshold was not selected in
1858 // any scientific way.
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00001859 ArrayRef<MVT> Tys = Subtarget.getHVXElementTypes();
1860 if (llvm::find(Tys, ElemTy) != Tys.end()) {
1861 unsigned HwWidth = 8*Subtarget.getVectorLength();
1862 unsigned VecWidth = VT.getSizeInBits();
1863 if (VecWidth >= HwWidth/2 && VecWidth < HwWidth)
1864 return TargetLoweringBase::TypeWidenVector;
1865 }
Krzysztof Parzyszek5439a702017-12-18 18:21:01 +00001866 }
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00001867 return TargetLoweringBase::TypeSplitVector;
Krzysztof Parzyszek5439a702017-12-18 18:21:01 +00001868}
1869
Krzysztof Parzyszekad83ce42018-02-14 20:46:06 +00001870std::pair<SDValue, int>
1871HexagonTargetLowering::getBaseAndOffset(SDValue Addr) const {
1872 if (Addr.getOpcode() == ISD::ADD) {
1873 SDValue Op1 = Addr.getOperand(1);
1874 if (auto *CN = dyn_cast<const ConstantSDNode>(Op1.getNode()))
1875 return { Addr.getOperand(0), CN->getSExtValue() };
1876 }
1877 return { Addr, 0 };
1878}
1879
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00001880// Lower a vector shuffle (V1, V2, V3). V1 and V2 are the two vectors
1881// to select data from, V3 is the permutation.
1882SDValue
1883HexagonTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG)
1884 const {
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001885 const auto *SVN = cast<ShuffleVectorSDNode>(Op);
1886 ArrayRef<int> AM = SVN->getMask();
1887 assert(AM.size() <= 8 && "Unexpected shuffle mask");
1888 unsigned VecLen = AM.size();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001889
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001890 MVT VecTy = ty(Op);
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00001891 assert(!Subtarget.isHVXVectorType(VecTy, true) &&
1892 "HVX shuffles should be legal");
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001893 assert(VecTy.getSizeInBits() <= 64 && "Unexpected vector length");
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001894
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001895 SDValue Op0 = Op.getOperand(0);
1896 SDValue Op1 = Op.getOperand(1);
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +00001897 const SDLoc &dl(Op);
1898
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001899 // If the inputs are not the same as the output, bail. This is not an
1900 // error situation, but complicates the handling and the default expansion
1901 // (into BUILD_VECTOR) should be adequate.
1902 if (ty(Op0) != VecTy || ty(Op1) != VecTy)
1903 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001904
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001905 // Normalize the mask so that the first non-negative index comes from
1906 // the first operand.
1907 SmallVector<int,8> Mask(AM.begin(), AM.end());
1908 unsigned F = llvm::find_if(AM, [](int M) { return M >= 0; }) - AM.data();
1909 if (F == AM.size())
1910 return DAG.getUNDEF(VecTy);
1911 if (AM[F] >= int(VecLen)) {
1912 ShuffleVectorSDNode::commuteMask(Mask);
1913 std::swap(Op0, Op1);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001914 }
1915
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001916 // Express the shuffle mask in terms of bytes.
1917 SmallVector<int,8> ByteMask;
1918 unsigned ElemBytes = VecTy.getVectorElementType().getSizeInBits() / 8;
1919 for (unsigned i = 0, e = Mask.size(); i != e; ++i) {
1920 int M = Mask[i];
1921 if (M < 0) {
1922 for (unsigned j = 0; j != ElemBytes; ++j)
1923 ByteMask.push_back(-1);
1924 } else {
1925 for (unsigned j = 0; j != ElemBytes; ++j)
1926 ByteMask.push_back(M*ElemBytes + j);
1927 }
1928 }
1929 assert(ByteMask.size() <= 8);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001930
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001931 // All non-undef (non-negative) indexes are well within [0..127], so they
1932 // fit in a single byte. Build two 64-bit words:
1933 // - MaskIdx where each byte is the corresponding index (for non-negative
1934 // indexes), and 0xFF for negative indexes, and
1935 // - MaskUnd that has 0xFF for each negative index.
1936 uint64_t MaskIdx = 0;
1937 uint64_t MaskUnd = 0;
1938 for (unsigned i = 0, e = ByteMask.size(); i != e; ++i) {
1939 unsigned S = 8*i;
1940 uint64_t M = ByteMask[i] & 0xFF;
1941 if (M == 0xFF)
1942 MaskUnd |= M << S;
1943 MaskIdx |= M << S;
1944 }
1945
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001946 if (ByteMask.size() == 4) {
1947 // Identity.
1948 if (MaskIdx == (0x03020100 | MaskUnd))
1949 return Op0;
1950 // Byte swap.
1951 if (MaskIdx == (0x00010203 | MaskUnd)) {
1952 SDValue T0 = DAG.getBitcast(MVT::i32, Op0);
1953 SDValue T1 = DAG.getNode(ISD::BSWAP, dl, MVT::i32, T0);
1954 return DAG.getBitcast(VecTy, T1);
1955 }
1956
1957 // Byte packs.
1958 SDValue Concat10 = DAG.getNode(HexagonISD::COMBINE, dl,
1959 typeJoin({ty(Op1), ty(Op0)}), {Op1, Op0});
1960 if (MaskIdx == (0x06040200 | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001961 return getInstr(Hexagon::S2_vtrunehb, dl, VecTy, {Concat10}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001962 if (MaskIdx == (0x07050301 | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001963 return getInstr(Hexagon::S2_vtrunohb, dl, VecTy, {Concat10}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001964
1965 SDValue Concat01 = DAG.getNode(HexagonISD::COMBINE, dl,
1966 typeJoin({ty(Op0), ty(Op1)}), {Op0, Op1});
1967 if (MaskIdx == (0x02000604 | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001968 return getInstr(Hexagon::S2_vtrunehb, dl, VecTy, {Concat01}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001969 if (MaskIdx == (0x03010705 | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001970 return getInstr(Hexagon::S2_vtrunohb, dl, VecTy, {Concat01}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001971 }
1972
1973 if (ByteMask.size() == 8) {
1974 // Identity.
1975 if (MaskIdx == (0x0706050403020100ull | MaskUnd))
1976 return Op0;
1977 // Byte swap.
1978 if (MaskIdx == (0x0001020304050607ull | MaskUnd)) {
1979 SDValue T0 = DAG.getBitcast(MVT::i64, Op0);
1980 SDValue T1 = DAG.getNode(ISD::BSWAP, dl, MVT::i64, T0);
1981 return DAG.getBitcast(VecTy, T1);
1982 }
1983
1984 // Halfword picks.
1985 if (MaskIdx == (0x0d0c050409080100ull | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001986 return getInstr(Hexagon::S2_shuffeh, dl, VecTy, {Op1, Op0}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001987 if (MaskIdx == (0x0f0e07060b0a0302ull | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001988 return getInstr(Hexagon::S2_shuffoh, dl, VecTy, {Op1, Op0}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001989 if (MaskIdx == (0x0d0c090805040100ull | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001990 return getInstr(Hexagon::S2_vtrunewh, dl, VecTy, {Op1, Op0}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001991 if (MaskIdx == (0x0f0e0b0a07060302ull | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001992 return getInstr(Hexagon::S2_vtrunowh, dl, VecTy, {Op1, Op0}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001993 if (MaskIdx == (0x0706030205040100ull | MaskUnd)) {
1994 VectorPair P = opSplit(Op0, dl, DAG);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001995 return getInstr(Hexagon::S2_packhl, dl, VecTy, {P.second, P.first}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001996 }
1997
1998 // Byte packs.
1999 if (MaskIdx == (0x0e060c040a020800ull | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002000 return getInstr(Hexagon::S2_shuffeb, dl, VecTy, {Op1, Op0}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00002001 if (MaskIdx == (0x0f070d050b030901ull | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002002 return getInstr(Hexagon::S2_shuffob, dl, VecTy, {Op1, Op0}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00002003 }
2004
2005 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002006}
2007
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00002008// Create a Hexagon-specific node for shifting a vector by an integer.
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002009SDValue
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00002010HexagonTargetLowering::getVectorShiftByInt(SDValue Op, SelectionDAG &DAG)
2011 const {
Krzysztof Parzyszek1108ee22018-01-31 20:49:24 +00002012 if (auto *BVN = dyn_cast<BuildVectorSDNode>(Op.getOperand(1).getNode())) {
2013 if (SDValue S = BVN->getSplatValue()) {
2014 unsigned NewOpc;
2015 switch (Op.getOpcode()) {
2016 case ISD::SHL:
2017 NewOpc = HexagonISD::VASL;
2018 break;
2019 case ISD::SRA:
2020 NewOpc = HexagonISD::VASR;
2021 break;
2022 case ISD::SRL:
2023 NewOpc = HexagonISD::VLSR;
2024 break;
2025 default:
2026 llvm_unreachable("Unexpected shift opcode");
2027 }
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00002028 return DAG.getNode(NewOpc, SDLoc(Op), ty(Op), Op.getOperand(0), S);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002029 }
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002030 }
2031
Krzysztof Parzyszek1108ee22018-01-31 20:49:24 +00002032 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002033}
2034
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002035SDValue
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00002036HexagonTargetLowering::LowerVECTOR_SHIFT(SDValue Op, SelectionDAG &DAG) const {
2037 return getVectorShiftByInt(Op, DAG);
2038}
2039
2040SDValue
Krzysztof Parzyszek3d671242018-06-12 12:49:36 +00002041HexagonTargetLowering::LowerROTL(SDValue Op, SelectionDAG &DAG) const {
2042 if (isa<ConstantSDNode>(Op.getOperand(1).getNode()))
2043 return Op;
2044 return SDValue();
2045}
2046
2047SDValue
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002048HexagonTargetLowering::LowerBITCAST(SDValue Op, SelectionDAG &DAG) const {
2049 MVT ResTy = ty(Op);
2050 SDValue InpV = Op.getOperand(0);
2051 MVT InpTy = ty(InpV);
2052 assert(ResTy.getSizeInBits() == InpTy.getSizeInBits());
2053 const SDLoc &dl(Op);
2054
2055 // Handle conversion from i8 to v8i1.
2056 if (ResTy == MVT::v8i1) {
2057 SDValue Sc = DAG.getBitcast(tyScalar(InpTy), InpV);
2058 SDValue Ext = DAG.getZExtOrTrunc(Sc, dl, MVT::i32);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002059 return getInstr(Hexagon::C2_tfrrp, dl, ResTy, Ext, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002060 }
2061
2062 return SDValue();
2063}
2064
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002065bool
2066HexagonTargetLowering::getBuildVectorConstInts(ArrayRef<SDValue> Values,
2067 MVT VecTy, SelectionDAG &DAG,
2068 MutableArrayRef<ConstantInt*> Consts) const {
2069 MVT ElemTy = VecTy.getVectorElementType();
2070 unsigned ElemWidth = ElemTy.getSizeInBits();
2071 IntegerType *IntTy = IntegerType::get(*DAG.getContext(), ElemWidth);
2072 bool AllConst = true;
2073
2074 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
2075 SDValue V = Values[i];
2076 if (V.isUndef()) {
2077 Consts[i] = ConstantInt::get(IntTy, 0);
2078 continue;
2079 }
Krzysztof Parzyszek4ef6cff2018-01-11 18:03:23 +00002080 // Make sure to always cast to IntTy.
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002081 if (auto *CN = dyn_cast<ConstantSDNode>(V.getNode())) {
2082 const ConstantInt *CI = CN->getConstantIntValue();
Krzysztof Parzyszek4ef6cff2018-01-11 18:03:23 +00002083 Consts[i] = ConstantInt::get(IntTy, CI->getValue().getSExtValue());
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002084 } else if (auto *CN = dyn_cast<ConstantFPSDNode>(V.getNode())) {
2085 const ConstantFP *CF = CN->getConstantFPValue();
2086 APInt A = CF->getValueAPF().bitcastToAPInt();
2087 Consts[i] = ConstantInt::get(IntTy, A.getZExtValue());
2088 } else {
2089 AllConst = false;
2090 }
2091 }
2092 return AllConst;
2093}
2094
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002095SDValue
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002096HexagonTargetLowering::buildVector32(ArrayRef<SDValue> Elem, const SDLoc &dl,
2097 MVT VecTy, SelectionDAG &DAG) const {
2098 MVT ElemTy = VecTy.getVectorElementType();
2099 assert(VecTy.getVectorNumElements() == Elem.size());
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002100
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002101 SmallVector<ConstantInt*,4> Consts(Elem.size());
2102 bool AllConst = getBuildVectorConstInts(Elem, VecTy, DAG, Consts);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002103
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002104 unsigned First, Num = Elem.size();
2105 for (First = 0; First != Num; ++First)
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002106 if (!isUndef(Elem[First]))
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002107 break;
2108 if (First == Num)
2109 return DAG.getUNDEF(VecTy);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002110
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002111 if (AllConst &&
2112 llvm::all_of(Consts, [](ConstantInt *CI) { return CI->isZero(); }))
2113 return getZero(dl, VecTy, DAG);
2114
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002115 if (ElemTy == MVT::i16) {
2116 assert(Elem.size() == 2);
2117 if (AllConst) {
2118 uint32_t V = (Consts[0]->getZExtValue() & 0xFFFF) |
2119 Consts[1]->getZExtValue() << 16;
2120 return DAG.getBitcast(MVT::v2i16, DAG.getConstant(V, dl, MVT::i32));
Krzysztof Parzyszek89b2d7c2017-07-13 18:17:58 +00002121 }
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002122 SDValue N = getInstr(Hexagon::A2_combine_ll, dl, MVT::i32,
2123 {Elem[1], Elem[0]}, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002124 return DAG.getBitcast(MVT::v2i16, N);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002125 }
2126
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002127 if (ElemTy == MVT::i8) {
2128 // First try generating a constant.
2129 if (AllConst) {
2130 int32_t V = (Consts[0]->getZExtValue() & 0xFF) |
2131 (Consts[1]->getZExtValue() & 0xFF) << 8 |
2132 (Consts[1]->getZExtValue() & 0xFF) << 16 |
2133 Consts[2]->getZExtValue() << 24;
2134 return DAG.getBitcast(MVT::v4i8, DAG.getConstant(V, dl, MVT::i32));
2135 }
2136
2137 // Then try splat.
2138 bool IsSplat = true;
2139 for (unsigned i = 0; i != Num; ++i) {
2140 if (i == First)
2141 continue;
2142 if (Elem[i] == Elem[First] || isUndef(Elem[i]))
2143 continue;
2144 IsSplat = false;
2145 break;
2146 }
2147 if (IsSplat) {
2148 // Legalize the operand to VSPLAT.
2149 SDValue Ext = DAG.getZExtOrTrunc(Elem[First], dl, MVT::i32);
2150 return DAG.getNode(HexagonISD::VSPLAT, dl, VecTy, Ext);
2151 }
2152
2153 // Generate
2154 // (zxtb(Elem[0]) | (zxtb(Elem[1]) << 8)) |
2155 // (zxtb(Elem[2]) | (zxtb(Elem[3]) << 8)) << 16
2156 assert(Elem.size() == 4);
2157 SDValue Vs[4];
2158 for (unsigned i = 0; i != 4; ++i) {
2159 Vs[i] = DAG.getZExtOrTrunc(Elem[i], dl, MVT::i32);
2160 Vs[i] = DAG.getZeroExtendInReg(Vs[i], dl, MVT::i8);
2161 }
2162 SDValue S8 = DAG.getConstant(8, dl, MVT::i32);
2163 SDValue T0 = DAG.getNode(ISD::SHL, dl, MVT::i32, {Vs[1], S8});
2164 SDValue T1 = DAG.getNode(ISD::SHL, dl, MVT::i32, {Vs[3], S8});
2165 SDValue B0 = DAG.getNode(ISD::OR, dl, MVT::i32, {Vs[0], T0});
2166 SDValue B1 = DAG.getNode(ISD::OR, dl, MVT::i32, {Vs[2], T1});
2167
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002168 SDValue R = getInstr(Hexagon::A2_combine_ll, dl, MVT::i32, {B1, B0}, DAG);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002169 return DAG.getBitcast(MVT::v4i8, R);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002170 }
2171
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002172#ifndef NDEBUG
2173 dbgs() << "VecTy: " << EVT(VecTy).getEVTString() << '\n';
2174#endif
2175 llvm_unreachable("Unexpected vector element type");
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002176}
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002177
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002178SDValue
2179HexagonTargetLowering::buildVector64(ArrayRef<SDValue> Elem, const SDLoc &dl,
2180 MVT VecTy, SelectionDAG &DAG) const {
2181 MVT ElemTy = VecTy.getVectorElementType();
2182 assert(VecTy.getVectorNumElements() == Elem.size());
2183
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002184 SmallVector<ConstantInt*,8> Consts(Elem.size());
2185 bool AllConst = getBuildVectorConstInts(Elem, VecTy, DAG, Consts);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002186
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002187 unsigned First, Num = Elem.size();
2188 for (First = 0; First != Num; ++First)
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002189 if (!isUndef(Elem[First]))
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002190 break;
2191 if (First == Num)
2192 return DAG.getUNDEF(VecTy);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002193
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002194 if (AllConst &&
2195 llvm::all_of(Consts, [](ConstantInt *CI) { return CI->isZero(); }))
2196 return getZero(dl, VecTy, DAG);
2197
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002198 // First try splat if possible.
2199 if (ElemTy == MVT::i16) {
2200 bool IsSplat = true;
2201 for (unsigned i = 0; i != Num; ++i) {
2202 if (i == First)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002203 continue;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002204 if (Elem[i] == Elem[First] || isUndef(Elem[i]))
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002205 continue;
2206 IsSplat = false;
2207 break;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002208 }
Krzysztof Parzyszekfb0fcac2017-12-20 20:33:49 +00002209 if (IsSplat) {
2210 // Legalize the operand to VSPLAT.
2211 SDValue Ext = DAG.getZExtOrTrunc(Elem[First], dl, MVT::i32);
2212 return DAG.getNode(HexagonISD::VSPLAT, dl, VecTy, Ext);
2213 }
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002214 }
2215
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002216 // Then try constant.
2217 if (AllConst) {
2218 uint64_t Val = 0;
2219 unsigned W = ElemTy.getSizeInBits();
2220 uint64_t Mask = (ElemTy == MVT::i8) ? 0xFFull
2221 : (ElemTy == MVT::i16) ? 0xFFFFull : 0xFFFFFFFFull;
2222 for (unsigned i = 0; i != Num; ++i)
Krzysztof Parzyszek240df6f2018-01-11 18:30:41 +00002223 Val = (Val << W) | (Consts[Num-1-i]->getZExtValue() & Mask);
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002224 SDValue V0 = DAG.getConstant(Val, dl, MVT::i64);
2225 return DAG.getBitcast(VecTy, V0);
2226 }
2227
2228 // Build two 32-bit vectors and concatenate.
2229 MVT HalfTy = MVT::getVectorVT(ElemTy, Num/2);
2230 SDValue L = (ElemTy == MVT::i32)
2231 ? Elem[0]
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002232 : buildVector32(Elem.take_front(Num/2), dl, HalfTy, DAG);
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002233 SDValue H = (ElemTy == MVT::i32)
2234 ? Elem[1]
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002235 : buildVector32(Elem.drop_front(Num/2), dl, HalfTy, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002236 return DAG.getNode(HexagonISD::COMBINE, dl, VecTy, {H, L});
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002237}
2238
2239SDValue
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002240HexagonTargetLowering::extractVector(SDValue VecV, SDValue IdxV,
2241 const SDLoc &dl, MVT ValTy, MVT ResTy,
2242 SelectionDAG &DAG) const {
2243 MVT VecTy = ty(VecV);
2244 assert(!ValTy.isVector() ||
2245 VecTy.getVectorElementType() == ValTy.getVectorElementType());
2246 unsigned VecWidth = VecTy.getSizeInBits();
2247 unsigned ValWidth = ValTy.getSizeInBits();
2248 unsigned ElemWidth = VecTy.getVectorElementType().getSizeInBits();
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002249 assert((VecWidth % ElemWidth) == 0);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002250 auto *IdxN = dyn_cast<ConstantSDNode>(IdxV);
2251
2252 // Special case for v{8,4,2}i1 (the only boolean vectors legal in Hexagon
2253 // without any coprocessors).
2254 if (ElemWidth == 1) {
2255 assert(VecWidth == VecTy.getVectorNumElements() && "Sanity failure");
2256 assert(VecWidth == 8 || VecWidth == 4 || VecWidth == 2);
2257 // Check if this is an extract of the lowest bit.
2258 if (IdxN) {
2259 // Extracting the lowest bit is a no-op, but it changes the type,
2260 // so it must be kept as an operation to avoid errors related to
2261 // type mismatches.
2262 if (IdxN->isNullValue() && ValTy.getSizeInBits() == 1)
2263 return DAG.getNode(HexagonISD::TYPECAST, dl, MVT::i1, VecV);
2264 }
2265
2266 // If the value extracted is a single bit, use tstbit.
2267 if (ValWidth == 1) {
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002268 SDValue A0 = getInstr(Hexagon::C2_tfrpr, dl, MVT::i32, {VecV}, DAG);
Krzysztof Parzyszek4e075092018-07-25 16:20:59 +00002269 SDValue M0 = DAG.getConstant(8 / VecWidth, dl, MVT::i32);
2270 SDValue I0 = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV, M0);
2271 return DAG.getNode(HexagonISD::TSTBIT, dl, MVT::i1, A0, I0);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002272 }
2273
2274 // Each bool vector (v2i1, v4i1, v8i1) always occupies 8 bits in
2275 // a predicate register. The elements of the vector are repeated
2276 // in the register (if necessary) so that the total number is 8.
2277 // The extracted subvector will need to be expanded in such a way.
2278 unsigned Scale = VecWidth / ValWidth;
2279
2280 // Generate (p2d VecV) >> 8*Idx to move the interesting bytes to
2281 // position 0.
2282 assert(ty(IdxV) == MVT::i32);
Krzysztof Parzyszek528aff32018-10-02 15:05:43 +00002283 unsigned VecRep = 8 / VecWidth;
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002284 SDValue S0 = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV,
Krzysztof Parzyszek528aff32018-10-02 15:05:43 +00002285 DAG.getConstant(8*VecRep, dl, MVT::i32));
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002286 SDValue T0 = DAG.getNode(HexagonISD::P2D, dl, MVT::i64, VecV);
2287 SDValue T1 = DAG.getNode(ISD::SRL, dl, MVT::i64, T0, S0);
2288 while (Scale > 1) {
2289 // The longest possible subvector is at most 32 bits, so it is always
2290 // contained in the low subregister.
2291 T1 = DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::i32, T1);
2292 T1 = expandPredicate(T1, dl, DAG);
2293 Scale /= 2;
2294 }
2295
2296 return DAG.getNode(HexagonISD::D2P, dl, ResTy, T1);
2297 }
2298
2299 assert(VecWidth == 32 || VecWidth == 64);
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002300
2301 // Cast everything to scalar integer types.
2302 MVT ScalarTy = tyScalar(VecTy);
2303 VecV = DAG.getBitcast(ScalarTy, VecV);
2304
2305 SDValue WidthV = DAG.getConstant(ValWidth, dl, MVT::i32);
2306 SDValue ExtV;
2307
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002308 if (IdxN) {
2309 unsigned Off = IdxN->getZExtValue() * ElemWidth;
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002310 if (VecWidth == 64 && ValWidth == 32) {
2311 assert(Off == 0 || Off == 32);
2312 unsigned SubIdx = Off == 0 ? Hexagon::isub_lo : Hexagon::isub_hi;
2313 ExtV = DAG.getTargetExtractSubreg(SubIdx, dl, MVT::i32, VecV);
2314 } else if (Off == 0 && (ValWidth % 8) == 0) {
2315 ExtV = DAG.getZeroExtendInReg(VecV, dl, tyScalar(ValTy));
2316 } else {
2317 SDValue OffV = DAG.getConstant(Off, dl, MVT::i32);
2318 // The return type of EXTRACTU must be the same as the type of the
2319 // input vector.
2320 ExtV = DAG.getNode(HexagonISD::EXTRACTU, dl, ScalarTy,
2321 {VecV, WidthV, OffV});
2322 }
2323 } else {
2324 if (ty(IdxV) != MVT::i32)
2325 IdxV = DAG.getZExtOrTrunc(IdxV, dl, MVT::i32);
2326 SDValue OffV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV,
2327 DAG.getConstant(ElemWidth, dl, MVT::i32));
Krzysztof Parzyszekb1b29602018-01-04 13:56:04 +00002328 ExtV = DAG.getNode(HexagonISD::EXTRACTU, dl, ScalarTy,
2329 {VecV, WidthV, OffV});
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002330 }
2331
2332 // Cast ExtV to the requested result type.
2333 ExtV = DAG.getZExtOrTrunc(ExtV, dl, tyScalar(ResTy));
2334 ExtV = DAG.getBitcast(ResTy, ExtV);
2335 return ExtV;
2336}
2337
2338SDValue
2339HexagonTargetLowering::insertVector(SDValue VecV, SDValue ValV, SDValue IdxV,
2340 const SDLoc &dl, MVT ValTy,
2341 SelectionDAG &DAG) const {
2342 MVT VecTy = ty(VecV);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002343 if (VecTy.getVectorElementType() == MVT::i1) {
2344 MVT ValTy = ty(ValV);
2345 assert(ValTy.getVectorElementType() == MVT::i1);
2346 SDValue ValR = DAG.getNode(HexagonISD::P2D, dl, MVT::i64, ValV);
2347 unsigned VecLen = VecTy.getVectorNumElements();
2348 unsigned Scale = VecLen / ValTy.getVectorNumElements();
2349 assert(Scale > 1);
2350
2351 for (unsigned R = Scale; R > 1; R /= 2) {
2352 ValR = contractPredicate(ValR, dl, DAG);
2353 ValR = DAG.getNode(HexagonISD::COMBINE, dl, MVT::i64,
2354 DAG.getUNDEF(MVT::i32), ValR);
2355 }
2356 // The longest possible subvector is at most 32 bits, so it is always
2357 // contained in the low subregister.
2358 ValR = DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::i32, ValR);
2359
2360 unsigned ValBytes = 64 / Scale;
2361 SDValue Width = DAG.getConstant(ValBytes*8, dl, MVT::i32);
2362 SDValue Idx = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV,
2363 DAG.getConstant(8, dl, MVT::i32));
2364 SDValue VecR = DAG.getNode(HexagonISD::P2D, dl, MVT::i64, VecV);
2365 SDValue Ins = DAG.getNode(HexagonISD::INSERT, dl, MVT::i32,
2366 {VecR, ValR, Width, Idx});
2367 return DAG.getNode(HexagonISD::D2P, dl, VecTy, Ins);
2368 }
2369
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002370 unsigned VecWidth = VecTy.getSizeInBits();
2371 unsigned ValWidth = ValTy.getSizeInBits();
2372 assert(VecWidth == 32 || VecWidth == 64);
2373 assert((VecWidth % ValWidth) == 0);
2374
2375 // Cast everything to scalar integer types.
2376 MVT ScalarTy = MVT::getIntegerVT(VecWidth);
2377 // The actual type of ValV may be different than ValTy (which is related
2378 // to the vector type).
2379 unsigned VW = ty(ValV).getSizeInBits();
2380 ValV = DAG.getBitcast(MVT::getIntegerVT(VW), ValV);
2381 VecV = DAG.getBitcast(ScalarTy, VecV);
2382 if (VW != VecWidth)
2383 ValV = DAG.getAnyExtOrTrunc(ValV, dl, ScalarTy);
2384
2385 SDValue WidthV = DAG.getConstant(ValWidth, dl, MVT::i32);
2386 SDValue InsV;
2387
2388 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(IdxV)) {
2389 unsigned W = C->getZExtValue() * ValWidth;
2390 SDValue OffV = DAG.getConstant(W, dl, MVT::i32);
2391 InsV = DAG.getNode(HexagonISD::INSERT, dl, ScalarTy,
2392 {VecV, ValV, WidthV, OffV});
2393 } else {
2394 if (ty(IdxV) != MVT::i32)
2395 IdxV = DAG.getZExtOrTrunc(IdxV, dl, MVT::i32);
2396 SDValue OffV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV, WidthV);
Krzysztof Parzyszekb1b29602018-01-04 13:56:04 +00002397 InsV = DAG.getNode(HexagonISD::INSERT, dl, ScalarTy,
2398 {VecV, ValV, WidthV, OffV});
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002399 }
2400
2401 return DAG.getNode(ISD::BITCAST, dl, VecTy, InsV);
2402}
2403
2404SDValue
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002405HexagonTargetLowering::expandPredicate(SDValue Vec32, const SDLoc &dl,
2406 SelectionDAG &DAG) const {
2407 assert(ty(Vec32).getSizeInBits() == 32);
2408 if (isUndef(Vec32))
2409 return DAG.getUNDEF(MVT::i64);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002410 return getInstr(Hexagon::S2_vsxtbh, dl, MVT::i64, {Vec32}, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002411}
2412
2413SDValue
2414HexagonTargetLowering::contractPredicate(SDValue Vec64, const SDLoc &dl,
2415 SelectionDAG &DAG) const {
2416 assert(ty(Vec64).getSizeInBits() == 64);
2417 if (isUndef(Vec64))
2418 return DAG.getUNDEF(MVT::i32);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002419 return getInstr(Hexagon::S2_vtrunehb, dl, MVT::i32, {Vec64}, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002420}
2421
2422SDValue
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002423HexagonTargetLowering::getZero(const SDLoc &dl, MVT Ty, SelectionDAG &DAG)
2424 const {
2425 if (Ty.isVector()) {
2426 assert(Ty.isInteger() && "Only integer vectors are supported here");
2427 unsigned W = Ty.getSizeInBits();
2428 if (W <= 64)
2429 return DAG.getBitcast(Ty, DAG.getConstant(0, dl, MVT::getIntegerVT(W)));
2430 return DAG.getNode(HexagonISD::VZERO, dl, Ty);
2431 }
2432
2433 if (Ty.isInteger())
2434 return DAG.getConstant(0, dl, Ty);
2435 if (Ty.isFloatingPoint())
2436 return DAG.getConstantFP(0.0, dl, Ty);
2437 llvm_unreachable("Invalid type for zero");
2438}
2439
2440SDValue
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002441HexagonTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002442 MVT VecTy = ty(Op);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002443 unsigned BW = VecTy.getSizeInBits();
2444 const SDLoc &dl(Op);
2445 SmallVector<SDValue,8> Ops;
2446 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i)
2447 Ops.push_back(Op.getOperand(i));
2448
2449 if (BW == 32)
2450 return buildVector32(Ops, dl, VecTy, DAG);
2451 if (BW == 64)
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002452 return buildVector64(Ops, dl, VecTy, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002453
2454 if (VecTy == MVT::v8i1 || VecTy == MVT::v4i1 || VecTy == MVT::v2i1) {
2455 // For each i1 element in the resulting predicate register, put 1
2456 // shifted by the index of the element into a general-purpose register,
2457 // then or them together and transfer it back into a predicate register.
2458 SDValue Rs[8];
2459 SDValue Z = getZero(dl, MVT::i32, DAG);
2460 // Always produce 8 bits, repeat inputs if necessary.
2461 unsigned Rep = 8 / VecTy.getVectorNumElements();
2462 for (unsigned i = 0; i != 8; ++i) {
Simon Pilgrimc1e22902018-01-23 21:22:16 +00002463 SDValue S = DAG.getConstant(1ull << i, dl, MVT::i32);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002464 Rs[i] = DAG.getSelect(dl, MVT::i32, Ops[i/Rep], S, Z);
2465 }
2466 for (ArrayRef<SDValue> A(Rs); A.size() != 1; A = A.drop_back(A.size()/2)) {
2467 for (unsigned i = 0, e = A.size()/2; i != e; ++i)
2468 Rs[i] = DAG.getNode(ISD::OR, dl, MVT::i32, Rs[2*i], Rs[2*i+1]);
2469 }
2470 // Move the value directly to a predicate register.
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002471 return getInstr(Hexagon::C2_tfrrp, dl, VecTy, {Rs[0]}, DAG);
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002472 }
2473
2474 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002475}
2476
2477SDValue
2478HexagonTargetLowering::LowerCONCAT_VECTORS(SDValue Op,
2479 SelectionDAG &DAG) const {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002480 MVT VecTy = ty(Op);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002481 const SDLoc &dl(Op);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002482 if (VecTy.getSizeInBits() == 64) {
2483 assert(Op.getNumOperands() == 2);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002484 return DAG.getNode(HexagonISD::COMBINE, dl, VecTy, Op.getOperand(1),
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002485 Op.getOperand(0));
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002486 }
2487
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002488 MVT ElemTy = VecTy.getVectorElementType();
2489 if (ElemTy == MVT::i1) {
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002490 assert(VecTy == MVT::v2i1 || VecTy == MVT::v4i1 || VecTy == MVT::v8i1);
2491 MVT OpTy = ty(Op.getOperand(0));
2492 // Scale is how many times the operands need to be contracted to match
2493 // the representation in the target register.
2494 unsigned Scale = VecTy.getVectorNumElements() / OpTy.getVectorNumElements();
2495 assert(Scale == Op.getNumOperands() && Scale > 1);
2496
2497 // First, convert all bool vectors to integers, then generate pairwise
2498 // inserts to form values of doubled length. Up until there are only
2499 // two values left to concatenate, all of these values will fit in a
2500 // 32-bit integer, so keep them as i32 to use 32-bit inserts.
2501 SmallVector<SDValue,4> Words[2];
2502 unsigned IdxW = 0;
2503
2504 for (SDValue P : Op.getNode()->op_values()) {
2505 SDValue W = DAG.getNode(HexagonISD::P2D, dl, MVT::i64, P);
2506 for (unsigned R = Scale; R > 1; R /= 2) {
2507 W = contractPredicate(W, dl, DAG);
2508 W = DAG.getNode(HexagonISD::COMBINE, dl, MVT::i64,
2509 DAG.getUNDEF(MVT::i32), W);
2510 }
2511 W = DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::i32, W);
2512 Words[IdxW].push_back(W);
2513 }
2514
2515 while (Scale > 2) {
2516 SDValue WidthV = DAG.getConstant(64 / Scale, dl, MVT::i32);
2517 Words[IdxW ^ 1].clear();
2518
2519 for (unsigned i = 0, e = Words[IdxW].size(); i != e; i += 2) {
2520 SDValue W0 = Words[IdxW][i], W1 = Words[IdxW][i+1];
2521 // Insert W1 into W0 right next to the significant bits of W0.
2522 SDValue T = DAG.getNode(HexagonISD::INSERT, dl, MVT::i32,
2523 {W0, W1, WidthV, WidthV});
2524 Words[IdxW ^ 1].push_back(T);
2525 }
2526 IdxW ^= 1;
2527 Scale /= 2;
2528 }
2529
2530 // Another sanity check. At this point there should only be two words
2531 // left, and Scale should be 2.
2532 assert(Scale == 2 && Words[IdxW].size() == 2);
2533
2534 SDValue WW = DAG.getNode(HexagonISD::COMBINE, dl, MVT::i64,
2535 Words[IdxW][1], Words[IdxW][0]);
2536 return DAG.getNode(HexagonISD::D2P, dl, VecTy, WW);
2537 }
2538
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002539 return SDValue();
2540}
2541
2542SDValue
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002543HexagonTargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
2544 SelectionDAG &DAG) const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002545 SDValue Vec = Op.getOperand(0);
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002546 MVT ElemTy = ty(Vec).getVectorElementType();
2547 return extractVector(Vec, Op.getOperand(1), SDLoc(Op), ElemTy, ty(Op), DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002548}
2549
2550SDValue
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002551HexagonTargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op,
2552 SelectionDAG &DAG) const {
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00002553 return extractVector(Op.getOperand(0), Op.getOperand(1), SDLoc(Op),
2554 ty(Op), ty(Op), DAG);
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002555}
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002556
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002557SDValue
2558HexagonTargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op,
2559 SelectionDAG &DAG) const {
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002560 return insertVector(Op.getOperand(0), Op.getOperand(1), Op.getOperand(2),
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00002561 SDLoc(Op), ty(Op).getVectorElementType(), DAG);
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002562}
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002563
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002564SDValue
2565HexagonTargetLowering::LowerINSERT_SUBVECTOR(SDValue Op,
2566 SelectionDAG &DAG) const {
2567 SDValue ValV = Op.getOperand(1);
2568 return insertVector(Op.getOperand(0), ValV, Op.getOperand(2),
2569 SDLoc(Op), ty(ValV), DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002570}
2571
Tim Northovera4415852013-08-06 09:12:35 +00002572bool
2573HexagonTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
2574 // Assuming the caller does not have either a signext or zeroext modifier, and
2575 // only one value is accepted, any reasonable truncation is allowed.
2576 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
2577 return false;
2578
2579 // FIXME: in principle up to 64-bit could be made safe, but it would be very
2580 // fragile at the moment: any support for multiple value returns would be
2581 // liable to disallow tail calls involving i64 -> iN truncation in many cases.
2582 return Ty1->getPrimitiveSizeInBits() <= 32;
2583}
2584
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002585SDValue
Krzysztof Parzyszek1df70592018-08-08 17:00:09 +00002586HexagonTargetLowering::LowerLoad(SDValue Op, SelectionDAG &DAG) const {
2587 LoadSDNode *LN = cast<LoadSDNode>(Op.getNode());
2588 unsigned ClaimAlign = LN->getAlignment();
2589 validateConstPtrAlignment(LN->getBasePtr(), SDLoc(Op), ClaimAlign);
2590 // Call LowerUnalignedLoad for all loads, it recognizes loads that
2591 // don't need extra aligning.
2592 return LowerUnalignedLoad(Op, DAG);
2593}
2594
2595SDValue
2596HexagonTargetLowering::LowerStore(SDValue Op, SelectionDAG &DAG) const {
2597 StoreSDNode *SN = cast<StoreSDNode>(Op.getNode());
2598 unsigned ClaimAlign = SN->getAlignment();
2599 SDValue Ptr = SN->getBasePtr();
2600 const SDLoc &dl(Op);
2601 validateConstPtrAlignment(Ptr, dl, ClaimAlign);
2602
2603 MVT StoreTy = SN->getMemoryVT().getSimpleVT();
2604 unsigned NeedAlign = Subtarget.getTypeAlignment(StoreTy);
2605 if (ClaimAlign < NeedAlign)
2606 return expandUnalignedStore(SN, DAG);
2607 return Op;
2608}
2609
2610SDValue
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002611HexagonTargetLowering::LowerUnalignedLoad(SDValue Op, SelectionDAG &DAG)
2612 const {
2613 LoadSDNode *LN = cast<LoadSDNode>(Op.getNode());
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002614 MVT LoadTy = ty(Op);
2615 unsigned NeedAlign = Subtarget.getTypeAlignment(LoadTy);
Krzysztof Parzyszek1df70592018-08-08 17:00:09 +00002616 unsigned HaveAlign = LN->getAlignment();
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002617 if (HaveAlign >= NeedAlign)
2618 return Op;
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002619
2620 const SDLoc &dl(Op);
2621 const DataLayout &DL = DAG.getDataLayout();
2622 LLVMContext &Ctx = *DAG.getContext();
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002623
2624 // If the load aligning is disabled or the load can be broken up into two
2625 // smaller legal loads, do the default (target-independent) expansion.
2626 bool DoDefault = false;
Krzysztof Parzyszek480ab2b2018-03-08 18:15:13 +00002627 // Handle it in the default way if this is an indexed load.
2628 if (!LN->isUnindexed())
2629 DoDefault = true;
2630
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002631 if (!AlignLoads) {
Simon Pilgrim266f4392019-06-11 11:00:23 +00002632 if (allowsMemoryAccess(Ctx, DL, LN->getMemoryVT(), *LN->getMemOperand()))
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002633 return Op;
2634 DoDefault = true;
2635 }
Simon Pilgrim266f4392019-06-11 11:00:23 +00002636 if (!DoDefault && (2 * HaveAlign) == NeedAlign) {
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002637 // The PartTy is the equivalent of "getLoadableTypeOfSize(HaveAlign)".
Simon Pilgrim266f4392019-06-11 11:00:23 +00002638 MVT PartTy = HaveAlign <= 8 ? MVT::getIntegerVT(8 * HaveAlign)
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002639 : MVT::getVectorVT(MVT::i8, HaveAlign);
Simon Pilgrim266f4392019-06-11 11:00:23 +00002640 DoDefault = allowsMemoryAccess(Ctx, DL, PartTy, *LN->getMemOperand());
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002641 }
2642 if (DoDefault) {
2643 std::pair<SDValue, SDValue> P = expandUnalignedLoad(LN, DAG);
2644 return DAG.getMergeValues({P.first, P.second}, dl);
2645 }
2646
2647 // The code below generates two loads, both aligned as NeedAlign, and
2648 // with the distance of NeedAlign between them. For that to cover the
2649 // bits that need to be loaded (and without overlapping), the size of
2650 // the loads should be equal to NeedAlign. This is true for all loadable
2651 // types, but add an assertion in case something changes in the future.
2652 assert(LoadTy.getSizeInBits() == 8*NeedAlign);
2653
2654 unsigned LoadLen = NeedAlign;
2655 SDValue Base = LN->getBasePtr();
2656 SDValue Chain = LN->getChain();
2657 auto BO = getBaseAndOffset(Base);
2658 unsigned BaseOpc = BO.first.getOpcode();
2659 if (BaseOpc == HexagonISD::VALIGNADDR && BO.second % LoadLen == 0)
2660 return Op;
2661
2662 if (BO.second % LoadLen != 0) {
2663 BO.first = DAG.getNode(ISD::ADD, dl, MVT::i32, BO.first,
2664 DAG.getConstant(BO.second % LoadLen, dl, MVT::i32));
2665 BO.second -= BO.second % LoadLen;
2666 }
2667 SDValue BaseNoOff = (BaseOpc != HexagonISD::VALIGNADDR)
2668 ? DAG.getNode(HexagonISD::VALIGNADDR, dl, MVT::i32, BO.first,
2669 DAG.getConstant(NeedAlign, dl, MVT::i32))
2670 : BO.first;
2671 SDValue Base0 = DAG.getMemBasePlusOffset(BaseNoOff, BO.second, dl);
2672 SDValue Base1 = DAG.getMemBasePlusOffset(BaseNoOff, BO.second+LoadLen, dl);
2673
2674 MachineMemOperand *WideMMO = nullptr;
2675 if (MachineMemOperand *MMO = LN->getMemOperand()) {
2676 MachineFunction &MF = DAG.getMachineFunction();
2677 WideMMO = MF.getMachineMemOperand(MMO->getPointerInfo(), MMO->getFlags(),
2678 2*LoadLen, LoadLen, MMO->getAAInfo(), MMO->getRanges(),
2679 MMO->getSyncScopeID(), MMO->getOrdering(),
2680 MMO->getFailureOrdering());
2681 }
2682
2683 SDValue Load0 = DAG.getLoad(LoadTy, dl, Chain, Base0, WideMMO);
2684 SDValue Load1 = DAG.getLoad(LoadTy, dl, Chain, Base1, WideMMO);
2685
2686 SDValue Aligned = DAG.getNode(HexagonISD::VALIGN, dl, LoadTy,
2687 {Load1, Load0, BaseNoOff.getOperand(0)});
2688 SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2689 Load0.getValue(1), Load1.getValue(1));
2690 SDValue M = DAG.getMergeValues({Aligned, NewChain}, dl);
2691 return M;
2692}
2693
2694SDValue
Krzysztof Parzyszek5abf80c2019-07-01 15:50:09 +00002695HexagonTargetLowering::LowerUAddSubO(SDValue Op, SelectionDAG &DAG) const {
2696 SDValue X = Op.getOperand(0), Y = Op.getOperand(1);
2697 auto *CY = dyn_cast<ConstantSDNode>(Y);
2698 if (!CY)
2699 return SDValue();
2700
2701 const SDLoc &dl(Op);
2702 SDVTList VTs = Op.getNode()->getVTList();
2703 assert(VTs.NumVTs == 2);
2704 assert(VTs.VTs[1] == MVT::i1);
2705 unsigned Opc = Op.getOpcode();
2706
2707 if (CY) {
2708 uint32_t VY = CY->getZExtValue();
2709 assert(VY != 0 && "This should have been folded");
2710 // X +/- 1
2711 if (VY != 1)
2712 return SDValue();
2713
2714 if (Opc == ISD::UADDO) {
2715 SDValue Op = DAG.getNode(ISD::ADD, dl, VTs.VTs[0], {X, Y});
2716 SDValue Ov = DAG.getSetCC(dl, MVT::i1, Op, getZero(dl, ty(Op), DAG),
2717 ISD::SETEQ);
2718 return DAG.getMergeValues({Op, Ov}, dl);
2719 }
2720 if (Opc == ISD::USUBO) {
2721 SDValue Op = DAG.getNode(ISD::SUB, dl, VTs.VTs[0], {X, Y});
2722 SDValue Ov = DAG.getSetCC(dl, MVT::i1, Op,
2723 DAG.getConstant(-1, dl, ty(Op)), ISD::SETEQ);
2724 return DAG.getMergeValues({Op, Ov}, dl);
2725 }
2726 }
2727
2728 return SDValue();
2729}
2730
2731SDValue
Krzysztof Parzyszek0b6187c2018-06-01 14:00:32 +00002732HexagonTargetLowering::LowerAddSubCarry(SDValue Op, SelectionDAG &DAG) const {
2733 const SDLoc &dl(Op);
2734 unsigned Opc = Op.getOpcode();
2735 SDValue X = Op.getOperand(0), Y = Op.getOperand(1), C = Op.getOperand(2);
2736
2737 if (Opc == ISD::ADDCARRY)
2738 return DAG.getNode(HexagonISD::ADDC, dl, Op.getNode()->getVTList(),
2739 { X, Y, C });
2740
2741 EVT CarryTy = C.getValueType();
2742 SDValue SubC = DAG.getNode(HexagonISD::SUBC, dl, Op.getNode()->getVTList(),
2743 { X, Y, DAG.getLogicalNOT(dl, C, CarryTy) });
2744 SDValue Out[] = { SubC.getValue(0),
2745 DAG.getLogicalNOT(dl, SubC.getValue(1), CarryTy) };
2746 return DAG.getMergeValues(Out, dl);
2747}
2748
2749SDValue
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002750HexagonTargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
2751 SDValue Chain = Op.getOperand(0);
2752 SDValue Offset = Op.getOperand(1);
2753 SDValue Handler = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002754 SDLoc dl(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00002755 auto PtrVT = getPointerTy(DAG.getDataLayout());
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002756
2757 // Mark function as containing a call to EH_RETURN.
2758 HexagonMachineFunctionInfo *FuncInfo =
2759 DAG.getMachineFunction().getInfo<HexagonMachineFunctionInfo>();
2760 FuncInfo->setHasEHReturn();
2761
2762 unsigned OffsetReg = Hexagon::R28;
2763
Mehdi Amini44ede332015-07-09 02:09:04 +00002764 SDValue StoreAddr =
2765 DAG.getNode(ISD::ADD, dl, PtrVT, DAG.getRegister(Hexagon::R30, PtrVT),
2766 DAG.getIntPtrConstant(4, dl));
Justin Lebar9c375812016-07-15 18:27:10 +00002767 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo());
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002768 Chain = DAG.getCopyToReg(Chain, dl, OffsetReg, Offset);
2769
2770 // Not needed we already use it as explict input to EH_RETURN.
2771 // MF.getRegInfo().addLiveOut(OffsetReg);
2772
2773 return DAG.getNode(HexagonISD::EH_RETURN, dl, MVT::Other, Chain);
2774}
2775
2776SDValue
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002777HexagonTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002778 unsigned Opc = Op.getOpcode();
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00002779
2780 // Handle INLINEASM first.
Craig Topper784929d2019-02-08 20:48:56 +00002781 if (Opc == ISD::INLINEASM || Opc == ISD::INLINEASM_BR)
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00002782 return LowerINLINEASM(Op, DAG);
2783
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002784 if (isHvxOperation(Op)) {
2785 // If HVX lowering returns nothing, try the default lowering.
2786 if (SDValue V = LowerHvxOperation(Op, DAG))
2787 return V;
2788 }
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00002789
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002790 switch (Opc) {
2791 default:
2792#ifndef NDEBUG
2793 Op.getNode()->dumpr(&DAG);
2794 if (Opc > HexagonISD::OP_BEGIN && Opc < HexagonISD::OP_END)
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002795 errs() << "Error: check for a non-legal type in this operation\n";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002796#endif
2797 llvm_unreachable("Should not custom lower this!");
2798 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002799 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
2800 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
2801 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
2802 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002803 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
2804 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002805 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Krzysztof Parzyszek1df70592018-08-08 17:00:09 +00002806 case ISD::LOAD: return LowerLoad(Op, DAG);
2807 case ISD::STORE: return LowerStore(Op, DAG);
Krzysztof Parzyszek5abf80c2019-07-01 15:50:09 +00002808 case ISD::UADDO:
2809 case ISD::USUBO: return LowerUAddSubO(Op, DAG);
Krzysztof Parzyszek0b6187c2018-06-01 14:00:32 +00002810 case ISD::ADDCARRY:
2811 case ISD::SUBCARRY: return LowerAddSubCarry(Op, DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002812 case ISD::SRA:
2813 case ISD::SHL:
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002814 case ISD::SRL: return LowerVECTOR_SHIFT(Op, DAG);
Krzysztof Parzyszek3d671242018-06-12 12:49:36 +00002815 case ISD::ROTL: return LowerROTL(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002816 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002817 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002818 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002819 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
2820 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00002821 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002822 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
2823 case ISD::GlobalAddress: return LowerGLOBALADDRESS(Op, DAG);
2824 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002825 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002826 case ISD::VASTART: return LowerVASTART(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002827 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
2828 case ISD::SETCC: return LowerSETCC(Op, DAG);
2829 case ISD::VSELECT: return LowerVSELECT(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002830 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00002831 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00002832 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG);
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +00002833 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Krzysztof Parzyszek9eb085e2018-01-31 20:48:11 +00002834 break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002835 }
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00002836
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002837 return SDValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002838}
2839
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002840void
Krzysztof Parzyszek1df70592018-08-08 17:00:09 +00002841HexagonTargetLowering::LowerOperationWrapper(SDNode *N,
2842 SmallVectorImpl<SDValue> &Results,
2843 SelectionDAG &DAG) const {
2844 // We are only custom-lowering stores to verify the alignment of the
2845 // address if it is a compile-time constant. Since a store can be modified
2846 // during type-legalization (the value being stored may need legalization),
2847 // return empty Results here to indicate that we don't really make any
2848 // changes in the custom lowering.
2849 if (N->getOpcode() != ISD::STORE)
2850 return TargetLowering::LowerOperationWrapper(N, Results, DAG);
2851}
2852
2853void
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002854HexagonTargetLowering::ReplaceNodeResults(SDNode *N,
2855 SmallVectorImpl<SDValue> &Results,
2856 SelectionDAG &DAG) const {
2857 const SDLoc &dl(N);
2858 switch (N->getOpcode()) {
2859 case ISD::SRL:
2860 case ISD::SRA:
2861 case ISD::SHL:
2862 return;
2863 case ISD::BITCAST:
2864 // Handle a bitcast from v8i1 to i8.
2865 if (N->getValueType(0) == MVT::i8) {
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002866 SDValue P = getInstr(Hexagon::C2_tfrpr, dl, MVT::i32,
2867 N->getOperand(0), DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002868 Results.push_back(P);
2869 }
2870 break;
2871 }
2872}
2873
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002874/// Returns relocation base for the given PIC jumptable.
2875SDValue
2876HexagonTargetLowering::getPICJumpTableRelocBase(SDValue Table,
2877 SelectionDAG &DAG) const {
2878 int Idx = cast<JumpTableSDNode>(Table)->getIndex();
2879 EVT VT = Table.getValueType();
2880 SDValue T = DAG.getTargetJumpTable(Idx, VT, HexagonII::MO_PCREL);
2881 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Table), VT, T);
2882}
2883
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002884//===----------------------------------------------------------------------===//
2885// Inline Assembly Support
2886//===----------------------------------------------------------------------===//
2887
Krzysztof Parzyszekca3b5322016-05-18 14:34:51 +00002888TargetLowering::ConstraintType
2889HexagonTargetLowering::getConstraintType(StringRef Constraint) const {
2890 if (Constraint.size() == 1) {
2891 switch (Constraint[0]) {
2892 case 'q':
2893 case 'v':
2894 if (Subtarget.useHVXOps())
Krzysztof Parzyszek3ad0d012017-07-21 17:51:27 +00002895 return C_RegisterClass;
2896 break;
2897 case 'a':
2898 return C_RegisterClass;
2899 default:
Krzysztof Parzyszekca3b5322016-05-18 14:34:51 +00002900 break;
2901 }
2902 }
2903 return TargetLowering::getConstraintType(Constraint);
2904}
2905
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002906std::pair<unsigned, const TargetRegisterClass*>
Eric Christopher11e4df72015-02-26 22:38:43 +00002907HexagonTargetLowering::getRegForInlineAsmConstraint(
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00002908 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002909
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002910 if (Constraint.size() == 1) {
2911 switch (Constraint[0]) {
2912 case 'r': // R0-R31
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002913 switch (VT.SimpleTy) {
2914 default:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002915 return {0u, nullptr};
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002916 case MVT::i1:
2917 case MVT::i8:
2918 case MVT::i16:
2919 case MVT::i32:
2920 case MVT::f32:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002921 return {0u, &Hexagon::IntRegsRegClass};
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002922 case MVT::i64:
2923 case MVT::f64:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002924 return {0u, &Hexagon::DoubleRegsRegClass};
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002925 }
Krzysztof Parzyszek3ad0d012017-07-21 17:51:27 +00002926 break;
2927 case 'a': // M0-M1
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002928 if (VT != MVT::i32)
2929 return {0u, nullptr};
2930 return {0u, &Hexagon::ModRegsRegClass};
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002931 case 'q': // q0-q3
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002932 switch (VT.getSizeInBits()) {
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002933 default:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002934 return {0u, nullptr};
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002935 case 512:
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002936 case 1024:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002937 return {0u, &Hexagon::HvxQRRegClass};
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002938 }
Krzysztof Parzyszek3ad0d012017-07-21 17:51:27 +00002939 break;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002940 case 'v': // V0-V31
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002941 switch (VT.getSizeInBits()) {
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002942 default:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002943 return {0u, nullptr};
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002944 case 512:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002945 return {0u, &Hexagon::HvxVRRegClass};
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002946 case 1024:
Krzysztof Parzyszekd8b780d2018-06-20 13:56:09 +00002947 if (Subtarget.hasV60Ops() && Subtarget.useHVX128BOps())
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002948 return {0u, &Hexagon::HvxVRRegClass};
2949 return {0u, &Hexagon::HvxWRRegClass};
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002950 case 2048:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002951 return {0u, &Hexagon::HvxWRRegClass};
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002952 }
Krzysztof Parzyszek3ad0d012017-07-21 17:51:27 +00002953 break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002954 default:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002955 return {0u, nullptr};
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002956 }
2957 }
2958
Eric Christopher11e4df72015-02-26 22:38:43 +00002959 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002960}
2961
Sirish Pande69295b82012-05-10 20:20:25 +00002962/// isFPImmLegal - Returns true if the target can instruction select the
2963/// specified FP immediate natively. If false, the legalizer will
2964/// materialize the FP immediate as a load from a constant pool.
Adhemerval Zanella664c1ef2019-03-18 18:40:07 +00002965bool HexagonTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT,
2966 bool ForCodeSize) const {
Krzysztof Parzyszek6bfc6572018-10-19 17:31:11 +00002967 return true;
Sirish Pande69295b82012-05-10 20:20:25 +00002968}
2969
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002970/// isLegalAddressingMode - Return true if the addressing mode represented by
2971/// AM is legal for this target, for a load/store of the specified type.
Mehdi Amini0cdec1e2015-07-09 02:09:40 +00002972bool HexagonTargetLowering::isLegalAddressingMode(const DataLayout &DL,
2973 const AddrMode &AM, Type *Ty,
Jonas Paulsson024e3192017-07-21 11:59:37 +00002974 unsigned AS, Instruction *I) const {
Krzysztof Parzyszeked4e7822016-08-03 15:06:18 +00002975 if (Ty->isSized()) {
2976 // When LSR detects uses of the same base address to access different
2977 // types (e.g. unions), it will assume a conservative type for these
2978 // uses:
2979 // LSR Use: Kind=Address of void in addrspace(4294967295), ...
2980 // The type Ty passed here would then be "void". Skip the alignment
2981 // checks, but do not return false right away, since that confuses
2982 // LSR into crashing.
2983 unsigned A = DL.getABITypeAlignment(Ty);
2984 // The base offset must be a multiple of the alignment.
2985 if ((AM.BaseOffs % A) != 0)
2986 return false;
2987 // The shifted offset must fit in 11 bits.
2988 if (!isInt<11>(AM.BaseOffs >> Log2_32(A)))
2989 return false;
2990 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002991
2992 // No global is ever allowed as a base.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002993 if (AM.BaseGV)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002994 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002995
2996 int Scale = AM.Scale;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00002997 if (Scale < 0)
2998 Scale = -Scale;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002999 switch (Scale) {
3000 case 0: // No scale reg, "r+i", "r", or just "i".
3001 break;
3002 default: // No scaled addressing mode.
3003 return false;
3004 }
3005 return true;
3006}
3007
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00003008/// Return true if folding a constant offset with the given GlobalAddress is
3009/// legal. It is frequently not legal in PIC relocation models.
3010bool HexagonTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA)
3011 const {
3012 return HTM.getRelocationModel() == Reloc::Static;
3013}
3014
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003015/// isLegalICmpImmediate - Return true if the specified immediate is legal
3016/// icmp immediate, that is the target has icmp instructions which can compare
3017/// a register against the immediate without having to materialize the
3018/// immediate into a register.
3019bool HexagonTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
3020 return Imm >= -512 && Imm <= 511;
3021}
3022
3023/// IsEligibleForTailCallOptimization - Check whether the call is eligible
3024/// for tail call optimization. Targets which want to do tail call
3025/// optimization should implement this function.
3026bool HexagonTargetLowering::IsEligibleForTailCallOptimization(
3027 SDValue Callee,
3028 CallingConv::ID CalleeCC,
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00003029 bool IsVarArg,
3030 bool IsCalleeStructRet,
3031 bool IsCallerStructRet,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003032 const SmallVectorImpl<ISD::OutputArg> &Outs,
3033 const SmallVectorImpl<SDValue> &OutVals,
3034 const SmallVectorImpl<ISD::InputArg> &Ins,
3035 SelectionDAG& DAG) const {
Matthias Braunf1caa282017-12-15 22:22:58 +00003036 const Function &CallerF = DAG.getMachineFunction().getFunction();
3037 CallingConv::ID CallerCC = CallerF.getCallingConv();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003038 bool CCMatch = CallerCC == CalleeCC;
3039
3040 // ***************************************************************************
3041 // Look for obvious safe cases to perform tail call optimization that do not
3042 // require ABI changes.
3043 // ***************************************************************************
3044
3045 // If this is a tail call via a function pointer, then don't do it!
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00003046 if (!isa<GlobalAddressSDNode>(Callee) &&
3047 !isa<ExternalSymbolSDNode>(Callee)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003048 return false;
3049 }
3050
Krzysztof Parzyszek0ba97542016-08-19 15:02:18 +00003051 // Do not optimize if the calling conventions do not match and the conventions
3052 // used are not C or Fast.
3053 if (!CCMatch) {
3054 bool R = (CallerCC == CallingConv::C || CallerCC == CallingConv::Fast);
3055 bool E = (CalleeCC == CallingConv::C || CalleeCC == CallingConv::Fast);
3056 // If R & E, then ok.
3057 if (!R || !E)
3058 return false;
3059 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003060
3061 // Do not tail call optimize vararg calls.
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00003062 if (IsVarArg)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003063 return false;
3064
3065 // Also avoid tail call optimization if either caller or callee uses struct
3066 // return semantics.
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00003067 if (IsCalleeStructRet || IsCallerStructRet)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003068 return false;
3069
3070 // In addition to the cases above, we also disable Tail Call Optimization if
3071 // the calling convention code that at least one outgoing argument needs to
3072 // go on the stack. We cannot check that here because at this point that
3073 // information is not available.
3074 return true;
3075}
Colin LeMahieu025f8602014-12-08 21:19:18 +00003076
Krzysztof Parzyszek3e409e12016-08-02 18:34:31 +00003077/// Returns the target specific optimal type for load and store operations as
3078/// a result of memset, memcpy, and memmove lowering.
3079///
3080/// If DstAlign is zero that means it's safe to destination alignment can
3081/// satisfy any constraint. Similarly if SrcAlign is zero it means there isn't
3082/// a need to check it against alignment requirement, probably because the
3083/// source does not need to be loaded. If 'IsMemset' is true, that means it's
3084/// expanding a memset. If 'ZeroMemset' is true, that means it's a memset of
3085/// zero. 'MemcpyStrSrc' indicates whether the memcpy source is constant so it
3086/// does not need to be loaded. It returns EVT::Other if the type should be
3087/// determined using generic target-independent logic.
3088EVT HexagonTargetLowering::getOptimalMemOpType(uint64_t Size,
3089 unsigned DstAlign, unsigned SrcAlign, bool IsMemset, bool ZeroMemset,
Sjoerd Meijer180f1ae2019-04-30 08:38:12 +00003090 bool MemcpyStrSrc, const AttributeList &FuncAttributes) const {
Krzysztof Parzyszek3e409e12016-08-02 18:34:31 +00003091
3092 auto Aligned = [](unsigned GivenA, unsigned MinA) -> bool {
3093 return (GivenA % MinA) == 0;
3094 };
3095
3096 if (Size >= 8 && Aligned(DstAlign, 8) && (IsMemset || Aligned(SrcAlign, 8)))
3097 return MVT::i64;
3098 if (Size >= 4 && Aligned(DstAlign, 4) && (IsMemset || Aligned(SrcAlign, 4)))
3099 return MVT::i32;
3100 if (Size >= 2 && Aligned(DstAlign, 2) && (IsMemset || Aligned(SrcAlign, 2)))
3101 return MVT::i16;
3102
3103 return MVT::Other;
3104}
3105
Simon Pilgrim4e0648a2019-06-12 17:14:03 +00003106bool HexagonTargetLowering::allowsMisalignedMemoryAccesses(
3107 EVT VT, unsigned AS, unsigned Align, MachineMemOperand::Flags Flags,
3108 bool *Fast) const {
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +00003109 if (Fast)
3110 *Fast = false;
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00003111 return Subtarget.isHVXVectorType(VT.getSimpleVT());
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +00003112}
3113
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003114std::pair<const TargetRegisterClass*, uint8_t>
3115HexagonTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI,
3116 MVT VT) const {
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00003117 if (Subtarget.isHVXVectorType(VT, true)) {
3118 unsigned BitWidth = VT.getSizeInBits();
3119 unsigned VecWidth = Subtarget.getVectorLength() * 8;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003120
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00003121 if (VT.getVectorElementType() == MVT::i1)
3122 return std::make_pair(&Hexagon::HvxQRRegClass, 1);
3123 if (BitWidth == VecWidth)
3124 return std::make_pair(&Hexagon::HvxVRRegClass, 1);
3125 assert(BitWidth == 2 * VecWidth);
3126 return std::make_pair(&Hexagon::HvxWRRegClass, 1);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003127 }
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00003128
3129 return TargetLowering::findRepresentativeClass(TRI, VT);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003130}
3131
Krzysztof Parzyszekf0705442018-11-02 14:17:47 +00003132bool HexagonTargetLowering::shouldReduceLoadWidth(SDNode *Load,
3133 ISD::LoadExtType ExtTy, EVT NewVT) const {
Sanjay Patel0a515592018-11-10 20:05:31 +00003134 // TODO: This may be worth removing. Check regression tests for diffs.
3135 if (!TargetLoweringBase::shouldReduceLoadWidth(Load, ExtTy, NewVT))
3136 return false;
3137
Krzysztof Parzyszekf0705442018-11-02 14:17:47 +00003138 auto *L = cast<LoadSDNode>(Load);
3139 std::pair<SDValue,int> BO = getBaseAndOffset(L->getBasePtr());
3140 // Small-data object, do not shrink.
3141 if (BO.first.getOpcode() == HexagonISD::CONST32_GP)
3142 return false;
3143 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(BO.first)) {
3144 auto &HTM = static_cast<const HexagonTargetMachine&>(getTargetMachine());
3145 const auto *GO = dyn_cast_or_null<const GlobalObject>(GA->getGlobal());
3146 return !GO || !HTM.getObjFileLowering()->isGlobalInSmallSection(GO, HTM);
3147 }
3148 return true;
3149}
3150
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003151Value *HexagonTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
3152 AtomicOrdering Ord) const {
3153 BasicBlock *BB = Builder.GetInsertBlock();
3154 Module *M = BB->getParent()->getParent();
Matt Arsenault39508332019-01-22 18:18:02 +00003155 auto PT = cast<PointerType>(Addr->getType());
3156 Type *Ty = PT->getElementType();
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003157 unsigned SZ = Ty->getPrimitiveSizeInBits();
3158 assert((SZ == 32 || SZ == 64) && "Only 32/64-bit atomic loads supported");
3159 Intrinsic::ID IntID = (SZ == 32) ? Intrinsic::hexagon_L2_loadw_locked
3160 : Intrinsic::hexagon_L4_loadd_locked;
James Y Knight7976eb52019-02-01 20:43:25 +00003161 Function *Fn = Intrinsic::getDeclaration(M, IntID);
Matt Arsenault39508332019-01-22 18:18:02 +00003162
3163 PointerType *NewPtrTy
3164 = Builder.getIntNTy(SZ)->getPointerTo(PT->getAddressSpace());
3165 Addr = Builder.CreateBitCast(Addr, NewPtrTy);
3166
Matt Arsenault39508332019-01-22 18:18:02 +00003167 Value *Call = Builder.CreateCall(Fn, Addr, "larx");
3168
3169 return Builder.CreateBitCast(Call, Ty);
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003170}
3171
3172/// Perform a store-conditional operation to Addr. Return the status of the
3173/// store. This should be 0 if the store succeeded, non-zero otherwise.
3174Value *HexagonTargetLowering::emitStoreConditional(IRBuilder<> &Builder,
3175 Value *Val, Value *Addr, AtomicOrdering Ord) const {
3176 BasicBlock *BB = Builder.GetInsertBlock();
3177 Module *M = BB->getParent()->getParent();
3178 Type *Ty = Val->getType();
3179 unsigned SZ = Ty->getPrimitiveSizeInBits();
Matt Arsenault39508332019-01-22 18:18:02 +00003180
3181 Type *CastTy = Builder.getIntNTy(SZ);
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003182 assert((SZ == 32 || SZ == 64) && "Only 32/64-bit atomic stores supported");
3183 Intrinsic::ID IntID = (SZ == 32) ? Intrinsic::hexagon_S2_storew_locked
3184 : Intrinsic::hexagon_S4_stored_locked;
James Y Knight7976eb52019-02-01 20:43:25 +00003185 Function *Fn = Intrinsic::getDeclaration(M, IntID);
Matt Arsenault39508332019-01-22 18:18:02 +00003186
3187 unsigned AS = Addr->getType()->getPointerAddressSpace();
3188 Addr = Builder.CreateBitCast(Addr, CastTy->getPointerTo(AS));
3189 Val = Builder.CreateBitCast(Val, CastTy);
3190
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003191 Value *Call = Builder.CreateCall(Fn, {Addr, Val}, "stcx");
3192 Value *Cmp = Builder.CreateICmpEQ(Call, Builder.getInt32(0), "");
3193 Value *Ext = Builder.CreateZExt(Cmp, Type::getInt32Ty(M->getContext()));
3194 return Ext;
3195}
3196
Ahmed Bougacha52468672015-09-11 17:08:28 +00003197TargetLowering::AtomicExpansionKind
3198HexagonTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const {
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003199 // Do not expand loads and stores that don't exceed 64 bits.
Ahmed Bougacha52468672015-09-11 17:08:28 +00003200 return LI->getType()->getPrimitiveSizeInBits() > 64
Tim Northoverf520eff2015-12-02 18:12:57 +00003201 ? AtomicExpansionKind::LLOnly
Ahmed Bougacha52468672015-09-11 17:08:28 +00003202 : AtomicExpansionKind::None;
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003203}
3204
3205bool HexagonTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const {
3206 // Do not expand loads and stores that don't exceed 64 bits.
3207 return SI->getValueOperand()->getType()->getPrimitiveSizeInBits() > 64;
3208}
Krzysztof Parzyszekf228c952016-06-22 16:07:10 +00003209
Alex Bradbury79518b02018-09-19 14:51:42 +00003210TargetLowering::AtomicExpansionKind
3211HexagonTargetLowering::shouldExpandAtomicCmpXchgInIR(
3212 AtomicCmpXchgInst *AI) const {
Krzysztof Parzyszekf228c952016-06-22 16:07:10 +00003213 const DataLayout &DL = AI->getModule()->getDataLayout();
3214 unsigned Size = DL.getTypeStoreSize(AI->getCompareOperand()->getType());
Alex Bradbury79518b02018-09-19 14:51:42 +00003215 if (Size >= 4 && Size <= 8)
3216 return AtomicExpansionKind::LLSC;
3217 return AtomicExpansionKind::None;
Krzysztof Parzyszekf228c952016-06-22 16:07:10 +00003218}