LLVM 17.0.0git
Go to the documentation of this file.
1//===---- IndirectThunks.h - Indirect Thunk Base Class ----------*- C++ -*-===//
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
9/// \file
10/// Contains a base class for Passes that inject an MI thunk.
19#include "llvm/IR/IRBuilder.h"
20#include "llvm/IR/Module.h"
22namespace llvm {
24template <typename Derived, typename InsertedThunksTy = bool>
26 Derived &getDerived() { return *static_cast<Derived *>(this); }
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;
36 bool Comdat = true);
39 void init(Module &M) {
40 InsertedThunks = InsertedThunksTy{};
41 getDerived().doInitialization(M);
42 }
43 // return `true` if `MMI` or `MF` was modified
47template <typename Derived, typename InsertedThunksTy>
50 assert(Name.startswith(getDerived().getThunkPrefix()) &&
51 "Created a thunk with an unexpected prefix!");
53 Module &M = const_cast<Module &>(*MMI.getModule());
54 LLVMContext &Ctx = M.getContext();
55 auto Type = FunctionType::get(Type::getVoidTy(Ctx), false);
59 Name, &M);
60 if (Comdat) {
61 F->setVisibility(GlobalValue::HiddenVisibility);
62 F->setComdat(M.getOrInsertComdat(Name));
63 }
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);
72 // Populate our function a bit so that we can verify.
73 BasicBlock *Entry = BasicBlock::Create(Ctx, "entry", F);
74 IRBuilder<> Builder(Entry);
76 Builder.CreateRetVoid();
78 // MachineFunctions aren't created automatically for the IR-level constructs
79 // we already made. Create them and insert them into the module.
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.
86 // Set MF properties. We never use vregs...
90template <typename Derived, typename InsertedThunksTy>
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;
107 InsertedThunks |= getDerived().insertThunks(MMI, MF);
108 return true;
109 }
111 // If this *is* a thunk function, we need to populate it with the correct MI.
112 getDerived().populateThunk(MF);
113 return true;
116} // namespace llvm
assume Assume Builder
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
std::string Name
#define F(x, y, z)
Definition: MD5.cpp:55
Module.h This file contains the declarations for the Module class.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Basic Block Representation.
Definition: BasicBlock.h:56
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:105
static FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
Definition: Function.h:136
@ HiddenVisibility
The GV is hidden.
Definition: GlobalValue.h:64
@ InternalLinkage
Rename collisions when linking (static functions).
Definition: GlobalValue.h:55
@ LinkOnceODRLinkage
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:51
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2558
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
MachineFunctionProperties & set(Property P)
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
const MachineFunctionProperties & getProperties() const
Get the function properties.
This class contains meta information specific to a module.
const Module * getModule() const
MachineFunction & getOrCreateMachineFunction(Function &F)
Returns the MachineFunction constructed for the IR function F.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
bool startswith(StringRef Prefix) const
Definition: StringRef.h:261
void createThunkFunction(MachineModuleInfo &MMI, StringRef Name, bool Comdat=true)
bool run(MachineModuleInfo &MMI, MachineFunction &MF)
void doInitialization(Module &M)
void init(Module &M)
InsertedThunksTy InsertedThunks
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
static Type * getVoidTy(LLVMContext &C)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18