27 #include "llvm/IR/IntrinsicsBPF.h" 36 #define DEBUG_TYPE "bpf-isel" 52 return "BPF DAG->DAG Pattern Instruction Selection";
61 void PreprocessISelDAG()
override;
63 bool SelectInlineAsmMemoryOperand(
const SDValue &
Op,
unsigned ConstraintCode,
64 std::vector<SDValue> &OutOps)
override;
69 #include "BPFGenDAGISel.inc" 79 void PreprocessCopyToReg(
SDNode *Node);
83 typedef std::vector<unsigned char> val_vec_type;
85 val_vec_type &Vals, uint64_t Offset);
87 val_vec_type &Vals,
int Offset);
89 val_vec_type &Vals,
int Offset);
91 val_vec_type &Vals,
int Offset);
93 uint64_t
Size,
unsigned char *ByteSeq);
95 std::map<const void *, val_vec_type> cs_vals_;
104 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(),
MVT::i64);
105 Offset = CurDAG->getTargetConstant(0, DL,
MVT::i64);
114 if (CurDAG->isBaseWithConstantOffset(Addr)) {
120 dyn_cast<FrameIndexSDNode>(Addr.
getOperand(0)))
121 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(),
MVT::i64);
131 Offset = CurDAG->getTargetConstant(0, DL,
MVT::i64);
140 if (!CurDAG->isBaseWithConstantOffset(Addr))
149 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(),
MVT::i64);
160 bool BPFDAGToDAGISel::SelectInlineAsmMemoryOperand(
161 const SDValue &
Op,
unsigned ConstraintCode, std::vector<SDValue> &OutOps) {
163 switch (ConstraintCode) {
167 if (!SelectAddr(Op, Op0, Op1))
174 OutOps.push_back(Op0);
175 OutOps.push_back(Op1);
176 OutOps.push_back(AluOp);
197 errs() <<
"Error at line " << DL.
getLine() <<
": ";
200 errs() <<
"Unsupport signed division for DAG: ";
202 errs() <<
"Please convert to unsigned div/mod.\n";
206 unsigned IntNo = cast<ConstantSDNode>(Node->
getOperand(1))->getZExtValue();
208 case Intrinsic::bpf_load_byte:
209 case Intrinsic::bpf_load_half:
210 case Intrinsic::bpf_load_word: {
218 Chain = CurDAG->getCopyToReg(Chain, DL, R6Reg, Skb,
SDValue());
219 Node = CurDAG->UpdateNodeOperands(Node, Chain, N1, R6Reg, N3);
227 int FI = cast<FrameIndexSDNode>(Node)->getIndex();
229 SDValue TFI = CurDAG->getTargetFrameIndex(FI, VT);
230 unsigned Opc = BPF::MOV_rr;
232 CurDAG->SelectNodeTo(Node, Opc, VT, TFI);
235 ReplaceNode(Node, CurDAG->getMachineNode(Opc,
SDLoc(Node), VT, TFI));
244 void BPFDAGToDAGISel::PreprocessLoad(
SDNode *Node,
252 bool to_replace =
false;
257 if (!size || size > 8 || (size & (size - 1)))
262 unsigned opcode = LDAddrNode->
getOpcode();
279 getConstantFieldValue(GADN, CDN->getZExtValue(),
size, new_val.c);
286 dyn_cast<GlobalAddressSDNode>(OP1.getNode()))
287 to_replace = getConstantFieldValue(GADN, 0, size, new_val.c);
305 LLVM_DEBUG(
dbgs() <<
"Replacing load of size " << size <<
" with constant " 314 CurDAG->ReplaceAllUsesOfValuesWith(From, To, 2);
317 CurDAG->DeleteNode(Node);
320 void BPFDAGToDAGISel::PreprocessISelDAG() {
329 E = CurDAG->allnodes_end();
334 PreprocessLoad(Node, I);
336 PreprocessTrunc(Node, I);
341 uint64_t Offset, uint64_t
Size,
342 unsigned char *ByteSeq) {
349 const DataLayout &DL = CurDAG->getDataLayout();
352 auto it = cs_vals_.find(static_cast<const void *>(Init));
353 if (it != cs_vals_.end()) {
356 uint64_t total_size = 0;
360 else if (
const ConstantArray *CA = dyn_cast<ConstantArray>(Init))
362 CA->getNumOperands();
366 val_vec_type Vals(total_size, 0);
367 if (fillGenericConstant(DL, Init, Vals, 0) ==
false)
369 cs_vals_[
static_cast<const void *
>(Init)] = Vals;
370 TmpVal = std::move(Vals);
384 bool endian_match = test_buf.s == test_val;
385 for (uint64_t i = Offset, j = 0; i < Offset +
Size; i++, j++)
386 ByteSeq[j] = endian_match ? TmpVal[i] : TmpVal[Offset + Size - 1 - j];
391 bool BPFDAGToDAGISel::fillGenericConstant(
const DataLayout &DL,
393 val_vec_type &Vals, uint64_t Offset) {
396 if (isa<ConstantAggregateZero>(CV) || isa<UndefValue>(CV))
399 if (
const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
400 uint64_t val = CI->getZExtValue();
401 LLVM_DEBUG(
dbgs() <<
"Byte array at offset " << Offset <<
" with value " 404 if (Size > 8 || (Size & (Size - 1)))
408 for (uint64_t i = 0; i <
Size; ++i) {
410 ? ((val >> (i * 8)) & 0xFF)
411 : ((val >> ((Size - i - 1) * 8)) & 0xFF);
417 return fillConstantDataArray(DL, CDA, Vals, Offset);
420 return fillConstantArray(DL, CA, Vals, Offset);
423 return fillConstantStruct(DL, CVS, Vals, Offset);
428 bool BPFDAGToDAGISel::fillConstantDataArray(
const DataLayout &DL,
430 val_vec_type &Vals,
int Offset) {
441 bool BPFDAGToDAGISel::fillConstantArray(
const DataLayout &DL,
443 val_vec_type &Vals,
int Offset) {
453 bool BPFDAGToDAGISel::fillConstantStruct(
const DataLayout &DL,
455 val_vec_type &Vals,
int Offset) {
460 if (fillGenericConstant(DL, Field, Vals, Offset + SizeSoFar) ==
false)
466 void BPFDAGToDAGISel::PreprocessTrunc(
SDNode *Node,
482 unsigned IntNo = cast<ConstantSDNode>(BaseV->
getOperand(1))->getZExtValue();
485 if (!((IntNo == Intrinsic::bpf_load_byte && MaskV == 0xFF) ||
486 (IntNo == Intrinsic::bpf_load_half && MaskV == 0xFFFF) ||
487 (IntNo == Intrinsic::bpf_load_word && MaskV == 0xFFFFFFFF)))
494 CurDAG->ReplaceAllUsesWith(
SDValue(Node, 0), BaseV);
496 CurDAG->DeleteNode(Node);
502 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.
const GlobalValue * getGlobal() const
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
This class represents lattice values for constants.
Constant * getElementAsConstant(unsigned i) const
Return a Constant for a specified index's element.
const StructLayout * getStructLayout(StructType *Ty) const
Returns a StructLayout object, indicating the alignment of the struct, its size, and the offsets of i...
EVT getValueType(unsigned ResNo) const
Return the type of a specified result.
uint64_t getSize() const
Return the size in bytes of the memory reference.
TargetGlobalAddress - Like GlobalAddress, but the DAG does no folding or anything else with this node...
constexpr bool isInt< 16 >(int64_t x)
SDNode * getNode() const
get the SDNode which holds the desired result
MachineMemOperand * getMemOperand() const
Return a MachineMemOperand object describing the memory reference performed by operation.
const DebugLoc & getDebugLoc() const
Return the source location info.
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...
bool hasOneUse() const
Return true if there is exactly one use of this node.
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
void write16le(void *P, uint16_t V)
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
int64_t getSExtValue() const
Type * getType() const
All values are typed, get the type of this value.
Simple integer binary arithmetic operators.
bool isLittleEndian() const
Layout endianness...
Value * getOperand(unsigned i) const
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 TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This is an important base class in LLVM.
const SDValue & getOperand(unsigned Num) const
This file contains the declarations for the subclasses of Constant, which represent the different fla...
TypeSize getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
FunctionPass class - This class is used to implement most global optimizations.
bool isMachineOpcode() const
Test if this node has a post-isel opcode, directly corresponding to a MachineInstr opcode...
unsigned getNumOperands() const
Return the number of values used by this operation.
void dump() const
Dump this node, for debugging.
Iterator for intrusive lists based on ilist_node.
unsigned getNumOperands() const
This is the shared class of boolean and integer constants.
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
BlockVerifier::State From
uint64_t getSizeInBytes() const
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.
ConstantArray - Constant Array Declarations.
void write16be(void *P, uint16_t V)
Bitwise operators - logical and, logical or, logical xor.
uint64_t getElementOffset(unsigned Idx) const
LOAD and STORE have token chains as their first operand, then the same operands as an LLVM load/store...
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
unsigned getOpcode() const
void print(raw_ostream &OS, const SelectionDAG *G=nullptr) const
bool hasInitializer() const
Definitions have initializers, declarations don't.
StringRef - Represent a constant reference to a string, i.e.
unsigned getNumElements() const
Return the number of elements in the array or vector.
const SDValue & getOperand(unsigned i) const
uint64_t getZExtValue() const
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation...
StructType * getType() const
Specialization - reduce amount of casting.
FunctionPass * createBPFISelDag(BPFTargetMachine &TM)
This class is used to represent ISD::LOAD nodes.