<div dir="ltr">Thanks Bjorn! But The problem is still there.</div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Aug 8, 2018 at 2:04 AM, Björn Pettersson A <span dir="ltr"><<a href="mailto:bjorn.a.pettersson@ericsson.com" target="_blank">bjorn.a.pettersson@ericsson.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">It looks quite dangerous to erase the instruction I inside the loop when iterating over all instructions in the function.<br>
I guess it depends on how the range based iterator is implemented if that works or not.<br>
<br>
</span>I think that you, for example, can change the range based iteration<br>
<span class=""><br>
for (auto &I : instructions(F))<br>
<br>
into something like this<br>
<br>
for (inst_iterator It = inst_begin(&F), Ie = inst_end(&F); It != Ie;) {<br>
Instruction *I = &*(It++);<br>
...<br>
}<br>
<br>
That will make sure that you step the iterator before modifying the data structure you iterate over.<br>
<br>
Regards,<br>
Björn<br>
______________________________<wbr>__________<br>
From: llvm-dev <<a href="mailto:llvm-dev-bounces@lists.llvm.org">llvm-dev-bounces@lists.llvm.<wbr>org</a>> on behalf of Ahmad Nouralizadeh via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>><br>
</span>Sent: Tuesday, August 7, 2018 22:26<br>
<span class="im HOEnZb">To: <a href="mailto:paul.robinson@sony.com">paul.robinson@sony.com</a><br>
Cc: <a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a><br>
Subject: Re: [llvm-dev] Error Calling eraseFromParent()<br>
<br>
</span><div class="HOEnZb"><div class="h5">The code is really simple. But I can not the reason for the segmentation fault. I only know that the eraseFromParent() function leads to it. The whole code is:<br>
...<br>
bool runOnFunction(Function &F) override {<br>
<br>
for (auto &I : instructions(F)) {<br>
<br>
if (auto* op = dyn_cast<BinaryOperator>(&I)) {<br>
IRBuilder<> builder(op);<br>
<br>
Value* lhs = op->getOperand(0);<br>
Value* rhs = op->getOperand(1);<br>
Value* mul = builder.CreateMul(lhs, rhs);<br>
<br>
for (auto& U : op->uses()) {<br>
User* user = U.getUser();<br>
user->setOperand(U.<wbr>getOperandNo(), mul);<br>
}<br>
<br>
I.eraseFromParent();<br>
}<br>
<br>
}<br>
...<br>
And I think that the code worked well with LLVM-3.6.0 that I tested one year ago. Now, I use LLVM-6.0.0.<br>
Regards.<br>
</div></div></blockquote></div><br></div>