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