LLVM  5.0.0
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 /// \brief 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 
54  return new WebAssemblyPeephole();
55 }
56 
57 /// If desirable, rewrite NewReg to a drop register.
58 static bool MaybeRewriteToDrop(unsigned OldReg, unsigned NewReg,
61  bool Changed = false;
62  if (OldReg == NewReg) {
63  Changed = true;
64  unsigned NewReg = MRI.createVirtualRegister(MRI.getRegClass(OldReg));
65  MO.setReg(NewReg);
66  MO.setIsDead();
67  MFI.stackifyVReg(NewReg);
68  }
69  return Changed;
70 }
71 
73  const MachineFunction &MF,
77  unsigned FallthroughOpc,
78  unsigned CopyLocalOpc) {
80  return false;
81  if (&MBB != &MF.back())
82  return false;
85  if (&MI != &MBB.back())
86  return false;
87  } else {
89  --End;
90  assert(End->getOpcode() == WebAssembly::END_FUNCTION);
91  --End;
92  if (&MI != &*End)
93  return false;
94  }
95 
96  if (FallthroughOpc != WebAssembly::FALLTHROUGH_RETURN_VOID) {
97  // If the operand isn't stackified, insert a COPY to read the operand and
98  // stackify it.
99  MachineOperand &MO = MI.getOperand(0);
100  unsigned Reg = MO.getReg();
101  if (!MFI.isVRegStackified(Reg)) {
102  unsigned NewReg = MRI.createVirtualRegister(MRI.getRegClass(Reg));
103  BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(CopyLocalOpc), NewReg)
104  .addReg(Reg);
105  MO.setReg(NewReg);
106  MFI.stackifyVReg(NewReg);
107  }
108  }
109 
110  // Rewrite the return.
111  MI.setDesc(TII.get(FallthroughOpc));
112  return true;
113 }
114 
115 bool WebAssemblyPeephole::runOnMachineFunction(MachineFunction &MF) {
116  DEBUG({
117  dbgs() << "********** Peephole **********\n"
118  << "********** Function: " << MF.getName() << '\n';
119  });
120 
123  const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
124  const WebAssemblyTargetLowering &TLI =
125  *MF.getSubtarget<WebAssemblySubtarget>().getTargetLowering();
126  auto &LibInfo = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
127  bool Changed = false;
128 
129  for (auto &MBB : MF)
130  for (auto &MI : MBB)
131  switch (MI.getOpcode()) {
132  default:
133  break;
134  case WebAssembly::CALL_I32:
135  case WebAssembly::CALL_I64: {
136  MachineOperand &Op1 = MI.getOperand(1);
137  if (Op1.isSymbol()) {
139  if (Name == TLI.getLibcallName(RTLIB::MEMCPY) ||
142  LibFunc Func;
143  if (LibInfo.getLibFunc(Name, Func)) {
144  const auto &Op2 = MI.getOperand(2);
145  if (!Op2.isReg())
146  report_fatal_error("Peephole: call to builtin function with "
147  "wrong signature, not consuming reg");
148  MachineOperand &MO = MI.getOperand(0);
149  unsigned OldReg = MO.getReg();
150  unsigned NewReg = Op2.getReg();
151 
152  if (MRI.getRegClass(NewReg) != MRI.getRegClass(OldReg))
153  report_fatal_error("Peephole: call to builtin function with "
154  "wrong signature, from/to mismatch");
155  Changed |= MaybeRewriteToDrop(OldReg, NewReg, MO, MFI, MRI);
156  }
157  }
158  }
159  break;
160  }
161  // Optimize away an explicit void return at the end of the function.
162  case WebAssembly::RETURN_I32:
163  Changed |= MaybeRewriteToFallthrough(
164  MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_I32,
165  WebAssembly::COPY_I32);
166  break;
167  case WebAssembly::RETURN_I64:
168  Changed |= MaybeRewriteToFallthrough(
169  MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_I64,
170  WebAssembly::COPY_I64);
171  break;
172  case WebAssembly::RETURN_F32:
173  Changed |= MaybeRewriteToFallthrough(
174  MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_F32,
175  WebAssembly::COPY_F32);
176  break;
177  case WebAssembly::RETURN_F64:
178  Changed |= MaybeRewriteToFallthrough(
179  MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_F64,
180  WebAssembly::COPY_F64);
181  break;
182  case WebAssembly::RETURN_v16i8:
183  Changed |= MaybeRewriteToFallthrough(
184  MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_v16i8,
185  WebAssembly::COPY_V128);
186  break;
187  case WebAssembly::RETURN_v8i16:
188  Changed |= MaybeRewriteToFallthrough(
189  MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_v8i16,
190  WebAssembly::COPY_V128);
191  break;
192  case WebAssembly::RETURN_v4i32:
193  Changed |= MaybeRewriteToFallthrough(
194  MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_v4i32,
195  WebAssembly::COPY_V128);
196  break;
197  case WebAssembly::RETURN_v4f32:
198  Changed |= MaybeRewriteToFallthrough(
199  MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_v4f32,
200  WebAssembly::COPY_V128);
201  break;
202  case WebAssembly::RETURN_VOID:
203  Changed |= MaybeRewriteToFallthrough(
204  MI, MBB, MF, MFI, MRI, TII, WebAssembly::FALLTHROUGH_RETURN_VOID,
205  WebAssembly::INSTRUCTION_LIST_END);
206  break;
207  }
208 
209  return Changed;
210 }
const Triple & getTargetTriple() const
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:103
unsigned createVirtualRegister(const TargetRegisterClass *RegClass)
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
static cl::opt< bool > DisableWebAssemblyFallthroughReturnOpt("disable-wasm-fallthrough-return-opt", cl::Hidden, cl::desc("WebAssembly: Disable fallthrough-return optimizations."), cl::init(false))
const char * getSymbolName() const
bool isVRegStackified(unsigned VReg) const
void setIsDead(bool Val=true)
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end...
AnalysisUsage & addRequired()
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
const TargetRegisterClass * getRegClass(unsigned Reg) const
Return the register class of the specified virtual register.
Reg
All possible values of the reg field in the ModR/M byte.
const char * Name
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:401
unsigned const MachineRegisterInfo * MRI
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:284
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.
bool isSymbol() const
isSymbol - Tests if this is a MO_ExternalSymbol operand.
static const unsigned End
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:298
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...
const char * getLibcallName(RTLIB::Libcall Call) const
Get the libcall routine name for the specified libcall.
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:278
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
Definition: MachineInstr.h:257
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
FunctionPass * createWebAssemblyPeephole()
Representation of each machine instruction.
Definition: MachineInstr.h:59
This class is derived from MachineFunctionInfo and contains private WebAssembly-specific information ...
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition: Triple.h:577
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.
static bool MaybeRewriteToDrop(unsigned OldReg, unsigned NewReg, MachineOperand &MO, WebAssemblyFunctionInfo &MFI, MachineRegisterInfo &MRI)
If desirable, rewrite NewReg to a drop register.
unsigned getReg() const
getReg - Returns the register number.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
#define DEBUG(X)
Definition: Debug.h:118
const MachineBasicBlock & back() const
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
StringRef getName() const
getName - Return the name of the corresponding LLVM function.