LLVM 19.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, StringRef TargetAttrs = "");
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 StringRef TargetAttrs) {
51 assert(Name.starts_with(getDerived().getThunkPrefix()) &&
52 "Created a thunk with an unexpected prefix!");
54 Module &M = const_cast<Module &>(*MMI.getModule());
55 LLVMContext &Ctx = M.getContext();
56 auto Type = FunctionType::get(Type::getVoidTy(Ctx), false);
60 Name, &M);
61 if (Comdat) {
62 F->setVisibility(GlobalValue::HiddenVisibility);
63 F->setComdat(M.getOrInsertComdat(Name));
64 }
66 // Add Attributes so that we don't create a frame, unwind information, or
67 // inline.
68 AttrBuilder B(Ctx);
69 B.addAttribute(llvm::Attribute::NoUnwind);
70 B.addAttribute(llvm::Attribute::Naked);
71 if (TargetAttrs != "")
72 B.addAttribute("target-features", TargetAttrs);
73 F->addFnAttrs(B);
75 // Populate our function a bit so that we can verify.
76 BasicBlock *Entry = BasicBlock::Create(Ctx, "entry", F);
77 IRBuilder<> Builder(Entry);
79 Builder.CreateRetVoid();
81 // MachineFunctions aren't created automatically for the IR-level constructs
82 // we already made. Create them and insert them into the module.
84 // A MachineBasicBlock must not be created for the Entry block; code
85 // generation from an empty naked function in C source code also does not
86 // generate one. At least GlobalISel asserts if this invariant isn't
87 // respected.
89 // Set MF properties. We never use vregs...
93template <typename Derived, typename InsertedThunksTy>
95 MachineFunction &MF) {
96 // If MF is not a thunk, check to see if we need to insert a thunk.
97 if (!MF.getName().starts_with(getDerived().getThunkPrefix())) {
98 // Only add a thunk if one of the functions has the corresponding feature
99 // enabled in its subtarget, and doesn't enable external thunks. The target
100 // can use InsertedThunks to detect whether relevant thunks have already
101 // been inserted.
102 // FIXME: Conditionalize on indirect calls so we don't emit a thunk when
103 // nothing will end up calling it.
104 // FIXME: It's a little silly to look at every function just to enumerate
105 // the subtargets, but eventually we'll want to look at them for indirect
106 // calls, so maybe this is OK.
107 if (!getDerived().mayUseThunk(MF, InsertedThunks))
108 return false;
110 InsertedThunks |= getDerived().insertThunks(MMI, MF);
111 return true;
112 }
114 // If this *is* a thunk function, we need to populate it with the correct MI.
115 getDerived().populateThunk(MF);
116 return true;
119} // namespace llvm
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:60
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:199
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:164
@ HiddenVisibility
The GV is hidden.
Definition: GlobalValue.h:67
@ InternalLinkage
Rename collisions when linking (static functions).
Definition: GlobalValue.h:58
@ LinkOnceODRLinkage
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:54
ReturnInst * CreateRetVoid()
Create a 'ret void' instruction.
Definition: IRBuilder.h:1090
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2666
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 starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:258
void createThunkFunction(MachineModuleInfo &MMI, StringRef Name, bool Comdat=true, StringRef TargetAttrs="")
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