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" |
| 16 | #include "WebAssembly.h" |
Dan Gohman | 5bf22fc | 2015-12-17 04:55:44 +0000 | [diff] [blame] | 17 | #include "WebAssemblyTargetObjectFile.h" |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 18 | #include "WebAssemblyTargetTransformInfo.h" |
| 19 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 20 | #include "llvm/CodeGen/Passes.h" |
| 21 | #include "llvm/CodeGen/RegAllocRegistry.h" |
Matthias Braun | 31d19d4 | 2016-05-10 03:21:59 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/TargetPassConfig.h" |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Function.h" |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 24 | #include "llvm/Support/TargetRegistry.h" |
| 25 | #include "llvm/Target/TargetOptions.h" |
JF Bastien | 03855df | 2015-07-01 23:41:25 +0000 | [diff] [blame] | 26 | #include "llvm/Transforms/Scalar.h" |
David Blaikie | a373d18 | 2018-03-28 17:44:36 +0000 | [diff] [blame] | 27 | #include "llvm/Transforms/Utils.h" |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 28 | using namespace llvm; |
| 29 | |
| 30 | #define DEBUG_TYPE "wasm" |
| 31 | |
Derek Schuff | f41f67d | 2016-08-01 21:34:04 +0000 | [diff] [blame] | 32 | // Emscripten's asm.js-style exception handling |
Derek Schuff | ccdceda | 2016-08-18 15:27:25 +0000 | [diff] [blame] | 33 | static cl::opt<bool> EnableEmException( |
Derek Schuff | 53b9af0 | 2016-08-09 00:29:55 +0000 | [diff] [blame] | 34 | "enable-emscripten-cxx-exceptions", |
Derek Schuff | f41f67d | 2016-08-01 21:34:04 +0000 | [diff] [blame] | 35 | cl::desc("WebAssembly Emscripten-style exception handling"), |
| 36 | cl::init(false)); |
| 37 | |
Derek Schuff | ccdceda | 2016-08-18 15:27:25 +0000 | [diff] [blame] | 38 | // Emscripten's asm.js-style setjmp/longjmp handling |
| 39 | static cl::opt<bool> EnableEmSjLj( |
| 40 | "enable-emscripten-sjlj", |
| 41 | cl::desc("WebAssembly Emscripten-style setjmp/longjmp handling"), |
| 42 | cl::init(false)); |
| 43 | |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 44 | extern "C" void LLVMInitializeWebAssemblyTarget() { |
| 45 | // Register the target. |
Mehdi Amini | f42454b | 2016-10-09 23:00:34 +0000 | [diff] [blame] | 46 | RegisterTargetMachine<WebAssemblyTargetMachine> X( |
| 47 | getTheWebAssemblyTarget32()); |
| 48 | RegisterTargetMachine<WebAssemblyTargetMachine> Y( |
| 49 | getTheWebAssemblyTarget64()); |
Derek Schuff | f41f67d | 2016-08-01 21:34:04 +0000 | [diff] [blame] | 50 | |
Jacob Gravelle | 4092645 | 2018-03-30 20:36:58 +0000 | [diff] [blame] | 51 | // Register backend passes |
| 52 | auto &PR = *PassRegistry::getPassRegistry(); |
Sam Clegg | 9261755 | 2018-07-11 04:29:36 +0000 | [diff] [blame] | 53 | initializeWebAssemblyAddMissingPrototypesPass(PR); |
Jacob Gravelle | 4092645 | 2018-03-30 20:36:58 +0000 | [diff] [blame] | 54 | initializeWebAssemblyLowerEmscriptenEHSjLjPass(PR); |
| 55 | initializeLowerGlobalDtorsPass(PR); |
| 56 | initializeFixFunctionBitcastsPass(PR); |
| 57 | initializeOptimizeReturnedPass(PR); |
| 58 | initializeWebAssemblyArgumentMovePass(PR); |
| 59 | initializeWebAssemblySetP2AlignOperandsPass(PR); |
| 60 | initializeWebAssemblyReplacePhysRegsPass(PR); |
| 61 | initializeWebAssemblyPrepareForLiveIntervalsPass(PR); |
| 62 | initializeWebAssemblyOptimizeLiveIntervalsPass(PR); |
Heejin Ahn | 321d522 | 2019-01-08 22:35:18 +0000 | [diff] [blame] | 63 | initializeWebAssemblyMemIntrinsicResultsPass(PR); |
Jacob Gravelle | 4092645 | 2018-03-30 20:36:58 +0000 | [diff] [blame] | 64 | initializeWebAssemblyRegStackifyPass(PR); |
| 65 | initializeWebAssemblyRegColoringPass(PR); |
| 66 | initializeWebAssemblyExplicitLocalsPass(PR); |
| 67 | initializeWebAssemblyFixIrreducibleControlFlowPass(PR); |
Heejin Ahn | 4934f76 | 2018-06-25 01:07:11 +0000 | [diff] [blame] | 68 | initializeWebAssemblyLateEHPreparePass(PR); |
Heejin Ahn | 04c4894 | 2018-06-25 01:20:21 +0000 | [diff] [blame] | 69 | initializeWebAssemblyExceptionInfoPass(PR); |
Jacob Gravelle | 4092645 | 2018-03-30 20:36:58 +0000 | [diff] [blame] | 70 | initializeWebAssemblyCFGSortPass(PR); |
| 71 | initializeWebAssemblyCFGStackifyPass(PR); |
| 72 | initializeWebAssemblyLowerBrUnlessPass(PR); |
| 73 | initializeWebAssemblyRegNumberingPass(PR); |
| 74 | initializeWebAssemblyPeepholePass(PR); |
| 75 | initializeWebAssemblyCallIndirectFixupPass(PR); |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | //===----------------------------------------------------------------------===// |
| 79 | // WebAssembly Lowering public interface. |
| 80 | //===----------------------------------------------------------------------===// |
| 81 | |
Dan Gohman | 41133a3 | 2016-05-19 03:00:05 +0000 | [diff] [blame] | 82 | static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM) { |
Sam Clegg | 74f5fd4 | 2018-11-16 18:59:51 +0000 | [diff] [blame] | 83 | if (!RM.hasValue()) { |
| 84 | // Default to static relocation model. This should always be more optimial |
| 85 | // than PIC since the static linker can determine all global addresses and |
| 86 | // assume direct function calls. |
| 87 | return Reloc::Static; |
| 88 | } |
Dan Gohman | 41133a3 | 2016-05-19 03:00:05 +0000 | [diff] [blame] | 89 | return *RM; |
| 90 | } |
| 91 | |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 92 | /// Create an WebAssembly architecture model. |
| 93 | /// |
| 94 | WebAssemblyTargetMachine::WebAssemblyTargetMachine( |
| 95 | const Target &T, const Triple &TT, StringRef CPU, StringRef FS, |
Dan Gohman | 41133a3 | 2016-05-19 03:00:05 +0000 | [diff] [blame] | 96 | const TargetOptions &Options, Optional<Reloc::Model> RM, |
Daniel Jasper | 314ed20 | 2017-08-03 05:15:53 +0000 | [diff] [blame] | 97 | Optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT) |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 98 | : LLVMTargetMachine(T, |
| 99 | TT.isArch64Bit() ? "e-m:e-p:64:64-i64:64-n32:64-S128" |
| 100 | : "e-m:e-p:32:32-i64:64-n32:64-S128", |
| 101 | TT, CPU, FS, Options, getEffectiveRelocModel(RM), |
David Green | ca29c27 | 2018-12-07 12:10:23 +0000 | [diff] [blame] | 102 | getEffectiveCodeModel(CM, CodeModel::Large), OL), |
Sam Clegg | cf2a9e2 | 2018-07-16 23:09:29 +0000 | [diff] [blame] | 103 | TLOF(new WebAssemblyTargetObjectFile()) { |
Dan Gohman | e040533 | 2016-10-03 22:43:53 +0000 | [diff] [blame] | 104 | // WebAssembly type-checks instructions, but a noreturn function with a return |
Derek Schuff | ffa143c | 2015-11-10 00:30:57 +0000 | [diff] [blame] | 105 | // type that doesn't match the context will cause a check failure. So we lower |
| 106 | // LLVM 'unreachable' to ISD::TRAP and then lower that to WebAssembly's |
Dan Gohman | e040533 | 2016-10-03 22:43:53 +0000 | [diff] [blame] | 107 | // 'unreachable' instructions which is meant for that case. |
Derek Schuff | ffa143c | 2015-11-10 00:30:57 +0000 | [diff] [blame] | 108 | this->Options.TrapUnreachable = true; |
| 109 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 110 | // WebAssembly treats each function as an independent unit. Force |
| 111 | // -ffunction-sections, effectively, so that we can emit them independently. |
Sam Clegg | cf2a9e2 | 2018-07-16 23:09:29 +0000 | [diff] [blame] | 112 | this->Options.FunctionSections = true; |
| 113 | this->Options.DataSections = true; |
| 114 | this->Options.UniqueSectionNames = true; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 115 | |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 116 | initAsmInfo(); |
| 117 | |
Thomas Lively | f3b4f99 | 2019-02-28 18:39:08 +0000 | [diff] [blame] | 118 | // Create a subtarget using the unmodified target machine features to |
| 119 | // initialize the used feature set with explicitly enabled features. |
| 120 | getSubtargetImpl(getTargetCPU(), getTargetFeatureString()); |
| 121 | |
Dan Gohman | d85ab7f | 2016-02-18 06:32:53 +0000 | [diff] [blame] | 122 | // Note that we don't use setRequiresStructuredCFG(true). It disables |
| 123 | // optimizations than we're ok with, and want, such as critical edge |
| 124 | // splitting and tail merging. |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Heejin Ahn | 18c56a0 | 2019-02-04 19:13:39 +0000 | [diff] [blame] | 127 | WebAssemblyTargetMachine::~WebAssemblyTargetMachine() = default; // anchor. |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 128 | |
| 129 | const WebAssemblySubtarget * |
Thomas Lively | f3b4f99 | 2019-02-28 18:39:08 +0000 | [diff] [blame] | 130 | WebAssemblyTargetMachine::getSubtargetImpl(std::string CPU, |
| 131 | std::string FS) const { |
| 132 | auto &I = SubtargetMap[CPU + FS]; |
| 133 | if (!I) { |
| 134 | I = llvm::make_unique<WebAssemblySubtarget>(TargetTriple, CPU, FS, *this); |
| 135 | UsedFeatures |= I->getFeatureBits(); |
| 136 | } |
| 137 | return I.get(); |
| 138 | } |
| 139 | |
| 140 | const WebAssemblySubtarget * |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 141 | WebAssemblyTargetMachine::getSubtargetImpl(const Function &F) const { |
| 142 | Attribute CPUAttr = F.getFnAttribute("target-cpu"); |
| 143 | Attribute FSAttr = F.getFnAttribute("target-features"); |
| 144 | |
| 145 | std::string CPU = !CPUAttr.hasAttribute(Attribute::None) |
| 146 | ? CPUAttr.getValueAsString().str() |
| 147 | : TargetCPU; |
| 148 | std::string FS = !FSAttr.hasAttribute(Attribute::None) |
| 149 | ? FSAttr.getValueAsString().str() |
| 150 | : TargetFS; |
| 151 | |
Thomas Lively | f3b4f99 | 2019-02-28 18:39:08 +0000 | [diff] [blame] | 152 | // This needs to be done before we create a new subtarget since any |
| 153 | // creation will depend on the TM and the code generation flags on the |
| 154 | // function that reside in TargetOptions. |
| 155 | resetTargetOptions(F); |
| 156 | |
| 157 | return getSubtargetImpl(CPU, FS); |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | namespace { |
Derek Schuff | 39b5367 | 2018-03-20 22:01:32 +0000 | [diff] [blame] | 161 | class StripThreadLocal final : public ModulePass { |
| 162 | // The default thread model for wasm is single, where thread-local variables |
| 163 | // are identical to regular globals and should be treated the same. So this |
| 164 | // pass just converts all GlobalVariables to NotThreadLocal |
| 165 | static char ID; |
| 166 | |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 167 | public: |
Derek Schuff | 39b5367 | 2018-03-20 22:01:32 +0000 | [diff] [blame] | 168 | StripThreadLocal() : ModulePass(ID) {} |
| 169 | bool runOnModule(Module &M) override { |
| 170 | for (auto &GV : M.globals()) |
| 171 | GV.setThreadLocalMode(GlobalValue::ThreadLocalMode::NotThreadLocal); |
| 172 | return true; |
| 173 | } |
| 174 | }; |
| 175 | char StripThreadLocal::ID = 0; |
| 176 | |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 177 | /// WebAssembly Code Generator Pass Configuration Options. |
| 178 | class WebAssemblyPassConfig final : public TargetPassConfig { |
| 179 | public: |
Matthias Braun | 5e394c3 | 2017-05-30 21:36:41 +0000 | [diff] [blame] | 180 | WebAssemblyPassConfig(WebAssemblyTargetMachine &TM, PassManagerBase &PM) |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 181 | : TargetPassConfig(TM, PM) {} |
| 182 | |
| 183 | WebAssemblyTargetMachine &getWebAssemblyTargetMachine() const { |
| 184 | return getTM<WebAssemblyTargetMachine>(); |
| 185 | } |
| 186 | |
| 187 | FunctionPass *createTargetRegisterAllocator(bool) override; |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 188 | |
| 189 | void addIRPasses() override; |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 190 | bool addInstSelector() override; |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 191 | void addPostRegAlloc() override; |
Derek Schuff | ad154c8 | 2016-03-28 17:05:30 +0000 | [diff] [blame] | 192 | bool addGCPasses() override { return false; } |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 193 | void addPreEmitPass() override; |
| 194 | }; |
| 195 | } // end anonymous namespace |
| 196 | |
Sanjoy Das | 26d11ca | 2017-12-22 18:21:59 +0000 | [diff] [blame] | 197 | TargetTransformInfo |
| 198 | WebAssemblyTargetMachine::getTargetTransformInfo(const Function &F) { |
| 199 | return TargetTransformInfo(WebAssemblyTTIImpl(this, F)); |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | TargetPassConfig * |
| 203 | WebAssemblyTargetMachine::createPassConfig(PassManagerBase &PM) { |
Matthias Braun | 5e394c3 | 2017-05-30 21:36:41 +0000 | [diff] [blame] | 204 | return new WebAssemblyPassConfig(*this, PM); |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | FunctionPass *WebAssemblyPassConfig::createTargetRegisterAllocator(bool) { |
| 208 | return nullptr; // No reg alloc |
| 209 | } |
| 210 | |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 211 | //===----------------------------------------------------------------------===// |
| 212 | // The following functions are called from lib/CodeGen/Passes.cpp to modify |
| 213 | // the CodeGen pass sequence. |
| 214 | //===----------------------------------------------------------------------===// |
| 215 | |
| 216 | void WebAssemblyPassConfig::addIRPasses() { |
Thomas Lively | f3b4f99 | 2019-02-28 18:39:08 +0000 | [diff] [blame] | 217 | if (static_cast<WebAssemblyTargetMachine *>(TM) |
| 218 | ->getUsedFeatures()[WebAssembly::FeatureAtomics]) { |
JF Bastien | 03855df | 2015-07-01 23:41:25 +0000 | [diff] [blame] | 219 | // Expand some atomic operations. WebAssemblyTargetLowering has hooks which |
| 220 | // control specifically what gets lowered. |
Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 221 | addPass(createAtomicExpandPass()); |
Thomas Lively | f3b4f99 | 2019-02-28 18:39:08 +0000 | [diff] [blame] | 222 | } else { |
| 223 | // If atomics are not enabled, they get lowered to non-atomics. |
| 224 | addPass(createLowerAtomicPass()); |
| 225 | addPass(new StripThreadLocal()); |
Derek Schuff | 39b5367 | 2018-03-20 22:01:32 +0000 | [diff] [blame] | 226 | } |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 227 | |
Sam Clegg | 9261755 | 2018-07-11 04:29:36 +0000 | [diff] [blame] | 228 | // Add signatures to prototype-less function declarations |
| 229 | addPass(createWebAssemblyAddMissingPrototypes()); |
| 230 | |
Sam Clegg | bafe690 | 2017-12-15 00:17:10 +0000 | [diff] [blame] | 231 | // Lower .llvm.global_dtors into .llvm_global_ctors with __cxa_atexit calls. |
| 232 | addPass(createWebAssemblyLowerGlobalDtors()); |
| 233 | |
Dan Gohman | 1b63745 | 2017-01-07 00:34:54 +0000 | [diff] [blame] | 234 | // Fix function bitcasts, as WebAssembly requires caller and callee signatures |
| 235 | // to match. |
| 236 | addPass(createWebAssemblyFixFunctionBitcasts()); |
| 237 | |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 238 | // Optimize "returned" function attributes. |
Dan Gohman | b13c91f | 2016-01-19 14:55:02 +0000 | [diff] [blame] | 239 | if (getOptLevel() != CodeGenOpt::None) |
| 240 | addPass(createWebAssemblyOptimizeReturned()); |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 241 | |
Heejin Ahn | c0f1817 | 2016-09-01 21:05:15 +0000 | [diff] [blame] | 242 | // If exception handling is not enabled and setjmp/longjmp handling is |
| 243 | // enabled, we lower invokes into calls and delete unreachable landingpad |
| 244 | // blocks. Lowering invokes when there is no EH support is done in |
| 245 | // TargetPassConfig::addPassesToHandleExceptions, but this runs after this |
| 246 | // function and SjLj handling expects all invokes to be lowered before. |
Heejin Ahn | 9386bde | 2018-02-24 00:40:50 +0000 | [diff] [blame] | 247 | if (!EnableEmException && |
| 248 | TM->Options.ExceptionModel == ExceptionHandling::None) { |
Heejin Ahn | c0f1817 | 2016-09-01 21:05:15 +0000 | [diff] [blame] | 249 | addPass(createLowerInvokePass()); |
| 250 | // The lower invoke pass may create unreachable code. Remove it in order not |
| 251 | // to process dead blocks in setjmp/longjmp handling. |
| 252 | addPass(createUnreachableBlockEliminationPass()); |
| 253 | } |
| 254 | |
| 255 | // Handle exceptions and setjmp/longjmp if enabled. |
Derek Schuff | ccdceda | 2016-08-18 15:27:25 +0000 | [diff] [blame] | 256 | if (EnableEmException || EnableEmSjLj) |
| 257 | addPass(createWebAssemblyLowerEmscriptenEHSjLj(EnableEmException, |
| 258 | EnableEmSjLj)); |
Derek Schuff | f41f67d | 2016-08-01 21:34:04 +0000 | [diff] [blame] | 259 | |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 260 | TargetPassConfig::addIRPasses(); |
| 261 | } |
| 262 | |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 263 | bool WebAssemblyPassConfig::addInstSelector() { |
Dan Gohman | b0921ca | 2015-12-05 19:24:17 +0000 | [diff] [blame] | 264 | (void)TargetPassConfig::addInstSelector(); |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 265 | addPass( |
| 266 | createWebAssemblyISelDag(getWebAssemblyTargetMachine(), getOptLevel())); |
Dan Gohman | 1cf96c0 | 2015-12-09 16:23:59 +0000 | [diff] [blame] | 267 | // Run the argument-move pass immediately after the ScheduleDAG scheduler |
| 268 | // so that we can fix up the ARGUMENT instructions before anything else |
| 269 | // sees them in the wrong place. |
| 270 | addPass(createWebAssemblyArgumentMove()); |
Dan Gohman | bb37224 | 2016-01-26 03:39:31 +0000 | [diff] [blame] | 271 | // Set the p2align operands. This information is present during ISel, however |
| 272 | // it's inconvenient to collect. Collect it now, and update the immediate |
| 273 | // operands. |
| 274 | addPass(createWebAssemblySetP2AlignOperands()); |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 275 | return false; |
| 276 | } |
| 277 | |
JF Bastien | 600aee9 | 2015-07-31 17:53:38 +0000 | [diff] [blame] | 278 | void WebAssemblyPassConfig::addPostRegAlloc() { |
Dan Gohman | 9c54d3b | 2015-11-25 18:13:18 +0000 | [diff] [blame] | 279 | // TODO: The following CodeGen passes don't currently support code containing |
| 280 | // virtual registers. Consider removing their restrictions and re-enabling |
| 281 | // them. |
Derek Schuff | ad154c8 | 2016-03-28 17:05:30 +0000 | [diff] [blame] | 282 | |
Matthias Braun | 1eb4736 | 2016-08-25 01:27:13 +0000 | [diff] [blame] | 283 | // These functions all require the NoVRegs property. |
JF Bastien | 600aee9 | 2015-07-31 17:53:38 +0000 | [diff] [blame] | 284 | disablePass(&MachineCopyPropagationID); |
Jun Bum Lim | 7ab1b32 | 2018-04-03 18:17:34 +0000 | [diff] [blame] | 285 | disablePass(&PostRAMachineSinkingID); |
Derek Schuff | ecabac6 | 2016-03-28 22:52:20 +0000 | [diff] [blame] | 286 | disablePass(&PostRASchedulerID); |
| 287 | disablePass(&FuncletLayoutID); |
| 288 | disablePass(&StackMapLivenessID); |
| 289 | disablePass(&LiveDebugValuesID); |
Sanjoy Das | fe71ec7 | 2016-04-19 06:24:58 +0000 | [diff] [blame] | 290 | disablePass(&PatchableFunctionID); |
Jun Bum Lim | 7ab1b32 | 2018-04-03 18:17:34 +0000 | [diff] [blame] | 291 | disablePass(&ShrinkWrapID); |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 292 | |
Dan Gohman | b0921ca | 2015-12-05 19:24:17 +0000 | [diff] [blame] | 293 | TargetPassConfig::addPostRegAlloc(); |
JF Bastien | 600aee9 | 2015-07-31 17:53:38 +0000 | [diff] [blame] | 294 | } |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 295 | |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 296 | void WebAssemblyPassConfig::addPreEmitPass() { |
Dan Gohman | b0921ca | 2015-12-05 19:24:17 +0000 | [diff] [blame] | 297 | TargetPassConfig::addPreEmitPass(); |
Dan Gohman | 05ac43f | 2015-12-17 01:39:00 +0000 | [diff] [blame] | 298 | |
Derek Schuff | 6f69783 | 2016-10-21 16:38:07 +0000 | [diff] [blame] | 299 | // Rewrite pseudo call_indirect instructions as real instructions. |
| 300 | // This needs to run before register stackification, because we change the |
| 301 | // order of the arguments. |
| 302 | addPass(createWebAssemblyCallIndirectFixup()); |
| 303 | |
Heejin Ahn | e95056d | 2019-01-08 01:25:12 +0000 | [diff] [blame] | 304 | // Eliminate multiple-entry loops. |
| 305 | addPass(createWebAssemblyFixIrreducibleControlFlow()); |
| 306 | |
| 307 | // Do various transformations for exception handling. |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 308 | // Every CFG-changing optimizations should come before this. |
Heejin Ahn | e95056d | 2019-01-08 01:25:12 +0000 | [diff] [blame] | 309 | addPass(createWebAssemblyLateEHPrepare()); |
| 310 | |
Heejin Ahn | 0bb9865 | 2019-01-30 22:44:45 +0000 | [diff] [blame] | 311 | // Now that we have a prologue and epilogue and all frame indices are |
| 312 | // rewritten, eliminate SP and FP. This allows them to be stackified, |
| 313 | // colored, and numbered with the rest of the registers. |
| 314 | addPass(createWebAssemblyReplacePhysRegs()); |
| 315 | |
Heejin Ahn | d6f4878 | 2019-01-30 03:21:57 +0000 | [diff] [blame] | 316 | // Preparations and optimizations related to register stackification. |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 317 | if (getOptLevel() != CodeGenOpt::None) { |
| 318 | // LiveIntervals isn't commonly run this late. Re-establish preconditions. |
| 319 | addPass(createWebAssemblyPrepareForLiveIntervals()); |
| 320 | |
| 321 | // Depend on LiveIntervals and perform some optimizations on it. |
| 322 | addPass(createWebAssemblyOptimizeLiveIntervals()); |
| 323 | |
Heejin Ahn | 321d522 | 2019-01-08 22:35:18 +0000 | [diff] [blame] | 324 | // Prepare memory intrinsic calls for register stackifying. |
| 325 | addPass(createWebAssemblyMemIntrinsicResults()); |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 326 | |
Dan Gohman | e040533 | 2016-10-03 22:43:53 +0000 | [diff] [blame] | 327 | // Mark registers as representing wasm's value stack. This is a key |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 328 | // code-compression technique in WebAssembly. We run this pass (and |
Heejin Ahn | 321d522 | 2019-01-08 22:35:18 +0000 | [diff] [blame] | 329 | // MemIntrinsicResults above) very late, so that it sees as much code as |
| 330 | // possible, including code emitted by PEI and expanded by late tail |
| 331 | // duplication. |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 332 | addPass(createWebAssemblyRegStackify()); |
| 333 | |
| 334 | // Run the register coloring pass to reduce the total number of registers. |
| 335 | // This runs after stackification so that it doesn't consider registers |
| 336 | // that become stackified. |
| 337 | addPass(createWebAssemblyRegColoring()); |
| 338 | } |
| 339 | |
Thomas Lively | 6a87dda | 2019-01-08 06:25:55 +0000 | [diff] [blame] | 340 | // Insert explicit local.get and local.set operators. |
Wouter van Oortmerssen | a7be375 | 2018-08-13 23:12:49 +0000 | [diff] [blame] | 341 | addPass(createWebAssemblyExplicitLocals()); |
| 342 | |
Dan Gohman | f52ee17 | 2017-02-27 22:38:58 +0000 | [diff] [blame] | 343 | // Sort the blocks of the CFG into topological order, a prerequisite for |
| 344 | // BLOCK and LOOP markers. |
| 345 | addPass(createWebAssemblyCFGSort()); |
| 346 | |
| 347 | // Insert BLOCK and LOOP markers. |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 348 | addPass(createWebAssemblyCFGStackify()); |
Dan Gohman | 5941bde | 2015-11-25 21:32:06 +0000 | [diff] [blame] | 349 | |
Dan Gohman | f0b165a | 2015-12-05 03:03:35 +0000 | [diff] [blame] | 350 | // Lower br_unless into br_if. |
| 351 | addPass(createWebAssemblyLowerBrUnless()); |
| 352 | |
Dan Gohman | 5941bde | 2015-11-25 21:32:06 +0000 | [diff] [blame] | 353 | // Perform the very last peephole optimizations on the code. |
Dan Gohman | b13c91f | 2016-01-19 14:55:02 +0000 | [diff] [blame] | 354 | if (getOptLevel() != CodeGenOpt::None) |
| 355 | addPass(createWebAssemblyPeephole()); |
Dan Gohman | b7c2400 | 2016-05-21 00:21:56 +0000 | [diff] [blame] | 356 | |
| 357 | // Create a mapping from LLVM CodeGen virtual registers to wasm registers. |
| 358 | addPass(createWebAssemblyRegNumbering()); |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 359 | } |