[llvm] r278870 - CodeGen: Avoid dereferencing end() when unconstifying iterators

Duncan P. N. Exon Smith via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 16 16:34:08 PDT 2016


Author: dexonsmith
Date: Tue Aug 16 18:34:07 2016
New Revision: 278870

URL: http://llvm.org/viewvc/llvm-project?rev=278870&view=rev
Log:
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.

Modified:
    llvm/trunk/include/llvm/CodeGen/MachineInstrBundleIterator.h
    llvm/trunk/lib/CodeGen/MachineScheduler.cpp

Modified: llvm/trunk/include/llvm/CodeGen/MachineInstrBundleIterator.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/CodeGen/MachineInstrBundleIterator.h?rev=278870&r1=278869&r2=278870&view=diff
==============================================================================
--- llvm/trunk/include/llvm/CodeGen/MachineInstrBundleIterator.h (original)
+++ llvm/trunk/include/llvm/CodeGen/MachineInstrBundleIterator.h Tue Aug 16 18:34:07 2016
@@ -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

Modified: llvm/trunk/lib/CodeGen/MachineScheduler.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/MachineScheduler.cpp?rev=278870&r1=278869&r2=278870&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/MachineScheduler.cpp (original)
+++ llvm/trunk/lib/CodeGen/MachineScheduler.cpp Tue Aug 16 18:34:07 2016
@@ -251,8 +251,8 @@ priorNonDebug(MachineBasicBlock::const_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_ite
 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.




More information about the llvm-commits mailing list