1 //===-- WebAssemblyMCLowerPrePass.cpp - Prepare for MC lower --------------===//
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
7 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
17 #include "Utils/WebAssemblyUtilities.h"
18 #include "WebAssembly.h"
19 #include "WebAssemblyMachineFunctionInfo.h"
20 #include "WebAssemblySubtarget.h"
21 #include "llvm/ADT/SCCIterator.h"
22 #include "llvm/CodeGen/MachineFrameInfo.h"
23 #include "llvm/CodeGen/MachineFunction.h"
24 #include "llvm/CodeGen/MachineInstrBuilder.h"
25 #include "llvm/CodeGen/MachineLoopInfo.h"
26 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
27 #include "llvm/CodeGen/MachineRegisterInfo.h"
28 #include "llvm/CodeGen/Passes.h"
29 #include "llvm/Support/Debug.h"
30 #include "llvm/Support/raw_ostream.h"
33 #define DEBUG_TYPE "wasm-mclower-prepass"
36 class WebAssemblyMCLowerPrePass final
: public ModulePass
{
37 StringRef
getPassName() const override
{
38 return "WebAssembly MC Lower Pre Pass";
41 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
43 ModulePass::getAnalysisUsage(AU
);
46 bool runOnModule(Module
&M
) override
;
49 static char ID
; // Pass identification, replacement for typeid
50 WebAssemblyMCLowerPrePass() : ModulePass(ID
) {}
52 } // end anonymous namespace
54 char WebAssemblyMCLowerPrePass::ID
= 0;
56 WebAssemblyMCLowerPrePass
, DEBUG_TYPE
,
57 "Collects information ahead of time for MC lowering",
60 ModulePass
*llvm::createWebAssemblyMCLowerPrePass() {
61 return new WebAssemblyMCLowerPrePass();
64 // NOTE: this is a ModulePass since we need to enforce that this code has run
65 // for all functions before AsmPrinter. If this way of doing things is ever
66 // suboptimal, we could opt to make it a MachineFunctionPass and instead use
67 // something like createBarrierNoopPass() to enforce ordering.
69 // The information stored here is essential for emitExternalDecls in the Wasm
71 bool WebAssemblyMCLowerPrePass::runOnModule(Module
&M
) {
72 auto *MMIWP
= getAnalysisIfAvailable
<MachineModuleInfoWrapperPass
>();
76 MachineModuleInfo
&MMI
= MMIWP
->getMMI();
77 MachineModuleInfoWasm
&MMIW
= MMI
.getObjFileInfo
<MachineModuleInfoWasm
>();
79 for (Function
&F
: M
) {
80 MachineFunction
*MF
= MMI
.getMachineFunction(F
);
84 LLVM_DEBUG(dbgs() << "********** MC Lower Pre Pass **********\n"
85 "********** Function: "
86 << MF
->getName() << '\n');
88 for (MachineBasicBlock
&MBB
: *MF
) {
89 for (auto &MI
: MBB
) {
90 // FIXME: what should all be filtered out beyond these?
91 if (MI
.isDebugInstr() || MI
.isInlineAsm())
93 for (MachineOperand
&MO
: MI
.uses()) {
95 MMIW
.MachineSymbolsUsed
.insert(MO
.getSymbolName());