blob: 56487caf14fde50cde873aca8208ef0354b13f36 [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;
Jacob Gravelle40926452018-03-30 20:36:58 +000053INITIALIZE_PASS(WebAssemblyPeephole, DEBUG_TYPE,
54 "WebAssembly peephole optimizations", false, false)
55
Dan Gohman81719f82015-11-25 16:55:01 +000056FunctionPass *llvm::createWebAssemblyPeephole() {
57 return new WebAssemblyPeephole();
58}
59
Dan Gohman71008092016-05-17 23:19:03 +000060/// If desirable, rewrite NewReg to a drop register.
61static bool MaybeRewriteToDrop(unsigned OldReg, unsigned NewReg,
Dan Gohmanb7c24002016-05-21 00:21:56 +000062 MachineOperand &MO, WebAssemblyFunctionInfo &MFI,
Dan Gohman71008092016-05-17 23:19:03 +000063 MachineRegisterInfo &MRI) {
Dan Gohman81719f82015-11-25 16:55:01 +000064 bool Changed = false;
Dan Gohman27a11eef2016-02-21 03:27:22 +000065 if (OldReg == NewReg) {
Dan Gohmanbdf08d52016-01-26 04:01:11 +000066 Changed = true;
Dan Gohman847afa22016-05-19 21:07:20 +000067 unsigned NewReg = MRI.createVirtualRegister(MRI.getRegClass(OldReg));
Dan Gohmanbdf08d52016-01-26 04:01:11 +000068 MO.setReg(NewReg);
69 MO.setIsDead();
70 MFI.stackifyVReg(NewReg);
Dan Gohmanbdf08d52016-01-26 04:01:11 +000071 }
72 return Changed;
73}
74
Dan Gohmanb7c24002016-05-21 00:21:56 +000075static bool MaybeRewriteToFallthrough(MachineInstr &MI, MachineBasicBlock &MBB,
76 const MachineFunction &MF,
77 WebAssemblyFunctionInfo &MFI,
78 MachineRegisterInfo &MRI,
79 const WebAssemblyInstrInfo &TII,
80 unsigned FallthroughOpc,
81 unsigned CopyLocalOpc) {
82 if (DisableWebAssemblyFallthroughReturnOpt)
83 return false;
84 if (&MBB != &MF.back())
85 return false;
Dan Gohmand934cb82017-02-24 23:18:00 +000086 if (MF.getSubtarget<WebAssemblySubtarget>()
87 .getTargetTriple().isOSBinFormatELF()) {
88 if (&MI != &MBB.back())
89 return false;
90 } else {
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 }
Dan Gohmanb7c24002016-05-21 00:21:56 +000098
Dan Gohmanb6afd202017-02-09 23:19:03 +000099 if (FallthroughOpc != WebAssembly::FALLTHROUGH_RETURN_VOID) {
100 // If the operand isn't stackified, insert a COPY to read the operand and
101 // stackify it.
102 MachineOperand &MO = MI.getOperand(0);
103 unsigned Reg = MO.getReg();
104 if (!MFI.isVRegStackified(Reg)) {
105 unsigned NewReg = MRI.createVirtualRegister(MRI.getRegClass(Reg));
106 BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(CopyLocalOpc), NewReg)
107 .addReg(Reg);
108 MO.setReg(NewReg);
109 MFI.stackifyVReg(NewReg);
110 }
Dan Gohmanb7c24002016-05-21 00:21:56 +0000111 }
112
113 // Rewrite the return.
114 MI.setDesc(TII.get(FallthroughOpc));
115 return true;
116}
117
Dan Gohmanbdf08d52016-01-26 04:01:11 +0000118bool WebAssemblyPeephole::runOnMachineFunction(MachineFunction &MF) {
119 DEBUG({
Dan Gohmanb7c24002016-05-21 00:21:56 +0000120 dbgs() << "********** Peephole **********\n"
Dan Gohmanbdf08d52016-01-26 04:01:11 +0000121 << "********** Function: " << MF.getName() << '\n';
122 });
Dan Gohman81719f82015-11-25 16:55:01 +0000123
124 MachineRegisterInfo &MRI = MF.getRegInfo();
125 WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
Derek Schuff5629ec12016-08-02 23:31:56 +0000126 const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
Dan Gohmanbdf08d52016-01-26 04:01:11 +0000127 const WebAssemblyTargetLowering &TLI =
128 *MF.getSubtarget<WebAssemblySubtarget>().getTargetLowering();
129 auto &LibInfo = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
130 bool Changed = false;
Dan Gohman81719f82015-11-25 16:55:01 +0000131
132 for (auto &MBB : MF)
133 for (auto &MI : MBB)
134 switch (MI.getOpcode()) {
135 default:
136 break;
Dan Gohmanbdf08d52016-01-26 04:01:11 +0000137 case WebAssembly::CALL_I32:
138 case WebAssembly::CALL_I64: {
139 MachineOperand &Op1 = MI.getOperand(1);
140 if (Op1.isSymbol()) {
141 StringRef Name(Op1.getSymbolName());
142 if (Name == TLI.getLibcallName(RTLIB::MEMCPY) ||
143 Name == TLI.getLibcallName(RTLIB::MEMMOVE) ||
144 Name == TLI.getLibcallName(RTLIB::MEMSET)) {
Derek Schuff4b320b72017-01-24 00:01:18 +0000145 LibFunc Func;
Dan Gohmanbdf08d52016-01-26 04:01:11 +0000146 if (LibInfo.getLibFunc(Name, Func)) {
JF Bastien1a6c7602016-01-26 20:22:42 +0000147 const auto &Op2 = MI.getOperand(2);
Derek Schuffe7305cc2016-01-26 21:08:27 +0000148 if (!Op2.isReg())
JF Bastien1a6c7602016-01-26 20:22:42 +0000149 report_fatal_error("Peephole: call to builtin function with "
Derek Schuffe7305cc2016-01-26 21:08:27 +0000150 "wrong signature, not consuming reg");
151 MachineOperand &MO = MI.getOperand(0);
152 unsigned OldReg = MO.getReg();
153 unsigned NewReg = Op2.getReg();
Derek Schuff90d9e8d2016-01-26 22:47:43 +0000154
Dan Gohman847afa22016-05-19 21:07:20 +0000155 if (MRI.getRegClass(NewReg) != MRI.getRegClass(OldReg))
Derek Schuffe7305cc2016-01-26 21:08:27 +0000156 report_fatal_error("Peephole: call to builtin function with "
157 "wrong signature, from/to mismatch");
Dan Gohman71008092016-05-17 23:19:03 +0000158 Changed |= MaybeRewriteToDrop(OldReg, NewReg, MO, MFI, MRI);
Dan Gohmanbdf08d52016-01-26 04:01:11 +0000159 }
160 }
Dan Gohman81719f82015-11-25 16:55:01 +0000161 }
Dan Gohmanb7c24002016-05-21 00:21:56 +0000162 break;
Dan Gohman81719f82015-11-25 16:55:01 +0000163 }
Dan Gohmanb7c24002016-05-21 00:21:56 +0000164 // Optimize away an explicit void return at the end of the function.
165 case WebAssembly::RETURN_I32:
166 Changed |= MaybeRewriteToFallthrough(
167 MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_I32,
Dan Gohman4fc4e422016-10-24 19:49:43 +0000168 WebAssembly::COPY_I32);
Dan Gohmanb7c24002016-05-21 00:21:56 +0000169 break;
170 case WebAssembly::RETURN_I64:
171 Changed |= MaybeRewriteToFallthrough(
172 MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_I64,
Dan Gohman4fc4e422016-10-24 19:49:43 +0000173 WebAssembly::COPY_I64);
Dan Gohmanb7c24002016-05-21 00:21:56 +0000174 break;
175 case WebAssembly::RETURN_F32:
176 Changed |= MaybeRewriteToFallthrough(
177 MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_F32,
Dan Gohman4fc4e422016-10-24 19:49:43 +0000178 WebAssembly::COPY_F32);
Dan Gohmanb7c24002016-05-21 00:21:56 +0000179 break;
180 case WebAssembly::RETURN_F64:
181 Changed |= MaybeRewriteToFallthrough(
182 MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_F64,
Dan Gohman4fc4e422016-10-24 19:49:43 +0000183 WebAssembly::COPY_F64);
Dan Gohmanb7c24002016-05-21 00:21:56 +0000184 break;
Derek Schuff39bf39f2016-08-02 23:16:09 +0000185 case WebAssembly::RETURN_v16i8:
Derek Schuff5629ec12016-08-02 23:31:56 +0000186 Changed |= MaybeRewriteToFallthrough(
187 MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_v16i8,
Dan Gohman4fc4e422016-10-24 19:49:43 +0000188 WebAssembly::COPY_V128);
Derek Schuff39bf39f2016-08-02 23:16:09 +0000189 break;
190 case WebAssembly::RETURN_v8i16:
Derek Schuff5629ec12016-08-02 23:31:56 +0000191 Changed |= MaybeRewriteToFallthrough(
192 MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_v8i16,
Dan Gohman4fc4e422016-10-24 19:49:43 +0000193 WebAssembly::COPY_V128);
Derek Schuff39bf39f2016-08-02 23:16:09 +0000194 break;
195 case WebAssembly::RETURN_v4i32:
Derek Schuff5629ec12016-08-02 23:31:56 +0000196 Changed |= MaybeRewriteToFallthrough(
197 MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_v4i32,
Dan Gohman4fc4e422016-10-24 19:49:43 +0000198 WebAssembly::COPY_V128);
Derek Schuff39bf39f2016-08-02 23:16:09 +0000199 break;
200 case WebAssembly::RETURN_v4f32:
Derek Schuff5629ec12016-08-02 23:31:56 +0000201 Changed |= MaybeRewriteToFallthrough(
202 MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_v4f32,
Dan Gohman4fc4e422016-10-24 19:49:43 +0000203 WebAssembly::COPY_V128);
Derek Schuff39bf39f2016-08-02 23:16:09 +0000204 break;
Dan Gohmanb7c24002016-05-21 00:21:56 +0000205 case WebAssembly::RETURN_VOID:
Dan Gohmanb6afd202017-02-09 23:19:03 +0000206 Changed |= MaybeRewriteToFallthrough(
207 MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_VOID,
208 WebAssembly::INSTRUCTION_LIST_END);
Dan Gohmanb7c24002016-05-21 00:21:56 +0000209 break;
Dan Gohman81719f82015-11-25 16:55:01 +0000210 }
211
212 return Changed;
213}