3,782 views

2 Answers

5 votes
5 votes

A compiler can perform some optimizations based only on local information. For example, consider the following code:

x = a + b;
x = 5 * 2;

It is easy for an optimizer to recognize that:

  • The first assignment to x is a "useless" assignment, since the value computed for x is never used (and thus the first statement can be eliminated from the program).
  • The expression 5 * 2 can be computed at compile time, simplifying the second assignment statement to x = 10;

Some optimizations, however, require more "global" information. For example, consider the following code:

a = 1;
b = 2;
c = 3;
if (...) x = a + 5;
else x = b + 4;
c = x + 1;

In this example, the initial assignment to c (at line 3) is useless, and the expression x + 1 can be simplified to 7, but it is less obvious how a compiler can discover these facts since they cannot be discovered by looking only at one or two consecutive statements. A more global analysis is needed so that the compiler knows at each point in the program:

  • which variables are guaranteed to have constant values, and
  • which variables will be used before being redefined.

To discover these kinds of properties, we use dataflow analysis. Dataflow analysis is usually performed on the program's control-flow graph (CFG); the goal is to associate with each program component (each node of the CFG) information that is guaranteed to hold at that point on all executions.

2 votes
2 votes
Difference between Data Flow Analysis and Control Flow Analysis

In one case you follow the flow of control or the actions of the system. In effect you are saying this happens and then this happens and then this happens. Along the way you might scope down and say something like “and when I say this happens, what I mean is that it consists of this and then this and then this. Or the flow of control might break into parts and instead of a single thing happening you might have several that proceed in parallel.

In data flow analysis you are tracking where bits of data flows go. In effect you are saying data goes to here and then to here and then to here. You might as above scope down by saying that what I mean by the data going from here to here is that it actually goes through several other smaller moves from here to here to here. Or the data might get copied to several places at once and you would see where each of the copies goes in parallel.

I hope this helps u up!!!. :)

Related questions

1 votes
1 votes
1 answer
1
3 votes
3 votes
0 answers
2
Akshay Jindal asked Nov 1, 2015
530 views
All the links that I could find on the web were too long. Kindly refer a video or a web link that you may find easier to grasp from?
0 votes
0 votes
1 answer
4