Presentation is loading. Please wait.

Presentation is loading. Please wait.

Basic Blocks Mooly Sagiv Schrierber 317 03-640-7606 Wed 10:00-12:00 html://www.math.tau.ac.il/~msagiv/courses/wcc01.html Chapter 8.

Similar presentations


Presentation on theme: "Basic Blocks Mooly Sagiv Schrierber 317 03-640-7606 Wed 10:00-12:00 html://www.math.tau.ac.il/~msagiv/courses/wcc01.html Chapter 8."— Presentation transcript:

1 Basic Blocks Mooly Sagiv Schrierber 317 03-640-7606 Wed 10:00-12:00 html://www.math.tau.ac.il/~msagiv/courses/wcc01.html Chapter 8

2 Already Studied Source program (string) lexical analysis syntax analysis semantic analysis Translate Tokens Abstract syntax tree Tree IR Abstract syntax tree

3 Mismatches between IR and Machine Languages CJUMP jumps into two labels –But typical machine instructions use one target BEQ Ri, Rj, L Optimizing IR programs is difficult due to side effects in expressions –ESEQ nodes –Call nodes Call nodes within expressions prevent passing arguments in registers

4 Mismatches between IR and Machine Languages Call nodes within expressions prevent passing arguments and results in registers binop pluscall Name f 1 exp 1 Name f 2 exp 2

5 Why can’t we be smarter? Avoid two-way jumps Do not use ESEQ expressions

6 Three Phase Solution Rewrite the tree into a list of canonical trees without SEQ or ESEQ nodes Group the list into basic blocks Order basic blocks into a set of traces –CJUMP is immediately followed by false label

7 nfact example function nfactor (n: int): int= if n = 0 then 1 else n * nfactor(n-1)

8 MOVE TEMP t103 ESEQ CJUMP EQ TEMP t128 CONST 0 ESEQ LABEL l0 ESEQ MOVE TEMP t129CONST 1 l1 l0 ESEQ JUMP NAME l2 ESEQ LABEL l1 ESEQ MOVE TEMP t129 BINOP TIMES TEMP t128 CALL nfactor BINOP MINUS TEMP t128 CONST 1 ESEQ LABEL l2 TEMP t129 Missing updates for static link

9 SEQ CJUMP EQ TEMP t128 CONST 0 LABEL l0 SEQ MOVE TEMP t129 CONST 1 l1 l0 SEQ JUMP NAME l2 SEQ LABEL l1 SEQ TEMP t129 BINOP TIMES TEMP t131 CALL nfactor BINOP MINUS TEMP t128CONST 1 MOVE TEMP t103 TEMP t129 SEQ LABEL l2 SEQ TEMP t130 MOVE TEMP t130 MOVE TEMP t128 TEMP t131 SEQ MOVE

10 LABEL(l3) CJUMP(EQ, TEMP t128, CONST 0, l0, l1) LABEL( l0) MOVE(TEMP t129, CONST 1) JUMP(NAME l2) LABEL( l1) MOVE(TEMP t131, TEMP t128) MOVE(TEMP t130, CALL(nfactor, BINOP(MINUS, TEMP t128, CONST 1))) MOVE(TEMP t129, BINOP(TIMES, TEMP t131, TEMP t30)) JUMP(NEME l2) LABEL( l2) MOVE(TEMP t103, TEMP t129) JUMP(NAME lend)

11 LABEL(l3) CJUMP(EQ, TEMP t128, CONST 0, l0, l1) LABEL( l0) MOVE(TEMP t129, CONST 1) JUMP(NAME l2) LABEL( l1) MOVE(TEMP t131, TEMP t128) MOVE(TEMP t130, CALL(nfactor, BINOP(MINUS, TEMP t128, CONST 1))) MOVE(TEMP t129, BINOP(TIMES, TEMP t131, TEMP t130)) /* JUMP(NAME l2) */ LABEL( l2) MOVE(TEMP t103, TEMP t129) JUMP(NAME lend)

12 Outline The Cannon Interface Phase 1: Removal of ESEQ nodes Phase 2: Basic Blocks Phase 3: Order traces –CJUMP is followed by a false label

13 /* canon.h */ typedef struct C_stmListList *C_stmListList; struct C_block {C_stmListList stmLists; Temp_label label;} struct C_stmListList_ { T_stmList head; C_stmListList tail;} T_stmList C_linearize(T_stm stm); /* Eliminate ESEQs */ struct C_block C_basicBlocks(T_stmList stmList); T_stmList C_traceSchedule(struct C_block b); /* main.c */ static void doProc(FILE *out, F_frame frame, T_stm body) { T_stmList stmList; AS_instrList iList; stmList = C_linearize(body); /* Removes ESEQs */ stmList = C_traceSchedule(C_basicBlocks(stmList)); iList = F_codegen(frame, stmList); /* 9 */ }

