262 views
1 1 vote

Consider the following three-address code sequence used for a loop:

i = 0
L1: t1 = i * 4
t2 = base + t1
val = load t2
i = i + 1
if i < 100 goto L1

Which optimization technique would most effectively transform this code to reduce the number of multiplications inside the loop, and what would be the resulting change to line $2$?

  1. Constant Folding; $\verb|t1 = 0|$
     
  2. Dead Code Elimination; Line $2$ is removed entirely.
     
  3. Strength Reduction; $\verb|t1 = t1 + 4|$ (with initialization $\verb|t1 = 0|$ before the loop). 
     
  4. Loop Unrolling; The loop body is copied $100$ times to remove the branch.

1 Answer

0 0 votes
Strength Reduction replaces a "heavy" operation like multiplication inside a loop with a "lighter" one like addition. By initializing $\verb|t1|$ to $0$ and adding $4$ in each iteration, the compiler eliminates the costly $\verb|i*4|$ calculation.
Answer:
Position:
Show:

Related questions

1 1 vote
1 1 answer
260
260 views
GO Classes asked Jan 23
260 views
An $\mathrm{LR(1)}$ parser is being constructed for a grammar. If a particular state in the corresponding $\mathrm{LR}(1)$ canonical collection contains the item $[A \rig...
1 1 vote
2 2 answers
255
255 views
GO Classes asked Jan 23
255 views
In a compiler's optimization phase, a Directed Acyclic Graph (DAG) is often used instead of a standard syntax tree to represent expressions. Consider the expression: $a=(...
0 0 votes
1 1 answer
281
281 views
GO Classes asked Jan 23
281 views
Consider a syntax tree for the expression $a+b * c-d / e$. If the expression is evaluated using a post-order traversal of this tree, and the values at the leaves are $a=2...
1 1 vote
1 1 answer
239
239 views
GO Classes asked Jan 23
239 views
Consider the expression tree shown below. Each leaf represents a numerical value, which can be chosen from the set $\{-1,1\}$. Over all possible choices of the values at ...