A CIL Tutorial - Department of Computer Science - ETH Zürich
A CIL Tutorial - Department of Computer Science - ETH Zürich
A CIL Tutorial - Department of Computer Science - ETH Zürich
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Chapter 3<br />
Dataow Analysis<br />
Dataow Analyses can be used to calculate an abstraction <strong>of</strong> the program state at each program<br />
location. Dataow analysis is a ow-sensitive analysis, meaning that, unlike ow-insensitive analysis,<br />
the order <strong>of</strong> the statements in the program is taken into account when calculating the abstraction<br />
<strong>of</strong> the state.<br />
Traditionally, compiler writers use dataow analyses extensively in performing compiler optimizations.<br />
For example, an available expressions dataow analysis can eliminate redundant computation.<br />
Most compiler textbooks discuss dataow analysis in detail [2, 3]. A more formal treatment<br />
can be found in the textbook by Nielson et al. [4].<br />
<strong>CIL</strong> has a number <strong>of</strong> useful dataow analyses built-in, which you can nd in the directory<br />
cil/src/ext. They include a liveness analysis, an available expressions analysis, a reaching denitions<br />
analysis, and others. The utility <strong>of</strong> even a simple liveness analysis cannot be underestimated.<br />
Consider the following code snippet:<br />
int *x = calloc(n, size<strong>of</strong> int);<br />
f(x);<br />
// x is never mentioned again<br />
Since x does not escape the function, and since it is not live after the call to f, even a simple liveness<br />
analysis can conclude that f is taking ownership <strong>of</strong> the memory allocated on the rst line.<br />
In this tutorial we explore <strong>CIL</strong>'s dataow analysis features. In particular, we employ the following<br />
functor from <strong>CIL</strong>'s Dataflow module found in cil/src/ext/dataflow.ml:<br />
module ForwardsDataFlow (T : ForwardsTransfer) = struct ...,<br />
which yields a module containing only a function called compute, which performs the dataow analysis<br />
using the standard worklist algorithm. The functor is documented in detail in cil/src/ext/dataflow.mli<br />
and in the <strong>CIL</strong> documentation.<br />
In order to use this functor, we must create an OCaml module implementing the ForwardsTransfer<br />
signature, which denes operations over the abstract state along with the transfer functions for the<br />
21