Docsity

Solutions for Homework 1 - Compiler Construction | EECS 483, Assignments of Electrical and Electronics Engineering

Toggle sidebar
bg1
EECS 483 – Homework 1 Answers
Fall 2003
1.) Given the following segment of code, use acyclic control flow optimizations to optimize the
code as much as possible. ASSUME that you don’t apply unreachable code elimination in this
example since you only have a segment of the actual function.
L1 if(a == b) goto L15
L2 r1 = load(32)
L3 if(c < d) goto L14
L4 goto L14
L5 r1 << 4
L6 goto L10
L7 store(r2, r7)
L8 r5 = r5 * 25
L9 r4 = r4 + r4
L10 if(m > n) goto L4
L11 r3 = load(25 + r9)
L12 if(j != k) goto L13
L13 r10 = 4 * r10
L14 r11 = r11/8
L15 goto L12
L1 if(a == b) goto L12
L2 r1 = load(32)
L3 goto L14
L4 goto L14
L5 r1 << 4
L6 if(m > n) goto L4
L6’ goto L10
L7 store(r2, r7)
L8 r5 = r5 * 25
L9 r4 = r4 + r4
L10 if(m > n) goto L5
L11 r3 = load(25 + r9)
L12 if(j != k) goto L13
L13 r10 = 4 * r10
L14 r11 = r11/8
L15 goto 13
Note: ops in blue have been modified or inserted, while those that are red can be removed.
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Solutions for Homework 1 - Compiler Construction | EECS 483 and more Assignments Electrical and Electronics Engineering in PDF only on Docsity!

EECS 483 – Homework 1 Answers

Fall 2003

1.) Given the following segment of code, use acyclic control flow optimizations to optimize the code as much as possible. ASSUME that you don’t apply unreachable code elimination in this example since you only have a segment of the actual function. L1 if(a == b) goto L L2 r1 = load(32) L3 if(c < d) goto L L4 goto L L5 r1 << 4 L6 goto L L7 store(r2, r7) L8 r5 = r5 * 25 L9 r4 = r4 + r L10 if(m > n) goto L L11 r3 = load(25 + r9) L12 if(j != k) goto L L13 r10 = 4 * r L14 r11 = r11/ L15 goto L L1 if(a == b) goto L L2 r1 = load(32) L3 goto L L4 goto L L5 r1 << 4 L6 if(m > n) goto L L6’ goto L L7 store(r2, r7) L8 r5 = r5 * 25 L9 r4 = r4 + r L10 if(m > n) goto L L11 r3 = load(25 + r9) L12 if(j != k) goto L L13 r10 = 4 * r L14 r11 = r11/ L15 goto 13 Note: ops in blue have been modified or inserted, while those that are red can be removed.

2.) Consider the control flow graph given below: a.) For each basic block, compute the dominator and post dominator sets. b.) Using dominator information, detect all of the loops in this graph. For each loop, list the back edges, header BB, loop BBs and exit BBs. DOM(Entry) = Entry DOM(1) = Entry, 1 DOM(2) = Entry, 1, 2 DOM(3) = Entry, 1, 3 DOM(4) = Entry, 1, 3, 4 DOM(5) = Entry, 1, 5 DOM(6) = Entry, 1, 6 DOM(7) = Entry, 1, 7 DOM(8) = Entry, 1, 8 DOM(9) = Entry, 1, 9 DOM(Exit) = Entry, 1, 9, Exit PDOM(Entry) = Entry, 1, 9, Exit PDOM(1) = 1, 9, Exit PDOM(2) = 2, 9, Exit PDOM(3) = 3, 4, 9, Exit PDOM(4) = 4, 9, Exit PDOM(5) = 5, 9, Exit PDOM(6) = 6, 9, Exit PDOM(7) = 7, 9, Exit PDOM(8) = 8, 9, Exit PDOM(9) = 9, Exit PDOM(Exit) = Exit Back edge: 5 1 Header: 1 NOT a loop, not a single entry point! Back edge: 6 3 Not a backedge, as 3 does not dominate 6 Back edge: 7 3 Again, not a backedge as 3 does not dominate 7 No loops in this example!, there are however 3 cycles BB BB2 BB

BB

BB5 BB BB BB Exit Entry

3.) Answer the following questions about common subexpression elimination (CSE): a.) Given any two operations in a control flow graph, describe the requirements for performing CSE in terms of def-use and use-def chains. You should not make any assumptions about the locations of the operations (ie they may be in the same basic block or different ones). Two operations are valid for CSE if the following conditions hold:  They have the same opcode  They have the same source operands  If the op is a load or store, there is no intervening store that may be to the same location  The BB(op1) dominates the BB(op2)  For each source operand, the set of defs that reach each source are exactly the same for both operations b.) Given the following control flow graph, eliminate as many redundant expressions as possible with CSE. Note: Cannot apply CSE between 2 occurrences of r10+1 because the first does not dominate. Also, cannot apply to last load(r9) because the intervening store may be to the same address. r1 = r2 + r r3 = r3 << 2 r4 = load(r9+0) r6 = r7 / 5 store(r10+0, r8) r11 = r7 / 5 r14 = r9 * 6 r4 = load(r9+0) r15 = r9 * 6 r10 = r10 + 1 r12 = r2 + r store(r10+0, r8) r13 = r10 + 1 r4 = load(r9+0) r1 = r2 + r r100 = r r3 = r3 << 2 r4 = load(r9) r101 = r r6 = r7 / 5 r102 = r store(10, r8) r11 = r r14 = r9 * 6 r103 = r r4 = r r15 = r r10 = r10 + 1 r12 = r store(10, r8) r13 = r10 + 1 r4 = load(r9)

