MachineModuleInfo.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. //===-- llvm/CodeGen/MachineModuleInfo.cpp ----------------------*- C++ -*-===//
  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. #include "llvm/CodeGen/MachineModuleInfo.h"
  9. #include "llvm/ADT/DenseMap.h"
  10. #include "llvm/ADT/StringRef.h"
  11. #include "llvm/CodeGen/MachineFunction.h"
  12. #include "llvm/CodeGen/Passes.h"
  13. #include "llvm/IR/Constants.h"
  14. #include "llvm/IR/DiagnosticInfo.h"
  15. #include "llvm/IR/LLVMContext.h"
  16. #include "llvm/IR/Module.h"
  17. #include "llvm/InitializePasses.h"
  18. #include "llvm/MC/MCContext.h"
  19. #include "llvm/Pass.h"
  20. #include "llvm/Support/CommandLine.h"
  21. #include "llvm/Support/ErrorHandling.h"
  22. #include "llvm/Target/TargetLoweringObjectFile.h"
  23. #include "llvm/Target/TargetMachine.h"
  24. #include <algorithm>
  25. #include <cassert>
  26. #include <memory>
  27. #include <utility>
  28. #include <vector>
  29. using namespace llvm;
  30. using namespace llvm::dwarf;
  31. static cl::opt<bool>
  32. DisableDebugInfoPrinting("disable-debug-info-print", cl::Hidden,
  33. cl::desc("Disable debug info printing"));
  34. // Out of line virtual method.
  35. MachineModuleInfoImpl::~MachineModuleInfoImpl() = default;
  36. void MachineModuleInfo::initialize() {
  37. ObjFileMMI = nullptr;
  38. CurCallSite = 0;
  39. NextFnNum = 0;
  40. UsesMSVCFloatingPoint = false;
  41. DbgInfoAvailable = false;
  42. }
  43. void MachineModuleInfo::finalize() {
  44. Context.reset();
  45. // We don't clear the ExternalContext.
  46. delete ObjFileMMI;
  47. ObjFileMMI = nullptr;
  48. }
  49. MachineModuleInfo::MachineModuleInfo(MachineModuleInfo &&MMI)
  50. : TM(std::move(MMI.TM)),
  51. Context(MMI.TM.getTargetTriple(), MMI.TM.getMCAsmInfo(),
  52. MMI.TM.getMCRegisterInfo(), MMI.TM.getMCSubtargetInfo(), nullptr,
  53. &MMI.TM.Options.MCOptions, false),
  54. MachineFunctions(std::move(MMI.MachineFunctions)) {
  55. Context.setObjectFileInfo(MMI.TM.getObjFileLowering());
  56. ObjFileMMI = MMI.ObjFileMMI;
  57. CurCallSite = MMI.CurCallSite;
  58. ExternalContext = MMI.ExternalContext;
  59. TheModule = MMI.TheModule;
  60. }
  61. MachineModuleInfo::MachineModuleInfo(const LLVMTargetMachine *TM)
  62. : TM(*TM), Context(TM->getTargetTriple(), TM->getMCAsmInfo(),
  63. TM->getMCRegisterInfo(), TM->getMCSubtargetInfo(),
  64. nullptr, &TM->Options.MCOptions, false) {
  65. Context.setObjectFileInfo(TM->getObjFileLowering());
  66. initialize();
  67. }
  68. MachineModuleInfo::MachineModuleInfo(const LLVMTargetMachine *TM,
  69. MCContext *ExtContext)
  70. : TM(*TM), Context(TM->getTargetTriple(), TM->getMCAsmInfo(),
  71. TM->getMCRegisterInfo(), TM->getMCSubtargetInfo(),
  72. nullptr, &TM->Options.MCOptions, false),
  73. ExternalContext(ExtContext) {
  74. Context.setObjectFileInfo(TM->getObjFileLowering());
  75. initialize();
  76. }
  77. MachineModuleInfo::~MachineModuleInfo() { finalize(); }
  78. MachineFunction *
  79. MachineModuleInfo::getMachineFunction(const Function &F) const {
  80. auto I = MachineFunctions.find(&F);
  81. return I != MachineFunctions.end() ? I->second.get() : nullptr;
  82. }
  83. MachineFunction &MachineModuleInfo::getOrCreateMachineFunction(Function &F) {
  84. // Shortcut for the common case where a sequence of MachineFunctionPasses
  85. // all query for the same Function.
  86. if (LastRequest == &F)
  87. return *LastResult;
  88. auto I = MachineFunctions.insert(
  89. std::make_pair(&F, std::unique_ptr<MachineFunction>()));
  90. MachineFunction *MF;
  91. if (I.second) {
  92. // No pre-existing machine function, create a new one.
  93. const TargetSubtargetInfo &STI = *TM.getSubtargetImpl(F);
  94. MF = new MachineFunction(F, TM, STI, NextFnNum++, *this);
  95. MF->initTargetMachineFunctionInfo(STI);
  96. // Update the set entry.
  97. I.first->second.reset(MF);
  98. } else {
  99. MF = I.first->second.get();
  100. }
  101. LastRequest = &F;
  102. LastResult = MF;
  103. return *MF;
  104. }
  105. void MachineModuleInfo::deleteMachineFunctionFor(Function &F) {
  106. MachineFunctions.erase(&F);
  107. LastRequest = nullptr;
  108. LastResult = nullptr;
  109. }
  110. void MachineModuleInfo::insertFunction(const Function &F,
  111. std::unique_ptr<MachineFunction> &&MF) {
  112. auto I = MachineFunctions.insert(std::make_pair(&F, std::move(MF)));
  113. assert(I.second && "machine function already mapped");
  114. (void)I;
  115. }
  116. namespace {
  117. /// This pass frees the MachineFunction object associated with a Function.
  118. class FreeMachineFunction : public FunctionPass {
  119. public:
  120. static char ID;
  121. FreeMachineFunction() : FunctionPass(ID) {}
  122. void getAnalysisUsage(AnalysisUsage &AU) const override {
  123. AU.addRequired<MachineModuleInfoWrapperPass>();
  124. AU.addPreserved<MachineModuleInfoWrapperPass>();
  125. }
  126. bool runOnFunction(Function &F) override {
  127. MachineModuleInfo &MMI =
  128. getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
  129. MMI.deleteMachineFunctionFor(F);
  130. return true;
  131. }
  132. StringRef getPassName() const override {
  133. return "Free MachineFunction";
  134. }
  135. };
  136. } // end anonymous namespace
  137. char FreeMachineFunction::ID;
  138. FunctionPass *llvm::createFreeMachineFunctionPass() {
  139. return new FreeMachineFunction();
  140. }
  141. MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass(
  142. const LLVMTargetMachine *TM)
  143. : ImmutablePass(ID), MMI(TM) {
  144. initializeMachineModuleInfoWrapperPassPass(*PassRegistry::getPassRegistry());
  145. }
  146. MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass(
  147. const LLVMTargetMachine *TM, MCContext *ExtContext)
  148. : ImmutablePass(ID), MMI(TM, ExtContext) {
  149. initializeMachineModuleInfoWrapperPassPass(*PassRegistry::getPassRegistry());
  150. }
  151. // Handle the Pass registration stuff necessary to use DataLayout's.
  152. INITIALIZE_PASS(MachineModuleInfoWrapperPass, "machinemoduleinfo",
  153. "Machine Module Information", false, false)
  154. char MachineModuleInfoWrapperPass::ID = 0;
  155. static unsigned getLocCookie(const SMDiagnostic &SMD, const SourceMgr &SrcMgr,
  156. std::vector<const MDNode *> &LocInfos) {
  157. // Look up a LocInfo for the buffer this diagnostic is coming from.
  158. unsigned BufNum = SrcMgr.FindBufferContainingLoc(SMD.getLoc());
  159. const MDNode *LocInfo = nullptr;
  160. if (BufNum > 0 && BufNum <= LocInfos.size())
  161. LocInfo = LocInfos[BufNum - 1];
  162. // If the inline asm had metadata associated with it, pull out a location
  163. // cookie corresponding to which line the error occurred on.
  164. unsigned LocCookie = 0;
  165. if (LocInfo) {
  166. unsigned ErrorLine = SMD.getLineNo() - 1;
  167. if (ErrorLine >= LocInfo->getNumOperands())
  168. ErrorLine = 0;
  169. if (LocInfo->getNumOperands() != 0)
  170. if (const ConstantInt *CI =
  171. mdconst::dyn_extract<ConstantInt>(LocInfo->getOperand(ErrorLine)))
  172. LocCookie = CI->getZExtValue();
  173. }
  174. return LocCookie;
  175. }
  176. bool MachineModuleInfoWrapperPass::doInitialization(Module &M) {
  177. MMI.initialize();
  178. MMI.TheModule = &M;
  179. // FIXME: Do this for new pass manager.
  180. LLVMContext &Ctx = M.getContext();
  181. MMI.getContext().setDiagnosticHandler(
  182. [&Ctx, &M](const SMDiagnostic &SMD, bool IsInlineAsm,
  183. const SourceMgr &SrcMgr,
  184. std::vector<const MDNode *> &LocInfos) {
  185. unsigned LocCookie = 0;
  186. if (IsInlineAsm)
  187. LocCookie = getLocCookie(SMD, SrcMgr, LocInfos);
  188. Ctx.diagnose(
  189. DiagnosticInfoSrcMgr(SMD, M.getName(), IsInlineAsm, LocCookie));
  190. });
  191. MMI.DbgInfoAvailable = !DisableDebugInfoPrinting &&
  192. !M.debug_compile_units().empty();
  193. return false;
  194. }
  195. bool MachineModuleInfoWrapperPass::doFinalization(Module &M) {
  196. MMI.finalize();
  197. return false;
  198. }
  199. AnalysisKey MachineModuleAnalysis::Key;
  200. MachineModuleInfo MachineModuleAnalysis::run(Module &M,
  201. ModuleAnalysisManager &) {
  202. MachineModuleInfo MMI(TM);
  203. MMI.TheModule = &M;
  204. MMI.DbgInfoAvailable = !DisableDebugInfoPrinting &&
  205. !M.debug_compile_units().empty();
  206. return MMI;
  207. }