1//===-- WebAssemblyPeephole.cpp - WebAssembly Peephole Optimizations ------===//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// Late peephole optimizations for WebAssembly.
11///
12//===----------------------------------------------------------------------===//
13
14#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
15#include "WebAssembly.h"
16#include "WebAssemblyMachineFunctionInfo.h"
17#include "WebAssemblySubtarget.h"
18#include "WebAssemblyUtilities.h"
19#include "llvm/Analysis/TargetLibraryInfo.h"
20#include "llvm/CodeGen/LibcallLoweringInfo.h"
21#include "llvm/CodeGen/MachineFunctionAnalysisManager.h"
22#include "llvm/CodeGen/MachineFunctionPass.h"
23#include "llvm/CodeGen/MachineInstrBuilder.h"
24#include "llvm/CodeGen/MachinePassManager.h"
25#include "llvm/CodeGen/MachineRegisterInfo.h"
26#include "llvm/IR/Analysis.h"
27#include "llvm/Support/ErrorHandling.h"
28using namespace llvm;
29
30#define DEBUG_TYPE "wasm-peephole"
31
32static cl::opt<bool> DisableWebAssemblyFallthroughReturnOpt(
33 "disable-wasm-fallthrough-return-opt", cl::Hidden,
34 cl::desc("WebAssembly: Disable fallthrough-return optimizations."),
35 cl::init(Val: false));
36
37namespace {
38class WebAssemblyPeepholeLegacy final : public MachineFunctionPass {
39 StringRef getPassName() const override {
40 return "WebAssembly late peephole optimizer";
41 }
42
43 void getAnalysisUsage(AnalysisUsage &AU) const override {
44 AU.setPreservesCFG();
45 AU.addRequired<TargetLibraryInfoWrapperPass>();
46 AU.addRequired<LibcallLoweringInfoWrapper>();
47 MachineFunctionPass::getAnalysisUsage(AU);
48 }
49
50 bool runOnMachineFunction(MachineFunction &MF) override;
51
52public:
53 static char ID;
54 WebAssemblyPeepholeLegacy() : MachineFunctionPass(ID) {}
55};
56} // end anonymous namespace
57
58char WebAssemblyPeepholeLegacy::ID = 0;
59INITIALIZE_PASS(WebAssemblyPeepholeLegacy, DEBUG_TYPE,
60 "WebAssembly peephole optimizations", false, false)
61
62FunctionPass *llvm::createWebAssemblyPeepholeLegacyPass() {
63 return new WebAssemblyPeepholeLegacy();
64}
65
66/// If desirable, rewrite NewReg to a drop register.
67static bool maybeRewriteToDrop(unsigned OldReg, unsigned NewReg,
68 MachineOperand &MO, WebAssemblyFunctionInfo &MFI,
69 MachineRegisterInfo &MRI) {
70 bool Changed = false;
71 if (OldReg == NewReg) {
72 Changed = true;
73 Register NewReg = MRI.createVirtualRegister(RegClass: MRI.getRegClass(Reg: OldReg));
74 MO.setReg(NewReg);
75 MO.setIsDead();
76 MFI.stackifyVReg(MRI, VReg: NewReg);
77 }
78 return Changed;
79}
80
81static bool maybeRewriteToFallthrough(MachineInstr &MI, MachineBasicBlock &MBB,
82 const MachineFunction &MF,
83 WebAssemblyFunctionInfo &MFI,
84 MachineRegisterInfo &MRI,
85 const WebAssemblyInstrInfo &TII) {
86 if (DisableWebAssemblyFallthroughReturnOpt)
87 return false;
88 if (&MBB != &MF.back())
89 return false;
90
91 MachineBasicBlock::iterator End = MBB.end();
92 --End;
93 assert(End->getOpcode() == WebAssembly::END_FUNCTION);
94 --End;
95 if (&MI != &*End)
96 return false;
97
98 for (auto &MO : MI.explicit_operands()) {
99 // If the operand isn't stackified, insert a COPY to read the operands and
100 // stackify them.
101 Register Reg = MO.getReg();
102 if (!MFI.isVRegStackified(VReg: Reg)) {
103 unsigned CopyLocalOpc;
104 const TargetRegisterClass *RegClass = MRI.getRegClass(Reg);
105 CopyLocalOpc = WebAssembly::getCopyOpcodeForRegClass(RC: RegClass);
106 Register NewReg = MRI.createVirtualRegister(RegClass);
107 BuildMI(BB&: MBB, I&: MI, MIMD: MI.getDebugLoc(), MCID: TII.get(Opcode: CopyLocalOpc), DestReg: NewReg)
108 .addReg(RegNo: Reg);
109 MO.setReg(NewReg);
110 MFI.stackifyVReg(MRI, VReg: NewReg);
111 }
112 }
113
114 MI.setDesc(TII.get(Opcode: WebAssembly::FALLTHROUGH_RETURN));
115 return true;
116}
117
118static bool peephole(MachineFunction &MF, TargetLibraryInfo &LibInfo,
119 const LibcallLoweringInfo &LibcallLowering) {
120 LLVM_DEBUG({
121 dbgs() << "********** Peephole **********\n"
122 << "********** Function: " << MF.getName() << '\n';
123 });
124
125 MachineRegisterInfo &MRI = MF.getRegInfo();
126 WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
127 const WebAssemblySubtarget &Subtarget =
128 MF.getSubtarget<WebAssemblySubtarget>();
129 const auto &TII = *Subtarget.getInstrInfo();
130
131 RTLIB::LibcallImpl MemcpyImpl = LibcallLowering.getLibcallImpl(Call: RTLIB::MEMCPY);
132 RTLIB::LibcallImpl MemmoveImpl =
133 LibcallLowering.getLibcallImpl(Call: RTLIB::MEMMOVE);
134 RTLIB::LibcallImpl MemsetImpl = LibcallLowering.getLibcallImpl(Call: RTLIB::MEMSET);
135
136 StringRef MemcpyName =
137 RTLIB::RuntimeLibcallsInfo::getLibcallImplName(CallImpl: MemcpyImpl);
138 StringRef MemmoveName =
139 RTLIB::RuntimeLibcallsInfo::getLibcallImplName(CallImpl: MemmoveImpl);
140 StringRef MemsetName =
141 RTLIB::RuntimeLibcallsInfo::getLibcallImplName(CallImpl: MemsetImpl);
142
143 bool Changed = false;
144
145 for (auto &MBB : MF)
146 for (auto &MI : MBB)
147 switch (MI.getOpcode()) {
148 default:
149 break;
150 case WebAssembly::CALL: {
151 MachineOperand &Op1 = MI.getOperand(i: 1);
152 if (Op1.isSymbol()) {
153 StringRef Name(Op1.getSymbolName());
154 if (Name == MemcpyName || Name == MemmoveName || Name == MemsetName) {
155 if (LibInfo.getLibFunc(funcName: Name) != NotLibFunc) {
156 const auto &Op2 = MI.getOperand(i: 2);
157 if (!Op2.isReg())
158 report_fatal_error(reason: "Peephole: call to builtin function with "
159 "wrong signature, not consuming reg");
160 MachineOperand &MO = MI.getOperand(i: 0);
161 Register OldReg = MO.getReg();
162 Register NewReg = Op2.getReg();
163
164 if (MRI.getRegClass(Reg: NewReg) != MRI.getRegClass(Reg: OldReg))
165 report_fatal_error(reason: "Peephole: call to builtin function with "
166 "wrong signature, from/to mismatch");
167 Changed |= maybeRewriteToDrop(OldReg, NewReg, MO, MFI, MRI);
168 }
169 }
170 }
171 break;
172 }
173 // Optimize away an explicit void return at the end of the function.
174 case WebAssembly::RETURN:
175 Changed |= maybeRewriteToFallthrough(MI, MBB, MF, MFI, MRI, TII);
176 break;
177 }
178
179 return Changed;
180}
181
182bool WebAssemblyPeepholeLegacy::runOnMachineFunction(MachineFunction &MF) {
183 TargetLibraryInfo &LibInfo =
184 getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F: MF.getFunction());
185 const WebAssemblySubtarget &Subtarget =
186 MF.getSubtarget<WebAssemblySubtarget>();
187 const LibcallLoweringInfo &LibcallLowering =
188 getAnalysis<LibcallLoweringInfoWrapper>().getLibcallLowering(
189 M: *MF.getFunction().getParent(), Subtarget);
190 return peephole(MF, LibInfo, LibcallLowering);
191}
192
193PreservedAnalyses
194WebAssemblyPeepholePass::run(MachineFunction &MF,
195 MachineFunctionAnalysisManager &MFAM) {
196 TargetLibraryInfo &LibInfo =
197 MFAM.getResult<FunctionAnalysisManagerMachineFunctionProxy>(IR&: MF)
198 .getManager()
199 .getResult<TargetLibraryAnalysis>(IR&: MF.getFunction());
200 const WebAssemblySubtarget &Subtarget =
201 MF.getSubtarget<WebAssemblySubtarget>();
202 const LibcallLoweringInfo &LibcallLowering = getLibcallLowering(
203 ModuleInfo: *MFAM.getResult<ModuleAnalysisManagerMachineFunctionProxy>(IR&: MF)
204 .getCachedResult<LibcallLoweringModuleAnalysis>(
205 IR&: *MF.getFunction().getParent()),
206 Subtarget);
207 return peephole(MF, LibInfo, LibcallLowering)
208 ? getMachineFunctionPassPreservedAnalyses()
209 .preserveSet<CFGAnalyses>()
210 : PreservedAnalyses::all();
211}
212