diff --git a/include/clang/Basic/Builtins.h b/include/clang/Basic/Builtins.h
index ef8955c94b29cdaa384504192280f5d69b74622c..b66c0db464d41e9cfbce7dcd25fb45cd5d15c1e5 100644
--- a/include/clang/Basic/Builtins.h
+++ b/include/clang/Basic/Builtins.h
@@ -171,6 +171,10 @@ public:
 
 private:
   const Info &GetRecord(unsigned ID) const;
+
+  /// \brief Is this builtin supported according to the given language options?
+  bool BuiltinIsSupported(const Builtin::Info &BuiltinInfo,
+                          const LangOptions &LangOpts);
 };
 
 }
diff --git a/include/clang/Basic/LangOptions.def b/include/clang/Basic/LangOptions.def
index e8fe482fa76df49916598bc9a11491da12a435e7..5feac32da19aeabfa066706f2004ffd9490f1b62 100644
--- a/include/clang/Basic/LangOptions.def
+++ b/include/clang/Basic/LangOptions.def
@@ -85,6 +85,7 @@ LANGOPT(RTTI              , 1, 1, "run-time type information")
 LANGOPT(MSBitfields       , 1, 0, "Microsoft-compatible structure layout")
 LANGOPT(Freestanding, 1, 0, "freestanding implementation")
 LANGOPT(NoBuiltin         , 1, 0, "disable builtin functions")
+LANGOPT(NoMathBuiltin     , 1, 0, "disable math builtin functions")
 
 BENIGN_LANGOPT(ThreadsafeStatics , 1, 1, "thread-safe static initializers")
 LANGOPT(POSIXThreads      , 1, 0, "POSIX thread support")
diff --git a/include/clang/Driver/Options.td b/include/clang/Driver/Options.td
index 2035bf8b7683a103cbcd3fbc909e6fc256ccfd01..27a844a7908b825fe54aa1c58a32cc386058579f 100644
--- a/include/clang/Driver/Options.td
+++ b/include/clang/Driver/Options.td
@@ -568,6 +568,8 @@ def fno_builtin_strcat : Flag<["-"], "fno-builtin-strcat">, Group<f_Group>;
 def fno_builtin_strcpy : Flag<["-"], "fno-builtin-strcpy">, Group<f_Group>;
 def fno_builtin : Flag<["-"], "fno-builtin">, Group<f_Group>, Flags<[CC1Option]>,
   HelpText<"Disable implicit builtin knowledge of functions">;
+def fno_math_builtin : Flag<["-"], "fno-math-builtin">, Group<f_Group>, Flags<[CC1Option]>,
+  HelpText<"Disable implicit builtin knowledge of math functions">;
 def fno_caret_diagnostics : Flag<["-"], "fno-caret-diagnostics">, Group<f_Group>,
  Flags<[CC1Option]>;
 def fno_color_diagnostics : Flag<["-"], "fno-color-diagnostics">, Group<f_Group>;
