diff options
| author | Duncan P. N. Exon Smith <dexonsmith@apple.com> | 2016-08-16 23:34:07 +0000 |
|---|---|---|
| committer | Duncan P. N. Exon Smith <dexonsmith@apple.com> | 2016-08-16 23:34:07 +0000 |
| commit | dcbce9c3919ab6919de09f64e83fcf26d6353f0b (patch) | |
| tree | bb05e5f17626e2f088127f15a820789125b2daa6 | |
| parent | 8321ba5437f7e35f37291473fc1bafc53d27c62f (diff) | |
| download | bcm5719-llvm-dcbce9c3919ab6919de09f64e83fcf26d6353f0b.tar.gz bcm5719-llvm-dcbce9c3919ab6919de09f64e83fcf26d6353f0b.zip | |
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
| -rw-r--r-- | llvm/include/llvm/CodeGen/MachineInstrBundleIterator.h | 13 | ||||
| -rw-r--r-- | llvm/lib/CodeGen/MachineScheduler.cpp | 12 |
2 files changed, 17 insertions, 8 deletions
diff --git a/llvm/include/llvm/CodeGen/MachineInstrBundleIterator.h b/llvm/include/llvm/CodeGen/MachineInstrBundleIterator.h index 8ee3a16b36a..f493dbc5e36 100644 --- a/llvm/include/llvm/CodeGen/MachineInstrBundleIterator.h +++ b/llvm/include/llvm/CodeGen/MachineInstrBundleIterator.h @@ -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 diff --git a/llvm/lib/CodeGen/MachineScheduler.cpp b/llvm/lib/CodeGen/MachineScheduler.cpp index 01ef9d834b6..e91fa051f36 100644 --- a/llvm/lib/CodeGen/MachineScheduler.cpp +++ b/llvm/lib/CodeGen/MachineScheduler.cpp @@ -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. |

