LLVM 24.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"
22#include "llvm/CodeGen/Passes.h"
23#include "llvm/IR/Analysis.h"
24#include "llvm/MC/MCInstrDesc.h"
25#include "llvm/Support/Debug.h"
27using namespace llvm;
28
29#define DEBUG_TYPE "wasm-clean-code-after-trap"
30
31namespace {
32class WebAssemblyCleanCodeAfterTrapLegacy final : public MachineFunctionPass {
33public:
34 static char ID; // Pass identification, replacement for typeid
35 WebAssemblyCleanCodeAfterTrapLegacy() : MachineFunctionPass(ID) {}
36
37 StringRef getPassName() const override {
38 return "WebAssembly Clean Code After Trap";
39 }
40
41 bool runOnMachineFunction(MachineFunction &MF) override;
42};
43} // end anonymous namespace
44
45char WebAssemblyCleanCodeAfterTrapLegacy::ID = 0;
46INITIALIZE_PASS(WebAssemblyCleanCodeAfterTrapLegacy, DEBUG_TYPE,
47 "WebAssembly Clean Code After Trap", false, false)
48
50 return new WebAssemblyCleanCodeAfterTrapLegacy();
51}
52
55 dbgs() << "********** CleanCodeAfterTrap **********\n"
56 << "********** Function: " << MF.getName() << '\n';
57 });
58
59 bool Changed = false;
60
61 for (MachineBasicBlock &BB : MF) {
62 bool HasTerminator = false;
64 for (MachineInstr &MI : BB) {
65 if (HasTerminator)
66 RemoveMI.push_back(&MI);
67 if (MI.hasProperty(MCID::Trap) && MI.isTerminator())
68 HasTerminator = true;
69 }
70 if (!RemoveMI.empty()) {
71 Changed = true;
73 for (MachineInstr *MI : RemoveMI) {
74 llvm::dbgs() << "* remove ";
75 MI->print(llvm::dbgs());
76 }
77 });
78 for (MachineInstr *MI : RemoveMI)
79 MI->eraseFromParent();
80 }
81 }
82 return Changed;
83}
84
85bool WebAssemblyCleanCodeAfterTrapLegacy::runOnMachineFunction(
86 MachineFunction &MF) {
87 return cleanCodeAfterTrap(MF);
88}
89
90PreservedAnalyses
#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:119
static bool cleanCodeAfterTrap(MachineFunction &MF)
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.
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
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.
Representation of each machine instruction.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
Changed
Pass manager infrastructure for declaring and invalidating analyses.
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.
FunctionPass * createWebAssemblyCleanCodeAfterTrapLegacyPass()
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209