LLVM  12.0.0git
IndirectThunks.h
Go to the documentation of this file.
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_INDIRECTTHUNKS_H
15 #define LLVM_INDIRECTTHUNKS_H
16 
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:
31 
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 
41 template <typename Derived>
43  StringRef Name) {
44  assert(Name.startswith(getDerived().getThunkPrefix()) &&
45  "Created a thunk with an unexpected prefix!");
46 
47  Module &M = const_cast<Module &>(*MMI.getModule());
48  LLVMContext &Ctx = M.getContext();
49  auto Type = FunctionType::get(Type::getVoidTy(Ctx), false);
50  Function *F =
53  F->setComdat(M.getOrInsertComdat(Name));
54 
55  // Add Attributes so that we don't create a frame, unwind information, or
56  // inline.
57  AttrBuilder B;
58  B.addAttribute(llvm::Attribute::NoUnwind);
59  B.addAttribute(llvm::Attribute::Naked);
61 
62  // Populate our function a bit so that we can verify.
63  BasicBlock *Entry = BasicBlock::Create(Ctx, "entry", F);
64  IRBuilder<> Builder(Entry);
65 
66  Builder.CreateRetVoid();
67 
68  // MachineFunctions aren't created automatically for the IR-level constructs
69  // we already made. Create them and insert them into the module.
71  // A MachineBasicBlock must not be created for the Entry block; code
72  // generation from an empty naked function in C source code also does not
73  // generate one. At least GlobalISel asserts if this invariant isn't
74  // respected.
75 
76  // Set MF properties. We never use vregs...
78 }
79 
80 template <typename Derived>
82  // If MF is not a thunk, check to see if we need to insert a thunk.
83  if (!MF.getName().startswith(getDerived().getThunkPrefix())) {
84  // If we've already inserted a thunk, nothing else to do.
85  if (InsertedThunks)
86  return false;
87 
88  // Only add a thunk if one of the functions has the corresponding feature
89  // enabled in its subtarget, and doesn't enable external thunks.
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))
96  return false;
97 
98  getDerived().insertThunks(MMI);
99  InsertedThunks = true;
100  return true;
101  }
102 
103  // If this *is* a thunk function, we need to populate it with the correct MI.
104  getDerived().populateThunk(MF);
105  return true;
106 }
107 
108 } // namespace llvm
109 
110 #endif
void setVisibility(VisibilityTypes V)
Definition: GlobalValue.h:235
This class represents lattice values for constants.
Definition: AllocatorList.h:23
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:67
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
void createThunkFunction(MachineModuleInfo &MMI, StringRef Name)
const MachineFunctionProperties & getProperties() const
Get the function properties.
LLVM_NODISCARD bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:289
F(f)
AttrBuilder & addAttribute(Attribute::AttrKind Val)
Add an attribute to the builder.
void init(Module &M)
bool run(MachineModuleInfo &MMI, MachineFunction &MF)
void setComdat(Comdat *C)
Definition: GlobalObject.h:128
void doInitialization(Module &M)
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
ReturnInst * CreateRetVoid()
Create a &#39;ret void&#39; instruction.
Definition: IRBuilder.h:928
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:51
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
Definition: Function.h:137
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
static Type * getVoidTy(LLVMContext &C)
Definition: Type.cpp:170
static FunctionType * get(Type *Result, ArrayRef< Type *> Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
Definition: Type.cpp:311
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:100
assume Assume Builder
Module.h This file contains the declarations for the Module class.
MachineFunctionProperties & set(Property P)
MachineFunction & getOrCreateMachineFunction(Function &F)
Returns the MachineFunction constructed for the IR function F.
const Module * getModule() const
void addAttributes(unsigned i, const AttrBuilder &Attrs)
adds the attributes to the list of attributes.
Definition: Function.cpp:452
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
This class contains meta information specific to a module.