LLVM  16.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 
16 #include "WebAssembly.h"
18 #include "WebAssemblySubtarget.h"
23 using 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 
32 namespace {
33 class 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 
46 public:
47  static char ID;
48  WebAssemblyPeephole() : MachineFunctionPass(ID) {}
49 };
50 } // end anonymous namespace
51 
53 INITIALIZE_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.
61 static bool maybeRewriteToDrop(unsigned OldReg, unsigned NewReg,
64  bool Changed = false;
65  if (OldReg == NewReg) {
66  Changed = true;
68  MO.setReg(NewReg);
69  MO.setIsDead();
70  MFI.stackifyVReg(MRI, NewReg);
71  }
72  return Changed;
73 }
74 
76  const MachineFunction &MF,
79  const WebAssemblyInstrInfo &TII) {
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 
112 bool 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)) {
139  LibFunc Func;
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 }
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:109
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
WebAssembly.h
llvm::AArch64PACKey::ID
ID
Definition: AArch64BaseInfo.h:818
llvm::MachineRegisterInfo::createVirtualRegister
Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
Definition: MachineRegisterInfo.cpp:156
DEBUG_TYPE
#define DEBUG_TYPE
Definition: WebAssemblyPeephole.cpp:25
llvm::MachineRegisterInfo
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Definition: MachineRegisterInfo.h:50
llvm::X86Disassembler::Reg
Reg
All possible values of the reg field in the ModR/M byte.
Definition: X86DisassemblerDecoder.h:462
llvm::MachineFunctionPass
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
Definition: MachineFunctionPass.h:30
llvm::MachineFunction::back
const MachineBasicBlock & back() const
Definition: MachineFunction.h:871
llvm::TargetLoweringBase::getLibcallName
const char * getLibcallName(RTLIB::Libcall Call) const
Get the libcall routine name for the specified libcall.
Definition: TargetLowering.h:3168
llvm::cl::Hidden
@ Hidden
Definition: CommandLine.h:139
llvm::MachineOperand::isSymbol
bool isSymbol() const
isSymbol - Tests if this is a MO_ExternalSymbol operand.
Definition: MachineOperand.h:340
llvm::NVPTXISD::RETURN
@ RETURN
Definition: NVPTXISelLowering.h:49
llvm::AArch64ISD::CALL
@ CALL
Definition: AArch64ISelLowering.h:53
llvm::ARMISD::MEMCPY
@ MEMCPY
Definition: ARMISelLowering.h:311
DisableWebAssemblyFallthroughReturnOpt
static cl::opt< bool > DisableWebAssemblyFallthroughReturnOpt("disable-wasm-fallthrough-return-opt", cl::Hidden, cl::desc("WebAssembly: Disable fallthrough-return optimizations."), cl::init(false))
llvm::MachineFunctionPass::getAnalysisUsage
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Definition: MachineFunctionPass.cpp:167
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:101
MachineRegisterInfo.h
llvm::WebAssemblyTargetLowering
Definition: WebAssemblyISelLowering.h:43
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
llvm::MachineFunction::getRegInfo
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Definition: MachineFunction.h:670
maybeRewriteToDrop
static bool maybeRewriteToDrop(unsigned OldReg, unsigned NewReg, MachineOperand &MO, WebAssemblyFunctionInfo &MFI, MachineRegisterInfo &MRI)
If desirable, rewrite NewReg to a drop register.
Definition: WebAssemblyPeephole.cpp:61
llvm::MachineFunction::getInfo
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
Definition: MachineFunction.h:758
llvm::LibFunc
LibFunc
Definition: TargetLibraryInfo.h:35
llvm::TargetRegisterClass
Definition: TargetRegisterInfo.h:45
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
TargetLibraryInfo.h
TII
const HexagonInstrInfo * TII
Definition: HexagonCopyToCombine.cpp:125
llvm::MachineOperand
MachineOperand class - Representation of each machine instruction operand.
Definition: MachineOperand.h:48
llvm::report_fatal_error
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:145
llvm::WebAssembly::getCopyOpcodeForRegClass
unsigned getCopyOpcodeForRegClass(const TargetRegisterClass *RC)
Returns the appropriate copy opcode for the given register class.
Definition: WebAssemblyUtilities.cpp:183
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:94
WebAssemblyUtilities.h
WebAssemblyMCTargetDesc.h
llvm::MachineRegisterInfo::getRegClass
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
Definition: MachineRegisterInfo.h:647
llvm::MachineFunction::getSubtarget
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Definition: MachineFunction.h:660
llvm::WebAssemblyFunctionInfo::stackifyVReg
void stackifyVReg(MachineRegisterInfo &MRI, unsigned VReg)
Definition: WebAssemblyMachineFunctionInfo.h:122
llvm::cl::opt< bool >
llvm::MachineOperand::setIsDead
void setIsDead(bool Val=true)
Definition: MachineOperand.h:515
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:66
llvm::TargetLibraryInfoWrapperPass
Definition: TargetLibraryInfo.h:474
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:446
MachineFunctionPass.h
llvm::WebAssemblyFunctionInfo::isVRegStackified
bool isVRegStackified(unsigned VReg) const
Definition: WebAssemblyMachineFunctionInfo.h:134
llvm::MachineFunction::getName
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
Definition: MachineFunction.cpp:575
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::WebAssemblyFunctionInfo
This class is derived from MachineFunctionInfo and contains private WebAssembly-specific information ...
Definition: WebAssemblyMachineFunctionInfo.h:33
llvm::MachineInstrBuilder::addReg
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
Definition: MachineInstrBuilder.h:97
llvm::MachineOperand::getReg
Register getReg() const
getReg - Returns the register number.
Definition: MachineOperand.h:359
WebAssemblyMachineFunctionInfo.h
INITIALIZE_PASS
INITIALIZE_PASS(WebAssemblyPeephole, DEBUG_TYPE, "WebAssembly peephole optimizations", false, false) FunctionPass *llvm
Definition: WebAssemblyPeephole.cpp:53
llvm::MachineFunction
Definition: MachineFunction.h:257
maybeRewriteToFallthrough
static bool maybeRewriteToFallthrough(MachineInstr &MI, MachineBasicBlock &MBB, const MachineFunction &MF, WebAssemblyFunctionInfo &MFI, MachineRegisterInfo &MRI, const WebAssemblyInstrInfo &TII)
Definition: WebAssemblyPeephole.cpp:75
llvm::AnalysisUsage::setPreservesCFG
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:265
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::WebAssemblySubtarget
Definition: WebAssemblySubtarget.h:35
llvm::ifs::IFSSymbolType::Func
@ Func
MRI
unsigned const MachineRegisterInfo * MRI
Definition: AArch64AdvSIMDScalarPass.cpp:105
llvm::Register
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
MBB
MachineBasicBlock & MBB
Definition: AArch64SLSHardening.cpp:74
llvm::WebAssemblyInstrInfo
Definition: WebAssemblyInstrInfo.h:38
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:50
llvm::MachineFunction::getFunction
Function & getFunction()
Return the LLVM function that this machine code represents.
Definition: MachineFunction.h:626
WebAssemblySubtarget.h
llvm::BuildMI
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
Definition: MachineInstrBuilder.h:357
MachineInstrBuilder.h
llvm::MachineOperand::getSymbolName
const char * getSymbolName() const
Definition: MachineOperand.h:617
llvm::MachineOperand::setReg
void setReg(Register Reg)
Change the register this operand corresponds to.
Definition: MachineOperand.cpp:56
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:308
llvm::AnalysisUsage::addRequired
AnalysisUsage & addRequired()
Definition: PassAnalysisSupport.h:75
llvm::cl::desc
Definition: CommandLine.h:412
llvm::MachineInstrBundleIterator< MachineInstr >
llvm::MachineBasicBlock::end
iterator end()
Definition: MachineBasicBlock.h:311
llvm::createWebAssemblyPeephole
FunctionPass * createWebAssemblyPeephole()