Coverage Report

Created: 2026-01-16 08:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/keystone/llvm/lib/MC/SubtargetFeature.cpp
Line
Count
Source
1
//===- SubtargetFeature.cpp - CPU characteristics Implementation ----------===//
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
// This file implements the SubtargetFeature interface.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "llvm/MC/SubtargetFeature.h"
15
#include "llvm/ADT/StringExtras.h"
16
#include "llvm/Support/Debug.h"
17
#include "llvm/Support/Format.h"
18
#include "llvm/Support/raw_ostream.h"
19
#include <algorithm>
20
#include <cassert>
21
#include <cctype>
22
#include <cstdlib>
23
using namespace llvm_ks;
24
25
//===----------------------------------------------------------------------===//
26
//                          Static Helper Functions
27
//===----------------------------------------------------------------------===//
28
29
/// hasFlag - Determine if a feature has a flag; '+' or '-'
30
///
31
1.38M
static inline bool hasFlag(StringRef Feature) {
32
1.38M
  assert(!Feature.empty() && "Empty string");
33
  // Get first character
34
1.38M
  char Ch = Feature[0];
35
  // Check if first character is '+' or '-' flag
36
1.38M
  return Ch == '+' || Ch =='-';
37
1.38M
}
38
39
/// StripFlag - Return string stripped of flag.
40
///
41
693k
static inline std::string StripFlag(StringRef Feature) {
42
693k
  return hasFlag(Feature) ? Feature.substr(1) : Feature;
43
693k
}
44
45
/// isEnabled - Return true if enable flag; '+'.
46
///
47
691k
static inline bool isEnabled(StringRef Feature) {
48
691k
  assert(!Feature.empty() && "Empty string");
49
  // Get first character
50
691k
  char Ch = Feature[0];
51
  // Check if first character is '+' for enabled
52
691k
  return Ch == '+';
53
691k
}
54
55
/// Split - Splits a string of comma separated items in to a vector of strings.
56
///
57
302k
static void Split(std::vector<std::string> &V, StringRef S) {
58
302k
  SmallVector<StringRef, 3> Tmp;
59
302k
  S.split(Tmp, ',', -1, false /* KeepEmpty */);
60
302k
  V.assign(Tmp.begin(), Tmp.end());
61
302k
}
62
63
/// Adding features.
64
0
void SubtargetFeatures::AddFeature(StringRef String, bool Enable) {
65
  // Don't add empty features.
66
0
  if (!String.empty())
67
    // Convert to lowercase, prepend flag if we don't already have a flag.
68
0
    Features.push_back(hasFlag(String) ? String.lower()
69
0
                                       : (Enable ? "+" : "-") + String.lower());
70
0
}
71
72
/// Find KV in array using binary search.
73
static const SubtargetFeatureKV *Find(StringRef S,
74
760k
                                      ArrayRef<SubtargetFeatureKV> A) {
75
  // Binary search the array
76
760k
  auto F = std::lower_bound(A.begin(), A.end(), S);
77
  // If not found then return NULL
78
760k
  if (F == A.end() || StringRef(F->Key) != S) return nullptr;
79
  // Return the found array item
80
760k
  return F;
81
760k
}
82
83
/// getLongestEntryLength - Return the length of the longest entry in the table.
84
///
85
0
static size_t getLongestEntryLength(ArrayRef<SubtargetFeatureKV> Table) {
86
0
  size_t MaxLen = 0;
87
0
  for (auto &I : Table)
88
0
    MaxLen = std::max(MaxLen, std::strlen(I.Key));
89
0
  return MaxLen;
90
0
}
91
92
/// Display help for feature choices.
93
///
94
static void Help(ArrayRef<SubtargetFeatureKV> CPUTable,
95
0
                 ArrayRef<SubtargetFeatureKV> FeatTable) {
96
  // Determine the length of the longest CPU and Feature entries.
97
0
  unsigned MaxCPULen  = getLongestEntryLength(CPUTable);
98
0
  unsigned MaxFeatLen = getLongestEntryLength(FeatTable);
99
100
  // Print the CPU table.
101
0
  errs() << "Available CPUs for this target:\n\n";
102
0
  for (auto &CPU : CPUTable)
103
0
    errs() << format("  %-*s - %s.\n", MaxCPULen, CPU.Key, CPU.Desc);
104
0
  errs() << '\n';
105
106
  // Print the Feature table.
107
0
  errs() << "Available features for this target:\n\n";
108
0
  for (auto &Feature : FeatTable)
109
0
    errs() << format("  %-*s - %s.\n", MaxFeatLen, Feature.Key, Feature.Desc);
110
0
  errs() << '\n';
111
112
0
  errs() << "Use +feature to enable a feature, or -feature to disable it.\n"
113
0
            "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
114
0
}
115
116
//===----------------------------------------------------------------------===//
117
//                    SubtargetFeatures Implementation
118
//===----------------------------------------------------------------------===//
119
120
302k
SubtargetFeatures::SubtargetFeatures(StringRef Initial) {
121
  // Break up string into separate features
122
302k
  Split(Features, Initial);
123
302k
}
124
125
126
0
std::string SubtargetFeatures::getString() const {
127
0
  return join(Features.begin(), Features.end(), ",");
128
0
}
129
130
/// SetImpliedBits - For each feature that is (transitively) implied by this
131
/// feature, set it.
132
///
133
static
134
void SetImpliedBits(FeatureBitset &Bits, const SubtargetFeatureKV *FeatureEntry,
135
6.82M
                    ArrayRef<SubtargetFeatureKV> FeatureTable) {
136
653M
  for (auto &FE : FeatureTable) {
137
653M
    if (FeatureEntry->Value == FE.Value) continue;
138
139
646M
    if ((FeatureEntry->Implies & FE.Value).any()) {
140
6.04M
      Bits |= FE.Value;
141
6.04M
      SetImpliedBits(Bits, &FE, FeatureTable);
142
6.04M
    }
143
646M
  }
144
6.82M
}
145
146
/// ClearImpliedBits - For each feature that (transitively) implies this
147
/// feature, clear it.
148
///
149
static
150
void ClearImpliedBits(FeatureBitset &Bits, 
151
                      const SubtargetFeatureKV *FeatureEntry,
152
2.01M
                      ArrayRef<SubtargetFeatureKV> FeatureTable) {
153
209M
  for (auto &FE : FeatureTable) {
154
209M
    if (FeatureEntry->Value == FE.Value) continue;
155
156
207M
    if ((FE.Implies & FeatureEntry->Value).any()) {
157
1.67M
      Bits &= ~FE.Value;
158
1.67M
      ClearImpliedBits(Bits, &FE, FeatureTable);
159
1.67M
    }
160
207M
  }
161
2.01M
}
162
163
/// ToggleFeature - Toggle a feature and update the feature bits.
164
void
165
SubtargetFeatures::ToggleFeature(FeatureBitset &Bits, StringRef Feature,
166
2.23k
                                 ArrayRef<SubtargetFeatureKV> FeatureTable) {
167
168
  // Find feature in table.
169
2.23k
  const SubtargetFeatureKV *FeatureEntry =
170
2.23k
      Find(StripFlag(Feature), FeatureTable);
171
  // If there is a match
172
2.23k
  if (FeatureEntry) {
173
2.23k
    if ((Bits & FeatureEntry->Value) == FeatureEntry->Value) {
174
152
      Bits &= ~FeatureEntry->Value;
175
      // For each feature that implies this, clear it.
176
152
      ClearImpliedBits(Bits, FeatureEntry, FeatureTable);
177
2.08k
    } else {
178
2.08k
      Bits |=  FeatureEntry->Value;
179
180
      // For each feature that this implies, set it.
181
2.08k
      SetImpliedBits(Bits, FeatureEntry, FeatureTable);
182
2.08k
    }
183
2.23k
  } else {
184
0
    errs() << "'" << Feature
185
0
           << "' is not a recognized feature for this target"
186
0
           << " (ignoring feature)\n";
187
0
  }
188
2.23k
}
189
190
void SubtargetFeatures::ApplyFeatureFlag(FeatureBitset &Bits, StringRef Feature,
191
691k
                                    ArrayRef<SubtargetFeatureKV> FeatureTable) {
192
193
691k
  assert(hasFlag(Feature));
194
195
  // Find feature in table.
196
691k
  const SubtargetFeatureKV *FeatureEntry =
197
691k
      Find(StripFlag(Feature), FeatureTable);
198
  // If there is a match
199
691k
  if (FeatureEntry) {
200
    // Enable/disable feature in bits
201
691k
    if (isEnabled(Feature)) {
202
352k
      Bits |= FeatureEntry->Value;
203
204
      // For each feature that this implies, set it.
205
352k
      SetImpliedBits(Bits, FeatureEntry, FeatureTable);
206
352k
    } else {
207
338k
      Bits &= ~FeatureEntry->Value;
208
209
      // For each feature that implies this, clear it.
210
338k
      ClearImpliedBits(Bits, FeatureEntry, FeatureTable);
211
338k
    }
212
691k
  } else {
213
0
    errs() << "'" << Feature
214
0
           << "' is not a recognized feature for this target"
215
0
           << " (ignoring feature)\n";
216
0
  }
217
691k
}
218
219
220
/// getFeatureBits - Get feature bits a CPU.
221
///
222
FeatureBitset
223
SubtargetFeatures::getFeatureBits(StringRef CPU,
224
                                  ArrayRef<SubtargetFeatureKV> CPUTable,
225
302k
                                  ArrayRef<SubtargetFeatureKV> FeatureTable) {
226
227
302k
  if (CPUTable.empty() || FeatureTable.empty())
228
0
    return FeatureBitset();
229
230
302k
#ifndef NDEBUG
231
302k
  assert(std::is_sorted(std::begin(CPUTable), std::end(CPUTable)) &&
232
302k
         "CPU table is not sorted");
233
302k
  assert(std::is_sorted(std::begin(FeatureTable), std::end(FeatureTable)) &&
234
302k
         "CPU features table is not sorted");
235
302k
#endif
236
  // Resulting bits
237
302k
  FeatureBitset Bits;
238
239
  // Check if help is needed
240
302k
  if (CPU == "help")
241
0
    Help(CPUTable, FeatureTable);
242
243
  // Find CPU entry if CPU name is specified.
244
302k
  else if (!CPU.empty()) {
245
66.5k
    const SubtargetFeatureKV *CPUEntry = Find(CPU, CPUTable);
246
247
    // If there is a match
248
66.5k
    if (CPUEntry) {
249
      // Set base feature bits
250
66.5k
      Bits = CPUEntry->Value;
251
252
      // Set the feature implied by this CPU feature, if any.
253
2.25M
      for (auto &FE : FeatureTable) {
254
2.25M
        if ((CPUEntry->Value & FE.Value).any())
255
425k
          SetImpliedBits(Bits, &FE, FeatureTable);
256
2.25M
      }
257
66.5k
    } else {
258
0
      errs() << "'" << CPU
259
0
             << "' is not a recognized processor for this target"
260
0
             << " (ignoring processor)\n";
261
0
    }
262
66.5k
  }
263
264
  // Iterate through each feature
265
343k
  for (auto &Feature : Features) {
266
    // Check for help
267
343k
    if (Feature == "+help")
268
0
      Help(CPUTable, FeatureTable);
269
270
343k
    ApplyFeatureFlag(Bits, Feature, FeatureTable);
271
343k
  }
272
273
302k
  return Bits;
274
302k
}
275
276
/// print - Print feature string.
277
///
278
0
void SubtargetFeatures::print(raw_ostream &OS) const {
279
0
  for (auto &F : Features)
280
0
    OS << F << " ";
281
0
  OS << "\n";
282
0
}
283
284
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
285
/// dump - Dump feature info.
286
///
287
0
LLVM_DUMP_METHOD void SubtargetFeatures::dump() const {
288
0
}
289
#endif
290
291
/// Adds the default features for the specified target triple.
292
///
293
/// FIXME: This is an inelegant way of specifying the features of a
294
/// subtarget. It would be better if we could encode this information
295
/// into the IR. See <rdar://5972456>.
296
///
297
0
void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
298
0
  if (Triple.getVendor() == Triple::Apple) {
299
0
    if (Triple.getArch() == Triple::ppc) {
300
      // powerpc-apple-*
301
0
      AddFeature("altivec");
302
0
    } else if (Triple.getArch() == Triple::ppc64) {
303
      // powerpc64-apple-*
304
0
      AddFeature("64bit");
305
0
      AddFeature("altivec");
306
0
    }
307
0
  }
308
0
}