Overview
-
要为不同的程序,不同的目的量身定制数据流分析方案 application-specific data
-
为了可行性,常常要做一定精度损失over-approximation of data flows
- may analysis/over analysis: 增加假阳结果,输出结果不一定为真。outputs information that may be true
- must analysis/under analysis: 减少真阳结果。outputs information that must be true.
- both for safety of analysis
-
在nodes(basic blocks)上,要定制transfer function,将上下流信息转化为存储在该节点上的状态
-
在edges(control flows, 跳转关系)上,需要确定control-flow handling基本方案,确保上下流处理顺序
Preliminaries
注意同句IR会有In status和Out status两种情况,
In each data-flow analysis application, we associate with every program point a data-flow value that represents an abstraction of the set of all possible program states that can be observed for that point. 每个program point都会给标上一个我们感兴趣的状态
Data-flow analysis is to find a solution to a set of safe-approximation-directed constraints on the IN[s]’s and OUT[s]’s, for all statements s. 要在保证安全估计的前提下找到一个解。这些对安全估计的约束是从transfer functions和control flow中定义的。
- constraints based on semantics of statements(transfer functions)
- constraints based on the flows of control
程序可以从开始向结尾分析,也可以反过来从结尾向开始分析,甚至还能转着圈分析,只要方便就行。
控制流不但有基本块之间的,还有基本块内部的,都需要好好注意。
Reaching Definitions Analysis
问题定义:在program point p,如果一个定义d满足:d已经被定义在program point q,而且存在一条执行路径从q到p且这条路径上d没有失效(这里说只要没有新定义即可)。
潜在应用:检测undefined expr
- why for(each basic block Bentry) out[B] = 空集中特意去掉entry? 解:感觉可能只是想往通用框架上靠吧
- why this algo can finally stop? 解:因为所有程序点的状态只会增长,不会减少,所以总会增长到不能再次增长的地方停止。
为什么Reaching Definitions是may analysis? 因为交互时取了并集。具体来说,d在路径1上可能是有效的,在2上无效,同时有entry->1->3->exit和entry->2->3->4->exit都是可行的路径,实际上程序走的是entry->1->3->exit这条路,但是4这里还是认为d有效。
Live Variables Analysis
问题定义:给定程序点p,变量v,如果从p开始的某条路径会用到v,那么v就是live的,找到p点所有live variables.
潜在应用:分配寄存器
为什么Live Variables Analysis是may analysis? 因为交互时取了并集。和上面一样
Available Expressions Analysis
问题定义:给定程序点p,给定表达式expr(x op y),若expr满足:1. 从entry到p的全部paths都经过该expr的计算 2. 在最后一次计算之后,不再有组成变量(x, y)的重定义
为什么Available Expressions Analysis是must analysis? 因为交互时取了交集。具体来说,expr在路径1上可能是有效的,在2上无效,同时有entry->1->3->exit和entry->2->3->4->exit都是可行的路径,实际上程序走的是entry->2->3->4->exit这条路,但是4这里还是认为expr无效。