outline
play

Outline Unreachable-Code Elimination P3 / 2003 Straightening - PDF document

Outline Unreachable-Code Elimination P3 / 2003 Straightening If and Loop Simplifications Control-Flow and Low-Level Loop Inversion and Unswitching Branch Optimizations Optimizations Tail Merging (Cross Jumping)


  1. Outline • Unreachable-Code Elimination P3 / 2003 • Straightening • If and Loop Simplifications Control-Flow and Low-Level • Loop Inversion and Unswitching • Branch Optimizations Optimizations • Tail Merging (Cross Jumping) • Conditional Moves • Dead-Code Elimination • Branch Prediction • Peephole Optimization • Machine Idioms & Instruction Combining Spring 2003 Kostis Sagonas 2 Unreachable-Code Elimination Unreachable-Code Elimination entry entry Unreachable code is code that cannot be executed, regardless of the input data c = a + b c = a + b – code that is never executable for any input data d = c d = c f = a + c e > c e > c – code that has become unreachable due to a previous g = e compiler transformation. a = e +c f = c – g f = c – g b = c + 1 b = c + 1 Unreachable code elimination removes this code. h = e + 1 d = 4 * a d = 4 * a e < a e = d – 7 e = d – 7 – Doing so, reduces the code space; f = e + 2 f = e + 2 – improves instruction-cache utilization; – enables other control-flow transformations. exit exit Spring 2003 Spring 2003 Kostis Sagonas 3 Kostis Sagonas 4 Outline Straightening • Unreachable-Code Elimination Straightening is applicable to pairs of basic blocks such • Straightening that the first has no successors other than the second • If and Loop Simplifications and the second has no predecessors other than the first. • Loop Inversion and Unswitching • Branch and Useless Control-Flow Optimizations • Tail Merging (Cross Jumping) … … a = b + c • Conditional Moves a = b + c • Dead-Code Elimination b = c * 2 b = c * 2 • Branch Prediction a = a + 1 a = a + 1 c < 0 • Peephole Optimization c < 0 • Machine Idioms & Instruction Combining Spring 2003 Spring 2003 Kostis Sagonas 5 Kostis Sagonas 6 1

  2. Straightening Example Outline • Unreachable-Code Elimination Straightening in the presence of fall-throughs is tricky... • Straightening • If and Loop Simplifications L1: … L1: … a = b + c a = b + c • Loop Inversion and Unswitching goto L2 b = c * 2 • Branch and Useless Control-Flow Optimizations a = a + 1 • Tail Merging (Cross Jumping) L6: … if c < 0 goto L3 • Conditional Moves goto L4 goto L5 • Dead-Code Elimination • Branch Prediction L2: b = c * 2 L6: … a = a + 1 goto L4 • Peephole Optimization if c < 0 goto L3 • Machine Idioms & Instruction Combining L5: … L5: … Spring 2003 Spring 2003 Kostis Sagonas 7 Kostis Sagonas 8 If Simplifications If Simplification Example a > d a > d If simplifications apply to conditional constructs Y Y N N one or both of whose branches are empty: b = a b = a – if either the ���� or the ���� part of an �� -construct is c = 4 * b c = 4 * b (a >= d) or bool empty, the corresponding branch can be eliminated Y N – one branch of an �� with a constant-valued condition d = b d = c d = b can also be eliminated – we can also simplify �� s whose condition, C, occurs e = a + b e = a + b in the scope of a condition that implies C (and none of the condition’s operands has changed value) … … Spring 2003 Spring 2003 Kostis Sagonas 9 Kostis Sagonas 10 Loop Simplifications Loop Simplification Example • A loop whose body is empty can be eliminated s = 0 i = 0 if the iteration-control code has no side-effects s = 0 i = i + 1 i = 0 (Side-effects might be simple enough that they can be s = s + i i = 4 L1: if i > 4 goto L2 replaced with non-looping code at compile time) i = i + 1 s = 10 i = i + 1 s = s + i L2: … s = s + i i = i + 1 goto L1 • If number of iterations is small enough, loops s = s + i L2: … i = i + 1 can be unrolled into branchless code and the s = s + i loop body can be executed at compile time L2: … Spring 2003 Spring 2003 Kostis Sagonas 11 Kostis Sagonas 12 2

  3. Outline Loop Inversion • Unreachable-Code Elimination Loop inversion transforms a ����� loop into a • Straightening ������ loop (i.e. moves the loop-closing test • If and Loop Simplifications from before the loop to after it). • Loop Inversion and Unswitching – Has the advantage that only one branch instruction • Branch and Useless Control-Flow Optimizations needs to be executed to close the loop. • Tail Merging (Cross Jumping) • Conditional Moves – Requires that we determine that the loop is entered • Dead-Code Elimination at least once! • Branch Prediction • Peephole Optimization • Machine Idioms & Instruction Combining Spring 2003 Spring 2003 Kostis Sagonas 13 Kostis Sagonas 14 Loop Inversion Example 1 Loop Inversion Example 2 for (i = 0; i < 100; i++) { Loop bounds are known a[i] = i + 1; } if (k >= n) goto L for (i = k; i < n; i++) { i = k; a[i] = i + 1; repeat { } a[i] = i + 1; i++; i = 0; i = 0; } until (i >= n) Loop bounds while (i < 100) { repeat { L: are unknown a[i] = i + 1; a[i] = i + 1; i++; i++; } } until (i >= 100) Spring 2003 Spring 2003 Kostis Sagonas 15 Kostis Sagonas 16 Unswitching Unswitching Example Unswitching is a control-flow transformation that moves loop-invariant conditional branches out if (k == 2) { of loops for (i = 1; i < 100; i++) { for (i = 1; i < 100; i++) { if (a[i] > 0) if (k == 2) { if (k == 2 && a[i] > 0) for (i = 1; i < 100; i++) { a[i] = a[i] + 1; for (i = 1; i < 100; i++) a[i] = a[i] + 1; if (k == 2) } a[i] = a[i] + 1; } a[i] = a[i] + 1; } else { } else { else i = 100; for (i = 1; i < 100; i++) a[i] = a[i] – 1; } a[i] = a[i] – 1; } } Spring 2003 Spring 2003 Kostis Sagonas 17 Kostis Sagonas 18 3

  4. Outline Branch Optimizations • Unreachable-Code Elimination Branches to branches are remarkably common! • Straightening – An unconditional branch to an unconditional branch can be • If and Loop Simplifications replaced by a branch to the latter’s target • Loop Inversion and Unswitching – A conditional branch to an unconditional branch can be • Branch and Useless Control-Flow Optimizations replaced by the corresponding conditional branch to the latter branch’s target • Tail Merging (Cross Jumping) – An unconditional branch to a conditional branch can be • Conditional Moves replaced by a copy of the conditional branch • Dead-Code Elimination – A conditional branch to a conditional branch can be replaced • Branch Prediction by a conditional branch with the former’s test and the latter’s • Peephole Optimization target as long as the latter condition is true whenever the • Machine Idioms & Instruction Combining former one is Spring 2003 Spring 2003 Kostis Sagonas 19 Kostis Sagonas 20 Branch Optimization Examples Eliminating Useless Control-Flow The Problem: if a = 0 goto L1 if a = 0 goto L2 … … – After optimization, the CFG can contain empty blocks L1: if a >= 0 goto L2 L1: if a >= 0 goto L2 – “Empty” blocks still end with either a branch or jump … … L2: … L2: … – Produces jump to jump, which wastes time and space The Algorithm: ( ����� ) goto L1 L1: … L1: … – Use four distinct transformations – Apply them in a carefully selected order if a = 0 goto L1 if a != 0 goto L2 goto L2 – Iterate until done L1: … L1: … Spring 2003 Spring 2003 Kostis Sagonas 21 Kostis Sagonas 22 Eliminating Useless Control-Flow Eliminating Useless Control-Flow Transformation 1 Transformation 2 Merging an empty block Both sides of branch target B2 – Empty B1 ends with a jump – Neither block must be empty B1 B1 empty – Coalesce B1 and B2 – Replace it with a jump to B1 B1 – Move B1’s incoming edges – Simple rewrite of the last – Eliminates extraneous jump operation in B1 B2 – Faster, smaller code B2 B2 B2 Branch, not a jump How does this happen? How does this happen? Eliminating redundant branches – By rewriting other branches Eliminating empty blocks – By eliminating operations in B1 How do we recognize it? How do we recognize it? – Check each branch – Test for empty block Spring 2003 Spring 2003 Kostis Sagonas 23 Kostis Sagonas 24 4

Recommend


More recommend