Skip to content

Commit 48cd5b7

Browse files
committed
Revert "[SLC] sprintf(dst, "%s", str) -> strcpy(dst, str)"
This reverts commit ab9fc8b. Incorrect transformation if the result is used. Causes breakages, e.g. http://green.lab.llvm.org/green/job/test-suite-verify-machineinstrs-x86_64-O3/8193/
1 parent 800f0ed commit 48cd5b7

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp

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

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

2494-
return emitStrCpy(CI->getArgOperand(0), CI->getArgOperand(2), B, TLI);
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);
24952505
}
24962506
return nullptr;
24972507
}

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

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

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

1211
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-
;
1912
entry:
2013
%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
2116
ret void
2217
}
2318

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

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

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

8686
define void @test_simplify5(i8* %dst, i8* %str) {
8787
; CHECK-LABEL: @test_simplify5(
88-
; CHECK-NEXT: [[STRCPY:%.*]] = call i8* @strcpy(i8* nonnull dereferenceable(1) [[DST:%.*]], i8* nonnull dereferenceable(1) [[STR:%.*]])
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)
8991
; CHECK-NEXT: ret void
9092
;
9193
; CHECK-IPRINTF-LABEL: @test_simplify5(
92-
; CHECK-IPRINTF-NEXT: [[STRCPY:%.*]] = call i8* @strcpy(i8* nonnull dereferenceable(1) [[DST:%.*]], i8* nonnull dereferenceable(1) [[STR:%.*]])
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)
9397
; CHECK-IPRINTF-NEXT: ret void
9498
;
9599
%fmt = getelementptr [3 x i8], [3 x i8]* @percent_s, i32 0, i32 0

0 commit comments

Comments
 (0)