0% found this document useful (0 votes)
54 views7 pages

CD Assignment 4

Code generation is the final phase of compilation where the compiler generates object code in a lower-level language like assembly code from the intermediate representation. Some key considerations for code generators include the target language, intermediate representation type, instruction selection, register allocation, and instruction ordering. Control flow analysis helps understand program flow and structure for optimizations. Data flow analysis determines how data is defined and used for optimizations. Code transformations aim to improve efficiency by consuming fewer resources while maintaining program semantics.

Uploaded by

Sagar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views7 pages

CD Assignment 4

Code generation is the final phase of compilation where the compiler generates object code in a lower-level language like assembly code from the intermediate representation. Some key considerations for code generators include the target language, intermediate representation type, instruction selection, register allocation, and instruction ordering. Control flow analysis helps understand program flow and structure for optimizations. Data flow analysis determines how data is defined and used for optimizations. Code transformations aim to improve efficiency by consuming fewer resources while maintaining program semantics.

Uploaded by

Sagar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Compiler Design Assignment no:-4

SAGAR KUMAR
Computer Science &Engg
RollNo-1816610909(3rd Year).
Q1: What is code generation? Discuss the design
issues of code generation.
A: Code generation can be considered as the final phase of
compilation. Through post code generation, optimization process
can be applied on the code, but that can be seen as a part of code
generation phase itself. The code generated by the compiler is an
object code of some lowerlevelprogramming language, for example,
assembly language. We have seen that the source code written in a
higher-level language is transformed
into a lower-level language that results in a lower-level object code,
which should have the following minimum properties:
• It should carry the exact meaning of the source code.
• It should be efficient in terms of CPU usage and memory
management.
A code generator is expected to have an understanding of the
target machine’s runtime environment and its instruction set. The
code generator should take the following things into consideration
to generate the code:
• Target language: The code generator has to be aware of the
nature of the target language for which the code is to be
transformed. That language may facilitate some machine-specific
instructions to help the compiler generate the code in a more
convenient way. The target machine can have either CISC or RISC
processor architecture.
• IR Type: Intermediate representation has various forms. It can be
in
Abstract Syntax Tree (AST) structure, Reverse Polish Notation, or 3-
address code.
• Selection of instruction: The code generator takes Intermediate
Representation as input and converts (maps) it into target
machine’s instruction set. One representation can have many ways
(instructions)
Compiler Design Assignment no:-4
to convert it, so it becomes the responsibility of the code generator
to choose the appropriate instructions wisely.
• Register allocation: A program has a number of values to be
maintained during the execution. The target machine’s
architecture may
not allow all of the values to be kept in the CPU memory or
registers. Code generator decides what values to keep in the
registers. Also, it decides the registers to be used to keep these
values.
• Ordering of instructions: At last, the code generator decides the
order in which the instruction will be executed.
It creates schedules for instructions to execute them.

Question No-02: Write short notes on:-


(1) Induction variable elimination
(2) Loop unrolling and loop jamming
Answer: Induction variable elimination: An induction variable is a
variable whose value on each loop iteration is a linear function of
the iteration index. When such variables and the expressions they
compute are found, often the variable itself can be eliminated or a
strength reduction can be performed.
In many programs, IVs can be redundant. For instance, a common
programming idiom is to introduce a variable only to use as a loop
guard (such as i in the following program).
int max = 10;
int result = 0;
for (int i = 0; i < max; i++)
{
result += 2;
}
return result;
In this example, we can eliminate the i variable by replacing its uses
with another basic induction variable result to get:
int max = 10;
int result = 0;
for (; result < max*2; result+=2)
{
Compiler Design Assignment no:-4
}
return result;
This obviously removes extraneous code by combining the "loop
counting" part of the loop with the actual work that it's doing.
Loop unrolling and loop jamming: Loop unrolling is a loop
transformation technique that helps to optimize the execution time
of a
program. We basically remove or reduce iterations. Loop unrolling
increases the program’s speed by eliminating loop control
instruction and loop test instructions.
Example:
Initial code:
for (int i=0; i<5; i++)
printf("Akash\n");
Optimized code:
printf("Akash\n");
printf("Akash\n");
printf("Akash\n");
printf("Akash\n");
printf("Akash\n");
Advantages:
• Increases program efficiency.
• Reduces loop overhead.
• If statements in loop are not dependent on each other, they can be
executed in parallel.
Disadvantages:
• Increased program code size, which can be undesirable.
• Possible increased usage of register in a single iteration to store
temporary variables which may reduce performance.
• Apart from very small and simple codes, unrolled loops that
contain branches are even slower than recursions.
Loop jamming is the combining the two or more loops in a single
loop. It reduces the time taken to compile the many number of
loops.
Example:
Initial Code:
for(int i=0; i<5; i++)
Compiler Design Assignment no:-4
a = i + 5;
for(int i=0; i<5; i++)
b = i + 10;
Optimized code:
for(int i=0; i<5; i++)
{
a = i + 5;
b = i + 10;
}

