| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 1 | //===-- WebAssemblyExplicitLocals.cpp - Make Locals Explicit --------------===// |
| 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 | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// |
| 9 | /// \file |
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 10 | /// This file converts any remaining registers into WebAssembly locals. |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 11 | /// |
| 12 | /// After register stackification and register coloring, convert non-stackified |
| Thomas Lively | 6a87dda | 2019-01-08 06:25:55 +0000 | [diff] [blame] | 13 | /// registers into locals, inserting explicit local.get and local.set |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 14 | /// instructions. |
| 15 | /// |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
| 18 | #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" |
| 19 | #include "WebAssembly.h" |
| Yury Delendik | adf7a0a | 2019-12-20 14:31:56 -0800 | [diff] [blame] | 20 | #include "WebAssemblyDebugValueManager.h" |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 21 | #include "WebAssemblyMachineFunctionInfo.h" |
| 22 | #include "WebAssemblySubtarget.h" |
| 23 | #include "WebAssemblyUtilities.h" |
| 24 | #include "llvm/CodeGen/MachineBlockFrequencyInfo.h" |
| 25 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 26 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 27 | #include "llvm/CodeGen/Passes.h" |
| 28 | #include "llvm/Support/Debug.h" |
| 29 | #include "llvm/Support/raw_ostream.h" |
| 30 | using namespace llvm; |
| 31 | |
| 32 | #define DEBUG_TYPE "wasm-explicit-locals" |
| 33 | |
| Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 34 | // A command-line option to disable this pass, and keep implicit locals |
| 35 | // for the purpose of testing with lit/llc ONLY. |
| 36 | // This produces output which is not valid WebAssembly, and is not supported |
| 37 | // by assemblers/disassemblers and other MC based tools. |
| 38 | static cl::opt<bool> WasmDisableExplicitLocals( |
| 39 | "wasm-disable-explicit-locals", cl::Hidden, |
| 40 | cl::desc("WebAssembly: output implicit locals in" |
| 41 | " instruction output for test purposes only."), |
| Dan Gohman | 7d7409e | 2017-02-28 23:37:04 +0000 | [diff] [blame] | 42 | cl::init(false)); |
| 43 | |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 44 | namespace { |
| 45 | class WebAssemblyExplicitLocals final : public MachineFunctionPass { |
| 46 | StringRef getPassName() const override { |
| 47 | return "WebAssembly Explicit Locals"; |
| 48 | } |
| 49 | |
| 50 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 51 | AU.setPreservesCFG(); |
| 52 | AU.addPreserved<MachineBlockFrequencyInfo>(); |
| 53 | MachineFunctionPass::getAnalysisUsage(AU); |
| 54 | } |
| 55 | |
| 56 | bool runOnMachineFunction(MachineFunction &MF) override; |
| 57 | |
| 58 | public: |
| 59 | static char ID; // Pass identification, replacement for typeid |
| 60 | WebAssemblyExplicitLocals() : MachineFunctionPass(ID) {} |
| 61 | }; |
| 62 | } // end anonymous namespace |
| 63 | |
| 64 | char WebAssemblyExplicitLocals::ID = 0; |
| Jacob Gravelle | 4092645 | 2018-03-30 20:36:58 +0000 | [diff] [blame] | 65 | INITIALIZE_PASS(WebAssemblyExplicitLocals, DEBUG_TYPE, |
| 66 | "Convert registers to WebAssembly locals", false, false) |
| 67 | |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 68 | FunctionPass *llvm::createWebAssemblyExplicitLocals() { |
| 69 | return new WebAssemblyExplicitLocals(); |
| 70 | } |
| 71 | |
| 72 | /// Return a local id number for the given register, assigning it a new one |
| 73 | /// if it doesn't yet have one. |
| 74 | static unsigned getLocalId(DenseMap<unsigned, unsigned> &Reg2Local, |
| 75 | unsigned &CurLocal, unsigned Reg) { |
| Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 76 | auto P = Reg2Local.insert(std::make_pair(Reg, CurLocal)); |
| 77 | if (P.second) |
| 78 | ++CurLocal; |
| 79 | return P.first->second; |
| 80 | } |
| 81 | |
| 82 | /// Get the appropriate drop opcode for the given register class. |
| 83 | static unsigned getDropOpcode(const TargetRegisterClass *RC) { |
| 84 | if (RC == &WebAssembly::I32RegClass) |
| 85 | return WebAssembly::DROP_I32; |
| 86 | if (RC == &WebAssembly::I64RegClass) |
| 87 | return WebAssembly::DROP_I64; |
| 88 | if (RC == &WebAssembly::F32RegClass) |
| 89 | return WebAssembly::DROP_F32; |
| 90 | if (RC == &WebAssembly::F64RegClass) |
| 91 | return WebAssembly::DROP_F64; |
| 92 | if (RC == &WebAssembly::V128RegClass) |
| 93 | return WebAssembly::DROP_V128; |
| Heejin Ahn | 9f96a58 | 2019-07-15 22:49:25 +0000 | [diff] [blame] | 94 | if (RC == &WebAssembly::EXNREFRegClass) |
| 95 | return WebAssembly::DROP_EXNREF; |
| Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 96 | llvm_unreachable("Unexpected register class"); |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| Thomas Lively | 6a87dda | 2019-01-08 06:25:55 +0000 | [diff] [blame] | 99 | /// Get the appropriate local.get opcode for the given register class. |
| Heejin Ahn | 34dc1f2 | 2019-03-19 05:07:33 +0000 | [diff] [blame] | 100 | static unsigned getLocalGetOpcode(const TargetRegisterClass *RC) { |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 101 | if (RC == &WebAssembly::I32RegClass) |
| Thomas Lively | 6a87dda | 2019-01-08 06:25:55 +0000 | [diff] [blame] | 102 | return WebAssembly::LOCAL_GET_I32; |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 103 | if (RC == &WebAssembly::I64RegClass) |
| Thomas Lively | 6a87dda | 2019-01-08 06:25:55 +0000 | [diff] [blame] | 104 | return WebAssembly::LOCAL_GET_I64; |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 105 | if (RC == &WebAssembly::F32RegClass) |
| Thomas Lively | 6a87dda | 2019-01-08 06:25:55 +0000 | [diff] [blame] | 106 | return WebAssembly::LOCAL_GET_F32; |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 107 | if (RC == &WebAssembly::F64RegClass) |
| Thomas Lively | 6a87dda | 2019-01-08 06:25:55 +0000 | [diff] [blame] | 108 | return WebAssembly::LOCAL_GET_F64; |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 109 | if (RC == &WebAssembly::V128RegClass) |
| Thomas Lively | 6a87dda | 2019-01-08 06:25:55 +0000 | [diff] [blame] | 110 | return WebAssembly::LOCAL_GET_V128; |
| Heejin Ahn | 9f96a58 | 2019-07-15 22:49:25 +0000 | [diff] [blame] | 111 | if (RC == &WebAssembly::EXNREFRegClass) |
| 112 | return WebAssembly::LOCAL_GET_EXNREF; |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 113 | llvm_unreachable("Unexpected register class"); |
| 114 | } |
| 115 | |
| Thomas Lively | 6a87dda | 2019-01-08 06:25:55 +0000 | [diff] [blame] | 116 | /// Get the appropriate local.set opcode for the given register class. |
| Heejin Ahn | 34dc1f2 | 2019-03-19 05:07:33 +0000 | [diff] [blame] | 117 | static unsigned getLocalSetOpcode(const TargetRegisterClass *RC) { |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 118 | if (RC == &WebAssembly::I32RegClass) |
| Thomas Lively | 6a87dda | 2019-01-08 06:25:55 +0000 | [diff] [blame] | 119 | return WebAssembly::LOCAL_SET_I32; |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 120 | if (RC == &WebAssembly::I64RegClass) |
| Thomas Lively | 6a87dda | 2019-01-08 06:25:55 +0000 | [diff] [blame] | 121 | return WebAssembly::LOCAL_SET_I64; |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 122 | if (RC == &WebAssembly::F32RegClass) |
| Thomas Lively | 6a87dda | 2019-01-08 06:25:55 +0000 | [diff] [blame] | 123 | return WebAssembly::LOCAL_SET_F32; |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 124 | if (RC == &WebAssembly::F64RegClass) |
| Thomas Lively | 6a87dda | 2019-01-08 06:25:55 +0000 | [diff] [blame] | 125 | return WebAssembly::LOCAL_SET_F64; |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 126 | if (RC == &WebAssembly::V128RegClass) |
| Thomas Lively | 6a87dda | 2019-01-08 06:25:55 +0000 | [diff] [blame] | 127 | return WebAssembly::LOCAL_SET_V128; |
| Heejin Ahn | 9f96a58 | 2019-07-15 22:49:25 +0000 | [diff] [blame] | 128 | if (RC == &WebAssembly::EXNREFRegClass) |
| 129 | return WebAssembly::LOCAL_SET_EXNREF; |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 130 | llvm_unreachable("Unexpected register class"); |
| 131 | } |
| 132 | |
| Thomas Lively | 6a87dda | 2019-01-08 06:25:55 +0000 | [diff] [blame] | 133 | /// Get the appropriate local.tee opcode for the given register class. |
| Heejin Ahn | 34dc1f2 | 2019-03-19 05:07:33 +0000 | [diff] [blame] | 134 | static unsigned getLocalTeeOpcode(const TargetRegisterClass *RC) { |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 135 | if (RC == &WebAssembly::I32RegClass) |
| Thomas Lively | 6a87dda | 2019-01-08 06:25:55 +0000 | [diff] [blame] | 136 | return WebAssembly::LOCAL_TEE_I32; |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 137 | if (RC == &WebAssembly::I64RegClass) |
| Thomas Lively | 6a87dda | 2019-01-08 06:25:55 +0000 | [diff] [blame] | 138 | return WebAssembly::LOCAL_TEE_I64; |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 139 | if (RC == &WebAssembly::F32RegClass) |
| Thomas Lively | 6a87dda | 2019-01-08 06:25:55 +0000 | [diff] [blame] | 140 | return WebAssembly::LOCAL_TEE_F32; |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 141 | if (RC == &WebAssembly::F64RegClass) |
| Thomas Lively | 6a87dda | 2019-01-08 06:25:55 +0000 | [diff] [blame] | 142 | return WebAssembly::LOCAL_TEE_F64; |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 143 | if (RC == &WebAssembly::V128RegClass) |
| Thomas Lively | 6a87dda | 2019-01-08 06:25:55 +0000 | [diff] [blame] | 144 | return WebAssembly::LOCAL_TEE_V128; |
| Heejin Ahn | 9f96a58 | 2019-07-15 22:49:25 +0000 | [diff] [blame] | 145 | if (RC == &WebAssembly::EXNREFRegClass) |
| 146 | return WebAssembly::LOCAL_TEE_EXNREF; |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 147 | llvm_unreachable("Unexpected register class"); |
| 148 | } |
| 149 | |
| 150 | /// Get the type associated with the given register class. |
| Dan Gohman | 3acb187 | 2016-10-24 23:27:49 +0000 | [diff] [blame] | 151 | static MVT typeForRegClass(const TargetRegisterClass *RC) { |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 152 | if (RC == &WebAssembly::I32RegClass) |
| Dan Gohman | 3acb187 | 2016-10-24 23:27:49 +0000 | [diff] [blame] | 153 | return MVT::i32; |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 154 | if (RC == &WebAssembly::I64RegClass) |
| Dan Gohman | 3acb187 | 2016-10-24 23:27:49 +0000 | [diff] [blame] | 155 | return MVT::i64; |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 156 | if (RC == &WebAssembly::F32RegClass) |
| Dan Gohman | 3acb187 | 2016-10-24 23:27:49 +0000 | [diff] [blame] | 157 | return MVT::f32; |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 158 | if (RC == &WebAssembly::F64RegClass) |
| Dan Gohman | 3acb187 | 2016-10-24 23:27:49 +0000 | [diff] [blame] | 159 | return MVT::f64; |
| Thomas Lively | 409f584 | 2018-10-09 23:33:16 +0000 | [diff] [blame] | 160 | if (RC == &WebAssembly::V128RegClass) |
| 161 | return MVT::v16i8; |
| Heejin Ahn | 9f96a58 | 2019-07-15 22:49:25 +0000 | [diff] [blame] | 162 | if (RC == &WebAssembly::EXNREFRegClass) |
| 163 | return MVT::exnref; |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 164 | llvm_unreachable("unrecognized register class"); |
| 165 | } |
| 166 | |
| 167 | /// Given a MachineOperand of a stackified vreg, return the instruction at the |
| 168 | /// start of the expression tree. |
| Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 169 | static MachineInstr *findStartOfTree(MachineOperand &MO, |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 170 | MachineRegisterInfo &MRI, |
| 171 | WebAssemblyFunctionInfo &MFI) { |
| Daniel Sanders | 05c145d | 2019-08-12 22:40:45 +0000 | [diff] [blame] | 172 | Register Reg = MO.getReg(); |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 173 | assert(MFI.isVRegStackified(Reg)); |
| 174 | MachineInstr *Def = MRI.getVRegDef(Reg); |
| 175 | |
| 176 | // Find the first stackified use and proceed from there. |
| 177 | for (MachineOperand &DefMO : Def->explicit_uses()) { |
| 178 | if (!DefMO.isReg()) |
| 179 | continue; |
| Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 180 | return findStartOfTree(DefMO, MRI, MFI); |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | // If there were no stackified uses, we've reached the start. |
| 184 | return Def; |
| 185 | } |
| 186 | |
| 187 | bool WebAssemblyExplicitLocals::runOnMachineFunction(MachineFunction &MF) { |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 188 | LLVM_DEBUG(dbgs() << "********** Make Locals Explicit **********\n" |
| 189 | "********** Function: " |
| 190 | << MF.getName() << '\n'); |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 191 | |
| Dan Gohman | 7d7409e | 2017-02-28 23:37:04 +0000 | [diff] [blame] | 192 | // Disable this pass if directed to do so. |
| Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 193 | if (WasmDisableExplicitLocals) |
| Dan Gohman | 7d7409e | 2017-02-28 23:37:04 +0000 | [diff] [blame] | 194 | return false; |
| 195 | |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 196 | bool Changed = false; |
| 197 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 198 | WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>(); |
| 199 | const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo(); |
| 200 | |
| 201 | // Map non-stackified virtual registers to their local ids. |
| 202 | DenseMap<unsigned, unsigned> Reg2Local; |
| 203 | |
| 204 | // Handle ARGUMENTS first to ensure that they get the designated numbers. |
| 205 | for (MachineBasicBlock::iterator I = MF.begin()->begin(), |
| 206 | E = MF.begin()->end(); |
| 207 | I != E;) { |
| 208 | MachineInstr &MI = *I++; |
| Wouter van Oortmerssen | d8ddf83 | 2019-07-12 22:08:25 +0000 | [diff] [blame] | 209 | if (!WebAssembly::isArgument(MI.getOpcode())) |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 210 | break; |
| Daniel Sanders | 05c145d | 2019-08-12 22:40:45 +0000 | [diff] [blame] | 211 | Register Reg = MI.getOperand(0).getReg(); |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 212 | assert(!MFI.isVRegStackified(Reg)); |
| Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 213 | Reg2Local[Reg] = static_cast<unsigned>(MI.getOperand(1).getImm()); |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 214 | MI.eraseFromParent(); |
| 215 | Changed = true; |
| 216 | } |
| 217 | |
| 218 | // Start assigning local numbers after the last parameter. |
| Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 219 | unsigned CurLocal = static_cast<unsigned>(MFI.getParams().size()); |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 220 | |
| Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 221 | // Precompute the set of registers that are unused, so that we can insert |
| 222 | // drops to their defs. |
| 223 | BitVector UseEmpty(MRI.getNumVirtRegs()); |
| Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 224 | for (unsigned I = 0, E = MRI.getNumVirtRegs(); I < E; ++I) |
| Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 225 | UseEmpty[I] = MRI.use_empty(Register::index2VirtReg(I)); |
| Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 226 | |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 227 | // Visit each instruction in the function. |
| 228 | for (MachineBasicBlock &MBB : MF) { |
| 229 | for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E;) { |
| 230 | MachineInstr &MI = *I++; |
| Wouter van Oortmerssen | d8ddf83 | 2019-07-12 22:08:25 +0000 | [diff] [blame] | 231 | assert(!WebAssembly::isArgument(MI.getOpcode())); |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 232 | |
| Shiva Chen | 801bf7e | 2018-05-09 02:42:00 +0000 | [diff] [blame] | 233 | if (MI.isDebugInstr() || MI.isLabel()) |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 234 | continue; |
| 235 | |
| Thomas Lively | 6a87dda | 2019-01-08 06:25:55 +0000 | [diff] [blame] | 236 | // Replace tee instructions with local.tee. The difference is that tee |
| 237 | // instructions have two defs, while local.tee instructions have one def |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 238 | // and an index of a local to write to. |
| Wouter van Oortmerssen | d8ddf83 | 2019-07-12 22:08:25 +0000 | [diff] [blame] | 239 | if (WebAssembly::isTee(MI.getOpcode())) { |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 240 | assert(MFI.isVRegStackified(MI.getOperand(0).getReg())); |
| 241 | assert(!MFI.isVRegStackified(MI.getOperand(1).getReg())); |
| Daniel Sanders | 05c145d | 2019-08-12 22:40:45 +0000 | [diff] [blame] | 242 | Register OldReg = MI.getOperand(2).getReg(); |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 243 | const TargetRegisterClass *RC = MRI.getRegClass(OldReg); |
| 244 | |
| 245 | // Stackify the input if it isn't stackified yet. |
| 246 | if (!MFI.isVRegStackified(OldReg)) { |
| 247 | unsigned LocalId = getLocalId(Reg2Local, CurLocal, OldReg); |
| Daniel Sanders | 05c145d | 2019-08-12 22:40:45 +0000 | [diff] [blame] | 248 | Register NewReg = MRI.createVirtualRegister(RC); |
| Heejin Ahn | 34dc1f2 | 2019-03-19 05:07:33 +0000 | [diff] [blame] | 249 | unsigned Opc = getLocalGetOpcode(RC); |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 250 | BuildMI(MBB, &MI, MI.getDebugLoc(), TII->get(Opc), NewReg) |
| 251 | .addImm(LocalId); |
| 252 | MI.getOperand(2).setReg(NewReg); |
| 253 | MFI.stackifyVReg(NewReg); |
| 254 | } |
| 255 | |
| Thomas Lively | 6a87dda | 2019-01-08 06:25:55 +0000 | [diff] [blame] | 256 | // Replace the TEE with a LOCAL_TEE. |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 257 | unsigned LocalId = |
| 258 | getLocalId(Reg2Local, CurLocal, MI.getOperand(1).getReg()); |
| Heejin Ahn | 34dc1f2 | 2019-03-19 05:07:33 +0000 | [diff] [blame] | 259 | unsigned Opc = getLocalTeeOpcode(RC); |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 260 | BuildMI(MBB, &MI, MI.getDebugLoc(), TII->get(Opc), |
| 261 | MI.getOperand(0).getReg()) |
| 262 | .addImm(LocalId) |
| 263 | .addReg(MI.getOperand(2).getReg()); |
| 264 | |
| Yury Delendik | adf7a0a | 2019-12-20 14:31:56 -0800 | [diff] [blame] | 265 | WebAssemblyDebugValueManager(&MI).replaceWithLocal(LocalId); |
| 266 | |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 267 | MI.eraseFromParent(); |
| 268 | Changed = true; |
| 269 | continue; |
| 270 | } |
| 271 | |
| Thomas Lively | 6a87dda | 2019-01-08 06:25:55 +0000 | [diff] [blame] | 272 | // Insert local.sets for any defs that aren't stackified yet. Currently |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 273 | // we handle at most one def. |
| 274 | assert(MI.getDesc().getNumDefs() <= 1); |
| 275 | if (MI.getDesc().getNumDefs() == 1) { |
| Daniel Sanders | 05c145d | 2019-08-12 22:40:45 +0000 | [diff] [blame] | 276 | Register OldReg = MI.getOperand(0).getReg(); |
| Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 277 | if (!MFI.isVRegStackified(OldReg)) { |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 278 | const TargetRegisterClass *RC = MRI.getRegClass(OldReg); |
| Daniel Sanders | 05c145d | 2019-08-12 22:40:45 +0000 | [diff] [blame] | 279 | Register NewReg = MRI.createVirtualRegister(RC); |
| Heejin Ahn | 5c644c9 | 2019-03-05 21:05:09 +0000 | [diff] [blame] | 280 | auto InsertPt = std::next(MI.getIterator()); |
| Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 281 | if (MI.getOpcode() == WebAssembly::IMPLICIT_DEF) { |
| 282 | MI.eraseFromParent(); |
| 283 | Changed = true; |
| 284 | continue; |
| 285 | } |
| Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 286 | if (UseEmpty[Register::virtReg2Index(OldReg)]) { |
| Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 287 | unsigned Opc = getDropOpcode(RC); |
| Heejin Ahn | 891a747 | 2018-06-19 20:30:42 +0000 | [diff] [blame] | 288 | MachineInstr *Drop = |
| 289 | BuildMI(MBB, InsertPt, MI.getDebugLoc(), TII->get(Opc)) |
| 290 | .addReg(NewReg); |
| 291 | // After the drop instruction, this reg operand will not be used |
| 292 | Drop->getOperand(0).setIsKill(); |
| Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 293 | } else { |
| 294 | unsigned LocalId = getLocalId(Reg2Local, CurLocal, OldReg); |
| Heejin Ahn | 34dc1f2 | 2019-03-19 05:07:33 +0000 | [diff] [blame] | 295 | unsigned Opc = getLocalSetOpcode(RC); |
| Yury Delendik | adf7a0a | 2019-12-20 14:31:56 -0800 | [diff] [blame] | 296 | |
| 297 | WebAssemblyDebugValueManager(&MI).replaceWithLocal(LocalId); |
| 298 | |
| Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 299 | BuildMI(MBB, InsertPt, MI.getDebugLoc(), TII->get(Opc)) |
| 300 | .addImm(LocalId) |
| 301 | .addReg(NewReg); |
| 302 | } |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 303 | MI.getOperand(0).setReg(NewReg); |
| Heejin Ahn | 4d98dfb | 2018-12-29 02:42:04 +0000 | [diff] [blame] | 304 | // This register operand of the original instruction is now being used |
| Thomas Lively | 6a87dda | 2019-01-08 06:25:55 +0000 | [diff] [blame] | 305 | // by the inserted drop or local.set instruction, so make it not dead |
| Heejin Ahn | 4d98dfb | 2018-12-29 02:42:04 +0000 | [diff] [blame] | 306 | // yet. |
| Heejin Ahn | 891a747 | 2018-06-19 20:30:42 +0000 | [diff] [blame] | 307 | MI.getOperand(0).setIsDead(false); |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 308 | MFI.stackifyVReg(NewReg); |
| 309 | Changed = true; |
| 310 | } |
| 311 | } |
| 312 | |
| Thomas Lively | 6a87dda | 2019-01-08 06:25:55 +0000 | [diff] [blame] | 313 | // Insert local.gets for any uses that aren't stackified yet. |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 314 | MachineInstr *InsertPt = &MI; |
| 315 | for (MachineOperand &MO : reverse(MI.explicit_uses())) { |
| 316 | if (!MO.isReg()) |
| 317 | continue; |
| 318 | |
| Daniel Sanders | 05c145d | 2019-08-12 22:40:45 +0000 | [diff] [blame] | 319 | Register OldReg = MO.getReg(); |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 320 | |
| Dan Gohman | b465aa0 | 2017-11-08 19:18:08 +0000 | [diff] [blame] | 321 | // Inline asm may have a def in the middle of the operands. Our contract |
| 322 | // with inline asm register operands is to provide local indices as |
| 323 | // immediates. |
| 324 | if (MO.isDef()) { |
| Craig Topper | c45e39b | 2019-02-04 21:24:13 +0000 | [diff] [blame] | 325 | assert(MI.isInlineAsm()); |
| Dan Gohman | b465aa0 | 2017-11-08 19:18:08 +0000 | [diff] [blame] | 326 | unsigned LocalId = getLocalId(Reg2Local, CurLocal, OldReg); |
| Heejin Ahn | 300f42f | 2018-09-12 21:34:39 +0000 | [diff] [blame] | 327 | // If this register operand is tied to another operand, we can't |
| 328 | // change it to an immediate. Untie it first. |
| 329 | MI.untieRegOperand(MI.getOperandNo(&MO)); |
| Dan Gohman | 045a217 | 2018-09-04 17:46:12 +0000 | [diff] [blame] | 330 | MO.ChangeToImmediate(LocalId); |
| Dan Gohman | b465aa0 | 2017-11-08 19:18:08 +0000 | [diff] [blame] | 331 | continue; |
| 332 | } |
| 333 | |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 334 | // If we see a stackified register, prepare to insert subsequent |
| Thomas Lively | 6a87dda | 2019-01-08 06:25:55 +0000 | [diff] [blame] | 335 | // local.gets before the start of its tree. |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 336 | if (MFI.isVRegStackified(OldReg)) { |
| Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 337 | InsertPt = findStartOfTree(MO, MRI, MFI); |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 338 | continue; |
| 339 | } |
| 340 | |
| Dan Gohman | b465aa0 | 2017-11-08 19:18:08 +0000 | [diff] [blame] | 341 | // Our contract with inline asm register operands is to provide local |
| 342 | // indices as immediates. |
| Craig Topper | c45e39b | 2019-02-04 21:24:13 +0000 | [diff] [blame] | 343 | if (MI.isInlineAsm()) { |
| Dan Gohman | b465aa0 | 2017-11-08 19:18:08 +0000 | [diff] [blame] | 344 | unsigned LocalId = getLocalId(Reg2Local, CurLocal, OldReg); |
| Heejin Ahn | 300f42f | 2018-09-12 21:34:39 +0000 | [diff] [blame] | 345 | // Untie it first if this reg operand is tied to another operand. |
| 346 | MI.untieRegOperand(MI.getOperandNo(&MO)); |
| Dan Gohman | 045a217 | 2018-09-04 17:46:12 +0000 | [diff] [blame] | 347 | MO.ChangeToImmediate(LocalId); |
| Dan Gohman | b465aa0 | 2017-11-08 19:18:08 +0000 | [diff] [blame] | 348 | continue; |
| 349 | } |
| 350 | |
| Thomas Lively | 6a87dda | 2019-01-08 06:25:55 +0000 | [diff] [blame] | 351 | // Insert a local.get. |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 352 | unsigned LocalId = getLocalId(Reg2Local, CurLocal, OldReg); |
| 353 | const TargetRegisterClass *RC = MRI.getRegClass(OldReg); |
| Daniel Sanders | 05c145d | 2019-08-12 22:40:45 +0000 | [diff] [blame] | 354 | Register NewReg = MRI.createVirtualRegister(RC); |
| Heejin Ahn | 34dc1f2 | 2019-03-19 05:07:33 +0000 | [diff] [blame] | 355 | unsigned Opc = getLocalGetOpcode(RC); |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 356 | InsertPt = |
| 357 | BuildMI(MBB, InsertPt, MI.getDebugLoc(), TII->get(Opc), NewReg) |
| 358 | .addImm(LocalId); |
| 359 | MO.setReg(NewReg); |
| 360 | MFI.stackifyVReg(NewReg); |
| 361 | Changed = true; |
| 362 | } |
| 363 | |
| 364 | // Coalesce and eliminate COPY instructions. |
| Wouter van Oortmerssen | d8ddf83 | 2019-07-12 22:08:25 +0000 | [diff] [blame] | 365 | if (WebAssembly::isCopy(MI.getOpcode())) { |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 366 | MRI.replaceRegWith(MI.getOperand(1).getReg(), |
| 367 | MI.getOperand(0).getReg()); |
| 368 | MI.eraseFromParent(); |
| 369 | Changed = true; |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | |
| Dan Gohman | 3acb187 | 2016-10-24 23:27:49 +0000 | [diff] [blame] | 374 | // Define the locals. |
| Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 375 | // TODO: Sort the locals for better compression. |
| 376 | MFI.setNumLocals(CurLocal - MFI.getParams().size()); |
| Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 377 | for (unsigned I = 0, E = MRI.getNumVirtRegs(); I < E; ++I) { |
| Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 378 | unsigned Reg = Register::index2VirtReg(I); |
| Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 379 | auto RL = Reg2Local.find(Reg); |
| 380 | if (RL == Reg2Local.end() || RL->second < MFI.getParams().size()) |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 381 | continue; |
| 382 | |
| Wouter van Oortmerssen | 8a9cb24 | 2018-08-27 15:45:51 +0000 | [diff] [blame] | 383 | MFI.setLocal(RL->second - MFI.getParams().size(), |
| Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 384 | typeForRegClass(MRI.getRegClass(Reg))); |
| Dan Gohman | 3acb187 | 2016-10-24 23:27:49 +0000 | [diff] [blame] | 385 | Changed = true; |
| Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 386 | } |
| 387 | |
| Wouter van Oortmerssen | a7be375 | 2018-08-13 23:12:49 +0000 | [diff] [blame] | 388 | #ifndef NDEBUG |
| 389 | // Assert that all registers have been stackified at this point. |
| 390 | for (const MachineBasicBlock &MBB : MF) { |
| 391 | for (const MachineInstr &MI : MBB) { |
| 392 | if (MI.isDebugInstr() || MI.isLabel()) |
| 393 | continue; |
| 394 | for (const MachineOperand &MO : MI.explicit_operands()) { |
| 395 | assert( |
| 396 | (!MO.isReg() || MRI.use_empty(MO.getReg()) || |
| 397 | MFI.isVRegStackified(MO.getReg())) && |
| 398 | "WebAssemblyExplicitLocals failed to stackify a register operand"); |
| 399 | } |
| 400 | } |
| Wouter van Oortmerssen | ab26bd0 | 2018-08-10 21:32:47 +0000 | [diff] [blame] | 401 | } |
| Wouter van Oortmerssen | a7be375 | 2018-08-13 23:12:49 +0000 | [diff] [blame] | 402 | #endif |
| 403 | |
| 404 | return Changed; |
| Wouter van Oortmerssen | ab26bd0 | 2018-08-10 21:32:47 +0000 | [diff] [blame] | 405 | } |