Page MenuHomePhabricator

[InstCombine] snprintf(NULL, 0, "%s", str) to strlen(str)
Needs ReviewPublic

Authored by xbolva00 on Sep 5 2021, 7:49 AM.

Details

Diff Detail

Event Timeline

xbolva00 created this revision.Sep 5 2021, 7:49 AM
xbolva00 requested review of this revision.Sep 5 2021, 7:49 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 5 2021, 7:49 AM
xbolva00 added inline comments.Sep 5 2021, 7:50 AM
llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
2597

snprintf returns int, strlen returns size_t so... trunc should be fine?

bjope added inline comments.Sep 15 2021, 10:29 AM
llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
2594

Is there a specific use-case for doing this (I guess a user could use strlen instead if that is what they want). Is it to save cycles due to strlen probably being faster, or because it is more likely that there are subsequent transformations involving strlen?

2597

Isn't the transform only valid if the number of bits in CI is greater than the number of bits in V?
I.e. the unsigned value returned by strlen must fit into the non-negative part of the signed destination type. And given that constraint we always need the trunc.

bjope added inline comments.Sep 15 2021, 10:33 AM
llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
2597

Well, given that constraint we always need a zext, not trunc.

xbolva00 added inline comments.Sep 15 2021, 10:45 AM
llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
2597

zext? We need change from i64 (from newly added strlen) to i32 (return type of snprintf)

bjope added inline comments.Sep 15 2021, 10:46 AM
llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
2597

If we consider the case when the value returned by strlen as size_t would be out-of-range for the non-negative range of int, then I guess snprintf should return a negative value? Or is the result undefined (I haven't seen any documentation saying anything about such cases for snprintf, while for strlen it says that the result in undefined if no null-termination is found)?

Either way it would be ok to do the transformation when sizeof(size_t)==sizeof(int), because that would give a negative value if the strlen result is out-of-range for the int.

But if sizeof(size_t)>sizeof(int), then maybe we need to saturate the value instead of doing a trunc, just to be safe?