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/ADT/StringExtras.h" 20 #include "llvm/IR/Instructions.h" 21 #include "llvm/IR/Intrinsics.h" 22 #include "llvm/IR/Module.h" 23 #include "llvm/MC/MCTargetOptionsCommandFlags.h" 24 #include "llvm/MC/SubtargetFeature.h" 25 #include "llvm/Support/CodeGen.h" 26 #include "llvm/Support/CommandLine.h" 27 #include "llvm/Support/Host.h" 28 #include "llvm/Target/TargetMachine.h" 29 #include "llvm/Target/TargetOptions.h" 30 #include <string> 31 using namespace llvm; 32 33 cl::opt<std::string> 34 MArch("march", cl::desc("Architecture to generate code for (see --version)")); 35 36 cl::opt<std::string> 37 MCPU("mcpu", 38 cl::desc("Target a specific cpu type (-mcpu=help for details)"), 39 cl::value_desc("cpu-name"), 40 cl::init("")); 41 42 cl::list<std::string> 43 MAttrs("mattr", 44 cl::CommaSeparated, 45 cl::desc("Target specific attributes (-mattr=help for details)"), 46 cl::value_desc("a1,+a2,-a3,...")); 47 48 cl::opt<Reloc::Model> RelocModel( 49 "relocation-model", cl::desc("Choose relocation model"), 50 cl::values( 51 clEnumValN(Reloc::Static, "static", "Non-relocatable code"), 52 clEnumValN(Reloc::PIC_, "pic", 53 "Fully relocatable, position independent code"), 54 clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic", 55 "Relocatable external references, non-relocatable code"), 56 clEnumValN(Reloc::ROPI, "ropi", 57 "Code and read-only data relocatable, accessed PC-relative"), 58 clEnumValN(Reloc::RWPI, "rwpi", 59 "Read-write data relocatable, accessed relative to static base"), 60 clEnumValN(Reloc::ROPI_RWPI, "ropi-rwpi", 61 "Combination of ropi and rwpi"))); 62 63 static inline Optional<Reloc::Model> getRelocModel() { 64 if (RelocModel.getNumOccurrences()) { 65 Reloc::Model R = RelocModel; 66 return R; 67 } 68 return None; 69 } 70 71 cl::opt<ThreadModel::Model> 72 TMModel("thread-model", 73 cl::desc("Choose threading model"), 74 cl::init(ThreadModel::POSIX), 75 cl::values(clEnumValN(ThreadModel::POSIX, "posix", 76 "POSIX thread model"), 77 clEnumValN(ThreadModel::Single, "single", 78 "Single thread model"))); 79 80 cl::opt<llvm::CodeModel::Model> 81 CMModel("code-model", 82 cl::desc("Choose code model"), 83 cl::init(CodeModel::Default), 84 cl::values(clEnumValN(CodeModel::Default, "default", 85 "Target default code model"), 86 clEnumValN(CodeModel::Small, "small", 87 "Small code model"), 88 clEnumValN(CodeModel::Kernel, "kernel", 89 "Kernel code model"), 90 clEnumValN(CodeModel::Medium, "medium", 91 "Medium code model"), 92 clEnumValN(CodeModel::Large, "large", 93 "Large code model"))); 94 95 cl::opt<llvm::ExceptionHandling> 96 ExceptionModel("exception-model", 97 cl::desc("exception model"), 98 cl::init(ExceptionHandling::None), 99 cl::values(clEnumValN(ExceptionHandling::None, "default", 100 "default exception handling model"), 101 clEnumValN(ExceptionHandling::DwarfCFI, "dwarf", 102 "DWARF-like CFI based exception handling"), 103 clEnumValN(ExceptionHandling::SjLj, "sjlj", 104 "SjLj exception handling"), 105 clEnumValN(ExceptionHandling::ARM, "arm", 106 "ARM EHABI exceptions"), 107 clEnumValN(ExceptionHandling::WinEH, "wineh", 108 "Windows exception model"))); 109 110 cl::opt<TargetMachine::CodeGenFileType> 111 FileType("filetype", cl::init(TargetMachine::CGFT_AssemblyFile), 112 cl::desc("Choose a file type (not all types are supported by all targets):"), 113 cl::values( 114 clEnumValN(TargetMachine::CGFT_AssemblyFile, "asm", 115 "Emit an assembly ('.s') file"), 116 clEnumValN(TargetMachine::CGFT_ObjectFile, "obj", 117 "Emit a native object ('.o') file"), 118 clEnumValN(TargetMachine::CGFT_Null, "null", 119 "Emit nothing, for performance testing"))); 120 121 cl::opt<bool> 122 DisableFPElim("disable-fp-elim", 123 cl::desc("Disable frame pointer elimination optimization"), 124 cl::init(false)); 125 126 cl::opt<bool> 127 EnableUnsafeFPMath("enable-unsafe-fp-math", 128 cl::desc("Enable optimizations that may decrease FP precision"), 129 cl::init(false)); 130 131 cl::opt<bool> 132 EnableNoInfsFPMath("enable-no-infs-fp-math", 133 cl::desc("Enable FP math optimizations that assume no +-Infs"), 134 cl::init(false)); 135 136 cl::opt<bool> 137 EnableNoNaNsFPMath("enable-no-nans-fp-math", 138 cl::desc("Enable FP math optimizations that assume no NaNs"), 139 cl::init(false)); 140 141 cl::opt<bool> 142 EnableNoSignedZerosFPMath("enable-no-signed-zeros-fp-math", 143 cl::desc("Enable FP math optimizations that assume " 144 "the sign of 0 is insignificant"), 145 cl::init(false)); 146 147 cl::opt<bool> 148 EnableNoTrappingFPMath("enable-no-trapping-fp-math", 149 cl::desc("Enable setting the FP exceptions build " 150 "attribute not to use exceptions"), 151 cl::init(false)); 152 153 cl::opt<llvm::FPDenormal::DenormalMode> 154 DenormalMode("denormal-fp-math", 155 cl::desc("Select which denormal numbers the code is permitted to require"), 156 cl::init(FPDenormal::IEEE), 157 cl::values( 158 clEnumValN(FPDenormal::IEEE, "ieee", 159 "IEEE 754 denormal numbers"), 160 clEnumValN(FPDenormal::PreserveSign, "preserve-sign", 161 "the sign of a flushed-to-zero number is preserved " 162 "in the sign of 0"), 163 clEnumValN(FPDenormal::PositiveZero, "positive-zero", 164 "denormals are flushed to positive zero"))); 165 166 cl::opt<bool> 167 EnableHonorSignDependentRoundingFPMath("enable-sign-dependent-rounding-fp-math", 168 cl::Hidden, 169 cl::desc("Force codegen to assume rounding mode can change dynamically"), 170 cl::init(false)); 171 172 cl::opt<llvm::FloatABI::ABIType> 173 FloatABIForCalls("float-abi", 174 cl::desc("Choose float ABI type"), 175 cl::init(FloatABI::Default), 176 cl::values( 177 clEnumValN(FloatABI::Default, "default", 178 "Target default float ABI type"), 179 clEnumValN(FloatABI::Soft, "soft", 180 "Soft float ABI (implied by -soft-float)"), 181 clEnumValN(FloatABI::Hard, "hard", 182 "Hard float ABI (uses FP registers)"))); 183 184 cl::opt<llvm::FPOpFusion::FPOpFusionMode> 185 FuseFPOps("fp-contract", 186 cl::desc("Enable aggressive formation of fused FP ops"), 187 cl::init(FPOpFusion::Standard), 188 cl::values( 189 clEnumValN(FPOpFusion::Fast, "fast", 190 "Fuse FP ops whenever profitable"), 191 clEnumValN(FPOpFusion::Standard, "on", 192 "Only fuse 'blessed' FP ops."), 193 clEnumValN(FPOpFusion::Strict, "off", 194 "Only fuse FP ops when the result won't be affected."))); 195 196 cl::opt<bool> 197 DontPlaceZerosInBSS("nozero-initialized-in-bss", 198 cl::desc("Don't place zero-initialized symbols into bss section"), 199 cl::init(false)); 200 201 cl::opt<bool> 202 EnableGuaranteedTailCallOpt("tailcallopt", 203 cl::desc("Turn fastcc calls into tail calls by (potentially) changing ABI."), 204 cl::init(false)); 205 206 cl::opt<bool> 207 DisableTailCalls("disable-tail-calls", 208 cl::desc("Never emit tail calls"), 209 cl::init(false)); 210 211 cl::opt<bool> 212 StackSymbolOrdering("stack-symbol-ordering", 213 cl::desc("Order local stack symbols."), 214 cl::init(true)); 215 216 cl::opt<unsigned> 217 OverrideStackAlignment("stack-alignment", 218 cl::desc("Override default stack alignment"), 219 cl::init(0)); 220 221 cl::opt<bool> 222 StackRealign("stackrealign", 223 cl::desc("Force align the stack to the minimum alignment"), 224 cl::init(false)); 225 226 cl::opt<std::string> 227 TrapFuncName("trap-func", cl::Hidden, 228 cl::desc("Emit a call to trap function rather than a trap instruction"), 229 cl::init("")); 230 231 cl::opt<bool> 232 UseCtors("use-ctors", 233 cl::desc("Use .ctors instead of .init_array."), 234 cl::init(false)); 235 236 cl::opt<bool> RelaxELFRelocations( 237 "relax-elf-relocations", 238 cl::desc("Emit GOTPCRELX/REX_GOTPCRELX instead of GOTPCREL on x86-64 ELF"), 239 cl::init(false)); 240 241 cl::opt<bool> DataSections("data-sections", 242 cl::desc("Emit data into separate sections"), 243 cl::init(false)); 244 245 cl::opt<bool> 246 FunctionSections("function-sections", 247 cl::desc("Emit functions into separate sections"), 248 cl::init(false)); 249 250 cl::opt<bool> EmulatedTLS("emulated-tls", 251 cl::desc("Use emulated TLS model"), 252 cl::init(false)); 253 254 cl::opt<bool> UniqueSectionNames("unique-section-names", 255 cl::desc("Give unique names to every section"), 256 cl::init(true)); 257 258 cl::opt<llvm::EABI> EABIVersion( 259 "meabi", cl::desc("Set EABI type (default depends on triple):"), 260 cl::init(EABI::Default), 261 cl::values(clEnumValN(EABI::Default, "default", 262 "Triple default EABI version"), 263 clEnumValN(EABI::EABI4, "4", "EABI version 4"), 264 clEnumValN(EABI::EABI5, "5", "EABI version 5"), 265 clEnumValN(EABI::GNU, "gnu", "EABI GNU"))); 266 267 cl::opt<DebuggerKind> 268 DebuggerTuningOpt("debugger-tune", 269 cl::desc("Tune debug info for a particular debugger"), 270 cl::init(DebuggerKind::Default), 271 cl::values( 272 clEnumValN(DebuggerKind::GDB, "gdb", "gdb"), 273 clEnumValN(DebuggerKind::LLDB, "lldb", "lldb"), 274 clEnumValN(DebuggerKind::SCE, "sce", 275 "SCE targets (e.g. PS4)"))); 276 277 // Common utility function tightly tied to the options listed here. Initializes 278 // a TargetOptions object with CodeGen flags and returns it. 279 static inline TargetOptions InitTargetOptionsFromCodeGenFlags() { 280 TargetOptions Options; 281 Options.AllowFPOpFusion = FuseFPOps; 282 Options.UnsafeFPMath = EnableUnsafeFPMath; 283 Options.NoInfsFPMath = EnableNoInfsFPMath; 284 Options.NoNaNsFPMath = EnableNoNaNsFPMath; 285 Options.NoSignedZerosFPMath = EnableNoSignedZerosFPMath; 286 Options.NoTrappingFPMath = EnableNoTrappingFPMath; 287 Options.FPDenormalMode = DenormalMode; 288 Options.HonorSignDependentRoundingFPMathOption = 289 EnableHonorSignDependentRoundingFPMath; 290 if (FloatABIForCalls != FloatABI::Default) 291 Options.FloatABIType = FloatABIForCalls; 292 Options.NoZerosInBSS = DontPlaceZerosInBSS; 293 Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt; 294 Options.StackAlignmentOverride = OverrideStackAlignment; 295 Options.StackSymbolOrdering = StackSymbolOrdering; 296 Options.UseInitArray = !UseCtors; 297 Options.RelaxELFRelocations = RelaxELFRelocations; 298 Options.DataSections = DataSections; 299 Options.FunctionSections = FunctionSections; 300 Options.UniqueSectionNames = UniqueSectionNames; 301 Options.EmulatedTLS = EmulatedTLS; 302 Options.ExceptionModel = ExceptionModel; 303 304 Options.MCOptions = InitMCTargetOptionsFromFlags(); 305 306 Options.ThreadModel = TMModel; 307 Options.EABIVersion = EABIVersion; 308 Options.DebuggerTuning = DebuggerTuningOpt; 309 310 return Options; 311 } 312 313 static inline std::string getCPUStr() { 314 // If user asked for the 'native' CPU, autodetect here. If autodection fails, 315 // this will set the CPU to an empty string which tells the target to 316 // pick a basic default. 317 if (MCPU == "native") 318 return sys::getHostCPUName(); 319 320 return MCPU; 321 } 322 323 static inline std::string getFeaturesStr() { 324 SubtargetFeatures Features; 325 326 // If user asked for the 'native' CPU, we need to autodetect features. 327 // This is necessary for x86 where the CPU might not support all the 328 // features the autodetected CPU name lists in the target. For example, 329 // not all Sandybridge processors support AVX. 330 if (MCPU == "native") { 331 StringMap<bool> HostFeatures; 332 if (sys::getHostCPUFeatures(HostFeatures)) 333 for (auto &F : HostFeatures) 334 Features.AddFeature(F.first(), F.second); 335 } 336 337 for (unsigned i = 0; i != MAttrs.size(); ++i) 338 Features.AddFeature(MAttrs[i]); 339 340 return Features.getString(); 341 } 342 343 /// \brief Set function attributes of functions in Module M based on CPU, 344 /// Features, and command line flags. 345 static inline void setFunctionAttributes(StringRef CPU, StringRef Features, 346 Module &M) { 347 for (auto &F : M) { 348 auto &Ctx = F.getContext(); 349 AttributeList Attrs = F.getAttributes(), NewAttrs; 350 351 if (!CPU.empty()) 352 NewAttrs = NewAttrs.addAttribute(Ctx, AttributeList::FunctionIndex, 353 "target-cpu", CPU); 354 355 if (!Features.empty()) 356 NewAttrs = NewAttrs.addAttribute(Ctx, AttributeList::FunctionIndex, 357 "target-features", Features); 358 359 if (DisableFPElim.getNumOccurrences() > 0) 360 NewAttrs = NewAttrs.addAttribute(Ctx, AttributeList::FunctionIndex, 361 "no-frame-pointer-elim", 362 DisableFPElim ? "true" : "false"); 363 364 if (DisableTailCalls.getNumOccurrences() > 0) 365 NewAttrs = NewAttrs.addAttribute(Ctx, AttributeList::FunctionIndex, 366 "disable-tail-calls", 367 toStringRef(DisableTailCalls)); 368 369 if (StackRealign) 370 NewAttrs = NewAttrs.addAttribute(Ctx, AttributeList::FunctionIndex, 371 "stackrealign"); 372 373 if (TrapFuncName.getNumOccurrences() > 0) 374 for (auto &B : F) 375 for (auto &I : B) 376 if (auto *Call = dyn_cast<CallInst>(&I)) 377 if (const auto *F = Call->getCalledFunction()) 378 if (F->getIntrinsicID() == Intrinsic::debugtrap || 379 F->getIntrinsicID() == Intrinsic::trap) 380 Call->addAttribute( 381 llvm::AttributeList::FunctionIndex, 382 Attribute::get(Ctx, "trap-func-name", TrapFuncName)); 383 384 // Let NewAttrs override Attrs. 385 NewAttrs = Attrs.addAttributes(Ctx, AttributeList::FunctionIndex, NewAttrs); 386 F.setAttributes(NewAttrs); 387 } 388 } 389 390 #endif 391