| 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 | |
| 14 | #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" |
| 15 | #include "WebAssembly.h" |
| 16 | #include "WebAssemblyMachineFunctionInfo.h" |
| 17 | #include "WebAssemblySubtarget.h" |
| 18 | #include "WebAssemblyUtilities.h" |
| 19 | #include "llvm/Analysis/TargetLibraryInfo.h" |
| 20 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 21 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 22 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 23 | using namespace llvm; |
| 24 | |
| 25 | #define DEBUG_TYPE "wasm-peephole" |
| 26 | |
| 27 | static cl::opt<bool> DisableWebAssemblyFallthroughReturnOpt( |
| 28 | "disable-wasm-fallthrough-return-opt" , cl::Hidden, |
| 29 | cl::desc("WebAssembly: Disable fallthrough-return optimizations." ), |
| 30 | cl::init(Val: 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(); |
| 40 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
| 41 | AU.addRequired<LibcallLoweringInfoWrapper>(); |
| 42 | MachineFunctionPass::getAnalysisUsage(AU); |
| 43 | } |
| 44 | |
| 45 | bool runOnMachineFunction(MachineFunction &MF) override; |
| 46 | |
| 47 | public: |
| 48 | static char ID; |
| 49 | WebAssemblyPeephole() : MachineFunctionPass(ID) {} |
| 50 | }; |
| 51 | } // end anonymous namespace |
| 52 | |
| 53 | char WebAssemblyPeephole::ID = 0; |
| 54 | INITIALIZE_PASS(WebAssemblyPeephole, DEBUG_TYPE, |
| 55 | "WebAssembly peephole optimizations" , false, false) |
| 56 | |
| 57 | FunctionPass *llvm::createWebAssemblyPeephole() { |
| 58 | return new WebAssemblyPeephole(); |
| 59 | } |
| 60 | |
| 61 | /// If desirable, rewrite NewReg to a drop register. |
| 62 | static bool maybeRewriteToDrop(unsigned OldReg, unsigned NewReg, |
| 63 | MachineOperand &MO, WebAssemblyFunctionInfo &MFI, |
| 64 | MachineRegisterInfo &MRI) { |
| 65 | bool Changed = false; |
| 66 | if (OldReg == NewReg) { |
| 67 | Changed = true; |
| 68 | Register NewReg = MRI.createVirtualRegister(RegClass: MRI.getRegClass(Reg: OldReg)); |
| 69 | MO.setReg(NewReg); |
| 70 | MO.setIsDead(); |
| 71 | MFI.stackifyVReg(MRI, VReg: NewReg); |
| 72 | } |
| 73 | return Changed; |
| 74 | } |
| 75 | |
| 76 | static bool maybeRewriteToFallthrough(MachineInstr &MI, MachineBasicBlock &MBB, |
| 77 | const MachineFunction &MF, |
| 78 | WebAssemblyFunctionInfo &MFI, |
| 79 | MachineRegisterInfo &MRI, |
| 80 | const WebAssemblyInstrInfo &TII) { |
| 81 | if (DisableWebAssemblyFallthroughReturnOpt) |
| 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 | for (auto &MO : MI.explicit_operands()) { |
| 94 | // If the operand isn't stackified, insert a COPY to read the operands and |
| 95 | // stackify them. |
| 96 | Register Reg = MO.getReg(); |
| 97 | if (!MFI.isVRegStackified(VReg: Reg)) { |
| 98 | unsigned CopyLocalOpc; |
| 99 | const TargetRegisterClass *RegClass = MRI.getRegClass(Reg); |
| 100 | CopyLocalOpc = WebAssembly::getCopyOpcodeForRegClass(RC: RegClass); |
| 101 | Register NewReg = MRI.createVirtualRegister(RegClass); |
| 102 | BuildMI(BB&: MBB, I&: MI, MIMD: MI.getDebugLoc(), MCID: TII.get(Opcode: CopyLocalOpc), DestReg: NewReg) |
| 103 | .addReg(RegNo: Reg); |
| 104 | MO.setReg(NewReg); |
| 105 | MFI.stackifyVReg(MRI, VReg: NewReg); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | MI.setDesc(TII.get(Opcode: WebAssembly::FALLTHROUGH_RETURN)); |
| 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 | |
| 119 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 120 | WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>(); |
| 121 | const WebAssemblySubtarget &Subtarget = |
| 122 | MF.getSubtarget<WebAssemblySubtarget>(); |
| 123 | const auto &TII = *Subtarget.getInstrInfo(); |
| 124 | auto &LibInfo = |
| 125 | getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F: MF.getFunction()); |
| 126 | |
| 127 | const LibcallLoweringInfo &LibcallLowering = |
| 128 | getAnalysis<LibcallLoweringInfoWrapper>().getLibcallLowering( |
| 129 | M: *MF.getFunction().getParent(), Subtarget); |
| 130 | |
| 131 | RTLIB::LibcallImpl MemcpyImpl = LibcallLowering.getLibcallImpl(Call: RTLIB::MEMCPY); |
| 132 | RTLIB::LibcallImpl MemmoveImpl = |
| 133 | LibcallLowering.getLibcallImpl(Call: RTLIB::MEMMOVE); |
| 134 | RTLIB::LibcallImpl MemsetImpl = LibcallLowering.getLibcallImpl(Call: RTLIB::MEMSET); |
| 135 | |
| 136 | StringRef MemcpyName = |
| 137 | RTLIB::RuntimeLibcallsInfo::getLibcallImplName(CallImpl: MemcpyImpl); |
| 138 | StringRef MemmoveName = |
| 139 | RTLIB::RuntimeLibcallsInfo::getLibcallImplName(CallImpl: MemmoveImpl); |
| 140 | StringRef MemsetName = |
| 141 | RTLIB::RuntimeLibcallsInfo::getLibcallImplName(CallImpl: MemsetImpl); |
| 142 | |
| 143 | bool Changed = false; |
| 144 | |
| 145 | for (auto &MBB : MF) |
| 146 | for (auto &MI : MBB) |
| 147 | switch (MI.getOpcode()) { |
| 148 | default: |
| 149 | break; |
| 150 | case WebAssembly::CALL: { |
| 151 | MachineOperand &Op1 = MI.getOperand(i: 1); |
| 152 | if (Op1.isSymbol()) { |
| 153 | StringRef Name(Op1.getSymbolName()); |
| 154 | if (Name == MemcpyName || Name == MemmoveName || Name == MemsetName) { |
| 155 | LibFunc Func; |
| 156 | if (LibInfo.getLibFunc(funcName: Name, F&: Func)) { |
| 157 | const auto &Op2 = MI.getOperand(i: 2); |
| 158 | if (!Op2.isReg()) |
| 159 | report_fatal_error(reason: "Peephole: call to builtin function with " |
| 160 | "wrong signature, not consuming reg" ); |
| 161 | MachineOperand &MO = MI.getOperand(i: 0); |
| 162 | Register OldReg = MO.getReg(); |
| 163 | Register NewReg = Op2.getReg(); |
| 164 | |
| 165 | if (MRI.getRegClass(Reg: NewReg) != MRI.getRegClass(Reg: OldReg)) |
| 166 | report_fatal_error(reason: "Peephole: call to builtin function with " |
| 167 | "wrong signature, from/to mismatch" ); |
| 168 | Changed |= maybeRewriteToDrop(OldReg, NewReg, MO, MFI, MRI); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | break; |
| 173 | } |
| 174 | // Optimize away an explicit void return at the end of the function. |
| 175 | case WebAssembly::RETURN: |
| 176 | Changed |= maybeRewriteToFallthrough(MI, MBB, MF, MFI, MRI, TII); |
| 177 | break; |
| 178 | } |
| 179 | |
| 180 | return Changed; |
| 181 | } |
| 182 | |