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