From 75af3af95780e1c379409bf56c516a272c4fa961 Mon Sep 17 00:00:00 2001 From: Xin Tong Date: Sat, 20 May 2017 22:40:25 +0000 Subject: [PATCH] Add pthread_self function prototype and make it speculatable. Summary: This allows pthread_self to be pulled out of a loop by LICM. Reviewers: hfinkel, arsenm, davide Reviewed By: davide Subscribers: davide, wdng, llvm-commits Differential Revision: https://reviews.llvm.org/D32782 llvm-svn: 303495 --- .../llvm/Analysis/TargetLibraryInfo.def | 3 ++ llvm/lib/Analysis/TargetLibraryInfo.cpp | 9 +++++ llvm/lib/Transforms/Utils/BuildLibCalls.cpp | 12 ++++++ llvm/test/Transforms/LICM/pthread.ll | 38 +++++++++++++++++++ .../Analysis/TargetLibraryInfoTest.cpp | 8 ++-- 5 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 llvm/test/Transforms/LICM/pthread.ll diff --git a/llvm/include/llvm/Analysis/TargetLibraryInfo.def b/llvm/include/llvm/Analysis/TargetLibraryInfo.def index 9cbe917c146d..be6685722d90 100644 --- a/llvm/include/llvm/Analysis/TargetLibraryInfo.def +++ b/llvm/include/llvm/Analysis/TargetLibraryInfo.def @@ -938,6 +938,9 @@ TLI_DEFINE_STRING_INTERNAL("pread") /// int printf(const char *format, ...); TLI_DEFINE_ENUM_INTERNAL(printf) TLI_DEFINE_STRING_INTERNAL("printf") +/// pthread_t pthread_self(void); +TLI_DEFINE_ENUM_INTERNAL(pthread_self) +TLI_DEFINE_STRING_INTERNAL("pthread_self") /// int putc(int c, FILE *stream); TLI_DEFINE_ENUM_INTERNAL(putc) TLI_DEFINE_STRING_INTERNAL("putc") diff --git a/llvm/lib/Analysis/TargetLibraryInfo.cpp b/llvm/lib/Analysis/TargetLibraryInfo.cpp index 2be5d5caf7c2..b12778278ce8 100644 --- a/llvm/lib/Analysis/TargetLibraryInfo.cpp +++ b/llvm/lib/Analysis/TargetLibraryInfo.cpp @@ -349,6 +349,9 @@ static void initialize(TargetLibraryInfoImpl &TLI, const Triple &T, TLI.setUnavailable(LibFunc_atoll); TLI.setUnavailable(LibFunc_frexpf); TLI.setUnavailable(LibFunc_llabs); + + // Win32 does *not* provide pthread_self. + TLI.setUnavailable(LibFunc_pthread_self); } switch (T.getOS()) { @@ -1263,6 +1266,12 @@ bool TargetLibraryInfoImpl::isValidProtoForLibFunc(const FunctionType &FTy, FTy.getParamType(0)->isPointerTy() && FTy.getParamType(1) == SizeTTy && FTy.getParamType(2) == SizeTTy); + // We do not attempt to match the return value here. i.e. thread identifiers + // should be considered opaque, for example, representation using either an + // arithmetic type or a structure is permitted. + case LibFunc_pthread_self: + return NumParams == 0; + case LibFunc_wcslen: return (NumParams == 1 && FTy.getParamType(0)->isPointerTy() && FTy.getReturnType()->isIntegerTy()); diff --git a/llvm/lib/Transforms/Utils/BuildLibCalls.cpp b/llvm/lib/Transforms/Utils/BuildLibCalls.cpp index ebde1f9a17dd..cf3453252548 100644 --- a/llvm/lib/Transforms/Utils/BuildLibCalls.cpp +++ b/llvm/lib/Transforms/Utils/BuildLibCalls.cpp @@ -38,6 +38,7 @@ STATISTIC(NumNoCapture, "Number of arguments inferred as nocapture"); STATISTIC(NumReadOnlyArg, "Number of arguments inferred as readonly"); STATISTIC(NumNoAlias, "Number of function returns inferred as noalias"); STATISTIC(NumNonNull, "Number of function returns inferred as nonnull returns"); +STATISTIC(NumSpeculatable, "Number of functions inferred as speculatable"); static bool setDoesNotAccessMemory(Function &F) { if (F.doesNotAccessMemory()) @@ -71,6 +72,14 @@ static bool setDoesNotThrow(Function &F) { return true; } +static bool setSpeculatable(Function &F) { + if (F.isSpeculatable()) + return false; + F.setSpeculatable(); + ++NumSpeculatable; + return true; +} + static bool setRetDoesNotAlias(Function &F) { if (F.hasAttribute(AttributeList::ReturnIndex, Attribute::NoAlias)) return false; @@ -530,6 +539,9 @@ bool llvm::inferLibFuncAttributes(Function &F, const TargetLibraryInfo &TLI) { Changed |= setOnlyReadsMemory(F, 0); Changed |= setOnlyReadsMemory(F, 1); return Changed; + case LibFunc_pthread_self: + Changed |= setSpeculatable(F); + return Changed; case LibFunc_vfscanf: Changed |= setDoesNotThrow(F); Changed |= setDoesNotCapture(F, 0); diff --git a/llvm/test/Transforms/LICM/pthread.ll b/llvm/test/Transforms/LICM/pthread.ll new file mode 100644 index 000000000000..6a7aa499b5a1 --- /dev/null +++ b/llvm/test/Transforms/LICM/pthread.ll @@ -0,0 +1,38 @@ +; RUN: opt < %s -S -inferattrs -licm | FileCheck %s + +; CHECK-LABEL: define void @pthread_self_safe( +; CHECK-NEXT: call i64 @pthread_self() +define void @pthread_self_safe(i32) { + br label %2 + +;