Skip to content

Commit 64f552c

Browse files
authored
[NFC][LLVM][CodeGen] Refactor MachineInstr operand accessors (#137261)
- Change MachineInstr operand accessors to use `ArrayRef` internally to slice the operand array into sub-arrays. - Minor: remove unnecessary {} on `MachineInstrBuilder::add`.
1 parent 8023375 commit 64f552c

File tree

3 files changed

+71
-74
lines changed

3 files changed

+71
-74
lines changed

llvm/include/llvm/CodeGen/MachineInstr.h

Lines changed: 66 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#ifndef LLVM_CODEGEN_MACHINEINSTR_H
1616
#define LLVM_CODEGEN_MACHINEINSTR_H
1717

18+
#include "llvm/ADT/ArrayRef.h"
1819
#include "llvm/ADT/DenseMapInfo.h"
1920
#include "llvm/ADT/PointerSumType.h"
2021
#include "llvm/ADT/ilist.h"
@@ -43,7 +44,6 @@ class Instruction;
4344
class MDNode;
4445
class AAResults;
4546
class BatchAAResults;
46-
template <typename T> class ArrayRef;
4747
class DIExpression;
4848
class DILocalVariable;
4949
class LiveRegUnits;
@@ -340,6 +340,13 @@ class MachineInstr
340340
return Op.isReg() && Op.isUse();
341341
}
342342

343+
MutableArrayRef<MachineOperand> operands_impl() {
344+
return {Operands, NumOperands};
345+
}
346+
ArrayRef<MachineOperand> operands_impl() const {
347+
return {Operands, NumOperands};
348+
}
349+
343350
public:
344351
MachineInstr(const MachineInstr &) = delete;
345352
MachineInstr &operator=(const MachineInstr &) = delete;
@@ -580,18 +587,12 @@ class MachineInstr
580587
unsigned getNumOperands() const { return NumOperands; }
581588

582589
/// Returns the total number of operands which are debug locations.
583-
unsigned getNumDebugOperands() const {
584-
return std::distance(debug_operands().begin(), debug_operands().end());
585-
}
590+
unsigned getNumDebugOperands() const { return size(debug_operands()); }
586591

