From 20e989e9de6abcf9a684978a2688acc4ea01036f Mon Sep 17 00:00:00 2001 From: Florian Hahn Date: Fri, 28 Aug 2020 09:37:01 +0100 Subject: [PATCH] [BuildLibCalls] Add argmemonly to more lib calls. strspn, strncmp, strcspn, strcasecmp, strncasecmp, memcmp, memchr, memrchr, memcpy, memmove, memcpy, mempcpy, strchr, strrchr, bcmp should all only access memory through their arguments. I broke out strcoll, strcasecmp, strncasecmp because the result depends on the locale, which might get accessed through memory. Reviewed By: jdoerfert Differential Revision: https://reviews.llvm.org/D86724 --- llvm/lib/Transforms/Utils/BuildLibCalls.cpp | 29 +++++++++---- .../Transforms/InferFunctionAttrs/annotate.ll | 41 ++++++++++--------- 2 files changed, 44 insertions(+), 26 deletions(-) diff --git a/llvm/lib/Transforms/Utils/BuildLibCalls.cpp b/llvm/lib/Transforms/Utils/BuildLibCalls.cpp index 563597beed06..d4d2957efab4 100644 --- a/llvm/lib/Transforms/Utils/BuildLibCalls.cpp +++ b/llvm/lib/Transforms/Utils/BuildLibCalls.cpp @@ -186,6 +186,7 @@ bool llvm::inferLibFuncAttributes(Function &F, const TargetLibraryInfo &TLI) { return Changed; case LibFunc_strchr: case LibFunc_strrchr: + Changed |= setOnlyAccessesArgMemory(F); Changed |= setOnlyReadsMemory(F); Changed |= setDoesNotThrow(F); return Changed; @@ -225,9 +226,17 @@ bool llvm::inferLibFuncAttributes(Function &F, const TargetLibraryInfo &TLI) { case LibFunc_strspn: // 0,1 case LibFunc_strncmp: // 0,1 case LibFunc_strcspn: // 0,1 - case LibFunc_strcoll: // 0,1 + Changed |= setDoesNotThrow(F); + Changed |= setOnlyAccessesArgMemory(F); + Changed |= setOnlyReadsMemory(F); + Changed |= setDoesNotCapture(F, 0); + Changed |= setDoesNotCapture(F, 1); + return Changed; + case LibFunc_strcoll: case LibFunc_strcasecmp: // 0,1 case LibFunc_strncasecmp: // + // Those functions may depend on the locale, which may be accessed through + // global memory. Changed |= setOnlyReadsMemory(F); Changed |= setDoesNotThrow(F); Changed |= setDoesNotCapture(F, 0); @@ -310,6 +319,7 @@ bool llvm::inferLibFuncAttributes(Function &F, const TargetLibraryInfo &TLI) { Changed |= setRetDoesNotAlias(F); return Changed; case LibFunc_memcmp: + Changed |= setOnlyAccessesArgMemory(F); Changed |= setOnlyReadsMemory(F); Changed |= setDoesNotThrow(F); Changed |= setDoesNotCapture(F, 0); @@ -317,8 +327,9 @@ bool llvm::inferLibFuncAttributes(Function &F, const TargetLibraryInfo &TLI) { return Changed; case LibFunc_memchr: case LibFunc_memrchr: - Changed |= setOnlyReadsMemory(F); Changed |= setDoesNotThrow(F); + Changed |= setOnlyAccessesArgMemory(F); + Changed |= setOnlyReadsMemory(F); return Changed; case LibFunc_modf: case LibFunc_modff: @@ -327,24 +338,27 @@ bool llvm::inferLibFuncAttributes(Function &F, const TargetLibraryInfo &TLI) { Changed |= setDoesNotCapture(F, 1); return Changed; case LibFunc_memcpy: - Changed |= setDoesNotAlias(F, 0); - Changed |= setDoesNotAlias(F, 1); - Changed |= setReturnedArg(F, 0); Changed |= setDoesNotThrow(F); + Changed |= setOnlyAccessesArgMemory(F); + Changed |= setDoesNotAlias(F, 0); + Changed |= setReturnedArg(F, 0); + Changed |= setDoesNotAlias(F, 1); Changed |= setDoesNotCapture(F, 1); Changed |= setOnlyReadsMemory(F, 1); return Changed; case LibFunc_memmove: - Changed |= setReturnedArg(F, 0); Changed |= setDoesNotThrow(F); + Changed |= setOnlyAccessesArgMemory(F); + Changed |= setReturnedArg(F, 0); Changed |= setDoesNotCapture(F, 1); Changed |= setOnlyReadsMemory(F, 1); return Changed; case LibFunc_mempcpy: case LibFunc_memccpy: + Changed |= setDoesNotThrow(F); + Changed |= setOnlyAccessesArgMemory(F); Changed |= setDoesNotAlias(F, 0); Changed |= setDoesNotAlias(F, 1); - Changed |= setDoesNotThrow(F); Changed |= setDoesNotCapture(F, 1); Changed |= setOnlyReadsMemory(F, 1); return Changed; @@ -416,6 +430,7 @@ bool llvm::inferLibFuncAttributes(Function &F, const TargetLibraryInfo &TLI) { return Changed; case LibFunc_bcmp: Changed |= setDoesNotThrow(F); + Changed |= setOnlyAccessesArgMemory(F); Changed |= setOnlyReadsMemory(F); Changed |= setDoesNotCapture(F, 0); Changed |= setDoesNotCapture(F, 1); diff --git a/llvm/test/Transforms/InferFunctionAttrs/annotate.ll b/llvm/test/Transforms/InferFunctionAttrs/annotate.ll index 371723011c85..85c6e35266b7 100644 --- a/llvm/test/Transforms/InferFunctionAttrs/annotate.ll +++ b/llvm/test/Transforms/InferFunctionAttrs/annotate.ll @@ -241,7 +241,7 @@ declare i64 @atol(i8*) ; CHECK: declare i64 @atoll(i8* nocapture) [[G2]] declare i64 @atoll(i8*) -; CHECK-LINUX: declare i32 @bcmp(i8* nocapture, i8* nocapture, i64) [[G2]] +; CHECK-LINUX: declare i32 @bcmp(i8* nocapture, i8* nocapture, i64) [[ARGMEMONLY_NOFREE_NOUNWIND_READONLY:#[0-9]+]] ; CHECK-DARWIN-NOT: declare i32 @bcmp(i8* nocapture, i8* nocapture, i64) [[G2]] ; CHECK-UNKNOWN-NOT: declare i32 @bcmp(i8* nocapture, i8* nocapture, i64) [[G2]] ; CHECK-NVPTX-NOT: declare i32 @bcmp(i8* nocapture, i8* nocapture, i64) [[G2]] @@ -619,22 +619,24 @@ declare i8* @malloc(i64) ; CHECK-LINUX: declare noalias i8* @memalign(i64, i64) [[G0]] declare i8* @memalign(i64, i64) -; CHECK: declare i8* @memccpy(i8* noalias, i8* noalias nocapture readonly, i32, i64) [[G1]] +; CHECK: declare i8* @memccpy(i8* noalias, i8* noalias nocapture readonly, i32, i64) [[ARGMEMONLY_NOFREE_NOUNWIND:#[0-9]+]] declare i8* @memccpy(i8*, i8*, i32, i64) -; CHECK: declare i8* @memchr(i8*, i32, i64) [[G2]] +; CHECK-LINUX: declare i8* @memchr(i8*, i32, i64) [[ARGMEMONLY_NOFREE_NOUNWIND_READONLY]] +; CHECK-DARWIN: declare i8* @memchr(i8*, i32, i64) [[ARGMEMONLY_NOFREE_NOUNWIND_READONLY:#[0-9]+]] +; CHECK-UNKNOWN: declare i8* @memchr(i8*, i32, i64) [[ARGMEMONLY_NOFREE_NOUNWIND_READONLY:#[0-9]+]] declare i8* @memchr(i8*, i32, i64) -; CHECK: declare i32 @memcmp(i8* nocapture, i8* nocapture, i64) [[G2]] +; CHECK: declare i32 @memcmp(i8* nocapture, i8* nocapture, i64) [[ARGMEMONLY_NOFREE_NOUNWIND_READONLY]] declare i32 @memcmp(i8*, i8*, i64) -; CHECK: declare i8* @memcpy(i8* noalias returned, i8* noalias nocapture readonly, i64) [[G1]] +; CHECK: declare i8* @memcpy(i8* noalias returned, i8* noalias nocapture readonly, i64) [[ARGMEMONLY_NOFREE_NOUNWIND]] declare i8* @memcpy(i8*, i8*, i64) -; CHECK: declare i8* @mempcpy(i8* noalias, i8* noalias nocapture readonly, i64) [[G1]] +; CHECK: declare i8* @mempcpy(i8* noalias, i8* noalias nocapture readonly, i64) [[ARGMEMONLY_NOFREE_NOUNWIND]] declare i8* @mempcpy(i8*, i8*, i64) -; CHECK: declare i8* @memmove(i8* returned, i8* nocapture readonly, i64) [[G1]] +; CHECK: declare i8* @memmove(i8* returned, i8* nocapture readonly, i64) [[ARGMEMONLY_NOFREE_NOUNWIND]] declare i8* @memmove(i8*, i8*, i64) ; CHECK: declare i8* @memset(i8*, i32, i64) [[G0]] @@ -835,10 +837,10 @@ declare i32 @strcasecmp(i8*, i8*) ; CHECK: declare i8* @strcat(i8* returned, i8* nocapture readonly) [[G1]] declare i8* @strcat(i8*, i8*) -; CHECK: declare i8* @strchr(i8*, i32) [[G2]] +; CHECK: declare i8* @strchr(i8*, i32) [[ARGMEMONLY_NOFREE_NOUNWIND_READONLY]] declare i8* @strchr(i8*, i32) -; CHECK: declare i32 @strcmp(i8* nocapture, i8* nocapture) [[G2]] +; CHECK: declare i32 @strcmp(i8* nocapture, i8* nocapture) [[ARGMEMONLY_NOFREE_NOUNWIND_READONLY]] declare i32 @strcmp(i8*, i8*) ; CHECK: declare i32 @strcoll(i8* nocapture, i8* nocapture) [[G2]] @@ -847,13 +849,13 @@ declare i32 @strcoll(i8*, i8*) ; CHECK: declare i8* @strcpy(i8* noalias returned, i8* noalias nocapture readonly) [[G1]] declare i8* @strcpy(i8*, i8*) -; CHECK: declare i64 @strcspn(i8* nocapture, i8* nocapture) [[G2]] +; CHECK: declare i64 @strcspn(i8* nocapture, i8* nocapture) [[ARGMEMONLY_NOFREE_NOUNWIND_READONLY]] declare i64 @strcspn(i8*, i8*) ; CHECK: declare noalias i8* @strdup(i8* nocapture readonly) [[G1]] declare i8* @strdup(i8*) -; CHECK: declare i64 @strlen(i8* nocapture) [[G4:#[0-9]+]] +; CHECK: declare i64 @strlen(i8* nocapture) [[ARGMEMONLY_NOFREE_NOUNWIND_READONLY]] declare i64 @strlen(i8*) ; CHECK: declare i32 @strncasecmp(i8* nocapture, i8* nocapture, i64) [[G2]] @@ -862,7 +864,7 @@ declare i32 @strncasecmp(i8*, i8*, i64) ; CHECK: declare i8* @strncat(i8* returned, i8* nocapture readonly, i64) [[G1]] declare i8* @strncat(i8*, i8*, i64) -; CHECK: declare i32 @strncmp(i8* nocapture, i8* nocapture, i64) [[G2]] +; CHECK: declare i32 @strncmp(i8* nocapture, i8* nocapture, i64) [[ARGMEMONLY_NOFREE_NOUNWIND_READONLY]] declare i32 @strncmp(i8*, i8*, i64) ; CHECK: declare i8* @strncpy(i8* noalias returned, i8* noalias nocapture readonly, i64) [[G1]] @@ -877,10 +879,10 @@ declare i64 @strnlen(i8*, i64) ; CHECK: declare i8* @strpbrk(i8*, i8* nocapture) [[G2]] declare i8* @strpbrk(i8*, i8*) -; CHECK: declare i8* @strrchr(i8*, i32) [[G2]] +; CHECK: declare i8* @strrchr(i8*, i32) [[ARGMEMONLY_NOFREE_NOUNWIND_READONLY]] declare i8* @strrchr(i8*, i32) -; CHECK: declare i64 @strspn(i8* nocapture, i8* nocapture) [[G2]] +; CHECK: declare i64 @strspn(i8* nocapture, i8* nocapture) [[ARGMEMONLY_NOFREE_NOUNWIND_READONLY]] declare i64 @strspn(i8*, i8*) ; CHECK: declare i8* @strstr(i8*, i8* nocapture) [[G2]] @@ -1005,12 +1007,13 @@ declare i64 @write(i32, i8*, i64) ; memset_pattern16 isn't available everywhere. -; CHECK-DARWIN: declare void @memset_pattern16(i8* nocapture, i8* nocapture readonly, i64) [[G5:#[0-9]+]] +; CHECK-DARWIN: declare void @memset_pattern16(i8* nocapture, i8* nocapture readonly, i64) [[G6:#[0-9]+]] declare void @memset_pattern16(i8*, i8*, i64) ; CHECK: attributes [[G0]] = { nofree } ; CHECK: attributes [[G1]] = { nofree nounwind } -; CHECK: attributes [[G2]] = { nofree nounwind readonly } -; CHECK: attributes [[G3]] = { nounwind } -; CHECK: attributes [[G4]] = { argmemonly nofree nounwind readonly } -; CHECK-DARWIN: attributes [[G5]] = { argmemonly nofree } +; CHECK-DAG: attributes [[G2]] = { nofree nounwind readonly } +; CHECK-DAG: attributes [[ARGMEMONLY_NOFREE_NOUNWIND_READONLY]] = { argmemonly nofree nounwind readonly } +; CHECK-DAG: attributes [[G3]] = { nounwind } +; CHECK-DAG: attributes [[ARGMEMONLY_NOFREE_NOUNWIND]] = { argmemonly nofree nounwind } +; CHECK-DARWIN: attributes [[G6]] = { argmemonly nofree }