Coverage Report

Created: 2024-01-17 10:31

/src/llvm-project/clang/lib/Driver/ToolChains/Arch/ARM.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- ARM.cpp - ARM (not AArch64) Helpers for Tools ----------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "ARM.h"
10
#include "clang/Driver/Driver.h"
11
#include "clang/Driver/DriverDiagnostic.h"
12
#include "clang/Driver/Options.h"
13
#include "llvm/ADT/StringSwitch.h"
14
#include "llvm/Option/ArgList.h"
15
#include "llvm/TargetParser/ARMTargetParser.h"
16
#include "llvm/TargetParser/Host.h"
17
18
using namespace clang::driver;
19
using namespace clang::driver::tools;
20
using namespace clang;
21
using namespace llvm::opt;
22
23
// Get SubArch (vN).
24
0
int arm::getARMSubArchVersionNumber(const llvm::Triple &Triple) {
25
0
  llvm::StringRef Arch = Triple.getArchName();
26
0
  return llvm::ARM::parseArchVersion(Arch);
27
0
}
28
29
// True if M-profile.
30
0
bool arm::isARMMProfile(const llvm::Triple &Triple) {
31
0
  llvm::StringRef Arch = Triple.getArchName();
32
0
  return llvm::ARM::parseArchProfile(Arch) == llvm::ARM::ProfileKind::M;
33
0
}
34
35
// On Arm the endianness of the output file is determined by the target and
36
// can be overridden by the pseudo-target flags '-mlittle-endian'/'-EL' and
37
// '-mbig-endian'/'-EB'. Unlike other targets the flag does not result in a
38
// normalized triple so we must handle the flag here.
39
0
bool arm::isARMBigEndian(const llvm::Triple &Triple, const ArgList &Args) {
40
0
  if (Arg *A = Args.getLastArg(options::OPT_mlittle_endian,
41
0
                               options::OPT_mbig_endian)) {
42
0
    return !A->getOption().matches(options::OPT_mlittle_endian);
43
0
  }
44
45
0
  return Triple.getArch() == llvm::Triple::armeb ||
46
0
         Triple.getArch() == llvm::Triple::thumbeb;
47
0
}
48
49
// True if A-profile.
50
0
bool arm::isARMAProfile(const llvm::Triple &Triple) {
51
0
  llvm::StringRef Arch = Triple.getArchName();
52
0
  return llvm::ARM::parseArchProfile(Arch) == llvm::ARM::ProfileKind::A;
53
0
}
54
55
// Get Arch/CPU from args.
56
void arm::getARMArchCPUFromArgs(const ArgList &Args, llvm::StringRef &Arch,
57
0
                                llvm::StringRef &CPU, bool FromAs) {
58
0
  if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_mcpu_EQ))
59
0
    CPU = A->getValue();
60
0
  if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
61
0
    Arch = A->getValue();
62
0
  if (!FromAs)
63
0
    return;
64
65
0
  for (const Arg *A :
66
0
       Args.filtered(options::OPT_Wa_COMMA, options::OPT_Xassembler)) {
67
    // Use getValues because -Wa can have multiple arguments
68
    // e.g. -Wa,-mcpu=foo,-mcpu=bar
69
0
    for (StringRef Value : A->getValues()) {
70
0
      if (Value.starts_with("-mcpu="))
71
0
        CPU = Value.substr(6);
72
0
      if (Value.starts_with("-march="))
73
0
        Arch = Value.substr(7);
74
0
    }
75
0
  }
76
0
}
77
78
// Handle -mhwdiv=.
79
// FIXME: Use ARMTargetParser.
80
static void getARMHWDivFeatures(const Driver &D, const Arg *A,
81
                                const ArgList &Args, StringRef HWDiv,
82
0
                                std::vector<StringRef> &Features) {
83
0
  uint64_t HWDivID = llvm::ARM::parseHWDiv(HWDiv);
84
0
  if (!llvm::ARM::getHWDivFeatures(HWDivID, Features))
85
0
    D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args);
86
0
}
87
88
// Handle -mfpu=.
89
static llvm::ARM::FPUKind getARMFPUFeatures(const Driver &D, const Arg *A,
90
                                            const ArgList &Args, StringRef FPU,
91
0
                                            std::vector<StringRef> &Features) {
92
0
  llvm::ARM::FPUKind FPUKind = llvm::ARM::parseFPU(FPU);
93
0
  if (!llvm::ARM::getFPUFeatures(FPUKind, Features))
94
0
    D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args);
95
0
  return FPUKind;
