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