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(const RecordKeeper &records) 89 : Records(records), CGH(records), Intrinsics(records) { 90 ArrayRef<const 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 /// getRegisterByName - If there is a register with the specific AsmName, 227 /// return it. 228 const CodeGenRegister *CodeGenTarget::getRegisterByName(StringRef Name) const { 229 return getRegBank().getRegistersByName().lookup(Name); 230 } 231 232 const CodeGenRegisterClass & 233 CodeGenTarget::getRegisterClass(const Record *R) const { 234 return *getRegBank().getRegClass(R); 235 } 236 237 std::vector<ValueTypeByHwMode> 238 CodeGenTarget::getRegisterVTs(const Record *R) const { 239 const CodeGenRegister *Reg = getRegBank().getReg(R); 240 std::vector<ValueTypeByHwMode> Result; 241 for (const auto &RC : getRegBank().getRegClasses()) { 242 if (RC.contains(Reg)) { 243 ArrayRef<ValueTypeByHwMode> InVTs = RC.getValueTypes(); 244 llvm::append_range(Result, InVTs); 245 } 246 } 247 248 // Remove duplicates. 249 llvm::sort(Result); 250 Result.erase(llvm::unique(Result), Result.end()); 251 return Result; 252 } 253 254 void CodeGenTarget::ReadLegalValueTypes() const { 255 for (const auto &RC : getRegBank().getRegClasses()) 256 llvm::append_range(LegalValueTypes, RC.VTs); 257 258 // Remove duplicates. 259 llvm::sort(LegalValueTypes); 260 LegalValueTypes.erase(llvm::unique(LegalValueTypes), LegalValueTypes.end()); 261 } 262 263 CodeGenSchedModels &CodeGenTarget::getSchedModels() const { 264 if (!SchedModels) 265 SchedModels = std::make_unique<CodeGenSchedModels>(Records, *this); 266 return *SchedModels; 267 } 268 269 void CodeGenTarget::ReadInstructions() const { 270 ArrayRef<const Record *> Insts = 271 Records.getAllDerivedDefinitions("Instruction"); 272 if (Insts.size() <= 2) 273 PrintFatalError("No 'Instruction' subclasses defined!"); 274 275 // Parse the instructions defined in the .td file. 276 for (const Record *R : Insts) { 277 Instructions[R] = std::make_unique<CodeGenInstruction>(R); 278 if (Instructions[R]->isVariableLengthEncoding()) 279 HasVariableLengthEncodings = true; 280 } 281 } 282 283 static const CodeGenInstruction *GetInstByName( 284 const char *Name, 285 const DenseMap<const Record *, std::unique_ptr<CodeGenInstruction>> &Insts, 286 const RecordKeeper &Records) { 287 const Record *Rec = Records.getDef(Name); 288 289 const auto I = Insts.find(Rec); 290 if (!Rec || I == Insts.end()) 291 PrintFatalError(Twine("Could not find '") + Name + "' instruction!"); 292 return I->second.get(); 293 } 294 295 static const char *FixedInstrs[] = { 296 #define HANDLE_TARGET_OPCODE(OPC) #OPC, 297 #include "llvm/Support/TargetOpcodes.def" 298 nullptr}; 299 300 unsigned CodeGenTarget::getNumFixedInstructions() { 301 return std::size(FixedInstrs) - 1; 302 } 303 304 /// Return all of the instructions defined by the target, ordered by 305 /// their enum value. 306 void CodeGenTarget::ComputeInstrsByEnum() const { 307 const auto &Insts = getInstructions(); 308 for (const char *const *p = FixedInstrs; *p; ++p) { 309 const CodeGenInstruction *Instr = GetInstByName(*p, Insts, Records); 310 assert(Instr && "Missing target independent instruction"); 311 assert(Instr->Namespace == "TargetOpcode" && "Bad namespace"); 312 InstrsByEnum.push_back(Instr); 313 } 314 unsigned EndOfPredefines = InstrsByEnum.size(); 315 assert(EndOfPredefines == getNumFixedInstructions() && 316 "Missing generic opcode"); 317 318 for (const auto &I : Insts) { 319 const CodeGenInstruction *CGI = I.second.get(); 320 if (CGI->Namespace != "TargetOpcode") { 321 InstrsByEnum.push_back(CGI); 322 if (CGI->TheDef->getValueAsBit("isPseudo")) 323 ++NumPseudoInstructions; 324 } 325 } 326 327 assert(InstrsByEnum.size() == Insts.size() && "Missing predefined instr"); 328 329 // All of the instructions are now in random order based on the map iteration. 330 llvm::sort( 331 InstrsByEnum.begin() + EndOfPredefines, InstrsByEnum.end(), 332 [](const CodeGenInstruction *Rec1, const CodeGenInstruction *Rec2) { 333 const auto &D1 = *Rec1->TheDef; 334 const auto &D2 = *Rec2->TheDef; 335 return std::tuple(!D1.getValueAsBit("isPseudo"), D1.getName()) < 336 std::tuple(!D2.getValueAsBit("isPseudo"), D2.getName()); 337 }); 338 339 // Assign an enum value to each instruction according to the sorted order. 340 unsigned Num = 0; 341 for (const CodeGenInstruction *Inst : InstrsByEnum) 342 Inst->EnumVal = Num++; 343 } 344 345 /// isLittleEndianEncoding - Return whether this target encodes its instruction 346 /// in little-endian format, i.e. bits laid out in the order [0..n] 347 /// 348 bool CodeGenTarget::isLittleEndianEncoding() const { 349 return getInstructionSet()->getValueAsBit("isLittleEndianEncoding"); 350 } 351 352 /// reverseBitsForLittleEndianEncoding - For little-endian instruction bit 353 /// encodings, reverse the bit order of all instructions. 354 void CodeGenTarget::reverseBitsForLittleEndianEncoding() { 355 if (!isLittleEndianEncoding()) 356 return; 357 358 for (const Record *R : 359 Records.getAllDerivedDefinitions("InstructionEncoding")) { 360 if (R->getValueAsString("Namespace") == "TargetOpcode" || 361 R->getValueAsBit("isPseudo")) 362 continue; 363 364 BitsInit *BI = R->getValueAsBitsInit("Inst"); 365 366 unsigned numBits = BI->getNumBits(); 367 368 SmallVector<Init *, 16> NewBits(numBits); 369 370 for (unsigned bit = 0, end = numBits / 2; bit != end; ++bit) { 371 unsigned bitSwapIdx = numBits - bit - 1; 372 Init *OrigBit = BI->getBit(bit); 373 Init *BitSwap = BI->getBit(bitSwapIdx); 374 NewBits[bit] = BitSwap; 375 NewBits[bitSwapIdx] = OrigBit; 376 } 377 if (numBits % 2) { 378 unsigned middle = (numBits + 1) / 2; 379 NewBits[middle] = BI->getBit(middle); 380 } 381 382 RecordKeeper &MutableRC = const_cast<RecordKeeper &>(Records); 383 BitsInit *NewBI = BitsInit::get(MutableRC, NewBits); 384 385 // Update the bits in reversed order so that emitters will get the correct 386 // endianness. 387 // FIXME: Eliminate mutation of TG records by creating a helper function 388 // to reverse bits and maintain a cache instead of mutating records. 389 Record *MutableR = const_cast<Record *>(R); 390 MutableR->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(const Record *R) { 407 Ty = R->getValueAsDef("Ty"); 408 NumOperands = R->getValueAsInt("NumOperands"); 409 SelectFunc = std::string(R->getValueAsString("SelectFunc")); 410 RootNodes = R->getValueAsListOfConstDefs("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