blob: 58a23756bdc1a197ad4c6098d961dab362ca0bf2 [file] [log] [blame]
Dan Gohman10e730a2015-06-29 23:51:55 +00001//- WebAssemblyISelDAGToDAG.cpp - A dag to dag inst selector for WebAssembly -//
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/// \file
11/// \brief This file defines an instruction selector for the WebAssembly target.
12///
13//===----------------------------------------------------------------------===//
14
15#include "WebAssembly.h"
16#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
17#include "WebAssemblyTargetMachine.h"
18#include "llvm/CodeGen/SelectionDAGISel.h"
19#include "llvm/IR/Function.h" // To access function attributes.
20#include "llvm/Support/Debug.h"
21#include "llvm/Support/MathExtras.h"
22#include "llvm/Support/raw_ostream.h"
23using namespace llvm;
24
25#define DEBUG_TYPE "wasm-isel"
26
27//===--------------------------------------------------------------------===//
28/// WebAssembly-specific code to select WebAssembly machine instructions for
29/// SelectionDAG operations.
30///
31namespace {
32class WebAssemblyDAGToDAGISel final : public SelectionDAGISel {
33 /// Keep a pointer to the WebAssemblySubtarget around so that we can make the
34 /// right decision when generating code for different targets.
35 const WebAssemblySubtarget *Subtarget;
36
37 bool ForCodeSize;
38
39public:
40 WebAssemblyDAGToDAGISel(WebAssemblyTargetMachine &tm,
41 CodeGenOpt::Level OptLevel)
42 : SelectionDAGISel(tm, OptLevel), Subtarget(nullptr), ForCodeSize(false) {
43 }
44
45 const char *getPassName() const override {
46 return "WebAssembly Instruction Selection";
47 }
48
49 bool runOnMachineFunction(MachineFunction &MF) override {
50 ForCodeSize =
51 MF.getFunction()->hasFnAttribute(Attribute::OptimizeForSize) ||
52 MF.getFunction()->hasFnAttribute(Attribute::MinSize);
53 Subtarget = &MF.getSubtarget<WebAssemblySubtarget>();
54 return SelectionDAGISel::runOnMachineFunction(MF);
55 }
56
57 SDNode *Select(SDNode *Node) override;
58
JF Bastienb9073fb2015-07-22 21:28:15 +000059// Include the pieces autogenerated from the target description.
60#include "WebAssemblyGenDAGISel.inc"
61
Dan Gohman10e730a2015-06-29 23:51:55 +000062private:
63 // add select functions here...
64};
65} // end anonymous namespace
66
67SDNode *WebAssemblyDAGToDAGISel::Select(SDNode *Node) {
JF Bastienb9073fb2015-07-22 21:28:15 +000068 // Dump information about the Node being selected.
69 DEBUG(errs() << "Selecting: ");
70 DEBUG(Node->dump(CurDAG));
71 DEBUG(errs() << "\n");
72
73 // If we have a custom node, we already have selected!
74 if (Node->isMachineOpcode()) {
75 DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
76 Node->setNodeId(-1);
77 return nullptr;
78 }
79
80 // Few custom selection stuff.
81 SDNode *ResNode = nullptr;
82 EVT VT = Node->getValueType(0);
83
84 switch (Node->getOpcode()) {
85 default:
86 break;
87 // FIXME: Implement WebAssembly-specific selection.
88 (void)VT;
89 }
90
91 // Select the default instruction.
92 ResNode = SelectCode(Node);
93
94 DEBUG(errs() << "=> ");
95 if (ResNode == nullptr || ResNode == Node)
96 DEBUG(Node->dump(CurDAG));
97 else
98 DEBUG(ResNode->dump(CurDAG));
99 DEBUG(errs() << "\n");
100
101 return ResNode;
Dan Gohman10e730a2015-06-29 23:51:55 +0000102}
103
104/// This pass converts a legalized DAG into a WebAssembly-specific DAG, ready
105/// for instruction scheduling.
106FunctionPass *llvm::createWebAssemblyISelDag(WebAssemblyTargetMachine &TM,
107 CodeGenOpt::Level OptLevel) {
108 return new WebAssemblyDAGToDAGISel(TM, OptLevel);
109}