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