Add a -fno-math-builtin option to the Clang -cc1
authorEli Bendersky <eliben@google.com>
Tue, 23 Jul 2013 00:13:01 +0000 (00:13 +0000)
committerEli Bendersky <eliben@google.com>
Tue, 23 Jul 2013 00:13:01 +0000 (00:13 +0000)
llvm-svn: 186899

clang/include/clang/Basic/Builtins.h
clang/include/clang/Basic/LangOptions.def
clang/include/clang/Driver/Options.td
clang/lib/Basic/Builtins.cpp
clang/lib/Frontend/CompilerInvocation.cpp
clang/test/CodeGen/nomathbuiltin.c [new file with mode: 0644]

index ef8955c..b66c0db 100644 (file)
@@ -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);
 };
 
 }
index e8fe482..5feac32 100644 (file)
@@ -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")
index 2035bf8..27a844a 100644 (file)
@@ -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>;
index d8aaa6e..e5775b6 100644 (file)
@@ -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)
index 97ca50a..bb7d67a 100644 (file)
@@ -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/clang/test/CodeGen/nomathbuiltin.c b/clang/test/CodeGen/nomathbuiltin.c
new file mode 100644 (file)
index 0000000..edd4823
--- /dev/null
@@ -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
+}
+