Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 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 | |
| 15 | #include "WebAssembly.h" |
| 16 | #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" |
| 17 | #include "WebAssemblyMachineFunctionInfo.h" |
Dan Gohman | bdf08d5 | 2016-01-26 04:01:11 +0000 | [diff] [blame^] | 18 | #include "WebAssemblySubtarget.h" |
| 19 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Dan Gohman | bdf08d5 | 2016-01-26 04:01:11 +0000 | [diff] [blame^] | 21 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | |
| 24 | #define DEBUG_TYPE "wasm-peephole" |
| 25 | |
| 26 | namespace { |
| 27 | class WebAssemblyPeephole final : public MachineFunctionPass { |
| 28 | const char *getPassName() const override { |
| 29 | return "WebAssembly late peephole optimizer"; |
| 30 | } |
| 31 | |
Dan Gohman | acc0941 | 2015-12-10 14:12:04 +0000 | [diff] [blame] | 32 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 33 | AU.setPreservesCFG(); |
Dan Gohman | bdf08d5 | 2016-01-26 04:01:11 +0000 | [diff] [blame^] | 34 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Dan Gohman | acc0941 | 2015-12-10 14:12:04 +0000 | [diff] [blame] | 35 | MachineFunctionPass::getAnalysisUsage(AU); |
| 36 | } |
| 37 | |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 38 | bool runOnMachineFunction(MachineFunction &MF) override; |
| 39 | |
| 40 | public: |
| 41 | static char ID; |
| 42 | WebAssemblyPeephole() : MachineFunctionPass(ID) {} |
| 43 | }; |
| 44 | } // end anonymous namespace |
| 45 | |
| 46 | char WebAssemblyPeephole::ID = 0; |
| 47 | FunctionPass *llvm::createWebAssemblyPeephole() { |
| 48 | return new WebAssemblyPeephole(); |
| 49 | } |
| 50 | |
Dan Gohman | bdf08d5 | 2016-01-26 04:01:11 +0000 | [diff] [blame^] | 51 | /// If desirable, rewrite NewReg to a discard register. |
| 52 | static bool MaybeRewriteToDiscard(unsigned OldReg, unsigned NewReg, |
| 53 | MachineOperand &MO, |
| 54 | WebAssemblyFunctionInfo &MFI, |
| 55 | MachineRegisterInfo &MRI) { |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 56 | bool Changed = false; |
Dan Gohman | bdf08d5 | 2016-01-26 04:01:11 +0000 | [diff] [blame^] | 57 | // TODO: Handle SP/physregs |
| 58 | if (OldReg == NewReg && TargetRegisterInfo::isVirtualRegister(NewReg)) { |
| 59 | Changed = true; |
| 60 | unsigned NewReg = MRI.createVirtualRegister(MRI.getRegClass(OldReg)); |
| 61 | MO.setReg(NewReg); |
| 62 | MO.setIsDead(); |
| 63 | MFI.stackifyVReg(NewReg); |
| 64 | MFI.addWAReg(NewReg, WebAssemblyFunctionInfo::UnusedReg); |
| 65 | } |
| 66 | return Changed; |
| 67 | } |
| 68 | |
| 69 | bool WebAssemblyPeephole::runOnMachineFunction(MachineFunction &MF) { |
| 70 | DEBUG({ |
| 71 | dbgs() << "********** Store Results **********\n" |
| 72 | << "********** Function: " << MF.getName() << '\n'; |
| 73 | }); |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 74 | |
| 75 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 76 | WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>(); |
Dan Gohman | bdf08d5 | 2016-01-26 04:01:11 +0000 | [diff] [blame^] | 77 | const WebAssemblyTargetLowering &TLI = |
| 78 | *MF.getSubtarget<WebAssemblySubtarget>().getTargetLowering(); |
| 79 | auto &LibInfo = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); |
| 80 | bool Changed = false; |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 81 | |
| 82 | for (auto &MBB : MF) |
| 83 | for (auto &MI : MBB) |
| 84 | switch (MI.getOpcode()) { |
| 85 | default: |
| 86 | break; |
| 87 | case WebAssembly::STORE8_I32: |
| 88 | case WebAssembly::STORE16_I32: |
| 89 | case WebAssembly::STORE8_I64: |
| 90 | case WebAssembly::STORE16_I64: |
| 91 | case WebAssembly::STORE32_I64: |
| 92 | case WebAssembly::STORE_F32: |
| 93 | case WebAssembly::STORE_F64: |
| 94 | case WebAssembly::STORE_I32: |
| 95 | case WebAssembly::STORE_I64: { |
| 96 | // Store instructions return their value operand. If we ended up using |
| 97 | // the same register for both, replace it with a dead def so that it |
| 98 | // can use $discard instead. |
| 99 | MachineOperand &MO = MI.getOperand(0); |
| 100 | unsigned OldReg = MO.getReg(); |
Dan Gohman | bdf08d5 | 2016-01-26 04:01:11 +0000 | [diff] [blame^] | 101 | unsigned NewReg = |
| 102 | MI.getOperand(WebAssembly::StoreValueOperandNo).getReg(); |
| 103 | Changed |= MaybeRewriteToDiscard(OldReg, NewReg, MO, MFI, MRI); |
| 104 | break; |
| 105 | } |
| 106 | case WebAssembly::CALL_I32: |
| 107 | case WebAssembly::CALL_I64: { |
| 108 | MachineOperand &Op1 = MI.getOperand(1); |
| 109 | if (Op1.isSymbol()) { |
| 110 | StringRef Name(Op1.getSymbolName()); |
| 111 | if (Name == TLI.getLibcallName(RTLIB::MEMCPY) || |
| 112 | Name == TLI.getLibcallName(RTLIB::MEMMOVE) || |
| 113 | Name == TLI.getLibcallName(RTLIB::MEMSET)) { |
| 114 | LibFunc::Func Func; |
| 115 | if (LibInfo.getLibFunc(Name, Func)) { |
| 116 | if (!MI.getOperand(2).isReg()) |
| 117 | report_fatal_error( |
| 118 | "Call to builtin function with wrong signature"); |
| 119 | MachineOperand &MO = MI.getOperand(0); |
| 120 | unsigned OldReg = MO.getReg(); |
| 121 | unsigned NewReg = MI.getOperand(2).getReg(); |
| 122 | if (MRI.getRegClass(NewReg) != MRI.getRegClass(OldReg)) |
| 123 | report_fatal_error( |
| 124 | "Call to builtin function with wrong signature"); |
| 125 | Changed |= MaybeRewriteToDiscard(OldReg, NewReg, MO, MFI, MRI); |
| 126 | } |
| 127 | } |
Dan Gohman | 81719f8 | 2015-11-25 16:55:01 +0000 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | return Changed; |
| 133 | } |