[llvm] a001e97 - [SimplifyLibCalls] Don't try to manually reprocess calls

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 5 01:45:35 PST 2024


Author: Nikita Popov
Date: 2024-01-05T10:45:26+01:00
New Revision: a001e9718fd974859f2797a9f9ed7bf87d364e4f

URL: https://github.com/llvm/llvm-project/commit/a001e9718fd974859f2797a9f9ed7bf87d364e4f
DIFF: https://github.com/llvm/llvm-project/commit/a001e9718fd974859f2797a9f9ed7bf87d364e4f.diff

LOG: [SimplifyLibCalls] Don't try to manually reprocess calls

The current code for reprocessing the result of fortified libcall
simplifications is not correct, because we might simplify to an
argument of the original call, and if that is again a libcall,
mistakenly think that this is actually the simplification result.

Instead of trying to fix this, simply remove the code entirely,
because InstCombine nowadays correctly handles reprocessing of
SimplifyLibCall results.

Fixes https://github.com/llvm/llvm-project/issues/77064.

Added: 
    llvm/test/Transforms/InstCombine/pr77064.ll

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

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
index 760a626c8b6fcb..a7cd68e860e467 100644
--- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -3735,26 +3735,8 @@ Value *LibCallSimplifier::optimizeCall(CallInst *CI, IRBuilderBase &Builder) {
 
   // Also try to simplify calls to fortified library functions.
   if (Value *SimplifiedFortifiedCI =
-          FortifiedSimplifier.optimizeCall(CI, Builder)) {
-    // Try to further simplify the result.
-    CallInst *SimplifiedCI = dyn_cast<CallInst>(SimplifiedFortifiedCI);
-    if (SimplifiedCI && SimplifiedCI->getCalledFunction()) {
-      // Ensure that SimplifiedCI's uses are complete, since some calls have
-      // their uses analyzed.
-      replaceAllUsesWith(CI, SimplifiedCI);
-
-      // Set insertion point to SimplifiedCI to guarantee we reach all uses
-      // we might replace later on.
-      IRBuilderBase::InsertPointGuard Guard(Builder);
-      Builder.SetInsertPoint(SimplifiedCI);
-      if (Value *V = optimizeStringMemoryLibCall(SimplifiedCI, Builder)) {
-        // If we were able to further simplify, remove the now redundant call.
-        substituteInParent(SimplifiedCI, V);
-        return V;
-      }
-    }
+          FortifiedSimplifier.optimizeCall(CI, Builder))
     return SimplifiedFortifiedCI;
-  }
 
   // Then check for known library functions.
   if (TLI->getLibFunc(*Callee, Func) && isLibFuncEmittable(M, TLI, Func)) {

diff  --git a/llvm/test/Transforms/InstCombine/pr77064.ll b/llvm/test/Transforms/InstCombine/pr77064.ll
new file mode 100644
index 00000000000000..b6afecf83f4570
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/pr77064.ll
@@ -0,0 +1,18 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
+; RUN: opt -S -passes=instcombine < %s | FileCheck %s
+
+define void @main(ptr %ptr) {
+; CHECK-LABEL: define void @main(
+; CHECK-SAME: ptr [[PTR:%.*]]) {
+; CHECK-NEXT:    [[OPENDIR:%.*]] = call fastcc ptr @opendir(ptr [[PTR]])
+; CHECK-NEXT:    call void @llvm.memset.p0.i64(ptr noundef nonnull align 1 dereferenceable(596) [[OPENDIR]], i8 0, i64 596, i1 false)
+; CHECK-NEXT:    ret void
+;
+  %opendir = call fastcc ptr @opendir(ptr %ptr)
+  %memset = call ptr @__memset_chk(ptr %opendir, i32 0, i64 596, i64 -1)
+  ret void
+}
+
+declare ptr @__memset_chk(ptr, i32, i64, i64)
+
+declare fastcc ptr @opendir(ptr)


        


More information about the llvm-commits mailing list