[PATCH] D18230: [SimplifyLibCalls] Simplify strlen to a subtraction for certain cases

Li Huang via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 16 15:02:18 PDT 2016


lihuang created this revision.
lihuang added reviewers: spatel, DavidKreitzer.
lihuang added a subscriber: llvm-commits.

This patch implements this optimization: 

            strlen(s + x)  --> strlen(s) - x,  

where s is a pointer to a string literal, and x is an integer variable. Note that strlen(s) is a constant which is known at compile time. This optimization is legal when one of the following is true:
      
      A. x is known to be within the range [0, strlen(s)]
      B. The string literal is null-terminated and has only one null terminator "\0" at its end, and (s + x) is proved to be an out-of-bound pointer when x < 0 or x > strlen(s). 

Case A is straightforward. For case B, it's legal to do this optimization because strlen will have undefined behavior when (s + x) is out of bound. 

http://reviews.llvm.org/D18230

Files:
  lib/Transforms/Utils/SimplifyLibCalls.cpp
  test/Transforms/InstCombine/strlen-1.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D18230.50860.patch
Type: text/x-patch
Size: 5432 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160316/4edc4326/attachment.bin>


More information about the llvm-commits mailing list