36 #define DEBUG_TYPE "bpf-isel"
46 return "BPF DAG->DAG Pattern Instruction Selection";
49 void PreprocessISelDAG()
override;
53 #include "BPFGenDAGISel.inc"
63 void PreprocessCopyToReg(
SDNode *Node);
67 typedef std::vector<unsigned char> val_vec_type;
69 val_vec_type &Vals, uint64_t Offset);
71 val_vec_type &Vals,
int Offset);
73 val_vec_type &Vals,
int Offset);
75 val_vec_type &Vals,
int Offset);
77 uint64_t
Size,
unsigned char *ByteSeq);
78 bool checkLoadDef(
unsigned DefReg,
unsigned match_load_op);
81 std::map<const void *, val_vec_type> cs_vals_;
83 std::map<unsigned, unsigned> load_to_vreg_;
92 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(),
MVT::i64);
93 Offset = CurDAG->getTargetConstant(0,
DL,
MVT::i64);
102 if (CurDAG->isBaseWithConstantOffset(Addr)) {
108 dyn_cast<FrameIndexSDNode>(Addr.
getOperand(0)))
109 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(),
MVT::i64);
119 Offset = CurDAG->getTargetConstant(0,
DL,
MVT::i64);
128 if (!CurDAG->isBaseWithConstantOffset(Addr))
137 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(),
MVT::i64);
168 errs() <<
"Error at line " << DL.
getLine() <<
": ";
171 errs() <<
"Unsupport signed division for DAG: ";
173 errs() <<
"Please convert to unsigned div/mod.\n";
177 unsigned IntNo = cast<ConstantSDNode>(Node->
getOperand(1))->getZExtValue();
179 case Intrinsic::bpf_load_byte:
180 case Intrinsic::bpf_load_half:
181 case Intrinsic::bpf_load_word: {
189 Chain = CurDAG->getCopyToReg(Chain, DL, R6Reg, Skb,
SDValue());
190 Node = CurDAG->UpdateNodeOperands(Node, Chain, N1, R6Reg, N3);
198 int FI = cast<FrameIndexSDNode>(Node)->getIndex();
200 SDValue TFI = CurDAG->getTargetFrameIndex(FI, VT);
201 unsigned Opc = BPF::MOV_rr;
203 CurDAG->SelectNodeTo(Node, Opc, VT, TFI);
206 ReplaceNode(Node, CurDAG->getMachineNode(Opc,
SDLoc(Node), VT, TFI));
215 void BPFDAGToDAGISel::PreprocessLoad(
SDNode *Node,
223 bool to_replace =
false;
228 if (!size || size > 8 || (size & (size - 1)))
233 unsigned opcode = LDAddrNode->
getOpcode();
250 getConstantFieldValue(GADN, CDN->getZExtValue(), size, new_val.c);
257 dyn_cast<GlobalAddressSDNode>(OP1.getNode()))
258 to_replace = getConstantFieldValue(GADN, 0, size, new_val.c);
276 DEBUG(
dbgs() <<
"Replacing load of size " << size <<
" with constant " << val
285 CurDAG->ReplaceAllUsesOfValuesWith(From, To, 2);
288 CurDAG->DeleteNode(Node);
291 void BPFDAGToDAGISel::PreprocessISelDAG() {
309 E = CurDAG->allnodes_end();
314 PreprocessLoad(Node, I);
316 PreprocessCopyToReg(Node);
318 PreprocessTrunc(Node, I);
323 uint64_t Offset, uint64_t
Size,
324 unsigned char *ByteSeq) {
331 const DataLayout &DL = CurDAG->getDataLayout();
334 auto it = cs_vals_.find(static_cast<const void *>(Init));
335 if (it != cs_vals_.end()) {
338 uint64_t total_size = 0;
342 else if (
const ConstantArray *CA = dyn_cast<ConstantArray>(Init))
344 CA->getNumOperands();
348 val_vec_type Vals(total_size, 0);
349 if (fillGenericConstant(DL, Init, Vals, 0) ==
false)
351 cs_vals_[
static_cast<const void *
>(Init)] = Vals;
352 TmpVal = std::move(Vals);
360 uint16_t test_val = 0x2345;
366 bool endian_match = test_buf.s == test_val;
367 for (uint64_t i = Offset, j = 0; i < Offset +
Size; i++, j++)
368 ByteSeq[j] = endian_match ? TmpVal[i] : TmpVal[Offset + Size - 1 - j];
373 bool BPFDAGToDAGISel::fillGenericConstant(
const DataLayout &DL,
375 val_vec_type &Vals, uint64_t Offset) {
378 if (isa<ConstantAggregateZero>(CV) || isa<UndefValue>(CV))
381 if (
const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
382 uint64_t val = CI->getZExtValue();
383 DEBUG(
dbgs() <<
"Byte array at offset " << Offset <<
" with value " << val
386 if (Size > 8 || (Size & (Size - 1)))
390 for (uint64_t i = 0; i <
Size; ++i) {
392 ? ((val >> (i * 8)) & 0xFF)
393 : ((val >> ((Size - i - 1) * 8)) & 0xFF);
399 return fillConstantDataArray(DL, CDA, Vals, Offset);
402 return fillConstantArray(DL, CA, Vals, Offset);
405 return fillConstantStruct(DL, CVS, Vals, Offset);
410 bool BPFDAGToDAGISel::fillConstantDataArray(
const DataLayout &DL,
412 val_vec_type &Vals,
int Offset) {
423 bool BPFDAGToDAGISel::fillConstantArray(
const DataLayout &DL,
425 val_vec_type &Vals,
int Offset) {
435 bool BPFDAGToDAGISel::fillConstantStruct(
const DataLayout &DL,
437 val_vec_type &Vals,
int Offset) {
442 if (fillGenericConstant(DL, Field, Vals, Offset + SizeSoFar) ==
false)
448 void BPFDAGToDAGISel::PreprocessCopyToReg(
SDNode *Node) {
458 unsigned mem_load_op = 0;
463 mem_load_op = BPF::LDW;
466 mem_load_op = BPF::LDH;
469 mem_load_op = BPF::LDB;
473 DEBUG(
dbgs() <<
"Find Load Value to VReg "
475 load_to_vreg_[RegN->
getReg()] = mem_load_op;
478 void BPFDAGToDAGISel::PreprocessTrunc(
SDNode *Node,
484 unsigned match_load_op = 0;
489 match_load_op = BPF::LDW;
492 match_load_op = BPF::LDH;
495 match_load_op = BPF::LDB;
510 unsigned AndOpReg = RegN->
getReg();
520 for (
auto &
MI : *MBB) {
521 for (
unsigned i = 0; i <
MI.getNumOperands(); ++i) {
533 if (MII ==
nullptr) {
535 if (!checkLoadDef(AndOpReg, match_load_op))
544 unsigned PrevReg = -1;
553 if (!checkLoadDef(PrevReg, match_load_op))
559 DEBUG(
dbgs() <<
"Remove the redundant AND operation in: "; Node->
dump();
563 CurDAG->ReplaceAllUsesWith(
SDValue(Node, 0), BaseV);
565 CurDAG->DeleteNode(Node);
568 bool BPFDAGToDAGISel::checkLoadDef(
unsigned DefReg,
unsigned match_load_op) {
569 auto it = load_to_vreg_.find(DefReg);
570 if (it == load_to_vreg_.end())
573 return it->second == match_load_op;
577 return new BPFDAGToDAGISel(TM);
BUILTIN_OP_END - This must be the last enum value in this list.
A parsed version of the target data layout string in and methods for querying it. ...
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
static unsigned virtReg2Index(unsigned Reg)
Convert a virtual register number to a 0-based index.
bool hasOneUse() const
Return true if there is exactly one use of this node.
unsigned getNumOperands() const
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
Constant * getElementAsConstant(unsigned i) const
Return a Constant for a specified index's element.
const GlobalValue * getGlobal() const
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
TargetGlobalAddress - Like GlobalAddress, but the DAG does no folding or anything else with this node...
unsigned getNumOperands() const
Return the number of values used by this operation.
constexpr bool isInt< 16 >(int64_t x)
const SDValue & getOperand(unsigned Num) const
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
RESULT,OUTCHAIN = INTRINSIC_W_CHAIN(INCHAIN, INTRINSICID, arg1, ...) This node represents a target in...
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
const StructLayout * getStructLayout(StructType *Ty) const
Returns a StructLayout object, indicating the alignment of the struct, its size, and the offsets of i...
void write16le(void *P, uint16_t V)
EVT getValueType(unsigned ResNo) const
Return the type of a specified result.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
CopyToReg - This node has three operands: a chain, a register number to set to this value...
Reg
All possible values of the reg field in the ModR/M byte.
unsigned getNumOperands() const
Access to explicit operands of the instruction.
Simple integer binary arithmetic operators.
bool isLittleEndian() const
Layout endianness...
uint64_t getElementOffset(unsigned Idx) const
SDNode * getNode() const
get the SDNode which holds the desired result
An array constant whose element type is a simple 1/2/4/8-byte integer or float/double, and whose elements are just simple data values (i.e.
const SDValue & getOperand(unsigned i) const
This is an important base class in LLVM.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
const MachineOperand & getOperand(unsigned i) const
unsigned getOpcode() const
FunctionPass class - This class is used to implement most global optimizations.
Value * getOperand(unsigned i) const
void print(raw_ostream &OS, const SelectionDAG *G=nullptr) const
Iterator for intrusive lists based on ilist_node.
This is the shared class of boolean and integer constants.
void dump() const
Dump this node, for debugging.
uint64_t getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
MachineOperand class - Representation of each machine instruction operand.
Type * getType() const
All values are typed, get the type of this value.
uint64_t getSizeInBytes() const
MachineMemOperand * getMemOperand() const
Return a MachineMemOperand object describing the memory reference performed by operation.
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
Represents one node in the SelectionDAG.
SelectionDAGISel - This is the common base class used for SelectionDAG-based pattern-matching instruc...
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
bool hasInitializer() const
Definitions have initializers, declarations don't.
ConstantArray - Constant Array Declarations.
int64_t getSExtValue() const
StructType * getType() const
Specialization - reduce amount of casting.
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
Representation of each machine instruction.
void write16be(void *P, uint16_t V)
Bitwise operators - logical and, logical or, logical xor.
LOAD and STORE have token chains as their first operand, then the same operands as an LLVM load/store...
unsigned getNumElements() const
Return the number of elements in the array or vector.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
CopyFromReg - This node indicates that the input value is a virtual or physical register that is defi...
const DebugLoc & getDebugLoc() const
Return the source location info.
unsigned getReg() const
getReg - Returns the register number.
uint64_t getSize() const
Return the size in bytes of the memory reference.
StringRef - Represent a constant reference to a string, i.e.
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation...
FunctionPass * createBPFISelDag(BPFTargetMachine &TM)
bool isMachineOpcode() const
Test if this node has a post-isel opcode, directly corresponding to a MachineInstr opcode...
uint64_t getZExtValue() const
This class is used to represent ISD::LOAD nodes.