1 //===-- HexagonISelLowering.cpp - Hexagon DAG Lowering Implementation -----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the interfaces that Hexagon uses to lower LLVM code
11 // into a selection DAG.
13 //===----------------------------------------------------------------------===//
15 #include "HexagonISelLowering.h"
16 #include "HexagonMachineFunctionInfo.h"
17 #include "HexagonSubtarget.h"
18 #include "HexagonTargetMachine.h"
19 #include "HexagonTargetObjectFile.h"
20 #include "llvm/CodeGen/CallingConvLower.h"
21 #include "llvm/CodeGen/MachineFrameInfo.h"
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/CodeGen/MachineInstrBuilder.h"
24 #include "llvm/CodeGen/MachineJumpTableInfo.h"
25 #include "llvm/CodeGen/MachineRegisterInfo.h"
26 #include "llvm/CodeGen/SelectionDAGISel.h"
27 #include "llvm/CodeGen/ValueTypes.h"
28 #include "llvm/IR/CallingConv.h"
29 #include "llvm/IR/DerivedTypes.h"
30 #include "llvm/IR/Function.h"
31 #include "llvm/IR/GlobalAlias.h"
32 #include "llvm/IR/GlobalVariable.h"
33 #include "llvm/IR/InlineAsm.h"
34 #include "llvm/IR/Intrinsics.h"
35 #include "llvm/Support/CommandLine.h"
36 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/ErrorHandling.h"
38 #include "llvm/Support/raw_ostream.h"
42 #define DEBUG_TYPE "hexagon-lowering"
45 EmitJumpTables("hexagon-emit-jump-tables", cl::init(true), cl::Hidden,
46 cl::desc("Control jump table emission on Hexagon target"));
48 static cl::opt<bool> EnableHexSDNodeSched("enable-hexagon-sdnode-sched",
49 cl::Hidden, cl::ZeroOrMore, cl::init(false),
50 cl::desc("Enable Hexagon SDNode scheduling"));
52 static cl::opt<bool> EnableFastMath("ffast-math",
53 cl::Hidden, cl::ZeroOrMore, cl::init(false),
54 cl::desc("Enable Fast Math processing"));
56 static cl::opt<int> MinimumJumpTables("minimum-jump-tables",
57 cl::Hidden, cl::ZeroOrMore, cl::init(5),
58 cl::desc("Set minimum jump tables"));
60 static cl::opt<int> MaxStoresPerMemcpyCL("max-store-memcpy",
61 cl::Hidden, cl::ZeroOrMore, cl::init(6),
62 cl::desc("Max #stores to inline memcpy"));
64 static cl::opt<int> MaxStoresPerMemcpyOptSizeCL("max-store-memcpy-Os",
65 cl::Hidden, cl::ZeroOrMore, cl::init(4),
66 cl::desc("Max #stores to inline memcpy"));
68 static cl::opt<int> MaxStoresPerMemmoveCL("max-store-memmove",
69 cl::Hidden, cl::ZeroOrMore, cl::init(6),
70 cl::desc("Max #stores to inline memmove"));
72 static cl::opt<int> MaxStoresPerMemmoveOptSizeCL("max-store-memmove-Os",
73 cl::Hidden, cl::ZeroOrMore, cl::init(4),
74 cl::desc("Max #stores to inline memmove"));
76 static cl::opt<int> MaxStoresPerMemsetCL("max-store-memset",
77 cl::Hidden, cl::ZeroOrMore, cl::init(8),
78 cl::desc("Max #stores to inline memset"));
80 static cl::opt<int> MaxStoresPerMemsetOptSizeCL("max-store-memset-Os",
81 cl::Hidden, cl::ZeroOrMore, cl::init(4),
82 cl::desc("Max #stores to inline memset"));
86 class HexagonCCState : public CCState {
87 unsigned NumNamedVarArgParams;
90 HexagonCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
91 SmallVectorImpl<CCValAssign> &locs, LLVMContext &C,
92 int NumNamedVarArgParams)
93 : CCState(CC, isVarArg, MF, locs, C),
94 NumNamedVarArgParams(NumNamedVarArgParams) {}
96 unsigned getNumNamedVarArgParams() const { return NumNamedVarArgParams; }
100 // Implement calling convention for Hexagon.
102 CC_Hexagon(unsigned ValNo, MVT ValVT,
103 MVT LocVT, CCValAssign::LocInfo LocInfo,
104 ISD::ArgFlagsTy ArgFlags, CCState &State);
107 CC_Hexagon32(unsigned ValNo, MVT ValVT,
108 MVT LocVT, CCValAssign::LocInfo LocInfo,
109 ISD::ArgFlagsTy ArgFlags, CCState &State);
112 CC_Hexagon64(unsigned ValNo, MVT ValVT,
113 MVT LocVT, CCValAssign::LocInfo LocInfo,
114 ISD::ArgFlagsTy ArgFlags, CCState &State);
117 RetCC_Hexagon(unsigned ValNo, MVT ValVT,
118 MVT LocVT, CCValAssign::LocInfo LocInfo,
119 ISD::ArgFlagsTy ArgFlags, CCState &State);
122 RetCC_Hexagon32(unsigned ValNo, MVT ValVT,
123 MVT LocVT, CCValAssign::LocInfo LocInfo,
124 ISD::ArgFlagsTy ArgFlags, CCState &State);
127 RetCC_Hexagon64(unsigned ValNo, MVT ValVT,
128 MVT LocVT, CCValAssign::LocInfo LocInfo,
129 ISD::ArgFlagsTy ArgFlags, CCState &State);
132 CC_Hexagon_VarArg (unsigned ValNo, MVT ValVT,
133 MVT LocVT, CCValAssign::LocInfo LocInfo,
134 ISD::ArgFlagsTy ArgFlags, CCState &State) {
135 HexagonCCState &HState = static_cast<HexagonCCState &>(State);
137 if (ValNo < HState.getNumNamedVarArgParams()) {
138 // Deal with named arguments.
139 return CC_Hexagon(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State);
142 // Deal with un-named arguments.
144 if (ArgFlags.isByVal()) {
145 // If pass-by-value, the size allocated on stack is decided
146 // by ArgFlags.getByValSize(), not by the size of LocVT.
147 ofst = State.AllocateStack(ArgFlags.getByValSize(),
148 ArgFlags.getByValAlign());
149 State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
152 if (LocVT == MVT::i1 || LocVT == MVT::i8 || LocVT == MVT::i16) {
155 if (ArgFlags.isSExt())
156 LocInfo = CCValAssign::SExt;
157 else if (ArgFlags.isZExt())
158 LocInfo = CCValAssign::ZExt;
160 LocInfo = CCValAssign::AExt;
162 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
163 ofst = State.AllocateStack(4, 4);
164 State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
167 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
168 ofst = State.AllocateStack(8, 8);
169 State.addLoc(CCValAssign::getMem(ValNo, ValVT, ofst, LocVT, LocInfo));
172 llvm_unreachable(nullptr);
177 CC_Hexagon (unsigned ValNo, MVT ValVT,
178 MVT LocVT, CCValAssign::LocInfo LocInfo,
179 ISD::ArgFlagsTy ArgFlags, CCState &State) {
181 if (ArgFlags.isByVal()) {
183 unsigned Offset = State.AllocateStack(ArgFlags.getByValSize(),
184 ArgFlags.getByValAlign());
185 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
189 if (LocVT == MVT::i1 || LocVT == MVT::i8 || LocVT == MVT::i16) {
192 if (ArgFlags.isSExt())
193 LocInfo = CCValAssign::SExt;
194 else if (ArgFlags.isZExt())
195 LocInfo = CCValAssign::ZExt;
197 LocInfo = CCValAssign::AExt;
198 } else if (LocVT == MVT::v4i8 || LocVT == MVT::v2i16) {
200 LocInfo = CCValAssign::BCvt;
201 } else if (LocVT == MVT::v8i8 || LocVT == MVT::v4i16 || LocVT == MVT::v2i32) {
203 LocInfo = CCValAssign::BCvt;
206 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
207 if (!CC_Hexagon32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
211 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
212 if (!CC_Hexagon64(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
216 return true; // CC didn't match.
220 static bool CC_Hexagon32(unsigned ValNo, MVT ValVT,
221 MVT LocVT, CCValAssign::LocInfo LocInfo,
222 ISD::ArgFlagsTy ArgFlags, CCState &State) {
224 static const MCPhysReg RegList[] = {
225 Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3, Hexagon::R4,
228 if (unsigned Reg = State.AllocateReg(RegList)) {
229 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
233 unsigned Offset = State.AllocateStack(4, 4);
234 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
238 static bool CC_Hexagon64(unsigned ValNo, MVT ValVT,
239 MVT LocVT, CCValAssign::LocInfo LocInfo,
240 ISD::ArgFlagsTy ArgFlags, CCState &State) {
242 if (unsigned Reg = State.AllocateReg(Hexagon::D0)) {
243 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
247 static const MCPhysReg RegList1[] = {
248 Hexagon::D1, Hexagon::D2
250 static const MCPhysReg RegList2[] = {
251 Hexagon::R1, Hexagon::R3
253 if (unsigned Reg = State.AllocateReg(RegList1, RegList2)) {
254 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
258 unsigned Offset = State.AllocateStack(8, 8, Hexagon::D2);
259 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
263 static bool RetCC_Hexagon(unsigned ValNo, MVT ValVT,
264 MVT LocVT, CCValAssign::LocInfo LocInfo,
265 ISD::ArgFlagsTy ArgFlags, CCState &State) {
268 if (LocVT == MVT::i1 ||
273 if (ArgFlags.isSExt())
274 LocInfo = CCValAssign::SExt;
275 else if (ArgFlags.isZExt())
276 LocInfo = CCValAssign::ZExt;
278 LocInfo = CCValAssign::AExt;
279 } else if (LocVT == MVT::v4i8 || LocVT == MVT::v2i16) {
281 LocInfo = CCValAssign::BCvt;
282 } else if (LocVT == MVT::v8i8 || LocVT == MVT::v4i16 || LocVT == MVT::v2i32) {
284 LocInfo = CCValAssign::BCvt;
287 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
288 if (!RetCC_Hexagon32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
292 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
293 if (!RetCC_Hexagon64(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))
297 return true; // CC didn't match.
300 static bool RetCC_Hexagon32(unsigned ValNo, MVT ValVT,
301 MVT LocVT, CCValAssign::LocInfo LocInfo,
302 ISD::ArgFlagsTy ArgFlags, CCState &State) {
304 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
305 if (unsigned Reg = State.AllocateReg(Hexagon::R0)) {
306 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
311 unsigned Offset = State.AllocateStack(4, 4);
312 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
316 static bool RetCC_Hexagon64(unsigned ValNo, MVT ValVT,
317 MVT LocVT, CCValAssign::LocInfo LocInfo,
318 ISD::ArgFlagsTy ArgFlags, CCState &State) {
319 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
320 if (unsigned Reg = State.AllocateReg(Hexagon::D0)) {
321 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
326 unsigned Offset = State.AllocateStack(8, 8);
327 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
332 HexagonTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG)
337 /// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
338 /// by "Src" to address "Dst" of size "Size". Alignment information is
339 /// specified by the specific parameter attribute. The copy will be passed as
340 /// a byval function parameter. Sometimes what we are copying is the end of a
341 /// larger object, the part that does not fit in registers.
343 CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
344 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
347 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), dl, MVT::i32);
348 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
349 /*isVolatile=*/false, /*AlwaysInline=*/false,
350 /*isTailCall=*/false,
351 MachinePointerInfo(), MachinePointerInfo());
355 // LowerReturn - Lower ISD::RET. If a struct is larger than 8 bytes and is
356 // passed by value, the function prototype is modified to return void and
357 // the value is stored in memory pointed by a pointer passed by caller.
359 HexagonTargetLowering::LowerReturn(SDValue Chain,
360 CallingConv::ID CallConv, bool isVarArg,
361 const SmallVectorImpl<ISD::OutputArg> &Outs,
362 const SmallVectorImpl<SDValue> &OutVals,
363 SDLoc dl, SelectionDAG &DAG) const {
365 // CCValAssign - represent the assignment of the return value to locations.
366 SmallVector<CCValAssign, 16> RVLocs;
368 // CCState - Info about the registers and stack slot.
369 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
372 // Analyze return values of ISD::RET
373 CCInfo.AnalyzeReturn(Outs, RetCC_Hexagon);
376 SmallVector<SDValue, 4> RetOps(1, Chain);
378 // Copy the result values into the output registers.
379 for (unsigned i = 0; i != RVLocs.size(); ++i) {
380 CCValAssign &VA = RVLocs[i];
382 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), OutVals[i], Flag);
384 // Guarantee that all emitted copies are stuck together with flags.
385 Flag = Chain.getValue(1);
386 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
389 RetOps[0] = Chain; // Update chain.
391 // Add the flag if we have it.
393 RetOps.push_back(Flag);
395 return DAG.getNode(HexagonISD::RET_FLAG, dl, MVT::Other, RetOps);
398 bool HexagonTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
399 // If either no tail call or told not to tail call at all, don't.
400 if (!CI->isTailCall() || HTM.Options.DisableTailCalls)
406 /// LowerCallResult - Lower the result values of an ISD::CALL into the
407 /// appropriate copies out of appropriate physical registers. This assumes that
408 /// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
409 /// being lowered. Returns a SDNode with the same number of values as the
412 HexagonTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
413 CallingConv::ID CallConv, bool isVarArg,
415 SmallVectorImpl<ISD::InputArg> &Ins,
416 SDLoc dl, SelectionDAG &DAG,
417 SmallVectorImpl<SDValue> &InVals,
418 const SmallVectorImpl<SDValue> &OutVals,
419 SDValue Callee) const {
421 // Assign locations to each value returned by this call.
422 SmallVector<CCValAssign, 16> RVLocs;
424 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
427 CCInfo.AnalyzeCallResult(Ins, RetCC_Hexagon);
429 // Copy all of the result registers out of their specified physreg.
430 for (unsigned i = 0; i != RVLocs.size(); ++i) {
431 Chain = DAG.getCopyFromReg(Chain, dl,
432 RVLocs[i].getLocReg(),
433 RVLocs[i].getValVT(), InFlag).getValue(1);
434 InFlag = Chain.getValue(2);
435 InVals.push_back(Chain.getValue(0));
441 /// LowerCall - Functions arguments are copied from virtual regs to
442 /// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
444 HexagonTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
445 SmallVectorImpl<SDValue> &InVals) const {
446 SelectionDAG &DAG = CLI.DAG;
448 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
449 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
450 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
451 SDValue Chain = CLI.Chain;
452 SDValue Callee = CLI.Callee;
453 bool &isTailCall = CLI.IsTailCall;
454 CallingConv::ID CallConv = CLI.CallConv;
455 bool isVarArg = CLI.IsVarArg;
456 bool doesNotReturn = CLI.DoesNotReturn;
458 bool IsStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
459 MachineFunction &MF = DAG.getMachineFunction();
461 // Check for varargs.
462 int NumNamedVarArgParams = -1;
463 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Callee))
465 const Function* CalleeFn = nullptr;
466 Callee = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, MVT::i32);
467 if ((CalleeFn = dyn_cast<Function>(GA->getGlobal())))
469 // If a function has zero args and is a vararg function, that's
470 // disallowed so it must be an undeclared function. Do not assume
471 // varargs if the callee is undefined.
472 if (CalleeFn->isVarArg() &&
473 CalleeFn->getFunctionType()->getNumParams() != 0) {
474 NumNamedVarArgParams = CalleeFn->getFunctionType()->getNumParams();
479 // Analyze operands of the call, assigning locations to each operand.
480 SmallVector<CCValAssign, 16> ArgLocs;
481 HexagonCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
482 *DAG.getContext(), NumNamedVarArgParams);
485 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon_VarArg);
487 CCInfo.AnalyzeCallOperands(Outs, CC_Hexagon);
489 if (DAG.getTarget().Options.DisableTailCalls)
493 bool StructAttrFlag = MF.getFunction()->hasStructRetAttr();
494 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
495 isVarArg, IsStructRet,
497 Outs, OutVals, Ins, DAG);
498 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
499 CCValAssign &VA = ArgLocs[i];
505 DEBUG(dbgs() << (isTailCall ? "Eligible for Tail Call\n"
506 : "Argument must be passed on stack. "
507 "Not eligible for Tail Call\n"));
509 // Get a count of how many bytes are to be pushed on the stack.
510 unsigned NumBytes = CCInfo.getNextStackOffset();
511 SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
512 SmallVector<SDValue, 8> MemOpChains;
514 auto &HRI = *Subtarget.getRegisterInfo();
515 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, HRI.getStackRegister(),
518 // Walk the register/memloc assignments, inserting copies/loads.
519 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
520 CCValAssign &VA = ArgLocs[i];
521 SDValue Arg = OutVals[i];
522 ISD::ArgFlagsTy Flags = Outs[i].Flags;
524 // Promote the value if needed.
525 switch (VA.getLocInfo()) {
527 // Loc info must be one of Full, SExt, ZExt, or AExt.
528 llvm_unreachable("Unknown loc info!");
529 case CCValAssign::BCvt:
530 case CCValAssign::Full:
532 case CCValAssign::SExt:
533 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
535 case CCValAssign::ZExt:
536 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
538 case CCValAssign::AExt:
539 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
544 unsigned LocMemOffset = VA.getLocMemOffset();
545 SDValue MemAddr = DAG.getConstant(LocMemOffset, dl,
546 StackPtr.getValueType());
547 MemAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, MemAddr);
548 if (Flags.isByVal()) {
549 // The argument is a struct passed by value. According to LLVM, "Arg"
551 MemOpChains.push_back(CreateCopyOfByValArgument(Arg, MemAddr, Chain,
554 MachinePointerInfo LocPI = MachinePointerInfo::getStack(LocMemOffset);
555 SDValue S = DAG.getStore(Chain, dl, Arg, MemAddr, LocPI, false,
557 MemOpChains.push_back(S);
562 // Arguments that can be passed on register must be kept at RegsToPass
565 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
568 // Transform all store nodes into one single node because all store
569 // nodes are independent of each other.
570 if (!MemOpChains.empty())
571 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
574 SDValue C = DAG.getConstant(NumBytes, dl, getPointerTy(), true);
575 Chain = DAG.getCALLSEQ_START(Chain, C, dl);
578 // Build a sequence of copy-to-reg nodes chained together with token
579 // chain and flag operands which copy the outgoing args into registers.
580 // The InFlag in necessary since all emitted instructions must be
584 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
585 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
586 RegsToPass[i].second, InFlag);
587 InFlag = Chain.getValue(1);
590 // For tail calls lower the arguments to the 'real' stack slot.
592 // Force all the incoming stack arguments to be loaded from the stack
593 // before any new outgoing arguments are stored to the stack, because the
594 // outgoing stack slots may alias the incoming argument stack slots, and
595 // the alias isn't otherwise explicit. This is slightly more conservative
596 // than necessary, because it means that each store effectively depends
597 // on every argument instead of just those arguments it would clobber.
599 // Do not flag preceding copytoreg stuff together with the following stuff.
601 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
602 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
603 RegsToPass[i].second, InFlag);
604 InFlag = Chain.getValue(1);
609 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
610 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
611 // node so that legalize doesn't hack it.
612 if (flag_aligned_memcpy) {
613 const char *MemcpyName =
614 "__hexagon_memcpy_likely_aligned_min32bytes_mult8bytes";
615 Callee = DAG.getTargetExternalSymbol(MemcpyName, getPointerTy());
616 flag_aligned_memcpy = false;
617 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
618 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy());
619 } else if (ExternalSymbolSDNode *S =
620 dyn_cast<ExternalSymbolSDNode>(Callee)) {
621 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
624 // Returns a chain & a flag for retval copy to use.
625 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
626 SmallVector<SDValue, 8> Ops;
627 Ops.push_back(Chain);
628 Ops.push_back(Callee);
630 // Add argument registers to the end of the list so that they are
631 // known live into the call.
632 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
633 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
634 RegsToPass[i].second.getValueType()));
637 if (InFlag.getNode())
638 Ops.push_back(InFlag);
641 MF.getFrameInfo()->setHasTailCall();
642 return DAG.getNode(HexagonISD::TC_RETURN, dl, NodeTys, Ops);
645 int OpCode = doesNotReturn ? HexagonISD::CALLv3nr : HexagonISD::CALLv3;
646 Chain = DAG.getNode(OpCode, dl, NodeTys, Ops);
647 InFlag = Chain.getValue(1);
649 // Create the CALLSEQ_END node.
650 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
651 DAG.getIntPtrConstant(0, dl, true), InFlag, dl);
652 InFlag = Chain.getValue(1);
654 // Handle result values, copying them out of physregs into vregs that we
656 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
657 InVals, OutVals, Callee);
660 static bool getIndexedAddressParts(SDNode *Ptr, EVT VT,
661 bool isSEXTLoad, SDValue &Base,
662 SDValue &Offset, bool &isInc,
664 if (Ptr->getOpcode() != ISD::ADD)
667 if (VT == MVT::i64 || VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
668 isInc = (Ptr->getOpcode() == ISD::ADD);
669 Base = Ptr->getOperand(0);
670 Offset = Ptr->getOperand(1);
671 // Ensure that Offset is a constant.
672 return (isa<ConstantSDNode>(Offset));
678 // TODO: Put this function along with the other isS* functions in
679 // HexagonISelDAGToDAG.cpp into a common file. Or better still, use the
680 // functions defined in HexagonOperands.td.
681 static bool Is_PostInc_S4_Offset(SDNode * S, int ShiftAmount) {
682 ConstantSDNode *N = cast<ConstantSDNode>(S);
684 // immS4 predicate - True if the immediate fits in a 4-bit sign extended.
686 int64_t v = (int64_t)N->getSExtValue();
688 if (ShiftAmount > 0) {
690 v = v >> ShiftAmount;
692 return (v <= 7) && (v >= -8) && (m == 0);
695 /// getPostIndexedAddressParts - returns true by value, base pointer and
696 /// offset pointer and addressing mode by reference if this node can be
697 /// combined with a load / store to form a post-indexed load / store.
698 bool HexagonTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
701 ISD::MemIndexedMode &AM,
702 SelectionDAG &DAG) const
706 bool isSEXTLoad = false;
708 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
709 VT = LD->getMemoryVT();
710 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
711 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
712 VT = ST->getMemoryVT();
713 if (ST->getValue().getValueType() == MVT::i64 && ST->isTruncatingStore()) {
721 bool isLegal = getIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
723 // ShiftAmount = number of left-shifted bits in the Hexagon instruction.
724 int ShiftAmount = VT.getSizeInBits() / 16;
725 if (isLegal && Is_PostInc_S4_Offset(Offset.getNode(), ShiftAmount)) {
726 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
733 SDValue HexagonTargetLowering::LowerINLINEASM(SDValue Op,
734 SelectionDAG &DAG) const {
735 SDNode *Node = Op.getNode();
736 MachineFunction &MF = DAG.getMachineFunction();
737 auto &FuncInfo = *MF.getInfo<HexagonMachineFunctionInfo>();
738 switch (Node->getOpcode()) {
739 case ISD::INLINEASM: {
740 unsigned NumOps = Node->getNumOperands();
741 if (Node->getOperand(NumOps-1).getValueType() == MVT::Glue)
742 --NumOps; // Ignore the flag operand.
744 for (unsigned i = InlineAsm::Op_FirstOperand; i != NumOps;) {
745 if (FuncInfo.hasClobberLR())
748 cast<ConstantSDNode>(Node->getOperand(i))->getZExtValue();
749 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
750 ++i; // Skip the ID value.
752 switch (InlineAsm::getKind(Flags)) {
753 default: llvm_unreachable("Bad flags!");
754 case InlineAsm::Kind_RegDef:
755 case InlineAsm::Kind_RegUse:
756 case InlineAsm::Kind_Imm:
757 case InlineAsm::Kind_Clobber:
758 case InlineAsm::Kind_Mem: {
759 for (; NumVals; --NumVals, ++i) {}
762 case InlineAsm::Kind_RegDefEarlyClobber: {
763 for (; NumVals; --NumVals, ++i) {
765 cast<RegisterSDNode>(Node->getOperand(i))->getReg();
768 const HexagonRegisterInfo *QRI = Subtarget.getRegisterInfo();
769 if (Reg == QRI->getRARegister()) {
770 FuncInfo.setHasClobberLR(true);
785 // Taken from the XCore backend.
787 SDValue HexagonTargetLowering::
788 LowerBR_JT(SDValue Op, SelectionDAG &DAG) const
790 SDValue Chain = Op.getOperand(0);
791 SDValue Table = Op.getOperand(1);
792 SDValue Index = Op.getOperand(2);
794 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
795 unsigned JTI = JT->getIndex();
796 MachineFunction &MF = DAG.getMachineFunction();
797 const MachineJumpTableInfo *MJTI = MF.getJumpTableInfo();
798 SDValue TargetJT = DAG.getTargetJumpTable(JT->getIndex(), MVT::i32);
800 // Mark all jump table targets as address taken.
801 const std::vector<MachineJumpTableEntry> &JTE = MJTI->getJumpTables();
802 const std::vector<MachineBasicBlock*> &JTBBs = JTE[JTI].MBBs;
803 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
804 MachineBasicBlock *MBB = JTBBs[i];
805 MBB->setHasAddressTaken();
806 // This line is needed to set the hasAddressTaken flag on the BasicBlock
808 BlockAddress::get(const_cast<BasicBlock *>(MBB->getBasicBlock()));
811 SDValue JumpTableBase = DAG.getNode(HexagonISD::JT, dl,
812 getPointerTy(), TargetJT);
813 SDValue ShiftIndex = DAG.getNode(ISD::SHL, dl, MVT::i32, Index,
814 DAG.getConstant(2, dl, MVT::i32));
815 SDValue JTAddress = DAG.getNode(ISD::ADD, dl, MVT::i32, JumpTableBase,
817 SDValue LoadTarget = DAG.getLoad(MVT::i32, dl, Chain, JTAddress,
818 MachinePointerInfo(), false, false, false,
820 return DAG.getNode(HexagonISD::BR_JT, dl, MVT::Other, Chain, LoadTarget);
825 HexagonTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
826 SelectionDAG &DAG) const {
827 SDValue Chain = Op.getOperand(0);
828 SDValue Size = Op.getOperand(1);
829 SDValue Align = Op.getOperand(2);
832 ConstantSDNode *AlignConst = dyn_cast<ConstantSDNode>(Align);
833 assert(AlignConst && "Non-constant Align in LowerDYNAMIC_STACKALLOC");
835 unsigned A = AlignConst->getSExtValue();
836 auto &HFI = *Subtarget.getFrameLowering();
837 // "Zero" means natural stack alignment.
839 A = HFI.getStackAlignment();
842 dbgs () << LLVM_FUNCTION_NAME << " Align: " << A << " Size: ";
843 Size.getNode()->dump(&DAG);
847 SDValue AC = DAG.getConstant(A, dl, MVT::i32);
848 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other);
849 return DAG.getNode(HexagonISD::ALLOCA, dl, VTs, Chain, Size, AC);
853 HexagonTargetLowering::LowerFormalArguments(SDValue Chain,
854 CallingConv::ID CallConv,
857 SmallVectorImpl<ISD::InputArg> &Ins,
858 SDLoc dl, SelectionDAG &DAG,
859 SmallVectorImpl<SDValue> &InVals)
862 MachineFunction &MF = DAG.getMachineFunction();
863 MachineFrameInfo *MFI = MF.getFrameInfo();
864 MachineRegisterInfo &RegInfo = MF.getRegInfo();
865 auto &FuncInfo = *MF.getInfo<HexagonMachineFunctionInfo>();
867 // Assign locations to all of the incoming arguments.
868 SmallVector<CCValAssign, 16> ArgLocs;
869 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
872 CCInfo.AnalyzeFormalArguments(Ins, CC_Hexagon);
874 // For LLVM, in the case when returning a struct by value (>8byte),
875 // the first argument is a pointer that points to the location on caller's
876 // stack where the return value will be stored. For Hexagon, the location on
877 // caller's stack is passed only when the struct size is smaller than (and
878 // equal to) 8 bytes. If not, no address will be passed into callee and
879 // callee return the result direclty through R0/R1.
881 SmallVector<SDValue, 4> MemOps;
883 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
884 CCValAssign &VA = ArgLocs[i];
885 ISD::ArgFlagsTy Flags = Ins[i].Flags;
887 unsigned StackLocation;
890 if ( (VA.isRegLoc() && !Flags.isByVal())
891 || (VA.isRegLoc() && Flags.isByVal() && Flags.getByValSize() > 8)) {
892 // Arguments passed in registers
893 // 1. int, long long, ptr args that get allocated in register.
894 // 2. Large struct that gets an register to put its address in.
895 EVT RegVT = VA.getLocVT();
896 if (RegVT == MVT::i8 || RegVT == MVT::i16 ||
897 RegVT == MVT::i32 || RegVT == MVT::f32) {
899 RegInfo.createVirtualRegister(&Hexagon::IntRegsRegClass);
900 RegInfo.addLiveIn(VA.getLocReg(), VReg);
901 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
902 } else if (RegVT == MVT::i64 || RegVT == MVT::f64) {
904 RegInfo.createVirtualRegister(&Hexagon::DoubleRegsRegClass);
905 RegInfo.addLiveIn(VA.getLocReg(), VReg);
906 InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
910 } else if (VA.isRegLoc() && Flags.isByVal() && Flags.getByValSize() <= 8) {
911 assert (0 && "ByValSize must be bigger than 8 bytes");
914 assert(VA.isMemLoc());
916 if (Flags.isByVal()) {
917 // If it's a byval parameter, then we need to compute the
918 // "real" size, not the size of the pointer.
919 ObjSize = Flags.getByValSize();
921 ObjSize = VA.getLocVT().getStoreSizeInBits() >> 3;
924 StackLocation = HEXAGON_LRFP_SIZE + VA.getLocMemOffset();
925 // Create the frame index object for this incoming parameter...
926 FI = MFI->CreateFixedObject(ObjSize, StackLocation, true);
928 // Create the SelectionDAG nodes cordl, responding to a load
929 // from this parameter.
930 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
932 if (Flags.isByVal()) {
933 // If it's a pass-by-value aggregate, then do not dereference the stack
934 // location. Instead, we should generate a reference to the stack
936 InVals.push_back(FIN);
938 InVals.push_back(DAG.getLoad(VA.getLocVT(), dl, Chain, FIN,
939 MachinePointerInfo(), false, false,
946 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
949 // This will point to the next argument passed via stack.
950 int FrameIndex = MFI->CreateFixedObject(Hexagon_PointerSize,
952 CCInfo.getNextStackOffset(),
954 FuncInfo.setVarArgsFrameIndex(FrameIndex);
961 HexagonTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
962 // VASTART stores the address of the VarArgsFrameIndex slot into the
963 // memory location argument.
964 MachineFunction &MF = DAG.getMachineFunction();
965 HexagonMachineFunctionInfo *QFI = MF.getInfo<HexagonMachineFunctionInfo>();
966 SDValue Addr = DAG.getFrameIndex(QFI->getVarArgsFrameIndex(), MVT::i32);
967 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
968 return DAG.getStore(Op.getOperand(0), SDLoc(Op), Addr,
969 Op.getOperand(1), MachinePointerInfo(SV), false,
973 // Creates a SPLAT instruction for a constant value VAL.
974 static SDValue createSplat(SelectionDAG &DAG, SDLoc dl, EVT VT, SDValue Val) {
975 if (VT.getSimpleVT() == MVT::v4i8)
976 return DAG.getNode(HexagonISD::VSPLATB, dl, VT, Val);
978 if (VT.getSimpleVT() == MVT::v4i16)
979 return DAG.getNode(HexagonISD::VSPLATH, dl, VT, Val);
984 static bool isSExtFree(SDValue N) {
985 // A sign-extend of a truncate of a sign-extend is free.
986 if (N.getOpcode() == ISD::TRUNCATE &&
987 N.getOperand(0).getOpcode() == ISD::AssertSext)
989 // We have sign-extended loads.
990 if (N.getOpcode() == ISD::LOAD)
995 SDValue HexagonTargetLowering::LowerCTPOP(SDValue Op, SelectionDAG &DAG) const {
997 SDValue InpVal = Op.getOperand(0);
998 if (isa<ConstantSDNode>(InpVal)) {
999 uint64_t V = cast<ConstantSDNode>(InpVal)->getZExtValue();
1000 return DAG.getTargetConstant(countPopulation(V), dl, MVT::i64);
1002 SDValue PopOut = DAG.getNode(HexagonISD::POPCOUNT, dl, MVT::i32, InpVal);
1003 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, PopOut);
1006 SDValue HexagonTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
1009 SDValue LHS = Op.getOperand(0);
1010 SDValue RHS = Op.getOperand(1);
1011 SDValue Cmp = Op.getOperand(2);
1012 ISD::CondCode CC = cast<CondCodeSDNode>(Cmp)->get();
1014 EVT VT = Op.getValueType();
1015 EVT LHSVT = LHS.getValueType();
1016 EVT RHSVT = RHS.getValueType();
1018 if (LHSVT == MVT::v2i16) {
1019 assert(ISD::isSignedIntSetCC(CC) || ISD::isUnsignedIntSetCC(CC));
1020 unsigned ExtOpc = ISD::isSignedIntSetCC(CC) ? ISD::SIGN_EXTEND
1022 SDValue LX = DAG.getNode(ExtOpc, dl, MVT::v2i32, LHS);
1023 SDValue RX = DAG.getNode(ExtOpc, dl, MVT::v2i32, RHS);
1024 SDValue SC = DAG.getNode(ISD::SETCC, dl, MVT::v2i1, LX, RX, Cmp);
1028 // Treat all other vector types as legal.
1032 // Equals and not equals should use sign-extend, not zero-extend, since
1033 // we can represent small negative values in the compare instructions.
1034 // The LLVM default is to use zero-extend arbitrarily in these cases.
1035 if ((CC == ISD::SETEQ || CC == ISD::SETNE) &&
1036 (RHSVT == MVT::i8 || RHSVT == MVT::i16) &&
1037 (LHSVT == MVT::i8 || LHSVT == MVT::i16)) {
1038 ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS);
1039 if (C && C->getAPIntValue().isNegative()) {
1040 LHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, LHS);
1041 RHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, RHS);
1042 return DAG.getNode(ISD::SETCC, dl, Op.getValueType(),
1043 LHS, RHS, Op.getOperand(2));
1045 if (isSExtFree(LHS) || isSExtFree(RHS)) {
1046 LHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, LHS);
1047 RHS = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, RHS);
1048 return DAG.getNode(ISD::SETCC, dl, Op.getValueType(),
1049 LHS, RHS, Op.getOperand(2));
1055 SDValue HexagonTargetLowering::LowerVSELECT(SDValue Op, SelectionDAG &DAG)
1057 SDValue PredOp = Op.getOperand(0);
1058 SDValue Op1 = Op.getOperand(1), Op2 = Op.getOperand(2);
1059 EVT OpVT = Op1.getValueType();
1062 if (OpVT == MVT::v2i16) {
1063 SDValue X1 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i32, Op1);
1064 SDValue X2 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i32, Op2);
1065 SDValue SL = DAG.getNode(ISD::VSELECT, DL, MVT::v2i32, PredOp, X1, X2);
1066 SDValue TR = DAG.getNode(ISD::TRUNCATE, DL, MVT::v2i16, SL);
1073 // Handle only specific vector loads.
1074 SDValue HexagonTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1075 EVT VT = Op.getValueType();
1077 LoadSDNode *LoadNode = cast<LoadSDNode>(Op);
1078 SDValue Chain = LoadNode->getChain();
1079 SDValue Ptr = Op.getOperand(1);
1080 SDValue LoweredLoad;
1082 SDValue Base = LoadNode->getBasePtr();
1083 ISD::LoadExtType Ext = LoadNode->getExtensionType();
1084 unsigned Alignment = LoadNode->getAlignment();
1087 if(Ext == ISD::NON_EXTLOAD)
1088 Ext = ISD::ZEXTLOAD;
1090 if (VT == MVT::v4i16) {
1091 if (Alignment == 2) {
1094 Loads[0] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Base,
1095 LoadNode->getPointerInfo(), MVT::i16,
1096 LoadNode->isVolatile(),
1097 LoadNode->isNonTemporal(),
1098 LoadNode->isInvariant(),
1101 SDValue Increment = DAG.getConstant(2, DL, MVT::i32);
1102 Ptr = DAG.getNode(ISD::ADD, DL, Base.getValueType(), Base, Increment);
1103 Loads[1] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Ptr,
1104 LoadNode->getPointerInfo(), MVT::i16,
1105 LoadNode->isVolatile(),
1106 LoadNode->isNonTemporal(),
1107 LoadNode->isInvariant(),
1109 // SHL 16, then OR base and base+2.
1110 SDValue ShiftAmount = DAG.getConstant(16, DL, MVT::i32);
1111 SDValue Tmp1 = DAG.getNode(ISD::SHL, DL, MVT::i32, Loads[1], ShiftAmount);
1112 SDValue Tmp2 = DAG.getNode(ISD::OR, DL, MVT::i32, Tmp1, Loads[0]);
1114 Increment = DAG.getConstant(4, DL, MVT::i32);
1115 Ptr = DAG.getNode(ISD::ADD, DL, Base.getValueType(), Base, Increment);
1116 Loads[2] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Ptr,
1117 LoadNode->getPointerInfo(), MVT::i16,
1118 LoadNode->isVolatile(),
1119 LoadNode->isNonTemporal(),
1120 LoadNode->isInvariant(),
1123 Increment = DAG.getConstant(6, DL, MVT::i32);
1124 Ptr = DAG.getNode(ISD::ADD, DL, Base.getValueType(), Base, Increment);
1125 Loads[3] = DAG.getExtLoad(Ext, DL, MVT::i32, Chain, Ptr,
1126 LoadNode->getPointerInfo(), MVT::i16,
1127 LoadNode->isVolatile(),
1128 LoadNode->isNonTemporal(),
1129 LoadNode->isInvariant(),
1131 // SHL 16, then OR base+4 and base+6.
1132 Tmp1 = DAG.getNode(ISD::SHL, DL, MVT::i32, Loads[3], ShiftAmount);
1133 SDValue Tmp4 = DAG.getNode(ISD::OR, DL, MVT::i32, Tmp1, Loads[2]);
1134 // Combine to i64. This could be optimised out later if we can
1135 // affect reg allocation of this code.
1136 Result = DAG.getNode(HexagonISD::COMBINE, DL, MVT::i64, Tmp4, Tmp2);
1137 LoadChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
1138 Loads[0].getValue(1), Loads[1].getValue(1),
1139 Loads[2].getValue(1), Loads[3].getValue(1));
1141 // Perform default type expansion.
1142 Result = DAG.getLoad(MVT::i64, DL, Chain, Ptr, LoadNode->getPointerInfo(),
1143 LoadNode->isVolatile(), LoadNode->isNonTemporal(),
1144 LoadNode->isInvariant(), LoadNode->getAlignment());
1145 LoadChain = Result.getValue(1);
1148 llvm_unreachable("Custom lowering unsupported load");
1150 Result = DAG.getNode(ISD::BITCAST, DL, VT, Result);
1151 // Since we pretend to lower a load, we need the original chain
1152 // info attached to the result.
1153 SDValue Ops[] = { Result, LoadChain };
1155 return DAG.getMergeValues(Ops, DL);
1160 HexagonTargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
1161 EVT ValTy = Op.getValueType();
1163 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
1165 if (CP->isMachineConstantPoolEntry())
1166 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), ValTy,
1167 CP->getAlignment());
1169 Res = DAG.getTargetConstantPool(CP->getConstVal(), ValTy,
1170 CP->getAlignment());
1171 return DAG.getNode(HexagonISD::CP, dl, ValTy, Res);
1175 HexagonTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const {
1176 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
1177 MachineFunction &MF = DAG.getMachineFunction();
1178 MachineFrameInfo &MFI = *MF.getFrameInfo();
1179 MFI.setReturnAddressIsTaken(true);
1181 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
1184 EVT VT = Op.getValueType();
1186 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1188 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
1189 SDValue Offset = DAG.getConstant(4, dl, MVT::i32);
1190 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
1191 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
1192 MachinePointerInfo(), false, false, false, 0);
1195 // Return LR, which contains the return address. Mark it an implicit live-in.
1196 unsigned Reg = MF.addLiveIn(HRI.getRARegister(), getRegClassFor(MVT::i32));
1197 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
1201 HexagonTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
1202 const HexagonRegisterInfo &HRI = *Subtarget.getRegisterInfo();
1203 MachineFrameInfo &MFI = *DAG.getMachineFunction().getFrameInfo();
1204 MFI.setFrameAddressIsTaken(true);
1206 EVT VT = Op.getValueType();
1208 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1209 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
1210 HRI.getFrameRegister(), VT);
1212 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
1213 MachinePointerInfo(),
1214 false, false, false, 0);
1218 SDValue HexagonTargetLowering::LowerATOMIC_FENCE(SDValue Op,
1219 SelectionDAG& DAG) const {
1221 return DAG.getNode(HexagonISD::BARRIER, dl, MVT::Other, Op.getOperand(0));
1225 SDValue HexagonTargetLowering::LowerGLOBALADDRESS(SDValue Op,
1226 SelectionDAG &DAG) const {
1228 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1229 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
1231 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
1233 const HexagonTargetObjectFile *TLOF =
1234 static_cast<const HexagonTargetObjectFile *>(
1235 getTargetMachine().getObjFileLowering());
1236 if (TLOF->IsGlobalInSmallSection(GV, getTargetMachine())) {
1237 return DAG.getNode(HexagonISD::CONST32_GP, dl, getPointerTy(), Result);
1240 return DAG.getNode(HexagonISD::CONST32, dl, getPointerTy(), Result);
1243 // Specifies that for loads and stores VT can be promoted to PromotedLdStVT.
1244 void HexagonTargetLowering::promoteLdStType(EVT VT, EVT PromotedLdStVT) {
1245 if (VT != PromotedLdStVT) {
1246 setOperationAction(ISD::LOAD, VT.getSimpleVT(), Promote);
1247 AddPromotedToType(ISD::LOAD, VT.getSimpleVT(),
1248 PromotedLdStVT.getSimpleVT());
1250 setOperationAction(ISD::STORE, VT.getSimpleVT(), Promote);
1251 AddPromotedToType(ISD::STORE, VT.getSimpleVT(),
1252 PromotedLdStVT.getSimpleVT());
1257 HexagonTargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
1258 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
1259 SDValue BA_SD = DAG.getTargetBlockAddress(BA, MVT::i32);
1261 return DAG.getNode(HexagonISD::CONST32_GP, dl, getPointerTy(), BA_SD);
1264 //===----------------------------------------------------------------------===//
1265 // TargetLowering Implementation
1266 //===----------------------------------------------------------------------===//
1268 HexagonTargetLowering::HexagonTargetLowering(const TargetMachine &TM,
1269 const HexagonSubtarget &STI)
1270 : TargetLowering(TM), HTM(static_cast<const HexagonTargetMachine&>(TM)),
1272 bool IsV4 = !Subtarget.hasV5TOps();
1273 auto &HRI = *Subtarget.getRegisterInfo();
1275 setPrefLoopAlignment(4);
1276 setPrefFunctionAlignment(4);
1277 setMinFunctionAlignment(2);
1278 setInsertFencesForAtomic(false);
1279 setExceptionPointerRegister(Hexagon::R0);
1280 setExceptionSelectorRegister(Hexagon::R1);
1281 setStackPointerRegisterToSaveRestore(HRI.getStackRegister());
1283 if (EnableHexSDNodeSched)
1284 setSchedulingPreference(Sched::VLIW);
1286 setSchedulingPreference(Sched::Source);
1288 // Limits for inline expansion of memcpy/memmove
1289 MaxStoresPerMemcpy = MaxStoresPerMemcpyCL;
1290 MaxStoresPerMemcpyOptSize = MaxStoresPerMemcpyOptSizeCL;
1291 MaxStoresPerMemmove = MaxStoresPerMemmoveCL;
1292 MaxStoresPerMemmoveOptSize = MaxStoresPerMemmoveOptSizeCL;
1293 MaxStoresPerMemset = MaxStoresPerMemsetCL;
1294 MaxStoresPerMemsetOptSize = MaxStoresPerMemsetOptSizeCL;
1297 // Set up register classes.
1300 addRegisterClass(MVT::i1, &Hexagon::PredRegsRegClass);
1301 addRegisterClass(MVT::v2i1, &Hexagon::PredRegsRegClass); // bbbbaaaa
1302 addRegisterClass(MVT::v4i1, &Hexagon::PredRegsRegClass); // ddccbbaa
1303 addRegisterClass(MVT::v8i1, &Hexagon::PredRegsRegClass); // hgfedcba
1304 addRegisterClass(MVT::i32, &Hexagon::IntRegsRegClass);
1305 addRegisterClass(MVT::v4i8, &Hexagon::IntRegsRegClass);
1306 addRegisterClass(MVT::v2i16, &Hexagon::IntRegsRegClass);
1307 addRegisterClass(MVT::i64, &Hexagon::DoubleRegsRegClass);
1308 addRegisterClass(MVT::v8i8, &Hexagon::DoubleRegsRegClass);
1309 addRegisterClass(MVT::v4i16, &Hexagon::DoubleRegsRegClass);
1310 addRegisterClass(MVT::v2i32, &Hexagon::DoubleRegsRegClass);
1312 if (Subtarget.hasV5TOps()) {
1313 addRegisterClass(MVT::f32, &Hexagon::IntRegsRegClass);
1314 addRegisterClass(MVT::f64, &Hexagon::DoubleRegsRegClass);
1318 // Handling of scalar operations.
1320 // All operations default to "legal", except:
1321 // - indexed loads and stores (pre-/post-incremented),
1322 // - ANY_EXTEND_VECTOR_INREG, ATOMIC_CMP_SWAP_WITH_SUCCESS, CONCAT_VECTORS,
1323 // ConstantFP, DEBUGTRAP, FCEIL, FCOPYSIGN, FEXP, FEXP2, FFLOOR, FGETSIGN,
1324 // FLOG, FLOG2, FLOG10, FMAXNUM, FMINNUM, FNEARBYINT, FRINT, FROUND, TRAP,
1325 // FTRUNC, PREFETCH, SIGN_EXTEND_VECTOR_INREG, ZERO_EXTEND_VECTOR_INREG,
1326 // which default to "expand" for at least one type.
1329 setOperationAction(ISD::ConstantFP, MVT::f32, Legal); // Default: expand
1330 setOperationAction(ISD::ConstantFP, MVT::f64, Legal); // Default: expand
1332 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
1333 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
1334 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
1335 setOperationAction(ISD::INLINEASM, MVT::Other, Custom);
1336 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
1337 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
1339 // Custom legalize GlobalAddress nodes into CONST32.
1340 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
1341 setOperationAction(ISD::GlobalAddress, MVT::i8, Custom);
1342 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
1344 // Hexagon needs to optimize cases with negative constants.
1345 setOperationAction(ISD::SETCC, MVT::i8, Custom);
1346 setOperationAction(ISD::SETCC, MVT::i16, Custom);
1348 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
1349 setOperationAction(ISD::VASTART, MVT::Other, Custom);
1350 setOperationAction(ISD::VAEND, MVT::Other, Expand);
1351 setOperationAction(ISD::VAARG, MVT::Other, Expand);
1353 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
1354 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
1355 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
1358 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
1360 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
1361 // Increase jump tables cutover to 5, was 4.
1362 setMinimumJumpTableEntries(MinimumJumpTables);
1364 // Hexagon has instructions for add/sub with carry. The problem with
1365 // modeling these instructions is that they produce 2 results: Rdd and Px.
1366 // To model the update of Px, we will have to use Defs[p0..p3] which will
1367 // cause any predicate live range to spill. So, we pretend we dont't have
1368 // these instructions.
1369 setOperationAction(ISD::ADDE, MVT::i8, Expand);
1370 setOperationAction(ISD::ADDE, MVT::i16, Expand);
1371 setOperationAction(ISD::ADDE, MVT::i32, Expand);
1372 setOperationAction(ISD::ADDE, MVT::i64, Expand);
1373 setOperationAction(ISD::SUBE, MVT::i8, Expand);
1374 setOperationAction(ISD::SUBE, MVT::i16, Expand);
1375 setOperationAction(ISD::SUBE, MVT::i32, Expand);
1376 setOperationAction(ISD::SUBE, MVT::i64, Expand);
1377 setOperationAction(ISD::ADDC, MVT::i8, Expand);
1378 setOperationAction(ISD::ADDC, MVT::i16, Expand);
1379 setOperationAction(ISD::ADDC, MVT::i32, Expand);
1380 setOperationAction(ISD::ADDC, MVT::i64, Expand);
1381 setOperationAction(ISD::SUBC, MVT::i8, Expand);
1382 setOperationAction(ISD::SUBC, MVT::i16, Expand);
1383 setOperationAction(ISD::SUBC, MVT::i32, Expand);
1384 setOperationAction(ISD::SUBC, MVT::i64, Expand);
1386 // Only add and sub that detect overflow are the saturating ones.
1387 for (MVT VT : MVT::integer_valuetypes()) {
1388 setOperationAction(ISD::UADDO, VT, Expand);
1389 setOperationAction(ISD::SADDO, VT, Expand);
1390 setOperationAction(ISD::USUBO, VT, Expand);
1391 setOperationAction(ISD::SSUBO, VT, Expand);
1394 setOperationAction(ISD::CTLZ, MVT::i8, Promote);
1395 setOperationAction(ISD::CTLZ, MVT::i16, Promote);
1396 setOperationAction(ISD::CTTZ, MVT::i8, Promote);
1397 setOperationAction(ISD::CTTZ, MVT::i16, Promote);
1398 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8, Promote);
1399 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16, Promote);
1400 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i8, Promote);
1401 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16, Promote);
1403 // In V5, popcount can count # of 1s in i64 but returns i32.
1404 // On V4 it will be expanded (set later).
1405 setOperationAction(ISD::CTPOP, MVT::i8, Promote);
1406 setOperationAction(ISD::CTPOP, MVT::i16, Promote);
1407 setOperationAction(ISD::CTPOP, MVT::i32, Promote);
1408 setOperationAction(ISD::CTPOP, MVT::i64, Custom);
1410 // We custom lower i64 to i64 mul, so that it is not considered as a legal
1411 // operation. There is a pattern that will match i64 mul and transform it
1412 // to a series of instructions.
1413 setOperationAction(ISD::MUL, MVT::i64, Expand);
1414 setOperationAction(ISD::MULHS, MVT::i64, Expand);
1416 for (unsigned IntExpOp :
1417 {ISD::SDIV, ISD::UDIV, ISD::SREM, ISD::UREM, ISD::SDIVREM, ISD::UDIVREM,
1418 ISD::ROTL, ISD::ROTR, ISD::BSWAP, ISD::SHL_PARTS, ISD::SRA_PARTS,
1419 ISD::SRL_PARTS, ISD::SMUL_LOHI, ISD::UMUL_LOHI}) {
1420 setOperationAction(IntExpOp, MVT::i32, Expand);
1421 setOperationAction(IntExpOp, MVT::i64, Expand);
1424 for (unsigned FPExpOp :
1425 {ISD::FDIV, ISD::FREM, ISD::FSQRT, ISD::FSIN, ISD::FCOS, ISD::FSINCOS,
1426 ISD::FPOW, ISD::FCOPYSIGN}) {
1427 setOperationAction(FPExpOp, MVT::f32, Expand);
1428 setOperationAction(FPExpOp, MVT::f64, Expand);
1431 // No extending loads from i32.
1432 for (MVT VT : MVT::integer_valuetypes()) {
1433 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand);
1434 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand);
1435 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand);
1437 // Turn FP truncstore into trunc + store.
1438 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
1439 // Turn FP extload into load/fextend.
1440 for (MVT VT : MVT::fp_valuetypes())
1441 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
1443 // Expand BR_CC and SELECT_CC for all integer and fp types.
1444 for (MVT VT : MVT::integer_valuetypes()) {
1445 setOperationAction(ISD::BR_CC, VT, Expand);
1446 setOperationAction(ISD::SELECT_CC, VT, Expand);
1448 for (MVT VT : MVT::fp_valuetypes()) {
1449 setOperationAction(ISD::BR_CC, VT, Expand);
1450 setOperationAction(ISD::SELECT_CC, VT, Expand);
1452 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
1455 // Handling of vector operations.
1458 // Custom lower v4i16 load only. Let v4i16 store to be
1459 // promoted for now.
1460 promoteLdStType(MVT::v4i8, MVT::i32);
1461 promoteLdStType(MVT::v2i16, MVT::i32);
1462 promoteLdStType(MVT::v8i8, MVT::i64);
1463 promoteLdStType(MVT::v2i32, MVT::i64);
1465 setOperationAction(ISD::LOAD, MVT::v4i16, Custom);
1466 setOperationAction(ISD::STORE, MVT::v4i16, Promote);
1467 AddPromotedToType(ISD::LOAD, MVT::v4i16, MVT::i64);
1468 AddPromotedToType(ISD::STORE, MVT::v4i16, MVT::i64);
1470 // Set the action for vector operations to "expand", then override it with
1471 // either "custom" or "legal" for specific cases.
1472 static unsigned VectExpOps[] = {
1473 // Integer arithmetic:
1474 ISD::ADD, ISD::SUB, ISD::MUL, ISD::SDIV, ISD::UDIV,
1475 ISD::SREM, ISD::UREM, ISD::SDIVREM, ISD::UDIVREM, ISD::ADDC,
1476 ISD::SUBC, ISD::SADDO, ISD::UADDO, ISD::SSUBO, ISD::USUBO,
1477 ISD::SMUL_LOHI, ISD::UMUL_LOHI,
1479 ISD::AND, ISD::OR, ISD::XOR, ISD::ROTL, ISD::ROTR,
1480 ISD::CTPOP, ISD::CTLZ, ISD::CTTZ, ISD::CTLZ_ZERO_UNDEF,
1481 ISD::CTTZ_ZERO_UNDEF,
1482 // Floating point arithmetic/math functions:
1483 ISD::FADD, ISD::FSUB, ISD::FMUL, ISD::FMA, ISD::FDIV,
1484 ISD::FREM, ISD::FNEG, ISD::FABS, ISD::FSQRT, ISD::FSIN,
1485 ISD::FCOS, ISD::FPOWI, ISD::FPOW, ISD::FLOG, ISD::FLOG2,
1486 ISD::FLOG10, ISD::FEXP, ISD::FEXP2, ISD::FCEIL, ISD::FTRUNC,
1487 ISD::FRINT, ISD::FNEARBYINT, ISD::FROUND, ISD::FFLOOR,
1488 ISD::FMINNUM, ISD::FMAXNUM, ISD::FSINCOS,
1490 ISD::SELECT, ISD::ConstantPool,
1492 ISD::BUILD_VECTOR, ISD::SCALAR_TO_VECTOR,
1493 ISD::EXTRACT_VECTOR_ELT, ISD::INSERT_VECTOR_ELT,
1494 ISD::EXTRACT_SUBVECTOR, ISD::INSERT_SUBVECTOR,
1495 ISD::CONCAT_VECTORS, ISD::VECTOR_SHUFFLE
1498 for (MVT VT : MVT::vector_valuetypes()) {
1499 for (unsigned VectExpOp : VectExpOps)
1500 setOperationAction(VectExpOp, VT, Expand);
1502 // Expand all extended loads and truncating stores:
1503 for (MVT TargetVT : MVT::vector_valuetypes()) {
1504 setLoadExtAction(ISD::EXTLOAD, TargetVT, VT, Expand);
1505 setTruncStoreAction(VT, TargetVT, Expand);
1508 setOperationAction(ISD::SRA, VT, Custom);
1509 setOperationAction(ISD::SHL, VT, Custom);
1510 setOperationAction(ISD::SRL, VT, Custom);
1513 // Types natively supported:
1514 for (MVT NativeVT : {MVT::v2i1, MVT::v4i1, MVT::v8i1, MVT::v32i1, MVT::v64i1,
1515 MVT::v4i8, MVT::v8i8, MVT::v2i16, MVT::v4i16, MVT::v1i32,
1516 MVT::v2i32, MVT::v1i64}) {
1517 setOperationAction(ISD::BUILD_VECTOR, NativeVT, Custom);
1518 setOperationAction(ISD::EXTRACT_VECTOR_ELT, NativeVT, Custom);
1519 setOperationAction(ISD::INSERT_VECTOR_ELT, NativeVT, Custom);
1520 setOperationAction(ISD::EXTRACT_SUBVECTOR, NativeVT, Custom);
1521 setOperationAction(ISD::INSERT_SUBVECTOR, NativeVT, Custom);
1522 setOperationAction(ISD::CONCAT_VECTORS, NativeVT, Custom);
1524 setOperationAction(ISD::ADD, NativeVT, Legal);
1525 setOperationAction(ISD::SUB, NativeVT, Legal);
1526 setOperationAction(ISD::MUL, NativeVT, Legal);
1527 setOperationAction(ISD::AND, NativeVT, Legal);
1528 setOperationAction(ISD::OR, NativeVT, Legal);
1529 setOperationAction(ISD::XOR, NativeVT, Legal);
1532 setOperationAction(ISD::SETCC, MVT::v2i16, Custom);
1533 setOperationAction(ISD::VSELECT, MVT::v2i16, Custom);
1534 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
1535 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
1537 // Subtarget-specific operation actions.
1539 if (Subtarget.hasV5TOps()) {
1540 setOperationAction(ISD::FMA, MVT::f64, Expand);
1541 setOperationAction(ISD::FADD, MVT::f64, Expand);
1542 setOperationAction(ISD::FSUB, MVT::f64, Expand);
1543 setOperationAction(ISD::FMUL, MVT::f64, Expand);
1545 setOperationAction(ISD::FP_TO_UINT, MVT::i1, Promote);
1546 setOperationAction(ISD::FP_TO_UINT, MVT::i8, Promote);
1547 setOperationAction(ISD::FP_TO_UINT, MVT::i16, Promote);
1548 setOperationAction(ISD::FP_TO_SINT, MVT::i1, Promote);
1549 setOperationAction(ISD::FP_TO_SINT, MVT::i8, Promote);
1550 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
1551 setOperationAction(ISD::UINT_TO_FP, MVT::i1, Promote);
1552 setOperationAction(ISD::UINT_TO_FP, MVT::i8, Promote);
1553 setOperationAction(ISD::UINT_TO_FP, MVT::i16, Promote);
1554 setOperationAction(ISD::SINT_TO_FP, MVT::i1, Promote);
1555 setOperationAction(ISD::SINT_TO_FP, MVT::i8, Promote);
1556 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
1559 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
1560 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Expand);
1561 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
1562 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
1563 setOperationAction(ISD::FP_TO_SINT, MVT::f64, Expand);
1564 setOperationAction(ISD::FP_TO_SINT, MVT::f32, Expand);
1565 setOperationAction(ISD::FP_EXTEND, MVT::f32, Expand);
1566 setOperationAction(ISD::FP_ROUND, MVT::f64, Expand);
1567 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
1569 setOperationAction(ISD::CTPOP, MVT::i8, Expand);
1570 setOperationAction(ISD::CTPOP, MVT::i16, Expand);
1571 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
1572 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
1574 // Expand these operations for both f32 and f64:
1575 for (unsigned FPExpOpV4 :
1576 {ISD::FADD, ISD::FSUB, ISD::FMUL, ISD::FABS, ISD::FNEG, ISD::FMA}) {
1577 setOperationAction(FPExpOpV4, MVT::f32, Expand);
1578 setOperationAction(FPExpOpV4, MVT::f64, Expand);
1581 for (ISD::CondCode FPExpCCV4 :
1582 {ISD::SETOEQ, ISD::SETOGT, ISD::SETOLT, ISD::SETOGE, ISD::SETOLE,
1583 ISD::SETUO, ISD::SETO}) {
1584 setCondCodeAction(FPExpCCV4, MVT::f32, Expand);
1585 setCondCodeAction(FPExpCCV4, MVT::f64, Expand);
1589 // Handling of indexed loads/stores: default is "expand".
1591 for (MVT LSXTy : {MVT::i8, MVT::i16, MVT::i32, MVT::i64}) {
1592 setIndexedLoadAction(ISD::POST_INC, LSXTy, Legal);
1593 setIndexedStoreAction(ISD::POST_INC, LSXTy, Legal);
1596 computeRegisterProperties(&HRI);
1599 // Library calls for unsupported operations
1601 bool FastMath = EnableFastMath;
1603 setLibcallName(RTLIB::SDIV_I32, "__hexagon_divsi3");
1604 setLibcallName(RTLIB::SDIV_I64, "__hexagon_divdi3");
1605 setLibcallName(RTLIB::UDIV_I32, "__hexagon_udivsi3");
1606 setLibcallName(RTLIB::UDIV_I64, "__hexagon_udivdi3");
1607 setLibcallName(RTLIB::SREM_I32, "__hexagon_modsi3");
1608 setLibcallName(RTLIB::SREM_I64, "__hexagon_moddi3");
1609 setLibcallName(RTLIB::UREM_I32, "__hexagon_umodsi3");
1610 setLibcallName(RTLIB::UREM_I64, "__hexagon_umoddi3");
1612 setLibcallName(RTLIB::SINTTOFP_I128_F64, "__hexagon_floattidf");
1613 setLibcallName(RTLIB::SINTTOFP_I128_F32, "__hexagon_floattisf");
1614 setLibcallName(RTLIB::FPTOUINT_F32_I128, "__hexagon_fixunssfti");
1615 setLibcallName(RTLIB::FPTOUINT_F64_I128, "__hexagon_fixunsdfti");
1616 setLibcallName(RTLIB::FPTOSINT_F32_I128, "__hexagon_fixsfti");
1617 setLibcallName(RTLIB::FPTOSINT_F64_I128, "__hexagon_fixdfti");
1620 // Handle single-precision floating point operations on V4.
1622 setLibcallName(RTLIB::ADD_F32, "__hexagon_fast_addsf3");
1623 setLibcallName(RTLIB::SUB_F32, "__hexagon_fast_subsf3");
1624 setLibcallName(RTLIB::MUL_F32, "__hexagon_fast_mulsf3");
1625 setLibcallName(RTLIB::OGT_F32, "__hexagon_fast_gtsf2");
1626 setLibcallName(RTLIB::OLT_F32, "__hexagon_fast_ltsf2");
1627 // Double-precision compares.
1628 setLibcallName(RTLIB::OGT_F64, "__hexagon_fast_gtdf2");
1629 setLibcallName(RTLIB::OLT_F64, "__hexagon_fast_ltdf2");
1631 setLibcallName(RTLIB::ADD_F32, "__hexagon_addsf3");
1632 setLibcallName(RTLIB::SUB_F32, "__hexagon_subsf3");
1633 setLibcallName(RTLIB::MUL_F32, "__hexagon_mulsf3");
1634 setLibcallName(RTLIB::OGT_F32, "__hexagon_gtsf2");
1635 setLibcallName(RTLIB::OLT_F32, "__hexagon_ltsf2");
1636 // Double-precision compares.
1637 setLibcallName(RTLIB::OGT_F64, "__hexagon_gtdf2");
1638 setLibcallName(RTLIB::OLT_F64, "__hexagon_ltdf2");
1642 // This is the only fast library function for sqrtd.
1644 setLibcallName(RTLIB::SQRT_F64, "__hexagon_fast2_sqrtdf2");
1646 // Prefix is: nothing for "slow-math",
1647 // "fast2_" for V4 fast-math and V5+ fast-math double-precision
1648 // (actually, keep fast-math and fast-math2 separate for now)
1650 setLibcallName(RTLIB::ADD_F64, "__hexagon_fast_adddf3");
1651 setLibcallName(RTLIB::SUB_F64, "__hexagon_fast_subdf3");
1652 setLibcallName(RTLIB::MUL_F64, "__hexagon_fast_muldf3");
1653 setLibcallName(RTLIB::DIV_F64, "__hexagon_fast_divdf3");
1654 // Calling __hexagon_fast2_divsf3 with fast-math on V5 (ok).
1655 setLibcallName(RTLIB::DIV_F32, "__hexagon_fast_divsf3");
1657 setLibcallName(RTLIB::ADD_F64, "__hexagon_adddf3");
1658 setLibcallName(RTLIB::SUB_F64, "__hexagon_subdf3");
1659 setLibcallName(RTLIB::MUL_F64, "__hexagon_muldf3");
1660 setLibcallName(RTLIB::DIV_F64, "__hexagon_divdf3");
1661 setLibcallName(RTLIB::DIV_F32, "__hexagon_divsf3");
1664 if (Subtarget.hasV5TOps()) {
1666 setLibcallName(RTLIB::SQRT_F32, "__hexagon_fast2_sqrtf");
1668 setLibcallName(RTLIB::SQRT_F32, "__hexagon_sqrtf");
1671 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__hexagon_floatsisf");
1672 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__hexagon_floatsidf");
1673 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__hexagon_floatdisf");
1674 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__hexagon_floatdidf");
1675 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__hexagon_floatunsisf");
1676 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__hexagon_floatunsidf");
1677 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__hexagon_floatundisf");
1678 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__hexagon_floatundidf");
1679 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__hexagon_fixunssfsi");
1680 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__hexagon_fixunssfdi");
1681 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__hexagon_fixunsdfsi");
1682 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__hexagon_fixunsdfdi");
1683 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__hexagon_fixsfsi");
1684 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__hexagon_fixsfdi");
1685 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__hexagon_fixdfsi");
1686 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__hexagon_fixdfdi");
1687 setLibcallName(RTLIB::FPEXT_F32_F64, "__hexagon_extendsfdf2");
1688 setLibcallName(RTLIB::FPROUND_F64_F32, "__hexagon_truncdfsf2");
1689 setLibcallName(RTLIB::OEQ_F32, "__hexagon_eqsf2");
1690 setLibcallName(RTLIB::OEQ_F64, "__hexagon_eqdf2");
1691 setLibcallName(RTLIB::OGE_F32, "__hexagon_gesf2");
1692 setLibcallName(RTLIB::OGE_F64, "__hexagon_gedf2");
1693 setLibcallName(RTLIB::OLE_F32, "__hexagon_lesf2");
1694 setLibcallName(RTLIB::OLE_F64, "__hexagon_ledf2");
1695 setLibcallName(RTLIB::UNE_F32, "__hexagon_nesf2");
1696 setLibcallName(RTLIB::UNE_F64, "__hexagon_nedf2");
1697 setLibcallName(RTLIB::UO_F32, "__hexagon_unordsf2");
1698 setLibcallName(RTLIB::UO_F64, "__hexagon_unorddf2");
1699 setLibcallName(RTLIB::O_F32, "__hexagon_unordsf2");
1700 setLibcallName(RTLIB::O_F64, "__hexagon_unorddf2");
1703 // These cause problems when the shift amount is non-constant.
1704 setLibcallName(RTLIB::SHL_I128, nullptr);
1705 setLibcallName(RTLIB::SRL_I128, nullptr);
1706 setLibcallName(RTLIB::SRA_I128, nullptr);
1710 const char* HexagonTargetLowering::getTargetNodeName(unsigned Opcode) const {
1711 switch ((HexagonISD::NodeType)Opcode) {
1712 case HexagonISD::ALLOCA: return "HexagonISD::ALLOCA";
1713 case HexagonISD::ARGEXTEND: return "HexagonISD::ARGEXTEND";
1714 case HexagonISD::AT_GOT: return "HexagonISD::AT_GOT";
1715 case HexagonISD::AT_PCREL: return "HexagonISD::AT_PCREL";
1716 case HexagonISD::BARRIER: return "HexagonISD::BARRIER";
1717 case HexagonISD::BR_JT: return "HexagonISD::BR_JT";
1718 case HexagonISD::CALLR: return "HexagonISD::CALLR";
1719 case HexagonISD::CALLv3nr: return "HexagonISD::CALLv3nr";
1720 case HexagonISD::CALLv3: return "HexagonISD::CALLv3";
1721 case HexagonISD::COMBINE: return "HexagonISD::COMBINE";
1722 case HexagonISD::CONST32_GP: return "HexagonISD::CONST32_GP";
1723 case HexagonISD::CONST32: return "HexagonISD::CONST32";
1724 case HexagonISD::CP: return "HexagonISD::CP";
1725 case HexagonISD::DCFETCH: return "HexagonISD::DCFETCH";
1726 case HexagonISD::EH_RETURN: return "HexagonISD::EH_RETURN";
1727 case HexagonISD::EXTRACTU: return "HexagonISD::EXTRACTU";
1728 case HexagonISD::EXTRACTURP: return "HexagonISD::EXTRACTURP";
1729 case HexagonISD::FCONST32: return "HexagonISD::FCONST32";
1730 case HexagonISD::INSERT: return "HexagonISD::INSERT";
1731 case HexagonISD::INSERTRP: return "HexagonISD::INSERTRP";
1732 case HexagonISD::JT: return "HexagonISD::JT";
1733 case HexagonISD::PACKHL: return "HexagonISD::PACKHL";
1734 case HexagonISD::PIC_ADD: return "HexagonISD::PIC_ADD";
1735 case HexagonISD::POPCOUNT: return "HexagonISD::POPCOUNT";
1736 case HexagonISD::RET_FLAG: return "HexagonISD::RET_FLAG";
1737 case HexagonISD::SHUFFEB: return "HexagonISD::SHUFFEB";
1738 case HexagonISD::SHUFFEH: return "HexagonISD::SHUFFEH";
1739 case HexagonISD::SHUFFOB: return "HexagonISD::SHUFFOB";
1740 case HexagonISD::SHUFFOH: return "HexagonISD::SHUFFOH";
1741 case HexagonISD::TC_RETURN: return "HexagonISD::TC_RETURN";
1742 case HexagonISD::VCMPBEQ: return "HexagonISD::VCMPBEQ";
1743 case HexagonISD::VCMPBGT: return "HexagonISD::VCMPBGT";
1744 case HexagonISD::VCMPBGTU: return "HexagonISD::VCMPBGTU";
1745 case HexagonISD::VCMPHEQ: return "HexagonISD::VCMPHEQ";
1746 case HexagonISD::VCMPHGT: return "HexagonISD::VCMPHGT";
1747 case HexagonISD::VCMPHGTU: return "HexagonISD::VCMPHGTU";
1748 case HexagonISD::VCMPWEQ: return "HexagonISD::VCMPWEQ";
1749 case HexagonISD::VCMPWGT: return "HexagonISD::VCMPWGT";
1750 case HexagonISD::VCMPWGTU: return "HexagonISD::VCMPWGTU";
1751 case HexagonISD::VSHLH: return "HexagonISD::VSHLH";
1752 case HexagonISD::VSHLW: return "HexagonISD::VSHLW";
1753 case HexagonISD::VSPLATB: return "HexagonISD::VSPLTB";
1754 case HexagonISD::VSPLATH: return "HexagonISD::VSPLATH";
1755 case HexagonISD::VSRAH: return "HexagonISD::VSRAH";
1756 case HexagonISD::VSRAW: return "HexagonISD::VSRAW";
1757 case HexagonISD::VSRLH: return "HexagonISD::VSRLH";
1758 case HexagonISD::VSRLW: return "HexagonISD::VSRLW";
1759 case HexagonISD::VSXTBH: return "HexagonISD::VSXTBH";
1760 case HexagonISD::VSXTBW: return "HexagonISD::VSXTBW";
1761 case HexagonISD::OP_END: break;
1766 bool HexagonTargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
1767 EVT MTy1 = EVT::getEVT(Ty1);
1768 EVT MTy2 = EVT::getEVT(Ty2);
1769 if (!MTy1.isSimple() || !MTy2.isSimple())
1771 return (MTy1.getSimpleVT() == MVT::i64) && (MTy2.getSimpleVT() == MVT::i32);
1774 bool HexagonTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
1775 if (!VT1.isSimple() || !VT2.isSimple())
1777 return (VT1.getSimpleVT() == MVT::i64) && (VT2.getSimpleVT() == MVT::i32);
1780 // shouldExpandBuildVectorWithShuffles
1781 // Should we expand the build vector with shuffles?
1783 HexagonTargetLowering::shouldExpandBuildVectorWithShuffles(EVT VT,
1784 unsigned DefinedValues) const {
1786 // Hexagon vector shuffle operates on element sizes of bytes or halfwords
1787 EVT EltVT = VT.getVectorElementType();
1788 int EltBits = EltVT.getSizeInBits();
1789 if ((EltBits != 8) && (EltBits != 16))
1792 return TargetLowering::shouldExpandBuildVectorWithShuffles(VT, DefinedValues);
1795 // LowerVECTOR_SHUFFLE - Lower a vector shuffle (V1, V2, V3). V1 and
1796 // V2 are the two vectors to select data from, V3 is the permutation.
1797 static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
1798 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
1799 SDValue V1 = Op.getOperand(0);
1800 SDValue V2 = Op.getOperand(1);
1802 EVT VT = Op.getValueType();
1804 if (V2.getOpcode() == ISD::UNDEF)
1807 if (SVN->isSplat()) {
1808 int Lane = SVN->getSplatIndex();
1809 if (Lane == -1) Lane = 0;
1811 // Test if V1 is a SCALAR_TO_VECTOR.
1812 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR)
1813 return createSplat(DAG, dl, VT, V1.getOperand(0));
1815 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
1816 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
1818 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
1819 !isa<ConstantSDNode>(V1.getOperand(0))) {
1820 bool IsScalarToVector = true;
1821 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
1822 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
1823 IsScalarToVector = false;
1826 if (IsScalarToVector)
1827 return createSplat(DAG, dl, VT, V1.getOperand(0));
1829 return createSplat(DAG, dl, VT, DAG.getConstant(Lane, dl, MVT::i32));
1832 // FIXME: We need to support more general vector shuffles. See
1833 // below the comment from the ARM backend that deals in the general
1834 // case with the vector shuffles. For now, let expand handle these.
1837 // If the shuffle is not directly supported and it has 4 elements, use
1838 // the PerfectShuffle-generated table to synthesize it from other shuffles.
1841 // If BUILD_VECTOR has same base element repeated several times,
1843 static bool isCommonSplatElement(BuildVectorSDNode *BVN) {
1844 unsigned NElts = BVN->getNumOperands();
1845 SDValue V0 = BVN->getOperand(0);
1847 for (unsigned i = 1, e = NElts; i != e; ++i) {
1848 if (BVN->getOperand(i) != V0)
1854 // LowerVECTOR_SHIFT - Lower a vector shift. Try to convert
1855 // <VT> = SHL/SRA/SRL <VT> by <VT> to Hexagon specific
1856 // <VT> = SHL/SRA/SRL <VT> by <IT/i32>.
1857 static SDValue LowerVECTOR_SHIFT(SDValue Op, SelectionDAG &DAG) {
1858 BuildVectorSDNode *BVN = 0;
1859 SDValue V1 = Op.getOperand(0);
1860 SDValue V2 = Op.getOperand(1);
1863 EVT VT = Op.getValueType();
1865 if ((BVN = dyn_cast<BuildVectorSDNode>(V1.getNode())) &&
1866 isCommonSplatElement(BVN))
1868 else if ((BVN = dyn_cast<BuildVectorSDNode>(V2.getNode())) &&
1869 isCommonSplatElement(BVN))
1874 SDValue CommonSplat = BVN->getOperand(0);
1877 if (VT.getSimpleVT() == MVT::v4i16) {
1878 switch (Op.getOpcode()) {
1880 Result = DAG.getNode(HexagonISD::VSRAH, dl, VT, V3, CommonSplat);
1883 Result = DAG.getNode(HexagonISD::VSHLH, dl, VT, V3, CommonSplat);
1886 Result = DAG.getNode(HexagonISD::VSRLH, dl, VT, V3, CommonSplat);
1891 } else if (VT.getSimpleVT() == MVT::v2i32) {
1892 switch (Op.getOpcode()) {
1894 Result = DAG.getNode(HexagonISD::VSRAW, dl, VT, V3, CommonSplat);
1897 Result = DAG.getNode(HexagonISD::VSHLW, dl, VT, V3, CommonSplat);
1900 Result = DAG.getNode(HexagonISD::VSRLW, dl, VT, V3, CommonSplat);
1909 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
1913 HexagonTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
1914 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
1916 EVT VT = Op.getValueType();
1918 unsigned Size = VT.getSizeInBits();
1920 // A vector larger than 64 bits cannot be represented in Hexagon.
1921 // Expand will split the vector.
1925 APInt APSplatBits, APSplatUndef;
1926 unsigned SplatBitSize;
1928 unsigned NElts = BVN->getNumOperands();
1930 // Try to generate a SPLAT instruction.
1931 if ((VT.getSimpleVT() == MVT::v4i8 || VT.getSimpleVT() == MVT::v4i16) &&
1932 (BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
1933 HasAnyUndefs, 0, true) && SplatBitSize <= 16)) {
1934 unsigned SplatBits = APSplatBits.getZExtValue();
1935 int32_t SextVal = ((int32_t) (SplatBits << (32 - SplatBitSize)) >>
1936 (32 - SplatBitSize));
1937 return createSplat(DAG, dl, VT, DAG.getConstant(SextVal, dl, MVT::i32));
1940 // Try to generate COMBINE to build v2i32 vectors.
1941 if (VT.getSimpleVT() == MVT::v2i32) {
1942 SDValue V0 = BVN->getOperand(0);
1943 SDValue V1 = BVN->getOperand(1);
1945 if (V0.getOpcode() == ISD::UNDEF)
1946 V0 = DAG.getConstant(0, dl, MVT::i32);
1947 if (V1.getOpcode() == ISD::UNDEF)
1948 V1 = DAG.getConstant(0, dl, MVT::i32);
1950 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(V0);
1951 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(V1);
1952 // If the element isn't a constant, it is in a register:
1953 // generate a COMBINE Register Register instruction.
1955 return DAG.getNode(HexagonISD::COMBINE, dl, VT, V1, V0);
1957 // If one of the operands is an 8 bit integer constant, generate
1958 // a COMBINE Immediate Immediate instruction.
1959 if (isInt<8>(C0->getSExtValue()) ||
1960 isInt<8>(C1->getSExtValue()))
1961 return DAG.getNode(HexagonISD::COMBINE, dl, VT, V1, V0);
1964 // Try to generate a S2_packhl to build v2i16 vectors.
1965 if (VT.getSimpleVT() == MVT::v2i16) {
1966 for (unsigned i = 0, e = NElts; i != e; ++i) {
1967 if (BVN->getOperand(i).getOpcode() == ISD::UNDEF)
1969 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(BVN->getOperand(i));
1970 // If the element isn't a constant, it is in a register:
1971 // generate a S2_packhl instruction.
1973 SDValue pack = DAG.getNode(HexagonISD::PACKHL, dl, MVT::v4i16,
1974 BVN->getOperand(1), BVN->getOperand(0));
1976 return DAG.getTargetExtractSubreg(Hexagon::subreg_loreg, dl, MVT::v2i16,
1982 // In the general case, generate a CONST32 or a CONST64 for constant vectors,
1983 // and insert_vector_elt for all the other cases.
1985 unsigned EltSize = Size / NElts;
1987 uint64_t Mask = ~uint64_t(0ULL) >> (64 - EltSize);
1988 bool HasNonConstantElements = false;
1990 for (unsigned i = 0, e = NElts; i != e; ++i) {
1991 // LLVM's BUILD_VECTOR operands are in Little Endian mode, whereas Hexagon's
1992 // combine, const64, etc. are Big Endian.
1993 unsigned OpIdx = NElts - i - 1;
1994 SDValue Operand = BVN->getOperand(OpIdx);
1995 if (Operand.getOpcode() == ISD::UNDEF)
1999 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Operand))
2000 Val = Cst->getSExtValue();
2002 HasNonConstantElements = true;
2005 Res = (Res << EltSize) | Val;
2009 ConstVal = DAG.getConstant(Res, dl, MVT::i64);
2011 ConstVal = DAG.getConstant(Res, dl, MVT::i32);
2013 // When there are non constant operands, add them with INSERT_VECTOR_ELT to
2014 // ConstVal, the constant part of the vector.
2015 if (HasNonConstantElements) {
2016 EVT EltVT = VT.getVectorElementType();
2017 SDValue Width = DAG.getConstant(EltVT.getSizeInBits(), dl, MVT::i64);
2018 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
2019 DAG.getConstant(32, dl, MVT::i64));
2021 for (unsigned i = 0, e = NElts; i != e; ++i) {
2022 // LLVM's BUILD_VECTOR operands are in Little Endian mode, whereas Hexagon
2024 unsigned OpIdx = NElts - i - 1;
2025 SDValue Operand = BVN->getOperand(OpIdx);
2026 if (isa<ConstantSDNode>(Operand))
2027 // This operand is already in ConstVal.
2030 if (VT.getSizeInBits() == 64 &&
2031 Operand.getValueType().getSizeInBits() == 32) {
2032 SDValue C = DAG.getConstant(0, dl, MVT::i32);
2033 Operand = DAG.getNode(HexagonISD::COMBINE, dl, VT, C, Operand);
2036 SDValue Idx = DAG.getConstant(OpIdx, dl, MVT::i64);
2037 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i64, Idx, Width);
2038 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2039 const SDValue Ops[] = {ConstVal, Operand, Combined};
2041 if (VT.getSizeInBits() == 32)
2042 ConstVal = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i32, Ops);
2044 ConstVal = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i64, Ops);
2048 return DAG.getNode(ISD::BITCAST, dl, VT, ConstVal);
2052 HexagonTargetLowering::LowerCONCAT_VECTORS(SDValue Op,
2053 SelectionDAG &DAG) const {
2055 EVT VT = Op.getValueType();
2056 unsigned NElts = Op.getNumOperands();
2057 SDValue Vec = Op.getOperand(0);
2058 EVT VecVT = Vec.getValueType();
2059 SDValue Width = DAG.getConstant(VecVT.getSizeInBits(), dl, MVT::i64);
2060 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
2061 DAG.getConstant(32, dl, MVT::i64));
2062 SDValue ConstVal = DAG.getConstant(0, dl, MVT::i64);
2064 ConstantSDNode *W = dyn_cast<ConstantSDNode>(Width);
2065 ConstantSDNode *S = dyn_cast<ConstantSDNode>(Shifted);
2067 if ((VecVT.getSimpleVT() == MVT::v2i16) && (NElts == 2) && W && S) {
2068 if ((W->getZExtValue() == 32) && ((S->getZExtValue() >> 32) == 32)) {
2069 // We are trying to concat two v2i16 to a single v4i16.
2070 SDValue Vec0 = Op.getOperand(1);
2071 SDValue Combined = DAG.getNode(HexagonISD::COMBINE, dl, VT, Vec0, Vec);
2072 return DAG.getNode(ISD::BITCAST, dl, VT, Combined);
2076 if ((VecVT.getSimpleVT() == MVT::v4i8) && (NElts == 2) && W && S) {
2077 if ((W->getZExtValue() == 32) && ((S->getZExtValue() >> 32) == 32)) {
2078 // We are trying to concat two v4i8 to a single v8i8.
2079 SDValue Vec0 = Op.getOperand(1);
2080 SDValue Combined = DAG.getNode(HexagonISD::COMBINE, dl, VT, Vec0, Vec);
2081 return DAG.getNode(ISD::BITCAST, dl, VT, Combined);
2085 for (unsigned i = 0, e = NElts; i != e; ++i) {
2086 unsigned OpIdx = NElts - i - 1;
2087 SDValue Operand = Op.getOperand(OpIdx);
2089 if (VT.getSizeInBits() == 64 &&
2090 Operand.getValueType().getSizeInBits() == 32) {
2091 SDValue C = DAG.getConstant(0, dl, MVT::i32);
2092 Operand = DAG.getNode(HexagonISD::COMBINE, dl, VT, C, Operand);
2095 SDValue Idx = DAG.getConstant(OpIdx, dl, MVT::i64);
2096 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i64, Idx, Width);
2097 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2098 const SDValue Ops[] = {ConstVal, Operand, Combined};
2100 if (VT.getSizeInBits() == 32)
2101 ConstVal = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i32, Ops);
2103 ConstVal = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i64, Ops);
2106 return DAG.getNode(ISD::BITCAST, dl, VT, ConstVal);
2110 HexagonTargetLowering::LowerEXTRACT_VECTOR(SDValue Op,
2111 SelectionDAG &DAG) const {
2112 EVT VT = Op.getValueType();
2113 int VTN = VT.isVector() ? VT.getVectorNumElements() : 1;
2115 SDValue Idx = Op.getOperand(1);
2116 SDValue Vec = Op.getOperand(0);
2117 EVT VecVT = Vec.getValueType();
2118 EVT EltVT = VecVT.getVectorElementType();
2119 int EltSize = EltVT.getSizeInBits();
2120 SDValue Width = DAG.getConstant(Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT ?
2121 EltSize : VTN * EltSize, dl, MVT::i64);
2123 // Constant element number.
2124 if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Idx)) {
2125 uint64_t X = CI->getZExtValue();
2126 SDValue Offset = DAG.getConstant(X * EltSize, dl, MVT::i32);
2127 const SDValue Ops[] = {Vec, Width, Offset};
2129 ConstantSDNode *CW = dyn_cast<ConstantSDNode>(Width);
2130 assert(CW && "Non constant width in LowerEXTRACT_VECTOR");
2133 MVT SVT = VecVT.getSimpleVT();
2134 uint64_t W = CW->getZExtValue();
2137 // Translate this node into EXTRACT_SUBREG.
2138 unsigned Subreg = (X == 0) ? Hexagon::subreg_loreg : 0;
2141 Subreg = Hexagon::subreg_loreg;
2142 else if (SVT == MVT::v2i32 && X == 1)
2143 Subreg = Hexagon::subreg_hireg;
2144 else if (SVT == MVT::v4i16 && X == 2)
2145 Subreg = Hexagon::subreg_hireg;
2146 else if (SVT == MVT::v8i8 && X == 4)
2147 Subreg = Hexagon::subreg_hireg;
2149 llvm_unreachable("Bad offset");
2150 N = DAG.getTargetExtractSubreg(Subreg, dl, MVT::i32, Vec);
2152 } else if (VecVT.getSizeInBits() == 32) {
2153 N = DAG.getNode(HexagonISD::EXTRACTU, dl, MVT::i32, Ops);
2155 N = DAG.getNode(HexagonISD::EXTRACTU, dl, MVT::i64, Ops);
2156 if (VT.getSizeInBits() == 32)
2157 N = DAG.getTargetExtractSubreg(Hexagon::subreg_loreg, dl, MVT::i32, N);
2160 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2163 // Variable element number.
2164 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i32, Idx,
2165 DAG.getConstant(EltSize, dl, MVT::i32));
2166 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
2167 DAG.getConstant(32, dl, MVT::i64));
2168 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2170 const SDValue Ops[] = {Vec, Combined};
2173 if (VecVT.getSizeInBits() == 32) {
2174 N = DAG.getNode(HexagonISD::EXTRACTURP, dl, MVT::i32, Ops);
2176 N = DAG.getNode(HexagonISD::EXTRACTURP, dl, MVT::i64, Ops);
2177 if (VT.getSizeInBits() == 32)
2178 N = DAG.getTargetExtractSubreg(Hexagon::subreg_loreg, dl, MVT::i32, N);
2180 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2184 HexagonTargetLowering::LowerINSERT_VECTOR(SDValue Op,
2185 SelectionDAG &DAG) const {
2186 EVT VT = Op.getValueType();
2187 int VTN = VT.isVector() ? VT.getVectorNumElements() : 1;
2189 SDValue Vec = Op.getOperand(0);
2190 SDValue Val = Op.getOperand(1);
2191 SDValue Idx = Op.getOperand(2);
2192 EVT VecVT = Vec.getValueType();
2193 EVT EltVT = VecVT.getVectorElementType();
2194 int EltSize = EltVT.getSizeInBits();
2195 SDValue Width = DAG.getConstant(Op.getOpcode() == ISD::INSERT_VECTOR_ELT ?
2196 EltSize : VTN * EltSize, dl, MVT::i64);
2198 if (ConstantSDNode *C = cast<ConstantSDNode>(Idx)) {
2199 SDValue Offset = DAG.getConstant(C->getSExtValue() * EltSize, dl, MVT::i32);
2200 const SDValue Ops[] = {Vec, Val, Width, Offset};
2203 if (VT.getSizeInBits() == 32)
2204 N = DAG.getNode(HexagonISD::INSERT, dl, MVT::i32, Ops);
2206 N = DAG.getNode(HexagonISD::INSERT, dl, MVT::i64, Ops);
2208 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2211 // Variable element number.
2212 SDValue Offset = DAG.getNode(ISD::MUL, dl, MVT::i32, Idx,
2213 DAG.getConstant(EltSize, dl, MVT::i32));
2214 SDValue Shifted = DAG.getNode(ISD::SHL, dl, MVT::i64, Width,
2215 DAG.getConstant(32, dl, MVT::i64));
2216 SDValue Combined = DAG.getNode(ISD::OR, dl, MVT::i64, Shifted, Offset);
2218 if (VT.getSizeInBits() == 64 &&
2219 Val.getValueType().getSizeInBits() == 32) {
2220 SDValue C = DAG.getConstant(0, dl, MVT::i32);
2221 Val = DAG.getNode(HexagonISD::COMBINE, dl, VT, C, Val);
2224 const SDValue Ops[] = {Vec, Val, Combined};
2227 if (VT.getSizeInBits() == 32)
2228 N = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i32, Ops);
2230 N = DAG.getNode(HexagonISD::INSERTRP, dl, MVT::i64, Ops);
2232 return DAG.getNode(ISD::BITCAST, dl, VT, N);
2236 HexagonTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
2237 // Assuming the caller does not have either a signext or zeroext modifier, and
2238 // only one value is accepted, any reasonable truncation is allowed.
2239 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
2242 // FIXME: in principle up to 64-bit could be made safe, but it would be very
2243 // fragile at the moment: any support for multiple value returns would be
2244 // liable to disallow tail calls involving i64 -> iN truncation in many cases.
2245 return Ty1->getPrimitiveSizeInBits() <= 32;
2249 HexagonTargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
2250 SDValue Chain = Op.getOperand(0);
2251 SDValue Offset = Op.getOperand(1);
2252 SDValue Handler = Op.getOperand(2);
2255 // Mark function as containing a call to EH_RETURN.
2256 HexagonMachineFunctionInfo *FuncInfo =
2257 DAG.getMachineFunction().getInfo<HexagonMachineFunctionInfo>();
2258 FuncInfo->setHasEHReturn();
2260 unsigned OffsetReg = Hexagon::R28;
2262 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(),
2263 DAG.getRegister(Hexagon::R30, getPointerTy()),
2264 DAG.getIntPtrConstant(4, dl));
2265 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
2267 Chain = DAG.getCopyToReg(Chain, dl, OffsetReg, Offset);
2269 // Not needed we already use it as explict input to EH_RETURN.
2270 // MF.getRegInfo().addLiveOut(OffsetReg);
2272 return DAG.getNode(HexagonISD::EH_RETURN, dl, MVT::Other, Chain);
2276 HexagonTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
2277 unsigned Opc = Op.getOpcode();
2281 Op.getNode()->dumpr(&DAG);
2282 if (Opc > HexagonISD::OP_BEGIN && Opc < HexagonISD::OP_END)
2283 errs() << "Check for a non-legal type in this operation\n";
2285 llvm_unreachable("Should not custom lower this!");
2286 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
2287 case ISD::INSERT_SUBVECTOR: return LowerINSERT_VECTOR(Op, DAG);
2288 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR(Op, DAG);
2289 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_VECTOR(Op, DAG);
2290 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR(Op, DAG);
2291 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
2292 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
2295 case ISD::SRL: return LowerVECTOR_SHIFT(Op, DAG);
2296 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
2297 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
2298 // Frame & Return address. Currently unimplemented.
2299 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
2300 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
2301 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
2302 case ISD::GlobalAddress: return LowerGLOBALADDRESS(Op, DAG);
2303 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
2304 case ISD::VASTART: return LowerVASTART(Op, DAG);
2305 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
2306 // Custom lower some vector loads.
2307 case ISD::LOAD: return LowerLOAD(Op, DAG);
2308 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
2309 case ISD::SETCC: return LowerSETCC(Op, DAG);
2310 case ISD::VSELECT: return LowerVSELECT(Op, DAG);
2311 case ISD::CTPOP: return LowerCTPOP(Op, DAG);
2312 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
2313 case ISD::INLINEASM: return LowerINLINEASM(Op, DAG);
2318 HexagonTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
2319 MachineBasicBlock *BB)
2321 switch (MI->getOpcode()) {
2322 case Hexagon::ALLOCA: {
2323 MachineFunction *MF = BB->getParent();
2324 auto *FuncInfo = MF->getInfo<HexagonMachineFunctionInfo>();
2325 FuncInfo->addAllocaAdjustInst(MI);
2328 default: llvm_unreachable("Unexpected instr type to insert");
2332 //===----------------------------------------------------------------------===//
2333 // Inline Assembly Support
2334 //===----------------------------------------------------------------------===//
2336 std::pair<unsigned, const TargetRegisterClass *>
2337 HexagonTargetLowering::getRegForInlineAsmConstraint(
2338 const TargetRegisterInfo *TRI, const std::string &Constraint,
2340 if (Constraint.size() == 1) {
2341 switch (Constraint[0]) {
2343 switch (VT.SimpleTy) {
2345 llvm_unreachable("getRegForInlineAsmConstraint Unhandled data type");
2350 return std::make_pair(0U, &Hexagon::IntRegsRegClass);
2353 return std::make_pair(0U, &Hexagon::DoubleRegsRegClass);
2356 llvm_unreachable("Unknown asm register class");
2360 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
2363 /// isFPImmLegal - Returns true if the target can instruction select the
2364 /// specified FP immediate natively. If false, the legalizer will
2365 /// materialize the FP immediate as a load from a constant pool.
2366 bool HexagonTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
2367 return Subtarget.hasV5TOps();
2370 /// isLegalAddressingMode - Return true if the addressing mode represented by
2371 /// AM is legal for this target, for a load/store of the specified type.
2372 bool HexagonTargetLowering::isLegalAddressingMode(const AddrMode &AM,
2374 // Allows a signed-extended 11-bit immediate field.
2375 if (AM.BaseOffs <= -(1LL << 13) || AM.BaseOffs >= (1LL << 13)-1)
2378 // No global is ever allowed as a base.
2382 int Scale = AM.Scale;
2383 if (Scale < 0) Scale = -Scale;
2385 case 0: // No scale reg, "r+i", "r", or just "i".
2387 default: // No scaled addressing mode.
2393 /// isLegalICmpImmediate - Return true if the specified immediate is legal
2394 /// icmp immediate, that is the target has icmp instructions which can compare
2395 /// a register against the immediate without having to materialize the
2396 /// immediate into a register.
2397 bool HexagonTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
2398 return Imm >= -512 && Imm <= 511;
2401 /// IsEligibleForTailCallOptimization - Check whether the call is eligible
2402 /// for tail call optimization. Targets which want to do tail call
2403 /// optimization should implement this function.
2404 bool HexagonTargetLowering::IsEligibleForTailCallOptimization(
2406 CallingConv::ID CalleeCC,
2408 bool isCalleeStructRet,
2409 bool isCallerStructRet,
2410 const SmallVectorImpl<ISD::OutputArg> &Outs,
2411 const SmallVectorImpl<SDValue> &OutVals,
2412 const SmallVectorImpl<ISD::InputArg> &Ins,
2413 SelectionDAG& DAG) const {
2414 const Function *CallerF = DAG.getMachineFunction().getFunction();
2415 CallingConv::ID CallerCC = CallerF->getCallingConv();
2416 bool CCMatch = CallerCC == CalleeCC;
2418 // ***************************************************************************
2419 // Look for obvious safe cases to perform tail call optimization that do not
2420 // require ABI changes.
2421 // ***************************************************************************
2423 // If this is a tail call via a function pointer, then don't do it!
2424 if (!(dyn_cast<GlobalAddressSDNode>(Callee))
2425 && !(dyn_cast<ExternalSymbolSDNode>(Callee))) {
2429 // Do not optimize if the calling conventions do not match.
2433 // Do not tail call optimize vararg calls.
2437 // Also avoid tail call optimization if either caller or callee uses struct
2438 // return semantics.
2439 if (isCalleeStructRet || isCallerStructRet)
2442 // In addition to the cases above, we also disable Tail Call Optimization if
2443 // the calling convention code that at least one outgoing argument needs to
2444 // go on the stack. We cannot check that here because at this point that
2445 // information is not available.
2449 // Return true when the given node fits in a positive half word.
2450 bool llvm::isPositiveHalfWord(SDNode *N) {
2451 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N);
2452 if (CN && CN->getSExtValue() > 0 && isInt<16>(CN->getSExtValue()))
2455 switch (N->getOpcode()) {
2458 case ISD::SIGN_EXTEND_INREG: