Refactoring of branch ordering and zero-check optimization based on PR feedback.

This commit is contained in:
Tyler McMullen
2019-01-02 11:51:58 -08:00
committed by Benjamin Bouvier
parent 861ef3a2e5
commit 67cc5aafec

View File

@@ -566,7 +566,13 @@ fn branch_opt(pos: &mut FuncCursor, inst: Inst) {
let args = pos.func.dfg.inst_args(inst);
args[0]
};
let iconst_inst =
if let ValueDef::Result(iconst_inst, _) = pos.func.dfg.value_def(first_arg) {
iconst_inst
} else {
return;
};
if let InstructionData::BinaryImm {
opcode: Opcode::IfcmpImm,
imm: cmp_imm,
@@ -578,25 +584,18 @@ fn branch_opt(pos: &mut FuncCursor, inst: Inst) {
return;
}
match br_cond {
IntCC::NotEqual => BranchOptInfo {
br_inst: inst,
cmp_arg: cmp_arg,
destination: destination,
args: args.clone(),
kind: BranchOptKind::NotEqualZero,
},
IntCC::Equal => BranchOptInfo {
br_inst: inst,
cmp_arg: cmp_arg,
destination: destination,
args: args.clone(),
kind: BranchOptKind::EqualZero,
},
let kind = match br_cond {
IntCC::NotEqual => BranchOptKind::NotEqualZero,
IntCC::Equal => BranchOptKind::EqualZero,
_ => return,
}
} else {
return;
};
BranchOptInfo {
br_inst: inst,
cmp_arg: cmp_arg,
destination: destination,
args: args.clone(),
kind: kind,
}
} else {
return;
@@ -648,21 +647,31 @@ fn branch_order(pos: &mut FuncCursor, cfg: &mut ControlFlowGraph, ebb: Ebb, inst
destination,
ref args,
} => {
if let Some(next_ebb) = pos.func.layout.next_ebb(ebb) {
let next_ebb = if let Some(next_ebb) = pos.func.layout.next_ebb(ebb) {
next_ebb
} else {
return;
};
if destination == next_ebb {
return;
}
if let Some(prev_inst) = pos.func.layout.prev_inst(inst) {
let prev_inst_data = &pos.func.dfg[prev_inst];
if !prev_inst_data.opcode().is_branch() {
let prev_inst = if let Some(prev_inst) = pos.func.layout.prev_inst(inst) {
prev_inst
} else {
return;
}
};
let prev_inst_data = &pos.func.dfg[prev_inst];
if let Some(prev_dest) = prev_inst_data.branch_destination() {
if prev_dest != next_ebb {
return;
}
} else {
return;
}
match prev_inst_data {
InstructionData::Branch {
@@ -676,28 +685,21 @@ fn branch_order(pos: &mut FuncCursor, cfg: &mut ControlFlowGraph, ebb: Ebb, inst
args[0]
};
match opcode {
Opcode::Brz => BranchOrderInfo {
term_inst: inst,
term_inst_args: args.clone(),
term_dest: destination,
cond_inst: prev_inst,
cond_arg: cond_arg,
cond_inst_args: prev_args.clone(),
cond_dest: *cond_dest,
kind: BranchOrderKind::BrzToBrnz,
},
Opcode::Brnz => BranchOrderInfo {
term_inst: inst,
term_inst_args: args.clone(),
term_dest: destination,
cond_inst: prev_inst,
cond_arg: cond_arg,
cond_inst_args: prev_args.clone(),
cond_dest: *cond_dest,
kind: BranchOrderKind::BrnzToBrz,
},
let kind = match opcode {
Opcode::Brz => BranchOrderKind::BrzToBrnz,
Opcode::Brnz => BranchOrderKind::BrnzToBrz,
_ => panic!("unexpected opcode"),
};
BranchOrderInfo {
term_inst: inst,
term_inst_args: args.clone(),
term_dest: destination,
cond_inst: prev_inst,
cond_arg: cond_arg,
cond_inst_args: prev_args.clone(),
cond_dest: *cond_dest,
kind: kind,
}
}
InstructionData::BranchInt {
@@ -746,16 +748,8 @@ fn branch_order(pos: &mut FuncCursor, cfg: &mut ControlFlowGraph, ebb: Ebb, inst
}
_ => return,
}
} else {
return;
}
} else {
return;
}
} else {
return;
}
}
_ => return,
};