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