[llvm] 87407fc - DSE: fix bug where we would only check libcalls for name rather than whole decl

Nuno Lopes via llvm-commits llvm-commits at lists.llvm.org
Sat Jan 11 03:57:58 PST 2020


Author: Nuno Lopes
Date: 2020-01-11T11:57:29Z
New Revision: 87407fc03c82d880cc42330a8e230e7a48174e3c

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

LOG: DSE: fix bug where we would only check libcalls for name rather than whole decl

Added: 
    llvm/test/Transforms/DeadStoreElimination/libcalls2.ll

Modified: 
    llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
    llvm/test/Transforms/DeadStoreElimination/libcalls.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp b/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
index dd87b57cbe7f..1ba4aab999e1 100644
--- a/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
@@ -179,15 +179,18 @@ static bool hasAnalyzableMemoryWrite(Instruction *I,
   }
   if (auto CS = CallSite(I)) {
     if (Function *F = CS.getCalledFunction()) {
-      StringRef FnName = F->getName();
-      if (TLI.has(LibFunc_strcpy) && FnName == TLI.getName(LibFunc_strcpy))
-        return true;
-      if (TLI.has(LibFunc_strncpy) && FnName == TLI.getName(LibFunc_strncpy))
-        return true;
-      if (TLI.has(LibFunc_strcat) && FnName == TLI.getName(LibFunc_strcat))
-        return true;
-      if (TLI.has(LibFunc_strncat) && FnName == TLI.getName(LibFunc_strncat))
-        return true;
+      LibFunc LF;
+      if (TLI.getLibFunc(*F, LF) && TLI.has(LF)) {
+        switch (LF) {
+        case LibFunc_strcpy:
+        case LibFunc_strncpy:
+        case LibFunc_strcat:
+        case LibFunc_strncat:
+          return true;
+        default:
+          return false;
+        }
+      }
     }
   }
   return false;

diff  --git a/llvm/test/Transforms/DeadStoreElimination/libcalls.ll b/llvm/test/Transforms/DeadStoreElimination/libcalls.ll
index 8afa148a58b7..eac7724608cf 100644
--- a/llvm/test/Transforms/DeadStoreElimination/libcalls.ll
+++ b/llvm/test/Transforms/DeadStoreElimination/libcalls.ll
@@ -1,5 +1,7 @@
 ; RUN: opt -S -basicaa -dse < %s | FileCheck %s
 
+target triple = "x86_64-unknown-linux-gnu"
+
 declare i8* @strcpy(i8* %dest, i8* %src) nounwind
 define void @test1(i8* %src) {
 ; CHECK-LABEL: @test1(
@@ -11,13 +13,13 @@ define void @test1(i8* %src) {
   ret void
 }
 
-declare i8* @strncpy(i8* %dest, i8* %src, i32 %n) nounwind
+declare i8* @strncpy(i8* %dest, i8* %src, i64 %n) nounwind
 define void @test2(i8* %src) {
 ; CHECK-LABEL: @test2(
   %B = alloca [16 x i8]
   %dest = getelementptr inbounds [16 x i8], [16 x i8]* %B, i64 0, i64 0
 ; CHECK-NOT: @strncpy
-  %call = call i8* @strncpy(i8* %dest, i8* %src, i32 12)
+  %call = call i8* @strncpy(i8* %dest, i8* %src, i64 12)
 ; CHECK: ret void
   ret void
 }
@@ -33,13 +35,13 @@ define void @test3(i8* %src) {
   ret void
 }
 
-declare i8* @strncat(i8* %dest, i8* %src, i32 %n) nounwind
+declare i8* @strncat(i8* %dest, i8* %src, i64 %n) nounwind
 define void @test4(i8* %src) {
 ; CHECK-LABEL: @test4(
   %B = alloca [16 x i8]
   %dest = getelementptr inbounds [16 x i8], [16 x i8]* %B, i64 0, i64 0
 ; CHECK-NOT: @strncat
-  %call = call i8* @strncat(i8* %dest, i8* %src, i32 12)
+  %call = call i8* @strncat(i8* %dest, i8* %src, i64 12)
 ; CHECK: ret void
   ret void
 }

diff  --git a/llvm/test/Transforms/DeadStoreElimination/libcalls2.ll b/llvm/test/Transforms/DeadStoreElimination/libcalls2.ll
new file mode 100644
index 000000000000..0e8d9eade9ee
--- /dev/null
+++ b/llvm/test/Transforms/DeadStoreElimination/libcalls2.ll
@@ -0,0 +1,14 @@
+; RUN: opt -S -basicaa -dse < %s | FileCheck %s
+
+target triple = "x86_64-unknown-linux-gnu"
+
+declare i8* @strncpy(i8* %dest, i8* %src, i32 %n) nounwind
+define void @test2(i8* %src) {
+; CHECK-LABEL: @test2(
+  %B = alloca [16 x i8]
+  %dest = getelementptr inbounds [16 x i8], [16 x i8]* %B, i64 0, i64 0
+; CHECK: @strncpy
+  %call = call i8* @strncpy(i8* %dest, i8* %src, i32 12)
+; CHECK: ret void
+  ret void
+}


        


More information about the llvm-commits mailing list