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

Benjamin Kramer benny.kra at googlemail.com
Tue Jun 15 14:48:11 PDT 2010


On 15.06.2010, at 23:43, Chris Lattner wrote:

> 
> On Jun 15, 2010, at 2:23 PM, John McCall wrote:
> 
>> 
>> On Jun 15, 2010, at 12:03 PM, Chris Lattner wrote:
>> 
>>> 
>>> 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.
>> 
>> It needs to be strlen(b).  strstr("foo", "food") != "foo".
> 
> Yep, he corrected me on irc.  :)

… and landed the patch with some minor tweaks in r106047.





More information about the llvm-commits mailing list