[mlir][LLVM] `LLVMTypeConverter`: Tighten materialization checks (#116532)
[llvm-project.git] / llvm / lib / Target / BPF / BPFISelLowering.h
blobd59098f9f569ba14663deac07294b549a72781fe
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.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_LIB_TARGET_BPF_BPFISELLOWERING_H
15 #define LLVM_LIB_TARGET_BPF_BPFISELLOWERING_H
17 #include "BPF.h"
18 #include "llvm/CodeGen/SelectionDAG.h"
19 #include "llvm/CodeGen/TargetLowering.h"
21 namespace llvm {
22 class BPFSubtarget;
23 namespace BPFISD {
24 enum NodeType : unsigned {
25 FIRST_NUMBER = ISD::BUILTIN_OP_END,
26 RET_GLUE,
27 CALL,
28 SELECT_CC,
29 BR_CC,
30 Wrapper,
31 MEMCPY
35 class BPFTargetLowering : public TargetLowering {
36 public:
37 explicit BPFTargetLowering(const TargetMachine &TM, const BPFSubtarget &STI);
39 // Provide custom lowering hooks for some operations.
40 SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const override;
42 // This method returns the name of a target specific DAG node.
43 const char *getTargetNodeName(unsigned Opcode) const override;
45 // This method decides whether folding a constant offset
46 // with the given GlobalAddress is legal.
47 bool isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const override;
49 BPFTargetLowering::ConstraintType
50 getConstraintType(StringRef Constraint) const override;
52 std::pair<unsigned, const TargetRegisterClass *>
53 getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
54 StringRef Constraint, MVT VT) const override;
56 MachineBasicBlock *
57 EmitInstrWithCustomInserter(MachineInstr &MI,
58 MachineBasicBlock *BB) const override;
60 bool getHasAlu32() const { return HasAlu32; }
61 bool getHasJmp32() const { return HasJmp32; }
62 bool getHasJmpExt() const { return HasJmpExt; }
64 EVT getSetCCResultType(const DataLayout &DL, LLVMContext &Context,
65 EVT VT) const override;
67 MVT getScalarShiftAmountTy(const DataLayout &, EVT) const override;
69 private:
70 // Control Instruction Selection Features
71 bool HasAlu32;
72 bool HasJmp32;
73 bool HasJmpExt;
74 bool HasMovsx;
76 SDValue LowerSDIVSREM(SDValue Op, SelectionDAG &DAG) const;
77 SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const;
78 SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG) const;
79 SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const;
81 SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) const;
82 SDValue LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const;
84 template <class NodeTy>
85 SDValue getAddr(NodeTy *N, SelectionDAG &DAG, unsigned Flags = 0) const;
87 // Lower the result values of a call, copying them out of physregs into vregs
88 SDValue LowerCallResult(SDValue Chain, SDValue InGlue,
89 CallingConv::ID CallConv, bool IsVarArg,
90 const SmallVectorImpl<ISD::InputArg> &Ins,
91 const SDLoc &DL, SelectionDAG &DAG,
92 SmallVectorImpl<SDValue> &InVals) const;
94 // Maximum number of arguments to a call
95 static const size_t MaxArgs;
97 // Lower a call into CALLSEQ_START - BPFISD:CALL - CALLSEQ_END chain
98 SDValue LowerCall(TargetLowering::CallLoweringInfo &CLI,
99 SmallVectorImpl<SDValue> &InVals) const override;
101 // Lower incoming arguments, copy physregs into vregs
102 SDValue LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv,
103 bool IsVarArg,
104 const SmallVectorImpl<ISD::InputArg> &Ins,
105 const SDLoc &DL, SelectionDAG &DAG,
106 SmallVectorImpl<SDValue> &InVals) const override;
108 SDValue LowerReturn(SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
109 const SmallVectorImpl<ISD::OutputArg> &Outs,
110 const SmallVectorImpl<SDValue> &OutVals, const SDLoc &DL,
111 SelectionDAG &DAG) const override;
113 void ReplaceNodeResults(SDNode *N, SmallVectorImpl<SDValue> &Results,
114 SelectionDAG &DAG) const override;
116 EVT getOptimalMemOpType(const MemOp &Op,
117 const AttributeList &FuncAttributes) const override {
118 return Op.size() >= 8 ? MVT::i64 : MVT::i32;
121 bool isIntDivCheap(EVT VT, AttributeList Attr) const override {
122 return false;
125 bool shouldConvertConstantLoadToIntImm(const APInt &Imm,
126 Type *Ty) const override {
127 return true;
130 // Prevent reducing load width during SelectionDag phase.
131 // Otherwise, we may transform the following
132 // ctx = ctx + reloc_offset
133 // ... (*(u32 *)ctx) & 0x8000...
134 // to
135 // ctx = ctx + reloc_offset
136 // ... (*(u8 *)(ctx + 1)) & 0x80 ...
137 // which will be rejected by the verifier.
138 bool shouldReduceLoadWidth(SDNode *Load, ISD::LoadExtType ExtTy,
139 EVT NewVT) const override {
140 return false;
143 bool isLegalAddressingMode(const DataLayout &DL, const AddrMode &AM,
144 Type *Ty, unsigned AS,
145 Instruction *I = nullptr) const override;
147 // isTruncateFree - Return true if it's free to truncate a value of
148 // type Ty1 to type Ty2. e.g. On BPF at alu32 mode, it's free to truncate
149 // a i64 value in register R1 to i32 by referencing its sub-register W1.
150 bool isTruncateFree(Type *Ty1, Type *Ty2) const override;
151 bool isTruncateFree(EVT VT1, EVT VT2) const override;
153 // For 32bit ALU result zext to 64bit is free.
154 bool isZExtFree(Type *Ty1, Type *Ty2) const override;
155 bool isZExtFree(EVT VT1, EVT VT2) const override;
156 bool isZExtFree(SDValue Val, EVT VT2) const override;
158 unsigned EmitSubregExt(MachineInstr &MI, MachineBasicBlock *BB, unsigned Reg,
159 bool isSigned) const;
161 MachineBasicBlock * EmitInstrWithCustomInserterMemcpy(MachineInstr &MI,
162 MachineBasicBlock *BB)
163 const;
168 #endif