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