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"
17#include "WebAssemblyUtilities.h"
18#include "llvm/CodeGen/MachineFrameInfo.h"
19#include "llvm/CodeGen/MachineFunction.h"
20#include "llvm/CodeGen/MachineFunctionAnalysis.h"
21#include "llvm/CodeGen/MachineModuleInfo.h"
22#include "llvm/CodeGen/MachineModuleInfoImpls.h"
23#include "llvm/CodeGen/MachinePassManager.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"
29#include "llvm/Support/raw_ostream.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();
42 ModulePass::getAnalysisUsage(AU);
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
58ModulePass *llvm::createWebAssemblyMCLowerPreLegacyPass() {
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,
70 llvm::function_ref<MachineFunction *(Function *)> GetMF) {
71 MachineModuleInfoWasm &MMIW = MMI.getObjFileInfo<MachineModuleInfoWasm>();
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(X: 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, GetMF: [MMIWP](Function *F) {
103 return MMIWP->getMMI().getMachineFunction(F: *F);
104 });
105 return false;
106}
107
108PreservedAnalyses WebAssemblyMCLowerPrePass::run(Module &M,
109 ModuleAnalysisManager &MAM) {
110 MachineModuleInfo &MMI = MAM.getResult<MachineModuleAnalysis>(IR&: M).getMMI();
111 mcLower(M, MMI, GetMF: [&](Function *F) {
112 MachineFunctionAnalysis::Result *MFA =
113 MAM.getResult<FunctionAnalysisManagerModuleProxy>(IR&: M)
114 .getManager()
115 .getCachedResult<MachineFunctionAnalysis>(IR&: *F);
116 return MFA ? &MFA->getMF() : nullptr;
117 });
118 return PreservedAnalyses::all();
119}
120