xref: /llvm-project/llvm/utils/TableGen/Common/CodeGenTarget.cpp (revision bdf02249e7f8f95177ff58c881caf219699acb98)
1 //===- CodeGenTarget.cpp - CodeGen Target Class Wrapper -------------------===//
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 // This class wraps target description classes used by the various code
10 // generation TableGen backends.  This makes it easier to access the data and
11 // provides a single place that needs to check it for validity.  All of these
12 // classes abort on error conditions.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "CodeGenTarget.h"
17 #include "CodeGenInstruction.h"
18 #include "CodeGenRegisters.h"
19 #include "CodeGenSchedule.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/Twine.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/TableGen/Error.h"
25 #include "llvm/TableGen/Record.h"
26 #include <algorithm>
27 #include <iterator>
28 #include <tuple>
29 using namespace llvm;
30 
31 cl::OptionCategory AsmParserCat("Options for -gen-asm-parser");
32 cl::OptionCategory AsmWriterCat("Options for -gen-asm-writer");
33 
34 static cl::opt<unsigned>
35     AsmParserNum("asmparsernum", cl::init(0),
36                  cl::desc("Make -gen-asm-parser emit assembly parser #N"),
37                  cl::cat(AsmParserCat));
38 
39 static cl::opt<unsigned>
40     AsmWriterNum("asmwriternum", cl::init(0),
41                  cl::desc("Make -gen-asm-writer emit assembly writer #N"),
42                  cl::cat(AsmWriterCat));
43 
44 /// getValueType - Return the MVT::SimpleValueType that the specified TableGen
45 /// record corresponds to.
46 MVT::SimpleValueType llvm::getValueType(const Record *Rec) {
47   return (MVT::SimpleValueType)Rec->getValueAsInt("Value");
48 }
49 
50 StringRef llvm::getName(MVT::SimpleValueType T) {
51   switch (T) {
52   case MVT::Other:
53     return "UNKNOWN";
54   case MVT::iPTR:
55     return "TLI.getPointerTy()";
56   case MVT::iPTRAny:
57     return "TLI.getPointerTy()";
58   default:
59     return getEnumName(T);
60   }
61 }
62 
63 StringRef llvm::getEnumName(MVT::SimpleValueType T) {
64   // clang-format off
65   switch (T) {
66 #define GET_VT_ATTR(Ty, N, Sz, Any, Int, FP, Vec, Sc, Tup, NF, NElem, EltTy)   \
67   case MVT::Ty: return "MVT::" # Ty;
68 #include "llvm/CodeGen/GenVT.inc"
69   default: llvm_unreachable("ILLEGAL VALUE TYPE!");
70   }
71   // clang-format on
72 }
73 
74 /// getQualifiedName - Return the name of the specified record, with a
75 /// namespace qualifier if the record contains one.
76 ///
77 std::string llvm::getQualifiedName(const Record *R) {
78   std::string Namespace;
79   if (R->getValue("Namespace"))
80     Namespace = std::string(R->getValueAsString("Namespace"));
81   if (Namespace.empty())
82     return std::string(R->getName());
83   return Namespace + "::" + R->getName().str();
84 }
85 
86 /// getTarget - Return the current instance of the Target class.
87 ///
88 CodeGenTarget::CodeGenTarget(RecordKeeper &records)
89     : Records(records), CGH(records), Intrinsics(records) {
90   std::vector<Record *> Targets = Records.getAllDerivedDefinitions("Target");
91   if (Targets.size() == 0)
92     PrintFatalError("No 'Target' subclasses defined!");
93   if (Targets.size() != 1)
94     PrintFatalError("Multiple subclasses of Target defined!");
95   TargetRec = Targets[0];
96   MacroFusions = Records.getAllDerivedDefinitions("Fusion");
97 }
98 
99 CodeGenTarget::~CodeGenTarget() {}
100 
101 StringRef CodeGenTarget::getName() const { return TargetRec->getName(); }
102 
103 /// getInstNamespace - Find and return the target machine's instruction
104 /// namespace. The namespace is cached because it is requested multiple times.
105 StringRef CodeGenTarget::getInstNamespace() const {
106   if (InstNamespace.empty()) {
107     for (const CodeGenInstruction *Inst : getInstructionsByEnumValue()) {
108       // We are not interested in the "TargetOpcode" namespace.
109       if (Inst->Namespace != "TargetOpcode") {
110         InstNamespace = Inst->Namespace;
111         break;
112       }
113     }
114   }
115 
116   return InstNamespace;
117 }
118 
119 StringRef CodeGenTarget::getRegNamespace() const {
120   auto &RegClasses = RegBank->getRegClasses();
121   return RegClasses.size() > 0 ? RegClasses.front().Namespace : "";
122 }
123 
124 Record *CodeGenTarget::getInstructionSet() const {
125   return TargetRec->getValueAsDef("InstructionSet");
126 }
127 
128 bool CodeGenTarget::getAllowRegisterRenaming() const {
129   return TargetRec->getValueAsInt("AllowRegisterRenaming");
130 }
131 
132 /// getAsmParser - Return the AssemblyParser definition for this target.
133 ///
134 Record *CodeGenTarget::getAsmParser() const {
135   std::vector<Record *> LI = TargetRec->getValueAsListOfDefs("AssemblyParsers");
136   if (AsmParserNum >= LI.size())
137     PrintFatalError("Target does not have an AsmParser #" +
138                     Twine(AsmParserNum) + "!");
139   return LI[AsmParserNum];
140 }
141 
142 /// getAsmParserVariant - Return the AssemblyParserVariant definition for
143 /// this target.
144 ///
145 Record *CodeGenTarget::getAsmParserVariant(unsigned i) const {
146   std::vector<Record *> LI =
147       TargetRec->getValueAsListOfDefs("AssemblyParserVariants");
148   if (i >= LI.size())
149     PrintFatalError("Target does not have an AsmParserVariant #" + Twine(i) +
150                     "!");
151   return LI[i];
152 }
153 
154 /// getAsmParserVariantCount - Return the AssemblyParserVariant definition
155 /// available for this target.
156 ///
157 unsigned CodeGenTarget::getAsmParserVariantCount() const {
158   std::vector<Record *> LI =
159       TargetRec->getValueAsListOfDefs("AssemblyParserVariants");
160   return LI.size();
161 }
162 
163 /// getAsmWriter - Return the AssemblyWriter definition for this target.
164 ///
165 Record *CodeGenTarget::getAsmWriter() const {
166   std::vector<Record *> LI = TargetRec->getValueAsListOfDefs("AssemblyWriters");
167   if (AsmWriterNum >= LI.size())
168     PrintFatalError("Target does not have an AsmWriter #" +
169                     Twine(AsmWriterNum) + "!");
170   return LI[AsmWriterNum];
171 }
172 
173 CodeGenRegBank &CodeGenTarget::getRegBank() const {
174   if (!RegBank)
175     RegBank = std::make_unique<CodeGenRegBank>(Records, getHwModes());
176   return *RegBank;
177 }
178 
179 std::optional<CodeGenRegisterClass *> CodeGenTarget::getSuperRegForSubReg(
180     const ValueTypeByHwMode &ValueTy, CodeGenRegBank &RegBank,
181     const CodeGenSubRegIndex *SubIdx, bool MustBeAllocatable) const {
182   std::vector<CodeGenRegisterClass *> Candidates;
183   auto &RegClasses = RegBank.getRegClasses();
184 
185   // Try to find a register class which supports ValueTy, and also contains
186   // SubIdx.
187   for (CodeGenRegisterClass &RC : RegClasses) {
188     // Is there a subclass of this class which contains this subregister index?
189     CodeGenRegisterClass *SubClassWithSubReg = RC.getSubClassWithSubReg(SubIdx);
190     if (!SubClassWithSubReg)
191       continue;
192 
193     // We have a class. Check if it supports this value type.
194     if (!llvm::is_contained(SubClassWithSubReg->VTs, ValueTy))
195       continue;
196 
197     // If necessary, check that it is allocatable.
198     if (MustBeAllocatable && !SubClassWithSubReg->Allocatable)
199       continue;
200 
201     // We have a register class which supports both the value type and
202     // subregister index. Remember it.
203     Candidates.push_back(SubClassWithSubReg);
204   }
205 
206   // If we didn't find anything, we're done.
207   if (Candidates.empty())
208     return std::nullopt;
209 
210   // Find and return the largest of our candidate classes.
211   llvm::stable_sort(Candidates, [&](const CodeGenRegisterClass *A,
212                                     const CodeGenRegisterClass *B) {
213     if (A->getMembers().size() > B->getMembers().size())
214       return true;
215 
216     if (A->getMembers().size() < B->getMembers().size())
217       return false;
218 
219     // Order by name as a tie-breaker.
220     return StringRef(A->getName()) < B->getName();
221   });
222 
223   return Candidates[0];
224 }
225 
226 void CodeGenTarget::ReadRegAltNameIndices() const {
227   RegAltNameIndices = Records.getAllDerivedDefinitions("RegAltNameIndex");
228   llvm::sort(RegAltNameIndices, LessRecord());
229 }
230 
231 /// getRegisterByName - If there is a register with the specific AsmName,
232 /// return it.
233 const CodeGenRegister *CodeGenTarget::getRegisterByName(StringRef Name) const {
234   return getRegBank().getRegistersByName().lookup(Name);
235 }
236 
237 const CodeGenRegisterClass &
238 CodeGenTarget::getRegisterClass(const Record *R) const {
239   return *getRegBank().getRegClass(R);
240 }
241 
242 std::vector<ValueTypeByHwMode> CodeGenTarget::getRegisterVTs(Record *R) const {
243   const CodeGenRegister *Reg = getRegBank().getReg(R);
244   std::vector<ValueTypeByHwMode> Result;
245   for (const auto &RC : getRegBank().getRegClasses()) {
246     if (RC.contains(Reg)) {
247       ArrayRef<ValueTypeByHwMode> InVTs = RC.getValueTypes();
248       llvm::append_range(Result, InVTs);
249     }
250   }
251 
252   // Remove duplicates.
253   llvm::sort(Result);
254   Result.erase(llvm::unique(Result), Result.end());
255   return Result;
256 }
257 
258 void CodeGenTarget::ReadLegalValueTypes() const {
259   for (const auto &RC : getRegBank().getRegClasses())
260     llvm::append_range(LegalValueTypes, RC.VTs);
261 
262   // Remove duplicates.
263   llvm::sort(LegalValueTypes);
264   LegalValueTypes.erase(llvm::unique(LegalValueTypes), LegalValueTypes.end());
265 }
266 
267 CodeGenSchedModels &CodeGenTarget::getSchedModels() const {
268   if (!SchedModels)
269     SchedModels = std::make_unique<CodeGenSchedModels>(Records, *this);
270   return *SchedModels;
271 }
272 
273 void CodeGenTarget::ReadInstructions() const {
274   std::vector<Record *> Insts = Records.getAllDerivedDefinitions("Instruction");
275   if (Insts.size() <= 2)
276     PrintFatalError("No 'Instruction' subclasses defined!");
277 
278   // Parse the instructions defined in the .td file.
279   for (Record *R : Insts) {
280     Instructions[R] = std::make_unique<CodeGenInstruction>(R);
281     if (Instructions[R]->isVariableLengthEncoding())
282       HasVariableLengthEncodings = true;
283   }
284 }
285 
286 static const CodeGenInstruction *GetInstByName(
287     const char *Name,
288     const DenseMap<const Record *, std::unique_ptr<CodeGenInstruction>> &Insts,
289     RecordKeeper &Records) {
290   const Record *Rec = Records.getDef(Name);
291 
292   const auto I = Insts.find(Rec);
293   if (!Rec || I == Insts.end())
294     PrintFatalError(Twine("Could not find '") + Name + "' instruction!");
295   return I->second.get();
296 }
297 
298 static const char *FixedInstrs[] = {
299 #define HANDLE_TARGET_OPCODE(OPC) #OPC,
300 #include "llvm/Support/TargetOpcodes.def"
301     nullptr};
302 
303 unsigned CodeGenTarget::getNumFixedInstructions() {
304   return std::size(FixedInstrs) - 1;
305 }
306 
307 /// Return all of the instructions defined by the target, ordered by
308 /// their enum value.
309 void CodeGenTarget::ComputeInstrsByEnum() const {
310   const auto &Insts = getInstructions();
311   for (const char *const *p = FixedInstrs; *p; ++p) {
312     const CodeGenInstruction *Instr = GetInstByName(*p, Insts, Records);
313     assert(Instr && "Missing target independent instruction");
314     assert(Instr->Namespace == "TargetOpcode" && "Bad namespace");
315     InstrsByEnum.push_back(Instr);
316   }
317   unsigned EndOfPredefines = InstrsByEnum.size();
318   assert(EndOfPredefines == getNumFixedInstructions() &&
319          "Missing generic opcode");
320 
321   for (const auto &I : Insts) {
322     const CodeGenInstruction *CGI = I.second.get();
323     if (CGI->Namespace != "TargetOpcode") {
324       InstrsByEnum.push_back(CGI);
325       if (CGI->TheDef->getValueAsBit("isPseudo"))
326         ++NumPseudoInstructions;
327     }
328   }
329 
330   assert(InstrsByEnum.size() == Insts.size() && "Missing predefined instr");
331 
332   // All of the instructions are now in random order based on the map iteration.
333   llvm::sort(
334       InstrsByEnum.begin() + EndOfPredefines, InstrsByEnum.end(),
335       [](const CodeGenInstruction *Rec1, const CodeGenInstruction *Rec2) {
336         const auto &D1 = *Rec1->TheDef;
337         const auto &D2 = *Rec2->TheDef;
338         return std::tuple(!D1.getValueAsBit("isPseudo"), D1.getName()) <
339                std::tuple(!D2.getValueAsBit("isPseudo"), D2.getName());
340       });
341 
342   // Assign an enum value to each instruction according to the sorted order.
343   unsigned Num = 0;
344   for (const CodeGenInstruction *Inst : InstrsByEnum)
345     Inst->EnumVal = Num++;
346 }
347 
348 /// isLittleEndianEncoding - Return whether this target encodes its instruction
349 /// in little-endian format, i.e. bits laid out in the order [0..n]
350 ///
351 bool CodeGenTarget::isLittleEndianEncoding() const {
352   return getInstructionSet()->getValueAsBit("isLittleEndianEncoding");
353 }
354 
355 /// reverseBitsForLittleEndianEncoding - For little-endian instruction bit
356 /// encodings, reverse the bit order of all instructions.
357 void CodeGenTarget::reverseBitsForLittleEndianEncoding() {
358   if (!isLittleEndianEncoding())
359     return;
360 
361   std::vector<Record *> Insts =
362       Records.getAllDerivedDefinitions("InstructionEncoding");
363   for (Record *R : Insts) {
364     if (R->getValueAsString("Namespace") == "TargetOpcode" ||
365         R->getValueAsBit("isPseudo"))
366       continue;
367 
368     BitsInit *BI = R->getValueAsBitsInit("Inst");
369 
370     unsigned numBits = BI->getNumBits();
371 
372     SmallVector<Init *, 16> NewBits(numBits);
373 
374     for (unsigned bit = 0, end = numBits / 2; bit != end; ++bit) {
375       unsigned bitSwapIdx = numBits - bit - 1;
376       Init *OrigBit = BI->getBit(bit);
377       Init *BitSwap = BI->getBit(bitSwapIdx);
378       NewBits[bit] = BitSwap;
379       NewBits[bitSwapIdx] = OrigBit;
380     }
381     if (numBits % 2) {
382       unsigned middle = (numBits + 1) / 2;
383       NewBits[middle] = BI->getBit(middle);
384     }
385 
386     BitsInit *NewBI = BitsInit::get(Records, NewBits);
387 
388     // Update the bits in reversed order so that emitInstrOpBits will get the
389     // correct endianness.
390     R->getValue("Inst")->setValue(NewBI);
391   }
392 }
393 
394 /// guessInstructionProperties - Return true if it's OK to guess instruction
395 /// properties instead of raising an error.
396 ///
397 /// This is configurable as a temporary migration aid. It will eventually be
398 /// permanently false.
399 bool CodeGenTarget::guessInstructionProperties() const {
400   return getInstructionSet()->getValueAsBit("guessInstructionProperties");
401 }
402 
403 //===----------------------------------------------------------------------===//
404 // ComplexPattern implementation
405 //
406 ComplexPattern::ComplexPattern(Record *R) {
407   Ty = R->getValueAsDef("Ty");
408   NumOperands = R->getValueAsInt("NumOperands");
409   SelectFunc = std::string(R->getValueAsString("SelectFunc"));
410   RootNodes = R->getValueAsListOfDefs("RootNodes");
411 
412   // FIXME: This is a hack to statically increase the priority of patterns which
413   // maps a sub-dag to a complex pattern. e.g. favors LEA over ADD. To get best
414   // possible pattern match we'll need to dynamically calculate the complexity
415   // of all patterns a dag can potentially map to.
416   int64_t RawComplexity = R->getValueAsInt("Complexity");
417   if (RawComplexity == -1)
418     Complexity = NumOperands * 3;
419   else
420     Complexity = RawComplexity;
421 
422   // FIXME: Why is this different from parseSDPatternOperatorProperties?
423   // Parse the properties.
424   Properties = 0;
425   std::vector<Record *> PropList = R->getValueAsListOfDefs("Properties");
426   for (unsigned i = 0, e = PropList.size(); i != e; ++i)
427     if (PropList[i]->getName() == "SDNPHasChain") {
428       Properties |= 1 << SDNPHasChain;
429     } else if (PropList[i]->getName() == "SDNPOptInGlue") {
430       Properties |= 1 << SDNPOptInGlue;
431     } else if (PropList[i]->getName() == "SDNPMayStore") {
432       Properties |= 1 << SDNPMayStore;
433     } else if (PropList[i]->getName() == "SDNPMayLoad") {
434       Properties |= 1 << SDNPMayLoad;
435     } else if (PropList[i]->getName() == "SDNPSideEffect") {
436       Properties |= 1 << SDNPSideEffect;
437     } else if (PropList[i]->getName() == "SDNPMemOperand") {
438       Properties |= 1 << SDNPMemOperand;
439     } else if (PropList[i]->getName() == "SDNPVariadic") {
440       Properties |= 1 << SDNPVariadic;
441     } else if (PropList[i]->getName() == "SDNPWantRoot") {
442       Properties |= 1 << SDNPWantRoot;
443     } else if (PropList[i]->getName() == "SDNPWantParent") {
444       Properties |= 1 << SDNPWantParent;
445     } else {
446       PrintFatalError(R->getLoc(), "Unsupported SD Node property '" +
447                                        PropList[i]->getName() +
448                                        "' on ComplexPattern '" + R->getName() +
449                                        "'!");
450     }
451 }
452