Add -mcpu=native support to opt.
authorCraig Topper <craig.topper@gmail.com>
Wed, 1 Apr 2015 05:32:04 +0000 (05:32 +0000)
committerCraig Topper <craig.topper@gmail.com>
Wed, 1 Apr 2015 05:32:04 +0000 (05:32 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@233789 91177308-0d34-0410-b5e6-96231b3b80d8

tools/opt/opt.cpp

index c64e907af22614378a916adb8d8fa21472fa928e..af86e2dbd601024bb8eae43ced958ecb8bb29e05 100644 (file)
@@ -39,6 +39,7 @@
 #include "llvm/IR/LegacyPassManager.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/FileSystem.h"
+#include "llvm/Support/Host.h"
 #include "llvm/Support/ManagedStatic.h"
 #include "llvm/Support/PluginLoader.h"
 #include "llvm/Support/PrettyStackTrace.h"
@@ -266,13 +267,28 @@ static TargetMachine* GetTargetMachine(Triple TheTriple) {
 
   // Package up features to be passed to target/subtarget
   std::string FeaturesStr;
-  if (MAttrs.size()) {
+  if (MAttrs.size() || MCPU == "native") {
     SubtargetFeatures Features;
+
+    // If user asked for the 'native' CPU, we need to autodetect features.
+    // This is necessary for x86 where the CPU might not support all the
+    // features the autodetected CPU name lists in the target. For example,
+    // not all Sandybridge processors support AVX.
+    if (MCPU == "native") {
+      StringMap<bool> HostFeatures;
+      if (sys::getHostCPUFeatures(HostFeatures))
+        for (auto &F : HostFeatures)
+          Features.AddFeature(F.first(), F.second);
+    }
+
     for (unsigned i = 0; i != MAttrs.size(); ++i)
       Features.AddFeature(MAttrs[i]);
     FeaturesStr = Features.getString();
   }
 
+  if (MCPU == "native")
+    MCPU = sys::getHostCPUName();
+
   return TheTarget->createTargetMachine(TheTriple.getTriple(),
                                         MCPU, FeaturesStr,
                                         InitTargetOptionsFromCodeGenFlags(),