4.) Consider the loop program segment below:

a.) Compute reaching definitions GEN/KILL sets for each basic block. b.) Compute the reaching definition IN/OUT sets for each basic block c.) Which definitions in the reaching definition IN set for BB5 are also available? 11: r7 = load(r2+0) IN/OUT computation (iter1) IN/OUT computation (iter2)

  • DOM(1) = Entry, DOM(Entry) = Entry
  • DOM(2) = Entry, 1,
  • DOM(3) = Entry, 1,
  • DOM(4) = Entry, 1, 3,
  • DOM(5) = Entry, 1,
  • DOM(6) = Entry, 1, 3, 4,
  • DOM(7) = Entry, 1, 3, 4,
  • DOM(8) = Entry, 1, 2,
  • DOM(9) = Entry, 1,
  • Loop DOM(Exit) = Entry, 1, 9, Exit
  • Back edge: 5
  • Header:
  • Loop BBs: 1, 2, 3, 4, 5, 6, 7,
  • Exit BBs: 7,
  • Loop
  • Back edge: 6
  • Header:
  • Loop BBs: 3, 4,
  • Exit BBs: 4,
  • Loop
  • Back edge: 7
  • Header:
  • Loop BBs: 3, 4, 6,
  • Exit BBs: 4,
  • Loop 2/ Merge Loops 2/3 because they have the same header
  • Backedges: 63, 7
  • Loop BBs: 3,4,6,
  • Exit BBs: 4,
  • while 1-8 is the outer loop. The inner loop has 2 backedges and the outer loop has Even though it may not look this way, this CFG is 2 nested loops. 3-4-6-7 is the inner loop, - BB - BB2 BB
    • BB8 BB - BB5 BB - BB - BB - 1: r1 = c) None of the reaching defs in IN(BB5) are available! - 2: r2 = - BB - BB
    • BB - BB - 4: r2 = r2 + 3: r1 = load(r2+0)
      • 5: r3 = r
      • 6: r2 = r3 +
      • 7: r8 = r8 + - 8: r7 = r1 - - 9: r1 = r - 10: r2 = r7 << - BB 12: store (r7+0, r8)
  • GEN(1) = 1, GEN/KILL computation
  • KILL(1) = 3,4,6,9,
  • GEN(2) = 3,
  • KILL(2) = 1,2,6,9,
  • GEN(3) = 5,6,
  • KILL(3) = 2,4,
  • GEN(4) = 8,9,
  • KILL(4) = 1,2,3,4,6,
  • GEN(5) =
  • KILL(5) = - OUT(1) = 1, IN(1) =  - IN(2) = 1, - OUT(2) = 3, - IN(3) = 3, - OUT(3) = 3,5,6, - IN(4) = 3, - OUT(4) = 8,9, - IN(5) = 3,5,6,7,8,9, - OUT(5) = 3,5,6,7,9,10, - OUT(1) = 1, IN(1) =  - IN(2) = 1,2,3,5,6,7,9,10, - OUT(2) = 3,4,5,7, - IN(3) = 3,4,5,7, - OUT(3) = 3,5,6,7, - IN(4) = 3,4,5,7, - OUT(4) = 5,7,8,9, - IN(5) = 3,5,6,7,8,9,10, - OUT(5) = 3,5,6,7,9,10,

r2 = load(r100 + 0) r6 = load(r101 + 8) r7 = r2 + r store(r102+0, r7) r4 = r4 + 1 r5 = r5 + 4 r100 = r100 + 2 r101 = r101 + 8 r101 = r102 + 4 r4 = addr(A) r5 = 0 r100 = r4 << 1 r101 = r4 << 3 r102 = 6 const prop const fold copy prop dead code elimination r2 = load(r100 + 0) r6 = load(r101 + 8) r7 = r2 + r store(r102+0, r7) r100 = r100 + 2 r101 = r101 + 8 r101 = r102 + 4 r100 = r4 << 1 r101 = r4 << 3 r102 = 6 ind var elim r4 not used r5 not used dead code elimination r100, r101, r102 cannot be eliminated in this case because they have different increments. Its very difficult to eliminate ind vars when the increments are different without introducing extra ops in the loop. Since you cannot add any extra operations into the loop, all 3 must remain.

6.) Consider the following segment of C source code: a.) Convert the segment into PISA style assembly code. However, do not use physical registers, just use virtual registers. Provide a table to indicate which variable is mapped to which virtual register. b.) Draw the CFG. Indicate which basic blocks contain updates to a, b, d, and e. do { a = *x if ((a > 1) || ((d > 2) && (e > 3))) { b++ } else { if (b > 23) d++ } e++ } while (e < 100) This is not PISA syntax, but you should get the idea! L1: r1 = load(r5) r6 = r1 > 1 branch r6 L L2: r7 = r3 <= 2 # inverse condition branch r7 L L3: r8 = r4 <= 3 # inverse condition branch r8 L L4: r2 = r2 + 1 branch L L5: r9 = r2 <= 23 # inverse condition branch r9 L L6: r3 = r3 + 1 L7: r4 = r4 + 1 r10 = r4 < 100 branch r10 L register map a r b r d r e r x r temps r6- BB a = *x BB

BB
BB
BB

b++ BB d++ BB e++