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. |
| 92 | static void QueryCallee(const MachineInstr *MI, unsigned CalleeOpNo, |
Dan Gohman | d08cd15 | 2016-05-17 21:14:26 +0000 | [diff] [blame] | 93 | bool &Read, bool &Write, bool &Effects, |
| 94 | bool &StackPointer) { |
| 95 | // All calls can use the stack pointer. |
| 96 | StackPointer = true; |
| 97 | |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 98 | const MachineOperand &MO = MI->getOperand(CalleeOpNo); |
| 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. |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 125 | static void Query(const MachineInstr *MI, AliasAnalysis &AA, |
Dan Gohman | d08cd15 | 2016-05-17 21:14:26 +0000 | [diff] [blame] | 126 | bool &Read, bool &Write, bool &Effects, bool &StackPointer) { |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 127 | assert(!MI->isPosition()); |
| 128 | assert(!MI->isTerminator()); |
| 129 | assert(!MI->isDebugValue()); |
| 130 | |
| 131 | // Check for loads. |
| 132 | if (MI->mayLoad() && !MI->isInvariantLoad(&AA)) |
| 133 | Read = true; |
| 134 | |
| 135 | // Check for stores. |
Dan Gohman | d08cd15 | 2016-05-17 21:14:26 +0000 | [diff] [blame] | 136 | if (MI->mayStore()) { |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 137 | Write = true; |
Dan Gohman | d08cd15 | 2016-05-17 21:14:26 +0000 | [diff] [blame] | 138 | |
| 139 | // Check for stores to __stack_pointer. |
| 140 | for (auto MMO : MI->memoperands()) { |
| 141 | const MachinePointerInfo &MPI = MMO->getPointerInfo(); |
| 142 | if (MPI.V.is<const PseudoSourceValue *>()) { |
| 143 | auto PSV = MPI.V.get<const PseudoSourceValue *>(); |
| 144 | if (const ExternalSymbolPseudoSourceValue *EPSV = |
| 145 | dyn_cast<ExternalSymbolPseudoSourceValue>(PSV)) |
| 146 | if (StringRef(EPSV->getSymbol()) == "__stack_pointer") |
| 147 | StackPointer = true; |
| 148 | } |
| 149 | } |
| 150 | } else if (MI->hasOrderedMemoryRef()) { |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 151 | switch (MI->getOpcode()) { |
| 152 | case WebAssembly::DIV_S_I32: case WebAssembly::DIV_S_I64: |
| 153 | case WebAssembly::REM_S_I32: case WebAssembly::REM_S_I64: |
| 154 | case WebAssembly::DIV_U_I32: case WebAssembly::DIV_U_I64: |
| 155 | case WebAssembly::REM_U_I32: case WebAssembly::REM_U_I64: |
| 156 | case WebAssembly::I32_TRUNC_S_F32: case WebAssembly::I64_TRUNC_S_F32: |
| 157 | case WebAssembly::I32_TRUNC_S_F64: case WebAssembly::I64_TRUNC_S_F64: |
| 158 | case WebAssembly::I32_TRUNC_U_F32: case WebAssembly::I64_TRUNC_U_F32: |
| 159 | case WebAssembly::I32_TRUNC_U_F64: case WebAssembly::I64_TRUNC_U_F64: |
| 160 | // These instruction have hasUnmodeledSideEffects() returning true |
| 161 | // because they trap on overflow and invalid so they can't be arbitrarily |
| 162 | // moved, however hasOrderedMemoryRef() interprets this plus their lack |
| 163 | // of memoperands as having a potential unknown memory reference. |
| 164 | break; |
| 165 | default: |
Dan Gohman | 1054570 | 2016-05-17 22:24:18 +0000 | [diff] [blame^] | 166 | // Record volatile accesses, unless it's a call, as calls are handled |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 167 | // specially below. |
Dan Gohman | 1054570 | 2016-05-17 22:24:18 +0000 | [diff] [blame^] | 168 | if (!MI->isCall()) { |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 169 | Write = true; |
Dan Gohman | 1054570 | 2016-05-17 22:24:18 +0000 | [diff] [blame^] | 170 | Effects = true; |
| 171 | } |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 172 | break; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | // Check for side effects. |
| 177 | if (MI->hasUnmodeledSideEffects()) { |
| 178 | switch (MI->getOpcode()) { |
| 179 | case WebAssembly::DIV_S_I32: case WebAssembly::DIV_S_I64: |
| 180 | case WebAssembly::REM_S_I32: case WebAssembly::REM_S_I64: |
| 181 | case WebAssembly::DIV_U_I32: case WebAssembly::DIV_U_I64: |
| 182 | case WebAssembly::REM_U_I32: case WebAssembly::REM_U_I64: |
| 183 | case WebAssembly::I32_TRUNC_S_F32: case WebAssembly::I64_TRUNC_S_F32: |
| 184 | case WebAssembly::I32_TRUNC_S_F64: case WebAssembly::I64_TRUNC_S_F64: |
| 185 | case WebAssembly::I32_TRUNC_U_F32: case WebAssembly::I64_TRUNC_U_F32: |
| 186 | case WebAssembly::I32_TRUNC_U_F64: case WebAssembly::I64_TRUNC_U_F64: |
| 187 | // These instructions have hasUnmodeledSideEffects() returning true |
| 188 | // because they trap on overflow and invalid so they can't be arbitrarily |
| 189 | // moved, however in the specific case of register stackifying, it is safe |
| 190 | // to move them because overflow and invalid are Undefined Behavior. |
| 191 | break; |
| 192 | default: |
| 193 | Effects = true; |
| 194 | break; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | // Analyze calls. |
| 199 | if (MI->isCall()) { |
| 200 | switch (MI->getOpcode()) { |
| 201 | case WebAssembly::CALL_VOID: |
Dan Gohman | 1054570 | 2016-05-17 22:24:18 +0000 | [diff] [blame^] | 202 | case WebAssembly::CALL_INDIRECT_VOID: |
Dan Gohman | d08cd15 | 2016-05-17 21:14:26 +0000 | [diff] [blame] | 203 | QueryCallee(MI, 0, Read, Write, Effects, StackPointer); |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 204 | break; |
Dan Gohman | 1054570 | 2016-05-17 22:24:18 +0000 | [diff] [blame^] | 205 | case WebAssembly::CALL_I32: case WebAssembly::CALL_I64: |
| 206 | case WebAssembly::CALL_F32: case WebAssembly::CALL_F64: |
| 207 | case WebAssembly::CALL_INDIRECT_I32: case WebAssembly::CALL_INDIRECT_I64: |
| 208 | case WebAssembly::CALL_INDIRECT_F32: case WebAssembly::CALL_INDIRECT_F64: |
Dan Gohman | d08cd15 | 2016-05-17 21:14:26 +0000 | [diff] [blame] | 209 | QueryCallee(MI, 1, Read, Write, Effects, StackPointer); |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 210 | break; |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 211 | default: |
| 212 | llvm_unreachable("unexpected call opcode"); |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | // Test whether Def is safe and profitable to rematerialize. |
| 218 | static bool ShouldRematerialize(const MachineInstr *Def, AliasAnalysis &AA, |
| 219 | const WebAssemblyInstrInfo *TII) { |
| 220 | return Def->isAsCheapAsAMove() && |
| 221 | TII->isTriviallyReMaterializable(Def, &AA); |
| 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); |
| 258 | for (auto I : MRI.use_operands(Reg)) { |
| 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, |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 277 | AliasAnalysis &AA, const LiveIntervals &LIS, |
| 278 | 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. |
| 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 | |
| 304 | // Ask LiveIntervals whether moving this virtual register use or def to |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 305 | // Insert will change which value numbers are seen. |
Dan Gohman | 12de0b9 | 2016-05-17 20:19:47 +0000 | [diff] [blame] | 306 | // |
| 307 | // If the operand is a use of a register that is also defined in the same |
| 308 | // instruction, test that the newly defined value reaches the insert point, |
| 309 | // since the operand will be moving along with the def. |
Dan Gohman | 8887d1f | 2015-12-25 00:31:02 +0000 | [diff] [blame] | 310 | const LiveInterval &LI = LIS.getInterval(Reg); |
Dan Gohman | b6fd39a | 2016-01-19 16:59:23 +0000 | [diff] [blame] | 311 | VNInfo *DefVNI = |
Dan Gohman | 12de0b9 | 2016-05-17 20:19:47 +0000 | [diff] [blame] | 312 | (MO.isDef() || Def->definesRegister(Reg)) ? |
| 313 | LI.getVNInfoAt(LIS.getInstructionIndex(*Def).getRegSlot()) : |
| 314 | LI.getVNInfoBefore(LIS.getInstructionIndex(*Def)); |
Dan Gohman | 8887d1f | 2015-12-25 00:31:02 +0000 | [diff] [blame] | 315 | assert(DefVNI && "Instruction input missing value number"); |
JF Bastien | 13d3b9b | 2016-02-27 16:38:23 +0000 | [diff] [blame] | 316 | VNInfo *InsVNI = LI.getVNInfoBefore(LIS.getInstructionIndex(*Insert)); |
Dan Gohman | 8887d1f | 2015-12-25 00:31:02 +0000 | [diff] [blame] | 317 | if (InsVNI && DefVNI != InsVNI) |
| 318 | return false; |
| 319 | } |
| 320 | |
Dan Gohman | d08cd15 | 2016-05-17 21:14:26 +0000 | [diff] [blame] | 321 | bool Read = false, Write = false, Effects = false, StackPointer = false; |
| 322 | Query(Def, AA, Read, Write, Effects, StackPointer); |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 323 | |
| 324 | // If the instruction does not access memory and has no side effects, it has |
| 325 | // no additional dependencies. |
Dan Gohman | d08cd15 | 2016-05-17 21:14:26 +0000 | [diff] [blame] | 326 | if (!Read && !Write && !Effects && !StackPointer) |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 327 | return true; |
| 328 | |
| 329 | // Scan through the intervening instructions between Def and Insert. |
| 330 | MachineBasicBlock::const_iterator D(Def), I(Insert); |
| 331 | for (--I; I != D; --I) { |
| 332 | bool InterveningRead = false; |
| 333 | bool InterveningWrite = false; |
| 334 | bool InterveningEffects = false; |
Dan Gohman | d08cd15 | 2016-05-17 21:14:26 +0000 | [diff] [blame] | 335 | bool InterveningStackPointer = false; |
| 336 | Query(I, AA, InterveningRead, InterveningWrite, InterveningEffects, |
| 337 | InterveningStackPointer); |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 338 | if (Effects && InterveningEffects) |
| 339 | return false; |
| 340 | if (Read && InterveningWrite) |
| 341 | return false; |
| 342 | if (Write && (InterveningRead || InterveningWrite)) |
| 343 | return false; |
Dan Gohman | d08cd15 | 2016-05-17 21:14:26 +0000 | [diff] [blame] | 344 | if (StackPointer && InterveningStackPointer) |
| 345 | return false; |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | return true; |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 351 | /// Test whether OneUse, a use of Reg, dominates all of Reg's other uses. |
| 352 | static bool OneUseDominatesOtherUses(unsigned Reg, const MachineOperand &OneUse, |
| 353 | const MachineBasicBlock &MBB, |
| 354 | const MachineRegisterInfo &MRI, |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 355 | const MachineDominatorTree &MDT, |
Dan Gohman | 1054570 | 2016-05-17 22:24:18 +0000 | [diff] [blame^] | 356 | LiveIntervals &LIS, |
| 357 | WebAssemblyFunctionInfo &MFI) { |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 358 | const LiveInterval &LI = LIS.getInterval(Reg); |
| 359 | |
| 360 | const MachineInstr *OneUseInst = OneUse.getParent(); |
| 361 | VNInfo *OneUseVNI = LI.getVNInfoBefore(LIS.getInstructionIndex(*OneUseInst)); |
| 362 | |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 363 | for (const MachineOperand &Use : MRI.use_operands(Reg)) { |
| 364 | if (&Use == &OneUse) |
| 365 | continue; |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 366 | |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 367 | const MachineInstr *UseInst = Use.getParent(); |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 368 | VNInfo *UseVNI = LI.getVNInfoBefore(LIS.getInstructionIndex(*UseInst)); |
| 369 | |
| 370 | if (UseVNI != OneUseVNI) |
| 371 | continue; |
| 372 | |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 373 | const MachineInstr *OneUseInst = OneUse.getParent(); |
Dan Gohman | 12de0b9 | 2016-05-17 20:19:47 +0000 | [diff] [blame] | 374 | if (UseInst == OneUseInst) { |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 375 | // Another use in the same instruction. We need to ensure that the one |
| 376 | // selected use happens "before" it. |
| 377 | if (&OneUse > &Use) |
| 378 | return false; |
| 379 | } else { |
| 380 | // Test that the use is dominated by the one selected use. |
Dan Gohman | 1054570 | 2016-05-17 22:24:18 +0000 | [diff] [blame^] | 381 | while (!MDT.dominates(OneUseInst, UseInst)) { |
| 382 | // Actually, dominating is over-conservative. Test that the use would |
| 383 | // happen after the one selected use in the stack evaluation order. |
| 384 | // |
| 385 | // This is needed as a consequence of using implicit get_locals for |
| 386 | // uses and implicit set_locals for defs. |
| 387 | if (UseInst->getDesc().getNumDefs() == 0) |
| 388 | return false; |
| 389 | const MachineOperand &MO = UseInst->getOperand(0); |
| 390 | if (!MO.isReg()) |
| 391 | return false; |
| 392 | unsigned DefReg = MO.getReg(); |
| 393 | if (!TargetRegisterInfo::isVirtualRegister(DefReg) || |
| 394 | !MFI.isVRegStackified(DefReg)) |
| 395 | return false; |
| 396 | assert(MRI.hasOneUse(DefReg)); |
| 397 | const MachineOperand &NewUse = *MRI.use_begin(DefReg); |
| 398 | const MachineInstr *NewUseInst = NewUse.getParent(); |
| 399 | if (NewUseInst == OneUseInst) { |
| 400 | if (&OneUse > &NewUse) |
| 401 | return false; |
| 402 | break; |
| 403 | } |
| 404 | UseInst = NewUseInst; |
| 405 | } |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | return true; |
| 409 | } |
| 410 | |
| 411 | /// Get the appropriate tee_local opcode for the given register class. |
| 412 | static unsigned GetTeeLocalOpcode(const TargetRegisterClass *RC) { |
| 413 | if (RC == &WebAssembly::I32RegClass) |
| 414 | return WebAssembly::TEE_LOCAL_I32; |
| 415 | if (RC == &WebAssembly::I64RegClass) |
| 416 | return WebAssembly::TEE_LOCAL_I64; |
| 417 | if (RC == &WebAssembly::F32RegClass) |
| 418 | return WebAssembly::TEE_LOCAL_F32; |
| 419 | if (RC == &WebAssembly::F64RegClass) |
| 420 | return WebAssembly::TEE_LOCAL_F64; |
| 421 | llvm_unreachable("Unexpected register class"); |
| 422 | } |
| 423 | |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 424 | // Shrink LI to its uses, cleaning up LI. |
| 425 | static void ShrinkToUses(LiveInterval &LI, LiveIntervals &LIS) { |
| 426 | if (LIS.shrinkToUses(&LI)) { |
| 427 | SmallVector<LiveInterval*, 4> SplitLIs; |
| 428 | LIS.splitSeparateComponents(LI, SplitLIs); |
| 429 | } |
| 430 | } |
| 431 | |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 432 | /// A single-use def in the same block with no intervening memory or register |
| 433 | /// dependencies; move the def down and nest it with the current instruction. |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 434 | static MachineInstr *MoveForSingleUse(unsigned Reg, MachineOperand& Op, |
| 435 | MachineInstr *Def, |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 436 | MachineBasicBlock &MBB, |
| 437 | MachineInstr *Insert, LiveIntervals &LIS, |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 438 | WebAssemblyFunctionInfo &MFI, |
| 439 | MachineRegisterInfo &MRI) { |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 440 | DEBUG(dbgs() << "Move for single use: "; Def->dump()); |
| 441 | |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 442 | MBB.splice(Insert, &MBB, Def); |
JF Bastien | 1afd1e2 | 2016-02-28 15:33:53 +0000 | [diff] [blame] | 443 | LIS.handleMove(*Def); |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 444 | |
Dan Gohman | 12de0b9 | 2016-05-17 20:19:47 +0000 | [diff] [blame] | 445 | if (MRI.hasOneDef(Reg) && MRI.hasOneUse(Reg)) { |
| 446 | // No one else is using this register for anything so we can just stackify |
| 447 | // it in place. |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 448 | MFI.stackifyVReg(Reg); |
| 449 | } else { |
Dan Gohman | 12de0b9 | 2016-05-17 20:19:47 +0000 | [diff] [blame] | 450 | // The register may have unrelated uses or defs; create a new register for |
| 451 | // just our one def and use so that we can stackify it. |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 452 | unsigned NewReg = MRI.createVirtualRegister(MRI.getRegClass(Reg)); |
| 453 | Def->getOperand(0).setReg(NewReg); |
| 454 | Op.setReg(NewReg); |
| 455 | |
| 456 | // Tell LiveIntervals about the new register. |
| 457 | LIS.createAndComputeVirtRegInterval(NewReg); |
| 458 | |
| 459 | // Tell LiveIntervals about the changes to the old register. |
| 460 | LiveInterval &LI = LIS.getInterval(Reg); |
| 461 | LIS.removeVRegDefAt(LI, LIS.getInstructionIndex(*Def).getRegSlot()); |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 462 | ShrinkToUses(LI, LIS); |
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. |
| 475 | static MachineInstr * |
| 476 | RematerializeCheapDef(unsigned Reg, MachineOperand &Op, MachineInstr *Def, |
| 477 | MachineBasicBlock &MBB, MachineInstr *Insert, |
| 478 | LiveIntervals &LIS, WebAssemblyFunctionInfo &MFI, |
| 479 | MachineRegisterInfo &MRI, const WebAssemblyInstrInfo *TII, |
| 480 | const WebAssemblyRegisterInfo *TRI) { |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 481 | DEBUG(dbgs() << "Rematerializing cheap def: "; Def->dump()); |
| 482 | DEBUG(dbgs() << " - for use in "; Op.getParent()->dump()); |
| 483 | |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 484 | unsigned NewReg = MRI.createVirtualRegister(MRI.getRegClass(Reg)); |
| 485 | TII->reMaterialize(MBB, Insert, NewReg, 0, Def, *TRI); |
| 486 | Op.setReg(NewReg); |
| 487 | MachineInstr *Clone = &*std::prev(MachineBasicBlock::instr_iterator(Insert)); |
JF Bastien | 13d3b9b | 2016-02-27 16:38:23 +0000 | [diff] [blame] | 488 | LIS.InsertMachineInstrInMaps(*Clone); |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 489 | LIS.createAndComputeVirtRegInterval(NewReg); |
| 490 | MFI.stackifyVReg(NewReg); |
| 491 | ImposeStackOrdering(Clone); |
| 492 | |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 493 | DEBUG(dbgs() << " - Cloned to "; Clone->dump()); |
| 494 | |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 495 | // Shrink the interval. |
| 496 | bool IsDead = MRI.use_empty(Reg); |
| 497 | if (!IsDead) { |
| 498 | LiveInterval &LI = LIS.getInterval(Reg); |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 499 | ShrinkToUses(LI, LIS); |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 500 | IsDead = !LI.liveAt(LIS.getInstructionIndex(*Def).getDeadSlot()); |
| 501 | } |
| 502 | |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 503 | // If that was the last use of the original, delete the original. |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 504 | if (IsDead) { |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 505 | DEBUG(dbgs() << " - Deleting original\n"); |
JF Bastien | 13d3b9b | 2016-02-27 16:38:23 +0000 | [diff] [blame] | 506 | SlotIndex Idx = LIS.getInstructionIndex(*Def).getRegSlot(); |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 507 | LIS.removePhysRegDefAt(WebAssembly::ARGUMENTS, Idx); |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 508 | LIS.removeInterval(Reg); |
JF Bastien | 13d3b9b | 2016-02-27 16:38:23 +0000 | [diff] [blame] | 509 | LIS.RemoveMachineInstrFromMaps(*Def); |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 510 | Def->eraseFromParent(); |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 511 | } |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 512 | |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 513 | return Clone; |
| 514 | } |
| 515 | |
| 516 | /// A multiple-use def in the same block with no intervening memory or register |
| 517 | /// dependencies; move the def down, nest it with the current instruction, and |
| 518 | /// insert a tee_local to satisfy the rest of the uses. As an illustration, |
| 519 | /// rewrite this: |
| 520 | /// |
| 521 | /// Reg = INST ... // Def |
| 522 | /// INST ..., Reg, ... // Insert |
| 523 | /// INST ..., Reg, ... |
| 524 | /// INST ..., Reg, ... |
| 525 | /// |
| 526 | /// to this: |
| 527 | /// |
Dan Gohman | 8aa237c | 2016-02-16 15:17:21 +0000 | [diff] [blame] | 528 | /// DefReg = INST ... // Def (to become the new Insert) |
Dan Gohman | 12de0b9 | 2016-05-17 20:19:47 +0000 | [diff] [blame] | 529 | /// TeeReg, Reg = TEE_LOCAL_... DefReg |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 530 | /// INST ..., TeeReg, ... // Insert |
| 531 | /// INST ..., NewReg, ... |
| 532 | /// INST ..., NewReg, ... |
| 533 | /// |
Dan Gohman | 8aa237c | 2016-02-16 15:17:21 +0000 | [diff] [blame] | 534 | /// with DefReg and TeeReg stackified. This eliminates a get_local from the |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 535 | /// resulting code. |
| 536 | static MachineInstr *MoveAndTeeForMultiUse( |
| 537 | unsigned Reg, MachineOperand &Op, MachineInstr *Def, MachineBasicBlock &MBB, |
| 538 | MachineInstr *Insert, LiveIntervals &LIS, WebAssemblyFunctionInfo &MFI, |
| 539 | MachineRegisterInfo &MRI, const WebAssemblyInstrInfo *TII) { |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 540 | DEBUG(dbgs() << "Move and tee for multi-use:"; Def->dump()); |
| 541 | |
Dan Gohman | 12de0b9 | 2016-05-17 20:19:47 +0000 | [diff] [blame] | 542 | // Move Def into place. |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 543 | MBB.splice(Insert, &MBB, Def); |
JF Bastien | 1afd1e2 | 2016-02-28 15:33:53 +0000 | [diff] [blame] | 544 | LIS.handleMove(*Def); |
Dan Gohman | 12de0b9 | 2016-05-17 20:19:47 +0000 | [diff] [blame] | 545 | |
| 546 | // Create the Tee and attach the registers. |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 547 | const auto *RegClass = MRI.getRegClass(Reg); |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 548 | unsigned TeeReg = MRI.createVirtualRegister(RegClass); |
Dan Gohman | 8aa237c | 2016-02-16 15:17:21 +0000 | [diff] [blame] | 549 | unsigned DefReg = MRI.createVirtualRegister(RegClass); |
Dan Gohman | 33e694a | 2016-05-12 04:19:09 +0000 | [diff] [blame] | 550 | MachineOperand &DefMO = Def->getOperand(0); |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 551 | MachineInstr *Tee = BuildMI(MBB, Insert, Insert->getDebugLoc(), |
| 552 | TII->get(GetTeeLocalOpcode(RegClass)), TeeReg) |
Dan Gohman | 12de0b9 | 2016-05-17 20:19:47 +0000 | [diff] [blame] | 553 | .addReg(Reg, RegState::Define) |
Dan Gohman | 33e694a | 2016-05-12 04:19:09 +0000 | [diff] [blame] | 554 | .addReg(DefReg, getUndefRegState(DefMO.isDead())); |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 555 | Op.setReg(TeeReg); |
Dan Gohman | 33e694a | 2016-05-12 04:19:09 +0000 | [diff] [blame] | 556 | DefMO.setReg(DefReg); |
Dan Gohman | 12de0b9 | 2016-05-17 20:19:47 +0000 | [diff] [blame] | 557 | SlotIndex TeeIdx = LIS.InsertMachineInstrInMaps(*Tee).getRegSlot(); |
| 558 | SlotIndex DefIdx = LIS.getInstructionIndex(*Def).getRegSlot(); |
| 559 | |
| 560 | // Tell LiveIntervals we moved the original vreg def from Def to Tee. |
| 561 | LiveInterval &LI = LIS.getInterval(Reg); |
| 562 | LiveInterval::iterator I = LI.FindSegmentContaining(DefIdx); |
| 563 | VNInfo *ValNo = LI.getVNInfoAt(DefIdx); |
| 564 | I->start = TeeIdx; |
| 565 | ValNo->def = TeeIdx; |
| 566 | ShrinkToUses(LI, LIS); |
| 567 | |
| 568 | // Finish stackifying the new regs. |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 569 | LIS.createAndComputeVirtRegInterval(TeeReg); |
Dan Gohman | 8aa237c | 2016-02-16 15:17:21 +0000 | [diff] [blame] | 570 | LIS.createAndComputeVirtRegInterval(DefReg); |
| 571 | MFI.stackifyVReg(DefReg); |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 572 | MFI.stackifyVReg(TeeReg); |
| 573 | ImposeStackOrdering(Def); |
| 574 | ImposeStackOrdering(Tee); |
Dan Gohman | 12de0b9 | 2016-05-17 20:19:47 +0000 | [diff] [blame] | 575 | |
| 576 | DEBUG(dbgs() << " - Replaced register: "; Def->dump()); |
| 577 | DEBUG(dbgs() << " - Tee instruction: "; Tee->dump()); |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 578 | return Def; |
| 579 | } |
| 580 | |
| 581 | namespace { |
| 582 | /// A stack for walking the tree of instructions being built, visiting the |
| 583 | /// MachineOperands in DFS order. |
| 584 | class TreeWalkerState { |
| 585 | typedef MachineInstr::mop_iterator mop_iterator; |
| 586 | typedef std::reverse_iterator<mop_iterator> mop_reverse_iterator; |
| 587 | typedef iterator_range<mop_reverse_iterator> RangeTy; |
| 588 | SmallVector<RangeTy, 4> Worklist; |
| 589 | |
| 590 | public: |
| 591 | explicit TreeWalkerState(MachineInstr *Insert) { |
| 592 | const iterator_range<mop_iterator> &Range = Insert->explicit_uses(); |
| 593 | if (Range.begin() != Range.end()) |
| 594 | Worklist.push_back(reverse(Range)); |
| 595 | } |
| 596 | |
| 597 | bool Done() const { return Worklist.empty(); } |
| 598 | |
| 599 | MachineOperand &Pop() { |
| 600 | RangeTy &Range = Worklist.back(); |
| 601 | MachineOperand &Op = *Range.begin(); |
| 602 | Range = drop_begin(Range, 1); |
| 603 | if (Range.begin() == Range.end()) |
| 604 | Worklist.pop_back(); |
| 605 | assert((Worklist.empty() || |
| 606 | Worklist.back().begin() != Worklist.back().end()) && |
| 607 | "Empty ranges shouldn't remain in the worklist"); |
| 608 | return Op; |
| 609 | } |
| 610 | |
| 611 | /// Push Instr's operands onto the stack to be visited. |
| 612 | void PushOperands(MachineInstr *Instr) { |
| 613 | const iterator_range<mop_iterator> &Range(Instr->explicit_uses()); |
| 614 | if (Range.begin() != Range.end()) |
| 615 | Worklist.push_back(reverse(Range)); |
| 616 | } |
| 617 | |
| 618 | /// Some of Instr's operands are on the top of the stack; remove them and |
| 619 | /// re-insert them starting from the beginning (because we've commuted them). |
| 620 | void ResetTopOperands(MachineInstr *Instr) { |
| 621 | assert(HasRemainingOperands(Instr) && |
| 622 | "Reseting operands should only be done when the instruction has " |
| 623 | "an operand still on the stack"); |
| 624 | Worklist.back() = reverse(Instr->explicit_uses()); |
| 625 | } |
| 626 | |
| 627 | /// Test whether Instr has operands remaining to be visited at the top of |
| 628 | /// the stack. |
| 629 | bool HasRemainingOperands(const MachineInstr *Instr) const { |
| 630 | if (Worklist.empty()) |
| 631 | return false; |
| 632 | const RangeTy &Range = Worklist.back(); |
| 633 | return Range.begin() != Range.end() && Range.begin()->getParent() == Instr; |
| 634 | } |
Dan Gohman | fbfe5ec | 2016-01-28 03:59:09 +0000 | [diff] [blame] | 635 | |
| 636 | /// Test whether the given register is present on the stack, indicating an |
| 637 | /// operand in the tree that we haven't visited yet. Moving a definition of |
| 638 | /// 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^] | 639 | /// |
| 640 | /// This is needed as a consequence of using implicit get_locals for |
| 641 | /// uses and implicit set_locals for defs. |
Dan Gohman | fbfe5ec | 2016-01-28 03:59:09 +0000 | [diff] [blame] | 642 | bool IsOnStack(unsigned Reg) const { |
| 643 | for (const RangeTy &Range : Worklist) |
| 644 | for (const MachineOperand &MO : Range) |
| 645 | if (MO.isReg() && MO.getReg() == Reg) |
| 646 | return true; |
| 647 | return false; |
| 648 | } |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 649 | }; |
| 650 | |
| 651 | /// State to keep track of whether commuting is in flight or whether it's been |
| 652 | /// tried for the current instruction and didn't work. |
| 653 | class CommutingState { |
| 654 | /// There are effectively three states: the initial state where we haven't |
| 655 | /// started commuting anything and we don't know anything yet, the tenative |
| 656 | /// state where we've commuted the operands of the current instruction and are |
| 657 | /// revisting it, and the declined state where we've reverted the operands |
| 658 | /// back to their original order and will no longer commute it further. |
| 659 | bool TentativelyCommuting; |
| 660 | bool Declined; |
| 661 | |
| 662 | /// During the tentative state, these hold the operand indices of the commuted |
| 663 | /// operands. |
| 664 | unsigned Operand0, Operand1; |
| 665 | |
| 666 | public: |
| 667 | CommutingState() : TentativelyCommuting(false), Declined(false) {} |
| 668 | |
| 669 | /// Stackification for an operand was not successful due to ordering |
| 670 | /// constraints. If possible, and if we haven't already tried it and declined |
| 671 | /// it, commute Insert's operands and prepare to revisit it. |
| 672 | void MaybeCommute(MachineInstr *Insert, TreeWalkerState &TreeWalker, |
| 673 | const WebAssemblyInstrInfo *TII) { |
| 674 | if (TentativelyCommuting) { |
| 675 | assert(!Declined && |
| 676 | "Don't decline commuting until you've finished trying it"); |
| 677 | // Commuting didn't help. Revert it. |
| 678 | TII->commuteInstruction(Insert, /*NewMI=*/false, Operand0, Operand1); |
| 679 | TentativelyCommuting = false; |
| 680 | Declined = true; |
| 681 | } else if (!Declined && TreeWalker.HasRemainingOperands(Insert)) { |
| 682 | Operand0 = TargetInstrInfo::CommuteAnyOperandIndex; |
| 683 | Operand1 = TargetInstrInfo::CommuteAnyOperandIndex; |
| 684 | if (TII->findCommutedOpIndices(Insert, Operand0, Operand1)) { |
| 685 | // Tentatively commute the operands and try again. |
| 686 | TII->commuteInstruction(Insert, /*NewMI=*/false, Operand0, Operand1); |
| 687 | TreeWalker.ResetTopOperands(Insert); |
| 688 | TentativelyCommuting = true; |
| 689 | Declined = false; |
| 690 | } |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | /// Stackification for some operand was successful. Reset to the default |
| 695 | /// state. |
| 696 | void Reset() { |
| 697 | TentativelyCommuting = false; |
| 698 | Declined = false; |
| 699 | } |
| 700 | }; |
| 701 | } // end anonymous namespace |
| 702 | |
Dan Gohman | 1462faa | 2015-11-16 16:18:28 +0000 | [diff] [blame] | 703 | bool WebAssemblyRegStackify::runOnMachineFunction(MachineFunction &MF) { |
| 704 | DEBUG(dbgs() << "********** Register Stackifying **********\n" |
| 705 | "********** Function: " |
| 706 | << MF.getName() << '\n'); |
| 707 | |
| 708 | bool Changed = false; |
| 709 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 710 | WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>(); |
Dan Gohman | b6fd39a | 2016-01-19 16:59:23 +0000 | [diff] [blame] | 711 | const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo(); |
| 712 | const auto *TRI = MF.getSubtarget<WebAssemblySubtarget>().getRegisterInfo(); |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 713 | AliasAnalysis &AA = getAnalysis<AAResultsWrapperPass>().getAAResults(); |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 714 | MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>(); |
Dan Gohman | 8887d1f | 2015-12-25 00:31:02 +0000 | [diff] [blame] | 715 | LiveIntervals &LIS = getAnalysis<LiveIntervals>(); |
Dan Gohman | d70e590 | 2015-12-08 03:30:42 +0000 | [diff] [blame] | 716 | |
Dan Gohman | 1462faa | 2015-11-16 16:18:28 +0000 | [diff] [blame] | 717 | // Walk the instructions from the bottom up. Currently we don't look past |
| 718 | // block boundaries, and the blocks aren't ordered so the block visitation |
| 719 | // order isn't significant, but we may want to change this in the future. |
| 720 | for (MachineBasicBlock &MBB : MF) { |
Dan Gohman | 8f59cf7 | 2016-01-06 18:29:35 +0000 | [diff] [blame] | 721 | // Don't use a range-based for loop, because we modify the list as we're |
| 722 | // iterating over it and the end iterator may change. |
| 723 | for (auto MII = MBB.rbegin(); MII != MBB.rend(); ++MII) { |
| 724 | MachineInstr *Insert = &*MII; |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 725 | // Don't nest anything inside an inline asm, because we don't have |
| 726 | // constraints for $push inputs. |
| 727 | if (Insert->getOpcode() == TargetOpcode::INLINEASM) |
Dan Gohman | 595e8ab | 2016-02-22 17:45:20 +0000 | [diff] [blame] | 728 | continue; |
| 729 | |
| 730 | // Ignore debugging intrinsics. |
| 731 | if (Insert->getOpcode() == TargetOpcode::DBG_VALUE) |
| 732 | continue; |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 733 | |
Dan Gohman | 1462faa | 2015-11-16 16:18:28 +0000 | [diff] [blame] | 734 | // Iterate through the inputs in reverse order, since we'll be pulling |
Dan Gohman | 53d1399 | 2015-12-02 18:08:49 +0000 | [diff] [blame] | 735 | // operands off the stack in LIFO order. |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 736 | CommutingState Commuting; |
| 737 | TreeWalkerState TreeWalker(Insert); |
| 738 | while (!TreeWalker.Done()) { |
| 739 | MachineOperand &Op = TreeWalker.Pop(); |
| 740 | |
Dan Gohman | 1462faa | 2015-11-16 16:18:28 +0000 | [diff] [blame] | 741 | // We're only interested in explicit virtual register operands. |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 742 | if (!Op.isReg()) |
Dan Gohman | 1462faa | 2015-11-16 16:18:28 +0000 | [diff] [blame] | 743 | continue; |
| 744 | |
| 745 | unsigned Reg = Op.getReg(); |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 746 | assert(Op.isUse() && "explicit_uses() should only iterate over uses"); |
| 747 | assert(!Op.isImplicit() && |
| 748 | "explicit_uses() should only iterate over explicit operands"); |
| 749 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) |
Dan Gohman | 1462faa | 2015-11-16 16:18:28 +0000 | [diff] [blame] | 750 | continue; |
| 751 | |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 752 | // Identify the definition for this register at this point. Most |
| 753 | // 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] | 754 | MachineInstr *Def = GetVRegDef(Reg, Insert, MRI, LIS); |
| 755 | if (!Def) |
| 756 | continue; |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 757 | |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 758 | // Don't nest an INLINE_ASM def into anything, because we don't have |
| 759 | // constraints for $pop outputs. |
| 760 | if (Def->getOpcode() == TargetOpcode::INLINEASM) |
| 761 | continue; |
| 762 | |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 763 | // Argument instructions represent live-in registers and not real |
| 764 | // instructions. |
| 765 | if (Def->getOpcode() == WebAssembly::ARGUMENT_I32 || |
| 766 | Def->getOpcode() == WebAssembly::ARGUMENT_I64 || |
| 767 | Def->getOpcode() == WebAssembly::ARGUMENT_F32 || |
| 768 | Def->getOpcode() == WebAssembly::ARGUMENT_F64) |
| 769 | continue; |
| 770 | |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 771 | // Decide which strategy to take. Prefer to move a single-use value |
| 772 | // over cloning it, and prefer cloning over introducing a tee_local. |
| 773 | // For moving, we require the def to be in the same block as the use; |
| 774 | // this makes things simpler (LiveIntervals' handleMove function only |
| 775 | // supports intra-block moves) and it's MachineSink's job to catch all |
| 776 | // the sinking opportunities anyway. |
| 777 | bool SameBlock = Def->getParent() == &MBB; |
Dan Gohman | fbfe5ec | 2016-01-28 03:59:09 +0000 | [diff] [blame] | 778 | bool CanMove = SameBlock && IsSafeToMove(Def, Insert, AA, LIS, MRI) && |
| 779 | !TreeWalker.IsOnStack(Reg); |
Dan Gohman | 12de0b9 | 2016-05-17 20:19:47 +0000 | [diff] [blame] | 780 | if (CanMove && HasOneUse(Reg, Def, MRI, MDT, LIS)) { |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 781 | Insert = MoveForSingleUse(Reg, Op, Def, MBB, Insert, LIS, MFI, MRI); |
Dan Gohman | 2644d74 | 2016-05-17 04:05:31 +0000 | [diff] [blame] | 782 | } else if (ShouldRematerialize(Def, AA, TII)) { |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 783 | Insert = RematerializeCheapDef(Reg, Op, Def, MBB, Insert, LIS, MFI, |
| 784 | MRI, TII, TRI); |
| 785 | } else if (CanMove && |
Dan Gohman | 1054570 | 2016-05-17 22:24:18 +0000 | [diff] [blame^] | 786 | OneUseDominatesOtherUses(Reg, Op, MBB, MRI, MDT, LIS, MFI)) { |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 787 | Insert = MoveAndTeeForMultiUse(Reg, Op, Def, MBB, Insert, LIS, MFI, |
| 788 | MRI, TII); |
| 789 | } else { |
| 790 | // We failed to stackify the operand. If the problem was ordering |
| 791 | // constraints, Commuting may be able to help. |
| 792 | if (!CanMove && SameBlock) |
| 793 | Commuting.MaybeCommute(Insert, TreeWalker, TII); |
| 794 | // Proceed to the next operand. |
| 795 | continue; |
Dan Gohman | b6fd39a | 2016-01-19 16:59:23 +0000 | [diff] [blame] | 796 | } |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 797 | |
| 798 | // We stackified an operand. Add the defining instruction's operands to |
| 799 | // the worklist stack now to continue to build an ever deeper tree. |
| 800 | Commuting.Reset(); |
| 801 | TreeWalker.PushOperands(Insert); |
Dan Gohman | 1462faa | 2015-11-16 16:18:28 +0000 | [diff] [blame] | 802 | } |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 803 | |
| 804 | // If we stackified any operands, skip over the tree to start looking for |
| 805 | // the next instruction we can build a tree on. |
| 806 | if (Insert != &*MII) { |
Dan Gohman | 8f59cf7 | 2016-01-06 18:29:35 +0000 | [diff] [blame] | 807 | ImposeStackOrdering(&*MII); |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 808 | MII = std::prev( |
Hans Wennborg | 369ebfe | 2016-03-14 11:04:15 +0000 | [diff] [blame] | 809 | llvm::make_reverse_iterator(MachineBasicBlock::iterator(Insert))); |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 810 | Changed = true; |
| 811 | } |
Dan Gohman | 1462faa | 2015-11-16 16:18:28 +0000 | [diff] [blame] | 812 | } |
| 813 | } |
| 814 | |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 815 | // If we used EXPR_STACK anywhere, add it to the live-in sets everywhere so |
| 816 | // that it never looks like a use-before-def. |
Dan Gohman | b0992da | 2015-11-20 02:19:12 +0000 | [diff] [blame] | 817 | if (Changed) { |
| 818 | MF.getRegInfo().addLiveIn(WebAssembly::EXPR_STACK); |
| 819 | for (MachineBasicBlock &MBB : MF) |
| 820 | MBB.addLiveIn(WebAssembly::EXPR_STACK); |
| 821 | } |
| 822 | |
Dan Gohman | 7bafa0e | 2015-11-20 02:33:24 +0000 | [diff] [blame] | 823 | #ifndef NDEBUG |
Dan Gohman | b6fd39a | 2016-01-19 16:59:23 +0000 | [diff] [blame] | 824 | // Verify that pushes and pops are performed in LIFO order. |
Dan Gohman | 7bafa0e | 2015-11-20 02:33:24 +0000 | [diff] [blame] | 825 | SmallVector<unsigned, 0> Stack; |
| 826 | for (MachineBasicBlock &MBB : MF) { |
| 827 | for (MachineInstr &MI : MBB) { |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 828 | if (MI.isDebugValue()) |
| 829 | continue; |
Dan Gohman | 7bafa0e | 2015-11-20 02:33:24 +0000 | [diff] [blame] | 830 | for (MachineOperand &MO : reverse(MI.explicit_operands())) { |
Dan Gohman | 7a6b982 | 2015-11-29 22:32:02 +0000 | [diff] [blame] | 831 | if (!MO.isReg()) |
| 832 | continue; |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 833 | unsigned Reg = MO.getReg(); |
Dan Gohman | 7bafa0e | 2015-11-20 02:33:24 +0000 | [diff] [blame] | 834 | |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 835 | if (MFI.isVRegStackified(Reg)) { |
Dan Gohman | 7bafa0e | 2015-11-20 02:33:24 +0000 | [diff] [blame] | 836 | if (MO.isDef()) |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 837 | Stack.push_back(Reg); |
Dan Gohman | 7bafa0e | 2015-11-20 02:33:24 +0000 | [diff] [blame] | 838 | else |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 839 | assert(Stack.pop_back_val() == Reg && |
| 840 | "Register stack pop should be paired with a push"); |
Dan Gohman | 7bafa0e | 2015-11-20 02:33:24 +0000 | [diff] [blame] | 841 | } |
| 842 | } |
| 843 | } |
| 844 | // TODO: Generalize this code to support keeping values on the stack across |
| 845 | // basic block boundaries. |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 846 | assert(Stack.empty() && |
| 847 | "Register stack pushes and pops should be balanced"); |
Dan Gohman | 7bafa0e | 2015-11-20 02:33:24 +0000 | [diff] [blame] | 848 | } |
| 849 | #endif |
| 850 | |
Dan Gohman | 1462faa | 2015-11-16 16:18:28 +0000 | [diff] [blame] | 851 | return Changed; |
| 852 | } |