Add a fold_redundant_jumps() pass to the branch relaxation phase. (#887)

This commit is contained in:
Sean Stangl
2019-08-09 15:30:11 -06:00
committed by GitHub
parent e6e274a3aa
commit c7b4b98cac
8 changed files with 178 additions and 41 deletions

View File

@@ -689,48 +689,48 @@ ebb0:
; asm: testq %rcx, %rcx
; asm: je ebb1
brz v1, ebb1 ; bin: 48 85 c9 74 1b
brz v1, ebb1 ; bin: 48 85 c9 74 19
fallthrough ebb3
ebb3:
; asm: testq %rsi, %rsi
; asm: je ebb1
brz v2, ebb1 ; bin: 48 85 f6 74 16
brz v2, ebb1 ; bin: 48 85 f6 74 14
fallthrough ebb4
ebb4:
; asm: testq %r10, %r10
; asm: je ebb1
brz v3, ebb1 ; bin: 4d 85 d2 74 11
brz v3, ebb1 ; bin: 4d 85 d2 74 0f
fallthrough ebb5
ebb5:
; asm: testq %rcx, %rcx
; asm: jne ebb1
brnz v1, ebb1 ; bin: 48 85 c9 75 0c
brnz v1, ebb1 ; bin: 48 85 c9 75 0a
fallthrough ebb6
ebb6:
; asm: testq %rsi, %rsi
; asm: jne ebb1
brnz v2, ebb1 ; bin: 48 85 f6 75 07
brnz v2, ebb1 ; bin: 48 85 f6 75 05
fallthrough ebb7
ebb7:
; asm: testq %r10, %r10
; asm: jne ebb1
brnz v3, ebb1 ; bin: 4d 85 d2 75 02
brnz v3, ebb1 ; bin: 4d 85 d2 75 00
; asm: jmp ebb2
jump ebb2 ; bin: eb 01
jump ebb2
; asm: ebb1:
ebb1:
return ; bin: c3
return
; asm: ebb2:
ebb2:
jump ebb1 ; bin: eb fd
jump ebb1
}
; CPU flag instructions.
@@ -1292,40 +1292,41 @@ ebb0:
; asm: testl %ecx, %ecx
; asm: je ebb1x
brz v1, ebb1 ; bin: 85 c9 74 18
brz v1, ebb1 ; bin: 85 c9 74 16
fallthrough ebb3
ebb3:
; asm: testl %esi, %esi
; asm: je ebb1x
brz v2, ebb1 ; bin: 85 f6 74 14
brz v2, ebb1 ; bin: 85 f6 74 12
fallthrough ebb4
ebb4:
; asm: testl %r10d, %r10d
; asm: je ebb1x
brz v3, ebb1 ; bin: 45 85 d2 74 0f
brz v3, ebb1 ; bin: 45 85 d2 74 0d
fallthrough ebb5
ebb5:
; asm: testl %ecx, %ecx
; asm: jne ebb1x
brnz v1, ebb1 ; bin: 85 c9 75 0b
brnz v1, ebb1 ; bin: 85 c9 75 09
fallthrough ebb6
ebb6:
; asm: testl %esi, %esi
; asm: jne ebb1x
brnz v2, ebb1 ; bin: 85 f6 75 07
brnz v2, ebb1 ; bin: 85 f6 75 05
fallthrough ebb7
ebb7:
; asm: testl %r10d, %r10d
; asm: jne ebb1x
brnz v3, ebb1 ; bin: 45 85 d2 75 02
brnz v3, ebb1 ; bin: 45 85 d2 75 00
; asm: jmp ebb2x
jump ebb2 ; bin: eb 01
; branch relaxation translates this into `fallthrough ebb1`
jump ebb2
; asm: ebb1x:
ebb1:

View File

@@ -11,9 +11,9 @@ function u0:0(i64) system_v {
ebb0(v0: i64):
v1 = stack_addr.i64 ss0
v2 = load.i8 v1
br_table v2, ebb2, jt0
br_table v2, ebb1, jt0
; check: $(oob=$V) = ifcmp_imm $(idx=$V), 1
; nextln: brif uge $oob, ebb2
; nextln: brif uge $oob, ebb1
; nextln: fallthrough $(inb=$EBB)
; check: $inb:
; nextln: $(final_idx=$V) = uextend.i64 $idx
@@ -22,9 +22,6 @@ ebb0(v0: i64):
; nextln: $(addr=$V) = iadd $base, $rel_addr
; nextln: indirect_jump_table_br $addr, jt0
ebb2:
jump ebb1
ebb1:
return
}

View File

@@ -7,6 +7,8 @@ use crate::match_directive::match_directive;
use crate::subtest::{Context, SubTest, SubtestResult};
use cranelift_codegen::binemit::{self, CodeInfo, CodeSink, RegDiversions};
use cranelift_codegen::dbg::DisplayList;
use cranelift_codegen::dominator_tree::DominatorTree;
use cranelift_codegen::flowgraph::ControlFlowGraph;
use cranelift_codegen::ir;
use cranelift_codegen::ir::entities::AnyEntity;
use cranelift_codegen::print_errors::pretty_error;
@@ -166,8 +168,11 @@ impl SubTest for TestBinEmit {
}
// Relax branches and compute EBB offsets based on the encodings.
let CodeInfo { total_size, .. } = binemit::relax_branches(&mut func, isa)
.map_err(|e| pretty_error(&func, context.isa, e))?;
let mut cfg = ControlFlowGraph::with_function(&func);
let mut domtree = DominatorTree::with_function(&func, &cfg);
let CodeInfo { total_size, .. } =
binemit::relax_branches(&mut func, &mut cfg, &mut domtree, isa)
.map_err(|e| pretty_error(&func, context.isa, e))?;
// Collect all of the 'bin:' directives on instructions.
let mut bins = HashMap::new();