blob: 4e08b2b079eb0e6bb3085a5696165be87f4b327c [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///
20//===----------------------------------------------------------------------===//
21
22#include "WebAssembly.h"
23#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
24#include "WebAssemblyMachineFunctionInfo.h"
25#include "WebAssemblySubtarget.h"
26#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
27#include "llvm/CodeGen/MachineDominators.h"
28#include "llvm/CodeGen/MachineRegisterInfo.h"
29#include "llvm/CodeGen/Passes.h"
30#include "llvm/Support/Debug.h"
31#include "llvm/Support/raw_ostream.h"
32using namespace llvm;
33
34#define DEBUG_TYPE "wasm-store-results"
35
36namespace {
37class WebAssemblyStoreResults final : public MachineFunctionPass {
38public:
39 static char ID; // Pass identification, replacement for typeid
40 WebAssemblyStoreResults() : MachineFunctionPass(ID) {}
41
42 const char *getPassName() const override {
43 return "WebAssembly Store Results";
44 }
45
46 void getAnalysisUsage(AnalysisUsage &AU) const override {
47 AU.setPreservesCFG();
48 AU.addRequired<MachineBlockFrequencyInfo>();
49 AU.addPreserved<MachineBlockFrequencyInfo>();
50 AU.addRequired<MachineDominatorTree>();
51 AU.addPreserved<MachineDominatorTree>();
52 MachineFunctionPass::getAnalysisUsage(AU);
53 }
54
55 bool runOnMachineFunction(MachineFunction &MF) override;
56
57private:
58};
59} // end anonymous namespace
60
61char WebAssemblyStoreResults::ID = 0;
62FunctionPass *llvm::createWebAssemblyStoreResults() {
63 return new WebAssemblyStoreResults();
64}
65
66bool WebAssemblyStoreResults::runOnMachineFunction(MachineFunction &MF) {
67 DEBUG({
68 dbgs() << "********** Store Results **********\n"
69 << "********** Function: " << MF.getName() << '\n';
70 });
71
72 const MachineRegisterInfo &MRI = MF.getRegInfo();
73 MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>();
Dan Gohmanb949b9c2015-12-10 14:17:36 +000074 bool Changed = false;
Dan Gohman81719f82015-11-25 16:55:01 +000075
Dan Gohmand70e5902015-12-08 03:30:42 +000076 assert(MRI.isSSA() && "StoreResults depends on SSA form");
77
Derek Schuff5268aaf2015-12-03 00:50:30 +000078 for (auto &MBB : MF) {
79 DEBUG(dbgs() << "Basic Block: " << MBB.getName() << '\n');
Dan Gohman81719f82015-11-25 16:55:01 +000080 for (auto &MI : MBB)
81 switch (MI.getOpcode()) {
82 default:
83 break;
84 case WebAssembly::STORE8_I32:
85 case WebAssembly::STORE16_I32:
86 case WebAssembly::STORE8_I64:
87 case WebAssembly::STORE16_I64:
88 case WebAssembly::STORE32_I64:
89 case WebAssembly::STORE_F32:
90 case WebAssembly::STORE_F64:
91 case WebAssembly::STORE_I32:
92 case WebAssembly::STORE_I64:
93 unsigned ToReg = MI.getOperand(0).getReg();
Derek Schuff9d779522015-12-05 00:26:39 +000094 unsigned FromReg = MI.getOperand(3).getReg();
Dan Gohman81719f82015-11-25 16:55:01 +000095 for (auto I = MRI.use_begin(FromReg), E = MRI.use_end(); I != E;) {
96 MachineOperand &O = *I++;
97 MachineInstr *Where = O.getParent();
Dan Gohman391a98a2015-12-03 23:07:03 +000098 if (Where->getOpcode() == TargetOpcode::PHI) {
99 // PHIs use their operands on their incoming CFG edges rather than
100 // in their parent blocks. Get the basic block paired with this use
101 // of FromReg and check that MI's block dominates it.
102 MachineBasicBlock *Pred =
103 Where->getOperand(&O - &Where->getOperand(0) + 1).getMBB();
104 if (!MDT.dominates(&MBB, Pred))
105 continue;
106 } else {
107 // For a non-PHI, check that MI dominates the instruction in the
108 // normal way.
109 if (&MI == Where || !MDT.dominates(&MI, Where))
110 continue;
111 }
Dan Gohmanb949b9c2015-12-10 14:17:36 +0000112 Changed = true;
Dan Gohman391a98a2015-12-03 23:07:03 +0000113 DEBUG(dbgs() << "Setting operand " << O << " in " << *Where
114 << " from " << MI << "\n");
Dan Gohman81719f82015-11-25 16:55:01 +0000115 O.setReg(ToReg);
Dan Gohman87b4aa82015-12-14 21:53:54 +0000116 // If the store's def was previously dead, it is no longer. But the
117 // dead flag shouldn't be set yet.
118 assert(!MI.getOperand(0).isDead() && "Dead flag set on store result");
Dan Gohman81719f82015-11-25 16:55:01 +0000119 }
120 }
Derek Schuff5268aaf2015-12-03 00:50:30 +0000121 }
Dan Gohman81719f82015-11-25 16:55:01 +0000122
Dan Gohmanb949b9c2015-12-10 14:17:36 +0000123 return Changed;
Dan Gohman81719f82015-11-25 16:55:01 +0000124}