OSDN Git Service

[TableGen] CodeGenDAGPatterns::GenerateVariants - basic caching of matching predicates
authorSimon Pilgrim <llvm-dev@redking.me.uk>
Tue, 28 Aug 2018 15:42:08 +0000 (15:42 +0000)
committerSimon Pilgrim <llvm-dev@redking.me.uk>
Tue, 28 Aug 2018 15:42:08 +0000 (15:42 +0000)
CodeGenDAGPatterns::GenerateVariants is a costly function in many tblgen commands (33.87% of the total runtime of x86 -gen-dag-isel), and due to the O(N^2) nature of the function, there are a high number of repeated comparisons of the pattern's vector<Predicate>.

This initial patch at least avoids repeating these comparisons for every Variant in a pattern. I began investigating caching all the matches before entering the loop but hit issues with how best to store the data and how to update the cache as patterns were added.

Saves around 15secs in debug builds of x86 -gen-dag-isel.

Differential Revision: https://reviews.llvm.org/D51035

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@340837 91177308-0d34-0410-b5e6-96231b3b80d8

utils/TableGen/CodeGenDAGPatterns.cpp

index dc88da2..e6bcca2 100644 (file)
@@ -13,6 +13,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "CodeGenDAGPatterns.h"
+#include "llvm/ADT/BitVector.h"
 #include "llvm/ADT/DenseSet.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SmallSet.h"
@@ -4477,6 +4478,16 @@ void CodeGenDAGPatterns::GenerateVariants() {
     LLVM_DEBUG(errs() << "FOUND VARIANTS OF: ";
                PatternsToMatch[i].getSrcPattern()->dump(); errs() << "\n");
 
+    // Cache matching predicates.
+    // TODO: Is it performant to pull this out of the loop entirely?
+    BitVector MatchedPredicates(PatternsToMatch.size(), false);
+    for (unsigned p = 0, e = PatternsToMatch.size(); p != e; ++p)
+      MatchedPredicates[p] = (i == p) || (PatternsToMatch[i].getPredicates() ==
+                                          PatternsToMatch[p].getPredicates());
+
+    unsigned NumMatches = MatchedPredicates.count();
+    (void)NumMatches;
+
     for (unsigned v = 0, e = Variants.size(); v != e; ++v) {
       TreePatternNodePtr Variant = Variants[v];
 
@@ -4487,8 +4498,7 @@ void CodeGenDAGPatterns::GenerateVariants() {
       bool AlreadyExists = false;
       for (unsigned p = 0, e = PatternsToMatch.size(); p != e; ++p) {
         // Skip if the top level predicates do not match.
-        if ((i != p) && (PatternsToMatch[i].getPredicates() !=
-                         PatternsToMatch[p].getPredicates()))
+        if (!MatchedPredicates[p])
           continue;
         // Check to see if this variant already exists.
         if (Variant->isIsomorphicTo(PatternsToMatch[p].getSrcPattern(),
@@ -4507,6 +4517,8 @@ void CodeGenDAGPatterns::GenerateVariants() {
           Variant, PatternsToMatch[i].getDstPatternShared(),
           PatternsToMatch[i].getDstRegs(),
           PatternsToMatch[i].getAddedComplexity(), Record::getNewUID()));
+      MatchedPredicates.resize(PatternsToMatch.size());
+      MatchedPredicates[PatternsToMatch.size() - 1] = true;
     }
 
     LLVM_DEBUG(errs() << "\n");