587-
const MachineOperand& getOperand(unsigned i) const {
588-
assert(i < getNumOperands() && "getOperand() out of range!");
589-
return Operands[i];
590-
}
591-
MachineOperand& getOperand(unsigned i) {
592-
assert(i < getNumOperands() && "getOperand() out of range!");
593-
return Operands[i];
592+
const MachineOperand &getOperand(unsigned i) const {
593+
return operands_impl()[i];
594594
}
595+
MachineOperand &getOperand(unsigned i) { return operands_impl()[i]; }
595596

596597
MachineOperand &getDebugOperand(unsigned Index) {
597598
assert(Index < getNumDebugOperands() && "getDebugOperand() out of range!");
@@ -667,101 +668,100 @@ class MachineInstr
667668
unsigned getNumExplicitDefs() const;
668669

669670
/// iterator/begin/end - Iterate over all operands of a machine instruction.
671+
672+
// The operands must always be in the following order:
673+
// - explicit reg defs,
674+
// - other explicit operands (reg uses, immediates, etc.),
675+
// - implicit reg defs
676+
// - implicit reg uses
670677
using mop_iterator = MachineOperand *;
671678
using const_mop_iterator = const MachineOperand *;
672679

680+
using mop_range = iterator_range<mop_iterator>;
681+
using const_mop_range = iterator_range<const_mop_iterator>;
682+
673683
mop_iterator operands_begin() { return Operands; }
674684
mop_iterator operands_end() { return Operands + NumOperands; }
675685

676686
const_mop_iterator operands_begin() const { return Operands; }
677687
const_mop_iterator operands_end() const { return Operands + NumOperands; }
678688

679-
iterator_range<mop_iterator> operands() {
680-
return make_range(operands_begin(), operands_end());
681-
}
682-
iterator_range<const_mop_iterator> operands() const {
683-
return make_range(operands_begin(), operands_end());
684-
}
685-
iterator_range<mop_iterator> explicit_operands() {
686-
return make_range(operands_begin(),
687-
operands_begin() + getNumExplicitOperands());
689+
mop_range operands() { return operands_impl(); }
690+
const_mop_range operands() const { return operands_impl(); }
691+
692+
mop_range explicit_operands() {
693+
return operands_impl().take_front(getNumExplicitOperands());
688694
}
689-
iterator_range<const_mop_iterator> explicit_operands() const {
690-
return make_range(operands_begin(),
691-
operands_begin() + getNumExplicitOperands());
695+
const_mop_range explicit_operands() const {
696+
return operands_impl().take_front(getNumExplicitOperands());
692697
}
693-
iterator_range<mop_iterator> implicit_operands() {
694-
return make_range(explicit_operands().end(), operands_end());
698+
mop_range implicit_operands() {
699+
return operands_impl().drop_front(getNumExplicitOperands());
695700
}
696-
iterator_range<const_mop_iterator> implicit_operands() const {
697-
return make_range(explicit_operands().end(), operands_end());
701+
const_mop_range implicit_operands() const {
702+
return operands_impl().drop_front(getNumExplicitOperands());
698703
}
699-
/// Returns a range over all operands that are used to determine the variable
704+
705+
/// Returns all operands that are used to determine the variable
700706
/// location for this DBG_VALUE instruction.
701-
iterator_range<mop_iterator> debug_operands() {
702-
assert((isDebugValueLike()) && "Must be a debug value instruction.");
703-
return isNonListDebugValue()
704-
? make_range(operands_begin(), operands_begin() + 1)
705-
: make_range(operands_begin() + 2, operands_end());
707+
mop_range debug_operands() {
708+
assert(isDebugValueLike() && "Must be a debug value instruction.");
709+
return isNonListDebugValue() ? operands_impl().take_front(1)
710+
: operands_impl().drop_front(2);
706711
}
707712
/// \copydoc debug_operands()
708-
iterator_range<const_mop_iterator> debug_operands() const {
709-
assert((isDebugValueLike()) && "Must be a debug value instruction.");
710-
return isNonListDebugValue()
711-
? make_range(operands_begin(), operands_begin() + 1)
712-
: make_range(operands_begin() + 2, operands_end());
713+
const_mop_range debug_operands() const {
714+
assert(isDebugValueLike() && "Must be a debug value instruction.");
715+
return isNonListDebugValue() ? operands_impl().take_front(1)
716+
: operands_impl().drop_front(2);
713717
}
714-
/// Returns a range over all explicit operands that are register definitions.
718+
/// Returns all explicit operands that are register definitions.
715719
/// Implicit definition are not included!
716-
iterator_range<mop_iterator> defs() {
717-
return make_range(operands_begin(),
718-
operands_begin() + getNumExplicitDefs());
719-
}
720+
mop_range defs() { return operands_impl().take_front(getNumExplicitDefs()); }
720721
/// \copydoc defs()
721-
iterator_range<const_mop_iterator> defs() const {
722-
return make_range(operands_begin(),
723-
operands_begin() + getNumExplicitDefs());
722+
const_mop_range defs() const {
723+
return operands_impl().take_front(getNumExplicitDefs());
724724
}
725-
/// Returns a range that includes all operands which may be register uses.
725+
/// Returns all operands which may be register uses.
726726
/// This may include unrelated operands which are not register uses.
727-
iterator_range<mop_iterator> uses() {
728-
return make_range(operands_begin() + getNumExplicitDefs(), operands_end());
729-
}
727+
mop_range uses() { return operands_impl().drop_front(getNumExplicitDefs()); }
730728
/// \copydoc uses()
731-
iterator_range<const_mop_iterator> uses() const {
732-
return make_range(operands_begin() + getNumExplicitDefs(), operands_end());
729+
const_mop_range uses() const {
730+
return operands_impl().drop_front(getNumExplicitDefs());
733731
}
734-
iterator_range<mop_iterator> explicit_uses() {
735-
return make_range(operands_begin() + getNumExplicitDefs(),
736-
operands_begin() + getNumExplicitOperands());
732+
mop_range explicit_uses() {
733+
return operands_impl()
734+
.take_front(getNumExplicitOperands())
735+
.drop_front(getNumExplicitDefs());
737736
}
738-
iterator_range<const_mop_iterator> explicit_uses() const {
739-
return make_range(operands_begin() + getNumExplicitDefs(),
740-
operands_begin() + getNumExplicitOperands());
737+
const_mop_range explicit_uses() const {
738+
return operands_impl()
739+
.take_front(getNumExplicitOperands())
740+
.drop_front(getNumExplicitDefs());
741741
}
742742

743-
using filtered_mop_iterator =
744-
filter_iterator<mop_iterator, bool (*)(const MachineOperand &)>;
745-
using filtered_const_mop_iterator =
746-
filter_iterator<const_mop_iterator, bool (*)(const MachineOperand &)>;
743+
using filtered_mop_range = iterator_range<
744+
filter_iterator<mop_iterator, bool (*)(const MachineOperand &)>>;
745+
using filtered_const_mop_range = iterator_range<
746+
filter_iterator<const_mop_iterator, bool (*)(const MachineOperand &)>>;
747747

748748
/// Returns an iterator range over all operands that are (explicit or
749749
/// implicit) register defs.
750-
iterator_range<filtered_mop_iterator> all_defs() {
750+
filtered_mop_range all_defs() {
751751
return make_filter_range(operands(), opIsRegDef);
752752
}
753753
/// \copydoc all_defs()
754-
iterator_range<filtered_const_mop_iterator> all_defs() const {
754+
filtered_const_mop_range all_defs() const {
755755
return make_filter_range(operands(), opIsRegDef);
756756
}
757757

758758
/// Returns an iterator range over all operands that are (explicit or
759759
/// implicit) register uses.
760-
iterator_range<filtered_mop_iterator> all_uses() {
760+
filtered_mop_range all_uses() {
761761
return make_filter_range(uses(), opIsRegUse);
762762
}
763763
/// \copydoc all_uses()
764-
iterator_range<filtered_const_mop_iterator> all_uses() const {
764+
filtered_const_mop_range all_uses() const {
765765
return make_filter_range(uses(), opIsRegUse);
766766
}
767767

llvm/include/llvm/CodeGen/MachineInstrBuilder.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,8 @@ class MachineInstrBuilder {
229229
}
230230

231231
const MachineInstrBuilder &add(ArrayRef<MachineOperand> MOs) const {
232-
for (const MachineOperand &MO : MOs) {
232+
for (const MachineOperand &MO : MOs)
233233
MI->addOperand(*MF, MO);
234-
}
235234
return *this;
236235
}
237236

llvm/lib/CodeGen/MachineInstr.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -821,8 +821,7 @@ unsigned MachineInstr::getNumExplicitOperands() const {
821821
if (!MCID->isVariadic())
822822
return NumOperands;
823823

824-
for (unsigned I = NumOperands, E = getNumOperands(); I != E; ++I) {
825-
const MachineOperand &MO = getOperand(I);
824+
for (const MachineOperand &MO : operands_impl().drop_front(NumOperands)) {
826825
// The operands must always be in the following order:
827826
// - explicit reg defs,
828827
// - other explicit operands (reg uses, immediates, etc.),
@@ -840,8 +839,7 @@ unsigned MachineInstr::getNumExplicitDefs() const {
840839
if (!MCID->isVariadic())
841840
return NumDefs;
842841

843-
for (unsigned I = NumDefs, E = getNumOperands(); I != E; ++I) {
844-
const MachineOperand &MO = getOperand(I);
842+
for (const MachineOperand &MO : operands_impl().drop_front(NumDefs)) {
845843
if (!MO.isReg() || !MO.isDef() || MO.isImplicit())
846844
break;
847845
++NumDefs;
@@ -1196,9 +1194,9 @@ void MachineInstr::tieOperands(unsigned DefIdx, unsigned UseIdx) {
11961194
assert(!DefMO.isTied() && "Def is already tied to another use");
11971195
assert(!UseMO.isTied() && "Use is already tied to another def");
11981196

1199-
if (DefIdx < TiedMax)
1197+
if (DefIdx < TiedMax) {
12001198
UseMO.TiedTo = DefIdx + 1;
1201-
else {
1199+
} else {
12021200
// Inline asm can use the group descriptors to find tied operands,
12031201
// statepoint tied operands are trivial to match (1-1 reg def with reg use),
12041202
// but on normal instruction, the tied def must be within the first TiedMax

0 commit comments

Comments
 (0)