Close list item tag, to conform with the style in this file. It's optional
[oota-llvm.git] / utils / TableGen / SubtargetEmitter.cpp
index b820b07f41667d0b5d99af378efb4a3a357b0c7c..b05b9968a2e83a149f09152e63f82c2a5a44d601 100644 (file)
@@ -2,8 +2,8 @@
 //
 //                     The LLVM Compiler Infrastructure
 //
-// This file was developed by James M. Laskey and is distributed under
-// the University of Illinois Open Source License. See LICENSE.TXT for details.
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
 //
 //===----------------------------------------------------------------------===//
 //
 #include <algorithm>
 using namespace llvm;
 
-//
-// Record sort by name function.
-//
-struct LessRecord {
-  bool operator()(const Record *Rec1, const Record *Rec2) const {
-    return Rec1->getName() < Rec2->getName();
-  }
-};
-
-//
-// Record sort by field "Name" function.
-//
-struct LessRecordFieldName {
-  bool operator()(const Record *Rec1, const Record *Rec2) const {
-    return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name");
-  }
-};
-
 //
 // Enumeration - Emit the specified class as an enumeration.
 //
@@ -56,8 +38,7 @@ void SubtargetEmitter::Enumeration(std::ostream &OS,
     Record *Def = DefList[i];
     
     // Get and emit name
-    std::string Name = Def->getName();
-    OS << "  " << Name;
+    OS << "  " << Def->getName();
     
     // If bit flags then emit expression (1 << i)
     if (isBits)  OS << " = " << " 1 << " << i;
@@ -73,37 +54,52 @@ void SubtargetEmitter::Enumeration(std::ostream &OS,
 }
 
 //
-// FeatureKeyValues - Emit data of all the subtarget features.  Used by command
-// line.
+// FeatureKeyValues - Emit data of all the subtarget features.  Used by the
+// command line.
 //
 void SubtargetEmitter::FeatureKeyValues(std::ostream &OS) {
   // Gather and sort all the features
   std::vector<Record*> FeatureList =
                            Records.getAllDerivedDefinitions("SubtargetFeature");
-  std::sort(FeatureList.begin(), FeatureList.end(), LessRecord());
+  std::sort(FeatureList.begin(), FeatureList.end(), LessRecordFieldName());
 
   // Begin feature table
   OS << "// Sorted (by key) array of values for CPU features.\n"
-     << "static llvm::SubtargetFeatureKV FeatureKV[] = {\n";
+     << "static const llvm::SubtargetFeatureKV FeatureKV[] = {\n";
   
   // For each feature
-  for (unsigned i = 0, N = FeatureList.size(); i < N;) {
+  for (unsigned i = 0, N = FeatureList.size(); i < N; ++i) {
     // Next feature
     Record *Feature = FeatureList[i];
 
-    std::string Name = Feature->getName();
-    std::string CommandLineName = Feature->getValueAsString("Name");
-    std::string Desc = Feature->getValueAsString("Desc");
+    const std::string &Name = Feature->getName();
+    const std::string &CommandLineName = Feature->getValueAsString("Name");
+    const std::string &Desc = Feature->getValueAsString("Desc");
+    
+    if (CommandLineName.empty()) continue;
     
-    // Emit as { "feature", "decription", feactureEnum }
+    // Emit as { "feature", "decription", feactureEnum, i1 | i2 | ... | in }
     OS << "  { "
        << "\"" << CommandLineName << "\", "
        << "\"" << Desc << "\", "
-       << Name
-       << " }";
+       << Name << ", ";
+
+    const std::vector<Record*> &ImpliesList = 
+      Feature->getValueAsListOfDefs("Implies");
+    
+    if (ImpliesList.empty()) {
+      OS << "0";
+    } else {
+      for (unsigned j = 0, M = ImpliesList.size(); j < M;) {
+        OS << ImpliesList[j]->getName();
+        if (++j < M) OS << " | ";
+      }
+    }
+
+    OS << " }";
     
     // Depending on 'if more in the list' emit comma
-    if (++i < N) OS << ",";
+    if ((i + 1) < N) OS << ",";
     
     OS << "\n";
   }
@@ -136,8 +132,8 @@ void SubtargetEmitter::CPUKeyValues(std::ostream &OS) {
     // Next processor
     Record *Processor = ProcessorList[i];
 
-    std::string Name = Processor->getValueAsString("Name");
-    std::vector<Record*> FeatureList = 
+    const std::string &Name = Processor->getValueAsString("Name");
+    const std::vector<Record*> &FeatureList = 
       Processor->getValueAsListOfDefs("Features");
     
     // Emit as { "cpu", "description", f1 | f2 | ... fn },
@@ -149,14 +145,13 @@ void SubtargetEmitter::CPUKeyValues(std::ostream &OS) {
       OS << "0";
     } else {
       for (unsigned j = 0, M = FeatureList.size(); j < M;) {
-        Record *Feature = FeatureList[j];
-        std::string Name = Feature->getName();
-        OS << Name;
+        OS << FeatureList[j]->getName();
         if (++j < M) OS << " | ";
       }
     }
     
-    OS << " }";
+    // The "0" is for the "implies" section of this data structure.
+    OS << ", 0 }";
     
     // Depending on 'if more in the list' emit comma
     if (++i < N) OS << ",";
@@ -188,11 +183,10 @@ unsigned SubtargetEmitter::CollectAllItinClasses(std::ostream &OS,
   unsigned N = ItinClassList.size();
   for (unsigned i = 0; i < N; i++) {
     // Next itinerary class
-    Record *ItinClass = ItinClassList[i];
+    const Record *ItinClass = ItinClassList[i];
     // Get name of itinerary class
-    std::string Name = ItinClass->getName();
     // Assign itinerary class a unique number
-    ItinClassesMap[Name] = i;
+    ItinClassesMap[ItinClass->getName()] = i;
   }
   
   // Emit size of table
@@ -212,33 +206,32 @@ void SubtargetEmitter::FormItineraryString(Record *ItinData,
                                            std::string &ItinString,
                                            unsigned &NStages) {
   // Get states list
-  std::vector<Record*> StageList = ItinData->getValueAsListOfDefs("Stages");
+  const std::vector<Record*> &StageList =
+    ItinData->getValueAsListOfDefs("Stages");
 
   // For each stage
   unsigned N = NStages = StageList.size();
-  for (unsigned i = 0; i < N; i++) {
+  for (unsigned i = 0; i < N;) {
     // Next stage
-    Record *Stage = StageList[i];
+    const Record *Stage = StageList[i];
   
     // Form string as ,{ cycles, u1 | u2 | ... | un }
     int Cycles = Stage->getValueAsInt("Cycles");
     ItinString += "  { " + itostr(Cycles) + ", ";
     
     // Get unit list
-    std::vector<Record*> UnitList = Stage->getValueAsListOfDefs("Units");
+    const std::vector<Record*> &UnitList = Stage->getValueAsListOfDefs("Units");
     
     // For each unit
     for (unsigned j = 0, M = UnitList.size(); j < M;) {
-      // Next unit
-      Record *Unit = UnitList[j];
-      
       // Add name and bitwise or
-      ItinString += Unit->getName();
+      ItinString += UnitList[j]->getName();
       if (++j < M) ItinString += " | ";
     }
     
     // Close off stage
     ItinString += " }";
+    if (++i < N) ItinString += ", ";
   }
 }
 
@@ -258,9 +251,10 @@ void SubtargetEmitter::EmitStageData(std::ostream &OS,
   if (ProcItinList.size() < 2) return;
 
   // Begin stages table
-  OS << "static llvm::InstrStage Stages[] = {\n"
+  OS << "static const llvm::InstrStage Stages[] = {\n"
         "  { 0, 0 }, // No itinerary\n";
         
+  unsigned StageCount = 1;
   unsigned ItinEnum = 1;
   std::map<std::string, unsigned> ItinMap;
   for (unsigned i = 0, N = ProcItinList.size(); i < N; i++) {
@@ -268,7 +262,7 @@ void SubtargetEmitter::EmitStageData(std::ostream &OS,
     Record *Proc = ProcItinList[i];
     
     // Get processor itinerary name
-    std::string Name = Proc->getName();
+    const std::string &Name = Proc->getName();
     
     // Skip default
     if (Name == "NoItineraries") continue;
@@ -297,15 +291,17 @@ void SubtargetEmitter::EmitStageData(std::ostream &OS,
       if (Find == 0) {
         // Emit as { cycles, u1 | u2 | ... | un }, // index
         OS << ItinString << ", // " << ItinEnum << "\n";
-        // Record Itin class number
-        ItinMap[ItinString] = Find = ItinEnum++;
+        // Record Itin class number.
+        ItinMap[ItinString] = Find = StageCount;
+        StageCount += NStages;
+        ItinEnum++;
       }
       
       // Set up itinerary as location and location + stage count
       InstrItinerary Intinerary = { Find, Find + NStages };
 
       // Locate where to inject into processor itinerary table
-      std::string Name = ItinData->getValueAsDef("TheClass")->getName();
+      const std::string &Name = ItinData->getValueAsDef("TheClass")->getName();
       Find = ItinClassesMap[Name];
       
       // Inject - empty slots will be 0, 0
@@ -344,14 +340,14 @@ void SubtargetEmitter::EmitProcessorData(std::ostream &OS,
     Record *Itin = Itins[i];
 
     // Get processor itinerary name
-    std::string Name = Itin->getName();
+    const std::string &Name = Itin->getName();
     
     // Skip default
     if (Name == "NoItineraries") continue;
 
     // Begin processor itinerary table
     OS << "\n";
-    OS << "static llvm::InstrItinerary " << Name << "[] = {\n";
+    OS << "static const llvm::InstrItinerary " << Name << "[] = {\n";
     
     // For each itinerary class
     std::vector<InstrItinerary> &ItinList = *ProcListIter++;
@@ -374,9 +370,6 @@ void SubtargetEmitter::EmitProcessorData(std::ostream &OS,
     // End processor itinerary table
     OS << "};\n";
   }
-  
-    OS << "\n";
-    OS << "static llvm::InstrItinerary NoItineraries[] = { {0, 0} };\n";
 }
 
 //
@@ -398,8 +391,9 @@ void SubtargetEmitter::EmitProcessorLookup(std::ostream &OS) {
     // Next processor
     Record *Processor = ProcessorList[i];
 
-    std::string Name = Processor->getValueAsString("Name");
-    std::string ProcItin = Processor->getValueAsDef("ProcItin")->getName();
+    const std::string &Name = Processor->getValueAsString("Name");
+    const std::string &ProcItin =
+      Processor->getValueAsDef("ProcItin")->getName();
     
     // Emit as { "cpu", procinit },
     OS << "  { "
@@ -456,33 +450,36 @@ void SubtargetEmitter::ParseFeaturesFunction(std::ostream &OS) {
   std::sort(Features.begin(), Features.end(), LessRecord());
 
   OS << "// ParseSubtargetFeatures - Parses features string setting specified\n" 
-        "// subtarget options.\n" 
-        "void llvm::";
+     << "// subtarget options.\n" 
+     << "void llvm::";
   OS << Target;
   OS << "Subtarget::ParseSubtargetFeatures(const std::string &FS,\n"
-        "                                  const std::string &CPU) {\n"
-        "  SubtargetFeatures Features(FS);\n"
-        "  Features.setCPUIfNone(CPU);\n"
-        "  uint32_t Bits =  Features.getBits(SubTypeKV, SubTypeKVSize,\n"
-        "                                    FeatureKV, FeatureKVSize);\n";
-        
+     << "                                  const std::string &CPU) {\n"
+     << "  SubtargetFeatures Features(FS);\n"
+     << "  Features.setCPUIfNone(CPU);\n"
+     << "  uint32_t Bits =  Features.getBits(SubTypeKV, SubTypeKVSize,\n"
+     << "                                    FeatureKV, FeatureKVSize);\n";
+
   for (unsigned i = 0; i < Features.size(); i++) {
     // Next record
     Record *R = Features[i];
-    std::string Instance = R->getName();
-    std::string Name = R->getValueAsString("Name");
-    std::string Value = R->getValueAsString("Value");
-    std::string Attribute = R->getValueAsString("Attribute");
-
-    OS << "  if ((Bits & " << Instance << ") != 0) "
-       << Attribute << " = " << Value << ";\n";
+    const std::string &Instance = R->getName();
+    const std::string &Value = R->getValueAsString("Value");
+    const std::string &Attribute = R->getValueAsString("Attribute");
+
+    if (Value=="true" || Value=="false")
+      OS << "  if ((Bits & " << Instance << ") != 0) "
+         << Attribute << " = " << Value << ";\n";
+    else
+      OS << "  if ((Bits & " << Instance << ") != 0 && " << Attribute << 
+            " < " << Value << ") " << Attribute << " = " << Value << ";\n";
   }
-  
+
   if (HasItineraries) {
     OS << "\n"
        << "  InstrItinerary *Itinerary = (InstrItinerary *)"
-                        "Features.getInfo(ProcItinKV, ProcItinKVSize);\n"
-          "  InstrItins = InstrItineraryData(Stages, Itinerary);\n";
+       <<              "Features.getInfo(ProcItinKV, ProcItinKVSize);\n"
+       << "  InstrItins = InstrItineraryData(Stages, Itinerary);\n";
   }
   
   OS << "}\n";