[LLVMdev] Problems in removing a cloned instruction.

Prabhat Kumar Saraswat prabhat.saraswat at gmail.com
Wed Apr 16 03:08:49 PDT 2008


Hi all,
   I am trying to write a pass where i am creating a clone of a
function (the only difference being, the new function returns void ,
instead of any value).
I am creating a new Function Type with a void return type (rest being
the same as original function), and using this i am creating a new
function body. Then, i clone the body of the old function into new
function, but when ever i encounter a return instruction with a value,
i am creating a new instruction (return void) and removing the cloned
instruction.

The part of the code that makes these changes is below:

// This function, takes a basic block and clones it to another
identical basic block, the only changes occur for the return
instructions, which are replaced by ret void.

BasicBlock *ProgSlicer::CloneBasicBlock(const BasicBlock *BB,
                  DenseMap<const Value*, Value*> &ValueMap,
                  const char *NameSuffix, Function *F) {

        BasicBlock *NewBB = new BasicBlock("", F);
          if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix);

          bool hasCalls = false, hasDynamicAllocas = false,
hasStaticAllocas = false, isTerminal =false;

  // Loop over all instructions, and copy them over.
          for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end();
                    II != IE; ++II)
          {
            const Instruction *NwInst = cast<Instruction>((II));

            Instruction *NewInst = II->clone();

            if(ReturnInst *RI = dyn_cast<ReturnInst>(NewInst))
            {
                cerr << "\n Return Found : "  << *RI<< "\n";

                Instruction *NewRInst = new ReturnInst();  // creating
a new return instruction
                Value *v = NULL;

                if (II->hasName())
                    NewRInst->setName(II->getName()+NameSuffix);
                NewBB->getInstList().push_back(NewRInst);
                ValueMap[II] = NewRInst;                // Add
instruction map to value.
                hasCalls |= isa<CallInst>(II);
            }

                if (II->hasName())
                NewInst->setName(II->getName()+NameSuffix);
                NewBB->getInstList().push_back(NewInst);
                ValueMap[II] = NewInst;                // Add
instruction map to value.
                    hasCalls |= isa<CallInst>(II);

            if(ReturnInst *RI = dyn_cast<ReturnInst>(NewInst)) //
deleting the duplicate return
            {
                cerr << "\n INST : "  << *NewInst << " Par : " <<
*(NewInst->getParent()) << "\n";
                NewInst->eraseFromParent();
            }

        }

    return NewBB;
      }


However,functionally  pass seems to be working fine (using manual
printfs inside the code), but during the module verification pass, i
get an assert
"opt: Value.cpp:57: virtual llvm::Value::~Value(): Assertion
`use_empty() && "Uses remain when a value is destroyed!"' failed."

What could be the probem!!

Thanks alot.

Regards
Prabhat



More information about the llvm-dev mailing list