blob: fdf3a30a5c0e2d9c9c7b84ccb9428d0899ecf81c [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
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000011/// This file defines an instruction selector for the WebAssembly target.
Dan Gohman10e730a2015-06-29 23:51:55 +000012///
13//===----------------------------------------------------------------------===//
14
Dan Gohman10e730a2015-06-29 23:51:55 +000015#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000016#include "WebAssembly.h"
Dan Gohman10e730a2015-06-29 23:51:55 +000017#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"
Craig Topper053cf4d2017-04-28 08:15:33 +000021#include "llvm/Support/KnownBits.h"
Dan Gohman10e730a2015-06-29 23:51:55 +000022#include "llvm/Support/MathExtras.h"
23#include "llvm/Support/raw_ostream.h"
24using namespace llvm;
25
26#define DEBUG_TYPE "wasm-isel"
27
28//===--------------------------------------------------------------------===//
29/// WebAssembly-specific code to select WebAssembly machine instructions for
30/// SelectionDAG operations.
31///
32namespace {
33class WebAssemblyDAGToDAGISel final : public SelectionDAGISel {
34 /// Keep a pointer to the WebAssemblySubtarget around so that we can make the
35 /// right decision when generating code for different targets.
36 const WebAssemblySubtarget *Subtarget;
37
38 bool ForCodeSize;
39
40public:
41 WebAssemblyDAGToDAGISel(WebAssemblyTargetMachine &tm,
42 CodeGenOpt::Level OptLevel)
43 : SelectionDAGISel(tm, OptLevel), Subtarget(nullptr), ForCodeSize(false) {
44 }
45
Mehdi Amini117296c2016-10-01 02:56:57 +000046 StringRef getPassName() const override {
Dan Gohman10e730a2015-06-29 23:51:55 +000047 return "WebAssembly Instruction Selection";
48 }
49
50 bool runOnMachineFunction(MachineFunction &MF) override {
David Blaikie21109242017-12-15 23:52:06 +000051 ForCodeSize = MF.getFunction().hasFnAttribute(Attribute::OptimizeForSize) ||
52 MF.getFunction().hasFnAttribute(Attribute::MinSize);
Dan Gohman10e730a2015-06-29 23:51:55 +000053 Subtarget = &MF.getSubtarget<WebAssemblySubtarget>();
54 return SelectionDAGISel::runOnMachineFunction(MF);
55 }
56
Justin Bognerc6afd4b2016-05-13 22:44:57 +000057 void Select(SDNode *Node) override;
Dan Gohman10e730a2015-06-29 23:51:55 +000058
Dan Gohmanf19ed562015-11-13 01:42:29 +000059 bool SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
60 std::vector<SDValue> &OutOps) override;
61
JF Bastienb9073fb2015-07-22 21:28:15 +000062// Include the pieces autogenerated from the target description.
63#include "WebAssemblyGenDAGISel.inc"
64
Dan Gohman10e730a2015-06-29 23:51:55 +000065private:
66 // add select functions here...
67};
68} // end anonymous namespace
69
Justin Bognerc6afd4b2016-05-13 22:44:57 +000070void WebAssemblyDAGToDAGISel::Select(SDNode *Node) {
JF Bastienb9073fb2015-07-22 21:28:15 +000071 // If we have a custom node, we already have selected!
72 if (Node->isMachineOpcode()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +000073 LLVM_DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
JF Bastienb9073fb2015-07-22 21:28:15 +000074 Node->setNodeId(-1);
Justin Bognerc6afd4b2016-05-13 22:44:57 +000075 return;
JF Bastienb9073fb2015-07-22 21:28:15 +000076 }
77
Heejin Ahna86152d2018-06-29 21:19:22 +000078 // Few custom selection stuff. If we need WebAssembly-specific selection,
79 // uncomment this block add corresponding case statements.
80 /*
JF Bastienb9073fb2015-07-22 21:28:15 +000081 switch (Node->getOpcode()) {
82 default:
83 break;
JF Bastienb9073fb2015-07-22 21:28:15 +000084 }
Heejin Ahna86152d2018-06-29 21:19:22 +000085 */
JF Bastienb9073fb2015-07-22 21:28:15 +000086
87 // Select the default instruction.
Justin Bognerc6afd4b2016-05-13 22:44:57 +000088 SelectCode(Node);
Dan Gohman10e730a2015-06-29 23:51:55 +000089}
90
Dan Gohmanf19ed562015-11-13 01:42:29 +000091bool WebAssemblyDAGToDAGISel::SelectInlineAsmMemoryOperand(
92 const SDValue &Op, unsigned ConstraintID, std::vector<SDValue> &OutOps) {
93 switch (ConstraintID) {
94 case InlineAsm::Constraint_i:
95 case InlineAsm::Constraint_m:
96 // We just support simple memory operands that just have a single address
97 // operand and need no special handling.
98 OutOps.push_back(Op);
99 return false;
100 default:
101 break;
102 }
103
104 return true;
105}
106
Dan Gohman10e730a2015-06-29 23:51:55 +0000107/// This pass converts a legalized DAG into a WebAssembly-specific DAG, ready
108/// for instruction scheduling.
109FunctionPass *llvm::createWebAssemblyISelDag(WebAssemblyTargetMachine &TM,
110 CodeGenOpt::Level OptLevel) {
111 return new WebAssemblyDAGToDAGISel(TM, OptLevel);
112}