use rucc_ir::{Block, Def, Extra, Flags, Func, Imm, Inst, IntPred, Opcode, Type, Value};
use crate::{Analyses, Fuel, Pass, Preserved, Stats};
const FOLDED: &str = "integer instruction folded to a constant";
const NO_FUEL: &str = "integer instruction not folded, the pass ran out of fuel";
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Fold;
impl Pass for Fold {
fn name(&self) -> &'static str {
"fold"
}
fn describe(&self) -> &'static str {
"an integer instruction whose operands are all constants becomes a constant"
}
fn preserves(&self) -> Preserved {
Preserved::ALL
}
fn run(&self, func: &mut Func, _an: &mut Analyses, fuel: &mut Fuel) -> Stats {
let blocks: Vec<Block> = func.blocks().collect();
let mut stats = Stats::new();
for block in blocks {
let insts: Vec<Inst> = func.insts(block).collect();
for inst in insts {
let Some(folded) = evaluate(func, inst) else { continue };
if !fuel.take() {
stats.missed(NO_FUEL);
continue;
}
let ty = func[result_of(func, inst)].ty;
let at = func.add_imm(folded);
let data = &mut func[inst];
data.opcode = Opcode::IConst;
data.flags = Flags::NONE;
data.args = rucc_ir::ValueList::EMPTY;
data.extra = Extra::Imm(at);
debug_assert!(ty.is_int(), "only an integer instruction folds");
stats.optimized(FOLDED);
}
}
stats
}
}
fn result_of(func: &Func, inst: Inst) -> Value {
func[inst].results().next().expect("an instruction that folds produces a value")
}
fn evaluate(func: &Func, inst: Inst) -> Option<Imm> {
let data = &func[inst];
if data.results != 1 {
return None;
}
let result = data.results().next()?;
let ty = func[result].ty;
if !ty.is_int() || !ty.is_scalar() {
return None;
}
let args = &func[data.args];
match data.opcode {
Opcode::Trunc | Opcode::SExt | Opcode::ZExt => {
let (value, from) = constant(func, *args.first()?)?;
Some(convert(data.opcode, value, from, ty))
}
Opcode::Shl | Opcode::LShr | Opcode::AShr => {
let (value, from) = constant(func, *args.first()?)?;
let (count, count_ty) = constant(func, *args.get(1)?)?;
shift(data.opcode, value, from, count, count_ty, ty, data.flags)
}
Opcode::Add | Opcode::Sub | Opcode::Mul | Opcode::And | Opcode::Or | Opcode::Xor => {
let (lhs, lhs_ty) = constant(func, *args.first()?)?;
let (rhs, _) = constant(func, *args.get(1)?)?;
binary(data.opcode, lhs, rhs, lhs_ty, ty, data.flags)
}
Opcode::Ctlz | Opcode::Cttz | Opcode::Ctpop | Opcode::Bswap | Opcode::Bitreverse => {
let (value, from) = constant(func, *args.first()?)?;
count(data.opcode, value, from, ty)
}
Opcode::ICmp => {
let Extra::IntPred(pred) = data.extra else { return None };
let (lhs, from) = constant(func, *args.first()?)?;
let (rhs, _) = constant(func, *args.get(1)?)?;
Some(Imm::int(i128::from(compare(pred, lhs, rhs, from)), ty))
}
_ => None,
}
}
pub(crate) fn constant(func: &Func, value: Value) -> Option<(Imm, Type)> {
let Def::Result { inst, .. } = func[value].def else { return None };
if func[inst].opcode != Opcode::IConst {
return None;
}
let Extra::Imm(at) = func[inst].extra else { return None };
let ty = func[value].ty;
ty.is_int().then(|| (func[at], ty))
}
fn convert(opcode: Opcode, value: Imm, from: Type, to: Type) -> Imm {
match opcode {
Opcode::Trunc | Opcode::SExt => Imm::int(value.signed(from), to),
_ => Imm::int(value.unsigned() as i128, to),
}
}
fn shift(
opcode: Opcode,
value: Imm,
from: Type,
count: Imm,
count_ty: Type,
to: Type,
flags: Flags,
) -> Option<Imm> {
let by = count.unsigned();
if by >= u128::from(to.bits()) || count.signed(count_ty) < 0 {
return None;
}
let by = by as u32;
let exact = match opcode {
Opcode::Shl => value.signed(from).checked_shl(by)?,
Opcode::LShr => (value.unsigned() >> by) as i128,
_ => value.signed(from) >> by,
};
if opcode == Opcode::Shl && overflowed(exact, to, flags) {
return None;
}
Some(Imm::int(exact, to))
}
fn binary(opcode: Opcode, lhs: Imm, rhs: Imm, from: Type, to: Type, flags: Flags) -> Option<Imm> {
let (a, b) = (lhs.signed(from), rhs.signed(from));
let exact = match opcode {
Opcode::And => a & b,
Opcode::Or => a | b,
Opcode::Xor => a ^ b,
Opcode::Add => a.checked_add(b)?,
Opcode::Sub => a.checked_sub(b)?,
_ => a.checked_mul(b)?,
};
if overflowed(exact, to, flags) {
return None;
}
Some(Imm::int(exact, to))
}
pub(crate) fn compare(pred: IntPred, lhs: Imm, rhs: Imm, ty: Type) -> bool {
match pred {
IntPred::Eq => lhs == rhs,
IntPred::Ne => lhs != rhs,
IntPred::Slt => lhs.signed(ty) < rhs.signed(ty),
IntPred::Sle => lhs.signed(ty) <= rhs.signed(ty),
IntPred::Sgt => lhs.signed(ty) > rhs.signed(ty),
IntPred::Sge => lhs.signed(ty) >= rhs.signed(ty),
IntPred::Ult => lhs.unsigned() < rhs.unsigned(),
IntPred::Ule => lhs.unsigned() <= rhs.unsigned(),
IntPred::Ugt => lhs.unsigned() > rhs.unsigned(),
IntPred::Uge => lhs.unsigned() >= rhs.unsigned(),
}
}
fn count(opcode: Opcode, value: Imm, from: Type, to: Type) -> Option<Imm> {
let width = from.bits();
if width == 0 || width > 128 {
return None;
}
let spare = 128 - width;
let bits = value.unsigned();
let answer = match opcode {
Opcode::Ctpop => i128::from(bits.count_ones()),
Opcode::Ctlz => i128::from(bits.leading_zeros() - spare),
Opcode::Cttz => i128::from(bits.trailing_zeros().min(width)),
Opcode::Bswap if width % 8 == 0 => (bits.swap_bytes() >> spare) as i128,
Opcode::Bitreverse => (bits.reverse_bits() >> spare) as i128,
_ => return None,
};
Some(Imm::int(answer, to))
}
fn overflowed(exact: i128, to: Type, flags: Flags) -> bool {
let stored = Imm::int(exact, to);
if flags.contains(Flags::NSW) && stored.signed(to) != exact {
return true;
}
flags.contains(Flags::NUW) && (exact < 0 || stored.unsigned() != exact as u128)
}
#[cfg(test)]
mod tests {
use rucc_base::Interner;
use rucc_ir::{
Block, Builder, Extra, Flags, Func, IntPred, Module, Opcode, Signature, Type, Value,
};
use rucc_target::{Arch, Env, Os, TargetInfo, Triple};
use crate::stats::Kind;
use crate::{Fuel, Pass, fold::Fold};
fn blank() -> (Interner, Func, Block) {
let mut names = Interner::new();
let name = names.intern("f");
let mut func = Func::new(name, Signature::new().with_returns(&[Type::int(64)]));
let block = func.create_block();
(names, func, block)
}
fn fold(func: &mut Func) -> bool {
Fold.run(func, &mut crate::machine::fixtures::analyses(), &mut Fuel::unlimited()).changed()
}
fn value_of(func: &Func, value: Value, ty: Type) -> Option<i128> {
let rucc_ir::Def::Result { inst, .. } = func[value].def else { return None };
if func[inst].opcode != Opcode::IConst {
return None;
}
let Extra::Imm(at) = func[inst].extra else { return None };
Some(func[at].signed(ty))
}
#[test]
fn a_widened_constant_becomes_a_constant_of_the_wider_type() {
let (_, mut func, block) = blank();
let mut build = Builder::new(&mut func, block);
let narrow = build.iconst(Type::int(32), 7);
let wide = build.unary(Opcode::SExt, narrow, Type::int(64));
build.ret(&[wide]);
assert!(fold(&mut func));
assert_eq!(value_of(&func, wide, Type::int(64)), Some(7));
}
#[test]
fn sign_extension_copies_the_sign_and_zero_extension_does_not() {
for (opcode, expected) in [(Opcode::SExt, -1_i128), (Opcode::ZExt, 0xffff_ffff)] {
let (_, mut func, block) = blank();
let mut build = Builder::new(&mut func, block);
let narrow = build.iconst(Type::int(32), -1);
let wide = build.unary(opcode, narrow, Type::int(64));
build.ret(&[wide]);
assert!(fold(&mut func));
assert_eq!(value_of(&func, wide, Type::int(64)), Some(expected), "{opcode:?}");
}
}
#[test]
fn truncation_keeps_the_low_bits_and_reads_them_at_the_narrow_width() {
let (_, mut func, block) = blank();
let mut build = Builder::new(&mut func, block);
let wide = build.iconst(Type::int(32), 0x1234_5680);
let narrow = build.unary(Opcode::Trunc, wide, Type::int(8));
build.ret(&[narrow]);
assert!(fold(&mut func));
assert_eq!(value_of(&func, narrow, Type::int(8)), Some(-128));
}
#[test]
fn the_arithmetic_and_the_bitwise_operations_are_evaluated() {
let cases = [
(Opcode::Add, 6_i128, 7_i128, 13_i128),
(Opcode::Sub, 6, 7, -1),
(Opcode::Mul, 6, 7, 42),
(Opcode::And, 0b1100, 0b1010, 0b1000),
(Opcode::Or, 0b1100, 0b1010, 0b1110),
(Opcode::Xor, 0b1100, 0b1010, 0b0110),
];
for (opcode, a, b, want) in cases {
let (_, mut func, block) = blank();
let mut build = Builder::new(&mut func, block);
let lhs = build.iconst(Type::int(64), a);
let rhs = build.iconst(Type::int(64), b);
let out = build.binary(opcode, lhs, rhs, Flags::NONE);
build.ret(&[out]);
assert!(fold(&mut func), "{opcode:?}");
assert_eq!(value_of(&func, out, Type::int(64)), Some(want), "{opcode:?}");
}
}
#[test]
fn the_three_shifts_are_evaluated_and_the_two_right_ones_differ_on_the_sign() {
let cases = [(Opcode::Shl, -8_i128, 1_i128, -16_i128), (Opcode::AShr, -8, 1, -4)];
for (opcode, a, b, want) in cases {
let (_, mut func, block) = blank();
let mut build = Builder::new(&mut func, block);
let lhs = build.iconst(Type::int(64), a);
let rhs = build.iconst(Type::int(64), b);
let out = build.binary(opcode, lhs, rhs, Flags::NONE);
build.ret(&[out]);
assert!(fold(&mut func), "{opcode:?}");
assert_eq!(value_of(&func, out, Type::int(64)), Some(want), "{opcode:?}");
}
let (_, mut func, block) = blank();
let mut build = Builder::new(&mut func, block);
let lhs = build.iconst(Type::int(64), -8);
let rhs = build.iconst(Type::int(64), 1);
let out = build.binary(Opcode::LShr, lhs, rhs, Flags::NONE);
build.ret(&[out]);
assert!(fold(&mut func));
assert_eq!(value_of(&func, out, Type::int(64)), Some(i128::from(i64::MAX) - 3));
}
fn one(opcode: Opcode, ty: Type, arg: i128) -> Option<i128> {
let (_, mut func, block) = blank();
let mut build = Builder::new(&mut func, block);
let value = build.iconst(ty, arg);
let out = build.unary(opcode, value, ty);
build.ret(&[out]);
fold(&mut func);
value_of(&func, out, ty)
}
#[test]
fn the_bit_counts_are_evaluated_at_the_width_they_were_asked_at() {
let cases = [
(Opcode::Ctlz, 64, 0x0000_1000_0000_0000_i128, 19_i128),
(Opcode::Ctlz, 32, 0x0000_1000, 19),
(Opcode::Cttz, 64, 0x0000_1000_0000_0000, 44),
(Opcode::Cttz, 32, 0x0000_1000, 12),
(Opcode::Ctpop, 64, 0x0000_1000_0000_0000, 1),
(Opcode::Ctpop, 32, -1, 32),
(Opcode::Ctpop, 64, -1, 64),
];
for (opcode, width, arg, want) in cases {
let ty = Type::int(width);
assert_eq!(one(opcode, ty, arg), Some(want), "{opcode:?} at {width} of {arg:#x}");
}
}
#[test]
fn a_search_for_a_bit_in_a_zero_answers_the_width_the_expansion_answers() {
for width in [8_u32, 16, 32, 64] {
let ty = Type::int(width);
let want = Some(i128::from(width));
assert_eq!(one(Opcode::Ctlz, ty, 0), want, "leading, at {width}");
assert_eq!(one(Opcode::Cttz, ty, 0), want, "trailing, at {width}");
assert_eq!(one(Opcode::Ctpop, ty, 0), Some(0), "count, at {width}");
}
}
#[test]
fn the_two_reversals_are_evaluated_and_a_byte_swap_of_a_part_of_a_byte_is_not() {
let ty = Type::int(32);
assert_eq!(one(Opcode::Bswap, ty, 0x1234_5678), Some(0x7856_3412));
assert_eq!(one(Opcode::Bswap, Type::int(16), 0x1234), Some(0x3412));
assert_eq!(one(Opcode::Bitreverse, Type::int(8), 0b1010_1100), Some(0b0011_0101));
let (_, mut func, block) = blank();
let mut build = Builder::new(&mut func, block);
let value = build.iconst(Type::int(4), 0b1010);
let out = build.unary(Opcode::Bswap, value, Type::int(4));
build.ret(&[out]);
assert!(!fold(&mut func));
}
#[test]
fn a_comparison_of_two_constants_becomes_a_one_or_a_nought() {
let cases = [
(IntPred::Eq, 7_i128, 7_i128, true),
(IntPred::Eq, 7, 8, false),
(IntPred::Ne, 7, 8, true),
(IntPred::Slt, -1, 1, true),
(IntPred::Sle, -1, -1, true),
(IntPred::Sgt, -1, 1, false),
(IntPred::Sge, 1, -1, true),
(IntPred::Ult, -1, 1, false),
(IntPred::Ule, -1, 1, false),
(IntPred::Ugt, -1, 1, true),
(IntPred::Uge, -1, 1, true),
];
for (pred, a, b, want) in cases {
let (_, mut func, block) = blank();
let mut build = Builder::new(&mut func, block);
let lhs = build.iconst(Type::int(64), a);
let rhs = build.iconst(Type::int(64), b);
let out = build.icmp(pred, lhs, rhs);
build.ret(&[out]);
assert!(fold(&mut func), "{pred:?} {a} {b}");
let got = value_of(&func, out, Type::I1).expect("the comparison folded");
assert_eq!(got != 0, want, "{pred:?} {a} {b}");
}
}
#[test]
fn a_comparison_at_a_narrow_width_is_read_at_that_width() {
let ty = Type::int(8);
for (pred, want) in [(IntPred::Slt, true), (IntPred::Ult, false)] {
let (_, mut func, block) = blank();
let mut build = Builder::new(&mut func, block);
let lhs = build.iconst(ty, 255);
let rhs = build.iconst(ty, 1);
let out = build.icmp(pred, lhs, rhs);
build.ret(&[out]);
assert!(fold(&mut func), "{pred:?}");
let got = value_of(&func, out, Type::I1).expect("the comparison folded");
assert_eq!(got != 0, want, "{pred:?}");
}
}
#[test]
fn a_comparison_with_one_constant_operand_is_left_alone() {
let (_, mut func, block) = blank();
let ty = Type::int(64);
let param = func.append_param(block, ty);
let mut build = Builder::new(&mut func, block);
let rhs = build.iconst(ty, 3);
let out = build.icmp(IntPred::Eq, param, rhs);
build.ret(&[out]);
assert!(!fold(&mut func));
}
#[test]
fn a_bit_count_of_something_that_is_not_a_constant_is_left_alone() {
for opcode in [Opcode::Ctlz, Opcode::Cttz, Opcode::Ctpop, Opcode::Bswap] {
let (_, mut func, block) = blank();
let ty = Type::int(64);
let param = func.append_param(block, ty);
let mut build = Builder::new(&mut func, block);
let out = build.unary(opcode, param, ty);
build.ret(&[out]);
assert!(!fold(&mut func), "{opcode:?}");
}
}
#[test]
fn a_shift_by_the_width_or_more_is_left_alone_because_the_language_does_not_define_it() {
for count in [64_i128, 65, -1] {
let (_, mut func, block) = blank();
let mut build = Builder::new(&mut func, block);
let lhs = build.iconst(Type::int(64), 1);
let rhs = build.iconst(Type::int(64), count);
let out = build.binary(Opcode::Shl, lhs, rhs, Flags::NONE);
build.ret(&[out]);
assert!(!fold(&mut func), "a shift by {count} was folded");
}
}
#[test]
fn an_operation_that_wraps_folds_and_the_same_one_promising_it_will_not_does_not() {
let big = i128::from(i32::MAX);
for (flags, folds) in [(Flags::NONE, true), (Flags::NSW, false)] {
let (_, mut func, block) = blank();
let mut build = Builder::new(&mut func, block);
let lhs = build.iconst(Type::int(32), big);
let rhs = build.iconst(Type::int(32), 1);
let out = build.binary(Opcode::Add, lhs, rhs, flags);
build.ret(&[out]);
assert_eq!(fold(&mut func), folds, "{flags}");
if folds {
assert_eq!(value_of(&func, out, Type::int(32)), Some(i128::from(i32::MIN)));
}
}
}
#[test]
fn an_unsigned_promise_is_broken_by_a_negative_result_as_well_as_by_a_large_one() {
let (_, mut func, block) = blank();
let mut build = Builder::new(&mut func, block);
let lhs = build.iconst(Type::int(32), 1);
let rhs = build.iconst(Type::int(32), 2);
let out = build.binary(Opcode::Sub, lhs, rhs, Flags::NUW);
build.ret(&[out]);
assert!(!fold(&mut func));
}
#[test]
fn an_operation_with_one_constant_operand_is_left_alone() {
let (_, mut func, block) = blank();
let param = func.append_param(block, Type::int(64));
let mut build = Builder::new(&mut func, block);
let rhs = build.iconst(Type::int(64), 7);
let out = build.binary(Opcode::Add, param, rhs, Flags::NONE);
build.ret(&[out]);
assert!(!fold(&mut func));
assert_eq!(func[out_inst(&func, out)].opcode, Opcode::Add);
}
#[test]
fn a_divide_is_not_folded_even_when_both_operands_are_constants() {
for opcode in [Opcode::SDiv, Opcode::UDiv, Opcode::SRem, Opcode::URem] {
let (_, mut func, block) = blank();
let mut build = Builder::new(&mut func, block);
let lhs = build.iconst(Type::int(64), 42);
let rhs = build.iconst(Type::int(64), 7);
let out = build.binary(opcode, lhs, rhs, Flags::NONE);
build.ret(&[out]);
assert!(!fold(&mut func), "{opcode:?}");
}
}
#[test]
fn folding_leaves_the_function_something_the_verifier_accepts() {
let mut names = Interner::new();
let name = names.intern("f");
let mut func = Func::new(name, Signature::new().with_returns(&[Type::int(64)]));
let block = func.create_block();
let mut build = Builder::new(&mut func, block);
let narrow = build.iconst(Type::int(32), 7);
let wide = build.unary(Opcode::SExt, narrow, Type::int(64));
build.ret(&[wide]);
assert!(fold(&mut func));
let target = TargetInfo::new(Triple::new(Arch::X86_64, Os::Linux, Env::Gnu));
let module_name = names.intern("m");
let mut module = Module::new(module_name, &target);
module.add_func(func);
rucc_ir::verify(&module, &names).expect("folding does not break the IR");
}
#[test]
fn fuel_stops_the_transformation_and_not_the_walk() {
let build_two = |func: &mut Func, block: Block| {
let mut build = Builder::new(func, block);
let a = build.iconst(Type::int(32), 7);
let wide_a = build.unary(Opcode::SExt, a, Type::int(64));
let b = build.iconst(Type::int(32), 9);
let wide_b = build.unary(Opcode::SExt, b, Type::int(64));
let sum = build.binary(Opcode::Add, wide_a, wide_b, Flags::NONE);
build.ret(&[sum]);
(wide_a, wide_b)
};
let (_, mut none, block) = blank();
let (first, _) = build_two(&mut none, block);
let stats =
Fold.run(&mut none, &mut crate::machine::fixtures::analyses(), &mut Fuel::of(0));
assert!(!stats.changed());
assert_eq!(none[out_inst(&none, first)].opcode, Opcode::SExt);
assert_eq!(stats.count(Kind::Missed, super::NO_FUEL), 2);
let (_, mut one, block) = blank();
let (first, second) = build_two(&mut one, block);
let mut fuel = Fuel::of(1);
let stats = Fold.run(&mut one, &mut crate::machine::fixtures::analyses(), &mut fuel);
assert!(stats.changed());
assert_eq!(fuel.spent(), 1);
assert_eq!(stats.count(Kind::Optimized, super::FOLDED), 1);
assert_eq!(stats.count(Kind::Missed, super::NO_FUEL), 1);
assert_eq!(one[out_inst(&one, first)].opcode, Opcode::IConst);
assert_eq!(one[out_inst(&one, second)].opcode, Opcode::SExt);
}
#[test]
fn folding_one_operation_uncovers_the_next() {
let (_, mut func, block) = blank();
let mut build = Builder::new(&mut func, block);
let a = build.iconst(Type::int(32), 7);
let wide = build.unary(Opcode::SExt, a, Type::int(64));
let b = build.iconst(Type::int(64), 9);
let sum = build.binary(Opcode::Add, wide, b, Flags::NONE);
build.ret(&[sum]);
assert!(fold(&mut func));
assert_eq!(value_of(&func, sum, Type::int(64)), Some(16));
}
#[test]
fn a_constant_is_left_where_it_is_and_folding_it_again_changes_nothing() {
let (_, mut func, block) = blank();
let mut build = Builder::new(&mut func, block);
let a = build.iconst(Type::int(32), 7);
let wide = build.unary(Opcode::SExt, a, Type::int(64));
build.ret(&[wide]);
assert!(fold(&mut func));
assert!(!fold(&mut func), "a second run found something to do");
}
fn out_inst(func: &Func, value: Value) -> rucc_ir::Inst {
match func[value].def {
rucc_ir::Def::Result { inst, .. } => inst,
rucc_ir::Def::Param { .. } => panic!("a parameter has no instruction"),
}
}
}