Index: llvm/lib/Transforms/IPO/FunctionSpecialization.cpp =================================================================== --- llvm/lib/Transforms/IPO/FunctionSpecialization.cpp +++ llvm/lib/Transforms/IPO/FunctionSpecialization.cpp @@ -471,17 +471,17 @@ } } - // Get the lattice value for the value the call site passes to the - // argument. If this value is not constant, move on to the next call - // site. Additionally, set the AllConstant flag to false. - if (V != A && !Solver.getLatticeValueFor(V).isConstant()) { - AllConstant = false; + // Add the constant to the set. + if (auto *C = dyn_cast(V)) { + Constants.push_back(C); continue; } - // Add the constant to the set. - if (auto *C = dyn_cast(CS.getArgOperand(A->getArgNo()))) - Constants.push_back(C); + // Get the lattice value for the value the call site passes to the + // argument. If this value is not constant, set the AllConstant flag to + // false. + if (!Solver.getLatticeValueFor(V).isConstant()) + AllConstant = false; } // If the argument can only take on constant values, AllConstant will be Index: llvm/test/Transforms/FunctionSpecialization/function-specialization-simple.ll =================================================================== --- /dev/null +++ llvm/test/Transforms/FunctionSpecialization/function-specialization-simple.ll @@ -0,0 +1,44 @@ +; RUN: opt -function-specialization -S < %s | FileCheck %s + +; Check that the direct constant parameter could be specialized. +; CHECK: @foo.1( +; CHECK: @foo.2( + +target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128" + +declare i32 @getValue() +declare i1 @getCond() + +define internal i32 @foo(i1 %break_cond) { +entry: + br label %loop.entry + +loop.entry: + br label %loop2.entry + +loop2.entry: + br label %loop2.body + +loop2.body: + %value = call i32 @getValue() + br i1 %break_cond, label %loop2.end, label %return + +loop2.end: + %cond.end = call i1 @getCond() + br i1 %cond.end, label %loop2.entry, label %loop.end + +loop.end: + %cond2.end = call i1 @getCond() + br i1 %cond2.end, label %loop.entry, label %return + +return: + ret i32 %value +} + +define dso_local i32 @bar(i32 %x, i32 %y) { +entry: + %retval.1 = call i32 @foo(i1 1) + %retval.2 = call i32 @foo(i1 0) + %retval = add nsw i32 %retval.1, %retval.2 + ret i32 %retval +} \ No newline at end of file