Add a -fno-math-builtin option to the Clang -cc1

llvm-svn: 186899
This commit is contained in:
Eli Bendersky 2013-07-23 00:13:01 +00:00
parent ead2ba4649
commit aefa5e2ff2
6 changed files with 44 additions and 3 deletions

View 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);
};
}

View 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")

View 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>;

View 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)

View 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);

View File

@ -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
}