Skip to content

Commit ab9fc8b

Browse files
[SLC] sprintf(dst, "%s", str) -> strcpy(dst, str)
Solves 46489
1 parent 097c8fb commit ab9fc8b

File tree

3 files changed

+12
-21
lines changed

3 files changed

+12
-21
lines changed

llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2487,21 +2487,11 @@ Value *LibCallSimplifier::optimizeSPrintFString(CallInst *CI,
24872487
}
24882488

24892489
if (FormatStr[1] == 's') {
2490-
// sprintf(dest, "%s", str) -> llvm.memcpy(align 1 dest, align 1 str,
2491-
// strlen(str)+1)
2490+
// sprintf(dest, "%s", str) -> strcpy(dest, str)
24922491
if (!CI->getArgOperand(2)->getType()->isPointerTy())
24932492
return nullptr;
24942493

2495-
Value *Len = emitStrLen(CI->getArgOperand(2), B, DL, TLI);
2496-
if (!Len)
2497-
return nullptr;
2498-
Value *IncLen =
2499-
B.CreateAdd(Len, ConstantInt::get(Len->getType(), 1), "leninc");
2500-
B.CreateMemCpy(CI->getArgOperand(0), Align(1), CI->getArgOperand(2),
2501-
Align(1), IncLen);
2502-
2503-
// The sprintf result is the unincremented number of bytes in the string.
2504-
return B.CreateIntCast(Len, CI->getType(), false);
2494+
return emitStrCpy(CI->getArgOperand(0), CI->getArgOperand(2), B, TLI);
25052495
}
25062496
return nullptr;
25072497
}

llvm/test/Transforms/InstCombine/2010-05-30-memcpy-Struct.ll

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
12
; RUN: opt -instcombine -S < %s | FileCheck %s
23
; PR7265
34

@@ -9,10 +10,14 @@ target triple = "x86_64-unknown-linux-gnu"
910
@.str = private constant [3 x i8] c"%s\00"
1011

1112
define void @CopyEventArg(%union.anon* %ev) nounwind {
13+
; CHECK-LABEL: @CopyEventArg(
14+
; CHECK-NEXT: entry:
15+
; CHECK-NEXT: [[CSTR:%.*]] = bitcast %union.anon* [[EV:%.*]] to i8*
16+
; CHECK-NEXT: [[STRCPY:%.*]] = call i8* @strcpy(i8* nonnull dereferenceable(1) undef, i8* nonnull dereferenceable(1) [[CSTR]])
17+
; CHECK-NEXT: ret void
18+
;
1219
entry:
1320
%call = call i32 (i8*, i8*, ...) @sprintf(i8* undef, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), %union.anon* %ev) nounwind
14-
; CHECK: bitcast %union.anon* %ev to i8*
15-
; CHECK: call void @llvm.memcpy.p0i8.p0i8.i64
1621
ret void
1722
}
1823

llvm/test/Transforms/InstCombine/sprintf-1.ll

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,15 @@ define void @test_simplify4(i8* %dst) {
8181
ret void
8282
}
8383

84-
; Check sprintf(dst, "%s", str) -> llvm.memcpy(dest, str, strlen(str) + 1, 1).
84+
; Check sprintf(dst, "%s", str) -> strcpy(dst, str)
8585

8686
define void @test_simplify5(i8* %dst, i8* %str) {
8787
; CHECK-LABEL: @test_simplify5(
88-
; CHECK-NEXT: [[STRLEN:%.*]] = call i32 @strlen(i8* nonnull dereferenceable(1) [[STR:%.*]])
89-
; CHECK-NEXT: [[LENINC:%.*]] = add i32 [[STRLEN]], 1
90-
; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 1 [[DST:%.*]], i8* align 1 [[STR]], i32 [[LENINC]], i1 false)
88+
; CHECK-NEXT: [[STRCPY:%.*]] = call i8* @strcpy(i8* nonnull dereferenceable(1) [[DST:%.*]], i8* nonnull dereferenceable(1) [[STR:%.*]])
9189
; CHECK-NEXT: ret void
9290
;
9391
; CHECK-IPRINTF-LABEL: @test_simplify5(
94-
; CHECK-IPRINTF-NEXT: [[STRLEN:%.*]] = call i32 @strlen(i8* nonnull dereferenceable(1) [[STR:%.*]])
95-
; CHECK-IPRINTF-NEXT: [[LENINC:%.*]] = add i32 [[STRLEN]], 1
96-
; CHECK-IPRINTF-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 1 [[DST:%.*]], i8* align 1 [[STR]], i32 [[LENINC]], i1 false)
92+
; CHECK-IPRINTF-NEXT: [[STRCPY:%.*]] = call i8* @strcpy(i8* nonnull dereferenceable(1) [[DST:%.*]], i8* nonnull dereferenceable(1) [[STR:%.*]])
9793
; CHECK-IPRINTF-NEXT: ret void
9894
;
9995
%fmt = getelementptr [3 x i8], [3 x i8]* @percent_s, i32 0, i32 0

0 commit comments

Comments
 (0)