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 | |
JF Bastien | a5b8ea0d | 2016-02-01 10:46:16 +0000 | [diff] [blame^] | 107 | static bool optimizeStore(MachineBasicBlock &MBB, MachineInstr &MI, |
| 108 | const MachineRegisterInfo &MRI, |
| 109 | MachineDominatorTree &MDT) { |
| 110 | const auto &Stored = MI.getOperand(WebAssembly::StoreValueOperandNo); |
| 111 | switch (Stored.getType()) { |
| 112 | case MachineOperand::MO_Register: { |
| 113 | unsigned ToReg = MI.getOperand(0).getReg(); |
| 114 | unsigned FromReg = Stored.getReg(); |
| 115 | return ReplaceDominatedUses(MBB, MI, FromReg, ToReg, MRI, MDT); |
| 116 | } |
| 117 | case MachineOperand::MO_FrameIndex: |
| 118 | // TODO: optimize. |
| 119 | return false; |
| 120 | default: |
| 121 | report_fatal_error("Store results: store not consuming reg or frame index"); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | static bool optimizeCall(MachineBasicBlock &MBB, MachineInstr &MI, |
| 126 | const MachineRegisterInfo &MRI, |
| 127 | MachineDominatorTree &MDT, |
| 128 | const WebAssemblyTargetLowering &TLI, |
| 129 | const TargetLibraryInfo &LibInfo) { |
| 130 | MachineOperand &Op1 = MI.getOperand(1); |
| 131 | if (!Op1.isSymbol()) |
| 132 | return false; |
| 133 | |
| 134 | StringRef Name(Op1.getSymbolName()); |
| 135 | bool callReturnsInput = Name == TLI.getLibcallName(RTLIB::MEMCPY) || |
| 136 | Name == TLI.getLibcallName(RTLIB::MEMMOVE) || |
| 137 | Name == TLI.getLibcallName(RTLIB::MEMSET); |
| 138 | if (!callReturnsInput) |
| 139 | return false; |
| 140 | |
| 141 | LibFunc::Func Func; |
| 142 | if (!LibInfo.getLibFunc(Name, Func)) |
| 143 | return false; |
| 144 | |
| 145 | const auto &Op2 = MI.getOperand(2); |
| 146 | switch (Op2.getType()) { |
| 147 | case MachineOperand::MO_Register: { |
| 148 | unsigned FromReg = Op2.getReg(); |
| 149 | unsigned ToReg = MI.getOperand(0).getReg(); |
| 150 | if (MRI.getRegClass(FromReg) != MRI.getRegClass(ToReg)) |
| 151 | report_fatal_error("Store results: call to builtin function with wrong " |
| 152 | "signature, from/to mismatch"); |
| 153 | return ReplaceDominatedUses(MBB, MI, FromReg, ToReg, MRI, MDT); |
| 154 | } |
| 155 | case MachineOperand::MO_FrameIndex: |
| 156 | // TODO: optimize. |
| 157 | return false; |
| 158 | default: |
| 159 | report_fatal_error("Store results: call to builtin function with wrong " |
| 160 | "signature, not consuming reg or frame index"); |
| 161 | } |
| 162 | } |
| 163 | |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 164 | bool WebAssemblyStoreResults::runOnMachineFunction(MachineFunction &MF) { |
| 165 | DEBUG({ |
| 166 | dbgs() << "********** Store Results **********\n" |
| 167 | << "********** Function: " << MF.getName() << '\n'; |
| 168 | }); |
| 169 | |
| 170 | const MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 171 | MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>(); |
Dan Gohman | bdf08d5 | 2016-01-26 04:01:11 +0000 | [diff] [blame] | 172 | const WebAssemblyTargetLowering &TLI = |
| 173 | *MF.getSubtarget<WebAssemblySubtarget>().getTargetLowering(); |
JF Bastien | a5b8ea0d | 2016-02-01 10:46:16 +0000 | [diff] [blame^] | 174 | const auto &LibInfo = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); |
Dan Gohman | b949b9c | 2015-12-10 14:17:36 +0000 | [diff] [blame] | 175 | bool Changed = false; |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 176 | |
Dan Gohman | d70e590 | 2015-12-08 03:30:42 +0000 | [diff] [blame] | 177 | assert(MRI.isSSA() && "StoreResults depends on SSA form"); |
| 178 | |
Derek Schuff | 5268aaf | 2015-12-03 00:50:30 +0000 | [diff] [blame] | 179 | for (auto &MBB : MF) { |
| 180 | DEBUG(dbgs() << "Basic Block: " << MBB.getName() << '\n'); |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 181 | for (auto &MI : MBB) |
| 182 | switch (MI.getOpcode()) { |
| 183 | default: |
| 184 | break; |
| 185 | case WebAssembly::STORE8_I32: |
| 186 | case WebAssembly::STORE16_I32: |
| 187 | case WebAssembly::STORE8_I64: |
| 188 | case WebAssembly::STORE16_I64: |
| 189 | case WebAssembly::STORE32_I64: |
| 190 | case WebAssembly::STORE_F32: |
| 191 | case WebAssembly::STORE_F64: |
| 192 | case WebAssembly::STORE_I32: |
JF Bastien | a5b8ea0d | 2016-02-01 10:46:16 +0000 | [diff] [blame^] | 193 | case WebAssembly::STORE_I64: |
| 194 | Changed |= optimizeStore(MBB, MI, MRI, MDT); |
Dan Gohman | bdf08d5 | 2016-01-26 04:01:11 +0000 | [diff] [blame] | 195 | break; |
Dan Gohman | bdf08d5 | 2016-01-26 04:01:11 +0000 | [diff] [blame] | 196 | case WebAssembly::CALL_I32: |
JF Bastien | a5b8ea0d | 2016-02-01 10:46:16 +0000 | [diff] [blame^] | 197 | case WebAssembly::CALL_I64: |
| 198 | Changed |= optimizeCall(MBB, MI, MRI, MDT, TLI, LibInfo); |
| 199 | break; |
Dan Gohman | bdf08d5 | 2016-01-26 04:01:11 +0000 | [diff] [blame] | 200 | } |
Derek Schuff | 5268aaf | 2015-12-03 00:50:30 +0000 | [diff] [blame] | 201 | } |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 202 | |
Dan Gohman | b949b9c | 2015-12-10 14:17:36 +0000 | [diff] [blame] | 203 | return Changed; |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 204 | } |