[PATCH] D126515: [InstCombine] Fold memchr of sequences of same characters

Martin Sebor via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 7 12:28:12 PDT 2022


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGfb6627fa0c6e: [InstCombine] Add substr helper function (NFC). (authored by msebor).

Changed prior to commit:
  https://reviews.llvm.org/D126515?vs=433202&id=434914#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D126515/new/

https://reviews.llvm.org/D126515

Files:
  llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp


Index: llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
===================================================================
--- llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -201,6 +201,11 @@
   return New;
 }
 
+// Helper to avoid truncating the length if size_t is 32-bits.
+static StringRef substr(StringRef Str, uint64_t Len) {
+  return Len >= Str.size() ? Str : Str.substr(0, Len);
+}
+
 //===----------------------------------------------------------------------===//
 // String and Memory Library Call Optimizations
 //===----------------------------------------------------------------------===//
@@ -452,10 +457,8 @@
   // strncmp(x, y)  -> cnst  (if both x and y are constant strings)
   if (HasStr1 && HasStr2) {
     // Avoid truncating the 64-bit Length to 32 bits in ILP32.
-    StringRef::size_type MinLen1 = std::min((uint64_t)Str1.size(), Length);
-    StringRef::size_type MinLen2 = std::min((uint64_t)Str2.size(), Length);
-    StringRef SubStr1 = Str1.substr(0, MinLen1);
-    StringRef SubStr2 = Str2.substr(0, MinLen2);
+    StringRef SubStr1 = substr(Str1, Length);
+    StringRef SubStr2 = substr(Str2, Length);
     return ConstantInt::get(CI->getType(), SubStr1.compare(SubStr2));
   }
 
@@ -1049,8 +1052,7 @@
 
   // Truncate the string to LenC without slicing when targeting LP64
   // on an ILP32 host.
-  uint64_t EndOff = std::min(LenC->getZExtValue(), (uint64_t)StringRef::npos);
-  Str = Str.substr(0, EndOff);
+  Str = substr(Str, LenC->getZExtValue());
 
   // If the char is variable but the input str and length are not we can turn
   // this memchr call into a simple bit field test. Of course this only works


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D126515.434914.patch
Type: text/x-patch
Size: 1702 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220607/a2b06bf9/attachment.bin>


More information about the llvm-commits mailing list