Skip to content

Commit 7cfa0c1

Browse files
wangpc-pptstellar
authored andcommitted
[TableGen] Add predicates for immediates comparison (#76004)
These predicates can be used to represent `<`, `<=`, `>`, `>=`. And a predicate for `in range` is added. (cherry picked from commit 664a0fa)
1 parent 62877e3 commit 7cfa0c1

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

llvm/include/llvm/Target/TargetInstrPredicate.td

+34
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,34 @@ class CheckImmOperand_s<int Index, string Value> : CheckOperandBase<Index> {
152152
string ImmVal = Value;
153153
}
154154

155+
// Check that the operand at position `Index` is less than `Imm`.
156+
// If field `FunctionMapper` is a non-empty string, then function
157+
// `FunctionMapper` is applied to the operand value, and the return value is then
158+
// compared against `Imm`.
159+
class CheckImmOperandLT<int Index, int Imm> : CheckOperandBase<Index> {
160+
int ImmVal = Imm;
161+
}
162+
163+
// Check that the operand at position `Index` is greater than `Imm`.
164+
// If field `FunctionMapper` is a non-empty string, then function
165+
// `FunctionMapper` is applied to the operand value, and the return value is then
166+
// compared against `Imm`.
167+
class CheckImmOperandGT<int Index, int Imm> : CheckOperandBase<Index> {
168+
int ImmVal = Imm;
169+
}
170+
171+
// Check that the operand at position `Index` is less 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 CheckImmOperandLE<int Index, int Imm> : CheckNot<CheckImmOperandGT<Index, Imm>>;
176+
177+
// Check that the operand at position `Index` is greater than or equal to `Imm`.
178+
// If field `FunctionMapper` is a non-empty string, then function
179+
// `FunctionMapper` is applied to the operand value, and the return value is then
180+
// compared against `Imm`.
181+
class CheckImmOperandGE<int Index, int Imm> : CheckNot<CheckImmOperandLT<Index, Imm>>;
182+
155183
// Expands to a call to `FunctionMapper` if field `FunctionMapper` is set.
156184
// Otherwise, it expands to a CheckNot<CheckInvalidRegOperand<Index>>.
157185
class CheckRegOperandSimple<int Index> : CheckOperandBase<Index>;
@@ -203,6 +231,12 @@ class CheckAll<list<MCInstPredicate> Sequence>
203231
class CheckAny<list<MCInstPredicate> Sequence>
204232
: CheckPredicateSequence<Sequence>;
205233

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

207241
// Used to expand the body of a function predicate. See the definition of
208242
// TIIPredicate below.

llvm/utils/TableGen/PredicateExpander.cpp

+34
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) {
@@ -352,6 +376,16 @@ void PredicateExpander::expandPredicate(raw_ostream &OS, const Record *Rec) {
352376
Rec->getValueAsString("ImmVal"),
353377
Rec->getValueAsString("FunctionMapper"));
354378

379+
if (Rec->isSubClassOf("CheckImmOperandLT"))
380+
return expandCheckImmOperandLT(OS, Rec->getValueAsInt("OpIndex"),
381+
Rec->getValueAsInt("ImmVal"),
382+
Rec->getValueAsString("FunctionMapper"));
383+
384+
if (Rec->isSubClassOf("CheckImmOperandGT"))
385+
return expandCheckImmOperandGT(OS, Rec->getValueAsInt("OpIndex"),
386+
Rec->getValueAsInt("ImmVal"),
387+
Rec->getValueAsString("FunctionMapper"));
388+
355389
if (Rec->isSubClassOf("CheckImmOperandSimple"))
356390
return expandCheckImmOperandSimple(OS, Rec->getValueAsInt("OpIndex"),
357391
Rec->getValueAsString("FunctionMapper"));

llvm/utils/TableGen/PredicateExpander.h

+4
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)