WebAssemblyTargetMachine.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. #include "WebAssembly.h"
  15. #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
  16. #include "WebAssemblyTargetMachine.h"
  17. #include "WebAssemblyTargetObjectFile.h"
  18. #include "WebAssemblyTargetTransformInfo.h"
  19. #include "llvm/CodeGen/MachineFunctionPass.h"
  20. #include "llvm/CodeGen/Passes.h"
  21. #include "llvm/CodeGen/RegAllocRegistry.h"
  22. #include "llvm/CodeGen/TargetPassConfig.h"
  23. #include "llvm/IR/Function.h"
  24. #include "llvm/Support/TargetRegistry.h"
  25. #include "llvm/Target/TargetOptions.h"
  26. #include "llvm/Transforms/Scalar.h"
  27. using namespace llvm;
  28. #define DEBUG_TYPE "wasm"
  29. // Emscripten's asm.js-style exception handling
  30. static cl::opt<bool> EnableEmException(
  31. "enable-emscripten-cxx-exceptions",
  32. cl::desc("WebAssembly Emscripten-style exception handling"),
  33. cl::init(false));
  34. // Emscripten's asm.js-style setjmp/longjmp handling
  35. static cl::opt<bool> EnableEmSjLj(
  36. "enable-emscripten-sjlj",
  37. cl::desc("WebAssembly Emscripten-style setjmp/longjmp handling"),
  38. cl::init(false));
  39. extern "C" void LLVMInitializeWebAssemblyTarget() {
  40. // Register the target.
  41. RegisterTargetMachine<WebAssemblyTargetMachine> X(
  42. getTheWebAssemblyTarget32());
  43. RegisterTargetMachine<WebAssemblyTargetMachine> Y(
  44. getTheWebAssemblyTarget64());
  45. // Register exception handling pass to opt
  46. initializeWebAssemblyLowerEmscriptenEHSjLjPass(
  47. *PassRegistry::getPassRegistry());
  48. }
  49. //===----------------------------------------------------------------------===//
  50. // WebAssembly Lowering public interface.
  51. //===----------------------------------------------------------------------===//
  52. static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM) {
  53. if (!RM.hasValue())
  54. return Reloc::PIC_;
  55. return *RM;
  56. }
  57. /// Create an WebAssembly architecture model.
  58. ///
  59. WebAssemblyTargetMachine::WebAssemblyTargetMachine(
  60. const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
  61. const TargetOptions &Options, Optional<Reloc::Model> RM,
  62. CodeModel::Model CM, CodeGenOpt::Level OL)
  63. : LLVMTargetMachine(T,
  64. TT.isArch64Bit() ? "e-m:e-p:64:64-i64:64-n32:64-S128"
  65. : "e-m:e-p:32:32-i64:64-n32:64-S128",
  66. TT, CPU, FS, Options, getEffectiveRelocModel(RM),
  67. CM, OL),
  68. TLOF(TT.isOSBinFormatELF() ?
  69. static_cast<TargetLoweringObjectFile*>(
  70. new WebAssemblyTargetObjectFileELF()) :
  71. static_cast<TargetLoweringObjectFile*>(
  72. new WebAssemblyTargetObjectFile())) {
  73. // WebAssembly type-checks instructions, but a noreturn function with a return
  74. // type that doesn't match the context will cause a check failure. So we lower
  75. // LLVM 'unreachable' to ISD::TRAP and then lower that to WebAssembly's
  76. // 'unreachable' instructions which is meant for that case.
  77. this->Options.TrapUnreachable = true;
  78. // WebAssembly treats each function as an independent unit. Force
  79. // -ffunction-sections, effectively, so that we can emit them independently.
  80. if (!TT.isOSBinFormatELF()) {
  81. this->Options.FunctionSections = true;
  82. this->Options.DataSections = true;
  83. this->Options.UniqueSectionNames = true;
  84. }
  85. initAsmInfo();
  86. // Note that we don't use setRequiresStructuredCFG(true). It disables
  87. // optimizations than we're ok with, and want, such as critical edge
  88. // splitting and tail merging.
  89. }
  90. WebAssemblyTargetMachine::~WebAssemblyTargetMachine() {}
  91. const WebAssemblySubtarget *
  92. WebAssemblyTargetMachine::getSubtargetImpl(const Function &F) const {
  93. Attribute CPUAttr = F.getFnAttribute("target-cpu");
  94. Attribute FSAttr = F.getFnAttribute("target-features");
  95. std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
  96. ? CPUAttr.getValueAsString().str()
  97. : TargetCPU;
  98. std::string FS = !FSAttr.hasAttribute(Attribute::None)
  99. ? FSAttr.getValueAsString().str()
  100. : TargetFS;
  101. auto &I = SubtargetMap[CPU + FS];
  102. if (!I) {
  103. // This needs to be done before we create a new subtarget since any
  104. // creation will depend on the TM and the code generation flags on the
  105. // function that reside in TargetOptions.
  106. resetTargetOptions(F);
  107. I = llvm::make_unique<WebAssemblySubtarget>(TargetTriple, CPU, FS, *this);
  108. }
  109. return I.get();
  110. }
  111. namespace {
  112. /// WebAssembly Code Generator Pass Configuration Options.
  113. class WebAssemblyPassConfig final : public TargetPassConfig {
  114. public:
  115. WebAssemblyPassConfig(WebAssemblyTargetMachine *TM, PassManagerBase &PM)
  116. : TargetPassConfig(TM, PM) {}
  117. WebAssemblyTargetMachine &getWebAssemblyTargetMachine() const {
  118. return getTM<WebAssemblyTargetMachine>();
  119. }
  120. FunctionPass *createTargetRegisterAllocator(bool) override;
  121. void addIRPasses() override;
  122. bool addInstSelector() override;
  123. void addPostRegAlloc() override;
  124. bool addGCPasses() override { return false; }
  125. void addPreEmitPass() override;
  126. };
  127. } // end anonymous namespace
  128. TargetIRAnalysis WebAssemblyTargetMachine::getTargetIRAnalysis() {
  129. return TargetIRAnalysis([this](const Function &F) {
  130. return TargetTransformInfo(WebAssemblyTTIImpl(this, F));
  131. });
  132. }
  133. TargetPassConfig *
  134. WebAssemblyTargetMachine::createPassConfig(PassManagerBase &PM) {
  135. return new WebAssemblyPassConfig(this, PM);
  136. }
  137. FunctionPass *WebAssemblyPassConfig::createTargetRegisterAllocator(bool) {
  138. return nullptr; // No reg alloc
  139. }
  140. //===----------------------------------------------------------------------===//
  141. // The following functions are called from lib/CodeGen/Passes.cpp to modify
  142. // the CodeGen pass sequence.
  143. //===----------------------------------------------------------------------===//
  144. void WebAssemblyPassConfig::addIRPasses() {
  145. if (TM->Options.ThreadModel == ThreadModel::Single)
  146. // In "single" mode, atomics get lowered to non-atomics.
  147. addPass(createLowerAtomicPass());
  148. else
  149. // Expand some atomic operations. WebAssemblyTargetLowering has hooks which
  150. // control specifically what gets lowered.
  151. addPass(createAtomicExpandPass());
  152. // Fix function bitcasts, as WebAssembly requires caller and callee signatures
  153. // to match.
  154. addPass(createWebAssemblyFixFunctionBitcasts());
  155. // Optimize "returned" function attributes.
  156. if (getOptLevel() != CodeGenOpt::None)
  157. addPass(createWebAssemblyOptimizeReturned());
  158. // If exception handling is not enabled and setjmp/longjmp handling is
  159. // enabled, we lower invokes into calls and delete unreachable landingpad
  160. // blocks. Lowering invokes when there is no EH support is done in
  161. // TargetPassConfig::addPassesToHandleExceptions, but this runs after this
  162. // function and SjLj handling expects all invokes to be lowered before.
  163. if (!EnableEmException) {
  164. addPass(createLowerInvokePass());
  165. // The lower invoke pass may create unreachable code. Remove it in order not
  166. // to process dead blocks in setjmp/longjmp handling.
  167. addPass(createUnreachableBlockEliminationPass());
  168. }
  169. // Handle exceptions and setjmp/longjmp if enabled.
  170. if (EnableEmException || EnableEmSjLj)
  171. addPass(createWebAssemblyLowerEmscriptenEHSjLj(EnableEmException,
  172. EnableEmSjLj));
  173. TargetPassConfig::addIRPasses();
  174. }
  175. bool WebAssemblyPassConfig::addInstSelector() {
  176. (void)TargetPassConfig::addInstSelector();
  177. addPass(
  178. createWebAssemblyISelDag(getWebAssemblyTargetMachine(), getOptLevel()));
  179. // Run the argument-move pass immediately after the ScheduleDAG scheduler
  180. // so that we can fix up the ARGUMENT instructions before anything else
  181. // sees them in the wrong place.
  182. addPass(createWebAssemblyArgumentMove());
  183. // Set the p2align operands. This information is present during ISel, however
  184. // it's inconvenient to collect. Collect it now, and update the immediate
  185. // operands.
  186. addPass(createWebAssemblySetP2AlignOperands());
  187. return false;
  188. }
  189. void WebAssemblyPassConfig::addPostRegAlloc() {
  190. // TODO: The following CodeGen passes don't currently support code containing
  191. // virtual registers. Consider removing their restrictions and re-enabling
  192. // them.
  193. // Has no asserts of its own, but was not written to handle virtual regs.
  194. disablePass(&ShrinkWrapID);
  195. // These functions all require the NoVRegs property.
  196. disablePass(&MachineCopyPropagationID);
  197. disablePass(&PostRASchedulerID);
  198. disablePass(&FuncletLayoutID);
  199. disablePass(&StackMapLivenessID);
  200. disablePass(&LiveDebugValuesID);
  201. disablePass(&PatchableFunctionID);
  202. TargetPassConfig::addPostRegAlloc();
  203. }
  204. void WebAssemblyPassConfig::addPreEmitPass() {
  205. TargetPassConfig::addPreEmitPass();
  206. // Now that we have a prologue and epilogue and all frame indices are
  207. // rewritten, eliminate SP and FP. This allows them to be stackified,
  208. // colored, and numbered with the rest of the registers.
  209. addPass(createWebAssemblyReplacePhysRegs());
  210. // Rewrite pseudo call_indirect instructions as real instructions.
  211. // This needs to run before register stackification, because we change the
  212. // order of the arguments.
  213. addPass(createWebAssemblyCallIndirectFixup());
  214. if (getOptLevel() != CodeGenOpt::None) {
  215. // LiveIntervals isn't commonly run this late. Re-establish preconditions.
  216. addPass(createWebAssemblyPrepareForLiveIntervals());
  217. // Depend on LiveIntervals and perform some optimizations on it.
  218. addPass(createWebAssemblyOptimizeLiveIntervals());
  219. // Prepare store instructions for register stackifying.
  220. addPass(createWebAssemblyStoreResults());
  221. // Mark registers as representing wasm's value stack. This is a key
  222. // code-compression technique in WebAssembly. We run this pass (and
  223. // StoreResults above) very late, so that it sees as much code as possible,
  224. // including code emitted by PEI and expanded by late tail duplication.
  225. addPass(createWebAssemblyRegStackify());
  226. // Run the register coloring pass to reduce the total number of registers.
  227. // This runs after stackification so that it doesn't consider registers
  228. // that become stackified.
  229. addPass(createWebAssemblyRegColoring());
  230. }
  231. // Eliminate multiple-entry loops. Do this before inserting explicit get_local
  232. // and set_local operators because we create a new variable that we want
  233. // converted into a local.
  234. addPass(createWebAssemblyFixIrreducibleControlFlow());
  235. // Insert explicit get_local and set_local operators.
  236. addPass(createWebAssemblyExplicitLocals());
  237. // Sort the blocks of the CFG into topological order, a prerequisite for
  238. // BLOCK and LOOP markers.
  239. addPass(createWebAssemblyCFGSort());
  240. // Insert BLOCK and LOOP markers.
  241. addPass(createWebAssemblyCFGStackify());
  242. // Lower br_unless into br_if.
  243. addPass(createWebAssemblyLowerBrUnless());
  244. // Perform the very last peephole optimizations on the code.
  245. if (getOptLevel() != CodeGenOpt::None)
  246. addPass(createWebAssemblyPeephole());
  247. // Create a mapping from LLVM CodeGen virtual registers to wasm registers.
  248. addPass(createWebAssemblyRegNumbering());
  249. }