1 //===-- BPFISelDAGToDAG.cpp - A dag to dag inst selector for BPF ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a DAG pattern matching instruction selector for BPF,
11 // converting from a legalized dag to a BPF dag.
13 //===----------------------------------------------------------------------===//
16 #include "BPFRegisterInfo.h"
17 #include "BPFSubtarget.h"
18 #include "BPFTargetMachine.h"
19 #include "llvm/CodeGen/MachineConstantPool.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MachineFrameInfo.h"
22 #include "llvm/CodeGen/MachineInstrBuilder.h"
23 #include "llvm/CodeGen/MachineRegisterInfo.h"
24 #include "llvm/CodeGen/SelectionDAGISel.h"
25 #include "llvm/Target/TargetMachine.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/IR/IntrinsicInst.h"
32 #define DEBUG_TYPE "bpf-isel"
34 // Instruction Selector Implementation
37 class BPFDAGToDAGISel : public SelectionDAGISel {
39 explicit BPFDAGToDAGISel(BPFTargetMachine &TM) : SelectionDAGISel(TM) {}
41 const char *getPassName() const override {
42 return "BPF DAG->DAG Pattern Instruction Selection";
46 // Include the pieces autogenerated from the target description.
47 #include "BPFGenDAGISel.inc"
49 SDNode *Select(SDNode *N) override;
51 // Complex Pattern for address selection.
52 bool SelectAddr(SDValue Addr, SDValue &Base, SDValue &Offset);
56 // ComplexPattern used on BPF Load/Store instructions
57 bool BPFDAGToDAGISel::SelectAddr(SDValue Addr, SDValue &Base, SDValue &Offset) {
58 // if Address is FI, get the TargetFrameIndex.
59 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
60 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i64);
61 Offset = CurDAG->getTargetConstant(0, MVT::i64);
65 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
66 Addr.getOpcode() == ISD::TargetGlobalAddress)
69 // Addresses of the form FI+const or FI|const
70 if (CurDAG->isBaseWithConstantOffset(Addr)) {
71 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1));
72 if (isInt<32>(CN->getSExtValue())) {
74 // If the first operand is a FI, get the TargetFI Node
75 if (FrameIndexSDNode *FIN =
76 dyn_cast<FrameIndexSDNode>(Addr.getOperand(0)))
77 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i64);
79 Base = Addr.getOperand(0);
81 Offset = CurDAG->getTargetConstant(CN->getSExtValue(), MVT::i64);
87 Offset = CurDAG->getTargetConstant(0, MVT::i64);
91 SDNode *BPFDAGToDAGISel::Select(SDNode *Node) {
92 unsigned Opcode = Node->getOpcode();
94 // Dump information about the Node being selected
95 DEBUG(dbgs() << "Selecting: "; Node->dump(CurDAG); dbgs() << '\n');
97 // If we have a custom node, we already have selected!
98 if (Node->isMachineOpcode()) {
99 DEBUG(dbgs() << "== "; Node->dump(CurDAG); dbgs() << '\n');
103 // tablegen selection should be handled here.
108 errs() << "BUG: "; Node->dump(CurDAG); errs() << '\n';
109 report_fatal_error("shouldn't see UNDEF during Select");
113 case ISD::INTRINSIC_W_CHAIN: {
114 unsigned IntNo = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
116 case Intrinsic::bpf_load_byte:
117 case Intrinsic::bpf_load_half:
118 case Intrinsic::bpf_load_word: {
120 SDValue Chain = Node->getOperand(0);
121 SDValue N1 = Node->getOperand(1);
122 SDValue Skb = Node->getOperand(2);
123 SDValue N3 = Node->getOperand(3);
125 SDValue R6Reg = CurDAG->getRegister(BPF::R6, MVT::i64);
126 Chain = CurDAG->getCopyToReg(Chain, DL, R6Reg, Skb, SDValue());
127 Node = CurDAG->UpdateNodeOperands(Node, Chain, N1, R6Reg, N3);
134 case ISD::FrameIndex: {
135 int FI = dyn_cast<FrameIndexSDNode>(Node)->getIndex();
136 EVT VT = Node->getValueType(0);
137 SDValue TFI = CurDAG->getTargetFrameIndex(FI, VT);
138 unsigned Opc = BPF::MOV_rr;
139 if (Node->hasOneUse())
140 return CurDAG->SelectNodeTo(Node, Opc, VT, TFI);
141 return CurDAG->getMachineNode(Opc, SDLoc(Node), VT, TFI);
145 // Select the default instruction
146 SDNode *ResNode = SelectCode(Node);
148 DEBUG(dbgs() << "=> ";
149 if (ResNode == nullptr || ResNode == Node)
152 ResNode->dump(CurDAG);
157 FunctionPass *llvm::createBPFISelDag(BPFTargetMachine &TM) {
158 return new BPFDAGToDAGISel(TM);