Problem to remove successors

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem to remove successors

Rasha Omar
Hi All,

I need to remove successors from every basic block to insert new ones

I tried this code, but it doesn't work


 void RemoveSuccessor(TerminatorInst *TI, unsigned SuccNum) {
    assert(SuccNum < TI->getNumSuccessors() &&
       "Trying to remove a nonexistant successor!");

    // If our old successor block contains any PHI nodes, remove the entry in the
    // PHI nodes that comes from this branch...
    //
    BasicBlock *BB = TI->getParent();
    TI->getSuccessor(SuccNum)->removePredecessor(BB);

    TerminatorInst *NewTI = 0;
    switch (TI->getOpcode()) {
    case Instruction::Br:
      // If this is a conditional branch... convert to unconditional branch.
      if (TI->getNumSuccessors() == 2) {
    cast<BranchInst>(TI)->setUnconditionalDest(TI->getSuccessor(1-SuccNum));
      } else {                    // Otherwise convert to a return instruction...
        Value *RetVal = 0;

    // Create a value to return... if the function doesn't return null...
    if (!(BB->getParent()->getReturnType())->isVoidTy())
      RetVal = Constant::getNullValue(BB->getParent()->getReturnType());
    // Create the return...
    NewTI = 0;
      }
      break;

    case Instruction::Invoke:    // Should convert to call
    case Instruction::Switch:    // Should remove entry
    default:
    case Instruction::Ret:       // Cannot happen, has no successors!
      assert(0 && "Unhandled terminator instruction type in RemoveSuccessor!");
      abort();
    }

    if (NewTI)   // If it's a different instruction, replace.
      ReplaceInstWithInst(TI, NewTI);
  }


Could you please help me to know where is the problem ?
Thank you

--
     Rasha Salah Omar
     Msc Student at E-JUST
     Demonestrator  at Faculty of Computers and Informatics
     Benha University

     e-mail: [hidden email]

P Please consider the environment before printing this email.


_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: Problem to remove successors

Duncan Sands
Hi,

On 31/07/13 09:05, Rasha Omar wrote:
> Hi All,
>
> I need to remove successors from every basic block to insert new ones
>
> I tried this code, but it doesn't work

take a look in include/llvm/Transforms/Utils/BasicBlockUtils.h, for example
SplitBlock.  After running SplitBlock to split off the existing terminator,
it should be easy to then transform the IR into what you want.

Ciao, Duncan.

>
>
>   void RemoveSuccessor(TerminatorInst *TI, unsigned SuccNum) {
>      assert(SuccNum < TI->getNumSuccessors() &&
>         "Trying to remove a nonexistant successor!");
>
>      // If our old successor block contains any PHI nodes, remove the entry in the
>      // PHI nodes that comes from this branch...
>      //
>      BasicBlock *BB = TI->getParent();
>      TI->getSuccessor(SuccNum)->removePredecessor(BB);
>
>      TerminatorInst *NewTI = 0;
>      switch (TI->getOpcode()) {
>      case Instruction::Br:
>        // If this is a conditional branch... convert to unconditional branch.
>        if (TI->getNumSuccessors() == 2) {
>      cast<BranchInst>(TI)->setUnconditionalDest(TI->getSuccessor(1-SuccNum));
>        } else {                    // Otherwise convert to a return instruction...
>          Value *RetVal = 0;
>
>      // Create a value to return... if the function doesn't return null...
>      if (!(BB->getParent()->getReturnType())->isVoidTy())
>        RetVal = Constant::getNullValue(BB->getParent()->getReturnType());
>      // Create the return...
>      NewTI = 0;
>        }
>        break;
>
>      case Instruction::Invoke:    // Should convert to call
>      case Instruction::Switch:    // Should remove entry
>      default:
>      case Instruction::Ret:       // Cannot happen, has no successors!
>        assert(0 && "Unhandled terminator instruction type in RemoveSuccessor!");
>        abort();
>      }
>
>      if (NewTI)   // If it's a different instruction, replace.
>        ReplaceInstWithInst(TI, NewTI);
>    }
>
>
> Could you please help me to know where is the problem ?
> Thank you
>
> --
> *Rasha Salah Omar
> Msc Student at E-JUST
> Demonestrator  at Faculty of Computers and Informatics
> Benha University*
>
> *     e-mail: [hidden email] <mailto:[hidden email]>*
>
> P* Please consider the environment before printing this email.*
>
>
>
> _______________________________________________
> LLVM Developers mailing list
> [hidden email]         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev