blob: 5e0b50a1a1770c881c2781065f3bb58655da8985 [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"
Tony Linthicum1213a7a2011-12-12 21:14:40 +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"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000043#include "llvm/IR/Module.h"
44#include "llvm/IR/Type.h"
45#include "llvm/IR/Value.h"
46#include "llvm/MC/MCRegisterInfo.h"
47#include "llvm/Support/Casting.h"
48#include "llvm/Support/CodeGen.h"
NAKAMURA Takumi54eed762012-04-21 15:31:36 +000049#include "llvm/Support/CommandLine.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000050#include "llvm/Support/Debug.h"
51#include "llvm/Support/ErrorHandling.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000052#include "llvm/Support/MathExtras.h"
NAKAMURA Takumie30303f2012-04-21 15:31:45 +000053#include "llvm/Support/raw_ostream.h"
Eugene Zelenko58655bb2016-12-17 01:09:05 +000054#include "llvm/Target/TargetMachine.h"
55#include <algorithm>
56#include <cassert>
57#include <cstddef>
58#include <cstdint>
59#include <limits>
60#include <utility>
NAKAMURA Takumi54eed762012-04-21 15:31:36 +000061
Craig Topperb25fda92012-03-17 18:46:09 +000062using namespace llvm;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000063
Chandler Carruthe96dd892014-04-21 22:55:11 +000064#define DEBUG_TYPE "hexagon-lowering"
65
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +000066static cl::opt<bool> EmitJumpTables("hexagon-emit-jump-tables",
67 cl::init(true), cl::Hidden,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +000068 cl::desc("Control jump table emission on Hexagon target"));
69
70static cl::opt<bool> EnableHexSDNodeSched("enable-hexagon-sdnode-sched",
71 cl::Hidden, cl::ZeroOrMore, cl::init(false),
72 cl::desc("Enable Hexagon SDNode scheduling"));
73
74static cl::opt<bool> EnableFastMath("ffast-math",
75 cl::Hidden, cl::ZeroOrMore, cl::init(false),
76 cl::desc("Enable Fast Math processing"));
77
78static cl::opt<int> MinimumJumpTables("minimum-jump-tables",
79 cl::Hidden, cl::ZeroOrMore, cl::init(5),
80 cl::desc("Set minimum jump tables"));
81
82static cl::opt<int> MaxStoresPerMemcpyCL("max-store-memcpy",
83 cl::Hidden, cl::ZeroOrMore, cl::init(6),
84 cl::desc("Max #stores to inline memcpy"));
85
86static cl::opt<int> MaxStoresPerMemcpyOptSizeCL("max-store-memcpy-Os",
87 cl::Hidden, cl::ZeroOrMore, cl::init(4),
88 cl::desc("Max #stores to inline memcpy"));
89
90static cl::opt<int> MaxStoresPerMemmoveCL("max-store-memmove",
91 cl::Hidden, cl::ZeroOrMore, cl::init(6),
92 cl::desc("Max #stores to inline memmove"));
93
94static cl::opt<int> MaxStoresPerMemmoveOptSizeCL("max-store-memmove-Os",
95 cl::Hidden, cl::ZeroOrMore, cl::init(4),
96 cl::desc("Max #stores to inline memmove"));
97
98static cl::opt<int> MaxStoresPerMemsetCL("max-store-memset",
99 cl::Hidden, cl::ZeroOrMore, cl::init(8),
100 cl::desc("Max #stores to inline memset"));
101
102static cl::opt<int> MaxStoresPerMemsetOptSizeCL("max-store-memset-Os",
103 cl::Hidden, cl::ZeroOrMore, cl::init(4),
104 cl::desc("Max #stores to inline memset"));
105
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +0000106static cl::opt<bool> AlignLoads("hexagon-align-loads",
107 cl::Hidden, cl::init(false),
108 cl::desc("Rewrite unaligned loads as a pair of aligned loads"));
109
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000110
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000111namespace {
Eugene Zelenko58655bb2016-12-17 01:09:05 +0000112
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +0000113 class HexagonCCState : public CCState {
Krzysztof Parzyszek18e0d2a2018-02-15 15:47:53 +0000114 unsigned NumNamedVarArgParams = 0;
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000115
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +0000116 public:
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000117 HexagonCCState(CallingConv::ID CC, bool IsVarArg, MachineFunction &MF,
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +0000118 SmallVectorImpl<CCValAssign> &locs, LLVMContext &C,
Krzysztof Parzyszeke0d7de72018-02-15 17:20:07 +0000119 unsigned NumNamedArgs)
120 : CCState(CC, IsVarArg, MF, locs, C),
121 NumNamedVarArgParams(NumNamedArgs) {}
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +0000122 unsigned getNumNamedVarArgParams() const { return NumNamedVarArgParams; }
123 };
124
Eugene Zelenko58655bb2016-12-17 01:09:05 +0000125} // end anonymous namespace
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000126
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000127
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000128// Implement calling convention for Hexagon.
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000129
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000130static bool CC_SkipOdd(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
131 CCValAssign::LocInfo &LocInfo,
132 ISD::ArgFlagsTy &ArgFlags, CCState &State) {
133 static const MCPhysReg ArgRegs[] = {
134 Hexagon::R0, Hexagon::R1, Hexagon::R2,
135 Hexagon::R3, Hexagon::R4, Hexagon::R5
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000136 };
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000137 const unsigned NumArgRegs = array_lengthof(ArgRegs);
138 unsigned RegNum = State.getFirstUnallocated(ArgRegs);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000139
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000140 // RegNum is an index into ArgRegs: skip a register if RegNum is odd.
141 if (RegNum != NumArgRegs && RegNum % 2 == 1)
142 State.AllocateReg(ArgRegs[RegNum]);
143
144 // Always return false here, as this function only makes sure that the first
145 // unallocated register has an even register number and does not actually
146 // allocate a register for the current argument.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000147 return false;
148}
149
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000150#include "HexagonGenCallingConv.inc"
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000151
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000152
Craig Topper18e69f42016-04-15 06:20:21 +0000153void HexagonTargetLowering::promoteLdStType(MVT VT, MVT PromotedLdStVT) {
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000154 if (VT != PromotedLdStVT) {
Craig Topper18e69f42016-04-15 06:20:21 +0000155 setOperationAction(ISD::LOAD, VT, Promote);
156 AddPromotedToType(ISD::LOAD, VT, PromotedLdStVT);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000157
Craig Topper18e69f42016-04-15 06:20:21 +0000158 setOperationAction(ISD::STORE, VT, Promote);
159 AddPromotedToType(ISD::STORE, VT, PromotedLdStVT);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000160 }
161}
162
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000163SDValue
164HexagonTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG)
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000165 const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000166 return SDValue();
167}
168
169/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
170/// by "Src" to address "Dst" of size "Size". Alignment information is
171/// specified by the specific parameter attribute. The copy will be passed as
172/// a byval function parameter. Sometimes what we are copying is the end of a
173/// larger object, the part that does not fit in registers.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000174static SDValue CreateCopyOfByValArgument(SDValue Src, SDValue Dst,
175 SDValue Chain, ISD::ArgFlagsTy Flags,
176 SelectionDAG &DAG, const SDLoc &dl) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000177 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000178 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
179 /*isVolatile=*/false, /*AlwaysInline=*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +0000180 /*isTailCall=*/false,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000181 MachinePointerInfo(), MachinePointerInfo());
182}
183
Krzysztof Parzyszek56199522017-04-13 15:05:51 +0000184bool
185HexagonTargetLowering::CanLowerReturn(
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000186 CallingConv::ID CallConv, MachineFunction &MF, bool IsVarArg,
Krzysztof Parzyszek56199522017-04-13 15:05:51 +0000187 const SmallVectorImpl<ISD::OutputArg> &Outs,
188 LLVMContext &Context) const {
189 SmallVector<CCValAssign, 16> RVLocs;
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000190 CCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
191
192 if (MF.getSubtarget<HexagonSubtarget>().useHVXOps())
193 return CCInfo.CheckReturn(Outs, RetCC_Hexagon_HVX);
Krzysztof Parzyszek56199522017-04-13 15:05:51 +0000194 return CCInfo.CheckReturn(Outs, RetCC_Hexagon);
195}
196
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000197// LowerReturn - Lower ISD::RET. If a struct is larger than 8 bytes and is
198// passed by value, the function prototype is modified to return void and
199// the value is stored in memory pointed by a pointer passed by caller.
200SDValue
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000201HexagonTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000202 bool IsVarArg,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000203 const SmallVectorImpl<ISD::OutputArg> &Outs,
204 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000205 const SDLoc &dl, SelectionDAG &DAG) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000206 // CCValAssign - represent the assignment of the return value to locations.
207 SmallVector<CCValAssign, 16> RVLocs;
208
209 // CCState - Info about the registers and stack slot.
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000210 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
Eric Christopherb5217502014-08-06 18:45:26 +0000211 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000212
213 // Analyze return values of ISD::RET
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000214 if (Subtarget.useHVXOps())
215 CCInfo.AnalyzeReturn(Outs, RetCC_Hexagon_HVX);
216 else
217 CCInfo.AnalyzeReturn(Outs, RetCC_Hexagon);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000218
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000219 SDValue Flag;
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000220 SmallVector<SDValue, 4> RetOps(1, Chain);
221
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000222 // Copy the result values into the output registers.
223 for (unsigned i = 0; i != RVLocs.size(); ++i) {
224 CCValAssign &VA = RVLocs[i];
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000225
226 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), OutVals[i], Flag);
227
228 // Guarantee that all emitted copies are stuck together with flags.
229 Flag = Chain.getValue(1);
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000230 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000231 }
232
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000233 RetOps[0] = Chain; // Update chain.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000234
Jakob Stoklund Olesen0af477c2013-02-05 18:08:43 +0000235 // Add the flag if we have it.
236 if (Flag.getNode())
237 RetOps.push_back(Flag);
238
Craig Topper48d114b2014-04-26 18:35:24 +0000239 return DAG.getNode(HexagonISD::RET_FLAG, dl, MVT::Other, RetOps);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000240}
241
Matt Arsenault31380752017-04-18 21:16:46 +0000242bool HexagonTargetLowering::mayBeEmittedAsTailCall(const CallInst *CI) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000243 // If either no tail call or told not to tail call at all, don't.
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000244 auto Attr =
245 CI->getParent()->getParent()->getFnAttribute("disable-tail-calls");
246 if (!CI->isTailCall() || Attr.getValueAsString() == "true")
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000247 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000248
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000249 return true;
250}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000251
252/// LowerCallResult - Lower the result values of an ISD::CALL into the
253/// appropriate copies out of appropriate physical registers. This assumes that
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000254/// Chain/Glue are the input chain/glue to use, and that TheCall is the call
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000255/// being lowered. Returns a SDNode with the same number of values as the
256/// ISD::CALL.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000257SDValue HexagonTargetLowering::LowerCallResult(
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000258 SDValue Chain, SDValue Glue, CallingConv::ID CallConv, bool IsVarArg,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000259 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
260 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals,
261 const SmallVectorImpl<SDValue> &OutVals, SDValue Callee) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000262 // Assign locations to each value returned by this call.
263 SmallVector<CCValAssign, 16> RVLocs;
264
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000265 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
Eric Christopherb5217502014-08-06 18:45:26 +0000266 *DAG.getContext());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000267
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000268 if (Subtarget.useHVXOps())
269 CCInfo.AnalyzeCallResult(Ins, RetCC_Hexagon_HVX);
270 else
271 CCInfo.AnalyzeCallResult(Ins, RetCC_Hexagon);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000272
273 // Copy all of the result registers out of their specified physreg.
274 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000275 SDValue RetVal;
276 if (RVLocs[i].getValVT() == MVT::i1) {
277 // Return values of type MVT::i1 require special handling. The reason
278 // is that MVT::i1 is associated with the PredRegs register class, but
279 // values of that type are still returned in R0. Generate an explicit
280 // copy into a predicate register from R0, and treat the value of the
281 // predicate register as the call result.
282 auto &MRI = DAG.getMachineFunction().getRegInfo();
283 SDValue FR0 = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000284 MVT::i32, Glue);
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000285 // FR0 = (Value, Chain, Glue)
286 unsigned PredR = MRI.createVirtualRegister(&Hexagon::PredRegsRegClass);
287 SDValue TPR = DAG.getCopyToReg(FR0.getValue(1), dl, PredR,
288 FR0.getValue(0), FR0.getValue(2));
289 // TPR = (Chain, Glue)
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000290 // Don't glue this CopyFromReg, because it copies from a virtual
291 // register. If it is glued to the call, InstrEmitter will add it
292 // as an implicit def to the call (EmitMachineNode).
293 RetVal = DAG.getCopyFromReg(TPR.getValue(0), dl, PredR, MVT::i1);
294 Glue = TPR.getValue(1);
Krzysztof Parzyszek6f06b6e2017-10-23 19:35:25 +0000295 Chain = TPR.getValue(0);
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000296 } else {
297 RetVal = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000298 RVLocs[i].getValVT(), Glue);
299 Glue = RetVal.getValue(2);
Krzysztof Parzyszek6f06b6e2017-10-23 19:35:25 +0000300 Chain = RetVal.getValue(1);
Krzysztof Parzyszek51155fc2016-03-04 17:38:05 +0000301 }
302 InVals.push_back(RetVal.getValue(0));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000303 }
304
305 return Chain;
306}
307
308/// LowerCall - Functions arguments are copied from virtual regs to
309/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
310SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +0000311HexagonTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000312 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +0000313 SelectionDAG &DAG = CLI.DAG;
Craig Topperb94011f2013-07-14 04:42:23 +0000314 SDLoc &dl = CLI.DL;
315 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
316 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
317 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000318 SDValue Chain = CLI.Chain;
319 SDValue Callee = CLI.Callee;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000320 CallingConv::ID CallConv = CLI.CallConv;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000321 bool IsVarArg = CLI.IsVarArg;
322 bool DoesNotReturn = CLI.DoesNotReturn;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000323
Krzysztof Parzyszeke0d7de72018-02-15 17:20:07 +0000324 bool IsStructRet = Outs.empty() ? false : Outs[0].Flags.isSRet();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000325 MachineFunction &MF = DAG.getMachineFunction();
Krzysztof Parzyszekf67cd822017-07-11 17:11:54 +0000326 MachineFrameInfo &MFI = MF.getFrameInfo();
Mehdi Amini44ede332015-07-09 02:09:04 +0000327 auto PtrVT = getPointerTy(MF.getDataLayout());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000328
Krzysztof Parzyszeke0d7de72018-02-15 17:20:07 +0000329 unsigned NumParams = CLI.CS.getInstruction()
330 ? CLI.CS.getFunctionType()->getNumParams()
331 : 0;
332 if (GlobalAddressSDNode *GAN = dyn_cast<GlobalAddressSDNode>(Callee))
333 Callee = DAG.getTargetGlobalAddress(GAN->getGlobal(), dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000334
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000335 // Analyze operands of the call, assigning locations to each operand.
336 SmallVector<CCValAssign, 16> ArgLocs;
Krzysztof Parzyszek18e0d2a2018-02-15 15:47:53 +0000337 HexagonCCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext(),
Krzysztof Parzyszeke0d7de72018-02-15 17:20:07 +0000338 NumParams);
Benjamin Kramer602bb4a2013-10-27 11:16:09 +0000339
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000340 if (Subtarget.useHVXOps())
341 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon_HVX);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000342 else
343 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon);
344
Matthias Braunf1caa282017-12-15 22:22:58 +0000345 auto Attr = MF.getFunction().getFnAttribute("disable-tail-calls");
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000346 if (Attr.getValueAsString() == "true")
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000347 CLI.IsTailCall = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000348
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000349 if (CLI.IsTailCall) {
Matthias Braunf1caa282017-12-15 22:22:58 +0000350 bool StructAttrFlag = MF.getFunction().hasStructRetAttr();
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000351 CLI.IsTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
352 IsVarArg, IsStructRet, StructAttrFlag, Outs,
353 OutVals, Ins, DAG);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000354 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000355 CCValAssign &VA = ArgLocs[i];
356 if (VA.isMemLoc()) {
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000357 CLI.IsTailCall = false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000358 break;
359 }
360 }
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000361 DEBUG(dbgs() << (CLI.IsTailCall ? "Eligible for Tail Call\n"
362 : "Argument must be passed on stack. "
363 "Not eligible for Tail Call\n"));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000364 }
365 // Get a count of how many bytes are to be pushed on the stack.
366 unsigned NumBytes = CCInfo.getNextStackOffset();
367 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
368 SmallVector<SDValue, 8> MemOpChains;
369
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000370 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Mehdi Amini44ede332015-07-09 02:09:04 +0000371 SDValue StackPtr =
372 DAG.getCopyFromReg(Chain, dl, HRI.getStackRegister(), PtrVT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000373
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000374 bool NeedsArgAlign = false;
375 unsigned LargestAlignSeen = 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000376 // Walk the register/memloc assignments, inserting copies/loads.
377 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
378 CCValAssign &VA = ArgLocs[i];
379 SDValue Arg = OutVals[i];
380 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000381 // Record if we need > 8 byte alignment on an argument.
Krzysztof Parzyszekac1966e2017-11-27 18:12:16 +0000382 bool ArgAlign = Subtarget.isHVXVectorType(VA.getValVT());
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000383 NeedsArgAlign |= ArgAlign;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000384
385 // Promote the value if needed.
386 switch (VA.getLocInfo()) {
387 default:
Krzysztof Parzyszek8f6b0c82017-12-20 14:44:05 +0000388 // Loc info must be one of Full, BCvt, SExt, ZExt, or AExt.
Craig Toppere55c5562012-02-07 02:50:20 +0000389 llvm_unreachable("Unknown loc info!");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000390 case CCValAssign::Full:
391 break;
Krzysztof Parzyszek8f6b0c82017-12-20 14:44:05 +0000392 case CCValAssign::BCvt:
393 Arg = DAG.getBitcast(VA.getLocVT(), Arg);
394 break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000395 case CCValAssign::SExt:
396 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
397 break;
398 case CCValAssign::ZExt:
399 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
400 break;
401 case CCValAssign::AExt:
402 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
403 break;
404 }
405
406 if (VA.isMemLoc()) {
407 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000408 SDValue MemAddr = DAG.getConstant(LocMemOffset, dl,
409 StackPtr.getValueType());
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000410 MemAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, MemAddr);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000411 if (ArgAlign)
412 LargestAlignSeen = std::max(LargestAlignSeen,
413 VA.getLocVT().getStoreSizeInBits() >> 3);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000414 if (Flags.isByVal()) {
415 // The argument is a struct passed by value. According to LLVM, "Arg"
416 // is is pointer.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000417 MemOpChains.push_back(CreateCopyOfByValArgument(Arg, MemAddr, Chain,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000418 Flags, DAG, dl));
419 } else {
Alex Lorenze40c8a22015-08-11 23:09:45 +0000420 MachinePointerInfo LocPI = MachinePointerInfo::getStack(
421 DAG.getMachineFunction(), LocMemOffset);
Justin Lebar9c375812016-07-15 18:27:10 +0000422 SDValue S = DAG.getStore(Chain, dl, Arg, MemAddr, LocPI);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000423 MemOpChains.push_back(S);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000424 }
425 continue;
426 }
427
428 // Arguments that can be passed on register must be kept at RegsToPass
429 // vector.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000430 if (VA.isRegLoc())
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000431 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000432 }
433
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000434 if (NeedsArgAlign && Subtarget.hasV60TOps()) {
435 DEBUG(dbgs() << "Function needs byte stack align due to call args\n");
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000436 unsigned VecAlign = HRI.getSpillAlignment(Hexagon::HvxVRRegClass);
437 LargestAlignSeen = std::max(LargestAlignSeen, VecAlign);
Matthias Braun941a7052016-07-28 18:40:00 +0000438 MFI.ensureMaxAlignment(LargestAlignSeen);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000439 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000440 // Transform all store nodes into one single node because all store
441 // nodes are independent of each other.
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000442 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +0000443 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000444
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000445 SDValue Glue;
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000446 if (!CLI.IsTailCall) {
Serge Pavlovd526b132017-05-09 13:35:13 +0000447 Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, dl);
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000448 Glue = Chain.getValue(1);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000449 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000450
451 // Build a sequence of copy-to-reg nodes chained together with token
452 // chain and flag operands which copy the outgoing args into registers.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000453 // The Glue is necessary since all emitted instructions must be
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000454 // stuck together.
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000455 if (!CLI.IsTailCall) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000456 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
457 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000458 RegsToPass[i].second, Glue);
459 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000460 }
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000461 } else {
462 // For tail calls lower the arguments to the 'real' stack slot.
463 //
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000464 // Force all the incoming stack arguments to be loaded from the stack
465 // before any new outgoing arguments are stored to the stack, because the
466 // outgoing stack slots may alias the incoming argument stack slots, and
467 // the alias isn't otherwise explicit. This is slightly more conservative
468 // than necessary, because it means that each store effectively depends
469 // on every argument instead of just those arguments it would clobber.
470 //
Benjamin Kramerbde91762012-06-02 10:20:22 +0000471 // Do not flag preceding copytoreg stuff together with the following stuff.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000472 Glue = SDValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000473 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
474 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000475 RegsToPass[i].second, Glue);
476 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000477 }
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000478 Glue = SDValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000479 }
480
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000481 bool LongCalls = MF.getSubtarget<HexagonSubtarget>().useLongCalls();
482 unsigned Flags = LongCalls ? HexagonII::HMOTF_ConstExtended : 0;
483
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000484 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
485 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
486 // node so that legalize doesn't hack it.
Tobias Edler von Kochb51460c2015-12-16 17:29:37 +0000487 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000488 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, PtrVT, 0, Flags);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000489 } else if (ExternalSymbolSDNode *S =
490 dyn_cast<ExternalSymbolSDNode>(Callee)) {
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000491 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), PtrVT, Flags);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000492 }
493
494 // Returns a chain & a flag for retval copy to use.
495 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
496 SmallVector<SDValue, 8> Ops;
497 Ops.push_back(Chain);
498 Ops.push_back(Callee);
499
500 // Add argument registers to the end of the list so that they are
501 // known live into the call.
502 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
503 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
504 RegsToPass[i].second.getValueType()));
505 }
506
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000507 const uint32_t *Mask = HRI.getCallPreservedMask(MF, CallConv);
508 assert(Mask && "Missing call preserved mask for calling convention");
509 Ops.push_back(DAG.getRegisterMask(Mask));
510
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000511 if (Glue.getNode())
512 Ops.push_back(Glue);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000513
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000514 if (CLI.IsTailCall) {
Krzysztof Parzyszekf67cd822017-07-11 17:11:54 +0000515 MFI.setHasTailCall();
Craig Topper48d114b2014-04-26 18:35:24 +0000516 return DAG.getNode(HexagonISD::TC_RETURN, dl, NodeTys, Ops);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +0000517 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000518
Krzysztof Parzyszekf67cd822017-07-11 17:11:54 +0000519 // Set this here because we need to know this for "hasFP" in frame lowering.
520 // The target-independent code calls getFrameRegister before setting it, and
521 // getFrameRegister uses hasFP to determine whether the function has FP.
522 MFI.setHasCalls(true);
523
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +0000524 unsigned OpCode = DoesNotReturn ? HexagonISD::CALLnr : HexagonISD::CALL;
Colin LeMahieu2e3a26d2015-01-16 17:05:27 +0000525 Chain = DAG.getNode(OpCode, dl, NodeTys, Ops);
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000526 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000527
528 // Create the CALLSEQ_END node.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000529 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000530 DAG.getIntPtrConstant(0, dl, true), Glue, dl);
531 Glue = Chain.getValue(1);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000532
533 // Handle result values, copying them out of physregs into vregs that we
534 // return.
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +0000535 return LowerCallResult(Chain, Glue, CallConv, IsVarArg, Ins, dl, DAG,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000536 InVals, OutVals, Callee);
537}
538
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +0000539/// Returns true by value, base pointer and offset pointer and addressing
540/// mode by reference if this node can be combined with a load / store to
541/// form a post-indexed load / store.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000542bool HexagonTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +0000543 SDValue &Base, SDValue &Offset, ISD::MemIndexedMode &AM,
544 SelectionDAG &DAG) const {
545 LSBaseSDNode *LSN = dyn_cast<LSBaseSDNode>(N);
546 if (!LSN)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000547 return false;
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +0000548 EVT VT = LSN->getMemoryVT();
549 if (!VT.isSimple())
550 return false;
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +0000551 bool IsLegalType = VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32 ||
552 VT == MVT::i64 || VT == MVT::v2i16 || MVT::v2i32 ||
553 VT == MVT::v4i8 || VT == MVT::v4i16 || MVT::v8i8 ||
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +0000554 Subtarget.isHVXVectorType(VT.getSimpleVT());
555 if (!IsLegalType)
556 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000557
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +0000558 if (Op->getOpcode() != ISD::ADD)
559 return false;
560 Base = Op->getOperand(0);
561 Offset = Op->getOperand(1);
562 if (!isa<ConstantSDNode>(Offset.getNode()))
563 return false;
564 AM = ISD::POST_INC;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000565
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +0000566 int32_t V = cast<ConstantSDNode>(Offset.getNode())->getSExtValue();
567 return Subtarget.getInstrInfo()->isValidAutoIncImm(VT, V);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000568}
569
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000570SDValue
571HexagonTargetLowering::LowerINLINEASM(SDValue Op, SelectionDAG &DAG) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000572 MachineFunction &MF = DAG.getMachineFunction();
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000573 auto &HMFI = *MF.getInfo<HexagonMachineFunctionInfo>();
574 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
575 unsigned LR = HRI.getRARegister();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000576
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000577 if (Op.getOpcode() != ISD::INLINEASM || HMFI.hasClobberLR())
578 return Op;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000579
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000580 unsigned NumOps = Op.getNumOperands();
581 if (Op.getOperand(NumOps-1).getValueType() == MVT::Glue)
582 --NumOps; // Ignore the flag operand.
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000583
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000584 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
585 unsigned Flags = cast<ConstantSDNode>(Op.getOperand(i))->getZExtValue();
586 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
587 ++i; // Skip the ID value.
588
589 switch (InlineAsm::getKind(Flags)) {
590 default:
591 llvm_unreachable("Bad flags!");
592 case InlineAsm::Kind_RegUse:
593 case InlineAsm::Kind_Imm:
594 case InlineAsm::Kind_Mem:
595 i += NumVals;
596 break;
597 case InlineAsm::Kind_Clobber:
598 case InlineAsm::Kind_RegDef:
599 case InlineAsm::Kind_RegDefEarlyClobber: {
600 for (; NumVals; --NumVals, ++i) {
601 unsigned Reg = cast<RegisterSDNode>(Op.getOperand(i))->getReg();
602 if (Reg != LR)
603 continue;
604 HMFI.setHasClobberLR(true);
605 return Op;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000606 }
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000607 break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000608 }
609 }
Krzysztof Parzyszek9eb75c42017-06-30 21:21:40 +0000610 }
611
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000612 return Op;
613}
614
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +0000615// Need to transform ISD::PREFETCH into something that doesn't inherit
616// all of the properties of ISD::PREFETCH, specifically SDNPMayLoad and
617// SDNPMayStore.
618SDValue HexagonTargetLowering::LowerPREFETCH(SDValue Op,
619 SelectionDAG &DAG) const {
620 SDValue Chain = Op.getOperand(0);
621 SDValue Addr = Op.getOperand(1);
622 // Lower it to DCFETCH($reg, #0). A "pat" will try to merge the offset in,
623 // if the "reg" is fed by an "add".
624 SDLoc DL(Op);
625 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
626 return DAG.getNode(HexagonISD::DCFETCH, DL, MVT::Other, Chain, Addr, Zero);
627}
628
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +0000629// Custom-handle ISD::READCYCLECOUNTER because the target-independent SDNode
630// is marked as having side-effects, while the register read on Hexagon does
631// not have any. TableGen refuses to accept the direct pattern from that node
632// to the A4_tfrcpp.
633SDValue HexagonTargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
634 SelectionDAG &DAG) const {
635 SDValue Chain = Op.getOperand(0);
636 SDLoc dl(Op);
637 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other);
638 return DAG.getNode(HexagonISD::READCYCLE, dl, VTs, Chain);
639}
640
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +0000641SDValue HexagonTargetLowering::LowerINTRINSIC_VOID(SDValue Op,
642 SelectionDAG &DAG) const {
643 SDValue Chain = Op.getOperand(0);
644 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
645 // Lower the hexagon_prefetch builtin to DCFETCH, as above.
646 if (IntNo == Intrinsic::hexagon_prefetch) {
647 SDValue Addr = Op.getOperand(2);
648 SDLoc DL(Op);
649 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
650 return DAG.getNode(HexagonISD::DCFETCH, DL, MVT::Other, Chain, Addr, Zero);
651 }
652 return SDValue();
653}
654
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000655SDValue
656HexagonTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
657 SelectionDAG &DAG) const {
658 SDValue Chain = Op.getOperand(0);
659 SDValue Size = Op.getOperand(1);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000660 SDValue Align = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000661 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000662
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000663 ConstantSDNode *AlignConst = dyn_cast<ConstantSDNode>(Align);
664 assert(AlignConst && "Non-constant Align in LowerDYNAMIC_STACKALLOC");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000665
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000666 unsigned A = AlignConst->getSExtValue();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000667 auto &HFI = *Subtarget.getFrameLowering();
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000668 // "Zero" means natural stack alignment.
669 if (A == 0)
670 A = HFI.getStackAlignment();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000671
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000672 DEBUG({
Reid Kleckner40d72302016-10-20 00:22:23 +0000673 dbgs () << __func__ << " Align: " << A << " Size: ";
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000674 Size.getNode()->dump(&DAG);
675 dbgs() << "\n";
676 });
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000677
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000678 SDValue AC = DAG.getConstant(A, dl, MVT::i32);
Krzysztof Parzyszek4fa2a9f2015-04-22 16:43:53 +0000679 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other);
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +0000680 SDValue AA = DAG.getNode(HexagonISD::ALLOCA, dl, VTs, Chain, Size, AC);
Nirav Davebfdb4832016-06-23 17:52:57 +0000681
682 DAG.ReplaceAllUsesOfValueWith(Op, AA);
Krzysztof Parzyszek23920ec2015-10-19 18:30:27 +0000683 return AA;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000684}
685
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000686SDValue HexagonTargetLowering::LowerFormalArguments(
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000687 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000688 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
689 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000690 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +0000691 MachineFrameInfo &MFI = MF.getFrameInfo();
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000692 MachineRegisterInfo &MRI = MF.getRegInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000693
694 // Assign locations to all of the incoming arguments.
695 SmallVector<CCValAssign, 16> ArgLocs;
Krzysztof Parzyszek18e0d2a2018-02-15 15:47:53 +0000696 HexagonCCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext(),
Krzysztof Parzyszeke0d7de72018-02-15 17:20:07 +0000697 MF.getFunction().getFunctionType()->getNumParams());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000698
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000699 if (Subtarget.useHVXOps())
700 CCInfo.AnalyzeFormalArguments(Ins, CC_Hexagon_HVX);
701 else
702 CCInfo.AnalyzeFormalArguments(Ins, CC_Hexagon);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000703
704 // For LLVM, in the case when returning a struct by value (>8byte),
705 // the first argument is a pointer that points to the location on caller's
706 // stack where the return value will be stored. For Hexagon, the location on
707 // caller's stack is passed only when the struct size is smaller than (and
708 // equal to) 8 bytes. If not, no address will be passed into callee and
709 // callee return the result direclty through R0/R1.
710
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000711 auto &HMFI = *MF.getInfo<HexagonMachineFunctionInfo>();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000712
713 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
714 CCValAssign &VA = ArgLocs[i];
715 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000716 bool ByVal = Flags.isByVal();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000717
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000718 // Arguments passed in registers:
719 // 1. 32- and 64-bit values and HVX vectors are passed directly,
720 // 2. Large structs are passed via an address, and the address is
721 // passed in a register.
722 if (VA.isRegLoc() && ByVal && Flags.getByValSize() <= 8)
723 llvm_unreachable("ByValSize must be bigger than 8 bytes");
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000724
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000725 bool InReg = VA.isRegLoc() &&
726 (!ByVal || (ByVal && Flags.getByValSize() > 8));
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +0000727
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000728 if (InReg) {
729 MVT RegVT = VA.getLocVT();
730 if (VA.getLocInfo() == CCValAssign::BCvt)
731 RegVT = VA.getValVT();
732
733 const TargetRegisterClass *RC = getRegClassFor(RegVT);
734 unsigned VReg = MRI.createVirtualRegister(RC);
735 SDValue Copy = DAG.getCopyFromReg(Chain, dl, VReg, RegVT);
736
737 // Treat values of type MVT::i1 specially: they are passed in
738 // registers of type i32, but they need to remain as values of
739 // type i1 for consistency of the argument lowering.
740 if (VA.getValVT() == MVT::i1) {
741 assert(RegVT.getSizeInBits() <= 32);
742 SDValue T = DAG.getNode(ISD::AND, dl, RegVT,
743 Copy, DAG.getConstant(1, dl, RegVT));
744 Copy = DAG.getSetCC(dl, MVT::i1, T, DAG.getConstant(0, dl, RegVT),
745 ISD::SETNE);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000746 } else {
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000747#ifndef NDEBUG
748 unsigned RegSize = RegVT.getSizeInBits();
749 assert(RegSize == 32 || RegSize == 64 ||
750 Subtarget.isHVXVectorType(RegVT));
751#endif
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000752 }
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000753 InVals.push_back(Copy);
754 MRI.addLiveIn(VA.getLocReg(), VReg);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000755 } else {
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000756 assert(VA.isMemLoc() && "Argument should be passed in memory");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000757
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000758 // If it's a byval parameter, then we need to compute the
759 // "real" size, not the size of the pointer.
760 unsigned ObjSize = Flags.isByVal()
761 ? Flags.getByValSize()
762 : VA.getLocVT().getStoreSizeInBits() / 8;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000763
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000764 // Create the frame index object for this incoming parameter.
765 int Offset = HEXAGON_LRFP_SIZE + VA.getLocMemOffset();
766 int FI = MFI.CreateFixedObject(ObjSize, Offset, true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000767 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
768
769 if (Flags.isByVal()) {
770 // If it's a pass-by-value aggregate, then do not dereference the stack
771 // location. Instead, we should generate a reference to the stack
772 // location.
773 InVals.push_back(FIN);
774 } else {
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000775 SDValue L = DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
776 MachinePointerInfo::getFixedStack(MF, FI, 0));
777 InVals.push_back(L);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000778 }
779 }
780 }
781
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000782
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000783 if (IsVarArg) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000784 // This will point to the next argument passed via stack.
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +0000785 int Offset = HEXAGON_LRFP_SIZE + CCInfo.getNextStackOffset();
786 int FI = MFI.CreateFixedObject(Hexagon_PointerSize, Offset, true);
787 HMFI.setVarArgsFrameIndex(FI);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000788 }
789
790 return Chain;
791}
792
793SDValue
794HexagonTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
795 // VASTART stores the address of the VarArgsFrameIndex slot into the
796 // memory location argument.
797 MachineFunction &MF = DAG.getMachineFunction();
798 HexagonMachineFunctionInfo *QFI = MF.getInfo<HexagonMachineFunctionInfo>();
799 SDValue Addr = DAG.getFrameIndex(QFI->getVarArgsFrameIndex(), MVT::i32);
800 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Justin Lebar9c375812016-07-15 18:27:10 +0000801 return DAG.getStore(Op.getOperand(0), SDLoc(Op), Addr, Op.getOperand(1),
802 MachinePointerInfo(SV));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000803}
804
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000805static bool isSExtFree(SDValue N) {
806 // A sign-extend of a truncate of a sign-extend is free.
807 if (N.getOpcode() == ISD::TRUNCATE &&
808 N.getOperand(0).getOpcode() == ISD::AssertSext)
809 return true;
810 // We have sign-extended loads.
811 if (N.getOpcode() == ISD::LOAD)
812 return true;
813 return false;
814}
815
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000816SDValue HexagonTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
817 SDLoc dl(Op);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000818 SDValue LHS = Op.getOperand(0);
819 SDValue RHS = Op.getOperand(1);
820 SDValue Cmp = Op.getOperand(2);
821 ISD::CondCode CC = cast<CondCodeSDNode>(Cmp)->get();
822
823 EVT VT = Op.getValueType();
824 EVT LHSVT = LHS.getValueType();
825 EVT RHSVT = RHS.getValueType();
826
827 if (LHSVT == MVT::v2i16) {
Krzysztof Parzyszekb2c458e2018-01-25 18:07:27 +0000828 assert(CC == ISD::SETEQ || CC == ISD::SETNE ||
829 ISD::isSignedIntSetCC(CC) || ISD::isUnsignedIntSetCC(CC));
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000830 unsigned ExtOpc = ISD::isSignedIntSetCC(CC) ? ISD::SIGN_EXTEND
831 : ISD::ZERO_EXTEND;
832 SDValue LX = DAG.getNode(ExtOpc, dl, MVT::v2i32, LHS);
833 SDValue RX = DAG.getNode(ExtOpc, dl, MVT::v2i32, RHS);
834 SDValue SC = DAG.getNode(ISD::SETCC, dl, MVT::v2i1, LX, RX, Cmp);
835 return SC;
836 }
837
838 // Treat all other vector types as legal.
839 if (VT.isVector())
840 return Op;
841
842 // Equals and not equals should use sign-extend, not zero-extend, since
843 // we can represent small negative values in the compare instructions.
844 // The LLVM default is to use zero-extend arbitrarily in these cases.
845 if ((CC == ISD::SETEQ || CC == ISD::SETNE) &&
846 (RHSVT == MVT::i8 || RHSVT == MVT::i16) &&
847 (LHSVT == MVT::i8 || LHSVT == MVT::i16)) {
848 ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS);
849 if (C && C->getAPIntValue().isNegative()) {
850 LHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, LHS);
851 RHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, RHS);
852 return DAG.getNode(ISD::SETCC, dl, Op.getValueType(),
853 LHS, RHS, Op.getOperand(2));
854 }
855 if (isSExtFree(LHS) || isSExtFree(RHS)) {
856 LHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, LHS);
857 RHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, RHS);
858 return DAG.getNode(ISD::SETCC, dl, Op.getValueType(),
859 LHS, RHS, Op.getOperand(2));
860 }
861 }
862 return SDValue();
863}
864
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000865SDValue
866HexagonTargetLowering::LowerVSELECT(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +0000867 SDValue PredOp = Op.getOperand(0);
868 SDValue Op1 = Op.getOperand(1), Op2 = Op.getOperand(2);
869 EVT OpVT = Op1.getValueType();
870 SDLoc DL(Op);
871
872 if (OpVT == MVT::v2i16) {
873 SDValue X1 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i32, Op1);
874 SDValue X2 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i32, Op2);
875 SDValue SL = DAG.getNode(ISD::VSELECT, DL, MVT::v2i32, PredOp, X1, X2);
876 SDValue TR = DAG.getNode(ISD::TRUNCATE, DL, MVT::v2i16, SL);
877 return TR;
878 }
879
880 return SDValue();
881}
882
Krzysztof Parzyszek91ff5c62017-08-01 13:12:53 +0000883static Constant *convert_i1_to_i8(const Constant *ConstVal) {
884 SmallVector<Constant *, 128> NewConst;
885 const ConstantVector *CV = dyn_cast<ConstantVector>(ConstVal);
886 if (!CV)
887 return nullptr;
888
889 LLVMContext &Ctx = ConstVal->getContext();
890 IRBuilder<> IRB(Ctx);
891 unsigned NumVectorElements = CV->getNumOperands();
892 assert(isPowerOf2_32(NumVectorElements) &&
893 "conversion only supported for pow2 VectorSize!");
894
895 for (unsigned i = 0; i < NumVectorElements / 8; ++i) {
896 uint8_t x = 0;
897 for (unsigned j = 0; j < 8; ++j) {
898 uint8_t y = CV->getOperand(i * 8 + j)->getUniqueInteger().getZExtValue();
899 x |= y << (7 - j);
900 }
901 assert((x == 0 || x == 255) && "Either all 0's or all 1's expected!");
902 NewConst.push_back(IRB.getInt8(x));
903 }
904 return ConstantVector::get(NewConst);
905}
906
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000907SDValue
Sirish Pande69295b82012-05-10 20:20:25 +0000908HexagonTargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
909 EVT ValTy = Op.getValueType();
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000910 ConstantPoolSDNode *CPN = cast<ConstantPoolSDNode>(Op);
Krzysztof Parzyszek91ff5c62017-08-01 13:12:53 +0000911 Constant *CVal = nullptr;
912 bool isVTi1Type = false;
913 if (const Constant *ConstVal = dyn_cast<Constant>(CPN->getConstVal())) {
914 Type *CValTy = ConstVal->getType();
915 if (CValTy->isVectorTy() &&
916 CValTy->getVectorElementType()->isIntegerTy(1)) {
917 CVal = convert_i1_to_i8(ConstVal);
918 isVTi1Type = (CVal != nullptr);
919 }
920 }
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000921 unsigned Align = CPN->getAlignment();
Rafael Espindola405e25a2016-06-26 22:24:01 +0000922 bool IsPositionIndependent = isPositionIndependent();
923 unsigned char TF = IsPositionIndependent ? HexagonII::MO_PCREL : 0;
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000924
Ron Lieberman822ee882016-08-13 23:41:11 +0000925 unsigned Offset = 0;
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000926 SDValue T;
927 if (CPN->isMachineConstantPoolEntry())
Ron Lieberman822ee882016-08-13 23:41:11 +0000928 T = DAG.getTargetConstantPool(CPN->getMachineCPVal(), ValTy, Align, Offset,
929 TF);
Krzysztof Parzyszek91ff5c62017-08-01 13:12:53 +0000930 else if (isVTi1Type)
931 T = DAG.getTargetConstantPool(CVal, ValTy, Align, Offset, TF);
Sirish Pande69295b82012-05-10 20:20:25 +0000932 else
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +0000933 T = DAG.getTargetConstantPool(CPN->getConstVal(), ValTy, Align, Offset, TF);
Ron Lieberman822ee882016-08-13 23:41:11 +0000934
935 assert(cast<ConstantPoolSDNode>(T)->getTargetFlags() == TF &&
936 "Inconsistent target flag encountered");
937
Rafael Espindola405e25a2016-06-26 22:24:01 +0000938 if (IsPositionIndependent)
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000939 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), ValTy, T);
940 return DAG.getNode(HexagonISD::CP, SDLoc(Op), ValTy, T);
941}
942
943SDValue
944HexagonTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
945 EVT VT = Op.getValueType();
946 int Idx = cast<JumpTableSDNode>(Op)->getIndex();
Rafael Espindola405e25a2016-06-26 22:24:01 +0000947 if (isPositionIndependent()) {
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000948 SDValue T = DAG.getTargetJumpTable(Idx, VT, HexagonII::MO_PCREL);
949 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), VT, T);
950 }
951
952 SDValue T = DAG.getTargetJumpTable(Idx, VT);
953 return DAG.getNode(HexagonISD::JT, SDLoc(Op), VT, T);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000954}
955
956SDValue
957HexagonTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000958 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000959 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +0000960 MachineFrameInfo &MFI = MF.getFrameInfo();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000961 MFI.setReturnAddressIsTaken(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000962
Bill Wendling908bf812014-01-06 00:43:20 +0000963 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +0000964 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +0000965
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000966 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000967 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000968 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
969 if (Depth) {
970 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000971 SDValue Offset = DAG.getConstant(4, dl, MVT::i32);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000972 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
973 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Justin Lebar9c375812016-07-15 18:27:10 +0000974 MachinePointerInfo());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000975 }
976
977 // Return LR, which contains the return address. Mark it an implicit live-in.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000978 unsigned Reg = MF.addLiveIn(HRI.getRARegister(), getRegClassFor(MVT::i32));
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000979 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
980}
981
982SDValue
983HexagonTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000984 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
Matthias Braun941a7052016-07-28 18:40:00 +0000985 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000986 MFI.setFrameAddressIsTaken(true);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000987
988 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000989 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000990 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
991 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +0000992 HRI.getFrameRegister(), VT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000993 while (Depth--)
994 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
Justin Lebar9c375812016-07-15 18:27:10 +0000995 MachinePointerInfo());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000996 return FrameAddr;
997}
998
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +0000999SDValue
1000HexagonTargetLowering::LowerATOMIC_FENCE(SDValue Op, SelectionDAG& DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001001 SDLoc dl(Op);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001002 return DAG.getNode(HexagonISD::BARRIER, dl, MVT::Other, Op.getOperand(0));
1003}
1004
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001005SDValue
1006HexagonTargetLowering::LowerGLOBALADDRESS(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001007 SDLoc dl(Op);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001008 auto *GAN = cast<GlobalAddressSDNode>(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00001009 auto PtrVT = getPointerTy(DAG.getDataLayout());
Krzysztof Parzyszek96a28412018-01-30 18:10:27 +00001010 auto *GV = GAN->getGlobal();
1011 int64_t Offset = GAN->getOffset();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001012
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001013 auto &HLOF = *HTM.getObjFileLowering();
1014 Reloc::Model RM = HTM.getRelocationModel();
1015
1016 if (RM == Reloc::Static) {
1017 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, Offset);
Peter Collingbourne67335642016-10-24 19:23:39 +00001018 const GlobalObject *GO = GV->getBaseObject();
1019 if (GO && HLOF.isGlobalInSmallSection(GO, HTM))
Krzysztof Parzyszek96a28412018-01-30 18:10:27 +00001020 return DAG.getNode(HexagonISD::CONST32_GP, dl, PtrVT, GA);
1021 return DAG.getNode(HexagonISD::CONST32, dl, PtrVT, GA);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001022 }
1023
Krzysztof Parzyszek96a28412018-01-30 18:10:27 +00001024 bool UsePCRel = getTargetMachine().shouldAssumeDSOLocal(*GV->getParent(), GV);
1025 if (UsePCRel) {
1026 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, Offset,
1027 HexagonII::MO_PCREL);
1028 return DAG.getNode(HexagonISD::AT_PCREL, dl, PtrVT, GA);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001029 }
1030
Krzysztof Parzyszek96a28412018-01-30 18:10:27 +00001031 // Use GOT index.
1032 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
1033 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, HexagonII::MO_GOT);
1034 SDValue Off = DAG.getConstant(Offset, dl, MVT::i32);
1035 return DAG.getNode(HexagonISD::AT_GOT, dl, PtrVT, GOT, GA, Off);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001036}
1037
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001038// Specifies that for loads and stores VT can be promoted to PromotedLdStVT.
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001039SDValue
1040HexagonTargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
1041 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Andrew Trickef9de2a2013-05-25 02:42:55 +00001042 SDLoc dl(Op);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001043 EVT PtrVT = getPointerTy(DAG.getDataLayout());
1044
1045 Reloc::Model RM = HTM.getRelocationModel();
1046 if (RM == Reloc::Static) {
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001047 SDValue A = DAG.getTargetBlockAddress(BA, PtrVT);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001048 return DAG.getNode(HexagonISD::CONST32_GP, dl, PtrVT, A);
1049 }
1050
1051 SDValue A = DAG.getTargetBlockAddress(BA, PtrVT, 0, HexagonII::MO_PCREL);
1052 return DAG.getNode(HexagonISD::AT_PCREL, dl, PtrVT, A);
1053}
1054
1055SDValue
1056HexagonTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op, SelectionDAG &DAG)
1057 const {
1058 EVT PtrVT = getPointerTy(DAG.getDataLayout());
1059 SDValue GOTSym = DAG.getTargetExternalSymbol(HEXAGON_GOT_SYM_NAME, PtrVT,
1060 HexagonII::MO_PCREL);
1061 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Op), PtrVT, GOTSym);
Jyotsna Verma2ba0c0b2013-03-07 19:10:28 +00001062}
1063
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001064SDValue
1065HexagonTargetLowering::GetDynamicTLSAddr(SelectionDAG &DAG, SDValue Chain,
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001066 GlobalAddressSDNode *GA, SDValue Glue, EVT PtrVT, unsigned ReturnReg,
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001067 unsigned char OperandFlags) const {
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001068 MachineFunction &MF = DAG.getMachineFunction();
1069 MachineFrameInfo &MFI = MF.getFrameInfo();
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001070 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1071 SDLoc dl(GA);
1072 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
1073 GA->getValueType(0),
1074 GA->getOffset(),
1075 OperandFlags);
1076 // Create Operands for the call.The Operands should have the following:
1077 // 1. Chain SDValue
1078 // 2. Callee which in this case is the Global address value.
1079 // 3. Registers live into the call.In this case its R0, as we
1080 // have just one argument to be passed.
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001081 // 4. Glue.
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001082 // Note: The order is important.
1083
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001084 const auto &HRI = *Subtarget.getRegisterInfo();
1085 const uint32_t *Mask = HRI.getCallPreservedMask(MF, CallingConv::C);
1086 assert(Mask && "Missing call preserved mask for calling convention");
1087 SDValue Ops[] = { Chain, TGA, DAG.getRegister(Hexagon::R0, PtrVT),
1088 DAG.getRegisterMask(Mask), Glue };
1089 Chain = DAG.getNode(HexagonISD::CALL, dl, NodeTys, Ops);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001090
1091 // Inform MFI that function has calls.
Matthias Braun941a7052016-07-28 18:40:00 +00001092 MFI.setAdjustsStack(true);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001093
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001094 Glue = Chain.getValue(1);
1095 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Glue);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001096}
1097
1098//
1099// Lower using the intial executable model for TLS addresses
1100//
1101SDValue
1102HexagonTargetLowering::LowerToTLSInitialExecModel(GlobalAddressSDNode *GA,
1103 SelectionDAG &DAG) const {
1104 SDLoc dl(GA);
1105 int64_t Offset = GA->getOffset();
1106 auto PtrVT = getPointerTy(DAG.getDataLayout());
1107
1108 // Get the thread pointer.
1109 SDValue TP = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Hexagon::UGP, PtrVT);
1110
Rafael Espindola405e25a2016-06-26 22:24:01 +00001111 bool IsPositionIndependent = isPositionIndependent();
1112 unsigned char TF =
1113 IsPositionIndependent ? HexagonII::MO_IEGOT : HexagonII::MO_IE;
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001114
1115 // First generate the TLS symbol address
1116 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT,
1117 Offset, TF);
1118
1119 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1120
Rafael Espindola405e25a2016-06-26 22:24:01 +00001121 if (IsPositionIndependent) {
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001122 // Generate the GOT pointer in case of position independent code
1123 SDValue GOT = LowerGLOBAL_OFFSET_TABLE(Sym, DAG);
1124
1125 // Add the TLS Symbol address to GOT pointer.This gives
1126 // GOT relative relocation for the symbol.
1127 Sym = DAG.getNode(ISD::ADD, dl, PtrVT, GOT, Sym);
1128 }
1129
1130 // Load the offset value for TLS symbol.This offset is relative to
1131 // thread pointer.
Justin Lebar9c375812016-07-15 18:27:10 +00001132 SDValue LoadOffset =
1133 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Sym, MachinePointerInfo());
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001134
1135 // Address of the thread local variable is the add of thread
1136 // pointer and the offset of the variable.
1137 return DAG.getNode(ISD::ADD, dl, PtrVT, TP, LoadOffset);
1138}
1139
1140//
1141// Lower using the local executable model for TLS addresses
1142//
1143SDValue
1144HexagonTargetLowering::LowerToTLSLocalExecModel(GlobalAddressSDNode *GA,
1145 SelectionDAG &DAG) const {
1146 SDLoc dl(GA);
1147 int64_t Offset = GA->getOffset();
1148 auto PtrVT = getPointerTy(DAG.getDataLayout());
1149
1150 // Get the thread pointer.
1151 SDValue TP = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Hexagon::UGP, PtrVT);
1152 // Generate the TLS symbol address
1153 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT, Offset,
1154 HexagonII::MO_TPREL);
1155 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1156
1157 // Address of the thread local variable is the add of thread
1158 // pointer and the offset of the variable.
1159 return DAG.getNode(ISD::ADD, dl, PtrVT, TP, Sym);
1160}
1161
1162//
1163// Lower using the general dynamic model for TLS addresses
1164//
1165SDValue
1166HexagonTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
1167 SelectionDAG &DAG) const {
1168 SDLoc dl(GA);
1169 int64_t Offset = GA->getOffset();
1170 auto PtrVT = getPointerTy(DAG.getDataLayout());
1171
1172 // First generate the TLS symbol address
1173 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, PtrVT, Offset,
1174 HexagonII::MO_GDGOT);
1175
1176 // Then, generate the GOT pointer
1177 SDValue GOT = LowerGLOBAL_OFFSET_TABLE(TGA, DAG);
1178
1179 // Add the TLS symbol and the GOT pointer
1180 SDValue Sym = DAG.getNode(HexagonISD::CONST32, dl, PtrVT, TGA);
1181 SDValue Chain = DAG.getNode(ISD::ADD, dl, PtrVT, GOT, Sym);
1182
1183 // Copy over the argument to R0
1184 SDValue InFlag;
1185 Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, Hexagon::R0, Chain, InFlag);
1186 InFlag = Chain.getValue(1);
1187
Krzysztof Parzyszeka7503832017-05-02 18:15:33 +00001188 unsigned Flags =
1189 static_cast<const HexagonSubtarget &>(DAG.getSubtarget()).useLongCalls()
1190 ? HexagonII::MO_GDPLT | HexagonII::HMOTF_ConstExtended
1191 : HexagonII::MO_GDPLT;
1192
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001193 return GetDynamicTLSAddr(DAG, Chain, GA, InFlag, PtrVT,
Krzysztof Parzyszeka7503832017-05-02 18:15:33 +00001194 Hexagon::R0, Flags);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001195}
1196
1197//
1198// Lower TLS addresses.
1199//
1200// For now for dynamic models, we only support the general dynamic model.
1201//
1202SDValue
1203HexagonTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1204 SelectionDAG &DAG) const {
1205 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1206
1207 switch (HTM.getTLSModel(GA->getGlobal())) {
1208 case TLSModel::GeneralDynamic:
1209 case TLSModel::LocalDynamic:
1210 return LowerToTLSGeneralDynamicModel(GA, DAG);
1211 case TLSModel::InitialExec:
1212 return LowerToTLSInitialExecModel(GA, DAG);
1213 case TLSModel::LocalExec:
1214 return LowerToTLSLocalExecModel(GA, DAG);
1215 }
1216 llvm_unreachable("Bogus TLS model");
1217}
1218
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001219//===----------------------------------------------------------------------===//
1220// TargetLowering Implementation
1221//===----------------------------------------------------------------------===//
1222
Eric Christopherd737b762015-02-02 22:11:36 +00001223HexagonTargetLowering::HexagonTargetLowering(const TargetMachine &TM,
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001224 const HexagonSubtarget &ST)
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001225 : TargetLowering(TM), HTM(static_cast<const HexagonTargetMachine&>(TM)),
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001226 Subtarget(ST) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001227 bool IsV4 = !Subtarget.hasV5TOps();
1228 auto &HRI = *Subtarget.getRegisterInfo();
Sirish Pande69295b82012-05-10 20:20:25 +00001229
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001230 setPrefLoopAlignment(4);
1231 setPrefFunctionAlignment(4);
1232 setMinFunctionAlignment(2);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001233 setStackPointerRegisterToSaveRestore(HRI.getStackRegister());
Krzysztof Parzyszekb3e50ac2018-01-05 20:41:50 +00001234 setBooleanContents(TargetLoweringBase::UndefinedBooleanContent);
1235 setBooleanVectorContents(TargetLoweringBase::UndefinedBooleanContent);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001236
Krzysztof Parzyszekf228c952016-06-22 16:07:10 +00001237 setMaxAtomicSizeInBitsSupported(64);
1238 setMinCmpXchgSizeInBits(32);
1239
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001240 if (EnableHexSDNodeSched)
1241 setSchedulingPreference(Sched::VLIW);
1242 else
1243 setSchedulingPreference(Sched::Source);
1244
1245 // Limits for inline expansion of memcpy/memmove
1246 MaxStoresPerMemcpy = MaxStoresPerMemcpyCL;
1247 MaxStoresPerMemcpyOptSize = MaxStoresPerMemcpyOptSizeCL;
1248 MaxStoresPerMemmove = MaxStoresPerMemmoveCL;
1249 MaxStoresPerMemmoveOptSize = MaxStoresPerMemmoveOptSizeCL;
1250 MaxStoresPerMemset = MaxStoresPerMemsetCL;
1251 MaxStoresPerMemsetOptSize = MaxStoresPerMemsetOptSizeCL;
1252
1253 //
1254 // Set up register classes.
1255 //
1256
1257 addRegisterClass(MVT::i1, &Hexagon::PredRegsRegClass);
1258 addRegisterClass(MVT::v2i1, &Hexagon::PredRegsRegClass); // bbbbaaaa
1259 addRegisterClass(MVT::v4i1, &Hexagon::PredRegsRegClass); // ddccbbaa
1260 addRegisterClass(MVT::v8i1, &Hexagon::PredRegsRegClass); // hgfedcba
1261 addRegisterClass(MVT::i32, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001262 addRegisterClass(MVT::v2i16, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00001263 addRegisterClass(MVT::v4i8, &Hexagon::IntRegsRegClass);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001264 addRegisterClass(MVT::i64, &Hexagon::DoubleRegsRegClass);
1265 addRegisterClass(MVT::v8i8, &Hexagon::DoubleRegsRegClass);
1266 addRegisterClass(MVT::v4i16, &Hexagon::DoubleRegsRegClass);
1267 addRegisterClass(MVT::v2i32, &Hexagon::DoubleRegsRegClass);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001268
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001269 if (Subtarget.hasV5TOps()) {
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001270 addRegisterClass(MVT::f32, &Hexagon::IntRegsRegClass);
1271 addRegisterClass(MVT::f64, &Hexagon::DoubleRegsRegClass);
1272 }
Sirish Pande69295b82012-05-10 20:20:25 +00001273
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001274 //
1275 // Handling of scalar operations.
1276 //
1277 // All operations default to "legal", except:
1278 // - indexed loads and stores (pre-/post-incremented),
1279 // - ANY_EXTEND_VECTOR_INREG, ATOMIC_CMP_SWAP_WITH_SUCCESS, CONCAT_VECTORS,
1280 // ConstantFP, DEBUGTRAP, FCEIL, FCOPYSIGN, FEXP, FEXP2, FFLOOR, FGETSIGN,
1281 // FLOG, FLOG2, FLOG10, FMAXNUM, FMINNUM, FNEARBYINT, FRINT, FROUND, TRAP,
1282 // FTRUNC, PREFETCH, SIGN_EXTEND_VECTOR_INREG, ZERO_EXTEND_VECTOR_INREG,
1283 // which default to "expand" for at least one type.
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001284
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001285 // Misc operations.
1286 setOperationAction(ISD::ConstantFP, MVT::f32, Legal); // Default: expand
1287 setOperationAction(ISD::ConstantFP, MVT::f64, Legal); // Default: expand
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001288
1289 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001290 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001291 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001292 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
1293 setOperationAction(ISD::INLINEASM, MVT::Other, Custom);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00001294 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +00001295 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00001296 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001297 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001298 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00001299 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001300 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001301
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001302 // Custom legalize GlobalAddress nodes into CONST32.
1303 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001304 setOperationAction(ISD::GlobalAddress, MVT::i8, Custom);
1305 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001306
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001307 // Hexagon needs to optimize cases with negative constants.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001308 setOperationAction(ISD::SETCC, MVT::i8, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001309 setOperationAction(ISD::SETCC, MVT::i16, Custom);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001310
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001311 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
1312 setOperationAction(ISD::VASTART, MVT::Other, Custom);
1313 setOperationAction(ISD::VAEND, MVT::Other, Expand);
1314 setOperationAction(ISD::VAARG, MVT::Other, Expand);
Krzysztof Parzyszekf6088122018-03-02 18:35:57 +00001315 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001316
1317 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
1318 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
1319 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
1320
1321 if (EmitJumpTables)
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001322 setMinimumJumpTableEntries(MinimumJumpTables);
Krzysztof Parzyszeka61f7da2016-01-13 21:43:13 +00001323 else
Eugene Zelenko58655bb2016-12-17 01:09:05 +00001324 setMinimumJumpTableEntries(std::numeric_limits<int>::max());
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00001325 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001326
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001327 // Hexagon has instructions for add/sub with carry. The problem with
1328 // modeling these instructions is that they produce 2 results: Rdd and Px.
1329 // To model the update of Px, we will have to use Defs[p0..p3] which will
1330 // cause any predicate live range to spill. So, we pretend we dont't have
1331 // these instructions.
1332 setOperationAction(ISD::ADDE, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001333 setOperationAction(ISD::ADDE, MVT::i16, Expand);
1334 setOperationAction(ISD::ADDE, MVT::i32, Expand);
1335 setOperationAction(ISD::ADDE, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001336 setOperationAction(ISD::SUBE, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001337 setOperationAction(ISD::SUBE, MVT::i16, Expand);
1338 setOperationAction(ISD::SUBE, MVT::i32, Expand);
1339 setOperationAction(ISD::SUBE, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001340 setOperationAction(ISD::ADDC, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001341 setOperationAction(ISD::ADDC, MVT::i16, Expand);
1342 setOperationAction(ISD::ADDC, MVT::i32, Expand);
1343 setOperationAction(ISD::ADDC, MVT::i64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001344 setOperationAction(ISD::SUBC, MVT::i8, Expand);
Eric Christopher6e9bcd12014-06-27 00:13:49 +00001345 setOperationAction(ISD::SUBC, MVT::i16, Expand);
1346 setOperationAction(ISD::SUBC, MVT::i32, Expand);
1347 setOperationAction(ISD::SUBC, MVT::i64, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001348
Krzysztof Parzyszek2c4487d2015-04-13 20:37:01 +00001349 // Only add and sub that detect overflow are the saturating ones.
1350 for (MVT VT : MVT::integer_valuetypes()) {
1351 setOperationAction(ISD::UADDO, VT, Expand);
1352 setOperationAction(ISD::SADDO, VT, Expand);
1353 setOperationAction(ISD::USUBO, VT, Expand);
1354 setOperationAction(ISD::SSUBO, VT, Expand);
1355 }
1356
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001357 setOperationAction(ISD::CTLZ, MVT::i8, Promote);
1358 setOperationAction(ISD::CTLZ, MVT::i16, Promote);
1359 setOperationAction(ISD::CTTZ, MVT::i8, Promote);
1360 setOperationAction(ISD::CTTZ, MVT::i16, Promote);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001361
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001362 // In V5, popcount can count # of 1s in i64 but returns i32.
1363 // On V4 it will be expanded (set later).
1364 setOperationAction(ISD::CTPOP, MVT::i8, Promote);
1365 setOperationAction(ISD::CTPOP, MVT::i16, Promote);
1366 setOperationAction(ISD::CTPOP, MVT::i32, Promote);
Krzysztof Parzyszekaf5ff652017-02-23 15:02:09 +00001367 setOperationAction(ISD::CTPOP, MVT::i64, Legal);
1368
1369 setOperationAction(ISD::BITREVERSE, MVT::i32, Legal);
1370 setOperationAction(ISD::BITREVERSE, MVT::i64, Legal);
1371 setOperationAction(ISD::BSWAP, MVT::i32, Legal);
1372 setOperationAction(ISD::BSWAP, MVT::i64, Legal);
Krzysztof Parzyszekef580172017-05-30 17:47:51 +00001373 setOperationAction(ISD::MUL, MVT::i64, Legal);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001374
Benjamin Kramer62460692015-04-25 14:46:53 +00001375 for (unsigned IntExpOp :
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001376 { ISD::SDIV, ISD::UDIV, ISD::SREM, ISD::UREM,
1377 ISD::SDIVREM, ISD::UDIVREM, ISD::ROTL, ISD::ROTR,
Krzysztof Parzyszekaf5ff652017-02-23 15:02:09 +00001378 ISD::SHL_PARTS, ISD::SRA_PARTS, ISD::SRL_PARTS,
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001379 ISD::SMUL_LOHI, ISD::UMUL_LOHI }) {
Benjamin Kramer62460692015-04-25 14:46:53 +00001380 setOperationAction(IntExpOp, MVT::i32, Expand);
1381 setOperationAction(IntExpOp, MVT::i64, Expand);
1382 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001383
Benjamin Kramer62460692015-04-25 14:46:53 +00001384 for (unsigned FPExpOp :
1385 {ISD::FDIV, ISD::FREM, ISD::FSQRT, ISD::FSIN, ISD::FCOS, ISD::FSINCOS,
1386 ISD::FPOW, ISD::FCOPYSIGN}) {
1387 setOperationAction(FPExpOp, MVT::f32, Expand);
1388 setOperationAction(FPExpOp, MVT::f64, Expand);
1389 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001390
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001391 // No extending loads from i32.
1392 for (MVT VT : MVT::integer_valuetypes()) {
1393 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand);
1394 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand);
1395 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand);
1396 }
1397 // Turn FP truncstore into trunc + store.
1398 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Michael Kuperstein2bc3d4d2016-08-18 20:08:15 +00001399 // Turn FP extload into load/fpextend.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001400 for (MVT VT : MVT::fp_valuetypes())
1401 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001402
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001403 // Expand BR_CC and SELECT_CC for all integer and fp types.
1404 for (MVT VT : MVT::integer_valuetypes()) {
1405 setOperationAction(ISD::BR_CC, VT, Expand);
1406 setOperationAction(ISD::SELECT_CC, VT, Expand);
1407 }
1408 for (MVT VT : MVT::fp_valuetypes()) {
1409 setOperationAction(ISD::BR_CC, VT, Expand);
1410 setOperationAction(ISD::SELECT_CC, VT, Expand);
1411 }
1412 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001413
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001414 //
1415 // Handling of vector operations.
1416 //
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001417
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001418 promoteLdStType(MVT::v4i8, MVT::i32);
1419 promoteLdStType(MVT::v2i16, MVT::i32);
1420 promoteLdStType(MVT::v8i8, MVT::i64);
Krzysztof Parzyszek5eef92e2017-07-17 15:45:45 +00001421 promoteLdStType(MVT::v4i16, MVT::i64);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001422 promoteLdStType(MVT::v2i32, MVT::i64);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001423
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001424 // Set the action for vector operations to "expand", then override it with
1425 // either "custom" or "legal" for specific cases.
Craig Topper26260942015-10-18 05:15:34 +00001426 static const unsigned VectExpOps[] = {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001427 // Integer arithmetic:
1428 ISD::ADD, ISD::SUB, ISD::MUL, ISD::SDIV, ISD::UDIV,
1429 ISD::SREM, ISD::UREM, ISD::SDIVREM, ISD::UDIVREM, ISD::ADDC,
1430 ISD::SUBC, ISD::SADDO, ISD::UADDO, ISD::SSUBO, ISD::USUBO,
1431 ISD::SMUL_LOHI, ISD::UMUL_LOHI,
1432 // Logical/bit:
1433 ISD::AND, ISD::OR, ISD::XOR, ISD::ROTL, ISD::ROTR,
Craig Topper33772c52016-04-28 03:34:31 +00001434 ISD::CTPOP, ISD::CTLZ, ISD::CTTZ,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001435 // Floating point arithmetic/math functions:
1436 ISD::FADD, ISD::FSUB, ISD::FMUL, ISD::FMA, ISD::FDIV,
1437 ISD::FREM, ISD::FNEG, ISD::FABS, ISD::FSQRT, ISD::FSIN,
Craig Topperf6d4dc52017-05-30 15:27:55 +00001438 ISD::FCOS, ISD::FPOW, ISD::FLOG, ISD::FLOG2,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001439 ISD::FLOG10, ISD::FEXP, ISD::FEXP2, ISD::FCEIL, ISD::FTRUNC,
1440 ISD::FRINT, ISD::FNEARBYINT, ISD::FROUND, ISD::FFLOOR,
1441 ISD::FMINNUM, ISD::FMAXNUM, ISD::FSINCOS,
1442 // Misc:
Krzysztof Parzyszek046da742016-10-27 14:30:16 +00001443 ISD::BR_CC, ISD::SELECT_CC, ISD::ConstantPool,
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001444 // Vector:
1445 ISD::BUILD_VECTOR, ISD::SCALAR_TO_VECTOR,
1446 ISD::EXTRACT_VECTOR_ELT, ISD::INSERT_VECTOR_ELT,
1447 ISD::EXTRACT_SUBVECTOR, ISD::INSERT_SUBVECTOR,
1448 ISD::CONCAT_VECTORS, ISD::VECTOR_SHUFFLE
1449 };
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001450
1451 for (MVT VT : MVT::vector_valuetypes()) {
Benjamin Kramer62460692015-04-25 14:46:53 +00001452 for (unsigned VectExpOp : VectExpOps)
1453 setOperationAction(VectExpOp, VT, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001454
Krzysztof Parzyszeka696b1b2016-09-08 17:42:14 +00001455 // Expand all extending loads and truncating stores:
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001456 for (MVT TargetVT : MVT::vector_valuetypes()) {
Krzysztof Parzyszeka696b1b2016-09-08 17:42:14 +00001457 if (TargetVT == VT)
1458 continue;
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001459 setLoadExtAction(ISD::EXTLOAD, TargetVT, VT, Expand);
Krzysztof Parzyszeka696b1b2016-09-08 17:42:14 +00001460 setLoadExtAction(ISD::ZEXTLOAD, TargetVT, VT, Expand);
1461 setLoadExtAction(ISD::SEXTLOAD, TargetVT, VT, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001462 setTruncStoreAction(VT, TargetVT, Expand);
1463 }
1464
Krzysztof Parzyszek046da742016-10-27 14:30:16 +00001465 // Normalize all inputs to SELECT to be vectors of i32.
1466 if (VT.getVectorElementType() != MVT::i32) {
1467 MVT VT32 = MVT::getVectorVT(MVT::i32, VT.getSizeInBits()/32);
1468 setOperationAction(ISD::SELECT, VT, Promote);
1469 AddPromotedToType(ISD::SELECT, VT, VT32);
1470 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001471 setOperationAction(ISD::SRA, VT, Custom);
1472 setOperationAction(ISD::SHL, VT, Custom);
1473 setOperationAction(ISD::SRL, VT, Custom);
1474 }
1475
Krzysztof Parzyszek64e5d7d2017-10-20 19:33:12 +00001476 // Extending loads from (native) vectors of i8 into (native) vectors of i16
1477 // are legal.
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001478 setLoadExtAction(ISD::EXTLOAD, MVT::v2i16, MVT::v2i8, Legal);
Krzysztof Parzyszek64e5d7d2017-10-20 19:33:12 +00001479 setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i16, MVT::v2i8, Legal);
1480 setLoadExtAction(ISD::SEXTLOAD, MVT::v2i16, MVT::v2i8, Legal);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001481 setLoadExtAction(ISD::EXTLOAD, MVT::v4i16, MVT::v4i8, Legal);
Krzysztof Parzyszek64e5d7d2017-10-20 19:33:12 +00001482 setLoadExtAction(ISD::ZEXTLOAD, MVT::v4i16, MVT::v4i8, Legal);
1483 setLoadExtAction(ISD::SEXTLOAD, MVT::v4i16, MVT::v4i8, Legal);
1484
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001485 // Types natively supported:
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001486 for (MVT NativeVT : {MVT::v8i1, MVT::v4i1, MVT::v2i1, MVT::v4i8,
1487 MVT::v8i8, MVT::v2i16, MVT::v4i16, MVT::v2i32}) {
Benjamin Kramer62460692015-04-25 14:46:53 +00001488 setOperationAction(ISD::BUILD_VECTOR, NativeVT, Custom);
1489 setOperationAction(ISD::EXTRACT_VECTOR_ELT, NativeVT, Custom);
1490 setOperationAction(ISD::INSERT_VECTOR_ELT, NativeVT, Custom);
1491 setOperationAction(ISD::EXTRACT_SUBVECTOR, NativeVT, Custom);
1492 setOperationAction(ISD::INSERT_SUBVECTOR, NativeVT, Custom);
1493 setOperationAction(ISD::CONCAT_VECTORS, NativeVT, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001494
Benjamin Kramer62460692015-04-25 14:46:53 +00001495 setOperationAction(ISD::ADD, NativeVT, Legal);
1496 setOperationAction(ISD::SUB, NativeVT, Legal);
1497 setOperationAction(ISD::MUL, NativeVT, Legal);
1498 setOperationAction(ISD::AND, NativeVT, Legal);
1499 setOperationAction(ISD::OR, NativeVT, Legal);
1500 setOperationAction(ISD::XOR, NativeVT, Legal);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001501 }
1502
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00001503 // Custom lower unaligned loads.
1504 for (MVT VecVT : {MVT::i32, MVT::v4i8, MVT::i64, MVT::v8i8,
1505 MVT::v2i16, MVT::v4i16, MVT::v2i32}) {
1506 setOperationAction(ISD::LOAD, VecVT, Custom);
1507 }
1508
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001509 // Custom-lower bitcasts from i8 to v8i1.
1510 setOperationAction(ISD::BITCAST, MVT::i8, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001511 setOperationAction(ISD::SETCC, MVT::v2i16, Custom);
1512 setOperationAction(ISD::VSELECT, MVT::v2i16, Custom);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001513 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i8, Custom);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001514 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
1515 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00001516
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001517 // Subtarget-specific operation actions.
1518 //
1519 if (Subtarget.hasV5TOps()) {
1520 setOperationAction(ISD::FMA, MVT::f64, Expand);
1521 setOperationAction(ISD::FADD, MVT::f64, Expand);
1522 setOperationAction(ISD::FSUB, MVT::f64, Expand);
1523 setOperationAction(ISD::FMUL, MVT::f64, Expand);
1524
Krzysztof Parzyszekbd8ef4b2016-08-19 13:34:31 +00001525 setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
1526 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
1527
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001528 setOperationAction(ISD::FP_TO_UINT, MVT::i1, Promote);
1529 setOperationAction(ISD::FP_TO_UINT, MVT::i8, Promote);
1530 setOperationAction(ISD::FP_TO_UINT, MVT::i16, Promote);
1531 setOperationAction(ISD::FP_TO_SINT, MVT::i1, Promote);
1532 setOperationAction(ISD::FP_TO_SINT, MVT::i8, Promote);
1533 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
1534 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Promote);
1535 setOperationAction(ISD::UINT_TO_FP, MVT::i8, Promote);
1536 setOperationAction(ISD::UINT_TO_FP, MVT::i16, Promote);
1537 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Promote);
1538 setOperationAction(ISD::SINT_TO_FP, MVT::i8, Promote);
1539 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001540 } else { // V4
1541 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
1542 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Expand);
1543 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
1544 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
1545 setOperationAction(ISD::FP_TO_SINT, MVT::f64, Expand);
1546 setOperationAction(ISD::FP_TO_SINT, MVT::f32, Expand);
1547 setOperationAction(ISD::FP_EXTEND, MVT::f32, Expand);
1548 setOperationAction(ISD::FP_ROUND, MVT::f64, Expand);
1549 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
1550
1551 setOperationAction(ISD::CTPOP, MVT::i8, Expand);
1552 setOperationAction(ISD::CTPOP, MVT::i16, Expand);
1553 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
1554 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
1555
1556 // Expand these operations for both f32 and f64:
Benjamin Kramer62460692015-04-25 14:46:53 +00001557 for (unsigned FPExpOpV4 :
1558 {ISD::FADD, ISD::FSUB, ISD::FMUL, ISD::FABS, ISD::FNEG, ISD::FMA}) {
1559 setOperationAction(FPExpOpV4, MVT::f32, Expand);
1560 setOperationAction(FPExpOpV4, MVT::f64, Expand);
1561 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001562
Benjamin Kramer62460692015-04-25 14:46:53 +00001563 for (ISD::CondCode FPExpCCV4 :
1564 {ISD::SETOEQ, ISD::SETOGT, ISD::SETOLT, ISD::SETOGE, ISD::SETOLE,
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00001565 ISD::SETUO, ISD::SETO}) {
Benjamin Kramer62460692015-04-25 14:46:53 +00001566 setCondCodeAction(FPExpCCV4, MVT::f32, Expand);
1567 setCondCodeAction(FPExpCCV4, MVT::f64, Expand);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001568 }
1569 }
1570
1571 // Handling of indexed loads/stores: default is "expand".
1572 //
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00001573 for (MVT VT : {MVT::i8, MVT::i16, MVT::i32, MVT::i64, MVT::v2i16,
1574 MVT::v2i32, MVT::v4i8, MVT::v4i16, MVT::v8i8}) {
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +00001575 setIndexedLoadAction(ISD::POST_INC, VT, Legal);
1576 setIndexedStoreAction(ISD::POST_INC, VT, Legal);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001577 }
1578
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00001579 if (Subtarget.useHVXOps())
1580 initializeHVXLowering();
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00001581
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001582 computeRegisterProperties(&HRI);
1583
1584 //
1585 // Library calls for unsupported operations
1586 //
1587 bool FastMath = EnableFastMath;
1588
Benjamin Kramera37c8092015-04-25 14:46:46 +00001589 setLibcallName(RTLIB::SDIV_I32, "__hexagon_divsi3");
1590 setLibcallName(RTLIB::SDIV_I64, "__hexagon_divdi3");
1591 setLibcallName(RTLIB::UDIV_I32, "__hexagon_udivsi3");
1592 setLibcallName(RTLIB::UDIV_I64, "__hexagon_udivdi3");
1593 setLibcallName(RTLIB::SREM_I32, "__hexagon_modsi3");
1594 setLibcallName(RTLIB::SREM_I64, "__hexagon_moddi3");
1595 setLibcallName(RTLIB::UREM_I32, "__hexagon_umodsi3");
1596 setLibcallName(RTLIB::UREM_I64, "__hexagon_umoddi3");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001597
Benjamin Kramera37c8092015-04-25 14:46:46 +00001598 setLibcallName(RTLIB::SINTTOFP_I128_F64, "__hexagon_floattidf");
1599 setLibcallName(RTLIB::SINTTOFP_I128_F32, "__hexagon_floattisf");
1600 setLibcallName(RTLIB::FPTOUINT_F32_I128, "__hexagon_fixunssfti");
1601 setLibcallName(RTLIB::FPTOUINT_F64_I128, "__hexagon_fixunsdfti");
1602 setLibcallName(RTLIB::FPTOSINT_F32_I128, "__hexagon_fixsfti");
1603 setLibcallName(RTLIB::FPTOSINT_F64_I128, "__hexagon_fixdfti");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001604
1605 if (IsV4) {
1606 // Handle single-precision floating point operations on V4.
Benjamin Kramera37c8092015-04-25 14:46:46 +00001607 if (FastMath) {
1608 setLibcallName(RTLIB::ADD_F32, "__hexagon_fast_addsf3");
1609 setLibcallName(RTLIB::SUB_F32, "__hexagon_fast_subsf3");
1610 setLibcallName(RTLIB::MUL_F32, "__hexagon_fast_mulsf3");
1611 setLibcallName(RTLIB::OGT_F32, "__hexagon_fast_gtsf2");
1612 setLibcallName(RTLIB::OLT_F32, "__hexagon_fast_ltsf2");
1613 // Double-precision compares.
1614 setLibcallName(RTLIB::OGT_F64, "__hexagon_fast_gtdf2");
1615 setLibcallName(RTLIB::OLT_F64, "__hexagon_fast_ltdf2");
1616 } else {
1617 setLibcallName(RTLIB::ADD_F32, "__hexagon_addsf3");
1618 setLibcallName(RTLIB::SUB_F32, "__hexagon_subsf3");
1619 setLibcallName(RTLIB::MUL_F32, "__hexagon_mulsf3");
1620 setLibcallName(RTLIB::OGT_F32, "__hexagon_gtsf2");
1621 setLibcallName(RTLIB::OLT_F32, "__hexagon_ltsf2");
1622 // Double-precision compares.
1623 setLibcallName(RTLIB::OGT_F64, "__hexagon_gtdf2");
1624 setLibcallName(RTLIB::OLT_F64, "__hexagon_ltdf2");
1625 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001626 }
1627
1628 // This is the only fast library function for sqrtd.
1629 if (FastMath)
Benjamin Kramera37c8092015-04-25 14:46:46 +00001630 setLibcallName(RTLIB::SQRT_F64, "__hexagon_fast2_sqrtdf2");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001631
Benjamin Kramera37c8092015-04-25 14:46:46 +00001632 // Prefix is: nothing for "slow-math",
1633 // "fast2_" for V4 fast-math and V5+ fast-math double-precision
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001634 // (actually, keep fast-math and fast-math2 separate for now)
Benjamin Kramera37c8092015-04-25 14:46:46 +00001635 if (FastMath) {
1636 setLibcallName(RTLIB::ADD_F64, "__hexagon_fast_adddf3");
1637 setLibcallName(RTLIB::SUB_F64, "__hexagon_fast_subdf3");
1638 setLibcallName(RTLIB::MUL_F64, "__hexagon_fast_muldf3");
1639 setLibcallName(RTLIB::DIV_F64, "__hexagon_fast_divdf3");
1640 // Calling __hexagon_fast2_divsf3 with fast-math on V5 (ok).
1641 setLibcallName(RTLIB::DIV_F32, "__hexagon_fast_divsf3");
1642 } else {
1643 setLibcallName(RTLIB::ADD_F64, "__hexagon_adddf3");
1644 setLibcallName(RTLIB::SUB_F64, "__hexagon_subdf3");
1645 setLibcallName(RTLIB::MUL_F64, "__hexagon_muldf3");
1646 setLibcallName(RTLIB::DIV_F64, "__hexagon_divdf3");
1647 setLibcallName(RTLIB::DIV_F32, "__hexagon_divsf3");
1648 }
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001649
1650 if (Subtarget.hasV5TOps()) {
1651 if (FastMath)
Benjamin Kramera37c8092015-04-25 14:46:46 +00001652 setLibcallName(RTLIB::SQRT_F32, "__hexagon_fast2_sqrtf");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001653 else
Benjamin Kramera37c8092015-04-25 14:46:46 +00001654 setLibcallName(RTLIB::SQRT_F32, "__hexagon_sqrtf");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001655 } else {
1656 // V4
Benjamin Kramera37c8092015-04-25 14:46:46 +00001657 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__hexagon_floatsisf");
1658 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__hexagon_floatsidf");
1659 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__hexagon_floatdisf");
1660 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__hexagon_floatdidf");
1661 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__hexagon_floatunsisf");
1662 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__hexagon_floatunsidf");
1663 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__hexagon_floatundisf");
1664 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__hexagon_floatundidf");
1665 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__hexagon_fixunssfsi");
1666 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__hexagon_fixunssfdi");
1667 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__hexagon_fixunsdfsi");
1668 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__hexagon_fixunsdfdi");
1669 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__hexagon_fixsfsi");
1670 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__hexagon_fixsfdi");
1671 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__hexagon_fixdfsi");
1672 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__hexagon_fixdfdi");
1673 setLibcallName(RTLIB::FPEXT_F32_F64, "__hexagon_extendsfdf2");
1674 setLibcallName(RTLIB::FPROUND_F64_F32, "__hexagon_truncdfsf2");
1675 setLibcallName(RTLIB::OEQ_F32, "__hexagon_eqsf2");
1676 setLibcallName(RTLIB::OEQ_F64, "__hexagon_eqdf2");
1677 setLibcallName(RTLIB::OGE_F32, "__hexagon_gesf2");
1678 setLibcallName(RTLIB::OGE_F64, "__hexagon_gedf2");
1679 setLibcallName(RTLIB::OLE_F32, "__hexagon_lesf2");
1680 setLibcallName(RTLIB::OLE_F64, "__hexagon_ledf2");
1681 setLibcallName(RTLIB::UNE_F32, "__hexagon_nesf2");
1682 setLibcallName(RTLIB::UNE_F64, "__hexagon_nedf2");
1683 setLibcallName(RTLIB::UO_F32, "__hexagon_unordsf2");
1684 setLibcallName(RTLIB::UO_F64, "__hexagon_unorddf2");
1685 setLibcallName(RTLIB::O_F32, "__hexagon_unordsf2");
1686 setLibcallName(RTLIB::O_F64, "__hexagon_unorddf2");
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001687 }
1688
1689 // These cause problems when the shift amount is non-constant.
1690 setLibcallName(RTLIB::SHL_I128, nullptr);
1691 setLibcallName(RTLIB::SRL_I128, nullptr);
1692 setLibcallName(RTLIB::SRA_I128, nullptr);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001693}
1694
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001695const char* HexagonTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00001696 switch ((HexagonISD::NodeType)Opcode) {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001697 case HexagonISD::ALLOCA: return "HexagonISD::ALLOCA";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001698 case HexagonISD::AT_GOT: return "HexagonISD::AT_GOT";
1699 case HexagonISD::AT_PCREL: return "HexagonISD::AT_PCREL";
1700 case HexagonISD::BARRIER: return "HexagonISD::BARRIER";
Krzysztof Parzyszekbe976d42016-08-12 11:12:02 +00001701 case HexagonISD::CALL: return "HexagonISD::CALL";
1702 case HexagonISD::CALLnr: return "HexagonISD::CALLnr";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001703 case HexagonISD::CALLR: return "HexagonISD::CALLR";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001704 case HexagonISD::COMBINE: return "HexagonISD::COMBINE";
1705 case HexagonISD::CONST32_GP: return "HexagonISD::CONST32_GP";
1706 case HexagonISD::CONST32: return "HexagonISD::CONST32";
1707 case HexagonISD::CP: return "HexagonISD::CP";
1708 case HexagonISD::DCFETCH: return "HexagonISD::DCFETCH";
1709 case HexagonISD::EH_RETURN: return "HexagonISD::EH_RETURN";
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001710 case HexagonISD::TSTBIT: return "HexagonISD::TSTBIT";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001711 case HexagonISD::EXTRACTU: return "HexagonISD::EXTRACTU";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001712 case HexagonISD::INSERT: return "HexagonISD::INSERT";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001713 case HexagonISD::JT: return "HexagonISD::JT";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001714 case HexagonISD::RET_FLAG: return "HexagonISD::RET_FLAG";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001715 case HexagonISD::TC_RETURN: return "HexagonISD::TC_RETURN";
Krzysztof Parzyszekf85dd9f2017-07-10 20:16:44 +00001716 case HexagonISD::VASL: return "HexagonISD::VASL";
1717 case HexagonISD::VASR: return "HexagonISD::VASR";
1718 case HexagonISD::VLSR: return "HexagonISD::VLSR";
1719 case HexagonISD::VSPLAT: return "HexagonISD::VSPLAT";
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001720 case HexagonISD::VEXTRACTW: return "HexagonISD::VEXTRACTW";
1721 case HexagonISD::VINSERTW0: return "HexagonISD::VINSERTW0";
1722 case HexagonISD::VROR: return "HexagonISD::VROR";
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +00001723 case HexagonISD::READCYCLE: return "HexagonISD::READCYCLE";
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00001724 case HexagonISD::VZERO: return "HexagonISD::VZERO";
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001725 case HexagonISD::D2P: return "HexagonISD::D2P";
1726 case HexagonISD::P2D: return "HexagonISD::P2D";
1727 case HexagonISD::V2Q: return "HexagonISD::V2Q";
1728 case HexagonISD::Q2V: return "HexagonISD::Q2V";
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00001729 case HexagonISD::QCAT: return "HexagonISD::QCAT";
Krzysztof Parzyszek69f1d7e2018-02-06 14:16:52 +00001730 case HexagonISD::QTRUE: return "HexagonISD::QTRUE";
1731 case HexagonISD::QFALSE: return "HexagonISD::QFALSE";
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001732 case HexagonISD::TYPECAST: return "HexagonISD::TYPECAST";
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00001733 case HexagonISD::VALIGN: return "HexagonISD::VALIGN";
Krzysztof Parzyszekad83ce42018-02-14 20:46:06 +00001734 case HexagonISD::VALIGNADDR: return "HexagonISD::VALIGNADDR";
Matthias Braund04893f2015-05-07 21:33:59 +00001735 case HexagonISD::OP_END: break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001736 }
Matthias Braund04893f2015-05-07 21:33:59 +00001737 return nullptr;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001738}
1739
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001740/// Given an intrinsic, checks if on the target the intrinsic will need to map
1741/// to a MemIntrinsicNode (touches memory). If this is the case, it returns
1742/// true and store the intrinsic information into the IntrinsicInfo that was
1743/// passed to the function.
1744bool HexagonTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
1745 const CallInst &I,
Matt Arsenault7d7adf42017-12-14 22:34:10 +00001746 MachineFunction &MF,
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001747 unsigned Intrinsic) const {
1748 switch (Intrinsic) {
1749 case Intrinsic::hexagon_V6_vgathermw:
1750 case Intrinsic::hexagon_V6_vgathermw_128B:
1751 case Intrinsic::hexagon_V6_vgathermh:
1752 case Intrinsic::hexagon_V6_vgathermh_128B:
1753 case Intrinsic::hexagon_V6_vgathermhw:
1754 case Intrinsic::hexagon_V6_vgathermhw_128B:
1755 case Intrinsic::hexagon_V6_vgathermwq:
1756 case Intrinsic::hexagon_V6_vgathermwq_128B:
1757 case Intrinsic::hexagon_V6_vgathermhq:
1758 case Intrinsic::hexagon_V6_vgathermhq_128B:
1759 case Intrinsic::hexagon_V6_vgathermhwq:
1760 case Intrinsic::hexagon_V6_vgathermhwq_128B: {
1761 const Module &M = *I.getParent()->getParent()->getParent();
1762 Info.opc = ISD::INTRINSIC_W_CHAIN;
1763 Type *VecTy = I.getArgOperand(1)->getType();
1764 Info.memVT = MVT::getVT(VecTy);
1765 Info.ptrVal = I.getArgOperand(0);
1766 Info.offset = 0;
1767 Info.align = M.getDataLayout().getTypeAllocSizeInBits(VecTy) / 8;
Matt Arsenault11171332017-12-14 21:39:51 +00001768 Info.flags = MachineMemOperand::MOLoad |
1769 MachineMemOperand::MOStore |
1770 MachineMemOperand::MOVolatile;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001771 return true;
1772 }
1773 default:
1774 break;
1775 }
1776 return false;
1777}
1778
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001779bool HexagonTargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00001780 return isTruncateFree(EVT::getEVT(Ty1), EVT::getEVT(Ty2));
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001781}
1782
1783bool HexagonTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00001784 if (!VT1.isSimple() || !VT2.isSimple())
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001785 return false;
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00001786 return VT1.getSimpleVT() == MVT::i64 && VT2.getSimpleVT() == MVT::i32;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00001787}
1788
Krzysztof Parzyszekbd8ef4b2016-08-19 13:34:31 +00001789bool HexagonTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
1790 return isOperationLegalOrCustom(ISD::FMA, VT);
1791}
1792
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001793// Should we expand the build vector with shuffles?
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00001794bool HexagonTargetLowering::shouldExpandBuildVectorWithShuffles(EVT VT,
1795 unsigned DefinedValues) const {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00001796 return false;
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00001797}
1798
Zvi Rackover1b736822017-07-26 08:06:58 +00001799bool HexagonTargetLowering::isShuffleMaskLegal(ArrayRef<int> Mask,
1800 EVT VT) const {
Krzysztof Parzyszekd19d0502016-09-13 21:16:07 +00001801 return true;
1802}
1803
Krzysztof Parzyszek5439a702017-12-18 18:21:01 +00001804TargetLoweringBase::LegalizeTypeAction
1805HexagonTargetLowering::getPreferredVectorAction(EVT VT) const {
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00001806 if (VT.getVectorNumElements() == 1)
1807 return TargetLoweringBase::TypeScalarizeVector;
1808
1809 // Always widen vectors of i1.
1810 MVT ElemTy = VT.getSimpleVT().getVectorElementType();
1811 if (ElemTy == MVT::i1)
1812 return TargetLoweringBase::TypeWidenVector;
1813
Krzysztof Parzyszek5439a702017-12-18 18:21:01 +00001814 if (Subtarget.useHVXOps()) {
1815 // If the size of VT is at least half of the vector length,
1816 // widen the vector. Note: the threshold was not selected in
1817 // any scientific way.
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00001818 ArrayRef<MVT> Tys = Subtarget.getHVXElementTypes();
1819 if (llvm::find(Tys, ElemTy) != Tys.end()) {
1820 unsigned HwWidth = 8*Subtarget.getVectorLength();
1821 unsigned VecWidth = VT.getSizeInBits();
1822 if (VecWidth >= HwWidth/2 && VecWidth < HwWidth)
1823 return TargetLoweringBase::TypeWidenVector;
1824 }
Krzysztof Parzyszek5439a702017-12-18 18:21:01 +00001825 }
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00001826 return TargetLoweringBase::TypeSplitVector;
Krzysztof Parzyszek5439a702017-12-18 18:21:01 +00001827}
1828
Krzysztof Parzyszekad83ce42018-02-14 20:46:06 +00001829std::pair<SDValue, int>
1830HexagonTargetLowering::getBaseAndOffset(SDValue Addr) const {
1831 if (Addr.getOpcode() == ISD::ADD) {
1832 SDValue Op1 = Addr.getOperand(1);
1833 if (auto *CN = dyn_cast<const ConstantSDNode>(Op1.getNode()))
1834 return { Addr.getOperand(0), CN->getSExtValue() };
1835 }
1836 return { Addr, 0 };
1837}
1838
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00001839// Lower a vector shuffle (V1, V2, V3). V1 and V2 are the two vectors
1840// to select data from, V3 is the permutation.
1841SDValue
1842HexagonTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG)
1843 const {
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001844 const auto *SVN = cast<ShuffleVectorSDNode>(Op);
1845 ArrayRef<int> AM = SVN->getMask();
1846 assert(AM.size() <= 8 && "Unexpected shuffle mask");
1847 unsigned VecLen = AM.size();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001848
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001849 MVT VecTy = ty(Op);
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00001850 assert(!Subtarget.isHVXVectorType(VecTy, true) &&
1851 "HVX shuffles should be legal");
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001852 assert(VecTy.getSizeInBits() <= 64 && "Unexpected vector length");
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001853
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001854 SDValue Op0 = Op.getOperand(0);
1855 SDValue Op1 = Op.getOperand(1);
Krzysztof Parzyszek7cfe7cb2018-02-09 15:30:02 +00001856 const SDLoc &dl(Op);
1857
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001858 // If the inputs are not the same as the output, bail. This is not an
1859 // error situation, but complicates the handling and the default expansion
1860 // (into BUILD_VECTOR) should be adequate.
1861 if (ty(Op0) != VecTy || ty(Op1) != VecTy)
1862 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001863
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001864 // Normalize the mask so that the first non-negative index comes from
1865 // the first operand.
1866 SmallVector<int,8> Mask(AM.begin(), AM.end());
1867 unsigned F = llvm::find_if(AM, [](int M) { return M >= 0; }) - AM.data();
1868 if (F == AM.size())
1869 return DAG.getUNDEF(VecTy);
1870 if (AM[F] >= int(VecLen)) {
1871 ShuffleVectorSDNode::commuteMask(Mask);
1872 std::swap(Op0, Op1);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001873 }
1874
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001875 // Express the shuffle mask in terms of bytes.
1876 SmallVector<int,8> ByteMask;
1877 unsigned ElemBytes = VecTy.getVectorElementType().getSizeInBits() / 8;
1878 for (unsigned i = 0, e = Mask.size(); i != e; ++i) {
1879 int M = Mask[i];
1880 if (M < 0) {
1881 for (unsigned j = 0; j != ElemBytes; ++j)
1882 ByteMask.push_back(-1);
1883 } else {
1884 for (unsigned j = 0; j != ElemBytes; ++j)
1885 ByteMask.push_back(M*ElemBytes + j);
1886 }
1887 }
1888 assert(ByteMask.size() <= 8);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001889
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001890 // All non-undef (non-negative) indexes are well within [0..127], so they
1891 // fit in a single byte. Build two 64-bit words:
1892 // - MaskIdx where each byte is the corresponding index (for non-negative
1893 // indexes), and 0xFF for negative indexes, and
1894 // - MaskUnd that has 0xFF for each negative index.
1895 uint64_t MaskIdx = 0;
1896 uint64_t MaskUnd = 0;
1897 for (unsigned i = 0, e = ByteMask.size(); i != e; ++i) {
1898 unsigned S = 8*i;
1899 uint64_t M = ByteMask[i] & 0xFF;
1900 if (M == 0xFF)
1901 MaskUnd |= M << S;
1902 MaskIdx |= M << S;
1903 }
1904
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001905 if (ByteMask.size() == 4) {
1906 // Identity.
1907 if (MaskIdx == (0x03020100 | MaskUnd))
1908 return Op0;
1909 // Byte swap.
1910 if (MaskIdx == (0x00010203 | MaskUnd)) {
1911 SDValue T0 = DAG.getBitcast(MVT::i32, Op0);
1912 SDValue T1 = DAG.getNode(ISD::BSWAP, dl, MVT::i32, T0);
1913 return DAG.getBitcast(VecTy, T1);
1914 }
1915
1916 // Byte packs.
1917 SDValue Concat10 = DAG.getNode(HexagonISD::COMBINE, dl,
1918 typeJoin({ty(Op1), ty(Op0)}), {Op1, Op0});
1919 if (MaskIdx == (0x06040200 | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001920 return getInstr(Hexagon::S2_vtrunehb, dl, VecTy, {Concat10}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001921 if (MaskIdx == (0x07050301 | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001922 return getInstr(Hexagon::S2_vtrunohb, dl, VecTy, {Concat10}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001923
1924 SDValue Concat01 = DAG.getNode(HexagonISD::COMBINE, dl,
1925 typeJoin({ty(Op0), ty(Op1)}), {Op0, Op1});
1926 if (MaskIdx == (0x02000604 | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001927 return getInstr(Hexagon::S2_vtrunehb, dl, VecTy, {Concat01}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001928 if (MaskIdx == (0x03010705 | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001929 return getInstr(Hexagon::S2_vtrunohb, dl, VecTy, {Concat01}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001930 }
1931
1932 if (ByteMask.size() == 8) {
1933 // Identity.
1934 if (MaskIdx == (0x0706050403020100ull | MaskUnd))
1935 return Op0;
1936 // Byte swap.
1937 if (MaskIdx == (0x0001020304050607ull | MaskUnd)) {
1938 SDValue T0 = DAG.getBitcast(MVT::i64, Op0);
1939 SDValue T1 = DAG.getNode(ISD::BSWAP, dl, MVT::i64, T0);
1940 return DAG.getBitcast(VecTy, T1);
1941 }
1942
1943 // Halfword picks.
1944 if (MaskIdx == (0x0d0c050409080100ull | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001945 return getInstr(Hexagon::S2_shuffeh, dl, VecTy, {Op1, Op0}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001946 if (MaskIdx == (0x0f0e07060b0a0302ull | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001947 return getInstr(Hexagon::S2_shuffoh, dl, VecTy, {Op1, Op0}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001948 if (MaskIdx == (0x0d0c090805040100ull | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001949 return getInstr(Hexagon::S2_vtrunewh, dl, VecTy, {Op1, Op0}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001950 if (MaskIdx == (0x0f0e0b0a07060302ull | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001951 return getInstr(Hexagon::S2_vtrunowh, dl, VecTy, {Op1, Op0}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001952 if (MaskIdx == (0x0706030205040100ull | MaskUnd)) {
1953 VectorPair P = opSplit(Op0, dl, DAG);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001954 return getInstr(Hexagon::S2_packhl, dl, VecTy, {P.second, P.first}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001955 }
1956
1957 // Byte packs.
1958 if (MaskIdx == (0x0e060c040a020800ull | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001959 return getInstr(Hexagon::S2_shuffeb, dl, VecTy, {Op1, Op0}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001960 if (MaskIdx == (0x0f070d050b030901ull | MaskUnd))
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00001961 return getInstr(Hexagon::S2_shuffob, dl, VecTy, {Op1, Op0}, DAG);
Krzysztof Parzyszekb8f2a1e2018-01-15 18:33:33 +00001962 }
1963
1964 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001965}
1966
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00001967// Create a Hexagon-specific node for shifting a vector by an integer.
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00001968SDValue
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00001969HexagonTargetLowering::getVectorShiftByInt(SDValue Op, SelectionDAG &DAG)
1970 const {
Krzysztof Parzyszek1108ee22018-01-31 20:49:24 +00001971 if (auto *BVN = dyn_cast<BuildVectorSDNode>(Op.getOperand(1).getNode())) {
1972 if (SDValue S = BVN->getSplatValue()) {
1973 unsigned NewOpc;
1974 switch (Op.getOpcode()) {
1975 case ISD::SHL:
1976 NewOpc = HexagonISD::VASL;
1977 break;
1978 case ISD::SRA:
1979 NewOpc = HexagonISD::VASR;
1980 break;
1981 case ISD::SRL:
1982 NewOpc = HexagonISD::VLSR;
1983 break;
1984 default:
1985 llvm_unreachable("Unexpected shift opcode");
1986 }
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00001987 return DAG.getNode(NewOpc, SDLoc(Op), ty(Op), Op.getOperand(0), S);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001988 }
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001989 }
1990
Krzysztof Parzyszek1108ee22018-01-31 20:49:24 +00001991 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00001992}
1993
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00001994SDValue
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00001995HexagonTargetLowering::LowerVECTOR_SHIFT(SDValue Op, SelectionDAG &DAG) const {
1996 return getVectorShiftByInt(Op, DAG);
1997}
1998
1999SDValue
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002000HexagonTargetLowering::LowerBITCAST(SDValue Op, SelectionDAG &DAG) const {
2001 MVT ResTy = ty(Op);
2002 SDValue InpV = Op.getOperand(0);
2003 MVT InpTy = ty(InpV);
2004 assert(ResTy.getSizeInBits() == InpTy.getSizeInBits());
2005 const SDLoc &dl(Op);
2006
2007 // Handle conversion from i8 to v8i1.
2008 if (ResTy == MVT::v8i1) {
2009 SDValue Sc = DAG.getBitcast(tyScalar(InpTy), InpV);
2010 SDValue Ext = DAG.getZExtOrTrunc(Sc, dl, MVT::i32);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002011 return getInstr(Hexagon::C2_tfrrp, dl, ResTy, Ext, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002012 }
2013
2014 return SDValue();
2015}
2016
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002017bool
2018HexagonTargetLowering::getBuildVectorConstInts(ArrayRef<SDValue> Values,
2019 MVT VecTy, SelectionDAG &DAG,
2020 MutableArrayRef<ConstantInt*> Consts) const {
2021 MVT ElemTy = VecTy.getVectorElementType();
2022 unsigned ElemWidth = ElemTy.getSizeInBits();
2023 IntegerType *IntTy = IntegerType::get(*DAG.getContext(), ElemWidth);
2024 bool AllConst = true;
2025
2026 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
2027 SDValue V = Values[i];
2028 if (V.isUndef()) {
2029 Consts[i] = ConstantInt::get(IntTy, 0);
2030 continue;
2031 }
Krzysztof Parzyszek4ef6cff2018-01-11 18:03:23 +00002032 // Make sure to always cast to IntTy.
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002033 if (auto *CN = dyn_cast<ConstantSDNode>(V.getNode())) {
2034 const ConstantInt *CI = CN->getConstantIntValue();
Krzysztof Parzyszek4ef6cff2018-01-11 18:03:23 +00002035 Consts[i] = ConstantInt::get(IntTy, CI->getValue().getSExtValue());
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002036 } else if (auto *CN = dyn_cast<ConstantFPSDNode>(V.getNode())) {
2037 const ConstantFP *CF = CN->getConstantFPValue();
2038 APInt A = CF->getValueAPF().bitcastToAPInt();
2039 Consts[i] = ConstantInt::get(IntTy, A.getZExtValue());
2040 } else {
2041 AllConst = false;
2042 }
2043 }
2044 return AllConst;
2045}
2046
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002047SDValue
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002048HexagonTargetLowering::buildVector32(ArrayRef<SDValue> Elem, const SDLoc &dl,
2049 MVT VecTy, SelectionDAG &DAG) const {
2050 MVT ElemTy = VecTy.getVectorElementType();
2051 assert(VecTy.getVectorNumElements() == Elem.size());
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002052
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002053 SmallVector<ConstantInt*,4> Consts(Elem.size());
2054 bool AllConst = getBuildVectorConstInts(Elem, VecTy, DAG, Consts);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002055
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002056 unsigned First, Num = Elem.size();
2057 for (First = 0; First != Num; ++First)
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002058 if (!isUndef(Elem[First]))
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002059 break;
2060 if (First == Num)
2061 return DAG.getUNDEF(VecTy);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002062
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002063 if (AllConst &&
2064 llvm::all_of(Consts, [](ConstantInt *CI) { return CI->isZero(); }))
2065 return getZero(dl, VecTy, DAG);
2066
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002067 if (ElemTy == MVT::i16) {
2068 assert(Elem.size() == 2);
2069 if (AllConst) {
2070 uint32_t V = (Consts[0]->getZExtValue() & 0xFFFF) |
2071 Consts[1]->getZExtValue() << 16;
2072 return DAG.getBitcast(MVT::v2i16, DAG.getConstant(V, dl, MVT::i32));
Krzysztof Parzyszek89b2d7c2017-07-13 18:17:58 +00002073 }
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002074 SDValue N = getInstr(Hexagon::A2_combine_ll, dl, MVT::i32,
2075 {Elem[1], Elem[0]}, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002076 return DAG.getBitcast(MVT::v2i16, N);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002077 }
2078
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002079 if (ElemTy == MVT::i8) {
2080 // First try generating a constant.
2081 if (AllConst) {
2082 int32_t V = (Consts[0]->getZExtValue() & 0xFF) |
2083 (Consts[1]->getZExtValue() & 0xFF) << 8 |
2084 (Consts[1]->getZExtValue() & 0xFF) << 16 |
2085 Consts[2]->getZExtValue() << 24;
2086 return DAG.getBitcast(MVT::v4i8, DAG.getConstant(V, dl, MVT::i32));
2087 }
2088
2089 // Then try splat.
2090 bool IsSplat = true;
2091 for (unsigned i = 0; i != Num; ++i) {
2092 if (i == First)
2093 continue;
2094 if (Elem[i] == Elem[First] || isUndef(Elem[i]))
2095 continue;
2096 IsSplat = false;
2097 break;
2098 }
2099 if (IsSplat) {
2100 // Legalize the operand to VSPLAT.
2101 SDValue Ext = DAG.getZExtOrTrunc(Elem[First], dl, MVT::i32);
2102 return DAG.getNode(HexagonISD::VSPLAT, dl, VecTy, Ext);
2103 }
2104
2105 // Generate
2106 // (zxtb(Elem[0]) | (zxtb(Elem[1]) << 8)) |
2107 // (zxtb(Elem[2]) | (zxtb(Elem[3]) << 8)) << 16
2108 assert(Elem.size() == 4);
2109 SDValue Vs[4];
2110 for (unsigned i = 0; i != 4; ++i) {
2111 Vs[i] = DAG.getZExtOrTrunc(Elem[i], dl, MVT::i32);
2112 Vs[i] = DAG.getZeroExtendInReg(Vs[i], dl, MVT::i8);
2113 }
2114 SDValue S8 = DAG.getConstant(8, dl, MVT::i32);
2115 SDValue T0 = DAG.getNode(ISD::SHL, dl, MVT::i32, {Vs[1], S8});
2116 SDValue T1 = DAG.getNode(ISD::SHL, dl, MVT::i32, {Vs[3], S8});
2117 SDValue B0 = DAG.getNode(ISD::OR, dl, MVT::i32, {Vs[0], T0});
2118 SDValue B1 = DAG.getNode(ISD::OR, dl, MVT::i32, {Vs[2], T1});
2119
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002120 SDValue R = getInstr(Hexagon::A2_combine_ll, dl, MVT::i32, {B1, B0}, DAG);
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002121 return DAG.getBitcast(MVT::v4i8, R);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002122 }
2123
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002124#ifndef NDEBUG
2125 dbgs() << "VecTy: " << EVT(VecTy).getEVTString() << '\n';
2126#endif
2127 llvm_unreachable("Unexpected vector element type");
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002128}
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002129
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002130SDValue
2131HexagonTargetLowering::buildVector64(ArrayRef<SDValue> Elem, const SDLoc &dl,
2132 MVT VecTy, SelectionDAG &DAG) const {
2133 MVT ElemTy = VecTy.getVectorElementType();
2134 assert(VecTy.getVectorNumElements() == Elem.size());
2135
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002136 SmallVector<ConstantInt*,8> Consts(Elem.size());
2137 bool AllConst = getBuildVectorConstInts(Elem, VecTy, DAG, Consts);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002138
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002139 unsigned First, Num = Elem.size();
2140 for (First = 0; First != Num; ++First)
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002141 if (!isUndef(Elem[First]))
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002142 break;
2143 if (First == Num)
2144 return DAG.getUNDEF(VecTy);
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002145
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002146 if (AllConst &&
2147 llvm::all_of(Consts, [](ConstantInt *CI) { return CI->isZero(); }))
2148 return getZero(dl, VecTy, DAG);
2149
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002150 // First try splat if possible.
2151 if (ElemTy == MVT::i16) {
2152 bool IsSplat = true;
2153 for (unsigned i = 0; i != Num; ++i) {
2154 if (i == First)
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002155 continue;
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002156 if (Elem[i] == Elem[First] || isUndef(Elem[i]))
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002157 continue;
2158 IsSplat = false;
2159 break;
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002160 }
Krzysztof Parzyszekfb0fcac2017-12-20 20:33:49 +00002161 if (IsSplat) {
2162 // Legalize the operand to VSPLAT.
2163 SDValue Ext = DAG.getZExtOrTrunc(Elem[First], dl, MVT::i32);
2164 return DAG.getNode(HexagonISD::VSPLAT, dl, VecTy, Ext);
2165 }
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002166 }
2167
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002168 // Then try constant.
2169 if (AllConst) {
2170 uint64_t Val = 0;
2171 unsigned W = ElemTy.getSizeInBits();
2172 uint64_t Mask = (ElemTy == MVT::i8) ? 0xFFull
2173 : (ElemTy == MVT::i16) ? 0xFFFFull : 0xFFFFFFFFull;
2174 for (unsigned i = 0; i != Num; ++i)
Krzysztof Parzyszek240df6f2018-01-11 18:30:41 +00002175 Val = (Val << W) | (Consts[Num-1-i]->getZExtValue() & Mask);
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002176 SDValue V0 = DAG.getConstant(Val, dl, MVT::i64);
2177 return DAG.getBitcast(VecTy, V0);
2178 }
2179
2180 // Build two 32-bit vectors and concatenate.
2181 MVT HalfTy = MVT::getVectorVT(ElemTy, Num/2);
2182 SDValue L = (ElemTy == MVT::i32)
2183 ? Elem[0]
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002184 : buildVector32(Elem.take_front(Num/2), dl, HalfTy, DAG);
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002185 SDValue H = (ElemTy == MVT::i32)
2186 ? Elem[1]
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002187 : buildVector32(Elem.drop_front(Num/2), dl, HalfTy, DAG);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002188 return DAG.getNode(HexagonISD::COMBINE, dl, VecTy, {H, L});
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002189}
2190
2191SDValue
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002192HexagonTargetLowering::extractVector(SDValue VecV, SDValue IdxV,
2193 const SDLoc &dl, MVT ValTy, MVT ResTy,
2194 SelectionDAG &DAG) const {
2195 MVT VecTy = ty(VecV);
2196 assert(!ValTy.isVector() ||
2197 VecTy.getVectorElementType() == ValTy.getVectorElementType());
2198 unsigned VecWidth = VecTy.getSizeInBits();
2199 unsigned ValWidth = ValTy.getSizeInBits();
2200 unsigned ElemWidth = VecTy.getVectorElementType().getSizeInBits();
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002201 assert((VecWidth % ElemWidth) == 0);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002202 auto *IdxN = dyn_cast<ConstantSDNode>(IdxV);
2203
2204 // Special case for v{8,4,2}i1 (the only boolean vectors legal in Hexagon
2205 // without any coprocessors).
2206 if (ElemWidth == 1) {
2207 assert(VecWidth == VecTy.getVectorNumElements() && "Sanity failure");
2208 assert(VecWidth == 8 || VecWidth == 4 || VecWidth == 2);
2209 // Check if this is an extract of the lowest bit.
2210 if (IdxN) {
2211 // Extracting the lowest bit is a no-op, but it changes the type,
2212 // so it must be kept as an operation to avoid errors related to
2213 // type mismatches.
2214 if (IdxN->isNullValue() && ValTy.getSizeInBits() == 1)
2215 return DAG.getNode(HexagonISD::TYPECAST, dl, MVT::i1, VecV);
2216 }
2217
2218 // If the value extracted is a single bit, use tstbit.
2219 if (ValWidth == 1) {
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002220 SDValue A0 = getInstr(Hexagon::C2_tfrpr, dl, MVT::i32, {VecV}, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002221 return DAG.getNode(HexagonISD::TSTBIT, dl, MVT::i1, A0, IdxV);
2222 }
2223
2224 // Each bool vector (v2i1, v4i1, v8i1) always occupies 8 bits in
2225 // a predicate register. The elements of the vector are repeated
2226 // in the register (if necessary) so that the total number is 8.
2227 // The extracted subvector will need to be expanded in such a way.
2228 unsigned Scale = VecWidth / ValWidth;
2229
2230 // Generate (p2d VecV) >> 8*Idx to move the interesting bytes to
2231 // position 0.
2232 assert(ty(IdxV) == MVT::i32);
2233 SDValue S0 = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV,
2234 DAG.getConstant(8, dl, MVT::i32));
2235 SDValue T0 = DAG.getNode(HexagonISD::P2D, dl, MVT::i64, VecV);
2236 SDValue T1 = DAG.getNode(ISD::SRL, dl, MVT::i64, T0, S0);
2237 while (Scale > 1) {
2238 // The longest possible subvector is at most 32 bits, so it is always
2239 // contained in the low subregister.
2240 T1 = DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::i32, T1);
2241 T1 = expandPredicate(T1, dl, DAG);
2242 Scale /= 2;
2243 }
2244
2245 return DAG.getNode(HexagonISD::D2P, dl, ResTy, T1);
2246 }
2247
2248 assert(VecWidth == 32 || VecWidth == 64);
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002249
2250 // Cast everything to scalar integer types.
2251 MVT ScalarTy = tyScalar(VecTy);
2252 VecV = DAG.getBitcast(ScalarTy, VecV);
2253
2254 SDValue WidthV = DAG.getConstant(ValWidth, dl, MVT::i32);
2255 SDValue ExtV;
2256
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002257 if (IdxN) {
2258 unsigned Off = IdxN->getZExtValue() * ElemWidth;
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002259 if (VecWidth == 64 && ValWidth == 32) {
2260 assert(Off == 0 || Off == 32);
2261 unsigned SubIdx = Off == 0 ? Hexagon::isub_lo : Hexagon::isub_hi;
2262 ExtV = DAG.getTargetExtractSubreg(SubIdx, dl, MVT::i32, VecV);
2263 } else if (Off == 0 && (ValWidth % 8) == 0) {
2264 ExtV = DAG.getZeroExtendInReg(VecV, dl, tyScalar(ValTy));
2265 } else {
2266 SDValue OffV = DAG.getConstant(Off, dl, MVT::i32);
2267 // The return type of EXTRACTU must be the same as the type of the
2268 // input vector.
2269 ExtV = DAG.getNode(HexagonISD::EXTRACTU, dl, ScalarTy,
2270 {VecV, WidthV, OffV});
2271 }
2272 } else {
2273 if (ty(IdxV) != MVT::i32)
2274 IdxV = DAG.getZExtOrTrunc(IdxV, dl, MVT::i32);
2275 SDValue OffV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV,
2276 DAG.getConstant(ElemWidth, dl, MVT::i32));
Krzysztof Parzyszekb1b29602018-01-04 13:56:04 +00002277 ExtV = DAG.getNode(HexagonISD::EXTRACTU, dl, ScalarTy,
2278 {VecV, WidthV, OffV});
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002279 }
2280
2281 // Cast ExtV to the requested result type.
2282 ExtV = DAG.getZExtOrTrunc(ExtV, dl, tyScalar(ResTy));
2283 ExtV = DAG.getBitcast(ResTy, ExtV);
2284 return ExtV;
2285}
2286
2287SDValue
2288HexagonTargetLowering::insertVector(SDValue VecV, SDValue ValV, SDValue IdxV,
2289 const SDLoc &dl, MVT ValTy,
2290 SelectionDAG &DAG) const {
2291 MVT VecTy = ty(VecV);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002292 if (VecTy.getVectorElementType() == MVT::i1) {
2293 MVT ValTy = ty(ValV);
2294 assert(ValTy.getVectorElementType() == MVT::i1);
2295 SDValue ValR = DAG.getNode(HexagonISD::P2D, dl, MVT::i64, ValV);
2296 unsigned VecLen = VecTy.getVectorNumElements();
2297 unsigned Scale = VecLen / ValTy.getVectorNumElements();
2298 assert(Scale > 1);
2299
2300 for (unsigned R = Scale; R > 1; R /= 2) {
2301 ValR = contractPredicate(ValR, dl, DAG);
2302 ValR = DAG.getNode(HexagonISD::COMBINE, dl, MVT::i64,
2303 DAG.getUNDEF(MVT::i32), ValR);
2304 }
2305 // The longest possible subvector is at most 32 bits, so it is always
2306 // contained in the low subregister.
2307 ValR = DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::i32, ValR);
2308
2309 unsigned ValBytes = 64 / Scale;
2310 SDValue Width = DAG.getConstant(ValBytes*8, dl, MVT::i32);
2311 SDValue Idx = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV,
2312 DAG.getConstant(8, dl, MVT::i32));
2313 SDValue VecR = DAG.getNode(HexagonISD::P2D, dl, MVT::i64, VecV);
2314 SDValue Ins = DAG.getNode(HexagonISD::INSERT, dl, MVT::i32,
2315 {VecR, ValR, Width, Idx});
2316 return DAG.getNode(HexagonISD::D2P, dl, VecTy, Ins);
2317 }
2318
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002319 unsigned VecWidth = VecTy.getSizeInBits();
2320 unsigned ValWidth = ValTy.getSizeInBits();
2321 assert(VecWidth == 32 || VecWidth == 64);
2322 assert((VecWidth % ValWidth) == 0);
2323
2324 // Cast everything to scalar integer types.
2325 MVT ScalarTy = MVT::getIntegerVT(VecWidth);
2326 // The actual type of ValV may be different than ValTy (which is related
2327 // to the vector type).
2328 unsigned VW = ty(ValV).getSizeInBits();
2329 ValV = DAG.getBitcast(MVT::getIntegerVT(VW), ValV);
2330 VecV = DAG.getBitcast(ScalarTy, VecV);
2331 if (VW != VecWidth)
2332 ValV = DAG.getAnyExtOrTrunc(ValV, dl, ScalarTy);
2333
2334 SDValue WidthV = DAG.getConstant(ValWidth, dl, MVT::i32);
2335 SDValue InsV;
2336
2337 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(IdxV)) {
2338 unsigned W = C->getZExtValue() * ValWidth;
2339 SDValue OffV = DAG.getConstant(W, dl, MVT::i32);
2340 InsV = DAG.getNode(HexagonISD::INSERT, dl, ScalarTy,
2341 {VecV, ValV, WidthV, OffV});
2342 } else {
2343 if (ty(IdxV) != MVT::i32)
2344 IdxV = DAG.getZExtOrTrunc(IdxV, dl, MVT::i32);
2345 SDValue OffV = DAG.getNode(ISD::MUL, dl, MVT::i32, IdxV, WidthV);
Krzysztof Parzyszekb1b29602018-01-04 13:56:04 +00002346 InsV = DAG.getNode(HexagonISD::INSERT, dl, ScalarTy,
2347 {VecV, ValV, WidthV, OffV});
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002348 }
2349
2350 return DAG.getNode(ISD::BITCAST, dl, VecTy, InsV);
2351}
2352
2353SDValue
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002354HexagonTargetLowering::expandPredicate(SDValue Vec32, const SDLoc &dl,
2355 SelectionDAG &DAG) const {
2356 assert(ty(Vec32).getSizeInBits() == 32);
2357 if (isUndef(Vec32))
2358 return DAG.getUNDEF(MVT::i64);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002359 return getInstr(Hexagon::S2_vsxtbh, dl, MVT::i64, {Vec32}, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002360}
2361
2362SDValue
2363HexagonTargetLowering::contractPredicate(SDValue Vec64, const SDLoc &dl,
2364 SelectionDAG &DAG) const {
2365 assert(ty(Vec64).getSizeInBits() == 64);
2366 if (isUndef(Vec64))
2367 return DAG.getUNDEF(MVT::i32);
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002368 return getInstr(Hexagon::S2_vtrunehb, dl, MVT::i32, {Vec64}, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002369}
2370
2371SDValue
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002372HexagonTargetLowering::getZero(const SDLoc &dl, MVT Ty, SelectionDAG &DAG)
2373 const {
2374 if (Ty.isVector()) {
2375 assert(Ty.isInteger() && "Only integer vectors are supported here");
2376 unsigned W = Ty.getSizeInBits();
2377 if (W <= 64)
2378 return DAG.getBitcast(Ty, DAG.getConstant(0, dl, MVT::getIntegerVT(W)));
2379 return DAG.getNode(HexagonISD::VZERO, dl, Ty);
2380 }
2381
2382 if (Ty.isInteger())
2383 return DAG.getConstant(0, dl, Ty);
2384 if (Ty.isFloatingPoint())
2385 return DAG.getConstantFP(0.0, dl, Ty);
2386 llvm_unreachable("Invalid type for zero");
2387}
2388
2389SDValue
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002390HexagonTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002391 MVT VecTy = ty(Op);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002392 unsigned BW = VecTy.getSizeInBits();
2393 const SDLoc &dl(Op);
2394 SmallVector<SDValue,8> Ops;
2395 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i)
2396 Ops.push_back(Op.getOperand(i));
2397
2398 if (BW == 32)
2399 return buildVector32(Ops, dl, VecTy, DAG);
2400 if (BW == 64)
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002401 return buildVector64(Ops, dl, VecTy, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002402
2403 if (VecTy == MVT::v8i1 || VecTy == MVT::v4i1 || VecTy == MVT::v2i1) {
2404 // For each i1 element in the resulting predicate register, put 1
2405 // shifted by the index of the element into a general-purpose register,
2406 // then or them together and transfer it back into a predicate register.
2407 SDValue Rs[8];
2408 SDValue Z = getZero(dl, MVT::i32, DAG);
2409 // Always produce 8 bits, repeat inputs if necessary.
2410 unsigned Rep = 8 / VecTy.getVectorNumElements();
2411 for (unsigned i = 0; i != 8; ++i) {
Simon Pilgrimc1e22902018-01-23 21:22:16 +00002412 SDValue S = DAG.getConstant(1ull << i, dl, MVT::i32);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002413 Rs[i] = DAG.getSelect(dl, MVT::i32, Ops[i/Rep], S, Z);
2414 }
2415 for (ArrayRef<SDValue> A(Rs); A.size() != 1; A = A.drop_back(A.size()/2)) {
2416 for (unsigned i = 0, e = A.size()/2; i != e; ++i)
2417 Rs[i] = DAG.getNode(ISD::OR, dl, MVT::i32, Rs[2*i], Rs[2*i+1]);
2418 }
2419 // Move the value directly to a predicate register.
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002420 return getInstr(Hexagon::C2_tfrrp, dl, VecTy, {Rs[0]}, DAG);
Krzysztof Parzyszek942fa162017-11-22 20:56:23 +00002421 }
2422
2423 return SDValue();
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002424}
2425
2426SDValue
2427HexagonTargetLowering::LowerCONCAT_VECTORS(SDValue Op,
2428 SelectionDAG &DAG) const {
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002429 MVT VecTy = ty(Op);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002430 const SDLoc &dl(Op);
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002431 if (VecTy.getSizeInBits() == 64) {
2432 assert(Op.getNumOperands() == 2);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002433 return DAG.getNode(HexagonISD::COMBINE, dl, VecTy, Op.getOperand(1),
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002434 Op.getOperand(0));
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002435 }
2436
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002437 MVT ElemTy = VecTy.getVectorElementType();
2438 if (ElemTy == MVT::i1) {
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002439 assert(VecTy == MVT::v2i1 || VecTy == MVT::v4i1 || VecTy == MVT::v8i1);
2440 MVT OpTy = ty(Op.getOperand(0));
2441 // Scale is how many times the operands need to be contracted to match
2442 // the representation in the target register.
2443 unsigned Scale = VecTy.getVectorNumElements() / OpTy.getVectorNumElements();
2444 assert(Scale == Op.getNumOperands() && Scale > 1);
2445
2446 // First, convert all bool vectors to integers, then generate pairwise
2447 // inserts to form values of doubled length. Up until there are only
2448 // two values left to concatenate, all of these values will fit in a
2449 // 32-bit integer, so keep them as i32 to use 32-bit inserts.
2450 SmallVector<SDValue,4> Words[2];
2451 unsigned IdxW = 0;
2452
2453 for (SDValue P : Op.getNode()->op_values()) {
2454 SDValue W = DAG.getNode(HexagonISD::P2D, dl, MVT::i64, P);
2455 for (unsigned R = Scale; R > 1; R /= 2) {
2456 W = contractPredicate(W, dl, DAG);
2457 W = DAG.getNode(HexagonISD::COMBINE, dl, MVT::i64,
2458 DAG.getUNDEF(MVT::i32), W);
2459 }
2460 W = DAG.getTargetExtractSubreg(Hexagon::isub_lo, dl, MVT::i32, W);
2461 Words[IdxW].push_back(W);
2462 }
2463
2464 while (Scale > 2) {
2465 SDValue WidthV = DAG.getConstant(64 / Scale, dl, MVT::i32);
2466 Words[IdxW ^ 1].clear();
2467
2468 for (unsigned i = 0, e = Words[IdxW].size(); i != e; i += 2) {
2469 SDValue W0 = Words[IdxW][i], W1 = Words[IdxW][i+1];
2470 // Insert W1 into W0 right next to the significant bits of W0.
2471 SDValue T = DAG.getNode(HexagonISD::INSERT, dl, MVT::i32,
2472 {W0, W1, WidthV, WidthV});
2473 Words[IdxW ^ 1].push_back(T);
2474 }
2475 IdxW ^= 1;
2476 Scale /= 2;
2477 }
2478
2479 // Another sanity check. At this point there should only be two words
2480 // left, and Scale should be 2.
2481 assert(Scale == 2 && Words[IdxW].size() == 2);
2482
2483 SDValue WW = DAG.getNode(HexagonISD::COMBINE, dl, MVT::i64,
2484 Words[IdxW][1], Words[IdxW][0]);
2485 return DAG.getNode(HexagonISD::D2P, dl, VecTy, WW);
2486 }
2487
Krzysztof Parzyszek0bd55a72016-07-29 16:44:27 +00002488 return SDValue();
2489}
2490
2491SDValue
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002492HexagonTargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
2493 SelectionDAG &DAG) const {
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002494 SDValue Vec = Op.getOperand(0);
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002495 MVT ElemTy = ty(Vec).getVectorElementType();
2496 return extractVector(Vec, Op.getOperand(1), SDLoc(Op), ElemTy, ty(Op), DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002497}
2498
2499SDValue
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002500HexagonTargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op,
2501 SelectionDAG &DAG) const {
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00002502 return extractVector(Op.getOperand(0), Op.getOperand(1), SDLoc(Op),
2503 ty(Op), ty(Op), DAG);
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002504}
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002505
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002506SDValue
2507HexagonTargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op,
2508 SelectionDAG &DAG) const {
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002509 return insertVector(Op.getOperand(0), Op.getOperand(1), Op.getOperand(2),
Krzysztof Parzyszek8abaf892018-02-06 20:22:20 +00002510 SDLoc(Op), ty(Op).getVectorElementType(), DAG);
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002511}
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002512
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002513SDValue
2514HexagonTargetLowering::LowerINSERT_SUBVECTOR(SDValue Op,
2515 SelectionDAG &DAG) const {
2516 SDValue ValV = Op.getOperand(1);
2517 return insertVector(Op.getOperand(0), ValV, Op.getOperand(2),
2518 SDLoc(Op), ty(ValV), DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002519}
2520
Tim Northovera4415852013-08-06 09:12:35 +00002521bool
2522HexagonTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
2523 // Assuming the caller does not have either a signext or zeroext modifier, and
2524 // only one value is accepted, any reasonable truncation is allowed.
2525 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
2526 return false;
2527
2528 // FIXME: in principle up to 64-bit could be made safe, but it would be very
2529 // fragile at the moment: any support for multiple value returns would be
2530 // liable to disallow tail calls involving i64 -> iN truncation in many cases.
2531 return Ty1->getPrimitiveSizeInBits() <= 32;
2532}
2533
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002534SDValue
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002535HexagonTargetLowering::LowerUnalignedLoad(SDValue Op, SelectionDAG &DAG)
2536 const {
2537 LoadSDNode *LN = cast<LoadSDNode>(Op.getNode());
2538 unsigned HaveAlign = LN->getAlignment();
2539 MVT LoadTy = ty(Op);
2540 unsigned NeedAlign = Subtarget.getTypeAlignment(LoadTy);
2541 if (HaveAlign >= NeedAlign)
2542 return Op;
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002543
2544 const SDLoc &dl(Op);
2545 const DataLayout &DL = DAG.getDataLayout();
2546 LLVMContext &Ctx = *DAG.getContext();
2547 unsigned AS = LN->getAddressSpace();
2548
2549 // If the load aligning is disabled or the load can be broken up into two
2550 // smaller legal loads, do the default (target-independent) expansion.
2551 bool DoDefault = false;
Krzysztof Parzyszek480ab2b2018-03-08 18:15:13 +00002552 // Handle it in the default way if this is an indexed load.
2553 if (!LN->isUnindexed())
2554 DoDefault = true;
2555
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002556 if (!AlignLoads) {
2557 if (allowsMemoryAccess(Ctx, DL, LN->getMemoryVT(), AS, HaveAlign))
2558 return Op;
2559 DoDefault = true;
2560 }
2561 if (!DoDefault && 2*HaveAlign == NeedAlign) {
2562 // The PartTy is the equivalent of "getLoadableTypeOfSize(HaveAlign)".
2563 MVT PartTy = HaveAlign <= 8 ? MVT::getIntegerVT(8*HaveAlign)
2564 : MVT::getVectorVT(MVT::i8, HaveAlign);
2565 DoDefault = allowsMemoryAccess(Ctx, DL, PartTy, AS, HaveAlign);
2566 }
2567 if (DoDefault) {
2568 std::pair<SDValue, SDValue> P = expandUnalignedLoad(LN, DAG);
2569 return DAG.getMergeValues({P.first, P.second}, dl);
2570 }
2571
2572 // The code below generates two loads, both aligned as NeedAlign, and
2573 // with the distance of NeedAlign between them. For that to cover the
2574 // bits that need to be loaded (and without overlapping), the size of
2575 // the loads should be equal to NeedAlign. This is true for all loadable
2576 // types, but add an assertion in case something changes in the future.
2577 assert(LoadTy.getSizeInBits() == 8*NeedAlign);
2578
2579 unsigned LoadLen = NeedAlign;
2580 SDValue Base = LN->getBasePtr();
2581 SDValue Chain = LN->getChain();
2582 auto BO = getBaseAndOffset(Base);
2583 unsigned BaseOpc = BO.first.getOpcode();
2584 if (BaseOpc == HexagonISD::VALIGNADDR && BO.second % LoadLen == 0)
2585 return Op;
2586
2587 if (BO.second % LoadLen != 0) {
2588 BO.first = DAG.getNode(ISD::ADD, dl, MVT::i32, BO.first,
2589 DAG.getConstant(BO.second % LoadLen, dl, MVT::i32));
2590 BO.second -= BO.second % LoadLen;
2591 }
2592 SDValue BaseNoOff = (BaseOpc != HexagonISD::VALIGNADDR)
2593 ? DAG.getNode(HexagonISD::VALIGNADDR, dl, MVT::i32, BO.first,
2594 DAG.getConstant(NeedAlign, dl, MVT::i32))
2595 : BO.first;
2596 SDValue Base0 = DAG.getMemBasePlusOffset(BaseNoOff, BO.second, dl);
2597 SDValue Base1 = DAG.getMemBasePlusOffset(BaseNoOff, BO.second+LoadLen, dl);
2598
2599 MachineMemOperand *WideMMO = nullptr;
2600 if (MachineMemOperand *MMO = LN->getMemOperand()) {
2601 MachineFunction &MF = DAG.getMachineFunction();
2602 WideMMO = MF.getMachineMemOperand(MMO->getPointerInfo(), MMO->getFlags(),
2603 2*LoadLen, LoadLen, MMO->getAAInfo(), MMO->getRanges(),
2604 MMO->getSyncScopeID(), MMO->getOrdering(),
2605 MMO->getFailureOrdering());
2606 }
2607
2608 SDValue Load0 = DAG.getLoad(LoadTy, dl, Chain, Base0, WideMMO);
2609 SDValue Load1 = DAG.getLoad(LoadTy, dl, Chain, Base1, WideMMO);
2610
2611 SDValue Aligned = DAG.getNode(HexagonISD::VALIGN, dl, LoadTy,
2612 {Load1, Load0, BaseNoOff.getOperand(0)});
2613 SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2614 Load0.getValue(1), Load1.getValue(1));
2615 SDValue M = DAG.getMergeValues({Aligned, NewChain}, dl);
2616 return M;
2617}
2618
2619SDValue
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002620HexagonTargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
2621 SDValue Chain = Op.getOperand(0);
2622 SDValue Offset = Op.getOperand(1);
2623 SDValue Handler = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002624 SDLoc dl(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00002625 auto PtrVT = getPointerTy(DAG.getDataLayout());
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002626
2627 // Mark function as containing a call to EH_RETURN.
2628 HexagonMachineFunctionInfo *FuncInfo =
2629 DAG.getMachineFunction().getInfo<HexagonMachineFunctionInfo>();
2630 FuncInfo->setHasEHReturn();
2631
2632 unsigned OffsetReg = Hexagon::R28;
2633
Mehdi Amini44ede332015-07-09 02:09:04 +00002634 SDValue StoreAddr =
2635 DAG.getNode(ISD::ADD, dl, PtrVT, DAG.getRegister(Hexagon::R30, PtrVT),
2636 DAG.getIntPtrConstant(4, dl));
Justin Lebar9c375812016-07-15 18:27:10 +00002637 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo());
Jyotsna Verma5ed51812013-05-01 21:37:34 +00002638 Chain = DAG.getCopyToReg(Chain, dl, OffsetReg, Offset);
2639
2640 // Not needed we already use it as explict input to EH_RETURN.
2641 // MF.getRegInfo().addLiveOut(OffsetReg);
2642
2643 return DAG.getNode(HexagonISD::EH_RETURN, dl, MVT::Other, Chain);
2644}
2645
2646SDValue
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002647HexagonTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002648 unsigned Opc = Op.getOpcode();
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00002649
2650 // Handle INLINEASM first.
2651 if (Opc == ISD::INLINEASM)
2652 return LowerINLINEASM(Op, DAG);
2653
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002654 if (isHvxOperation(Op)) {
2655 // If HVX lowering returns nothing, try the default lowering.
2656 if (SDValue V = LowerHvxOperation(Op, DAG))
2657 return V;
2658 }
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00002659
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002660 switch (Opc) {
2661 default:
2662#ifndef NDEBUG
2663 Op.getNode()->dumpr(&DAG);
2664 if (Opc > HexagonISD::OP_BEGIN && Opc < HexagonISD::OP_END)
Krzysztof Parzyszeke4ce92c2017-12-20 20:49:43 +00002665 errs() << "Error: check for a non-legal type in this operation\n";
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002666#endif
2667 llvm_unreachable("Should not custom lower this!");
2668 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Krzysztof Parzyszek6a8e5f42017-11-29 19:58:10 +00002669 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
2670 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
2671 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
2672 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002673 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
2674 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002675 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Krzysztof Parzyszek2c3edf02018-03-07 17:27:18 +00002676 case ISD::LOAD: return LowerUnalignedLoad(Op, DAG);
Krzysztof Parzyszek42113342015-03-19 16:33:08 +00002677 case ISD::SRA:
2678 case ISD::SHL:
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002679 case ISD::SRL: return LowerVECTOR_SHIFT(Op, DAG);
2680 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002681 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002682 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002683 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
2684 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Krzysztof Parzyszek7a737d12016-02-18 15:42:57 +00002685 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002686 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
2687 case ISD::GlobalAddress: return LowerGLOBALADDRESS(Op, DAG);
2688 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002689 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002690 case ISD::VASTART: return LowerVASTART(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002691 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
2692 case ISD::SETCC: return LowerSETCC(Op, DAG);
2693 case ISD::VSELECT: return LowerVSELECT(Op, DAG);
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002694 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00002695 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
Krzysztof Parzyszek6895b2c2016-02-18 13:58:38 +00002696 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG);
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +00002697 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Krzysztof Parzyszek9eb085e2018-01-31 20:48:11 +00002698 break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002699 }
Krzysztof Parzyszek88f11002018-02-06 14:24:57 +00002700
Krzysztof Parzyszek7d37dd82017-12-06 16:40:37 +00002701 return SDValue();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002702}
2703
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002704void
2705HexagonTargetLowering::ReplaceNodeResults(SDNode *N,
2706 SmallVectorImpl<SDValue> &Results,
2707 SelectionDAG &DAG) const {
2708 const SDLoc &dl(N);
2709 switch (N->getOpcode()) {
2710 case ISD::SRL:
2711 case ISD::SRA:
2712 case ISD::SHL:
2713 return;
2714 case ISD::BITCAST:
2715 // Handle a bitcast from v8i1 to i8.
2716 if (N->getValueType(0) == MVT::i8) {
Krzysztof Parzyszek15efa982018-01-31 21:17:03 +00002717 SDValue P = getInstr(Hexagon::C2_tfrpr, dl, MVT::i32,
2718 N->getOperand(0), DAG);
Krzysztof Parzyszek3780a0e2018-01-23 17:53:59 +00002719 Results.push_back(P);
2720 }
2721 break;
2722 }
2723}
2724
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002725/// Returns relocation base for the given PIC jumptable.
2726SDValue
2727HexagonTargetLowering::getPICJumpTableRelocBase(SDValue Table,
2728 SelectionDAG &DAG) const {
2729 int Idx = cast<JumpTableSDNode>(Table)->getIndex();
2730 EVT VT = Table.getValueType();
2731 SDValue T = DAG.getTargetJumpTable(Idx, VT, HexagonII::MO_PCREL);
2732 return DAG.getNode(HexagonISD::AT_PCREL, SDLoc(Table), VT, T);
2733}
2734
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002735//===----------------------------------------------------------------------===//
2736// Inline Assembly Support
2737//===----------------------------------------------------------------------===//
2738
Krzysztof Parzyszekca3b5322016-05-18 14:34:51 +00002739TargetLowering::ConstraintType
2740HexagonTargetLowering::getConstraintType(StringRef Constraint) const {
2741 if (Constraint.size() == 1) {
2742 switch (Constraint[0]) {
2743 case 'q':
2744 case 'v':
2745 if (Subtarget.useHVXOps())
Krzysztof Parzyszek3ad0d012017-07-21 17:51:27 +00002746 return C_RegisterClass;
2747 break;
2748 case 'a':
2749 return C_RegisterClass;
2750 default:
Krzysztof Parzyszekca3b5322016-05-18 14:34:51 +00002751 break;
2752 }
2753 }
2754 return TargetLowering::getConstraintType(Constraint);
2755}
2756
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002757std::pair<unsigned, const TargetRegisterClass*>
Eric Christopher11e4df72015-02-26 22:38:43 +00002758HexagonTargetLowering::getRegForInlineAsmConstraint(
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00002759 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002760
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002761 if (Constraint.size() == 1) {
2762 switch (Constraint[0]) {
2763 case 'r': // R0-R31
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002764 switch (VT.SimpleTy) {
2765 default:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002766 return {0u, nullptr};
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002767 case MVT::i1:
2768 case MVT::i8:
2769 case MVT::i16:
2770 case MVT::i32:
2771 case MVT::f32:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002772 return {0u, &Hexagon::IntRegsRegClass};
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002773 case MVT::i64:
2774 case MVT::f64:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002775 return {0u, &Hexagon::DoubleRegsRegClass};
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002776 }
Krzysztof Parzyszek3ad0d012017-07-21 17:51:27 +00002777 break;
2778 case 'a': // M0-M1
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002779 if (VT != MVT::i32)
2780 return {0u, nullptr};
2781 return {0u, &Hexagon::ModRegsRegClass};
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002782 case 'q': // q0-q3
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002783 switch (VT.getSizeInBits()) {
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002784 default:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002785 return {0u, nullptr};
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002786 case 512:
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002787 case 1024:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002788 return {0u, &Hexagon::HvxQRRegClass};
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002789 }
Krzysztof Parzyszek3ad0d012017-07-21 17:51:27 +00002790 break;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002791 case 'v': // V0-V31
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002792 switch (VT.getSizeInBits()) {
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002793 default:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002794 return {0u, nullptr};
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002795 case 512:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002796 return {0u, &Hexagon::HvxVRRegClass};
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002797 case 1024:
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +00002798 if (Subtarget.hasV60TOps() && Subtarget.useHVX128BOps())
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002799 return {0u, &Hexagon::HvxVRRegClass};
2800 return {0u, &Hexagon::HvxWRRegClass};
Krzysztof Parzyszekfcbb7d12017-03-02 17:50:24 +00002801 case 2048:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002802 return {0u, &Hexagon::HvxWRRegClass};
Krzysztof Parzyszekfb4c4172016-08-19 19:29:15 +00002803 }
Krzysztof Parzyszek3ad0d012017-07-21 17:51:27 +00002804 break;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002805 default:
Krzysztof Parzyszek022922b2017-10-20 20:24:44 +00002806 return {0u, nullptr};
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002807 }
2808 }
2809
Eric Christopher11e4df72015-02-26 22:38:43 +00002810 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002811}
2812
Sirish Pande69295b82012-05-10 20:20:25 +00002813/// isFPImmLegal - Returns true if the target can instruction select the
2814/// specified FP immediate natively. If false, the legalizer will
2815/// materialize the FP immediate as a load from a constant pool.
2816bool HexagonTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002817 return Subtarget.hasV5TOps();
Sirish Pande69295b82012-05-10 20:20:25 +00002818}
2819
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002820/// isLegalAddressingMode - Return true if the addressing mode represented by
2821/// AM is legal for this target, for a load/store of the specified type.
Mehdi Amini0cdec1e2015-07-09 02:09:40 +00002822bool HexagonTargetLowering::isLegalAddressingMode(const DataLayout &DL,
2823 const AddrMode &AM, Type *Ty,
Jonas Paulsson024e3192017-07-21 11:59:37 +00002824 unsigned AS, Instruction *I) const {
Krzysztof Parzyszeked4e7822016-08-03 15:06:18 +00002825 if (Ty->isSized()) {
2826 // When LSR detects uses of the same base address to access different
2827 // types (e.g. unions), it will assume a conservative type for these
2828 // uses:
2829 // LSR Use: Kind=Address of void in addrspace(4294967295), ...
2830 // The type Ty passed here would then be "void". Skip the alignment
2831 // checks, but do not return false right away, since that confuses
2832 // LSR into crashing.
2833 unsigned A = DL.getABITypeAlignment(Ty);
2834 // The base offset must be a multiple of the alignment.
2835 if ((AM.BaseOffs % A) != 0)
2836 return false;
2837 // The shifted offset must fit in 11 bits.
2838 if (!isInt<11>(AM.BaseOffs >> Log2_32(A)))
2839 return false;
2840 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002841
2842 // No global is ever allowed as a base.
Krzysztof Parzyszek952d9512015-04-22 21:17:00 +00002843 if (AM.BaseGV)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002844 return false;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002845
2846 int Scale = AM.Scale;
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00002847 if (Scale < 0)
2848 Scale = -Scale;
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002849 switch (Scale) {
2850 case 0: // No scale reg, "r+i", "r", or just "i".
2851 break;
2852 default: // No scaled addressing mode.
2853 return false;
2854 }
2855 return true;
2856}
2857
Krzysztof Parzyszek21dc8bd2015-12-18 20:19:30 +00002858/// Return true if folding a constant offset with the given GlobalAddress is
2859/// legal. It is frequently not legal in PIC relocation models.
2860bool HexagonTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA)
2861 const {
2862 return HTM.getRelocationModel() == Reloc::Static;
2863}
2864
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002865/// isLegalICmpImmediate - Return true if the specified immediate is legal
2866/// icmp immediate, that is the target has icmp instructions which can compare
2867/// a register against the immediate without having to materialize the
2868/// immediate into a register.
2869bool HexagonTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
2870 return Imm >= -512 && Imm <= 511;
2871}
2872
2873/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2874/// for tail call optimization. Targets which want to do tail call
2875/// optimization should implement this function.
2876bool HexagonTargetLowering::IsEligibleForTailCallOptimization(
2877 SDValue Callee,
2878 CallingConv::ID CalleeCC,
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00002879 bool IsVarArg,
2880 bool IsCalleeStructRet,
2881 bool IsCallerStructRet,
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002882 const SmallVectorImpl<ISD::OutputArg> &Outs,
2883 const SmallVectorImpl<SDValue> &OutVals,
2884 const SmallVectorImpl<ISD::InputArg> &Ins,
2885 SelectionDAG& DAG) const {
Matthias Braunf1caa282017-12-15 22:22:58 +00002886 const Function &CallerF = DAG.getMachineFunction().getFunction();
2887 CallingConv::ID CallerCC = CallerF.getCallingConv();
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002888 bool CCMatch = CallerCC == CalleeCC;
2889
2890 // ***************************************************************************
2891 // Look for obvious safe cases to perform tail call optimization that do not
2892 // require ABI changes.
2893 // ***************************************************************************
2894
2895 // If this is a tail call via a function pointer, then don't do it!
Krzysztof Parzyszek317d42c2016-08-01 20:31:50 +00002896 if (!isa<GlobalAddressSDNode>(Callee) &&
2897 !isa<ExternalSymbolSDNode>(Callee)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002898 return false;
2899 }
2900
Krzysztof Parzyszek0ba97542016-08-19 15:02:18 +00002901 // Do not optimize if the calling conventions do not match and the conventions
2902 // used are not C or Fast.
2903 if (!CCMatch) {
2904 bool R = (CallerCC == CallingConv::C || CallerCC == CallingConv::Fast);
2905 bool E = (CalleeCC == CallingConv::C || CalleeCC == CallingConv::Fast);
2906 // If R & E, then ok.
2907 if (!R || !E)
2908 return false;
2909 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002910
2911 // Do not tail call optimize vararg calls.
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00002912 if (IsVarArg)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002913 return false;
2914
2915 // Also avoid tail call optimization if either caller or callee uses struct
2916 // return semantics.
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00002917 if (IsCalleeStructRet || IsCallerStructRet)
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002918 return false;
2919
2920 // In addition to the cases above, we also disable Tail Call Optimization if
2921 // the calling convention code that at least one outgoing argument needs to
2922 // go on the stack. We cannot check that here because at this point that
2923 // information is not available.
2924 return true;
2925}
Colin LeMahieu025f8602014-12-08 21:19:18 +00002926
Krzysztof Parzyszek3e409e12016-08-02 18:34:31 +00002927/// Returns the target specific optimal type for load and store operations as
2928/// a result of memset, memcpy, and memmove lowering.
2929///
2930/// If DstAlign is zero that means it's safe to destination alignment can
2931/// satisfy any constraint. Similarly if SrcAlign is zero it means there isn't
2932/// a need to check it against alignment requirement, probably because the
2933/// source does not need to be loaded. If 'IsMemset' is true, that means it's
2934/// expanding a memset. If 'ZeroMemset' is true, that means it's a memset of
2935/// zero. 'MemcpyStrSrc' indicates whether the memcpy source is constant so it
2936/// does not need to be loaded. It returns EVT::Other if the type should be
2937/// determined using generic target-independent logic.
2938EVT HexagonTargetLowering::getOptimalMemOpType(uint64_t Size,
2939 unsigned DstAlign, unsigned SrcAlign, bool IsMemset, bool ZeroMemset,
2940 bool MemcpyStrSrc, MachineFunction &MF) const {
2941
2942 auto Aligned = [](unsigned GivenA, unsigned MinA) -> bool {
2943 return (GivenA % MinA) == 0;
2944 };
2945
2946 if (Size >= 8 && Aligned(DstAlign, 8) && (IsMemset || Aligned(SrcAlign, 8)))
2947 return MVT::i64;
2948 if (Size >= 4 && Aligned(DstAlign, 4) && (IsMemset || Aligned(SrcAlign, 4)))
2949 return MVT::i32;
2950 if (Size >= 2 && Aligned(DstAlign, 2) && (IsMemset || Aligned(SrcAlign, 2)))
2951 return MVT::i16;
2952
2953 return MVT::Other;
2954}
2955
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +00002956bool HexagonTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
2957 unsigned AS, unsigned Align, bool *Fast) const {
2958 if (Fast)
2959 *Fast = false;
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00002960 return Subtarget.isHVXVectorType(VT.getSimpleVT());
Krzysztof Parzyszek2d65ea72016-03-28 15:43:03 +00002961}
2962
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002963std::pair<const TargetRegisterClass*, uint8_t>
2964HexagonTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI,
2965 MVT VT) const {
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00002966 if (Subtarget.isHVXVectorType(VT, true)) {
2967 unsigned BitWidth = VT.getSizeInBits();
2968 unsigned VecWidth = Subtarget.getVectorLength() * 8;
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002969
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00002970 if (VT.getVectorElementType() == MVT::i1)
2971 return std::make_pair(&Hexagon::HvxQRRegClass, 1);
2972 if (BitWidth == VecWidth)
2973 return std::make_pair(&Hexagon::HvxVRRegClass, 1);
2974 assert(BitWidth == 2 * VecWidth);
2975 return std::make_pair(&Hexagon::HvxWRRegClass, 1);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002976 }
Krzysztof Parzyszekcfbe6ba2018-02-13 15:35:07 +00002977
2978 return TargetLowering::findRepresentativeClass(TRI, VT);
Krzysztof Parzyszek08ff8882015-11-26 18:38:27 +00002979}
2980
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00002981Value *HexagonTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
2982 AtomicOrdering Ord) const {
2983 BasicBlock *BB = Builder.GetInsertBlock();
2984 Module *M = BB->getParent()->getParent();
2985 Type *Ty = cast<PointerType>(Addr->getType())->getElementType();
2986 unsigned SZ = Ty->getPrimitiveSizeInBits();
2987 assert((SZ == 32 || SZ == 64) && "Only 32/64-bit atomic loads supported");
2988 Intrinsic::ID IntID = (SZ == 32) ? Intrinsic::hexagon_L2_loadw_locked
2989 : Intrinsic::hexagon_L4_loadd_locked;
2990 Value *Fn = Intrinsic::getDeclaration(M, IntID);
2991 return Builder.CreateCall(Fn, Addr, "larx");
2992}
2993
2994/// Perform a store-conditional operation to Addr. Return the status of the
2995/// store. This should be 0 if the store succeeded, non-zero otherwise.
2996Value *HexagonTargetLowering::emitStoreConditional(IRBuilder<> &Builder,
2997 Value *Val, Value *Addr, AtomicOrdering Ord) const {
2998 BasicBlock *BB = Builder.GetInsertBlock();
2999 Module *M = BB->getParent()->getParent();
3000 Type *Ty = Val->getType();
3001 unsigned SZ = Ty->getPrimitiveSizeInBits();
3002 assert((SZ == 32 || SZ == 64) && "Only 32/64-bit atomic stores supported");
3003 Intrinsic::ID IntID = (SZ == 32) ? Intrinsic::hexagon_S2_storew_locked
3004 : Intrinsic::hexagon_S4_stored_locked;
3005 Value *Fn = Intrinsic::getDeclaration(M, IntID);
3006 Value *Call = Builder.CreateCall(Fn, {Addr, Val}, "stcx");
3007 Value *Cmp = Builder.CreateICmpEQ(Call, Builder.getInt32(0), "");
3008 Value *Ext = Builder.CreateZExt(Cmp, Type::getInt32Ty(M->getContext()));
3009 return Ext;
3010}
3011
Ahmed Bougacha52468672015-09-11 17:08:28 +00003012TargetLowering::AtomicExpansionKind
3013HexagonTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const {
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003014 // Do not expand loads and stores that don't exceed 64 bits.
Ahmed Bougacha52468672015-09-11 17:08:28 +00003015 return LI->getType()->getPrimitiveSizeInBits() > 64
Tim Northoverf520eff2015-12-02 18:12:57 +00003016 ? AtomicExpansionKind::LLOnly
Ahmed Bougacha52468672015-09-11 17:08:28 +00003017 : AtomicExpansionKind::None;
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +00003018}
3019
3020bool HexagonTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const {
3021 // Do not expand loads and stores that don't exceed 64 bits.
3022 return SI->getValueOperand()->getType()->getPrimitiveSizeInBits() > 64;
3023}
Krzysztof Parzyszekf228c952016-06-22 16:07:10 +00003024
3025bool HexagonTargetLowering::shouldExpandAtomicCmpXchgInIR(
3026 AtomicCmpXchgInst *AI) const {
3027 const DataLayout &DL = AI->getModule()->getDataLayout();
3028 unsigned Size = DL.getTypeStoreSize(AI->getCompareOperand()->getType());
3029 return Size >= 4 && Size <= 8;
3030}