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