Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 1 | //===-- WebAssemblyStoreResults.cpp - Optimize using store result values --===// |
| 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 an optimization pass using store result values. |
| 12 | /// |
Dan Gohman | 391a98a | 2015-12-03 23:07:03 +0000 | [diff] [blame] | 13 | /// WebAssembly's store instructions return the stored value. This is to enable |
| 14 | /// an optimization wherein uses of the stored value can be replaced by uses of |
| 15 | /// the store's result value, making the stored value register more likely to |
| 16 | /// be single-use, thus more likely to be useful to register stackifying, and |
| 17 | /// potentially also exposing the store to register stackifying. These both can |
| 18 | /// reduce get_local/set_local traffic. |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 19 | /// |
Dan Gohman | bdf08d5 | 2016-01-26 04:01:11 +0000 | [diff] [blame] | 20 | /// This pass also performs this optimization for memcpy, memmove, and memset |
| 21 | /// calls, since the LLVM intrinsics for these return void so they can't use the |
| 22 | /// returned attribute and consequently aren't handled by the OptimizeReturned |
| 23 | /// pass. |
| 24 | /// |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 25 | //===----------------------------------------------------------------------===// |
| 26 | |
| 27 | #include "WebAssembly.h" |
| 28 | #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" |
| 29 | #include "WebAssemblyMachineFunctionInfo.h" |
| 30 | #include "WebAssemblySubtarget.h" |
Dan Gohman | bdf08d5 | 2016-01-26 04:01:11 +0000 | [diff] [blame] | 31 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineBlockFrequencyInfo.h" |
| 33 | #include "llvm/CodeGen/MachineDominators.h" |
| 34 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 35 | #include "llvm/CodeGen/Passes.h" |
| 36 | #include "llvm/Support/Debug.h" |
| 37 | #include "llvm/Support/raw_ostream.h" |
| 38 | using namespace llvm; |
| 39 | |
| 40 | #define DEBUG_TYPE "wasm-store-results" |
| 41 | |
| 42 | namespace { |
| 43 | class WebAssemblyStoreResults final : public MachineFunctionPass { |
| 44 | public: |
| 45 | static char ID; // Pass identification, replacement for typeid |
| 46 | WebAssemblyStoreResults() : MachineFunctionPass(ID) {} |
| 47 | |
| 48 | const char *getPassName() const override { |
| 49 | return "WebAssembly Store Results"; |
| 50 | } |
| 51 | |
| 52 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 53 | AU.setPreservesCFG(); |
| 54 | AU.addRequired<MachineBlockFrequencyInfo>(); |
| 55 | AU.addPreserved<MachineBlockFrequencyInfo>(); |
| 56 | AU.addRequired<MachineDominatorTree>(); |
| 57 | AU.addPreserved<MachineDominatorTree>(); |
Dan Gohman | bdf08d5 | 2016-01-26 04:01:11 +0000 | [diff] [blame] | 58 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 59 | MachineFunctionPass::getAnalysisUsage(AU); |
| 60 | } |
| 61 | |
| 62 | bool runOnMachineFunction(MachineFunction &MF) override; |
| 63 | |
| 64 | private: |
| 65 | }; |
| 66 | } // end anonymous namespace |
| 67 | |
| 68 | char WebAssemblyStoreResults::ID = 0; |
| 69 | FunctionPass *llvm::createWebAssemblyStoreResults() { |
| 70 | return new WebAssemblyStoreResults(); |
| 71 | } |
| 72 | |
Dan Gohman | bdf08d5 | 2016-01-26 04:01:11 +0000 | [diff] [blame] | 73 | // Replace uses of FromReg with ToReg if they are dominated by MI. |
| 74 | static bool ReplaceDominatedUses(MachineBasicBlock &MBB, MachineInstr &MI, |
| 75 | unsigned FromReg, unsigned ToReg, |
| 76 | const MachineRegisterInfo &MRI, |
| 77 | MachineDominatorTree &MDT) { |
| 78 | bool Changed = false; |
| 79 | for (auto I = MRI.use_begin(FromReg), E = MRI.use_end(); I != E;) { |
| 80 | MachineOperand &O = *I++; |
| 81 | MachineInstr *Where = O.getParent(); |
| 82 | if (Where->getOpcode() == TargetOpcode::PHI) { |
| 83 | // PHIs use their operands on their incoming CFG edges rather than |
| 84 | // in their parent blocks. Get the basic block paired with this use |
| 85 | // of FromReg and check that MI's block dominates it. |
| 86 | MachineBasicBlock *Pred = |
| 87 | Where->getOperand(&O - &Where->getOperand(0) + 1).getMBB(); |
| 88 | if (!MDT.dominates(&MBB, Pred)) |
| 89 | continue; |
| 90 | } else { |
| 91 | // For a non-PHI, check that MI dominates the instruction in the |
| 92 | // normal way. |
| 93 | if (&MI == Where || !MDT.dominates(&MI, Where)) |
| 94 | continue; |
| 95 | } |
| 96 | Changed = true; |
| 97 | DEBUG(dbgs() << "Setting operand " << O << " in " << *Where << " from " |
| 98 | << MI << "\n"); |
| 99 | O.setReg(ToReg); |
| 100 | // If the store's def was previously dead, it is no longer. But the |
| 101 | // dead flag shouldn't be set yet. |
| 102 | assert(!MI.getOperand(0).isDead() && "Unexpected dead flag"); |
| 103 | } |
| 104 | return Changed; |
| 105 | } |
| 106 | |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 107 | bool WebAssemblyStoreResults::runOnMachineFunction(MachineFunction &MF) { |
| 108 | DEBUG({ |
| 109 | dbgs() << "********** Store Results **********\n" |
| 110 | << "********** Function: " << MF.getName() << '\n'; |
| 111 | }); |
| 112 | |
| 113 | const MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 114 | MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>(); |
Dan Gohman | bdf08d5 | 2016-01-26 04:01:11 +0000 | [diff] [blame] | 115 | const WebAssemblyTargetLowering &TLI = |
| 116 | *MF.getSubtarget<WebAssemblySubtarget>().getTargetLowering(); |
| 117 | auto &LibInfo = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); |
Dan Gohman | b949b9c | 2015-12-10 14:17:36 +0000 | [diff] [blame] | 118 | bool Changed = false; |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 119 | |
Dan Gohman | d70e590 | 2015-12-08 03:30:42 +0000 | [diff] [blame] | 120 | assert(MRI.isSSA() && "StoreResults depends on SSA form"); |
| 121 | |
Derek Schuff | 5268aaf | 2015-12-03 00:50:30 +0000 | [diff] [blame] | 122 | for (auto &MBB : MF) { |
| 123 | DEBUG(dbgs() << "Basic Block: " << MBB.getName() << '\n'); |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 124 | for (auto &MI : MBB) |
| 125 | switch (MI.getOpcode()) { |
| 126 | default: |
| 127 | break; |
| 128 | case WebAssembly::STORE8_I32: |
| 129 | case WebAssembly::STORE16_I32: |
| 130 | case WebAssembly::STORE8_I64: |
| 131 | case WebAssembly::STORE16_I64: |
| 132 | case WebAssembly::STORE32_I64: |
| 133 | case WebAssembly::STORE_F32: |
| 134 | case WebAssembly::STORE_F64: |
| 135 | case WebAssembly::STORE_I32: |
Dan Gohman | bdf08d5 | 2016-01-26 04:01:11 +0000 | [diff] [blame] | 136 | case WebAssembly::STORE_I64: { |
JF Bastien | fbc89d2 | 2016-01-30 14:11:26 +0000 | [diff] [blame] | 137 | const auto &Stored = MI.getOperand(WebAssembly::StoreValueOperandNo); |
| 138 | if (Stored.isReg()) { |
| 139 | unsigned ToReg = MI.getOperand(0).getReg(); |
| 140 | unsigned FromReg = Stored.getReg(); |
| 141 | Changed |= ReplaceDominatedUses(MBB, MI, FromReg, ToReg, MRI, MDT); |
| 142 | } else if (Stored.isFI()) { |
| 143 | break; |
| 144 | } else { |
| 145 | report_fatal_error( |
| 146 | "Store results: store not consuming reg or frame index"); |
| 147 | } |
Dan Gohman | bdf08d5 | 2016-01-26 04:01:11 +0000 | [diff] [blame] | 148 | break; |
| 149 | } |
| 150 | case WebAssembly::CALL_I32: |
| 151 | case WebAssembly::CALL_I64: { |
| 152 | MachineOperand &Op1 = MI.getOperand(1); |
| 153 | if (Op1.isSymbol()) { |
| 154 | StringRef Name(Op1.getSymbolName()); |
| 155 | if (Name == TLI.getLibcallName(RTLIB::MEMCPY) || |
| 156 | Name == TLI.getLibcallName(RTLIB::MEMMOVE) || |
| 157 | Name == TLI.getLibcallName(RTLIB::MEMSET)) { |
| 158 | LibFunc::Func Func; |
| 159 | if (LibInfo.getLibFunc(Name, Func)) { |
JF Bastien | 1a6c760 | 2016-01-26 20:22:42 +0000 | [diff] [blame] | 160 | const auto &Op2 = MI.getOperand(2); |
| 161 | if (Op2.isReg()) { |
| 162 | unsigned FromReg = Op2.getReg(); |
| 163 | unsigned ToReg = MI.getOperand(0).getReg(); |
| 164 | if (MRI.getRegClass(FromReg) != MRI.getRegClass(ToReg)) |
| 165 | report_fatal_error("Store results: call to builtin function " |
| 166 | "with wrong signature, from/to mismatch"); |
| 167 | Changed |= |
| 168 | ReplaceDominatedUses(MBB, MI, FromReg, ToReg, MRI, MDT); |
| 169 | } else if (Op2.isFI()) { |
| 170 | break; |
| 171 | } else { |
| 172 | report_fatal_error("Store results: call to builtin function " |
| 173 | "with wrong signature, not consuming reg or " |
| 174 | "frame index"); |
| 175 | } |
Dan Gohman | bdf08d5 | 2016-01-26 04:01:11 +0000 | [diff] [blame] | 176 | } |
Dan Gohman | 391a98a | 2015-12-03 23:07:03 +0000 | [diff] [blame] | 177 | } |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 178 | } |
| 179 | } |
Dan Gohman | bdf08d5 | 2016-01-26 04:01:11 +0000 | [diff] [blame] | 180 | } |
Derek Schuff | 5268aaf | 2015-12-03 00:50:30 +0000 | [diff] [blame] | 181 | } |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 182 | |
Dan Gohman | b949b9c | 2015-12-10 14:17:36 +0000 | [diff] [blame] | 183 | return Changed; |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 184 | } |