aboutsummaryrefslogtreecommitdiff
path: root/llvm/utils/TableGen/Basic/TargetFeaturesEmitter.cpp
blob: 6b723bc0fb02d985c4b899a5bd87ca1e9d9a5aad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//===- TargetFeaturesEmitter.cpp - Generate CPU Target feature ----===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
//  This tablegen backend exports cpu target features
//  and cpu sub-type.
//
//===----------------------------------------------------------------------===//

#include "TargetFeaturesEmitter.h"
#include "llvm/TableGen/Error.h"
#include "llvm/TableGen/TableGenBackend.h"
#include "llvm/TargetParser/SubtargetFeature.h"

using namespace llvm;

using FeatureMapTy = DenseMap<const Record *, unsigned>;
using ConstRecVec = std::vector<const Record *>;

TargetFeaturesEmitter::TargetFeaturesEmitter(const RecordKeeper &R)
    : Records(R) {
  ArrayRef<const Record *> Targets = Records.getAllDerivedDefinitions("Target");
  if (Targets.size() == 0)
    PrintFatalError("No 'Target' subclasses defined!");
  if (Targets.size() != 1)
    PrintFatalError("Multiple subclasses of Target defined!");
  Target = Targets[0]->getName();
}

FeatureMapTy TargetFeaturesEmitter::enumeration(raw_ostream &OS) {
  ArrayRef<const Record *> DefList =
      Records.getAllDerivedDefinitions("SubtargetFeature");

  unsigned N = DefList.size();
  if (N == 0)
    return FeatureMapTy();

  if (N + 1 > MAX_SUBTARGET_FEATURES)
    PrintFatalError(
        "Too many subtarget features! Bump MAX_SUBTARGET_FEATURES.");

  OS << "namespace " << Target << " {\n";

  OS << "enum {\n";

  FeatureMapTy FeatureMap;
  for (unsigned I = 0; I < N; ++I) {
    const Record *Def = DefList[I];
    // Print the Feature Name.
    OS << "  " << Def->getName() << " = " << I << ",\n";

    FeatureMap[Def] = I;
  }

  OS << "  " << "NumSubtargetFeatures = " << N << "\n";

  // Close enumeration and namespace
  OS << "};\n";
  OS << "} // end namespace " << Target << "\n";
  return FeatureMap;
}

void TargetFeaturesEmitter::printFeatureMask(
    raw_ostream &OS, ArrayRef<const Record *> FeatureList,
    const FeatureMapTy &FeatureMap) {
  std::array<uint64_t, MAX_SUBTARGET_WORDS> Mask = {};
  for (const Record *Feature : FeatureList) {
    unsigned Bit = FeatureMap.lookup(Feature);
    Mask[Bit / 64] |= 1ULL << (Bit % 64);
  }

  OS << "{ { { ";
  for (unsigned I = 0; I != Mask.size(); ++I) {
    OS << "0x";
    OS.write_hex(Mask[I]);
    OS << "ULL, ";
  }
  OS << "} } }";
}

void TargetFeaturesEmitter::printFeatureKeyValues(
    raw_ostream &OS, const FeatureMapTy &FeatureMap) {
  std::vector<const Record *> FeatureList =
      Records.getAllDerivedDefinitions("SubtargetFeature");

  // Remove features with empty name.
  llvm::erase_if(FeatureList, [](const Record *Rec) {
    return Rec->getValueAsString("Name").empty();
  });

  if (FeatureList.empty())
    return;

  llvm::sort(FeatureList, LessRecordFieldName());

  // Begin feature table.
  OS << "// Sorted (by key) array of values for CPU features.\n"
     << "extern const llvm::BasicSubtargetFeatureKV " << "Basic" << Target
     << "FeatureKV[] = {\n";

  for (const Record *Feature : FeatureList) {
    StringRef Name = Feature->getName();
    StringRef ValueName = Feature->getValueAsString("Name");

    OS << "  { " << "\"" << ValueName << "\", " << Target << "::" << Name
       << ", ";

    ConstRecVec ImpliesList = Feature->getValueAsListOfDefs("Implies");

    printFeatureMask(OS, ImpliesList, FeatureMap);

    OS << " },\n";
  }

  // End feature table.
  OS << "};\n";
}

void TargetFeaturesEmitter::printCPUKeyValues(raw_ostream &OS,
                                              const FeatureMapTy &FeatureMap) {
  // Gather and sort processor information
  std::vector<const Record *> ProcessorList =
      Records.getAllDerivedDefinitions("Processor");
  llvm::sort(ProcessorList, LessRecordFieldName());

  // Begin processor table.
  OS << "// Sorted (by key) array of values for CPU subtype.\n"
     << "extern const llvm::BasicSubtargetSubTypeKV " << "Basic" << Target
     << "SubTypeKV[] = {\n";

  for (const Record *Processor : ProcessorList) {
    StringRef Name = Processor->getValueAsString("Name");
    ConstRecVec FeatureList = Processor->getValueAsListOfDefs("Features");

    OS << " { " << "\"" << Name << "\", ";

    printFeatureMask(OS, FeatureList, FeatureMap);
    OS << " },\n";
  }

  // End processor table.
  OS << "};\n";
}

void TargetFeaturesEmitter::run(raw_ostream &OS) {
  OS << "// Autogenerated by TargetFeatureEmitter.cpp\n\n";

  OS << "\n#ifdef GET_SUBTARGETFEATURES_ENUM\n";
  OS << "#undef GET_SUBTARGETFEATURES_ENUM\n\n";

  OS << "namespace llvm {\n";
  auto FeatureMap = enumeration(OS);
  OS << "} // end namespace llvm\n\n";
  OS << "#endif // GET_SUBTARGETFEATURES_ENUM\n\n";

  OS << "\n#ifdef GET_SUBTARGETFEATURES_KV\n";
  OS << "#undef GET_SUBTARGETFEATURES_KV\n\n";

  OS << "namespace llvm {\n";
  printFeatureKeyValues(OS, FeatureMap);
  OS << "\n";

  printCPUKeyValues(OS, FeatureMap);
  OS << "\n";
  OS << "} // end namespace llvm\n\n";
  OS << "#endif // GET_SUBTARGETFEATURES_KV\n\n";
}

static TableGen::Emitter::OptClass<TargetFeaturesEmitter>
    X("gen-target-features", "Generate subtarget enumerations");