27#include "llvm/IR/IntrinsicsBPF.h"
36#define DEBUG_TYPE "bpf-isel"
37#define PASS_NAME "BPF DAG->DAG Pattern Instruction Selection"
51 BPFDAGToDAGISel() =
delete;
65 std::vector<SDValue> &OutOps)
override;
70#include "BPFGenDAGISel.inc"
83 typedef std::vector<unsigned char> val_vec_type;
87 val_vec_type &Vals,
int Offset);
89 val_vec_type &Vals,
int Offset);
91 val_vec_type &Vals,
int Offset);
95 std::map<const void *, val_vec_type> cs_vals_;
99char BPFDAGToDAGISel::ID = 0;
107 if (
auto *FIN = dyn_cast<FrameIndexSDNode>(
Addr)) {
108 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i64);
109 Offset = CurDAG->getTargetConstant(0,
DL, MVT::i64);
118 if (CurDAG->isBaseWithConstantOffset(
Addr)) {
119 auto *CN = cast<ConstantSDNode>(
Addr.getOperand(1));
120 if (isInt<16>(CN->getSExtValue())) {
122 if (
auto *FIN = dyn_cast<FrameIndexSDNode>(
Addr.getOperand(0)))
123 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i64);
127 Offset = CurDAG->getTargetConstant(CN->getSExtValue(),
DL, MVT::i64);
133 Offset = CurDAG->getTargetConstant(0,
DL, MVT::i64);
142 if (!CurDAG->isBaseWithConstantOffset(
Addr))
146 auto *CN = cast<ConstantSDNode>(
Addr.getOperand(1));
147 if (isInt<16>(CN->getSExtValue())) {
149 if (
auto *FIN = dyn_cast<FrameIndexSDNode>(
Addr.getOperand(0)))
150 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i64);
154 Offset = CurDAG->getTargetConstant(CN->getSExtValue(),
DL, MVT::i64);
161bool BPFDAGToDAGISel::SelectInlineAsmMemoryOperand(
162 const SDValue &Op,
unsigned ConstraintCode, std::vector<SDValue> &OutOps) {
164 switch (ConstraintCode) {
168 if (!SelectAddr(Op, Op0, Op1))
175 OutOps.push_back(Op0);
176 OutOps.push_back(Op1);
177 OutOps.push_back(AluOp);
182 unsigned Opcode =
Node->getOpcode();
185 if (
Node->isMachineOpcode()) {
198 errs() <<
"Error at line " <<
DL.getLine() <<
": ";
201 errs() <<
"Unsupport signed division for DAG: ";
203 errs() <<
"Please convert to unsigned div/mod.\n";
207 unsigned IntNo = cast<ConstantSDNode>(
Node->getOperand(1))->getZExtValue();
209 case Intrinsic::bpf_load_byte:
210 case Intrinsic::bpf_load_half:
211 case Intrinsic::bpf_load_word: {
218 SDValue R6Reg = CurDAG->getRegister(BPF::R6, MVT::i64);
219 Chain = CurDAG->getCopyToReg(Chain,
DL, R6Reg, Skb,
SDValue());
220 Node = CurDAG->UpdateNodeOperands(
Node, Chain, N1, R6Reg, N3);
228 int FI = cast<FrameIndexSDNode>(
Node)->getIndex();
229 EVT VT =
Node->getValueType(0);
230 SDValue TFI = CurDAG->getTargetFrameIndex(FI, VT);
231 unsigned Opc = BPF::MOV_rr;
232 if (
Node->hasOneUse()) {
233 CurDAG->SelectNodeTo(
Node, Opc, VT, TFI);
236 ReplaceNode(
Node, CurDAG->getMachineNode(Opc,
SDLoc(
Node), VT, TFI));
245void BPFDAGToDAGISel::PreprocessLoad(
SDNode *
Node,
253 bool to_replace =
false;
258 if (!size || size > 8 || (size & (size - 1)) || !
LD->isSimple())
261 SDNode *LDAddrNode =
LD->getOperand(1).getNode();
263 unsigned opcode = LDAddrNode->
getOpcode();
280 getConstantFieldValue(GADN, CDN->
getZExtValue(), size, new_val.c);
287 dyn_cast<GlobalAddressSDNode>(
OP1.getNode()))
288 to_replace = getConstantFieldValue(GADN, 0, size, new_val.c);
306 LLVM_DEBUG(
dbgs() <<
"Replacing load of size " << size <<
" with constant "
308 SDValue NVal = CurDAG->getConstant(val,
DL,
LD->getValueType(0));
315 CurDAG->ReplaceAllUsesOfValuesWith(
From, To, 2);
318 CurDAG->DeleteNode(
Node);
321void BPFDAGToDAGISel::PreprocessISelDAG() {
330 E = CurDAG->allnodes_end();
333 unsigned Opcode =
Node->getOpcode();
335 PreprocessLoad(
Node,
I);
337 PreprocessTrunc(
Node,
I);
343 unsigned char *ByteSeq) {
346 if (!V || !
V->hasInitializer() || !
V->isConstant())
353 auto it = cs_vals_.find(
static_cast<const void *
>(
Init));
354 if (it != cs_vals_.end()) {
360 DL.getStructLayout(cast<StructType>(CS->getType()))->getSizeInBytes();
362 total_size =
DL.getTypeAllocSize(CA->getType()->getElementType()) *
363 CA->getNumOperands();
367 val_vec_type Vals(total_size, 0);
368 if (fillGenericConstant(
DL,
Init, Vals, 0) ==
false)
370 cs_vals_[
static_cast<const void *
>(
Init)] = Vals;
371 TmpVal = std::move(Vals);
380 if (
DL.isLittleEndian())
385 bool endian_match = test_buf.s == test_val;
387 ByteSeq[j] = endian_match ? TmpVal[i] : TmpVal[
Offset +
Size - 1 - j];
392bool BPFDAGToDAGISel::fillGenericConstant(
const DataLayout &
DL,
397 if (isa<ConstantAggregateZero>(CV) || isa<UndefValue>(CV))
400 if (
const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
410 Vals[
Offset + i] =
DL.isLittleEndian()
411 ? ((val >> (i * 8)) & 0xFF)
412 : ((val >> ((
Size - i - 1) * 8)) & 0xFF);
418 return fillConstantDataArray(
DL, CDA, Vals,
Offset);
421 return fillConstantArray(
DL, CA, Vals,
Offset);
424 return fillConstantStruct(
DL, CVS, Vals,
Offset);
429bool BPFDAGToDAGISel::fillConstantDataArray(
const DataLayout &
DL,
431 val_vec_type &Vals,
int Offset) {
442bool BPFDAGToDAGISel::fillConstantArray(
const DataLayout &
DL,
444 val_vec_type &Vals,
int Offset) {
454bool BPFDAGToDAGISel::fillConstantStruct(
const DataLayout &
DL,
456 val_vec_type &Vals,
int Offset) {
461 if (fillGenericConstant(
DL,
Field, Vals,
Offset + SizeSoFar) ==
false)
467void BPFDAGToDAGISel::PreprocessTrunc(
SDNode *
Node,
483 unsigned IntNo = cast<ConstantSDNode>(BaseV->
getOperand(1))->getZExtValue();
486 if (!((IntNo == Intrinsic::bpf_load_byte && MaskV == 0xFF) ||
487 (IntNo == Intrinsic::bpf_load_half && MaskV == 0xFFFF) ||
488 (IntNo == Intrinsic::bpf_load_word && MaskV == 0xFFFFFFFF)))
495 CurDAG->ReplaceAllUsesWith(
SDValue(
Node, 0), BaseV);
497 CurDAG->DeleteNode(
Node);
501 return new BPFDAGToDAGISel(
TM);
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
amdgpu AMDGPU Register Bank Select
BlockVerifier::State From
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
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 ...
const char LLVMTargetMachineRef TM
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
void print(OutputBuffer &OB) const
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.
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
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 void PreprocessISelDAG()
PreprocessISelDAG - This hook allows targets to hack on the graph before instruction selection starts...
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID, std::vector< SDValue > &OutOps)
SelectInlineAsmMemoryOperand - Select the specified address as a target addressing mode,...
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.
Iterator for intrusive lists based on ilist_node.
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.
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
FunctionPass * createBPFISelDag(BPFTargetMachine &TM)