CommandFlags.h 13 KB

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