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 | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/MachineBlockFrequencyInfo.h" |
| 34 | #include "llvm/CodeGen/MachineDominators.h" |
| 35 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 36 | #include "llvm/CodeGen/Passes.h" |
| 37 | #include "llvm/Support/Debug.h" |
| 38 | #include "llvm/Support/raw_ostream.h" |
| 39 | using namespace llvm; |
| 40 | |
| 41 | #define DEBUG_TYPE "wasm-store-results" |
| 42 | |
| 43 | namespace { |
| 44 | class WebAssemblyStoreResults final : public MachineFunctionPass { |
| 45 | public: |
| 46 | static char ID; // Pass identification, replacement for typeid |
| 47 | WebAssemblyStoreResults() : MachineFunctionPass(ID) {} |
| 48 | |
| 49 | const char *getPassName() const override { |
| 50 | return "WebAssembly Store Results"; |
| 51 | } |
| 52 | |
| 53 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 54 | AU.setPreservesCFG(); |
| 55 | AU.addRequired<MachineBlockFrequencyInfo>(); |
| 56 | AU.addPreserved<MachineBlockFrequencyInfo>(); |
| 57 | AU.addRequired<MachineDominatorTree>(); |
| 58 | AU.addPreserved<MachineDominatorTree>(); |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 59 | AU.addRequired<LiveIntervals>(); |
| 60 | AU.addPreserved<SlotIndexes>(); |
| 61 | AU.addPreserved<LiveIntervals>(); |
Dan Gohman | bdf08d5 | 2016-01-26 04:01:11 +0000 | [diff] [blame] | 62 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 63 | MachineFunctionPass::getAnalysisUsage(AU); |
| 64 | } |
| 65 | |
| 66 | bool runOnMachineFunction(MachineFunction &MF) override; |
| 67 | |
| 68 | private: |
| 69 | }; |
| 70 | } // end anonymous namespace |
| 71 | |
| 72 | char WebAssemblyStoreResults::ID = 0; |
| 73 | FunctionPass *llvm::createWebAssemblyStoreResults() { |
| 74 | return new WebAssemblyStoreResults(); |
| 75 | } |
| 76 | |
Dan Gohman | bdf08d5 | 2016-01-26 04:01:11 +0000 | [diff] [blame] | 77 | // Replace uses of FromReg with ToReg if they are dominated by MI. |
| 78 | static bool ReplaceDominatedUses(MachineBasicBlock &MBB, MachineInstr &MI, |
| 79 | unsigned FromReg, unsigned ToReg, |
| 80 | const MachineRegisterInfo &MRI, |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 81 | MachineDominatorTree &MDT, |
| 82 | LiveIntervals &LIS) { |
Dan Gohman | bdf08d5 | 2016-01-26 04:01:11 +0000 | [diff] [blame] | 83 | bool Changed = false; |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 84 | |
| 85 | LiveInterval *FromLI = &LIS.getInterval(FromReg); |
| 86 | LiveInterval *ToLI = &LIS.getInterval(ToReg); |
| 87 | |
| 88 | SlotIndex FromIdx = LIS.getInstructionIndex(MI).getRegSlot(); |
| 89 | VNInfo *FromVNI = FromLI->getVNInfoAt(FromIdx); |
| 90 | |
| 91 | SmallVector<SlotIndex, 4> Indices; |
| 92 | |
Dominic Chen | a8a6382 | 2016-08-17 23:42:27 +0000 | [diff] [blame] | 93 | for (auto I = MRI.use_nodbg_begin(FromReg), E = MRI.use_nodbg_end(); I != E;) { |
Dan Gohman | bdf08d5 | 2016-01-26 04:01:11 +0000 | [diff] [blame] | 94 | MachineOperand &O = *I++; |
| 95 | MachineInstr *Where = O.getParent(); |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 96 | |
| 97 | // Check that MI dominates the instruction in the normal way. |
| 98 | if (&MI == Where || !MDT.dominates(&MI, Where)) |
| 99 | continue; |
| 100 | |
| 101 | // If this use gets a different value, skip it. |
| 102 | SlotIndex WhereIdx = LIS.getInstructionIndex(*Where); |
| 103 | VNInfo *WhereVNI = FromLI->getVNInfoAt(WhereIdx); |
| 104 | if (WhereVNI && WhereVNI != FromVNI) |
| 105 | continue; |
| 106 | |
| 107 | // Make sure ToReg isn't clobbered before it gets there. |
| 108 | VNInfo *ToVNI = ToLI->getVNInfoAt(WhereIdx); |
| 109 | if (ToVNI && ToVNI != FromVNI) |
| 110 | continue; |
| 111 | |
Dan Gohman | bdf08d5 | 2016-01-26 04:01:11 +0000 | [diff] [blame] | 112 | Changed = true; |
| 113 | DEBUG(dbgs() << "Setting operand " << O << " in " << *Where << " from " |
| 114 | << MI << "\n"); |
| 115 | O.setReg(ToReg); |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 116 | |
| 117 | // If the store's def was previously dead, it is no longer. |
Dan Gohman | 33e694a | 2016-05-12 04:19:09 +0000 | [diff] [blame] | 118 | if (!O.isUndef()) { |
| 119 | MI.getOperand(0).setIsDead(false); |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 120 | |
Dan Gohman | 33e694a | 2016-05-12 04:19:09 +0000 | [diff] [blame] | 121 | Indices.push_back(WhereIdx.getRegSlot()); |
| 122 | } |
Dan Gohman | bdf08d5 | 2016-01-26 04:01:11 +0000 | [diff] [blame] | 123 | } |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 124 | |
| 125 | if (Changed) { |
| 126 | // Extend ToReg's liveness. |
| 127 | LIS.extendToIndices(*ToLI, Indices); |
| 128 | |
| 129 | // Shrink FromReg's liveness. |
| 130 | LIS.shrinkToUses(FromLI); |
| 131 | |
| 132 | // If we replaced all dominated uses, FromReg is now killed at MI. |
| 133 | if (!FromLI->liveAt(FromIdx.getDeadSlot())) |
| 134 | MI.addRegisterKilled(FromReg, |
| 135 | MBB.getParent()->getSubtarget<WebAssemblySubtarget>() |
| 136 | .getRegisterInfo()); |
| 137 | } |
| 138 | |
Dan Gohman | bdf08d5 | 2016-01-26 04:01:11 +0000 | [diff] [blame] | 139 | return Changed; |
| 140 | } |
| 141 | |
JF Bastien | a5b8ea0d | 2016-02-01 10:46:16 +0000 | [diff] [blame] | 142 | static bool optimizeCall(MachineBasicBlock &MBB, MachineInstr &MI, |
| 143 | const MachineRegisterInfo &MRI, |
| 144 | MachineDominatorTree &MDT, |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 145 | LiveIntervals &LIS, |
JF Bastien | a5b8ea0d | 2016-02-01 10:46:16 +0000 | [diff] [blame] | 146 | const WebAssemblyTargetLowering &TLI, |
| 147 | const TargetLibraryInfo &LibInfo) { |
| 148 | MachineOperand &Op1 = MI.getOperand(1); |
| 149 | if (!Op1.isSymbol()) |
| 150 | return false; |
| 151 | |
| 152 | StringRef Name(Op1.getSymbolName()); |
| 153 | bool callReturnsInput = Name == TLI.getLibcallName(RTLIB::MEMCPY) || |
| 154 | Name == TLI.getLibcallName(RTLIB::MEMMOVE) || |
| 155 | Name == TLI.getLibcallName(RTLIB::MEMSET); |
| 156 | if (!callReturnsInput) |
| 157 | return false; |
| 158 | |
| 159 | LibFunc::Func Func; |
| 160 | if (!LibInfo.getLibFunc(Name, Func)) |
| 161 | return false; |
| 162 | |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 163 | unsigned FromReg = MI.getOperand(2).getReg(); |
| 164 | unsigned ToReg = MI.getOperand(0).getReg(); |
| 165 | if (MRI.getRegClass(FromReg) != MRI.getRegClass(ToReg)) |
JF Bastien | a5b8ea0d | 2016-02-01 10:46:16 +0000 | [diff] [blame] | 166 | report_fatal_error("Store results: call to builtin function with wrong " |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 167 | "signature, from/to mismatch"); |
| 168 | return ReplaceDominatedUses(MBB, MI, FromReg, ToReg, MRI, MDT, LIS); |
JF Bastien | a5b8ea0d | 2016-02-01 10:46:16 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 171 | bool WebAssemblyStoreResults::runOnMachineFunction(MachineFunction &MF) { |
| 172 | DEBUG({ |
| 173 | dbgs() << "********** Store Results **********\n" |
| 174 | << "********** Function: " << MF.getName() << '\n'; |
| 175 | }); |
| 176 | |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 177 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 178 | MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>(); |
Dan Gohman | bdf08d5 | 2016-01-26 04:01:11 +0000 | [diff] [blame] | 179 | const WebAssemblyTargetLowering &TLI = |
| 180 | *MF.getSubtarget<WebAssemblySubtarget>().getTargetLowering(); |
JF Bastien | a5b8ea0d | 2016-02-01 10:46:16 +0000 | [diff] [blame] | 181 | const auto &LibInfo = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 182 | LiveIntervals &LIS = getAnalysis<LiveIntervals>(); |
Dan Gohman | b949b9c | 2015-12-10 14:17:36 +0000 | [diff] [blame] | 183 | bool Changed = false; |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 184 | |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 185 | // We don't preserve SSA form. |
| 186 | MRI.leaveSSA(); |
| 187 | |
| 188 | assert(MRI.tracksLiveness() && "StoreResults expects liveness tracking"); |
Dan Gohman | d70e590 | 2015-12-08 03:30:42 +0000 | [diff] [blame] | 189 | |
Derek Schuff | 5268aaf | 2015-12-03 00:50:30 +0000 | [diff] [blame] | 190 | for (auto &MBB : MF) { |
| 191 | DEBUG(dbgs() << "Basic Block: " << MBB.getName() << '\n'); |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 192 | for (auto &MI : MBB) |
| 193 | switch (MI.getOpcode()) { |
| 194 | default: |
| 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: |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 198 | Changed |= optimizeCall(MBB, MI, MRI, MDT, LIS, TLI, LibInfo); |
JF Bastien | a5b8ea0d | 2016-02-01 10:46:16 +0000 | [diff] [blame] | 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 | } |