1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-10-19 11:02:59 +02:00

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
This commit is contained in:
Duncan P. N. Exon Smith 2016-08-16 23:34:07 +00:00
parent 84ab1f4796
commit ed41d37ba9
2 changed files with 17 additions and 8 deletions

View File

@ -37,6 +37,13 @@ public:
typedef typename instr_iterator::const_pointer const_pointer;
typedef typename instr_iterator::const_reference const_reference;
private:
typedef typename std::remove_const<value_type>::type nonconst_value_type;
typedef ilist_node<nonconst_value_type> node_type;
typedef ilist_iterator<nonconst_value_type> nonconst_instr_iterator;
typedef MachineInstrBundleIterator<nonconst_value_type> nonconst_iterator;
public:
MachineInstrBundleIterator(instr_iterator MI) : MII(MI) {}
MachineInstrBundleIterator(reference MI) : MII(MI) {
@ -130,6 +137,12 @@ public:
}
instr_iterator getInstrIterator() const { return MII; }
nonconst_iterator getNonConstIterator() const {
if (auto *N = const_cast<node_type *>(MII.getNodePtr()))
return nonconst_iterator(nonconst_instr_iterator(*N));
return nonconst_iterator();
}
};
} // end namespace llvm

View File

@ -251,8 +251,8 @@ priorNonDebug(MachineBasicBlock::const_iterator I,
static MachineBasicBlock::iterator
priorNonDebug(MachineBasicBlock::iterator I,
MachineBasicBlock::const_iterator Beg) {
return const_cast<MachineInstr*>(
&*priorNonDebug(MachineBasicBlock::const_iterator(I), Beg));
return priorNonDebug(MachineBasicBlock::const_iterator(I), Beg)
.getNonConstIterator();
}
/// If this iterator is a debug value, increment until reaching the End or a
@ -271,12 +271,8 @@ nextIfDebug(MachineBasicBlock::const_iterator I,
static MachineBasicBlock::iterator
nextIfDebug(MachineBasicBlock::iterator I,
MachineBasicBlock::const_iterator End) {
// Cast the return value to nonconst MachineInstr, then cast to an
// instr_iterator, which does not check for null, finally return a
// bundle_iterator.
return MachineBasicBlock::instr_iterator(
const_cast<MachineInstr*>(
&*nextIfDebug(MachineBasicBlock::const_iterator(I), End)));
return nextIfDebug(MachineBasicBlock::const_iterator(I), End)
.getNonConstIterator();
}
/// Instantiate a ScheduleDAGInstrs that will be owned by the caller.