Skip to content

Commit ff1b01b

Browse files
authored
[llvm-exegesis] Begin replacing unsigned with MCRegister. NFC (#123109)
Some of this was needed to fix implicit conversions from MCRegister to unsigned when calling getReg() on MCOperand for example. The majority was done by reviewing parts of the code that dealt with registers, converting them to MCRegister and then seeing what new implicit conversions were created and fixing those. There were a few places where I used MCPhysReg instead of MCRegiser for static arrays since its uint16_t instead of unsigned.
1 parent fc7a1ed commit ff1b01b

26 files changed

+157
-150
lines changed

llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ static unsigned getLoadImmediateOpcode(unsigned RegBitWidth) {
2626
}
2727

2828
// Generates instruction to load an immediate value into a register.
29-
static MCInst loadImmediate(unsigned Reg, unsigned RegBitWidth,
29+
static MCInst loadImmediate(MCRegister Reg, unsigned RegBitWidth,
3030
const APInt &Value) {
3131
if (Value.getBitWidth() > RegBitWidth)
3232
llvm_unreachable("Value must fit in the Register");
@@ -45,7 +45,7 @@ class ExegesisAArch64Target : public ExegesisTarget {
4545
: ExegesisTarget(AArch64CpuPfmCounters, AArch64_MC::isOpcodeAvailable) {}
4646

4747
private:
48-
std::vector<MCInst> setRegTo(const MCSubtargetInfo &STI, unsigned Reg,
48+
std::vector<MCInst> setRegTo(const MCSubtargetInfo &STI, MCRegister Reg,
4949
const APInt &Value) const override {
5050
if (AArch64::GPR32RegClass.contains(Reg))
5151
return {loadImmediate(Reg, 32, Value)};

llvm/tools/llvm-exegesis/lib/Assembler.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ static bool generateSnippetSetupCode(const ExegesisTarget &ET,
8181
// If we're generating memory instructions, don't load in the value for
8282
// the register with the stack pointer as it will be used later to finish
8383
// the setup.
84-
if (RV.Register == StackPointerRegister)
84+
if (Register(RV.Register) == StackPointerRegister)
8585
continue;
8686
}
8787
// Load a constant in the register.
@@ -98,7 +98,7 @@ static bool generateSnippetSetupCode(const ExegesisTarget &ET,
9898
// Load in the stack register now as we're done using it elsewhere
9999
// and need to set the value in preparation for executing the
100100
// snippet.
101-
if (RV.Register != StackPointerRegister)
101+
if (Register(RV.Register) != StackPointerRegister)
102102
continue;
103103
const auto SetRegisterCode = ET.setRegTo(*MSI, RV.Register, RV.Value);
104104
if (SetRegisterCode.empty())
@@ -208,7 +208,7 @@ void BasicBlockFiller::addReturn(const ExegesisTarget &ET,
208208
}
209209

210210
FunctionFiller::FunctionFiller(MachineFunction &MF,
211-
std::vector<unsigned> RegistersSetUp)
211+
std::vector<MCRegister> RegistersSetUp)
212212
: MF(MF), MCII(MF.getTarget().getMCInstrInfo()), Entry(addBasicBlock()),
213213
RegistersSetUp(std::move(RegistersSetUp)) {}
214214

@@ -218,7 +218,7 @@ BasicBlockFiller FunctionFiller::addBasicBlock() {
218218
return BasicBlockFiller(MF, MBB, MCII);
219219
}
220220

221-
ArrayRef<unsigned> FunctionFiller::getRegistersSetUp() const {
221+
ArrayRef<MCRegister> FunctionFiller::getRegistersSetUp() const {
222222
return RegistersSetUp;
223223
}
224224

@@ -241,7 +241,7 @@ BitVector getFunctionReservedRegs(const TargetMachine &TM) {
241241

242242
Error assembleToStream(const ExegesisTarget &ET,
243243
std::unique_ptr<TargetMachine> TM,
244-
ArrayRef<unsigned> LiveIns, const FillFunction &Fill,
244+
ArrayRef<MCRegister> LiveIns, const FillFunction &Fill,
245245
raw_pwrite_stream &AsmStream, const BenchmarkKey &Key,
246246
bool GenerateMemoryInstructions) {
247247
auto Context = std::make_unique<LLVMContext>();
@@ -259,35 +259,35 @@ Error assembleToStream(const ExegesisTarget &ET,
259259
Properties.reset(MachineFunctionProperties::Property::IsSSA);
260260
Properties.set(MachineFunctionProperties::Property::NoPHIs);
261261

262-
for (const unsigned Reg : LiveIns)
262+
for (const MCRegister Reg : LiveIns)
263263
MF.getRegInfo().addLiveIn(Reg);
264264

265265
if (GenerateMemoryInstructions) {
266-
for (const unsigned Reg : ET.getArgumentRegisters())
266+
for (const MCRegister Reg : ET.getArgumentRegisters())
267267
MF.getRegInfo().addLiveIn(Reg);
268268
// Add a live in for registers that need saving so that the machine verifier
269269
// doesn't fail if the register is never defined.
270-
for (const unsigned Reg : ET.getRegistersNeedSaving())
270+
for (const MCRegister Reg : ET.getRegistersNeedSaving())
271271
MF.getRegInfo().addLiveIn(Reg);
272272
}
273273

274-
std::vector<unsigned> RegistersSetUp;
274+
std::vector<MCRegister> RegistersSetUp;
275275
RegistersSetUp.reserve(Key.RegisterInitialValues.size());
276276
for (const auto &InitValue : Key.RegisterInitialValues) {
277277
RegistersSetUp.push_back(InitValue.Register);
278278
}
279279
FunctionFiller Sink(MF, std::move(RegistersSetUp));
280280
auto Entry = Sink.getEntry();
281281

282-
for (const unsigned Reg : LiveIns)
282+
for (const MCRegister Reg : LiveIns)
283283
Entry.MBB->addLiveIn(Reg);
284284

285285
if (GenerateMemoryInstructions) {
286-
for (const unsigned Reg : ET.getArgumentRegisters())
286+
for (const MCRegister Reg : ET.getArgumentRegisters())
287287
Entry.MBB->addLiveIn(Reg);
288288
// Add a live in for registers that need saving so that the machine verifier
289289
// doesn't fail if the register is never defined.
290-
for (const unsigned Reg : ET.getRegistersNeedSaving())
290+
for (const MCRegister Reg : ET.getRegistersNeedSaving())
291291
Entry.MBB->addLiveIn(Reg);
292292
}
293293

llvm/tools/llvm-exegesis/lib/Assembler.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class BasicBlockFiller {
6161
// Helper to fill in a function.
6262
class FunctionFiller {
6363
public:
64-
FunctionFiller(MachineFunction &MF, std::vector<unsigned> RegistersSetUp);
64+
FunctionFiller(MachineFunction &MF, std::vector<MCRegister> RegistersSetUp);
6565

6666
// Adds a basic block to the function.
6767
BasicBlockFiller addBasicBlock();
@@ -73,12 +73,12 @@ class FunctionFiller {
7373
const MCInstrInfo *const MCII;
7474

7575
// Returns the set of registers in the snippet setup code.
76-
ArrayRef<unsigned> getRegistersSetUp() const;
76+
ArrayRef<MCRegister> getRegistersSetUp() const;
7777

7878
private:
7979
BasicBlockFiller Entry;
8080
// The set of registers that are set up in the basic block.
81-
std::vector<unsigned> RegistersSetUp;
81+
std::vector<MCRegister> RegistersSetUp;
8282
};
8383

8484
// A callback that fills a function.
@@ -90,7 +90,7 @@ using FillFunction = std::function<void(FunctionFiller &)>;
9090
// AsmStream, the temporary function is eventually discarded.
9191
Error assembleToStream(const ExegesisTarget &ET,
9292
std::unique_ptr<TargetMachine> TM,
93-
ArrayRef<unsigned> LiveIns, const FillFunction &Fill,
93+
ArrayRef<MCRegister> LiveIns, const FillFunction &Fill,
9494
raw_pwrite_stream &AsmStreamm, const BenchmarkKey &Key,
9595
bool GenerateMemoryInstructions);
9696

llvm/tools/llvm-exegesis/lib/BenchmarkCode.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ struct BenchmarkCode {
2323

2424
// We also need to provide the registers that are live on entry for the
2525
// assembler to generate proper prologue/epilogue.
26-
std::vector<unsigned> LiveIns;
26+
std::vector<MCRegister> LiveIns;
2727

2828
// Informations about how this configuration was built.
2929
std::string Info;

llvm/tools/llvm-exegesis/lib/BenchmarkResult.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,17 @@ struct YamlContext {
6565

6666
raw_string_ostream &getErrorStream() { return ErrorStream; }
6767

68-
StringRef getRegName(unsigned RegNo) {
69-
// Special case: RegNo 0 is NoRegister. We have to deal with it explicitly.
70-
if (RegNo == 0)
68+
StringRef getRegName(MCRegister Reg) {
69+
// Special case: Reg may be invalid. We have to deal with it explicitly.
70+
if (!Reg.isValid())
7171
return kNoRegister;
72-
const StringRef RegName = State->getRegInfo().getName(RegNo);
72+
const StringRef RegName = State->getRegInfo().getName(Reg);
7373
if (RegName.empty())
74-
ErrorStream << "No register with enum value '" << RegNo << "'\n";
74+
ErrorStream << "No register with enum value '" << Reg.id() << "'\n";
7575
return RegName;
7676
}
7777

78-
std::optional<unsigned> getRegNo(StringRef RegName) {
78+
std::optional<MCRegister> getRegNo(StringRef RegName) {
7979
std::optional<MCRegister> RegisterNumber =
8080
State->getRegisterNumberFromName(RegName);
8181
if (!RegisterNumber.has_value())
@@ -261,7 +261,7 @@ template <> struct ScalarTraits<exegesis::RegisterValue> {
261261
String.split(Pieces, "=0x", /* MaxSplit */ -1,
262262
/* KeepEmpty */ false);
263263
YamlContext &Context = getTypedContext(Ctx);
264-
std::optional<unsigned> RegNo;
264+
std::optional<MCRegister> RegNo;
265265
if (Pieces.size() == 2 && (RegNo = Context.getRegNo(Pieces[0]))) {
266266
RV.Register = *RegNo;
267267
const unsigned BitsNeeded = APInt::getBitsNeeded(Pieces[1], kRadix);

llvm/tools/llvm-exegesis/lib/BenchmarkResult.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ struct BenchmarkKey {
7575
// being used supports it.
7676
uintptr_t SnippetAddress = 0;
7777
// The register that should be used to hold the loop counter.
78-
unsigned LoopRegister;
78+
MCRegister LoopRegister;
7979
};
8080

8181
struct BenchmarkMeasure {

llvm/tools/llvm-exegesis/lib/CodeTemplate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ struct CodeTemplate {
131131
std::vector<InstructionTemplate> Instructions;
132132
// If the template uses the provided scratch memory, the register in which
133133
// the pointer to this memory is passed in to the function.
134-
unsigned ScratchSpacePointerInReg = 0;
134+
MCRegister ScratchSpacePointerInReg;
135135

136136
#if defined(__GNUC__) && (defined(__clang__) || LLVM_GNUC_PREREQ(8, 0, 0))
137137
// FIXME: GCC7 bug workaround. Drop #if after GCC7 no longer supported.

llvm/tools/llvm-exegesis/lib/LlvmState.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ LLVMState::LLVMState(std::unique_ptr<const TargetMachine> TM,
8383
OpcodeNameToOpcodeIdxMapping(createOpcodeNameToOpcodeIdxMapping()),
8484
RegNameToRegNoMapping(createRegNameToRegNoMapping()) {
8585
BitVector ReservedRegs = getFunctionReservedRegs(getTargetMachine());
86-
for (const unsigned Reg : TheExegesisTarget->getUnavailableRegisters())
86+
for (const MCPhysReg Reg : TheExegesisTarget->getUnavailableRegisters())
8787
ReservedRegs.set(Reg);
8888
RATC.reset(
8989
new RegisterAliasingTrackerCache(getRegInfo(), std::move(ReservedRegs)));

llvm/tools/llvm-exegesis/lib/MCInstrDescView.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ bool Operand::isExplicit() const { return Info; }
3838

3939
bool Operand::isImplicit() const { return !Info; }
4040

41-
bool Operand::isImplicitReg() const { return ImplicitReg; }
41+
bool Operand::isImplicitReg() const { return ImplicitReg.isValid(); }
4242

4343
bool Operand::isDef() const { return IsDef; }
4444

@@ -64,7 +64,7 @@ unsigned Operand::getTiedToIndex() const { return *TiedToIndex; }
6464

6565
unsigned Operand::getVariableIndex() const { return *VariableIndex; }
6666

67-
unsigned Operand::getImplicitReg() const {
67+
MCRegister Operand::getImplicitReg() const {
6868
assert(ImplicitReg);
6969
return ImplicitReg;
7070
}

llvm/tools/llvm-exegesis/lib/MCInstrDescView.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ struct Operand {
7575
unsigned getIndex() const;
7676
unsigned getTiedToIndex() const;
7777
unsigned getVariableIndex() const;
78-
unsigned getImplicitReg() const;
78+
MCRegister getImplicitReg() const;
7979
const RegisterAliasingTracker &getRegisterAliasing() const;
8080
const MCOperandInfo &getExplicitOperandInfo() const;
8181

@@ -85,7 +85,7 @@ struct Operand {
8585
const RegisterAliasingTracker *Tracker = nullptr; // Set for Register Op.
8686
const MCOperandInfo *Info = nullptr; // Set for Explicit Op.
8787
std::optional<uint8_t> TiedToIndex; // Set for Reg&Explicit Op.
88-
MCPhysReg ImplicitReg = 0; // Non-0 for Implicit Op.
88+
MCRegister ImplicitReg; // Non-0 for Implicit Op.
8989
std::optional<uint8_t> VariableIndex; // Set for Explicit Op.
9090
};
9191

0 commit comments

Comments
 (0)