[llvm] r243722 - TableGen: Support folding casts from bits to int

Matt Arsenault Matthew.Arsenault at amd.com
Thu Jul 30 18:12:06 PDT 2015


Author: arsenm
Date: Thu Jul 30 20:12:06 2015
New Revision: 243722

URL: http://llvm.org/viewvc/llvm-project?rev=243722&view=rev
Log:
TableGen: Support folding casts from bits to int

This is to fix an incorrect error when trying to initialize
DwarfNumbers with a !cast<int> of a bits initializer.
getValuesAsListOfInts("DwarfNumbers") would not see an IntInit
and instead the cast, so would give up.

It seems likely that this could be generalized to attempt
the convertInitializerTo for any type. I'm not really sure
why the existing code seems to special case the string cast cases
when convertInitializerTo seems like it should generally handle this
sort of thing.

Added:
    llvm/trunk/test/TableGen/cast-list-initializer.td
Modified:
    llvm/trunk/lib/TableGen/Record.cpp

Modified: llvm/trunk/lib/TableGen/Record.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/TableGen/Record.cpp?rev=243722&r1=243721&r2=243722&view=diff
==============================================================================
--- llvm/trunk/lib/TableGen/Record.cpp (original)
+++ llvm/trunk/lib/TableGen/Record.cpp Thu Jul 30 20:12:06 2015
@@ -673,6 +673,14 @@ Init *UnOpInit::Fold(Record *CurRec, Mul
         PrintFatalError(CurRec->getLoc(),
                         "Undefined reference:'" + Name + "'\n");
       }
+
+      if (isa<IntRecTy>(getType())) {
+        if (BitsInit *BI = dyn_cast<BitsInit>(LHS)) {
+          if (Init *NewInit = BI->convertInitializerTo(IntRecTy::get()))
+            return NewInit;
+          break;
+        }
+      }
     }
     break;
   }

Added: llvm/trunk/test/TableGen/cast-list-initializer.td
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/TableGen/cast-list-initializer.td?rev=243722&view=auto
==============================================================================
--- llvm/trunk/test/TableGen/cast-list-initializer.td (added)
+++ llvm/trunk/test/TableGen/cast-list-initializer.td Thu Jul 30 20:12:06 2015
@@ -0,0 +1,10 @@
+// RUN: llvm-tblgen %s | FileCheck %s
+
+class Foo<bits<8> b> {
+// CHECK: list<int> ListOfInts = [170];
+// CHECK: list<int> AnotherList = [170, 7];
+  list<int> ListOfInts = [!cast<int>(b)];
+  list<int> AnotherList = [!cast<int>(b), !cast<int>({1, 1, 1})];
+}
+
+def : Foo<{1, 0, 1, 0, 1, 0, 1, 0}>;





More information about the llvm-commits mailing list