10b57cec5SDimitry Andric //===- CodeEmitterGen.cpp - Code Emitter Generator ------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // CodeEmitterGen uses the descriptions of instructions and their fields to
100b57cec5SDimitry Andric // construct an automated code emitter: a function that, given a MachineInstr,
110b57cec5SDimitry Andric // returns the (currently, 32-bit unsigned) value of the instruction.
120b57cec5SDimitry Andric //
130b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
140b57cec5SDimitry Andric
150b57cec5SDimitry Andric #include "CodeGenInstruction.h"
160b57cec5SDimitry Andric #include "CodeGenTarget.h"
170b57cec5SDimitry Andric #include "SubtargetFeatureInfo.h"
180b57cec5SDimitry Andric #include "Types.h"
198bcb0991SDimitry Andric #include "llvm/ADT/APInt.h"
200b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
210b57cec5SDimitry Andric #include "llvm/ADT/StringExtras.h"
220b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
230b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
240b57cec5SDimitry Andric #include "llvm/TableGen/Record.h"
250b57cec5SDimitry Andric #include "llvm/TableGen/TableGenBackend.h"
260b57cec5SDimitry Andric #include <cassert>
270b57cec5SDimitry Andric #include <cstdint>
280b57cec5SDimitry Andric #include <map>
290b57cec5SDimitry Andric #include <set>
300b57cec5SDimitry Andric #include <string>
310b57cec5SDimitry Andric #include <utility>
320b57cec5SDimitry Andric #include <vector>
330b57cec5SDimitry Andric
340b57cec5SDimitry Andric using namespace llvm;
350b57cec5SDimitry Andric
360b57cec5SDimitry Andric namespace {
370b57cec5SDimitry Andric
380b57cec5SDimitry Andric class CodeEmitterGen {
390b57cec5SDimitry Andric RecordKeeper &Records;
400b57cec5SDimitry Andric
410b57cec5SDimitry Andric public:
CodeEmitterGen(RecordKeeper & R)420b57cec5SDimitry Andric CodeEmitterGen(RecordKeeper &R) : Records(R) {}
430b57cec5SDimitry Andric
440b57cec5SDimitry Andric void run(raw_ostream &o);
450b57cec5SDimitry Andric
460b57cec5SDimitry Andric private:
470b57cec5SDimitry Andric int getVariableBit(const std::string &VarName, BitsInit *BI, int bit);
480b57cec5SDimitry Andric std::string getInstructionCase(Record *R, CodeGenTarget &Target);
498bcb0991SDimitry Andric std::string getInstructionCaseForEncoding(Record *R, Record *EncodingDef,
508bcb0991SDimitry Andric CodeGenTarget &Target);
510b57cec5SDimitry Andric void AddCodeToMergeInOperand(Record *R, BitsInit *BI,
520b57cec5SDimitry Andric const std::string &VarName,
530b57cec5SDimitry Andric unsigned &NumberedOp,
540b57cec5SDimitry Andric std::set<unsigned> &NamedOpIndices,
550b57cec5SDimitry Andric std::string &Case, CodeGenTarget &Target);
560b57cec5SDimitry Andric
578bcb0991SDimitry Andric void emitInstructionBaseValues(
588bcb0991SDimitry Andric raw_ostream &o, ArrayRef<const CodeGenInstruction *> NumberedInstructions,
598bcb0991SDimitry Andric CodeGenTarget &Target, int HwMode = -1);
608bcb0991SDimitry Andric unsigned BitWidth;
618bcb0991SDimitry Andric bool UseAPInt;
620b57cec5SDimitry Andric };
630b57cec5SDimitry Andric
640b57cec5SDimitry Andric // If the VarBitInit at position 'bit' matches the specified variable then
650b57cec5SDimitry Andric // return the variable bit position. Otherwise return -1.
getVariableBit(const std::string & VarName,BitsInit * BI,int bit)660b57cec5SDimitry Andric int CodeEmitterGen::getVariableBit(const std::string &VarName,
670b57cec5SDimitry Andric BitsInit *BI, int bit) {
680b57cec5SDimitry Andric if (VarBitInit *VBI = dyn_cast<VarBitInit>(BI->getBit(bit))) {
690b57cec5SDimitry Andric if (VarInit *VI = dyn_cast<VarInit>(VBI->getBitVar()))
700b57cec5SDimitry Andric if (VI->getName() == VarName)
710b57cec5SDimitry Andric return VBI->getBitNum();
720b57cec5SDimitry Andric } else if (VarInit *VI = dyn_cast<VarInit>(BI->getBit(bit))) {
730b57cec5SDimitry Andric if (VI->getName() == VarName)
740b57cec5SDimitry Andric return 0;
750b57cec5SDimitry Andric }
760b57cec5SDimitry Andric
770b57cec5SDimitry Andric return -1;
780b57cec5SDimitry Andric }
790b57cec5SDimitry Andric
800b57cec5SDimitry Andric void CodeEmitterGen::
AddCodeToMergeInOperand(Record * R,BitsInit * BI,const std::string & VarName,unsigned & NumberedOp,std::set<unsigned> & NamedOpIndices,std::string & Case,CodeGenTarget & Target)810b57cec5SDimitry Andric AddCodeToMergeInOperand(Record *R, BitsInit *BI, const std::string &VarName,
820b57cec5SDimitry Andric unsigned &NumberedOp,
830b57cec5SDimitry Andric std::set<unsigned> &NamedOpIndices,
840b57cec5SDimitry Andric std::string &Case, CodeGenTarget &Target) {
850b57cec5SDimitry Andric CodeGenInstruction &CGI = Target.getInstruction(R);
860b57cec5SDimitry Andric
870b57cec5SDimitry Andric // Determine if VarName actually contributes to the Inst encoding.
880b57cec5SDimitry Andric int bit = BI->getNumBits()-1;
890b57cec5SDimitry Andric
900b57cec5SDimitry Andric // Scan for a bit that this contributed to.
910b57cec5SDimitry Andric for (; bit >= 0; ) {
920b57cec5SDimitry Andric if (getVariableBit(VarName, BI, bit) != -1)
930b57cec5SDimitry Andric break;
940b57cec5SDimitry Andric
950b57cec5SDimitry Andric --bit;
960b57cec5SDimitry Andric }
970b57cec5SDimitry Andric
980b57cec5SDimitry Andric // If we found no bits, ignore this value, otherwise emit the call to get the
990b57cec5SDimitry Andric // operand encoding.
1000b57cec5SDimitry Andric if (bit < 0) return;
1010b57cec5SDimitry Andric
1020b57cec5SDimitry Andric // If the operand matches by name, reference according to that
1030b57cec5SDimitry Andric // operand number. Non-matching operands are assumed to be in
1040b57cec5SDimitry Andric // order.
1050b57cec5SDimitry Andric unsigned OpIdx;
1060b57cec5SDimitry Andric if (CGI.Operands.hasOperandNamed(VarName, OpIdx)) {
1070b57cec5SDimitry Andric // Get the machine operand number for the indicated operand.
1080b57cec5SDimitry Andric OpIdx = CGI.Operands[OpIdx].MIOperandNo;
1090b57cec5SDimitry Andric assert(!CGI.Operands.isFlatOperandNotEmitted(OpIdx) &&
1100b57cec5SDimitry Andric "Explicitly used operand also marked as not emitted!");
1110b57cec5SDimitry Andric } else {
1120b57cec5SDimitry Andric unsigned NumberOps = CGI.Operands.size();
1130b57cec5SDimitry Andric /// If this operand is not supposed to be emitted by the
1140b57cec5SDimitry Andric /// generated emitter, skip it.
1150b57cec5SDimitry Andric while (NumberedOp < NumberOps &&
1160b57cec5SDimitry Andric (CGI.Operands.isFlatOperandNotEmitted(NumberedOp) ||
1170b57cec5SDimitry Andric (!NamedOpIndices.empty() && NamedOpIndices.count(
1180b57cec5SDimitry Andric CGI.Operands.getSubOperandNumber(NumberedOp).first)))) {
1190b57cec5SDimitry Andric ++NumberedOp;
1200b57cec5SDimitry Andric
1210b57cec5SDimitry Andric if (NumberedOp >= CGI.Operands.back().MIOperandNo +
1220b57cec5SDimitry Andric CGI.Operands.back().MINumOperands) {
1230b57cec5SDimitry Andric errs() << "Too few operands in record " << R->getName() <<
1240b57cec5SDimitry Andric " (no match for variable " << VarName << "):\n";
1250b57cec5SDimitry Andric errs() << *R;
1260b57cec5SDimitry Andric errs() << '\n';
1270b57cec5SDimitry Andric
1280b57cec5SDimitry Andric return;
1290b57cec5SDimitry Andric }
1300b57cec5SDimitry Andric }
1310b57cec5SDimitry Andric
1320b57cec5SDimitry Andric OpIdx = NumberedOp++;
1330b57cec5SDimitry Andric }
1340b57cec5SDimitry Andric
1350b57cec5SDimitry Andric std::pair<unsigned, unsigned> SO = CGI.Operands.getSubOperandNumber(OpIdx);
1360b57cec5SDimitry Andric std::string &EncoderMethodName = CGI.Operands[SO.first].EncoderMethodName;
1370b57cec5SDimitry Andric
1388bcb0991SDimitry Andric if (UseAPInt)
1398bcb0991SDimitry Andric Case += " op.clearAllBits();\n";
1408bcb0991SDimitry Andric
1410b57cec5SDimitry Andric // If the source operand has a custom encoder, use it. This will
1420b57cec5SDimitry Andric // get the encoding for all of the suboperands.
1430b57cec5SDimitry Andric if (!EncoderMethodName.empty()) {
1440b57cec5SDimitry Andric // A custom encoder has all of the information for the
1450b57cec5SDimitry Andric // sub-operands, if there are more than one, so only
1460b57cec5SDimitry Andric // query the encoder once per source operand.
1470b57cec5SDimitry Andric if (SO.second == 0) {
1488bcb0991SDimitry Andric Case += " // op: " + VarName + "\n";
1498bcb0991SDimitry Andric if (UseAPInt) {
1508bcb0991SDimitry Andric Case += " " + EncoderMethodName + "(MI, " + utostr(OpIdx);
1518bcb0991SDimitry Andric Case += ", op";
1528bcb0991SDimitry Andric } else {
1538bcb0991SDimitry Andric Case += " op = " + EncoderMethodName + "(MI, " + utostr(OpIdx);
1548bcb0991SDimitry Andric }
1558bcb0991SDimitry Andric Case += ", Fixups, STI);\n";
1560b57cec5SDimitry Andric }
1570b57cec5SDimitry Andric } else {
1588bcb0991SDimitry Andric Case += " // op: " + VarName + "\n";
1598bcb0991SDimitry Andric if (UseAPInt) {
1608bcb0991SDimitry Andric Case += " getMachineOpValue(MI, MI.getOperand(" + utostr(OpIdx) + ")";
1618bcb0991SDimitry Andric Case += ", op, Fixups, STI";
1628bcb0991SDimitry Andric } else {
1638bcb0991SDimitry Andric Case += " op = getMachineOpValue(MI, MI.getOperand(" + utostr(OpIdx) + ")";
1640b57cec5SDimitry Andric Case += ", Fixups, STI";
1658bcb0991SDimitry Andric }
1660b57cec5SDimitry Andric Case += ");\n";
1670b57cec5SDimitry Andric }
1680b57cec5SDimitry Andric
1698bcb0991SDimitry Andric // Precalculate the number of lits this variable contributes to in the
1708bcb0991SDimitry Andric // operand. If there is a single lit (consecutive range of bits) we can use a
1718bcb0991SDimitry Andric // destructive sequence on APInt that reduces memory allocations.
1728bcb0991SDimitry Andric int numOperandLits = 0;
1738bcb0991SDimitry Andric for (int tmpBit = bit; tmpBit >= 0;) {
1748bcb0991SDimitry Andric int varBit = getVariableBit(VarName, BI, tmpBit);
1758bcb0991SDimitry Andric
1768bcb0991SDimitry Andric // If this bit isn't from a variable, skip it.
1778bcb0991SDimitry Andric if (varBit == -1) {
1788bcb0991SDimitry Andric --tmpBit;
1798bcb0991SDimitry Andric continue;
1808bcb0991SDimitry Andric }
1818bcb0991SDimitry Andric
1828bcb0991SDimitry Andric // Figure out the consecutive range of bits covered by this operand, in
1838bcb0991SDimitry Andric // order to generate better encoding code.
1848bcb0991SDimitry Andric int beginVarBit = varBit;
1858bcb0991SDimitry Andric int N = 1;
1868bcb0991SDimitry Andric for (--tmpBit; tmpBit >= 0;) {
1878bcb0991SDimitry Andric varBit = getVariableBit(VarName, BI, tmpBit);
1888bcb0991SDimitry Andric if (varBit == -1 || varBit != (beginVarBit - N))
1898bcb0991SDimitry Andric break;
1908bcb0991SDimitry Andric ++N;
1918bcb0991SDimitry Andric --tmpBit;
1928bcb0991SDimitry Andric }
1938bcb0991SDimitry Andric ++numOperandLits;
1948bcb0991SDimitry Andric }
1958bcb0991SDimitry Andric
1960b57cec5SDimitry Andric for (; bit >= 0; ) {
1970b57cec5SDimitry Andric int varBit = getVariableBit(VarName, BI, bit);
1980b57cec5SDimitry Andric
1990b57cec5SDimitry Andric // If this bit isn't from a variable, skip it.
2000b57cec5SDimitry Andric if (varBit == -1) {
2010b57cec5SDimitry Andric --bit;
2020b57cec5SDimitry Andric continue;
2030b57cec5SDimitry Andric }
2040b57cec5SDimitry Andric
2050b57cec5SDimitry Andric // Figure out the consecutive range of bits covered by this operand, in
2060b57cec5SDimitry Andric // order to generate better encoding code.
2070b57cec5SDimitry Andric int beginInstBit = bit;
2080b57cec5SDimitry Andric int beginVarBit = varBit;
2090b57cec5SDimitry Andric int N = 1;
2100b57cec5SDimitry Andric for (--bit; bit >= 0;) {
2110b57cec5SDimitry Andric varBit = getVariableBit(VarName, BI, bit);
2120b57cec5SDimitry Andric if (varBit == -1 || varBit != (beginVarBit - N)) break;
2130b57cec5SDimitry Andric ++N;
2140b57cec5SDimitry Andric --bit;
2150b57cec5SDimitry Andric }
2160b57cec5SDimitry Andric
2178bcb0991SDimitry Andric std::string maskStr;
2188bcb0991SDimitry Andric int opShift;
2198bcb0991SDimitry Andric
2208bcb0991SDimitry Andric unsigned loBit = beginVarBit - N + 1;
2218bcb0991SDimitry Andric unsigned hiBit = loBit + N;
2228bcb0991SDimitry Andric unsigned loInstBit = beginInstBit - N + 1;
2238bcb0991SDimitry Andric if (UseAPInt) {
2248bcb0991SDimitry Andric std::string extractStr;
2258bcb0991SDimitry Andric if (N >= 64) {
2268bcb0991SDimitry Andric extractStr = "op.extractBits(" + itostr(hiBit - loBit) + ", " +
2278bcb0991SDimitry Andric itostr(loBit) + ")";
2288bcb0991SDimitry Andric Case += " Value.insertBits(" + extractStr + ", " +
2298bcb0991SDimitry Andric itostr(loInstBit) + ");\n";
2308bcb0991SDimitry Andric } else {
2318bcb0991SDimitry Andric extractStr = "op.extractBitsAsZExtValue(" + itostr(hiBit - loBit) +
2328bcb0991SDimitry Andric ", " + itostr(loBit) + ")";
2338bcb0991SDimitry Andric Case += " Value.insertBits(" + extractStr + ", " +
2348bcb0991SDimitry Andric itostr(loInstBit) + ", " + itostr(hiBit - loBit) + ");\n";
2358bcb0991SDimitry Andric }
2368bcb0991SDimitry Andric } else {
2370b57cec5SDimitry Andric uint64_t opMask = ~(uint64_t)0 >> (64 - N);
2388bcb0991SDimitry Andric opShift = beginVarBit - N + 1;
2390b57cec5SDimitry Andric opMask <<= opShift;
2408bcb0991SDimitry Andric maskStr = "UINT64_C(" + utostr(opMask) + ")";
2410b57cec5SDimitry Andric opShift = beginInstBit - beginVarBit;
2420b57cec5SDimitry Andric
2438bcb0991SDimitry Andric if (numOperandLits == 1) {
2448bcb0991SDimitry Andric Case += " op &= " + maskStr + ";\n";
2450b57cec5SDimitry Andric if (opShift > 0) {
2468bcb0991SDimitry Andric Case += " op <<= " + itostr(opShift) + ";\n";
2478bcb0991SDimitry Andric } else if (opShift < 0) {
2488bcb0991SDimitry Andric Case += " op >>= " + itostr(-opShift) + ";\n";
2498bcb0991SDimitry Andric }
2508bcb0991SDimitry Andric Case += " Value |= op;\n";
2518bcb0991SDimitry Andric } else {
2528bcb0991SDimitry Andric if (opShift > 0) {
2538bcb0991SDimitry Andric Case += " Value |= (op & " + maskStr + ") << " +
2540b57cec5SDimitry Andric itostr(opShift) + ";\n";
2550b57cec5SDimitry Andric } else if (opShift < 0) {
2568bcb0991SDimitry Andric Case += " Value |= (op & " + maskStr + ") >> " +
2570b57cec5SDimitry Andric itostr(-opShift) + ";\n";
2580b57cec5SDimitry Andric } else {
2598bcb0991SDimitry Andric Case += " Value |= (op & " + maskStr + ");\n";
2608bcb0991SDimitry Andric }
2618bcb0991SDimitry Andric }
2620b57cec5SDimitry Andric }
2630b57cec5SDimitry Andric }
2640b57cec5SDimitry Andric }
2650b57cec5SDimitry Andric
getInstructionCase(Record * R,CodeGenTarget & Target)2660b57cec5SDimitry Andric std::string CodeEmitterGen::getInstructionCase(Record *R,
2670b57cec5SDimitry Andric CodeGenTarget &Target) {
2680b57cec5SDimitry Andric std::string Case;
2698bcb0991SDimitry Andric if (const RecordVal *RV = R->getValue("EncodingInfos")) {
2708bcb0991SDimitry Andric if (auto *DI = dyn_cast_or_null<DefInit>(RV->getValue())) {
2718bcb0991SDimitry Andric const CodeGenHwModes &HWM = Target.getHwModes();
2728bcb0991SDimitry Andric EncodingInfoByHwMode EBM(DI->getDef(), HWM);
2738bcb0991SDimitry Andric Case += " switch (HwMode) {\n";
2748bcb0991SDimitry Andric Case += " default: llvm_unreachable(\"Unhandled HwMode\");\n";
275*5f7ddb14SDimitry Andric for (auto &KV : EBM) {
2768bcb0991SDimitry Andric Case += " case " + itostr(KV.first) + ": {\n";
2778bcb0991SDimitry Andric Case += getInstructionCaseForEncoding(R, KV.second, Target);
2788bcb0991SDimitry Andric Case += " break;\n";
2798bcb0991SDimitry Andric Case += " }\n";
2808bcb0991SDimitry Andric }
2818bcb0991SDimitry Andric Case += " }\n";
2828bcb0991SDimitry Andric return Case;
2838bcb0991SDimitry Andric }
2848bcb0991SDimitry Andric }
2858bcb0991SDimitry Andric return getInstructionCaseForEncoding(R, R, Target);
2868bcb0991SDimitry Andric }
2878bcb0991SDimitry Andric
getInstructionCaseForEncoding(Record * R,Record * EncodingDef,CodeGenTarget & Target)2888bcb0991SDimitry Andric std::string CodeEmitterGen::getInstructionCaseForEncoding(Record *R, Record *EncodingDef,
2898bcb0991SDimitry Andric CodeGenTarget &Target) {
2908bcb0991SDimitry Andric std::string Case;
2918bcb0991SDimitry Andric BitsInit *BI = EncodingDef->getValueAsBitsInit("Inst");
2920b57cec5SDimitry Andric unsigned NumberedOp = 0;
2930b57cec5SDimitry Andric std::set<unsigned> NamedOpIndices;
2940b57cec5SDimitry Andric
2950b57cec5SDimitry Andric // Collect the set of operand indices that might correspond to named
2960b57cec5SDimitry Andric // operand, and skip these when assigning operands based on position.
2970b57cec5SDimitry Andric if (Target.getInstructionSet()->
2980b57cec5SDimitry Andric getValueAsBit("noNamedPositionallyEncodedOperands")) {
2990b57cec5SDimitry Andric CodeGenInstruction &CGI = Target.getInstruction(R);
3000b57cec5SDimitry Andric for (const RecordVal &RV : R->getValues()) {
3010b57cec5SDimitry Andric unsigned OpIdx;
3020b57cec5SDimitry Andric if (!CGI.Operands.hasOperandNamed(RV.getName(), OpIdx))
3030b57cec5SDimitry Andric continue;
3040b57cec5SDimitry Andric
3050b57cec5SDimitry Andric NamedOpIndices.insert(OpIdx);
3060b57cec5SDimitry Andric }
3070b57cec5SDimitry Andric }
3080b57cec5SDimitry Andric
3090b57cec5SDimitry Andric // Loop over all of the fields in the instruction, determining which are the
3100b57cec5SDimitry Andric // operands to the instruction.
3118bcb0991SDimitry Andric for (const RecordVal &RV : EncodingDef->getValues()) {
3120b57cec5SDimitry Andric // Ignore fixed fields in the record, we're looking for values like:
3130b57cec5SDimitry Andric // bits<5> RST = { ?, ?, ?, ?, ? };
314af732203SDimitry Andric if (RV.isNonconcreteOK() || RV.getValue()->isComplete())
3150b57cec5SDimitry Andric continue;
3160b57cec5SDimitry Andric
3175ffd83dbSDimitry Andric AddCodeToMergeInOperand(R, BI, std::string(RV.getName()), NumberedOp,
3180b57cec5SDimitry Andric NamedOpIndices, Case, Target);
3190b57cec5SDimitry Andric }
3200b57cec5SDimitry Andric
3210b57cec5SDimitry Andric StringRef PostEmitter = R->getValueAsString("PostEncoderMethod");
3220b57cec5SDimitry Andric if (!PostEmitter.empty()) {
3230b57cec5SDimitry Andric Case += " Value = ";
3240b57cec5SDimitry Andric Case += PostEmitter;
3250b57cec5SDimitry Andric Case += "(MI, Value";
3260b57cec5SDimitry Andric Case += ", STI";
3270b57cec5SDimitry Andric Case += ");\n";
3280b57cec5SDimitry Andric }
3290b57cec5SDimitry Andric
3300b57cec5SDimitry Andric return Case;
3310b57cec5SDimitry Andric }
3320b57cec5SDimitry Andric
3330b57cec5SDimitry Andric static std::string
getNameForFeatureBitset(const std::vector<Record * > & FeatureBitset)3340b57cec5SDimitry Andric getNameForFeatureBitset(const std::vector<Record *> &FeatureBitset) {
3350b57cec5SDimitry Andric std::string Name = "CEFBS";
3360b57cec5SDimitry Andric for (const auto &Feature : FeatureBitset)
3370b57cec5SDimitry Andric Name += ("_" + Feature->getName()).str();
3380b57cec5SDimitry Andric return Name;
3390b57cec5SDimitry Andric }
3400b57cec5SDimitry Andric
emitInstBits(raw_ostream & OS,const APInt & Bits)3418bcb0991SDimitry Andric static void emitInstBits(raw_ostream &OS, const APInt &Bits) {
3428bcb0991SDimitry Andric for (unsigned I = 0; I < Bits.getNumWords(); ++I)
3438bcb0991SDimitry Andric OS << ((I > 0) ? ", " : "") << "UINT64_C(" << utostr(Bits.getRawData()[I])
3448bcb0991SDimitry Andric << ")";
3458bcb0991SDimitry Andric }
3468bcb0991SDimitry Andric
emitInstructionBaseValues(raw_ostream & o,ArrayRef<const CodeGenInstruction * > NumberedInstructions,CodeGenTarget & Target,int HwMode)3478bcb0991SDimitry Andric void CodeEmitterGen::emitInstructionBaseValues(
3488bcb0991SDimitry Andric raw_ostream &o, ArrayRef<const CodeGenInstruction *> NumberedInstructions,
3498bcb0991SDimitry Andric CodeGenTarget &Target, int HwMode) {
3508bcb0991SDimitry Andric const CodeGenHwModes &HWM = Target.getHwModes();
3518bcb0991SDimitry Andric if (HwMode == -1)
3528bcb0991SDimitry Andric o << " static const uint64_t InstBits[] = {\n";
3538bcb0991SDimitry Andric else
3548bcb0991SDimitry Andric o << " static const uint64_t InstBits_" << HWM.getMode(HwMode).Name
3558bcb0991SDimitry Andric << "[] = {\n";
3568bcb0991SDimitry Andric
3578bcb0991SDimitry Andric for (const CodeGenInstruction *CGI : NumberedInstructions) {
3588bcb0991SDimitry Andric Record *R = CGI->TheDef;
3598bcb0991SDimitry Andric
3608bcb0991SDimitry Andric if (R->getValueAsString("Namespace") == "TargetOpcode" ||
3618bcb0991SDimitry Andric R->getValueAsBit("isPseudo")) {
3628bcb0991SDimitry Andric o << " "; emitInstBits(o, APInt(BitWidth, 0)); o << ",\n";
3638bcb0991SDimitry Andric continue;
3648bcb0991SDimitry Andric }
3658bcb0991SDimitry Andric
3668bcb0991SDimitry Andric Record *EncodingDef = R;
3678bcb0991SDimitry Andric if (const RecordVal *RV = R->getValue("EncodingInfos")) {
3688bcb0991SDimitry Andric if (auto *DI = dyn_cast_or_null<DefInit>(RV->getValue())) {
3698bcb0991SDimitry Andric EncodingInfoByHwMode EBM(DI->getDef(), HWM);
3708bcb0991SDimitry Andric if (EBM.hasMode(HwMode))
3718bcb0991SDimitry Andric EncodingDef = EBM.get(HwMode);
3728bcb0991SDimitry Andric }
3738bcb0991SDimitry Andric }
3748bcb0991SDimitry Andric BitsInit *BI = EncodingDef->getValueAsBitsInit("Inst");
3758bcb0991SDimitry Andric
3768bcb0991SDimitry Andric // Start by filling in fixed values.
3778bcb0991SDimitry Andric APInt Value(BitWidth, 0);
3788bcb0991SDimitry Andric for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) {
3798bcb0991SDimitry Andric if (BitInit *B = dyn_cast<BitInit>(BI->getBit(e - i - 1)))
3808bcb0991SDimitry Andric Value |= APInt(BitWidth, (uint64_t)B->getValue()) << (e - i - 1);
3818bcb0991SDimitry Andric }
3828bcb0991SDimitry Andric o << " ";
3838bcb0991SDimitry Andric emitInstBits(o, Value);
3848bcb0991SDimitry Andric o << "," << '\t' << "// " << R->getName() << "\n";
3858bcb0991SDimitry Andric }
3868bcb0991SDimitry Andric o << " UINT64_C(0)\n };\n";
3878bcb0991SDimitry Andric }
3888bcb0991SDimitry Andric
run(raw_ostream & o)3890b57cec5SDimitry Andric void CodeEmitterGen::run(raw_ostream &o) {
3900b57cec5SDimitry Andric CodeGenTarget Target(Records);
3910b57cec5SDimitry Andric std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
3920b57cec5SDimitry Andric
3930b57cec5SDimitry Andric // For little-endian instruction bit encodings, reverse the bit order
3940b57cec5SDimitry Andric Target.reverseBitsForLittleEndianEncoding();
3950b57cec5SDimitry Andric
3960b57cec5SDimitry Andric ArrayRef<const CodeGenInstruction*> NumberedInstructions =
3970b57cec5SDimitry Andric Target.getInstructionsByEnumValue();
3980b57cec5SDimitry Andric
3998bcb0991SDimitry Andric const CodeGenHwModes &HWM = Target.getHwModes();
4008bcb0991SDimitry Andric // The set of HwModes used by instruction encodings.
4018bcb0991SDimitry Andric std::set<unsigned> HwModes;
4028bcb0991SDimitry Andric BitWidth = 0;
4038bcb0991SDimitry Andric for (const CodeGenInstruction *CGI : NumberedInstructions) {
4048bcb0991SDimitry Andric Record *R = CGI->TheDef;
4058bcb0991SDimitry Andric if (R->getValueAsString("Namespace") == "TargetOpcode" ||
4068bcb0991SDimitry Andric R->getValueAsBit("isPseudo"))
4078bcb0991SDimitry Andric continue;
4088bcb0991SDimitry Andric
4098bcb0991SDimitry Andric if (const RecordVal *RV = R->getValue("EncodingInfos")) {
4108bcb0991SDimitry Andric if (DefInit *DI = dyn_cast_or_null<DefInit>(RV->getValue())) {
4118bcb0991SDimitry Andric EncodingInfoByHwMode EBM(DI->getDef(), HWM);
412*5f7ddb14SDimitry Andric for (auto &KV : EBM) {
4138bcb0991SDimitry Andric BitsInit *BI = KV.second->getValueAsBitsInit("Inst");
4148bcb0991SDimitry Andric BitWidth = std::max(BitWidth, BI->getNumBits());
4158bcb0991SDimitry Andric HwModes.insert(KV.first);
4168bcb0991SDimitry Andric }
4178bcb0991SDimitry Andric continue;
4188bcb0991SDimitry Andric }
4198bcb0991SDimitry Andric }
4208bcb0991SDimitry Andric BitsInit *BI = R->getValueAsBitsInit("Inst");
4218bcb0991SDimitry Andric BitWidth = std::max(BitWidth, BI->getNumBits());
4228bcb0991SDimitry Andric }
4238bcb0991SDimitry Andric UseAPInt = BitWidth > 64;
4248bcb0991SDimitry Andric
4250b57cec5SDimitry Andric // Emit function declaration
4268bcb0991SDimitry Andric if (UseAPInt) {
4278bcb0991SDimitry Andric o << "void " << Target.getName()
4288bcb0991SDimitry Andric << "MCCodeEmitter::getBinaryCodeForInstr(const MCInst &MI,\n"
4298bcb0991SDimitry Andric << " SmallVectorImpl<MCFixup> &Fixups,\n"
4308bcb0991SDimitry Andric << " APInt &Inst,\n"
4318bcb0991SDimitry Andric << " APInt &Scratch,\n"
4328bcb0991SDimitry Andric << " const MCSubtargetInfo &STI) const {\n";
4338bcb0991SDimitry Andric } else {
4340b57cec5SDimitry Andric o << "uint64_t " << Target.getName();
4350b57cec5SDimitry Andric o << "MCCodeEmitter::getBinaryCodeForInstr(const MCInst &MI,\n"
4360b57cec5SDimitry Andric << " SmallVectorImpl<MCFixup> &Fixups,\n"
4370b57cec5SDimitry Andric << " const MCSubtargetInfo &STI) const {\n";
4388bcb0991SDimitry Andric }
4390b57cec5SDimitry Andric
4400b57cec5SDimitry Andric // Emit instruction base values
4418bcb0991SDimitry Andric if (HwModes.empty()) {
4428bcb0991SDimitry Andric emitInstructionBaseValues(o, NumberedInstructions, Target, -1);
4438bcb0991SDimitry Andric } else {
4448bcb0991SDimitry Andric for (unsigned HwMode : HwModes)
4458bcb0991SDimitry Andric emitInstructionBaseValues(o, NumberedInstructions, Target, (int)HwMode);
4460b57cec5SDimitry Andric }
4470b57cec5SDimitry Andric
4488bcb0991SDimitry Andric if (!HwModes.empty()) {
4498bcb0991SDimitry Andric o << " const uint64_t *InstBits;\n";
4508bcb0991SDimitry Andric o << " unsigned HwMode = STI.getHwMode();\n";
4518bcb0991SDimitry Andric o << " switch (HwMode) {\n";
4528bcb0991SDimitry Andric o << " default: llvm_unreachable(\"Unknown hardware mode!\"); break;\n";
4538bcb0991SDimitry Andric for (unsigned I : HwModes) {
4548bcb0991SDimitry Andric o << " case " << I << ": InstBits = InstBits_" << HWM.getMode(I).Name
4558bcb0991SDimitry Andric << "; break;\n";
4560b57cec5SDimitry Andric }
4578bcb0991SDimitry Andric o << " };\n";
4580b57cec5SDimitry Andric }
4590b57cec5SDimitry Andric
4600b57cec5SDimitry Andric // Map to accumulate all the cases.
4610b57cec5SDimitry Andric std::map<std::string, std::vector<std::string>> CaseMap;
4620b57cec5SDimitry Andric
4630b57cec5SDimitry Andric // Construct all cases statement for each opcode
464*5f7ddb14SDimitry Andric for (Record *R : Insts) {
4650b57cec5SDimitry Andric if (R->getValueAsString("Namespace") == "TargetOpcode" ||
4660b57cec5SDimitry Andric R->getValueAsBit("isPseudo"))
4670b57cec5SDimitry Andric continue;
4680b57cec5SDimitry Andric std::string InstName =
4690b57cec5SDimitry Andric (R->getValueAsString("Namespace") + "::" + R->getName()).str();
4700b57cec5SDimitry Andric std::string Case = getInstructionCase(R, Target);
4710b57cec5SDimitry Andric
4720b57cec5SDimitry Andric CaseMap[Case].push_back(std::move(InstName));
4730b57cec5SDimitry Andric }
4740b57cec5SDimitry Andric
4750b57cec5SDimitry Andric // Emit initial function code
4768bcb0991SDimitry Andric if (UseAPInt) {
4778bcb0991SDimitry Andric int NumWords = APInt::getNumWords(BitWidth);
4788bcb0991SDimitry Andric int NumBytes = (BitWidth + 7) / 8;
4798bcb0991SDimitry Andric o << " const unsigned opcode = MI.getOpcode();\n"
4808bcb0991SDimitry Andric << " if (Inst.getBitWidth() != " << BitWidth << ")\n"
4818bcb0991SDimitry Andric << " Inst = Inst.zext(" << BitWidth << ");\n"
4828bcb0991SDimitry Andric << " if (Scratch.getBitWidth() != " << BitWidth << ")\n"
4838bcb0991SDimitry Andric << " Scratch = Scratch.zext(" << BitWidth << ");\n"
484*5f7ddb14SDimitry Andric << " LoadIntFromMemory(Inst, (const uint8_t *)&InstBits[opcode * "
485*5f7ddb14SDimitry Andric << NumWords << "], " << NumBytes << ");\n"
4868bcb0991SDimitry Andric << " APInt &Value = Inst;\n"
4878bcb0991SDimitry Andric << " APInt &op = Scratch;\n"
4888bcb0991SDimitry Andric << " switch (opcode) {\n";
4898bcb0991SDimitry Andric } else {
4900b57cec5SDimitry Andric o << " const unsigned opcode = MI.getOpcode();\n"
4910b57cec5SDimitry Andric << " uint64_t Value = InstBits[opcode];\n"
4920b57cec5SDimitry Andric << " uint64_t op = 0;\n"
4930b57cec5SDimitry Andric << " (void)op; // suppress warning\n"
4940b57cec5SDimitry Andric << " switch (opcode) {\n";
4958bcb0991SDimitry Andric }
4960b57cec5SDimitry Andric
4970b57cec5SDimitry Andric // Emit each case statement
4980b57cec5SDimitry Andric std::map<std::string, std::vector<std::string>>::iterator IE, EE;
4990b57cec5SDimitry Andric for (IE = CaseMap.begin(), EE = CaseMap.end(); IE != EE; ++IE) {
5000b57cec5SDimitry Andric const std::string &Case = IE->first;
5010b57cec5SDimitry Andric std::vector<std::string> &InstList = IE->second;
5020b57cec5SDimitry Andric
5030b57cec5SDimitry Andric for (int i = 0, N = InstList.size(); i < N; i++) {
5040b57cec5SDimitry Andric if (i) o << "\n";
5050b57cec5SDimitry Andric o << " case " << InstList[i] << ":";
5060b57cec5SDimitry Andric }
5070b57cec5SDimitry Andric o << " {\n";
5080b57cec5SDimitry Andric o << Case;
5090b57cec5SDimitry Andric o << " break;\n"
5100b57cec5SDimitry Andric << " }\n";
5110b57cec5SDimitry Andric }
5120b57cec5SDimitry Andric
5130b57cec5SDimitry Andric // Default case: unhandled opcode
5140b57cec5SDimitry Andric o << " default:\n"
5150b57cec5SDimitry Andric << " std::string msg;\n"
5160b57cec5SDimitry Andric << " raw_string_ostream Msg(msg);\n"
5170b57cec5SDimitry Andric << " Msg << \"Not supported instr: \" << MI;\n"
5180b57cec5SDimitry Andric << " report_fatal_error(Msg.str());\n"
5198bcb0991SDimitry Andric << " }\n";
5208bcb0991SDimitry Andric if (UseAPInt)
5218bcb0991SDimitry Andric o << " Inst = Value;\n";
5228bcb0991SDimitry Andric else
5238bcb0991SDimitry Andric o << " return Value;\n";
5248bcb0991SDimitry Andric o << "}\n\n";
5250b57cec5SDimitry Andric
5260b57cec5SDimitry Andric const auto &All = SubtargetFeatureInfo::getAll(Records);
5270b57cec5SDimitry Andric std::map<Record *, SubtargetFeatureInfo, LessRecordByID> SubtargetFeatures;
5280b57cec5SDimitry Andric SubtargetFeatures.insert(All.begin(), All.end());
5290b57cec5SDimitry Andric
5300b57cec5SDimitry Andric o << "#ifdef ENABLE_INSTR_PREDICATE_VERIFIER\n"
5310b57cec5SDimitry Andric << "#undef ENABLE_INSTR_PREDICATE_VERIFIER\n"
5320b57cec5SDimitry Andric << "#include <sstream>\n\n";
5330b57cec5SDimitry Andric
5340b57cec5SDimitry Andric // Emit the subtarget feature enumeration.
5350b57cec5SDimitry Andric SubtargetFeatureInfo::emitSubtargetFeatureBitEnumeration(SubtargetFeatures,
5360b57cec5SDimitry Andric o);
5370b57cec5SDimitry Andric
5380b57cec5SDimitry Andric // Emit the name table for error messages.
5390b57cec5SDimitry Andric o << "#ifndef NDEBUG\n";
5400b57cec5SDimitry Andric SubtargetFeatureInfo::emitNameTable(SubtargetFeatures, o);
5410b57cec5SDimitry Andric o << "#endif // NDEBUG\n";
5420b57cec5SDimitry Andric
5430b57cec5SDimitry Andric // Emit the available features compute function.
5440b57cec5SDimitry Andric SubtargetFeatureInfo::emitComputeAssemblerAvailableFeatures(
5450b57cec5SDimitry Andric Target.getName(), "MCCodeEmitter", "computeAvailableFeatures",
5460b57cec5SDimitry Andric SubtargetFeatures, o);
5470b57cec5SDimitry Andric
5480b57cec5SDimitry Andric std::vector<std::vector<Record *>> FeatureBitsets;
5490b57cec5SDimitry Andric for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
5500b57cec5SDimitry Andric FeatureBitsets.emplace_back();
5510b57cec5SDimitry Andric for (Record *Predicate : Inst->TheDef->getValueAsListOfDefs("Predicates")) {
5520b57cec5SDimitry Andric const auto &I = SubtargetFeatures.find(Predicate);
5530b57cec5SDimitry Andric if (I != SubtargetFeatures.end())
5540b57cec5SDimitry Andric FeatureBitsets.back().push_back(I->second.TheDef);
5550b57cec5SDimitry Andric }
5560b57cec5SDimitry Andric }
5570b57cec5SDimitry Andric
5580b57cec5SDimitry Andric llvm::sort(FeatureBitsets, [&](const std::vector<Record *> &A,
5590b57cec5SDimitry Andric const std::vector<Record *> &B) {
5600b57cec5SDimitry Andric if (A.size() < B.size())
5610b57cec5SDimitry Andric return true;
5620b57cec5SDimitry Andric if (A.size() > B.size())
5630b57cec5SDimitry Andric return false;
564480093f4SDimitry Andric for (auto Pair : zip(A, B)) {
5650b57cec5SDimitry Andric if (std::get<0>(Pair)->getName() < std::get<1>(Pair)->getName())
5660b57cec5SDimitry Andric return true;
5670b57cec5SDimitry Andric if (std::get<0>(Pair)->getName() > std::get<1>(Pair)->getName())
5680b57cec5SDimitry Andric return false;
5690b57cec5SDimitry Andric }
5700b57cec5SDimitry Andric return false;
5710b57cec5SDimitry Andric });
5720b57cec5SDimitry Andric FeatureBitsets.erase(
5730b57cec5SDimitry Andric std::unique(FeatureBitsets.begin(), FeatureBitsets.end()),
5740b57cec5SDimitry Andric FeatureBitsets.end());
5750b57cec5SDimitry Andric o << "#ifndef NDEBUG\n"
5760b57cec5SDimitry Andric << "// Feature bitsets.\n"
5770b57cec5SDimitry Andric << "enum : " << getMinimalTypeForRange(FeatureBitsets.size()) << " {\n"
5780b57cec5SDimitry Andric << " CEFBS_None,\n";
5790b57cec5SDimitry Andric for (const auto &FeatureBitset : FeatureBitsets) {
5800b57cec5SDimitry Andric if (FeatureBitset.empty())
5810b57cec5SDimitry Andric continue;
5820b57cec5SDimitry Andric o << " " << getNameForFeatureBitset(FeatureBitset) << ",\n";
5830b57cec5SDimitry Andric }
5840b57cec5SDimitry Andric o << "};\n\n"
5858bcb0991SDimitry Andric << "static constexpr FeatureBitset FeatureBitsets[] = {\n"
5860b57cec5SDimitry Andric << " {}, // CEFBS_None\n";
5870b57cec5SDimitry Andric for (const auto &FeatureBitset : FeatureBitsets) {
5880b57cec5SDimitry Andric if (FeatureBitset.empty())
5890b57cec5SDimitry Andric continue;
5900b57cec5SDimitry Andric o << " {";
5910b57cec5SDimitry Andric for (const auto &Feature : FeatureBitset) {
5920b57cec5SDimitry Andric const auto &I = SubtargetFeatures.find(Feature);
5930b57cec5SDimitry Andric assert(I != SubtargetFeatures.end() && "Didn't import predicate?");
5940b57cec5SDimitry Andric o << I->second.getEnumBitName() << ", ";
5950b57cec5SDimitry Andric }
5960b57cec5SDimitry Andric o << "},\n";
5970b57cec5SDimitry Andric }
5980b57cec5SDimitry Andric o << "};\n"
5990b57cec5SDimitry Andric << "#endif // NDEBUG\n\n";
6000b57cec5SDimitry Andric
6010b57cec5SDimitry Andric
6020b57cec5SDimitry Andric // Emit the predicate verifier.
6030b57cec5SDimitry Andric o << "void " << Target.getName()
6040b57cec5SDimitry Andric << "MCCodeEmitter::verifyInstructionPredicates(\n"
6050b57cec5SDimitry Andric << " const MCInst &Inst, const FeatureBitset &AvailableFeatures) const {\n"
6060b57cec5SDimitry Andric << "#ifndef NDEBUG\n"
6070b57cec5SDimitry Andric << " static " << getMinimalTypeForRange(FeatureBitsets.size())
6080b57cec5SDimitry Andric << " RequiredFeaturesRefs[] = {\n";
6090b57cec5SDimitry Andric unsigned InstIdx = 0;
6100b57cec5SDimitry Andric for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
6110b57cec5SDimitry Andric o << " CEFBS";
6120b57cec5SDimitry Andric unsigned NumPredicates = 0;
6130b57cec5SDimitry Andric for (Record *Predicate : Inst->TheDef->getValueAsListOfDefs("Predicates")) {
6140b57cec5SDimitry Andric const auto &I = SubtargetFeatures.find(Predicate);
6150b57cec5SDimitry Andric if (I != SubtargetFeatures.end()) {
6160b57cec5SDimitry Andric o << '_' << I->second.TheDef->getName();
6170b57cec5SDimitry Andric NumPredicates++;
6180b57cec5SDimitry Andric }
6190b57cec5SDimitry Andric }
6200b57cec5SDimitry Andric if (!NumPredicates)
6210b57cec5SDimitry Andric o << "_None";
6220b57cec5SDimitry Andric o << ", // " << Inst->TheDef->getName() << " = " << InstIdx << "\n";
6230b57cec5SDimitry Andric InstIdx++;
6240b57cec5SDimitry Andric }
6250b57cec5SDimitry Andric o << " };\n\n";
6260b57cec5SDimitry Andric o << " assert(Inst.getOpcode() < " << InstIdx << ");\n";
6270b57cec5SDimitry Andric o << " const FeatureBitset &RequiredFeatures = "
6280b57cec5SDimitry Andric "FeatureBitsets[RequiredFeaturesRefs[Inst.getOpcode()]];\n";
6290b57cec5SDimitry Andric o << " FeatureBitset MissingFeatures =\n"
6300b57cec5SDimitry Andric << " (AvailableFeatures & RequiredFeatures) ^\n"
6310b57cec5SDimitry Andric << " RequiredFeatures;\n"
6320b57cec5SDimitry Andric << " if (MissingFeatures.any()) {\n"
6330b57cec5SDimitry Andric << " std::ostringstream Msg;\n"
6340b57cec5SDimitry Andric << " Msg << \"Attempting to emit \" << "
6350b57cec5SDimitry Andric "MCII.getName(Inst.getOpcode()).str()\n"
6360b57cec5SDimitry Andric << " << \" instruction but the \";\n"
6370b57cec5SDimitry Andric << " for (unsigned i = 0, e = MissingFeatures.size(); i != e; ++i)\n"
6380b57cec5SDimitry Andric << " if (MissingFeatures.test(i))\n"
6390b57cec5SDimitry Andric << " Msg << SubtargetFeatureNames[i] << \" \";\n"
6400b57cec5SDimitry Andric << " Msg << \"predicate(s) are not met\";\n"
6410b57cec5SDimitry Andric << " report_fatal_error(Msg.str());\n"
6420b57cec5SDimitry Andric << " }\n"
6430b57cec5SDimitry Andric << "#else\n"
6440b57cec5SDimitry Andric << " // Silence unused variable warning on targets that don't use MCII for "
6450b57cec5SDimitry Andric "other purposes (e.g. BPF).\n"
6460b57cec5SDimitry Andric << " (void)MCII;\n"
6470b57cec5SDimitry Andric << "#endif // NDEBUG\n";
6480b57cec5SDimitry Andric o << "}\n";
6490b57cec5SDimitry Andric o << "#endif\n";
6500b57cec5SDimitry Andric }
6510b57cec5SDimitry Andric
6520b57cec5SDimitry Andric } // end anonymous namespace
6530b57cec5SDimitry Andric
6540b57cec5SDimitry Andric namespace llvm {
6550b57cec5SDimitry Andric
EmitCodeEmitter(RecordKeeper & RK,raw_ostream & OS)6560b57cec5SDimitry Andric void EmitCodeEmitter(RecordKeeper &RK, raw_ostream &OS) {
6570b57cec5SDimitry Andric emitSourceFileHeader("Machine Code Emitter", OS);
6580b57cec5SDimitry Andric CodeEmitterGen(RK).run(OS);
6590b57cec5SDimitry Andric }
6600b57cec5SDimitry Andric
6610b57cec5SDimitry Andric } // end namespace llvm
662