<br><br><div class="gmail_quote">On Tue, Oct 11, 2011 at 8:08 AM, David Blaikie <span dir="ltr"><<a href="mailto:dblaikie@gmail.com">dblaikie@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<br><br><div class="gmail_quote"><div class="im">On Tue, Oct 11, 2011 at 5:55 AM, Kalle Raiskila <span dir="ltr"><<a href="mailto:kalle.raiskila@nokia.com" target="_blank">kalle.raiskila@nokia.com</a>></span> wrote:<br>
</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">

Fix a iterator out of bounds error, that triggers rarely.<br><br></div> for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I) {<div class="im"><br>
     if (I->getOpcode() == SPU::HBRA ||<br>
         I->getOpcode() == SPU::HBR_LABEL){<br>
       I=MBB.erase(I);<br>
+      if (I == MBB.end())<br>
+        break;<br>
     }<br>
   }<br>
 }<br></div></blockquote><div><br>Is it OK that this algorithm skips any element just after the one it removed? Or is it possible that two elements that satisfy the removal condition could be adjacent & should both be removed? <br>

<br>The way to make this work if that's how it should behave is:<br><br>for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ) {<br>  if (...)<br>    I = MBB.erase(I);<br>  else<br>    ++I;<br>}<br></div>

</div>
</blockquote></div><br><div>Oh, and one other thing - this is an O(N^2) algorithm for sequential containers like std::vector - the erase/remove idiom is technically the right way to go but a bit more work to implement (& may or may not be worth it, depending on the number of items in the container & the number expected to be removed, etc). The easiest way is to write your predicate as a function (which is llvm's recommended coding style anyway) and then use: MBB.erase(std::remove_if(MBB.begin(), MBB.end(), pred), MBB.end());</div>
<div><br></div><div>- David</div>