Skip to content

Commit 9b20f31

Browse files
committed
[TableGen] Add predicates for immediates comparison
These predicates can be used to represent `<`, `<=`, `>`, `>=`. And a predicate for `in range` is added.
1 parent 18e1179 commit 9b20f31

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

llvm/include/llvm/Target/TargetInstrPredicate.td

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,34 @@ class CheckImmOperand_s<int Index, string Value> : CheckOperandBase<Index> {
146146
string ImmVal = Value;
147147
}
148148

149+
// Check that the operand at position `Index` is less than `Imm`.
150+
// If field `FunctionMapper` is a non-empty string, then function
151+
// `FunctionMapper` is applied to the operand value, and the return value is then
152+
// compared against `Imm`.
153+
class CheckImmOperandLT<int Index, int Imm> : CheckOperandBase<Index> {
154+
int ImmVal = Imm;
155+
}
156+
157+
// Check that the operand at position `Index` is greater than `Imm`.
158+
// If field `FunctionMapper` is a non-empty string, then function
159+
// `FunctionMapper` is applied to the operand value, and the return value is then
160+
// compared against `Imm`.
161+
class CheckImmOperandGT<int Index, int Imm> : CheckOperandBase<Index> {
162+
int ImmVal = Imm;
163+
}
164+
165+
// Check that the operand at position `Index` is less than or equal to `Imm`.
166+
// If field `FunctionMapper` is a non-empty string, then function
167+
// `FunctionMapper` is applied to the operand value, and the return value is then
168+
// compared against `Imm`.
169+
class CheckImmOperandLE<int Index, int Imm> : CheckNot<CheckImmOperandGT<Index, Imm>>;
170+
171+
// Check that the operand at position `Index` is greater than or equal to `Imm`.
172+
// If field `FunctionMapper` is a non-empty string, then function
173+
// `FunctionMapper` is applied to the operand value, and the return value is then
174+
// compared against `Imm`.
175+
class CheckImmOperandGE<int Index, int Imm> : CheckNot<CheckImmOperandLT<Index, Imm>>;
176+
149177
// Expands to a call to `FunctionMapper` if field `FunctionMapper` is set.
150178
// Otherwise, it expands to a CheckNot<CheckInvalidRegOperand<Index>>.
151179
class CheckRegOperandSimple<int Index> : CheckOperandBase<Index>;
@@ -197,6 +225,12 @@ class CheckAll<list<MCInstPredicate> Sequence>
197225
class CheckAny<list<MCInstPredicate> Sequence>
198226
: CheckPredicateSequence<Sequence>;
199227

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

201235
// Used to expand the body of a function predicate. See the definition of
202236
// TIIPredicate below.

llvm/utils/TableGen/PredicateExpander.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,30 @@ void PredicateExpander::expandCheckImmOperandSimple(raw_ostream &OS,
5959
OS << ")";
6060
}
6161

62+
void PredicateExpander::expandCheckImmOperandLT(raw_ostream &OS, int OpIndex,
63+
int ImmVal,
64+
StringRef FunctionMapper) {
65+
if (!FunctionMapper.empty())
66+
OS << FunctionMapper << "(";
67+
OS << "MI" << (isByRef() ? "." : "->") << "getOperand(" << OpIndex
68+
<< ").getImm()";
69+
if (!FunctionMapper.empty())
70+
OS << ")";
71+
OS << (shouldNegate() ? " >= " : " < ") << ImmVal;
72+
}
73+
74+
void PredicateExpander::expandCheckImmOperandGT(raw_ostream &OS, int OpIndex,
75+
int ImmVal,
76+
StringRef FunctionMapper) {
77+
if (!FunctionMapper.empty())
78+
OS << FunctionMapper << "(";
79+
OS << "MI" << (isByRef() ? "." : "->") << "getOperand(" << OpIndex
80+
<< ").getImm()";
81+
if (!FunctionMapper.empty())
82+
OS << ")";
83+
OS << (shouldNegate() ? " <= " : " > ") << ImmVal;
84+
}
85+
6286
void PredicateExpander::expandCheckRegOperand(raw_ostream &OS, int OpIndex,
6387
const Record *Reg,
6488
StringRef FunctionMapper) {
@@ -344,6 +368,16 @@ void PredicateExpander::expandPredicate(raw_ostream &OS, const Record *Rec) {
344368
Rec->getValueAsString("ImmVal"),
345369
Rec->getValueAsString("FunctionMapper"));
346370

371+
if (Rec->isSubClassOf("CheckImmOperandLT"))
372+
return expandCheckImmOperandLT(OS, Rec->getValueAsInt("OpIndex"),
373+
Rec->getValueAsInt("ImmVal"),
374+
Rec->getValueAsString("FunctionMapper"));
375+
376+
if (Rec->isSubClassOf("CheckImmOperandGT"))
377+
return expandCheckImmOperandGT(OS, Rec->getValueAsInt("OpIndex"),
378+
Rec->getValueAsInt("ImmVal"),
379+
Rec->getValueAsString("FunctionMapper"));
380+
347381
if (Rec->isSubClassOf("CheckImmOperandSimple"))
348382
return expandCheckImmOperandSimple(OS, Rec->getValueAsInt("OpIndex"),
349383
Rec->getValueAsString("FunctionMapper"));

llvm/utils/TableGen/PredicateExpander.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ class PredicateExpander {
6161
StringRef FunctionMapperer);
6262
void expandCheckImmOperandSimple(raw_ostream &OS, int OpIndex,
6363
StringRef FunctionMapper);
64+
void expandCheckImmOperandLT(raw_ostream &OS, int OpIndex, int ImmVal,
65+
StringRef FunctionMapper);
66+
void expandCheckImmOperandGT(raw_ostream &OS, int OpIndex, int ImmVal,
67+
StringRef FunctionMapper);
6468
void expandCheckRegOperand(raw_ostream &OS, int OpIndex, const Record *Reg,
6569
StringRef FunctionMapper);
6670
void expandCheckRegOperandSimple(raw_ostream &OS, int OpIndex,

0 commit comments

Comments
 (0)