0

I want to use LLVM to analyze if a basic block is affected by a control flow of an if(i.e., br instruction). "A basic block BB is NOT affected by br" means that no matter which of two blocks the br goes to BB will be executed for sure. I use an example to briefly show what I want:

enter image description here

My current rule to determine if a basic block BB is affected is (if true, affected.)

¬(postDominate(BB, BranchInst->leftBB) && postDominate(BB, BranchInst->rightBB))

Since I can not exhaustively test the rule above to all possible CFGs, I want to know if this rule is sound and complete.

Thanks!

Further question

I am also confused if I should use dominate rather than postDominate like (I know the difference between post-dominance and dominance, but which should I use? Both rules seems work in this example, but I am not sure which one will/won't work in other cases):

Dominate(BranchInst->leftBB, BB) || Dominate(BranchInst->rightBB, BB)
markalex
  • 8,623
  • 2
  • 7
  • 32
Tim He
  • 350
  • 2
  • 13

2 Answers2

2

A block Y is control dependent on block X if and only if Y postdominates at least one but not all successors of X.

llvm::ReverseIDFCalculator in llvm/Analysis/IteratedDominanceFrontier.h will calculate the post-dominance frontier for you, which is exactly what you need. The "iterated" part isn't relevant to your use case, ignore the setLiveInBlocks() method.

I have not tested this at all, but I expect that something like this should do the trick:

  // PDT is the llvm::PostDominatorTree
  SmallPtrSet<BasicBlock *, 1> BBSet{block_with_branch};
  SmallVector<BasicBlock *, 32> IDFBlocks;
  ReverseIDFCalculator IDFs(PDT);
  IDFs.setDefiningBlocks(BBSet);
  IDFs.calculate(IDFBlocks);
Nick Lewycky
  • 1,182
  • 6
  • 14
  • Thanks for the answer. For the example above, the code would produce: `IDFBlocks` is empty, when `BBSet`=`entry` (the block contains the interested branch). But iterating all blocks and finding when `IDFBlocks` of the block contains `entry` works. – Tim He Apr 29 '22 at 10:10
  • control dependent seems a little subtle. Since by definition, the block `if.then1` post-dominates neither successor (i.e., `if.then` and `if.end3`) of the branch, but it may still be affected by the branch (only when `config == true`). So I think maybe I can relax the definition to "if Y post dominates not all successors of X." – Tim He Apr 29 '22 at 10:21
0

The relation control dependent is transitive. Applying the definition to all the control-dependent-impacted block(s) iteratively is the right way.

Tim He
  • 350
  • 2
  • 13