1 votes 1 votes What is the Static Single Assignment (SSA) form for the following code segment: a = b+c; c = a+x; d = b+c; d = a+x; Should we use only 1 temporary variable for c=a+x and d=a+x because both of them evaluate the same expression finally? Compiler Design made-easy-test-series compiler-design intermediate-code static-single-assignment + – Heena Jain asked Nov 14, 2018 edited Mar 4, 2019 by akash.dinkar12 Heena Jain 1.7k views answer comment Share Follow See all 22 Comments See all 22 22 Comments reply Arjun commented Nov 14, 2018 reply Follow Share nopes. That is code optimization. SSA just requires a single variable to be assigned only once. 1 votes 1 votes Heena Jain commented Nov 14, 2018 reply Follow Share But in made easy test series, answer is given as 6 total variables (because of code optimization) whereas my answer is 7. so which one is correct sir? 0 votes 0 votes `JEET commented Nov 15, 2018 reply Follow Share I would like to make it more precise that you can assign a single variable once, only in the LHS, though you can make use of it any number of time in the RHS. 0 votes 0 votes `JEET commented Nov 15, 2018 i edited by `JEET Nov 15, 2018 reply Follow Share Yes, 6 should be the answer. Here is my approach: t1 = b + c; t2 = a + x; b = b + c; a = a + x; 0 votes 0 votes Heena Jain commented Nov 15, 2018 reply Follow Share They have provided this solution: 0 votes 0 votes `JEET commented Nov 15, 2018 reply Follow Share Yes, even mine is correct. The only trick was in the question is not using the same variables in equation 1 and 2 otherwise the value may change. 0 votes 0 votes `JEET commented Nov 15, 2018 reply Follow Share Wait.. How they have used the same variable twice in the LHS? 0 votes 0 votes Heena Jain commented Nov 15, 2018 reply Follow Share Yes exactly... That is why I was confused 0 votes 0 votes `JEET commented Nov 15, 2018 reply Follow Share That is actually wrong. You cannot use the same variable on the LHS side. It looks completely wrong to me this way. Check out mine I did it in 6 variables. A small trick was not using the same variable of RHS in lines 1 and 2 as they are again changing in line 3rd and 4th. I hope u got it this way. 0 votes 0 votes Heena Jain commented Nov 15, 2018 reply Follow Share It should be t3= b+ t2 and not b=b+c because the value of c has changed to a+x 0 votes 0 votes `JEET commented Nov 15, 2018 reply Follow Share Due to this reason in the second line I have written t2 = a + x and didn't use c over here as its changing. Didn't get you? 0 votes 0 votes Heena Jain commented Nov 15, 2018 reply Follow Share Ah that is correct, but in the third line you wrote b=b+c which is wrong... It should be b=b+t2 0 votes 0 votes Heena Jain commented Nov 15, 2018 reply Follow Share According to me the SSA form should be: t1=b+c t2=t1+x t3=b+t2 t4=t1+x So total 7 variables 2 votes 2 votes `JEET commented Nov 15, 2018 reply Follow Share Why is that wrong? I didn't change my c initially and I am changing my b in b = b + c, because this way b will not be used after that. So, do you mean I can't use b in LHS and RHS simultaneously? 0 votes 0 votes Heena Jain commented Nov 15, 2018 reply Follow Share Your approach is similar to finding minimum number of registers. But that is not how it is done in SSA. 0 votes 0 votes Hemanth_13 commented Nov 15, 2018 reply Follow Share a1=b+c c1=a1+x d1=b+c1 d2=a1+x temp variables: a1 c1 d1 d2---> 4 variables: b c x----> 3 total variables = 4+3 ==> 7 2 votes 2 votes muthu kumar commented Nov 15, 2018 reply Follow Share So, 7 is the correct answer. Anybody confirm plz... 1 votes 1 votes `JEET commented Nov 20, 2018 reply Follow Share So, @Heena What's the correct answer. Is the solution given by made easy correct. I mean so can be use same temporary variable twice in the LHS? Please confirm, if found out. 0 votes 0 votes Hemanth_13 commented Nov 20, 2018 reply Follow Share @`JEET what i feel is we cannot have same value on LHS because static variables cannot be reintialized. So the answer should be 7 Correct me if I'm wrong 1 votes 1 votes Heena Jain commented Nov 21, 2018 reply Follow Share @JEET we definitely cannot have two same variables on the left.... So i feel the answer should be 7. But I'm not sure whether we can optimize the code prior to converting it to SSA form. If we can, then the answer would be 6 otherwise 7 (according to me) 0 votes 0 votes `JEET commented Nov 21, 2018 reply Follow Share A guy told me that you can't optimize in SSA and but can optimize in DAG. Do someone have any authenticate content to read this topic. I didn't found anything in the Ulman as well. 0 votes 0 votes Deepak Poonia commented Dec 7, 2018 reply Follow Share But I'm not sure whether we can optimize the code prior to converting it to SSA form. No, You can not do the optimization before converting into SSA form. Optimization is a next phase of Intermediate Representation and SSA is a Type of IR which is widely used by various compilers because it makes various types of Optimizations easy like Dead Code Elimination, Constant Propagation etc. Answer will be : $a_0 = b_0+c_0;$ $c_1 = a_0+x_0;$ $d_0 = b_0+c_1;$ $d_1 = a_0+x_0;$ 5 votes 5 votes Please log in or register to add a comment.