IndirectThunks.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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, typename InsertedThunksTy = bool>
  26. class ThunkInserter {
  27. Derived &getDerived() { return *static_cast<Derived *>(this); }
  28. protected:
  29. // A variable used to track whether (and possible which) thunks have been
  30. // inserted so far. InsertedThunksTy is usually a bool, but can be other types
  31. // to represent more than one type of thunk. Requires an |= operator to
  32. // accumulate results.
  33. InsertedThunksTy InsertedThunks;
  34. void doInitialization(Module &M) {}
  35. void createThunkFunction(MachineModuleInfo &MMI, StringRef Name,
  36. bool Comdat = true);
  37. public:
  38. void init(Module &M) {
  39. InsertedThunks = InsertedThunksTy{};
  40. getDerived().doInitialization(M);
  41. }
  42. // return `true` if `MMI` or `MF` was modified
  43. bool run(MachineModuleInfo &MMI, MachineFunction &MF);
  44. };
  45. template <typename Derived, typename InsertedThunksTy>
  46. void ThunkInserter<Derived, InsertedThunksTy>::createThunkFunction(
  47. MachineModuleInfo &MMI, StringRef Name, bool Comdat) {
  48. assert(Name.startswith(getDerived().getThunkPrefix()) &&
  49. "Created a thunk with an unexpected prefix!");
  50. Module &M = const_cast<Module &>(*MMI.getModule());
  51. LLVMContext &Ctx = M.getContext();
  52. auto Type = FunctionType::get(Type::getVoidTy(Ctx), false);
  53. Function *F = Function::Create(Type,
  54. Comdat ? GlobalValue::LinkOnceODRLinkage
  55. : GlobalValue::InternalLinkage,
  56. Name, &M);
  57. if (Comdat) {
  58. F->setVisibility(GlobalValue::HiddenVisibility);
  59. F->setComdat(M.getOrInsertComdat(Name));
  60. }
  61. // Add Attributes so that we don't create a frame, unwind information, or
  62. // inline.
  63. AttrBuilder B(Ctx);
  64. B.addAttribute(llvm::Attribute::NoUnwind);
  65. B.addAttribute(llvm::Attribute::Naked);
  66. F->addFnAttrs(B);
  67. // Populate our function a bit so that we can verify.
  68. BasicBlock *Entry = BasicBlock::Create(Ctx, "entry", F);
  69. IRBuilder<> Builder(Entry);
  70. Builder.CreateRetVoid();
  71. // MachineFunctions aren't created automatically for the IR-level constructs
  72. // we already made. Create them and insert them into the module.
  73. MachineFunction &MF = MMI.getOrCreateMachineFunction(*F);
  74. // A MachineBasicBlock must not be created for the Entry block; code
  75. // generation from an empty naked function in C source code also does not
  76. // generate one. At least GlobalISel asserts if this invariant isn't
  77. // respected.
  78. // Set MF properties. We never use vregs...
  79. MF.getProperties().set(MachineFunctionProperties::Property::NoVRegs);
  80. }
  81. template <typename Derived, typename InsertedThunksTy>
  82. bool ThunkInserter<Derived, InsertedThunksTy>::run(MachineModuleInfo &MMI,
  83. MachineFunction &MF) {
  84. // If MF is not a thunk, check to see if we need to insert a thunk.
  85. if (!MF.getName().startswith(getDerived().getThunkPrefix())) {
  86. // Only add a thunk if one of the functions has the corresponding feature
  87. // enabled in its subtarget, and doesn't enable external thunks. The target
  88. // can use InsertedThunks to detect whether relevant thunks have already
  89. // been inserted.
  90. // FIXME: Conditionalize on indirect calls so we don't emit a thunk when
  91. // nothing will end up calling it.
  92. // FIXME: It's a little silly to look at every function just to enumerate
  93. // the subtargets, but eventually we'll want to look at them for indirect
  94. // calls, so maybe this is OK.
  95. if (!getDerived().mayUseThunk(MF, InsertedThunks))
  96. return false;
  97. InsertedThunks |= getDerived().insertThunks(MMI, MF);
  98. return true;
  99. }
  100. // If this *is* a thunk function, we need to populate it with the correct MI.
  101. getDerived().populateThunk(MF);
  102. return true;
  103. }
  104. } // namespace llvm
  105. #endif
  106. #ifdef __GNUC__
  107. #pragma GCC diagnostic pop
  108. #endif