Help getting condition of branch instructions in pass

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.

There's almost identical code in CFGPrinter.cpp, but where it gets
called in GraphWriter.h, the child_iterator type is a pretty hairy
thing, so I still don't quite understand how to get one from a
BasicBlock in my own code...
Is there a simpler way to do it without getting into all that?


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;