Re-sort includes with sort-includes.py and insert raw_ostream.h where it's used.
[oota-llvm.git] / lib / Target / BPF / BPFISelDAGToDAG.cpp
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"
30 using namespace llvm;
31
32 #define DEBUG_TYPE "bpf-isel"
33
34 // Instruction Selector Implementation
35 namespace {
36
37 class BPFDAGToDAGISel : public SelectionDAGISel {
38 public:
39   explicit BPFDAGToDAGISel(BPFTargetMachine &TM) : SelectionDAGISel(TM) {}
40
41   const char *getPassName() const override {
42     return "BPF DAG->DAG Pattern Instruction Selection";
43   }
44
45 private:
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 };
54 }
55
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);
62     return true;
63   }
64
65   if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
66       Addr.getOpcode() == ISD::TargetGlobalAddress)
67     return false;
68
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())) {
73
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);
78       else
79         Base = Addr.getOperand(0);
80
81       Offset = CurDAG->getTargetConstant(CN->getSExtValue(), MVT::i64);
82       return true;
83     }
84   }
85
86   Base   = Addr;
87   Offset = CurDAG->getTargetConstant(0, MVT::i64);
88   return true;
89 }
90
91 SDNode *BPFDAGToDAGISel::Select(SDNode *Node) {
92   unsigned Opcode = Node->getOpcode();
93
94   // Dump information about the Node being selected
95   DEBUG(dbgs() << "Selecting: "; Node->dump(CurDAG); dbgs() << '\n');
96
97   // If we have a custom node, we already have selected!
98   if (Node->isMachineOpcode()) {
99     DEBUG(dbgs() << "== "; Node->dump(CurDAG); dbgs() << '\n');
100     return NULL;
101   }
102
103   // tablegen selection should be handled here.
104   switch (Opcode) {
105   default: break;
106
107   case ISD::UNDEF: {
108     errs() << "BUG: "; Node->dump(CurDAG); errs() << '\n';
109     report_fatal_error("shouldn't see UNDEF during Select");
110     break;
111   }
112
113   case ISD::INTRINSIC_W_CHAIN: {
114     unsigned IntNo = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
115     switch (IntNo) {
116     case Intrinsic::bpf_load_byte:
117     case Intrinsic::bpf_load_half:
118     case Intrinsic::bpf_load_word: {
119       SDLoc DL(Node);
120       SDValue Chain = Node->getOperand(0);
121       SDValue N1 = Node->getOperand(1);
122       SDValue Skb = Node->getOperand(2);
123       SDValue N3 = Node->getOperand(3);
124
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);
128       break;
129     }
130     }
131     break;
132   }
133
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);
142   }
143   }
144
145   // Select the default instruction
146   SDNode *ResNode = SelectCode(Node);
147
148   DEBUG(dbgs() << "=> ";
149         if (ResNode == nullptr || ResNode == Node)
150           Node->dump(CurDAG);
151         else
152           ResNode->dump(CurDAG);
153         dbgs() << '\n');
154   return ResNode;
155 }
156
157 FunctionPass *llvm::createBPFISelDag(BPFTargetMachine &TM) {
158   return new BPFDAGToDAGISel(TM);
159 }