LLVM 19.0.0git
WebAssemblyPeephole.cpp
Go to the documentation of this file.
1//===-- WebAssemblyPeephole.cpp - WebAssembly Peephole Optimiztions -------===//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// Late peephole optimizations for WebAssembly.
11///
12//===----------------------------------------------------------------------===//
13
15#include "WebAssembly.h"
23using namespace llvm;
24
25#define DEBUG_TYPE "wasm-peephole"
26
28 "disable-wasm-fallthrough-return-opt", cl::Hidden,
29 cl::desc("WebAssembly: Disable fallthrough-return optimizations."),
30 cl::init(false));
31
32namespace {
33class WebAssemblyPeephole final : public MachineFunctionPass {
34 StringRef getPassName() const override {
35 return "WebAssembly late peephole optimizer";
36 }
37
38 void getAnalysisUsage(AnalysisUsage &AU) const override {
39 AU.setPreservesCFG();
42 }
43
44 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;
53INITIALIZE_PASS(WebAssemblyPeephole, DEBUG_TYPE,
54 "WebAssembly peephole optimizations", false, false)
55
57 return new WebAssemblyPeephole();
58}
59
60/// If desirable, rewrite NewReg to a drop register.
61static bool maybeRewriteToDrop(unsigned OldReg, unsigned NewReg,
64 bool Changed = false;
65 if (OldReg == NewReg) {
66 Changed = true;
67 Register NewReg = MRI.createVirtualRegister(MRI.getRegClass(OldReg));
68 MO.setReg(NewReg);
69 MO.setIsDead();
70 MFI.stackifyVReg(MRI, NewReg);
71 }
72 return Changed;
73}
74
76 const MachineFunction &MF,
81 return false;
82 if (&MBB != &MF.back())
83 return false;
84
86 --End;
87 assert(End->getOpcode() == WebAssembly::END_FUNCTION);
88 --End;
89 if (&MI != &*End)
90 return false;
91
92 for (auto &MO : MI.explicit_operands()) {
93 // If the operand isn't stackified, insert a COPY to read the operands and
94 // stackify them.
95 Register Reg = MO.getReg();
96 if (!MFI.isVRegStackified(Reg)) {
97 unsigned CopyLocalOpc;
98 const TargetRegisterClass *RegClass = MRI.getRegClass(Reg);
99 CopyLocalOpc = WebAssembly::getCopyOpcodeForRegClass(RegClass);
100 Register NewReg = MRI.createVirtualRegister(RegClass);
101 BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(CopyLocalOpc), NewReg)
102 .addReg(Reg);
103 MO.setReg(NewReg);
104 MFI.stackifyVReg(MRI, NewReg);
105 }
106 }
107
108 MI.setDesc(TII.get(WebAssembly::FALLTHROUGH_RETURN));
109 return true;
110}
111
112bool WebAssemblyPeephole::runOnMachineFunction(MachineFunction &MF) {
113 LLVM_DEBUG({
114 dbgs() << "********** Peephole **********\n"
115 << "********** Function: " << MF.getName() << '\n';
116 });
117
120 const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
121 const WebAssemblyTargetLowering &TLI =
122 *MF.getSubtarget<WebAssemblySubtarget>().getTargetLowering();
123 auto &LibInfo =
124 getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(MF.getFunction());
125 bool Changed = false;
126
127 for (auto &MBB : MF)
128 for (auto &MI : MBB)
129 switch (MI.getOpcode()) {
130 default:
131 break;
132 case WebAssembly::CALL: {
133 MachineOperand &Op1 = MI.getOperand(1);
134 if (Op1.isSymbol()) {
136 if (Name == TLI.getLibcallName(RTLIB::MEMCPY) ||
137 Name == TLI.getLibcallName(RTLIB::MEMMOVE) ||
138 Name == TLI.getLibcallName(RTLIB::MEMSET)) {
140 if (LibInfo.getLibFunc(Name, Func)) {
141 const auto &Op2 = MI.getOperand(2);
142 if (!Op2.isReg())
143 report_fatal_error("Peephole: call to builtin function with "
144 "wrong signature, not consuming reg");
145 MachineOperand &MO = MI.getOperand(0);
146 Register OldReg = MO.getReg();
147 Register NewReg = Op2.getReg();
148
149 if (MRI.getRegClass(NewReg) != MRI.getRegClass(OldReg))
150 report_fatal_error("Peephole: call to builtin function with "
151 "wrong signature, from/to mismatch");
152 Changed |= maybeRewriteToDrop(OldReg, NewReg, MO, MFI, MRI);
153 }
154 }
155 }
156 break;
157 }
158 // Optimize away an explicit void return at the end of the function.
159 case WebAssembly::RETURN:
160 Changed |= maybeRewriteToFallthrough(MI, MBB, MF, MFI, MRI, TII);
161 break;
162 }
163
164 return Changed;
165}
unsigned const MachineRegisterInfo * MRI
MachineBasicBlock & MBB
#define LLVM_DEBUG(X)
Definition: Debug.h:101
std::string Name
bool End
Definition: ELF_riscv.cpp:480
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file provides WebAssembly-specific target descriptions.
This file declares WebAssembly-specific per-machine-function information.
static bool maybeRewriteToDrop(unsigned OldReg, unsigned NewReg, MachineOperand &MO, WebAssemblyFunctionInfo &MFI, MachineRegisterInfo &MRI)
If desirable, rewrite NewReg to a drop register.
static bool maybeRewriteToFallthrough(MachineInstr &MI, MachineBasicBlock &MBB, const MachineFunction &MF, WebAssemblyFunctionInfo &MFI, MachineRegisterInfo &MRI, const WebAssemblyInstrInfo &TII)
static cl::opt< bool > DisableWebAssemblyFallthroughReturnOpt("disable-wasm-fallthrough-return-opt", cl::Hidden, cl::desc("WebAssembly: Disable fallthrough-return optimizations."), cl::init(false))
#define DEBUG_TYPE
This file declares the WebAssembly-specific subclass of TargetSubtarget.
This file contains the declaration of the WebAssembly-specific utility functions.
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:269
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:311
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineBasicBlock & back() const
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
Representation of each machine instruction.
Definition: MachineInstr.h:69
MachineOperand class - Representation of each machine instruction operand.
void setIsDead(bool Val=true)
void setReg(Register Reg)
Change the register this operand corresponds to.
bool isSymbol() const
isSymbol - Tests if this is a MO_ExternalSymbol operand.
const char * getSymbolName() const
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
const char * getLibcallName(RTLIB::Libcall Call) const
Get the libcall routine name for the specified libcall.
This class is derived from MachineFunctionInfo and contains private WebAssembly-specific information ...
void stackifyVReg(MachineRegisterInfo &MRI, unsigned VReg)
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
unsigned getCopyOpcodeForRegClass(const TargetRegisterClass *RC)
Returns the appropriate copy opcode for the given register class.
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:450
NodeAddr< FuncNode * > Func
Definition: RDFGraph.h:393
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:156
FunctionPass * createWebAssemblyPeephole()