Skip to content

Commit

Permalink
Merge branch 'inline'
Browse files Browse the repository at this point in the history
  • Loading branch information
JuniMay committed Jul 15, 2024
2 parents 2828f1f + 56b7976 commit 50453a1
Show file tree
Hide file tree
Showing 10 changed files with 571 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/bin/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use orzcc::{
control_flow::{CfgCanonicalize, CfgSimplify, CFG_SIMPLIFY},
fold::{ConstantFolding, CONSTANT_FOLDING},
gvn::{GlobalValueNumbering, GVN},
inline::{Inline, INLINE},
instcombine::{InstCombine, INSTCOMBINE},
loops::{LoopInvariantMotion, LOOP_INVARIANT_MOTION},
mem2reg::{Mem2reg, MEM2REG},
Expand Down Expand Up @@ -64,6 +65,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut ir = sysy::irgen(&ast);

if cmd.opt > 0 {
passman.run_transform(INLINE, &mut ir, 1);
passman.run_transform(MEM2REG, &mut ir, 1);
passman.run_transform(LOOP_INVARIANT_MOTION, &mut ir, 1);

Expand Down Expand Up @@ -104,6 +106,7 @@ fn register_passes(passman: &mut PassManager) {
SimpleDce::register(passman);
ConstantFolding::register(passman);
InstCombine::register(passman);
Inline::register(passman);

LoopInvariantMotion::register(passman);
GlobalValueNumbering::register(passman);
Expand Down
10 changes: 7 additions & 3 deletions src/ir/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ impl Block {
})
}

pub fn insn(self, ctx: &Context) -> usize { self.iter(ctx).count() }

pub fn set_source_span(self, ctx: &mut Context, span: impl Into<Span>) {
self.deref_mut(ctx).source_span = span.into();
}
Expand Down Expand Up @@ -164,13 +166,15 @@ impl Block {
LinkedListContainerPtr::merge(self, ctx, other);
}

pub fn split(self, ctx: &mut Context, inst: Inst) -> Block {
pub fn split(self, ctx: &mut Context, inst: Inst, create_jump: bool) -> Block {
let other = Block::new(ctx);
LinkedListContainerPtr::split(self, ctx, other, inst);
LinkedListNodePtr::insert_after(self, ctx, other);

let jump = Inst::jump(ctx, other, Vec::new());
self.push_back(ctx, jump);
if create_jump {
let jump = Inst::jump(ctx, other, Vec::new());
self.push_back(ctx, jump);
}

other
}
Expand Down
29 changes: 29 additions & 0 deletions src/ir/deep_clone.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use rustc_hash::FxHashMap;

use super::{Block, Value};

/// The mapping of values and blocks from the original to the cloned.
#[derive(Default)]
pub struct DeepCloneMap {
value_map: FxHashMap<Value, Value>,
block_map: FxHashMap<Block, Block>,
}

impl DeepCloneMap {
pub fn insert_value(&mut self, old: Value, new: Value) { self.value_map.insert(old, new); }

pub fn insert_block(&mut self, old: Block, new: Block) { self.block_map.insert(old, new); }

pub fn get_value(&self, old: Value) -> Option<Value> { self.value_map.get(&old).copied() }

pub fn get_block(&self, old: Block) -> Option<Block> { self.block_map.get(&old).copied() }

pub fn get_value_or_old(&self, old: Value) -> Value { self.get_value(old).unwrap_or(old) }

pub fn get_block_or_old(&self, old: Block) -> Block { self.get_block(old).unwrap_or(old) }

pub fn clear(&mut self) {
self.value_map.clear();
self.block_map.clear();
}
}
3 changes: 3 additions & 0 deletions src/ir/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ impl Func {
pub fn comment(&self, ctx: &mut Context, pos: CommentPos, content: impl Into<String>) {
self.deref(ctx).name.clone().comment(ctx, pos, content);
}

/// Get the number of instructions in the function.
pub fn insn(self, ctx: &Context) -> usize { self.iter(ctx).map(|block| block.insn(ctx)).sum() }
}

impl CfgRegion for Func {
Expand Down
65 changes: 65 additions & 0 deletions src/ir/inst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use rustc_hash::FxHashMap;
use super::{
constant::FloatConstant,
debug::CommentPos,
deep_clone::DeepCloneMap,
source_loc::Span,
Block,
Context,
Expand Down Expand Up @@ -356,6 +357,10 @@ impl Successor {
pub fn block(&self) -> Block { self.block.inner() }

pub fn args(&self) -> &FxHashMap<Value, Operand<Value>> { &self.args }

pub fn get_arg(&self, param: Value) -> Option<Value> {
self.args.get(&param).map(|arg| arg.inner())
}
}

pub struct DisplaySuccessor<'a> {
Expand Down Expand Up @@ -1191,6 +1196,66 @@ impl Inst {
}
}

pub fn deep_clone(self, ctx: &mut Context, map: &mut DeepCloneMap) -> Inst {
let kind = self.kind(ctx).clone();

let opds = self
.operands(ctx)
.into_iter()
.map(|opd| map.get_value_or_old(opd))
.collect::<Vec<_>>();

let result_tys = self
.results(ctx)
.iter()
.map(|r| r.ty(ctx))
.collect::<Vec<_>>();

let inst = Self::new(ctx, kind, result_tys, opds);

// succs
let blocks = self
.deref(ctx)
.successors
.iter()
.map(|succ| {
let old_block = succ.block();
let new_block = map.get_block_or_old(old_block);

let args = old_block
.params(ctx)
.iter()
.zip(new_block.params(ctx).iter())
.map(|(old_param, new_param)| {
let old_arg = succ.get_arg(*old_param).unwrap();
let new_arg = map.get_value_or_old(old_arg);
(*new_param, new_arg)
})
.collect::<FxHashMap<_, _>>();

(new_block, args)
})
.collect::<Vec<_>>();

for (block, args) in blocks {
let mut new_succ = Successor::new(Operand::new(ctx, block, inst));
for (param, arg) in args {
new_succ.add_arg(param, Operand::new(ctx, arg, inst));
}
inst.add_successor(ctx, new_succ);
}

for (old_result, new_result) in self.results(ctx).iter().zip(inst.results(ctx).iter()) {
map.insert_value(*old_result, *new_result);
}

inst
}

pub fn is_stack_slot(self, ctx: &Context) -> bool {
matches!(self.deref(ctx).kind, InstKind::StackSlot(_))
}

pub fn is_iconst(self, ctx: &Context) -> bool {
matches!(self.deref(ctx).kind, InstKind::IConst(_))
}
Expand Down
1 change: 1 addition & 0 deletions src/ir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod block;
mod call_graph;
mod constant;
mod context;
mod deep_clone;
mod fold;
mod global;
mod inst;
Expand Down
Loading

0 comments on commit 50453a1

Please sign in to comment.