Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 1 | //===- WebAssemblyTargetMachine.cpp - Define TargetMachine for WebAssembly -==// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// |
| 9 | /// \file |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 10 | /// This file defines the WebAssembly-specific subclass of TargetMachine. |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 11 | /// |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 14 | #include "WebAssemblyTargetMachine.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 15 | #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" |
Richard Trieu | c6c4213 | 2019-05-15 01:03:00 +0000 | [diff] [blame] | 16 | #include "TargetInfo/WebAssemblyTargetInfo.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 17 | #include "WebAssembly.h" |
Heejin Ahn | 52221d5 | 2019-03-26 17:35:35 +0000 | [diff] [blame] | 18 | #include "WebAssemblyMachineFunctionInfo.h" |
Dan Gohman | 5bf22fc | 2015-12-17 04:55:44 +0000 | [diff] [blame] | 19 | #include "WebAssemblyTargetObjectFile.h" |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 20 | #include "WebAssemblyTargetTransformInfo.h" |
Heejin Ahn | 52221d5 | 2019-03-26 17:35:35 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MIRParser/MIParser.h" |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 23 | #include "llvm/CodeGen/Passes.h" |
| 24 | #include "llvm/CodeGen/RegAllocRegistry.h" |
Matthias Braun | 31d19d4 | 2016-05-10 03:21:59 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/TargetPassConfig.h" |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 26 | #include "llvm/IR/Function.h" |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 27 | #include "llvm/Support/TargetRegistry.h" |
| 28 | #include "llvm/Target/TargetOptions.h" |
JF Bastien | 03855df | 2015-07-01 23:41:25 +0000 | [diff] [blame] | 29 | #include "llvm/Transforms/Scalar.h" |
Thomas Lively | 3f34e1b8 | 2019-03-29 00:14:01 +0000 | [diff] [blame] | 30 | #include "llvm/Transforms/Scalar/LowerAtomic.h" |
David Blaikie | a373d18 | 2018-03-28 17:44:36 +0000 | [diff] [blame] | 31 | #include "llvm/Transforms/Utils.h" |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 32 | using namespace llvm; |
| 33 | |
| 34 | #define DEBUG_TYPE "wasm" |
| 35 | |
Derek Schuff | f41f67d | 2016-08-01 21:34:04 +0000 | [diff] [blame] | 36 | // Emscripten's asm.js-style exception handling |
Derek Schuff | ccdceda | 2016-08-18 15:27:25 +0000 | [diff] [blame] | 37 | static cl::opt<bool> EnableEmException( |
Derek Schuff | 53b9af0 | 2016-08-09 00:29:55 +0000 | [diff] [blame] | 38 | "enable-emscripten-cxx-exceptions", |
Derek Schuff | f41f67d | 2016-08-01 21:34:04 +0000 | [diff] [blame] | 39 | cl::desc("WebAssembly Emscripten-style exception handling"), |
| 40 | cl::init(false)); |
| 41 | |
Derek Schuff | ccdceda | 2016-08-18 15:27:25 +0000 | [diff] [blame] | 42 | // Emscripten's asm.js-style setjmp/longjmp handling |
| 43 | static cl::opt<bool> EnableEmSjLj( |
| 44 | "enable-emscripten-sjlj", |
| 45 | cl::desc("WebAssembly Emscripten-style setjmp/longjmp handling"), |
| 46 | cl::init(false)); |
| 47 | |
Tom Stellard | 4b0b261 | 2019-06-11 03:21:13 +0000 | [diff] [blame] | 48 | extern "C" void LLVMInitializeWebAssemblyTarget() { |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 49 | // Register the target. |
Mehdi Amini | f42454b | 2016-10-09 23:00:34 +0000 | [diff] [blame] | 50 | RegisterTargetMachine<WebAssemblyTargetMachine> X( |
| 51 | getTheWebAssemblyTarget32()); |
| 52 | RegisterTargetMachine<WebAssemblyTargetMachine> Y( |
| 53 | getTheWebAssemblyTarget64()); |
Derek Schuff | f41f67d | 2016-08-01 21:34:04 +0000 | [diff] [blame] | 54 | |
Jacob Gravelle | 4092645 | 2018-03-30 20:36:58 +0000 | [diff] [blame] | 55 | // Register backend passes |
| 56 | auto &PR = *PassRegistry::getPassRegistry(); |
Sam Clegg | 9261755 | 2018-07-11 04:29:36 +0000 | [diff] [blame] | 57 | initializeWebAssemblyAddMissingPrototypesPass(PR); |
Jacob Gravelle | 4092645 | 2018-03-30 20:36:58 +0000 | [diff] [blame] | 58 | initializeWebAssemblyLowerEmscriptenEHSjLjPass(PR); |
| 59 | initializeLowerGlobalDtorsPass(PR); |
| 60 | initializeFixFunctionBitcastsPass(PR); |
| 61 | initializeOptimizeReturnedPass(PR); |
| 62 | initializeWebAssemblyArgumentMovePass(PR); |
| 63 | initializeWebAssemblySetP2AlignOperandsPass(PR); |
| 64 | initializeWebAssemblyReplacePhysRegsPass(PR); |
| 65 | initializeWebAssemblyPrepareForLiveIntervalsPass(PR); |
| 66 | initializeWebAssemblyOptimizeLiveIntervalsPass(PR); |
Heejin Ahn | 321d522 | 2019-01-08 22:35:18 +0000 | [diff] [blame] | 67 | initializeWebAssemblyMemIntrinsicResultsPass(PR); |
Jacob Gravelle | 4092645 | 2018-03-30 20:36:58 +0000 | [diff] [blame] | 68 | initializeWebAssemblyRegStackifyPass(PR); |
| 69 | initializeWebAssemblyRegColoringPass(PR); |
Jacob Gravelle | 4092645 | 2018-03-30 20:36:58 +0000 | [diff] [blame] | 70 | initializeWebAssemblyFixIrreducibleControlFlowPass(PR); |
Heejin Ahn | 4934f76 | 2018-06-25 01:07:11 +0000 | [diff] [blame] | 71 | initializeWebAssemblyLateEHPreparePass(PR); |
Heejin Ahn | 04c4894 | 2018-06-25 01:20:21 +0000 | [diff] [blame] | 72 | initializeWebAssemblyExceptionInfoPass(PR); |
Jacob Gravelle | 4092645 | 2018-03-30 20:36:58 +0000 | [diff] [blame] | 73 | initializeWebAssemblyCFGSortPass(PR); |
| 74 | initializeWebAssemblyCFGStackifyPass(PR); |
Heejin Ahn | e9fd907 | 2019-03-30 09:29:57 +0000 | [diff] [blame] | 75 | initializeWebAssemblyExplicitLocalsPass(PR); |
Jacob Gravelle | 4092645 | 2018-03-30 20:36:58 +0000 | [diff] [blame] | 76 | initializeWebAssemblyLowerBrUnlessPass(PR); |
| 77 | initializeWebAssemblyRegNumberingPass(PR); |
| 78 | initializeWebAssemblyPeepholePass(PR); |
| 79 | initializeWebAssemblyCallIndirectFixupPass(PR); |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | //===----------------------------------------------------------------------===// |
| 83 | // WebAssembly Lowering public interface. |
| 84 | //===----------------------------------------------------------------------===// |
| 85 | |
Dan Gohman | 53572d0 | 2019-06-05 20:01:01 +0000 | [diff] [blame] | 86 | static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM, |
| 87 | const Triple &TT) { |
Sam Clegg | 74f5fd4 | 2018-11-16 18:59:51 +0000 | [diff] [blame] | 88 | if (!RM.hasValue()) { |
| 89 | // Default to static relocation model. This should always be more optimial |
| 90 | // than PIC since the static linker can determine all global addresses and |
| 91 | // assume direct function calls. |
| 92 | return Reloc::Static; |
| 93 | } |
Dan Gohman | 53572d0 | 2019-06-05 20:01:01 +0000 | [diff] [blame] | 94 | |
| 95 | if (!TT.isOSEmscripten()) { |
| 96 | // Relocation modes other than static are currently implemented in a way |
| 97 | // that only works for Emscripten, so disable them if we aren't targeting |
| 98 | // Emscripten. |
| 99 | return Reloc::Static; |
| 100 | } |
| 101 | |
Dan Gohman | 41133a3 | 2016-05-19 03:00:05 +0000 | [diff] [blame] | 102 | return *RM; |
| 103 | } |
| 104 | |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 105 | /// Create an WebAssembly architecture model. |
| 106 | /// |
| 107 | WebAssemblyTargetMachine::WebAssemblyTargetMachine( |
| 108 | const Target &T, const Triple &TT, StringRef CPU, StringRef FS, |
Dan Gohman | 41133a3 | 2016-05-19 03:00:05 +0000 | [diff] [blame] | 109 | const TargetOptions &Options, Optional<Reloc::Model> RM, |
Daniel Jasper | 314ed20 | 2017-08-03 05:15:53 +0000 | [diff] [blame] | 110 | Optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT) |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 111 | : LLVMTargetMachine(T, |
| 112 | TT.isArch64Bit() ? "e-m:e-p:64:64-i64:64-n32:64-S128" |
| 113 | : "e-m:e-p:32:32-i64:64-n32:64-S128", |
Dan Gohman | 53572d0 | 2019-06-05 20:01:01 +0000 | [diff] [blame] | 114 | TT, CPU, FS, Options, getEffectiveRelocModel(RM, TT), |
David Green | ca29c27 | 2018-12-07 12:10:23 +0000 | [diff] [blame] | 115 | getEffectiveCodeModel(CM, CodeModel::Large), OL), |
Sam Clegg | cf2a9e2 | 2018-07-16 23:09:29 +0000 | [diff] [blame] | 116 | TLOF(new WebAssemblyTargetObjectFile()) { |
Dan Gohman | e040533 | 2016-10-03 22:43:53 +0000 | [diff] [blame] | 117 | // WebAssembly type-checks instructions, but a noreturn function with a return |
Derek Schuff | ffa143c | 2015-11-10 00:30:57 +0000 | [diff] [blame] | 118 | // type that doesn't match the context will cause a check failure. So we lower |
| 119 | // LLVM 'unreachable' to ISD::TRAP and then lower that to WebAssembly's |
Dan Gohman | e040533 | 2016-10-03 22:43:53 +0000 | [diff] [blame] | 120 | // 'unreachable' instructions which is meant for that case. |
Derek Schuff | ffa143c | 2015-11-10 00:30:57 +0000 | [diff] [blame] | 121 | this->Options.TrapUnreachable = true; |
| 122 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 123 | // WebAssembly treats each function as an independent unit. Force |
| 124 | // -ffunction-sections, effectively, so that we can emit them independently. |
Sam Clegg | cf2a9e2 | 2018-07-16 23:09:29 +0000 | [diff] [blame] | 125 | this->Options.FunctionSections = true; |
| 126 | this->Options.DataSections = true; |
| 127 | this->Options.UniqueSectionNames = true; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 128 | |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 129 | initAsmInfo(); |
| 130 | |
Dan Gohman | d85ab7f | 2016-02-18 06:32:53 +0000 | [diff] [blame] | 131 | // Note that we don't use setRequiresStructuredCFG(true). It disables |
| 132 | // optimizations than we're ok with, and want, such as critical edge |
| 133 | // splitting and tail merging. |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 136 | WebAssemblyTargetMachine::~WebAssemblyTargetMachine() = default; // anchor. |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 137 | |
| 138 | const WebAssemblySubtarget * |
Thomas Lively | f3b4f99 | 2019-02-28 18:39:08 +0000 | [diff] [blame] | 139 | WebAssemblyTargetMachine::getSubtargetImpl(std::string CPU, |
| 140 | std::string FS) const { |
| 141 | auto &I = SubtargetMap[CPU + FS]; |
| 142 | if (!I) { |
| 143 | I = llvm::make_unique<WebAssemblySubtarget>(TargetTriple, CPU, FS, *this); |
Thomas Lively | f3b4f99 | 2019-02-28 18:39:08 +0000 | [diff] [blame] | 144 | } |
| 145 | return I.get(); |
| 146 | } |
| 147 | |
| 148 | const WebAssemblySubtarget * |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 149 | WebAssemblyTargetMachine::getSubtargetImpl(const Function &F) const { |
| 150 | Attribute CPUAttr = F.getFnAttribute("target-cpu"); |
| 151 | Attribute FSAttr = F.getFnAttribute("target-features"); |
| 152 | |
| 153 | std::string CPU = !CPUAttr.hasAttribute(Attribute::None) |
| 154 | ? CPUAttr.getValueAsString().str() |
| 155 | : TargetCPU; |
| 156 | std::string FS = !FSAttr.hasAttribute(Attribute::None) |
| 157 | ? FSAttr.getValueAsString().str() |
| 158 | : TargetFS; |
| 159 | |
Thomas Lively | f3b4f99 | 2019-02-28 18:39:08 +0000 | [diff] [blame] | 160 | // This needs to be done before we create a new subtarget since any |
| 161 | // creation will depend on the TM and the code generation flags on the |
| 162 | // function that reside in TargetOptions. |
| 163 | resetTargetOptions(F); |
| 164 | |
| 165 | return getSubtargetImpl(CPU, FS); |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | namespace { |
Thomas Lively | 3f34e1b8 | 2019-03-29 00:14:01 +0000 | [diff] [blame] | 169 | |
| 170 | class CoalesceFeaturesAndStripAtomics final : public ModulePass { |
| 171 | // Take the union of all features used in the module and use it for each |
| 172 | // function individually, since having multiple feature sets in one module |
| 173 | // currently does not make sense for WebAssembly. If atomics are not enabled, |
| 174 | // also strip atomic operations and thread local storage. |
Derek Schuff | 39b5367 | 2018-03-20 22:01:32 +0000 | [diff] [blame] | 175 | static char ID; |
Thomas Lively | 3f34e1b8 | 2019-03-29 00:14:01 +0000 | [diff] [blame] | 176 | WebAssemblyTargetMachine *WasmTM; |
Derek Schuff | 39b5367 | 2018-03-20 22:01:32 +0000 | [diff] [blame] | 177 | |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 178 | public: |
Thomas Lively | 3f34e1b8 | 2019-03-29 00:14:01 +0000 | [diff] [blame] | 179 | CoalesceFeaturesAndStripAtomics(WebAssemblyTargetMachine *WasmTM) |
| 180 | : ModulePass(ID), WasmTM(WasmTM) {} |
| 181 | |
Derek Schuff | 39b5367 | 2018-03-20 22:01:32 +0000 | [diff] [blame] | 182 | bool runOnModule(Module &M) override { |
Thomas Lively | 3f34e1b8 | 2019-03-29 00:14:01 +0000 | [diff] [blame] | 183 | FeatureBitset Features = coalesceFeatures(M); |
| 184 | |
| 185 | std::string FeatureStr = getFeatureString(Features); |
| 186 | for (auto &F : M) |
| 187 | replaceFeatures(F, FeatureStr); |
| 188 | |
Guanzhong Chen | 42bba4b | 2019-07-16 22:00:45 +0000 | [diff] [blame^] | 189 | bool StrippedAtomics = false; |
| 190 | bool StrippedTLS = false; |
Thomas Lively | 3f34e1b8 | 2019-03-29 00:14:01 +0000 | [diff] [blame] | 191 | |
Guanzhong Chen | 42bba4b | 2019-07-16 22:00:45 +0000 | [diff] [blame^] | 192 | if (!Features[WebAssembly::FeatureAtomics]) |
| 193 | StrippedAtomics = stripAtomics(M); |
| 194 | |
| 195 | if (!Features[WebAssembly::FeatureBulkMemory]) |
| 196 | StrippedTLS = stripThreadLocals(M); |
| 197 | |
| 198 | if (StrippedAtomics && !StrippedTLS) |
| 199 | stripThreadLocals(M); |
| 200 | else if (StrippedTLS && !StrippedAtomics) |
| 201 | stripAtomics(M); |
| 202 | |
| 203 | recordFeatures(M, Features, StrippedAtomics || StrippedTLS); |
Thomas Lively | 3f34e1b8 | 2019-03-29 00:14:01 +0000 | [diff] [blame] | 204 | |
| 205 | // Conservatively assume we have made some change |
Derek Schuff | 39b5367 | 2018-03-20 22:01:32 +0000 | [diff] [blame] | 206 | return true; |
| 207 | } |
Thomas Lively | 3f34e1b8 | 2019-03-29 00:14:01 +0000 | [diff] [blame] | 208 | |
| 209 | private: |
| 210 | FeatureBitset coalesceFeatures(const Module &M) { |
| 211 | FeatureBitset Features = |
| 212 | WasmTM |
| 213 | ->getSubtargetImpl(WasmTM->getTargetCPU(), |
| 214 | WasmTM->getTargetFeatureString()) |
| 215 | ->getFeatureBits(); |
| 216 | for (auto &F : M) |
| 217 | Features |= WasmTM->getSubtargetImpl(F)->getFeatureBits(); |
| 218 | return Features; |
| 219 | } |
| 220 | |
| 221 | std::string getFeatureString(const FeatureBitset &Features) { |
| 222 | std::string Ret; |
| 223 | for (const SubtargetFeatureKV &KV : WebAssemblyFeatureKV) { |
| 224 | if (Features[KV.Value]) |
| 225 | Ret += (StringRef("+") + KV.Key + ",").str(); |
| 226 | } |
| 227 | return Ret; |
| 228 | } |
| 229 | |
| 230 | void replaceFeatures(Function &F, const std::string &Features) { |
| 231 | F.removeFnAttr("target-features"); |
| 232 | F.removeFnAttr("target-cpu"); |
| 233 | F.addFnAttr("target-features", Features); |
| 234 | } |
| 235 | |
| 236 | bool stripAtomics(Module &M) { |
| 237 | // Detect whether any atomics will be lowered, since there is no way to tell |
| 238 | // whether the LowerAtomic pass lowers e.g. stores. |
| 239 | bool Stripped = false; |
| 240 | for (auto &F : M) { |
| 241 | for (auto &B : F) { |
| 242 | for (auto &I : B) { |
| 243 | if (I.isAtomic()) { |
| 244 | Stripped = true; |
| 245 | goto done; |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | done: |
| 252 | if (!Stripped) |
| 253 | return false; |
| 254 | |
| 255 | LowerAtomicPass Lowerer; |
| 256 | FunctionAnalysisManager FAM; |
| 257 | for (auto &F : M) |
| 258 | Lowerer.run(F, FAM); |
| 259 | |
| 260 | return true; |
| 261 | } |
| 262 | |
| 263 | bool stripThreadLocals(Module &M) { |
| 264 | bool Stripped = false; |
| 265 | for (auto &GV : M.globals()) { |
| 266 | if (GV.getThreadLocalMode() != |
| 267 | GlobalValue::ThreadLocalMode::NotThreadLocal) { |
| 268 | Stripped = true; |
| 269 | GV.setThreadLocalMode(GlobalValue::ThreadLocalMode::NotThreadLocal); |
| 270 | } |
| 271 | } |
| 272 | return Stripped; |
| 273 | } |
| 274 | |
| 275 | void recordFeatures(Module &M, const FeatureBitset &Features, bool Stripped) { |
| 276 | for (const SubtargetFeatureKV &KV : WebAssemblyFeatureKV) { |
| 277 | std::string MDKey = (StringRef("wasm-feature-") + KV.Key).str(); |
| 278 | if (KV.Value == WebAssembly::FeatureAtomics && Stripped) { |
| 279 | // "atomics" is special: code compiled without atomics may have had its |
| 280 | // atomics lowered to nonatomic operations. In that case, atomics is |
| 281 | // disallowed to prevent unsafe linking with atomics-enabled objects. |
Guanzhong Chen | 42bba4b | 2019-07-16 22:00:45 +0000 | [diff] [blame^] | 282 | assert(!Features[WebAssembly::FeatureAtomics] || |
| 283 | !Features[WebAssembly::FeatureBulkMemory]); |
Thomas Lively | 3f34e1b8 | 2019-03-29 00:14:01 +0000 | [diff] [blame] | 284 | M.addModuleFlag(Module::ModFlagBehavior::Error, MDKey, |
| 285 | wasm::WASM_FEATURE_PREFIX_DISALLOWED); |
| 286 | } else if (Features[KV.Value]) { |
| 287 | // Otherwise features are marked Used or not mentioned |
| 288 | M.addModuleFlag(Module::ModFlagBehavior::Error, MDKey, |
| 289 | wasm::WASM_FEATURE_PREFIX_USED); |
| 290 | } |
| 291 | } |
| 292 | } |
Derek Schuff | 39b5367 | 2018-03-20 22:01:32 +0000 | [diff] [blame] | 293 | }; |
Thomas Lively | 3f34e1b8 | 2019-03-29 00:14:01 +0000 | [diff] [blame] | 294 | char CoalesceFeaturesAndStripAtomics::ID = 0; |
Derek Schuff | 39b5367 | 2018-03-20 22:01:32 +0000 | [diff] [blame] | 295 | |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 296 | /// WebAssembly Code Generator Pass Configuration Options. |
| 297 | class WebAssemblyPassConfig final : public TargetPassConfig { |
| 298 | public: |
Matthias Braun | 5e394c3 | 2017-05-30 21:36:41 +0000 | [diff] [blame] | 299 | WebAssemblyPassConfig(WebAssemblyTargetMachine &TM, PassManagerBase &PM) |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 300 | : TargetPassConfig(TM, PM) {} |
| 301 | |
| 302 | WebAssemblyTargetMachine &getWebAssemblyTargetMachine() const { |
| 303 | return getTM<WebAssemblyTargetMachine>(); |
| 304 | } |
| 305 | |
| 306 | FunctionPass *createTargetRegisterAllocator(bool) override; |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 307 | |
| 308 | void addIRPasses() override; |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 309 | bool addInstSelector() override; |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 310 | void addPostRegAlloc() override; |
Derek Schuff | ad154c8 | 2016-03-28 17:05:30 +0000 | [diff] [blame] | 311 | bool addGCPasses() override { return false; } |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 312 | void addPreEmitPass() override; |
Matt Arsenault | cf55a65 | 2019-03-19 19:33:12 +0000 | [diff] [blame] | 313 | |
| 314 | // No reg alloc |
| 315 | bool addRegAssignmentFast() override { return false; } |
| 316 | |
| 317 | // No reg alloc |
| 318 | bool addRegAssignmentOptimized() override { return false; } |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 319 | }; |
| 320 | } // end anonymous namespace |
| 321 | |
Sanjoy Das | 26d11ca | 2017-12-22 18:21:59 +0000 | [diff] [blame] | 322 | TargetTransformInfo |
| 323 | WebAssemblyTargetMachine::getTargetTransformInfo(const Function &F) { |
| 324 | return TargetTransformInfo(WebAssemblyTTIImpl(this, F)); |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | TargetPassConfig * |
| 328 | WebAssemblyTargetMachine::createPassConfig(PassManagerBase &PM) { |
Matthias Braun | 5e394c3 | 2017-05-30 21:36:41 +0000 | [diff] [blame] | 329 | return new WebAssemblyPassConfig(*this, PM); |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | FunctionPass *WebAssemblyPassConfig::createTargetRegisterAllocator(bool) { |
| 333 | return nullptr; // No reg alloc |
| 334 | } |
| 335 | |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 336 | //===----------------------------------------------------------------------===// |
| 337 | // The following functions are called from lib/CodeGen/Passes.cpp to modify |
| 338 | // the CodeGen pass sequence. |
| 339 | //===----------------------------------------------------------------------===// |
| 340 | |
| 341 | void WebAssemblyPassConfig::addIRPasses() { |
Thomas Lively | 3f34e1b8 | 2019-03-29 00:14:01 +0000 | [diff] [blame] | 342 | // Runs LowerAtomicPass if necessary |
| 343 | addPass(new CoalesceFeaturesAndStripAtomics(&getWebAssemblyTargetMachine())); |
| 344 | |
| 345 | // This is a no-op if atomics are not used in the module |
| 346 | addPass(createAtomicExpandPass()); |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 347 | |
Sam Clegg | 9261755 | 2018-07-11 04:29:36 +0000 | [diff] [blame] | 348 | // Add signatures to prototype-less function declarations |
| 349 | addPass(createWebAssemblyAddMissingPrototypes()); |
| 350 | |
Sam Clegg | bafe690 | 2017-12-15 00:17:10 +0000 | [diff] [blame] | 351 | // Lower .llvm.global_dtors into .llvm_global_ctors with __cxa_atexit calls. |
| 352 | addPass(createWebAssemblyLowerGlobalDtors()); |
| 353 | |
Dan Gohman | 1b63745 | 2017-01-07 00:34:54 +0000 | [diff] [blame] | 354 | // Fix function bitcasts, as WebAssembly requires caller and callee signatures |
| 355 | // to match. |
| 356 | addPass(createWebAssemblyFixFunctionBitcasts()); |
| 357 | |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 358 | // Optimize "returned" function attributes. |
Dan Gohman | b13c91f | 2016-01-19 14:55:02 +0000 | [diff] [blame] | 359 | if (getOptLevel() != CodeGenOpt::None) |
| 360 | addPass(createWebAssemblyOptimizeReturned()); |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 361 | |
Heejin Ahn | c0f1817 | 2016-09-01 21:05:15 +0000 | [diff] [blame] | 362 | // If exception handling is not enabled and setjmp/longjmp handling is |
| 363 | // enabled, we lower invokes into calls and delete unreachable landingpad |
| 364 | // blocks. Lowering invokes when there is no EH support is done in |
| 365 | // TargetPassConfig::addPassesToHandleExceptions, but this runs after this |
| 366 | // function and SjLj handling expects all invokes to be lowered before. |
Heejin Ahn | 9386bde | 2018-02-24 00:40:50 +0000 | [diff] [blame] | 367 | if (!EnableEmException && |
| 368 | TM->Options.ExceptionModel == ExceptionHandling::None) { |
Heejin Ahn | c0f1817 | 2016-09-01 21:05:15 +0000 | [diff] [blame] | 369 | addPass(createLowerInvokePass()); |
| 370 | // The lower invoke pass may create unreachable code. Remove it in order not |
| 371 | // to process dead blocks in setjmp/longjmp handling. |
| 372 | addPass(createUnreachableBlockEliminationPass()); |
| 373 | } |
| 374 | |
| 375 | // Handle exceptions and setjmp/longjmp if enabled. |
Derek Schuff | ccdceda | 2016-08-18 15:27:25 +0000 | [diff] [blame] | 376 | if (EnableEmException || EnableEmSjLj) |
| 377 | addPass(createWebAssemblyLowerEmscriptenEHSjLj(EnableEmException, |
| 378 | EnableEmSjLj)); |
Derek Schuff | f41f67d | 2016-08-01 21:34:04 +0000 | [diff] [blame] | 379 | |
Derek Schuff | ec4be57 | 2019-07-03 23:54:06 +0000 | [diff] [blame] | 380 | // Expand indirectbr instructions to switches. |
| 381 | addPass(createIndirectBrExpandPass()); |
| 382 | |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 383 | TargetPassConfig::addIRPasses(); |
| 384 | } |
| 385 | |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 386 | bool WebAssemblyPassConfig::addInstSelector() { |
Dan Gohman | b0921ca | 2015-12-05 19:24:17 +0000 | [diff] [blame] | 387 | (void)TargetPassConfig::addInstSelector(); |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 388 | addPass( |
| 389 | createWebAssemblyISelDag(getWebAssemblyTargetMachine(), getOptLevel())); |
Dan Gohman | 1cf96c0 | 2015-12-09 16:23:59 +0000 | [diff] [blame] | 390 | // Run the argument-move pass immediately after the ScheduleDAG scheduler |
| 391 | // so that we can fix up the ARGUMENT instructions before anything else |
| 392 | // sees them in the wrong place. |
| 393 | addPass(createWebAssemblyArgumentMove()); |
Dan Gohman | bb37224 | 2016-01-26 03:39:31 +0000 | [diff] [blame] | 394 | // Set the p2align operands. This information is present during ISel, however |
| 395 | // it's inconvenient to collect. Collect it now, and update the immediate |
| 396 | // operands. |
| 397 | addPass(createWebAssemblySetP2AlignOperands()); |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 398 | return false; |
| 399 | } |
| 400 | |
JF Bastien | 600aee9 | 2015-07-31 17:53:38 +0000 | [diff] [blame] | 401 | void WebAssemblyPassConfig::addPostRegAlloc() { |
Dan Gohman | 9c54d3b | 2015-11-25 18:13:18 +0000 | [diff] [blame] | 402 | // TODO: The following CodeGen passes don't currently support code containing |
| 403 | // virtual registers. Consider removing their restrictions and re-enabling |
| 404 | // them. |
Derek Schuff | ad154c8 | 2016-03-28 17:05:30 +0000 | [diff] [blame] | 405 | |
Matthias Braun | 1eb4736 | 2016-08-25 01:27:13 +0000 | [diff] [blame] | 406 | // These functions all require the NoVRegs property. |
JF Bastien | 600aee9 | 2015-07-31 17:53:38 +0000 | [diff] [blame] | 407 | disablePass(&MachineCopyPropagationID); |
Jun Bum Lim | 7ab1b32 | 2018-04-03 18:17:34 +0000 | [diff] [blame] | 408 | disablePass(&PostRAMachineSinkingID); |
Derek Schuff | ecabac6 | 2016-03-28 22:52:20 +0000 | [diff] [blame] | 409 | disablePass(&PostRASchedulerID); |
| 410 | disablePass(&FuncletLayoutID); |
| 411 | disablePass(&StackMapLivenessID); |
| 412 | disablePass(&LiveDebugValuesID); |
Sanjoy Das | fe71ec7 | 2016-04-19 06:24:58 +0000 | [diff] [blame] | 413 | disablePass(&PatchableFunctionID); |
Jun Bum Lim | 7ab1b32 | 2018-04-03 18:17:34 +0000 | [diff] [blame] | 414 | disablePass(&ShrinkWrapID); |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 415 | |
Heejin Ahn | ef9d6ae | 2019-03-05 20:35:34 +0000 | [diff] [blame] | 416 | // This pass hurts code size for wasm because it can generate irreducible |
| 417 | // control flow. |
| 418 | disablePass(&MachineBlockPlacementID); |
| 419 | |
Dan Gohman | b0921ca | 2015-12-05 19:24:17 +0000 | [diff] [blame] | 420 | TargetPassConfig::addPostRegAlloc(); |
JF Bastien | 600aee9 | 2015-07-31 17:53:38 +0000 | [diff] [blame] | 421 | } |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 422 | |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 423 | void WebAssemblyPassConfig::addPreEmitPass() { |
Dan Gohman | b0921ca | 2015-12-05 19:24:17 +0000 | [diff] [blame] | 424 | TargetPassConfig::addPreEmitPass(); |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 425 | |
Derek Schuff | 6f69783 | 2016-10-21 16:38:07 +0000 | [diff] [blame] | 426 | // Rewrite pseudo call_indirect instructions as real instructions. |
| 427 | // This needs to run before register stackification, because we change the |
| 428 | // order of the arguments. |
| 429 | addPass(createWebAssemblyCallIndirectFixup()); |
| 430 | |
Heejin Ahn | e95056d | 2019-01-08 01:25:12 +0000 | [diff] [blame] | 431 | // Eliminate multiple-entry loops. |
| 432 | addPass(createWebAssemblyFixIrreducibleControlFlow()); |
| 433 | |
| 434 | // Do various transformations for exception handling. |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 435 | // Every CFG-changing optimizations should come before this. |
Heejin Ahn | e95056d | 2019-01-08 01:25:12 +0000 | [diff] [blame] | 436 | addPass(createWebAssemblyLateEHPrepare()); |
| 437 | |
Heejin Ahn | 0bb9865 | 2019-01-30 22:44:45 +0000 | [diff] [blame] | 438 | // Now that we have a prologue and epilogue and all frame indices are |
| 439 | // rewritten, eliminate SP and FP. This allows them to be stackified, |
| 440 | // colored, and numbered with the rest of the registers. |
| 441 | addPass(createWebAssemblyReplacePhysRegs()); |
| 442 | |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 443 | // Preparations and optimizations related to register stackification. |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 444 | if (getOptLevel() != CodeGenOpt::None) { |
| 445 | // LiveIntervals isn't commonly run this late. Re-establish preconditions. |
| 446 | addPass(createWebAssemblyPrepareForLiveIntervals()); |
| 447 | |
| 448 | // Depend on LiveIntervals and perform some optimizations on it. |
| 449 | addPass(createWebAssemblyOptimizeLiveIntervals()); |
| 450 | |
Heejin Ahn | 321d522 | 2019-01-08 22:35:18 +0000 | [diff] [blame] | 451 | // Prepare memory intrinsic calls for register stackifying. |
| 452 | addPass(createWebAssemblyMemIntrinsicResults()); |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 453 | |
Dan Gohman | e040533 | 2016-10-03 22:43:53 +0000 | [diff] [blame] | 454 | // Mark registers as representing wasm's value stack. This is a key |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 455 | // code-compression technique in WebAssembly. We run this pass (and |
Heejin Ahn | 321d522 | 2019-01-08 22:35:18 +0000 | [diff] [blame] | 456 | // MemIntrinsicResults above) very late, so that it sees as much code as |
| 457 | // possible, including code emitted by PEI and expanded by late tail |
| 458 | // duplication. |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 459 | addPass(createWebAssemblyRegStackify()); |
| 460 | |
| 461 | // Run the register coloring pass to reduce the total number of registers. |
| 462 | // This runs after stackification so that it doesn't consider registers |
| 463 | // that become stackified. |
| 464 | addPass(createWebAssemblyRegColoring()); |
| 465 | } |
| 466 | |
Dan Gohman | f52ee17 | 2017-02-27 22:38:58 +0000 | [diff] [blame] | 467 | // Sort the blocks of the CFG into topological order, a prerequisite for |
| 468 | // BLOCK and LOOP markers. |
| 469 | addPass(createWebAssemblyCFGSort()); |
| 470 | |
| 471 | // Insert BLOCK and LOOP markers. |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 472 | addPass(createWebAssemblyCFGStackify()); |
Dan Gohman | 5941bde | 2015-11-25 21:32:06 +0000 | [diff] [blame] | 473 | |
Heejin Ahn | e9fd907 | 2019-03-30 09:29:57 +0000 | [diff] [blame] | 474 | // Insert explicit local.get and local.set operators. |
| 475 | addPass(createWebAssemblyExplicitLocals()); |
| 476 | |
Dan Gohman | f0b165a | 2015-12-05 03:03:35 +0000 | [diff] [blame] | 477 | // Lower br_unless into br_if. |
| 478 | addPass(createWebAssemblyLowerBrUnless()); |
| 479 | |
Dan Gohman | 5941bde | 2015-11-25 21:32:06 +0000 | [diff] [blame] | 480 | // Perform the very last peephole optimizations on the code. |
Dan Gohman | b13c91f | 2016-01-19 14:55:02 +0000 | [diff] [blame] | 481 | if (getOptLevel() != CodeGenOpt::None) |
| 482 | addPass(createWebAssemblyPeephole()); |
Dan Gohman | b7c2400 | 2016-05-21 00:21:56 +0000 | [diff] [blame] | 483 | |
| 484 | // Create a mapping from LLVM CodeGen virtual registers to wasm registers. |
| 485 | addPass(createWebAssemblyRegNumbering()); |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 486 | } |
Heejin Ahn | 52221d5 | 2019-03-26 17:35:35 +0000 | [diff] [blame] | 487 | |
| 488 | yaml::MachineFunctionInfo * |
| 489 | WebAssemblyTargetMachine::createDefaultFuncInfoYAML() const { |
| 490 | return new yaml::WebAssemblyFunctionInfo(); |
| 491 | } |
| 492 | |
| 493 | yaml::MachineFunctionInfo *WebAssemblyTargetMachine::convertFuncInfoToYAML( |
| 494 | const MachineFunction &MF) const { |
| 495 | const auto *MFI = MF.getInfo<WebAssemblyFunctionInfo>(); |
| 496 | return new yaml::WebAssemblyFunctionInfo(*MFI); |
| 497 | } |
| 498 | |
| 499 | bool WebAssemblyTargetMachine::parseMachineFunctionInfo( |
| 500 | const yaml::MachineFunctionInfo &MFI, PerFunctionMIParsingState &PFS, |
| 501 | SMDiagnostic &Error, SMRange &SourceRange) const { |
| 502 | const auto &YamlMFI = |
| 503 | reinterpret_cast<const yaml::WebAssemblyFunctionInfo &>(MFI); |
| 504 | MachineFunction &MF = PFS.MF; |
| 505 | MF.getInfo<WebAssemblyFunctionInfo>()->initializeBaseYamlFields(YamlMFI); |
| 506 | return false; |
| 507 | } |