Daniel Dunbar | f89a32a | 2009-11-10 19:51:53 +0000 | [diff] [blame^] | 1 | //===--- Options.cpp - clang-cc Option Handling ---------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // This file contains "pure" option handling, it is only responsible for turning |
| 11 | // the options into internal *Option classes, but shouldn't have any other |
| 12 | // logic. |
| 13 | |
| 14 | #include "Options.h" |
| 15 | #include "clang/Frontend/CompileOptions.h" |
| 16 | #include "clang/Basic/LangOptions.h" |
| 17 | #include "clang/Basic/TargetInfo.h" |
| 18 | #include "llvm/ADT/StringMap.h" |
| 19 | #include "llvm/Support/CommandLine.h" |
| 20 | |
| 21 | using namespace clang; |
| 22 | |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | // Code Generation Options |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | |
| 27 | namespace codegenoptions { |
| 28 | |
| 29 | static llvm::cl::opt<bool> |
| 30 | DisableLLVMOptimizations("disable-llvm-optzns", |
| 31 | llvm::cl::desc("Don't run LLVM optimization passes")); |
| 32 | |
| 33 | static llvm::cl::opt<bool> |
| 34 | DisableRedZone("disable-red-zone", |
| 35 | llvm::cl::desc("Do not emit code that uses the red zone."), |
| 36 | llvm::cl::init(false)); |
| 37 | |
| 38 | static llvm::cl::opt<bool> |
| 39 | GenerateDebugInfo("g", |
| 40 | llvm::cl::desc("Generate source level debug information")); |
| 41 | |
| 42 | static llvm::cl::opt<bool> |
| 43 | NoCommon("fno-common", |
| 44 | llvm::cl::desc("Compile common globals like normal definitions"), |
| 45 | llvm::cl::ValueDisallowed); |
| 46 | |
| 47 | static llvm::cl::opt<bool> |
| 48 | NoImplicitFloat("no-implicit-float", |
| 49 | llvm::cl::desc("Don't generate implicit floating point instructions (x86-only)"), |
| 50 | llvm::cl::init(false)); |
| 51 | |
| 52 | static llvm::cl::opt<bool> |
| 53 | NoMergeConstants("fno-merge-all-constants", |
| 54 | llvm::cl::desc("Disallow merging of constants.")); |
| 55 | |
| 56 | // It might be nice to add bounds to the CommandLine library directly. |
| 57 | struct OptLevelParser : public llvm::cl::parser<unsigned> { |
| 58 | bool parse(llvm::cl::Option &O, llvm::StringRef ArgName, |
| 59 | llvm::StringRef Arg, unsigned &Val) { |
| 60 | if (llvm::cl::parser<unsigned>::parse(O, ArgName, Arg, Val)) |
| 61 | return true; |
| 62 | if (Val > 3) |
| 63 | return O.error("'" + Arg + "' invalid optimization level!"); |
| 64 | return false; |
| 65 | } |
| 66 | }; |
| 67 | static llvm::cl::opt<unsigned, false, OptLevelParser> |
| 68 | OptLevel("O", llvm::cl::Prefix, |
| 69 | llvm::cl::desc("Optimization level"), |
| 70 | llvm::cl::init(0)); |
| 71 | |
| 72 | static llvm::cl::opt<bool> |
| 73 | OptSize("Os", llvm::cl::desc("Optimize for size")); |
| 74 | |
| 75 | static llvm::cl::opt<std::string> |
| 76 | TargetCPU("mcpu", |
| 77 | llvm::cl::desc("Target a specific cpu type (-mcpu=help for details)")); |
| 78 | |
| 79 | static llvm::cl::list<std::string> |
| 80 | TargetFeatures("target-feature", llvm::cl::desc("Target specific attributes")); |
| 81 | |
| 82 | } |
| 83 | |
| 84 | //===----------------------------------------------------------------------===// |
| 85 | // Option Object Construction |
| 86 | //===----------------------------------------------------------------------===// |
| 87 | |
| 88 | /// ComputeTargetFeatures - Recompute the target feature list to only |
| 89 | /// be the list of things that are enabled, based on the target cpu |
| 90 | /// and feature list. |
| 91 | void clang::ComputeFeatureMap(TargetInfo &Target, |
| 92 | llvm::StringMap<bool> &Features) { |
| 93 | using namespace codegenoptions; |
| 94 | assert(Features.empty() && "invalid map"); |
| 95 | |
| 96 | // Initialize the feature map based on the target. |
| 97 | Target.getDefaultFeatures(TargetCPU, Features); |
| 98 | |
| 99 | // Apply the user specified deltas. |
| 100 | for (llvm::cl::list<std::string>::iterator it = TargetFeatures.begin(), |
| 101 | ie = TargetFeatures.end(); it != ie; ++it) { |
| 102 | const char *Name = it->c_str(); |
| 103 | |
| 104 | // FIXME: Don't handle errors like this. |
| 105 | if (Name[0] != '-' && Name[0] != '+') { |
| 106 | fprintf(stderr, "error: clang-cc: invalid target feature string: %s\n", |
| 107 | Name); |
| 108 | exit(1); |
| 109 | } |
| 110 | if (!Target.setFeatureEnabled(Features, Name + 1, (Name[0] == '+'))) { |
| 111 | fprintf(stderr, "error: clang-cc: invalid target feature name: %s\n", |
| 112 | Name + 1); |
| 113 | exit(1); |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | void clang::InitializeCompileOptions(CompileOptions &Opts, |
| 119 | const llvm::StringMap<bool> &Features) { |
| 120 | using namespace codegenoptions; |
| 121 | Opts.OptimizeSize = OptSize; |
| 122 | Opts.DebugInfo = GenerateDebugInfo; |
| 123 | Opts.DisableLLVMOpts = DisableLLVMOptimizations; |
| 124 | |
| 125 | // -Os implies -O2 |
| 126 | Opts.OptimizationLevel = OptSize ? 2 : OptLevel; |
| 127 | |
| 128 | // We must always run at least the always inlining pass. |
| 129 | Opts.Inlining = (Opts.OptimizationLevel > 1) ? CompileOptions::NormalInlining |
| 130 | : CompileOptions::OnlyAlwaysInlining; |
| 131 | |
| 132 | Opts.UnrollLoops = (Opts.OptimizationLevel > 1 && !OptSize); |
| 133 | Opts.SimplifyLibCalls = 1; |
| 134 | |
| 135 | #ifdef NDEBUG |
| 136 | Opts.VerifyModule = 0; |
| 137 | #endif |
| 138 | |
| 139 | Opts.CPU = TargetCPU; |
| 140 | Opts.Features.clear(); |
| 141 | for (llvm::StringMap<bool>::const_iterator it = Features.begin(), |
| 142 | ie = Features.end(); it != ie; ++it) { |
| 143 | // FIXME: If we are completely confident that we have the right set, we only |
| 144 | // need to pass the minuses. |
| 145 | std::string Name(it->second ? "+" : "-"); |
| 146 | Name += it->first(); |
| 147 | Opts.Features.push_back(Name); |
| 148 | } |
| 149 | |
| 150 | Opts.NoCommon = NoCommon; |
| 151 | |
| 152 | Opts.DisableRedZone = DisableRedZone; |
| 153 | Opts.NoImplicitFloat = NoImplicitFloat; |
| 154 | |
| 155 | Opts.MergeAllConstants = !NoMergeConstants; |
| 156 | } |