blob: 4035eae83f3b48e4b0e5720505061a31f435774d [file] [log] [blame]
Dan Gohman81719f82015-11-25 16:55:01 +00001//===-- 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 Gohman391a98a2015-12-03 23:07:03 +000013/// 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 Gohman81719f82015-11-25 16:55:01 +000019///
Dan Gohmanbdf08d52016-01-26 04:01:11 +000020/// 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 Gohman81719f82015-11-25 16:55:01 +000025//===----------------------------------------------------------------------===//
26
27#include "WebAssembly.h"
28#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
29#include "WebAssemblyMachineFunctionInfo.h"
30#include "WebAssemblySubtarget.h"
Dan Gohmanbdf08d52016-01-26 04:01:11 +000031#include "llvm/Analysis/TargetLibraryInfo.h"
Dan Gohman81719f82015-11-25 16:55:01 +000032#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"
38using namespace llvm;
39
40#define DEBUG_TYPE "wasm-store-results"
41
42namespace {
43class WebAssemblyStoreResults final : public MachineFunctionPass {
44public:
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 Gohmanbdf08d52016-01-26 04:01:11 +000058 AU.addRequired<TargetLibraryInfoWrapperPass>();
Dan Gohman81719f82015-11-25 16:55:01 +000059 MachineFunctionPass::getAnalysisUsage(AU);
60 }
61
62 bool runOnMachineFunction(MachineFunction &MF) override;
63
64private:
65};
66} // end anonymous namespace
67
68char WebAssemblyStoreResults::ID = 0;
69FunctionPass *llvm::createWebAssemblyStoreResults() {
70 return new WebAssemblyStoreResults();
71}
72
Dan Gohmanbdf08d52016-01-26 04:01:11 +000073// Replace uses of FromReg with ToReg if they are dominated by MI.
74static 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 Gohman81719f82015-11-25 16:55:01 +0000107bool 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 Gohmanbdf08d52016-01-26 04:01:11 +0000115 const WebAssemblyTargetLowering &TLI =
116 *MF.getSubtarget<WebAssemblySubtarget>().getTargetLowering();
117 auto &LibInfo = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Dan Gohmanb949b9c2015-12-10 14:17:36 +0000118 bool Changed = false;
Dan Gohman81719f82015-11-25 16:55:01 +0000119
Dan Gohmand70e5902015-12-08 03:30:42 +0000120 assert(MRI.isSSA() && "StoreResults depends on SSA form");
121
Derek Schuff5268aaf2015-12-03 00:50:30 +0000122 for (auto &MBB : MF) {
123 DEBUG(dbgs() << "Basic Block: " << MBB.getName() << '\n');
Dan Gohman81719f82015-11-25 16:55:01 +0000124 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 Gohmanbdf08d52016-01-26 04:01:11 +0000136 case WebAssembly::STORE_I64: {
JF Bastienfbc89d22016-01-30 14:11:26 +0000137 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 Gohmanbdf08d52016-01-26 04:01:11 +0000148 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 Bastien1a6c7602016-01-26 20:22:42 +0000160 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 Gohmanbdf08d52016-01-26 04:01:11 +0000176 }
Dan Gohman391a98a2015-12-03 23:07:03 +0000177 }
Dan Gohman81719f82015-11-25 16:55:01 +0000178 }
179 }
Dan Gohmanbdf08d52016-01-26 04:01:11 +0000180 }
Derek Schuff5268aaf2015-12-03 00:50:30 +0000181 }
Dan Gohman81719f82015-11-25 16:55:01 +0000182
Dan Gohmanb949b9c2015-12-10 14:17:36 +0000183 return Changed;
Dan Gohman81719f82015-11-25 16:55:01 +0000184}