LLVM 22.0.0git
MergeFunctions.cpp File Reference
#include "llvm/Transforms/IPO/MergeFunctions.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/IR/Argument.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/DebugLoc.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/GlobalValue.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/StructuralHash.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Use.h"
#include "llvm/IR/User.h"
#include "llvm/IR/Value.h"
#include "llvm/IR/ValueHandle.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/IPO.h"
#include "llvm/Transforms/Utils/FunctionComparator.h"
#include "llvm/Transforms/Utils/ModuleUtils.h"
#include <algorithm>
#include <cassert>
#include <iterator>
#include <set>
#include <utility>
#include <vector>

Go to the source code of this file.

Macros

#define DEBUG_TYPE   "mergefunc"

Functions

 STATISTIC (NumFunctionsMerged, "Number of functions merged")
 STATISTIC (NumThunksWritten, "Number of thunks generated")
 STATISTIC (NumAliasesWritten, "Number of aliases generated")
 STATISTIC (NumDoubleWeak, "Number of new functions created")
static bool hasDistinctMetadataIntrinsic (const Function &F)
 Check whether F has an intrinsic which references distinct metadata as an operand.
static bool isEligibleForMerging (Function &F)
 Check whether F is eligible for function merging.
FunctionasPtr (Function *Fn)
FunctionasPtr (Function &Fn)
static bool canCreateThunkFor (Function *F)
 Whether this function may be replaced by a forwarding thunk.
static void copyMetadataIfPresent (Function *From, Function *To, StringRef Kind)
 Copy all metadata of a specific kind from one function to another.
static bool canCreateAliasFor (Function *F)
static bool isODR (const Function *F)
 Returns true if F is either weak_odr or linkonce_odr.
static bool isFuncOrderCorrect (const Function *F, const Function *G)

Variables

static cl::opt< unsignedNumFunctionsForVerificationCheck ("mergefunc-verify", cl::desc("How many functions in a module could be used for " "MergeFunctions to pass a basic correctness check. " "'0' disables this check. Works only with '-debug' key."), cl::init(0), cl::Hidden)
static cl::opt< boolMergeFunctionsPDI ("mergefunc-preserve-debug-info", cl::Hidden, cl::init(false), cl::desc("Preserve debug info in thunk when mergefunc " "transformations are made."))
static cl::opt< boolMergeFunctionsAliases ("mergefunc-use-aliases", cl::Hidden, cl::init(false), cl::desc("Allow mergefunc to create aliases"))

Macro Definition Documentation

◆ DEBUG_TYPE

#define DEBUG_TYPE   "mergefunc"

Definition at line 130 of file MergeFunctions.cpp.

Function Documentation

◆ asPtr() [1/2]

Function * asPtr ( Function & Fn)
inline

Definition at line 435 of file MergeFunctions.cpp.

◆ asPtr() [2/2]

Function * asPtr ( Function * Fn)
inline

Definition at line 434 of file MergeFunctions.cpp.

◆ canCreateAliasFor()

bool canCreateAliasFor ( Function * F)
static

Definition at line 813 of file MergeFunctions.cpp.

References assert(), F, and MergeFunctionsAliases.

◆ canCreateThunkFor()

bool canCreateThunkFor ( Function * F)
static

Whether this function may be replaced by a forwarding thunk.

Definition at line 696 of file MergeFunctions.cpp.

References llvm::dbgs(), F, and LLVM_DEBUG.

◆ copyMetadataIfPresent()

void copyMetadataIfPresent ( Function * From,
Function * To,
StringRef Kind )
static

Copy all metadata of a specific kind from one function to another.

Definition at line 713 of file MergeFunctions.cpp.

References llvm::GlobalObject::addMetadata(), and llvm::GlobalObject::getMetadata().

◆ hasDistinctMetadataIntrinsic()

bool hasDistinctMetadataIntrinsic ( const Function & F)
static

Check whether F has an intrinsic which references distinct metadata as an operand.

The most common instance of this would be CFI checks for function-local types.

Definition at line 409 of file MergeFunctions.cpp.

References llvm::dyn_cast(), F, I, llvm::isa(), and N.

Referenced by isEligibleForMerging().

◆ isEligibleForMerging()

bool isEligibleForMerging ( Function & F)
static

Check whether F is eligible for function merging.

Definition at line 429 of file MergeFunctions.cpp.

References F, and hasDistinctMetadataIntrinsic().

◆ isFuncOrderCorrect()

bool isFuncOrderCorrect ( const Function * F,
const Function * G )
static

Definition at line 981 of file MergeFunctions.cpp.

References F, G, and isODR().

◆ isODR()

bool isODR ( const Function * F)
static

Returns true if F is either weak_odr or linkonce_odr.

Definition at line 867 of file MergeFunctions.cpp.

References F.

Referenced by isFuncOrderCorrect().

◆ STATISTIC() [1/4]

STATISTIC ( NumAliasesWritten ,
"Number of aliases generated"  )

◆ STATISTIC() [2/4]

STATISTIC ( NumDoubleWeak ,
"Number of new functions created"  )

◆ STATISTIC() [3/4]

STATISTIC ( NumFunctionsMerged ,
"Number of functions merged"  )

◆ STATISTIC() [4/4]

STATISTIC ( NumThunksWritten ,
"Number of thunks generated"  )

Variable Documentation

◆ MergeFunctionsAliases

cl::opt< bool > MergeFunctionsAliases("mergefunc-use-aliases", cl::Hidden, cl::init(false), cl::desc("Allow mergefunc to create aliases")) ( "mergefunc-use-aliases" ,
cl::Hidden ,
cl::init(false) ,
cl::desc("Allow mergefunc to create aliases")  )
static

Referenced by canCreateAliasFor().

◆ MergeFunctionsPDI

cl::opt< bool > MergeFunctionsPDI("mergefunc-preserve-debug-info", cl::Hidden, cl::init(false), cl::desc("Preserve debug info in thunk when mergefunc " "transformations are made.")) ( "mergefunc-preserve-debug-info" ,
cl::Hidden ,
cl::init(false) ,
cl::desc("Preserve debug info in thunk when mergefunc " "transformations are made.")  )
static

◆ NumFunctionsForVerificationCheck

cl::opt< unsigned > NumFunctionsForVerificationCheck("mergefunc-verify", cl::desc("How many functions in a module could be used for " "MergeFunctions to pass a basic correctness check. " "'0' disables this check. Works only with '-debug' key."), cl::init(0), cl::Hidden) ( "mergefunc-verify" ,
cl::desc("How many functions in a module could be used for " "MergeFunctions to pass a basic correctness check. " "'0' disables this check. Works only with '-debug' key.") ,
cl::init(0) ,
cl::Hidden  )
static