GATE / CS ARCHIVE

SIMILAR QUESTIONS / COMPILER DESIGN

Rename assignments into static single assignment

2 questions solved the same way, newest first. Back to Compiler Design

How these are solved

Assign a new version to each definition and rewrite uses with the latest reaching version. Different statements/counts, same straight-line renaming procedure.

1 mark · MCQ
Report issue

Intermediate Code

Question 12, GATE 2017: Convert p=a−b; q=p*c; p=u*v; q=p+q to static single assignment form. Options: A p1=a−b;q1=p1*c;p1=u*v;q1=p1+q1. B p3=a−b;q4=p3*c;p4=u*v;q5=p4+q4. C p1=a−b;q1=p2*c;p3=u*v;q2=p4+q3. D p1=a−b;q1=p*c;p2=u*v;q2=p+q.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

1. SSA requires each defined variable version to have exactly one assignment. Subscripts need not start at 1, but every use must refer to the correct reaching definition.

2. First create p3=a−b; then q4=p3*c must use that version. The reassignment p=u*v needs a different version, p4.

3. The last sum uses new p and old q: q5=p4+q4. This is option B. A repeats definitions, C uses undefined versions, and D leaves unversioned references.

Original paper, page 7 · Answer key

1 mark · NAT
Report issue

Intermediate Code

Question 19, GATE 2016: Find the minimum total number of variable names in static single-assignment form for this straight-line code, including incoming variables:
x=u−t;
y=x*v;
x=y+w;
y=t−z;
y=x*y;
Tap to enlarge
Answer & solution

Official answer: 10.0 : 10.0

Our answer: 10

The five incoming values u, t, v, w, z each need one name. The two definitions of x and three of y need five distinct SSA names: x1=u−t; y1=x1*v; x2=y1+w; y2=t−z; y3=x2*y2. Total 5+5=10 variables, counting incoming values as requested.

In SSA, each name has exactly one definition, but it can be read many times. Hence the two reads of t share one incoming name, while the successive writes to y cannot share a name. When renaming, every use must refer to its most recent preceding definition: the final product uses x2 and y2, not the original names. There are no branches here, so no φ-functions are necessary. Five new defined names plus five distinct incoming names is the requested total.

Original paper, page 9 · Answer key