LLVM 19.0.0git
Public Member Functions | Protected Member Functions | List of all members
llvm::ThunkInserter< Derived, InsertedThunksTy > Class Template Reference

This class assists in inserting MI thunk functions into the module and rewriting the existing machine functions to call these thunks. More...

#include "llvm/CodeGen/IndirectThunks.h"

Public Member Functions

void init (Module &M)
 
bool run (MachineModuleInfo &MMI, MachineFunction &MF)
 

Protected Member Functions

void createThunkFunction (MachineModuleInfo &MMI, StringRef Name, bool Comdat=true, StringRef TargetAttrs="")
 Create an empty thunk function.
 
void doInitialization (Module &M)
 Initializes thunk inserter.
 
const chargetThunkPrefix ()
 Returns common prefix for thunk function's names.
 
bool mayUseThunk (const MachineFunction &MF)
 Checks if MF may use thunks (true - maybe, false - definitely not).
 
InsertedThunksTy insertThunks (MachineModuleInfo &MMI, MachineFunction &MF, InsertedThunksTy ExistingThunks)
 Rewrites the function if necessary, returns the set of thunks added.
 
void populateThunk (MachineFunction &MF)
 Populate the thunk function with instructions.
 

Detailed Description

template<typename Derived, typename InsertedThunksTy = bool>
class llvm::ThunkInserter< Derived, InsertedThunksTy >

This class assists in inserting MI thunk functions into the module and rewriting the existing machine functions to call these thunks.

One of the common cases is implementing security mitigations that involve replacing some machine code patterns with calls to special thunk functions.

Inserting a module pass late in the codegen pipeline may increase memory usage, as it serializes the transformations and forces preceding passes to produce machine code for all functions before running the module pass. For that reason, ThunkInserter can be driven by a MachineFunctionPass by passing one MachineFunction at a time to its run(MMI, MF) method. Then, the derived class should

Note that if some other pass is responsible for rewriting the functions, the insertThunks method may simply create all possible thunks at once, probably postponed until the first occurrence of possibly affected MF.

Alternatively, insertThunks method can rewrite MF by itself and only insert the thunks being called. In that case InsertedThunks variable can be used to track which thunks were already inserted.

In any case, the thunk function has to be inserted on behalf of some other function and then populated on its own "iteration" later - this is because MachineFunctionPass will see the newly created functions, but they first have to go through the preceding passes from the same pass manager, possibly even through the instruction selector.

Definition at line 61 of file IndirectThunks.h.

Member Function Documentation

◆ createThunkFunction()

template<typename Derived , typename InsertedThunksTy >
void llvm::ThunkInserter< Derived, InsertedThunksTy >::createThunkFunction ( MachineModuleInfo MMI,
StringRef  Name,
bool  Comdat = true,
StringRef  TargetAttrs = "" 
)
protected

◆ doInitialization()

template<typename Derived , typename InsertedThunksTy = bool>
void llvm::ThunkInserter< Derived, InsertedThunksTy >::doInitialization ( Module M)
inlineprotected

Initializes thunk inserter.

Definition at line 87 of file IndirectThunks.h.

◆ getThunkPrefix()

template<typename Derived , typename InsertedThunksTy = bool>
const char * llvm::ThunkInserter< Derived, InsertedThunksTy >::getThunkPrefix ( )
protected

Returns common prefix for thunk function's names.

◆ init()

template<typename Derived , typename InsertedThunksTy = bool>
void llvm::ThunkInserter< Derived, InsertedThunksTy >::init ( Module M)
inline

Definition at line 110 of file IndirectThunks.h.

◆ insertThunks()

template<typename Derived , typename InsertedThunksTy = bool>
InsertedThunksTy llvm::ThunkInserter< Derived, InsertedThunksTy >::insertThunks ( MachineModuleInfo MMI,
MachineFunction MF,
InsertedThunksTy  ExistingThunks 
)
protected

Rewrites the function if necessary, returns the set of thunks added.

◆ mayUseThunk()

template<typename Derived , typename InsertedThunksTy = bool>
bool llvm::ThunkInserter< Derived, InsertedThunksTy >::mayUseThunk ( const MachineFunction MF)
protected

Checks if MF may use thunks (true - maybe, false - definitely not).

◆ populateThunk()

template<typename Derived , typename InsertedThunksTy = bool>
void llvm::ThunkInserter< Derived, InsertedThunksTy >::populateThunk ( MachineFunction MF)
protected

Populate the thunk function with instructions.

If multiple thunks are created, the content that must be inserted in the thunk function body should be derived from the MF's name.

Depending on the preceding passes in the pass manager, by the time populateThunk is called, MF may have a few target-specific instructions (such as a single MBB containing the return instruction).

◆ run()

template<typename Derived , typename InsertedThunksTy >
bool llvm::ThunkInserter< Derived, InsertedThunksTy >::run ( MachineModuleInfo MMI,
MachineFunction MF 
)

The documentation for this class was generated from the following file: