LLVM 24.0.0git
WebAssemblyMCLowerPrePass.cpp
Go to the documentation of this file.
1//===-- WebAssemblyMCLowerPrePass.cpp - Prepare for MC lower --------------===//
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/// Some information in MC lowering / asm printing gets generated as
11/// instructions get emitted, but may be necessary at the start, such as for
12/// .globaltype declarations. This pass collects this information.
13///
14//===----------------------------------------------------------------------===//
15
16#include "WebAssembly.h"
24#include "llvm/CodeGen/Passes.h"
25#include "llvm/IR/Analysis.h"
26#include "llvm/IR/Module.h"
27#include "llvm/IR/PassManager.h"
28#include "llvm/Support/Debug.h"
30using namespace llvm;
31
32#define DEBUG_TYPE "wasm-mclower-prepass"
33
34namespace {
35class WebAssemblyMCLowerPreLegacy final : public ModulePass {
36 StringRef getPassName() const override {
37 return "WebAssembly MC Lower Pre Pass";
38 }
39
40 void getAnalysisUsage(AnalysisUsage &AU) const override {
41 AU.setPreservesAll();
43 }
44
45 bool runOnModule(Module &M) override;
46
47public:
48 static char ID; // Pass identification, replacement for typeid
49 WebAssemblyMCLowerPreLegacy() : ModulePass(ID) {}
50};
51} // end anonymous namespace
52
53char WebAssemblyMCLowerPreLegacy::ID = 0;
54INITIALIZE_PASS(WebAssemblyMCLowerPreLegacy, DEBUG_TYPE,
55 "Collects information ahead of time for MC lowering", false,
56 false)
57
59 return new WebAssemblyMCLowerPreLegacy();
60}
61
62// NOTE: this is a ModulePass since we need to enforce that this code has run
63// for all functions before AsmPrinter. If this way of doing things is ever
64// suboptimal, we could opt to make it a MachineFunctionPass and instead use
65// something like createBarrierNoopPass() to enforce ordering.
66//
67// The information stored here is essential for emitExternalDecls in the Wasm
68// AsmPrinter
69static void mcLower(Module &M, MachineModuleInfo &MMI,
72
73 for (Function &F : M) {
74 MachineFunction *MF = GetMF(&F);
75 if (!MF)
76 continue;
77
78 LLVM_DEBUG(dbgs() << "********** MC Lower Pre Pass **********\n"
79 "********** Function: "
80 << MF->getName() << '\n');
81
82 for (MachineBasicBlock &MBB : *MF) {
83 for (auto &MI : MBB) {
84 // FIXME: what should all be filtered out beyond these?
85 if (MI.isDebugInstr() || MI.isInlineAsm())
86 continue;
87 for (MachineOperand &MO : MI.uses()) {
88 if (MO.isSymbol()) {
89 MMIW.MachineSymbolsUsed.insert(MO.getSymbolName());
90 }
91 }
92 }
93 }
94 }
95}
96
97bool WebAssemblyMCLowerPreLegacy::runOnModule(Module &M) {
98 auto *MMIWP = getAnalysisIfAvailable<MachineModuleInfoWrapperPass>();
99 if (!MMIWP)
100 return false;
101 MachineModuleInfo &MMI = MMIWP->getMMI();
102 mcLower(M, MMI, [MMIWP](Function *F) {
103 return MMIWP->getMMI().getMachineFunction(*F);
104 });
105 return false;
106}
107
110 MachineModuleInfo &MMI = MAM.getResult<MachineModuleAnalysis>(M).getMMI();
111 mcLower(M, MMI, [&](Function *F) {
114 .getManager()
115 .getCachedResult<MachineFunctionAnalysis>(*F);
116 return MFA ? &MFA->getMF() : nullptr;
117 });
118 return PreservedAnalyses::all();
119}
MachineBasicBlock & MBB
#define DEBUG_TYPE
IRTranslator LLVM IR MI
Module.h This file contains the declarations for the Module class.
This header defines various interfaces for pass management in LLVM.
#define F(x, y, z)
Definition MD5.cpp:54
ModuleAnalysisManager MAM
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
#define LLVM_DEBUG(...)
Definition Debug.h:119
static void mcLower(Module &M, MachineModuleInfo &MMI, llvm::function_ref< MachineFunction *(Function *)> GetMF)
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.
Represent the analysis usage information of a pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
This analysis create MachineFunction for given Function.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
An analysis that produces MachineModuleInfo for a module.
MachineModuleInfoWasm - This is a MachineModuleInfoImpl implementation for Wasm targets.
SetVector< StringRef > MachineSymbolsUsed
This class contains meta information specific to a module.
Ty & getObjFileInfo()
Keep track of various per-module pieces of information for backends that would like to do so.
MachineOperand class - Representation of each machine instruction operand.
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition Pass.h:255
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition Pass.cpp:112
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition SetVector.h:157
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM)
An efficient, type-erasing, non-owning reference to a callable.
Pass manager infrastructure for declaring and invalidating analyses.
This is an optimization pass for GlobalISel generic memory operations.
ModulePass * createWebAssemblyMCLowerPreLegacyPass()
InnerAnalysisManagerProxy< FunctionAnalysisManager, Module > FunctionAnalysisManagerModuleProxy
Provide the FunctionAnalysisManager to Module proxy.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39