IndirectThunks.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===---- IndirectThunks.h - Indirect Thunk Base Class ----------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. ///
  14. /// \file
  15. /// Contains a base class for Passes that inject an MI thunk.
  16. ///
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_CODEGEN_INDIRECTTHUNKS_H
  19. #define LLVM_CODEGEN_INDIRECTTHUNKS_H
  20. #include "llvm/CodeGen/MachineFunction.h"
  21. #include "llvm/CodeGen/MachineModuleInfo.h"
  22. #include "llvm/IR/IRBuilder.h"
  23. #include "llvm/IR/Module.h"
  24. namespace llvm {
  25. template <typename Derived> class ThunkInserter {
  26. Derived &getDerived() { return *static_cast<Derived *>(this); }
  27. protected:
  28. bool InsertedThunks;
  29. void doInitialization(Module &M) {}
  30. void createThunkFunction(MachineModuleInfo &MMI, StringRef Name,
  31. bool Comdat = true);
  32. public:
  33. void init(Module &M) {
  34. InsertedThunks = false;
  35. getDerived().doInitialization(M);
  36. }
  37. // return `true` if `MMI` or `MF` was modified
  38. bool run(MachineModuleInfo &MMI, MachineFunction &MF);
  39. };
  40. template <typename Derived>
  41. void ThunkInserter<Derived>::createThunkFunction(MachineModuleInfo &MMI,
  42. StringRef Name, bool Comdat) {
  43. assert(Name.startswith(getDerived().getThunkPrefix()) &&
  44. "Created a thunk with an unexpected prefix!");
  45. Module &M = const_cast<Module &>(*MMI.getModule());
  46. LLVMContext &Ctx = M.getContext();
  47. auto Type = FunctionType::get(Type::getVoidTy(Ctx), false);
  48. Function *F = Function::Create(Type,
  49. Comdat ? GlobalValue::LinkOnceODRLinkage
  50. : GlobalValue::InternalLinkage,
  51. Name, &M);
  52. if (Comdat) {
  53. F->setVisibility(GlobalValue::HiddenVisibility);
  54. F->setComdat(M.getOrInsertComdat(Name));
  55. }
  56. // Add Attributes so that we don't create a frame, unwind information, or
  57. // inline.
  58. AttrBuilder B(Ctx);
  59. B.addAttribute(llvm::Attribute::NoUnwind);
  60. B.addAttribute(llvm::Attribute::Naked);
  61. F->addFnAttrs(B);
  62. // Populate our function a bit so that we can verify.
  63. BasicBlock *Entry = BasicBlock::Create(Ctx, "entry", F);
  64. IRBuilder<> Builder(Entry);
  65. Builder.CreateRetVoid();
  66. // MachineFunctions aren't created automatically for the IR-level constructs
  67. // we already made. Create them and insert them into the module.
  68. MachineFunction &MF = MMI.getOrCreateMachineFunction(*F);
  69. // A MachineBasicBlock must not be created for the Entry block; code
  70. // generation from an empty naked function in C source code also does not
  71. // generate one. At least GlobalISel asserts if this invariant isn't
  72. // respected.
  73. // Set MF properties. We never use vregs...
  74. MF.getProperties().set(MachineFunctionProperties::Property::NoVRegs);
  75. }
  76. template <typename Derived>
  77. bool ThunkInserter<Derived>::run(MachineModuleInfo &MMI, MachineFunction &MF) {
  78. // If MF is not a thunk, check to see if we need to insert a thunk.
  79. if (!MF.getName().startswith(getDerived().getThunkPrefix())) {
  80. // If we've already inserted a thunk, nothing else to do.
  81. if (InsertedThunks)
  82. return false;
  83. // Only add a thunk if one of the functions has the corresponding feature
  84. // enabled in its subtarget, and doesn't enable external thunks.
  85. // FIXME: Conditionalize on indirect calls so we don't emit a thunk when
  86. // nothing will end up calling it.
  87. // FIXME: It's a little silly to look at every function just to enumerate
  88. // the subtargets, but eventually we'll want to look at them for indirect
  89. // calls, so maybe this is OK.
  90. if (!getDerived().mayUseThunk(MF))
  91. return false;
  92. getDerived().insertThunks(MMI);
  93. InsertedThunks = true;
  94. return true;
  95. }
  96. // If this *is* a thunk function, we need to populate it with the correct MI.
  97. getDerived().populateThunk(MF);
  98. return true;
  99. }
  100. } // namespace llvm
  101. #endif
  102. #ifdef __GNUC__
  103. #pragma GCC diagnostic pop
  104. #endif