LLVM  10.0.0svn
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;
66  unsigned NewReg = MRI.createVirtualRegister(MRI.getRegClass(OldReg));
67  MO.setReg(NewReg);
68  MO.setIsDead();
69  MFI.stackifyVReg(NewReg);
70  }
71  return Changed;
72 }
73 
75  const MachineFunction &MF,
79  unsigned FallthroughOpc,
80  unsigned CopyLocalOpc) {
82  return false;
83  if (&MBB != &MF.back())
84  return false;
85 
86  MachineBasicBlock::iterator End = MBB.end();
87  --End;
88  assert(End->getOpcode() == WebAssembly::END_FUNCTION);
89  --End;
90  if (&MI != &*End)
91  return false;
92 
93  if (FallthroughOpc != WebAssembly::FALLTHROUGH_RETURN_VOID) {
94  // If the operand isn't stackified, insert a COPY to read the operand and
95  // stackify it.
96  MachineOperand &MO = MI.getOperand(0);
97  unsigned Reg = MO.getReg();
98  if (!MFI.isVRegStackified(Reg)) {
99  unsigned NewReg = MRI.createVirtualRegister(MRI.getRegClass(Reg));
100  BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(CopyLocalOpc), NewReg)
101  .addReg(Reg);
102  MO.setReg(NewReg);
103  MFI.stackifyVReg(NewReg);
104  }
105  }
106 
107  // Rewrite the return.
108  MI.setDesc(TII.get(FallthroughOpc));
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 = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
124  bool Changed = false;
125 
126  for (auto &MBB : MF)
127  for (auto &MI : MBB)
128  switch (MI.getOpcode()) {
129  default:
130  break;
131  case WebAssembly::CALL_i32:
132  case WebAssembly::CALL_i64: {
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  unsigned OldReg = MO.getReg();
147  unsigned 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_I32:
160  Changed |= maybeRewriteToFallthrough(
161  MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_I32,
162  WebAssembly::COPY_I32);
163  break;
164  case WebAssembly::RETURN_I64:
165  Changed |= maybeRewriteToFallthrough(
166  MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_I64,
167  WebAssembly::COPY_I64);
168  break;
169  case WebAssembly::RETURN_F32:
170  Changed |= maybeRewriteToFallthrough(
171  MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_F32,
172  WebAssembly::COPY_F32);
173  break;
174  case WebAssembly::RETURN_F64:
175  Changed |= maybeRewriteToFallthrough(
176  MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_F64,
177  WebAssembly::COPY_F64);
178  break;
179  case WebAssembly::RETURN_v16i8:
180  Changed |= maybeRewriteToFallthrough(
181  MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_v16i8,
182  WebAssembly::COPY_V128);
183  break;
184  case WebAssembly::RETURN_v8i16:
185  Changed |= maybeRewriteToFallthrough(
186  MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_v8i16,
187  WebAssembly::COPY_V128);
188  break;
189  case WebAssembly::RETURN_v4i32:
190  Changed |= maybeRewriteToFallthrough(
191  MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_v4i32,
192  WebAssembly::COPY_V128);
193  break;
194  case WebAssembly::RETURN_v2i64:
195  Changed |= maybeRewriteToFallthrough(
196  MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_v2i64,
197  WebAssembly::COPY_V128);
198  break;
199  case WebAssembly::RETURN_v4f32:
200  Changed |= maybeRewriteToFallthrough(
201  MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_v4f32,
202  WebAssembly::COPY_V128);
203  break;
204  case WebAssembly::RETURN_v2f64:
205  Changed |= maybeRewriteToFallthrough(
206  MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_v2f64,
207  WebAssembly::COPY_V128);
208  break;
209  case WebAssembly::RETURN_VOID:
210  Changed |= maybeRewriteToFallthrough(
211  MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_VOID,
212  WebAssembly::INSTRUCTION_LIST_END);
213  break;
214  }
215 
216  return Changed;
217 }
const TargetRegisterClass * getRegClass(unsigned Reg) const
Return the register class of the specified virtual register.
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:139
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
Definition: MachineInstr.h:385
FunctionPass * createWebAssemblyPeephole()
unsigned Reg
static cl::opt< bool > DisableWebAssemblyFallthroughReturnOpt("disable-wasm-fallthrough-return-opt", cl::Hidden, cl::desc("WebAssembly: Disable fallthrough-return optimizations."), cl::init(false))
void setIsDead(bool Val=true)
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end...
static bool maybeRewriteToDrop(unsigned OldReg, unsigned NewReg, MachineOperand &MO, WebAssemblyFunctionInfo &MFI, MachineRegisterInfo &MRI)
If desirable, rewrite NewReg to a drop register.
AnalysisUsage & addRequired()
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
const char * getSymbolName() const
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:432
unsigned const MachineRegisterInfo * MRI
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
This file provides WebAssembly-specific target descriptions.
Represent the analysis usage information of a pass.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:284
static bool maybeRewriteToFallthrough(MachineInstr &MI, MachineBasicBlock &MBB, const MachineFunction &MF, WebAssemblyFunctionInfo &MFI, MachineRegisterInfo &MRI, const WebAssemblyInstrInfo &TII, unsigned FallthroughOpc, unsigned CopyLocalOpc)
This file declares the WebAssembly-specific subclass of TargetSubtarget.
void setDesc(const MCInstrDesc &tid)
Replace the instruction descriptor (thus opcode) of the current instruction with a new one...
MachineOperand class - Representation of each machine instruction operand.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:301
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
Representation of each machine instruction.
Definition: MachineInstr.h:64
This class is derived from MachineFunctionInfo and contains private WebAssembly-specific information ...
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
void setReg(unsigned Reg)
Change the register this operand corresponds to.
This file declares WebAssembly-specific per-machine-function information.
const MachineBasicBlock & back() const
bool isSymbol() const
isSymbol - Tests if this is a MO_ExternalSymbol operand.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
INITIALIZE_PASS(WebAssemblyPeephole, DEBUG_TYPE, "WebAssembly peephole optimizations", false, false) FunctionPass *llvm
Register getReg() const
getReg - Returns the register number.
#define LLVM_DEBUG(X)
Definition: Debug.h:122
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:416
#define DEBUG_TYPE
const char * getLibcallName(RTLIB::Libcall Call) const
Get the libcall routine name for the specified libcall.