Docsity

Control Flow IV: Control Flow Optimizations Start Dataflow Analysis | EECS 483, Study Guides, Projects, Research of Electrical and Electronics Engineering

Partial preview of the text

Download Control Flow IV: Control Flow Optimizations Start Dataflow Analysis | EECS 483 and more Study Guides, Projects, Research Electrical and Electronics Engineering in PDF only on Docsity!

Control Flow IV: Control Flow Optimizations Start Dataflow Analysis

EECS 483 – Lecture 22 University of Michigan Wednesday, November 22, 2006

  • 1 -

Announcements and Reading

Project 3

» Get started building openimpact!! » Make sure you get the environment variables

set up before trying to install

Reading

» 10.

  • 3 -

l_code.h: Core Lcode Data Structures (cont)

L_Flow – control flow edges for CFG

» src_cb, dst_cb, weight » Connected in doubly linked list (prev/next)

L_Func – function

» First/last cbs, weight » Oper and cb hash tables (id -> L_Oper/L_Cb)

L_Attr – extra information

» L_Oper, L_Cb, and L_Func have attributes » Name, array of L_Operands

  • 4 -

Example Opti: Local Constant Propagation

for (opA = cb->first_op; opA != NULL; opA = opA->next_op) {

/* Match pattern */ if (!L_move_opcode (opA)) continue; if (!L_is_constant (opA->src[0])) continue; for (opB = opA->next_op; opB != NULL; opB = opB->next_op) {

if (!L_is_src_operand (opA->dest[0], opB)) continue; if (!L_can_change_src_operand (opB, opA->dest[0])) continue; if (!L_no_defs_between (opA->dest[0], opA, opB)) continue; macro_flag = L_is_fragile_macro (opA->dest[0]); load_flag = 0; store_flag = 0; if (!L_no_danger (macro_flag, load_flag, store_flag, opA, opB))

break; /* Replace pattern */ for (i = 0; i < L_max_src_operand; i++) {

if (L_same_operand (opA->dest[0], opB->src[i])) {

opB->src[i] = L_copy_operand (opA->src[0]); }

}

} }

In reality, more code than this, but this is the important stuff

  • 6 -

Running Things Manually, Debugging - cont

Running Lemulate manually

» ls *.O > list » Lemulate –i list » Generates a .c file for each .O file

Compile the .c files with gcc

»

gcc –g *.c $IMPACT_REL_PATH/platform/x86lin_gcc/impact_lemul_lib.o –lm

» Generates a.out, so now run it.

Debugging – gdb

» Can gdb the benchmark a.out or Lopti » Can also add print statements to Lopti

  • 7 -

From Last Time: Loop Unroll Summary

Goals

» Reduce number of executed branches inside loop

y
Note: Type1/Type2 only

» Enable the overlapped execution of multiple iterations

y
Reorder instructions between iterations

» Expose optimizations across iterations

Type 1 – compile-time constant trip count

Type 2 – counted, but trip count unknown atcompile time

Type 3 – non-counted

  • 9 -

From Last Time: Class Problem (cont’d)

j = i; while (j < 100) {
A[j++]--;
Expanding the while loop – For the problem this needs to be done for both while loops^ }

j = itripcount = 100 – jtripcount = tripcount / 1remainder = tc % 2final = remainder * 1 final = final + j / I forgot this on the*

*unroll 2 slide /

Remloop:while (j < final) {

A[j++]--; } Unrolledloop:while (j < 100) {

A[j]--;A[j+1]--j+=2; }

  • 10 -

Loop Peeling

Unravel first P iterations of a loop

» Enable overlap of instructions from the peeled

iterations with preheader instructions

» Increase potential for ILP» Enables further optimization of main body

Loop:r1 = MEM[r2 + 0]r4 = r1 * r5r6 = r4 << 2MEM[r3 + 0] = r6r2 = r2 + 1blt r2 100 Loop

r1 = MEM[r2 + 0]r4 = r1 * r5r6 = r4 << 2MEM[r3 + 0] = r6r2 = r2 + 1bge r2 100 Done

Preheader

Iteration 1

More iters?

Done:
  • 12 -

Acyclic Control Flow Optimizations (1)

1. Branch to unconditional branch

L1: if (a < b) goto L2... L2: goto L

L1: if (a < b) goto L3... L2: goto L

Æ

may be deleted

2. Unconditional branch to branch

L1: goto L2.. .L2: if (a < b) goto L3L4:

L1: if (a < b) goto L3goto L4:.. .L2: if (a < b) goto L

Æ

may be deleted

L4:

  • 13 -

Acyclic Control Flow Optimizations (2)

3. Branch to next basic block

4. Basic block merging

.. .L1: if (a < b) goto L2 L2:...

BB1 BB
.. .L1: L2:...
BB1 BB
Branch is unnecessary
.. .L1: L2:...
.. .L1:L2:...
BB1 BB
Merge BBs when single edge between^ BB
  • 15 -

Unreachable Code Elimination^ Mark procedure entry BB visited^ to_visit = procedure entry BB^ while (to_visit not empty) {

current = to_visit.pop() for (each successor block of current) {
Mark successor as visited; to_visit += successor
} Eliminate all unvisited blocks

entry

bb

bb

bb

bb

bb

Which BB(s) can be deleted?

Algorithm

  • 16 -

Class Problem

L1: if (a < b) goto L11L2: goto L7L3: goto L4L4: stuff4L5: if (c < d) goto L15L6: goto L2L7: if (c < d) goto L13L8: goto L12L9: stuff 9L10: if (a < c) goto L3L11:goto L9L12: goto L2L13: stuff 13L14: if (e < f) goto L11L15: stuff 15L16: rts

Maximally optimize the control flow of this code

  • 18 -

Linearizing a Trace

BB2 BB4 BB
BB
BB
BB

20 (side exit) 10 (side exit)

10 (entry count)

90 (entry/ exit count)

10 (exit count)

20 (side entrance) 10 (side entrance)

  • 19 -

Intelligent Trace Layout for IcachePerformance

BB2 BB4 BB
BB
BB
BB

trace1 trace 2 trace 3 The rest

Intraprocedural code placement Procedure positioning Procedure splitting

Procedure view
Trace view