LLVM  3.7.0
WebAssemblyTargetMachine.cpp
Go to the documentation of this file.
1 //===- WebAssemblyTargetMachine.cpp - Define TargetMachine 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 the WebAssembly-specific subclass of TargetMachine.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #include "WebAssembly.h"
21 #include "llvm/CodeGen/Passes.h"
23 #include "llvm/IR/Function.h"
27 #include "llvm/Transforms/Scalar.h"
28 using namespace llvm;
29 
30 #define DEBUG_TYPE "wasm"
31 
33  // Register the target.
36 }
37 
38 //===----------------------------------------------------------------------===//
39 // WebAssembly Lowering public interface.
40 //===----------------------------------------------------------------------===//
41 
42 /// Create an WebAssembly architecture model.
43 ///
45  const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
46  const TargetOptions &Options, Reloc::Model RM, CodeModel::Model CM,
48  : LLVMTargetMachine(T, TT.isArch64Bit()
49  ? "e-p:64:64-i64:64-v128:8:128-n32:64-S128"
50  : "e-p:32:32-i64:64-v128:8:128-n32:64-S128",
51  TT, CPU, FS, Options, RM, CM, OL),
53  initAsmInfo();
54 
55  // We need a reducible CFG, so disable some optimizations which tend to
56  // introduce irreducibility.
58 }
59 
61 
64  Attribute CPUAttr = F.getFnAttribute("target-cpu");
65  Attribute FSAttr = F.getFnAttribute("target-features");
66 
67  std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
68  ? CPUAttr.getValueAsString().str()
69  : TargetCPU;
70  std::string FS = !FSAttr.hasAttribute(Attribute::None)
71  ? FSAttr.getValueAsString().str()
72  : TargetFS;
73 
74  auto &I = SubtargetMap[CPU + FS];
75  if (!I) {
76  // This needs to be done before we create a new subtarget since any
77  // creation will depend on the TM and the code generation flags on the
78  // function that reside in TargetOptions.
80  I = make_unique<WebAssemblySubtarget>(TargetTriple, CPU, FS, *this);
81  }
82  return I.get();
83 }
84 
85 namespace {
86 /// WebAssembly Code Generator Pass Configuration Options.
87 class WebAssemblyPassConfig final : public TargetPassConfig {
88 public:
89  WebAssemblyPassConfig(WebAssemblyTargetMachine *TM, PassManagerBase &PM)
90  : TargetPassConfig(TM, PM) {}
91 
92  WebAssemblyTargetMachine &getWebAssemblyTargetMachine() const {
93  return getTM<WebAssemblyTargetMachine>();
94  }
95 
96  FunctionPass *createTargetRegisterAllocator(bool) override;
97  void addFastRegAlloc(FunctionPass *RegAllocPass) override;
98  void addOptimizedRegAlloc(FunctionPass *RegAllocPass) override;
99 
100  void addIRPasses() override;
101  bool addPreISel() override;
102  bool addInstSelector() override;
103  bool addILPOpts() override;
104  void addPreRegAlloc() override;
105  void addRegAllocPasses(bool Optimized);
106  void addPostRegAlloc() override;
107  void addPreSched2() override;
108  void addPreEmitPass() override;
109 };
110 } // end anonymous namespace
111 
113  return TargetIRAnalysis([this](Function &F) {
114  return TargetTransformInfo(WebAssemblyTTIImpl(this, F));
115  });
116 }
117 
120  return new WebAssemblyPassConfig(this, PM);
121 }
122 
123 FunctionPass *WebAssemblyPassConfig::createTargetRegisterAllocator(bool) {
124  return nullptr; // No reg alloc
125 }
126 
127 void WebAssemblyPassConfig::addFastRegAlloc(FunctionPass *RegAllocPass) {
128  assert(!RegAllocPass && "WebAssembly uses no regalloc!");
129  addRegAllocPasses(false);
130 }
131 
132 void WebAssemblyPassConfig::addOptimizedRegAlloc(FunctionPass *RegAllocPass) {
133  assert(!RegAllocPass && "WebAssembly uses no regalloc!");
134  addRegAllocPasses(true);
135 }
136 
137 //===----------------------------------------------------------------------===//
138 // The following functions are called from lib/CodeGen/Passes.cpp to modify
139 // the CodeGen pass sequence.
140 //===----------------------------------------------------------------------===//
141 
142 void WebAssemblyPassConfig::addIRPasses() {
143  // FIXME: the default for this option is currently POSIX, whereas
144  // WebAssembly's MVP should default to Single.
145  if (TM->Options.ThreadModel == ThreadModel::Single)
146  addPass(createLowerAtomicPass());
147  else
148  // Expand some atomic operations. WebAssemblyTargetLowering has hooks which
149  // control specifically what gets lowered.
150  addPass(createAtomicExpandPass(TM));
151 
153 }
154 
155 bool WebAssemblyPassConfig::addPreISel() { return false; }
156 
157 bool WebAssemblyPassConfig::addInstSelector() {
158  addPass(
159  createWebAssemblyISelDag(getWebAssemblyTargetMachine(), getOptLevel()));
160  return false;
161 }
162 
163 bool WebAssemblyPassConfig::addILPOpts() { return true; }
164 
165 void WebAssemblyPassConfig::addPreRegAlloc() {}
166 
167 void WebAssemblyPassConfig::addRegAllocPasses(bool Optimized) {}
168 
169 void WebAssemblyPassConfig::addPostRegAlloc() {}
170 
171 void WebAssemblyPassConfig::addPreSched2() {}
172 
173 void WebAssemblyPassConfig::addPreEmitPass() {}
virtual void addIRPasses()
Add common target configurable passes that perform LLVM IR to IR transforms following machine indepen...
Definition: Passes.cpp:377
Triple TargetTriple
Triple string, CPU name, and target feature strings the TargetMachine instance is created with...
This file a TargetTransformInfo::Concept conforming object specific to the WebAssembly target machine...
Target TheWebAssemblyTarget64
Analysis pass providing the TargetTransformInfo.
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:188
TargetIRAnalysis getTargetIRAnalysis() override
Get the TargetIRAnalysis for this target.
Attribute getFnAttribute(Attribute::AttrKind Kind) const
Return the attribute for the given attribute kind.
Definition: Function.h:225
F(f)
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end...
bool hasAttribute(AttrKind Val) const
Return true if the attribute is present.
Definition: Attributes.cpp:147
Target TheWebAssemblyTarget32
This file declares the WebAssembly-specific subclass of TargetLoweringObjectFile. ...
WebAssemblyTargetMachine(const Target &T, const Triple &TT, StringRef CPU, StringRef FS, const TargetOptions &Options, Reloc::Model RM, CodeModel::Model CM, CodeGenOpt::Level OL)
Create an WebAssembly architecture model.
FunctionPass * createAtomicExpandPass(const TargetMachine *TM)
No attributes have been set.
Definition: Attributes.h:66
Target-Independent Code Generator Pass Configuration Options.
This file declares the WebAssembly-specific subclass of TargetMachine.
std::enable_if<!std::is_array< T >::value, std::unique_ptr< T > >::type make_unique(Args &&...args)
Constructs a new T() with the given args and returns a unique_ptr<T> which owns the object...
Definition: STLExtras.h:354
FunctionPass * createWebAssemblyISelDag(WebAssemblyTargetMachine &TM, CodeGenOpt::Level OptLevel)
This pass converts a legalized DAG into a WebAssembly-specific DAG, ready for instruction scheduling...
This file provides WebAssembly-specific target descriptions.
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
This class describes a target machine that is implemented with the LLVM target-independent code gener...
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:294
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
TargetPassConfig * createPassConfig(PassManagerBase &PM) override
Create a pass configuration object to be used by addPassToEmitX methods for generating a pipeline of ...
void setRequiresStructuredCFG(bool Value)
Pass * createLowerAtomicPass()
Target - Wrapper for Target specific information.
#define I(x, y, z)
Definition: MD5.cpp:54
void resetTargetOptions(const Function &F) const
Reset the target options based on the function's attributes.
void LLVMInitializeWebAssemblyTarget()
StringRef getValueAsString() const
Return the attribute's value as a string.
Definition: Attributes.cpp:140
RegisterTargetMachine - Helper template for registering a target machine implementation, for use in the target machine initialization function.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml","ocaml 3.10-compatible collector")
const WebAssemblySubtarget * getSubtargetImpl(const Function &F) const override
Virtual method implemented by subclasses that returns a reference to that target's TargetSubtargetInf...