CodeGenTarget.h revision 218893
1193323Sed//===- CodeGenTarget.h - Target Class Wrapper -------------------*- C++ -*-===// 2193323Sed// 3193323Sed// The LLVM Compiler Infrastructure 4193323Sed// 5193323Sed// This file is distributed under the University of Illinois Open Source 6193323Sed// License. See LICENSE.TXT for details. 7193323Sed// 8193323Sed//===----------------------------------------------------------------------===// 9193323Sed// 10193323Sed// This file defines wrappers for the Target class and related global 11193323Sed// functionality. This makes it easier to access the data and provides a single 12193323Sed// place that needs to check it for validity. All of these classes throw 13193323Sed// exceptions on error conditions. 14193323Sed// 15193323Sed//===----------------------------------------------------------------------===// 16193323Sed 17193323Sed#ifndef CODEGEN_TARGET_H 18193323Sed#define CODEGEN_TARGET_H 19193323Sed 20193323Sed#include "CodeGenRegisters.h" 21193323Sed#include "CodeGenInstruction.h" 22208599Srdivacky#include "Record.h" 23205407Srdivacky#include "llvm/Support/raw_ostream.h" 24193323Sed#include <algorithm> 25193323Sed 26193323Sednamespace llvm { 27193323Sed 28193323Sedstruct CodeGenRegister; 29193323Sedclass CodeGenTarget; 30193323Sed 31193323Sed// SelectionDAG node properties. 32193323Sed// SDNPMemOperand: indicates that a node touches memory and therefore must 33193323Sed// have an associated memory operand that describes the access. 34193323Sedenum SDNP { 35193323Sed SDNPCommutative, 36193323Sed SDNPAssociative, 37193323Sed SDNPHasChain, 38218893Sdim SDNPOutGlue, 39218893Sdim SDNPInGlue, 40218893Sdim SDNPOptInGlue, 41193323Sed SDNPMayLoad, 42193323Sed SDNPMayStore, 43193323Sed SDNPSideEffect, 44205407Srdivacky SDNPMemOperand, 45218893Sdim SDNPVariadic, 46218893Sdim SDNPWantRoot, 47218893Sdim SDNPWantParent 48193323Sed}; 49193323Sed 50193323Sed/// getValueType - Return the MVT::SimpleValueType that the specified TableGen 51193323Sed/// record corresponds to. 52193323SedMVT::SimpleValueType getValueType(Record *Rec); 53193323Sed 54193323Sedstd::string getName(MVT::SimpleValueType T); 55193323Sedstd::string getEnumName(MVT::SimpleValueType T); 56193323Sed 57193323Sed/// getQualifiedName - Return the name of the specified record, with a 58193323Sed/// namespace qualifier if the record contains one. 59193323Sedstd::string getQualifiedName(const Record *R); 60193323Sed 61193323Sed/// CodeGenTarget - This class corresponds to the Target class in the .td files. 62193323Sed/// 63193323Sedclass CodeGenTarget { 64218893Sdim RecordKeeper &Records; 65193323Sed Record *TargetRec; 66193323Sed 67205407Srdivacky mutable DenseMap<const Record*, CodeGenInstruction*> Instructions; 68193323Sed mutable std::vector<CodeGenRegister> Registers; 69208599Srdivacky mutable std::vector<Record*> SubRegIndices; 70193323Sed mutable std::vector<CodeGenRegisterClass> RegisterClasses; 71193323Sed mutable std::vector<MVT::SimpleValueType> LegalValueTypes; 72193323Sed void ReadRegisters() const; 73208599Srdivacky void ReadSubRegIndices() const; 74193323Sed void ReadRegisterClasses() const; 75193323Sed void ReadInstructions() const; 76193323Sed void ReadLegalValueTypes() const; 77205407Srdivacky 78205407Srdivacky mutable std::vector<const CodeGenInstruction*> InstrsByEnum; 79193323Sedpublic: 80218893Sdim CodeGenTarget(RecordKeeper &Records); 81193323Sed 82193323Sed Record *getTargetRecord() const { return TargetRec; } 83193323Sed const std::string &getName() const; 84193323Sed 85193323Sed /// getInstNamespace - Return the target-specific instruction namespace. 86193323Sed /// 87193323Sed std::string getInstNamespace() const; 88193323Sed 89193323Sed /// getInstructionSet - Return the InstructionSet object. 90193323Sed /// 91193323Sed Record *getInstructionSet() const; 92193323Sed 93198090Srdivacky /// getAsmParser - Return the AssemblyParser definition for this target. 94198090Srdivacky /// 95198090Srdivacky Record *getAsmParser() const; 96198090Srdivacky 97193323Sed /// getAsmWriter - Return the AssemblyWriter definition for this target. 98193323Sed /// 99193323Sed Record *getAsmWriter() const; 100193323Sed 101193323Sed const std::vector<CodeGenRegister> &getRegisters() const { 102193323Sed if (Registers.empty()) ReadRegisters(); 103193323Sed return Registers; 104193323Sed } 105218893Sdim 106218893Sdim /// getRegisterByName - If there is a register with the specific AsmName, 107218893Sdim /// return it. 108218893Sdim const CodeGenRegister *getRegisterByName(StringRef Name) const; 109193323Sed 110208599Srdivacky const std::vector<Record*> &getSubRegIndices() const { 111208599Srdivacky if (SubRegIndices.empty()) ReadSubRegIndices(); 112208599Srdivacky return SubRegIndices; 113208599Srdivacky } 114208599Srdivacky 115208599Srdivacky // Map a SubRegIndex Record to its number. 116208599Srdivacky unsigned getSubRegIndexNo(Record *idx) const { 117208599Srdivacky if (SubRegIndices.empty()) ReadSubRegIndices(); 118208599Srdivacky std::vector<Record*>::const_iterator i = 119208599Srdivacky std::find(SubRegIndices.begin(), SubRegIndices.end(), idx); 120208599Srdivacky assert(i != SubRegIndices.end() && "Not a SubRegIndex"); 121208599Srdivacky return (i - SubRegIndices.begin()) + 1; 122208599Srdivacky } 123208599Srdivacky 124193323Sed const std::vector<CodeGenRegisterClass> &getRegisterClasses() const { 125193323Sed if (RegisterClasses.empty()) ReadRegisterClasses(); 126193323Sed return RegisterClasses; 127193323Sed } 128208599Srdivacky 129193323Sed const CodeGenRegisterClass &getRegisterClass(Record *R) const { 130193323Sed const std::vector<CodeGenRegisterClass> &RC = getRegisterClasses(); 131193323Sed for (unsigned i = 0, e = RC.size(); i != e; ++i) 132193323Sed if (RC[i].TheDef == R) 133193323Sed return RC[i]; 134193323Sed assert(0 && "Didn't find the register class"); 135193323Sed abort(); 136193323Sed } 137193323Sed 138193323Sed /// getRegisterClassForRegister - Find the register class that contains the 139193323Sed /// specified physical register. If the register is not in a register 140193323Sed /// class, return null. If the register is in multiple classes, and the 141193323Sed /// classes have a superset-subset relationship and the same set of 142193323Sed /// types, return the superclass. Otherwise return null. 143193323Sed const CodeGenRegisterClass *getRegisterClassForRegister(Record *R) const { 144193323Sed const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses(); 145193323Sed const CodeGenRegisterClass *FoundRC = 0; 146193323Sed for (unsigned i = 0, e = RCs.size(); i != e; ++i) { 147193323Sed const CodeGenRegisterClass &RC = RegisterClasses[i]; 148193323Sed for (unsigned ei = 0, ee = RC.Elements.size(); ei != ee; ++ei) { 149193323Sed if (R != RC.Elements[ei]) 150193323Sed continue; 151193323Sed 152193323Sed // If a register's classes have different types, return null. 153193323Sed if (FoundRC && RC.getValueTypes() != FoundRC->getValueTypes()) 154193323Sed return 0; 155193323Sed 156193323Sed // If this is the first class that contains the register, 157193323Sed // make a note of it and go on to the next class. 158193323Sed if (!FoundRC) { 159193323Sed FoundRC = &RC; 160193323Sed break; 161193323Sed } 162193323Sed 163193323Sed std::vector<Record *> Elements(RC.Elements); 164193323Sed std::vector<Record *> FoundElements(FoundRC->Elements); 165193323Sed std::sort(Elements.begin(), Elements.end()); 166193323Sed std::sort(FoundElements.begin(), FoundElements.end()); 167193323Sed 168193323Sed // Check to see if the previously found class that contains 169193323Sed // the register is a subclass of the current class. If so, 170193323Sed // prefer the superclass. 171193323Sed if (std::includes(Elements.begin(), Elements.end(), 172193323Sed FoundElements.begin(), FoundElements.end())) { 173193323Sed FoundRC = &RC; 174193323Sed break; 175193323Sed } 176193323Sed 177193323Sed // Check to see if the previously found class that contains 178193323Sed // the register is a superclass of the current class. If so, 179193323Sed // prefer the superclass. 180193323Sed if (std::includes(FoundElements.begin(), FoundElements.end(), 181193323Sed Elements.begin(), Elements.end())) 182193323Sed break; 183193323Sed 184193323Sed // Multiple classes, and neither is a superclass of the other. 185193323Sed // Return null. 186193323Sed return 0; 187193323Sed } 188193323Sed } 189193323Sed return FoundRC; 190193323Sed } 191193323Sed 192193323Sed /// getRegisterVTs - Find the union of all possible SimpleValueTypes for the 193193323Sed /// specified physical register. 194205218Srdivacky std::vector<MVT::SimpleValueType> getRegisterVTs(Record *R) const; 195193323Sed 196193323Sed const std::vector<MVT::SimpleValueType> &getLegalValueTypes() const { 197193323Sed if (LegalValueTypes.empty()) ReadLegalValueTypes(); 198193323Sed return LegalValueTypes; 199193323Sed } 200193323Sed 201193323Sed /// isLegalValueType - Return true if the specified value type is natively 202193323Sed /// supported by the target (i.e. there are registers that directly hold it). 203193323Sed bool isLegalValueType(MVT::SimpleValueType VT) const { 204193323Sed const std::vector<MVT::SimpleValueType> &LegalVTs = getLegalValueTypes(); 205193323Sed for (unsigned i = 0, e = LegalVTs.size(); i != e; ++i) 206193323Sed if (LegalVTs[i] == VT) return true; 207193323Sed return false; 208193323Sed } 209193323Sed 210205407Srdivackyprivate: 211205407Srdivacky DenseMap<const Record*, CodeGenInstruction*> &getInstructions() const { 212193323Sed if (Instructions.empty()) ReadInstructions(); 213193323Sed return Instructions; 214193323Sed } 215205407Srdivackypublic: 216205407Srdivacky 217205407Srdivacky CodeGenInstruction &getInstruction(const Record *InstRec) const { 218193323Sed if (Instructions.empty()) ReadInstructions(); 219205407Srdivacky DenseMap<const Record*, CodeGenInstruction*>::iterator I = 220205407Srdivacky Instructions.find(InstRec); 221205407Srdivacky assert(I != Instructions.end() && "Not an instruction"); 222205407Srdivacky return *I->second; 223193323Sed } 224193323Sed 225193323Sed /// getInstructionsByEnumValue - Return all of the instructions defined by the 226193323Sed /// target, ordered by their enum value. 227205407Srdivacky const std::vector<const CodeGenInstruction*> & 228205407Srdivacky getInstructionsByEnumValue() const { 229205407Srdivacky if (InstrsByEnum.empty()) ComputeInstrsByEnum(); 230205407Srdivacky return InstrsByEnum; 231205407Srdivacky } 232193323Sed 233205407Srdivacky typedef std::vector<const CodeGenInstruction*>::const_iterator inst_iterator; 234205407Srdivacky inst_iterator inst_begin() const{return getInstructionsByEnumValue().begin();} 235205407Srdivacky inst_iterator inst_end() const { return getInstructionsByEnumValue().end(); } 236205407Srdivacky 237205407Srdivacky 238193323Sed /// isLittleEndianEncoding - are instruction bit patterns defined as [0..n]? 239193323Sed /// 240193323Sed bool isLittleEndianEncoding() const; 241205407Srdivacky 242205407Srdivackyprivate: 243205407Srdivacky void ComputeInstrsByEnum() const; 244193323Sed}; 245193323Sed 246193323Sed/// ComplexPattern - ComplexPattern info, corresponding to the ComplexPattern 247193323Sed/// tablegen class in TargetSelectionDAG.td 248193323Sedclass ComplexPattern { 249193323Sed MVT::SimpleValueType Ty; 250193323Sed unsigned NumOperands; 251193323Sed std::string SelectFunc; 252193323Sed std::vector<Record*> RootNodes; 253193323Sed unsigned Properties; // Node properties 254193323Sedpublic: 255199481Srdivacky ComplexPattern() : NumOperands(0) {} 256193323Sed ComplexPattern(Record *R); 257193323Sed 258193323Sed MVT::SimpleValueType getValueType() const { return Ty; } 259193323Sed unsigned getNumOperands() const { return NumOperands; } 260193323Sed const std::string &getSelectFunc() const { return SelectFunc; } 261193323Sed const std::vector<Record*> &getRootNodes() const { 262193323Sed return RootNodes; 263193323Sed } 264193323Sed bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); } 265193323Sed}; 266193323Sed 267193323Sed} // End llvm namespace 268193323Sed 269193323Sed#endif 270