diff --git a/lib/Basic/Builtins.cpp b/lib/Basic/Builtins.cpp
index d8aaa6e8c9e163d7325de2964e436ec9dd2ac335..e5775b687ce7e8402e8f0ff983a5bbef78e7e9cb 100644
--- a/lib/Basic/Builtins.cpp
+++ b/lib/Basic/Builtins.cpp
@@ -16,6 +16,7 @@
 #include "clang/Basic/LangOptions.h"
 #include "clang/Basic/TargetInfo.h"
 #include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/StringRef.h"
 using namespace clang;
 
 static const Builtin::Info BuiltinInfo[] = {
@@ -44,6 +45,21 @@ void Builtin::Context::InitializeTarget(const TargetInfo &Target) {
   Target.getTargetBuiltins(TSRecords, NumTSRecords);  
 }
 
+bool Builtin::Context::BuiltinIsSupported(const Builtin::Info &BuiltinInfo,
+                                          const LangOptions &LangOpts) {
+  bool BuiltinsUnsupported = LangOpts.NoBuiltin &&
+                             strchr(BuiltinInfo.Attributes, 'f');
+  bool MathBuiltinsUnsupported =
+    LangOpts.NoMathBuiltin && BuiltinInfo.HeaderName &&      
+    llvm::StringRef(BuiltinInfo.HeaderName).equals("math.h");
+  bool GnuModeUnsupported = !LangOpts.GNUMode &&
+                            (BuiltinInfo.builtin_lang & GNU_LANG);
+  bool ObjCUnsupported = !LangOpts.ObjC1 &&
+                         BuiltinInfo.builtin_lang == OBJC_LANG;
+  return !BuiltinsUnsupported && !MathBuiltinsUnsupported &&
+         !GnuModeUnsupported && !ObjCUnsupported;
+}
+
 /// InitializeBuiltins - Mark the identifiers for all the builtins with their
 /// appropriate builtin ID # and mark any non-portable builtin identifiers as
 /// such.
@@ -51,10 +67,9 @@ void Builtin::Context::InitializeBuiltins(IdentifierTable &Table,
                                           const LangOptions& LangOpts) {
   // Step #1: mark all target-independent builtins with their ID's.
   for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
-    if ((!LangOpts.NoBuiltin || !strchr(BuiltinInfo[i].Attributes, 'f')) &&
-        (LangOpts.GNUMode || !(BuiltinInfo[i].builtin_lang & GNU_LANG)) &&
-        (LangOpts.ObjC1 || BuiltinInfo[i].builtin_lang != OBJC_LANG))
+    if (BuiltinIsSupported(BuiltinInfo[i], LangOpts)) {
       Table.get(BuiltinInfo[i].Name).setBuiltinID(i);
+    }
 
   // Step #2: Register target-specific builtins.
   for (unsigned i = 0, e = NumTSRecords; i != e; ++i)
diff --git a/lib/Frontend/CompilerInvocation.cpp b/lib/Frontend/CompilerInvocation.cpp
index 97ca50ac4d6732ac0bf0466b0f43d13b4a76377a..bb7d67ae37663ab65fd615c1cfcf83b463682841 100644
--- a/lib/Frontend/CompilerInvocation.cpp
+++ b/lib/Frontend/CompilerInvocation.cpp
@@ -1272,6 +1272,7 @@ static void ParseLangArgs(LangOptions &Opts, ArgList &Args, InputKind IK,
   Opts.ShortEnums = Args.hasArg(OPT_fshort_enums);
   Opts.Freestanding = Args.hasArg(OPT_ffreestanding);
   Opts.NoBuiltin = Args.hasArg(OPT_fno_builtin) || Opts.Freestanding;
+  Opts.NoMathBuiltin = Args.hasArg(OPT_fno_math_builtin);
   Opts.AssumeSaneOperatorNew = !Args.hasArg(OPT_fno_assume_sane_operator_new);
   Opts.HeinousExtensions = Args.hasArg(OPT_fheinous_gnu_extensions);
   Opts.AccessControl = !Args.hasArg(OPT_fno_access_control);
diff --git a/test/CodeGen/nomathbuiltin.c b/test/CodeGen/nomathbuiltin.c
new file mode 100644
index 0000000000000000000000000000000000000000..edd48237999cf67b2bd97c3f9c4a239709f2025f
--- /dev/null
+++ b/test/CodeGen/nomathbuiltin.c
@@ -0,0 +1,18 @@
+// RUN: %clang_cc1 -fno-math-builtin -emit-llvm -o - %s | FileCheck %s
+
+// Check that the -fno-math-builtin option for -cc1 is working properly,
+// by disabling just math builtin generation (other lib functions will
+// be generated as builtins).
+
+extern char *p1, *p2;
+
+double pow(double, double);
+void *memcpy(void *, const void *, unsigned long);
+
+double foo(double a, double b) {
+  memcpy(p1, p2, (unsigned long) b);
+// CHECK: call void @llvm.memcpy
+  return pow(a, b);
+// CHECK: call double @pow
+}
+