Where we are abstract syntax tree syntax-directed translation (IR generation) CS 4120 intermediate code Introduction to Compilers syntax-directed translation (flattening) Andrew Myers reordering with traces canonical intermediate code Cornell University instruction selection abstract assembly code Lecture 16: Basic blocks, CFGs, traces register allocation assembly code 1 CS 4120 Introduction to Compilers 2 IR lowering Conditional jumps • IR is now just a linear list of statements with • We lower the IR to a canonical form in one side e ff ect per statement which code is a sequence of statements, • Still contains CJUMP nodes : two-way branches each containing a single side e ff ect. • Real machines : fall-through branches ( e.g . JZ , • Done by transformations that lift side- JNZ ) e ff ecting statements to the top of the IR tree. CJUMP(e, t, f) evaluate e • L[s] = s 1 ...s n ... JZ f LABEL(t) if-true code • L[e] = s 1 ...s n ; e’ if-true code f: LABEL(f) – Side e ff ects of e in s i . Value of e computed by side-e ff ect-free e’ CS 4120 Introduction to Compilers 3 CS 4120 Introduction to Compilers 4
Simple Solution Basic blocks • Translate CJUMP into conditional branch • Unit of reordering is a basic block followed by unconditional branch • A sequence of statements that is always begun at its start and always exits at the end: LABEL( l ) CJUMP(TEMP(t1)==TEMP(t2), t, f) CMP t1,t2 –starts with a LABEL( n ) statement … JZ t (or beginning of all statements) CJUMP( e , l 1 , l 2 ) JMP f –ends with a JUMP or CJUMP statement, or just before a LABEL statement •JMP is usually gratuitous –contains no other JUMP or CJUMP statement • Code can be reordered so jump goes to next –contains no interior LABEL used as a jump target statement • No point to breaking up a basic block during reordering CS 4120 Introduction to Compilers 5 CS 4120 Introduction to Compilers 6 Basic block example Control flow graph • Control flow graph has basic blocks as nodes • Edges show control flow between basic blocks CJUMP(e, L2, L3) LABEL(L1) MOVE(TEMP(x), TEMP(y) LABEL(L2) MOVE(TEMP(x), TEMP(y) + TEMP(z)) JUMP(NAME(L1)) LABEL(L3) EXP(CALL(NAME(f)), TEMP(x)) CJUMP(e, L2, L3) CJUMP(e, L2, L3) LABEL(L1) MOVE(TEMP(x), TEMP(y) LABEL(L2) MOVE(TEMP(x), TEMP(y) + TEMP(z)) JUMP(NAME(L1)) LABEL(L3) LABEL(L2) EXP(CALL(NAME(f)), TEMP(x)) MOVE(TEMP(x), TEMP(y) + TEMP(z)) JUMP(NAME(L1)) LABEL(L3) LABEL(L1) EXP(CALL(NAME(f)), TEMP(x)) MOVE(TEMP(x), TEMP(y) CS 4120 Introduction to Compilers 7 CS 4120 Introduction to Compilers
Fixing conditional jumps Traces • Idea: order blocks according to a possible trace: a • Reorder basic blocks so that (if possible) sequence of blocks that might (naively) be executed in –the “false” direction of two-way jumps goes to sequence, never visiting a block more than once the very next block • Algorithm: –JUMPs go to the next block (are deleted) –pick an unmarked block (begin w/ start block) –run a trace until no more unmarked blocks can be visited, • What if not satisfied? marking each block on arrival –For CJUMP add another JUMP immediately –repeat until no more unmarked blocks after to go to the right basic block • How to find such an ordering of the basic blocks? CS 4120 Introduction to Compilers 9 CS 4120 Introduction to Compilers 10 Example Arranging by traces • Possible traces? 1 1 1 2 4 5 2 3 3 2 4 1 2 4 • Can use profiling information, heuristics 4 5 5 to choose which branch to follow 5 3 3 CS 4120 Introduction to Compilers 11 CS 4120 Introduction to Compilers 12
Reordered code Reversing sense of jumps CJUMP(e, L2, [L3]) CJUMP(e, L2, L3) JUMP(L3) CJUMP(e, L2, [L3]) LABEL(L2) JUMP(L3) CJUMP(NOT(e), L3, [L2]) MOVE(TEMP(x), TEMP(y) + TEMP(z)) LABEL(L2) LABEL(L2) JUMP(L1) MOVE(TEMP(x), TEMP(y) + MOVE(TEMP(x), TEMP(y) + LABEL(L2) TEMP(z)) TEMP(z)) LABEL(L1) MOVE(TEMP(x), …) MOVE(TEMP(x), TEMP(y) JUMP(L1) LABEL(L1) LABEL(L1) JUMP(L2) MOVE(TEMP(x), TEMP(y) MOVE(TEMP(x), TEMP(y) JUMP(L2) JUMP(L2) LABEL(L3) EXP(CALL(NAME(f)), TEMP(x)) LABEL(L3) LABEL(L3) LABEL(L3) LABEL(L1) EXP(CALL(NAME(f)), TEMP(x)) EXP(CALL(NAME(f)), TEMP(x)) EXP(CALL(f), MOVE(TEMP(x), TEMP(x)) TEMP(y)) CS 4120 Introduction to Compilers 13 CS 4120 Introduction to Compilers 14 Progress abstract syntax tree syntax-directed translation (IR generation) intermediate code syntax-directed translation (flattening) reordering with traces canonical intermediate code instruction selection ( tiling ) abstract assembly code register allocation assembly code CS 4120 Introduction to Compilers 15
Recommend
More recommend