clang  5.0.0
AArch64.cpp
Go to the documentation of this file.
1 //===--- AArch64.cpp - AArch64 (not ARM) Helpers for Tools ------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "AArch64.h"
11 #include "clang/Driver/Driver.h"
13 #include "clang/Driver/Options.h"
14 #include "llvm/Option/ArgList.h"
15 #include "llvm/Support/TargetParser.h"
16 
17 using namespace clang::driver;
18 using namespace clang::driver::tools;
19 using namespace clang;
20 using namespace llvm::opt;
21 
22 /// getAArch64TargetCPU - Get the (LLVM) name of the AArch64 cpu we are
23 /// targeting. Set \p A to the Arg corresponding to the -mcpu or -mtune
24 /// arguments if they are provided, or to nullptr otherwise.
25 std::string aarch64::getAArch64TargetCPU(const ArgList &Args, Arg *&A) {
26  std::string CPU;
27  // If we have -mtune or -mcpu, use that.
28  if ((A = Args.getLastArg(clang::driver::options::OPT_mtune_EQ))) {
29  CPU = StringRef(A->getValue()).lower();
30  } else if ((A = Args.getLastArg(options::OPT_mcpu_EQ))) {
31  StringRef Mcpu = A->getValue();
32  CPU = Mcpu.split("+").first.lower();
33  }
34 
35  // Handle CPU name is 'native'.
36  if (CPU == "native")
37  return llvm::sys::getHostCPUName();
38  else if (CPU.size())
39  return CPU;
40 
41  // Make sure we pick "cyclone" if -arch is used.
42  // FIXME: Should this be picked by checking the target triple instead?
43  if (Args.getLastArg(options::OPT_arch))
44  return "cyclone";
45 
46  return "generic";
47 }
48 
49 // Decode AArch64 features from string like +[no]featureA+[no]featureB+...
50 static bool DecodeAArch64Features(const Driver &D, StringRef text,
51  std::vector<StringRef> &Features) {
53  text.split(Split, StringRef("+"), -1, false);
54 
55  for (StringRef Feature : Split) {
56  StringRef FeatureName = llvm::AArch64::getArchExtFeature(Feature);
57  if (!FeatureName.empty())
58  Features.push_back(FeatureName);
59  else if (Feature == "neon" || Feature == "noneon")
60  D.Diag(clang::diag::err_drv_no_neon_modifier);
61  else
62  return false;
63  }
64  return true;
65 }
66 
67 // Check if the CPU name and feature modifiers in -mcpu are legal. If yes,
68 // decode CPU and feature.
69 static bool DecodeAArch64Mcpu(const Driver &D, StringRef Mcpu, StringRef &CPU,
70  std::vector<StringRef> &Features) {
71  std::pair<StringRef, StringRef> Split = Mcpu.split("+");
72  CPU = Split.first;
73 
74  if (CPU == "generic") {
75  Features.push_back("+neon");
76  } else {
77  unsigned ArchKind = llvm::AArch64::parseCPUArch(CPU);
78  if (!llvm::AArch64::getArchFeatures(ArchKind, Features))
79  return false;
80 
81  unsigned Extension = llvm::AArch64::getDefaultExtensions(CPU, ArchKind);
82  if (!llvm::AArch64::getExtensionFeatures(Extension, Features))
83  return false;
84  }
85 
86  if (Split.second.size() && !DecodeAArch64Features(D, Split.second, Features))
87  return false;
88 
89  return true;
90 }
91 
92 static bool
93 getAArch64ArchFeaturesFromMarch(const Driver &D, StringRef March,
94  const ArgList &Args,
95  std::vector<StringRef> &Features) {
96  std::string MarchLowerCase = March.lower();
97  std::pair<StringRef, StringRef> Split = StringRef(MarchLowerCase).split("+");
98 
99  unsigned ArchKind = llvm::AArch64::parseArch(Split.first);
100  if (ArchKind == static_cast<unsigned>(llvm::AArch64::ArchKind::AK_INVALID) ||
101  !llvm::AArch64::getArchFeatures(ArchKind, Features) ||
102  (Split.second.size() && !DecodeAArch64Features(D, Split.second, Features)))
103  return false;
104 
105  return true;
106 }
107 
108 static bool
109 getAArch64ArchFeaturesFromMcpu(const Driver &D, StringRef Mcpu,
110  const ArgList &Args,
111  std::vector<StringRef> &Features) {
112  StringRef CPU;
113  std::string McpuLowerCase = Mcpu.lower();
114  if (!DecodeAArch64Mcpu(D, McpuLowerCase, CPU, Features))
115  return false;
116 
117  return true;
118 }
119 
120 static bool
122  const ArgList &Args,
123  std::vector<StringRef> &Features) {
124  std::string MtuneLowerCase = Mtune.lower();
125  // Handle CPU name is 'native'.
126  if (MtuneLowerCase == "native")
127  MtuneLowerCase = llvm::sys::getHostCPUName();
128  if (MtuneLowerCase == "cyclone") {
129  Features.push_back("+zcm");
130  Features.push_back("+zcz");
131  }
132  return true;
133 }
134 
135 static bool
137  const ArgList &Args,
138  std::vector<StringRef> &Features) {
139  StringRef CPU;
140  std::vector<StringRef> DecodedFeature;
141  std::string McpuLowerCase = Mcpu.lower();
142  if (!DecodeAArch64Mcpu(D, McpuLowerCase, CPU, DecodedFeature))
143  return false;
144 
145  return getAArch64MicroArchFeaturesFromMtune(D, CPU, Args, Features);
146 }
147 
148 void aarch64::getAArch64TargetFeatures(const Driver &D, const ArgList &Args,
149  std::vector<StringRef> &Features) {
150  Arg *A;
151  bool success = true;
152  // Enable NEON by default.
153  Features.push_back("+neon");
154  if ((A = Args.getLastArg(options::OPT_march_EQ)))
155  success = getAArch64ArchFeaturesFromMarch(D, A->getValue(), Args, Features);
156  else if ((A = Args.getLastArg(options::OPT_mcpu_EQ)))
157  success = getAArch64ArchFeaturesFromMcpu(D, A->getValue(), Args, Features);
158  else if (Args.hasArg(options::OPT_arch))
160  Args, Features);
161 
162  if (success && (A = Args.getLastArg(clang::driver::options::OPT_mtune_EQ)))
163  success =
164  getAArch64MicroArchFeaturesFromMtune(D, A->getValue(), Args, Features);
165  else if (success && (A = Args.getLastArg(options::OPT_mcpu_EQ)))
166  success =
167  getAArch64MicroArchFeaturesFromMcpu(D, A->getValue(), Args, Features);
168  else if (success && Args.hasArg(options::OPT_arch))
170  D, getAArch64TargetCPU(Args, A), Args, Features);
171 
172  if (!success)
173  D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
174 
175  if (Args.getLastArg(options::OPT_mgeneral_regs_only)) {
176  Features.push_back("-fp-armv8");
177  Features.push_back("-crypto");
178  Features.push_back("-neon");
179  }
180 
181  // En/disable crc
182  if (Arg *A = Args.getLastArg(options::OPT_mcrc, options::OPT_mnocrc)) {
183  if (A->getOption().matches(options::OPT_mcrc))
184  Features.push_back("+crc");
185  else
186  Features.push_back("-crc");
187  }
188 
189  if (Arg *A = Args.getLastArg(options::OPT_mno_unaligned_access,
190  options::OPT_munaligned_access))
191  if (A->getOption().matches(options::OPT_mno_unaligned_access))
192  Features.push_back("+strict-align");
193 
194  if (Args.hasArg(options::OPT_ffixed_x18))
195  Features.push_back("+reserve-x18");
196 
197  if (Args.hasArg(options::OPT_mno_neg_immediates))
198  Features.push_back("+no-neg-immediates");
199 }
static bool getAArch64ArchFeaturesFromMarch(const Driver &D, StringRef March, const ArgList &Args, std::vector< StringRef > &Features)
Definition: AArch64.cpp:93
DiagnosticBuilder Diag(unsigned DiagID) const
Definition: Driver.h:116
std::string getAArch64TargetCPU(const llvm::opt::ArgList &Args, llvm::opt::Arg *&A)
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:65
static bool DecodeAArch64Features(const Driver &D, StringRef text, std::vector< StringRef > &Features)
Definition: AArch64.cpp:50
static bool DecodeAArch64Mcpu(const Driver &D, StringRef Mcpu, StringRef &CPU, std::vector< StringRef > &Features)
Definition: AArch64.cpp:69
static bool getAArch64MicroArchFeaturesFromMcpu(const Driver &D, StringRef Mcpu, const ArgList &Args, std::vector< StringRef > &Features)
Definition: AArch64.cpp:136
void getAArch64TargetFeatures(const Driver &D, const llvm::opt::ArgList &Args, std::vector< llvm::StringRef > &Features)
static bool getAArch64ArchFeaturesFromMcpu(const Driver &D, StringRef Mcpu, const ArgList &Args, std::vector< StringRef > &Features)
Definition: AArch64.cpp:109
static bool getAArch64MicroArchFeaturesFromMtune(const Driver &D, StringRef Mtune, const ArgList &Args, std::vector< StringRef > &Features)
Definition: AArch64.cpp:121