Index: llvm/lib/Transforms/IPO/FunctionSpecialization.cpp =================================================================== --- llvm/lib/Transforms/IPO/FunctionSpecialization.cpp +++ llvm/lib/Transforms/IPO/FunctionSpecialization.cpp @@ -618,7 +618,8 @@ // If the argument isn't overdefined, there's nothing to do. It should // already be constant. - if (!Solver.getLatticeValueFor(A).isOverdefined()) { + if (!EnableSpecializationForLiteralConstant && + !Solver.getLatticeValueFor(A).isOverdefined()) { LLVM_DEBUG(dbgs() << "FnSpecialization: nothing to do, arg is already " << "constant?\n"); return false; Index: llvm/test/Transforms/FunctionSpecialization/function-specialization-constant-integers1.ll =================================================================== --- /dev/null +++ llvm/test/Transforms/FunctionSpecialization/function-specialization-constant-integers1.ll @@ -0,0 +1,48 @@ +; RUN: opt -function-specialization -function-specialization-for-literal-constant=true -force-function-specialization -S < %s | FileCheck %s + +; Check that the literal constant parameter could be specialized. +; CHECK: @f( +; CHECK: @f.1( + +@.str = private unnamed_addr constant [3 x i8] c"%d\00", align 1 + +define dso_local i32 @main(i32 %argc, i8** %argv) { +entry: + br label %for.cond + +for.cond: ; preds = %for.body, %entry + %i.0 = phi i32 [ 0, %entry ], [ %inc, %for.body ] + %cmp = icmp slt i32 %i.0, 100 + br i1 %cmp, label %for.body, label %for.end + +for.body: ; preds = %for.cond + %call = call i32 @f(i32 0, i32 %i.0) + %call1 = call i32 @f(i32 %i.0, i32 13) + %inc = add nsw i32 %i.0, 1 + br label %for.cond + +for.end: ; preds = %for.cond + ret i32 0 +} + +define internal i32 @f(i32 %a, i32 %x) { +entry: + %cmp = icmp sgt i32 %a, 1 + br i1 %cmp, label %if.then, label %if.else + +if.then: ; preds = %entry + %mul = mul nsw i32 %a, %x + %call = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %mul) + br label %if.end + +if.else: ; preds = %entry + %sub = sub nsw i32 %a, %x + %call1 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %sub) + br label %if.end + +if.end: ; preds = %if.else, %if.then + ret i32 undef +} + +declare dso_local noundef i32 @printf(i8* nocapture noundef readonly, ...) +