[llvm-dev] Constants propagation into printf format string

Davide Italiano via llvm-dev llvm-dev at lists.llvm.org
Thu May 17 17:27:44 PDT 2018


On Thu, May 17, 2018 at 5:10 PM, Friedman, Eli <efriedma at codeaurora.org> wrote:
> On 5/17/2018 4:04 PM, Davide Italiano via llvm-dev wrote:
>>
>> On Thu, May 17, 2018 at 3:53 PM, Dávid Bolvanský via llvm-dev
>> <llvm-dev at lists.llvm.org> wrote:
>>>
>>> Hello,
>>>
>>> I was thinking about a new possible simplification in InstCombine which
>>> would transform code like this:
>>>
>>> const char * NAME = "Prog";
>>>
>>> void printer(char **arr, int len) {
>>>    for (int i = 0; i < len; ++i) {
>>>      printf("%s: %s", NAME, arr[i]);
>>>    }
>>> }
>>>
>>> into
>>>
>>> void printer(char **arr, int len) {
>>>    for (int i = 0; i < len; ++i) {
>>>      printf("Prog: %s", arr[i]);
>>>    }
>>> }
>>>
>>> This transformation would take constant strings/integers/chars and put
>>> them
>>> to the format string.
>>>
>> This is not a peephole optimization, therefore it doesn't belong to
>> instcombine.
>> If you want to try to implement something like this, I'd recommend
>> taking a look at ConstantFolding or SCCP (and try to understand why
>> the value doesn't get propagated).
>
>
> What?  Transforming `printf("FOO: %s", "BAR")` to `printf("FOO: BAR")` looks
> like a peephole to me.
>

Apologies, I read the example incorrectly. I think you can probably
peephole easy cases like this.

--
Davide


More information about the llvm-dev mailing list