1 //- WebAssemblyISelDAGToDAG.cpp - A dag to dag inst selector for WebAssembly -//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// This file defines an instruction selector for the WebAssembly target.
13 //===----------------------------------------------------------------------===//
15 #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
16 #include "WebAssembly.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/KnownBits.h"
22 #include "llvm/Support/MathExtras.h"
23 #include "llvm/Support/raw_ostream.h"
26 #define DEBUG_TYPE "wasm-isel"
28 //===--------------------------------------------------------------------===//
29 /// WebAssembly-specific code to select WebAssembly machine instructions for
30 /// SelectionDAG operations.
33 class 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;
41 WebAssemblyDAGToDAGISel(WebAssemblyTargetMachine &tm,
42 CodeGenOpt::Level OptLevel)
43 : SelectionDAGISel(tm, OptLevel), Subtarget(nullptr), ForCodeSize(false) {
46 StringRef getPassName() const override {
47 return "WebAssembly Instruction Selection";
50 bool runOnMachineFunction(MachineFunction &MF) override {
51 LLVM_DEBUG(dbgs() << "********** ISelDAGToDAG **********\n"
52 "********** Function: "
53 << MF.getName() << '\n');
55 ForCodeSize = MF.getFunction().hasFnAttribute(Attribute::OptimizeForSize) ||
56 MF.getFunction().hasFnAttribute(Attribute::MinSize);
57 Subtarget = &MF.getSubtarget<WebAssemblySubtarget>();
58 return SelectionDAGISel::runOnMachineFunction(MF);
61 void Select(SDNode *Node) override;
63 bool SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
64 std::vector<SDValue> &OutOps) override;
66 // Include the pieces autogenerated from the target description.
67 #include "WebAssemblyGenDAGISel.inc"
70 // add select functions here...
72 } // end anonymous namespace
74 void WebAssemblyDAGToDAGISel::Select(SDNode *Node) {
75 // If we have a custom node, we already have selected!
76 if (Node->isMachineOpcode()) {
77 LLVM_DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
82 // Few custom selection stuff. If we need WebAssembly-specific selection,
83 // uncomment this block add corresponding case statements.
85 switch (Node->getOpcode()) {
91 // Select the default instruction.
95 bool WebAssemblyDAGToDAGISel::SelectInlineAsmMemoryOperand(
96 const SDValue &Op, unsigned ConstraintID, std::vector<SDValue> &OutOps) {
97 switch (ConstraintID) {
98 case InlineAsm::Constraint_i:
99 case InlineAsm::Constraint_m:
100 // We just support simple memory operands that just have a single address
101 // operand and need no special handling.
102 OutOps.push_back(Op);
111 /// This pass converts a legalized DAG into a WebAssembly-specific DAG, ready
112 /// for instruction scheduling.
113 FunctionPass *llvm::createWebAssemblyISelDag(WebAssemblyTargetMachine &TM,
114 CodeGenOpt::Level OptLevel) {
115 return new WebAssemblyDAGToDAGISel(TM, OptLevel);