[llvm-commits] [PATCH] simplify-libcalls: fold strstr(a, b) == a -> strncmp(a, b, strlen(b)) == 0

Chris Lattner clattner at apple.com
Tue Jun 15 12:03:46 PDT 2010


On Jun 15, 2010, at 11:50 AM, Benjamin Kramer wrote:

> This seems to be a common idiom to check the prefix of a string (lldb uses it) and it triggers O(n*m) behavior in most libc implementations. strncmp brings it down to O(m).
> 
> I'm not sure if simplify-libcalls is the right place for this. The code basically works around the pass's infrastructure (skipping statistics etc.)

This looks great to me.  As a micro-micro-optimization, you could check to see if 'a' has a known length.  If so, you could use strlen(a) instead since using strlen(a) or strlen(b) both work.

-Chris



More information about the llvm-commits mailing list