From da137d041b3766db755ab390528ea5d9eb60d0f0 Mon Sep 17 00:00:00 2001 From: uabkaka Date: Tue, 18 Oct 2022 15:48:26 +0200 Subject: [PATCH] [SimplifyLibCalls] Add NoUndef/NonNull/Dereferenceable attributes to iprintf/siprintf When SimplifyLibCalls fail to optimize printf and sprintf it add NoUndef/NonNull/Dereferenceable attributes. This patch add the same attributes if SimplifyLibCalls optimize printf/sprintf into the integer only iprintf/siprintf. Reviewed By: nikic Differential Revision: https://reviews.llvm.org/D136140 --- llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp | 6 ++++-- llvm/test/Transforms/InstCombine/printf-1.ll | 4 ++-- llvm/test/Transforms/InstCombine/sprintf-1.ll | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp index 8f25e80..dd25fb3 100644 --- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp @@ -2887,6 +2887,8 @@ Value *LibCallSimplifier::optimizePrintF(CallInst *CI, IRBuilderBase &B) { return V; } + annotateNonNullNoUndefBasedOnAccess(CI, 0); + // printf(format, ...) -> iprintf(format, ...) if no floating point // arguments. if (isLibFuncEmittable(M, TLI, LibFunc_iprintf) && @@ -2911,7 +2913,6 @@ Value *LibCallSimplifier::optimizePrintF(CallInst *CI, IRBuilderBase &B) { return New; } - annotateNonNullNoUndefBasedOnAccess(CI, 0); return nullptr; } @@ -3010,6 +3011,8 @@ Value *LibCallSimplifier::optimizeSPrintF(CallInst *CI, IRBuilderBase &B) { return V; } + annotateNonNullNoUndefBasedOnAccess(CI, {0, 1}); + // sprintf(str, format, ...) -> siprintf(str, format, ...) if no floating // point arguments. if (isLibFuncEmittable(M, TLI, LibFunc_siprintf) && @@ -3034,7 +3037,6 @@ Value *LibCallSimplifier::optimizeSPrintF(CallInst *CI, IRBuilderBase &B) { return New; } - annotateNonNullNoUndefBasedOnAccess(CI, {0, 1}); return nullptr; } diff --git a/llvm/test/Transforms/InstCombine/printf-1.ll b/llvm/test/Transforms/InstCombine/printf-1.ll index 3fd1ddc..f162b2d 100644 --- a/llvm/test/Transforms/InstCombine/printf-1.ll +++ b/llvm/test/Transforms/InstCombine/printf-1.ll @@ -127,7 +127,7 @@ define void @test_simplify7() { ; CHECK-NEXT: ret void ; ; CHECK-IPRINTF-LABEL: @test_simplify7( -; CHECK-IPRINTF-NEXT: [[TMP1:%.*]] = call i32 (ptr, ...) @iprintf(ptr nonnull @percent_d, i32 187) +; CHECK-IPRINTF-NEXT: [[TMP1:%.*]] = call i32 (ptr, ...) @iprintf(ptr noundef nonnull dereferenceable(1) @percent_d, i32 187) ; CHECK-IPRINTF-NEXT: ret void ; call i32 (ptr, ...) @printf(ptr @percent_d, i32 187) @@ -166,7 +166,7 @@ define i32 @test_no_simplify3() { ; CHECK-NEXT: ret i32 [[RET]] ; ; CHECK-IPRINTF-LABEL: @test_no_simplify3( -; CHECK-IPRINTF-NEXT: [[TMP1:%.*]] = call i32 (ptr, ...) @iprintf(ptr nonnull @h) +; CHECK-IPRINTF-NEXT: [[TMP1:%.*]] = call i32 (ptr, ...) @iprintf(ptr noundef nonnull dereferenceable(1) @h) ; CHECK-IPRINTF-NEXT: ret i32 [[TMP1]] ; %ret = call i32 (ptr, ...) @printf(ptr @h) diff --git a/llvm/test/Transforms/InstCombine/sprintf-1.ll b/llvm/test/Transforms/InstCombine/sprintf-1.ll index ca49dcc..6b2fddb 100644 --- a/llvm/test/Transforms/InstCombine/sprintf-1.ll +++ b/llvm/test/Transforms/InstCombine/sprintf-1.ll @@ -79,7 +79,7 @@ define void @test_simplify5(ptr %dst, ptr %str) { define void @test_simplify6(ptr %dst) { ; CHECK-IPRINTF-LABEL: @test_simplify6( -; CHECK-IPRINTF-NEXT: [[TMP1:%.*]] = call i32 (ptr, ptr, ...) @siprintf(ptr [[DST:%.*]], ptr nonnull @percent_d, i32 187) +; CHECK-IPRINTF-NEXT: [[TMP1:%.*]] = call i32 (ptr, ptr, ...) @siprintf(ptr noundef nonnull dereferenceable(1) [[DST:%.*]], ptr noundef nonnull dereferenceable(1) @percent_d, i32 187) ; CHECK-IPRINTF-NEXT: ret void ; ; WIN-LABEL: @test_simplify6( -- 2.7.4