How does llvm::LibCallSimplifier optimize call instruction

hello! I am reading the source of a project built on llvm, and there is a use of LibCallSimplifier::optimizeCall(Instruction*), I simply read the annotation of this function and the class it belong to, but I still don’t understand what this class and this function do exactly with the instruction.

In short, what exactly does Libcall mean here, or how distinguish between libcalls and other calls, and how does the so-called “optimize” work? I can’t understand why transform “printf” to “puts”(in class annotation)…

thanks for your help!

LibCall refers to a call to a library function as described in the comment. Only a subset of functions are recognized by the class with printf being one of them. The printf to puts example replacement perhaps isn’t the most revealing (it also isn’t entirely accurate but that’s beside the point). The idea behind it is that calls to printf are slow because the function must parse the format string looking for formatting directives, while calls to puts are faster because they just print the string (followed by a newline).

An example simplification that might make the benefits more obvious is the substitution of constants for calls such as folding strlen("123") to 3, or substituting the expression *s == '\0' for strlen(s) == 0. The replacements are much preferable because they avoid any calls at all and also lend themselves to further optimization.

To learn more about what the class does read its implementation in llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp, or look at some of the tests in the llvm/test/Transforms/InstCombine subdirectory.