diff --git a/llvm/lib/TableGen/Record.cpp b/llvm/lib/TableGen/Record.cpp index 67622a9d5c12..3657a15ab198 100644 --- a/llvm/lib/TableGen/Record.cpp +++ b/llvm/lib/TableGen/Record.cpp @@ -1564,9 +1564,24 @@ const Init *BinOpInit::Fold(const Record *CurRec) const { case AND: Result = LHSv & RHSv; break; case OR: Result = LHSv | RHSv; break; case XOR: Result = LHSv ^ RHSv; break; - case SHL: Result = (uint64_t)LHSv << (uint64_t)RHSv; break; - case SRA: Result = LHSv >> RHSv; break; - case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break; + case SHL: + if (RHSv < 0 || RHSv >= 64) + PrintFatalError(CurRec->getLoc(), + "Illegal operation: out of bounds shift"); + Result = (uint64_t)LHSv << (uint64_t)RHSv; + break; + case SRA: + if (RHSv < 0 || RHSv >= 64) + PrintFatalError(CurRec->getLoc(), + "Illegal operation: out of bounds shift"); + Result = LHSv >> (uint64_t)RHSv; + break; + case SRL: + if (RHSv < 0 || RHSv >= 64) + PrintFatalError(CurRec->getLoc(), + "Illegal operation: out of bounds shift"); + Result = (uint64_t)LHSv >> (uint64_t)RHSv; + break; } return IntInit::get(getRecordKeeper(), Result); } diff --git a/llvm/test/TableGen/math.td b/llvm/test/TableGen/math.td index 64151a5ca007..fe8faf0a79e3 100644 --- a/llvm/test/TableGen/math.td +++ b/llvm/test/TableGen/math.td @@ -3,6 +3,9 @@ // RUN: not llvm-tblgen -DERROR2 %s 2>&1 | FileCheck --check-prefix=ERROR2 %s // RUN: not llvm-tblgen -DERROR3 %s 2>&1 | FileCheck --check-prefix=ERROR3 %s // RUN: not llvm-tblgen -DERROR4 %s 2>&1 | FileCheck --check-prefix=ERROR4 %s +// RUN: not llvm-tblgen -DERROR5 %s 2>&1 | FileCheck --check-prefix=ERROR5 %s +// RUN: not llvm-tblgen -DERROR6 %s 2>&1 | FileCheck --check-prefix=ERROR6 %s +// RUN: not llvm-tblgen -DERROR7 %s 2>&1 | FileCheck --check-prefix=ERROR7 %s // XFAIL: vg_leak // CHECK: def shifts @@ -143,3 +146,18 @@ def v954 : Int; // CHECK: def vneg // CHECK: Value = -2 def vneg : Int; + +#ifdef ERROR5 +// ERROR5: error: Illegal operation: out of bounds shift +def vshl_neg : Int; +#endif + +#ifdef ERROR6 +// ERROR6: error: Illegal operation: out of bounds shift +def vsra_large : Int; +#endif + +#ifdef ERROR7 +// ERROR7: error: Illegal operation: out of bounds shift +def vsrl_large : Int; +#endif