Skip to content

Commit

Permalink
Finish inst transition for IR structures
Browse files Browse the repository at this point in the history
  • Loading branch information
Y-Nak committed Sep 19, 2024
1 parent 05005ea commit 3b970ee
Show file tree
Hide file tree
Showing 41 changed files with 646 additions and 1,539 deletions.
10 changes: 5 additions & 5 deletions crates/codegen/src/critical_edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use sonatina_ir::{func_cursor::FuncCursor, ControlFlowGraph};

use sonatina_ir::{
func_cursor::{CursorLocation, InsnInserter},
insn::InsnData,
inst::InsnData,
BlockId, Function, Insn,
};

Expand All @@ -28,7 +28,7 @@ impl CriticalEdgeSplitter {
self.clear();

for block in func.layout.iter_block() {
if let Some(last_insn) = func.layout.last_insn_of(block) {
if let Some(last_insn) = func.layout.last_inst_of(block) {
self.add_critical_edges(last_insn, func, cfg);
}
}
Expand Down Expand Up @@ -59,7 +59,7 @@ impl CriticalEdgeSplitter {
fn split_edge(&mut self, edge: CriticalEdge, func: &mut Function, cfg: &mut ControlFlowGraph) {
let insn = edge.insn;
let original_dest = edge.to;
let source_block = func.layout.insn_block(insn);
let source_block = func.layout.inst_block(insn);

// Create a new block that contains only a jump insn to the destinating block of the
// critical edge.
Expand All @@ -68,7 +68,7 @@ impl CriticalEdgeSplitter {
let mut cursor = InsnInserter::at_location(CursorLocation::BlockTop(original_dest));
cursor.append_block(func, inserted_dest);
cursor.set_location(CursorLocation::BlockTop(inserted_dest));
cursor.append_insn(func, jump);
cursor.append_inst(func, jump);

// Rewrite branch destination to the new block.
func.dfg
Expand All @@ -83,7 +83,7 @@ impl CriticalEdgeSplitter {
original_dest: BlockId,
inserted_dest: BlockId,
) {
for insn in func.layout.iter_insn(original_dest) {
for insn in func.layout.iter_inst(original_dest) {
if !func.dfg.is_phi(insn) {
continue;
}
Expand Down
16 changes: 8 additions & 8 deletions crates/codegen/src/optim/adce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::post_domtree::{PDFSet, PDTIdom, PostDomTree};

use sonatina_ir::{
func_cursor::{CursorLocation, FuncCursor, InsnInserter},
insn::InsnData,
inst::InsnData,
BlockId, Function, Insn,
};

Expand Down Expand Up @@ -56,7 +56,7 @@ impl AdceSolver {
}

for block in func.layout.iter_block() {
for insn in func.layout.iter_insn(block) {
for insn in func.layout.iter_inst(block) {
if func.dfg.has_side_effect(insn) {
self.mark_insn(func, insn);
}
Expand Down Expand Up @@ -92,9 +92,9 @@ impl AdceSolver {
}
};

let insn_block = func.layout.insn_block(insn);
let insn_block = func.layout.inst_block(insn);
if mark_insn(insn, insn_block) {
let last_insn = func.layout.last_insn_of(insn_block).unwrap();
let last_insn = func.layout.last_inst_of(insn_block).unwrap();
mark_insn(last_insn, insn_block);
}
}
Expand All @@ -118,9 +118,9 @@ impl AdceSolver {
}
}

let insn_block = func.layout.insn_block(insn);
let insn_block = func.layout.inst_block(insn);
for &block in pdf_set.frontiers(insn_block) {
if let Some(last_insn) = func.layout.last_insn_of(block) {
if let Some(last_insn) = func.layout.last_inst_of(block) {
self.mark_insn(func, last_insn)
}
}
Expand All @@ -140,7 +140,7 @@ impl AdceSolver {
if self.does_insn_live(insn) {
inserter.proceed(func);
} else {
inserter.remove_insn(func)
inserter.remove_inst(func)
}
}

Expand Down Expand Up @@ -189,7 +189,7 @@ impl AdceSolver {
inserter: &mut InsnInserter,
block: BlockId,
) -> bool {
let last_insn = match func.layout.last_insn_of(block) {
let last_insn = match func.layout.last_inst_of(block) {
Some(insn) => insn,
None => return false,
};
Expand Down
2 changes: 1 addition & 1 deletion crates/codegen/src/optim/constant_folding.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use sonatina_ir::{
insn::{BinaryOp, CastOp, UnaryOp},
inst::{BinaryOp, CastOp, UnaryOp},
DataFlowGraph, Immediate, InsnData,
};

Expand Down
18 changes: 9 additions & 9 deletions crates/codegen/src/optim/gvn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::domtree::{DomTree, DominatorTreeTraversable};

use sonatina_ir::{
func_cursor::{CursorLocation, FuncCursor, InsnInserter},
insn::{BinaryOp, CastOp, InsnData, UnaryOp},
inst::{BinaryOp, CastOp, InsnData, UnaryOp},
BlockId, ControlFlowGraph, DataFlowGraph, Function, Immediate, Insn, Type, ValueId,
};

Expand Down Expand Up @@ -106,7 +106,7 @@ impl GvnSolver {
self.blocks[block].rank = rank;
rank += 1;

for insn in func.layout.iter_insn(block) {
for insn in func.layout.iter_inst(block) {
if let Some(insn_result) = func.dfg.insn_result(insn) {
self.values[insn_result].rank = rank;
rank += 1;
Expand Down Expand Up @@ -194,17 +194,17 @@ impl GvnSolver {
continue;
}

let mut next_insn = func.layout.first_insn_of(block);
let mut next_insn = func.layout.first_inst_of(block);
while let Some(insn) = next_insn {
// Reassign congruence class to the result value of the insn.
if let Some(insn_result) = func.dfg.insn_result(insn) {
changed |= self.reassign_congruence(func, domtree, insn, insn_result);
}
next_insn = func.layout.next_insn_of(insn);
next_insn = func.layout.next_inst_of(insn);
}

// If insn is terminator, analyze it to update edge and block reachability.
if let Some(last_insn) = func.layout.last_insn_of(block) {
if let Some(last_insn) = func.layout.last_inst_of(block) {
changed |= self.analyze_last_insn(func, domtree, block, last_insn);
}
}
Expand Down Expand Up @@ -320,7 +320,7 @@ impl GvnSolver {
insn_result: ValueId,
) -> bool {
// Perform symbolic evaluation for the insn.
let block = func.layout.insn_block(insn);
let block = func.layout.inst_block(insn);
let gvn_insn = self.perform_symbolic_evaluation(
func,
domtree,
Expand Down Expand Up @@ -1247,7 +1247,7 @@ impl<'a> ValuePhiFinder<'a> {
}

let class = self.solver.value_class(value);
let phi_block = func.layout.insn_block(func.dfg.value_insn(value)?);
let phi_block = func.layout.inst_block(func.dfg.value_insn(value)?);

// if the gvn_insn of the value class is phi, then create `ValuePhi::Insn` from its args.
if let GvnInsn::Insn(InsnData::Phi { values, blocks, .. }) =
Expand Down Expand Up @@ -1440,7 +1440,7 @@ impl<'a> RedundantCodeRemover<'a> {
// Use representative value if the class is in avail set.
if let Some(value) = avails.get(&class) {
self.change_to_alias(func, insn_result, *value);
inserter.remove_insn(func);
inserter.remove_inst(func);
continue;
}

Expand Down Expand Up @@ -1489,7 +1489,7 @@ impl<'a> RedundantCodeRemover<'a> {
);

self.change_to_alias(func, insn_result, value);
inserter.remove_insn(func);
inserter.remove_inst(func);
continue;
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/codegen/src/optim/insn_simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl InsnSimplifySolver {
}

while let Some(insn) = self.worklist.pop_front() {
if !func.layout.is_insn_inserted(insn) {
if !func.layout.is_inst_inserted(insn) {
continue;
}

Expand Down Expand Up @@ -81,7 +81,7 @@ impl InsnSimplifySolver {
func.dfg.change_to_alias(insn_result, value);
};

inserter.remove_insn(func);
inserter.remove_inst(func);
}

pub fn replace_insn_with_data(
Expand Down
14 changes: 7 additions & 7 deletions crates/codegen/src/optim/licm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl LicmSolver {

let mut loop_var = FxHashSet::default();
for block in block_in_loop_rpo {
for insn in func.layout.iter_insn(block) {
for insn in func.layout.iter_inst(block) {
if self.is_invariant(func, &loop_var, insn) {
self.invariants.push(insn);
} else if let Some(result) = func.dfg.insn_result(insn) {
Expand Down Expand Up @@ -122,7 +122,7 @@ impl LicmSolver {

// Rewrite branch destination of original preheaders and modify cfg.
for block in original_preheaders.iter().copied() {
let last_insn = func.layout.last_insn_of(block).unwrap();
let last_insn = func.layout.last_inst_of(block).unwrap();
func.dfg
.rewrite_branch_dest(last_insn, lp_header, new_preheader);
cfg.remove_edge(block, lp_header);
Expand All @@ -141,10 +141,10 @@ impl LicmSolver {

/// Hoist invariants to the preheader.
fn hoist_invariants(&self, func: &mut Function, preheader: BlockId) {
let last_insn = func.layout.last_insn_of(preheader).unwrap();
let last_insn = func.layout.last_inst_of(preheader).unwrap();
for invariant in self.invariants.iter().copied() {
func.layout.remove_insn(invariant);
func.layout.insert_insn_before(invariant, last_insn);
func.layout.remove_inst(invariant);
func.layout.insert_inst_before(invariant, last_insn);
}
}

Expand All @@ -159,7 +159,7 @@ impl LicmSolver {
// Record inserted phis to avoid duplication of the same phi.
let mut inserted_phis = FxHashMap::default();

let mut next_insn = func.layout.first_insn_of(lp_header);
let mut next_insn = func.layout.first_inst_of(lp_header);
while let Some(insn) = next_insn {
if !func.dfg.is_phi(insn) {
break;
Expand Down Expand Up @@ -198,7 +198,7 @@ impl LicmSolver {
// Append the result of new phi insn.
func.dfg.append_phi_arg(insn, phi_result, new_preheader);

next_insn = func.layout.next_insn_of(insn);
next_insn = func.layout.next_inst_of(insn);
}
}
}
Expand Down
24 changes: 12 additions & 12 deletions crates/codegen/src/optim/sccp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use cranelift_entity::SecondaryMap;

use sonatina_ir::{
func_cursor::{CursorLocation, FuncCursor, InsnInserter},
insn::{BinaryOp, CastOp, InsnData, UnaryOp},
inst::{BinaryOp, CastOp, InsnData, UnaryOp},
BlockId, ControlFlowGraph, Function, Immediate, Insn, Type, ValueId,
};

Expand Down Expand Up @@ -63,7 +63,7 @@ impl SccpSolver {
while let Some(value) = self.ssa_work.pop() {
changed = true;
for &user in func.dfg.users(value) {
let user_block = func.layout.insn_block(user);
let user_block = func.layout.inst_block(user);
if self.reachable_blocks.contains(&user_block) {
if func.dfg.is_phi(user) {
self.eval_phi(func, user);
Expand Down Expand Up @@ -103,7 +103,7 @@ impl SccpSolver {
self.eval_insns_in(func, dest);
}

if let Some(last_insn) = func.layout.last_insn_of(dest) {
if let Some(last_insn) = func.layout.last_inst_of(dest) {
let branch_info = func.dfg.analyze_branch(last_insn);
if branch_info.dests_num() == 1 {
self.flow_work.push(FlowEdge::new(
Expand All @@ -115,7 +115,7 @@ impl SccpSolver {
}

fn eval_phis_in(&mut self, func: &Function, block: BlockId) {
for insn in func.layout.iter_insn(block) {
for insn in func.layout.iter_inst(block) {
if func.dfg.is_phi(insn) {
self.eval_phi(func, insn);
}
Expand All @@ -126,15 +126,15 @@ impl SccpSolver {
debug_assert!(func.dfg.is_phi(insn));
debug_assert!(self
.reachable_blocks
.contains(&func.layout.insn_block(insn)));
.contains(&func.layout.inst_block(insn)));

for &arg in func.dfg.insn_args(insn) {
if let Some(imm) = func.dfg.value_imm(arg) {
self.set_lattice_cell(arg, LatticeCell::Const(imm));
}
}

let block = func.layout.insn_block(insn);
let block = func.layout.inst_block(insn);

let mut eval_result = LatticeCell::Bot;
for (i, from) in func.dfg.phi_blocks(insn).iter().enumerate() {
Expand All @@ -153,7 +153,7 @@ impl SccpSolver {
}

fn eval_insns_in(&mut self, func: &Function, block: BlockId) {
for insn in func.layout.iter_insn(block) {
for insn in func.layout.iter_inst(block) {
if func.dfg.is_phi(insn) {
self.eval_phi(func, insn);
} else {
Expand Down Expand Up @@ -352,9 +352,9 @@ impl SccpSolver {
rpo.reverse();

for block in rpo {
let mut next_insn = func.layout.first_insn_of(block);
let mut next_insn = func.layout.first_inst_of(block);
while let Some(insn) = next_insn {
next_insn = func.layout.next_insn_of(insn);
next_insn = func.layout.next_inst_of(insn);
self.fold(func, insn);
}
}
Expand All @@ -368,7 +368,7 @@ impl SccpSolver {

match self.lattice[insn_result].to_imm() {
Some(imm) => {
InsnInserter::at_location(CursorLocation::At(insn)).remove_insn(func);
InsnInserter::at_location(CursorLocation::At(insn)).remove_inst(func);
let new_value = func.dfg.make_imm_value(imm);
func.dfg.change_to_alias(insn_result, new_value);
}
Expand All @@ -394,12 +394,12 @@ impl SccpSolver {
let phi_value = func.dfg.insn_result(insn).unwrap();
func.dfg
.change_to_alias(phi_value, func.dfg.insn_arg(insn, 0));
InsnInserter::at_location(CursorLocation::At(insn)).remove_insn(func);
InsnInserter::at_location(CursorLocation::At(insn)).remove_inst(func);
}
}

fn is_reachable(&self, func: &Function, from: BlockId, to: BlockId) -> bool {
let last_insn = if let Some(insn) = func.layout.last_insn_of(from) {
let last_insn = if let Some(insn) = func.layout.last_inst_of(from) {
insn
} else {
return false;
Expand Down
2 changes: 1 addition & 1 deletion crates/codegen/src/optim/simplify_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use smallvec::SmallVec;
use cranelift_entity::{entity_impl, PrimaryMap, SecondaryMap};

use sonatina_ir::{
insn::{BinaryOp, CastOp, DataLocationKind, UnaryOp},
inst::{BinaryOp, CastOp, DataLocationKind, UnaryOp},
module::FuncRef,
BlockId, DataFlowGraph, Immediate, Insn, InsnData, Type, ValueId,
};
Expand Down
6 changes: 3 additions & 3 deletions crates/interpreter/src/pc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl ReservedValue for ProgramCounter {
impl ProgramCounter {
pub fn new(entry_func: FuncRef, layout: &Layout) -> Self {
let entry = layout.entry_block().unwrap();
let insn = layout.first_insn_of(entry).unwrap();
let insn = layout.first_inst_of(entry).unwrap();

Self {
func_ref: entry_func,
Expand All @@ -35,11 +35,11 @@ impl ProgramCounter {
}

pub fn next_insn(&mut self, layout: &Layout) {
self.insn = layout.next_insn_of(self.insn).unwrap();
self.insn = layout.next_inst_of(self.insn).unwrap();
}

pub fn branch_to(&mut self, block: BlockId, layout: &Layout) {
self.insn = layout.first_insn_of(block).unwrap();
self.insn = layout.first_inst_of(block).unwrap();
}

pub fn resume_frame_at(&mut self, ret_addr: Self) {
Expand Down
Loading

0 comments on commit 3b970ee

Please sign in to comment.