Question No-03: Explain the following in the


organization of code optimization:
❖Contol flow analysis
❖ Data flow analysis
❖ Transformations
Answer:
❖Contol flow analysis:
1. Control-flow analysis (CFA) helps us to understand the
structure of control-flow graphs (CFG)
2. To determine the loop structure of CFGs
3. Formulation of conditions for code motion use dominator
information, which is obtained by CFA
4. Construction of the static single assignment form (SSA)
requires dominance frontier information from CFA
5. It is possible to use interval structure obtained from CFA to
carry out data-flow analysis
6. Finding Control dependence, which is needed in
parallelization, requires CFA
7. Basic blocks in a program can be represented by means of
control flow graphs. A control flow graph depicts how the
program control is being passed among the blocks. It is a
useful tool that helps in optimization by help locating any
unwanted loops in the program.
Compiler Design Assignment no:-4

❖ Data flow analysis: It is the analysis of flow of data in control


flow graph, i.e., the analysis that determines the information
regarding the definition and use of data in program. With the help
of this analysis optimization can be done. In general, its process in
which values are computed using data flow analysis. The data flow
property represents information which can be used for
optimization.
Basic Terminologies –
1. Definition Point: a point in a program containing some
definition.
2. Reference Point: a point in a program containing a reference
to a data item.
3. Evaluation Point: a point in a program containing evaluation
of expression.
4. Properties of data flow:-
5. Available Expression – A expression is said to be available at a
program point x iff along paths its reaching to x. A Expression
is available at its evaluation point. A expression a+b is said to
be available if none of the operands gets modified before their
use.
Compiler Design Assignment no:-4
6. Example –
7. Reaching Definition – A definition D is reaches a point x if
there is path from D to x in which D is not killed, i.e., not
redefined.
8. Example –
9. Live variable – A variable is said to be live at some point p if
from p to end the variable is used before it is redefined else it
becomes dead.
Example –
10.Busy Expression – An expression is busy along a path iff its
evaluation exists along that path and none of its operand definition
exists before its evaluation along the path.

❖ Transformations: The code optimization in the synthesis phase is


a program transformation technique, which tries to improve the
intermediate code by making it consume fewer resources (i.e. CPU,
Memory) so that faster-running machine code will result.
Compiler optimizing process should meet the following objectives :
1. The optimization must be correct, it must not, in any way,
change the meaning of the program.
2. Optimization should increase the speed and performance of
the program.
3. The compilation time must be kept reasonable.
4. The optimization process should not delay the overall
compiling process.
5. Optimization of the code is often performed at the end of the
development stage since it reduces readability and adds code
that is used to increase the performance.
6. Types of Code Optimization –The optimization process can be
broadly classified into two types:
7. Machine Independent Optimization – This code optimization
phase attempts to improve the intermediate code to get a
better target code as the output. The part of the intermediate
code which is transformed here does not involve any CPU
registers or absolute memory locations.
8. Machine Dependent Optimization – Machine-dependent
optimization is done after the target code has been generated
Compiler Design Assignment no:-4
and when the code is transformed according to the target
machine architecture. It involves CPU registers and may have
absolute memory references rather than relative references.
Machine-dependent optimizers put efforts to take maximum
advantage of the memory hierarchy.

You might also like