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