[PATCH] D32782: Add pthread_self function prototype and make it speculatable.

Xin Tong via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue May 2 20:14:21 PDT 2017


trentxintong created this revision.
Herald added a subscriber: wdng.

This allows pthread_self to be pulled out of a loop by LICM.


https://reviews.llvm.org/D32782

Files:
  include/llvm/Analysis/TargetLibraryInfo.def
  lib/Analysis/TargetLibraryInfo.cpp
  lib/Transforms/Utils/BuildLibCalls.cpp
  unittests/Analysis/TargetLibraryInfoTest.cpp


Index: unittests/Analysis/TargetLibraryInfoTest.cpp
===================================================================
--- unittests/Analysis/TargetLibraryInfoTest.cpp
+++ unittests/Analysis/TargetLibraryInfoTest.cpp
@@ -246,6 +246,7 @@
     "declare float @powf(float, float)\n"
     "declare x86_fp80 @powl(x86_fp80, x86_fp80)\n"
     "declare i32 @printf(i8*, ...)\n"
+    "declare i64 @pthread_self()\n"
     "declare i32 @putc(i32, %struct*)\n"
     "declare i32 @putchar(i32)\n"
     "declare i32 @puts(i8*)\n"
Index: lib/Transforms/Utils/BuildLibCalls.cpp
===================================================================
--- lib/Transforms/Utils/BuildLibCalls.cpp
+++ lib/Transforms/Utils/BuildLibCalls.cpp
@@ -38,6 +38,7 @@
 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 @@
   return true;
 }
 
+static bool setSpeculatable(Function &F) {
+  if (F.isSpeculatable())
+    return false;
+  F.setSpeculatable();
+  ++NumSpeculatable;
+  return true;
+}
+
 static bool setDoesNotCapture(Function &F, unsigned n) {
   if (F.doesNotCapture(n))
     return false;
@@ -530,6 +539,9 @@
     Changed |= setOnlyReadsMemory(F, 1);
     Changed |= setOnlyReadsMemory(F, 2);
     return Changed;
+  case LibFunc_pthread_self:
+    Changed |= setSpeculatable(F);
+    return Changed;
   case LibFunc_vfscanf:
     Changed |= setDoesNotThrow(F);
     Changed |= setDoesNotCapture(F, 1);
Index: lib/Analysis/TargetLibraryInfo.cpp
===================================================================
--- lib/Analysis/TargetLibraryInfo.cpp
+++ lib/Analysis/TargetLibraryInfo.cpp
@@ -304,6 +304,9 @@
     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()) {
@@ -1176,6 +1179,9 @@
             FTy.getParamType(0)->isPointerTy() &&
             FTy.getParamType(1) == SizeTTy && FTy.getParamType(2) == SizeTTy);
 
+  case LibFunc_pthread_self:
+    return NumParams == 0 && FTy.getReturnType()->isIntegerTy();
+
   case LibFunc::NumLibFuncs:
     break;
   }
Index: include/llvm/Analysis/TargetLibraryInfo.def
===================================================================
--- include/llvm/Analysis/TargetLibraryInfo.def
+++ include/llvm/Analysis/TargetLibraryInfo.def
@@ -813,6 +813,9 @@
 /// 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")


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D32782.97541.patch
Type: text/x-patch
Size: 3070 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170503/bca07ad2/attachment.bin>


More information about the llvm-commits mailing list