1//===-- BPFISelDAGToDAG.cpp - A dag to dag inst selector for BPF ----------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a DAG pattern matching instruction selector for BPF,
11// converting from a legalized dag to a BPF dag.
12//
13//===----------------------------------------------------------------------===//
14
15#include "BPF.h"
16#include "BPFRegisterInfo.h"
17#include "BPFSubtarget.h"
18#include "BPFTargetMachine.h"
19#include "llvm/CodeGen/MachineConstantPool.h"
20#include "llvm/CodeGen/MachineFrameInfo.h"
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineInstrBuilder.h"
23#include "llvm/CodeGen/MachineRegisterInfo.h"
24#include "llvm/CodeGen/SelectionDAGISel.h"
25#include "llvm/IR/IntrinsicInst.h"
26#include "llvm/Support/Debug.h"
27#include "llvm/Support/ErrorHandling.h"
28#include "llvm/Support/raw_ostream.h"
29#include "llvm/Target/TargetMachine.h"
30using namespace llvm;
31
32#define DEBUG_TYPE "bpf-isel"
33
34// Instruction Selector Implementation
35namespace {
36
37class BPFDAGToDAGISel : public SelectionDAGISel {
38public:
39  explicit BPFDAGToDAGISel(BPFTargetMachine &TM) : SelectionDAGISel(TM) {}
40
41  const char *getPassName() const override {
42    return "BPF DAG->DAG Pattern Instruction Selection";
43  }
44
45private:
46// Include the pieces autogenerated from the target description.
47#include "BPFGenDAGISel.inc"
48
49  SDNode *Select(SDNode *N) override;
50
51  // Complex Pattern for address selection.
52  bool SelectAddr(SDValue Addr, SDValue &Base, SDValue &Offset);
53  bool SelectFIAddr(SDValue Addr, SDValue &Base, SDValue &Offset);
54};
55}
56
57// ComplexPattern used on BPF Load/Store instructions
58bool BPFDAGToDAGISel::SelectAddr(SDValue Addr, SDValue &Base, SDValue &Offset) {
59  // if Address is FI, get the TargetFrameIndex.
60  SDLoc DL(Addr);
61  if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
62    Base   = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i64);
63    Offset = CurDAG->getTargetConstant(0, DL, MVT::i64);
64    return true;
65  }
66
67  if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
68      Addr.getOpcode() == ISD::TargetGlobalAddress)
69    return false;
70
71  // Addresses of the form Addr+const or Addr|const
72  if (CurDAG->isBaseWithConstantOffset(Addr)) {
73    ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1));
74    if (isInt<32>(CN->getSExtValue())) {
75
76      // If the first operand is a FI, get the TargetFI Node
77      if (FrameIndexSDNode *FIN =
78              dyn_cast<FrameIndexSDNode>(Addr.getOperand(0)))
79        Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i64);
80      else
81        Base = Addr.getOperand(0);
82
83      Offset = CurDAG->getTargetConstant(CN->getSExtValue(), DL, MVT::i64);
84      return true;
85    }
86  }
87
88  Base   = Addr;
89  Offset = CurDAG->getTargetConstant(0, DL, MVT::i64);
90  return true;
91}
92
93// ComplexPattern used on BPF FI instruction
94bool BPFDAGToDAGISel::SelectFIAddr(SDValue Addr, SDValue &Base, SDValue &Offset) {
95  SDLoc DL(Addr);
96
97  if (!CurDAG->isBaseWithConstantOffset(Addr))
98    return false;
99
100  // Addresses of the form Addr+const or Addr|const
101  ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1));
102  if (isInt<32>(CN->getSExtValue())) {
103
104    // If the first operand is a FI, get the TargetFI Node
105    if (FrameIndexSDNode *FIN =
106            dyn_cast<FrameIndexSDNode>(Addr.getOperand(0)))
107      Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i64);
108    else
109      return false;
110
111    Offset = CurDAG->getTargetConstant(CN->getSExtValue(), DL, MVT::i64);
112    return true;
113  }
114
115  return false;
116}
117
118SDNode *BPFDAGToDAGISel::Select(SDNode *Node) {
119  unsigned Opcode = Node->getOpcode();
120
121  // Dump information about the Node being selected
122  DEBUG(dbgs() << "Selecting: "; Node->dump(CurDAG); dbgs() << '\n');
123
124  // If we have a custom node, we already have selected!
125  if (Node->isMachineOpcode()) {
126    DEBUG(dbgs() << "== "; Node->dump(CurDAG); dbgs() << '\n');
127    return NULL;
128  }
129
130  // tablegen selection should be handled here.
131  switch (Opcode) {
132  default: break;
133  case ISD::INTRINSIC_W_CHAIN: {
134    unsigned IntNo = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
135    switch (IntNo) {
136    case Intrinsic::bpf_load_byte:
137    case Intrinsic::bpf_load_half:
138    case Intrinsic::bpf_load_word: {
139      SDLoc DL(Node);
140      SDValue Chain = Node->getOperand(0);
141      SDValue N1 = Node->getOperand(1);
142      SDValue Skb = Node->getOperand(2);
143      SDValue N3 = Node->getOperand(3);
144
145      SDValue R6Reg = CurDAG->getRegister(BPF::R6, MVT::i64);
146      Chain = CurDAG->getCopyToReg(Chain, DL, R6Reg, Skb, SDValue());
147      Node = CurDAG->UpdateNodeOperands(Node, Chain, N1, R6Reg, N3);
148      break;
149    }
150    }
151    break;
152  }
153
154  case ISD::FrameIndex: {
155    int FI = cast<FrameIndexSDNode>(Node)->getIndex();
156    EVT VT = Node->getValueType(0);
157    SDValue TFI = CurDAG->getTargetFrameIndex(FI, VT);
158    unsigned Opc = BPF::MOV_rr;
159    if (Node->hasOneUse())
160      return CurDAG->SelectNodeTo(Node, Opc, VT, TFI);
161    return CurDAG->getMachineNode(Opc, SDLoc(Node), VT, TFI);
162  }
163  }
164
165  // Select the default instruction
166  SDNode *ResNode = SelectCode(Node);
167
168  DEBUG(dbgs() << "=> ";
169        if (ResNode == nullptr || ResNode == Node)
170          Node->dump(CurDAG);
171        else
172          ResNode->dump(CurDAG);
173        dbgs() << '\n');
174  return ResNode;
175}
176
177FunctionPass *llvm::createBPFISelDag(BPFTargetMachine &TM) {
178  return new BPFDAGToDAGISel(TM);
179}
180