[LLVMdev] Help getting condition of branch instructions in pass

Chris Lattner sabre at nondot.org
Fri Aug 27 00:24:26 PDT 2004


On Thu, 26 Aug 2004, Michael McCracken wrote:

> Hi, this is a bit of a newbie question:
>
> I am trying to discover, given a block with a conditional and its
> successors, which condition (T/F) each successor applies to.

If you #include "llvm/Support/CFG.h", you will be provided with succ_ and
pred_iterator's.  You can grep the sourcebase to see how these are used.

Alternatively, you can get successor information like this:

BasicBlock *BB = ...
if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator()))
  if (BI->isConditional()) {
    Value *Cond = BI->getCondition();
    BasicBlock *TrueDest = BI->getSuccessor(0);
    BasicBlock *FalseDest = BI->getSuccessor(1);
    ...
  }

Using succ_iterator would look like this, which works for all basic
blocks, but does not give you any special information about branches:

for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI) {
  BasicBlock *ASuccessor = *SI;
  ...
}

-Chris

-- 
http://llvm.org/
http://nondot.org/sabre/




More information about the llvm-dev mailing list