[llvm] [AggressiveInstCombine] Expand strchr/memchr with small constant strings (PR #98501)

via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 15 01:48:23 PDT 2024


================
@@ -1103,6 +1108,85 @@ void StrNCmpInliner::inlineCompare(Value *LHS, StringRef RHS, uint64_t N,
   }
 }
 
+/// Convert strchr/memchr with a small constant string into a switch
+static bool foldStrChr(CallInst *Call, LibFunc Func, DomTreeUpdater *DTU,
+                       const DataLayout &DL) {
+  assert((Func == LibFunc_strchr || Func == LibFunc_memchr) &&
+         "Unexpected LibFunc");
+  if (isa<Constant>(Call->getArgOperand(1)))
+    return false;
+
+  StringRef Str;
+  Value *Base = Call->getArgOperand(0);
+  if (!getConstantStringInfo(Base, Str, /*TrimAtNul=*/Func == LibFunc_strchr))
+    return false;
----------------
goldsteinn wrote:

Doesn't `strchr` with a constant string + non-constant search character already get optimized to memchr:
https://github.com/llvm/llvm-project/blob/9f4a25e2a7cd176bd4f946dc651bc18c7a2e8c92/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp#L477-L497

Can this function be made to only handle memchr?

https://github.com/llvm/llvm-project/pull/98501


More information about the llvm-commits mailing list