Dan Gohman | 1462faa | 2015-11-16 16:18:28 +0000 | [diff] [blame] | 1 | //===-- WebAssemblyRegStackify.cpp - Register Stackification --------------===// |
| 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 | 1462faa | 2015-11-16 16:18:28 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// |
| 9 | /// \file |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 10 | /// This file implements a register stacking pass. |
Dan Gohman | 1462faa | 2015-11-16 16:18:28 +0000 | [diff] [blame] | 11 | /// |
| 12 | /// This pass reorders instructions to put register uses and defs in an order |
| 13 | /// such that they form single-use expression trees. Registers fitting this form |
| 14 | /// are then marked as "stackified", meaning references to them are replaced by |
Dan Gohman | e040533 | 2016-10-03 22:43:53 +0000 | [diff] [blame] | 15 | /// "push" and "pop" from the value stack. |
Dan Gohman | 1462faa | 2015-11-16 16:18:28 +0000 | [diff] [blame] | 16 | /// |
Dan Gohman | 31448f1 | 2015-12-08 03:43:03 +0000 | [diff] [blame] | 17 | /// This is primarily a code size optimization, since temporary values on the |
Dan Gohman | e040533 | 2016-10-03 22:43:53 +0000 | [diff] [blame] | 18 | /// value stack don't need to be named. |
Dan Gohman | 1462faa | 2015-11-16 16:18:28 +0000 | [diff] [blame] | 19 | /// |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 22 | #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" // for WebAssembly::ARGUMENT_* |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 23 | #include "WebAssembly.h" |
Yury Delendik | be24c02 | 2019-01-15 18:14:12 +0000 | [diff] [blame] | 24 | #include "WebAssemblyDebugValueManager.h" |
Dan Gohman | 7a6b982 | 2015-11-29 22:32:02 +0000 | [diff] [blame] | 25 | #include "WebAssemblyMachineFunctionInfo.h" |
Dan Gohman | b6fd39a | 2016-01-19 16:59:23 +0000 | [diff] [blame] | 26 | #include "WebAssemblySubtarget.h" |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 27 | #include "WebAssemblyUtilities.h" |
Yury Delendik | 7c18d60 | 2018-09-25 18:59:34 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/SmallPtrSet.h" |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 29 | #include "llvm/Analysis/AliasAnalysis.h" |
Matthias Braun | f842297 | 2017-12-13 02:51:04 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/LiveIntervals.h" |
Dan Gohman | 1462faa | 2015-11-16 16:18:28 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineBlockFrequencyInfo.h" |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineDominators.h" |
| 33 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Dan Gohman | 82607f5 | 2017-02-24 23:46:05 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/MachineModuleInfoImpls.h" |
Dan Gohman | 1462faa | 2015-11-16 16:18:28 +0000 | [diff] [blame] | 35 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 36 | #include "llvm/CodeGen/Passes.h" |
| 37 | #include "llvm/Support/Debug.h" |
| 38 | #include "llvm/Support/raw_ostream.h" |
| 39 | using namespace llvm; |
| 40 | |
| 41 | #define DEBUG_TYPE "wasm-reg-stackify" |
| 42 | |
| 43 | namespace { |
| 44 | class WebAssemblyRegStackify final : public MachineFunctionPass { |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 45 | StringRef getPassName() const override { |
Dan Gohman | 1462faa | 2015-11-16 16:18:28 +0000 | [diff] [blame] | 46 | return "WebAssembly Register Stackify"; |
| 47 | } |
| 48 | |
| 49 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 50 | AU.setPreservesCFG(); |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 51 | AU.addRequired<AAResultsWrapperPass>(); |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 52 | AU.addRequired<MachineDominatorTree>(); |
Dan Gohman | 8887d1f | 2015-12-25 00:31:02 +0000 | [diff] [blame] | 53 | AU.addRequired<LiveIntervals>(); |
Dan Gohman | 1462faa | 2015-11-16 16:18:28 +0000 | [diff] [blame] | 54 | AU.addPreserved<MachineBlockFrequencyInfo>(); |
Dan Gohman | 8887d1f | 2015-12-25 00:31:02 +0000 | [diff] [blame] | 55 | AU.addPreserved<SlotIndexes>(); |
| 56 | AU.addPreserved<LiveIntervals>(); |
Dan Gohman | 8887d1f | 2015-12-25 00:31:02 +0000 | [diff] [blame] | 57 | AU.addPreservedID(LiveVariablesID); |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 58 | AU.addPreserved<MachineDominatorTree>(); |
Dan Gohman | 1462faa | 2015-11-16 16:18:28 +0000 | [diff] [blame] | 59 | MachineFunctionPass::getAnalysisUsage(AU); |
| 60 | } |
| 61 | |
| 62 | bool runOnMachineFunction(MachineFunction &MF) override; |
| 63 | |
| 64 | public: |
| 65 | static char ID; // Pass identification, replacement for typeid |
| 66 | WebAssemblyRegStackify() : MachineFunctionPass(ID) {} |
| 67 | }; |
| 68 | } // end anonymous namespace |
| 69 | |
| 70 | char WebAssemblyRegStackify::ID = 0; |
Jacob Gravelle | 4092645 | 2018-03-30 20:36:58 +0000 | [diff] [blame] | 71 | INITIALIZE_PASS(WebAssemblyRegStackify, DEBUG_TYPE, |
| 72 | "Reorder instructions to use the WebAssembly value stack", |
| 73 | false, false) |
| 74 | |
Dan Gohman | 1462faa | 2015-11-16 16:18:28 +0000 | [diff] [blame] | 75 | FunctionPass *llvm::createWebAssemblyRegStackify() { |
| 76 | return new WebAssemblyRegStackify(); |
| 77 | } |
| 78 | |
Dan Gohman | b0992da | 2015-11-20 02:19:12 +0000 | [diff] [blame] | 79 | // Decorate the given instruction with implicit operands that enforce the |
Dan Gohman | 8887d1f | 2015-12-25 00:31:02 +0000 | [diff] [blame] | 80 | // expression stack ordering constraints for an instruction which is on |
| 81 | // the expression stack. |
| 82 | static void ImposeStackOrdering(MachineInstr *MI) { |
Dan Gohman | e040533 | 2016-10-03 22:43:53 +0000 | [diff] [blame] | 83 | // Write the opaque VALUE_STACK register. |
| 84 | if (!MI->definesRegister(WebAssembly::VALUE_STACK)) |
| 85 | MI->addOperand(MachineOperand::CreateReg(WebAssembly::VALUE_STACK, |
Dan Gohman | 4da4abd | 2015-12-05 00:51:40 +0000 | [diff] [blame] | 86 | /*isDef=*/true, |
| 87 | /*isImp=*/true)); |
Dan Gohman | 4da4abd | 2015-12-05 00:51:40 +0000 | [diff] [blame] | 88 | |
Dan Gohman | e040533 | 2016-10-03 22:43:53 +0000 | [diff] [blame] | 89 | // Also read the opaque VALUE_STACK register. |
| 90 | if (!MI->readsRegister(WebAssembly::VALUE_STACK)) |
| 91 | MI->addOperand(MachineOperand::CreateReg(WebAssembly::VALUE_STACK, |
Dan Gohman | a712a6c | 2015-12-14 22:37:23 +0000 | [diff] [blame] | 92 | /*isDef=*/false, |
| 93 | /*isImp=*/true)); |
Dan Gohman | b0992da | 2015-11-20 02:19:12 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Dan Gohman | e81021a | 2016-11-08 19:40:38 +0000 | [diff] [blame] | 96 | // Convert an IMPLICIT_DEF instruction into an instruction which defines |
| 97 | // a constant zero value. |
| 98 | static void ConvertImplicitDefToConstZero(MachineInstr *MI, |
| 99 | MachineRegisterInfo &MRI, |
| 100 | const TargetInstrInfo *TII, |
Thomas Lively | feb18fe | 2018-12-20 04:20:32 +0000 | [diff] [blame] | 101 | MachineFunction &MF, |
| 102 | LiveIntervals &LIS) { |
Dan Gohman | e81021a | 2016-11-08 19:40:38 +0000 | [diff] [blame] | 103 | assert(MI->getOpcode() == TargetOpcode::IMPLICIT_DEF); |
| 104 | |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 105 | const auto *RegClass = MRI.getRegClass(MI->getOperand(0).getReg()); |
Dan Gohman | e81021a | 2016-11-08 19:40:38 +0000 | [diff] [blame] | 106 | if (RegClass == &WebAssembly::I32RegClass) { |
| 107 | MI->setDesc(TII->get(WebAssembly::CONST_I32)); |
| 108 | MI->addOperand(MachineOperand::CreateImm(0)); |
| 109 | } else if (RegClass == &WebAssembly::I64RegClass) { |
| 110 | MI->setDesc(TII->get(WebAssembly::CONST_I64)); |
| 111 | MI->addOperand(MachineOperand::CreateImm(0)); |
| 112 | } else if (RegClass == &WebAssembly::F32RegClass) { |
| 113 | MI->setDesc(TII->get(WebAssembly::CONST_F32)); |
| 114 | ConstantFP *Val = cast<ConstantFP>(Constant::getNullValue( |
David Blaikie | 2110924 | 2017-12-15 23:52:06 +0000 | [diff] [blame] | 115 | Type::getFloatTy(MF.getFunction().getContext()))); |
Dan Gohman | e81021a | 2016-11-08 19:40:38 +0000 | [diff] [blame] | 116 | MI->addOperand(MachineOperand::CreateFPImm(Val)); |
| 117 | } else if (RegClass == &WebAssembly::F64RegClass) { |
| 118 | MI->setDesc(TII->get(WebAssembly::CONST_F64)); |
| 119 | ConstantFP *Val = cast<ConstantFP>(Constant::getNullValue( |
David Blaikie | 2110924 | 2017-12-15 23:52:06 +0000 | [diff] [blame] | 120 | Type::getDoubleTy(MF.getFunction().getContext()))); |
Dan Gohman | e81021a | 2016-11-08 19:40:38 +0000 | [diff] [blame] | 121 | MI->addOperand(MachineOperand::CreateFPImm(Val)); |
Thomas Lively | 6ff31fe | 2018-10-31 23:50:53 +0000 | [diff] [blame] | 122 | } else if (RegClass == &WebAssembly::V128RegClass) { |
Thomas Lively | feb18fe | 2018-12-20 04:20:32 +0000 | [diff] [blame] | 123 | unsigned TempReg = MRI.createVirtualRegister(&WebAssembly::I32RegClass); |
| 124 | MI->setDesc(TII->get(WebAssembly::SPLAT_v4i32)); |
| 125 | MI->addOperand(MachineOperand::CreateReg(TempReg, false)); |
| 126 | MachineInstr *Const = BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), |
| 127 | TII->get(WebAssembly::CONST_I32), TempReg) |
| 128 | .addImm(0); |
| 129 | LIS.InsertMachineInstrInMaps(*Const); |
Dan Gohman | e81021a | 2016-11-08 19:40:38 +0000 | [diff] [blame] | 130 | } else { |
| 131 | llvm_unreachable("Unexpected reg class"); |
| 132 | } |
| 133 | } |
| 134 | |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 135 | // Determine whether a call to the callee referenced by |
| 136 | // MI->getOperand(CalleeOpNo) reads memory, writes memory, and/or has side |
| 137 | // effects. |
Duncan P. N. Exon Smith | 500d046 | 2016-07-08 19:36:40 +0000 | [diff] [blame] | 138 | static void QueryCallee(const MachineInstr &MI, unsigned CalleeOpNo, bool &Read, |
| 139 | bool &Write, bool &Effects, bool &StackPointer) { |
Dan Gohman | d08cd15 | 2016-05-17 21:14:26 +0000 | [diff] [blame] | 140 | // All calls can use the stack pointer. |
| 141 | StackPointer = true; |
| 142 | |
Duncan P. N. Exon Smith | 500d046 | 2016-07-08 19:36:40 +0000 | [diff] [blame] | 143 | const MachineOperand &MO = MI.getOperand(CalleeOpNo); |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 144 | if (MO.isGlobal()) { |
| 145 | const Constant *GV = MO.getGlobal(); |
| 146 | if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) |
| 147 | if (!GA->isInterposable()) |
| 148 | GV = GA->getAliasee(); |
| 149 | |
| 150 | if (const Function *F = dyn_cast<Function>(GV)) { |
| 151 | if (!F->doesNotThrow()) |
| 152 | Effects = true; |
| 153 | if (F->doesNotAccessMemory()) |
| 154 | return; |
| 155 | if (F->onlyReadsMemory()) { |
| 156 | Read = true; |
| 157 | return; |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // Assume the worst. |
| 163 | Write = true; |
| 164 | Read = true; |
| 165 | Effects = true; |
| 166 | } |
| 167 | |
Dan Gohman | d08cd15 | 2016-05-17 21:14:26 +0000 | [diff] [blame] | 168 | // Determine whether MI reads memory, writes memory, has side effects, |
Dan Gohman | 82607f5 | 2017-02-24 23:46:05 +0000 | [diff] [blame] | 169 | // and/or uses the stack pointer value. |
Duncan P. N. Exon Smith | 500d046 | 2016-07-08 19:36:40 +0000 | [diff] [blame] | 170 | static void Query(const MachineInstr &MI, AliasAnalysis &AA, bool &Read, |
| 171 | bool &Write, bool &Effects, bool &StackPointer) { |
Duncan P. N. Exon Smith | 500d046 | 2016-07-08 19:36:40 +0000 | [diff] [blame] | 172 | assert(!MI.isTerminator()); |
Dan Gohman | 6c8f20d | 2016-05-23 17:42:57 +0000 | [diff] [blame] | 173 | |
Heejin Ahn | 5ef4d5f | 2018-05-31 22:25:54 +0000 | [diff] [blame] | 174 | if (MI.isDebugInstr() || MI.isPosition()) |
Dan Gohman | 6c8f20d | 2016-05-23 17:42:57 +0000 | [diff] [blame] | 175 | return; |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 176 | |
| 177 | // Check for loads. |
Justin Lebar | d98cf00 | 2016-09-10 01:03:20 +0000 | [diff] [blame] | 178 | if (MI.mayLoad() && !MI.isDereferenceableInvariantLoad(&AA)) |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 179 | Read = true; |
| 180 | |
| 181 | // Check for stores. |
Duncan P. N. Exon Smith | 500d046 | 2016-07-08 19:36:40 +0000 | [diff] [blame] | 182 | if (MI.mayStore()) { |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 183 | Write = true; |
Duncan P. N. Exon Smith | 500d046 | 2016-07-08 19:36:40 +0000 | [diff] [blame] | 184 | } else if (MI.hasOrderedMemoryRef()) { |
| 185 | switch (MI.getOpcode()) { |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 186 | case WebAssembly::DIV_S_I32: |
| 187 | case WebAssembly::DIV_S_I64: |
| 188 | case WebAssembly::REM_S_I32: |
| 189 | case WebAssembly::REM_S_I64: |
| 190 | case WebAssembly::DIV_U_I32: |
| 191 | case WebAssembly::DIV_U_I64: |
| 192 | case WebAssembly::REM_U_I32: |
| 193 | case WebAssembly::REM_U_I64: |
| 194 | case WebAssembly::I32_TRUNC_S_F32: |
| 195 | case WebAssembly::I64_TRUNC_S_F32: |
| 196 | case WebAssembly::I32_TRUNC_S_F64: |
| 197 | case WebAssembly::I64_TRUNC_S_F64: |
| 198 | case WebAssembly::I32_TRUNC_U_F32: |
| 199 | case WebAssembly::I64_TRUNC_U_F32: |
| 200 | case WebAssembly::I32_TRUNC_U_F64: |
| 201 | case WebAssembly::I64_TRUNC_U_F64: |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 202 | // These instruction have hasUnmodeledSideEffects() returning true |
| 203 | // because they trap on overflow and invalid so they can't be arbitrarily |
| 204 | // moved, however hasOrderedMemoryRef() interprets this plus their lack |
| 205 | // of memoperands as having a potential unknown memory reference. |
| 206 | break; |
| 207 | default: |
Dan Gohman | 1054570 | 2016-05-17 22:24:18 +0000 | [diff] [blame] | 208 | // Record volatile accesses, unless it's a call, as calls are handled |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 209 | // specially below. |
Duncan P. N. Exon Smith | 500d046 | 2016-07-08 19:36:40 +0000 | [diff] [blame] | 210 | if (!MI.isCall()) { |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 211 | Write = true; |
Dan Gohman | 1054570 | 2016-05-17 22:24:18 +0000 | [diff] [blame] | 212 | Effects = true; |
| 213 | } |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 214 | break; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | // Check for side effects. |
Duncan P. N. Exon Smith | 500d046 | 2016-07-08 19:36:40 +0000 | [diff] [blame] | 219 | if (MI.hasUnmodeledSideEffects()) { |
| 220 | switch (MI.getOpcode()) { |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 221 | case WebAssembly::DIV_S_I32: |
| 222 | case WebAssembly::DIV_S_I64: |
| 223 | case WebAssembly::REM_S_I32: |
| 224 | case WebAssembly::REM_S_I64: |
| 225 | case WebAssembly::DIV_U_I32: |
| 226 | case WebAssembly::DIV_U_I64: |
| 227 | case WebAssembly::REM_U_I32: |
| 228 | case WebAssembly::REM_U_I64: |
| 229 | case WebAssembly::I32_TRUNC_S_F32: |
| 230 | case WebAssembly::I64_TRUNC_S_F32: |
| 231 | case WebAssembly::I32_TRUNC_S_F64: |
| 232 | case WebAssembly::I64_TRUNC_S_F64: |
| 233 | case WebAssembly::I32_TRUNC_U_F32: |
| 234 | case WebAssembly::I64_TRUNC_U_F32: |
| 235 | case WebAssembly::I32_TRUNC_U_F64: |
| 236 | case WebAssembly::I64_TRUNC_U_F64: |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 237 | // These instructions have hasUnmodeledSideEffects() returning true |
| 238 | // because they trap on overflow and invalid so they can't be arbitrarily |
| 239 | // moved, however in the specific case of register stackifying, it is safe |
| 240 | // to move them because overflow and invalid are Undefined Behavior. |
| 241 | break; |
| 242 | default: |
| 243 | Effects = true; |
| 244 | break; |
| 245 | } |
| 246 | } |
| 247 | |
Heejin Ahn | e73c7a1 | 2019-01-10 23:12:07 +0000 | [diff] [blame] | 248 | // Check for writes to __stack_pointer global. |
| 249 | if (MI.getOpcode() == WebAssembly::GLOBAL_SET_I32 && |
| 250 | strcmp(MI.getOperand(0).getSymbolName(), "__stack_pointer") == 0) |
| 251 | StackPointer = true; |
| 252 | |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 253 | // Analyze calls. |
Duncan P. N. Exon Smith | 500d046 | 2016-07-08 19:36:40 +0000 | [diff] [blame] | 254 | if (MI.isCall()) { |
Heejin Ahn | 56e79dd | 2018-08-28 17:49:39 +0000 | [diff] [blame] | 255 | unsigned CalleeOpNo = WebAssembly::getCalleeOpNo(MI); |
| 256 | QueryCallee(MI, CalleeOpNo, Read, Write, Effects, StackPointer); |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
| 260 | // Test whether Def is safe and profitable to rematerialize. |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 261 | static bool ShouldRematerialize(const MachineInstr &Def, AliasAnalysis &AA, |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 262 | const WebAssemblyInstrInfo *TII) { |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 263 | return Def.isAsCheapAsAMove() && TII->isTriviallyReMaterializable(Def, &AA); |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Dan Gohman | 12de0b9 | 2016-05-17 20:19:47 +0000 | [diff] [blame] | 266 | // Identify the definition for this register at this point. This is a |
| 267 | // generalization of MachineRegisterInfo::getUniqueVRegDef that uses |
| 268 | // LiveIntervals to handle complex cases. |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 269 | static MachineInstr *GetVRegDef(unsigned Reg, const MachineInstr *Insert, |
| 270 | const MachineRegisterInfo &MRI, |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 271 | const LiveIntervals &LIS) { |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 272 | // Most registers are in SSA form here so we try a quick MRI query first. |
| 273 | if (MachineInstr *Def = MRI.getUniqueVRegDef(Reg)) |
| 274 | return Def; |
| 275 | |
| 276 | // MRI doesn't know what the Def is. Try asking LIS. |
| 277 | if (const VNInfo *ValNo = LIS.getInterval(Reg).getVNInfoBefore( |
| 278 | LIS.getInstructionIndex(*Insert))) |
| 279 | return LIS.getInstructionFromIndex(ValNo->def); |
| 280 | |
| 281 | return nullptr; |
| 282 | } |
| 283 | |
Dan Gohman | 12de0b9 | 2016-05-17 20:19:47 +0000 | [diff] [blame] | 284 | // Test whether Reg, as defined at Def, has exactly one use. This is a |
| 285 | // generalization of MachineRegisterInfo::hasOneUse that uses LiveIntervals |
| 286 | // to handle complex cases. |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 287 | static bool HasOneUse(unsigned Reg, MachineInstr *Def, MachineRegisterInfo &MRI, |
| 288 | MachineDominatorTree &MDT, LiveIntervals &LIS) { |
Dan Gohman | 12de0b9 | 2016-05-17 20:19:47 +0000 | [diff] [blame] | 289 | // Most registers are in SSA form here so we try a quick MRI query first. |
| 290 | if (MRI.hasOneUse(Reg)) |
| 291 | return true; |
| 292 | |
| 293 | bool HasOne = false; |
| 294 | const LiveInterval &LI = LIS.getInterval(Reg); |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 295 | const VNInfo *DefVNI = |
| 296 | LI.getVNInfoAt(LIS.getInstructionIndex(*Def).getRegSlot()); |
Dan Gohman | 12de0b9 | 2016-05-17 20:19:47 +0000 | [diff] [blame] | 297 | assert(DefVNI); |
Dominic Chen | a8a6382 | 2016-08-17 23:42:27 +0000 | [diff] [blame] | 298 | for (auto &I : MRI.use_nodbg_operands(Reg)) { |
Dan Gohman | 12de0b9 | 2016-05-17 20:19:47 +0000 | [diff] [blame] | 299 | const auto &Result = LI.Query(LIS.getInstructionIndex(*I.getParent())); |
| 300 | if (Result.valueIn() == DefVNI) { |
| 301 | if (!Result.isKill()) |
| 302 | return false; |
| 303 | if (HasOne) |
| 304 | return false; |
| 305 | HasOne = true; |
| 306 | } |
| 307 | } |
| 308 | return HasOne; |
| 309 | } |
| 310 | |
Dan Gohman | 8887d1f | 2015-12-25 00:31:02 +0000 | [diff] [blame] | 311 | // Test whether it's safe to move Def to just before Insert. |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 312 | // TODO: Compute memory dependencies in a way that doesn't require always |
| 313 | // walking the block. |
| 314 | // TODO: Compute memory dependencies in a way that uses AliasAnalysis to be |
| 315 | // more precise. |
| 316 | static bool IsSafeToMove(const MachineInstr *Def, const MachineInstr *Insert, |
Derek Schuff | e9e6891 | 2016-09-30 18:02:54 +0000 | [diff] [blame] | 317 | AliasAnalysis &AA, const MachineRegisterInfo &MRI) { |
Dan Gohman | 391a98a | 2015-12-03 23:07:03 +0000 | [diff] [blame] | 318 | assert(Def->getParent() == Insert->getParent()); |
Dan Gohman | 8887d1f | 2015-12-25 00:31:02 +0000 | [diff] [blame] | 319 | |
| 320 | // Check for register dependencies. |
Derek Schuff | e9e6891 | 2016-09-30 18:02:54 +0000 | [diff] [blame] | 321 | SmallVector<unsigned, 4> MutableRegisters; |
Dan Gohman | 8887d1f | 2015-12-25 00:31:02 +0000 | [diff] [blame] | 322 | for (const MachineOperand &MO : Def->operands()) { |
| 323 | if (!MO.isReg() || MO.isUndef()) |
| 324 | continue; |
| 325 | unsigned Reg = MO.getReg(); |
| 326 | |
| 327 | // If the register is dead here and at Insert, ignore it. |
| 328 | if (MO.isDead() && Insert->definesRegister(Reg) && |
| 329 | !Insert->readsRegister(Reg)) |
| 330 | continue; |
| 331 | |
| 332 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) { |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 333 | // Ignore ARGUMENTS; it's just used to keep the ARGUMENT_* instructions |
| 334 | // from moving down, and we've already checked for that. |
| 335 | if (Reg == WebAssembly::ARGUMENTS) |
| 336 | continue; |
Dan Gohman | 8887d1f | 2015-12-25 00:31:02 +0000 | [diff] [blame] | 337 | // If the physical register is never modified, ignore it. |
| 338 | if (!MRI.isPhysRegModified(Reg)) |
| 339 | continue; |
| 340 | // Otherwise, it's a physical register with unknown liveness. |
| 341 | return false; |
| 342 | } |
| 343 | |
Derek Schuff | e9e6891 | 2016-09-30 18:02:54 +0000 | [diff] [blame] | 344 | // If one of the operands isn't in SSA form, it has different values at |
| 345 | // different times, and we need to make sure we don't move our use across |
| 346 | // a different def. |
| 347 | if (!MO.isDef() && !MRI.hasOneDef(Reg)) |
| 348 | MutableRegisters.push_back(Reg); |
Dan Gohman | 8887d1f | 2015-12-25 00:31:02 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Dan Gohman | d08cd15 | 2016-05-17 21:14:26 +0000 | [diff] [blame] | 351 | bool Read = false, Write = false, Effects = false, StackPointer = false; |
Duncan P. N. Exon Smith | 500d046 | 2016-07-08 19:36:40 +0000 | [diff] [blame] | 352 | Query(*Def, AA, Read, Write, Effects, StackPointer); |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 353 | |
| 354 | // If the instruction does not access memory and has no side effects, it has |
| 355 | // no additional dependencies. |
Derek Schuff | e9e6891 | 2016-09-30 18:02:54 +0000 | [diff] [blame] | 356 | bool HasMutableRegisters = !MutableRegisters.empty(); |
| 357 | if (!Read && !Write && !Effects && !StackPointer && !HasMutableRegisters) |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 358 | return true; |
| 359 | |
| 360 | // Scan through the intervening instructions between Def and Insert. |
| 361 | MachineBasicBlock::const_iterator D(Def), I(Insert); |
| 362 | for (--I; I != D; --I) { |
| 363 | bool InterveningRead = false; |
| 364 | bool InterveningWrite = false; |
| 365 | bool InterveningEffects = false; |
Dan Gohman | d08cd15 | 2016-05-17 21:14:26 +0000 | [diff] [blame] | 366 | bool InterveningStackPointer = false; |
Duncan P. N. Exon Smith | 500d046 | 2016-07-08 19:36:40 +0000 | [diff] [blame] | 367 | Query(*I, AA, InterveningRead, InterveningWrite, InterveningEffects, |
Dan Gohman | d08cd15 | 2016-05-17 21:14:26 +0000 | [diff] [blame] | 368 | InterveningStackPointer); |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 369 | if (Effects && InterveningEffects) |
| 370 | return false; |
| 371 | if (Read && InterveningWrite) |
| 372 | return false; |
| 373 | if (Write && (InterveningRead || InterveningWrite)) |
| 374 | return false; |
Dan Gohman | d08cd15 | 2016-05-17 21:14:26 +0000 | [diff] [blame] | 375 | if (StackPointer && InterveningStackPointer) |
| 376 | return false; |
Derek Schuff | e9e6891 | 2016-09-30 18:02:54 +0000 | [diff] [blame] | 377 | |
| 378 | for (unsigned Reg : MutableRegisters) |
| 379 | for (const MachineOperand &MO : I->operands()) |
| 380 | if (MO.isReg() && MO.isDef() && MO.getReg() == Reg) |
| 381 | return false; |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | return true; |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 387 | /// Test whether OneUse, a use of Reg, dominates all of Reg's other uses. |
| 388 | static bool OneUseDominatesOtherUses(unsigned Reg, const MachineOperand &OneUse, |
| 389 | const MachineBasicBlock &MBB, |
| 390 | const MachineRegisterInfo &MRI, |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 391 | const MachineDominatorTree &MDT, |
Dan Gohman | 1054570 | 2016-05-17 22:24:18 +0000 | [diff] [blame] | 392 | LiveIntervals &LIS, |
| 393 | WebAssemblyFunctionInfo &MFI) { |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 394 | const LiveInterval &LI = LIS.getInterval(Reg); |
| 395 | |
| 396 | const MachineInstr *OneUseInst = OneUse.getParent(); |
| 397 | VNInfo *OneUseVNI = LI.getVNInfoBefore(LIS.getInstructionIndex(*OneUseInst)); |
| 398 | |
Dominic Chen | a8a6382 | 2016-08-17 23:42:27 +0000 | [diff] [blame] | 399 | for (const MachineOperand &Use : MRI.use_nodbg_operands(Reg)) { |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 400 | if (&Use == &OneUse) |
| 401 | continue; |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 402 | |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 403 | const MachineInstr *UseInst = Use.getParent(); |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 404 | VNInfo *UseVNI = LI.getVNInfoBefore(LIS.getInstructionIndex(*UseInst)); |
| 405 | |
| 406 | if (UseVNI != OneUseVNI) |
| 407 | continue; |
| 408 | |
Dan Gohman | 12de0b9 | 2016-05-17 20:19:47 +0000 | [diff] [blame] | 409 | if (UseInst == OneUseInst) { |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 410 | // Another use in the same instruction. We need to ensure that the one |
| 411 | // selected use happens "before" it. |
| 412 | if (&OneUse > &Use) |
| 413 | return false; |
| 414 | } else { |
| 415 | // Test that the use is dominated by the one selected use. |
Dan Gohman | 1054570 | 2016-05-17 22:24:18 +0000 | [diff] [blame] | 416 | while (!MDT.dominates(OneUseInst, UseInst)) { |
| 417 | // Actually, dominating is over-conservative. Test that the use would |
| 418 | // happen after the one selected use in the stack evaluation order. |
| 419 | // |
Thomas Lively | 6a87dda | 2019-01-08 06:25:55 +0000 | [diff] [blame] | 420 | // This is needed as a consequence of using implicit local.gets for |
| 421 | // uses and implicit local.sets for defs. |
Dominic Chen | 4173fff | 2016-08-11 04:10:56 +0000 | [diff] [blame] | 422 | if (UseInst->getDesc().getNumDefs() == 0) |
Dan Gohman | 1054570 | 2016-05-17 22:24:18 +0000 | [diff] [blame] | 423 | return false; |
| 424 | const MachineOperand &MO = UseInst->getOperand(0); |
| 425 | if (!MO.isReg()) |
| 426 | return false; |
| 427 | unsigned DefReg = MO.getReg(); |
| 428 | if (!TargetRegisterInfo::isVirtualRegister(DefReg) || |
| 429 | !MFI.isVRegStackified(DefReg)) |
| 430 | return false; |
Yury Delendik | b3857e4 | 2018-09-26 23:49:21 +0000 | [diff] [blame] | 431 | assert(MRI.hasOneNonDBGUse(DefReg)); |
| 432 | const MachineOperand &NewUse = *MRI.use_nodbg_begin(DefReg); |
Dan Gohman | 1054570 | 2016-05-17 22:24:18 +0000 | [diff] [blame] | 433 | const MachineInstr *NewUseInst = NewUse.getParent(); |
| 434 | if (NewUseInst == OneUseInst) { |
| 435 | if (&OneUse > &NewUse) |
| 436 | return false; |
| 437 | break; |
| 438 | } |
| 439 | UseInst = NewUseInst; |
| 440 | } |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | return true; |
| 444 | } |
| 445 | |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 446 | /// Get the appropriate tee opcode for the given register class. |
| 447 | static unsigned GetTeeOpcode(const TargetRegisterClass *RC) { |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 448 | if (RC == &WebAssembly::I32RegClass) |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 449 | return WebAssembly::TEE_I32; |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 450 | if (RC == &WebAssembly::I64RegClass) |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 451 | return WebAssembly::TEE_I64; |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 452 | if (RC == &WebAssembly::F32RegClass) |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 453 | return WebAssembly::TEE_F32; |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 454 | if (RC == &WebAssembly::F64RegClass) |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 455 | return WebAssembly::TEE_F64; |
Derek Schuff | 39bf39f | 2016-08-02 23:16:09 +0000 | [diff] [blame] | 456 | if (RC == &WebAssembly::V128RegClass) |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 457 | return WebAssembly::TEE_V128; |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 458 | llvm_unreachable("Unexpected register class"); |
| 459 | } |
| 460 | |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 461 | // Shrink LI to its uses, cleaning up LI. |
| 462 | static void ShrinkToUses(LiveInterval &LI, LiveIntervals &LIS) { |
| 463 | if (LIS.shrinkToUses(&LI)) { |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 464 | SmallVector<LiveInterval *, 4> SplitLIs; |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 465 | LIS.splitSeparateComponents(LI, SplitLIs); |
| 466 | } |
| 467 | } |
| 468 | |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 469 | /// A single-use def in the same block with no intervening memory or register |
| 470 | /// dependencies; move the def down and nest it with the current instruction. |
Heejin Ahn | f208f63 | 2018-09-05 01:27:38 +0000 | [diff] [blame] | 471 | static MachineInstr *MoveForSingleUse(unsigned Reg, MachineOperand &Op, |
| 472 | MachineInstr *Def, MachineBasicBlock &MBB, |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 473 | MachineInstr *Insert, LiveIntervals &LIS, |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 474 | WebAssemblyFunctionInfo &MFI, |
| 475 | MachineRegisterInfo &MRI) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 476 | LLVM_DEBUG(dbgs() << "Move for single use: "; Def->dump()); |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 477 | |
Yury Delendik | be24c02 | 2019-01-15 18:14:12 +0000 | [diff] [blame] | 478 | WebAssemblyDebugValueManager DefDIs(Def); |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 479 | MBB.splice(Insert, &MBB, Def); |
Yury Delendik | be24c02 | 2019-01-15 18:14:12 +0000 | [diff] [blame] | 480 | DefDIs.move(Insert); |
JF Bastien | 1afd1e2 | 2016-02-28 15:33:53 +0000 | [diff] [blame] | 481 | LIS.handleMove(*Def); |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 482 | |
Dan Gohman | 12de0b9 | 2016-05-17 20:19:47 +0000 | [diff] [blame] | 483 | if (MRI.hasOneDef(Reg) && MRI.hasOneUse(Reg)) { |
| 484 | // No one else is using this register for anything so we can just stackify |
| 485 | // it in place. |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 486 | MFI.stackifyVReg(Reg); |
| 487 | } else { |
Dan Gohman | 12de0b9 | 2016-05-17 20:19:47 +0000 | [diff] [blame] | 488 | // The register may have unrelated uses or defs; create a new register for |
| 489 | // just our one def and use so that we can stackify it. |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 490 | unsigned NewReg = MRI.createVirtualRegister(MRI.getRegClass(Reg)); |
| 491 | Def->getOperand(0).setReg(NewReg); |
| 492 | Op.setReg(NewReg); |
| 493 | |
| 494 | // Tell LiveIntervals about the new register. |
| 495 | LIS.createAndComputeVirtRegInterval(NewReg); |
| 496 | |
| 497 | // Tell LiveIntervals about the changes to the old register. |
| 498 | LiveInterval &LI = LIS.getInterval(Reg); |
Dan Gohman | 6c8f20d | 2016-05-23 17:42:57 +0000 | [diff] [blame] | 499 | LI.removeSegment(LIS.getInstructionIndex(*Def).getRegSlot(), |
| 500 | LIS.getInstructionIndex(*Op.getParent()).getRegSlot(), |
| 501 | /*RemoveDeadValNo=*/true); |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 502 | |
| 503 | MFI.stackifyVReg(NewReg); |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 504 | |
Yury Delendik | be24c02 | 2019-01-15 18:14:12 +0000 | [diff] [blame] | 505 | DefDIs.updateReg(NewReg); |
Yury Delendik | 7c18d60 | 2018-09-25 18:59:34 +0000 | [diff] [blame] | 506 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 507 | LLVM_DEBUG(dbgs() << " - Replaced register: "; Def->dump()); |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 510 | ImposeStackOrdering(Def); |
| 511 | return Def; |
| 512 | } |
| 513 | |
| 514 | /// A trivially cloneable instruction; clone it and nest the new copy with the |
| 515 | /// current instruction. |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 516 | static MachineInstr *RematerializeCheapDef( |
| 517 | unsigned Reg, MachineOperand &Op, MachineInstr &Def, MachineBasicBlock &MBB, |
| 518 | MachineBasicBlock::instr_iterator Insert, LiveIntervals &LIS, |
| 519 | WebAssemblyFunctionInfo &MFI, MachineRegisterInfo &MRI, |
| 520 | const WebAssemblyInstrInfo *TII, const WebAssemblyRegisterInfo *TRI) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 521 | LLVM_DEBUG(dbgs() << "Rematerializing cheap def: "; Def.dump()); |
| 522 | LLVM_DEBUG(dbgs() << " - for use in "; Op.getParent()->dump()); |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 523 | |
Yury Delendik | be24c02 | 2019-01-15 18:14:12 +0000 | [diff] [blame] | 524 | WebAssemblyDebugValueManager DefDIs(&Def); |
| 525 | |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 526 | unsigned NewReg = MRI.createVirtualRegister(MRI.getRegClass(Reg)); |
| 527 | TII->reMaterialize(MBB, Insert, NewReg, 0, Def, *TRI); |
| 528 | Op.setReg(NewReg); |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 529 | MachineInstr *Clone = &*std::prev(Insert); |
JF Bastien | 13d3b9b | 2016-02-27 16:38:23 +0000 | [diff] [blame] | 530 | LIS.InsertMachineInstrInMaps(*Clone); |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 531 | LIS.createAndComputeVirtRegInterval(NewReg); |
| 532 | MFI.stackifyVReg(NewReg); |
| 533 | ImposeStackOrdering(Clone); |
| 534 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 535 | LLVM_DEBUG(dbgs() << " - Cloned to "; Clone->dump()); |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 536 | |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 537 | // Shrink the interval. |
| 538 | bool IsDead = MRI.use_empty(Reg); |
| 539 | if (!IsDead) { |
| 540 | LiveInterval &LI = LIS.getInterval(Reg); |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 541 | ShrinkToUses(LI, LIS); |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 542 | IsDead = !LI.liveAt(LIS.getInstructionIndex(Def).getDeadSlot()); |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 545 | // If that was the last use of the original, delete the original. |
Yury Delendik | 7c18d60 | 2018-09-25 18:59:34 +0000 | [diff] [blame] | 546 | // Move or clone corresponding DBG_VALUEs to the 'Insert' location. |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 547 | if (IsDead) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 548 | LLVM_DEBUG(dbgs() << " - Deleting original\n"); |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 549 | SlotIndex Idx = LIS.getInstructionIndex(Def).getRegSlot(); |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 550 | LIS.removePhysRegDefAt(WebAssembly::ARGUMENTS, Idx); |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 551 | LIS.removeInterval(Reg); |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 552 | LIS.RemoveMachineInstrFromMaps(Def); |
| 553 | Def.eraseFromParent(); |
Yury Delendik | 7c18d60 | 2018-09-25 18:59:34 +0000 | [diff] [blame] | 554 | |
Yury Delendik | be24c02 | 2019-01-15 18:14:12 +0000 | [diff] [blame] | 555 | DefDIs.move(&*Insert); |
| 556 | DefDIs.updateReg(NewReg); |
Yury Delendik | 7c18d60 | 2018-09-25 18:59:34 +0000 | [diff] [blame] | 557 | } else { |
Yury Delendik | be24c02 | 2019-01-15 18:14:12 +0000 | [diff] [blame] | 558 | DefDIs.clone(&*Insert, NewReg); |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 559 | } |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 560 | |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 561 | return Clone; |
| 562 | } |
| 563 | |
| 564 | /// A multiple-use def in the same block with no intervening memory or register |
| 565 | /// dependencies; move the def down, nest it with the current instruction, and |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 566 | /// insert a tee to satisfy the rest of the uses. As an illustration, rewrite |
| 567 | /// this: |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 568 | /// |
| 569 | /// Reg = INST ... // Def |
| 570 | /// INST ..., Reg, ... // Insert |
| 571 | /// INST ..., Reg, ... |
| 572 | /// INST ..., Reg, ... |
| 573 | /// |
| 574 | /// to this: |
| 575 | /// |
Dan Gohman | 8aa237c | 2016-02-16 15:17:21 +0000 | [diff] [blame] | 576 | /// DefReg = INST ... // Def (to become the new Insert) |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 577 | /// TeeReg, Reg = TEE_... DefReg |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 578 | /// INST ..., TeeReg, ... // Insert |
Dan Gohman | 6c8f20d | 2016-05-23 17:42:57 +0000 | [diff] [blame] | 579 | /// INST ..., Reg, ... |
| 580 | /// INST ..., Reg, ... |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 581 | /// |
Thomas Lively | 6a87dda | 2019-01-08 06:25:55 +0000 | [diff] [blame] | 582 | /// with DefReg and TeeReg stackified. This eliminates a local.get from the |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 583 | /// resulting code. |
| 584 | static MachineInstr *MoveAndTeeForMultiUse( |
| 585 | unsigned Reg, MachineOperand &Op, MachineInstr *Def, MachineBasicBlock &MBB, |
| 586 | MachineInstr *Insert, LiveIntervals &LIS, WebAssemblyFunctionInfo &MFI, |
| 587 | MachineRegisterInfo &MRI, const WebAssemblyInstrInfo *TII) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 588 | LLVM_DEBUG(dbgs() << "Move and tee for multi-use:"; Def->dump()); |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 589 | |
Yury Delendik | be24c02 | 2019-01-15 18:14:12 +0000 | [diff] [blame] | 590 | WebAssemblyDebugValueManager DefDIs(Def); |
| 591 | |
Dan Gohman | 12de0b9 | 2016-05-17 20:19:47 +0000 | [diff] [blame] | 592 | // Move Def into place. |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 593 | MBB.splice(Insert, &MBB, Def); |
JF Bastien | 1afd1e2 | 2016-02-28 15:33:53 +0000 | [diff] [blame] | 594 | LIS.handleMove(*Def); |
Dan Gohman | 12de0b9 | 2016-05-17 20:19:47 +0000 | [diff] [blame] | 595 | |
| 596 | // Create the Tee and attach the registers. |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 597 | const auto *RegClass = MRI.getRegClass(Reg); |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 598 | unsigned TeeReg = MRI.createVirtualRegister(RegClass); |
Dan Gohman | 8aa237c | 2016-02-16 15:17:21 +0000 | [diff] [blame] | 599 | unsigned DefReg = MRI.createVirtualRegister(RegClass); |
Dan Gohman | 33e694a | 2016-05-12 04:19:09 +0000 | [diff] [blame] | 600 | MachineOperand &DefMO = Def->getOperand(0); |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 601 | MachineInstr *Tee = BuildMI(MBB, Insert, Insert->getDebugLoc(), |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 602 | TII->get(GetTeeOpcode(RegClass)), TeeReg) |
Dan Gohman | 12de0b9 | 2016-05-17 20:19:47 +0000 | [diff] [blame] | 603 | .addReg(Reg, RegState::Define) |
Dan Gohman | 33e694a | 2016-05-12 04:19:09 +0000 | [diff] [blame] | 604 | .addReg(DefReg, getUndefRegState(DefMO.isDead())); |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 605 | Op.setReg(TeeReg); |
Dan Gohman | 33e694a | 2016-05-12 04:19:09 +0000 | [diff] [blame] | 606 | DefMO.setReg(DefReg); |
Dan Gohman | 12de0b9 | 2016-05-17 20:19:47 +0000 | [diff] [blame] | 607 | SlotIndex TeeIdx = LIS.InsertMachineInstrInMaps(*Tee).getRegSlot(); |
| 608 | SlotIndex DefIdx = LIS.getInstructionIndex(*Def).getRegSlot(); |
| 609 | |
Yury Delendik | be24c02 | 2019-01-15 18:14:12 +0000 | [diff] [blame] | 610 | DefDIs.move(Insert); |
Yury Delendik | 7c18d60 | 2018-09-25 18:59:34 +0000 | [diff] [blame] | 611 | |
Dan Gohman | 12de0b9 | 2016-05-17 20:19:47 +0000 | [diff] [blame] | 612 | // Tell LiveIntervals we moved the original vreg def from Def to Tee. |
| 613 | LiveInterval &LI = LIS.getInterval(Reg); |
| 614 | LiveInterval::iterator I = LI.FindSegmentContaining(DefIdx); |
| 615 | VNInfo *ValNo = LI.getVNInfoAt(DefIdx); |
| 616 | I->start = TeeIdx; |
| 617 | ValNo->def = TeeIdx; |
| 618 | ShrinkToUses(LI, LIS); |
| 619 | |
| 620 | // Finish stackifying the new regs. |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 621 | LIS.createAndComputeVirtRegInterval(TeeReg); |
Dan Gohman | 8aa237c | 2016-02-16 15:17:21 +0000 | [diff] [blame] | 622 | LIS.createAndComputeVirtRegInterval(DefReg); |
| 623 | MFI.stackifyVReg(DefReg); |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 624 | MFI.stackifyVReg(TeeReg); |
| 625 | ImposeStackOrdering(Def); |
| 626 | ImposeStackOrdering(Tee); |
Dan Gohman | 12de0b9 | 2016-05-17 20:19:47 +0000 | [diff] [blame] | 627 | |
Yury Delendik | be24c02 | 2019-01-15 18:14:12 +0000 | [diff] [blame] | 628 | DefDIs.clone(Tee, DefReg); |
| 629 | DefDIs.clone(Insert, TeeReg); |
Yury Delendik | 7c18d60 | 2018-09-25 18:59:34 +0000 | [diff] [blame] | 630 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 631 | LLVM_DEBUG(dbgs() << " - Replaced register: "; Def->dump()); |
| 632 | LLVM_DEBUG(dbgs() << " - Tee instruction: "; Tee->dump()); |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 633 | return Def; |
| 634 | } |
| 635 | |
| 636 | namespace { |
| 637 | /// A stack for walking the tree of instructions being built, visiting the |
| 638 | /// MachineOperands in DFS order. |
| 639 | class TreeWalkerState { |
| 640 | typedef MachineInstr::mop_iterator mop_iterator; |
| 641 | typedef std::reverse_iterator<mop_iterator> mop_reverse_iterator; |
| 642 | typedef iterator_range<mop_reverse_iterator> RangeTy; |
| 643 | SmallVector<RangeTy, 4> Worklist; |
| 644 | |
| 645 | public: |
| 646 | explicit TreeWalkerState(MachineInstr *Insert) { |
| 647 | const iterator_range<mop_iterator> &Range = Insert->explicit_uses(); |
| 648 | if (Range.begin() != Range.end()) |
| 649 | Worklist.push_back(reverse(Range)); |
| 650 | } |
| 651 | |
| 652 | bool Done() const { return Worklist.empty(); } |
| 653 | |
| 654 | MachineOperand &Pop() { |
| 655 | RangeTy &Range = Worklist.back(); |
| 656 | MachineOperand &Op = *Range.begin(); |
| 657 | Range = drop_begin(Range, 1); |
| 658 | if (Range.begin() == Range.end()) |
| 659 | Worklist.pop_back(); |
| 660 | assert((Worklist.empty() || |
| 661 | Worklist.back().begin() != Worklist.back().end()) && |
| 662 | "Empty ranges shouldn't remain in the worklist"); |
| 663 | return Op; |
| 664 | } |
| 665 | |
| 666 | /// Push Instr's operands onto the stack to be visited. |
| 667 | void PushOperands(MachineInstr *Instr) { |
| 668 | const iterator_range<mop_iterator> &Range(Instr->explicit_uses()); |
| 669 | if (Range.begin() != Range.end()) |
| 670 | Worklist.push_back(reverse(Range)); |
| 671 | } |
| 672 | |
| 673 | /// Some of Instr's operands are on the top of the stack; remove them and |
| 674 | /// re-insert them starting from the beginning (because we've commuted them). |
| 675 | void ResetTopOperands(MachineInstr *Instr) { |
| 676 | assert(HasRemainingOperands(Instr) && |
| 677 | "Reseting operands should only be done when the instruction has " |
| 678 | "an operand still on the stack"); |
| 679 | Worklist.back() = reverse(Instr->explicit_uses()); |
| 680 | } |
| 681 | |
| 682 | /// Test whether Instr has operands remaining to be visited at the top of |
| 683 | /// the stack. |
| 684 | bool HasRemainingOperands(const MachineInstr *Instr) const { |
| 685 | if (Worklist.empty()) |
| 686 | return false; |
| 687 | const RangeTy &Range = Worklist.back(); |
| 688 | return Range.begin() != Range.end() && Range.begin()->getParent() == Instr; |
| 689 | } |
Dan Gohman | fbfe5ec | 2016-01-28 03:59:09 +0000 | [diff] [blame] | 690 | |
| 691 | /// Test whether the given register is present on the stack, indicating an |
| 692 | /// operand in the tree that we haven't visited yet. Moving a definition of |
| 693 | /// Reg to a point in the tree after that would change its value. |
Dan Gohman | 1054570 | 2016-05-17 22:24:18 +0000 | [diff] [blame] | 694 | /// |
Thomas Lively | 6a87dda | 2019-01-08 06:25:55 +0000 | [diff] [blame] | 695 | /// This is needed as a consequence of using implicit local.gets for |
| 696 | /// uses and implicit local.sets for defs. |
Dan Gohman | fbfe5ec | 2016-01-28 03:59:09 +0000 | [diff] [blame] | 697 | bool IsOnStack(unsigned Reg) const { |
| 698 | for (const RangeTy &Range : Worklist) |
| 699 | for (const MachineOperand &MO : Range) |
| 700 | if (MO.isReg() && MO.getReg() == Reg) |
| 701 | return true; |
| 702 | return false; |
| 703 | } |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 704 | }; |
| 705 | |
| 706 | /// State to keep track of whether commuting is in flight or whether it's been |
| 707 | /// tried for the current instruction and didn't work. |
| 708 | class CommutingState { |
| 709 | /// There are effectively three states: the initial state where we haven't |
Heejin Ahn | 99d3946 | 2018-12-26 22:27:46 +0000 | [diff] [blame] | 710 | /// started commuting anything and we don't know anything yet, the tentative |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 711 | /// state where we've commuted the operands of the current instruction and are |
Heejin Ahn | 99d3946 | 2018-12-26 22:27:46 +0000 | [diff] [blame] | 712 | /// revisiting it, and the declined state where we've reverted the operands |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 713 | /// back to their original order and will no longer commute it further. |
| 714 | bool TentativelyCommuting; |
| 715 | bool Declined; |
| 716 | |
| 717 | /// During the tentative state, these hold the operand indices of the commuted |
| 718 | /// operands. |
| 719 | unsigned Operand0, Operand1; |
| 720 | |
| 721 | public: |
| 722 | CommutingState() : TentativelyCommuting(false), Declined(false) {} |
| 723 | |
| 724 | /// Stackification for an operand was not successful due to ordering |
| 725 | /// constraints. If possible, and if we haven't already tried it and declined |
| 726 | /// it, commute Insert's operands and prepare to revisit it. |
| 727 | void MaybeCommute(MachineInstr *Insert, TreeWalkerState &TreeWalker, |
| 728 | const WebAssemblyInstrInfo *TII) { |
| 729 | if (TentativelyCommuting) { |
| 730 | assert(!Declined && |
| 731 | "Don't decline commuting until you've finished trying it"); |
| 732 | // Commuting didn't help. Revert it. |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 733 | TII->commuteInstruction(*Insert, /*NewMI=*/false, Operand0, Operand1); |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 734 | TentativelyCommuting = false; |
| 735 | Declined = true; |
| 736 | } else if (!Declined && TreeWalker.HasRemainingOperands(Insert)) { |
| 737 | Operand0 = TargetInstrInfo::CommuteAnyOperandIndex; |
| 738 | Operand1 = TargetInstrInfo::CommuteAnyOperandIndex; |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 739 | if (TII->findCommutedOpIndices(*Insert, Operand0, Operand1)) { |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 740 | // Tentatively commute the operands and try again. |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 741 | TII->commuteInstruction(*Insert, /*NewMI=*/false, Operand0, Operand1); |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 742 | TreeWalker.ResetTopOperands(Insert); |
| 743 | TentativelyCommuting = true; |
| 744 | Declined = false; |
| 745 | } |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | /// Stackification for some operand was successful. Reset to the default |
| 750 | /// state. |
| 751 | void Reset() { |
| 752 | TentativelyCommuting = false; |
| 753 | Declined = false; |
| 754 | } |
| 755 | }; |
| 756 | } // end anonymous namespace |
| 757 | |
Dan Gohman | 1462faa | 2015-11-16 16:18:28 +0000 | [diff] [blame] | 758 | bool WebAssemblyRegStackify::runOnMachineFunction(MachineFunction &MF) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 759 | LLVM_DEBUG(dbgs() << "********** Register Stackifying **********\n" |
| 760 | "********** Function: " |
| 761 | << MF.getName() << '\n'); |
Dan Gohman | 1462faa | 2015-11-16 16:18:28 +0000 | [diff] [blame] | 762 | |
| 763 | bool Changed = false; |
| 764 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 765 | WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>(); |
Dan Gohman | b6fd39a | 2016-01-19 16:59:23 +0000 | [diff] [blame] | 766 | const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo(); |
| 767 | const auto *TRI = MF.getSubtarget<WebAssemblySubtarget>().getRegisterInfo(); |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 768 | AliasAnalysis &AA = getAnalysis<AAResultsWrapperPass>().getAAResults(); |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 769 | MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>(); |
Dan Gohman | 8887d1f | 2015-12-25 00:31:02 +0000 | [diff] [blame] | 770 | LiveIntervals &LIS = getAnalysis<LiveIntervals>(); |
Dan Gohman | d70e590 | 2015-12-08 03:30:42 +0000 | [diff] [blame] | 771 | |
Dan Gohman | 1462faa | 2015-11-16 16:18:28 +0000 | [diff] [blame] | 772 | // Walk the instructions from the bottom up. Currently we don't look past |
| 773 | // block boundaries, and the blocks aren't ordered so the block visitation |
| 774 | // order isn't significant, but we may want to change this in the future. |
| 775 | for (MachineBasicBlock &MBB : MF) { |
Dan Gohman | 8f59cf7 | 2016-01-06 18:29:35 +0000 | [diff] [blame] | 776 | // Don't use a range-based for loop, because we modify the list as we're |
| 777 | // iterating over it and the end iterator may change. |
| 778 | for (auto MII = MBB.rbegin(); MII != MBB.rend(); ++MII) { |
| 779 | MachineInstr *Insert = &*MII; |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 780 | // Don't nest anything inside an inline asm, because we don't have |
| 781 | // constraints for $push inputs. |
| 782 | if (Insert->getOpcode() == TargetOpcode::INLINEASM) |
Dan Gohman | 595e8ab | 2016-02-22 17:45:20 +0000 | [diff] [blame] | 783 | continue; |
| 784 | |
| 785 | // Ignore debugging intrinsics. |
| 786 | if (Insert->getOpcode() == TargetOpcode::DBG_VALUE) |
| 787 | continue; |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 788 | |
Dan Gohman | 1462faa | 2015-11-16 16:18:28 +0000 | [diff] [blame] | 789 | // Iterate through the inputs in reverse order, since we'll be pulling |
Dan Gohman | 53d1399 | 2015-12-02 18:08:49 +0000 | [diff] [blame] | 790 | // operands off the stack in LIFO order. |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 791 | CommutingState Commuting; |
| 792 | TreeWalkerState TreeWalker(Insert); |
| 793 | while (!TreeWalker.Done()) { |
| 794 | MachineOperand &Op = TreeWalker.Pop(); |
| 795 | |
Dan Gohman | 1462faa | 2015-11-16 16:18:28 +0000 | [diff] [blame] | 796 | // We're only interested in explicit virtual register operands. |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 797 | if (!Op.isReg()) |
Dan Gohman | 1462faa | 2015-11-16 16:18:28 +0000 | [diff] [blame] | 798 | continue; |
| 799 | |
| 800 | unsigned Reg = Op.getReg(); |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 801 | assert(Op.isUse() && "explicit_uses() should only iterate over uses"); |
| 802 | assert(!Op.isImplicit() && |
| 803 | "explicit_uses() should only iterate over explicit operands"); |
| 804 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) |
Dan Gohman | 1462faa | 2015-11-16 16:18:28 +0000 | [diff] [blame] | 805 | continue; |
| 806 | |
Dan Gohman | ffc184b | 2016-10-03 22:32:21 +0000 | [diff] [blame] | 807 | // Identify the definition for this register at this point. |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 808 | MachineInstr *Def = GetVRegDef(Reg, Insert, MRI, LIS); |
| 809 | if (!Def) |
| 810 | continue; |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 811 | |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 812 | // Don't nest an INLINE_ASM def into anything, because we don't have |
| 813 | // constraints for $pop outputs. |
| 814 | if (Def->getOpcode() == TargetOpcode::INLINEASM) |
| 815 | continue; |
| 816 | |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 817 | // Argument instructions represent live-in registers and not real |
| 818 | // instructions. |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 819 | if (WebAssembly::isArgument(*Def)) |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 820 | continue; |
| 821 | |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 822 | // Decide which strategy to take. Prefer to move a single-use value |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 823 | // over cloning it, and prefer cloning over introducing a tee. |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 824 | // For moving, we require the def to be in the same block as the use; |
| 825 | // this makes things simpler (LiveIntervals' handleMove function only |
| 826 | // supports intra-block moves) and it's MachineSink's job to catch all |
| 827 | // the sinking opportunities anyway. |
| 828 | bool SameBlock = Def->getParent() == &MBB; |
Derek Schuff | e9e6891 | 2016-09-30 18:02:54 +0000 | [diff] [blame] | 829 | bool CanMove = SameBlock && IsSafeToMove(Def, Insert, AA, MRI) && |
Dan Gohman | fbfe5ec | 2016-01-28 03:59:09 +0000 | [diff] [blame] | 830 | !TreeWalker.IsOnStack(Reg); |
Dan Gohman | 12de0b9 | 2016-05-17 20:19:47 +0000 | [diff] [blame] | 831 | if (CanMove && HasOneUse(Reg, Def, MRI, MDT, LIS)) { |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 832 | Insert = MoveForSingleUse(Reg, Op, Def, MBB, Insert, LIS, MFI, MRI); |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 833 | } else if (ShouldRematerialize(*Def, AA, TII)) { |
| 834 | Insert = |
| 835 | RematerializeCheapDef(Reg, Op, *Def, MBB, Insert->getIterator(), |
| 836 | LIS, MFI, MRI, TII, TRI); |
Sam Clegg | cf2a9e2 | 2018-07-16 23:09:29 +0000 | [diff] [blame] | 837 | } else if (CanMove && |
Dan Gohman | 1054570 | 2016-05-17 22:24:18 +0000 | [diff] [blame] | 838 | OneUseDominatesOtherUses(Reg, Op, MBB, MRI, MDT, LIS, MFI)) { |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 839 | Insert = MoveAndTeeForMultiUse(Reg, Op, Def, MBB, Insert, LIS, MFI, |
| 840 | MRI, TII); |
| 841 | } else { |
| 842 | // We failed to stackify the operand. If the problem was ordering |
| 843 | // constraints, Commuting may be able to help. |
| 844 | if (!CanMove && SameBlock) |
| 845 | Commuting.MaybeCommute(Insert, TreeWalker, TII); |
| 846 | // Proceed to the next operand. |
| 847 | continue; |
Dan Gohman | b6fd39a | 2016-01-19 16:59:23 +0000 | [diff] [blame] | 848 | } |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 849 | |
Dan Gohman | e81021a | 2016-11-08 19:40:38 +0000 | [diff] [blame] | 850 | // If the instruction we just stackified is an IMPLICIT_DEF, convert it |
| 851 | // to a constant 0 so that the def is explicit, and the push/pop |
| 852 | // correspondence is maintained. |
| 853 | if (Insert->getOpcode() == TargetOpcode::IMPLICIT_DEF) |
Thomas Lively | feb18fe | 2018-12-20 04:20:32 +0000 | [diff] [blame] | 854 | ConvertImplicitDefToConstZero(Insert, MRI, TII, MF, LIS); |
Dan Gohman | e81021a | 2016-11-08 19:40:38 +0000 | [diff] [blame] | 855 | |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 856 | // We stackified an operand. Add the defining instruction's operands to |
| 857 | // the worklist stack now to continue to build an ever deeper tree. |
| 858 | Commuting.Reset(); |
| 859 | TreeWalker.PushOperands(Insert); |
Dan Gohman | 1462faa | 2015-11-16 16:18:28 +0000 | [diff] [blame] | 860 | } |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 861 | |
| 862 | // If we stackified any operands, skip over the tree to start looking for |
| 863 | // the next instruction we can build a tree on. |
| 864 | if (Insert != &*MII) { |
Dan Gohman | 8f59cf7 | 2016-01-06 18:29:35 +0000 | [diff] [blame] | 865 | ImposeStackOrdering(&*MII); |
Eric Liu | c7e5a9c | 2016-09-12 09:35:59 +0000 | [diff] [blame] | 866 | MII = MachineBasicBlock::iterator(Insert).getReverse(); |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 867 | Changed = true; |
| 868 | } |
Dan Gohman | 1462faa | 2015-11-16 16:18:28 +0000 | [diff] [blame] | 869 | } |
| 870 | } |
| 871 | |
Dan Gohman | e040533 | 2016-10-03 22:43:53 +0000 | [diff] [blame] | 872 | // If we used VALUE_STACK anywhere, add it to the live-in sets everywhere so |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 873 | // that it never looks like a use-before-def. |
Dan Gohman | b0992da | 2015-11-20 02:19:12 +0000 | [diff] [blame] | 874 | if (Changed) { |
Dan Gohman | e040533 | 2016-10-03 22:43:53 +0000 | [diff] [blame] | 875 | MF.getRegInfo().addLiveIn(WebAssembly::VALUE_STACK); |
Dan Gohman | b0992da | 2015-11-20 02:19:12 +0000 | [diff] [blame] | 876 | for (MachineBasicBlock &MBB : MF) |
Dan Gohman | e040533 | 2016-10-03 22:43:53 +0000 | [diff] [blame] | 877 | MBB.addLiveIn(WebAssembly::VALUE_STACK); |
Dan Gohman | b0992da | 2015-11-20 02:19:12 +0000 | [diff] [blame] | 878 | } |
| 879 | |
Dan Gohman | 7bafa0e | 2015-11-20 02:33:24 +0000 | [diff] [blame] | 880 | #ifndef NDEBUG |
Dan Gohman | b6fd39a | 2016-01-19 16:59:23 +0000 | [diff] [blame] | 881 | // Verify that pushes and pops are performed in LIFO order. |
Dan Gohman | 7bafa0e | 2015-11-20 02:33:24 +0000 | [diff] [blame] | 882 | SmallVector<unsigned, 0> Stack; |
| 883 | for (MachineBasicBlock &MBB : MF) { |
| 884 | for (MachineInstr &MI : MBB) { |
Shiva Chen | 801bf7e | 2018-05-09 02:42:00 +0000 | [diff] [blame] | 885 | if (MI.isDebugInstr()) |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 886 | continue; |
Dan Gohman | 7bafa0e | 2015-11-20 02:33:24 +0000 | [diff] [blame] | 887 | for (MachineOperand &MO : reverse(MI.explicit_operands())) { |
Dan Gohman | 7a6b982 | 2015-11-29 22:32:02 +0000 | [diff] [blame] | 888 | if (!MO.isReg()) |
| 889 | continue; |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 890 | unsigned Reg = MO.getReg(); |
Dan Gohman | 7bafa0e | 2015-11-20 02:33:24 +0000 | [diff] [blame] | 891 | |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 892 | if (MFI.isVRegStackified(Reg)) { |
Dan Gohman | 7bafa0e | 2015-11-20 02:33:24 +0000 | [diff] [blame] | 893 | if (MO.isDef()) |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 894 | Stack.push_back(Reg); |
Dan Gohman | 7bafa0e | 2015-11-20 02:33:24 +0000 | [diff] [blame] | 895 | else |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 896 | assert(Stack.pop_back_val() == Reg && |
| 897 | "Register stack pop should be paired with a push"); |
Dan Gohman | 7bafa0e | 2015-11-20 02:33:24 +0000 | [diff] [blame] | 898 | } |
| 899 | } |
| 900 | } |
| 901 | // TODO: Generalize this code to support keeping values on the stack across |
| 902 | // basic block boundaries. |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 903 | assert(Stack.empty() && |
| 904 | "Register stack pushes and pops should be balanced"); |
Dan Gohman | 7bafa0e | 2015-11-20 02:33:24 +0000 | [diff] [blame] | 905 | } |
| 906 | #endif |
| 907 | |
Dan Gohman | 1462faa | 2015-11-16 16:18:28 +0000 | [diff] [blame] | 908 | return Changed; |
| 909 | } |