[PATCH] D47159: [InstCombine] Format String optimizations

Richard Smith - zygoloid via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue May 22 18:04:00 PDT 2018


rsmith added a comment.

Converting to `printf("single string literal")` will likely be a small performance pessimization (because the string literal must be scanned for embedded `%` characters). I think the most efficient form is instead likely to be a format string comprising *only* format specifiers; for example, given

  printf("hello %s, my favorite number is %d because it is %s", "world", n, "prime");

... when optimizing for speed, the best code we can produce is probably

  printf("%s%d%s", "hello world, my favorite number is ", n, " because it is prime");

... whereas when optimizing for size, it's probably

  printf("hello world, my favorite number is %d because it is prime", n);

(but in the latter case we probably also want to check that each of the input string literals has only one use or we risk increasing the data size).



================
Comment at: test/Transforms/InstCombine/format-str.ll:30
+; CHECK: @str.10 = private unnamed_addr constant [3 x i8] c"%d\00"
+; CHECK: @str.11 = private unnamed_addr constant [13 x i8] c"str: str: %s\00"
+
----------------
The transform miscompiles this case. The original program would print out "str: str: %s" and the transformed program will make an invalid call to printf.


https://reviews.llvm.org/D47159





More information about the llvm-commits mailing list