<div>John, thank you for your patience and good suggestion.</div>
<div> </div>
<div>I put all the instructions I want to delete into a std::vector,and replace all the use of the instrutions with an undef value, and then delete the instructions.</div>
<div> </div>
<div>For your suggestion, I successed. Thank you very much!</div>
<div> </div>
<div>Best Regards!<br><br></div>
<div class="gmail_quote">2010/4/21 John Criswell <span dir="ltr"><<a href="mailto:criswell@uiuc.edu">criswell@uiuc.edu</a>></span><br>
<blockquote style="BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px 0px 0.8ex; PADDING-LEFT: 1ex" class="gmail_quote">
<div>
<div></div>
<div class="h5">lucefe wrote:<br>
<blockquote style="BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px 0px 0.8ex; PADDING-LEFT: 1ex" class="gmail_quote">I did a simple test just now, but I alse failed.<br> I delete several ordered instructions from end to begin,<br>
but after deleting the first instruction(the last instruction of F), the program crashed.<br>My test code is below (F is a function only containing several sequential instructions):<br> for (inst_iterator inst == --inst_end(F); inst != inst_begin(F); --inst) {<br>
Instruction * i = &*inst;<br>i->eraseFromParent();<br>}<br> Best Regards!<br></blockquote><br></div></div>First, you may be invalidating the iterator i by erasing the value inside the loop. In my code, I almost always iterate through the instructions once and record them in a std::vector. I think loop through the std::vector, processing the end element and then remove it from the std::vector. It is less efficient but ensures no nasty iterator invalidation errors.<br>
<br>Second, this code does not work correctly on loops. A basic block could branch to itself, meaning that a phi node at the beginning of the basic block could use a value computed at the end of itself.<br><br>You should do the following:<br>
<br>1) For each instruction to delete, replace its use with an Undef value. When this is done for all instructions you want to delete, none of them will have any uses. You should be able to delete them then.<br><br>I can provide sample code tomorrow that illustrates how to do this (I'm busy writing a short paper tonight). In the meantime, someone may be able to show you an even better way.<br>
<br>-- John T.<br><br></blockquote></div><br>