| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 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> class ThunkInserter { |
| 25 |
Derived &getDerived() { return *static_cast<Derived *>(this); } |
| 26 |
|
| 27 |
protected: |
| 28 |
bool InsertedThunks; |
| 29 |
void doInitialization(Module &M) {} |
| 30 |
void createThunkFunction(MachineModuleInfo &MMI, StringRef Name, |
| 31 |
bool Comdat = true); |
| 32 |
|
| 33 |
public: |
| 34 |
void init(Module &M) { |
| 35 |
InsertedThunks = false; |
| 36 |
getDerived().doInitialization(M); |
| 37 |
} |
| 38 |
|
| 39 |
bool run(MachineModuleInfo &MMI, MachineFunction &MF); |
| 40 |
}; |
| 41 |
|
| 42 |
template <typename Derived> |
| 43 |
void ThunkInserter<Derived>::createThunkFunction(MachineModuleInfo &MMI, |
| 44 |
StringRef Name, bool Comdat) { |
| 45 |
assert(Name.startswith(getDerived().getThunkPrefix()) && |
| 46 |
"Created a thunk with an unexpected prefix!"); |
| 47 |
|
| 48 |
Module &M = const_cast<Module &>(*MMI.getModule()); |
| 49 |
LLVMContext &Ctx = M.getContext(); |
| 50 |
auto Type = FunctionType::get(Type::getVoidTy(Ctx), false); |
| 51 |
Function *F = Function::Create(Type, |
| 52 |
Comdat ? GlobalValue::LinkOnceODRLinkage |
| 53 |
: GlobalValue::InternalLinkage, |
| 54 |
Name, &M); |
| 55 |
if (Comdat) { |
| 56 |
F->setVisibility(GlobalValue::HiddenVisibility); |
| 57 |
F->setComdat(M.getOrInsertComdat(Name)); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
AttrBuilder B(Ctx); |
| 63 |
B.addAttribute(llvm::Attribute::NoUnwind); |
| 64 |
B.addAttribute(llvm::Attribute::Naked); |
| 65 |
F->addFnAttrs(B); |
| 66 |
|
| 67 |
|
| 68 |
BasicBlock *Entry = BasicBlock::Create(Ctx, "entry", F); |
| 69 |
IRBuilder<> Builder(Entry); |
| 70 |
|
| 71 |
Builder.CreateRetVoid(); |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
MachineFunction &MF = MMI.getOrCreateMachineFunction(*F); |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
MF.getProperties().set(MachineFunctionProperties::Property::NoVRegs); |
| 83 |
} |
| 84 |
|
| 85 |
template <typename Derived> |
| 86 |
bool ThunkInserter<Derived>::run(MachineModuleInfo &MMI, MachineFunction &MF) { |
| 87 |
|
| 88 |
if (!MF.getName().startswith(getDerived().getThunkPrefix())) { |
| 89 |
|
| 90 |
if (InsertedThunks) |
| 91 |
return false; |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
if (!getDerived().mayUseThunk(MF)) |
| 101 |
return false; |
| 102 |
|
| 103 |
getDerived().insertThunks(MMI); |
| 104 |
InsertedThunks = true; |
| 105 |
return true; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
getDerived().populateThunk(MF); |
| 110 |
return true; |
| 111 |
} |
| 112 |
|
| 113 |
} |
| 114 |
|
| 115 |
#endif |