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;
56 void PreprocessISelDAG()
override;
58 bool SelectInlineAsmMemoryOperand(
const SDValue &
Op,
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;
109 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i64);
110 Offset = CurDAG->getTargetConstant(0,
DL, MVT::i64);
119 if (CurDAG->isBaseWithConstantOffset(Addr)) {
124 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i64);
126 Base = Addr.getOperand(0);
128 Offset = CurDAG->getTargetConstant(CN->getSExtValue(),
DL, MVT::i64);
134 Offset = CurDAG->getTargetConstant(0,
DL, MVT::i64);
143 if (!CurDAG->isBaseWithConstantOffset(Addr))
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);
183void BPFDAGToDAGISel::Select(SDNode *Node) {
184 unsigned Opcode =
Node->getOpcode();
187 if (
Node->isMachineOpcode()) {
198 EVT VT =
Node->getValueType(0);
199 SDValue TFI = CurDAG->getTargetFrameIndex(FI, VT);
200 unsigned Opc = BPF::MOV_rr;
201 if (
Node->hasOneUse()) {
202 CurDAG->SelectNodeTo(Node,
Opc, VT, TFI);
205 ReplaceNode(Node, CurDAG->getMachineNode(
Opc, SDLoc(Node), VT, TFI));
214void BPFDAGToDAGISel::PreprocessLoad(SDNode *Node,
222 bool to_replace =
false;
225 if (!
LD->getMemOperand()->getSize().hasValue())
227 uint64_t
size =
LD->getMemOperand()->getSize().getValue();
232 SDNode *LDAddrNode =
LD->getOperand(1).getNode();
234 unsigned opcode = LDAddrNode->
getOpcode();
240 SDNode *OP1N =
OP1.getNode();
246 const GlobalAddressSDNode *GADN =
257 if (
const GlobalAddressSDNode *GADN =
259 to_replace = getConstantFieldValue(GADN, 0,
size, new_val.c);
279 SDValue NVal = CurDAG->getConstant(val,
DL,
LD->getValueType(0));
286 CurDAG->ReplaceAllUsesOfValuesWith(From, To, 2);
289 CurDAG->DeleteNode(Node);
292void BPFDAGToDAGISel::PreprocessISelDAG() {
301 E = CurDAG->allnodes_end();
303 SDNode *
Node = &*
I++;
304 unsigned Opcode =
Node->getOpcode();
305 if (Opcode == ISD::LOAD)
306 PreprocessLoad(Node,
I);
308 PreprocessTrunc(Node,
I);
312bool BPFDAGToDAGISel::getConstantFieldValue(
const GlobalAddressSDNode *Node,
314 unsigned char *ByteSeq) {
317 if (!V || !
V->hasInitializer() || !
V->isConstant())
320 const Constant *Init =
V->getInitializer();
321 const DataLayout &
DL = CurDAG->getDataLayout();
324 auto it = cs_vals_.find(
static_cast<const void *
>(Init));
325 if (it != cs_vals_.end()) {
328 uint64_t total_size = 0;
333 total_size =
DL.getTypeAllocSize(CA->getType()->getElementType()) *
334 CA->getNumOperands();
338 val_vec_type Vals(total_size, 0);
339 if (fillGenericConstant(
DL, Init, Vals, 0) ==
false)
341 cs_vals_[
static_cast<const void *
>(Init)] = Vals;
342 TmpVal = std::move(Vals);
350 uint16_t test_val = 0x2345;
351 if (
DL.isLittleEndian())
356 bool endian_match = test_buf.s == test_val;
358 ByteSeq[j] = endian_match ? TmpVal[i] : TmpVal[
Offset +
Size - 1 -
j];
363bool BPFDAGToDAGISel::fillGenericConstant(
const DataLayout &
DL,
365 val_vec_type &Vals, uint64_t
Offset) {
372 uint64_t val = CI->getZExtValue();
380 for (uint64_t i = 0; i <
Size; ++i) {
381 Vals[
Offset + i] =
DL.isLittleEndian()
382 ? ((val >> (i * 8)) & 0xFF)
383 : ((val >> ((
Size - i - 1) * 8)) & 0xFF);
389 return fillConstantDataArray(
DL, CDA, Vals,
Offset);
392 return fillConstantArray(
DL, CA, Vals,
Offset);
395 return fillConstantStruct(
DL, CVS, Vals,
Offset);
400bool BPFDAGToDAGISel::fillConstantDataArray(
const DataLayout &
DL,
401 const ConstantDataArray *CDA,
402 val_vec_type &Vals,
int Offset) {
413bool BPFDAGToDAGISel::fillConstantArray(
const DataLayout &
DL,
414 const ConstantArray *CA,
415 val_vec_type &Vals,
int Offset) {
425bool BPFDAGToDAGISel::fillConstantStruct(
const DataLayout &
DL,
426 const ConstantStruct *CS,
427 val_vec_type &Vals,
int Offset) {
428 const StructLayout *Layout =
DL.getStructLayout(CS->
getType());
432 if (fillGenericConstant(
DL,
Field, Vals,
Offset + SizeSoFar) ==
false)
438void BPFDAGToDAGISel::PreprocessTrunc(SDNode *Node,
457 if (!((IntNo == Intrinsic::bpf_load_byte && MaskV == 0xFF) ||
458 (IntNo == Intrinsic::bpf_load_half && MaskV == 0xFFFF) ||
459 (IntNo == Intrinsic::bpf_load_word && MaskV == 0xFFFFFFFF)))
466 CurDAG->ReplaceAllUsesWith(
SDValue(Node, 0), BaseV);
468 CurDAG->DeleteNode(Node);
472 return new BPFDAGToDAGISelLegacy(TM);
AMDGPU Register Bank Select
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
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 ...
OptimizedStructLayoutField Field
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
ConstantArray - Constant Array Declarations.
An array constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
LLVM_ABI Constant * getElementAsConstant(uint64_t i) const
Return a Constant for a specified index's element.
LLVM_ABI uint64_t getNumElements() const
Return the number of elements in the array or vector.
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.
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
const SDValue & getOperand(unsigned i) const
unsigned getOpcode() const
SelectionDAGISel - This is the common base class used for SelectionDAG-based pattern-matching instruc...
virtual bool runOnMachineFunction(MachineFunction &mf)
ilist< SDNode >::iterator allnodes_iterator
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.
@ 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...
NodeAddr< NodeBase * > Node
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.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
FunctionPass * createBPFISelDag(BPFTargetMachine &TM)