llvm.org GIT mirror llvm / af0de50
TableGen: Add !ne, !le, !lt, !ge, and !gt comparisons Change-Id: I8e2ece677268972d578a787467f7ef52a1f33a71 Differential revision: https://reviews.llvm.org/D44114 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@327496 91177308-0d34-0410-b5e6-96231b3b80d8 Nicolai Haehnle 1 year, 7 months ago
10 changed file(s) with 162 addition(s) and 20 deletion(s). Raw diff Collapse all Expand all
265265 'bit 1' if string a is equal to string b, 0 otherwise. This only operates
266266 on string, int and bit objects. Use !cast to compare other types of
267267 objects.
268
269 ``!ne(a,b)``
270 The negation of ``!eq(a,b)``.
271
272 ``!le(a,b), !lt(a,b), !ge(a,b), !gt(a,b)``
273 (Signed) comparison of integer values that returns bit 1 or 0 depending on
274 the result of the comparison.
268275
269276 ``!shl(a,b)`` ``!srl(a,b)`` ``!sra(a,b)``
270277 The usual shift operators. Operations are on 64-bit integers, the result
9898 :!add !shl !sra !srl !and
9999 :!or !empty !subst !foreach !strconcat
100100 :!cast !listconcat !size !foldl
101 :!isa !dag
101 :!isa !dag !le !lt !ge
102 :!gt !ne
102103
103104
104105 Syntax
802802 class BinOpInit : public OpInit, public FoldingSetNode {
803803 public:
804804 enum BinaryOp : uint8_t { ADD, AND, OR, SHL, SRA, SRL, LISTCONCAT,
805 STRCONCAT, CONCAT, EQ };
805 STRCONCAT, CONCAT, EQ, NE, LE, LT, GE, GT };
806806
807807 private:
808808 Init *LHS, *RHS;
897897 return ConcatStringInits(LHSs, RHSs);
898898 break;
899899 }
900 case EQ: {
900 case EQ:
901 case NE:
902 case LE:
903 case LT:
904 case GE:
905 case GT: {
901906 // try to fold eq comparison for 'bit' and 'int', otherwise fallback
902907 // to string objects.
903908 IntInit *L =
904 dyn_cast_or_null(LHS->convertInitializerTo(IntRecTy::get()));
909 dyn_cast_or_null(LHS->convertInitializerTo(IntRecTy::get()));
905910 IntInit *R =
906 dyn_cast_or_null(RHS->convertInitializerTo(IntRecTy::get()));
907
908 if (L && R)
909 return IntInit::get(L->getValue() == R->getValue());
910
911 StringInit *LHSs = dyn_cast(LHS);
912 StringInit *RHSs = dyn_cast(RHS);
913
914 // Make sure we've resolved
915 if (LHSs && RHSs)
916 return IntInit::get(LHSs->getValue() == RHSs->getValue());
911 dyn_cast_or_null(RHS->convertInitializerTo(IntRecTy::get()));
912
913 if (L && R) {
914 bool Result;
915 switch (getOpcode()) {
916 case EQ: Result = L->getValue() == R->getValue(); break;
917 case NE: Result = L->getValue() != R->getValue(); break;
918 case LE: Result = L->getValue() <= R->getValue(); break;
919 case LT: Result = L->getValue() < R->getValue(); break;
920 case GE: Result = L->getValue() >= R->getValue(); break;
921 case GT: Result = L->getValue() > R->getValue(); break;
922 default: llvm_unreachable("unhandled comparison");
923 }
924 return BitInit::get(Result);
925 }
926
927 if (getOpcode() == EQ || getOpcode() == NE) {
928 StringInit *LHSs = dyn_cast(LHS);
929 StringInit *RHSs = dyn_cast(RHS);
930
931 // Make sure we've resolved
932 if (LHSs && RHSs) {
933 bool Equal = LHSs->getValue() == RHSs->getValue();
934 return BitInit::get(getOpcode() == EQ ? Equal : !Equal);
935 }
936 }
917937
918938 break;
919939 }
968988 case SRA: Result = "!sra"; break;
969989 case SRL: Result = "!srl"; break;
970990 case EQ: Result = "!eq"; break;
991 case NE: Result = "!ne"; break;
992 case LE: Result = "!le"; break;
993 case LT: Result = "!lt"; break;
994 case GE: Result = "!ge"; break;
995 case GT: Result = "!gt"; break;
971996 case LISTCONCAT: Result = "!listconcat"; break;
972997 case STRCONCAT: Result = "!strconcat"; break;
973998 }
466466 tgtok::TokKind Kind =
467467 StringSwitch(StringRef(Start, CurPtr - Start))
468468 .Case("eq", tgtok::XEq)
469 .Case("ne", tgtok::XNe)
470 .Case("le", tgtok::XLe)
471 .Case("lt", tgtok::XLt)
472 .Case("ge", tgtok::XGe)
473 .Case("gt", tgtok::XGt)
469474 .Case("if", tgtok::XIf)
470475 .Case("isa", tgtok::XIsA)
471476 .Case("head", tgtok::XHead)
4848 // !keywords.
4949 XConcat, XADD, XAND, XOR, XSRA, XSRL, XSHL, XListConcat, XStrConcat, XCast,
5050 XSubst, XForEach, XFoldl, XHead, XTail, XSize, XEmpty, XIf, XEq, XIsA, XDag,
51 XNe, XLe, XLt, XGe, XGt,
5152
5253 // Integer value.
5354 IntVal,
973973 case tgtok::XSRL:
974974 case tgtok::XSHL:
975975 case tgtok::XEq:
976 case tgtok::XNe:
977 case tgtok::XLe:
978 case tgtok::XLt:
979 case tgtok::XGe:
980 case tgtok::XGt:
976981 case tgtok::XListConcat:
977982 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
978983 tgtok::TokKind OpTok = Lex.getCode();
990995 case tgtok::XSRL: Code = BinOpInit::SRL; break;
991996 case tgtok::XSHL: Code = BinOpInit::SHL; break;
992997 case tgtok::XEq: Code = BinOpInit::EQ; break;
998 case tgtok::XNe: Code = BinOpInit::NE; break;
999 case tgtok::XLe: Code = BinOpInit::LE; break;
1000 case tgtok::XLt: Code = BinOpInit::LT; break;
1001 case tgtok::XGe: Code = BinOpInit::GE; break;
1002 case tgtok::XGt: Code = BinOpInit::GT; break;
9931003 case tgtok::XListConcat: Code = BinOpInit::LISTCONCAT; break;
9941004 case tgtok::XStrConcat: Code = BinOpInit::STRCONCAT; break;
9951005 }
10131023 ArgType = IntRecTy::get();
10141024 break;
10151025 case tgtok::XEq:
1026 case tgtok::XNe:
10161027 Type = BitRecTy::get();
1017 // ArgType for Eq is not known at this point
1028 // ArgType for Eq / Ne is not known at this point
1029 break;
1030 case tgtok::XLe:
1031 case tgtok::XLt:
1032 case tgtok::XGe:
1033 case tgtok::XGt:
1034 Type = BitRecTy::get();
1035 ArgType = IntRecTy::get();
10181036 break;
10191037 case tgtok::XListConcat:
10201038 // We don't know the list type until we parse the first argument
10601078 }
10611079 break;
10621080 case BinOpInit::EQ:
1081 case BinOpInit::NE:
10631082 if (!ArgType->typeIsConvertibleTo(IntRecTy::get()) &&
10641083 !ArgType->typeIsConvertibleTo(StringRecTy::get())) {
10651084 Error(InitLoc, Twine("expected int, bits, or string; got value of "
18331852 case tgtok::XSRL:
18341853 case tgtok::XSHL:
18351854 case tgtok::XEq:
1855 case tgtok::XNe:
1856 case tgtok::XLe:
1857 case tgtok::XLt:
1858 case tgtok::XGe:
1859 case tgtok::XGt:
18361860 case tgtok::XListConcat:
18371861 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
18381862 case tgtok::XIf:
0 // RUN: llvm-tblgen %s | FileCheck %s
1 // XFAIL: vg_leak
2
3 // CHECK: --- Defs ---
4
5 // CHECK: def A0 {
6 // CHECK: bit eq = 1;
7 // CHECK: bit ne = 0;
8 // CHECK: bit le = 1;
9 // CHECK: bit lt = 0;
10 // CHECK: bit ge = 1;
11 // CHECK: bit gt = 0;
12 // CHECK: }
13
14 // CHECK: def A1 {
15 // CHECK: bit eq = 0;
16 // CHECK: bit ne = 1;
17 // CHECK: bit le = 1;
18 // CHECK: bit lt = 1;
19 // CHECK: bit ge = 0;
20 // CHECK: bit gt = 0;
21 // CHECK: }
22
23 // CHECK: def A2 {
24 // CHECK: bit eq = 0;
25 // CHECK: bit ne = 1;
26 // CHECK: bit le = 0;
27 // CHECK: bit lt = 0;
28 // CHECK: bit ge = 1;
29 // CHECK: bit gt = 1;
30 // CHECK: }
31
32 // CHECK: def A3 {
33 // CHECK: bit eq = 0;
34 // CHECK: bit ne = 1;
35 // CHECK: bit le = 0;
36 // CHECK: bit lt = 0;
37 // CHECK: bit ge = 1;
38 // CHECK: bit gt = 1;
39 // CHECK: }
40
41 class A {
42 bit eq = !eq(x, y);
43 bit ne = !ne(x, y);
44 bit le = !le(x, y);
45 bit lt = !lt(x, y);
46 bit ge = !ge(x, y);
47 bit gt = !gt(x, y);
48 }
49
50 def A0 : A<-3, -3>;
51 def A1 : A<-1, 4>;
52 def A2 : A<3, -2>;
53 def A3 : A<4, 2>;
0 // RUN: llvm-tblgen %s | FileCheck %s
11 // XFAIL: vg_leak
2 // CHECK: Value = 0
3 // CHECK: Value = 1
2
3 // CHECK-LABEL: def FALSE {
4 // CHECK: int Value = 0;
5 // CHECK: }
6
7 // CHECK-LABEL: def TRUE {
8 // CHECK: int Value = 1;
9 // CHECK: }
10
11 // CHECK-LABEL: def X_NE {
12 // CHECK: bit a = 1;
13 // CHECK: bit b = 0;
14 // CHECK: }
415
516 class Base {
617 int Value = V;
1122
1223 def TRUE : Derived<"true">;
1324 def FALSE : Derived<"false">;
25
26 def X_NE {
27 bit a = !ne("true", "false");
28 bit b = !ne("foo", "foo");
29 }
0 // RUN: llvm-tblgen %s | FileCheck %s
11 // XFAIL: vg_leak
2 // CHECK: a = 6
3 // CHECK: a = 5
2
3 // CHECK-LABEL: def X {
4 // CHECK: int a = 6;
5 // CHECK: int c = 5;
6 // CHECK: }
7
8 // CHECK-LABEL: def Y {
9 // CHECK: int a = 5;
10 // CHECK: int c = 6;
11 // CHECK: }
412
513 class A {
614 int a = !if(!eq(b, 1), 5, 6);
15 int c = !if(!ne(b, 1), 5, 6);
716 }
817
918 def X : A<0>;