Factor out a function which determines the cpu and feature strings based on
[oota-llvm.git] / include / llvm / CodeGen / CommandFlags.h
1 //===-- CommandFlags.h - Command Line Flags Interface -----------*- C++ -*-===//
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 codegen-specific flags that are shared between different
11 // command line tools. The tools "llc" and "opt" both use this file to prevent
12 // flag duplication.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_CODEGEN_COMMANDFLAGS_H
17 #define LLVM_CODEGEN_COMMANDFLAGS_H
18
19 #include "llvm/MC/MCTargetOptionsCommandFlags.h"
20 #include "llvm//MC/SubtargetFeature.h"
21 #include "llvm/Support/CodeGen.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/Host.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include "llvm/Target/TargetOptions.h"
26 #include <string>
27 using namespace llvm;
28
29 cl::opt<std::string>
30 MArch("march", cl::desc("Architecture to generate code for (see --version)"));
31
32 cl::opt<std::string>
33 MCPU("mcpu",
34      cl::desc("Target a specific cpu type (-mcpu=help for details)"),
35      cl::value_desc("cpu-name"),
36      cl::init(""));
37
38 cl::list<std::string>
39 MAttrs("mattr",
40        cl::CommaSeparated,
41        cl::desc("Target specific attributes (-mattr=help for details)"),
42        cl::value_desc("a1,+a2,-a3,..."));
43
44 cl::opt<Reloc::Model>
45 RelocModel("relocation-model",
46            cl::desc("Choose relocation model"),
47            cl::init(Reloc::Default),
48            cl::values(
49               clEnumValN(Reloc::Default, "default",
50                       "Target default relocation model"),
51               clEnumValN(Reloc::Static, "static",
52                       "Non-relocatable code"),
53               clEnumValN(Reloc::PIC_, "pic",
54                       "Fully relocatable, position independent code"),
55               clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic",
56                       "Relocatable external references, non-relocatable code"),
57               clEnumValEnd));
58
59 cl::opt<ThreadModel::Model>
60 TMModel("thread-model",
61         cl::desc("Choose threading model"),
62         cl::init(ThreadModel::POSIX),
63         cl::values(clEnumValN(ThreadModel::POSIX, "posix",
64                               "POSIX thread model"),
65                    clEnumValN(ThreadModel::Single, "single",
66                               "Single thread model"),
67                    clEnumValEnd));
68
69 cl::opt<llvm::CodeModel::Model>
70 CMModel("code-model",
71         cl::desc("Choose code model"),
72         cl::init(CodeModel::Default),
73         cl::values(clEnumValN(CodeModel::Default, "default",
74                               "Target default code model"),
75                    clEnumValN(CodeModel::Small, "small",
76                               "Small code model"),
77                    clEnumValN(CodeModel::Kernel, "kernel",
78                               "Kernel code model"),
79                    clEnumValN(CodeModel::Medium, "medium",
80                               "Medium code model"),
81                    clEnumValN(CodeModel::Large, "large",
82                               "Large code model"),
83                    clEnumValEnd));
84
85 cl::opt<TargetMachine::CodeGenFileType>
86 FileType("filetype", cl::init(TargetMachine::CGFT_AssemblyFile),
87   cl::desc("Choose a file type (not all types are supported by all targets):"),
88   cl::values(
89              clEnumValN(TargetMachine::CGFT_AssemblyFile, "asm",
90                         "Emit an assembly ('.s') file"),
91              clEnumValN(TargetMachine::CGFT_ObjectFile, "obj",
92                         "Emit a native object ('.o') file"),
93              clEnumValN(TargetMachine::CGFT_Null, "null",
94                         "Emit nothing, for performance testing"),
95              clEnumValEnd));
96
97 cl::opt<bool>
98 EnableFPMAD("enable-fp-mad",
99             cl::desc("Enable less precise MAD instructions to be generated"),
100             cl::init(false));
101
102 cl::opt<bool>
103 DisableFPElim("disable-fp-elim",
104               cl::desc("Disable frame pointer elimination optimization"),
105               cl::init(false));
106
107 cl::opt<bool>
108 EnableUnsafeFPMath("enable-unsafe-fp-math",
109                 cl::desc("Enable optimizations that may decrease FP precision"),
110                 cl::init(false));
111
112 cl::opt<bool>
113 EnableNoInfsFPMath("enable-no-infs-fp-math",
114                 cl::desc("Enable FP math optimizations that assume no +-Infs"),
115                 cl::init(false));
116
117 cl::opt<bool>
118 EnableNoNaNsFPMath("enable-no-nans-fp-math",
119                    cl::desc("Enable FP math optimizations that assume no NaNs"),
120                    cl::init(false));
121
122 cl::opt<bool>
123 EnableHonorSignDependentRoundingFPMath("enable-sign-dependent-rounding-fp-math",
124       cl::Hidden,
125       cl::desc("Force codegen to assume rounding mode can change dynamically"),
126       cl::init(false));
127
128 cl::opt<bool>
129 GenerateSoftFloatCalls("soft-float",
130                     cl::desc("Generate software floating point library calls"),
131                     cl::init(false));
132
133 cl::opt<llvm::FloatABI::ABIType>
134 FloatABIForCalls("float-abi",
135                  cl::desc("Choose float ABI type"),
136                  cl::init(FloatABI::Default),
137                  cl::values(
138                      clEnumValN(FloatABI::Default, "default",
139                                 "Target default float ABI type"),
140                      clEnumValN(FloatABI::Soft, "soft",
141                                 "Soft float ABI (implied by -soft-float)"),
142                      clEnumValN(FloatABI::Hard, "hard",
143                                 "Hard float ABI (uses FP registers)"),
144                      clEnumValEnd));
145
146 cl::opt<llvm::FPOpFusion::FPOpFusionMode>
147 FuseFPOps("fp-contract",
148           cl::desc("Enable aggressive formation of fused FP ops"),
149           cl::init(FPOpFusion::Standard),
150           cl::values(
151               clEnumValN(FPOpFusion::Fast, "fast",
152                          "Fuse FP ops whenever profitable"),
153               clEnumValN(FPOpFusion::Standard, "on",
154                          "Only fuse 'blessed' FP ops."),
155               clEnumValN(FPOpFusion::Strict, "off",
156                          "Only fuse FP ops when the result won't be effected."),
157               clEnumValEnd));
158
159 cl::opt<bool>
160 DontPlaceZerosInBSS("nozero-initialized-in-bss",
161               cl::desc("Don't place zero-initialized symbols into bss section"),
162               cl::init(false));
163
164 cl::opt<bool>
165 EnableGuaranteedTailCallOpt("tailcallopt",
166   cl::desc("Turn fastcc calls into tail calls by (potentially) changing ABI."),
167   cl::init(false));
168
169 cl::opt<bool>
170 DisableTailCalls("disable-tail-calls",
171                  cl::desc("Never emit tail calls"),
172                  cl::init(false));
173
174 cl::opt<unsigned>
175 OverrideStackAlignment("stack-alignment",
176                        cl::desc("Override default stack alignment"),
177                        cl::init(0));
178
179 cl::opt<std::string>
180 TrapFuncName("trap-func", cl::Hidden,
181         cl::desc("Emit a call to trap function rather than a trap instruction"),
182         cl::init(""));
183
184 cl::opt<bool>
185 EnablePIE("enable-pie",
186           cl::desc("Assume the creation of a position independent executable."),
187           cl::init(false));
188
189 cl::opt<bool>
190 UseCtors("use-ctors",
191              cl::desc("Use .ctors instead of .init_array."),
192              cl::init(false));
193
194 cl::opt<std::string> StopAfter("stop-after",
195                             cl::desc("Stop compilation after a specific pass"),
196                             cl::value_desc("pass-name"),
197                                       cl::init(""));
198 cl::opt<std::string> StartAfter("start-after",
199                           cl::desc("Resume compilation after a specific pass"),
200                           cl::value_desc("pass-name"),
201                           cl::init(""));
202
203 cl::opt<bool> DataSections("data-sections",
204                            cl::desc("Emit data into separate sections"),
205                            cl::init(false));
206
207 cl::opt<bool>
208 FunctionSections("function-sections",
209                  cl::desc("Emit functions into separate sections"),
210                  cl::init(false));
211
212 cl::opt<bool> UniqueSectionNames("unique-section-names",
213                                  cl::desc("Give unique names to every section"),
214                                  cl::init(true));
215
216 cl::opt<llvm::JumpTable::JumpTableType>
217 JTableType("jump-table-type",
218           cl::desc("Choose the type of Jump-Instruction Table for jumptable."),
219           cl::init(JumpTable::Single),
220           cl::values(
221               clEnumValN(JumpTable::Single, "single",
222                          "Create a single table for all jumptable functions"),
223               clEnumValN(JumpTable::Arity, "arity",
224                          "Create one table per number of parameters."),
225               clEnumValN(JumpTable::Simplified, "simplified",
226                          "Create one table per simplified function type."),
227               clEnumValN(JumpTable::Full, "full",
228                          "Create one table per unique function type."),
229               clEnumValEnd));
230
231 // Common utility function tightly tied to the options listed here. Initializes
232 // a TargetOptions object with CodeGen flags and returns it.
233 static inline TargetOptions InitTargetOptionsFromCodeGenFlags() {
234   TargetOptions Options;
235   Options.LessPreciseFPMADOption = EnableFPMAD;
236   Options.NoFramePointerElim = DisableFPElim;
237   Options.AllowFPOpFusion = FuseFPOps;
238   Options.UnsafeFPMath = EnableUnsafeFPMath;
239   Options.NoInfsFPMath = EnableNoInfsFPMath;
240   Options.NoNaNsFPMath = EnableNoNaNsFPMath;
241   Options.HonorSignDependentRoundingFPMathOption =
242       EnableHonorSignDependentRoundingFPMath;
243   Options.UseSoftFloat = GenerateSoftFloatCalls;
244   if (FloatABIForCalls != FloatABI::Default)
245     Options.FloatABIType = FloatABIForCalls;
246   Options.NoZerosInBSS = DontPlaceZerosInBSS;
247   Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt;
248   Options.DisableTailCalls = DisableTailCalls;
249   Options.StackAlignmentOverride = OverrideStackAlignment;
250   Options.TrapFuncName = TrapFuncName;
251   Options.PositionIndependentExecutable = EnablePIE;
252   Options.UseInitArray = !UseCtors;
253   Options.DataSections = DataSections;
254   Options.FunctionSections = FunctionSections;
255   Options.UniqueSectionNames = UniqueSectionNames;
256
257   Options.MCOptions = InitMCTargetOptionsFromFlags();
258   Options.JTType = JTableType;
259
260   Options.ThreadModel = TMModel;
261
262   return Options;
263 }
264
265 static inline std::string getCPUStr() {
266   // If user asked for the 'native' CPU, autodetect here. If autodection fails,
267   // this will set the CPU to an empty string which tells the target to
268   // pick a basic default.
269   if (MCPU == "native")
270     return sys::getHostCPUName();
271
272   return MCPU;
273 }
274
275 static inline std::string getFeaturesStr() {
276   SubtargetFeatures Features;
277
278   // If user asked for the 'native' CPU, we need to autodetect features.
279   // This is necessary for x86 where the CPU might not support all the
280   // features the autodetected CPU name lists in the target. For example,
281   if (MCPU == "native") {
282     StringMap<bool> HostFeatures;
283     if (sys::getHostCPUFeatures(HostFeatures))
284       for (auto &F : HostFeatures)
285         Features.AddFeature(F.first(), F.second);
286   }
287
288   for (unsigned i = 0; i != MAttrs.size(); ++i)
289     Features.AddFeature(MAttrs[i]);
290
291   return Features.getString();
292 }
293
294 #endif