blob: 19c227d2f94257cda5668e25d5a4ceeb4549ca4f [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
15#include "WebAssembly.h"
16#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
17#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 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
26namespace {
27class WebAssemblyPeephole final : public MachineFunctionPass {
28 const char *getPassName() const override {
29 return "WebAssembly late peephole optimizer";
30 }
31
Dan Gohmanacc09412015-12-10 14:12:04 +000032 void getAnalysisUsage(AnalysisUsage &AU) const override {
33 AU.setPreservesCFG();
Dan Gohmanbdf08d52016-01-26 04:01:11 +000034 AU.addRequired<TargetLibraryInfoWrapperPass>();
Dan Gohmanacc09412015-12-10 14:12:04 +000035 MachineFunctionPass::getAnalysisUsage(AU);
36 }
37
Dan Gohman81719f82015-11-25 16:55:01 +000038 bool runOnMachineFunction(MachineFunction &MF) override;
39
40public:
41 static char ID;
42 WebAssemblyPeephole() : MachineFunctionPass(ID) {}
43};
44} // end anonymous namespace
45
46char WebAssemblyPeephole::ID = 0;
47FunctionPass *llvm::createWebAssemblyPeephole() {
48 return new WebAssemblyPeephole();
49}
50
Dan Gohman71008092016-05-17 23:19:03 +000051/// If desirable, rewrite NewReg to a drop register.
52static bool MaybeRewriteToDrop(unsigned OldReg, unsigned NewReg,
53 MachineOperand &MO,
54 WebAssemblyFunctionInfo &MFI,
55 MachineRegisterInfo &MRI) {
Dan Gohman81719f82015-11-25 16:55:01 +000056 bool Changed = false;
Dan Gohman27a11eef2016-02-21 03:27:22 +000057 if (OldReg == NewReg) {
Dan Gohmanbdf08d52016-01-26 04:01:11 +000058 Changed = true;
Dan Gohman847afa22016-05-19 21:07:20 +000059 unsigned NewReg = MRI.createVirtualRegister(MRI.getRegClass(OldReg));
Dan Gohmanbdf08d52016-01-26 04:01:11 +000060 MO.setReg(NewReg);
61 MO.setIsDead();
62 MFI.stackifyVReg(NewReg);
63 MFI.addWAReg(NewReg, WebAssemblyFunctionInfo::UnusedReg);
64 }
65 return Changed;
66}
67
68bool WebAssemblyPeephole::runOnMachineFunction(MachineFunction &MF) {
69 DEBUG({
70 dbgs() << "********** Store Results **********\n"
71 << "********** Function: " << MF.getName() << '\n';
72 });
Dan Gohman81719f82015-11-25 16:55:01 +000073
74 MachineRegisterInfo &MRI = MF.getRegInfo();
75 WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
Dan Gohmanbdf08d52016-01-26 04:01:11 +000076 const WebAssemblyTargetLowering &TLI =
77 *MF.getSubtarget<WebAssemblySubtarget>().getTargetLowering();
78 auto &LibInfo = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
79 bool Changed = false;
Dan Gohman81719f82015-11-25 16:55:01 +000080
81 for (auto &MBB : MF)
82 for (auto &MI : MBB)
83 switch (MI.getOpcode()) {
84 default:
85 break;
86 case WebAssembly::STORE8_I32:
87 case WebAssembly::STORE16_I32:
88 case WebAssembly::STORE8_I64:
89 case WebAssembly::STORE16_I64:
90 case WebAssembly::STORE32_I64:
91 case WebAssembly::STORE_F32:
92 case WebAssembly::STORE_F64:
93 case WebAssembly::STORE_I32:
94 case WebAssembly::STORE_I64: {
95 // Store instructions return their value operand. If we ended up using
96 // the same register for both, replace it with a dead def so that it
Dan Gohman71008092016-05-17 23:19:03 +000097 // can use $drop instead.
Dan Gohman81719f82015-11-25 16:55:01 +000098 MachineOperand &MO = MI.getOperand(0);
99 unsigned OldReg = MO.getReg();
Dan Gohmanbdf08d52016-01-26 04:01:11 +0000100 unsigned NewReg =
101 MI.getOperand(WebAssembly::StoreValueOperandNo).getReg();
Dan Gohman71008092016-05-17 23:19:03 +0000102 Changed |= MaybeRewriteToDrop(OldReg, NewReg, MO, MFI, MRI);
Dan Gohmanbdf08d52016-01-26 04:01:11 +0000103 break;
104 }
105 case WebAssembly::CALL_I32:
106 case WebAssembly::CALL_I64: {
107 MachineOperand &Op1 = MI.getOperand(1);
108 if (Op1.isSymbol()) {
109 StringRef Name(Op1.getSymbolName());
110 if (Name == TLI.getLibcallName(RTLIB::MEMCPY) ||
111 Name == TLI.getLibcallName(RTLIB::MEMMOVE) ||
112 Name == TLI.getLibcallName(RTLIB::MEMSET)) {
113 LibFunc::Func Func;
114 if (LibInfo.getLibFunc(Name, Func)) {
JF Bastien1a6c7602016-01-26 20:22:42 +0000115 const auto &Op2 = MI.getOperand(2);
Derek Schuffe7305cc2016-01-26 21:08:27 +0000116 if (!Op2.isReg())
JF Bastien1a6c7602016-01-26 20:22:42 +0000117 report_fatal_error("Peephole: call to builtin function with "
Derek Schuffe7305cc2016-01-26 21:08:27 +0000118 "wrong signature, not consuming reg");
119 MachineOperand &MO = MI.getOperand(0);
120 unsigned OldReg = MO.getReg();
121 unsigned NewReg = Op2.getReg();
Derek Schuff90d9e8d2016-01-26 22:47:43 +0000122
Dan Gohman847afa22016-05-19 21:07:20 +0000123 if (MRI.getRegClass(NewReg) != MRI.getRegClass(OldReg))
Derek Schuffe7305cc2016-01-26 21:08:27 +0000124 report_fatal_error("Peephole: call to builtin function with "
125 "wrong signature, from/to mismatch");
Dan Gohman71008092016-05-17 23:19:03 +0000126 Changed |= MaybeRewriteToDrop(OldReg, NewReg, MO, MFI, MRI);
Dan Gohmanbdf08d52016-01-26 04:01:11 +0000127 }
128 }
Dan Gohman81719f82015-11-25 16:55:01 +0000129 }
130 }
131 }
132
133 return Changed;
134}