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 DstAlign, Align SrcAlign, bool isVolatile,
68 bool AlwaysInline, MachinePointerInfo DstPtrInfo,
69 MachinePointerInfo SrcPtrInfo) const {
70 Align Alignment = std::min(a: DstAlign, b: SrcAlign);
71 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Val&: Size);
72 if (AlwaysInline || Alignment < Align(4) || !ConstantSize)
73 return SDValue();
74
75 uint64_t SizeVal = ConstantSize->getZExtValue();
76 if (SizeVal < 32 || (SizeVal % 8) != 0)
77 return SDValue();
78
79 // Special case aligned memcpys with size >= 32 bytes and a multiple of 8.
80 //
81 const TargetLowering &TLI = *DAG.getSubtarget().getTargetLowering();
82 TargetLowering::ArgListTy Args;
83 Type *ArgTy = DAG.getDataLayout().getIntPtrType(C&: *DAG.getContext());
84 Args.emplace_back(args&: Dst, args&: ArgTy);
85 Args.emplace_back(args&: Src, args&: ArgTy);
86 Args.emplace_back(args&: Size, args&: ArgTy);
87
88 RTLIB::LibcallImpl SpecialMemcpyImpl = DAG.getLibcalls().getLibcallImpl(
89 Call: RTLIB::HEXAGON_MEMCPY_LIKELY_ALIGNED_MIN32BYTES_MULT8BYTES);
90 if (SpecialMemcpyImpl == RTLIB::Unsupported)
91 return SDValue();
92
93 const MachineFunction &MF = DAG.getMachineFunction();
94 bool LongCalls = MF.getSubtarget<HexagonSubtarget>().useLongCalls();
95 unsigned Flags = LongCalls ? HexagonII::HMOTF_ConstExtended : 0;
96
97 CallingConv::ID CC =
98 DAG.getLibcalls().getLibcallImplCallingConv(Call: SpecialMemcpyImpl);
99
100 TargetLowering::CallLoweringInfo CLI(DAG);
101 CLI.setDebugLoc(dl)
102 .setChain(Chain)
103 .setLibCallee(
104 CC, ResultType: Type::getVoidTy(C&: *DAG.getContext()),
105 Target: DAG.getTargetExternalSymbol(
106 LCImpl: SpecialMemcpyImpl, VT: TLI.getPointerTy(DL: DAG.getDataLayout()), TargetFlags: Flags),
107 ArgsList: std::move(Args))
108 .setDiscardResult();
109
110 std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI);
111 return CallResult.second;
112}
113