Define __OPTIMIZE__ and __OPTIMIZE_SIZE__ if the -O[12] and -Os flags are passed to the compiler.

llvm-svn: 68450
This commit is contained in:
Anders Carlsson 2009-04-06 17:37:10 +00:00
parent 8887072bce
commit 65cb90efc1
4 changed files with 74 additions and 25 deletions

View File

@ -65,6 +65,9 @@ public:
unsigned HeinousExtensions : 1; // Extensions that we really don't like and
// may be ripped out at any time.
unsigned Optimize : 1; // Whether __OPTIMIZE__ should be defined.
unsigned OptimizeSize : 1; // Whether __OPTIMIZE_SIZE__ should be
// defined.
private:
unsigned GC : 2; // Objective-C Garbage Collection modes. We declare
// this enum as unsigned because MSVC insists on making enums
@ -100,6 +103,9 @@ public:
OverflowChecking = 0;
InstantiationDepth = 99;
Optimize = 0;
OptimizeSize = 0;
}
GCMode getGCMode() const { return (GCMode) GC; }

View File

@ -547,6 +547,11 @@ static void InitializePredefinedMacros(Preprocessor &PP,
DefineBuiltinMacro(Buf, "__int64=__INT64_TYPE__");
}
if (PP.getLangOptions().Optimize)
DefineBuiltinMacro(Buf, "__OPTIMIZE__=1");
if (PP.getLangOptions().OptimizeSize)
DefineBuiltinMacro(Buf, "__OPTIMIZE_SIZE__=1");
// Initialize target-specific preprocessor defines.
const TargetInfo &TI = PP.getTargetInfo();

View File

@ -0,0 +1,29 @@
// RUN: clang-cc -Eonly optimize.c -DOPT_O2 -O2 -verify &&
#ifdef OPT_O2
#ifndef __OPTIMIZE__
#error "__OPTIMIZE__ not defined"
#endif
#ifdef __OPTIMIZE_SIZE
#error "__OPTIMIZE_SIZE__ defined"
#endif
#endif
// RUN: clang-cc -Eonly optimize.c -DOPT_O0 -O0 -verify &&
#ifdef OPT_O0
#ifdef __OPTIMIZE__
#error "__OPTIMIZE__ defined"
#endif
#ifdef __OPTIMIZE_SIZE
#error "__OPTIMIZE_SIZE__ defined"
#endif
#endif
// RUN: clang-cc -Eonly optimize.c -DOPT_OS -Os -verify
#ifdef OPT_OS
#ifndef __OPTIMIZE__
#error "__OPTIMIZE__ not defined"
#endif
#ifndef __OPTIMIZE_SIZE
#error "__OPTIMIZE_SIZE__ not defined"
#endif
#endif

View File

@ -601,6 +601,33 @@ TemplateDepth("ftemplate-depth", llvm::cl::init(99),
llvm::cl::desc("Maximum depth of recursive template "
"instantiation"));
static llvm::cl::opt<bool>
OptSize("Os", llvm::cl::desc("Optimize for size"));
static llvm::cl::opt<bool>
NoCommon("fno-common",
llvm::cl::desc("Compile common globals like normal definitions"),
llvm::cl::ValueDisallowed);
// It might be nice to add bounds to the CommandLine library directly.
struct OptLevelParser : public llvm::cl::parser<unsigned> {
bool parse(llvm::cl::Option &O, const char *ArgName,
const std::string &Arg, unsigned &Val) {
if (llvm::cl::parser<unsigned>::parse(O, ArgName, Arg, Val))
return true;
// FIXME: Support -O4.
if (Val > 3)
return O.error(": '" + Arg + "' invalid optimization level!");
return false;
}
};
static llvm::cl::opt<unsigned, false, OptLevelParser>
OptLevel("O", llvm::cl::Prefix,
llvm::cl::desc("Optimization level"),
llvm::cl::init(0));
// FIXME: add:
// -fdollars-in-identifiers
static void InitializeLanguageStandard(LangOptions &Options, LangKind LK,
@ -746,6 +773,13 @@ static void InitializeLanguageStandard(LangOptions &Options, LangKind LK,
if (EmitAllDecls)
Options.EmitAllDecls = 1;
if (OptSize)
Options.OptimizeSize = 1;
// -Os implies -O2
if (Options.OptimizeSize || OptLevel)
Options.Optimize = 1;
}
static llvm::cl::opt<bool>
@ -1308,31 +1342,6 @@ static llvm::cl::opt<bool>
GenerateDebugInfo("g",
llvm::cl::desc("Generate source level debug information"));
static llvm::cl::opt<bool>
OptSize("Os", llvm::cl::desc("Optimize for size"));
static llvm::cl::opt<bool>
NoCommon("fno-common",
llvm::cl::desc("Compile common globals like normal definitions"),
llvm::cl::ValueDisallowed);
// It might be nice to add bounds to the CommandLine library directly.
struct OptLevelParser : public llvm::cl::parser<unsigned> {
bool parse(llvm::cl::Option &O, const char *ArgName,
const std::string &Arg, unsigned &Val) {
if (llvm::cl::parser<unsigned>::parse(O, ArgName, Arg, Val))
return true;
// FIXME: Support -O4.
if (Val > 3)
return O.error(": '" + Arg + "' invalid optimization level!");
return false;
}
};
static llvm::cl::opt<unsigned, false, OptLevelParser>
OptLevel("O", llvm::cl::Prefix,
llvm::cl::desc("Optimization level"),
llvm::cl::init(0));
static llvm::cl::opt<std::string>
TargetCPU("mcpu",
llvm::cl::desc("Target a specific cpu type (-mcpu=help for details)"));