blob: 61cac288e48545b2aff853302d24b3cde1a1583d [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)
Daniel Sanders0c476112019-08-15 19:22:08 +0000289 Register PredR = MRI.createVirtualRegister(&Hexagon::PredRegsRegClass);
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000290 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);
Daniel Sanders0c476112019-08-15 19:22:08 +0000739 Register VReg = MRI.createVirtualRegister(RC);
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000740 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);
Krzysztof Parzyszek8e987702019-08-15 19:20:09 +0000873 MVT OpTy = ty(Op1);
874 const SDLoc &dl(Op);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000875
Krzysztof Parzyszek8e987702019-08-15 19:20:09 +0000876 if (OpTy == MVT::v2i16 || OpTy == MVT::v4i8) {
877 MVT ElemTy = OpTy.getVectorElementType();
878 assert(ElemTy.isScalarInteger());
879 MVT WideTy = MVT::getVectorVT(MVT::getIntegerVT(2*ElemTy.getSizeInBits()),
880 OpTy.getVectorNumElements());
881 // Generate (trunc (select (_, sext, sext))).
882 return DAG.getSExtOrTrunc(
883 DAG.getSelect(dl, WideTy, PredOp,
884 DAG.getSExtOrTrunc(Op1, dl, WideTy),
885 DAG.getSExtOrTrunc(Op2, dl, WideTy)),
886 dl, OpTy);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000887 }
888
889 return SDValue();
890}
891
Krzysztof Parzyszek91ff5c62017-08-01 13:12:53 +0000892static Constant *convert_i1_to_i8(const Constant *ConstVal) {
893 SmallVector<Constant *, 128> NewConst;
894 const ConstantVector *CV = dyn_cast<ConstantVector>(ConstVal);
895 if (!CV)
896 return nullptr;
897
898 LLVMContext &Ctx = ConstVal->getContext();
899 IRBuilder<> IRB(Ctx);
900 unsigned NumVectorElements = CV->getNumOperands();
901 assert(isPowerOf2_32(NumVectorElements) &&
902 "conversion only supported for pow2 VectorSize!");
903
904 for (unsigned i = 0; i < NumVectorElements / 8; ++i) {
905 uint8_t x = 0;
906 for (unsigned j = 0; j < 8; ++j) {
907 uint8_t y = CV->getOperand(i * 8 + j)->getUniqueInteger().getZExtValue();
908 x |= y << (7 - j);
909 }
910 assert((x == 0 || x == 255) && "Either all 0's or all 1's expected!");
911 NewConst.push_back(IRB.getInt8(x));
912 }
913 return ConstantVector::get(NewConst);
914}
915
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000916SDValue
Sirish Pande69295b82012-05-10 20:20:25 +0000917HexagonTargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
918 EVT ValTy = Op.getValueType();
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000919 ConstantPoolSDNode *CPN = cast<ConstantPoolSDNode>(Op);
Krzysztof Parzyszek91ff5c62017-08-01 13:12:53 +0000920 Constant *CVal = nullptr;
921 bool isVTi1Type = false;
922 if (const Constant *ConstVal = dyn_cast<Constant>(CPN->getConstVal())) {
923 Type *CValTy = ConstVal->getType();
924 if (CValTy->isVectorTy() &&
925 CValTy->getVectorElementType()->isIntegerTy(1)) {
926 CVal = convert_i1_to_i8(ConstVal);
927 isVTi1Type = (CVal != nullptr);
928 }
929 }
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000930 unsigned Align = CPN->getAlignment();
Rafael Espindola405e25a2016-06-26 22:24:01 +0000931 bool IsPositionIndependent = isPositionIndependent();
932 unsigned char TF = IsPositionIndependent ? HexagonII::MO_PCREL : 0;
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000933
Ron Lieberman822ee882016-08-13 23:41:11 +0000934 unsigned Offset = 0;
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000935 SDValue T;
936 if (CPN->isMachineConstantPoolEntry())
Ron Lieberman822ee882016-08-13 23:41:11 +0000937 T = DAG.getTargetConstantPool(CPN->getMachineCPVal(), ValTy, Align, Offset,
938 TF);
Krzysztof Parzyszek91ff5c62017-08-01 13:12:53 +0000939 else if (isVTi1Type)
940 T = DAG.getTargetConstantPool(CVal, ValTy, Align, Offset, TF);
Sirish Pande69295b82012-05-10 20:20:25 +0000941 else
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +0000942 T = DAG.getTargetConstantPool(CPN->getConstVal(), ValTy, Align, Offset, TF);
Ron Lieberman822ee882016-08-13 23:41:11 +0000943
944 assert(cast<ConstantPoolSDNode>(T)->getTargetFlags() == TF &&
945 "Inconsistent target flag encountered");
946
Rafael Espindola405e25a2016-06-26 22:24:01 +0000947 if (IsPositionIndependent)
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000948 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), ValTy, T);
949 return DAG.getNode(HexagonISD::CP, SDLoc(Op), ValTy, T);
950}
951
952SDValue
953HexagonTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
954 EVT VT = Op.getValueType();
955 int Idx = cast<JumpTableSDNode>(Op)->getIndex();
Rafael Espindola405e25a2016-06-26 22:24:01 +0000956 if (isPositionIndependent()) {
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000957 SDValue T = DAG.getTargetJumpTable(Idx, VT, HexagonII::MO_PCREL);
958 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), VT, T);
959 }
960
961 SDValue T = DAG.getTargetJumpTable(Idx, VT);
962 return DAG.getNode(HexagonISD::JT, SDLoc(Op), VT, T);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000963}
964
965SDValue
966HexagonTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000967 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000968 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +0000969 MachineFrameInfo &MFI = MF.getFrameInfo();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000970 MFI.setReturnAddressIsTaken(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000971
Bill Wendling908bf812014-01-06 00:43:20 +0000972 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +0000973 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +0000974
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000975 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000976 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000977 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
978 if (Depth) {
979 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000980 SDValue Offset = DAG.getConstant(4, dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000981 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
982 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Justin Lebar9c375812016-07-15 18:27:10 +0000983 MachinePointerInfo());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000984 }
985
986 // Return LR, which contains the return address. Mark it an implicit live-in.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000987 unsigned Reg = MF.addLiveIn(HRI.getRARegister(), getRegClassFor(MVT::i32));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000988 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
989}
990
991SDValue
992HexagonTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000993 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Matthias Braun941a7052016-07-28 18:40:00 +0000994 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000995 MFI.setFrameAddressIsTaken(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000996
997 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000998 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000999 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1000 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001001 HRI.getFrameRegister(), VT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001002 while (Depth--)
1003 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
Justin Lebar9c375812016-07-15 18:27:10 +00001004 MachinePointerInfo());
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001005 return FrameAddr;
1006}
1007
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001008SDValue
1009HexagonTargetLowering::LowerATOMIC_FENCE(SDValue Op, SelectionDAG& DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001010 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001011 return DAG.getNode(HexagonISD::BARRIER, dl, MVT::Other, Op.getOperand(0));
1012}
1013
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001014SDValue
1015HexagonTargetLowering::LowerGLOBALADDRESS(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001016 SDLoc dl(Op);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001017 auto *GAN = cast<GlobalAddressSDNode>(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00001018 auto PtrVT = getPointerTy(DAG.getDataLayout());
Krzysztof Parzyszek96a28412018-01-30 18:10:27 +00001019 auto *GV = GAN->getGlobal();
1020 int64_t Offset = GAN->getOffset();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001021
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001022 auto &HLOF = *HTM.getObjFileLowering();
1023 Reloc::Model RM = HTM.getRelocationModel();
1024
1025 if (RM == Reloc::Static) {
1026 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, Offset);
Peter Collingbourne67335642016-10-24 19:23:39 +00001027 const GlobalObject *GO = GV->getBaseObject();
Krzysztof Parzyszek44e180b2018-05-14 21:01:56 +00001028 if (GO && Subtarget.useSmallData() && HLOF.isGlobalInSmallSection(GO, HTM))
Krzysztof Parzyszek96a28412018-01-30 18:10:27 +00001029 return DAG.getNode(HexagonISD::CONST32_GP, dl, PtrVT, GA);
1030 return DAG.getNode(HexagonISD::CONST32, dl, PtrVT, GA);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001031 }
1032
Krzysztof Parzyszek96a28412018-01-30 18:10:27 +00001033 bool UsePCRel = getTargetMachine().shouldAssumeDSOLocal(*GV->getParent(), GV);
1034 if (UsePCRel) {
1035 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, Offset,
1036 HexagonII::MO_PCREL);
1037 return DAG.getNode(HexagonISD::AT_PCREL, dl, PtrVT, GA);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001038 }
1039
Krzysztof Parzyszek96a28412018-01-30 18:10:27 +00001040 // Use GOT index.
1041 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
1042 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, HexagonII::MO_GOT);
1043 SDValue Off = DAG.getConstant(Offset, dl, MVT::i32);
1044 return DAG.getNode(HexagonISD::AT_GOT, dl, PtrVT, GOT, GA, Off);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001045}
1046
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001047// Specifies that for loads and stores VT can be promoted to PromotedLdStVT.
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001048SDValue
1049HexagonTargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
1050 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001051 SDLoc dl(Op);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001052 EVT PtrVT = getPointerTy(DAG.getDataLayout());
1053
1054 Reloc::Model RM = HTM.getRelocationModel();
1055 if (RM == Reloc::Static) {
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001056 SDValue A = DAG.getTargetBlockAddress(BA, PtrVT);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001057 return DAG.getNode(HexagonISD::CONST32_GP, dl, PtrVT, A);
1058 }
1059
1060 SDValue A = DAG.getTargetBlockAddress(BA, PtrVT, 0, HexagonII::MO_PCREL);
1061 return DAG.getNode(HexagonISD::AT_PCREL, dl, PtrVT, A);
1062}
1063
1064SDValue
1065HexagonTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op, SelectionDAG &DAG)
1066 const {
1067 EVT PtrVT = getPointerTy(DAG.getDataLayout());
1068 SDValue GOTSym = DAG.getTargetExternalSymbol(HEXAGON_GOT_SYM_NAME, PtrVT,
1069 HexagonII::MO_PCREL);
1070 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), PtrVT, GOTSym);
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001071}
1072
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001073SDValue
1074HexagonTargetLowering::GetDynamicTLSAddr(SelectionDAG &DAG, SDValue Chain,
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001075 GlobalAddressSDNode *GA, SDValue Glue, EVT PtrVT, unsigned ReturnReg,
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001076 unsigned char OperandFlags) const {
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001077 MachineFunction &MF = DAG.getMachineFunction();
1078 MachineFrameInfo &MFI = MF.getFrameInfo();
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001079 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1080 SDLoc dl(GA);
1081 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
1082 GA->getValueType(0),
1083 GA->getOffset(),
1084 OperandFlags);
1085 // Create Operands for the call.The Operands should have the following:
1086 // 1. Chain SDValue
1087 // 2. Callee which in this case is the Global address value.
1088 // 3. Registers live into the call.In this case its R0, as we
1089 // have just one argument to be passed.
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001090 // 4. Glue.
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001091 // Note: The order is important.
1092
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001093 const auto &HRI = *Subtarget.getRegisterInfo();
1094 const uint32_t *Mask = HRI.getCallPreservedMask(MF, CallingConv::C);
1095 assert(Mask && "Missing call preserved mask for calling convention");
1096 SDValue Ops[] = { Chain, TGA, DAG.getRegister(Hexagon::R0, PtrVT),
1097 DAG.getRegisterMask(Mask), Glue };
1098 Chain = DAG.getNode(HexagonISD::CALL, dl, NodeTys, Ops);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001099
1100 // Inform MFI that function has calls.
Matthias Braun941a7052016-07-28 18:40:00 +00001101 MFI.setAdjustsStack(true);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001102
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001103 Glue = Chain.getValue(1);
1104 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Glue);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001105}
1106
1107//
1108// Lower using the intial executable model for TLS addresses
1109//
1110SDValue
1111HexagonTargetLowering::LowerToTLSInitialExecModel(GlobalAddressSDNode *GA,
1112 SelectionDAG &DAG) const {
1113 SDLoc dl(GA);
1114 int64_t Offset = GA->getOffset();
1115 auto PtrVT = getPointerTy(DAG.getDataLayout());
1116
1117 // Get the thread pointer.
1118 SDValue TP = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Hexagon::UGP, PtrVT);
1119
Rafael Espindola405e25a2016-06-26 22:24:01 +00001120 bool IsPositionIndependent = isPositionIndependent();
1121 unsigned char TF =
1122 IsPositionIndependent ? HexagonII::MO_IEGOT : HexagonII::MO_IE;
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001123
1124 // First generate the TLS symbol address
1125 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT,
1126 Offset, TF);
1127
1128 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1129
Rafael Espindola405e25a2016-06-26 22:24:01 +00001130 if (IsPositionIndependent) {
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001131 // Generate the GOT pointer in case of position independent code
1132 SDValue GOT = LowerGLOBAL_OFFSET_TABLE(Sym, DAG);
1133
1134 // Add the TLS Symbol address to GOT pointer.This gives
1135 // GOT relative relocation for the symbol.
1136 Sym = DAG.getNode(ISD::ADD, dl, PtrVT, GOT, Sym);
1137 }
1138
1139 // Load the offset value for TLS symbol.This offset is relative to
1140 // thread pointer.
Justin Lebar9c375812016-07-15 18:27:10 +00001141 SDValue LoadOffset =
1142 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Sym, MachinePointerInfo());
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001143
1144 // Address of the thread local variable is the add of thread
1145 // pointer and the offset of the variable.
1146 return DAG.getNode(ISD::ADD, dl, PtrVT, TP, LoadOffset);
1147}
1148
1149//
1150// Lower using the local executable model for TLS addresses
1151//
1152SDValue
1153HexagonTargetLowering::LowerToTLSLocalExecModel(GlobalAddressSDNode *GA,
1154 SelectionDAG &DAG) const {
1155 SDLoc dl(GA);
1156 int64_t Offset = GA->getOffset();
1157 auto PtrVT = getPointerTy(DAG.getDataLayout());
1158
1159 // Get the thread pointer.
1160 SDValue TP = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Hexagon::UGP, PtrVT);
1161 // Generate the TLS symbol address
1162 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT, Offset,
1163 HexagonII::MO_TPREL);
1164 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1165
1166 // Address of the thread local variable is the add of thread
1167 // pointer and the offset of the variable.
1168 return DAG.getNode(ISD::ADD, dl, PtrVT, TP, Sym);
1169}
1170
1171//
1172// Lower using the general dynamic model for TLS addresses
1173//
1174SDValue
1175HexagonTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
1176 SelectionDAG &DAG) const {
1177 SDLoc dl(GA);
1178 int64_t Offset = GA->getOffset();
1179 auto PtrVT = getPointerTy(DAG.getDataLayout());
1180
1181 // First generate the TLS symbol address
1182 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT, Offset,
1183 HexagonII::MO_GDGOT);
1184
1185 // Then, generate the GOT pointer
1186 SDValue GOT = LowerGLOBAL_OFFSET_TABLE(TGA, DAG);
1187
1188 // Add the TLS symbol and the GOT pointer
1189 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1190 SDValue Chain = DAG.getNode(ISD::ADD, dl, PtrVT, GOT, Sym);
1191
1192 // Copy over the argument to R0
1193 SDValue InFlag;
1194 Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, Hexagon::R0, Chain, InFlag);
1195 InFlag = Chain.getValue(1);
1196
Krzysztof Parzyszeka7503832017-05-02 18:15:33 +00001197 unsigned Flags =
1198 static_cast<const HexagonSubtarget &>(DAG.getSubtarget()).useLongCalls()
1199 ? HexagonII::MO_GDPLT | HexagonII::HMOTF_ConstExtended
1200 : HexagonII::MO_GDPLT;
1201
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001202 return GetDynamicTLSAddr(DAG, Chain, GA, InFlag, PtrVT,
Krzysztof Parzyszeka7503832017-05-02 18:15:33 +00001203 Hexagon::R0, Flags);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001204}
1205
1206//
1207// Lower TLS addresses.
1208//
1209// For now for dynamic models, we only support the general dynamic model.
1210//
1211SDValue
1212HexagonTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1213 SelectionDAG &DAG) const {
1214 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1215
1216 switch (HTM.getTLSModel(GA->getGlobal())) {
1217 case TLSModel::GeneralDynamic:
1218 case TLSModel::LocalDynamic:
1219 return LowerToTLSGeneralDynamicModel(GA, DAG);
1220 case TLSModel::InitialExec:
1221 return LowerToTLSInitialExecModel(GA, DAG);
1222 case TLSModel::LocalExec:
1223 return LowerToTLSLocalExecModel(GA, DAG);
1224 }
1225 llvm_unreachable("Bogus TLS model");
1226}
1227
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001228//===----------------------------------------------------------------------===//
1229// TargetLowering Implementation
1230//===----------------------------------------------------------------------===//
1231
Eric Christopherd737b762015-02-02 22:11:36 +00001232HexagonTargetLowering::HexagonTargetLowering(const TargetMachine &TM,
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001233 const HexagonSubtarget &ST)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001234 : TargetLowering(TM), HTM(static_cast<const HexagonTargetMachine&>(TM)),
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001235 Subtarget(ST) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001236 auto &HRI = *Subtarget.getRegisterInfo();
Sirish Pande69295b82012-05-10 20:20:25 +00001237
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001238 setPrefLoopAlignment(4);
1239 setPrefFunctionAlignment(4);
1240 setMinFunctionAlignment(2);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001241 setStackPointerRegisterToSaveRestore(HRI.getStackRegister());
Krzysztof Parzyszekb3e50ac2018-01-05 20:41:50 +00001242 setBooleanContents(TargetLoweringBase::UndefinedBooleanContent);
1243 setBooleanVectorContents(TargetLoweringBase::UndefinedBooleanContent);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001244
Krzysztof Parzyszekf228c952016-06-22 16:07:10 +00001245 setMaxAtomicSizeInBitsSupported(64);
1246 setMinCmpXchgSizeInBits(32);
1247
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001248 if (EnableHexSDNodeSched)
1249 setSchedulingPreference(Sched::VLIW);
1250 else
1251 setSchedulingPreference(Sched::Source);
1252
1253 // Limits for inline expansion of memcpy/memmove
1254 MaxStoresPerMemcpy = MaxStoresPerMemcpyCL;
1255 MaxStoresPerMemcpyOptSize = MaxStoresPerMemcpyOptSizeCL;
1256 MaxStoresPerMemmove = MaxStoresPerMemmoveCL;
1257 MaxStoresPerMemmoveOptSize = MaxStoresPerMemmoveOptSizeCL;
1258 MaxStoresPerMemset = MaxStoresPerMemsetCL;
1259 MaxStoresPerMemsetOptSize = MaxStoresPerMemsetOptSizeCL;
1260
1261 //
1262 // Set up register classes.
1263 //
1264
1265 addRegisterClass(MVT::i1, &Hexagon::PredRegsRegClass);
1266 addRegisterClass(MVT::v2i1, &Hexagon::PredRegsRegClass); // bbbbaaaa
1267 addRegisterClass(MVT::v4i1, &Hexagon::PredRegsRegClass); // ddccbbaa
1268 addRegisterClass(MVT::v8i1, &Hexagon::PredRegsRegClass); // hgfedcba
1269 addRegisterClass(MVT::i32, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001270 addRegisterClass(MVT::v2i16, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00001271 addRegisterClass(MVT::v4i8, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001272 addRegisterClass(MVT::i64, &Hexagon::DoubleRegsRegClass);
1273 addRegisterClass(MVT::v8i8, &Hexagon::DoubleRegsRegClass);
1274 addRegisterClass(MVT::v4i16, &Hexagon::DoubleRegsRegClass);
1275 addRegisterClass(MVT::v2i32, &Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001276
Krzysztof Parzyszek6bfc6572018-10-19 17:31:11 +00001277 addRegisterClass(MVT::f32, &Hexagon::IntRegsRegClass);
1278 addRegisterClass(MVT::f64, &Hexagon::DoubleRegsRegClass);
Sirish Pande69295b82012-05-10 20:20:25 +00001279
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001280 //
1281 // Handling of scalar operations.
1282 //
1283 // All operations default to "legal", except:
1284 // - indexed loads and stores (pre-/post-incremented),
1285 // - ANY_EXTEND_VECTOR_INREG, ATOMIC_CMP_SWAP_WITH_SUCCESS, CONCAT_VECTORS,
1286 // ConstantFP, DEBUGTRAP, FCEIL, FCOPYSIGN, FEXP, FEXP2, FFLOOR, FGETSIGN,
1287 // FLOG, FLOG2, FLOG10, FMAXNUM, FMINNUM, FNEARBYINT, FRINT, FROUND, TRAP,
1288 // FTRUNC, PREFETCH, SIGN_EXTEND_VECTOR_INREG, ZERO_EXTEND_VECTOR_INREG,
1289 // which default to "expand" for at least one type.
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001290
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001291 // Misc operations.
Krzysztof Parzyszek75c2ca32018-08-09 18:03:45 +00001292 setOperationAction(ISD::ConstantFP, MVT::f32, Legal);
1293 setOperationAction(ISD::ConstantFP, MVT::f64, Legal);
1294 setOperationAction(ISD::TRAP, MVT::Other, Legal);
1295 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
1296 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
1297 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
1298 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
1299 setOperationAction(ISD::INLINEASM, MVT::Other, Custom);
Craig Topper784929d2019-02-08 20:48:56 +00001300 setOperationAction(ISD::INLINEASM_BR, MVT::Other, Custom);
Krzysztof Parzyszek75c2ca32018-08-09 18:03:45 +00001301 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
1302 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
1303 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
1304 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
1305 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
1306 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
1307 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001308
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001309 // Custom legalize GlobalAddress nodes into CONST32.
1310 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001311 setOperationAction(ISD::GlobalAddress, MVT::i8, Custom);
1312 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001313
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001314 // Hexagon needs to optimize cases with negative constants.
Krzysztof Parzyszek23bcf062018-04-19 14:24:31 +00001315 setOperationAction(ISD::SETCC, MVT::i8, Custom);
1316 setOperationAction(ISD::SETCC, MVT::i16, Custom);
1317 setOperationAction(ISD::SETCC, MVT::v4i8, Custom);
1318 setOperationAction(ISD::SETCC, MVT::v2i16, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001319
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001320 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
1321 setOperationAction(ISD::VASTART, MVT::Other, Custom);
1322 setOperationAction(ISD::VAEND, MVT::Other, Expand);
1323 setOperationAction(ISD::VAARG, MVT::Other, Expand);
Krzysztof Parzyszekf6088122018-03-02 18:35:57 +00001324 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001325
1326 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
1327 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
1328 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
1329
1330 if (EmitJumpTables)
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001331 setMinimumJumpTableEntries(MinimumJumpTables);
Krzysztof Parzyszeka61f7da2016-01-13 21:43:13 +00001332 else
Evandro Menezes56c45e92019-06-19 16:12:01 +00001333 setMinimumJumpTableEntries(std::numeric_limits<unsigned>::max());
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001334 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001335
Krzysztof Parzyszek82d284c2018-06-12 21:51:49 +00001336 setOperationAction(ISD::ABS, MVT::i32, Legal);
1337 setOperationAction(ISD::ABS, MVT::i64, Legal);
1338
Krzysztof Parzyszek0b6187c2018-06-01 14:00:32 +00001339 // Hexagon has A4_addp_c and A4_subp_c that take and generate a carry bit,
1340 // but they only operate on i64.
Krzysztof Parzyszek2c4487d2015-04-13 20:37:01 +00001341 for (MVT VT : MVT::integer_valuetypes()) {
Krzysztof Parzyszek5abf80c2019-07-01 15:50:09 +00001342 setOperationAction(ISD::UADDO, VT, Custom);
1343 setOperationAction(ISD::USUBO, VT, Custom);
Krzysztof Parzyszek0b6187c2018-06-01 14:00:32 +00001344 setOperationAction(ISD::SADDO, VT, Expand);
1345 setOperationAction(ISD::SSUBO, VT, Expand);
1346 setOperationAction(ISD::ADDCARRY, VT, Expand);
1347 setOperationAction(ISD::SUBCARRY, VT, Expand);
Krzysztof Parzyszek2c4487d2015-04-13 20:37:01 +00001348 }
Krzysztof Parzyszek0b6187c2018-06-01 14:00:32 +00001349 setOperationAction(ISD::ADDCARRY, MVT::i64, Custom);
1350 setOperationAction(ISD::SUBCARRY, MVT::i64, Custom);
Krzysztof Parzyszek2c4487d2015-04-13 20:37:01 +00001351
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001352 setOperationAction(ISD::CTLZ, MVT::i8, Promote);
1353 setOperationAction(ISD::CTLZ, MVT::i16, Promote);
1354 setOperationAction(ISD::CTTZ, MVT::i8, Promote);
1355 setOperationAction(ISD::CTTZ, MVT::i16, Promote);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001356
Krzysztof Parzyszek6bfc6572018-10-19 17:31:11 +00001357 // Popcount can count # of 1s in i64 but returns i32.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001358 setOperationAction(ISD::CTPOP, MVT::i8, Promote);
1359 setOperationAction(ISD::CTPOP, MVT::i16, Promote);
1360 setOperationAction(ISD::CTPOP, MVT::i32, Promote);
Krzysztof Parzyszekaf5ff652017-02-23 15:02:09 +00001361 setOperationAction(ISD::CTPOP, MVT::i64, Legal);
1362
1363 setOperationAction(ISD::BITREVERSE, MVT::i32, Legal);
1364 setOperationAction(ISD::BITREVERSE, MVT::i64, Legal);
1365 setOperationAction(ISD::BSWAP, MVT::i32, Legal);
1366 setOperationAction(ISD::BSWAP, MVT::i64, Legal);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001367
Krzysztof Parzyszek30c42e22018-12-20 16:39:20 +00001368 setOperationAction(ISD::FSHL, MVT::i32, Legal);
1369 setOperationAction(ISD::FSHL, MVT::i64, Legal);
1370 setOperationAction(ISD::FSHR, MVT::i32, Legal);
1371 setOperationAction(ISD::FSHR, MVT::i64, Legal);
1372
Benjamin Kramer62460692015-04-25 14:46:53 +00001373 for (unsigned IntExpOp :
Krzysztof Parzyszekaafb8c22018-06-05 12:49:19 +00001374 {ISD::SDIV, ISD::UDIV, ISD::SREM, ISD::UREM,
1375 ISD::SDIVREM, ISD::UDIVREM, ISD::ROTL, ISD::ROTR,
1376 ISD::SHL_PARTS, ISD::SRA_PARTS, ISD::SRL_PARTS,
1377 ISD::SMUL_LOHI, ISD::UMUL_LOHI}) {
1378 for (MVT VT : MVT::integer_valuetypes())
1379 setOperationAction(IntExpOp, VT, Expand);
Benjamin Kramer62460692015-04-25 14:46:53 +00001380 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001381
Benjamin Kramer62460692015-04-25 14:46:53 +00001382 for (unsigned FPExpOp :
1383 {ISD::FDIV, ISD::FREM, ISD::FSQRT, ISD::FSIN, ISD::FCOS, ISD::FSINCOS,
1384 ISD::FPOW, ISD::FCOPYSIGN}) {
Krzysztof Parzyszekaafb8c22018-06-05 12:49:19 +00001385 for (MVT VT : MVT::fp_valuetypes())
1386 setOperationAction(FPExpOp, VT, Expand);
Benjamin Kramer62460692015-04-25 14:46:53 +00001387 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001388
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001389 // No extending loads from i32.
1390 for (MVT VT : MVT::integer_valuetypes()) {
1391 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand);
1392 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand);
1393 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand);
1394 }
1395 // Turn FP truncstore into trunc + store.
1396 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Michael Kuperstein2bc3d4d2016-08-18 20:08:15 +00001397 // Turn FP extload into load/fpextend.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001398 for (MVT VT : MVT::fp_valuetypes())
1399 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001400
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001401 // Expand BR_CC and SELECT_CC for all integer and fp types.
1402 for (MVT VT : MVT::integer_valuetypes()) {
1403 setOperationAction(ISD::BR_CC, VT, Expand);
1404 setOperationAction(ISD::SELECT_CC, VT, Expand);
1405 }
1406 for (MVT VT : MVT::fp_valuetypes()) {
1407 setOperationAction(ISD::BR_CC, VT, Expand);
1408 setOperationAction(ISD::SELECT_CC, VT, Expand);
1409 }
1410 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001411
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001412 //
1413 // Handling of vector operations.
1414 //
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001415
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001416 // Set the action for vector operations to "expand", then override it with
1417 // either "custom" or "legal" for specific cases.
Craig Topper26260942015-10-18 05:15:34 +00001418 static const unsigned VectExpOps[] = {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001419 // Integer arithmetic:
Amaury Sechet84674112018-06-01 13:21:33 +00001420 ISD::ADD, ISD::SUB, ISD::MUL, ISD::SDIV, ISD::UDIV,
1421 ISD::SREM, ISD::UREM, ISD::SDIVREM, ISD::UDIVREM, ISD::SADDO,
1422 ISD::UADDO, ISD::SSUBO, ISD::USUBO, ISD::SMUL_LOHI, ISD::UMUL_LOHI,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001423 // Logical/bit:
1424 ISD::AND, ISD::OR, ISD::XOR, ISD::ROTL, ISD::ROTR,
Craig Topper33772c52016-04-28 03:34:31 +00001425 ISD::CTPOP, ISD::CTLZ, ISD::CTTZ,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001426 // Floating point arithmetic/math functions:
1427 ISD::FADD, ISD::FSUB, ISD::FMUL, ISD::FMA, ISD::FDIV,
1428 ISD::FREM, ISD::FNEG, ISD::FABS, ISD::FSQRT, ISD::FSIN,
Craig Topperf6d4dc52017-05-30 15:27:55 +00001429 ISD::FCOS, ISD::FPOW, ISD::FLOG, ISD::FLOG2,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001430 ISD::FLOG10, ISD::FEXP, ISD::FEXP2, ISD::FCEIL, ISD::FTRUNC,
1431 ISD::FRINT, ISD::FNEARBYINT, ISD::FROUND, ISD::FFLOOR,
1432 ISD::FMINNUM, ISD::FMAXNUM, ISD::FSINCOS,
1433 // Misc:
Krzysztof Parzyszek046da742016-10-27 14:30:16 +00001434 ISD::BR_CC, ISD::SELECT_CC, ISD::ConstantPool,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001435 // Vector:
1436 ISD::BUILD_VECTOR, ISD::SCALAR_TO_VECTOR,
1437 ISD::EXTRACT_VECTOR_ELT, ISD::INSERT_VECTOR_ELT,
1438 ISD::EXTRACT_SUBVECTOR, ISD::INSERT_SUBVECTOR,
1439 ISD::CONCAT_VECTORS, ISD::VECTOR_SHUFFLE
1440 };
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001441
1442 for (MVT VT : MVT::vector_valuetypes()) {
Benjamin Kramer62460692015-04-25 14:46:53 +00001443 for (unsigned VectExpOp : VectExpOps)
1444 setOperationAction(VectExpOp, VT, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001445
Krzysztof Parzyszeka696b1b2016-09-08 17:42:14 +00001446 // Expand all extending loads and truncating stores:
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001447 for (MVT TargetVT : MVT::vector_valuetypes()) {
Krzysztof Parzyszeka696b1b2016-09-08 17:42:14 +00001448 if (TargetVT == VT)
1449 continue;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001450 setLoadExtAction(ISD::EXTLOAD, TargetVT, VT, Expand);
Krzysztof Parzyszeka696b1b2016-09-08 17:42:14 +00001451 setLoadExtAction(ISD::ZEXTLOAD, TargetVT, VT, Expand);
1452 setLoadExtAction(ISD::SEXTLOAD, TargetVT, VT, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001453 setTruncStoreAction(VT, TargetVT, Expand);
1454 }
1455
Krzysztof Parzyszek046da742016-10-27 14:30:16 +00001456 // Normalize all inputs to SELECT to be vectors of i32.
1457 if (VT.getVectorElementType() != MVT::i32) {
1458 MVT VT32 = MVT::getVectorVT(MVT::i32, VT.getSizeInBits()/32);
1459 setOperationAction(ISD::SELECT, VT, Promote);
1460 AddPromotedToType(ISD::SELECT, VT, VT32);
1461 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001462 setOperationAction(ISD::SRA, VT, Custom);
1463 setOperationAction(ISD::SHL, VT, Custom);
1464 setOperationAction(ISD::SRL, VT, Custom);
1465 }
1466
Krzysztof Parzyszek64e5d7d2017-10-20 19:33:12 +00001467 // Extending loads from (native) vectors of i8 into (native) vectors of i16
1468 // are legal.
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001469 setLoadExtAction(ISD::EXTLOAD, MVT::v2i16, MVT::v2i8, Legal);
Krzysztof Parzyszek64e5d7d2017-10-20 19:33:12 +00001470 setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i16, MVT::v2i8, Legal);
1471 setLoadExtAction(ISD::SEXTLOAD, MVT::v2i16, MVT::v2i8, Legal);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001472 setLoadExtAction(ISD::EXTLOAD, MVT::v4i16, MVT::v4i8, Legal);
Krzysztof Parzyszek64e5d7d2017-10-20 19:33:12 +00001473 setLoadExtAction(ISD::ZEXTLOAD, MVT::v4i16, MVT::v4i8, Legal);
1474 setLoadExtAction(ISD::SEXTLOAD, MVT::v4i16, MVT::v4i8, Legal);
1475
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001476 // Types natively supported:
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001477 for (MVT NativeVT : {MVT::v8i1, MVT::v4i1, MVT::v2i1, MVT::v4i8,
1478 MVT::v8i8, MVT::v2i16, MVT::v4i16, MVT::v2i32}) {
Benjamin Kramer62460692015-04-25 14:46:53 +00001479 setOperationAction(ISD::BUILD_VECTOR, NativeVT, Custom);
1480 setOperationAction(ISD::EXTRACT_VECTOR_ELT, NativeVT, Custom);
1481 setOperationAction(ISD::INSERT_VECTOR_ELT, NativeVT, Custom);
1482 setOperationAction(ISD::EXTRACT_SUBVECTOR, NativeVT, Custom);
1483 setOperationAction(ISD::INSERT_SUBVECTOR, NativeVT, Custom);
1484 setOperationAction(ISD::CONCAT_VECTORS, NativeVT, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001485
Benjamin Kramer62460692015-04-25 14:46:53 +00001486 setOperationAction(ISD::ADD, NativeVT, Legal);
1487 setOperationAction(ISD::SUB, NativeVT, Legal);
1488 setOperationAction(ISD::MUL, NativeVT, Legal);
1489 setOperationAction(ISD::AND, NativeVT, Legal);
1490 setOperationAction(ISD::OR, NativeVT, Legal);
1491 setOperationAction(ISD::XOR, NativeVT, Legal);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001492 }
1493
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00001494 // Custom lower unaligned loads.
Krzysztof Parzyszek1df70592018-08-08 17:00:09 +00001495 // Also, for both loads and stores, verify the alignment of the address
1496 // in case it is a compile-time constant. This is a usability feature to
1497 // provide a meaningful error message to users.
1498 for (MVT VT : {MVT::i16, MVT::i32, MVT::v4i8, MVT::i64, MVT::v8i8,
1499 MVT::v2i16, MVT::v4i16, MVT::v2i32}) {
1500 setOperationAction(ISD::LOAD, VT, Custom);
1501 setOperationAction(ISD::STORE, VT, Custom);
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00001502 }
1503
Krzysztof Parzyszek99152912018-03-16 15:03:37 +00001504 for (MVT VT : {MVT::v2i16, MVT::v4i8, MVT::v2i32, MVT::v4i16, MVT::v2i32}) {
1505 setCondCodeAction(ISD::SETLT, VT, Expand);
1506 setCondCodeAction(ISD::SETLE, VT, Expand);
1507 setCondCodeAction(ISD::SETULT, VT, Expand);
1508 setCondCodeAction(ISD::SETULE, VT, Expand);
1509 }
1510
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001511 // Custom-lower bitcasts from i8 to v8i1.
1512 setOperationAction(ISD::BITCAST, MVT::i8, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001513 setOperationAction(ISD::SETCC, MVT::v2i16, Custom);
Krzysztof Parzyszek8e987702019-08-15 19:20:09 +00001514 setOperationAction(ISD::VSELECT, MVT::v4i8, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001515 setOperationAction(ISD::VSELECT, MVT::v2i16, Custom);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001516 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i8, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001517 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
1518 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00001519
Krzysztof Parzyszek6bfc6572018-10-19 17:31:11 +00001520 // V5+.
1521 setOperationAction(ISD::FMA, MVT::f64, Expand);
1522 setOperationAction(ISD::FADD, MVT::f64, Expand);
1523 setOperationAction(ISD::FSUB, MVT::f64, Expand);
1524 setOperationAction(ISD::FMUL, MVT::f64, Expand);
Krzysztof Parzyszekbd8ef4b2016-08-19 13:34:31 +00001525
Krzysztof Parzyszek6bfc6572018-10-19 17:31:11 +00001526 setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
1527 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001528
Krzysztof Parzyszek6bfc6572018-10-19 17:31:11 +00001529 setOperationAction(ISD::FP_TO_UINT, MVT::i1, Promote);
1530 setOperationAction(ISD::FP_TO_UINT, MVT::i8, Promote);
1531 setOperationAction(ISD::FP_TO_UINT, MVT::i16, Promote);
1532 setOperationAction(ISD::FP_TO_SINT, MVT::i1, Promote);
1533 setOperationAction(ISD::FP_TO_SINT, MVT::i8, Promote);
1534 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
1535 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Promote);
1536 setOperationAction(ISD::UINT_TO_FP, MVT::i8, Promote);
1537 setOperationAction(ISD::UINT_TO_FP, MVT::i16, Promote);
1538 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Promote);
1539 setOperationAction(ISD::SINT_TO_FP, MVT::i8, Promote);
1540 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001541
1542 // Handling of indexed loads/stores: default is "expand".
1543 //
Brendon Cahoone5ed5632018-05-18 18:14:44 +00001544 for (MVT VT : {MVT::i8, MVT::i16, MVT::i32, MVT::i64, MVT::f32, MVT::f64,
1545 MVT::v2i16, MVT::v2i32, MVT::v4i8, MVT::v4i16, MVT::v8i8}) {
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +00001546 setIndexedLoadAction(ISD::POST_INC, VT, Legal);
1547 setIndexedStoreAction(ISD::POST_INC, VT, Legal);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001548 }
1549
Krzysztof Parzyszek545a68c2018-12-05 21:01:07 +00001550 // Subtarget-specific operation actions.
1551 //
1552 if (Subtarget.hasV60Ops()) {
Krzysztof Parzyszek30c42e22018-12-20 16:39:20 +00001553 setOperationAction(ISD::ROTL, MVT::i32, Legal);
1554 setOperationAction(ISD::ROTL, MVT::i64, Legal);
1555 setOperationAction(ISD::ROTR, MVT::i32, Legal);
1556 setOperationAction(ISD::ROTR, MVT::i64, Legal);
Krzysztof Parzyszek545a68c2018-12-05 21:01:07 +00001557 }
1558 if (Subtarget.hasV66Ops()) {
1559 setOperationAction(ISD::FADD, MVT::f64, Legal);
1560 setOperationAction(ISD::FSUB, MVT::f64, Legal);
1561 }
1562
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00001563 if (Subtarget.useHVXOps())
1564 initializeHVXLowering();
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00001565
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001566 computeRegisterProperties(&HRI);
1567
1568 //
1569 // Library calls for unsupported operations
1570 //
1571 bool FastMath = EnableFastMath;
1572
Benjamin Kramera37c8092015-04-25 14:46:46 +00001573 setLibcallName(RTLIB::SDIV_I32, "__hexagon_divsi3");
1574 setLibcallName(RTLIB::SDIV_I64, "__hexagon_divdi3");
1575 setLibcallName(RTLIB::UDIV_I32, "__hexagon_udivsi3");
1576 setLibcallName(RTLIB::UDIV_I64, "__hexagon_udivdi3");
1577 setLibcallName(RTLIB::SREM_I32, "__hexagon_modsi3");
1578 setLibcallName(RTLIB::SREM_I64, "__hexagon_moddi3");
1579 setLibcallName(RTLIB::UREM_I32, "__hexagon_umodsi3");
1580 setLibcallName(RTLIB::UREM_I64, "__hexagon_umoddi3");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001581
Benjamin Kramera37c8092015-04-25 14:46:46 +00001582 setLibcallName(RTLIB::SINTTOFP_I128_F64, "__hexagon_floattidf");
1583 setLibcallName(RTLIB::SINTTOFP_I128_F32, "__hexagon_floattisf");
1584 setLibcallName(RTLIB::FPTOUINT_F32_I128, "__hexagon_fixunssfti");
1585 setLibcallName(RTLIB::FPTOUINT_F64_I128, "__hexagon_fixunsdfti");
1586 setLibcallName(RTLIB::FPTOSINT_F32_I128, "__hexagon_fixsfti");
1587 setLibcallName(RTLIB::FPTOSINT_F64_I128, "__hexagon_fixdfti");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001588
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001589 // This is the only fast library function for sqrtd.
1590 if (FastMath)
Benjamin Kramera37c8092015-04-25 14:46:46 +00001591 setLibcallName(RTLIB::SQRT_F64, "__hexagon_fast2_sqrtdf2");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001592
Benjamin Kramera37c8092015-04-25 14:46:46 +00001593 // Prefix is: nothing for "slow-math",
Krzysztof Parzyszek6bfc6572018-10-19 17:31:11 +00001594 // "fast2_" for V5+ fast-math double-precision
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001595 // (actually, keep fast-math and fast-math2 separate for now)
Benjamin Kramera37c8092015-04-25 14:46:46 +00001596 if (FastMath) {
1597 setLibcallName(RTLIB::ADD_F64, "__hexagon_fast_adddf3");
1598 setLibcallName(RTLIB::SUB_F64, "__hexagon_fast_subdf3");
1599 setLibcallName(RTLIB::MUL_F64, "__hexagon_fast_muldf3");
1600 setLibcallName(RTLIB::DIV_F64, "__hexagon_fast_divdf3");
Benjamin Kramera37c8092015-04-25 14:46:46 +00001601 setLibcallName(RTLIB::DIV_F32, "__hexagon_fast_divsf3");
1602 } else {
1603 setLibcallName(RTLIB::ADD_F64, "__hexagon_adddf3");
1604 setLibcallName(RTLIB::SUB_F64, "__hexagon_subdf3");
1605 setLibcallName(RTLIB::MUL_F64, "__hexagon_muldf3");
1606 setLibcallName(RTLIB::DIV_F64, "__hexagon_divdf3");
1607 setLibcallName(RTLIB::DIV_F32, "__hexagon_divsf3");
1608 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001609
Krzysztof Parzyszek6bfc6572018-10-19 17:31:11 +00001610 if (FastMath)
1611 setLibcallName(RTLIB::SQRT_F32, "__hexagon_fast2_sqrtf");
1612 else
1613 setLibcallName(RTLIB::SQRT_F32, "__hexagon_sqrtf");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001614
1615 // These cause problems when the shift amount is non-constant.
1616 setLibcallName(RTLIB::SHL_I128, nullptr);
1617 setLibcallName(RTLIB::SRL_I128, nullptr);
1618 setLibcallName(RTLIB::SRA_I128, nullptr);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001619}
1620
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001621const char* HexagonTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00001622 switch ((HexagonISD::NodeType)Opcode) {
Krzysztof Parzyszek0b6187c2018-06-01 14:00:32 +00001623 case HexagonISD::ADDC: return "HexagonISD::ADDC";
1624 case HexagonISD::SUBC: return "HexagonISD::SUBC";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001625 case HexagonISD::ALLOCA: return "HexagonISD::ALLOCA";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001626 case HexagonISD::AT_GOT: return "HexagonISD::AT_GOT";
1627 case HexagonISD::AT_PCREL: return "HexagonISD::AT_PCREL";
1628 case HexagonISD::BARRIER: return "HexagonISD::BARRIER";
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00001629 case HexagonISD::CALL: return "HexagonISD::CALL";
1630 case HexagonISD::CALLnr: return "HexagonISD::CALLnr";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001631 case HexagonISD::CALLR: return "HexagonISD::CALLR";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001632 case HexagonISD::COMBINE: return "HexagonISD::COMBINE";
1633 case HexagonISD::CONST32_GP: return "HexagonISD::CONST32_GP";
1634 case HexagonISD::CONST32: return "HexagonISD::CONST32";
1635 case HexagonISD::CP: return "HexagonISD::CP";
1636 case HexagonISD::DCFETCH: return "HexagonISD::DCFETCH";
1637 case HexagonISD::EH_RETURN: return "HexagonISD::EH_RETURN";
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001638 case HexagonISD::TSTBIT: return "HexagonISD::TSTBIT";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001639 case HexagonISD::EXTRACTU: return "HexagonISD::EXTRACTU";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001640 case HexagonISD::INSERT: return "HexagonISD::INSERT";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001641 case HexagonISD::JT: return "HexagonISD::JT";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001642 case HexagonISD::RET_FLAG: return "HexagonISD::RET_FLAG";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001643 case HexagonISD::TC_RETURN: return "HexagonISD::TC_RETURN";
Krzysztof Parzyszekf85dd9f2017-07-10 20:16:44 +00001644 case HexagonISD::VASL: return "HexagonISD::VASL";
1645 case HexagonISD::VASR: return "HexagonISD::VASR";
1646 case HexagonISD::VLSR: return "HexagonISD::VLSR";
1647 case HexagonISD::VSPLAT: return "HexagonISD::VSPLAT";
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001648 case HexagonISD::VEXTRACTW: return "HexagonISD::VEXTRACTW";
1649 case HexagonISD::VINSERTW0: return "HexagonISD::VINSERTW0";
1650 case HexagonISD::VROR: return "HexagonISD::VROR";
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +00001651 case HexagonISD::READCYCLE: return "HexagonISD::READCYCLE";
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00001652 case HexagonISD::VZERO: return "HexagonISD::VZERO";
Krzysztof Parzyszek41a24b72018-04-20 19:38:37 +00001653 case HexagonISD::VSPLATW: return "HexagonISD::VSPLATW";
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001654 case HexagonISD::D2P: return "HexagonISD::D2P";
1655 case HexagonISD::P2D: return "HexagonISD::P2D";
1656 case HexagonISD::V2Q: return "HexagonISD::V2Q";
1657 case HexagonISD::Q2V: return "HexagonISD::Q2V";
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00001658 case HexagonISD::QCAT: return "HexagonISD::QCAT";
Krzysztof Parzyszek69f1d7e2018-02-06 14:16:52 +00001659 case HexagonISD::QTRUE: return "HexagonISD::QTRUE";
1660 case HexagonISD::QFALSE: return "HexagonISD::QFALSE";
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001661 case HexagonISD::TYPECAST: return "HexagonISD::TYPECAST";
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00001662 case HexagonISD::VALIGN: return "HexagonISD::VALIGN";
Krzysztof Parzyszekad83ce42018-02-14 20:46:06 +00001663 case HexagonISD::VALIGNADDR: return "HexagonISD::VALIGNADDR";
Matthias Braund04893f2015-05-07 21:33:59 +00001664 case HexagonISD::OP_END: break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001665 }
Matthias Braund04893f2015-05-07 21:33:59 +00001666 return nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001667}
1668
Krzysztof Parzyszek1df70592018-08-08 17:00:09 +00001669void
1670HexagonTargetLowering::validateConstPtrAlignment(SDValue Ptr, const SDLoc &dl,
1671 unsigned NeedAlign) const {
1672 auto *CA = dyn_cast<ConstantSDNode>(Ptr);
1673 if (!CA)
1674 return;
1675 unsigned Addr = CA->getZExtValue();
1676 unsigned HaveAlign = Addr != 0 ? 1u << countTrailingZeros(Addr) : NeedAlign;
1677 if (HaveAlign < NeedAlign) {
1678 std::string ErrMsg;
1679 raw_string_ostream O(ErrMsg);
1680 O << "Misaligned constant address: " << format_hex(Addr, 10)
1681 << " has alignment " << HaveAlign
1682 << ", but the memory access requires " << NeedAlign;
1683 if (DebugLoc DL = dl.getDebugLoc())
1684 DL.print(O << ", at ");
1685 report_fatal_error(O.str());
1686 }
1687}
1688
Krzysztof Parzyszekdc7a5572018-03-29 13:52:46 +00001689// Bit-reverse Load Intrinsic: Check if the instruction is a bit reverse load
1690// intrinsic.
1691static bool isBrevLdIntrinsic(const Value *Inst) {
1692 unsigned ID = cast<IntrinsicInst>(Inst)->getIntrinsicID();
1693 return (ID == Intrinsic::hexagon_L2_loadrd_pbr ||
1694 ID == Intrinsic::hexagon_L2_loadri_pbr ||
1695 ID == Intrinsic::hexagon_L2_loadrh_pbr ||
1696 ID == Intrinsic::hexagon_L2_loadruh_pbr ||
1697 ID == Intrinsic::hexagon_L2_loadrb_pbr ||
1698 ID == Intrinsic::hexagon_L2_loadrub_pbr);
1699}
1700
1701// Bit-reverse Load Intrinsic :Crawl up and figure out the object from previous
1702// instruction. So far we only handle bitcast, extract value and bit reverse
1703// load intrinsic instructions. Should we handle CGEP ?
1704static Value *getBrevLdObject(Value *V) {
1705 if (Operator::getOpcode(V) == Instruction::ExtractValue ||
1706 Operator::getOpcode(V) == Instruction::BitCast)
1707 V = cast<Operator>(V)->getOperand(0);
1708 else if (isa<IntrinsicInst>(V) && isBrevLdIntrinsic(V))
1709 V = cast<Instruction>(V)->getOperand(0);
1710 return V;
1711}
1712
1713// Bit-reverse Load Intrinsic: For a PHI Node return either an incoming edge or
1714// a back edge. If the back edge comes from the intrinsic itself, the incoming
1715// edge is returned.
1716static Value *returnEdge(const PHINode *PN, Value *IntrBaseVal) {
1717 const BasicBlock *Parent = PN->getParent();
1718 int Idx = -1;
1719 for (unsigned i = 0, e = PN->getNumIncomingValues(); i < e; ++i) {
1720 BasicBlock *Blk = PN->getIncomingBlock(i);
1721 // Determine if the back edge is originated from intrinsic.
1722 if (Blk == Parent) {
1723 Value *BackEdgeVal = PN->getIncomingValue(i);
1724 Value *BaseVal;
1725 // Loop over till we return the same Value or we hit the IntrBaseVal.
1726 do {
1727 BaseVal = BackEdgeVal;
1728 BackEdgeVal = getBrevLdObject(BackEdgeVal);
1729 } while ((BaseVal != BackEdgeVal) && (IntrBaseVal != BackEdgeVal));
1730 // If the getBrevLdObject returns IntrBaseVal, we should return the
1731 // incoming edge.
1732 if (IntrBaseVal == BackEdgeVal)
1733 continue;
1734 Idx = i;
1735 break;
1736 } else // Set the node to incoming edge.
1737 Idx = i;
1738 }
1739 assert(Idx >= 0 && "Unexpected index to incoming argument in PHI");
1740 return PN->getIncomingValue(Idx);
1741}
1742
1743// Bit-reverse Load Intrinsic: Figure out the underlying object the base
1744// pointer points to, for the bit-reverse load intrinsic. Setting this to
1745// memoperand might help alias analysis to figure out the dependencies.
1746static Value *getUnderLyingObjectForBrevLdIntr(Value *V) {
1747 Value *IntrBaseVal = V;
1748 Value *BaseVal;
1749 // Loop over till we return the same Value, implies we either figure out
1750 // the object or we hit a PHI
1751 do {
1752 BaseVal = V;
1753 V = getBrevLdObject(V);
1754 } while (BaseVal != V);
1755
1756 // Identify the object from PHINode.
1757 if (const PHINode *PN = dyn_cast<PHINode>(V))
1758 return returnEdge(PN, IntrBaseVal);
1759 // For non PHI nodes, the object is the last value returned by getBrevLdObject
1760 else
1761 return V;
1762}
1763
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001764/// Given an intrinsic, checks if on the target the intrinsic will need to map
1765/// to a MemIntrinsicNode (touches memory). If this is the case, it returns
1766/// true and store the intrinsic information into the IntrinsicInfo that was
1767/// passed to the function.
1768bool HexagonTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
1769 const CallInst &I,
Matt Arsenault7d7adf42017-12-14 22:34:10 +00001770 MachineFunction &MF,
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001771 unsigned Intrinsic) const {
1772 switch (Intrinsic) {
Krzysztof Parzyszekdc7a5572018-03-29 13:52:46 +00001773 case Intrinsic::hexagon_L2_loadrd_pbr:
1774 case Intrinsic::hexagon_L2_loadri_pbr:
1775 case Intrinsic::hexagon_L2_loadrh_pbr:
1776 case Intrinsic::hexagon_L2_loadruh_pbr:
1777 case Intrinsic::hexagon_L2_loadrb_pbr:
1778 case Intrinsic::hexagon_L2_loadrub_pbr: {
1779 Info.opc = ISD::INTRINSIC_W_CHAIN;
1780 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
1781 auto &Cont = I.getCalledFunction()->getParent()->getContext();
1782 // The intrinsic function call is of the form { ElTy, i8* }
1783 // @llvm.hexagon.L2.loadXX.pbr(i8*, i32). The pointer and memory access type
1784 // should be derived from ElTy.
James Y Knight62df5ee2019-01-10 16:07:20 +00001785 Type *ElTy = I.getCalledFunction()->getReturnType()->getStructElementType(0);
1786 Info.memVT = MVT::getVT(ElTy);
Krzysztof Parzyszekdc7a5572018-03-29 13:52:46 +00001787 llvm::Value *BasePtrVal = I.getOperand(0);
1788 Info.ptrVal = getUnderLyingObjectForBrevLdIntr(BasePtrVal);
1789 // The offset value comes through Modifier register. For now, assume the
1790 // offset is 0.
1791 Info.offset = 0;
Guillaume Chateletc97a3d12019-08-05 11:02:05 +00001792 Info.align =
1793 MaybeAlign(DL.getABITypeAlignment(Info.memVT.getTypeForEVT(Cont)));
Krzysztof Parzyszekdc7a5572018-03-29 13:52:46 +00001794 Info.flags = MachineMemOperand::MOLoad;
1795 return true;
1796 }
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001797 case Intrinsic::hexagon_V6_vgathermw:
1798 case Intrinsic::hexagon_V6_vgathermw_128B:
1799 case Intrinsic::hexagon_V6_vgathermh:
1800 case Intrinsic::hexagon_V6_vgathermh_128B:
1801 case Intrinsic::hexagon_V6_vgathermhw:
1802 case Intrinsic::hexagon_V6_vgathermhw_128B:
1803 case Intrinsic::hexagon_V6_vgathermwq:
1804 case Intrinsic::hexagon_V6_vgathermwq_128B:
1805 case Intrinsic::hexagon_V6_vgathermhq:
1806 case Intrinsic::hexagon_V6_vgathermhq_128B:
1807 case Intrinsic::hexagon_V6_vgathermhwq:
1808 case Intrinsic::hexagon_V6_vgathermhwq_128B: {
1809 const Module &M = *I.getParent()->getParent()->getParent();
1810 Info.opc = ISD::INTRINSIC_W_CHAIN;
1811 Type *VecTy = I.getArgOperand(1)->getType();
1812 Info.memVT = MVT::getVT(VecTy);
1813 Info.ptrVal = I.getArgOperand(0);
1814 Info.offset = 0;
Guillaume Chateletc97a3d12019-08-05 11:02:05 +00001815 Info.align =
1816 MaybeAlign(M.getDataLayout().getTypeAllocSizeInBits(VecTy) / 8);
Matt Arsenault11171332017-12-14 21:39:51 +00001817 Info.flags = MachineMemOperand::MOLoad |
1818 MachineMemOperand::MOStore |
1819 MachineMemOperand::MOVolatile;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001820 return true;
1821 }
1822 default:
1823 break;
1824 }
1825 return false;
1826}
1827
Roman Lebedev017e2722019-07-24 22:57:22 +00001828bool HexagonTargetLowering::hasBitTest(SDValue X, SDValue Y) const {
1829 return X.getValueType().isScalarInteger(); // 'tstbit'
1830}
1831
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001832bool HexagonTargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00001833 return isTruncateFree(EVT::getEVT(Ty1), EVT::getEVT(Ty2));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001834}
1835
1836bool HexagonTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001837 if (!VT1.isSimple() || !VT2.isSimple())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001838 return false;
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00001839 return VT1.getSimpleVT() == MVT::i64 && VT2.getSimpleVT() == MVT::i32;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001840}
1841
Krzysztof Parzyszekbd8ef4b2016-08-19 13:34:31 +00001842bool HexagonTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
1843 return isOperationLegalOrCustom(ISD::FMA, VT);
1844}
1845
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001846// Should we expand the build vector with shuffles?
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00001847bool HexagonTargetLowering::shouldExpandBuildVectorWithShuffles(EVT VT,
1848 unsigned DefinedValues) const {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001849 return false;
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00001850}
1851
Zvi Rackover1b736822017-07-26 08:06:58 +00001852bool HexagonTargetLowering::isShuffleMaskLegal(ArrayRef<int> Mask,
1853 EVT VT) const {
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00001854 return true;
1855}
1856
Krzysztof Parzyszek5439a702017-12-18 18:21:01 +00001857TargetLoweringBase::LegalizeTypeAction
Craig Topper0b5f8162018-11-05 23:26:13 +00001858HexagonTargetLowering::getPreferredVectorAction(MVT VT) const {
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00001859 if (VT.getVectorNumElements() == 1)
1860 return TargetLoweringBase::TypeScalarizeVector;
1861
1862 // Always widen vectors of i1.
Craig Topper0b5f8162018-11-05 23:26:13 +00001863 MVT ElemTy = VT.getVectorElementType();
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00001864 if (ElemTy == MVT::i1)
1865 return TargetLoweringBase::TypeWidenVector;
1866
Krzysztof Parzyszek5439a702017-12-18 18:21:01 +00001867 if (Subtarget.useHVXOps()) {
1868 // If the size of VT is at least half of the vector length,
1869 // widen the vector. Note: the threshold was not selected in
1870 // any scientific way.
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00001871 ArrayRef<MVT> Tys = Subtarget.getHVXElementTypes();
1872 if (llvm::find(Tys, ElemTy) != Tys.end()) {
1873 unsigned HwWidth = 8*Subtarget.getVectorLength();
1874 unsigned VecWidth = VT.getSizeInBits();
1875 if (VecWidth >= HwWidth/2 && VecWidth < HwWidth)
1876 return TargetLoweringBase::TypeWidenVector;
1877 }
Krzysztof Parzyszek5439a702017-12-18 18:21:01 +00001878 }
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00001879 return TargetLoweringBase::TypeSplitVector;
Krzysztof Parzyszek5439a702017-12-18 18:21:01 +00001880}
1881
Krzysztof Parzyszekad83ce42018-02-14 20:46:06 +00001882std::pair<SDValue, int>
1883HexagonTargetLowering::getBaseAndOffset(SDValue Addr) const {
1884 if (Addr.getOpcode() == ISD::ADD) {
1885 SDValue Op1 = Addr.getOperand(1);
1886 if (auto *CN = dyn_cast<const ConstantSDNode>(Op1.getNode()))
1887 return { Addr.getOperand(0), CN->getSExtValue() };
1888 }
1889 return { Addr, 0 };
1890}
1891
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00001892// Lower a vector shuffle (V1, V2, V3). V1 and V2 are the two vectors
1893// to select data from, V3 is the permutation.
1894SDValue
1895HexagonTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG)
1896 const {
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001897 const auto *SVN = cast<ShuffleVectorSDNode>(Op);
1898 ArrayRef<int> AM = SVN->getMask();
1899 assert(AM.size() <= 8 && "Unexpected shuffle mask");
1900 unsigned VecLen = AM.size();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001901
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001902 MVT VecTy = ty(Op);
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00001903 assert(!Subtarget.isHVXVectorType(VecTy, true) &&
1904 "HVX shuffles should be legal");
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001905 assert(VecTy.getSizeInBits() <= 64 && "Unexpected vector length");
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001906
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001907 SDValue Op0 = Op.getOperand(0);
1908 SDValue Op1 = Op.getOperand(1);
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +00001909 const SDLoc &dl(Op);
1910
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001911 // If the inputs are not the same as the output, bail. This is not an
1912 // error situation, but complicates the handling and the default expansion
1913 // (into BUILD_VECTOR) should be adequate.
1914 if (ty(Op0) != VecTy || ty(Op1) != VecTy)
1915 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001916
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001917 // Normalize the mask so that the first non-negative index comes from
1918 // the first operand.
1919 SmallVector<int,8> Mask(AM.begin(), AM.end());
1920 unsigned F = llvm::find_if(AM, [](int M) { return M >= 0; }) - AM.data();
1921 if (F == AM.size())
1922 return DAG.getUNDEF(VecTy);
1923 if (AM[F] >= int(VecLen)) {
1924 ShuffleVectorSDNode::commuteMask(Mask);
1925 std::swap(Op0, Op1);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001926 }
1927
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001928 // Express the shuffle mask in terms of bytes.
1929 SmallVector<int,8> ByteMask;
1930 unsigned ElemBytes = VecTy.getVectorElementType().getSizeInBits() / 8;
1931 for (unsigned i = 0, e = Mask.size(); i != e; ++i) {
1932 int M = Mask[i];
1933 if (M < 0) {
1934 for (unsigned j = 0; j != ElemBytes; ++j)
1935 ByteMask.push_back(-1);
1936 } else {
1937 for (unsigned j = 0; j != ElemBytes; ++j)
1938 ByteMask.push_back(M*ElemBytes + j);
1939 }
1940 }
1941 assert(ByteMask.size() <= 8);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001942
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001943 // All non-undef (non-negative) indexes are well within [0..127], so they
1944 // fit in a single byte. Build two 64-bit words:
1945 // - MaskIdx where each byte is the corresponding index (for non-negative
1946 // indexes), and 0xFF for negative indexes, and
1947 // - MaskUnd that has 0xFF for each negative index.
1948 uint64_t MaskIdx = 0;
1949 uint64_t MaskUnd = 0;
1950 for (unsigned i = 0, e = ByteMask.size(); i != e; ++i) {
1951 unsigned S = 8*i;
1952 uint64_t M = ByteMask[i] & 0xFF;
1953 if (M == 0xFF)
1954 MaskUnd |= M << S;
1955 MaskIdx |= M << S;
1956 }
1957
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001958 if (ByteMask.size() == 4) {
1959 // Identity.
1960 if (MaskIdx == (0x03020100 | MaskUnd))
1961 return Op0;
1962 // Byte swap.
1963 if (MaskIdx == (0x00010203 | MaskUnd)) {
1964 SDValue T0 = DAG.getBitcast(MVT::i32, Op0);
1965 SDValue T1 = DAG.getNode(ISD::BSWAP, dl, MVT::i32, T0);
1966 return DAG.getBitcast(VecTy, T1);
1967 }
1968
1969 // Byte packs.
1970 SDValue Concat10 = DAG.getNode(HexagonISD::COMBINE, dl,
1971 typeJoin({ty(Op1), ty(Op0)}), {Op1, Op0});
1972 if (MaskIdx == (0x06040200 | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001973 return getInstr(Hexagon::S2_vtrunehb, dl, VecTy, {Concat10}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001974 if (MaskIdx == (0x07050301 | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001975 return getInstr(Hexagon::S2_vtrunohb, dl, VecTy, {Concat10}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001976
1977 SDValue Concat01 = DAG.getNode(HexagonISD::COMBINE, dl,
1978 typeJoin({ty(Op0), ty(Op1)}), {Op0, Op1});
1979 if (MaskIdx == (0x02000604 | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001980 return getInstr(Hexagon::S2_vtrunehb, dl, VecTy, {Concat01}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001981 if (MaskIdx == (0x03010705 | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001982 return getInstr(Hexagon::S2_vtrunohb, dl, VecTy, {Concat01}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001983 }
1984
1985 if (ByteMask.size() == 8) {
1986 // Identity.
1987 if (MaskIdx == (0x0706050403020100ull | MaskUnd))
1988 return Op0;
1989 // Byte swap.
1990 if (MaskIdx == (0x0001020304050607ull | MaskUnd)) {
1991 SDValue T0 = DAG.getBitcast(MVT::i64, Op0);
1992 SDValue T1 = DAG.getNode(ISD::BSWAP, dl, MVT::i64, T0);
1993 return DAG.getBitcast(VecTy, T1);
1994 }
1995
1996 // Halfword picks.
1997 if (MaskIdx == (0x0d0c050409080100ull | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001998 return getInstr(Hexagon::S2_shuffeh, dl, VecTy, {Op1, Op0}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001999 if (MaskIdx == (0x0f0e07060b0a0302ull | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002000 return getInstr(Hexagon::S2_shuffoh, dl, VecTy, {Op1, Op0}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00002001 if (MaskIdx == (0x0d0c090805040100ull | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002002 return getInstr(Hexagon::S2_vtrunewh, dl, VecTy, {Op1, Op0}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00002003 if (MaskIdx == (0x0f0e0b0a07060302ull | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002004 return getInstr(Hexagon::S2_vtrunowh, dl, VecTy, {Op1, Op0}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00002005 if (MaskIdx == (0x0706030205040100ull | MaskUnd)) {
2006 VectorPair P = opSplit(Op0, dl, DAG);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002007 return getInstr(Hexagon::S2_packhl, dl, VecTy, {P.second, P.first}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00002008 }
2009
2010 // Byte packs.
2011 if (MaskIdx == (0x0e060c040a020800ull | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002012 return getInstr(Hexagon::S2_shuffeb, dl, VecTy, {Op1, Op0}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00002013 if (MaskIdx == (0x0f070d050b030901ull | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002014 return getInstr(Hexagon::S2_shuffob, dl, VecTy, {Op1, Op0}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00002015 }
2016
2017 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002018}
2019
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00002020// Create a Hexagon-specific node for shifting a vector by an integer.
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002021SDValue
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00002022HexagonTargetLowering::getVectorShiftByInt(SDValue Op, SelectionDAG &DAG)
2023 const {
Krzysztof Parzyszek1108ee22018-01-31 20:49:24 +00002024 if (auto *BVN = dyn_cast<BuildVectorSDNode>(Op.getOperand(1).getNode())) {
2025 if (SDValue S = BVN->getSplatValue()) {
2026 unsigned NewOpc;
2027 switch (Op.getOpcode()) {
2028 case ISD::SHL:
2029 NewOpc = HexagonISD::VASL;
2030 break;
2031 case ISD::SRA:
2032 NewOpc = HexagonISD::VASR;
2033 break;
2034 case ISD::SRL:
2035 NewOpc = HexagonISD::VLSR;
2036 break;
2037 default:
2038 llvm_unreachable("Unexpected shift opcode");
2039 }
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00002040 return DAG.getNode(NewOpc, SDLoc(Op), ty(Op), Op.getOperand(0), S);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002041 }
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002042 }
2043
Krzysztof Parzyszek1108ee22018-01-31 20:49:24 +00002044 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002045}
2046
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002047SDValue
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00002048HexagonTargetLowering::LowerVECTOR_SHIFT(SDValue Op, SelectionDAG &DAG) const {
2049 return getVectorShiftByInt(Op, DAG);
2050}
2051
2052SDValue
Krzysztof Parzyszek3d671242018-06-12 12:49:36 +00002053HexagonTargetLowering::LowerROTL(SDValue Op, SelectionDAG &DAG) const {
2054 if (isa<ConstantSDNode>(Op.getOperand(1).getNode()))
2055 return Op;
2056 return SDValue();
2057}
2058
2059SDValue
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002060HexagonTargetLowering::LowerBITCAST(SDValue Op, SelectionDAG &DAG) const {
2061 MVT ResTy = ty(Op);
2062 SDValue InpV = Op.getOperand(0);
2063 MVT InpTy = ty(InpV);
2064 assert(ResTy.getSizeInBits() == InpTy.getSizeInBits());
2065 const SDLoc &dl(Op);
2066
2067 // Handle conversion from i8 to v8i1.
2068 if (ResTy == MVT::v8i1) {
2069 SDValue Sc = DAG.getBitcast(tyScalar(InpTy), InpV);
2070 SDValue Ext = DAG.getZExtOrTrunc(Sc, dl, MVT::i32);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002071 return getInstr(Hexagon::C2_tfrrp, dl, ResTy, Ext, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002072 }
2073
2074 return SDValue();
2075}
2076
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002077bool
2078HexagonTargetLowering::getBuildVectorConstInts(ArrayRef<SDValue> Values,
2079 MVT VecTy, SelectionDAG &DAG,
2080 MutableArrayRef<ConstantInt*> Consts) const {
2081 MVT ElemTy = VecTy.getVectorElementType();
2082 unsigned ElemWidth = ElemTy.getSizeInBits();
2083 IntegerType *IntTy = IntegerType::get(*DAG.getContext(), ElemWidth);
2084 bool AllConst = true;
2085
2086 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
2087 SDValue V = Values[i];
2088 if (V.isUndef()) {
2089 Consts[i] = ConstantInt::get(IntTy, 0);
2090 continue;
2091 }
Krzysztof Parzyszek4ef6cff2018-01-11 18:03:23 +00002092 // Make sure to always cast to IntTy.
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002093 if (auto *CN = dyn_cast<ConstantSDNode>(V.getNode())) {
2094 const ConstantInt *CI = CN->getConstantIntValue();
Krzysztof Parzyszek4ef6cff2018-01-11 18:03:23 +00002095 Consts[i] = ConstantInt::get(IntTy, CI->getValue().getSExtValue());
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002096 } else if (auto *CN = dyn_cast<ConstantFPSDNode>(V.getNode())) {
2097 const ConstantFP *CF = CN->getConstantFPValue();
2098 APInt A = CF->getValueAPF().bitcastToAPInt();
2099 Consts[i] = ConstantInt::get(IntTy, A.getZExtValue());
2100 } else {
2101 AllConst = false;
2102 }
2103 }
2104 return AllConst;
2105}
2106
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002107SDValue
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002108HexagonTargetLowering::buildVector32(ArrayRef<SDValue> Elem, const SDLoc &dl,
2109 MVT VecTy, SelectionDAG &DAG) const {
2110 MVT ElemTy = VecTy.getVectorElementType();
2111 assert(VecTy.getVectorNumElements() == Elem.size());
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002112
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002113 SmallVector<ConstantInt*,4> Consts(Elem.size());
2114 bool AllConst = getBuildVectorConstInts(Elem, VecTy, DAG, Consts);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002115
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002116 unsigned First, Num = Elem.size();
2117 for (First = 0; First != Num; ++First)
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002118 if (!isUndef(Elem[First]))
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002119 break;
2120 if (First == Num)
2121 return DAG.getUNDEF(VecTy);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002122
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002123 if (AllConst &&
2124 llvm::all_of(Consts, [](ConstantInt *CI) { return CI->isZero(); }))
2125 return getZero(dl, VecTy, DAG);
2126
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002127 if (ElemTy == MVT::i16) {
2128 assert(Elem.size() == 2);
2129 if (AllConst) {
2130 uint32_t V = (Consts[0]->getZExtValue() & 0xFFFF) |
2131 Consts[1]->getZExtValue() << 16;
2132 return DAG.getBitcast(MVT::v2i16, DAG.getConstant(V, dl, MVT::i32));
Krzysztof Parzyszek89b2d7c2017-07-13 18:17:58 +00002133 }
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002134 SDValue N = getInstr(Hexagon::A2_combine_ll, dl, MVT::i32,
2135 {Elem[1], Elem[0]}, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002136 return DAG.getBitcast(MVT::v2i16, N);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002137 }
2138
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002139 if (ElemTy == MVT::i8) {
2140 // First try generating a constant.
2141 if (AllConst) {
2142 int32_t V = (Consts[0]->getZExtValue() & 0xFF) |
2143 (Consts[1]->getZExtValue() & 0xFF) << 8 |
2144 (Consts[1]->getZExtValue() & 0xFF) << 16 |
2145 Consts[2]->getZExtValue() << 24;
2146 return DAG.getBitcast(MVT::v4i8, DAG.getConstant(V, dl, MVT::i32));
2147 }
2148
2149 // Then try splat.
2150 bool IsSplat = true;
2151 for (unsigned i = 0; i != Num; ++i) {
2152 if (i == First)
2153 continue;
2154 if (Elem[i] == Elem[First] || isUndef(Elem[i]))
2155 continue;
2156 IsSplat = false;
2157 break;
2158 }
2159 if (IsSplat) {
2160 // Legalize the operand to VSPLAT.
2161 SDValue Ext = DAG.getZExtOrTrunc(Elem[First], dl, MVT::i32);
2162 return DAG.getNode(HexagonISD::VSPLAT, dl, VecTy, Ext);
2163 }
2164
2165 // Generate
2166 // (zxtb(Elem[0]) | (zxtb(Elem[1]) << 8)) |
2167 // (zxtb(Elem[2]) | (zxtb(Elem[3]) << 8)) << 16
2168 assert(Elem.size() == 4);
2169 SDValue Vs[4];
2170 for (unsigned i = 0; i != 4; ++i) {
2171 Vs[i] = DAG.getZExtOrTrunc(Elem[i], dl, MVT::i32);
2172 Vs[i] = DAG.getZeroExtendInReg(Vs[i], dl, MVT::i8);
2173 }
2174 SDValue S8 = DAG.getConstant(8, dl, MVT::i32);
2175 SDValue T0 = DAG.getNode(ISD::SHL, dl, MVT::i32, {Vs[1], S8});
2176 SDValue T1 = DAG.getNode(ISD::SHL, dl, MVT::i32, {Vs[3], S8});
2177 SDValue B0 = DAG.getNode(ISD::OR, dl, MVT::i32, {Vs[0], T0});
2178 SDValue B1 = DAG.getNode(ISD::OR, dl, MVT::i32, {Vs[2], T1});
2179
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002180 SDValue R = getInstr(Hexagon::A2_combine_ll, dl, MVT::i32, {B1, B0}, DAG);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002181 return DAG.getBitcast(MVT::v4i8, R);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002182 }
2183
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002184#ifndef NDEBUG
2185 dbgs() << "VecTy: " << EVT(VecTy).getEVTString() << '\n';
2186#endif
2187 llvm_unreachable("Unexpected vector element type");
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002188}
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002189
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002190SDValue
2191HexagonTargetLowering::buildVector64(ArrayRef<SDValue> Elem, const SDLoc &dl,
2192 MVT VecTy, SelectionDAG &DAG) const {
2193 MVT ElemTy = VecTy.getVectorElementType();
2194 assert(VecTy.getVectorNumElements() == Elem.size());
2195
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002196 SmallVector<ConstantInt*,8> Consts(Elem.size());
2197 bool AllConst = getBuildVectorConstInts(Elem, VecTy, DAG, Consts);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002198
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002199 unsigned First, Num = Elem.size();
2200 for (First = 0; First != Num; ++First)
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002201 if (!isUndef(Elem[First]))
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002202 break;
2203 if (First == Num)
2204 return DAG.getUNDEF(VecTy);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002205
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002206 if (AllConst &&
2207 llvm::all_of(Consts, [](ConstantInt *CI) { return CI->isZero(); }))
2208 return getZero(dl, VecTy, DAG);
2209
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002210 // First try splat if possible.
2211 if (ElemTy == MVT::i16) {
2212 bool IsSplat = true;
2213 for (unsigned i = 0; i != Num; ++i) {
2214 if (i == First)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002215 continue;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002216 if (Elem[i] == Elem[First] || isUndef(Elem[i]))
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002217 continue;
2218 IsSplat = false;
2219 break;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002220 }
Krzysztof Parzyszekfb0fcac2017-12-20 20:33:49 +00002221 if (IsSplat) {
2222 // Legalize the operand to VSPLAT.
2223 SDValue Ext = DAG.getZExtOrTrunc(Elem[First], dl, MVT::i32);
2224 return DAG.getNode(HexagonISD::VSPLAT, dl, VecTy, Ext);
2225 }
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002226 }
2227
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002228 // Then try constant.
2229 if (AllConst) {
2230 uint64_t Val = 0;
2231 unsigned W = ElemTy.getSizeInBits();
2232 uint64_t Mask = (ElemTy == MVT::i8) ? 0xFFull
2233 : (ElemTy == MVT::i16) ? 0xFFFFull : 0xFFFFFFFFull;
2234 for (unsigned i = 0; i != Num; ++i)
Krzysztof Parzyszek240df6f2018-01-11 18:30:41 +00002235 Val = (Val << W) | (Consts[Num-1-i]->getZExtValue() & Mask);
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002236 SDValue V0 = DAG.getConstant(Val, dl, MVT::i64);
2237 return DAG.getBitcast(VecTy, V0);
2238 }
2239
2240 // Build two 32-bit vectors and concatenate.
2241 MVT HalfTy = MVT::getVectorVT(ElemTy, Num/2);
2242 SDValue L = (ElemTy == MVT::i32)
2243 ? Elem[0]
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002244 : buildVector32(Elem.take_front(Num/2), dl, HalfTy, DAG);
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002245 SDValue H = (ElemTy == MVT::i32)
2246 ? Elem[1]
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002247 : buildVector32(Elem.drop_front(Num/2), dl, HalfTy, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002248 return DAG.getNode(HexagonISD::COMBINE, dl, VecTy, {H, L});
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002249}
2250
2251SDValue
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002252HexagonTargetLowering::extractVector(SDValue VecV, SDValue IdxV,
2253 const SDLoc &dl, MVT ValTy, MVT ResTy,
2254 SelectionDAG &DAG) const {
2255 MVT VecTy = ty(VecV);
2256 assert(!ValTy.isVector() ||
2257 VecTy.getVectorElementType() == ValTy.getVectorElementType());
2258 unsigned VecWidth = VecTy.getSizeInBits();
2259 unsigned ValWidth = ValTy.getSizeInBits();
2260 unsigned ElemWidth = VecTy.getVectorElementType().getSizeInBits();
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002261 assert((VecWidth % ElemWidth) == 0);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002262 auto *IdxN = dyn_cast<ConstantSDNode>(IdxV);
2263
2264 // Special case for v{8,4,2}i1 (the only boolean vectors legal in Hexagon
2265 // without any coprocessors).
2266 if (ElemWidth == 1) {
2267 assert(VecWidth == VecTy.getVectorNumElements() && "Sanity failure");
2268 assert(VecWidth == 8 || VecWidth == 4 || VecWidth == 2);
2269 // Check if this is an extract of the lowest bit.
2270 if (IdxN) {
2271 // Extracting the lowest bit is a no-op, but it changes the type,
2272 // so it must be kept as an operation to avoid errors related to
2273 // type mismatches.
2274 if (IdxN->isNullValue() && ValTy.getSizeInBits() == 1)
2275 return DAG.getNode(HexagonISD::TYPECAST, dl, MVT::i1, VecV);
2276 }
2277
2278 // If the value extracted is a single bit, use tstbit.
2279 if (ValWidth == 1) {
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002280 SDValue A0 = getInstr(Hexagon::C2_tfrpr, dl, MVT::i32, {VecV}, DAG);
Krzysztof Parzyszek4e075092018-07-25 16:20:59 +00002281 SDValue M0 = DAG.getConstant(8 / VecWidth, dl, MVT::i32);
2282 SDValue I0 = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV, M0);
2283 return DAG.getNode(HexagonISD::TSTBIT, dl, MVT::i1, A0, I0);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002284 }
2285
2286 // Each bool vector (v2i1, v4i1, v8i1) always occupies 8 bits in
2287 // a predicate register. The elements of the vector are repeated
2288 // in the register (if necessary) so that the total number is 8.
2289 // The extracted subvector will need to be expanded in such a way.
2290 unsigned Scale = VecWidth / ValWidth;
2291
2292 // Generate (p2d VecV) >> 8*Idx to move the interesting bytes to
2293 // position 0.
2294 assert(ty(IdxV) == MVT::i32);
Krzysztof Parzyszek528aff32018-10-02 15:05:43 +00002295 unsigned VecRep = 8 / VecWidth;
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002296 SDValue S0 = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV,
Krzysztof Parzyszek528aff32018-10-02 15:05:43 +00002297 DAG.getConstant(8*VecRep, dl, MVT::i32));
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002298 SDValue T0 = DAG.getNode(HexagonISD::P2D, dl, MVT::i64, VecV);
2299 SDValue T1 = DAG.getNode(ISD::SRL, dl, MVT::i64, T0, S0);
2300 while (Scale > 1) {
2301 // The longest possible subvector is at most 32 bits, so it is always
2302 // contained in the low subregister.
2303 T1 = DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::i32, T1);
2304 T1 = expandPredicate(T1, dl, DAG);
2305 Scale /= 2;
2306 }
2307
2308 return DAG.getNode(HexagonISD::D2P, dl, ResTy, T1);
2309 }
2310
2311 assert(VecWidth == 32 || VecWidth == 64);
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002312
2313 // Cast everything to scalar integer types.
2314 MVT ScalarTy = tyScalar(VecTy);
2315 VecV = DAG.getBitcast(ScalarTy, VecV);
2316
2317 SDValue WidthV = DAG.getConstant(ValWidth, dl, MVT::i32);
2318 SDValue ExtV;
2319
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002320 if (IdxN) {
2321 unsigned Off = IdxN->getZExtValue() * ElemWidth;
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002322 if (VecWidth == 64 && ValWidth == 32) {
2323 assert(Off == 0 || Off == 32);
2324 unsigned SubIdx = Off == 0 ? Hexagon::isub_lo : Hexagon::isub_hi;
2325 ExtV = DAG.getTargetExtractSubreg(SubIdx, dl, MVT::i32, VecV);
2326 } else if (Off == 0 && (ValWidth % 8) == 0) {
2327 ExtV = DAG.getZeroExtendInReg(VecV, dl, tyScalar(ValTy));
2328 } else {
2329 SDValue OffV = DAG.getConstant(Off, dl, MVT::i32);
2330 // The return type of EXTRACTU must be the same as the type of the
2331 // input vector.
2332 ExtV = DAG.getNode(HexagonISD::EXTRACTU, dl, ScalarTy,
2333 {VecV, WidthV, OffV});
2334 }
2335 } else {
2336 if (ty(IdxV) != MVT::i32)
2337 IdxV = DAG.getZExtOrTrunc(IdxV, dl, MVT::i32);
2338 SDValue OffV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV,
2339 DAG.getConstant(ElemWidth, dl, MVT::i32));
Krzysztof Parzyszekb1b29602018-01-04 13:56:04 +00002340 ExtV = DAG.getNode(HexagonISD::EXTRACTU, dl, ScalarTy,
2341 {VecV, WidthV, OffV});
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002342 }
2343
2344 // Cast ExtV to the requested result type.
2345 ExtV = DAG.getZExtOrTrunc(ExtV, dl, tyScalar(ResTy));
2346 ExtV = DAG.getBitcast(ResTy, ExtV);
2347 return ExtV;
2348}
2349
2350SDValue
2351HexagonTargetLowering::insertVector(SDValue VecV, SDValue ValV, SDValue IdxV,
2352 const SDLoc &dl, MVT ValTy,
2353 SelectionDAG &DAG) const {
2354 MVT VecTy = ty(VecV);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002355 if (VecTy.getVectorElementType() == MVT::i1) {
2356 MVT ValTy = ty(ValV);
2357 assert(ValTy.getVectorElementType() == MVT::i1);
2358 SDValue ValR = DAG.getNode(HexagonISD::P2D, dl, MVT::i64, ValV);
2359 unsigned VecLen = VecTy.getVectorNumElements();
2360 unsigned Scale = VecLen / ValTy.getVectorNumElements();
2361 assert(Scale > 1);
2362
2363 for (unsigned R = Scale; R > 1; R /= 2) {
2364 ValR = contractPredicate(ValR, dl, DAG);
2365 ValR = DAG.getNode(HexagonISD::COMBINE, dl, MVT::i64,
2366 DAG.getUNDEF(MVT::i32), ValR);
2367 }
2368 // The longest possible subvector is at most 32 bits, so it is always
2369 // contained in the low subregister.
2370 ValR = DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::i32, ValR);
2371
2372 unsigned ValBytes = 64 / Scale;
2373 SDValue Width = DAG.getConstant(ValBytes*8, dl, MVT::i32);
2374 SDValue Idx = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV,
2375 DAG.getConstant(8, dl, MVT::i32));
2376 SDValue VecR = DAG.getNode(HexagonISD::P2D, dl, MVT::i64, VecV);
2377 SDValue Ins = DAG.getNode(HexagonISD::INSERT, dl, MVT::i32,
2378 {VecR, ValR, Width, Idx});
2379 return DAG.getNode(HexagonISD::D2P, dl, VecTy, Ins);
2380 }
2381
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002382 unsigned VecWidth = VecTy.getSizeInBits();
2383 unsigned ValWidth = ValTy.getSizeInBits();
2384 assert(VecWidth == 32 || VecWidth == 64);
2385 assert((VecWidth % ValWidth) == 0);
2386
2387 // Cast everything to scalar integer types.
2388 MVT ScalarTy = MVT::getIntegerVT(VecWidth);
2389 // The actual type of ValV may be different than ValTy (which is related
2390 // to the vector type).
2391 unsigned VW = ty(ValV).getSizeInBits();
2392 ValV = DAG.getBitcast(MVT::getIntegerVT(VW), ValV);
2393 VecV = DAG.getBitcast(ScalarTy, VecV);
2394 if (VW != VecWidth)
2395 ValV = DAG.getAnyExtOrTrunc(ValV, dl, ScalarTy);
2396
2397 SDValue WidthV = DAG.getConstant(ValWidth, dl, MVT::i32);
2398 SDValue InsV;
2399
2400 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(IdxV)) {
2401 unsigned W = C->getZExtValue() * ValWidth;
2402 SDValue OffV = DAG.getConstant(W, dl, MVT::i32);
2403 InsV = DAG.getNode(HexagonISD::INSERT, dl, ScalarTy,
2404 {VecV, ValV, WidthV, OffV});
2405 } else {
2406 if (ty(IdxV) != MVT::i32)
2407 IdxV = DAG.getZExtOrTrunc(IdxV, dl, MVT::i32);
2408 SDValue OffV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV, WidthV);
Krzysztof Parzyszekb1b29602018-01-04 13:56:04 +00002409 InsV = DAG.getNode(HexagonISD::INSERT, dl, ScalarTy,
2410 {VecV, ValV, WidthV, OffV});
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002411 }
2412
2413 return DAG.getNode(ISD::BITCAST, dl, VecTy, InsV);
2414}
2415
2416SDValue
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002417HexagonTargetLowering::expandPredicate(SDValue Vec32, const SDLoc &dl,
2418 SelectionDAG &DAG) const {
2419 assert(ty(Vec32).getSizeInBits() == 32);
2420 if (isUndef(Vec32))
2421 return DAG.getUNDEF(MVT::i64);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002422 return getInstr(Hexagon::S2_vsxtbh, dl, MVT::i64, {Vec32}, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002423}
2424
2425SDValue
2426HexagonTargetLowering::contractPredicate(SDValue Vec64, const SDLoc &dl,
2427 SelectionDAG &DAG) const {
2428 assert(ty(Vec64).getSizeInBits() == 64);
2429 if (isUndef(Vec64))
2430 return DAG.getUNDEF(MVT::i32);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002431 return getInstr(Hexagon::S2_vtrunehb, dl, MVT::i32, {Vec64}, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002432}
2433
2434SDValue
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002435HexagonTargetLowering::getZero(const SDLoc &dl, MVT Ty, SelectionDAG &DAG)
2436 const {
2437 if (Ty.isVector()) {
2438 assert(Ty.isInteger() && "Only integer vectors are supported here");
2439 unsigned W = Ty.getSizeInBits();
2440 if (W <= 64)
2441 return DAG.getBitcast(Ty, DAG.getConstant(0, dl, MVT::getIntegerVT(W)));
2442 return DAG.getNode(HexagonISD::VZERO, dl, Ty);
2443 }
2444
2445 if (Ty.isInteger())
2446 return DAG.getConstant(0, dl, Ty);
2447 if (Ty.isFloatingPoint())
2448 return DAG.getConstantFP(0.0, dl, Ty);
2449 llvm_unreachable("Invalid type for zero");
2450}
2451
2452SDValue
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002453HexagonTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002454 MVT VecTy = ty(Op);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002455 unsigned BW = VecTy.getSizeInBits();
2456 const SDLoc &dl(Op);
2457 SmallVector<SDValue,8> Ops;
2458 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i)
2459 Ops.push_back(Op.getOperand(i));
2460
2461 if (BW == 32)
2462 return buildVector32(Ops, dl, VecTy, DAG);
2463 if (BW == 64)
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002464 return buildVector64(Ops, dl, VecTy, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002465
2466 if (VecTy == MVT::v8i1 || VecTy == MVT::v4i1 || VecTy == MVT::v2i1) {
2467 // For each i1 element in the resulting predicate register, put 1
2468 // shifted by the index of the element into a general-purpose register,
2469 // then or them together and transfer it back into a predicate register.
2470 SDValue Rs[8];
2471 SDValue Z = getZero(dl, MVT::i32, DAG);
2472 // Always produce 8 bits, repeat inputs if necessary.
2473 unsigned Rep = 8 / VecTy.getVectorNumElements();
2474 for (unsigned i = 0; i != 8; ++i) {
Simon Pilgrimc1e22902018-01-23 21:22:16 +00002475 SDValue S = DAG.getConstant(1ull << i, dl, MVT::i32);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002476 Rs[i] = DAG.getSelect(dl, MVT::i32, Ops[i/Rep], S, Z);
2477 }
2478 for (ArrayRef<SDValue> A(Rs); A.size() != 1; A = A.drop_back(A.size()/2)) {
2479 for (unsigned i = 0, e = A.size()/2; i != e; ++i)
2480 Rs[i] = DAG.getNode(ISD::OR, dl, MVT::i32, Rs[2*i], Rs[2*i+1]);
2481 }
2482 // Move the value directly to a predicate register.
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002483 return getInstr(Hexagon::C2_tfrrp, dl, VecTy, {Rs[0]}, DAG);
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002484 }
2485
2486 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002487}
2488
2489SDValue
2490HexagonTargetLowering::LowerCONCAT_VECTORS(SDValue Op,
2491 SelectionDAG &DAG) const {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002492 MVT VecTy = ty(Op);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002493 const SDLoc &dl(Op);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002494 if (VecTy.getSizeInBits() == 64) {
2495 assert(Op.getNumOperands() == 2);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002496 return DAG.getNode(HexagonISD::COMBINE, dl, VecTy, Op.getOperand(1),
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002497 Op.getOperand(0));
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002498 }
2499
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002500 MVT ElemTy = VecTy.getVectorElementType();
2501 if (ElemTy == MVT::i1) {
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002502 assert(VecTy == MVT::v2i1 || VecTy == MVT::v4i1 || VecTy == MVT::v8i1);
2503 MVT OpTy = ty(Op.getOperand(0));
2504 // Scale is how many times the operands need to be contracted to match
2505 // the representation in the target register.
2506 unsigned Scale = VecTy.getVectorNumElements() / OpTy.getVectorNumElements();
2507 assert(Scale == Op.getNumOperands() && Scale > 1);
2508
2509 // First, convert all bool vectors to integers, then generate pairwise
2510 // inserts to form values of doubled length. Up until there are only
2511 // two values left to concatenate, all of these values will fit in a
2512 // 32-bit integer, so keep them as i32 to use 32-bit inserts.
2513 SmallVector<SDValue,4> Words[2];
2514 unsigned IdxW = 0;
2515
2516 for (SDValue P : Op.getNode()->op_values()) {
2517 SDValue W = DAG.getNode(HexagonISD::P2D, dl, MVT::i64, P);
2518 for (unsigned R = Scale; R > 1; R /= 2) {
2519 W = contractPredicate(W, dl, DAG);
2520 W = DAG.getNode(HexagonISD::COMBINE, dl, MVT::i64,
2521 DAG.getUNDEF(MVT::i32), W);
2522 }
2523 W = DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::i32, W);
2524 Words[IdxW].push_back(W);
2525 }
2526
2527 while (Scale > 2) {
2528 SDValue WidthV = DAG.getConstant(64 / Scale, dl, MVT::i32);
2529 Words[IdxW ^ 1].clear();
2530
2531 for (unsigned i = 0, e = Words[IdxW].size(); i != e; i += 2) {
2532 SDValue W0 = Words[IdxW][i], W1 = Words[IdxW][i+1];
2533 // Insert W1 into W0 right next to the significant bits of W0.
2534 SDValue T = DAG.getNode(HexagonISD::INSERT, dl, MVT::i32,
2535 {W0, W1, WidthV, WidthV});
2536 Words[IdxW ^ 1].push_back(T);
2537 }
2538 IdxW ^= 1;
2539 Scale /= 2;
2540 }
2541
2542 // Another sanity check. At this point there should only be two words
2543 // left, and Scale should be 2.
2544 assert(Scale == 2 && Words[IdxW].size() == 2);
2545
2546 SDValue WW = DAG.getNode(HexagonISD::COMBINE, dl, MVT::i64,
2547 Words[IdxW][1], Words[IdxW][0]);
2548 return DAG.getNode(HexagonISD::D2P, dl, VecTy, WW);
2549 }
2550
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002551 return SDValue();
2552}
2553
2554SDValue
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002555HexagonTargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
2556 SelectionDAG &DAG) const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002557 SDValue Vec = Op.getOperand(0);
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002558 MVT ElemTy = ty(Vec).getVectorElementType();
2559 return extractVector(Vec, Op.getOperand(1), SDLoc(Op), ElemTy, ty(Op), DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002560}
2561
2562SDValue
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002563HexagonTargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op,
2564 SelectionDAG &DAG) const {
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00002565 return extractVector(Op.getOperand(0), Op.getOperand(1), SDLoc(Op),
2566 ty(Op), ty(Op), DAG);
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002567}
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002568
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002569SDValue
2570HexagonTargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op,
2571 SelectionDAG &DAG) const {
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002572 return insertVector(Op.getOperand(0), Op.getOperand(1), Op.getOperand(2),
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00002573 SDLoc(Op), ty(Op).getVectorElementType(), DAG);
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002574}
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002575
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002576SDValue
2577HexagonTargetLowering::LowerINSERT_SUBVECTOR(SDValue Op,
2578 SelectionDAG &DAG) const {
2579 SDValue ValV = Op.getOperand(1);
2580 return insertVector(Op.getOperand(0), ValV, Op.getOperand(2),
2581 SDLoc(Op), ty(ValV), DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002582}
2583
Tim Northovera4415852013-08-06 09:12:35 +00002584bool
2585HexagonTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
2586 // Assuming the caller does not have either a signext or zeroext modifier, and
2587 // only one value is accepted, any reasonable truncation is allowed.
2588 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
2589 return false;
2590
2591 // FIXME: in principle up to 64-bit could be made safe, but it would be very
2592 // fragile at the moment: any support for multiple value returns would be
2593 // liable to disallow tail calls involving i64 -> iN truncation in many cases.
2594 return Ty1->getPrimitiveSizeInBits() <= 32;
2595}
2596
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002597SDValue
Krzysztof Parzyszek1df70592018-08-08 17:00:09 +00002598HexagonTargetLowering::LowerLoad(SDValue Op, SelectionDAG &DAG) const {
2599 LoadSDNode *LN = cast<LoadSDNode>(Op.getNode());
2600 unsigned ClaimAlign = LN->getAlignment();
2601 validateConstPtrAlignment(LN->getBasePtr(), SDLoc(Op), ClaimAlign);
2602 // Call LowerUnalignedLoad for all loads, it recognizes loads that
2603 // don't need extra aligning.
2604 return LowerUnalignedLoad(Op, DAG);
2605}
2606
2607SDValue
2608HexagonTargetLowering::LowerStore(SDValue Op, SelectionDAG &DAG) const {
2609 StoreSDNode *SN = cast<StoreSDNode>(Op.getNode());
2610 unsigned ClaimAlign = SN->getAlignment();
2611 SDValue Ptr = SN->getBasePtr();
2612 const SDLoc &dl(Op);
2613 validateConstPtrAlignment(Ptr, dl, ClaimAlign);
2614
2615 MVT StoreTy = SN->getMemoryVT().getSimpleVT();
2616 unsigned NeedAlign = Subtarget.getTypeAlignment(StoreTy);
2617 if (ClaimAlign < NeedAlign)
2618 return expandUnalignedStore(SN, DAG);
2619 return Op;
2620}
2621
2622SDValue
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002623HexagonTargetLowering::LowerUnalignedLoad(SDValue Op, SelectionDAG &DAG)
2624 const {
2625 LoadSDNode *LN = cast<LoadSDNode>(Op.getNode());
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002626 MVT LoadTy = ty(Op);
2627 unsigned NeedAlign = Subtarget.getTypeAlignment(LoadTy);
Krzysztof Parzyszek1df70592018-08-08 17:00:09 +00002628 unsigned HaveAlign = LN->getAlignment();
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002629 if (HaveAlign >= NeedAlign)
2630 return Op;
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002631
2632 const SDLoc &dl(Op);
2633 const DataLayout &DL = DAG.getDataLayout();
2634 LLVMContext &Ctx = *DAG.getContext();
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002635
2636 // If the load aligning is disabled or the load can be broken up into two
2637 // smaller legal loads, do the default (target-independent) expansion.
2638 bool DoDefault = false;
Krzysztof Parzyszek480ab2b2018-03-08 18:15:13 +00002639 // Handle it in the default way if this is an indexed load.
2640 if (!LN->isUnindexed())
2641 DoDefault = true;
2642
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002643 if (!AlignLoads) {
Simon Pilgrim266f4392019-06-11 11:00:23 +00002644 if (allowsMemoryAccess(Ctx, DL, LN->getMemoryVT(), *LN->getMemOperand()))
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002645 return Op;
2646 DoDefault = true;
2647 }
Simon Pilgrim266f4392019-06-11 11:00:23 +00002648 if (!DoDefault && (2 * HaveAlign) == NeedAlign) {
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002649 // The PartTy is the equivalent of "getLoadableTypeOfSize(HaveAlign)".
Simon Pilgrim266f4392019-06-11 11:00:23 +00002650 MVT PartTy = HaveAlign <= 8 ? MVT::getIntegerVT(8 * HaveAlign)
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002651 : MVT::getVectorVT(MVT::i8, HaveAlign);
Simon Pilgrim266f4392019-06-11 11:00:23 +00002652 DoDefault = allowsMemoryAccess(Ctx, DL, PartTy, *LN->getMemOperand());
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002653 }
2654 if (DoDefault) {
2655 std::pair<SDValue, SDValue> P = expandUnalignedLoad(LN, DAG);
2656 return DAG.getMergeValues({P.first, P.second}, dl);
2657 }
2658
2659 // The code below generates two loads, both aligned as NeedAlign, and
2660 // with the distance of NeedAlign between them. For that to cover the
2661 // bits that need to be loaded (and without overlapping), the size of
2662 // the loads should be equal to NeedAlign. This is true for all loadable
2663 // types, but add an assertion in case something changes in the future.
2664 assert(LoadTy.getSizeInBits() == 8*NeedAlign);
2665
2666 unsigned LoadLen = NeedAlign;
2667 SDValue Base = LN->getBasePtr();
2668 SDValue Chain = LN->getChain();
2669 auto BO = getBaseAndOffset(Base);
2670 unsigned BaseOpc = BO.first.getOpcode();
2671 if (BaseOpc == HexagonISD::VALIGNADDR && BO.second % LoadLen == 0)
2672 return Op;
2673
2674 if (BO.second % LoadLen != 0) {
2675 BO.first = DAG.getNode(ISD::ADD, dl, MVT::i32, BO.first,
2676 DAG.getConstant(BO.second % LoadLen, dl, MVT::i32));
2677 BO.second -= BO.second % LoadLen;
2678 }
2679 SDValue BaseNoOff = (BaseOpc != HexagonISD::VALIGNADDR)
2680 ? DAG.getNode(HexagonISD::VALIGNADDR, dl, MVT::i32, BO.first,
2681 DAG.getConstant(NeedAlign, dl, MVT::i32))
2682 : BO.first;
2683 SDValue Base0 = DAG.getMemBasePlusOffset(BaseNoOff, BO.second, dl);
2684 SDValue Base1 = DAG.getMemBasePlusOffset(BaseNoOff, BO.second+LoadLen, dl);
2685
2686 MachineMemOperand *WideMMO = nullptr;
2687 if (MachineMemOperand *MMO = LN->getMemOperand()) {
2688 MachineFunction &MF = DAG.getMachineFunction();
2689 WideMMO = MF.getMachineMemOperand(MMO->getPointerInfo(), MMO->getFlags(),
2690 2*LoadLen, LoadLen, MMO->getAAInfo(), MMO->getRanges(),
2691 MMO->getSyncScopeID(), MMO->getOrdering(),
2692 MMO->getFailureOrdering());
2693 }
2694
2695 SDValue Load0 = DAG.getLoad(LoadTy, dl, Chain, Base0, WideMMO);
2696 SDValue Load1 = DAG.getLoad(LoadTy, dl, Chain, Base1, WideMMO);
2697
2698 SDValue Aligned = DAG.getNode(HexagonISD::VALIGN, dl, LoadTy,
2699 {Load1, Load0, BaseNoOff.getOperand(0)});
2700 SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2701 Load0.getValue(1), Load1.getValue(1));
2702 SDValue M = DAG.getMergeValues({Aligned, NewChain}, dl);
2703 return M;
2704}
2705
2706SDValue
Krzysztof Parzyszek5abf80c2019-07-01 15:50:09 +00002707HexagonTargetLowering::LowerUAddSubO(SDValue Op, SelectionDAG &DAG) const {
2708 SDValue X = Op.getOperand(0), Y = Op.getOperand(1);
2709 auto *CY = dyn_cast<ConstantSDNode>(Y);
2710 if (!CY)
2711 return SDValue();
2712
2713 const SDLoc &dl(Op);
2714 SDVTList VTs = Op.getNode()->getVTList();
2715 assert(VTs.NumVTs == 2);
2716 assert(VTs.VTs[1] == MVT::i1);
2717 unsigned Opc = Op.getOpcode();
2718
2719 if (CY) {
2720 uint32_t VY = CY->getZExtValue();
2721 assert(VY != 0 && "This should have been folded");
2722 // X +/- 1
2723 if (VY != 1)
2724 return SDValue();
2725
2726 if (Opc == ISD::UADDO) {
2727 SDValue Op = DAG.getNode(ISD::ADD, dl, VTs.VTs[0], {X, Y});
2728 SDValue Ov = DAG.getSetCC(dl, MVT::i1, Op, getZero(dl, ty(Op), DAG),
2729 ISD::SETEQ);
2730 return DAG.getMergeValues({Op, Ov}, dl);
2731 }
2732 if (Opc == ISD::USUBO) {
2733 SDValue Op = DAG.getNode(ISD::SUB, dl, VTs.VTs[0], {X, Y});
2734 SDValue Ov = DAG.getSetCC(dl, MVT::i1, Op,
2735 DAG.getConstant(-1, dl, ty(Op)), ISD::SETEQ);
2736 return DAG.getMergeValues({Op, Ov}, dl);
2737 }
2738 }
2739
2740 return SDValue();
2741}
2742
2743SDValue
Krzysztof Parzyszek0b6187c2018-06-01 14:00:32 +00002744HexagonTargetLowering::LowerAddSubCarry(SDValue Op, SelectionDAG &DAG) const {
2745 const SDLoc &dl(Op);
2746 unsigned Opc = Op.getOpcode();
2747 SDValue X = Op.getOperand(0), Y = Op.getOperand(1), C = Op.getOperand(2);
2748
2749 if (Opc == ISD::ADDCARRY)
2750 return DAG.getNode(HexagonISD::ADDC, dl, Op.getNode()->getVTList(),
2751 { X, Y, C });
2752
2753 EVT CarryTy = C.getValueType();
2754 SDValue SubC = DAG.getNode(HexagonISD::SUBC, dl, Op.getNode()->getVTList(),
2755 { X, Y, DAG.getLogicalNOT(dl, C, CarryTy) });
2756 SDValue Out[] = { SubC.getValue(0),
2757 DAG.getLogicalNOT(dl, SubC.getValue(1), CarryTy) };
2758 return DAG.getMergeValues(Out, dl);
2759}
2760
2761SDValue
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002762HexagonTargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
2763 SDValue Chain = Op.getOperand(0);
2764 SDValue Offset = Op.getOperand(1);
2765 SDValue Handler = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002766 SDLoc dl(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00002767 auto PtrVT = getPointerTy(DAG.getDataLayout());
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002768
2769 // Mark function as containing a call to EH_RETURN.
2770 HexagonMachineFunctionInfo *FuncInfo =
2771 DAG.getMachineFunction().getInfo<HexagonMachineFunctionInfo>();
2772 FuncInfo->setHasEHReturn();
2773
2774 unsigned OffsetReg = Hexagon::R28;
2775
Mehdi Amini44ede332015-07-09 02:09:04 +00002776 SDValue StoreAddr =
2777 DAG.getNode(ISD::ADD, dl, PtrVT, DAG.getRegister(Hexagon::R30, PtrVT),
2778 DAG.getIntPtrConstant(4, dl));
Justin Lebar9c375812016-07-15 18:27:10 +00002779 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo());
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002780 Chain = DAG.getCopyToReg(Chain, dl, OffsetReg, Offset);
2781
2782 // Not needed we already use it as explict input to EH_RETURN.
2783 // MF.getRegInfo().addLiveOut(OffsetReg);
2784
2785 return DAG.getNode(HexagonISD::EH_RETURN, dl, MVT::Other, Chain);
2786}
2787
2788SDValue
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002789HexagonTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002790 unsigned Opc = Op.getOpcode();
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00002791
2792 // Handle INLINEASM first.
Craig Topper784929d2019-02-08 20:48:56 +00002793 if (Opc == ISD::INLINEASM || Opc == ISD::INLINEASM_BR)
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00002794 return LowerINLINEASM(Op, DAG);
2795
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002796 if (isHvxOperation(Op)) {
2797 // If HVX lowering returns nothing, try the default lowering.
2798 if (SDValue V = LowerHvxOperation(Op, DAG))
2799 return V;
2800 }
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00002801
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002802 switch (Opc) {
2803 default:
2804#ifndef NDEBUG
2805 Op.getNode()->dumpr(&DAG);
2806 if (Opc > HexagonISD::OP_BEGIN && Opc < HexagonISD::OP_END)
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002807 errs() << "Error: check for a non-legal type in this operation\n";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002808#endif
2809 llvm_unreachable("Should not custom lower this!");
2810 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002811 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
2812 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
2813 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
2814 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002815 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
2816 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002817 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Krzysztof Parzyszek1df70592018-08-08 17:00:09 +00002818 case ISD::LOAD: return LowerLoad(Op, DAG);
2819 case ISD::STORE: return LowerStore(Op, DAG);
Krzysztof Parzyszek5abf80c2019-07-01 15:50:09 +00002820 case ISD::UADDO:
2821 case ISD::USUBO: return LowerUAddSubO(Op, DAG);
Krzysztof Parzyszek0b6187c2018-06-01 14:00:32 +00002822 case ISD::ADDCARRY:
2823 case ISD::SUBCARRY: return LowerAddSubCarry(Op, DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002824 case ISD::SRA:
2825 case ISD::SHL:
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002826 case ISD::SRL: return LowerVECTOR_SHIFT(Op, DAG);
Krzysztof Parzyszek3d671242018-06-12 12:49:36 +00002827 case ISD::ROTL: return LowerROTL(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002828 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002829 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002830 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002831 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
2832 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00002833 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002834 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
2835 case ISD::GlobalAddress: return LowerGLOBALADDRESS(Op, DAG);
2836 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002837 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002838 case ISD::VASTART: return LowerVASTART(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002839 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
2840 case ISD::SETCC: return LowerSETCC(Op, DAG);
2841 case ISD::VSELECT: return LowerVSELECT(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002842 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00002843 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00002844 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG);
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +00002845 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Krzysztof Parzyszek9eb085e2018-01-31 20:48:11 +00002846 break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002847 }
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00002848
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002849 return SDValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002850}
2851
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002852void
Krzysztof Parzyszek1df70592018-08-08 17:00:09 +00002853HexagonTargetLowering::LowerOperationWrapper(SDNode *N,
2854 SmallVectorImpl<SDValue> &Results,
2855 SelectionDAG &DAG) const {
2856 // We are only custom-lowering stores to verify the alignment of the
2857 // address if it is a compile-time constant. Since a store can be modified
2858 // during type-legalization (the value being stored may need legalization),
2859 // return empty Results here to indicate that we don't really make any
2860 // changes in the custom lowering.
2861 if (N->getOpcode() != ISD::STORE)
2862 return TargetLowering::LowerOperationWrapper(N, Results, DAG);
2863}
2864
2865void
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002866HexagonTargetLowering::ReplaceNodeResults(SDNode *N,
2867 SmallVectorImpl<SDValue> &Results,
2868 SelectionDAG &DAG) const {
2869 const SDLoc &dl(N);
2870 switch (N->getOpcode()) {
2871 case ISD::SRL:
2872 case ISD::SRA:
2873 case ISD::SHL:
2874 return;
2875 case ISD::BITCAST:
2876 // Handle a bitcast from v8i1 to i8.
2877 if (N->getValueType(0) == MVT::i8) {
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002878 SDValue P = getInstr(Hexagon::C2_tfrpr, dl, MVT::i32,
2879 N->getOperand(0), DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002880 Results.push_back(P);
2881 }
2882 break;
2883 }
2884}
2885
Krzysztof Parzyszek84603012019-08-15 16:13:17 +00002886SDValue
2887HexagonTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
2888 const {
2889 SDValue Op(N, 0);
2890 if (isHvxOperation(Op)) {
2891 if (SDValue V = PerformHvxDAGCombine(N, DCI))
2892 return V;
2893 }
2894 return SDValue();
2895}
2896
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002897/// Returns relocation base for the given PIC jumptable.
2898SDValue
2899HexagonTargetLowering::getPICJumpTableRelocBase(SDValue Table,
2900 SelectionDAG &DAG) const {
2901 int Idx = cast<JumpTableSDNode>(Table)->getIndex();
2902 EVT VT = Table.getValueType();
2903 SDValue T = DAG.getTargetJumpTable(Idx, VT, HexagonII::MO_PCREL);
2904 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Table), VT, T);
2905}
2906
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002907//===----------------------------------------------------------------------===//
2908// Inline Assembly Support
2909//===----------------------------------------------------------------------===//
2910
Krzysztof Parzyszekca3b5322016-05-18 14:34:51 +00002911TargetLowering::ConstraintType
2912HexagonTargetLowering::getConstraintType(StringRef Constraint) const {
2913 if (Constraint.size() == 1) {
2914 switch (Constraint[0]) {
2915 case 'q':
2916 case 'v':
2917 if (Subtarget.useHVXOps())
Krzysztof Parzyszek3ad0d012017-07-21 17:51:27 +00002918 return C_RegisterClass;
2919 break;
2920 case 'a':
2921 return C_RegisterClass;
2922 default:
Krzysztof Parzyszekca3b5322016-05-18 14:34:51 +00002923 break;
2924 }
2925 }
2926 return TargetLowering::getConstraintType(Constraint);
2927}
2928
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002929std::pair<unsigned, const TargetRegisterClass*>
Eric Christopher11e4df72015-02-26 22:38:43 +00002930HexagonTargetLowering::getRegForInlineAsmConstraint(
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00002931 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002932
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002933 if (Constraint.size() == 1) {
2934 switch (Constraint[0]) {
2935 case 'r': // R0-R31
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002936 switch (VT.SimpleTy) {
2937 default:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002938 return {0u, nullptr};
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002939 case MVT::i1:
2940 case MVT::i8:
2941 case MVT::i16:
2942 case MVT::i32:
2943 case MVT::f32:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002944 return {0u, &Hexagon::IntRegsRegClass};
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002945 case MVT::i64:
2946 case MVT::f64:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002947 return {0u, &Hexagon::DoubleRegsRegClass};
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002948 }
Krzysztof Parzyszek3ad0d012017-07-21 17:51:27 +00002949 break;
2950 case 'a': // M0-M1
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002951 if (VT != MVT::i32)
2952 return {0u, nullptr};
2953 return {0u, &Hexagon::ModRegsRegClass};
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002954 case 'q': // q0-q3
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002955 switch (VT.getSizeInBits()) {
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002956 default:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002957 return {0u, nullptr};
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002958 case 512:
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002959 case 1024:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002960 return {0u, &Hexagon::HvxQRRegClass};
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002961 }
Krzysztof Parzyszek3ad0d012017-07-21 17:51:27 +00002962 break;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002963 case 'v': // V0-V31
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002964 switch (VT.getSizeInBits()) {
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002965 default:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002966 return {0u, nullptr};
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002967 case 512:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002968 return {0u, &Hexagon::HvxVRRegClass};
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002969 case 1024:
Krzysztof Parzyszekd8b780d2018-06-20 13:56:09 +00002970 if (Subtarget.hasV60Ops() && Subtarget.useHVX128BOps())
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002971 return {0u, &Hexagon::HvxVRRegClass};
2972 return {0u, &Hexagon::HvxWRRegClass};
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002973 case 2048:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002974 return {0u, &Hexagon::HvxWRRegClass};
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002975 }
Krzysztof Parzyszek3ad0d012017-07-21 17:51:27 +00002976 break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002977 default:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002978 return {0u, nullptr};
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002979 }
2980 }
2981
Eric Christopher11e4df72015-02-26 22:38:43 +00002982 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002983}
2984
Sirish Pande69295b82012-05-10 20:20:25 +00002985/// isFPImmLegal - Returns true if the target can instruction select the
2986/// specified FP immediate natively. If false, the legalizer will
2987/// materialize the FP immediate as a load from a constant pool.
Adhemerval Zanella664c1ef2019-03-18 18:40:07 +00002988bool HexagonTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT,
2989 bool ForCodeSize) const {
Krzysztof Parzyszek6bfc6572018-10-19 17:31:11 +00002990 return true;
Sirish Pande69295b82012-05-10 20:20:25 +00002991}
2992
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002993/// isLegalAddressingMode - Return true if the addressing mode represented by
2994/// AM is legal for this target, for a load/store of the specified type.
Mehdi Amini0cdec1e2015-07-09 02:09:40 +00002995bool HexagonTargetLowering::isLegalAddressingMode(const DataLayout &DL,
2996 const AddrMode &AM, Type *Ty,
Jonas Paulsson024e3192017-07-21 11:59:37 +00002997 unsigned AS, Instruction *I) const {
Krzysztof Parzyszeked4e7822016-08-03 15:06:18 +00002998 if (Ty->isSized()) {
2999 // When LSR detects uses of the same base address to access different
3000 // types (e.g. unions), it will assume a conservative type for these
3001 // uses:
3002 // LSR Use: Kind=Address of void in addrspace(4294967295), ...
3003 // The type Ty passed here would then be "void". Skip the alignment
3004 // checks, but do not return false right away, since that confuses
3005 // LSR into crashing.
3006 unsigned A = DL.getABITypeAlignment(Ty);
3007 // The base offset must be a multiple of the alignment.
3008 if ((AM.BaseOffs % A) != 0)
3009 return false;
3010 // The shifted offset must fit in 11 bits.
3011 if (!isInt<11>(AM.BaseOffs >> Log2_32(A)))
3012 return false;
3013 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003014
3015 // No global is ever allowed as a base.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00003016 if (AM.BaseGV)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003017 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003018
3019 int Scale = AM.Scale;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00003020 if (Scale < 0)
3021 Scale = -Scale;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003022 switch (Scale) {
3023 case 0: // No scale reg, "r+i", "r", or just "i".
3024 break;
3025 default: // No scaled addressing mode.
3026 return false;
3027 }
3028 return true;
3029}
3030
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00003031/// Return true if folding a constant offset with the given GlobalAddress is
3032/// legal. It is frequently not legal in PIC relocation models.
3033bool HexagonTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA)
3034 const {
3035 return HTM.getRelocationModel() == Reloc::Static;
3036}
3037
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003038/// isLegalICmpImmediate - Return true if the specified immediate is legal
3039/// icmp immediate, that is the target has icmp instructions which can compare
3040/// a register against the immediate without having to materialize the
3041/// immediate into a register.
3042bool HexagonTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
3043 return Imm >= -512 && Imm <= 511;
3044}
3045
3046/// IsEligibleForTailCallOptimization - Check whether the call is eligible
3047/// for tail call optimization. Targets which want to do tail call
3048/// optimization should implement this function.
3049bool HexagonTargetLowering::IsEligibleForTailCallOptimization(
3050 SDValue Callee,
3051 CallingConv::ID CalleeCC,
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00003052 bool IsVarArg,
3053 bool IsCalleeStructRet,
3054 bool IsCallerStructRet,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003055 const SmallVectorImpl<ISD::OutputArg> &Outs,
3056 const SmallVectorImpl<SDValue> &OutVals,
3057 const SmallVectorImpl<ISD::InputArg> &Ins,
3058 SelectionDAG& DAG) const {
Matthias Braunf1caa282017-12-15 22:22:58 +00003059 const Function &CallerF = DAG.getMachineFunction().getFunction();
3060 CallingConv::ID CallerCC = CallerF.getCallingConv();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003061 bool CCMatch = CallerCC == CalleeCC;
3062
3063 // ***************************************************************************
3064 // Look for obvious safe cases to perform tail call optimization that do not
3065 // require ABI changes.
3066 // ***************************************************************************
3067
3068 // If this is a tail call via a function pointer, then don't do it!
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00003069 if (!isa<GlobalAddressSDNode>(Callee) &&
3070 !isa<ExternalSymbolSDNode>(Callee)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003071 return false;
3072 }
3073
Krzysztof Parzyszek0ba97542016-08-19 15:02:18 +00003074 // Do not optimize if the calling conventions do not match and the conventions
3075 // used are not C or Fast.
3076 if (!CCMatch) {
3077 bool R = (CallerCC == CallingConv::C || CallerCC == CallingConv::Fast);
3078 bool E = (CalleeCC == CallingConv::C || CalleeCC == CallingConv::Fast);
3079 // If R & E, then ok.
3080 if (!R || !E)
3081 return false;
3082 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003083
3084 // Do not tail call optimize vararg calls.
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00003085 if (IsVarArg)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003086 return false;
3087
3088 // Also avoid tail call optimization if either caller or callee uses struct
3089 // return semantics.
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00003090 if (IsCalleeStructRet || IsCallerStructRet)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003091 return false;
3092
3093 // In addition to the cases above, we also disable Tail Call Optimization if
3094 // the calling convention code that at least one outgoing argument needs to
3095 // go on the stack. We cannot check that here because at this point that
3096 // information is not available.
3097 return true;
3098}
Colin LeMahieu025f8602014-12-08 21:19:18 +00003099
Krzysztof Parzyszek3e409e12016-08-02 18:34:31 +00003100/// Returns the target specific optimal type for load and store operations as
3101/// a result of memset, memcpy, and memmove lowering.
3102///
3103/// If DstAlign is zero that means it's safe to destination alignment can
3104/// satisfy any constraint. Similarly if SrcAlign is zero it means there isn't
3105/// a need to check it against alignment requirement, probably because the
3106/// source does not need to be loaded. If 'IsMemset' is true, that means it's
3107/// expanding a memset. If 'ZeroMemset' is true, that means it's a memset of
3108/// zero. 'MemcpyStrSrc' indicates whether the memcpy source is constant so it
3109/// does not need to be loaded. It returns EVT::Other if the type should be
3110/// determined using generic target-independent logic.
3111EVT HexagonTargetLowering::getOptimalMemOpType(uint64_t Size,
3112 unsigned DstAlign, unsigned SrcAlign, bool IsMemset, bool ZeroMemset,
Sjoerd Meijer180f1ae2019-04-30 08:38:12 +00003113 bool MemcpyStrSrc, const AttributeList &FuncAttributes) const {
Krzysztof Parzyszek3e409e12016-08-02 18:34:31 +00003114
3115 auto Aligned = [](unsigned GivenA, unsigned MinA) -> bool {
3116 return (GivenA % MinA) == 0;
3117 };
3118
3119 if (Size >= 8 && Aligned(DstAlign, 8) && (IsMemset || Aligned(SrcAlign, 8)))
3120 return MVT::i64;
3121 if (Size >= 4 && Aligned(DstAlign, 4) && (IsMemset || Aligned(SrcAlign, 4)))
3122 return MVT::i32;
3123 if (Size >= 2 && Aligned(DstAlign, 2) && (IsMemset || Aligned(SrcAlign, 2)))
3124 return MVT::i16;
3125
3126 return MVT::Other;
3127}
3128
Simon Pilgrim4e0648a2019-06-12 17:14:03 +00003129bool HexagonTargetLowering::allowsMisalignedMemoryAccesses(
3130 EVT VT, unsigned AS, unsigned Align, MachineMemOperand::Flags Flags,
3131 bool *Fast) const {
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +00003132 if (Fast)
3133 *Fast = false;
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00003134 return Subtarget.isHVXVectorType(VT.getSimpleVT());
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +00003135}
3136
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003137std::pair<const TargetRegisterClass*, uint8_t>
3138HexagonTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI,
3139 MVT VT) const {
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00003140 if (Subtarget.isHVXVectorType(VT, true)) {
3141 unsigned BitWidth = VT.getSizeInBits();
3142 unsigned VecWidth = Subtarget.getVectorLength() * 8;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003143
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00003144 if (VT.getVectorElementType() == MVT::i1)
3145 return std::make_pair(&Hexagon::HvxQRRegClass, 1);
3146 if (BitWidth == VecWidth)
3147 return std::make_pair(&Hexagon::HvxVRRegClass, 1);
3148 assert(BitWidth == 2 * VecWidth);
3149 return std::make_pair(&Hexagon::HvxWRRegClass, 1);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003150 }
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00003151
3152 return TargetLowering::findRepresentativeClass(TRI, VT);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003153}
3154
Krzysztof Parzyszekf0705442018-11-02 14:17:47 +00003155bool HexagonTargetLowering::shouldReduceLoadWidth(SDNode *Load,
3156 ISD::LoadExtType ExtTy, EVT NewVT) const {
Sanjay Patel0a515592018-11-10 20:05:31 +00003157 // TODO: This may be worth removing. Check regression tests for diffs.
3158 if (!TargetLoweringBase::shouldReduceLoadWidth(Load, ExtTy, NewVT))
3159 return false;
3160
Krzysztof Parzyszekf0705442018-11-02 14:17:47 +00003161 auto *L = cast<LoadSDNode>(Load);
3162 std::pair<SDValue,int> BO = getBaseAndOffset(L->getBasePtr());
3163 // Small-data object, do not shrink.
3164 if (BO.first.getOpcode() == HexagonISD::CONST32_GP)
3165 return false;
3166 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(BO.first)) {
3167 auto &HTM = static_cast<const HexagonTargetMachine&>(getTargetMachine());
3168 const auto *GO = dyn_cast_or_null<const GlobalObject>(GA->getGlobal());
3169 return !GO || !HTM.getObjFileLowering()->isGlobalInSmallSection(GO, HTM);
3170 }
3171 return true;
3172}
3173
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003174Value *HexagonTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
3175 AtomicOrdering Ord) const {
3176 BasicBlock *BB = Builder.GetInsertBlock();
3177 Module *M = BB->getParent()->getParent();
Matt Arsenault39508332019-01-22 18:18:02 +00003178 auto PT = cast<PointerType>(Addr->getType());
3179 Type *Ty = PT->getElementType();
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003180 unsigned SZ = Ty->getPrimitiveSizeInBits();
3181 assert((SZ == 32 || SZ == 64) && "Only 32/64-bit atomic loads supported");
3182 Intrinsic::ID IntID = (SZ == 32) ? Intrinsic::hexagon_L2_loadw_locked
3183 : Intrinsic::hexagon_L4_loadd_locked;
James Y Knight7976eb52019-02-01 20:43:25 +00003184 Function *Fn = Intrinsic::getDeclaration(M, IntID);
Matt Arsenault39508332019-01-22 18:18:02 +00003185
3186 PointerType *NewPtrTy
3187 = Builder.getIntNTy(SZ)->getPointerTo(PT->getAddressSpace());
3188 Addr = Builder.CreateBitCast(Addr, NewPtrTy);
3189
Matt Arsenault39508332019-01-22 18:18:02 +00003190 Value *Call = Builder.CreateCall(Fn, Addr, "larx");
3191
3192 return Builder.CreateBitCast(Call, Ty);
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003193}
3194
3195/// Perform a store-conditional operation to Addr. Return the status of the
3196/// store. This should be 0 if the store succeeded, non-zero otherwise.
3197Value *HexagonTargetLowering::emitStoreConditional(IRBuilder<> &Builder,
3198 Value *Val, Value *Addr, AtomicOrdering Ord) const {
3199 BasicBlock *BB = Builder.GetInsertBlock();
3200 Module *M = BB->getParent()->getParent();
3201 Type *Ty = Val->getType();
3202 unsigned SZ = Ty->getPrimitiveSizeInBits();
Matt Arsenault39508332019-01-22 18:18:02 +00003203
3204 Type *CastTy = Builder.getIntNTy(SZ);
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003205 assert((SZ == 32 || SZ == 64) && "Only 32/64-bit atomic stores supported");
3206 Intrinsic::ID IntID = (SZ == 32) ? Intrinsic::hexagon_S2_storew_locked
3207 : Intrinsic::hexagon_S4_stored_locked;
James Y Knight7976eb52019-02-01 20:43:25 +00003208 Function *Fn = Intrinsic::getDeclaration(M, IntID);
Matt Arsenault39508332019-01-22 18:18:02 +00003209
3210 unsigned AS = Addr->getType()->getPointerAddressSpace();
3211 Addr = Builder.CreateBitCast(Addr, CastTy->getPointerTo(AS));
3212 Val = Builder.CreateBitCast(Val, CastTy);
3213
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003214 Value *Call = Builder.CreateCall(Fn, {Addr, Val}, "stcx");
3215 Value *Cmp = Builder.CreateICmpEQ(Call, Builder.getInt32(0), "");
3216 Value *Ext = Builder.CreateZExt(Cmp, Type::getInt32Ty(M->getContext()));
3217 return Ext;
3218}
3219
Ahmed Bougacha52468672015-09-11 17:08:28 +00003220TargetLowering::AtomicExpansionKind
3221HexagonTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const {
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003222 // Do not expand loads and stores that don't exceed 64 bits.
Ahmed Bougacha52468672015-09-11 17:08:28 +00003223 return LI->getType()->getPrimitiveSizeInBits() > 64
Tim Northoverf520eff2015-12-02 18:12:57 +00003224 ? AtomicExpansionKind::LLOnly
Ahmed Bougacha52468672015-09-11 17:08:28 +00003225 : AtomicExpansionKind::None;
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003226}
3227
3228bool HexagonTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const {
3229 // Do not expand loads and stores that don't exceed 64 bits.
3230 return SI->getValueOperand()->getType()->getPrimitiveSizeInBits() > 64;
3231}
Krzysztof Parzyszekf228c952016-06-22 16:07:10 +00003232
Alex Bradbury79518b02018-09-19 14:51:42 +00003233TargetLowering::AtomicExpansionKind
3234HexagonTargetLowering::shouldExpandAtomicCmpXchgInIR(
3235 AtomicCmpXchgInst *AI) const {
Krzysztof Parzyszekf228c952016-06-22 16:07:10 +00003236 const DataLayout &DL = AI->getModule()->getDataLayout();
3237 unsigned Size = DL.getTypeStoreSize(AI->getCompareOperand()->getType());
Alex Bradbury79518b02018-09-19 14:51:42 +00003238 if (Size >= 4 && Size <= 8)
3239 return AtomicExpansionKind::LLSC;
3240 return AtomicExpansionKind::None;
Krzysztof Parzyszekf228c952016-06-22 16:07:10 +00003241}