2015-11-26 00:55:01 +08:00
|
|
|
//===-- WebAssemblyPeephole.cpp - WebAssembly Peephole Optimiztions -------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
|
|
|
/// \brief Late peephole optimizations for WebAssembly.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "WebAssembly.h"
|
|
|
|
#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
|
|
|
|
#include "WebAssemblyMachineFunctionInfo.h"
|
2016-01-26 12:01:11 +08:00
|
|
|
#include "WebAssemblySubtarget.h"
|
|
|
|
#include "llvm/Analysis/TargetLibraryInfo.h"
|
2015-11-26 00:55:01 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2016-01-26 12:01:11 +08:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2015-11-26 00:55:01 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "wasm-peephole"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class WebAssemblyPeephole final : public MachineFunctionPass {
|
|
|
|
const char *getPassName() const override {
|
|
|
|
return "WebAssembly late peephole optimizer";
|
|
|
|
}
|
|
|
|
|
2015-12-10 22:12:04 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.setPreservesCFG();
|
2016-01-26 12:01:11 +08:00
|
|
|
AU.addRequired<TargetLibraryInfoWrapperPass>();
|
2015-12-10 22:12:04 +08:00
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
2015-11-26 00:55:01 +08:00
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
WebAssemblyPeephole() : MachineFunctionPass(ID) {}
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
char WebAssemblyPeephole::ID = 0;
|
|
|
|
FunctionPass *llvm::createWebAssemblyPeephole() {
|
|
|
|
return new WebAssemblyPeephole();
|
|
|
|
}
|
|
|
|
|
2016-01-26 12:01:11 +08:00
|
|
|
/// If desirable, rewrite NewReg to a discard register.
|
|
|
|
static bool MaybeRewriteToDiscard(unsigned OldReg, unsigned NewReg,
|
|
|
|
MachineOperand &MO,
|
|
|
|
WebAssemblyFunctionInfo &MFI,
|
|
|
|
MachineRegisterInfo &MRI) {
|
2015-11-26 00:55:01 +08:00
|
|
|
bool Changed = false;
|
2016-01-26 12:01:11 +08:00
|
|
|
// TODO: Handle SP/physregs
|
|
|
|
if (OldReg == NewReg && TargetRegisterInfo::isVirtualRegister(NewReg)) {
|
|
|
|
Changed = true;
|
|
|
|
unsigned NewReg = MRI.createVirtualRegister(MRI.getRegClass(OldReg));
|
|
|
|
MO.setReg(NewReg);
|
|
|
|
MO.setIsDead();
|
|
|
|
MFI.stackifyVReg(NewReg);
|
|
|
|
MFI.addWAReg(NewReg, WebAssemblyFunctionInfo::UnusedReg);
|
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WebAssemblyPeephole::runOnMachineFunction(MachineFunction &MF) {
|
|
|
|
DEBUG({
|
|
|
|
dbgs() << "********** Store Results **********\n"
|
|
|
|
<< "********** Function: " << MF.getName() << '\n';
|
|
|
|
});
|
2015-11-26 00:55:01 +08:00
|
|
|
|
|
|
|
MachineRegisterInfo &MRI = MF.getRegInfo();
|
|
|
|
WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
|
2016-01-26 12:01:11 +08:00
|
|
|
const WebAssemblyTargetLowering &TLI =
|
|
|
|
*MF.getSubtarget<WebAssemblySubtarget>().getTargetLowering();
|
|
|
|
auto &LibInfo = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
|
|
|
|
bool Changed = false;
|
2015-11-26 00:55:01 +08:00
|
|
|
|
|
|
|
for (auto &MBB : MF)
|
|
|
|
for (auto &MI : MBB)
|
|
|
|
switch (MI.getOpcode()) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case WebAssembly::STORE8_I32:
|
|
|
|
case WebAssembly::STORE16_I32:
|
|
|
|
case WebAssembly::STORE8_I64:
|
|
|
|
case WebAssembly::STORE16_I64:
|
|
|
|
case WebAssembly::STORE32_I64:
|
|
|
|
case WebAssembly::STORE_F32:
|
|
|
|
case WebAssembly::STORE_F64:
|
|
|
|
case WebAssembly::STORE_I32:
|
|
|
|
case WebAssembly::STORE_I64: {
|
|
|
|
// Store instructions return their value operand. If we ended up using
|
|
|
|
// the same register for both, replace it with a dead def so that it
|
|
|
|
// can use $discard instead.
|
|
|
|
MachineOperand &MO = MI.getOperand(0);
|
|
|
|
unsigned OldReg = MO.getReg();
|
2016-01-26 12:01:11 +08:00
|
|
|
unsigned NewReg =
|
|
|
|
MI.getOperand(WebAssembly::StoreValueOperandNo).getReg();
|
|
|
|
Changed |= MaybeRewriteToDiscard(OldReg, NewReg, MO, MFI, MRI);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case WebAssembly::CALL_I32:
|
|
|
|
case WebAssembly::CALL_I64: {
|
|
|
|
MachineOperand &Op1 = MI.getOperand(1);
|
|
|
|
if (Op1.isSymbol()) {
|
|
|
|
StringRef Name(Op1.getSymbolName());
|
|
|
|
if (Name == TLI.getLibcallName(RTLIB::MEMCPY) ||
|
|
|
|
Name == TLI.getLibcallName(RTLIB::MEMMOVE) ||
|
|
|
|
Name == TLI.getLibcallName(RTLIB::MEMSET)) {
|
|
|
|
LibFunc::Func Func;
|
|
|
|
if (LibInfo.getLibFunc(Name, Func)) {
|
|
|
|
if (!MI.getOperand(2).isReg())
|
|
|
|
report_fatal_error(
|
|
|
|
"Call to builtin function with wrong signature");
|
|
|
|
MachineOperand &MO = MI.getOperand(0);
|
|
|
|
unsigned OldReg = MO.getReg();
|
|
|
|
unsigned NewReg = MI.getOperand(2).getReg();
|
|
|
|
if (MRI.getRegClass(NewReg) != MRI.getRegClass(OldReg))
|
|
|
|
report_fatal_error(
|
|
|
|
"Call to builtin function with wrong signature");
|
|
|
|
Changed |= MaybeRewriteToDiscard(OldReg, NewReg, MO, MFI, MRI);
|
|
|
|
}
|
|
|
|
}
|
2015-11-26 00:55:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|