Skip to content

Commit e3a676e

Browse files
committed
CodeGen: Introduce a class for registers
Avoids using a plain unsigned for registers throughoug codegen. Doesn't attempt to change every register use, just something a little more than the set needed to build after changing the return type of MachineOperand::getReg(). llvm-svn: 364191
1 parent 3260ef1 commit e3a676e

File tree

95 files changed

+553
-487
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+553
-487
lines changed

llvm/include/llvm/CodeGen/GlobalISel/CallLowering.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ class CallLowering {
163163
///
164164
/// \return True if the lowering succeeds, false otherwise.
165165
virtual bool lowerReturn(MachineIRBuilder &MIRBuilder, const Value *Val,
166-
ArrayRef<unsigned> VRegs,
167-
unsigned SwiftErrorVReg) const {
166+
ArrayRef<Register> VRegs,
167+
Register SwiftErrorVReg) const {
168168
if (!supportSwiftError()) {
169169
assert(SwiftErrorVReg == 0 && "attempt to use unsupported swifterror");
170170
return lowerReturn(MIRBuilder, Val, VRegs);
@@ -175,7 +175,7 @@ class CallLowering {
175175
/// This hook behaves as the extended lowerReturn function, but for targets
176176
/// that do not support swifterror value promotion.
177177
virtual bool lowerReturn(MachineIRBuilder &MIRBuilder, const Value *Val,
178-
ArrayRef<unsigned> VRegs) const {
178+
ArrayRef<Register> VRegs) const {
179179
return false;
180180
}
181181

@@ -191,7 +191,7 @@ class CallLowering {
191191
/// \return True if the lowering succeeded, false otherwise.
192192
virtual bool lowerFormalArguments(MachineIRBuilder &MIRBuilder,
193193
const Function &F,
194-
ArrayRef<unsigned> VRegs) const {
194+
ArrayRef<Register> VRegs) const {
195195
return false;
196196
}
197197

@@ -216,7 +216,7 @@ class CallLowering {
216216
virtual bool lowerCall(MachineIRBuilder &MIRBuilder, CallingConv::ID CallConv,
217217
const MachineOperand &Callee, const ArgInfo &OrigRet,
218218
ArrayRef<ArgInfo> OrigArgs,
219-
unsigned SwiftErrorVReg) const {
219+
Register SwiftErrorVReg) const {
220220
if (!supportSwiftError()) {
221221
assert(SwiftErrorVReg == 0 && "trying to use unsupported swifterror");
222222
return lowerCall(MIRBuilder, CallConv, Callee, OrigRet, OrigArgs);
@@ -254,8 +254,8 @@ class CallLowering {
254254
///
255255
/// \return true if the lowering succeeded, false otherwise.
256256
bool lowerCall(MachineIRBuilder &MIRBuilder, ImmutableCallSite CS,
257-
unsigned ResReg, ArrayRef<unsigned> ArgRegs,
258-
unsigned SwiftErrorVReg,
257+
Register ResReg, ArrayRef<Register> ArgRegs,
258+
Register SwiftErrorVReg,
259259
std::function<unsigned()> GetCalleeReg) const;
260260

261261
};

llvm/include/llvm/CodeGen/GlobalISel/IRTranslator.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class IRTranslator : public MachineFunctionPass {
7171
public:
7272
ValueToVRegInfo() = default;
7373

74-
using VRegListT = SmallVector<unsigned, 1>;
74+
using VRegListT = SmallVector<Register, 1>;
7575
using OffsetListT = SmallVector<uint64_t, 1>;
7676

7777
using const_vreg_iterator =
@@ -559,9 +559,9 @@ class IRTranslator : public MachineFunctionPass {
559559
/// Non-aggregate types have just one corresponding VReg and the list can be
560560
/// used as a single "unsigned". Aggregates get flattened. If such VRegs do
561561
/// not exist, they are created.
562-
ArrayRef<unsigned> getOrCreateVRegs(const Value &Val);
562+
ArrayRef<Register> getOrCreateVRegs(const Value &Val);
563563

564-
unsigned getOrCreateVReg(const Value &Val) {
564+
Register getOrCreateVReg(const Value &Val) {
565565
auto Regs = getOrCreateVRegs(Val);
566566
if (Regs.empty())
567567
return 0;

llvm/include/llvm/CodeGen/GlobalISel/LegalizationArtifactCombiner.h

+15-15
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ class LegalizationArtifactCombiner {
3939
return false;
4040

4141
Builder.setInstr(MI);
42-
unsigned DstReg = MI.getOperand(0).getReg();
43-
unsigned SrcReg = lookThroughCopyInstrs(MI.getOperand(1).getReg());
42+
Register DstReg = MI.getOperand(0).getReg();
43+
Register SrcReg = lookThroughCopyInstrs(MI.getOperand(1).getReg());
4444

4545
// aext(trunc x) - > aext/copy/trunc x
46-
unsigned TruncSrc;
46+
Register TruncSrc;
4747
if (mi_match(SrcReg, MRI, m_GTrunc(m_Reg(TruncSrc)))) {
4848
LLVM_DEBUG(dbgs() << ".. Combine MI: " << MI;);
4949
Builder.buildAnyExtOrTrunc(DstReg, TruncSrc);
@@ -52,7 +52,7 @@ class LegalizationArtifactCombiner {
5252
}
5353

5454
// aext([asz]ext x) -> [asz]ext x
55-
unsigned ExtSrc;
55+
Register ExtSrc;
5656
MachineInstr *ExtMI;
5757
if (mi_match(SrcReg, MRI,
5858
m_all_of(m_MInstr(ExtMI), m_any_of(m_GAnyExt(m_Reg(ExtSrc)),
@@ -89,11 +89,11 @@ class LegalizationArtifactCombiner {
8989
return false;
9090

9191
Builder.setInstr(MI);
92-
unsigned DstReg = MI.getOperand(0).getReg();
93-
unsigned SrcReg = lookThroughCopyInstrs(MI.getOperand(1).getReg());
92+
Register DstReg = MI.getOperand(0).getReg();
93+
Register SrcReg = lookThroughCopyInstrs(MI.getOperand(1).getReg());
9494

9595
// zext(trunc x) - > and (aext/copy/trunc x), mask
96-
unsigned TruncSrc;
96+
Register TruncSrc;
9797
if (mi_match(SrcReg, MRI, m_GTrunc(m_Reg(TruncSrc)))) {
9898
LLT DstTy = MRI.getType(DstReg);
9999
if (isInstUnsupported({TargetOpcode::G_AND, {DstTy}}) ||
@@ -118,11 +118,11 @@ class LegalizationArtifactCombiner {
118118
return false;
119119

120120
Builder.setInstr(MI);
121-
unsigned DstReg = MI.getOperand(0).getReg();
122-
unsigned SrcReg = lookThroughCopyInstrs(MI.getOperand(1).getReg());
121+
Register DstReg = MI.getOperand(0).getReg();
122+
Register SrcReg = lookThroughCopyInstrs(MI.getOperand(1).getReg());
123123

124124
// sext(trunc x) - > ashr (shl (aext/copy/trunc x), c), c
125-
unsigned TruncSrc;
125+
Register TruncSrc;
126126
if (mi_match(SrcReg, MRI, m_GTrunc(m_Reg(TruncSrc)))) {
127127
LLT DstTy = MRI.getType(DstReg);
128128
// Guess on the RHS shift amount type, which should be re-legalized if
@@ -156,7 +156,7 @@ class LegalizationArtifactCombiner {
156156
if (MachineInstr *DefMI = getOpcodeDef(TargetOpcode::G_IMPLICIT_DEF,
157157
MI.getOperand(1).getReg(), MRI)) {
158158
Builder.setInstr(MI);
159-
unsigned DstReg = MI.getOperand(0).getReg();
159+
Register DstReg = MI.getOperand(0).getReg();
160160
LLT DstTy = MRI.getType(DstReg);
161161

162162
if (Opcode == TargetOpcode::G_ANYEXT) {
@@ -224,7 +224,7 @@ class LegalizationArtifactCombiner {
224224

225225
const unsigned NewNumDefs = NumDefs / NumMergeRegs;
226226
for (unsigned Idx = 0; Idx < NumMergeRegs; ++Idx) {
227-
SmallVector<unsigned, 2> DstRegs;
227+
SmallVector<Register, 2> DstRegs;
228228
for (unsigned j = 0, DefIdx = Idx * NewNumDefs; j < NewNumDefs;
229229
++j, ++DefIdx)
230230
DstRegs.push_back(MI.getOperand(DefIdx).getReg());
@@ -246,7 +246,7 @@ class LegalizationArtifactCombiner {
246246

247247
const unsigned NumRegs = NumMergeRegs / NumDefs;
248248
for (unsigned DefIdx = 0; DefIdx < NumDefs; ++DefIdx) {
249-
SmallVector<unsigned, 2> Regs;
249+
SmallVector<Register, 2> Regs;
250250
for (unsigned j = 0, Idx = NumRegs * DefIdx + 1; j < NumRegs;
251251
++j, ++Idx)
252252
Regs.push_back(MergeI->getOperand(Idx).getReg());
@@ -457,8 +457,8 @@ class LegalizationArtifactCombiner {
457457

458458
/// Looks through copy instructions and returns the actual
459459
/// source register.
460-
unsigned lookThroughCopyInstrs(unsigned Reg) {
461-
unsigned TmpReg;
460+
unsigned lookThroughCopyInstrs(Register Reg) {
461+
Register TmpReg;
462462
while (mi_match(Reg, MRI, m_Copy(m_Reg(TmpReg)))) {
463463
if (MRI.getType(TmpReg).isValid())
464464
Reg = TmpReg;

llvm/include/llvm/CodeGen/GlobalISel/LegalizerHelper.h

+11-11
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,14 @@ class LegalizerHelper {
141141
/// Helper function to split a wide generic register into bitwise blocks with
142142
/// the given Type (which implies the number of blocks needed). The generic
143143
/// registers created are appended to Ops, starting at bit 0 of Reg.
144-
void extractParts(unsigned Reg, LLT Ty, int NumParts,
145-
SmallVectorImpl<unsigned> &VRegs);
144+
void extractParts(Register Reg, LLT Ty, int NumParts,
145+
SmallVectorImpl<Register> &VRegs);
146146

147147
/// Version which handles irregular splits.
148-
bool extractParts(unsigned Reg, LLT RegTy, LLT MainTy,
148+
bool extractParts(Register Reg, LLT RegTy, LLT MainTy,
149149
LLT &LeftoverTy,
150-
SmallVectorImpl<unsigned> &VRegs,
151-
SmallVectorImpl<unsigned> &LeftoverVRegs);
150+
SmallVectorImpl<Register> &VRegs,
151+
SmallVectorImpl<Register> &LeftoverVRegs);
152152

153153
/// Helper function to build a wide generic register \p DstReg of type \p
154154
/// RegTy from smaller parts. This will produce a G_MERGE_VALUES,
@@ -159,16 +159,16 @@ class LegalizerHelper {
159159
///
160160
/// If \p ResultTy does not evenly break into \p PartTy sized pieces, the
161161
/// remainder must be specified with \p LeftoverRegs of type \p LeftoverTy.
162-
void insertParts(unsigned DstReg, LLT ResultTy,
163-
LLT PartTy, ArrayRef<unsigned> PartRegs,
164-
LLT LeftoverTy = LLT(), ArrayRef<unsigned> LeftoverRegs = {});
162+
void insertParts(Register DstReg, LLT ResultTy,
163+
LLT PartTy, ArrayRef<Register> PartRegs,
164+
LLT LeftoverTy = LLT(), ArrayRef<Register> LeftoverRegs = {});
165165

166166
/// Perform generic multiplication of values held in multiple registers.
167167
/// Generated instructions use only types NarrowTy and i1.
168168
/// Destination can be same or two times size of the source.
169-
void multiplyRegisters(SmallVectorImpl<unsigned> &DstRegs,
170-
ArrayRef<unsigned> Src1Regs,
171-
ArrayRef<unsigned> Src2Regs, LLT NarrowTy);
169+
void multiplyRegisters(SmallVectorImpl<Register> &DstRegs,
170+
ArrayRef<Register> Src1Regs,
171+
ArrayRef<Register> Src2Regs, LLT NarrowTy);
172172

173173
LegalizeResult fewerElementsVectorImplicitDef(MachineInstr &MI,
174174
unsigned TypeIdx, LLT NarrowTy);

llvm/include/llvm/CodeGen/GlobalISel/MIPatternMatch.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ template <typename Class> struct bind_ty {
160160
}
161161
};
162162

163-
inline bind_ty<unsigned> m_Reg(unsigned &R) { return R; }
163+
inline bind_ty<Register> m_Reg(Register &R) { return R; }
164164
inline bind_ty<MachineInstr *> m_MInstr(MachineInstr *&MI) { return MI; }
165165
inline bind_ty<LLT> m_Type(LLT &Ty) { return Ty; }
166166

llvm/include/llvm/CodeGen/GlobalISel/MachineIRBuilder.h

+12-10
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class DstOp {
6666
public:
6767
enum class DstType { Ty_LLT, Ty_Reg, Ty_RC };
6868
DstOp(unsigned R) : Reg(R), Ty(DstType::Ty_Reg) {}
69+
DstOp(Register R) : Reg(R), Ty(DstType::Ty_Reg) {}
6970
DstOp(const MachineOperand &Op) : Reg(Op.getReg()), Ty(DstType::Ty_Reg) {}
7071
DstOp(const LLT &T) : LLTTy(T), Ty(DstType::Ty_LLT) {}
7172
DstOp(const TargetRegisterClass *TRC) : RC(TRC), Ty(DstType::Ty_RC) {}
@@ -126,6 +127,7 @@ class SrcOp {
126127
public:
127128
enum class SrcType { Ty_Reg, Ty_MIB, Ty_Predicate };
128129
SrcOp(unsigned R) : Reg(R), Ty(SrcType::Ty_Reg) {}
130+
SrcOp(Register R) : Reg(R), Ty(SrcType::Ty_Reg) {}
129131
SrcOp(const MachineOperand &Op) : Reg(Op.getReg()), Ty(SrcType::Ty_Reg) {}
130132
SrcOp(const MachineInstrBuilder &MIB) : SrcMIB(MIB), Ty(SrcType::Ty_MIB) {}
131133
SrcOp(const CmpInst::Predicate P) : Pred(P), Ty(SrcType::Ty_Predicate) {}
@@ -401,7 +403,7 @@ class MachineIRBuilder {
401403
/// type as \p Op0 or \p Op0 itself.
402404
///
403405
/// \return a MachineInstrBuilder for the newly created instruction.
404-
Optional<MachineInstrBuilder> materializeGEP(unsigned &Res, unsigned Op0,
406+
Optional<MachineInstrBuilder> materializeGEP(Register &Res, Register Op0,
405407
const LLT &ValueTy,
406408
uint64_t Value);
407409

@@ -717,7 +719,7 @@ class MachineIRBuilder {
717719
/// \pre The bits defined by each Op (derived from index and scalar size) must
718720
/// not overlap.
719721
/// \pre \p Indices must be in ascending order of bit position.
720-
void buildSequence(unsigned Res, ArrayRef<unsigned> Ops,
722+
void buildSequence(Register Res, ArrayRef<Register> Ops,
721723
ArrayRef<uint64_t> Indices);
722724

723725
/// Build and insert \p Res = G_MERGE_VALUES \p Op0, ...
@@ -731,7 +733,7 @@ class MachineIRBuilder {
731733
/// \pre The type of all \p Ops registers must be identical.
732734
///
733735
/// \return a MachineInstrBuilder for the newly created instruction.
734-
MachineInstrBuilder buildMerge(const DstOp &Res, ArrayRef<unsigned> Ops);
736+
MachineInstrBuilder buildMerge(const DstOp &Res, ArrayRef<Register> Ops);
735737

736738
/// Build and insert \p Res0, ... = G_UNMERGE_VALUES \p Op
737739
///
@@ -744,7 +746,7 @@ class MachineIRBuilder {
744746
///
745747
/// \return a MachineInstrBuilder for the newly created instruction.
746748
MachineInstrBuilder buildUnmerge(ArrayRef<LLT> Res, const SrcOp &Op);
747-
MachineInstrBuilder buildUnmerge(ArrayRef<unsigned> Res, const SrcOp &Op);
749+
MachineInstrBuilder buildUnmerge(ArrayRef<Register> Res, const SrcOp &Op);
748750

749751
/// Build and insert an unmerge of \p Res sized pieces to cover \p Op
750752
MachineInstrBuilder buildUnmerge(LLT Res, const SrcOp &Op);
@@ -759,7 +761,7 @@ class MachineIRBuilder {
759761
///
760762
/// \return a MachineInstrBuilder for the newly created instruction.
761763
MachineInstrBuilder buildBuildVector(const DstOp &Res,
762-
ArrayRef<unsigned> Ops);
764+
ArrayRef<Register> Ops);
763765

764766
/// Build and insert \p Res = G_BUILD_VECTOR with \p Src replicated to fill
765767
/// the number of elements
@@ -780,7 +782,7 @@ class MachineIRBuilder {
780782
///
781783
/// \return a MachineInstrBuilder for the newly created instruction.
782784
MachineInstrBuilder buildBuildVectorTrunc(const DstOp &Res,
783-
ArrayRef<unsigned> Ops);
785+
ArrayRef<Register> Ops);
784786

785787
/// Build and insert \p Res = G_CONCAT_VECTORS \p Op0, ...
786788
///
@@ -794,10 +796,10 @@ class MachineIRBuilder {
794796
///
795797
/// \return a MachineInstrBuilder for the newly created instruction.
796798
MachineInstrBuilder buildConcatVectors(const DstOp &Res,
797-
ArrayRef<unsigned> Ops);
799+
ArrayRef<Register> Ops);
798800

799-
MachineInstrBuilder buildInsert(unsigned Res, unsigned Src,
800-
unsigned Op, unsigned Index);
801+
MachineInstrBuilder buildInsert(Register Res, Register Src,
802+
Register Op, unsigned Index);
801803

802804
/// Build and insert either a G_INTRINSIC (if \p HasSideEffects is false) or
803805
/// G_INTRINSIC_W_SIDE_EFFECTS instruction. Its first operand will be the
@@ -809,7 +811,7 @@ class MachineIRBuilder {
809811
/// \pre setBasicBlock or setMI must have been called.
810812
///
811813
/// \return a MachineInstrBuilder for the newly created instruction.
812-
MachineInstrBuilder buildIntrinsic(Intrinsic::ID ID, ArrayRef<unsigned> Res,
814+
MachineInstrBuilder buildIntrinsic(Intrinsic::ID ID, ArrayRef<Register> Res,
813815
bool HasSideEffects);
814816
MachineInstrBuilder buildIntrinsic(Intrinsic::ID ID, ArrayRef<DstOp> Res,
815817
bool HasSideEffects);

llvm/include/llvm/CodeGen/MachineOperand.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define LLVM_CODEGEN_MACHINEOPERAND_H
1515

1616
#include "llvm/ADT/DenseMap.h"
17+
#include "llvm/CodeGen/Register.h"
1718
#include "llvm/IR/Intrinsics.h"
1819
#include "llvm/Support/DataTypes.h"
1920
#include "llvm/Support/LowLevelTypeImpl.h"
@@ -345,9 +346,9 @@ class MachineOperand {
345346
//===--------------------------------------------------------------------===//
346347

347348
/// getReg - Returns the register number.
348-
unsigned getReg() const {
349+
Register getReg() const {
349350
assert(isReg() && "This is not a register operand!");
350-
return SmallContents.RegNo;
351+
return Register(SmallContents.RegNo);
351352
}
352353

353354
unsigned getSubReg() const {

llvm/include/llvm/CodeGen/MachineRegisterInfo.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -712,12 +712,12 @@ class MachineRegisterInfo {
712712

713713
/// createVirtualRegister - Create and return a new virtual register in the
714714
/// function with the specified register class.
715-
unsigned createVirtualRegister(const TargetRegisterClass *RegClass,
715+
Register createVirtualRegister(const TargetRegisterClass *RegClass,
716716
StringRef Name = "");
717717

718718
/// Create and return a new virtual register in the function with the same
719719
/// attributes as the given register.
720-
unsigned cloneVirtualRegister(unsigned VReg, StringRef Name = "");
720+
Register cloneVirtualRegister(Register VReg, StringRef Name = "");
721721

722722
/// Get the low-level type of \p Reg or LLT{} if Reg is not a generic
723723
/// (target independent) virtual register.
@@ -732,7 +732,7 @@ class MachineRegisterInfo {
732732

733733
/// Create and return a new generic virtual register with low-level
734734
/// type \p Ty.
735-
unsigned createGenericVirtualRegister(LLT Ty, StringRef Name = "");
735+
Register createGenericVirtualRegister(LLT Ty, StringRef Name = "");
736736

737737
/// Remove all types associated to virtual registers (after instruction
738738
/// selection and constraining of all generic virtual registers).

0 commit comments

Comments
 (0)