TableGen: Allow AddedComplexity values to be negative
authorTom Stellard <thomas.stellard@amd.com>
Mon, 21 Jul 2014 13:28:54 +0000 (13:28 +0000)
committerTom Stellard <thomas.stellard@amd.com>
Mon, 21 Jul 2014 13:28:54 +0000 (13:28 +0000)
This is useful for cases when stand-alone patterns are preferred to the
patterns included in the instruction definitions.  Instead of requiring
that stand-alone patterns set a larger AddedComplexity value, which
can be confusing to new developers, the allows us to reduce the
complexity of the included patterns to achieve the same result.

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

test/TableGen/NegativeAddedComplexity.ll [new file with mode: 0644]
utils/TableGen/CodeGenDAGPatterns.cpp
utils/TableGen/CodeGenDAGPatterns.h
utils/TableGen/DAGISelEmitter.cpp

diff --git a/test/TableGen/NegativeAddedComplexity.ll b/test/TableGen/NegativeAddedComplexity.ll
new file mode 100644 (file)
index 0000000..54c52ab
--- /dev/null
@@ -0,0 +1,41 @@
+// RUN: llvm-tblgen -I../../include -gen-dag-isel %s | FileCheck %s
+// XFAIL: vg_leak
+
+include "llvm/Target/Target.td"
+
+// Make sure the higher complexity pattern comes first
+// CHECK: TARGET_VAL(::ADD0)
+// CHECK: Complexity = {{[^-]}}
+// Make sure the ADD1 pattern has a negative complexity
+// CHECK: TARGET_VAL(::ADD1)
+// CHECK: Complexity = -{{[0-9]+}}
+
+def TestRC : RegisterClass<"TEST", [i32], 32, (add)>;
+
+def TestInstrInfo : InstrInfo;
+
+def Test : Target {
+  let InstructionSet = TestInstrInfo;
+}
+
+def ADD0 : Instruction {
+  let OutOperandList = (outs TestRC:$dst);
+  let InOperandList = (ins TestRC:$src0, TestRC:$src1);
+}
+
+def ADD1 : Instruction {
+  let OutOperandList = (outs TestRC:$dst);
+  let InOperandList = (ins TestRC:$src0, TestRC:$src1);
+}
+
+def : Pat <
+  (add i32:$src0, i32:$src1),
+  (ADD1 $src0, $src1)
+> {
+  let AddedComplexity = -1000;
+}
+
+def : Pat <
+   (add i32:$src0, i32:$src1),
+   (ADD0 $src0, $src1)
+>;
index 2602bbcf6f61c20be64eb9876f8f9dab1117a90b..d195ba823b5bf368d976558e36292b7a22cb23bd 100644 (file)
@@ -771,7 +771,7 @@ static unsigned getPatternSize(const TreePatternNode *P,
 
 /// Compute the complexity metric for the input pattern.  This roughly
 /// corresponds to the number of nodes that are covered.
-unsigned PatternToMatch::
+int PatternToMatch::
 getPatternComplexity(const CodeGenDAGPatterns &CGP) const {
   return getPatternSize(getSrcPattern(), CGP) + getAddedComplexity();
 }
index fb30cdd94853107f3de9306b077986342276e294..ef6c787c3e08fca0ef4f42e9de30a52facd007c7 100644 (file)
@@ -667,7 +667,7 @@ public:
   PatternToMatch(Record *srcrecord, ListInit *preds,
                  TreePatternNode *src, TreePatternNode *dst,
                  const std::vector<Record*> &dstregs,
-                 unsigned complexity, unsigned uid)
+                 int complexity, unsigned uid)
     : SrcRecord(srcrecord), Predicates(preds), SrcPattern(src), DstPattern(dst),
       Dstregs(dstregs), AddedComplexity(complexity), ID(uid) {}
 
@@ -676,7 +676,7 @@ public:
   TreePatternNode *SrcPattern;  // Source pattern to match.
   TreePatternNode *DstPattern;  // Resulting pattern.
   std::vector<Record*> Dstregs; // Physical register defs being matched.
-  unsigned         AddedComplexity; // Add to matching pattern complexity.
+  int              AddedComplexity; // Add to matching pattern complexity.
   unsigned         ID;          // Unique ID for the record.
 
   Record          *getSrcRecord()  const { return SrcRecord; }
@@ -684,13 +684,13 @@ public:
   TreePatternNode *getSrcPattern() const { return SrcPattern; }
   TreePatternNode *getDstPattern() const { return DstPattern; }
   const std::vector<Record*> &getDstRegs() const { return Dstregs; }
-  unsigned         getAddedComplexity() const { return AddedComplexity; }
+  int         getAddedComplexity() const { return AddedComplexity; }
 
   std::string getPredicateCheck() const;
 
   /// Compute the complexity metric for the input pattern.  This roughly
   /// corresponds to the number of nodes that are covered.
-  unsigned getPatternComplexity(const CodeGenDAGPatterns &CGP) const;
+  int getPatternComplexity(const CodeGenDAGPatterns &CGP) const;
 };
 
 class CodeGenDAGPatterns {
index 82682cd5a5adcadd22b513ab5e5f794a7fcbc8e9..e2e6ab19cba45a96870edb55e179975492852c0d 100644 (file)
@@ -94,8 +94,8 @@ struct PatternSortingPredicate {
     // Otherwise, if the patterns might both match, sort based on complexity,
     // which means that we prefer to match patterns that cover more nodes in the
     // input over nodes that cover fewer.
-    unsigned LHSSize = LHS->getPatternComplexity(CGP);
-    unsigned RHSSize = RHS->getPatternComplexity(CGP);
+    int LHSSize = LHS->getPatternComplexity(CGP);
+    int RHSSize = RHS->getPatternComplexity(CGP);
     if (LHSSize > RHSSize) return true;   // LHS -> bigger -> less cost
     if (LHSSize < RHSSize) return false;