Delete loop by eraseFromParent command in llvm

* I would delete Loop. I used the following code:

cout << "begin to delete loop" << endl;
for (Loop::block_iterator bi = L->block_begin(), bi2; bi != L->block_end(); bi = bi2) {
    bi2 = bi;
    bi2++;
    BasicBlock * BB = *bi;
    for (BasicBlock::iterator ii = BB->begin(), ii2; ii != BB->end(); ii= ii2) {
        ii2 = ii;
        ii2++;
        Instruction *inst = ii;
        inst->eraseFromParent();
    }
    BB->eraseFromParent();
}

But I get the following error:

Use still stuck after destroying Def:% t1 = icmp sle i32% t0, 9 opt: /home/llvm/src/lib/VMCore/Value.cpp: 75: virtual llvm :: Value :: ~ Value (): Assertion `use_empty () && & && &" Use remains when value is destroyed! " "Failed. 0 opt 0x0848e569 Stack Reset:

What suggestions do you have to solve this problem? *

+5
source share
4 answers

The solution to your problem is as follows:

make sure to remove all references for each instruction in the loop, and then simply delete all the base blocks of the loop.

here is my sample code

for (Loop::block_iterator block = CPLoop->block_begin(), end = CPLoop->block_end(); block != end; block++) {
        BasicBlock * bb = *block;
        for (BasicBlock::iterator II = bb->begin(); II != bb->end(); ++II) {
            Instruction * insII = &(*II);
            insII->dropAllReferences();
        }
    }
    for (Loop::block_iterator block = CPLoop->block_begin(), end = CPLoop->block_end(); block != end; block++) {
        BasicBlock * bb = *block;
        bb->removeFromParent();
    }

I hope this helps

+6

, , - , LLVM, , .

SSA :

  • ,
  • ( ), .

-def def-use.

, (a.k.a. " Value" ) , .

, , :

LLVM: def-use use-def. (u) , , (inst) (, inst: add u v → add X v). , , , . ( , , llvm pass, CFG - ).

+3

inst->eraseFromParent();

Instruction* std::vector .

.

+2

"" : . IR- sth. :

  ...
  br label %loop
loop:
  <loop body>
  br i1 %exitcond, label %exit, label %loop
exit:
  ...

to sth. :

...
  br i1 0, label %loop, label %exit
loop:
  <loop body>
  br i1 %exitcond, label %exit, label %loop
exit:
  ...

You will probably run the optimization (for example, removing dead code) in your generated IR, so why bother with all references to the loop (for example, in LoopInfoor ValueMaps)?

0
source

All Articles