diff --git a/llvm/lib/TableGen/Record.cpp b/llvm/lib/TableGen/Record.cpp index 4c8b41237c604..7f2ed77a74099 100644 --- a/llvm/lib/TableGen/Record.cpp +++ b/llvm/lib/TableGen/Record.cpp @@ -1557,8 +1557,7 @@ const Init *BinOpInit::Fold(const Record *CurRec) const { } const Init *BinOpInit::resolveReferences(Resolver &R) const { - const Init *lhs = LHS->resolveReferences(R); - const Init *rhs = RHS->resolveReferences(R); + const Init *NewLHS = LHS->resolveReferences(R); unsigned Opc = getOpcode(); if (Opc == AND || Opc == OR) { @@ -1570,15 +1569,17 @@ const Init *BinOpInit::resolveReferences(Resolver &R) const { // limited version of short-circuit against all ones (`true` is casted // to 1 rather than all ones before we evaluate `!or`). if (const auto *LHSi = dyn_cast_or_null( - lhs->convertInitializerTo(IntRecTy::get(getRecordKeeper())))) { + NewLHS->convertInitializerTo(IntRecTy::get(getRecordKeeper())))) { if ((Opc == AND && !LHSi->getValue()) || (Opc == OR && LHSi->getValue() == -1)) return LHSi; } } - if (LHS != lhs || RHS != rhs) - return (BinOpInit::get(getOpcode(), lhs, rhs, getType())) + const Init *NewRHS = RHS->resolveReferences(R); + + if (LHS != NewLHS || RHS != NewRHS) + return (BinOpInit::get(getOpcode(), NewLHS, NewRHS, getType())) ->Fold(R.getCurrentRecord()); return this; } diff --git a/llvm/test/TableGen/true-false.td b/llvm/test/TableGen/true-false.td index 5a59f20b21d25..5fa5702314489 100644 --- a/llvm/test/TableGen/true-false.td +++ b/llvm/test/TableGen/true-false.td @@ -67,13 +67,18 @@ def rec7 { bits<3> flags = { true, false, true }; } -// `!and` and `!or` should be short-circuit such that `!tail` on empty list will never -// be evaluated. +// `!and` and `!or` should be short-circuited such that any of the `!head` or +// `!tail` on empty list below will never be evaluated. // CHECK: def rec8 +// CHECK: bit v = 0; +// CHECK: int v2 = -1; // CHECK: list newSeq = []; // CHECK: list newSeq2 = []; class Foo seq = []> { + bit v = !and(false, !head(seq)); + int v2 = !or(-1, !head(seq)); + bit unresolved = !ne(!find(NAME, "BAR"), -1); list newSeq = !if(!and(false, unresolved), !tail(seq), seq); list newSeq2 = !if(!or(-1, unresolved), seq, !tail(seq));