96
0
}
97
98
// Decode ARM features from string like +[no]featureA+[no]featureB+...
99
static bool DecodeARMFeatures(const Driver &D, StringRef text, StringRef CPU,
100
                              llvm::ARM::ArchKind ArchKind,
101
                              std::vector<StringRef> &Features,
102
0
                              llvm::ARM::FPUKind &ArgFPUKind) {
103
0
  SmallVector<StringRef, 8> Split;
104
0
  text.split(Split, StringRef("+"), -1, false);
105
106
0
  for (StringRef Feature : Split) {
107
0
    if (!appendArchExtFeatures(CPU, ArchKind, Feature, Features, ArgFPUKind))
108
0
      return false;
109
0
  }
110
0
  return true;
111
0
}
112
113
static void DecodeARMFeaturesFromCPU(const Driver &D, StringRef CPU,
114
0
                                     std::vector<StringRef> &Features) {
115
0
  CPU = CPU.split("+").first;
116
0
  if (CPU != "generic") {
117
0
    llvm::ARM::ArchKind ArchKind = llvm::ARM::parseCPUArch(CPU);
118
0
    uint64_t Extension = llvm::ARM::getDefaultExtensions(CPU, ArchKind);
119
0
    llvm::ARM::getExtensionFeatures(Extension, Features);
120
0
  }
121
0
}
122
123
// Check if -march is valid by checking if it can be canonicalised and parsed.
124
// getARMArch is used here instead of just checking the -march value in order
125
// to handle -march=native correctly.
126
static void checkARMArchName(const Driver &D, const Arg *A, const ArgList &Args,
127
                             llvm::StringRef ArchName, llvm::StringRef CPUName,
128
                             std::vector<StringRef> &Features,
129
                             const llvm::Triple &Triple,
130
0
                             llvm::ARM::FPUKind &ArgFPUKind) {
131
0
  std::pair<StringRef, StringRef> Split = ArchName.split("+");
132
133
0
  std::string MArch = arm::getARMArch(ArchName, Triple);
134
0
  llvm::ARM::ArchKind ArchKind = llvm::ARM::parseArch(MArch);
135
0
  if (ArchKind == llvm::ARM::ArchKind::INVALID ||
136
0
      (Split.second.size() &&
137
0
       !DecodeARMFeatures(D, Split.second, CPUName, ArchKind, Features,
138
0
                          ArgFPUKind)))
139
0
    D.Diag(clang::diag::err_drv_unsupported_option_argument)
140
0
        << A->getSpelling() << A->getValue();
141
0
}
142
143
// Check -mcpu=. Needs ArchName to handle -mcpu=generic.
144
static void checkARMCPUName(const Driver &D, const Arg *A, const ArgList &Args,
145
                            llvm::StringRef CPUName, llvm::StringRef ArchName,
146
                            std::vector<StringRef> &Features,
147
                            const llvm::Triple &Triple,
148
0
                            llvm::ARM::FPUKind &ArgFPUKind) {
149
0
  std::pair<StringRef, StringRef> Split = CPUName.split("+");
150
151
0
  std::string CPU = arm::getARMTargetCPU(CPUName, ArchName, Triple);
152
0
  llvm::ARM::ArchKind ArchKind =
153
0
    arm::getLLVMArchKindForARM(CPU, ArchName, Triple);
154
0
  if (ArchKind == llvm::ARM::ArchKind::INVALID ||
155
0
      (Split.second.size() && !DecodeARMFeatures(D, Split.second, CPU, ArchKind,
156
0
                                                 Features, ArgFPUKind)))
157
0
    D.Diag(clang::diag::err_drv_unsupported_option_argument)
158
0
        << A->getSpelling() << A->getValue();
159
0
}
160
161
// If -mfloat-abi=hard or -mhard-float are specified explicitly then check that
162
// floating point registers are available on the target CPU.
163
static void checkARMFloatABI(const Driver &D, const ArgList &Args,
164
0
                             bool HasFPRegs) {
165
0
  if (HasFPRegs)
166
0
    return;
167
0
  const Arg *A =
168
0
      Args.getLastArg(options::OPT_msoft_float, options::OPT_mhard_float,
169
0
                      options::OPT_mfloat_abi_EQ);
170
0
  if (A && (A->getOption().matches(options::OPT_mhard_float) ||
171
0
            (A->getOption().matches(options::OPT_mfloat_abi_EQ) &&
172
0
             A->getValue() == StringRef("hard"))))
173
0
    D.Diag(clang::diag::warn_drv_no_floating_point_registers)
174
0
        << A->getAsString(Args);
175
0
}
176
177
0
bool arm::useAAPCSForMachO(const llvm::Triple &T) {
178
  // The backend is hardwired to assume AAPCS for M-class processors, ensure
179
  // the frontend matches that.
180
0
  return T.getEnvironment() == llvm::Triple::EABI ||
181
0
         T.getEnvironment() == llvm::Triple::EABIHF ||
182
0
         T.getOS() == llvm::Triple::UnknownOS || isARMMProfile(T);
183
0
}
184
185
// We follow GCC and support when the backend has support for the MRC/MCR
186
// instructions that are used to set the hard thread pointer ("CP15 C13
187
// Thread id").
188
0
bool arm::isHardTPSupported(const llvm::Triple &Triple) {
189
0
  int Ver = getARMSubArchVersionNumber(Triple);
190
0
  llvm::ARM::ArchKind AK = llvm::ARM::parseArch(Triple.getArchName());
191
0
  return Triple.isARM() || AK == llvm::ARM::ArchKind::ARMV6T2 ||
192
0
         (Ver >= 7 && AK != llvm::ARM::ArchKind::ARMV8MBaseline);
193
0
}
194
195
// Select mode for reading thread pointer (-mtp=soft/cp15).
196
arm::ReadTPMode arm::getReadTPMode(const Driver &D, const ArgList &Args,
197
0
                                   const llvm::Triple &Triple, bool ForAS) {
198
0
  if (Arg *A = Args.getLastArg(options::OPT_mtp_mode_EQ)) {
199
0
    arm::ReadTPMode ThreadPointer =
200
0
        llvm::StringSwitch<arm::ReadTPMode>(A->getValue())
201
0
            .Case("cp15", ReadTPMode::TPIDRURO)
202
0
            .Case("tpidrurw", ReadTPMode::TPIDRURW)
203
0
            .Case("tpidruro", ReadTPMode::TPIDRURO)
204
0
            .Case("tpidrprw", ReadTPMode::TPIDRPRW)
205
0
            .Case("soft", ReadTPMode::Soft)
206
0
            .Default(ReadTPMode::Invalid);
207
0
    if ((ThreadPointer == ReadTPMode::TPIDRURW ||
208
0
         ThreadPointer == ReadTPMode::TPIDRURO ||
209
0
         ThreadPointer == ReadTPMode::TPIDRPRW) &&
210
0
        !isHardTPSupported(Triple) && !ForAS) {
211
0
      D.Diag(diag::err_target_unsupported_tp_hard) << Triple.getArchName();
212
0
      return ReadTPMode::Invalid;
213
0
    }
214
0
    if (ThreadPointer != ReadTPMode::Invalid)
215
0
      return ThreadPointer;
216
0
    if (StringRef(A->getValue()).empty())
217
0
      D.Diag(diag::err_drv_missing_arg_mtp) << A->getAsString(Args);
218
0
    else
219
0
      D.Diag(diag::err_drv_invalid_mtp) << A->getAsString(Args);
220
0
    return ReadTPMode::Invalid;
221
0
  }
222
0
  return ReadTPMode::Soft;
223
0
}
224
225
void arm::setArchNameInTriple(const Driver &D, const ArgList &Args,
226
0
                              types::ID InputType, llvm::Triple &Triple) {
227
0
  StringRef MCPU, MArch;
228
0
  if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
229
0
    MCPU = A->getValue();
230
0
  if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
231
0
    MArch = A->getValue();
232
233
0
  std::string CPU = Triple.isOSBinFormatMachO()
234
0
                        ? tools::arm::getARMCPUForMArch(MArch, Triple).str()
235
0
                        : tools::arm::getARMTargetCPU(MCPU, MArch, Triple);
236
0
  StringRef Suffix = tools::arm::getLLVMArchSuffixForARM(CPU, MArch, Triple);
237
238
0
  bool IsBigEndian = Triple.getArch() == llvm::Triple::armeb ||
239
0
                     Triple.getArch() == llvm::Triple::thumbeb;
240
  // Handle pseudo-target flags '-mlittle-endian'/'-EL' and
241
  // '-mbig-endian'/'-EB'.
242
0
  if (Arg *A = Args.getLastArg(options::OPT_mlittle_endian,
243
0
                               options::OPT_mbig_endian)) {
244
0
    IsBigEndian = !A->getOption().matches(options::OPT_mlittle_endian);
245
0
  }
246
0
  std::string ArchName = IsBigEndian ? "armeb" : "arm";
247
248
  // FIXME: Thumb should just be another -target-feaure, not in the triple.
249
0
  bool IsMProfile =
250
0
      llvm::ARM::parseArchProfile(Suffix) == llvm::ARM::ProfileKind::M;
251
0
  bool ThumbDefault = IsMProfile ||
252
                      // Thumb2 is the default for V7 on Darwin.
253
0
                      (llvm::ARM::parseArchVersion(Suffix) == 7 &&
254
0
                       Triple.isOSBinFormatMachO()) ||
255
                      // FIXME: this is invalid for WindowsCE
256
0
                      Triple.isOSWindows();
257
258
  // Check if ARM ISA was explicitly selected (using -mno-thumb or -marm) for
259
  // M-Class CPUs/architecture variants, which is not supported.
260
0
  bool ARMModeRequested =
261
0
      !Args.hasFlag(options::OPT_mthumb, options::OPT_mno_thumb, ThumbDefault);
262
0
  if (IsMProfile && ARMModeRequested) {
263
0
    if (MCPU.size())
264
0
      D.Diag(diag::err_cpu_unsupported_isa) << CPU << "ARM";
265
0
    else
266
0
      D.Diag(diag::err_arch_unsupported_isa)
267
0
          << tools::arm::getARMArch(MArch, Triple) << "ARM";
268
0
  }
269
270
  // Check to see if an explicit choice to use thumb has been made via
271
  // -mthumb. For assembler files we must check for -mthumb in the options
272
  // passed to the assembler via -Wa or -Xassembler.
273
0
  bool IsThumb = false;
274
0
  if (InputType != types::TY_PP_Asm)
275
0
    IsThumb =
276
0
        Args.hasFlag(options::OPT_mthumb, options::OPT_mno_thumb, ThumbDefault);
277
0
  else {
278
    // Ideally we would check for these flags in
279
    // CollectArgsForIntegratedAssembler but we can't change the ArchName at
280
    // that point.
281
0
    llvm::StringRef WaMArch, WaMCPU;
282
0
    for (const auto *A :
283
0
         Args.filtered(options::OPT_Wa_COMMA, options::OPT_Xassembler)) {
284
0
      for (StringRef Value : A->getValues()) {
285
        // There is no assembler equivalent of -mno-thumb, -marm, or -mno-arm.
286
0
        if (Value == "-mthumb")
287
0
          IsThumb = true;
288
0
        else if (Value.starts_with("-march="))
289
0
          WaMArch = Value.substr(7);
290
0
        else if (Value.starts_with("-mcpu="))
291
0
          WaMCPU = Value.substr(6);
292
0
      }
293
0
    }
294
295
0
    if (WaMCPU.size() || WaMArch.size()) {
296
      // The way this works means that we prefer -Wa,-mcpu's architecture
297
      // over -Wa,-march. Which matches the compiler behaviour.
298
0
      Suffix = tools::arm::getLLVMArchSuffixForARM(WaMCPU, WaMArch, Triple);
299
0
    }
300
0
  }
301
302
  // Assembly files should start in ARM mode, unless arch is M-profile, or
303
  // -mthumb has been passed explicitly to the assembler. Windows is always
304
  // thumb.
305
0
  if (IsThumb || IsMProfile || Triple.isOSWindows()) {
306
0
    if (IsBigEndian)
307
0
      ArchName = "thumbeb";
308
0
    else
309
0
      ArchName = "thumb";
310
0
  }
311
0
  Triple.setArchName(ArchName + Suffix.str());
312
0
}
313
314
void arm::setFloatABIInTriple(const Driver &D, const ArgList &Args,
315
0
                              llvm::Triple &Triple) {
316
0
  if (Triple.isOSLiteOS()) {
317
0
    Triple.setEnvironment(llvm::Triple::OpenHOS);
318
0
    return;
319
0
  }
320
321
0
  bool isHardFloat =
322
0
      (arm::getARMFloatABI(D, Triple, Args) == arm::FloatABI::Hard);
323
324
0
  switch (Triple.getEnvironment()) {
325
0
  case llvm::Triple::GNUEABI:
326
0
  case llvm::Triple::GNUEABIHF:
327
0
    Triple.setEnvironment(isHardFloat ? llvm::Triple::GNUEABIHF
328
0
                                      : llvm::Triple::GNUEABI);
329
0
    break;
330
0
  case llvm::Triple::EABI:
331
0
  case llvm::Triple::EABIHF:
332
0
    Triple.setEnvironment(isHardFloat ? llvm::Triple::EABIHF
333
0
                                      : llvm::Triple::EABI);
334
0
    break;
335
0
  case llvm::Triple::MuslEABI:
336
0
  case llvm::Triple::MuslEABIHF:
337
0
    Triple.setEnvironment(isHardFloat ? llvm::Triple::MuslEABIHF
338
0
                                      : llvm::Triple::MuslEABI);
339
0
    break;
340
0
  case llvm::Triple::OpenHOS:
341
0
    break;
342
0
  default: {
343
0
    arm::FloatABI DefaultABI = arm::getDefaultFloatABI(Triple);
344
0
    if (DefaultABI != arm::FloatABI::Invalid &&
345
0
        isHardFloat != (DefaultABI == arm::FloatABI::Hard)) {
346
0
      Arg *ABIArg =
347
0
          Args.getLastArg(options::OPT_msoft_float, options::OPT_mhard_float,
348
0
                          options::OPT_mfloat_abi_EQ);
349
0
      assert(ABIArg && "Non-default float abi expected to be from arg");
350
0
      D.Diag(diag::err_drv_unsupported_opt_for_target)
351
0
          << ABIArg->getAsString(Args) << Triple.getTriple();
352
0
    }
353
0
    break;
354
0
  }
355
0
  }
356
0
}
357
358
0
arm::FloatABI arm::getARMFloatABI(const ToolChain &TC, const ArgList &Args) {
359
0
  return arm::getARMFloatABI(TC.getDriver(), TC.getEffectiveTriple(), Args);
360
0
}
361
362
0
arm::FloatABI arm::getDefaultFloatABI(const llvm::Triple &Triple) {
363
0
  auto SubArch = getARMSubArchVersionNumber(Triple);
364
0
  switch (Triple.getOS()) {
365
0
  case llvm::Triple::Darwin:
366
0
  case llvm::Triple::MacOSX:
367
0
  case llvm::Triple::IOS:
368
0
  case llvm::Triple::TvOS:
369
0
  case llvm::Triple::DriverKit:
370
    // Darwin defaults to "softfp" for v6 and v7.
371
0
    if (Triple.isWatchABI())
372
0
      return FloatABI::Hard;
373
0
    else
374
0
      return (SubArch == 6 || SubArch == 7) ? FloatABI::SoftFP : FloatABI::Soft;
375
376
0
  case llvm::Triple::WatchOS:
377
0
    return FloatABI::Hard;
378
379
  // FIXME: this is invalid for WindowsCE
380
0
  case llvm::Triple::Win32:
381
    // It is incorrect to select hard float ABI on MachO platforms if the ABI is
382
    // "apcs-gnu".
383
0
    if (Triple.isOSBinFormatMachO() && !useAAPCSForMachO(Triple))
384
0
      return FloatABI::Soft;
385
0
    return FloatABI::Hard;
386
387
0
  case llvm::Triple::NetBSD:
388
0
    switch (Triple.getEnvironment()) {
389
0
    case llvm::Triple::EABIHF:
390
0
    case llvm::Triple::GNUEABIHF:
391
0
      return FloatABI::Hard;
392
0
    default:
393
0
      return FloatABI::Soft;
394
0
    }
395
0
    break;
396
397
0
  case llvm::Triple::FreeBSD:
398
0
    switch (Triple.getEnvironment()) {
399
0
    case llvm::Triple::GNUEABIHF:
400
0
      return FloatABI::Hard;
401
0
    default:
402
      // FreeBSD defaults to soft float
403
0
      return FloatABI::Soft;
404
0
    }
405
0
    break;
406
407
0
  case llvm::Triple::Haiku:
408
0
  case llvm::Triple::OpenBSD:
409
0
    return FloatABI::SoftFP;
410
411
0
  default:
412
0
    if (Triple.isOHOSFamily())
413
0
      return FloatABI::Soft;
414
0
    switch (Triple.getEnvironment()) {
415
0
    case llvm::Triple::GNUEABIHF:
416
0
    case llvm::Triple::MuslEABIHF:
417
0
    case llvm::Triple::EABIHF:
418
0
      return FloatABI::Hard;
419
0
    case llvm::Triple::GNUEABI:
420
0
    case llvm::Triple::MuslEABI:
421
0
    case llvm::Triple::EABI:
422
      // EABI is always AAPCS, and if it was not marked 'hard', it's softfp
423
0
      return FloatABI::SoftFP;
424
0
    case llvm::Triple::Android:
425
0
      return (SubArch >= 7) ? FloatABI::SoftFP : FloatABI::Soft;
426
0
    default:
427
0
      return FloatABI::Invalid;
428
0
    }
429
0
  }
430
0
  return FloatABI::Invalid;
431
0
}
432
433
// Select the float ABI as determined by -msoft-float, -mhard-float, and
434
// -mfloat-abi=.
435
arm::FloatABI arm::getARMFloatABI(const Driver &D, const llvm::Triple &Triple,
436
0
                                  const ArgList &Args) {
437
0
  arm::FloatABI ABI = FloatABI::Invalid;
438
0
  if (Arg *A =
439
0
          Args.getLastArg(options::OPT_msoft_float, options::OPT_mhard_float,
440
0
                          options::OPT_mfloat_abi_EQ)) {
441
0
    if (A->getOption().matches(options::OPT_msoft_float)) {
442
0
      ABI = FloatABI::Soft;
443
0
    } else if (A->getOption().matches(options::OPT_mhard_float)) {
444
0
      ABI = FloatABI::Hard;
445
0
    } else {
446
0
      ABI = llvm::StringSwitch<arm::FloatABI>(A->getValue())
447
0
                .Case("soft", FloatABI::Soft)
448
0
                .Case("softfp", FloatABI::SoftFP)
449
0
                .Case("hard", FloatABI::Hard)
450
0
                .Default(FloatABI::Invalid);
451
0
      if (ABI == FloatABI::Invalid && !StringRef(A->getValue()).empty()) {
452
0
        D.Diag(diag::err_drv_invalid_mfloat_abi) << A->getAsString(Args);
453
0
        ABI = FloatABI::Soft;
454
0
      }
455
0
    }
456
0
  }
457
458
  // If unspecified, choose the default based on the platform.
459
0
  if (ABI == FloatABI::Invalid)
460
0
    ABI = arm::getDefaultFloatABI(Triple);
461
462
0
  if (ABI == FloatABI::Invalid) {
463
    // Assume "soft", but warn the user we are guessing.
464
0
    if (Triple.isOSBinFormatMachO() &&
465
0
        Triple.getSubArch() == llvm::Triple::ARMSubArch_v7em)
466
0
      ABI = FloatABI::Hard;
467
0
    else
468
0
      ABI = FloatABI::Soft;
469
470
0
    if (Triple.getOS() != llvm::Triple::UnknownOS ||
471
0
        !Triple.isOSBinFormatMachO())
472
0
      D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
473
0
  }
474
475
0
  assert(ABI != FloatABI::Invalid && "must select an ABI");
476
0
  return ABI;
477
0
}
478
479
0
static bool hasIntegerMVE(const std::vector<StringRef> &F) {
480
0
  auto MVE = llvm::find(llvm::reverse(F), "+mve");
481
0
  auto NoMVE = llvm::find(llvm::reverse(F), "-mve");
482
0
  return MVE != F.rend() &&
483
0
         (NoMVE == F.rend() || std::distance(MVE, NoMVE) > 0);
484
0
}
485
486
llvm::ARM::FPUKind arm::getARMTargetFeatures(const Driver &D,
487
                                             const llvm::Triple &Triple,
488
                                             const ArgList &Args,
489
                                             std::vector<StringRef> &Features,
490
0
                                             bool ForAS, bool ForMultilib) {
491
0
  bool KernelOrKext =
492
0
      Args.hasArg(options::OPT_mkernel, options::OPT_fapple_kext);
493
0
  arm::FloatABI ABI = arm::getARMFloatABI(D, Triple, Args);
494
0
  std::optional<std::pair<const Arg *, StringRef>> WaCPU, WaFPU, WaHDiv, WaArch;
495
496
  // This vector will accumulate features from the architecture
497
  // extension suffixes on -mcpu and -march (e.g. the 'bar' in
498
  // -mcpu=foo+bar). We want to apply those after the features derived
499
  // from the FPU, in case -mfpu generates a negative feature which
500
  // the +bar is supposed to override.
501
0
  std::vector<StringRef> ExtensionFeatures;
502
503
0
  if (!ForAS) {
504
    // FIXME: Note, this is a hack, the LLVM backend doesn't actually use these
505
    // yet (it uses the -mfloat-abi and -msoft-float options), and it is
506
    // stripped out by the ARM target. We should probably pass this a new
507
    // -target-option, which is handled by the -cc1/-cc1as invocation.
508
    //
509
    // FIXME2:  For consistency, it would be ideal if we set up the target
510
    // machine state the same when using the frontend or the assembler. We don't
511
    // currently do that for the assembler, we pass the options directly to the
512
    // backend and never even instantiate the frontend TargetInfo. If we did,
513
    // and used its handleTargetFeatures hook, then we could ensure the
514
    // assembler and the frontend behave the same.
515
516
    // Use software floating point operations?
517
0
    if (ABI == arm::FloatABI::Soft)
518
0
      Features.push_back("+soft-float");
519
520
    // Use software floating point argument passing?
521
0
    if (ABI != arm::FloatABI::Hard)
522
0
      Features.push_back("+soft-float-abi");
523
0
  } else {
524
    // Here, we make sure that -Wa,-mfpu/cpu/arch/hwdiv will be passed down
525
    // to the assembler correctly.
526
0
    for (const Arg *A :
527
0
         Args.filtered(options::OPT_Wa_COMMA, options::OPT_Xassembler)) {
528
      // We use getValues here because you can have many options per -Wa
529
      // We will keep the last one we find for each of these
530
0
      for (StringRef Value : A->getValues()) {
531
0
        if (Value.starts_with("-mfpu=")) {
532
0
          WaFPU = std::make_pair(A, Value.substr(6));
533
0
        } else if (Value.starts_with("-mcpu=")) {
534
0
          WaCPU = std::make_pair(A, Value.substr(6));
535
0
        } else if (Value.starts_with("-mhwdiv=")) {
536
0
          WaHDiv = std::make_pair(A, Value.substr(8));
537
0
        } else if (Value.starts_with("-march=")) {
538
0
          WaArch = std::make_pair(A, Value.substr(7));
539
0
        }
540
0
      }
541
0
    }
542
543
    // The integrated assembler doesn't implement e_flags setting behavior for
544
    // -meabi=gnu (gcc -mabi={apcs-gnu,atpcs} passes -meabi=gnu to gas). For
545
    // compatibility we accept but warn.
546
0
    if (Arg *A = Args.getLastArgNoClaim(options::OPT_mabi_EQ))
547
0
      A->ignoreTargetSpecific();
548
0
  }
549
550
0
  if (getReadTPMode(D, Args, Triple, ForAS) == ReadTPMode::TPIDRURW)
551
0
    Features.push_back("+read-tp-tpidrurw");
552
0
  if (getReadTPMode(D, Args, Triple, ForAS) == ReadTPMode::TPIDRURO)
553
0
    Features.push_back("+read-tp-tpidruro");
554
0
  if (getReadTPMode(D, Args, Triple, ForAS) == ReadTPMode::TPIDRPRW)
555
0
    Features.push_back("+read-tp-tpidrprw");
556
557
0
  const Arg *ArchArg = Args.getLastArg(options::OPT_march_EQ);
558
0
  const Arg *CPUArg = Args.getLastArg(options::OPT_mcpu_EQ);
559
0
  StringRef ArchName;
560
0
  StringRef CPUName;
561
0
  llvm::ARM::FPUKind ArchArgFPUKind = llvm::ARM::FK_INVALID;
562
0
  llvm::ARM::FPUKind CPUArgFPUKind = llvm::ARM::FK_INVALID;
563
564
  // Check -mcpu. ClangAs gives preference to -Wa,-mcpu=.
565
0
  if (WaCPU) {
566
0
    if (CPUArg)
567
0
      D.Diag(clang::diag::warn_drv_unused_argument)
568
0
          << CPUArg->getAsString(Args);
569
0
    CPUName = WaCPU->second;
570
0
    CPUArg = WaCPU->first;
571
0
  } else if (CPUArg)
572
0
    CPUName = CPUArg->getValue();
573
574
  // Check -march. ClangAs gives preference to -Wa,-march=.
575
0
  if (WaArch) {
576
0
    if (ArchArg)
577
0
      D.Diag(clang::diag::warn_drv_unused_argument)
578
0
          << ArchArg->getAsString(Args);
579
0
    ArchName = WaArch->second;
580
    // This will set any features after the base architecture.
581
0
    checkARMArchName(D, WaArch->first, Args, ArchName, CPUName,
582
0
                     ExtensionFeatures, Triple, ArchArgFPUKind);
583
    // The base architecture was handled in ToolChain::ComputeLLVMTriple because
584
    // triple is read only by this point.
585
0
  } else if (ArchArg) {
586
0
    ArchName = ArchArg->getValue();
587
0
    checkARMArchName(D, ArchArg, Args, ArchName, CPUName, ExtensionFeatures,
588
0
                     Triple, ArchArgFPUKind);
589
0
  }
590
591
  // Add CPU features for generic CPUs
592
0
  if (CPUName == "native") {
593
0
    llvm::StringMap<bool> HostFeatures;
594
0
    if (llvm::sys::getHostCPUFeatures(HostFeatures))
595
0
      for (auto &F : HostFeatures)
596
0
        Features.push_back(
597
0
            Args.MakeArgString((F.second ? "+" : "-") + F.first()));
598
0
  } else if (!CPUName.empty()) {
599
    // This sets the default features for the specified CPU. We certainly don't
600
    // want to override the features that have been explicitly specified on the
601
    // command line. Therefore, process them directly instead of appending them
602
    // at the end later.
603
0
    DecodeARMFeaturesFromCPU(D, CPUName, Features);
604
0
  }
605
606
0
  if (CPUArg)
607
0
    checkARMCPUName(D, CPUArg, Args, CPUName, ArchName, ExtensionFeatures,
608
0
                    Triple, CPUArgFPUKind);
609
610
  // TODO Handle -mtune=. Suppress -Wunused-command-line-argument as a
611
  // longstanding behavior.
612
0
  (void)Args.getLastArg(options::OPT_mtune_EQ);
613
614
  // Honor -mfpu=. ClangAs gives preference to -Wa,-mfpu=.
615
0
  llvm::ARM::FPUKind FPUKind = llvm::ARM::FK_INVALID;
616
0
  const Arg *FPUArg = Args.getLastArg(options::OPT_mfpu_EQ);
617
0
  if (WaFPU) {
618
0
    if (FPUArg)
619
0
      D.Diag(clang::diag::warn_drv_unused_argument)
620
0
          << FPUArg->getAsString(Args);
621
0
    (void)getARMFPUFeatures(D, WaFPU->first, Args, WaFPU->second, Features);
622
0
  } else if (FPUArg) {
623
0
    FPUKind = getARMFPUFeatures(D, FPUArg, Args, FPUArg->getValue(), Features);
624
0
  } else if (Triple.isAndroid() && getARMSubArchVersionNumber(Triple) >= 7) {
625
0
    const char *AndroidFPU = "neon";
626
0
    FPUKind = llvm::ARM::parseFPU(AndroidFPU);
627
0
    if (!llvm::ARM::getFPUFeatures(FPUKind, Features))
628
0
      D.Diag(clang::diag::err_drv_clang_unsupported)
629
0
          << std::string("-mfpu=") + AndroidFPU;
630
0
  } else if (ArchArgFPUKind != llvm::ARM::FK_INVALID ||
631
0
             CPUArgFPUKind != llvm::ARM::FK_INVALID) {
632
0
    FPUKind =
633
0
        CPUArgFPUKind != llvm::ARM::FK_INVALID ? CPUArgFPUKind : ArchArgFPUKind;
634
0
    (void)llvm::ARM::getFPUFeatures(FPUKind, Features);
635
0
  } else {
636
0
    if (!ForAS) {
637
0
      std::string CPU = arm::getARMTargetCPU(CPUName, ArchName, Triple);
638
0
      llvm::ARM::ArchKind ArchKind =
639
0
          arm::getLLVMArchKindForARM(CPU, ArchName, Triple);
640
0
      FPUKind = llvm::ARM::getDefaultFPU(CPU, ArchKind);
641
0
      (void)llvm::ARM::getFPUFeatures(FPUKind, Features);
642
0
    }
643
0
  }
644
645
  // Now we've finished accumulating features from arch, cpu and fpu,
646
  // we can append the ones for architecture extensions that we
647
  // collected separately.
648
0
  Features.insert(std::end(Features),
649
0
                  std::begin(ExtensionFeatures), std::end(ExtensionFeatures));
650
651
  // Honor -mhwdiv=. ClangAs gives preference to -Wa,-mhwdiv=.
652
0
  const Arg *HDivArg = Args.getLastArg(options::OPT_mhwdiv_EQ);
653
0
  if (WaHDiv) {
654
0
    if (HDivArg)
655
0
      D.Diag(clang::diag::warn_drv_unused_argument)
656
0
          << HDivArg->getAsString(Args);
657
0
    getARMHWDivFeatures(D, WaHDiv->first, Args, WaHDiv->second, Features);
658
0
  } else if (HDivArg)
659
0
    getARMHWDivFeatures(D, HDivArg, Args, HDivArg->getValue(), Features);
660
661
  // Handle (arch-dependent) fp16fml/fullfp16 relationship.
662
  // Must happen before any features are disabled due to soft-float.
663
  // FIXME: this fp16fml option handling will be reimplemented after the
664
  // TargetParser rewrite.
665
0
  const auto ItRNoFullFP16 = std::find(Features.rbegin(), Features.rend(), "-fullfp16");
666
0
  const auto ItRFP16FML = std::find(Features.rbegin(), Features.rend(), "+fp16fml");
667
0
  if (Triple.getSubArch() == llvm::Triple::SubArchType::ARMSubArch_v8_4a) {
668
0
    const auto ItRFullFP16  = std::find(Features.rbegin(), Features.rend(), "+fullfp16");
669
0
    if (ItRFullFP16 < ItRNoFullFP16 && ItRFullFP16 < ItRFP16FML) {
670
      // Only entangled feature that can be to the right of this +fullfp16 is -fp16fml.
671
      // Only append the +fp16fml if there is no -fp16fml after the +fullfp16.
672
0
      if (std::find(Features.rbegin(), ItRFullFP16, "-fp16fml") == ItRFullFP16)
673
0
        Features.push_back("+fp16fml");
674
0
    }
675
0
    else
676
0
      goto fp16_fml_fallthrough;
677
0
  }
678
0
  else {
679
0
fp16_fml_fallthrough:
680
    // In both of these cases, putting the 'other' feature on the end of the vector will
681
    // result in the same effect as placing it immediately after the current feature.
682
0
    if (ItRNoFullFP16 < ItRFP16FML)
683
0
      Features.push_back("-fp16fml");
684
0
    else if (ItRNoFullFP16 > ItRFP16FML)
685
0
      Features.push_back("+fullfp16");
686
0
  }
687
688
  // Setting -msoft-float/-mfloat-abi=soft, -mfpu=none, or adding +nofp to
689
  // -march/-mcpu effectively disables the FPU (GCC ignores the -mfpu options in
690
  // this case). Note that the ABI can also be set implicitly by the target
691
  // selected.
692
0
  bool HasFPRegs = true;
693
0
  if (ABI == arm::FloatABI::Soft) {
694
0
    llvm::ARM::getFPUFeatures(llvm::ARM::FK_NONE, Features);
695
696
    // Disable all features relating to hardware FP, not already disabled by the
697
    // above call.
698
0
    Features.insert(Features.end(),
699
0
                    {"-dotprod", "-fp16fml", "-bf16", "-mve", "-mve.fp"});
700
0
    HasFPRegs = false;
701
0
    FPUKind = llvm::ARM::FK_NONE;
702
0
  } else if (FPUKind == llvm::ARM::FK_NONE ||
703
0
             ArchArgFPUKind == llvm::ARM::FK_NONE ||
704
0
             CPUArgFPUKind == llvm::ARM::FK_NONE) {
705
    // -mfpu=none, -march=armvX+nofp or -mcpu=X+nofp is *very* similar to
706
    // -mfloat-abi=soft, only that it should not disable MVE-I. They disable the
707
    // FPU, but not the FPU registers, thus MVE-I, which depends only on the
708
    // latter, is still supported.
709
0
    Features.insert(Features.end(),
710
0
                    {"-dotprod", "-fp16fml", "-bf16", "-mve.fp"});
711
0
    HasFPRegs = hasIntegerMVE(Features);
712
0
    FPUKind = llvm::ARM::FK_NONE;
713
0
  }
714
0
  if (!HasFPRegs)
715
0
    Features.emplace_back("-fpregs");
716
717
  // En/disable crc code generation.
718
0
  if (Arg *A = Args.getLastArg(options::OPT_mcrc, options::OPT_mnocrc)) {
719
0
    if (A->getOption().matches(options::OPT_mcrc))
720
0
      Features.push_back("+crc");
721
0
    else
722
0
      Features.push_back("-crc");
723
0
  }
724
725
  // For Arch >= ARMv8.0 && A or R profile:  crypto = sha2 + aes
726
  // Rather than replace within the feature vector, determine whether each
727
  // algorithm is enabled and append this to the end of the vector.
728
  // The algorithms can be controlled by their specific feature or the crypto
729
  // feature, so their status can be determined by the last occurance of
730
  // either in the vector. This allows one to supercede the other.
731
  // e.g. +crypto+noaes in -march/-mcpu should enable sha2, but not aes
732
  // FIXME: this needs reimplementation after the TargetParser rewrite
733
0
  bool HasSHA2 = false;
734
0
  bool HasAES = false;
735
0
  const auto ItCrypto =
736
0
      llvm::find_if(llvm::reverse(Features), [](const StringRef F) {
737
0
        return F.contains("crypto");
738
0
      });
739
0
  const auto ItSHA2 =
740
0
      llvm::find_if(llvm::reverse(Features), [](const StringRef F) {
741
0
        return F.contains("crypto") || F.contains("sha2");
742
0
      });
743
0
  const auto ItAES =
744
0
      llvm::find_if(llvm::reverse(Features), [](const StringRef F) {
745
0
        return F.contains("crypto") || F.contains("aes");
746
0
      });
747
0
  const bool FoundSHA2 = ItSHA2 != Features.rend();
748
0
  const bool FoundAES = ItAES != Features.rend();
749
0
  if (FoundSHA2)
750
0
    HasSHA2 = ItSHA2->take_front() == "+";
751
0
  if (FoundAES)
752
0
    HasAES = ItAES->take_front() == "+";
753
0
  if (ItCrypto != Features.rend()) {
754
0
    if (HasSHA2 && HasAES)
755
0
      Features.push_back("+crypto");
756
0
    else
757
0
      Features.push_back("-crypto");
758
0
    if (HasSHA2)
759
0
      Features.push_back("+sha2");
760
0
    else
761
0
      Features.push_back("-sha2");
762
0
    if (HasAES)
763
0
      Features.push_back("+aes");
764
0
    else
765
0
      Features.push_back("-aes");
766
0
  }
767
768
0
  if (HasSHA2 || HasAES) {
769
0
    StringRef ArchSuffix = arm::getLLVMArchSuffixForARM(
770
0
        arm::getARMTargetCPU(CPUName, ArchName, Triple), ArchName, Triple);
771
0
    llvm::ARM::ProfileKind ArchProfile =
772
0
        llvm::ARM::parseArchProfile(ArchSuffix);
773
0
    if (!((llvm::ARM::parseArchVersion(ArchSuffix) >= 8) &&
774
0
          (ArchProfile == llvm::ARM::ProfileKind::A ||
775
0
           ArchProfile == llvm::ARM::ProfileKind::R))) {
776
0
      if (HasSHA2)
777
0
        D.Diag(clang::diag::warn_target_unsupported_extension)
778
0
            << "sha2"
779
0
            << llvm::ARM::getArchName(llvm::ARM::parseArch(ArchSuffix));
780
0
      if (HasAES)
781
0
        D.Diag(clang::diag::warn_target_unsupported_extension)
782
0
            << "aes"
783
0
            << llvm::ARM::getArchName(llvm::ARM::parseArch(ArchSuffix));
784
      // With -fno-integrated-as -mfpu=crypto-neon-fp-armv8 some assemblers such
785
      // as the GNU assembler will permit the use of crypto instructions as the
786
      // fpu will override the architecture. We keep the crypto feature in this
787
      // case to preserve compatibility. In all other cases we remove the crypto
788
      // feature.
789
0
      if (!Args.hasArg(options::OPT_fno_integrated_as)) {
790
0
        Features.push_back("-sha2");
791
0
        Features.push_back("-aes");
792
0
      }
793
0
    }
794
0
  }
795
796
  // Propagate frame-chain model selection
797
0
  if (Arg *A = Args.getLastArg(options::OPT_mframe_chain)) {
798
0
    StringRef FrameChainOption = A->getValue();
799
0
    if (FrameChainOption.starts_with("aapcs"))
800
0
      Features.push_back("+aapcs-frame-chain");
801
0
    if (FrameChainOption == "aapcs+leaf")
802
0
      Features.push_back("+aapcs-frame-chain-leaf");
803
0
  }
804
805
  // CMSE: Check for target 8M (for -mcmse to be applicable) is performed later.
806
0
  if (Args.getLastArg(options::OPT_mcmse))
807
0
    Features.push_back("+8msecext");
808
809
0
  if (Arg *A = Args.getLastArg(options::OPT_mfix_cmse_cve_2021_35465,
810
0
                               options::OPT_mno_fix_cmse_cve_2021_35465)) {
811
0
    if (!Args.getLastArg(options::OPT_mcmse))
812
0
      D.Diag(diag::err_opt_not_valid_without_opt)
813
0
          << A->getOption().getName() << "-mcmse";
814
815
0
    if (A->getOption().matches(options::OPT_mfix_cmse_cve_2021_35465))
816
0
      Features.push_back("+fix-cmse-cve-2021-35465");
817
0
    else
818
0
      Features.push_back("-fix-cmse-cve-2021-35465");
819
0
  }
820
821
  // This also handles the -m(no-)fix-cortex-a72-1655431 arguments via aliases.
822
0
  if (Arg *A = Args.getLastArg(options::OPT_mfix_cortex_a57_aes_1742098,
823
0
                               options::OPT_mno_fix_cortex_a57_aes_1742098)) {
824
0
    if (A->getOption().matches(options::OPT_mfix_cortex_a57_aes_1742098)) {
825
0
      Features.push_back("+fix-cortex-a57-aes-1742098");
826
0
    } else {
827
0
      Features.push_back("-fix-cortex-a57-aes-1742098");
828
0
    }
829
0
  }
830
831
  // Look for the last occurrence of -mlong-calls or -mno-long-calls. If
832
  // neither options are specified, see if we are compiling for kernel/kext and
833
  // decide whether to pass "+long-calls" based on the OS and its version.
834
0
  if (Arg *A = Args.getLastArg(options::OPT_mlong_calls,
835
0
                               options::OPT_mno_long_calls)) {
836
0
    if (A->getOption().matches(options::OPT_mlong_calls))
837
0
      Features.push_back("+long-calls");
838
0
  } else if (KernelOrKext && (!Triple.isiOS() || Triple.isOSVersionLT(6)) &&
839
0
             !Triple.isWatchOS()) {
840
0
      Features.push_back("+long-calls");
841
0
  }
842
843
  // Generate execute-only output (no data access to code sections).
844
  // This only makes sense for the compiler, not for the assembler.
845
  // It's not needed for multilib selection and may hide an unused
846
  // argument diagnostic if the code is always run.
847
0
  if (!ForAS && !ForMultilib) {
848
    // Supported only on ARMv6T2 and ARMv7 and above.
849
    // Cannot be combined with -mno-movt.
850
0
    if (Arg *A = Args.getLastArg(options::OPT_mexecute_only, options::OPT_mno_execute_only)) {
851
0
      if (A->getOption().matches(options::OPT_mexecute_only)) {
852
0
        if (getARMSubArchVersionNumber(Triple) < 7 &&
853
0
            llvm::ARM::parseArch(Triple.getArchName()) != llvm::ARM::ArchKind::ARMV6T2 &&
854
0
            llvm::ARM::parseArch(Triple.getArchName()) != llvm::ARM::ArchKind::ARMV6M)
855
0
              D.Diag(diag::err_target_unsupported_execute_only) << Triple.getArchName();
856
0
        else if (llvm::ARM::parseArch(Triple.getArchName()) == llvm::ARM::ArchKind::ARMV6M) {
857
0
          if (Arg *PIArg = Args.getLastArg(options::OPT_fropi, options::OPT_frwpi,
858
0
                                           options::OPT_fpic, options::OPT_fpie,
859
0
                                           options::OPT_fPIC, options::OPT_fPIE))
860
0
            D.Diag(diag::err_opt_not_valid_with_opt_on_target)
861
0
                << A->getAsString(Args) << PIArg->getAsString(Args) << Triple.getArchName();
862
0
        } else if (Arg *B = Args.getLastArg(options::OPT_mno_movt))
863
0
          D.Diag(diag::err_opt_not_valid_with_opt)
864
0
              << A->getAsString(Args) << B->getAsString(Args);
865
0
        Features.push_back("+execute-only");
866
0
      }
867
0
    }
868
0
  }
869
870
  // Kernel code has more strict alignment requirements.
871
0
  if (KernelOrKext) {
872
0
    Features.push_back("+strict-align");
873
0
  } else if (Arg *A = Args.getLastArg(options::OPT_mno_unaligned_access,
874
0
                                      options::OPT_munaligned_access)) {
875
0
    if (A->getOption().matches(options::OPT_munaligned_access)) {
876
      // No v6M core supports unaligned memory access (v6M ARM ARM A3.2).
877
0
      if (Triple.getSubArch() == llvm::Triple::SubArchType::ARMSubArch_v6m)
878
0
        D.Diag(diag::err_target_unsupported_unaligned) << "v6m";
879
      // v8M Baseline follows on from v6M, so doesn't support unaligned memory
880
      // access either.
881
0
      else if (Triple.getSubArch() == llvm::Triple::SubArchType::ARMSubArch_v8m_baseline)
882
0
        D.Diag(diag::err_target_unsupported_unaligned) << "v8m.base";
883
0
    } else
884
0
      Features.push_back("+strict-align");
885
0
  } else {
886
    // Assume pre-ARMv6 doesn't support unaligned accesses.
887
    //
888
    // ARMv6 may or may not support unaligned accesses depending on the
889
    // SCTLR.U bit, which is architecture-specific. We assume ARMv6
890
    // Darwin and NetBSD targets support unaligned accesses, and others don't.
891
    //
892
    // ARMv7 always has SCTLR.U set to 1, but it has a new SCTLR.A bit
893
    // which raises an alignment fault on unaligned accesses. Linux
894
    // defaults this bit to 0 and handles it as a system-wide (not
895
    // per-process) setting. It is therefore safe to assume that ARMv7+
896
    // Linux targets support unaligned accesses. The same goes for NaCl
897
    // and Windows.
898
    //
899
    // The above behavior is consistent with GCC.
900
0
    int VersionNum = getARMSubArchVersionNumber(Triple);
901
0
    if (Triple.isOSDarwin() || Triple.isOSNetBSD()) {
902
0
      if (VersionNum < 6 ||
903
0
          Triple.getSubArch() == llvm::Triple::SubArchType::ARMSubArch_v6m)
904
0
        Features.push_back("+strict-align");
905
0
    } else if (Triple.isOSLinux() || Triple.isOSNaCl() ||
906
0
               Triple.isOSWindows()) {
907
0
      if (VersionNum < 7)
908
0
        Features.push_back("+strict-align");
909
0
    } else
910
0
      Features.push_back("+strict-align");
911
0
  }
912
913
  // llvm does not support reserving registers in general. There is support
914
  // for reserving r9 on ARM though (defined as a platform-specific register
915
  // in ARM EABI).
916
0
  if (Args.hasArg(options::OPT_ffixed_r9))
917
0
    Features.push_back("+reserve-r9");
918
919
  // The kext linker doesn't know how to deal with movw/movt.
920
0
  if (KernelOrKext || Args.hasArg(options::OPT_mno_movt))
921
0
    Features.push_back("+no-movt");
922
923
0
  if (Args.hasArg(options::OPT_mno_neg_immediates))
924
0
    Features.push_back("+no-neg-immediates");
925
926
  // Enable/disable straight line speculation hardening.
927
0
  if (Arg *A = Args.getLastArg(options::OPT_mharden_sls_EQ)) {
928
0
    StringRef Scope = A->getValue();
929
0
    bool EnableRetBr = false;
930
0
    bool EnableBlr = false;
931
0
    bool DisableComdat = false;
932
0
    if (Scope != "none") {
933
0
      SmallVector<StringRef, 4> Opts;
934
0
      Scope.split(Opts, ",");
935
0
      for (auto Opt : Opts) {
936
0
        Opt = Opt.trim();
937
0
        if (Opt == "all") {
938
0
          EnableBlr = true;
939
0
          EnableRetBr = true;
940
0
          continue;
941
0
        }
942
0
        if (Opt == "retbr") {
943
0
          EnableRetBr = true;
944
0
          continue;
945
0
        }
946
0
        if (Opt == "blr") {
947
0
          EnableBlr = true;
948
0
          continue;
949
0
        }
950
0
        if (Opt == "comdat") {
951
0
          DisableComdat = false;
952
0
          continue;
953
0
        }
954
0
        if (Opt == "nocomdat") {
955
0
          DisableComdat = true;
956
0
          continue;
957
0
        }
958
0
        D.Diag(diag::err_drv_unsupported_option_argument)
959
0
            << A->getSpelling() << Scope;
960
0
        break;
961
0
      }
962
0
    }
963
964
0
    if (EnableRetBr || EnableBlr)
965
0
      if (!(isARMAProfile(Triple) && getARMSubArchVersionNumber(Triple) >= 7))
966
0
        D.Diag(diag::err_sls_hardening_arm_not_supported)
967
0
            << Scope << A->getAsString(Args);
968
969
0
    if (EnableRetBr)
970
0
      Features.push_back("+harden-sls-retbr");
971
0
    if (EnableBlr)
972
0
      Features.push_back("+harden-sls-blr");
973
0
    if (DisableComdat) {
974
0
      Features.push_back("+harden-sls-nocomdat");
975
0
    }
976
0
  }
977
978
0
  if (Args.getLastArg(options::OPT_mno_bti_at_return_twice))
979
0
    Features.push_back("+no-bti-at-return-twice");
980
981
0
  checkARMFloatABI(D, Args, HasFPRegs);
982
983
0
  return FPUKind;
984
0
}
985
986
0
std::string arm::getARMArch(StringRef Arch, const llvm::Triple &Triple) {
987
0
  std::string MArch;
988
0
  if (!Arch.empty())
989
0
    MArch = std::string(Arch);
990
0
  else
991
0
    MArch = std::string(Triple.getArchName());
992
0
  MArch = StringRef(MArch).split("+").first.lower();
993
994
  // Handle -march=native.
995
0
  if (MArch == "native") {
996
0
    std::string CPU = std::string(llvm::sys::getHostCPUName());
997
0
    if (CPU != "generic") {
998
      // Translate the native cpu into the architecture suffix for that CPU.
999
0
      StringRef Suffix = arm::getLLVMArchSuffixForARM(CPU, MArch, Triple);
1000
      // If there is no valid architecture suffix for this CPU we don't know how
1001
      // to handle it, so return no architecture.
1002
0
      if (Suffix.empty())
1003
0
        MArch = "";
1004
0
      else
1005
0
        MArch = std::string("arm") + Suffix.str();
1006
0
    }
1007
0
  }
1008
1009
0
  return MArch;
1010
0
}
1011
1012
/// Get the (LLVM) name of the minimum ARM CPU for the arch we are targeting.
1013
0
StringRef arm::getARMCPUForMArch(StringRef Arch, const llvm::Triple &Triple) {
1014
0
  std::string MArch = getARMArch(Arch, Triple);
1015
  // getARMCPUForArch defaults to the triple if MArch is empty, but empty MArch
1016
  // here means an -march=native that we can't handle, so instead return no CPU.
1017
0
  if (MArch.empty())
1018
0
    return StringRef();
1019
1020
  // We need to return an empty string here on invalid MArch values as the
1021
  // various places that call this function can't cope with a null result.
1022
0
  return llvm::ARM::getARMCPUForArch(Triple, MArch);
1023
0
}
1024
1025
/// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting.
1026
std::string arm::getARMTargetCPU(StringRef CPU, StringRef Arch,
1027
0
                                 const llvm::Triple &Triple) {
1028
  // FIXME: Warn on inconsistent use of -mcpu and -march.
1029
  // If we have -mcpu=, use that.
1030
0
  if (!CPU.empty()) {
1031
0
    std::string MCPU = StringRef(CPU).split("+").first.lower();
1032
    // Handle -mcpu=native.
1033
0
    if (MCPU == "native")
1034
0
      return std::string(llvm::sys::getHostCPUName());
1035
0
    else
1036
0
      return MCPU;
1037
0
  }
1038
1039
0
  return std::string(getARMCPUForMArch(Arch, Triple));
1040
0
}
1041
1042
/// getLLVMArchSuffixForARM - Get the LLVM ArchKind value to use for a
1043
/// particular CPU (or Arch, if CPU is generic). This is needed to
1044
/// pass to functions like llvm::ARM::getDefaultFPU which need an
1045
/// ArchKind as well as a CPU name.
1046
llvm::ARM::ArchKind arm::getLLVMArchKindForARM(StringRef CPU, StringRef Arch,
1047
0
                                               const llvm::Triple &Triple) {
1048
0
  llvm::ARM::ArchKind ArchKind;
1049
0
  if (CPU == "generic" || CPU.empty()) {
1050
0
    std::string ARMArch = tools::arm::getARMArch(Arch, Triple);
1051
0
    ArchKind = llvm::ARM::parseArch(ARMArch);
1052
0
    if (ArchKind == llvm::ARM::ArchKind::INVALID)
1053
      // In case of generic Arch, i.e. "arm",
1054
      // extract arch from default cpu of the Triple
1055
0
      ArchKind =
1056
0
          llvm::ARM::parseCPUArch(llvm::ARM::getARMCPUForArch(Triple, ARMArch));
1057
0
  } else {
1058
    // FIXME: horrible hack to get around the fact that Cortex-A7 is only an
1059
    // armv7k triple if it's actually been specified via "-arch armv7k".
1060
0
    ArchKind = (Arch == "armv7k" || Arch == "thumbv7k")
1061
0
                          ? llvm::ARM::ArchKind::ARMV7K
1062
0
                          : llvm::ARM::parseCPUArch(CPU);
1063
0
  }
1064
0
  return ArchKind;
1065
0
}
1066
1067
/// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular
1068
/// CPU  (or Arch, if CPU is generic).
1069
// FIXME: This is redundant with -mcpu, why does LLVM use this.
1070
StringRef arm::getLLVMArchSuffixForARM(StringRef CPU, StringRef Arch,
1071
0
                                       const llvm::Triple &Triple) {
1072
0
  llvm::ARM::ArchKind ArchKind = getLLVMArchKindForARM(CPU, Arch, Triple);
1073
0
  if (ArchKind == llvm::ARM::ArchKind::INVALID)
1074
0
    return "";
1075
0
  return llvm::ARM::getSubArch(ArchKind);
1076
0
}
1077
1078
void arm::appendBE8LinkFlag(const ArgList &Args, ArgStringList &CmdArgs,
1079
0
                            const llvm::Triple &Triple) {
1080
0
  if (Args.hasArg(options::OPT_r))
1081
0
    return;
1082
1083
  // ARMv7 (and later) and ARMv6-M do not support BE-32, so instruct the linker
1084
  // to generate BE-8 executables.
1085
0
  if (arm::getARMSubArchVersionNumber(Triple) >= 7 || arm::isARMMProfile(Triple))
1086
0
    CmdArgs.push_back("--be8");
1087
0
}