Skip to content

Commit dcbce9c

Browse files
committed
CodeGen: Avoid dereferencing end() when unconstifying iterators
Rather than doing a funny dance that relies on dereferencing end() not crashing, add some API to MachineInstrBundleIterator to get a non-const version of the iterator. llvm-svn: 278870
1 parent 8321ba5 commit dcbce9c

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

llvm/include/llvm/CodeGen/MachineInstrBundleIterator.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ class MachineInstrBundleIterator
3737
typedef typename instr_iterator::const_pointer const_pointer;
3838
typedef typename instr_iterator::const_reference const_reference;
3939

40+
private:
41+
typedef typename std::remove_const<value_type>::type nonconst_value_type;
42+
typedef ilist_node<nonconst_value_type> node_type;
43+
typedef ilist_iterator<nonconst_value_type> nonconst_instr_iterator;
44+
typedef MachineInstrBundleIterator<nonconst_value_type> nonconst_iterator;
45+
46+
public:
4047
MachineInstrBundleIterator(instr_iterator MI) : MII(MI) {}
4148

4249
MachineInstrBundleIterator(reference MI) : MII(MI) {
@@ -130,6 +137,12 @@ class MachineInstrBundleIterator
130137
}
131138

132139
instr_iterator getInstrIterator() const { return MII; }
140+
141+
nonconst_iterator getNonConstIterator() const {
142+
if (auto *N = const_cast<node_type *>(MII.getNodePtr()))
143+
return nonconst_iterator(nonconst_instr_iterator(*N));
144+
return nonconst_iterator();
145+
}
133146
};
134147

135148
} // end namespace llvm

llvm/lib/CodeGen/MachineScheduler.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,8 @@ priorNonDebug(MachineBasicBlock::const_iterator I,
251251
static MachineBasicBlock::iterator
252252
priorNonDebug(MachineBasicBlock::iterator I,
253253
MachineBasicBlock::const_iterator Beg) {
254-
return const_cast<MachineInstr*>(
255-
&*priorNonDebug(MachineBasicBlock::const_iterator(I), Beg));
254+
return priorNonDebug(MachineBasicBlock::const_iterator(I), Beg)
255+
.getNonConstIterator();
256256
}
257257

258258
/// If this iterator is a debug value, increment until reaching the End or a
@@ -271,12 +271,8 @@ nextIfDebug(MachineBasicBlock::const_iterator I,
271271
static MachineBasicBlock::iterator
272272
nextIfDebug(MachineBasicBlock::iterator I,
273273
MachineBasicBlock::const_iterator End) {
274-
// Cast the return value to nonconst MachineInstr, then cast to an
275-
// instr_iterator, which does not check for null, finally return a
276-
// bundle_iterator.
277-
return MachineBasicBlock::instr_iterator(
278-
const_cast<MachineInstr*>(
279-
&*nextIfDebug(MachineBasicBlock::const_iterator(I), End)));
274+
return nextIfDebug(MachineBasicBlock::const_iterator(I), End)
275+
.getNonConstIterator();
280276
}
281277

282278
/// Instantiate a ScheduleDAGInstrs that will be owned by the caller.

0 commit comments

Comments
 (0)