xref: /freebsd/contrib/llvm-project/llvm/lib/Target/WebAssembly/WebAssemblyMCLowerPrePass.cpp (revision fe6060f10f634930ff71b7c50291ddc610da2475)
1*fe6060f1SDimitry Andric //===-- WebAssemblyMCLowerPrePass.cpp - Prepare for MC lower --------------===//
2*fe6060f1SDimitry Andric //
3*fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*fe6060f1SDimitry Andric //
7*fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
8*fe6060f1SDimitry Andric ///
9*fe6060f1SDimitry Andric /// \file
10*fe6060f1SDimitry Andric /// Some information in MC lowering / asm printing gets generated as
11*fe6060f1SDimitry Andric /// instructions get emitted, but may be necessary at the start, such as for
12*fe6060f1SDimitry Andric /// .globaltype declarations. This pass collects this information.
13*fe6060f1SDimitry Andric ///
14*fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
15*fe6060f1SDimitry Andric 
16*fe6060f1SDimitry Andric #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
17*fe6060f1SDimitry Andric #include "Utils/WebAssemblyUtilities.h"
18*fe6060f1SDimitry Andric #include "WebAssembly.h"
19*fe6060f1SDimitry Andric #include "WebAssemblyMachineFunctionInfo.h"
20*fe6060f1SDimitry Andric #include "WebAssemblySubtarget.h"
21*fe6060f1SDimitry Andric #include "llvm/ADT/SCCIterator.h"
22*fe6060f1SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
23*fe6060f1SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
24*fe6060f1SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
25*fe6060f1SDimitry Andric #include "llvm/CodeGen/MachineLoopInfo.h"
26*fe6060f1SDimitry Andric #include "llvm/CodeGen/MachineModuleInfoImpls.h"
27*fe6060f1SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
28*fe6060f1SDimitry Andric #include "llvm/CodeGen/Passes.h"
29*fe6060f1SDimitry Andric #include "llvm/Support/Debug.h"
30*fe6060f1SDimitry Andric #include "llvm/Support/raw_ostream.h"
31*fe6060f1SDimitry Andric using namespace llvm;
32*fe6060f1SDimitry Andric 
33*fe6060f1SDimitry Andric #define DEBUG_TYPE "wasm-mclower-prepass"
34*fe6060f1SDimitry Andric 
35*fe6060f1SDimitry Andric namespace {
36*fe6060f1SDimitry Andric class WebAssemblyMCLowerPrePass final : public MachineFunctionPass {
37*fe6060f1SDimitry Andric   StringRef getPassName() const override {
38*fe6060f1SDimitry Andric     return "WebAssembly MC Lower Pre Pass";
39*fe6060f1SDimitry Andric   }
40*fe6060f1SDimitry Andric 
41*fe6060f1SDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override {
42*fe6060f1SDimitry Andric     AU.setPreservesCFG();
43*fe6060f1SDimitry Andric     MachineFunctionPass::getAnalysisUsage(AU);
44*fe6060f1SDimitry Andric   }
45*fe6060f1SDimitry Andric 
46*fe6060f1SDimitry Andric   bool runOnMachineFunction(MachineFunction &MF) override;
47*fe6060f1SDimitry Andric 
48*fe6060f1SDimitry Andric public:
49*fe6060f1SDimitry Andric   static char ID; // Pass identification, replacement for typeid
50*fe6060f1SDimitry Andric   WebAssemblyMCLowerPrePass() : MachineFunctionPass(ID) {}
51*fe6060f1SDimitry Andric };
52*fe6060f1SDimitry Andric } // end anonymous namespace
53*fe6060f1SDimitry Andric 
54*fe6060f1SDimitry Andric char WebAssemblyMCLowerPrePass::ID = 0;
55*fe6060f1SDimitry Andric INITIALIZE_PASS(
56*fe6060f1SDimitry Andric     WebAssemblyMCLowerPrePass, DEBUG_TYPE,
57*fe6060f1SDimitry Andric     "Collects information ahead of time for MC lowering",
58*fe6060f1SDimitry Andric     false, false)
59*fe6060f1SDimitry Andric 
60*fe6060f1SDimitry Andric FunctionPass *llvm::createWebAssemblyMCLowerPrePass() {
61*fe6060f1SDimitry Andric   return new WebAssemblyMCLowerPrePass();
62*fe6060f1SDimitry Andric }
63*fe6060f1SDimitry Andric 
64*fe6060f1SDimitry Andric bool WebAssemblyMCLowerPrePass::runOnMachineFunction(MachineFunction &MF) {
65*fe6060f1SDimitry Andric   LLVM_DEBUG(dbgs() << "********** MC Lower Pre Pass **********\n"
66*fe6060f1SDimitry Andric                        "********** Function: "
67*fe6060f1SDimitry Andric                     << MF.getName() << '\n');
68*fe6060f1SDimitry Andric 
69*fe6060f1SDimitry Andric   MachineModuleInfo &MMI = MF.getMMI();
70*fe6060f1SDimitry Andric   MachineModuleInfoWasm &MMIW = MMI.getObjFileInfo<MachineModuleInfoWasm>();
71*fe6060f1SDimitry Andric 
72*fe6060f1SDimitry Andric   for (MachineBasicBlock &MBB : MF) {
73*fe6060f1SDimitry Andric     for (auto &MI : MBB) {
74*fe6060f1SDimitry Andric       // FIXME: what should all be filtered out beyond these?
75*fe6060f1SDimitry Andric       if (MI.isDebugInstr() || MI.isInlineAsm())
76*fe6060f1SDimitry Andric         continue;
77*fe6060f1SDimitry Andric       for (MachineOperand &MO : MI.uses()) {
78*fe6060f1SDimitry Andric         if (MO.isSymbol()) {
79*fe6060f1SDimitry Andric           MMIW.MachineSymbolsUsed.insert(MO.getSymbolName());
80*fe6060f1SDimitry Andric         }
81*fe6060f1SDimitry Andric       }
82*fe6060f1SDimitry Andric     }
83*fe6060f1SDimitry Andric   }
84*fe6060f1SDimitry Andric 
85*fe6060f1SDimitry Andric   return true;
86*fe6060f1SDimitry Andric }
87