retagged by
14,845 views
41 votes
41 votes

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

retagged by

6 Answers

Best answer
42 votes
42 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/

edited by
27 votes
27 votes

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.

edited by
3 votes
3 votes
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).
2 votes
2 votes

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 ). 

Answer:

Related questions

37 votes
37 votes
3 answers
1
Kathleen asked Sep 12, 2014
15,653 views
An LALR(1) parser for a grammar G can have shift-reduce (S-R) conflicts if and only ifThe SLR(1) parser for G has S-R conflictsThe LR(1) parser for G has S-R conflictsThe...