[llvm] ab9fc8b - [SLC] sprintf(dst, "%s", str) -> strcpy(dst, str)
Eli Friedman via llvm-commits
llvm-commits at lists.llvm.org
Thu Aug 13 16:15:50 PDT 2020
I think I'd prefer to use pre-commit review for all functional changes to SimplifyLibCalls, even if you're confident the change is safe; it's very easy to get something subtly wrong.
This patch LGTM.
-Eli
-----Original Message-----
From: llvm-commits <llvm-commits-bounces at lists.llvm.org> On Behalf Of Dávid Bolvanský via llvm-commits
Sent: Thursday, August 13, 2020 3:08 PM
To: llvm-commits at lists.llvm.org
Subject: [EXT] [llvm] ab9fc8b - [SLC] sprintf(dst, "%s", str) -> strcpy(dst, str)
Author: Dávid Bolvanský
Date: 2020-08-14T00:05:55+02:00
New Revision: ab9fc8bae805c785066779e76e7846aabad5609e
URL: https://github.com/llvm/llvm-project/commit/ab9fc8bae805c785066779e76e7846aabad5609e
DIFF: https://github.com/llvm/llvm-project/commit/ab9fc8bae805c785066779e76e7846aabad5609e.diff
LOG: [SLC] sprintf(dst, "%s", str) -> strcpy(dst, str)
Solves 46489
Added:
Modified:
llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
llvm/test/Transforms/InstCombine/2010-05-30-memcpy-Struct.ll
llvm/test/Transforms/InstCombine/sprintf-1.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
index 2f6e60fc09c4..b5f0f56029a4 100644
--- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -2487,21 +2487,11 @@ Value *LibCallSimplifier::optimizeSPrintFString(CallInst *CI,
}
if (FormatStr[1] == 's') {
- // sprintf(dest, "%s", str) -> llvm.memcpy(align 1 dest, align 1 str,
- // strlen(str)+1)
+ // sprintf(dest, "%s", str) -> strcpy(dest, str)
if (!CI->getArgOperand(2)->getType()->isPointerTy())
return nullptr;
- Value *Len = emitStrLen(CI->getArgOperand(2), B, DL, TLI);
- if (!Len)
- return nullptr;
- Value *IncLen =
- B.CreateAdd(Len, ConstantInt::get(Len->getType(), 1), "leninc");
- B.CreateMemCpy(CI->getArgOperand(0), Align(1), CI->getArgOperand(2),
- Align(1), IncLen);
-
- // The sprintf result is the unincremented number of bytes in the string.
- return B.CreateIntCast(Len, CI->getType(), false);
+ return emitStrCpy(CI->getArgOperand(0), CI->getArgOperand(2), B, TLI);
}
return nullptr;
}
diff --git a/llvm/test/Transforms/InstCombine/2010-05-30-memcpy-Struct.ll b/llvm/test/Transforms/InstCombine/2010-05-30-memcpy-Struct.ll
index 51610698c244..e3cf763917a0 100644
--- a/llvm/test/Transforms/InstCombine/2010-05-30-memcpy-Struct.ll
+++ b/llvm/test/Transforms/InstCombine/2010-05-30-memcpy-Struct.ll
@@ -1,3 +1,4 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -instcombine -S < %s | FileCheck %s
; PR7265
@@ -9,10 +10,14 @@ target triple = "x86_64-unknown-linux-gnu"
@.str = private constant [3 x i8] c"%s\00"
define void @CopyEventArg(%union.anon* %ev) nounwind {
+; CHECK-LABEL: @CopyEventArg(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[CSTR:%.*]] = bitcast %union.anon* [[EV:%.*]] to i8*
+; CHECK-NEXT: [[STRCPY:%.*]] = call i8* @strcpy(i8* nonnull dereferenceable(1) undef, i8* nonnull dereferenceable(1) [[CSTR]])
+; CHECK-NEXT: ret void
+;
entry:
%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
-; CHECK: bitcast %union.anon* %ev to i8*
-; CHECK: call void @llvm.memcpy.p0i8.p0i8.i64
ret void
}
diff --git a/llvm/test/Transforms/InstCombine/sprintf-1.ll b/llvm/test/Transforms/InstCombine/sprintf-1.ll
index 9dbfecaecb31..77c2d4bbd9b6 100644
--- a/llvm/test/Transforms/InstCombine/sprintf-1.ll
+++ b/llvm/test/Transforms/InstCombine/sprintf-1.ll
@@ -81,19 +81,15 @@ define void @test_simplify4(i8* %dst) {
ret void
}
-; Check sprintf(dst, "%s", str) -> llvm.memcpy(dest, str, strlen(str) + 1, 1).
+; Check sprintf(dst, "%s", str) -> strcpy(dst, str)
define void @test_simplify5(i8* %dst, i8* %str) {
; CHECK-LABEL: @test_simplify5(
-; CHECK-NEXT: [[STRLEN:%.*]] = call i32 @strlen(i8* nonnull dereferenceable(1) [[STR:%.*]])
-; CHECK-NEXT: [[LENINC:%.*]] = add i32 [[STRLEN]], 1
-; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 1 [[DST:%.*]], i8* align 1 [[STR]], i32 [[LENINC]], i1 false)
+; CHECK-NEXT: [[STRCPY:%.*]] = call i8* @strcpy(i8* nonnull dereferenceable(1) [[DST:%.*]], i8* nonnull dereferenceable(1) [[STR:%.*]])
; CHECK-NEXT: ret void
;
; CHECK-IPRINTF-LABEL: @test_simplify5(
-; CHECK-IPRINTF-NEXT: [[STRLEN:%.*]] = call i32 @strlen(i8* nonnull dereferenceable(1) [[STR:%.*]])
-; CHECK-IPRINTF-NEXT: [[LENINC:%.*]] = add i32 [[STRLEN]], 1
-; CHECK-IPRINTF-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 1 [[DST:%.*]], i8* align 1 [[STR]], i32 [[LENINC]], i1 false)
+; CHECK-IPRINTF-NEXT: [[STRCPY:%.*]] = call i8* @strcpy(i8* nonnull dereferenceable(1) [[DST:%.*]], i8* nonnull dereferenceable(1) [[STR:%.*]])
; CHECK-IPRINTF-NEXT: ret void
;
%fmt = getelementptr [3 x i8], [3 x i8]* @percent_s, i32 0, i32 0
_______________________________________________
llvm-commits mailing list
llvm-commits at lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
More information about the llvm-commits
mailing list