Skip to content

Commit 854f268

Browse files
committed
[MC] Move deprecation infos from MCTargetDesc to MCInstrInfo
This allows emitting it only when the feature is used by a target. Shrinks Release+Asserts clang by 900k.
1 parent b9d9968 commit 854f268

File tree

8 files changed

+136
-63
lines changed

8 files changed

+136
-63
lines changed

llvm/include/llvm/MC/MCInstrDesc.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -197,15 +197,6 @@ class MCInstrDesc {
197197
const MCPhysReg *ImplicitUses; // Registers implicitly read by this instr
198198
const MCPhysReg *ImplicitDefs; // Registers implicitly defined by this instr
199199
const MCOperandInfo *OpInfo; // 'NumOperands' entries about operands
200-
// Subtarget feature that this is deprecated on, if any
201-
// -1 implies this is not deprecated by any single feature. It may still be
202-
// deprecated due to a "complex" reason, below.
203-
int64_t DeprecatedFeature;
204-
205-
// A complex method to determine if a certain instruction is deprecated or
206-
// not, and return the reason for deprecation.
207-
bool (*ComplexDeprecationInfo)(MCInst &, const MCSubtargetInfo &,
208-
std::string &);
209200

210201
/// Returns the value of the specific constraint if
211202
/// it is set. Returns -1 if it is not set.
@@ -219,11 +210,6 @@ class MCInstrDesc {
219210
return -1;
220211
}
221212

222-
/// Returns true if a certain instruction is deprecated and if so
223-
/// returns the reason in \p Info.
224-
bool getDeprecatedInfo(MCInst &MI, const MCSubtargetInfo &STI,
225-
std::string &Info) const;
226-
227213
/// Return the opcode number for this descriptor.
228214
unsigned getOpcode() const { return Opcode; }
229215

llvm/include/llvm/MC/MCInstrInfo.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,35 @@ namespace llvm {
2121
//---------------------------------------------------------------------------
2222
/// Interface to description of machine instruction set.
2323
class MCInstrInfo {
24+
public:
25+
using ComplexDeprecationPredicate = bool (*)(MCInst &,
26+
const MCSubtargetInfo &,
27+
std::string &);
28+
29+
private:
2430
const MCInstrDesc *Desc; // Raw array to allow static init'n
2531
const unsigned *InstrNameIndices; // Array for name indices in InstrNameData
2632
const char *InstrNameData; // Instruction name string pool
33+
// Subtarget feature that an instruction is deprecated on, if any
34+
// -1 implies this is not deprecated by any single feature. It may still be
35+
// deprecated due to a "complex" reason, below.
36+
const uint8_t *DeprecatedFeatures;
37+
// A complex method to determine if a certain instruction is deprecated or
38+
// not, and return the reason for deprecation.
39+
const ComplexDeprecationPredicate *ComplexDeprecationInfos;
2740
unsigned NumOpcodes; // Number of entries in the desc array
2841

2942
public:
3043
/// Initialize MCInstrInfo, called by TableGen auto-generated routines.
3144
/// *DO NOT USE*.
3245
void InitMCInstrInfo(const MCInstrDesc *D, const unsigned *NI, const char *ND,
33-
unsigned NO) {
46+
const uint8_t *DF,
47+
const ComplexDeprecationPredicate *CDI, unsigned NO) {
3448
Desc = D;
3549
InstrNameIndices = NI;
3650
InstrNameData = ND;
51+
DeprecatedFeatures = DF;
52+
ComplexDeprecationInfos = CDI;
3753
NumOpcodes = NO;
3854
}
3955

@@ -51,6 +67,11 @@ class MCInstrInfo {
5167
assert(Opcode < NumOpcodes && "Invalid opcode!");
5268
return StringRef(&InstrNameData[InstrNameIndices[Opcode]]);
5369
}
70+
71+
/// Returns true if a certain instruction is deprecated and if so
72+
/// returns the reason in \p Info.
73+
bool getDeprecatedInfo(MCInst &MI, const MCSubtargetInfo &STI,
74+
std::string &Info) const;
5475
};
5576

5677
} // End llvm namespace

llvm/lib/MC/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ add_llvm_component_library(LLVMMC
2323
MCInstPrinter.cpp
2424
MCInstrAnalysis.cpp
2525
MCInstrDesc.cpp
26+
MCInstrInfo.cpp
2627
MCLabel.cpp
2728
MCLinkerOptimizationHint.cpp
2829
MCMachOStreamer.cpp

llvm/lib/MC/MCInstrDesc.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,6 @@
1818

1919
using namespace llvm;
2020

21-
bool MCInstrDesc::getDeprecatedInfo(MCInst &MI, const MCSubtargetInfo &STI,
22-
std::string &Info) const {
23-
if (ComplexDeprecationInfo)
24-
return ComplexDeprecationInfo(MI, STI, Info);
25-
if (DeprecatedFeature != -1 && STI.getFeatureBits()[DeprecatedFeature]) {
26-
// FIXME: it would be nice to include the subtarget feature here.
27-
Info = "deprecated";
28-
return true;
29-
}
30-
return false;
31-
}
3221
bool MCInstrDesc::mayAffectControlFlow(const MCInst &MI,
3322
const MCRegisterInfo &RI) const {
3423
if (isBranch() || isCall() || isReturn() || isIndirectBranch())

llvm/lib/MC/MCInstrInfo.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===- lib/MC/MCInstrInfo.cpp - Target Instruction Info -------------------===//
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+
#include "llvm/MC/MCInstrInfo.h"
10+
#include "llvm/MC/MCInst.h"
11+
#include "llvm/MC/MCSubtargetInfo.h"
12+
13+
using namespace llvm;
14+
15+
bool MCInstrInfo::getDeprecatedInfo(MCInst &MI, const MCSubtargetInfo &STI,
16+
std::string &Info) const {
17+
unsigned Opcode = MI.getOpcode();
18+
if (ComplexDeprecationInfos && ComplexDeprecationInfos[Opcode])
19+
return ComplexDeprecationInfos[Opcode](MI, STI, Info);
20+
if (DeprecatedFeatures && DeprecatedFeatures[Opcode] != uint8_t(-1U) &&
21+
STI.getFeatureBits()[DeprecatedFeatures[Opcode]]) {
22+
// FIXME: it would be nice to include the subtarget feature here.
23+
Info = "deprecated";
24+
return true;
25+
}
26+
return false;
27+
}

llvm/unittests/CodeGen/MachineInstrTest.cpp

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ TEST(IsIdenticalToTest, DifferentDefs) {
5050
{0, 0, MCOI::OPERAND_REGISTER, 0},
5151
{0, 1 << MCOI::OptionalDef, MCOI::OPERAND_REGISTER, 0}};
5252
MCInstrDesc MCID = {
53-
0, NumOps, NumDefs, 0, 0, 1ULL << MCID::HasOptionalDef,
54-
0, nullptr, nullptr, OpInfo, 0, nullptr};
53+
0, NumOps, NumDefs, 0, 0, 1ULL << MCID::HasOptionalDef,
54+
0, nullptr, nullptr, OpInfo};
5555

5656
// Create two MIs with different virtual reg defs and the same uses.
5757
unsigned VirtualDef1 = -42; // The value doesn't matter, but the sign does.
@@ -121,8 +121,8 @@ TEST(MachineInstrExpressionTraitTest, IsEqualAgreesWithGetHashValue) {
121121
{0, 0, MCOI::OPERAND_REGISTER, 0},
122122
{0, 1 << MCOI::OptionalDef, MCOI::OPERAND_REGISTER, 0}};
123123
MCInstrDesc MCID = {
124-
0, NumOps, NumDefs, 0, 0, 1ULL << MCID::HasOptionalDef,
125-
0, nullptr, nullptr, OpInfo, 0, nullptr};
124+
0, NumOps, NumDefs, 0, 0, 1ULL << MCID::HasOptionalDef,
125+
0, nullptr, nullptr, OpInfo};
126126

127127
// Define a series of instructions with different kinds of operands and make
128128
// sure that the hash function is consistent with isEqual for various
@@ -196,8 +196,7 @@ TEST(MachineInstrPrintingTest, DebugLocPrinting) {
196196
auto MF = createMachineFunction(Ctx, Mod);
197197

198198
MCOperandInfo OpInfo{0, 0, MCOI::OPERAND_REGISTER, 0};
199-
MCInstrDesc MCID = {0, 1, 1, 0, 0, 0,
200-
0, nullptr, nullptr, &OpInfo, 0, nullptr};
199+
MCInstrDesc MCID = {0, 1, 1, 0, 0, 0, 0, nullptr, nullptr, &OpInfo};
201200

202201
DIFile *DIF = DIFile::getDistinct(Ctx, "filename", "");
203202
DISubprogram *DIS = DISubprogram::getDistinct(
@@ -224,8 +223,7 @@ TEST(MachineInstrSpan, DistanceBegin) {
224223
auto MF = createMachineFunction(Ctx, Mod);
225224
auto MBB = MF->CreateMachineBasicBlock();
226225

227-
MCInstrDesc MCID = {0, 0, 0, 0, 0, 0,
228-
0, nullptr, nullptr, nullptr, 0, nullptr};
226+
MCInstrDesc MCID = {0, 0, 0, 0, 0, 0, 0, nullptr, nullptr, nullptr};
229227

230228
auto MII = MBB->begin();
231229
MachineInstrSpan MIS(MII, MBB);
@@ -242,8 +240,7 @@ TEST(MachineInstrSpan, DistanceEnd) {
242240
auto MF = createMachineFunction(Ctx, Mod);
243241
auto MBB = MF->CreateMachineBasicBlock();
244242

245-
MCInstrDesc MCID = {0, 0, 0, 0, 0, 0,
246-
0, nullptr, nullptr, nullptr, 0, nullptr};
243+
MCInstrDesc MCID = {0, 0, 0, 0, 0, 0, 0, nullptr, nullptr, nullptr};
247244

248245
auto MII = MBB->end();
249246
MachineInstrSpan MIS(MII, MBB);
@@ -258,8 +255,7 @@ TEST(MachineInstrExtraInfo, AddExtraInfo) {
258255
LLVMContext Ctx;
259256
Module Mod("Module", Ctx);
260257
auto MF = createMachineFunction(Ctx, Mod);
261-
MCInstrDesc MCID = {0, 0, 0, 0, 0, 0,
262-
0, nullptr, nullptr, nullptr, 0, nullptr};
258+
MCInstrDesc MCID = {0, 0, 0, 0, 0, 0, 0, nullptr, nullptr, nullptr};
263259

264260
auto MI = MF->CreateMachineInstr(MCID, DebugLoc());
265261
auto MAI = MCAsmInfo();
@@ -306,8 +302,7 @@ TEST(MachineInstrExtraInfo, ChangeExtraInfo) {
306302
LLVMContext Ctx;
307303
Module Mod("Module", Ctx);
308304
auto MF = createMachineFunction(Ctx, Mod);
309-
MCInstrDesc MCID = {0, 0, 0, 0, 0, 0,
310-
0, nullptr, nullptr, nullptr, 0, nullptr};
305+
MCInstrDesc MCID = {0, 0, 0, 0, 0, 0, 0, nullptr, nullptr, nullptr};
311306

312307
auto MI = MF->CreateMachineInstr(MCID, DebugLoc());
313308
auto MAI = MCAsmInfo();
@@ -344,8 +339,7 @@ TEST(MachineInstrExtraInfo, RemoveExtraInfo) {
344339
LLVMContext Ctx;
345340
Module Mod("Module", Ctx);
346341
auto MF = createMachineFunction(Ctx, Mod);
347-
MCInstrDesc MCID = {0, 0, 0, 0, 0, 0,
348-
0, nullptr, nullptr, nullptr, 0, nullptr};
342+
MCInstrDesc MCID = {0, 0, 0, 0, 0, 0, 0, nullptr, nullptr, nullptr};
349343

350344
auto MI = MF->CreateMachineInstr(MCID, DebugLoc());
351345
auto MAI = MCAsmInfo();

llvm/utils/TableGen/AsmMatcherEmitter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3863,7 +3863,7 @@ void AsmMatcherEmitter::run(raw_ostream &OS) {
38633863
OS << " std::string Info;\n";
38643864
OS << " if (!getParser().getTargetParser().\n";
38653865
OS << " getTargetOptions().MCNoDeprecatedWarn &&\n";
3866-
OS << " MII.get(Inst.getOpcode()).getDeprecatedInfo(Inst, getSTI(), Info)) {\n";
3866+
OS << " MII.getDeprecatedInfo(Inst, getSTI(), Info)) {\n";
38673867
OS << " SMLoc Loc = ((" << Target.getName()
38683868
<< "Operand&)*Operands[0]).getStartLoc();\n";
38693869
OS << " getParser().Warning(Loc, Info, None);\n";

llvm/utils/TableGen/InstrInfoEmitter.cpp

Lines changed: 75 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -581,15 +581,66 @@ void InstrInfoEmitter::run(raw_ostream &OS) {
581581
OS << InstrNames.get(std::string(Inst->TheDef->getName())) << "U, ";
582582
++Num;
583583
}
584-
585584
OS << "\n};\n\n";
586585

586+
bool HasDeprecationFeatures =
587+
llvm::any_of(NumberedInstructions, [](const CodeGenInstruction *Inst) {
588+
return !Inst->HasComplexDeprecationPredicate &&
589+
!Inst->DeprecatedReason.empty();
590+
});
591+
if (HasDeprecationFeatures) {
592+
OS << "extern const uint8_t " << TargetName
593+
<< "InstrDeprecationFeatures[] = {";
594+
Num = 0;
595+
for (const CodeGenInstruction *Inst : NumberedInstructions) {
596+
if (Num % 8 == 0)
597+
OS << "\n ";
598+
if (!Inst->HasComplexDeprecationPredicate &&
599+
!Inst->DeprecatedReason.empty())
600+
OS << Target.getInstNamespace() << "::" << Inst->DeprecatedReason
601+
<< ", ";
602+
else
603+
OS << "uint8_t(-1), ";
604+
++Num;
605+
}
606+
OS << "\n};\n\n";
607+
}
608+
609+
bool HasComplexDeprecationInfos =
610+
llvm::any_of(NumberedInstructions, [](const CodeGenInstruction *Inst) {
611+
return Inst->HasComplexDeprecationPredicate;
612+
});
613+
if (HasComplexDeprecationInfos) {
614+
OS << "extern const MCInstrInfo::ComplexDeprecationPredicate " << TargetName
615+
<< "InstrComplexDeprecationInfos[] = {";
616+
Num = 0;
617+
for (const CodeGenInstruction *Inst : NumberedInstructions) {
618+
if (Num % 8 == 0)
619+
OS << "\n ";
620+
if (Inst->HasComplexDeprecationPredicate)
621+
// Emit a function pointer to the complex predicate method.
622+
OS << "&get" << Inst->DeprecatedReason << "DeprecationInfo, ";
623+
else
624+
OS << "nullptr, ";
625+
++Num;
626+
}
627+
OS << "\n};\n\n";
628+
}
629+
587630
// MCInstrInfo initialization routine.
588631
OS << "static inline void Init" << TargetName
589632
<< "MCInstrInfo(MCInstrInfo *II) {\n";
590-
OS << " II->InitMCInstrInfo(" << TargetName << "Insts, "
591-
<< TargetName << "InstrNameIndices, " << TargetName << "InstrNameData, "
592-
<< NumberedInstructions.size() << ");\n}\n\n";
633+
OS << " II->InitMCInstrInfo(" << TargetName << "Insts, " << TargetName
634+
<< "InstrNameIndices, " << TargetName << "InstrNameData, ";
635+
if (HasDeprecationFeatures)
636+
OS << TargetName << "InstrDeprecationFeatures, ";
637+
else
638+
OS << "nullptr, ";
639+
if (HasComplexDeprecationInfos)
640+
OS << TargetName << "InstrComplexDeprecationInfos, ";
641+
else
642+
OS << "nullptr, ";
643+
OS << NumberedInstructions.size() << ");\n}\n\n";
593644

594645
OS << "} // end namespace llvm\n";
595646

@@ -629,12 +680,28 @@ void InstrInfoEmitter::run(raw_ostream &OS) {
629680
OS << "extern const MCInstrDesc " << TargetName << "Insts[];\n";
630681
OS << "extern const unsigned " << TargetName << "InstrNameIndices[];\n";
631682
OS << "extern const char " << TargetName << "InstrNameData[];\n";
683+
if (HasDeprecationFeatures)
684+
OS << "extern const uint8_t " << TargetName
685+
<< "InstrDeprecationFeatures[];\n";
686+
if (HasComplexDeprecationInfos)
687+
OS << "extern const MCInstrInfo::ComplexDeprecationPredicate " << TargetName
688+
<< "InstrComplexDeprecationInfos[];\n";
632689
OS << ClassName << "::" << ClassName
633-
<< "(int CFSetupOpcode, int CFDestroyOpcode, int CatchRetOpcode, int ReturnOpcode)\n"
634-
<< " : TargetInstrInfo(CFSetupOpcode, CFDestroyOpcode, CatchRetOpcode, ReturnOpcode) {\n"
690+
<< "(int CFSetupOpcode, int CFDestroyOpcode, int CatchRetOpcode, int "
691+
"ReturnOpcode)\n"
692+
<< " : TargetInstrInfo(CFSetupOpcode, CFDestroyOpcode, CatchRetOpcode, "
693+
"ReturnOpcode) {\n"
635694
<< " InitMCInstrInfo(" << TargetName << "Insts, " << TargetName
636-
<< "InstrNameIndices, " << TargetName << "InstrNameData, "
637-
<< NumberedInstructions.size() << ");\n}\n";
695+
<< "InstrNameIndices, " << TargetName << "InstrNameData, ";
696+
if (HasDeprecationFeatures)
697+
OS << TargetName << "InstrDeprecationFeatures, ";
698+
else
699+
OS << "nullptr, ";
700+
if (HasComplexDeprecationInfos)
701+
OS << TargetName << "InstrComplexDeprecationInfos, ";
702+
else
703+
OS << "nullptr, ";
704+
OS << NumberedInstructions.size() << ");\n}\n";
638705
OS << "} // end namespace llvm\n";
639706

640707
OS << "#endif // GET_INSTRINFO_CTOR_DTOR\n\n";
@@ -745,18 +812,6 @@ void InstrInfoEmitter::emitRecord(const CodeGenInstruction &Inst, unsigned Num,
745812
else
746813
OS << "OperandInfo" << OpInfo.find(OperandInfo)->second;
747814

748-
if (Inst.HasComplexDeprecationPredicate)
749-
// Emit a function pointer to the complex predicate method.
750-
OS << ", -1 "
751-
<< ",&get" << Inst.DeprecatedReason << "DeprecationInfo";
752-
else if (!Inst.DeprecatedReason.empty())
753-
// Emit the Subtarget feature.
754-
OS << ", " << Target.getInstNamespace() << "::" << Inst.DeprecatedReason
755-
<< " ,nullptr";
756-
else
757-
// Instruction isn't deprecated.
758-
OS << ", -1 ,nullptr";
759-
760815
OS << " }, // Inst #" << Num << " = " << Inst.TheDef->getName() << "\n";
761816
}
762817

0 commit comments

Comments
 (0)