2011-07-09 01:36:35 +08:00
|
|
|
//===- PseudoLoweringEmitter.cpp - PseudoLowering Generator -----*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2011-07-09 01:36:35 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CodeGenInstruction.h"
|
2012-06-11 23:37:55 +08:00
|
|
|
#include "CodeGenTarget.h"
|
2011-07-09 01:36:35 +08:00
|
|
|
#include "llvm/ADT/IndexedMap.h"
|
2012-06-11 23:37:55 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2011-07-09 01:36:35 +08:00
|
|
|
#include "llvm/ADT/StringMap.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
2012-06-11 23:37:55 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/TableGen/Error.h"
|
|
|
|
#include "llvm/TableGen/Record.h"
|
|
|
|
#include "llvm/TableGen/TableGenBackend.h"
|
2011-07-09 01:36:35 +08:00
|
|
|
#include <vector>
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 11:06:00 +08:00
|
|
|
#define DEBUG_TYPE "pseudo-lowering"
|
|
|
|
|
2012-06-11 23:37:55 +08:00
|
|
|
namespace {
|
|
|
|
class PseudoLoweringEmitter {
|
|
|
|
struct OpData {
|
|
|
|
enum MapKind { Operand, Imm, Reg };
|
|
|
|
MapKind Kind;
|
|
|
|
union {
|
|
|
|
unsigned Operand; // Operand number mapped to.
|
|
|
|
uint64_t Imm; // Integer immedate value.
|
|
|
|
Record *Reg; // Physical register.
|
|
|
|
} Data;
|
|
|
|
};
|
|
|
|
struct PseudoExpansion {
|
|
|
|
CodeGenInstruction Source; // The source pseudo instruction definition.
|
|
|
|
CodeGenInstruction Dest; // The destination instruction to lower to.
|
|
|
|
IndexedMap<OpData> OperandMap;
|
|
|
|
|
|
|
|
PseudoExpansion(CodeGenInstruction &s, CodeGenInstruction &d,
|
|
|
|
IndexedMap<OpData> &m) :
|
|
|
|
Source(s), Dest(d), OperandMap(m) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
RecordKeeper &Records;
|
|
|
|
|
|
|
|
// It's overkill to have an instance of the full CodeGenTarget object,
|
|
|
|
// but it loads everything on demand, not in the constructor, so it's
|
|
|
|
// lightweight in performance, so it works out OK.
|
|
|
|
CodeGenTarget Target;
|
|
|
|
|
|
|
|
SmallVector<PseudoExpansion, 64> Expansions;
|
|
|
|
|
|
|
|
unsigned addDagOperandMapping(Record *Rec, DagInit *Dag,
|
|
|
|
CodeGenInstruction &Insn,
|
|
|
|
IndexedMap<OpData> &OperandMap,
|
|
|
|
unsigned BaseIdx);
|
|
|
|
void evaluateExpansion(Record *Pseudo);
|
|
|
|
void emitLoweringEmitter(raw_ostream &o);
|
|
|
|
public:
|
|
|
|
PseudoLoweringEmitter(RecordKeeper &R) : Records(R), Target(R) {}
|
|
|
|
|
|
|
|
/// run - Output the pseudo-lowerings.
|
|
|
|
void run(raw_ostream &o);
|
|
|
|
};
|
|
|
|
} // End anonymous namespace
|
|
|
|
|
2011-07-09 01:36:35 +08:00
|
|
|
// FIXME: This pass currently can only expand a pseudo to a single instruction.
|
|
|
|
// The pseudo expansion really should take a list of dags, not just
|
|
|
|
// a single dag, so we can do fancier things.
|
|
|
|
|
|
|
|
unsigned PseudoLoweringEmitter::
|
2011-07-30 06:43:06 +08:00
|
|
|
addDagOperandMapping(Record *Rec, DagInit *Dag, CodeGenInstruction &Insn,
|
2011-07-09 01:36:35 +08:00
|
|
|
IndexedMap<OpData> &OperandMap, unsigned BaseIdx) {
|
|
|
|
unsigned OpsAdded = 0;
|
|
|
|
for (unsigned i = 0, e = Dag->getNumArgs(); i != e; ++i) {
|
2012-10-11 04:24:43 +08:00
|
|
|
if (DefInit *DI = dyn_cast<DefInit>(Dag->getArg(i))) {
|
2011-07-09 01:36:35 +08:00
|
|
|
// Physical register reference. Explicit check for the special case
|
|
|
|
// "zero_reg" definition.
|
|
|
|
if (DI->getDef()->isSubClassOf("Register") ||
|
|
|
|
DI->getDef()->getName() == "zero_reg") {
|
|
|
|
OperandMap[BaseIdx + i].Kind = OpData::Reg;
|
|
|
|
OperandMap[BaseIdx + i].Data.Reg = DI->getDef();
|
|
|
|
++OpsAdded;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Normal operands should always have the same type, or we have a
|
|
|
|
// problem.
|
|
|
|
// FIXME: We probably shouldn't ever get a non-zero BaseIdx here.
|
|
|
|
assert(BaseIdx == 0 && "Named subargument in pseudo expansion?!");
|
2020-09-26 01:23:07 +08:00
|
|
|
// FIXME: Are the message operand types backward?
|
|
|
|
if (DI->getDef() != Insn.Operands[BaseIdx + i].Rec) {
|
|
|
|
PrintError(Rec, "In pseudo instruction '" + Rec->getName() +
|
|
|
|
"', operand type '" + DI->getDef()->getName() +
|
|
|
|
"' does not match expansion operand type '" +
|
|
|
|
Insn.Operands[BaseIdx + i].Rec->getName() + "'");
|
|
|
|
PrintFatalNote(DI->getDef(),
|
|
|
|
"Value was assigned at the following location:");
|
|
|
|
}
|
2011-07-09 01:36:35 +08:00
|
|
|
// Source operand maps to destination operand. The Data element
|
|
|
|
// will be filled in later, just set the Kind for now. Do it
|
|
|
|
// for each corresponding MachineInstr operand, not just the first.
|
|
|
|
for (unsigned I = 0, E = Insn.Operands[i].MINumOperands; I != E; ++I)
|
|
|
|
OperandMap[BaseIdx + i + I].Kind = OpData::Operand;
|
|
|
|
OpsAdded += Insn.Operands[i].MINumOperands;
|
2012-10-11 04:24:43 +08:00
|
|
|
} else if (IntInit *II = dyn_cast<IntInit>(Dag->getArg(i))) {
|
2011-07-09 01:36:35 +08:00
|
|
|
OperandMap[BaseIdx + i].Kind = OpData::Imm;
|
|
|
|
OperandMap[BaseIdx + i].Data.Imm = II->getValue();
|
|
|
|
++OpsAdded;
|
2021-03-20 14:06:46 +08:00
|
|
|
} else if (auto *BI = dyn_cast<BitsInit>(Dag->getArg(i))) {
|
2021-03-23 13:46:34 +08:00
|
|
|
auto *II = cast<IntInit>(BI->convertInitializerTo(IntRecTy::get()));
|
2021-03-20 14:06:46 +08:00
|
|
|
OperandMap[BaseIdx + i].Kind = OpData::Imm;
|
|
|
|
OperandMap[BaseIdx + i].Data.Imm = II->getValue();
|
|
|
|
++OpsAdded;
|
2012-10-11 04:24:43 +08:00
|
|
|
} else if (DagInit *SubDag = dyn_cast<DagInit>(Dag->getArg(i))) {
|
2011-07-09 01:36:35 +08:00
|
|
|
// Just add the operands recursively. This is almost certainly
|
|
|
|
// a constant value for a complex operand (> 1 MI operand).
|
|
|
|
unsigned NewOps =
|
|
|
|
addDagOperandMapping(Rec, SubDag, Insn, OperandMap, BaseIdx + i);
|
|
|
|
OpsAdded += NewOps;
|
|
|
|
// Since we added more than one, we also need to adjust the base.
|
|
|
|
BaseIdx += NewOps - 1;
|
|
|
|
} else
|
2012-02-05 15:21:30 +08:00
|
|
|
llvm_unreachable("Unhandled pseudo-expansion argument type!");
|
2011-07-09 01:36:35 +08:00
|
|
|
}
|
|
|
|
return OpsAdded;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PseudoLoweringEmitter::evaluateExpansion(Record *Rec) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Pseudo definition: " << Rec->getName() << "\n");
|
2011-07-09 01:36:35 +08:00
|
|
|
|
|
|
|
// Validate that the result pattern has the corrent number and types
|
|
|
|
// of arguments for the instruction it references.
|
2011-07-30 06:43:06 +08:00
|
|
|
DagInit *Dag = Rec->getValueAsDag("ResultInst");
|
2011-07-09 01:36:35 +08:00
|
|
|
assert(Dag && "Missing result instruction in pseudo expansion!");
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " Result: " << *Dag << "\n");
|
2011-07-09 01:36:35 +08:00
|
|
|
|
2012-10-11 04:24:43 +08:00
|
|
|
DefInit *OpDef = dyn_cast<DefInit>(Dag->getOperator());
|
2020-09-26 01:23:07 +08:00
|
|
|
if (!OpDef) {
|
|
|
|
PrintError(Rec, "In pseudo instruction '" + Rec->getName() +
|
|
|
|
"', result operator is not a record");
|
|
|
|
PrintFatalNote(Rec->getValue("ResultInst"),
|
|
|
|
"Result was assigned at the following location:");
|
|
|
|
}
|
2011-07-09 01:36:35 +08:00
|
|
|
Record *Operator = OpDef->getDef();
|
2020-09-26 01:23:07 +08:00
|
|
|
if (!Operator->isSubClassOf("Instruction")) {
|
|
|
|
PrintError(Rec, "In pseudo instruction '" + Rec->getName() +
|
|
|
|
"', result operator '" + Operator->getName() +
|
|
|
|
"' is not an instruction");
|
|
|
|
PrintFatalNote(Rec->getValue("ResultInst"),
|
|
|
|
"Result was assigned at the following location:");
|
|
|
|
}
|
2011-07-09 01:36:35 +08:00
|
|
|
|
|
|
|
CodeGenInstruction Insn(Operator);
|
|
|
|
|
2020-09-26 01:23:07 +08:00
|
|
|
if (Insn.isCodeGenOnly || Insn.isPseudo) {
|
|
|
|
PrintError(Rec, "In pseudo instruction '" + Rec->getName() +
|
|
|
|
"', result operator '" + Operator->getName() +
|
|
|
|
"' cannot be a pseudo instruction");
|
|
|
|
PrintFatalNote(Rec->getValue("ResultInst"),
|
|
|
|
"Result was assigned at the following location:");
|
|
|
|
}
|
2011-07-09 01:36:35 +08:00
|
|
|
|
2020-09-26 01:23:07 +08:00
|
|
|
if (Insn.Operands.size() != Dag->getNumArgs()) {
|
|
|
|
PrintError(Rec, "In pseudo instruction '" + Rec->getName() +
|
|
|
|
"', result operator '" + Operator->getName() +
|
|
|
|
"' has the wrong number of operands");
|
|
|
|
PrintFatalNote(Rec->getValue("ResultInst"),
|
|
|
|
"Result was assigned at the following location:");
|
|
|
|
}
|
2011-07-09 01:36:35 +08:00
|
|
|
|
2012-03-21 05:07:51 +08:00
|
|
|
unsigned NumMIOperands = 0;
|
|
|
|
for (unsigned i = 0, e = Insn.Operands.size(); i != e; ++i)
|
|
|
|
NumMIOperands += Insn.Operands[i].MINumOperands;
|
2011-07-09 01:36:35 +08:00
|
|
|
IndexedMap<OpData> OperandMap;
|
2012-03-21 05:07:51 +08:00
|
|
|
OperandMap.grow(NumMIOperands);
|
2011-07-09 01:36:35 +08:00
|
|
|
|
|
|
|
addDagOperandMapping(Rec, Dag, Insn, OperandMap, 0);
|
|
|
|
|
|
|
|
// If there are more operands that weren't in the DAG, they have to
|
|
|
|
// be operands that have default values, or we have an error. Currently,
|
2014-05-20 05:18:47 +08:00
|
|
|
// Operands that are a subclass of OperandWithDefaultOp have default values.
|
2011-07-09 01:36:35 +08:00
|
|
|
|
|
|
|
// Validate that each result pattern argument has a matching (by name)
|
|
|
|
// argument in the source instruction, in either the (outs) or (ins) list.
|
|
|
|
// Also check that the type of the arguments match.
|
|
|
|
//
|
|
|
|
// Record the mapping of the source to result arguments for use by
|
|
|
|
// the lowering emitter.
|
|
|
|
CodeGenInstruction SourceInsn(Rec);
|
|
|
|
StringMap<unsigned> SourceOperands;
|
|
|
|
for (unsigned i = 0, e = SourceInsn.Operands.size(); i != e; ++i)
|
|
|
|
SourceOperands[SourceInsn.Operands[i].Name] = i;
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " Operand mapping:\n");
|
2011-07-09 01:36:35 +08:00
|
|
|
for (unsigned i = 0, e = Insn.Operands.size(); i != e; ++i) {
|
|
|
|
// We've already handled constant values. Just map instruction operands
|
|
|
|
// here.
|
|
|
|
if (OperandMap[Insn.Operands[i].MIOperandNo].Kind != OpData::Operand)
|
|
|
|
continue;
|
|
|
|
StringMap<unsigned>::iterator SourceOp =
|
2016-12-05 14:00:46 +08:00
|
|
|
SourceOperands.find(Dag->getArgNameStr(i));
|
2020-09-26 01:23:07 +08:00
|
|
|
if (SourceOp == SourceOperands.end()) {
|
|
|
|
PrintError(Rec, "In pseudo instruction '" + Rec->getName() +
|
|
|
|
"', output operand '" + Dag->getArgNameStr(i) +
|
|
|
|
"' has no matching source operand");
|
|
|
|
PrintFatalNote(Rec->getValue("ResultInst"),
|
|
|
|
"Value was assigned at the following location:");
|
|
|
|
}
|
2011-07-09 01:36:35 +08:00
|
|
|
// Map the source operand to the destination operand index for each
|
|
|
|
// MachineInstr operand.
|
|
|
|
for (unsigned I = 0, E = Insn.Operands[i].MINumOperands; I != E; ++I)
|
|
|
|
OperandMap[Insn.Operands[i].MIOperandNo + I].Data.Operand =
|
|
|
|
SourceOp->getValue();
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " " << SourceOp->getValue() << " ==> " << i
|
|
|
|
<< "\n");
|
2011-07-09 01:36:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Expansions.push_back(PseudoExpansion(SourceInsn, Insn, OperandMap));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PseudoLoweringEmitter::emitLoweringEmitter(raw_ostream &o) {
|
|
|
|
// Emit file header.
|
2012-06-11 23:37:55 +08:00
|
|
|
emitSourceFileHeader("Pseudo-instruction MC lowering Source Fragment", o);
|
2011-07-09 01:36:35 +08:00
|
|
|
|
|
|
|
o << "bool " << Target.getName() + "AsmPrinter" << "::\n"
|
|
|
|
<< "emitPseudoExpansionLowering(MCStreamer &OutStreamer,\n"
|
2014-05-23 23:33:39 +08:00
|
|
|
<< " const MachineInstr *MI) {\n";
|
2011-07-09 01:36:35 +08:00
|
|
|
|
2014-05-23 23:33:39 +08:00
|
|
|
if (!Expansions.empty()) {
|
|
|
|
o << " switch (MI->getOpcode()) {\n"
|
2020-11-06 22:19:59 +08:00
|
|
|
<< " default: return false;\n";
|
2014-05-23 23:33:39 +08:00
|
|
|
for (auto &Expansion : Expansions) {
|
|
|
|
CodeGenInstruction &Source = Expansion.Source;
|
|
|
|
CodeGenInstruction &Dest = Expansion.Dest;
|
2020-11-06 22:19:59 +08:00
|
|
|
o << " case " << Source.Namespace << "::"
|
2014-05-23 23:33:39 +08:00
|
|
|
<< Source.TheDef->getName() << ": {\n"
|
2020-11-06 22:19:59 +08:00
|
|
|
<< " MCInst TmpInst;\n"
|
|
|
|
<< " MCOperand MCOp;\n"
|
|
|
|
<< " TmpInst.setOpcode(" << Dest.Namespace << "::"
|
2014-05-23 23:33:39 +08:00
|
|
|
<< Dest.TheDef->getName() << ");\n";
|
|
|
|
|
|
|
|
// Copy the operands from the source instruction.
|
|
|
|
// FIXME: Instruction operands with defaults values (predicates and cc_out
|
|
|
|
// in ARM, for example shouldn't need explicit values in the
|
|
|
|
// expansion DAG.
|
|
|
|
unsigned MIOpNo = 0;
|
|
|
|
for (const auto &DestOperand : Dest.Operands) {
|
2020-11-06 22:19:59 +08:00
|
|
|
o << " // Operand: " << DestOperand.Name << "\n";
|
2014-05-23 23:33:39 +08:00
|
|
|
for (unsigned i = 0, e = DestOperand.MINumOperands; i != e; ++i) {
|
|
|
|
switch (Expansion.OperandMap[MIOpNo + i].Kind) {
|
|
|
|
case OpData::Operand:
|
2020-11-06 22:19:59 +08:00
|
|
|
o << " lowerOperand(MI->getOperand("
|
2014-05-23 23:33:39 +08:00
|
|
|
<< Source.Operands[Expansion.OperandMap[MIOpNo].Data
|
|
|
|
.Operand].MIOperandNo + i
|
|
|
|
<< "), MCOp);\n"
|
2020-11-06 22:19:59 +08:00
|
|
|
<< " TmpInst.addOperand(MCOp);\n";
|
2014-05-23 23:33:39 +08:00
|
|
|
break;
|
|
|
|
case OpData::Imm:
|
2020-11-06 22:19:59 +08:00
|
|
|
o << " TmpInst.addOperand(MCOperand::createImm("
|
2014-05-23 23:33:39 +08:00
|
|
|
<< Expansion.OperandMap[MIOpNo + i].Data.Imm << "));\n";
|
|
|
|
break;
|
|
|
|
case OpData::Reg: {
|
|
|
|
Record *Reg = Expansion.OperandMap[MIOpNo + i].Data.Reg;
|
2020-11-06 22:19:59 +08:00
|
|
|
o << " TmpInst.addOperand(MCOperand::createReg(";
|
2014-05-23 23:33:39 +08:00
|
|
|
// "zero_reg" is special.
|
|
|
|
if (Reg->getName() == "zero_reg")
|
|
|
|
o << "0";
|
|
|
|
else
|
|
|
|
o << Reg->getValueAsString("Namespace") << "::"
|
|
|
|
<< Reg->getName();
|
|
|
|
o << "));\n";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-07-09 01:36:35 +08:00
|
|
|
}
|
2014-05-23 23:33:39 +08:00
|
|
|
MIOpNo += DestOperand.MINumOperands;
|
2011-07-09 01:36:35 +08:00
|
|
|
}
|
2014-05-23 23:33:39 +08:00
|
|
|
if (Dest.Operands.isVariadic) {
|
|
|
|
MIOpNo = Source.Operands.size() + 1;
|
2020-11-06 22:19:59 +08:00
|
|
|
o << " // variable_ops\n";
|
|
|
|
o << " for (unsigned i = " << MIOpNo
|
2014-05-23 23:33:39 +08:00
|
|
|
<< ", e = MI->getNumOperands(); i != e; ++i)\n"
|
2020-11-06 22:19:59 +08:00
|
|
|
<< " if (lowerOperand(MI->getOperand(i), MCOp))\n"
|
|
|
|
<< " TmpInst.addOperand(MCOp);\n";
|
2014-05-23 23:33:39 +08:00
|
|
|
}
|
2020-11-06 22:19:59 +08:00
|
|
|
o << " EmitToStreamer(OutStreamer, TmpInst);\n"
|
|
|
|
<< " break;\n"
|
|
|
|
<< " }\n";
|
2011-07-09 01:36:35 +08:00
|
|
|
}
|
2014-05-23 23:33:39 +08:00
|
|
|
o << " }\n return true;";
|
|
|
|
} else
|
|
|
|
o << " return false;";
|
|
|
|
|
|
|
|
o << "\n}\n\n";
|
2011-07-09 01:36:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PseudoLoweringEmitter::run(raw_ostream &o) {
|
2020-10-05 02:48:44 +08:00
|
|
|
StringRef Classes[] = {"PseudoInstExpansion", "Instruction"};
|
|
|
|
std::vector<Record *> Insts =
|
|
|
|
Records.getAllDerivedDefinitions(makeArrayRef(Classes));
|
2011-07-09 01:36:35 +08:00
|
|
|
|
|
|
|
// Process the pseudo expansion definitions, validating them as we do so.
|
2020-10-22 14:58:49 +08:00
|
|
|
Records.startTimer("Process definitions");
|
2011-07-09 01:36:35 +08:00
|
|
|
for (unsigned i = 0, e = Insts.size(); i != e; ++i)
|
|
|
|
evaluateExpansion(Insts[i]);
|
|
|
|
|
|
|
|
// Generate expansion code to lower the pseudo to an MCInst of the real
|
|
|
|
// instruction.
|
2020-10-22 14:58:49 +08:00
|
|
|
Records.startTimer("Emit expansion code");
|
2011-07-09 01:36:35 +08:00
|
|
|
emitLoweringEmitter(o);
|
|
|
|
}
|
|
|
|
|
2012-06-11 23:37:55 +08:00
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
void EmitPseudoLowering(RecordKeeper &RK, raw_ostream &OS) {
|
|
|
|
PseudoLoweringEmitter(RK).run(OS);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // End llvm namespace
|