Skip to content

Commit ded48e9

Browse files
[SLC] Preserve attrs for strncpy(x, "", y) -> memset(align 1 x, '\0', y)
llvm-svn: 372101
1 parent 1a9195d commit ded48e9

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,10 @@ Value *LibCallSimplifier::optimizeStrNCpy(CallInst *CI, IRBuilder<> &B) {
610610

611611
if (SrcLen == 0) {
612612
// strncpy(x, "", y) -> memset(align 1 x, '\0', y)
613-
B.CreateMemSet(Dst, B.getInt8('\0'), Size, 1);
613+
CallInst *NewCI = B.CreateMemSet(Dst, B.getInt8('\0'), Size, 1);
614+
AttrBuilder ArgAttrs(CI->getAttributes().getParamAttributes(0));
615+
NewCI->setAttributes(NewCI->getAttributes().addParamAttributes(
616+
CI->getContext(), 0, ArgAttrs));
614617
return Dst;
615618
}
616619

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,16 @@ define i8* @test2(i8* %dst) {
134134
ret i8* %ret
135135
}
136136

137+
define i8* @test3(i8* %dst, i32 %n) {
138+
; CHECK-LABEL: @test3(
139+
; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* noalias nonnull align 1 dereferenceable(5) [[DST:%.*]], i8 0, i32 5, i1 false)
140+
; CHECK-NEXT: ret i8* [[DST]]
141+
;
142+
%src = getelementptr [1 x i8], [1 x i8]* @null, i32 0, i32 0
143+
%ret = call i8* @strncpy(i8* noalias nonnull %dst, i8* nonnull %src, i32 5);
144+
ret i8* %ret
145+
}
146+
137147
; Check cases that shouldn't be simplified.
138148

139149
define void @test_no_simplify1() {

0 commit comments

Comments
 (0)