Index: llvm/lib/Transforms/IPO/FunctionSpecialization.cpp =================================================================== --- llvm/lib/Transforms/IPO/FunctionSpecialization.cpp +++ llvm/lib/Transforms/IPO/FunctionSpecialization.cpp @@ -64,6 +64,11 @@ cl::desc("Average loop iteration count cost"), cl::init(10)); +static cl::opt SpecializeConstGlobals( + "function-specialization-const-globals", cl::init(true), cl::Hidden, + cl::desc("Enable function specialization for constant globals and function " + "pointers")); + // Helper to check if \p LV is either overdefined or a constant int. static bool isOverdefined(const ValueLatticeElement &LV) { return !LV.isUnknownOrUndef() && !LV.isConstant(); @@ -485,8 +490,9 @@ continue; } - // Add the constant to the set. - if (auto *C = dyn_cast(CS.getArgOperand(A->getArgNo()))) + // Add the constant global or function pointer to the set. + auto *C = dyn_cast(CS.getArgOperand(A->getArgNo())); + if (C && SpecializeConstGlobals) Constants.push_back(C); } Index: llvm/test/Transforms/FunctionSpecialization/function-specialization3.ll =================================================================== --- llvm/test/Transforms/FunctionSpecialization/function-specialization3.ll +++ llvm/test/Transforms/FunctionSpecialization/function-specialization3.ll @@ -1,6 +1,8 @@ ; RUN: opt -function-specialization -func-specialization-avg-iters-cost=3 -S < %s | \ ; RUN: FileCheck %s --check-prefixes=COMMON,DISABLED -; RUN: opt -function-specialization -force-function-specialization -S < %s | \ +; RUN: opt -function-specialization -force-function-specialization -function-specialization-const-globals=0 -S < %s | \ +; RUN: FileCheck %s --check-prefixes=COMMON,DISABLED +; RUN: opt -function-specialization -force-function-specialization -function-specialization-const-globals=1 -S < %s | \ ; RUN: FileCheck %s --check-prefixes=COMMON,FORCE ; RUN: opt -function-specialization -func-specialization-avg-iters-cost=3 -force-function-specialization -S < %s | \ ; RUN: FileCheck %s --check-prefixes=COMMON,FORCE