2018-05-25 23:55:37 +08:00
|
|
|
//===--------------------- PredicateExpander.cpp --------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// \file
|
|
|
|
/// Functionalities used by the Tablegen backends to expand machine predicates.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "PredicateExpander.h"
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2018-08-13 23:13:35 +08:00
|
|
|
void PredicateExpander::expandTrue(raw_ostream &OS) { OS << "true"; }
|
|
|
|
void PredicateExpander::expandFalse(raw_ostream &OS) { OS << "false"; }
|
2018-05-25 23:55:37 +08:00
|
|
|
|
2018-08-13 23:13:35 +08:00
|
|
|
void PredicateExpander::expandCheckImmOperand(raw_ostream &OS, int OpIndex,
|
|
|
|
int ImmVal) {
|
2018-07-18 00:11:37 +08:00
|
|
|
OS << "MI" << (isByRef() ? "." : "->") << "getOperand(" << OpIndex
|
|
|
|
<< ").getImm() " << (shouldNegate() ? "!= " : "== ") << ImmVal;
|
2018-05-25 23:55:37 +08:00
|
|
|
}
|
|
|
|
|
2018-08-13 23:13:35 +08:00
|
|
|
void PredicateExpander::expandCheckImmOperand(raw_ostream &OS, int OpIndex,
|
|
|
|
StringRef ImmVal) {
|
2018-07-18 00:11:37 +08:00
|
|
|
OS << "MI" << (isByRef() ? "." : "->") << "getOperand(" << OpIndex
|
|
|
|
<< ").getImm() " << (shouldNegate() ? "!= " : "== ") << ImmVal;
|
2018-05-25 23:55:37 +08:00
|
|
|
}
|
|
|
|
|
2018-08-13 23:13:35 +08:00
|
|
|
void PredicateExpander::expandCheckRegOperand(raw_ostream &OS, int OpIndex,
|
|
|
|
const Record *Reg) {
|
2018-05-25 23:55:37 +08:00
|
|
|
assert(Reg->isSubClassOf("Register") && "Expected a register Record!");
|
|
|
|
|
|
|
|
OS << "MI" << (isByRef() ? "." : "->") << "getOperand(" << OpIndex
|
|
|
|
<< ").getReg() " << (shouldNegate() ? "!= " : "== ");
|
|
|
|
const StringRef Str = Reg->getValueAsString("Namespace");
|
|
|
|
if (!Str.empty())
|
|
|
|
OS << Str << "::";
|
|
|
|
OS << Reg->getName();
|
|
|
|
}
|
|
|
|
|
2018-08-13 23:13:35 +08:00
|
|
|
void PredicateExpander::expandCheckInvalidRegOperand(raw_ostream &OS,
|
2018-07-18 19:03:22 +08:00
|
|
|
int OpIndex) {
|
|
|
|
OS << "MI" << (isByRef() ? "." : "->") << "getOperand(" << OpIndex
|
|
|
|
<< ").getReg() " << (shouldNegate() ? "!= " : "== ") << "0";
|
|
|
|
}
|
|
|
|
|
2018-08-13 23:13:35 +08:00
|
|
|
void PredicateExpander::expandCheckSameRegOperand(raw_ostream &OS, int First,
|
|
|
|
int Second) {
|
2018-05-25 23:55:37 +08:00
|
|
|
OS << "MI" << (isByRef() ? "." : "->") << "getOperand(" << First
|
|
|
|
<< ").getReg() " << (shouldNegate() ? "!=" : "==") << " MI"
|
|
|
|
<< (isByRef() ? "." : "->") << "getOperand(" << Second << ").getReg()";
|
|
|
|
}
|
|
|
|
|
2018-08-13 23:13:35 +08:00
|
|
|
void PredicateExpander::expandCheckNumOperands(raw_ostream &OS, int NumOps) {
|
2018-05-25 23:55:37 +08:00
|
|
|
OS << "MI" << (isByRef() ? "." : "->") << "getNumOperands() "
|
|
|
|
<< (shouldNegate() ? "!= " : "== ") << NumOps;
|
|
|
|
}
|
|
|
|
|
2018-08-13 23:13:35 +08:00
|
|
|
void PredicateExpander::expandCheckOpcode(raw_ostream &OS, const Record *Inst) {
|
2018-05-25 23:55:37 +08:00
|
|
|
OS << "MI" << (isByRef() ? "." : "->") << "getOpcode() "
|
|
|
|
<< (shouldNegate() ? "!= " : "== ") << Inst->getValueAsString("Namespace")
|
|
|
|
<< "::" << Inst->getName();
|
|
|
|
}
|
|
|
|
|
2018-08-13 23:13:35 +08:00
|
|
|
void PredicateExpander::expandCheckOpcode(raw_ostream &OS,
|
2018-05-25 23:55:37 +08:00
|
|
|
const RecVec &Opcodes) {
|
|
|
|
assert(!Opcodes.empty() && "Expected at least one opcode to check!");
|
|
|
|
bool First = true;
|
|
|
|
|
|
|
|
if (Opcodes.size() == 1) {
|
|
|
|
OS << "( ";
|
|
|
|
expandCheckOpcode(OS, Opcodes[0]);
|
|
|
|
OS << " )";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
OS << '(';
|
|
|
|
increaseIndentLevel();
|
|
|
|
for (const Record *Rec : Opcodes) {
|
|
|
|
OS << '\n';
|
2018-08-13 23:13:35 +08:00
|
|
|
OS.indent(getIndentLevel() * 2);
|
2018-05-25 23:55:37 +08:00
|
|
|
if (!First)
|
|
|
|
OS << (shouldNegate() ? "&& " : "|| ");
|
|
|
|
|
|
|
|
expandCheckOpcode(OS, Rec);
|
|
|
|
First = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
OS << '\n';
|
|
|
|
decreaseIndentLevel();
|
2018-08-13 23:13:35 +08:00
|
|
|
OS.indent(getIndentLevel() * 2);
|
2018-05-25 23:55:37 +08:00
|
|
|
OS << ')';
|
|
|
|
}
|
|
|
|
|
2018-08-13 23:13:35 +08:00
|
|
|
void PredicateExpander::expandCheckPseudo(raw_ostream &OS,
|
2018-05-25 23:55:37 +08:00
|
|
|
const RecVec &Opcodes) {
|
|
|
|
if (shouldExpandForMC())
|
|
|
|
expandFalse(OS);
|
|
|
|
else
|
|
|
|
expandCheckOpcode(OS, Opcodes);
|
|
|
|
}
|
|
|
|
|
2018-08-13 23:13:35 +08:00
|
|
|
void PredicateExpander::expandPredicateSequence(raw_ostream &OS,
|
2018-05-25 23:55:37 +08:00
|
|
|
const RecVec &Sequence,
|
|
|
|
bool IsCheckAll) {
|
|
|
|
assert(!Sequence.empty() && "Found an invalid empty predicate set!");
|
|
|
|
if (Sequence.size() == 1)
|
|
|
|
return expandPredicate(OS, Sequence[0]);
|
|
|
|
|
|
|
|
// Okay, there is more than one predicate in the set.
|
|
|
|
bool First = true;
|
|
|
|
OS << (shouldNegate() ? "!(" : "(");
|
|
|
|
increaseIndentLevel();
|
|
|
|
|
|
|
|
bool OldValue = shouldNegate();
|
|
|
|
setNegatePredicate(false);
|
|
|
|
for (const Record *Rec : Sequence) {
|
|
|
|
OS << '\n';
|
2018-08-13 23:13:35 +08:00
|
|
|
OS.indent(getIndentLevel() * 2);
|
2018-05-25 23:55:37 +08:00
|
|
|
if (!First)
|
|
|
|
OS << (IsCheckAll ? "&& " : "|| ");
|
|
|
|
expandPredicate(OS, Rec);
|
|
|
|
First = false;
|
|
|
|
}
|
|
|
|
OS << '\n';
|
|
|
|
decreaseIndentLevel();
|
2018-08-13 23:13:35 +08:00
|
|
|
OS.indent(getIndentLevel() * 2);
|
2018-05-25 23:55:37 +08:00
|
|
|
OS << ')';
|
|
|
|
setNegatePredicate(OldValue);
|
|
|
|
}
|
|
|
|
|
2018-08-13 23:13:35 +08:00
|
|
|
void PredicateExpander::expandTIIFunctionCall(raw_ostream &OS,
|
2018-05-25 23:55:37 +08:00
|
|
|
StringRef MethodName) {
|
|
|
|
OS << (shouldNegate() ? "!" : "");
|
2018-08-15 02:36:54 +08:00
|
|
|
OS << TargetName << (shouldExpandForMC() ? "_MC::" : "GenInstrInfo::");
|
2018-05-25 23:55:37 +08:00
|
|
|
OS << MethodName << (isByRef() ? "(MI)" : "(*MI)");
|
|
|
|
}
|
|
|
|
|
2018-08-13 23:13:35 +08:00
|
|
|
void PredicateExpander::expandCheckIsRegOperand(raw_ostream &OS, int OpIndex) {
|
2018-05-25 23:55:37 +08:00
|
|
|
OS << (shouldNegate() ? "!" : "") << "MI" << (isByRef() ? "." : "->")
|
|
|
|
<< "getOperand(" << OpIndex << ").isReg() ";
|
|
|
|
}
|
|
|
|
|
2018-08-13 23:13:35 +08:00
|
|
|
void PredicateExpander::expandCheckIsImmOperand(raw_ostream &OS, int OpIndex) {
|
2018-05-25 23:55:37 +08:00
|
|
|
OS << (shouldNegate() ? "!" : "") << "MI" << (isByRef() ? "." : "->")
|
|
|
|
<< "getOperand(" << OpIndex << ").isImm() ";
|
|
|
|
}
|
|
|
|
|
2018-08-13 23:13:35 +08:00
|
|
|
void PredicateExpander::expandCheckFunctionPredicate(raw_ostream &OS,
|
2018-05-25 23:55:37 +08:00
|
|
|
StringRef MCInstFn,
|
|
|
|
StringRef MachineInstrFn) {
|
|
|
|
OS << (shouldExpandForMC() ? MCInstFn : MachineInstrFn)
|
|
|
|
<< (isByRef() ? "(MI)" : "(*MI)");
|
|
|
|
}
|
|
|
|
|
2018-08-13 23:13:35 +08:00
|
|
|
void PredicateExpander::expandCheckNonPortable(raw_ostream &OS,
|
2018-05-25 23:55:37 +08:00
|
|
|
StringRef Code) {
|
|
|
|
if (shouldExpandForMC())
|
|
|
|
return expandFalse(OS);
|
|
|
|
|
|
|
|
OS << '(' << Code << ')';
|
|
|
|
}
|
|
|
|
|
2018-08-13 23:13:35 +08:00
|
|
|
void PredicateExpander::expandReturnStatement(raw_ostream &OS,
|
2018-08-09 23:32:48 +08:00
|
|
|
const Record *Rec) {
|
2018-08-13 23:13:35 +08:00
|
|
|
std::string Buffer;
|
|
|
|
raw_string_ostream SS(Buffer);
|
|
|
|
|
|
|
|
SS << "return ";
|
|
|
|
expandPredicate(SS, Rec);
|
|
|
|
SS << ";";
|
|
|
|
SS.flush();
|
|
|
|
OS << Buffer;
|
2018-08-09 23:32:48 +08:00
|
|
|
}
|
|
|
|
|
2018-08-13 23:13:35 +08:00
|
|
|
void PredicateExpander::expandOpcodeSwitchCase(raw_ostream &OS,
|
2018-08-09 23:32:48 +08:00
|
|
|
const Record *Rec) {
|
|
|
|
const RecVec &Opcodes = Rec->getValueAsListOfDefs("Opcodes");
|
|
|
|
for (const Record *Opcode : Opcodes) {
|
2018-08-13 23:13:35 +08:00
|
|
|
OS.indent(getIndentLevel() * 2);
|
2018-08-09 23:32:48 +08:00
|
|
|
OS << "case " << Opcode->getValueAsString("Namespace")
|
|
|
|
<< "::" << Opcode->getName() << " :\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
increaseIndentLevel();
|
2018-08-13 23:13:35 +08:00
|
|
|
OS.indent(getIndentLevel() * 2);
|
2018-08-09 23:32:48 +08:00
|
|
|
expandStatement(OS, Rec->getValueAsDef("CaseStmt"));
|
|
|
|
decreaseIndentLevel();
|
|
|
|
}
|
|
|
|
|
2018-08-13 23:13:35 +08:00
|
|
|
void PredicateExpander::expandOpcodeSwitchStatement(raw_ostream &OS,
|
2018-08-09 23:32:48 +08:00
|
|
|
const RecVec &Cases,
|
|
|
|
const Record *Default) {
|
2018-08-13 23:13:35 +08:00
|
|
|
std::string Buffer;
|
|
|
|
raw_string_ostream SS(Buffer);
|
2018-08-09 23:32:48 +08:00
|
|
|
|
2018-08-13 23:13:35 +08:00
|
|
|
SS << "switch(MI" << (isByRef() ? "." : "->") << "getOpcode()) {\n";
|
2018-08-09 23:32:48 +08:00
|
|
|
for (const Record *Rec : Cases) {
|
2018-08-13 23:13:35 +08:00
|
|
|
expandOpcodeSwitchCase(SS, Rec);
|
|
|
|
SS << '\n';
|
2018-08-09 23:32:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Expand the default case.
|
2018-08-13 23:13:35 +08:00
|
|
|
SS.indent(getIndentLevel() * 2);
|
|
|
|
SS << "default :\n";
|
|
|
|
|
2018-08-09 23:32:48 +08:00
|
|
|
increaseIndentLevel();
|
2018-08-13 23:13:35 +08:00
|
|
|
SS.indent(getIndentLevel() * 2);
|
|
|
|
expandStatement(SS, Default);
|
2018-08-09 23:32:48 +08:00
|
|
|
decreaseIndentLevel();
|
2018-08-13 23:13:35 +08:00
|
|
|
SS << '\n';
|
2018-08-09 23:32:48 +08:00
|
|
|
|
2018-08-13 23:13:35 +08:00
|
|
|
SS.indent(getIndentLevel() * 2);
|
|
|
|
SS << "} // end of switch-stmt";
|
|
|
|
SS.flush();
|
|
|
|
OS << Buffer;
|
2018-08-09 23:32:48 +08:00
|
|
|
}
|
|
|
|
|
2018-08-13 23:13:35 +08:00
|
|
|
void PredicateExpander::expandStatement(raw_ostream &OS, const Record *Rec) {
|
|
|
|
// Assume that padding has been added by the caller.
|
2018-08-09 23:32:48 +08:00
|
|
|
if (Rec->isSubClassOf("MCOpcodeSwitchStatement")) {
|
|
|
|
expandOpcodeSwitchStatement(OS, Rec->getValueAsListOfDefs("Cases"),
|
|
|
|
Rec->getValueAsDef("DefaultCase"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Rec->isSubClassOf("MCReturnStatement")) {
|
|
|
|
expandReturnStatement(OS, Rec->getValueAsDef("Pred"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("No known rules to expand this MCStatement");
|
|
|
|
}
|
|
|
|
|
2018-08-13 23:13:35 +08:00
|
|
|
void PredicateExpander::expandPredicate(raw_ostream &OS, const Record *Rec) {
|
|
|
|
// Assume that padding has been added by the caller.
|
2018-05-25 23:55:37 +08:00
|
|
|
if (Rec->isSubClassOf("MCTrue")) {
|
|
|
|
if (shouldNegate())
|
|
|
|
return expandFalse(OS);
|
|
|
|
return expandTrue(OS);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Rec->isSubClassOf("MCFalse")) {
|
|
|
|
if (shouldNegate())
|
|
|
|
return expandTrue(OS);
|
|
|
|
return expandFalse(OS);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Rec->isSubClassOf("CheckNot")) {
|
|
|
|
flipNegatePredicate();
|
|
|
|
expandPredicate(OS, Rec->getValueAsDef("Pred"));
|
|
|
|
flipNegatePredicate();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Rec->isSubClassOf("CheckIsRegOperand"))
|
|
|
|
return expandCheckIsRegOperand(OS, Rec->getValueAsInt("OpIndex"));
|
|
|
|
|
|
|
|
if (Rec->isSubClassOf("CheckIsImmOperand"))
|
|
|
|
return expandCheckIsImmOperand(OS, Rec->getValueAsInt("OpIndex"));
|
|
|
|
|
|
|
|
if (Rec->isSubClassOf("CheckRegOperand"))
|
|
|
|
return expandCheckRegOperand(OS, Rec->getValueAsInt("OpIndex"),
|
|
|
|
Rec->getValueAsDef("Reg"));
|
|
|
|
|
2018-07-18 19:03:22 +08:00
|
|
|
if (Rec->isSubClassOf("CheckInvalidRegOperand"))
|
|
|
|
return expandCheckInvalidRegOperand(OS, Rec->getValueAsInt("OpIndex"));
|
|
|
|
|
2018-05-25 23:55:37 +08:00
|
|
|
if (Rec->isSubClassOf("CheckImmOperand"))
|
|
|
|
return expandCheckImmOperand(OS, Rec->getValueAsInt("OpIndex"),
|
|
|
|
Rec->getValueAsInt("ImmVal"));
|
|
|
|
|
|
|
|
if (Rec->isSubClassOf("CheckImmOperand_s"))
|
|
|
|
return expandCheckImmOperand(OS, Rec->getValueAsInt("OpIndex"),
|
|
|
|
Rec->getValueAsString("ImmVal"));
|
|
|
|
|
|
|
|
if (Rec->isSubClassOf("CheckSameRegOperand"))
|
|
|
|
return expandCheckSameRegOperand(OS, Rec->getValueAsInt("FirstIndex"),
|
|
|
|
Rec->getValueAsInt("SecondIndex"));
|
|
|
|
|
|
|
|
if (Rec->isSubClassOf("CheckNumOperands"))
|
|
|
|
return expandCheckNumOperands(OS, Rec->getValueAsInt("NumOps"));
|
|
|
|
|
|
|
|
if (Rec->isSubClassOf("CheckPseudo"))
|
|
|
|
return expandCheckPseudo(OS, Rec->getValueAsListOfDefs("ValidOpcodes"));
|
|
|
|
|
|
|
|
if (Rec->isSubClassOf("CheckOpcode"))
|
|
|
|
return expandCheckOpcode(OS, Rec->getValueAsListOfDefs("ValidOpcodes"));
|
|
|
|
|
|
|
|
if (Rec->isSubClassOf("CheckAll"))
|
|
|
|
return expandPredicateSequence(OS, Rec->getValueAsListOfDefs("Predicates"),
|
|
|
|
/* AllOf */ true);
|
|
|
|
|
|
|
|
if (Rec->isSubClassOf("CheckAny"))
|
|
|
|
return expandPredicateSequence(OS, Rec->getValueAsListOfDefs("Predicates"),
|
|
|
|
/* AllOf */ false);
|
|
|
|
|
|
|
|
if (Rec->isSubClassOf("CheckFunctionPredicate"))
|
|
|
|
return expandCheckFunctionPredicate(
|
|
|
|
OS, Rec->getValueAsString("MCInstFnName"),
|
|
|
|
Rec->getValueAsString("MachineInstrFnName"));
|
|
|
|
|
|
|
|
if (Rec->isSubClassOf("CheckNonPortable"))
|
|
|
|
return expandCheckNonPortable(OS, Rec->getValueAsString("CodeBlock"));
|
|
|
|
|
|
|
|
if (Rec->isSubClassOf("TIIPredicate"))
|
2018-08-15 02:36:54 +08:00
|
|
|
return expandTIIFunctionCall(OS, Rec->getValueAsString("FunctionName"));
|
2018-05-25 23:55:37 +08:00
|
|
|
|
|
|
|
llvm_unreachable("No known rules to expand this MCInstPredicate");
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace llvm
|