Skip to content

Commit 74908ce

Browse files
committed
Create utility functions for mapping between the numeric opcode of a SILValue/SILInstruction and its mnemonic name
Use it for the stats collection, but also for SIL printing.
1 parent 7f63ad2 commit 74908ce

File tree

6 files changed

+69
-36
lines changed

6 files changed

+69
-36
lines changed

include/swift/SIL/SILInstruction.h

+6
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ enum class SILInstructionKind : std::underlying_type<ValueKind>::type {
6868
#include "SILNodes.def"
6969
};
7070

71+
/// Map SILInstruction's mnemonic name to its SILInstructionKind.
72+
SILInstructionKind getSILInstructionKind(StringRef InstName);
73+
74+
/// Map SILInstructionKind to a corresponding SILInstruction name.
75+
StringRef getSILInstructionName(SILInstructionKind Kind);
76+
7177
/// This is the root class for all instructions that can be used as the contents
7278
/// of a Swift SILBasicBlock.
7379
class SILInstruction : public ValueBase,public llvm::ilist_node<SILInstruction>{

include/swift/SIL/SILValue.h

+6
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,12 @@ inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, SILValue V) {
785785
return OS;
786786
}
787787

788+
/// Map a SILValue mnemonic name to its ValueKind.
789+
ValueKind getSILValueKind(StringRef Name);
790+
791+
/// Map ValueKind to a corresponding mnemonic name.
792+
StringRef getSILValueName(ValueKind Kind);
793+
788794
} // end namespace swift
789795

790796

lib/SIL/SILInstruction.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,25 @@ void SILInstruction::dropAllReferences() {
172172
int SILInstruction::NumCreatedInstructions = 0;
173173
int SILInstruction::NumDeletedInstructions = 0;
174174

175+
/// Map a SILInstruction name to its SILInstructionKind.
176+
SILInstructionKind swift::getSILInstructionKind(StringRef InstName) {
177+
auto Kind = getSILValueKind(InstName);
178+
if (Kind >= ValueKind::First_SILInstruction &&
179+
Kind <= ValueKind::Last_SILInstruction)
180+
return SILInstructionKind(Kind);
181+
182+
#ifdef NDEBUG
183+
llvm::errs() << "Unknown SIL instruction name\n";
184+
abort();
185+
#endif
186+
llvm_unreachable("Unknown SIL insruction name");
187+
}
188+
189+
/// Map SILInstructionKind to a corresponding SILInstruction name.
190+
StringRef swift::getSILInstructionName(SILInstructionKind Kind) {
191+
return getSILValueName(ValueKind(Kind));
192+
}
193+
175194
namespace {
176195
class InstructionDestroyer
177196
: public SILInstructionVisitor<InstructionDestroyer> {

lib/SIL/SILPrinter.cpp

+1-8
Original file line numberDiff line numberDiff line change
@@ -826,14 +826,7 @@ class SILPrinter : public SILVisitor<SILPrinter> {
826826
}
827827

828828
void printInstOpCode(SILInstruction *I) {
829-
// If we have a SILInstruction, print out the opcode.
830-
switch (SILInstructionKind(I->getKind())) {
831-
#define INST(Id, Parent, TextualName, MemoryBehavior, ReleasingBehavior) \
832-
case SILInstructionKind::Id: \
833-
*this << #TextualName " "; \
834-
break;
835-
#include "swift/SIL/SILNodes.def"
836-
}
829+
*this << getSILValueName(I->getKind()) << " ";
837830
}
838831

839832
void print(SILValue V) {

lib/SIL/SILValue.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,40 @@ ValueOwnershipKind SILValue::getOwnershipKind() const {
182182
sil::ValueOwnershipKindClassifier Classifier;
183183
return Classifier.visit(const_cast<ValueBase *>(Value));
184184
}
185+
186+
/// Map a SILValue mnemonic name to its ValueKind.
187+
ValueKind swift::getSILValueKind(StringRef Name) {
188+
#define INST(Id, Parent, TextualName, MemoryBehavior, ReleasingBehavior) \
189+
if (Name == #TextualName) \
190+
return ValueKind::Id;
191+
192+
#define VALUE(Id, Parent) \
193+
if (Name == #Id) \
194+
return ValueKind::Id;
195+
196+
#include "swift/SIL/SILNodes.def"
197+
198+
#ifdef NDEBUG
199+
llvm::errs()
200+
<< "Unknown SILValue name\n";
201+
abort();
202+
#endif
203+
llvm_unreachable("Unknown SILValue name");
204+
}
205+
206+
/// Map ValueKind to a corresponding mnemonic name.
207+
StringRef swift::getSILValueName(ValueKind Kind) {
208+
switch (Kind) {
209+
#define INST(Id, Parent, TextualName, MemoryBehavior, ReleasingBehavior) \
210+
case ValueKind::Id: \
211+
return #TextualName; \
212+
break;
213+
214+
#define VALUE(Id, Parent) \
215+
case ValueKind::Id: \
216+
return #Id; \
217+
break;
218+
219+
#include "swift/SIL/SILNodes.def"
220+
}
221+
}

lib/SILOptimizer/Utils/OptimizerStatsUtils.cpp

-28
Original file line numberDiff line numberDiff line change
@@ -84,34 +84,6 @@ constexpr unsigned SILInstructionsNum = int(ValueKind::Last_SILInstruction) + 1;
8484
/// A set of counters, one per SILInstruction kind.
8585
using InstructionCounts = SmallVector<int, SILInstructionsNum>;
8686

87-
/// Map a SILInstruction name to its SILInstructionKind.
88-
SILInstructionKind getSILInstructionKind(StringRef InstName) {
89-
#define INST(Id, Parent, TextualName, MemoryBehavior, ReleasingBehavior) \
90-
if (InstName == #TextualName) \
91-
return SILInstructionKind::Id;
92-
93-
#include "swift/SIL/SILNodes.def"
94-
95-
#ifdef NDEBUG
96-
llvm::errs()
97-
<< "Unknown instruction name used by the -sil-stats-only-instructions\n";
98-
abort();
99-
#endif
100-
llvm_unreachable(
101-
"Unknown insruction name used by the -sil-stats-only-instructions");
102-
}
103-
104-
/// Map SILInstructionKind to a corresponding SILInstruction name.
105-
const char *getSILInstructionName(SILInstructionKind Kind) {
106-
switch (Kind) {
107-
#define INST(Id, Parent, TextualName, MemoryBehavior, ReleasingBehavior) \
108-
case SILInstructionKind::Id: \
109-
return #TextualName; \
110-
break;
111-
#include "swift/SIL/SILNodes.def"
112-
}
113-
}
114-
11587
/// A helper type to parse a comma separated list of SIL instruction names
11688
/// provided as argument of the -sil-stats-only-instructions options.
11789
class StatsOnlyInstructionsOpt {

0 commit comments

Comments
 (0)