blob: ea6cd09a604c5820675d86af0d5a06c14d8312ec [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.
Heejin Ahn18c56a02019-02-04 19:13:39 +000060static 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;
Daniel Sanders05c145d2019-08-12 22:40:45 +000066 Register 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
Heejin Ahn18c56a02019-02-04 19:13:39 +000074static bool maybeRewriteToFallthrough(MachineInstr &MI, MachineBasicBlock &MBB,
Dan Gohmanb7c24002016-05-21 00:21:56 +000075 const MachineFunction &MF,
76 WebAssemblyFunctionInfo &MFI,
77 MachineRegisterInfo &MRI,
Thomas Lively00f9e5a2019-10-09 21:42:08 +000078 const WebAssemblyInstrInfo &TII) {
Dan Gohmanb7c24002016-05-21 00:21:56 +000079 if (DisableWebAssemblyFallthroughReturnOpt)
80 return false;
81 if (&MBB != &MF.back())
82 return false;
Sam Cleggcf2a9e22018-07-16 23:09:29 +000083
84 MachineBasicBlock::iterator End = MBB.end();
85 --End;
86 assert(End->getOpcode() == WebAssembly::END_FUNCTION);
87 --End;
88 if (&MI != &*End)
89 return false;
Dan Gohmanb7c24002016-05-21 00:21:56 +000090
Thomas Lively00f9e5a2019-10-09 21:42:08 +000091 for (auto &MO : MI.explicit_operands()) {
92 // If the operand isn't stackified, insert a COPY to read the operands and
93 // stackify them.
Daniel Sanders05c145d2019-08-12 22:40:45 +000094 Register Reg = MO.getReg();
Dan Gohmanb6afd202017-02-09 23:19:03 +000095 if (!MFI.isVRegStackified(Reg)) {
Thomas Lively00f9e5a2019-10-09 21:42:08 +000096 unsigned CopyLocalOpc;
97 const TargetRegisterClass *RegClass = MRI.getRegClass(Reg);
98 switch (RegClass->getID()) {
99 case WebAssembly::I32RegClassID:
100 CopyLocalOpc = WebAssembly::COPY_I32;
101 break;
102 case WebAssembly::I64RegClassID:
103 CopyLocalOpc = WebAssembly::COPY_I64;
104 break;
105 case WebAssembly::F32RegClassID:
106 CopyLocalOpc = WebAssembly::COPY_F32;
107 break;
108 case WebAssembly::F64RegClassID:
109 CopyLocalOpc = WebAssembly::COPY_F64;
110 break;
111 case WebAssembly::V128RegClassID:
112 CopyLocalOpc = WebAssembly::COPY_V128;
113 break;
114 case WebAssembly::EXNREFRegClassID:
115 CopyLocalOpc = WebAssembly::COPY_EXNREF;
116 break;
117 default:
118 llvm_unreachable("Unexpected register class for return operand");
119 }
120 Register NewReg = MRI.createVirtualRegister(RegClass);
Dan Gohmanb6afd202017-02-09 23:19:03 +0000121 BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(CopyLocalOpc), NewReg)
122 .addReg(Reg);
123 MO.setReg(NewReg);
124 MFI.stackifyVReg(NewReg);
125 }
Dan Gohmanb7c24002016-05-21 00:21:56 +0000126 }
127
Thomas Lively00f9e5a2019-10-09 21:42:08 +0000128 MI.setDesc(TII.get(WebAssembly::FALLTHROUGH_RETURN));
Dan Gohmanb7c24002016-05-21 00:21:56 +0000129 return true;
130}
131
Dan Gohmanbdf08d52016-01-26 04:01:11 +0000132bool WebAssemblyPeephole::runOnMachineFunction(MachineFunction &MF) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000133 LLVM_DEBUG({
Dan Gohmanb7c24002016-05-21 00:21:56 +0000134 dbgs() << "********** Peephole **********\n"
Dan Gohmanbdf08d52016-01-26 04:01:11 +0000135 << "********** Function: " << MF.getName() << '\n';
136 });
Dan Gohman81719f82015-11-25 16:55:01 +0000137
138 MachineRegisterInfo &MRI = MF.getRegInfo();
139 WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
Derek Schuff5629ec12016-08-02 23:31:56 +0000140 const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
Dan Gohmanbdf08d52016-01-26 04:01:11 +0000141 const WebAssemblyTargetLowering &TLI =
142 *MF.getSubtarget<WebAssemblySubtarget>().getTargetLowering();
Teresa Johnson9c27b592019-09-07 03:09:36 +0000143 auto &LibInfo =
144 getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(MF.getFunction());
Dan Gohmanbdf08d52016-01-26 04:01:11 +0000145 bool Changed = false;
Dan Gohman81719f82015-11-25 16:55:01 +0000146
147 for (auto &MBB : MF)
148 for (auto &MI : MBB)
149 switch (MI.getOpcode()) {
150 default:
151 break;
Thomas Livelya1d97a92019-06-26 16:17:15 +0000152 case WebAssembly::CALL_i32:
153 case WebAssembly::CALL_i64: {
Dan Gohmanbdf08d52016-01-26 04:01:11 +0000154 MachineOperand &Op1 = MI.getOperand(1);
155 if (Op1.isSymbol()) {
156 StringRef Name(Op1.getSymbolName());
157 if (Name == TLI.getLibcallName(RTLIB::MEMCPY) ||
158 Name == TLI.getLibcallName(RTLIB::MEMMOVE) ||
159 Name == TLI.getLibcallName(RTLIB::MEMSET)) {
Derek Schuff4b320b72017-01-24 00:01:18 +0000160 LibFunc Func;
Dan Gohmanbdf08d52016-01-26 04:01:11 +0000161 if (LibInfo.getLibFunc(Name, Func)) {
JF Bastien1a6c7602016-01-26 20:22:42 +0000162 const auto &Op2 = MI.getOperand(2);
Derek Schuffe7305cc2016-01-26 21:08:27 +0000163 if (!Op2.isReg())
JF Bastien1a6c7602016-01-26 20:22:42 +0000164 report_fatal_error("Peephole: call to builtin function with "
Derek Schuffe7305cc2016-01-26 21:08:27 +0000165 "wrong signature, not consuming reg");
166 MachineOperand &MO = MI.getOperand(0);
Daniel Sanders05c145d2019-08-12 22:40:45 +0000167 Register OldReg = MO.getReg();
168 Register NewReg = Op2.getReg();
Derek Schuff90d9e8d2016-01-26 22:47:43 +0000169
Dan Gohman847afa22016-05-19 21:07:20 +0000170 if (MRI.getRegClass(NewReg) != MRI.getRegClass(OldReg))
Derek Schuffe7305cc2016-01-26 21:08:27 +0000171 report_fatal_error("Peephole: call to builtin function with "
172 "wrong signature, from/to mismatch");
Heejin Ahn18c56a02019-02-04 19:13:39 +0000173 Changed |= maybeRewriteToDrop(OldReg, NewReg, MO, MFI, MRI);
Dan Gohmanbdf08d52016-01-26 04:01:11 +0000174 }
175 }
Dan Gohman81719f82015-11-25 16:55:01 +0000176 }
Dan Gohmanb7c24002016-05-21 00:21:56 +0000177 break;
Dan Gohman81719f82015-11-25 16:55:01 +0000178 }
Dan Gohmanb7c24002016-05-21 00:21:56 +0000179 // Optimize away an explicit void return at the end of the function.
Thomas Lively00f9e5a2019-10-09 21:42:08 +0000180 case WebAssembly::RETURN:
181 Changed |= maybeRewriteToFallthrough(MI, MBB, MF, MFI, MRI, TII);
Dan Gohmanb7c24002016-05-21 00:21:56 +0000182 break;
Dan Gohman81719f82015-11-25 16:55:01 +0000183 }
184
185 return Changed;
186}