WebAssemblyMCLowerPrePass.cpp 3.4 KB

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