





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






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
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)
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)
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
b++ BB d++ BB e++