let the '!eq' expression support 'int' and 'bit' types
authorBruno Cardoso Lopes <bruno.cardoso@gmail.com>
Wed, 16 Jun 2010 23:24:12 +0000 (23:24 +0000)
committerBruno Cardoso Lopes <bruno.cardoso@gmail.com>
Wed, 16 Jun 2010 23:24:12 +0000 (23:24 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@106171 91177308-0d34-0410-b5e6-96231b3b80d8

docs/TableGenFundamentals.html
test/TableGen/eqbit.td [new file with mode: 0644]
utils/TableGen/Record.cpp

index e504a894640bbdbe171e73ab4bbd19781002b8f2..3410cacb62dc744effe830ac4060542fc8e01cea 100644 (file)
@@ -425,8 +425,8 @@ class.  This operation is analogous to $(foreach) in GNU make.</dd>
   <dd>'b' if the result of integer operator 'a' is nonzero, 'c' otherwise.</dd>
 <dt><tt>!eq(a,b)</tt></dt>
   <dd>Integer one if string a is equal to string b, zero otherwise.  This
-      only operates on string objects.  Use !cast<string> to compare other
-      types of objects.</dd>
+      only operates on string, int and bit objects.  Use !cast<string> to
+      compare other types of objects.</dd>
 </dl>
 
 <p>Note that all of the values have rules specifying how they convert to values
diff --git a/test/TableGen/eqbit.td b/test/TableGen/eqbit.td
new file mode 100644 (file)
index 0000000..3953252
--- /dev/null
@@ -0,0 +1,11 @@
+// RUN: tblgen %s | FileCheck %s
+// XFAIL: vg_leak
+// CHECK: a = 6
+// CHECK: a = 5
+
+class A<bit b = 1> {
+  int a = !if(!eq(b, 1), 5, 6);
+}
+
+def X : A<0>;
+def Y : A;
index 4f9f6045c14c9ea35c7c7f685237b8f1341cb418..d9c5dd30e18863af077dde36a9c3ae3845d4186e 100644 (file)
@@ -721,9 +721,20 @@ Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
     break;
   }
   case EQ: {
-    // Make sure we've resolved
+    // try to fold eq comparison for 'bit' and 'int', otherwise fallback
+    // to string objects.
+    IntInit* L =
+      dynamic_cast<IntInit*>(LHS->convertInitializerTo(new IntRecTy()));
+    IntInit* R =
+      dynamic_cast<IntInit*>(RHS->convertInitializerTo(new IntRecTy()));
+
+    if (L && R)
+      return new IntInit(L->getValue() == R->getValue());
+
     StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
     StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
+
+    // Make sure we've resolved
     if (LHSs && RHSs)
       return new IntInit(LHSs->getValue() == RHSs->getValue());