in Compiler Design retagged by
14,813 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

in Compiler Design retagged by
14.8k views

4 Comments

Don't skip this topic if you are praparing for other exam...like ISRO,BARC
1
1

I am from future @reena_kandari and I don’t think this will be a good idea. xD

2
2
edited by

I saw a few resources where it was stated that data flow analysis is a global code optimization technique. This is a misconception, right? Data flow analysis can be used for code optimization, but it is not a code optimization technique (at least I found no such claim in the Dragon book). If I am right, then this is the reason why option \((C)\) is false, i.e., data flow analysis can always be done (and the information derived from it can always be used for code optimization), irrespective of whether or not code optimization has been done on the intermediate code.

And, I believe option \((D)\) is wrong because the information from the front-end can always be used for optimization, for example – symbol tables (a major portion of which gets filled in via the phases constituting the front-end of the compiler), which the code optimization phase absolutely needs to access.

For option \((B)\), please check my comment here.

And, please correct me if any of the above claims seem to be objectionable.

0
0

6 Answers

42 votes
42 votes
Best answer

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
by

4 Comments

To ALL the students, I would suggest to also read the following discussion between Arjun Sir and Sandeep Uniyal.

Comment by Sandeep Uniyal : https://gateoverflow.in/410/Gate-cse-2008-question-12?show=6401#c6401

Reply by Arjun Sir : https://gateoverflow.in/410/Gate-cse-2008-question-12?show=6421#c6421

Following slide is from Stanford University :

https://web.stanford.edu/class/cs143/lectures/lecture14.pdf

7
7
for msq , we have to select both option a and b ? both will be correct ?
0
0

I think option \((B)\) isn’t quite right, the reason being that it says ‘program analysis is more accurate on intermediate code than on machine code’. But program analysis, in a strictly formal sense, is always accompanied by a proof, right? How can one proof be more accurate than the other? If a proof isn’t accurate, it isn’t a proof after all, in which case, the program analysis isn’t a program analysis as well, right? If the option were to say ‘program analysis is easier on intermediate code than on machine code’, then it’s a different story. Please correct me if I am mistaken.

0
0
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

4 Comments

Code optimization is any method of code modification to improve code quality and efficiency. A program may be optimized so that it becomes a smaller size, consumes less memory, executes more rapidly, or performs fewer input/output operations.

 

So option B must be correct

0
0
You should give better reasoning.
0
0
i thought the same thing - why A? Acc to me, the main subject here is code optimization and the answer should be related to that, not intermediate code.
0
0
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).

1 comment

B seems correct...Do you know what answer given by IIT on 2008 answer sheet..?
0
0
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