Add an error when calling a builtin that requires features that don't
match the feature set of the function that they're being called from.

This ensures that we can effectively diagnose some[1] code that would
instead ICE in the backend with a failure to select message.

Example:

__m128d foo(__m128d a, __m128d b) {
  return __builtin_ia32_addsubps(b, a);
}

compiled for normal x86_64 via:

clang -target x86_64-linux-gnu -c

would fail to compile in the back end because the normal subtarget
features for x86_64 only include sse2 and the builtin requires sse3.

[1] We're still not erroring on:

__m128i bar(__m128i const *p) { return _mm_lddqu_si128(p); }

where we should fail and error on an always_inline function being
inlined into a function that doesn't support the subtarget features
required.

llvm-svn: 250473
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index bcc6ac9..6dff363 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -21,6 +21,7 @@
 #include "clang/Basic/TargetBuiltins.h"
 #include "clang/Basic/TargetInfo.h"
 #include "clang/CodeGen/CGFunctionInfo.h"
+#include "clang/Sema/SemaDiagnostic.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/IR/CallSite.h"
 #include "llvm/IR/DataLayout.h"
@@ -288,6 +289,62 @@
   return Builder.CreateCall(CGM.getIntrinsic(inst), ArgValue);
 }
 
+// Returns true if we have a valid set of target features.
+bool CodeGenFunction::checkBuiltinTargetFeatures(
+    const FunctionDecl *TargetDecl) {
+  // Early exit if this is an indirect call.
+  if (!TargetDecl)
+    return true;
+
+  // Get the current enclosing function if it exists.
+  if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {
+    unsigned BuiltinID = TargetDecl->getBuiltinID();
+    const char *FeatureList =
+        CGM.getContext().BuiltinInfo.getRequiredFeatures(BuiltinID);
+    if (FeatureList && StringRef(FeatureList) != "") {
+      StringRef TargetCPU = Target.getTargetOpts().CPU;
+      llvm::StringMap<bool> FeatureMap;
+
+      if (const auto *TD = FD->getAttr<TargetAttr>()) {
+        // If we have a TargetAttr build up the feature map based on that.
+        TargetAttr::ParsedTargetAttr ParsedAttr = TD->parse();
+
+        // Make a copy of the features as passed on the command line into the
+        // beginning of the additional features from the function to override.
+        ParsedAttr.first.insert(
+            ParsedAttr.first.begin(),
+            Target.getTargetOpts().FeaturesAsWritten.begin(),
+            Target.getTargetOpts().FeaturesAsWritten.end());
+
+        if (ParsedAttr.second != "")
+          TargetCPU = ParsedAttr.second;
+
+        // Now populate the feature map, first with the TargetCPU which is
+        // either
+        // the default or a new one from the target attribute string. Then we'll
+        // use the passed in features (FeaturesAsWritten) along with the new
+        // ones
+        // from the attribute.
+        Target.initFeatureMap(FeatureMap, CGM.getDiags(), TargetCPU,
+                              ParsedAttr.first);
+      } else {
+        Target.initFeatureMap(FeatureMap, CGM.getDiags(), TargetCPU,
+                              Target.getTargetOpts().Features);
+      }
+
+      // If we have at least one of the features in the feature list return
+      // true, otherwise return false.
+      SmallVector<StringRef, 1> AttrFeatures;
+      StringRef(FeatureList).split(AttrFeatures, ",");
+      for (const auto &Feature : AttrFeatures)
+        if (FeatureMap[Feature])
+	  return true;
+      return false;
+    }
+  }
+  return true;
+}
+
 RValue CodeGenFunction::EmitBuiltinExpr(const FunctionDecl *FD,
                                         unsigned BuiltinID, const CallExpr *E,
                                         ReturnValueSlot ReturnValue) {
@@ -1789,6 +1846,16 @@
   if (getContext().BuiltinInfo.isPredefinedLibFunction(BuiltinID))
     return emitLibraryCall(*this, FD, E, EmitScalarExpr(E->getCallee()));
 
+  // Check that a call to a target specific builtin has the correct target
+  // features.
+  // This is down here to avoid non-target specific builtins, however, if
+  // generic builtins start to require generic target features then we
+  // can move this up to the beginning of the function.
+  if (!checkBuiltinTargetFeatures(FD))
+    CGM.getDiags().Report(E->getLocStart(), diag::err_builtin_needs_feature)
+        << FD->getDeclName()
+        << CGM.getContext().BuiltinInfo.getRequiredFeatures(BuiltinID);
+
   // See if we have a target specific intrinsic.
   const char *Name = getContext().BuiltinInfo.getName(BuiltinID);
   Intrinsic::ID IntrinsicID = Intrinsic::not_intrinsic;
diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp
index 60770b1..bb7fdca 100644
--- a/clang/lib/CodeGen/CGCall.cpp
+++ b/clang/lib/CodeGen/CGCall.cpp
@@ -21,6 +21,7 @@
 #include "clang/AST/Decl.h"
 #include "clang/AST/DeclCXX.h"
 #include "clang/AST/DeclObjC.h"
+#include "clang/Basic/TargetBuiltins.h"
 #include "clang/Basic/TargetInfo.h"
 #include "clang/CodeGen/CGFunctionInfo.h"
 #include "clang/Frontend/CodeGenOptions.h"
diff --git a/clang/lib/CodeGen/CodeGenFunction.h b/clang/lib/CodeGen/CodeGenFunction.h
index 390e346..d67e6f6 100644
--- a/clang/lib/CodeGen/CodeGenFunction.h
+++ b/clang/lib/CodeGen/CodeGenFunction.h
@@ -2633,6 +2633,8 @@
   RValue EmitCallExpr(const CallExpr *E,
                       ReturnValueSlot ReturnValue = ReturnValueSlot());
 
+  bool checkBuiltinTargetFeatures(const FunctionDecl *TargetDecl);
+
   llvm::CallInst *EmitRuntimeCall(llvm::Value *callee,
                                   const Twine &name = "");
   llvm::CallInst *EmitRuntimeCall(llvm::Value *callee,