LLVM 19.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 LLVM_DEBUG(X)
Definition: Debug.h:101
IRTranslator LLVM IR MI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
This file defines the SmallVector class.
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:311
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
Representation of each machine instruction.
Definition: MachineInstr.h:69
virtual void print(raw_ostream &OS, const Module *M) const
print - Print out the internal state of the pass.
Definition: Pass.cpp:130
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
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.
Definition: AddressRanges.h:18
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
FunctionPass * createWebAssemblyCleanCodeAfterTrap()