Question

In: Computer Science

The following is the original code, (add, t1, 3, 5) (assign, a, t1) (add, t2, 3,...

The following is the original code,

(add, t1, 3, 5)

(assign, a, t1)

(add, t2, 3, 5)

(sub, t3, t2, z)

(assign, b, t3)

Constant folding is applied to give the following,

(assign, t1, 8)

(assign, a, t1)

(assign, t2, 8)

(sub, t3, t2, z)

(assign, b, t3)

After the constant folding is applied, the (assign, t2, 8) becomes redundant. What optimizations should take place in order to remove this redundancy in the constant folded code? Also show the optimized code.

Solutions

Expert Solution

Constant folding is an optimization method used in compiler design.

In this problem, we have already applied it. But (assign,t2,8) is redundant.

we can solve it by using common sub-expression elimination followed by constant propagation.

In common sub-expression elimination, we can replace a repeated expression with one expression or variable.

In this code, the assign operation is repeated as (assign,t1,8) and (assign,t2,8)

so we can replace it by (assign, temp,8) and we can use this variable temp whenever we see t1 or t2.

so we get the new code as

(assign, temp, 8)

(assign, a, temp)

(sub, t3, temp, z)

(assign, b, t3)

Now the problem of redundancy of (assign t2,8) is resolved.

But we can further optimize this using Constant propagation.

Constant propagation is the process of substituting known values in the expression. T

In this code in line (assign, temp,8) we can see that we are assigning the value 8. so now the compiler knows that temp is 8 it can simply replace temp with 8 anywhere we see temp.

so we get

(assign, temp, 8)

(assign, a, 8)

(sub, t3, 8, z)

(assign, b, t3)

The second step is not actually needed to eliminate the redundancy of assign operation. Only the sub-expression elimination is needed. But I provided extra knowledge so that you can understand what happens next.


Related Solutions

Find the data hazards in the following code segment lw $t1,0($t1) addi $t1,$t1,100 or $t2,$t3,$t1 add...
Find the data hazards in the following code segment lw $t1,0($t1) addi $t1,$t1,100 or $t2,$t3,$t1 add $a0,$a1,$t2 ori $a0,$a0,42 add $t5,$a0,$t2 Reorder the following code segment to remove the data hazards. Assume that data forwarding takes place: lw $t0,24($a0) sub $t4,$t4,$t0 sub $t8,$t8,$t3 add $t6,$t6,$t5 mul $t7,$t7,$t1 What is the CPI for the reordered sequence of instructions in the preceding problem?
Two solid bodies at initial temperatures T1 and T2, with T1 > T2, are placed in...
Two solid bodies at initial temperatures T1 and T2, with T1 > T2, are placed in thermal contact with each other. The bodies exchange heat only with eachother but not with the environment. The heat capacities C ≡ Q/∆T of each body are denoted C1 and C2, and are assumed to be positive. (a) Is there any work done on the system? What is the total heat absorbed by the system? Does the internal energy of each subsystem U1 and...
If T1 , T2 : Rn → Rm are linear maps with range(T1) = range(T2), show...
If T1 , T2 : Rn → Rm are linear maps with range(T1) = range(T2), show that there exists an invertible linear map U:Rn →Rn so that T1=U◦T2
ON PYTHON: a) Write a function named concatTuples(t1, t2) that concatenates two tuples t1 and t2...
ON PYTHON: a) Write a function named concatTuples(t1, t2) that concatenates two tuples t1 and t2 and returns the concatenated tuple. Test your function with t1 = (4, 5, 6) and t2 = (7,) What happens if t2 = 7? Note the name of the error. b) Write try-except-else-finally to handle the above tuple concatenation problem as follows: If the user inputs an integer instead of a tuple the result of the concatenation would be an empty tuple. Include an...
On Python: a) Write a function named concatTuples(t1, t2) that concatenates two tuples t1 and t2...
On Python: a) Write a function named concatTuples(t1, t2) that concatenates two tuples t1 and t2 and returns the concatenated tuple. Test your function with t1 = (4, 5, 6) and t2 = (7,) What happens if t2 = 7? Note the name of the error. b) Write try-except-else-finally to handle the above tuple concatenation problem as follows: If the user inputs an integer instead of a tuple the result of the concatenation would be an empty tuple. Include an...
Suppose T1 and T2 are iid Exp(1). What is the probability density function of T1+T2? What...
Suppose T1 and T2 are iid Exp(1). What is the probability density function of T1+T2? What is the probability that T1+T2 ≥ 3?
Starting with "m1g - T1 = m1a ", "T2 - m2g = m2a" and "(T1 -...
Starting with "m1g - T1 = m1a ", "T2 - m2g = m2a" and "(T1 - T2) R = I ?" show the derivation steps of "(m1 - m2)g = (m1 + m2 + I/R^2) a
Consider the following code: lb $t0, 1($t1) sw $t0, 0($t2) Assume the followings. • The register...
Consider the following code: lb $t0, 1($t1) sw $t0, 0($t2) Assume the followings. • The register $t1 contains the content 0x1000,0000. • The word at memory address 0x1000,0000 is 0xAABB,CCDD. What value of the word is stored at the memory address pointed to by register $t2? Show the value of the word in hexadecimal.
Consider the following history H: T2:R(Y), T1:R(X), T3:R(Y), T2:R(X), T2:W(Y), T2:Commit, T1:W(X), T1:Commit, T3:R(X), T3:Commit Assume...
Consider the following history H: T2:R(Y), T1:R(X), T3:R(Y), T2:R(X), T2:W(Y), T2:Commit, T1:W(X), T1:Commit, T3:R(X), T3:Commit Assume that each transaction is consistent. Does the final database state satisfy all integrity constraints? Explain.
Show that, for a Carnot engine operating between reservoirs at temperatures T1 and T2 (T1 >...
Show that, for a Carnot engine operating between reservoirs at temperatures T1 and T2 (T1 > T2), the thermal efficiency is given by Eta sub R = T1-T2/T1
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT