使用LLVM,我试图找出流控制中是否存在指令(if/switch/for)等,我必须在IR级别执行此操作.伪代码如下所示.
if cond
inst
endif
Run Code Online (Sandbox Code Playgroud)
我正在查看函数的SCC,但我不确定如何推断出流控件中是否存在指令.
以这个IR的万花筒示例为例.
declare double @foo()
declare double @bar()
define double @baz(double %x) {
entry:
%ifcond = fcmp one double %x, 0.000000e+00
%0 = call double @foo()
br i1 %ifcond, label %then, label %else
then: ; preds = %entry
%calltmp = call double @foo()
br label %ifcont
else: ; preds = %entry
%calltmp1 = call double @bar()
br label %ifcont
ifcont: ; preds = %else, %then
%iftmp = phi double [ %calltmp, %then ], [ %calltmp1, %else ]
%1 = call double @foo()
ret double %iftmp
}
Run Code Online (Sandbox Code Playgroud)
所以在上面的IR中,让我说我想找出函数foo的所有调用.所以在入口块中我们有一个调用%0和一个调用then:block我们有另一个调用foo,最后一个调用ifcont:block.
所以问题是虽然then:block中的调用属于if块生成的代码,但我如何推断?即,将执行入口块和ifcont块,但是根据条件不一定执行then:块.
有人可以给我一些指示吗?谢谢
编辑:再多想一想,Dominator树可能有助于确定这一点,但我还没有算法.