Path: blob/main/contrib/llvm-project/llvm/lib/Target/WebAssembly/WebAssemblyCleanCodeAfterTrap.cpp
35266 views
//===-- WebAssemblyCleanCodeAfterTrap.cpp - Clean Code After Trap ---------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7///8/// \file9/// This file remove instruction after trap.10/// ``llvm.trap`` will be convert as ``unreachable`` which is terminator.11/// Instruction after terminator will cause validation failed.12///13//===----------------------------------------------------------------------===//1415#include "WebAssembly.h"16#include "WebAssemblyUtilities.h"17#include "llvm/ADT/SmallVector.h"18#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"19#include "llvm/CodeGen/Passes.h"20#include "llvm/MC/MCInstrDesc.h"21#include "llvm/Support/Debug.h"22#include "llvm/Support/raw_ostream.h"23using namespace llvm;2425#define DEBUG_TYPE "wasm-clean-code-after-trap"2627namespace {28class WebAssemblyCleanCodeAfterTrap final : public MachineFunctionPass {29public:30static char ID; // Pass identification, replacement for typeid31WebAssemblyCleanCodeAfterTrap() : MachineFunctionPass(ID) {}3233StringRef getPassName() const override {34return "WebAssembly Clean Code After Trap";35}3637bool runOnMachineFunction(MachineFunction &MF) override;38};39} // end anonymous namespace4041char WebAssemblyCleanCodeAfterTrap::ID = 0;42INITIALIZE_PASS(WebAssemblyCleanCodeAfterTrap, DEBUG_TYPE,43"WebAssembly Clean Code After Trap", false, false)4445FunctionPass *llvm::createWebAssemblyCleanCodeAfterTrap() {46return new WebAssemblyCleanCodeAfterTrap();47}4849bool WebAssemblyCleanCodeAfterTrap::runOnMachineFunction(MachineFunction &MF) {50LLVM_DEBUG({51dbgs() << "********** CleanCodeAfterTrap **********\n"52<< "********** Function: " << MF.getName() << '\n';53});5455bool Changed = false;5657for (MachineBasicBlock &BB : MF) {58bool HasTerminator = false;59llvm::SmallVector<MachineInstr *> RemoveMI{};60for (MachineInstr &MI : BB) {61if (HasTerminator)62RemoveMI.push_back(&MI);63if (MI.hasProperty(MCID::Trap) && MI.isTerminator())64HasTerminator = true;65}66if (!RemoveMI.empty()) {67Changed = true;68LLVM_DEBUG({69for (MachineInstr *MI : RemoveMI) {70llvm::dbgs() << "* remove ";71MI->print(llvm::dbgs());72}73});74for (MachineInstr *MI : RemoveMI)75MI->eraseFromParent();76}77}78return Changed;79}808182