106c3fb27SDimitry Andric //===--- RISCV.cpp - RISC-V Helpers for Tools -------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "RISCV.h"
10fcaf7f86SDimitry Andric #include "../Clang.h"
11349cc55cSDimitry Andric #include "ToolChains/CommonArgs.h"
120b57cec5SDimitry Andric #include "clang/Basic/CharInfo.h"
130b57cec5SDimitry Andric #include "clang/Driver/Driver.h"
140b57cec5SDimitry Andric #include "clang/Driver/DriverDiagnostic.h"
150b57cec5SDimitry Andric #include "clang/Driver/Options.h"
16349cc55cSDimitry Andric #include "llvm/Option/ArgList.h"
17349cc55cSDimitry Andric #include "llvm/Support/Error.h"
18349cc55cSDimitry Andric #include "llvm/Support/RISCVISAInfo.h"
190b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
2006c3fb27SDimitry Andric #include "llvm/TargetParser/Host.h"
21bdd1243dSDimitry Andric #include "llvm/TargetParser/RISCVTargetParser.h"
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric using namespace clang::driver;
240b57cec5SDimitry Andric using namespace clang::driver::tools;
250b57cec5SDimitry Andric using namespace clang;
260b57cec5SDimitry Andric using namespace llvm::opt;
270b57cec5SDimitry Andric 
28a7dea167SDimitry Andric // Returns false if an error is diagnosed.
getArchFeatures(const Driver & D,StringRef Arch,std::vector<StringRef> & Features,const ArgList & Args)29349cc55cSDimitry Andric static bool getArchFeatures(const Driver &D, StringRef Arch,
30a7dea167SDimitry Andric                             std::vector<StringRef> &Features,
31a7dea167SDimitry Andric                             const ArgList &Args) {
32349cc55cSDimitry Andric   bool EnableExperimentalExtensions =
33349cc55cSDimitry Andric       Args.hasArg(options::OPT_menable_experimental_extensions);
34349cc55cSDimitry Andric   auto ISAInfo =
35349cc55cSDimitry Andric       llvm::RISCVISAInfo::parseArchString(Arch, EnableExperimentalExtensions);
36349cc55cSDimitry Andric   if (!ISAInfo) {
37349cc55cSDimitry Andric     handleAllErrors(ISAInfo.takeError(), [&](llvm::StringError &ErrMsg) {
380b57cec5SDimitry Andric       D.Diag(diag::err_drv_invalid_riscv_arch_name)
39349cc55cSDimitry Andric           << Arch << ErrMsg.getMessage();
40349cc55cSDimitry Andric     });
41349cc55cSDimitry Andric 
42a7dea167SDimitry Andric     return false;
430b57cec5SDimitry Andric   }
440b57cec5SDimitry Andric 
451db9f3b2SDimitry Andric   for (const std::string &Str : (*ISAInfo)->toFeatures(/*AddAllExtension=*/true,
461db9f3b2SDimitry Andric                                                        /*IgnoreUnknown=*/false))
471db9f3b2SDimitry Andric     Features.push_back(Args.MakeArgString(Str));
48cb14a3feSDimitry Andric 
49cb14a3feSDimitry Andric   if (EnableExperimentalExtensions)
50cb14a3feSDimitry Andric     Features.push_back(Args.MakeArgString("+experimental"));
51cb14a3feSDimitry Andric 
52a7dea167SDimitry Andric   return true;
530b57cec5SDimitry Andric }
540b57cec5SDimitry Andric 
55590d96feSDimitry Andric // Get features except standard extension feature
getRISCFeaturesFromMcpu(const Driver & D,const Arg * A,const llvm::Triple & Triple,StringRef Mcpu,std::vector<StringRef> & Features)5606c3fb27SDimitry Andric static void getRISCFeaturesFromMcpu(const Driver &D, const Arg *A,
5706c3fb27SDimitry Andric                                     const llvm::Triple &Triple,
5806c3fb27SDimitry Andric                                     StringRef Mcpu,
59590d96feSDimitry Andric                                     std::vector<StringRef> &Features) {
60bdd1243dSDimitry Andric   bool Is64Bit = Triple.isRISCV64();
6106c3fb27SDimitry Andric   if (!llvm::RISCV::parseCPU(Mcpu, Is64Bit)) {
6206c3fb27SDimitry Andric     // Try inverting Is64Bit in case the CPU is valid, but for the wrong target.
6306c3fb27SDimitry Andric     if (llvm::RISCV::parseCPU(Mcpu, !Is64Bit))
6406c3fb27SDimitry Andric       D.Diag(clang::diag::err_drv_invalid_riscv_cpu_name_for_target)
6506c3fb27SDimitry Andric           << Mcpu << Is64Bit;
6606c3fb27SDimitry Andric     else
6706c3fb27SDimitry Andric       D.Diag(clang::diag::err_drv_unsupported_option_argument)
6806c3fb27SDimitry Andric           << A->getSpelling() << Mcpu;
6906c3fb27SDimitry Andric   }
705f757f3fSDimitry Andric 
715f757f3fSDimitry Andric   if (llvm::RISCV::hasFastUnalignedAccess(Mcpu))
725f757f3fSDimitry Andric     Features.push_back("+fast-unaligned-access");
73590d96feSDimitry Andric }
74590d96feSDimitry Andric 
getRISCVTargetFeatures(const Driver & D,const llvm::Triple & Triple,const ArgList & Args,std::vector<StringRef> & Features)75a7dea167SDimitry Andric void riscv::getRISCVTargetFeatures(const Driver &D, const llvm::Triple &Triple,
76a7dea167SDimitry Andric                                    const ArgList &Args,
77a7dea167SDimitry Andric                                    std::vector<StringRef> &Features) {
78480093f4SDimitry Andric   StringRef MArch = getRISCVArch(Args, Triple);
79a7dea167SDimitry Andric 
80480093f4SDimitry Andric   if (!getArchFeatures(D, MArch, Features, Args))
81a7dea167SDimitry Andric     return;
82a7dea167SDimitry Andric 
83590d96feSDimitry Andric   // If users give march and mcpu, get std extension feature from MArch
84590d96feSDimitry Andric   // and other features (ex. mirco architecture feature) from mcpu
85bdd1243dSDimitry Andric   if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
86bdd1243dSDimitry Andric     StringRef CPU = A->getValue();
87bdd1243dSDimitry Andric     if (CPU == "native")
88bdd1243dSDimitry Andric       CPU = llvm::sys::getHostCPUName();
8906c3fb27SDimitry Andric 
9006c3fb27SDimitry Andric     getRISCFeaturesFromMcpu(D, A, Triple, CPU, Features);
91bdd1243dSDimitry Andric   }
92590d96feSDimitry Andric 
93480093f4SDimitry Andric   // Handle features corresponding to "-ffixed-X" options
94480093f4SDimitry Andric   if (Args.hasArg(options::OPT_ffixed_x1))
95480093f4SDimitry Andric     Features.push_back("+reserve-x1");
96480093f4SDimitry Andric   if (Args.hasArg(options::OPT_ffixed_x2))
97480093f4SDimitry Andric     Features.push_back("+reserve-x2");
98480093f4SDimitry Andric   if (Args.hasArg(options::OPT_ffixed_x3))
99480093f4SDimitry Andric     Features.push_back("+reserve-x3");
100480093f4SDimitry Andric   if (Args.hasArg(options::OPT_ffixed_x4))
101480093f4SDimitry Andric     Features.push_back("+reserve-x4");
102480093f4SDimitry Andric   if (Args.hasArg(options::OPT_ffixed_x5))
103480093f4SDimitry Andric     Features.push_back("+reserve-x5");
104480093f4SDimitry Andric   if (Args.hasArg(options::OPT_ffixed_x6))
105480093f4SDimitry Andric     Features.push_back("+reserve-x6");
106480093f4SDimitry Andric   if (Args.hasArg(options::OPT_ffixed_x7))
107480093f4SDimitry Andric     Features.push_back("+reserve-x7");
108480093f4SDimitry Andric   if (Args.hasArg(options::OPT_ffixed_x8))
109480093f4SDimitry Andric     Features.push_back("+reserve-x8");
110480093f4SDimitry Andric   if (Args.hasArg(options::OPT_ffixed_x9))
111480093f4SDimitry Andric     Features.push_back("+reserve-x9");
112480093f4SDimitry Andric   if (Args.hasArg(options::OPT_ffixed_x10))
113480093f4SDimitry Andric     Features.push_back("+reserve-x10");
114480093f4SDimitry Andric   if (Args.hasArg(options::OPT_ffixed_x11))
115480093f4SDimitry Andric     Features.push_back("+reserve-x11");
116480093f4SDimitry Andric   if (Args.hasArg(options::OPT_ffixed_x12))
117480093f4SDimitry Andric     Features.push_back("+reserve-x12");
118480093f4SDimitry Andric   if (Args.hasArg(options::OPT_ffixed_x13))
119480093f4SDimitry Andric     Features.push_back("+reserve-x13");
120480093f4SDimitry Andric   if (Args.hasArg(options::OPT_ffixed_x14))
121480093f4SDimitry Andric     Features.push_back("+reserve-x14");
122480093f4SDimitry Andric   if (Args.hasArg(options::OPT_ffixed_x15))
123480093f4SDimitry Andric     Features.push_back("+reserve-x15");
124480093f4SDimitry Andric   if (Args.hasArg(options::OPT_ffixed_x16))
125480093f4SDimitry Andric     Features.push_back("+reserve-x16");
126480093f4SDimitry Andric   if (Args.hasArg(options::OPT_ffixed_x17))
127480093f4SDimitry Andric     Features.push_back("+reserve-x17");
128480093f4SDimitry Andric   if (Args.hasArg(options::OPT_ffixed_x18))
129480093f4SDimitry Andric     Features.push_back("+reserve-x18");
130480093f4SDimitry Andric   if (Args.hasArg(options::OPT_ffixed_x19))
131480093f4SDimitry Andric     Features.push_back("+reserve-x19");
132480093f4SDimitry Andric   if (Args.hasArg(options::OPT_ffixed_x20))
133480093f4SDimitry Andric     Features.push_back("+reserve-x20");
134480093f4SDimitry Andric   if (Args.hasArg(options::OPT_ffixed_x21))
135480093f4SDimitry Andric     Features.push_back("+reserve-x21");
136480093f4SDimitry Andric   if (Args.hasArg(options::OPT_ffixed_x22))
137480093f4SDimitry Andric     Features.push_back("+reserve-x22");
138480093f4SDimitry Andric   if (Args.hasArg(options::OPT_ffixed_x23))
139480093f4SDimitry Andric     Features.push_back("+reserve-x23");
140480093f4SDimitry Andric   if (Args.hasArg(options::OPT_ffixed_x24))
141480093f4SDimitry Andric     Features.push_back("+reserve-x24");
142480093f4SDimitry Andric   if (Args.hasArg(options::OPT_ffixed_x25))
143480093f4SDimitry Andric     Features.push_back("+reserve-x25");
144480093f4SDimitry Andric   if (Args.hasArg(options::OPT_ffixed_x26))
145480093f4SDimitry Andric     Features.push_back("+reserve-x26");
146480093f4SDimitry Andric   if (Args.hasArg(options::OPT_ffixed_x27))
147480093f4SDimitry Andric     Features.push_back("+reserve-x27");
148480093f4SDimitry Andric   if (Args.hasArg(options::OPT_ffixed_x28))
149480093f4SDimitry Andric     Features.push_back("+reserve-x28");
150480093f4SDimitry Andric   if (Args.hasArg(options::OPT_ffixed_x29))
151480093f4SDimitry Andric     Features.push_back("+reserve-x29");
152480093f4SDimitry Andric   if (Args.hasArg(options::OPT_ffixed_x30))
153480093f4SDimitry Andric     Features.push_back("+reserve-x30");
154480093f4SDimitry Andric   if (Args.hasArg(options::OPT_ffixed_x31))
155480093f4SDimitry Andric     Features.push_back("+reserve-x31");
156480093f4SDimitry Andric 
15782343267SDimitry Andric   // FreeBSD local, because ld.lld doesn't support relaxations
15882343267SDimitry Andric   // -mno-relax is default, unless -mrelax is specified.
159fcaf7f86SDimitry Andric   if (Args.hasFlag(options::OPT_mrelax, options::OPT_mno_relax, false)) {
1600b57cec5SDimitry Andric     Features.push_back("+relax");
161fcaf7f86SDimitry Andric     // -gsplit-dwarf -mrelax requires DW_AT_high_pc/DW_AT_ranges/... indexing
162fcaf7f86SDimitry Andric     // into .debug_addr, which is currently not implemented.
163fcaf7f86SDimitry Andric     Arg *A;
164fcaf7f86SDimitry Andric     if (getDebugFissionKind(D, Args, A) != DwarfFissionKind::None)
165fcaf7f86SDimitry Andric       D.Diag(clang::diag::err_drv_riscv_unsupported_with_linker_relaxation)
166fcaf7f86SDimitry Andric           << A->getAsString(Args);
167fcaf7f86SDimitry Andric   } else {
1680b57cec5SDimitry Andric     Features.push_back("-relax");
169fcaf7f86SDimitry Andric   }
1700b57cec5SDimitry Andric 
1715f757f3fSDimitry Andric   // -mno-unaligned-access is default, unless -munaligned-access is specified.
1725f757f3fSDimitry Andric   AddTargetFeature(Args, Features, options::OPT_munaligned_access,
1735f757f3fSDimitry Andric                    options::OPT_mno_unaligned_access, "fast-unaligned-access");
1745f757f3fSDimitry Andric 
1750b57cec5SDimitry Andric   // Now add any that the user explicitly requested on the command line,
1760b57cec5SDimitry Andric   // which may override the defaults.
17706c3fb27SDimitry Andric   handleTargetFeaturesGroup(D, Triple, Args, Features,
17806c3fb27SDimitry Andric                             options::OPT_m_riscv_Features_Group);
1790b57cec5SDimitry Andric }
1800b57cec5SDimitry Andric 
getRISCVABI(const ArgList & Args,const llvm::Triple & Triple)1810b57cec5SDimitry Andric StringRef riscv::getRISCVABI(const ArgList &Args, const llvm::Triple &Triple) {
182bdd1243dSDimitry Andric   assert(Triple.isRISCV() && "Unexpected triple");
183a7dea167SDimitry Andric 
184480093f4SDimitry Andric   // GCC's logic around choosing a default `-mabi=` is complex. If GCC is not
185480093f4SDimitry Andric   // configured using `--with-abi=`, then the logic for the default choice is
186590d96feSDimitry Andric   // defined in config.gcc. This function is based on the logic in GCC 9.2.0.
187480093f4SDimitry Andric   //
188590d96feSDimitry Andric   // The logic used in GCC 9.2.0 is the following, in order:
189480093f4SDimitry Andric   // 1. Explicit choices using `--with-abi=`
190480093f4SDimitry Andric   // 2. A default based on `--with-arch=`, if provided
191480093f4SDimitry Andric   // 3. A default based on the target triple's arch
192480093f4SDimitry Andric   //
193480093f4SDimitry Andric   // The logic in config.gcc is a little circular but it is not inconsistent.
194480093f4SDimitry Andric   //
195480093f4SDimitry Andric   // Clang does not have `--with-arch=` or `--with-abi=`, so we use `-march=`
196480093f4SDimitry Andric   // and `-mabi=` respectively instead.
197590d96feSDimitry Andric   //
198590d96feSDimitry Andric   // In order to make chosing logic more clear, Clang uses the following logic,
199590d96feSDimitry Andric   // in order:
200590d96feSDimitry Andric   // 1. Explicit choices using `-mabi=`
201590d96feSDimitry Andric   // 2. A default based on the architecture as determined by getRISCVArch
202590d96feSDimitry Andric   // 3. Choose a default based on the triple
203480093f4SDimitry Andric 
204480093f4SDimitry Andric   // 1. If `-mabi=` is specified, use it.
205a7dea167SDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT_mabi_EQ))
2060b57cec5SDimitry Andric     return A->getValue();
2070b57cec5SDimitry Andric 
208590d96feSDimitry Andric   // 2. Choose a default based on the target architecture.
209480093f4SDimitry Andric   //
210480093f4SDimitry Andric   // rv32g | rv32*d -> ilp32d
211480093f4SDimitry Andric   // rv32e -> ilp32e
212480093f4SDimitry Andric   // rv32* -> ilp32
213480093f4SDimitry Andric   // rv64g | rv64*d -> lp64d
2147a6dacacSDimitry Andric   // rv64e -> lp64e
215480093f4SDimitry Andric   // rv64* -> lp64
216349cc55cSDimitry Andric   StringRef Arch = getRISCVArch(Args, Triple);
217480093f4SDimitry Andric 
218349cc55cSDimitry Andric   auto ParseResult = llvm::RISCVISAInfo::parseArchString(
219349cc55cSDimitry Andric       Arch, /* EnableExperimentalExtension */ true);
220349cc55cSDimitry Andric   // Ignore parsing error, just go 3rd step.
221647cbc5dSDimitry Andric   if (!llvm::errorToBool(ParseResult.takeError()))
22281ad6265SDimitry Andric     return (*ParseResult)->computeDefaultABI();
223480093f4SDimitry Andric 
224480093f4SDimitry Andric   // 3. Choose a default based on the triple
225480093f4SDimitry Andric   //
226480093f4SDimitry Andric   // We deviate from GCC's defaults here:
227480093f4SDimitry Andric   // - On `riscv{XLEN}-unknown-elf` we use the integer calling convention only.
228480093f4SDimitry Andric   // - On all other OSs we use the double floating point calling convention.
229bdd1243dSDimitry Andric   if (Triple.isRISCV32()) {
230480093f4SDimitry Andric     if (Triple.getOS() == llvm::Triple::UnknownOS)
231480093f4SDimitry Andric       return "ilp32";
232480093f4SDimitry Andric     else
233480093f4SDimitry Andric       return "ilp32d";
234480093f4SDimitry Andric   } else {
235480093f4SDimitry Andric     if (Triple.getOS() == llvm::Triple::UnknownOS)
236480093f4SDimitry Andric       return "lp64";
237480093f4SDimitry Andric     else
238480093f4SDimitry Andric       return "lp64d";
239480093f4SDimitry Andric   }
240480093f4SDimitry Andric }
241480093f4SDimitry Andric 
getRISCVArch(const llvm::opt::ArgList & Args,const llvm::Triple & Triple)242480093f4SDimitry Andric StringRef riscv::getRISCVArch(const llvm::opt::ArgList &Args,
243480093f4SDimitry Andric                               const llvm::Triple &Triple) {
244bdd1243dSDimitry Andric   assert(Triple.isRISCV() && "Unexpected triple");
245480093f4SDimitry Andric 
246480093f4SDimitry Andric   // GCC's logic around choosing a default `-march=` is complex. If GCC is not
247480093f4SDimitry Andric   // configured using `--with-arch=`, then the logic for the default choice is
248480093f4SDimitry Andric   // defined in config.gcc. This function is based on the logic in GCC 9.2.0. We
249590d96feSDimitry Andric   // deviate from GCC's default on additional `-mcpu` option (GCC does not
250590d96feSDimitry Andric   // support `-mcpu`) and baremetal targets (UnknownOS) where neither `-march`
251590d96feSDimitry Andric   // nor `-mabi` is specified.
252480093f4SDimitry Andric   //
253590d96feSDimitry Andric   // The logic used in GCC 9.2.0 is the following, in order:
254480093f4SDimitry Andric   // 1. Explicit choices using `--with-arch=`
255480093f4SDimitry Andric   // 2. A default based on `--with-abi=`, if provided
256480093f4SDimitry Andric   // 3. A default based on the target triple's arch
257480093f4SDimitry Andric   //
258480093f4SDimitry Andric   // The logic in config.gcc is a little circular but it is not inconsistent.
259480093f4SDimitry Andric   //
260480093f4SDimitry Andric   // Clang does not have `--with-arch=` or `--with-abi=`, so we use `-march=`
261480093f4SDimitry Andric   // and `-mabi=` respectively instead.
262480093f4SDimitry Andric   //
263590d96feSDimitry Andric   // Clang uses the following logic, in order:
264590d96feSDimitry Andric   // 1. Explicit choices using `-march=`
265590d96feSDimitry Andric   // 2. Based on `-mcpu` if the target CPU has a default ISA string
266590d96feSDimitry Andric   // 3. A default based on `-mabi`, if provided
267590d96feSDimitry Andric   // 4. A default based on the target triple's arch
268590d96feSDimitry Andric   //
269480093f4SDimitry Andric   // Clang does not yet support MULTILIB_REUSE, so we use `rv{XLEN}imafdc`
270480093f4SDimitry Andric   // instead of `rv{XLEN}gc` though they are (currently) equivalent.
271480093f4SDimitry Andric 
272480093f4SDimitry Andric   // 1. If `-march=` is specified, use it.
273480093f4SDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
274480093f4SDimitry Andric     return A->getValue();
275480093f4SDimitry Andric 
276590d96feSDimitry Andric   // 2. Get march (isa string) based on `-mcpu=`
277590d96feSDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
278bdd1243dSDimitry Andric     StringRef CPU = A->getValue();
279bdd1243dSDimitry Andric     if (CPU == "native")
280bdd1243dSDimitry Andric       CPU = llvm::sys::getHostCPUName();
281bdd1243dSDimitry Andric     StringRef MArch = llvm::RISCV::getMArchFromMcpu(CPU);
282590d96feSDimitry Andric     // Bypass if target cpu's default march is empty.
283590d96feSDimitry Andric     if (MArch != "")
284590d96feSDimitry Andric       return MArch;
285590d96feSDimitry Andric   }
286590d96feSDimitry Andric 
287590d96feSDimitry Andric   // 3. Choose a default based on `-mabi=`
288480093f4SDimitry Andric   //
289480093f4SDimitry Andric   // ilp32e -> rv32e
2907a6dacacSDimitry Andric   // lp64e -> rv64e
291480093f4SDimitry Andric   // ilp32 | ilp32f | ilp32d -> rv32imafdc
292480093f4SDimitry Andric   // lp64 | lp64f | lp64d -> rv64imafdc
293480093f4SDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
294480093f4SDimitry Andric     StringRef MABI = A->getValue();
295480093f4SDimitry Andric 
296fe6060f1SDimitry Andric     if (MABI.equals_insensitive("ilp32e"))
297480093f4SDimitry Andric       return "rv32e";
2987a6dacacSDimitry Andric     else if (MABI.equals_insensitive("lp64e"))
2997a6dacacSDimitry Andric       return "rv64e";
30006c3fb27SDimitry Andric     else if (MABI.starts_with_insensitive("ilp32"))
301480093f4SDimitry Andric       return "rv32imafdc";
30206c3fb27SDimitry Andric     else if (MABI.starts_with_insensitive("lp64")) {
30306c3fb27SDimitry Andric       if (Triple.isAndroid())
3045f757f3fSDimitry Andric         return "rv64imafdcv_zba_zbb_zbs";
30506c3fb27SDimitry Andric 
306480093f4SDimitry Andric       return "rv64imafdc";
307480093f4SDimitry Andric     }
30806c3fb27SDimitry Andric   }
309480093f4SDimitry Andric 
310590d96feSDimitry Andric   // 4. Choose a default based on the triple
311480093f4SDimitry Andric   //
312480093f4SDimitry Andric   // We deviate from GCC's defaults here:
313480093f4SDimitry Andric   // - On `riscv{XLEN}-unknown-elf` we default to `rv{XLEN}imac`
314480093f4SDimitry Andric   // - On all other OSs we use `rv{XLEN}imafdc` (equivalent to `rv{XLEN}gc`)
315bdd1243dSDimitry Andric   if (Triple.isRISCV32()) {
316480093f4SDimitry Andric     if (Triple.getOS() == llvm::Triple::UnknownOS)
317480093f4SDimitry Andric       return "rv32imac";
318480093f4SDimitry Andric     else
319480093f4SDimitry Andric       return "rv32imafdc";
320480093f4SDimitry Andric   } else {
321480093f4SDimitry Andric     if (Triple.getOS() == llvm::Triple::UnknownOS)
322480093f4SDimitry Andric       return "rv64imac";
32306c3fb27SDimitry Andric     else if (Triple.isAndroid())
3245f757f3fSDimitry Andric       return "rv64imafdcv_zba_zbb_zbs";
325480093f4SDimitry Andric     else
326480093f4SDimitry Andric       return "rv64imafdc";
327480093f4SDimitry Andric   }
3280b57cec5SDimitry Andric }
329bdd1243dSDimitry Andric 
getRISCVTargetCPU(const llvm::opt::ArgList & Args,const llvm::Triple & Triple)330bdd1243dSDimitry Andric std::string riscv::getRISCVTargetCPU(const llvm::opt::ArgList &Args,
331bdd1243dSDimitry Andric                                      const llvm::Triple &Triple) {
332bdd1243dSDimitry Andric   std::string CPU;
333bdd1243dSDimitry Andric   // If we have -mcpu, use that.
334bdd1243dSDimitry Andric   if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
335bdd1243dSDimitry Andric     CPU = A->getValue();
336bdd1243dSDimitry Andric 
337bdd1243dSDimitry Andric   // Handle CPU name is 'native'.
338bdd1243dSDimitry Andric   if (CPU == "native")
339bdd1243dSDimitry Andric     CPU = llvm::sys::getHostCPUName();
340bdd1243dSDimitry Andric 
341bdd1243dSDimitry Andric   if (!CPU.empty())
342bdd1243dSDimitry Andric     return CPU;
343bdd1243dSDimitry Andric 
344bdd1243dSDimitry Andric   return Triple.isRISCV64() ? "generic-rv64" : "generic-rv32";
345bdd1243dSDimitry Andric }
346