[gn build] Port 077cc3deeebe
[llvm-project.git] / llvm / lib / Target / WebAssembly / WebAssemblyMCLowerPrePass.cpp
blob3f73ca6b564eebb8f5db46241aa1390aa3313ca2
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 //===----------------------------------------------------------------------===//
16 #include "WebAssembly.h"
17 #include "WebAssemblyUtilities.h"
18 #include "llvm/CodeGen/MachineFrameInfo.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
21 #include "llvm/CodeGen/Passes.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/raw_ostream.h"
25 using namespace llvm;
27 #define DEBUG_TYPE "wasm-mclower-prepass"
29 namespace {
30 class WebAssemblyMCLowerPrePass final : public ModulePass {
31 StringRef getPassName() const override {
32 return "WebAssembly MC Lower Pre Pass";
35 void getAnalysisUsage(AnalysisUsage &AU) const override {
36 AU.setPreservesCFG();
37 ModulePass::getAnalysisUsage(AU);
40 bool runOnModule(Module &M) override;
42 public:
43 static char ID; // Pass identification, replacement for typeid
44 WebAssemblyMCLowerPrePass() : ModulePass(ID) {}
46 } // end anonymous namespace
48 char WebAssemblyMCLowerPrePass::ID = 0;
49 INITIALIZE_PASS(
50 WebAssemblyMCLowerPrePass, DEBUG_TYPE,
51 "Collects information ahead of time for MC lowering",
52 false, false)
54 ModulePass *llvm::createWebAssemblyMCLowerPrePass() {
55 return new WebAssemblyMCLowerPrePass();
58 // NOTE: this is a ModulePass since we need to enforce that this code has run
59 // for all functions before AsmPrinter. If this way of doing things is ever
60 // suboptimal, we could opt to make it a MachineFunctionPass and instead use
61 // something like createBarrierNoopPass() to enforce ordering.
63 // The information stored here is essential for emitExternalDecls in the Wasm
64 // AsmPrinter
65 bool WebAssemblyMCLowerPrePass::runOnModule(Module &M) {
66 auto *MMIWP = getAnalysisIfAvailable<MachineModuleInfoWrapperPass>();
67 if (!MMIWP)
68 return true;
70 MachineModuleInfo &MMI = MMIWP->getMMI();
71 MachineModuleInfoWasm &MMIW = MMI.getObjFileInfo<MachineModuleInfoWasm>();
73 for (Function &F : M) {
74 MachineFunction *MF = MMI.getMachineFunction(F);
75 if (!MF)
76 continue;
78 LLVM_DEBUG(dbgs() << "********** MC Lower Pre Pass **********\n"
79 "********** Function: "
80 << MF->getName() << '\n');
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());
95 return true;