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 "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"
31 using namespace llvm;
32 
33 #define DEBUG_TYPE "wasm-mclower-prepass"
34 
35 namespace {
36 class WebAssemblyMCLowerPrePass final : public MachineFunctionPass {
37   StringRef getPassName() const override {
38     return "WebAssembly MC Lower Pre Pass";
39   }
40 
41   void getAnalysisUsage(AnalysisUsage &AU) const override {
42     AU.setPreservesCFG();
43     MachineFunctionPass::getAnalysisUsage(AU);
44   }
45 
46   bool runOnMachineFunction(MachineFunction &MF) override;
47 
48 public:
49   static char ID; // Pass identification, replacement for typeid
50   WebAssemblyMCLowerPrePass() : MachineFunctionPass(ID) {}
51 };
52 } // end anonymous namespace
53 
54 char WebAssemblyMCLowerPrePass::ID = 0;
55 INITIALIZE_PASS(
56     WebAssemblyMCLowerPrePass, DEBUG_TYPE,
57     "Collects information ahead of time for MC lowering",
58     false, false)
59 
60 FunctionPass *llvm::createWebAssemblyMCLowerPrePass() {
61   return new WebAssemblyMCLowerPrePass();
62 }
63 
64 bool WebAssemblyMCLowerPrePass::runOnMachineFunction(MachineFunction &MF) {
65   LLVM_DEBUG(dbgs() << "********** MC Lower Pre Pass **********\n"
66                        "********** Function: "
67                     << MF.getName() << '\n');
68 
69   MachineModuleInfo &MMI = MF.getMMI();
70   MachineModuleInfoWasm &MMIW = MMI.getObjFileInfo<MachineModuleInfoWasm>();
71 
72   for (MachineBasicBlock &MBB : MF) {
73     for (auto &MI : MBB) {
74       // FIXME: what should all be filtered out beyond these?
75       if (MI.isDebugInstr() || MI.isInlineAsm())
76         continue;
77       for (MachineOperand &MO : MI.uses()) {
78         if (MO.isSymbol()) {
79           MMIW.MachineSymbolsUsed.insert(MO.getSymbolName());
80         }
81       }
82     }
83   }
84 
85   return true;
86 }
87