blob: 12091449cc11a255631edc2270580d4388b7fec0 [file] [log] [blame]
Alexei Starovoitove4c8c802015-01-24 17:51:26 +00001//===-- 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"
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000020#include "llvm/CodeGen/MachineFrameInfo.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000021#include "llvm/CodeGen/MachineFunction.h"
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000022#include "llvm/CodeGen/MachineInstrBuilder.h"
23#include "llvm/CodeGen/MachineRegisterInfo.h"
24#include "llvm/CodeGen/SelectionDAGISel.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000025#include "llvm/IR/IntrinsicInst.h"
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000026#include "llvm/Support/Debug.h"
27#include "llvm/Support/ErrorHandling.h"
28#include "llvm/Support/raw_ostream.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000029#include "llvm/Target/TargetMachine.h"
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000030using 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
Mehdi Amini117296c2016-10-01 02:56:57 +000041 StringRef getPassName() const override {
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000042 return "BPF DAG->DAG Pattern Instruction Selection";
43 }
44
45private:
46// Include the pieces autogenerated from the target description.
47#include "BPFGenDAGISel.inc"
48
Justin Bognerf076e792016-05-12 21:14:47 +000049 void Select(SDNode *N) override;
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000050
51 // Complex Pattern for address selection.
52 bool SelectAddr(SDValue Addr, SDValue &Base, SDValue &Offset);
Alexei Starovoitov4e01a382015-10-06 04:00:53 +000053 bool SelectFIAddr(SDValue Addr, SDValue &Base, SDValue &Offset);
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000054};
Alexander Kornienkof00654e2015-06-23 09:49:53 +000055}
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000056
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.
Alexei Starovoitov659ece92015-04-28 20:38:56 +000060 SDLoc DL(Addr);
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000061 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
62 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i64);
Alexei Starovoitov659ece92015-04-28 20:38:56 +000063 Offset = CurDAG->getTargetConstant(0, DL, MVT::i64);
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000064 return true;
65 }
66
67 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
68 Addr.getOpcode() == ISD::TargetGlobalAddress)
69 return false;
70
Alexei Starovoitov4e01a382015-10-06 04:00:53 +000071 // Addresses of the form Addr+const or Addr|const
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000072 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
Alexei Starovoitov659ece92015-04-28 20:38:56 +000083 Offset = CurDAG->getTargetConstant(CN->getSExtValue(), DL, MVT::i64);
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000084 return true;
85 }
86 }
87
88 Base = Addr;
Alexei Starovoitov659ece92015-04-28 20:38:56 +000089 Offset = CurDAG->getTargetConstant(0, DL, MVT::i64);
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000090 return true;
91}
92
Alexei Starovoitov4e01a382015-10-06 04:00:53 +000093// 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
Justin Bognerf076e792016-05-12 21:14:47 +0000118void BPFDAGToDAGISel::Select(SDNode *Node) {
Alexei Starovoitove4c8c802015-01-24 17:51:26 +0000119 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');
Justin Bognerf076e792016-05-12 21:14:47 +0000127 return;
Alexei Starovoitove4c8c802015-01-24 17:51:26 +0000128 }
129
130 // tablegen selection should be handled here.
131 switch (Opcode) {
132 default: break;
Alexei Starovoitov7e453bb2016-03-18 22:02:47 +0000133 case ISD::SDIV: {
134 DebugLoc Empty;
135 const DebugLoc &DL = Node->getDebugLoc();
136 if (DL != Empty)
137 errs() << "Error at line " << DL.getLine() << ": ";
138 else
139 errs() << "Error: ";
140 errs() << "Unsupport signed division for DAG: ";
141 Node->dump(CurDAG);
142 errs() << "Please convert to unsigned div/mod.\n";
143 break;
144 }
Alexei Starovoitove4c8c802015-01-24 17:51:26 +0000145 case ISD::INTRINSIC_W_CHAIN: {
146 unsigned IntNo = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
147 switch (IntNo) {
148 case Intrinsic::bpf_load_byte:
149 case Intrinsic::bpf_load_half:
150 case Intrinsic::bpf_load_word: {
151 SDLoc DL(Node);
152 SDValue Chain = Node->getOperand(0);
153 SDValue N1 = Node->getOperand(1);
154 SDValue Skb = Node->getOperand(2);
155 SDValue N3 = Node->getOperand(3);
156
157 SDValue R6Reg = CurDAG->getRegister(BPF::R6, MVT::i64);
158 Chain = CurDAG->getCopyToReg(Chain, DL, R6Reg, Skb, SDValue());
159 Node = CurDAG->UpdateNodeOperands(Node, Chain, N1, R6Reg, N3);
160 break;
161 }
162 }
163 break;
164 }
165
166 case ISD::FrameIndex: {
Benjamin Kramer619c4e52015-04-10 11:24:51 +0000167 int FI = cast<FrameIndexSDNode>(Node)->getIndex();
Alexei Starovoitove4c8c802015-01-24 17:51:26 +0000168 EVT VT = Node->getValueType(0);
169 SDValue TFI = CurDAG->getTargetFrameIndex(FI, VT);
170 unsigned Opc = BPF::MOV_rr;
Justin Bognerf076e792016-05-12 21:14:47 +0000171 if (Node->hasOneUse()) {
172 CurDAG->SelectNodeTo(Node, Opc, VT, TFI);
173 return;
174 }
175 ReplaceNode(Node, CurDAG->getMachineNode(Opc, SDLoc(Node), VT, TFI));
176 return;
Alexei Starovoitove4c8c802015-01-24 17:51:26 +0000177 }
178 }
179
180 // Select the default instruction
Justin Bognerf076e792016-05-12 21:14:47 +0000181 SelectCode(Node);
Alexei Starovoitove4c8c802015-01-24 17:51:26 +0000182}
183
184FunctionPass *llvm::createBPFISelDag(BPFTargetMachine &TM) {
185 return new BPFDAGToDAGISel(TM);
186}