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