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