Coverage Report

Created: 2020-02-18 08:44

/Users/buildslave/jenkins/workspace/coverage/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/Support/TargetParser.h"
16
#include "llvm/Support/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
5.33k
int arm::getARMSubArchVersionNumber(const llvm::Triple &Triple) {
25
5.33k
  llvm::StringRef Arch = Triple.getArchName();
26
5.33k
  return llvm::ARM::parseArchVersion(Arch);
27
5.33k
}
28
29
// True if M-profile.
30
20
bool arm::isARMMProfile(const llvm::Triple &Triple) {
31
20
  llvm::StringRef Arch = Triple.getArchName();
32
20
  return llvm::ARM::parseArchProfile(Arch) == llvm::ARM::ProfileKind::M;
33
20
}
34
35
// Get Arch/CPU from args.
36
void arm::getARMArchCPUFromArgs(const ArgList &Args, llvm::StringRef &Arch,
37
3.33k
                                llvm::StringRef &CPU, bool FromAs) {
38
3.33k
  if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_mcpu_EQ))
39
1.00k
    CPU = A->getValue();
40
3.33k
  if (const Arg *A = Args.getLastArg(options::OPT_march_EQ))
41
938
    Arch = A->getValue();
42
3.33k
  if (!FromAs)
43
3.27k
    return;
44
61
45
61
  for (const Arg *A :
46
61
       Args.filtered(options::OPT_Wa_COMMA, options::OPT_Xassembler)) {
47
14
    StringRef Value = A->getValue();
48
14
    if (Value.startswith("-mcpu="))
49
3
      CPU = Value.substr(6);
50
14
    if (Value.startswith("-march="))
51
2
      Arch = Value.substr(7);
52
14
  }
53
61
}
54
55
// Handle -mhwdiv=.
56
// FIXME: Use ARMTargetParser.
57
static void getARMHWDivFeatures(const Driver &D, const Arg *A,
58
                                const ArgList &Args, StringRef HWDiv,
59
25
                                std::vector<StringRef> &Features) {
60
25
  uint64_t HWDivID = llvm::ARM::parseHWDiv(HWDiv);
61
25
  if (!llvm::ARM::getHWDivFeatures(HWDivID, Features))
62
1
    D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args);
63
25
}
64
65
// Handle -mfpu=.
66
static unsigned getARMFPUFeatures(const Driver &D, const Arg *A,
67
                                  const ArgList &Args, StringRef FPU,
68
143
                                  std::vector<StringRef> &Features) {
69
143
  unsigned FPUID = llvm::ARM::parseFPU(FPU);
70
143
  if (!llvm::ARM::getFPUFeatures(FPUID, Features))
71
6
    D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args);
72
143
  return FPUID;
73
143
}
74
75
// Decode ARM features from string like +[no]featureA+[no]featureB+...
76
static bool DecodeARMFeatures(const Driver &D, StringRef text,
77
                              StringRef CPU, llvm::ARM::ArchKind ArchKind,
78
157
                              std::vector<StringRef> &Features) {
79
157
  SmallVector<StringRef, 8> Split;
80
157
  text.split(Split, StringRef("+"), -1, false);
81
157
82
216
  for (StringRef Feature : Split) {
83
216
    if (!appendArchExtFeatures(CPU, ArchKind, Feature, Features))
84
1
      return false;
85
216
  }
86
157
  
return true156
;
87
157
}
88
89
static void DecodeARMFeaturesFromCPU(const Driver &D, StringRef CPU,
90
503
                                     std::vector<StringRef> &Features) {
91
503
  CPU = CPU.split("+").first;
92
503
  if (CPU != "generic") {
93
468
    llvm::ARM::ArchKind ArchKind = llvm::ARM::parseCPUArch(CPU);
94
468
    uint64_t Extension = llvm::ARM::getDefaultExtensions(CPU, ArchKind);
95
468
    llvm::ARM::getExtensionFeatures(Extension, Features);
96
468
  }
97
503
}
98
99
// Check if -march is valid by checking if it can be canonicalised and parsed.
100
// getARMArch is used here instead of just checking the -march value in order
101
// to handle -march=native correctly.
102
static void checkARMArchName(const Driver &D, const Arg *A, const ArgList &Args,
103
                             llvm::StringRef ArchName, llvm::StringRef CPUName,
104
                             std::vector<StringRef> &Features,
105
479
                             const llvm::Triple &Triple) {
106
479
  std::pair<StringRef, StringRef> Split = ArchName.split("+");
107
479
108
479
  std::string MArch = arm::getARMArch(ArchName, Triple);
109
479
  llvm::ARM::ArchKind ArchKind = llvm::ARM::parseArch(MArch);
110
479
  if (ArchKind == llvm::ARM::ArchKind::INVALID ||
111
479
      
(474
Split.second.size()474
&& !DecodeARMFeatures(
112
138
        D, Split.second, CPUName, ArchKind, Features)))
113
5
    D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args);
