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

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ class ExegesisMipsTarget : public ExegesisTarget {
5858
: ExegesisTarget(MipsCpuPfmCounters, Mips_MC::isOpcodeAvailable) {}
5959

6060
private:
61-
unsigned getScratchMemoryRegister(const Triple &TT) const override;
61+
MCRegister getScratchMemoryRegister(const Triple &TT) const override;
6262
unsigned getMaxMemoryAccessSize() const override { return 64; }
63-
void fillMemoryOperands(InstructionTemplate &IT, unsigned Reg,
63+
void fillMemoryOperands(InstructionTemplate &IT, MCRegister Reg,
6464
unsigned Offset) const override;
6565

66-
std::vector<MCInst> setRegTo(const MCSubtargetInfo &STI, unsigned Reg,
66+
std::vector<MCInst> setRegTo(const MCSubtargetInfo &STI, MCRegister Reg,
6767
const APInt &Value) const override;
6868
bool matchesArch(Triple::ArchType Arch) const override {
6969
return Arch == Triple::mips || Arch == Triple::mipsel ||
@@ -73,7 +73,7 @@ class ExegesisMipsTarget : public ExegesisTarget {
7373
} // end anonymous namespace
7474

7575
// Generates instructions to load an immediate value into a register.
76-
static std::vector<MCInst> loadImmediate(unsigned Reg, bool IsGPR32,
76+
static std::vector<MCInst> loadImmediate(MCRegister Reg, bool IsGPR32,
7777
const APInt &Value) {
7878
unsigned ZeroReg;
7979
unsigned ORi, LUi, SLL;
@@ -134,12 +134,13 @@ static std::vector<MCInst> loadImmediate(unsigned Reg, bool IsGPR32,
134134
llvm_unreachable("Not implemented for values wider than 32 bits");
135135
}
136136

137-
unsigned ExegesisMipsTarget::getScratchMemoryRegister(const Triple &TT) const {
137+
MCRegister
138+
ExegesisMipsTarget::getScratchMemoryRegister(const Triple &TT) const {
138139
return TT.isArch64Bit() ? Mips::A0_64 : Mips::A0;
139140
}
140141

141142
void ExegesisMipsTarget::fillMemoryOperands(InstructionTemplate &IT,
142-
unsigned Reg,
143+
MCRegister Reg,
143144
unsigned Offset) const {
144145
assert(!isInvalidMemoryInstr(IT.getInstr()) &&
145146
"fillMemoryOperands requires a valid memory instruction");
@@ -149,7 +150,7 @@ void ExegesisMipsTarget::fillMemoryOperands(InstructionTemplate &IT,
149150
}
150151

151152
std::vector<MCInst> ExegesisMipsTarget::setRegTo(const MCSubtargetInfo &STI,
152-
unsigned Reg,
153+
MCRegister Reg,
153154
const APInt &Value) const {
154155
if (Mips::GPR32RegClass.contains(Reg))
155156
return loadImmediate(Reg, true, Value);

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ static bool hasVariablesWithTiedOperands(const Instruction &Instr) {
9090
ParallelSnippetGenerator::~ParallelSnippetGenerator() = default;
9191

9292
void ParallelSnippetGenerator::instantiateMemoryOperands(
93-
const unsigned ScratchSpacePointerInReg,
93+
const MCRegister ScratchSpacePointerInReg,
9494
std::vector<InstructionTemplate> &Instructions) const {
95-
if (ScratchSpacePointerInReg == 0)
95+
if (!ScratchSpacePointerInReg)
9696
return; // no memory operands.
9797
const auto &ET = State.getExegesisTarget();
9898
const unsigned MemStep = ET.getMaxMemoryAccessSize();
@@ -261,10 +261,10 @@ generateSnippetForInstrAvoidingDefUseOverlap(
261261
if (Op.isReg() && Op.isImplicit() && !Op.isMemory()) {
262262
assert(Op.isImplicitReg() && "Not an implicit register operand?");
263263
if (Op.isUse())
264-
ImplicitUses.set(Op.getImplicitReg());
264+
ImplicitUses.set(Op.getImplicitReg().id());
265265
else {
266266
assert(Op.isDef() && "Not a use and not a def?");
267-
ImplicitDefs.set(Op.getImplicitReg());
267+
ImplicitDefs.set(Op.getImplicitReg().id());
268268
}
269269
}
270270
}
@@ -300,7 +300,7 @@ ParallelSnippetGenerator::generateCodeTemplates(
300300
Instr.hasMemoryOperands()
301301
? State.getExegesisTarget().getScratchMemoryRegister(
302302
State.getTargetMachine().getTargetTriple())
303-
: 0;
303+
: MCRegister();
304304
const AliasingConfigurations SelfAliasing(Instr, Instr, ForbiddenRegisters);
305305
if (SelfAliasing.empty()) {
306306
CT.Info = "instruction is parallel, repeating a random one.";

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class ParallelSnippetGenerator : public SnippetGenerator {
5555
// add eax, [rdi + 192]
5656
// mov eax, [rdi + 256]
5757
void instantiateMemoryOperands(
58-
unsigned ScratchSpaceReg,
58+
MCRegister ScratchSpaceReg,
5959
std::vector<InstructionTemplate> &SnippetTemplate) const;
6060
};
6161

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ class ExegesisPowerPCTarget : public ExegesisTarget {
3333
: ExegesisTarget(PPCCpuPfmCounters, PPC_MC::isOpcodeAvailable) {}
3434

3535
private:
36-
std::vector<MCInst> setRegTo(const MCSubtargetInfo &STI, unsigned Reg,
36+
std::vector<MCInst> setRegTo(const MCSubtargetInfo &STI, MCRegister Reg,
3737
const APInt &Value) const override;
3838
bool matchesArch(Triple::ArchType Arch) const override {
3939
return Arch == Triple::ppc64le;
4040
}
41-
unsigned getScratchMemoryRegister(const Triple &) const override;
42-
void fillMemoryOperands(InstructionTemplate &IT, unsigned Reg,
41+
MCRegister getScratchMemoryRegister(const Triple &) const override;
42+
void fillMemoryOperands(InstructionTemplate &IT, MCRegister Reg,
4343
unsigned Offset) const override;
4444
};
4545
} // end anonymous namespace
@@ -55,7 +55,7 @@ static unsigned getLoadImmediateOpcode(unsigned RegBitWidth) {
5555
}
5656

5757
// Generates instruction to load an immediate value into a register.
58-
static MCInst loadImmediate(unsigned Reg, unsigned RegBitWidth,
58+
static MCInst loadImmediate(MCRegister Reg, unsigned RegBitWidth,
5959
const APInt &Value) {
6060
if (Value.getBitWidth() > RegBitWidth)
6161
llvm_unreachable("Value must fit in the Register");
@@ -67,15 +67,15 @@ static MCInst loadImmediate(unsigned Reg, unsigned RegBitWidth,
6767
.addImm(Value.getZExtValue());
6868
}
6969

70-
unsigned
70+
MCRegister
7171
ExegesisPowerPCTarget::getScratchMemoryRegister(const Triple &TT) const {
7272
// R13 is reserved as Thread Pointer, we won't use threading in benchmark, so
7373
// use it as scratch memory register
7474
return TT.isArch64Bit() ? PPC::X13 : PPC::R13;
7575
}
7676

7777
void ExegesisPowerPCTarget::fillMemoryOperands(InstructionTemplate &IT,
78-
unsigned Reg,
78+
MCRegister Reg,
7979
unsigned Offset) const {
8080
int MemOpIdx = 0;
8181
if (IT.getInstr().hasTiedRegisters())
@@ -93,7 +93,7 @@ void ExegesisPowerPCTarget::fillMemoryOperands(InstructionTemplate &IT,
9393
}
9494

9595
std::vector<MCInst> ExegesisPowerPCTarget::setRegTo(const MCSubtargetInfo &STI,
96-
unsigned Reg,
96+
MCRegister Reg,
9797
const APInt &Value) const {
9898
// X11 is optional use in function linkage, should be the least used one
9999
// Use it as scratch reg to load immediate.

0 commit comments

Comments
 (0)