Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TableGen] Add predicates for immediates comparison #76004

Merged
merged 1 commit into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions llvm/include/llvm/Target/TargetInstrPredicate.td
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,34 @@ class CheckImmOperand_s<int Index, string Value> : CheckOperandBase<Index> {
string ImmVal = Value;
}

// Check that the operand at position `Index` is less than `Imm`.
// If field `FunctionMapper` is a non-empty string, then function
// `FunctionMapper` is applied to the operand value, and the return value is then
// compared against `Imm`.
class CheckImmOperandLT<int Index, int Imm> : CheckOperandBase<Index> {
int ImmVal = Imm;
}

// Check that the operand at position `Index` is greater than `Imm`.
// If field `FunctionMapper` is a non-empty string, then function
// `FunctionMapper` is applied to the operand value, and the return value is then
// compared against `Imm`.
class CheckImmOperandGT<int Index, int Imm> : CheckOperandBase<Index> {
int ImmVal = Imm;
}

// Check that the operand at position `Index` is less than or equal to `Imm`.
// If field `FunctionMapper` is a non-empty string, then function
// `FunctionMapper` is applied to the operand value, and the return value is then
// compared against `Imm`.
class CheckImmOperandLE<int Index, int Imm> : CheckNot<CheckImmOperandGT<Index, Imm>>;

// Check that the operand at position `Index` is greater than or equal to `Imm`.
// If field `FunctionMapper` is a non-empty string, then function
// `FunctionMapper` is applied to the operand value, and the return value is then
// compared against `Imm`.
class CheckImmOperandGE<int Index, int Imm> : CheckNot<CheckImmOperandLT<Index, Imm>>;

// Expands to a call to `FunctionMapper` if field `FunctionMapper` is set.
// Otherwise, it expands to a CheckNot<CheckInvalidRegOperand<Index>>.
class CheckRegOperandSimple<int Index> : CheckOperandBase<Index>;
Expand Down Expand Up @@ -203,6 +231,12 @@ class CheckAll<list<MCInstPredicate> Sequence>
class CheckAny<list<MCInstPredicate> Sequence>
: CheckPredicateSequence<Sequence>;

// Check that the operand at position `Index` is in range [Start, End].
// If field `FunctionMapper` is a non-empty string, then function
// `FunctionMapper` is applied to the operand value, and the return value is then
// compared against range [Start, End].
class CheckImmOperandRange<int Index, int Start, int End>
: CheckAll<[CheckImmOperandGE<Index, Start>, CheckImmOperandLE<Index, End>]>;

// Used to expand the body of a function predicate. See the definition of
// TIIPredicate below.
Expand Down
34 changes: 34 additions & 0 deletions llvm/utils/TableGen/PredicateExpander.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,30 @@ void PredicateExpander::expandCheckImmOperandSimple(raw_ostream &OS,
OS << ")";
}

void PredicateExpander::expandCheckImmOperandLT(raw_ostream &OS, int OpIndex,
int ImmVal,
StringRef FunctionMapper) {
if (!FunctionMapper.empty())
OS << FunctionMapper << "(";
OS << "MI" << (isByRef() ? "." : "->") << "getOperand(" << OpIndex
<< ").getImm()";
if (!FunctionMapper.empty())
OS << ")";
OS << (shouldNegate() ? " >= " : " < ") << ImmVal;
}

void PredicateExpander::expandCheckImmOperandGT(raw_ostream &OS, int OpIndex,
int ImmVal,
StringRef FunctionMapper) {
if (!FunctionMapper.empty())
OS << FunctionMapper << "(";
OS << "MI" << (isByRef() ? "." : "->") << "getOperand(" << OpIndex
<< ").getImm()";
if (!FunctionMapper.empty())
OS << ")";
OS << (shouldNegate() ? " <= " : " > ") << ImmVal;
}

void PredicateExpander::expandCheckRegOperand(raw_ostream &OS, int OpIndex,
const Record *Reg,
StringRef FunctionMapper) {
Expand Down Expand Up @@ -352,6 +376,16 @@ void PredicateExpander::expandPredicate(raw_ostream &OS, const Record *Rec) {
Rec->getValueAsString("ImmVal"),
Rec->getValueAsString("FunctionMapper"));

if (Rec->isSubClassOf("CheckImmOperandLT"))
return expandCheckImmOperandLT(OS, Rec->getValueAsInt("OpIndex"),
Rec->getValueAsInt("ImmVal"),
Rec->getValueAsString("FunctionMapper"));

if (Rec->isSubClassOf("CheckImmOperandGT"))
return expandCheckImmOperandGT(OS, Rec->getValueAsInt("OpIndex"),
Rec->getValueAsInt("ImmVal"),
Rec->getValueAsString("FunctionMapper"));

if (Rec->isSubClassOf("CheckImmOperandSimple"))
return expandCheckImmOperandSimple(OS, Rec->getValueAsInt("OpIndex"),
Rec->getValueAsString("FunctionMapper"));
Expand Down
4 changes: 4 additions & 0 deletions llvm/utils/TableGen/PredicateExpander.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ class PredicateExpander {
StringRef FunctionMapperer);
void expandCheckImmOperandSimple(raw_ostream &OS, int OpIndex,
StringRef FunctionMapper);
void expandCheckImmOperandLT(raw_ostream &OS, int OpIndex, int ImmVal,
StringRef FunctionMapper);
void expandCheckImmOperandGT(raw_ostream &OS, int OpIndex, int ImmVal,
StringRef FunctionMapper);
void expandCheckRegOperand(raw_ostream &OS, int OpIndex, const Record *Reg,
StringRef FunctionMapper);
void expandCheckRegOperandSimple(raw_ostream &OS, int OpIndex,
Expand Down