blob: 4fe6e3a96210f6f3dac2d5190ccc2951b621be9d [file] [log] [blame]
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001//===-- HexagonISelLowering.cpp - Hexagon DAG Lowering Implementation -----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the interfaces that Hexagon uses to lower LLVM code
11// into a selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "HexagonISelLowering.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000016#include "Hexagon.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000017#include "HexagonMachineFunctionInfo.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000018#include "HexagonRegisterInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000019#include "HexagonSubtarget.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "HexagonTargetMachine.h"
21#include "HexagonTargetObjectFile.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000022#include "llvm/ADT/APInt.h"
23#include "llvm/ADT/ArrayRef.h"
24#include "llvm/ADT/SmallVector.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000025#include "llvm/CodeGen/CallingConvLower.h"
26#include "llvm/CodeGen/MachineFrameInfo.h"
27#include "llvm/CodeGen/MachineFunction.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000028#include "llvm/CodeGen/MachineMemOperand.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000029#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000030#include "llvm/CodeGen/RuntimeLibcalls.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000031#include "llvm/CodeGen/SelectionDAG.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000032#include "llvm/CodeGen/TargetCallingConv.h"
Craig Topper2fa14362018-03-29 17:21:10 +000033#include "llvm/CodeGen/ValueTypes.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000034#include "llvm/IR/BasicBlock.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000035#include "llvm/IR/CallingConv.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000036#include "llvm/IR/DataLayout.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000037#include "llvm/IR/DerivedTypes.h"
38#include "llvm/IR/Function.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000039#include "llvm/IR/GlobalValue.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000040#include "llvm/IR/InlineAsm.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000041#include "llvm/IR/Instructions.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000042#include "llvm/IR/Intrinsics.h"
Krzysztof Parzyszekdc7a5572018-03-29 13:52:46 +000043#include "llvm/IR/IntrinsicInst.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000044#include "llvm/IR/Module.h"
45#include "llvm/IR/Type.h"
46#include "llvm/IR/Value.h"
47#include "llvm/MC/MCRegisterInfo.h"
48#include "llvm/Support/Casting.h"
49#include "llvm/Support/CodeGen.h"
NAKAMURA Takumi54eed762012-04-21 15:31:36 +000050#include "llvm/Support/CommandLine.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000051#include "llvm/Support/Debug.h"
52#include "llvm/Support/ErrorHandling.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000053#include "llvm/Support/MathExtras.h"
NAKAMURA Takumie30303f2012-04-21 15:31:45 +000054#include "llvm/Support/raw_ostream.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000055#include "llvm/Target/TargetMachine.h"
56#include <algorithm>
57#include <cassert>
58#include <cstddef>
59#include <cstdint>
60#include <limits>
61#include <utility>
NAKAMURA Takumi54eed762012-04-21 15:31:36 +000062
Craig Topperb25fda92012-03-17 18:46:09 +000063using namespace llvm;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000064
Chandler Carruthe96dd892014-04-21 22:55:11 +000065#define DEBUG_TYPE "hexagon-lowering"
66
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +000067static cl::opt<bool> EmitJumpTables("hexagon-emit-jump-tables",
68 cl::init(true), cl::Hidden,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +000069 cl::desc("Control jump table emission on Hexagon target"));
70
71static cl::opt<bool> EnableHexSDNodeSched("enable-hexagon-sdnode-sched",
72 cl::Hidden, cl::ZeroOrMore, cl::init(false),
73 cl::desc("Enable Hexagon SDNode scheduling"));
74
75static cl::opt<bool> EnableFastMath("ffast-math",
76 cl::Hidden, cl::ZeroOrMore, cl::init(false),
77 cl::desc("Enable Fast Math processing"));
78
79static cl::opt<int> MinimumJumpTables("minimum-jump-tables",
80 cl::Hidden, cl::ZeroOrMore, cl::init(5),
81 cl::desc("Set minimum jump tables"));
82
83static cl::opt<int> MaxStoresPerMemcpyCL("max-store-memcpy",
84 cl::Hidden, cl::ZeroOrMore, cl::init(6),
85 cl::desc("Max #stores to inline memcpy"));
86
87static cl::opt<int> MaxStoresPerMemcpyOptSizeCL("max-store-memcpy-Os",
88 cl::Hidden, cl::ZeroOrMore, cl::init(4),
89 cl::desc("Max #stores to inline memcpy"));
90
91static cl::opt<int> MaxStoresPerMemmoveCL("max-store-memmove",
92 cl::Hidden, cl::ZeroOrMore, cl::init(6),
93 cl::desc("Max #stores to inline memmove"));
94
95static cl::opt<int> MaxStoresPerMemmoveOptSizeCL("max-store-memmove-Os",
96 cl::Hidden, cl::ZeroOrMore, cl::init(4),
97 cl::desc("Max #stores to inline memmove"));
98
99static cl::opt<int> MaxStoresPerMemsetCL("max-store-memset",
100 cl::Hidden, cl::ZeroOrMore, cl::init(8),
101 cl::desc("Max #stores to inline memset"));
102
103static cl::opt<int> MaxStoresPerMemsetOptSizeCL("max-store-memset-Os",
104 cl::Hidden, cl::ZeroOrMore, cl::init(4),
105 cl::desc("Max #stores to inline memset"));
106
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +0000107static cl::opt<bool> AlignLoads("hexagon-align-loads",
108 cl::Hidden, cl::init(false),
109 cl::desc("Rewrite unaligned loads as a pair of aligned loads"));
110
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000111
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000112namespace {
Eugene Zelenko58655bb2016-12-17 01:09:05 +0000113
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +0000114 class HexagonCCState : public CCState {
Krzysztof Parzyszek18e0d2a2018-02-15 15:47:53 +0000115 unsigned NumNamedVarArgParams = 0;
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000116
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +0000117 public:
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000118 HexagonCCState(CallingConv::ID CC, bool IsVarArg, MachineFunction &MF,
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +0000119 SmallVectorImpl<CCValAssign> &locs, LLVMContext &C,
Krzysztof Parzyszeke0d7de72018-02-15 17:20:07 +0000120 unsigned NumNamedArgs)
121 : CCState(CC, IsVarArg, MF, locs, C),
122 NumNamedVarArgParams(NumNamedArgs) {}
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +0000123 unsigned getNumNamedVarArgParams() const { return NumNamedVarArgParams; }
124 };
125
Eugene Zelenko58655bb2016-12-17 01:09:05 +0000126} // end anonymous namespace
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000127
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000128
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000129// Implement calling convention for Hexagon.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000130
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000131static bool CC_SkipOdd(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
132 CCValAssign::LocInfo &LocInfo,
133 ISD::ArgFlagsTy &ArgFlags, CCState &State) {
134 static const MCPhysReg ArgRegs[] = {
135 Hexagon::R0, Hexagon::R1, Hexagon::R2,
136 Hexagon::R3, Hexagon::R4, Hexagon::R5
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000137 };
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000138 const unsigned NumArgRegs = array_lengthof(ArgRegs);
139 unsigned RegNum = State.getFirstUnallocated(ArgRegs);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000140
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000141 // RegNum is an index into ArgRegs: skip a register if RegNum is odd.
142 if (RegNum != NumArgRegs && RegNum % 2 == 1)
143 State.AllocateReg(ArgRegs[RegNum]);
144
145 // Always return false here, as this function only makes sure that the first
146 // unallocated register has an even register number and does not actually
147 // allocate a register for the current argument.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000148 return false;
149}
150
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000151#include "HexagonGenCallingConv.inc"
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000152
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000153
Craig Topper18e69f42016-04-15 06:20:21 +0000154void HexagonTargetLowering::promoteLdStType(MVT VT, MVT PromotedLdStVT) {
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000155 if (VT != PromotedLdStVT) {
Craig Topper18e69f42016-04-15 06:20:21 +0000156 setOperationAction(ISD::LOAD, VT, Promote);
157 AddPromotedToType(ISD::LOAD, VT, PromotedLdStVT);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000158
Craig Topper18e69f42016-04-15 06:20:21 +0000159 setOperationAction(ISD::STORE, VT, Promote);
160 AddPromotedToType(ISD::STORE, VT, PromotedLdStVT);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000161 }
162}
163
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000164SDValue
165HexagonTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG)
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000166 const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000167 return SDValue();
168}
169
170/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
171/// by "Src" to address "Dst" of size "Size". Alignment information is
172/// specified by the specific parameter attribute. The copy will be passed as
173/// a byval function parameter. Sometimes what we are copying is the end of a
174/// larger object, the part that does not fit in registers.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000175static SDValue CreateCopyOfByValArgument(SDValue Src, SDValue Dst,
176 SDValue Chain, ISD::ArgFlagsTy Flags,
177 SelectionDAG &DAG, const SDLoc &dl) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000178 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000179 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
180 /*isVolatile=*/false, /*AlwaysInline=*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +0000181 /*isTailCall=*/false,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000182 MachinePointerInfo(), MachinePointerInfo());
183}
184
Krzysztof Parzyszek56199522017-04-13 15:05:51 +0000185bool
186HexagonTargetLowering::CanLowerReturn(
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000187 CallingConv::ID CallConv, MachineFunction &MF, bool IsVarArg,
Krzysztof Parzyszek56199522017-04-13 15:05:51 +0000188 const SmallVectorImpl<ISD::OutputArg> &Outs,
189 LLVMContext &Context) const {
190 SmallVector<CCValAssign, 16> RVLocs;
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000191 CCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
192
193 if (MF.getSubtarget<HexagonSubtarget>().useHVXOps())
194 return CCInfo.CheckReturn(Outs, RetCC_Hexagon_HVX);
Krzysztof Parzyszek56199522017-04-13 15:05:51 +0000195 return CCInfo.CheckReturn(Outs, RetCC_Hexagon);
196}
197
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000198// LowerReturn - Lower ISD::RET. If a struct is larger than 8 bytes and is
199// passed by value, the function prototype is modified to return void and
200// the value is stored in memory pointed by a pointer passed by caller.
201SDValue
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000202HexagonTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000203 bool IsVarArg,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000204 const SmallVectorImpl<ISD::OutputArg> &Outs,
205 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000206 const SDLoc &dl, SelectionDAG &DAG) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000207 // CCValAssign - represent the assignment of the return value to locations.
208 SmallVector<CCValAssign, 16> RVLocs;
209
210 // CCState - Info about the registers and stack slot.
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000211 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
Eric Christopherb5217502014-08-06 18:45:26 +0000212 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000213
214 // Analyze return values of ISD::RET
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000215 if (Subtarget.useHVXOps())
216 CCInfo.AnalyzeReturn(Outs, RetCC_Hexagon_HVX);
217 else
218 CCInfo.AnalyzeReturn(Outs, RetCC_Hexagon);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000219
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000220 SDValue Flag;
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000221 SmallVector<SDValue, 4> RetOps(1, Chain);
222
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000223 // Copy the result values into the output registers.
224 for (unsigned i = 0; i != RVLocs.size(); ++i) {
225 CCValAssign &VA = RVLocs[i];
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000226
227 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), OutVals[i], Flag);
228
229 // Guarantee that all emitted copies are stuck together with flags.
230 Flag = Chain.getValue(1);
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000231 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000232 }
233
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000234 RetOps[0] = Chain; // Update chain.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000235
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000236 // Add the flag if we have it.
237 if (Flag.getNode())
238 RetOps.push_back(Flag);
239
Craig Topper48d114b2014-04-26 18:35:24 +0000240 return DAG.getNode(HexagonISD::RET_FLAG, dl, MVT::Other, RetOps);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000241}
242
Matt Arsenault31380752017-04-18 21:16:46 +0000243bool HexagonTargetLowering::mayBeEmittedAsTailCall(const CallInst *CI) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000244 // If either no tail call or told not to tail call at all, don't.
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000245 auto Attr =
246 CI->getParent()->getParent()->getFnAttribute("disable-tail-calls");
247 if (!CI->isTailCall() || Attr.getValueAsString() == "true")
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000248 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000249
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000250 return true;
251}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000252
253/// LowerCallResult - Lower the result values of an ISD::CALL into the
254/// appropriate copies out of appropriate physical registers. This assumes that
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000255/// Chain/Glue are the input chain/glue to use, and that TheCall is the call
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000256/// being lowered. Returns a SDNode with the same number of values as the
257/// ISD::CALL.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000258SDValue HexagonTargetLowering::LowerCallResult(
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000259 SDValue Chain, SDValue Glue, CallingConv::ID CallConv, bool IsVarArg,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000260 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
261 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals,
262 const SmallVectorImpl<SDValue> &OutVals, SDValue Callee) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000263 // Assign locations to each value returned by this call.
264 SmallVector<CCValAssign, 16> RVLocs;
265
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000266 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
Eric Christopherb5217502014-08-06 18:45:26 +0000267 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000268
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000269 if (Subtarget.useHVXOps())
270 CCInfo.AnalyzeCallResult(Ins, RetCC_Hexagon_HVX);
271 else
272 CCInfo.AnalyzeCallResult(Ins, RetCC_Hexagon);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000273
274 // Copy all of the result registers out of their specified physreg.
275 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000276 SDValue RetVal;
277 if (RVLocs[i].getValVT() == MVT::i1) {
278 // Return values of type MVT::i1 require special handling. The reason
279 // is that MVT::i1 is associated with the PredRegs register class, but
280 // values of that type are still returned in R0. Generate an explicit
281 // copy into a predicate register from R0, and treat the value of the
282 // predicate register as the call result.
283 auto &MRI = DAG.getMachineFunction().getRegInfo();
284 SDValue FR0 = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000285 MVT::i32, Glue);
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000286 // FR0 = (Value, Chain, Glue)
287 unsigned PredR = MRI.createVirtualRegister(&Hexagon::PredRegsRegClass);
288 SDValue TPR = DAG.getCopyToReg(FR0.getValue(1), dl, PredR,
289 FR0.getValue(0), FR0.getValue(2));
290 // TPR = (Chain, Glue)
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000291 // Don't glue this CopyFromReg, because it copies from a virtual
292 // register. If it is glued to the call, InstrEmitter will add it
293 // as an implicit def to the call (EmitMachineNode).
294 RetVal = DAG.getCopyFromReg(TPR.getValue(0), dl, PredR, MVT::i1);
295 Glue = TPR.getValue(1);
Krzysztof Parzyszek6f06b6e2017-10-23 19:35:25 +0000296 Chain = TPR.getValue(0);
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000297 } else {
298 RetVal = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000299 RVLocs[i].getValVT(), Glue);
300 Glue = RetVal.getValue(2);
Krzysztof Parzyszek6f06b6e2017-10-23 19:35:25 +0000301 Chain = RetVal.getValue(1);
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000302 }
303 InVals.push_back(RetVal.getValue(0));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000304 }
305
306 return Chain;
307}
308
309/// LowerCall - Functions arguments are copied from virtual regs to
310/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
311SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +0000312HexagonTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000313 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +0000314 SelectionDAG &DAG = CLI.DAG;
Craig Topperb94011f2013-07-14 04:42:23 +0000315 SDLoc &dl = CLI.DL;
316 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
317 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
318 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000319 SDValue Chain = CLI.Chain;
320 SDValue Callee = CLI.Callee;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000321 CallingConv::ID CallConv = CLI.CallConv;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000322 bool IsVarArg = CLI.IsVarArg;
323 bool DoesNotReturn = CLI.DoesNotReturn;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000324
Krzysztof Parzyszeke0d7de72018-02-15 17:20:07 +0000325 bool IsStructRet = Outs.empty() ? false : Outs[0].Flags.isSRet();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000326 MachineFunction &MF = DAG.getMachineFunction();
Krzysztof Parzyszekf67cd822017-07-11 17:11:54 +0000327 MachineFrameInfo &MFI = MF.getFrameInfo();
Mehdi Amini44ede332015-07-09 02:09:04 +0000328 auto PtrVT = getPointerTy(MF.getDataLayout());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000329
Krzysztof Parzyszeke0d7de72018-02-15 17:20:07 +0000330 unsigned NumParams = CLI.CS.getInstruction()
331 ? CLI.CS.getFunctionType()->getNumParams()
332 : 0;
333 if (GlobalAddressSDNode *GAN = dyn_cast<GlobalAddressSDNode>(Callee))
334 Callee = DAG.getTargetGlobalAddress(GAN->getGlobal(), dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000335
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000336 // Analyze operands of the call, assigning locations to each operand.
337 SmallVector<CCValAssign, 16> ArgLocs;
Krzysztof Parzyszek18e0d2a2018-02-15 15:47:53 +0000338 HexagonCCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext(),
Krzysztof Parzyszeke0d7de72018-02-15 17:20:07 +0000339 NumParams);
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000340
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000341 if (Subtarget.useHVXOps())
342 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon_HVX);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000343 else
344 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon);
345
Matthias Braunf1caa282017-12-15 22:22:58 +0000346 auto Attr = MF.getFunction().getFnAttribute("disable-tail-calls");
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000347 if (Attr.getValueAsString() == "true")
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000348 CLI.IsTailCall = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000349
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000350 if (CLI.IsTailCall) {
Matthias Braunf1caa282017-12-15 22:22:58 +0000351 bool StructAttrFlag = MF.getFunction().hasStructRetAttr();
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000352 CLI.IsTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
353 IsVarArg, IsStructRet, StructAttrFlag, Outs,
354 OutVals, Ins, DAG);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000355 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000356 CCValAssign &VA = ArgLocs[i];
357 if (VA.isMemLoc()) {
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000358 CLI.IsTailCall = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000359 break;
360 }
361 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000362 LLVM_DEBUG(dbgs() << (CLI.IsTailCall ? "Eligible for Tail Call\n"
363 : "Argument must be passed on stack. "
364 "Not eligible for Tail Call\n"));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000365 }
366 // Get a count of how many bytes are to be pushed on the stack.
367 unsigned NumBytes = CCInfo.getNextStackOffset();
368 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
369 SmallVector<SDValue, 8> MemOpChains;
370
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000371 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Mehdi Amini44ede332015-07-09 02:09:04 +0000372 SDValue StackPtr =
373 DAG.getCopyFromReg(Chain, dl, HRI.getStackRegister(), PtrVT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000374
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000375 bool NeedsArgAlign = false;
376 unsigned LargestAlignSeen = 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000377 // Walk the register/memloc assignments, inserting copies/loads.
378 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
379 CCValAssign &VA = ArgLocs[i];
380 SDValue Arg = OutVals[i];
381 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000382 // Record if we need > 8 byte alignment on an argument.
Krzysztof Parzyszekac1966e2017-11-27 18:12:16 +0000383 bool ArgAlign = Subtarget.isHVXVectorType(VA.getValVT());
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000384 NeedsArgAlign |= ArgAlign;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000385
386 // Promote the value if needed.
387 switch (VA.getLocInfo()) {
388 default:
Krzysztof Parzyszek8f6b0c82017-12-20 14:44:05 +0000389 // Loc info must be one of Full, BCvt, SExt, ZExt, or AExt.
Craig Toppere55c5562012-02-07 02:50:20 +0000390 llvm_unreachable("Unknown loc info!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000391 case CCValAssign::Full:
392 break;
Krzysztof Parzyszek8f6b0c82017-12-20 14:44:05 +0000393 case CCValAssign::BCvt:
394 Arg = DAG.getBitcast(VA.getLocVT(), Arg);
395 break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000396 case CCValAssign::SExt:
397 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
398 break;
399 case CCValAssign::ZExt:
400 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
401 break;
402 case CCValAssign::AExt:
403 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
404 break;
405 }
406
407 if (VA.isMemLoc()) {
408 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000409 SDValue MemAddr = DAG.getConstant(LocMemOffset, dl,
410 StackPtr.getValueType());
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000411 MemAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, MemAddr);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000412 if (ArgAlign)
413 LargestAlignSeen = std::max(LargestAlignSeen,
414 VA.getLocVT().getStoreSizeInBits() >> 3);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000415 if (Flags.isByVal()) {
416 // The argument is a struct passed by value. According to LLVM, "Arg"
Fangrui Song956ee792018-03-30 22:22:31 +0000417 // is a pointer.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000418 MemOpChains.push_back(CreateCopyOfByValArgument(Arg, MemAddr, Chain,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000419 Flags, DAG, dl));
420 } else {
Alex Lorenze40c8a22015-08-11 23:09:45 +0000421 MachinePointerInfo LocPI = MachinePointerInfo::getStack(
422 DAG.getMachineFunction(), LocMemOffset);
Justin Lebar9c375812016-07-15 18:27:10 +0000423 SDValue S = DAG.getStore(Chain, dl, Arg, MemAddr, LocPI);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000424 MemOpChains.push_back(S);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000425 }
426 continue;
427 }
428
429 // Arguments that can be passed on register must be kept at RegsToPass
430 // vector.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000431 if (VA.isRegLoc())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000432 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000433 }
434
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000435 if (NeedsArgAlign && Subtarget.hasV60TOps()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000436 LLVM_DEBUG(dbgs() << "Function needs byte stack align due to call args\n");
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000437 unsigned VecAlign = HRI.getSpillAlignment(Hexagon::HvxVRRegClass);
438 LargestAlignSeen = std::max(LargestAlignSeen, VecAlign);
Matthias Braun941a7052016-07-28 18:40:00 +0000439 MFI.ensureMaxAlignment(LargestAlignSeen);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000440 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000441 // Transform all store nodes into one single node because all store
442 // nodes are independent of each other.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000443 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +0000444 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000445
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000446 SDValue Glue;
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000447 if (!CLI.IsTailCall) {
Serge Pavlovd526b132017-05-09 13:35:13 +0000448 Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, dl);
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000449 Glue = Chain.getValue(1);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000450 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000451
452 // Build a sequence of copy-to-reg nodes chained together with token
453 // chain and flag operands which copy the outgoing args into registers.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000454 // The Glue is necessary since all emitted instructions must be
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000455 // stuck together.
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000456 if (!CLI.IsTailCall) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000457 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
458 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000459 RegsToPass[i].second, Glue);
460 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000461 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000462 } else {
463 // For tail calls lower the arguments to the 'real' stack slot.
464 //
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000465 // Force all the incoming stack arguments to be loaded from the stack
466 // before any new outgoing arguments are stored to the stack, because the
467 // outgoing stack slots may alias the incoming argument stack slots, and
468 // the alias isn't otherwise explicit. This is slightly more conservative
469 // than necessary, because it means that each store effectively depends
470 // on every argument instead of just those arguments it would clobber.
471 //
Benjamin Kramerbde91762012-06-02 10:20:22 +0000472 // Do not flag preceding copytoreg stuff together with the following stuff.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000473 Glue = SDValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000474 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
475 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000476 RegsToPass[i].second, Glue);
477 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000478 }
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000479 Glue = SDValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000480 }
481
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000482 bool LongCalls = MF.getSubtarget<HexagonSubtarget>().useLongCalls();
483 unsigned Flags = LongCalls ? HexagonII::HMOTF_ConstExtended : 0;
484
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000485 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
486 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
487 // node so that legalize doesn't hack it.
Tobias Edler von Kochb51460c2015-12-16 17:29:37 +0000488 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000489 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, PtrVT, 0, Flags);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000490 } else if (ExternalSymbolSDNode *S =
491 dyn_cast<ExternalSymbolSDNode>(Callee)) {
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000492 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), PtrVT, Flags);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000493 }
494
495 // Returns a chain & a flag for retval copy to use.
496 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
497 SmallVector<SDValue, 8> Ops;
498 Ops.push_back(Chain);
499 Ops.push_back(Callee);
500
501 // Add argument registers to the end of the list so that they are
502 // known live into the call.
503 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
504 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
505 RegsToPass[i].second.getValueType()));
506 }
507
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000508 const uint32_t *Mask = HRI.getCallPreservedMask(MF, CallConv);
509 assert(Mask && "Missing call preserved mask for calling convention");
510 Ops.push_back(DAG.getRegisterMask(Mask));
511
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000512 if (Glue.getNode())
513 Ops.push_back(Glue);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000514
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000515 if (CLI.IsTailCall) {
Krzysztof Parzyszekf67cd822017-07-11 17:11:54 +0000516 MFI.setHasTailCall();
Craig Topper48d114b2014-04-26 18:35:24 +0000517 return DAG.getNode(HexagonISD::TC_RETURN, dl, NodeTys, Ops);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +0000518 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000519
Krzysztof Parzyszekf67cd822017-07-11 17:11:54 +0000520 // Set this here because we need to know this for "hasFP" in frame lowering.
521 // The target-independent code calls getFrameRegister before setting it, and
522 // getFrameRegister uses hasFP to determine whether the function has FP.
523 MFI.setHasCalls(true);
524
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +0000525 unsigned OpCode = DoesNotReturn ? HexagonISD::CALLnr : HexagonISD::CALL;
Colin LeMahieu2e3a26d2015-01-16 17:05:27 +0000526 Chain = DAG.getNode(OpCode, dl, NodeTys, Ops);
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000527 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000528
529 // Create the CALLSEQ_END node.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000530 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000531 DAG.getIntPtrConstant(0, dl, true), Glue, dl);
532 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000533
534 // Handle result values, copying them out of physregs into vregs that we
535 // return.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000536 return LowerCallResult(Chain, Glue, CallConv, IsVarArg, Ins, dl, DAG,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000537 InVals, OutVals, Callee);
538}
539
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +0000540/// Returns true by value, base pointer and offset pointer and addressing
541/// mode by reference if this node can be combined with a load / store to
542/// form a post-indexed load / store.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000543bool HexagonTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +0000544 SDValue &Base, SDValue &Offset, ISD::MemIndexedMode &AM,
545 SelectionDAG &DAG) const {
546 LSBaseSDNode *LSN = dyn_cast<LSBaseSDNode>(N);
547 if (!LSN)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000548 return false;
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +0000549 EVT VT = LSN->getMemoryVT();
550 if (!VT.isSimple())
551 return false;
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +0000552 bool IsLegalType = VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32 ||
Brendon Cahoone5ed5632018-05-18 18:14:44 +0000553 VT == MVT::i64 || VT == MVT::f32 || VT == MVT::f64 ||
554 VT == MVT::v2i16 || VT == MVT::v2i32 || VT == MVT::v4i8 ||
555 VT == MVT::v4i16 || VT == MVT::v8i8 ||
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +0000556 Subtarget.isHVXVectorType(VT.getSimpleVT());
557 if (!IsLegalType)
558 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000559
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +0000560 if (Op->getOpcode() != ISD::ADD)
561 return false;
562 Base = Op->getOperand(0);
563 Offset = Op->getOperand(1);
564 if (!isa<ConstantSDNode>(Offset.getNode()))
565 return false;
566 AM = ISD::POST_INC;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000567
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +0000568 int32_t V = cast<ConstantSDNode>(Offset.getNode())->getSExtValue();
569 return Subtarget.getInstrInfo()->isValidAutoIncImm(VT, V);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000570}
571
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000572SDValue
573HexagonTargetLowering::LowerINLINEASM(SDValue Op, SelectionDAG &DAG) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000574 MachineFunction &MF = DAG.getMachineFunction();
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000575 auto &HMFI = *MF.getInfo<HexagonMachineFunctionInfo>();
576 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
577 unsigned LR = HRI.getRARegister();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000578
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000579 if (Op.getOpcode() != ISD::INLINEASM || HMFI.hasClobberLR())
580 return Op;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000581
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000582 unsigned NumOps = Op.getNumOperands();
583 if (Op.getOperand(NumOps-1).getValueType() == MVT::Glue)
584 --NumOps; // Ignore the flag operand.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000585
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000586 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
587 unsigned Flags = cast<ConstantSDNode>(Op.getOperand(i))->getZExtValue();
588 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
589 ++i; // Skip the ID value.
590
591 switch (InlineAsm::getKind(Flags)) {
592 default:
593 llvm_unreachable("Bad flags!");
594 case InlineAsm::Kind_RegUse:
595 case InlineAsm::Kind_Imm:
596 case InlineAsm::Kind_Mem:
597 i += NumVals;
598 break;
599 case InlineAsm::Kind_Clobber:
600 case InlineAsm::Kind_RegDef:
601 case InlineAsm::Kind_RegDefEarlyClobber: {
602 for (; NumVals; --NumVals, ++i) {
603 unsigned Reg = cast<RegisterSDNode>(Op.getOperand(i))->getReg();
604 if (Reg != LR)
605 continue;
606 HMFI.setHasClobberLR(true);
607 return Op;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000608 }
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000609 break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000610 }
611 }
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000612 }
613
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000614 return Op;
615}
616
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +0000617// Need to transform ISD::PREFETCH into something that doesn't inherit
618// all of the properties of ISD::PREFETCH, specifically SDNPMayLoad and
619// SDNPMayStore.
620SDValue HexagonTargetLowering::LowerPREFETCH(SDValue Op,
621 SelectionDAG &DAG) const {
622 SDValue Chain = Op.getOperand(0);
623 SDValue Addr = Op.getOperand(1);
624 // Lower it to DCFETCH($reg, #0). A "pat" will try to merge the offset in,
625 // if the "reg" is fed by an "add".
626 SDLoc DL(Op);
627 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
628 return DAG.getNode(HexagonISD::DCFETCH, DL, MVT::Other, Chain, Addr, Zero);
629}
630
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +0000631// Custom-handle ISD::READCYCLECOUNTER because the target-independent SDNode
632// is marked as having side-effects, while the register read on Hexagon does
633// not have any. TableGen refuses to accept the direct pattern from that node
634// to the A4_tfrcpp.
635SDValue HexagonTargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
636 SelectionDAG &DAG) const {
637 SDValue Chain = Op.getOperand(0);
638 SDLoc dl(Op);
639 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other);
640 return DAG.getNode(HexagonISD::READCYCLE, dl, VTs, Chain);
641}
642
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +0000643SDValue HexagonTargetLowering::LowerINTRINSIC_VOID(SDValue Op,
644 SelectionDAG &DAG) const {
645 SDValue Chain = Op.getOperand(0);
646 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
647 // Lower the hexagon_prefetch builtin to DCFETCH, as above.
648 if (IntNo == Intrinsic::hexagon_prefetch) {
649 SDValue Addr = Op.getOperand(2);
650 SDLoc DL(Op);
651 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
652 return DAG.getNode(HexagonISD::DCFETCH, DL, MVT::Other, Chain, Addr, Zero);
653 }
654 return SDValue();
655}
656
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000657SDValue
658HexagonTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
659 SelectionDAG &DAG) const {
660 SDValue Chain = Op.getOperand(0);
661 SDValue Size = Op.getOperand(1);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000662 SDValue Align = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000663 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000664
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000665 ConstantSDNode *AlignConst = dyn_cast<ConstantSDNode>(Align);
666 assert(AlignConst && "Non-constant Align in LowerDYNAMIC_STACKALLOC");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000667
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000668 unsigned A = AlignConst->getSExtValue();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000669 auto &HFI = *Subtarget.getFrameLowering();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000670 // "Zero" means natural stack alignment.
671 if (A == 0)
672 A = HFI.getStackAlignment();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000673
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000674 LLVM_DEBUG({
Reid Kleckner40d72302016-10-20 00:22:23 +0000675 dbgs () << __func__ << " Align: " << A << " Size: ";
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000676 Size.getNode()->dump(&DAG);
677 dbgs() << "\n";
678 });
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000679
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000680 SDValue AC = DAG.getConstant(A, dl, MVT::i32);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000681 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other);
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +0000682 SDValue AA = DAG.getNode(HexagonISD::ALLOCA, dl, VTs, Chain, Size, AC);
Nirav Davebfdb4832016-06-23 17:52:57 +0000683
684 DAG.ReplaceAllUsesOfValueWith(Op, AA);
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +0000685 return AA;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000686}
687
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000688SDValue HexagonTargetLowering::LowerFormalArguments(
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000689 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000690 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
691 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000692 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +0000693 MachineFrameInfo &MFI = MF.getFrameInfo();
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000694 MachineRegisterInfo &MRI = MF.getRegInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000695
696 // Assign locations to all of the incoming arguments.
697 SmallVector<CCValAssign, 16> ArgLocs;
Krzysztof Parzyszek18e0d2a2018-02-15 15:47:53 +0000698 HexagonCCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext(),
Krzysztof Parzyszeke0d7de72018-02-15 17:20:07 +0000699 MF.getFunction().getFunctionType()->getNumParams());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000700
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000701 if (Subtarget.useHVXOps())
702 CCInfo.AnalyzeFormalArguments(Ins, CC_Hexagon_HVX);
703 else
704 CCInfo.AnalyzeFormalArguments(Ins, CC_Hexagon);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000705
706 // For LLVM, in the case when returning a struct by value (>8byte),
707 // the first argument is a pointer that points to the location on caller's
708 // stack where the return value will be stored. For Hexagon, the location on
709 // caller's stack is passed only when the struct size is smaller than (and
710 // equal to) 8 bytes. If not, no address will be passed into callee and
711 // callee return the result direclty through R0/R1.
712
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000713 auto &HMFI = *MF.getInfo<HexagonMachineFunctionInfo>();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000714
715 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
716 CCValAssign &VA = ArgLocs[i];
717 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000718 bool ByVal = Flags.isByVal();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000719
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000720 // Arguments passed in registers:
721 // 1. 32- and 64-bit values and HVX vectors are passed directly,
722 // 2. Large structs are passed via an address, and the address is
723 // passed in a register.
724 if (VA.isRegLoc() && ByVal && Flags.getByValSize() <= 8)
725 llvm_unreachable("ByValSize must be bigger than 8 bytes");
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000726
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000727 bool InReg = VA.isRegLoc() &&
728 (!ByVal || (ByVal && Flags.getByValSize() > 8));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000729
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000730 if (InReg) {
731 MVT RegVT = VA.getLocVT();
732 if (VA.getLocInfo() == CCValAssign::BCvt)
733 RegVT = VA.getValVT();
734
735 const TargetRegisterClass *RC = getRegClassFor(RegVT);
736 unsigned VReg = MRI.createVirtualRegister(RC);
737 SDValue Copy = DAG.getCopyFromReg(Chain, dl, VReg, RegVT);
738
739 // Treat values of type MVT::i1 specially: they are passed in
740 // registers of type i32, but they need to remain as values of
741 // type i1 for consistency of the argument lowering.
742 if (VA.getValVT() == MVT::i1) {
743 assert(RegVT.getSizeInBits() <= 32);
744 SDValue T = DAG.getNode(ISD::AND, dl, RegVT,
745 Copy, DAG.getConstant(1, dl, RegVT));
746 Copy = DAG.getSetCC(dl, MVT::i1, T, DAG.getConstant(0, dl, RegVT),
747 ISD::SETNE);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000748 } else {
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000749#ifndef NDEBUG
750 unsigned RegSize = RegVT.getSizeInBits();
751 assert(RegSize == 32 || RegSize == 64 ||
752 Subtarget.isHVXVectorType(RegVT));
753#endif
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000754 }
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000755 InVals.push_back(Copy);
756 MRI.addLiveIn(VA.getLocReg(), VReg);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000757 } else {
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000758 assert(VA.isMemLoc() && "Argument should be passed in memory");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000759
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000760 // If it's a byval parameter, then we need to compute the
761 // "real" size, not the size of the pointer.
762 unsigned ObjSize = Flags.isByVal()
763 ? Flags.getByValSize()
764 : VA.getLocVT().getStoreSizeInBits() / 8;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000765
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000766 // Create the frame index object for this incoming parameter.
767 int Offset = HEXAGON_LRFP_SIZE + VA.getLocMemOffset();
768 int FI = MFI.CreateFixedObject(ObjSize, Offset, true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000769 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
770
771 if (Flags.isByVal()) {
772 // If it's a pass-by-value aggregate, then do not dereference the stack
773 // location. Instead, we should generate a reference to the stack
774 // location.
775 InVals.push_back(FIN);
776 } else {
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000777 SDValue L = DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
778 MachinePointerInfo::getFixedStack(MF, FI, 0));
779 InVals.push_back(L);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000780 }
781 }
782 }
783
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000784
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000785 if (IsVarArg) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000786 // This will point to the next argument passed via stack.
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000787 int Offset = HEXAGON_LRFP_SIZE + CCInfo.getNextStackOffset();
788 int FI = MFI.CreateFixedObject(Hexagon_PointerSize, Offset, true);
789 HMFI.setVarArgsFrameIndex(FI);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000790 }
791
792 return Chain;
793}
794
795SDValue
796HexagonTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
797 // VASTART stores the address of the VarArgsFrameIndex slot into the
798 // memory location argument.
799 MachineFunction &MF = DAG.getMachineFunction();
800 HexagonMachineFunctionInfo *QFI = MF.getInfo<HexagonMachineFunctionInfo>();
801 SDValue Addr = DAG.getFrameIndex(QFI->getVarArgsFrameIndex(), MVT::i32);
802 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Justin Lebar9c375812016-07-15 18:27:10 +0000803 return DAG.getStore(Op.getOperand(0), SDLoc(Op), Addr, Op.getOperand(1),
804 MachinePointerInfo(SV));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000805}
806
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000807SDValue HexagonTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek23bcf062018-04-19 14:24:31 +0000808 const SDLoc &dl(Op);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000809 SDValue LHS = Op.getOperand(0);
810 SDValue RHS = Op.getOperand(1);
Krzysztof Parzyszek23bcf062018-04-19 14:24:31 +0000811 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
812 MVT ResTy = ty(Op);
813 MVT OpTy = ty(LHS);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000814
Krzysztof Parzyszek23bcf062018-04-19 14:24:31 +0000815 if (OpTy == MVT::v2i16 || OpTy == MVT::v4i8) {
816 MVT ElemTy = OpTy.getVectorElementType();
817 assert(ElemTy.isScalarInteger());
818 MVT WideTy = MVT::getVectorVT(MVT::getIntegerVT(2*ElemTy.getSizeInBits()),
819 OpTy.getVectorNumElements());
820 return DAG.getSetCC(dl, ResTy,
821 DAG.getSExtOrTrunc(LHS, SDLoc(LHS), WideTy),
822 DAG.getSExtOrTrunc(RHS, SDLoc(RHS), WideTy), CC);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000823 }
824
825 // Treat all other vector types as legal.
Krzysztof Parzyszek23bcf062018-04-19 14:24:31 +0000826 if (ResTy.isVector())
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000827 return Op;
828
Krzysztof Parzyszek23bcf062018-04-19 14:24:31 +0000829 // Comparisons of short integers should use sign-extend, not zero-extend,
830 // since we can represent small negative values in the compare instructions.
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000831 // The LLVM default is to use zero-extend arbitrarily in these cases.
Krzysztof Parzyszek23bcf062018-04-19 14:24:31 +0000832 auto isSExtFree = [this](SDValue N) {
833 switch (N.getOpcode()) {
834 case ISD::TRUNCATE: {
835 // A sign-extend of a truncate of a sign-extend is free.
836 SDValue Op = N.getOperand(0);
837 if (Op.getOpcode() != ISD::AssertSext)
838 return false;
839 MVT OrigTy = cast<VTSDNode>(Op.getOperand(1))->getVT().getSimpleVT();
840 unsigned ThisBW = ty(N).getSizeInBits();
841 unsigned OrigBW = OrigTy.getSizeInBits();
842 // The type that was sign-extended to get the AssertSext must be
843 // narrower than the type of N (so that N has still the same value
844 // as the original).
845 return ThisBW >= OrigBW;
846 }
847 case ISD::LOAD:
848 // We have sign-extended loads.
849 return true;
850 }
851 return false;
852 };
853
854 if (OpTy == MVT::i8 || OpTy == MVT::i16) {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000855 ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS);
Krzysztof Parzyszek23bcf062018-04-19 14:24:31 +0000856 bool IsNegative = C && C->getAPIntValue().isNegative();
857 if (IsNegative || isSExtFree(LHS) || isSExtFree(RHS))
858 return DAG.getSetCC(dl, ResTy,
859 DAG.getSExtOrTrunc(LHS, SDLoc(LHS), MVT::i32),
860 DAG.getSExtOrTrunc(RHS, SDLoc(RHS), MVT::i32), CC);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000861 }
Krzysztof Parzyszek23bcf062018-04-19 14:24:31 +0000862
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000863 return SDValue();
864}
865
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000866SDValue
867HexagonTargetLowering::LowerVSELECT(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000868 SDValue PredOp = Op.getOperand(0);
869 SDValue Op1 = Op.getOperand(1), Op2 = Op.getOperand(2);
870 EVT OpVT = Op1.getValueType();
871 SDLoc DL(Op);
872
873 if (OpVT == MVT::v2i16) {
874 SDValue X1 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i32, Op1);
875 SDValue X2 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i32, Op2);
876 SDValue SL = DAG.getNode(ISD::VSELECT, DL, MVT::v2i32, PredOp, X1, X2);
877 SDValue TR = DAG.getNode(ISD::TRUNCATE, DL, MVT::v2i16, SL);
878 return TR;
879 }
880
881 return SDValue();
882}
883
Krzysztof Parzyszek91ff5c62017-08-01 13:12:53 +0000884static Constant *convert_i1_to_i8(const Constant *ConstVal) {
885 SmallVector<Constant *, 128> NewConst;
886 const ConstantVector *CV = dyn_cast<ConstantVector>(ConstVal);
887 if (!CV)
888 return nullptr;
889
890 LLVMContext &Ctx = ConstVal->getContext();
891 IRBuilder<> IRB(Ctx);
892 unsigned NumVectorElements = CV->getNumOperands();
893 assert(isPowerOf2_32(NumVectorElements) &&
894 "conversion only supported for pow2 VectorSize!");
895
896 for (unsigned i = 0; i < NumVectorElements / 8; ++i) {
897 uint8_t x = 0;
898 for (unsigned j = 0; j < 8; ++j) {
899 uint8_t y = CV->getOperand(i * 8 + j)->getUniqueInteger().getZExtValue();
900 x |= y << (7 - j);
901 }
902 assert((x == 0 || x == 255) && "Either all 0's or all 1's expected!");
903 NewConst.push_back(IRB.getInt8(x));
904 }
905 return ConstantVector::get(NewConst);
906}
907
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000908SDValue
Sirish Pande69295b82012-05-10 20:20:25 +0000909HexagonTargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
910 EVT ValTy = Op.getValueType();
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000911 ConstantPoolSDNode *CPN = cast<ConstantPoolSDNode>(Op);
Krzysztof Parzyszek91ff5c62017-08-01 13:12:53 +0000912 Constant *CVal = nullptr;
913 bool isVTi1Type = false;
914 if (const Constant *ConstVal = dyn_cast<Constant>(CPN->getConstVal())) {
915 Type *CValTy = ConstVal->getType();
916 if (CValTy->isVectorTy() &&
917 CValTy->getVectorElementType()->isIntegerTy(1)) {
918 CVal = convert_i1_to_i8(ConstVal);
919 isVTi1Type = (CVal != nullptr);
920 }
921 }
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000922 unsigned Align = CPN->getAlignment();
Rafael Espindola405e25a2016-06-26 22:24:01 +0000923 bool IsPositionIndependent = isPositionIndependent();
924 unsigned char TF = IsPositionIndependent ? HexagonII::MO_PCREL : 0;
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000925
Ron Lieberman822ee882016-08-13 23:41:11 +0000926 unsigned Offset = 0;
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000927 SDValue T;
928 if (CPN->isMachineConstantPoolEntry())
Ron Lieberman822ee882016-08-13 23:41:11 +0000929 T = DAG.getTargetConstantPool(CPN->getMachineCPVal(), ValTy, Align, Offset,
930 TF);
Krzysztof Parzyszek91ff5c62017-08-01 13:12:53 +0000931 else if (isVTi1Type)
932 T = DAG.getTargetConstantPool(CVal, ValTy, Align, Offset, TF);
Sirish Pande69295b82012-05-10 20:20:25 +0000933 else
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +0000934 T = DAG.getTargetConstantPool(CPN->getConstVal(), ValTy, Align, Offset, TF);
Ron Lieberman822ee882016-08-13 23:41:11 +0000935
936 assert(cast<ConstantPoolSDNode>(T)->getTargetFlags() == TF &&
937 "Inconsistent target flag encountered");
938
Rafael Espindola405e25a2016-06-26 22:24:01 +0000939 if (IsPositionIndependent)
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000940 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), ValTy, T);
941 return DAG.getNode(HexagonISD::CP, SDLoc(Op), ValTy, T);
942}
943
944SDValue
945HexagonTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
946 EVT VT = Op.getValueType();
947 int Idx = cast<JumpTableSDNode>(Op)->getIndex();
Rafael Espindola405e25a2016-06-26 22:24:01 +0000948 if (isPositionIndependent()) {
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000949 SDValue T = DAG.getTargetJumpTable(Idx, VT, HexagonII::MO_PCREL);
950 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), VT, T);
951 }
952
953 SDValue T = DAG.getTargetJumpTable(Idx, VT);
954 return DAG.getNode(HexagonISD::JT, SDLoc(Op), VT, T);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000955}
956
957SDValue
958HexagonTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000959 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000960 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +0000961 MachineFrameInfo &MFI = MF.getFrameInfo();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000962 MFI.setReturnAddressIsTaken(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000963
Bill Wendling908bf812014-01-06 00:43:20 +0000964 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +0000965 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +0000966
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000967 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000968 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000969 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
970 if (Depth) {
971 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000972 SDValue Offset = DAG.getConstant(4, dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000973 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
974 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Justin Lebar9c375812016-07-15 18:27:10 +0000975 MachinePointerInfo());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000976 }
977
978 // Return LR, which contains the return address. Mark it an implicit live-in.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000979 unsigned Reg = MF.addLiveIn(HRI.getRARegister(), getRegClassFor(MVT::i32));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000980 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
981}
982
983SDValue
984HexagonTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000985 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Matthias Braun941a7052016-07-28 18:40:00 +0000986 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000987 MFI.setFrameAddressIsTaken(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000988
989 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000990 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000991 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
992 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000993 HRI.getFrameRegister(), VT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000994 while (Depth--)
995 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
Justin Lebar9c375812016-07-15 18:27:10 +0000996 MachinePointerInfo());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000997 return FrameAddr;
998}
999
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001000SDValue
1001HexagonTargetLowering::LowerATOMIC_FENCE(SDValue Op, SelectionDAG& DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001002 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001003 return DAG.getNode(HexagonISD::BARRIER, dl, MVT::Other, Op.getOperand(0));
1004}
1005
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001006SDValue
1007HexagonTargetLowering::LowerGLOBALADDRESS(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001008 SDLoc dl(Op);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001009 auto *GAN = cast<GlobalAddressSDNode>(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00001010 auto PtrVT = getPointerTy(DAG.getDataLayout());
Krzysztof Parzyszek96a28412018-01-30 18:10:27 +00001011 auto *GV = GAN->getGlobal();
1012 int64_t Offset = GAN->getOffset();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001013
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001014 auto &HLOF = *HTM.getObjFileLowering();
1015 Reloc::Model RM = HTM.getRelocationModel();
1016
1017 if (RM == Reloc::Static) {
1018 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, Offset);
Peter Collingbourne67335642016-10-24 19:23:39 +00001019 const GlobalObject *GO = GV->getBaseObject();
Krzysztof Parzyszek44e180b2018-05-14 21:01:56 +00001020 if (GO && Subtarget.useSmallData() && HLOF.isGlobalInSmallSection(GO, HTM))
Krzysztof Parzyszek96a28412018-01-30 18:10:27 +00001021 return DAG.getNode(HexagonISD::CONST32_GP, dl, PtrVT, GA);
1022 return DAG.getNode(HexagonISD::CONST32, dl, PtrVT, GA);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001023 }
1024
Krzysztof Parzyszek96a28412018-01-30 18:10:27 +00001025 bool UsePCRel = getTargetMachine().shouldAssumeDSOLocal(*GV->getParent(), GV);
1026 if (UsePCRel) {
1027 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, Offset,
1028 HexagonII::MO_PCREL);
1029 return DAG.getNode(HexagonISD::AT_PCREL, dl, PtrVT, GA);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001030 }
1031
Krzysztof Parzyszek96a28412018-01-30 18:10:27 +00001032 // Use GOT index.
1033 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
1034 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, HexagonII::MO_GOT);
1035 SDValue Off = DAG.getConstant(Offset, dl, MVT::i32);
1036 return DAG.getNode(HexagonISD::AT_GOT, dl, PtrVT, GOT, GA, Off);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001037}
1038
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001039// Specifies that for loads and stores VT can be promoted to PromotedLdStVT.
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001040SDValue
1041HexagonTargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
1042 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001043 SDLoc dl(Op);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001044 EVT PtrVT = getPointerTy(DAG.getDataLayout());
1045
1046 Reloc::Model RM = HTM.getRelocationModel();
1047 if (RM == Reloc::Static) {
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001048 SDValue A = DAG.getTargetBlockAddress(BA, PtrVT);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001049 return DAG.getNode(HexagonISD::CONST32_GP, dl, PtrVT, A);
1050 }
1051
1052 SDValue A = DAG.getTargetBlockAddress(BA, PtrVT, 0, HexagonII::MO_PCREL);
1053 return DAG.getNode(HexagonISD::AT_PCREL, dl, PtrVT, A);
1054}
1055
1056SDValue
1057HexagonTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op, SelectionDAG &DAG)
1058 const {
1059 EVT PtrVT = getPointerTy(DAG.getDataLayout());
1060 SDValue GOTSym = DAG.getTargetExternalSymbol(HEXAGON_GOT_SYM_NAME, PtrVT,
1061 HexagonII::MO_PCREL);
1062 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), PtrVT, GOTSym);
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001063}
1064
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001065SDValue
1066HexagonTargetLowering::GetDynamicTLSAddr(SelectionDAG &DAG, SDValue Chain,
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001067 GlobalAddressSDNode *GA, SDValue Glue, EVT PtrVT, unsigned ReturnReg,
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001068 unsigned char OperandFlags) const {
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001069 MachineFunction &MF = DAG.getMachineFunction();
1070 MachineFrameInfo &MFI = MF.getFrameInfo();
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001071 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1072 SDLoc dl(GA);
1073 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
1074 GA->getValueType(0),
1075 GA->getOffset(),
1076 OperandFlags);
1077 // Create Operands for the call.The Operands should have the following:
1078 // 1. Chain SDValue
1079 // 2. Callee which in this case is the Global address value.
1080 // 3. Registers live into the call.In this case its R0, as we
1081 // have just one argument to be passed.
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001082 // 4. Glue.
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001083 // Note: The order is important.
1084
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001085 const auto &HRI = *Subtarget.getRegisterInfo();
1086 const uint32_t *Mask = HRI.getCallPreservedMask(MF, CallingConv::C);
1087 assert(Mask && "Missing call preserved mask for calling convention");
1088 SDValue Ops[] = { Chain, TGA, DAG.getRegister(Hexagon::R0, PtrVT),
1089 DAG.getRegisterMask(Mask), Glue };
1090 Chain = DAG.getNode(HexagonISD::CALL, dl, NodeTys, Ops);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001091
1092 // Inform MFI that function has calls.
Matthias Braun941a7052016-07-28 18:40:00 +00001093 MFI.setAdjustsStack(true);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001094
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001095 Glue = Chain.getValue(1);
1096 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Glue);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001097}
1098
1099//
1100// Lower using the intial executable model for TLS addresses
1101//
1102SDValue
1103HexagonTargetLowering::LowerToTLSInitialExecModel(GlobalAddressSDNode *GA,
1104 SelectionDAG &DAG) const {
1105 SDLoc dl(GA);
1106 int64_t Offset = GA->getOffset();
1107 auto PtrVT = getPointerTy(DAG.getDataLayout());
1108
1109 // Get the thread pointer.
1110 SDValue TP = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Hexagon::UGP, PtrVT);
1111
Rafael Espindola405e25a2016-06-26 22:24:01 +00001112 bool IsPositionIndependent = isPositionIndependent();
1113 unsigned char TF =
1114 IsPositionIndependent ? HexagonII::MO_IEGOT : HexagonII::MO_IE;
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001115
1116 // First generate the TLS symbol address
1117 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT,
1118 Offset, TF);
1119
1120 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1121
Rafael Espindola405e25a2016-06-26 22:24:01 +00001122 if (IsPositionIndependent) {
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001123 // Generate the GOT pointer in case of position independent code
1124 SDValue GOT = LowerGLOBAL_OFFSET_TABLE(Sym, DAG);
1125
1126 // Add the TLS Symbol address to GOT pointer.This gives
1127 // GOT relative relocation for the symbol.
1128 Sym = DAG.getNode(ISD::ADD, dl, PtrVT, GOT, Sym);
1129 }
1130
1131 // Load the offset value for TLS symbol.This offset is relative to
1132 // thread pointer.
Justin Lebar9c375812016-07-15 18:27:10 +00001133 SDValue LoadOffset =
1134 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Sym, MachinePointerInfo());
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001135
1136 // Address of the thread local variable is the add of thread
1137 // pointer and the offset of the variable.
1138 return DAG.getNode(ISD::ADD, dl, PtrVT, TP, LoadOffset);
1139}
1140
1141//
1142// Lower using the local executable model for TLS addresses
1143//
1144SDValue
1145HexagonTargetLowering::LowerToTLSLocalExecModel(GlobalAddressSDNode *GA,
1146 SelectionDAG &DAG) const {
1147 SDLoc dl(GA);
1148 int64_t Offset = GA->getOffset();
1149 auto PtrVT = getPointerTy(DAG.getDataLayout());
1150
1151 // Get the thread pointer.
1152 SDValue TP = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Hexagon::UGP, PtrVT);
1153 // Generate the TLS symbol address
1154 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT, Offset,
1155 HexagonII::MO_TPREL);
1156 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1157
1158 // Address of the thread local variable is the add of thread
1159 // pointer and the offset of the variable.
1160 return DAG.getNode(ISD::ADD, dl, PtrVT, TP, Sym);
1161}
1162
1163//
1164// Lower using the general dynamic model for TLS addresses
1165//
1166SDValue
1167HexagonTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
1168 SelectionDAG &DAG) const {
1169 SDLoc dl(GA);
1170 int64_t Offset = GA->getOffset();
1171 auto PtrVT = getPointerTy(DAG.getDataLayout());
1172
1173 // First generate the TLS symbol address
1174 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT, Offset,
1175 HexagonII::MO_GDGOT);
1176
1177 // Then, generate the GOT pointer
1178 SDValue GOT = LowerGLOBAL_OFFSET_TABLE(TGA, DAG);
1179
1180 // Add the TLS symbol and the GOT pointer
1181 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1182 SDValue Chain = DAG.getNode(ISD::ADD, dl, PtrVT, GOT, Sym);
1183
1184 // Copy over the argument to R0
1185 SDValue InFlag;
1186 Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, Hexagon::R0, Chain, InFlag);
1187 InFlag = Chain.getValue(1);
1188
Krzysztof Parzyszeka7503832017-05-02 18:15:33 +00001189 unsigned Flags =
1190 static_cast<const HexagonSubtarget &>(DAG.getSubtarget()).useLongCalls()
1191 ? HexagonII::MO_GDPLT | HexagonII::HMOTF_ConstExtended
1192 : HexagonII::MO_GDPLT;
1193
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001194 return GetDynamicTLSAddr(DAG, Chain, GA, InFlag, PtrVT,
Krzysztof Parzyszeka7503832017-05-02 18:15:33 +00001195 Hexagon::R0, Flags);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001196}
1197
1198//
1199// Lower TLS addresses.
1200//
1201// For now for dynamic models, we only support the general dynamic model.
1202//
1203SDValue
1204HexagonTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1205 SelectionDAG &DAG) const {
1206 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1207
1208 switch (HTM.getTLSModel(GA->getGlobal())) {
1209 case TLSModel::GeneralDynamic:
1210 case TLSModel::LocalDynamic:
1211 return LowerToTLSGeneralDynamicModel(GA, DAG);
1212 case TLSModel::InitialExec:
1213 return LowerToTLSInitialExecModel(GA, DAG);
1214 case TLSModel::LocalExec:
1215 return LowerToTLSLocalExecModel(GA, DAG);
1216 }
1217 llvm_unreachable("Bogus TLS model");
1218}
1219
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001220//===----------------------------------------------------------------------===//
1221// TargetLowering Implementation
1222//===----------------------------------------------------------------------===//
1223
Eric Christopherd737b762015-02-02 22:11:36 +00001224HexagonTargetLowering::HexagonTargetLowering(const TargetMachine &TM,
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001225 const HexagonSubtarget &ST)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001226 : TargetLowering(TM), HTM(static_cast<const HexagonTargetMachine&>(TM)),
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001227 Subtarget(ST) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001228 bool IsV4 = !Subtarget.hasV5TOps();
1229 auto &HRI = *Subtarget.getRegisterInfo();
Sirish Pande69295b82012-05-10 20:20:25 +00001230
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001231 setPrefLoopAlignment(4);
1232 setPrefFunctionAlignment(4);
1233 setMinFunctionAlignment(2);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001234 setStackPointerRegisterToSaveRestore(HRI.getStackRegister());
Krzysztof Parzyszekb3e50ac2018-01-05 20:41:50 +00001235 setBooleanContents(TargetLoweringBase::UndefinedBooleanContent);
1236 setBooleanVectorContents(TargetLoweringBase::UndefinedBooleanContent);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001237
Krzysztof Parzyszekf228c952016-06-22 16:07:10 +00001238 setMaxAtomicSizeInBitsSupported(64);
1239 setMinCmpXchgSizeInBits(32);
1240
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001241 if (EnableHexSDNodeSched)
1242 setSchedulingPreference(Sched::VLIW);
1243 else
1244 setSchedulingPreference(Sched::Source);
1245
1246 // Limits for inline expansion of memcpy/memmove
1247 MaxStoresPerMemcpy = MaxStoresPerMemcpyCL;
1248 MaxStoresPerMemcpyOptSize = MaxStoresPerMemcpyOptSizeCL;
1249 MaxStoresPerMemmove = MaxStoresPerMemmoveCL;
1250 MaxStoresPerMemmoveOptSize = MaxStoresPerMemmoveOptSizeCL;
1251 MaxStoresPerMemset = MaxStoresPerMemsetCL;
1252 MaxStoresPerMemsetOptSize = MaxStoresPerMemsetOptSizeCL;
1253
1254 //
1255 // Set up register classes.
1256 //
1257
1258 addRegisterClass(MVT::i1, &Hexagon::PredRegsRegClass);
1259 addRegisterClass(MVT::v2i1, &Hexagon::PredRegsRegClass); // bbbbaaaa
1260 addRegisterClass(MVT::v4i1, &Hexagon::PredRegsRegClass); // ddccbbaa
1261 addRegisterClass(MVT::v8i1, &Hexagon::PredRegsRegClass); // hgfedcba
1262 addRegisterClass(MVT::i32, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001263 addRegisterClass(MVT::v2i16, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00001264 addRegisterClass(MVT::v4i8, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001265 addRegisterClass(MVT::i64, &Hexagon::DoubleRegsRegClass);
1266 addRegisterClass(MVT::v8i8, &Hexagon::DoubleRegsRegClass);
1267 addRegisterClass(MVT::v4i16, &Hexagon::DoubleRegsRegClass);
1268 addRegisterClass(MVT::v2i32, &Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001269
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001270 if (Subtarget.hasV5TOps()) {
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001271 addRegisterClass(MVT::f32, &Hexagon::IntRegsRegClass);
1272 addRegisterClass(MVT::f64, &Hexagon::DoubleRegsRegClass);
1273 }
Sirish Pande69295b82012-05-10 20:20:25 +00001274
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001275 //
1276 // Handling of scalar operations.
1277 //
1278 // All operations default to "legal", except:
1279 // - indexed loads and stores (pre-/post-incremented),
1280 // - ANY_EXTEND_VECTOR_INREG, ATOMIC_CMP_SWAP_WITH_SUCCESS, CONCAT_VECTORS,
1281 // ConstantFP, DEBUGTRAP, FCEIL, FCOPYSIGN, FEXP, FEXP2, FFLOOR, FGETSIGN,
1282 // FLOG, FLOG2, FLOG10, FMAXNUM, FMINNUM, FNEARBYINT, FRINT, FROUND, TRAP,
1283 // FTRUNC, PREFETCH, SIGN_EXTEND_VECTOR_INREG, ZERO_EXTEND_VECTOR_INREG,
1284 // which default to "expand" for at least one type.
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001285
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001286 // Misc operations.
1287 setOperationAction(ISD::ConstantFP, MVT::f32, Legal); // Default: expand
1288 setOperationAction(ISD::ConstantFP, MVT::f64, Legal); // Default: expand
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001289
1290 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001291 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001292 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001293 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
1294 setOperationAction(ISD::INLINEASM, MVT::Other, Custom);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00001295 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +00001296 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00001297 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001298 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001299 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001300 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001301 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001302
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001303 // Custom legalize GlobalAddress nodes into CONST32.
1304 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001305 setOperationAction(ISD::GlobalAddress, MVT::i8, Custom);
1306 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001307
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001308 // Hexagon needs to optimize cases with negative constants.
Krzysztof Parzyszek23bcf062018-04-19 14:24:31 +00001309 setOperationAction(ISD::SETCC, MVT::i8, Custom);
1310 setOperationAction(ISD::SETCC, MVT::i16, Custom);
1311 setOperationAction(ISD::SETCC, MVT::v4i8, Custom);
1312 setOperationAction(ISD::SETCC, MVT::v2i16, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001313
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001314 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
1315 setOperationAction(ISD::VASTART, MVT::Other, Custom);
1316 setOperationAction(ISD::VAEND, MVT::Other, Expand);
1317 setOperationAction(ISD::VAARG, MVT::Other, Expand);
Krzysztof Parzyszekf6088122018-03-02 18:35:57 +00001318 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001319
1320 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
1321 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
1322 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
1323
1324 if (EmitJumpTables)
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001325 setMinimumJumpTableEntries(MinimumJumpTables);
Krzysztof Parzyszeka61f7da2016-01-13 21:43:13 +00001326 else
Eugene Zelenko58655bb2016-12-17 01:09:05 +00001327 setMinimumJumpTableEntries(std::numeric_limits<int>::max());
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001328 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001329
Krzysztof Parzyszek0b6187c2018-06-01 14:00:32 +00001330 // Hexagon has A4_addp_c and A4_subp_c that take and generate a carry bit,
1331 // but they only operate on i64.
Krzysztof Parzyszek2c4487d2015-04-13 20:37:01 +00001332 for (MVT VT : MVT::integer_valuetypes()) {
Krzysztof Parzyszek0b6187c2018-06-01 14:00:32 +00001333 setOperationAction(ISD::UADDO, VT, Expand);
1334 setOperationAction(ISD::USUBO, VT, Expand);
1335 setOperationAction(ISD::SADDO, VT, Expand);
1336 setOperationAction(ISD::SSUBO, VT, Expand);
1337 setOperationAction(ISD::ADDCARRY, VT, Expand);
1338 setOperationAction(ISD::SUBCARRY, VT, Expand);
Krzysztof Parzyszek2c4487d2015-04-13 20:37:01 +00001339 }
Krzysztof Parzyszek0b6187c2018-06-01 14:00:32 +00001340 setOperationAction(ISD::ADDCARRY, MVT::i64, Custom);
1341 setOperationAction(ISD::SUBCARRY, MVT::i64, Custom);
Krzysztof Parzyszek2c4487d2015-04-13 20:37:01 +00001342
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001343 setOperationAction(ISD::CTLZ, MVT::i8, Promote);
1344 setOperationAction(ISD::CTLZ, MVT::i16, Promote);
1345 setOperationAction(ISD::CTTZ, MVT::i8, Promote);
1346 setOperationAction(ISD::CTTZ, MVT::i16, Promote);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001347
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001348 // In V5, popcount can count # of 1s in i64 but returns i32.
1349 // On V4 it will be expanded (set later).
1350 setOperationAction(ISD::CTPOP, MVT::i8, Promote);
1351 setOperationAction(ISD::CTPOP, MVT::i16, Promote);
1352 setOperationAction(ISD::CTPOP, MVT::i32, Promote);
Krzysztof Parzyszekaf5ff652017-02-23 15:02:09 +00001353 setOperationAction(ISD::CTPOP, MVT::i64, Legal);
1354
1355 setOperationAction(ISD::BITREVERSE, MVT::i32, Legal);
1356 setOperationAction(ISD::BITREVERSE, MVT::i64, Legal);
1357 setOperationAction(ISD::BSWAP, MVT::i32, Legal);
1358 setOperationAction(ISD::BSWAP, MVT::i64, Legal);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001359
Benjamin Kramer62460692015-04-25 14:46:53 +00001360 for (unsigned IntExpOp :
Krzysztof Parzyszekaafb8c22018-06-05 12:49:19 +00001361 {ISD::SDIV, ISD::UDIV, ISD::SREM, ISD::UREM,
1362 ISD::SDIVREM, ISD::UDIVREM, ISD::ROTL, ISD::ROTR,
1363 ISD::SHL_PARTS, ISD::SRA_PARTS, ISD::SRL_PARTS,
1364 ISD::SMUL_LOHI, ISD::UMUL_LOHI}) {
1365 for (MVT VT : MVT::integer_valuetypes())
1366 setOperationAction(IntExpOp, VT, Expand);
Benjamin Kramer62460692015-04-25 14:46:53 +00001367 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001368
Benjamin Kramer62460692015-04-25 14:46:53 +00001369 for (unsigned FPExpOp :
1370 {ISD::FDIV, ISD::FREM, ISD::FSQRT, ISD::FSIN, ISD::FCOS, ISD::FSINCOS,
1371 ISD::FPOW, ISD::FCOPYSIGN}) {
Krzysztof Parzyszekaafb8c22018-06-05 12:49:19 +00001372 for (MVT VT : MVT::fp_valuetypes())
1373 setOperationAction(FPExpOp, VT, Expand);
Benjamin Kramer62460692015-04-25 14:46:53 +00001374 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001375
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001376 // No extending loads from i32.
1377 for (MVT VT : MVT::integer_valuetypes()) {
1378 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand);
1379 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand);
1380 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand);
1381 }
1382 // Turn FP truncstore into trunc + store.
1383 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Michael Kuperstein2bc3d4d2016-08-18 20:08:15 +00001384 // Turn FP extload into load/fpextend.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001385 for (MVT VT : MVT::fp_valuetypes())
1386 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001387
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001388 // Expand BR_CC and SELECT_CC for all integer and fp types.
1389 for (MVT VT : MVT::integer_valuetypes()) {
1390 setOperationAction(ISD::BR_CC, VT, Expand);
1391 setOperationAction(ISD::SELECT_CC, VT, Expand);
1392 }
1393 for (MVT VT : MVT::fp_valuetypes()) {
1394 setOperationAction(ISD::BR_CC, VT, Expand);
1395 setOperationAction(ISD::SELECT_CC, VT, Expand);
1396 }
1397 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001398
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001399 //
1400 // Handling of vector operations.
1401 //
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001402
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001403 promoteLdStType(MVT::v4i8, MVT::i32);
1404 promoteLdStType(MVT::v2i16, MVT::i32);
1405 promoteLdStType(MVT::v8i8, MVT::i64);
Krzysztof Parzyszek5eef92e2017-07-17 15:45:45 +00001406 promoteLdStType(MVT::v4i16, MVT::i64);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001407 promoteLdStType(MVT::v2i32, MVT::i64);
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.
1488 for (MVT VecVT : {MVT::i32, MVT::v4i8, MVT::i64, MVT::v8i8,
1489 MVT::v2i16, MVT::v4i16, MVT::v2i32}) {
1490 setOperationAction(ISD::LOAD, VecVT, Custom);
1491 }
1492
Krzysztof Parzyszek99152912018-03-16 15:03:37 +00001493 for (MVT VT : {MVT::v2i16, MVT::v4i8, MVT::v2i32, MVT::v4i16, MVT::v2i32}) {
1494 setCondCodeAction(ISD::SETLT, VT, Expand);
1495 setCondCodeAction(ISD::SETLE, VT, Expand);
1496 setCondCodeAction(ISD::SETULT, VT, Expand);
1497 setCondCodeAction(ISD::SETULE, VT, Expand);
1498 }
1499
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001500 // Custom-lower bitcasts from i8 to v8i1.
1501 setOperationAction(ISD::BITCAST, MVT::i8, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001502 setOperationAction(ISD::SETCC, MVT::v2i16, Custom);
1503 setOperationAction(ISD::VSELECT, MVT::v2i16, Custom);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001504 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i8, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001505 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
1506 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00001507
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001508 // Subtarget-specific operation actions.
1509 //
Krzysztof Parzyszek3d671242018-06-12 12:49:36 +00001510 if (Subtarget.hasV60TOps()) {
1511 setOperationAction(ISD::ROTL, MVT::i32, Custom);
1512 setOperationAction(ISD::ROTL, MVT::i64, Custom);
1513 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001514 if (Subtarget.hasV5TOps()) {
1515 setOperationAction(ISD::FMA, MVT::f64, Expand);
1516 setOperationAction(ISD::FADD, MVT::f64, Expand);
1517 setOperationAction(ISD::FSUB, MVT::f64, Expand);
1518 setOperationAction(ISD::FMUL, MVT::f64, Expand);
1519
Krzysztof Parzyszekbd8ef4b2016-08-19 13:34:31 +00001520 setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
1521 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
1522
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001523 setOperationAction(ISD::FP_TO_UINT, MVT::i1, Promote);
1524 setOperationAction(ISD::FP_TO_UINT, MVT::i8, Promote);
1525 setOperationAction(ISD::FP_TO_UINT, MVT::i16, Promote);
1526 setOperationAction(ISD::FP_TO_SINT, MVT::i1, Promote);
1527 setOperationAction(ISD::FP_TO_SINT, MVT::i8, Promote);
1528 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
1529 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Promote);
1530 setOperationAction(ISD::UINT_TO_FP, MVT::i8, Promote);
1531 setOperationAction(ISD::UINT_TO_FP, MVT::i16, Promote);
1532 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Promote);
1533 setOperationAction(ISD::SINT_TO_FP, MVT::i8, Promote);
1534 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001535 } else { // V4
1536 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
1537 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Expand);
1538 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
1539 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
1540 setOperationAction(ISD::FP_TO_SINT, MVT::f64, Expand);
1541 setOperationAction(ISD::FP_TO_SINT, MVT::f32, Expand);
1542 setOperationAction(ISD::FP_EXTEND, MVT::f32, Expand);
1543 setOperationAction(ISD::FP_ROUND, MVT::f64, Expand);
1544 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
1545
1546 setOperationAction(ISD::CTPOP, MVT::i8, Expand);
1547 setOperationAction(ISD::CTPOP, MVT::i16, Expand);
1548 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
1549 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
1550
1551 // Expand these operations for both f32 and f64:
Benjamin Kramer62460692015-04-25 14:46:53 +00001552 for (unsigned FPExpOpV4 :
1553 {ISD::FADD, ISD::FSUB, ISD::FMUL, ISD::FABS, ISD::FNEG, ISD::FMA}) {
1554 setOperationAction(FPExpOpV4, MVT::f32, Expand);
1555 setOperationAction(FPExpOpV4, MVT::f64, Expand);
1556 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001557
Benjamin Kramer62460692015-04-25 14:46:53 +00001558 for (ISD::CondCode FPExpCCV4 :
1559 {ISD::SETOEQ, ISD::SETOGT, ISD::SETOLT, ISD::SETOGE, ISD::SETOLE,
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001560 ISD::SETUO, ISD::SETO}) {
Benjamin Kramer62460692015-04-25 14:46:53 +00001561 setCondCodeAction(FPExpCCV4, MVT::f32, Expand);
1562 setCondCodeAction(FPExpCCV4, MVT::f64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001563 }
1564 }
1565
1566 // Handling of indexed loads/stores: default is "expand".
1567 //
Brendon Cahoone5ed5632018-05-18 18:14:44 +00001568 for (MVT VT : {MVT::i8, MVT::i16, MVT::i32, MVT::i64, MVT::f32, MVT::f64,
1569 MVT::v2i16, MVT::v2i32, MVT::v4i8, MVT::v4i16, MVT::v8i8}) {
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +00001570 setIndexedLoadAction(ISD::POST_INC, VT, Legal);
1571 setIndexedStoreAction(ISD::POST_INC, VT, Legal);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001572 }
1573
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00001574 if (Subtarget.useHVXOps())
1575 initializeHVXLowering();
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00001576
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001577 computeRegisterProperties(&HRI);
1578
1579 //
1580 // Library calls for unsupported operations
1581 //
1582 bool FastMath = EnableFastMath;
1583
Benjamin Kramera37c8092015-04-25 14:46:46 +00001584 setLibcallName(RTLIB::SDIV_I32, "__hexagon_divsi3");
1585 setLibcallName(RTLIB::SDIV_I64, "__hexagon_divdi3");
1586 setLibcallName(RTLIB::UDIV_I32, "__hexagon_udivsi3");
1587 setLibcallName(RTLIB::UDIV_I64, "__hexagon_udivdi3");
1588 setLibcallName(RTLIB::SREM_I32, "__hexagon_modsi3");
1589 setLibcallName(RTLIB::SREM_I64, "__hexagon_moddi3");
1590 setLibcallName(RTLIB::UREM_I32, "__hexagon_umodsi3");
1591 setLibcallName(RTLIB::UREM_I64, "__hexagon_umoddi3");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001592
Benjamin Kramera37c8092015-04-25 14:46:46 +00001593 setLibcallName(RTLIB::SINTTOFP_I128_F64, "__hexagon_floattidf");
1594 setLibcallName(RTLIB::SINTTOFP_I128_F32, "__hexagon_floattisf");
1595 setLibcallName(RTLIB::FPTOUINT_F32_I128, "__hexagon_fixunssfti");
1596 setLibcallName(RTLIB::FPTOUINT_F64_I128, "__hexagon_fixunsdfti");
1597 setLibcallName(RTLIB::FPTOSINT_F32_I128, "__hexagon_fixsfti");
1598 setLibcallName(RTLIB::FPTOSINT_F64_I128, "__hexagon_fixdfti");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001599
1600 if (IsV4) {
1601 // Handle single-precision floating point operations on V4.
Benjamin Kramera37c8092015-04-25 14:46:46 +00001602 if (FastMath) {
1603 setLibcallName(RTLIB::ADD_F32, "__hexagon_fast_addsf3");
1604 setLibcallName(RTLIB::SUB_F32, "__hexagon_fast_subsf3");
1605 setLibcallName(RTLIB::MUL_F32, "__hexagon_fast_mulsf3");
1606 setLibcallName(RTLIB::OGT_F32, "__hexagon_fast_gtsf2");
1607 setLibcallName(RTLIB::OLT_F32, "__hexagon_fast_ltsf2");
1608 // Double-precision compares.
1609 setLibcallName(RTLIB::OGT_F64, "__hexagon_fast_gtdf2");
1610 setLibcallName(RTLIB::OLT_F64, "__hexagon_fast_ltdf2");
1611 } else {
1612 setLibcallName(RTLIB::ADD_F32, "__hexagon_addsf3");
1613 setLibcallName(RTLIB::SUB_F32, "__hexagon_subsf3");
1614 setLibcallName(RTLIB::MUL_F32, "__hexagon_mulsf3");
1615 setLibcallName(RTLIB::OGT_F32, "__hexagon_gtsf2");
1616 setLibcallName(RTLIB::OLT_F32, "__hexagon_ltsf2");
1617 // Double-precision compares.
1618 setLibcallName(RTLIB::OGT_F64, "__hexagon_gtdf2");
1619 setLibcallName(RTLIB::OLT_F64, "__hexagon_ltdf2");
1620 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001621 }
1622
1623 // This is the only fast library function for sqrtd.
1624 if (FastMath)
Benjamin Kramera37c8092015-04-25 14:46:46 +00001625 setLibcallName(RTLIB::SQRT_F64, "__hexagon_fast2_sqrtdf2");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001626
Benjamin Kramera37c8092015-04-25 14:46:46 +00001627 // Prefix is: nothing for "slow-math",
1628 // "fast2_" for V4 fast-math and V5+ fast-math double-precision
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001629 // (actually, keep fast-math and fast-math2 separate for now)
Benjamin Kramera37c8092015-04-25 14:46:46 +00001630 if (FastMath) {
1631 setLibcallName(RTLIB::ADD_F64, "__hexagon_fast_adddf3");
1632 setLibcallName(RTLIB::SUB_F64, "__hexagon_fast_subdf3");
1633 setLibcallName(RTLIB::MUL_F64, "__hexagon_fast_muldf3");
1634 setLibcallName(RTLIB::DIV_F64, "__hexagon_fast_divdf3");
1635 // Calling __hexagon_fast2_divsf3 with fast-math on V5 (ok).
1636 setLibcallName(RTLIB::DIV_F32, "__hexagon_fast_divsf3");
1637 } else {
1638 setLibcallName(RTLIB::ADD_F64, "__hexagon_adddf3");
1639 setLibcallName(RTLIB::SUB_F64, "__hexagon_subdf3");
1640 setLibcallName(RTLIB::MUL_F64, "__hexagon_muldf3");
1641 setLibcallName(RTLIB::DIV_F64, "__hexagon_divdf3");
1642 setLibcallName(RTLIB::DIV_F32, "__hexagon_divsf3");
1643 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001644
1645 if (Subtarget.hasV5TOps()) {
1646 if (FastMath)
Benjamin Kramera37c8092015-04-25 14:46:46 +00001647 setLibcallName(RTLIB::SQRT_F32, "__hexagon_fast2_sqrtf");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001648 else
Benjamin Kramera37c8092015-04-25 14:46:46 +00001649 setLibcallName(RTLIB::SQRT_F32, "__hexagon_sqrtf");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001650 } else {
1651 // V4
Benjamin Kramera37c8092015-04-25 14:46:46 +00001652 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__hexagon_floatsisf");
1653 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__hexagon_floatsidf");
1654 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__hexagon_floatdisf");
1655 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__hexagon_floatdidf");
1656 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__hexagon_floatunsisf");
1657 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__hexagon_floatunsidf");
1658 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__hexagon_floatundisf");
1659 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__hexagon_floatundidf");
1660 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__hexagon_fixunssfsi");
1661 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__hexagon_fixunssfdi");
1662 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__hexagon_fixunsdfsi");
1663 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__hexagon_fixunsdfdi");
1664 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__hexagon_fixsfsi");
1665 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__hexagon_fixsfdi");
1666 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__hexagon_fixdfsi");
1667 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__hexagon_fixdfdi");
1668 setLibcallName(RTLIB::FPEXT_F32_F64, "__hexagon_extendsfdf2");
1669 setLibcallName(RTLIB::FPROUND_F64_F32, "__hexagon_truncdfsf2");
1670 setLibcallName(RTLIB::OEQ_F32, "__hexagon_eqsf2");
1671 setLibcallName(RTLIB::OEQ_F64, "__hexagon_eqdf2");
1672 setLibcallName(RTLIB::OGE_F32, "__hexagon_gesf2");
1673 setLibcallName(RTLIB::OGE_F64, "__hexagon_gedf2");
1674 setLibcallName(RTLIB::OLE_F32, "__hexagon_lesf2");
1675 setLibcallName(RTLIB::OLE_F64, "__hexagon_ledf2");
1676 setLibcallName(RTLIB::UNE_F32, "__hexagon_nesf2");
1677 setLibcallName(RTLIB::UNE_F64, "__hexagon_nedf2");
1678 setLibcallName(RTLIB::UO_F32, "__hexagon_unordsf2");
1679 setLibcallName(RTLIB::UO_F64, "__hexagon_unorddf2");
1680 setLibcallName(RTLIB::O_F32, "__hexagon_unordsf2");
1681 setLibcallName(RTLIB::O_F64, "__hexagon_unorddf2");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001682 }
1683
1684 // These cause problems when the shift amount is non-constant.
1685 setLibcallName(RTLIB::SHL_I128, nullptr);
1686 setLibcallName(RTLIB::SRL_I128, nullptr);
1687 setLibcallName(RTLIB::SRA_I128, nullptr);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001688}
1689
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001690const char* HexagonTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00001691 switch ((HexagonISD::NodeType)Opcode) {
Krzysztof Parzyszek0b6187c2018-06-01 14:00:32 +00001692 case HexagonISD::ADDC: return "HexagonISD::ADDC";
1693 case HexagonISD::SUBC: return "HexagonISD::SUBC";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001694 case HexagonISD::ALLOCA: return "HexagonISD::ALLOCA";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001695 case HexagonISD::AT_GOT: return "HexagonISD::AT_GOT";
1696 case HexagonISD::AT_PCREL: return "HexagonISD::AT_PCREL";
1697 case HexagonISD::BARRIER: return "HexagonISD::BARRIER";
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00001698 case HexagonISD::CALL: return "HexagonISD::CALL";
1699 case HexagonISD::CALLnr: return "HexagonISD::CALLnr";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001700 case HexagonISD::CALLR: return "HexagonISD::CALLR";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001701 case HexagonISD::COMBINE: return "HexagonISD::COMBINE";
1702 case HexagonISD::CONST32_GP: return "HexagonISD::CONST32_GP";
1703 case HexagonISD::CONST32: return "HexagonISD::CONST32";
1704 case HexagonISD::CP: return "HexagonISD::CP";
1705 case HexagonISD::DCFETCH: return "HexagonISD::DCFETCH";
1706 case HexagonISD::EH_RETURN: return "HexagonISD::EH_RETURN";
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001707 case HexagonISD::TSTBIT: return "HexagonISD::TSTBIT";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001708 case HexagonISD::EXTRACTU: return "HexagonISD::EXTRACTU";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001709 case HexagonISD::INSERT: return "HexagonISD::INSERT";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001710 case HexagonISD::JT: return "HexagonISD::JT";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001711 case HexagonISD::RET_FLAG: return "HexagonISD::RET_FLAG";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001712 case HexagonISD::TC_RETURN: return "HexagonISD::TC_RETURN";
Krzysztof Parzyszekf85dd9f2017-07-10 20:16:44 +00001713 case HexagonISD::VASL: return "HexagonISD::VASL";
1714 case HexagonISD::VASR: return "HexagonISD::VASR";
1715 case HexagonISD::VLSR: return "HexagonISD::VLSR";
1716 case HexagonISD::VSPLAT: return "HexagonISD::VSPLAT";
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001717 case HexagonISD::VEXTRACTW: return "HexagonISD::VEXTRACTW";
1718 case HexagonISD::VINSERTW0: return "HexagonISD::VINSERTW0";
1719 case HexagonISD::VROR: return "HexagonISD::VROR";
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +00001720 case HexagonISD::READCYCLE: return "HexagonISD::READCYCLE";
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00001721 case HexagonISD::VZERO: return "HexagonISD::VZERO";
Krzysztof Parzyszek41a24b72018-04-20 19:38:37 +00001722 case HexagonISD::VSPLATW: return "HexagonISD::VSPLATW";
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001723 case HexagonISD::D2P: return "HexagonISD::D2P";
1724 case HexagonISD::P2D: return "HexagonISD::P2D";
1725 case HexagonISD::V2Q: return "HexagonISD::V2Q";
1726 case HexagonISD::Q2V: return "HexagonISD::Q2V";
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00001727 case HexagonISD::QCAT: return "HexagonISD::QCAT";
Krzysztof Parzyszek69f1d7e2018-02-06 14:16:52 +00001728 case HexagonISD::QTRUE: return "HexagonISD::QTRUE";
1729 case HexagonISD::QFALSE: return "HexagonISD::QFALSE";
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001730 case HexagonISD::TYPECAST: return "HexagonISD::TYPECAST";
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00001731 case HexagonISD::VALIGN: return "HexagonISD::VALIGN";
Krzysztof Parzyszekad83ce42018-02-14 20:46:06 +00001732 case HexagonISD::VALIGNADDR: return "HexagonISD::VALIGNADDR";
Matthias Braund04893f2015-05-07 21:33:59 +00001733 case HexagonISD::OP_END: break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001734 }
Matthias Braund04893f2015-05-07 21:33:59 +00001735 return nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001736}
1737
Krzysztof Parzyszekdc7a5572018-03-29 13:52:46 +00001738// Bit-reverse Load Intrinsic: Check if the instruction is a bit reverse load
1739// intrinsic.
1740static bool isBrevLdIntrinsic(const Value *Inst) {
1741 unsigned ID = cast<IntrinsicInst>(Inst)->getIntrinsicID();
1742 return (ID == Intrinsic::hexagon_L2_loadrd_pbr ||
1743 ID == Intrinsic::hexagon_L2_loadri_pbr ||
1744 ID == Intrinsic::hexagon_L2_loadrh_pbr ||
1745 ID == Intrinsic::hexagon_L2_loadruh_pbr ||
1746 ID == Intrinsic::hexagon_L2_loadrb_pbr ||
1747 ID == Intrinsic::hexagon_L2_loadrub_pbr);
1748}
1749
1750// Bit-reverse Load Intrinsic :Crawl up and figure out the object from previous
1751// instruction. So far we only handle bitcast, extract value and bit reverse
1752// load intrinsic instructions. Should we handle CGEP ?
1753static Value *getBrevLdObject(Value *V) {
1754 if (Operator::getOpcode(V) == Instruction::ExtractValue ||
1755 Operator::getOpcode(V) == Instruction::BitCast)
1756 V = cast<Operator>(V)->getOperand(0);
1757 else if (isa<IntrinsicInst>(V) && isBrevLdIntrinsic(V))
1758 V = cast<Instruction>(V)->getOperand(0);
1759 return V;
1760}
1761
1762// Bit-reverse Load Intrinsic: For a PHI Node return either an incoming edge or
1763// a back edge. If the back edge comes from the intrinsic itself, the incoming
1764// edge is returned.
1765static Value *returnEdge(const PHINode *PN, Value *IntrBaseVal) {
1766 const BasicBlock *Parent = PN->getParent();
1767 int Idx = -1;
1768 for (unsigned i = 0, e = PN->getNumIncomingValues(); i < e; ++i) {
1769 BasicBlock *Blk = PN->getIncomingBlock(i);
1770 // Determine if the back edge is originated from intrinsic.
1771 if (Blk == Parent) {
1772 Value *BackEdgeVal = PN->getIncomingValue(i);
1773 Value *BaseVal;
1774 // Loop over till we return the same Value or we hit the IntrBaseVal.
1775 do {
1776 BaseVal = BackEdgeVal;
1777 BackEdgeVal = getBrevLdObject(BackEdgeVal);
1778 } while ((BaseVal != BackEdgeVal) && (IntrBaseVal != BackEdgeVal));
1779 // If the getBrevLdObject returns IntrBaseVal, we should return the
1780 // incoming edge.
1781 if (IntrBaseVal == BackEdgeVal)
1782 continue;
1783 Idx = i;
1784 break;
1785 } else // Set the node to incoming edge.
1786 Idx = i;
1787 }
1788 assert(Idx >= 0 && "Unexpected index to incoming argument in PHI");
1789 return PN->getIncomingValue(Idx);
1790}
1791
1792// Bit-reverse Load Intrinsic: Figure out the underlying object the base
1793// pointer points to, for the bit-reverse load intrinsic. Setting this to
1794// memoperand might help alias analysis to figure out the dependencies.
1795static Value *getUnderLyingObjectForBrevLdIntr(Value *V) {
1796 Value *IntrBaseVal = V;
1797 Value *BaseVal;
1798 // Loop over till we return the same Value, implies we either figure out
1799 // the object or we hit a PHI
1800 do {
1801 BaseVal = V;
1802 V = getBrevLdObject(V);
1803 } while (BaseVal != V);
1804
1805 // Identify the object from PHINode.
1806 if (const PHINode *PN = dyn_cast<PHINode>(V))
1807 return returnEdge(PN, IntrBaseVal);
1808 // For non PHI nodes, the object is the last value returned by getBrevLdObject
1809 else
1810 return V;
1811}
1812
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001813/// Given an intrinsic, checks if on the target the intrinsic will need to map
1814/// to a MemIntrinsicNode (touches memory). If this is the case, it returns
1815/// true and store the intrinsic information into the IntrinsicInfo that was
1816/// passed to the function.
1817bool HexagonTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
1818 const CallInst &I,
Matt Arsenault7d7adf42017-12-14 22:34:10 +00001819 MachineFunction &MF,
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001820 unsigned Intrinsic) const {
1821 switch (Intrinsic) {
Krzysztof Parzyszekdc7a5572018-03-29 13:52:46 +00001822 case Intrinsic::hexagon_L2_loadrd_pbr:
1823 case Intrinsic::hexagon_L2_loadri_pbr:
1824 case Intrinsic::hexagon_L2_loadrh_pbr:
1825 case Intrinsic::hexagon_L2_loadruh_pbr:
1826 case Intrinsic::hexagon_L2_loadrb_pbr:
1827 case Intrinsic::hexagon_L2_loadrub_pbr: {
1828 Info.opc = ISD::INTRINSIC_W_CHAIN;
1829 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
1830 auto &Cont = I.getCalledFunction()->getParent()->getContext();
1831 // The intrinsic function call is of the form { ElTy, i8* }
1832 // @llvm.hexagon.L2.loadXX.pbr(i8*, i32). The pointer and memory access type
1833 // should be derived from ElTy.
1834 PointerType *PtrTy = I.getCalledFunction()
1835 ->getReturnType()
1836 ->getContainedType(0)
1837 ->getPointerTo();
1838 Info.memVT = MVT::getVT(PtrTy->getElementType());
1839 llvm::Value *BasePtrVal = I.getOperand(0);
1840 Info.ptrVal = getUnderLyingObjectForBrevLdIntr(BasePtrVal);
1841 // The offset value comes through Modifier register. For now, assume the
1842 // offset is 0.
1843 Info.offset = 0;
1844 Info.align = DL.getABITypeAlignment(Info.memVT.getTypeForEVT(Cont));
1845 Info.flags = MachineMemOperand::MOLoad;
1846 return true;
1847 }
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001848 case Intrinsic::hexagon_V6_vgathermw:
1849 case Intrinsic::hexagon_V6_vgathermw_128B:
1850 case Intrinsic::hexagon_V6_vgathermh:
1851 case Intrinsic::hexagon_V6_vgathermh_128B:
1852 case Intrinsic::hexagon_V6_vgathermhw:
1853 case Intrinsic::hexagon_V6_vgathermhw_128B:
1854 case Intrinsic::hexagon_V6_vgathermwq:
1855 case Intrinsic::hexagon_V6_vgathermwq_128B:
1856 case Intrinsic::hexagon_V6_vgathermhq:
1857 case Intrinsic::hexagon_V6_vgathermhq_128B:
1858 case Intrinsic::hexagon_V6_vgathermhwq:
1859 case Intrinsic::hexagon_V6_vgathermhwq_128B: {
1860 const Module &M = *I.getParent()->getParent()->getParent();
1861 Info.opc = ISD::INTRINSIC_W_CHAIN;
1862 Type *VecTy = I.getArgOperand(1)->getType();
1863 Info.memVT = MVT::getVT(VecTy);
1864 Info.ptrVal = I.getArgOperand(0);
1865 Info.offset = 0;
1866 Info.align = M.getDataLayout().getTypeAllocSizeInBits(VecTy) / 8;
Matt Arsenault11171332017-12-14 21:39:51 +00001867 Info.flags = MachineMemOperand::MOLoad |
1868 MachineMemOperand::MOStore |
1869 MachineMemOperand::MOVolatile;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001870 return true;
1871 }
1872 default:
1873 break;
1874 }
1875 return false;
1876}
1877
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001878bool HexagonTargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00001879 return isTruncateFree(EVT::getEVT(Ty1), EVT::getEVT(Ty2));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001880}
1881
1882bool HexagonTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001883 if (!VT1.isSimple() || !VT2.isSimple())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001884 return false;
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00001885 return VT1.getSimpleVT() == MVT::i64 && VT2.getSimpleVT() == MVT::i32;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001886}
1887
Krzysztof Parzyszekbd8ef4b2016-08-19 13:34:31 +00001888bool HexagonTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
1889 return isOperationLegalOrCustom(ISD::FMA, VT);
1890}
1891
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001892// Should we expand the build vector with shuffles?
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00001893bool HexagonTargetLowering::shouldExpandBuildVectorWithShuffles(EVT VT,
1894 unsigned DefinedValues) const {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001895 return false;
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00001896}
1897
Zvi Rackover1b736822017-07-26 08:06:58 +00001898bool HexagonTargetLowering::isShuffleMaskLegal(ArrayRef<int> Mask,
1899 EVT VT) const {
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00001900 return true;
1901}
1902
Krzysztof Parzyszek5439a702017-12-18 18:21:01 +00001903TargetLoweringBase::LegalizeTypeAction
1904HexagonTargetLowering::getPreferredVectorAction(EVT VT) const {
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00001905 if (VT.getVectorNumElements() == 1)
1906 return TargetLoweringBase::TypeScalarizeVector;
1907
1908 // Always widen vectors of i1.
1909 MVT ElemTy = VT.getSimpleVT().getVectorElementType();
1910 if (ElemTy == MVT::i1)
1911 return TargetLoweringBase::TypeWidenVector;
1912
Krzysztof Parzyszek5439a702017-12-18 18:21:01 +00001913 if (Subtarget.useHVXOps()) {
1914 // If the size of VT is at least half of the vector length,
1915 // widen the vector. Note: the threshold was not selected in
1916 // any scientific way.
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00001917 ArrayRef<MVT> Tys = Subtarget.getHVXElementTypes();
1918 if (llvm::find(Tys, ElemTy) != Tys.end()) {
1919 unsigned HwWidth = 8*Subtarget.getVectorLength();
1920 unsigned VecWidth = VT.getSizeInBits();
1921 if (VecWidth >= HwWidth/2 && VecWidth < HwWidth)
1922 return TargetLoweringBase::TypeWidenVector;
1923 }
Krzysztof Parzyszek5439a702017-12-18 18:21:01 +00001924 }
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00001925 return TargetLoweringBase::TypeSplitVector;
Krzysztof Parzyszek5439a702017-12-18 18:21:01 +00001926}
1927
Krzysztof Parzyszekad83ce42018-02-14 20:46:06 +00001928std::pair<SDValue, int>
1929HexagonTargetLowering::getBaseAndOffset(SDValue Addr) const {
1930 if (Addr.getOpcode() == ISD::ADD) {
1931 SDValue Op1 = Addr.getOperand(1);
1932 if (auto *CN = dyn_cast<const ConstantSDNode>(Op1.getNode()))
1933 return { Addr.getOperand(0), CN->getSExtValue() };
1934 }
1935 return { Addr, 0 };
1936}
1937
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00001938// Lower a vector shuffle (V1, V2, V3). V1 and V2 are the two vectors
1939// to select data from, V3 is the permutation.
1940SDValue
1941HexagonTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG)
1942 const {
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001943 const auto *SVN = cast<ShuffleVectorSDNode>(Op);
1944 ArrayRef<int> AM = SVN->getMask();
1945 assert(AM.size() <= 8 && "Unexpected shuffle mask");
1946 unsigned VecLen = AM.size();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001947
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001948 MVT VecTy = ty(Op);
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00001949 assert(!Subtarget.isHVXVectorType(VecTy, true) &&
1950 "HVX shuffles should be legal");
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001951 assert(VecTy.getSizeInBits() <= 64 && "Unexpected vector length");
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001952
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001953 SDValue Op0 = Op.getOperand(0);
1954 SDValue Op1 = Op.getOperand(1);
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +00001955 const SDLoc &dl(Op);
1956
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001957 // If the inputs are not the same as the output, bail. This is not an
1958 // error situation, but complicates the handling and the default expansion
1959 // (into BUILD_VECTOR) should be adequate.
1960 if (ty(Op0) != VecTy || ty(Op1) != VecTy)
1961 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001962
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001963 // Normalize the mask so that the first non-negative index comes from
1964 // the first operand.
1965 SmallVector<int,8> Mask(AM.begin(), AM.end());
1966 unsigned F = llvm::find_if(AM, [](int M) { return M >= 0; }) - AM.data();
1967 if (F == AM.size())
1968 return DAG.getUNDEF(VecTy);
1969 if (AM[F] >= int(VecLen)) {
1970 ShuffleVectorSDNode::commuteMask(Mask);
1971 std::swap(Op0, Op1);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001972 }
1973
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001974 // Express the shuffle mask in terms of bytes.
1975 SmallVector<int,8> ByteMask;
1976 unsigned ElemBytes = VecTy.getVectorElementType().getSizeInBits() / 8;
1977 for (unsigned i = 0, e = Mask.size(); i != e; ++i) {
1978 int M = Mask[i];
1979 if (M < 0) {
1980 for (unsigned j = 0; j != ElemBytes; ++j)
1981 ByteMask.push_back(-1);
1982 } else {
1983 for (unsigned j = 0; j != ElemBytes; ++j)
1984 ByteMask.push_back(M*ElemBytes + j);
1985 }
1986 }
1987 assert(ByteMask.size() <= 8);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001988
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001989 // All non-undef (non-negative) indexes are well within [0..127], so they
1990 // fit in a single byte. Build two 64-bit words:
1991 // - MaskIdx where each byte is the corresponding index (for non-negative
1992 // indexes), and 0xFF for negative indexes, and
1993 // - MaskUnd that has 0xFF for each negative index.
1994 uint64_t MaskIdx = 0;
1995 uint64_t MaskUnd = 0;
1996 for (unsigned i = 0, e = ByteMask.size(); i != e; ++i) {
1997 unsigned S = 8*i;
1998 uint64_t M = ByteMask[i] & 0xFF;
1999 if (M == 0xFF)
2000 MaskUnd |= M << S;
2001 MaskIdx |= M << S;
2002 }
2003
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00002004 if (ByteMask.size() == 4) {
2005 // Identity.
2006 if (MaskIdx == (0x03020100 | MaskUnd))
2007 return Op0;
2008 // Byte swap.
2009 if (MaskIdx == (0x00010203 | MaskUnd)) {
2010 SDValue T0 = DAG.getBitcast(MVT::i32, Op0);
2011 SDValue T1 = DAG.getNode(ISD::BSWAP, dl, MVT::i32, T0);
2012 return DAG.getBitcast(VecTy, T1);
2013 }
2014
2015 // Byte packs.
2016 SDValue Concat10 = DAG.getNode(HexagonISD::COMBINE, dl,
2017 typeJoin({ty(Op1), ty(Op0)}), {Op1, Op0});
2018 if (MaskIdx == (0x06040200 | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002019 return getInstr(Hexagon::S2_vtrunehb, dl, VecTy, {Concat10}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00002020 if (MaskIdx == (0x07050301 | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002021 return getInstr(Hexagon::S2_vtrunohb, dl, VecTy, {Concat10}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00002022
2023 SDValue Concat01 = DAG.getNode(HexagonISD::COMBINE, dl,
2024 typeJoin({ty(Op0), ty(Op1)}), {Op0, Op1});
2025 if (MaskIdx == (0x02000604 | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002026 return getInstr(Hexagon::S2_vtrunehb, dl, VecTy, {Concat01}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00002027 if (MaskIdx == (0x03010705 | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002028 return getInstr(Hexagon::S2_vtrunohb, dl, VecTy, {Concat01}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00002029 }
2030
2031 if (ByteMask.size() == 8) {
2032 // Identity.
2033 if (MaskIdx == (0x0706050403020100ull | MaskUnd))
2034 return Op0;
2035 // Byte swap.
2036 if (MaskIdx == (0x0001020304050607ull | MaskUnd)) {
2037 SDValue T0 = DAG.getBitcast(MVT::i64, Op0);
2038 SDValue T1 = DAG.getNode(ISD::BSWAP, dl, MVT::i64, T0);
2039 return DAG.getBitcast(VecTy, T1);
2040 }
2041
2042 // Halfword picks.
2043 if (MaskIdx == (0x0d0c050409080100ull | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002044 return getInstr(Hexagon::S2_shuffeh, dl, VecTy, {Op1, Op0}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00002045 if (MaskIdx == (0x0f0e07060b0a0302ull | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002046 return getInstr(Hexagon::S2_shuffoh, dl, VecTy, {Op1, Op0}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00002047 if (MaskIdx == (0x0d0c090805040100ull | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002048 return getInstr(Hexagon::S2_vtrunewh, dl, VecTy, {Op1, Op0}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00002049 if (MaskIdx == (0x0f0e0b0a07060302ull | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002050 return getInstr(Hexagon::S2_vtrunowh, dl, VecTy, {Op1, Op0}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00002051 if (MaskIdx == (0x0706030205040100ull | MaskUnd)) {
2052 VectorPair P = opSplit(Op0, dl, DAG);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002053 return getInstr(Hexagon::S2_packhl, dl, VecTy, {P.second, P.first}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00002054 }
2055
2056 // Byte packs.
2057 if (MaskIdx == (0x0e060c040a020800ull | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002058 return getInstr(Hexagon::S2_shuffeb, dl, VecTy, {Op1, Op0}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00002059 if (MaskIdx == (0x0f070d050b030901ull | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002060 return getInstr(Hexagon::S2_shuffob, dl, VecTy, {Op1, Op0}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00002061 }
2062
2063 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002064}
2065
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00002066// Create a Hexagon-specific node for shifting a vector by an integer.
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002067SDValue
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00002068HexagonTargetLowering::getVectorShiftByInt(SDValue Op, SelectionDAG &DAG)
2069 const {
Krzysztof Parzyszek1108ee22018-01-31 20:49:24 +00002070 if (auto *BVN = dyn_cast<BuildVectorSDNode>(Op.getOperand(1).getNode())) {
2071 if (SDValue S = BVN->getSplatValue()) {
2072 unsigned NewOpc;
2073 switch (Op.getOpcode()) {
2074 case ISD::SHL:
2075 NewOpc = HexagonISD::VASL;
2076 break;
2077 case ISD::SRA:
2078 NewOpc = HexagonISD::VASR;
2079 break;
2080 case ISD::SRL:
2081 NewOpc = HexagonISD::VLSR;
2082 break;
2083 default:
2084 llvm_unreachable("Unexpected shift opcode");
2085 }
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00002086 return DAG.getNode(NewOpc, SDLoc(Op), ty(Op), Op.getOperand(0), S);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002087 }
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002088 }
2089
Krzysztof Parzyszek1108ee22018-01-31 20:49:24 +00002090 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002091}
2092
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002093SDValue
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00002094HexagonTargetLowering::LowerVECTOR_SHIFT(SDValue Op, SelectionDAG &DAG) const {
2095 return getVectorShiftByInt(Op, DAG);
2096}
2097
2098SDValue
Krzysztof Parzyszek3d671242018-06-12 12:49:36 +00002099HexagonTargetLowering::LowerROTL(SDValue Op, SelectionDAG &DAG) const {
2100 if (isa<ConstantSDNode>(Op.getOperand(1).getNode()))
2101 return Op;
2102 return SDValue();
2103}
2104
2105SDValue
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002106HexagonTargetLowering::LowerBITCAST(SDValue Op, SelectionDAG &DAG) const {
2107 MVT ResTy = ty(Op);
2108 SDValue InpV = Op.getOperand(0);
2109 MVT InpTy = ty(InpV);
2110 assert(ResTy.getSizeInBits() == InpTy.getSizeInBits());
2111 const SDLoc &dl(Op);
2112
2113 // Handle conversion from i8 to v8i1.
2114 if (ResTy == MVT::v8i1) {
2115 SDValue Sc = DAG.getBitcast(tyScalar(InpTy), InpV);
2116 SDValue Ext = DAG.getZExtOrTrunc(Sc, dl, MVT::i32);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002117 return getInstr(Hexagon::C2_tfrrp, dl, ResTy, Ext, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002118 }
2119
2120 return SDValue();
2121}
2122
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002123bool
2124HexagonTargetLowering::getBuildVectorConstInts(ArrayRef<SDValue> Values,
2125 MVT VecTy, SelectionDAG &DAG,
2126 MutableArrayRef<ConstantInt*> Consts) const {
2127 MVT ElemTy = VecTy.getVectorElementType();
2128 unsigned ElemWidth = ElemTy.getSizeInBits();
2129 IntegerType *IntTy = IntegerType::get(*DAG.getContext(), ElemWidth);
2130 bool AllConst = true;
2131
2132 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
2133 SDValue V = Values[i];
2134 if (V.isUndef()) {
2135 Consts[i] = ConstantInt::get(IntTy, 0);
2136 continue;
2137 }
Krzysztof Parzyszek4ef6cff2018-01-11 18:03:23 +00002138 // Make sure to always cast to IntTy.
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002139 if (auto *CN = dyn_cast<ConstantSDNode>(V.getNode())) {
2140 const ConstantInt *CI = CN->getConstantIntValue();
Krzysztof Parzyszek4ef6cff2018-01-11 18:03:23 +00002141 Consts[i] = ConstantInt::get(IntTy, CI->getValue().getSExtValue());
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002142 } else if (auto *CN = dyn_cast<ConstantFPSDNode>(V.getNode())) {
2143 const ConstantFP *CF = CN->getConstantFPValue();
2144 APInt A = CF->getValueAPF().bitcastToAPInt();
2145 Consts[i] = ConstantInt::get(IntTy, A.getZExtValue());
2146 } else {
2147 AllConst = false;
2148 }
2149 }
2150 return AllConst;
2151}
2152
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002153SDValue
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002154HexagonTargetLowering::buildVector32(ArrayRef<SDValue> Elem, const SDLoc &dl,
2155 MVT VecTy, SelectionDAG &DAG) const {
2156 MVT ElemTy = VecTy.getVectorElementType();
2157 assert(VecTy.getVectorNumElements() == Elem.size());
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002158
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002159 SmallVector<ConstantInt*,4> Consts(Elem.size());
2160 bool AllConst = getBuildVectorConstInts(Elem, VecTy, DAG, Consts);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002161
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002162 unsigned First, Num = Elem.size();
2163 for (First = 0; First != Num; ++First)
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002164 if (!isUndef(Elem[First]))
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002165 break;
2166 if (First == Num)
2167 return DAG.getUNDEF(VecTy);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002168
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002169 if (AllConst &&
2170 llvm::all_of(Consts, [](ConstantInt *CI) { return CI->isZero(); }))
2171 return getZero(dl, VecTy, DAG);
2172
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002173 if (ElemTy == MVT::i16) {
2174 assert(Elem.size() == 2);
2175 if (AllConst) {
2176 uint32_t V = (Consts[0]->getZExtValue() & 0xFFFF) |
2177 Consts[1]->getZExtValue() << 16;
2178 return DAG.getBitcast(MVT::v2i16, DAG.getConstant(V, dl, MVT::i32));
Krzysztof Parzyszek89b2d7c2017-07-13 18:17:58 +00002179 }
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002180 SDValue N = getInstr(Hexagon::A2_combine_ll, dl, MVT::i32,
2181 {Elem[1], Elem[0]}, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002182 return DAG.getBitcast(MVT::v2i16, N);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002183 }
2184
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002185 if (ElemTy == MVT::i8) {
2186 // First try generating a constant.
2187 if (AllConst) {
2188 int32_t V = (Consts[0]->getZExtValue() & 0xFF) |
2189 (Consts[1]->getZExtValue() & 0xFF) << 8 |
2190 (Consts[1]->getZExtValue() & 0xFF) << 16 |
2191 Consts[2]->getZExtValue() << 24;
2192 return DAG.getBitcast(MVT::v4i8, DAG.getConstant(V, dl, MVT::i32));
2193 }
2194
2195 // Then try splat.
2196 bool IsSplat = true;
2197 for (unsigned i = 0; i != Num; ++i) {
2198 if (i == First)
2199 continue;
2200 if (Elem[i] == Elem[First] || isUndef(Elem[i]))
2201 continue;
2202 IsSplat = false;
2203 break;
2204 }
2205 if (IsSplat) {
2206 // Legalize the operand to VSPLAT.
2207 SDValue Ext = DAG.getZExtOrTrunc(Elem[First], dl, MVT::i32);
2208 return DAG.getNode(HexagonISD::VSPLAT, dl, VecTy, Ext);
2209 }
2210
2211 // Generate
2212 // (zxtb(Elem[0]) | (zxtb(Elem[1]) << 8)) |
2213 // (zxtb(Elem[2]) | (zxtb(Elem[3]) << 8)) << 16
2214 assert(Elem.size() == 4);
2215 SDValue Vs[4];
2216 for (unsigned i = 0; i != 4; ++i) {
2217 Vs[i] = DAG.getZExtOrTrunc(Elem[i], dl, MVT::i32);
2218 Vs[i] = DAG.getZeroExtendInReg(Vs[i], dl, MVT::i8);
2219 }
2220 SDValue S8 = DAG.getConstant(8, dl, MVT::i32);
2221 SDValue T0 = DAG.getNode(ISD::SHL, dl, MVT::i32, {Vs[1], S8});
2222 SDValue T1 = DAG.getNode(ISD::SHL, dl, MVT::i32, {Vs[3], S8});
2223 SDValue B0 = DAG.getNode(ISD::OR, dl, MVT::i32, {Vs[0], T0});
2224 SDValue B1 = DAG.getNode(ISD::OR, dl, MVT::i32, {Vs[2], T1});
2225
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002226 SDValue R = getInstr(Hexagon::A2_combine_ll, dl, MVT::i32, {B1, B0}, DAG);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002227 return DAG.getBitcast(MVT::v4i8, R);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002228 }
2229
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002230#ifndef NDEBUG
2231 dbgs() << "VecTy: " << EVT(VecTy).getEVTString() << '\n';
2232#endif
2233 llvm_unreachable("Unexpected vector element type");
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002234}
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002235
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002236SDValue
2237HexagonTargetLowering::buildVector64(ArrayRef<SDValue> Elem, const SDLoc &dl,
2238 MVT VecTy, SelectionDAG &DAG) const {
2239 MVT ElemTy = VecTy.getVectorElementType();
2240 assert(VecTy.getVectorNumElements() == Elem.size());
2241
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002242 SmallVector<ConstantInt*,8> Consts(Elem.size());
2243 bool AllConst = getBuildVectorConstInts(Elem, VecTy, DAG, Consts);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002244
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002245 unsigned First, Num = Elem.size();
2246 for (First = 0; First != Num; ++First)
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002247 if (!isUndef(Elem[First]))
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002248 break;
2249 if (First == Num)
2250 return DAG.getUNDEF(VecTy);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002251
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002252 if (AllConst &&
2253 llvm::all_of(Consts, [](ConstantInt *CI) { return CI->isZero(); }))
2254 return getZero(dl, VecTy, DAG);
2255
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002256 // First try splat if possible.
2257 if (ElemTy == MVT::i16) {
2258 bool IsSplat = true;
2259 for (unsigned i = 0; i != Num; ++i) {
2260 if (i == First)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002261 continue;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002262 if (Elem[i] == Elem[First] || isUndef(Elem[i]))
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002263 continue;
2264 IsSplat = false;
2265 break;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002266 }
Krzysztof Parzyszekfb0fcac2017-12-20 20:33:49 +00002267 if (IsSplat) {
2268 // Legalize the operand to VSPLAT.
2269 SDValue Ext = DAG.getZExtOrTrunc(Elem[First], dl, MVT::i32);
2270 return DAG.getNode(HexagonISD::VSPLAT, dl, VecTy, Ext);
2271 }
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002272 }
2273
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002274 // Then try constant.
2275 if (AllConst) {
2276 uint64_t Val = 0;
2277 unsigned W = ElemTy.getSizeInBits();
2278 uint64_t Mask = (ElemTy == MVT::i8) ? 0xFFull
2279 : (ElemTy == MVT::i16) ? 0xFFFFull : 0xFFFFFFFFull;
2280 for (unsigned i = 0; i != Num; ++i)
Krzysztof Parzyszek240df6f2018-01-11 18:30:41 +00002281 Val = (Val << W) | (Consts[Num-1-i]->getZExtValue() & Mask);
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002282 SDValue V0 = DAG.getConstant(Val, dl, MVT::i64);
2283 return DAG.getBitcast(VecTy, V0);
2284 }
2285
2286 // Build two 32-bit vectors and concatenate.
2287 MVT HalfTy = MVT::getVectorVT(ElemTy, Num/2);
2288 SDValue L = (ElemTy == MVT::i32)
2289 ? Elem[0]
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002290 : buildVector32(Elem.take_front(Num/2), dl, HalfTy, DAG);
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002291 SDValue H = (ElemTy == MVT::i32)
2292 ? Elem[1]
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002293 : buildVector32(Elem.drop_front(Num/2), dl, HalfTy, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002294 return DAG.getNode(HexagonISD::COMBINE, dl, VecTy, {H, L});
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002295}
2296
2297SDValue
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002298HexagonTargetLowering::extractVector(SDValue VecV, SDValue IdxV,
2299 const SDLoc &dl, MVT ValTy, MVT ResTy,
2300 SelectionDAG &DAG) const {
2301 MVT VecTy = ty(VecV);
2302 assert(!ValTy.isVector() ||
2303 VecTy.getVectorElementType() == ValTy.getVectorElementType());
2304 unsigned VecWidth = VecTy.getSizeInBits();
2305 unsigned ValWidth = ValTy.getSizeInBits();
2306 unsigned ElemWidth = VecTy.getVectorElementType().getSizeInBits();
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002307 assert((VecWidth % ElemWidth) == 0);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002308 auto *IdxN = dyn_cast<ConstantSDNode>(IdxV);
2309
2310 // Special case for v{8,4,2}i1 (the only boolean vectors legal in Hexagon
2311 // without any coprocessors).
2312 if (ElemWidth == 1) {
2313 assert(VecWidth == VecTy.getVectorNumElements() && "Sanity failure");
2314 assert(VecWidth == 8 || VecWidth == 4 || VecWidth == 2);
2315 // Check if this is an extract of the lowest bit.
2316 if (IdxN) {
2317 // Extracting the lowest bit is a no-op, but it changes the type,
2318 // so it must be kept as an operation to avoid errors related to
2319 // type mismatches.
2320 if (IdxN->isNullValue() && ValTy.getSizeInBits() == 1)
2321 return DAG.getNode(HexagonISD::TYPECAST, dl, MVT::i1, VecV);
2322 }
2323
2324 // If the value extracted is a single bit, use tstbit.
2325 if (ValWidth == 1) {
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002326 SDValue A0 = getInstr(Hexagon::C2_tfrpr, dl, MVT::i32, {VecV}, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002327 return DAG.getNode(HexagonISD::TSTBIT, dl, MVT::i1, A0, IdxV);
2328 }
2329
2330 // Each bool vector (v2i1, v4i1, v8i1) always occupies 8 bits in
2331 // a predicate register. The elements of the vector are repeated
2332 // in the register (if necessary) so that the total number is 8.
2333 // The extracted subvector will need to be expanded in such a way.
2334 unsigned Scale = VecWidth / ValWidth;
2335
2336 // Generate (p2d VecV) >> 8*Idx to move the interesting bytes to
2337 // position 0.
2338 assert(ty(IdxV) == MVT::i32);
2339 SDValue S0 = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV,
Krzysztof Parzyszek99152912018-03-16 15:03:37 +00002340 DAG.getConstant(8*Scale, dl, MVT::i32));
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002341 SDValue T0 = DAG.getNode(HexagonISD::P2D, dl, MVT::i64, VecV);
2342 SDValue T1 = DAG.getNode(ISD::SRL, dl, MVT::i64, T0, S0);
2343 while (Scale > 1) {
2344 // The longest possible subvector is at most 32 bits, so it is always
2345 // contained in the low subregister.
2346 T1 = DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::i32, T1);
2347 T1 = expandPredicate(T1, dl, DAG);
2348 Scale /= 2;
2349 }
2350
2351 return DAG.getNode(HexagonISD::D2P, dl, ResTy, T1);
2352 }
2353
2354 assert(VecWidth == 32 || VecWidth == 64);
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002355
2356 // Cast everything to scalar integer types.
2357 MVT ScalarTy = tyScalar(VecTy);
2358 VecV = DAG.getBitcast(ScalarTy, VecV);
2359
2360 SDValue WidthV = DAG.getConstant(ValWidth, dl, MVT::i32);
2361 SDValue ExtV;
2362
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002363 if (IdxN) {
2364 unsigned Off = IdxN->getZExtValue() * ElemWidth;
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002365 if (VecWidth == 64 && ValWidth == 32) {
2366 assert(Off == 0 || Off == 32);
2367 unsigned SubIdx = Off == 0 ? Hexagon::isub_lo : Hexagon::isub_hi;
2368 ExtV = DAG.getTargetExtractSubreg(SubIdx, dl, MVT::i32, VecV);
2369 } else if (Off == 0 && (ValWidth % 8) == 0) {
2370 ExtV = DAG.getZeroExtendInReg(VecV, dl, tyScalar(ValTy));
2371 } else {
2372 SDValue OffV = DAG.getConstant(Off, dl, MVT::i32);
2373 // The return type of EXTRACTU must be the same as the type of the
2374 // input vector.
2375 ExtV = DAG.getNode(HexagonISD::EXTRACTU, dl, ScalarTy,
2376 {VecV, WidthV, OffV});
2377 }
2378 } else {
2379 if (ty(IdxV) != MVT::i32)
2380 IdxV = DAG.getZExtOrTrunc(IdxV, dl, MVT::i32);
2381 SDValue OffV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV,
2382 DAG.getConstant(ElemWidth, dl, MVT::i32));
Krzysztof Parzyszekb1b29602018-01-04 13:56:04 +00002383 ExtV = DAG.getNode(HexagonISD::EXTRACTU, dl, ScalarTy,
2384 {VecV, WidthV, OffV});
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002385 }
2386
2387 // Cast ExtV to the requested result type.
2388 ExtV = DAG.getZExtOrTrunc(ExtV, dl, tyScalar(ResTy));
2389 ExtV = DAG.getBitcast(ResTy, ExtV);
2390 return ExtV;
2391}
2392
2393SDValue
2394HexagonTargetLowering::insertVector(SDValue VecV, SDValue ValV, SDValue IdxV,
2395 const SDLoc &dl, MVT ValTy,
2396 SelectionDAG &DAG) const {
2397 MVT VecTy = ty(VecV);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002398 if (VecTy.getVectorElementType() == MVT::i1) {
2399 MVT ValTy = ty(ValV);
2400 assert(ValTy.getVectorElementType() == MVT::i1);
2401 SDValue ValR = DAG.getNode(HexagonISD::P2D, dl, MVT::i64, ValV);
2402 unsigned VecLen = VecTy.getVectorNumElements();
2403 unsigned Scale = VecLen / ValTy.getVectorNumElements();
2404 assert(Scale > 1);
2405
2406 for (unsigned R = Scale; R > 1; R /= 2) {
2407 ValR = contractPredicate(ValR, dl, DAG);
2408 ValR = DAG.getNode(HexagonISD::COMBINE, dl, MVT::i64,
2409 DAG.getUNDEF(MVT::i32), ValR);
2410 }
2411 // The longest possible subvector is at most 32 bits, so it is always
2412 // contained in the low subregister.
2413 ValR = DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::i32, ValR);
2414
2415 unsigned ValBytes = 64 / Scale;
2416 SDValue Width = DAG.getConstant(ValBytes*8, dl, MVT::i32);
2417 SDValue Idx = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV,
2418 DAG.getConstant(8, dl, MVT::i32));
2419 SDValue VecR = DAG.getNode(HexagonISD::P2D, dl, MVT::i64, VecV);
2420 SDValue Ins = DAG.getNode(HexagonISD::INSERT, dl, MVT::i32,
2421 {VecR, ValR, Width, Idx});
2422 return DAG.getNode(HexagonISD::D2P, dl, VecTy, Ins);
2423 }
2424
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002425 unsigned VecWidth = VecTy.getSizeInBits();
2426 unsigned ValWidth = ValTy.getSizeInBits();
2427 assert(VecWidth == 32 || VecWidth == 64);
2428 assert((VecWidth % ValWidth) == 0);
2429
2430 // Cast everything to scalar integer types.
2431 MVT ScalarTy = MVT::getIntegerVT(VecWidth);
2432 // The actual type of ValV may be different than ValTy (which is related
2433 // to the vector type).
2434 unsigned VW = ty(ValV).getSizeInBits();
2435 ValV = DAG.getBitcast(MVT::getIntegerVT(VW), ValV);
2436 VecV = DAG.getBitcast(ScalarTy, VecV);
2437 if (VW != VecWidth)
2438 ValV = DAG.getAnyExtOrTrunc(ValV, dl, ScalarTy);
2439
2440 SDValue WidthV = DAG.getConstant(ValWidth, dl, MVT::i32);
2441 SDValue InsV;
2442
2443 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(IdxV)) {
2444 unsigned W = C->getZExtValue() * ValWidth;
2445 SDValue OffV = DAG.getConstant(W, dl, MVT::i32);
2446 InsV = DAG.getNode(HexagonISD::INSERT, dl, ScalarTy,
2447 {VecV, ValV, WidthV, OffV});
2448 } else {
2449 if (ty(IdxV) != MVT::i32)
2450 IdxV = DAG.getZExtOrTrunc(IdxV, dl, MVT::i32);
2451 SDValue OffV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV, WidthV);
Krzysztof Parzyszekb1b29602018-01-04 13:56:04 +00002452 InsV = DAG.getNode(HexagonISD::INSERT, dl, ScalarTy,
2453 {VecV, ValV, WidthV, OffV});
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002454 }
2455
2456 return DAG.getNode(ISD::BITCAST, dl, VecTy, InsV);
2457}
2458
2459SDValue
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002460HexagonTargetLowering::expandPredicate(SDValue Vec32, const SDLoc &dl,
2461 SelectionDAG &DAG) const {
2462 assert(ty(Vec32).getSizeInBits() == 32);
2463 if (isUndef(Vec32))
2464 return DAG.getUNDEF(MVT::i64);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002465 return getInstr(Hexagon::S2_vsxtbh, dl, MVT::i64, {Vec32}, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002466}
2467
2468SDValue
2469HexagonTargetLowering::contractPredicate(SDValue Vec64, const SDLoc &dl,
2470 SelectionDAG &DAG) const {
2471 assert(ty(Vec64).getSizeInBits() == 64);
2472 if (isUndef(Vec64))
2473 return DAG.getUNDEF(MVT::i32);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002474 return getInstr(Hexagon::S2_vtrunehb, dl, MVT::i32, {Vec64}, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002475}
2476
2477SDValue
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002478HexagonTargetLowering::getZero(const SDLoc &dl, MVT Ty, SelectionDAG &DAG)
2479 const {
2480 if (Ty.isVector()) {
2481 assert(Ty.isInteger() && "Only integer vectors are supported here");
2482 unsigned W = Ty.getSizeInBits();
2483 if (W <= 64)
2484 return DAG.getBitcast(Ty, DAG.getConstant(0, dl, MVT::getIntegerVT(W)));
2485 return DAG.getNode(HexagonISD::VZERO, dl, Ty);
2486 }
2487
2488 if (Ty.isInteger())
2489 return DAG.getConstant(0, dl, Ty);
2490 if (Ty.isFloatingPoint())
2491 return DAG.getConstantFP(0.0, dl, Ty);
2492 llvm_unreachable("Invalid type for zero");
2493}
2494
2495SDValue
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002496HexagonTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002497 MVT VecTy = ty(Op);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002498 unsigned BW = VecTy.getSizeInBits();
2499 const SDLoc &dl(Op);
2500 SmallVector<SDValue,8> Ops;
2501 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i)
2502 Ops.push_back(Op.getOperand(i));
2503
2504 if (BW == 32)
2505 return buildVector32(Ops, dl, VecTy, DAG);
2506 if (BW == 64)
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002507 return buildVector64(Ops, dl, VecTy, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002508
2509 if (VecTy == MVT::v8i1 || VecTy == MVT::v4i1 || VecTy == MVT::v2i1) {
2510 // For each i1 element in the resulting predicate register, put 1
2511 // shifted by the index of the element into a general-purpose register,
2512 // then or them together and transfer it back into a predicate register.
2513 SDValue Rs[8];
2514 SDValue Z = getZero(dl, MVT::i32, DAG);
2515 // Always produce 8 bits, repeat inputs if necessary.
2516 unsigned Rep = 8 / VecTy.getVectorNumElements();
2517 for (unsigned i = 0; i != 8; ++i) {
Simon Pilgrimc1e22902018-01-23 21:22:16 +00002518 SDValue S = DAG.getConstant(1ull << i, dl, MVT::i32);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002519 Rs[i] = DAG.getSelect(dl, MVT::i32, Ops[i/Rep], S, Z);
2520 }
2521 for (ArrayRef<SDValue> A(Rs); A.size() != 1; A = A.drop_back(A.size()/2)) {
2522 for (unsigned i = 0, e = A.size()/2; i != e; ++i)
2523 Rs[i] = DAG.getNode(ISD::OR, dl, MVT::i32, Rs[2*i], Rs[2*i+1]);
2524 }
2525 // Move the value directly to a predicate register.
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002526 return getInstr(Hexagon::C2_tfrrp, dl, VecTy, {Rs[0]}, DAG);
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002527 }
2528
2529 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002530}
2531
2532SDValue
2533HexagonTargetLowering::LowerCONCAT_VECTORS(SDValue Op,
2534 SelectionDAG &DAG) const {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002535 MVT VecTy = ty(Op);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002536 const SDLoc &dl(Op);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002537 if (VecTy.getSizeInBits() == 64) {
2538 assert(Op.getNumOperands() == 2);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002539 return DAG.getNode(HexagonISD::COMBINE, dl, VecTy, Op.getOperand(1),
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002540 Op.getOperand(0));
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002541 }
2542
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002543 MVT ElemTy = VecTy.getVectorElementType();
2544 if (ElemTy == MVT::i1) {
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002545 assert(VecTy == MVT::v2i1 || VecTy == MVT::v4i1 || VecTy == MVT::v8i1);
2546 MVT OpTy = ty(Op.getOperand(0));
2547 // Scale is how many times the operands need to be contracted to match
2548 // the representation in the target register.
2549 unsigned Scale = VecTy.getVectorNumElements() / OpTy.getVectorNumElements();
2550 assert(Scale == Op.getNumOperands() && Scale > 1);
2551
2552 // First, convert all bool vectors to integers, then generate pairwise
2553 // inserts to form values of doubled length. Up until there are only
2554 // two values left to concatenate, all of these values will fit in a
2555 // 32-bit integer, so keep them as i32 to use 32-bit inserts.
2556 SmallVector<SDValue,4> Words[2];
2557 unsigned IdxW = 0;
2558
2559 for (SDValue P : Op.getNode()->op_values()) {
2560 SDValue W = DAG.getNode(HexagonISD::P2D, dl, MVT::i64, P);
2561 for (unsigned R = Scale; R > 1; R /= 2) {
2562 W = contractPredicate(W, dl, DAG);
2563 W = DAG.getNode(HexagonISD::COMBINE, dl, MVT::i64,
2564 DAG.getUNDEF(MVT::i32), W);
2565 }
2566 W = DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::i32, W);
2567 Words[IdxW].push_back(W);
2568 }
2569
2570 while (Scale > 2) {
2571 SDValue WidthV = DAG.getConstant(64 / Scale, dl, MVT::i32);
2572 Words[IdxW ^ 1].clear();
2573
2574 for (unsigned i = 0, e = Words[IdxW].size(); i != e; i += 2) {
2575 SDValue W0 = Words[IdxW][i], W1 = Words[IdxW][i+1];
2576 // Insert W1 into W0 right next to the significant bits of W0.
2577 SDValue T = DAG.getNode(HexagonISD::INSERT, dl, MVT::i32,
2578 {W0, W1, WidthV, WidthV});
2579 Words[IdxW ^ 1].push_back(T);
2580 }
2581 IdxW ^= 1;
2582 Scale /= 2;
2583 }
2584
2585 // Another sanity check. At this point there should only be two words
2586 // left, and Scale should be 2.
2587 assert(Scale == 2 && Words[IdxW].size() == 2);
2588
2589 SDValue WW = DAG.getNode(HexagonISD::COMBINE, dl, MVT::i64,
2590 Words[IdxW][1], Words[IdxW][0]);
2591 return DAG.getNode(HexagonISD::D2P, dl, VecTy, WW);
2592 }
2593
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002594 return SDValue();
2595}
2596
2597SDValue
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002598HexagonTargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
2599 SelectionDAG &DAG) const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002600 SDValue Vec = Op.getOperand(0);
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002601 MVT ElemTy = ty(Vec).getVectorElementType();
2602 return extractVector(Vec, Op.getOperand(1), SDLoc(Op), ElemTy, ty(Op), DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002603}
2604
2605SDValue
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002606HexagonTargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op,
2607 SelectionDAG &DAG) const {
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00002608 return extractVector(Op.getOperand(0), Op.getOperand(1), SDLoc(Op),
2609 ty(Op), ty(Op), DAG);
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002610}
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002611
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002612SDValue
2613HexagonTargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op,
2614 SelectionDAG &DAG) const {
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002615 return insertVector(Op.getOperand(0), Op.getOperand(1), Op.getOperand(2),
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00002616 SDLoc(Op), ty(Op).getVectorElementType(), DAG);
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002617}
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002618
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002619SDValue
2620HexagonTargetLowering::LowerINSERT_SUBVECTOR(SDValue Op,
2621 SelectionDAG &DAG) const {
2622 SDValue ValV = Op.getOperand(1);
2623 return insertVector(Op.getOperand(0), ValV, Op.getOperand(2),
2624 SDLoc(Op), ty(ValV), DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002625}
2626
Tim Northovera4415852013-08-06 09:12:35 +00002627bool
2628HexagonTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
2629 // Assuming the caller does not have either a signext or zeroext modifier, and
2630 // only one value is accepted, any reasonable truncation is allowed.
2631 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
2632 return false;
2633
2634 // FIXME: in principle up to 64-bit could be made safe, but it would be very
2635 // fragile at the moment: any support for multiple value returns would be
2636 // liable to disallow tail calls involving i64 -> iN truncation in many cases.
2637 return Ty1->getPrimitiveSizeInBits() <= 32;
2638}
2639
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002640SDValue
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002641HexagonTargetLowering::LowerUnalignedLoad(SDValue Op, SelectionDAG &DAG)
2642 const {
2643 LoadSDNode *LN = cast<LoadSDNode>(Op.getNode());
2644 unsigned HaveAlign = LN->getAlignment();
2645 MVT LoadTy = ty(Op);
2646 unsigned NeedAlign = Subtarget.getTypeAlignment(LoadTy);
2647 if (HaveAlign >= NeedAlign)
2648 return Op;
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002649
2650 const SDLoc &dl(Op);
2651 const DataLayout &DL = DAG.getDataLayout();
2652 LLVMContext &Ctx = *DAG.getContext();
2653 unsigned AS = LN->getAddressSpace();
2654
2655 // If the load aligning is disabled or the load can be broken up into two
2656 // smaller legal loads, do the default (target-independent) expansion.
2657 bool DoDefault = false;
Krzysztof Parzyszek480ab2b2018-03-08 18:15:13 +00002658 // Handle it in the default way if this is an indexed load.
2659 if (!LN->isUnindexed())
2660 DoDefault = true;
2661
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002662 if (!AlignLoads) {
2663 if (allowsMemoryAccess(Ctx, DL, LN->getMemoryVT(), AS, HaveAlign))
2664 return Op;
2665 DoDefault = true;
2666 }
2667 if (!DoDefault && 2*HaveAlign == NeedAlign) {
2668 // The PartTy is the equivalent of "getLoadableTypeOfSize(HaveAlign)".
2669 MVT PartTy = HaveAlign <= 8 ? MVT::getIntegerVT(8*HaveAlign)
2670 : MVT::getVectorVT(MVT::i8, HaveAlign);
2671 DoDefault = allowsMemoryAccess(Ctx, DL, PartTy, AS, HaveAlign);
2672 }
2673 if (DoDefault) {
2674 std::pair<SDValue, SDValue> P = expandUnalignedLoad(LN, DAG);
2675 return DAG.getMergeValues({P.first, P.second}, dl);
2676 }
2677
2678 // The code below generates two loads, both aligned as NeedAlign, and
2679 // with the distance of NeedAlign between them. For that to cover the
2680 // bits that need to be loaded (and without overlapping), the size of
2681 // the loads should be equal to NeedAlign. This is true for all loadable
2682 // types, but add an assertion in case something changes in the future.
2683 assert(LoadTy.getSizeInBits() == 8*NeedAlign);
2684
2685 unsigned LoadLen = NeedAlign;
2686 SDValue Base = LN->getBasePtr();
2687 SDValue Chain = LN->getChain();
2688 auto BO = getBaseAndOffset(Base);
2689 unsigned BaseOpc = BO.first.getOpcode();
2690 if (BaseOpc == HexagonISD::VALIGNADDR && BO.second % LoadLen == 0)
2691 return Op;
2692
2693 if (BO.second % LoadLen != 0) {
2694 BO.first = DAG.getNode(ISD::ADD, dl, MVT::i32, BO.first,
2695 DAG.getConstant(BO.second % LoadLen, dl, MVT::i32));
2696 BO.second -= BO.second % LoadLen;
2697 }
2698 SDValue BaseNoOff = (BaseOpc != HexagonISD::VALIGNADDR)
2699 ? DAG.getNode(HexagonISD::VALIGNADDR, dl, MVT::i32, BO.first,
2700 DAG.getConstant(NeedAlign, dl, MVT::i32))
2701 : BO.first;
2702 SDValue Base0 = DAG.getMemBasePlusOffset(BaseNoOff, BO.second, dl);
2703 SDValue Base1 = DAG.getMemBasePlusOffset(BaseNoOff, BO.second+LoadLen, dl);
2704
2705 MachineMemOperand *WideMMO = nullptr;
2706 if (MachineMemOperand *MMO = LN->getMemOperand()) {
2707 MachineFunction &MF = DAG.getMachineFunction();
2708 WideMMO = MF.getMachineMemOperand(MMO->getPointerInfo(), MMO->getFlags(),
2709 2*LoadLen, LoadLen, MMO->getAAInfo(), MMO->getRanges(),
2710 MMO->getSyncScopeID(), MMO->getOrdering(),
2711 MMO->getFailureOrdering());
2712 }
2713
2714 SDValue Load0 = DAG.getLoad(LoadTy, dl, Chain, Base0, WideMMO);
2715 SDValue Load1 = DAG.getLoad(LoadTy, dl, Chain, Base1, WideMMO);
2716
2717 SDValue Aligned = DAG.getNode(HexagonISD::VALIGN, dl, LoadTy,
2718 {Load1, Load0, BaseNoOff.getOperand(0)});
2719 SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2720 Load0.getValue(1), Load1.getValue(1));
2721 SDValue M = DAG.getMergeValues({Aligned, NewChain}, dl);
2722 return M;
2723}
2724
2725SDValue
Krzysztof Parzyszek0b6187c2018-06-01 14:00:32 +00002726HexagonTargetLowering::LowerAddSubCarry(SDValue Op, SelectionDAG &DAG) const {
2727 const SDLoc &dl(Op);
2728 unsigned Opc = Op.getOpcode();
2729 SDValue X = Op.getOperand(0), Y = Op.getOperand(1), C = Op.getOperand(2);
2730
2731 if (Opc == ISD::ADDCARRY)
2732 return DAG.getNode(HexagonISD::ADDC, dl, Op.getNode()->getVTList(),
2733 { X, Y, C });
2734
2735 EVT CarryTy = C.getValueType();
2736 SDValue SubC = DAG.getNode(HexagonISD::SUBC, dl, Op.getNode()->getVTList(),
2737 { X, Y, DAG.getLogicalNOT(dl, C, CarryTy) });
2738 SDValue Out[] = { SubC.getValue(0),
2739 DAG.getLogicalNOT(dl, SubC.getValue(1), CarryTy) };
2740 return DAG.getMergeValues(Out, dl);
2741}
2742
2743SDValue
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002744HexagonTargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
2745 SDValue Chain = Op.getOperand(0);
2746 SDValue Offset = Op.getOperand(1);
2747 SDValue Handler = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002748 SDLoc dl(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00002749 auto PtrVT = getPointerTy(DAG.getDataLayout());
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002750
2751 // Mark function as containing a call to EH_RETURN.
2752 HexagonMachineFunctionInfo *FuncInfo =
2753 DAG.getMachineFunction().getInfo<HexagonMachineFunctionInfo>();
2754 FuncInfo->setHasEHReturn();
2755
2756 unsigned OffsetReg = Hexagon::R28;
2757
Mehdi Amini44ede332015-07-09 02:09:04 +00002758 SDValue StoreAddr =
2759 DAG.getNode(ISD::ADD, dl, PtrVT, DAG.getRegister(Hexagon::R30, PtrVT),
2760 DAG.getIntPtrConstant(4, dl));
Justin Lebar9c375812016-07-15 18:27:10 +00002761 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo());
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002762 Chain = DAG.getCopyToReg(Chain, dl, OffsetReg, Offset);
2763
2764 // Not needed we already use it as explict input to EH_RETURN.
2765 // MF.getRegInfo().addLiveOut(OffsetReg);
2766
2767 return DAG.getNode(HexagonISD::EH_RETURN, dl, MVT::Other, Chain);
2768}
2769
2770SDValue
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002771HexagonTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002772 unsigned Opc = Op.getOpcode();
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00002773
2774 // Handle INLINEASM first.
2775 if (Opc == ISD::INLINEASM)
2776 return LowerINLINEASM(Op, DAG);
2777
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002778 if (isHvxOperation(Op)) {
2779 // If HVX lowering returns nothing, try the default lowering.
2780 if (SDValue V = LowerHvxOperation(Op, DAG))
2781 return V;
2782 }
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00002783
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002784 switch (Opc) {
2785 default:
2786#ifndef NDEBUG
2787 Op.getNode()->dumpr(&DAG);
2788 if (Opc > HexagonISD::OP_BEGIN && Opc < HexagonISD::OP_END)
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002789 errs() << "Error: check for a non-legal type in this operation\n";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002790#endif
2791 llvm_unreachable("Should not custom lower this!");
2792 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002793 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
2794 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
2795 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
2796 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002797 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
2798 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002799 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002800 case ISD::LOAD: return LowerUnalignedLoad(Op, DAG);
Krzysztof Parzyszek0b6187c2018-06-01 14:00:32 +00002801 case ISD::ADDCARRY:
2802 case ISD::SUBCARRY: return LowerAddSubCarry(Op, DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002803 case ISD::SRA:
2804 case ISD::SHL:
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002805 case ISD::SRL: return LowerVECTOR_SHIFT(Op, DAG);
Krzysztof Parzyszek3d671242018-06-12 12:49:36 +00002806 case ISD::ROTL: return LowerROTL(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002807 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002808 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002809 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002810 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
2811 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00002812 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002813 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
2814 case ISD::GlobalAddress: return LowerGLOBALADDRESS(Op, DAG);
2815 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002816 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002817 case ISD::VASTART: return LowerVASTART(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002818 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
2819 case ISD::SETCC: return LowerSETCC(Op, DAG);
2820 case ISD::VSELECT: return LowerVSELECT(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002821 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00002822 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00002823 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG);
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +00002824 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Krzysztof Parzyszek9eb085e2018-01-31 20:48:11 +00002825 break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002826 }
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00002827
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002828 return SDValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002829}
2830
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002831void
2832HexagonTargetLowering::ReplaceNodeResults(SDNode *N,
2833 SmallVectorImpl<SDValue> &Results,
2834 SelectionDAG &DAG) const {
2835 const SDLoc &dl(N);
2836 switch (N->getOpcode()) {
2837 case ISD::SRL:
2838 case ISD::SRA:
2839 case ISD::SHL:
2840 return;
2841 case ISD::BITCAST:
2842 // Handle a bitcast from v8i1 to i8.
2843 if (N->getValueType(0) == MVT::i8) {
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002844 SDValue P = getInstr(Hexagon::C2_tfrpr, dl, MVT::i32,
2845 N->getOperand(0), DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002846 Results.push_back(P);
2847 }
2848 break;
2849 }
2850}
2851
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002852/// Returns relocation base for the given PIC jumptable.
2853SDValue
2854HexagonTargetLowering::getPICJumpTableRelocBase(SDValue Table,
2855 SelectionDAG &DAG) const {
2856 int Idx = cast<JumpTableSDNode>(Table)->getIndex();
2857 EVT VT = Table.getValueType();
2858 SDValue T = DAG.getTargetJumpTable(Idx, VT, HexagonII::MO_PCREL);
2859 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Table), VT, T);
2860}
2861
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002862//===----------------------------------------------------------------------===//
2863// Inline Assembly Support
2864//===----------------------------------------------------------------------===//
2865
Krzysztof Parzyszekca3b5322016-05-18 14:34:51 +00002866TargetLowering::ConstraintType
2867HexagonTargetLowering::getConstraintType(StringRef Constraint) const {
2868 if (Constraint.size() == 1) {
2869 switch (Constraint[0]) {
2870 case 'q':
2871 case 'v':
2872 if (Subtarget.useHVXOps())
Krzysztof Parzyszek3ad0d012017-07-21 17:51:27 +00002873 return C_RegisterClass;
2874 break;
2875 case 'a':
2876 return C_RegisterClass;
2877 default:
Krzysztof Parzyszekca3b5322016-05-18 14:34:51 +00002878 break;
2879 }
2880 }
2881 return TargetLowering::getConstraintType(Constraint);
2882}
2883
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002884std::pair<unsigned, const TargetRegisterClass*>
Eric Christopher11e4df72015-02-26 22:38:43 +00002885HexagonTargetLowering::getRegForInlineAsmConstraint(
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00002886 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002887
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002888 if (Constraint.size() == 1) {
2889 switch (Constraint[0]) {
2890 case 'r': // R0-R31
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002891 switch (VT.SimpleTy) {
2892 default:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002893 return {0u, nullptr};
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002894 case MVT::i1:
2895 case MVT::i8:
2896 case MVT::i16:
2897 case MVT::i32:
2898 case MVT::f32:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002899 return {0u, &Hexagon::IntRegsRegClass};
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002900 case MVT::i64:
2901 case MVT::f64:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002902 return {0u, &Hexagon::DoubleRegsRegClass};
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002903 }
Krzysztof Parzyszek3ad0d012017-07-21 17:51:27 +00002904 break;
2905 case 'a': // M0-M1
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002906 if (VT != MVT::i32)
2907 return {0u, nullptr};
2908 return {0u, &Hexagon::ModRegsRegClass};
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002909 case 'q': // q0-q3
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002910 switch (VT.getSizeInBits()) {
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002911 default:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002912 return {0u, nullptr};
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002913 case 512:
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002914 case 1024:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002915 return {0u, &Hexagon::HvxQRRegClass};
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002916 }
Krzysztof Parzyszek3ad0d012017-07-21 17:51:27 +00002917 break;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002918 case 'v': // V0-V31
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002919 switch (VT.getSizeInBits()) {
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002920 default:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002921 return {0u, nullptr};
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002922 case 512:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002923 return {0u, &Hexagon::HvxVRRegClass};
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002924 case 1024:
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +00002925 if (Subtarget.hasV60TOps() && Subtarget.useHVX128BOps())
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002926 return {0u, &Hexagon::HvxVRRegClass};
2927 return {0u, &Hexagon::HvxWRRegClass};
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002928 case 2048:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002929 return {0u, &Hexagon::HvxWRRegClass};
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002930 }
Krzysztof Parzyszek3ad0d012017-07-21 17:51:27 +00002931 break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002932 default:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002933 return {0u, nullptr};
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002934 }
2935 }
2936
Eric Christopher11e4df72015-02-26 22:38:43 +00002937 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002938}
2939
Sirish Pande69295b82012-05-10 20:20:25 +00002940/// isFPImmLegal - Returns true if the target can instruction select the
2941/// specified FP immediate natively. If false, the legalizer will
2942/// materialize the FP immediate as a load from a constant pool.
2943bool HexagonTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002944 return Subtarget.hasV5TOps();
Sirish Pande69295b82012-05-10 20:20:25 +00002945}
2946
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002947/// isLegalAddressingMode - Return true if the addressing mode represented by
2948/// AM is legal for this target, for a load/store of the specified type.
Mehdi Amini0cdec1e2015-07-09 02:09:40 +00002949bool HexagonTargetLowering::isLegalAddressingMode(const DataLayout &DL,
2950 const AddrMode &AM, Type *Ty,
Jonas Paulsson024e3192017-07-21 11:59:37 +00002951 unsigned AS, Instruction *I) const {
Krzysztof Parzyszeked4e7822016-08-03 15:06:18 +00002952 if (Ty->isSized()) {
2953 // When LSR detects uses of the same base address to access different
2954 // types (e.g. unions), it will assume a conservative type for these
2955 // uses:
2956 // LSR Use: Kind=Address of void in addrspace(4294967295), ...
2957 // The type Ty passed here would then be "void". Skip the alignment
2958 // checks, but do not return false right away, since that confuses
2959 // LSR into crashing.
2960 unsigned A = DL.getABITypeAlignment(Ty);
2961 // The base offset must be a multiple of the alignment.
2962 if ((AM.BaseOffs % A) != 0)
2963 return false;
2964 // The shifted offset must fit in 11 bits.
2965 if (!isInt<11>(AM.BaseOffs >> Log2_32(A)))
2966 return false;
2967 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002968
2969 // No global is ever allowed as a base.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002970 if (AM.BaseGV)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002971 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002972
2973 int Scale = AM.Scale;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00002974 if (Scale < 0)
2975 Scale = -Scale;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002976 switch (Scale) {
2977 case 0: // No scale reg, "r+i", "r", or just "i".
2978 break;
2979 default: // No scaled addressing mode.
2980 return false;
2981 }
2982 return true;
2983}
2984
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002985/// Return true if folding a constant offset with the given GlobalAddress is
2986/// legal. It is frequently not legal in PIC relocation models.
2987bool HexagonTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA)
2988 const {
2989 return HTM.getRelocationModel() == Reloc::Static;
2990}
2991
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002992/// isLegalICmpImmediate - Return true if the specified immediate is legal
2993/// icmp immediate, that is the target has icmp instructions which can compare
2994/// a register against the immediate without having to materialize the
2995/// immediate into a register.
2996bool HexagonTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
2997 return Imm >= -512 && Imm <= 511;
2998}
2999
3000/// IsEligibleForTailCallOptimization - Check whether the call is eligible
3001/// for tail call optimization. Targets which want to do tail call
3002/// optimization should implement this function.
3003bool HexagonTargetLowering::IsEligibleForTailCallOptimization(
3004 SDValue Callee,
3005 CallingConv::ID CalleeCC,
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00003006 bool IsVarArg,
3007 bool IsCalleeStructRet,
3008 bool IsCallerStructRet,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003009 const SmallVectorImpl<ISD::OutputArg> &Outs,
3010 const SmallVectorImpl<SDValue> &OutVals,
3011 const SmallVectorImpl<ISD::InputArg> &Ins,
3012 SelectionDAG& DAG) const {
Matthias Braunf1caa282017-12-15 22:22:58 +00003013 const Function &CallerF = DAG.getMachineFunction().getFunction();
3014 CallingConv::ID CallerCC = CallerF.getCallingConv();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003015 bool CCMatch = CallerCC == CalleeCC;
3016
3017 // ***************************************************************************
3018 // Look for obvious safe cases to perform tail call optimization that do not
3019 // require ABI changes.
3020 // ***************************************************************************
3021
3022 // If this is a tail call via a function pointer, then don't do it!
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00003023 if (!isa<GlobalAddressSDNode>(Callee) &&
3024 !isa<ExternalSymbolSDNode>(Callee)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003025 return false;
3026 }
3027
Krzysztof Parzyszek0ba97542016-08-19 15:02:18 +00003028 // Do not optimize if the calling conventions do not match and the conventions
3029 // used are not C or Fast.
3030 if (!CCMatch) {
3031 bool R = (CallerCC == CallingConv::C || CallerCC == CallingConv::Fast);
3032 bool E = (CalleeCC == CallingConv::C || CalleeCC == CallingConv::Fast);
3033 // If R & E, then ok.
3034 if (!R || !E)
3035 return false;
3036 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003037
3038 // Do not tail call optimize vararg calls.
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00003039 if (IsVarArg)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003040 return false;
3041
3042 // Also avoid tail call optimization if either caller or callee uses struct
3043 // return semantics.
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00003044 if (IsCalleeStructRet || IsCallerStructRet)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00003045 return false;
3046
3047 // In addition to the cases above, we also disable Tail Call Optimization if
3048 // the calling convention code that at least one outgoing argument needs to
3049 // go on the stack. We cannot check that here because at this point that
3050 // information is not available.
3051 return true;
3052}
Colin LeMahieu025f8602014-12-08 21:19:18 +00003053
Krzysztof Parzyszek3e409e12016-08-02 18:34:31 +00003054/// Returns the target specific optimal type for load and store operations as
3055/// a result of memset, memcpy, and memmove lowering.
3056///
3057/// If DstAlign is zero that means it's safe to destination alignment can
3058/// satisfy any constraint. Similarly if SrcAlign is zero it means there isn't
3059/// a need to check it against alignment requirement, probably because the
3060/// source does not need to be loaded. If 'IsMemset' is true, that means it's
3061/// expanding a memset. If 'ZeroMemset' is true, that means it's a memset of
3062/// zero. 'MemcpyStrSrc' indicates whether the memcpy source is constant so it
3063/// does not need to be loaded. It returns EVT::Other if the type should be
3064/// determined using generic target-independent logic.
3065EVT HexagonTargetLowering::getOptimalMemOpType(uint64_t Size,
3066 unsigned DstAlign, unsigned SrcAlign, bool IsMemset, bool ZeroMemset,
3067 bool MemcpyStrSrc, MachineFunction &MF) const {
3068
3069 auto Aligned = [](unsigned GivenA, unsigned MinA) -> bool {
3070 return (GivenA % MinA) == 0;
3071 };
3072
3073 if (Size >= 8 && Aligned(DstAlign, 8) && (IsMemset || Aligned(SrcAlign, 8)))
3074 return MVT::i64;
3075 if (Size >= 4 && Aligned(DstAlign, 4) && (IsMemset || Aligned(SrcAlign, 4)))
3076 return MVT::i32;
3077 if (Size >= 2 && Aligned(DstAlign, 2) && (IsMemset || Aligned(SrcAlign, 2)))
3078 return MVT::i16;
3079
3080 return MVT::Other;
3081}
3082
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +00003083bool HexagonTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
3084 unsigned AS, unsigned Align, bool *Fast) const {
3085 if (Fast)
3086 *Fast = false;
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00003087 return Subtarget.isHVXVectorType(VT.getSimpleVT());
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +00003088}
3089
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003090std::pair<const TargetRegisterClass*, uint8_t>
3091HexagonTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI,
3092 MVT VT) const {
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00003093 if (Subtarget.isHVXVectorType(VT, true)) {
3094 unsigned BitWidth = VT.getSizeInBits();
3095 unsigned VecWidth = Subtarget.getVectorLength() * 8;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003096
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00003097 if (VT.getVectorElementType() == MVT::i1)
3098 return std::make_pair(&Hexagon::HvxQRRegClass, 1);
3099 if (BitWidth == VecWidth)
3100 return std::make_pair(&Hexagon::HvxVRRegClass, 1);
3101 assert(BitWidth == 2 * VecWidth);
3102 return std::make_pair(&Hexagon::HvxWRRegClass, 1);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003103 }
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00003104
3105 return TargetLowering::findRepresentativeClass(TRI, VT);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00003106}
3107
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003108Value *HexagonTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
3109 AtomicOrdering Ord) const {
3110 BasicBlock *BB = Builder.GetInsertBlock();
3111 Module *M = BB->getParent()->getParent();
3112 Type *Ty = cast<PointerType>(Addr->getType())->getElementType();
3113 unsigned SZ = Ty->getPrimitiveSizeInBits();
3114 assert((SZ == 32 || SZ == 64) && "Only 32/64-bit atomic loads supported");
3115 Intrinsic::ID IntID = (SZ == 32) ? Intrinsic::hexagon_L2_loadw_locked
3116 : Intrinsic::hexagon_L4_loadd_locked;
3117 Value *Fn = Intrinsic::getDeclaration(M, IntID);
3118 return Builder.CreateCall(Fn, Addr, "larx");
3119}
3120
3121/// Perform a store-conditional operation to Addr. Return the status of the
3122/// store. This should be 0 if the store succeeded, non-zero otherwise.
3123Value *HexagonTargetLowering::emitStoreConditional(IRBuilder<> &Builder,
3124 Value *Val, Value *Addr, AtomicOrdering Ord) const {
3125 BasicBlock *BB = Builder.GetInsertBlock();
3126 Module *M = BB->getParent()->getParent();
3127 Type *Ty = Val->getType();
3128 unsigned SZ = Ty->getPrimitiveSizeInBits();
3129 assert((SZ == 32 || SZ == 64) && "Only 32/64-bit atomic stores supported");
3130 Intrinsic::ID IntID = (SZ == 32) ? Intrinsic::hexagon_S2_storew_locked
3131 : Intrinsic::hexagon_S4_stored_locked;
3132 Value *Fn = Intrinsic::getDeclaration(M, IntID);
3133 Value *Call = Builder.CreateCall(Fn, {Addr, Val}, "stcx");
3134 Value *Cmp = Builder.CreateICmpEQ(Call, Builder.getInt32(0), "");
3135 Value *Ext = Builder.CreateZExt(Cmp, Type::getInt32Ty(M->getContext()));
3136 return Ext;
3137}
3138
Ahmed Bougacha52468672015-09-11 17:08:28 +00003139TargetLowering::AtomicExpansionKind
3140HexagonTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const {
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003141 // Do not expand loads and stores that don't exceed 64 bits.
Ahmed Bougacha52468672015-09-11 17:08:28 +00003142 return LI->getType()->getPrimitiveSizeInBits() > 64
Tim Northoverf520eff2015-12-02 18:12:57 +00003143 ? AtomicExpansionKind::LLOnly
Ahmed Bougacha52468672015-09-11 17:08:28 +00003144 : AtomicExpansionKind::None;
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003145}
3146
3147bool HexagonTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const {
3148 // Do not expand loads and stores that don't exceed 64 bits.
3149 return SI->getValueOperand()->getType()->getPrimitiveSizeInBits() > 64;
3150}
Krzysztof Parzyszekf228c952016-06-22 16:07:10 +00003151
3152bool HexagonTargetLowering::shouldExpandAtomicCmpXchgInIR(
3153 AtomicCmpXchgInst *AI) const {
3154 const DataLayout &DL = AI->getModule()->getDataLayout();
3155 unsigned Size = DL.getTypeStoreSize(AI->getCompareOperand()->getType());
3156 return Size >= 4 && Size <= 8;
3157}