Induction analysis : A variable is called an induction variable if its value is altered within the loop by a loop-invariant value.
#Dag in compiler design examples code#
This code can be moved out of the loop by saving it to be computed only once, rather than with each iteration. Invariant code : A fragment of code that resides in the loop and computes the same value at each iteration is called a loop-invariant code. Loops can be optimized by the following techniques: It becomes necessary to optimize the loops in order to save CPU cycles and memory. Most programs run as a loop in the system. It is a useful tool that helps in optimization by help locating any unwanted loops in the program. A control flow graph depicts how the program control is being passed among the blocks. Control Flow Graphīasic blocks in a program can be represented by means of control flow graphs. If any variable is being used more than once, the register memory allocated to that variable need not be emptied unless the block finishes execution. Header statements and the statements following them form a basic block.Ī basic block does not include any header statement of any other basic block.īasic blocks are important concepts from both code generation and optimization point of view.īasic blocks play an important role in identifying variables, which are being used more than once in a single basic block. Statements that follow any branch statement.Statements that are target of any branch (conditional/unconditional).
Search header statements of all the basic blocks from where a basic block starts:
We may use the following algorithm to find the basic blocks in a program: These basic blocks do not have any jump statements among them, i.e., when the first instruction is executed, all the instructions in the same basic block will be executed in their sequence of appearance without losing the flow control of the program.Ī program can have various constructs as basic blocks, like IF-THEN-ELSE, SWITCH-CASE conditional statements and loops such as DO-WHILE, FOR, and REPEAT-UNTIL, etc. Source codes generally have a number of instructions, which are always executed in sequence and are considered as the basic blocks of the code. Machine-dependent optimizers put efforts to take maximum advantage of memory hierarchy. It involves CPU registers and may have absolute memory references rather than relative references. Machine-dependent optimization is done after the target code has been generated and when the code is transformed according to the target machine architecture. Should not only save the CPU cycles, but can be used on any processor. This code involves repeated assignment of the identifier item, which if we put this way: In this optimization, the compiler takes in the intermediate code and transforms a part of the code that does not involve any CPU registers and/or absolute memory locations. Optimization can be categorized broadly into two types : machine independent and machine dependent. While producing the target machine code, the compiler can make use of memory hierarchy and CPU registers. Optimization should itself be fast and should not delay the overall compiling process.Įfforts for an optimized code can be made at various levels of compiling the process.Īt the beginning, users can change/rearrange the code or use better algorithms to write the code.Īfter generating intermediate code, the compiler can modify the intermediate code by address calculations and improving loops. Optimization should increase the speed of the program and if possible, the program should demand less number of resources. The output code must not, in any way, change the meaning of the program. A code optimizing process must follow the three rules given below: In optimization, high-level general programming constructs are replaced by very efficient low-level programming codes. Hence, this is a DAG.Optimization is a program transformation technique, which tries to improve the code by making it consume less resources (i.e.
In the above directed graph, if we find the paths from any node, say u, we will never find a path that come back to u. for each edge in the graph, the start vertex of the edge occurs earlier in the sequence than the ending vertex of the edge. The graph formed is a directed acyclic graph.Ī DAG is always topologically ordered, i.e. If we traverse along the direction of the edges and we find that no closed loops are formed along any path, we say that there are no directed cycles. In a directed graph or a digraph, each edge is associated with a direction from a start vertex to an end vertex. In graph theory, a graph refers to a set of vertices which are connected by lines called edges. In computer science and mathematics, a directed acyclic graph (DAG) refers to a directed graph which has no directed cycles.