2015-12-05 11:03:35 +08:00
|
|
|
//===-- WebAssemblyLowerBrUnless.cpp - Lower br_unless --------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2015-12-05 11:03:35 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
2018-05-01 23:54:18 +08:00
|
|
|
/// This file lowers br_unless into br_if with an inverted condition.
|
2015-12-05 11:03:35 +08:00
|
|
|
///
|
|
|
|
/// br_unless is not currently in the spec, but it's very convenient for LLVM
|
|
|
|
/// to use. This pass allows LLVM to use it, for now.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-01-20 13:54:22 +08:00
|
|
|
#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "WebAssembly.h"
|
2015-12-05 11:03:35 +08:00
|
|
|
#include "WebAssemblyMachineFunctionInfo.h"
|
|
|
|
#include "WebAssemblySubtarget.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "wasm-lower-br_unless"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class WebAssemblyLowerBrUnless final : public MachineFunctionPass {
|
2016-10-01 10:56:57 +08:00
|
|
|
StringRef getPassName() const override {
|
2015-12-05 11:03:35 +08:00
|
|
|
return "WebAssembly Lower br_unless";
|
|
|
|
}
|
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.setPreservesCFG();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
WebAssemblyLowerBrUnless() : MachineFunctionPass(ID) {}
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
char WebAssemblyLowerBrUnless::ID = 0;
|
2018-03-31 04:36:58 +08:00
|
|
|
INITIALIZE_PASS(WebAssemblyLowerBrUnless, DEBUG_TYPE,
|
|
|
|
"Lowers br_unless into inverted br_if", false, false)
|
|
|
|
|
2015-12-05 11:03:35 +08:00
|
|
|
FunctionPass *llvm::createWebAssemblyLowerBrUnless() {
|
|
|
|
return new WebAssemblyLowerBrUnless();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WebAssemblyLowerBrUnless::runOnMachineFunction(MachineFunction &MF) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "********** Lowering br_unless **********\n"
|
|
|
|
"********** Function: "
|
|
|
|
<< MF.getName() << '\n');
|
2015-12-05 11:03:35 +08:00
|
|
|
|
|
|
|
auto &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
|
|
|
|
const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
|
|
|
|
auto &MRI = MF.getRegInfo();
|
|
|
|
|
|
|
|
for (auto &MBB : MF) {
|
2016-01-20 13:54:22 +08:00
|
|
|
for (auto MII = MBB.begin(); MII != MBB.end();) {
|
2015-12-05 11:03:35 +08:00
|
|
|
MachineInstr *MI = &*MII++;
|
|
|
|
if (MI->getOpcode() != WebAssembly::BR_UNLESS)
|
|
|
|
continue;
|
|
|
|
|
[webassembly] Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Reviewers: aheejin
Subscribers: jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision for whole review: https://reviews.llvm.org/D65962
llvm-svn: 368627
2019-08-13 06:40:45 +08:00
|
|
|
Register Cond = MI->getOperand(1).getReg();
|
2015-12-05 11:03:35 +08:00
|
|
|
bool Inverted = false;
|
|
|
|
|
|
|
|
// Attempt to invert the condition in place.
|
|
|
|
if (MFI.isVRegStackified(Cond)) {
|
|
|
|
assert(MRI.hasOneDef(Cond));
|
|
|
|
MachineInstr *Def = MRI.getVRegDef(Cond);
|
|
|
|
switch (Def->getOpcode()) {
|
2016-01-20 13:54:22 +08:00
|
|
|
using namespace WebAssembly;
|
2018-09-05 09:27:38 +08:00
|
|
|
case EQ_I32:
|
|
|
|
Def->setDesc(TII.get(NE_I32));
|
|
|
|
Inverted = true;
|
|
|
|
break;
|
|
|
|
case NE_I32:
|
|
|
|
Def->setDesc(TII.get(EQ_I32));
|
|
|
|
Inverted = true;
|
|
|
|
break;
|
|
|
|
case GT_S_I32:
|
|
|
|
Def->setDesc(TII.get(LE_S_I32));
|
|
|
|
Inverted = true;
|
|
|
|
break;
|
|
|
|
case GE_S_I32:
|
|
|
|
Def->setDesc(TII.get(LT_S_I32));
|
|
|
|
Inverted = true;
|
|
|
|
break;
|
|
|
|
case LT_S_I32:
|
|
|
|
Def->setDesc(TII.get(GE_S_I32));
|
|
|
|
Inverted = true;
|
|
|
|
break;
|
|
|
|
case LE_S_I32:
|
|
|
|
Def->setDesc(TII.get(GT_S_I32));
|
|
|
|
Inverted = true;
|
|
|
|
break;
|
|
|
|
case GT_U_I32:
|
|
|
|
Def->setDesc(TII.get(LE_U_I32));
|
|
|
|
Inverted = true;
|
|
|
|
break;
|
|
|
|
case GE_U_I32:
|
|
|
|
Def->setDesc(TII.get(LT_U_I32));
|
|
|
|
Inverted = true;
|
|
|
|
break;
|
|
|
|
case LT_U_I32:
|
|
|
|
Def->setDesc(TII.get(GE_U_I32));
|
|
|
|
Inverted = true;
|
|
|
|
break;
|
|
|
|
case LE_U_I32:
|
|
|
|
Def->setDesc(TII.get(GT_U_I32));
|
|
|
|
Inverted = true;
|
|
|
|
break;
|
|
|
|
case EQ_I64:
|
|
|
|
Def->setDesc(TII.get(NE_I64));
|
|
|
|
Inverted = true;
|
|
|
|
break;
|
|
|
|
case NE_I64:
|
|
|
|
Def->setDesc(TII.get(EQ_I64));
|
|
|
|
Inverted = true;
|
|
|
|
break;
|
|
|
|
case GT_S_I64:
|
|
|
|
Def->setDesc(TII.get(LE_S_I64));
|
|
|
|
Inverted = true;
|
|
|
|
break;
|
|
|
|
case GE_S_I64:
|
|
|
|
Def->setDesc(TII.get(LT_S_I64));
|
|
|
|
Inverted = true;
|
|
|
|
break;
|
|
|
|
case LT_S_I64:
|
|
|
|
Def->setDesc(TII.get(GE_S_I64));
|
|
|
|
Inverted = true;
|
|
|
|
break;
|
|
|
|
case LE_S_I64:
|
|
|
|
Def->setDesc(TII.get(GT_S_I64));
|
|
|
|
Inverted = true;
|
|
|
|
break;
|
|
|
|
case GT_U_I64:
|
|
|
|
Def->setDesc(TII.get(LE_U_I64));
|
|
|
|
Inverted = true;
|
|
|
|
break;
|
|
|
|
case GE_U_I64:
|
|
|
|
Def->setDesc(TII.get(LT_U_I64));
|
|
|
|
Inverted = true;
|
|
|
|
break;
|
|
|
|
case LT_U_I64:
|
|
|
|
Def->setDesc(TII.get(GE_U_I64));
|
|
|
|
Inverted = true;
|
|
|
|
break;
|
|
|
|
case LE_U_I64:
|
|
|
|
Def->setDesc(TII.get(GT_U_I64));
|
|
|
|
Inverted = true;
|
|
|
|
break;
|
|
|
|
case EQ_F32:
|
|
|
|
Def->setDesc(TII.get(NE_F32));
|
|
|
|
Inverted = true;
|
|
|
|
break;
|
|
|
|
case NE_F32:
|
|
|
|
Def->setDesc(TII.get(EQ_F32));
|
|
|
|
Inverted = true;
|
|
|
|
break;
|
|
|
|
case EQ_F64:
|
|
|
|
Def->setDesc(TII.get(NE_F64));
|
|
|
|
Inverted = true;
|
|
|
|
break;
|
|
|
|
case NE_F64:
|
|
|
|
Def->setDesc(TII.get(EQ_F64));
|
|
|
|
Inverted = true;
|
|
|
|
break;
|
2017-11-30 04:20:11 +08:00
|
|
|
case EQZ_I32: {
|
|
|
|
// Invert an eqz by replacing it with its operand.
|
|
|
|
Cond = Def->getOperand(1).getReg();
|
|
|
|
Def->eraseFromParent();
|
|
|
|
Inverted = true;
|
|
|
|
break;
|
|
|
|
}
|
2018-09-05 09:27:38 +08:00
|
|
|
default:
|
|
|
|
break;
|
2015-12-05 11:03:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we weren't able to invert the condition in place. Insert an
|
2016-10-04 06:43:53 +08:00
|
|
|
// instruction to invert it.
|
2015-12-05 11:03:35 +08:00
|
|
|
if (!Inverted) {
|
[webassembly] Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Reviewers: aheejin
Subscribers: jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision for whole review: https://reviews.llvm.org/D65962
llvm-svn: 368627
2019-08-13 06:40:45 +08:00
|
|
|
Register Tmp = MRI.createVirtualRegister(&WebAssembly::I32RegClass);
|
2016-05-17 02:59:34 +08:00
|
|
|
BuildMI(MBB, MI, MI->getDebugLoc(), TII.get(WebAssembly::EQZ_I32), Tmp)
|
|
|
|
.addReg(Cond);
|
2016-10-25 03:49:43 +08:00
|
|
|
MFI.stackifyVReg(Tmp);
|
2015-12-05 11:03:35 +08:00
|
|
|
Cond = Tmp;
|
|
|
|
Inverted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The br_unless condition has now been inverted. Insert a br_if and
|
|
|
|
// delete the br_unless.
|
|
|
|
assert(Inverted);
|
|
|
|
BuildMI(MBB, MI, MI->getDebugLoc(), TII.get(WebAssembly::BR_IF))
|
2017-01-13 17:58:52 +08:00
|
|
|
.add(MI->getOperand(0))
|
2016-02-09 05:50:13 +08:00
|
|
|
.addReg(Cond);
|
2015-12-05 11:03:35 +08:00
|
|
|
MBB.erase(MI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|