Skip to content

Commit 87407fc

Browse files
committed
DSE: fix bug where we would only check libcalls for name rather than whole decl
1 parent 142ba7d commit 87407fc

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -179,15 +179,18 @@ static bool hasAnalyzableMemoryWrite(Instruction *I,
179179
}
180180
if (auto CS = CallSite(I)) {
181181
if (Function *F = CS.getCalledFunction()) {
182-
StringRef FnName = F->getName();
183-
if (TLI.has(LibFunc_strcpy) && FnName == TLI.getName(LibFunc_strcpy))
184-
return true;
185-
if (TLI.has(LibFunc_strncpy) && FnName == TLI.getName(LibFunc_strncpy))
186-
return true;
187-
if (TLI.has(LibFunc_strcat) && FnName == TLI.getName(LibFunc_strcat))
188-
return true;
189-
if (TLI.has(LibFunc_strncat) && FnName == TLI.getName(LibFunc_strncat))
190-
return true;
182+
LibFunc LF;
183+
if (TLI.getLibFunc(*F, LF) && TLI.has(LF)) {
184+
switch (LF) {
185+
case LibFunc_strcpy:
186+
case LibFunc_strncpy:
187+
case LibFunc_strcat:
188+
case LibFunc_strncat:
189+
return true;
190+
default:
191+
return false;
192+
}
193+
}
191194
}
192195
}
193196
return false;

llvm/test/Transforms/DeadStoreElimination/libcalls.ll

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
; RUN: opt -S -basicaa -dse < %s | FileCheck %s
22

3+
target triple = "x86_64-unknown-linux-gnu"
4+
35
declare i8* @strcpy(i8* %dest, i8* %src) nounwind
46
define void @test1(i8* %src) {
57
; CHECK-LABEL: @test1(
@@ -11,13 +13,13 @@ define void @test1(i8* %src) {
1113
ret void
1214
}
1315

14-
declare i8* @strncpy(i8* %dest, i8* %src, i32 %n) nounwind
16+
declare i8* @strncpy(i8* %dest, i8* %src, i64 %n) nounwind
1517
define void @test2(i8* %src) {
1618
; CHECK-LABEL: @test2(
1719
%B = alloca [16 x i8]
1820
%dest = getelementptr inbounds [16 x i8], [16 x i8]* %B, i64 0, i64 0
1921
; CHECK-NOT: @strncpy
20-
%call = call i8* @strncpy(i8* %dest, i8* %src, i32 12)
22+
%call = call i8* @strncpy(i8* %dest, i8* %src, i64 12)
2123
; CHECK: ret void
2224
ret void
2325
}
@@ -33,13 +35,13 @@ define void @test3(i8* %src) {
3335
ret void
3436
}
3537

36-
declare i8* @strncat(i8* %dest, i8* %src, i32 %n) nounwind
38+
declare i8* @strncat(i8* %dest, i8* %src, i64 %n) nounwind
3739
define void @test4(i8* %src) {
3840
; CHECK-LABEL: @test4(
3941
%B = alloca [16 x i8]
4042
%dest = getelementptr inbounds [16 x i8], [16 x i8]* %B, i64 0, i64 0
4143
; CHECK-NOT: @strncat
42-
%call = call i8* @strncat(i8* %dest, i8* %src, i32 12)
44+
%call = call i8* @strncat(i8* %dest, i8* %src, i64 12)
4345
; CHECK: ret void
4446
ret void
4547
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
; RUN: opt -S -basicaa -dse < %s | FileCheck %s
2+
3+
target triple = "x86_64-unknown-linux-gnu"
4+
5+
declare i8* @strncpy(i8* %dest, i8* %src, i32 %n) nounwind
6+
define void @test2(i8* %src) {
7+
; CHECK-LABEL: @test2(
8+
%B = alloca [16 x i8]
9+
%dest = getelementptr inbounds [16 x i8], [16 x i8]* %B, i64 0, i64 0
10+
; CHECK: @strncpy
11+
%call = call i8* @strncpy(i8* %dest, i8* %src, i32 12)
12+
; CHECK: ret void
13+
ret void
14+
}

0 commit comments

Comments
 (0)