[llvm] 656ebd5 - [SimplifyLibCalls] Don't change alignment when creating memset
via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 21 11:42:31 PDT 2021
Author: sstefan1
Date: 2021-04-21T20:34:13+02:00
New Revision: 656ebd519e3fd52050e1c8abeacafcf94d1fa260
URL: https://github.com/llvm/llvm-project/commit/656ebd519e3fd52050e1c8abeacafcf94d1fa260
DIFF: https://github.com/llvm/llvm-project/commit/656ebd519e3fd52050e1c8abeacafcf94d1fa260.diff
LOG: [SimplifyLibCalls] Don't change alignment when creating memset
Fix for PR49984
This was discovered during Attributor testing.
Memset was always created with alignment of 1
and in case when strncpy alignment was changed
it triggered an assertion in the AttrBuilder.
Memset will now be created with appropriate alignment.
Differential Revision: https://reviews.llvm.org/D100875
Added:
Modified:
llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
llvm/test/Transforms/InstCombine/strncpy-1.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
index 2e440511d6fde..ab181be20964d 100644
--- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -575,8 +575,10 @@ Value *LibCallSimplifier::optimizeStrNCpy(CallInst *CI, IRBuilderBase &B) {
}
if (SrcLen == 0) {
- // strncpy(x, "", y) -> memset(align 1 x, '\0', y)
- CallInst *NewCI = B.CreateMemSet(Dst, B.getInt8('\0'), Size, Align(1));
+ // strncpy(x, "", y) -> memset(x, '\0', y)
+ Align MemSetAlign =
+ CI->getAttributes().getParamAttributes(0).getAlignment().valueOrOne();
+ CallInst *NewCI = B.CreateMemSet(Dst, B.getInt8('\0'), Size, MemSetAlign);
AttrBuilder ArgAttrs(CI->getAttributes().getParamAttributes(0));
NewCI->setAttributes(NewCI->getAttributes().addParamAttributes(
CI->getContext(), 0, ArgAttrs));
diff --git a/llvm/test/Transforms/InstCombine/strncpy-1.ll b/llvm/test/Transforms/InstCombine/strncpy-1.ll
index bc104cab081ae..210df0e7c1ea4 100644
--- a/llvm/test/Transforms/InstCombine/strncpy-1.ll
+++ b/llvm/test/Transforms/InstCombine/strncpy-1.ll
@@ -144,6 +144,16 @@ define i8* @test3(i8* %dst, i32 %n) {
ret i8* %ret
}
+define i8* @test4(i8* %dst, i32 %n) {
+; CHECK-LABEL: @test4(
+; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* noalias noundef nonnull align 16 dereferenceable(5) [[DST:%.*]], i8 0, i32 5, i1 false)
+; CHECK-NEXT: ret i8* [[DST]]
+;
+ %src = getelementptr [1 x i8], [1 x i8]* @null, i32 0, i32 0
+ %ret = call i8* @strncpy(i8* align(16) noalias nonnull %dst, i8* nonnull %src, i32 5);
+ ret i8* %ret
+}
+
; Check cases that shouldn't be simplified.
define void @test_no_simplify1() {
More information about the llvm-commits
mailing list