[clang] 4df38a5 - [X86] Optimize out a few extra strlen calls in getX86TargetCPU. NFCI

Craig Topper via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 6 13:18:43 PDT 2020


Author: Craig Topper
Date: 2020-08-06T13:18:15-07:00
New Revision: 4df38a5589f6fa23e161a76bdaa3180ad053791e

URL: https://github.com/llvm/llvm-project/commit/4df38a5589f6fa23e161a76bdaa3180ad053791e
DIFF: https://github.com/llvm/llvm-project/commit/4df38a5589f6fa23e161a76bdaa3180ad053791e.diff

LOG: [X86] Optimize out a few extra strlen calls in getX86TargetCPU. NFCI

We had a conversion from const char * to StringRef and const char *
to std::string conversion. These both do their own
strlen call if the compiler doens't figure out how to share them.
By adding the temporary StringRef we can convert it to std::string
instead.

The other case is to use a StringSwitch<StringRef> instead of
StringSwitch<const char *> since the output values of the switch
are string literals. This allows the length to be computed at
compile time. Otherwise we have to convert from const char *
to std::string after the StringSwitch.

Added: 
    

Modified: 
    clang/lib/Driver/ToolChains/Arch/X86.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Driver/ToolChains/Arch/X86.cpp b/clang/lib/Driver/ToolChains/Arch/X86.cpp
index 5d9a7234b027..c49aaadc42ae 100644
--- a/clang/lib/Driver/ToolChains/Arch/X86.cpp
+++ b/clang/lib/Driver/ToolChains/Arch/X86.cpp
@@ -23,41 +23,42 @@ using namespace llvm::opt;
 std::string x86::getX86TargetCPU(const ArgList &Args,
                                  const llvm::Triple &Triple) {
   if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_march_EQ)) {
-    if (StringRef(A->getValue()) != "native")
-      return A->getValue();
+    StringRef CPU = A->getValue();
+    if (CPU != "native")
+      return std::string(CPU);
 
     // FIXME: Reject attempts to use -march=native unless the target matches
     // the host.
     //
     // FIXME: We should also incorporate the detected target features for use
     // with -native.
-    std::string CPU = std::string(llvm::sys::getHostCPUName());
+    CPU = llvm::sys::getHostCPUName();
     if (!CPU.empty() && CPU != "generic")
-      return CPU;
+      return std::string(CPU);
   }
 
   if (const Arg *A = Args.getLastArgNoClaim(options::OPT__SLASH_arch)) {
     // Mapping built by looking at lib/Basic's X86TargetInfo::initFeatureMap().
     StringRef Arch = A->getValue();
-    const char *CPU = nullptr;
+    StringRef CPU;
     if (Triple.getArch() == llvm::Triple::x86) {  // 32-bit-only /arch: flags.
-      CPU = llvm::StringSwitch<const char *>(Arch)
+      CPU = llvm::StringSwitch<StringRef>(Arch)
                 .Case("IA32", "i386")
                 .Case("SSE", "pentium3")
                 .Case("SSE2", "pentium4")
-                .Default(nullptr);
+                .Default("");
     }
-    if (CPU == nullptr) {  // 32-bit and 64-bit /arch: flags.
-      CPU = llvm::StringSwitch<const char *>(Arch)
+    if (CPU.empty()) {  // 32-bit and 64-bit /arch: flags.
+      CPU = llvm::StringSwitch<StringRef>(Arch)
                 .Case("AVX", "sandybridge")
                 .Case("AVX2", "haswell")
                 .Case("AVX512F", "knl")
                 .Case("AVX512", "skylake-avx512")
-                .Default(nullptr);
+                .Default("");
     }
-    if (CPU) {
+    if (!CPU.empty()) {
       A->claim();
-      return CPU;
+      return std::string(CPU);
     }
   }
 


        


More information about the cfe-commits mailing list