在LLVM中如何判断二个基本块(Basic Block)的支配关系

来源:互联网 发布:淘宝网店如何运营 编辑:程序博客网 时间:2024/06/05 21:01

一、支配的定义

根据维基百科 支配的概念如下:

控制流图(CFG)的一个节点 d 支配节点 n,当且仅当从开始节点(可以理解为源)到节点 n的每一条路径均要经过节点d,写作d dom n (一写作d \gg n)。根据上述定义,容易得到每个节点均控制其自身。

英文维基百科中提到的一些概念

There are a number of related concepts:

  • A node d strictly dominates a node n if d dominates n and d does not equal n.
  • The immediate dominator or idom of a node n is the unique node that strictly dominates n but does not strictly dominate any other node that strictly dominates n. Not all nodes have immediate dominators (e.g. entry nodes don't).
  • The dominance frontier of a node d is the set of all nodes n such that d dominates an immediate predecessor of n, but d does not strictly dominate n. It is the set of nodes where d's dominance stops.
  • dominator tree is a tree where each node's children are those nodes it immediately dominates. Because the immediate dominator is unique, it is a tree. The start node is the root of the tree.


二、LLVM打印出支配树的遍

我们可以使用命令
opt --help|grep dom
来查看LLVM中提供的与支配树相关的遍。
例如可以使用
opt -dot-dom test.ll
打印出test.ll的支配树。


三、判断二个基本块(Basic Block)的支配关系

在LLVM中我们可以使用如下代码判断基本块支配关系(需要包含头文件llvm/Analysis/Dominators.h)。
    Function* F;    ...    DominatorTree*T=new DominatorTree();    T->runOnFunction(*F);    T->print(errs());//打印出支配树    //打印出函数F所有模块之间的支配关系    for(Function::iterator I=F->begin(),E=F->end();I!=E;I++){        for(Function::iterator J=F->begin(),JE=F->end();J!=JE;J++){            //1表示I支配J,0表示I不支配J            errs()<<I->getName()<<" "<<J->getName()<<" "<<T->dominates(I,J)<<"\n";        }    }

如果想判断严格支配关系,可以使用函数properlyDominates来替代dominates函数。



0 0
原创粉丝点击