blob: f79fe046b19991e32d9d6ac46b9f3c5195420053 [file] [log] [blame]
Dan Gohman81719f82015-11-25 16:55:01 +00001//===-- WebAssemblyPeephole.cpp - WebAssembly Peephole Optimiztions -------===//
2//
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
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000010/// Late peephole optimizations for WebAssembly.
Dan Gohman81719f82015-11-25 16:55:01 +000011///
12//===----------------------------------------------------------------------===//
13
Dan Gohman81719f82015-11-25 16:55:01 +000014#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
Dan Gohmanb7c24002016-05-21 00:21:56 +000015#include "WebAssembly.h"
Dan Gohman81719f82015-11-25 16:55:01 +000016#include "WebAssemblyMachineFunctionInfo.h"
Dan Gohmanbdf08d52016-01-26 04:01:11 +000017#include "WebAssemblySubtarget.h"
18#include "llvm/Analysis/TargetLibraryInfo.h"
Dan Gohman81719f82015-11-25 16:55:01 +000019#include "llvm/CodeGen/MachineFunctionPass.h"
Dan Gohmanb7c24002016-05-21 00:21:56 +000020#include "llvm/CodeGen/MachineInstrBuilder.h"
Dan Gohmanbdf08d52016-01-26 04:01:11 +000021#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman81719f82015-11-25 16:55:01 +000022using namespace llvm;
23
24#define DEBUG_TYPE "wasm-peephole"
25
Dan Gohmanb7c24002016-05-21 00:21:56 +000026static cl::opt<bool> DisableWebAssemblyFallthroughReturnOpt(
27 "disable-wasm-fallthrough-return-opt", cl::Hidden,
28 cl::desc("WebAssembly: Disable fallthrough-return optimizations."),
29 cl::init(false));
30
Dan Gohman81719f82015-11-25 16:55:01 +000031namespace {
32class WebAssemblyPeephole final : public MachineFunctionPass {
Mehdi Amini117296c2016-10-01 02:56:57 +000033 StringRef getPassName() const override {
Dan Gohman81719f82015-11-25 16:55:01 +000034 return "WebAssembly late peephole optimizer";
35 }
36
Dan Gohmanacc09412015-12-10 14:12:04 +000037 void getAnalysisUsage(AnalysisUsage &AU) const override {
38 AU.setPreservesCFG();
Dan Gohmanbdf08d52016-01-26 04:01:11 +000039 AU.addRequired<TargetLibraryInfoWrapperPass>();
Dan Gohmanacc09412015-12-10 14:12:04 +000040 MachineFunctionPass::getAnalysisUsage(AU);
41 }
42
Dan Gohman81719f82015-11-25 16:55:01 +000043 bool runOnMachineFunction(MachineFunction &MF) override;
44
45public:
46 static char ID;
47 WebAssemblyPeephole() : MachineFunctionPass(ID) {}
48};
49} // end anonymous namespace
50
51char WebAssemblyPeephole::ID = 0;
Jacob Gravelle40926452018-03-30 20:36:58 +000052INITIALIZE_PASS(WebAssemblyPeephole, DEBUG_TYPE,
53 "WebAssembly peephole optimizations", false, false)
54
Dan Gohman81719f82015-11-25 16:55:01 +000055FunctionPass *llvm::createWebAssemblyPeephole() {
56 return new WebAssemblyPeephole();
57}
58
Dan Gohman71008092016-05-17 23:19:03 +000059/// If desirable, rewrite NewReg to a drop register.
60static bool MaybeRewriteToDrop(unsigned OldReg, unsigned NewReg,
Dan Gohmanb7c24002016-05-21 00:21:56 +000061 MachineOperand &MO, WebAssemblyFunctionInfo &MFI,
Dan Gohman71008092016-05-17 23:19:03 +000062 MachineRegisterInfo &MRI) {
Dan Gohman81719f82015-11-25 16:55:01 +000063 bool Changed = false;
Dan Gohman27a11eef2016-02-21 03:27:22 +000064 if (OldReg == NewReg) {
Dan Gohmanbdf08d52016-01-26 04:01:11 +000065 Changed = true;
Dan Gohman847afa22016-05-19 21:07:20 +000066 unsigned NewReg = MRI.createVirtualRegister(MRI.getRegClass(OldReg));
Dan Gohmanbdf08d52016-01-26 04:01:11 +000067 MO.setReg(NewReg);
68 MO.setIsDead();
69 MFI.stackifyVReg(NewReg);
Dan Gohmanbdf08d52016-01-26 04:01:11 +000070 }
71 return Changed;
72}
73
Dan Gohmanb7c24002016-05-21 00:21:56 +000074static bool MaybeRewriteToFallthrough(MachineInstr &MI, MachineBasicBlock &MBB,
75 const MachineFunction &MF,
76 WebAssemblyFunctionInfo &MFI,
77 MachineRegisterInfo &MRI,
78 const WebAssemblyInstrInfo &TII,
79 unsigned FallthroughOpc,
80 unsigned CopyLocalOpc) {
81 if (DisableWebAssemblyFallthroughReturnOpt)
82 return false;
83 if (&MBB != &MF.back())
84 return false;
Sam Cleggcf2a9e22018-07-16 23:09:29 +000085
86 MachineBasicBlock::iterator End = MBB.end();
87 --End;
88 assert(End->getOpcode() == WebAssembly::END_FUNCTION);
89 --End;
90 if (&MI != &*End)
91 return false;
Dan Gohmanb7c24002016-05-21 00:21:56 +000092
Dan Gohmanb6afd202017-02-09 23:19:03 +000093 if (FallthroughOpc != WebAssembly::FALLTHROUGH_RETURN_VOID) {
94 // If the operand isn't stackified, insert a COPY to read the operand and
95 // stackify it.
96 MachineOperand &MO = MI.getOperand(0);
97 unsigned Reg = MO.getReg();
98 if (!MFI.isVRegStackified(Reg)) {
99 unsigned NewReg = MRI.createVirtualRegister(MRI.getRegClass(Reg));
100 BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(CopyLocalOpc), NewReg)
101 .addReg(Reg);
102 MO.setReg(NewReg);
103 MFI.stackifyVReg(NewReg);
104 }
Dan Gohmanb7c24002016-05-21 00:21:56 +0000105 }
106
107 // Rewrite the return.
108 MI.setDesc(TII.get(FallthroughOpc));
109 return true;
110}
111
Dan Gohmanbdf08d52016-01-26 04:01:11 +0000112bool WebAssemblyPeephole::runOnMachineFunction(MachineFunction &MF) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000113 LLVM_DEBUG({
Dan Gohmanb7c24002016-05-21 00:21:56 +0000114 dbgs() << "********** Peephole **********\n"
Dan Gohmanbdf08d52016-01-26 04:01:11 +0000115 << "********** Function: " << MF.getName() << '\n';
116 });
Dan Gohman81719f82015-11-25 16:55:01 +0000117
118 MachineRegisterInfo &MRI = MF.getRegInfo();
119 WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
Derek Schuff5629ec12016-08-02 23:31:56 +0000120 const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
Dan Gohmanbdf08d52016-01-26 04:01:11 +0000121 const WebAssemblyTargetLowering &TLI =
122 *MF.getSubtarget<WebAssemblySubtarget>().getTargetLowering();
123 auto &LibInfo = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
124 bool Changed = false;
Dan Gohman81719f82015-11-25 16:55:01 +0000125
126 for (auto &MBB : MF)
127 for (auto &MI : MBB)
128 switch (MI.getOpcode()) {
129 default:
130 break;
Dan Gohmanbdf08d52016-01-26 04:01:11 +0000131 case WebAssembly::CALL_I32:
132 case WebAssembly::CALL_I64: {
133 MachineOperand &Op1 = MI.getOperand(1);
134 if (Op1.isSymbol()) {
135 StringRef Name(Op1.getSymbolName());
136 if (Name == TLI.getLibcallName(RTLIB::MEMCPY) ||
137 Name == TLI.getLibcallName(RTLIB::MEMMOVE) ||
138 Name == TLI.getLibcallName(RTLIB::MEMSET)) {
Derek Schuff4b320b72017-01-24 00:01:18 +0000139 LibFunc Func;
Dan Gohmanbdf08d52016-01-26 04:01:11 +0000140 if (LibInfo.getLibFunc(Name, Func)) {
JF Bastien1a6c7602016-01-26 20:22:42 +0000141 const auto &Op2 = MI.getOperand(2);
Derek Schuffe7305cc2016-01-26 21:08:27 +0000142 if (!Op2.isReg())
JF Bastien1a6c7602016-01-26 20:22:42 +0000143 report_fatal_error("Peephole: call to builtin function with "
Derek Schuffe7305cc2016-01-26 21:08:27 +0000144 "wrong signature, not consuming reg");
145 MachineOperand &MO = MI.getOperand(0);
146 unsigned OldReg = MO.getReg();
147 unsigned NewReg = Op2.getReg();
Derek Schuff90d9e8d2016-01-26 22:47:43 +0000148
Dan Gohman847afa22016-05-19 21:07:20 +0000149 if (MRI.getRegClass(NewReg) != MRI.getRegClass(OldReg))
Derek Schuffe7305cc2016-01-26 21:08:27 +0000150 report_fatal_error("Peephole: call to builtin function with "
151 "wrong signature, from/to mismatch");
Dan Gohman71008092016-05-17 23:19:03 +0000152 Changed |= MaybeRewriteToDrop(OldReg, NewReg, MO, MFI, MRI);
Dan Gohmanbdf08d52016-01-26 04:01:11 +0000153 }
154 }
Dan Gohman81719f82015-11-25 16:55:01 +0000155 }
Dan Gohmanb7c24002016-05-21 00:21:56 +0000156 break;
Dan Gohman81719f82015-11-25 16:55:01 +0000157 }
Dan Gohmanb7c24002016-05-21 00:21:56 +0000158 // Optimize away an explicit void return at the end of the function.
159 case WebAssembly::RETURN_I32:
160 Changed |= MaybeRewriteToFallthrough(
161 MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_I32,
Dan Gohman4fc4e422016-10-24 19:49:43 +0000162 WebAssembly::COPY_I32);
Dan Gohmanb7c24002016-05-21 00:21:56 +0000163 break;
164 case WebAssembly::RETURN_I64:
165 Changed |= MaybeRewriteToFallthrough(
166 MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_I64,
Dan Gohman4fc4e422016-10-24 19:49:43 +0000167 WebAssembly::COPY_I64);
Dan Gohmanb7c24002016-05-21 00:21:56 +0000168 break;
169 case WebAssembly::RETURN_F32:
170 Changed |= MaybeRewriteToFallthrough(
171 MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_F32,
Dan Gohman4fc4e422016-10-24 19:49:43 +0000172 WebAssembly::COPY_F32);
Dan Gohmanb7c24002016-05-21 00:21:56 +0000173 break;
174 case WebAssembly::RETURN_F64:
175 Changed |= MaybeRewriteToFallthrough(
176 MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_F64,
Dan Gohman4fc4e422016-10-24 19:49:43 +0000177 WebAssembly::COPY_F64);
Dan Gohmanb7c24002016-05-21 00:21:56 +0000178 break;
Derek Schuff39bf39f2016-08-02 23:16:09 +0000179 case WebAssembly::RETURN_v16i8:
Derek Schuff5629ec12016-08-02 23:31:56 +0000180 Changed |= MaybeRewriteToFallthrough(
181 MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_v16i8,
Dan Gohman4fc4e422016-10-24 19:49:43 +0000182 WebAssembly::COPY_V128);
Derek Schuff39bf39f2016-08-02 23:16:09 +0000183 break;
184 case WebAssembly::RETURN_v8i16:
Derek Schuff5629ec12016-08-02 23:31:56 +0000185 Changed |= MaybeRewriteToFallthrough(
186 MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_v8i16,
Dan Gohman4fc4e422016-10-24 19:49:43 +0000187 WebAssembly::COPY_V128);
Derek Schuff39bf39f2016-08-02 23:16:09 +0000188 break;
189 case WebAssembly::RETURN_v4i32:
Derek Schuff5629ec12016-08-02 23:31:56 +0000190 Changed |= MaybeRewriteToFallthrough(
191 MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_v4i32,
Dan Gohman4fc4e422016-10-24 19:49:43 +0000192 WebAssembly::COPY_V128);
Derek Schuff39bf39f2016-08-02 23:16:09 +0000193 break;
Derek Schuff51ed1312018-08-07 21:24:01 +0000194 case WebAssembly::RETURN_v2i64:
195 Changed |= MaybeRewriteToFallthrough(
196 MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_v2i64,
197 WebAssembly::COPY_V128);
198 break;
Derek Schuff39bf39f2016-08-02 23:16:09 +0000199 case WebAssembly::RETURN_v4f32:
Derek Schuff5629ec12016-08-02 23:31:56 +0000200 Changed |= MaybeRewriteToFallthrough(
201 MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_v4f32,
Dan Gohman4fc4e422016-10-24 19:49:43 +0000202 WebAssembly::COPY_V128);
Derek Schuff39bf39f2016-08-02 23:16:09 +0000203 break;
Derek Schuff51ed1312018-08-07 21:24:01 +0000204 case WebAssembly::RETURN_v2f64:
205 Changed |= MaybeRewriteToFallthrough(
206 MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_v2f64,
207 WebAssembly::COPY_V128);
208 break;
Dan Gohmanb7c24002016-05-21 00:21:56 +0000209 case WebAssembly::RETURN_VOID:
Dan Gohmanb6afd202017-02-09 23:19:03 +0000210 Changed |= MaybeRewriteToFallthrough(
211 MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_VOID,
212 WebAssembly::INSTRUCTION_LIST_END);
Dan Gohmanb7c24002016-05-21 00:21:56 +0000213 break;
Dan Gohman81719f82015-11-25 16:55:01 +0000214 }
215
216 return Changed;
217}