CodeGenSchedule.cpp revision e30f32a69ba57dfecbd670d971048bccaf727798
12661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick//===- CodeGenSchedule.cpp - Scheduling MachineModels ---------------------===//
22661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick//
32661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick//                     The LLVM Compiler Infrastructure
42661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick//
52661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick// This file is distributed under the University of Illinois Open Source
62661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick// License. See LICENSE.TXT for details.
72661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick//
82661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick//===----------------------------------------------------------------------===//
92661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick//
102661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick// This file defines structures to encapsulate the machine model as decribed in
112661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick// the target description.
122661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick//
132661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick//===----------------------------------------------------------------------===//
142661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick
152661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick#define DEBUG_TYPE "subtarget-emitter"
162661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick
172661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick#include "CodeGenSchedule.h"
182661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick#include "CodeGenTarget.h"
194ffd89fa4d2788611187d1a534d2ed46adf1702cChandler Carruth#include "llvm/ADT/STLExtras.h"
202661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick#include "llvm/Support/Debug.h"
2113745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick#include "llvm/Support/Regex.h"
224ffd89fa4d2788611187d1a534d2ed46adf1702cChandler Carruth#include "llvm/TableGen/Error.h"
232661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick
242661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trickusing namespace llvm;
252661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick
2648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick#ifndef NDEBUG
2748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trickstatic void dumpIdxVec(const IdxVec &V) {
2848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  for (unsigned i = 0, e = V.size(); i < e; ++i) {
2948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    dbgs() << V[i] << ", ";
3048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  }
3148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick}
325e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trickstatic void dumpIdxVec(const SmallVectorImpl<unsigned> &V) {
335e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  for (unsigned i = 0, e = V.size(); i < e; ++i) {
345e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    dbgs() << V[i] << ", ";
355e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  }
365e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick}
3748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick#endif
3848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick
3913745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick// (instrs a, b, ...) Evaluate and union all arguments. Identical to AddOp.
4013745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trickstruct InstrsOp : public SetTheory::Operator {
412c6d71388fb1b68ce6fdbb88642a95a24b27b2a7Joerg Sonnenberger  void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts,
422c6d71388fb1b68ce6fdbb88642a95a24b27b2a7Joerg Sonnenberger             ArrayRef<SMLoc> Loc) {
432c6d71388fb1b68ce6fdbb88642a95a24b27b2a7Joerg Sonnenberger    ST.evaluate(Expr->arg_begin(), Expr->arg_end(), Elts, Loc);
4413745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick  }
4513745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick};
4613745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick
4713745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick// (instregex "OpcPat",...) Find all instructions matching an opcode pattern.
4813745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick//
4913745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick// TODO: Since this is a prefix match, perform a binary search over the
5013745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick// instruction names using lower_bound. Note that the predefined instrs must be
5113745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick// scanned linearly first. However, this is only safe if the regex pattern has
5213745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick// no top-level bars. The DAG already has a list of patterns, so there's no
5313745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick// reason to use top-level bars, but we need a way to verify they don't exist
5413745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick// before implementing the optimization.
5513745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trickstruct InstRegexOp : public SetTheory::Operator {
5613745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick  const CodeGenTarget &Target;
5713745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick  InstRegexOp(const CodeGenTarget &t): Target(t) {}
5813745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick
592c6d71388fb1b68ce6fdbb88642a95a24b27b2a7Joerg Sonnenberger  void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts,
602c6d71388fb1b68ce6fdbb88642a95a24b27b2a7Joerg Sonnenberger             ArrayRef<SMLoc> Loc) {
6113745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick    SmallVector<Regex*, 4> RegexList;
6213745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick    for (DagInit::const_arg_iterator
6313745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick           AI = Expr->arg_begin(), AE = Expr->arg_end(); AI != AE; ++AI) {
646cfc806a6b82b60a3e923b6b89f2b4da62cdb50bSean Silva      StringInit *SI = dyn_cast<StringInit>(*AI);
6513745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick      if (!SI)
6661131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger        PrintFatalError(Loc, "instregex requires pattern string: "
672c6d71388fb1b68ce6fdbb88642a95a24b27b2a7Joerg Sonnenberger          + Expr->getAsString());
6813745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick      std::string pat = SI->getValue();
6913745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick      // Implement a python-style prefix match.
7013745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick      if (pat[0] != '^') {
7113745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick        pat.insert(0, "^(");
7213745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick        pat.insert(pat.end(), ')');
7313745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick      }
7413745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick      RegexList.push_back(new Regex(pat));
7513745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick    }
7613745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick    for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
7713745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick           E = Target.inst_end(); I != E; ++I) {
7813745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick      for (SmallVectorImpl<Regex*>::iterator
7913745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick             RI = RegexList.begin(), RE = RegexList.end(); RI != RE; ++RI) {
8013745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick        if ((*RI)->match((*I)->TheDef->getName()))
8113745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick          Elts.insert((*I)->TheDef);
8213745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick      }
8313745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick    }
8413745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick    DeleteContainerPointers(RegexList);
8513745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick  }
8613745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick};
8713745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick
8848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick/// CodeGenModels ctor interprets machine model records and populates maps.
892661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew TrickCodeGenSchedModels::CodeGenSchedModels(RecordKeeper &RK,
902661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick                                       const CodeGenTarget &TGT):
911ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick  Records(RK), Target(TGT) {
9248605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick
9313745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick  Sets.addFieldExpander("InstRW", "Instrs");
9413745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick
9513745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick  // Allow Set evaluation to recognize the dags used in InstRW records:
9613745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick  // (instrs Op1, Op1...)
9713745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick  Sets.addOperator("instrs", new InstrsOp);
9813745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick  Sets.addOperator("instregex", new InstRegexOp(Target));
9913745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick
10048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  // Instantiate a CodeGenProcModel for each SchedMachineModel with the values
10148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  // that are explicitly referenced in tablegen records. Resources associated
10248605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  // with each processor will be derived later. Populate ProcModelMap with the
10348605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  // CodeGenProcModel instances.
10448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  collectProcModels();
10548605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick
10648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  // Instantiate a CodeGenSchedRW for each SchedReadWrite record explicitly
10748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  // defined, and populate SchedReads and SchedWrites vectors. Implicit
10848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  // SchedReadWrites that represent sequences derived from expanded variant will
10948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  // be inferred later.
11048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  collectSchedRW();
11148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick
11248605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  // Instantiate a CodeGenSchedClass for each unique SchedRW signature directly
11348605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  // required by an instruction definition, and populate SchedClassIdxMap. Set
11448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  // NumItineraryClasses to the number of explicit itinerary classes referenced
11548605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  // by instructions. Set NumInstrSchedClasses to the number of itinerary
11648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  // classes plus any classes implied by instructions that derive from class
11748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  // Sched and provide SchedRW list. This does not infer any new classes from
11848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  // SchedVariant.
11948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  collectSchedClasses();
12048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick
12148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  // Find instruction itineraries for each processor. Sort and populate
12292649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick  // CodeGenProcModel::ItinDefList. (Cycle-to-cycle itineraries). This requires
12348605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  // all itinerary classes to be discovered.
12448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  collectProcItins();
12548605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick
12648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  // Find ItinRW records for each processor and itinerary class.
12748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  // (For per-operand resources mapped to itinerary classes).
12848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  collectProcItinRW();
1295e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick
1305e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  // Infer new SchedClasses from SchedVariant.
1315e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  inferSchedClasses();
1325e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick
1333cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick  // Populate each CodeGenProcModel's WriteResDefs, ReadAdvanceDefs, and
1343cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick  // ProcResourceDefs.
1353cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick  collectProcResources();
13648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick}
13748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick
13848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick/// Gather all processor models.
13948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trickvoid CodeGenSchedModels::collectProcModels() {
14048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  RecVec ProcRecords = Records.getAllDerivedDefinitions("Processor");
14148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  std::sort(ProcRecords.begin(), ProcRecords.end(), LessRecordFieldName());
14248605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick
14348605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  // Reserve space because we can. Reallocation would be ok.
14448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  ProcModels.reserve(ProcRecords.size()+1);
14548605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick
14648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  // Use idx=0 for NoModel/NoItineraries.
14748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  Record *NoModelDef = Records.getDef("NoSchedModel");
14848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  Record *NoItinsDef = Records.getDef("NoItineraries");
14948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  ProcModels.push_back(CodeGenProcModel(0, "NoSchedModel",
15048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick                                        NoModelDef, NoItinsDef));
15148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  ProcModelMap[NoModelDef] = 0;
15248605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick
15348605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  // For each processor, find a unique machine model.
15448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  for (unsigned i = 0, N = ProcRecords.size(); i < N; ++i)
15548605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    addProcModel(ProcRecords[i]);
15648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick}
15748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick
15848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick/// Get a unique processor model based on the defined MachineModel and
15948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick/// ProcessorItineraries.
16048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trickvoid CodeGenSchedModels::addProcModel(Record *ProcDef) {
16148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  Record *ModelKey = getModelOrItinDef(ProcDef);
16248605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  if (!ProcModelMap.insert(std::make_pair(ModelKey, ProcModels.size())).second)
16348605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    return;
16448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick
16548605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  std::string Name = ModelKey->getName();
16648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  if (ModelKey->isSubClassOf("SchedMachineModel")) {
16748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    Record *ItinsDef = ModelKey->getValueAsDef("Itineraries");
16848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    ProcModels.push_back(
16948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      CodeGenProcModel(ProcModels.size(), Name, ModelKey, ItinsDef));
17048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  }
17148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  else {
17248605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    // An itinerary is defined without a machine model. Infer a new model.
17348605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    if (!ModelKey->getValueAsListOfDefs("IID").empty())
17448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      Name = Name + "Model";
17548605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    ProcModels.push_back(
17648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      CodeGenProcModel(ProcModels.size(), Name,
17748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick                       ProcDef->getValueAsDef("SchedModel"), ModelKey));
17848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  }
17948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  DEBUG(ProcModels.back().dump());
18048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick}
18148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick
18248605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick// Recursively find all reachable SchedReadWrite records.
18348605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trickstatic void scanSchedRW(Record *RWDef, RecVec &RWDefs,
18448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick                        SmallPtrSet<Record*, 16> &RWSet) {
18548605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  if (!RWSet.insert(RWDef))
18648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    return;
18748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  RWDefs.push_back(RWDef);
18848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  // Reads don't current have sequence records, but it can be added later.
18948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  if (RWDef->isSubClassOf("WriteSequence")) {
19048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    RecVec Seq = RWDef->getValueAsListOfDefs("Writes");
19148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    for (RecIter I = Seq.begin(), E = Seq.end(); I != E; ++I)
19248605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      scanSchedRW(*I, RWDefs, RWSet);
19348605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  }
19448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  else if (RWDef->isSubClassOf("SchedVariant")) {
19548605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    // Visit each variant (guarded by a different predicate).
19648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    RecVec Vars = RWDef->getValueAsListOfDefs("Variants");
19748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    for (RecIter VI = Vars.begin(), VE = Vars.end(); VI != VE; ++VI) {
19848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      // Visit each RW in the sequence selected by the current variant.
19948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      RecVec Selected = (*VI)->getValueAsListOfDefs("Selected");
20048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      for (RecIter I = Selected.begin(), E = Selected.end(); I != E; ++I)
20148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick        scanSchedRW(*I, RWDefs, RWSet);
20248605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    }
20348605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  }
20448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick}
20548605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick
20648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick// Collect and sort all SchedReadWrites reachable via tablegen records.
20748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick// More may be inferred later when inferring new SchedClasses from variants.
20848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trickvoid CodeGenSchedModels::collectSchedRW() {
20948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  // Reserve idx=0 for invalid writes/reads.
21048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  SchedWrites.resize(1);
21148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  SchedReads.resize(1);
2122661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick
21348605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  SmallPtrSet<Record*, 16> RWSet;
2142661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick
21548605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  // Find all SchedReadWrites referenced by instruction defs.
21648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  RecVec SWDefs, SRDefs;
21748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
21848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick         E = Target.inst_end(); I != E; ++I) {
21948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    Record *SchedDef = (*I)->TheDef;
22064110ffc9eecbe999c29ac9d9f6697447a110036Jakob Stoklund Olesen    if (SchedDef->isValueUnset("SchedRW"))
22148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      continue;
22248605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    RecVec RWs = SchedDef->getValueAsListOfDefs("SchedRW");
22348605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    for (RecIter RWI = RWs.begin(), RWE = RWs.end(); RWI != RWE; ++RWI) {
22448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      if ((*RWI)->isSubClassOf("SchedWrite"))
22548605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick        scanSchedRW(*RWI, SWDefs, RWSet);
22648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      else {
22748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick        assert((*RWI)->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
22848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick        scanSchedRW(*RWI, SRDefs, RWSet);
22948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      }
23048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    }
23148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  }
23248605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  // Find all ReadWrites referenced by InstRW.
23348605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  RecVec InstRWDefs = Records.getAllDerivedDefinitions("InstRW");
23448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  for (RecIter OI = InstRWDefs.begin(), OE = InstRWDefs.end(); OI != OE; ++OI) {
23548605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    // For all OperandReadWrites.
23648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    RecVec RWDefs = (*OI)->getValueAsListOfDefs("OperandReadWrites");
23748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    for (RecIter RWI = RWDefs.begin(), RWE = RWDefs.end();
23848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick         RWI != RWE; ++RWI) {
23948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      if ((*RWI)->isSubClassOf("SchedWrite"))
24048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick        scanSchedRW(*RWI, SWDefs, RWSet);
24148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      else {
24248605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick        assert((*RWI)->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
24348605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick        scanSchedRW(*RWI, SRDefs, RWSet);
24448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      }
24548605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    }
24648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  }
24748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  // Find all ReadWrites referenced by ItinRW.
24848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  RecVec ItinRWDefs = Records.getAllDerivedDefinitions("ItinRW");
24948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  for (RecIter II = ItinRWDefs.begin(), IE = ItinRWDefs.end(); II != IE; ++II) {
25048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    // For all OperandReadWrites.
25148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    RecVec RWDefs = (*II)->getValueAsListOfDefs("OperandReadWrites");
25248605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    for (RecIter RWI = RWDefs.begin(), RWE = RWDefs.end();
25348605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick         RWI != RWE; ++RWI) {
25448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      if ((*RWI)->isSubClassOf("SchedWrite"))
25548605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick        scanSchedRW(*RWI, SWDefs, RWSet);
25648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      else {
25748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick        assert((*RWI)->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
25848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick        scanSchedRW(*RWI, SRDefs, RWSet);
25948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      }
26048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    }
26148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  }
26292649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick  // Find all ReadWrites referenced by SchedAlias. AliasDefs needs to be sorted
26392649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick  // for the loop below that initializes Alias vectors.
26492649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick  RecVec AliasDefs = Records.getAllDerivedDefinitions("SchedAlias");
26592649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick  std::sort(AliasDefs.begin(), AliasDefs.end(), LessRecord());
26692649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick  for (RecIter AI = AliasDefs.begin(), AE = AliasDefs.end(); AI != AE; ++AI) {
26792649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick    Record *MatchDef = (*AI)->getValueAsDef("MatchRW");
26892649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick    Record *AliasDef = (*AI)->getValueAsDef("AliasRW");
26992649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick    if (MatchDef->isSubClassOf("SchedWrite")) {
27092649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick      if (!AliasDef->isSubClassOf("SchedWrite"))
27161131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger        PrintFatalError((*AI)->getLoc(), "SchedWrite Alias must be SchedWrite");
27292649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick      scanSchedRW(AliasDef, SWDefs, RWSet);
27392649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick    }
27492649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick    else {
27592649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick      assert(MatchDef->isSubClassOf("SchedRead") && "Unknown SchedReadWrite");
27692649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick      if (!AliasDef->isSubClassOf("SchedRead"))
27761131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger        PrintFatalError((*AI)->getLoc(), "SchedRead Alias must be SchedRead");
27892649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick      scanSchedRW(AliasDef, SRDefs, RWSet);
27992649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick    }
28092649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick  }
28148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  // Sort and add the SchedReadWrites directly referenced by instructions or
28248605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  // itinerary resources. Index reads and writes in separate domains.
28348605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  std::sort(SWDefs.begin(), SWDefs.end(), LessRecord());
28448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  for (RecIter SWI = SWDefs.begin(), SWE = SWDefs.end(); SWI != SWE; ++SWI) {
28548605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    assert(!getSchedRWIdx(*SWI, /*IsRead=*/false) && "duplicate SchedWrite");
2862062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    SchedWrites.push_back(CodeGenSchedRW(SchedWrites.size(), *SWI));
28748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  }
28848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  std::sort(SRDefs.begin(), SRDefs.end(), LessRecord());
28948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  for (RecIter SRI = SRDefs.begin(), SRE = SRDefs.end(); SRI != SRE; ++SRI) {
29048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    assert(!getSchedRWIdx(*SRI, /*IsRead-*/true) && "duplicate SchedWrite");
2912062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    SchedReads.push_back(CodeGenSchedRW(SchedReads.size(), *SRI));
29248605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  }
29348605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  // Initialize WriteSequence vectors.
29448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  for (std::vector<CodeGenSchedRW>::iterator WI = SchedWrites.begin(),
29548605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick         WE = SchedWrites.end(); WI != WE; ++WI) {
29648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    if (!WI->IsSequence)
29748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      continue;
29848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    findRWs(WI->TheDef->getValueAsListOfDefs("Writes"), WI->Sequence,
29948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick            /*IsRead=*/false);
30048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  }
30192649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick  // Initialize Aliases vectors.
30292649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick  for (RecIter AI = AliasDefs.begin(), AE = AliasDefs.end(); AI != AE; ++AI) {
30392649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick    Record *AliasDef = (*AI)->getValueAsDef("AliasRW");
30492649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick    getSchedRW(AliasDef).IsAlias = true;
30592649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick    Record *MatchDef = (*AI)->getValueAsDef("MatchRW");
30692649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick    CodeGenSchedRW &RW = getSchedRW(MatchDef);
30792649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick    if (RW.IsAlias)
30861131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger      PrintFatalError((*AI)->getLoc(), "Cannot Alias an Alias");
30992649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick    RW.Aliases.push_back(*AI);
31092649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick  }
31148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  DEBUG(
31248605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    for (unsigned WIdx = 0, WEnd = SchedWrites.size(); WIdx != WEnd; ++WIdx) {
31348605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      dbgs() << WIdx << ": ";
31448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      SchedWrites[WIdx].dump();
31548605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      dbgs() << '\n';
31648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    }
31748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    for (unsigned RIdx = 0, REnd = SchedReads.size(); RIdx != REnd; ++RIdx) {
31848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      dbgs() << RIdx << ": ";
31948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      SchedReads[RIdx].dump();
32048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      dbgs() << '\n';
32148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    }
32248605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    RecVec RWDefs = Records.getAllDerivedDefinitions("SchedReadWrite");
32348605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    for (RecIter RI = RWDefs.begin(), RE = RWDefs.end();
32448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick         RI != RE; ++RI) {
32548605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      if (!getSchedRWIdx(*RI, (*RI)->isSubClassOf("SchedRead"))) {
32648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick        const std::string &Name = (*RI)->getName();
32748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick        if (Name != "NoWrite" && Name != "ReadDefault")
32848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick          dbgs() << "Unused SchedReadWrite " << (*RI)->getName() << '\n';
32948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      }
33048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    });
33148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick}
33248605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick
33348605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick/// Compute a SchedWrite name from a sequence of writes.
33448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trickstd::string CodeGenSchedModels::genRWName(const IdxVec& Seq, bool IsRead) {
33548605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  std::string Name("(");
33648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  for (IdxIter I = Seq.begin(), E = Seq.end(); I != E; ++I) {
33748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    if (I != Seq.begin())
33848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      Name += '_';
33948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    Name += getSchedRW(*I, IsRead).Name;
34048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  }
34148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  Name += ')';
34248605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  return Name;
34348605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick}
34448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick
34548605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trickunsigned CodeGenSchedModels::getSchedRWIdx(Record *Def, bool IsRead,
34648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick                                           unsigned After) const {
34748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  const std::vector<CodeGenSchedRW> &RWVec = IsRead ? SchedReads : SchedWrites;
34848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  assert(After < RWVec.size() && "start position out of bounds");
34948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  for (std::vector<CodeGenSchedRW>::const_iterator I = RWVec.begin() + After,
35048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick         E = RWVec.end(); I != E; ++I) {
35148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    if (I->TheDef == Def)
35248605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      return I - RWVec.begin();
35348605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  }
35448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  return 0;
35548605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick}
35648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick
3573b8fb648c6e1c519b7e0f487f4fb511744869d35Andrew Trickbool CodeGenSchedModels::hasReadOfWrite(Record *WriteDef) const {
3583b8fb648c6e1c519b7e0f487f4fb511744869d35Andrew Trick  for (unsigned i = 0, e = SchedReads.size(); i < e; ++i) {
3593b8fb648c6e1c519b7e0f487f4fb511744869d35Andrew Trick    Record *ReadDef = SchedReads[i].TheDef;
3603b8fb648c6e1c519b7e0f487f4fb511744869d35Andrew Trick    if (!ReadDef || !ReadDef->isSubClassOf("ProcReadAdvance"))
3613b8fb648c6e1c519b7e0f487f4fb511744869d35Andrew Trick      continue;
3623b8fb648c6e1c519b7e0f487f4fb511744869d35Andrew Trick
3633b8fb648c6e1c519b7e0f487f4fb511744869d35Andrew Trick    RecVec ValidWrites = ReadDef->getValueAsListOfDefs("ValidWrites");
3643b8fb648c6e1c519b7e0f487f4fb511744869d35Andrew Trick    if (std::find(ValidWrites.begin(), ValidWrites.end(), WriteDef)
3653b8fb648c6e1c519b7e0f487f4fb511744869d35Andrew Trick        != ValidWrites.end()) {
3663b8fb648c6e1c519b7e0f487f4fb511744869d35Andrew Trick      return true;
3673b8fb648c6e1c519b7e0f487f4fb511744869d35Andrew Trick    }
3683b8fb648c6e1c519b7e0f487f4fb511744869d35Andrew Trick  }
3693b8fb648c6e1c519b7e0f487f4fb511744869d35Andrew Trick  return false;
3703b8fb648c6e1c519b7e0f487f4fb511744869d35Andrew Trick}
3713b8fb648c6e1c519b7e0f487f4fb511744869d35Andrew Trick
37248605c340614fc1fb2ae1d975fc565a4188182e0Andrew Tricknamespace llvm {
37348605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trickvoid splitSchedReadWrites(const RecVec &RWDefs,
37448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick                          RecVec &WriteDefs, RecVec &ReadDefs) {
37548605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  for (RecIter RWI = RWDefs.begin(), RWE = RWDefs.end(); RWI != RWE; ++RWI) {
37648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    if ((*RWI)->isSubClassOf("SchedWrite"))
37748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      WriteDefs.push_back(*RWI);
37848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    else {
37948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      assert((*RWI)->isSubClassOf("SchedRead") && "unknown SchedReadWrite");
38048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      ReadDefs.push_back(*RWI);
38148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    }
38248605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  }
38348605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick}
38448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick} // namespace llvm
38548605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick
38648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick// Split the SchedReadWrites defs and call findRWs for each list.
38748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trickvoid CodeGenSchedModels::findRWs(const RecVec &RWDefs,
38848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick                                 IdxVec &Writes, IdxVec &Reads) const {
38948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    RecVec WriteDefs;
39048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    RecVec ReadDefs;
39148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    splitSchedReadWrites(RWDefs, WriteDefs, ReadDefs);
39248605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    findRWs(WriteDefs, Writes, false);
39348605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    findRWs(ReadDefs, Reads, true);
39448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick}
39548605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick
39648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick// Call getSchedRWIdx for all elements in a sequence of SchedRW defs.
39748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trickvoid CodeGenSchedModels::findRWs(const RecVec &RWDefs, IdxVec &RWs,
39848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick                                 bool IsRead) const {
39948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  for (RecIter RI = RWDefs.begin(), RE = RWDefs.end(); RI != RE; ++RI) {
40048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    unsigned Idx = getSchedRWIdx(*RI, IsRead);
40148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    assert(Idx && "failed to collect SchedReadWrite");
40248605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    RWs.push_back(Idx);
40348605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  }
4042661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick}
4052661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick
4065e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trickvoid CodeGenSchedModels::expandRWSequence(unsigned RWIdx, IdxVec &RWSeq,
4075e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick                                          bool IsRead) const {
4085e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  const CodeGenSchedRW &SchedRW = getSchedRW(RWIdx, IsRead);
4095e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  if (!SchedRW.IsSequence) {
4105e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    RWSeq.push_back(RWIdx);
4115e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    return;
4125e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  }
4135e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  int Repeat =
4145e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    SchedRW.TheDef ? SchedRW.TheDef->getValueAsInt("Repeat") : 1;
4155e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  for (int i = 0; i < Repeat; ++i) {
4165e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    for (IdxIter I = SchedRW.Sequence.begin(), E = SchedRW.Sequence.end();
4175e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick         I != E; ++I) {
4185e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick      expandRWSequence(*I, RWSeq, IsRead);
4195e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    }
4205e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  }
4215e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick}
4225e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick
4232062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick// Expand a SchedWrite as a sequence following any aliases that coincide with
4242062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick// the given processor model.
4252062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trickvoid CodeGenSchedModels::expandRWSeqForProc(
4262062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick  unsigned RWIdx, IdxVec &RWSeq, bool IsRead,
4272062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick  const CodeGenProcModel &ProcModel) const {
4282062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick
4292062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick  const CodeGenSchedRW &SchedWrite = getSchedRW(RWIdx, IsRead);
4302062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick  Record *AliasDef = 0;
4312062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick  for (RecIter AI = SchedWrite.Aliases.begin(), AE = SchedWrite.Aliases.end();
4322062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick       AI != AE; ++AI) {
4332062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    const CodeGenSchedRW &AliasRW = getSchedRW((*AI)->getValueAsDef("AliasRW"));
4342062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    if ((*AI)->getValueInit("SchedModel")->isComplete()) {
4352062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick      Record *ModelDef = (*AI)->getValueAsDef("SchedModel");
4362062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick      if (&getProcModel(ModelDef) != &ProcModel)
4372062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick        continue;
4382062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    }
4392062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    if (AliasDef)
44061131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger      PrintFatalError(AliasRW.TheDef->getLoc(), "Multiple aliases "
44161131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger                      "defined for processor " + ProcModel.ModelName +
44261131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger                      " Ensure only one SchedAlias exists per RW.");
4432062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    AliasDef = AliasRW.TheDef;
4442062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick  }
4452062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick  if (AliasDef) {
4462062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    expandRWSeqForProc(getSchedRWIdx(AliasDef, IsRead),
4472062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick                       RWSeq, IsRead,ProcModel);
4482062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    return;
4492062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick  }
4502062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick  if (!SchedWrite.IsSequence) {
4512062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    RWSeq.push_back(RWIdx);
4522062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    return;
4532062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick  }
4542062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick  int Repeat =
4552062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    SchedWrite.TheDef ? SchedWrite.TheDef->getValueAsInt("Repeat") : 1;
4562062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick  for (int i = 0; i < Repeat; ++i) {
4572062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    for (IdxIter I = SchedWrite.Sequence.begin(), E = SchedWrite.Sequence.end();
4582062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick         I != E; ++I) {
4592062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick      expandRWSeqForProc(*I, RWSeq, IsRead, ProcModel);
4602062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    }
4612062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick  }
4622062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick}
4632062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick
4645e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick// Find the existing SchedWrite that models this sequence of writes.
4655e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trickunsigned CodeGenSchedModels::findRWForSequence(const IdxVec &Seq,
4665e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick                                               bool IsRead) {
4675e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  std::vector<CodeGenSchedRW> &RWVec = IsRead ? SchedReads : SchedWrites;
4685e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick
4695e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  for (std::vector<CodeGenSchedRW>::iterator I = RWVec.begin(), E = RWVec.end();
4705e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick       I != E; ++I) {
4715e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    if (I->Sequence == Seq)
4725e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick      return I - RWVec.begin();
4735e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  }
4745e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  // Index zero reserved for invalid RW.
4755e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  return 0;
4765e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick}
4775e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick
4785e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick/// Add this ReadWrite if it doesn't already exist.
4795e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trickunsigned CodeGenSchedModels::findOrInsertRW(ArrayRef<unsigned> Seq,
4805e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick                                            bool IsRead) {
4815e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  assert(!Seq.empty() && "cannot insert empty sequence");
4825e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  if (Seq.size() == 1)
4835e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    return Seq.back();
4845e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick
4855e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  unsigned Idx = findRWForSequence(Seq, IsRead);
4865e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  if (Idx)
4875e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    return Idx;
4885e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick
4892062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick  unsigned RWIdx = IsRead ? SchedReads.size() : SchedWrites.size();
4902062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick  CodeGenSchedRW SchedRW(RWIdx, IsRead, Seq, genRWName(Seq, IsRead));
4912062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick  if (IsRead)
4925e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    SchedReads.push_back(SchedRW);
4932062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick  else
4942062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    SchedWrites.push_back(SchedRW);
4952062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick  return RWIdx;
4965e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick}
4975e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick
49848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick/// Visit all the instruction definitions for this target to gather and
49948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick/// enumerate the itinerary classes. These are the explicitly specified
50048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick/// SchedClasses. More SchedClasses may be inferred.
50148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trickvoid CodeGenSchedModels::collectSchedClasses() {
5022661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick
50348605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  // NoItinerary is always the first class at Idx=0
5042661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick  SchedClasses.resize(1);
5051ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick  SchedClasses.back().Index = 0;
5061ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick  SchedClasses.back().Name = "NoInstrModel";
5071ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick  SchedClasses.back().ItinClassDef = Records.getDef("NoItinerary");
50848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  SchedClasses.back().ProcIndices.push_back(0);
5092661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick
5101ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick  // Create a SchedClass for each unique combination of itinerary class and
5111ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick  // SchedRW list.
5122661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick  for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
5132661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick         E = Target.inst_end(); I != E; ++I) {
51448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    Record *ItinDef = (*I)->TheDef->getValueAsDef("Itinerary");
51548605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    IdxVec Writes, Reads;
5161ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick    if (!(*I)->TheDef->isValueUnset("SchedRW"))
5171ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick      findRWs((*I)->TheDef->getValueAsListOfDefs("SchedRW"), Writes, Reads);
5181ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick
51948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    // ProcIdx == 0 indicates the class applies to all processors.
52048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    IdxVec ProcIndices(1, 0);
5211ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick
5221ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick    unsigned SCIdx = addSchedClass(ItinDef, Writes, Reads, ProcIndices);
5231ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick    InstrClassMap[(*I)->TheDef] = SCIdx;
52448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  }
52592649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick  // Create classes for InstRW defs.
52648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  RecVec InstRWDefs = Records.getAllDerivedDefinitions("InstRW");
52748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  std::sort(InstRWDefs.begin(), InstRWDefs.end(), LessRecord());
52848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  for (RecIter OI = InstRWDefs.begin(), OE = InstRWDefs.end(); OI != OE; ++OI)
52948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    createInstRWClass(*OI);
5302661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick
53148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  NumInstrSchedClasses = SchedClasses.size();
53248605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick
53348605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  bool EnableDump = false;
53448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  DEBUG(EnableDump = true);
53548605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  if (!EnableDump)
53648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    return;
5371ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick
53848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
53948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick         E = Target.inst_end(); I != E; ++I) {
5401ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick
54148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    std::string InstName = (*I)->TheDef->getName();
5421ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick    unsigned SCIdx = InstrClassMap.lookup((*I)->TheDef);
5431ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick    if (!SCIdx) {
5441ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick      dbgs() << "No machine model for " << (*I)->TheDef->getName() << '\n';
5451ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick      continue;
5461ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick    }
5471ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick    CodeGenSchedClass &SC = getSchedClass(SCIdx);
5481ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick    if (SC.ProcIndices[0] != 0)
5491ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick      PrintFatalError((*I)->TheDef->getLoc(), "Instruction's sched class "
5501ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick                      "must not be subtarget specific.");
5511ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick
5521ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick    IdxVec ProcIndices;
5531ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick    if (SC.ItinClassDef->getName() != "NoItinerary") {
5541ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick      ProcIndices.push_back(0);
5551ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick      dbgs() << "Itinerary for " << InstName << ": "
5561ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick             << SC.ItinClassDef->getName() << '\n';
5571ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick    }
5581ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick    if (!SC.Writes.empty()) {
5591ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick      ProcIndices.push_back(0);
5601ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick      dbgs() << "SchedRW machine model for " << InstName;
5611ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick      for (IdxIter WI = SC.Writes.begin(), WE = SC.Writes.end(); WI != WE; ++WI)
5621ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick        dbgs() << " " << SchedWrites[*WI].Name;
5631ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick      for (IdxIter RI = SC.Reads.begin(), RE = SC.Reads.end(); RI != RE; ++RI)
5641ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick        dbgs() << " " << SchedReads[*RI].Name;
5651ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick      dbgs() << '\n';
5661ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick    }
5671ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick    const RecVec &RWDefs = SchedClasses[SCIdx].InstRWs;
5681ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick    for (RecIter RWI = RWDefs.begin(), RWE = RWDefs.end();
5691ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick         RWI != RWE; ++RWI) {
5701ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick      const CodeGenProcModel &ProcModel =
5711ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick        getProcModel((*RWI)->getValueAsDef("SchedModel"));
5721ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick      ProcIndices.push_back(ProcModel.Index);
5731ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick      dbgs() << "InstRW on " << ProcModel.ModelName << " for " << InstName;
57448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      IdxVec Writes;
57548605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      IdxVec Reads;
5761ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick      findRWs((*RWI)->getValueAsListOfDefs("OperandReadWrites"),
5771ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick              Writes, Reads);
57848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      for (IdxIter WI = Writes.begin(), WE = Writes.end(); WI != WE; ++WI)
57948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick        dbgs() << " " << SchedWrites[*WI].Name;
58048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      for (IdxIter RI = Reads.begin(), RE = Reads.end(); RI != RE; ++RI)
58148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick        dbgs() << " " << SchedReads[*RI].Name;
58248605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      dbgs() << '\n';
58348605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    }
5841ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick    for (std::vector<CodeGenProcModel>::iterator PI = ProcModels.begin(),
5851ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick           PE = ProcModels.end(); PI != PE; ++PI) {
5861ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick      if (!std::count(ProcIndices.begin(), ProcIndices.end(), PI->Index))
5871ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick        dbgs() << "No machine model for " << (*I)->TheDef->getName()
5881ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick               << " on processor " << PI->ModelName << '\n';
58948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    }
59048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  }
5912661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick}
5922661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick
59348605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick/// Find an SchedClass that has been inferred from a per-operand list of
59448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick/// SchedWrites and SchedReads.
5951ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trickunsigned CodeGenSchedModels::findSchedClassIdx(Record *ItinClassDef,
5961ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick                                               const IdxVec &Writes,
59748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick                                               const IdxVec &Reads) const {
59848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  for (SchedClassIter I = schedClassBegin(), E = schedClassEnd(); I != E; ++I) {
5991ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick    if (I->ItinClassDef == ItinClassDef
6001ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick        && I->Writes == Writes && I->Reads == Reads) {
60148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      return I - schedClassBegin();
60248605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    }
60348605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  }
60448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  return 0;
6052661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick}
6062661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick
60748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick// Get the SchedClass index for an instruction.
60848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trickunsigned CodeGenSchedModels::getSchedClassIdx(
60948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  const CodeGenInstruction &Inst) const {
6102661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick
6111ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick  return InstrClassMap.lookup(Inst.TheDef);
61248605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick}
6132661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick
61448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trickstd::string CodeGenSchedModels::createSchedClassName(
6151ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick  Record *ItinClassDef, const IdxVec &OperWrites, const IdxVec &OperReads) {
61648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick
61748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  std::string Name;
6181ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick  if (ItinClassDef && ItinClassDef->getName() != "NoItinerary")
6191ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick    Name = ItinClassDef->getName();
62048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  for (IdxIter WI = OperWrites.begin(), WE = OperWrites.end(); WI != WE; ++WI) {
6211ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick    if (!Name.empty())
62248605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      Name += '_';
62348605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    Name += SchedWrites[*WI].Name;
6242661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick  }
62548605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  for (IdxIter RI = OperReads.begin(), RE = OperReads.end(); RI != RE; ++RI) {
62648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    Name += '_';
62748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    Name += SchedReads[*RI].Name;
6282661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick  }
62948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  return Name;
63048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick}
6312661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick
63248605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trickstd::string CodeGenSchedModels::createSchedClassName(const RecVec &InstDefs) {
6332661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick
63448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  std::string Name;
63548605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  for (RecIter I = InstDefs.begin(), E = InstDefs.end(); I != E; ++I) {
63648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    if (I != InstDefs.begin())
63748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      Name += '_';
63848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    Name += (*I)->getName();
63948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  }
64048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  return Name;
64148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick}
64248605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick
6431ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick/// Add an inferred sched class from an itinerary class and per-operand list of
6441ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick/// SchedWrites and SchedReads. ProcIndices contains the set of IDs of
6451ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick/// processors that may utilize this class.
6461ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trickunsigned CodeGenSchedModels::addSchedClass(Record *ItinClassDef,
6471ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick                                           const IdxVec &OperWrites,
64848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick                                           const IdxVec &OperReads,
64948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick                                           const IdxVec &ProcIndices)
65048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick{
65148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  assert(!ProcIndices.empty() && "expect at least one ProcIdx");
65248605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick
6531ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick  unsigned Idx = findSchedClassIdx(ItinClassDef, OperWrites, OperReads);
6541ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick  if (Idx || SchedClasses[0].isKeyEqual(ItinClassDef, OperWrites, OperReads)) {
65548605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    IdxVec PI;
65648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    std::set_union(SchedClasses[Idx].ProcIndices.begin(),
65748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick                   SchedClasses[Idx].ProcIndices.end(),
65848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick                   ProcIndices.begin(), ProcIndices.end(),
65948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick                   std::back_inserter(PI));
66048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    SchedClasses[Idx].ProcIndices.swap(PI);
66148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    return Idx;
66248605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  }
66348605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  Idx = SchedClasses.size();
66448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  SchedClasses.resize(Idx+1);
66548605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  CodeGenSchedClass &SC = SchedClasses.back();
6661ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick  SC.Index = Idx;
6671ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick  SC.Name = createSchedClassName(ItinClassDef, OperWrites, OperReads);
6681ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick  SC.ItinClassDef = ItinClassDef;
66948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  SC.Writes = OperWrites;
67048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  SC.Reads = OperReads;
67148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  SC.ProcIndices = ProcIndices;
6722661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick
67348605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  return Idx;
67448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick}
67548605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick
67648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick// Create classes for each set of opcodes that are in the same InstReadWrite
67748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick// definition across all processors.
67848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trickvoid CodeGenSchedModels::createInstRWClass(Record *InstRWDef) {
67948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  // ClassInstrs will hold an entry for each subset of Instrs in InstRWDef that
68048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  // intersects with an existing class via a previous InstRWDef. Instrs that do
68148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  // not intersect with an existing class refer back to their former class as
68248605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  // determined from ItinDef or SchedRW.
68348605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  SmallVector<std::pair<unsigned, SmallVector<Record *, 8> >, 4> ClassInstrs;
68448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  // Sort Instrs into sets.
68513745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick  const RecVec *InstDefs = Sets.expand(InstRWDef);
68613745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick  if (InstDefs->empty())
68761131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger    PrintFatalError(InstRWDef->getLoc(), "No matching instruction opcodes");
68813745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick
68913745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick  for (RecIter I = InstDefs->begin(), E = InstDefs->end(); I != E; ++I) {
69048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    InstClassMapTy::const_iterator Pos = InstrClassMap.find(*I);
6911ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick    if (Pos == InstrClassMap.end())
6921ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick      PrintFatalError((*I)->getLoc(), "No sched class for instruction.");
6931ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick    unsigned SCIdx = Pos->second;
69448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    unsigned CIdx = 0, CEnd = ClassInstrs.size();
69548605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    for (; CIdx != CEnd; ++CIdx) {
69648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      if (ClassInstrs[CIdx].first == SCIdx)
69748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick        break;
69848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    }
69948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    if (CIdx == CEnd) {
70048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      ClassInstrs.resize(CEnd + 1);
70148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      ClassInstrs[CIdx].first = SCIdx;
70248605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    }
70348605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    ClassInstrs[CIdx].second.push_back(*I);
70448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  }
70548605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  // For each set of Instrs, create a new class if necessary, and map or remap
70648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  // the Instrs to it.
70748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  unsigned CIdx = 0, CEnd = ClassInstrs.size();
70848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  for (; CIdx != CEnd; ++CIdx) {
70948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    unsigned OldSCIdx = ClassInstrs[CIdx].first;
71048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    ArrayRef<Record*> InstDefs = ClassInstrs[CIdx].second;
71148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    // If the all instrs in the current class are accounted for, then leave
71248605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    // them mapped to their old class.
7131ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick    if (OldSCIdx && SchedClasses[OldSCIdx].InstRWs.size() == InstDefs.size()) {
71448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      assert(SchedClasses[OldSCIdx].ProcIndices[0] == 0 &&
71548605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick             "expected a generic SchedClass");
71648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      continue;
71748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    }
71848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    unsigned SCIdx = SchedClasses.size();
71948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    SchedClasses.resize(SCIdx+1);
72048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    CodeGenSchedClass &SC = SchedClasses.back();
7211ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick    SC.Index = SCIdx;
72248605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    SC.Name = createSchedClassName(InstDefs);
72348605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    // Preserve ItinDef and Writes/Reads for processors without an InstRW entry.
72448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    SC.ItinClassDef = SchedClasses[OldSCIdx].ItinClassDef;
72548605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    SC.Writes = SchedClasses[OldSCIdx].Writes;
72648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    SC.Reads = SchedClasses[OldSCIdx].Reads;
72748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    SC.ProcIndices.push_back(0);
72848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    // Map each Instr to this new class.
72948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    // Note that InstDefs may be a smaller list than InstRWDef's "Instrs".
73013745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick    Record *RWModelDef = InstRWDef->getValueAsDef("SchedModel");
73113745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick    SmallSet<unsigned, 4> RemappedClassIDs;
73248605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    for (ArrayRef<Record*>::const_iterator
73348605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick           II = InstDefs.begin(), IE = InstDefs.end(); II != IE; ++II) {
73448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      unsigned OldSCIdx = InstrClassMap[*II];
73513745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick      if (OldSCIdx && RemappedClassIDs.insert(OldSCIdx)) {
73613745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick        for (RecIter RI = SchedClasses[OldSCIdx].InstRWs.begin(),
73713745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick               RE = SchedClasses[OldSCIdx].InstRWs.end(); RI != RE; ++RI) {
73813745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick          if ((*RI)->getValueAsDef("SchedModel") == RWModelDef) {
73961131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger            PrintFatalError(InstRWDef->getLoc(), "Overlapping InstRW def " +
74013745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick                          (*II)->getName() + " also matches " +
74113745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick                          (*RI)->getValue("Instrs")->getValue()->getAsString());
74213745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick          }
74313745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick          assert(*RI != InstRWDef && "SchedClass has duplicate InstRW def");
74413745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick          SC.InstRWs.push_back(*RI);
74513745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick        }
74648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      }
74748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      InstrClassMap[*II] = SCIdx;
74848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    }
74948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    SC.InstRWs.push_back(InstRWDef);
75048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  }
7512661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick}
7522661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick
7531ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick// True if collectProcItins found anything.
7541ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trickbool CodeGenSchedModels::hasItineraries() const {
7551ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick  for (CodeGenSchedModels::ProcIter PI = procModelBegin(), PE = procModelEnd();
7561ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick       PI != PE; ++PI) {
7571ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick    if (PI->hasItineraries())
7581ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick      return true;
7591ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick  }
7601ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick  return false;
7611ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick}
7621ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick
7632661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick// Gather the processor itineraries.
76448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trickvoid CodeGenSchedModels::collectProcItins() {
76548605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  for (std::vector<CodeGenProcModel>::iterator PI = ProcModels.begin(),
76648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick         PE = ProcModels.end(); PI != PE; ++PI) {
76748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    CodeGenProcModel &ProcModel = *PI;
7681ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick    if (!ProcModel.hasItineraries())
76948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      continue;
7702661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick
7711ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick    RecVec ItinRecords = ProcModel.ItinsDef->getValueAsListOfDefs("IID");
7721ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick    assert(!ItinRecords.empty() && "ProcModel.hasItineraries is incorrect");
7731ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick
7741ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick    // Populate ItinDefList with Itinerary records.
7751ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick    ProcModel.ItinDefList.resize(NumInstrSchedClasses);
7762661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick
77748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    // Insert each itinerary data record in the correct position within
77848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    // the processor model's ItinDefList.
77948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    for (unsigned i = 0, N = ItinRecords.size(); i < N; i++) {
78048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      Record *ItinData = ItinRecords[i];
78148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      Record *ItinDef = ItinData->getValueAsDef("TheClass");
78202fec34de127d23987352b90bf095ad3fac75fb9Andrew Trick      bool FoundClass = false;
78302fec34de127d23987352b90bf095ad3fac75fb9Andrew Trick      for (SchedClassIter SCI = schedClassBegin(), SCE = schedClassEnd();
78402fec34de127d23987352b90bf095ad3fac75fb9Andrew Trick           SCI != SCE; ++SCI) {
78502fec34de127d23987352b90bf095ad3fac75fb9Andrew Trick        // Multiple SchedClasses may share an itinerary. Update all of them.
7861ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick        if (SCI->ItinClassDef == ItinDef) {
7871ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick          ProcModel.ItinDefList[SCI->Index] = ItinData;
78802fec34de127d23987352b90bf095ad3fac75fb9Andrew Trick          FoundClass = true;
7891ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick        }
7901ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick      }
79102fec34de127d23987352b90bf095ad3fac75fb9Andrew Trick      if (!FoundClass) {
79248605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick        DEBUG(dbgs() << ProcModel.ItinsDef->getName()
7931ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick              << " missing class for itinerary " << ItinDef->getName() << '\n');
79448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      }
79548605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    }
79648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    // Check for missing itinerary entries.
79748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    assert(!ProcModel.ItinDefList[0] && "NoItinerary class can't have rec");
79848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    DEBUG(
79948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      for (unsigned i = 1, N = ProcModel.ItinDefList.size(); i < N; ++i) {
80048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick        if (!ProcModel.ItinDefList[i])
80148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick          dbgs() << ProcModel.ItinsDef->getName()
80248605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick                 << " missing itinerary for class "
80348605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick                 << SchedClasses[i].Name << '\n';
80448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      });
80548605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  }
80648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick}
8072661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick
80848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick// Gather the read/write types for each itinerary class.
80948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trickvoid CodeGenSchedModels::collectProcItinRW() {
81048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  RecVec ItinRWDefs = Records.getAllDerivedDefinitions("ItinRW");
81148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  std::sort(ItinRWDefs.begin(), ItinRWDefs.end(), LessRecord());
81248605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  for (RecIter II = ItinRWDefs.begin(), IE = ItinRWDefs.end(); II != IE; ++II) {
81348605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    if (!(*II)->getValueInit("SchedModel")->isComplete())
81461131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger      PrintFatalError((*II)->getLoc(), "SchedModel is undefined");
81548605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    Record *ModelDef = (*II)->getValueAsDef("SchedModel");
81648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    ProcModelMapTy::const_iterator I = ProcModelMap.find(ModelDef);
81748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    if (I == ProcModelMap.end()) {
81861131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger      PrintFatalError((*II)->getLoc(), "Undefined SchedMachineModel "
81948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick                    + ModelDef->getName());
8202661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick    }
82148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    ProcModels[I->second].ItinRWDefs.push_back(*II);
8222661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick  }
82348605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick}
82448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick
8255e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick/// Infer new classes from existing classes. In the process, this may create new
8265e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick/// SchedWrites from sequences of existing SchedWrites.
8275e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trickvoid CodeGenSchedModels::inferSchedClasses() {
8281ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick  DEBUG(dbgs() << NumInstrSchedClasses << " instr sched classes.\n");
8291ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick
8305e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  // Visit all existing classes and newly created classes.
8315e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  for (unsigned Idx = 0; Idx != SchedClasses.size(); ++Idx) {
8321ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick    assert(SchedClasses[Idx].Index == Idx && "bad SCIdx");
8331ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick
8345e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    if (SchedClasses[Idx].ItinClassDef)
8355e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick      inferFromItinClass(SchedClasses[Idx].ItinClassDef, Idx);
8361ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick    if (!SchedClasses[Idx].InstRWs.empty())
8375e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick      inferFromInstRWs(Idx);
8381ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick    if (!SchedClasses[Idx].Writes.empty()) {
8395e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick      inferFromRW(SchedClasses[Idx].Writes, SchedClasses[Idx].Reads,
8405e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick                  Idx, SchedClasses[Idx].ProcIndices);
8415e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    }
8425e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    assert(SchedClasses.size() < (NumInstrSchedClasses*6) &&
8435e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick           "too many SchedVariants");
8445e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  }
8455e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick}
8465e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick
8475e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick/// Infer classes from per-processor itinerary resources.
8485e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trickvoid CodeGenSchedModels::inferFromItinClass(Record *ItinClassDef,
8495e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick                                            unsigned FromClassIdx) {
8505e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  for (unsigned PIdx = 0, PEnd = ProcModels.size(); PIdx != PEnd; ++PIdx) {
8515e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    const CodeGenProcModel &PM = ProcModels[PIdx];
8525e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    // For all ItinRW entries.
8535e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    bool HasMatch = false;
8545e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    for (RecIter II = PM.ItinRWDefs.begin(), IE = PM.ItinRWDefs.end();
8555e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick         II != IE; ++II) {
8565e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick      RecVec Matched = (*II)->getValueAsListOfDefs("MatchedItinClasses");
8575e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick      if (!std::count(Matched.begin(), Matched.end(), ItinClassDef))
8585e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick        continue;
8595e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick      if (HasMatch)
86061131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger        PrintFatalError((*II)->getLoc(), "Duplicate itinerary class "
8615e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick                      + ItinClassDef->getName()
8625e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick                      + " in ItinResources for " + PM.ModelName);
8635e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick      HasMatch = true;
8645e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick      IdxVec Writes, Reads;
8655e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick      findRWs((*II)->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
8665e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick      IdxVec ProcIndices(1, PIdx);
8675e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick      inferFromRW(Writes, Reads, FromClassIdx, ProcIndices);
8685e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    }
8695e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  }
8705e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick}
8715e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick
8725e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick/// Infer classes from per-processor InstReadWrite definitions.
8735e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trickvoid CodeGenSchedModels::inferFromInstRWs(unsigned SCIdx) {
8745e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  const RecVec &RWDefs = SchedClasses[SCIdx].InstRWs;
8755e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  for (RecIter RWI = RWDefs.begin(), RWE = RWDefs.end(); RWI != RWE; ++RWI) {
87613745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick    const RecVec *InstDefs = Sets.expand(*RWI);
87713745262a8db98d6c4513ff9934db4be75a8b26cAndrew Trick    RecIter II = InstDefs->begin(), IE = InstDefs->end();
8785e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    for (; II != IE; ++II) {
8795e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick      if (InstrClassMap[*II] == SCIdx)
8805e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick        break;
8815e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    }
8825e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    // If this class no longer has any instructions mapped to it, it has become
8835e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    // irrelevant.
8845e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    if (II == IE)
8855e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick      continue;
8865e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    IdxVec Writes, Reads;
8875e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    findRWs((*RWI)->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
8885e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    unsigned PIdx = getProcModel((*RWI)->getValueAsDef("SchedModel")).Index;
8895e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    IdxVec ProcIndices(1, PIdx);
8905e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    inferFromRW(Writes, Reads, SCIdx, ProcIndices);
8915e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  }
8925e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick}
8935e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick
8945e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Tricknamespace {
89592649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick// Helper for substituteVariantOperand.
89692649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trickstruct TransVariant {
8972062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick  Record *VarOrSeqDef;  // Variant or sequence.
8982062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick  unsigned RWIdx;       // Index of this variant or sequence's matched type.
89992649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick  unsigned ProcIdx;     // Processor model index or zero for any.
90092649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick  unsigned TransVecIdx; // Index into PredTransitions::TransVec.
90192649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick
90292649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick  TransVariant(Record *def, unsigned rwi, unsigned pi, unsigned ti):
9032062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    VarOrSeqDef(def), RWIdx(rwi), ProcIdx(pi), TransVecIdx(ti) {}
90492649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick};
90592649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick
9065e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick// Associate a predicate with the SchedReadWrite that it guards.
9075e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick// RWIdx is the index of the read/write variant.
9085e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trickstruct PredCheck {
9095e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  bool IsRead;
9105e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  unsigned RWIdx;
9115e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  Record *Predicate;
9125e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick
9135e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  PredCheck(bool r, unsigned w, Record *p): IsRead(r), RWIdx(w), Predicate(p) {}
9145e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick};
9155e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick
9165e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick// A Predicate transition is a list of RW sequences guarded by a PredTerm.
9175e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trickstruct PredTransition {
9185e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  // A predicate term is a conjunction of PredChecks.
9195e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  SmallVector<PredCheck, 4> PredTerm;
9205e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  SmallVector<SmallVector<unsigned,4>, 16> WriteSequences;
9215e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  SmallVector<SmallVector<unsigned,4>, 16> ReadSequences;
92292649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick  SmallVector<unsigned, 4> ProcIndices;
9235e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick};
9245e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick
9255e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick// Encapsulate a set of partially constructed transitions.
9265e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick// The results are built by repeated calls to substituteVariants.
9275e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trickclass PredTransitions {
9285e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  CodeGenSchedModels &SchedModels;
9295e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick
9305e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trickpublic:
9315e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  std::vector<PredTransition> TransVec;
9325e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick
9335e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  PredTransitions(CodeGenSchedModels &sm): SchedModels(sm) {}
9345e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick
9355e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  void substituteVariantOperand(const SmallVectorImpl<unsigned> &RWSeq,
9365e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick                                bool IsRead, unsigned StartIdx);
9375e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick
9385e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  void substituteVariants(const PredTransition &Trans);
9395e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick
9405e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick#ifndef NDEBUG
9415e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  void dump() const;
9425e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick#endif
9435e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick
9445e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trickprivate:
9455e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  bool mutuallyExclusive(Record *PredDef, ArrayRef<PredCheck> Term);
9462062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick  void getIntersectingVariants(
9472062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    const CodeGenSchedRW &SchedRW, unsigned TransIdx,
9482062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    std::vector<TransVariant> &IntersectingVariants);
94992649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick  void pushVariant(const TransVariant &VInfo, bool IsRead);
9505e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick};
9515e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick} // anonymous
9525e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick
9535e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick// Return true if this predicate is mutually exclusive with a PredTerm. This
9545e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick// degenerates into checking if the predicate is mutually exclusive with any
9555e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick// predicate in the Term's conjunction.
9565e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick//
9575e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick// All predicates associated with a given SchedRW are considered mutually
9585e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick// exclusive. This should work even if the conditions expressed by the
9595e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick// predicates are not exclusive because the predicates for a given SchedWrite
9605e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick// are always checked in the order they are defined in the .td file. Later
9615e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick// conditions implicitly negate any prior condition.
9625e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trickbool PredTransitions::mutuallyExclusive(Record *PredDef,
9635e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick                                        ArrayRef<PredCheck> Term) {
9645e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick
9655e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  for (ArrayRef<PredCheck>::iterator I = Term.begin(), E = Term.end();
9665e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick       I != E; ++I) {
9675e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    if (I->Predicate == PredDef)
9685e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick      return false;
9695e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick
9705e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(I->RWIdx, I->IsRead);
9715e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    assert(SchedRW.HasVariants && "PredCheck must refer to a SchedVariant");
9725e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    RecVec Variants = SchedRW.TheDef->getValueAsListOfDefs("Variants");
9735e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    for (RecIter VI = Variants.begin(), VE = Variants.end(); VI != VE; ++VI) {
9745e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick      if ((*VI)->getValueAsDef("Predicate") == PredDef)
9755e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick        return true;
9765e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    }
9775e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  }
9785e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  return false;
9795e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick}
9805e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick
9812062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trickstatic bool hasAliasedVariants(const CodeGenSchedRW &RW,
9822062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick                               CodeGenSchedModels &SchedModels) {
9832062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick  if (RW.HasVariants)
9842062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    return true;
9852062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick
9862062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick  for (RecIter I = RW.Aliases.begin(), E = RW.Aliases.end(); I != E; ++I) {
9872062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    const CodeGenSchedRW &AliasRW =
9882062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick      SchedModels.getSchedRW((*I)->getValueAsDef("AliasRW"));
9892062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    if (AliasRW.HasVariants)
9902062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick      return true;
9912062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    if (AliasRW.IsSequence) {
9922062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick      IdxVec ExpandedRWs;
9932062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick      SchedModels.expandRWSequence(AliasRW.Index, ExpandedRWs, AliasRW.IsRead);
9942062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick      for (IdxIter SI = ExpandedRWs.begin(), SE = ExpandedRWs.end();
9952062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick           SI != SE; ++SI) {
9962062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick        if (hasAliasedVariants(SchedModels.getSchedRW(*SI, AliasRW.IsRead),
9972062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick                               SchedModels)) {
9982062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick          return true;
9992062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick        }
10002062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick      }
10012062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    }
10022062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick  }
10032062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick  return false;
10042062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick}
10052062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick
10062062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trickstatic bool hasVariant(ArrayRef<PredTransition> Transitions,
10072062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick                       CodeGenSchedModels &SchedModels) {
10082062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick  for (ArrayRef<PredTransition>::iterator
10092062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick         PTI = Transitions.begin(), PTE = Transitions.end();
10102062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick       PTI != PTE; ++PTI) {
10112062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    for (SmallVectorImpl<SmallVector<unsigned,4> >::const_iterator
10122062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick           WSI = PTI->WriteSequences.begin(), WSE = PTI->WriteSequences.end();
10132062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick         WSI != WSE; ++WSI) {
10142062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick      for (SmallVectorImpl<unsigned>::const_iterator
10152062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick             WI = WSI->begin(), WE = WSI->end(); WI != WE; ++WI) {
10162062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick        if (hasAliasedVariants(SchedModels.getSchedWrite(*WI), SchedModels))
10172062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick          return true;
10182062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick      }
10192062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    }
10202062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    for (SmallVectorImpl<SmallVector<unsigned,4> >::const_iterator
10212062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick           RSI = PTI->ReadSequences.begin(), RSE = PTI->ReadSequences.end();
10222062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick         RSI != RSE; ++RSI) {
10232062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick      for (SmallVectorImpl<unsigned>::const_iterator
10242062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick             RI = RSI->begin(), RE = RSI->end(); RI != RE; ++RI) {
10252062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick        if (hasAliasedVariants(SchedModels.getSchedRead(*RI), SchedModels))
10262062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick          return true;
10272062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick      }
10282062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    }
10292062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick  }
10302062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick  return false;
10312062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick}
10322062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick
10332062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick// Populate IntersectingVariants with any variants or aliased sequences of the
10342062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick// given SchedRW whose processor indices and predicates are not mutually
1035bc4de7cec1b87fd84e6dad2c512c927d67967a22Andrew Trick// exclusive with the given transition.
10362062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trickvoid PredTransitions::getIntersectingVariants(
10372062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick  const CodeGenSchedRW &SchedRW, unsigned TransIdx,
10382062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick  std::vector<TransVariant> &IntersectingVariants) {
10392062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick
1040bc4de7cec1b87fd84e6dad2c512c927d67967a22Andrew Trick  bool GenericRW = false;
1041bc4de7cec1b87fd84e6dad2c512c927d67967a22Andrew Trick
10422062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick  std::vector<TransVariant> Variants;
10432062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick  if (SchedRW.HasVariants) {
10442062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    unsigned VarProcIdx = 0;
10452062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    if (SchedRW.TheDef->getValueInit("SchedModel")->isComplete()) {
10462062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick      Record *ModelDef = SchedRW.TheDef->getValueAsDef("SchedModel");
10472062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick      VarProcIdx = SchedModels.getProcModel(ModelDef).Index;
10482062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    }
10492062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    // Push each variant. Assign TransVecIdx later.
10502062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    const RecVec VarDefs = SchedRW.TheDef->getValueAsListOfDefs("Variants");
10512062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    for (RecIter RI = VarDefs.begin(), RE = VarDefs.end(); RI != RE; ++RI)
10522062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick      Variants.push_back(TransVariant(*RI, SchedRW.Index, VarProcIdx, 0));
1053bc4de7cec1b87fd84e6dad2c512c927d67967a22Andrew Trick    if (VarProcIdx == 0)
1054bc4de7cec1b87fd84e6dad2c512c927d67967a22Andrew Trick      GenericRW = true;
10552062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick  }
10562062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick  for (RecIter AI = SchedRW.Aliases.begin(), AE = SchedRW.Aliases.end();
10572062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick       AI != AE; ++AI) {
10582062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    // If either the SchedAlias itself or the SchedReadWrite that it aliases
10592062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    // to is defined within a processor model, constrain all variants to
10602062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    // that processor.
10612062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    unsigned AliasProcIdx = 0;
10622062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    if ((*AI)->getValueInit("SchedModel")->isComplete()) {
10632062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick      Record *ModelDef = (*AI)->getValueAsDef("SchedModel");
10642062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick      AliasProcIdx = SchedModels.getProcModel(ModelDef).Index;
10652062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    }
10662062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    const CodeGenSchedRW &AliasRW =
10672062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick      SchedModels.getSchedRW((*AI)->getValueAsDef("AliasRW"));
10682062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick
10692062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    if (AliasRW.HasVariants) {
10702062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick      const RecVec VarDefs = AliasRW.TheDef->getValueAsListOfDefs("Variants");
10712062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick      for (RecIter RI = VarDefs.begin(), RE = VarDefs.end(); RI != RE; ++RI)
10722062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick        Variants.push_back(TransVariant(*RI, AliasRW.Index, AliasProcIdx, 0));
10732062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    }
10742062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    if (AliasRW.IsSequence) {
10752062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick      Variants.push_back(
10762062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick        TransVariant(AliasRW.TheDef, SchedRW.Index, AliasProcIdx, 0));
10772062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    }
1078bc4de7cec1b87fd84e6dad2c512c927d67967a22Andrew Trick    if (AliasProcIdx == 0)
1079bc4de7cec1b87fd84e6dad2c512c927d67967a22Andrew Trick      GenericRW = true;
10802062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick  }
10812062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick  for (unsigned VIdx = 0, VEnd = Variants.size(); VIdx != VEnd; ++VIdx) {
10822062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    TransVariant &Variant = Variants[VIdx];
10832062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    // Don't expand variants if the processor models don't intersect.
10842062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    // A zero processor index means any processor.
10852062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    SmallVector<unsigned, 4> &ProcIndices = TransVec[TransIdx].ProcIndices;
10862062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    if (ProcIndices[0] && Variants[VIdx].ProcIdx) {
10872062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick      unsigned Cnt = std::count(ProcIndices.begin(), ProcIndices.end(),
10882062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick                                Variant.ProcIdx);
10892062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick      if (!Cnt)
10902062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick        continue;
10912062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick      if (Cnt > 1) {
10922062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick        const CodeGenProcModel &PM =
10932062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick          *(SchedModels.procModelBegin() + Variant.ProcIdx);
109461131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger        PrintFatalError(Variant.VarOrSeqDef->getLoc(),
109561131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger                        "Multiple variants defined for processor " +
109661131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger                        PM.ModelName +
109761131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger                        " Ensure only one SchedAlias exists per RW.");
10982062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick      }
10992062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    }
11002062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    if (Variant.VarOrSeqDef->isSubClassOf("SchedVar")) {
11012062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick      Record *PredDef = Variant.VarOrSeqDef->getValueAsDef("Predicate");
11022062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick      if (mutuallyExclusive(PredDef, TransVec[TransIdx].PredTerm))
11032062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick        continue;
11042062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    }
11052062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    if (IntersectingVariants.empty()) {
11062062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick      // The first variant builds on the existing transition.
11072062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick      Variant.TransVecIdx = TransIdx;
11082062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick      IntersectingVariants.push_back(Variant);
11092062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    }
11102062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    else {
11112062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick      // Push another copy of the current transition for more variants.
11122062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick      Variant.TransVecIdx = TransVec.size();
11132062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick      IntersectingVariants.push_back(Variant);
11141cbd4017566ea2fcb1a7cd605f412322da879e34Dan Gohman      TransVec.push_back(TransVec[TransIdx]);
11152062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    }
11162062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick  }
1117bc4de7cec1b87fd84e6dad2c512c927d67967a22Andrew Trick  if (GenericRW && IntersectingVariants.empty()) {
1118bc4de7cec1b87fd84e6dad2c512c927d67967a22Andrew Trick    PrintFatalError(SchedRW.TheDef->getLoc(), "No variant of this type has "
1119bc4de7cec1b87fd84e6dad2c512c927d67967a22Andrew Trick                    "a matching predicate on any processor");
1120bc4de7cec1b87fd84e6dad2c512c927d67967a22Andrew Trick  }
11212062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick}
11222062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick
112392649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick// Push the Reads/Writes selected by this variant onto the PredTransition
112492649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick// specified by VInfo.
112592649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trickvoid PredTransitions::
112692649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew TrickpushVariant(const TransVariant &VInfo, bool IsRead) {
112792649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick
112892649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick  PredTransition &Trans = TransVec[VInfo.TransVecIdx];
112992649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick
113092649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick  // If this operand transition is reached through a processor-specific alias,
113192649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick  // then the whole transition is specific to this processor.
113292649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick  if (VInfo.ProcIdx != 0)
113392649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick    Trans.ProcIndices.assign(1, VInfo.ProcIdx);
113492649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick
11355e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  IdxVec SelectedRWs;
11362062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick  if (VInfo.VarOrSeqDef->isSubClassOf("SchedVar")) {
11372062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    Record *PredDef = VInfo.VarOrSeqDef->getValueAsDef("Predicate");
11382062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    Trans.PredTerm.push_back(PredCheck(IsRead, VInfo.RWIdx,PredDef));
11392062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    RecVec SelectedDefs = VInfo.VarOrSeqDef->getValueAsListOfDefs("Selected");
11402062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    SchedModels.findRWs(SelectedDefs, SelectedRWs, IsRead);
11412062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick  }
11422062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick  else {
11432062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    assert(VInfo.VarOrSeqDef->isSubClassOf("WriteSequence") &&
11442062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick           "variant must be a SchedVariant or aliased WriteSequence");
11452062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick    SelectedRWs.push_back(SchedModels.getSchedRWIdx(VInfo.VarOrSeqDef, IsRead));
11462062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick  }
11475e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick
114892649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick  const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(VInfo.RWIdx, IsRead);
11495e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick
11505e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  SmallVectorImpl<SmallVector<unsigned,4> > &RWSequences = IsRead
11515e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    ? Trans.ReadSequences : Trans.WriteSequences;
11525e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  if (SchedRW.IsVariadic) {
11535e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    unsigned OperIdx = RWSequences.size()-1;
11545e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    // Make N-1 copies of this transition's last sequence.
11555e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    for (unsigned i = 1, e = SelectedRWs.size(); i != e; ++i) {
11565e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick      RWSequences.push_back(RWSequences[OperIdx]);
11575e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    }
11585e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    // Push each of the N elements of the SelectedRWs onto a copy of the last
11595e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    // sequence (split the current operand into N operands).
11605e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    // Note that write sequences should be expanded within this loop--the entire
11615e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    // sequence belongs to a single operand.
11625e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    for (IdxIter RWI = SelectedRWs.begin(), RWE = SelectedRWs.end();
11635e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick         RWI != RWE; ++RWI, ++OperIdx) {
11645e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick      IdxVec ExpandedRWs;
11655e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick      if (IsRead)
11665e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick        ExpandedRWs.push_back(*RWI);
11675e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick      else
11685e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick        SchedModels.expandRWSequence(*RWI, ExpandedRWs, IsRead);
11695e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick      RWSequences[OperIdx].insert(RWSequences[OperIdx].end(),
11705e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick                                  ExpandedRWs.begin(), ExpandedRWs.end());
11715e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    }
11725e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    assert(OperIdx == RWSequences.size() && "missed a sequence");
11735e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  }
11745e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  else {
11755e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    // Push this transition's expanded sequence onto this transition's last
11765e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    // sequence (add to the current operand's sequence).
11775e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    SmallVectorImpl<unsigned> &Seq = RWSequences.back();
11785e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    IdxVec ExpandedRWs;
11795e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    for (IdxIter RWI = SelectedRWs.begin(), RWE = SelectedRWs.end();
11805e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick         RWI != RWE; ++RWI) {
11815e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick      if (IsRead)
11825e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick        ExpandedRWs.push_back(*RWI);
11835e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick      else
11845e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick        SchedModels.expandRWSequence(*RWI, ExpandedRWs, IsRead);
11855e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    }
11865e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    Seq.insert(Seq.end(), ExpandedRWs.begin(), ExpandedRWs.end());
11875e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  }
11885e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick}
11895e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick
11905e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick// RWSeq is a sequence of all Reads or all Writes for the next read or write
11915e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick// operand. StartIdx is an index into TransVec where partial results
119292649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick// starts. RWSeq must be applied to all transitions between StartIdx and the end
11935e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick// of TransVec.
11945e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trickvoid PredTransitions::substituteVariantOperand(
11955e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  const SmallVectorImpl<unsigned> &RWSeq, bool IsRead, unsigned StartIdx) {
11965e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick
11975e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  // Visit each original RW within the current sequence.
11985e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  for (SmallVectorImpl<unsigned>::const_iterator
11995e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick         RWI = RWSeq.begin(), RWE = RWSeq.end(); RWI != RWE; ++RWI) {
12005e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    const CodeGenSchedRW &SchedRW = SchedModels.getSchedRW(*RWI, IsRead);
12015e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    // Push this RW on all partial PredTransitions or distribute variants.
12025e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    // New PredTransitions may be pushed within this loop which should not be
12035e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    // revisited (TransEnd must be loop invariant).
12045e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    for (unsigned TransIdx = StartIdx, TransEnd = TransVec.size();
12055e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick         TransIdx != TransEnd; ++TransIdx) {
12065e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick      // In the common case, push RW onto the current operand's sequence.
120792649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick      if (!hasAliasedVariants(SchedRW, SchedModels)) {
12085e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick        if (IsRead)
12095e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick          TransVec[TransIdx].ReadSequences.back().push_back(*RWI);
12105e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick        else
12115e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick          TransVec[TransIdx].WriteSequences.back().push_back(*RWI);
12125e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick        continue;
12135e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick      }
12145e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick      // Distribute this partial PredTransition across intersecting variants.
12152062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick      // This will push a copies of TransVec[TransIdx] on the back of TransVec.
121692649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick      std::vector<TransVariant> IntersectingVariants;
12172062b1260fa9df3e69e7b4d24a657a0ebb7f8710Andrew Trick      getIntersectingVariants(SchedRW, TransIdx, IntersectingVariants);
12185e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick      // Now expand each variant on top of its copy of the transition.
121992649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick      for (std::vector<TransVariant>::const_iterator
12205e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick             IVI = IntersectingVariants.begin(),
12215e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick             IVE = IntersectingVariants.end();
122292649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick           IVI != IVE; ++IVI) {
122392649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick        pushVariant(*IVI, IsRead);
122492649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick      }
12255e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    }
12265e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  }
12275e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick}
12285e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick
12295e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick// For each variant of a Read/Write in Trans, substitute the sequence of
12305e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick// Read/Writes guarded by the variant. This is exponential in the number of
12315e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick// variant Read/Writes, but in practice detection of mutually exclusive
12325e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick// predicates should result in linear growth in the total number variants.
12335e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick//
12345e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick// This is one step in a breadth-first search of nested variants.
12355e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trickvoid PredTransitions::substituteVariants(const PredTransition &Trans) {
12365e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  // Build up a set of partial results starting at the back of
12375e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  // PredTransitions. Remember the first new transition.
12385e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  unsigned StartIdx = TransVec.size();
12395e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  TransVec.resize(TransVec.size() + 1);
12405e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  TransVec.back().PredTerm = Trans.PredTerm;
124192649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick  TransVec.back().ProcIndices = Trans.ProcIndices;
12425e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick
12435e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  // Visit each original write sequence.
12445e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  for (SmallVectorImpl<SmallVector<unsigned,4> >::const_iterator
12455e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick         WSI = Trans.WriteSequences.begin(), WSE = Trans.WriteSequences.end();
12465e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick       WSI != WSE; ++WSI) {
12475e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    // Push a new (empty) write sequence onto all partial Transitions.
12485e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    for (std::vector<PredTransition>::iterator I =
12495e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick           TransVec.begin() + StartIdx, E = TransVec.end(); I != E; ++I) {
12505e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick      I->WriteSequences.resize(I->WriteSequences.size() + 1);
12515e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    }
12525e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    substituteVariantOperand(*WSI, /*IsRead=*/false, StartIdx);
12535e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  }
12545e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  // Visit each original read sequence.
12555e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  for (SmallVectorImpl<SmallVector<unsigned,4> >::const_iterator
12565e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick         RSI = Trans.ReadSequences.begin(), RSE = Trans.ReadSequences.end();
12575e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick       RSI != RSE; ++RSI) {
12585e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    // Push a new (empty) read sequence onto all partial Transitions.
12595e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    for (std::vector<PredTransition>::iterator I =
12605e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick           TransVec.begin() + StartIdx, E = TransVec.end(); I != E; ++I) {
12615e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick      I->ReadSequences.resize(I->ReadSequences.size() + 1);
12625e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    }
12635e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    substituteVariantOperand(*RSI, /*IsRead=*/true, StartIdx);
12645e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  }
12655e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick}
12665e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick
12675e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick// Create a new SchedClass for each variant found by inferFromRW. Pass
12685e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trickstatic void inferFromTransitions(ArrayRef<PredTransition> LastTransitions,
126992649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick                                 unsigned FromClassIdx,
12705e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick                                 CodeGenSchedModels &SchedModels) {
12715e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  // For each PredTransition, create a new CodeGenSchedTransition, which usually
12725e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  // requires creating a new SchedClass.
12735e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  for (ArrayRef<PredTransition>::iterator
12745e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick         I = LastTransitions.begin(), E = LastTransitions.end(); I != E; ++I) {
12755e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    IdxVec OperWritesVariant;
12765e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    for (SmallVectorImpl<SmallVector<unsigned,4> >::const_iterator
12775e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick           WSI = I->WriteSequences.begin(), WSE = I->WriteSequences.end();
12785e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick         WSI != WSE; ++WSI) {
12795e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick      // Create a new write representing the expanded sequence.
12805e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick      OperWritesVariant.push_back(
12815e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick        SchedModels.findOrInsertRW(*WSI, /*IsRead=*/false));
12825e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    }
12835e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    IdxVec OperReadsVariant;
12845e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    for (SmallVectorImpl<SmallVector<unsigned,4> >::const_iterator
12855e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick           RSI = I->ReadSequences.begin(), RSE = I->ReadSequences.end();
12865e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick         RSI != RSE; ++RSI) {
128792649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick      // Create a new read representing the expanded sequence.
12885e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick      OperReadsVariant.push_back(
12895e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick        SchedModels.findOrInsertRW(*RSI, /*IsRead=*/true));
12905e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    }
129192649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick    IdxVec ProcIndices(I->ProcIndices.begin(), I->ProcIndices.end());
12925e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    CodeGenSchedTransition SCTrans;
12935e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    SCTrans.ToClassIdx =
12941ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick      SchedModels.addSchedClass(/*ItinClassDef=*/0, OperWritesVariant,
12951ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick                                OperReadsVariant, ProcIndices);
12965e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    SCTrans.ProcIndices = ProcIndices;
12975e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    // The final PredTerm is unique set of predicates guarding the transition.
12985e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    RecVec Preds;
12995e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    for (SmallVectorImpl<PredCheck>::const_iterator
13005e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick           PI = I->PredTerm.begin(), PE = I->PredTerm.end(); PI != PE; ++PI) {
13015e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick      Preds.push_back(PI->Predicate);
13025e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    }
13035e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    RecIter PredsEnd = std::unique(Preds.begin(), Preds.end());
13045e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    Preds.resize(PredsEnd - Preds.begin());
13055e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    SCTrans.PredTerm = Preds;
13065e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    SchedModels.getSchedClass(FromClassIdx).Transitions.push_back(SCTrans);
13075e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  }
13085e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick}
13095e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick
131092649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick// Create new SchedClasses for the given ReadWrite list. If any of the
131192649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick// ReadWrites refers to a SchedVariant, create a new SchedClass for each variant
131292649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick// of the ReadWrite list, following Aliases if necessary.
13135e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trickvoid CodeGenSchedModels::inferFromRW(const IdxVec &OperWrites,
13145e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick                                     const IdxVec &OperReads,
13155e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick                                     unsigned FromClassIdx,
13165e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick                                     const IdxVec &ProcIndices) {
131782e7c4f533a98a16b0cadd5209c1d3dc8ce33d87Andrew Trick  DEBUG(dbgs() << "INFER RW proc("; dumpIdxVec(ProcIndices); dbgs() << ") ");
13185e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick
13195e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  // Create a seed transition with an empty PredTerm and the expanded sequences
13205e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  // of SchedWrites for the current SchedClass.
13215e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  std::vector<PredTransition> LastTransitions;
13225e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  LastTransitions.resize(1);
132392649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick  LastTransitions.back().ProcIndices.append(ProcIndices.begin(),
132492649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick                                            ProcIndices.end());
132592649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick
13265e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  for (IdxIter I = OperWrites.begin(), E = OperWrites.end(); I != E; ++I) {
13275e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    IdxVec WriteSeq;
13285e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    expandRWSequence(*I, WriteSeq, /*IsRead=*/false);
13295e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    unsigned Idx = LastTransitions[0].WriteSequences.size();
13305e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    LastTransitions[0].WriteSequences.resize(Idx + 1);
13315e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    SmallVectorImpl<unsigned> &Seq = LastTransitions[0].WriteSequences[Idx];
13325e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    for (IdxIter WI = WriteSeq.begin(), WE = WriteSeq.end(); WI != WE; ++WI)
13335e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick      Seq.push_back(*WI);
13345e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    DEBUG(dbgs() << "("; dumpIdxVec(Seq); dbgs() << ") ");
13355e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  }
13365e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  DEBUG(dbgs() << " Reads: ");
13375e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  for (IdxIter I = OperReads.begin(), E = OperReads.end(); I != E; ++I) {
13385e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    IdxVec ReadSeq;
13395e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    expandRWSequence(*I, ReadSeq, /*IsRead=*/true);
13405e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    unsigned Idx = LastTransitions[0].ReadSequences.size();
13415e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    LastTransitions[0].ReadSequences.resize(Idx + 1);
13425e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    SmallVectorImpl<unsigned> &Seq = LastTransitions[0].ReadSequences[Idx];
13435e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    for (IdxIter RI = ReadSeq.begin(), RE = ReadSeq.end(); RI != RE; ++RI)
13445e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick      Seq.push_back(*RI);
13455e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    DEBUG(dbgs() << "("; dumpIdxVec(Seq); dbgs() << ") ");
13465e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  }
13475e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  DEBUG(dbgs() << '\n');
13485e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick
13495e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  // Collect all PredTransitions for individual operands.
13505e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  // Iterate until no variant writes remain.
13515e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  while (hasVariant(LastTransitions, *this)) {
13525e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    PredTransitions Transitions(*this);
13535e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    for (std::vector<PredTransition>::const_iterator
13545e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick           I = LastTransitions.begin(), E = LastTransitions.end();
13555e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick         I != E; ++I) {
13565e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick      Transitions.substituteVariants(*I);
13575e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    }
13585e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    DEBUG(Transitions.dump());
13595e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    LastTransitions.swap(Transitions.TransVec);
13605e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  }
13615e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  // If the first transition has no variants, nothing to do.
13625e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  if (LastTransitions[0].PredTerm.empty())
13635e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    return;
13645e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick
13655e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  // WARNING: We are about to mutate the SchedClasses vector. Do not refer to
13665e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  // OperWrites, OperReads, or ProcIndices after calling inferFromTransitions.
136792649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick  inferFromTransitions(LastTransitions, FromClassIdx, *this);
13685e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick}
13695e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick
1370e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick// Check if any processor resource group contains all resource records in
1371e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick// SubUnits.
1372e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trickbool CodeGenSchedModels::hasSuperGroup(RecVec &SubUnits, CodeGenProcModel &PM) {
1373e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick  for (unsigned i = 0, e = PM.ProcResourceDefs.size(); i < e; ++i) {
1374e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick    if (!PM.ProcResourceDefs[i]->isSubClassOf("ProcResGroup"))
1375e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick      continue;
1376e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick    RecVec SuperUnits =
1377e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick      PM.ProcResourceDefs[i]->getValueAsListOfDefs("Resources");
1378e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick    RecIter RI = SubUnits.begin(), RE = SubUnits.end();
1379e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick    for ( ; RI != RE; ++RI) {
1380e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick      if (std::find(SuperUnits.begin(), SuperUnits.end(), *RI)
1381e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick          == SuperUnits.end()) {
1382e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick        break;
1383e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick      }
1384e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick    }
1385e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick    if (RI == RE)
1386e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick      return true;
1387e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick  }
1388e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick  return false;
1389e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick}
1390e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick
1391e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick// Verify that overlapping groups have a common supergroup.
1392e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trickvoid CodeGenSchedModels::verifyProcResourceGroups(CodeGenProcModel &PM) {
1393e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick  for (unsigned i = 0, e = PM.ProcResourceDefs.size(); i < e; ++i) {
1394e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick    if (!PM.ProcResourceDefs[i]->isSubClassOf("ProcResGroup"))
1395e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick      continue;
1396e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick    RecVec CheckUnits =
1397e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick      PM.ProcResourceDefs[i]->getValueAsListOfDefs("Resources");
1398e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick    for (unsigned j = i+1; j < e; ++j) {
1399e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick      if (!PM.ProcResourceDefs[j]->isSubClassOf("ProcResGroup"))
1400e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick        continue;
1401e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick      RecVec OtherUnits =
1402e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick        PM.ProcResourceDefs[j]->getValueAsListOfDefs("Resources");
1403e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick      if (std::find_first_of(CheckUnits.begin(), CheckUnits.end(),
1404e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick                             OtherUnits.begin(), OtherUnits.end())
1405e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick          != CheckUnits.end()) {
1406e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick        // CheckUnits and OtherUnits overlap
1407e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick        OtherUnits.insert(OtherUnits.end(), CheckUnits.begin(),
1408e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick                          CheckUnits.end());
1409e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick        if (!hasSuperGroup(OtherUnits, PM)) {
1410e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick          PrintFatalError((PM.ProcResourceDefs[i])->getLoc(),
1411e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick                          "proc resource group overlaps with "
1412e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick                          + PM.ProcResourceDefs[j]->getName()
1413e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick                          + " but no supergroup contains both.");
1414e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick        }
1415e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick      }
1416e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick    }
1417e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick  }
1418e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick}
1419e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick
14203cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick// Collect and sort WriteRes, ReadAdvance, and ProcResources.
14213cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trickvoid CodeGenSchedModels::collectProcResources() {
14223cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick  // Add any subtarget-specific SchedReadWrites that are directly associated
14233cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick  // with processor resources. Refer to the parent SchedClass's ProcIndices to
14243cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick  // determine which processors they apply to.
14253cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick  for (SchedClassIter SCI = schedClassBegin(), SCE = schedClassEnd();
14263cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick       SCI != SCE; ++SCI) {
14273cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick    if (SCI->ItinClassDef)
14283cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick      collectItinProcResources(SCI->ItinClassDef);
1429d9a4f0cbd28351155a7d9bd62aa08d8394435f82Andrew Trick    else {
1430d9a4f0cbd28351155a7d9bd62aa08d8394435f82Andrew Trick      // This class may have a default ReadWrite list which can be overriden by
1431d9a4f0cbd28351155a7d9bd62aa08d8394435f82Andrew Trick      // InstRW definitions.
1432d9a4f0cbd28351155a7d9bd62aa08d8394435f82Andrew Trick      if (!SCI->InstRWs.empty()) {
1433d9a4f0cbd28351155a7d9bd62aa08d8394435f82Andrew Trick        for (RecIter RWI = SCI->InstRWs.begin(), RWE = SCI->InstRWs.end();
1434d9a4f0cbd28351155a7d9bd62aa08d8394435f82Andrew Trick             RWI != RWE; ++RWI) {
1435d9a4f0cbd28351155a7d9bd62aa08d8394435f82Andrew Trick          Record *RWModelDef = (*RWI)->getValueAsDef("SchedModel");
1436d9a4f0cbd28351155a7d9bd62aa08d8394435f82Andrew Trick          IdxVec ProcIndices(1, getProcModel(RWModelDef).Index);
1437d9a4f0cbd28351155a7d9bd62aa08d8394435f82Andrew Trick          IdxVec Writes, Reads;
1438d9a4f0cbd28351155a7d9bd62aa08d8394435f82Andrew Trick          findRWs((*RWI)->getValueAsListOfDefs("OperandReadWrites"),
1439d9a4f0cbd28351155a7d9bd62aa08d8394435f82Andrew Trick                  Writes, Reads);
1440d9a4f0cbd28351155a7d9bd62aa08d8394435f82Andrew Trick          collectRWResources(Writes, Reads, ProcIndices);
1441d9a4f0cbd28351155a7d9bd62aa08d8394435f82Andrew Trick        }
1442d9a4f0cbd28351155a7d9bd62aa08d8394435f82Andrew Trick      }
14433cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick      collectRWResources(SCI->Writes, SCI->Reads, SCI->ProcIndices);
1444d9a4f0cbd28351155a7d9bd62aa08d8394435f82Andrew Trick    }
14453cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick  }
14463cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick  // Add resources separately defined by each subtarget.
14473cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick  RecVec WRDefs = Records.getAllDerivedDefinitions("WriteRes");
14483cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick  for (RecIter WRI = WRDefs.begin(), WRE = WRDefs.end(); WRI != WRE; ++WRI) {
14493cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick    Record *ModelDef = (*WRI)->getValueAsDef("SchedModel");
14503cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick    addWriteRes(*WRI, getProcModel(ModelDef).Index);
14513cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick  }
14523cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick  RecVec RADefs = Records.getAllDerivedDefinitions("ReadAdvance");
14533cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick  for (RecIter RAI = RADefs.begin(), RAE = RADefs.end(); RAI != RAE; ++RAI) {
14543cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick    Record *ModelDef = (*RAI)->getValueAsDef("SchedModel");
14553cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick    addReadAdvance(*RAI, getProcModel(ModelDef).Index);
14563cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick  }
14573cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick  // Finalize each ProcModel by sorting the record arrays.
14583cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick  for (unsigned PIdx = 0, PEnd = ProcModels.size(); PIdx != PEnd; ++PIdx) {
14593cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick    CodeGenProcModel &PM = ProcModels[PIdx];
14603cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick    std::sort(PM.WriteResDefs.begin(), PM.WriteResDefs.end(),
14613cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick              LessRecord());
14623cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick    std::sort(PM.ReadAdvanceDefs.begin(), PM.ReadAdvanceDefs.end(),
14633cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick              LessRecord());
14643cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick    std::sort(PM.ProcResourceDefs.begin(), PM.ProcResourceDefs.end(),
14653cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick              LessRecord());
14663cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick    DEBUG(
14673cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick      PM.dump();
14683cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick      dbgs() << "WriteResDefs: ";
14693cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick      for (RecIter RI = PM.WriteResDefs.begin(),
14703cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick             RE = PM.WriteResDefs.end(); RI != RE; ++RI) {
14713cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick        if ((*RI)->isSubClassOf("WriteRes"))
14723cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick          dbgs() << (*RI)->getValueAsDef("WriteType")->getName() << " ";
14733cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick        else
14743cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick          dbgs() << (*RI)->getName() << " ";
14753cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick      }
14763cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick      dbgs() << "\nReadAdvanceDefs: ";
14773cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick      for (RecIter RI = PM.ReadAdvanceDefs.begin(),
14783cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick             RE = PM.ReadAdvanceDefs.end(); RI != RE; ++RI) {
14793cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick        if ((*RI)->isSubClassOf("ReadAdvance"))
14803cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick          dbgs() << (*RI)->getValueAsDef("ReadType")->getName() << " ";
14813cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick        else
14823cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick          dbgs() << (*RI)->getName() << " ";
14833cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick      }
14843cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick      dbgs() << "\nProcResourceDefs: ";
14853cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick      for (RecIter RI = PM.ProcResourceDefs.begin(),
14863cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick             RE = PM.ProcResourceDefs.end(); RI != RE; ++RI) {
14873cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick        dbgs() << (*RI)->getName() << " ";
14883cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick      }
14893cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick      dbgs() << '\n');
1490e30f32a69ba57dfecbd670d971048bccaf727798Andrew Trick    verifyProcResourceGroups(PM);
14913cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick  }
14923cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick}
14933cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick
14943cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick// Collect itinerary class resources for each processor.
14953cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trickvoid CodeGenSchedModels::collectItinProcResources(Record *ItinClassDef) {
14963cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick  for (unsigned PIdx = 0, PEnd = ProcModels.size(); PIdx != PEnd; ++PIdx) {
14973cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick    const CodeGenProcModel &PM = ProcModels[PIdx];
14983cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick    // For all ItinRW entries.
14993cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick    bool HasMatch = false;
15003cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick    for (RecIter II = PM.ItinRWDefs.begin(), IE = PM.ItinRWDefs.end();
15013cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick         II != IE; ++II) {
15023cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick      RecVec Matched = (*II)->getValueAsListOfDefs("MatchedItinClasses");
15033cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick      if (!std::count(Matched.begin(), Matched.end(), ItinClassDef))
15043cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick        continue;
15053cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick      if (HasMatch)
150661131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger        PrintFatalError((*II)->getLoc(), "Duplicate itinerary class "
150761131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger                        + ItinClassDef->getName()
150861131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger                        + " in ItinResources for " + PM.ModelName);
15093cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick      HasMatch = true;
15103cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick      IdxVec Writes, Reads;
15113cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick      findRWs((*II)->getValueAsListOfDefs("OperandReadWrites"), Writes, Reads);
15123cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick      IdxVec ProcIndices(1, PIdx);
15133cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick      collectRWResources(Writes, Reads, ProcIndices);
15143cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick    }
15153cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick  }
15163cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick}
15173cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick
1518dbe6d43dfac78d567973dac8fc2a0190dad5135fAndrew Trickvoid CodeGenSchedModels::collectRWResources(unsigned RWIdx, bool IsRead,
15193cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick                                            const IdxVec &ProcIndices) {
1520dbe6d43dfac78d567973dac8fc2a0190dad5135fAndrew Trick  const CodeGenSchedRW &SchedRW = getSchedRW(RWIdx, IsRead);
1521dbe6d43dfac78d567973dac8fc2a0190dad5135fAndrew Trick  if (SchedRW.TheDef) {
1522dbe6d43dfac78d567973dac8fc2a0190dad5135fAndrew Trick    if (!IsRead && SchedRW.TheDef->isSubClassOf("SchedWriteRes")) {
15233cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick      for (IdxIter PI = ProcIndices.begin(), PE = ProcIndices.end();
15243cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick           PI != PE; ++PI) {
15253cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick        addWriteRes(SchedRW.TheDef, *PI);
15263cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick      }
15273cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick    }
1528dbe6d43dfac78d567973dac8fc2a0190dad5135fAndrew Trick    else if (IsRead && SchedRW.TheDef->isSubClassOf("SchedReadAdvance")) {
15293cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick      for (IdxIter PI = ProcIndices.begin(), PE = ProcIndices.end();
15303cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick           PI != PE; ++PI) {
15313cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick        addReadAdvance(SchedRW.TheDef, *PI);
15323cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick      }
15333cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick    }
1534dbe6d43dfac78d567973dac8fc2a0190dad5135fAndrew Trick  }
1535dbe6d43dfac78d567973dac8fc2a0190dad5135fAndrew Trick  for (RecIter AI = SchedRW.Aliases.begin(), AE = SchedRW.Aliases.end();
1536dbe6d43dfac78d567973dac8fc2a0190dad5135fAndrew Trick       AI != AE; ++AI) {
1537dbe6d43dfac78d567973dac8fc2a0190dad5135fAndrew Trick    IdxVec AliasProcIndices;
1538dbe6d43dfac78d567973dac8fc2a0190dad5135fAndrew Trick    if ((*AI)->getValueInit("SchedModel")->isComplete()) {
1539dbe6d43dfac78d567973dac8fc2a0190dad5135fAndrew Trick      AliasProcIndices.push_back(
1540dbe6d43dfac78d567973dac8fc2a0190dad5135fAndrew Trick        getProcModel((*AI)->getValueAsDef("SchedModel")).Index);
1541dbe6d43dfac78d567973dac8fc2a0190dad5135fAndrew Trick    }
1542dbe6d43dfac78d567973dac8fc2a0190dad5135fAndrew Trick    else
1543dbe6d43dfac78d567973dac8fc2a0190dad5135fAndrew Trick      AliasProcIndices = ProcIndices;
1544dbe6d43dfac78d567973dac8fc2a0190dad5135fAndrew Trick    const CodeGenSchedRW &AliasRW = getSchedRW((*AI)->getValueAsDef("AliasRW"));
1545dbe6d43dfac78d567973dac8fc2a0190dad5135fAndrew Trick    assert(AliasRW.IsRead == IsRead && "cannot alias reads to writes");
1546dbe6d43dfac78d567973dac8fc2a0190dad5135fAndrew Trick
1547dbe6d43dfac78d567973dac8fc2a0190dad5135fAndrew Trick    IdxVec ExpandedRWs;
1548dbe6d43dfac78d567973dac8fc2a0190dad5135fAndrew Trick    expandRWSequence(AliasRW.Index, ExpandedRWs, IsRead);
1549dbe6d43dfac78d567973dac8fc2a0190dad5135fAndrew Trick    for (IdxIter SI = ExpandedRWs.begin(), SE = ExpandedRWs.end();
1550dbe6d43dfac78d567973dac8fc2a0190dad5135fAndrew Trick         SI != SE; ++SI) {
1551dbe6d43dfac78d567973dac8fc2a0190dad5135fAndrew Trick      collectRWResources(*SI, IsRead, AliasProcIndices);
155292649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick    }
15533cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick  }
15543cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick}
15553cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick
1556dbe6d43dfac78d567973dac8fc2a0190dad5135fAndrew Trick// Collect resources for a set of read/write types and processor indices.
1557dbe6d43dfac78d567973dac8fc2a0190dad5135fAndrew Trickvoid CodeGenSchedModels::collectRWResources(const IdxVec &Writes,
1558dbe6d43dfac78d567973dac8fc2a0190dad5135fAndrew Trick                                            const IdxVec &Reads,
1559dbe6d43dfac78d567973dac8fc2a0190dad5135fAndrew Trick                                            const IdxVec &ProcIndices) {
1560dbe6d43dfac78d567973dac8fc2a0190dad5135fAndrew Trick
1561dbe6d43dfac78d567973dac8fc2a0190dad5135fAndrew Trick  for (IdxIter WI = Writes.begin(), WE = Writes.end(); WI != WE; ++WI)
1562dbe6d43dfac78d567973dac8fc2a0190dad5135fAndrew Trick    collectRWResources(*WI, /*IsRead=*/false, ProcIndices);
1563dbe6d43dfac78d567973dac8fc2a0190dad5135fAndrew Trick
1564dbe6d43dfac78d567973dac8fc2a0190dad5135fAndrew Trick  for (IdxIter RI = Reads.begin(), RE = Reads.end(); RI != RE; ++RI)
1565dbe6d43dfac78d567973dac8fc2a0190dad5135fAndrew Trick    collectRWResources(*RI, /*IsRead=*/true, ProcIndices);
1566dbe6d43dfac78d567973dac8fc2a0190dad5135fAndrew Trick}
1567dbe6d43dfac78d567973dac8fc2a0190dad5135fAndrew Trick
1568dbe6d43dfac78d567973dac8fc2a0190dad5135fAndrew Trick
15693cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick// Find the processor's resource units for this kind of resource.
15703cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew TrickRecord *CodeGenSchedModels::findProcResUnits(Record *ProcResKind,
15713cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick                                             const CodeGenProcModel &PM) const {
15723cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick  if (ProcResKind->isSubClassOf("ProcResourceUnits"))
15733cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick    return ProcResKind;
15743cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick
15753cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick  Record *ProcUnitDef = 0;
15763cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick  RecVec ProcResourceDefs =
15773cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick    Records.getAllDerivedDefinitions("ProcResourceUnits");
15783cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick
15793cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick  for (RecIter RI = ProcResourceDefs.begin(), RE = ProcResourceDefs.end();
15803cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick       RI != RE; ++RI) {
15813cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick
15823cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick    if ((*RI)->getValueAsDef("Kind") == ProcResKind
15833cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick        && (*RI)->getValueAsDef("SchedModel") == PM.ModelDef) {
15843cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick      if (ProcUnitDef) {
158561131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger        PrintFatalError((*RI)->getLoc(),
158661131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger                        "Multiple ProcessorResourceUnits associated with "
158761131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger                        + ProcResKind->getName());
15883cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick      }
15893cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick      ProcUnitDef = *RI;
15903cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick    }
15913cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick  }
15921754aca83af1658c832706889c0e2933f8dfa8eeAndrew Trick  RecVec ProcResGroups = Records.getAllDerivedDefinitions("ProcResGroup");
15931754aca83af1658c832706889c0e2933f8dfa8eeAndrew Trick  for (RecIter RI = ProcResGroups.begin(), RE = ProcResGroups.end();
15941754aca83af1658c832706889c0e2933f8dfa8eeAndrew Trick       RI != RE; ++RI) {
15951754aca83af1658c832706889c0e2933f8dfa8eeAndrew Trick
15961754aca83af1658c832706889c0e2933f8dfa8eeAndrew Trick    if (*RI == ProcResKind
15971754aca83af1658c832706889c0e2933f8dfa8eeAndrew Trick        && (*RI)->getValueAsDef("SchedModel") == PM.ModelDef) {
15981754aca83af1658c832706889c0e2933f8dfa8eeAndrew Trick      if (ProcUnitDef) {
15991754aca83af1658c832706889c0e2933f8dfa8eeAndrew Trick        PrintFatalError((*RI)->getLoc(),
16001754aca83af1658c832706889c0e2933f8dfa8eeAndrew Trick                        "Multiple ProcessorResourceUnits associated with "
16011754aca83af1658c832706889c0e2933f8dfa8eeAndrew Trick                        + ProcResKind->getName());
16021754aca83af1658c832706889c0e2933f8dfa8eeAndrew Trick      }
16031754aca83af1658c832706889c0e2933f8dfa8eeAndrew Trick      ProcUnitDef = *RI;
16041754aca83af1658c832706889c0e2933f8dfa8eeAndrew Trick    }
16051754aca83af1658c832706889c0e2933f8dfa8eeAndrew Trick  }
16063cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick  if (!ProcUnitDef) {
160761131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger    PrintFatalError(ProcResKind->getLoc(),
160861131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger                    "No ProcessorResources associated with "
160961131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger                    + ProcResKind->getName());
16103cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick  }
16113cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick  return ProcUnitDef;
16123cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick}
16133cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick
16143cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick// Iteratively add a resource and its super resources.
16153cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trickvoid CodeGenSchedModels::addProcResource(Record *ProcResKind,
16163cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick                                         CodeGenProcModel &PM) {
16173cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick  for (;;) {
16183cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick    Record *ProcResUnits = findProcResUnits(ProcResKind, PM);
16193cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick
16203cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick    // See if this ProcResource is already associated with this processor.
16213cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick    RecIter I = std::find(PM.ProcResourceDefs.begin(),
16223cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick                          PM.ProcResourceDefs.end(), ProcResUnits);
16233cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick    if (I != PM.ProcResourceDefs.end())
16243cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick      return;
16253cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick
16263cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick    PM.ProcResourceDefs.push_back(ProcResUnits);
16271754aca83af1658c832706889c0e2933f8dfa8eeAndrew Trick    if (ProcResUnits->isSubClassOf("ProcResGroup"))
16281754aca83af1658c832706889c0e2933f8dfa8eeAndrew Trick      return;
16291754aca83af1658c832706889c0e2933f8dfa8eeAndrew Trick
16303cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick    if (!ProcResUnits->getValueInit("Super")->isComplete())
16313cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick      return;
16323cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick
16333cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick    ProcResKind = ProcResUnits->getValueAsDef("Super");
16343cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick  }
16353cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick}
16363cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick
16373cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick// Add resources for a SchedWrite to this processor if they don't exist.
16383cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trickvoid CodeGenSchedModels::addWriteRes(Record *ProcWriteResDef, unsigned PIdx) {
163992649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick  assert(PIdx && "don't add resources to an invalid Processor model");
164092649883119aaa8edd9ccf612eaaff5ccc8fcc77Andrew Trick
16413cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick  RecVec &WRDefs = ProcModels[PIdx].WriteResDefs;
16423cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick  RecIter WRI = std::find(WRDefs.begin(), WRDefs.end(), ProcWriteResDef);
16433cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick  if (WRI != WRDefs.end())
16443cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick    return;
16453cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick  WRDefs.push_back(ProcWriteResDef);
16463cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick
16473cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick  // Visit ProcResourceKinds referenced by the newly discovered WriteRes.
16483cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick  RecVec ProcResDefs = ProcWriteResDef->getValueAsListOfDefs("ProcResources");
16493cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick  for (RecIter WritePRI = ProcResDefs.begin(), WritePRE = ProcResDefs.end();
16503cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick       WritePRI != WritePRE; ++WritePRI) {
16513cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick    addProcResource(*WritePRI, ProcModels[PIdx]);
16523cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick  }
16533cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick}
16543cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick
16553cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick// Add resources for a ReadAdvance to this processor if they don't exist.
16563cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trickvoid CodeGenSchedModels::addReadAdvance(Record *ProcReadAdvanceDef,
16573cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick                                        unsigned PIdx) {
16583cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick  RecVec &RADefs = ProcModels[PIdx].ReadAdvanceDefs;
16593cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick  RecIter I = std::find(RADefs.begin(), RADefs.end(), ProcReadAdvanceDef);
16603cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick  if (I != RADefs.end())
16613cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick    return;
16623cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick  RADefs.push_back(ProcReadAdvanceDef);
16633cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick}
16643cbd1786ac06fe751dc4b5ad55e75115cb1d51ceAndrew Trick
1665bc4ff6e3cf129294d10c2366cb157796964c903eAndrew Trickunsigned CodeGenProcModel::getProcResourceIdx(Record *PRDef) const {
1666bc4ff6e3cf129294d10c2366cb157796964c903eAndrew Trick  RecIter PRPos = std::find(ProcResourceDefs.begin(), ProcResourceDefs.end(),
1667bc4ff6e3cf129294d10c2366cb157796964c903eAndrew Trick                            PRDef);
1668bc4ff6e3cf129294d10c2366cb157796964c903eAndrew Trick  if (PRPos == ProcResourceDefs.end())
166961131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger    PrintFatalError(PRDef->getLoc(), "ProcResource def is not included in "
167061131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger                    "the ProcResources list for " + ModelName);
1671bc4ff6e3cf129294d10c2366cb157796964c903eAndrew Trick  // Idx=0 is reserved for invalid.
1672322ff8834532915a4de0210a083672008d6499b6Rafael Espindola  return 1 + (PRPos - ProcResourceDefs.begin());
1673bc4ff6e3cf129294d10c2366cb157796964c903eAndrew Trick}
1674bc4ff6e3cf129294d10c2366cb157796964c903eAndrew Trick
16752661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick#ifndef NDEBUG
167648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trickvoid CodeGenProcModel::dump() const {
167748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  dbgs() << Index << ": " << ModelName << " "
167848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick         << (ModelDef ? ModelDef->getName() : "inferred") << " "
167948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick         << (ItinsDef ? ItinsDef->getName() : "no itinerary") << '\n';
168048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick}
168148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick
168248605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trickvoid CodeGenSchedRW::dump() const {
168348605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  dbgs() << Name << (IsVariadic ? " (V) " : " ");
168448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  if (IsSequence) {
168548605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    dbgs() << "(";
168648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    dumpIdxVec(Sequence);
168748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    dbgs() << ")";
16882661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick  }
16892661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick}
169048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick
169148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trickvoid CodeGenSchedClass::dump(const CodeGenSchedModels* SchedModels) const {
16921ab961f6d3cdd284f5d6c696f3e26eb3627e2c8bAndrew Trick  dbgs() << "SCHEDCLASS " << Index << ":" << Name << '\n'
169348605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick         << "  Writes: ";
169448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  for (unsigned i = 0, N = Writes.size(); i < N; ++i) {
169548605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    SchedModels->getSchedWrite(Writes[i]).dump();
169648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    if (i < N-1) {
169748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      dbgs() << '\n';
169848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      dbgs().indent(10);
169948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    }
170048605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  }
170148605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  dbgs() << "\n  Reads: ";
170248605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  for (unsigned i = 0, N = Reads.size(); i < N; ++i) {
170348605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    SchedModels->getSchedRead(Reads[i]).dump();
170448605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    if (i < N-1) {
170548605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      dbgs() << '\n';
170648605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick      dbgs().indent(10);
170748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick    }
170848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  }
170948605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick  dbgs() << "\n  ProcIdx: "; dumpIdxVec(ProcIndices); dbgs() << '\n';
171082e7c4f533a98a16b0cadd5209c1d3dc8ce33d87Andrew Trick  if (!Transitions.empty()) {
171182e7c4f533a98a16b0cadd5209c1d3dc8ce33d87Andrew Trick    dbgs() << "\n Transitions for Proc ";
171282e7c4f533a98a16b0cadd5209c1d3dc8ce33d87Andrew Trick    for (std::vector<CodeGenSchedTransition>::const_iterator
171382e7c4f533a98a16b0cadd5209c1d3dc8ce33d87Andrew Trick           TI = Transitions.begin(), TE = Transitions.end(); TI != TE; ++TI) {
171482e7c4f533a98a16b0cadd5209c1d3dc8ce33d87Andrew Trick      dumpIdxVec(TI->ProcIndices);
171582e7c4f533a98a16b0cadd5209c1d3dc8ce33d87Andrew Trick    }
171682e7c4f533a98a16b0cadd5209c1d3dc8ce33d87Andrew Trick  }
171748605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick}
17185e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick
17195e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trickvoid PredTransitions::dump() const {
17205e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  dbgs() << "Expanded Variants:\n";
17215e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  for (std::vector<PredTransition>::const_iterator
17225e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick         TI = TransVec.begin(), TE = TransVec.end(); TI != TE; ++TI) {
17235e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    dbgs() << "{";
17245e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    for (SmallVectorImpl<PredCheck>::const_iterator
17255e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick           PCI = TI->PredTerm.begin(), PCE = TI->PredTerm.end();
17265e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick         PCI != PCE; ++PCI) {
17275e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick      if (PCI != TI->PredTerm.begin())
17285e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick        dbgs() << ", ";
17295e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick      dbgs() << SchedModels.getSchedRW(PCI->RWIdx, PCI->IsRead).Name
17305e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick             << ":" << PCI->Predicate->getName();
17315e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    }
17325e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    dbgs() << "},\n  => {";
17335e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    for (SmallVectorImpl<SmallVector<unsigned,4> >::const_iterator
17345e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick           WSI = TI->WriteSequences.begin(), WSE = TI->WriteSequences.end();
17355e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick         WSI != WSE; ++WSI) {
17365e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick      dbgs() << "(";
17375e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick      for (SmallVectorImpl<unsigned>::const_iterator
17385e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick             WI = WSI->begin(), WE = WSI->end(); WI != WE; ++WI) {
17395e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick        if (WI != WSI->begin())
17405e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick          dbgs() << ", ";
17415e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick        dbgs() << SchedModels.getSchedWrite(*WI).Name;
17425e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick      }
17435e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick      dbgs() << "),";
17445e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    }
17455e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick    dbgs() << "}\n";
17465e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick  }
17475e613c260bb3044eb059dea74cd6bccfa9b85bddAndrew Trick}
174848605c340614fc1fb2ae1d975fc565a4188182e0Andrew Trick#endif // NDEBUG
1749