This is an archive of the discontinued LLVM Phabricator instance.

Strlen loop idiom recognition and folding
Needs ReviewPublic

Authored by musman on Jun 23 2020, 10:24 AM.

Details

Summary

This adds strlen to LoopIdiomRecognize.cpp and also recognizes strlens with 16 bit chars. For these cases, a new intrinsic (strlen16) is used. If a strlen/strlen16 function call is only used for zero equality comparison then it is replaced with a load of the first element. If a strlen16 intrinsic cannot be folded, it is expanded back out to the canonical loop form.

Examples that this recognizes:

unsigned strlen1(char *Str) {
  char *Src = Str;
  if (!Src)
    return 0;
  for (; *Src;)
    Src++;
  return Src - Str;
}

unsigned strlen2(char *Str) {
  unsigned Len = 0;
  if (!Str)
    return 0;
  while(*Str) {
    Len++;
    Str++;
  }
  return Len;
}

Diff Detail

Event Timeline

musman created this revision.Jun 23 2020, 10:24 AM
lebedev.ri added a subscriber: lebedev.ri.EditedJun 23 2020, 10:55 AM

This should be several patches:

  • Loop idiom recognition for plain strlen
  • langref for new strlen16
  • other strlen16 patches
  • Loop idiom recognition for strlen16
musman added a comment.Jul 8 2020, 6:35 AM

This should be several patches:

  • Loop idiom recognition for plain strlen
  • langref for new strlen16
  • other strlen16 patches
  • Loop idiom recognition for strlen16

Thanks, I will split this up into smaller patches

sanjoy resigned from this revision.Jan 29 2022, 5:24 PM