[LLVMdev] replacing a global variable by a constant

Duncan Sands baldrick at free.fr
Thu Oct 6 01:05:18 PDT 2011


Hi neda 8664,

> I would delete the 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;

The instruction may be used by some other instruction outside this basic block.
I suggest that at this point you do:
   inst->replaceAllUsesWith(UndefValue::get(inst->getType()));
That way all users of the instruction will use an undefined value instead.
Thus the instruction no longer has any uses so you can freely delete it.
>              inst->eraseFromParent();
>          }
>          BB->eraseFromParent();
Similarly, predecessors of the basic block may still branch to it.  Also, the
basic block itself may be used by blockaddress constants.  Probably rather
than deleting the basic block it is easier to insert an unreachable instruction
in it and let the standard optimizers delete it.
>      }

That said, the simplest thing to do is to use the DeleteDeadBlock routine.  It
will take care of deleting the instructions in the basic block for you, and
will also delete the basic block itself.  All you have to do is ensure that
the predecessors of the basic block go somewhere else first, which you would
have to arrange in any case.

Even easier is to simply make the loop unreachable then let the usual
optimizers delete it.  You can also directly call something like SimplifyCFG
(see Local.h) to have it be deleted.

Ciao, Duncan.

>
> But I get the following error:
>
> Use still stuck around after Def is destroyed:  %t1 = icmp sle i32 %t0, 9
> opt: /home/llvm/src/lib/VMCore/Value.cpp:75: virtual llvm::Value::~Value():
> Assertion `use_empty() && "Uses remain when a value is destroyed!"' failed.
> 0  opt 0x0848e569
> Stack dump:
>
>
> What suggestions do you have for solve this problem?




More information about the llvm-dev mailing list