From 05814af29f67facfaf56168551ac9421bee04a1c Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Wed, 28 Sep 2005 17:57:56 +0000 Subject: [PATCH] Prefer cheaper patterns to more expensive ones. Print the costs to the generated file git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23492 91177308-0d34-0410-b5e6-96231b3b80d8 --- utils/TableGen/DAGISelEmitter.cpp | 89 +++++++++++++++++-------------- 1 file changed, 50 insertions(+), 39 deletions(-) diff --git a/utils/TableGen/DAGISelEmitter.cpp b/utils/TableGen/DAGISelEmitter.cpp index e915ee57935..9f5e387b169 100644 --- a/utils/TableGen/DAGISelEmitter.cpp +++ b/utils/TableGen/DAGISelEmitter.cpp @@ -995,6 +995,52 @@ void DAGISelEmitter::ParsePatterns() { }); } +/// 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(MVT::isInteger(P->getType()) || MVT::isFloatingPoint(P->getType()) && + "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->getType() != 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) 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. @@ -1160,16 +1206,16 @@ void DAGISelEmitter::EmitCodeForPattern(PatternToMatch &Pattern, 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 VariableMap; EmitMatchForPattern(Pattern.first, "N", VariableMap, PatternNo, OS); - OS << " // Emit: "; - Pattern.second->print(OS); - OS << "\n"; - unsigned TmpNo = 0; unsigned Res = CodeGenPatternResult(Pattern.second, TmpNo, VariableMap, OS); @@ -1179,41 +1225,6 @@ void DAGISelEmitter::EmitCodeForPattern(PatternToMatch &Pattern, OS << " }\n P" << PatternNo << "Fail:\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(MVT::isInteger(P->getType()) || MVT::isFloatingPoint(P->getType()) && - "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->getType() != MVT::Other) - Size += getPatternSize(Child); - } - - return Size; -} - -// 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 they are equal, compare cost. - // FIXME: Compute cost! - return false; - } -}; - namespace { /// CompareByRecordName - An ordering predicate that implements less-than by