Coverage Report

Created: 2019-07-24 05:18

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