|
| 1 | +//===------ MacroFusionPredicatorEmitter.cpp - Generator for Fusion ------===// |
| 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 | +// |
| 9 | +// MacroFusionPredicatorEmitter implements a TableGen-driven predicators |
| 10 | +// generator for macro-op fusions. |
| 11 | +// |
| 12 | +//===---------------------------------------------------------------------===// |
| 13 | + |
| 14 | +#include "CodeGenTarget.h" |
| 15 | +#include "PredicateExpander.h" |
| 16 | +#include "llvm/ADT/SmallVector.h" |
| 17 | +#include "llvm/Support/Debug.h" |
| 18 | +#include "llvm/TableGen/Error.h" |
| 19 | +#include "llvm/TableGen/Record.h" |
| 20 | +#include "llvm/TableGen/TableGenBackend.h" |
| 21 | +#include <set> |
| 22 | +#include <vector> |
| 23 | + |
| 24 | +using namespace llvm; |
| 25 | + |
| 26 | +#define DEBUG_TYPE "macro-fusion-predicator" |
| 27 | + |
| 28 | +namespace { |
| 29 | +class MacroFusionPredicatorEmitter { |
| 30 | + RecordKeeper &Records; |
| 31 | + CodeGenTarget Target; |
| 32 | + |
| 33 | + void emitMacroFusionDecl(std::vector<Record *> Fusions, PredicateExpander &PE, |
| 34 | + raw_ostream &OS); |
| 35 | + void emitMacroFusionImpl(std::vector<Record *> Fusions, PredicateExpander &PE, |
| 36 | + raw_ostream &OS); |
| 37 | + void emitPredicates(std::vector<Record *> &FirstPredicate, |
| 38 | + PredicateExpander &PE, raw_ostream &OS); |
| 39 | + void emitFirstPredicate(Record *SecondPredicate, PredicateExpander &PE, |
| 40 | + raw_ostream &OS); |
| 41 | + void emitSecondPredicate(Record *SecondPredicate, PredicateExpander &PE, |
| 42 | + raw_ostream &OS); |
| 43 | + void emitBothPredicate(Record *Predicates, PredicateExpander &PE, |
| 44 | + raw_ostream &OS); |
| 45 | + |
| 46 | +public: |
| 47 | + MacroFusionPredicatorEmitter(RecordKeeper &R) : Records(R), Target(R) {} |
| 48 | + |
| 49 | + void run(raw_ostream &OS); |
| 50 | +}; |
| 51 | +} // End anonymous namespace. |
| 52 | + |
| 53 | +void MacroFusionPredicatorEmitter::emitMacroFusionDecl( |
| 54 | + std::vector<Record *> Fusions, PredicateExpander &PE, raw_ostream &OS) { |
| 55 | + OS << "#ifdef GET_" << Target.getName() << "_MACRO_FUSION_PRED_DECL\n\n"; |
| 56 | + |
| 57 | + for (Record *Fusion : Fusions) { |
| 58 | + OS << "bool is" << Fusion->getName() << "(const TargetInstrInfo &, " |
| 59 | + << "const TargetSubtargetInfo &, " |
| 60 | + << "const MachineInstr *, " |
| 61 | + << "const MachineInstr &);\n"; |
| 62 | + } |
| 63 | + |
| 64 | + OS << "\n#endif\n"; |
| 65 | + OS << "#undef GET_" << Target.getName() << "_MACRO_FUSION_PRED_DECL\n"; |
| 66 | +} |
| 67 | + |
| 68 | +void MacroFusionPredicatorEmitter::emitMacroFusionImpl( |
| 69 | + std::vector<Record *> Fusions, PredicateExpander &PE, raw_ostream &OS) { |
| 70 | + OS << "#ifdef GET_" << Target.getName() << "_MACRO_FUSION_PRED_IMPL\n\n"; |
| 71 | + |
| 72 | + for (Record *Fusion : Fusions) { |
| 73 | + std::vector<Record *> Predicates = |
| 74 | + Fusion->getValueAsListOfDefs("Predicates"); |
| 75 | + |
| 76 | + OS << "bool is" << Fusion->getName() << "(\n"; |
| 77 | + OS.indent(5) << "const TargetInstrInfo &TII,\n"; |
| 78 | + OS.indent(5) << "const TargetSubtargetInfo &STI,\n"; |
| 79 | + OS.indent(5) << "const MachineInstr *FirstMI,\n"; |
| 80 | + OS.indent(5) << "const MachineInstr &SecondMI) {\n"; |
| 81 | + OS.indent(2) << "auto &MRI = SecondMI.getMF()->getRegInfo();\n"; |
| 82 | + |
| 83 | + emitPredicates(Predicates, PE, OS); |
| 84 | + |
| 85 | + OS.indent(2) << "return true;\n"; |
| 86 | + OS << "}\n"; |
| 87 | + } |
| 88 | + |
| 89 | + OS << "\n#endif\n"; |
| 90 | + OS << "#undef GET_" << Target.getName() << "_MACRO_FUSION_PRED_IMPL\n\n"; |
| 91 | +} |
| 92 | + |
| 93 | +void MacroFusionPredicatorEmitter::emitPredicates( |
| 94 | + std::vector<Record *> &Predicates, PredicateExpander &PE, raw_ostream &OS) { |
| 95 | + for (Record *Predicate : Predicates) { |
| 96 | + Record *Target = Predicate->getValueAsDef("Target"); |
| 97 | + if (Target->getName() == "first") |
| 98 | + emitFirstPredicate(Predicate, PE, OS); |
| 99 | + else if (Target->getName() == "second") |
| 100 | + emitSecondPredicate(Predicate, PE, OS); |
| 101 | + else if (Target->getName() == "both") |
| 102 | + emitBothPredicate(Predicate, PE, OS); |
| 103 | + else |
| 104 | + PrintFatalError(Target->getLoc(), |
| 105 | + "Unsupported 'FusionTarget': " + Target->getName()); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +void MacroFusionPredicatorEmitter::emitFirstPredicate(Record *Predicate, |
| 110 | + PredicateExpander &PE, |
| 111 | + raw_ostream &OS) { |
| 112 | + if (Predicate->isSubClassOf("WildcardPred")) { |
| 113 | + OS.indent(2) << "if (!FirstMI)\n"; |
| 114 | + OS.indent(2) << " return " |
| 115 | + << (Predicate->getValueAsBit("ReturnValue") ? "true" : "false") |
| 116 | + << ";\n"; |
| 117 | + } else if (Predicate->isSubClassOf("OneUsePred")) { |
| 118 | + OS.indent(2) << "{\n"; |
| 119 | + OS.indent(4) << "Register FirstDest = FirstMI->getOperand(0).getReg();\n"; |
| 120 | + OS.indent(4) |
| 121 | + << "if (FirstDest.isVirtual() && !MRI.hasOneNonDBGUse(FirstDest))\n"; |
| 122 | + OS.indent(4) << " return false;\n"; |
| 123 | + OS.indent(2) << "}\n"; |
| 124 | + } else if (Predicate->isSubClassOf( |
| 125 | + "FirstFusionPredicateWithMCInstPredicate")) { |
| 126 | + OS.indent(2) << "{\n"; |
| 127 | + OS.indent(4) << "const MachineInstr *MI = FirstMI;\n"; |
| 128 | + OS.indent(4) << "if ("; |
| 129 | + PE.setNegatePredicate(true); |
| 130 | + PE.setIndentLevel(3); |
| 131 | + PE.expandPredicate(OS, Predicate->getValueAsDef("Predicate")); |
| 132 | + OS << ")\n"; |
| 133 | + OS.indent(4) << " return false;\n"; |
| 134 | + OS.indent(2) << "}\n"; |
| 135 | + } else { |
| 136 | + PrintFatalError(Predicate->getLoc(), |
| 137 | + "Unsupported predicate for first instruction: " + |
| 138 | + Predicate->getType()->getAsString()); |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +void MacroFusionPredicatorEmitter::emitSecondPredicate(Record *Predicate, |
| 143 | + PredicateExpander &PE, |
| 144 | + raw_ostream &OS) { |
| 145 | + if (Predicate->isSubClassOf("SecondFusionPredicateWithMCInstPredicate")) { |
| 146 | + OS.indent(2) << "{\n"; |
| 147 | + OS.indent(4) << "const MachineInstr *MI = &SecondMI;\n"; |
| 148 | + OS.indent(4) << "if ("; |
| 149 | + PE.setNegatePredicate(true); |
| 150 | + PE.setIndentLevel(3); |
| 151 | + PE.expandPredicate(OS, Predicate->getValueAsDef("Predicate")); |
| 152 | + OS << ")\n"; |
| 153 | + OS.indent(4) << " return false;\n"; |
| 154 | + OS.indent(2) << "}\n"; |
| 155 | + } else { |
| 156 | + PrintFatalError(Predicate->getLoc(), |
| 157 | + "Unsupported predicate for first instruction: " + |
| 158 | + Predicate->getType()->getAsString()); |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +void MacroFusionPredicatorEmitter::emitBothPredicate(Record *Predicate, |
| 163 | + PredicateExpander &PE, |
| 164 | + raw_ostream &OS) { |
| 165 | + if (Predicate->isSubClassOf("FusionPredicateWithCode")) |
| 166 | + OS << Predicate->getValueAsString("Predicate"); |
| 167 | + else if (Predicate->isSubClassOf("BothFusionPredicateWithMCInstPredicate")) { |
| 168 | + Record *MCPred = Predicate->getValueAsDef("Predicate"); |
| 169 | + emitFirstPredicate(MCPred, PE, OS); |
| 170 | + emitSecondPredicate(MCPred, PE, OS); |
| 171 | + } else if (Predicate->isSubClassOf("TieReg")) { |
| 172 | + int FirstOpIdx = Predicate->getValueAsInt("FirstOpIdx"); |
| 173 | + int SecondOpIdx = Predicate->getValueAsInt("SecondOpIdx"); |
| 174 | + OS.indent(2) << "if (!(FirstMI->getOperand(" << FirstOpIdx |
| 175 | + << ").isReg() &&\n"; |
| 176 | + OS.indent(2) << " SecondMI.getOperand(" << SecondOpIdx |
| 177 | + << ").isReg() &&\n"; |
| 178 | + OS.indent(2) << " FirstMI->getOperand(" << FirstOpIdx |
| 179 | + << ").getReg() == SecondMI.getOperand(" << SecondOpIdx |
| 180 | + << ").getReg()))\n"; |
| 181 | + OS.indent(2) << " return false;\n"; |
| 182 | + } else |
| 183 | + PrintFatalError(Predicate->getLoc(), |
| 184 | + "Unsupported predicate for both instruction: " + |
| 185 | + Predicate->getType()->getAsString()); |
| 186 | +} |
| 187 | + |
| 188 | +void MacroFusionPredicatorEmitter::run(raw_ostream &OS) { |
| 189 | + // Emit file header. |
| 190 | + emitSourceFileHeader("Macro Fusion Predicators", OS); |
| 191 | + |
| 192 | + PredicateExpander PE(Target.getName()); |
| 193 | + PE.setByRef(false); |
| 194 | + PE.setExpandForMC(false); |
| 195 | + |
| 196 | + std::vector<Record *> Fusions = Records.getAllDerivedDefinitions("Fusion"); |
| 197 | + // Sort macro fusions by name. |
| 198 | + sort(Fusions, LessRecord()); |
| 199 | + emitMacroFusionDecl(Fusions, PE, OS); |
| 200 | + emitMacroFusionImpl(Fusions, PE, OS); |
| 201 | +} |
| 202 | + |
| 203 | +static TableGen::Emitter::OptClass<MacroFusionPredicatorEmitter> |
| 204 | + X("gen-macro-fusion-pred", "Generate macro fusion predicators."); |
0 commit comments