]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/Driver/ToolChains/Hexagon.cpp
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / lib / Driver / ToolChains / Hexagon.cpp
1 //===--- Hexagon.cpp - Hexagon ToolChain Implementations --------*- 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 #include "Hexagon.h"
11 #include "CommonArgs.h"
12 #include "InputInfo.h"
13 #include "clang/Driver/Compilation.h"
14 #include "clang/Driver/Driver.h"
15 #include "clang/Driver/DriverDiagnostic.h"
16 #include "clang/Driver/Options.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/Option/ArgList.h"
19 #include "llvm/Support/FileSystem.h"
20 #include "llvm/Support/Path.h"
21 #include "llvm/Support/VirtualFileSystem.h"
22
23 using namespace clang::driver;
24 using namespace clang::driver::tools;
25 using namespace clang::driver::toolchains;
26 using namespace clang;
27 using namespace llvm::opt;
28
29 // Default hvx-length for various versions.
30 static StringRef getDefaultHvxLength(StringRef Cpu) {
31   return llvm::StringSwitch<StringRef>(Cpu)
32       .Case("v60", "64b")
33       .Case("v62", "64b")
34       .Case("v65", "64b")
35       .Case("v66", "128b")
36       .Default("128b");
37 }
38
39 static void handleHVXWarnings(const Driver &D, const ArgList &Args) {
40   // Handle the unsupported values passed to mhvx-length.
41   if (Arg *A = Args.getLastArg(options::OPT_mhexagon_hvx_length_EQ)) {
42     StringRef Val = A->getValue();
43     if (!Val.equals_lower("64b") && !Val.equals_lower("128b"))
44       D.Diag(diag::err_drv_unsupported_option_argument)
45           << A->getOption().getName() << Val;
46   }
47 }
48
49 // Handle hvx target features explicitly.
50 static void handleHVXTargetFeatures(const Driver &D, const ArgList &Args,
51                                     std::vector<StringRef> &Features,
52                                     bool &HasHVX) {
53   // Handle HVX warnings.
54   handleHVXWarnings(D, Args);
55
56   // Add the +hvx* features based on commandline flags.
57   StringRef HVXFeature, HVXLength;
58   StringRef Cpu(toolchains::HexagonToolChain::GetTargetCPUVersion(Args));
59
60   // Handle -mhvx, -mhvx=, -mno-hvx.
61   if (Arg *A = Args.getLastArg(options::OPT_mno_hexagon_hvx,
62                                options::OPT_mhexagon_hvx,
63                                options::OPT_mhexagon_hvx_EQ)) {
64     if (A->getOption().matches(options::OPT_mno_hexagon_hvx))
65       return;
66     if (A->getOption().matches(options::OPT_mhexagon_hvx_EQ)) {
67       HasHVX = true;
68       HVXFeature = Cpu = A->getValue();
69       HVXFeature = Args.MakeArgString(llvm::Twine("+hvx") + HVXFeature.lower());
70     } else if (A->getOption().matches(options::OPT_mhexagon_hvx)) {
71       HasHVX = true;
72       HVXFeature = Args.MakeArgString(llvm::Twine("+hvx") + Cpu);
73     }
74     Features.push_back(HVXFeature);
75   }
76
77   // Handle -mhvx-length=.
78   if (Arg *A = Args.getLastArg(options::OPT_mhexagon_hvx_length_EQ)) {
79     // These flags are valid only if HVX in enabled.
80     if (!HasHVX)
81       D.Diag(diag::err_drv_invalid_hvx_length);
82     else if (A->getOption().matches(options::OPT_mhexagon_hvx_length_EQ))
83       HVXLength = A->getValue();
84   }
85   // Default hvx-length based on Cpu.
86   else if (HasHVX)
87     HVXLength = getDefaultHvxLength(Cpu);
88
89   if (!HVXLength.empty()) {
90     HVXFeature =
91         Args.MakeArgString(llvm::Twine("+hvx-length") + HVXLength.lower());
92     Features.push_back(HVXFeature);
93   }
94 }
95
96 // Hexagon target features.
97 void hexagon::getHexagonTargetFeatures(const Driver &D, const ArgList &Args,
98                                        std::vector<StringRef> &Features) {
99   handleTargetFeaturesGroup(Args, Features,
100                             options::OPT_m_hexagon_Features_Group);
101
102   bool UseLongCalls = false;
103   if (Arg *A = Args.getLastArg(options::OPT_mlong_calls,
104                                options::OPT_mno_long_calls)) {
105     if (A->getOption().matches(options::OPT_mlong_calls))
106       UseLongCalls = true;
107   }
108
109   Features.push_back(UseLongCalls ? "+long-calls" : "-long-calls");
110
111   bool HasHVX = false;
112   handleHVXTargetFeatures(D, Args, Features, HasHVX);
113
114   if (HexagonToolChain::isAutoHVXEnabled(Args) && !HasHVX)
115     D.Diag(diag::warn_drv_vectorize_needs_hvx);
116 }
117
118 // Hexagon tools start.
119 void hexagon::Assembler::RenderExtraToolArgs(const JobAction &JA,
120                                              ArgStringList &CmdArgs) const {
121 }
122
123 void hexagon::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
124                                       const InputInfo &Output,
125                                       const InputInfoList &Inputs,
126                                       const ArgList &Args,
127                                       const char *LinkingOutput) const {
128   claimNoWarnArgs(Args);
129
130   auto &HTC = static_cast<const toolchains::HexagonToolChain&>(getToolChain());
131   const Driver &D = HTC.getDriver();
132   ArgStringList CmdArgs;
133
134   CmdArgs.push_back("-march=hexagon");
135
136   RenderExtraToolArgs(JA, CmdArgs);
137
138   const char *AsName = "hexagon-llvm-mc";
139   CmdArgs.push_back("-filetype=obj");
140   CmdArgs.push_back(Args.MakeArgString(
141       "-mcpu=hexagon" +
142       toolchains::HexagonToolChain::GetTargetCPUVersion(Args)));
143
144   if (Output.isFilename()) {
145     CmdArgs.push_back("-o");
146     CmdArgs.push_back(Output.getFilename());
147   } else {
148     assert(Output.isNothing() && "Unexpected output");
149     CmdArgs.push_back("-fsyntax-only");
150   }
151
152   if (auto G = toolchains::HexagonToolChain::getSmallDataThreshold(Args)) {
153     CmdArgs.push_back(Args.MakeArgString("-gpsize=" + Twine(G.getValue())));
154   }
155
156   Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
157
158   // Only pass -x if gcc will understand it; otherwise hope gcc
159   // understands the suffix correctly. The main use case this would go
160   // wrong in is for linker inputs if they happened to have an odd
161   // suffix; really the only way to get this to happen is a command
162   // like '-x foobar a.c' which will treat a.c like a linker input.
163   //
164   // FIXME: For the linker case specifically, can we safely convert
165   // inputs into '-Wl,' options?
166   for (const auto &II : Inputs) {
167     // Don't try to pass LLVM or AST inputs to a generic gcc.
168     if (types::isLLVMIR(II.getType()))
169       D.Diag(clang::diag::err_drv_no_linker_llvm_support)
170           << HTC.getTripleString();
171     else if (II.getType() == types::TY_AST)
172       D.Diag(clang::diag::err_drv_no_ast_support)
173           << HTC.getTripleString();
174     else if (II.getType() == types::TY_ModuleFile)
175       D.Diag(diag::err_drv_no_module_support)
176           << HTC.getTripleString();
177
178     if (II.isFilename())
179       CmdArgs.push_back(II.getFilename());
180     else
181       // Don't render as input, we need gcc to do the translations.
182       // FIXME: What is this?
183       II.getInputArg().render(Args, CmdArgs);
184   }
185
186   auto *Exec = Args.MakeArgString(HTC.GetProgramPath(AsName));
187   C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
188 }
189
190 void hexagon::Linker::RenderExtraToolArgs(const JobAction &JA,
191                                           ArgStringList &CmdArgs) const {
192 }
193
194 static void
195 constructHexagonLinkArgs(Compilation &C, const JobAction &JA,
196                          const toolchains::HexagonToolChain &HTC,
197                          const InputInfo &Output, const InputInfoList &Inputs,
198                          const ArgList &Args, ArgStringList &CmdArgs,
199                          const char *LinkingOutput) {
200
201   const Driver &D = HTC.getDriver();
202
203   //----------------------------------------------------------------------------
204   //
205   //----------------------------------------------------------------------------
206   bool IsStatic = Args.hasArg(options::OPT_static);
207   bool IsShared = Args.hasArg(options::OPT_shared);
208   bool IsPIE = Args.hasArg(options::OPT_pie);
209   bool IncStdLib = !Args.hasArg(options::OPT_nostdlib);
210   bool IncStartFiles = !Args.hasArg(options::OPT_nostartfiles);
211   bool IncDefLibs = !Args.hasArg(options::OPT_nodefaultlibs);
212   bool UseG0 = false;
213   bool UseShared = IsShared && !IsStatic;
214
215   //----------------------------------------------------------------------------
216   // Silence warnings for various options
217   //----------------------------------------------------------------------------
218   Args.ClaimAllArgs(options::OPT_g_Group);
219   Args.ClaimAllArgs(options::OPT_emit_llvm);
220   Args.ClaimAllArgs(options::OPT_w); // Other warning options are already
221                                      // handled somewhere else.
222   Args.ClaimAllArgs(options::OPT_static_libgcc);
223
224   //----------------------------------------------------------------------------
225   //
226   //----------------------------------------------------------------------------
227   if (Args.hasArg(options::OPT_s))
228     CmdArgs.push_back("-s");
229
230   if (Args.hasArg(options::OPT_r))
231     CmdArgs.push_back("-r");
232
233   for (const auto &Opt : HTC.ExtraOpts)
234     CmdArgs.push_back(Opt.c_str());
235
236   CmdArgs.push_back("-march=hexagon");
237   StringRef CpuVer = toolchains::HexagonToolChain::GetTargetCPUVersion(Args);
238   CmdArgs.push_back(Args.MakeArgString("-mcpu=hexagon" + CpuVer));
239
240   if (IsShared) {
241     CmdArgs.push_back("-shared");
242     // The following should be the default, but doing as hexagon-gcc does.
243     CmdArgs.push_back("-call_shared");
244   }
245
246   if (IsStatic)
247     CmdArgs.push_back("-static");
248
249   if (IsPIE && !IsShared)
250     CmdArgs.push_back("-pie");
251
252   if (auto G = toolchains::HexagonToolChain::getSmallDataThreshold(Args)) {
253     CmdArgs.push_back(Args.MakeArgString("-G" + Twine(G.getValue())));
254     UseG0 = G.getValue() == 0;
255   }
256
257   //----------------------------------------------------------------------------
258   //
259   //----------------------------------------------------------------------------
260   CmdArgs.push_back("-o");
261   CmdArgs.push_back(Output.getFilename());
262
263   //----------------------------------------------------------------------------
264   // moslib
265   //----------------------------------------------------------------------------
266   std::vector<std::string> OsLibs;
267   bool HasStandalone = false;
268
269   for (const Arg *A : Args.filtered(options::OPT_moslib_EQ)) {
270     A->claim();
271     OsLibs.emplace_back(A->getValue());
272     HasStandalone = HasStandalone || (OsLibs.back() == "standalone");
273   }
274   if (OsLibs.empty()) {
275     OsLibs.push_back("standalone");
276     HasStandalone = true;
277   }
278
279   //----------------------------------------------------------------------------
280   // Start Files
281   //----------------------------------------------------------------------------
282   const std::string MCpuSuffix = "/" + CpuVer.str();
283   const std::string MCpuG0Suffix = MCpuSuffix + "/G0";
284   const std::string RootDir =
285       HTC.getHexagonTargetDir(D.InstalledDir, D.PrefixDirs) + "/";
286   const std::string StartSubDir =
287       "hexagon/lib" + (UseG0 ? MCpuG0Suffix : MCpuSuffix);
288
289   auto Find = [&HTC] (const std::string &RootDir, const std::string &SubDir,
290                       const char *Name) -> std::string {
291     std::string RelName = SubDir + Name;
292     std::string P = HTC.GetFilePath(RelName.c_str());
293     if (llvm::sys::fs::exists(P))
294       return P;
295     return RootDir + RelName;
296   };
297
298   if (IncStdLib && IncStartFiles) {
299     if (!IsShared) {
300       if (HasStandalone) {
301         std::string Crt0SA = Find(RootDir, StartSubDir, "/crt0_standalone.o");
302         CmdArgs.push_back(Args.MakeArgString(Crt0SA));
303       }
304       std::string Crt0 = Find(RootDir, StartSubDir, "/crt0.o");
305       CmdArgs.push_back(Args.MakeArgString(Crt0));
306     }
307     std::string Init = UseShared
308           ? Find(RootDir, StartSubDir + "/pic", "/initS.o")
309           : Find(RootDir, StartSubDir, "/init.o");
310     CmdArgs.push_back(Args.MakeArgString(Init));
311   }
312
313   //----------------------------------------------------------------------------
314   // Library Search Paths
315   //----------------------------------------------------------------------------
316   const ToolChain::path_list &LibPaths = HTC.getFilePaths();
317   for (const auto &LibPath : LibPaths)
318     CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + LibPath));
319
320   //----------------------------------------------------------------------------
321   //
322   //----------------------------------------------------------------------------
323   Args.AddAllArgs(CmdArgs,
324                   {options::OPT_T_Group, options::OPT_e, options::OPT_s,
325                    options::OPT_t, options::OPT_u_Group});
326
327   AddLinkerInputs(HTC, Inputs, Args, CmdArgs, JA);
328
329   //----------------------------------------------------------------------------
330   // Libraries
331   //----------------------------------------------------------------------------
332   if (IncStdLib && IncDefLibs) {
333     if (D.CCCIsCXX()) {
334       if (HTC.ShouldLinkCXXStdlib(Args))
335         HTC.AddCXXStdlibLibArgs(Args, CmdArgs);
336       CmdArgs.push_back("-lm");
337     }
338
339     CmdArgs.push_back("--start-group");
340
341     if (!IsShared) {
342       for (StringRef Lib : OsLibs)
343         CmdArgs.push_back(Args.MakeArgString("-l" + Lib));
344       CmdArgs.push_back("-lc");
345     }
346     CmdArgs.push_back("-lgcc");
347
348     CmdArgs.push_back("--end-group");
349   }
350
351   //----------------------------------------------------------------------------
352   // End files
353   //----------------------------------------------------------------------------
354   if (IncStdLib && IncStartFiles) {
355     std::string Fini = UseShared
356           ? Find(RootDir, StartSubDir + "/pic", "/finiS.o")
357           : Find(RootDir, StartSubDir, "/fini.o");
358     CmdArgs.push_back(Args.MakeArgString(Fini));
359   }
360 }
361
362 void hexagon::Linker::ConstructJob(Compilation &C, const JobAction &JA,
363                                    const InputInfo &Output,
364                                    const InputInfoList &Inputs,
365                                    const ArgList &Args,
366                                    const char *LinkingOutput) const {
367   auto &HTC = static_cast<const toolchains::HexagonToolChain&>(getToolChain());
368
369   ArgStringList CmdArgs;
370   constructHexagonLinkArgs(C, JA, HTC, Output, Inputs, Args, CmdArgs,
371                            LinkingOutput);
372
373   const char *Exec = Args.MakeArgString(HTC.GetLinkerPath());
374   C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
375 }
376 // Hexagon tools end.
377
378 /// Hexagon Toolchain
379
380 std::string HexagonToolChain::getHexagonTargetDir(
381       const std::string &InstalledDir,
382       const SmallVectorImpl<std::string> &PrefixDirs) const {
383   std::string InstallRelDir;
384   const Driver &D = getDriver();
385
386   // Locate the rest of the toolchain ...
387   for (auto &I : PrefixDirs)
388     if (D.getVFS().exists(I))
389       return I;
390
391   if (getVFS().exists(InstallRelDir = InstalledDir + "/../target"))
392     return InstallRelDir;
393
394   return InstalledDir;
395 }
396
397 Optional<unsigned> HexagonToolChain::getSmallDataThreshold(
398       const ArgList &Args) {
399   StringRef Gn = "";
400   if (Arg *A = Args.getLastArg(options::OPT_G)) {
401     Gn = A->getValue();
402   } else if (Args.getLastArg(options::OPT_shared, options::OPT_fpic,
403                              options::OPT_fPIC)) {
404     Gn = "0";
405   }
406
407   unsigned G;
408   if (!Gn.getAsInteger(10, G))
409     return G;
410
411   return None;
412 }
413
414 void HexagonToolChain::getHexagonLibraryPaths(const ArgList &Args,
415       ToolChain::path_list &LibPaths) const {
416   const Driver &D = getDriver();
417
418   //----------------------------------------------------------------------------
419   // -L Args
420   //----------------------------------------------------------------------------
421   for (Arg *A : Args.filtered(options::OPT_L))
422     for (const char *Value : A->getValues())
423       LibPaths.push_back(Value);
424
425   //----------------------------------------------------------------------------
426   // Other standard paths
427   //----------------------------------------------------------------------------
428   std::vector<std::string> RootDirs;
429   std::copy(D.PrefixDirs.begin(), D.PrefixDirs.end(),
430             std::back_inserter(RootDirs));
431
432   std::string TargetDir = getHexagonTargetDir(D.getInstalledDir(),
433                                               D.PrefixDirs);
434   if (std::find(RootDirs.begin(), RootDirs.end(), TargetDir) == RootDirs.end())
435     RootDirs.push_back(TargetDir);
436
437   bool HasPIC = Args.hasArg(options::OPT_fpic, options::OPT_fPIC);
438   // Assume G0 with -shared.
439   bool HasG0 = Args.hasArg(options::OPT_shared);
440   if (auto G = getSmallDataThreshold(Args))
441     HasG0 = G.getValue() == 0;
442
443   const std::string CpuVer = GetTargetCPUVersion(Args).str();
444   for (auto &Dir : RootDirs) {
445     std::string LibDir = Dir + "/hexagon/lib";
446     std::string LibDirCpu = LibDir + '/' + CpuVer;
447     if (HasG0) {
448       if (HasPIC)
449         LibPaths.push_back(LibDirCpu + "/G0/pic");
450       LibPaths.push_back(LibDirCpu + "/G0");
451     }
452     LibPaths.push_back(LibDirCpu);
453     LibPaths.push_back(LibDir);
454   }
455 }
456
457 HexagonToolChain::HexagonToolChain(const Driver &D, const llvm::Triple &Triple,
458                                    const llvm::opt::ArgList &Args)
459     : Linux(D, Triple, Args) {
460   const std::string TargetDir = getHexagonTargetDir(D.getInstalledDir(),
461                                                     D.PrefixDirs);
462
463   // Note: Generic_GCC::Generic_GCC adds InstalledDir and getDriver().Dir to
464   // program paths
465   const std::string BinDir(TargetDir + "/bin");
466   if (D.getVFS().exists(BinDir))
467     getProgramPaths().push_back(BinDir);
468
469   ToolChain::path_list &LibPaths = getFilePaths();
470
471   // Remove paths added by Linux toolchain. Currently Hexagon_TC really targets
472   // 'elf' OS type, so the Linux paths are not appropriate. When we actually
473   // support 'linux' we'll need to fix this up
474   LibPaths.clear();
475   getHexagonLibraryPaths(Args, LibPaths);
476 }
477
478 HexagonToolChain::~HexagonToolChain() {}
479
480 Tool *HexagonToolChain::buildAssembler() const {
481   return new tools::hexagon::Assembler(*this);
482 }
483
484 Tool *HexagonToolChain::buildLinker() const {
485   return new tools::hexagon::Linker(*this);
486 }
487
488 unsigned HexagonToolChain::getOptimizationLevel(
489     const llvm::opt::ArgList &DriverArgs) const {
490   // Copied in large part from lib/Frontend/CompilerInvocation.cpp.
491   Arg *A = DriverArgs.getLastArg(options::OPT_O_Group);
492   if (!A)
493     return 0;
494
495   if (A->getOption().matches(options::OPT_O0))
496     return 0;
497   if (A->getOption().matches(options::OPT_Ofast) ||
498       A->getOption().matches(options::OPT_O4))
499     return 3;
500   assert(A->getNumValues() != 0);
501   StringRef S(A->getValue());
502   if (S == "s" || S == "z" || S.empty())
503     return 2;
504   if (S == "g")
505     return 1;
506
507   unsigned OptLevel;
508   if (S.getAsInteger(10, OptLevel))
509     return 0;
510   return OptLevel;
511 }
512
513 void HexagonToolChain::addClangTargetOptions(const ArgList &DriverArgs,
514                                              ArgStringList &CC1Args,
515                                              Action::OffloadKind) const {
516   if (DriverArgs.hasArg(options::OPT_ffixed_r19)) {
517     CC1Args.push_back("-target-feature");
518     CC1Args.push_back("+reserved-r19");
519   }
520   if (isAutoHVXEnabled(DriverArgs)) {
521     CC1Args.push_back("-mllvm");
522     CC1Args.push_back("-hexagon-autohvx");
523   }
524 }
525
526 void HexagonToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
527                                                  ArgStringList &CC1Args) const {
528   if (DriverArgs.hasArg(options::OPT_nostdinc) ||
529       DriverArgs.hasArg(options::OPT_nostdlibinc))
530     return;
531
532   const Driver &D = getDriver();
533   std::string TargetDir = getHexagonTargetDir(D.getInstalledDir(),
534                                               D.PrefixDirs);
535   addExternCSystemInclude(DriverArgs, CC1Args, TargetDir + "/hexagon/include");
536 }
537
538
539 void HexagonToolChain::addLibStdCxxIncludePaths(
540     const llvm::opt::ArgList &DriverArgs,
541     llvm::opt::ArgStringList &CC1Args) const {
542   const Driver &D = getDriver();
543   std::string TargetDir = getHexagonTargetDir(D.InstalledDir, D.PrefixDirs);
544   addLibStdCXXIncludePaths(TargetDir, "/hexagon/include/c++", "", "", "", "",
545                            DriverArgs, CC1Args);
546 }
547
548 ToolChain::CXXStdlibType
549 HexagonToolChain::GetCXXStdlibType(const ArgList &Args) const {
550   Arg *A = Args.getLastArg(options::OPT_stdlib_EQ);
551   if (!A)
552     return ToolChain::CST_Libstdcxx;
553
554   StringRef Value = A->getValue();
555   if (Value != "libstdc++")
556     getDriver().Diag(diag::err_drv_invalid_stdlib_name) << A->getAsString(Args);
557
558   return ToolChain::CST_Libstdcxx;
559 }
560
561 bool HexagonToolChain::isAutoHVXEnabled(const llvm::opt::ArgList &Args) {
562   if (Arg *A = Args.getLastArg(options::OPT_fvectorize,
563                                options::OPT_fno_vectorize))
564     return A->getOption().matches(options::OPT_fvectorize);
565   return false;
566 }
567
568 //
569 // Returns the default CPU for Hexagon. This is the default compilation target
570 // if no Hexagon processor is selected at the command-line.
571 //
572 const StringRef HexagonToolChain::GetDefaultCPU() {
573   return "hexagonv60";
574 }
575
576 const StringRef HexagonToolChain::GetTargetCPUVersion(const ArgList &Args) {
577   Arg *CpuArg = nullptr;
578   if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ, options::OPT_march_EQ))
579     CpuArg = A;
580
581   StringRef CPU = CpuArg ? CpuArg->getValue() : GetDefaultCPU();
582   if (CPU.startswith("hexagon"))
583     return CPU.substr(sizeof("hexagon") - 1);
584   return CPU;
585 }