xref: /freebsd/contrib/llvm-project/llvm/lib/Target/WebAssembly/WebAssemblyMCLowerPrePass.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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 "MCTargetDesc/WebAssemblyMCTargetDesc.h"
17 #include "WebAssembly.h"
18 #include "WebAssemblyMachineFunctionInfo.h"
19 #include "WebAssemblySubtarget.h"
20 #include "WebAssemblyUtilities.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/IR/Module.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/raw_ostream.h"
32 using namespace llvm;
33 
34 #define DEBUG_TYPE "wasm-mclower-prepass"
35 
36 namespace {
37 class WebAssemblyMCLowerPrePass final : public ModulePass {
getPassName() const38   StringRef getPassName() const override {
39     return "WebAssembly MC Lower Pre Pass";
40   }
41 
getAnalysisUsage(AnalysisUsage & AU) const42   void getAnalysisUsage(AnalysisUsage &AU) const override {
43     AU.setPreservesCFG();
44     ModulePass::getAnalysisUsage(AU);
45   }
46 
47   bool runOnModule(Module &M) override;
48 
49 public:
50   static char ID; // Pass identification, replacement for typeid
WebAssemblyMCLowerPrePass()51   WebAssemblyMCLowerPrePass() : ModulePass(ID) {}
52 };
53 } // end anonymous namespace
54 
55 char WebAssemblyMCLowerPrePass::ID = 0;
56 INITIALIZE_PASS(
57     WebAssemblyMCLowerPrePass, DEBUG_TYPE,
58     "Collects information ahead of time for MC lowering",
59     false, false)
60 
createWebAssemblyMCLowerPrePass()61 ModulePass *llvm::createWebAssemblyMCLowerPrePass() {
62   return new WebAssemblyMCLowerPrePass();
63 }
64 
65 // NOTE: this is a ModulePass since we need to enforce that this code has run
66 // for all functions before AsmPrinter. If this way of doing things is ever
67 // suboptimal, we could opt to make it a MachineFunctionPass and instead use
68 // something like createBarrierNoopPass() to enforce ordering.
69 //
70 // The information stored here is essential for emitExternalDecls in the Wasm
71 // AsmPrinter
runOnModule(Module & M)72 bool WebAssemblyMCLowerPrePass::runOnModule(Module &M) {
73   auto *MMIWP = getAnalysisIfAvailable<MachineModuleInfoWrapperPass>();
74   if (!MMIWP)
75     return true;
76 
77   MachineModuleInfo &MMI = MMIWP->getMMI();
78   MachineModuleInfoWasm &MMIW = MMI.getObjFileInfo<MachineModuleInfoWasm>();
79 
80   for (Function &F : M) {
81     MachineFunction *MF = MMI.getMachineFunction(F);
82     if (!MF)
83       continue;
84 
85     LLVM_DEBUG(dbgs() << "********** MC Lower Pre Pass **********\n"
86                          "********** Function: "
87                       << MF->getName() << '\n');
88 
89     for (MachineBasicBlock &MBB : *MF) {
90       for (auto &MI : MBB) {
91         // FIXME: what should all be filtered out beyond these?
92         if (MI.isDebugInstr() || MI.isInlineAsm())
93           continue;
94         for (MachineOperand &MO : MI.uses()) {
95           if (MO.isSymbol()) {
96             MMIW.MachineSymbolsUsed.insert(MO.getSymbolName());
97           }
98         }
99       }
100     }
101   }
102   return true;
103 }
104