[PATCH] D128954: [InstCombine] Transform strrchr to memrchr for constant strings

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 1 08:33:52 PDT 2022


nikic accepted this revision.
nikic added a comment.
This revision is now accepted and ready to land.

LGTM



================
Comment at: llvm/lib/Transforms/Utils/BuildLibCalls.cpp:1543
+Value *llvm::emitMemRChr(Value *Ptr, Value *Val, Value *Len, IRBuilderBase &B,
+                        const DataLayout &DL, const TargetLibraryInfo *TLI) {
+  LLVMContext &Context = B.GetInsertBlock()->getContext();
----------------
msebor wrote:
> nikic wrote:
> > This one probably needs an isLibFuncEmittable check, because memrchr is a GNU extension. It looks like PS targets do mark it as unavailable.
> `isLibFuncEmittable` is called from `emitLibCall` so I think this parts should be fine.  Or am I missing something here?
Nope, my bad. It also looks like copyFlags will already handle a null argument correctly, so your code is fine as-is.


================
Comment at: llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp:362
+  Value *Size = ConstantInt::get(IntPtrType, NBytes);
+  return copyFlags(*CI, emitMemRChr(SrcStr, CharVal, Size, B, DL, TLI));
 }
----------------
msebor wrote:
> I did forget that `memrchr` isn't standard so this will fail on targets without it as you noted.  Are we okay with losing that optimization on those targets or should I keep the existing optimization and use it when `emitMemRChr` fails?
As it's just the one target that disables it, I think it's fine to drop the optimization.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D128954



More information about the llvm-commits mailing list