Start a very simple GVN pass (#79)

* Skeleton simple_gvn pass.
* Basic testing infrastructure for simple-gvn.
* Add can_load and can_store flags to instructions.
* Move the replace_values function into the DataFlowGraph.
* Make InstructionData derive from Hash, PartialEq, and Eq.
* Make EntityList's hash and eq functions panic.
* Change Ieee32 and Ieee64 to store u32 and u64, respectively.
This commit is contained in:
Dan Gohman
2017-05-18 18:18:57 -07:00
committed by Jakob Stoklund Olesen
parent 1d8efaad83
commit c826aefa0a
16 changed files with 256 additions and 57 deletions

View File

@@ -0,0 +1,65 @@
//! A simple GVN pass.
use flowgraph::ControlFlowGraph;
use dominator_tree::DominatorTree;
use ir::{Cursor, InstructionData, Function, Inst, Opcode};
use std::collections::HashMap;
/// Test whether the given opcode is unsafe to even consider for GVN.
fn trivially_unsafe_for_gvn(opcode: Opcode) -> bool {
opcode.is_call() || opcode.is_branch() || opcode.is_terminator() || opcode.is_return() ||
opcode.can_trap()
}
/// Perform simple GVN on `func`.
///
pub fn do_simple_gvn(func: &mut Function, cfg: &mut ControlFlowGraph) {
let mut visible_values: HashMap<InstructionData, Inst> = HashMap::new();
let domtree = DominatorTree::with_function(func, &cfg);
// Visit EBBs in a reverse post-order.
let mut postorder = cfg.postorder_ebbs();
let mut pos = Cursor::new(&mut func.layout);
while let Some(ebb) = postorder.pop() {
pos.goto_top(ebb);
while let Some(inst) = pos.next_inst() {
let opcode = func.dfg[inst].opcode();
if trivially_unsafe_for_gvn(opcode) {
continue;
}
// TODO: Implement simple redundant-load elimination.
if opcode.can_store() {
continue;
}
if opcode.can_load() {
continue;
}
let key = func.dfg[inst].clone();
let entry = visible_values.entry(key);
use std::collections::hash_map::Entry::*;
match entry {
Occupied(mut entry) => {
if domtree.dominates(*entry.get(), inst, &pos.layout) {
func.dfg.replace_with_aliases(inst, *entry.get());
pos.remove_inst();
} else {
// The prior instruction doesn't dominate inst, so it
// won't dominate any subsequent instructions we'll
// visit, so just replace it.
*entry.get_mut() = inst;
continue;
}
}
Vacant(entry) => {
entry.insert(inst);
}
}
}
}
}