blob: b3781fe9e7113f7481707f404cceb4aeec2d5415 [file] [log] [blame]
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001//===-- HexagonISelLowering.cpp - Hexagon DAG Lowering Implementation -----===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Tony Linthicum1213a7a2011-12-12 21:14:40 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the interfaces that Hexagon uses to lower LLVM code
10// into a selection DAG.
11//
12//===----------------------------------------------------------------------===//
13
14#include "HexagonISelLowering.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000015#include "Hexagon.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000016#include "HexagonMachineFunctionInfo.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000017#include "HexagonRegisterInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000018#include "HexagonSubtarget.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "HexagonTargetMachine.h"
20#include "HexagonTargetObjectFile.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000021#include "llvm/ADT/APInt.h"
22#include "llvm/ADT/ArrayRef.h"
23#include "llvm/ADT/SmallVector.h"
Sid Manning9ad0f022018-09-07 13:36:21 +000024#include "llvm/ADT/StringSwitch.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000025#include "llvm/CodeGen/CallingConvLower.h"
26#include "llvm/CodeGen/MachineFrameInfo.h"
27#include "llvm/CodeGen/MachineFunction.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000028#include "llvm/CodeGen/MachineMemOperand.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000029#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000030#include "llvm/CodeGen/RuntimeLibcalls.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000031#include "llvm/CodeGen/SelectionDAG.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000032#include "llvm/CodeGen/TargetCallingConv.h"
Craig Topper2fa14362018-03-29 17:21:10 +000033#include "llvm/CodeGen/ValueTypes.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000034#include "llvm/IR/BasicBlock.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000035#include "llvm/IR/CallingConv.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000036#include "llvm/IR/DataLayout.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000037#include "llvm/IR/DerivedTypes.h"
38#include "llvm/IR/Function.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000039#include "llvm/IR/GlobalValue.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000040#include "llvm/IR/InlineAsm.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000041#include "llvm/IR/Instructions.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000042#include "llvm/IR/Intrinsics.h"
Krzysztof Parzyszekdc7a5572018-03-29 13:52:46 +000043#include "llvm/IR/IntrinsicInst.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000044#include "llvm/IR/Module.h"
45#include "llvm/IR/Type.h"
46#include "llvm/IR/Value.h"
47#include "llvm/MC/MCRegisterInfo.h"
48#include "llvm/Support/Casting.h"
49#include "llvm/Support/CodeGen.h"
NAKAMURA Takumi54eed762012-04-21 15:31:36 +000050#include "llvm/Support/CommandLine.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000051#include "llvm/Support/Debug.h"
52#include "llvm/Support/ErrorHandling.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000053#include "llvm/Support/MathExtras.h"
NAKAMURA Takumie30303f2012-04-21 15:31:45 +000054#include "llvm/Support/raw_ostream.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000055#include "llvm/Target/TargetMachine.h"
56#include <algorithm>
57#include <cassert>
58#include <cstddef>
59#include <cstdint>
60#include <limits>
61#include <utility>
NAKAMURA Takumi54eed762012-04-21 15:31:36 +000062
Craig Topperb25fda92012-03-17 18:46:09 +000063using namespace llvm;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000064
Chandler Carruthe96dd892014-04-21 22:55:11 +000065#define DEBUG_TYPE "hexagon-lowering"
66
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +000067static cl::opt<bool> EmitJumpTables("hexagon-emit-jump-tables",
68 cl::init(true), cl::Hidden,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +000069 cl::desc("Control jump table emission on Hexagon target"));
70
71static cl::opt<bool> EnableHexSDNodeSched("enable-hexagon-sdnode-sched",
72 cl::Hidden, cl::ZeroOrMore, cl::init(false),
73 cl::desc("Enable Hexagon SDNode scheduling"));
74
75static cl::opt<bool> EnableFastMath("ffast-math",
76 cl::Hidden, cl::ZeroOrMore, cl::init(false),
77 cl::desc("Enable Fast Math processing"));
78
79static cl::opt<int> MinimumJumpTables("minimum-jump-tables",
80 cl::Hidden, cl::ZeroOrMore, cl::init(5),
81 cl::desc("Set minimum jump tables"));
82
83static cl::opt<int> MaxStoresPerMemcpyCL("max-store-memcpy",
84 cl::Hidden, cl::ZeroOrMore, cl::init(6),
85 cl::desc("Max #stores to inline memcpy"));
86
87static cl::opt<int> MaxStoresPerMemcpyOptSizeCL("max-store-memcpy-Os",
88 cl::Hidden, cl::ZeroOrMore, cl::init(4),
89 cl::desc("Max #stores to inline memcpy"));
90
91static cl::opt<int> MaxStoresPerMemmoveCL("max-store-memmove",
92 cl::Hidden, cl::ZeroOrMore, cl::init(6),
93 cl::desc("Max #stores to inline memmove"));
94
95static cl::opt<int> MaxStoresPerMemmoveOptSizeCL("max-store-memmove-Os",
96 cl::Hidden, cl::ZeroOrMore, cl::init(4),
97 cl::desc("Max #stores to inline memmove"));
98
99static cl::opt<int> MaxStoresPerMemsetCL("max-store-memset",
100 cl::Hidden, cl::ZeroOrMore, cl::init(8),
101 cl::desc("Max #stores to inline memset"));
102
103static cl::opt<int> MaxStoresPerMemsetOptSizeCL("max-store-memset-Os",
104 cl::Hidden, cl::ZeroOrMore, cl::init(4),
105 cl::desc("Max #stores to inline memset"));
106
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +0000107static cl::opt<bool> AlignLoads("hexagon-align-loads",
108 cl::Hidden, cl::init(false),
109 cl::desc("Rewrite unaligned loads as a pair of aligned loads"));
110
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000111
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000112namespace {
Eugene Zelenko58655bb2016-12-17 01:09:05 +0000113
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +0000114 class HexagonCCState : public CCState {
Krzysztof Parzyszek18e0d2a2018-02-15 15:47:53 +0000115 unsigned NumNamedVarArgParams = 0;
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000116
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +0000117 public:
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000118 HexagonCCState(CallingConv::ID CC, bool IsVarArg, MachineFunction &MF,
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +0000119 SmallVectorImpl<CCValAssign> &locs, LLVMContext &C,
Krzysztof Parzyszeke0d7de72018-02-15 17:20:07 +0000120 unsigned NumNamedArgs)
121 : CCState(CC, IsVarArg, MF, locs, C),
122 NumNamedVarArgParams(NumNamedArgs) {}
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +0000123 unsigned getNumNamedVarArgParams() const { return NumNamedVarArgParams; }
124 };
125
Eugene Zelenko58655bb2016-12-17 01:09:05 +0000126} // end anonymous namespace
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000127
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000128
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000129// Implement calling convention for Hexagon.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000130
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000131static bool CC_SkipOdd(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
132 CCValAssign::LocInfo &LocInfo,
133 ISD::ArgFlagsTy &ArgFlags, CCState &State) {
134 static const MCPhysReg ArgRegs[] = {
135 Hexagon::R0, Hexagon::R1, Hexagon::R2,
136 Hexagon::R3, Hexagon::R4, Hexagon::R5
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000137 };
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000138 const unsigned NumArgRegs = array_lengthof(ArgRegs);
139 unsigned RegNum = State.getFirstUnallocated(ArgRegs);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000140
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000141 // RegNum is an index into ArgRegs: skip a register if RegNum is odd.
142 if (RegNum != NumArgRegs && RegNum % 2 == 1)
143 State.AllocateReg(ArgRegs[RegNum]);
144
145 // Always return false here, as this function only makes sure that the first
146 // unallocated register has an even register number and does not actually
147 // allocate a register for the current argument.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000148 return false;
149}
150
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000151#include "HexagonGenCallingConv.inc"
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000152
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000153
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000154SDValue
155HexagonTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG)
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000156 const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000157 return SDValue();
158}
159
160/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
161/// by "Src" to address "Dst" of size "Size". Alignment information is
162/// specified by the specific parameter attribute. The copy will be passed as
163/// a byval function parameter. Sometimes what we are copying is the end of a
164/// larger object, the part that does not fit in registers.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000165static SDValue CreateCopyOfByValArgument(SDValue Src, SDValue Dst,
166 SDValue Chain, ISD::ArgFlagsTy Flags,
167 SelectionDAG &DAG, const SDLoc &dl) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000168 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000169 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
170 /*isVolatile=*/false, /*AlwaysInline=*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +0000171 /*isTailCall=*/false,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000172 MachinePointerInfo(), MachinePointerInfo());
173}
174
Krzysztof Parzyszek56199522017-04-13 15:05:51 +0000175bool
176HexagonTargetLowering::CanLowerReturn(
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000177 CallingConv::ID CallConv, MachineFunction &MF, bool IsVarArg,
Krzysztof Parzyszek56199522017-04-13 15:05:51 +0000178 const SmallVectorImpl<ISD::OutputArg> &Outs,
179 LLVMContext &Context) const {
180 SmallVector<CCValAssign, 16> RVLocs;
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000181 CCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
182
183 if (MF.getSubtarget<HexagonSubtarget>().useHVXOps())
184 return CCInfo.CheckReturn(Outs, RetCC_Hexagon_HVX);
Krzysztof Parzyszek56199522017-04-13 15:05:51 +0000185 return CCInfo.CheckReturn(Outs, RetCC_Hexagon);
186}
187
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000188// LowerReturn - Lower ISD::RET. If a struct is larger than 8 bytes and is
189// passed by value, the function prototype is modified to return void and
190// the value is stored in memory pointed by a pointer passed by caller.
191SDValue
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000192HexagonTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000193 bool IsVarArg,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000194 const SmallVectorImpl<ISD::OutputArg> &Outs,
195 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000196 const SDLoc &dl, SelectionDAG &DAG) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000197 // CCValAssign - represent the assignment of the return value to locations.
198 SmallVector<CCValAssign, 16> RVLocs;
199
200 // CCState - Info about the registers and stack slot.
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000201 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
Eric Christopherb5217502014-08-06 18:45:26 +0000202 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000203
204 // Analyze return values of ISD::RET
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000205 if (Subtarget.useHVXOps())
206 CCInfo.AnalyzeReturn(Outs, RetCC_Hexagon_HVX);
207 else
208 CCInfo.AnalyzeReturn(Outs, RetCC_Hexagon);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000209
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000210 SDValue Flag;
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000211 SmallVector<SDValue, 4> RetOps(1, Chain);
212
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000213 // Copy the result values into the output registers.
214 for (unsigned i = 0; i != RVLocs.size(); ++i) {
215 CCValAssign &VA = RVLocs[i];
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000216
217 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), OutVals[i], Flag);
218
219 // Guarantee that all emitted copies are stuck together with flags.
220 Flag = Chain.getValue(1);
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000221 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000222 }
223
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000224 RetOps[0] = Chain; // Update chain.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000225
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000226 // Add the flag if we have it.
227 if (Flag.getNode())
228 RetOps.push_back(Flag);
229
Craig Topper48d114b2014-04-26 18:35:24 +0000230 return DAG.getNode(HexagonISD::RET_FLAG, dl, MVT::Other, RetOps);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000231}
232
Matt Arsenault31380752017-04-18 21:16:46 +0000233bool HexagonTargetLowering::mayBeEmittedAsTailCall(const CallInst *CI) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000234 // If either no tail call or told not to tail call at all, don't.
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000235 auto Attr =
236 CI->getParent()->getParent()->getFnAttribute("disable-tail-calls");
237 if (!CI->isTailCall() || Attr.getValueAsString() == "true")
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000238 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000239
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000240 return true;
241}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000242
Sid Manning9ad0f022018-09-07 13:36:21 +0000243unsigned HexagonTargetLowering::getRegisterByName(const char* RegName, EVT VT,
244 SelectionDAG &DAG) const {
245 // Just support r19, the linux kernel uses it.
246 unsigned Reg = StringSwitch<unsigned>(RegName)
247 .Case("r19", Hexagon::R19)
248 .Default(0);
249 if (Reg)
250 return Reg;
251
252 report_fatal_error("Invalid register name global variable");
253}
254
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000255/// LowerCallResult - Lower the result values of an ISD::CALL into the
256/// appropriate copies out of appropriate physical registers. This assumes that
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000257/// Chain/Glue are the input chain/glue to use, and that TheCall is the call
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000258/// being lowered. Returns a SDNode with the same number of values as the
259/// ISD::CALL.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000260SDValue HexagonTargetLowering::LowerCallResult(
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000261 SDValue Chain, SDValue Glue, CallingConv::ID CallConv, bool IsVarArg,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000262 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
263 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals,
264 const SmallVectorImpl<SDValue> &OutVals, SDValue Callee) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000265 // Assign locations to each value returned by this call.
266 SmallVector<CCValAssign, 16> RVLocs;
267
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000268 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
Eric Christopherb5217502014-08-06 18:45:26 +0000269 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000270
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000271 if (Subtarget.useHVXOps())
272 CCInfo.AnalyzeCallResult(Ins, RetCC_Hexagon_HVX);
273 else
274 CCInfo.AnalyzeCallResult(Ins, RetCC_Hexagon);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000275
276 // Copy all of the result registers out of their specified physreg.
277 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000278 SDValue RetVal;
279 if (RVLocs[i].getValVT() == MVT::i1) {
280 // Return values of type MVT::i1 require special handling. The reason
281 // is that MVT::i1 is associated with the PredRegs register class, but
282 // values of that type are still returned in R0. Generate an explicit
283 // copy into a predicate register from R0, and treat the value of the
284 // predicate register as the call result.
285 auto &MRI = DAG.getMachineFunction().getRegInfo();
286 SDValue FR0 = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000287 MVT::i32, Glue);
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000288 // FR0 = (Value, Chain, Glue)
289 unsigned PredR = MRI.createVirtualRegister(&Hexagon::PredRegsRegClass);
290 SDValue TPR = DAG.getCopyToReg(FR0.getValue(1), dl, PredR,
291 FR0.getValue(0), FR0.getValue(2));
292 // TPR = (Chain, Glue)
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000293 // Don't glue this CopyFromReg, because it copies from a virtual
294 // register. If it is glued to the call, InstrEmitter will add it
295 // as an implicit def to the call (EmitMachineNode).
296 RetVal = DAG.getCopyFromReg(TPR.getValue(0), dl, PredR, MVT::i1);
297 Glue = TPR.getValue(1);
Krzysztof Parzyszek6f06b6e2017-10-23 19:35:25 +0000298 Chain = TPR.getValue(0);
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000299 } else {
300 RetVal = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000301 RVLocs[i].getValVT(), Glue);
302 Glue = RetVal.getValue(2);
Krzysztof Parzyszek6f06b6e2017-10-23 19:35:25 +0000303 Chain = RetVal.getValue(1);
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000304 }
305 InVals.push_back(RetVal.getValue(0));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000306 }
307
308 return Chain;
309}
310
311/// LowerCall - Functions arguments are copied from virtual regs to
312/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
313SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +0000314HexagonTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000315 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +0000316 SelectionDAG &DAG = CLI.DAG;
Craig Topperb94011f2013-07-14 04:42:23 +0000317 SDLoc &dl = CLI.DL;
318 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
319 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
320 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000321 SDValue Chain = CLI.Chain;
322 SDValue Callee = CLI.Callee;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000323 CallingConv::ID CallConv = CLI.CallConv;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000324 bool IsVarArg = CLI.IsVarArg;
325 bool DoesNotReturn = CLI.DoesNotReturn;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000326
Krzysztof Parzyszeke0d7de72018-02-15 17:20:07 +0000327 bool IsStructRet = Outs.empty() ? false : Outs[0].Flags.isSRet();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000328 MachineFunction &MF = DAG.getMachineFunction();
Krzysztof Parzyszekf67cd822017-07-11 17:11:54 +0000329 MachineFrameInfo &MFI = MF.getFrameInfo();
Mehdi Amini44ede332015-07-09 02:09:04 +0000330 auto PtrVT = getPointerTy(MF.getDataLayout());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000331
Krzysztof Parzyszeke0d7de72018-02-15 17:20:07 +0000332 unsigned NumParams = CLI.CS.getInstruction()
333 ? CLI.CS.getFunctionType()->getNumParams()
334 : 0;
335 if (GlobalAddressSDNode *GAN = dyn_cast<GlobalAddressSDNode>(Callee))
336 Callee = DAG.getTargetGlobalAddress(GAN->getGlobal(), dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000337
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000338 // Analyze operands of the call, assigning locations to each operand.
339 SmallVector<CCValAssign, 16> ArgLocs;
Krzysztof Parzyszek18e0d2a2018-02-15 15:47:53 +0000340 HexagonCCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext(),
Krzysztof Parzyszeke0d7de72018-02-15 17:20:07 +0000341 NumParams);
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000342
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000343 if (Subtarget.useHVXOps())
344 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon_HVX);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000345 else
346 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon);
347
Matthias Braunf1caa282017-12-15 22:22:58 +0000348 auto Attr = MF.getFunction().getFnAttribute("disable-tail-calls");
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000349 if (Attr.getValueAsString() == "true")
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000350 CLI.IsTailCall = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000351
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000352 if (CLI.IsTailCall) {
Matthias Braunf1caa282017-12-15 22:22:58 +0000353 bool StructAttrFlag = MF.getFunction().hasStructRetAttr();
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000354 CLI.IsTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
355 IsVarArg, IsStructRet, StructAttrFlag, Outs,
356 OutVals, Ins, DAG);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000357 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000358 CCValAssign &VA = ArgLocs[i];
359 if (VA.isMemLoc()) {
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000360 CLI.IsTailCall = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000361 break;
362 }
363 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000364 LLVM_DEBUG(dbgs() << (CLI.IsTailCall ? "Eligible for Tail Call\n"
365 : "Argument must be passed on stack. "
366 "Not eligible for Tail Call\n"));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000367 }
368 // Get a count of how many bytes are to be pushed on the stack.
369 unsigned NumBytes = CCInfo.getNextStackOffset();
370 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
371 SmallVector<SDValue, 8> MemOpChains;
372
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000373 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Mehdi Amini44ede332015-07-09 02:09:04 +0000374 SDValue StackPtr =
375 DAG.getCopyFromReg(Chain, dl, HRI.getStackRegister(), PtrVT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000376
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000377 bool NeedsArgAlign = false;
378 unsigned LargestAlignSeen = 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000379 // Walk the register/memloc assignments, inserting copies/loads.
380 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
381 CCValAssign &VA = ArgLocs[i];
382 SDValue Arg = OutVals[i];
383 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000384 // Record if we need > 8 byte alignment on an argument.
Krzysztof Parzyszekac1966e2017-11-27 18:12:16 +0000385 bool ArgAlign = Subtarget.isHVXVectorType(VA.getValVT());
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000386 NeedsArgAlign |= ArgAlign;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000387
388 // Promote the value if needed.
389 switch (VA.getLocInfo()) {
390 default:
Krzysztof Parzyszek8f6b0c82017-12-20 14:44:05 +0000391 // Loc info must be one of Full, BCvt, SExt, ZExt, or AExt.
Craig Toppere55c5562012-02-07 02:50:20 +0000392 llvm_unreachable("Unknown loc info!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000393 case CCValAssign::Full:
394 break;
Krzysztof Parzyszek8f6b0c82017-12-20 14:44:05 +0000395 case CCValAssign::BCvt:
396 Arg = DAG.getBitcast(VA.getLocVT(), Arg);
397 break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000398 case CCValAssign::SExt:
399 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
400 break;
401 case CCValAssign::ZExt:
402 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
403 break;
404 case CCValAssign::AExt:
405 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
406 break;
407 }
408
409 if (VA.isMemLoc()) {
410 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000411 SDValue MemAddr = DAG.getConstant(LocMemOffset, dl,
412 StackPtr.getValueType());
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000413 MemAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, MemAddr);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000414 if (ArgAlign)
415 LargestAlignSeen = std::max(LargestAlignSeen,
416 VA.getLocVT().getStoreSizeInBits() >> 3);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000417 if (Flags.isByVal()) {
418 // The argument is a struct passed by value. According to LLVM, "Arg"
Fangrui Song956ee792018-03-30 22:22:31 +0000419 // is a pointer.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000420 MemOpChains.push_back(CreateCopyOfByValArgument(Arg, MemAddr, Chain,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000421 Flags, DAG, dl));
422 } else {
Alex Lorenze40c8a22015-08-11 23:09:45 +0000423 MachinePointerInfo LocPI = MachinePointerInfo::getStack(
424 DAG.getMachineFunction(), LocMemOffset);
Justin Lebar9c375812016-07-15 18:27:10 +0000425 SDValue S = DAG.getStore(Chain, dl, Arg, MemAddr, LocPI);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000426 MemOpChains.push_back(S);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000427 }
428 continue;
429 }
430
431 // Arguments that can be passed on register must be kept at RegsToPass
432 // vector.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000433 if (VA.isRegLoc())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000434 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000435 }
436
Krzysztof Parzyszekd8b780d2018-06-20 13:56:09 +0000437 if (NeedsArgAlign && Subtarget.hasV60Ops()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000438 LLVM_DEBUG(dbgs() << "Function needs byte stack align due to call args\n");
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000439 unsigned VecAlign = HRI.getSpillAlignment(Hexagon::HvxVRRegClass);
440 LargestAlignSeen = std::max(LargestAlignSeen, VecAlign);
Matthias Braun941a7052016-07-28 18:40:00 +0000441 MFI.ensureMaxAlignment(LargestAlignSeen);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000442 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000443 // Transform all store nodes into one single node because all store
444 // nodes are independent of each other.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000445 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +0000446 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000447
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000448 SDValue Glue;
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000449 if (!CLI.IsTailCall) {
Serge Pavlovd526b132017-05-09 13:35:13 +0000450 Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, dl);
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000451 Glue = Chain.getValue(1);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000452 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000453
454 // Build a sequence of copy-to-reg nodes chained together with token
455 // chain and flag operands which copy the outgoing args into registers.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000456 // The Glue is necessary since all emitted instructions must be
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000457 // stuck together.
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000458 if (!CLI.IsTailCall) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000459 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
460 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000461 RegsToPass[i].second, Glue);
462 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000463 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000464 } else {
465 // For tail calls lower the arguments to the 'real' stack slot.
466 //
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000467 // Force all the incoming stack arguments to be loaded from the stack
468 // before any new outgoing arguments are stored to the stack, because the
469 // outgoing stack slots may alias the incoming argument stack slots, and
470 // the alias isn't otherwise explicit. This is slightly more conservative
471 // than necessary, because it means that each store effectively depends
472 // on every argument instead of just those arguments it would clobber.
473 //
Benjamin Kramerbde91762012-06-02 10:20:22 +0000474 // Do not flag preceding copytoreg stuff together with the following stuff.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000475 Glue = SDValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000476 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
477 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000478 RegsToPass[i].second, Glue);
479 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000480 }
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000481 Glue = SDValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000482 }
483
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000484 bool LongCalls = MF.getSubtarget<HexagonSubtarget>().useLongCalls();
485 unsigned Flags = LongCalls ? HexagonII::HMOTF_ConstExtended : 0;
486
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000487 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
488 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
489 // node so that legalize doesn't hack it.
Tobias Edler von Kochb51460c2015-12-16 17:29:37 +0000490 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000491 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, PtrVT, 0, Flags);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000492 } else if (ExternalSymbolSDNode *S =
493 dyn_cast<ExternalSymbolSDNode>(Callee)) {
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000494 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), PtrVT, Flags);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000495 }
496
497 // Returns a chain & a flag for retval copy to use.
498 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
499 SmallVector<SDValue, 8> Ops;
500 Ops.push_back(Chain);
501 Ops.push_back(Callee);
502
503 // Add argument registers to the end of the list so that they are
504 // known live into the call.
505 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
506 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
507 RegsToPass[i].second.getValueType()));
508 }
509
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000510 const uint32_t *Mask = HRI.getCallPreservedMask(MF, CallConv);
511 assert(Mask && "Missing call preserved mask for calling convention");
512 Ops.push_back(DAG.getRegisterMask(Mask));
513
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000514 if (Glue.getNode())
515 Ops.push_back(Glue);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000516
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000517 if (CLI.IsTailCall) {
Krzysztof Parzyszekf67cd822017-07-11 17:11:54 +0000518 MFI.setHasTailCall();
Craig Topper48d114b2014-04-26 18:35:24 +0000519 return DAG.getNode(HexagonISD::TC_RETURN, dl, NodeTys, Ops);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +0000520 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000521
Krzysztof Parzyszekf67cd822017-07-11 17:11:54 +0000522 // Set this here because we need to know this for "hasFP" in frame lowering.
523 // The target-independent code calls getFrameRegister before setting it, and
524 // getFrameRegister uses hasFP to determine whether the function has FP.
525 MFI.setHasCalls(true);
526
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +0000527 unsigned OpCode = DoesNotReturn ? HexagonISD::CALLnr : HexagonISD::CALL;
Colin LeMahieu2e3a26d2015-01-16 17:05:27 +0000528 Chain = DAG.getNode(OpCode, dl, NodeTys, Ops);
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000529 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000530
531 // Create the CALLSEQ_END node.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000532 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000533 DAG.getIntPtrConstant(0, dl, true), Glue, dl);
534 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000535
536 // Handle result values, copying them out of physregs into vregs that we
537 // return.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000538 return LowerCallResult(Chain, Glue, CallConv, IsVarArg, Ins, dl, DAG,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000539 InVals, OutVals, Callee);
540}
541
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +0000542/// Returns true by value, base pointer and offset pointer and addressing
543/// mode by reference if this node can be combined with a load / store to
544/// form a post-indexed load / store.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000545bool HexagonTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +0000546 SDValue &Base, SDValue &Offset, ISD::MemIndexedMode &AM,
547 SelectionDAG &DAG) const {
548 LSBaseSDNode *LSN = dyn_cast<LSBaseSDNode>(N);
549 if (!LSN)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000550 return false;
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +0000551 EVT VT = LSN->getMemoryVT();
552 if (!VT.isSimple())
553 return false;
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +0000554 bool IsLegalType = VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32 ||
Brendon Cahoone5ed5632018-05-18 18:14:44 +0000555 VT == MVT::i64 || VT == MVT::f32 || VT == MVT::f64 ||
556 VT == MVT::v2i16 || VT == MVT::v2i32 || VT == MVT::v4i8 ||
557 VT == MVT::v4i16 || VT == MVT::v8i8 ||
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +0000558 Subtarget.isHVXVectorType(VT.getSimpleVT());
559 if (!IsLegalType)
560 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000561
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +0000562 if (Op->getOpcode() != ISD::ADD)
563 return false;
564 Base = Op->getOperand(0);
565 Offset = Op->getOperand(1);
566 if (!isa<ConstantSDNode>(Offset.getNode()))
567 return false;
568 AM = ISD::POST_INC;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000569
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +0000570 int32_t V = cast<ConstantSDNode>(Offset.getNode())->getSExtValue();
571 return Subtarget.getInstrInfo()->isValidAutoIncImm(VT, V);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000572}
573
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000574SDValue
575HexagonTargetLowering::LowerINLINEASM(SDValue Op, SelectionDAG &DAG) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000576 MachineFunction &MF = DAG.getMachineFunction();
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000577 auto &HMFI = *MF.getInfo<HexagonMachineFunctionInfo>();
578 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
579 unsigned LR = HRI.getRARegister();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000580
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000581 if (Op.getOpcode() != ISD::INLINEASM || HMFI.hasClobberLR())
582 return Op;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000583
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000584 unsigned NumOps = Op.getNumOperands();
585 if (Op.getOperand(NumOps-1).getValueType() == MVT::Glue)
586 --NumOps; // Ignore the flag operand.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000587
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000588 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
589 unsigned Flags = cast<ConstantSDNode>(Op.getOperand(i))->getZExtValue();
590 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
591 ++i; // Skip the ID value.
592
593 switch (InlineAsm::getKind(Flags)) {
594 default:
595 llvm_unreachable("Bad flags!");
596 case InlineAsm::Kind_RegUse:
597 case InlineAsm::Kind_Imm:
598 case InlineAsm::Kind_Mem:
599 i += NumVals;
600 break;
601 case InlineAsm::Kind_Clobber:
602 case InlineAsm::Kind_RegDef:
603 case InlineAsm::Kind_RegDefEarlyClobber: {
604 for (; NumVals; --NumVals, ++i) {
605 unsigned Reg = cast<RegisterSDNode>(Op.getOperand(i))->getReg();
606 if (Reg != LR)
607 continue;
608 HMFI.setHasClobberLR(true);
609 return Op;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000610 }
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000611 break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000612 }
613 }
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000614 }
615
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000616 return Op;
617}
618
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +0000619// Need to transform ISD::PREFETCH into something that doesn't inherit
620// all of the properties of ISD::PREFETCH, specifically SDNPMayLoad and
621// SDNPMayStore.
622SDValue HexagonTargetLowering::LowerPREFETCH(SDValue Op,
623 SelectionDAG &DAG) const {
624 SDValue Chain = Op.getOperand(0);
625 SDValue Addr = Op.getOperand(1);
626 // Lower it to DCFETCH($reg, #0). A "pat" will try to merge the offset in,
627 // if the "reg" is fed by an "add".
628 SDLoc DL(Op);
629 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
630 return DAG.getNode(HexagonISD::DCFETCH, DL, MVT::Other, Chain, Addr, Zero);
631}
632
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +0000633// Custom-handle ISD::READCYCLECOUNTER because the target-independent SDNode
634// is marked as having side-effects, while the register read on Hexagon does
635// not have any. TableGen refuses to accept the direct pattern from that node
636// to the A4_tfrcpp.
637SDValue HexagonTargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
638 SelectionDAG &DAG) const {
639 SDValue Chain = Op.getOperand(0);
640 SDLoc dl(Op);
641 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other);
642 return DAG.getNode(HexagonISD::READCYCLE, dl, VTs, Chain);
643}
644
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +0000645SDValue HexagonTargetLowering::LowerINTRINSIC_VOID(SDValue Op,
646 SelectionDAG &DAG) const {
647 SDValue Chain = Op.getOperand(0);
648 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
649 // Lower the hexagon_prefetch builtin to DCFETCH, as above.
650 if (IntNo == Intrinsic::hexagon_prefetch) {
651 SDValue Addr = Op.getOperand(2);
652 SDLoc DL(Op);
653 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
654 return DAG.getNode(HexagonISD::DCFETCH, DL, MVT::Other, Chain, Addr, Zero);
655 }
656 return SDValue();
657}
658
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000659SDValue
660HexagonTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
661 SelectionDAG &DAG) const {
662 SDValue Chain = Op.getOperand(0);
663 SDValue Size = Op.getOperand(1);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000664 SDValue Align = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000665 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000666
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000667 ConstantSDNode *AlignConst = dyn_cast<ConstantSDNode>(Align);
668 assert(AlignConst && "Non-constant Align in LowerDYNAMIC_STACKALLOC");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000669
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000670 unsigned A = AlignConst->getSExtValue();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000671 auto &HFI = *Subtarget.getFrameLowering();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000672 // "Zero" means natural stack alignment.
673 if (A == 0)
674 A = HFI.getStackAlignment();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000675
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000676 LLVM_DEBUG({
Reid Kleckner40d72302016-10-20 00:22:23 +0000677 dbgs () << __func__ << " Align: " << A << " Size: ";
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000678 Size.getNode()->dump(&DAG);
679 dbgs() << "\n";
680 });
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000681
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000682 SDValue AC = DAG.getConstant(A, dl, MVT::i32);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000683 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other);
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +0000684 SDValue AA = DAG.getNode(HexagonISD::ALLOCA, dl, VTs, Chain, Size, AC);
Nirav Davebfdb4832016-06-23 17:52:57 +0000685
686 DAG.ReplaceAllUsesOfValueWith(Op, AA);
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +0000687 return AA;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000688}
689
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000690SDValue HexagonTargetLowering::LowerFormalArguments(
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000691 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000692 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
693 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000694 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +0000695 MachineFrameInfo &MFI = MF.getFrameInfo();
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000696 MachineRegisterInfo &MRI = MF.getRegInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000697
698 // Assign locations to all of the incoming arguments.
699 SmallVector<CCValAssign, 16> ArgLocs;
Krzysztof Parzyszek18e0d2a2018-02-15 15:47:53 +0000700 HexagonCCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext(),
Krzysztof Parzyszeke0d7de72018-02-15 17:20:07 +0000701 MF.getFunction().getFunctionType()->getNumParams());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000702
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000703 if (Subtarget.useHVXOps())
704 CCInfo.AnalyzeFormalArguments(Ins, CC_Hexagon_HVX);
705 else
706 CCInfo.AnalyzeFormalArguments(Ins, CC_Hexagon);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000707
708 // For LLVM, in the case when returning a struct by value (>8byte),
709 // the first argument is a pointer that points to the location on caller's
710 // stack where the return value will be stored. For Hexagon, the location on
711 // caller's stack is passed only when the struct size is smaller than (and
712 // equal to) 8 bytes. If not, no address will be passed into callee and
713 // callee return the result direclty through R0/R1.
714
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000715 auto &HMFI = *MF.getInfo<HexagonMachineFunctionInfo>();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000716
717 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
718 CCValAssign &VA = ArgLocs[i];
719 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000720 bool ByVal = Flags.isByVal();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000721
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000722 // Arguments passed in registers:
723 // 1. 32- and 64-bit values and HVX vectors are passed directly,
724 // 2. Large structs are passed via an address, and the address is
725 // passed in a register.
726 if (VA.isRegLoc() && ByVal && Flags.getByValSize() <= 8)
727 llvm_unreachable("ByValSize must be bigger than 8 bytes");
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000728
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000729 bool InReg = VA.isRegLoc() &&
730 (!ByVal || (ByVal && Flags.getByValSize() > 8));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000731
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000732 if (InReg) {
733 MVT RegVT = VA.getLocVT();
734 if (VA.getLocInfo() == CCValAssign::BCvt)
735 RegVT = VA.getValVT();
736
737 const TargetRegisterClass *RC = getRegClassFor(RegVT);
738 unsigned VReg = MRI.createVirtualRegister(RC);
739 SDValue Copy = DAG.getCopyFromReg(Chain, dl, VReg, RegVT);
740
741 // Treat values of type MVT::i1 specially: they are passed in
742 // registers of type i32, but they need to remain as values of
743 // type i1 for consistency of the argument lowering.
744 if (VA.getValVT() == MVT::i1) {
745 assert(RegVT.getSizeInBits() <= 32);
746 SDValue T = DAG.getNode(ISD::AND, dl, RegVT,
747 Copy, DAG.getConstant(1, dl, RegVT));
748 Copy = DAG.getSetCC(dl, MVT::i1, T, DAG.getConstant(0, dl, RegVT),
749 ISD::SETNE);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000750 } else {
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000751#ifndef NDEBUG
752 unsigned RegSize = RegVT.getSizeInBits();
753 assert(RegSize == 32 || RegSize == 64 ||
754 Subtarget.isHVXVectorType(RegVT));
755#endif
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000756 }
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000757 InVals.push_back(Copy);
758 MRI.addLiveIn(VA.getLocReg(), VReg);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000759 } else {
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000760 assert(VA.isMemLoc() && "Argument should be passed in memory");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000761
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000762 // If it's a byval parameter, then we need to compute the
763 // "real" size, not the size of the pointer.
764 unsigned ObjSize = Flags.isByVal()
765 ? Flags.getByValSize()
766 : VA.getLocVT().getStoreSizeInBits() / 8;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000767
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000768 // Create the frame index object for this incoming parameter.
769 int Offset = HEXAGON_LRFP_SIZE + VA.getLocMemOffset();
770 int FI = MFI.CreateFixedObject(ObjSize, Offset, true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000771 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
772
773 if (Flags.isByVal()) {
774 // If it's a pass-by-value aggregate, then do not dereference the stack
775 // location. Instead, we should generate a reference to the stack
776 // location.
777 InVals.push_back(FIN);
778 } else {
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000779 SDValue L = DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
780 MachinePointerInfo::getFixedStack(MF, FI, 0));
781 InVals.push_back(L);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000782 }
783 }
784 }
785
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000786
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000787 if (IsVarArg) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000788 // This will point to the next argument passed via stack.
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000789 int Offset = HEXAGON_LRFP_SIZE + CCInfo.getNextStackOffset();
790 int FI = MFI.CreateFixedObject(Hexagon_PointerSize, Offset, true);
791 HMFI.setVarArgsFrameIndex(FI);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000792 }
793
794 return Chain;
795}
796
797SDValue
798HexagonTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
799 // VASTART stores the address of the VarArgsFrameIndex slot into the
800 // memory location argument.
801 MachineFunction &MF = DAG.getMachineFunction();
802 HexagonMachineFunctionInfo *QFI = MF.getInfo<HexagonMachineFunctionInfo>();
803 SDValue Addr = DAG.getFrameIndex(QFI->getVarArgsFrameIndex(), MVT::i32);
804 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Justin Lebar9c375812016-07-15 18:27:10 +0000805 return DAG.getStore(Op.getOperand(0), SDLoc(Op), Addr, Op.getOperand(1),
806 MachinePointerInfo(SV));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000807}
808
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000809SDValue HexagonTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek23bcf062018-04-19 14:24:31 +0000810 const SDLoc &dl(Op);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000811 SDValue LHS = Op.getOperand(0);
812 SDValue RHS = Op.getOperand(1);
Krzysztof Parzyszek23bcf062018-04-19 14:24:31 +0000813 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
814 MVT ResTy = ty(Op);
815 MVT OpTy = ty(LHS);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000816
Krzysztof Parzyszek23bcf062018-04-19 14:24:31 +0000817 if (OpTy == MVT::v2i16 || OpTy == MVT::v4i8) {
818 MVT ElemTy = OpTy.getVectorElementType();
819 assert(ElemTy.isScalarInteger());
820 MVT WideTy = MVT::getVectorVT(MVT::getIntegerVT(2*ElemTy.getSizeInBits()),
821 OpTy.getVectorNumElements());
822 return DAG.getSetCC(dl, ResTy,
823 DAG.getSExtOrTrunc(LHS, SDLoc(LHS), WideTy),
824 DAG.getSExtOrTrunc(RHS, SDLoc(RHS), WideTy), CC);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000825 }
826
827 // Treat all other vector types as legal.
Krzysztof Parzyszek23bcf062018-04-19 14:24:31 +0000828 if (ResTy.isVector())
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000829 return Op;
830
Krzysztof Parzyszek23bcf062018-04-19 14:24:31 +0000831 // Comparisons of short integers should use sign-extend, not zero-extend,
832 // since we can represent small negative values in the compare instructions.
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000833 // The LLVM default is to use zero-extend arbitrarily in these cases.
Krzysztof Parzyszek23bcf062018-04-19 14:24:31 +0000834 auto isSExtFree = [this](SDValue N) {
835 switch (N.getOpcode()) {
836 case ISD::TRUNCATE: {
837 // A sign-extend of a truncate of a sign-extend is free.
838 SDValue Op = N.getOperand(0);
839 if (Op.getOpcode() != ISD::AssertSext)
840 return false;
Eli Friedman0319c282018-07-11 23:26:35 +0000841 EVT OrigTy = cast<VTSDNode>(Op.getOperand(1))->getVT();
Krzysztof Parzyszek23bcf062018-04-19 14:24:31 +0000842 unsigned ThisBW = ty(N).getSizeInBits();
843 unsigned OrigBW = OrigTy.getSizeInBits();
844 // The type that was sign-extended to get the AssertSext must be
845 // narrower than the type of N (so that N has still the same value
846 // as the original).
847 return ThisBW >= OrigBW;
848 }
849 case ISD::LOAD:
850 // We have sign-extended loads.
851 return true;
852 }
853 return false;
854 };
855
856 if (OpTy == MVT::i8 || OpTy == MVT::i16) {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000857 ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS);
Krzysztof Parzyszek23bcf062018-04-19 14:24:31 +0000858 bool IsNegative = C && C->getAPIntValue().isNegative();
859 if (IsNegative || isSExtFree(LHS) || isSExtFree(RHS))
860 return DAG.getSetCC(dl, ResTy,
861 DAG.getSExtOrTrunc(LHS, SDLoc(LHS), MVT::i32),
862 DAG.getSExtOrTrunc(RHS, SDLoc(RHS), MVT::i32), CC);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000863 }
Krzysztof Parzyszek23bcf062018-04-19 14:24:31 +0000864
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000865 return SDValue();
866}
867
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000868SDValue
869HexagonTargetLowering::LowerVSELECT(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000870 SDValue PredOp = Op.getOperand(0);
871 SDValue Op1 = Op.getOperand(1), Op2 = Op.getOperand(2);
872 EVT OpVT = Op1.getValueType();
873 SDLoc DL(Op);
874
875 if (OpVT == MVT::v2i16) {
876 SDValue X1 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i32, Op1);
877 SDValue X2 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i32, Op2);
878 SDValue SL = DAG.getNode(ISD::VSELECT, DL, MVT::v2i32, PredOp, X1, X2);
879 SDValue TR = DAG.getNode(ISD::TRUNCATE, DL, MVT::v2i16, SL);
880 return TR;
881 }
882
883 return SDValue();
884}
885
Krzysztof Parzyszek91ff5c62017-08-01 13:12:53 +0000886static Constant *convert_i1_to_i8(const Constant *ConstVal) {
887 SmallVector<Constant *, 128> NewConst;
888 const ConstantVector *CV = dyn_cast<ConstantVector>(ConstVal);
889 if (!CV)
890 return nullptr;
891
892 LLVMContext &Ctx = ConstVal->getContext();
893 IRBuilder<> IRB(Ctx);
894 unsigned NumVectorElements = CV->getNumOperands();
895 assert(isPowerOf2_32(NumVectorElements) &&
896 "conversion only supported for pow2 VectorSize!");
897
898 for (unsigned i = 0; i < NumVectorElements / 8; ++i) {
899 uint8_t x = 0;
900 for (unsigned j = 0; j < 8; ++j) {
901 uint8_t y = CV->getOperand(i * 8 + j)->getUniqueInteger().getZExtValue();
902 x |= y << (7 - j);
903 }
904 assert((x == 0 || x == 255) && "Either all 0's or all 1's expected!");
905 NewConst.push_back(IRB.getInt8(x));
906 }
907 return ConstantVector::get(NewConst);
908}
909
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000910SDValue
Sirish Pande69295b82012-05-10 20:20:25 +0000911HexagonTargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
912 EVT ValTy = Op.getValueType();
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000913 ConstantPoolSDNode *CPN = cast<ConstantPoolSDNode>(Op);
Krzysztof Parzyszek91ff5c62017-08-01 13:12:53 +0000914 Constant *CVal = nullptr;
915 bool isVTi1Type = false;
916 if (const Constant *ConstVal = dyn_cast<Constant>(CPN->getConstVal())) {
917 Type *CValTy = ConstVal->getType();
918 if (CValTy->isVectorTy() &&
919 CValTy->getVectorElementType()->isIntegerTy(1)) {
920 CVal = convert_i1_to_i8(ConstVal);
921 isVTi1Type = (CVal != nullptr);
922 }
923 }
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000924 unsigned Align = CPN->getAlignment();
Rafael Espindola405e25a2016-06-26 22:24:01 +0000925 bool IsPositionIndependent = isPositionIndependent();
926 unsigned char TF = IsPositionIndependent ? HexagonII::MO_PCREL : 0;
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000927
Ron Lieberman822ee882016-08-13 23:41:11 +0000928 unsigned Offset = 0;
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000929 SDValue T;
930 if (CPN->isMachineConstantPoolEntry())
Ron Lieberman822ee882016-08-13 23:41:11 +0000931 T = DAG.getTargetConstantPool(CPN->getMachineCPVal(), ValTy, Align, Offset,
932 TF);
Krzysztof Parzyszek91ff5c62017-08-01 13:12:53 +0000933 else if (isVTi1Type)
934 T = DAG.getTargetConstantPool(CVal, ValTy, Align, Offset, TF);
Sirish Pande69295b82012-05-10 20:20:25 +0000935 else
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +0000936 T = DAG.getTargetConstantPool(CPN->getConstVal(), ValTy, Align, Offset, TF);
Ron Lieberman822ee882016-08-13 23:41:11 +0000937
938 assert(cast<ConstantPoolSDNode>(T)->getTargetFlags() == TF &&
939 "Inconsistent target flag encountered");
940
Rafael Espindola405e25a2016-06-26 22:24:01 +0000941 if (IsPositionIndependent)
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000942 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), ValTy, T);
943 return DAG.getNode(HexagonISD::CP, SDLoc(Op), ValTy, T);
944}
945
946SDValue
947HexagonTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
948 EVT VT = Op.getValueType();
949 int Idx = cast<JumpTableSDNode>(Op)->getIndex();
Rafael Espindola405e25a2016-06-26 22:24:01 +0000950 if (isPositionIndependent()) {
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000951 SDValue T = DAG.getTargetJumpTable(Idx, VT, HexagonII::MO_PCREL);
952 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), VT, T);
953 }
954
955 SDValue T = DAG.getTargetJumpTable(Idx, VT);
956 return DAG.getNode(HexagonISD::JT, SDLoc(Op), VT, T);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000957}
958
959SDValue
960HexagonTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000961 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000962 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +0000963 MachineFrameInfo &MFI = MF.getFrameInfo();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000964 MFI.setReturnAddressIsTaken(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000965
Bill Wendling908bf812014-01-06 00:43:20 +0000966 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +0000967 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +0000968
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000969 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000970 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000971 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
972 if (Depth) {
973 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000974 SDValue Offset = DAG.getConstant(4, dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000975 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
976 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Justin Lebar9c375812016-07-15 18:27:10 +0000977 MachinePointerInfo());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000978 }
979
980 // Return LR, which contains the return address. Mark it an implicit live-in.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000981 unsigned Reg = MF.addLiveIn(HRI.getRARegister(), getRegClassFor(MVT::i32));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000982 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
983}
984
985SDValue
986HexagonTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000987 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Matthias Braun941a7052016-07-28 18:40:00 +0000988 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000989 MFI.setFrameAddressIsTaken(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000990
991 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000992 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000993 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
994 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000995 HRI.getFrameRegister(), VT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000996 while (Depth--)
997 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
Justin Lebar9c375812016-07-15 18:27:10 +0000998 MachinePointerInfo());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000999 return FrameAddr;
1000}
1001
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001002SDValue
1003HexagonTargetLowering::LowerATOMIC_FENCE(SDValue Op, SelectionDAG& DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001004 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001005 return DAG.getNode(HexagonISD::BARRIER, dl, MVT::Other, Op.getOperand(0));
1006}
1007
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001008SDValue
1009HexagonTargetLowering::LowerGLOBALADDRESS(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001010 SDLoc dl(Op);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001011 auto *GAN = cast<GlobalAddressSDNode>(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00001012 auto PtrVT = getPointerTy(DAG.getDataLayout());
Krzysztof Parzyszek96a28412018-01-30 18:10:27 +00001013 auto *GV = GAN->getGlobal();
1014 int64_t Offset = GAN->getOffset();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001015
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001016 auto &HLOF = *HTM.getObjFileLowering();
1017 Reloc::Model RM = HTM.getRelocationModel();
1018
1019 if (RM == Reloc::Static) {
1020 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, Offset);
Peter Collingbourne67335642016-10-24 19:23:39 +00001021 const GlobalObject *GO = GV->getBaseObject();
Krzysztof Parzyszek44e180b2018-05-14 21:01:56 +00001022 if (GO && Subtarget.useSmallData() && HLOF.isGlobalInSmallSection(GO, HTM))
Krzysztof Parzyszek96a28412018-01-30 18:10:27 +00001023 return DAG.getNode(HexagonISD::CONST32_GP, dl, PtrVT, GA);
1024 return DAG.getNode(HexagonISD::CONST32, dl, PtrVT, GA);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001025 }
1026
Krzysztof Parzyszek96a28412018-01-30 18:10:27 +00001027 bool UsePCRel = getTargetMachine().shouldAssumeDSOLocal(*GV->getParent(), GV);
1028 if (UsePCRel) {
1029 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, Offset,
1030 HexagonII::MO_PCREL);
1031 return DAG.getNode(HexagonISD::AT_PCREL, dl, PtrVT, GA);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001032 }
1033
Krzysztof Parzyszek96a28412018-01-30 18:10:27 +00001034 // Use GOT index.
1035 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
1036 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, HexagonII::MO_GOT);
1037 SDValue Off = DAG.getConstant(Offset, dl, MVT::i32);
1038 return DAG.getNode(HexagonISD::AT_GOT, dl, PtrVT, GOT, GA, Off);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001039}
1040
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001041// Specifies that for loads and stores VT can be promoted to PromotedLdStVT.
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001042SDValue
1043HexagonTargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
1044 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001045 SDLoc dl(Op);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001046 EVT PtrVT = getPointerTy(DAG.getDataLayout());
1047
1048 Reloc::Model RM = HTM.getRelocationModel();
1049 if (RM == Reloc::Static) {
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001050 SDValue A = DAG.getTargetBlockAddress(BA, PtrVT);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001051 return DAG.getNode(HexagonISD::CONST32_GP, dl, PtrVT, A);
1052 }
1053
1054 SDValue A = DAG.getTargetBlockAddress(BA, PtrVT, 0, HexagonII::MO_PCREL);
1055 return DAG.getNode(HexagonISD::AT_PCREL, dl, PtrVT, A);
1056}
1057
1058SDValue
1059HexagonTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op, SelectionDAG &DAG)
1060 const {
1061 EVT PtrVT = getPointerTy(DAG.getDataLayout());
1062 SDValue GOTSym = DAG.getTargetExternalSymbol(HEXAGON_GOT_SYM_NAME, PtrVT,
1063 HexagonII::MO_PCREL);
1064 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), PtrVT, GOTSym);
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001065}
1066
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001067SDValue
1068HexagonTargetLowering::GetDynamicTLSAddr(SelectionDAG &DAG, SDValue Chain,
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001069 GlobalAddressSDNode *GA, SDValue Glue, EVT PtrVT, unsigned ReturnReg,
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001070 unsigned char OperandFlags) const {
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001071 MachineFunction &MF = DAG.getMachineFunction();
1072 MachineFrameInfo &MFI = MF.getFrameInfo();
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001073 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1074 SDLoc dl(GA);
1075 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
1076 GA->getValueType(0),
1077 GA->getOffset(),
1078 OperandFlags);
1079 // Create Operands for the call.The Operands should have the following:
1080 // 1. Chain SDValue
1081 // 2. Callee which in this case is the Global address value.
1082 // 3. Registers live into the call.In this case its R0, as we
1083 // have just one argument to be passed.
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001084 // 4. Glue.
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001085 // Note: The order is important.
1086
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001087 const auto &HRI = *Subtarget.getRegisterInfo();
1088 const uint32_t *Mask = HRI.getCallPreservedMask(MF, CallingConv::C);
1089 assert(Mask && "Missing call preserved mask for calling convention");
1090 SDValue Ops[] = { Chain, TGA, DAG.getRegister(Hexagon::R0, PtrVT),
1091 DAG.getRegisterMask(Mask), Glue };
1092 Chain = DAG.getNode(HexagonISD::CALL, dl, NodeTys, Ops);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001093
1094 // Inform MFI that function has calls.
Matthias Braun941a7052016-07-28 18:40:00 +00001095 MFI.setAdjustsStack(true);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001096
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001097 Glue = Chain.getValue(1);
1098 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Glue);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001099}
1100
1101//
1102// Lower using the intial executable model for TLS addresses
1103//
1104SDValue
1105HexagonTargetLowering::LowerToTLSInitialExecModel(GlobalAddressSDNode *GA,
1106 SelectionDAG &DAG) const {
1107 SDLoc dl(GA);
1108 int64_t Offset = GA->getOffset();
1109 auto PtrVT = getPointerTy(DAG.getDataLayout());
1110
1111 // Get the thread pointer.
1112 SDValue TP = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Hexagon::UGP, PtrVT);
1113
Rafael Espindola405e25a2016-06-26 22:24:01 +00001114 bool IsPositionIndependent = isPositionIndependent();
1115 unsigned char TF =
1116 IsPositionIndependent ? HexagonII::MO_IEGOT : HexagonII::MO_IE;
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001117
1118 // First generate the TLS symbol address
1119 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT,
1120 Offset, TF);
1121
1122 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1123
Rafael Espindola405e25a2016-06-26 22:24:01 +00001124 if (IsPositionIndependent) {
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001125 // Generate the GOT pointer in case of position independent code
1126 SDValue GOT = LowerGLOBAL_OFFSET_TABLE(Sym, DAG);
1127
1128 // Add the TLS Symbol address to GOT pointer.This gives
1129 // GOT relative relocation for the symbol.
1130 Sym = DAG.getNode(ISD::ADD, dl, PtrVT, GOT, Sym);
1131 }
1132
1133 // Load the offset value for TLS symbol.This offset is relative to
1134 // thread pointer.
Justin Lebar9c375812016-07-15 18:27:10 +00001135 SDValue LoadOffset =
1136 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Sym, MachinePointerInfo());
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001137
1138 // Address of the thread local variable is the add of thread
1139 // pointer and the offset of the variable.
1140 return DAG.getNode(ISD::ADD, dl, PtrVT, TP, LoadOffset);
1141}
1142
1143//
1144// Lower using the local executable model for TLS addresses
1145//
1146SDValue
1147HexagonTargetLowering::LowerToTLSLocalExecModel(GlobalAddressSDNode *GA,
1148 SelectionDAG &DAG) const {
1149 SDLoc dl(GA);
1150 int64_t Offset = GA->getOffset();
1151 auto PtrVT = getPointerTy(DAG.getDataLayout());
1152
1153 // Get the thread pointer.
1154 SDValue TP = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Hexagon::UGP, PtrVT);
1155 // Generate the TLS symbol address
1156 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT, Offset,
1157 HexagonII::MO_TPREL);
1158 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1159
1160 // Address of the thread local variable is the add of thread
1161 // pointer and the offset of the variable.
1162 return DAG.getNode(ISD::ADD, dl, PtrVT, TP, Sym);
1163}
1164
1165//
1166// Lower using the general dynamic model for TLS addresses
1167//
1168SDValue
1169HexagonTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
1170 SelectionDAG &DAG) const {
1171 SDLoc dl(GA);
1172 int64_t Offset = GA->getOffset();
1173 auto PtrVT = getPointerTy(DAG.getDataLayout());
1174
1175 // First generate the TLS symbol address
1176 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT, Offset,
1177 HexagonII::MO_GDGOT);
1178
1179 // Then, generate the GOT pointer
1180 SDValue GOT = LowerGLOBAL_OFFSET_TABLE(TGA, DAG);
1181
1182 // Add the TLS symbol and the GOT pointer
1183 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1184 SDValue Chain = DAG.getNode(ISD::ADD, dl, PtrVT, GOT, Sym);
1185
1186 // Copy over the argument to R0
1187 SDValue InFlag;
1188 Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, Hexagon::R0, Chain, InFlag);
1189 InFlag = Chain.getValue(1);
1190
Krzysztof Parzyszeka7503832017-05-02 18:15:33 +00001191 unsigned Flags =
1192 static_cast<const HexagonSubtarget &>(DAG.getSubtarget()).useLongCalls()
1193 ? HexagonII::MO_GDPLT | HexagonII::HMOTF_ConstExtended
1194 : HexagonII::MO_GDPLT;
1195
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001196 return GetDynamicTLSAddr(DAG, Chain, GA, InFlag, PtrVT,
Krzysztof Parzyszeka7503832017-05-02 18:15:33 +00001197 Hexagon::R0, Flags);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001198}
1199
1200//
1201// Lower TLS addresses.
1202//
1203// For now for dynamic models, we only support the general dynamic model.
1204//
1205SDValue
1206HexagonTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1207 SelectionDAG &DAG) const {
1208 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1209
1210 switch (HTM.getTLSModel(GA->getGlobal())) {
1211 case TLSModel::GeneralDynamic:
1212 case TLSModel::LocalDynamic:
1213 return LowerToTLSGeneralDynamicModel(GA, DAG);
1214 case TLSModel::InitialExec:
1215 return LowerToTLSInitialExecModel(GA, DAG);
1216 case TLSModel::LocalExec:
1217 return LowerToTLSLocalExecModel(GA, DAG);
1218 }
1219 llvm_unreachable("Bogus TLS model");
1220}
1221
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001222//===----------------------------------------------------------------------===//
1223// TargetLowering Implementation
1224//===----------------------------------------------------------------------===//
1225
Eric Christopherd737b762015-02-02 22:11:36 +00001226HexagonTargetLowering::HexagonTargetLowering(const TargetMachine &TM,
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001227 const HexagonSubtarget &ST)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001228 : TargetLowering(TM), HTM(static_cast<const HexagonTargetMachine&>(TM)),
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001229 Subtarget(ST) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001230 auto &HRI = *Subtarget.getRegisterInfo();
Sirish Pande69295b82012-05-10 20:20:25 +00001231
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001232 setPrefLoopAlignment(4);
1233 setPrefFunctionAlignment(4);
1234 setMinFunctionAlignment(2);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001235 setStackPointerRegisterToSaveRestore(HRI.getStackRegister());
Krzysztof Parzyszekb3e50ac2018-01-05 20:41:50 +00001236 setBooleanContents(TargetLoweringBase::UndefinedBooleanContent);
1237 setBooleanVectorContents(TargetLoweringBase::UndefinedBooleanContent);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001238
Krzysztof Parzyszekf228c952016-06-22 16:07:10 +00001239 setMaxAtomicSizeInBitsSupported(64);
1240 setMinCmpXchgSizeInBits(32);
1241
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001242 if (EnableHexSDNodeSched)
1243 setSchedulingPreference(Sched::VLIW);
1244 else
1245 setSchedulingPreference(Sched::Source);
1246
1247 // Limits for inline expansion of memcpy/memmove
1248 MaxStoresPerMemcpy = MaxStoresPerMemcpyCL;
1249 MaxStoresPerMemcpyOptSize = MaxStoresPerMemcpyOptSizeCL;
1250 MaxStoresPerMemmove = MaxStoresPerMemmoveCL;
1251 MaxStoresPerMemmoveOptSize = MaxStoresPerMemmoveOptSizeCL;
1252 MaxStoresPerMemset = MaxStoresPerMemsetCL;
1253 MaxStoresPerMemsetOptSize = MaxStoresPerMemsetOptSizeCL;
1254
1255 //
1256 // Set up register classes.
1257 //
1258
1259 addRegisterClass(MVT::i1, &Hexagon::PredRegsRegClass);
1260 addRegisterClass(MVT::v2i1, &Hexagon::PredRegsRegClass); // bbbbaaaa
1261 addRegisterClass(MVT::v4i1, &Hexagon::PredRegsRegClass); // ddccbbaa
1262 addRegisterClass(MVT::v8i1, &Hexagon::PredRegsRegClass); // hgfedcba
1263 addRegisterClass(MVT::i32, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001264 addRegisterClass(MVT::v2i16, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00001265 addRegisterClass(MVT::v4i8, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001266 addRegisterClass(MVT::i64, &Hexagon::DoubleRegsRegClass);
1267 addRegisterClass(MVT::v8i8, &Hexagon::DoubleRegsRegClass);
1268 addRegisterClass(MVT::v4i16, &Hexagon::DoubleRegsRegClass);
1269 addRegisterClass(MVT::v2i32, &Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001270
Krzysztof Parzyszek6bfc6572018-10-19 17:31:11 +00001271 addRegisterClass(MVT::f32, &Hexagon::IntRegsRegClass);
1272 addRegisterClass(MVT::f64, &Hexagon::DoubleRegsRegClass);
Sirish Pande69295b82012-05-10 20:20:25 +00001273
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001274 //
1275 // Handling of scalar operations.
1276 //
1277 // All operations default to "legal", except:
1278 // - indexed loads and stores (pre-/post-incremented),
1279 // - ANY_EXTEND_VECTOR_INREG, ATOMIC_CMP_SWAP_WITH_SUCCESS, CONCAT_VECTORS,
1280 // ConstantFP, DEBUGTRAP, FCEIL, FCOPYSIGN, FEXP, FEXP2, FFLOOR, FGETSIGN,
1281 // FLOG, FLOG2, FLOG10, FMAXNUM, FMINNUM, FNEARBYINT, FRINT, FROUND, TRAP,
1282 // FTRUNC, PREFETCH, SIGN_EXTEND_VECTOR_INREG, ZERO_EXTEND_VECTOR_INREG,
1283 // which default to "expand" for at least one type.
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001284
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001285 // Misc operations.
Krzysztof Parzyszek75c2ca32018-08-09 18:03:45 +00001286 setOperationAction(ISD::ConstantFP, MVT::f32, Legal);
1287 setOperationAction(ISD::ConstantFP, MVT::f64, Legal);
1288 setOperationAction(ISD::TRAP, MVT::Other, Legal);
1289 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
1290 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
1291 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
1292 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
1293 setOperationAction(ISD::INLINEASM, MVT::Other, Custom);
1294 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
1295 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
1296 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
1297 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
1298 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
1299 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
1300 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001301
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001302 // Custom legalize GlobalAddress nodes into CONST32.
1303 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001304 setOperationAction(ISD::GlobalAddress, MVT::i8, Custom);
1305 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001306
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001307 // Hexagon needs to optimize cases with negative constants.
Krzysztof Parzyszek23bcf062018-04-19 14:24:31 +00001308 setOperationAction(ISD::SETCC, MVT::i8, Custom);
1309 setOperationAction(ISD::SETCC, MVT::i16, Custom);
1310 setOperationAction(ISD::SETCC, MVT::v4i8, Custom);
1311 setOperationAction(ISD::SETCC, MVT::v2i16, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001312
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001313 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
1314 setOperationAction(ISD::VASTART, MVT::Other, Custom);
1315 setOperationAction(ISD::VAEND, MVT::Other, Expand);
1316 setOperationAction(ISD::VAARG, MVT::Other, Expand);
Krzysztof Parzyszekf6088122018-03-02 18:35:57 +00001317 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001318
1319 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
1320 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
1321 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
1322
1323 if (EmitJumpTables)
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001324 setMinimumJumpTableEntries(MinimumJumpTables);
Krzysztof Parzyszeka61f7da2016-01-13 21:43:13 +00001325 else
Eugene Zelenko58655bb2016-12-17 01:09:05 +00001326 setMinimumJumpTableEntries(std::numeric_limits<int>::max());
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001327 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001328
Krzysztof Parzyszek82d284c2018-06-12 21:51:49 +00001329 setOperationAction(ISD::ABS, MVT::i32, Legal);
1330 setOperationAction(ISD::ABS, MVT::i64, Legal);
1331
Krzysztof Parzyszek0b6187c2018-06-01 14:00:32 +00001332 // Hexagon has A4_addp_c and A4_subp_c that take and generate a carry bit,
1333 // but they only operate on i64.
Krzysztof Parzyszek2c4487d2015-04-13 20:37:01 +00001334 for (MVT VT : MVT::integer_valuetypes()) {
Krzysztof Parzyszek0b6187c2018-06-01 14:00:32 +00001335 setOperationAction(ISD::UADDO, VT, Expand);
1336 setOperationAction(ISD::USUBO, VT, Expand);
1337 setOperationAction(ISD::SADDO, VT, Expand);
1338 setOperationAction(ISD::SSUBO, VT, Expand);
1339 setOperationAction(ISD::ADDCARRY, VT, Expand);
1340 setOperationAction(ISD::SUBCARRY, VT, Expand);
Krzysztof Parzyszek2c4487d2015-04-13 20:37:01 +00001341 }
Krzysztof Parzyszek0b6187c2018-06-01 14:00:32 +00001342 setOperationAction(ISD::ADDCARRY, MVT::i64, Custom);
1343 setOperationAction(ISD::SUBCARRY, MVT::i64, Custom);
Krzysztof Parzyszek2c4487d2015-04-13 20:37:01 +00001344
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001345 setOperationAction(ISD::CTLZ, MVT::i8, Promote);
1346 setOperationAction(ISD::CTLZ, MVT::i16, Promote);
1347 setOperationAction(ISD::CTTZ, MVT::i8, Promote);
1348 setOperationAction(ISD::CTTZ, MVT::i16, Promote);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001349
Krzysztof Parzyszek6bfc6572018-10-19 17:31:11 +00001350 // Popcount can count # of 1s in i64 but returns i32.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001351 setOperationAction(ISD::CTPOP, MVT::i8, Promote);
1352 setOperationAction(ISD::CTPOP, MVT::i16, Promote);
1353 setOperationAction(ISD::CTPOP, MVT::i32, Promote);
Krzysztof Parzyszekaf5ff652017-02-23 15:02:09 +00001354 setOperationAction(ISD::CTPOP, MVT::i64, Legal);
1355
1356 setOperationAction(ISD::BITREVERSE, MVT::i32, Legal);
1357 setOperationAction(ISD::BITREVERSE, MVT::i64, Legal);
1358 setOperationAction(ISD::BSWAP, MVT::i32, Legal);
1359 setOperationAction(ISD::BSWAP, MVT::i64, Legal);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001360
Krzysztof Parzyszek30c42e22018-12-20 16:39:20 +00001361 setOperationAction(ISD::FSHL, MVT::i32, Legal);
1362 setOperationAction(ISD::FSHL, MVT::i64, Legal);
1363 setOperationAction(ISD::FSHR, MVT::i32, Legal);
1364 setOperationAction(ISD::FSHR, MVT::i64, Legal);
1365
Benjamin Kramer62460692015-04-25 14:46:53 +00001366 for (unsigned IntExpOp :
Krzysztof Parzyszekaafb8c22018-06-05 12:49:19 +00001367 {ISD::SDIV, ISD::UDIV, ISD::SREM, ISD::UREM,
1368 ISD::SDIVREM, ISD::UDIVREM, ISD::ROTL, ISD::ROTR,
1369 ISD::SHL_PARTS, ISD::SRA_PARTS, ISD::SRL_PARTS,
1370 ISD::SMUL_LOHI, ISD::UMUL_LOHI}) {
1371 for (MVT VT : MVT::integer_valuetypes())
1372 setOperationAction(IntExpOp, VT, Expand);
Benjamin Kramer62460692015-04-25 14:46:53 +00001373 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001374
Benjamin Kramer62460692015-04-25 14:46:53 +00001375 for (unsigned FPExpOp :
1376 {ISD::FDIV, ISD::FREM, ISD::FSQRT, ISD::FSIN, ISD::FCOS, ISD::FSINCOS,
1377 ISD::FPOW, ISD::FCOPYSIGN}) {
Krzysztof Parzyszekaafb8c22018-06-05 12:49:19 +00001378 for (MVT VT : MVT::fp_valuetypes())
1379 setOperationAction(FPExpOp, VT, Expand);
Benjamin Kramer62460692015-04-25 14:46:53 +00001380 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001381
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001382 // No extending loads from i32.
1383 for (MVT VT : MVT::integer_valuetypes()) {
1384 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand);
1385 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand);
1386 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand);
1387 }
1388 // Turn FP truncstore into trunc + store.
1389 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Michael Kuperstein2bc3d4d2016-08-18 20:08:15 +00001390 // Turn FP extload into load/fpextend.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001391 for (MVT VT : MVT::fp_valuetypes())
1392 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001393
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001394 // Expand BR_CC and SELECT_CC for all integer and fp types.
1395 for (MVT VT : MVT::integer_valuetypes()) {
1396 setOperationAction(ISD::BR_CC, VT, Expand);
1397 setOperationAction(ISD::SELECT_CC, VT, Expand);
1398 }
1399 for (MVT VT : MVT::fp_valuetypes()) {
1400 setOperationAction(ISD::BR_CC, VT, Expand);
1401 setOperationAction(ISD::SELECT_CC, VT, Expand);
1402 }
1403 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001404
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001405 //
1406 // Handling of vector operations.
1407 //
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001408
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001409 // Set the action for vector operations to "expand", then override it with
1410 // either "custom" or "legal" for specific cases.
Craig Topper26260942015-10-18 05:15:34 +00001411 static const unsigned VectExpOps[] = {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001412 // Integer arithmetic:
Amaury Sechet84674112018-06-01 13:21:33 +00001413 ISD::ADD, ISD::SUB, ISD::MUL, ISD::SDIV, ISD::UDIV,
1414 ISD::SREM, ISD::UREM, ISD::SDIVREM, ISD::UDIVREM, ISD::SADDO,
1415 ISD::UADDO, ISD::SSUBO, ISD::USUBO, ISD::SMUL_LOHI, ISD::UMUL_LOHI,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001416 // Logical/bit:
1417 ISD::AND, ISD::OR, ISD::XOR, ISD::ROTL, ISD::ROTR,
Craig Topper33772c52016-04-28 03:34:31 +00001418 ISD::CTPOP, ISD::CTLZ, ISD::CTTZ,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001419 // Floating point arithmetic/math functions:
1420 ISD::FADD, ISD::FSUB, ISD::FMUL, ISD::FMA, ISD::FDIV,
1421 ISD::FREM, ISD::FNEG, ISD::FABS, ISD::FSQRT, ISD::FSIN,
Craig Topperf6d4dc52017-05-30 15:27:55 +00001422 ISD::FCOS, ISD::FPOW, ISD::FLOG, ISD::FLOG2,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001423 ISD::FLOG10, ISD::FEXP, ISD::FEXP2, ISD::FCEIL, ISD::FTRUNC,
1424 ISD::FRINT, ISD::FNEARBYINT, ISD::FROUND, ISD::FFLOOR,
1425 ISD::FMINNUM, ISD::FMAXNUM, ISD::FSINCOS,
1426 // Misc:
Krzysztof Parzyszek046da742016-10-27 14:30:16 +00001427 ISD::BR_CC, ISD::SELECT_CC, ISD::ConstantPool,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001428 // Vector:
1429 ISD::BUILD_VECTOR, ISD::SCALAR_TO_VECTOR,
1430 ISD::EXTRACT_VECTOR_ELT, ISD::INSERT_VECTOR_ELT,
1431 ISD::EXTRACT_SUBVECTOR, ISD::INSERT_SUBVECTOR,
1432 ISD::CONCAT_VECTORS, ISD::VECTOR_SHUFFLE
1433 };
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001434
1435 for (MVT VT : MVT::vector_valuetypes()) {
Benjamin Kramer62460692015-04-25 14:46:53 +00001436 for (unsigned VectExpOp : VectExpOps)
1437 setOperationAction(VectExpOp, VT, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001438
Krzysztof Parzyszeka696b1b2016-09-08 17:42:14 +00001439 // Expand all extending loads and truncating stores:
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001440 for (MVT TargetVT : MVT::vector_valuetypes()) {
Krzysztof Parzyszeka696b1b2016-09-08 17:42:14 +00001441 if (TargetVT == VT)
1442 continue;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001443 setLoadExtAction(ISD::EXTLOAD, TargetVT, VT, Expand);
Krzysztof Parzyszeka696b1b2016-09-08 17:42:14 +00001444 setLoadExtAction(ISD::ZEXTLOAD, TargetVT, VT, Expand);
1445 setLoadExtAction(ISD::SEXTLOAD, TargetVT, VT, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001446 setTruncStoreAction(VT, TargetVT, Expand);
1447 }
1448
Krzysztof Parzyszek046da742016-10-27 14:30:16 +00001449 // Normalize all inputs to SELECT to be vectors of i32.
1450 if (VT.getVectorElementType() != MVT::i32) {
1451 MVT VT32 = MVT::getVectorVT(MVT::i32, VT.getSizeInBits()/32);
1452 setOperationAction(ISD::SELECT, VT, Promote);
1453 AddPromotedToType(ISD::SELECT, VT, VT32);
1454 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001455 setOperationAction(ISD::SRA, VT, Custom);
1456 setOperationAction(ISD::SHL, VT, Custom);
1457 setOperationAction(ISD::SRL, VT, Custom);
1458 }
1459
Krzysztof Parzyszek64e5d7d2017-10-20 19:33:12 +00001460 // Extending loads from (native) vectors of i8 into (native) vectors of i16
1461 // are legal.
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001462 setLoadExtAction(ISD::EXTLOAD, MVT::v2i16, MVT::v2i8, Legal);
Krzysztof Parzyszek64e5d7d2017-10-20 19:33:12 +00001463 setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i16, MVT::v2i8, Legal);
1464 setLoadExtAction(ISD::SEXTLOAD, MVT::v2i16, MVT::v2i8, Legal);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001465 setLoadExtAction(ISD::EXTLOAD, MVT::v4i16, MVT::v4i8, Legal);
Krzysztof Parzyszek64e5d7d2017-10-20 19:33:12 +00001466 setLoadExtAction(ISD::ZEXTLOAD, MVT::v4i16, MVT::v4i8, Legal);
1467 setLoadExtAction(ISD::SEXTLOAD, MVT::v4i16, MVT::v4i8, Legal);
1468
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001469 // Types natively supported:
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001470 for (MVT NativeVT : {MVT::v8i1, MVT::v4i1, MVT::v2i1, MVT::v4i8,
1471 MVT::v8i8, MVT::v2i16, MVT::v4i16, MVT::v2i32}) {
Benjamin Kramer62460692015-04-25 14:46:53 +00001472 setOperationAction(ISD::BUILD_VECTOR, NativeVT, Custom);
1473 setOperationAction(ISD::EXTRACT_VECTOR_ELT, NativeVT, Custom);
1474 setOperationAction(ISD::INSERT_VECTOR_ELT, NativeVT, Custom);
1475 setOperationAction(ISD::EXTRACT_SUBVECTOR, NativeVT, Custom);
1476 setOperationAction(ISD::INSERT_SUBVECTOR, NativeVT, Custom);
1477 setOperationAction(ISD::CONCAT_VECTORS, NativeVT, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001478
Benjamin Kramer62460692015-04-25 14:46:53 +00001479 setOperationAction(ISD::ADD, NativeVT, Legal);
1480 setOperationAction(ISD::SUB, NativeVT, Legal);
1481 setOperationAction(ISD::MUL, NativeVT, Legal);
1482 setOperationAction(ISD::AND, NativeVT, Legal);
1483 setOperationAction(ISD::OR, NativeVT, Legal);
1484 setOperationAction(ISD::XOR, NativeVT, Legal);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001485 }
1486
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00001487 // Custom lower unaligned loads.
Krzysztof Parzyszek1df70592018-08-08 17:00:09 +00001488 // Also, for both loads and stores, verify the alignment of the address
1489 // in case it is a compile-time constant. This is a usability feature to
1490 // provide a meaningful error message to users.
1491 for (MVT VT : {MVT::i16, MVT::i32, MVT::v4i8, MVT::i64, MVT::v8i8,
1492 MVT::v2i16, MVT::v4i16, MVT::v2i32}) {
1493 setOperationAction(ISD::LOAD, VT, Custom);
1494 setOperationAction(ISD::STORE, VT, Custom);
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00001495 }
1496
Krzysztof Parzyszek99152912018-03-16 15:03:37 +00001497 for (MVT VT : {MVT::v2i16, MVT::v4i8, MVT::v2i32, MVT::v4i16, MVT::v2i32}) {
1498 setCondCodeAction(ISD::SETLT, VT, Expand);
1499 setCondCodeAction(ISD::SETLE, VT, Expand);
1500 setCondCodeAction(ISD::SETULT, VT, Expand);
1501 setCondCodeAction(ISD::SETULE, VT, Expand);
1502 }
1503
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001504 // Custom-lower bitcasts from i8 to v8i1.
1505 setOperationAction(ISD::BITCAST, MVT::i8, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001506 setOperationAction(ISD::SETCC, MVT::v2i16, Custom);
1507 setOperationAction(ISD::VSELECT, MVT::v2i16, Custom);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001508 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i8, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001509 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
1510 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00001511
Krzysztof Parzyszek6bfc6572018-10-19 17:31:11 +00001512 // V5+.
1513 setOperationAction(ISD::FMA, MVT::f64, Expand);
1514 setOperationAction(ISD::FADD, MVT::f64, Expand);
1515 setOperationAction(ISD::FSUB, MVT::f64, Expand);
1516 setOperationAction(ISD::FMUL, MVT::f64, Expand);
Krzysztof Parzyszekbd8ef4b2016-08-19 13:34:31 +00001517
Krzysztof Parzyszek6bfc6572018-10-19 17:31:11 +00001518 setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
1519 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001520
Krzysztof Parzyszek6bfc6572018-10-19 17:31:11 +00001521 setOperationAction(ISD::FP_TO_UINT, MVT::i1, Promote);
1522 setOperationAction(ISD::FP_TO_UINT, MVT::i8, Promote);
1523 setOperationAction(ISD::FP_TO_UINT, MVT::i16, Promote);
1524 setOperationAction(ISD::FP_TO_SINT, MVT::i1, Promote);
1525 setOperationAction(ISD::FP_TO_SINT, MVT::i8, Promote);
1526 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
1527 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Promote);
1528 setOperationAction(ISD::UINT_TO_FP, MVT::i8, Promote);
1529 setOperationAction(ISD::UINT_TO_FP, MVT::i16, Promote);
1530 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Promote);
1531 setOperationAction(ISD::SINT_TO_FP, MVT::i8, Promote);
1532 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001533
1534 // Handling of indexed loads/stores: default is "expand".
1535 //
Brendon Cahoone5ed5632018-05-18 18:14:44 +00001536 for (MVT VT : {MVT::i8, MVT::i16, MVT::i32, MVT::i64, MVT::f32, MVT::f64,
1537 MVT::v2i16, MVT::v2i32, MVT::v4i8, MVT::v4i16, MVT::v8i8}) {
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +00001538 setIndexedLoadAction(ISD::POST_INC, VT, Legal);
1539 setIndexedStoreAction(ISD::POST_INC, VT, Legal);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001540 }
1541
Krzysztof Parzyszek545a68c2018-12-05 21:01:07 +00001542 // Subtarget-specific operation actions.
1543 //
1544 if (Subtarget.hasV60Ops()) {
Krzysztof Parzyszek30c42e22018-12-20 16:39:20 +00001545 setOperationAction(ISD::ROTL, MVT::i32, Legal);
1546 setOperationAction(ISD::ROTL, MVT::i64, Legal);
1547 setOperationAction(ISD::ROTR, MVT::i32, Legal);
1548 setOperationAction(ISD::ROTR, MVT::i64, Legal);
Krzysztof Parzyszek545a68c2018-12-05 21:01:07 +00001549 }
1550 if (Subtarget.hasV66Ops()) {
1551 setOperationAction(ISD::FADD, MVT::f64, Legal);
1552 setOperationAction(ISD::FSUB, MVT::f64, Legal);
1553 }
1554
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00001555 if (Subtarget.useHVXOps())
1556 initializeHVXLowering();
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00001557
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001558 computeRegisterProperties(&HRI);
1559
1560 //
1561 // Library calls for unsupported operations
1562 //
1563 bool FastMath = EnableFastMath;
1564
Benjamin Kramera37c8092015-04-25 14:46:46 +00001565 setLibcallName(RTLIB::SDIV_I32, "__hexagon_divsi3");
1566 setLibcallName(RTLIB::SDIV_I64, "__hexagon_divdi3");
1567 setLibcallName(RTLIB::UDIV_I32, "__hexagon_udivsi3");
1568 setLibcallName(RTLIB::UDIV_I64, "__hexagon_udivdi3");
1569 setLibcallName(RTLIB::SREM_I32, "__hexagon_modsi3");
1570 setLibcallName(RTLIB::SREM_I64, "__hexagon_moddi3");
1571 setLibcallName(RTLIB::UREM_I32, "__hexagon_umodsi3");
1572 setLibcallName(RTLIB::UREM_I64, "__hexagon_umoddi3");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001573
Benjamin Kramera37c8092015-04-25 14:46:46 +00001574 setLibcallName(RTLIB::SINTTOFP_I128_F64, "__hexagon_floattidf");
1575 setLibcallName(RTLIB::SINTTOFP_I128_F32, "__hexagon_floattisf");
1576 setLibcallName(RTLIB::FPTOUINT_F32_I128, "__hexagon_fixunssfti");
1577 setLibcallName(RTLIB::FPTOUINT_F64_I128, "__hexagon_fixunsdfti");
1578 setLibcallName(RTLIB::FPTOSINT_F32_I128, "__hexagon_fixsfti");
1579 setLibcallName(RTLIB::FPTOSINT_F64_I128, "__hexagon_fixdfti");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001580
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001581 // This is the only fast library function for sqrtd.
1582 if (FastMath)
Benjamin Kramera37c8092015-04-25 14:46:46 +00001583 setLibcallName(RTLIB::SQRT_F64, "__hexagon_fast2_sqrtdf2");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001584
Benjamin Kramera37c8092015-04-25 14:46:46 +00001585 // Prefix is: nothing for "slow-math",
Krzysztof Parzyszek6bfc6572018-10-19 17:31:11 +00001586 // "fast2_" for V5+ fast-math double-precision
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001587 // (actually, keep fast-math and fast-math2 separate for now)
Benjamin Kramera37c8092015-04-25 14:46:46 +00001588 if (FastMath) {
1589 setLibcallName(RTLIB::ADD_F64, "__hexagon_fast_adddf3");
1590 setLibcallName(RTLIB::SUB_F64, "__hexagon_fast_subdf3");
1591 setLibcallName(RTLIB::MUL_F64, "__hexagon_fast_muldf3");
1592 setLibcallName(RTLIB::DIV_F64, "__hexagon_fast_divdf3");
Benjamin Kramera37c8092015-04-25 14:46:46 +00001593 setLibcallName(RTLIB::DIV_F32, "__hexagon_fast_divsf3");
1594 } else {
1595 setLibcallName(RTLIB::ADD_F64, "__hexagon_adddf3");
1596 setLibcallName(RTLIB::SUB_F64, "__hexagon_subdf3");
1597 setLibcallName(RTLIB::MUL_F64, "__hexagon_muldf3");
1598 setLibcallName(RTLIB::DIV_F64, "__hexagon_divdf3");
1599 setLibcallName(RTLIB::DIV_F32, "__hexagon_divsf3");
1600 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001601
Krzysztof Parzyszek6bfc6572018-10-19 17:31:11 +00001602 if (FastMath)
1603 setLibcallName(RTLIB::SQRT_F32, "__hexagon_fast2_sqrtf");
1604 else
1605 setLibcallName(RTLIB::SQRT_F32, "__hexagon_sqrtf");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001606
1607 // These cause problems when the shift amount is non-constant.
1608 setLibcallName(RTLIB::SHL_I128, nullptr);
1609 setLibcallName(RTLIB::SRL_I128, nullptr);
1610 setLibcallName(RTLIB::SRA_I128, nullptr);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001611}
1612
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001613const char* HexagonTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00001614 switch ((HexagonISD::NodeType)Opcode) {
Krzysztof Parzyszek0b6187c2018-06-01 14:00:32 +00001615 case HexagonISD::ADDC: return "HexagonISD::ADDC";
1616 case HexagonISD::SUBC: return "HexagonISD::SUBC";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001617 case HexagonISD::ALLOCA: return "HexagonISD::ALLOCA";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001618 case HexagonISD::AT_GOT: return "HexagonISD::AT_GOT";
1619 case HexagonISD::AT_PCREL: return "HexagonISD::AT_PCREL";
1620 case HexagonISD::BARRIER: return "HexagonISD::BARRIER";
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00001621 case HexagonISD::CALL: return "HexagonISD::CALL";
1622 case HexagonISD::CALLnr: return "HexagonISD::CALLnr";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001623 case HexagonISD::CALLR: return "HexagonISD::CALLR";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001624 case HexagonISD::COMBINE: return "HexagonISD::COMBINE";
1625 case HexagonISD::CONST32_GP: return "HexagonISD::CONST32_GP";
1626 case HexagonISD::CONST32: return "HexagonISD::CONST32";
1627 case HexagonISD::CP: return "HexagonISD::CP";
1628 case HexagonISD::DCFETCH: return "HexagonISD::DCFETCH";
1629 case HexagonISD::EH_RETURN: return "HexagonISD::EH_RETURN";
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001630 case HexagonISD::TSTBIT: return "HexagonISD::TSTBIT";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001631 case HexagonISD::EXTRACTU: return "HexagonISD::EXTRACTU";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001632 case HexagonISD::INSERT: return "HexagonISD::INSERT";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001633 case HexagonISD::JT: return "HexagonISD::JT";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001634 case HexagonISD::RET_FLAG: return "HexagonISD::RET_FLAG";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001635 case HexagonISD::TC_RETURN: return "HexagonISD::TC_RETURN";
Krzysztof Parzyszekf85dd9f2017-07-10 20:16:44 +00001636 case HexagonISD::VASL: return "HexagonISD::VASL";
1637 case HexagonISD::VASR: return "HexagonISD::VASR";
1638 case HexagonISD::VLSR: return "HexagonISD::VLSR";
1639 case HexagonISD::VSPLAT: return "HexagonISD::VSPLAT";
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001640 case HexagonISD::VEXTRACTW: return "HexagonISD::VEXTRACTW";
1641 case HexagonISD::VINSERTW0: return "HexagonISD::VINSERTW0";
1642 case HexagonISD::VROR: return "HexagonISD::VROR";
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +00001643 case HexagonISD::READCYCLE: return "HexagonISD::READCYCLE";
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00001644 case HexagonISD::VZERO: return "HexagonISD::VZERO";
Krzysztof Parzyszek41a24b72018-04-20 19:38:37 +00001645 case HexagonISD::VSPLATW: return "HexagonISD::VSPLATW";
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001646 case HexagonISD::D2P: return "HexagonISD::D2P";
1647 case HexagonISD::P2D: return "HexagonISD::P2D";
1648 case HexagonISD::V2Q: return "HexagonISD::V2Q";
1649 case HexagonISD::Q2V: return "HexagonISD::Q2V";
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00001650 case HexagonISD::QCAT: return "HexagonISD::QCAT";
Krzysztof Parzyszek69f1d7e2018-02-06 14:16:52 +00001651 case HexagonISD::QTRUE: return "HexagonISD::QTRUE";
1652 case HexagonISD::QFALSE: return "HexagonISD::QFALSE";
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001653 case HexagonISD::TYPECAST: return "HexagonISD::TYPECAST";
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00001654 case HexagonISD::VALIGN: return "HexagonISD::VALIGN";
Krzysztof Parzyszekad83ce42018-02-14 20:46:06 +00001655 case HexagonISD::VALIGNADDR: return "HexagonISD::VALIGNADDR";
Matthias Braund04893f2015-05-07 21:33:59 +00001656 case HexagonISD::OP_END: break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001657 }
Matthias Braund04893f2015-05-07 21:33:59 +00001658 return nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001659}
1660
Krzysztof Parzyszek1df70592018-08-08 17:00:09 +00001661void
1662HexagonTargetLowering::validateConstPtrAlignment(SDValue Ptr, const SDLoc &dl,
1663 unsigned NeedAlign) const {
1664 auto *CA = dyn_cast<ConstantSDNode>(Ptr);
1665 if (!CA)
1666 return;
1667 unsigned Addr = CA->getZExtValue();
1668 unsigned HaveAlign = Addr != 0 ? 1u << countTrailingZeros(Addr) : NeedAlign;
1669 if (HaveAlign < NeedAlign) {
1670 std::string ErrMsg;
1671 raw_string_ostream O(ErrMsg);
1672 O << "Misaligned constant address: " << format_hex(Addr, 10)
1673 << " has alignment " << HaveAlign
1674 << ", but the memory access requires " << NeedAlign;
1675 if (DebugLoc DL = dl.getDebugLoc())
1676 DL.print(O << ", at ");
1677 report_fatal_error(O.str());
1678 }
1679}
1680
Krzysztof Parzyszekdc7a5572018-03-29 13:52:46 +00001681// Bit-reverse Load Intrinsic: Check if the instruction is a bit reverse load
1682// intrinsic.
1683static bool isBrevLdIntrinsic(const Value *Inst) {
1684 unsigned ID = cast<IntrinsicInst>(Inst)->getIntrinsicID();
1685 return (ID == Intrinsic::hexagon_L2_loadrd_pbr ||
1686 ID == Intrinsic::hexagon_L2_loadri_pbr ||
1687 ID == Intrinsic::hexagon_L2_loadrh_pbr ||
1688 ID == Intrinsic::hexagon_L2_loadruh_pbr ||
1689 ID == Intrinsic::hexagon_L2_loadrb_pbr ||
1690 ID == Intrinsic::hexagon_L2_loadrub_pbr);
1691}
1692
1693// Bit-reverse Load Intrinsic :Crawl up and figure out the object from previous
1694// instruction. So far we only handle bitcast, extract value and bit reverse
1695// load intrinsic instructions. Should we handle CGEP ?
1696static Value *getBrevLdObject(Value *V) {
1697 if (Operator::getOpcode(V) == Instruction::ExtractValue ||
1698 Operator::getOpcode(V) == Instruction::BitCast)
1699 V = cast<Operator>(V)->getOperand(0);
1700 else if (isa<IntrinsicInst>(V) && isBrevLdIntrinsic(V))
1701 V = cast<Instruction>(V)->getOperand(0);
1702 return V;
1703}
1704
1705// Bit-reverse Load Intrinsic: For a PHI Node return either an incoming edge or
1706// a back edge. If the back edge comes from the intrinsic itself, the incoming
1707// edge is returned.
1708static Value *returnEdge(const PHINode *PN, Value *IntrBaseVal) {
1709 const BasicBlock *Parent = PN->getParent();
1710 int Idx = -1;
1711 for (unsigned i = 0, e = PN->getNumIncomingValues(); i < e; ++i) {
1712 BasicBlock *Blk = PN->getIncomingBlock(i);
1713 // Determine if the back edge is originated from intrinsic.
1714 if (Blk == Parent) {
1715 Value *BackEdgeVal = PN->getIncomingValue(i);
1716 Value *BaseVal;
1717 // Loop over till we return the same Value or we hit the IntrBaseVal.
1718 do {
1719 BaseVal = BackEdgeVal;
1720 BackEdgeVal = getBrevLdObject(BackEdgeVal);
1721 } while ((BaseVal != BackEdgeVal) && (IntrBaseVal != BackEdgeVal));
1722 // If the getBrevLdObject returns IntrBaseVal, we should return the
1723 // incoming edge.
1724 if (IntrBaseVal == BackEdgeVal)
1725 continue;
1726 Idx = i;
1727 break;
1728 } else // Set the node to incoming edge.
1729 Idx = i;
1730 }
1731 assert(Idx >= 0 && "Unexpected index to incoming argument in PHI");
1732 return PN->getIncomingValue(Idx);
1733}
1734
1735// Bit-reverse Load Intrinsic: Figure out the underlying object the base
1736// pointer points to, for the bit-reverse load intrinsic. Setting this to
1737// memoperand might help alias analysis to figure out the dependencies.
1738static Value *getUnderLyingObjectForBrevLdIntr(Value *V) {
1739 Value *IntrBaseVal = V;
1740 Value *BaseVal;
1741 // Loop over till we return the same Value, implies we either figure out
1742 // the object or we hit a PHI
1743 do {
1744 BaseVal = V;
1745 V = getBrevLdObject(V);
1746 } while (BaseVal != V);
1747
1748 // Identify the object from PHINode.
1749 if (const PHINode *PN = dyn_cast<PHINode>(V))
1750 return returnEdge(PN, IntrBaseVal);
1751 // For non PHI nodes, the object is the last value returned by getBrevLdObject
1752 else
1753 return V;
1754}
1755
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001756/// Given an intrinsic, checks if on the target the intrinsic will need to map
1757/// to a MemIntrinsicNode (touches memory). If this is the case, it returns
1758/// true and store the intrinsic information into the IntrinsicInfo that was
1759/// passed to the function.
1760bool HexagonTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
1761 const CallInst &I,
Matt Arsenault7d7adf42017-12-14 22:34:10 +00001762 MachineFunction &MF,
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001763 unsigned Intrinsic) const {
1764 switch (Intrinsic) {
Krzysztof Parzyszekdc7a5572018-03-29 13:52:46 +00001765 case Intrinsic::hexagon_L2_loadrd_pbr:
1766 case Intrinsic::hexagon_L2_loadri_pbr:
1767 case Intrinsic::hexagon_L2_loadrh_pbr:
1768 case Intrinsic::hexagon_L2_loadruh_pbr:
1769 case Intrinsic::hexagon_L2_loadrb_pbr:
1770 case Intrinsic::hexagon_L2_loadrub_pbr: {
1771 Info.opc = ISD::INTRINSIC_W_CHAIN;
1772 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
1773 auto &Cont = I.getCalledFunction()->getParent()->getContext();
1774 // The intrinsic function call is of the form { ElTy, i8* }
1775 // @llvm.hexagon.L2.loadXX.pbr(i8*, i32). The pointer and memory access type
1776 // should be derived from ElTy.
James Y Knight62df5ee2019-01-10 16:07:20 +00001777 Type *ElTy = I.getCalledFunction()->getReturnType()->getStructElementType(0);
1778 Info.memVT = MVT::getVT(ElTy);
Krzysztof Parzyszekdc7a5572018-03-29 13:52:46 +00001779 llvm::Value *BasePtrVal = I.getOperand(0);
1780 Info.ptrVal = getUnderLyingObjectForBrevLdIntr(BasePtrVal);
1781 // The offset value comes through Modifier register. For now, assume the
1782 // offset is 0.
1783 Info.offset = 0;
1784 Info.align = DL.getABITypeAlignment(Info.memVT.getTypeForEVT(Cont));
1785 Info.flags = MachineMemOperand::MOLoad;
1786 return true;
1787 }
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001788 case Intrinsic::hexagon_V6_vgathermw:
1789 case Intrinsic::hexagon_V6_vgathermw_128B:
1790 case Intrinsic::hexagon_V6_vgathermh:
1791 case Intrinsic::hexagon_V6_vgathermh_128B:
1792 case Intrinsic::hexagon_V6_vgathermhw:
1793 case Intrinsic::hexagon_V6_vgathermhw_128B:
1794 case Intrinsic::hexagon_V6_vgathermwq:
1795 case Intrinsic::hexagon_V6_vgathermwq_128B:
1796 case Intrinsic::hexagon_V6_vgathermhq:
1797 case Intrinsic::hexagon_V6_vgathermhq_128B:
1798 case Intrinsic::hexagon_V6_vgathermhwq:
1799 case Intrinsic::hexagon_V6_vgathermhwq_128B: {
1800 const Module &M = *I.getParent()->getParent()->getParent();
1801 Info.opc = ISD::INTRINSIC_W_CHAIN;
1802 Type *VecTy = I.getArgOperand(1)->getType();
1803 Info.memVT = MVT::getVT(VecTy);
1804 Info.ptrVal = I.getArgOperand(0);
1805 Info.offset = 0;
1806 Info.align = M.getDataLayout().getTypeAllocSizeInBits(VecTy) / 8;
Matt Arsenault11171332017-12-14 21:39:51 +00001807 Info.flags = MachineMemOperand::MOLoad |
1808 MachineMemOperand::MOStore |
1809 MachineMemOperand::MOVolatile;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001810 return true;
1811 }
1812 default:
1813 break;
1814 }
1815 return false;
1816}
1817
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001818bool HexagonTargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00001819 return isTruncateFree(EVT::getEVT(Ty1), EVT::getEVT(Ty2));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001820}
1821
1822bool HexagonTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001823 if (!VT1.isSimple() || !VT2.isSimple())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001824 return false;
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00001825 return VT1.getSimpleVT() == MVT::i64 && VT2.getSimpleVT() == MVT::i32;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001826}
1827
Krzysztof Parzyszekbd8ef4b2016-08-19 13:34:31 +00001828bool HexagonTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
1829 return isOperationLegalOrCustom(ISD::FMA, VT);
1830}
1831
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001832// Should we expand the build vector with shuffles?
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00001833bool HexagonTargetLowering::shouldExpandBuildVectorWithShuffles(EVT VT,
1834 unsigned DefinedValues) const {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001835 return false;
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00001836}
1837
Zvi Rackover1b736822017-07-26 08:06:58 +00001838bool HexagonTargetLowering::isShuffleMaskLegal(ArrayRef<int> Mask,
1839 EVT VT) const {
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00001840 return true;
1841}
1842
Krzysztof Parzyszek5439a702017-12-18 18:21:01 +00001843TargetLoweringBase::LegalizeTypeAction
Craig Topper0b5f8162018-11-05 23:26:13 +00001844HexagonTargetLowering::getPreferredVectorAction(MVT VT) const {
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00001845 if (VT.getVectorNumElements() == 1)
1846 return TargetLoweringBase::TypeScalarizeVector;
1847
1848 // Always widen vectors of i1.
Craig Topper0b5f8162018-11-05 23:26:13 +00001849 MVT ElemTy = VT.getVectorElementType();
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00001850 if (ElemTy == MVT::i1)
1851 return TargetLoweringBase::TypeWidenVector;
1852
Krzysztof Parzyszek5439a702017-12-18 18:21:01 +00001853 if (Subtarget.useHVXOps()) {
1854 // If the size of VT is at least half of the vector length,
1855 // widen the vector. Note: the threshold was not selected in
1856 // any scientific way.
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00001857 ArrayRef<MVT> Tys = Subtarget.getHVXElementTypes();
1858 if (llvm::find(Tys, ElemTy) != Tys.end()) {
1859 unsigned HwWidth = 8*Subtarget.getVectorLength();
1860 unsigned VecWidth = VT.getSizeInBits();
1861 if (VecWidth >= HwWidth/2 && VecWidth < HwWidth)
1862 return TargetLoweringBase::TypeWidenVector;
1863 }
Krzysztof Parzyszek5439a702017-12-18 18:21:01 +00001864 }
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00001865 return TargetLoweringBase::TypeSplitVector;
Krzysztof Parzyszek5439a702017-12-18 18:21:01 +00001866}
1867
Krzysztof Parzyszekad83ce42018-02-14 20:46:06 +00001868std::pair<SDValue, int>
1869HexagonTargetLowering::getBaseAndOffset(SDValue Addr) const {
1870 if (Addr.getOpcode() == ISD::ADD) {
1871 SDValue Op1 = Addr.getOperand(1);
1872 if (auto *CN = dyn_cast<const ConstantSDNode>(Op1.getNode()))
1873 return { Addr.getOperand(0), CN->getSExtValue() };
1874 }
1875 return { Addr, 0 };
1876}
1877
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00001878// Lower a vector shuffle (V1, V2, V3). V1 and V2 are the two vectors
1879// to select data from, V3 is the permutation.
1880SDValue
1881HexagonTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG)
1882 const {
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001883 const auto *SVN = cast<ShuffleVectorSDNode>(Op);
1884 ArrayRef<int> AM = SVN->getMask();
1885 assert(AM.size() <= 8 && "Unexpected shuffle mask");
1886 unsigned VecLen = AM.size();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001887
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001888 MVT VecTy = ty(Op);
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00001889 assert(!Subtarget.isHVXVectorType(VecTy, true) &&
1890 "HVX shuffles should be legal");
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001891 assert(VecTy.getSizeInBits() <= 64 && "Unexpected vector length");
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001892
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001893 SDValue Op0 = Op.getOperand(0);
1894 SDValue Op1 = Op.getOperand(1);
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +00001895 const SDLoc &dl(Op);
1896
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001897 // If the inputs are not the same as the output, bail. This is not an
1898 // error situation, but complicates the handling and the default expansion
1899 // (into BUILD_VECTOR) should be adequate.
1900 if (ty(Op0) != VecTy || ty(Op1) != VecTy)
1901 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001902
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001903 // Normalize the mask so that the first non-negative index comes from
1904 // the first operand.
1905 SmallVector<int,8> Mask(AM.begin(), AM.end());
1906 unsigned F = llvm::find_if(AM, [](int M) { return M >= 0; }) - AM.data();
1907 if (F == AM.size())
1908 return DAG.getUNDEF(VecTy);
1909 if (AM[F] >= int(VecLen)) {
1910 ShuffleVectorSDNode::commuteMask(Mask);
1911 std::swap(Op0, Op1);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001912 }
1913
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001914 // Express the shuffle mask in terms of bytes.
1915 SmallVector<int,8> ByteMask;
1916 unsigned ElemBytes = VecTy.getVectorElementType().getSizeInBits() / 8;
1917 for (unsigned i = 0, e = Mask.size(); i != e; ++i) {
1918 int M = Mask[i];
1919 if (M < 0) {
1920 for (unsigned j = 0; j != ElemBytes; ++j)
1921 ByteMask.push_back(-1);
1922 } else {
1923 for (unsigned j = 0; j != ElemBytes; ++j)
1924 ByteMask.push_back(M*ElemBytes + j);
1925 }
1926 }
1927 assert(ByteMask.size() <= 8);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001928
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001929 // All non-undef (non-negative) indexes are well within [0..127], so they
1930 // fit in a single byte. Build two 64-bit words:
1931 // - MaskIdx where each byte is the corresponding index (for non-negative
1932 // indexes), and 0xFF for negative indexes, and
1933 // - MaskUnd that has 0xFF for each negative index.
1934 uint64_t MaskIdx = 0;
1935 uint64_t MaskUnd = 0;
1936 for (unsigned i = 0, e = ByteMask.size(); i != e; ++i) {
1937 unsigned S = 8*i;
1938 uint64_t M = ByteMask[i] & 0xFF;
1939 if (M == 0xFF)
1940 MaskUnd |= M << S;
1941 MaskIdx |= M << S;
1942 }
1943
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001944 if (ByteMask.size() == 4) {
1945 // Identity.
1946 if (MaskIdx == (0x03020100 | MaskUnd))
1947 return Op0;
1948 // Byte swap.
1949 if (MaskIdx == (0x00010203 | MaskUnd)) {
1950 SDValue T0 = DAG.getBitcast(MVT::i32, Op0);
1951 SDValue T1 = DAG.getNode(ISD::BSWAP, dl, MVT::i32, T0);
1952 return DAG.getBitcast(VecTy, T1);
1953 }
1954
1955 // Byte packs.
1956 SDValue Concat10 = DAG.getNode(HexagonISD::COMBINE, dl,
1957 typeJoin({ty(Op1), ty(Op0)}), {Op1, Op0});
1958 if (MaskIdx == (0x06040200 | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001959 return getInstr(Hexagon::S2_vtrunehb, dl, VecTy, {Concat10}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001960 if (MaskIdx == (0x07050301 | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001961 return getInstr(Hexagon::S2_vtrunohb, dl, VecTy, {Concat10}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001962
1963 SDValue Concat01 = DAG.getNode(HexagonISD::COMBINE, dl,
1964 typeJoin({ty(Op0), ty(Op1)}), {Op0, Op1});
1965 if (MaskIdx == (0x02000604 | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001966 return getInstr(Hexagon::S2_vtrunehb, dl, VecTy, {Concat01}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001967 if (MaskIdx == (0x03010705 | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001968 return getInstr(Hexagon::S2_vtrunohb, dl, VecTy, {Concat01}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001969 }
1970
1971 if (ByteMask.size() == 8) {
1972 // Identity.
1973 if (MaskIdx == (0x0706050403020100ull | MaskUnd))
1974 return Op0;
1975 // Byte swap.
1976 if (MaskIdx == (0x0001020304050607ull | MaskUnd)) {
1977 SDValue T0 = DAG.getBitcast(MVT::i64, Op0);
1978 SDValue T1 = DAG.getNode(ISD::BSWAP, dl, MVT::i64, T0);
1979 return DAG.getBitcast(VecTy, T1);
1980 }
1981
1982 // Halfword picks.
1983 if (MaskIdx == (0x0d0c050409080100ull | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001984 return getInstr(Hexagon::S2_shuffeh, dl, VecTy, {Op1, Op0}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001985 if (MaskIdx == (0x0f0e07060b0a0302ull | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001986 return getInstr(Hexagon::S2_shuffoh, dl, VecTy, {Op1, Op0}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001987 if (MaskIdx == (0x0d0c090805040100ull | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001988 return getInstr(Hexagon::S2_vtrunewh, dl, VecTy, {Op1, Op0}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001989 if (MaskIdx == (0x0f0e0b0a07060302ull | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001990 return getInstr(Hexagon::S2_vtrunowh, dl, VecTy, {Op1, Op0}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001991 if (MaskIdx == (0x0706030205040100ull | MaskUnd)) {
1992 VectorPair P = opSplit(Op0, dl, DAG);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001993 return getInstr(Hexagon::S2_packhl, dl, VecTy, {P.second, P.first}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001994 }
1995
1996 // Byte packs.
1997 if (MaskIdx == (0x0e060c040a020800ull | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001998 return getInstr(Hexagon::S2_shuffeb, dl, VecTy, {Op1, Op0}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001999 if (MaskIdx == (0x0f070d050b030901ull | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002000 return getInstr(Hexagon::S2_shuffob, dl, VecTy, {Op1, Op0}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00002001 }
2002
2003 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002004}
2005
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00002006// Create a Hexagon-specific node for shifting a vector by an integer.
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002007SDValue
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00002008HexagonTargetLowering::getVectorShiftByInt(SDValue Op, SelectionDAG &DAG)
2009 const {
Krzysztof Parzyszek1108ee22018-01-31 20:49:24 +00002010 if (auto *BVN = dyn_cast<BuildVectorSDNode>(Op.getOperand(1).getNode())) {
2011 if (SDValue S = BVN->getSplatValue()) {
2012 unsigned NewOpc;
2013 switch (Op.getOpcode()) {
2014 case ISD::SHL:
2015 NewOpc = HexagonISD::VASL;
2016 break;
2017 case ISD::SRA:
2018 NewOpc = HexagonISD::VASR;
2019 break;
2020 case ISD::SRL:
2021 NewOpc = HexagonISD::VLSR;
2022 break;
2023 default:
2024 llvm_unreachable("Unexpected shift opcode");
2025 }
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00002026 return DAG.getNode(NewOpc, SDLoc(Op), ty(Op), Op.getOperand(0), S);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002027 }
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002028 }
2029
Krzysztof Parzyszek1108ee22018-01-31 20:49:24 +00002030 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002031}
2032
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002033SDValue
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00002034HexagonTargetLowering::LowerVECTOR_SHIFT(SDValue Op, SelectionDAG &DAG) const {
2035 return getVectorShiftByInt(Op, DAG);
2036}
2037
2038SDValue
Krzysztof Parzyszek3d671242018-06-12 12:49:36 +00002039HexagonTargetLowering::LowerROTL(SDValue Op, SelectionDAG &DAG) const {
2040 if (isa<ConstantSDNode>(Op.getOperand(1).getNode()))
2041 return Op;
2042 return SDValue();
2043}
2044
2045SDValue
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002046HexagonTargetLowering::LowerBITCAST(SDValue Op, SelectionDAG &DAG) const {
2047 MVT ResTy = ty(Op);
2048 SDValue InpV = Op.getOperand(0);
2049 MVT InpTy = ty(InpV);
2050 assert(ResTy.getSizeInBits() == InpTy.getSizeInBits());
2051 const SDLoc &dl(Op);
2052
2053 // Handle conversion from i8 to v8i1.
2054 if (ResTy == MVT::v8i1) {
2055 SDValue Sc = DAG.getBitcast(tyScalar(InpTy), InpV);
2056 SDValue Ext = DAG.getZExtOrTrunc(Sc, dl, MVT::i32);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002057 return getInstr(Hexagon::C2_tfrrp, dl, ResTy, Ext, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002058 }
2059
2060 return SDValue();
2061}
2062
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002063bool
2064HexagonTargetLowering::getBuildVectorConstInts(ArrayRef<SDValue> Values,
2065 MVT VecTy, SelectionDAG &DAG,
2066 MutableArrayRef<ConstantInt*> Consts) const {
2067 MVT ElemTy = VecTy.getVectorElementType();
2068 unsigned ElemWidth = ElemTy.getSizeInBits();
2069 IntegerType *IntTy = IntegerType::get(*DAG.getContext(), ElemWidth);
2070 bool AllConst = true;
2071
2072 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
2073 SDValue V = Values[i];
2074 if (V.isUndef()) {
2075 Consts[i] = ConstantInt::get(IntTy, 0);
2076 continue;
2077 }
Krzysztof Parzyszek4ef6cff2018-01-11 18:03:23 +00002078 // Make sure to always cast to IntTy.
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002079 if (auto *CN = dyn_cast<ConstantSDNode>(V.getNode())) {
2080 const ConstantInt *CI = CN->getConstantIntValue();
Krzysztof Parzyszek4ef6cff2018-01-11 18:03:23 +00002081 Consts[i] = ConstantInt::get(IntTy, CI->getValue().getSExtValue());
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002082 } else if (auto *CN = dyn_cast<ConstantFPSDNode>(V.getNode())) {
2083 const ConstantFP *CF = CN->getConstantFPValue();
2084 APInt A = CF->getValueAPF().bitcastToAPInt();
2085 Consts[i] = ConstantInt::get(IntTy, A.getZExtValue());
2086 } else {
2087 AllConst = false;
2088 }
2089 }
2090 return AllConst;
2091}
2092
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002093SDValue
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002094HexagonTargetLowering::buildVector32(ArrayRef<SDValue> Elem, const SDLoc &dl,
2095 MVT VecTy, SelectionDAG &DAG) const {
2096 MVT ElemTy = VecTy.getVectorElementType();
2097 assert(VecTy.getVectorNumElements() == Elem.size());
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002098
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002099 SmallVector<ConstantInt*,4> Consts(Elem.size());
2100 bool AllConst = getBuildVectorConstInts(Elem, VecTy, DAG, Consts);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002101
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002102 unsigned First, Num = Elem.size();
2103 for (First = 0; First != Num; ++First)
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002104 if (!isUndef(Elem[First]))
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002105 break;
2106 if (First == Num)
2107 return DAG.getUNDEF(VecTy);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002108
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002109 if (AllConst &&
2110 llvm::all_of(Consts, [](ConstantInt *CI) { return CI->isZero(); }))
2111 return getZero(dl, VecTy, DAG);
2112
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002113 if (ElemTy == MVT::i16) {
2114 assert(Elem.size() == 2);
2115 if (AllConst) {
2116 uint32_t V = (Consts[0]->getZExtValue() & 0xFFFF) |
2117 Consts[1]->getZExtValue() << 16;
2118 return DAG.getBitcast(MVT::v2i16, DAG.getConstant(V, dl, MVT::i32));
Krzysztof Parzyszek89b2d7c2017-07-13 18:17:58 +00002119 }
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002120 SDValue N = getInstr(Hexagon::A2_combine_ll, dl, MVT::i32,
2121 {Elem[1], Elem[0]}, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002122 return DAG.getBitcast(MVT::v2i16, N);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002123 }
2124
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002125 if (ElemTy == MVT::i8) {
2126 // First try generating a constant.
2127 if (AllConst) {
2128 int32_t V = (Consts[0]->getZExtValue() & 0xFF) |
2129 (Consts[1]->getZExtValue() & 0xFF) << 8 |
2130 (Consts[1]->getZExtValue() & 0xFF) << 16 |
2131 Consts[2]->getZExtValue() << 24;
2132 return DAG.getBitcast(MVT::v4i8, DAG.getConstant(V, dl, MVT::i32));
2133 }
2134
2135 // Then try splat.
2136 bool IsSplat = true;
2137 for (unsigned i = 0; i != Num; ++i) {
2138 if (i == First)
2139 continue;
2140 if (Elem[i] == Elem[First] || isUndef(Elem[i]))
2141 continue;
2142 IsSplat = false;
2143 break;
2144 }
2145 if (IsSplat) {
2146 // Legalize the operand to VSPLAT.
2147 SDValue Ext = DAG.getZExtOrTrunc(Elem[First], dl, MVT::i32);
2148 return DAG.getNode(HexagonISD::VSPLAT, dl, VecTy, Ext);
2149 }
2150
2151 // Generate
2152 // (zxtb(Elem[0]) | (zxtb(Elem[1]) << 8)) |
2153 // (zxtb(Elem[2]) | (zxtb(Elem[3]) << 8)) << 16
2154 assert(Elem.size() == 4);
2155 SDValue Vs[4];
2156 for (unsigned i = 0; i != 4; ++i) {
2157 Vs[i] = DAG.getZExtOrTrunc(Elem[i], dl, MVT::i32);
2158 Vs[i] = DAG.getZeroExtendInReg(Vs[i], dl, MVT::i8);
2159 }
2160 SDValue S8 = DAG.getConstant(8, dl, MVT::i32);
2161 SDValue T0 = DAG.getNode(ISD::SHL, dl, MVT::i32, {Vs[1], S8});
2162 SDValue T1 = DAG.getNode(ISD::SHL, dl, MVT::i32, {Vs[3], S8});
2163 SDValue B0 = DAG.getNode(ISD::OR, dl, MVT::i32, {Vs[0], T0});
2164 SDValue B1 = DAG.getNode(ISD::OR, dl, MVT::i32, {Vs[2], T1});
2165
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002166 SDValue R = getInstr(Hexagon::A2_combine_ll, dl, MVT::i32, {B1, B0}, DAG);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002167 return DAG.getBitcast(MVT::v4i8, R);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002168 }
2169
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002170#ifndef NDEBUG
2171 dbgs() << "VecTy: " << EVT(VecTy).getEVTString() << '\n';
2172#endif
2173 llvm_unreachable("Unexpected vector element type");
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002174}
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002175
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002176SDValue
2177HexagonTargetLowering::buildVector64(ArrayRef<SDValue> Elem, const SDLoc &dl,
2178 MVT VecTy, SelectionDAG &DAG) const {
2179 MVT ElemTy = VecTy.getVectorElementType();
2180 assert(VecTy.getVectorNumElements() == Elem.size());
2181
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002182 SmallVector<ConstantInt*,8> Consts(Elem.size());
2183 bool AllConst = getBuildVectorConstInts(Elem, VecTy, DAG, Consts);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002184
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002185 unsigned First, Num = Elem.size();
2186 for (First = 0; First != Num; ++First)
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002187 if (!isUndef(Elem[First]))
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002188 break;
2189 if (First == Num)
2190 return DAG.getUNDEF(VecTy);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002191
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002192 if (AllConst &&
2193 llvm::all_of(Consts, [](ConstantInt *CI) { return CI->isZero(); }))
2194 return getZero(dl, VecTy, DAG);
2195
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002196 // First try splat if possible.
2197 if (ElemTy == MVT::i16) {
2198 bool IsSplat = true;
2199 for (unsigned i = 0; i != Num; ++i) {
2200 if (i == First)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002201 continue;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002202 if (Elem[i] == Elem[First] || isUndef(Elem[i]))
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002203 continue;
2204 IsSplat = false;
2205 break;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002206 }
Krzysztof Parzyszekfb0fcac2017-12-20 20:33:49 +00002207 if (IsSplat) {
2208 // Legalize the operand to VSPLAT.
2209 SDValue Ext = DAG.getZExtOrTrunc(Elem[First], dl, MVT::i32);
2210 return DAG.getNode(HexagonISD::VSPLAT, dl, VecTy, Ext);
2211 }
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002212 }
2213
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002214 // Then try constant.
2215 if (AllConst) {
2216 uint64_t Val = 0;
2217 unsigned W = ElemTy.getSizeInBits();
2218 uint64_t Mask = (ElemTy == MVT::i8) ? 0xFFull
2219 : (ElemTy == MVT::i16) ? 0xFFFFull : 0xFFFFFFFFull;
2220 for (unsigned i = 0; i != Num; ++i)
Krzysztof Parzyszek240df6f2018-01-11 18:30:41 +00002221 Val = (Val << W) | (Consts[Num-1-i]->getZExtValue() & Mask);
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002222 SDValue V0 = DAG.getConstant(Val, dl, MVT::i64);
2223 return DAG.getBitcast(VecTy, V0);
2224 }
2225
2226 // Build two 32-bit vectors and concatenate.
2227 MVT HalfTy = MVT::getVectorVT(ElemTy, Num/2);
2228 SDValue L = (ElemTy == MVT::i32)
2229 ? Elem[0]
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002230 : buildVector32(Elem.take_front(Num/2), dl, HalfTy, DAG);
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002231 SDValue H = (ElemTy == MVT::i32)
2232 ? Elem[1]
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002233 : buildVector32(Elem.drop_front(Num/2), dl, HalfTy, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002234 return DAG.getNode(HexagonISD::COMBINE, dl, VecTy, {H, L});
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002235}
2236
2237SDValue
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002238HexagonTargetLowering::extractVector(SDValue VecV, SDValue IdxV,
2239 const SDLoc &dl, MVT ValTy, MVT ResTy,
2240 SelectionDAG &DAG) const {
2241 MVT VecTy = ty(VecV);
2242 assert(!ValTy.isVector() ||
2243 VecTy.getVectorElementType() == ValTy.getVectorElementType());
2244 unsigned VecWidth = VecTy.getSizeInBits();
2245 unsigned ValWidth = ValTy.getSizeInBits();
2246 unsigned ElemWidth = VecTy.getVectorElementType().getSizeInBits();
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002247 assert((VecWidth % ElemWidth) == 0);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002248 auto *IdxN = dyn_cast<ConstantSDNode>(IdxV);
2249
2250 // Special case for v{8,4,2}i1 (the only boolean vectors legal in Hexagon
2251 // without any coprocessors).
2252 if (ElemWidth == 1) {
2253 assert(VecWidth == VecTy.getVectorNumElements() && "Sanity failure");
2254 assert(VecWidth == 8 || VecWidth == 4 || VecWidth == 2);
2255 // Check if this is an extract of the lowest bit.
2256 if (IdxN) {
2257 // Extracting the lowest bit is a no-op, but it changes the type,
2258 // so it must be kept as an operation to avoid errors related to
2259 // type mismatches.
2260 if (IdxN->isNullValue() && ValTy.getSizeInBits() == 1)
2261 return DAG.getNode(HexagonISD::TYPECAST, dl, MVT::i1, VecV);
2262 }
2263
2264 // If the value extracted is a single bit, use tstbit.
2265 if (ValWidth == 1) {
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002266 SDValue A0 = getInstr(Hexagon::C2_tfrpr, dl, MVT::i32, {VecV}, DAG);
Krzysztof Parzyszek4e075092018-07-25 16:20:59 +00002267 SDValue M0 = DAG.getConstant(8 / VecWidth, dl, MVT::i32);
2268 SDValue I0 = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV, M0);
2269 return DAG.getNode(HexagonISD::TSTBIT, dl, MVT::i1, A0, I0);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002270 }
2271
2272 // Each bool vector (v2i1, v4i1, v8i1) always occupies 8 bits in
2273 // a predicate register. The elements of the vector are repeated
2274 // in the register (if necessary) so that the total number is 8.
2275 // The extracted subvector will need to be expanded in such a way.
2276 unsigned Scale = VecWidth / ValWidth;
2277
2278 // Generate (p2d VecV) >> 8*Idx to move the interesting bytes to
2279 // position 0.
2280 assert(ty(IdxV) == MVT::i32);
Krzysztof Parzyszek528aff32018-10-02 15:05:43 +00002281 unsigned VecRep = 8 / VecWidth;
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002282 SDValue S0 = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV,
Krzysztof Parzyszek528aff32018-10-02 15:05:43 +00002283 DAG.getConstant(8*VecRep, dl, MVT::i32));
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002284 SDValue T0 = DAG.getNode(HexagonISD::P2D, dl, MVT::i64, VecV);
2285 SDValue T1 = DAG.getNode(ISD::SRL, dl, MVT::i64, T0, S0);
2286 while (Scale > 1) {
2287 // The longest possible subvector is at most 32 bits, so it is always
2288 // contained in the low subregister.
2289 T1 = DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::i32, T1);
2290 T1 = expandPredicate(T1, dl, DAG);
2291 Scale /= 2;
2292 }
2293
2294 return DAG.getNode(HexagonISD::D2P, dl, ResTy, T1);
2295 }
2296
2297 assert(VecWidth == 32 || VecWidth == 64);
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002298
2299 // Cast everything to scalar integer types.
2300 MVT ScalarTy = tyScalar(VecTy);
2301 VecV = DAG.getBitcast(ScalarTy, VecV);
2302
2303 SDValue WidthV = DAG.getConstant(ValWidth, dl, MVT::i32);
2304 SDValue ExtV;
2305
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002306 if (IdxN) {
2307 unsigned Off = IdxN->getZExtValue() * ElemWidth;
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002308 if (VecWidth == 64 && ValWidth == 32) {
2309 assert(Off == 0 || Off == 32);
2310 unsigned SubIdx = Off == 0 ? Hexagon::isub_lo : Hexagon::isub_hi;
2311 ExtV = DAG.getTargetExtractSubreg(SubIdx, dl, MVT::i32, VecV);
2312 } else if (Off == 0 && (ValWidth % 8) == 0) {
2313 ExtV = DAG.getZeroExtendInReg(VecV, dl, tyScalar(ValTy));
2314 } else {
2315 SDValue OffV = DAG.getConstant(Off, dl, MVT::i32);
2316 // The return type of EXTRACTU must be the same as the type of the
2317 // input vector.
2318 ExtV = DAG.getNode(HexagonISD::EXTRACTU, dl, ScalarTy,
2319 {VecV, WidthV, OffV});
2320 }
2321 } else {
2322 if (ty(IdxV) != MVT::i32)
2323 IdxV = DAG.getZExtOrTrunc(IdxV, dl, MVT::i32);
2324 SDValue OffV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV,
2325 DAG.getConstant(ElemWidth, dl, MVT::i32));
Krzysztof Parzyszekb1b29602018-01-04 13:56:04 +00002326 ExtV = DAG.getNode(HexagonISD::EXTRACTU, dl, ScalarTy,
2327 {VecV, WidthV, OffV});
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002328 }
2329
2330 // Cast ExtV to the requested result type.
2331 ExtV = DAG.getZExtOrTrunc(ExtV, dl, tyScalar(ResTy));
2332 ExtV = DAG.getBitcast(ResTy, ExtV);
2333 return ExtV;
2334}
2335
2336SDValue
2337HexagonTargetLowering::insertVector(SDValue VecV, SDValue ValV, SDValue IdxV,
2338 const SDLoc &dl, MVT ValTy,
2339 SelectionDAG &DAG) const {
2340 MVT VecTy = ty(VecV);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002341 if (VecTy.getVectorElementType() == MVT::i1) {
2342 MVT ValTy = ty(ValV);
2343 assert(ValTy.getVectorElementType() == MVT::i1);
2344 SDValue ValR = DAG.getNode(HexagonISD::P2D, dl, MVT::i64, ValV);
2345 unsigned VecLen = VecTy.getVectorNumElements();
2346 unsigned Scale = VecLen / ValTy.getVectorNumElements();
2347 assert(Scale > 1);
2348
2349 for (unsigned R = Scale; R > 1; R /= 2) {
2350 ValR = contractPredicate(ValR, dl, DAG);
2351 ValR = DAG.getNode(HexagonISD::COMBINE, dl, MVT::i64,
2352 DAG.getUNDEF(MVT::i32), ValR);
2353 }
2354 // The longest possible subvector is at most 32 bits, so it is always
2355 // contained in the low subregister.
2356 ValR = DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::i32, ValR);
2357
2358 unsigned ValBytes = 64 / Scale;
2359 SDValue Width = DAG.getConstant(ValBytes*8, dl, MVT::i32);
2360 SDValue Idx = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV,
2361 DAG.getConstant(8, dl, MVT::i32));
2362 SDValue VecR = DAG.getNode(HexagonISD::P2D, dl, MVT::i64, VecV);
2363 SDValue Ins = DAG.getNode(HexagonISD::INSERT, dl, MVT::i32,
2364 {VecR, ValR, Width, Idx});
2365 return DAG.getNode(HexagonISD::D2P, dl, VecTy, Ins);
2366 }
2367
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002368 unsigned VecWidth = VecTy.getSizeInBits();
2369 unsigned ValWidth = ValTy.getSizeInBits();
2370 assert(VecWidth == 32 || VecWidth == 64);
2371 assert((VecWidth % ValWidth) == 0);
2372
2373 // Cast everything to scalar integer types.
2374 MVT ScalarTy = MVT::getIntegerVT(VecWidth);
2375 // The actual type of ValV may be different than ValTy (which is related
2376 // to the vector type).
2377 unsigned VW = ty(ValV).getSizeInBits();
2378 ValV = DAG.getBitcast(MVT::getIntegerVT(VW), ValV);
2379 VecV = DAG.getBitcast(ScalarTy, VecV);
2380 if (VW != VecWidth)
2381 ValV = DAG.getAnyExtOrTrunc(ValV, dl, ScalarTy);
2382
2383 SDValue WidthV = DAG.getConstant(ValWidth, dl, MVT::i32);
2384 SDValue InsV;
2385
2386 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(IdxV)) {
2387 unsigned W = C->getZExtValue() * ValWidth;
2388 SDValue OffV = DAG.getConstant(W, dl, MVT::i32);
2389 InsV = DAG.getNode(HexagonISD::INSERT, dl, ScalarTy,
2390 {VecV, ValV, WidthV, OffV});
2391 } else {
2392 if (ty(IdxV) != MVT::i32)
2393 IdxV = DAG.getZExtOrTrunc(IdxV, dl, MVT::i32);
2394 SDValue OffV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV, WidthV);
Krzysztof Parzyszekb1b29602018-01-04 13:56:04 +00002395 InsV = DAG.getNode(HexagonISD::INSERT, dl, ScalarTy,
2396 {VecV, ValV, WidthV, OffV});
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002397 }
2398
2399 return DAG.getNode(ISD::BITCAST, dl, VecTy, InsV);
2400}
2401
2402SDValue
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002403HexagonTargetLowering::expandPredicate(SDValue Vec32, const SDLoc &dl,
2404 SelectionDAG &DAG) const {
2405 assert(ty(Vec32).getSizeInBits() == 32);
2406 if (isUndef(Vec32))
2407 return DAG.getUNDEF(MVT::i64);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002408 return getInstr(Hexagon::S2_vsxtbh, dl, MVT::i64, {Vec32}, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002409}
2410
2411SDValue
2412HexagonTargetLowering::contractPredicate(SDValue Vec64, const SDLoc &dl,
2413 SelectionDAG &DAG) const {
2414 assert(ty(Vec64).getSizeInBits() == 64);
2415 if (isUndef(Vec64))
2416 return DAG.getUNDEF(MVT::i32);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002417 return getInstr(Hexagon::S2_vtrunehb, dl, MVT::i32, {Vec64}, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002418}
2419
2420SDValue
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002421HexagonTargetLowering::getZero(const SDLoc &dl, MVT Ty, SelectionDAG &DAG)
2422 const {
2423 if (Ty.isVector()) {
2424 assert(Ty.isInteger() && "Only integer vectors are supported here");
2425 unsigned W = Ty.getSizeInBits();
2426 if (W <= 64)
2427 return DAG.getBitcast(Ty, DAG.getConstant(0, dl, MVT::getIntegerVT(W)));
2428 return DAG.getNode(HexagonISD::VZERO, dl, Ty);
2429 }
2430
2431 if (Ty.isInteger())
2432 return DAG.getConstant(0, dl, Ty);
2433 if (Ty.isFloatingPoint())
2434 return DAG.getConstantFP(0.0, dl, Ty);
2435 llvm_unreachable("Invalid type for zero");
2436}
2437
2438SDValue
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002439HexagonTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002440 MVT VecTy = ty(Op);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002441 unsigned BW = VecTy.getSizeInBits();
2442 const SDLoc &dl(Op);
2443 SmallVector<SDValue,8> Ops;
2444 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i)
2445 Ops.push_back(Op.getOperand(i));
2446
2447 if (BW == 32)
2448 return buildVector32(Ops, dl, VecTy, DAG);
2449 if (BW == 64)
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002450 return buildVector64(Ops, dl, VecTy, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002451
2452 if (VecTy == MVT::v8i1 || VecTy == MVT::v4i1 || VecTy == MVT::v2i1) {
2453 // For each i1 element in the resulting predicate register, put 1
2454 // shifted by the index of the element into a general-purpose register,
2455 // then or them together and transfer it back into a predicate register.
2456 SDValue Rs[8];
2457 SDValue Z = getZero(dl, MVT::i32, DAG);
2458 // Always produce 8 bits, repeat inputs if necessary.
2459 unsigned Rep = 8 / VecTy.getVectorNumElements();
2460 for (unsigned i = 0; i != 8; ++i) {
Simon Pilgrimc1e22902018-01-23 21:22:16 +00002461 SDValue S = DAG.getConstant(1ull << i, dl, MVT::i32);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002462 Rs[i] = DAG.getSelect(dl, MVT::i32, Ops[i/Rep], S, Z);
2463 }
2464 for (ArrayRef<SDValue> A(Rs); A.size() != 1; A = A.drop_back(A.size()/2)) {
2465 for (unsigned i = 0, e = A.size()/2; i != e; ++i)
2466 Rs[i] = DAG.getNode(ISD::OR, dl, MVT::i32, Rs[2*i], Rs[2*i+1]);
2467 }
2468 // Move the value directly to a predicate register.
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002469 return getInstr(Hexagon::C2_tfrrp, dl, VecTy, {Rs[0]}, DAG);
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002470 }
2471
2472 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002473}
2474
2475SDValue
2476HexagonTargetLowering::LowerCONCAT_VECTORS(SDValue Op,
2477 SelectionDAG &DAG) const {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002478 MVT VecTy = ty(Op);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002479 const SDLoc &dl(Op);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002480 if (VecTy.getSizeInBits() == 64) {
2481 assert(Op.getNumOperands() == 2);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002482 return DAG.getNode(HexagonISD::COMBINE, dl, VecTy, Op.getOperand(1),
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002483 Op.getOperand(0));
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002484 }
2485
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002486 MVT ElemTy = VecTy.getVectorElementType();
2487 if (ElemTy == MVT::i1) {
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002488 assert(VecTy == MVT::v2i1 || VecTy == MVT::v4i1 || VecTy == MVT::v8i1);
2489 MVT OpTy = ty(Op.getOperand(0));
2490 // Scale is how many times the operands need to be contracted to match
2491 // the representation in the target register.
2492 unsigned Scale = VecTy.getVectorNumElements() / OpTy.getVectorNumElements();
2493 assert(Scale == Op.getNumOperands() && Scale > 1);
2494
2495 // First, convert all bool vectors to integers, then generate pairwise
2496 // inserts to form values of doubled length. Up until there are only
2497 // two values left to concatenate, all of these values will fit in a
2498 // 32-bit integer, so keep them as i32 to use 32-bit inserts.
2499 SmallVector<SDValue,4> Words[2];
2500 unsigned IdxW = 0;
2501
2502 for (SDValue P : Op.getNode()->op_values()) {
2503 SDValue W = DAG.getNode(HexagonISD::P2D, dl, MVT::i64, P);
2504 for (unsigned R = Scale; R > 1; R /= 2) {
2505 W = contractPredicate(W, dl, DAG);
2506 W = DAG.getNode(HexagonISD::COMBINE, dl, MVT::i64,
2507 DAG.getUNDEF(MVT::i32), W);
2508 }
2509 W = DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::i32, W);
2510 Words[IdxW].push_back(W);
2511 }
2512
2513 while (Scale > 2) {
2514 SDValue WidthV = DAG.getConstant(64 / Scale, dl, MVT::i32);
2515 Words[IdxW ^ 1].clear();
2516
2517 for (unsigned i = 0, e = Words[IdxW].size(); i != e; i += 2) {
2518 SDValue W0 = Words[IdxW][i], W1 = Words[IdxW][i+1];
2519 // Insert W1 into W0 right next to the significant bits of W0.
2520 SDValue T = DAG.getNode(HexagonISD::INSERT, dl, MVT::i32,
2521 {W0, W1, WidthV, WidthV});
2522 Words[IdxW ^ 1].push_back(T);
2523 }
2524 IdxW ^= 1;
2525 Scale /= 2;
2526 }
2527
2528 // Another sanity check. At this point there should only be two words
2529 // left, and Scale should be 2.
2530 assert(Scale == 2 && Words[IdxW].size() == 2);
2531
2532 SDValue WW = DAG.getNode(HexagonISD::COMBINE, dl, MVT::i64,
2533 Words[IdxW][1], Words[IdxW][0]);
2534 return DAG.getNode(HexagonISD::D2P, dl, VecTy, WW);
2535 }
2536
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002537 return SDValue();
2538}
2539
2540SDValue
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002541HexagonTargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
2542 SelectionDAG &DAG) const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002543 SDValue Vec = Op.getOperand(0);
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002544 MVT ElemTy = ty(Vec).getVectorElementType();
2545 return extractVector(Vec, Op.getOperand(1), SDLoc(Op), ElemTy, ty(Op), DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002546}
2547
2548SDValue
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002549HexagonTargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op,
2550 SelectionDAG &DAG) const {
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00002551 return extractVector(Op.getOperand(0), Op.getOperand(1), SDLoc(Op),
2552 ty(Op), ty(Op), DAG);
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002553}
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002554
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002555SDValue
2556HexagonTargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op,
2557 SelectionDAG &DAG) const {
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002558 return insertVector(Op.getOperand(0), Op.getOperand(1), Op.getOperand(2),
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00002559 SDLoc(Op), ty(Op).getVectorElementType(), DAG);
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002560}
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002561
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002562SDValue
2563HexagonTargetLowering::LowerINSERT_SUBVECTOR(SDValue Op,
2564 SelectionDAG &DAG) const {
2565 SDValue ValV = Op.getOperand(1);
2566 return insertVector(Op.getOperand(0), ValV, Op.getOperand(2),
2567 SDLoc(Op), ty(ValV), DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002568}
2569
Tim Northovera4415852013-08-06 09:12:35 +00002570bool
2571HexagonTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
2572 // Assuming the caller does not have either a signext or zeroext modifier, and
2573 // only one value is accepted, any reasonable truncation is allowed.
2574 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
2575 return false;
2576
2577 // FIXME: in principle up to 64-bit could be made safe, but it would be very
2578 // fragile at the moment: any support for multiple value returns would be
2579 // liable to disallow tail calls involving i64 -> iN truncation in many cases.
2580 return Ty1->getPrimitiveSizeInBits() <= 32;
2581}
2582
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002583SDValue
Krzysztof Parzyszek1df70592018-08-08 17:00:09 +00002584HexagonTargetLowering::LowerLoad(SDValue Op, SelectionDAG &DAG) const {
2585 LoadSDNode *LN = cast<LoadSDNode>(Op.getNode());
2586 unsigned ClaimAlign = LN->getAlignment();
2587 validateConstPtrAlignment(LN->getBasePtr(), SDLoc(Op), ClaimAlign);
2588 // Call LowerUnalignedLoad for all loads, it recognizes loads that
2589 // don't need extra aligning.
2590 return LowerUnalignedLoad(Op, DAG);
2591}
2592
2593SDValue
2594HexagonTargetLowering::LowerStore(SDValue Op, SelectionDAG &DAG) const {
2595 StoreSDNode *SN = cast<StoreSDNode>(Op.getNode());
2596 unsigned ClaimAlign = SN->getAlignment();
2597 SDValue Ptr = SN->getBasePtr();
2598 const SDLoc &dl(Op);
2599 validateConstPtrAlignment(Ptr, dl, ClaimAlign);
2600
2601 MVT StoreTy = SN->getMemoryVT().getSimpleVT();
2602 unsigned NeedAlign = Subtarget.getTypeAlignment(StoreTy);
2603 if (ClaimAlign < NeedAlign)
2604 return expandUnalignedStore(SN, DAG);
2605 return Op;
2606}
2607
2608SDValue
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002609HexagonTargetLowering::LowerUnalignedLoad(SDValue Op, SelectionDAG &DAG)
2610 const {
2611 LoadSDNode *LN = cast<LoadSDNode>(Op.getNode());
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002612 MVT LoadTy = ty(Op);
2613 unsigned NeedAlign = Subtarget.getTypeAlignment(LoadTy);
Krzysztof Parzyszek1df70592018-08-08 17:00:09 +00002614 unsigned HaveAlign = LN->getAlignment();
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002615 if (HaveAlign >= NeedAlign)
2616 return Op;
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002617
2618 const SDLoc &dl(Op);
2619 const DataLayout &DL = DAG.getDataLayout();
2620 LLVMContext &Ctx = *DAG.getContext();
2621 unsigned AS = LN->getAddressSpace();
2622
2623 // If the load aligning is disabled or the load can be broken up into two
2624 // smaller legal loads, do the default (target-independent) expansion.
2625 bool DoDefault = false;
Krzysztof Parzyszek480ab2b2018-03-08 18:15:13 +00002626 // Handle it in the default way if this is an indexed load.
2627 if (!LN->isUnindexed())
2628 DoDefault = true;
2629
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002630 if (!AlignLoads) {
2631 if (allowsMemoryAccess(Ctx, DL, LN->getMemoryVT(), AS, HaveAlign))
2632 return Op;
2633 DoDefault = true;
2634 }
2635 if (!DoDefault && 2*HaveAlign == NeedAlign) {
2636 // The PartTy is the equivalent of "getLoadableTypeOfSize(HaveAlign)".
2637 MVT PartTy = HaveAlign <= 8 ? MVT::getIntegerVT(8*HaveAlign)
2638 : MVT::getVectorVT(MVT::i8, HaveAlign);
2639 DoDefault = allowsMemoryAccess(Ctx, DL, PartTy, AS, HaveAlign);
2640 }
2641 if (DoDefault) {
2642 std::pair<SDValue, SDValue> P = expandUnalignedLoad(LN, DAG);
2643 return DAG.getMergeValues({P.first, P.second}, dl);
2644 }
2645
2646 // The code below generates two loads, both aligned as NeedAlign, and
2647 // with the distance of NeedAlign between them. For that to cover the
2648 // bits that need to be loaded (and without overlapping), the size of
2649 // the loads should be equal to NeedAlign. This is true for all loadable
2650 // types, but add an assertion in case something changes in the future.
2651 assert(LoadTy.getSizeInBits() == 8*NeedAlign);
2652
2653 unsigned LoadLen = NeedAlign;
2654 SDValue Base = LN->getBasePtr();
2655 SDValue Chain = LN->getChain();
2656 auto BO = getBaseAndOffset(Base);
2657 unsigned BaseOpc = BO.first.getOpcode();
2658 if (BaseOpc == HexagonISD::VALIGNADDR && BO.second % LoadLen == 0)
2659 return Op;
2660
2661 if (BO.second % LoadLen != 0) {
2662 BO.first = DAG.getNode(ISD::ADD, dl, MVT::i32, BO.first,
2663 DAG.getConstant(BO.second % LoadLen, dl, MVT::i32));
2664 BO.second -= BO.second % LoadLen;
2665 }
2666 SDValue BaseNoOff = (BaseOpc != HexagonISD::VALIGNADDR)
2667 ? DAG.getNode(HexagonISD::VALIGNADDR, dl, MVT::i32, BO.first,
2668 DAG.getConstant(NeedAlign, dl, MVT::i32))
2669 : BO.first;
2670 SDValue Base0 = DAG.getMemBasePlusOffset(BaseNoOff, BO.second, dl);
2671 SDValue Base1 = DAG.getMemBasePlusOffset(BaseNoOff, BO.second+LoadLen, dl);
2672
2673 MachineMemOperand *WideMMO = nullptr;
2674 if (MachineMemOperand *MMO = LN->getMemOperand()) {
2675 MachineFunction &MF = DAG.getMachineFunction();
2676 WideMMO = MF.getMachineMemOperand(MMO->getPointerInfo(), MMO->getFlags(),
2677 2*LoadLen, LoadLen, MMO->getAAInfo(), MMO->getRanges(),
2678 MMO->getSyncScopeID(), MMO->getOrdering(),
2679 MMO->getFailureOrdering());
2680 }
2681
2682 SDValue Load0 = DAG.getLoad(LoadTy, dl, Chain, Base0, WideMMO);
2683 SDValue Load1 = DAG.getLoad(LoadTy, dl, Chain, Base1, WideMMO);
2684
2685 SDValue Aligned = DAG.getNode(HexagonISD::VALIGN, dl, LoadTy,
2686 {Load1, Load0, BaseNoOff.getOperand(0)});
2687 SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2688 Load0.getValue(1), Load1.getValue(1));
2689 SDValue M = DAG.getMergeValues({Aligned, NewChain}, dl);
2690 return M;
2691}
2692
2693SDValue
Krzysztof Parzyszek0b6187c2018-06-01 14:00:32 +00002694HexagonTargetLowering::LowerAddSubCarry(SDValue Op, SelectionDAG &DAG) const {
2695 const SDLoc &dl(Op);
2696 unsigned Opc = Op.getOpcode();
2697 SDValue X = Op.getOperand(0), Y = Op.getOperand(1), C = Op.getOperand(2);
2698
2699 if (Opc == ISD::ADDCARRY)
2700 return DAG.getNode(HexagonISD::ADDC, dl, Op.getNode()->getVTList(),
2701 { X, Y, C });
2702
2703 EVT CarryTy = C.getValueType();
2704 SDValue SubC = DAG.getNode(HexagonISD::SUBC, dl, Op.getNode()->getVTList(),
2705 { X, Y, DAG.getLogicalNOT(dl, C, CarryTy) });
2706 SDValue Out[] = { SubC.getValue(0),
2707 DAG.getLogicalNOT(dl, SubC.getValue(1), CarryTy) };
2708 return DAG.getMergeValues(Out, dl);
2709}
2710
2711SDValue
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002712HexagonTargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
2713 SDValue Chain = Op.getOperand(0);
2714 SDValue Offset = Op.getOperand(1);
2715 SDValue Handler = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002716 SDLoc dl(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00002717 auto PtrVT = getPointerTy(DAG.getDataLayout());
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002718
2719 // Mark function as containing a call to EH_RETURN.
2720 HexagonMachineFunctionInfo *FuncInfo =
2721 DAG.getMachineFunction().getInfo<HexagonMachineFunctionInfo>();
2722 FuncInfo->setHasEHReturn();
2723
2724 unsigned OffsetReg = Hexagon::R28;
2725
Mehdi Amini44ede332015-07-09 02:09:04 +00002726 SDValue StoreAddr =
2727 DAG.getNode(ISD::ADD, dl, PtrVT, DAG.getRegister(Hexagon::R30, PtrVT),
2728 DAG.getIntPtrConstant(4, dl));
Justin Lebar9c375812016-07-15 18:27:10 +00002729 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo());
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002730 Chain = DAG.getCopyToReg(Chain, dl, OffsetReg, Offset);
2731
2732 // Not needed we already use it as explict input to EH_RETURN.
2733 // MF.getRegInfo().addLiveOut(OffsetReg);
2734
2735 return DAG.getNode(HexagonISD::EH_RETURN, dl, MVT::Other, Chain);
2736}
2737
2738SDValue
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002739HexagonTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002740 unsigned Opc = Op.getOpcode();
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00002741
2742 // Handle INLINEASM first.
2743 if (Opc == ISD::INLINEASM)
2744 return LowerINLINEASM(Op, DAG);
2745
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002746 if (isHvxOperation(Op)) {
2747 // If HVX lowering returns nothing, try the default lowering.
2748 if (SDValue V = LowerHvxOperation(Op, DAG))
2749 return V;
2750 }
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00002751
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002752 switch (Opc) {
2753 default:
2754#ifndef NDEBUG
2755 Op.getNode()->dumpr(&DAG);
2756 if (Opc > HexagonISD::OP_BEGIN && Opc < HexagonISD::OP_END)
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002757 errs() << "Error: check for a non-legal type in this operation\n";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002758#endif
2759 llvm_unreachable("Should not custom lower this!");
2760 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002761 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
2762 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
2763 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
2764 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002765 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
2766 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002767 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Krzysztof Parzyszek1df70592018-08-08 17:00:09 +00002768 case ISD::LOAD: return LowerLoad(Op, DAG);
2769 case ISD::STORE: return LowerStore(Op, DAG);
Krzysztof Parzyszek0b6187c2018-06-01 14:00:32 +00002770 case ISD::ADDCARRY:
2771 case ISD::SUBCARRY: return LowerAddSubCarry(Op, DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002772 case ISD::SRA:
2773 case ISD::SHL:
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002774 case ISD::SRL: return LowerVECTOR_SHIFT(Op, DAG);
Krzysztof Parzyszek3d671242018-06-12 12:49:36 +00002775 case ISD::ROTL: return LowerROTL(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002776 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002777 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002778 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002779 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
2780 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00002781 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002782 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
2783 case ISD::GlobalAddress: return LowerGLOBALADDRESS(Op, DAG);
2784 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002785 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002786 case ISD::VASTART: return LowerVASTART(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002787 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
2788 case ISD::SETCC: return LowerSETCC(Op, DAG);
2789 case ISD::VSELECT: return LowerVSELECT(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002790 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00002791 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00002792 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG);
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +00002793 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Krzysztof Parzyszek9eb085e2018-01-31 20:48:11 +00002794 break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002795 }
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00002796
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002797 return SDValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002798}
2799
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002800void
Krzysztof Parzyszek1df70592018-08-08 17:00:09 +00002801HexagonTargetLowering::LowerOperationWrapper(SDNode *N,
2802 SmallVectorImpl<SDValue> &Results,
2803 SelectionDAG &DAG) const {
2804 // We are only custom-lowering stores to verify the alignment of the
2805 // address if it is a compile-time constant. Since a store can be modified
2806 // during type-legalization (the value being stored may need legalization),
2807 // return empty Results here to indicate that we don't really make any
2808 // changes in the custom lowering.
2809 if (N->getOpcode() != ISD::STORE)
2810 return TargetLowering::LowerOperationWrapper(N, Results, DAG);
2811}
2812
2813void
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002814HexagonTargetLowering::ReplaceNodeResults(SDNode *N,
2815 SmallVectorImpl<SDValue> &Results,
2816 SelectionDAG &DAG) const {
2817 const SDLoc &dl(N);
2818 switch (N->getOpcode()) {
2819 case ISD::SRL:
2820 case ISD::SRA:
2821 case ISD::SHL:
2822 return;
2823 case ISD::BITCAST:
2824 // Handle a bitcast from v8i1 to i8.
2825 if (N->getValueType(0) == MVT::i8) {
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002826 SDValue P = getInstr(Hexagon::C2_tfrpr, dl, MVT::i32,
2827 N->getOperand(0), DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002828 Results.push_back(P);
2829 }
2830 break;
2831 }
2832}
2833
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002834/// Returns relocation base for the given PIC jumptable.
2835SDValue
2836HexagonTargetLowering::getPICJumpTableRelocBase(SDValue Table,
2837 SelectionDAG &DAG) const {
2838 int Idx = cast<JumpTableSDNode>(Table)->getIndex();
2839 EVT VT = Table.getValueType();
2840 SDValue T = DAG.getTargetJumpTable(Idx, VT, HexagonII::MO_PCREL);
2841 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Table), VT, T);
2842}
2843
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002844//===----------------------------------------------------------------------===//
2845// Inline Assembly Support
2846//===----------------------------------------------------------------------===//
2847
Krzysztof Parzyszekca3b5322016-05-18 14:34:51 +00002848TargetLowering::ConstraintType
2849HexagonTargetLowering::getConstraintType(StringRef Constraint) const {
2850 if (Constraint.size() == 1) {
2851 switch (Constraint[0]) {
2852 case 'q':
2853 case 'v':
2854 if (Subtarget.useHVXOps())
Krzysztof Parzyszek3ad0d012017-07-21 17:51:27 +00002855 return C_RegisterClass;
2856 break;
2857 case 'a':
2858 return C_RegisterClass;
2859 default:
Krzysztof Parzyszekca3b5322016-05-18 14:34:51 +00002860 break;
2861 }
2862 }
2863 return TargetLowering::getConstraintType(Constraint);
2864}
2865
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002866std::pair<unsigned, const TargetRegisterClass*>
Eric Christopher11e4df72015-02-26 22:38:43 +00002867HexagonTargetLowering::getRegForInlineAsmConstraint(
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00002868 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002869
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002870 if (Constraint.size() == 1) {
2871 switch (Constraint[0]) {
2872 case 'r': // R0-R31
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002873 switch (VT.SimpleTy) {
2874 default:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002875 return {0u, nullptr};
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002876 case MVT::i1:
2877 case MVT::i8:
2878 case MVT::i16:
2879 case MVT::i32:
2880 case MVT::f32:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002881 return {0u, &Hexagon::IntRegsRegClass};
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002882 case MVT::i64:
2883 case MVT::f64:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002884 return {0u, &Hexagon::DoubleRegsRegClass};
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002885 }
Krzysztof Parzyszek3ad0d012017-07-21 17:51:27 +00002886 break;
2887 case 'a': // M0-M1
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002888 if (VT != MVT::i32)
2889 return {0u, nullptr};
2890 return {0u, &Hexagon::ModRegsRegClass};
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002891 case 'q': // q0-q3
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002892 switch (VT.getSizeInBits()) {
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002893 default:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002894 return {0u, nullptr};
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002895 case 512:
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002896 case 1024:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002897 return {0u, &Hexagon::HvxQRRegClass};
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002898 }
Krzysztof Parzyszek3ad0d012017-07-21 17:51:27 +00002899 break;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002900 case 'v': // V0-V31
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002901 switch (VT.getSizeInBits()) {
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002902 default:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002903 return {0u, nullptr};
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002904 case 512:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002905 return {0u, &Hexagon::HvxVRRegClass};
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002906 case 1024:
Krzysztof Parzyszekd8b780d2018-06-20 13:56:09 +00002907 if (Subtarget.hasV60Ops() && Subtarget.useHVX128BOps())
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002908 return {0u, &Hexagon::HvxVRRegClass};
2909 return {0u, &Hexagon::HvxWRRegClass};
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002910 case 2048:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002911 return {0u, &Hexagon::HvxWRRegClass};
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002912 }
Krzysztof Parzyszek3ad0d012017-07-21 17:51:27 +00002913 break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002914 default:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002915 return {0u, nullptr};
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002916 }
2917 }
2918
Eric Christopher11e4df72015-02-26 22:38:43 +00002919 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002920}
2921
Sirish Pande69295b82012-05-10 20:20:25 +00002922/// isFPImmLegal - Returns true if the target can instruction select the
2923/// specified FP immediate natively. If false, the legalizer will
2924/// materialize the FP immediate as a load from a constant pool.
2925bool HexagonTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Krzysztof Parzyszek6bfc6572018-10-19 17:31:11 +00002926 return true;
Sirish Pande69295b82012-05-10 20:20:25 +00002927}
2928
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002929/// isLegalAddressingMode - Return true if the addressing mode represented by
2930/// AM is legal for this target, for a load/store of the specified type.
Mehdi Amini0cdec1e2015-07-09 02:09:40 +00002931bool HexagonTargetLowering::isLegalAddressingMode(const DataLayout &DL,
2932 const AddrMode &AM, Type *Ty,
Jonas Paulsson024e3192017-07-21 11:59:37 +00002933 unsigned AS, Instruction *I) const {
Krzysztof Parzyszeked4e7822016-08-03 15:06:18 +00002934 if (Ty->isSized()) {
2935 // When LSR detects uses of the same base address to access different
2936 // types (e.g. unions), it will assume a conservative type for these
2937 // uses:
2938 // LSR Use: Kind=Address of void in addrspace(4294967295), ...
2939 // The type Ty passed here would then be "void". Skip the alignment
2940 // checks, but do not return false right away, since that confuses
2941 // LSR into crashing.
2942 unsigned A = DL.getABITypeAlignment(Ty);
2943 // The base offset must be a multiple of the alignment.
2944 if ((AM.BaseOffs % A) != 0)
2945 return false;
2946 // The shifted offset must fit in 11 bits.
2947 if (!isInt<11>(AM.BaseOffs >> Log2_32(A)))
2948 return false;
2949 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002950
2951 // No global is ever allowed as a base.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002952 if (AM.BaseGV)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002953 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002954
2955 int Scale = AM.Scale;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00002956 if (Scale < 0)
2957 Scale = -Scale;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002958 switch (Scale) {
2959 case 0: // No scale reg, "r+i", "r", or just "i".
2960 break;
2961 default: // No scaled addressing mode.
2962 return false;
2963 }
2964 return true;
2965}
2966
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002967/// Return true if folding a constant offset with the given GlobalAddress is
2968/// legal. It is frequently not legal in PIC relocation models.
2969bool HexagonTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA)
2970 const {
2971 return HTM.getRelocationModel() == Reloc::Static;
2972}
2973
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002974/// isLegalICmpImmediate - Return true if the specified immediate is legal
2975/// icmp immediate, that is the target has icmp instructions which can compare
2976/// a register against the immediate without having to materialize the
2977/// immediate into a register.
2978bool HexagonTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
2979 return Imm >= -512 && Imm <= 511;
2980}
2981
2982/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2983/// for tail call optimization. Targets which want to do tail call
2984/// optimization should implement this function.
2985bool HexagonTargetLowering::IsEligibleForTailCallOptimization(
2986 SDValue Callee,
2987 CallingConv::ID CalleeCC,
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00002988 bool IsVarArg,
2989 bool IsCalleeStructRet,
2990 bool IsCallerStructRet,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002991 const SmallVectorImpl<ISD::OutputArg> &Outs,
2992 const SmallVectorImpl<SDValue> &OutVals,
2993 const SmallVectorImpl<ISD::InputArg> &Ins,
2994 SelectionDAG& DAG) const {
Matthias Braunf1caa282017-12-15 22:22:58 +00002995 const Function &CallerF = DAG.getMachineFunction().getFunction();
2996 CallingConv::ID CallerCC = CallerF.getCallingConv();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002997 bool CCMatch = CallerCC == CalleeCC;
2998
2999 // ***************************************************************************
3000 // Look for obvious safe cases to perform tail call optimization that do not
3001 // require ABI changes.
3002 // ***************************************************************************
3003
3004 // If this is a tail call via a function pointer, then don't do it!
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00003005 if (!isa<GlobalAddressSDNode>(Callee) &&
3006 !isa<ExternalSymbolSDNode>(Callee)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003007 return false;
3008 }
3009
Krzysztof Parzyszek0ba97542016-08-19 15:02:18 +00003010 // Do not optimize if the calling conventions do not match and the conventions
3011 // used are not C or Fast.
3012 if (!CCMatch) {
3013 bool R = (CallerCC == CallingConv::C || CallerCC == CallingConv::Fast);
3014 bool E = (CalleeCC == CallingConv::C || CalleeCC == CallingConv::Fast);
3015 // If R & E, then ok.
3016 if (!R || !E)
3017 return false;
3018 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003019
3020 // Do not tail call optimize vararg calls.
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00003021 if (IsVarArg)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003022 return false;
3023
3024 // Also avoid tail call optimization if either caller or callee uses struct
3025 // return semantics.
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00003026 if (IsCalleeStructRet || IsCallerStructRet)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003027 return false;
3028
3029 // In addition to the cases above, we also disable Tail Call Optimization if
3030 // the calling convention code that at least one outgoing argument needs to
3031 // go on the stack. We cannot check that here because at this point that
3032 // information is not available.
3033 return true;
3034}
Colin LeMahieu025f8602014-12-08 21:19:18 +00003035
Krzysztof Parzyszek3e409e12016-08-02 18:34:31 +00003036/// Returns the target specific optimal type for load and store operations as
3037/// a result of memset, memcpy, and memmove lowering.
3038///
3039/// If DstAlign is zero that means it's safe to destination alignment can
3040/// satisfy any constraint. Similarly if SrcAlign is zero it means there isn't
3041/// a need to check it against alignment requirement, probably because the
3042/// source does not need to be loaded. If 'IsMemset' is true, that means it's
3043/// expanding a memset. If 'ZeroMemset' is true, that means it's a memset of
3044/// zero. 'MemcpyStrSrc' indicates whether the memcpy source is constant so it
3045/// does not need to be loaded. It returns EVT::Other if the type should be
3046/// determined using generic target-independent logic.
3047EVT HexagonTargetLowering::getOptimalMemOpType(uint64_t Size,
3048 unsigned DstAlign, unsigned SrcAlign, bool IsMemset, bool ZeroMemset,
3049 bool MemcpyStrSrc, MachineFunction &MF) const {
3050
3051 auto Aligned = [](unsigned GivenA, unsigned MinA) -> bool {
3052 return (GivenA % MinA) == 0;
3053 };
3054
3055 if (Size >= 8 && Aligned(DstAlign, 8) && (IsMemset || Aligned(SrcAlign, 8)))
3056 return MVT::i64;
3057 if (Size >= 4 && Aligned(DstAlign, 4) && (IsMemset || Aligned(SrcAlign, 4)))
3058 return MVT::i32;
3059 if (Size >= 2 && Aligned(DstAlign, 2) && (IsMemset || Aligned(SrcAlign, 2)))
3060 return MVT::i16;
3061
3062 return MVT::Other;
3063}
3064
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +00003065bool HexagonTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
3066 unsigned AS, unsigned Align, bool *Fast) const {
3067 if (Fast)
3068 *Fast = false;
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00003069 return Subtarget.isHVXVectorType(VT.getSimpleVT());
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +00003070}
3071
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003072std::pair<const TargetRegisterClass*, uint8_t>
3073HexagonTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI,
3074 MVT VT) const {
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00003075 if (Subtarget.isHVXVectorType(VT, true)) {
3076 unsigned BitWidth = VT.getSizeInBits();
3077 unsigned VecWidth = Subtarget.getVectorLength() * 8;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003078
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00003079 if (VT.getVectorElementType() == MVT::i1)
3080 return std::make_pair(&Hexagon::HvxQRRegClass, 1);
3081 if (BitWidth == VecWidth)
3082 return std::make_pair(&Hexagon::HvxVRRegClass, 1);
3083 assert(BitWidth == 2 * VecWidth);
3084 return std::make_pair(&Hexagon::HvxWRRegClass, 1);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003085 }
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00003086
3087 return TargetLowering::findRepresentativeClass(TRI, VT);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003088}
3089
Krzysztof Parzyszekf0705442018-11-02 14:17:47 +00003090bool HexagonTargetLowering::shouldReduceLoadWidth(SDNode *Load,
3091 ISD::LoadExtType ExtTy, EVT NewVT) const {
Sanjay Patel0a515592018-11-10 20:05:31 +00003092 // TODO: This may be worth removing. Check regression tests for diffs.
3093 if (!TargetLoweringBase::shouldReduceLoadWidth(Load, ExtTy, NewVT))
3094 return false;
3095
Krzysztof Parzyszekf0705442018-11-02 14:17:47 +00003096 auto *L = cast<LoadSDNode>(Load);
3097 std::pair<SDValue,int> BO = getBaseAndOffset(L->getBasePtr());
3098 // Small-data object, do not shrink.
3099 if (BO.first.getOpcode() == HexagonISD::CONST32_GP)
3100 return false;
3101 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(BO.first)) {
3102 auto &HTM = static_cast<const HexagonTargetMachine&>(getTargetMachine());
3103 const auto *GO = dyn_cast_or_null<const GlobalObject>(GA->getGlobal());
3104 return !GO || !HTM.getObjFileLowering()->isGlobalInSmallSection(GO, HTM);
3105 }
3106 return true;
3107}
3108
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003109Value *HexagonTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
3110 AtomicOrdering Ord) const {
3111 BasicBlock *BB = Builder.GetInsertBlock();
3112 Module *M = BB->getParent()->getParent();
Matt Arsenault39508332019-01-22 18:18:02 +00003113 auto PT = cast<PointerType>(Addr->getType());
3114 Type *Ty = PT->getElementType();
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003115 unsigned SZ = Ty->getPrimitiveSizeInBits();
3116 assert((SZ == 32 || SZ == 64) && "Only 32/64-bit atomic loads supported");
3117 Intrinsic::ID IntID = (SZ == 32) ? Intrinsic::hexagon_L2_loadw_locked
3118 : Intrinsic::hexagon_L4_loadd_locked;
Matt Arsenault39508332019-01-22 18:18:02 +00003119
3120 PointerType *NewPtrTy
3121 = Builder.getIntNTy(SZ)->getPointerTo(PT->getAddressSpace());
3122 Addr = Builder.CreateBitCast(Addr, NewPtrTy);
3123
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003124 Value *Fn = Intrinsic::getDeclaration(M, IntID);
Matt Arsenault39508332019-01-22 18:18:02 +00003125 Value *Call = Builder.CreateCall(Fn, Addr, "larx");
3126
3127 return Builder.CreateBitCast(Call, Ty);
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003128}
3129
3130/// Perform a store-conditional operation to Addr. Return the status of the
3131/// store. This should be 0 if the store succeeded, non-zero otherwise.
3132Value *HexagonTargetLowering::emitStoreConditional(IRBuilder<> &Builder,
3133 Value *Val, Value *Addr, AtomicOrdering Ord) const {
3134 BasicBlock *BB = Builder.GetInsertBlock();
3135 Module *M = BB->getParent()->getParent();
3136 Type *Ty = Val->getType();
3137 unsigned SZ = Ty->getPrimitiveSizeInBits();
Matt Arsenault39508332019-01-22 18:18:02 +00003138
3139 Type *CastTy = Builder.getIntNTy(SZ);
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003140 assert((SZ == 32 || SZ == 64) && "Only 32/64-bit atomic stores supported");
3141 Intrinsic::ID IntID = (SZ == 32) ? Intrinsic::hexagon_S2_storew_locked
3142 : Intrinsic::hexagon_S4_stored_locked;
3143 Value *Fn = Intrinsic::getDeclaration(M, IntID);
Matt Arsenault39508332019-01-22 18:18:02 +00003144
3145 unsigned AS = Addr->getType()->getPointerAddressSpace();
3146 Addr = Builder.CreateBitCast(Addr, CastTy->getPointerTo(AS));
3147 Val = Builder.CreateBitCast(Val, CastTy);
3148
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003149 Value *Call = Builder.CreateCall(Fn, {Addr, Val}, "stcx");
3150 Value *Cmp = Builder.CreateICmpEQ(Call, Builder.getInt32(0), "");
3151 Value *Ext = Builder.CreateZExt(Cmp, Type::getInt32Ty(M->getContext()));
3152 return Ext;
3153}
3154
Ahmed Bougacha52468672015-09-11 17:08:28 +00003155TargetLowering::AtomicExpansionKind
3156HexagonTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const {
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003157 // Do not expand loads and stores that don't exceed 64 bits.
Ahmed Bougacha52468672015-09-11 17:08:28 +00003158 return LI->getType()->getPrimitiveSizeInBits() > 64
Tim Northoverf520eff2015-12-02 18:12:57 +00003159 ? AtomicExpansionKind::LLOnly
Ahmed Bougacha52468672015-09-11 17:08:28 +00003160 : AtomicExpansionKind::None;
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003161}
3162
3163bool HexagonTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const {
3164 // Do not expand loads and stores that don't exceed 64 bits.
3165 return SI->getValueOperand()->getType()->getPrimitiveSizeInBits() > 64;
3166}
Krzysztof Parzyszekf228c952016-06-22 16:07:10 +00003167
Alex Bradbury79518b02018-09-19 14:51:42 +00003168TargetLowering::AtomicExpansionKind
3169HexagonTargetLowering::shouldExpandAtomicCmpXchgInIR(
3170 AtomicCmpXchgInst *AI) const {
Krzysztof Parzyszekf228c952016-06-22 16:07:10 +00003171 const DataLayout &DL = AI->getModule()->getDataLayout();
3172 unsigned Size = DL.getTypeStoreSize(AI->getCompareOperand()->getType());
Alex Bradbury79518b02018-09-19 14:51:42 +00003173 if (Size >= 4 && Size <= 8)
3174 return AtomicExpansionKind::LLSC;
3175 return AtomicExpansionKind::None;
Krzysztof Parzyszekf228c952016-06-22 16:07:10 +00003176}