[llvm] r314185 - TargetLibraryInfo: Stop guessing wchar_t size

Matthias Braun via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 25 19:36:57 PDT 2017


Author: matze
Date: Mon Sep 25 19:36:57 2017
New Revision: 314185

URL: http://llvm.org/viewvc/llvm-project?rev=314185&view=rev
Log:
TargetLibraryInfo: Stop guessing wchar_t size

Usually the frontend communicates the size of wchar_t via metadata and
we can optimize wcslen (and possibly other calls in the future). In
cases without the wchar_size metadata we would previously try to guess
the correct size based on the target triple; however this is fragile to
keep up to date and may miss users manually changing the size via flags.
Better be safe and stop guessing and optimizing if the frontend didn't
communicate the size.

Differential Revision: https://reviews.llvm.org/D38106

Added:
    llvm/trunk/test/Transforms/InstCombine/wcslen-4.ll
      - copied, changed from r314183, llvm/trunk/test/Transforms/InstCombine/wcslen-2.ll
Modified:
    llvm/trunk/include/llvm/Analysis/TargetLibraryInfo.h
    llvm/trunk/lib/Analysis/TargetLibraryInfo.cpp
    llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp
    llvm/trunk/test/Transforms/InstCombine/wcslen-1.ll
    llvm/trunk/test/Transforms/InstCombine/wcslen-2.ll

Modified: llvm/trunk/include/llvm/Analysis/TargetLibraryInfo.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Analysis/TargetLibraryInfo.h?rev=314185&r1=314184&r2=314185&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Analysis/TargetLibraryInfo.h (original)
+++ llvm/trunk/include/llvm/Analysis/TargetLibraryInfo.h Mon Sep 25 19:36:57 2017
@@ -193,13 +193,9 @@ public:
     ShouldSignExtI32Param = Val;
   }
 
-  /// Returns the size of the wchar_t type in bytes.
+  /// Returns the size of the wchar_t type in bytes or 0 if the size is unknown.
+  /// This queries the 'wchar_size' metadata.
   unsigned getWCharSize(const Module &M) const;
-
-  /// Returns size of the default wchar_t type on target \p T. This is mostly
-  /// intended to verify that the size in the frontend matches LLVM. All other
-  /// queries should use getWCharSize() instead.
-  static unsigned getTargetWCharSize(const Triple &T);
 };
 
 /// Provides information about what library functions are available for

Modified: llvm/trunk/lib/Analysis/TargetLibraryInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/TargetLibraryInfo.cpp?rev=314185&r1=314184&r2=314185&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/TargetLibraryInfo.cpp (original)
+++ llvm/trunk/lib/Analysis/TargetLibraryInfo.cpp Mon Sep 25 19:36:57 2017
@@ -1519,20 +1519,11 @@ TargetLibraryInfoImpl &TargetLibraryAnal
   return *Impl;
 }
 
-unsigned TargetLibraryInfoImpl::getTargetWCharSize(const Triple &T) {
-  // See also clang/lib/Basic/Targets.cpp.
-  if (T.isPS4() || T.isOSWindows() || T.isArch16Bit())
-    return 2;
-  if (T.getArch() == Triple::xcore)
-    return 1;
-  return 4;
-}
-
 unsigned TargetLibraryInfoImpl::getWCharSize(const Module &M) const {
   if (auto *ShortWChar = cast_or_null<ConstantAsMetadata>(
       M.getModuleFlag("wchar_size")))
     return cast<ConstantInt>(ShortWChar->getValue())->getZExtValue();
-  return getTargetWCharSize(Triple(M.getTargetTriple()));
+  return 0;
 }
 
 TargetLibraryInfoWrapperPass::TargetLibraryInfoWrapperPass()

Modified: llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp?rev=314185&r1=314184&r2=314185&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp Mon Sep 25 19:36:57 2017
@@ -508,6 +508,9 @@ Value *LibCallSimplifier::optimizeStrLen
 Value *LibCallSimplifier::optimizeWcslen(CallInst *CI, IRBuilder<> &B) {
   Module &M = *CI->getParent()->getParent()->getParent();
   unsigned WCharSize = TLI->getWCharSize(M) * 8;
+  // We cannot perform this optimization without wchar_size metadata.
+  if (WCharSize == 0)
+    return nullptr;
 
   return optimizeStringLength(CI, B, WCharSize);
 }

Modified: llvm/trunk/test/Transforms/InstCombine/wcslen-1.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/wcslen-1.ll?rev=314185&r1=314184&r2=314185&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/wcslen-1.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/wcslen-1.ll Mon Sep 25 19:36:57 2017
@@ -7,6 +7,9 @@ target datalayout = "e-m:o-i64:64-f80:12
 
 declare i64 @wcslen(i32*)
 
+!0 = !{i32 1, !"wchar_size", i32 4}
+!llvm.module.flags = !{!0}
+
 @hello = constant [6 x i32] [i32 104, i32 101, i32 108, i32 108, i32 111, i32 0]
 @longer = constant [7 x i32] [i32 108, i32 111, i32 110, i32 103, i32 101, i32 114, i32 0]
 @null = constant [1 x i32] zeroinitializer

Modified: llvm/trunk/test/Transforms/InstCombine/wcslen-2.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/wcslen-2.ll?rev=314185&r1=314184&r2=314185&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/wcslen-2.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/wcslen-2.ll Mon Sep 25 19:36:57 2017
@@ -4,6 +4,9 @@
 
 target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
 
+!0 = !{i32 1, !"wchar_size", i32 4}
+!llvm.module.flags = !{!0}
+
 @hello = constant [6 x i32] [i32 104, i32 101, i32 108, i32 108, i32 111, i32 0]
 
 declare i64 @wcslen(i32*, i32)

Copied: llvm/trunk/test/Transforms/InstCombine/wcslen-4.ll (from r314183, llvm/trunk/test/Transforms/InstCombine/wcslen-2.ll)
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/wcslen-4.ll?p2=llvm/trunk/test/Transforms/InstCombine/wcslen-4.ll&p1=llvm/trunk/test/Transforms/InstCombine/wcslen-2.ll&r1=314183&r2=314185&rev=314185&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/wcslen-2.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/wcslen-4.ll Mon Sep 25 19:36:57 2017
@@ -4,15 +4,17 @@
 
 target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
 
+; Without the wchar_size metadata we should see no optimization happening.
+
 @hello = constant [6 x i32] [i32 104, i32 101, i32 108, i32 108, i32 111, i32 0]
 
-declare i64 @wcslen(i32*, i32)
+declare i64 @wcslen(i32*)
 
 define i64 @test_no_simplify1() {
 ; CHECK-LABEL: @test_no_simplify1(
+; CHECK-NEXT: %hello_l = call i64 @wcslen(i32* getelementptr inbounds ([6 x i32], [6 x i32]* @hello, i64 0, i64 0))
+; CHECK-NEXT: ret i64 %hello_l
   %hello_p = getelementptr [6 x i32], [6 x i32]* @hello, i64 0, i64 0
-  %hello_l = call i64 @wcslen(i32* %hello_p, i32 187)
-; CHECK-NEXT: %hello_l = call i64 @wcslen
+  %hello_l = call i64 @wcslen(i32* %hello_p)
   ret i64 %hello_l
-; CHECK-NEXT: ret i64 %hello_l
 }




More information about the llvm-commits mailing list