1//===-- XCoreSelectionDAGInfo.cpp - XCore 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 XCoreSelectionDAGInfo class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "XCoreSelectionDAGInfo.h"
14#include "XCoreTargetMachine.h"
15
16#define GET_SDNODE_DESC
17#include "XCoreGenSDNodeInfo.inc"
18
19using namespace llvm;
20
21#define DEBUG_TYPE "xcore-selectiondag-info"
22
23XCoreSelectionDAGInfo::XCoreSelectionDAGInfo()
24 : SelectionDAGGenTargetInfo(XCoreGenSDNodeInfo) {}
25
26SDValue XCoreSelectionDAGInfo::EmitTargetCodeForMemcpy(
27 SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src,
28 SDValue Size, Align DstAlign, Align SrcAlign, bool isVolatile,
29 bool AlwaysInline, bool &IsTailCall, MachinePointerInfo DstPtrInfo,
30 MachinePointerInfo SrcPtrInfo) const {
31 bool CanTailCall = IsTailCall;
32 IsTailCall = false;
33
34 unsigned SizeBitWidth = Size.getValueSizeInBits();
35 // Call __memcpy_4 if the src, dst and size are all 4 byte aligned.
36 if (!AlwaysInline && SrcAlign >= Align(4) && DstAlign >= Align(4) &&
37 DAG.MaskedValueIsZero(Op: Size, Mask: APInt(SizeBitWidth, 3))) {
38 const TargetLowering &TLI = *DAG.getSubtarget().getTargetLowering();
39 TargetLowering::ArgListTy Args;
40 Type *ArgTy = DAG.getDataLayout().getIntPtrType(C&: *DAG.getContext());
41 Args.emplace_back(args&: Dst, args&: ArgTy);
42 Args.emplace_back(args&: Src, args&: ArgTy);
43 Args.emplace_back(args&: Size, args&: ArgTy);
44
45 RTLIB::LibcallImpl MemcpyAlign4Impl =
46 DAG.getLibcalls().getLibcallImpl(Call: RTLIB::MEMCPY_ALIGN_4);
47 if (MemcpyAlign4Impl == RTLIB::Unsupported)
48 return SDValue();
49
50 CallingConv::ID CC =
51 DAG.getLibcalls().getLibcallImplCallingConv(Call: MemcpyAlign4Impl);
52
53 TargetLowering::CallLoweringInfo CLI(DAG);
54 CLI.setDebugLoc(dl)
55 .setChain(Chain)
56 .setLibCallee(
57 CC, ResultType: Type::getVoidTy(C&: *DAG.getContext()),
58 Target: DAG.getExternalSymbol(LCImpl: MemcpyAlign4Impl,
59 VT: TLI.getPointerTy(DL: DAG.getDataLayout())),
60 ArgsList: std::move(Args))
61 .setDiscardResult()
62 .setTailCall(CanTailCall);
63
64 std::pair<SDValue,SDValue> CallResult = TLI.LowerCallTo(CLI);
65 IsTailCall = CLI.IsTailCall;
66 return CallResult.second;
67 }
68
69 // Otherwise have the target-independent code call memcpy.
70 return SDValue();
71}
72