14 Canonical Trees (Phase 1) Rewrite the tree –No SEQ and ESEQ –The parent of each CALL is either EXP or MOVE(TEMP t, …) Apply “meaning preserving” rewriting rules Sometimes generates temporaries

15 ESEQ s1 ESEQ s2 e ESEQ SEQ s1 e s2

16 BINOP op e2 s e1 ESEQ s BINOP e2 op e1

17 MEM s e ESEQ s e MEM

18 JUMP s e ESEQ SEQ s e JUMP

19 CJUMP s e1 ESEQ op e2l1

20 BINOP op ESEQ s e2 e1 ESEQ s BINOP e2 op e1 When s and e1 commutes

21 Which statements commute? In general very difficult Example The compiler decides conservatively MEM e1 MOVE MEM e2 e3

22 Which statements commute? static bool commute(T_stm x, T_exp y) { if (isNop(x)) return TRUE; if (y->kind == T_NAME || y->kind == T_CONST) return TRUE; return FALSE; }

23 BINOP op ESEQ s e2 e1 ESEQ MOVE BINOP e2 op TEMP t When s and e1 may not commute s TEMP t e1 ESEQ

24 CJUMP s e2 e1 op ESEQl1 When s and e1 may not commute CJUMP s TEMP t op e2 l1 SEQ MOVE TEMP t e1 SEQ

25 MOVE TEMP t103 ESEQ CJUMP EQ TEMP t128 CONST 0 ESEQ LABEL l0 ESEQ MOVE TEMP t129CONST 1 l1 l0 ESEQ JUMP NAME l2 ESEQ LABEL l1 ESEQ MOVE TEMP t129 BINOP TIMES TEMP t128 CALL nfactor BINOP MINUS TEMP t128 CONST 1 ESEQ LABEL l2 TEMP t129

26 SEQ CJUMP EQ TEMP t128 CONST 0 ESEQ LABEL l0 ESEQ MOVE TEMP t129CONST 1 l1 l0 ESEQ JUMP NAME l2 ESEQ LABEL l1 ESEQ MOVE TEMP t129 BINOP TIMES TEMP t128 CALL nfactor BINOP MINUS TEMP t128 CONST 1 ESEQ LABEL l2 TEMP t129 TEMP t103 MOVE

27 SEQ CJUMP EQ TEMP t128 CONST 0 LABEL l0 ESEQ MOVE TEMP t129CONST 1 l1 l0 ESEQ JUMP NAME l2 ESEQ LABEL l1 ESEQ MOVE TEMP t129 BINOP TIMES TEMP t128 CALL nfactor BINOP MINUS TEMP t128 CONST 1 ESEQ LABEL l2 TEMP t129 TEMP t103 MOVE SEQ

28 CJUMP EQ TEMP t128 CONST 0 LABEL l0 SEQ MOVE TEMP t129 CONST 1 l1 l0 ESEQ JUMP NAME l2 ESEQ LABEL l1 ESEQ MOVE TEMP t129 BINOP TIMES TEMP t128 CALL nfactor BINOP MINUS TEMP t128 CONST 1 ESEQ LABEL l2 TEMP t129 SEQ TEMP t103 MOVE

29 SEQ CJUMP EQ TEMP t128 CONST 0 LABEL l0 SEQ MOVE TEMP t129 CONST 1 l1 l0 SEQ JUMP NAME l2 ESEQ LABEL l1 ESEQ MOVE TEMP t129 BINOP TIMES TEMP t128 CALL nfactor BINOP MINUS TEMP t128 CONST 1 ESEQ LABEL l2 TEMP t129 SEQ TEMP t103 MOVE

30 SEQ CJUMP EQ TEMP t128 CONST 0 LABEL l0 SEQ MOVE TEMP t129 CONST 1 l1 l0 SEQ JUMP NAME l2 SEQ LABEL l1 ESEQ MOVE TEMP t129 BINOP TIMES TEMP t128 CALL nfactor BINOP MINUS TEMP t128 CONST 1 ESEQ LABEL l2 TEMP t129 SEQ TEMP t103 MOVE

31 SEQ CJUMP EQ TEMP t128 CONST 0 LABEL l0 SEQ MOVE TEMP t129 CONST 1 l1 l0 SEQ JUMP NAME l2 SEQ LABEL l1 SEQ MOVE TEMP t129 BINOP TIMES TEMP t128 CALL nfactor BINOP MINUS TEMP t128 CONST 1 ESEQ LABEL l2 TEMP t129 SEQ TEMP t103 MOVE

