1//===-- BPFISelLowering.h - BPF DAG Lowering Interface ----------*- C++ -*-===//
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 defines the interfaces that BPF uses to lower LLVM code into a
10// selection DAG.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_LIB_TARGET_BPF_BPFISELLOWERING_H
15#define LLVM_LIB_TARGET_BPF_BPFISELLOWERING_H
16
17#include "BPF.h"
18#include "llvm/CodeGen/SelectionDAG.h"
19#include "llvm/CodeGen/TargetLowering.h"
20
21namespace llvm {
22class BPFSubtarget;
23
24class BPFTargetLowering : public TargetLowering {
25public:
26 explicit BPFTargetLowering(const TargetMachine &TM, const BPFSubtarget &STI);
27
28 // Provide custom lowering hooks for some operations.
29 SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const override;
30
31 // This method decides whether folding a constant offset
32 // with the given GlobalAddress is legal.
33 bool isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const override;
34
35 bool allowsMisalignedMemoryAccesses(EVT VT, unsigned, Align,
36 MachineMemOperand::Flags,
37 unsigned *) const override;
38
39 BPFTargetLowering::ConstraintType
40 getConstraintType(StringRef Constraint) const override;
41
42 std::pair<unsigned, const TargetRegisterClass *>
43 getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
44 StringRef Constraint, MVT VT) const override;
45
46 MachineBasicBlock *
47 EmitInstrWithCustomInserter(MachineInstr &MI,
48 MachineBasicBlock *BB) const override;
49
50 AtomicExpansionKind
51 shouldExpandAtomicRMWInIR(const AtomicRMWInst *AI) const override;
52 AtomicExpansionKind shouldExpandAtomicStoreInIR(StoreInst *SI) const override;
53
54 bool getHasAlu32() const { return HasAlu32; }
55 bool getHasJmp32() const { return HasJmp32; }
56 bool getHasJmpExt() const { return HasJmpExt; }
57
58 EVT getSetCCResultType(const DataLayout &DL, LLVMContext &Context,
59 EVT VT) const override;
60
61 // Exception handling support.
62 Register getExceptionPointerRegister(ExceptionHandling EH,
63 const Constant *) const override {
64 return BPF::R0;
65 }
66 Register getExceptionSelectorRegister(ExceptionHandling EH,
67 const Constant *) const override {
68 return BPF::R0;
69 }
70
71 MVT getScalarShiftAmountTy(const DataLayout &, EVT) const override;
72
73 unsigned getJumpTableEncoding() const override;
74
75 /// The entries are addresses of blocks, also in position independent code.
76 bool isJumpTableRelative() const override { return false; }
77
78private:
79 // Control Instruction Selection Features
80 bool HasAlu32;
81 bool HasJmp32;
82 bool HasJmpExt;
83 bool HasMovsx;
84 bool HasLoadAcqStoreRel;
85
86 // Allows Misalignment
87 bool AllowsMisalignedMemAccess;
88
89 SDValue LowerSDIVSREM(SDValue Op, SelectionDAG &DAG) const;
90 SDValue LowerShiftParts(SDValue Op, SelectionDAG &DAG) const;
91 SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const;
92 SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG) const;
93 SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const;
94 SDValue LowerATOMIC_LOAD_STORE(SDValue Op, SelectionDAG &DAG) const;
95 SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG) const;
96 SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) const;
97 SDValue LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const;
98 SDValue LowerTRAP(SDValue Op, SelectionDAG &DAG) const;
99 SDValue LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const;
100 SDValue LowerJumpTable(SDValue Op, SelectionDAG &DAG) const;
101 SDValue LowerBR_JT(SDValue Op, SelectionDAG &DAG) const;
102
103 template <class NodeTy>
104 SDValue getAddr(NodeTy *N, SelectionDAG &DAG, unsigned Flags = 0) const;
105
106 // Lower the result values of a call, copying them out of physregs into vregs
107 SDValue LowerCallResult(SDValue Chain, SDValue InGlue,
108 CallingConv::ID CallConv, bool IsVarArg,
109 const SmallVectorImpl<ISD::InputArg> &Ins,
110 const SDLoc &DL, SelectionDAG &DAG,
111 SmallVectorImpl<SDValue> &InVals) const;
112
113 // Lower a call into CALLSEQ_START - BPFISD:CALL - CALLSEQ_END chain
114 SDValue LowerCall(TargetLowering::CallLoweringInfo &CLI,
115 SmallVectorImpl<SDValue> &InVals) const override;
116
117 // Lower incoming arguments, copy physregs into vregs
118 SDValue LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv,
119 bool IsVarArg,
120 const SmallVectorImpl<ISD::InputArg> &Ins,
121 const SDLoc &DL, SelectionDAG &DAG,
122 SmallVectorImpl<SDValue> &InVals) const override;
123
124 SDValue LowerReturn(SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
125 const SmallVectorImpl<ISD::OutputArg> &Outs,
126 const SmallVectorImpl<SDValue> &OutVals, const SDLoc &DL,
127 SelectionDAG &DAG) const override;
128
129 void ReplaceNodeResults(SDNode *N, SmallVectorImpl<SDValue> &Results,
130 SelectionDAG &DAG) const override;
131
132 bool isIntDivCheap(EVT VT, AttributeList Attr) const override {
133 return false;
134 }
135
136 bool shouldConvertConstantLoadToIntImm(const APInt &Imm,
137 Type *Ty) const override {
138 return true;
139 }
140
141 // Prevent reducing load width during SelectionDag phase.
142 // Otherwise, we may transform the following
143 // ctx = ctx + reloc_offset
144 // ... (*(u32 *)ctx) & 0x8000...
145 // to
146 // ctx = ctx + reloc_offset
147 // ... (*(u8 *)(ctx + 1)) & 0x80 ...
148 // which will be rejected by the verifier.
149 bool
150 shouldReduceLoadWidth(SDNode *Load, ISD::LoadExtType ExtTy, EVT NewVT,
151 std::optional<unsigned> ByteOffset) const override {
152 return false;
153 }
154
155 bool isLegalAddressingMode(const DataLayout &DL, const AddrMode &AM,
156 Type *Ty, unsigned AS,
157 Instruction *I = nullptr) const override;
158
159 // isTruncateFree - Return true if it's free to truncate a value of
160 // type Ty1 to type Ty2. e.g. On BPF at alu32 mode, it's free to truncate
161 // a i64 value in register R1 to i32 by referencing its sub-register W1.
162 bool isTruncateFree(Type *Ty1, Type *Ty2) const override;
163 bool isTruncateFree(EVT VT1, EVT VT2) const override;
164
165 // For 32bit ALU result zext to 64bit is free.
166 bool isZExtFree(Type *Ty1, Type *Ty2) const override;
167 bool isZExtFree(EVT VT1, EVT VT2) const override;
168 bool isZExtFree(SDValue Val, EVT VT2) const override;
169
170 unsigned EmitSubregExt(MachineInstr &MI, MachineBasicBlock *BB, unsigned Reg,
171 bool isSigned) const;
172
173 MachineBasicBlock * EmitInstrWithCustomInserterMemcpy(MachineInstr &MI,
174 MachineBasicBlock *BB)
175 const;
176 MachineBasicBlock *
177 EmitInstrWithCustomInserterLDimm64(MachineInstr &MI,
178 MachineBasicBlock *BB) const;
179
180 bool CanLowerReturn(CallingConv::ID CallConv, MachineFunction &MF,
181 bool IsVarArg,
182 const SmallVectorImpl<ISD::OutputArg> &Outs,
183 LLVMContext &Context, const Type *RetTy) const override;
184};
185}
186
187#endif
188