1//===--------------------- PredicateExpander.cpp --------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8/// \file
9/// Functionalities used by the Tablegen backends to expand machine predicates.
10//
11//===----------------------------------------------------------------------===//
12
13#include "PredicateExpander.h"
14#include "CodeGenSchedule.h" // Definition of STIPredicateFunction.
15#include "llvm/TableGen/Record.h"
16
17using namespace llvm;
18
19void PredicateExpander::expandTrue(raw_ostream &OS) { OS << "true"; }
20void PredicateExpander::expandFalse(raw_ostream &OS) { OS << "false"; }
21
22void PredicateExpander::expandCheckImmOperand(raw_ostream &OS, int OpIndex,
23 int ImmVal,
24 StringRef FunctionMapper) {
25 if (!FunctionMapper.empty())
26 OS << FunctionMapper << "(";
27 OS << "MI" << (isByRef() ? "." : "->") << "getOperand(" << OpIndex
28 << ").getImm()";
29 if (!FunctionMapper.empty())
30 OS << ")";
31 OS << (shouldNegate() ? " != " : " == ") << ImmVal;
32}
33
34void PredicateExpander::expandCheckImmOperand(raw_ostream &OS, int OpIndex,
35 StringRef ImmVal,
36 StringRef FunctionMapper) {
37 if (ImmVal.empty())
38 expandCheckImmOperandSimple(OS, OpIndex, FunctionMapper);
39
40 if (!FunctionMapper.empty())
41 OS << FunctionMapper << "(";
42 OS << "MI" << (isByRef() ? "." : "->") << "getOperand(" << OpIndex
43 << ").getImm()";
44 if (!FunctionMapper.empty())
45 OS << ")";
46 OS << (shouldNegate() ? " != " : " == ") << ImmVal;
47}
48
49void PredicateExpander::expandCheckImmOperandSimple(raw_ostream &OS,
50 int OpIndex,
51 StringRef FunctionMapper) {
52 if (shouldNegate())
53 OS << "!";
54 if (!FunctionMapper.empty())
55 OS << FunctionMapper << "(";
56 OS << "MI" << (isByRef() ? "." : "->") << "getOperand(" << OpIndex
57 << ").getImm()";
58 if (!FunctionMapper.empty())
59 OS << ")";
60}
61
62void 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
74void 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
86void PredicateExpander::expandCheckRegOperand(raw_ostream &OS, int OpIndex,
87 const Record *Reg,
88 StringRef FunctionMapper) {
89 assert(Reg->isSubClassOf("Register") && "Expected a register Record!");
90
91 if (!FunctionMapper.empty())
92 OS << FunctionMapper << "(";
93 OS << "MI" << (isByRef() ? "." : "->") << "getOperand(" << OpIndex
94 << ").getReg()";
95 if (!FunctionMapper.empty())
96 OS << ")";
97 OS << (shouldNegate() ? " != " : " == ");
98 const StringRef Str = Reg->getValueAsString(FieldName: "Namespace");
99 if (!Str.empty())
100 OS << Str << "::";
101 OS << Reg->getName();
102}
103
104void PredicateExpander::expandCheckRegOperandSimple(raw_ostream &OS,
105 int OpIndex,
106 StringRef FunctionMapper) {
107 if (shouldNegate())
108 OS << "!";
109 if (!FunctionMapper.empty())
110 OS << FunctionMapper << "(";
111 OS << "MI" << (isByRef() ? "." : "->") << "getOperand(" << OpIndex
112 << ").getReg()";
113 if (!FunctionMapper.empty())
114 OS << ")";
115}
116
117void PredicateExpander::expandCheckInvalidRegOperand(raw_ostream &OS,
118 int OpIndex) {
119 if (!shouldNegate())
120 OS << "!";
121 OS << "MI" << (isByRef() ? "." : "->") << "getOperand(" << OpIndex
122 << ").getReg().isValid()";
123}
124
125void PredicateExpander::expandCheckSameRegOperand(raw_ostream &OS, int First,
126 int Second) {
127 OS << "MI" << (isByRef() ? "." : "->") << "getOperand(" << First
128 << ").getReg() " << (shouldNegate() ? "!=" : "==") << " MI"
129 << (isByRef() ? "." : "->") << "getOperand(" << Second << ").getReg()";
130}
131
132void PredicateExpander::expandCheckNumOperands(raw_ostream &OS, int NumOps) {
133 OS << "MI" << (isByRef() ? "." : "->") << "getNumOperands() "
134 << (shouldNegate() ? "!= " : "== ") << NumOps;
135}
136
137void PredicateExpander::expandCheckOpcode(raw_ostream &OS, const Record *Inst) {
138 OS << "MI" << (isByRef() ? "." : "->") << "getOpcode() "
139 << (shouldNegate() ? "!= " : "== ") << Inst->getValueAsString(FieldName: "Namespace")
140 << "::" << Inst->getName();
141}
142
143void PredicateExpander::expandCheckOpcode(raw_ostream &OS,
144 ArrayRef<const Record *> Opcodes) {
145 assert(!Opcodes.empty() && "Expected at least one opcode to check!");
146
147 if (Opcodes.size() == 1) {
148 OS << "( ";
149 expandCheckOpcode(OS, Inst: Opcodes[0]);
150 OS << " )";
151 return;
152 }
153
154 if (shouldNegate())
155 OS << '!';
156 OS << "llvm::is_contained(";
157 ListSeparator Sep;
158 OS << '{';
159 for (const Record *Inst : Opcodes)
160 OS << Sep << Inst->getValueAsString(FieldName: "Namespace") << "::" << Inst->getName();
161 OS << '}';
162 OS << ", MI" << (isByRef() ? "." : "->") << "getOpcode())";
163}
164
165void PredicateExpander::expandCheckPseudo(raw_ostream &OS,
166 ArrayRef<const Record *> Opcodes) {
167 if (shouldExpandForMC())
168 expandFalse(OS);
169 else
170 expandCheckOpcode(OS, Opcodes);
171}
172
173void PredicateExpander::expandPredicateSequence(
174 raw_ostream &OS, ArrayRef<const Record *> Sequence, bool IsCheckAll) {
175 assert(!Sequence.empty() && "Found an invalid empty predicate set!");
176 if (Sequence.size() == 1)
177 return expandPredicate(OS, Rec: Sequence[0]);
178
179 // Okay, there is more than one predicate in the set.
180 ListSeparator LS(IsCheckAll ? "&& " : "|| ");
181 OS << (shouldNegate() ? "!(" : "(");
182 ++Indent;
183
184 bool OldValue = shouldNegate();
185 setNegatePredicate(false);
186 for (const Record *Rec : Sequence) {
187 OS << '\n' << Indent << LS;
188 expandPredicate(OS, Rec);
189 }
190 --Indent;
191 OS << '\n' << Indent << ')';
192 setNegatePredicate(OldValue);
193}
194
195void PredicateExpander::expandTIIFunctionCall(raw_ostream &OS,
196 StringRef MethodName) {
197 OS << (shouldNegate() ? "!" : "");
198 OS << TargetName << (shouldExpandForMC() ? "_MC::" : "InstrInfo::");
199 OS << MethodName << (isByRef() ? "(MI)" : "(*MI)");
200}
201
202void PredicateExpander::expandCheckIsRegOperand(raw_ostream &OS, int OpIndex) {
203 OS << (shouldNegate() ? "!" : "") << "MI" << (isByRef() ? "." : "->")
204 << "getOperand(" << OpIndex << ").isReg() ";
205}
206
207void PredicateExpander::expandCheckIsVRegOperand(raw_ostream &OS, int OpIndex) {
208 OS << (shouldNegate() ? "!" : "") << "MI" << (isByRef() ? "." : "->")
209 << "getOperand(" << OpIndex << ").getReg().isVirtual()";
210}
211
212void PredicateExpander::expandCheckIsImmOperand(raw_ostream &OS, int OpIndex) {
213 OS << (shouldNegate() ? "!" : "") << "MI" << (isByRef() ? "." : "->")
214 << "getOperand(" << OpIndex << ").isImm() ";
215}
216
217void PredicateExpander::expandCheckFunctionPredicateWithTII(
218 raw_ostream &OS, StringRef MCInstFn, StringRef MachineInstrFn,
219 StringRef TIIPtr) {
220 if (!shouldExpandForMC()) {
221 OS << (TIIPtr.empty() ? "TII" : TIIPtr) << "->" << MachineInstrFn;
222 OS << (isByRef() ? "(MI)" : "(*MI)");
223 return;
224 }
225
226 OS << MCInstFn << (isByRef() ? "(MI" : "(*MI") << ", MCII)";
227}
228
229void PredicateExpander::expandCheckFunctionPredicate(raw_ostream &OS,
230 StringRef MCInstFn,
231 StringRef MachineInstrFn) {
232 OS << (shouldExpandForMC() ? MCInstFn : MachineInstrFn)
233 << (isByRef() ? "(MI)" : "(*MI)");
234}
235
236void PredicateExpander::expandCheckNonPortable(raw_ostream &OS,
237 StringRef Code) {
238 if (shouldExpandForMC())
239 return expandFalse(OS);
240
241 OS << '(' << Code << ')';
242}
243
244void PredicateExpander::expandReturnStatement(raw_ostream &OS,
245 const Record *Rec) {
246 std::string Buffer;
247 raw_string_ostream SS(Buffer);
248
249 SS << "return ";
250 expandPredicate(OS&: SS, Rec);
251 SS << ";";
252 OS << Buffer;
253}
254
255void PredicateExpander::expandOpcodeSwitchCase(raw_ostream &OS,
256 const Record *Rec) {
257 for (const Record *Opcode : Rec->getValueAsListOfDefs(FieldName: "Opcodes")) {
258 OS << Indent << "case " << Opcode->getValueAsString(FieldName: "Namespace")
259 << "::" << Opcode->getName() << ":\n";
260 }
261
262 ++Indent;
263 OS << Indent;
264 expandStatement(OS, Rec: Rec->getValueAsDef(FieldName: "CaseStmt"));
265 --Indent;
266}
267
268void PredicateExpander::expandOpcodeSwitchStatement(
269 raw_ostream &OS, ArrayRef<const Record *> Cases, const Record *Default) {
270 std::string Buffer;
271 raw_string_ostream SS(Buffer);
272
273 SS << "switch(MI" << (isByRef() ? "." : "->") << "getOpcode()) {\n";
274 for (const Record *Rec : Cases) {
275 expandOpcodeSwitchCase(OS&: SS, Rec);
276 SS << '\n';
277 }
278
279 // Expand the default case.
280 SS << Indent << "default:\n";
281
282 ++Indent;
283 SS << Indent;
284 expandStatement(OS&: SS, Rec: Default);
285 SS << '\n' << Indent << "} // end of switch-stmt";
286 OS << Buffer;
287}
288
289void PredicateExpander::expandStatement(raw_ostream &OS, const Record *Rec) {
290 // Assume that padding has been added by the caller.
291 if (Rec->isSubClassOf(Name: "MCOpcodeSwitchStatement")) {
292 expandOpcodeSwitchStatement(OS, Cases: Rec->getValueAsListOfDefs(FieldName: "Cases"),
293 Default: Rec->getValueAsDef(FieldName: "DefaultCase"));
294 return;
295 }
296
297 if (Rec->isSubClassOf(Name: "MCReturnStatement")) {
298 expandReturnStatement(OS, Rec: Rec->getValueAsDef(FieldName: "Pred"));
299 return;
300 }
301
302 llvm_unreachable("No known rules to expand this MCStatement");
303}
304
305void PredicateExpander::expandPredicate(raw_ostream &OS, const Record *Rec) {
306 // Assume that padding has been added by the caller.
307 if (Rec->isSubClassOf(Name: "MCTrue")) {
308 if (shouldNegate())
309 return expandFalse(OS);
310 return expandTrue(OS);
311 }
312
313 if (Rec->isSubClassOf(Name: "MCFalse")) {
314 if (shouldNegate())
315 return expandTrue(OS);
316 return expandFalse(OS);
317 }
318
319 if (Rec->isSubClassOf(Name: "CheckNot")) {
320 flipNegatePredicate();
321 expandPredicate(OS, Rec: Rec->getValueAsDef(FieldName: "Pred"));
322 flipNegatePredicate();
323 return;
324 }
325
326 if (Rec->isSubClassOf(Name: "CheckIsRegOperand"))
327 return expandCheckIsRegOperand(OS, OpIndex: Rec->getValueAsInt(FieldName: "OpIndex"));
328
329 if (Rec->isSubClassOf(Name: "CheckIsVRegOperand"))
330 return expandCheckIsVRegOperand(OS, OpIndex: Rec->getValueAsInt(FieldName: "OpIndex"));
331
332 if (Rec->isSubClassOf(Name: "CheckIsImmOperand"))
333 return expandCheckIsImmOperand(OS, OpIndex: Rec->getValueAsInt(FieldName: "OpIndex"));
334
335 if (Rec->isSubClassOf(Name: "CheckRegOperand"))
336 return expandCheckRegOperand(OS, OpIndex: Rec->getValueAsInt(FieldName: "OpIndex"),
337 Reg: Rec->getValueAsDef(FieldName: "Reg"),
338 FunctionMapper: Rec->getValueAsString(FieldName: "FunctionMapper"));
339
340 if (Rec->isSubClassOf(Name: "CheckRegOperandSimple"))
341 return expandCheckRegOperandSimple(OS, OpIndex: Rec->getValueAsInt(FieldName: "OpIndex"),
342 FunctionMapper: Rec->getValueAsString(FieldName: "FunctionMapper"));
343
344 if (Rec->isSubClassOf(Name: "CheckInvalidRegOperand"))
345 return expandCheckInvalidRegOperand(OS, OpIndex: Rec->getValueAsInt(FieldName: "OpIndex"));
346
347 if (Rec->isSubClassOf(Name: "CheckImmOperand"))
348 return expandCheckImmOperand(OS, OpIndex: Rec->getValueAsInt(FieldName: "OpIndex"),
349 ImmVal: Rec->getValueAsInt(FieldName: "ImmVal"),
350 FunctionMapper: Rec->getValueAsString(FieldName: "FunctionMapper"));
351
352 if (Rec->isSubClassOf(Name: "CheckImmOperand_s"))
353 return expandCheckImmOperand(OS, OpIndex: Rec->getValueAsInt(FieldName: "OpIndex"),
354 ImmVal: Rec->getValueAsString(FieldName: "ImmVal"),
355 FunctionMapper: Rec->getValueAsString(FieldName: "FunctionMapper"));
356
357 if (Rec->isSubClassOf(Name: "CheckImmOperandLT"))
358 return expandCheckImmOperandLT(OS, OpIndex: Rec->getValueAsInt(FieldName: "OpIndex"),
359 ImmVal: Rec->getValueAsInt(FieldName: "ImmVal"),
360 FunctionMapper: Rec->getValueAsString(FieldName: "FunctionMapper"));
361
362 if (Rec->isSubClassOf(Name: "CheckImmOperandGT"))
363 return expandCheckImmOperandGT(OS, OpIndex: Rec->getValueAsInt(FieldName: "OpIndex"),
364 ImmVal: Rec->getValueAsInt(FieldName: "ImmVal"),
365 FunctionMapper: Rec->getValueAsString(FieldName: "FunctionMapper"));
366
367 if (Rec->isSubClassOf(Name: "CheckImmOperandSimple"))
368 return expandCheckImmOperandSimple(OS, OpIndex: Rec->getValueAsInt(FieldName: "OpIndex"),
369 FunctionMapper: Rec->getValueAsString(FieldName: "FunctionMapper"));
370
371 if (Rec->isSubClassOf(Name: "CheckSameRegOperand"))
372 return expandCheckSameRegOperand(OS, First: Rec->getValueAsInt(FieldName: "FirstIndex"),
373 Second: Rec->getValueAsInt(FieldName: "SecondIndex"));
374
375 if (Rec->isSubClassOf(Name: "CheckNumOperands"))
376 return expandCheckNumOperands(OS, NumOps: Rec->getValueAsInt(FieldName: "NumOps"));
377
378 if (Rec->isSubClassOf(Name: "CheckPseudo"))
379 return expandCheckPseudo(OS, Opcodes: Rec->getValueAsListOfDefs(FieldName: "ValidOpcodes"));
380
381 if (Rec->isSubClassOf(Name: "CheckOpcode"))
382 return expandCheckOpcode(OS, Opcodes: Rec->getValueAsListOfDefs(FieldName: "ValidOpcodes"));
383
384 if (Rec->isSubClassOf(Name: "CheckAll"))
385 return expandPredicateSequence(OS, Sequence: Rec->getValueAsListOfDefs(FieldName: "Predicates"),
386 /* AllOf */ IsCheckAll: true);
387
388 if (Rec->isSubClassOf(Name: "CheckAny"))
389 return expandPredicateSequence(OS, Sequence: Rec->getValueAsListOfDefs(FieldName: "Predicates"),
390 /* AllOf */ IsCheckAll: false);
391
392 if (Rec->isSubClassOf(Name: "CheckFunctionPredicate")) {
393 return expandCheckFunctionPredicate(
394 OS, MCInstFn: Rec->getValueAsString(FieldName: "MCInstFnName"),
395 MachineInstrFn: Rec->getValueAsString(FieldName: "MachineInstrFnName"));
396 }
397
398 if (Rec->isSubClassOf(Name: "CheckFunctionPredicateWithTII")) {
399 return expandCheckFunctionPredicateWithTII(
400 OS, MCInstFn: Rec->getValueAsString(FieldName: "MCInstFnName"),
401 MachineInstrFn: Rec->getValueAsString(FieldName: "MachineInstrFnName"),
402 TIIPtr: Rec->getValueAsString(FieldName: "TIIPtrName"));
403 }
404
405 if (Rec->isSubClassOf(Name: "CheckNonPortable"))
406 return expandCheckNonPortable(OS, Code: Rec->getValueAsString(FieldName: "CodeBlock"));
407
408 if (Rec->isSubClassOf(Name: "TIIPredicate"))
409 return expandTIIFunctionCall(OS, MethodName: Rec->getValueAsString(FieldName: "FunctionName"));
410
411 llvm_unreachable("No known rules to expand this MCInstPredicate");
412}
413
414void STIPredicateExpander::expandHeader(raw_ostream &OS,
415 const STIPredicateFunction &Fn) {
416 const Record *Rec = Fn.getDeclaration();
417 StringRef FunctionName = Rec->getValueAsString(FieldName: "Name");
418
419 OS << Indent << "bool ";
420 if (shouldExpandDefinition())
421 OS << getClassPrefix() << "::";
422 OS << FunctionName << "(";
423 if (shouldExpandForMC())
424 OS << "const MCInst " << (isByRef() ? "&" : "*") << "MI";
425 else
426 OS << "const MachineInstr " << (isByRef() ? "&" : "*") << "MI";
427 if (Rec->getValueAsBit(FieldName: "UpdatesOpcodeMask"))
428 OS << ", APInt &Mask";
429 OS << (shouldExpandForMC() ? ", unsigned ProcessorID) const " : ") const ");
430 if (shouldExpandDefinition()) {
431 OS << "{\n";
432 return;
433 }
434
435 if (Rec->getValueAsBit(FieldName: "OverridesBaseClassMember"))
436 OS << "override";
437 OS << ";\n";
438}
439
440void STIPredicateExpander::expandPrologue(raw_ostream &OS,
441 const STIPredicateFunction &Fn) {
442 bool UpdatesOpcodeMask =
443 Fn.getDeclaration()->getValueAsBit(FieldName: "UpdatesOpcodeMask");
444
445 ++Indent;
446 for (const Record *Delegate :
447 Fn.getDeclaration()->getValueAsListOfDefs(FieldName: "Delegates")) {
448 OS << Indent << "if (" << Delegate->getValueAsString(FieldName: "Name") << "(MI";
449 if (UpdatesOpcodeMask)
450 OS << ", Mask";
451 if (shouldExpandForMC())
452 OS << ", ProcessorID";
453 OS << "))\n";
454 OS << Indent + 1 << "return true;\n\n";
455 }
456
457 if (shouldExpandForMC())
458 return;
459
460 OS << Indent << "unsigned ProcessorID = getSchedModel().getProcessorID();\n";
461}
462
463void STIPredicateExpander::expandOpcodeGroup(raw_ostream &OS,
464 const OpcodeGroup &Group,
465 bool ShouldUpdateOpcodeMask) {
466 const OpcodeInfo &OI = Group.getOpcodeInfo();
467 for (const PredicateInfo &PI : OI.getPredicates()) {
468 const APInt &ProcModelMask = PI.ProcModelMask;
469 bool FirstProcID = true;
470 for (unsigned I = 0, E = ProcModelMask.getActiveBits(); I < E; ++I) {
471 if (!ProcModelMask[I])
472 continue;
473
474 if (FirstProcID) {
475 OS << Indent << "if (ProcessorID == " << I;
476 } else {
477 OS << " || ProcessorID == " << I;
478 }
479 FirstProcID = false;
480 }
481
482 OS << ") {\n";
483
484 ++Indent;
485 OS << Indent;
486 if (ShouldUpdateOpcodeMask) {
487 if (PI.OperandMask.isZero())
488 OS << "Mask.clearAllBits();\n";
489 else
490 OS << "Mask = " << PI.OperandMask << ";\n";
491 OS << Indent;
492 }
493 OS << "return ";
494 expandPredicate(OS, Rec: PI.Predicate);
495 OS << ";\n";
496 --Indent;
497 OS << Indent << "}\n";
498 }
499}
500
501void STIPredicateExpander::expandBody(raw_ostream &OS,
502 const STIPredicateFunction &Fn) {
503 bool UpdatesOpcodeMask =
504 Fn.getDeclaration()->getValueAsBit(FieldName: "UpdatesOpcodeMask");
505
506 OS << Indent << "switch(MI" << (isByRef() ? "." : "->") << "getOpcode()) {\n";
507 OS << Indent << "default:\n";
508 OS << Indent << " break;";
509
510 for (const OpcodeGroup &Group : Fn.getGroups()) {
511 for (const Record *Opcode : Group.getOpcodes()) {
512 OS << '\n'
513 << Indent << "case " << getTargetName() << "::" << Opcode->getName()
514 << ":";
515 }
516
517 OS << '\n';
518 ++Indent;
519 expandOpcodeGroup(OS, Group, ShouldUpdateOpcodeMask: UpdatesOpcodeMask);
520
521 OS << Indent << "break;\n";
522 --Indent;
523 }
524
525 OS << Indent << "}\n";
526}
527
528void STIPredicateExpander::expandEpilogue(raw_ostream &OS,
529 const STIPredicateFunction &Fn) {
530 OS << '\n' << Indent;
531 OS << "return ";
532 expandPredicate(OS, Rec: Fn.getDefaultReturnPredicate());
533 OS << ";\n";
534
535 --Indent;
536 StringRef FunctionName = Fn.getDeclaration()->getValueAsString(FieldName: "Name");
537 OS << Indent << "} // " << ClassPrefix << "::" << FunctionName << "\n\n";
538}
539
540void STIPredicateExpander::expandSTIPredicate(raw_ostream &OS,
541 const STIPredicateFunction &Fn) {
542 const Record *Rec = Fn.getDeclaration();
543 if (shouldExpandForMC() && !Rec->getValueAsBit(FieldName: "ExpandForMC"))
544 return;
545
546 expandHeader(OS, Fn);
547 if (shouldExpandDefinition()) {
548 expandPrologue(OS, Fn);
549 expandBody(OS, Fn);
550 expandEpilogue(OS, Fn);
551 }
552}
553