32 SEQ CJUMP EQ TEMP t128 CONST 0 LABEL l0 SEQ MOVE TEMP t129 CONST 1 l1 l0 SEQ JUMP NAME l2 SEQ LABEL l1 SEQ MOVE TEMP t129 BINOP TIMES TEMP t128 CALL nfactor BINOP MINUS TEMP t128CONST 1 ESEQ LABEL l2 TEMP t129 SEQ TEMP t103 MOVE ESEQ TEMP t130 MOVE TEMP t130

33 SEQ CJUMP EQ TEMP t128 CONST 0 LABEL l0 SEQ MOVE TEMP t129 CONST 1 l1 l0 SEQ JUMP NAME l2 SEQ LABEL l1 SEQ MOVE TEMP t129 BINOP TIMES TEMP t131 CALL nfactor BINOP MINUS TEMP t128CONST 1 ESEQ LABEL l2 TEMP t129 SEQ TEMP t103 MOVE TEMP t130 MOVE TEMP t130 ESEQ MOVE TEMP t128 TEMP t131 ESEQ

34 SEQ CJUMP EQ TEMP t128 CONST 0 LABEL l0 SEQ MOVE TEMP t129 CONST 1 l1 l0 SEQ JUMP NAME l2 SEQ LABEL l1 SEQ TEMP t129 BINOP TIMES TEMP t131 CALL nfactor BINOP MINUS TEMP t128CONST 1 ESEQ LABEL l2 TEMP t129 SEQ TEMP t103 MOVE TEMP t130 MOVE TEMP t130 MOVE TEMP t128 TEMP t131 ESEQ MOVE

35 SEQ CJUMP EQ TEMP t128 CONST 0 LABEL l0 SEQ MOVE TEMP t129 CONST 1 l1 l0 SEQ JUMP NAME l2 SEQ LABEL l1 SEQ TEMP t129 BINOP TIMES TEMP t131 CALL nfactor BINOP MINUS TEMP t128CONST 1 ESEQ LABEL l2 TEMP t129 SEQ TEMP t103 MOVE TEMP t130 MOVE TEMP t130 MOVE TEMP t128 TEMP t131 SEQ MOVE

36 SEQ CJUMP EQ TEMP t128 CONST 0 LABEL l0 SEQ MOVE TEMP t129 CONST 1 l1 l0 SEQ JUMP NAME l2 SEQ LABEL l1 SEQ TEMP t129 BINOP TIMES TEMP t131 CALL nfactor BINOP MINUS TEMP t128CONST 1 MOVE TEMP t103 TEMP t129 SEQ LABEL l2 SEQ TEMP t130 MOVE TEMP t130 MOVE TEMP t128 TEMP t131 SEQ MOVE

37 A Theoretical Solution Apply rewriting rules until convergence The result need not be unique Efficiency and termination of the compiler

38 A Practical Solution Apply rewriting rules in “one” pass Two mutually recursive routines –do_stm(s) applies rewritings to s –do_exp(e) applies rewritings to e reorder(expRefList) –Returns the side effect statements in expRefList –Replaces expressions by temporaries Code distributed in “cannon.c”

39 Taming Conditional Brunch Reorder statements so that CJUMP is followed by a false label Two subphases: –Partition the statement list into basic blocks (straightline programs starting with a label and ending with a branch) –Reorder basic blocks (Traces)

40 Phase 2: Basic Blocks The compiler does not know which branch will be taken Conservatively analyze the control flow of the program A basic block –The first statement is a LABEL –The last statement is JUMP or CJUMP –There are no other LABELs, JUMPs, or CJUMPs

41 An Algorithm for Basic Blocks C_basicBlocks() Applied for each function body Scan the statement list from left to right Whenever a LABEL is found –a new block begins (and the previous block ends) Whenever JUMP or CJUMP are found –the current block ends (and the next block begins) When a block ends without JUMP or CJUMP – JUMP to the following LABEL When a block does not start with a LABEL –Add a LABEL At the end of the function body jump to the beginning of the epilogue

42 LABEL(l3) CJUMP(EQ, TEMP t128, CONST 0, l0, l1) LABEL( l0) MOVE(TEMP t129, CONST 1) JUMP(NAME l2) LABEL( l1) MOVE(TEMP t131, TEMP t128) MOVE(TEMP t130, CALL(nfactor, BINOP(MINUS, TEMP t128, CONST 1))) MOVE(TEMP t129, BINOP(TIMES, TEMP t131, TEMP t130)) LABEL( l2) MOVE(TEMP t103, TEMP t129) JUMP(NAME l2) JUMP(NAME lend)

