blob: 5751ec92bd012aee8ad576f7ed8bca8a6b07655b [file] [log] [blame]
Heejin Ahn321d5222019-01-08 22:35:18 +00001//== WebAssemblyMemIntrinsicResults.cpp - Optimize memory intrinsic results ==//
Dan Gohman81719f82015-11-25 16:55:01 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Dan Gohman81719f82015-11-25 16:55:01 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
Heejin Ahn321d5222019-01-08 22:35:18 +000010/// This file implements an optimization pass using memory intrinsic results.
Dan Gohman81719f82015-11-25 16:55:01 +000011///
Heejin Ahn321d5222019-01-08 22:35:18 +000012/// Calls to memory intrinsics (memcpy, memmove, memset) return the destination
13/// address. They are in the form of
14/// %dst_new = call @memcpy %dst, %src, %len
15/// where %dst and %dst_new registers contain the same value.
Dan Gohman81719f82015-11-25 16:55:01 +000016///
Heejin Ahn321d5222019-01-08 22:35:18 +000017/// This is to enable an optimization wherein uses of the %dst register used in
18/// the parameter can be replaced by uses of the %dst_new register used in the
19/// result, making the %dst register more likely to be single-use, thus more
20/// likely to be useful to register stackifying, and potentially also exposing
21/// the call instruction itself to register stackifying. These both can reduce
22/// local.get/local.set traffic.
23///
24/// The LLVM intrinsics for these return void so they can't use the returned
25/// attribute and consequently aren't handled by the OptimizeReturned pass.
Dan Gohmanbdf08d52016-01-26 04:01:11 +000026///
Dan Gohman81719f82015-11-25 16:55:01 +000027//===----------------------------------------------------------------------===//
28
Dan Gohman81719f82015-11-25 16:55:01 +000029#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000030#include "WebAssembly.h"
Dan Gohman81719f82015-11-25 16:55:01 +000031#include "WebAssemblyMachineFunctionInfo.h"
32#include "WebAssemblySubtarget.h"
Dan Gohmanbdf08d52016-01-26 04:01:11 +000033#include "llvm/Analysis/TargetLibraryInfo.h"
Matthias Braunf8422972017-12-13 02:51:04 +000034#include "llvm/CodeGen/LiveIntervals.h"
Dan Gohman81719f82015-11-25 16:55:01 +000035#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
36#include "llvm/CodeGen/MachineDominators.h"
37#include "llvm/CodeGen/MachineRegisterInfo.h"
38#include "llvm/CodeGen/Passes.h"
39#include "llvm/Support/Debug.h"
40#include "llvm/Support/raw_ostream.h"
41using namespace llvm;
42
Heejin Ahn321d5222019-01-08 22:35:18 +000043#define DEBUG_TYPE "wasm-mem-intrinsic-results"
Dan Gohman81719f82015-11-25 16:55:01 +000044
45namespace {
Heejin Ahn321d5222019-01-08 22:35:18 +000046class WebAssemblyMemIntrinsicResults final : public MachineFunctionPass {
Dan Gohman81719f82015-11-25 16:55:01 +000047public:
48 static char ID; // Pass identification, replacement for typeid
Heejin Ahn321d5222019-01-08 22:35:18 +000049 WebAssemblyMemIntrinsicResults() : MachineFunctionPass(ID) {}
Dan Gohman81719f82015-11-25 16:55:01 +000050
Heejin Ahn321d5222019-01-08 22:35:18 +000051 StringRef getPassName() const override {
52 return "WebAssembly Memory Intrinsic Results";
53 }
Dan Gohman81719f82015-11-25 16:55:01 +000054
55 void getAnalysisUsage(AnalysisUsage &AU) const override {
56 AU.setPreservesCFG();
57 AU.addRequired<MachineBlockFrequencyInfo>();
58 AU.addPreserved<MachineBlockFrequencyInfo>();
59 AU.addRequired<MachineDominatorTree>();
60 AU.addPreserved<MachineDominatorTree>();
Dan Gohman0cfb5f82016-05-10 04:24:02 +000061 AU.addRequired<LiveIntervals>();
62 AU.addPreserved<SlotIndexes>();
63 AU.addPreserved<LiveIntervals>();
Dan Gohmanbdf08d52016-01-26 04:01:11 +000064 AU.addRequired<TargetLibraryInfoWrapperPass>();
Dan Gohman81719f82015-11-25 16:55:01 +000065 MachineFunctionPass::getAnalysisUsage(AU);
66 }
67
68 bool runOnMachineFunction(MachineFunction &MF) override;
69
70private:
71};
72} // end anonymous namespace
73
Heejin Ahn321d5222019-01-08 22:35:18 +000074char WebAssemblyMemIntrinsicResults::ID = 0;
75INITIALIZE_PASS(WebAssemblyMemIntrinsicResults, DEBUG_TYPE,
76 "Optimize memory intrinsic result values for WebAssembly",
77 false, false)
Jacob Gravelle40926452018-03-30 20:36:58 +000078
Heejin Ahn321d5222019-01-08 22:35:18 +000079FunctionPass *llvm::createWebAssemblyMemIntrinsicResults() {
80 return new WebAssemblyMemIntrinsicResults();
Dan Gohman81719f82015-11-25 16:55:01 +000081}
82
Dan Gohmanbdf08d52016-01-26 04:01:11 +000083// Replace uses of FromReg with ToReg if they are dominated by MI.
84static bool ReplaceDominatedUses(MachineBasicBlock &MBB, MachineInstr &MI,
85 unsigned FromReg, unsigned ToReg,
86 const MachineRegisterInfo &MRI,
Dan Gohman0cfb5f82016-05-10 04:24:02 +000087 MachineDominatorTree &MDT,
88 LiveIntervals &LIS) {
Dan Gohmanbdf08d52016-01-26 04:01:11 +000089 bool Changed = false;
Dan Gohman0cfb5f82016-05-10 04:24:02 +000090
91 LiveInterval *FromLI = &LIS.getInterval(FromReg);
92 LiveInterval *ToLI = &LIS.getInterval(ToReg);
93
94 SlotIndex FromIdx = LIS.getInstructionIndex(MI).getRegSlot();
95 VNInfo *FromVNI = FromLI->getVNInfoAt(FromIdx);
96
97 SmallVector<SlotIndex, 4> Indices;
98
Heejin Ahnf208f632018-09-05 01:27:38 +000099 for (auto I = MRI.use_nodbg_begin(FromReg), E = MRI.use_nodbg_end();
100 I != E;) {
Dan Gohmanbdf08d52016-01-26 04:01:11 +0000101 MachineOperand &O = *I++;
102 MachineInstr *Where = O.getParent();
Dan Gohman0cfb5f82016-05-10 04:24:02 +0000103
104 // Check that MI dominates the instruction in the normal way.
105 if (&MI == Where || !MDT.dominates(&MI, Where))
106 continue;
107
108 // If this use gets a different value, skip it.
109 SlotIndex WhereIdx = LIS.getInstructionIndex(*Where);
110 VNInfo *WhereVNI = FromLI->getVNInfoAt(WhereIdx);
111 if (WhereVNI && WhereVNI != FromVNI)
112 continue;
113
114 // Make sure ToReg isn't clobbered before it gets there.
115 VNInfo *ToVNI = ToLI->getVNInfoAt(WhereIdx);
116 if (ToVNI && ToVNI != FromVNI)
117 continue;
118
Dan Gohmanbdf08d52016-01-26 04:01:11 +0000119 Changed = true;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000120 LLVM_DEBUG(dbgs() << "Setting operand " << O << " in " << *Where << " from "
121 << MI << "\n");
Dan Gohmanbdf08d52016-01-26 04:01:11 +0000122 O.setReg(ToReg);
Dan Gohman0cfb5f82016-05-10 04:24:02 +0000123
124 // If the store's def was previously dead, it is no longer.
Dan Gohman33e694a2016-05-12 04:19:09 +0000125 if (!O.isUndef()) {
126 MI.getOperand(0).setIsDead(false);
Dan Gohman0cfb5f82016-05-10 04:24:02 +0000127
Dan Gohman33e694a2016-05-12 04:19:09 +0000128 Indices.push_back(WhereIdx.getRegSlot());
129 }
Dan Gohmanbdf08d52016-01-26 04:01:11 +0000130 }
Dan Gohman0cfb5f82016-05-10 04:24:02 +0000131
132 if (Changed) {
133 // Extend ToReg's liveness.
134 LIS.extendToIndices(*ToLI, Indices);
135
136 // Shrink FromReg's liveness.
137 LIS.shrinkToUses(FromLI);
138
139 // If we replaced all dominated uses, FromReg is now killed at MI.
140 if (!FromLI->liveAt(FromIdx.getDeadSlot()))
Heejin Ahnf208f632018-09-05 01:27:38 +0000141 MI.addRegisterKilled(FromReg, MBB.getParent()
142 ->getSubtarget<WebAssemblySubtarget>()
143 .getRegisterInfo());
Dan Gohman0cfb5f82016-05-10 04:24:02 +0000144 }
145
Dan Gohmanbdf08d52016-01-26 04:01:11 +0000146 return Changed;
147}
148
JF Bastiena5b8ea0d2016-02-01 10:46:16 +0000149static bool optimizeCall(MachineBasicBlock &MBB, MachineInstr &MI,
150 const MachineRegisterInfo &MRI,
Heejin Ahnf208f632018-09-05 01:27:38 +0000151 MachineDominatorTree &MDT, LiveIntervals &LIS,
JF Bastiena5b8ea0d2016-02-01 10:46:16 +0000152 const WebAssemblyTargetLowering &TLI,
153 const TargetLibraryInfo &LibInfo) {
154 MachineOperand &Op1 = MI.getOperand(1);
155 if (!Op1.isSymbol())
156 return false;
157
158 StringRef Name(Op1.getSymbolName());
159 bool callReturnsInput = Name == TLI.getLibcallName(RTLIB::MEMCPY) ||
160 Name == TLI.getLibcallName(RTLIB::MEMMOVE) ||
161 Name == TLI.getLibcallName(RTLIB::MEMSET);
162 if (!callReturnsInput)
163 return false;
164
Derek Schuff4b320b72017-01-24 00:01:18 +0000165 LibFunc Func;
JF Bastiena5b8ea0d2016-02-01 10:46:16 +0000166 if (!LibInfo.getLibFunc(Name, Func))
167 return false;
168
Dan Gohman0cfb5f82016-05-10 04:24:02 +0000169 unsigned FromReg = MI.getOperand(2).getReg();
170 unsigned ToReg = MI.getOperand(0).getReg();
171 if (MRI.getRegClass(FromReg) != MRI.getRegClass(ToReg))
Heejin Ahn321d5222019-01-08 22:35:18 +0000172 report_fatal_error("Memory Intrinsic results: call to builtin function "
173 "with wrong signature, from/to mismatch");
Dan Gohman0cfb5f82016-05-10 04:24:02 +0000174 return ReplaceDominatedUses(MBB, MI, FromReg, ToReg, MRI, MDT, LIS);
JF Bastiena5b8ea0d2016-02-01 10:46:16 +0000175}
176
Heejin Ahn321d5222019-01-08 22:35:18 +0000177bool WebAssemblyMemIntrinsicResults::runOnMachineFunction(MachineFunction &MF) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000178 LLVM_DEBUG({
Heejin Ahn321d5222019-01-08 22:35:18 +0000179 dbgs() << "********** Memory Intrinsic Results **********\n"
Dan Gohman81719f82015-11-25 16:55:01 +0000180 << "********** Function: " << MF.getName() << '\n';
181 });
182
Dan Gohman0cfb5f82016-05-10 04:24:02 +0000183 MachineRegisterInfo &MRI = MF.getRegInfo();
Dan Gohman81719f82015-11-25 16:55:01 +0000184 MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>();
Dan Gohmanbdf08d52016-01-26 04:01:11 +0000185 const WebAssemblyTargetLowering &TLI =
186 *MF.getSubtarget<WebAssemblySubtarget>().getTargetLowering();
JF Bastiena5b8ea0d2016-02-01 10:46:16 +0000187 const auto &LibInfo = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Dan Gohman0cfb5f82016-05-10 04:24:02 +0000188 LiveIntervals &LIS = getAnalysis<LiveIntervals>();
Dan Gohmanb949b9c2015-12-10 14:17:36 +0000189 bool Changed = false;
Dan Gohman81719f82015-11-25 16:55:01 +0000190
Dan Gohman0cfb5f82016-05-10 04:24:02 +0000191 // We don't preserve SSA form.
192 MRI.leaveSSA();
193
Heejin Ahn321d5222019-01-08 22:35:18 +0000194 assert(MRI.tracksLiveness() &&
195 "MemIntrinsicResults expects liveness tracking");
Dan Gohmand70e5902015-12-08 03:30:42 +0000196
Derek Schuff5268aaf2015-12-03 00:50:30 +0000197 for (auto &MBB : MF) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000198 LLVM_DEBUG(dbgs() << "Basic Block: " << MBB.getName() << '\n');
Dan Gohman81719f82015-11-25 16:55:01 +0000199 for (auto &MI : MBB)
200 switch (MI.getOpcode()) {
201 default:
202 break;
Dan Gohmanbdf08d52016-01-26 04:01:11 +0000203 case WebAssembly::CALL_I32:
JF Bastiena5b8ea0d2016-02-01 10:46:16 +0000204 case WebAssembly::CALL_I64:
Dan Gohman0cfb5f82016-05-10 04:24:02 +0000205 Changed |= optimizeCall(MBB, MI, MRI, MDT, LIS, TLI, LibInfo);
JF Bastiena5b8ea0d2016-02-01 10:46:16 +0000206 break;
Dan Gohmanbdf08d52016-01-26 04:01:11 +0000207 }
Derek Schuff5268aaf2015-12-03 00:50:30 +0000208 }
Dan Gohman81719f82015-11-25 16:55:01 +0000209
Dan Gohmanb949b9c2015-12-10 14:17:36 +0000210 return Changed;
Dan Gohman81719f82015-11-25 16:55:01 +0000211}