LLVM  13.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"
17 #include "WebAssemblySubtarget.h"
22 using namespace llvm;
23 
24 #define DEBUG_TYPE "wasm-peephole"
25 
27  "disable-wasm-fallthrough-return-opt", cl::Hidden,
28  cl::desc("WebAssembly: Disable fallthrough-return optimizations."),
29  cl::init(false));
30 
31 namespace {
32 class WebAssemblyPeephole final : public MachineFunctionPass {
33  StringRef getPassName() const override {
34  return "WebAssembly late peephole optimizer";
35  }
36 
37  void getAnalysisUsage(AnalysisUsage &AU) const override {
38  AU.setPreservesCFG();
41  }
42 
43  bool runOnMachineFunction(MachineFunction &MF) override;
44 
45 public:
46  static char ID;
47  WebAssemblyPeephole() : MachineFunctionPass(ID) {}
48 };
49 } // end anonymous namespace
50 
52 INITIALIZE_PASS(WebAssemblyPeephole, DEBUG_TYPE,
53  "WebAssembly peephole optimizations", false, false)
54 
56  return new WebAssemblyPeephole();
57 }
58 
59 /// If desirable, rewrite NewReg to a drop register.
60 static bool maybeRewriteToDrop(unsigned OldReg, unsigned NewReg,
63  bool Changed = false;
64  if (OldReg == NewReg) {
65  Changed = true;
67  MO.setReg(NewReg);
68  MO.setIsDead();
69  MFI.stackifyVReg(MRI, NewReg);
70  }
71  return Changed;
72 }
73 
75  const MachineFunction &MF,
78  const WebAssemblyInstrInfo &TII) {
80  return false;
81  if (&MBB != &MF.back())
82  return false;
83 
85  --End;
86  assert(End->getOpcode() == WebAssembly::END_FUNCTION);
87  --End;
88  if (&MI != &*End)
89  return false;
90 
91  for (auto &MO : MI.explicit_operands()) {
92  // If the operand isn't stackified, insert a COPY to read the operands and
93  // stackify them.
94  Register Reg = MO.getReg();
95  if (!MFI.isVRegStackified(Reg)) {
96  unsigned CopyLocalOpc;
97  const TargetRegisterClass *RegClass = MRI.getRegClass(Reg);
98  switch (RegClass->getID()) {
99  case WebAssembly::I32RegClassID:
100  CopyLocalOpc = WebAssembly::COPY_I32;
101  break;
102  case WebAssembly::I64RegClassID:
103  CopyLocalOpc = WebAssembly::COPY_I64;
104  break;
105  case WebAssembly::F32RegClassID:
106  CopyLocalOpc = WebAssembly::COPY_F32;
107  break;
108  case WebAssembly::F64RegClassID:
109  CopyLocalOpc = WebAssembly::COPY_F64;
110  break;
111  case WebAssembly::V128RegClassID:
112  CopyLocalOpc = WebAssembly::COPY_V128;
113  break;
114  case WebAssembly::FUNCREFRegClassID:
115  CopyLocalOpc = WebAssembly::COPY_FUNCREF;
116  break;
117  case WebAssembly::EXTERNREFRegClassID:
118  CopyLocalOpc = WebAssembly::COPY_EXTERNREF;
119  break;
120  default:
121  llvm_unreachable("Unexpected register class for return operand");
122  }
123  Register NewReg = MRI.createVirtualRegister(RegClass);
124  BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(CopyLocalOpc), NewReg)
125  .addReg(Reg);
126  MO.setReg(NewReg);
127  MFI.stackifyVReg(MRI, NewReg);
128  }
129  }
130 
131  MI.setDesc(TII.get(WebAssembly::FALLTHROUGH_RETURN));
132  return true;
133 }
134 
135 bool WebAssemblyPeephole::runOnMachineFunction(MachineFunction &MF) {
136  LLVM_DEBUG({
137  dbgs() << "********** Peephole **********\n"
138  << "********** Function: " << MF.getName() << '\n';
139  });
140 
143  const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
144  const WebAssemblyTargetLowering &TLI =
145  *MF.getSubtarget<WebAssemblySubtarget>().getTargetLowering();
146  auto &LibInfo =
147  getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(MF.getFunction());
148  bool Changed = false;
149 
150  for (auto &MBB : MF)
151  for (auto &MI : MBB)
152  switch (MI.getOpcode()) {
153  default:
154  break;
155  case WebAssembly::CALL: {
156  MachineOperand &Op1 = MI.getOperand(1);
157  if (Op1.isSymbol()) {
159  if (Name == TLI.getLibcallName(RTLIB::MEMCPY) ||
160  Name == TLI.getLibcallName(RTLIB::MEMMOVE) ||
161  Name == TLI.getLibcallName(RTLIB::MEMSET)) {
162  LibFunc Func;
163  if (LibInfo.getLibFunc(Name, Func)) {
164  const auto &Op2 = MI.getOperand(2);
165  if (!Op2.isReg())
166  report_fatal_error("Peephole: call to builtin function with "
167  "wrong signature, not consuming reg");
168  MachineOperand &MO = MI.getOperand(0);
169  Register OldReg = MO.getReg();
170  Register NewReg = Op2.getReg();
171 
172  if (MRI.getRegClass(NewReg) != MRI.getRegClass(OldReg))
173  report_fatal_error("Peephole: call to builtin function with "
174  "wrong signature, from/to mismatch");
175  Changed |= maybeRewriteToDrop(OldReg, NewReg, MO, MFI, MRI);
176  }
177  }
178  }
179  break;
180  }
181  // Optimize away an explicit void return at the end of the function.
182  case WebAssembly::RETURN:
183  Changed |= maybeRewriteToFallthrough(MI, MBB, MF, MFI, MRI, TII);
184  break;
185  }
186 
187  return Changed;
188 }
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:100
llvm::TargetRegisterClass::getID
unsigned getID() const
Return the register class ID number.
Definition: TargetRegisterInfo.h:69
llvm
Definition: AllocatorList.h:23
Reg
unsigned Reg
Definition: MachineSink.cpp:1566
WebAssembly.h
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:158
DEBUG_TYPE
#define DEBUG_TYPE
Definition: WebAssemblyPeephole.cpp:24
llvm::MachineRegisterInfo
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Definition: MachineRegisterInfo.h:52
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:751
llvm::TargetLoweringBase::getLibcallName
const char * getLibcallName(RTLIB::Libcall Call) const
Get the libcall routine name for the specified libcall.
Definition: TargetLowering.h:2811
llvm::cl::Hidden
@ Hidden
Definition: CommandLine.h:140
llvm::MachineOperand::isSymbol
bool isSymbol() const
isSymbol - Tests if this is a MO_ExternalSymbol operand.
Definition: MachineOperand.h:338
llvm::NVPTXISD::RETURN
@ RETURN
Definition: NVPTXISelLowering.h:49
llvm::AArch64ISD::CALL
@ CALL
Definition: AArch64ISelLowering.h:52
llvm::ARMISD::MEMCPY
@ MEMCPY
Definition: ARMISelLowering.h:294
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:102
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:122
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:132
llvm::MachineFunction::getRegInfo
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Definition: MachineFunction.h:565
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:60
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:653
llvm::LibFunc
LibFunc
Definition: TargetLibraryInfo.h:34
llvm::TargetRegisterClass
Definition: TargetRegisterInfo.h:46
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
TargetLibraryInfo.h
TII
const HexagonInstrInfo * TII
Definition: HexagonCopyToCombine.cpp:129
llvm::MachineOperand
MachineOperand class - Representation of each machine instruction operand.
Definition: MachineOperand.h:49
llvm::report_fatal_error
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:140
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:95
WebAssemblyMCTargetDesc.h
llvm::MachineRegisterInfo::getRegClass
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
Definition: MachineRegisterInfo.h:634
llvm::MachineFunction::getSubtarget
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Definition: MachineFunction.h:555
llvm::WebAssemblyFunctionInfo::stackifyVReg
void stackifyVReg(MachineRegisterInfo &MRI, unsigned VReg)
Definition: WebAssemblyMachineFunctionInfo.h:124
llvm::cl::opt< bool >
llvm::MachineOperand::setIsDead
void setIsDead(bool Val=true)
Definition: MachineOperand.h:503
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:64
llvm::TargetLibraryInfoWrapperPass
Definition: TargetLibraryInfo.h:446
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:440
MachineFunctionPass.h
llvm::WebAssemblyFunctionInfo::isVRegStackified
bool isVRegStackified(unsigned VReg) const
Definition: WebAssemblyMachineFunctionInfo.h:136
llvm::MachineFunction::getName
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
Definition: MachineFunction.cpp:522
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::elfabi::ELFSymbolType::Func
@ Func
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:98
llvm::MachineOperand::getReg
Register getReg() const
getReg - Returns the register number.
Definition: MachineOperand.h:357
WebAssemblyMachineFunctionInfo.h
INITIALIZE_PASS
INITIALIZE_PASS(WebAssemblyPeephole, DEBUG_TYPE, "WebAssembly peephole optimizations", false, false) FunctionPass *llvm
Definition: WebAssemblyPeephole.cpp:52
llvm::MachineFunction
Definition: MachineFunction.h:227
maybeRewriteToFallthrough
static bool maybeRewriteToFallthrough(MachineInstr &MI, MachineBasicBlock &MBB, const MachineFunction &MF, WebAssemblyFunctionInfo &MFI, MachineRegisterInfo &MRI, const WebAssemblyInstrInfo &TII)
Definition: WebAssemblyPeephole.cpp:74
llvm::AnalysisUsage::setPreservesCFG
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:253
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:136
llvm::WebAssemblySubtarget
Definition: WebAssemblySubtarget.h:35
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:52
llvm::MachineFunction::getFunction
Function & getFunction()
Return the LLVM function that this machine code represents.
Definition: MachineFunction.h:521
WebAssemblySubtarget.h
MachineInstrBuilder.h
llvm::MachineOperand::getSymbolName
const char * getSymbolName() const
Definition: MachineOperand.h:605
llvm::BuildMI
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
Definition: MachineInstrBuilder.h:329
llvm::MachineOperand::setReg
void setReg(Register Reg)
Change the register this operand corresponds to.
Definition: MachineOperand.cpp:55
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
llvm::AnalysisUsage::addRequired
AnalysisUsage & addRequired()
Definition: PassAnalysisSupport.h:75
llvm::cl::desc
Definition: CommandLine.h:411
llvm::MachineInstrBundleIterator< MachineInstr >
llvm::MachineBasicBlock::end
iterator end()
Definition: MachineBasicBlock.h:270
llvm::createWebAssemblyPeephole
FunctionPass * createWebAssemblyPeephole()
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:38