24#include "llvm/IR/IntrinsicsBPF.h"
32#define DEBUG_TYPE "bpf-isel"
33#define PASS_NAME "BPF DAG->DAG Pattern Instruction Selection"
45 BPFDAGToDAGISel() =
delete;
60 std::vector<SDValue> &OutOps)
override;
64#include "BPFGenDAGISel.inc"
77 typedef std::vector<unsigned char> val_vec_type;
81 val_vec_type &Vals,
int Offset);
83 val_vec_type &Vals,
int Offset);
85 val_vec_type &Vals,
int Offset);
89 std::map<const void *, val_vec_type> cs_vals_;
100char BPFDAGToDAGISelLegacy::ID = 0;
108 if (
auto *FIN = dyn_cast<FrameIndexSDNode>(
Addr)) {
109 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i64);
110 Offset = CurDAG->getTargetConstant(0,
DL, MVT::i64);
119 if (CurDAG->isBaseWithConstantOffset(
Addr)) {
120 auto *CN = cast<ConstantSDNode>(
Addr.getOperand(1));
121 if (isInt<16>(CN->getSExtValue())) {
123 if (
auto *FIN = dyn_cast<FrameIndexSDNode>(
Addr.getOperand(0)))
124 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i64);
128 Offset = CurDAG->getTargetConstant(CN->getSExtValue(),
DL, MVT::i64);
134 Offset = CurDAG->getTargetConstant(0,
DL, MVT::i64);
143 if (!CurDAG->isBaseWithConstantOffset(
Addr))
147 auto *CN = cast<ConstantSDNode>(
Addr.getOperand(1));
148 if (isInt<16>(CN->getSExtValue())) {
150 if (
auto *FIN = dyn_cast<FrameIndexSDNode>(
Addr.getOperand(0)))
151 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i64);
155 Offset = CurDAG->getTargetConstant(CN->getSExtValue(),
DL, MVT::i64);
162bool BPFDAGToDAGISel::SelectInlineAsmMemoryOperand(
164 std::vector<SDValue> &OutOps) {
166 switch (ConstraintCode) {
169 case InlineAsm::ConstraintCode::m:
170 if (!SelectAddr(
Op, Op0, Op1))
177 OutOps.push_back(Op0);
178 OutOps.push_back(Op1);
179 OutOps.push_back(AluOp);
184 unsigned Opcode =
Node->getOpcode();
187 if (
Node->isMachineOpcode()) {
197 unsigned IntNo =
Node->getConstantOperandVal(1);
199 case Intrinsic::bpf_load_byte:
200 case Intrinsic::bpf_load_half:
201 case Intrinsic::bpf_load_word: {
208 SDValue R6Reg = CurDAG->getRegister(BPF::R6, MVT::i64);
209 Chain = CurDAG->getCopyToReg(Chain,
DL, R6Reg, Skb,
SDValue());
210 Node = CurDAG->UpdateNodeOperands(
Node, Chain, N1, R6Reg, N3);
218 int FI = cast<FrameIndexSDNode>(
Node)->getIndex();
219 EVT VT =
Node->getValueType(0);
220 SDValue TFI = CurDAG->getTargetFrameIndex(FI, VT);
221 unsigned Opc = BPF::MOV_rr;
222 if (
Node->hasOneUse()) {
223 CurDAG->SelectNodeTo(
Node, Opc, VT, TFI);
226 ReplaceNode(
Node, CurDAG->getMachineNode(Opc,
SDLoc(
Node), VT, TFI));
235void BPFDAGToDAGISel::PreprocessLoad(
SDNode *
Node,
243 bool to_replace =
false;
246 if (!
LD->getMemOperand()->getSize().hasValue())
250 if (!size || size > 8 || (size & (size - 1)) || !
LD->isSimple())
253 SDNode *LDAddrNode =
LD->getOperand(1).getNode();
255 unsigned opcode = LDAddrNode->
getOpcode();
272 getConstantFieldValue(GADN, CDN->
getZExtValue(), size, new_val.c);
279 dyn_cast<GlobalAddressSDNode>(
OP1.getNode()))
280 to_replace = getConstantFieldValue(GADN, 0, size, new_val.c);
298 LLVM_DEBUG(
dbgs() <<
"Replacing load of size " << size <<
" with constant "
300 SDValue NVal = CurDAG->getConstant(val,
DL,
LD->getValueType(0));
307 CurDAG->ReplaceAllUsesOfValuesWith(
From, To, 2);
310 CurDAG->DeleteNode(
Node);
313void BPFDAGToDAGISel::PreprocessISelDAG() {
322 E = CurDAG->allnodes_end();
325 unsigned Opcode =
Node->getOpcode();
327 PreprocessLoad(
Node,
I);
329 PreprocessTrunc(
Node,
I);
335 unsigned char *ByteSeq) {
338 if (!V || !
V->hasInitializer() || !
V->isConstant())
345 auto it = cs_vals_.find(
static_cast<const void *
>(
Init));
346 if (it != cs_vals_.end()) {
352 DL.getStructLayout(cast<StructType>(CS->getType()))->getSizeInBytes();
354 total_size =
DL.getTypeAllocSize(CA->getType()->getElementType()) *
355 CA->getNumOperands();
359 val_vec_type Vals(total_size, 0);
360 if (fillGenericConstant(
DL,
Init, Vals, 0) ==
false)
362 cs_vals_[
static_cast<const void *
>(
Init)] = Vals;
363 TmpVal = std::move(Vals);
372 if (
DL.isLittleEndian())
377 bool endian_match = test_buf.s == test_val;
379 ByteSeq[j] = endian_match ? TmpVal[i] : TmpVal[
Offset +
Size - 1 - j];
384bool BPFDAGToDAGISel::fillGenericConstant(
const DataLayout &
DL,
389 if (isa<ConstantAggregateZero>(CV) || isa<UndefValue>(CV))
392 if (
const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
402 Vals[
Offset + i] =
DL.isLittleEndian()
403 ? ((val >> (i * 8)) & 0xFF)
404 : ((val >> ((
Size - i - 1) * 8)) & 0xFF);
410 return fillConstantDataArray(
DL, CDA, Vals,
Offset);
413 return fillConstantArray(
DL, CA, Vals,
Offset);
416 return fillConstantStruct(
DL, CVS, Vals,
Offset);
421bool BPFDAGToDAGISel::fillConstantDataArray(
const DataLayout &
DL,
423 val_vec_type &Vals,
int Offset) {
434bool BPFDAGToDAGISel::fillConstantArray(
const DataLayout &
DL,
436 val_vec_type &Vals,
int Offset) {
446bool BPFDAGToDAGISel::fillConstantStruct(
const DataLayout &
DL,
448 val_vec_type &Vals,
int Offset) {
453 if (fillGenericConstant(
DL,
Field, Vals,
Offset + SizeSoFar) ==
false)
459void BPFDAGToDAGISel::PreprocessTrunc(
SDNode *
Node,
478 if (!((IntNo == Intrinsic::bpf_load_byte && MaskV == 0xFF) ||
479 (IntNo == Intrinsic::bpf_load_half && MaskV == 0xFFFF) ||
480 (IntNo == Intrinsic::bpf_load_word && MaskV == 0xFFFFFFFF)))
487 CurDAG->ReplaceAllUsesWith(
SDValue(
Node, 0), BaseV);
489 CurDAG->DeleteNode(
Node);
493 return new BPFDAGToDAGISelLegacy(TM);
AMDGPU Register Bank Select
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
BlockVerifier::State From
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
DEMANGLE_DUMP_METHOD void dump() const
ConstantArray - Constant Array Declarations.
An array constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
unsigned getNumElements() const
Return the number of elements in the array or vector.
Constant * getElementAsConstant(unsigned i) const
Return a Constant for a specified index's element.
This is the shared class of boolean and integer constants.
uint64_t getZExtValue() const
StructType * getType() const
Specialization - reduce amount of casting.
This is an important base class in LLVM.
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
FunctionPass class - This class is used to implement most global optimizations.
This class is used to represent ISD::LOAD nodes.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
Represents one node in the SelectionDAG.
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
unsigned getNumOperands() const
Return the number of values used by this operation.
const SDValue & getOperand(unsigned Num) const
uint64_t getConstantOperandVal(unsigned Num) const
Helper method returns the integer value of a ConstantSDNode operand.
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
SDNode * getNode() const
get the SDNode which holds the desired result
unsigned getOpcode() const
SelectionDAGISel - This is the common base class used for SelectionDAG-based pattern-matching instruc...
virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op, InlineAsm::ConstraintCode ConstraintID, std::vector< SDValue > &OutOps)
SelectInlineAsmMemoryOperand - Select the specified address as a target addressing mode,...
virtual void PreprocessISelDAG()
PreprocessISelDAG - This hook allows targets to hack on the graph before instruction selection starts...
virtual bool runOnMachineFunction(MachineFunction &mf)
ilist< SDNode >::iterator allnodes_iterator
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
TypeSize getElementOffset(unsigned Idx) const
Value * getOperand(unsigned i) const
unsigned getNumOperands() const
Type * getType() const
All values are typed, get the type of this value.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
@ ADD
Simple integer binary arithmetic operators.
@ LOAD
LOAD and STORE have token chains as their first operand, then the same operands as an LLVM load/store...
@ BUILTIN_OP_END
BUILTIN_OP_END - This must be the last enum value in this list.
@ TargetGlobalAddress
TargetGlobalAddress - Like GlobalAddress, but the DAG does no folding or anything else with this node...
@ AND
Bitwise operators - logical and, logical or, logical xor.
@ INTRINSIC_W_CHAIN
RESULT,OUTCHAIN = INTRINSIC_W_CHAIN(INCHAIN, INTRINSICID, arg1, ...) This node represents a target in...
void write16be(void *P, uint16_t V)
void write16le(void *P, uint16_t V)
This is an optimization pass for GlobalISel generic memory operations.
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
FunctionPass * createBPFISelDag(BPFTargetMachine &TM)