120 likes | 219 Views
Three-Address Implementation. Triples Quads N-tuples (my choice) Lhs = oper(op 1 , op 2 , …, op n ) Lhs = call(func, arg 1 , arg 2 , … arg n ) If condOper(op 1 , op 2 , Label) br Label Label:. Example “Source”. a = ((c-1) * b) + (-c * b). Example 3-Address. t1 = c - 1 t2 = b * t1
E N D
Three-Address Implementation • Triples • Quads • N-tuples (my choice) • Lhs = oper(op1, op2, …, opn) • Lhs = call(func, arg1, arg2, … argn) • If condOper(op1, op2, Label) • br Label • Label:
Example “Source” a = ((c-1) * b) + (-c * b)
Example 3-Address t1 = c - 1 t2 = b * t1 t3 = -c t4 = t3 * b t5 = t2 + t4 a = t5
Control Flow Graph • Nodes are Basic Blocks • Single entry, single exit • No branch exempt (possibly) at bottom • Edges represent one possible flow of execution between two basic blocks • Whole CFG represents a function
Bubble Sort begin; int A[10]; main(){ int i,j; Do 10 i = 0, 9, 1 10 A[i] = random(); Do 20 i = 1, 9, 1 Do 20 j = 1, 9, 1 20 if( A[j] > A[j+1]) swap(j); }
Bubble Sort (cont.) int swap(int i) { int temp; temp = A[i]; A[i] = A[i+1]; A[i+1] = temp; } end;
Building CFG • Starting with 3-addr code • Each leader starts a basic block which ends immediately before the next leader • ID “leaders” (heads of basic blocks) • First statement is a leader • Any statement that is the target of a conditional or unconditional goto is a leader • Any statement immediately following a goto or conditional goto is a leader • Each leader starts a basic block which ends immediately before the next leader
Live Variables – Basic Block • For each block, BB, compute • Written, read, readB4written • Written, read for each 3-addr statement in block • Initialize each BB • Live-in = readB4written, Live-out = Ø • Repeat until neither live-in, live-out change • For each BB • Live-out(BB) U= U Live-in(Successor) • Live-in(BB) U= Live-out(BB) – written(BB)
Live Variable Analysis (BB) • Live = BB->live-out • For each stmt in BB, in reverse order • Live = Live - stmt->written • stmt->live = Live • Live = Live U stmt->read
Building Register Interference Graph • Make node for each symbolic register, sr • Foreach Statement, stmt, in function • Foreach symbolic reg, srl, in stmt->live • Foreach sym reg, srw, in stmt->written • Add edge from srw to srl