Coverage Report

Created: 2020-02-15 09:57

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Driver/ToolChains/Arch/AArch64.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- AArch64.cpp - AArch64 (not ARM) 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 "AArch64.h"
10
#include "clang/Driver/Driver.h"
11
#include "clang/Driver/DriverDiagnostic.h"
12
#include "clang/Driver/Options.h"
13
#include "llvm/Option/ArgList.h"
14
#include "llvm/Support/TargetParser.h"
15
#include "llvm/Support/Host.h"
16
17
using namespace clang::driver;
18
using namespace clang::driver::tools;
19
using namespace clang;
20
using namespace llvm::opt;
21
22
/// \returns true if the given triple can determine the default CPU type even
23
/// if -arch is not specified.
24
1.06k
static bool isCPUDeterminedByTriple(const llvm::Triple &Triple) {
25
1.06k
  return Triple.isOSDarwin();
26
1.06k
}
27
28
/// getAArch64TargetCPU - Get the (LLVM) name of the AArch64 cpu we are
29
/// targeting. Set \p A to the Arg corresponding to the -mcpu argument if it is
30
/// provided, or to nullptr otherwise.
31
std::string aarch64::getAArch64TargetCPU(const ArgList &Args,
32
1.10k
                                         const llvm::Triple &Triple, Arg *&A) {
33
1.10k
  std::string CPU;
34
1.10k
  // If we have -mcpu, use that.
35
1.10k
  if ((A = Args.getLastArg(options::OPT_mcpu_EQ))) {
36
182
    StringRef Mcpu = A->getValue();
37
182
    CPU = Mcpu.split("+").first.lower();
38
182
  }
39
1.10k
40
1.10k
  // Handle CPU name is 'native'.
41
1.10k
  if (CPU == "native")
42
1
    return std::string(llvm::sys::getHostCPUName());
43
1.10k
  else if (CPU.size())
44
181
    return CPU;
45
919
46
919
  // Make sure we pick the appropriate Apple CPU if -arch is used or when
47
919
  // targetting a Darwin OS.
48
919
  if (Args.getLastArg(options::OPT_arch) || 
Triple.isOSDarwin()845
)
49
197
    return Triple.getArch() == llvm::Triple::aarch64_32 ? 
"apple-s4"9
50
197
                                                        : 
"apple-a7"188
;
51
722
52
722
  return "generic";
53
722
}
54
55
// Decode AArch64 features from string like +[no]featureA+[no]featureB+...
56
static bool DecodeAArch64Features(const Driver &D, StringRef text,
57
206
                                  std::vector<StringRef> &Features) {
58
206
  SmallVector<StringRef, 8> Split;
59
206
  text.split(Split, StringRef("+"), -1, false);
60
206
61
290
  for (StringRef Feature : Split) {
62
290
    StringRef FeatureName = llvm::AArch64::getArchExtFeature(Feature);
63
290
    if (!FeatureName.empty())
64
284
      Features.push_back(FeatureName);
65
6
    else if (Feature == "neon" || 
Feature == "noneon"3
)
66
6
      D.Diag(clang::diag::err_drv_no_neon_modifier);
67
0
    else
68
0
      return false;
69
290
  }
70
206
  return true;
71
206
}
72
73
// Check if the CPU name and feature modifiers in -mcpu are legal. If yes,
74
// decode CPU and feature.
75
static bool DecodeAArch64Mcpu(const Driver &D, StringRef Mcpu, StringRef &CPU,
76
815
                              std::vector<StringRef> &Features) {
77
815
  std::pair<StringRef, StringRef> Split = Mcpu.split("+");
78
815
  CPU = Split.first;
79
815
80
815
  if (CPU == "native")
81
1
    CPU = llvm::sys::getHostCPUName();
82
815
83
815
  if (CPU == "generic") {
84
45
    Features.push_back("+neon");
85
770
  } else {
86
770
    llvm::AArch64::ArchKind ArchKind = llvm::AArch64::parseCPUArch(CPU);
87
770
    if (!llvm::AArch64::getArchFeatures(ArchKind, Features))
88
3
      return false;
89
767
90
767
    unsigned Extension = llvm::AArch64::getDefaultExtensions(CPU, ArchKind);
91
767
    if (!llvm::AArch64::getExtensionFeatures(Extension, Features))
92
0
      return false;
93
812
   }
94
812
95
812
  if (Split.second.size() && 
!DecodeAArch64Features(D, Split.second, Features)38
)
96
0
    return false;
97
812
98
812
  return true;
99
812
}
100
101
static bool
102
getAArch64ArchFeaturesFromMarch(const Driver &D, StringRef March,
103
                                const ArgList &Args,
104
285
                                std::vector<StringRef> &Features) {
105
285
  std::string MarchLowerCase = March.lower();
106
285
  std::pair<StringRef, StringRef> Split = StringRef(MarchLowerCase).split("+");
107
285
108
285
  llvm::AArch64::ArchKind ArchKind = llvm::AArch64::parseArch(Split.first);
109
285
  if (ArchKind == llvm::AArch64::ArchKind::INVALID ||
110
285
      
!llvm::AArch64::getArchFeatures(ArchKind, Features)283
||
111
285
      
(283
Split.second.size()283
&&
!DecodeAArch64Features(D, Split.second, Features)168
))
112
2
    return false;
113
283
114
283
  return true;
115
283
}
116
117
static bool
118
getAArch64ArchFeaturesFromMcpu(const Driver &D, StringRef Mcpu,
119
                               const ArgList &Args,
120
239
                               std::vector<StringRef> &Features) {
121
239
  StringRef CPU;
122
239
  std::string McpuLowerCase = Mcpu.lower();
123
239
  if (!DecodeAArch64Mcpu(D, McpuLowerCase, CPU, Features))
124
2
    return false;
125
237
126
237
  return true;
127
237
}
128
129
static bool
130
getAArch64MicroArchFeaturesFromMtune(const Driver &D, StringRef Mtune,
131
                                     const ArgList &Args,
132
345
                                     std::vector<StringRef> &Features) {
133
345
  std::string MtuneLowerCase = Mtune.lower();
134
345
  // Check CPU name is valid
135
345
  std::vector<StringRef> MtuneFeatures;
136
345
  StringRef Tune;
137
345
  if (!DecodeAArch64Mcpu(D, MtuneLowerCase, Tune, MtuneFeatures))
138
1
    return false;
139
344
140
344
  // Handle CPU name is 'native'.
141
344
  if (MtuneLowerCase == "native")
142
0
    MtuneLowerCase = std::string(llvm::sys::getHostCPUName());
143
344
  if (MtuneLowerCase == "cyclone" || 
MtuneLowerCase.find("apple") == 0331
) {
144
87
    Features.push_back("+zcm");
145
87
    Features.push_back("+zcz");
146
87
  }
147
344
  return true;
148
344
}
149
150
static bool
151
getAArch64MicroArchFeaturesFromMcpu(const Driver &D, StringRef Mcpu,
152
                                    const ArgList &Args,
153
231
                                    std::vector<StringRef> &Features) {
154
231
  StringRef CPU;
155
231
  std::vector<StringRef> DecodedFeature;
156
231
  std::string McpuLowerCase = Mcpu.lower();
157
231
  if (!DecodeAArch64Mcpu(D, McpuLowerCase, CPU, DecodedFeature))
158
0
    return false;
159
231
160
231
  return getAArch64MicroArchFeaturesFromMtune(D, CPU, Args, Features);
161
231
}
162
163
void aarch64::getAArch64TargetFeatures(const Driver &D,
164
                                       const llvm::Triple &Triple,
165
                                       const ArgList &Args,
166
928
                                       std::vector<StringRef> &Features) {
167
928
  Arg *A;
168
928
  bool success = true;
169
928
  // Enable NEON by default.
170
928
  Features.push_back("+neon");
171
928
  if ((A = Args.getLastArg(options::OPT_march_EQ)))
172
285
    success = getAArch64ArchFeaturesFromMarch(D, A->getValue(), Args, Features);
173
643
  else if ((A = Args.getLastArg(options::OPT_mcpu_EQ)))
174
173
    success = getAArch64ArchFeaturesFromMcpu(D, A->getValue(), Args, Features);
175
470
  else if (Args.hasArg(options::OPT_arch) || 
isCPUDeterminedByTriple(Triple)445
)
176
66
    success = getAArch64ArchFeaturesFromMcpu(
177
66
        D, getAArch64TargetCPU(Args, Triple, A), Args, Features);
178
928
179
928
  if (success && 
(A = Args.getLastArg(clang::driver::options::OPT_mtune_EQ))924
)
180
114
    success =
181
114
        getAArch64MicroArchFeaturesFromMtune(D, A->getValue(), Args, Features);
182
814
  else if (success && 
(A = Args.getLastArg(options::OPT_mcpu_EQ))810
)
183
166
    success =
184
166
        getAArch64MicroArchFeaturesFromMcpu(D, A->getValue(), Args, Features);
185
648
  else if (success &&
186
648
           
(644
Args.hasArg(options::OPT_arch)644
||
isCPUDeterminedByTriple(Triple)620
))
187
65
    success = getAArch64MicroArchFeaturesFromMcpu(
188
65
        D, getAArch64TargetCPU(Args, Triple, A), Args, Features);
189
928
190
928
  if (!success)
191
5
    D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
192
928
193
928
  if (Args.getLastArg(options::OPT_mgeneral_regs_only)) {
194
2
    Features.push_back("-fp-armv8");
195
2
    Features.push_back("-crypto");
196
2
    Features.push_back("-neon");
197
2
  }
198
928
199
928
  if (Arg *A = Args.getLastArg(options::OPT_mtp_mode_EQ)) {
200
4
    StringRef Mtp = A->getValue();
201
4
    if (Mtp == "el3")
202
1
      Features.push_back("+tpidr-el3");
203
3
    else if (Mtp == "el2")
204
1
      Features.push_back("+tpidr-el2");
205
2
    else if (Mtp == "el1")
206
1
      Features.push_back("+tpidr-el1");
207
1
    else if (Mtp != "el0")
208
0
      D.Diag(diag::err_drv_invalid_mtp) << A->getAsString(Args);
209
4
  }
210
928
211
928
  // En/disable crc
212
928
  if (Arg *A = Args.getLastArg(options::OPT_mcrc, options::OPT_mnocrc)) {
213
2
    if (A->getOption().matches(options::OPT_mcrc))
214
2
      Features.push_back("+crc");
215
0
    else
216
0
      Features.push_back("-crc");
217
2
  }
218
928
219
928
  // Handle (arch-dependent) fp16fml/fullfp16 relationship.
220
928
  // FIXME: this fp16fml option handling will be reimplemented after the
221
928
  // TargetParser rewrite.
222
928
  const auto ItRNoFullFP16 = std::find(Features.rbegin(), Features.rend(), "-fullfp16");
223
928
  const auto ItRFP16FML = std::find(Features.rbegin(), Features.rend(), "+fp16fml");
224
928
  if (llvm::is_contained(Features, "+v8.4a")) {
225
48
    const auto ItRFullFP16  = std::find(Features.rbegin(), Features.rend(), "+fullfp16");
226
48
    if (ItRFullFP16 < ItRNoFullFP16 && 
ItRFullFP16 < ItRFP16FML11
) {
227
11
      // Only entangled feature that can be to the right of this +fullfp16 is -fp16fml.
228
11
      // Only append the +fp16fml if there is no -fp16fml after the +fullfp16.
229
11
      if (std::find(Features.rbegin(), ItRFullFP16, "-fp16fml") == ItRFullFP16)
230
8
        Features.push_back("+fp16fml");
231
11
    }
232
37
    else
233
37
      goto fp16_fml_fallthrough;
234
880
  } else {
235
917
fp16_fml_fallthrough:
236
917
    // In both of these cases, putting the 'other' feature on the end of the vector will
237
917
    // result in the same effect as placing it immediately after the current feature.
238
917
    if (ItRNoFullFP16 < ItRFP16FML)
239
10
      Features.push_back("-fp16fml");
240
907
    else if (ItRNoFullFP16 > ItRFP16FML)
241
22
      Features.push_back("+fullfp16");
242
917
  }
243
928
244
928
  // FIXME: this needs reimplementation too after the TargetParser rewrite
245
928
  //
246
928
  // Context sensitive meaning of Crypto:
247
928
  // 1) For Arch >= ARMv8.4a:  crypto = sm4 + sha3 + sha2 + aes
248
928
  // 2) For Arch <= ARMv8.3a:  crypto = sha2 + aes
249
928
  const auto ItBegin = Features.begin();
250
928
  const auto ItEnd = Features.end();
251
928
  const auto ItRBegin = Features.rbegin();
252
928
  const auto ItREnd = Features.rend();
253
928
  const auto ItRCrypto = std::find(ItRBegin, ItREnd, "+crypto");
254
928
  const auto ItRNoCrypto = std::find(ItRBegin, ItREnd, "-crypto");
255
928
  const auto HasCrypto  = ItRCrypto != ItREnd;
256
928
  const auto HasNoCrypto = ItRNoCrypto != ItREnd;
257
928
  const ptrdiff_t PosCrypto = ItRCrypto - ItRBegin;
258
928
  const ptrdiff_t PosNoCrypto = ItRNoCrypto - ItRBegin;
259
928
260
928
  bool NoCrypto = false;
261
928
  if (HasCrypto && 
HasNoCrypto240
) {
262
8
    if (PosNoCrypto < PosCrypto)
263
6
      NoCrypto = true;
264
8
  }
265
928
266
928
  if (std::find(ItBegin, ItEnd, "+v8.4a") != ItEnd) {
267
48
    if (HasCrypto && 
!NoCrypto3
) {
268
3
      // Check if we have NOT disabled an algorithm with something like:
269
3
      //   +crypto, -algorithm
270
3
      // And if "-algorithm" does not occur, we enable that crypto algorithm.
271
3
      const bool HasSM4  = (std::find(ItBegin, ItEnd, "-sm4") == ItEnd);
272
3
      const bool HasSHA3 = (std::find(ItBegin, ItEnd, "-sha3") == ItEnd);
273
3
      const bool HasSHA2 = (std::find(ItBegin, ItEnd, "-sha2") == ItEnd);
274
3
      const bool HasAES  = (std::find(ItBegin, ItEnd, "-aes") == ItEnd);
275
3
      if (HasSM4)
276
2
        Features.push_back("+sm4");
277
3
      if (HasSHA3)
278
2
        Features.push_back("+sha3");
279
3
      if (HasSHA2)
280
3
        Features.push_back("+sha2");
281
3
      if (HasAES)
282
3
        Features.push_back("+aes");
283
45
    } else if (HasNoCrypto) {
284
1
      // Check if we have NOT enabled a crypto algorithm with something like:
285
1
      //   -crypto, +algorithm
286
1
      // And if "+algorithm" does not occur, we disable that crypto algorithm.
287
1
      const bool HasSM4  = (std::find(ItBegin, ItEnd, "+sm4") != ItEnd);
288
1
      const bool HasSHA3 = (std::find(ItBegin, ItEnd, "+sha3") != ItEnd);
289
1
      const bool HasSHA2 = (std::find(ItBegin, ItEnd, "+sha2") != ItEnd);
290
1
      const bool HasAES  = (std::find(ItBegin, ItEnd, "+aes") != ItEnd);
291
1
      if (!HasSM4)
292
1
        Features.push_back("-sm4");
293
1
      if (!HasSHA3)
294
1
        Features.push_back("-sha3");
295
1
      if (!HasSHA2)
296
1
        Features.push_back("-sha2");
297
1
      if (!HasAES)
298
1
        Features.push_back("-aes");
299
1
    }
300
880
  } else {
301
880
    if (HasCrypto && 
!NoCrypto237
) {
302
231
      const bool HasSHA2 = (std::find(ItBegin, ItEnd, "-sha2") == ItEnd);
303
231
      const bool HasAES = (std::find(ItBegin, ItEnd, "-aes") == ItEnd);
304
231
      if (HasSHA2)
305
230
        Features.push_back("+sha2");
306
231
      if (HasAES)
307
230
        Features.push_back("+aes");
308
649
    } else if (HasNoCrypto) {
309
16
      const bool HasSHA2 = (std::find(ItBegin, ItEnd, "+sha2") != ItEnd);
310
16
      const bool HasAES  = (std::find(ItBegin, ItEnd, "+aes") != ItEnd);
311
16
      const bool HasV82a = (std::find(ItBegin, ItEnd, "+v8.2a") != ItEnd);
312
16
      const bool HasV83a = (std::find(ItBegin, ItEnd, "+v8.3a") != ItEnd);
313
16
      const bool HasV84a = (std::find(ItBegin, ItEnd, "+v8.4a") != ItEnd);
314
16
      if (!HasSHA2)
315
15
        Features.push_back("-sha2");
316
16
      if (!HasAES)
317
15
        Features.push_back("-aes");
318
16
      if (HasV82a || 
HasV83a15
||
HasV84a13
) {
319
3
        Features.push_back("-sm4");
320
3
        Features.push_back("-sha3");
321
3
      }
322
16
    }
323
880
  }
324
928
325
928
  if (Arg *A = Args.getLastArg(options::OPT_mno_unaligned_access,
326
9
                               options::OPT_munaligned_access))
327
9
    if (A->getOption().matches(options::OPT_mno_unaligned_access))
328
6
      Features.push_back("+strict-align");
329
928
330
928
  if (Args.hasArg(options::OPT_ffixed_x1))
331
3
    Features.push_back("+reserve-x1");
332
928
333
928
  if (Args.hasArg(options::OPT_ffixed_x2))
334
3
    Features.push_back("+reserve-x2");
335
928
336
928
  if (Args.hasArg(options::OPT_ffixed_x3))
337
2
    Features.push_back("+reserve-x3");
338
928
339
928
  if (Args.hasArg(options::OPT_ffixed_x4))
340
2
    Features.push_back("+reserve-x4");
341
928
342
928
  if (Args.hasArg(options::OPT_ffixed_x5))
343
2
    Features.push_back("+reserve-x5");
344
928
345
928
  if (Args.hasArg(options::OPT_ffixed_x6))
346
2
    Features.push_back("+reserve-x6");
347
928
348
928
  if (Args.hasArg(options::OPT_ffixed_x7))
349
2
    Features.push_back("+reserve-x7");
350
928
351
928
  if (Args.hasArg(options::OPT_ffixed_x9))
352
2
    Features.push_back("+reserve-x9");
353
928
354
928
  if (Args.hasArg(options::OPT_ffixed_x10))
355
2
    Features.push_back("+reserve-x10");
356
928
357
928
  if (Args.hasArg(options::OPT_ffixed_x11))
358
2
    Features.push_back("+reserve-x11");
359
928
360
928
  if (Args.hasArg(options::OPT_ffixed_x12))
361
2
    Features.push_back("+reserve-x12");
362
928
363
928
  if (Args.hasArg(options::OPT_ffixed_x13))
364
2
    Features.push_back("+reserve-x13");
365
928
366
928
  if (Args.hasArg(options::OPT_ffixed_x14))
367
2
    Features.push_back("+reserve-x14");
368
928
369
928
  if (Args.hasArg(options::OPT_ffixed_x15))
370
2
    Features.push_back("+reserve-x15");
371
928
372
928
  if (Args.hasArg(options::OPT_ffixed_x18))
373
5
    Features.push_back("+reserve-x18");
374
928
375
928
  if (Args.hasArg(options::OPT_ffixed_x20))
376
2
    Features.push_back("+reserve-x20");
377
928
378
928
  if (Args.hasArg(options::OPT_ffixed_x21))
379
2
    Features.push_back("+reserve-x21");
380
928
381
928
  if (Args.hasArg(options::OPT_ffixed_x22))
382
2
    Features.push_back("+reserve-x22");
383
928
384
928
  if (Args.hasArg(options::OPT_ffixed_x23))
385
2
    Features.push_back("+reserve-x23");
386
928
387
928
  if (Args.hasArg(options::OPT_ffixed_x24))
388
2
    Features.push_back("+reserve-x24");
389
928
390
928
  if (Args.hasArg(options::OPT_ffixed_x25))
391
2
    Features.push_back("+reserve-x25");
392
928
393
928
  if (Args.hasArg(options::OPT_ffixed_x26))
394
2
    Features.push_back("+reserve-x26");
395
928
396
928
  if (Args.hasArg(options::OPT_ffixed_x27))
397
2
    Features.push_back("+reserve-x27");
398
928
399
928
  if (Args.hasArg(options::OPT_ffixed_x28))
400
2
    Features.push_back("+reserve-x28");
401
928
402
928
  if (Args.hasArg(options::OPT_fcall_saved_x8))
403
2
    Features.push_back("+call-saved-x8");
404
928
405
928
  if (Args.hasArg(options::OPT_fcall_saved_x9))
406
2
    Features.push_back("+call-saved-x9");
407
928
408
928
  if (Args.hasArg(options::OPT_fcall_saved_x10))
409
2
    Features.push_back("+call-saved-x10");
410
928
411
928
  if (Args.hasArg(options::OPT_fcall_saved_x11))
412
2
    Features.push_back("+call-saved-x11");
413
928
414
928
  if (Args.hasArg(options::OPT_fcall_saved_x12))
415
2
    Features.push_back("+call-saved-x12");
416
928
417
928
  if (Args.hasArg(options::OPT_fcall_saved_x13))
418
2
    Features.push_back("+call-saved-x13");
419
928
420
928
  if (Args.hasArg(options::OPT_fcall_saved_x14))
421
2
    Features.push_back("+call-saved-x14");
422
928
423
928
  if (Args.hasArg(options::OPT_fcall_saved_x15))
424
2
    Features.push_back("+call-saved-x15");
425
928
426
928
  if (Args.hasArg(options::OPT_fcall_saved_x18))
427
3
    Features.push_back("+call-saved-x18");
428
928
429
928
  if (Args.hasArg(options::OPT_mno_neg_immediates))
430
1
    Features.push_back("+no-neg-immediates");
431
928
}