+void DAGISelEmitter::ParsePatterns() {
+ std::vector<Record*> Patterns = Records.getAllDerivedDefinitions("Pattern");
+
+ for (unsigned i = 0, e = Patterns.size(); i != e; ++i) {
+ DagInit *Tree = Patterns[i]->getValueAsDag("PatternToMatch");
+ TreePattern *Pattern = new TreePattern(Patterns[i], Tree, *this);
+
+ // Inline pattern fragments into it.
+ Pattern->InlinePatternFragments();
+
+ // Infer as many types as possible. If we cannot infer all of them, we can
+ // never do anything with this pattern: report it to the user.
+ if (!Pattern->InferAllTypes())
+ Pattern->error("Could not infer all types in pattern!");
+
+ ListInit *LI = Patterns[i]->getValueAsListInit("ResultInstrs");
+ if (LI->getSize() == 0) continue; // no pattern.
+
+ // Parse the instruction.
+ TreePattern *Result = new TreePattern(Patterns[i], LI, *this);
+
+ // Inline pattern fragments into it.
+ Result->InlinePatternFragments();
+
+ // Infer as many types as possible. If we cannot infer all of them, we can
+ // never do anything with this pattern: report it to the user.
+ if (!Result->InferAllTypes())
+ Result->error("Could not infer all types in pattern result!");
+
+ if (Result->getNumTrees() != 1)
+ Result->error("Cannot handle instructions producing instructions "
+ "with temporaries yet!");
+
+ std::string Reason;
+ if (!Pattern->getOnlyTree()->canPatternMatch(Reason, *this))
+ Pattern->error("Pattern can never match: " + Reason);
+
+ PatternsToMatch.push_back(std::make_pair(Pattern->getOnlyTree(),
+ Result->getOnlyTree()));
+ }
+}
+
+/// CombineChildVariants - Given a bunch of permutations of each child of the
+/// 'operator' node, put them together in all possible ways.
+static void CombineChildVariants(TreePatternNode *Orig,
+ const std::vector<std::vector<TreePatternNode*> > &ChildVariants,
+ std::vector<TreePatternNode*> &OutVariants,
+ DAGISelEmitter &ISE) {
+ // Make sure that each operand has at least one variant to choose from.
+ for (unsigned i = 0, e = ChildVariants.size(); i != e; ++i)
+ if (ChildVariants[i].empty())
+ return;
+
+ // The end result is an all-pairs construction of the resultant pattern.
+ std::vector<unsigned> Idxs;
+ Idxs.resize(ChildVariants.size());
+ bool NotDone = true;
+ while (NotDone) {
+ // Create the variant and add it to the output list.
+ std::vector<TreePatternNode*> NewChildren;
+ for (unsigned i = 0, e = ChildVariants.size(); i != e; ++i)
+ NewChildren.push_back(ChildVariants[i][Idxs[i]]);
+ TreePatternNode *R = new TreePatternNode(Orig->getOperator(), NewChildren);
+
+ // Copy over properties.
+ R->setName(Orig->getName());
+ R->setPredicateFn(Orig->getPredicateFn());
+ R->setTransformFn(Orig->getTransformFn());
+ R->setType(Orig->getExtType());
+
+ // If this pattern cannot every match, do not include it as a variant.
+ std::string ErrString;
+ if (!R->canPatternMatch(ErrString, ISE)) {
+ delete R;
+ } else {
+ bool AlreadyExists = false;
+
+ // Scan to see if this pattern has already been emitted. We can get
+ // duplication due to things like commuting:
+ // (and GPRC:$a, GPRC:$b) -> (and GPRC:$b, GPRC:$a)
+ // which are the same pattern. Ignore the dups.
+ for (unsigned i = 0, e = OutVariants.size(); i != e; ++i)
+ if (R->isIsomorphicTo(OutVariants[i])) {
+ AlreadyExists = true;
+ break;
+ }
+
+ if (AlreadyExists)
+ delete R;
+ else
+ OutVariants.push_back(R);
+ }
+
+ // Increment indices to the next permutation.
+ NotDone = false;
+ // Look for something we can increment without causing a wrap-around.
+ for (unsigned IdxsIdx = 0; IdxsIdx != Idxs.size(); ++IdxsIdx) {
+ if (++Idxs[IdxsIdx] < ChildVariants[IdxsIdx].size()) {
+ NotDone = true; // Found something to increment.
+ break;
+ }
+ Idxs[IdxsIdx] = 0;
+ }
+ }
+}
+
+/// CombineChildVariants - A helper function for binary operators.
+///
+static void CombineChildVariants(TreePatternNode *Orig,
+ const std::vector<TreePatternNode*> &LHS,
+ const std::vector<TreePatternNode*> &RHS,
+ std::vector<TreePatternNode*> &OutVariants,
+ DAGISelEmitter &ISE) {
+ std::vector<std::vector<TreePatternNode*> > ChildVariants;
+ ChildVariants.push_back(LHS);
+ ChildVariants.push_back(RHS);
+ CombineChildVariants(Orig, ChildVariants, OutVariants, ISE);
+}
+
+
+static void GatherChildrenOfAssociativeOpcode(TreePatternNode *N,
+ std::vector<TreePatternNode *> &Children) {
+ assert(N->getNumChildren()==2 &&"Associative but doesn't have 2 children!");
+ Record *Operator = N->getOperator();
+
+ // Only permit raw nodes.
+ if (!N->getName().empty() || !N->getPredicateFn().empty() ||
+ N->getTransformFn()) {
+ Children.push_back(N);
+ return;
+ }
+
+ if (N->getChild(0)->isLeaf() || N->getChild(0)->getOperator() != Operator)
+ Children.push_back(N->getChild(0));
+ else
+ GatherChildrenOfAssociativeOpcode(N->getChild(0), Children);
+
+ if (N->getChild(1)->isLeaf() || N->getChild(1)->getOperator() != Operator)
+ Children.push_back(N->getChild(1));
+ else
+ GatherChildrenOfAssociativeOpcode(N->getChild(1), Children);
+}
+
+/// GenerateVariantsOf - Given a pattern N, generate all permutations we can of
+/// the (potentially recursive) pattern by using algebraic laws.
+///
+static void GenerateVariantsOf(TreePatternNode *N,
+ std::vector<TreePatternNode*> &OutVariants,
+ DAGISelEmitter &ISE) {
+ // We cannot permute leaves.
+ if (N->isLeaf()) {
+ OutVariants.push_back(N);
+ return;
+ }
+
+ // Look up interesting info about the node.
+ const SDNodeInfo &NodeInfo = ISE.getSDNodeInfo(N->getOperator());
+
+ // If this node is associative, reassociate.
+ if (NodeInfo.hasProperty(SDNodeInfo::SDNPAssociative)) {
+ // Reassociate by pulling together all of the linked operators
+ std::vector<TreePatternNode*> MaximalChildren;
+ GatherChildrenOfAssociativeOpcode(N, MaximalChildren);
+
+ // Only handle child sizes of 3. Otherwise we'll end up trying too many
+ // permutations.
+ if (MaximalChildren.size() == 3) {
+ // Find the variants of all of our maximal children.
+ std::vector<TreePatternNode*> AVariants, BVariants, CVariants;
+ GenerateVariantsOf(MaximalChildren[0], AVariants, ISE);
+ GenerateVariantsOf(MaximalChildren[1], BVariants, ISE);
+ GenerateVariantsOf(MaximalChildren[2], CVariants, ISE);
+
+ // There are only two ways we can permute the tree:
+ // (A op B) op C and A op (B op C)
+ // Within these forms, we can also permute A/B/C.
+
+ // Generate legal pair permutations of A/B/C.
+ std::vector<TreePatternNode*> ABVariants;
+ std::vector<TreePatternNode*> BAVariants;
+ std::vector<TreePatternNode*> ACVariants;
+ std::vector<TreePatternNode*> CAVariants;
+ std::vector<TreePatternNode*> BCVariants;
+ std::vector<TreePatternNode*> CBVariants;
+ CombineChildVariants(N, AVariants, BVariants, ABVariants, ISE);
+ CombineChildVariants(N, BVariants, AVariants, BAVariants, ISE);
+ CombineChildVariants(N, AVariants, CVariants, ACVariants, ISE);
+ CombineChildVariants(N, CVariants, AVariants, CAVariants, ISE);
+ CombineChildVariants(N, BVariants, CVariants, BCVariants, ISE);
+ CombineChildVariants(N, CVariants, BVariants, CBVariants, ISE);
+
+ // Combine those into the result: (x op x) op x
+ CombineChildVariants(N, ABVariants, CVariants, OutVariants, ISE);
+ CombineChildVariants(N, BAVariants, CVariants, OutVariants, ISE);
+ CombineChildVariants(N, ACVariants, BVariants, OutVariants, ISE);
+ CombineChildVariants(N, CAVariants, BVariants, OutVariants, ISE);
+ CombineChildVariants(N, BCVariants, AVariants, OutVariants, ISE);
+ CombineChildVariants(N, CBVariants, AVariants, OutVariants, ISE);
+
+ // Combine those into the result: x op (x op x)
+ CombineChildVariants(N, CVariants, ABVariants, OutVariants, ISE);
+ CombineChildVariants(N, CVariants, BAVariants, OutVariants, ISE);
+ CombineChildVariants(N, BVariants, ACVariants, OutVariants, ISE);
+ CombineChildVariants(N, BVariants, CAVariants, OutVariants, ISE);
+ CombineChildVariants(N, AVariants, BCVariants, OutVariants, ISE);
+ CombineChildVariants(N, AVariants, CBVariants, OutVariants, ISE);
+ return;
+ }
+ }
+
+ // Compute permutations of all children.
+ std::vector<std::vector<TreePatternNode*> > ChildVariants;
+ ChildVariants.resize(N->getNumChildren());
+ for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
+ GenerateVariantsOf(N->getChild(i), ChildVariants[i], ISE);
+
+ // Build all permutations based on how the children were formed.
+ CombineChildVariants(N, ChildVariants, OutVariants, ISE);
+
+ // If this node is commutative, consider the commuted order.
+ if (NodeInfo.hasProperty(SDNodeInfo::SDNPCommutative)) {
+ assert(N->getNumChildren()==2 &&"Commutative but doesn't have 2 children!");
+ // Consider the commuted order.
+ CombineChildVariants(N, ChildVariants[1], ChildVariants[0],
+ OutVariants, ISE);
+ }
+}
+
+
+// GenerateVariants - Generate variants. For example, commutative patterns can
+// match multiple ways. Add them to PatternsToMatch as well.
+void DAGISelEmitter::GenerateVariants() {
+
+ DEBUG(std::cerr << "Generating instruction variants.\n");
+
+ // Loop over all of the patterns we've collected, checking to see if we can
+ // generate variants of the instruction, through the exploitation of
+ // identities. This permits the target to provide agressive matching without
+ // the .td file having to contain tons of variants of instructions.
+ //
+ // Note that this loop adds new patterns to the PatternsToMatch list, but we
+ // intentionally do not reconsider these. Any variants of added patterns have
+ // already been added.
+ //
+ for (unsigned i = 0, e = PatternsToMatch.size(); i != e; ++i) {
+ std::vector<TreePatternNode*> Variants;
+ GenerateVariantsOf(PatternsToMatch[i].first, Variants, *this);
+
+ assert(!Variants.empty() && "Must create at least original variant!");
+ Variants.erase(Variants.begin()); // Remove the original pattern.
+
+ if (Variants.empty()) // No variants for this pattern.
+ continue;
+
+ DEBUG(std::cerr << "FOUND VARIANTS OF: ";
+ PatternsToMatch[i].first->dump();
+ std::cerr << "\n");
+
+ for (unsigned v = 0, e = Variants.size(); v != e; ++v) {
+ TreePatternNode *Variant = Variants[v];
+
+ DEBUG(std::cerr << " VAR#" << v << ": ";
+ Variant->dump();
+ std::cerr << "\n");
+
+ // Scan to see if an instruction or explicit pattern already matches this.
+ bool AlreadyExists = false;
+ for (unsigned p = 0, e = PatternsToMatch.size(); p != e; ++p) {
+ // Check to see if this variant already exists.
+ if (Variant->isIsomorphicTo(PatternsToMatch[p].first)) {
+ DEBUG(std::cerr << " *** ALREADY EXISTS, ignoring variant.\n");
+ AlreadyExists = true;
+ break;
+ }
+ }
+ // If we already have it, ignore the variant.
+ if (AlreadyExists) continue;
+
+ // Otherwise, add it to the list of patterns we have.
+ PatternsToMatch.push_back(std::make_pair(Variant,
+ PatternsToMatch[i].second));
+ }
+
+ DEBUG(std::cerr << "\n");
+ }
+}
+
+
+/// getPatternSize - Return the 'size' of this pattern. We want to match large
+/// patterns before small ones. This is used to determine the size of a
+/// pattern.
+static unsigned getPatternSize(TreePatternNode *P) {
+ assert(isExtIntegerVT(P->getExtType()) ||
+ isExtFloatingPointVT(P->getExtType()) &&
+ "Not a valid pattern node to size!");
+ unsigned Size = 1; // The node itself.
+
+ // Count children in the count if they are also nodes.
+ for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i) {
+ TreePatternNode *Child = P->getChild(i);
+ if (!Child->isLeaf() && Child->getExtType() != MVT::Other)
+ Size += getPatternSize(Child);
+ }
+
+ return Size;
+}
+
+/// getResultPatternCost - Compute the number of instructions for this pattern.
+/// This is a temporary hack. We should really include the instruction
+/// latencies in this calculation.
+static unsigned getResultPatternCost(TreePatternNode *P) {
+ if (P->isLeaf()) return 0;
+
+ unsigned Cost = P->getOperator()->isSubClassOf("Instruction");
+ for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i)
+ Cost += getResultPatternCost(P->getChild(i));
+ return Cost;
+}
+
+// PatternSortingPredicate - return true if we prefer to match LHS before RHS.
+// In particular, we want to match maximal patterns first and lowest cost within
+// a particular complexity first.
+struct PatternSortingPredicate {
+ bool operator()(DAGISelEmitter::PatternToMatch *LHS,
+ DAGISelEmitter::PatternToMatch *RHS) {
+ unsigned LHSSize = getPatternSize(LHS->first);
+ unsigned RHSSize = getPatternSize(RHS->first);
+ if (LHSSize > RHSSize) return true; // LHS -> bigger -> less cost
+ if (LHSSize < RHSSize) return false;
+
+ // If the patterns have equal complexity, compare generated instruction cost
+ return getResultPatternCost(LHS->second) <getResultPatternCost(RHS->second);
+ }
+};
+
+/// EmitMatchForPattern - Emit a matcher for N, going to the label for PatternNo
+/// if the match fails. At this point, we already know that the opcode for N
+/// matches, and the SDNode for the result has the RootName specified name.
+void DAGISelEmitter::EmitMatchForPattern(TreePatternNode *N,
+ const std::string &RootName,
+ std::map<std::string,std::string> &VarMap,
+ unsigned PatternNo, std::ostream &OS) {
+ assert(!N->isLeaf() && "Cannot match against a leaf!");
+
+ // If this node has a name associated with it, capture it in VarMap. If
+ // we already saw this in the pattern, emit code to verify dagness.
+ if (!N->getName().empty()) {
+ std::string &VarMapEntry = VarMap[N->getName()];
+ if (VarMapEntry.empty()) {
+ VarMapEntry = RootName;
+ } else {
+ // If we get here, this is a second reference to a specific name. Since
+ // we already have checked that the first reference is valid, we don't
+ // have to recursively match it, just check that it's the same as the
+ // previously named thing.
+ OS << " if (" << VarMapEntry << " != " << RootName
+ << ") goto P" << PatternNo << "Fail;\n";
+ return;
+ }
+ }
+
+ // Emit code to load the child nodes and match their contents recursively.
+ for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) {
+ OS << " SDOperand " << RootName << i <<" = " << RootName
+ << ".getOperand(" << i << ");\n";
+ TreePatternNode *Child = N->getChild(i);
+
+ if (!Child->isLeaf()) {
+ // If it's not a leaf, recursively match.
+ const SDNodeInfo &CInfo = getSDNodeInfo(Child->getOperator());
+ OS << " if (" << RootName << i << ".getOpcode() != "
+ << CInfo.getEnumName() << ") goto P" << PatternNo << "Fail;\n";
+ EmitMatchForPattern(Child, RootName + utostr(i), VarMap, PatternNo, OS);
+ } else {
+ // If this child has a name associated with it, capture it in VarMap. If
+ // we already saw this in the pattern, emit code to verify dagness.
+ if (!Child->getName().empty()) {
+ std::string &VarMapEntry = VarMap[Child->getName()];
+ if (VarMapEntry.empty()) {
+ VarMapEntry = RootName + utostr(i);
+ } else {
+ // If we get here, this is a second reference to a specific name. Since
+ // we already have checked that the first reference is valid, we don't
+ // have to recursively match it, just check that it's the same as the
+ // previously named thing.
+ OS << " if (" << VarMapEntry << " != " << RootName << i
+ << ") goto P" << PatternNo << "Fail;\n";
+ continue;
+ }
+ }
+
+ // Handle leaves of various types.
+ Init *LeafVal = Child->getLeafValue();
+ Record *LeafRec = dynamic_cast<DefInit*>(LeafVal)->getDef();
+ if (LeafRec->isSubClassOf("RegisterClass")) {
+ // Handle register references. Nothing to do here.
+ } else if (LeafRec->isSubClassOf("ValueType")) {
+ // Make sure this is the specified value type.
+ OS << " if (cast<VTSDNode>(" << RootName << i << ")->getVT() != "
+ << "MVT::" << LeafRec->getName() << ") goto P" << PatternNo
+ << "Fail;\n";
+ } else {
+ Child->dump();
+ assert(0 && "Unknown leaf type!");
+ }
+ }
+ }
+
+ // If there is a node predicate for this, emit the call.
+ if (!N->getPredicateFn().empty())
+ OS << " if (!" << N->getPredicateFn() << "(" << RootName
+ << ".Val)) goto P" << PatternNo << "Fail;\n";
+}
+
+/// CodeGenPatternResult - Emit the action for a pattern. Now that it has
+/// matched, we actually have to build a DAG!
+unsigned DAGISelEmitter::
+CodeGenPatternResult(TreePatternNode *N, unsigned &Ctr,
+ std::map<std::string,std::string> &VariableMap,
+ std::ostream &OS) {
+ // This is something selected from the pattern we matched.
+ if (!N->getName().empty()) {
+ std::string &Val = VariableMap[N->getName()];
+ assert(!Val.empty() &&
+ "Variable referenced but not defined and not caught earlier!");
+ if (Val[0] == 'T' && Val[1] == 'm' && Val[2] == 'p') {
+ // Already selected this operand, just return the tmpval.
+ return atoi(Val.c_str()+3);
+ }
+
+ unsigned ResNo = Ctr++;
+ if (!N->isLeaf() && N->getOperator()->getName() == "imm") {
+ switch (N->getType()) {
+ default: assert(0 && "Unknown type for constant node!");
+ case MVT::i1: OS << " bool Tmp"; break;
+ case MVT::i8: OS << " unsigned char Tmp"; break;
+ case MVT::i16: OS << " unsigned short Tmp"; break;
+ case MVT::i32: OS << " unsigned Tmp"; break;
+ case MVT::i64: OS << " uint64_t Tmp"; break;
+ }
+ OS << ResNo << "C = cast<ConstantSDNode>(" << Val << ")->getValue();\n";
+ OS << " SDOperand Tmp" << ResNo << " = CurDAG->getTargetConstant(Tmp"
+ << ResNo << "C, MVT::" << getEnumName(N->getType()) << ");\n";
+ } else {
+ OS << " SDOperand Tmp" << ResNo << " = Select(" << Val << ");\n";
+ }
+ // Add Tmp<ResNo> to VariableMap, so that we don't multiply select this
+ // value if used multiple times by this pattern result.
+ Val = "Tmp"+utostr(ResNo);
+ return ResNo;
+ }
+
+ if (N->isLeaf()) {
+ N->dump();
+ assert(0 && "Unknown leaf type!");
+ return ~0U;
+ }
+
+ Record *Op = N->getOperator();
+ if (Op->isSubClassOf("Instruction")) {
+ // Emit all of the operands.
+ std::vector<unsigned> Ops;
+ for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
+ Ops.push_back(CodeGenPatternResult(N->getChild(i), Ctr, VariableMap, OS));
+
+ CodeGenInstruction &II = Target.getInstruction(Op->getName());
+ unsigned ResNo = Ctr++;
+
+ OS << " SDOperand Tmp" << ResNo << " = CurDAG->getTargetNode("
+ << II.Namespace << "::" << II.TheDef->getName() << ", MVT::"
+ << getEnumName(N->getType());
+ for (unsigned i = 0, e = Ops.size(); i != e; ++i)
+ OS << ", Tmp" << Ops[i];
+ OS << ");\n";
+ return ResNo;
+ } else if (Op->isSubClassOf("SDNodeXForm")) {
+ assert(N->getNumChildren() == 1 && "node xform should have one child!");
+ unsigned OpVal = CodeGenPatternResult(N->getChild(0), Ctr, VariableMap, OS);
+
+ unsigned ResNo = Ctr++;
+ OS << " SDOperand Tmp" << ResNo << " = Transform_" << Op->getName()
+ << "(Tmp" << OpVal << ".Val);\n";
+ return ResNo;
+ } else {
+ N->dump();
+ assert(0 && "Unknown node in result pattern!");
+ return ~0U;
+ }
+}
+
+/// RemoveAllTypes - A quick recursive walk over a pattern which removes all
+/// type information from it.
+static void RemoveAllTypes(TreePatternNode *N) {
+ N->setType(MVT::isUnknown);
+ if (!N->isLeaf())
+ for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
+ RemoveAllTypes(N->getChild(i));
+}
+
+/// EmitCodeForPattern - Given a pattern to match, emit code to the specified
+/// stream to match the pattern, and generate the code for the match if it
+/// succeeds.
+void DAGISelEmitter::EmitCodeForPattern(PatternToMatch &Pattern,
+ std::ostream &OS) {
+ static unsigned PatternCount = 0;
+ unsigned PatternNo = PatternCount++;
+ OS << " { // Pattern #" << PatternNo << ": ";
+ Pattern.first->print(OS);
+ OS << "\n // Emits: ";
+ Pattern.second->print(OS);
+ OS << "\n";
+ OS << " // Pattern complexity = " << getPatternSize(Pattern.first)
+ << " cost = " << getResultPatternCost(Pattern.second) << "\n";
+
+ // Emit the matcher, capturing named arguments in VariableMap.
+ std::map<std::string,std::string> VariableMap;
+ EmitMatchForPattern(Pattern.first, "N", VariableMap, PatternNo, OS);
+
+ // TP - Get *SOME* tree pattern, we don't care which.
+ TreePattern &TP = *PatternFragments.begin()->second;
+
+ // At this point, we know that we structurally match the pattern, but the
+ // types of the nodes may not match. Figure out the fewest number of type
+ // comparisons we need to emit. For example, if there is only one integer
+ // type supported by a target, there should be no type comparisons at all for
+ // integer patterns!
+ //
+ // To figure out the fewest number of type checks needed, clone the pattern,
+ // remove the types, then perform type inference on the pattern as a whole.
+ // If there are unresolved types, emit an explicit check for those types,
+ // apply the type to the tree, then rerun type inference. Iterate until all
+ // types are resolved.
+ //
+ TreePatternNode *Pat = Pattern.first->clone();
+ RemoveAllTypes(Pat);
+ bool MadeChange = true;
+ try {
+ while (MadeChange)
+ MadeChange = Pat->ApplyTypeConstraints(TP,true/*Ignore reg constraints*/);
+ } catch (...) {
+ assert(0 && "Error: could not find consistent types for something we"
+ " already decided was ok!");
+ abort();
+ }
+
+ if (!Pat->ContainsUnresolvedType()) {
+ unsigned TmpNo = 0;
+ unsigned Res = CodeGenPatternResult(Pattern.second, TmpNo, VariableMap, OS);
+
+ // Add the result to the map if it has multiple uses.
+ OS << " if (!N.Val->hasOneUse()) CodeGenMap[N] = Tmp" << Res << ";\n";
+ OS << " return Tmp" << Res << ";\n";
+ }
+
+ delete Pat;
+
+ OS << " }\n P" << PatternNo << "Fail:\n";
+}
+
+
+namespace {
+ /// CompareByRecordName - An ordering predicate that implements less-than by
+ /// comparing the names records.
+ struct CompareByRecordName {
+ bool operator()(const Record *LHS, const Record *RHS) const {
+ // Sort by name first.
+ if (LHS->getName() < RHS->getName()) return true;
+ // If both names are equal, sort by pointer.
+ return LHS->getName() == RHS->getName() && LHS < RHS;
+ }
+ };
+}
+