blob: 40ec91555e6e295febf4e8f30d3ba68bfd74cf91 [file] [log] [blame]
Dan Gohman10e730a2015-06-29 23:51:55 +00001//- WebAssemblyISelDAGToDAG.cpp - A dag to dag inst selector for WebAssembly -//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Dan Gohman10e730a2015-06-29 23:51:55 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000010/// This file defines an instruction selector for the WebAssembly target.
Dan Gohman10e730a2015-06-29 23:51:55 +000011///
12//===----------------------------------------------------------------------===//
13
Dan Gohman10e730a2015-06-29 23:51:55 +000014#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000015#include "WebAssembly.h"
Dan Gohman10e730a2015-06-29 23:51:55 +000016#include "WebAssemblyTargetMachine.h"
17#include "llvm/CodeGen/SelectionDAGISel.h"
18#include "llvm/IR/Function.h" // To access function attributes.
19#include "llvm/Support/Debug.h"
Craig Topper053cf4d2017-04-28 08:15:33 +000020#include "llvm/Support/KnownBits.h"
Dan Gohman10e730a2015-06-29 23:51:55 +000021#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:
Heejin Ahn18c56a02019-02-04 19:13:39 +000040 WebAssemblyDAGToDAGISel(WebAssemblyTargetMachine &TM,
Dan Gohman10e730a2015-06-29 23:51:55 +000041 CodeGenOpt::Level OptLevel)
Heejin Ahn18c56a02019-02-04 19:13:39 +000042 : SelectionDAGISel(TM, OptLevel), Subtarget(nullptr), ForCodeSize(false) {
Dan Gohman10e730a2015-06-29 23:51:55 +000043 }
44
Mehdi Amini117296c2016-10-01 02:56:57 +000045 StringRef getPassName() const override {
Dan Gohman10e730a2015-06-29 23:51:55 +000046 return "WebAssembly Instruction Selection";
47 }
48
49 bool runOnMachineFunction(MachineFunction &MF) override {
Heejin Ahn569f0902019-01-09 23:05:21 +000050 LLVM_DEBUG(dbgs() << "********** ISelDAGToDAG **********\n"
51 "********** Function: "
52 << MF.getName() << '\n');
53
David Blaikie21109242017-12-15 23:52:06 +000054 ForCodeSize = MF.getFunction().hasFnAttribute(Attribute::OptimizeForSize) ||
55 MF.getFunction().hasFnAttribute(Attribute::MinSize);
Dan Gohman10e730a2015-06-29 23:51:55 +000056 Subtarget = &MF.getSubtarget<WebAssemblySubtarget>();
57 return SelectionDAGISel::runOnMachineFunction(MF);
58 }
59
Justin Bognerc6afd4b2016-05-13 22:44:57 +000060 void Select(SDNode *Node) override;
Dan Gohman10e730a2015-06-29 23:51:55 +000061
Dan Gohmanf19ed562015-11-13 01:42:29 +000062 bool SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
63 std::vector<SDValue> &OutOps) override;
64
JF Bastienb9073fb2015-07-22 21:28:15 +000065// Include the pieces autogenerated from the target description.
66#include "WebAssemblyGenDAGISel.inc"
67
Dan Gohman10e730a2015-06-29 23:51:55 +000068private:
69 // add select functions here...
70};
71} // end anonymous namespace
72
Justin Bognerc6afd4b2016-05-13 22:44:57 +000073void WebAssemblyDAGToDAGISel::Select(SDNode *Node) {
JF Bastienb9073fb2015-07-22 21:28:15 +000074 // If we have a custom node, we already have selected!
75 if (Node->isMachineOpcode()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +000076 LLVM_DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
JF Bastienb9073fb2015-07-22 21:28:15 +000077 Node->setNodeId(-1);
Justin Bognerc6afd4b2016-05-13 22:44:57 +000078 return;
JF Bastienb9073fb2015-07-22 21:28:15 +000079 }
80
Heejin Ahna86152d2018-06-29 21:19:22 +000081 // Few custom selection stuff. If we need WebAssembly-specific selection,
82 // uncomment this block add corresponding case statements.
83 /*
JF Bastienb9073fb2015-07-22 21:28:15 +000084 switch (Node->getOpcode()) {
85 default:
86 break;
JF Bastienb9073fb2015-07-22 21:28:15 +000087 }
Heejin Ahna86152d2018-06-29 21:19:22 +000088 */
JF Bastienb9073fb2015-07-22 21:28:15 +000089
90 // Select the default instruction.
Justin Bognerc6afd4b2016-05-13 22:44:57 +000091 SelectCode(Node);
Dan Gohman10e730a2015-06-29 23:51:55 +000092}
93
Dan Gohmanf19ed562015-11-13 01:42:29 +000094bool WebAssemblyDAGToDAGISel::SelectInlineAsmMemoryOperand(
95 const SDValue &Op, unsigned ConstraintID, std::vector<SDValue> &OutOps) {
96 switch (ConstraintID) {
97 case InlineAsm::Constraint_i:
98 case InlineAsm::Constraint_m:
99 // We just support simple memory operands that just have a single address
100 // operand and need no special handling.
101 OutOps.push_back(Op);
102 return false;
103 default:
104 break;
105 }
106
107 return true;
108}
109
Dan Gohman10e730a2015-06-29 23:51:55 +0000110/// This pass converts a legalized DAG into a WebAssembly-specific DAG, ready
111/// for instruction scheduling.
112FunctionPass *llvm::createWebAssemblyISelDag(WebAssemblyTargetMachine &TM,
113 CodeGenOpt::Level OptLevel) {
114 return new WebAssemblyDAGToDAGISel(TM, OptLevel);
115}