<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/85450>85450</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Add printf folding when the arguments are constant
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            enhancement,
            missed-optimization
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          michaelrj-google
      </td>
    </tr>
</table>

<pre>
    Existing printf optimizations are very narrow, only handling format strings with no conversions or a single, narrowly defined conversion (%c or %s). This leads to missed optimizations when the printf formatting could be done at compile time, such as for integer conversions or multiple strings.

example: https://godbolt.org/z/e498c1Mhx

My suggestion for how to solve this problem is to check if the printf format string is valid and matches the arguments (similar to what is done for the `-Wformat` warning), then check if the arguments are known at compile time. If both conditions are met, then it should be possible to just run sprintf at compile time and replace the printf call in the generated code with a puts (or fputs) call on a constant string.

cc: @nickdesaulniers @efriedma-quic 
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJxkVMFyqzgQ_BpxmbILZEPMgUP2ZV21h3fbqj0LaUCTCInVCDt-X78liF8S78V2GXWru6cHxUyjR-xE_YeoXwq1JBtiN5G2Cl183Y0hjA6LPphb9-c7cSI_whzJpwHCnGiiXypR8AwqIlww3sCrGMNVyB8QvLuBVd64jBpCnFQCTpH8yHClZMEH0MFfMPLKESIoYPKjw4zfmNwNDA7k0Xw5C0KehKx1hghZs5DtHv62xOBQGYYUYCJmNA8qrxY9JIt3D5uo1ZUOizPQI5jgEVQCHaaZHEKiaZXDi7agOGOAfMIR46P6aXGJZod3l3tRvojyefvEdzXNDsXhGWxKM4vDs5BnIc9jMH1waR_iKOT5l5BnPLYnXf2071_xP2_AyzgiZyurChuu2SkHd0FI2f0cQ-9wAloj0Bb1G9Dwf8cfAvO5i3JkQHkDk0raIq-nVRyXCX3inDTTRE7FTHm1KmXUGlLWkA-Lptz9sxGLpoSrip78KGSbY0s58m9KPrlzad58uPrHvPfw1wB9SDYnbOizYhOm36SUgO19anNgpj7DA7wunCAuHvjD9AP76jbi7JTGr9lo5RzQVpARPUaV1tYZ3OqqYF62REKEIf8Wst1QwYPKWjkpf0_32_S1zoMXx9KTfjPIanGeMHL-C4dIaCa1-3chDYXpDqY9tKrArnqqylq27eFQ2K6qn07tUFWVOjT9U1WfqqZUzbFvmlYeB3MqqJOlPJaHqq7aSlbl_nDST01ZV5VUzVPf1vmuSZHbO3eZct8KYl6wO9XHuiyc6tHx-i6QEr1VXmOek5BSyB9Cym2ldl9XKj-rX4rYZcZdv4wsjqUjTvx5R6LksHs25rODzuT2_d7G7424x1gs0XUPq0LJLv1eh0nIc77g42s3x_CKOgl5Xh2xkOfV1H8BAAD__5cRrcs">