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