Live Variable Analysis

[ << Go Back ]



Try to implement Intraprocedural Live Variable Analysis yourself before you look at how it is done here. (You might implement a better version.) If you get stuck you can use this as a guide.


Live Variable Analysis is an any-path (may) backward flow data analysis. It is by far the simplest asnd easiest to implement. First we create the LiveVariableAnalysis class by extending the BackwardFlowAnalysis class from the Soot library. We create the constructor which takes a DirectedGraph g as an argument; then it calls the constructor of the super-class and passes it the graph, super(g), and then calls doAnalysis() to perform the actual analysis.

For both the initialization functions newInitialFlow() and entryInitialFlow() we return a new instance of the empty set. We use the ArraySparseSet implementation of the FlowSet interface for maintaining sets of live variables at various points. Since we are keeping track of local variables, all our sets are simply ArraySparseSet instances of Local().

The merge function will be set union which we can use from the corresponding implementation of ArraySparseSet. Similarly we use the set copy implementation for the copy function.



Finally for the flowThrough function, we should take the IN set, remove all writes and add all reads. We can get the writes and READS using the getUseBoxes() and getDefBoxes() functions of the node. We check whether the values received are local variables (by checking if they are instances of Local) and then add them to the writes or reads.



Running Flow Analysis as a Phase in Soot

Soot runs in phases or packs with each phase transforming the program. We need to add our data flow analysis to one of the whole program phases. For this we create a new class by extending the SceneTransformer class, and override the internalTransform function in which we get the graph of the appropriate method and create an instance of the LiveVariableAnalysis (which does all the analysis, since the constructor of the class contains the doAnalysis call). After this we can print the live variables, store them for later phases, or do anything else with them.


To execute this we need to add a new instance of the transformer we created to one of the whole program packs. We need the control flow graph for our transformer and so we cannot add it to the control graph cg pack. So we add it to the next pack-the whole jimple tranformation pack (wjtp). Then we call the Soot's main function, soot.Main.main(), with appropriate arguments. The arguments are the same as the command line arguments and are given as an array of strings.