Skip to content

Commit a5fda5b

Browse files
committed
[TableGen] Avoid evaluating RHS of a BinOp until short-circuit is complete
1 parent 4268360 commit a5fda5b

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

llvm/lib/TableGen/Record.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1558,7 +1558,6 @@ const Init *BinOpInit::Fold(const Record *CurRec) const {
15581558

15591559
const Init *BinOpInit::resolveReferences(Resolver &R) const {
15601560
const Init *lhs = LHS->resolveReferences(R);
1561-
const Init *rhs = RHS->resolveReferences(R);
15621561

15631562
unsigned Opc = getOpcode();
15641563
if (Opc == AND || Opc == OR) {
@@ -1577,6 +1576,8 @@ const Init *BinOpInit::resolveReferences(Resolver &R) const {
15771576
}
15781577
}
15791578

1579+
const Init *rhs = RHS->resolveReferences(R);
1580+
15801581
if (LHS != lhs || RHS != rhs)
15811582
return (BinOpInit::get(getOpcode(), lhs, rhs, getType()))
15821583
->Fold(R.getCurrentRecord());

llvm/test/TableGen/true-false.td

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,18 @@ def rec7 {
6767
bits<3> flags = { true, false, true };
6868
}
6969

70-
// `!and` and `!or` should be short-circuit such that `!tail` on empty list will never
71-
// be evaluated.
70+
// `!and` and `!or` should be short-circuit such that any of the `!head` or
71+
// `!tail` on empty list below will never be evaluated.
7272
// CHECK: def rec8
73+
// CHECK: bit v = 0;
74+
// CHECK: int v2 = -1;
7375
// CHECK: list<int> newSeq = [];
7476
// CHECK: list<int> newSeq2 = [];
7577

7678
class Foo <list<int> seq = []> {
79+
bit v = !and(false, !head(seq));
80+
int v2 = !or(-1, !head(seq));
81+
7782
bit unresolved = !ne(!find(NAME, "BAR"), -1);
7883
list<int> newSeq = !if(!and(false, unresolved), !tail(seq), seq);
7984
list<int> newSeq2 = !if(!or(-1, unresolved), seq, !tail(seq));

0 commit comments

Comments
 (0)