IndirectThunks.h 4.1 KB

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