1 //===- CodeGenTarget.cpp - CodeGen Target Class Wrapper -------------------===// 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 class wraps target description classes used by the various code 11 // generation TableGen backends. This makes it easier to access the data and 12 // provides a single place that needs to check it for validity. All of these 13 // classes throw exceptions on error conditions. 14 // 15 //===----------------------------------------------------------------------===// 16 17 #include "CodeGenTarget.h" 18 #include "CodeGenIntrinsics.h" 19 #include "llvm/TableGen/Record.h" 20 #include "llvm/ADT/StringExtras.h" 21 #include "llvm/ADT/STLExtras.h" 22 #include "llvm/Support/CommandLine.h" 23 #include <algorithm> 24 using namespace llvm; 25 26 static cl::opt<unsigned> 27 AsmParserNum("asmparsernum", cl::init(0), 28 cl::desc("Make -gen-asm-parser emit assembly parser #N")); 29 30 static cl::opt<unsigned> 31 AsmWriterNum("asmwriternum", cl::init(0), 32 cl::desc("Make -gen-asm-writer emit assembly writer #N")); 33 34 /// getValueType - Return the MVT::SimpleValueType that the specified TableGen 35 /// record corresponds to. 36 MVT::SimpleValueType llvm::getValueType(Record *Rec) { 37 return (MVT::SimpleValueType)Rec->getValueAsInt("Value"); 38 } 39 40 std::string llvm::getName(MVT::SimpleValueType T) { 41 switch (T) { 42 case MVT::Other: return "UNKNOWN"; 43 case MVT::iPTR: return "TLI.getPointerTy()"; 44 case MVT::iPTRAny: return "TLI.getPointerTy()"; 45 default: return getEnumName(T); 46 } 47 } 48 49 std::string llvm::getEnumName(MVT::SimpleValueType T) { 50 switch (T) { 51 case MVT::Other: return "MVT::Other"; 52 case MVT::i1: return "MVT::i1"; 53 case MVT::i8: return "MVT::i8"; 54 case MVT::i16: return "MVT::i16"; 55 case MVT::i32: return "MVT::i32"; 56 case MVT::i64: return "MVT::i64"; 57 case MVT::i128: return "MVT::i128"; 58 case MVT::iAny: return "MVT::iAny"; 59 case MVT::fAny: return "MVT::fAny"; 60 case MVT::vAny: return "MVT::vAny"; 61 case MVT::f16: return "MVT::f16"; 62 case MVT::f32: return "MVT::f32"; 63 case MVT::f64: return "MVT::f64"; 64 case MVT::f80: return "MVT::f80"; 65 case MVT::f128: return "MVT::f128"; 66 case MVT::ppcf128: return "MVT::ppcf128"; 67 case MVT::x86mmx: return "MVT::x86mmx"; 68 case MVT::Glue: return "MVT::Glue"; 69 case MVT::isVoid: return "MVT::isVoid"; 70 case MVT::v2i8: return "MVT::v2i8"; 71 case MVT::v4i8: return "MVT::v4i8"; 72 case MVT::v8i8: return "MVT::v8i8"; 73 case MVT::v16i8: return "MVT::v16i8"; 74 case MVT::v32i8: return "MVT::v32i8"; 75 case MVT::v2i16: return "MVT::v2i16"; 76 case MVT::v4i16: return "MVT::v4i16"; 77 case MVT::v8i16: return "MVT::v8i16"; 78 case MVT::v16i16: return "MVT::v16i16"; 79 case MVT::v2i32: return "MVT::v2i32"; 80 case MVT::v4i32: return "MVT::v4i32"; 81 case MVT::v8i32: return "MVT::v8i32"; 82 case MVT::v1i64: return "MVT::v1i64"; 83 case MVT::v2i64: return "MVT::v2i64"; 84 case MVT::v4i64: return "MVT::v4i64"; 85 case MVT::v8i64: return "MVT::v8i64"; 86 case MVT::v2f16: return "MVT::v2f16"; 87 case MVT::v2f32: return "MVT::v2f32"; 88 case MVT::v4f32: return "MVT::v4f32"; 89 case MVT::v8f32: return "MVT::v8f32"; 90 case MVT::v2f64: return "MVT::v2f64"; 91 case MVT::v4f64: return "MVT::v4f64"; 92 case MVT::Metadata: return "MVT::Metadata"; 93 case MVT::iPTR: return "MVT::iPTR"; 94 case MVT::iPTRAny: return "MVT::iPTRAny"; 95 case MVT::Untyped: return "MVT::Untyped"; 96 default: llvm_unreachable("ILLEGAL VALUE TYPE!"); 97 } 98 } 99 100 /// getQualifiedName - Return the name of the specified record, with a 101 /// namespace qualifier if the record contains one. 102 /// 103 std::string llvm::getQualifiedName(const Record *R) { 104 std::string Namespace; 105 if (R->getValue("Namespace")) 106 Namespace = R->getValueAsString("Namespace"); 107 if (Namespace.empty()) return R->getName(); 108 return Namespace + "::" + R->getName(); 109 } 110 111 const char *llvm::getMinimalTypeForRange(uint64_t Range) { 112 assert(Range < 0xFFFFFFFFULL && "Enum too large"); 113 if (Range > 0xFFFF) 114 return "uint32_t"; 115 if (Range > 0xFF) 116 return "uint16_t"; 117 return "uint8_t"; 118 } 119 120 /// getTarget - Return the current instance of the Target class. 121 /// 122 CodeGenTarget::CodeGenTarget(RecordKeeper &records) 123 : Records(records), RegBank(0) { 124 std::vector<Record*> Targets = Records.getAllDerivedDefinitions("Target"); 125 if (Targets.size() == 0) 126 throw std::string("ERROR: No 'Target' subclasses defined!"); 127 if (Targets.size() != 1) 128 throw std::string("ERROR: Multiple subclasses of Target defined!"); 129 TargetRec = Targets[0]; 130 } 131 132 133 const std::string &CodeGenTarget::getName() const { 134 return TargetRec->getName(); 135 } 136 137 std::string CodeGenTarget::getInstNamespace() const { 138 for (inst_iterator i = inst_begin(), e = inst_end(); i != e; ++i) { 139 // Make sure not to pick up "TargetOpcode" by accidentally getting 140 // the namespace off the PHI instruction or something. 141 if ((*i)->Namespace != "TargetOpcode") 142 return (*i)->Namespace; 143 } 144 145 return ""; 146 } 147 148 Record *CodeGenTarget::getInstructionSet() const { 149 return TargetRec->getValueAsDef("InstructionSet"); 150 } 151 152 153 /// getAsmParser - Return the AssemblyParser definition for this target. 154 /// 155 Record *CodeGenTarget::getAsmParser() const { 156 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyParsers"); 157 if (AsmParserNum >= LI.size()) 158 throw "Target does not have an AsmParser #" + utostr(AsmParserNum) + "!"; 159 return LI[AsmParserNum]; 160 } 161 162 /// getAsmParserVariant - Return the AssmblyParserVariant definition for 163 /// this target. 164 /// 165 Record *CodeGenTarget::getAsmParserVariant(unsigned i) const { 166 std::vector<Record*> LI = 167 TargetRec->getValueAsListOfDefs("AssemblyParserVariants"); 168 if (i >= LI.size()) 169 throw "Target does not have an AsmParserVariant #" + utostr(i) + "!"; 170 return LI[i]; 171 } 172 173 /// getAsmParserVariantCount - Return the AssmblyParserVariant definition 174 /// available for this target. 175 /// 176 unsigned CodeGenTarget::getAsmParserVariantCount() const { 177 std::vector<Record*> LI = 178 TargetRec->getValueAsListOfDefs("AssemblyParserVariants"); 179 return LI.size(); 180 } 181 182 /// getAsmWriter - Return the AssemblyWriter definition for this target. 183 /// 184 Record *CodeGenTarget::getAsmWriter() const { 185 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyWriters"); 186 if (AsmWriterNum >= LI.size()) 187 throw "Target does not have an AsmWriter #" + utostr(AsmWriterNum) + "!"; 188 return LI[AsmWriterNum]; 189 } 190 191 CodeGenRegBank &CodeGenTarget::getRegBank() const { 192 if (!RegBank) 193 RegBank = new CodeGenRegBank(Records); 194 return *RegBank; 195 } 196 197 void CodeGenTarget::ReadRegAltNameIndices() const { 198 RegAltNameIndices = Records.getAllDerivedDefinitions("RegAltNameIndex"); 199 std::sort(RegAltNameIndices.begin(), RegAltNameIndices.end(), LessRecord()); 200 } 201 202 /// getRegisterByName - If there is a register with the specific AsmName, 203 /// return it. 204 const CodeGenRegister *CodeGenTarget::getRegisterByName(StringRef Name) const { 205 const std::vector<CodeGenRegister*> &Regs = getRegBank().getRegisters(); 206 for (unsigned i = 0, e = Regs.size(); i != e; ++i) 207 if (Regs[i]->TheDef->getValueAsString("AsmName") == Name) 208 return Regs[i]; 209 210 return 0; 211 } 212 213 std::vector<MVT::SimpleValueType> CodeGenTarget:: 214 getRegisterVTs(Record *R) const { 215 const CodeGenRegister *Reg = getRegBank().getReg(R); 216 std::vector<MVT::SimpleValueType> Result; 217 ArrayRef<CodeGenRegisterClass*> RCs = getRegBank().getRegClasses(); 218 for (unsigned i = 0, e = RCs.size(); i != e; ++i) { 219 const CodeGenRegisterClass &RC = *RCs[i]; 220 if (RC.contains(Reg)) { 221 const std::vector<MVT::SimpleValueType> &InVTs = RC.getValueTypes(); 222 Result.insert(Result.end(), InVTs.begin(), InVTs.end()); 223 } 224 } 225 226 // Remove duplicates. 227 array_pod_sort(Result.begin(), Result.end()); 228 Result.erase(std::unique(Result.begin(), Result.end()), Result.end()); 229 return Result; 230 } 231 232 233 void CodeGenTarget::ReadLegalValueTypes() const { 234 ArrayRef<CodeGenRegisterClass*> RCs = getRegBank().getRegClasses(); 235 for (unsigned i = 0, e = RCs.size(); i != e; ++i) 236 for (unsigned ri = 0, re = RCs[i]->VTs.size(); ri != re; ++ri) 237 LegalValueTypes.push_back(RCs[i]->VTs[ri]); 238 239 // Remove duplicates. 240 std::sort(LegalValueTypes.begin(), LegalValueTypes.end()); 241 LegalValueTypes.erase(std::unique(LegalValueTypes.begin(), 242 LegalValueTypes.end()), 243 LegalValueTypes.end()); 244 } 245 246 247 void CodeGenTarget::ReadInstructions() const { 248 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction"); 249 if (Insts.size() <= 2) 250 throw std::string("No 'Instruction' subclasses defined!"); 251 252 // Parse the instructions defined in the .td file. 253 for (unsigned i = 0, e = Insts.size(); i != e; ++i) 254 Instructions[Insts[i]] = new CodeGenInstruction(Insts[i]); 255 } 256 257 static const CodeGenInstruction * 258 GetInstByName(const char *Name, 259 const DenseMap<const Record*, CodeGenInstruction*> &Insts, 260 RecordKeeper &Records) { 261 const Record *Rec = Records.getDef(Name); 262 263 DenseMap<const Record*, CodeGenInstruction*>::const_iterator 264 I = Insts.find(Rec); 265 if (Rec == 0 || I == Insts.end()) 266 throw std::string("Could not find '") + Name + "' instruction!"; 267 return I->second; 268 } 269 270 namespace { 271 /// SortInstByName - Sorting predicate to sort instructions by name. 272 /// 273 struct SortInstByName { 274 bool operator()(const CodeGenInstruction *Rec1, 275 const CodeGenInstruction *Rec2) const { 276 return Rec1->TheDef->getName() < Rec2->TheDef->getName(); 277 } 278 }; 279 } 280 281 /// getInstructionsByEnumValue - Return all of the instructions defined by the 282 /// target, ordered by their enum value. 283 void CodeGenTarget::ComputeInstrsByEnum() const { 284 // The ordering here must match the ordering in TargetOpcodes.h. 285 const char *const FixedInstrs[] = { 286 "PHI", 287 "INLINEASM", 288 "PROLOG_LABEL", 289 "EH_LABEL", 290 "GC_LABEL", 291 "KILL", 292 "EXTRACT_SUBREG", 293 "INSERT_SUBREG", 294 "IMPLICIT_DEF", 295 "SUBREG_TO_REG", 296 "COPY_TO_REGCLASS", 297 "DBG_VALUE", 298 "REG_SEQUENCE", 299 "COPY", 300 "BUNDLE", 301 0 302 }; 303 const DenseMap<const Record*, CodeGenInstruction*> &Insts = getInstructions(); 304 for (const char *const *p = FixedInstrs; *p; ++p) { 305 const CodeGenInstruction *Instr = GetInstByName(*p, Insts, Records); 306 assert(Instr && "Missing target independent instruction"); 307 assert(Instr->Namespace == "TargetOpcode" && "Bad namespace"); 308 InstrsByEnum.push_back(Instr); 309 } 310 unsigned EndOfPredefines = InstrsByEnum.size(); 311 312 for (DenseMap<const Record*, CodeGenInstruction*>::const_iterator 313 I = Insts.begin(), E = Insts.end(); I != E; ++I) { 314 const CodeGenInstruction *CGI = I->second; 315 if (CGI->Namespace != "TargetOpcode") 316 InstrsByEnum.push_back(CGI); 317 } 318 319 assert(InstrsByEnum.size() == Insts.size() && "Missing predefined instr"); 320 321 // All of the instructions are now in random order based on the map iteration. 322 // Sort them by name. 323 std::sort(InstrsByEnum.begin()+EndOfPredefines, InstrsByEnum.end(), 324 SortInstByName()); 325 } 326 327 328 /// isLittleEndianEncoding - Return whether this target encodes its instruction 329 /// in little-endian format, i.e. bits laid out in the order [0..n] 330 /// 331 bool CodeGenTarget::isLittleEndianEncoding() const { 332 return getInstructionSet()->getValueAsBit("isLittleEndianEncoding"); 333 } 334 335 //===----------------------------------------------------------------------===// 336 // ComplexPattern implementation 337 // 338 ComplexPattern::ComplexPattern(Record *R) { 339 Ty = ::getValueType(R->getValueAsDef("Ty")); 340 NumOperands = R->getValueAsInt("NumOperands"); 341 SelectFunc = R->getValueAsString("SelectFunc"); 342 RootNodes = R->getValueAsListOfDefs("RootNodes"); 343 344 // Parse the properties. 345 Properties = 0; 346 std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties"); 347 for (unsigned i = 0, e = PropList.size(); i != e; ++i) 348 if (PropList[i]->getName() == "SDNPHasChain") { 349 Properties |= 1 << SDNPHasChain; 350 } else if (PropList[i]->getName() == "SDNPOptInGlue") { 351 Properties |= 1 << SDNPOptInGlue; 352 } else if (PropList[i]->getName() == "SDNPMayStore") { 353 Properties |= 1 << SDNPMayStore; 354 } else if (PropList[i]->getName() == "SDNPMayLoad") { 355 Properties |= 1 << SDNPMayLoad; 356 } else if (PropList[i]->getName() == "SDNPSideEffect") { 357 Properties |= 1 << SDNPSideEffect; 358 } else if (PropList[i]->getName() == "SDNPMemOperand") { 359 Properties |= 1 << SDNPMemOperand; 360 } else if (PropList[i]->getName() == "SDNPVariadic") { 361 Properties |= 1 << SDNPVariadic; 362 } else if (PropList[i]->getName() == "SDNPWantRoot") { 363 Properties |= 1 << SDNPWantRoot; 364 } else if (PropList[i]->getName() == "SDNPWantParent") { 365 Properties |= 1 << SDNPWantParent; 366 } else { 367 errs() << "Unsupported SD Node property '" << PropList[i]->getName() 368 << "' on ComplexPattern '" << R->getName() << "'!\n"; 369 exit(1); 370 } 371 } 372 373 //===----------------------------------------------------------------------===// 374 // CodeGenIntrinsic Implementation 375 //===----------------------------------------------------------------------===// 376 377 std::vector<CodeGenIntrinsic> llvm::LoadIntrinsics(const RecordKeeper &RC, 378 bool TargetOnly) { 379 std::vector<Record*> I = RC.getAllDerivedDefinitions("Intrinsic"); 380 381 std::vector<CodeGenIntrinsic> Result; 382 383 for (unsigned i = 0, e = I.size(); i != e; ++i) { 384 bool isTarget = I[i]->getValueAsBit("isTarget"); 385 if (isTarget == TargetOnly) 386 Result.push_back(CodeGenIntrinsic(I[i])); 387 } 388 return Result; 389 } 390 391 CodeGenIntrinsic::CodeGenIntrinsic(Record *R) { 392 TheDef = R; 393 std::string DefName = R->getName(); 394 ModRef = ReadWriteMem; 395 isOverloaded = false; 396 isCommutative = false; 397 canThrow = false; 398 399 if (DefName.size() <= 4 || 400 std::string(DefName.begin(), DefName.begin() + 4) != "int_") 401 throw "Intrinsic '" + DefName + "' does not start with 'int_'!"; 402 403 EnumName = std::string(DefName.begin()+4, DefName.end()); 404 405 if (R->getValue("GCCBuiltinName")) // Ignore a missing GCCBuiltinName field. 406 GCCBuiltinName = R->getValueAsString("GCCBuiltinName"); 407 408 TargetPrefix = R->getValueAsString("TargetPrefix"); 409 Name = R->getValueAsString("LLVMName"); 410 411 if (Name == "") { 412 // If an explicit name isn't specified, derive one from the DefName. 413 Name = "llvm."; 414 415 for (unsigned i = 0, e = EnumName.size(); i != e; ++i) 416 Name += (EnumName[i] == '_') ? '.' : EnumName[i]; 417 } else { 418 // Verify it starts with "llvm.". 419 if (Name.size() <= 5 || 420 std::string(Name.begin(), Name.begin() + 5) != "llvm.") 421 throw "Intrinsic '" + DefName + "'s name does not start with 'llvm.'!"; 422 } 423 424 // If TargetPrefix is specified, make sure that Name starts with 425 // "llvm.<targetprefix>.". 426 if (!TargetPrefix.empty()) { 427 if (Name.size() < 6+TargetPrefix.size() || 428 std::string(Name.begin() + 5, Name.begin() + 6 + TargetPrefix.size()) 429 != (TargetPrefix + ".")) 430 throw "Intrinsic '" + DefName + "' does not start with 'llvm." + 431 TargetPrefix + ".'!"; 432 } 433 434 // Parse the list of return types. 435 std::vector<MVT::SimpleValueType> OverloadedVTs; 436 ListInit *TypeList = R->getValueAsListInit("RetTypes"); 437 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) { 438 Record *TyEl = TypeList->getElementAsRecord(i); 439 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!"); 440 MVT::SimpleValueType VT; 441 if (TyEl->isSubClassOf("LLVMMatchType")) { 442 unsigned MatchTy = TyEl->getValueAsInt("Number"); 443 assert(MatchTy < OverloadedVTs.size() && 444 "Invalid matching number!"); 445 VT = OverloadedVTs[MatchTy]; 446 // It only makes sense to use the extended and truncated vector element 447 // variants with iAny types; otherwise, if the intrinsic is not 448 // overloaded, all the types can be specified directly. 449 assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") && 450 !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) || 451 VT == MVT::iAny || VT == MVT::vAny) && 452 "Expected iAny or vAny type"); 453 } else { 454 VT = getValueType(TyEl->getValueAsDef("VT")); 455 } 456 if (EVT(VT).isOverloaded()) { 457 OverloadedVTs.push_back(VT); 458 isOverloaded = true; 459 } 460 461 // Reject invalid types. 462 if (VT == MVT::isVoid) 463 throw "Intrinsic '" + DefName + " has void in result type list!"; 464 465 IS.RetVTs.push_back(VT); 466 IS.RetTypeDefs.push_back(TyEl); 467 } 468 469 // Parse the list of parameter types. 470 TypeList = R->getValueAsListInit("ParamTypes"); 471 for (unsigned i = 0, e = TypeList->getSize(); i != e; ++i) { 472 Record *TyEl = TypeList->getElementAsRecord(i); 473 assert(TyEl->isSubClassOf("LLVMType") && "Expected a type!"); 474 MVT::SimpleValueType VT; 475 if (TyEl->isSubClassOf("LLVMMatchType")) { 476 unsigned MatchTy = TyEl->getValueAsInt("Number"); 477 assert(MatchTy < OverloadedVTs.size() && 478 "Invalid matching number!"); 479 VT = OverloadedVTs[MatchTy]; 480 // It only makes sense to use the extended and truncated vector element 481 // variants with iAny types; otherwise, if the intrinsic is not 482 // overloaded, all the types can be specified directly. 483 assert(((!TyEl->isSubClassOf("LLVMExtendedElementVectorType") && 484 !TyEl->isSubClassOf("LLVMTruncatedElementVectorType")) || 485 VT == MVT::iAny || VT == MVT::vAny) && 486 "Expected iAny or vAny type"); 487 } else 488 VT = getValueType(TyEl->getValueAsDef("VT")); 489 490 if (EVT(VT).isOverloaded()) { 491 OverloadedVTs.push_back(VT); 492 isOverloaded = true; 493 } 494 495 // Reject invalid types. 496 if (VT == MVT::isVoid && i != e-1 /*void at end means varargs*/) 497 throw "Intrinsic '" + DefName + " has void in result type list!"; 498 499 IS.ParamVTs.push_back(VT); 500 IS.ParamTypeDefs.push_back(TyEl); 501 } 502 503 // Parse the intrinsic properties. 504 ListInit *PropList = R->getValueAsListInit("Properties"); 505 for (unsigned i = 0, e = PropList->getSize(); i != e; ++i) { 506 Record *Property = PropList->getElementAsRecord(i); 507 assert(Property->isSubClassOf("IntrinsicProperty") && 508 "Expected a property!"); 509 510 if (Property->getName() == "IntrNoMem") 511 ModRef = NoMem; 512 else if (Property->getName() == "IntrReadArgMem") 513 ModRef = ReadArgMem; 514 else if (Property->getName() == "IntrReadMem") 515 ModRef = ReadMem; 516 else if (Property->getName() == "IntrReadWriteArgMem") 517 ModRef = ReadWriteArgMem; 518 else if (Property->getName() == "Commutative") 519 isCommutative = true; 520 else if (Property->getName() == "Throws") 521 canThrow = true; 522 else if (Property->isSubClassOf("NoCapture")) { 523 unsigned ArgNo = Property->getValueAsInt("ArgNo"); 524 ArgumentAttributes.push_back(std::make_pair(ArgNo, NoCapture)); 525 } else 526 llvm_unreachable("Unknown property!"); 527 } 528 529 // Sort the argument attributes for later benefit. 530 std::sort(ArgumentAttributes.begin(), ArgumentAttributes.end()); 531 } 532