The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+15 votes
1.5k views

Some code optimizations are carried out on the intermediate code because

  1. They enhance the portability of the compiler to the target processor

  2. Program analysis is more accurate on intermediate code than on machine code

  3. The information from dataflow analysis cannot otherwise be used for optimization

  4. The information from the front end cannot otherwise be used for optimization

asked in Compiler Design by Veteran (68.8k points)
retagged by | 1.5k views
need some refference here.
must be tagged as out-of-syllabus.

4 Answers

+23 votes
Best answer

Ans is (A)

Intermediate codes are machine independent codes. So, intermediate code can be used for code optimization since a given source code can be converted to target machine code.

answered by Boss (6.3k points)
edited by
Yes. And that is the most appropriate choice. There are a lot of architectures present and no one would want to do optimization for each of them.
What does the optimization has to do with the PORTABILITY of code. Why would you want to optimize the code just to make that portable !!

Your GENERATING the intermediate code itself ENHANCES the portability of the code. Optimization is something that does not matter in case you are talking of portability.

So even if you DON'T optimize your intermediate code further that is nowhere going to harm your portability. According to me optimizations on intermediate codes are easy and a wide range of optimizations are actually available for INTERMEDIATE code only because it is EASIER to optimize intermediate code rather than machine code where you need to consider the machine architecture as well.

So answer to this question is (B).
Its portability of the compiler, and not of the code.
But even if talk of portability of COMPILER, doesn't generating intermediate enhances its portability !!. Because we can club any back-end with the generic front-end generating only intermediate code !!

you please explain to me HOW WOULD THE PORTABILITY OF COMPILER BE AFFECTED IF YOU DON'T OPTIMIZE THE CODE.Will not optimizing of intermediate code make it LESS PORTABLE ??
It is not that "WE MUST optimize intermediate code". If possible all optimizations must be done on intermediate code so that the same implementation for the optimizations can be used for different front and back ends.

Say you have 5 front ends and 6 back ends. If we optimize at the front end, we need 5 implementation and if we optimize at the back end we need 6 implementations. But if we optimize at the intermediate level, we need JUST 1 implementation.

(There will always be backend dependent optimizations though which can never be avoided)
But what's wrong with option B. It also looks correct.
+8 votes

Answer: A

Option (B) is also true. But the main purpose of doing some code-optimization on intermediate code generation is to enhance the portability of the compiler to target processors. So Option A) is more suitable here. Intermediate code is machine/architecture independent code. So a compiler can optimize it without worrying about the architecture on which the code is going to execute (it may be the same or the other ). So that kind of compiler can be used by multiple different architectures. In contrast to that, suppose code optimization is done on target code, which is machine/architecture dependent, then the compiler has be specific about the optimizations on that kind of code. In this case the compiler can't be used by multiple different architectures, because the target code produced on different architectures would be different. Hence portability reduces here.

ref-http://quiz.geeksforgeeks.org/code-generation-and-optimization/

 

answered by Boss (7k points)
+1 vote
What does the optimization has to do with the PORTABILITY of code. Why would you want to optimize the code just to make that portable !!

Your GENERATING the intermediate code itself ENHANCES the portability of the front end and hence the code. Optimization is something that does not matter in case you are talking of portability.

So even if you DON'T optimize your intermediate code further that is nowhere going to harm your portability. According to me optimizations on intermediate codes are easy and a wide range of optimizations are actually available for INTERMEDIATE code only because it is EASIER to optimize intermediate code rather than machine code where you need to consider the machine architecture as well.

So answer to this question is (B).
answered by Boss (7.5k points)
B seems correct...Do you know what answer given by IIT on 2008 answer sheet..?
+1 vote

http://stackoverflow.com/questions/33184269/what-is-the-purpose-of-code-optimization-at-intermediate-phase-in-compiler

Program analysis is faster and not accurate as explained in the above link. IR code is cleaner and more easier for analysis.

First, it has a sequential representation (similar to binary code) which can be easily modified. Second, the IR preserves most of the information available in the abstract syntax tree. This includes global, local and temporary variable definitions and types. This expressiveness enables the compiler to optimize the code much more effectively. Third, it's low-level such that its instructions are primitive and only one or few consecutive IL instructions are mapped to few target ISA instructions. This helps the code generator to fulfill its purpose quickly.

So Option A) is more suitable here. Intermediate code is machine/architecture independent code. So a compiler can optimize it without worrying about the architecture on which the code is going to execute (it may be the same or the other ). 

answered by (313 points)


Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

32,470 questions
39,199 answers
109,000 comments
36,575 users