1//===-- HexagonSelectionDAGInfo.cpp - Hexagon SelectionDAG Info -----------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the HexagonSelectionDAGInfo class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "HexagonSelectionDAGInfo.h"
14#include "HexagonTargetMachine.h"
15#include "llvm/CodeGen/SelectionDAG.h"
16
17#define GET_SDNODE_DESC
18#include "HexagonGenSDNodeInfo.inc"
19
20using namespace llvm;
21
22#define DEBUG_TYPE "hexagon-selectiondag-info"
23
24HexagonSelectionDAGInfo::HexagonSelectionDAGInfo()
25 : SelectionDAGGenTargetInfo(HexagonGenSDNodeInfo) {}
26
27const char *HexagonSelectionDAGInfo::getTargetNodeName(unsigned Opcode) const {
28#define CASE(NAME) \
29 case HexagonISD::NAME: \
30 return "HexagonISD::" #NAME
31
32 // These nodes don't have corresponding entries in *.td files yet.
33 switch (static_cast<HexagonISD::NodeType>(Opcode)) {
34 CASE(CALLR);
35 CASE(VROR);
36 CASE(D2P);
37 CASE(P2D);
38 CASE(V2Q);
39 CASE(Q2V);
40 CASE(TL_EXTEND);
41 CASE(TL_TRUNCATE);
42 CASE(TYPECAST);
43 CASE(ISEL);
44 }
45#undef CASE
46
47 return SelectionDAGGenTargetInfo::getTargetNodeName(Opcode);
48}
49
50void HexagonSelectionDAGInfo::verifyTargetNode(const SelectionDAG &DAG,
51 const SDNode *N) const {
52 switch (N->getOpcode()) {
53 default:
54 break;
55 case HexagonISD::VALIGNADDR:
56 // invalid number of operands; expected 1, got 2
57 case HexagonISD::VINSERTW0:
58 // operand #1 must have type i32, but has type v4i8/v2i16
59 return;
60 }
61
62 SelectionDAGGenTargetInfo::verifyTargetNode(DAG, N);
63}
64
65SDValue HexagonSelectionDAGInfo::EmitTargetCodeForMemcpy(
66 SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src,
67 SDValue Size, Align Alignment, bool isVolatile, bool AlwaysInline,
68 MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo) const {
69 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Val&: Size);
70 if (AlwaysInline || Alignment < Align(4) || !ConstantSize)
71 return SDValue();
72
73 uint64_t SizeVal = ConstantSize->getZExtValue();
74 if (SizeVal < 32 || (SizeVal % 8) != 0)
75 return SDValue();
76
77 // Special case aligned memcpys with size >= 32 bytes and a multiple of 8.
78 //
79 const TargetLowering &TLI = *DAG.getSubtarget().getTargetLowering();
80 TargetLowering::ArgListTy Args;
81 Type *ArgTy = DAG.getDataLayout().getIntPtrType(C&: *DAG.getContext());
82 Args.emplace_back(args&: Dst, args&: ArgTy);
83 Args.emplace_back(args&: Src, args&: ArgTy);
84 Args.emplace_back(args&: Size, args&: ArgTy);
85
86 RTLIB::LibcallImpl SpecialMemcpyImpl = DAG.getLibcalls().getLibcallImpl(
87 Call: RTLIB::HEXAGON_MEMCPY_LIKELY_ALIGNED_MIN32BYTES_MULT8BYTES);
88 if (SpecialMemcpyImpl == RTLIB::Unsupported)
89 return SDValue();
90
91 const MachineFunction &MF = DAG.getMachineFunction();
92 bool LongCalls = MF.getSubtarget<HexagonSubtarget>().useLongCalls();
93 unsigned Flags = LongCalls ? HexagonII::HMOTF_ConstExtended : 0;
94
95 CallingConv::ID CC =
96 DAG.getLibcalls().getLibcallImplCallingConv(Call: SpecialMemcpyImpl);
97
98 TargetLowering::CallLoweringInfo CLI(DAG);
99 CLI.setDebugLoc(dl)
100 .setChain(Chain)
101 .setLibCallee(
102 CC, ResultType: Type::getVoidTy(C&: *DAG.getContext()),
103 Target: DAG.getTargetExternalSymbol(
104 LCImpl: SpecialMemcpyImpl, VT: TLI.getPointerTy(DL: DAG.getDataLayout()), TargetFlags: Flags),
105 ArgsList: std::move(Args))
106 .setDiscardResult();
107
108 std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI);
109 return CallResult.second;
110}
111