1// WebAssemblyTargetMachine.h - Define TargetMachine for WebAssembly -*- C++ -*-
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file declares the WebAssembly-specific subclass of
11/// TargetMachine.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_LIB_TARGET_WEBASSEMBLY_WEBASSEMBLYTARGETMACHINE_H
16#define LLVM_LIB_TARGET_WEBASSEMBLY_WEBASSEMBLYTARGETMACHINE_H
17
18#include "WebAssemblySubtarget.h"
19#include "llvm/CodeGen/CodeGenTargetMachineImpl.h"
20#include "llvm/Target/LastSubtargetCache.h"
21#include <optional>
22
23namespace llvm {
24
25namespace WebAssembly {
26// Exception handling / setjmp-longjmp handling command-line options
27extern cl::opt<bool> WasmDisableExplicitLocals;
28extern cl::opt<bool> WasmEnableEmSjLj; // asm.js-style SjLJ
29extern cl::opt<bool> WasmEnableSjLj; // SjLj using Wasm EH instructions
30extern cl::opt<bool> WasmUseLegacyEH; // Legacy Wasm EH
31} // namespace WebAssembly
32
33class WebAssemblyTargetMachine final : public CodeGenTargetMachineImpl {
34 std::unique_ptr<TargetLoweringObjectFile> TLOF;
35 mutable StringMap<std::unique_ptr<WebAssemblySubtarget>> SubtargetMap;
36
37 /// The attributes that select a subtarget.
38 struct SubtargetAttrs {
39 Attribute CPU, FS;
40 StringRef ABIName;
41
42 bool operator==(const SubtargetAttrs &RHS) const {
43 return CPU == RHS.CPU && FS == RHS.FS && ABIName == RHS.ABIName;
44 }
45
46 bool operator!=(const SubtargetAttrs &RHS) const { return !(*this == RHS); }
47 };
48 mutable LastSubtargetCache<WebAssemblySubtarget, SubtargetAttrs>
49 LastSubtarget;
50
51public:
52 WebAssemblyTargetMachine(const Target &T, const Triple &TT, StringRef CPU,
53 StringRef FS, const TargetOptions &Options,
54 std::optional<Reloc::Model> RM,
55 std::optional<CodeModel::Model> CM,
56 CodeGenOptLevel OL, bool JIT);
57
58 ~WebAssemblyTargetMachine() override;
59
60 const WebAssemblySubtarget *getSubtargetImpl(StringRef CPU, StringRef FS,
61 StringRef ABIName) const;
62 const WebAssemblySubtarget *
63 getSubtargetImpl(const Function &F) const override;
64
65 // Pass Pipeline Configuration
66 TargetPassConfig *createPassConfig(PassManagerBase &PM) override;
67
68 TargetLoweringObjectFile *getObjFileLowering() const override {
69 return TLOF.get();
70 }
71
72 MachineFunctionInfo *
73 createMachineFunctionInfo(BumpPtrAllocator &Allocator, const Function &F,
74 const TargetSubtargetInfo *STI) const override;
75
76 TargetTransformInfo getTargetTransformInfo(const Function &F) const override;
77
78 bool usesPhysRegsForValues() const override { return false; }
79
80 yaml::MachineFunctionInfo *createDefaultFuncInfoYAML() const override;
81 yaml::MachineFunctionInfo *
82 convertFuncInfoToYAML(const MachineFunction &MF) const override;
83 bool parseMachineFunctionInfo(const yaml::MachineFunctionInfo &,
84 PerFunctionMIParsingState &PFS,
85 SMDiagnostic &Error,
86 SMRange &SourceRange) const override;
87
88 void registerPassBuilderCallbacks(PassBuilder &PbB) override;
89
90 Error buildCodeGenPipeline(ModulePassManager &MPM, ModuleAnalysisManager &MAM,
91 raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut,
92 CodeGenFileType FileType,
93 const CGPassBuilderOption &Opt, MCContext &Ctx,
94 PassInstrumentationCallbacks *PIC) override;
95};
96
97} // end namespace llvm
98
99#endif
100