43 Traces Reorder basic blocks –Every CJUMP is followed by a false label –Many of the unconditional jumps are followed by the corresponding labels can be eliminated A trace –a sequence of basic blocks that are executed sequentially A program has many overlapping traces Find a set of traces that exactly covers the program –every block appears in exactly one trace Minimize the number of traces

44 An Algorithm for Generating Traces C_traceSchedule() Put all the blocks of the program into a list Q while Q is not empty do Start a new (empty) trace, call it T Remove the head element b of Q while b is not marked do mark b append b to the end of the current trace T if there is an unmarked successor c of b b := c end of current trace T

45 LABEL(l3) CJUMP(EQ, TEMP t128, CONST 0, l0, l1) LABEL( l0) MOVE(TEMP t129, CONST 1) JUMP(NAME l2) LABEL( l1) MOVE(TEMP t131, TEMP t128) MOVE(TEMP t130, CALL(nfactor, BINOP(MINUS, TEMP t128, CONST 1))) MOVE(TEMP t129, BINOP(TIMES, TEMP t131, TEMP t130)) JUMP(NAME l2) LABEL( l2) MOVE(TEMP t103, TEMP t129) JUMP(NAME lend) T1 T2

46 Finishing-Up CJUMP followed by a false label is left alone JUMP (NAME l) that is followed by a label l is removed CJUMP followed by a true label –replace true and false labels and negate the condition If CJUMP(cond, a, b, l t, l f ) is not followed by l t or l f –Replace by: CJUMP(cond, a, b, l t, l' f ) LABEL(l' f ) JUMP(NAME l f ) At the end of the process flat basic blocks (trade simplicity for efficiency of the compiler and of the generated code)

47 LABEL(l3) CJUMP(EQ, TEMP t128, CONST 0, l0, l1) LABEL( l0) MOVE(TEMP t129, CONST 1) JUMP(NAME l2) LABEL( l1) MOVE(TEMP t131, TEMP t128) MOVE(TEMP t130, CALL(nfactor, BINOP(MINUS, TEMP t128, CONST 1))) MOVE(TEMP t129, BINOP(TIMES, TEMP t131, TEMP t130)) JUMP(NAME l2) LABEL( l2) MOVE(TEMP t103, TEMP t129) JUMP(NAME lend) T1 T2

48 LABEL(l3) CJUMP(EQ, TEMP t128, CONST 0, l0, l1) LABEL( l0) MOVE(TEMP t129, CONST 1) JUMP(NAME l2) LABEL( l1) MOVE(TEMP t131, TEMP t128) MOVE(TEMP t130, CALL(nfactor, BINOP(MINUS, TEMP t128, CONST 1))) MOVE(TEMP t129, BINOP(TIMES, TEMP t131, TEMP t130)) JUMP(NAME l2) LABEL( l2) MOVE(TEMP t103, TEMP t129) JUMP(NAME lend)

49 LABEL(l3) CJUMP(EQ, TEMP t128, CONST 0, l0, l1) LABEL( l0) MOVE(TEMP t129, CONST 1) JUMP(NAME l2) LABEL( l1) MOVE(TEMP t131, TEMP t128) MOVE(TEMP t130, CALL(nfactor, BINOP(MINUS, TEMP t128, CONST 1))) MOVE(TEMP t129, BINOP(TIMES, TEMP t131, TEMP t130)) /* JUMP(NAME l2) */ LABEL( l2) MOVE(TEMP t103, TEMP t129) JUMP(NAME lend)

50 Optimal-Traces Optimizing compilers locate traces for frequently executed instructions Minimize the (dynamic) number of jumps –Improve instruction cache performance Improves register allocation Optimize loops Sometimes use –Static heuristics –Profiling information –Dynamic compilation

51 prologue statements JUMP (NAME test) LABEL (test) CJ(>, i, N, done, body) LABEL(body) loop-body statements JUMP(NAME(test)) LABEL(done) epilogue statements prologue statements JUMP (NAME test) LABEL (test) CJ(<=, i, N, body, done) LABEL(done) epilogue statements LABEL(body) loop-body statements JUMP(NAME test) prologue statements JUMP (NAME test) LABEL (body) loop-body statements JUMP(NAME TEST) LABEL(TEST) CJ(<=, i, N, body, done) LABEL(done) epilogue statements

52 Summary Tree like IR saves temporary variables But ESEQ nodes may require some temporaries –Late decision Rewriting rules is a powerful mechanism Estimating “commuting” statements is a challenge Traces may effect the performance of the generated code


Download ppt "Basic Blocks Mooly Sagiv Schrierber 317 03-640-7606 Wed 10:00-12:00 html://www.math.tau.ac.il/~msagiv/courses/wcc01.html Chapter 8."

Similar presentations


Ads by Google