WebAssemblyTargetMachine.cpp 13 KB

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