[PATCH] D139096: [DSE] Use precise loc for memset_chk during overwrite checks.

Florian Hahn via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 1 04:11:40 PST 2022


fhahn created this revision.
fhahn added reviewers: nikic, ab, efriedma, asbirlea.
Herald added a subscriber: hiraditya.
Herald added a project: All.
fhahn requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Extends the logic from D115167 <https://reviews.llvm.org/D115167> to memcpy_chk.

Depends on D115167 <https://reviews.llvm.org/D115167>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D139096

Files:
  llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
  llvm/test/Transforms/DeadStoreElimination/libcalls-chk.ll


Index: llvm/test/Transforms/DeadStoreElimination/libcalls-chk.ll
===================================================================
--- llvm/test/Transforms/DeadStoreElimination/libcalls-chk.ll
+++ llvm/test/Transforms/DeadStoreElimination/libcalls-chk.ll
@@ -107,8 +107,7 @@
 
 define void @dse_strncpy_memcpy_chk_test1(ptr noalias %out, ptr noalias %in, i64 %n) {
 ; CHECK-LABEL: @dse_strncpy_memcpy_chk_test1(
-; CHECK-NEXT:    store i32 0, ptr [[OUT:%.*]], align 4
-; CHECK-NEXT:    [[CALL_1:%.*]] = tail call ptr @__memcpy_chk(ptr [[OUT]], ptr [[IN:%.*]], i64 100, i64 [[N:%.*]])
+; CHECK-NEXT:    [[CALL_1:%.*]] = tail call ptr @__memcpy_chk(ptr [[OUT:%.*]], ptr [[IN:%.*]], i64 100, i64 [[N:%.*]])
 ; CHECK-NEXT:    ret void
 ;
   store i32 0, ptr %out
Index: llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
+++ llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
@@ -882,7 +882,7 @@
                                       LocationSize Size) const {
     if (auto *CB = dyn_cast<CallBase>(I)) {
       LibFunc F;
-      if (TLI.getLibFunc(*CB, F) && TLI.has(F) && F == LibFunc_memset_chk) {
+      if (TLI.getLibFunc(*CB, F) && TLI.has(F)) {
         // Use the precise location size specified by the 3rd argument during
         // for determining KillingI overwrites DeadLoc if it is a memset_chk
         // instruction. memset_chk will write either the amount specified as 3rd
@@ -891,8 +891,16 @@
         // is larger than the allocation size due to that being UB. To avoid
         // returning potentially invalid NoAlias results by AA, limit the use of
         // the precise location size to isOverwrite.
-        if (const auto *Len = dyn_cast<ConstantInt>(CB->getArgOperand(2)))
-          return LocationSize::precise(Len->getZExtValue());
+
+          switch (F) {
+          default:
+            break;
+          case LibFunc_memset_chk:
+          case LibFunc_memcpy_chk:
+            if (const auto *Len = dyn_cast<ConstantInt>(CB->getArgOperand(2)))
+              return LocationSize::precise(Len->getZExtValue());
+            break;
+          }
       }
     }
     return Size;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D139096.479238.patch
Type: text/x-patch
Size: 2241 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221201/d1244b46/attachment.bin>


More information about the llvm-commits mailing list