[llvm] r213277 - [TableGen] Allow shift operators to take bits<n>

Adam Nemet anemet at apple.com
Thu Jul 17 10:04:27 PDT 2014


Author: anemet
Date: Thu Jul 17 12:04:27 2014
New Revision: 213277

URL: http://llvm.org/viewvc/llvm-project?rev=213277&view=rev
Log:
[TableGen] Allow shift operators to take bits<n>

Convert the operand to int if possible, i.e. if the value is properly
initialized.  (I suppose there is further room for improvement here to also
peform the shift if the uninitialized bits are shifted out.)

With this little change we can now compute the scaling factor for compressed
displacement with pure tablegen code in the X86 backend.  This is useful
because both the X86-disassembler-specific part of tablegen and the assembler
need this and TD is the natural sharing place.

The patch also adds the missing documentation for the shift and add operator.

Modified:
    llvm/trunk/docs/TableGen/LangIntro.rst
    llvm/trunk/lib/TableGen/Record.cpp
    llvm/trunk/test/TableGen/math.td

Modified: llvm/trunk/docs/TableGen/LangIntro.rst
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/docs/TableGen/LangIntro.rst?rev=213277&r1=213276&r2=213277&view=diff
==============================================================================
--- llvm/trunk/docs/TableGen/LangIntro.rst (original)
+++ llvm/trunk/docs/TableGen/LangIntro.rst Thu Jul 17 12:04:27 2014
@@ -208,6 +208,12 @@ supported include:
     on string, int and bit objects.  Use !cast<string> to compare other types of
     objects.
 
+``!shl(a,b)``
+``!srl(a,b)``
+``!sra(a,b)``
+``!add(a,b)``
+    The usual logical and arithmetic operators.
+
 Note that all of the values have rules specifying how they convert to values
 for different types.  These rules allow you to assign a value like "``7``"
 to a "``bits<4>``" value, for example.

Modified: llvm/trunk/lib/TableGen/Record.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/TableGen/Record.cpp?rev=213277&r1=213276&r2=213277&view=diff
==============================================================================
--- llvm/trunk/lib/TableGen/Record.cpp (original)
+++ llvm/trunk/lib/TableGen/Record.cpp Thu Jul 17 12:04:27 2014
@@ -955,8 +955,10 @@ Init *BinOpInit::Fold(Record *CurRec, Mu
   case SHL:
   case SRA:
   case SRL: {
-    IntInit *LHSi = dyn_cast<IntInit>(LHS);
-    IntInit *RHSi = dyn_cast<IntInit>(RHS);
+    IntInit *LHSi =
+      dyn_cast_or_null<IntInit>(LHS->convertInitializerTo(IntRecTy::get()));
+    IntInit *RHSi =
+      dyn_cast_or_null<IntInit>(RHS->convertInitializerTo(IntRecTy::get()));
     if (LHSi && RHSi) {
       int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
       int64_t Result;

Modified: llvm/trunk/test/TableGen/math.td
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/TableGen/math.td?rev=213277&r1=213276&r2=213277&view=diff
==============================================================================
--- llvm/trunk/test/TableGen/math.td (original)
+++ llvm/trunk/test/TableGen/math.td Thu Jul 17 12:04:27 2014
@@ -1,6 +1,16 @@
 // RUN: llvm-tblgen %s | FileCheck %s
 // XFAIL: vg_leak
 
+def shifts {
+    bits<2> b = 0b10;
+    int i = 2;
+    int shifted_b = !shl(b, 2);
+    int shifted_i = !shl(i, 2);
+}
+// CHECK: def shifts
+// CHECK: shifted_b = 8
+// CHECK: shifted_i = 8
+
 class Int<int value> {
   int Value = value;
 }





More information about the llvm-commits mailing list