LLVM  8.0.1
WebAssemblyPeephole.cpp
Go to the documentation of this file.
1 //===-- 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 /// Late peephole optimizations for WebAssembly.
12 ///
13 //===----------------------------------------------------------------------===//
14 
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;
67  unsigned NewReg = MRI.createVirtualRegister(MRI.getRegClass(OldReg));
68  MO.setReg(NewReg);
69  MO.setIsDead();
70  MFI.stackifyVReg(NewReg);
71  }
72  return Changed;
73 }
74 
76  const MachineFunction &MF,
80  unsigned FallthroughOpc,
81  unsigned CopyLocalOpc) {
83  return false;
84  if (&MBB != &MF.back())
85  return false;
86 
87  MachineBasicBlock::iterator End = MBB.end();
88  --End;
89  assert(End->getOpcode() == WebAssembly::END_FUNCTION);
90  --End;
91  if (&MI != &*End)
92  return false;
93 
94  if (FallthroughOpc != WebAssembly::FALLTHROUGH_RETURN_VOID) {
95  // If the operand isn't stackified, insert a COPY to read the operand and
96  // stackify it.
97  MachineOperand &MO = MI.getOperand(0);
98  unsigned Reg = MO.getReg();
99  if (!MFI.isVRegStackified(Reg)) {
100  unsigned NewReg = MRI.createVirtualRegister(MRI.getRegClass(Reg));
101  BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(CopyLocalOpc), NewReg)
102  .addReg(Reg);
103  MO.setReg(NewReg);
104  MFI.stackifyVReg(NewReg);
105  }
106  }
107 
108  // Rewrite the return.
109  MI.setDesc(TII.get(FallthroughOpc));
110  return true;
111 }
112 
113 bool WebAssemblyPeephole::runOnMachineFunction(MachineFunction &MF) {
114  LLVM_DEBUG({
115  dbgs() << "********** Peephole **********\n"
116  << "********** Function: " << MF.getName() << '\n';
117  });
118 
121  const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
122  const WebAssemblyTargetLowering &TLI =
123  *MF.getSubtarget<WebAssemblySubtarget>().getTargetLowering();
124  auto &LibInfo = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
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_I32:
133  case WebAssembly::CALL_I64: {
134  MachineOperand &Op1 = MI.getOperand(1);
135  if (Op1.isSymbol()) {
137  if (Name == TLI.getLibcallName(RTLIB::MEMCPY) ||
138  Name == TLI.getLibcallName(RTLIB::MEMMOVE) ||
139  Name == TLI.getLibcallName(RTLIB::MEMSET)) {
140  LibFunc Func;
141  if (LibInfo.getLibFunc(Name, Func)) {
142  const auto &Op2 = MI.getOperand(2);
143  if (!Op2.isReg())
144  report_fatal_error("Peephole: call to builtin function with "
145  "wrong signature, not consuming reg");
146  MachineOperand &MO = MI.getOperand(0);
147  unsigned OldReg = MO.getReg();
148  unsigned NewReg = Op2.getReg();
149 
150  if (MRI.getRegClass(NewReg) != MRI.getRegClass(OldReg))
151  report_fatal_error("Peephole: call to builtin function with "
152  "wrong signature, from/to mismatch");
153  Changed |= MaybeRewriteToDrop(OldReg, NewReg, MO, MFI, MRI);
154  }
155  }
156  }
157  break;
158  }
159  // Optimize away an explicit void return at the end of the function.
160  case WebAssembly::RETURN_I32:
161  Changed |= MaybeRewriteToFallthrough(
162  MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_I32,
163  WebAssembly::COPY_I32);
164  break;
165  case WebAssembly::RETURN_I64:
166  Changed |= MaybeRewriteToFallthrough(
167  MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_I64,
168  WebAssembly::COPY_I64);
169  break;
170  case WebAssembly::RETURN_F32:
171  Changed |= MaybeRewriteToFallthrough(
172  MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_F32,
173  WebAssembly::COPY_F32);
174  break;
175  case WebAssembly::RETURN_F64:
176  Changed |= MaybeRewriteToFallthrough(
177  MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_F64,
178  WebAssembly::COPY_F64);
179  break;
180  case WebAssembly::RETURN_v16i8:
181  Changed |= MaybeRewriteToFallthrough(
182  MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_v16i8,
183  WebAssembly::COPY_V128);
184  break;
185  case WebAssembly::RETURN_v8i16:
186  Changed |= MaybeRewriteToFallthrough(
187  MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_v8i16,
188  WebAssembly::COPY_V128);
189  break;
190  case WebAssembly::RETURN_v4i32:
191  Changed |= MaybeRewriteToFallthrough(
192  MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_v4i32,
193  WebAssembly::COPY_V128);
194  break;
195  case WebAssembly::RETURN_v2i64:
196  Changed |= MaybeRewriteToFallthrough(
197  MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_v2i64,
198  WebAssembly::COPY_V128);
199  break;
200  case WebAssembly::RETURN_v4f32:
201  Changed |= MaybeRewriteToFallthrough(
202  MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_v4f32,
203  WebAssembly::COPY_V128);
204  break;
205  case WebAssembly::RETURN_v2f64:
206  Changed |= MaybeRewriteToFallthrough(
207  MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_v2f64,
208  WebAssembly::COPY_V128);
209  break;
210  case WebAssembly::RETURN_VOID:
211  Changed |= MaybeRewriteToFallthrough(
212  MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_VOID,
213  WebAssembly::INSTRUCTION_LIST_END);
214  break;
215  }
216 
217  return Changed;
218 }
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:140
This class represents lattice values for constants.
Definition: AllocatorList.h:24
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
Definition: MachineInstr.h:383
FunctionPass * createWebAssemblyPeephole()
unsigned getReg() const
getReg - Returns the register number.
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...
AnalysisUsage & addRequired()
amdgpu Simplify well known AMD library false Value Value const Twine & Name
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:423
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:285
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:286
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:133
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
static bool MaybeRewriteToDrop(unsigned OldReg, unsigned NewReg, MachineOperand &MO, WebAssemblyFunctionInfo &MFI, MachineRegisterInfo &MRI)
If desirable, rewrite NewReg to a drop register.
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:49
INITIALIZE_PASS(WebAssemblyPeephole, DEBUG_TYPE, "WebAssembly peephole optimizations", false, false) FunctionPass *llvm
#define LLVM_DEBUG(X)
Definition: Debug.h:123
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:414
#define DEBUG_TYPE
unsigned createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
const char * getLibcallName(RTLIB::Libcall Call) const
Get the libcall routine name for the specified libcall.