1//===-- BPFSelectionDAGInfo.cpp - BPF 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 BPFSelectionDAGInfo class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "BPFSelectionDAGInfo.h"
14#include "llvm/CodeGen/SelectionDAG.h"
15
16#define GET_SDNODE_DESC
17#include "BPFGenSDNodeInfo.inc"
18
19using namespace llvm;
20
21#define DEBUG_TYPE "bpf-selectiondag-info"
22
23static cl::opt<unsigned> BPFMaxStoresPerMemFunc(
24 "bpf-max-stores-per-memfunc", cl::Hidden, cl::init(Val: 192),
25 cl::desc("Set the maximum number of stores for inlined BPF memory "
26 "intrinsics"));
27
28BPFSelectionDAGInfo::BPFSelectionDAGInfo()
29 : SelectionDAGGenTargetInfo(BPFGenSDNodeInfo) {}
30
31unsigned BPFSelectionDAGInfo::getCommonMaxStoresPerMemFunc() const {
32 return BPFMaxStoresPerMemFunc;
33}
34
35SDValue BPFSelectionDAGInfo::EmitTargetCodeForMemcpy(
36 SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src,
37 SDValue Size, Align DstAlign, Align SrcAlign, bool isVolatile,
38 bool AlwaysInline, MachinePointerInfo DstPtrInfo,
39 MachinePointerInfo SrcPtrInfo) const {
40 Align Alignment = std::min(a: DstAlign, b: SrcAlign);
41
42 // Requires the copy size to be a constant.
43 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Val&: Size);
44 if (!ConstantSize)
45 return SDValue();
46
47 // `BPFInstrInfo::expandMEMCPY` supports alignment up to 8 bytes.
48 if (Alignment.value() > 8)
49 return SDValue();
50
51 unsigned CopyLen = ConstantSize->getZExtValue();
52 unsigned StoresNumEstimate = alignTo(Size: CopyLen, A: Alignment) >> Log2(A: Alignment);
53 // Impose the same copy length limit as MaxStoresPerMemcpy.
54 if (StoresNumEstimate > getCommonMaxStoresPerMemFunc())
55 return SDValue();
56
57 return DAG.getNode(Opcode: BPFISD::MEMCPY, DL: dl, VT: MVT::Other, N1: Chain, N2: Dst, N3: Src,
58 N4: DAG.getConstant(Val: CopyLen, DL: dl, VT: MVT::i64),
59 N5: DAG.getConstant(Val: Alignment.value(), DL: dl, VT: MVT::i64));
60}
61