114
479
}
115
116
// Check -mcpu=. Needs ArchName to handle -mcpu=generic.
117
static void checkARMCPUName(const Driver &D, const Arg *A, const ArgList &Args,
118
                            llvm::StringRef CPUName, llvm::StringRef ArchName,
119
                            std::vector<StringRef> &Features,
120
503
                            const llvm::Triple &Triple) {
121
503
  std::pair<StringRef, StringRef> Split = CPUName.split("+");
122
503
123
503
  std::string CPU = arm::getARMTargetCPU(CPUName, ArchName, Triple);
124
503
  llvm::ARM::ArchKind ArchKind =
125
503
    arm::getLLVMArchKindForARM(CPU, ArchName, Triple);
126
503
  if (ArchKind == llvm::ARM::ArchKind::INVALID ||
127
503
      
(501
Split.second.size()501
&& !DecodeARMFeatures(
128
19
        D, Split.second, CPU, ArchKind, Features)))
129
3
    D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args);
130
503
}
131
132
25
bool arm::useAAPCSForMachO(const llvm::Triple &T) {
133
25
  // The backend is hardwired to assume AAPCS for M-class processors, ensure
134
25
  // the frontend matches that.
135
25
  return T.getEnvironment() == llvm::Triple::EABI ||
136
25
         
T.getOS() == llvm::Triple::UnknownOS23
||
isARMMProfile(T)8
;
137
25
}
138
139
// Select mode for reading thread pointer (-mtp=soft/cp15).
140
1.69k
arm::ReadTPMode arm::getReadTPMode(const Driver &D, const ArgList &Args) {
141
1.69k
  if (Arg *A = Args.getLastArg(options::OPT_mtp_mode_EQ)) {
142
2
    arm::ReadTPMode ThreadPointer =
143
2
        llvm::StringSwitch<arm::ReadTPMode>(A->getValue())
144
2
            .Case("cp15", ReadTPMode::Cp15)
145
2
            .Case("soft", ReadTPMode::Soft)
146
2
            .Default(ReadTPMode::Invalid);
147
2
    if (ThreadPointer != ReadTPMode::Invalid)
148
2
      return ThreadPointer;
149
0
    if (StringRef(A->getValue()).empty())
150
0
      D.Diag(diag::err_drv_missing_arg_mtp) << A->getAsString(Args);
151
0
    else
152
0
      D.Diag(diag::err_drv_invalid_mtp) << A->getAsString(Args);
153
0
    return ReadTPMode::Invalid;
154
0
  }
155
1.68k
  return ReadTPMode::Soft;
156
1.68k
}
157
158
1.85k
arm::FloatABI arm::getARMFloatABI(const ToolChain &TC, const ArgList &Args) {
159
1.85k
  return arm::getARMFloatABI(TC.getDriver(), TC.getEffectiveTriple(), Args);
160
1.85k
}
161
162
// Select the float ABI as determined by -msoft-float, -mhard-float, and
163
// -mfloat-abi=.
164
arm::FloatABI arm::getARMFloatABI(const Driver &D, const llvm::Triple &Triple,
165
3.54k
                                  const ArgList &Args) {
166
3.54k
  auto SubArch = getARMSubArchVersionNumber(Triple);
167
3.54k
  arm::FloatABI ABI = FloatABI::Invalid;
168
3.54k
  if (Arg *A =
169
155
          Args.getLastArg(options::OPT_msoft_float, options::OPT_mhard_float,
170
155
                          options::OPT_mfloat_abi_EQ)) {
171
155
    if (A->getOption().matches(options::OPT_msoft_float)) {
172
12
      ABI = FloatABI::Soft;
173
143
    } else if (A->getOption().matches(options::OPT_mhard_float)) {
174
20
      ABI = FloatABI::Hard;
175
123
    } else {
176
123
      ABI = llvm::StringSwitch<arm::FloatABI>(A->getValue())
177
123
                .Case("soft", FloatABI::Soft)
178
123
                .Case("softfp", FloatABI::SoftFP)
179
123
                .Case("hard", FloatABI::Hard)
180
123
                .Default(FloatABI::Invalid);
181
123
      if (ABI == FloatABI::Invalid && 
!StringRef(A->getValue()).empty()0
) {
182
0
        D.Diag(diag::err_drv_invalid_mfloat_abi) << A->getAsString(Args);
183
0
        ABI = FloatABI::Soft;
184
0
      }
185
123
    }
186
155
187
155
    // It is incorrect to select hard float ABI on MachO platforms if the ABI is
188
155
    // "apcs-gnu".
189
155
    if (Triple.isOSBinFormatMachO() && 
!useAAPCSForMachO(Triple)25
&&
190
155
        
ABI == FloatABI::Hard8
) {
191
4
      D.Diag(diag::err_drv_unsupported_opt_for_target) << A->getAsString(Args)
192
4
                                                       << Triple.getArchName();
193
4
    }
194
155
  }
195
3.54k
196
3.54k
  // If unspecified, choose the default based on the platform.
197
3.54k
  if (ABI == FloatABI::Invalid) {
198
3.38k
    switch (Triple.getOS()) {
199
164
    case llvm::Triple::Darwin:
200
164
    case llvm::Triple::MacOSX:
201
164
    case llvm::Triple::IOS:
202
164
    case llvm::Triple::TvOS: {
203
164
      // Darwin defaults to "softfp" for v6 and v7.
204
164
      ABI = (SubArch == 6 || 
SubArch == 7150
) ?
FloatABI::SoftFP152
:
FloatABI::Soft12
;
205
164
      ABI = Triple.isWatchABI() ? 
FloatABI::Hard8
:
ABI156
;
206
164
      break;
207
164
    }
208
164
    case llvm::Triple::WatchOS:
209
42
      ABI = FloatABI::Hard;
210
42
      break;
211
164
212
164
    // FIXME: this is invalid for WindowsCE
213
164
    case llvm::Triple::Win32:
214
131
      ABI = FloatABI::Hard;
215
131
      break;
216
164
217
164
    case llvm::Triple::NetBSD:
218
78
      switch (Triple.getEnvironment()) {
219
12
      case llvm::Triple::EABIHF:
220
12
      case llvm::Triple::GNUEABIHF:
221
12
        ABI = FloatABI::Hard;
222
12
        break;
223
66
      default:
224
66
        ABI = FloatABI::Soft;
225
66
        break;
226
78
      }
227
78
      break;
228
78
229
78
    case llvm::Triple::FreeBSD:
230
15
      switch (Triple.getEnvironment()) {
231
7
      case llvm::Triple::GNUEABIHF:
232
7
        ABI = FloatABI::Hard;
233
7
        break;
234
8
      default:
235
8
        // FreeBSD defaults to soft float
236
8
        ABI = FloatABI::Soft;
237
8
        break;
238
15
      }
239
15
      break;
240
15
241
30
    case llvm::Triple::OpenBSD:
242
30
      ABI = FloatABI::SoftFP;
243
30
      break;
244
15
245
2.92k
    default:
246
2.92k
      switch (Triple.getEnvironment()) {
247
126
      case llvm::Triple::GNUEABIHF:
248
126
      case llvm::Triple::MuslEABIHF:
249
126
      case llvm::Triple::EABIHF:
250
126
        ABI = FloatABI::Hard;
251
126
        break;
252
1.39k
      case llvm::Triple::GNUEABI:
253
1.39k
      case llvm::Triple::MuslEABI:
254
1.39k
      case llvm::Triple::EABI:
255
1.39k
        // EABI is always AAPCS, and if it was not marked 'hard', it's softfp
256
1.39k
        ABI = FloatABI::SoftFP;
257
1.39k
        break;
258
1.39k
      case llvm::Triple::Android:
259
202
        ABI = (SubArch >= 7) ? 
FloatABI::SoftFP35
:
FloatABI::Soft167
;
260
202
        break;
261
1.39k
      default:
262
1.21k
        // Assume "soft", but warn the user we are guessing.
263
1.21k
        if (Triple.isOSBinFormatMachO() &&
264
1.21k
            
Triple.getSubArch() == llvm::Triple::ARMSubArch_v7em54
)
265
11
          ABI = FloatABI::Hard;
266
1.20k
        else
267
1.20k
          ABI = FloatABI::Soft;
268
1.21k
269
1.21k
        if (Triple.getOS() != llvm::Triple::UnknownOS ||
270
1.21k
            
!Triple.isOSBinFormatMachO()980
)
271
1.15k
          D.Diag(diag::warn_drv_assuming_mfloat_abi_is) << "soft";
272
1.21k
        break;
273
3.54k
      }
274
3.38k
    }
275
3.38k
  }
276
3.54k
277
3.54k
  assert(ABI != FloatABI::Invalid && "must select an ABI");
278
3.54k
  return ABI;
279
3.54k
}
280
281
15
static bool hasIntegerMVE(const std::vector<StringRef> &F) {
282
15
  auto MVE = llvm::find(llvm::reverse(F), "+mve");
283
15
  auto NoMVE = llvm::find(llvm::reverse(F), "-mve");
284
15
  return MVE != F.rend() &&
285
15
         
(4
NoMVE == F.rend()4
||
std::distance(MVE, NoMVE) > 01
);
286
15
}
287
288
void arm::getARMTargetFeatures(const Driver &D, const llvm::Triple &Triple,
289
                               const ArgList &Args, ArgStringList &CmdArgs,
290
1.69k
                               std::vector<StringRef> &Features, bool ForAS) {
291
1.69k
  bool KernelOrKext =
292
1.69k
      Args.hasArg(options::OPT_mkernel, options::OPT_fapple_kext);
293
1.69k
  arm::FloatABI ABI = arm::getARMFloatABI(D, Triple, Args);
294
1.69k
  arm::ReadTPMode ThreadPointer = arm::getReadTPMode(D, Args);
295
1.69k
  const Arg *WaCPU = nullptr, *WaFPU = nullptr;
296
1.69k
  const Arg *WaHDiv = nullptr, *WaArch = nullptr;
297
1.69k
298
1.69k
  // This vector will accumulate features from the architecture
299
1.69k
  // extension suffixes on -mcpu and -march (e.g. the 'bar' in
300
1.69k
  // -mcpu=foo+bar). We want to apply those after the features derived
301
1.69k
  // from the FPU, in case -mfpu generates a negative feature which
302
1.69k
  // the +bar is supposed to override.
303
1.69k
  std::vector<StringRef> ExtensionFeatures;
304
1.69k
305
1.69k
  if (!ForAS) {
306
1.63k
    // FIXME: Note, this is a hack, the LLVM backend doesn't actually use these
307
1.63k
    // yet (it uses the -mfloat-abi and -msoft-float options), and it is
308
1.63k
    // stripped out by the ARM target. We should probably pass this a new
309
1.63k
    // -target-option, which is handled by the -cc1/-cc1as invocation.
310
1.63k
    //
311
1.63k
    // FIXME2:  For consistency, it would be ideal if we set up the target
312
1.63k
    // machine state the same when using the frontend or the assembler. We don't
313
1.63k
    // currently do that for the assembler, we pass the options directly to the
314
1.63k
    // backend and never even instantiate the frontend TargetInfo. If we did,
315
1.63k
    // and used its handleTargetFeatures hook, then we could ensure the
316
1.63k
    // assembler and the frontend behave the same.
317
1.63k
318
1.63k
    // Use software floating point operations?
319
1.63k
    if (ABI == arm::FloatABI::Soft)
320
713
      Features.push_back("+soft-float");
321
1.63k
322
1.63k
    // Use software floating point argument passing?
323
1.63k
    if (ABI != arm::FloatABI::Hard)
324
1.46k
      Features.push_back("+soft-float-abi");
325
1.63k
  } else {
326
52
    // Here, we make sure that -Wa,-mfpu/cpu/arch/hwdiv will be passed down
327
52
    // to the assembler correctly.
328
52
    for (const Arg *A :
329
52
         Args.filtered(options::OPT_Wa_COMMA, options::OPT_Xassembler)) {
330
14
      StringRef Value = A->getValue();
331
14
      if (Value.startswith("-mfpu=")) {
332
3
        WaFPU = A;
333
11
      } else if (Value.startswith("-mcpu=")) {
334
3
        WaCPU = A;
335
8
      } else if (Value.startswith("-mhwdiv=")) {
336
4
        WaHDiv = A;
337
4
      } else if (Value.startswith("-march=")) {
338
2
        WaArch = A;
339
2
      }
340
14
    }
341
52
  }
342
1.69k
343
1.69k
  if (ThreadPointer == arm::ReadTPMode::Cp15)
344
1
    Features.push_back("+read-tp-hard");
345
1.69k
346
1.69k
  const Arg *ArchArg = Args.getLastArg(options::OPT_march_EQ);
347
1.69k
  const Arg *CPUArg = Args.getLastArg(options::OPT_mcpu_EQ);
348
1.69k
  StringRef ArchName;
349
1.69k
  StringRef CPUName;
350
1.69k
351
1.69k
  // Check -mcpu. ClangAs gives preference to -Wa,-mcpu=.
352
1.69k
  if (WaCPU) {
353
3
    if (CPUArg)
354
1
      D.Diag(clang::diag::warn_drv_unused_argument)
355
1
          << CPUArg->getAsString(Args);
356
3
    CPUName = StringRef(WaCPU->getValue()).substr(6);
357
3
    CPUArg = WaCPU;
358
1.68k
  } else if (CPUArg)
359
500
    CPUName = CPUArg->getValue();
360
1.69k
361
1.69k
  // Check -march. ClangAs gives preference to -Wa,-march=.
362
1.69k
  if (WaArch) {
363
2
    if (ArchArg)
364
1
      D.Diag(clang::diag::warn_drv_unused_argument)
365
1
          << ArchArg->getAsString(Args);
366
2
    ArchName = StringRef(WaArch->getValue()).substr(7);
367
2
    checkARMArchName(D, WaArch, Args, ArchName, CPUName,
368
2
                     ExtensionFeatures, Triple);
369
2
    // FIXME: Set Arch.
370
2
    D.Diag(clang::diag::warn_drv_unused_argument) << WaArch->getAsString(Args);
371
1.68k
  } else if (ArchArg) {
372
477
    ArchName = ArchArg->getValue();
373
477
    checkARMArchName(D, ArchArg, Args, ArchName, CPUName,
374
477
                     ExtensionFeatures, Triple);
375
477
  }
376
1.69k
377
1.69k
  // Add CPU features for generic CPUs
378
1.69k
  if (CPUName == "native") {
379
0
    llvm::StringMap<bool> HostFeatures;
380
0
    if (llvm::sys::getHostCPUFeatures(HostFeatures))
381
0
      for (auto &F : HostFeatures)
382
0
        Features.push_back(
383
0
            Args.MakeArgString((F.second ? "+" : "-") + F.first()));
384
1.69k
  } else if (!CPUName.empty()) {
385
503
    // This sets the default features for the specified CPU. We certainly don't
386
503
    // want to override the features that have been explicitly specified on the
387
503
    // command line. Therefore, process them directly instead of appending them
388
503
    // at the end later.
389
503
    DecodeARMFeaturesFromCPU(D, CPUName, Features);
390
503
  }
391
1.69k
392
1.69k
  if (CPUArg)
393
503
    checkARMCPUName(D, CPUArg, Args, CPUName, ArchName,
394
503
                    ExtensionFeatures, Triple);
395
1.69k
  // Honor -mfpu=. ClangAs gives preference to -Wa,-mfpu=.
396
1.69k
  unsigned FPUID = llvm::ARM::FK_INVALID;
397
1.69k
  const Arg *FPUArg = Args.getLastArg(options::OPT_mfpu_EQ);
398
1.69k
  if (WaFPU) {
399
3
    if (FPUArg)
400
1
      D.Diag(clang::diag::warn_drv_unused_argument)
401
1
          << FPUArg->getAsString(Args);
402
3
    (void)getARMFPUFeatures(D, WaFPU, Args, StringRef(WaFPU->getValue()).substr(6),
403
3
                            Features);
404
1.68k
  } else if (FPUArg) {
405
140
    FPUID = getARMFPUFeatures(D, FPUArg, Args, FPUArg->getValue(), Features);
406
1.54k
  } else if (Triple.isAndroid() && 
getARMSubArchVersionNumber(Triple) >= 789
) {
407
16
    const char *AndroidFPU = "neon";
408
16
    FPUID = llvm::ARM::parseFPU(AndroidFPU);
409
16
    if (!llvm::ARM::getFPUFeatures(FPUID, Features))
410
0
      D.Diag(clang::diag::err_drv_clang_unsupported)
411
0
          << std::string("-mfpu=") + AndroidFPU;
412
16
  }
413
1.69k
414
1.69k
  // Now we've finished accumulating features from arch, cpu and fpu,
415
1.69k
  // we can append the ones for architecture extensions that we
416
1.69k
  // collected separately.
417
1.69k
  Features.insert(std::end(Features),
418
1.69k
                  std::begin(ExtensionFeatures), std::end(ExtensionFeatures));
419
1.69k
420
1.69k
  // Honor -mhwdiv=. ClangAs gives preference to -Wa,-mhwdiv=.
421
1.69k
  const Arg *HDivArg = Args.getLastArg(options::OPT_mhwdiv_EQ);
422
1.69k
  if (WaHDiv) {
423
4
    if (HDivArg)
424
1
      D.Diag(clang::diag::warn_drv_unused_argument)
425
1
          << HDivArg->getAsString(Args);
426
4
    getARMHWDivFeatures(D, WaHDiv, Args,
427
4
                        StringRef(WaHDiv->getValue()).substr(8), Features);
428
1.68k
  } else if (HDivArg)
429
21
    getARMHWDivFeatures(D, HDivArg, Args, HDivArg->getValue(), Features);
430
1.69k
431
1.69k
  // Handle (arch-dependent) fp16fml/fullfp16 relationship.
432
1.69k
  // Must happen before any features are disabled due to soft-float.
433
1.69k
  // FIXME: this fp16fml option handling will be reimplemented after the
434
1.69k
  // TargetParser rewrite.
435
1.69k
  const auto ItRNoFullFP16 = std::find(Features.rbegin(), Features.rend(), "-fullfp16");
436
1.69k
  const auto ItRFP16FML = std::find(Features.rbegin(), Features.rend(), "+fp16fml");
437
1.69k
  if (Triple.getSubArch() == llvm::Triple::SubArchType::ARMSubArch_v8_4a) {
438
42
    const auto ItRFullFP16  = std::find(Features.rbegin(), Features.rend(), "+fullfp16");
439
42
    if (ItRFullFP16 < ItRNoFullFP16 && 
ItRFullFP16 < ItRFP16FML13
) {
440
11
      // Only entangled feature that can be to the right of this +fullfp16 is -fp16fml.
441
11
      // Only append the +fp16fml if there is no -fp16fml after the +fullfp16.
442
11
      if (std::find(Features.rbegin(), ItRFullFP16, "-fp16fml") == ItRFullFP16)
443
9
        Features.push_back("+fp16fml");
444
11
    }
445
31
    else
446
31
      goto fp16_fml_fallthrough;
447
1.64k
  }
448
1.64k
  else {
449
1.67k
fp16_fml_fallthrough:
450
1.67k
    // In both of these cases, putting the 'other' feature on the end of the vector will
451
1.67k
    // result in the same effect as placing it immediately after the current feature.
452
1.67k
    if (ItRNoFullFP16 < ItRFP16FML)
453
514
      Features.push_back("-fp16fml");
454
1.16k
    else if (ItRNoFullFP16 > ItRFP16FML)
455
23
      Features.push_back("+fullfp16");
456
1.67k
  }
457
1.69k
458
1.69k
  // Setting -msoft-float/-mfloat-abi=soft effectively disables the FPU (GCC
459
1.69k
  // ignores the -mfpu options in this case).
460
1.69k
  // Note that the ABI can also be set implicitly by the target selected.
461
1.69k
  if (ABI == arm::FloatABI::Soft) {
462
724
    llvm::ARM::getFPUFeatures(llvm::ARM::FK_NONE, Features);
463
724
464
724
    // Disable all features relating to hardware FP, not already disabled by the
465
724
    // above call.
466
724
    Features.insert(Features.end(),
467
724
                    {"-dotprod", "-fp16fml", "-mve", "-mve.fp", "-fpregs"});
468
966
  } else if (FPUID == llvm::ARM::FK_NONE) {
469
15
    // -mfpu=none is *very* similar to -mfloat-abi=soft, only that it should not
470
15
    // disable MVE-I.
471
15
    Features.insert(Features.end(), {"-dotprod", "-fp16fml", "-mve.fp"});
472
15
    if (!hasIntegerMVE(Features))
473
12
      Features.emplace_back("-fpregs");
474
15
  }
475
1.69k
476
1.69k
  // En/disable crc code generation.
477
1.69k
  if (Arg *A = Args.getLastArg(options::OPT_mcrc, options::OPT_mnocrc)) {
478
3
    if (A->getOption().matches(options::OPT_mcrc))
479
1
      Features.push_back("+crc");
480
2
    else
481
2
      Features.push_back("-crc");
482
3
  }
483
1.69k
484
1.69k
  // For Arch >= ARMv8.0 && A profile:  crypto = sha2 + aes
485
1.69k
  // FIXME: this needs reimplementation after the TargetParser rewrite
486
10.3k
  auto CryptoIt = llvm::find_if(llvm::reverse(Features), [](const StringRef F) {
487
10.3k
    return F.contains("crypto");
488
10.3k
  });
489
1.69k
  if (CryptoIt != Features.rend()) {
490
1.09k
    if (CryptoIt->take_front() == "+") {
491
29
      StringRef ArchSuffix = arm::getLLVMArchSuffixForARM(
492
29
          arm::getARMTargetCPU(CPUName, ArchName, Triple), ArchName, Triple);
493
29
      if (llvm::ARM::parseArchVersion(ArchSuffix) >= 8 &&
494
29
          llvm::ARM::parseArchProfile(ArchSuffix) ==
495
23
              llvm::ARM::ProfileKind::A) {
496
19
        if (ArchName.find_lower("+nosha2") == StringRef::npos &&
497
19
            
CPUName.find_lower("+nosha2") == StringRef::npos18
)
498
16
          Features.push_back("+sha2");
499
19
        if (ArchName.find_lower("+noaes") == StringRef::npos &&
500
19
            
CPUName.find_lower("+noaes") == StringRef::npos18
)
501
16
          Features.push_back("+aes");
502
19
      } else {
503
10
        D.Diag(clang::diag::warn_target_unsupported_extension)
504
10
            << "crypto"
505
10
            << llvm::ARM::getArchName(llvm::ARM::parseArch(ArchSuffix));
506
10
        // With -fno-integrated-as -mfpu=crypto-neon-fp-armv8 some assemblers such as the GNU assembler
507
10
        // will permit the use of crypto instructions as the fpu will override the architecture.
508
10
        // We keep the crypto feature in this case to preserve compatibility.
509
10
        // In all other cases we remove the crypto feature.
510
10
        if (!Args.hasArg(options::OPT_fno_integrated_as))
511
6
          Features.push_back("-crypto");
512
10
      }
513
29
    }
514
1.09k
  }
515
1.69k
516
1.69k
  // CMSE: Check for target 8M (for -mcmse to be applicable) is performed later.
517
1.69k
  if (Args.getLastArg(options::OPT_mcmse))
518
7
    Features.push_back("+8msecext");
519
1.69k
520
1.69k
  // Look for the last occurrence of -mlong-calls or -mno-long-calls. If
521
1.69k
  // neither options are specified, see if we are compiling for kernel/kext and
522
1.69k
  // decide whether to pass "+long-calls" based on the OS and its version.
523
1.69k
  if (Arg *A = Args.getLastArg(options::OPT_mlong_calls,
524
4
                               options::OPT_mno_long_calls)) {
525
4
    if (A->getOption().matches(options::OPT_mlong_calls))
526
3
      Features.push_back("+long-calls");
527
1.68k
  } else if (KernelOrKext && 
(8
!Triple.isiOS()8
||
Triple.isOSVersionLT(6)7
) &&
528
1.68k
             
!Triple.isWatchOS()5
) {
529
4
      Features.push_back("+long-calls");
530
4
  }
531
1.69k
532
1.69k
  // Generate execute-only output (no data access to code sections).
533
1.69k
  // This only makes sense for the compiler, not for the assembler.
534
1.69k
  if (!ForAS) {
535
1.63k
    // Supported only on ARMv6T2 and ARMv7 and above.
536
1.63k
    // Cannot be combined with -mno-movt or -mlong-calls
537
1.63k
    if (Arg *A = Args.getLastArg(options::OPT_mexecute_only, options::OPT_mno_execute_only)) {
538
22
      if (A->getOption().matches(options::OPT_mexecute_only)) {
539
14
        if (getARMSubArchVersionNumber(Triple) < 7 &&
540
14
            
llvm::ARM::parseArch(Triple.getArchName()) != llvm::ARM::ArchKind::ARMV6T24
)
541
2
              D.Diag(diag::err_target_unsupported_execute_only) << Triple.getArchName();
542
12
        else if (Arg *B = Args.getLastArg(options::OPT_mno_movt))
543
2
          D.Diag(diag::err_opt_not_valid_with_opt) << A->getAsString(Args) << B->getAsString(Args);
544
10
        // Long calls create constant pool entries and have not yet been fixed up
545
10
        // to play nicely with execute-only. Hence, they cannot be used in
546
10
        // execute-only code for now
547
10
        else if (Arg *B = Args.getLastArg(options::OPT_mlong_calls, options::OPT_mno_long_calls)) {
548
2
          if (B->getOption().matches(options::OPT_mlong_calls))
549
2
            D.Diag(diag::err_opt_not_valid_with_opt) << A->getAsString(Args) << B->getAsString(Args);
550
2
        }
551
14
        Features.push_back("+execute-only");
552
14
      }
553
22
    }
554
1.63k
  }
555
1.69k
556
1.69k
  // Kernel code has more strict alignment requirements.
557
1.69k
  if (KernelOrKext)
558
8
    Features.push_back("+strict-align");
559
1.68k
  else if (Arg *A = Args.getLastArg(options::OPT_mno_unaligned_access,
560
11
                                    options::OPT_munaligned_access)) {
561
11
    if (A->getOption().matches(options::OPT_munaligned_access)) {
562
6
      // No v6M core supports unaligned memory access (v6M ARM ARM A3.2).
563
6
      if (Triple.getSubArch() == llvm::Triple::SubArchType::ARMSubArch_v6m)
564
2
        D.Diag(diag::err_target_unsupported_unaligned) << "v6m";
565
4
      // v8M Baseline follows on from v6M, so doesn't support unaligned memory
566
4
      // access either.
567
4
      else if (Triple.getSubArch() == llvm::Triple::SubArchType::ARMSubArch_v8m_baseline)
568
1
        D.Diag(diag::err_target_unsupported_unaligned) << "v8m.base";
569
6
    } else
570
5
      Features.push_back("+strict-align");
571
1.67k
  } else {
572
1.67k
    // Assume pre-ARMv6 doesn't support unaligned accesses.
573
1.67k
    //
574
1.67k
    // ARMv6 may or may not support unaligned accesses depending on the
575
1.67k
    // SCTLR.U bit, which is architecture-specific. We assume ARMv6
576
1.67k
    // Darwin and NetBSD targets support unaligned accesses, and others don't.
577
1.67k
    //
578
1.67k
    // ARMv7 always has SCTLR.U set to 1, but it has a new SCTLR.A bit
579
1.67k
    // which raises an alignment fault on unaligned accesses. Linux
580
1.67k
    // defaults this bit to 0 and handles it as a system-wide (not
581
1.67k
    // per-process) setting. It is therefore safe to assume that ARMv7+
582
1.67k
    // Linux targets support unaligned accesses. The same goes for NaCl.
583
1.67k
    //
584
1.67k
    // The above behavior is consistent with GCC.
585
1.67k
    int VersionNum = getARMSubArchVersionNumber(Triple);
586
1.67k
    if (Triple.isOSDarwin() || 
Triple.isOSNetBSD()1.56k
) {
587
142
      if (VersionNum < 6 ||
588
142
          
Triple.getSubArch() == llvm::Triple::SubArchType::ARMSubArch_v6m113
)
589
30
        Features.push_back("+strict-align");
590
1.52k
    } else if (Triple.isOSLinux() || 
Triple.isOSNaCl()955
) {
591
579
      if (VersionNum < 7)
592
228
        Features.push_back("+strict-align");
593
579
    } else
594
950
      Features.push_back("+strict-align");
595
1.67k
  }
596
1.69k
597
1.69k
  // llvm does not support reserving registers in general. There is support
598
1.69k
  // for reserving r9 on ARM though (defined as a platform-specific register
599
1.69k
  // in ARM EABI).
600
1.69k
  if (Args.hasArg(options::OPT_ffixed_r9))
601
1
    Features.push_back("+reserve-r9");
602
1.69k
603
1.69k
  // The kext linker doesn't know how to deal with movw/movt.
604
1.69k
  if (KernelOrKext || 
Args.hasArg(options::OPT_mno_movt)1.68k
)
605
11
    Features.push_back("+no-movt");
606
1.69k
607
1.69k
  if (Args.hasArg(options::OPT_mno_neg_immediates))
608
1
    Features.push_back("+no-neg-immediates");
609
1.69k
}
610
611
5.63k
const std::string arm::getARMArch(StringRef Arch, const llvm::Triple &Triple) {
612
5.63k
  std::string MArch;
613
5.63k
  if (!Arch.empty())
614
2.69k
    MArch = std::string(Arch);
615
2.94k
  else
616
2.94k
    MArch = std::string(Triple.getArchName());
617
5.63k
  MArch = StringRef(MArch).split("+").first.lower();
618
5.63k
619
5.63k
  // Handle -march=native.
620
5.63k
  if (MArch == "native") {
621
0
    std::string CPU = std::string(llvm::sys::getHostCPUName());
622
0
    if (CPU != "generic") {
623
0
      // Translate the native cpu into the architecture suffix for that CPU.
624
0
      StringRef Suffix = arm::getLLVMArchSuffixForARM(CPU, MArch, Triple);
625
0
      // If there is no valid architecture suffix for this CPU we don't know how
626
0
      // to handle it, so return no architecture.
627
0
      if (Suffix.empty())
628
0
        MArch = "";
629
0
      else
630
0
        MArch = std::string("arm") + Suffix.str();
631
0
    }
632
0
  }
633
5.63k
634
5.63k
  return MArch;
635
5.63k
}
636
637
/// Get the (LLVM) name of the minimum ARM CPU for the arch we are targeting.
638
4.20k
StringRef arm::getARMCPUForMArch(StringRef Arch, const llvm::Triple &Triple) {
639
4.20k
  std::string MArch = getARMArch(Arch, Triple);
640
4.20k
  // getARMCPUForArch defaults to the triple if MArch is empty, but empty MArch
641
4.20k
  // here means an -march=native that we can't handle, so instead return no CPU.
642
4.20k
  if (MArch.empty())
643
0
    return StringRef();
644
4.20k
645
4.20k
  // We need to return an empty string here on invalid MArch values as the
646
4.20k
  // various places that call this function can't cope with a null result.
647
4.20k
  return Triple.getARMCPUForArch(MArch);
648
4.20k
}
649
650
/// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targeting.
651
std::string arm::getARMTargetCPU(StringRef CPU, StringRef Arch,
652
5.83k
                                 const llvm::Triple &Triple) {
653
5.83k
  // FIXME: Warn on inconsistent use of -mcpu and -march.
654
5.83k
  // If we have -mcpu=, use that.
655
5.83k
  if (!CPU.empty()) {
656
2.04k
    std::string MCPU = StringRef(CPU).split("+").first.lower();
657
2.04k
    // Handle -mcpu=native.
658
2.04k
    if (MCPU == "native")
659
0
      return std::string(llvm::sys::getHostCPUName());
660
2.04k
    else
661
2.04k
      return MCPU;
662
3.78k
  }
663
3.78k
664
3.78k
  return std::string(getARMCPUForMArch(Arch, Triple));
665
3.78k
}
666
667
/// getLLVMArchSuffixForARM - Get the LLVM ArchKind value to use for a
668
/// particular CPU (or Arch, if CPU is generic). This is needed to
669
/// pass to functions like llvm::ARM::getDefaultFPU which need an
670
/// ArchKind as well as a CPU name.
671
llvm::ARM::ArchKind arm::getLLVMArchKindForARM(StringRef CPU, StringRef Arch,
672
2.91k
                                               const llvm::Triple &Triple) {
673
2.91k
  llvm::ARM::ArchKind ArchKind;
674
2.91k
  if (CPU == "generic" || 
CPU.empty()1.97k
) {
675
942
    std::string ARMArch = tools::arm::getARMArch(Arch, Triple);
676
942
    ArchKind = llvm::ARM::parseArch(ARMArch);
677
942
    if (ArchKind == llvm::ARM::ArchKind::INVALID)
678
8
      // In case of generic Arch, i.e. "arm",
679
8
      // extract arch from default cpu of the Triple
680
8
      ArchKind = llvm::ARM::parseCPUArch(Triple.getARMCPUForArch(ARMArch));
681
1.97k
  } else {
682
1.97k
    // FIXME: horrible hack to get around the fact that Cortex-A7 is only an
683
1.97k
    // armv7k triple if it's actually been specified via "-arch armv7k".
684
1.97k
    ArchKind = (Arch == "armv7k" || 
Arch == "thumbv7k"1.91k
)
685
1.97k
                          ? 
llvm::ARM::ArchKind::ARMV7K58
686
1.97k
                          : 
llvm::ARM::parseCPUArch(CPU)1.91k
;
687
1.97k
  }
688
2.91k
  return ArchKind;
689
2.91k
}
690
691
/// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular
692
/// CPU  (or Arch, if CPU is generic).
693
// FIXME: This is redundant with -mcpu, why does LLVM use this.
694
StringRef arm::getLLVMArchSuffixForARM(StringRef CPU, StringRef Arch,
695
2.41k
                                       const llvm::Triple &Triple) {
696
2.41k
  llvm::ARM::ArchKind ArchKind = getLLVMArchKindForARM(CPU, Arch, Triple);
697
2.41k
  if (ArchKind == llvm::ARM::ArchKind::INVALID)
698
3
    return "";
699
2.41k
  return llvm::ARM::getSubArch(ArchKind);
700
2.41k
}
701
702
void arm::appendBE8LinkFlag(const ArgList &Args, ArgStringList &CmdArgs,
703
20
                            const llvm::Triple &Triple) {
704
20
  if (Args.hasArg(options::OPT_r))
705
1
    return;
706
19
707
19
  // ARMv7 (and later) and ARMv6-M do not support BE-32, so instruct the linker
708
19
  // to generate BE-8 executables.
709
19
  if (arm::getARMSubArchVersionNumber(Triple) >= 7 || 
arm::isARMMProfile(Triple)12
)
710
7
    CmdArgs.push_back("--be8");
711
19
}