[PATCH] D135581: [MemCpyOpt] Use usub_sat in processMemSetMemCpyDependence

Bjorn Pettersson via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 10 14:04:48 PDT 2022


bjope added inline comments.


================
Comment at: llvm/include/llvm/IR/IRBuilder.h:945
+  CallInst *CreateUSubSat(Value *LHS, Value *RHS, const Twine &Name = "") {
+    return CreateBinaryIntrinsic(Intrinsic::usub_sat, LHS, RHS, nullptr, Name);
+  }
----------------
Looks like we do not handle this in the ConstantFolder so if LHS and RHS are constant the usub_sat will remain in the IR until we simplify this.


================
Comment at: llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp:1279
 
-  Value *Ule = Builder.CreateICmpULE(DestSize, SrcSize);
-  Value *SizeDiff = Builder.CreateSub(DestSize, SrcSize);
-  Value *MemsetLen = Builder.CreateSelect(
-      Ule, ConstantInt::getNullValue(DestSize->getType()), SizeDiff);
+  Value *MemsetLen = Builder.CreateUSubSat(DestSize, SrcSize, "memset.len");
   unsigned DestAS = Dest->getType()->getPointerAddressSpace();
----------------
Maybe this isn't always desired. One situation I could imagine is that if for example the code is comparing DestSize and SrcSize for other reasons, then maybe the InstCombine transform is more careful and thinking that it isn't preferable to use usub_sat as we need to do the comparison anyhow.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D135581



More information about the llvm-commits mailing list