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