LLVM 22.0.0git
WebAssemblyCleanCodeAfterTrap.cpp
Go to the documentation of this file.
1//===-- WebAssemblyCleanCodeAfterTrap.cpp - Clean Code After Trap ---------===//
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/// This file remove instruction after trap.
11/// ``llvm.trap`` will be convert as ``unreachable`` which is terminator.
12/// Instruction after terminator will cause validation failed.
13///
14//===----------------------------------------------------------------------===//
15
16#include "WebAssembly.h"
20#include "llvm/CodeGen/Passes.h"
21#include "llvm/MC/MCInstrDesc.h"
22#include "llvm/Support/Debug.h"
24using namespace llvm;
25
26#define DEBUG_TYPE "wasm-clean-code-after-trap"
27
28namespace {
29class WebAssemblyCleanCodeAfterTrap final : public MachineFunctionPass {
30public:
31 static char ID; // Pass identification, replacement for typeid
32 WebAssemblyCleanCodeAfterTrap() : MachineFunctionPass(ID) {}
33
34 StringRef getPassName() const override {
35 return "WebAssembly Clean Code After Trap";
36 }
37
38 bool runOnMachineFunction(MachineFunction &MF) override;
39};
40} // end anonymous namespace
41
42char WebAssemblyCleanCodeAfterTrap::ID = 0;
43INITIALIZE_PASS(WebAssemblyCleanCodeAfterTrap, DEBUG_TYPE,
44 "WebAssembly Clean Code After Trap", false, false)
45
47 return new WebAssemblyCleanCodeAfterTrap();
48}
49
50bool WebAssemblyCleanCodeAfterTrap::runOnMachineFunction(MachineFunction &MF) {
52 dbgs() << "********** CleanCodeAfterTrap **********\n"
53 << "********** Function: " << MF.getName() << '\n';
54 });
55
56 bool Changed = false;
57
58 for (MachineBasicBlock &BB : MF) {
59 bool HasTerminator = false;
61 for (MachineInstr &MI : BB) {
62 if (HasTerminator)
63 RemoveMI.push_back(&MI);
64 if (MI.hasProperty(MCID::Trap) && MI.isTerminator())
65 HasTerminator = true;
66 }
67 if (!RemoveMI.empty()) {
68 Changed = true;
70 for (MachineInstr *MI : RemoveMI) {
71 llvm::dbgs() << "* remove ";
73 }
74 });
75 for (MachineInstr *MI : RemoveMI)
76 MI->eraseFromParent();
77 }
78 }
79 return Changed;
80}
#define DEBUG_TYPE
IRTranslator LLVM IR MI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
This file defines the SmallVector class.
#define LLVM_DEBUG(...)
Definition Debug.h:114
This file contains the declaration of the WebAssembly-specific utility functions.
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end.
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
virtual void print(raw_ostream &OS, const Module *M) const
print - Print out the internal state of the pass.
Definition Pass.cpp:140
void push_back(const T &Elt)
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
Changed
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
FunctionPass * createWebAssemblyCleanCodeAfterTrap()