blob: 0120431c639b0fb9f2c981465dc1cfb5458c4305 [file] [log] [blame]
Dan Gohman81719f82015-11-25 16:55:01 +00001//===-- WebAssemblyPeephole.cpp - WebAssembly Peephole Optimiztions -------===//
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 Late peephole optimizations for WebAssembly.
12///
13//===----------------------------------------------------------------------===//
14
Dan Gohman81719f82015-11-25 16:55:01 +000015#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
Dan Gohmanb7c24002016-05-21 00:21:56 +000016#include "WebAssembly.h"
Dan Gohman81719f82015-11-25 16:55:01 +000017#include "WebAssemblyMachineFunctionInfo.h"
Dan Gohmanbdf08d52016-01-26 04:01:11 +000018#include "WebAssemblySubtarget.h"
19#include "llvm/Analysis/TargetLibraryInfo.h"
Dan Gohman81719f82015-11-25 16:55:01 +000020#include "llvm/CodeGen/MachineFunctionPass.h"
Dan Gohmanb7c24002016-05-21 00:21:56 +000021#include "llvm/CodeGen/MachineInstrBuilder.h"
Dan Gohmanbdf08d52016-01-26 04:01:11 +000022#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman81719f82015-11-25 16:55:01 +000023using namespace llvm;
24
25#define DEBUG_TYPE "wasm-peephole"
26
Dan Gohmanb7c24002016-05-21 00:21:56 +000027static cl::opt<bool> DisableWebAssemblyFallthroughReturnOpt(
28 "disable-wasm-fallthrough-return-opt", cl::Hidden,
29 cl::desc("WebAssembly: Disable fallthrough-return optimizations."),
30 cl::init(false));
31
Dan Gohman81719f82015-11-25 16:55:01 +000032namespace {
33class WebAssemblyPeephole final : public MachineFunctionPass {
Mehdi Amini117296c2016-10-01 02:56:57 +000034 StringRef getPassName() const override {
Dan Gohman81719f82015-11-25 16:55:01 +000035 return "WebAssembly late peephole optimizer";
36 }
37
Dan Gohmanacc09412015-12-10 14:12:04 +000038 void getAnalysisUsage(AnalysisUsage &AU) const override {
39 AU.setPreservesCFG();
Dan Gohmanbdf08d52016-01-26 04:01:11 +000040 AU.addRequired<TargetLibraryInfoWrapperPass>();
Dan Gohmanacc09412015-12-10 14:12:04 +000041 MachineFunctionPass::getAnalysisUsage(AU);
42 }
43
Dan Gohman81719f82015-11-25 16:55:01 +000044 bool runOnMachineFunction(MachineFunction &MF) override;
45
46public:
47 static char ID;
48 WebAssemblyPeephole() : MachineFunctionPass(ID) {}
49};
50} // end anonymous namespace
51
52char WebAssemblyPeephole::ID = 0;
53FunctionPass *llvm::createWebAssemblyPeephole() {
54 return new WebAssemblyPeephole();
55}
56
Dan Gohman71008092016-05-17 23:19:03 +000057/// If desirable, rewrite NewReg to a drop register.
58static bool MaybeRewriteToDrop(unsigned OldReg, unsigned NewReg,
Dan Gohmanb7c24002016-05-21 00:21:56 +000059 MachineOperand &MO, WebAssemblyFunctionInfo &MFI,
Dan Gohman71008092016-05-17 23:19:03 +000060 MachineRegisterInfo &MRI) {
Dan Gohman81719f82015-11-25 16:55:01 +000061 bool Changed = false;
Dan Gohman27a11eef2016-02-21 03:27:22 +000062 if (OldReg == NewReg) {
Dan Gohmanbdf08d52016-01-26 04:01:11 +000063 Changed = true;
Dan Gohman847afa22016-05-19 21:07:20 +000064 unsigned NewReg = MRI.createVirtualRegister(MRI.getRegClass(OldReg));
Dan Gohmanbdf08d52016-01-26 04:01:11 +000065 MO.setReg(NewReg);
66 MO.setIsDead();
67 MFI.stackifyVReg(NewReg);
Dan Gohmanbdf08d52016-01-26 04:01:11 +000068 }
69 return Changed;
70}
71
Dan Gohmanb7c24002016-05-21 00:21:56 +000072static bool MaybeRewriteToFallthrough(MachineInstr &MI, MachineBasicBlock &MBB,
73 const MachineFunction &MF,
74 WebAssemblyFunctionInfo &MFI,
75 MachineRegisterInfo &MRI,
76 const WebAssemblyInstrInfo &TII,
77 unsigned FallthroughOpc,
78 unsigned CopyLocalOpc) {
79 if (DisableWebAssemblyFallthroughReturnOpt)
80 return false;
81 if (&MBB != &MF.back())
82 return false;
83 if (&MI != &MBB.back())
84 return false;
85
86 // If the operand isn't stackified, insert a COPY_LOCAL to read the operand
87 // and stackify it.
88 MachineOperand &MO = MI.getOperand(0);
89 unsigned Reg = MO.getReg();
90 if (!MFI.isVRegStackified(Reg)) {
91 unsigned NewReg = MRI.createVirtualRegister(MRI.getRegClass(Reg));
92 BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(CopyLocalOpc), NewReg)
93 .addReg(Reg);
94 MO.setReg(NewReg);
95 MFI.stackifyVReg(NewReg);
96 }
97
98 // Rewrite the return.
99 MI.setDesc(TII.get(FallthroughOpc));
100 return true;
101}
102
Dan Gohmanbdf08d52016-01-26 04:01:11 +0000103bool WebAssemblyPeephole::runOnMachineFunction(MachineFunction &MF) {
104 DEBUG({
Dan Gohmanb7c24002016-05-21 00:21:56 +0000105 dbgs() << "********** Peephole **********\n"
Dan Gohmanbdf08d52016-01-26 04:01:11 +0000106 << "********** Function: " << MF.getName() << '\n';
107 });
Dan Gohman81719f82015-11-25 16:55:01 +0000108
109 MachineRegisterInfo &MRI = MF.getRegInfo();
110 WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
Derek Schuff5629ec12016-08-02 23:31:56 +0000111 const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
Dan Gohmanbdf08d52016-01-26 04:01:11 +0000112 const WebAssemblyTargetLowering &TLI =
113 *MF.getSubtarget<WebAssemblySubtarget>().getTargetLowering();
114 auto &LibInfo = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
115 bool Changed = false;
Dan Gohman81719f82015-11-25 16:55:01 +0000116
117 for (auto &MBB : MF)
118 for (auto &MI : MBB)
119 switch (MI.getOpcode()) {
120 default:
121 break;
122 case WebAssembly::STORE8_I32:
123 case WebAssembly::STORE16_I32:
124 case WebAssembly::STORE8_I64:
125 case WebAssembly::STORE16_I64:
126 case WebAssembly::STORE32_I64:
127 case WebAssembly::STORE_F32:
128 case WebAssembly::STORE_F64:
129 case WebAssembly::STORE_I32:
130 case WebAssembly::STORE_I64: {
131 // Store instructions return their value operand. If we ended up using
132 // the same register for both, replace it with a dead def so that it
Dan Gohman71008092016-05-17 23:19:03 +0000133 // can use $drop instead.
Dan Gohman81719f82015-11-25 16:55:01 +0000134 MachineOperand &MO = MI.getOperand(0);
135 unsigned OldReg = MO.getReg();
Dan Gohmanbdf08d52016-01-26 04:01:11 +0000136 unsigned NewReg =
137 MI.getOperand(WebAssembly::StoreValueOperandNo).getReg();
Dan Gohman71008092016-05-17 23:19:03 +0000138 Changed |= MaybeRewriteToDrop(OldReg, NewReg, MO, MFI, MRI);
Dan Gohmanbdf08d52016-01-26 04:01:11 +0000139 break;
140 }
141 case WebAssembly::CALL_I32:
142 case WebAssembly::CALL_I64: {
143 MachineOperand &Op1 = MI.getOperand(1);
144 if (Op1.isSymbol()) {
145 StringRef Name(Op1.getSymbolName());
146 if (Name == TLI.getLibcallName(RTLIB::MEMCPY) ||
147 Name == TLI.getLibcallName(RTLIB::MEMMOVE) ||
148 Name == TLI.getLibcallName(RTLIB::MEMSET)) {
149 LibFunc::Func Func;
150 if (LibInfo.getLibFunc(Name, Func)) {
JF Bastien1a6c7602016-01-26 20:22:42 +0000151 const auto &Op2 = MI.getOperand(2);
Derek Schuffe7305cc2016-01-26 21:08:27 +0000152 if (!Op2.isReg())
JF Bastien1a6c7602016-01-26 20:22:42 +0000153 report_fatal_error("Peephole: call to builtin function with "
Derek Schuffe7305cc2016-01-26 21:08:27 +0000154 "wrong signature, not consuming reg");
155 MachineOperand &MO = MI.getOperand(0);
156 unsigned OldReg = MO.getReg();
157 unsigned NewReg = Op2.getReg();
Derek Schuff90d9e8d2016-01-26 22:47:43 +0000158
Dan Gohman847afa22016-05-19 21:07:20 +0000159 if (MRI.getRegClass(NewReg) != MRI.getRegClass(OldReg))
Derek Schuffe7305cc2016-01-26 21:08:27 +0000160 report_fatal_error("Peephole: call to builtin function with "
161 "wrong signature, from/to mismatch");
Dan Gohman71008092016-05-17 23:19:03 +0000162 Changed |= MaybeRewriteToDrop(OldReg, NewReg, MO, MFI, MRI);
Dan Gohmanbdf08d52016-01-26 04:01:11 +0000163 }
164 }
Dan Gohman81719f82015-11-25 16:55:01 +0000165 }
Dan Gohmanb7c24002016-05-21 00:21:56 +0000166 break;
Dan Gohman81719f82015-11-25 16:55:01 +0000167 }
Dan Gohmanb7c24002016-05-21 00:21:56 +0000168 // Optimize away an explicit void return at the end of the function.
169 case WebAssembly::RETURN_I32:
170 Changed |= MaybeRewriteToFallthrough(
171 MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_I32,
172 WebAssembly::COPY_LOCAL_I32);
173 break;
174 case WebAssembly::RETURN_I64:
175 Changed |= MaybeRewriteToFallthrough(
176 MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_I64,
177 WebAssembly::COPY_LOCAL_I64);
178 break;
179 case WebAssembly::RETURN_F32:
180 Changed |= MaybeRewriteToFallthrough(
181 MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_F32,
182 WebAssembly::COPY_LOCAL_F32);
183 break;
184 case WebAssembly::RETURN_F64:
185 Changed |= MaybeRewriteToFallthrough(
186 MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_F64,
187 WebAssembly::COPY_LOCAL_F64);
188 break;
Derek Schuff39bf39f2016-08-02 23:16:09 +0000189 case WebAssembly::RETURN_v16i8:
Derek Schuff5629ec12016-08-02 23:31:56 +0000190 Changed |= MaybeRewriteToFallthrough(
191 MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_v16i8,
192 WebAssembly::COPY_LOCAL_V128);
Derek Schuff39bf39f2016-08-02 23:16:09 +0000193 break;
194 case WebAssembly::RETURN_v8i16:
Derek Schuff5629ec12016-08-02 23:31:56 +0000195 Changed |= MaybeRewriteToFallthrough(
196 MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_v8i16,
197 WebAssembly::COPY_LOCAL_V128);
Derek Schuff39bf39f2016-08-02 23:16:09 +0000198 break;
199 case WebAssembly::RETURN_v4i32:
Derek Schuff5629ec12016-08-02 23:31:56 +0000200 Changed |= MaybeRewriteToFallthrough(
201 MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_v4i32,
202 WebAssembly::COPY_LOCAL_V128);
Derek Schuff39bf39f2016-08-02 23:16:09 +0000203 break;
204 case WebAssembly::RETURN_v4f32:
Derek Schuff5629ec12016-08-02 23:31:56 +0000205 Changed |= MaybeRewriteToFallthrough(
206 MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_v4f32,
207 WebAssembly::COPY_LOCAL_V128);
Derek Schuff39bf39f2016-08-02 23:16:09 +0000208 break;
Dan Gohmanb7c24002016-05-21 00:21:56 +0000209 case WebAssembly::RETURN_VOID:
210 if (!DisableWebAssemblyFallthroughReturnOpt &&
211 &MBB == &MF.back() && &MI == &MBB.back())
212 MI.setDesc(TII.get(WebAssembly::FALLTHROUGH_RETURN_VOID));
213 break;
Dan Gohman81719f82015-11-25 16:55:01 +0000214 }
215
216 return Changed;
217}