GATE Computer ScienceVery hardGATE CS (PYQ pattern)
In 1: i = 0; 2: L1: if i >= n goto L3; 3: if i > k goto L2; 4: t = a * b; 5: x = t + 1; 6: L2: y = a + b; 7: i = i + 1; 8: goto L1; 9: L3: return t the loop is statements 2 to 8. A statement is loop-invariant if each operand is a constant, has all its reaching definitions outside the loop, or has exactly one reaching definition, which is an invariant statement in the loop. An invariant statement s: x = … may be hoisted to a preheader only if its block dominates every loop exit and it is the only definition of x in the loop. How many statements are invariant, and how many may be hoisted?
- A. 3 invariant, 1 hoistable
- B. 3 invariant, 3 hoistable
- C. 3 invariant, 0 hoistable
- D. 2 invariant, 0 hoistable
Correct answer: C. 3 invariant, 0 hoistable
t = a * b and y = a + b use only a and b, never assigned in the loop; x = t + 1 has the single reaching definition t = a * b, which is invariant: 3 invariant statements. The only exit is the test at L1 (statement 2), and neither the block {4, 5} (skipped when i > k) nor the block at L2 dominates it. So none of the three may be hoisted; t, for instance, is used after the loop and must not be computed when the loop body never takes that branch.
More Compiler Design questions
- For the grammar S → X Y Z; X → a X | ε; Y → b Y | X; Z → c | ε, what is FIRST(S)? (ε counts as a member if S is nullable.)
- The statement r = (a + b) (b + a) − (a + b) / c is turned into a DAG (+ and commutative, − and / not), and three-address code is generated…
- For S′ → S; S → a S a | b S b | c, the canonical LR(1) states with identical cores are merged to obtain LALR(1). The LALR state whose only…
- Which of the following about intermediate representations is/are true? I. In triples, an operand that is a result of another statement is…
- Backpatching with no fall-through optimisation, numbering from 100, for: if (!(p < q)) x = 1; while (i < n) { if (a < b || c < d) k = k +…
- An LR parser for E → id | E [ E ] | ( E ) parses "( id ) [ id ]". Each shift and each reduce is one move (the final accept is not a move)…