Machine-translated by Google. Questions are authored in English.
Top

Code Optimization Questions and Answers

Answer: C

Correct answer: floating point operations are not associative

\( (a+b)+c \) and \( a+(b+c) \) can differ in floating point because each addition rounds, so reassociating changes the result. Integer arithmetic is exactly associative, so the same transformation is safe there. This is why compilers require an explicit fast-math flag before reassociating floating point.

Please log in or register to report this question.

52.

State whether the following is true or false.
A compiler may freely reorder floating point additions to expose parallelism.

Answer: D

Correct answer: False

Reordering additions changes where the rounding occurs and so can change the answer, sometimes substantially in an ill-conditioned sum. A conforming compiler will not do it by default, which is precisely why flags exist to permit it - and why a program that suddenly gives different results under such a flag is behaving as documented rather than as a defect.

Please log in or register to report this question.

Answer: A

Correct answer: its value may change by means the compiler cannot see

A volatile object may be a memory-mapped device register or a location written by another thread or a signal handler, so the compiler’s reasoning about what the program does to it is incomplete. Every read and write must therefore be performed exactly as written, in order, and none may be cached in a register or elided.

Please log in or register to report this question.

Answer: B

If the compiler cannot prove two pointers are distinct it must assume they may coincide. That assumption blocks reordering, caching in registers and much else. Languages with restricted pointers give the optimiser far more freedom.

Please log in or register to report this question.

Answer: C

Assuming distinctness without proof would produce wrong code. Reporting a possible alias costs an optimisation, which is the acceptable side on which to err. Type based rules let compilers rule out some aliases without any analysis.

Please log in or register to report this question.

Answer: D

Correct answer: one transformation creating or destroying opportunities for another

Inlining exposes constants that propagation can use; propagation exposes dead code; dead code elimination may make a loop invariant. But an ordering good for one program may be poor for another, and no single sequence is best in general - which is why compilers run some passes repeatedly rather than once each.

Please log in or register to report this question.

57.

Assertion (A): Optimisation passes are often run more than once in a compiler.
Reason (R): One transformation frequently exposes opportunities that a pass which already ran would have taken.

Answer: A

Correct answer: Both A and R are true, and R is the correct explanation of A

Both are true and the reason gives the mechanism. Because transformations expose opportunities for one another, a pass that ran early may have missed work that only becomes visible later. Running the pipeline round again is the practical response to a phase ordering problem that has no optimal solution.

Please log in or register to report this question.

Answer: B

Higher levels apply more and costlier analyses. Correctness is not negotiable at any level, though a bug in an optimisation may of course surface only at the higher settings. That is why a program failing only when optimised deserves careful examination.

Please log in or register to report this question.

Answer: C

Optimisation is meant to preserve behaviour exactly. A difference in results indicates either a compiler defect or a program relying on behaviour the language leaves undefined. The latter explanation is by far the more common of the two.

Please log in or register to report this question.

Answer: D

Correct answer: undefined behaviour in the program itself

An optimiser is entitled to assume the program does not do anything the language leaves undefined - no signed overflow, no reading uninitialised storage, no aliasing violations. A program relying on what happened to work unoptimised has usually been depending on one of those, and the transformation has merely exposed it.

Please log in or register to report this question.

Loading…

Other Subjects

Tags: Code Optimization Questions and Answers || Code Optimization MCQ Questions and Answers || Code Optimization GK Questions and Answers || Code Optimization GK MCQ Questions || Code Optimization Multiple Choice Questions and Answers || Code Optimization GK || GK on Code Optimization || Compiler Design Questions and Answers || Compiler Design MCQ Questions and Answers || Compiler Design GK Questions and Answers || GK on Compiler Design